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 parent process ID of the child is the process ID of the parent (caller of fork()) 21 22 * The steps are: 23 * -> create a child 24 * -> check its parent process ID is the PID of its parent. 25 26 * The test fails if the IDs differ. 27 28 */ 29 30 31 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */ 32 #define _POSIX_C_SOURCE 200112L 33 34/********************************************************************************************/ 35/****************************** standard includes *****************************************/ 36/********************************************************************************************/ 37 #include <pthread.h> 38 #include <stdarg.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 #include <sys/wait.h> 45 #include <errno.h> 46 47/********************************************************************************************/ 48/****************************** Test framework *****************************************/ 49/********************************************************************************************/ 50 #include "testfrmw.h" 51 #include "testfrmw.c" 52 /* This header is responsible for defining the following macros: 53 * UNRESOLVED(ret, descr); 54 * where descr is a description of the error and ret is an int (error code for example) 55 * FAILED(descr); 56 * where descr is a short text saying why the test has failed. 57 * PASSED(); 58 * No parameter. 59 * 60 * Both three macros shall terminate the calling process. 61 * The testcase shall not terminate in any other maneer. 62 * 63 * The other file defines the functions 64 * void output_init() 65 * void output(char * string, ...) 66 * 67 * Those may be used to output information. 68 */ 69 70/********************************************************************************************/ 71/********************************** Configuration ******************************************/ 72/********************************************************************************************/ 73#ifndef VERBOSE 74#define VERBOSE 1 75#endif 76 77 78/********************************************************************************************/ 79/*********************************** Test case *****************************************/ 80/********************************************************************************************/ 81 82/* The main test function. */ 83int main(int argc, char * argv[]) 84{ 85 int status; 86 pid_t child, ctl; 87 88 /* Initialize output */ 89 output_init(); 90 91 /* Get parent process ID */ 92 ctl = getpid(); 93 94 /* Create the child */ 95 child = fork(); 96 if (child == (pid_t) -1) { UNRESOLVED(errno, "Failed to fork"); } 97 98 /* child */ 99 if (child == (pid_t) 0) 100 { 101 /* Check the parent process ID */ 102 if (ctl != getppid()) 103 { 104 FAILED("The parent process ID is not the PID of the parent"); 105 } 106 107 /* We're done */ 108 exit(PTS_PASS); 109 } 110 111 /* Parent joins the child */ 112 ctl = waitpid(child, &status, 0); 113 if (ctl != child) { UNRESOLVED(errno, "Waitpid returned the wrong PID"); } 114 if ((!WIFEXITED(status)) || (WEXITSTATUS(status) != PTS_PASS)) 115 { 116 UNRESOLVED(status, "Child exited abnormally"); 117 } 118 119 /* Test passed */ 120 #if VERBOSE > 0 121 output("Test passed\n"); 122 #endif 123 124 PASSED; 125} 126 127 128