chiark / gitweb /
core: free word later in parse_proc_cmdline
[elogind.git] / src / core / main.c
index 022d05a31b815cf8b9bc11afa887268a2f48a9e4..12af3cdd270bab97d255c3a7c7ac9d188413391b 100644 (file)
@@ -168,12 +168,12 @@ _noreturn_ static void crash(int sig) {
 
                 pid = fork();
                 if (pid < 0)
-                        log_error("Failed to fork off crash shell: %s", strerror(errno));
+                        log_error("Failed to fork off crash shell: %m");
                 else if (pid == 0) {
                         make_console_stdio();
                         execl("/bin/sh", "/bin/sh", NULL);
 
-                        log_error("execl() failed: %s", strerror(errno));
+                        log_error("execl() failed: %m");
                         _exit(1);
                 }
 
@@ -350,12 +350,12 @@ static int parse_proc_cmdline_word(const char *word) {
                 if (!eq) {
                         r = unsetenv(cenv);
                         if (r < 0)
-                                log_warning("unsetenv failed %s. Ignoring.", strerror(errno));
+                                log_warning("unsetenv failed %m. Ignoring.");
                 } else {
                         *eq = 0;
                         r = setenv(cenv, eq + 1, 1);
                         if (r < 0)
-                                log_warning("setenv failed %s. Ignoring.", strerror(errno));
+                                log_warning("setenv failed %m. Ignoring.");
                 }
                 free(cenv);
 
@@ -495,14 +495,14 @@ static int config_parse_cpu_affinity2(
                 unsigned cpu;
 
                 if (!(t = strndup(w, l)))
-                        return -ENOMEM;
+                        return log_oom();
 
                 r = safe_atou(t, &cpu);
                 free(t);
 
                 if (!c)
                         if (!(c = cpu_set_malloc(&ncpus)))
-                                return -ENOMEM;
+                                return log_oom();
 
                 if (r < 0 || cpu >= ncpus) {
                         log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
@@ -568,7 +568,7 @@ static int config_parse_join_controllers(
 
                 s = strndup(w, length);
                 if (!s)
-                        return -ENOMEM;
+                        return log_oom();
 
                 l = strv_split(s, ",");
                 free(s);
@@ -584,7 +584,7 @@ static int config_parse_join_controllers(
                         arg_join_controllers = new(char**, 2);
                         if (!arg_join_controllers) {
                                 strv_free(l);
-                                return -ENOMEM;
+                                return log_oom();
                         }
 
                         arg_join_controllers[0] = l;
@@ -598,7 +598,7 @@ static int config_parse_join_controllers(
                         t = new0(char**, n+2);
                         if (!t) {
                                 strv_free(l);
-                                return -ENOMEM;
+                                return log_oom();
                         }
 
                         n = 0;
@@ -612,7 +612,7 @@ static int config_parse_join_controllers(
                                         if (!c) {
                                                 strv_free(l);
                                                 strv_free_free(t);
-                                                return -ENOMEM;
+                                                return log_oom();
                                         }
 
                                         strv_free(l);
@@ -624,7 +624,7 @@ static int config_parse_join_controllers(
                                         if (!c) {
                                                 strv_free(l);
                                                 strv_free_free(t);
-                                                return -ENOMEM;
+                                                return log_oom();
                                         }
 
                                         t[n++] = c;
@@ -727,10 +727,13 @@ static int parse_proc_cmdline(void) {
                 }
 
                 r = parse_proc_cmdline_word(word);
-                free(word);
-
-                if (r < 0)
+                if (r < 0) {
+                        log_error("Failed on cmdline argument %s: %s", word, strerror(-r));
+                        free(word);
                         goto finish;
+                }
+
+                free(word);
         }
 
         r = 0;
@@ -1017,8 +1020,10 @@ static int parse_argv(int argc, char *argv[]) {
                  * instead. */
 
                 for (a = argv; a < argv + argc; a++)
-                        if ((r = parse_proc_cmdline_word(*a)) < 0)
+                        if ((r = parse_proc_cmdline_word(*a)) < 0) {
+                                log_error("Failed on cmdline argument %s: %s", *a, strerror(-r));
                                 return r;
+                        }
         }
 
         return 0;
@@ -1058,7 +1063,7 @@ static int version(void) {
         return 0;
 }
 
-static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
+static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool serialize_jobs) {
         FILE *f = NULL;
         FDSet *fds = NULL;
         int r;
@@ -1070,18 +1075,21 @@ static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
         /* Make sure nothing is really destructed when we shut down */
         m->n_reloading ++;
 
-        if ((r = manager_open_serialization(m, &f)) < 0) {
+        r = manager_open_serialization(m, &f);
+        if (r < 0) {
                 log_error("Failed to create serialization file: %s", strerror(-r));
                 goto fail;
         }
 
-        if (!(fds = fdset_new())) {
+        fds = fdset_new();
+        if (!fds) {
                 r = -ENOMEM;
                 log_error("Failed to allocate fd set: %s", strerror(-r));
                 goto fail;
         }
 
-        if ((r = manager_serialize(m, f, fds)) < 0) {
+        r = manager_serialize(m, f, fds, serialize_jobs);
+        if (r < 0) {
                 log_error("Failed to serialize state: %s", strerror(-r));
                 goto fail;
         }
@@ -1091,12 +1099,14 @@ static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
                 goto fail;
         }
 
-        if ((r = fd_cloexec(fileno(f), false)) < 0) {
+        r = fd_cloexec(fileno(f), false);
+        if (r < 0) {
                 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
                 goto fail;
         }
 
-        if ((r = fdset_cloexec(fds, false)) < 0) {
+        r = fdset_cloexec(fds, false);
+        if (r < 0) {
                 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
                 goto fail;
         }
@@ -1288,8 +1298,11 @@ int main(int argc, char *argv[]) {
         }
 
         /* Initialize default unit */
-        if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
+        r = set_default_unit(SPECIAL_DEFAULT_TARGET);
+        if (r < 0) {
+                log_error("Failed to set default unit %s: %s", SPECIAL_DEFAULT_TARGET, strerror(-r));
                 goto finish;
+        }
 
         /* By default, mount "cpu" and "cpuacct" together */
         arg_join_controllers = new(char**, 2);
@@ -1495,6 +1508,15 @@ int main(int argc, char *argv[]) {
                 }
         }
 
+        if (arg_running_as == MANAGER_USER) {
+                /* Become reaper of our children */
+                if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) {
+                        log_warning("Failed to make us a subreaper: %m");
+                        if (errno == EINVAL)
+                                log_info("Perhaps the kernel version is too old (< 3.4?)");
+                }
+        }
+
         r = manager_new(arg_running_as, &m);
         if (r < 0) {
                 log_error("Failed to allocate manager object: %s", strerror(-r));
@@ -1624,7 +1646,7 @@ int main(int argc, char *argv[]) {
 
                 case MANAGER_REEXECUTE:
 
-                        if (prepare_reexecute(m, &serialization, &fds) < 0)
+                        if (prepare_reexecute(m, &serialization, &fds, true) < 0)
                                 goto finish;
 
                         reexecute = true;
@@ -1638,7 +1660,7 @@ int main(int argc, char *argv[]) {
                         m->switch_root = m->switch_root_init = NULL;
 
                         if (!switch_root_init)
-                                if (prepare_reexecute(m, &serialization, &fds) < 0)
+                                if (prepare_reexecute(m, &serialization, &fds, false) < 0)
                                         goto finish;
 
                         reexecute = true;