chiark / gitweb /
cgls: fix missing initialization
[elogind.git] / src / systemctl.c
index 76f6b84aa59e2f8589d1808333afde9d85a01db7..a9a975ee8188648ceb775d5b3ce0e6e1bc801986 100644 (file)
@@ -41,6 +41,8 @@
 #include "special.h"
 #include "initreq.h"
 #include "strv.h"
+#include "dbus-common.h"
+#include "cgroup-show.h"
 
 static const char *arg_type = NULL;
 static const char *arg_property = NULL;
@@ -74,6 +76,8 @@ enum action {
         _ACTION_MAX
 } arg_action = ACTION_SYSTEMCTL;
 
+static bool private_bus = false;
+
 static bool error_is_no_service(DBusError *error) {
 
         assert(error);
@@ -106,57 +110,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;
-
-        if (parsed_columns > 0)
-                return parsed_columns;
-
-        if ((e = getenv("COLUMNS")))
-                parsed_columns = atoi(e);
-
-        if (parsed_columns <= 0) {
-                struct winsize ws;
-                zero(ws);
-
-                if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
-                        parsed_columns = ws.ws_col;
-        }
-
-        if (parsed_columns <= 0)
-                parsed_columns = 80;
-
-        return parsed_columns;
-
-}
-
 static void warn_wall(enum action action) {
         static const char *table[_ACTION_MAX] = {
                 [ACTION_HALT]      = "The system is going down for system halt NOW!",
@@ -262,6 +215,9 @@ static int list_units(DBusConnection *bus, char **args, unsigned n) {
 
                         int a = 0, b = 0;
 
+                        if (streq(active_state, "maintenance"))
+                                fputs(ANSI_HIGHLIGHT_ON, stdout);
+
                         printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
 
                         if (job_id != 0)
@@ -273,9 +229,12 @@ static int list_units(DBusConnection *bus, char **args, unsigned n) {
                                 if (job_id == 0)
                                         printf("                ");
 
-                                printf("%.*s", columns() - a - b - 2, description);
+                                printf(" %.*s", columns() - a - b - 2, description);
                         }
 
+                        if (streq(active_state, "maintenance"))
+                                fputs(ANSI_HIGHLIGHT_OFF, stdout);
+
                         fputs("\n", stdout);
                         k++;
                 }
@@ -546,10 +505,10 @@ static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *me
 
         dbus_error_init(&error);
 
-        /* log_debug("Got D-Bus request: %s.%s() on %s", */
-        /*           dbus_message_get_interface(message), */
-        /*           dbus_message_get_member(message), */
-        /*           dbus_message_get_path(message)); */
+        log_debug("Got D-Bus request: %s.%s() on %s",
+                  dbus_message_get_interface(message),
+                  dbus_message_get_member(message),
+                  dbus_message_get_path(message));
 
         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
                 log_error("Warning! D-Bus connection terminated.");
@@ -586,6 +545,9 @@ static int enable_wait_for_jobs(DBusConnection *bus) {
 
         assert(bus);
 
+        if (private_bus)
+                return 0;
+
         dbus_error_init(&error);
         dbus_bus_add_match(bus,
                            "type='signal',"
@@ -881,6 +843,7 @@ static int check_unit(DBusConnection *bus, char **args, unsigned n) {
                         if (!arg_quiet)
                                 puts("unknown");
 
+                        dbus_error_free(&error);
                         continue;
                 }
 
@@ -959,43 +922,6 @@ finish:
         return r;
 }
 
-static void show_cgroup(const char *name) {
-        char *fn, *pids;
-        int r;
-        char *p;
-
-        if (!startswith(name, "name=systemd:"))
-                return;
-
-        if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0)
-                return;
-
-        r = read_one_line_file(fn, &pids);
-        free(fn);
-
-        if (r < 0)
-                return;
-
-        p = pids;
-        while (p[0]) {
-                unsigned long ul;
-                char *t = NULL;
-
-                p += strspn(p, WHITESPACE);
-
-                errno = 0;
-                ul = strtoul(p, &p, 0);
-                if (errno != 0 || ul <= 0)
-                        break;
-
-                get_process_cmdline((pid_t) ul, 60, &t);
-                printf("\t\t%lu %s\n", ul, strna(t));
-                free(t);
-        }
-
-        free(pids);
-}
-
 typedef struct UnitStatusInfo {
         const char *id;
         const char *load_state;
@@ -1117,8 +1043,16 @@ static void print_status_info(UnitStatusInfo *i) {
         }
 
         if (i->default_control_group) {
+                unsigned c;
+
                 printf("\t  CGroup: %s\n", i->default_control_group);
-                show_cgroup(i->default_control_group);
+
+                if ((c = columns()) > 18)
+                        c -= 18;
+                else
+                        c = 0;
+
+                show_cgroup_recursive(i->default_control_group, "\t\t  ", c);
         }
 }
 
@@ -1632,6 +1566,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 +1621,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,
@@ -1698,10 +1668,10 @@ static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage
 
         dbus_error_init(&error);
 
-        /* log_debug("Got D-Bus request: %s.%s() on %s", */
-        /*           dbus_message_get_interface(message), */
-        /*           dbus_message_get_member(message), */
-        /*           dbus_message_get_path(message)); */
+        log_debug("Got D-Bus request: %s.%s() on %s",
+                  dbus_message_get_interface(message),
+                  dbus_message_get_member(message),
+                  dbus_message_get_path(message));
 
         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
                 log_error("Warning! D-Bus connection terminated.");
@@ -1827,43 +1797,45 @@ static int monitor(DBusConnection *bus, char **args, unsigned n) {
 
         dbus_error_init(&error);
 
-        dbus_bus_add_match(bus,
-                           "type='signal',"
-                           "sender='org.freedesktop.systemd1',"
-                           "interface='org.freedesktop.systemd1.Manager',"
-                           "path='/org/freedesktop/systemd1'",
-                           &error);
+        if (!private_bus) {
+                dbus_bus_add_match(bus,
+                                   "type='signal',"
+                                   "sender='org.freedesktop.systemd1',"
+                                   "interface='org.freedesktop.systemd1.Manager',"
+                                   "path='/org/freedesktop/systemd1'",
+                                   &error);
 
-        if (dbus_error_is_set(&error)) {
-                log_error("Failed to add match: %s", error.message);
-                r = -EIO;
-                goto finish;
-        }
+                if (dbus_error_is_set(&error)) {
+                        log_error("Failed to add match: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
 
-        dbus_bus_add_match(bus,
-                           "type='signal',"
-                           "sender='org.freedesktop.systemd1',"
-                           "interface='org.freedesktop.systemd1.Unit',"
-                           "member='Changed'",
-                           &error);
+                dbus_bus_add_match(bus,
+                                   "type='signal',"
+                                   "sender='org.freedesktop.systemd1',"
+                                   "interface='org.freedesktop.systemd1.Unit',"
+                                   "member='Changed'",
+                                   &error);
 
-        if (dbus_error_is_set(&error)) {
-                log_error("Failed to add match: %s", error.message);
-                r = -EIO;
-                goto finish;
-        }
+                if (dbus_error_is_set(&error)) {
+                        log_error("Failed to add match: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
 
-        dbus_bus_add_match(bus,
-                           "type='signal',"
-                           "sender='org.freedesktop.systemd1',"
-                           "interface='org.freedesktop.systemd1.Job',"
-                           "member='Changed'",
-                           &error);
+                dbus_bus_add_match(bus,
+                                   "type='signal',"
+                                   "sender='org.freedesktop.systemd1',"
+                                   "interface='org.freedesktop.systemd1.Job',"
+                                   "member='Changed'",
+                                   &error);
 
-        if (dbus_error_is_set(&error)) {
-                log_error("Failed to add match: %s", error.message);
-                r = -EIO;
-                goto finish;
+                if (dbus_error_is_set(&error)) {
+                        log_error("Failed to add match: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
         }
 
         if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
@@ -3072,7 +3044,7 @@ static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
                 { "reboot",            EQUAL, 1, start_special   },
                 { "default",           EQUAL, 1, start_special   },
                 { "rescue",            EQUAL, 1, start_special   },
-                { "emergency",         EQUAL, 1, start_special   },
+                { "emergency",         EQUAL, 1, start_special   }
         };
 
         int left;
@@ -3264,19 +3236,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, &private_bus, &error);
 
         switch (arg_action) {