#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
+#include <string.h>
#include "execute.h"
#include "strv.h"
#include "macro.h"
#include "util.h"
+#include "log.h"
static int close_fds(int except[], unsigned n_except) {
DIR *d;
assert(ret);
assert(fds || n_fds <= 0);
+ log_debug("About to execute %s", command->path);
+
if ((pid = fork()) < 0)
return -errno;
LIST_REMOVE(ExecCommand, command, c, i);
free(i->path);
- free(i->argv);
+ strv_free(i->argv);
free(i);
}
}
}
}
-
void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
assert(c);
assert(f);
s->status = status;
s->timestamp = now(CLOCK_REALTIME);
}
+
+char *exec_command_line(ExecCommand *c) {
+ size_t k;
+ char *n, *p, **a;
+ bool first = true;
+
+ assert(c);
+ assert(c->argv);
+
+ k = 1;
+ STRV_FOREACH(a, c->argv)
+ k += strlen(*a)+3;
+
+ if (!(n = new(char, k)))
+ return NULL;
+
+ p = n;
+ STRV_FOREACH(a, c->argv) {
+
+ if (!first)
+ *(p++) = ' ';
+ else
+ first = false;
+
+ if (strpbrk(*a, WHITESPACE)) {
+ *(p++) = '\'';
+ p = stpcpy(p, *a);
+ *(p++) = '\'';
+ } else
+ p = stpcpy(p, *a);
+
+ }
+
+ *p = 0;
+
+ /* FIXME: this doesn't really handle arguments that have
+ * spaces and ticks in them */
+
+ return n;
+}
+
+void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
+ char *cmd;
+
+ assert(c);
+ assert(f);
+
+ if (!prefix)
+ prefix = "";
+
+ cmd = exec_command_line(c);
+
+ fprintf(f,
+ "%sCommand Line: %s\n",
+ prefix, cmd ? cmd : strerror(ENOMEM));
+
+ free(cmd);
+}
+
+void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
+ assert(f);
+
+ if (!prefix)
+ prefix = "";
+
+ LIST_FOREACH(command, c, c)
+ exec_command_dump(c, f, prefix);
+}