X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Ftools.c;h=4874fc62645fdff6aa372ecc6637227e6377865e;hp=dc42ae47ac427fdc46065d0262c7386738aadb54;hb=HEAD;hpb=09d5cd70d7e8105a003ed9583eadb077de014f3b diff --git a/tools/tools.c b/tools/tools.c index dc42ae47..4874fc62 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +39,8 @@ char *run_with_timeout(const void *ctx, const char *cmd, int p[2]; struct rbuf in; int status, ms; - struct timespec start; + struct timeabs start; + char *ret; *ok = false; if (pipe(p) != 0) @@ -70,7 +72,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 = timespec_to_timeval(time_from_msec(*timeout_ms)); + itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms).ts); setitimer(ITIMER_REAL, &itim, NULL); status = system(cmd); @@ -81,29 +83,28 @@ char *run_with_timeout(const void *ctx, const char *cmd, } close(p[1]); - rbuf_init(&in, p[0], tal_arr(ctx, char, 4096), 4096); - if (!rbuf_read_str(&in, 0, do_tal_realloc) && errno) - in.buf = tal_free(in.buf); + rbuf_init(&in, p[0], tal_arr(ctx, char, 4096), 4096, tal_rbuf_enlarge); + ret = rbuf_read_str(&in, 0); /* This shouldn't fail... */ if (waitpid(pid, &status, 0) != pid) err(1, "Failed to wait for child"); - ms = time_to_msec(time_sub(time_now(), start)); + ms = time_to_msec(time_between(time_now(), start)); if (ms > *timeout_ms) *timeout_ms = 0; else *timeout_ms -= ms; close(p[0]); if (tools_verbose) { - printf("%s", in.buf); + printf("%s", ret); 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 in.buf; + return ret; } /* Tals *output off ctx; return false if command fails. */ @@ -218,7 +219,6 @@ char *temp_file(const void *ctx, const char *extension, const char *srcname) bool move_file(const char *oldname, const char *newname) { char *contents; - size_t size; int fd; bool ret; @@ -233,7 +233,7 @@ bool move_file(const char *oldname, const char *newname) } /* Try copy and delete: not atomic! */ - contents = tal_grab_file(NULL, oldname, &size); + contents = grab_file(NULL, oldname); if (!contents) { if (tools_verbose) printf("read failed: %s\n", strerror(errno)); @@ -248,7 +248,7 @@ bool move_file(const char *oldname, const char *newname) goto free; } - ret = write_all(fd, contents, size); + ret = write_all(fd, contents, tal_count(contents)-1); if (close(fd) != 0) ret = false; @@ -267,28 +267,8 @@ free: return ret; } -void *do_tal_realloc(void *p, size_t size) +void *tal_rbuf_enlarge(struct membuf *mb, void *p, size_t size) { tal_resize((char **)&p, size); return p; } - -void *tal_grab_file(const void *ctx, const char *filename, size_t *size) -{ - struct rbuf rbuf; - char *buf = tal_arr(ctx, char, 0); - - if (!rbuf_open(&rbuf, filename, buf, 0)) - return tal_free(buf); - - if (!rbuf_fill_all(&rbuf, do_tal_realloc) && errno) - rbuf.buf = tal_free(rbuf.buf); - else { - rbuf.buf[rbuf.len] = '\0'; - if (size) - *size = rbuf.len; - } - close(rbuf.fd); - - return rbuf.buf; -}