From 2edfa688d51080305a7c300bc10e4649bc5f484e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 31 Dec 2015 23:45:15 +0100 Subject: preserve working directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/daemon.c | 5 +++-- src/sat.c | 40 ++++++++++++++++++++-------------------- src/satd-list.c | 17 ++++++++++------- 3 files changed, 33 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/daemon.c b/src/daemon.c index 12c1aea..e9c79d9 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -279,7 +279,7 @@ run_job_or_hook(struct job *job, const char *hook) t (!(args = restore_array(job->payload, job->n, &argsn))); t (!(argv = sublist(args, (size_t)(job->argc)))); - t (!(envp = sublist(args + job->argc, argsn - (size_t)(job->argc)))); + t (!(envp = sublist(args + job->argc, argsn - (size_t)(job->argc)))); /* Includes wdir. */ free(args), args = NULL; if (hook) { @@ -298,7 +298,8 @@ run_job_or_hook(struct job *job, const char *hook) close(STATE_FILENO); close(BOOT_FILENO); close(REAL_FILENO); - environ = envp; + (void)(status = chdir(envp[0])); + environ = envp + 1; execvp(*argv, argv); exit(1); default: diff --git a/src/sat.c b/src/sat.c index 2cc4f09..e3e1e84 100644 --- a/src/sat.c +++ b/src/sat.c @@ -23,6 +23,7 @@ #include "client.h" #include "common.h" #include +#include @@ -50,10 +51,13 @@ USAGE("TIME COMMAND...") int main(int argc, char *argv[], char *envp[]) { +#define E(CASE, DESC) case CASE: return fprintf(stderr, "%s: %s: %s\n", argv0, DESC, argv[1]), 2 + struct timespec ts; clockid_t clk; char *msg = NULL; - size_t n; + void *new; + size_t n, size = 64; if ((argc < 3) || (argv[1][0] == '-')) usage(); @@ -63,33 +67,29 @@ main(int argc, char *argv[], char *envp[]) /* Parse the time argument. */ if (parse_time(argv[1], &ts, &clk)) { switch (errno) { - case EINVAL: - fprintf(stderr, - "%s: time parameter cound not be parsed, perhaps " - "you need an external parser: %s\n", argv0, argv[1]); - return 2; - case ERANGE: - fprintf(stderr, - "%s: the specified time is beyond the limit of what " - "can be represented by `struct timespec`: %s\n", argv0, argv[1]); - return 2; - case EDOM: - fprintf(stderr, - "%s: the specified time is in past, and more than " - "a day ago: %s\n", argv0, argv[1]); - return 2; - default: - goto fail; + E (EINVAL, "time parameter cound not be parsed, perhaps you need an external parser"); + E (ERANGE, "the specified time is beyond the limit of what can be stored"); + E (EDOM, "the specified time is in past, and more than a day ago"); + default: goto fail; } } argc -= 2; argv += 2; +retry: + /* Get the size of the current working directory's pathname. */ + t (!(new = realloc(msg, size <<= 1))); + if (!getcwd(msg = new, size)) { + t (errno != ERANGE); + goto retry; + } + size = strlen(getcwd(msg, size)) + 1, free(msg); + /* Construct message to send to the daemon. */ - n = measure_array(argv) + measure_array(envp); + n = measure_array(argv) + size + measure_array(envp); t (!(msg = malloc(n + sizeof(int) + sizeof(clk) + sizeof(ts)))); - store_array(store_array(msg, argv), envp); + store_array(getcwd(store_array(msg, argv), size) + size, envp); memcpy(msg + n, &argc, sizeof(int)), n += sizeof(int); memcpy(msg + n, &clk, sizeof(clk)), n += sizeof(clk); memcpy(msg + n, &ts, sizeof(ts)), n += sizeof(ts); diff --git a/src/satd-list.c b/src/satd-list.c index 0591368..75f7996 100644 --- a/src/satd-list.c +++ b/src/satd-list.c @@ -73,7 +73,6 @@ quote(const char *str) rc[i++] = (char)*s; if (*s == '\'') rc[i++] = '\\', rc[i++] = '\'', rc[i++] = '\''; - } break; default: @@ -146,6 +145,7 @@ send_job_human(struct job *job) const char *clk; char rem_s[3 * sizeof(time_t) + sizeof("d00:00:00")]; char *qstr = NULL; + char *wdir = NULL; char line[sizeof("job: %zu clock: unrecognised argc: %i remaining: , argv[0]: ") + 3 * sizeof(size_t) + 3 * sizeof(int) + sizeof(rem_s) + 9]; char timestr_a[sizeof("-00-00 00:00:00") + 3 * sizeof(time_t)]; @@ -195,17 +195,19 @@ send_job_human(struct job *job) /* Get arguments. */ t (!(args = restore_array(job->payload, job->n, &argsn))); t (!(argv = sublist(args, (size_t)(job->argc)))); - t (!(envp = sublist(args + job->argc, argsn - (size_t)(job->argc)))); + t (!(envp = sublist(args + job->argc, argsn - (size_t)(job->argc)))); /* Includes wdir. */ /* Send message. */ t (!(qstr = quote(args[0]))); + t (!(wdir = quote(envp[0]))); sprintf(line, "job: %zu clock: %s argc: %i remaining: %s.%09li argv[0]: ", job->no, clk, job->argc, rem_s, rem.tv_nsec); t (send_string(SOCK_FILENO, STDOUT_FILENO, - line, qstr, "\n", - " time: ", timestr_a, ".", timestr_b, "\n", - " argv:", - NULL)); + line, qstr, "\n", + " time: ", timestr_a, ".", timestr_b, "\n", + " wdir:", wdir, "\n", + " argv:", + NULL)); for (arg = argv; *arg; arg++) { free(qstr); t (!(qstr = quote(*arg))); @@ -213,7 +215,7 @@ send_job_human(struct job *job) } free(qstr), qstr = NULL; t (send_string(SOCK_FILENO, STDOUT_FILENO, "\n envp:", NULL)); - for (arg = envp; *arg; arg++) { + for (arg = envp + 1; *arg; arg++) { t (!(qstr = quote(*arg))); t (send_string(SOCK_FILENO, STDOUT_FILENO, " ", qstr, NULL)); free(qstr); @@ -226,6 +228,7 @@ done: free(qstr); free(args); free(argv); + free(wdir); free(envp); errno = saved_errno; return rc; -- cgit v1.2.3-70-g09d2