chiark / gitweb /
systemctl: fix assertion hit by incorrect comparison
[elogind.git] / src / systemctl / systemctl.c
index b22010dcac78d87c76e04bb80ea623d5b7384a56..7cf51dceb8d5c0d7a67076c3ec80172368b93f4e 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <systemd/sd-daemon.h>
 #include <systemd/sd-shutdown.h>
+#include <systemd/sd-login.h>
 
 #include "log.h"
 #include "util.h"
@@ -671,7 +672,7 @@ static int list_unit_files(DBusConnection *bus, char **args) {
                         UnitFileList *u;
                         const char *state;
 
-                        assert(dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT);
+                        assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT);
 
                         if (c >= n_units) {
                                 UnitFileList *w;
@@ -1041,7 +1042,7 @@ static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
         /* We ignore all errors here, since this is used to show a warning only */
 
         n = unit_name_mangle(unit);
-        if (n)
+        if (!n)
                 return log_oom();
 
         r = bus_method_call_with_reply (
@@ -1120,7 +1121,6 @@ static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *me
         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
                 uint32_t id;
                 const char *path, *result, *unit;
-                dbus_bool_t success = true;
 
                 if (dbus_message_get_args(message, &error,
                                           DBUS_TYPE_UINT32, &id,
@@ -1128,10 +1128,8 @@ static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *me
                                           DBUS_TYPE_STRING, &unit,
                                           DBUS_TYPE_STRING, &result,
                                           DBUS_TYPE_INVALID)) {
-                        char *p;
 
-                        p = set_remove(d->set, (char*) path);
-                        free(p);
+                        free(set_remove(d->set, (char*) path));
 
                         if (!isempty(result))
                                 d->result = strdup(result);
@@ -1148,40 +1146,16 @@ static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *me
                                           DBUS_TYPE_OBJECT_PATH, &path,
                                           DBUS_TYPE_STRING, &result,
                                           DBUS_TYPE_INVALID)) {
-                        char *p;
-
                         /* Compatibility with older systemd versions <
                          * 183 during upgrades. This should be dropped
                          * one day. */
-                        p = set_remove(d->set, (char*) path);
-                        free(p);
+                        free(set_remove(d->set, (char*) path));
 
                         if (*result)
                                 d->result = strdup(result);
 
                         goto finish;
                 }
-
-                dbus_error_free(&error);
-                if (dbus_message_get_args(message, &error,
-                                          DBUS_TYPE_UINT32, &id,
-                                          DBUS_TYPE_OBJECT_PATH, &path,
-                                          DBUS_TYPE_BOOLEAN, &success,
-                                          DBUS_TYPE_INVALID)) {
-                        char *p;
-
-                        /* Compatibility with older systemd versions <
-                         * 19 during upgrades. This should be dropped
-                         * one day */
-
-                        p = set_remove(d->set, (char*) path);
-                        free(p);
-
-                        if (!success)
-                                d->result = strdup("failed");
-
-                        goto finish;
-                }
 #endif
 
                 log_error("Failed to parse message: %s", bus_error_message(&error));
@@ -1721,6 +1695,8 @@ static int check_inhibitors(DBusConnection *bus, enum action a) {
         DBusMessageIter iter, sub, sub2;
         int r;
         unsigned c = 0;
+        _cleanup_strv_free_ char **sessions = NULL;
+        char **s;
 
         if (!bus)
                 return 0;
@@ -1762,7 +1738,7 @@ static int check_inhibitors(DBusConnection *bus, enum action a) {
                 const char *what, *who, *why, *mode;
                 uint32_t uid, pid;
                 _cleanup_strv_free_ char **sv = NULL;
-                _cleanup_free_ char *comm = NULL;
+                _cleanup_free_ char *comm = NULL, *user = NULL;
 
                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
                         log_error("Failed to parse reply.");
@@ -1796,7 +1772,9 @@ static int check_inhibitors(DBusConnection *bus, enum action a) {
                         goto next;
 
                 get_process_comm(pid, &comm);
-                log_warning("Operation inhibited by \"%s\" (PID %lu \"%s\", UID %lu), reason is \"%s\".", who, (unsigned long) pid, strna(comm), (unsigned long) uid, why);
+                user = uid_to_name(uid);
+                log_warning("Operation inhibited by \"%s\" (PID %lu \"%s\", user %s), reason is \"%s\".",
+                            who, (unsigned long) pid, strna(comm), strna(user), why);
                 c++;
 
         next:
@@ -1805,10 +1783,34 @@ static int check_inhibitors(DBusConnection *bus, enum action a) {
 
         dbus_message_iter_recurse(&iter, &sub);
 
+        /* Check for current sessions */
+        sd_get_sessions(&sessions);
+        STRV_FOREACH(s, sessions) {
+                uid_t uid;
+                _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
+
+                if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
+                        continue;
+
+                if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
+                        continue;
+
+                if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
+                        continue;
+
+                sd_session_get_tty(*s, &tty);
+                sd_session_get_seat(*s, &seat);
+                sd_session_get_service(*s, &service);
+                user = uid_to_name(uid);
+
+                log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
+                c++;
+        }
+
         if (c <= 0)
                 return 0;
 
-        log_error("Please try again after closing inhibitors or ignore them with 'systemctl %s -i'.",
+        log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
                   a == ACTION_HALT ? "halt" :
                   a == ACTION_POWEROFF ? "poweroff" :
                   a == ACTION_REBOOT ? "reboot" :
@@ -3173,17 +3175,19 @@ static int snapshot(DBusConnection *bus, char **args) {
         dbus_bool_t cleanup = FALSE;
         DBusMessageIter iter, sub;
         const char
-                *name = "", *path, *id,
+                *path, *id,
                 *interface = "org.freedesktop.systemd1.Unit",
                 *property = "Id";
         _cleanup_free_ char *n = NULL;
 
         dbus_error_init(&error);
 
-        if (strv_length(args) > 1) {
-                name = args[1];
-                n = unit_name_mangle(name);
-        }
+        if (strv_length(args) > 1)
+                n = snapshot_name_mangle(args[1]);
+        else
+                n = strdup("");
+        if (!n)
+                return log_oom();
 
         r = bus_method_call_with_reply (
                         bus,
@@ -3193,18 +3197,18 @@ static int snapshot(DBusConnection *bus, char **args) {
                         "CreateSnapshot",
                         &reply,
                         NULL,
-                        DBUS_TYPE_STRING, n ? (const char**) &n : &name,
+                        DBUS_TYPE_STRING, &n,
                         DBUS_TYPE_BOOLEAN, &cleanup,
                         DBUS_TYPE_INVALID);
         if (r < 0)
-                goto finish;
+                return r;
 
         if (!dbus_message_get_args(reply, &error,
                                    DBUS_TYPE_OBJECT_PATH, &path,
                                    DBUS_TYPE_INVALID)) {
                 log_error("Failed to parse reply: %s", bus_error_message(&error));
-                r = -EIO;
-                goto finish;
+                dbus_error_free(&error);
+                return -EIO;
         }
 
         dbus_message_unref(reply);
@@ -3222,21 +3226,19 @@ static int snapshot(DBusConnection *bus, char **args) {
                         DBUS_TYPE_STRING, &property,
                         DBUS_TYPE_INVALID);
         if (r < 0)
-                goto finish;
+                return r;
 
         if (!dbus_message_iter_init(reply, &iter) ||
             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
                 log_error("Failed to parse reply.");
-                r = -EIO;
-                goto finish;
+                return -EIO;
         }
 
         dbus_message_iter_recurse(&iter, &sub);
 
         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
                 log_error("Failed to parse reply.");
-                r = -EIO;
-                goto finish;
+                return -EIO;
         }
 
         dbus_message_iter_get_basic(&sub, &id);
@@ -3244,10 +3246,7 @@ static int snapshot(DBusConnection *bus, char **args) {
         if (!arg_quiet)
                 puts(id);
 
-finish:
-        dbus_error_free(&error);
-
-        return r;
+        return 0;
 }
 
 static int delete_snapshot(DBusConnection *bus, char **args) {
@@ -3259,7 +3258,10 @@ static int delete_snapshot(DBusConnection *bus, char **args) {
                 _cleanup_free_ char *n = NULL;
                 int r;
 
-                n = unit_name_mangle(*name);
+                n = snapshot_name_mangle(*name);
+                if (!n)
+                        return log_oom();
+
                 r = bus_method_call_with_reply(
                                 bus,
                                 "org.freedesktop.systemd1",
@@ -3268,7 +3270,7 @@ static int delete_snapshot(DBusConnection *bus, char **args) {
                                 "RemoveSnapshot",
                                 NULL,
                                 NULL,
-                                DBUS_TYPE_STRING, n ? &n : name,
+                                DBUS_TYPE_STRING, &n,
                                 DBUS_TYPE_INVALID);
                 if (r < 0)
                         return r;
@@ -3302,7 +3304,7 @@ static int daemon_reload(DBusConnection *bus, char **args) {
                                     /* "daemon-reload" */ "Reload";
         }
 
-        r = bus_method_call_with_reply (
+        r = bus_method_call_with_reply(
                         bus,
                         "org.freedesktop.systemd1",
                         "/org/freedesktop/systemd1",
@@ -3320,10 +3322,10 @@ static int daemon_reload(DBusConnection *bus, char **args) {
                 /* On reexecution, we expect a disconnect, not
                  * a reply */
                 r = 0;
-        else if (r)
+        else if (r < 0)
                 log_error("Failed to issue method call: %s", bus_error_message(&error));
-        dbus_error_free(&error);
 
+        dbus_error_free(&error);
         return r;
 }