#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
char dir[256];
char target[512];
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);
{
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) {
return 1;
}
}
}
usleep(20000);
} else {
n_log(
LOG_ERR,
"ex_spawn: no file named %s appeared", target);
}
unlink(target);
rmdir(dir);
{
const char* none[] = {NULL};
}
}
{
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) {
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);
}
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);
}
n_log(
LOG_ERR,
"ex_spawn: a NULL program was accepted by n_spawn_wait");
}
}
{
const char* none[] = {NULL};
const char* long_one[] = {"30", NULL};
int code = 0;
time_t started = 0;
n_log(
LOG_ERR,
"ex_spawn: true with a lowered flag did not succeed (%d)", code);
}
started = time(NULL);
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));
}
}
#endif
}
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.