failtest: use high-numbers file descriptors to stay out of the way.
[ccan] / tools / ccanlint / async.c
index 32a67d29add913cbc2c4ae3f557b91534d87456b..e12e140daae9b0a4d5cccd0ae08b076fb782f5c4 100644 (file)
@@ -47,10 +47,12 @@ static void run_more(void)
 
        while (num_running < lbalance_target(lb)) {
                int p[2];
+
                c = tlist_top(&pending, struct command, list);
                if (!c)
                        break;
 
+               fflush(stdout);
                if (pipe(p) != 0)
                        err(1, "Pipe failed");
                c->pid = fork();
@@ -148,14 +150,16 @@ static void reap_output(void)
        tlist_for_each_safe(&running, c, next, list) {
                if (FD_ISSET(c->output_fd, &in)) {
                        int old_len, len;
+                       /* This length includes nul terminator! */
                        old_len = talloc_array_length(c->output);
                        c->output = talloc_realloc(c, c->output, char,
                                                   old_len + 1024);
-                       len = read(c->output_fd, c->output + old_len, 1024);
+                       len = read(c->output_fd, c->output + old_len - 1, 1024);
                        if (len < 0)
                                err(1, "Reading from async command");
                        c->output = talloc_realloc(c, c->output, char,
                                                   old_len + len);
+                       c->output[old_len + len - 1] = '\0';
                        if (len == 0) {
                                struct rusage ru;
                                wait4(c->pid, &c->status, 0, &ru);
@@ -198,3 +202,18 @@ void *collect_command(bool *ok, char **output)
        talloc_free(c);
        return (void *)ctx;
 }
+
+/* Compile and link single C file, with object files, async. */
+void compile_and_link_async(const void *ctx, unsigned int time_ms,
+                           const char *cfile, const char *ccandir,
+                           const char *objs, const char *compiler,
+                           const char *cflags,
+                           const char *libs, const char *outfile)
+{
+       if (compile_verbose)
+               printf("Compiling and linking (async) %s\n", outfile);
+       run_command_async(ctx, time_ms,
+                         "%s %s -I%s -o %s %s %s %s",
+                         compiler, cflags,
+                         ccandir, outfile, cfile, objs, libs);
+}