ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit c4ff95798cd66d7c36789e58db83fc51d46442fc
parent e8b61e9b7ee779ddec9372585c6d3f379cef3ebe
Author: sin <sin@2f30.org>
Date:   Thu, 17 Apr 2014 14:00:36 +0100

Add respawn

Diffstat:
MMakefile | 2++
Arespawn.1 | 12++++++++++++
Arespawn.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -40,6 +40,7 @@ SRC = \ pidof.c \ pivot_root.c \ ps.c \ + respawn.c \ rmmod.c \ stat.c \ su.c \ @@ -67,6 +68,7 @@ MAN1 = \ pagesize.1 \ pidof.1 \ ps.1 \ + respawn.1 \ stat.1 \ su.1 \ truncate.1 \ diff --git a/respawn.1 b/respawn.1 @@ -0,0 +1,12 @@ +.TH RESPAWN 1 ubase-VERSION +.SH NAME +\fBrespawn\fR - Spawn the given command repeatedly +.SH SYNOPSIS +\fBrespawn\fR [\fB-d\fI N\fR] \fIcmd\fR [\fIargs...\fR] +.SH DESCRIPTION +\fBrespawn\fR spawns the given \fIcmd\fR in a new session +repeatedly. +.SH OPTIONS +.TP +\fB-d\fR +Set the delay between invocations of \fIcmd\fR. It defaults to 0. diff --git a/respawn.c b/respawn.c @@ -0,0 +1,54 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: respawn [-d N] cmd [args...]\n"); +} + +int +main(int argc, char **argv) +{ + pid_t pid; + int savederrno; + unsigned int delay = 0; + + ARGBEGIN { + case 'd': + delay = estrtol(EARGF(usage()), 0); + break; + default: + usage(); + } ARGEND; + + if(argc < 1) + usage(); + + while (1) { + pid = fork(); + if (pid < 0) + eprintf("fork:"); + switch (pid) { + case 0: + if (setsid() < 0) + eprintf("setsid:"); + execvp(argv[0], argv); + savederrno = errno; + weprintf("execvp %s:", argv[0]); + _exit(savederrno == ENOENT ? 127 : 126); + break; + default: + waitpid(pid, NULL, 0); + break; + } + sleep(delay); + } + /* not reachable */ + return EXIT_SUCCESS; +}