19a695a1des/*-
29a695a1des * Copyright (c) 2007-2009 Dag-Erling Co��dan Sm��rgrav
39a695a1des * All rights reserved.
49a695a1des *
59a695a1des * Redistribution and use in source and binary forms, with or without
69a695a1des * modification, are permitted provided that the following conditions
79a695a1des * are met:
89a695a1des * 1. Redistributions of source code must retain the above copyright
99a695a1des *    notice, this list of conditions and the following disclaimer
109a695a1des *    in this position and unchanged.
119a695a1des * 2. Redistributions in binary form must reproduce the above copyright
129a695a1des *    notice, this list of conditions and the following disclaimer in the
139a695a1des *    documentation and/or other materials provided with the distribution.
149a695a1des *
159a695a1des * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
169a695a1des * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
179a695a1des * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189a695a1des * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199a695a1des * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
209a695a1des * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
219a695a1des * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
229a695a1des * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
239a695a1des * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
249a695a1des * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
259a695a1des * SUCH DAMAGE.
269a695a1des */
279a695a1des
289a695a1des#include <sys/cdefs.h>
299a695a1des__FBSDID("$FreeBSD$");
309a695a1des
319a695a1des#include <sys/param.h>
329a695a1des#include <sys/wait.h>
33a26096bbr#include <sys/event.h>
349a695a1des
359a695a1des#include <fcntl.h>
369a695a1des#include <errno.h>
379a695a1des#include <signal.h>
389a695a1des#include <stdint.h>
399a695a1des#include <stdio.h>
409a695a1des#include <stdlib.h>
419a695a1des#include <string.h>
429a695a1des#include <unistd.h>
439a695a1des
449a695a1des#include <libutil.h>
459a695a1des
469a695a1des/*
47a26096bbr * We need a signal handler so kill(2) will interrupt the child
48a26096bbr * instead of killing it.
499a695a1des */
509a695a1desstatic void
519a695a1dessignal_handler(int sig)
529a695a1des{
539a695a1des	(void)sig;
549a695a1des}
559a695a1des
569a695a1des/*
579a695a1des * Test that pidfile_open() can create a pidfile and that pidfile_write()
589a695a1des * can write to it.
599a695a1des */
609a695a1desstatic const char *
619a695a1destest_pidfile_uncontested(void)
629a695a1des{
639a695a1des	const char *fn = "test_pidfile_uncontested";
649a695a1des	struct pidfh *pf;
659a695a1des	pid_t other = 0;
669a695a1des
679a695a1des	unlink(fn);
689a695a1des	pf = pidfile_open(fn, 0600, &other);
699a695a1des	if (pf == NULL && other != 0)
709a695a1des		return ("pidfile exists and is locked");
719a695a1des	if (pf == NULL)
729a695a1des		return (strerror(errno));
739a695a1des	if (pidfile_write(pf) != 0) {
749a695a1des		pidfile_close(pf);
759a695a1des		unlink(fn);
769a695a1des		return ("failed to write PID");
779a695a1des	}
789a695a1des	pidfile_close(pf);
799a695a1des	unlink(fn);
809a695a1des	return (NULL);
819a695a1des}
829a695a1des
839a695a1des/*
849a695a1des * Test that pidfile_open() locks against self.
859a695a1des */
869a695a1desstatic const char *
879a695a1destest_pidfile_self(void)
889a695a1des{
899a695a1des	const char *fn = "test_pidfile_self";
909a695a1des	struct pidfh *pf1, *pf2;
919a695a1des	pid_t other = 0;
929a695a1des	int serrno;
939a695a1des
949a695a1des	unlink(fn);
959a695a1des	pf1 = pidfile_open(fn, 0600, &other);
969a695a1des	if (pf1 == NULL && other != 0)
979a695a1des		return ("pidfile exists and is locked");
989a695a1des	if (pf1 == NULL)
999a695a1des		return (strerror(errno));
1009a695a1des	if (pidfile_write(pf1) != 0) {
1019a695a1des		serrno = errno;
1029a695a1des		pidfile_close(pf1);
1039a695a1des		unlink(fn);
1049a695a1des		return (strerror(serrno));
1059a695a1des	}
1069a695a1des	// second open should fail
1079a695a1des	pf2 = pidfile_open(fn, 0600, &other);
1089a695a1des	if (pf2 != NULL) {
1099a695a1des		pidfile_close(pf1);
1109a695a1des		pidfile_close(pf2);
1119a695a1des		unlink(fn);
1129a695a1des		return ("managed to opened pidfile twice");
1139a695a1des	}
1149a695a1des	if (other != getpid()) {
1159a695a1des		pidfile_close(pf1);
1169a695a1des		unlink(fn);
1179a695a1des		return ("pidfile contained wrong PID");
1189a695a1des	}
1199a695a1des	pidfile_close(pf1);
1209a695a1des	unlink(fn);
1219a695a1des	return (NULL);
1229a695a1des}
1239a695a1des
1249a695a1des/*
1259a695a1des * Common code for test_pidfile_{contested,inherited}.
1269a695a1des */
1279a695a1desstatic const char *
1289a695a1descommon_test_pidfile_child(const char *fn, int parent_open)
1299a695a1des{
1309a695a1des	struct pidfh *pf = NULL;
1319a695a1des	pid_t other = 0, pid = 0;
1329a695a1des	int fd[2], serrno, status;
133a26096bbr	struct kevent event, ke;
1349a695a1des	char ch;
135a26096bbr	int kq;
1369a695a1des
1379a695a1des	unlink(fn);
1389a695a1des	if (pipe(fd) != 0)
1399a695a1des		return (strerror(errno));
1409a695a1des
1419a695a1des	if (parent_open) {
1429a695a1des		pf = pidfile_open(fn, 0600, &other);
1439a695a1des		if (pf == NULL && other != 0)
1449a695a1des			return ("pidfile exists and is locked");
1459a695a1des		if (pf == NULL)
1469a695a1des			return (strerror(errno));
1479a695a1des	}
1489a695a1des
1499a695a1des	pid = fork();
1509a695a1des	if (pid == -1)
1519a695a1des		return (strerror(errno));
1529a695a1des	if (pid == 0) {
1539a695a1des		// child
1549a695a1des		close(fd[0]);
1559a695a1des		signal(SIGINT, signal_handler);
1569a695a1des		if (!parent_open) {
1579a695a1des			pf = pidfile_open(fn, 0600, &other);
1589a695a1des			if (pf == NULL && other != 0)
1599a695a1des				return ("pidfile exists and is locked");
1609a695a1des			if (pf == NULL)
1619a695a1des				return (strerror(errno));
1629a695a1des		}
1639a695a1des		if (pidfile_write(pf) != 0) {
1649a695a1des			serrno = errno;
1659a695a1des			pidfile_close(pf);
1669a695a1des			unlink(fn);
1679a695a1des			return (strerror(serrno));
1689a695a1des		}
1699a695a1des		if (pf == NULL)
1709a695a1des			_exit(1);
1719a695a1des		if (pidfile_write(pf) != 0)
172a26096bbr			_exit(2);
173a26096bbr		kq = kqueue();
174a26096bbr		if (kq == -1)
175a26096bbr			_exit(3);
176a26096bbr		EV_SET(&ke, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
177a26096bbr		/* Attach event to the kqueue. */
178a26096bbr		if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0)
179a26096bbr			_exit(4);
180a26096bbr		/* Inform the parent we are ready to receive SIGINT */
1819a695a1des		if (write(fd[1], "*", 1) != 1)
182a26096bbr			_exit(5);
183a26096bbr		/* Wait for SIGINT received */
184a26096bbr		if (kevent(kq, NULL, 0, &event, 1, NULL) != 1)
185a26096bbr			_exit(6);
1869a695a1des		_exit(0);
1879a695a1des	}
1889a695a1des	// parent
1899a695a1des	close(fd[1]);
1909a695a1des	if (pf)
1919a695a1des		pidfile_close(pf);
1929a695a1des
1939a695a1des	// wait for the child to signal us
1949a695a1des	if (read(fd[0], &ch, 1) != 1) {
1959a695a1des		serrno = errno;
1969a695a1des		unlink(fn);
1979a695a1des		kill(pid, SIGTERM);
1989a695a1des		errno = serrno;
1999a695a1des		return (strerror(errno));
2009a695a1des	}
2019a695a1des
2029a695a1des	// We shouldn't be able to lock the same pidfile as our child
2039a695a1des	pf = pidfile_open(fn, 0600, &other);
2049a695a1des	if (pf != NULL) {
2059a695a1des		pidfile_close(pf);
2069a695a1des		unlink(fn);
2079a695a1des		return ("managed to lock contested pidfile");
2089a695a1des	}
2099a695a1des
2109a695a1des	// Failed to lock, but not because it was contested
2119a695a1des	if (other == 0) {
2129a695a1des		unlink(fn);
2139a695a1des		return (strerror(errno));
2149a695a1des	}
2159a695a1des
2169a695a1des	// Locked by the wrong process
2179a695a1des	if (other != pid) {
2189a695a1des		unlink(fn);
2199a695a1des		return ("pidfile contained wrong PID");
2209a695a1des	}
2219a695a1des
2229a695a1des	// check our child's fate
2239a695a1des	if (pf)
2249a695a1des		pidfile_close(pf);
2259a695a1des	unlink(fn);
2269a695a1des	if (kill(pid, SIGINT) != 0)
2279a695a1des		return (strerror(errno));
2289a695a1des	if (waitpid(pid, &status, 0) == -1)
2299a695a1des		return (strerror(errno));
2309a695a1des	if (WIFSIGNALED(status))
2319a695a1des		return ("child caught signal");
2329a695a1des	if (WEXITSTATUS(status) != 0)
2339a695a1des		return ("child returned non-zero status");
2349a695a1des
2359a695a1des	// success
2369a695a1des	return (NULL);
2379a695a1des}
2389a695a1des
2399a695a1des/*
2409a695a1des * Test that pidfile_open() fails when attempting to open a pidfile that
2419a695a1des * is already locked, and that it returns the correct PID.
2429a695a1des */
2439a695a1desstatic const char *
2449a695a1destest_pidfile_contested(void)
2459a695a1des{
2469a695a1des	const char *fn = "test_pidfile_contested";
2479a695a1des	const char *result;
2489a695a1des
2499a695a1des	result = common_test_pidfile_child(fn, 0);
2509a695a1des	return (result);
2519a695a1des}
2529a695a1des
2539a695a1des/*
2549a695a1des * Test that the pidfile lock is inherited.
2559a695a1des */
2569a695a1desstatic const char *
2579a695a1destest_pidfile_inherited(void)
2589a695a1des{
2599a695a1des	const char *fn = "test_pidfile_inherited";
2609a695a1des	const char *result;
2619a695a1des
2629a695a1des	result = common_test_pidfile_child(fn, 1);
2639a695a1des	return (result);
2649a695a1des}
2659a695a1des
266562ca52markj/*
267562ca52markj * Make sure we handle relative pidfile paths correctly.
268562ca52markj */
269562ca52markjstatic const char *
270562ca52markjtest_pidfile_relative(void)
271562ca52markj{
272562ca52markj	char path[PATH_MAX], pid[32], tmpdir[PATH_MAX];
273562ca52markj	struct pidfh *pfh;
274562ca52markj	int fd;
275562ca52markj
276562ca52markj	(void)snprintf(tmpdir, sizeof(tmpdir), "%s.XXXXXX", __func__);
277562ca52markj	if (mkdtemp(tmpdir) == NULL)
278562ca52markj		return (strerror(errno));
279562ca52markj	(void)snprintf(path, sizeof(path), "%s/pidfile", tmpdir);
280562ca52markj
281562ca52markj	pfh = pidfile_open(path, 0600, NULL);
282562ca52markj	if (pfh == NULL)
283562ca52markj		return (strerror(errno));
284562ca52markj	if (pidfile_write(pfh) != 0)
285562ca52markj		return (strerror(errno));
286562ca52markj	fd = open(path, O_RDONLY);
287562ca52markj	if (fd < 0)
288562ca52markj		return (strerror(errno));
289562ca52markj	if (read(fd, pid, sizeof(pid)) < 0)
290562ca52markj		return (strerror(errno));
291562ca52markj	if (atoi(pid) != getpid())
292562ca52markj		return ("pid mismatch");
293562ca52markj	if (close(fd) != 0)
294562ca52markj		return (strerror(errno));
295562ca52markj	if (pidfile_close(pfh) != 0)
296562ca52markj		return (strerror(errno));
297562ca52markj	return (NULL);
298562ca52markj}
299562ca52markj
3009a695a1desstatic struct test {
3019a695a1des	const char *name;
3029a695a1des	const char *(*func)(void);
3039a695a1des} t[] = {
3049a695a1des	{ "pidfile_uncontested", test_pidfile_uncontested },
3059a695a1des	{ "pidfile_self", test_pidfile_self },
3069a695a1des	{ "pidfile_contested", test_pidfile_contested },
3079a695a1des	{ "pidfile_inherited", test_pidfile_inherited },
308562ca52markj	{ "pidfile_relative", test_pidfile_relative },
3099a695a1des};
3109a695a1des
3119a695a1desint
3129a695a1desmain(void)
3139a695a1des{
3149a695a1des	const char *result;
3159a695a1des	int i, nt;
3169a695a1des
3179a695a1des	nt = sizeof(t) / sizeof(*t);
3189a695a1des	printf("1..%d\n", nt);
3199a695a1des	for (i = 0; i < nt; ++i) {
3209a695a1des		if ((result = t[i].func()) != NULL)
3219a695a1des			printf("not ok %d - %s # %s\n", i + 1,
3229a695a1des			    t[i].name, result);
3239a695a1des		else
3249a695a1des			printf("ok %d - %s\n", i + 1,
3259a695a1des			    t[i].name);
3269a695a1des	}
3279a695a1des	exit(0);
3289a695a1des}
329