| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-error.h" |
| 7 |
#include "bus-locator.h" |
| 8 |
#include "bus-unit-util.h" |
| 9 |
#include "bus-util.h" |
| 10 |
#include "install.h" |
| 11 |
#include "log.h" |
| 12 |
#include "proc-cmdline.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "systemctl.h" |
| 15 |
#include "systemctl-daemon-reload.h" |
| 16 |
#include "systemctl-set-default.h" |
| 17 |
#include "systemctl-util.h" |
| 18 |
#include "unit-file.h" |
| 19 |
#include "unit-name.h" |
| 20 |
|
| 21 |
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
| 22 |
char **ret = data; |
| 23 |
|
| 24 |
if (streq(key, "systemd.unit")) { |
| 25 |
if (proc_cmdline_value_missing(key, value)) |
| 26 |
return 0; |
| 27 |
if (!unit_name_is_valid(value, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { |
| 28 |
log_warning("Unit name specified on %s= is not valid, ignoring: %s", key, value); |
| 29 |
return 0; |
| 30 |
} |
| 31 |
|
| 32 |
return free_and_strdup_warn(ret, key); |
| 33 |
|
| 34 |
} else if (!value) { |
| 35 |
if (runlevel_to_target(key)) |
| 36 |
return free_and_strdup_warn(ret, key); |
| 37 |
} |
| 38 |
|
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
static void emit_cmdline_warning(void) { |
| 43 |
if (arg_quiet || arg_root) |
| 44 |
/* don't bother checking the command line if we're operating on a container */ |
| 45 |
return; |
| 46 |
|
| 47 |
_cleanup_free_ char *override = NULL; |
| 48 |
int r; |
| 49 |
|
| 50 |
r = proc_cmdline_parse(parse_proc_cmdline_item, &override, 0); |
| 51 |
if (r < 0) |
| 52 |
log_debug_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
| 53 |
if (override) |
| 54 |
log_notice("Note: found \"%s\" on the kernel command line, which overrides the default unit.", |
| 55 |
override); |
| 56 |
} |
| 57 |
|
| 58 |
static int determine_default(char **ret_name) { |
| 59 |
int r; |
| 60 |
|
| 61 |
if (install_client_side() != INSTALL_CLIENT_SIDE_NO) { |
| 62 |
r = unit_file_get_default(arg_runtime_scope, arg_root, ret_name); |
| 63 |
if (r == -ERFKILL) |
| 64 |
return log_error_errno(r, "Failed to get default target: Unit file is masked."); |
| 65 |
if (r < 0) |
| 66 |
return log_error_errno(r, "Failed to get default target: %m"); |
| 67 |
return 0; |
| 68 |
|
| 69 |
} else { |
| 70 |
sd_bus *bus; |
| 71 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 72 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 73 |
const char *name; |
| 74 |
|
| 75 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
|
| 79 |
r = bus_call_method(bus, bus_systemd_mgr, "GetDefaultTarget", &error, &reply, NULL); |
| 80 |
if (r < 0) |
| 81 |
return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); |
| 82 |
|
| 83 |
r = sd_bus_message_read(reply, "s", &name); |
| 84 |
if (r < 0) |
| 85 |
return bus_log_parse_error(r); |
| 86 |
|
| 87 |
return free_and_strdup_warn(ret_name, name); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
int verb_get_default(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 92 |
_cleanup_free_ char *name = NULL; |
| 93 |
int r; |
| 94 |
|
| 95 |
r = determine_default(&name); |
| 96 |
if (r < 0) |
| 97 |
return r; |
| 98 |
|
| 99 |
printf("%s\n", name); |
| 100 |
|
| 101 |
emit_cmdline_warning(); |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
int verb_set_default(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 107 |
_cleanup_free_ char *unit = NULL; |
| 108 |
int r; |
| 109 |
|
| 110 |
assert(argc >= 2); |
| 111 |
assert(argv); |
| 112 |
|
| 113 |
r = unit_name_mangle_with_suffix(argv[1], "set-default", |
| 114 |
arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, |
| 115 |
".target", &unit); |
| 116 |
if (r < 0) |
| 117 |
return log_error_errno(r, "Failed to mangle unit name: %m"); |
| 118 |
|
| 119 |
if (install_client_side() != INSTALL_CLIENT_SIDE_NO) { |
| 120 |
InstallChange *changes = NULL; |
| 121 |
size_t n_changes = 0; |
| 122 |
|
| 123 |
CLEANUP_ARRAY(changes, n_changes, install_changes_free); |
| 124 |
|
| 125 |
r = unit_file_set_default(arg_runtime_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes); |
| 126 |
r = install_changes_dump(r, "set default", changes, n_changes, arg_quiet); |
| 127 |
if (r < 0) |
| 128 |
return r; |
| 129 |
} else { |
| 130 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 131 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 132 |
sd_bus *bus; |
| 133 |
|
| 134 |
polkit_agent_open_maybe(); |
| 135 |
|
| 136 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 137 |
if (r < 0) |
| 138 |
return r; |
| 139 |
|
| 140 |
r = bus_call_method(bus, bus_systemd_mgr, "SetDefaultTarget", &error, &reply, "sb", unit, 1); |
| 141 |
if (r < 0) |
| 142 |
return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); |
| 143 |
|
| 144 |
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet); |
| 145 |
if (r < 0) |
| 146 |
return r; |
| 147 |
|
| 148 |
/* Try to reload if enabled */ |
| 149 |
if (!arg_no_reload) { |
| 150 |
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false); |
| 151 |
if (r < 0) |
| 152 |
return r; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
emit_cmdline_warning(); |
| 157 |
|
| 158 |
if (!arg_quiet) { |
| 159 |
_cleanup_free_ char *final = NULL; |
| 160 |
|
| 161 |
r = determine_default(&final); |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
|
| 165 |
if (!streq(final, unit)) |
| 166 |
log_notice("Note: \"%s\" is the default unit (possibly a runtime override).", final); |
| 167 |
} |
| 168 |
|
| 169 |
return 0; |
| 170 |
} |