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  *	sched_setscheduler01.c
23  *
24  * DESCRIPTION
25  *	Testcase to test whether sched_setscheduler(2) sets the errnos
26  *	correctly.
27  *
28  * ALGORITHM
29  *	1.	Call sched_setscheduler as a non-root uid, and expect EPERM
30  *	to be returned.
31  *
32  * USAGE:  <for command-line>
33  *  sched_setscheduler02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
34  *     where,  -c n : Run n copies concurrently.
35  *             -e   : Turn on errno logging.
36  *             -i n : Execute test n times.
37  *             -I x : Execute test for x seconds.
38  *             -P x : Pause for x seconds between iterations.
39  *             -t   : Turn on syscall timing.
40  *
41  * HISTORY
42  *	07/2001 Ported by Wayne Boyer
43  *
44  * RESTRICTIONS
45  *	Must run test as root.
46  */
47 #include <stdio.h>
48 #include <errno.h>
49 #include <sched.h>
50 #include <pwd.h>
51 #include <sys/types.h>
52 #include <sys/wait.h>
53 
54 #include "test.h"
55 #include "safe_macros.h"
56 
57 #define SCHED_INVALID	99
58 #define INVALID_PID	999999
59 
60 char *TCID = "sched_setscheduler02";
61 int TST_TOTAL = 1;
62 
63 void setup(void);
64 void cleanup(void);
65 
66 static uid_t nobody_uid;
67 
main(int ac,char ** av)68 int main(int ac, char **av)
69 {
70 	int lc;
71 	pid_t pid;
72 	struct sched_param param;
73 	int status;
74 
75 	tst_parse_opts(ac, av, NULL, NULL);
76 
77 	setup();
78 
79 	for (lc = 0; TEST_LOOPING(lc); lc++) {
80 
81 		/* reset tst_count in case we are looping */
82 		tst_count = 0;
83 
84 		if ((pid = FORK_OR_VFORK()) == -1) {
85 			tst_brkm(TBROK, cleanup, "fork failed");
86 		}
87 
88 		if (pid == 0) {	/* child */
89 			param.sched_priority = 1;
90 
91 			SAFE_SETEUID(cleanup, nobody_uid);
92 
93 			TEST(sched_setscheduler(pid, SCHED_FIFO, &param));
94 
95 			if (TEST_ERRNO) {
96 			}
97 
98 			if (TEST_RETURN != -1) {
99 				tst_resm(TFAIL, "sched_setscheduler(2) passed "
100 					 "with non root priveledges");
101 			} else if (TEST_ERRNO != EPERM) {
102 				tst_resm(TFAIL, "Expected EPERM, got %d",
103 					 TEST_ERRNO);
104 			} else {
105 				tst_resm(TPASS, "got EPERM");
106 			}
107 		} else {	/* parent */
108 			/* let the child carry on */
109 			wait(&status);
110 			if (WIFEXITED(status) != 0) {	/* Exit with errors */
111 				exit(WEXITSTATUS(status));
112 			} else {
113 				exit(0);
114 			}
115 		}
116 
117 		SAFE_SETEUID(cleanup, 0);
118 	}
119 	cleanup();
120 	tst_exit();
121 
122 }
123 
124 /*
125  * setup() - performs all ONE TIME setup for this test.
126  */
setup(void)127 void setup(void)
128 {
129 	struct passwd *pw;
130 
131 	tst_require_root();
132 
133 	pw = SAFE_GETPWNAM(NULL, "nobody");
134 	nobody_uid = pw->pw_uid;
135 
136 	tst_sig(FORK, DEF_HANDLER, cleanup);
137 
138 	TEST_PAUSE;
139 }
140 
141 /*
142  * cleanup() - performs all ONE TIME cleanup for this test at
143  *	       completion or premature exit.
144  */
cleanup(void)145 void cleanup(void)
146 {
147 
148 }
149