chiark / gitweb /
logind: rename 'pos' to 'position'
[elogind.git] / src / login / logind-core.c
index ca34d378ff02fdffdf0c98b93dbce61f7cba08ac..59f5f8cc09b701fcebd346464fa9cde11c12ccfa 100644 (file)
 ***/
 
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <pwd.h>
-#include <unistd.h>
 #include <linux/vt.h>
 
 #include "strv.h"
 #include "cgroup-util.h"
-#include "audit.h"
 #include "bus-util.h"
 #include "bus-error.h"
 #include "udev-util.h"
 #include "logind.h"
+#include "terminal-util.h"
 
 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
         Device *d;
@@ -101,7 +99,7 @@ int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **
         assert(m);
         assert(name);
 
-        u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
+        u = hashmap_get(m->users, UID_TO_PTR(uid));
         if (!u) {
                 u = user_new(m, uid, gid, name);
                 if (!u)
@@ -314,55 +312,47 @@ int manager_process_button_device(Manager *m, struct udev_device *d) {
 }
 
 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
-        _cleanup_free_ char *unit = NULL;
+        _cleanup_free_ char *session_name = NULL;
         Session *s;
         int r;
 
         assert(m);
-        assert(session);
 
         if (pid < 1)
                 return -EINVAL;
 
-        r = cg_pid_get_unit(pid, &unit);
+        r = cg_pid_get_session(pid, &session_name);
         if (r < 0)
                 return 0;
 
-        s = hashmap_get(m->session_units, unit);
+        s = hashmap_get(m->sessions, session_name);
         if (!s)
                 return 0;
 
-        *session = s;
+        if (session)
+                *session = s;
         return 1;
 }
 
 int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
-        _cleanup_free_ char *unit = NULL;
-        User *u;
+        Session *s;
         int r;
 
         assert(m);
         assert(user);
 
-        if (pid < 1)
-                return -EINVAL;
-
-        r = cg_pid_get_slice(pid, &unit);
-        if (r < 0)
-                return 0;
-
-        u = hashmap_get(m->user_units, unit);
-        if (!u)
-                return 0;
+        r = manager_get_session_by_pid (m, pid, &s);
+        if (r <= 0)
+                return r;
 
-        *user = u;
+        *user = s->user;
         return 1;
 }
 
 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
         Session *s;
         bool idle_hint;
-        dual_timestamp ts = { 0, 0 };
+        dual_timestamp ts = DUAL_TIMESTAMP_NULL;
         Iterator i;
 
         assert(m);
@@ -439,47 +429,7 @@ static int vt_is_busy(unsigned int vtnr) {
         return r;
 }
 
-int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
-        int r;
-
-        assert(m);
-        assert(vtnr >= 1);
-
-        if (vtnr > m->n_autovts &&
-            vtnr != m->reserve_vt)
-                return 0;
-
-        if (vtnr != m->reserve_vt) {
-                /* If this is the reserved TTY, we'll start the getty
-                 * on it in any case, but otherwise only if it is not
-                 * busy. */
-
-                r = vt_is_busy(vtnr);
-                if (r < 0)
-                        return r;
-                else if (r > 0)
-                        return -EBUSY;
-        }
-
-        snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
-        r = sd_bus_call_method(
-                        m->bus,
-                        "org.freedesktop.systemd1",
-                        "/org/freedesktop/systemd1",
-                        "org.freedesktop.systemd1.Manager",
-                        "StartUnit",
-                        &error,
-                        NULL,
-                        "ss", name, "fail");
-        if (r < 0)
-                log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
-
-        return r;
-}
-
-bool manager_is_docked(Manager *m) {
+static bool manager_is_docked(Manager *m) {
         Iterator i;
         Button *b;
 
@@ -490,7 +440,7 @@ bool manager_is_docked(Manager *m) {
         return false;
 }
 
-int manager_count_displays(Manager *m) {
+static int manager_count_external_displays(Manager *m) {
         _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
         struct udev_list_entry *item = NULL, *first = NULL;
         int r;
@@ -512,7 +462,8 @@ int manager_count_displays(Manager *m) {
         udev_list_entry_foreach(item, first) {
                 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
                 struct udev_device *p;
-                const char *status;
+                const char *status, *enabled, *dash, *nn, *i;
+                bool external = false;
 
                 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
                 if (!d)
@@ -520,7 +471,7 @@ int manager_count_displays(Manager *m) {
 
                 p = udev_device_get_parent(d);
                 if (!p)
-                        return -ENOMEM;
+                        continue;
 
                 /* If the parent shares the same subsystem as the
                  * device we are looking at then it is a connector,
@@ -528,6 +479,40 @@ int manager_count_displays(Manager *m) {
                 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
                         continue;
 
+                nn = udev_device_get_sysname(d);
+                if (!nn)
+                        continue;
+
+                /* Ignore internal displays: the type is encoded in
+                 * the sysfs name, as the second dash seperated item
+                 * (the first is the card name, the last the connector
+                 * number). We implement a whitelist of external
+                 * displays here, rather than a whitelist, to ensure
+                 * we don't block suspends too eagerly. */
+                dash = strchr(nn, '-');
+                if (!dash)
+                        continue;
+
+                dash++;
+                FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
+                               "Composite-", "SVIDEO-", "Component-",
+                               "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
+
+                        if (startswith(dash, i)) {
+                                external = true;
+                                break;
+                        }
+                }
+                if (!external)
+                        continue;
+
+                /* Ignore ports that are not enabled */
+                enabled = udev_device_get_sysattr_value(d, "enabled");
+                if (!enabled)
+                        continue;
+                if (!streq_ptr(enabled, "enabled"))
+                        continue;
+
                 /* We count any connector which is not explicitly
                  * "disconnected" as connected. */
                 status = udev_device_get_sysattr_value(d, "status");
@@ -537,3 +522,25 @@ int manager_count_displays(Manager *m) {
 
         return n;
 }
+
+bool manager_is_docked_or_external_displays(Manager *m) {
+        int n;
+
+        /* If we are docked don't react to lid closing */
+        if (manager_is_docked(m)) {
+                log_debug("System is docked.");
+                return true;
+        }
+
+        /* If we have more than one display connected,
+         * assume that we are docked. */
+        n = manager_count_external_displays(m);
+        if (n < 0)
+                log_warning_errno(n, "Display counting failed: %m");
+        else if (n >= 1) {
+                log_debug("External (%i) displays connected.", n);
+                return true;
+        }
+
+        return false;
+}