2014-04-17 13:00:36 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-04-21 10:48:09 +00:00
|
|
|
#include <sys/stat.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <sys/time.h>
|
2014-04-17 13:00:36 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-09-25 10:24:17 +00:00
|
|
|
#include <poll.h>
|
2014-07-06 20:08:19 +00:00
|
|
|
#include <signal.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-04-17 13:00:36 +00:00
|
|
|
#include <unistd.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2014-04-17 13:00:36 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-07-06 20:08:19 +00:00
|
|
|
static void
|
|
|
|
sigterm(int sig)
|
|
|
|
{
|
|
|
|
if (sig == SIGTERM) {
|
|
|
|
kill(0, SIGTERM);
|
2014-10-02 22:45:25 +00:00
|
|
|
_exit(0);
|
2014-07-06 20:08:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 13:00:36 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-06 14:19:33 +00:00
|
|
|
eprintf("usage: %s [-l fifo] [-d N] cmd [args...]\n", argv0);
|
2014-04-17 13:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-04-18 10:34:02 +00:00
|
|
|
main(int argc, char *argv[])
|
2014-04-17 13:00:36 +00:00
|
|
|
{
|
2014-04-21 10:48:09 +00:00
|
|
|
char *fifo = NULL;
|
|
|
|
unsigned int delay = 0;
|
2014-04-17 13:00:36 +00:00
|
|
|
pid_t pid;
|
2014-04-21 10:48:09 +00:00
|
|
|
char buf[BUFSIZ];
|
2014-04-17 13:00:36 +00:00
|
|
|
int savederrno;
|
2014-04-21 10:48:09 +00:00
|
|
|
ssize_t n;
|
2016-09-25 10:24:17 +00:00
|
|
|
struct pollfd pollset[1];
|
|
|
|
int polln;
|
2014-04-17 13:00:36 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'd':
|
|
|
|
delay = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
2014-04-21 10:48:09 +00:00
|
|
|
case 'l':
|
|
|
|
fifo = EARGF(usage());
|
|
|
|
break;
|
2014-04-17 13:00:36 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-07-09 15:39:32 +00:00
|
|
|
if (argc < 1)
|
2014-04-17 13:00:36 +00:00
|
|
|
usage();
|
|
|
|
|
2014-04-21 10:48:09 +00:00
|
|
|
if (fifo && delay > 0)
|
|
|
|
usage();
|
|
|
|
|
2014-07-06 20:35:35 +00:00
|
|
|
setsid();
|
|
|
|
|
2014-07-06 20:08:19 +00:00
|
|
|
signal(SIGTERM, sigterm);
|
|
|
|
|
2014-04-21 10:48:09 +00:00
|
|
|
if (fifo) {
|
2016-09-25 10:24:17 +00:00
|
|
|
pollset->fd = open(fifo, O_RDONLY | O_NONBLOCK);
|
|
|
|
if (pollset->fd < 0)
|
2014-04-21 10:48:09 +00:00
|
|
|
eprintf("open %s:", fifo);
|
2016-09-25 10:24:17 +00:00
|
|
|
pollset->events = POLLIN;
|
2014-04-21 10:48:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 13:00:36 +00:00
|
|
|
while (1) {
|
2014-04-21 10:48:09 +00:00
|
|
|
if (fifo) {
|
2016-09-25 10:24:17 +00:00
|
|
|
pollset->revents = 0;
|
|
|
|
polln = poll(pollset, 1, -1);
|
|
|
|
if (polln <= 0) {
|
|
|
|
if (polln == 0 || errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
eprintf("poll:");
|
|
|
|
}
|
|
|
|
while ((n = read(pollset->fd, buf, sizeof(buf))) > 0)
|
2014-04-21 10:48:09 +00:00
|
|
|
;
|
|
|
|
if (n < 0)
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
eprintf("read %s:", fifo);
|
2016-09-25 10:24:17 +00:00
|
|
|
if (n == 0) {
|
|
|
|
close(pollset->fd);
|
|
|
|
pollset->fd = open(fifo, O_RDONLY | O_NONBLOCK);
|
|
|
|
if (pollset->fd < 0)
|
|
|
|
eprintf("open %s:", fifo);
|
|
|
|
pollset->events = POLLIN;
|
|
|
|
}
|
2014-04-21 10:48:09 +00:00
|
|
|
}
|
2014-04-17 13:00:36 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
eprintf("fork:");
|
|
|
|
switch (pid) {
|
|
|
|
case 0:
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
savederrno = errno;
|
|
|
|
weprintf("execvp %s:", argv[0]);
|
|
|
|
_exit(savederrno == ENOENT ? 127 : 126);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
break;
|
|
|
|
}
|
2014-04-21 10:48:09 +00:00
|
|
|
if (!fifo)
|
|
|
|
sleep(delay);
|
2014-04-17 13:00:36 +00:00
|
|
|
}
|
|
|
|
/* not reachable */
|
2014-10-02 22:45:25 +00:00
|
|
|
return 0;
|
2014-04-17 13:00:36 +00:00
|
|
|
}
|