]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/systemctl/systemctl.c
tree-wide: drop {} from one-line if blocks
[thirdparty/systemd.git] / src / systemctl / systemctl.c
index a2eb435fce78733d87b27b1abc97fba4d3c484ec..a82b9b83a2356ad9939c544f16bd2cecbef7f6ca 100644 (file)
@@ -535,10 +535,8 @@ static int get_unit_list(
                 return bus_log_create_error(r);
 
         r = sd_bus_call(bus, m, 0, &error, &reply);
-        if (r < 0) {
-                log_error("Failed to list units: %s", bus_error_message(&error, r));
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r));
 
         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
         if (r < 0)
@@ -605,7 +603,7 @@ static int get_unit_list_recursive(
         r = set_put(replies, reply);
         if (r < 0) {
                 sd_bus_message_unref(reply);
-                return r;
+                return log_oom();
         }
 
         if (arg_recursive) {
@@ -614,15 +612,15 @@ static int get_unit_list_recursive(
 
                 r = sd_get_machine_names(&machines);
                 if (r < 0)
-                        return r;
+                        return log_error_errno(r, "Failed to get machine names: %m");
 
                 STRV_FOREACH(i, machines) {
-                        _cleanup_bus_close_unref_ sd_bus *container = NULL;
+                        _cleanup_bus_flush_close_unref_ sd_bus *container = NULL;
                         int k;
 
                         r = sd_bus_open_system_machine(&container, *i);
                         if (r < 0) {
-                                log_error_errno(r, "Failed to connect to container %s: %m", *i);
+                                log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i);
                                 continue;
                         }
 
@@ -635,7 +633,7 @@ static int get_unit_list_recursive(
                         r = set_put(replies, reply);
                         if (r < 0) {
                                 sd_bus_message_unref(reply);
-                                return r;
+                                return log_oom();
                         }
                 }
 
@@ -1183,7 +1181,7 @@ static int list_timers(sd_bus *bus, char **args) {
 
         for (u = unit_infos; u < unit_infos + n; u++) {
                 _cleanup_strv_free_ char **triggered = NULL;
-                dual_timestamp next = {};
+                dual_timestamp next = DUAL_TIMESTAMP_NULL;
                 usec_t m, last = 0;
 
                 if (!endswith(u->id, ".timer"))
@@ -1474,6 +1472,7 @@ static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, cha
                                        "Requisite\0"
                                        "RequisiteOverridable\0"
                                        "Wants\0"
+                                       "ConsistsOf\0"
                                        "BindsTo\0",
                 [DEPENDENCY_REVERSE] = "RequiredBy\0"
                                        "RequiredByOverridable\0"
@@ -1709,7 +1708,7 @@ static int compare_machine_info(const void *a, const void *b) {
 }
 
 static int get_machine_properties(sd_bus *bus, struct machine_info *mi) {
-        _cleanup_bus_close_unref_ sd_bus *container = NULL;
+        _cleanup_bus_flush_close_unref_ sd_bus *container = NULL;
         int r;
 
         assert(mi);
@@ -1743,7 +1742,7 @@ static int get_machine_list(
         _cleanup_free_ char *hn = NULL;
         size_t sz = 0;
         char **i;
-        int c = 0;
+        int c = 0, r;
 
         hn = gethostname_malloc();
         if (!hn)
@@ -1761,7 +1760,10 @@ static int get_machine_list(
                 c++;
         }
 
-        sd_get_machine_names(&m);
+        r = sd_get_machine_names(&m);
+        if (r < 0)
+                return log_error_errno(r, "Failed to get machine list: %m");
+
         STRV_FOREACH(i, m) {
                 _cleanup_free_ char *class = NULL;
 
@@ -2794,6 +2796,33 @@ static int reboot_with_logind(sd_bus *bus, enum action a) {
                 return -EINVAL;
         }
 
+        if (!strv_isempty(arg_wall)) {
+                _cleanup_free_ char *m;
+
+                m = strv_join(arg_wall, " ");
+                if (!m)
+                        return log_oom();
+
+                r = sd_bus_call_method(
+                               bus,
+                               "org.freedesktop.login1",
+                               "/org/freedesktop/login1",
+                               "org.freedesktop.login1.Manager",
+                               "SetWallMessage",
+                               &error,
+                               NULL,
+                               "sb",
+                               m,
+                               !arg_no_wall);
+
+                if (r < 0) {
+                        log_warning_errno(r, "Failed to set wall message, ignoring: %s",
+                                          bus_error_message(&error, r));
+                        sd_bus_error_free(&error);
+                }
+        }
+
+
         r = sd_bus_call_method(
                         bus,
                         "org.freedesktop.login1",
@@ -3327,8 +3356,7 @@ static void print_status_info(
                         if (! dir || last) {
                                 printf(dir ? "        " : "  Drop-In: ");
 
-                                free(dir);
-                                dir = NULL;
+                                dir = mfree(dir);
 
                                 if (path_get_parent(*dropin, &dir) < 0) {
                                         log_oom();
@@ -3531,7 +3559,7 @@ static void print_status_info(
 
         if (i->control_group &&
             (i->main_pid > 0 || i->control_pid > 0 ||
-             ((arg_transport != BUS_TRANSPORT_LOCAL && arg_transport != BUS_TRANSPORT_MACHINE) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0))) {
+             ((arg_transport != BUS_TRANSPORT_LOCAL && arg_transport != BUS_TRANSPORT_MACHINE) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group) == 0))) {
                 unsigned c;
 
                 printf("   CGroup: %s\n", i->control_group);
@@ -3557,7 +3585,7 @@ static void print_status_info(
                 }
         }
 
-        if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) {
+        if (i->id && arg_transport == BUS_TRANSPORT_LOCAL)
                 show_journal_by_unit(
                                 stdout,
                                 i->id,
@@ -3570,7 +3598,6 @@ static void print_status_info(
                                 SD_JOURNAL_LOCAL_ONLY,
                                 arg_scope == UNIT_FILE_SYSTEM,
                                 ellipsized);
-        }
 
         if (i->need_daemon_reload)
                 warn_unit_file_changed(i->id);
@@ -4241,10 +4268,8 @@ static int show_one(
                         &error,
                         &reply,
                         "s", "");
-        if (r < 0) {
-                log_error("Failed to get properties: %s", bus_error_message(&error, r));
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
 
         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
         if (r < 0)
@@ -5687,7 +5712,7 @@ static int create_edit_temp_file(const char *new_path, const char *original_path
         assert(original_path);
         assert(ret_tmp_fn);
 
-        r = tempfn_random(new_path, &t);
+        r = tempfn_random(new_path, NULL, &t);
         if (r < 0)
                 return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path);
 
@@ -5855,23 +5880,15 @@ static int run_editor(char **paths) {
 
         if (pid == 0) {
                 const char **args;
-                char *editor;
+                char *editor, **editor_args = NULL;
                 char **tmp_path, **original_path, *p;
-                unsigned i = 1;
+                unsigned n_editor_args = 0, i = 1;
                 size_t argc;
 
                 (void) reset_all_signal_handlers();
                 (void) reset_signal_mask();
 
                 argc = strv_length(paths)/2 + 1;
-                args = newa(const char*, argc + 1);
-
-                args[0] = NULL;
-                STRV_FOREACH_PAIR(original_path, tmp_path, paths) {
-                        args[i] = *tmp_path;
-                        i++;
-                }
-                args[argc] = NULL;
 
                 /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL
                  * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present,
@@ -5884,10 +5901,31 @@ static int run_editor(char **paths) {
                         editor = getenv("VISUAL");
 
                 if (!isempty(editor)) {
-                        args[0] = editor;
-                        execvp(editor, (char* const*) args);
+                        editor_args = strv_split(editor, WHITESPACE);
+                        if (!editor_args) {
+                                (void) log_oom();
+                                _exit(EXIT_FAILURE);
+                        }
+                        n_editor_args = strv_length(editor_args);
+                        argc += n_editor_args - 1;
+                }
+                args = newa(const char*, argc + 1);
+
+                if (n_editor_args > 0) {
+                        args[0] = editor_args[0];
+                        for (; i < n_editor_args; i++)
+                                args[i] = editor_args[i];
                 }
 
+                STRV_FOREACH_PAIR(original_path, tmp_path, paths) {
+                        args[i] = *tmp_path;
+                        i++;
+                }
+                args[i] = NULL;
+
+                if (n_editor_args > 0)
+                        execvp(args[0], (char* const*) args);
+
                 FOREACH_STRING(p, "editor", "nano", "vim", "vi") {
                         args[0] = p;
                         execvp(p, (char* const*) args);
@@ -6248,6 +6286,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 ARG_PRESET_MODE,
                 ARG_FIRMWARE_SETUP,
                 ARG_NOW,
+                ARG_MESSAGE,
         };
 
         static const struct option options[] = {
@@ -6292,6 +6331,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 { "preset-mode",         required_argument, NULL, ARG_PRESET_MODE         },
                 { "firmware-setup",      no_argument,       NULL, ARG_FIRMWARE_SETUP      },
                 { "now",                 no_argument,       NULL, ARG_NOW                 },
+                { "message",             required_argument, NULL, ARG_MESSAGE             },
                 {}
         };
 
@@ -6576,6 +6616,11 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         arg_now = true;
                         break;
 
+                case ARG_MESSAGE:
+                        if (strv_extend(&arg_wall, optarg) < 0)
+                                return log_oom();
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -7280,6 +7325,7 @@ static int halt_now(enum action a) {
                 reboot(RB_POWER_OFF);
                 return -errno;
 
+        case ACTION_KEXEC:
         case ACTION_REBOOT: {
                 _cleanup_free_ char *param = NULL;
 
@@ -7327,7 +7373,7 @@ static int halt_main(sd_bus *bus) {
 
         if (arg_when > 0) {
                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-                _cleanup_bus_close_unref_ sd_bus *b = NULL;
+                _cleanup_bus_flush_close_unref_ sd_bus *b = NULL;
                 _cleanup_free_ char *m = NULL;
 
                 if (avoid_bus()) {
@@ -7343,30 +7389,20 @@ static int halt_main(sd_bus *bus) {
                 if (!m)
                         return log_oom();
 
-                r = sd_bus_set_property(
-                                b,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "WallMessage",
-                                &error,
-                                "s", m);
-                if (r < 0) {
-                        log_warning_errno(r, "Failed to set WallMessage property in logind: %s",
-                                          bus_error_message(&error, r));
-                        sd_bus_error_free(&error);
-                }
+                r = sd_bus_call_method(
+                               b,
+                               "org.freedesktop.login1",
+                               "/org/freedesktop/login1",
+                               "org.freedesktop.login1.Manager",
+                               "SetWallMessage",
+                               &error,
+                               NULL,
+                               "sb",
+                               m,
+                               !arg_no_wall);
 
-                r = sd_bus_set_property(
-                                b,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "EnableWallMessages",
-                                &error,
-                                "b", !arg_no_wall);
                 if (r < 0) {
-                        log_warning_errno(r, "Failed to set EnableWallMessages property in logind: %s",
+                        log_warning_errno(r, "Failed to set wall message, ignoring: %s",
                                           bus_error_message(&error, r));
                         sd_bus_error_free(&error);
                 }
@@ -7436,7 +7472,7 @@ static int runlevel_main(void) {
 }
 
 int main(int argc, char*argv[]) {
-        _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+        _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
         int r;
 
         setlocale(LC_ALL, "");
@@ -7504,7 +7540,7 @@ int main(int argc, char*argv[]) {
 
         case ACTION_CANCEL_SHUTDOWN: {
                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-                _cleanup_bus_close_unref_ sd_bus *b = NULL;
+                _cleanup_bus_flush_close_unref_ sd_bus *b = NULL;
                 _cleanup_free_ char *m = NULL;
 
                 if (avoid_bus()) {
@@ -7524,30 +7560,20 @@ int main(int argc, char*argv[]) {
                         }
                 }
 
-                r = sd_bus_set_property(
-                                b,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "WallMessage",
-                                &error,
-                                "s", arg_wall);
-                if (r < 0) {
-                        log_warning_errno(r, "Failed to set WallMessage property in logind: %s",
-                                          bus_error_message(&error, r));
-                        sd_bus_error_free(&error);
-                }
+                r = sd_bus_call_method(
+                               b,
+                               "org.freedesktop.login1",
+                               "/org/freedesktop/login1",
+                               "org.freedesktop.login1.Manager",
+                               "SetWallMessage",
+                               &error,
+                               NULL,
+                               "sb",
+                               m,
+                               !arg_no_wall);
 
-                r = sd_bus_set_property(
-                                b,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "EnableWallMessages",
-                                &error,
-                                "b", !arg_no_wall);
                 if (r < 0) {
-                        log_warning_errno(r, "Failed to set EnableWallMessages property in logind: %s",
+                        log_warning_errno(r, "Failed to set wall message, ignoring: %s",
                                           bus_error_message(&error, r));
                         sd_bus_error_free(&error);
                 }