aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_os_start_process.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_os_start_process.c')
-rw-r--r--src/util/test_os_start_process.c283
1 files changed, 145 insertions, 138 deletions
diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c
index abf00a09e..fbcfa6729 100644
--- a/src/util/test_os_start_process.c
+++ b/src/util/test_os_start_process.c
@@ -46,9 +46,10 @@ static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
46 */ 46 */
47static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout; 47static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
48 48
49static struct GNUNET_SCHEDULER_Task * die_task; 49static struct GNUNET_SCHEDULER_Task *die_task;
50 50
51struct read_context { 51struct read_context
52{
52 char buf[16]; 53 char buf[16];
53 int buf_offset; 54 int buf_offset;
54 const struct GNUNET_DISK_FileHandle *stdout_read_handle; 55 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
@@ -59,122 +60,124 @@ static struct read_context rc;
59 60
60 61
61static void 62static void
62end_task(void *cls) 63end_task (void *cls)
63{ 64{
64 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) 65 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
65 { 66 {
66 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 67 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
67 } 68 }
68 GNUNET_assert(GNUNET_OK == GNUNET_OS_process_wait(proc)); 69 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
69 GNUNET_OS_process_destroy(proc); 70 GNUNET_OS_process_destroy (proc);
70 proc = NULL; 71 proc = NULL;
71 GNUNET_DISK_pipe_close(hello_pipe_stdout); 72 GNUNET_DISK_pipe_close (hello_pipe_stdout);
72 GNUNET_DISK_pipe_close(hello_pipe_stdin); 73 GNUNET_DISK_pipe_close (hello_pipe_stdin);
73} 74}
74 75
75 76
76static void 77static void
77read_call(void *cls) 78read_call (void *cls)
78{ 79{
79 int bytes; 80 int bytes;
80 81
81 bytes = GNUNET_DISK_file_read(rc.stdout_read_handle, 82 bytes = GNUNET_DISK_file_read (rc.stdout_read_handle,
82 &rc.buf[rc.buf_offset], 83 &rc.buf[rc.buf_offset],
83 sizeof(rc.buf) - rc.buf_offset); 84 sizeof(rc.buf) - rc.buf_offset);
84 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "bytes is %d\n", 86 "bytes is %d\n",
86 bytes); 87 bytes);
87 88
88 if (bytes < 1) 89 if (bytes < 1)
89 { 90 {
90 GNUNET_break(0); 91 GNUNET_break (0);
91 ok = 1; 92 ok = 1;
92 GNUNET_SCHEDULER_cancel(die_task); 93 GNUNET_SCHEDULER_cancel (die_task);
93 (void)GNUNET_SCHEDULER_add_now(&end_task, NULL); 94 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
94 return; 95 return;
95 } 96 }
96 97
97 ok = strncmp(rc.buf, test_phrase, strlen(test_phrase)); 98 ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
98 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 "read %s\n", 100 "read %s\n",
100 &rc.buf[rc.buf_offset]); 101 &rc.buf[rc.buf_offset]);
101 rc.buf_offset += bytes; 102 rc.buf_offset += bytes;
102 103
103 if (0 == ok) 104 if (0 == ok)
104 { 105 {
105 GNUNET_SCHEDULER_cancel(die_task); 106 GNUNET_SCHEDULER_cancel (die_task);
106 (void)GNUNET_SCHEDULER_add_now(&end_task, NULL); 107 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
107 return; 108 return;
108 } 109 }
109 110
110 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 111 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
111 rc.stdout_read_handle, 112 rc.stdout_read_handle,
112 &read_call, 113 &read_call,
113 NULL); 114 NULL);
114} 115}
115 116
116 117
117static void 118static void
118run_task(void *cls) 119run_task (void *cls)
119{ 120{
120 char *fn; 121 char *fn;
121 const struct GNUNET_DISK_FileHandle *stdout_read_handle; 122 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
122 const struct GNUNET_DISK_FileHandle *wh; 123 const struct GNUNET_DISK_FileHandle *wh;
123 124
124 GNUNET_asprintf(&fn, "cat"); 125 GNUNET_asprintf (&fn, "cat");
125 126
126 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 127 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
127 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 128 GNUNET_NO);
129 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
130 GNUNET_YES);
128 131
129 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) 132 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
130 { 133 {
131 GNUNET_break(0); 134 GNUNET_break (0);
132 ok = 1; 135 ok = 1;
133 GNUNET_free(fn); 136 GNUNET_free (fn);
134 return; 137 return;
135 } 138 }
136 139
137 proc = 140 proc =
138 GNUNET_OS_start_process(GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, 141 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
139 hello_pipe_stdin, hello_pipe_stdout, NULL, 142 hello_pipe_stdin, hello_pipe_stdout, NULL,
140 fn, 143 fn,
141 "test_gnunet_echo_hello", "-", NULL); 144 "test_gnunet_echo_hello", "-", NULL);
142 GNUNET_free(fn); 145 GNUNET_free (fn);
143 146
144 /* Close the write end of the read pipe */ 147 /* Close the write end of the read pipe */
145 GNUNET_DISK_pipe_close_end(hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); 148 GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
146 /* Close the read end of the write pipe */ 149 /* Close the read end of the write pipe */
147 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ); 150 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
148 151
149 wh = GNUNET_DISK_pipe_handle(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); 152 wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
150 153
151 /* Write the test_phrase to the cat process */ 154 /* Write the test_phrase to the cat process */
152 if (GNUNET_DISK_file_write(wh, test_phrase, strlen(test_phrase) + 1) != 155 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
153 strlen(test_phrase) + 1) 156 strlen (test_phrase) + 1)
154 { 157 {
155 GNUNET_break(0); 158 GNUNET_break (0);
156 ok = 1; 159 ok = 1;
157 return; 160 return;
158 } 161 }
159 162
160 /* Close the write end to end the cycle! */ 163 /* Close the write end to end the cycle! */
161 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); 164 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
162 165
163 stdout_read_handle = 166 stdout_read_handle =
164 GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ); 167 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
165 168
166 die_task = 169 die_task =
167 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply 170 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
168 (GNUNET_TIME_UNIT_MINUTES, 1), 171 (GNUNET_TIME_UNIT_MINUTES, 1),
169 &end_task, 172 &end_task,
170 NULL); 173 NULL);
171 174
172 memset(&rc, 0, sizeof(rc)); 175 memset (&rc, 0, sizeof(rc));
173 rc.stdout_read_handle = stdout_read_handle; 176 rc.stdout_read_handle = stdout_read_handle;
174 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 177 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
175 stdout_read_handle, 178 stdout_read_handle,
176 &read_call, 179 &read_call,
177 NULL); 180 NULL);
178} 181}
179 182
180 183
@@ -183,10 +186,10 @@ run_task(void *cls)
183 * checks that "ok" is correct at the end. 186 * checks that "ok" is correct at the end.
184 */ 187 */
185static int 188static int
186check_run() 189check_run ()
187{ 190{
188 ok = 1; 191 ok = 1;
189 GNUNET_SCHEDULER_run(&run_task, &ok); 192 GNUNET_SCHEDULER_run (&run_task, &ok);
190 return ok; 193 return ok;
191} 194}
192 195
@@ -195,40 +198,42 @@ check_run()
195 * Test killing via pipe. 198 * Test killing via pipe.
196 */ 199 */
197static int 200static int
198check_kill() 201check_kill ()
199{ 202{
200 char *fn; 203 char *fn;
201 204
202 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 205 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
203 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 206 GNUNET_NO);
207 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
208 GNUNET_YES);
204 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) 209 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
205 { 210 {
206 return 1; 211 return 1;
207 } 212 }
208 fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver"); 213 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
209 proc = 214 proc =
210 GNUNET_OS_start_process(GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, 215 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
211 hello_pipe_stdin, 216 hello_pipe_stdin,
212 hello_pipe_stdout, 217 hello_pipe_stdout,
213 NULL, 218 NULL,
214 fn, 219 fn,
215 "gnunet-service-resolver", "-", 220 "gnunet-service-resolver", "-",
216 NULL); 221 NULL);
217 if (NULL == proc) 222 if (NULL == proc)
218 { 223 {
219 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n"); 225 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
221 return 77; 226 return 77;
222 } 227 }
223 sleep(1); /* give process time to start, so we actually use the pipe-kill mechanism! */ 228 sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
224 GNUNET_free(fn); 229 GNUNET_free (fn);
225 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) 230 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
226 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 231 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
227 GNUNET_assert(GNUNET_OK == GNUNET_OS_process_wait(proc)); 232 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
228 GNUNET_OS_process_destroy(proc); 233 GNUNET_OS_process_destroy (proc);
229 proc = NULL; 234 proc = NULL;
230 GNUNET_DISK_pipe_close(hello_pipe_stdout); 235 GNUNET_DISK_pipe_close (hello_pipe_stdout);
231 GNUNET_DISK_pipe_close(hello_pipe_stdin); 236 GNUNET_DISK_pipe_close (hello_pipe_stdin);
232 return 0; 237 return 0;
233} 238}
234 239
@@ -237,55 +242,57 @@ check_kill()
237 * Test killing via pipe. 242 * Test killing via pipe.
238 */ 243 */
239static int 244static int
240check_instant_kill() 245check_instant_kill ()
241{ 246{
242 char *fn; 247 char *fn;
243 248
244 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); 249 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES,
245 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 250 GNUNET_NO);
251 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO,
252 GNUNET_YES);
246 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) 253 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
247 { 254 {
248 return 1; 255 return 1;
249 } 256 }
250 fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver"); 257 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
251 proc = 258 proc =
252 GNUNET_OS_start_process(GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, 259 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
253 hello_pipe_stdin, hello_pipe_stdout, NULL, 260 hello_pipe_stdin, hello_pipe_stdout, NULL,
254 fn, 261 fn,
255 "gnunet-service-resolver", "-", NULL); 262 "gnunet-service-resolver", "-", NULL);
256 if (NULL == proc) 263 if (NULL == proc)
257 { 264 {
258 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n"); 266 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
260 return 77; 267 return 77;
261 } 268 }
262 if (0 != GNUNET_OS_process_kill(proc, 269 if (0 != GNUNET_OS_process_kill (proc,
263 GNUNET_TERM_SIG)) 270 GNUNET_TERM_SIG))
264 { 271 {
265 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); 272 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
266 } 273 }
267 GNUNET_free(fn); 274 GNUNET_free (fn);
268 GNUNET_assert(GNUNET_OK == GNUNET_OS_process_wait(proc)); 275 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
269 GNUNET_OS_process_destroy(proc); 276 GNUNET_OS_process_destroy (proc);
270 proc = NULL; 277 proc = NULL;
271 GNUNET_DISK_pipe_close(hello_pipe_stdout); 278 GNUNET_DISK_pipe_close (hello_pipe_stdout);
272 GNUNET_DISK_pipe_close(hello_pipe_stdin); 279 GNUNET_DISK_pipe_close (hello_pipe_stdin);
273 return 0; 280 return 0;
274} 281}
275 282
276 283
277int 284int
278main(int argc, char *argv[]) 285main (int argc, char *argv[])
279{ 286{
280 int ret; 287 int ret;
281 288
282 GNUNET_log_setup("test-os-start-process", 289 GNUNET_log_setup ("test-os-start-process",
283 "WARNING", 290 "WARNING",
284 NULL); 291 NULL);
285 ret = 0; 292 ret = 0;
286 ret |= check_run(); 293 ret |= check_run ();
287 ret |= check_kill(); 294 ret |= check_kill ();
288 ret |= check_instant_kill(); 295 ret |= check_instant_kill ();
289 return ret; 296 return ret;
290} 297}
291 298