2
Plan 9 from User Space src/lib9/rfork.c
3
http://code.swtch.com/plan9port/src/tip/src/lib9/rfork.c
5
Copyright 2001-2007 Russ Cox. All Rights Reserved.
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:
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
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
45
extern char **environ;
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));
52
werrstr("unknown flags %08ux in rfork", n);
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
73
* Parent - wait for child to fork wait-free child.
74
* Then read pid from pipe. Assume pipe buffer can absorb the write.
78
if(wait4(pid, &status, 0, 0) < 0){
79
werrstr("pipe dance - wait4 - %r");
83
n = readn(p[0], buf, sizeof buf-1);
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);
91
werrstr("pipe dance - pipe read error - %r");
93
werrstr("pipe dance - pipe read eof");
95
werrstr("pipe dance - unknown failure");
100
werrstr("%s", buf+2);
103
pid = strtol(buf, &q, 0);
106
* Child - fork a new child whose wait message can't
107
* get back to the parent because we're going to exit!
109
signal(SIGCHLD, SIG_IGN);
113
/* Child parent - send status over pipe and exit. */
115
fprint(p[1], "%d", pid);
117
fprint(p[1], "x %r");
121
/* Child child - close pipe. */
133
werrstr("cannot use rfork for shared memory -- use libthread");
137
/* XXX set $NAMESPACE to a new directory */
141
setpgid(0, getpid());
145
werrstr("cannot use RFNOWAIT without RFPROC");
149
werrstr("unknown flags %08ux in rfork", flags);