runbg: new helper function inspired by the Infix project

The runbg() function is a delayed execvp() that has double-forked to the
background (to not create zombies).  It is useful for running commands
in the background for APIs that need to return a reply, e.g. a NETCONF
RPC to reboot the system.

Compared to the original in Infix, this version double forks to reparent
itself to PID 1 (init), closes all open files, including stdio.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-09-17 11:00:09 +02:00
parent 921afdecb4
commit 92bb0ca399
7 changed files with 135 additions and 3 deletions
+13
View File
@@ -249,6 +249,19 @@ Generic Functions
`delete`, it is now called `opt`. The APIs are 100% compatible if the
value `1` was used.
- `runbg(cmd, delay)`
Run a command in the background, after delay microseconds, essentially
a background `execvp()` with a delay. The function employs double-fork
to prevent zombies and closes all open files, including stdio.
```C
char *cmd[] = { "sh", "-c", "echo 'hej' >/tmp/foo", NULL};
runbg(cmd, 200000); /* Create /tmp/foo after 200 millisec */
/* Execution continues here immediately */
```
- `strmatch(str, list)`, `strnmatch(str, list, len)`
Find matching string in an array of strings. Returns index in array on