Implement fifo listener mode for respawn

This commit is contained in:
sin 2014-04-21 11:48:09 +01:00
parent 053e63968d
commit 0e6d7ca286
2 changed files with 48 additions and 5 deletions

View File

@ -2,7 +2,7 @@
.SH NAME .SH NAME
\fBrespawn\fR - Spawn the given command repeatedly \fBrespawn\fR - Spawn the given command repeatedly
.SH SYNOPSIS .SH SYNOPSIS
\fBrespawn\fR [\fB-d\fI N\fR] \fIcmd\fR [\fIargs...\fR] \fBrespawn\fR [\fB-l\fI fifo\fR] [\fB-d\fI N\fR] \fIcmd\fR [\fIargs...\fR]
.SH DESCRIPTION .SH DESCRIPTION
\fBrespawn\fR spawns the given \fIcmd\fR in a new session \fBrespawn\fR spawns the given \fIcmd\fR in a new session
repeatedly. repeatedly.
@ -10,3 +10,9 @@ repeatedly.
.TP .TP
\fB-d\fR \fB-d\fR
Set the delay between invocations of \fIcmd\fR. It defaults to 0. Set the delay between invocations of \fIcmd\fR. It defaults to 0.
.TP
\fB-l\fR
Listen on the specified \fIfifo\fR for writes. For each write
spawn a new instance of \fIcmd\fR. This can be used in conjunction
with a process supervisor to restart a particular program. The \fB-l\fR
and \fB-d\fR options are incompatible. All writes are discarded.

View File

@ -1,6 +1,11 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
@ -9,20 +14,28 @@
static void static void
usage(void) usage(void)
{ {
eprintf("usage: respawn [-d N] cmd [args...]\n"); eprintf("usage: respawn [-l fifo] [-d N] cmd [args...]\n");
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
pid_t pid; char *fifo = NULL;
int savederrno;
unsigned int delay = 0; unsigned int delay = 0;
pid_t pid;
char buf[BUFSIZ];
int savederrno;
int fd;
ssize_t n;
fd_set rdfd;
ARGBEGIN { ARGBEGIN {
case 'd': case 'd':
delay = estrtol(EARGF(usage()), 0); delay = estrtol(EARGF(usage()), 0);
break; break;
case 'l':
fifo = EARGF(usage());
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -30,7 +43,30 @@ main(int argc, char *argv[])
if(argc < 1) if(argc < 1)
usage(); usage();
if (fifo && delay > 0)
usage();
if (fifo) {
fd = open(fifo, O_RDWR | O_NONBLOCK);
if (fd < 0)
eprintf("open %s:", fifo);
}
while (1) { while (1) {
if (fifo) {
FD_ZERO(&rdfd);
FD_SET(fd, &rdfd);
n = select(fd + 1, &rdfd, NULL, NULL, NULL);
if (n < 0)
eprintf("select:");
if (n == 0 || FD_ISSET(fd, &rdfd) == 0)
continue;
while ((n = read(fd, buf, sizeof(buf))) > 0)
;
if (n < 0)
if (errno != EAGAIN)
eprintf("read %s:", fifo);
}
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
eprintf("fork:"); eprintf("fork:");
@ -47,7 +83,8 @@ main(int argc, char *argv[])
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
break; break;
} }
sleep(delay); if (!fifo)
sleep(delay);
} }
/* not reachable */ /* not reachable */
return EXIT_SUCCESS; return EXIT_SUCCESS;