runner-unix.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. #include "runner-unix.h"
  22. #include "runner.h"
  23. #include <limits.h>
  24. #include <stdint.h> /* uintptr_t */
  25. #include <errno.h>
  26. #include <unistd.h> /* usleep */
  27. #include <string.h> /* strdup */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <signal.h>
  32. #include <sys/wait.h>
  33. #include <sys/stat.h>
  34. #include <assert.h>
  35. #include <sys/select.h>
  36. #include <sys/time.h>
  37. #include <pthread.h>
  38. extern char** environ;
  39. static void closefd(int fd) {
  40. if (close(fd) == 0 || errno == EINTR || errno == EINPROGRESS)
  41. return;
  42. perror("close");
  43. abort();
  44. }
  45. void notify_parent_process(void) {
  46. char* arg;
  47. int fd;
  48. arg = getenv("UV_TEST_RUNNER_FD");
  49. if (arg == NULL)
  50. return;
  51. fd = atoi(arg);
  52. assert(fd > STDERR_FILENO);
  53. unsetenv("UV_TEST_RUNNER_FD");
  54. closefd(fd);
  55. }
  56. /* Do platform-specific initialization. */
  57. void platform_init(int argc, char **argv) {
  58. /* Disable stdio output buffering. */
  59. setvbuf(stdout, NULL, _IONBF, 0);
  60. setvbuf(stderr, NULL, _IONBF, 0);
  61. signal(SIGPIPE, SIG_IGN);
  62. snprintf(executable_path, sizeof(executable_path), "%s", argv[0]);
  63. }
  64. /* Invoke "argv[0] test-name [test-part]". Store process info in *p. Make sure
  65. * that all stdio output of the processes is buffered up. */
  66. int process_start(char* name, char* part, process_info_t* p, int is_helper) {
  67. FILE* stdout_file;
  68. int stdout_fd;
  69. const char* arg;
  70. char* args[16];
  71. int pipefd[2];
  72. char fdstr[8];
  73. ssize_t rc;
  74. int n;
  75. pid_t pid;
  76. arg = getenv("UV_USE_VALGRIND");
  77. n = 0;
  78. /* Disable valgrind for helpers, it complains about helpers leaking memory.
  79. * They're killed after the test and as such never get a chance to clean up.
  80. */
  81. if (is_helper == 0 && arg != NULL && atoi(arg) != 0) {
  82. args[n++] = "valgrind";
  83. args[n++] = "--quiet";
  84. args[n++] = "--leak-check=full";
  85. args[n++] = "--show-reachable=yes";
  86. args[n++] = "--error-exitcode=125";
  87. }
  88. args[n++] = executable_path;
  89. args[n++] = name;
  90. args[n++] = part;
  91. args[n++] = NULL;
  92. stdout_file = tmpfile();
  93. stdout_fd = fileno(stdout_file);
  94. if (!stdout_file) {
  95. perror("tmpfile");
  96. return -1;
  97. }
  98. if (is_helper) {
  99. if (pipe(pipefd)) {
  100. perror("pipe");
  101. return -1;
  102. }
  103. snprintf(fdstr, sizeof(fdstr), "%d", pipefd[1]);
  104. if (setenv("UV_TEST_RUNNER_FD", fdstr, /* overwrite */ 1)) {
  105. perror("setenv");
  106. return -1;
  107. }
  108. }
  109. p->terminated = 0;
  110. p->status = 0;
  111. pid = fork();
  112. if (pid < 0) {
  113. perror("fork");
  114. return -1;
  115. }
  116. if (pid == 0) {
  117. /* child */
  118. if (is_helper)
  119. closefd(pipefd[0]);
  120. dup2(stdout_fd, STDOUT_FILENO);
  121. dup2(stdout_fd, STDERR_FILENO);
  122. execve(args[0], args, environ);
  123. perror("execve()");
  124. _exit(127);
  125. }
  126. /* parent */
  127. p->pid = pid;
  128. p->name = strdup(name);
  129. p->stdout_file = stdout_file;
  130. if (!is_helper)
  131. return 0;
  132. closefd(pipefd[1]);
  133. unsetenv("UV_TEST_RUNNER_FD");
  134. do
  135. rc = read(pipefd[0], &n, 1);
  136. while (rc == -1 && errno == EINTR);
  137. closefd(pipefd[0]);
  138. if (rc == -1) {
  139. perror("read");
  140. return -1;
  141. }
  142. if (rc > 0) {
  143. fprintf(stderr, "EOF expected but got data.\n");
  144. return -1;
  145. }
  146. return 0;
  147. }
  148. typedef struct {
  149. int pipe[2];
  150. process_info_t* vec;
  151. int n;
  152. } dowait_args;
  153. /* This function is run inside a pthread. We do this so that we can possibly
  154. * timeout.
  155. */
  156. static void* dowait(void* data) {
  157. dowait_args* args = data;
  158. int i, r;
  159. process_info_t* p;
  160. for (i = 0; i < args->n; i++) {
  161. p = (process_info_t*)(args->vec + i * sizeof(process_info_t));
  162. if (p->terminated) continue;
  163. r = waitpid(p->pid, &p->status, 0);
  164. if (r < 0) {
  165. perror("waitpid");
  166. return NULL;
  167. }
  168. p->terminated = 1;
  169. }
  170. if (args->pipe[1] >= 0) {
  171. /* Write a character to the main thread to notify it about this. */
  172. ssize_t r;
  173. do
  174. r = write(args->pipe[1], "", 1);
  175. while (r == -1 && errno == EINTR);
  176. }
  177. return NULL;
  178. }
  179. /* Wait for all `n` processes in `vec` to terminate. Time out after `timeout`
  180. * msec, or never if timeout == -1. Return 0 if all processes are terminated,
  181. * -1 on error, -2 on timeout. */
  182. int process_wait(process_info_t* vec, int n, int timeout) {
  183. int i;
  184. int r;
  185. int retval;
  186. process_info_t* p;
  187. dowait_args args;
  188. pthread_t tid;
  189. pthread_attr_t attr;
  190. unsigned int elapsed_ms;
  191. struct timeval timebase;
  192. struct timeval tv;
  193. fd_set fds;
  194. args.vec = vec;
  195. args.n = n;
  196. args.pipe[0] = -1;
  197. args.pipe[1] = -1;
  198. /* The simple case is where there is no timeout */
  199. if (timeout == -1) {
  200. dowait(&args);
  201. return 0;
  202. }
  203. /* Hard case. Do the wait with a timeout.
  204. *
  205. * Assumption: we are the only ones making this call right now. Otherwise
  206. * we'd need to lock vec.
  207. */
  208. r = pipe((int*)&(args.pipe));
  209. if (r) {
  210. perror("pipe()");
  211. return -1;
  212. }
  213. if (pthread_attr_init(&attr))
  214. abort();
  215. #if defined(__MVS__)
  216. if (pthread_attr_setstacksize(&attr, 1024 * 1024))
  217. #else
  218. if (pthread_attr_setstacksize(&attr, 256 * 1024))
  219. #endif
  220. abort();
  221. r = pthread_create(&tid, &attr, dowait, &args);
  222. if (pthread_attr_destroy(&attr))
  223. abort();
  224. if (r) {
  225. perror("pthread_create()");
  226. retval = -1;
  227. goto terminate;
  228. }
  229. if (gettimeofday(&timebase, NULL))
  230. abort();
  231. tv = timebase;
  232. for (;;) {
  233. /* Check that gettimeofday() doesn't jump back in time. */
  234. assert(tv.tv_sec > timebase.tv_sec ||
  235. (tv.tv_sec == timebase.tv_sec && tv.tv_usec >= timebase.tv_usec));
  236. elapsed_ms =
  237. (tv.tv_sec - timebase.tv_sec) * 1000 +
  238. (tv.tv_usec / 1000) -
  239. (timebase.tv_usec / 1000);
  240. r = 0; /* Timeout. */
  241. if (elapsed_ms >= (unsigned) timeout)
  242. break;
  243. tv.tv_sec = (timeout - elapsed_ms) / 1000;
  244. tv.tv_usec = (timeout - elapsed_ms) % 1000 * 1000;
  245. FD_ZERO(&fds);
  246. FD_SET(args.pipe[0], &fds);
  247. r = select(args.pipe[0] + 1, &fds, NULL, NULL, &tv);
  248. if (!(r == -1 && errno == EINTR))
  249. break;
  250. if (gettimeofday(&tv, NULL))
  251. abort();
  252. }
  253. if (r == -1) {
  254. perror("select()");
  255. retval = -1;
  256. } else if (r) {
  257. /* The thread completed successfully. */
  258. retval = 0;
  259. } else {
  260. /* Timeout. Kill all the children. */
  261. for (i = 0; i < n; i++) {
  262. p = (process_info_t*)(vec + i * sizeof(process_info_t));
  263. kill(p->pid, SIGTERM);
  264. }
  265. retval = -2;
  266. }
  267. if (pthread_join(tid, NULL))
  268. abort();
  269. terminate:
  270. close(args.pipe[0]);
  271. close(args.pipe[1]);
  272. return retval;
  273. }
  274. /* Returns the number of bytes in the stdio output buffer for process `p`. */
  275. long int process_output_size(process_info_t *p) {
  276. /* Size of the p->stdout_file */
  277. struct stat buf;
  278. int r = fstat(fileno(p->stdout_file), &buf);
  279. if (r < 0) {
  280. return -1;
  281. }
  282. return (long)buf.st_size;
  283. }
  284. /* Copy the contents of the stdio output buffer to `fd`. */
  285. int process_copy_output(process_info_t* p, FILE* stream) {
  286. char buf[1024];
  287. int r;
  288. r = fseek(p->stdout_file, 0, SEEK_SET);
  289. if (r < 0) {
  290. perror("fseek");
  291. return -1;
  292. }
  293. /* TODO: what if the line is longer than buf */
  294. while ((r = fread(buf, 1, sizeof(buf), p->stdout_file)) != 0)
  295. print_lines(buf, r, stream);
  296. if (ferror(p->stdout_file)) {
  297. perror("read");
  298. return -1;
  299. }
  300. return 0;
  301. }
  302. /* Copy the last line of the stdio output buffer to `buffer` */
  303. int process_read_last_line(process_info_t *p,
  304. char* buffer,
  305. size_t buffer_len) {
  306. char* ptr;
  307. int r = fseek(p->stdout_file, 0, SEEK_SET);
  308. if (r < 0) {
  309. perror("fseek");
  310. return -1;
  311. }
  312. buffer[0] = '\0';
  313. while (fgets(buffer, buffer_len, p->stdout_file) != NULL) {
  314. for (ptr = buffer; *ptr && *ptr != '\r' && *ptr != '\n'; ptr++)
  315. ;
  316. *ptr = '\0';
  317. }
  318. if (ferror(p->stdout_file)) {
  319. perror("read");
  320. buffer[0] = '\0';
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. /* Return the name that was specified when `p` was started by process_start */
  326. char* process_get_name(process_info_t *p) {
  327. return p->name;
  328. }
  329. /* Terminate process `p`. */
  330. int process_terminate(process_info_t *p) {
  331. return kill(p->pid, SIGTERM);
  332. }
  333. /* Return the exit code of process p. On error, return -1. */
  334. int process_reap(process_info_t *p) {
  335. if (WIFEXITED(p->status)) {
  336. return WEXITSTATUS(p->status);
  337. } else {
  338. return p->status; /* ? */
  339. }
  340. }
  341. /* Clean up after terminating process `p` (e.g. free the output buffer etc.). */
  342. void process_cleanup(process_info_t *p) {
  343. fclose(p->stdout_file);
  344. free(p->name);
  345. }
  346. /* Move the console cursor one line up and back to the first column. */
  347. void rewind_cursor(void) {
  348. #if defined(__MVS__)
  349. fprintf(stderr, "\047[2K\r");
  350. #else
  351. fprintf(stderr, "\033[2K\r");
  352. #endif
  353. }