5 #include <sys/resource.h>
13 #include <ccan/lbalance/lbalance.h>
14 #include <ccan/tlist/tlist.h>
15 #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);
25 struct list_node list;
30 struct lbalance_task *task;
37 static void killme(int sig)
39 kill(-getpid(), SIGKILL);
42 static void run_more(void)
46 while (num_running < lbalance_target(lb)) {
49 c = tlist_top(&pending, list);
55 err(1, "Pipe failed");
58 err(1, "Fork failed");
60 struct itimerval itim;
62 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
63 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
65 || close(STDIN_FILENO) != 0
66 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
69 signal(SIGALRM, killme);
70 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
71 itim.it_value = timespec_to_timeval(time_from_msec(c->time_ms).ts);
72 setitimer(ITIMER_REAL, &itim, NULL);
74 c->status = system(c->command);
75 if (WIFEXITED(c->status))
76 exit(WEXITSTATUS(c->status));
77 /* Here's a hint... */
78 exit(128 + WTERMSIG(c->status));
82 printf("Running async: %s => %i\n", c->command, c->pid);
86 c->task = lbalance_task_new(lb);
87 tlist_del_from(&pending, c, list);
88 tlist_add_tail(&running, c, list);
93 static void destroy_command(struct command *command)
95 if (!command->done && command->pid) {
96 kill(-command->pid, SIGKILL);
97 close(command->output_fd);
101 tlist_del(command, list);
104 void run_command_async(const void *ctx, unsigned int time_ms,
105 const char *fmt, ...)
107 struct command *command;
115 command = tal(ctx, struct command);
117 command->time_ms = time_ms;
119 /* We want to track length, so don't use tal_strdup */
120 command->output = tal_arrz(command, char, 1);
122 command->command = tal_vfmt(command, fmt, ap);
124 tlist_add_tail(&pending, command, list);
125 command->done = false;
126 tal_add_destructor(command, destroy_command);
131 static void reap_output(void)
134 struct command *c, *next;
139 tlist_for_each(&running, c, list) {
140 FD_SET(c->output_fd, &in);
141 if (c->output_fd > max_fd)
142 max_fd = c->output_fd;
145 if (select(max_fd+1, &in, NULL, NULL, NULL) < 0)
146 err(1, "select failed");
148 tlist_for_each_safe(&running, c, next, list) {
149 if (FD_ISSET(c->output_fd, &in)) {
151 /* This length includes nul terminator! */
152 old_len = tal_count(c->output);
153 tal_resize(&c->output, old_len + 1024);
154 len = read(c->output_fd, c->output + old_len - 1, 1024);
156 err(1, "Reading from async command");
157 tal_resize(&c->output, old_len + len);
158 c->output[old_len + len - 1] = '\0';
161 wait4(c->pid, &c->status, 0, &ru);
163 printf("Finished async %i: %s %u\n",
167 : "killed by signal",
169 ? WEXITSTATUS(c->status)
170 : WTERMSIG(c->status));
171 lbalance_task_free(c->task, &ru);
175 tlist_del_from(&running, c, list);
176 tlist_add_tail(&done, c, list);
183 void *collect_command(bool *ok, char **output)
188 while ((c = tlist_top(&done, list)) == NULL) {
189 if (tlist_empty(&pending) && tlist_empty(&running))
195 *ok = (WIFEXITED(c->status) && WEXITSTATUS(c->status) == 0);
197 *output = tal_steal(ctx, c->output);
202 /* Compile and link single C file, with object files, async. */
203 void compile_and_link_async(const void *ctx, unsigned int time_ms,
204 const char *cfile, const char *ccandir,
205 const char *objs, const char *compiler,
207 const char *libs, const char *outfile)
210 printf("Compiling and linking (async) %s\n", outfile);
211 run_command_async(ctx, time_ms,
212 "%s %s -I%s -o %s %s %s %s",
214 ccandir, outfile, cfile, objs, libs);