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 opened directory streams are copied to the child process.
     20 * Positionning information may be shared between both processes.
     21 
     22 * The steps are:
     23 * -> Open the current directory,
     24 * -> Count the directory entries, then rewind.
     25 * -> create a child
     26 * -> The child counts the directory entries.
     27 
     28 * The test fails if the directory is read in the parent and not in the child.
     29 
     30 */
     31 
     32 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
     33 #define _POSIX_C_SOURCE 200112L
     34 
     35 #include <pthread.h>
     36 #include <stdarg.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <string.h>
     40 #include <unistd.h>
     41 
     42 #include <sys/wait.h>
     43 #include <errno.h>
     44 
     45 #include <dirent.h>
     46 
     47 #include "../testfrmw/testfrmw.h"
     48 #include "../testfrmw/testfrmw.c"
     49 
     50 #ifndef VERBOSE
     51 #define VERBOSE 1
     52 #endif
     53 
     54 static int count(DIR * thedir)
     55 {
     56 	int counter = 0;
     57 
     58 	struct dirent *dp;
     59 
     60 	rewinddir(thedir);
     61 
     62 	/* Count the directory entries */
     63 
     64 	do {
     65 		dp = readdir(thedir);
     66 
     67 		if (dp != NULL)
     68 			counter++;
     69 	}
     70 	while (dp != NULL);
     71 
     72 	return counter;
     73 }
     74 
     75 int main(void)
     76 {
     77 	int ret, status;
     78 	pid_t child, ctl;
     79 
     80 	int counted;
     81 
     82 	/* the '.' directory pointers */
     83 	DIR *dotdir;
     84 
     85 	output_init();
     86 
     87 	/* Open the directory */
     88 	dotdir = opendir(".");
     89 
     90 	if (dotdir == NULL) {
     91 		UNRESOLVED(errno, "opendir failed");
     92 	}
     93 
     94 	/* Count entries */
     95 	counted = count(dotdir);
     96 
     97 #if VERBOSE > 0
     98 
     99 	output("Found %d entries in current dir\n", counted);
    100 
    101 #endif
    102 
    103 	/* Create the child */
    104 	child = fork();
    105 
    106 	if (child == -1) {
    107 		UNRESOLVED(errno, "Failed to fork");
    108 	}
    109 
    110 	/* child */
    111 	if (child == 0) {
    112 		/* Count in child process */
    113 		counted = count(dotdir);
    114 
    115 #if VERBOSE > 0
    116 
    117 		output("Found %d entries in current dir from child\n", counted);
    118 #endif
    119 
    120 		ret = closedir(dotdir);
    121 
    122 		if (ret != 0) {
    123 			UNRESOLVED(errno, "Failed to close dir in child");
    124 		}
    125 
    126 		/* We're done */
    127 		exit(PTS_PASS);
    128 	}
    129 
    130 	/* Parent joins the child */
    131 	ctl = waitpid(child, &status, 0);
    132 
    133 	if (ctl != child) {
    134 		UNRESOLVED(errno, "Waitpid returned the wrong PID");
    135 	}
    136 
    137 	if (!WIFEXITED(status) || (WEXITSTATUS(status) != PTS_PASS)) {
    138 		FAILED("Child exited abnormally -- dir stream not copied?");
    139 	}
    140 
    141 	/* close the directory stream */
    142 	ret = closedir(dotdir);
    143 
    144 	if (ret != 0) {
    145 		UNRESOLVED(errno, "Failed to closedir in parent");
    146 	}
    147 
    148 #if VERBOSE > 0
    149 	output("Test passed\n");
    150 
    151 #endif
    152 
    153 	PASSED;
    154 }
    155