X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fsystemctl.c;h=5c91c325cd2d5ae8b5012248dbb163f35ad7f117;hb=7e61bf2019c2cab4e1e21ee775bab25ef1a1bb81;hp=76f6b84aa59e2f8589d1808333afde9d85a01db7;hpb=c59760eedae9d9de3be1572b9b612dfd8cc37547;p=elogind.git diff --git a/src/systemctl.c b/src/systemctl.c index 76f6b84aa..5c91c325c 100644 --- a/src/systemctl.c +++ b/src/systemctl.c @@ -41,6 +41,7 @@ #include "special.h" #include "initreq.h" #include "strv.h" +#include "dbus-common.h" static const char *arg_type = NULL; static const char *arg_property = NULL; @@ -106,32 +107,6 @@ static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *da return 0; } -static int bus_check_peercred(DBusConnection *c) { - int fd; - struct ucred ucred; - socklen_t l; - - assert(c); - - assert_se(dbus_connection_get_unix_fd(c, &fd)); - - l = sizeof(struct ucred); - if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) { - log_error("SO_PEERCRED failed: %m"); - return -errno; - } - - if (l != sizeof(struct ucred)) { - log_error("SO_PEERCRED returned wrong size."); - return -E2BIG; - } - - if (ucred.uid != 0) - return -EPERM; - - return 1; -} - static int columns(void) { static int parsed_columns = 0; const char *e; @@ -267,7 +242,7 @@ static int list_units(DBusConnection *bus, char **args, unsigned n) { if (job_id != 0) printf(" %-15s%n", job_type, &b); else - b = 1 + 15; + b = 1 + 16; if (a + b + 2 < columns()) { if (job_id == 0) @@ -881,6 +856,7 @@ static int check_unit(DBusConnection *bus, char **args, unsigned n) { if (!arg_quiet) puts("unknown"); + dbus_error_free(&error); continue; } @@ -960,9 +936,9 @@ finish: } static void show_cgroup(const char *name) { - char *fn, *pids; - int r; - char *p; + char *fn; + FILE *f; + pid_t last = 0; if (!startswith(name, "name=systemd:")) return; @@ -970,30 +946,40 @@ static void show_cgroup(const char *name) { if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0) return; - r = read_one_line_file(fn, &pids); + f = fopen(fn, "r"); free(fn); - if (r < 0) + if (!f) return; - p = pids; - while (p[0]) { + while (!feof(f)) { unsigned long ul; - char *t = NULL; - - p += strspn(p, WHITESPACE); - errno = 0; - ul = strtoul(p, &p, 0); - if (errno != 0 || ul <= 0) + if (fscanf(f, "%lu", &ul) != 1) break; - get_process_cmdline((pid_t) ul, 60, &t); - printf("\t\t%lu %s\n", ul, strna(t)); + if (ul <= 0) + continue; + + if (last > 0) { + char *t = NULL; + get_process_cmdline(last, 60, &t); + printf("\t\t \342\224\234 %lu %s\n", (unsigned long) last, strna(t)); + free(t); + } else + printf("\t\t \342\224\202\n"); + + last = (pid_t) ul; + } + + if (last > 0) { + char *t = NULL; + get_process_cmdline(last, 60, &t); + printf("\t\t \342\224\224 %lu %s\n", (unsigned long) last, strna(t)); free(t); } - free(pids); + fclose(f); } typedef struct UnitStatusInfo { @@ -1632,6 +1618,42 @@ static int show(DBusConnection *bus, char **args, unsigned n) { goto finish; } + if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { + + if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) { + log_error("Failed to issue method call: %s", error.message); + r = -EIO; + goto finish; + } + + dbus_error_free(&error); + + dbus_message_unref(m); + if (!(m = dbus_message_new_method_call( + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + "GetUnit"))) { + log_error("Could not allocate message."); + r = -ENOMEM; + goto finish; + } + + if (!dbus_message_append_args(m, + DBUS_TYPE_STRING, &args[i], + DBUS_TYPE_INVALID)) { + log_error("Could not append arguments to message."); + r = -ENOMEM; + goto finish; + } + + if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { + log_error("Failed to issue method call: %s", error.message); + r = -EIO; + goto finish; + } + } + } else { if (!(m = dbus_message_new_method_call( @@ -1651,12 +1673,12 @@ static int show(DBusConnection *bus, char **args, unsigned n) { r = -ENOMEM; goto finish; } - } - if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { - log_error("Failed to issue method call: %s", error.message); - r = -EIO; - goto finish; + if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) { + log_error("Failed to issue method call: %s", error.message); + r = -EIO; + goto finish; + } } if (!dbus_message_get_args(reply, &error, @@ -3264,19 +3286,7 @@ int main(int argc, char*argv[]) { goto finish; } - /* If we are root, then let's not go via the bus */ - if (geteuid() == 0 && !arg_session) { - bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error); - - if (bus && bus_check_peercred(bus) < 0) { - log_error("Failed to verify owner of bus."); - goto finish; - } - } else - bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error); - - if (bus) - dbus_connection_set_exit_on_disconnect(bus, FALSE); + bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &error); switch (arg_action) {