Skip to content

Commit

Permalink
Close and reopen fifo on POLLHUP
Browse files Browse the repository at this point in the history
This fixes the 100% CPU load issue seen in Infix systems when stopping
containers.  We now close and reopen the fifo when the writer end closes
its descriptor.

Also, simplify the code by using open() instead of fopen() + fcntl().

Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Apr 15, 2024
1 parent 2265ece commit 7e37ff9
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int main(int argc, char *argv[])
{
char msg[1024] = { 0 }, buf[512], *fn, *pidfn = NULL;
int create = 0, daemonize = 1, facility = LOG_USER;
int partial, fd, flags, c, opts = LOG_PID;
int partial, fd, c, opts = LOG_PID;
struct pollfd pfd;
FILE *fp;

Expand Down Expand Up @@ -157,17 +157,20 @@ int main(int argc, char *argv[])
if (pidfile(pidfn))
logit(LOG_ERR, "failed creating pidfile: %s", strerror(errno));

fp = fopen(fn, "r");
if (!fp) {
reopen:
logit(LOG_INFO, "opening fifo %s", fn);
fd = open(fn, O_RDONLY | O_NONBLOCK);
if (fd == -1) {
logit(LOG_ERR, "failed opening %sd: %s", fn, strerror(errno));
err(1, "failed opening %s", fn);
}

fd = fileno(fp);
flags = fcntl(fd, F_GETFL, 0);
if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
logit(LOG_ERR, "failed setting pipe fd non-blocking");
err(1, "failed setting pipe fd non-blocking");
fp = fdopen(fd, "r");
if (!fp) {
logit(LOG_ERR, "failed opening fifo stream: %s", strerror(errno));
close(fd);
log_close();
exit(1);
}

pfd.fd = fd;
Expand All @@ -179,6 +182,11 @@ int main(int argc, char *argv[])
int priority = LOG_NOTICE;
char *ptr;

if (pfd.revents & POLLHUP) {
fclose(fp);
goto reopen;
}

if (!fgets(buf, sizeof(buf), fp))
continue;
ptr = chomp(buf);
Expand Down

0 comments on commit 7e37ff9

Please sign in to comment.