Home | History | Annotate | Line # | Download | only in libutil
pidlock.c revision 1.1
      1  1.1  cjs /*	$NetBSD: pidlock.c,v 1.1 1997/10/11 02:56:23 cjs Exp $ */
      2  1.1  cjs 
      3  1.1  cjs /*
      4  1.1  cjs  * Copyright 1996, 1997 by Curt Sampson <cjs (at) netbsd.org>.
      5  1.1  cjs  *
      6  1.1  cjs  * Redistribution and use in source and binary forms, with or without
      7  1.1  cjs  * modification, are permitted provided that the following conditions
      8  1.1  cjs  * are met:
      9  1.1  cjs  * 1. Redistributions of source code must retain the above copyright
     10  1.1  cjs  *    notice, this list of conditions and the following disclaimer.
     11  1.1  cjs  *
     12  1.1  cjs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     13  1.1  cjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     14  1.1  cjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     15  1.1  cjs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     16  1.1  cjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17  1.1  cjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     18  1.1  cjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     19  1.1  cjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     20  1.1  cjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     21  1.1  cjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     22  1.1  cjs  * SUCH DAMAGE.
     23  1.1  cjs  */
     24  1.1  cjs 
     25  1.1  cjs #include <sys/cdefs.h>
     26  1.1  cjs #if defined(LIBC_SCCS) && !defined(lint)
     27  1.1  cjs __RCSID("$NetBSD: pidlock.c,v 1.1 1997/10/11 02:56:23 cjs Exp $");
     28  1.1  cjs #endif /* LIBC_SCCS and not lint */
     29  1.1  cjs 
     30  1.1  cjs #include <sys/errno.h>
     31  1.1  cjs #include <sys/param.h>
     32  1.1  cjs #include <sys/stat.h>
     33  1.1  cjs #include <sys/types.h>
     34  1.1  cjs 
     35  1.1  cjs #include <fcntl.h>
     36  1.1  cjs #include <signal.h>
     37  1.1  cjs #include <stdio.h>
     38  1.1  cjs #include <stdlib.h>
     39  1.1  cjs #include <string.h>
     40  1.1  cjs #include <unistd.h>
     41  1.1  cjs #include <util.h>
     42  1.1  cjs 
     43  1.1  cjs /*
     44  1.1  cjs  * Create a lockfile. Return 0 when locked, -1 on error.
     45  1.1  cjs  */
     46  1.1  cjs int
     47  1.1  cjs pidlock(lockfile, flags, locker, info)
     48  1.1  cjs 	const char *lockfile;
     49  1.1  cjs 	int flags;
     50  1.1  cjs 	pid_t *locker;
     51  1.1  cjs 	const char *info;
     52  1.1  cjs {
     53  1.1  cjs 	char	tempfile[MAXPATHLEN];
     54  1.1  cjs 	char	hostname[256];
     55  1.1  cjs 	pid_t	pid2 = -1;
     56  1.1  cjs 	int	f;
     57  1.1  cjs 	char	s[256];
     58  1.1  cjs 	char	*p;
     59  1.1  cjs 
     60  1.1  cjs 	/* Build a path to the temporary file. */
     61  1.1  cjs 	snprintf(s, sizeof(s), "%d", getpid());
     62  1.1  cjs 	/* XXX This breaks if PIDs can be longer than ten decimal digits. */
     63  1.1  cjs 	/* XXX But then again, so does the lockfile format. :-) */
     64  1.1  cjs 	strncpy(tempfile, lockfile, sizeof(tempfile)-26);
     65  1.1  cjs 	if (strlen(tempfile) < strlen(lockfile))  {
     66  1.1  cjs 		errno = ENAMETOOLONG;
     67  1.1  cjs 		return -1;
     68  1.1  cjs 	}
     69  1.1  cjs 	strcat(tempfile, ".tmp.");
     70  1.1  cjs 	strcat(tempfile, s);
     71  1.1  cjs 
     72  1.1  cjs 	/* Open it, write pid, hostname, info. */
     73  1.1  cjs 	if ( (f = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1 )  {
     74  1.1  cjs 		unlink(tempfile);
     75  1.1  cjs 		return -1;
     76  1.1  cjs 	}
     77  1.1  cjs 	snprintf(s, sizeof(s), "%10d\n", getpid());	/* pid */
     78  1.1  cjs 	if (write(f, s, 11) != 11)  {
     79  1.1  cjs 		close(f); unlink(tempfile); return -1;
     80  1.1  cjs 	}
     81  1.1  cjs 	if (gethostname(hostname, sizeof(hostname)))	/* hostname */
     82  1.1  cjs 		return -1;
     83  1.1  cjs 	if ((flags & PIDLOCK_USEHOSTNAME))  {
     84  1.1  cjs 		if ((write(f, hostname, strlen(hostname)) != strlen(hostname))
     85  1.1  cjs 		    || (write(f, "\n", 1) != 1))  {
     86  1.1  cjs 			close(f); unlink(tempfile); return -1;
     87  1.1  cjs 		}
     88  1.1  cjs 	}
     89  1.1  cjs 	if (info)  {					/* info */
     90  1.1  cjs 		if (!(flags & PIDLOCK_USEHOSTNAME))  {
     91  1.1  cjs 			/* write blank line because there's no hostname */
     92  1.1  cjs 			if (write(f, "\n", 1) != 1)  {
     93  1.1  cjs 				close(f); unlink(tempfile); return -1;
     94  1.1  cjs 			}
     95  1.1  cjs 		}
     96  1.1  cjs 		if (write(f, info, strlen(info)) != strlen(info) ||
     97  1.1  cjs 		    (write(f, "\n", 1) != 1))  {
     98  1.1  cjs 			close(f); unlink(tempfile); return -1;
     99  1.1  cjs 		}
    100  1.1  cjs 	}
    101  1.1  cjs 	close(f);
    102  1.1  cjs 
    103  1.1  cjs 	/* Hard link the temporary file to the real lock file. */
    104  1.1  cjs 	/* This is an atomic operation. */
    105  1.1  cjs 	while (link(tempfile, lockfile) != 0)  {
    106  1.1  cjs 		if (errno != EEXIST)  {
    107  1.1  cjs 			unlink(tempfile);
    108  1.1  cjs 			return -1;
    109  1.1  cjs 		}
    110  1.1  cjs 		/* Find out who has this lockfile. */
    111  1.1  cjs 		if (!(f = open(lockfile, O_RDONLY, 0)))
    112  1.1  cjs 			goto retry;
    113  1.1  cjs 		read(f, s, 11);
    114  1.1  cjs 		pid2 = atoi(s);
    115  1.1  cjs 		read(f, s, sizeof(s)-2);
    116  1.1  cjs 		s[sizeof(s)-1] = '\0';
    117  1.1  cjs 		if ((p=strchr(s, '\n')))
    118  1.1  cjs 			*p = '\0';
    119  1.1  cjs 		close(f);
    120  1.1  cjs 
    121  1.1  cjs 		if (!((flags & PIDLOCK_USEHOSTNAME) && strcmp(s, hostname)))  {
    122  1.1  cjs 			if ((kill(pid2, 0) != 0) && (errno == ESRCH))  {
    123  1.1  cjs 				/* process doesn't exist */
    124  1.1  cjs 				unlink(lockfile);
    125  1.1  cjs 				continue;
    126  1.1  cjs 			}
    127  1.1  cjs 		}
    128  1.1  cjs retry:
    129  1.1  cjs 		if (flags & PIDLOCK_NONBLOCK)  {
    130  1.1  cjs 			if (locker)
    131  1.1  cjs 				*locker = pid2;
    132  1.1  cjs 			unlink(tempfile);
    133  1.1  cjs 			errno = EWOULDBLOCK;
    134  1.1  cjs 			return -1;
    135  1.1  cjs 		} else
    136  1.1  cjs 			sleep(5);
    137  1.1  cjs 	}
    138  1.1  cjs 
    139  1.1  cjs 	/* return this process's PID on lock */
    140  1.1  cjs 	if (locker)
    141  1.1  cjs 		*locker = getpid();
    142  1.1  cjs 	unlink(tempfile);
    143  1.1  cjs 
    144  1.1  cjs 	return 0;
    145  1.1  cjs }
    146  1.1  cjs 
    147  1.1  cjs #define LOCKPATH	"/var/spool/lock/LCK.."
    148  1.1  cjs #define	DEVPATH		"/dev/"
    149  1.1  cjs 
    150  1.1  cjs int
    151  1.1  cjs ttylock(tty, flags, locker)
    152  1.1  cjs 	const char *tty;
    153  1.1  cjs 	int flags;
    154  1.1  cjs 	pid_t *locker;
    155  1.1  cjs {
    156  1.1  cjs 	char	lockfile[MAXPATHLEN];
    157  1.1  cjs 	char	ttyfile[MAXPATHLEN];
    158  1.1  cjs 	struct stat sb;
    159  1.1  cjs 
    160  1.1  cjs 	/* make sure the tty exists */
    161  1.1  cjs 	strcpy(ttyfile, DEVPATH);
    162  1.1  cjs 	strncat(ttyfile, tty, MAXPATHLEN-strlen(DEVPATH));
    163  1.1  cjs 	if (stat(ttyfile, &sb))  {
    164  1.1  cjs 		errno = ENOENT; return -1;
    165  1.1  cjs 	}
    166  1.1  cjs 	if (!(sb.st_mode & S_IFCHR))  {
    167  1.1  cjs 		errno = ENOENT; return -1;
    168  1.1  cjs 	}
    169  1.1  cjs 
    170  1.1  cjs 	/* do the lock */
    171  1.1  cjs 	strcpy(lockfile, LOCKPATH);
    172  1.1  cjs 	strncat(lockfile, tty, MAXPATHLEN-strlen(LOCKPATH));
    173  1.1  cjs 	return pidlock(lockfile, 0, 0, 0);
    174  1.1  cjs }
    175  1.1  cjs 
    176  1.1  cjs int
    177  1.1  cjs ttyunlock(tty)
    178  1.1  cjs 	const char *tty;
    179  1.1  cjs {
    180  1.1  cjs 	char	lockfile[MAXPATHLEN];
    181  1.1  cjs 	char	ttyfile[MAXPATHLEN];
    182  1.1  cjs 	struct stat sb;
    183  1.1  cjs 
    184  1.1  cjs 	/* make sure the tty exists */
    185  1.1  cjs 	strcpy(ttyfile, DEVPATH);
    186  1.1  cjs 	strncat(ttyfile, tty, MAXPATHLEN-strlen(DEVPATH));
    187  1.1  cjs 	if (stat(ttyfile, &sb))  {
    188  1.1  cjs 		errno = ENOENT; return -1;
    189  1.1  cjs 	}
    190  1.1  cjs 	if (!(sb.st_mode & S_IFCHR))  {
    191  1.1  cjs 		errno = ENOENT; return -1;
    192  1.1  cjs 	}
    193  1.1  cjs 
    194  1.1  cjs 	/* undo the lock */
    195  1.1  cjs 	strcpy(lockfile, LOCKPATH);
    196  1.1  cjs 	strncat(lockfile, tty, MAXPATHLEN-strlen(LOCKPATH));
    197  1.1  cjs 	return unlink(lockfile);
    198  1.1  cjs }
    199