blob: 96b7ec31d564ef2c046f7ad69959ac0fad9a6a8e [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: nice04
22 *
23 * Test Description:
24 * Verify that, nice(2) fails when, a non-root user attempts to increase
25 * the priority of a process by specifying a negative increment value.
26 *
27 * Expected Result:
28 * nice() returns -1 and sets errno to EPERM.
29 *
30 * Algorithm:
31 * Setup:
32 * Setup signal handling.
33 * Pause for SIGUSR1 if option specified.
34 *
35 * Test:
36 * Loop if the proper options are given.
37 * Execute system call
38 * Check return code, if system call failed (return=-1)
39 * if errno set == expected errno
40 * PASS
41 * Issue sys call fails with expected return value and errno.
42 * Otherwise,
43 * FAIL
44 * Issue sys call fails with unexpected errno.
45 * Otherwise,
46 * Issue sys call returns unexpected value.
47 *
48 * Cleanup:
49 * Print errno log and/or timing stats if options given
50 *
51 * Usage: <for command-line>
52 * nice04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
53 * where, -c n : Run n copies concurrently.
54 * -e : Turn on errno logging.
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 executed by 'non-super-user' only.
65 */
robbiewc277a362001-09-12 18:44:14 +000066#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000067#include <unistd.h>
68#include <errno.h>
69
70#include "test.h"
plars865695b2001-08-27 22:15:12 +000071
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020072char *TCID = "nice04";
73int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000074
robbiewc277a362001-09-12 18:44:14 +000075char nobody_uid[] = "nobody";
76struct passwd *ltpuser;
77
subrata_modak56207ce2009-03-23 13:35:39 +000078struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +000079 int nice_val;
80 char *desc;
81 int exp_errno;
82} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000083 {
84 -5, "Non-root cannot specify higher priority", EPERM}
plars865695b2001-08-27 22:15:12 +000085};
86
87void setup(); /* Main setup function of test */
88void cleanup(); /* cleanup function for the test */
89
subrata_modak56207ce2009-03-23 13:35:39 +000090int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000091{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020092 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020093 const char *msg;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020094 int i;
plars865695b2001-08-27 22:15:12 +000095 int incr_val; /* nice value for the process */
subrata_modak56207ce2009-03-23 13:35:39 +000096 char *test_desc; /* test specific error message */
97
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080098 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000100
plars865695b2001-08-27 22:15:12 +0000101 setup();
102
plars865695b2001-08-27 22:15:12 +0000103 for (lc = 0; TEST_LOOPING(lc); lc++) {
104
Caspar Zhangd59a6592013-03-07 14:59:12 +0800105 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000106
subrata_modak56207ce2009-03-23 13:35:39 +0000107 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000108 incr_val = Test_cases[i].nice_val;
109 test_desc = Test_cases[i].desc;
110
subrata_modak4bb656a2009-02-26 12:02:09 +0000111 /*
plars865695b2001-08-27 22:15:12 +0000112 * Call nice(2) with different 'incr' parameter
113 * values and verify that it fails as expected.
114 */
115 TEST(nice(incr_val));
116
117 /* check return code from nice(2) */
118 if (TEST_RETURN == -1) {
subrata_modak923b23f2009-11-02 13:57:16 +0000119 tst_resm(TPASS, "nice(2) returned %ld for %s",
plars865695b2001-08-27 22:15:12 +0000120 TEST_RETURN, test_desc);
121 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 tst_resm(TFAIL | TTERRNO,
subrata_modak923b23f2009-11-02 13:57:16 +0000123 "nice() returned %ld for %s",
124 TEST_RETURN, test_desc);
plars865695b2001-08-27 22:15:12 +0000125 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800126 }
127 }
plars865695b2001-08-27 22:15:12 +0000128
plars865695b2001-08-27 22:15:12 +0000129 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800130 tst_exit();
plars865695b2001-08-27 22:15:12 +0000131
Garrett Cooper2c282152010-12-16 00:55:50 -0800132}
plars865695b2001-08-27 22:15:12 +0000133
134/*
135 * setup() - performs all ONE TIME setup for this test.
136 * Make sure the test process uid is non-root only.
137 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400138void setup(void)
plars865695b2001-08-27 22:15:12 +0000139{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200140 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800141
plars865695b2001-08-27 22:15:12 +0000142 tst_sig(NOFORK, DEF_HANDLER, cleanup);
143
subrata_modak56207ce2009-03-23 13:35:39 +0000144 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000145 ltpuser = getpwnam(nobody_uid);
146 if (setuid(ltpuser->pw_uid) == -1) {
147 tst_resm(TINFO, "setuid failed to "
148 "to set the effective uid to %d", ltpuser->pw_uid);
149 perror("setuid");
150 }
plars865695b2001-08-27 22:15:12 +0000151
plars865695b2001-08-27 22:15:12 +0000152 TEST_PAUSE;
153}
154
155/*
156 * cleanup() - performs all ONE TIME cleanup for this test at
157 * completion or premature exit.
158 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400159void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000160{
plars865695b2001-08-27 22:15:12 +0000161
Chris Dearmanec6edca2012-10-17 19:54:01 -0700162}