home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / src / niftp / ni_mail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-28  |  4.5 KB  |  166 lines

  1. #include "util.h"
  2. #include "mmdf.h"
  3.  
  4. /*  Main routine called by Q NIFTP process to invoke            */
  5. /*  Q NIFTP channel of MMDF                                     */
  6. /*                                                              */
  7. /*  Note that the files are prefixed nn_                        */
  8. /*  wheras the routines are prefixed ni_                        */
  9. /*  This is to distinguish between linked and unlinked files    */
  10. /*                                                              */
  11. /*      Steve Kille     August 1982                             */
  12.  
  13. #define RETRY_TIME      5       /* Delay between retries in secs*/
  14.  
  15.  
  16. extern char *cmddfldir;
  17.                 /* Default directory for NIFTP  */
  18.                 /* to MMDF routine              */
  19.  
  20. char nichan[] = "ni_niftp";
  21.  
  22.  
  23. extern int   ipcs_print;
  24.  
  25. extern char *dupfpath ();
  26.  
  27.  
  28. LOCVAR char tmpstring[LINESIZE];
  29.                 /* static buffer for return message */
  30.  
  31. /* */
  32.  
  33. ni_mail (fname, queue, TSname, max_tries, rpstring)
  34. char  fname[];                  /* File to be transferred       */
  35. char  queue[];                  /* name of NIFTP queue          */
  36. char  TSname[];                 /* TS name of calling host      */
  37. int   max_tries;                /* Max number of retries        */
  38.                 /* If 0 mailer will detach and  */
  39.                 /* Not return                   */
  40. char  **rpstring;               /* Where to stuff result string */
  41.                 /* This might be passed to the  */
  42.                 /* remote NIFTP                 */
  43. {
  44.     int   pid;                  /* for fork                     */
  45.     int   status;
  46.     char  chpath[LINESIZE];     /* Full path of NIFTP channel   */
  47.     int   no_tries;             /* Number of tries to deliver   */
  48.     int   fd;                   /* temp file pointer            */
  49.  
  50.     int   nfiles;
  51.  
  52.  
  53.  
  54.     if (ipcs_print > 0)
  55.     printf ("ni_mail (f='%s', q='%s', TS='%s', n='%d')\n", fname,
  56.         queue, TSname, max_tries);
  57.  
  58.  
  59.     *rpstring = tmpstring;
  60.         /* point to line buffer                         */
  61.  
  62.  
  63.     getfpath (nichan, cmddfldir, chpath);
  64.                     /* get path of channel process  */
  65.  
  66.  
  67.                 /* Now try to submit the mail to the chan */
  68.                 /* In the case of partial failure repeat  */
  69.                 /* as specified by max_tries              */
  70.     if (ipcs_print > 0)
  71.     printf ("execl (%s, %s, %s, %s, %s)", chpath, nichan, queue,
  72.         fname, TSname);
  73.  
  74.  
  75.     no_tries = 0;               /* First attempt                          */
  76.     FOREVER                     /* no test here as we must enter once     */
  77.     {
  78.     if ((pid = fork ()) == NOTOK)
  79.     {
  80.         status = RP_AGN;    /* Should be temporary                    */
  81.         continue;
  82.     }
  83.  
  84.     switch (pid)
  85.     {
  86.         case 0:            /* In the child                            */
  87.                 /* close off any open files  NIFTP leaves   */
  88.                 /* lying around                             */
  89. #ifdef _NFILE
  90.         nfiles = _NFILE;
  91. #else
  92.         nfiles = getdtablesize();
  93. #endif
  94.         for (fd = 0; fd < nfiles; fd++)
  95.             close (fd);
  96.                 /* open standard output                    */
  97.         open ("/dev/null", 1);
  98.  
  99.         execl (chpath, nichan, queue, fname, TSname, (char *)0);
  100.                 /* If we get here something hs screwed      */
  101.         exit (RP_MECH);
  102.         break;
  103.  
  104.        default:
  105.         if (max_tries < 1)      /* don't wait around              */
  106.         {
  107.             sprintf (tmpstring,
  108.                 "Not waiting for MMDF to transfer mail");
  109.             return (OK);
  110.         }
  111.  
  112.         status = pgmwait (pid); /* hang around for MMDF            */
  113.                     /* but not any of its children     */
  114.                     /* should protect this with alarms */
  115.                     /* But UCL being the place it is...*/
  116.  
  117.         if (status == 0)
  118.                     /* Fatal crash                  */
  119.         {
  120.             sprintf (tmpstring, "MMDF crashed - aarrg");
  121.             return (NOTOK);
  122.         }
  123.  
  124.         if (rp_isgood (status))
  125.                     /* Success                         */
  126.         {
  127.             sprintf (tmpstring, "Mail transferred correctly (%s)",
  128.                         rp_valstr (status));
  129.             return (OK);
  130.         }
  131.  
  132.         if ((rp_gbval (status)) ==  RP_BNO)
  133.                     /* Complete disaster so go home     */
  134.         {
  135.             switch (status)
  136.             {
  137.             case  RP_NDEL:
  138.                 sprintf (tmpstring, "No valid recipients" );
  139.                 return (OK);
  140.                 break;
  141.             default:
  142.                 sprintf (tmpstring,
  143.                     "Fatal error on mail transfer (%s)",
  144.                     rp_valstr (status));
  145.                 return (NOTOK);
  146.              }
  147.         }
  148.     }
  149.                     /* Do we need to make any more tries*/
  150.     if (no_tries++ >= max_tries)
  151.         break;
  152.                     /* Otherwise sleep a while and then */
  153.                     /* Try again                        */
  154.     sleep (RETRY_TIME);
  155.    }
  156.  
  157.                     /*  We have tried as many times as  */
  158.                     /*  allowed                         */
  159.  
  160.     sprintf (tmpstring, "Repeated temporary MMDF failure (%s) after %d attempts",
  161.                   rp_valstr (status), no_tries);
  162.     return (NOTOK);
  163. }
  164.  
  165.  
  166.