X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=execute.c;h=f3b4df930f24f8a768d3b7150c65573efc33f541;hp=8cd4f60cdd7ddba66ab7cd3996dd94a789f8fdb1;hb=acbb02252a38214ecba3aa8a5c9b3669f9c9317e;hpb=034c6ed7da5e44bfdde5a5d0da75f7b7a59953b8 diff --git a/execute.c b/execute.c index 8cd4f60cd..f3b4df930 100644 --- a/execute.c +++ b/execute.c @@ -5,11 +5,13 @@ #include #include #include +#include #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; @@ -110,6 +112,8 @@ int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, assert(ret); assert(fds || n_fds <= 0); + log_debug("About to execute %s", command->path); + if ((pid = fork()) < 0) return -errno; @@ -235,7 +239,7 @@ void exec_command_free_list(ExecCommand *c) { LIST_REMOVE(ExecCommand, command, c, i); free(i->path); - free(i->argv); + strv_free(i->argv); free(i); } } @@ -249,7 +253,6 @@ void exec_command_free_array(ExecCommand **c, unsigned n) { } } - void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { assert(c); assert(f); @@ -276,3 +279,71 @@ void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) { 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); +}