35#if defined(__windows__)
36 n_log(
LOG_NOTICE,
"ex_spawn: the detached start is checked on POSIX; nothing to do here");
39 snprintf(dir,
sizeof(dir),
"%s/ex_spawn_%d", getenv(
"TMPDIR") ? getenv(
"TMPDIR") :
"/tmp",
41 snprintf(target,
sizeof(target),
"%s/a file; with \"quotes\" and $HOME", dir);
44 const char* args[] = {
"-c",
"echo ok > \"$1\"",
"ex_spawn", target, NULL};
47 snprintf(mk,
sizeof(mk),
"mkdir -p '%s'", dir);
48 if (system(mk) != 0) {
58 for (i = 0; i < 100 && !
file_exist(target); i++)
63 n_log(
LOG_ERR,
"ex_spawn: no file named %s appeared", target);
70 const char* none[] = {NULL};
84 const char* write_it[] = {
"-c",
"echo ok > \"$1\"; exit 3",
"ex_spawn", target, NULL};
85 const char* none[] = {NULL};
89 snprintf(mk,
sizeof(mk),
"mkdir -p '%s'", dir);
90 if (system(mk) != 0) {
94 if (
n_spawn_wait(
"sh", write_it, &code) != FALSE || code != 3) {
95 n_log(
LOG_ERR,
"ex_spawn: an exit code of 3 came back as %d", code);
99 n_log(
LOG_ERR,
"ex_spawn: the waited for program's file is not there on return");
104 if (
n_spawn_wait(
"true", none, &code) != TRUE || code != 0) {
105 n_log(
LOG_ERR,
"ex_spawn: true did not succeed (%d)", code);
108 if (
n_spawn_wait(
"ex-spawn-no-such-program", none, &code) != FALSE || code != 127) {
109 n_log(
LOG_ERR,
"ex_spawn: a missing program came back as %d, not 127", code);
113 n_log(
LOG_ERR,
"ex_spawn: a NULL program was accepted by n_spawn_wait");
122 const char* none[] = {NULL};
123 const char* long_one[] = {
"30", NULL};
124 volatile int stop = 0;
129 n_log(
LOG_ERR,
"ex_spawn: true with a lowered flag did not succeed (%d)", code);
133 started = time(NULL);
135 n_log(
LOG_ERR,
"ex_spawn: a stopped program reported success");
138 if (time(NULL) - started > 5) {
139 n_log(
LOG_ERR,
"ex_spawn: the stop took %ld seconds", (
long)(time(NULL) - started));
static void stop(int sig)
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.
int n_spawn_detached(const char *program, const char *const *args)
Start a program and do not wait for it.
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.
int file_exist(const char *filename)
test if file exist and if it's readable
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_NOTICE
normal but significant condition
Common headers and low-level functions & define.