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  *
19  *    TEST IDENTIFIER	: sched_setparam03
20  *
21  *    EXECUTED BY	: root / superuser
22  *
23  *    TEST TITLE	: Checks functionality for sched_setparam(2) for pid!=0
24  *
25  *    TEST CASE TOTAL	: 1
26  *
27  *    AUTHOR		: Saji Kumar.V.R <saji.kumar@wipro.com>
28  *
29  *    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
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  *	 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
53  *
54  * 	Cleanup:
55  * 	  Print errno log and/or timing stats if options given
56  *
57  * 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 
71 #include <err.h>
72 #include <errno.h>
73 #include <sched.h>
74 #include <sys/wait.h>
75 #include "test.h"
76 
77 #define NEW_PRIORITY 5
78 
79 static void setup();
80 static void cleanup();
81 static int verify_priority();
82 
83 char *TCID = "sched_setparam03";
84 int TST_TOTAL = 1;
85 
86 static struct sched_param param = { NEW_PRIORITY };
87 
88 int main(int ac, char **av)
89 {
90 
91 	int lc;
92 	int status;
93 	pid_t child_pid;
94 
95 	tst_parse_opts(ac, av, NULL, NULL);
96 
97 	setup();
98 
99 	for (lc = 0; TEST_LOOPING(lc); lc++) {
100 
101 		tst_count = 0;
102 
103 		switch (child_pid = FORK_OR_VFORK()) {
104 
105 		case -1:
106 			/* fork() failed */
107 			tst_resm(TFAIL, "fork() failed");
108 			continue;
109 
110 		case 0:
111 			/* Child */
112 
113 			/*
114 			 * Call sched_setparam(2) with pid = getppid() so that
115 			 * it will set the scheduling parameters for parent
116 			 * process
117 			 */
118 			TEST(sched_setparam(getppid(), &param));
119 
120 			if (TEST_RETURN == -1) {
121 				err(0, "sched_setparam returned %ld",
122 				    TEST_RETURN);
123 			}
124 			exit(1);
125 
126 		default:
127 			/* Parent */
128 			if ((waitpid(child_pid, &status, 0)) < 0) {
129 				tst_resm(TFAIL, "wait() failed");
130 				continue;
131 			}
132 
133 			/*
134 			 * Verify that parent's scheduling priority has
135 			 * changed.
136 			 */
137 			if ((WIFEXITED(status)) && (WEXITSTATUS(status)) &&
138 			    (verify_priority())) {
139 				tst_resm(TPASS, "Test Passed");
140 			} else {
141 				tst_resm(TFAIL, "Test Failed");
142 			}
143 		}
144 	}
145 
146 	cleanup();
147 	tst_exit();
148 }
149 
150 /* setup() - performs all ONE TIME setup for this test */
151 void setup(void)
152 {
153 	struct sched_param p = { 1 };
154 
155 	tst_require_root();
156 
157 	tst_sig(FORK, DEF_HANDLER, cleanup);
158 
159 	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 
166 }
167 
168 /*
169  *cleanup() -   performs all ONE TIME cleanup for this test at
170  *		completion or premature exit.
171  */
172 void cleanup(void)
173 {
174 }
175 
176 /*
177  * verify_priority() -  This function checks whether the priority is
178  *			set correctly
179  */
180 int verify_priority(void)
181 {
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"
189 				 " value as %d", p.sched_priority);
190 			return 0;
191 		}
192 	}
193 
194 	tst_resm(TWARN, "sched_getparam() failed");
195 	return 0;
196 }
197