home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / netaux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  4.9 KB  |  277 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)netaux.c    1.11    (Berkeley) 2/25/88";
  3. #endif
  4.  
  5. /*
  6.  * Routines to deal with network stuff for
  7.  * stand-alone version of server.
  8.  */
  9.  
  10. #include "common.h"
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #ifndef EXCELAN
  14. #include <netdb.h>
  15. #endif not EXCELAN
  16. #include <sys/ioctl.h>
  17. #include <signal.h>
  18. #ifdef USG
  19. #include <time.h>
  20. #else not USG
  21. #include <sys/time.h>
  22. #endif USG
  23.  
  24. #ifdef ALONE
  25.  
  26.  
  27. /*
  28.  * disassociate this process from the invoker's terminal.
  29.  * Close all file descriptors, and then open 0, 1, and 2 to
  30.  * somewhere bogus (i.e., "/", O_RDONLY).  This way we will know
  31.  * that stdin/out/err will at least be claimed.
  32.  *
  33.  *    Parameters:    None.
  34.  *
  35.  *    Returns:    Nothing.
  36.  *
  37.  *    Side effects:    Disassociates this process from
  38.  *            a terminal; closes file descriptors;
  39.  *            fd 0-2 opened as O_RDONLY to /.
  40.  */
  41.  
  42. disassoc()
  43. {
  44.     register int    i;
  45.  
  46. #ifdef USG
  47.     (void) signal(SIGTERM, SIG_IGN);
  48.     (void) signal(SIGINT, SIG_IGN);
  49.     (void) signal(SIGQUIT, SIG_IGN);
  50. #endif
  51.  
  52.     if (fork())
  53.         exit(0);
  54.  
  55.     for (i = 0; i < 10; i++)
  56.         (void) close(i);
  57.  
  58. #ifdef USG
  59.     (void) open("/", 0);
  60.     (void) dup2(0, 1);
  61.     (void) dup2(0, 2);
  62.     setpgrp();
  63.     umask(000);
  64. #else /* not USG */
  65.     i = open("/dev/tty", O_RDWR);
  66.     if (i >= 0) {
  67.         ioctl(i, TIOCNOTTY, 0);
  68.         (void) close(i);
  69.     }
  70.  
  71.     i = open("/", O_RDONLY);
  72.     if (i >= 0) {
  73.         if (i != 0) {            /* should never happen */
  74.             (void) dup2(i, 0);
  75.             (void) close(i);
  76.         }
  77.         (void) dup2(0, 1);
  78.         (void) dup2(1, 2);
  79.     }
  80. #endif /* not USG */
  81. }
  82.  
  83.  
  84. /*
  85.  * get_socket -- create a socket bound to the appropriate
  86.  *    port number.
  87.  *
  88.  *    Parameters:    None.
  89.  *
  90.  *    Returns:    Socket bound to correct address.
  91.  *
  92.  *    Side effects:    None.
  93.  *
  94.  *    Errors:        Syslogd, cause abortion.
  95.  */
  96.  
  97. get_socket()
  98. {
  99.     int            s;
  100.     struct sockaddr_in    sin;
  101. #ifndef EXCELAN
  102.     struct servent        *sp;
  103.     int on = 1;
  104.  
  105.     sp = getservbyname("nntp", "tcp");
  106.     if (sp == NULL) {
  107. #ifdef SYSLOG
  108.         syslog(LOG_ERR, "get_socket: tcp/nntp, unknown service.");
  109. #endif
  110.         exit(1);
  111.     }
  112. #endif /* not EXCELAN */
  113.  
  114.     bzero((char *) &sin, sizeof (sin));
  115.     sin.sin_family = AF_INET;
  116.     sin.sin_addr.s_addr = htonl(INADDR_ANY);
  117. #ifndef EXCELAN
  118.     sin.sin_port = sp->s_port;
  119.  
  120.     s = socket(AF_INET, SOCK_STREAM, 0);
  121. #else /* EXCELAN */
  122.     sin.sin_port = htons(IPPORT_NNTP);
  123.     s = 3;        /* WTF??? */
  124.     s = socket(SOCK_STREAM, (struct sockproto *)0, &sin,
  125.         (SO_KEEPALIVE|SO_ACCEPTCONN));
  126. #endif /* EXCELAN */
  127.     if (s < 0) {
  128. #ifdef EXCELAN
  129.         sleep(5);
  130.         return (-1);
  131. #else /* not EXCELAN */
  132. #ifdef SYSLOG
  133.         syslog(LOG_ERR, "get_socket: socket: %m");
  134. #endif /* SYSLOG */
  135.         exit(1);
  136. #endif /* not EXCELAN */
  137.     }
  138. #ifndef EXCELAN
  139. #ifdef SO_REUSEADDR
  140.     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  141.         (char *) &on, sizeof (on)) < 0)
  142.         perror("setsockopt - SO_REUSEADDR");
  143. #endif /* SO_REUSEADDR */
  144.     if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
  145. #ifdef SYSLOG
  146.         syslog(LOG_ERR, "get_socket: bind: %m");
  147. #endif
  148.         exit(1);
  149.     }
  150. #endif /* not EXCELAN */
  151.  
  152.     return (s);
  153. }
  154.  
  155. /*
  156.  * make_stdio -- make a given socket be our standard input
  157.  *    and output.
  158.  *
  159.  *    Parameters:    "sockt" is the socket we want to
  160.  *            be file descriptors 0, 1, and 2.
  161.  *
  162.  *    Returns:    Nothing.
  163.  *
  164.  *    Side effects:    None.
  165.  */
  166.  
  167. make_stdio(sockt)
  168.     int    sockt;
  169. {
  170.     if (sockt != 0) {
  171.         (void) dup2(sockt, 0);
  172.         (void) close(sockt);
  173.     }
  174.     (void) dup2(0, 1);
  175.     (void) dup2(1, 2);
  176. }
  177.  
  178. /*
  179.  * set_timer -- set up the interval timer so that
  180.  *    the active file is read in every so often.
  181.  *
  182.  *    Parameters:    None.
  183.  *
  184.  *    Returns:    Nothing.
  185.  *
  186.  *    Side effects:    Sets interval timer to READINTVL seconds.
  187.  *            Sets SIGALRM to call read_again.
  188.  */
  189.  
  190. set_timer()
  191. {
  192. #ifndef USG
  193.     struct itimerval    new, old;
  194. #endif /* not USG */
  195.     extern int        read_again();
  196.  
  197.     (void) signal(SIGALRM, read_again);
  198. #ifdef USG
  199.     alarm(READINTVL);
  200. #else /* not USG */
  201.  
  202.     new.it_value.tv_sec = READINTVL;
  203.     new.it_value.tv_usec = 0;
  204.     new.it_interval.tv_sec = READINTVL;
  205.     new.it_interval.tv_usec = 0;
  206.     old.it_value.tv_sec = 0;
  207.     old.it_value.tv_usec = 0;
  208.     old.it_interval.tv_sec = 0;
  209.     old.it_interval.tv_usec = 0;
  210.  
  211.     if (setitimer(ITIMER_REAL, &new, &old) < 0) {
  212. #ifdef SYSLOG
  213.         syslog(LOG_ERR, "set_timer: setitimer: %m\n");
  214. #endif SYSLOG
  215.         exit(1);
  216.     }
  217. #endif /* not USG */
  218. }
  219.  
  220.  
  221. /*
  222.  * read_again -- (maybe) read in the active file again,
  223.  *    if it's changed since the last time we checked.
  224.  *
  225.  *    Parameters:    None (called by interrupt).
  226.  *
  227.  *    Returns:    Nothing.
  228.  *
  229.  *    Side effects:    May change "num_groups" and "group_array".
  230.  */
  231.  
  232. read_again()
  233. {
  234.     static long    last_mtime;    /* Last time active file was changed */
  235.     struct stat    statbuf;
  236.  
  237.     if (stat(activefile, &statbuf) < 0)
  238.         return;
  239.  
  240.     if (statbuf.st_mtime != last_mtime) {
  241.         last_mtime = statbuf.st_mtime;
  242.         num_groups = read_groups();
  243.     }
  244. }
  245.  
  246.  
  247. /*
  248.  * reaper -- reap children who are ready to die.
  249.  *    Called by signal.
  250.  *
  251.  *    Parameters:    None.
  252.  *
  253.  *    Returns:    Nothing.
  254.  *
  255.  *    Side effects:    None.
  256.  */
  257.  
  258. reaper()
  259. {
  260. #ifndef USG
  261.     union wait    status;
  262.  
  263.     while (wait3(&status, WNOHANG, (struct rusage *)0) > 0)
  264.         ;
  265. #endif /* not USG */
  266. }
  267.  
  268. #else /* not ALONE */
  269.  
  270. /* Kludge for greenhill's C compiler */
  271.  
  272. static
  273. netaux_greenkludge()
  274. {
  275. }
  276. #endif /* not ALONE */
  277.