blob: 573f0788bc8f6fa79cbae736041f98c270722443 [file] [log] [blame]
vapierd13d74b2006-02-11 07:24:00 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapierd13d74b2006-02-11 07:24:00 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
vapierd13d74b2006-02-11 07:24:00 +000019 * TEST IDENTIFIER : sched_setparam05
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
vapierd13d74b2006-02-11 07:24:00 +000021 * EXECUTED BY : root/superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
vapierd13d74b2006-02-11 07:24:00 +000023 * TEST TITLE : verify that sched_setparam() fails if the user does
24 * not have proper privilages
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
vapierd13d74b2006-02-11 07:24:00 +000026 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
vapierd13d74b2006-02-11 07:24:00 +000028 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
vapierd13d74b2006-02-11 07:24:00 +000030 * SIGNALS
31 * Uses SIGUSR1 to pause before test if option set.
32 * (See the parse_opts(3) man page).
33 *
34 * DESCRIPTION
35 * Verify that sched_setparam() fails if the user does
36 * not have proper privilages
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
vapierd13d74b2006-02-11 07:24:00 +000038 * Setup:
39 * Setup signal handling.
40 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
vapierd13d74b2006-02-11 07:24:00 +000042 * Test:
43 * Loop if the proper options are given.
44 * Fork a child
45 *
46 * CHILD:
47 * Changes euid to "nobody" user.
48 * Try to Change scheduling priority for parent
49 * If call failed with errno = EPERM,
50 * Test passed
subrata_modak4bb656a2009-02-26 12:02:09 +000051 * else
vapierd13d74b2006-02-11 07:24:00 +000052 * Test failed
53 *
54 * PARENT:
55 * wait for child to finish
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
vapierd13d74b2006-02-11 07:24:00 +000057 * Cleanup:
58 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
vapierd13d74b2006-02-11 07:24:00 +000060 * USAGE: <for command-line>
61 * sched_setparam05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
62 * where, -c n : Run n copies concurrently.
63 * -e : Turn on errno logging.
64 * -h : Show help screen
65 * -f : Turn off functional testing
66 * -i n : Execute test n times.
67 * -I x : Execute test for x seconds.
68 * -p : Pause for SIGUSR1 before starting
69 * -P x : Pause for x seconds between iterations.
70 * -t : Turn on syscall timing.
71 *
72 ****************************************************************/
73
74#include <errno.h>
75#include <sched.h>
76#include <pwd.h>
77#include <sys/wait.h>
78#include "test.h"
vapierd13d74b2006-02-11 07:24:00 +000079
vapierd13d74b2006-02-11 07:24:00 +000080static void setup();
81static void cleanup();
82
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020083char *TCID = "sched_setparam05";
84int TST_TOTAL = 1;
vapierd13d74b2006-02-11 07:24:00 +000085
86static struct sched_param param = { 0 };
Wanlong Gao354ebb42012-12-07 10:10:04 +080087
vapierd13d74b2006-02-11 07:24:00 +000088static char nobody_uid[] = "nobody";
89struct passwd *ltpuser;
90
subrata_modak56207ce2009-03-23 13:35:39 +000091int main(int ac, char **av)
vapierd13d74b2006-02-11 07:24:00 +000092{
93
Cyril Hrubis89af32a2012-10-24 16:39:11 +020094 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020095 const char *msg;
vapierd13d74b2006-02-11 07:24:00 +000096 int status;
97 pid_t child_pid;
subrata_modak56207ce2009-03-23 13:35:39 +000098
Garrett Cooper53740502010-12-16 00:04:01 -080099 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800100 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +0000101
vapierd13d74b2006-02-11 07:24:00 +0000102 setup();
103
vapierd13d74b2006-02-11 07:24:00 +0000104 for (lc = 0; TEST_LOOPING(lc); lc++) {
105
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 tst_count = 0;
vapierd13d74b2006-02-11 07:24:00 +0000107
subrata_modak56207ce2009-03-23 13:35:39 +0000108 switch (child_pid = FORK_OR_VFORK()) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000109
vapierd13d74b2006-02-11 07:24:00 +0000110 case -1:
111 /* fork() failed */
112 tst_resm(TFAIL, "fork() failed");
113 continue;
subrata_modak4bb656a2009-02-26 12:02:09 +0000114
vapierd13d74b2006-02-11 07:24:00 +0000115 case 0:
116 /* Child */
117
subrata_modak56207ce2009-03-23 13:35:39 +0000118 /* Switch to nobody user */
119 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800120 tst_brkm(TBROK, NULL, "\"nobody\" user"
subrata_modak56207ce2009-03-23 13:35:39 +0000121 "not present");
vapierd13d74b2006-02-11 07:24:00 +0000122 }
123 if (seteuid(ltpuser->pw_uid) == -1) {
124 tst_resm(TWARN, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000125 "to set the effective uid to %d",
126 ltpuser->pw_uid);
vapierd13d74b2006-02-11 07:24:00 +0000127 exit(1);
128 }
129
130 /*
131 * Call sched_setparam(2) with pid = getppid()
132 */
133 TEST(sched_setparam(getppid(), &param));
134
135 if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) {
vapierd13d74b2006-02-11 07:24:00 +0000136 exit(0);
137 }
138
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 tst_resm(TWARN | TTERRNO,
140 "Test failed, sched_setparam()"
141 " returned : %ld", TEST_RETURN);
vapierd13d74b2006-02-11 07:24:00 +0000142 exit(1);
143
subrata_modak56207ce2009-03-23 13:35:39 +0000144 default:
vapierd13d74b2006-02-11 07:24:00 +0000145 /* Parent */
146 if ((waitpid(child_pid, &status, 0)) < 0) {
147 tst_resm(TFAIL, "wait() failed");
148 continue;
149 }
150 if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 0)) {
151 tst_resm(TPASS, "Test passed, Got EPERM");
152 } else {
153 tst_resm(TFAIL, "Test Failed");
154 }
155 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800156 }
vapierd13d74b2006-02-11 07:24:00 +0000157
vapierd13d74b2006-02-11 07:24:00 +0000158 cleanup();
Garrett Coopere57b3f72010-12-19 08:46:29 -0800159 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800160}
vapierd13d74b2006-02-11 07:24:00 +0000161
162/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400163void setup(void)
vapierd13d74b2006-02-11 07:24:00 +0000164{
165
Garrett Coopere57b3f72010-12-19 08:46:29 -0800166 tst_require_root(NULL);
vapierd13d74b2006-02-11 07:24:00 +0000167
vapierd13d74b2006-02-11 07:24:00 +0000168 tst_sig(FORK, DEF_HANDLER, cleanup);
169
vapierd13d74b2006-02-11 07:24:00 +0000170 TEST_PAUSE;
171
Garrett Cooper2c282152010-12-16 00:55:50 -0800172}
vapierd13d74b2006-02-11 07:24:00 +0000173
174/*
175 *cleanup() - performs all ONE TIME cleanup for this test at
176 * completion or premature exit.
177 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400178void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000179{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700180}