Changeset View
Changeset View
Standalone View
Standalone View
head/lib/libutil/tests/pidfile_test.c
Show All 24 Lines | |||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/wait.h> | #include <sys/wait.h> | ||||
#include <sys/event.h> | |||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <signal.h> | #include <signal.h> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <libutil.h> | #include <libutil.h> | ||||
/* | /* | ||||
* We need a signal handler so kill(2) will interrupt our child's | * We need a signal handler so kill(2) will interrupt the child | ||||
* select(2) instead of killing it. | * instead of killing it. | ||||
*/ | */ | ||||
static void | static void | ||||
signal_handler(int sig) | signal_handler(int sig) | ||||
{ | { | ||||
(void)sig; | (void)sig; | ||||
} | } | ||||
/* | /* | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | |||||
* Common code for test_pidfile_{contested,inherited}. | * Common code for test_pidfile_{contested,inherited}. | ||||
*/ | */ | ||||
static const char * | static const char * | ||||
common_test_pidfile_child(const char *fn, int parent_open) | common_test_pidfile_child(const char *fn, int parent_open) | ||||
{ | { | ||||
struct pidfh *pf = NULL; | struct pidfh *pf = NULL; | ||||
pid_t other = 0, pid = 0; | pid_t other = 0, pid = 0; | ||||
int fd[2], serrno, status; | int fd[2], serrno, status; | ||||
struct kevent event, ke; | |||||
char ch; | char ch; | ||||
int kq; | |||||
unlink(fn); | unlink(fn); | ||||
if (pipe(fd) != 0) | if (pipe(fd) != 0) | ||||
return (strerror(errno)); | return (strerror(errno)); | ||||
if (parent_open) { | if (parent_open) { | ||||
pf = pidfile_open(fn, 0600, &other); | pf = pidfile_open(fn, 0600, &other); | ||||
if (pf == NULL && other != 0) | if (pf == NULL && other != 0) | ||||
Show All 20 Lines | if (pidfile_write(pf) != 0) { | ||||
serrno = errno; | serrno = errno; | ||||
pidfile_close(pf); | pidfile_close(pf); | ||||
unlink(fn); | unlink(fn); | ||||
return (strerror(serrno)); | return (strerror(serrno)); | ||||
} | } | ||||
if (pf == NULL) | if (pf == NULL) | ||||
_exit(1); | _exit(1); | ||||
if (pidfile_write(pf) != 0) | if (pidfile_write(pf) != 0) | ||||
_exit(1); | _exit(2); | ||||
kq = kqueue(); | |||||
if (kq == -1) | |||||
_exit(3); | |||||
EV_SET(&ke, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); | |||||
/* Attach event to the kqueue. */ | |||||
if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0) | |||||
_exit(4); | |||||
/* Inform the parent we are ready to receive SIGINT */ | |||||
if (write(fd[1], "*", 1) != 1) | if (write(fd[1], "*", 1) != 1) | ||||
_exit(1); | _exit(5); | ||||
select(0, 0, 0, 0, 0); | /* Wait for SIGINT received */ | ||||
if (kevent(kq, NULL, 0, &event, 1, NULL) != 1) | |||||
_exit(6); | |||||
_exit(0); | _exit(0); | ||||
} | } | ||||
// parent | // parent | ||||
close(fd[1]); | close(fd[1]); | ||||
if (pf) | if (pf) | ||||
pidfile_close(pf); | pidfile_close(pf); | ||||
// wait for the child to signal us | // wait for the child to signal us | ||||
▲ Show 20 Lines • Show All 100 Lines • Show Last 20 Lines |