blob: b4a592f242551048fa4b62d4b20a8b829b422ae9 [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_setparam03
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 : Checks functionality for sched_setparam(2) for pid!=0
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
vapierd13d74b2006-02-11 07:24:00 +000025 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
vapierd13d74b2006-02-11 07:24:00 +000027 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
vapierd13d74b2006-02-11 07:24:00 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
34 * This test forks a child & changes its parent's scheduling priority
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
vapierd13d74b2006-02-11 07:24:00 +000036 * Setup:
37 * Setup signal handling.
38 * Pause for SIGUSR1 if option specified.
39 * Change scheduling policy to SCHED_FIFO
subrata_modak4bb656a2009-02-26 12:02:09 +000040 *
vapierd13d74b2006-02-11 07:24:00 +000041 * Test:
42 * Loop if the proper options are given.
43 * Fork a child
44 *
45 * CHILD:
46 * Changes scheduling priority for parent
47 *
48 * PARENT:
49 * If scheduling priority is set properly,
50 * TEST passed
51 * else
52 * TEST failed
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
vapierd13d74b2006-02-11 07:24:00 +000054 * Cleanup:
55 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
vapierd13d74b2006-02-11 07:24:00 +000057 * USAGE: <for command-line>
58 * sched_setparam03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
59 * where, -c n : Run n copies concurrently.
60 * -e : Turn on errno logging.
61 * -h : Show help screen
62 * -f : Turn off functional testing
63 * -i n : Execute test n times.
64 * -I x : Execute test for x seconds.
65 * -p : Pause for SIGUSR1 before starting
66 * -P x : Pause for x seconds between iterations.
67 * -t : Turn on syscall timing.
68 *
69 ****************************************************************/
70
Garrett Coopere57b3f72010-12-19 08:46:29 -080071#include <err.h>
vapierd13d74b2006-02-11 07:24:00 +000072#include <errno.h>
73#include <sched.h>
74#include <sys/wait.h>
75#include "test.h"
vapierd13d74b2006-02-11 07:24:00 +000076
77#define NEW_PRIORITY 5
78
79static void setup();
80static void cleanup();
81static int verify_priority();
82
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020083char *TCID = "sched_setparam03";
84int TST_TOTAL = 1;
vapierd13d74b2006-02-11 07:24:00 +000085
86static struct sched_param param = { NEW_PRIORITY };
87
subrata_modak56207ce2009-03-23 13:35:39 +000088int main(int ac, char **av)
vapierd13d74b2006-02-11 07:24:00 +000089{
90
Cyril Hrubis89af32a2012-10-24 16:39:11 +020091 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020092 const char *msg;
vapierd13d74b2006-02-11 07:24:00 +000093 int status;
94 pid_t child_pid;
95
Garrett Cooper53740502010-12-16 00:04:01 -080096 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080097 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +000098
vapierd13d74b2006-02-11 07:24:00 +000099 setup();
100
vapierd13d74b2006-02-11 07:24:00 +0000101 for (lc = 0; TEST_LOOPING(lc); lc++) {
102
Caspar Zhangd59a6592013-03-07 14:59:12 +0800103 tst_count = 0;
vapierd13d74b2006-02-11 07:24:00 +0000104
subrata_modak56207ce2009-03-23 13:35:39 +0000105 switch (child_pid = FORK_OR_VFORK()) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000106
vapierd13d74b2006-02-11 07:24:00 +0000107 case -1:
108 /* fork() failed */
109 tst_resm(TFAIL, "fork() failed");
110 continue;
subrata_modak4bb656a2009-02-26 12:02:09 +0000111
vapierd13d74b2006-02-11 07:24:00 +0000112 case 0:
113 /* Child */
114
115 /*
116 * Call sched_setparam(2) with pid = getppid() so that
117 * it will set the scheduling parameters for parent
118 * process
119 */
120 TEST(sched_setparam(getppid(), &param));
121
122 if (TEST_RETURN == -1) {
Garrett Coopere57b3f72010-12-19 08:46:29 -0800123 err(0, "sched_setparam returned %ld",
124 TEST_RETURN);
vapierd13d74b2006-02-11 07:24:00 +0000125 }
126 exit(1);
127
subrata_modak56207ce2009-03-23 13:35:39 +0000128 default:
vapierd13d74b2006-02-11 07:24:00 +0000129 /* Parent */
130 if ((waitpid(child_pid, &status, 0)) < 0) {
131 tst_resm(TFAIL, "wait() failed");
132 continue;
133 }
subrata_modak56207ce2009-03-23 13:35:39 +0000134
vapierd13d74b2006-02-11 07:24:00 +0000135 /*
136 * Verify that parent's scheduling priority has
137 * changed.
138 */
139 if ((WIFEXITED(status)) && (WEXITSTATUS(status)) &&
subrata_modak56207ce2009-03-23 13:35:39 +0000140 (verify_priority())) {
vapierd13d74b2006-02-11 07:24:00 +0000141 tst_resm(TPASS, "Test Passed");
142 } else {
143 tst_resm(TFAIL, "Test Failed");
144 }
145 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800146 }
vapierd13d74b2006-02-11 07:24:00 +0000147
vapierd13d74b2006-02-11 07:24:00 +0000148 cleanup();
Garrett Coopere57b3f72010-12-19 08:46:29 -0800149 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800150}
vapierd13d74b2006-02-11 07:24:00 +0000151
152/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400153void setup(void)
vapierd13d74b2006-02-11 07:24:00 +0000154{
155 struct sched_param p = { 1 };
subrata_modakbdbaec52009-02-26 12:14:51 +0000156
vapierd13d74b2006-02-11 07:24:00 +0000157 tst_sig(FORK, DEF_HANDLER, cleanup);
158
vapierd13d74b2006-02-11 07:24:00 +0000159 TEST_PAUSE;
160
161 /* Change scheduling policy to SCHED_FIFO */
162 if ((sched_setscheduler(0, SCHED_FIFO, &p)) == -1) {
163 tst_brkm(TBROK, cleanup, "sched_setscheduler() failed");
164 }
165
Garrett Cooper2c282152010-12-16 00:55:50 -0800166}
vapierd13d74b2006-02-11 07:24:00 +0000167
168/*
169 *cleanup() - performs all ONE TIME cleanup for this test at
170 * completion or premature exit.
171 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400172void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000173{
Garrett Cooper2c282152010-12-16 00:55:50 -0800174}
vapierd13d74b2006-02-11 07:24:00 +0000175
176/*
177 * verify_priority() - This function checks whether the priority is
178 * set correctly
subrata_modak56207ce2009-03-23 13:35:39 +0000179 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400180int verify_priority(void)
vapierd13d74b2006-02-11 07:24:00 +0000181{
182 struct sched_param p;
183
184 if ((sched_getparam(0, &p)) == 0) {
185 if (p.sched_priority == NEW_PRIORITY) {
186 return 1;
187 } else {
188 tst_resm(TWARN, "sched_getparam() returned priority"
subrata_modak56207ce2009-03-23 13:35:39 +0000189 " value as %d", p.sched_priority);
vapierd13d74b2006-02-11 07:24:00 +0000190 return 0;
191 }
192 }
193
194 tst_resm(TWARN, "sched_getparam() failed");
195 return 0;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700196}