aboutsummaryrefslogtreecommitdiff
path: root/session
diff options
context:
space:
mode:
Diffstat (limited to 'session')
-rw-r--r--session/CMakeLists.txt12
-rw-r--r--session/direct-ipc.c235
-rw-r--r--session/direct.c203
-rw-r--r--session/logind.c29
-rw-r--r--session/session.c6
5 files changed, 458 insertions, 27 deletions
diff --git a/session/CMakeLists.txt b/session/CMakeLists.txt
index 23077c12..bacd412f 100644
--- a/session/CMakeLists.txt
+++ b/session/CMakeLists.txt
@@ -1,10 +1,12 @@
include_directories(
- ${WAYLAND_INCLUDE_DIR}
+ ${WAYLAND_INCLUDE_DIR}
+ ${DRM_INCLUDE_DIRS}
)
set(sources
session.c
direct.c
+ direct-ipc.c
)
set(libs
@@ -21,5 +23,13 @@ if (SYSTEMD_FOUND)
list(APPEND libs ${SYSTEMD_LIBRARIES})
endif ()
+if (Libcap_FOUND)
+ add_definitions(${Libcap_DEFINITIONS})
+ include_directories(${Libcap_INCLUDE_DIRS})
+
+ add_definitions(-DHAS_LIBCAP)
+ list(APPEND libs ${Libcap_LIBRARIES})
+endif ()
+
add_library(wlr-session ${sources})
target_link_libraries(wlr-session ${libs})
diff --git a/session/direct-ipc.c b/session/direct-ipc.c
new file mode 100644
index 00000000..fbbde02a
--- /dev/null
+++ b/session/direct-ipc.c
@@ -0,0 +1,235 @@
+#define _POSIX_C_SOURCE 200809L
+#include <errno.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/sysmacros.h>
+#include <sys/wait.h>
+#include <xf86drm.h>
+#include <linux/major.h>
+#include <wlr/util/log.h>
+#include "session/direct-ipc.h"
+
+enum { DRM_MAJOR = 226 };
+
+#ifdef HAS_LIBCAP
+#include <sys/capability.h>
+
+static bool have_permissions(void) {
+ cap_t cap = cap_get_proc();
+ cap_flag_value_t val;
+
+ if (!cap || cap_get_flag(cap, CAP_SYS_ADMIN, CAP_PERMITTED, &val) || val != CAP_SET) {
+ wlr_log(L_ERROR, "Do not have CAP_SYS_ADMIN; cannot become DRM master");
+ cap_free(cap);
+ return false;
+ }
+
+ cap_free(cap);
+ return true;
+}
+#else
+static bool have_permissions(void) {
+ if (geteuid() != 0) {
+ wlr_log(L_ERROR, "Do not have root privileges; cannot become DRM master");
+ return false;
+ }
+
+ return true;
+}
+#endif
+
+static void send_msg(int sock, int fd, void *buf, size_t buf_len) {
+ char control[CMSG_SPACE(sizeof(fd))] = {0};
+ struct iovec iovec = { .iov_base = buf, .iov_len = buf_len };
+ struct msghdr msghdr = {0};
+
+ if (buf) {
+ msghdr.msg_iov = &iovec;
+ msghdr.msg_iovlen = 1;
+ }
+
+ if (fd >= 0) {
+ msghdr.msg_control = &control;
+ msghdr.msg_controllen = sizeof(control);
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr);
+ *cmsg = (struct cmsghdr) {
+ .cmsg_level = SOL_SOCKET,
+ .cmsg_type = SCM_RIGHTS,
+ .cmsg_len = CMSG_LEN(sizeof(fd)),
+ };
+ *(int *)CMSG_DATA(cmsg) = fd;
+ }
+
+ ssize_t ret;
+ do {
+ ret = sendmsg(sock, &msghdr, 0);
+ } while (ret < 0 && errno == EINTR);
+}
+
+static ssize_t recv_msg(int sock, int *fd_out, void *buf, size_t buf_len) {
+ char control[CMSG_SPACE(sizeof(*fd_out))] = {0};
+ struct iovec iovec = { .iov_base = buf, .iov_len = buf_len };
+ struct msghdr msghdr = {0};
+
+ if (buf) {
+ msghdr.msg_iov = &iovec;
+ msghdr.msg_iovlen = 1;
+ }
+
+ if (fd_out) {
+ msghdr.msg_control = &control;
+ msghdr.msg_controllen = sizeof(control);
+ }
+
+ ssize_t ret;
+ do {
+ ret = recvmsg(sock, &msghdr, MSG_CMSG_CLOEXEC);
+ } while (ret < 0 && errno == EINTR);
+
+ if (fd_out) {
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr);
+ *fd_out = cmsg ? *(int *)CMSG_DATA(cmsg) : -1;
+ }
+
+ return ret;
+}
+
+enum msg_type {
+ MSG_OPEN,
+ MSG_SETMASTER,
+ MSG_DROPMASTER,
+ MSG_END,
+};
+
+struct msg {
+ enum msg_type type;
+ char path[256];
+};
+
+static void communicate(int sock) {
+ struct msg msg;
+ int drm_fd = -1;
+ bool running = true;
+
+ while (running && recv_msg(sock, &drm_fd, &msg, sizeof(msg)) >= 0) {
+ switch (msg.type) {
+ case MSG_OPEN:
+ errno = 0;
+
+ // These are the same flags that logind opens files with
+ int fd = open(msg.path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
+ int ret = errno;
+ if (fd == -1) {
+ goto error;
+ }
+
+ struct stat st;
+ if (fstat(fd, &st) < 0) {
+ ret = errno;
+ goto error;
+ }
+
+ uint32_t maj = major(st.st_rdev);
+ if (maj != INPUT_MAJOR && maj != DRM_MAJOR) {
+ ret = ENOTSUP;
+ goto error;
+ }
+
+ if (maj == DRM_MAJOR && drmSetMaster(fd)) {
+ ret = errno;
+ }
+error:
+ send_msg(sock, ret ? -1 : fd, &ret, sizeof(ret));
+ close(fd);
+
+ break;
+
+ case MSG_SETMASTER:
+ drmSetMaster(drm_fd);
+ close(drm_fd);
+ send_msg(sock, -1, NULL, 0);
+ break;
+
+ case MSG_DROPMASTER:
+ drmDropMaster(drm_fd);
+ close(drm_fd);
+ send_msg(sock, -1, NULL, 0);
+ break;
+
+ case MSG_END:
+ running = false;
+ send_msg(sock, -1, NULL, 0);
+ break;
+ }
+ }
+
+ close(sock);
+}
+
+int direct_ipc_open(int sock, const char *path) {
+ struct msg msg = { .type = MSG_OPEN };
+ snprintf(msg.path, sizeof(msg.path), "%s", path);
+
+ send_msg(sock, -1, &msg, sizeof(msg));
+
+ int fd, err;
+ recv_msg(sock, &fd, &err, sizeof(err));
+
+ return err ? -err : fd;
+}
+
+void direct_ipc_setmaster(int sock, int fd) {
+ struct msg msg = { .type = MSG_SETMASTER };
+
+ send_msg(sock, fd, &msg, sizeof(msg));
+ recv_msg(sock, NULL, NULL, 0);
+}
+
+void direct_ipc_dropmaster(int sock, int fd) {
+ struct msg msg = { .type = MSG_DROPMASTER };
+
+ send_msg(sock, fd, &msg, sizeof(msg));
+ recv_msg(sock, NULL, NULL, 0);
+}
+
+void direct_ipc_finish(int sock, pid_t pid) {
+ struct msg msg = { .type = MSG_END };
+
+ send_msg(sock, -1, &msg, sizeof(msg));
+ recv_msg(sock, NULL, NULL, 0);
+
+ waitpid(pid, NULL, 0);
+}
+
+int direct_ipc_start(pid_t *pid_out) {
+ if (!have_permissions()) {
+ return -1;
+ }
+
+ int sock[2];
+ if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock) < 0) {
+ wlr_log_errno(L_ERROR, "Failed to create socket pair");
+ return -1;
+ }
+
+ pid_t pid = fork();
+ if (pid < 0) {
+ wlr_log_errno(L_ERROR, "Fork failed");
+ close(sock[0]);
+ close(sock[1]);
+ return -1;
+ } else if (pid == 0) {
+ close(sock[0]);
+ communicate(sock[1]);
+ _Exit(0);
+ }
+
+ close(sock[1]);
+ *pid_out = pid;
+ return sock[0];
+}
diff --git a/session/direct.c b/session/direct.c
index 74f2faf1..1247663b 100644
--- a/session/direct.c
+++ b/session/direct.c
@@ -1,53 +1,238 @@
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdlib.h>
+#include <stdio.h>
#include <stdbool.h>
-#include <string.h>
-#include <fcntl.h>
#include <unistd.h>
+#include <signal.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <linux/kd.h>
+#include <linux/major.h>
+#include <linux/input.h>
+#include <linux/vt.h>
#include <wayland-server.h>
#include <wlr/session/interface.h>
#include <wlr/util/log.h>
+#include "session/direct-ipc.h"
+
+enum { DRM_MAJOR = 226 };
const struct session_impl session_direct;
struct direct_session {
struct wlr_session base;
+ int tty_fd;
+ int old_kbmode;
+ int sock;
+ pid_t child;
+
+ struct wl_event_source *vt_source;
};
-static int direct_session_open(struct wlr_session *restrict base,
- const char *restrict path) {
- return open(path, O_RDWR | O_CLOEXEC);
+static int direct_session_open(struct wlr_session *base, const char *path) {
+ struct direct_session *session = wl_container_of(base, session, base);
+
+ int fd = direct_ipc_open(session->sock, path);
+ if (fd < 0) {
+ wlr_log(L_ERROR, "Failed to open %s: %s%s", path, strerror(-fd),
+ fd == -EINVAL ? "; is another display server running?" : "");
+ return fd;
+ }
+
+ struct stat st;
+ if (fstat(fd, &st) < 0) {
+ close(fd);
+ return -errno;
+ }
+
+ if (major(st.st_rdev) == DRM_MAJOR) {
+ session->base.drm_fd = fd;
+ }
+
+ return fd;
}
static void direct_session_close(struct wlr_session *base, int fd) {
+ struct direct_session *session = wl_container_of(base, session, base);
+
+ struct stat st;
+ if (fstat(fd, &st) < 0) {
+ wlr_log_errno(L_ERROR, "Stat failed");
+ close(fd);
+ return;
+ }
+
+ if (major(st.st_rdev) == DRM_MAJOR) {
+ direct_ipc_dropmaster(session->sock, session->base.drm_fd);
+ session->base.drm_fd = -1;
+ } else if (major(st.st_rdev) == INPUT_MAJOR) {
+ ioctl(fd, EVIOCREVOKE, 0);
+ }
+
close(fd);
}
-static bool direct_change_vt(struct wlr_session *base, int vt) {
- // TODO
- return false;
+static bool direct_change_vt(struct wlr_session *base, unsigned vt) {
+ struct direct_session *session = wl_container_of(base, session, base);
+ return ioctl(session->tty_fd, VT_ACTIVATE, (int)vt) == 0;
}
static void direct_session_finish(struct wlr_session *base) {
struct direct_session *session = wl_container_of(base, session, base);
+ struct vt_mode mode = {
+ .mode = VT_AUTO,
+ };
+
+ errno = 0;
+
+ ioctl(session->tty_fd, KDSKBMODE, session->old_kbmode);
+ ioctl(session->tty_fd, KDSETMODE, KD_TEXT);
+ ioctl(session->tty_fd, VT_SETMODE, &mode);
+ if (errno) {
+ wlr_log(L_ERROR, "Failed to restore tty");
+ }
+
+ direct_ipc_finish(session->sock, session->child);
+ close(session->sock);
+
+ wl_event_source_remove(session->vt_source);
+ close(session->tty_fd);
free(session);
}
+static int vt_handler(int signo, void *data) {
+ struct direct_session *session = data;
+
+ if (session->base.active) {
+ session->base.active = false;
+ wl_signal_emit(&session->base.session_signal, session);
+ direct_ipc_dropmaster(session->sock, session->base.drm_fd);
+ ioctl(session->tty_fd, VT_RELDISP, 1);
+ } else {
+ ioctl(session->tty_fd, VT_RELDISP, VT_ACKACQ);
+ direct_ipc_setmaster(session->sock, session->base.drm_fd);
+ session->base.active = true;
+ wl_signal_emit(&session->base.session_signal, session);
+ }
+
+ return 1;
+}
+
+static bool setup_tty(struct direct_session *session, struct wl_display *display) {
+ int fd = dup(STDIN_FILENO);
+ if (fd == -1) {
+ wlr_log_errno(L_ERROR, "Cannot open tty");
+ return false;
+ }
+
+ struct stat st;
+ if (fstat(fd, &st) == -1 || major(st.st_rdev) != TTY_MAJOR || minor(st.st_rdev) == 0) {
+ wlr_log(L_ERROR, "Not running from a virtual terminal");
+ goto error;
+ }
+
+ int tty = minor(st.st_rdev);
+ int ret, kd_mode, old_kbmode;
+
+ ret = ioctl(fd, KDGETMODE, &kd_mode);
+ if (ret) {
+ wlr_log_errno(L_ERROR, "Failed to get tty mode");
+ goto error;
+ }
+
+ if (kd_mode != KD_TEXT) {
+ wlr_log(L_ERROR,
+ "tty already in graphics mode; is another display server running?");
+ goto error;
+ }
+
+ ioctl(fd, VT_ACTIVATE, tty);
+ ioctl(fd, VT_WAITACTIVE, tty);
+
+ if (ioctl(fd, KDGKBMODE, &old_kbmode)) {
+ wlr_log_errno(L_ERROR, "Failed to read keyboard mode");
+ goto error;
+ }
+
+ if (ioctl(fd, KDSKBMODE, K_OFF)) {
+ wlr_log_errno(L_ERROR, "Failed to set keyboard mode");
+ goto error;
+ }
+
+ if (ioctl(fd, KDSETMODE, KD_GRAPHICS)) {
+ wlr_log_errno(L_ERROR, "Failed to set graphics mode on tty");
+ goto error;
+ }
+
+ struct vt_mode mode = {
+ .mode = VT_PROCESS,
+ .relsig = SIGUSR1,
+ .acqsig = SIGUSR1,
+ };
+
+ if (ioctl(fd, VT_SETMODE, &mode) < 0) {
+ wlr_log(L_ERROR, "Failed to take control of tty");
+ goto error;
+ }
+
+ struct wl_event_loop *loop = wl_display_get_event_loop(display);
+ session->vt_source = wl_event_loop_add_signal(loop, SIGUSR1,
+ vt_handler, session);
+ if (!session->vt_source) {
+ goto error;
+ }
+
+ session->base.vtnr = tty;
+ session->tty_fd = fd;
+ session->old_kbmode = old_kbmode;
+
+ return true;
+
+error:
+ close(fd);
+ return false;
+}
+
static struct wlr_session *direct_session_start(struct wl_display *disp) {
struct direct_session *session = calloc(1, sizeof(*session));
if (!session) {
- wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
+ wlr_log_errno(L_ERROR, "Allocation failed");
return NULL;
}
+ session->sock = direct_ipc_start(&session->child);
+ if (session->sock == -1) {
+ goto error_session;
+ }
+
+ if (!setup_tty(session, disp)) {
+ goto error_ipc;
+ }
+
+ // XXX: Is it okay to trust the environment like this?
+ const char *seat = getenv("XDG_SEAT");
+ if (!seat) {
+ seat = "seat0";
+ }
+
wlr_log(L_INFO, "Successfully loaded direct session");
+ snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat);
+ session->base.drm_fd = -1;
session->base.impl = &session_direct;
session->base.active = true;
wl_signal_init(&session->base.session_signal);
return &session->base;
+
+error_ipc:
+ direct_ipc_finish(session->sock, session->child);
+ close(session->sock);
+error_session:
+ free(session);
+ return NULL;
}
const struct session_impl session_direct = {
diff --git a/session/logind.c b/session/logind.c
index 6eb837ed..cfbdb4c7 100644
--- a/session/logind.c
+++ b/session/logind.c
@@ -26,13 +26,9 @@ struct logind_session {
char *id;
char *path;
- char *seat;
-
- int drm_fd;
};
-static int logind_take_device(struct wlr_session *restrict base,
- const char *restrict path) {
+static int logind_take_device(struct wlr_session *base, const char *path) {
struct logind_session *session = wl_container_of(base, session, base);
int ret;
@@ -72,7 +68,7 @@ static int logind_take_device(struct wlr_session *restrict base,
}
if (major(st.st_rdev) == DRM_MAJOR) {
- session->drm_fd = fd;
+ session->base.drm_fd = fd;
}
error:
@@ -102,14 +98,14 @@ static void logind_release_device(struct wlr_session *base, int fd) {
}
if (major(st.st_rdev) == DRM_MAJOR) {
- session->drm_fd = -1;
+ session->base.drm_fd = -1;
}
sd_bus_error_free(&error);
sd_bus_message_unref(msg);
}
-static bool logind_change_vt(struct wlr_session *base, int vt) {
+static bool logind_change_vt(struct wlr_session *base, unsigned vt) {
struct logind_session *session = wl_container_of(base, session, base);
int ret;
@@ -187,7 +183,6 @@ static void logind_session_finish(struct wlr_session *base) {
sd_bus_unref(session->bus);
free(session->id);
free(session->path);
- free(session->seat);
free(session);
}
@@ -242,7 +237,7 @@ static int resume_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_
}
if (major == DRM_MAJOR) {
- dup2(fd, session->drm_fd);
+ dup2(fd, session->base.drm_fd);
session->base.active = true;
wl_signal_emit(&session->base.session_signal, session);
}
@@ -305,11 +300,20 @@ static struct wlr_session *logind_session_start(struct wl_display *disp) {
goto error;
}
- ret = sd_session_get_seat(session->id, &session->seat);
+ ret = sd_session_get_vt(session->id, &session->base.vtnr);
+ if (ret < 0) {
+ wlr_log(L_ERROR, "Session not running in virtual terminal");
+ goto error;
+ }
+
+ char *seat;
+ ret = sd_session_get_seat(session->id, &seat);
if (ret < 0) {
wlr_log(L_ERROR, "Failed to get seat id: %s", strerror(-ret));
goto error;
}
+ snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat);
+ free(seat);
const char *fmt = "/org/freedesktop/login1/session/%s";
int len = snprintf(NULL, 0, fmt, session->id);
@@ -345,7 +349,7 @@ static struct wlr_session *logind_session_start(struct wl_display *disp) {
wlr_log(L_INFO, "Successfully loaded logind session");
- session->drm_fd = -1;
+ session->base.drm_fd = -1;
session->base.impl = &session_logind;
session->base.active = true;
wl_signal_init(&session->base.session_signal);
@@ -357,7 +361,6 @@ error_bus:
error:
free(session->path);
free(session->id);
- free(session->seat);
return NULL;
}
diff --git a/session/session.c b/session/session.c
index 9de93b1d..6f6aa2ea 100644
--- a/session/session.c
+++ b/session/session.c
@@ -33,9 +33,7 @@ void wlr_session_finish(struct wlr_session *session) {
session->impl->finish(session);
};
-int wlr_session_open_file(struct wlr_session *restrict session,
- const char *restrict path) {
-
+int wlr_session_open_file(struct wlr_session *session, const char *path) {
return session->impl->open(session, path);
}
@@ -43,6 +41,6 @@ void wlr_session_close_file(struct wlr_session *session, int fd) {
session->impl->close(session, fd);
}
-bool wlr_session_change_vt(struct wlr_session *session, int vt) {
+bool wlr_session_change_vt(struct wlr_session *session, unsigned vt) {
return session->impl->change_vt(session, vt);
}