Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_spawn.c
Go to the documentation of this file.
1
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <time.h>
27#include <unistd.h>
28
29#include "nilorea/n_common.h"
30#include "nilorea/n_log.h"
31
32int main(void) {
33 char dir[256];
34 char target[512];
35 int failures = 0;
36 int i = 0;
37
39#if defined(__windows__)
40 n_log(LOG_NOTICE, "ex_spawn: the detached start is checked on POSIX; nothing to do here");
41 return 0;
42#else
43 snprintf(dir, sizeof(dir), "%s/ex_spawn_%d", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp",
44 (int)getpid());
45 snprintf(target, sizeof(target), "%s/a file; with \"quotes\" and $HOME", dir);
46 {
47 /* The script writes "ok" into the file named by its first argument. */
48 const char* args[] = {"-c", "echo ok > \"$1\"", "ex_spawn", target, NULL};
49 char mk[300];
50
51 snprintf(mk, sizeof(mk), "mkdir -p '%s'", dir);
52 if (system(mk) != 0) {
53 n_log(LOG_ERR, "ex_spawn: cannot make %s", dir);
54 return 1;
55 }
56 if (n_spawn_detached("sh", args) != TRUE) {
57 n_log(LOG_ERR, "ex_spawn: sh could not be started");
58 failures++;
59 }
60 }
61 /* Detached: nothing to wait on, so the file is looked for. */
62 for (i = 0; i < 100 && !file_exist(target); i++)
63 usleep(20000);
64 if (file_exist(target)) {
65 n_log(LOG_NOTICE, "ex_spawn: the argument arrived whole");
66 } else {
67 n_log(LOG_ERR, "ex_spawn: no file named %s appeared", target);
68 failures++;
69 }
70 unlink(target);
71 rmdir(dir);
72
73 {
74 const char* none[] = {NULL};
75
76 /* Not found: the child reports it and exits 127; the call itself
77 * started it, so it is not an error here, and nothing is left. */
78 n_spawn_detached("ex-spawn-no-such-program", none);
79 }
80 if (n_spawn_detached(NULL, NULL) != FALSE) {
81 n_log(LOG_ERR, "ex_spawn: a NULL program was accepted");
82 failures++;
83 }
84
85 /* The waiting form: the exit code comes back, and the file the program
86 * wrote is there the moment the call returns, with no polling. */
87 {
88 const char* write_it[] = {"-c", "echo ok > \"$1\"; exit 3", "ex_spawn", target, NULL};
89 const char* none[] = {NULL};
90 char mk[300];
91 int code = 0;
92
93 snprintf(mk, sizeof(mk), "mkdir -p '%s'", dir);
94 if (system(mk) != 0) {
95 n_log(LOG_ERR, "ex_spawn: cannot make %s", dir);
96 return 1;
97 }
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);
100 failures++;
101 }
102 if (!file_exist(target)) {
103 n_log(LOG_ERR, "ex_spawn: the waited for program's file is not there on return");
104 failures++;
105 }
106 unlink(target);
107 rmdir(dir);
108 if (n_spawn_wait("true", none, &code) != TRUE || code != 0) {
109 n_log(LOG_ERR, "ex_spawn: true did not succeed (%d)", code);
110 failures++;
111 }
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);
114 failures++;
115 }
116 if (n_spawn_wait(NULL, NULL, &code) != FALSE) {
117 n_log(LOG_ERR, "ex_spawn: a NULL program was accepted by n_spawn_wait");
118 failures++;
119 }
120 }
121
122 /* The stoppable form. A flag left down waits as n_spawn_wait() does; a
123 * flag raised ends a program that would otherwise run for half a
124 * minute, and the call returns within the two seconds of grace. */
125 {
126 const char* none[] = {NULL};
127 const char* long_one[] = {"30", NULL};
128 volatile int stop = 0;
129 int code = 0;
130 time_t started = 0;
131
132 if (n_spawn_wait_cancel("true", none, &code, &stop) != TRUE || code != 0) {
133 n_log(LOG_ERR, "ex_spawn: true with a lowered flag did not succeed (%d)", code);
134 failures++;
135 }
136 stop = 1;
137 started = time(NULL);
138 if (n_spawn_wait_cancel("sleep", long_one, &code, &stop) != FALSE) {
139 n_log(LOG_ERR, "ex_spawn: a stopped program reported success");
140 failures++;
141 }
142 if (time(NULL) - started > 5) {
143 n_log(LOG_ERR, "ex_spawn: the stop took %ld seconds", (long)(time(NULL) - started));
144 failures++;
145 }
146 }
147 /* The piped form. */
148 {
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};
152 char log_path[600];
153 char mk[300];
154 static char block[1 << 20];
155 N_SPAWN_PIPE* sp = NULL;
156 FILE* in = NULL;
157 long counted = 0;
158 int code = 0;
159 time_t started = 0;
160
161 snprintf(mk, sizeof(mk), "mkdir -p '%s'", dir);
162 if (system(mk) != 0) {
163 n_log(LOG_ERR, "ex_spawn: cannot make %s", dir);
164 return 1;
165 }
166 snprintf(log_path, sizeof(log_path), "%s/log", dir);
167 memset(block, 'x', sizeof(block));
168 sp = n_spawn_pipe_open("sh", count_it, log_path);
169 if (!sp || n_spawn_pipe_write(sp, block, sizeof(block)) != TRUE ||
170 n_spawn_pipe_close(&sp, 0, &code) != TRUE || code != 0 || sp) {
171 n_log(LOG_ERR, "ex_spawn: the piped program failed (%d)", code);
172 failures++;
173 }
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));
177 failures++;
178 }
179 if (in)
180 fclose(in);
181 in = fopen(log_path, "r");
182 mk[0] = '\0';
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");
185 failures++;
186 }
187 if (in)
188 fclose(in);
189 unlink(target);
190 unlink(log_path);
191 rmdir(dir);
192
193 /* Gone before reading: the write fails, and this process lives on
194 * to say so. A megabyte is more than a pipe holds, so the write
195 * cannot succeed into the buffer alone. */
196 sp = n_spawn_pipe_open("sh", deaf, NULL);
197 if (!sp) {
198 n_log(LOG_ERR, "ex_spawn: sh could not be piped to");
199 failures++;
200 } else {
201 usleep(200000);
202 if (n_spawn_pipe_write(sp, block, sizeof(block)) != FALSE) {
203 n_log(LOG_ERR, "ex_spawn: a write to a program that has gone succeeded");
204 failures++;
205 }
206 n_spawn_pipe_close(&sp, 0, &code);
207 }
208
209 sp = n_spawn_pipe_open("sleep", long_one, NULL);
210 started = time(NULL);
211 if (!sp || n_spawn_pipe_close(&sp, 1, &code) != FALSE || time(NULL) - started > 5) {
212 n_log(LOG_ERR, "ex_spawn: a stopped piped program was not stopped");
213 failures++;
214 }
215 if (n_spawn_pipe_open(NULL, NULL, NULL) || n_spawn_pipe_close(NULL, 0, NULL) != FALSE) {
216 n_log(LOG_ERR, "ex_spawn: NULL was accepted by the piped form");
217 failures++;
218 }
219 }
220 n_log(LOG_NOTICE, "ex_spawn: %s", failures ? "FAILED" : "ok");
221 return failures ? 1 : 0;
222#endif
223}
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.