blob: a88a972ecf0f97274dde8c634378780341933af8 [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: nice02
22 *
23 * Test Description:
24 * Verify that any user can successfully increase the nice value of
25 * the process by passing a higher increment value (> max. applicable limits)
26 * to nice() system call.
27 *
28 * Expected Result:
29 * nice() should return value 0 on success and root user should succeed to
30 * increase the nice value of the 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 * nice02 [-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 * none
65 */
66#include <unistd.h>
67#include <errno.h>
68#include <fcntl.h>
69#include <sys/time.h>
70#include <sys/resource.h>
71
72#include "test.h"
plars865695b2001-08-27 22:15:12 +000073
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020074char *TCID = "nice02";
75int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000076
77#define NICEINC 50
78
79void setup(); /* Main setup function of test */
80void cleanup(); /* cleanup function for the test */
81
subrata_modak56207ce2009-03-23 13:35:39 +000082int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000083{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020084 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020085 const char *msg;
plars865695b2001-08-27 22:15:12 +000086 int New_nice; /* priority of process after nice() */
87 int max_val; /* Maximum nice value per OS. */
subrata_modak56207ce2009-03-23 13:35:39 +000088
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080089 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080090 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000091
plars865695b2001-08-27 22:15:12 +000092 setup();
93
plars865695b2001-08-27 22:15:12 +000094 for (lc = 0; TEST_LOOPING(lc); lc++) {
95
Caspar Zhangd59a6592013-03-07 14:59:12 +080096 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000097
subrata_modak4bb656a2009-02-26 12:02:09 +000098 /*
plars865695b2001-08-27 22:15:12 +000099 * Call nice(2) with an 'incr' parameter set
100 * to a higher +ve value.
101 */
102 TEST(nice(NICEINC));
103
104 /* check return code */
105 if (TEST_RETURN == -1) {
106 tst_resm(TFAIL, "nice(%d) Failed, errno=%d : %s",
107 NICEINC, TEST_ERRNO, strerror(TEST_ERRNO));
108 continue;
109 }
110
111 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200112 * Get the current priority of the test process.
plars865695b2001-08-27 22:15:12 +0000113 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200114 errno = 0;
115 New_nice = getpriority(PRIO_PROCESS, 0);
116 if (New_nice == -1 && errno != 0) {
117 tst_brkm(TFAIL, cleanup, "Fail to get priority "
118 "of process after nice()");
119 }
plars865695b2001-08-27 22:15:12 +0000120
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121 /*
122 * Validate functionality of the nice().
123 *
124 * Default priority is 0, Max is 20.
125 */
126 max_val = 20;
plars865695b2001-08-27 22:15:12 +0000127
Cyril Hrubise38b9612014-06-02 17:20:57 +0200128 if (New_nice != (max_val - 1)) {
129 tst_resm(TFAIL, "Priority of process : %d "
130 "doesn't match the expected:%d",
131 New_nice, (max_val - 1));
plars865695b2001-08-27 22:15:12 +0000132 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200133 tst_resm(TPASS, "Functionality of nice(%d)"
134 " successful", NICEINC);
plars865695b2001-08-27 22:15:12 +0000135 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800136 }
plars865695b2001-08-27 22:15:12 +0000137
plars865695b2001-08-27 22:15:12 +0000138 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800139 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800140}
plars865695b2001-08-27 22:15:12 +0000141
142/*
143 * setup() - performs all ONE TIME setup for this test.
144 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400145void setup(void)
plars865695b2001-08-27 22:15:12 +0000146{
Garrett Cooper2c282152010-12-16 00:55:50 -0800147
plars865695b2001-08-27 22:15:12 +0000148 tst_sig(NOFORK, DEF_HANDLER, cleanup);
149
plars865695b2001-08-27 22:15:12 +0000150 TEST_PAUSE;
151}
152
153/*
154 * cleanup() - performs all ONE TIME cleanup for this test at
155 * completion or premature exit.
156 * Remove the test directory and testfile created in the setup.
157 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400158void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000159{
plars865695b2001-08-27 22:15:12 +0000160
Chris Dearmanec6edca2012-10-17 19:54:01 -0700161}