better handling

This commit is contained in:
rsc 2005-01-04 22:22:09 +00:00
parent c66e8adfa3
commit 230ead2fbe

View file

@ -1,5 +1,6 @@
#include "threadimpl.h" #include "threadimpl.h"
#undef waitpid
#undef pipe #undef pipe
#undef wait #undef wait
@ -10,13 +11,22 @@ static void
child(void) child(void)
{ {
int status, pid; int status, pid;
pid = wait(&status);
if(pid < 0) notedisable("sys: child");
fprint(2, "wait: %r\n"); pid = waitpid(sigpid, &status, 0);
else if(WIFEXITED(status)) if(pid < 0){
_exit(WEXITSTATUS(status)); fprint(2, "%s: wait: %r\n", argv0);
print("pid %d if %d %d\n", pid, WIFEXITED(status), WEXITSTATUS(status));
_exit(97); _exit(97);
}
if(WIFEXITED(status))
_exit(WEXITSTATUS(status));
if(WIFSIGNALED(status)){
signal(WTERMSIG(status), SIG_DFL);
raise(WTERMSIG(status));
_exit(98); /* not reached */
}
fprint(2, "%s: wait pid %d status 0x%ux\n", pid, status);
_exit(99);
} }
static void static void
@ -54,6 +64,7 @@ _threadsetupdaemonize(void)
if(fcntl(p[0], F_SETFD, 1) < 0 || fcntl(p[1], F_SETFD, 1) < 0) if(fcntl(p[0], F_SETFD, 1) < 0 || fcntl(p[1], F_SETFD, 1) < 0)
sysfatal("passer pipe pipe fcntl: %r"); sysfatal("passer pipe pipe fcntl: %r");
noteenable("sys: child");
signal(SIGCHLD, sigpass); signal(SIGCHLD, sigpass);
switch(pid = fork()){ switch(pid = fork()){
case -1: case -1:
@ -62,6 +73,8 @@ _threadsetupdaemonize(void)
close(p[1]); close(p[1]);
break; break;
case 0: case 0:
for(i=0; i<100; i++) sched_yield();
notedisable("sys: child");
signal(SIGCHLD, SIG_DFL); signal(SIGCHLD, SIG_DFL);
rfork(RFNOTEG); rfork(RFNOTEG);
close(p[0]); close(p[0]);
@ -81,8 +94,9 @@ _threadsetupdaemonize(void)
for(;;){ for(;;){
n = read(p[0], buf, sizeof buf-1); n = read(p[0], buf, sizeof buf-1);
if(n == 0) /* program exited */ if(n == 0){ /* program exited */
child(); child();
}
if(n > 0) if(n > 0)
break; break;
print("passer read: %r\n"); print("passer read: %r\n");
@ -92,7 +106,7 @@ _threadsetupdaemonize(void)
} }
void void
threaddaemonize(void) _threaddaemonize(void)
{ {
if(threadpassfd >= 0){ if(threadpassfd >= 0){
write(threadpassfd, "0", 1); write(threadpassfd, "0", 1);