async.c 5.0 KB

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