5 #include <sys/resource.h>
13 #include <ccan/lbalance/lbalance.h>
14 #include <ccan/tlist/tlist.h>
15 #include <ccan/grab_file/grab_file.h>
16 #include <ccan/time/time.h>
17 #include <ccan/talloc/talloc.h>
19 static struct lbalance *lb;
20 TLIST_TYPE(command, struct command);
21 static struct tlist_command pending = TLIST_INIT(pending);
22 static struct tlist_command running = TLIST_INIT(running);
23 static unsigned int num_running = 0;
24 static struct tlist_command done = TLIST_INIT(done);
27 struct list_node list;
32 struct lbalance_task *task;
39 static void killme(int sig)
41 kill(-getpid(), SIGKILL);
44 static void run_more(void)
48 while (num_running < lbalance_target(lb)) {
50 c = tlist_top(&pending, struct command, 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 = time_from_msec(c->time_ms);
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 int 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);
105 void run_command_async(const void *ctx, unsigned int time_ms,
106 const char *fmt, ...)
108 struct command *command;
116 command = talloc(ctx, struct command);
118 command->time_ms = time_ms;
120 command->output = talloc_strdup(command, "");
122 command->command = talloc_vasprintf(command, fmt, ap);
124 tlist_add_tail(&pending, command, list);
125 command->done = false;
126 talloc_set_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 = talloc_array_length(c->output);
153 c->output = talloc_realloc(c, c->output, char,
155 len = read(c->output_fd, c->output + old_len - 1, 1024);
157 err(1, "Reading from async command");
158 c->output = talloc_realloc(c, c->output, char,
160 c->output[old_len + len - 1] = '\0';
163 wait4(c->pid, &c->status, 0, &ru);
165 printf("Finished async %i: %s %u\n",
169 : "killed by signal",
171 ? WEXITSTATUS(c->status)
172 : WTERMSIG(c->status));
173 lbalance_task_free(c->task, &ru);
177 tlist_del_from(&running, c, list);
178 tlist_add_tail(&done, c, list);
185 void *collect_command(bool *ok, char **output)
190 while ((c = tlist_top(&done, struct command, list)) == NULL) {
191 if (tlist_empty(&pending) && tlist_empty(&running))
197 *ok = (WIFEXITED(c->status) && WEXITSTATUS(c->status) == 0);
199 *output = talloc_steal(ctx, c->output);
204 /* Compile and link single C file, with object files, async. */
205 void compile_and_link_async(const void *ctx, unsigned int time_ms,
206 const char *cfile, const char *ccandir,
207 const char *objs, const char *compiler,
209 const char *libs, const char *outfile)
212 printf("Compiling and linking (async) %s\n", outfile);
213 run_command_async(ctx, time_ms,
214 "%s %s -I%s -o %s %s %s %s",
216 ccandir, outfile, cfile, objs, libs);