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
60char *TCID = "sched_setscheduler02";
61int TST_TOTAL = 1;
62
63void setup(void);
64void cleanup(void);
65
66static uid_t nobody_uid;
67
68int 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			if (seteuid(nobody_uid) == -1) {
92				tst_brkm(TBROK, cleanup, "seteuid() failed");
93			}
94
95			TEST(sched_setscheduler(pid, SCHED_FIFO, &param));
96
97			if (TEST_ERRNO) {
98			}
99
100			if (TEST_RETURN != -1) {
101				tst_resm(TFAIL, "sched_setscheduler(2) passed "
102					 "with non root priveledges");
103			} else if (TEST_ERRNO != EPERM) {
104				tst_resm(TFAIL, "Expected EPERM, got %d",
105					 TEST_ERRNO);
106			} else {
107				tst_resm(TPASS, "got EPERM");
108			}
109		} else {	/* parent */
110			/* let the child carry on */
111			wait(&status);
112			if (WIFEXITED(status) != 0) {	/* Exit with errors */
113				exit(WEXITSTATUS(status));
114			} else {
115				exit(0);
116			}
117		}
118
119		if (seteuid(0) == -1) {
120			tst_brkm(TBROK, cleanup, "seteuid(0) failed");
121		}
122	}
123	cleanup();
124	tst_exit();
125
126}
127
128/*
129 * setup() - performs all ONE TIME setup for this test.
130 */
131void setup(void)
132{
133	struct passwd *pw;
134
135	tst_require_root();
136
137	pw = SAFE_GETPWNAM(NULL, "nobody");
138	nobody_uid = pw->pw_uid;
139
140	tst_sig(FORK, DEF_HANDLER, cleanup);
141
142	TEST_PAUSE;
143}
144
145/*
146 * cleanup() - performs all ONE TIME cleanup for this test at
147 *	       completion or premature exit.
148 */
149void cleanup(void)
150{
151
152}
153