2017-05-03 04:11:22 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2017-08-26 04:50:16 +02:00
|
|
|
#include <assert.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-05-03 04:11:22 +02:00
|
|
|
#include <stdbool.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <stdio.h>
|
2017-05-03 04:11:22 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <unistd.h>
|
2019-07-27 10:53:54 +02:00
|
|
|
#include <wayland-server-core.h>
|
2017-07-11 09:18:34 +02:00
|
|
|
#include <wlr/backend/session/interface.h>
|
2018-07-21 15:44:20 +02:00
|
|
|
#include <wlr/config.h>
|
2017-06-21 18:10:07 +02:00
|
|
|
#include <wlr/util/log.h>
|
2018-02-12 19:45:58 +01:00
|
|
|
#include "util/signal.h"
|
2017-05-03 04:11:22 +02:00
|
|
|
|
2018-11-12 10:12:46 +01:00
|
|
|
#if WLR_HAS_SYSTEMD
|
2017-09-22 19:02:55 +02:00
|
|
|
#include <systemd/sd-bus.h>
|
|
|
|
#include <systemd/sd-login.h>
|
2018-11-12 10:12:46 +01:00
|
|
|
#elif WLR_HAS_ELOGIND
|
2017-09-22 19:02:55 +02:00
|
|
|
#include <elogind/sd-bus.h>
|
2018-02-12 09:12:31 +01:00
|
|
|
#include <elogind/sd-login.h>
|
2017-09-22 19:02:55 +02:00
|
|
|
#endif
|
|
|
|
|
2017-06-21 02:47:53 +02:00
|
|
|
enum { DRM_MAJOR = 226 };
|
|
|
|
|
2017-06-05 01:30:37 +02:00
|
|
|
const struct session_impl session_logind;
|
|
|
|
|
2017-05-03 04:11:22 +02:00
|
|
|
struct logind_session {
|
|
|
|
struct wlr_session base;
|
|
|
|
|
|
|
|
sd_bus *bus;
|
2017-05-13 13:56:40 +02:00
|
|
|
struct wl_event_source *event;
|
2017-05-03 04:11:22 +02:00
|
|
|
|
|
|
|
char *id;
|
|
|
|
char *path;
|
2019-11-02 21:48:57 +01:00
|
|
|
char *seat_path;
|
2018-09-24 23:17:08 +02:00
|
|
|
|
2019-11-02 21:48:57 +01:00
|
|
|
bool can_graphical;
|
2018-09-24 23:17:08 +02:00
|
|
|
// specifies whether a drm device was taken
|
|
|
|
// if so, the session will be (de)activated with the drm fd,
|
|
|
|
// otherwise with the dbus PropertiesChanged on "active" signal
|
|
|
|
bool has_drm;
|
2017-05-03 04:11:22 +02:00
|
|
|
};
|
|
|
|
|
2018-09-17 22:01:35 +02:00
|
|
|
static struct logind_session *logind_session_from_session(
|
|
|
|
struct wlr_session *base) {
|
|
|
|
assert(base->impl == &session_logind);
|
|
|
|
return (struct logind_session *)base;
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:53:13 +02:00
|
|
|
static int logind_take_device(struct wlr_session *base, const char *path) {
|
2018-09-17 22:01:35 +02:00
|
|
|
struct logind_session *session = logind_session_from_session(base);
|
2017-05-03 04:11:22 +02:00
|
|
|
|
|
|
|
int fd = -1;
|
2018-12-04 19:57:17 +01:00
|
|
|
int ret;
|
2017-05-03 04:11:22 +02:00
|
|
|
sd_bus_message *msg = NULL;
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (stat(path, &st) < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to stat '%s'", path);
|
2017-05-03 04:11:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-24 23:17:08 +02:00
|
|
|
if (major(st.st_rdev) == DRM_MAJOR) {
|
|
|
|
session->has_drm = true;
|
|
|
|
}
|
|
|
|
|
2017-05-03 04:11:22 +02:00
|
|
|
ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
|
|
|
session->path, "org.freedesktop.login1.Session", "TakeDevice",
|
|
|
|
&error, &msg, "uu", major(st.st_rdev), minor(st.st_rdev));
|
|
|
|
if (ret < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to take device '%s': %s", path,
|
|
|
|
error.message);
|
|
|
|
goto out;
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int paused = 0;
|
|
|
|
ret = sd_bus_message_read(msg, "hb", &fd, &paused);
|
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to parse D-Bus response for '%s': %s",
|
2017-05-03 04:11:22 +02:00
|
|
|
path, strerror(-ret));
|
2018-12-04 19:57:17 +01:00
|
|
|
goto out;
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
2017-06-21 02:47:53 +02:00
|
|
|
// The original fd seems to be closed when the message is freed
|
2017-05-03 04:11:22 +02:00
|
|
|
// so we just clone it.
|
|
|
|
fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
|
2018-12-04 19:57:17 +01:00
|
|
|
if (fd < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to clone file descriptor for '%s': %s",
|
2017-05-03 04:11:22 +02:00
|
|
|
path, strerror(errno));
|
2018-12-04 19:57:17 +01:00
|
|
|
goto out;
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:57:17 +01:00
|
|
|
out:
|
2017-05-03 04:11:22 +02:00
|
|
|
sd_bus_error_free(&error);
|
|
|
|
sd_bus_message_unref(msg);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void logind_release_device(struct wlr_session *base, int fd) {
|
2018-09-17 22:01:35 +02:00
|
|
|
struct logind_session *session = logind_session_from_session(base);
|
2017-05-03 04:11:22 +02:00
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd, &st) < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to stat device '%d': %s", fd,
|
|
|
|
strerror(errno));
|
2020-04-15 20:26:23 +02:00
|
|
|
close(fd);
|
2017-05-03 04:11:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-15 20:26:23 +02:00
|
|
|
close(fd);
|
2017-05-03 04:11:22 +02:00
|
|
|
|
2018-12-04 19:57:17 +01:00
|
|
|
sd_bus_message *msg = NULL;
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
int ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
2017-05-03 04:11:22 +02:00
|
|
|
session->path, "org.freedesktop.login1.Session", "ReleaseDevice",
|
|
|
|
&error, &msg, "uu", major(st.st_rdev), minor(st.st_rdev));
|
|
|
|
if (ret < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to release device '%d': %s", fd,
|
|
|
|
error.message);
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
sd_bus_message_unref(msg);
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:53:13 +02:00
|
|
|
static bool logind_change_vt(struct wlr_session *base, unsigned vt) {
|
2018-09-17 22:01:35 +02:00
|
|
|
struct logind_session *session = logind_session_from_session(base);
|
2017-05-13 13:56:40 +02:00
|
|
|
|
2018-06-14 10:46:16 +02:00
|
|
|
// Only seat0 has VTs associated with it
|
|
|
|
if (strcmp(session->base.seat, "seat0") != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-13 13:56:40 +02:00
|
|
|
int ret;
|
|
|
|
sd_bus_message *msg = NULL;
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
2019-05-22 14:43:13 +02:00
|
|
|
"/org/freedesktop/login1/seat/seat0", "org.freedesktop.login1.Seat", "SwitchTo",
|
2017-05-13 13:56:40 +02:00
|
|
|
&error, &msg, "u", (uint32_t)vt);
|
|
|
|
if (ret < 0) {
|
2020-06-04 14:33:32 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to change to vt '%u'", vt);
|
2017-05-13 13:56:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
sd_bus_message_unref(msg);
|
|
|
|
return ret >= 0;
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:45:52 +01:00
|
|
|
static bool find_session_path(struct logind_session *session) {
|
2017-07-28 19:41:17 +02:00
|
|
|
int ret;
|
|
|
|
sd_bus_message *msg = NULL;
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
|
|
|
"/org/freedesktop/login1", "org.freedesktop.login1.Manager",
|
|
|
|
"GetSession", &error, &msg, "s", session->id);
|
|
|
|
if (ret < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to get session path: %s", error.message);
|
2017-07-28 19:41:17 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *path;
|
|
|
|
ret = sd_bus_message_read(msg, "o", &path);
|
|
|
|
if (ret < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Could not parse session path: %s", error.message);
|
2017-07-28 19:41:17 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
session->path = strdup(path);
|
|
|
|
|
|
|
|
out:
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
sd_bus_message_unref(msg);
|
|
|
|
|
|
|
|
return ret >= 0;
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:48:57 +01:00
|
|
|
static bool find_seat_path(struct logind_session *session) {
|
|
|
|
int ret;
|
|
|
|
sd_bus_message *msg = NULL;
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
|
|
|
"/org/freedesktop/login1", "org.freedesktop.login1.Manager",
|
|
|
|
"GetSeat", &error, &msg, "s", session->base.seat);
|
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to get seat path: %s", error.message);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *path;
|
|
|
|
ret = sd_bus_message_read(msg, "o", &path);
|
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Could not parse seat path: %s", error.message);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
session->seat_path = strdup(path);
|
|
|
|
|
|
|
|
out:
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
sd_bus_message_unref(msg);
|
|
|
|
|
|
|
|
return ret >= 0;
|
|
|
|
}
|
|
|
|
|
2017-05-03 04:11:22 +02:00
|
|
|
static bool session_activate(struct logind_session *session) {
|
|
|
|
int ret;
|
|
|
|
sd_bus_message *msg = NULL;
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
|
|
|
session->path, "org.freedesktop.login1.Session", "Activate",
|
|
|
|
&error, &msg, "");
|
|
|
|
if (ret < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to activate session: %s", error.message);
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
sd_bus_message_unref(msg);
|
|
|
|
return ret >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool take_control(struct logind_session *session) {
|
|
|
|
int ret;
|
|
|
|
sd_bus_message *msg = NULL;
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
|
|
|
session->path, "org.freedesktop.login1.Session", "TakeControl",
|
|
|
|
&error, &msg, "b", false);
|
|
|
|
if (ret < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to take control of session: %s",
|
|
|
|
error.message);
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
sd_bus_message_unref(msg);
|
|
|
|
return ret >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release_control(struct logind_session *session) {
|
|
|
|
int ret;
|
|
|
|
sd_bus_message *msg = NULL;
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
|
|
|
session->path, "org.freedesktop.login1.Session", "ReleaseControl",
|
|
|
|
&error, &msg, "");
|
|
|
|
if (ret < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to release control of session: %s",
|
|
|
|
error.message);
|
2017-05-03 04:11:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
sd_bus_message_unref(msg);
|
|
|
|
}
|
|
|
|
|
2017-08-26 01:56:43 +02:00
|
|
|
static void logind_session_destroy(struct wlr_session *base) {
|
2018-09-17 22:01:35 +02:00
|
|
|
struct logind_session *session = logind_session_from_session(base);
|
2017-05-03 04:11:22 +02:00
|
|
|
|
|
|
|
release_control(session);
|
|
|
|
|
2017-05-13 13:56:40 +02:00
|
|
|
wl_event_source_remove(session->event);
|
2017-05-03 04:11:22 +02:00
|
|
|
sd_bus_unref(session->bus);
|
|
|
|
free(session->id);
|
|
|
|
free(session->path);
|
2019-11-02 21:48:57 +01:00
|
|
|
free(session->seat_path);
|
2017-05-03 04:11:22 +02:00
|
|
|
free(session);
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:57:17 +01:00
|
|
|
static int session_removed(sd_bus_message *msg, void *userdata,
|
|
|
|
sd_bus_error *ret_error) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_INFO, "SessionRemoved signal received");
|
2017-05-13 13:56:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:57:17 +01:00
|
|
|
static struct wlr_device *find_device(struct wlr_session *session,
|
|
|
|
dev_t devnum) {
|
2017-08-26 04:50:16 +02:00
|
|
|
struct wlr_device *dev;
|
|
|
|
|
|
|
|
wl_list_for_each(dev, &session->devices, link) {
|
|
|
|
if (dev->dev == devnum) {
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Tried to use dev_t %lu not opened by session",
|
2017-08-26 04:50:16 +02:00
|
|
|
(unsigned long)devnum);
|
|
|
|
assert(0);
|
2019-08-09 15:36:35 +02:00
|
|
|
return NULL;
|
2017-08-26 04:50:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:57:17 +01:00
|
|
|
static int pause_device(sd_bus_message *msg, void *userdata,
|
|
|
|
sd_bus_error *ret_error) {
|
2017-05-13 15:12:47 +02:00
|
|
|
struct logind_session *session = userdata;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
uint32_t major, minor;
|
2017-06-21 02:47:53 +02:00
|
|
|
const char *type;
|
|
|
|
ret = sd_bus_message_read(msg, "uus", &major, &minor, &type);
|
2017-05-13 15:12:47 +02:00
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to parse D-Bus response for PauseDevice: %s",
|
2017-05-13 15:12:47 +02:00
|
|
|
strerror(-ret));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-05-13 16:43:35 +02:00
|
|
|
if (major == DRM_MAJOR && strcmp(type, "gone") != 0) {
|
2018-09-24 23:17:08 +02:00
|
|
|
assert(session->has_drm);
|
2017-06-21 02:47:53 +02:00
|
|
|
session->base.active = false;
|
2018-02-12 09:12:31 +01:00
|
|
|
wlr_signal_emit_safe(&session->base.session_signal, session);
|
2017-06-21 02:47:53 +02:00
|
|
|
}
|
2017-05-13 15:12:47 +02:00
|
|
|
|
2017-06-21 02:47:53 +02:00
|
|
|
if (strcmp(type, "pause") == 0) {
|
|
|
|
ret = sd_bus_call_method(session->bus, "org.freedesktop.login1",
|
|
|
|
session->path, "org.freedesktop.login1.Session", "PauseDeviceComplete",
|
|
|
|
ret_error, &msg, "uu", major, minor);
|
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to send PauseDeviceComplete signal: %s",
|
2017-06-21 02:47:53 +02:00
|
|
|
strerror(-ret));
|
|
|
|
}
|
2017-05-13 15:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
2017-05-13 13:56:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-31 16:11:45 +01:00
|
|
|
static int resume_device(sd_bus_message *msg, void *userdata,
|
|
|
|
sd_bus_error *ret_error) {
|
2017-05-13 15:12:47 +02:00
|
|
|
struct logind_session *session = userdata;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
uint32_t major, minor;
|
|
|
|
ret = sd_bus_message_read(msg, "uuh", &major, &minor, &fd);
|
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to parse D-Bus response for ResumeDevice: %s",
|
2017-05-13 15:12:47 +02:00
|
|
|
strerror(-ret));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-06-21 02:47:53 +02:00
|
|
|
if (major == DRM_MAJOR) {
|
2017-08-26 04:50:16 +02:00
|
|
|
struct wlr_device *dev = find_device(&session->base, makedev(major, minor));
|
2019-01-31 16:11:45 +01:00
|
|
|
|
|
|
|
close(dev->fd);
|
|
|
|
if (fcntl(fd, F_DUPFD_CLOEXEC, dev->fd) < 0) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to duplicate file descriptor");
|
|
|
|
goto error;
|
|
|
|
}
|
2017-08-26 04:50:16 +02:00
|
|
|
|
|
|
|
if (!session->base.active) {
|
|
|
|
session->base.active = true;
|
2018-02-12 09:12:31 +01:00
|
|
|
wlr_signal_emit_safe(&session->base.session_signal, session);
|
2017-08-26 04:50:16 +02:00
|
|
|
}
|
2017-06-21 02:47:53 +02:00
|
|
|
}
|
2017-05-13 15:12:47 +02:00
|
|
|
|
|
|
|
error:
|
2017-05-13 13:56:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:48:57 +01:00
|
|
|
static int session_properties_changed(sd_bus_message *msg, void *userdata,
|
2018-09-24 23:17:08 +02:00
|
|
|
sd_bus_error *ret_error) {
|
|
|
|
struct logind_session *session = userdata;
|
|
|
|
int ret = 0;
|
|
|
|
|
2018-10-04 13:46:01 +02:00
|
|
|
// if we have a drm fd we don't depend on this
|
2018-09-24 23:17:08 +02:00
|
|
|
if (session->has_drm) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-04 13:46:01 +02:00
|
|
|
// PropertiesChanged arg 1: interface
|
2018-09-24 23:17:08 +02:00
|
|
|
const char *interface;
|
|
|
|
ret = sd_bus_message_read_basic(msg, 's', &interface); // skip path
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(interface, "org.freedesktop.login1.Session") != 0) {
|
|
|
|
// not interesting for us; ignore
|
|
|
|
wlr_log(WLR_DEBUG, "ignoring PropertiesChanged from %s", interface);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-04 13:46:01 +02:00
|
|
|
// PropertiesChanged arg 2: changed properties with values
|
2018-09-24 23:17:08 +02:00
|
|
|
ret = sd_bus_message_enter_container(msg, 'a', "{sv}");
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *s;
|
|
|
|
while ((ret = sd_bus_message_enter_container(msg, 'e', "sv")) > 0) {
|
|
|
|
ret = sd_bus_message_read_basic(msg, 's', &s);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(s, "Active") == 0) {
|
2018-10-04 13:46:01 +02:00
|
|
|
int ret;
|
|
|
|
ret = sd_bus_message_enter_container(msg, 'v', "b");
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool active;
|
|
|
|
ret = sd_bus_message_read_basic(msg, 'b', &active);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session->base.active != active) {
|
|
|
|
session->base.active = active;
|
|
|
|
wlr_signal_emit_safe(&session->base.session_signal, session);
|
|
|
|
}
|
2018-09-24 23:17:08 +02:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sd_bus_message_skip(msg, "{sv}");
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sd_bus_message_exit_container(msg);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sd_bus_message_exit_container(msg);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2018-10-04 13:46:01 +02:00
|
|
|
// PropertiesChanged arg 3: changed properties without values
|
2018-09-24 23:17:08 +02:00
|
|
|
sd_bus_message_enter_container(msg, 'a', "s");
|
|
|
|
while ((ret = sd_bus_message_read_basic(msg, 's', &s)) > 0) {
|
|
|
|
if (strcmp(s, "Active") == 0) {
|
|
|
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
2018-10-04 13:46:01 +02:00
|
|
|
bool active;
|
|
|
|
ret = sd_bus_get_property_trivial(session->bus,
|
|
|
|
"org.freedesktop.login1", session->path,
|
|
|
|
"org.freedesktop.login1.Session", "Active", &error,
|
|
|
|
'b', &active);
|
2018-09-24 23:17:08 +02:00
|
|
|
if (ret < 0) {
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to get 'Active' property: %s",
|
|
|
|
error.message);
|
2018-10-04 13:46:01 +02:00
|
|
|
return 0;
|
2018-09-24 23:17:08 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:46:01 +02:00
|
|
|
if (session->base.active != active) {
|
|
|
|
session->base.active = active;
|
|
|
|
wlr_signal_emit_safe(&session->base.session_signal, session);
|
|
|
|
}
|
2018-09-24 23:17:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2018-10-04 13:46:01 +02:00
|
|
|
return 0;
|
|
|
|
|
2018-09-24 23:17:08 +02:00
|
|
|
error:
|
2018-12-04 19:57:17 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to parse D-Bus PropertiesChanged: %s",
|
2018-10-04 13:46:01 +02:00
|
|
|
strerror(-ret));
|
2018-09-24 23:17:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:48:57 +01:00
|
|
|
static int seat_properties_changed(sd_bus_message *msg, void *userdata,
|
|
|
|
sd_bus_error *ret_error) {
|
|
|
|
struct logind_session *session = userdata;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
// if we have a drm fd we don't depend on this
|
|
|
|
if (session->has_drm) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropertiesChanged arg 1: interface
|
|
|
|
const char *interface;
|
|
|
|
ret = sd_bus_message_read_basic(msg, 's', &interface); // skip path
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(interface, "org.freedesktop.login1.Seat") != 0) {
|
|
|
|
// not interesting for us; ignore
|
|
|
|
wlr_log(WLR_DEBUG, "ignoring PropertiesChanged from %s", interface);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropertiesChanged arg 2: changed properties with values
|
|
|
|
ret = sd_bus_message_enter_container(msg, 'a', "{sv}");
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *s;
|
|
|
|
while ((ret = sd_bus_message_enter_container(msg, 'e', "sv")) > 0) {
|
|
|
|
ret = sd_bus_message_read_basic(msg, 's', &s);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(s, "CanGraphical") == 0) {
|
|
|
|
int ret;
|
|
|
|
ret = sd_bus_message_enter_container(msg, 'v', "b");
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sd_bus_message_read_basic(msg, 'b', &session->can_graphical);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sd_bus_message_skip(msg, "{sv}");
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sd_bus_message_exit_container(msg);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sd_bus_message_exit_container(msg);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropertiesChanged arg 3: changed properties without values
|
|
|
|
sd_bus_message_enter_container(msg, 'a', "s");
|
|
|
|
while ((ret = sd_bus_message_read_basic(msg, 's', &s)) > 0) {
|
|
|
|
if (strcmp(s, "CanGraphical") == 0) {
|
|
|
|
session->can_graphical = sd_seat_can_graphical(session->base.seat);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
wlr_log(WLR_ERROR, "Failed to parse D-Bus PropertiesChanged: %s",
|
|
|
|
strerror(-ret));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-13 13:56:40 +02:00
|
|
|
static bool add_signal_matches(struct logind_session *session) {
|
2019-11-03 01:35:28 +01:00
|
|
|
static const char *logind = "org.freedesktop.login1";
|
|
|
|
static const char *logind_path = "/org/freedesktop/login1";
|
|
|
|
static const char *manager_interface = "org.freedesktop.login1.Manager";
|
|
|
|
static const char *session_interface = "org.freedesktop.login1.Session";
|
|
|
|
static const char *property_interface = "org.freedesktop.DBus.Properties";
|
2017-05-13 13:56:40 +02:00
|
|
|
int ret;
|
|
|
|
|
2019-11-03 01:35:28 +01:00
|
|
|
ret = sd_bus_match_signal(session->bus, NULL, logind, logind_path,
|
|
|
|
manager_interface, "SessionRemoved", session_removed, session);
|
2017-05-13 13:56:40 +02:00
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
|
2017-05-13 13:56:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-03 01:35:28 +01:00
|
|
|
ret = sd_bus_match_signal(session->bus, NULL, logind, session->path,
|
|
|
|
session_interface, "PauseDevice", pause_device, session);
|
2017-05-13 13:56:40 +02:00
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
|
2017-05-13 13:56:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-03 01:35:28 +01:00
|
|
|
ret = sd_bus_match_signal(session->bus, NULL, logind, session->path,
|
|
|
|
session_interface, "ResumeDevice", resume_device, session);
|
2017-05-13 13:56:40 +02:00
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
|
2017-05-13 13:56:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-03 01:35:28 +01:00
|
|
|
ret = sd_bus_match_signal(session->bus, NULL, logind, session->path,
|
|
|
|
property_interface, "PropertiesChanged",
|
2019-11-02 21:48:57 +01:00
|
|
|
session_properties_changed, session);
|
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-03 01:35:28 +01:00
|
|
|
ret = sd_bus_match_signal(session->bus, NULL, logind, session->seat_path,
|
|
|
|
property_interface, "PropertiesChanged",
|
|
|
|
seat_properties_changed, session);
|
2018-09-24 23:17:08 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-13 13:56:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dbus_event(int fd, uint32_t mask, void *data) {
|
|
|
|
sd_bus *bus = data;
|
2018-01-21 16:28:21 +01:00
|
|
|
while (sd_bus_process(bus, NULL) > 0) {
|
2018-01-21 17:00:15 +01:00
|
|
|
// Do nothing.
|
2018-01-21 16:28:21 +01:00
|
|
|
}
|
2017-05-13 13:56:40 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-02 03:56:31 +01:00
|
|
|
static bool contains_str(const char *needle, const char **haystack) {
|
|
|
|
for (int i = 0; haystack[i]; i++) {
|
|
|
|
if (strcmp(haystack[i], needle) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_greeter_session(char **session_id) {
|
|
|
|
char *class = NULL;
|
|
|
|
char **user_sessions = NULL;
|
|
|
|
int user_session_count = sd_uid_get_sessions(getuid(), 1, &user_sessions);
|
|
|
|
|
|
|
|
if (user_session_count < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to get sessions");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user_session_count == 0) {
|
|
|
|
wlr_log(WLR_ERROR, "User has no sessions");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < user_session_count; ++i) {
|
|
|
|
int ret = sd_session_get_class(user_sessions[i], &class);
|
|
|
|
if (ret < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(class, "greeter") == 0) {
|
|
|
|
*session_id = strdup(user_sessions[i]);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(class);
|
|
|
|
for (int i = 0; i < user_session_count; ++i) {
|
|
|
|
free(user_sessions[i]);
|
|
|
|
}
|
|
|
|
free(user_sessions);
|
|
|
|
|
|
|
|
return *session_id != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_display_session(char **session_id) {
|
|
|
|
assert(session_id != NULL);
|
|
|
|
int ret;
|
|
|
|
|
2019-05-22 15:25:42 +02:00
|
|
|
char *type = NULL;
|
|
|
|
char *state = NULL;
|
|
|
|
char *xdg_session_id = getenv("XDG_SESSION_ID");
|
|
|
|
|
|
|
|
if (xdg_session_id) {
|
|
|
|
// This just checks whether the supplied session ID is valid
|
|
|
|
if (sd_session_is_active(xdg_session_id) < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Invalid XDG_SESSION_ID: '%s'", xdg_session_id);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
*session_id = strdup(xdg_session_id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-02 03:56:31 +01:00
|
|
|
// If there's a session active for the current process then just use that
|
|
|
|
ret = sd_pid_get_session(getpid(), session_id);
|
|
|
|
if (ret == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find any active sessions for the user if the process isn't part of an
|
|
|
|
// active session itself
|
|
|
|
ret = sd_uid_get_display(getuid(), session_id);
|
|
|
|
if (ret < 0 && ret != -ENODATA) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to get display: %s", strerror(-ret));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != 0 && !get_greeter_session(session_id)) {
|
|
|
|
wlr_log(WLR_ERROR, "Couldn't find an active session or a greeter session");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(*session_id != NULL);
|
|
|
|
|
|
|
|
// Check that the available session is graphical
|
|
|
|
ret = sd_session_get_type(*session_id, &type);
|
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Couldn't get a type for session '%s': %s",
|
|
|
|
*session_id, strerror(-ret));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *graphical_session_types[] = {"wayland", "x11", "mir", NULL};
|
|
|
|
if (!contains_str(type, graphical_session_types)) {
|
|
|
|
wlr_log(WLR_ERROR, "Session '%s' isn't a graphical session (type: '%s')",
|
|
|
|
*session_id, type);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the session is active
|
|
|
|
ret = sd_session_get_state(*session_id, &state);
|
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Couldn't get state for session '%s': %s",
|
|
|
|
*session_id, strerror(-ret));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *active_states[] = {"active", "online", NULL};
|
|
|
|
if (!contains_str(state, active_states)) {
|
|
|
|
wlr_log(WLR_ERROR, "Session '%s' is not active", *session_id);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(type);
|
|
|
|
free(state);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
free(type);
|
|
|
|
free(state);
|
|
|
|
free(*session_id);
|
|
|
|
*session_id = NULL;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-26 01:56:43 +02:00
|
|
|
static struct wlr_session *logind_session_create(struct wl_display *disp) {
|
2017-05-03 04:11:22 +02:00
|
|
|
int ret;
|
|
|
|
struct logind_session *session = calloc(1, sizeof(*session));
|
|
|
|
if (!session) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Allocation failed: %s", strerror(errno));
|
2017-05-03 04:11:22 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-02-02 03:56:31 +01:00
|
|
|
if (!get_display_session(&session->id)) {
|
2017-05-03 04:11:22 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:53:13 +02:00
|
|
|
char *seat;
|
|
|
|
ret = sd_session_get_seat(session->id, &seat);
|
2017-05-03 04:11:22 +02:00
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to get seat id: %s", strerror(-ret));
|
2017-05-03 04:11:22 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2017-07-09 07:53:13 +02:00
|
|
|
snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat);
|
2017-12-02 18:37:28 +01:00
|
|
|
|
|
|
|
if (strcmp(seat, "seat0") == 0) {
|
2017-12-02 16:50:00 +01:00
|
|
|
ret = sd_session_get_vt(session->id, &session->base.vtnr);
|
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Session not running in virtual terminal");
|
2017-12-02 16:50:00 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 07:53:13 +02:00
|
|
|
free(seat);
|
2017-05-03 04:11:22 +02:00
|
|
|
|
|
|
|
ret = sd_bus_default_system(&session->bus);
|
|
|
|
if (ret < 0) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to open D-Bus connection: %s", strerror(-ret));
|
2017-05-03 04:11:22 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:45:52 +01:00
|
|
|
if (!find_session_path(session)) {
|
2017-07-28 19:41:17 +02:00
|
|
|
sd_bus_unref(session->bus);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:48:57 +01:00
|
|
|
if (!find_seat_path(session)) {
|
|
|
|
sd_bus_unref(session->bus);
|
|
|
|
free(session->path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-05-13 13:56:40 +02:00
|
|
|
if (!add_signal_matches(session)) {
|
2017-05-03 04:11:22 +02:00
|
|
|
goto error_bus;
|
2017-05-13 13:56:40 +02:00
|
|
|
}
|
2017-05-03 04:11:22 +02:00
|
|
|
|
2017-05-13 13:56:40 +02:00
|
|
|
struct wl_event_loop *event_loop = wl_display_get_event_loop(disp);
|
|
|
|
session->event = wl_event_loop_add_fd(event_loop, sd_bus_get_fd(session->bus),
|
|
|
|
WL_EVENT_READABLE, dbus_event, session->bus);
|
|
|
|
|
|
|
|
if (!session_activate(session)) {
|
2017-05-03 04:11:22 +02:00
|
|
|
goto error_bus;
|
2017-05-13 13:56:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!take_control(session)) {
|
|
|
|
goto error_bus;
|
|
|
|
}
|
2017-05-03 04:11:22 +02:00
|
|
|
|
2019-11-02 21:48:57 +01:00
|
|
|
// Check for CanGraphical first
|
|
|
|
session->can_graphical = sd_seat_can_graphical(session->base.seat);
|
|
|
|
if (!session->can_graphical) {
|
|
|
|
wlr_log(WLR_INFO, "Waiting for 'CanGraphical' on seat %s", session->base.seat);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!session->can_graphical) {
|
|
|
|
ret = wl_event_loop_dispatch(event_loop, -1);
|
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Polling error waiting for 'CanGraphical' on seat %s",
|
|
|
|
session->base.seat);
|
|
|
|
goto error_bus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_INFO, "Successfully loaded logind session");
|
2017-05-03 06:23:07 +02:00
|
|
|
|
2017-06-05 01:30:37 +02:00
|
|
|
session->base.impl = &session_logind;
|
2017-05-03 04:11:22 +02:00
|
|
|
return &session->base;
|
|
|
|
|
|
|
|
error_bus:
|
|
|
|
sd_bus_unref(session->bus);
|
2017-07-28 19:41:17 +02:00
|
|
|
free(session->path);
|
2019-11-02 21:48:57 +01:00
|
|
|
free(session->seat_path);
|
2017-05-03 04:11:22 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
free(session->id);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-05 01:30:37 +02:00
|
|
|
const struct session_impl session_logind = {
|
2017-08-26 01:56:43 +02:00
|
|
|
.create = logind_session_create,
|
|
|
|
.destroy = logind_session_destroy,
|
2017-05-03 04:11:22 +02:00
|
|
|
.open = logind_take_device,
|
|
|
|
.close = logind_release_device,
|
2017-05-13 13:56:40 +02:00
|
|
|
.change_vt = logind_change_vt,
|
2017-05-03 04:11:22 +02:00
|
|
|
};
|