blob: 971c4d5c27624396051d1ceeebaf5852dddc505f [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 * NAME
22 * setpriority05.c
23 *
24 * DESCRIPTION
25 * setpriority05 - test for an expected failure by trying to change
26 * a process with an ID that is different from the
27 * test process
28 *
29 * ALGORITHM
30 * loop if that option was specified
31 * issue the system call with init's process id (1)
32 * check the errno value
33 * issue a PASS message if we get EPERM - errno 1
34 * otherwise, the tests fails
35 * issue a FAIL message
36 * break any remaining tests
37 * call cleanup
38 *
39 * USAGE: <for command-line>
40 * setpriority05 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
41 * where, -c n : Run n copies concurrently.
42 * -e : Turn on errno logging.
43 * -i n : Execute test n times.
44 * -I x : Execute test for x seconds.
45 * -P x : Pause for x seconds between iterations.
46 * -t : Turn on syscall timing.
47 *
48 * HISTORY
49 * 03/2001 - Written by Wayne Boyer
50 *
51 * RESTRICTIONS
52 * none
53 */
54
55#include "test.h"
plars865695b2001-08-27 22:15:12 +000056
57#include <errno.h>
58#include <sys/time.h>
59#include <sys/resource.h>
robbiew5901ff42001-08-28 15:11:41 +000060#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000061
62void cleanup(void);
63void setup(void);
64
subrata_modak56207ce2009-03-23 13:35:39 +000065char *TCID = "setpriority05";
plars865695b2001-08-27 22:15:12 +000066int TST_TOTAL = 1;
robbiew5901ff42001-08-28 15:11:41 +000067char nobody_uid[] = "nobody";
68struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000069
plars74948ad2002-11-14 16:16:14 +000070int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000071{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020072 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020073 const char *msg;
plars865695b2001-08-27 22:15:12 +000074 int new_val = 2;
subrata_modak56207ce2009-03-23 13:35:39 +000075 int init_val = 1; /* the init process = id 1 */
plars865695b2001-08-27 22:15:12 +000076
Garrett Cooper45e285d2010-11-22 12:19:25 -080077 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080078 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000079 }
80
subrata_modak56207ce2009-03-23 13:35:39 +000081 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000082
83 /* The following loop checks looping state if -i option given */
84
85 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080086 /* reset tst_count in case we are looping */
87 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000088
89 /*
90 * Try to access the init process.
91 * This should give an EPERM error.
92 */
subrata_modakbdbaec52009-02-26 12:14:51 +000093
plars865695b2001-08-27 22:15:12 +000094 /* call the system call with the TEST() macro */
95 TEST(setpriority(PRIO_PROCESS, init_val, new_val));
subrata_modakbdbaec52009-02-26 12:14:51 +000096
plars865695b2001-08-27 22:15:12 +000097 if (TEST_RETURN == 0) {
subrata_modak56207ce2009-03-23 13:35:39 +000098 tst_resm(TFAIL, "call failed to produce expected error "
plars865695b2001-08-27 22:15:12 +000099 "- errno = %d - %s", TEST_ERRNO,
100 strerror(TEST_ERRNO));
101 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000102
plars865695b2001-08-27 22:15:12 +0000103 switch (TEST_ERRNO) {
104 case EPERM:
105 tst_resm(TPASS, "expected failure - errno = %d - %s",
106 TEST_ERRNO, strerror(TEST_ERRNO));
107 break;
108 default:
109 tst_resm(TFAIL, "call failed to produce expected error "
110 "- errno = %d - %s", TEST_ERRNO,
111 strerror(TEST_ERRNO));
112 }
113 }
114 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800115 tst_exit();
plars865695b2001-08-27 22:15:12 +0000116
plars865695b2001-08-27 22:15:12 +0000117}
118
119/*
120 * setup() - performs all the ONE TIME setup for this test.
121 */
subrata_modak56207ce2009-03-23 13:35:39 +0000122void setup(void)
plars865695b2001-08-27 22:15:12 +0000123{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200124 tst_require_root(NULL);
125
robbiew5901ff42001-08-28 15:11:41 +0000126 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000127 ltpuser = getpwnam(nobody_uid);
128 if (setuid(ltpuser->pw_uid) == -1) {
129 tst_resm(TINFO, "setuid failed to "
130 "to set the effective uid to %d", ltpuser->pw_uid);
131 perror("setuid");
132 }
robbiew5901ff42001-08-28 15:11:41 +0000133
plars865695b2001-08-27 22:15:12 +0000134 tst_sig(NOFORK, DEF_HANDLER, cleanup);
135
plars865695b2001-08-27 22:15:12 +0000136 TEST_PAUSE;
137}
138
139/*
140 * cleanup() - performs all the ONE TIME cleanup for this test at completion
subrata_modak56207ce2009-03-23 13:35:39 +0000141 * or premature exit.
plars865695b2001-08-27 22:15:12 +0000142 */
subrata_modak56207ce2009-03-23 13:35:39 +0000143void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000144{
plars865695b2001-08-27 22:15:12 +0000145
Chris Dearmanec6edca2012-10-17 19:54:01 -0700146}