tdon't rfork(RFNOTEG) because then you lose the ability to read from the console.  damn. - 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 e9dbe11dbf43197892680f3b5084cd12b6dc198b
 (DIR) parent 2b85f70db0d42f72fc532de906494fa173efa834
 (HTM) Author: rsc <devnull@localhost>
       Date:   Tue, 11 Jan 2005 21:06:55 +0000
       
       don't rfork(RFNOTEG) because then you lose
       tthe ability to read from the console.  damn.
       
       also, handle case where child exits before
       fork returns in parent.  have to record that
       sigchld was seen and then run the handler later.
       
       Diffstat:
         M src/libthread/daemonize.c           |      26 +++++++++++++++++++++++---
       
       1 file changed, 23 insertions(+), 3 deletions(-)
       ---
 (DIR) diff --git a/src/libthread/daemonize.c b/src/libthread/daemonize.c
       t@@ -9,6 +9,7 @@
        
        static int sigpid;
        static int threadpassfd;
       +static int gotsigchld;
        
        static void
        child(void)
       t@@ -44,12 +45,28 @@ child(void)
        static void
        sigpass(int sig)
        {
       +        if(sigpid == 1){
       +                gotsigchld = 1;
       +                return;
       +        }
       +
                if(sig == SIGCHLD)
                        child();
                else
                        kill(sigpid, sig);
        }
        
       +static int sigs[] = 
       +{
       +        SIGHUP, SIGINT, SIGQUIT, SIGILL,
       +        SIGTRAP, SIGABRT, SIGBUS, SIGFPE,
       +        SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE,
       +        SIGALRM, SIGTERM, SIGCHLD, SIGSTOP,
       +        SIGTSTP, SIGTTIN, SIGTTOU, SIGURG, 
       +        SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF,
       +        SIGWINCH, SIGIO, SIGPWR, SIGSYS
       +};
       +
        void
        _threadsetupdaemonize(void)
        {
       t@@ -88,20 +105,23 @@ _threadsetupdaemonize(void)
                        for(i=0; i<100; i++) sched_yield();
                        notedisable("sys: child");
                        signal(SIGCHLD, SIG_DFL);
       -                rfork(RFNOTEG);
       +        /*        rfork(RFNOTEG); */
                        close(p[0]);
                        threadpassfd = p[1];
                        return;
                }
        
                sigpid = pid;
       -        for(i=0; i<NSIG; i++){
       +        if(gotsigchld)
       +                sigpass(SIGCHLD);
       +
       +        for(i=0; i<nelem(sigs); i++){
                        struct sigaction sa;
        
                        memset(&sa, 0, sizeof sa);
                        sa.sa_handler = sigpass;
                        sa.sa_flags |= SA_RESTART;
       -                sigaction(i, &sa, nil);
       +                sigaction(sigs[i], &sa, nil);
                }
        
                for(;;){