home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume23 / mlpd / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  3.6 KB  |  128 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Riverside. 
  11.  *
  12.  * NOTE : That's Riverside.  Not Berkeley, not Santa Cruz, not even
  13.  *        Irvine.  Riverside.  Ri - ver - side.
  14.  *
  15.  * The name of the University may not be used to endorse or promote 
  16.  * products derived from this software without specific prior written
  17.  * permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  * 
  23.  * MLPD -- Multiple Line Printer Daemon (Version 1.3)
  24.  * SCCS keywords: @(#)lock.c    1.3 12/1/90
  25.  */
  26.  
  27. #include "config.h"
  28.  
  29. /*
  30. // lock_printer() -- Lock up the printer daemon for once per machine.
  31. // 
  32. // The lock_printer() process will try to save the pid of the process
  33. // that is about to run to a file, to reference later when we need to
  34. // check to see if the program is running (We do not want to let the
  35. // program run more than once per machine.)
  36. //
  37. // Arguments : None
  38. */
  39. int lock_printer(base)
  40. char *base;
  41. {
  42.     FILE *fp, *fopen();
  43.     void exit();
  44.     char buf[1024];
  45.     int pid;
  46.  
  47.     /*
  48.     // First we will try to fopen() the file.  If we can't get
  49.     // the file open, we have to assume that either the file has
  50.     // been moved, or the program has been recompiled and there
  51.     // is a new lock file directory.  If we can fopen() it,
  52.     // we will run through this [if..then] loop and handle the
  53.     // conditions.
  54.     */
  55.     buf[0] = '\0';
  56.     (void)sprintf(buf, "/etc/mlpd.%s.pid", base);
  57.     if ((fp = fopen(buf, "r")) != NULL)
  58.     {
  59.         /*
  60.         // Try to read the number from the file.
  61.         */
  62.         if (fscanf(fp, "%d", &pid) < 0)
  63.         {
  64.             perror("lock_printer:fscanf:fopen");
  65.             exit(-1);
  66.         }
  67.         /*
  68.         // If we can read the number, then we need to see whether
  69.         // the process still exists.  If it does, then we want to
  70.         // Just exit out, and tell the user that the program is 
  71.         // still running; otherwise, we need to clean up the file
  72.         // that exists in the directory, and create the file showing
  73.         // the new PID in the file.
  74.         */
  75.         if (getpgrp(pid) >= 0)
  76.         {
  77.             if (fprintf(stderr, "This program is already running \
  78.                     (PID# : %d)\n", pid) < 0)
  79.             {
  80.                 bomb("lock_printer");
  81.             }
  82.             exit(1);
  83.         }
  84.     }
  85.     /*
  86.     // If we got to this point in the code, then either there wasn't a
  87.     // lock file in the directory, or the process which was running
  88.     // died off.  In either case, remove the file that we found, if
  89.     // there.
  90.     */
  91.     if (unlink(buf) < 0)
  92.     {
  93.         /* 
  94.         // Do nothing.  We want to try to remove the 
  95.         // file no matter what.
  96.         */
  97.     }
  98.     /*
  99.     // Now we want to try and fopen the file for writing.  This is
  100.     // to place the present getpid() into the file for reference
  101.     // later.
  102.     */
  103.     if ((fp = fopen(buf, "w")) == NULL)
  104.     {
  105.         bomb("lock_printer:/etc/mlpd.pid:fopen");
  106.     }
  107.     /*
  108.     // Try to fprintf() the getpid() return value into the file.
  109.     */
  110.     if (fprintf(fp, "%d", getpid()) < 0)
  111.     {
  112.         bomb("lock_printer:fprintf");
  113.     }
  114.     /*
  115.     // If we get here, all we have to do is close the file, and
  116.     // go back to the init_daemon() process.
  117.     */
  118.     if (fclose(fp) < 0)
  119.     {
  120.         bomb("lock_printer:fclose");
  121.     }
  122.     /*
  123.     // Well, everything must have worked, so we'll just head back and
  124.     // tell the calling program that everything is okay.
  125.     */
  126.     return 1;
  127. }
  128.