runner-unix.c 8.0 KB

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