chiark / gitweb /
main: add a native implementation of the 'nomodules' kernel option understood by...
[elogind.git] / src / systemctl.c
index d32a688d8415d6045c5297203c592e0e8bf986d8..02c8e30418b6e49b312b6e5b8baa6d6be705d163 100644 (file)
@@ -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;
@@ -881,6 +856,7 @@ static int check_unit(DBusConnection *bus, char **args, unsigned n) {
                         if (!arg_quiet)
                                 puts("unknown");
 
+                        dbus_error_free(&error);
                         continue;
                 }
 
@@ -959,6 +935,54 @@ finish:
         return r;
 }
 
+static void show_cgroup(const char *name) {
+        char *fn;
+        FILE *f;
+        pid_t last = 0;
+
+        if (!startswith(name, "name=systemd:"))
+                return;
+
+        if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0)
+                return;
+
+        f = fopen(fn, "r");
+        free(fn);
+
+        if (!f)
+                return;
+
+        printf("\t\t  \342\224\202\n");
+
+        while (!feof(f)) {
+                unsigned long ul;
+
+                if (fscanf(f, "%lu", &ul) != 1)
+                        break;
+
+                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);
+                }
+
+                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);
+        }
+
+        fclose(f);
+}
+
 typedef struct UnitStatusInfo {
         const char *id;
         const char *load_state;
@@ -1079,8 +1103,10 @@ static void print_status_info(UnitStatusInfo *i) {
                 printf("\n");
         }
 
-        if (i->default_control_group)
+        if (i->default_control_group) {
                 printf("\t  CGroup: %s\n", i->default_control_group);
+                show_cgroup(i->default_control_group);
+        }
 }
 
 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
@@ -1593,6 +1619,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(
@@ -1612,12 +1674,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,
@@ -3225,19 +3287,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) {