blob: 5217eb7f2bb3e78dd97844442e1a2fdf243434c3 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: nice01
22 *
23 * Test Description:
24 * Verify that root can provide a negative value to nice()
25 * and hence root can decrease the nice value of the process
26 * using nice() system call
27 *
28 * Expected Result:
29 * nice() should return value 0 on success and root user should succeed
30 * to decrease the nice value of test process.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Pause for SIGUSR1 if option specified.
36 *
37 * Test:
38 * Loop if the proper options are given.
39 * Execute system call
40 * Check return code, if system call failed (return=-1)
41 * Log the errno and Issue a FAIL message.
42 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * if successful,
45 * Issue Functionality-Pass message.
46 * Otherwise,
47 * Issue Functionality-Fail message.
48 * Cleanup:
49 * Print errno log and/or timing stats if options given
50 *
51 * Usage: <for command-line>
52 * nice01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
53 * where, -c n : Run n copies concurrently.
54 * -f : Turn off functionality Testing.
55 * -i n : Execute test n times.
56 * -I x : Execute test for x seconds.
57 * -P x : Pause for x seconds between iterations.
58 * -t : Turn on syscall timing.
59 *
60 * HISTORY
61 * 07/2001 Ported by Wayne Boyer
62 *
63 * RESTRICTIONS:
64 * This test should be run by 'super-user' (root) only.
65 *
66 */
67#include <unistd.h>
68#include <sys/types.h>
69#include <errno.h>
70#include <fcntl.h>
71#include <sys/time.h>
72#include <sys/resource.h>
73
74#include "test.h"
plars865695b2001-08-27 22:15:12 +000075
subrata_modak4bb656a2009-02-26 12:02:09 +000076#define NICEINC -12
plars865695b2001-08-27 22:15:12 +000077#define TEMPFILE "temp_file"
78
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020079char *TCID = "nice01";
80int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000081
82int Org_nice; /* original priority of the test process */
83FILE *fp;
84
85void setup(); /* Main setup function of test */
86void cleanup(); /* cleanup function for the test */
87
subrata_modak56207ce2009-03-23 13:35:39 +000088int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000089{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020090 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020091 const char *msg;
plars865695b2001-08-27 22:15:12 +000092 int New_nice; /* priority of process after nice() */
93 int rval;
subrata_modak56207ce2009-03-23 13:35:39 +000094
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080095 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080096 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000097
plars865695b2001-08-27 22:15:12 +000098 setup();
99
plars865695b2001-08-27 22:15:12 +0000100 for (lc = 0; TEST_LOOPING(lc); lc++) {
101
Caspar Zhangd59a6592013-03-07 14:59:12 +0800102 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000103
subrata_modak4bb656a2009-02-26 12:02:09 +0000104 /*
plars865695b2001-08-27 22:15:12 +0000105 * Call nice(2) with an 'incr' parameter set
106 * to a negative value.
107 */
108 TEST(nice(NICEINC));
109
110 /* check return code */
111 if (TEST_RETURN == -1) {
112 tst_resm(TFAIL, "nice(%d) Failed, errno=%d : %s",
113 NICEINC, TEST_ERRNO, strerror(TEST_ERRNO));
114 continue;
115 }
116
Cyril Hrubise38b9612014-06-02 17:20:57 +0200117 New_nice = getpriority(PRIO_PROCESS, 0);
plars865695b2001-08-27 22:15:12 +0000118
Cyril Hrubise38b9612014-06-02 17:20:57 +0200119 /* Validate functionality of the nice() */
120 if (New_nice != (Org_nice + NICEINC)) {
121 tst_resm(TFAIL, "nice() fails to modify the "
122 "priority of process");
plars865695b2001-08-27 22:15:12 +0000123 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200124 tst_resm(TPASS, "Functionality of nice(%d) "
125 "successful", NICEINC);
plars865695b2001-08-27 22:15:12 +0000126 }
127
128 /* return the process to the original priority */
129 rval = nice(-NICEINC);
130
Garrett Cooper2c282152010-12-16 00:55:50 -0800131 }
plars865695b2001-08-27 22:15:12 +0000132
plars865695b2001-08-27 22:15:12 +0000133 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800134 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800135}
plars865695b2001-08-27 22:15:12 +0000136
137/*
138 * setup() - performs all ONE TIME setup for this test.
139 * Make sure the test process uid is super user.
140 * Get the current priority value.
141 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400142void setup(void)
plars865695b2001-08-27 22:15:12 +0000143{
Garrett Cooper2c282152010-12-16 00:55:50 -0800144
plars865695b2001-08-27 22:15:12 +0000145 tst_sig(NOFORK, DEF_HANDLER, cleanup);
146
147 /* Make sure the calling process is super-user only */
148 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800149 tst_brkm(TBROK, NULL, "Must be ROOT to run this test.");
plars865695b2001-08-27 22:15:12 +0000150 }
151
plars865695b2001-08-27 22:15:12 +0000152 TEST_PAUSE;
153
154 Org_nice = getpriority(PRIO_PROCESS, 0);
155}
156
157/*
158 * cleanup() - performs all ONE TIME cleanup for this test at
159 * completion or premature exit.
160 * Remove the test directory and testfile created in the setup.
161 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400162void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000163{
plars865695b2001-08-27 22:15:12 +0000164
Chris Dearmanec6edca2012-10-17 19:54:01 -0700165}