/*- * Copyright (c) 1996 * David Parsons. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by David Parsons * (orc@pell.chi.il.us) * 4. My name may not be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ static char rcsid[] = "$Header: /home/orc/src/CVS/automod/lock.c,v 1.4 1996/10/27 18:27:59 orc Exp $"; /* * lock: do lock waiting to ensure single-threading. */ #include "automod.h" #include #include #include #include #include #include #include /* enough room to fit a reasonable lockfile name */ #define TEMPLATE "LCK..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" /* enough room to fit the canonical resonable lockfile name and * the directory it lives in. */ static char lockfile[] = LOCK_PREFIX TEMPLATE; /* * unlock an existing lock; called from atexit (if it exists * on the system) or manually when we exit. */ void thread_unlock() { unlink(lockfile); } /* thread_unlock */ /* * lock ourselves down and set an atexit unlocker to repair * things when we exit. We could use a semaphore, but this * is not guaranteed to exist on joe random hardware (alas) */ int thread_lock(char *lockid) { int fd; struct stat info; #define PID_LEN 11 /* 10 digits plus newline plus null */ char pid_string[PID_LEN+1]; pid_t pid; int rc; int backoff = 0; time_t now; thread_unlock(); sprintf(lockfile, "%s/LCK..%s", LOCK_PREFIX, lockid); while (1) { fd = open(lockfile, O_CREAT|O_EXCL, 0660); if (fd >= 0) { sprintf(pid_string, "%10d\n", getpid()); rc = write(fd, pid_string, PID_LEN); close(fd); #if ATEXIT_DEFINED atexit(thread_unlock); #endif return (rc == PID_LEN) ? 1 : 2; } if (errno != EEXIST) syslog(LOG_NOTICE, "%s: %m", lockfile); /* file exists; see if the owner is still there. */ sleep(1); /* give time for the owner to write their pid */ fd = open(lockfile, O_RDWR, 0660); if (fd < 0) { /* lockfile is gone! */ if (errno != ENOENT) syslog(LOG_NOTICE, "%s: %m", lockfile); continue; } memset(pid_string, 0, sizeof pid_string); rc = read(fd, pid_string, PID_LEN); if (rc == PID_LEN && sscanf(pid_string, "%d", &pid) == 1) { /* read the pid out; validate it. */ if ((rc=kill(pid, 0)) == 0) { syslog(LOG_NOTICE, "%s: stale lock", lockfile); writelock: sprintf(pid_string, "%10d\n", getpid()); /* process is GONE; dump a PID into the file */ lseek(fd, 0, 0); rc = write(fd, pid_string, PID_LEN); close(fd); return (rc == PID_LEN) ? 1 : 2; } /* check periodically, } else { /* It's a badly formatted lock; check its age and if it's * more than 5 minutes old, dump it and take over. /* else we didn't read enough; sleep briefly, then try again */ fstat(fd, &info); time(&now); if ((now-info.st_mtime) > 300) { syslog(LOG_NOTICE, "%s: old corrupt lock", lockfile); goto writelock; } } close(fd); backoff += backoff+1; if (backoff > 600) backoff = 600; sleep(backoff); } /* our little infinite loop */ } /* thread_lock */