[hackers] [ubase] Implement fifo listener mode for respawn || sin

From: <git_AT_suckless.org>
Date: Tue, 22 Apr 2014 17:40:15 +0200

commit 0e6d7ca2864634a9aecf7c03e40e1fcd046f826b
Author: sin <sin_AT_2f30.org>
Date: Mon Apr 21 11:48:09 2014 +0100

    Implement fifo listener mode for respawn

diff --git a/respawn.1 b/respawn.1
index f9d68a1..05ad86b 100644
--- a/respawn.1
+++ b/respawn.1
_AT_@ -2,7 +2,7 @@
 .SH NAME
  Brespawn R - Spawn the given command repeatedly
 .SH SYNOPSIS
- Brespawn R [ B-d I N R] Icmd R [ Iargs... R]
+ Brespawn R [ B-l I fifo R] [ B-d I N R] Icmd R [ Iargs... R]
 .SH DESCRIPTION
  Brespawn R spawns the given Icmd R in a new session
 repeatedly.
_AT_@ -10,3 +10,9 @@ repeatedly.
 .TP
  B-d R
 Set the delay between invocations of Icmd R. It defaults to 0.
+.TP
+ B-l R
+Listen on the specified Ififo R for writes. For each write
+spawn a new instance of Icmd R. This can be used in conjunction
+with a process supervisor to restart a particular program. The B-l R
+and B-d R options are incompatible. All writes are discarded.
diff --git a/respawn.c b/respawn.c
index 453c534..4f65e04 100644
--- a/respawn.c
+++ b/respawn.c
_AT_@ -1,6 +1,11 @@
 /* See LICENSE file for copyright and license details. */
+#include <fcntl.h>
 #include <errno.h>
+#include <stdio.h>
 #include <stdlib.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
_AT_@ -9,20 +14,28 @@
 static void
 usage(void)
 {
- eprintf("usage: respawn [-d N] cmd [args...]
");
+ eprintf("usage: respawn [-l fifo] [-d N] cmd [args...]
");
 }
 
 int
 main(int argc, char *argv[])
 {
+ char *fifo = NULL;
+ unsigned int delay = 0;
         pid_t pid;
+ char buf[BUFSIZ];
         int savederrno;
- unsigned int delay = 0;
+ int fd;
+ ssize_t n;
+ fd_set rdfd;
 
         ARGBEGIN {
         case 'd':
                 delay = estrtol(EARGF(usage()), 0);
                 break;
+ case 'l':
+ fifo = EARGF(usage());
+ break;
         default:
                 usage();
         } ARGEND;
_AT_@ -30,7 +43,30 @@ main(int argc, char *argv[])
         if(argc < 1)
                 usage();
 
+ if (fifo && delay > 0)
+ usage();
+
+ if (fifo) {
+ fd = open(fifo, O_RDWR | O_NONBLOCK);
+ if (fd < 0)
+ eprintf("open %s:", fifo);
+ }
+
         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();
                 if (pid < 0)
                         eprintf("fork:");
_AT_@ -47,7 +83,8 @@ main(int argc, char *argv[])
                         waitpid(pid, NULL, 0);
                         break;
                 }
- sleep(delay);
+ if (!fifo)
+ sleep(delay);
         }
         /* not reachable */
         return EXIT_SUCCESS;
Received on Tue Apr 22 2014 - 17:40:15 CEST

This archive was generated by hypermail 2.3.0 : Tue Apr 22 2014 - 17:48:07 CEST