tbetter handling - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 230ead2fbe90864d309218048d5ad13070d536fa
 (DIR) parent c66e8adfa3c56b87b8deb38cc8e0a0f997a9eded
 (HTM) Author: rsc <devnull@localhost>
       Date:   Tue,  4 Jan 2005 22:22:09 +0000
       
       better handling
       
       Diffstat:
         M src/libthread/daemonize.c           |      30 ++++++++++++++++++++++--------
       
       1 file changed, 22 insertions(+), 8 deletions(-)
       ---
 (DIR) diff --git a/src/libthread/daemonize.c b/src/libthread/daemonize.c
       t@@ -1,5 +1,6 @@
        #include "threadimpl.h"
        
       +#undef waitpid
        #undef pipe
        #undef wait
        
       t@@ -10,13 +11,22 @@ static void
        child(void)
        {
                int status, pid;
       -        pid = wait(&status);
       -        if(pid < 0)
       -                fprint(2, "wait: %r\n");
       -        else if(WIFEXITED(status))
       +
       +        notedisable("sys: child");
       +        pid = waitpid(sigpid, &status, 0);
       +        if(pid < 0){
       +                fprint(2, "%s: wait: %r\n", argv0);
       +                _exit(97);
       +        }
       +        if(WIFEXITED(status))
                         _exit(WEXITSTATUS(status));
       -print("pid %d if %d %d\n", pid, WIFEXITED(status), WEXITSTATUS(status));
       -        _exit(97);
       +        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
       t@@ -54,6 +64,7 @@ _threadsetupdaemonize(void)
                if(fcntl(p[0], F_SETFD, 1) < 0 || fcntl(p[1], F_SETFD, 1) < 0)
                        sysfatal("passer pipe pipe fcntl: %r");
        
       +        noteenable("sys: child");
                signal(SIGCHLD, sigpass);
                switch(pid = fork()){
                case -1:
       t@@ -62,6 +73,8 @@ _threadsetupdaemonize(void)
                        close(p[1]);
                        break;
                case 0:
       +                for(i=0; i<100; i++) sched_yield();
       +                notedisable("sys: child");
                        signal(SIGCHLD, SIG_DFL);
                        rfork(RFNOTEG);
                        close(p[0]);
       t@@ -81,8 +94,9 @@ _threadsetupdaemonize(void)
        
                for(;;){
                        n = read(p[0], buf, sizeof buf-1);
       -                if(n == 0)        /* program exited */
       +                if(n == 0){        /* program exited */
                                child();
       +                }
                        if(n > 0)
                                break;
                        print("passer read: %r\n");
       t@@ -92,7 +106,7 @@ _threadsetupdaemonize(void)
        }
        
        void
       -threaddaemonize(void)
       +_threaddaemonize(void)
        {
                if(threadpassfd >= 0){
                        write(threadpassfd, "0", 1);