2004-04-21 04:48:25 +00:00
|
|
|
#include <u.h>
|
2004-04-21 22:44:37 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <signal.h>
|
2004-04-21 04:48:25 +00:00
|
|
|
#include <libc.h>
|
|
|
|
#undef rfork
|
2003-11-23 18:12:54 +00:00
|
|
|
|
2004-10-22 17:12:38 +00:00
|
|
|
static void
|
|
|
|
nop(int x)
|
|
|
|
{
|
|
|
|
USED(x);
|
|
|
|
}
|
|
|
|
|
2003-11-23 18:12:54 +00:00
|
|
|
int
|
|
|
|
p9rfork(int flags)
|
|
|
|
{
|
2004-04-21 22:44:37 +00:00
|
|
|
int pid, status;
|
|
|
|
int p[2];
|
2006-02-12 00:36:58 +00:00
|
|
|
int n;
|
2004-04-21 22:44:37 +00:00
|
|
|
char buf[128], *q;
|
2006-02-11 22:58:29 +00:00
|
|
|
extern char **environ;
|
2003-12-04 00:17:10 +00:00
|
|
|
|
|
|
|
if((flags&(RFPROC|RFFDG|RFMEM)) == (RFPROC|RFFDG)){
|
|
|
|
/* check other flags before we commit */
|
2006-02-11 22:58:29 +00:00
|
|
|
flags &= ~(RFPROC|RFFDG|RFENVG);
|
|
|
|
n = (flags & ~(RFNOTEG|RFNAMEG|RFNOWAIT|RFCENVG));
|
2004-04-21 22:44:37 +00:00
|
|
|
if(n){
|
|
|
|
werrstr("unknown flags %08ux in rfork", n);
|
2003-12-04 00:17:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-21 22:44:37 +00:00
|
|
|
if(flags&RFNOWAIT){
|
2004-10-22 17:12:38 +00:00
|
|
|
/*
|
|
|
|
* BUG - should put the signal handler back after we
|
|
|
|
* finish, but I just don't care. If a program calls with
|
|
|
|
* NOWAIT once, they're not likely to want child notes
|
|
|
|
* after that.
|
|
|
|
*/
|
|
|
|
signal(SIGCHLD, nop);
|
2004-04-21 22:44:37 +00:00
|
|
|
if(pipe(p) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2003-12-04 00:17:10 +00:00
|
|
|
pid = fork();
|
2004-04-21 22:44:37 +00:00
|
|
|
if(pid == -1)
|
|
|
|
return -1;
|
|
|
|
if(flags&RFNOWAIT){
|
|
|
|
flags &= ~RFNOWAIT;
|
|
|
|
if(pid){
|
|
|
|
/*
|
|
|
|
* Parent - wait for child to fork wait-free child.
|
|
|
|
* Then read pid from pipe. Assume pipe buffer can absorb the write.
|
|
|
|
*/
|
|
|
|
close(p[1]);
|
2004-10-22 17:12:38 +00:00
|
|
|
status = 0;
|
|
|
|
if(wait4(pid, &status, 0, 0) < 0){
|
|
|
|
werrstr("pipe dance - wait4 - %r");
|
|
|
|
close(p[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-21 22:44:37 +00:00
|
|
|
n = readn(p[0], buf, sizeof buf-1);
|
|
|
|
close(p[0]);
|
|
|
|
if(!WIFEXITED(status) || WEXITSTATUS(status)!=0 || n <= 0){
|
2004-10-22 17:12:38 +00:00
|
|
|
if(!WIFEXITED(status))
|
|
|
|
werrstr("pipe dance - !exited 0x%ux", status);
|
|
|
|
else if(WEXITSTATUS(status) != 0)
|
|
|
|
werrstr("pipe dance - non-zero status 0x%ux", status);
|
|
|
|
else if(n < 0)
|
|
|
|
werrstr("pipe dance - pipe read error - %r");
|
|
|
|
else if(n == 0)
|
|
|
|
werrstr("pipe dance - pipe read eof");
|
|
|
|
else
|
|
|
|
werrstr("pipe dance - unknown failure");
|
2004-04-21 22:44:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf[n] = 0;
|
2004-10-22 17:12:38 +00:00
|
|
|
if(buf[0] == 'x'){
|
|
|
|
werrstr("%s", buf+2);
|
2004-04-21 22:44:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2004-10-22 17:12:38 +00:00
|
|
|
pid = strtol(buf, &q, 0);
|
2004-04-21 22:44:37 +00:00
|
|
|
}else{
|
|
|
|
/*
|
|
|
|
* Child - fork a new child whose wait message can't
|
|
|
|
* get back to the parent because we're going to exit!
|
|
|
|
*/
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
close(p[0]);
|
|
|
|
pid = fork();
|
|
|
|
if(pid){
|
|
|
|
/* Child parent - send status over pipe and exit. */
|
|
|
|
if(pid > 0)
|
|
|
|
fprint(p[1], "%d", pid);
|
|
|
|
else
|
2004-10-22 17:12:38 +00:00
|
|
|
fprint(p[1], "x %r");
|
2004-04-21 22:44:37 +00:00
|
|
|
close(p[1]);
|
|
|
|
_exit(0);
|
|
|
|
}else{
|
|
|
|
/* Child child - close pipe. */
|
|
|
|
close(p[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-12-04 00:17:10 +00:00
|
|
|
if(pid != 0)
|
|
|
|
return pid;
|
2006-02-11 22:58:29 +00:00
|
|
|
if(flags&RFCENVG)
|
|
|
|
if(environ)
|
|
|
|
*environ = nil;
|
2003-12-04 00:17:10 +00:00
|
|
|
}
|
2003-11-23 18:12:54 +00:00
|
|
|
if(flags&RFPROC){
|
2005-10-29 16:26:32 +00:00
|
|
|
werrstr("cannot use rfork for shared memory -- use libthread");
|
2003-11-23 18:12:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-12-11 17:48:38 +00:00
|
|
|
if(flags&RFNAMEG){
|
|
|
|
/* XXX set $NAMESPACE to a new directory */
|
|
|
|
flags &= ~RFNAMEG;
|
|
|
|
}
|
2003-11-23 18:12:54 +00:00
|
|
|
if(flags&RFNOTEG){
|
2003-11-23 19:49:17 +00:00
|
|
|
setpgid(0, getpid());
|
2003-11-23 18:12:54 +00:00
|
|
|
flags &= ~RFNOTEG;
|
|
|
|
}
|
2004-04-21 22:44:37 +00:00
|
|
|
if(flags&RFNOWAIT){
|
|
|
|
werrstr("cannot use RFNOWAIT without RFPROC");
|
|
|
|
return -1;
|
|
|
|
}
|
2003-11-23 18:12:54 +00:00
|
|
|
if(flags){
|
|
|
|
werrstr("unknown flags %08ux in rfork", flags);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|