respawn: reopen the fifo at end of line, and use read-only

Signed-off-by: Mattias Andrée <maandree@kth.se>
This commit is contained in:
Mattias Andrée 2016-09-25 12:24:17 +02:00 committed by sin
parent fa7ae96e0f
commit e3dacbb542

View File

@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/select.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
@ -7,6 +6,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <poll.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -37,9 +37,9 @@ main(int argc, char *argv[])
pid_t pid; pid_t pid;
char buf[BUFSIZ]; char buf[BUFSIZ];
int savederrno; int savederrno;
int fd;
ssize_t n; ssize_t n;
fd_set rdfd; struct pollfd pollset[1];
int polln;
ARGBEGIN { ARGBEGIN {
case 'd': case 'd':
@ -63,26 +63,33 @@ main(int argc, char *argv[])
signal(SIGTERM, sigterm); signal(SIGTERM, sigterm);
if (fifo) { if (fifo) {
/* TODO: we should use O_RDONLY and re-open the fd on EOF */ pollset->fd = open(fifo, O_RDONLY | O_NONBLOCK);
fd = open(fifo, O_RDWR | O_NONBLOCK); if (pollset->fd < 0)
if (fd < 0)
eprintf("open %s:", fifo); eprintf("open %s:", fifo);
pollset->events = POLLIN;
} }
while (1) { while (1) {
if (fifo) { if (fifo) {
FD_ZERO(&rdfd); pollset->revents = 0;
FD_SET(fd, &rdfd); polln = poll(pollset, 1, -1);
n = select(fd + 1, &rdfd, NULL, NULL, NULL); if (polln <= 0) {
if (n < 0) if (polln == 0 || errno == EAGAIN)
eprintf("select:"); continue;
if (n == 0 || FD_ISSET(fd, &rdfd) == 0) eprintf("poll:");
continue; }
while ((n = read(fd, buf, sizeof(buf))) > 0) while ((n = read(pollset->fd, buf, sizeof(buf))) > 0)
; ;
if (n < 0) if (n < 0)
if (errno != EAGAIN) if (errno != EAGAIN)
eprintf("read %s:", fifo); eprintf("read %s:", fifo);
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;
}
} }
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)