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 new process' ID does not match any existing process or group ID. 21 22 * The steps are: 23 * -> create a child; then terminate this child. 24 * -> check that no other process or group has the same ID. 25 26 * The test fails if another object shares the same ID. 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 #include <signal.h> 48 49/********************************************************************************************/ 50/****************************** Test framework *****************************************/ 51/********************************************************************************************/ 52 #include "testfrmw.h" 53 #include "testfrmw.c" 54 /* This header is responsible for defining the following macros: 55 * UNRESOLVED(ret, descr); 56 * where descr is a description of the error and ret is an int (error code for example) 57 * FAILED(descr); 58 * where descr is a short text saying why the test has failed. 59 * PASSED(); 60 * No parameter. 61 * 62 * Both three macros shall terminate the calling process. 63 * The testcase shall not terminate in any other maneer. 64 * 65 * The other file defines the functions 66 * void output_init() 67 * void output(char * string, ...) 68 * 69 * Those may be used to output information. 70 */ 71 72/********************************************************************************************/ 73/********************************** Configuration ******************************************/ 74/********************************************************************************************/ 75#ifndef VERBOSE 76#define VERBOSE 1 77#endif 78 79 80/********************************************************************************************/ 81/*********************************** Test case *****************************************/ 82/********************************************************************************************/ 83 84/* The main test function. */ 85int main(int argc, char * argv[]) 86{ 87 int ret, status; 88 pid_t child, ctl; 89 90 /* Initialize output */ 91 output_init(); 92 93 /* Create the child */ 94 child = fork(); 95 if (child == (pid_t) -1) { UNRESOLVED(errno, "Failed to fork"); } 96 97 /* child */ 98 if (child == (pid_t) 0) 99 { 100 /* The child stops immediatly */ 101 exit(PTS_PASS); 102 } 103 104 /* Parent joins the child */ 105 ctl = waitpid(child, &status, 0); 106 if (ctl != child) { UNRESOLVED(errno, "Waitpid returned the wrong PID"); } 107 if ((!WIFEXITED(status)) || (WEXITSTATUS(status) != PTS_PASS)) 108 { 109 UNRESOLVED(status, "Child exited abnormally"); 110 } 111 112 ret = kill(child, 0); 113 if ((ret == 0) || (errno != ESRCH)) 114 { 115 output("Kill returned %d (%d: %s)\n", ret, errno, strerror(errno)); 116 FAILED("Another process with the same PID as the child exists"); 117 } 118 119 ret = kill((pid_t) (0 - (int)child), 0); 120 if ((ret == 0) || (errno != ESRCH)) 121 { 122 output("Kill returned %d (%d: %s)\n", ret, errno, strerror(errno)); 123 FAILED("A process group with the same PID as the child exists"); 124 } 125 126 127 /* Test passed */ 128 #if VERBOSE > 0 129 output("Test passed\n"); 130 #endif 131 132 PASSED; 133} 134 135 136