1/*
2* Copyright (c) 2004, Bull S.A..  All rights reserved.
3* Created by: Sebastien Decugis
4
5* This program is free software; you can redistribute it and/or modify it
6* under the terms of version 2 of the GNU General Public License as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it would be useful, but
10* WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*
13* You should have received a copy of the GNU General Public License along
14* with this program; if not, write the Free Software Foundation, Inc., 59
15* Temple Place - Suite 330, Boston MA 02111-1307, USA.
16
17
18* This sample test aims to check the following assertion:
19*
20* For the SCHED_FIFO and SCHED_RR scheduling policies,
21* the child process inherits the policy and priority
22* settings of the parent process during a fork() function.
23
24* The steps are:
25* -> Change the parent's scheduling policy and parameter
26* -> fork
27* -> check the child inherited the same policy.
28
29* The test fails if the child does not inherit the parent's values.
30
31*/
32
33
34/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
35#define _POSIX_C_SOURCE 200112L
36
37/********************************************************************************************/
38/****************************** standard includes *****************************************/
39/********************************************************************************************/
40#include <pthread.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47#include <sys/wait.h>
48 #include <errno.h>
49
50#include <sched.h>
51
52/********************************************************************************************/
53/******************************   Test framework   *****************************************/
54/********************************************************************************************/
55#include "testfrmw.h"
56 #include "testfrmw.c"
57/* This header is responsible for defining the following macros:
58 * UNRESOLVED(ret, descr);
59 *    where descr is a description of the error and ret is an int (error code for example)
60 * FAILED(descr);
61 *    where descr is a short text saying why the test has failed.
62 * PASSED();
63 *    No parameter.
64 *
65 * Both three macros shall terminate the calling process.
66 * The testcase shall not terminate in any other maneer.
67 *
68 * The other file defines the functions
69 * void output_init()
70 * void output(char * string, ...)
71 *
72 * Those may be used to output information.
73 */
74
75/********************************************************************************************/
76/********************************** Configuration ******************************************/
77/********************************************************************************************/
78#ifndef VERBOSE
79#define VERBOSE 1
80#endif
81
82#define POLICY SCHED_RR
83
84/********************************************************************************************/
85/***********************************    Test case   *****************************************/
86/********************************************************************************************/
87/* The main test function. */
88int main( int argc, char * argv[] )
89{
90	int ret, param, status;
91	pid_t child, ctl;
92
93	struct sched_param sp;
94
95	/* Initialize output */
96	output_init();
97
98	/* Change process policy and parameters */
99	sp.sched_priority = param = sched_get_priority_max( POLICY );
100
101	if ( sp.sched_priority == -1 )
102	{
103		UNRESOLVED( errno, "Failed to get max priority value" );
104	}
105
106	ret = sched_setscheduler( 0, POLICY, &sp );
107
108	if ( ret == -1 )
109	{
110		UNRESOLVED( errno, "Failed to change process scheduling policy" );
111	}
112
113	/* Create the child */
114	child = fork();
115
116	if ( child == ( pid_t ) - 1 )
117	{
118		UNRESOLVED( errno, "Failed to fork" );
119	}
120
121	/* child */
122	if ( child == ( pid_t ) 0 )
123	{
124
125		/* Check the scheduling policy */
126		ret = sched_getscheduler( 0 );
127
128		if ( ret == -1 )
129		{
130			UNRESOLVED( errno, "Failed to read scheduling policy in child" );
131		}
132
133		if ( ret != POLICY )
134		{
135			FAILED( "The scheduling policy was not inherited" );
136		}
137
138		ret = sched_getparam( 0, &sp );
139
140		if ( ret != 0 )
141		{
142			UNRESOLVED( errno, "Failed to read scheduling parameter in child" );
143		}
144
145		if ( sp.sched_priority != param )
146		{
147			FAILED( "The scheduling parameter was not inherited" );
148		}
149
150		/* We're done */
151		exit( PTS_PASS );
152	}
153
154	/* Parent joins the child */
155	ctl = waitpid( child, &status, 0 );
156
157	if ( ctl != child )
158	{
159		UNRESOLVED( errno, "Waitpid returned the wrong PID" );
160	}
161
162	if ( ( !WIFEXITED( status ) ) || ( WEXITSTATUS( status ) != PTS_PASS ) )
163	{
164		FAILED( "Child exited abnormally" );
165	}
166
167
168	/* Test passed */
169#if VERBOSE > 0
170	output( "Test passed\n" );
171
172#endif
173	PASSED;
174}
175
176