X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=tools%2Ftools.c;h=f3c4fe8821b8b10e351a8ecb35334a025ffd6d87;hb=aabf300e324f7da5134d7ad45afba11225045c24;hp=0a29ddf8e64cce7875a8a9d8c8fe37a0c6d90a13;hpb=2bce880a453a64d2d1dfc2e48184553ff4f2550b;p=ccan diff --git a/tools/tools.c b/tools/tools.c index 0a29ddf8..f3c4fe88 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -1,6 +1,7 @@ #include -#include +#include #include +#include #include #include #include @@ -13,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -71,9 +71,9 @@ char *run_with_timeout(const void *ctx, const char *cmd, { pid_t pid; int p[2]; - char *ret; + struct rbuf in; int status, ms; - struct timeval start; + struct timespec start; *ok = false; if (pipe(p) != 0) @@ -92,7 +92,6 @@ char *run_with_timeout(const void *ctx, const char *cmd, close_noerr(p[1]); return talloc_asprintf(ctx, "Failed to fork: %s", strerror(errno)); - return NULL; } if (pid == 0) { @@ -107,7 +106,7 @@ char *run_with_timeout(const void *ctx, const char *cmd, signal(SIGALRM, killme); itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0; - itim.it_value = time_from_msec(*timeout_ms); + itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms)); setitimer(ITIMER_REAL, &itim, NULL); status = system(cmd); @@ -118,7 +117,12 @@ char *run_with_timeout(const void *ctx, const char *cmd, } close(p[1]); - ret = grab_fd(ctx, p[0], NULL); + rbuf_init(&in, p[0], talloc_array(ctx, char, 4096), 4096); + if (!rbuf_read_str(&in, 0, do_talloc_realloc) && errno) { + talloc_free(in.buf); + in.buf = NULL; + } + /* This shouldn't fail... */ if (waitpid(pid, &status, 0) != pid) err(1, "Failed to wait for child"); @@ -130,14 +134,14 @@ char *run_with_timeout(const void *ctx, const char *cmd, *timeout_ms -= ms; close(p[0]); if (tools_verbose) { - printf("%s", ret); + printf("%s", in.buf); printf("Finished: %u ms, %s %u\n", ms, WIFEXITED(status) ? "exit status" : "killed by signal", WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)); } *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0); - return ret; + return in.buf; } /* Tallocs *output off ctx; return false if command fails. */ @@ -213,8 +217,7 @@ int unlink_file_destructor(char *filename) return 0; } -char *maybe_temp_file(const void *ctx, const char *extension, bool keep, - const char *srcname) +char *temp_file(const void *ctx, const char *extension, const char *srcname) { unsigned baselen; char *f, *suffix = talloc_strdup(ctx, ""); @@ -237,10 +240,7 @@ char *maybe_temp_file(const void *ctx, const char *extension, bool keep, } while (lstat(f, &st) == 0); if (tools_verbose) - printf("Creating %sfile %s\n", keep ? "" : "temporary ", f); - - if (!keep) - talloc_set_destructor(f, unlink_file_destructor); + printf("Creating file %s\n", f); talloc_free(suffix); return f; @@ -264,7 +264,7 @@ bool move_file(const char *oldname, const char *newname) } /* Try copy and delete: not atomic! */ - contents = grab_file(NULL, oldname, &size); + contents = talloc_grab_file(NULL, oldname, &size); if (!contents) { if (tools_verbose) printf("read failed: %s\n", strerror(errno)); @@ -297,3 +297,30 @@ free: talloc_free(contents); return ret; } + +void *do_talloc_realloc(void *p, size_t size) +{ + return talloc_realloc(NULL, p, char, size); +} + +void *talloc_grab_file(const void *ctx, const char *filename, size_t *size) +{ + struct rbuf rbuf; + char *buf = talloc_size(ctx, 4096); + + if (!rbuf_open(&rbuf, filename, buf, 4096)) { + talloc_free(buf); + return NULL; + } + if (!rbuf_fill_all(&rbuf, do_talloc_realloc)) { + talloc_free(rbuf.buf); + rbuf.buf = NULL; + } else { + rbuf.buf[rbuf.len] = '\0'; + if (size) + *size = rbuf.len; + } + close(rbuf.fd); + + return rbuf.buf; +}