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* Interval timers are reset in the child process.
21
22* The steps are:
23* -> create an interval timer in the parent process
24* -> fork
25* -> check the timer has been cleared in child.
26
27* The test fails if the timer is running in the child.
28
29*/
30
31
32/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
33#define _POSIX_C_SOURCE 200112L
34
35/* The interval timers are an XSI feature */
36#ifndef WITHOUT_XOPEN
37#define _XOPEN_SOURCE 600
38#endif
39
40/********************************************************************************************/
41/****************************** standard includes *****************************************/
42/********************************************************************************************/
43#include <pthread.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50#include <sys/wait.h>
51 #include <errno.h>
52
53#include <sys/time.h>
54
55/********************************************************************************************/
56/******************************   Test framework   *****************************************/
57/********************************************************************************************/
58#include "testfrmw.h"
59 #include "testfrmw.c"
60/* This header is responsible for defining the following macros:
61 * UNRESOLVED(ret, descr);
62 *    where descr is a description of the error and ret is an int (error code for example)
63 * FAILED(descr);
64 *    where descr is a short text saying why the test has failed.
65 * PASSED();
66 *    No parameter.
67 *
68 * Both three macros shall terminate the calling process.
69 * The testcase shall not terminate in any other maneer.
70 *
71 * The other file defines the functions
72 * void output_init()
73 * void output(char * string, ...)
74 *
75 * Those may be used to output information.
76 */
77
78/********************************************************************************************/
79/********************************** Configuration ******************************************/
80/********************************************************************************************/
81#ifndef VERBOSE
82#define VERBOSE 1
83#endif
84
85/********************************************************************************************/
86/***********************************    Test case   *****************************************/
87/********************************************************************************************/
88#ifndef WITHOUT_XOPEN
89/* The main test function. */
90int main( int argc, char * argv[] )
91{
92	int ret, status;
93	pid_t child, ctl;
94
95	struct itimerval it;
96
97	/* Initialize output */
98	output_init();
99
100	/* Create the interval timer */
101	it.it_interval.tv_sec = 15;
102	it.it_interval.tv_usec = 0;
103	it.it_value.tv_sec = 10;
104	it.it_value.tv_usec = 0;
105
106	ret = setitimer( ITIMER_REAL, &it, NULL );
107
108	if ( ret != 0 )
109	{
110		UNRESOLVED( errno, "Failed to set interval timer for ITIMER_REAL" );
111	}
112
113	ret = setitimer( ITIMER_VIRTUAL, &it, NULL );
114
115	if ( ret != 0 )
116	{
117		UNRESOLVED( errno, "Failed to set interval timer for ITIMER_VIRTUAL" );
118	}
119
120	ret = setitimer( ITIMER_PROF, &it, NULL );
121
122	if ( ret != 0 )
123	{
124		UNRESOLVED( errno, "Failed to set interval timer for ITIMER_PROF" );
125	}
126
127#if VERBOSE > 0
128	output( "All interval timers are set.\n" );
129
130#endif
131
132	/* Create the child */
133	child = fork();
134
135	if ( child == ( pid_t ) - 1 )
136	{
137		UNRESOLVED( errno, "Failed to fork" );
138	}
139
140	/* child */
141	if ( child == ( pid_t ) 0 )
142	{
143		/* Check we get the correct information: timer is reset */
144		ret = getitimer( ITIMER_REAL, &it );
145
146		if ( ret != 0 )
147		{
148			UNRESOLVED( errno, "Failed to read ITIMER_REAL in child" );
149		}
150
151		if ( it.it_value.tv_sec != 0 )
152		{
153			FAILED( "Timer ITIMER_REAL was not reset in child" );
154		}
155
156		ret = getitimer( ITIMER_VIRTUAL, &it );
157
158		if ( ret != 0 )
159		{
160			UNRESOLVED( errno, "Failed to read ITIMER_VIRTUAL in child" );
161		}
162
163		if ( it.it_value.tv_sec != 0 )
164		{
165			FAILED( "Timer ITIMER_VIRTUAL was not reset in child" );
166		}
167
168		ret = getitimer( ITIMER_PROF, &it );
169
170		if ( ret != 0 )
171		{
172			UNRESOLVED( errno, "Failed to read ITIMER_PROF in child" );
173		}
174
175		if ( it.it_value.tv_sec != 0 )
176		{
177			FAILED( "Timer ITIMER_PROF was not reset in child" );
178		}
179
180		/* We're done */
181		exit( PTS_PASS );
182	}
183
184	/* Parent joins the child */
185	ctl = waitpid( child, &status, 0 );
186
187	if ( ctl != child )
188	{
189		UNRESOLVED( errno, "Waitpid returned the wrong PID" );
190	}
191
192	if ( ( !WIFEXITED( status ) ) || ( WEXITSTATUS( status ) != PTS_PASS ) )
193	{
194		FAILED( "Child exited abnormally" );
195	}
196
197	/* Test passed */
198#if VERBOSE > 0
199
200	output( "Test passed\n" );
201
202#endif
203
204	PASSED;
205}
206
207#else /* WITHOUT_XOPEN */
208int main( int argc, char * argv[] )
209{
210	output_init();
211	UNTESTED( "This testcase requires XSI features" );
212}
213#endif
214