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  *	setpriority02.c
23  *
24  * DESCRIPTION
25  *	setpriority02 - test for an expected failure by trying to raise
26  *			the priority for the test process while not having
27  *			permissions to do so.
28  *
29  * ALGORITHM
30  *	loop if that option was specified
31  *	issue the system call
32  *	check the errno value
33  *	  issue a PASS message if we get EACCESS - errno 13
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  *  setpriority02 [-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 = "setpriority02";
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 
75 	tst_parse_opts(ac, av, NULL, NULL);
76 
77 	setup();		/* global setup */
78 
79 	/* The following loop checks looping state if -i option given */
80 
81 	for (lc = 0; TEST_LOOPING(lc); lc++) {
82 		/* reset tst_count in case we are looping */
83 		tst_count = 0;
84 
85 		/*
86 		 * Try to raise the priority of this process from a default of
87 		 * 0 to a new value of -2.  This should give an EACCES error.
88 		 */
89 
90 		/* call the system call with the TEST() macro */
91 		TEST(setpriority(PRIO_PROCESS, 0, new_val));
92 
93 		if (TEST_RETURN == 0) {
94 			tst_resm(TFAIL, "call failed to produce expected error "
95 				 "- errno = %d - %s", TEST_ERRNO,
96 				 strerror(TEST_ERRNO));
97 			continue;
98 		}
99 
100 		switch (TEST_ERRNO) {
101 		case EACCES:
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 (seteuid(ltpuser->pw_uid) == -1) {
126 		tst_resm(TINFO, "setreuid failed to "
127 			 "to set the effective uid to %d", ltpuser->pw_uid);
128 		perror("setreuid");
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