xref: /netbsd-src/lib/libutil/pidlock.c (revision b061283a45cc67fe4fd6b3ed11dc29bfed4deb87)
1*b061283aSryo /*	$NetBSD: pidlock.c,v 1.17 2020/03/30 08:24:36 ryo Exp $ */
209ced793Scjs 
309ced793Scjs /*
499410184Ssalo  * Copyright 1996, 1997 by Curt Sampson <cjs@NetBSD.org>.
509ced793Scjs  *
609ced793Scjs  * Redistribution and use in source and binary forms, with or without
709ced793Scjs  * modification, are permitted provided that the following conditions
809ced793Scjs  * are met:
909ced793Scjs  * 1. Redistributions of source code must retain the above copyright
1009ced793Scjs  *    notice, this list of conditions and the following disclaimer.
1109ced793Scjs  *
1209ced793Scjs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
1309ced793Scjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1409ced793Scjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1509ced793Scjs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
1609ced793Scjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1709ced793Scjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1809ced793Scjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1909ced793Scjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2009ced793Scjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2109ced793Scjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2209ced793Scjs  * SUCH DAMAGE.
2309ced793Scjs  */
2409ced793Scjs 
2509ced793Scjs #include <sys/cdefs.h>
2609ced793Scjs #if defined(LIBC_SCCS) && !defined(lint)
27*b061283aSryo __RCSID("$NetBSD: pidlock.c,v 1.17 2020/03/30 08:24:36 ryo Exp $");
2809ced793Scjs #endif /* LIBC_SCCS and not lint */
2909ced793Scjs 
3009ced793Scjs #include <sys/param.h>
3109ced793Scjs #include <sys/stat.h>
3209ced793Scjs #include <sys/types.h>
3309ced793Scjs 
34b48252f3Slukem #include <assert.h>
3566208204Skleink #include <errno.h>
3609ced793Scjs #include <fcntl.h>
3709ced793Scjs #include <signal.h>
3809ced793Scjs #include <stdio.h>
3909ced793Scjs #include <stdlib.h>
4009ced793Scjs #include <string.h>
4109ced793Scjs #include <unistd.h>
4209ced793Scjs #include <util.h>
436b99850eSchristos #include <paths.h>
4409ced793Scjs 
4509ced793Scjs /*
4609ced793Scjs  * Create a lockfile. Return 0 when locked, -1 on error.
4709ced793Scjs  */
4809ced793Scjs int
pidlock(const char * lockfile,int flags,pid_t * locker,const char * info)49fce98185Sad pidlock(const char *lockfile, int flags, pid_t *locker, const char *info)
5009ced793Scjs {
5109ced793Scjs 	char	tempfile[MAXPATHLEN];
522beab49aSmrg 	char	hostname[MAXHOSTNAMELEN + 1];
5309ced793Scjs 	pid_t	pid2 = -1;
548e29c988Scjs 	struct	stat st;
554a87bdacSchristos 	ssize_t	n;
564a87bdacSchristos 	int	f = -1, savee;
5709ced793Scjs 	char	s[256];
5809ced793Scjs 	char	*p;
596b99850eSchristos 	size_t	len;
6009ced793Scjs 
61b48252f3Slukem 	_DIAGASSERT(lockfile != NULL);
62b48252f3Slukem 	/* locker may be NULL */
63b48252f3Slukem 	/* info may be NULL */
64b48252f3Slukem 
65b48252f3Slukem 
668e29c988Scjs 	if (gethostname(hostname, sizeof(hostname)))
678e29c988Scjs 		return -1;
682beab49aSmrg 	hostname[sizeof(hostname) - 1] = '\0';
698e29c988Scjs 
70*b061283aSryo 	/* avoid '/' in hostname, as it may contain arbitrary characters */
71*b061283aSryo 	for (p = hostname; *p != '\0'; p++) {
72*b061283aSryo 		if (*p == '/')
73*b061283aSryo 			*p = '_';
74*b061283aSryo 	}
75*b061283aSryo 
768e29c988Scjs 	/*
778e29c988Scjs 	 * Build a path to the temporary file.
788e29c988Scjs 	 * We use the path with the PID and hostname appended.
798e29c988Scjs 	 * XXX This is not thread safe.
808e29c988Scjs 	 */
818e29c988Scjs 	if (snprintf(tempfile, sizeof(tempfile), "%s.%d.%s", lockfile,
82c5eb4ab6Slukem 	    (int) getpid(), hostname) >= (int)sizeof(tempfile))  {
8309ced793Scjs 		errno = ENAMETOOLONG;
8409ced793Scjs 		return -1;
8509ced793Scjs 	}
8609ced793Scjs 
8709ced793Scjs 	/* Open it, write pid, hostname, info. */
886b99850eSchristos 	if ((f = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1)
896b99850eSchristos 		goto out;
906b99850eSchristos 
916b99850eSchristos 	(void)snprintf(s, sizeof(s), "%10d\n", getpid());	/* pid */
926b99850eSchristos 	if (write(f, s, (size_t)11) != 11)
936b99850eSchristos 		goto out;
946b99850eSchristos 
958e29c988Scjs 	if ((flags & PIDLOCK_USEHOSTNAME))  {		/* hostname */
966b99850eSchristos 		len = strlen(hostname);
97c5eb4ab6Slukem 		if ((size_t)write(f, hostname, len) != len
986b99850eSchristos 		    || write(f, "\n", (size_t)1) != 1)
996b99850eSchristos 			goto out;
10009ced793Scjs 	}
10109ced793Scjs 	if (info)  {					/* info */
10209ced793Scjs 		if (!(flags & PIDLOCK_USEHOSTNAME))  {
10309ced793Scjs 			/* write blank line because there's no hostname */
1046b99850eSchristos 			if (write(f, "\n", (size_t)1) != 1)
1056b99850eSchristos 				goto out;
10609ced793Scjs 		}
1076b99850eSchristos 		len = strlen(info);
108c5eb4ab6Slukem 		if ((size_t)write(f, info, len) != len ||
1096b99850eSchristos 		    write(f, "\n", (size_t)1) != 1)
1106b99850eSchristos 			goto out;
11109ced793Scjs 	}
1126b99850eSchristos 	(void)close(f);
1136b99850eSchristos 	f = -1;
11409ced793Scjs 
11509ced793Scjs 	/* Hard link the temporary file to the real lock file. */
11609ced793Scjs 	/* This is an atomic operation. */
1178e29c988Scjs lockfailed:
1186b99850eSchristos 	while (link(tempfile, lockfile) == -1)  {
1196b99850eSchristos 		if (errno != EEXIST)
1206b99850eSchristos 			goto out;
12109ced793Scjs 		/* Find out who has this lockfile. */
1226b99850eSchristos 		if ((f = open(lockfile, O_RDONLY, 0)) != -1)  {
1234a87bdacSchristos 			if ((n = read(f, s, (size_t)11)) == -1)
1246b99850eSchristos 				goto out;
1254a87bdacSchristos 			if (n == 0) {
1266b99850eSchristos 				errno = EINVAL;
1276b99850eSchristos 				goto out;
1286b99850eSchristos 			}
12909ced793Scjs 			pid2 = atoi(s);
1304a87bdacSchristos 			if ((n = read(f, s, sizeof(s) - 2)) == -1)
1316b99850eSchristos 				goto out;
1324a87bdacSchristos 			if (n == 0)
1336b99850eSchristos 				*s = '\0';
13409ced793Scjs 			s[sizeof(s) - 1] = '\0';
135983a6b1fSchristos 			if ((p = strchr(s, '\n')) != NULL)
13609ced793Scjs 				*p = '\0';
1376b99850eSchristos 			(void)close(f);
1386b99850eSchristos 			f = -1;
13909ced793Scjs 
1406b99850eSchristos 			if ((flags & PIDLOCK_USEHOSTNAME) == 0 ||
1416b99850eSchristos 			    strcmp(s, hostname) == 0)  {
1426b99850eSchristos 				if (kill(pid2, 0) == -1 && errno == ESRCH)  {
14309ced793Scjs 					/* process doesn't exist */
1446b99850eSchristos 					(void)unlink(lockfile);
14509ced793Scjs 					continue;
14609ced793Scjs 				}
14709ced793Scjs 			}
1488e29c988Scjs 		}
14909ced793Scjs 		if (flags & PIDLOCK_NONBLOCK)  {
15009ced793Scjs 			if (locker)
15109ced793Scjs 				*locker = pid2;
15209ced793Scjs 			errno = EWOULDBLOCK;
1536b99850eSchristos 			goto out;
15409ced793Scjs 		} else
15509ced793Scjs 			sleep(5);
15609ced793Scjs 	}
1578e29c988Scjs 	/*
1588e29c988Scjs 	 * Check to see that we really were successful (in case we're
1598e29c988Scjs 	 * using NFS) by making sure that something really is linked
1608e29c988Scjs 	 * to our tempfile (reference count is two).
1618e29c988Scjs 	 */
1626b99850eSchristos 	if (stat(tempfile, &st) == -1)
1636b99850eSchristos 		goto out;
1648e29c988Scjs 	if (st.st_nlink != 2)
1658e29c988Scjs 		goto lockfailed;
16609ced793Scjs 
1676b99850eSchristos 	(void)unlink(tempfile);
1688e29c988Scjs  	if (locker)
1698e29c988Scjs  		*locker = getpid();	/* return this process's PID on lock */
1708e29c988Scjs 	errno = 0;
17109ced793Scjs 	return 0;
1726b99850eSchristos out:
1734a87bdacSchristos 	savee = errno;
1746b99850eSchristos 	if (f != -1)
1756b99850eSchristos 		(void)close(f);
1766b99850eSchristos 	(void)unlink(tempfile);
1774a87bdacSchristos 	errno = savee;
1786b99850eSchristos 	return -1;
1796b99850eSchristos }
1806b99850eSchristos 
1816b99850eSchristos static int
checktty(const char * tty)1826b99850eSchristos checktty(const char *tty)
1836b99850eSchristos {
1846b99850eSchristos 	char	ttyfile[MAXPATHLEN];
1856b99850eSchristos 	struct stat sb;
1866b99850eSchristos 
1876b99850eSchristos 	(void)strlcpy(ttyfile, _PATH_DEV, sizeof(ttyfile));
1886b99850eSchristos 	(void)strlcat(ttyfile, tty, sizeof(ttyfile));
1896b99850eSchristos 
1906b99850eSchristos 	/* make sure the tty exists */
1916b99850eSchristos 	if (stat(ttyfile, &sb) == -1)
1926b99850eSchristos 		return -1;
1936b99850eSchristos 	if (!S_ISCHR(sb.st_mode))  {
1946b99850eSchristos 		errno = EFTYPE;
1956b99850eSchristos 		return -1;
1966b99850eSchristos 	}
1976b99850eSchristos 	return 0;
19809ced793Scjs }
19909ced793Scjs 
20009ced793Scjs #define LOCKPATH	"/var/spool/lock/LCK.."
2016b99850eSchristos 
2026b99850eSchristos static char *
makelock(char * buf,size_t bufsiz,const char * tty)2036b99850eSchristos makelock(char *buf, size_t bufsiz, const char *tty)
2046b99850eSchristos {
2056b99850eSchristos 	(void)strlcpy(buf, LOCKPATH, bufsiz);
2066b99850eSchristos 	(void)strlcat(buf, tty, bufsiz);
2076b99850eSchristos 	return buf;
2086b99850eSchristos }
20909ced793Scjs 
210983a6b1fSchristos /*ARGSUSED*/
21109ced793Scjs int
ttylock(const char * tty,int flags,pid_t * locker)212fce98185Sad ttylock(const char *tty, int flags, pid_t *locker)
21309ced793Scjs {
21409ced793Scjs 	char	lockfile[MAXPATHLEN];
21509ced793Scjs 
216b48252f3Slukem 	_DIAGASSERT(tty != NULL);
217b48252f3Slukem 
2186b99850eSchristos 	if (checktty(tty) != 0)
2196b99850eSchristos 		return -1;
22009ced793Scjs 
22109ced793Scjs 	/* do the lock */
2226b99850eSchristos 	return pidlock(makelock(lockfile, sizeof(lockfile), tty),
2236b99850eSchristos 	    flags, locker, 0);
22409ced793Scjs }
22509ced793Scjs 
22609ced793Scjs int
ttyunlock(const char * tty)227fce98185Sad ttyunlock(const char *tty)
22809ced793Scjs {
22909ced793Scjs 	char	lockfile[MAXPATHLEN];
23009ced793Scjs 
231b48252f3Slukem 	_DIAGASSERT(tty != NULL);
232b48252f3Slukem 
2336b99850eSchristos 	if (checktty(tty) != 0)
2346b99850eSchristos 		return -1;
23509ced793Scjs 
2366b99850eSchristos 	/* remove the lock */
2376b99850eSchristos 	return unlink(makelock(lockfile, sizeof(lockfile), tty));
23809ced793Scjs }
239