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 / util.c < prev    next >
C/C++ Source or Header  |  1990-08-31  |  11KB  |  350 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.  
  23. /*************************************************************************/
  24. /* FUNCTION  : daemon_start                                              */
  25. /* PURPOSE   : To disassociate newsxd from the calling process so it can */
  26. /*             run as a daemon.                                          */
  27. /* ARGUMENTS : none                                                      */
  28. /*************************************************************************/
  29.  
  30. void
  31. daemon_start()
  32.  
  33. {
  34. register int    childpid, fd;
  35. extern   int    errno;
  36.  
  37.    /* Ignore the terminal stop signals */
  38.    (void) signal(SIGTTOU, SIG_IGN);
  39.    (void) signal(SIGTTIN, SIG_IGN);
  40.    (void) signal(SIGTSTP, SIG_IGN);
  41.  
  42.    /* Fork and let the parent process exit */
  43.    if ((childpid = fork()) < 0) {
  44.       logerr("newsxd: can't fork to enter daemon mode\n");
  45.       (void) exit(1);
  46.    } else if (childpid > 0) exit(0);
  47.  
  48.    /* Lose the process group */
  49.    if (setpgrp(0, getpid()) == -1) {
  50.       logerr("newsxd: can't change pgrp to enter daemon mode\n");
  51.       (void) exit(1);
  52.    }
  53.  
  54.    /* Lose the controlling terminal */
  55.    if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
  56.       (void) ioctl(fd, TIOCNOTTY, (char *) NULL);
  57.       (void) close(fd);
  58.    }
  59.  
  60.    /* Close any open files */
  61.    for (fd = 0; fd < NOFILE; fd++) (void) close(fd);
  62.    errno = 0;
  63.  
  64.    /* Set a proper default umask */
  65.    (void) umask(022);
  66. }
  67.  
  68. /*************************************************************************/
  69. /* FUNCTION  : getla                                                     */
  70. /* PURPOSE   : Return the current system load                            */
  71. /* ARGUMENTS : none                                                      */
  72. /* NOTES     : this code stolen from sendmail 5.61 which stole it from   */
  73. /*             from something else...                                    */
  74. /*************************************************************************/
  75.  
  76. int
  77. getla()
  78.  
  79. {
  80. #if defined(sun) | defined(mips)
  81. long    avenrun[3];
  82. #else
  83. double  avenrun[3];
  84. #endif
  85. extern  off_t lseek();
  86. static  int     kmem = -1;
  87.  
  88. static    struct  nlist Nl[] =
  89. {
  90.         { "_avenrun" },
  91. #define X_AVENRUN       0
  92.         { 0 },
  93. };
  94.  
  95.  
  96.    if (kmem < 0) {
  97.       kmem = open("/dev/kmem", 0, 0);
  98.       if (kmem < 0) return (-1);
  99.       (void) ioctl(kmem, (int) FIOCLEX, (char *) 0);
  100.       (void) nlist("/vmunix", Nl);
  101.       if (Nl[0].n_type == 0) return (-1);
  102.    }
  103.  
  104.    if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
  105.        read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
  106.    {
  107.       /* thank you Ian */
  108.       return (-1);
  109.    }
  110. #ifdef sun
  111.    return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
  112. #else
  113. #ifdef mips
  114.    return (FIX_TO_INT(avenrun[0]));
  115. #else
  116.    return ((int) (avenrun[0] + 0.5));
  117. #endif
  118. #endif
  119. }
  120.  
  121. /*************************************************************************/
  122. /* FUNCTION  : processarg                                                */
  123. /* PURPOSE   : Do %value substitutions in xargv                          */
  124. /* ARGUMENTS : which value of argv to modify, hostptr, classptr          */
  125. /*************************************************************************/
  126.  
  127. void
  128. processarg(which, hostptr, classptr)
  129.    int  which;
  130.    struct host  *hostptr;
  131.    struct class *classptr;
  132.  
  133. {
  134. char    buf1[MAXPATHLEN],
  135.         buf2[MAXPATHLEN],
  136.         charbuf[2],
  137.         *newxargv,
  138.         *strptr;
  139.  
  140.    if (classptr->xargv[which] == NULL) return;
  141.  
  142.    charbuf[1] = '\0';
  143.    *buf1 = '\0';
  144.  
  145.    for (strptr = classptr->xargv[which]; *strptr != '\0'; strptr++) {
  146.       if (*strptr != '%') {
  147.          charbuf[0] = *strptr;
  148.          (void) strcat(buf1, charbuf);
  149.       } else {
  150.          strptr++;
  151.          switch (*strptr) {
  152.             case 'h': (void) strcat(buf1, hostptr->hostname);
  153.                       break;
  154.             case 'f': (void) strcat(buf1, "%f");
  155.                       /* We shouldn't have seen a %f at this point! */
  156.                       break;
  157.             case 'd': (void) strcat(buf1, "%d");
  158.                       /* We shouldn't have seen a %d at this point! */
  159.                       break;
  160.             case 'b': (void) sprintf(buf2, batchfile, hostptr->hostname);
  161.                       (void) strcat(buf1, buf2);
  162.                       break;
  163.             case 'w': if (classptr->flags[C_NOWORK])
  164.                          (void) sprintf(buf2, batchfile, hostptr->hostname);
  165.                       else
  166.                          (void) sprintf(buf2, workfile, hostptr->hostname);
  167.                       (void) strcat(buf1, buf2);
  168.                       break;
  169.             case 's': (void) sprintf(buf2, "%d", hostptr->classslot);
  170.                       (void) strcat(buf1, buf2);
  171.                       break;
  172.             case '%': (void) strcat(buf1, "%"); /* %% changes to % */
  173.                       break;
  174.             default : strptr--;
  175.          }
  176.       }
  177.    }
  178.  
  179.    newxargv = (char *) malloc(strlen(buf1) + 1);
  180.    (void) strcpy(newxargv, buf1);
  181.    classptr->xargv[which] = newxargv;
  182.  
  183. }
  184.  
  185. /*************************************************************************/
  186. /* FUNCTION  : parse_time                                                */
  187. /* PURPOSE   : Check a single valid-time-string against the current time */
  188. /*             and return whether or not a match occurs.                 */
  189. /* ARGUMENTS : a pointer to the time-string                              */
  190. /*************************************************************************/
  191.  
  192. int
  193. parsetime(whattime)
  194. char    *whattime;
  195.  
  196. {
  197. static char *days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Wk" };
  198. long    clock;
  199. struct  tm *curtime;
  200. int     wday, start, stop, ltime, validday, loop, match;
  201.  
  202.    (void) time(&clock);
  203.    curtime = localtime(&clock);
  204.    wday = curtime->tm_wday;
  205.    validday = 0;
  206.    match = 1;
  207.  
  208.    while (match && isalpha(*whattime) && isupper(*whattime)) {
  209.       match = 0;
  210.       for (loop = 0; loop < 8; loop++) {
  211.          if (strncmp(days[loop], whattime, 2) == NULL) {
  212.             whattime += 2;
  213.             match = 1;
  214.             if ((wday == loop) | ((loop == 7) && wday && (wday < 6))) {
  215.                validday = 1;
  216.             }
  217.          }
  218.       }
  219.    }
  220.  
  221.    if (strncmp(whattime, "Any", 3) == NULL) {
  222.       validday = 1;
  223.       whattime += 3;
  224.    }
  225.  
  226.    if (!validday) return 0;
  227.  
  228.    if (sscanf(whattime, "%d-%d", &start, &stop) == 2) {
  229.       ltime = curtime->tm_min + 100 * curtime->tm_hour;
  230.       if ((start < stop) && ((ltime > start) & ltime < stop)) return 1;
  231.       if ((start > stop) && ((ltime > start) | ltime < stop)) return 1;
  232.    } else return 1;
  233.  
  234.    return 0;
  235. }
  236.  
  237. /*************************************************************************/
  238. /* FUNCTION  : validtime                                                 */
  239. /* PURPOSE   : Break apart a set of valid time-strings and pass them to  */
  240. /*             parse_time, returning whether or not ANY matches occurred */
  241. /* ARGUMENTS : a pointer to the time-string                              */
  242. /*************************************************************************/
  243.  
  244. int
  245. validtime(ptr)
  246. char    *ptr;
  247.  
  248. {
  249. char    *nextptr;
  250. int    good;
  251.  
  252.    while (1) {
  253.       nextptr = STRCHR(ptr, '|');
  254.       if (STRCHR(ptr, '|') == NULL) return(parsetime(ptr));
  255.       *nextptr = '\0';
  256.       good = parsetime(ptr);
  257.       *nextptr++ = '|';  /* gotta restore the | or things get skipped! */
  258.       if (good) return(1);
  259.       ptr = nextptr;
  260.    }
  261. }
  262.  
  263. /*************************************************************************/
  264. /* FUNCTION  : getclassslot                                              */
  265. /* PURPOSE   :                                                           */
  266. /* ARGUMENTS : a pointer to the class structure                          */
  267. /* RETURNS   : the slot number                                           */
  268. /*************************************************************************/
  269.  
  270. int    getclassslot(classptr)
  271. struct    class    *classptr;
  272.  
  273. {
  274. int    loop;
  275.  
  276.    for (loop = 0; loop < MAXCLASSXMITTERS; loop++)
  277.       if (classptr->slots[loop] != 'X') {
  278.          classptr->slots[loop] = 'X';
  279.          return loop;
  280.       }
  281.  
  282.    logerr("(getclassslot) Too many xmitters for class %s", classptr->classname);
  283.    return -1;
  284.  
  285. }
  286.  
  287. /*************************************************************************/
  288. /* FUNCTION  : freeclassslot                                             */
  289. /* PURPOSE   :                                                           */
  290. /* ARGUMENTS : slot number to free and a pointer to the class structure  */
  291. /*************************************************************************/
  292.  
  293. void    freeclassslot(classptr, slot)
  294. struct    class    *classptr;
  295. int        slot;
  296.  
  297. {
  298.  
  299.    classptr->slots[slot] = '.';
  300.  
  301. }
  302.  
  303. /*************************************************************************/
  304. /* FUNCTION  : idle                                                      */
  305. /* PURPOSE   : Set newsxd to idle mode.  Don't process xmitter queue...  */
  306. /* ARGUMENTS : none                                                      */
  307. /*************************************************************************/
  308.  
  309. void
  310. idle()
  311.  
  312. {
  313.  
  314.    daemon_idle = 1;
  315.  
  316. }
  317.  
  318. /*************************************************************************/
  319. /* FUNCTION  : reset                                                     */
  320. /* PURPOSE   : Kill all outstanding transmitters and idle newsxd.        */
  321. /* ARGUMENTS : none                                                      */
  322. /*************************************************************************/
  323.  
  324. void
  325. reset()
  326.  
  327. {
  328.  
  329.    kill_children(0);
  330.    daemon_idle = 1;
  331.  
  332. }
  333.  
  334. #ifdef CNEWSLOCKING
  335.  
  336. /*************************************************************************/
  337. /* FUNCTION  : unprivileged                                              */
  338. /* PURPOSE   : keep the cnews libraries happy.                           */
  339. /* ARGUMENTS : none used.                                                */
  340. /*************************************************************************/
  341.  
  342. int
  343. unprivileged(reason)
  344. char    *reason;
  345. {
  346.  
  347. }
  348.  
  349. #endif CNEWSLOCKING
  350.