home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCPbb_2_1_src.lzh / UUCPBB21 / uuxqt.c < prev    next >
Text File  |  1994-09-25  |  12KB  |  483 lines

  1. /*  uuxqt.c   The main program which processes work from a remote system.
  2.     Copyright (C) 1990, 1993  Rick Adams and Bob Billson
  3.  
  4.     This file is part of the OS-9 UUCP package, UUCPbb.
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     The author of UUCPbb, Bob Billson, can be contacted at:
  21.     bob@kc2wz.bubble.org  or  uunet!kc2wz!bob  or  by snail mail:
  22.     21 Bates Way, Westfield, NJ 07090
  23. */
  24.  
  25. /* Usage:  uuxqt [opts]  <sys> [<sys>...]  [opts]
  26.  
  27.         <sys>  -process work for <sys>  If <sys> name is 'ALL', all
  28.                   systems in the Systems file are processed
  29.  
  30.    opts: -xN   -set debug level to N.  0 (default) = OFF, 9 = highest
  31.          -q    -work quietly, i.e. no messages to the screen */
  32.  
  33. #define MAIN
  34.  
  35. #include "uucp.h"
  36. #ifndef _OSK
  37. #include <os9.h>
  38. #endif
  39. #include <signal.h>
  40. #include <dir.h>
  41. #include "getopt.h"
  42.  
  43. #define MOREWORK  128                          /* more work signal */
  44. #define PROC      "procs"                      /* PROC command */
  45.  
  46. extern QQ int childid;                         /* defined in docmd.c */
  47.  
  48. EXTERN QQ unsigned myuid;
  49.  
  50. QQ int debuglvl = 0;                           /* made direct page variable */
  51. QQ flag quiet = FALSE;                         /* don't work silently */
  52. QQ flag didwork = FALSE;
  53. QQ flag allflag = FALSE;
  54. QQ flag morework = FALSE;                      /* signal flag */
  55. QQ flag logopen = FALSE;
  56. QQ char *errormsg = "ERROR--";
  57. QQ char *rnews = "rnews";
  58. QQ char *rmail = "rmail";
  59. QQ char **syslist;
  60. QQ FILE *log;
  61. char sender[6] = "uuxqt";
  62. char sysname[9] = "";
  63. char fname[100];
  64. void openlog(), closelog();
  65.  
  66.  
  67. main (argc, argv)
  68. int argc;
  69. char *argv[];
  70. {
  71.      int option, interrupt();
  72.      register char **sysptr;
  73.  
  74.      log = NULL;
  75.      childid = -1;                                 /* no children yet */
  76.  
  77.      intercept (interrupt);                        /* set signal trap */
  78.      myuid = getuid();
  79.  
  80.      if ((logdir = getenv ("LOGDIR")) != NULL)
  81.           logdir = strdup (logdir);
  82.      else
  83.           logdir = LOGDIR;
  84.  
  85.      openlog();
  86.  
  87.      /* is another copy of uuxqt currently running? */
  88.      isuuxqtrunning();
  89.  
  90.      if ((spooldir = getdirs ("spooldir")) == NULL)
  91.           fatal ("spooldir not in Parameters");
  92.  
  93.      syslist = sysptr = (char **) malloc (argc * sizeof (char));
  94.      
  95.      if (syslist == NULL)
  96.           fatal ("can't malloc() syslist");
  97.  
  98.      *syslist = NULL;
  99.  
  100.      while (((option = getopt (argc, argv, "d:x:qs:?")) != NONOPT)
  101.              || optarg != NULL)
  102.        {
  103.           switch (option)
  104.             {
  105.                case 'd':
  106.                case 'x':
  107.                     debuglvl = atoi (optarg);
  108.                     break;
  109.  
  110.                case 'q':
  111.                     quiet = TRUE;
  112.                     break;
  113.  
  114.                case 's':
  115.                case NONOPT:
  116.                     if (strcmp (optarg, "ALL") == 0)
  117.                          allflag = TRUE;
  118.  
  119.                     *sysptr++ = optarg;
  120.                     *sysptr = NULL;
  121.                     break;
  122.  
  123.                case '?':
  124.                     usage();
  125.                     break;
  126.             }
  127.        }
  128.  
  129.      if (!*syslist)
  130.        {
  131.           fputs ("uuxqt: no system specified\n\n", stderr);
  132.           usage();
  133.        }
  134.  
  135. #ifdef _OSK
  136.      if (modlink (rnews, 0) == -1)
  137.        {
  138.           char tmp[64];
  139.  
  140.           if (modloadp (rnews, 0, tmp) == -1)
  141.                fatal ("can't load rnews");
  142.        }
  143.  
  144.      if (modlink (rmail, 0) == -1)
  145.        {
  146.           char tmp[64];
  147.  
  148.           if (modloadp (rmail, 0, tmp) == -1)
  149.             {
  150.                munload (rnews, 0);
  151.                fatal ("can't load rmail");
  152.             }
  153.        }
  154. #else
  155.      if (nmlink (rnews, 0, 0) == -1)
  156.           if (nmload (rnews, 0, 0) == -1)
  157.                fatal ("can't load rnews");
  158.  
  159.      if (nmlink (rmail, 0, 0) == -1)
  160.           if (nmload (rmail, 0, 0) == -1)
  161.             {
  162.                munload (rnews, 0);
  163.                fatal ("can't load rmail");
  164.             }
  165. #endif
  166.  
  167.      do
  168.           processmail();
  169.      while (morework);
  170.  
  171.      free (syslist);
  172.      free (spooldir);
  173.  
  174.      /* unlink modules and close log file */
  175.      munload (rmail, 0);
  176.      munload (rnews, 0);
  177.      closelog();
  178.      exit (0);
  179. }
  180.  
  181.  
  182.  
  183. /* Check to see if another copy of uuxqt is running.  If it is, send it a
  184.    signal to indicate new mail which it doesn't know about.  We then exit
  185.    quietly.  This functions requires the PROC command be available in memory
  186.    or the current execution directory. */
  187.  
  188. int isuuxqtrunning()
  189. {
  190.      char line[80];
  191.      register char *p;
  192.      char *proc = PROC;
  193.      FILE *fp;
  194.      flag foundanother = FALSE;
  195.      int uupid, ourpid;
  196.  
  197.      p = line;
  198.  
  199.      /* look for another uuxqt running besides us */
  200.      ourpid = getpid();
  201.  
  202.      if ((fp = popen (proc, "r")) == NULL)
  203.        {
  204.           sprintf (line, "isuuxqtrunning() can't load '%s'", proc);
  205.  
  206.           if (!quiet  &&  logopen)
  207.                fprintf (stderr, "uuxqt: %s...error %d\n", line, errno);
  208.  
  209.           fatal (line);
  210.        }
  211.  
  212.      while (mfgets (p, sizeof (line), fp) != NULL)
  213. #ifndef _OSK
  214.           if (patmatch ("*uuxqt*", line, TRUE) == TRUE)
  215. #else
  216.           if (findstr (1, p, "uuxqt") != 0)
  217. #endif
  218.                if (atoi (p) != ourpid)
  219.                  {
  220.                     uupid = atoi (p);
  221.                     foundanother = TRUE;
  222.                  }
  223.      pclose (fp);
  224.  
  225.      /* if we found another uuxqt tell it about new work and die */
  226.      if (foundanother)
  227.        {
  228.           asetuid (0);
  229.           kill (uupid, MOREWORK);
  230.           exit (0);
  231.        }
  232. }
  233.  
  234.  
  235.  
  236. int processmail()
  237. {
  238.      DIR *dir;                                     /* defined is dir_6809.h */
  239.      struct direct *dirptr;                        /* defined in dir_6809.h */
  240.      register char **sysptr;
  241.      char *badsys = " (bad system name)";
  242.      char *cantchg = "can't change to spool directory for: ";
  243.  
  244.      if (morework)
  245.        {
  246.           morework = FALSE;
  247.           allflag = TRUE;
  248.  
  249.           if (!quiet)
  250.                puts ("\nnew work arrived...");
  251.        }
  252.  
  253.      /* change to the main spool directory */
  254.      if (chdir (spooldir) == ERROR)
  255.        {
  256.           char tmp[128];
  257.  
  258.           sprintf (tmp, "%s%s", cantchg, spooldir);
  259.           fatal (tmp);
  260.        }
  261.  
  262.      /* if we were passed a specific systems, do work for each */
  263.      if (!allflag)
  264.           for (sysptr = syslist; *sysptr; sysptr++)
  265.             {
  266.                /* change to the system's spool directory */
  267.                strcpy (sysname, *sysptr);
  268.                strlwr (sysname);
  269.  
  270.                if (!quiet)
  271.                  {
  272.                     printf ("uuxqt: doing work for '%s'...", sysname);
  273.                     fflush (stdout);
  274.                  }
  275.  
  276.                if (chdir (sysname) == ERROR)
  277.                  {
  278.                     char tmp[128];
  279.  
  280.                     if (!quiet)
  281.                          puts (badsys);
  282.  
  283.                     sprintf (tmp, "%s%s", cantchg, spooldir);
  284.                     error (tmp);
  285.                     continue;
  286.                  }
  287.  
  288.                findxwork();
  289.                chdir ("..");
  290.  
  291.                if (!quiet)
  292.                     putchar ('\n');
  293.             }
  294.      else
  295.        {
  296.           /* system name 'ALL' was passed, process all remotes */
  297.  
  298.           /* open top spool directory */
  299.           if ((dir = opendir (".")) == NULL)
  300.             {
  301.                char tmp[128];
  302.  
  303.                sprintf (tmp, "can't open: %s", spooldir);
  304.                fatal (tmp);
  305.             }
  306.  
  307.           /* loop through each directory in ./SPOOL/UUCP */
  308.           while ((dirptr = readdir (dir)) != NULL)
  309.             {
  310.                if (dirptr->d_name[0] == '.')
  311.                     continue;
  312.  
  313.                strcpy (sysname, dirptr->d_name);
  314.                strlwr (sysname);
  315.  
  316.                if (!quiet)
  317.                  {
  318.                     printf ("uuqxt: doing work for '%s'...", sysname);
  319.                     fflush (stdout);
  320.                  }
  321.  
  322.                /* change system's spool directory and look for work to do */
  323.                if (chdir (sysname) == ERROR)
  324.                  {
  325.                     char tmp[128];
  326.  
  327.                     if (!quiet)
  328.                          puts (badsys);
  329.  
  330.                     sprintf (tmp, "%s%s", cantchg, sysname);
  331.                     error (tmp);
  332.                     continue;
  333.                  }
  334.  
  335.                /* handle any waiting work */
  336.                findxwork();
  337.                chdir ("..");
  338.  
  339.                if (!quiet)
  340.                     putchar ('\n');
  341.             }
  342.           closedir (dir);
  343.        }
  344. }
  345.  
  346.  
  347.  
  348. void openlog()
  349. {
  350.      char tmp[128];
  351.  
  352.      if (logopen)
  353.           return;
  354.  
  355.      strcat (strcpy (tmp, logdir), "/uulog");
  356.  
  357.      /* Open log file, uulog.  It should be unbuffered. */
  358.      if ((log = fopen (tmp, "a")) != NULL)
  359.        {
  360.           setbuf (log, NULL);
  361.           logopen = TRUE;
  362.        }
  363.      else
  364.           log = stderr;
  365. }
  366.  
  367.  
  368.  
  369. /* To keep uulog from getting fatally fragmented, recopy it. */
  370.  
  371. void closelog()
  372. {
  373.      char out[128], in[128];
  374.  
  375.      if (log == stderr)
  376.           return;
  377.  
  378.      if (logopen)
  379.           fclose (log);
  380.  
  381.      logopen = FALSE;
  382.  
  383.      if (didwork)
  384.        {
  385.           /* try to rename the original log file */
  386.           sprintf (out, "%s/uulog.orig", logdir);
  387.           unlink (out);
  388.           sprintf (out, "rename %s/uulog uulog.orig", logdir);
  389.  
  390.          /* if we can't, forget about it this time */
  391.          if (docmd_na (out) == 0)
  392.            {
  393.               sprintf (in, "%s/uulog.orig", logdir);
  394.               sprintf (out, "%s/uulog", logdir);
  395.               filemove (in, out);
  396.            }
  397.        }
  398. }
  399.  
  400.  
  401.  
  402. int interrupt (sig)
  403. int sig;
  404. {
  405.      switch (sig)
  406.        {
  407.           case MOREWORK:
  408.                morework = TRUE;
  409.                break;
  410.  
  411.           case SIGWAKE:
  412.                break;
  413.  
  414.           case SIGQUIT:
  415.           case SIGINT:
  416.           case SIGKILL:
  417.                if (childid != -1)           /* kill off any children */
  418.                  {
  419.                     kill (childid, SIGKILL);
  420.                     childid = -1;
  421.                  }
  422.  
  423.                munload (rmail, 0);
  424.                munload (rnews, 0);
  425.                closelog();
  426.                exit (sig);
  427.        }
  428. }
  429.  
  430.  
  431.  
  432. int error (msg)
  433. char *msg;
  434. {
  435.      fprintf (log, "%s %s %s %s%s",
  436.                    sender, sysname, gtime(), errormsg, msg);
  437.  
  438.      if (errno != 0)
  439.           fprintf ("...error #%d", errno);
  440.  
  441.      putc ('\n', log);
  442. }
  443.  
  444.  
  445.  
  446. int fatal (msg)
  447. char *msg;
  448. {
  449.      interrupt (errno);
  450.      error (msg);
  451.      exit (0);
  452. }
  453.  
  454.  
  455.  
  456. int usage()
  457. {
  458.      char temp[80];
  459.      register char **use;
  460.      static char *usetxt[] = {
  461.        " ",
  462.        "uuxqt: process incoming queued UUCP traffic",
  463.        " ",
  464.        "Usage:  uuxqt [opts]  <sys> [<sys>...]  [opts]",
  465.        " ",
  466.        "\t<sys>   -process work for <sys>.  If <sys> name is 'ALL', all systems in",
  467.        "\t\t\t the Systems file are processed",
  468.        " ",
  469.        "opts:  -xN   -set debug level to N.  0 = OFF (default), 9 = highest",
  470.        "\t  -q    -work quietly (no messages to the screen)",
  471.        " ",
  472.        NULL
  473.      };
  474.  
  475.      for (use = usetxt; *use != NULL; ++use)
  476.           fprintf (stderr, " %s\n", strdetab (strcpy (temp, *use), 5));
  477.  
  478.      fprintf (stderr, "\nv%s (%s) This is free software released under the GNU General Public\n",
  479.                       version, VERDATE);
  480.      fputs ("License.  Please send suggestions/bug reports to:  bob@kc2wz.bubble.org\n", stderr);
  481.      exit (0);
  482. }
  483.