X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Ftools.c;h=eacfbf9c8d353ac89bb7e782b19d379e0aefd1a1;hp=769d6aeee7b1535ad191c3c22167ccb616ed283c;hb=64eca8b85ef0bd13d72381b6c70110e4c342339b;hpb=20077630db617903098f44ac49d5816f421e4cc5 diff --git a/tools/tools.c b/tools/tools.c index 769d6aee..eacfbf9c 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -63,10 +63,8 @@ static void killme(int sig) kill(-getpid(), SIGKILL); } -static char *run_with_timeout(const void *ctx, - const char *cmd, - bool *ok, - unsigned *timeout_ms) +char *run_with_timeout(const void *ctx, const char *cmd, + bool *ok, unsigned *timeout_ms) { pid_t pid; int p[2]; @@ -99,7 +97,6 @@ static char *run_with_timeout(const void *ctx, || open("/dev/null", O_RDONLY) != STDIN_FILENO) exit(128); - setpgid(0, 0); signal(SIGALRM, killme); itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0; itim.it_value.tv_sec = *timeout_ms / 1000; @@ -145,6 +142,8 @@ char *run_command(const void *ctx, unsigned int *time_ms, const char *fmt, ...) if (!time_ms) time_ms = &default_time; + else if (*time_ms == 0) + return talloc_strdup(ctx, "\n== TIMED OUT ==\n"); va_start(ap, fmt); cmd = talloc_vasprintf(ctx, fmt, ap); @@ -173,7 +172,7 @@ static int unlink_all(char *dir) return 0; } -char *temp_file(const void *ctx, const char *extension) +char *temp_dir(const void *ctx) { /* For first call, create dir. */ while (!tmpdir) { @@ -193,8 +192,25 @@ char *temp_file(const void *ctx, const char *extension) } talloc_set_destructor(tmpdir, unlink_all); } + return tmpdir; +} + +char *temp_file(const void *ctx, const char *extension) +{ + return talloc_asprintf(ctx, "%s/%u%s", + temp_dir(ctx), count++, extension); +} + +char *maybe_temp_file(const void *ctx, const char *extension, bool keep, + const char *srcname) +{ + size_t baselen; + + if (!keep) + return temp_file(ctx, extension); - return talloc_asprintf(ctx, "%s/%u%s", tmpdir, count++, extension); + baselen = strrchr(srcname, '.') - srcname; + return talloc_asprintf(ctx, "%.*s%s", baselen, srcname, extension); } bool move_file(const char *oldname, const char *newname)