1 /*
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
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"
56 
57 #include <errno.h>
58 #include <sys/time.h>
59 #include <sys/resource.h>
60 #include <pwd.h>
61 
62 void cleanup(void);
63 void setup(void);
64 
65 char *TCID = "setpriority05";
66 int TST_TOTAL = 1;
67 char nobody_uid[] = "nobody";
68 struct passwd *ltpuser;
69 
main(int ac,char ** av)70 int main(int ac, char **av)
71 {
72 	int lc;
73 	int new_val = 2;
74 	int init_val = 1;	/* the init process = id 1 */
75 
76 	tst_parse_opts(ac, av, NULL, NULL);
77 
78 	setup();		/* global setup */
79 
80 	/* The following loop checks looping state if -i option given */
81 
82 	for (lc = 0; TEST_LOOPING(lc); lc++) {
83 		/* reset tst_count in case we are looping */
84 		tst_count = 0;
85 
86 		/*
87 		 * Try to access the init process.
88 		 * This should give an EPERM error.
89 		 */
90 
91 		/* call the system call with the TEST() macro */
92 		TEST(setpriority(PRIO_PROCESS, init_val, new_val));
93 
94 		if (TEST_RETURN == 0) {
95 			tst_resm(TFAIL, "call failed to produce expected error "
96 				 "- errno = %d - %s", TEST_ERRNO,
97 				 strerror(TEST_ERRNO));
98 		}
99 
100 		switch (TEST_ERRNO) {
101 		case EPERM:
102 			tst_resm(TPASS, "expected failure - errno = %d - %s",
103 				 TEST_ERRNO, strerror(TEST_ERRNO));
104 			break;
105 		default:
106 			tst_resm(TFAIL, "call failed to produce expected error "
107 				 "- errno = %d - %s", TEST_ERRNO,
108 				 strerror(TEST_ERRNO));
109 		}
110 	}
111 	cleanup();
112 	tst_exit();
113 
114 }
115 
116 /*
117  * setup() - performs all the ONE TIME setup for this test.
118  */
setup(void)119 void setup(void)
120 {
121 	tst_require_root();
122 
123 	/* Switch to nobody user for correct error code collection */
124 	ltpuser = getpwnam(nobody_uid);
125 	if (setuid(ltpuser->pw_uid) == -1) {
126 		tst_resm(TINFO, "setuid failed to "
127 			 "to set the effective uid to %d", ltpuser->pw_uid);
128 		perror("setuid");
129 	}
130 
131 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
132 
133 	TEST_PAUSE;
134 }
135 
136 /*
137  * cleanup() - performs all the ONE TIME cleanup for this test at completion
138  *	       or premature exit.
139  */
cleanup(void)140 void cleanup(void)
141 {
142 
143 }
144