chiark / gitweb /
systemctl: when calling 'status' accept a PID
[elogind.git] / src / systemctl.c
index 1844638ba234ead83829d8c24579520739d8f98b..cf075ed7a3d6ab186ea23f2a9b8e7fc0c05f996a 100644 (file)
@@ -96,18 +96,31 @@ static bool private_bus = false;
 
 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
 
-static const char *ansi_highlight(bool b) {
+static bool on_tty(void) {
         static int t = -1;
 
         if (_unlikely_(t < 0))
                 t = isatty(STDOUT_FILENO) > 0;
 
-        if (!t)
+        return t;
+}
+
+static const char *ansi_highlight(bool b) {
+
+        if (!on_tty())
                 return "";
 
         return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
 }
 
+static const char *ansi_highlight_green(bool b) {
+
+        if (!on_tty())
+                return "";
+
+        return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
+}
+
 static bool error_is_no_service(DBusError *error) {
 
         assert(error);
@@ -246,14 +259,20 @@ static int list_units(DBusConnection *bus, char **args, unsigned n) {
                     (arg_all || !(streq(active_state, "inactive") || following[0]) || job_id > 0)) {
                         char *e;
                         int a = 0, b = 0;
+                        const char *on, *off;
 
-                        if (streq(active_state, "maintenance"))
-                                fputs(ansi_highlight(true), stdout);
+                        if (streq(active_state, "maintenance")) {
+                                on = ansi_highlight(true);
+                                off = ansi_highlight(false);
+                        } else
+                                on = off = "";
 
                         e = arg_full ? NULL : ellipsize(id, 45, 33);
-                        printf("%-45s %-6s %-12s %-12s%n", e ? e : id, load_state, active_state, sub_state, &a);
+                        printf("%-45s %-6s %s%-12s %-12s%s%n", e ? e : id, load_state, on, active_state, sub_state, off, &a);
                         free(e);
 
+                        a -= strlen(on) + strlen(off);
+
                         if (job_id != 0)
                                 printf(" => %-12s%n", job_type, &b);
                         else
@@ -266,9 +285,6 @@ static int list_units(DBusConnection *bus, char **args, unsigned n) {
                                 printf(" %.*s", columns() - a - b - 2, description);
                         }
 
-                        if (streq(active_state, "maintenance"))
-                                fputs(ansi_highlight(false), stdout);
-
                         fputs("\n", stdout);
                         k++;
                 }
@@ -311,6 +327,7 @@ static int dot_one_property(const char *name, const char *prop, DBusMessageIter
                 "RequisiteOverridable",  "[color=\"darkblue\"]",
                 "Wants",                 "[color=\"darkgrey\"]",
                 "Conflicts",             "[color=\"red\"]",
+                "ConflictedBy",          "[color=\"red\"]",
                 "After",                 "[color=\"green\"]"
         };
 
@@ -440,6 +457,8 @@ static int dot_one(DBusConnection *bus, const char *name, const char *path) {
                 dbus_message_iter_next(&sub);
         }
 
+        r = 0;
+
 finish:
         if (m)
                 dbus_message_unref(m);
@@ -781,7 +800,7 @@ finish:
 }
 
 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
-        DBusMessage *m, *reply;
+        DBusMessage *m = NULL, *reply = NULL;
         dbus_bool_t b = FALSE;
         DBusMessageIter iter, sub;
         const char
@@ -1399,7 +1418,7 @@ typedef struct UnitStatusInfo {
 
         const char *description;
 
-        const char *fragment_path;
+        const char *path;
         const char *default_control_group;
 
         bool need_daemon_reload;
@@ -1434,6 +1453,7 @@ typedef struct UnitStatusInfo {
 
 static void print_status_info(UnitStatusInfo *i) {
         ExecStatusInfo *p;
+        const char *on, *off, *ss;
 
         assert(i);
 
@@ -1447,37 +1467,39 @@ static void print_status_info(UnitStatusInfo *i) {
 
         printf("\n");
 
-        if (i->fragment_path)
-                printf("\t  Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
-        else if (streq_ptr(i->load_state, "failed"))
-                printf("\t  Loaded: %s%s%s\n",
-                       ansi_highlight(true),
-                       strna(i->load_state),
-                       ansi_highlight(false));
+        if (streq_ptr(i->load_state, "failed")) {
+                on = ansi_highlight(true);
+                off = ansi_highlight(false);
+        } else
+                on = off = "";
+
+        if (i->path)
+                printf("\t  Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
         else
-                printf("\t  Loaded: %s\n", strna(i->load_state));
+                printf("\t  Loaded: %s%s%s\n", on, strna(i->load_state), off);
+
+        ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
 
         if (streq_ptr(i->active_state, "maintenance")) {
-                        if (streq_ptr(i->active_state, i->sub_state))
-                                printf("\t  Active: %s%s%s\n",
-                                       ansi_highlight(true),
-                                       strna(i->active_state),
-                                       ansi_highlight(false));
-                        else
-                                printf("\t  Active: %s%s (%s)%s\n",
-                                       ansi_highlight(true),
-                                       strna(i->active_state),
-                                       strna(i->sub_state),
-                                       ansi_highlight(false));
-        } else {
-                if (streq_ptr(i->active_state, i->sub_state))
-                        printf("\t  Active: %s\n",
-                               strna(i->active_state));
-                else
-                        printf("\t  Active: %s (%s)\n",
-                               strna(i->active_state),
-                               strna(i->sub_state));
-        }
+                on = ansi_highlight(true);
+                off = ansi_highlight(false);
+        } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
+                on = ansi_highlight_green(true);
+                off = ansi_highlight_green(false);
+        } else
+                on = off = "";
+
+        if (ss)
+                printf("\t  Active: %s%s (%s)%s\n",
+                       on,
+                       strna(i->active_state),
+                       ss,
+                       off);
+        else
+                printf("\t  Active: %s%s%s\n",
+                       on,
+                       strna(i->active_state),
+                       off);
 
         if (i->sysfs_path)
                 printf("\t  Device: %s\n", i->sysfs_path);
@@ -1529,14 +1551,15 @@ static void print_status_info(UnitStatusInfo *i) {
                                         printf(" (%s)", t);
                                         free(t);
                                 }
-                        } else {
+                        } else if (i->exit_code > 0) {
                                 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
 
                                 if (i->exit_code == CLD_EXITED)
                                         printf("status=%i", i->exit_status);
                                 else
                                         printf("signal=%s", signal_to_string(i->exit_status));
-                                printf(")");                        }
+                                printf(")");
+                        }
                 }
 
                 if (i->main_pid > 0 && i->control_pid > 0)
@@ -1601,7 +1624,9 @@ static int status_property(const char *name, DBusMessageIter *iter, UnitStatusIn
                         else if (streq(name, "Description"))
                                 i->description = s;
                         else if (streq(name, "FragmentPath"))
-                                i->fragment_path = s;
+                                i->path = s;
+                        else if (streq(name, "SysVPath"))
+                                i->path = s;
                         else if (streq(name, "DefaultControlGroup"))
                                 i->default_control_group = s;
                         else if (streq(name, "StatusText"))
@@ -2096,7 +2121,9 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
                 const char *path = NULL;
                 uint32_t id;
 
-                if (!show_properties || safe_atou32(args[i], &id) < 0) {
+                if (safe_atou32(args[i], &id) < 0) {
+
+                        /* Interpret as unit name */
 
                         if (!(m = dbus_message_new_method_call(
                                               "org.freedesktop.systemd1",
@@ -2152,7 +2179,9 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
                                 }
                         }
 
-                } else {
+                } else if (show_properties) {
+
+                        /* Interpret as job id */
 
                         if (!(m = dbus_message_new_method_call(
                                               "org.freedesktop.systemd1",
@@ -2172,6 +2201,33 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
                                 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 {
+
+                        /* Interpret as PID */
+
+                        if (!(m = dbus_message_new_method_call(
+                                              "org.freedesktop.systemd1",
+                                              "/org/freedesktop/systemd1",
+                                              "org.freedesktop.systemd1.Manager",
+                                              "GetUnitByPID"))) {
+                                log_error("Could not allocate message.");
+                                r = -ENOMEM;
+                                goto finish;
+                        }
+
+                        if (!dbus_message_append_args(m,
+                                                      DBUS_TYPE_UINT32, &id,
+                                                      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;
@@ -3134,7 +3190,7 @@ static int remove_marked_symlinks_fd(int fd, const char *config_path, const char
                         free(p);
 
                         if (r == 0)
-                                q = r;
+                                r = q;
 
                 } else if (is_link) {
                         char *p, *dest, *c;
@@ -4510,11 +4566,10 @@ static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError
 }
 
 static int reload_with_fallback(DBusConnection *bus) {
-        int r;
 
         if (bus) {
                 /* First, try systemd via D-Bus. */
-                if ((r = daemon_reload(bus, NULL, 0)) > 0)
+                if (daemon_reload(bus, NULL, 0) > 0)
                         return 0;
         }
 
@@ -4530,22 +4585,21 @@ static int reload_with_fallback(DBusConnection *bus) {
 }
 
 static int start_with_fallback(DBusConnection *bus) {
-        int r;
 
         if (bus) {
                 /* First, try systemd via D-Bus. */
-                if ((r = start_unit(bus, NULL, 0)) > 0)
+                if (start_unit(bus, NULL, 0) > 0)
                         goto done;
         }
 
         /* Hmm, talking to systemd via D-Bus didn't work. Then
          * let's try to talk to Upstart via D-Bus. */
-        if ((r = talk_upstart()) > 0)
+        if (talk_upstart() > 0)
                 goto done;
 
         /* Nothing else worked, so let's try
          * /dev/initctl */
-        if ((r = talk_initctl()) != 0)
+        if (talk_initctl() != 0)
                 goto done;
 
         log_error("Failed to talk to init daemon.");
@@ -4584,17 +4638,17 @@ static int halt_main(DBusConnection *bus) {
         switch (arg_action) {
 
         case ACTION_HALT:
-                log_info("Halting");
+                log_info("Halting.");
                 reboot(RB_HALT_SYSTEM);
                 break;
 
         case ACTION_POWEROFF:
-                log_info("Powering off");
+                log_info("Powering off.");
                 reboot(RB_POWER_OFF);
                 break;
 
         case ACTION_REBOOT:
-                log_info("Rebooting");
+                log_info("Rebooting.");
                 reboot(RB_AUTOBOOT);
                 break;
 
@@ -4610,7 +4664,7 @@ static int runlevel_main(void) {
         int r, runlevel, previous;
 
         if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
-                printf("unknown");
+                printf("unknown\n");
                 return r;
         }