39#if defined(__windows__)
40 n_log(
LOG_NOTICE,
"ex_spawn: the detached start is checked on POSIX; nothing to do here");
43 snprintf(dir,
sizeof(dir),
"%s/ex_spawn_%d", getenv(
"TMPDIR") ? getenv(
"TMPDIR") :
"/tmp",
45 snprintf(target,
sizeof(target),
"%s/a file; with \"quotes\" and $HOME", dir);
48 const char* args[] = {
"-c",
"echo ok > \"$1\"",
"ex_spawn", target, NULL};
51 snprintf(mk,
sizeof(mk),
"mkdir -p '%s'", dir);
52 if (system(mk) != 0) {
62 for (i = 0; i < 100 && !
file_exist(target); i++)
67 n_log(
LOG_ERR,
"ex_spawn: no file named %s appeared", target);
74 const char* none[] = {NULL};
88 const char* write_it[] = {
"-c",
"echo ok > \"$1\"; exit 3",
"ex_spawn", target, NULL};
89 const char* none[] = {NULL};
93 snprintf(mk,
sizeof(mk),
"mkdir -p '%s'", dir);
94 if (system(mk) != 0) {
98 if (
n_spawn_wait(
"sh", write_it, &code) != FALSE || code != 3) {
99 n_log(
LOG_ERR,
"ex_spawn: an exit code of 3 came back as %d", code);
103 n_log(
LOG_ERR,
"ex_spawn: the waited for program's file is not there on return");
108 if (
n_spawn_wait(
"true", none, &code) != TRUE || code != 0) {
109 n_log(
LOG_ERR,
"ex_spawn: true did not succeed (%d)", code);
112 if (
n_spawn_wait(
"ex-spawn-no-such-program", none, &code) != FALSE || code != 127) {
113 n_log(
LOG_ERR,
"ex_spawn: a missing program came back as %d, not 127", code);
117 n_log(
LOG_ERR,
"ex_spawn: a NULL program was accepted by n_spawn_wait");
126 const char* none[] = {NULL};
127 const char* long_one[] = {
"30", NULL};
128 volatile int stop = 0;
133 n_log(
LOG_ERR,
"ex_spawn: true with a lowered flag did not succeed (%d)", code);
137 started = time(NULL);
139 n_log(
LOG_ERR,
"ex_spawn: a stopped program reported success");
142 if (time(NULL) - started > 5) {
143 n_log(
LOG_ERR,
"ex_spawn: the stop took %ld seconds", (
long)(time(NULL) - started));
149 const char* count_it[] = {
"-c",
"wc -c > \"$1\"; echo said >&2",
"ex_spawn", target, NULL};
150 const char* deaf[] = {
"-c",
"exit 0", NULL};
151 const char* long_one[] = {
"30", NULL};
154 static char block[1 << 20];
161 snprintf(mk,
sizeof(mk),
"mkdir -p '%s'", dir);
162 if (system(mk) != 0) {
166 snprintf(log_path,
sizeof(log_path),
"%s/log", dir);
167 memset(block,
'x',
sizeof(block));
171 n_log(
LOG_ERR,
"ex_spawn: the piped program failed (%d)", code);
174 in = fopen(target,
"r");
175 if (!in || fscanf(in,
"%ld", &counted) != 1 || counted != (
long)
sizeof(block)) {
176 n_log(
LOG_ERR,
"ex_spawn: %ld bytes arrived of %zu", counted,
sizeof(block));
181 in = fopen(log_path,
"r");
183 if (!in || !fgets(mk,
sizeof(mk), in) || strncmp(mk,
"said", 4) != 0) {
184 n_log(
LOG_ERR,
"ex_spawn: the program's words did not reach the log");
203 n_log(
LOG_ERR,
"ex_spawn: a write to a program that has gone succeeded");
210 started = time(NULL);
212 n_log(
LOG_ERR,
"ex_spawn: a stopped piped program was not stopped");
216 n_log(
LOG_ERR,
"ex_spawn: NULL was accepted by the piped form");
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 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.
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.
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.
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
A program started with its standard input on a pipe the caller writes to (n_spawn_pipe_open()).
Common headers and low-level functions & define.