#include <sys/param.h>
#include <sys/wait.h>
#include <sys/event.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libutil.h>
static void
signal_handler(int sig)
{
(void)sig;
}
static const char *
test_pidfile_uncontested(void)
{
const char *fn = "test_pidfile_uncontested";
struct pidfh *pf;
pid_t other = 0;
unlink(fn);
pf = pidfile_open(fn, 0600, &other);
if (pf == NULL && other != 0)
return ("pidfile exists and is locked");
if (pf == NULL)
return (strerror(errno));
if (pidfile_write(pf) != 0) {
pidfile_close(pf);
unlink(fn);
return ("failed to write PID");
}
pidfile_close(pf);
unlink(fn);
return (NULL);
}
static const char *
test_pidfile_self(void)
{
const char *fn = "test_pidfile_self";
struct pidfh *pf1, *pf2;
pid_t other = 0;
int serrno;
unlink(fn);
pf1 = pidfile_open(fn, 0600, &other);
if (pf1 == NULL && other != 0)
return ("pidfile exists and is locked");
if (pf1 == NULL)
return (strerror(errno));
if (pidfile_write(pf1) != 0) {
serrno = errno;
pidfile_close(pf1);
unlink(fn);
return (strerror(serrno));
}
pf2 = pidfile_open(fn, 0600, &other);
if (pf2 != NULL) {
pidfile_close(pf1);
pidfile_close(pf2);
unlink(fn);
return ("managed to opened pidfile twice");
}
if (other != getpid()) {
pidfile_close(pf1);
unlink(fn);
return ("pidfile contained wrong PID");
}
pidfile_close(pf1);
unlink(fn);
return (NULL);
}
static const char *
common_test_pidfile_child(const char *fn, int parent_open)
{
struct pidfh *pf = NULL;
pid_t other = 0, pid = 0;
int fd[2], serrno, status;
struct kevent event, ke;
char ch;
int kq;
unlink(fn);
if (pipe(fd) != 0)
return (strerror(errno));
if (parent_open) {
pf = pidfile_open(fn, 0600, &other);
if (pf == NULL && other != 0)
return ("pidfile exists and is locked");
if (pf == NULL)
return (strerror(errno));
}
pid = fork();
if (pid == -1)
return (strerror(errno));
if (pid == 0) {
close(fd[0]);
signal(SIGINT, signal_handler);
if (!parent_open) {
pf = pidfile_open(fn, 0600, &other);
if (pf == NULL && other != 0)
return ("pidfile exists and is locked");
if (pf == NULL)
return (strerror(errno));
}
if (pidfile_write(pf) != 0) {
serrno = errno;
pidfile_close(pf);
unlink(fn);
return (strerror(serrno));
}
if (pf == NULL)
_exit(1);
if (pidfile_write(pf) != 0)
_exit(2);
kq = kqueue();
if (kq == -1)
_exit(3);
EV_SET(&ke, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0)
_exit(4);
if (write(fd[1], "*", 1) != 1)
_exit(5);
if (kevent(kq, NULL, 0, &event, 1, NULL) != 1)
_exit(6);
_exit(0);
}
close(fd[1]);
if (pf)
pidfile_close(pf);
if (read(fd[0], &ch, 1) != 1) {
serrno = errno;
unlink(fn);
kill(pid, SIGTERM);
errno = serrno;
return (strerror(errno));
}
pf = pidfile_open(fn, 0600, &other);
if (pf != NULL) {
pidfile_close(pf);
unlink(fn);
return ("managed to lock contested pidfile");
}
if (other == 0) {
unlink(fn);
return (strerror(errno));
}
if (other != pid) {
unlink(fn);
return ("pidfile contained wrong PID");
}
if (pf)
pidfile_close(pf);
unlink(fn);
if (kill(pid, SIGINT) != 0)
return (strerror(errno));
if (waitpid(pid, &status, 0) == -1)
return (strerror(errno));
if (WIFSIGNALED(status))
return ("child caught signal");
if (WEXITSTATUS(status) != 0)
return ("child returned non-zero status");
return (NULL);
}
static const char *
test_pidfile_contested(void)
{
const char *fn = "test_pidfile_contested";
const char *result;
result = common_test_pidfile_child(fn, 0);
return (result);
}
static const char *
test_pidfile_inherited(void)
{
const char *fn = "test_pidfile_inherited";
const char *result;
result = common_test_pidfile_child(fn, 1);
return (result);
}
static const char *
test_pidfile_relative(void)
{
char path[PATH_MAX], pid[32], tmpdir[PATH_MAX];
struct pidfh *pfh;
int fd;
(void)snprintf(tmpdir, sizeof(tmpdir), "%s.XXXXXX", __func__);
if (mkdtemp(tmpdir) == NULL)
return (strerror(errno));
(void)snprintf(path, sizeof(path), "%s/pidfile", tmpdir);
pfh = pidfile_open(path, 0600, NULL);
if (pfh == NULL)
return (strerror(errno));
if (pidfile_write(pfh) != 0)
return (strerror(errno));
fd = open(path, O_RDONLY);
if (fd < 0)
return (strerror(errno));
memset(pid, 0, sizeof(pid));
if (read(fd, pid, sizeof(pid) - 1) < 0)
return (strerror(errno));
if (atoi(pid) != getpid())
return ("pid mismatch");
if (close(fd) != 0)
return (strerror(errno));
if (pidfile_close(pfh) != 0)
return (strerror(errno));
return (NULL);
}
static struct test {
const char *name;
const char *(*func)(void);
} t[] = {
{ "pidfile_uncontested", test_pidfile_uncontested },
{ "pidfile_self", test_pidfile_self },
{ "pidfile_contested", test_pidfile_contested },
{ "pidfile_inherited", test_pidfile_inherited },
{ "pidfile_relative", test_pidfile_relative },
};
int
main(void)
{
const char *result;
int i, nt;
nt = sizeof(t) / sizeof(*t);
printf("1..%d\n", nt);
for (i = 0; i < nt; ++i) {
if ((result = t[i].func()) != NULL)
printf("not ok %d - %s # %s\n", i + 1,
t[i].name, result);
else
printf("ok %d - %s\n", i + 1,
t[i].name);
}
exit(0);
}