#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));
}
}
{
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];
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) {
return 1;
}
snprintf(log_path, sizeof(log_path), "%s/log", dir);
memset(block, 'x', sizeof(block));
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);
if (!sp) {
} else {
usleep(200000);
n_log(
LOG_ERR,
"ex_spawn: a write to a program that has gone succeeded");
}
}
started = time(NULL);
n_log(
LOG_ERR,
"ex_spawn: a stopped piped program was not stopped");
}
n_log(
LOG_ERR,
"ex_spawn: NULL was accepted by the piped form");
}
}
#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 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.