home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume1 / 8711 / 1 next >
Text File  |  1990-07-13  |  4KB  |  170 lines

  1. Path: uunet!seismo!sundc!pitstop!sun!amdcad!ames!necntc!ncoast!allbery
  2. From: lyndon@ncc.UUCP (Lyndon Nerenberg)
  3. Newsgroups: comp.sources.misc
  4. Subject: UUCP lock file cleaner-upper
  5. Message-ID: <4975@ncoast.UUCP>
  6. Date: 2 Nov 87 02:39:02 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Organization: Nexus Computing Inc.
  9. Lines: 157
  10. Approved: allbery@ncoast.UUCP
  11. X-Archive: comp.sources.misc/8711/1
  12.  
  13. When uucico core dumps (an all to common occurence on our
  14. system) it leaves an LCK.. file or two lying around. If
  15. this happens at night and you only have one dialout line,
  16. not much traffic moves until someone manually fixes things
  17. up. Uulck is a little program I wrote to zap these stray
  18. lock files. Run it once per hour from cron (I run it just
  19. before our polling rounds at 10 to and 20 after the hour).
  20. It's written for System V, but a BSD port is trivial (almost).
  21.  
  22.  
  23. /*
  24.  * @(#)uulck.c 1.1    (ncc!lyndon)
  25.  *
  26.  * Scan the UUCP spool directory for stray lock files
  27.  * that don't belong to a currently running process
  28.  * and remove them.
  29.  *
  30.  * Invoke it from cron, or run it out of your polling
  31.  * scripts. There are no command line parameters.
  32.  *
  33.  * This program is in the public domain. You may copy it, sell
  34.  * it, or do whatever else feels good with it. You also accept
  35.  * any and all consequences of running this software.
  36.  *
  37.  * If you make any enhancements, I would appreciate receiving
  38.  * a copy of them. Enjoy!
  39.  *
  40.  * Lyndon Nerenberg
  41.  * Nexus Computing Inc.
  42.  * {alberta,pyramid,uwvax}!ncc!lyndon
  43.  *
  44.  */
  45.  
  46. #include <stdio.h>
  47. #include <fcntl.h>
  48. #include <string.h>
  49. #include <signal.h>
  50. #include <errno.h>
  51. #include <sys/dir.h>
  52.  
  53. #define LOCKDIR    "/usr/spool/uucp/"    /* Need trailing '/'    */
  54. #define LCKPFX        "LCK."
  55. #define EVER        ;;
  56.  
  57. main() {
  58.  
  59. int        spoolfd, lockfd;    /* One each for spool directory */
  60.                     /* and current lock file.       */
  61. int        lockpid;        /* Holds pid from LCK file.     */
  62. int        rc;            /* General purpose return code. */
  63. register int    i;
  64.  
  65. struct direct    direntry;        /* Storage for directory entry. */
  66.  
  67. char        lockpath[ sizeof(LOCKDIR) + DIRSIZ + 1];
  68.  
  69.  
  70.     spoolfd = open(LOCKDIR, O_RDONLY);
  71.  
  72.     if (spoolfd == -1) {
  73.         if (isatty(2)) {
  74.             fprintf(stderr, "[%s] ", LOCKDIR);
  75.             perror("LOCKDIR open failed");
  76.         }
  77.         exit(1);
  78.     }
  79.  
  80.     for (EVER) {
  81.  
  82.         rc = read(spoolfd, &direntry, sizeof(direntry));
  83.  
  84.         if (rc == 0)
  85.             break;        /* Finished processing directory*/
  86.  
  87.         if (rc == -1) {
  88.             if (isatty(2)) {
  89.                 fprintf(stderr, "[%s] ", LOCKDIR);
  90.                 perror("LOCKDIR read error");
  91.             }
  92.             exit(1);
  93.         }
  94.  
  95.         if (direntry.d_ino == 0)
  96.             continue;    /* Deleted entry */
  97.  
  98.         (void) strcpy(lockpath,LOCKDIR);
  99.  
  100.     /* Build the path string, and make sure it's terminated */
  101.  
  102.         for (i = 0; i < DIRSIZ; i++)
  103.             lockpath[sizeof(LOCKDIR)+i-1] = direntry.d_name[i];
  104.  
  105.         lockpath[sizeof(LOCKDIR) + DIRSIZ + 1] = '\0';
  106.  
  107.         if (strncmp(LCKPFX, direntry.d_name, strlen(LCKPFX)) != 0)
  108.             continue;
  109.  
  110.         lockfd = open(lockpath, O_RDONLY);
  111.  
  112.         if (lockfd == -1) {
  113.             if (isatty(2)) {
  114.                 fprintf(stderr, "[%s] ", lockpath);
  115.                 perror("open failed");
  116.             }
  117.             continue;
  118.         }
  119.  
  120.         rc = read(lockfd, &lockpid, 4);    /* Grab the process ID */
  121.  
  122.         if (rc == -1) {
  123.             if (isatty(2)) {
  124.                 fprintf(stderr, "[%s] ", lockpath);
  125.                 perror("read failed");
  126.             }
  127.             (void) close(lockfd);
  128.             continue;
  129.         }
  130.  
  131.         if (rc != 4) {
  132.             if (isatty(2))
  133.                 fprintf(stderr,
  134.                     "read returned %d bytes?!?\n",
  135.                     rc);
  136.             (void) close(lockfd);
  137.             continue;
  138.         }
  139.  
  140.         (void) close(lockfd);    /* Done with the file */
  141.  
  142.         /* This is probably the ONLY thing I like about Sys V */
  143.  
  144.         rc = kill(lockpid, 0);
  145.  
  146.         if (rc == 0)
  147.             continue;    /* Task exists */
  148.  
  149.         if ((rc == -1) && (errno == ESRCH)) {
  150.             rc = unlink(lockpath);    /* Something died! */
  151.             if ((rc == -1) && isatty(2)) {
  152.                 fprintf(stderr, "[%s] ", lockpath);
  153.                 perror("unlink failed");
  154.             }
  155.             continue;
  156.         }
  157.  
  158.         /* We only get here if kill() failed in a strange way */
  159.  
  160.         if (isatty(2)) {
  161.             fprintf(stderr, "lockpid = %d ", lockpid);
  162.             perror("kill failed");
  163.         }
  164.  
  165.         continue;
  166.     }
  167.  
  168.     exit(0);
  169. }
  170.