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 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  6.3 KB  |  324 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)$Header: main.c,v 1.18 91/01/12 10:54:01 sob Exp $";
  3. #endif
  4.  
  5. /*
  6.  *    Network News Transfer Protocol server
  7.  *
  8.  *    Phil Lapsley
  9.  *    University of California, Berkeley
  10.  *    (Internet: phil@berkeley.edu; UUCP: ...!ucbvax!phil)
  11.  *    Stan Barber
  12.  *    Baylor College of Medicine
  13.  *    (Internet: sob@tmc.edu; UUCP: ...!bcm!sob)
  14.  */
  15.  
  16. #include "common.h"
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #ifndef EXCELAN
  20. #include <netdb.h>
  21. #else
  22. struct sockaddr_in current_peer = { AF_INET, IPPORT_NNTP };
  23. #endif
  24. #include <signal.h>
  25. /* XXX Should be #ifdef VARARGS */
  26. #if defined(sun) || defined(hpux)
  27. #include <varargs.h>
  28. #endif
  29.  
  30. #ifdef SETPROCTITLE
  31. char    **Argv = NULL;        /* pointer to argument vector */
  32. char    *LastArgv = NULL;    /* end of argv */
  33. #endif /* SETPROCTITLE */
  34.  
  35. main(argc,argv,envp)
  36. char **argv, **envp;
  37. {
  38.  
  39. #ifdef ALONE    /* If no inetd */
  40.  
  41.     int            sockt, client, length;
  42.     struct sockaddr_in    from;
  43.     extern int         reaper();
  44. #ifdef LOAD
  45.     register int load;
  46. #endif /* LOAD */
  47.  
  48.     disassoc();
  49.  
  50.     /* fd 0-2 should be open and point to / now. */
  51.  
  52.     /* Let's close all the other FD's just to be sure */
  53.     for(sockt = 3; sockt < 40; sockt++)
  54.         (void) close(sockt);
  55.  
  56. #ifdef SYSLOG
  57. #ifdef BSD_42
  58.     openlog("nntpd", LOG_PID);            /* fd 3 */
  59. #else /* !BSD_42 */
  60.     openlog("nntpd", LOG_PID, SYSLOG);        /* fd 3 */
  61. #endif /* BSD_42 */
  62. #endif /* SYSLOG */
  63.  
  64.  
  65. #ifdef FASTFORK
  66.     num_groups = read_groups();    /* Read active file now (fd 4) */
  67.                     /* and then do it every */
  68.     set_timer();            /* so often later */
  69. #endif /* FASTFORK */
  70.  
  71. #ifndef EXCELAN
  72.     sockt = get_socket();        /* should be fd 4 or 5 */
  73. #ifdef USG
  74.     (void) signal(SIGCLD, SIG_IGN);
  75. #else /* !USG */
  76.     (void) signal(SIGCHLD, reaper);
  77. #endif /* USG */
  78.  
  79. #ifdef DEBUG
  80.     (void) signal(SIGUSR1, debugup);
  81.     (void) signal(SIGUSR2, debugdown);
  82. #endif
  83.  
  84.     if (listen(sockt, SOMAXCONN) < 0) {
  85. #ifdef SYSLOG
  86.         syslog(LOG_ERR, "main: listen: %m");
  87. #endif /* SYSLOG */
  88.         exit(1);
  89.     }
  90. #endif /* EXCELAN */
  91.  
  92. #ifdef SETPROCTITLE
  93.     /*
  94.      *  Save start and extent of argv for setproctitle.
  95.      */
  96.  
  97.     Argv = argv;
  98. #ifdef SDD
  99.     LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
  100. #else /*SDD*/
  101.     while (*envp)
  102.         envp++;
  103.     LastArgv = envp[-1] + strlen(envp[-1]);
  104. #endif /*SDD*/
  105. #endif /* SETPROCTITLE */
  106. #if defined(LOAD) && defined(SETPROCTITLE)
  107.     /* If LOAD and SETPROCTITLE, display load before first accept() */
  108.     load = getla();
  109.     setproctitle("%sing connections: loadav %d",
  110.         load > LOAD ? "reject" : "accept", load);
  111. #endif /* LOAD && SETPROCTITLE */
  112.  
  113.     for (;;) {
  114. #ifdef LOAD
  115.         char oline[NNTP_STRLEN];
  116. #endif /* LOAD */
  117. #ifdef EXCELAN
  118.         int status;
  119.         sockt = 3;
  120.         sockt = get_socket();
  121.         if (sockt < 0)
  122.             continue;
  123. #ifdef USG
  124.         (void) signal(SIGCLD, SIG_IGN);
  125.         memset((char *)&from,'\0',sizeof(from));
  126. #else /* !USG */
  127.         bzero((char *)&from,sizeof(from));
  128. #endif /* USG */
  129.         client = accept(sockt, &from);
  130. #else /* !EXCELAN */
  131.         length = sizeof (from);
  132.         client = accept(sockt, &from, &length);
  133. #endif /* EXCELAN */
  134.         if (client < 0) {
  135. #ifdef SYSLOG
  136. #ifdef EXCELAN
  137.             if (errno != EINTR && errno != 60 )
  138. #else /* !EXCELAN */
  139.             if (errno != EINTR)
  140. #endif /* EXCELAN */
  141.                 syslog(LOG_ERR, "accept: %m\n");
  142. #endif /* SYSLOG */
  143. #ifdef EXCELAN
  144.             close(sockt);
  145.             sleep(1);
  146. #endif /* EXCELAN */
  147.             continue;
  148.         }
  149.  
  150. #ifdef LOAD
  151.         if (( load = getla()) > LOAD ) {
  152. #ifdef SETPROCTITLE
  153.             setproctitle("rejecting connections: loadav %d", load);
  154. #endif /* SETPROCTITLE */
  155.             sprintf( oline, "%d loadav at %d, try later\r\n",
  156.                 ERR_GOODBYE, load );
  157.             write( client, oline, strlen( oline ));
  158. #ifdef SYSLOG
  159.             syslog( LOG_INFO, "loadav at %d, sleeping", load );
  160. #endif /* SYSLOG */
  161.             close( client );
  162.             sleep( 5 );
  163.             continue;
  164.         } else {
  165. #ifdef SETPROCTITLE
  166.             setproctitle("accepting connections: loadav %d", load);
  167. #endif /* SETPROCTITLE */
  168.         }
  169. #endif /* LOAD */
  170.  
  171.         switch (fork()) {
  172.         case    -1:
  173. #ifdef SYSLOG
  174.                 syslog(LOG_ERR, "fork: %m\n");
  175. #endif /* SYSLOG */
  176. #ifdef EXCELAN
  177.                 (void) close(sockt);
  178. #endif /* EXCELAN */
  179.                 (void) close(client);
  180.                 break;
  181.  
  182.         case    0:
  183. #ifdef EXCELAN
  184.                 if (fork())
  185.                     exit(0);
  186. #ifdef USG
  187.                 (void * )memcpy(¤t_peer,&from,
  188.                      sizeof(from));
  189. #else /* !USG */
  190.                 bcopy(&from,¤t_peer,sizeof(from));
  191. #endif /* USG */
  192.                 make_stdio(sockt);
  193. #else /* !EXCELAN */
  194.                 (void) close(sockt);
  195.                 make_stdio(client);
  196. #endif /* EXCELAN */
  197. #ifdef USG
  198.                 (void) signal(SIGCLD,SIG_DFL);
  199. #endif /* USG */
  200.                 serve();
  201.                 break;
  202.  
  203.         default:
  204. #ifdef EXCELAN
  205.                 (void) close(sockt);
  206. #else /* !EXCELAN */
  207.                 (void) close(client);
  208. #endif /* EXCELAN */
  209.                 break;
  210.         }
  211.     }
  212.  
  213. #else /* !ALONE */        /* We have inetd */
  214.  
  215. #ifdef LOAD
  216.     {
  217.         register int load;
  218.  
  219.         if (( load = getla()) > LOAD ) {
  220.             printf("%d loadav at %d, try later\r\n", 
  221.                    ERR_GOODBYE, load );
  222. #ifdef SYSLOG
  223. #ifdef BSD_42
  224.             openlog("nntpd", LOG_PID);
  225. #else /* !BSD_42 */
  226.             openlog("nntpd", LOG_PID, SYSLOG);
  227. #endif /* BSD_42 */
  228.             syslog( LOG_INFO, "loadav at %d, exiting", load );
  229. #endif /* SYSLOG */
  230.             (void) fflush(stdout);
  231.             exit(1);
  232.         }
  233.     }
  234. #endif /* LOAD */
  235. #ifdef SETPROCTITLE
  236.     /*
  237.      *  Save start and extent of argv for setproctitle.
  238.      */
  239.  
  240.     Argv = argv;
  241. #ifdef SDD
  242.     LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
  243. #else /*SDD*/
  244.     while (*envp)
  245.         envp++;
  246.     LastArgv = envp[-1] + strlen(envp[-1]);
  247. #endif /*SDD*/
  248. #endif /* SETPROCTITLE */
  249.  
  250. #ifdef USG
  251.     (void) signal(SIGCLD,SIG_DFL);
  252. #endif /* USG */
  253.  
  254.     serve();
  255.  
  256. #endif /* ALONE */
  257. }
  258.  
  259. /*
  260.  * clobber argv so ps will show what we're doing.
  261.  * stolen from sendmail
  262.  */
  263. #ifdef SETPROCTITLE
  264. #if defined(sun) || defined(hpux)
  265. /*VARARGS*/
  266. void
  267. setproctitle(va_alist)
  268.     va_dcl
  269. {
  270.     register char *p, *fmt;
  271.     register int i;
  272.     char buf[BUFSIZ];
  273.     va_list ap;
  274.  
  275.     va_start(ap);
  276.     fmt = va_arg(ap, char *);
  277.     
  278.     (void) vsprintf(buf, fmt, ap);
  279.  
  280.     /* make ps print "(nntpd)" */
  281.     p = Argv[0];
  282.     *p++ = '-';
  283.  
  284.     i = strlen(buf);
  285.     if (i > LastArgv - p - 2) {
  286.     i = LastArgv - p - 2;
  287.     buf[i] = '\0';
  288.     }
  289.     strcpy(p, buf);
  290.     p += i;
  291.     while (p < LastArgv)
  292.     *p++ = ' ';
  293.  
  294.     va_end(ap);
  295. }
  296. #else
  297. /*VARARGS1*/
  298. void
  299. setproctitle(fmt, a, b, c)
  300. char *fmt;
  301. {
  302.     register char *p;
  303.     register int i;
  304.     char buf[BUFSIZ];
  305.  
  306.     (void) sprintf(buf, fmt, a, b, c);
  307.  
  308.     /* make ps print "(nntpd)" */
  309.     p = Argv[0];
  310.     *p++ = '-';
  311.  
  312.     i = strlen(buf);
  313.     if (i > LastArgv - p - 2) {
  314.         i = LastArgv - p - 2;
  315.         buf[i] = '\0';
  316.     }
  317.     (void) strcpy(p, buf);
  318.     p += i;
  319.     while (p < LastArgv)
  320.         *p++ = ' ';
  321. }
  322. #endif /* hpux */
  323. #endif /* SETPROCTITLE */
  324.