1 /*
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
13 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 */
16 /**********************************************************
17 *
18 * TEST IDENTIFIER : sched_setparam02
19 *
20 * EXECUTED BY : root / superuser
21 *
22 * TEST TITLE : Checks functionality for sched_setparam(2)
23 *
24 * TEST CASE TOTAL : 1
25 *
26 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
27 *
28 * SIGNALS
29 * Uses SIGUSR1 to pause before test if option set.
30 * (See the parse_opts(3) man page).
31 *
32 * DESCRIPTION
33 * This test changes the scheduling priority for current process
34 * and verifies it by calling sched_getparam().
35 *
36 * Setup:
37 * Setup signal handling.
38 * Pause for SIGUSR1 if option specified.
39 * Change scheduling policy to SCHED_FIFO
40 *
41 * Test:
42 * Loop if the proper options are given.
43 * Execute system call
44 * If scheduling priority is set properly,
45 * TEST passed
46 * else
47 * TEST failed
48 *
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 *
52 * USAGE: <for command-line>
53 * sched_setparam02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
54 * where, -c n : Run n copies concurrently.
55 * -e : Turn on errno logging.
56 * -h : Show help screen
57 * -f : Turn off functional testing
58 * -i n : Execute test n times.
59 * -I x : Execute test for x seconds.
60 * -p : Pause for SIGUSR1 before starting
61 * -P x : Pause for x seconds between iterations.
62 * -t : Turn on syscall timing.
63 *
64 ****************************************************************/
65
66 #include <errno.h>
67 #include <sched.h>
68 #include "test.h"
69
70 #define FIFO_OR_RR_PRIO 5
71 #define OTHER_PRIO 0
72
73 static void setup();
74 static void cleanup();
75 static int verify_priority(int);
76
77 char *TCID = "sched_setparam02";
78
79 static struct sched_param param;
80 static struct sched_param param1 = { 1 };
81
82 static struct test_cases_t {
83 char *desc;
84 int policy;
85 int priority;
86 } testcases[] = {
87 {
88 "Test with policy SCHED_FIFO", SCHED_FIFO, FIFO_OR_RR_PRIO}, {
89 "Test with policy SCHED_RR", SCHED_RR, FIFO_OR_RR_PRIO}, {
90 "Test with SCHED_OTHER", SCHED_OTHER, OTHER_PRIO}
91 };
92
93 int TST_TOTAL = sizeof(testcases) / sizeof(testcases[0]);
94
main(int ac,char ** av)95 int main(int ac, char **av)
96 {
97
98 int lc, i;
99
100 tst_parse_opts(ac, av, NULL, NULL);
101
102 setup();
103
104 for (lc = 0; TEST_LOOPING(lc); lc++) {
105
106 tst_count = 0;
107
108 for (i = 0; i < TST_TOTAL; ++i) {
109
110 if (i == 2) {
111 param1.sched_priority = 0;
112 } else {
113 param1.sched_priority = 1;
114 }
115 if ((sched_setscheduler(0, testcases[i].policy,
116 ¶m1)) == -1) {
117 tst_brkm(TBROK, cleanup, "sched_setscheduler()"
118 " failed");
119 }
120 param.sched_priority = testcases[i].priority;
121 /*
122 * Call sched_setparam(2) with pid=0 sothat it will
123 * set the scheduling parameters for the calling process
124 */
125 TEST(sched_setparam(0, ¶m));
126
127 if ((TEST_RETURN == 0) && (verify_priority(i))) {
128 tst_resm(TPASS, "%s Passed", testcases[i].desc);
129 } else {
130 tst_resm(TFAIL | TTERRNO,
131 "%s Failed. sched_setparam()"
132 " returned %ld", testcases[i].desc,
133 TEST_RETURN);
134 }
135 }
136 }
137
138 /* cleanup and exit */
139 cleanup();
140
141 tst_exit();
142
143 }
144
145 /* setup() - performs all ONE TIME setup for this test */
setup(void)146 void setup(void)
147 {
148 tst_require_root();
149
150 tst_sig(NOFORK, DEF_HANDLER, cleanup);
151
152 TEST_PAUSE;
153
154 }
155
156 /*
157 *cleanup() - performs all ONE TIME cleanup for this test at
158 * completion or premature exit.
159 */
cleanup(void)160 void cleanup(void)
161 {
162 }
163
164 /*
165 * verify_priority() - This function checks whether the priority is
166 * set correctly
167 */
verify_priority(int i)168 int verify_priority(int i)
169 {
170 struct sched_param p;
171
172 if ((sched_getparam(0, &p)) == 0) {
173 if (p.sched_priority == testcases[i].priority) {
174 return 1;
175 } else {
176 tst_resm(TWARN, "sched_getparam() returned priority"
177 " value as %d", p.sched_priority);
178 return 0;
179 }
180 }
181
182 tst_resm(TWARN, "sched_getparam() failed");
183 return 0;
184 }
185