home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / mindexd / daemon.c < prev    next >
C/C++ Source or Header  |  1992-04-08  |  3KB  |  155 lines

  1. #include "mindexd.h"
  2.  
  3. #define MY_BSD 1       /* System V signal handling doesn't seem to work
  4.                Correctly */
  5.  
  6. /*
  7.  * A BSD style SIGCLD signal handler that can be used by a server
  8.  * that's not interested in its child's exit status, but needs to
  9.  * wait for them, to avoid clogging up the system with zombies.
  10.  *
  11.  * Beware that the calling process may get an interrupted system
  12.  * call when we return, so they had better handle that.
  13.  *
  14.  * (From Stevens pg 82)
  15.  */
  16.  
  17. #include <sys/ioctl.h>
  18. #include <sys/wait.h>
  19. #include <signal.h>
  20.  
  21. #ifdef HP9000
  22.   #define TIOCNOTTY       _IO('t', 113)           /* void tty association */
  23. #endif
  24.  
  25. sig_child()
  26. {
  27. #ifdef MY_BSD
  28.      /*
  29.       * Use the wait3() system call with the WNOHANG option
  30.       */
  31.  
  32.      int pid;
  33.      union wait  status;
  34.  
  35.      while ( (pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
  36.       ;
  37. #endif
  38. }
  39.  
  40.  
  41. #include <stdio.h>
  42. #include <sys/param.h>
  43. #include <errno.h>
  44. extern int errno;
  45.  
  46. #ifdef SIGTSTP    /* True on a BSD system */
  47. #include <sys/file.h>
  48. #include <sys/ioctl.h>
  49. #endif
  50.  
  51. /*
  52.  * Detach a daemon process from login session context
  53.  */
  54.  
  55. daemon_start(ignsigcld)
  56. int ignsigcld;          /* Nonzero -> nuke zombie children */
  57. {
  58.      register int childpid, fd;
  59.  
  60.      /*
  61.       * If we were started by init (process 1) from the /etc/inittab
  62.       * file there's no need to detach.
  63.       */
  64.       
  65.      if (getppid() != 1) {
  66.  
  67.       
  68.  
  69. #ifdef SIGTTOU
  70.       signal(SIGTTOU, SIG_IGN);
  71. #endif
  72. #ifdef SIGTTIN
  73.       signal(SIGTTIN, SIG_IGN);
  74. #endif
  75. #ifdef SIGTSTP
  76.       signal(SIGTSTP, SIG_IGN);
  77. #endif
  78.  
  79.       /*
  80.        * If we were not started in the background, fork and let
  81.        * the parent exit.  This also guarantees the first child
  82.        * is not a process group leader.
  83.        */
  84.  
  85.       if ( (childpid = fork()) < 0)
  86.            err_sys("can't fork first child");
  87.       else if (childpid >0)
  88.            exit(0);     /* parent */
  89.       
  90.       /*
  91.        * First Child process
  92.        *
  93.        * Disassociate from controlling terminal and process group.
  94.        * Ensure the process can't reacquire a new controlling terminal
  95.        */
  96.       
  97. #ifdef SIGTSTP  /* BSD */
  98.  
  99.       if (setpgrp(0, getpid()) == -1)
  100.            err_sys("can't change process group");
  101.       
  102.       if ( (fd = open("/dev/tty", O_RDWR)) >= 0) {
  103.            ioctl(fd, TIOCNOTTY, (char *)NULL); /* lose controlling TTY*/
  104.            close(fd);
  105.       }
  106.       
  107. #else /* System V */
  108.       
  109.       if (setpgrp() == -1)
  110.            err_sys("Can't change process group");
  111.       
  112.       signal(SIGHUP, SIG_IGN);  /* immune from pgrp leader death */
  113.       
  114.       if ( (childpid = fork()) < 0)
  115.            err_sys("Can't fork second child");
  116.       else if (childpid > 0)
  117.            exit(0); /* First child */
  118.       
  119.       /* second child */
  120. #endif
  121.  
  122.      }  /* End of if test for ppid == 1 */
  123.  
  124.      /** Close any file descriptors **/
  125.  
  126.      for (fd = 0; fd < NOFILE; fd++)
  127.       close(fd);
  128.  
  129.      errno = 0;  /* probably set to EBADF from a close */
  130.  
  131.  
  132.      /** Change the current directory to / **/
  133.  
  134.       chdir("/");
  135.  
  136.      /** Clear inherited file mode creation mask. **/
  137.  
  138.      umask(0);
  139.  
  140.      /*
  141.       * See if the caller isn't ineterested in the exit status of its
  142.       * children, and doesn't want to have them become zombies
  143.       */
  144.  
  145.      if (ignsigcld) {
  146. #ifdef SIGTSTP
  147.       int sig_child();
  148.  
  149.       signal(SIGCLD, sig_child);  /* BSD */
  150. #else
  151.       signal(SIGCLD, SIG_IGN);    /* System V */
  152. #endif
  153.      }
  154. }
  155.