2012-04-07 20:17:17 +04:00
|
|
|
/* $NetBSD: pidlock.c,v 1.16 2012/04/07 16:17:17 christos Exp $ */
|
1997-10-11 06:56:22 +04:00
|
|
|
|
|
|
|
/*
|
2003-07-26 23:24:24 +04:00
|
|
|
* Copyright 1996, 1997 by Curt Sampson <cjs@NetBSD.org>.
|
1997-10-11 06:56:22 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2012-04-07 20:17:17 +04:00
|
|
|
__RCSID("$NetBSD: pidlock.c,v 1.16 2012/04/07 16:17:17 christos Exp $");
|
1997-10-11 06:56:22 +04:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <assert.h>
|
1999-01-12 02:20:35 +03:00
|
|
|
#include <errno.h>
|
1997-10-11 06:56:22 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
2006-03-20 00:55:37 +03:00
|
|
|
#include <paths.h>
|
1997-10-11 06:56:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a lockfile. Return 0 when locked, -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
2000-07-05 15:46:40 +04:00
|
|
|
pidlock(const char *lockfile, int flags, pid_t *locker, const char *info)
|
1997-10-11 06:56:22 +04:00
|
|
|
{
|
|
|
|
char tempfile[MAXPATHLEN];
|
1998-07-06 10:45:41 +04:00
|
|
|
char hostname[MAXHOSTNAMELEN + 1];
|
1997-10-11 06:56:22 +04:00
|
|
|
pid_t pid2 = -1;
|
1997-10-12 13:58:23 +04:00
|
|
|
struct stat st;
|
2012-04-07 20:17:17 +04:00
|
|
|
ssize_t n;
|
|
|
|
int f = -1, savee;
|
1997-10-11 06:56:22 +04:00
|
|
|
char s[256];
|
|
|
|
char *p;
|
2006-03-20 00:55:37 +03:00
|
|
|
size_t len;
|
1997-10-11 06:56:22 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(lockfile != NULL);
|
|
|
|
/* locker may be NULL */
|
|
|
|
/* info may be NULL */
|
|
|
|
|
|
|
|
|
1997-10-12 13:58:23 +04:00
|
|
|
if (gethostname(hostname, sizeof(hostname)))
|
|
|
|
return -1;
|
1998-07-06 10:45:41 +04:00
|
|
|
hostname[sizeof(hostname) - 1] = '\0';
|
1997-10-12 13:58:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Build a path to the temporary file.
|
|
|
|
* We use the path with the PID and hostname appended.
|
|
|
|
* XXX This is not thread safe.
|
|
|
|
*/
|
|
|
|
if (snprintf(tempfile, sizeof(tempfile), "%s.%d.%s", lockfile,
|
2009-01-18 14:56:51 +03:00
|
|
|
(int) getpid(), hostname) >= (int)sizeof(tempfile)) {
|
1997-10-11 06:56:22 +04:00
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open it, write pid, hostname, info. */
|
2006-03-20 00:55:37 +03:00
|
|
|
if ((f = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
(void)snprintf(s, sizeof(s), "%10d\n", getpid()); /* pid */
|
|
|
|
if (write(f, s, (size_t)11) != 11)
|
|
|
|
goto out;
|
|
|
|
|
1997-10-12 13:58:23 +04:00
|
|
|
if ((flags & PIDLOCK_USEHOSTNAME)) { /* hostname */
|
2006-03-20 00:55:37 +03:00
|
|
|
len = strlen(hostname);
|
2009-01-18 14:56:51 +03:00
|
|
|
if ((size_t)write(f, hostname, len) != len
|
2006-03-20 00:55:37 +03:00
|
|
|
|| write(f, "\n", (size_t)1) != 1)
|
|
|
|
goto out;
|
1997-10-11 06:56:22 +04:00
|
|
|
}
|
|
|
|
if (info) { /* info */
|
|
|
|
if (!(flags & PIDLOCK_USEHOSTNAME)) {
|
|
|
|
/* write blank line because there's no hostname */
|
2006-03-20 00:55:37 +03:00
|
|
|
if (write(f, "\n", (size_t)1) != 1)
|
|
|
|
goto out;
|
1997-10-11 06:56:22 +04:00
|
|
|
}
|
2006-03-20 00:55:37 +03:00
|
|
|
len = strlen(info);
|
2009-01-18 14:56:51 +03:00
|
|
|
if ((size_t)write(f, info, len) != len ||
|
2006-03-20 00:55:37 +03:00
|
|
|
write(f, "\n", (size_t)1) != 1)
|
|
|
|
goto out;
|
1997-10-11 06:56:22 +04:00
|
|
|
}
|
2006-03-20 00:55:37 +03:00
|
|
|
(void)close(f);
|
|
|
|
f = -1;
|
1997-10-11 06:56:22 +04:00
|
|
|
|
|
|
|
/* Hard link the temporary file to the real lock file. */
|
|
|
|
/* This is an atomic operation. */
|
1997-10-12 13:58:23 +04:00
|
|
|
lockfailed:
|
2006-03-20 00:55:37 +03:00
|
|
|
while (link(tempfile, lockfile) == -1) {
|
|
|
|
if (errno != EEXIST)
|
|
|
|
goto out;
|
1997-10-11 06:56:22 +04:00
|
|
|
/* Find out who has this lockfile. */
|
2006-03-20 00:55:37 +03:00
|
|
|
if ((f = open(lockfile, O_RDONLY, 0)) != -1) {
|
2012-04-07 20:17:17 +04:00
|
|
|
if ((n = read(f, s, (size_t)11)) == -1)
|
2006-03-20 00:55:37 +03:00
|
|
|
goto out;
|
2012-04-07 20:17:17 +04:00
|
|
|
if (n == 0) {
|
2006-03-20 00:55:37 +03:00
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
1997-10-12 13:58:23 +04:00
|
|
|
pid2 = atoi(s);
|
2012-04-07 20:17:17 +04:00
|
|
|
if ((n = read(f, s, sizeof(s) - 2)) == -1)
|
2006-03-20 00:55:37 +03:00
|
|
|
goto out;
|
2012-04-07 20:17:17 +04:00
|
|
|
if (n == 0)
|
2006-03-20 00:55:37 +03:00
|
|
|
*s = '\0';
|
|
|
|
s[sizeof(s) - 1] = '\0';
|
|
|
|
if ((p = strchr(s, '\n')) != NULL)
|
1997-10-12 13:58:23 +04:00
|
|
|
*p = '\0';
|
2006-03-20 00:55:37 +03:00
|
|
|
(void)close(f);
|
|
|
|
f = -1;
|
1997-10-11 06:56:22 +04:00
|
|
|
|
2006-03-20 00:55:37 +03:00
|
|
|
if ((flags & PIDLOCK_USEHOSTNAME) == 0 ||
|
|
|
|
strcmp(s, hostname) == 0) {
|
|
|
|
if (kill(pid2, 0) == -1 && errno == ESRCH) {
|
1997-10-12 13:58:23 +04:00
|
|
|
/* process doesn't exist */
|
2006-03-20 00:55:37 +03:00
|
|
|
(void)unlink(lockfile);
|
1997-10-12 13:58:23 +04:00
|
|
|
continue;
|
|
|
|
}
|
1997-10-11 06:56:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags & PIDLOCK_NONBLOCK) {
|
|
|
|
if (locker)
|
|
|
|
*locker = pid2;
|
|
|
|
errno = EWOULDBLOCK;
|
2006-03-20 00:55:37 +03:00
|
|
|
goto out;
|
1997-10-11 06:56:22 +04:00
|
|
|
} else
|
|
|
|
sleep(5);
|
|
|
|
}
|
1997-10-12 13:58:23 +04:00
|
|
|
/*
|
|
|
|
* Check to see that we really were successful (in case we're
|
|
|
|
* using NFS) by making sure that something really is linked
|
|
|
|
* to our tempfile (reference count is two).
|
|
|
|
*/
|
2006-03-20 00:55:37 +03:00
|
|
|
if (stat(tempfile, &st) == -1)
|
|
|
|
goto out;
|
1997-10-12 13:58:23 +04:00
|
|
|
if (st.st_nlink != 2)
|
|
|
|
goto lockfailed;
|
1997-10-11 06:56:22 +04:00
|
|
|
|
2006-03-20 00:55:37 +03:00
|
|
|
(void)unlink(tempfile);
|
1997-10-12 13:58:23 +04:00
|
|
|
if (locker)
|
|
|
|
*locker = getpid(); /* return this process's PID on lock */
|
|
|
|
errno = 0;
|
1997-10-11 06:56:22 +04:00
|
|
|
return 0;
|
2006-03-20 00:55:37 +03:00
|
|
|
out:
|
2012-04-07 20:17:17 +04:00
|
|
|
savee = errno;
|
2006-03-20 00:55:37 +03:00
|
|
|
if (f != -1)
|
|
|
|
(void)close(f);
|
|
|
|
(void)unlink(tempfile);
|
2012-04-07 20:17:17 +04:00
|
|
|
errno = savee;
|
2006-03-20 00:55:37 +03:00
|
|
|
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));
|
|
|
|
|
|
|
|
/* make sure the tty exists */
|
|
|
|
if (stat(ttyfile, &sb) == -1)
|
|
|
|
return -1;
|
|
|
|
if (!S_ISCHR(sb.st_mode)) {
|
|
|
|
errno = EFTYPE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
1997-10-11 06:56:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define LOCKPATH "/var/spool/lock/LCK.."
|
2006-03-20 00:55:37 +03:00
|
|
|
|
|
|
|
static char *
|
|
|
|
makelock(char *buf, size_t bufsiz, const char *tty)
|
|
|
|
{
|
|
|
|
(void)strlcpy(buf, LOCKPATH, bufsiz);
|
|
|
|
(void)strlcat(buf, tty, bufsiz);
|
|
|
|
return buf;
|
|
|
|
}
|
1997-10-11 06:56:22 +04:00
|
|
|
|
1998-12-09 17:35:02 +03:00
|
|
|
/*ARGSUSED*/
|
1997-10-11 06:56:22 +04:00
|
|
|
int
|
2000-07-05 15:46:40 +04:00
|
|
|
ttylock(const char *tty, int flags, pid_t *locker)
|
1997-10-11 06:56:22 +04:00
|
|
|
{
|
|
|
|
char lockfile[MAXPATHLEN];
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(tty != NULL);
|
|
|
|
|
2006-03-20 00:55:37 +03:00
|
|
|
if (checktty(tty) != 0)
|
|
|
|
return -1;
|
1997-10-11 06:56:22 +04:00
|
|
|
|
|
|
|
/* do the lock */
|
2006-03-20 00:55:37 +03:00
|
|
|
return pidlock(makelock(lockfile, sizeof(lockfile), tty),
|
|
|
|
flags, locker, 0);
|
1997-10-11 06:56:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-05 15:46:40 +04:00
|
|
|
ttyunlock(const char *tty)
|
1997-10-11 06:56:22 +04:00
|
|
|
{
|
|
|
|
char lockfile[MAXPATHLEN];
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(tty != NULL);
|
|
|
|
|
2006-03-20 00:55:37 +03:00
|
|
|
if (checktty(tty) != 0)
|
|
|
|
return -1;
|
1997-10-11 06:56:22 +04:00
|
|
|
|
2006-03-20 00:55:37 +03:00
|
|
|
/* remove the lock */
|
|
|
|
return unlink(makelock(lockfile, sizeof(lockfile), tty));
|
1997-10-11 06:56:22 +04:00
|
|
|
}
|