1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. |
2 | * |
3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
4 | * of this software and associated documentation files (the "Software"), to |
5 | * deal in the Software without restriction, including without limitation the |
6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
7 | * sell copies of the Software, and to permit persons to whom the Software is |
8 | * furnished to do so, subject to the following conditions: |
9 | * |
10 | * The above copyright notice and this permission notice shall be included in |
11 | * all copies or substantial portions of the Software. |
12 | * |
13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
19 | * IN THE SOFTWARE. |
20 | */ |
21 | |
22 | #include "runner-unix.h" |
23 | #include "runner.h" |
24 | |
25 | #include <limits.h> |
26 | #include <stdint.h> /* uintptr_t */ |
27 | |
28 | #include <errno.h> |
29 | #include <unistd.h> /* readlink, usleep */ |
30 | #include <string.h> /* strdup */ |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
33 | #include <sys/types.h> |
34 | #include <signal.h> |
35 | #include <sys/wait.h> |
36 | #include <sys/stat.h> |
37 | #include <assert.h> |
38 | |
39 | #include <sys/select.h> |
40 | #include <sys/time.h> |
41 | #include <pthread.h> |
42 | |
43 | extern char** environ; |
44 | |
45 | static void closefd(int fd) { |
46 | if (close(fd) == 0 || errno == EINTR || errno == EINPROGRESS) |
47 | return; |
48 | |
49 | perror("close" ); |
50 | abort(); |
51 | } |
52 | |
53 | |
54 | void notify_parent_process(void) { |
55 | char* arg; |
56 | int fd; |
57 | |
58 | arg = getenv("UV_TEST_RUNNER_FD" ); |
59 | if (arg == NULL) |
60 | return; |
61 | |
62 | fd = atoi(arg); |
63 | assert(fd > STDERR_FILENO); |
64 | unsetenv("UV_TEST_RUNNER_FD" ); |
65 | closefd(fd); |
66 | } |
67 | |
68 | |
69 | /* Do platform-specific initialization. */ |
70 | int platform_init(int argc, char **argv) { |
71 | /* Disable stdio output buffering. */ |
72 | setvbuf(stdout, NULL, _IONBF, 0); |
73 | setvbuf(stderr, NULL, _IONBF, 0); |
74 | signal(SIGPIPE, SIG_IGN); |
75 | |
76 | if (realpath(argv[0], executable_path) == NULL) { |
77 | perror("realpath" ); |
78 | return -1; |
79 | } |
80 | |
81 | return 0; |
82 | } |
83 | |
84 | |
85 | /* Invoke "argv[0] test-name [test-part]". Store process info in *p. Make sure |
86 | * that all stdio output of the processes is buffered up. */ |
87 | int process_start(char* name, char* part, process_info_t* p, int is_helper) { |
88 | FILE* stdout_file; |
89 | int stdout_fd; |
90 | const char* arg; |
91 | char* args[16]; |
92 | int pipefd[2]; |
93 | char fdstr[8]; |
94 | ssize_t rc; |
95 | int n; |
96 | pid_t pid; |
97 | |
98 | arg = getenv("UV_USE_VALGRIND" ); |
99 | n = 0; |
100 | |
101 | /* Disable valgrind for helpers, it complains about helpers leaking memory. |
102 | * They're killed after the test and as such never get a chance to clean up. |
103 | */ |
104 | if (is_helper == 0 && arg != NULL && atoi(arg) != 0) { |
105 | args[n++] = "valgrind" ; |
106 | args[n++] = "--quiet" ; |
107 | args[n++] = "--leak-check=full" ; |
108 | args[n++] = "--show-reachable=yes" ; |
109 | args[n++] = "--error-exitcode=125" ; |
110 | } |
111 | |
112 | args[n++] = executable_path; |
113 | args[n++] = name; |
114 | args[n++] = part; |
115 | args[n++] = NULL; |
116 | |
117 | stdout_file = tmpfile(); |
118 | stdout_fd = fileno(stdout_file); |
119 | if (!stdout_file) { |
120 | perror("tmpfile" ); |
121 | return -1; |
122 | } |
123 | |
124 | if (is_helper) { |
125 | if (pipe(pipefd)) { |
126 | perror("pipe" ); |
127 | return -1; |
128 | } |
129 | |
130 | snprintf(fdstr, sizeof(fdstr), "%d" , pipefd[1]); |
131 | if (setenv("UV_TEST_RUNNER_FD" , fdstr, /* overwrite */ 1)) { |
132 | perror("setenv" ); |
133 | return -1; |
134 | } |
135 | } |
136 | |
137 | p->terminated = 0; |
138 | p->status = 0; |
139 | |
140 | pid = fork(); |
141 | |
142 | if (pid < 0) { |
143 | perror("fork" ); |
144 | return -1; |
145 | } |
146 | |
147 | if (pid == 0) { |
148 | /* child */ |
149 | if (is_helper) |
150 | closefd(pipefd[0]); |
151 | dup2(stdout_fd, STDOUT_FILENO); |
152 | dup2(stdout_fd, STDERR_FILENO); |
153 | execve(args[0], args, environ); |
154 | perror("execve()" ); |
155 | _exit(127); |
156 | } |
157 | |
158 | /* parent */ |
159 | p->pid = pid; |
160 | p->name = strdup(name); |
161 | p->stdout_file = stdout_file; |
162 | |
163 | if (!is_helper) |
164 | return 0; |
165 | |
166 | closefd(pipefd[1]); |
167 | unsetenv("UV_TEST_RUNNER_FD" ); |
168 | |
169 | do |
170 | rc = read(pipefd[0], &n, 1); |
171 | while (rc == -1 && errno == EINTR); |
172 | |
173 | closefd(pipefd[0]); |
174 | |
175 | if (rc == -1) { |
176 | perror("read" ); |
177 | return -1; |
178 | } |
179 | |
180 | if (rc > 0) { |
181 | fprintf(stderr, "EOF expected but got data.\n" ); |
182 | return -1; |
183 | } |
184 | |
185 | return 0; |
186 | } |
187 | |
188 | |
189 | typedef struct { |
190 | int pipe[2]; |
191 | process_info_t* vec; |
192 | int n; |
193 | } dowait_args; |
194 | |
195 | |
196 | /* This function is run inside a pthread. We do this so that we can possibly |
197 | * timeout. |
198 | */ |
199 | static void* dowait(void* data) { |
200 | dowait_args* args = data; |
201 | |
202 | int i, r; |
203 | process_info_t* p; |
204 | |
205 | for (i = 0; i < args->n; i++) { |
206 | p = (process_info_t*)(args->vec + i * sizeof(process_info_t)); |
207 | if (p->terminated) continue; |
208 | r = waitpid(p->pid, &p->status, 0); |
209 | if (r < 0) { |
210 | perror("waitpid" ); |
211 | return NULL; |
212 | } |
213 | p->terminated = 1; |
214 | } |
215 | |
216 | if (args->pipe[1] >= 0) { |
217 | /* Write a character to the main thread to notify it about this. */ |
218 | ssize_t r; |
219 | |
220 | do |
221 | r = write(args->pipe[1], "" , 1); |
222 | while (r == -1 && errno == EINTR); |
223 | } |
224 | |
225 | return NULL; |
226 | } |
227 | |
228 | |
229 | /* Wait for all `n` processes in `vec` to terminate. Time out after `timeout` |
230 | * msec, or never if timeout == -1. Return 0 if all processes are terminated, |
231 | * -1 on error, -2 on timeout. */ |
232 | int process_wait(process_info_t* vec, int n, int timeout) { |
233 | int i; |
234 | int r; |
235 | int retval; |
236 | process_info_t* p; |
237 | dowait_args args; |
238 | pthread_t tid; |
239 | pthread_attr_t attr; |
240 | unsigned int elapsed_ms; |
241 | struct timeval timebase; |
242 | struct timeval tv; |
243 | fd_set fds; |
244 | |
245 | args.vec = vec; |
246 | args.n = n; |
247 | args.pipe[0] = -1; |
248 | args.pipe[1] = -1; |
249 | |
250 | /* The simple case is where there is no timeout */ |
251 | if (timeout == -1) { |
252 | dowait(&args); |
253 | return 0; |
254 | } |
255 | |
256 | /* Hard case. Do the wait with a timeout. |
257 | * |
258 | * Assumption: we are the only ones making this call right now. Otherwise |
259 | * we'd need to lock vec. |
260 | */ |
261 | |
262 | r = pipe((int*)&(args.pipe)); |
263 | if (r) { |
264 | perror("pipe()" ); |
265 | return -1; |
266 | } |
267 | |
268 | if (pthread_attr_init(&attr)) |
269 | abort(); |
270 | |
271 | #if defined(__MVS__) |
272 | if (pthread_attr_setstacksize(&attr, 1024 * 1024)) |
273 | #else |
274 | if (pthread_attr_setstacksize(&attr, 256 * 1024)) |
275 | #endif |
276 | abort(); |
277 | |
278 | r = pthread_create(&tid, &attr, dowait, &args); |
279 | |
280 | if (pthread_attr_destroy(&attr)) |
281 | abort(); |
282 | |
283 | if (r) { |
284 | perror("pthread_create()" ); |
285 | retval = -1; |
286 | goto terminate; |
287 | } |
288 | |
289 | if (gettimeofday(&timebase, NULL)) |
290 | abort(); |
291 | |
292 | tv = timebase; |
293 | for (;;) { |
294 | /* Check that gettimeofday() doesn't jump back in time. */ |
295 | assert(tv.tv_sec > timebase.tv_sec || |
296 | (tv.tv_sec == timebase.tv_sec && tv.tv_usec >= timebase.tv_usec)); |
297 | |
298 | elapsed_ms = |
299 | (tv.tv_sec - timebase.tv_sec) * 1000 + |
300 | (tv.tv_usec / 1000) - |
301 | (timebase.tv_usec / 1000); |
302 | |
303 | r = 0; /* Timeout. */ |
304 | if (elapsed_ms >= (unsigned) timeout) |
305 | break; |
306 | |
307 | tv.tv_sec = (timeout - elapsed_ms) / 1000; |
308 | tv.tv_usec = (timeout - elapsed_ms) % 1000 * 1000; |
309 | |
310 | FD_ZERO(&fds); |
311 | FD_SET(args.pipe[0], &fds); |
312 | |
313 | r = select(args.pipe[0] + 1, &fds, NULL, NULL, &tv); |
314 | if (!(r == -1 && errno == EINTR)) |
315 | break; |
316 | |
317 | if (gettimeofday(&tv, NULL)) |
318 | abort(); |
319 | } |
320 | |
321 | if (r == -1) { |
322 | perror("select()" ); |
323 | retval = -1; |
324 | |
325 | } else if (r) { |
326 | /* The thread completed successfully. */ |
327 | retval = 0; |
328 | |
329 | } else { |
330 | /* Timeout. Kill all the children. */ |
331 | for (i = 0; i < n; i++) { |
332 | p = (process_info_t*)(vec + i * sizeof(process_info_t)); |
333 | kill(p->pid, SIGTERM); |
334 | } |
335 | retval = -2; |
336 | } |
337 | |
338 | if (pthread_join(tid, NULL)) |
339 | abort(); |
340 | |
341 | terminate: |
342 | close(args.pipe[0]); |
343 | close(args.pipe[1]); |
344 | return retval; |
345 | } |
346 | |
347 | |
348 | /* Returns the number of bytes in the stdio output buffer for process `p`. */ |
349 | long int process_output_size(process_info_t *p) { |
350 | /* Size of the p->stdout_file */ |
351 | struct stat buf; |
352 | |
353 | int r = fstat(fileno(p->stdout_file), &buf); |
354 | if (r < 0) { |
355 | return -1; |
356 | } |
357 | |
358 | return (long)buf.st_size; |
359 | } |
360 | |
361 | |
362 | /* Copy the contents of the stdio output buffer to `fd`. */ |
363 | int process_copy_output(process_info_t* p, FILE* stream) { |
364 | char buf[1024]; |
365 | int r; |
366 | |
367 | r = fseek(p->stdout_file, 0, SEEK_SET); |
368 | if (r < 0) { |
369 | perror("fseek" ); |
370 | return -1; |
371 | } |
372 | |
373 | /* TODO: what if the line is longer than buf */ |
374 | while (fgets(buf, sizeof(buf), p->stdout_file) != NULL) |
375 | print_lines(buf, strlen(buf), stream); |
376 | |
377 | if (ferror(p->stdout_file)) { |
378 | perror("read" ); |
379 | return -1; |
380 | } |
381 | |
382 | return 0; |
383 | } |
384 | |
385 | |
386 | /* Copy the last line of the stdio output buffer to `buffer` */ |
387 | int process_read_last_line(process_info_t *p, |
388 | char* buffer, |
389 | size_t buffer_len) { |
390 | char* ptr; |
391 | |
392 | int r = fseek(p->stdout_file, 0, SEEK_SET); |
393 | if (r < 0) { |
394 | perror("fseek" ); |
395 | return -1; |
396 | } |
397 | |
398 | buffer[0] = '\0'; |
399 | |
400 | while (fgets(buffer, buffer_len, p->stdout_file) != NULL) { |
401 | for (ptr = buffer; *ptr && *ptr != '\r' && *ptr != '\n'; ptr++); |
402 | *ptr = '\0'; |
403 | } |
404 | |
405 | if (ferror(p->stdout_file)) { |
406 | perror("read" ); |
407 | buffer[0] = '\0'; |
408 | return -1; |
409 | } |
410 | return 0; |
411 | } |
412 | |
413 | |
414 | /* Return the name that was specified when `p` was started by process_start */ |
415 | char* process_get_name(process_info_t *p) { |
416 | return p->name; |
417 | } |
418 | |
419 | |
420 | /* Terminate process `p`. */ |
421 | int process_terminate(process_info_t *p) { |
422 | return kill(p->pid, SIGTERM); |
423 | } |
424 | |
425 | |
426 | /* Return the exit code of process p. On error, return -1. */ |
427 | int process_reap(process_info_t *p) { |
428 | if (WIFEXITED(p->status)) { |
429 | return WEXITSTATUS(p->status); |
430 | } else { |
431 | return p->status; /* ? */ |
432 | } |
433 | } |
434 | |
435 | |
436 | /* Clean up after terminating process `p` (e.g. free the output buffer etc.). */ |
437 | void process_cleanup(process_info_t *p) { |
438 | fclose(p->stdout_file); |
439 | free(p->name); |
440 | } |
441 | |
442 | |
443 | /* Move the console cursor one line up and back to the first column. */ |
444 | void rewind_cursor(void) { |
445 | #if defined(__MVS__) |
446 | fprintf(stderr, "\047[2K\r" ); |
447 | #else |
448 | fprintf(stderr, "\033[2K\r" ); |
449 | #endif |
450 | } |
451 | |
452 | |
453 | /* Pause the calling thread for a number of milliseconds. */ |
454 | void uv_sleep(int msec) { |
455 | int sec; |
456 | int usec; |
457 | |
458 | sec = msec / 1000; |
459 | usec = (msec % 1000) * 1000; |
460 | if (sec > 0) |
461 | sleep(sec); |
462 | if (usec > 0) |
463 | usleep(usec); |
464 | } |
465 | |