home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1990 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Riverside.
- *
- * NOTE : That's Riverside. Not Berkeley, not Santa Cruz, not even
- * Irvine. Riverside. Ri - ver - side.
- *
- * The name of the University may not be used to endorse or promote
- * products derived from this software without specific prior written
- * permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- *
- * MLPD -- Multiple Line Printer Daemon (Version 1.3)
- * SCCS keywords: @(#)lock.c 1.3 12/1/90
- */
-
- #include "config.h"
-
- /*
- // lock_printer() -- Lock up the printer daemon for once per machine.
- //
- // The lock_printer() process will try to save the pid of the process
- // that is about to run to a file, to reference later when we need to
- // check to see if the program is running (We do not want to let the
- // program run more than once per machine.)
- //
- // Arguments : None
- */
- int lock_printer(base)
- char *base;
- {
- FILE *fp, *fopen();
- void exit();
- char buf[1024];
- int pid;
-
- /*
- // First we will try to fopen() the file. If we can't get
- // the file open, we have to assume that either the file has
- // been moved, or the program has been recompiled and there
- // is a new lock file directory. If we can fopen() it,
- // we will run through this [if..then] loop and handle the
- // conditions.
- */
- buf[0] = '\0';
- (void)sprintf(buf, "/etc/mlpd.%s.pid", base);
- if ((fp = fopen(buf, "r")) != NULL)
- {
- /*
- // Try to read the number from the file.
- */
- if (fscanf(fp, "%d", &pid) < 0)
- {
- perror("lock_printer:fscanf:fopen");
- exit(-1);
- }
- /*
- // If we can read the number, then we need to see whether
- // the process still exists. If it does, then we want to
- // Just exit out, and tell the user that the program is
- // still running; otherwise, we need to clean up the file
- // that exists in the directory, and create the file showing
- // the new PID in the file.
- */
- if (getpgrp(pid) >= 0)
- {
- if (fprintf(stderr, "This program is already running \
- (PID# : %d)\n", pid) < 0)
- {
- bomb("lock_printer");
- }
- exit(1);
- }
- }
- /*
- // If we got to this point in the code, then either there wasn't a
- // lock file in the directory, or the process which was running
- // died off. In either case, remove the file that we found, if
- // there.
- */
- if (unlink(buf) < 0)
- {
- /*
- // Do nothing. We want to try to remove the
- // file no matter what.
- */
- }
- /*
- // Now we want to try and fopen the file for writing. This is
- // to place the present getpid() into the file for reference
- // later.
- */
- if ((fp = fopen(buf, "w")) == NULL)
- {
- bomb("lock_printer:/etc/mlpd.pid:fopen");
- }
- /*
- // Try to fprintf() the getpid() return value into the file.
- */
- if (fprintf(fp, "%d", getpid()) < 0)
- {
- bomb("lock_printer:fprintf");
- }
- /*
- // If we get here, all we have to do is close the file, and
- // go back to the init_daemon() process.
- */
- if (fclose(fp) < 0)
- {
- bomb("lock_printer:fclose");
- }
- /*
- // Well, everything must have worked, so we'll just head back and
- // tell the calling program that everything is okay.
- */
- return 1;
- }
-