xref: /openbsd-src/regress/lib/libpthread/fork/fork.c (revision d8d4eb71fdd4c0ca905967d2dfb841292c49d5ca)
1*d8d4eb71Sbluhm /*	$OpenBSD: fork.c,v 1.6 2017/07/29 17:36:59 bluhm Exp $	*/
2b2ea75c1Sfgsch /*
3b2ea75c1Sfgsch  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4b2ea75c1Sfgsch  * proven@mit.edu All rights reserved.
5b2ea75c1Sfgsch  *
6b2ea75c1Sfgsch  * Redistribution and use in source and binary forms, with or without
7b2ea75c1Sfgsch  * modification, are permitted provided that the following conditions
8b2ea75c1Sfgsch  * are met:
9b2ea75c1Sfgsch  * 1. Redistributions of source code must retain the above copyright
10b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer.
11b2ea75c1Sfgsch  * 2. Redistributions in binary form must reproduce the above copyright
12b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer in the
13b2ea75c1Sfgsch  *    documentation and/or other materials provided with the distribution.
14b2ea75c1Sfgsch  * 3. All advertising materials mentioning features or use of this software
15b2ea75c1Sfgsch  *    must display the following acknowledgement:
16b2ea75c1Sfgsch  *	This product includes software developed by Chris Provenzano,
17b2ea75c1Sfgsch  *	the University of California, Berkeley, and contributors.
18b2ea75c1Sfgsch  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19b2ea75c1Sfgsch  *   contributors may be used to endorse or promote products derived
20b2ea75c1Sfgsch  *   from this software without specific prior written permission.
21b2ea75c1Sfgsch  *
22b2ea75c1Sfgsch  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23b2ea75c1Sfgsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b2ea75c1Sfgsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b2ea75c1Sfgsch  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26b2ea75c1Sfgsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27b2ea75c1Sfgsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28b2ea75c1Sfgsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29b2ea75c1Sfgsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30b2ea75c1Sfgsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31b2ea75c1Sfgsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32b2ea75c1Sfgsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33b2ea75c1Sfgsch  */
34b2ea75c1Sfgsch 
35b2ea75c1Sfgsch /*
36b2ea75c1Sfgsch  * Copyright (c) 1994 by Chris Provenzano, proven@athena.mit.edu
37b2ea75c1Sfgsch  *
38b2ea75c1Sfgsch  * Test the fork system call.
39b2ea75c1Sfgsch  */
40b2ea75c1Sfgsch 
41b2ea75c1Sfgsch #include <pthread.h>
42b2ea75c1Sfgsch #include <pthread_np.h>
43b2ea75c1Sfgsch #include <stdio.h>
44b2ea75c1Sfgsch #include <fcntl.h>
45b2ea75c1Sfgsch #include <sys/types.h>
46b2ea75c1Sfgsch #include <unistd.h>
478445c537Stodd #include <stdlib.h>
48b2ea75c1Sfgsch #include <signal.h>
49b2ea75c1Sfgsch #include <sys/wait.h>
50b2ea75c1Sfgsch #include "test.h"
51b2ea75c1Sfgsch 
52db3296cfSderaadt static void *
sleeper(void * arg)53b2ea75c1Sfgsch sleeper(void *arg)
54b2ea75c1Sfgsch {
55b2ea75c1Sfgsch 	pthread_set_name_np(pthread_self(), "slpr");
56b2ea75c1Sfgsch 	sleep(10);
57b2ea75c1Sfgsch 	PANIC("sleeper timed out");
58b2ea75c1Sfgsch }
59b2ea75c1Sfgsch 
60b2ea75c1Sfgsch 
61b2ea75c1Sfgsch int
main(int argc,char * argv[])62db3296cfSderaadt main(int argc, char *argv[])
63b2ea75c1Sfgsch {
64b2ea75c1Sfgsch 	int flags;
65b2ea75c1Sfgsch 	pthread_t sleeper_thread;
66b2ea75c1Sfgsch 	int status;
67b2ea75c1Sfgsch 	pid_t parent_pid;
68b2ea75c1Sfgsch 	pid_t child_pid;
69b2ea75c1Sfgsch 
70b2ea75c1Sfgsch 	parent_pid = getpid();
71b2ea75c1Sfgsch 
72b2ea75c1Sfgsch 	CHECKe(flags = fcntl(STDOUT_FILENO, F_GETFL));
73b2ea75c1Sfgsch 	if ((flags & (O_NONBLOCK | O_NDELAY))) {
74b2ea75c1Sfgsch 		/* This fails when stdout is /dev/null!? */
75b2ea75c1Sfgsch 		/*CHECKe*/(fcntl(STDOUT_FILENO, F_SETFL,
76b2ea75c1Sfgsch 		    flags & ~(O_NONBLOCK | O_NDELAY)));
77b2ea75c1Sfgsch 	}
78b2ea75c1Sfgsch 
79b2ea75c1Sfgsch 	CHECKr(pthread_create(&sleeper_thread, NULL, sleeper, NULL));
80b2ea75c1Sfgsch 	sleep(1);
81b2ea75c1Sfgsch 
82b2ea75c1Sfgsch 	printf("forking from pid %d\n", getpid());
83b2ea75c1Sfgsch 
84b2ea75c1Sfgsch 	CHECKe(child_pid = fork());
85b2ea75c1Sfgsch 	if (child_pid == 0) {
86b2ea75c1Sfgsch 		/* child: */
87b2ea75c1Sfgsch 		printf("child = pid %d\n", getpid());
88b2ea75c1Sfgsch 		/* Our pid should change */
89b2ea75c1Sfgsch 		ASSERT(getpid() != parent_pid);
90b2ea75c1Sfgsch 		/* Our sleeper thread should have disappeared */
91b2ea75c1Sfgsch 		printf("sleeper should have disappeared\n");
923f2812baSmarc 
93*d8d4eb71Sbluhm 		printf("child ok\n");
94b2ea75c1Sfgsch 		_exit(0);
95b2ea75c1Sfgsch 		PANIC("child _exit");
96b2ea75c1Sfgsch 	}
97b2ea75c1Sfgsch 
98b2ea75c1Sfgsch 	/* parent: */
99b2ea75c1Sfgsch 	printf("parent = pid %d\n", getpid());
100b2ea75c1Sfgsch 	/* Our pid should stay the same */
101b2ea75c1Sfgsch 	ASSERT(getpid() == parent_pid);
102b2ea75c1Sfgsch 	/* wait for the child */
103b2ea75c1Sfgsch 	ASSERTe(wait(&status), == child_pid);
104b2ea75c1Sfgsch 	/* the child should have called exit(0) */
105b2ea75c1Sfgsch 	ASSERT(WIFEXITED(status));
106b2ea75c1Sfgsch 	ASSERT(WEXITSTATUS(status) == 0);
107b2ea75c1Sfgsch 	/* Our sleeper thread should still be around */
108b2ea75c1Sfgsch 	CHECKr(pthread_detach(sleeper_thread));
109b2ea75c1Sfgsch 	printf("parent ok\n");
110b2ea75c1Sfgsch 	SUCCEED;
111b2ea75c1Sfgsch }
112