Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_spawn.c

start a program detached or waited for, with arguments no shell touches

start a program detached or waited for, with arguments no shell touches

Author
Castagnier Mickael
Version
1.0
Date
24/09/2026

Starts a small shell script with n_spawn_detached(), passing it a file name full of the characters a shell would take apart - spaces, quotes, a semicolon, a dollar - and checks that the script received it whole: the script writes its first argument into a file of that very name, which exists afterwards only if the argument arrived as one piece. Also checks that a program that does not exist is not fatal, and that a NULL program is refused. Then the same through n_spawn_wait(): the exit code comes back and the file is there on return, a missing program is 127. Then n_spawn_wait_cancel(): a raised flag ends a long program within seconds. Last, n_spawn_pipe_open(): a megabyte written to a program's input arrives whole, what it says lands in the log file, a program that stops reading fails the write instead of ending this one with SIGPIPE, and a stop ends a program that would run for half a minute.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "nilorea/n_log.h"
int main(void) {
char dir[256];
char target[512];
int failures = 0;
int i = 0;
#if defined(__windows__)
n_log(LOG_NOTICE, "ex_spawn: the detached start is checked on POSIX; nothing to do here");
return 0;
#else
snprintf(dir, sizeof(dir), "%s/ex_spawn_%d", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp",
(int)getpid());
snprintf(target, sizeof(target), "%s/a file; with \"quotes\" and $HOME", dir);
{
/* The script writes "ok" into the file named by its first argument. */
const char* args[] = {"-c", "echo ok > \"$1\"", "ex_spawn", target, NULL};
char mk[300];
snprintf(mk, sizeof(mk), "mkdir -p '%s'", dir);
if (system(mk) != 0) {
n_log(LOG_ERR, "ex_spawn: cannot make %s", dir);
return 1;
}
if (n_spawn_detached("sh", args) != TRUE) {
n_log(LOG_ERR, "ex_spawn: sh could not be started");
}
}
/* Detached: nothing to wait on, so the file is looked for. */
for (i = 0; i < 100 && !file_exist(target); i++)
usleep(20000);
if (file_exist(target)) {
n_log(LOG_NOTICE, "ex_spawn: the argument arrived whole");
} else {
n_log(LOG_ERR, "ex_spawn: no file named %s appeared", target);
}
unlink(target);
rmdir(dir);
{
const char* none[] = {NULL};
/* Not found: the child reports it and exits 127; the call itself
* started it, so it is not an error here, and nothing is left. */
n_spawn_detached("ex-spawn-no-such-program", none);
}
if (n_spawn_detached(NULL, NULL) != FALSE) {
n_log(LOG_ERR, "ex_spawn: a NULL program was accepted");
}
/* The waiting form: the exit code comes back, and the file the program
* wrote is there the moment the call returns, with no polling. */
{
const char* write_it[] = {"-c", "echo ok > \"$1\"; exit 3", "ex_spawn", target, NULL};
const char* none[] = {NULL};
char mk[300];
int code = 0;
snprintf(mk, sizeof(mk), "mkdir -p '%s'", dir);
if (system(mk) != 0) {
n_log(LOG_ERR, "ex_spawn: cannot make %s", dir);
return 1;
}
if (n_spawn_wait("sh", write_it, &code) != FALSE || code != 3) {
n_log(LOG_ERR, "ex_spawn: an exit code of 3 came back as %d", code);
}
if (!file_exist(target)) {
n_log(LOG_ERR, "ex_spawn: the waited for program's file is not there on return");
}
unlink(target);
rmdir(dir);
if (n_spawn_wait("true", none, &code) != TRUE || code != 0) {
n_log(LOG_ERR, "ex_spawn: true did not succeed (%d)", code);
}
if (n_spawn_wait("ex-spawn-no-such-program", none, &code) != FALSE || code != 127) {
n_log(LOG_ERR, "ex_spawn: a missing program came back as %d, not 127", code);
}
if (n_spawn_wait(NULL, NULL, &code) != FALSE) {
n_log(LOG_ERR, "ex_spawn: a NULL program was accepted by n_spawn_wait");
}
}
/* The stoppable form. A flag left down waits as n_spawn_wait() does; a
* flag raised ends a program that would otherwise run for half a
* minute, and the call returns within the two seconds of grace. */
{
const char* none[] = {NULL};
const char* long_one[] = {"30", NULL};
volatile int stop = 0;
int code = 0;
time_t started = 0;
if (n_spawn_wait_cancel("true", none, &code, &stop) != TRUE || code != 0) {
n_log(LOG_ERR, "ex_spawn: true with a lowered flag did not succeed (%d)", code);
}
stop = 1;
started = time(NULL);
if (n_spawn_wait_cancel("sleep", long_one, &code, &stop) != FALSE) {
n_log(LOG_ERR, "ex_spawn: a stopped program reported success");
}
if (time(NULL) - started > 5) {
n_log(LOG_ERR, "ex_spawn: the stop took %ld seconds", (long)(time(NULL) - started));
}
}
/* The piped form. */
{
const char* count_it[] = {"-c", "wc -c > \"$1\"; echo said >&2", "ex_spawn", target, NULL};
const char* deaf[] = {"-c", "exit 0", NULL};
const char* long_one[] = {"30", NULL};
char log_path[600];
char mk[300];
static char block[1 << 20];
N_SPAWN_PIPE* sp = NULL;
FILE* in = NULL;
long counted = 0;
int code = 0;
time_t started = 0;
snprintf(mk, sizeof(mk), "mkdir -p '%s'", dir);
if (system(mk) != 0) {
n_log(LOG_ERR, "ex_spawn: cannot make %s", dir);
return 1;
}
snprintf(log_path, sizeof(log_path), "%s/log", dir);
memset(block, 'x', sizeof(block));
sp = n_spawn_pipe_open("sh", count_it, log_path);
if (!sp || n_spawn_pipe_write(sp, block, sizeof(block)) != TRUE ||
n_spawn_pipe_close(&sp, 0, &code) != TRUE || code != 0 || sp) {
n_log(LOG_ERR, "ex_spawn: the piped program failed (%d)", code);
}
in = fopen(target, "r");
if (!in || fscanf(in, "%ld", &counted) != 1 || counted != (long)sizeof(block)) {
n_log(LOG_ERR, "ex_spawn: %ld bytes arrived of %zu", counted, sizeof(block));
}
if (in)
fclose(in);
in = fopen(log_path, "r");
mk[0] = '\0';
if (!in || !fgets(mk, sizeof(mk), in) || strncmp(mk, "said", 4) != 0) {
n_log(LOG_ERR, "ex_spawn: the program's words did not reach the log");
}
if (in)
fclose(in);
unlink(target);
unlink(log_path);
rmdir(dir);
/* Gone before reading: the write fails, and this process lives on
* to say so. A megabyte is more than a pipe holds, so the write
* cannot succeed into the buffer alone. */
sp = n_spawn_pipe_open("sh", deaf, NULL);
if (!sp) {
n_log(LOG_ERR, "ex_spawn: sh could not be piped to");
} else {
usleep(200000);
if (n_spawn_pipe_write(sp, block, sizeof(block)) != FALSE) {
n_log(LOG_ERR, "ex_spawn: a write to a program that has gone succeeded");
}
n_spawn_pipe_close(&sp, 0, &code);
}
sp = n_spawn_pipe_open("sleep", long_one, NULL);
started = time(NULL);
if (!sp || n_spawn_pipe_close(&sp, 1, &code) != FALSE || time(NULL) - started > 5) {
n_log(LOG_ERR, "ex_spawn: a stopped piped program was not stopped");
}
if (n_spawn_pipe_open(NULL, NULL, NULL) || n_spawn_pipe_close(NULL, 0, NULL) != FALSE) {
n_log(LOG_ERR, "ex_spawn: NULL was accepted by the piped form");
}
}
n_log(LOG_NOTICE, "ex_spawn: %s", failures ? "FAILED" : "ok");
return failures ? 1 : 0;
#endif
}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void stop(int sig)
Definition ex_kafka.c:75
int n_spawn_wait(const char *program, const char *const *args, int *exit_code)
Run a program with arguments and wait for it to finish.
Definition n_common.c:370
int n_spawn_detached(const char *program, const char *const *args)
Start a program and do not wait for it.
Definition n_common.c:293
int n_spawn_wait_cancel(const char *program, const char *const *args, int *exit_code, const volatile int *cancel)
Run a program and wait for it, ending it early when asked.
Definition n_common.c:374
int n_spawn_pipe_close(N_SPAWN_PIPE **pipe_ptr, int stop, int *exit_code)
End a program's input, or end the program, and wait for it.
Definition n_common.c:716
N_SPAWN_PIPE * n_spawn_pipe_open(const char *program, const char *const *args, const char *log_path)
Start a program whose standard input is fed by the caller.
Definition n_common.c:525
int n_spawn_pipe_write(N_SPAWN_PIPE *sp, const void *data, size_t len)
Write to a program's standard input, all of it.
Definition n_common.c:655
int file_exist(const char *filename)
test if file exist and if it's readable
Definition n_common.c:105
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
A program started with its standard input on a pipe the caller writes to (n_spawn_pipe_open()).
Definition n_common.c:509
Common headers and low-level functions & define.
Generic log system.