1 /*
2 Plan 9 from User Space src/lib9/rfork.c
3 http://code.swtch.com/plan9port/src/tip/src/lib9/rfork.c
4
5 Copyright 2001-2007 Russ Cox. All Rights Reserved.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25
26 #include <u.h>
27 #include <sys/wait.h>
28 #include <signal.h>
29 #include <libc.h>
30 #undef rfork
31
32 static void
33 nop(int x)
34 {
35 USED(x);
36 }
37
38 int
39 p9rfork(int flags)
40 {
41 int pid, status;
42 int p[2];
43 int n;
44 char buf[128], *q;
45 extern char **environ;
46
47 if((flags&(RFPROC|RFFDG|RFMEM)) == (RFPROC|RFFDG)){
48 /* check other flags before we commit */
49 flags &= ~(RFPROC|RFFDG|RFENVG);
50 n = (flags & ~(RFNOTEG|RFNAMEG|RFNOWAIT|RFCENVG));
51 if(n){
52 werrstr("unknown flags %08ux in rfork", n);
53 return -1;
54 }
55 if(flags&RFNOWAIT){
56 /*
57 * BUG - should put the signal handler back after we
58 * finish, but I just don't care. If a program calls with
59 * NOWAIT once, they're not likely to want child notes
60 * after that.
61 */
62 signal(SIGCHLD, nop);
63 if(pipe(p) < 0)
64 return -1;
65 }
66 pid = fork();
67 if(pid == -1)
68 return -1;
69 if(flags&RFNOWAIT){
70 flags &= ~RFNOWAIT;
71 if(pid){
72 /*
73 * Parent - wait for child to fork wait-free child.
74 * Then read pid from pipe. Assume pipe buffer can absorb the write.
75 */
76 close(p[1]);
77 status = 0;
78 if(wait4(pid, &status, 0, 0) < 0){
79 werrstr("pipe dance - wait4 - %r");
80 close(p[0]);
81 return -1;
82 }
83 n = readn(p[0], buf, sizeof buf-1);
84 close(p[0]);
85 if(!WIFEXITED(status) || WEXITSTATUS(status)!=0 || n <= 0){
86 if(!WIFEXITED(status))
87 werrstr("pipe dance - !exited 0x%ux", status);
88 else if(WEXITSTATUS(status) != 0)
89 werrstr("pipe dance - non-zero status 0x%ux", status);
90 else if(n < 0)
91 werrstr("pipe dance - pipe read error - %r");
92 else if(n == 0)
93 werrstr("pipe dance - pipe read eof");
94 else
95 werrstr("pipe dance - unknown failure");
96 return -1;
97 }
98 buf[n] = 0;
99 if(buf[0] == 'x'){
100 werrstr("%s", buf+2);
101 return -1;
102 }
103 pid = strtol(buf, &q, 0);
104 }else{
105 /*
106 * Child - fork a new child whose wait message can't
107 * get back to the parent because we're going to exit!
108 */
109 signal(SIGCHLD, SIG_IGN);
110 close(p[0]);
111 pid = fork();
112 if(pid){
113 /* Child parent - send status over pipe and exit. */
114 if(pid > 0)
115 fprint(p[1], "%d", pid);
116 else
117 fprint(p[1], "x %r");
118 close(p[1]);
119 _exit(0);
120 }else{
121 /* Child child - close pipe. */
122 close(p[1]);
123 }
124 }
125 }
126 if(pid != 0)
127 return pid;
128 if(flags&RFCENVG)
129 if(environ)
130 *environ = nil;
131 }
132 if(flags&RFPROC){
133 werrstr("cannot use rfork for shared memory -- use libthread");
134 return -1;
135 }
136 if(flags&RFNAMEG){
137 /* XXX set $NAMESPACE to a new directory */
138 flags &= ~RFNAMEG;
139 }
140 if(flags&RFNOTEG){
141 setpgid(0, getpid());
142 flags &= ~RFNOTEG;
143 }
144 if(flags&RFNOWAIT){
145 werrstr("cannot use RFNOWAIT without RFPROC");
146 return -1;
147 }
148 if(flags){
149 werrstr("unknown flags %08ux in rfork", flags);
150 return -1;
151 }
152 return 0;
153 }