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* The time left until an alarm clock is triggered is reset to zero, 21* and the alarm, if any, is canceled 22 23* The steps are: 24* -> Trig an alarm 25* -> fork 26* -> Check the alarm is not running in the child process. 27* -> join the child 28 29* The test fails if the child has a pending alarm. 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 51/********************************************************************************************/ 52/****************************** Test framework *****************************************/ 53/********************************************************************************************/ 54#include "testfrmw.h" 55 #include "testfrmw.c" 56/* This header is responsible for defining the following macros: 57 * UNRESOLVED(ret, descr); 58 * where descr is a description of the error and ret is an int (error code for example) 59 * FAILED(descr); 60 * where descr is a short text saying why the test has failed. 61 * PASSED(); 62 * No parameter. 63 * 64 * Both three macros shall terminate the calling process. 65 * The testcase shall not terminate in any other maneer. 66 * 67 * The other file defines the functions 68 * void output_init() 69 * void output(char * string, ...) 70 * 71 * Those may be used to output information. 72 */ 73 74/********************************************************************************************/ 75/********************************** Configuration ******************************************/ 76/********************************************************************************************/ 77#ifndef VERBOSE 78#define VERBOSE 1 79#endif 80 81/********************************************************************************************/ 82/*********************************** Test case *****************************************/ 83/********************************************************************************************/ 84 85/* The main test function. */ 86int main( int argc, char * argv[] ) 87{ 88 int ret, status; 89 pid_t child, ctl; 90 91 /* Initialize output */ 92 output_init(); 93 94 /* Set the alarm pending */ 95 alarm( 10 ); 96 97 /* Check the alarm() behavior */ 98 ret = alarm( 10 ); 99 100 if ( ret == 0 ) 101 { 102 FAILED( "the alarm() routine does not behave as expected" ); 103 } 104 105 /* Create the child */ 106 child = fork(); 107 108 if ( child == ( pid_t ) - 1 ) 109 { 110 UNRESOLVED( errno, "Failed to fork" ); 111 } 112 113 /* child */ 114 if ( child == ( pid_t ) 0 ) 115 { 116 117 ret = alarm( 10 ); 118 119 if ( ret != 0 ) 120 { 121 FAILED( "The child alarm pending was not reset." ); 122 } 123 124 /* We're done */ 125 exit( PTS_PASS ); 126 } 127 128 /* Parent joins the child */ 129 ctl = waitpid( child, &status, 0 ); 130 131 if ( ctl != child ) 132 { 133 UNRESOLVED( errno, "Waitpid returned the wrong PID" ); 134 } 135 136 if ( ( !WIFEXITED( status ) ) || ( WEXITSTATUS( status ) != PTS_PASS ) ) 137 { 138 FAILED( "Child exited abnormally" ); 139 } 140 141 alarm( 0 ); 142 143 /* Test passed */ 144#if VERBOSE > 0 145 146 output( "Test passed\n" ); 147 148#endif 149 150 PASSED; 151} 152 153 154