async.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "ccanlint.h"
  2. #include "../tools.h"
  3. #include <sys/types.h>
  4. #include <sys/time.h>
  5. #include <sys/resource.h>
  6. #include <sys/wait.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <signal.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <err.h>
  13. #include <assert.h>
  14. #include <ccan/lbalance/lbalance.h>
  15. #include <ccan/tlist/tlist.h>
  16. #include <ccan/time/time.h>
  17. static struct lbalance *lb;
  18. TLIST_TYPE(command, struct command);
  19. static struct tlist_command pending = TLIST_INIT(pending);
  20. static struct tlist_command running = TLIST_INIT(running);
  21. static unsigned int num_running = 0;
  22. static struct tlist_command done = TLIST_INIT(done);
  23. struct command {
  24. struct list_node list;
  25. char *command;
  26. pid_t pid;
  27. int output_fd;
  28. unsigned int time_ms;
  29. struct lbalance_task *task;
  30. int status;
  31. char *output;
  32. bool done;
  33. const void *ctx;
  34. };
  35. static void killme(int sig UNNEEDED)
  36. {
  37. kill(-getpid(), SIGKILL);
  38. }
  39. static void run_more(void)
  40. {
  41. struct command *c;
  42. while (num_running < lbalance_target(lb)) {
  43. int p[2];
  44. c = tlist_top(&pending, list);
  45. if (!c)
  46. break;
  47. fflush(stdout);
  48. if (pipe(p) != 0)
  49. err(1, "Pipe failed");
  50. c->pid = fork();
  51. if (c->pid == -1)
  52. err(1, "Fork failed");
  53. if (c->pid == 0) {
  54. struct itimerval itim;
  55. if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
  56. || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
  57. || close(p[0]) != 0
  58. || close(STDIN_FILENO) != 0
  59. || open("/dev/null", O_RDONLY) != STDIN_FILENO)
  60. exit(128);
  61. signal(SIGALRM, killme);
  62. itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
  63. itim.it_value = timespec_to_timeval(time_from_msec(c->time_ms).ts);
  64. setitimer(ITIMER_REAL, &itim, NULL);
  65. c->status = system(c->command);
  66. if (WIFEXITED(c->status))
  67. exit(WEXITSTATUS(c->status));
  68. /* Here's a hint... */
  69. exit(128 + WTERMSIG(c->status));
  70. }
  71. if (tools_verbose)
  72. printf("Running async: %s => %i\n", c->command, c->pid);
  73. close(p[1]);
  74. c->output_fd = p[0];
  75. c->task = lbalance_task_new(lb);
  76. tlist_del_from(&pending, c, list);
  77. tlist_add_tail(&running, c, list);
  78. num_running++;
  79. }
  80. }
  81. static void destroy_command(struct command *command)
  82. {
  83. if (!command->done && command->pid) {
  84. kill(-command->pid, SIGKILL);
  85. close(command->output_fd);
  86. num_running--;
  87. }
  88. tlist_del(command, list);
  89. }
  90. void run_command_async(const void *ctx, unsigned int time_ms,
  91. const char *fmt, ...)
  92. {
  93. struct command *command;
  94. va_list ap;
  95. assert(ctx);
  96. if (!lb)
  97. lb = lbalance_new();
  98. command = tal(ctx, struct command);
  99. command->ctx = ctx;
  100. command->time_ms = time_ms;
  101. command->pid = 0;
  102. /* We want to track length, so don't use tal_strdup */
  103. command->output = tal_arrz(command, char, 1);
  104. va_start(ap, fmt);
  105. command->command = tal_vfmt(command, fmt, ap);
  106. va_end(ap);
  107. tlist_add_tail(&pending, command, list);
  108. command->done = false;
  109. tal_add_destructor(command, destroy_command);
  110. run_more();
  111. }
  112. static void reap_output(void)
  113. {
  114. fd_set in;
  115. struct command *c, *next;
  116. int max_fd = 0;
  117. FD_ZERO(&in);
  118. tlist_for_each(&running, c, list) {
  119. FD_SET(c->output_fd, &in);
  120. if (c->output_fd > max_fd)
  121. max_fd = c->output_fd;
  122. }
  123. if (select(max_fd+1, &in, NULL, NULL, NULL) < 0)
  124. err(1, "select failed");
  125. tlist_for_each_safe(&running, c, next, list) {
  126. if (FD_ISSET(c->output_fd, &in)) {
  127. int old_len, len;
  128. /* This length includes nul terminator! */
  129. old_len = tal_count(c->output);
  130. tal_resize(&c->output, old_len + 1024);
  131. len = read(c->output_fd, c->output + old_len - 1, 1024);
  132. if (len < 0)
  133. err(1, "Reading from async command");
  134. tal_resize(&c->output, old_len + len);
  135. c->output[old_len + len - 1] = '\0';
  136. if (len == 0) {
  137. struct rusage ru;
  138. wait4(c->pid, &c->status, 0, &ru);
  139. if (tools_verbose)
  140. printf("Finished async %i: %s %u\n",
  141. c->pid,
  142. WIFEXITED(c->status)
  143. ? "exit status"
  144. : "killed by signal",
  145. WIFEXITED(c->status)
  146. ? WEXITSTATUS(c->status)
  147. : WTERMSIG(c->status));
  148. lbalance_task_free(c->task, &ru);
  149. c->task = NULL;
  150. c->done = true;
  151. close(c->output_fd);
  152. tlist_del_from(&running, c, list);
  153. tlist_add_tail(&done, c, list);
  154. num_running--;
  155. }
  156. }
  157. }
  158. }
  159. void *collect_command(bool *ok, char **output)
  160. {
  161. struct command *c;
  162. const void *ctx;
  163. while ((c = tlist_top(&done, list)) == NULL) {
  164. if (tlist_empty(&pending) && tlist_empty(&running))
  165. return NULL;
  166. reap_output();
  167. run_more();
  168. }
  169. *ok = (WIFEXITED(c->status) && WEXITSTATUS(c->status) == 0);
  170. ctx = c->ctx;
  171. *output = tal_steal(ctx, c->output);
  172. tal_free(c);
  173. return (void *)ctx;
  174. }
  175. /* Compile and link single C file, with object files, async. */
  176. void compile_and_link_async(const void *ctx, unsigned int time_ms,
  177. const char *cfile, const char *ccandir,
  178. const char *objs, const char *compiler,
  179. const char *cflags,
  180. const char *libs, const char *outfile)
  181. {
  182. if (compile_verbose)
  183. printf("Compiling and linking (async) %s\n", outfile);
  184. run_command_async(ctx, time_ms,
  185. "%s %s -I%s -o %s %s %s %s",
  186. compiler, cflags,
  187. ccandir, outfile, cfile, objs, libs);
  188. }