tools/ccanlint: Add missing header file
[ccan] / tools / ccanlint / async.c
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
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);
24
25 struct command {
26         struct list_node list;
27         char *command;
28         pid_t pid;
29         int output_fd;
30         unsigned int time_ms;
31         struct lbalance_task *task;
32         int status;
33         char *output;
34         bool done;
35         const void *ctx;
36 };
37
38 static void killme(int sig UNNEEDED)
39 {
40         kill(-getpid(), SIGKILL);
41 }
42
43 static void run_more(void)
44 {
45         struct command *c;
46
47         while (num_running < lbalance_target(lb)) {
48                 int p[2];
49
50                 c = tlist_top(&pending, list);
51                 if (!c)
52                         break;
53
54                 fflush(stdout);
55                 if (pipe(p) != 0)
56                         err(1, "Pipe failed");
57                 c->pid = fork();
58                 if (c->pid == -1)
59                         err(1, "Fork failed");
60                 if (c->pid == 0) {
61                         struct itimerval itim;
62
63                         if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
64                             || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
65                             || close(p[0]) != 0
66                             || close(STDIN_FILENO) != 0
67                             || open("/dev/null", O_RDONLY) != STDIN_FILENO)
68                                 exit(128);
69
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);
74
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));
80                 }
81
82                 if (tools_verbose)
83                         printf("Running async: %s => %i\n", c->command, c->pid);
84
85                 close(p[1]);
86                 c->output_fd = p[0];
87                 c->task = lbalance_task_new(lb);
88                 tlist_del_from(&pending, c, list);
89                 tlist_add_tail(&running, c, list);
90                 num_running++;
91         }
92 }
93
94 static void destroy_command(struct command *command)
95 {
96         if (!command->done && command->pid) {
97                 kill(-command->pid, SIGKILL);
98                 close(command->output_fd);
99                 num_running--;
100         }
101
102         tlist_del(command, list);
103 }
104
105 void run_command_async(const void *ctx, unsigned int time_ms,
106                        const char *fmt, ...)
107 {
108         struct command *command;
109         va_list ap;
110
111         assert(ctx);
112
113         if (!lb)
114                 lb = lbalance_new();
115
116         command = tal(ctx, struct command);
117         command->ctx = ctx;
118         command->time_ms = time_ms;
119         command->pid = 0;
120         /* We want to track length, so don't use tal_strdup */
121         command->output = tal_arrz(command, char, 1);
122         va_start(ap, fmt);
123         command->command = tal_vfmt(command, fmt, ap);
124         va_end(ap);
125         tlist_add_tail(&pending, command, list);
126         command->done = false;
127         tal_add_destructor(command, destroy_command);
128
129         run_more();
130 }
131
132 static void reap_output(void)
133 {
134         fd_set in;
135         struct command *c, *next;
136         int max_fd = 0;
137
138         FD_ZERO(&in);
139
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;
144         }
145
146         if (select(max_fd+1, &in, NULL, NULL, NULL) < 0)
147                 err(1, "select failed");
148
149         tlist_for_each_safe(&running, c, next, list) {
150                 if (FD_ISSET(c->output_fd, &in)) {
151                         int old_len, len;
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);
156                         if (len < 0)
157                                 err(1, "Reading from async command");
158                         tal_resize(&c->output, old_len + len);
159                         c->output[old_len + len - 1] = '\0';
160                         if (len == 0) {
161                                 struct rusage ru;
162                                 wait4(c->pid, &c->status, 0, &ru);
163                                 if (tools_verbose)
164                                         printf("Finished async %i: %s %u\n",
165                                                c->pid,
166                                                WIFEXITED(c->status)
167                                                ? "exit status"
168                                                : "killed by signal",
169                                                WIFEXITED(c->status)
170                                                ? WEXITSTATUS(c->status)
171                                                : WTERMSIG(c->status));
172                                 lbalance_task_free(c->task, &ru);
173                                 c->task = NULL;
174                                 c->done = true;
175                                 close(c->output_fd);
176                                 tlist_del_from(&running, c, list);
177                                 tlist_add_tail(&done, c, list);
178                                 num_running--;
179                         }
180                 }
181         }
182 }
183
184 void *collect_command(bool *ok, char **output)
185 {
186         struct command *c;
187         const void *ctx;
188
189         while ((c = tlist_top(&done, list)) == NULL) {
190                 if (tlist_empty(&pending) && tlist_empty(&running))
191                         return NULL;
192                 reap_output();
193                 run_more();
194         }
195
196         *ok = (WIFEXITED(c->status) && WEXITSTATUS(c->status) == 0);
197         ctx = c->ctx;
198         *output = tal_steal(ctx, c->output);
199         tal_free(c);
200         return (void *)ctx;
201 }
202
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,
207                             const char *cflags,
208                             const char *libs, const char *outfile)
209 {
210         if (compile_verbose)
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",
214                           compiler, cflags,
215                           ccandir, outfile, cfile, objs, libs);
216 }