#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: pidlock.c,v 1.17 2020/03/30 08:24:36 ryo Exp $");
#endif
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include <paths.h>
int
pidlock(const char *lockfile, int flags, pid_t *locker, const char *info)
{
char tempfile[MAXPATHLEN];
char hostname[MAXHOSTNAMELEN + 1];
pid_t pid2 = -1;
struct stat st;
ssize_t n;
int f = -1, savee;
char s[256];
char *p;
size_t len;
_DIAGASSERT(lockfile != NULL);
if (gethostname(hostname, sizeof(hostname)))
return -1;
hostname[sizeof(hostname) - 1] = '\0';
for (p = hostname; *p != '\0'; p++) {
if (*p == '/')
*p = '_';
}
if (snprintf(tempfile, sizeof(tempfile), "%s.%d.%s", lockfile,
(int) getpid(), hostname) >= (int)sizeof(tempfile)) {
errno = ENAMETOOLONG;
return -1;
}
if ((f = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1)
goto out;
(void)snprintf(s, sizeof(s), "%10d\n", getpid());
if (write(f, s, (size_t)11) != 11)
goto out;
if ((flags & PIDLOCK_USEHOSTNAME)) {
len = strlen(hostname);
if ((size_t)write(f, hostname, len) != len
|| write(f, "\n", (size_t)1) != 1)
goto out;
}
if (info) {
if (!(flags & PIDLOCK_USEHOSTNAME)) {
if (write(f, "\n", (size_t)1) != 1)
goto out;
}
len = strlen(info);
if ((size_t)write(f, info, len) != len ||
write(f, "\n", (size_t)1) != 1)
goto out;
}
(void)close(f);
f = -1;
lockfailed:
while (link(tempfile, lockfile) == -1) {
if (errno != EEXIST)
goto out;
if ((f = open(lockfile, O_RDONLY, 0)) != -1) {
if ((n = read(f, s, (size_t)11)) == -1)
goto out;
if (n == 0) {
errno = EINVAL;
goto out;
}
pid2 = atoi(s);
if ((n = read(f, s, sizeof(s) - 2)) == -1)
goto out;
if (n == 0)
*s = '\0';
s[sizeof(s) - 1] = '\0';
if ((p = strchr(s, '\n')) != NULL)
*p = '\0';
(void)close(f);
f = -1;
if ((flags & PIDLOCK_USEHOSTNAME) == 0 ||
strcmp(s, hostname) == 0) {
if (kill(pid2, 0) == -1 && errno == ESRCH) {
(void)unlink(lockfile);
continue;
}
}
}
if (flags & PIDLOCK_NONBLOCK) {
if (locker)
*locker = pid2;
errno = EWOULDBLOCK;
goto out;
} else
sleep(5);
}
if (stat(tempfile, &st) == -1)
goto out;
if (st.st_nlink != 2)
goto lockfailed;
(void)unlink(tempfile);
if (locker)
*locker = getpid();
errno = 0;
return 0;
out:
savee = errno;
if (f != -1)
(void)close(f);
(void)unlink(tempfile);
errno = savee;
return -1;
}
static int
checktty(const char *tty)
{
char ttyfile[MAXPATHLEN];
struct stat sb;
(void)strlcpy(ttyfile, _PATH_DEV, sizeof(ttyfile));
(void)strlcat(ttyfile, tty, sizeof(ttyfile));
if (stat(ttyfile, &sb) == -1)
return -1;
if (!S_ISCHR(sb.st_mode)) {
errno = EFTYPE;
return -1;
}
return 0;
}
#define LOCKPATH "/var/spool/lock/LCK.."
static char *
makelock(char *buf, size_t bufsiz, const char *tty)
{
(void)strlcpy(buf, LOCKPATH, bufsiz);
(void)strlcat(buf, tty, bufsiz);
return buf;
}
int
ttylock(const char *tty, int flags, pid_t *locker)
{
char lockfile[MAXPATHLEN];
_DIAGASSERT(tty != NULL);
if (checktty(tty) != 0)
return -1;
return pidlock(makelock(lockfile, sizeof(lockfile), tty),
flags, locker, 0);
}
int
ttyunlock(const char *tty)
{
char lockfile[MAXPATHLEN];
_DIAGASSERT(tty != NULL);
if (checktty(tty) != 0)
return -1;
return unlink(makelock(lockfile, sizeof(lockfile), tty));
}