xref: /minix3/lib/libutil/pidlock.c (revision dba3562d7800d1fed3cb2cd859754872fbb2e84f)
1*dba3562dSLionel Sambuc /*	$NetBSD: pidlock.c,v 1.16 2012/04/07 16:17:17 christos Exp $ */
20c3983b2SBen Gras 
30c3983b2SBen Gras /*
40c3983b2SBen Gras  * Copyright 1996, 1997 by Curt Sampson <cjs@NetBSD.org>.
50c3983b2SBen Gras  *
60c3983b2SBen Gras  * Redistribution and use in source and binary forms, with or without
70c3983b2SBen Gras  * modification, are permitted provided that the following conditions
80c3983b2SBen Gras  * are met:
90c3983b2SBen Gras  * 1. Redistributions of source code must retain the above copyright
100c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer.
110c3983b2SBen Gras  *
120c3983b2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
130c3983b2SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
140c3983b2SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
150c3983b2SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
160c3983b2SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
170c3983b2SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
180c3983b2SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
190c3983b2SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
200c3983b2SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
210c3983b2SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
220c3983b2SBen Gras  * SUCH DAMAGE.
230c3983b2SBen Gras  */
240c3983b2SBen Gras 
250c3983b2SBen Gras #include <sys/cdefs.h>
260c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
27*dba3562dSLionel Sambuc __RCSID("$NetBSD: pidlock.c,v 1.16 2012/04/07 16:17:17 christos Exp $");
280c3983b2SBen Gras #endif /* LIBC_SCCS and not lint */
290c3983b2SBen Gras 
300c3983b2SBen Gras #include <sys/param.h>
310c3983b2SBen Gras #include <sys/stat.h>
320c3983b2SBen Gras #include <sys/types.h>
330c3983b2SBen Gras 
340c3983b2SBen Gras #include <assert.h>
350c3983b2SBen Gras #include <errno.h>
360c3983b2SBen Gras #include <fcntl.h>
370c3983b2SBen Gras #include <signal.h>
380c3983b2SBen Gras #include <stdio.h>
390c3983b2SBen Gras #include <stdlib.h>
400c3983b2SBen Gras #include <string.h>
410c3983b2SBen Gras #include <unistd.h>
420c3983b2SBen Gras #include <util.h>
430c3983b2SBen Gras #include <paths.h>
440c3983b2SBen Gras 
450c3983b2SBen Gras /*
460c3983b2SBen Gras  * Create a lockfile. Return 0 when locked, -1 on error.
470c3983b2SBen Gras  */
480c3983b2SBen Gras int
pidlock(const char * lockfile,int flags,pid_t * locker,const char * info)490c3983b2SBen Gras pidlock(const char *lockfile, int flags, pid_t *locker, const char *info)
500c3983b2SBen Gras {
510c3983b2SBen Gras 	char	tempfile[MAXPATHLEN];
520c3983b2SBen Gras 	char	hostname[MAXHOSTNAMELEN + 1];
530c3983b2SBen Gras 	pid_t	pid2 = -1;
540c3983b2SBen Gras 	struct	stat st;
55*dba3562dSLionel Sambuc 	ssize_t	n;
56*dba3562dSLionel Sambuc 	int	f = -1, savee;
570c3983b2SBen Gras 	char	s[256];
580c3983b2SBen Gras 	char	*p;
590c3983b2SBen Gras 	size_t	len;
600c3983b2SBen Gras 
610c3983b2SBen Gras 	_DIAGASSERT(lockfile != NULL);
620c3983b2SBen Gras 	/* locker may be NULL */
630c3983b2SBen Gras 	/* info may be NULL */
640c3983b2SBen Gras 
650c3983b2SBen Gras 
660c3983b2SBen Gras 	if (gethostname(hostname, sizeof(hostname)))
670c3983b2SBen Gras 		return -1;
680c3983b2SBen Gras 	hostname[sizeof(hostname) - 1] = '\0';
690c3983b2SBen Gras 
700c3983b2SBen Gras 	/*
710c3983b2SBen Gras 	 * Build a path to the temporary file.
720c3983b2SBen Gras 	 * We use the path with the PID and hostname appended.
730c3983b2SBen Gras 	 * XXX This is not thread safe.
740c3983b2SBen Gras 	 */
750c3983b2SBen Gras 	if (snprintf(tempfile, sizeof(tempfile), "%s.%d.%s", lockfile,
760c3983b2SBen Gras 	    (int) getpid(), hostname) >= (int)sizeof(tempfile))  {
770c3983b2SBen Gras 		errno = ENAMETOOLONG;
780c3983b2SBen Gras 		return -1;
790c3983b2SBen Gras 	}
800c3983b2SBen Gras 
810c3983b2SBen Gras 	/* Open it, write pid, hostname, info. */
820c3983b2SBen Gras 	if ((f = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1)
830c3983b2SBen Gras 		goto out;
840c3983b2SBen Gras 
850c3983b2SBen Gras 	(void)snprintf(s, sizeof(s), "%10d\n", getpid());	/* pid */
860c3983b2SBen Gras 	if (write(f, s, (size_t)11) != 11)
870c3983b2SBen Gras 		goto out;
880c3983b2SBen Gras 
890c3983b2SBen Gras 	if ((flags & PIDLOCK_USEHOSTNAME))  {		/* hostname */
900c3983b2SBen Gras 		len = strlen(hostname);
910c3983b2SBen Gras 		if ((size_t)write(f, hostname, len) != len
920c3983b2SBen Gras 		    || write(f, "\n", (size_t)1) != 1)
930c3983b2SBen Gras 			goto out;
940c3983b2SBen Gras 	}
950c3983b2SBen Gras 	if (info)  {					/* info */
960c3983b2SBen Gras 		if (!(flags & PIDLOCK_USEHOSTNAME))  {
970c3983b2SBen Gras 			/* write blank line because there's no hostname */
980c3983b2SBen Gras 			if (write(f, "\n", (size_t)1) != 1)
990c3983b2SBen Gras 				goto out;
1000c3983b2SBen Gras 		}
1010c3983b2SBen Gras 		len = strlen(info);
1020c3983b2SBen Gras 		if ((size_t)write(f, info, len) != len ||
1030c3983b2SBen Gras 		    write(f, "\n", (size_t)1) != 1)
1040c3983b2SBen Gras 			goto out;
1050c3983b2SBen Gras 	}
1060c3983b2SBen Gras 	(void)close(f);
1070c3983b2SBen Gras 	f = -1;
1080c3983b2SBen Gras 
1090c3983b2SBen Gras 	/* Hard link the temporary file to the real lock file. */
1100c3983b2SBen Gras 	/* This is an atomic operation. */
1110c3983b2SBen Gras lockfailed:
1120c3983b2SBen Gras 	while (link(tempfile, lockfile) == -1)  {
1130c3983b2SBen Gras 		if (errno != EEXIST)
1140c3983b2SBen Gras 			goto out;
1150c3983b2SBen Gras 		/* Find out who has this lockfile. */
1160c3983b2SBen Gras 		if ((f = open(lockfile, O_RDONLY, 0)) != -1)  {
117*dba3562dSLionel Sambuc 			if ((n = read(f, s, (size_t)11)) == -1)
1180c3983b2SBen Gras 				goto out;
119*dba3562dSLionel Sambuc 			if (n == 0) {
1200c3983b2SBen Gras 				errno = EINVAL;
1210c3983b2SBen Gras 				goto out;
1220c3983b2SBen Gras 			}
1230c3983b2SBen Gras 			pid2 = atoi(s);
124*dba3562dSLionel Sambuc 			if ((n = read(f, s, sizeof(s) - 2)) == -1)
1250c3983b2SBen Gras 				goto out;
126*dba3562dSLionel Sambuc 			if (n == 0)
1270c3983b2SBen Gras 				*s = '\0';
1280c3983b2SBen Gras 			s[sizeof(s) - 1] = '\0';
1290c3983b2SBen Gras 			if ((p = strchr(s, '\n')) != NULL)
1300c3983b2SBen Gras 				*p = '\0';
1310c3983b2SBen Gras 			(void)close(f);
1320c3983b2SBen Gras 			f = -1;
1330c3983b2SBen Gras 
1340c3983b2SBen Gras 			if ((flags & PIDLOCK_USEHOSTNAME) == 0 ||
1350c3983b2SBen Gras 			    strcmp(s, hostname) == 0)  {
1360c3983b2SBen Gras 				if (kill(pid2, 0) == -1 && errno == ESRCH)  {
1370c3983b2SBen Gras 					/* process doesn't exist */
1380c3983b2SBen Gras 					(void)unlink(lockfile);
1390c3983b2SBen Gras 					continue;
1400c3983b2SBen Gras 				}
1410c3983b2SBen Gras 			}
1420c3983b2SBen Gras 		}
1430c3983b2SBen Gras 		if (flags & PIDLOCK_NONBLOCK)  {
1440c3983b2SBen Gras 			if (locker)
1450c3983b2SBen Gras 				*locker = pid2;
1460c3983b2SBen Gras 			errno = EWOULDBLOCK;
1470c3983b2SBen Gras 			goto out;
1480c3983b2SBen Gras 		} else
1490c3983b2SBen Gras 			sleep(5);
1500c3983b2SBen Gras 	}
1510c3983b2SBen Gras 	/*
1520c3983b2SBen Gras 	 * Check to see that we really were successful (in case we're
1530c3983b2SBen Gras 	 * using NFS) by making sure that something really is linked
1540c3983b2SBen Gras 	 * to our tempfile (reference count is two).
1550c3983b2SBen Gras 	 */
1560c3983b2SBen Gras 	if (stat(tempfile, &st) == -1)
1570c3983b2SBen Gras 		goto out;
1580c3983b2SBen Gras 	if (st.st_nlink != 2)
1590c3983b2SBen Gras 		goto lockfailed;
1600c3983b2SBen Gras 
1610c3983b2SBen Gras 	(void)unlink(tempfile);
1620c3983b2SBen Gras  	if (locker)
1630c3983b2SBen Gras  		*locker = getpid();	/* return this process's PID on lock */
1640c3983b2SBen Gras 	errno = 0;
1650c3983b2SBen Gras 	return 0;
1660c3983b2SBen Gras out:
167*dba3562dSLionel Sambuc 	savee = errno;
1680c3983b2SBen Gras 	if (f != -1)
1690c3983b2SBen Gras 		(void)close(f);
1700c3983b2SBen Gras 	(void)unlink(tempfile);
171*dba3562dSLionel Sambuc 	errno = savee;
1720c3983b2SBen Gras 	return -1;
1730c3983b2SBen Gras }
1740c3983b2SBen Gras 
1750c3983b2SBen Gras static int
checktty(const char * tty)1760c3983b2SBen Gras checktty(const char *tty)
1770c3983b2SBen Gras {
1780c3983b2SBen Gras 	char	ttyfile[MAXPATHLEN];
1790c3983b2SBen Gras 	struct stat sb;
1800c3983b2SBen Gras 
1810c3983b2SBen Gras 	(void)strlcpy(ttyfile, _PATH_DEV, sizeof(ttyfile));
1820c3983b2SBen Gras 	(void)strlcat(ttyfile, tty, sizeof(ttyfile));
1830c3983b2SBen Gras 
1840c3983b2SBen Gras 	/* make sure the tty exists */
1850c3983b2SBen Gras 	if (stat(ttyfile, &sb) == -1)
1860c3983b2SBen Gras 		return -1;
1870c3983b2SBen Gras 	if (!S_ISCHR(sb.st_mode))  {
1880c3983b2SBen Gras 		errno = EFTYPE;
1890c3983b2SBen Gras 		return -1;
1900c3983b2SBen Gras 	}
1910c3983b2SBen Gras 	return 0;
1920c3983b2SBen Gras }
1930c3983b2SBen Gras 
1940c3983b2SBen Gras #define LOCKPATH	"/var/spool/lock/LCK.."
1950c3983b2SBen Gras 
1960c3983b2SBen Gras static char *
makelock(char * buf,size_t bufsiz,const char * tty)1970c3983b2SBen Gras makelock(char *buf, size_t bufsiz, const char *tty)
1980c3983b2SBen Gras {
1990c3983b2SBen Gras 	(void)strlcpy(buf, LOCKPATH, bufsiz);
2000c3983b2SBen Gras 	(void)strlcat(buf, tty, bufsiz);
2010c3983b2SBen Gras 	return buf;
2020c3983b2SBen Gras }
2030c3983b2SBen Gras 
2040c3983b2SBen Gras /*ARGSUSED*/
2050c3983b2SBen Gras int
ttylock(const char * tty,int flags,pid_t * locker)2060c3983b2SBen Gras ttylock(const char *tty, int flags, pid_t *locker)
2070c3983b2SBen Gras {
2080c3983b2SBen Gras 	char	lockfile[MAXPATHLEN];
2090c3983b2SBen Gras 
2100c3983b2SBen Gras 	_DIAGASSERT(tty != NULL);
2110c3983b2SBen Gras 
2120c3983b2SBen Gras 	if (checktty(tty) != 0)
2130c3983b2SBen Gras 		return -1;
2140c3983b2SBen Gras 
2150c3983b2SBen Gras 	/* do the lock */
2160c3983b2SBen Gras 	return pidlock(makelock(lockfile, sizeof(lockfile), tty),
2170c3983b2SBen Gras 	    flags, locker, 0);
2180c3983b2SBen Gras }
2190c3983b2SBen Gras 
2200c3983b2SBen Gras int
ttyunlock(const char * tty)2210c3983b2SBen Gras ttyunlock(const char *tty)
2220c3983b2SBen Gras {
2230c3983b2SBen Gras 	char	lockfile[MAXPATHLEN];
2240c3983b2SBen Gras 
2250c3983b2SBen Gras 	_DIAGASSERT(tty != NULL);
2260c3983b2SBen Gras 
2270c3983b2SBen Gras 	if (checktty(tty) != 0)
2280c3983b2SBen Gras 		return -1;
2290c3983b2SBen Gras 
2300c3983b2SBen Gras 	/* remove the lock */
2310c3983b2SBen Gras 	return unlink(makelock(lockfile, sizeof(lockfile), tty));
2320c3983b2SBen Gras }
233