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 parent process ID of the child is the process ID of the parent (caller of fork())
     20 
     21  * The steps are:
     22  * -> create a child
     23  * -> check its parent process ID is the PID of its parent.
     24 
     25  * The test fails if the IDs differ.
     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 "../testfrmw/testfrmw.h"
     43 #include "../testfrmw/testfrmw.c"
     44 
     45 #ifndef VERBOSE
     46 #define VERBOSE 1
     47 #endif
     48 
     49 int main(void)
     50 {
     51 	int status;
     52 	pid_t child, ctl;
     53 
     54 	output_init();
     55 
     56 	ctl = getpid();
     57 
     58 	/* Create the child */
     59 	child = fork();
     60 	if (child == -1) {
     61 		UNRESOLVED(errno, "Failed to fork");
     62 	}
     63 
     64 	/* child */
     65 	if (child == 0) {
     66 		/* Check the parent process ID */
     67 		if (ctl != getppid()) {
     68 			FAILED
     69 			    ("The parent process ID is not the PID of the parent");
     70 		}
     71 
     72 		/* We're done */
     73 		exit(PTS_PASS);
     74 	}
     75 
     76 	/* Parent joins the child */
     77 	ctl = waitpid(child, &status, 0);
     78 	if (ctl != child) {
     79 		UNRESOLVED(errno, "Waitpid returned the wrong PID");
     80 	}
     81 	if ((!WIFEXITED(status)) || (WEXITSTATUS(status) != PTS_PASS)) {
     82 		UNRESOLVED(status, "Child exited abnormally");
     83 	}
     84 
     85 #if VERBOSE > 0
     86 	output("Test passed\n");
     87 #endif
     88 
     89 	PASSED;
     90 }
     91