Home | History | Annotate | Download | only in fork
      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.,
     15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16 
     17  * This sample test aims to check the following assertion:
     18  *
     19  * The new process' ID does not match any existing process or group ID.
     20 
     21  * The steps are:
     22  * -> create a child; then terminate this child.
     23  * -> check that no other process or group has the same ID.
     24 
     25  * The test fails if another object shares the same ID.
     26 
     27  */
     28 
     29  /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
     30 #define _POSIX_C_SOURCE 200112L
     31 
     32 #include <pthread.h>
     33 #include <stdarg.h>
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 #include <unistd.h>
     38 
     39 #include <sys/wait.h>
     40 #include <errno.h>
     41 
     42 #include <signal.h>
     43 
     44 #include "../testfrmw/testfrmw.h"
     45 #include "../testfrmw/testfrmw.c"
     46 
     47 #ifndef VERBOSE
     48 #define VERBOSE 1
     49 #endif
     50 
     51 int main(void)
     52 {
     53 	int ret, status;
     54 	pid_t child, ctl;
     55 
     56 	/* Initialize output */
     57 	output_init();
     58 
     59 	/* Create the child */
     60 	child = fork();
     61 	if (child == -1) {
     62 		UNRESOLVED(errno, "Failed to fork");
     63 	}
     64 
     65 	/* child */
     66 	if (child == 0) {
     67 		/* The child stops immediatly */
     68 		exit(PTS_PASS);
     69 	}
     70 
     71 	/* Parent joins the child */
     72 	ctl = waitpid(child, &status, 0);
     73 	if (ctl != child) {
     74 		UNRESOLVED(errno, "Waitpid returned the wrong PID");
     75 	}
     76 	if ((!WIFEXITED(status)) || (WEXITSTATUS(status) != PTS_PASS)) {
     77 		UNRESOLVED(status, "Child exited abnormally");
     78 	}
     79 
     80 	ret = kill(child, 0);
     81 	if ((ret == 0) || (errno != ESRCH)) {
     82 		output("Kill returned %d (%d: %s)\n", ret, errno,
     83 		       strerror(errno));
     84 		FAILED("Another process with the same PID as the child exists");
     85 	}
     86 
     87 	ret = kill((0 - (int)child), 0);
     88 	if ((ret == 0) || (errno != ESRCH)) {
     89 		output("Kill returned %d (%d: %s)\n", ret, errno,
     90 		       strerror(errno));
     91 		FAILED("A process group with the same PID as the child exists");
     92 	}
     93 
     94 #if VERBOSE > 0
     95 	output("Test passed\n");
     96 #endif
     97 
     98 	PASSED;
     99 }
    100