X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fsystemctl%2Fsystemctl.c;h=10213afbc244a2c1b892b3d685a06828a5edd28c;hp=773b4ccd87f19e139023adf2a00bbfe2086c7bf8;hb=95d383ee47db488f182048cfd6846f2e6b859f2b;hpb=1cfa9a4cbb41ef770614fa97961cc056a63c1b7c diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 773b4ccd8..10213afbc 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -22,21 +22,16 @@ #include #include -#include #include #include #include #include #include #include -#include -#include #include #include #include -#include #include -#include #include "sd-daemon.h" #include "sd-shutdown.h" @@ -55,7 +50,6 @@ #include "cgroup-util.h" #include "list.h" #include "path-lookup.h" -#include "conf-parser.h" #include "exit-status.h" #include "build.h" #include "unit-name.h" @@ -1327,7 +1321,6 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) { static int list_unit_files(sd_bus *bus, char **args) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_free_ UnitFileList *units = NULL; UnitFileList *unit; size_t size = 0; @@ -1374,6 +1367,8 @@ static int list_unit_files(sd_bus *bus, char **args) { assert(c <= n_units); hashmap_free(h); } else { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + r = sd_bus_call_method( bus, "org.freedesktop.systemd1", @@ -1882,7 +1877,6 @@ static int list_machines(sd_bus *bus, char **args) { static int get_default(sd_bus *bus, char **args) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_free_ char *_path = NULL; const char *path; int r; @@ -1894,6 +1888,8 @@ static int get_default(sd_bus *bus, char **args) { path = _path; } else { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + r = sd_bus_call_method( bus, "org.freedesktop.systemd1", @@ -1952,28 +1948,20 @@ static int set_default(sd_bus *bus, char **args) { r = 0; } else { - _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - r = sd_bus_message_new_method_call( + polkit_agent_open_if_enabled(); + + r = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "SetDefaultTarget"); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_append(m, "sb", unit, 1); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_call(bus, m, 0, &error, &reply); + "SetDefaultTarget", + &error, + &reply, + "sb", unit, 1); if (r < 0) { log_error("Failed to set default target: %s", bus_error_message(&error, -r)); return r; @@ -2130,7 +2118,6 @@ static int list_jobs(sd_bus *bus, char **args) { } static int cancel_job(sd_bus *bus, char **args) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; char **name; int r = 0; @@ -2139,8 +2126,10 @@ static int cancel_job(sd_bus *bus, char **args) { if (strv_length(args) <= 1) return daemon_reload(bus, args); + polkit_agent_open_if_enabled(); + STRV_FOREACH(name, args+1) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; uint32_t id; int q; @@ -2148,25 +2137,15 @@ static int cancel_job(sd_bus *bus, char **args) { if (q < 0) return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); - q = sd_bus_message_new_method_call( + q = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "CancelJob"); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (q < 0) - return bus_log_create_error(1); - - q = sd_bus_message_append(m, "u", id); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_call(bus, m, 0, &error, NULL); + "CancelJob", + &error, + NULL, + "u", id); if (q < 0) { log_error("Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); if (r == 0) @@ -2250,12 +2229,13 @@ static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **un return 0; } -static int unit_find_paths(sd_bus *bus, - const char *unit_name, - bool avoid_bus_cache, - LookupPaths *lp, - char **fragment_path, - char ***dropin_paths) { +static int unit_find_paths( + sd_bus *bus, + const char *unit_name, + bool avoid_bus_cache, + LookupPaths *lp, + char **fragment_path, + char ***dropin_paths) { _cleanup_free_ char *path = NULL; _cleanup_strv_free_ char **dropins = NULL; @@ -2556,7 +2536,7 @@ static int start_unit_one( sd_bus_error *error, BusWaitForJobs *w) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; const char *path; int r; @@ -2567,25 +2547,15 @@ static int start_unit_one( log_debug("Calling manager for %s on %s, %s", method, name, mode); - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - method); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_append(m, "ss", name, mode); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_call(bus, m, 0, error, &reply); + method, + error, + &reply, + "ss", name, mode); if (r < 0) { const char *verb; @@ -2674,10 +2644,10 @@ static const struct { [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, - [ACTION_RUNLEVEL2] = { SPECIAL_RUNLEVEL2_TARGET, NULL, "isolate" }, - [ACTION_RUNLEVEL3] = { SPECIAL_RUNLEVEL3_TARGET, NULL, "isolate" }, - [ACTION_RUNLEVEL4] = { SPECIAL_RUNLEVEL4_TARGET, NULL, "isolate" }, - [ACTION_RUNLEVEL5] = { SPECIAL_RUNLEVEL5_TARGET, NULL, "isolate" }, + [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, + [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, + [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, + [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, @@ -2881,6 +2851,9 @@ static int check_inhibitors(sd_bus *bus, enum action a) { if (!sv) return log_oom(); + if ((pid_t) pid < 0) + return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid); + if (!strv_contains(sv, a == ACTION_HALT || a == ACTION_POWEROFF || @@ -2892,7 +2865,7 @@ static int check_inhibitors(sd_bus *bus, enum action a) { user = uid_to_name(uid); log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", - who, pid, strna(comm), strna(user), why); + who, (pid_t) pid, strna(comm), strna(user), why); c++; } @@ -3029,7 +3002,6 @@ static int check_unit_failed(sd_bus *bus, char **args) { } static int kill_unit(sd_bus *bus, char **args) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_strv_free_ char **names = NULL; char **name; int r, q; @@ -3047,27 +3019,17 @@ static int kill_unit(sd_bus *bus, char **args) { log_error_errno(r, "Failed to expand names: %m"); STRV_FOREACH(name, names) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - q = sd_bus_message_new_method_call( + q = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "KillUnit"); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_message_append(m, "ssi", *names, arg_kill_who, arg_signal); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_call(bus, m, 0, &error, NULL); + "KillUnit", + &error, + NULL, + "ssi", *names, arg_kill_who, arg_signal); if (q < 0) { log_error("Failed to kill unit %s: %s", *names, bus_error_message(&error, q)); if (r == 0) @@ -4552,6 +4514,23 @@ static int init_home_and_lookup_paths(char **user_home, char **user_runtime, Loo return 0; } +static int cat_file(const char *filename, bool newline) { + _cleanup_close_ int fd; + + fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY); + if (fd < 0) + return -errno; + + printf("%s%s# %s%s\n", + newline ? "\n" : "", + ansi_highlight_blue(), + filename, + ansi_highlight_off()); + fflush(stdout); + + return copy_bytes(fd, STDOUT_FILENO, (off_t) -1, false); +} + static int cat(sd_bus *bus, char **args) { _cleanup_free_ char *user_home = NULL; _cleanup_free_ char *user_runtime = NULL; @@ -4597,32 +4576,15 @@ static int cat(sd_bus *bus, char **args) { puts(""); if (fragment_path) { - printf("%s# %s%s\n", - ansi_highlight_blue(), - fragment_path, - ansi_highlight_off()); - fflush(stdout); - - r = copy_file_fd(fragment_path, STDOUT_FILENO, false); - if (r < 0) { - log_warning_errno(r, "Failed to cat %s: %m", fragment_path); - continue; - } + r = cat_file(fragment_path, false); + if (r < 0) + return log_warning_errno(r, "Failed to cat %s: %m", fragment_path); } STRV_FOREACH(path, dropin_paths) { - printf("%s%s# %s%s\n", - isempty(fragment_path) && path == dropin_paths ? "" : "\n", - ansi_highlight_blue(), - *path, - ansi_highlight_off()); - fflush(stdout); - - r = copy_file_fd(*path, STDOUT_FILENO, false); - if (r < 0) { - log_warning_errno(r, "Failed to cat %s: %m", *path); - continue; - } + r = cat_file(*path, path == dropin_paths); + if (r < 0) + return log_warning_errno(r, "Failed to cat %s: %m", *path); } } @@ -4648,10 +4610,6 @@ static int set_property(sd_bus *bus, char **args) { if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - n = unit_name_mangle(args[1], MANGLE_NOGLOB); if (!n) return log_oom(); @@ -4693,7 +4651,7 @@ static int set_property(sd_bus *bus, char **args) { static int snapshot(sd_bus *bus, char **args) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_free_ char *n = NULL, *id = NULL; const char *path; int r; @@ -4707,25 +4665,15 @@ static int snapshot(sd_bus *bus, char **args) { if (!n) return log_oom(); - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "CreateSnapshot"); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_append(m, "sb", n, false); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_call(bus, m, 0, &error, &reply); + "CreateSnapshot", + &error, + &reply, + "sb", n, false); if (r < 0) { log_error("Failed to create snapshot: %s", bus_error_message(&error, r)); return r; @@ -4755,7 +4703,6 @@ static int snapshot(sd_bus *bus, char **args) { } static int delete_snapshot(sd_bus *bus, char **args) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_strv_free_ char **names = NULL; char **name; int r; @@ -4769,28 +4716,18 @@ static int delete_snapshot(sd_bus *bus, char **args) { log_error_errno(r, "Failed to expand names: %m"); STRV_FOREACH(name, names) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; int q; - q = sd_bus_message_new_method_call( + q = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "RemoveSnapshot"); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_message_append(m, "s", *name); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_call(bus, m, 0, &error, NULL); + "RemoveSnapshot", + &error, + NULL, + "s", *name); if (q < 0) { log_error("Failed to remove snapshot %s: %s", *name, bus_error_message(&error, q)); if (r == 0) @@ -4803,7 +4740,6 @@ static int delete_snapshot(sd_bus *bus, char **args) { static int daemon_reload(sd_bus *bus, char **args) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_message_unref_ sd_bus_message *m = NULL; const char *method; int r; @@ -4829,21 +4765,15 @@ static int daemon_reload(sd_bus *bus, char **args) { /* "daemon-reload" */ "Reload"; } - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - method); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_call(bus, m, 0, &error, NULL); + method, + &error, + NULL, + NULL); if (r == -ENOENT && arg_action != ACTION_SYSTEMCTL) /* There's always a fallback possible for * legacy actions. */ @@ -4859,7 +4789,6 @@ static int daemon_reload(sd_bus *bus, char **args) { } static int reset_failed(sd_bus *bus, char **args) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_strv_free_ char **names = NULL; char **name; int r, q; @@ -4874,27 +4803,17 @@ static int reset_failed(sd_bus *bus, char **args) { log_error_errno(r, "Failed to expand names: %m"); STRV_FOREACH(name, names) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - q = sd_bus_message_new_method_call( + q = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "ResetFailedUnit"); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_message_append(m, "s", *name); - if (q < 0) - return bus_log_create_error(q); - - q = sd_bus_call(bus, m, 0, &error, NULL); + "ResetFailedUnit", + &error, + NULL, + "s", *name); if (q < 0) { log_error("Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); if (r == 0) @@ -5013,6 +4932,8 @@ static int set_environment(sd_bus *bus, char **args) { assert(bus); assert(args); + polkit_agent_open_if_enabled(); + method = streq(args[0], "set-environment") ? "SetEnvironment" : "UnsetEnvironment"; @@ -5027,10 +4948,6 @@ static int set_environment(sd_bus *bus, char **args) { if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - r = sd_bus_message_append_strv(m, args + 1); if (r < 0) return bus_log_create_error(r); @@ -5052,6 +4969,8 @@ static int import_environment(sd_bus *bus, char **args) { assert(bus); assert(args); + polkit_agent_open_if_enabled(); + r = sd_bus_message_new_method_call( bus, &m, @@ -5062,10 +4981,6 @@ static int import_environment(sd_bus *bus, char **args) { if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - if (strv_isempty(args + 1)) r = sd_bus_message_append_strv(m, environ); else { @@ -5370,10 +5285,6 @@ static int enable_unit(sd_bus *bus, char **args) { if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - r = sd_bus_message_append_strv(m, names); if (r < 0) return bus_log_create_error(r); @@ -5489,10 +5400,6 @@ static int add_dependency(sd_bus *bus, char **args) { if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - r = sd_bus_message_append_strv(m, names); if (r < 0) return bus_log_create_error(r); @@ -5539,35 +5446,23 @@ static int preset_all(sd_bus *bus, char **args) { r = 0; } else { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; polkit_agent_open_if_enabled(); - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, - &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "PresetAllUnitFiles"); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_append( - m, + "PresetAllUnitFiles", + &error, + &reply, "sbb", unit_file_preset_mode_to_string(arg_preset_mode), arg_runtime, arg_force); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_call(bus, m, 0, &error, &reply); if (r < 0) { log_error("Failed to execute operation: %s", bus_error_message(&error, r)); return r; @@ -7204,6 +7099,11 @@ found: } } + /* Increase max number of open files to 16K if we can, we + * might needs this when browsing journal files, which might + * be split up into many files. */ + setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(16384)); + return verb->dispatch(bus, argv + optind); } @@ -7453,14 +7353,12 @@ int main(int argc, char*argv[]) { goto finish; } - /* Increase max number of open files to 16K if we can, we - * might needs this when browsing journal files, which might - * be split up into many files. */ - setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(16384)); - if (!avoid_bus()) r = bus_open_transport_systemd(arg_transport, arg_host, arg_scope != UNIT_FILE_SYSTEM, &bus); + if (bus) + sd_bus_set_allow_interactive_authorization(bus, arg_ask_password); + /* systemctl_main() will print an error message for the bus * connection, but only if it needs to */