5 #include <sys/resource.h>
14 #include <ccan/lbalance/lbalance.h>
15 #include <ccan/tlist/tlist.h>
16 #include <ccan/time/time.h>
18 static struct lbalance *lb;
19 TLIST_TYPE(command, struct command);
20 static struct tlist_command pending = TLIST_INIT(pending);
21 static struct tlist_command running = TLIST_INIT(running);
22 static unsigned int num_running = 0;
23 static struct tlist_command done = TLIST_INIT(done);
26 struct list_node list;
31 struct lbalance_task *task;
38 static void killme(int sig UNNEEDED)
40 kill(-getpid(), SIGKILL);
43 static void run_more(void)
47 while (num_running < lbalance_target(lb)) {
50 c = tlist_top(&pending, list);
56 err(1, "Pipe failed");
59 err(1, "Fork failed");
61 struct itimerval itim;
63 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
64 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
66 || close(STDIN_FILENO) != 0
67 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
70 signal(SIGALRM, killme);
71 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
72 itim.it_value = timespec_to_timeval(time_from_msec(c->time_ms).ts);
73 setitimer(ITIMER_REAL, &itim, NULL);
75 c->status = system(c->command);
76 if (WIFEXITED(c->status))
77 exit(WEXITSTATUS(c->status));
78 /* Here's a hint... */
79 exit(128 + WTERMSIG(c->status));
83 printf("Running async: %s => %i\n", c->command, c->pid);
87 c->task = lbalance_task_new(lb);
88 tlist_del_from(&pending, c, list);
89 tlist_add_tail(&running, c, list);
94 static void destroy_command(struct command *command)
96 if (!command->done && command->pid) {
97 kill(-command->pid, SIGKILL);
98 close(command->output_fd);
102 tlist_del(command, list);
105 void run_command_async(const void *ctx, unsigned int time_ms,
106 const char *fmt, ...)
108 struct command *command;
116 command = tal(ctx, struct command);
118 command->time_ms = time_ms;
120 /* We want to track length, so don't use tal_strdup */
121 command->output = tal_arrz(command, char, 1);
123 command->command = tal_vfmt(command, fmt, ap);
125 tlist_add_tail(&pending, command, list);
126 command->done = false;
127 tal_add_destructor(command, destroy_command);
132 static void reap_output(void)
135 struct command *c, *next;
140 tlist_for_each(&running, c, list) {
141 FD_SET(c->output_fd, &in);
142 if (c->output_fd > max_fd)
143 max_fd = c->output_fd;
146 if (select(max_fd+1, &in, NULL, NULL, NULL) < 0)
147 err(1, "select failed");
149 tlist_for_each_safe(&running, c, next, list) {
150 if (FD_ISSET(c->output_fd, &in)) {
152 /* This length includes nul terminator! */
153 old_len = tal_count(c->output);
154 tal_resize(&c->output, old_len + 1024);
155 len = read(c->output_fd, c->output + old_len - 1, 1024);
157 err(1, "Reading from async command");
158 tal_resize(&c->output, old_len + len);
159 c->output[old_len + len - 1] = '\0';
162 wait4(c->pid, &c->status, 0, &ru);
164 printf("Finished async %i: %s %u\n",
168 : "killed by signal",
170 ? WEXITSTATUS(c->status)
171 : WTERMSIG(c->status));
172 lbalance_task_free(c->task, &ru);
176 tlist_del_from(&running, c, list);
177 tlist_add_tail(&done, c, list);
184 void *collect_command(bool *ok, char **output)
189 while ((c = tlist_top(&done, list)) == NULL) {
190 if (tlist_empty(&pending) && tlist_empty(&running))
196 *ok = (WIFEXITED(c->status) && WEXITSTATUS(c->status) == 0);
198 *output = tal_steal(ctx, c->output);
203 /* Compile and link single C file, with object files, async. */
204 void compile_and_link_async(const void *ctx, unsigned int time_ms,
205 const char *cfile, const char *ccandir,
206 const char *objs, const char *compiler,
208 const char *libs, const char *outfile)
211 printf("Compiling and linking (async) %s\n", outfile);
212 run_command_async(ctx, time_ms,
213 "%s %s -I%s -o %s %s %s %s",
215 ccandir, outfile, cfile, objs, libs);