home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / usenet / newsxd.tar / newsxd / main.c < prev    next >
C/C++ Source or Header  |  1990-08-31  |  5KB  |  159 lines

  1. /*
  2.  * #include <legal/bs.h>
  3.  >
  4.  > Copyright (c) 1989 Washington University in Saint Louis, Missouri and
  5.  > Chris Myers. All rights reserved.
  6.  >
  7.  > Permission is hereby granted to copy, reproduce, redistribute or
  8.  > otherwise use this software as long as: (1) there is no monetary
  9.  > profit gained specifically from the use or reproduction of this
  10.  > software, (2) it is not sold, rented, traded, or otherwise marketed,
  11.  > (3) the above copyright notice and this paragraph is included
  12.  > prominently in any copy made, and (4) that the name of the University
  13.  > is not used to endorse or promote products derived from this software
  14.  > without the specific prior written permission of the University.
  15.  > THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  > IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  > WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  >
  19.  */
  20.  
  21. #include "defs.h"
  22. char    *progname = "newsxd";
  23.  
  24. /*************************************************************************/
  25. /* FUNCTION  : main                                                      */
  26. /* PURPOSE   :                                                           */
  27. /* ARGUMENTS : argc and argv passed in by calling program                */
  28. /*************************************************************************/
  29.  
  30. void
  31. main(argc, argv, envp)
  32.    int  argc;
  33.    char *argv[], *envp[];
  34.  
  35. {
  36. int     loop,
  37.         ltime,
  38.         clock,
  39.     pid,
  40.         sleeptime;
  41.  
  42. FILE    *pidout;
  43.  
  44. struct  tm      *curtime;
  45.  
  46.    debug = 0;
  47.    DEBUG = 0;
  48.    newsxdebug = 0;
  49.  
  50.    (void) strcpy(configfile, default_configfile);
  51.    (void) strcpy(statusfile, default_statusfile);
  52.    (void) strcpy(pidfile, default_pidfile);
  53. #ifdef  FAKESYSLOG
  54.    (void) strcpy(fakelogfile, FAKESYSLOG);
  55. #endif  FAKESYSLOG
  56.  
  57.    for (loop = 0; loop < argc; loop++) {
  58.  
  59.       if (strcmp(argv[loop], "-v") == 0) {
  60.          (void) fprintf(stderr, "newsxd version %1.1f", VERSION);
  61. #if    PATCHLEVEL > 0
  62.          (void) fprintf(stderr, "-%d", PATCHLEVEL);
  63. #endif
  64.          (void) fprintf(stderr, ", written by Chris Myers <chris@wugate.wustl.edu>\n");
  65. #ifdef SYSLOG
  66.          (void) fprintf(stderr, "Logging to syslog\n");
  67. #endif
  68. #ifdef FAKESYSLOG
  69.          (void) fprintf(stderr, "Logging to %s\n", fakelogfile);
  70. #endif
  71.          exit(0);
  72.       }
  73.       if (strcmp(argv[loop], "-debug") == 0) debug++;
  74.       if (strcmp(argv[loop], "-DEBUG") == 0) { DEBUG++; debug++; }
  75.       if (strcmp(argv[loop], "-newsxdebug") == 0) newsxdebug++;
  76.       if ((strcmp(argv[loop], "-c") == 0) && (loop < argc - 1))
  77.          (void) strcpy(configfile, argv[++loop]);
  78.       if ((strcmp(argv[loop], "-pid") == 0) && (loop < argc - 1))
  79.          (void) strcpy(pidfile, argv[++loop]);
  80.       if ((strcmp(argv[loop], "-status") == 0) && (loop < argc - 1))
  81.          (void) strcpy(statusfile, argv[++loop]);
  82. #ifdef  FAKESYSLOG
  83.       if ((strcmp(argv[loop], "-log") == 0) && (loop < argc - 1))
  84.          (void) strcpy(fakelogfile, argv[++loop]);
  85. #endif
  86.  
  87.    }
  88.  
  89.    (void) signal(SIGCHLD, xmit_done);
  90.    (void) signal(SIGHUP, read_config);
  91.    (void) signal(SIGQUIT, dump_config);
  92.    (void) signal(SIGTERM, kill_children);
  93.    (void) signal(SIGUSR1, debug_on);
  94.    (void) signal(SIGUSR2, debug_off);
  95.    (void) signal(SIGIO, idle);
  96.    (void) signal(SIGIOT, reset);
  97.    (void) signal(SIGTRAP, dump_info);
  98.  
  99.    (void) setuid(geteuid());
  100.    (void) setgid(getegid());
  101.  
  102.    if (!debug) daemon_start();
  103.  
  104. #ifdef  SYSLOG
  105. # ifdef LOG_LOCAL7
  106.    openlog("newsxd", LOG_PID, SYSLOG);
  107. # else
  108.    openlog("newsxd", LOG_PID);
  109. # endif
  110. #endif
  111.  
  112.    pidout = fopen(pidfile, "r");
  113.    if (pidout != (FILE *) NULL) {
  114.       (void) fscanf(pidout, "%d", &pid);
  115.       (void) fclose(pidout);
  116.       if (kill(pid, 0) == 0) {
  117.          logerr("duplicate invocation of newsxd -- aborting\n");
  118.          (void) exit(1);
  119.       }
  120.    }
  121.  
  122.    log(LOG_INFO, "starting\n");
  123.  
  124.    for (loop = 0; loop < MAXXMITTERS; loop++) {
  125.       pidlist[loop] = 0;
  126.       pidmap[loop] = (struct host *) NULL;
  127.    }
  128.  
  129.    read_config(0);
  130.  
  131.    if (debug) dump_config();
  132.  
  133.    pidout = fopen(pidfile, "w");
  134.  
  135.    if (pidout != (FILE *) NULL) {
  136.       (void) fprintf(pidout, "%d\n", getpid());
  137.       (void) fflush(pidout);
  138.       (void) fclose(pidout);
  139.    }
  140.  
  141.    while (1) {
  142.       if (!daemon_idle) run_queue();
  143.  
  144.       xmit_done(0);
  145.  
  146.       (void) time(&clock);
  147.       curtime = localtime(&clock);
  148.       ltime = curtime->tm_sec + curtime->tm_min * 60 + curtime->tm_hour * 3600;
  149.  
  150.       sleeptime = queueinterval - (ltime % queueinterval);
  151.       if (sleeptime == 0) sleeptime++;
  152.  
  153.       dprintf("%d:sleeping for %d seconds\n", ltime, sleeptime);
  154.  
  155.       (void) sleep((unsigned) sleeptime);
  156.  
  157.    }
  158. }
  159.