chiark / gitweb /
systemctl: return r instead of always returning 0
[elogind.git] / src / systemctl / systemctl.c
index 9d22aad28700ebe1247aaa54ba769b862702a20a..631cd3df53b9b98e775ffe5777e5511e47e156bb 100644 (file)
@@ -20,6 +20,8 @@
 ***/
 
 #include <sys/reboot.h>
+#include <linux/reboot.h>
+#include <sys/syscall.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <locale.h>
@@ -287,14 +289,16 @@ static bool output_show_unit(const struct unit_info *u) {
 }
 
 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
-        unsigned id_len, max_id_len, active_len, sub_len, job_len, desc_len, n_shown = 0;
+        unsigned id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len;
+        unsigned n_shown = 0;
         const struct unit_info *u;
         int job_count = 0;
 
-        max_id_len = sizeof("UNIT")-1;
-        active_len = sizeof("ACTIVE")-1;
-        sub_len = sizeof("SUB")-1;
-        job_len = sizeof("JOB")-1;
+        max_id_len = strlen("UNIT");
+        load_len = strlen("LOAD");
+        active_len = strlen("ACTIVE");
+        sub_len = strlen("SUB");
+        job_len = strlen("JOB");
         desc_len = 0;
 
         for (u = unit_infos; u < unit_infos + c; u++) {
@@ -302,6 +306,7 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
                         continue;
 
                 max_id_len = MAX(max_id_len, strlen(u->id));
+                load_len = MAX(load_len, strlen(u->load_state));
                 active_len = MAX(active_len, strlen(u->active_state));
                 sub_len = MAX(sub_len, strlen(u->sub_state));
                 if (u->job_id != 0) {
@@ -344,7 +349,7 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
                         continue;
 
                 if (!n_shown && !arg_no_legend) {
-                        printf("%-*s %-6s %-*s %-*s ", id_len, "UNIT", "LOAD",
+                        printf("%-*s %-*s %-*s %-*s ", id_len, "UNIT", load_len, "LOAD",
                                active_len, "ACTIVE", sub_len, "SUB");
                         if (job_count)
                                 printf("%-*s ", job_len, "JOB");
@@ -371,9 +376,9 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
 
                 e = arg_full ? NULL : ellipsize(u->id, id_len, 33);
 
-                printf("%s%-*s%s %s%-6s%s %s%-*s %-*s%s %-*s",
+                printf("%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
                        on, id_len, e ? e : u->id, off,
-                       on_loaded, u->load_state, off_loaded,
+                       on_loaded, load_len, u->load_state, off_loaded,
                        on_active, active_len, u->active_state,
                        sub_len, u->sub_state, off_active,
                        job_count ? job_len + 1 : 0, u->job_id ? u->job_type : "");
@@ -1253,12 +1258,11 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
                         shorten = true;
                 }
 
-                if (on_tty())
-                        printf("%*s %-*s %-*s %-*s\n",
-                               l0, "JOB",
-                               l1, "UNIT",
-                               l2, "TYPE",
-                               l3, "STATE");
+                printf("%*s %-*s %-*s %-*s\n",
+                       l0, "JOB",
+                       l1, "UNIT",
+                       l2, "TYPE",
+                       l3, "STATE");
 
                 for (i = 0, j = jobs; i < n; i++, j++) {
                         _cleanup_free_ char *e = NULL;
@@ -1281,8 +1285,7 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
         on = ansi_highlight();
         off = ansi_highlight_off();
 
-        if (on_tty())
-                printf("\n%s%zu jobs listed%s.\n", on, n, off);
+        printf("\n%s%zu jobs listed%s.\n", on, n, off);
 }
 
 static int list_jobs(DBusConnection *bus, char **args) {
@@ -1362,7 +1365,7 @@ static int list_jobs(DBusConnection *bus, char **args) {
         }
         free(jobs);
 
-        return 0;
+        return r;
 }
 
 static int cancel_job(DBusConnection *bus, char **args) {
@@ -4804,7 +4807,7 @@ static int systemctl_help(void) {
                "  emergency                       Enter system emergency mode\n"
                "  halt                            Shut down and halt the system\n"
                "  poweroff                        Shut down and power-off the system\n"
-               "  reboot                          Shut down and reboot the system\n"
+               "  reboot [ARG]                    Shut down and reboot the system\n"
                "  kexec                           Shut down and reboot the system with kexec\n"
                "  exit                            Request user instance exit\n"
                "  switch-root [ROOT] [INIT]       Change to a different root file system\n"
@@ -4818,7 +4821,7 @@ static int systemctl_help(void) {
 
 static int halt_help(void) {
 
-        printf("%s [OPTIONS...]\n\n"
+        printf("%s [OPTIONS...]%s\n\n"
                "%s the system.\n\n"
                "     --help      Show this help\n"
                "     --halt      Halt the machine\n"
@@ -4829,6 +4832,7 @@ static int halt_help(void) {
                "  -d --no-wtmp   Don't write wtmp record\n"
                "     --no-wall   Don't send wall message before halt/power-off/reboot\n",
                program_invocation_short_name,
+               arg_action == ACTION_REBOOT   ? " [ARG]" : "",
                arg_action == ACTION_REBOOT   ? "Reboot" :
                arg_action == ACTION_POWEROFF ? "Power off" :
                                                "Halt");
@@ -5253,7 +5257,7 @@ static int halt_parse_argv(int argc, char *argv[]) {
                 {}
         };
 
-        int c, runlevel;
+        int c, r, runlevel;
 
         assert(argc >= 0);
         assert(argv);
@@ -5311,7 +5315,14 @@ static int halt_parse_argv(int argc, char *argv[]) {
                 }
         }
 
-        if (optind < argc) {
+        if (arg_action == ACTION_REBOOT && argc == optind + 1) {
+                r = write_string_file(REBOOT_PARAM_FILE, argv[optind]);
+                if (r < 0) {
+                        log_error("Failed to write reboot param to "
+                                  REBOOT_PARAM_FILE": %s", strerror(-r));
+                        return r;
+                }
+        } else if (optind < argc) {
                 log_error("Too many arguments.");
                 return -EINVAL;
         }
@@ -5944,6 +5955,8 @@ done:
 
 static _noreturn_ void halt_now(enum action a) {
 
+        _cleanup_free_ char *param = NULL;
+
        /* Make sure C-A-D is handled by the kernel from this
          * point on... */
         reboot(RB_ENABLE_CAD);
@@ -5961,8 +5974,14 @@ static _noreturn_ void halt_now(enum action a) {
                 break;
 
         case ACTION_REBOOT:
-                log_info("Rebooting.");
-                reboot(RB_AUTOBOOT);
+                if (read_one_line_file(REBOOT_PARAM_FILE, &param) == 0) {
+                        log_info("Rebooting with arg '%s'.", param);
+                        syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
+                                LINUX_REBOOT_CMD_RESTART2, param);
+                } else {
+                        log_info("Rebooting.");
+                        reboot(RB_AUTOBOOT);
+                }
                 break;
 
         default: