#include <ccan/talloc/talloc.h>
-#include <ccan/grab_file/grab_file.h>
+#include <ccan/err/err.h>
#include <ccan/noerr/noerr.h>
+#include <ccan/rbuf/rbuf.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/noerr/noerr.h>
#include <ccan/time/time.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
-#include <err.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
{
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)
close_noerr(p[1]);
return talloc_asprintf(ctx, "Failed to fork: %s",
strerror(errno));
- return NULL;
}
if (pid == 0) {
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);
}
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");
*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. */
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, "");
} 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;
}
/* 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));
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;
+}