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