chiark / gitweb /
Use format patterns for usec_t, pid_t, nsec_t, usec_t
[elogind.git] / src / core / execute.c
index 5abc69dec4e6de7fcf146ea64fe9772a98cf72fe..39c0fed7acaeea19343aaf842c01bd9be26e0415 100644 (file)
@@ -1017,6 +1017,92 @@ static void do_idle_pipe_dance(int idle_pipe[4]) {
                 close_nointr_nofail(idle_pipe[3]);
 }
 
+static int build_environment(
+                ExecContext *c,
+                unsigned n_fds,
+                usec_t watchdog_usec,
+                const char *home,
+                const char *username,
+                const char *shell,
+                char ***ret) {
+
+        _cleanup_strv_free_ char **our_env = NULL;
+        unsigned n_env = 0;
+        char *x;
+
+        assert(c);
+        assert(ret);
+
+        our_env = new0(char*, 10);
+        if (!our_env)
+                return -ENOMEM;
+
+        if (n_fds > 0) {
+                if (asprintf(&x, "LISTEN_PID="PID_FMT, getpid()) < 0)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+
+                if (asprintf(&x, "LISTEN_FDS=%u", n_fds) < 0)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (watchdog_usec > 0) {
+                if (asprintf(&x, "WATCHDOG_PID="PID_FMT, getpid()) < 0)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+
+                if (asprintf(&x, "WATCHDOG_USEC=%llu", (unsigned long long) watchdog_usec) < 0)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (home) {
+                x = strappend("HOME=", home);
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (username) {
+                x = strappend("LOGNAME=", username);
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+
+                x = strappend("USER=", username);
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (shell) {
+                x = strappend("SHELL=", shell);
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (is_terminal_input(c->std_input) ||
+            c->std_output == EXEC_OUTPUT_TTY ||
+            c->std_error == EXEC_OUTPUT_TTY ||
+            c->tty_path) {
+
+                x = strdup(default_term_for_tty(tty_path(c)));
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        our_env[n_env++] = NULL;
+        assert(n_env <= 10);
+
+        *ret = our_env;
+        our_env = NULL;
+
+        return 0;
+}
+
 int exec_spawn(ExecCommand *command,
                char **argv,
                ExecContext *context,
@@ -1029,6 +1115,7 @@ int exec_spawn(ExecCommand *command,
                CGroupControllerMask cgroup_supported,
                const char *cgroup_path,
                const char *unit_id,
+               usec_t watchdog_usec,
                int idle_pipe[4],
                ExecRuntime *runtime,
                pid_t *ret) {
@@ -1089,7 +1176,7 @@ int exec_spawn(ExecCommand *command,
         if (pid == 0) {
                 _cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL;
                 const char *username = NULL, *home = NULL, *shell = NULL;
-                unsigned n_dont_close = 0, n_env = 0;
+                unsigned n_dont_close = 0;
                 int dont_close[n_fds + 3];
                 uid_t uid = (uid_t) -1;
                 gid_t gid = (gid_t) -1;
@@ -1485,29 +1572,12 @@ int exec_spawn(ExecCommand *command,
                         }
                 }
 
-                our_env = new(char*, 8);
-                if (!our_env ||
-                    (n_fds > 0 && (
-                            asprintf(our_env + n_env++, "LISTEN_PID=%lu", (unsigned long) getpid()) < 0 ||
-                            asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0)) ||
-                    (home && asprintf(our_env + n_env++, "HOME=%s", home) < 0) ||
-                    (username && (
-                            asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
-                            asprintf(our_env + n_env++, "USER=%s", username) < 0)) ||
-                    (shell && asprintf(our_env + n_env++, "SHELL=%s", shell) < 0) ||
-                    ((is_terminal_input(context->std_input) ||
-                      context->std_output == EXEC_OUTPUT_TTY ||
-                      context->std_error == EXEC_OUTPUT_TTY) && (
-                              !(our_env[n_env++] = strdup(default_term_for_tty(tty_path(context))))))) {
-
-                        err = -ENOMEM;
+                err = build_environment(context, n_fds, watchdog_usec, home, username, shell, &our_env);
+                if (r < 0) {
                         r = EXIT_MEMORY;
                         goto fail_child;
                 }
 
-                our_env[n_env++] = NULL;
-                assert(n_env <= 8);
-
                 final_env = strv_env_merge(5,
                                            environment,
                                            our_env,
@@ -1566,8 +1636,8 @@ int exec_spawn(ExecCommand *command,
 
         log_struct_unit(LOG_DEBUG,
                         unit_id,
-                        "MESSAGE=Forked %s as %lu",
-                        command->path, (unsigned long) pid,
+                        "MESSAGE=Forked %s as "PID_FMT,
+                        command->path, pid,
                         NULL);
 
         /* We add the new process to the cgroup both in the child (so
@@ -1904,12 +1974,12 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
                 fprintf(f, "%sCPUAffinity:", prefix);
                 for (i = 0; i < c->cpuset_ncpus; i++)
                         if (CPU_ISSET_S(i, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset))
-                                fprintf(f, " %i", i);
+                                fprintf(f, " %u", i);
                 fputs("\n", f);
         }
 
         if (c->timer_slack_nsec != (nsec_t) -1)
-                fprintf(f, "%sTimerSlackNSec: %lu\n", prefix, (unsigned long)c->timer_slack_nsec);
+                fprintf(f, "%sTimerSlackNSec: "NSEC_FMT "\n", prefix, c->timer_slack_nsec);
 
         fprintf(f,
                 "%sStandardInput: %s\n"
@@ -2069,8 +2139,8 @@ void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
                 return;
 
         fprintf(f,
-                "%sPID: %lu\n",
-                prefix, (unsigned long) s->pid);
+                "%sPID: "PID_FMT"\n",
+                prefix, s->pid);
 
         if (s->start_timestamp.realtime > 0)
                 fprintf(f,
@@ -2212,7 +2282,7 @@ static int exec_runtime_allocate(ExecRuntime **rt) {
                 return 0;
 
         *rt = new0(ExecRuntime, 1);
-        if (!rt)
+        if (!*rt)
                 return -ENOMEM;
 
         (*rt)->n_ref = 1;