home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / tcpip / netkit-a.06 / netkit-a / NetKit-A-0.06 / dip337d-uri / daemon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-10  |  2.7 KB  |  142 lines

  1. /*
  2.  * dip        A program for handling dialup IP connecions.
  3.  *        Handle the process of going into the background.
  4.  *
  5.  * Version:    @(#)daemon.c    3.3.3    08/16/93
  6.  *
  7.  * Author:      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  8.  *        Copyright 1988-1993 MicroWalt Corporation
  9.  *
  10.  *        This program is free software; you can redistribute it
  11.  *        and/or  modify it under  the terms of  the GNU General
  12.  *        Public  License as  published  by  the  Free  Software
  13.  *        Foundation;  either  version 2 of the License, or  (at
  14.  *        your option) any later version.
  15.  */
  16. #include "dip.h"
  17.  
  18. extern int tty_lock(char *path, int mode);
  19. static void (*oldalrmsig)(int);
  20.  
  21. /* Catch any signals. */
  22. static void
  23. sig_catcher(int sig)
  24. {
  25.   alarm (0);
  26.   (void) signal (SIGALRM, oldalrmsig);
  27.  
  28.   (void) tty_close();
  29.   exit(0);
  30. }
  31.  
  32.  
  33. static void sig_login_catcher(int sig)
  34. {
  35.   (void) signal (SIGALRM, oldalrmsig);
  36.   alarm (0);
  37.  
  38.   (void) tty_login_close();
  39.   exit(0);
  40. }
  41.  
  42.  
  43.  
  44. /* Record the current processID in a file. */
  45. static void
  46. dip_record(void)
  47. {
  48.   FILE *fp;
  49.  
  50.   fp = fopen(_PATH_DIP_PID, "w");
  51.   if (fp == NULL) {
  52.     syslog(LOG_ERR, "DIP: cannot create %s: %s\n",
  53.        _PATH_DIP_PID, strerror(errno));
  54.     fprintf(stderr, "DIP: cannot create %s: %s\n",
  55.         _PATH_DIP_PID, strerror(errno));
  56.     return;
  57.   }
  58.   fprintf(fp, "%d", getpid());
  59.   (void) fclose(fp);
  60. }
  61.  
  62.  
  63. int
  64. dip_setup(struct dip *dp)
  65. {
  66.   int i;
  67.  
  68.   oldalrmsig = signal (SIGALRM, SIG_IGN);
  69.   for(i = 1; i < 32; i++) 
  70.     (void) signal(i, SIG_IGN);
  71.   (void) signal(SIGHUP,  sig_login_catcher);
  72.   (void) signal(SIGINT,  sig_catcher);
  73.   (void) signal(SIGTERM, sig_catcher);
  74.   (void) signal(SIGQUIT,  sig_catcher);
  75.  
  76.   (void) chdir(dp->home);
  77.  
  78.   /* Fire up the protocol here. */
  79.   protosw[dp->protonr-1].func(dp);
  80.  
  81.   /* Wait forever to terminate. */
  82.   while(1) {
  83.     (void) sleep(30);
  84.   }
  85.  
  86.   return(0);
  87. }
  88.  
  89.  
  90.  
  91. int dip_login_setup(struct dip *dp)
  92. {
  93.   int i;
  94.  
  95.   oldalrmsig = signal (SIGALRM, SIG_IGN);
  96.   for(i = 1; i < 32; i++) 
  97.     (void) signal(i, SIG_IGN);
  98.   (void) signal(SIGHUP,  sig_login_catcher);
  99.   (void) signal(SIGINT,  sig_catcher);
  100.   (void) signal(SIGTERM, sig_login_catcher);
  101.   (void) signal(SIGQUIT, sig_catcher);
  102.  
  103.   (void) chdir(dp->home);
  104.  
  105.   /* Fire up the protocol here. */
  106.   protosw[dp->protonr-1].func(dp);
  107.  
  108.   /* Wait forever to terminate. */
  109.   while(1) {
  110.     (void) sleep(30);
  111.   }
  112.  
  113.   return(0);
  114. }
  115.  
  116.  
  117.  
  118. int
  119. dip_daemon(struct dip *dip)
  120. {
  121.   int i;
  122.  
  123.   /* First of all, fork off a sub-process. */
  124.   if ((i = fork()) < 0) return(-1);
  125.   if (i != 0) exit(0);
  126.  
  127. #if 1
  128.   /* Make tty the control terminal, detect DCD loss from now. */
  129.   if ((i = tty_notlocal()) < 0)
  130.       return i;
  131. #endif
  132.  
  133.   /* Record our PID. */
  134.   dip_record();
  135.  
  136.   /* Re-acquire the lock */
  137.   (void)tty_lock("no_matter", 2);
  138.  
  139.   /* Standard BSD behaviour: change to the root dir. */
  140.   return(dip_setup(dip));
  141. }
  142.