From bd5bca52830e78364ab48efb0f6e4d0f1d9773db Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 9 Jul 2017 17:53:13 +1200 Subject: Added public fields to wlr_session --- include/wlr/session.h | 12 +++++++----- include/wlr/session/interface.h | 5 ++--- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/wlr/session.h b/include/wlr/session.h index 4a5b2174..52dbf8ca 100644 --- a/include/wlr/session.h +++ b/include/wlr/session.h @@ -9,16 +9,18 @@ struct session_impl; struct wlr_session { const struct session_impl *impl; - - bool active; struct wl_signal session_signal; + bool active; + + int drm_fd; + unsigned vtnr; + char seat[8]; }; struct wlr_session *wlr_session_start(struct wl_display *disp); void wlr_session_finish(struct wlr_session *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); void wlr_session_close_file(struct wlr_session *session, int fd); -bool wlr_session_change_vt(struct wlr_session *session, int vt); +bool wlr_session_change_vt(struct wlr_session *session, unsigned vt); #endif diff --git a/include/wlr/session/interface.h b/include/wlr/session/interface.h index f75acfa4..4938110d 100644 --- a/include/wlr/session/interface.h +++ b/include/wlr/session/interface.h @@ -6,10 +6,9 @@ struct session_impl { struct wlr_session *(*start)(struct wl_display *disp); void (*finish)(struct wlr_session *session); - int (*open)(struct wlr_session *restrict session, - const char *restrict path); + int (*open)(struct wlr_session *session, const char *path); void (*close)(struct wlr_session *session, int fd); - bool (*change_vt)(struct wlr_session *session, int vt); + bool (*change_vt)(struct wlr_session *session, unsigned vt); }; #endif -- cgit v1.2.3 From 2f2c8205d841a98649e9697ad5ba8cfe430ffaa7 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 9 Jul 2017 22:12:50 +1200 Subject: Moved IPC to its own file. --- include/session/direct-ipc.h | 12 ++ session/CMakeLists.txt | 1 + session/direct-ipc.c | 241 +++++++++++++++++++++++++++++++++++++ session/direct.c | 274 ++++++++----------------------------------- 4 files changed, 303 insertions(+), 225 deletions(-) create mode 100644 include/session/direct-ipc.h create mode 100644 session/direct-ipc.c (limited to 'include') diff --git a/include/session/direct-ipc.h b/include/session/direct-ipc.h new file mode 100644 index 00000000..a8d9469d --- /dev/null +++ b/include/session/direct-ipc.h @@ -0,0 +1,12 @@ +#ifndef SESSION_DIRECT_IPC +#define SESSION_DIRECT_IPC + +#include + +int direct_ipc_open(int sock, const char *path); +void direct_ipc_setmaster(int sock); +void direct_ipc_dropmaster(int sock); +void direct_ipc_finish(int sock, pid_t pid); +int direct_ipc_start(pid_t *pid_out); + +#endif diff --git a/session/CMakeLists.txt b/session/CMakeLists.txt index d79991ee..bacd412f 100644 --- a/session/CMakeLists.txt +++ b/session/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories( set(sources session.c direct.c + direct-ipc.c ) set(libs diff --git a/session/direct-ipc.c b/session/direct-ipc.c new file mode 100644 index 00000000..ef8de68c --- /dev/null +++ b/session/direct-ipc.c @@ -0,0 +1,241 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "session/direct-ipc.h" + +enum { DRM_MAJOR = 226 }; + +#ifdef HAS_LIBCAP +#include + +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, NULL, &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) { + if (drmSetMaster(fd)) { + ret = errno; + } else { + drm_fd = fd; + } + } +error: + send_msg(sock, ret ? -1 : fd, &ret, sizeof(ret)); + + if (fd != drm_fd) { + close(fd); + } + + break; + + case MSG_SETMASTER: + drmSetMaster(drm_fd); + send_msg(sock, -1, NULL, 0); + break; + + case MSG_DROPMASTER: + drmDropMaster(drm_fd); + send_msg(sock, -1, NULL, 0); + break; + + case MSG_END: + running = false; + send_msg(sock, -1, NULL, 0); + break; + } + } + + close(drm_fd); + 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) { + struct msg msg = { .type = MSG_SETMASTER }; + + send_msg(sock, -1, &msg, sizeof(msg)); + recv_msg(sock, NULL, NULL, 0); +} + +void direct_ipc_dropmaster(int sock) { + struct msg msg = { .type = MSG_DROPMASTER }; + + send_msg(sock, -1, &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 3932af68..23ce1ee1 100644 --- a/session/direct.c +++ b/session/direct.c @@ -3,25 +3,19 @@ #include #include #include -#include -#include #include #include #include #include #include -#include -#include #include #include +#include #include #include -#include #include #include -#ifdef HAS_LIBCAP -#include -#endif +#include "session/direct-ipc.h" enum { DRM_MAJOR = 226 }; @@ -37,82 +31,23 @@ struct direct_session { struct wl_event_source *vt_source; }; -enum session_message_type { - SESSION_OPEN, - SESSION_SETMASTER, - SESSION_DROPMASTER, - SESSION_END, -}; - -struct session_message { - enum session_message_type type; - char path[60]; -}; - -static int send_message(int sock, enum session_message_type type, const char *path) { - struct session_message msg = { - .type = type, - }; - struct msghdr request = { - .msg_iov = &(struct iovec) { - .iov_base = &msg, - .iov_len = sizeof(msg), - }, - .msg_iovlen = 1, - }; - - if (path) { - snprintf(msg.path, sizeof(msg.path), "%s", path); - } - - sendmsg(sock, &request, 0); - - int err = 0, fd = -1; - char control[CMSG_SPACE(sizeof(fd))] = {0}; - struct msghdr reply = { - .msg_iov = &(struct iovec) { - .iov_base = &err, - .iov_len = sizeof(err), - }, - .msg_iovlen = 1, - .msg_control = control, - .msg_controllen = sizeof(control), - }; - - recvmsg(sock, &reply, 0); - - // The other types have no meaningful return value - if (type != SESSION_OPEN) { - return 0; - } - - struct cmsghdr *cmsg = CMSG_FIRSTHDR(&reply); - memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd)); - - return err ? -err : fd; -} - static int direct_session_open(struct wlr_session *base, const char *path) { struct direct_session *session = wl_container_of(base, session, base); - struct stat st; - if (stat(path, &st)) { - return -errno; - } - - uint32_t maj = major(st.st_rdev); - if (maj != DRM_MAJOR && maj != INPUT_MAJOR) { - return -EINVAL; - } - - int fd = send_message(session->sock, SESSION_OPEN, path); + 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; } - if (maj == DRM_MAJOR) { + struct stat st; + if (fstat(fd, &st) < 0) { + close(fd); + return -errno; + } + + if (major(st.st_rdev) == DRM_MAJOR) { session->base.drm_fd = fd; } @@ -122,9 +57,18 @@ static int direct_session_open(struct wlr_session *base, const char *path) { static void direct_session_close(struct wlr_session *base, int fd) { struct direct_session *session = wl_container_of(base, session, base); - if (fd == session->base.drm_fd) { - send_message(session->sock, SESSION_DROPMASTER, NULL); + 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 = -1; + } else if (major(st.st_rdev) == INPUT_MAJOR) { + ioctl(fd, EVIOCREVOKE, 0); } close(fd); @@ -151,9 +95,8 @@ static void direct_session_finish(struct wlr_session *base) { wlr_log(L_ERROR, "Failed to restore tty"); } - send_message(session->sock, SESSION_END, NULL); + direct_ipc_finish(session->sock, session->child); close(session->sock); - wait(NULL); wl_event_source_remove(session->vt_source); close(session->tty_fd); @@ -166,11 +109,11 @@ static int vt_handler(int signo, void *data) { if (session->base.active) { session->base.active = false; wl_signal_emit(&session->base.session_signal, session); - send_message(session->sock, SESSION_DROPMASTER, NULL); + direct_ipc_dropmaster(session->sock); ioctl(session->tty_fd, VT_RELDISP, 1); } else { ioctl(session->tty_fd, VT_RELDISP, VT_ACKACQ); - send_message(session->sock, SESSION_SETMASTER, NULL); + direct_ipc_setmaster(session->sock); session->base.active = true; wl_signal_emit(&session->base.session_signal, session); } @@ -179,24 +122,22 @@ static int vt_handler(int signo, void *data) { } static bool setup_tty(struct direct_session *session, struct wl_display *display) { - session->tty_fd = dup(STDIN_FILENO); - if (session->tty_fd == -1) { + int fd = dup(STDIN_FILENO); + if (fd == -1) { wlr_log_errno(L_ERROR, "Cannot open tty"); return false; } struct stat st; - if (fstat(session->tty_fd, &st) == -1 || major(st.st_rdev) != TTY_MAJOR || - minor(st.st_rdev) == 0) { + 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; - session->base.vtnr = tty; + int ret, kd_mode, old_kbmode; - ret = ioctl(session->tty_fd, KDGETMODE, &kd_mode); + ret = ioctl(fd, KDGETMODE, &kd_mode); if (ret) { wlr_log_errno(L_ERROR, "Failed to get tty mode"); goto error; @@ -208,20 +149,20 @@ static bool setup_tty(struct direct_session *session, struct wl_display *display goto error; } - ioctl(session->tty_fd, VT_ACTIVATE, tty); - ioctl(session->tty_fd, VT_WAITACTIVE, tty); + ioctl(fd, VT_ACTIVATE, tty); + ioctl(fd, VT_WAITACTIVE, tty); - if (ioctl(session->tty_fd, KDGKBMODE, &session->old_kbmode)) { + if (ioctl(fd, KDGKBMODE, &old_kbmode)) { wlr_log_errno(L_ERROR, "Failed to read keyboard mode"); goto error; } - if (ioctl(session->tty_fd, KDSKBMODE, K_OFF)) { + if (ioctl(fd, KDSKBMODE, K_OFF)) { wlr_log_errno(L_ERROR, "Failed to set keyboard mode"); goto error; } - if (ioctl(session->tty_fd, KDSETMODE, KD_GRAPHICS)) { + if (ioctl(fd, KDSETMODE, KD_GRAPHICS)) { wlr_log_errno(L_ERROR, "Failed to set graphics mode on tty"); goto error; } @@ -232,7 +173,7 @@ static bool setup_tty(struct direct_session *session, struct wl_display *display .acqsig = SIGUSR1, }; - if (ioctl(session->tty_fd, VT_SETMODE, &mode) < 0) { + if (ioctl(fd, VT_SETMODE, &mode) < 0) { wlr_log(L_ERROR, "Failed to take control of tty"); goto error; } @@ -244,145 +185,31 @@ static bool setup_tty(struct direct_session *session, struct wl_display *display goto error; } + session->base.vtnr = tty; + session->tty_fd = fd; + session->old_kbmode = old_kbmode; + return true; error: - close(session->tty_fd); + close(fd); return false; } -static void communicate(int sock) { - struct session_message msg; - struct msghdr hdr = { - .msg_iov = &(struct iovec) { - .iov_base = &msg, - .iov_len = sizeof(msg), - }, - .msg_iovlen = 1, - }; - - int drm_fd = -1; - - while (recvmsg(sock, &hdr, 0) >= 0 || errno == EINTR) { - switch (msg.type) { - case SESSION_OPEN: - errno = 0; - // These are the flags that logind use - int fd = open(msg.path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK); - int e = errno; - - struct stat st; - if (fstat(fd, &st) >= 0 && major(st.st_rdev) == DRM_MAJOR) { - if (drmSetMaster(fd)) { - close(fd); - fd = -1; - e = errno; - } - - drm_fd = fd; - } - - char control[CMSG_SPACE(sizeof(fd))] = {0}; - struct msghdr reply = { - .msg_iov = &(struct iovec) { - .iov_base = &e, - .iov_len = sizeof(e), - }, - .msg_iovlen = 1, - .msg_control = &control, - .msg_controllen = sizeof(control), - }; - struct cmsghdr *cmsg = CMSG_FIRSTHDR(&reply); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); - memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); - - sendmsg(sock, &reply, 0); - break; - case SESSION_SETMASTER: - if (drm_fd != -1) { - drmSetMaster(drm_fd); - } - - sendmsg(sock, &(struct msghdr){0}, 0); - break; - case SESSION_DROPMASTER: - if (drm_fd != -1) { - drmDropMaster(drm_fd); - } - - sendmsg(sock, &(struct msghdr){0}, 0); - break; - case SESSION_END: - sendmsg(sock, &(struct msghdr){0}, 0); - return; - } - } -} - -#ifdef HAS_LIBCAP -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 struct wlr_session *direct_session_start(struct wl_display *disp) { - if (!have_permissions()) { - return NULL; - } - - int sock[2]; - if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock) < 0) { - wlr_log_errno(L_ERROR, "Failed to create socket pair"); - return NULL; - } - - pid_t pid = fork(); - if (pid < 0) { - wlr_log_errno(L_ERROR, "Fork failed"); - goto error_sock; - } else if (pid == 0) { - close(sock[0]); - - communicate(sock[1]); - - _Exit(0); - } - - close(sock[1]); - sock[1] = -1; - struct direct_session *session = calloc(1, sizeof(*session)); if (!session) { wlr_log_errno(L_ERROR, "Allocation failed"); - goto error_child; + return NULL; } - session->child = pid; - session->sock = sock[0]; + session->sock = direct_ipc_start(&session->child); + if (session->sock == -1) { + goto error_session; + } if (!setup_tty(session, disp)) { - goto error_session; + goto error_ipc; } // XXX: Is it okay to trust the environment like this? @@ -400,14 +227,11 @@ static struct wlr_session *direct_session_start(struct wl_display *disp) { 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); -error_child: - send_message(sock[0], SESSION_END, NULL); - wait(NULL); -error_sock: - close(sock[0]); - close(sock[1]); return NULL; } -- cgit v1.2.3 From 3779ef802ddf89067e1588077ed8c72b4674565a Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 9 Jul 2017 22:23:54 +1200 Subject: Stop remembering the drm fd in child. --- include/session/direct-ipc.h | 4 ++-- session/direct-ipc.c | 26 ++++++++++---------------- session/direct.c | 6 +++--- 3 files changed, 15 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/session/direct-ipc.h b/include/session/direct-ipc.h index a8d9469d..96504f04 100644 --- a/include/session/direct-ipc.h +++ b/include/session/direct-ipc.h @@ -4,8 +4,8 @@ #include int direct_ipc_open(int sock, const char *path); -void direct_ipc_setmaster(int sock); -void direct_ipc_dropmaster(int sock); +void direct_ipc_setmaster(int sock, int fd); +void direct_ipc_dropmaster(int sock, int fd); void direct_ipc_finish(int sock, pid_t pid); int direct_ipc_start(pid_t *pid_out); diff --git a/session/direct-ipc.c b/session/direct-ipc.c index ef8de68c..fbbde02a 100644 --- a/session/direct-ipc.c +++ b/session/direct-ipc.c @@ -116,7 +116,7 @@ static void communicate(int sock) { int drm_fd = -1; bool running = true; - while (running && recv_msg(sock, NULL, &msg, sizeof(msg)) >= 0) { + while (running && recv_msg(sock, &drm_fd, &msg, sizeof(msg)) >= 0) { switch (msg.type) { case MSG_OPEN: errno = 0; @@ -140,29 +140,24 @@ static void communicate(int sock) { goto error; } - if (maj == DRM_MAJOR) { - if (drmSetMaster(fd)) { - ret = errno; - } else { - drm_fd = fd; - } + if (maj == DRM_MAJOR && drmSetMaster(fd)) { + ret = errno; } error: send_msg(sock, ret ? -1 : fd, &ret, sizeof(ret)); - - if (fd != drm_fd) { - close(fd); - } + 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; @@ -173,7 +168,6 @@ error: } } - close(drm_fd); close(sock); } @@ -189,17 +183,17 @@ int direct_ipc_open(int sock, const char *path) { return err ? -err : fd; } -void direct_ipc_setmaster(int sock) { +void direct_ipc_setmaster(int sock, int fd) { struct msg msg = { .type = MSG_SETMASTER }; - send_msg(sock, -1, &msg, sizeof(msg)); + send_msg(sock, fd, &msg, sizeof(msg)); recv_msg(sock, NULL, NULL, 0); } -void direct_ipc_dropmaster(int sock) { +void direct_ipc_dropmaster(int sock, int fd) { struct msg msg = { .type = MSG_DROPMASTER }; - send_msg(sock, -1, &msg, sizeof(msg)); + send_msg(sock, fd, &msg, sizeof(msg)); recv_msg(sock, NULL, NULL, 0); } diff --git a/session/direct.c b/session/direct.c index 23ce1ee1..1247663b 100644 --- a/session/direct.c +++ b/session/direct.c @@ -65,7 +65,7 @@ static void direct_session_close(struct wlr_session *base, int fd) { } if (major(st.st_rdev) == DRM_MAJOR) { - direct_ipc_dropmaster(session->sock); + 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); @@ -109,11 +109,11 @@ static int vt_handler(int signo, void *data) { if (session->base.active) { session->base.active = false; wl_signal_emit(&session->base.session_signal, session); - direct_ipc_dropmaster(session->sock); + 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); + direct_ipc_setmaster(session->sock, session->base.drm_fd); session->base.active = true; wl_signal_emit(&session->base.session_signal, session); } -- cgit v1.2.3 From 5b8ec107b9ca91ef8d81ce4e561605b808b6a03e Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 9 Jul 2017 23:02:41 +1200 Subject: Session documentation. --- include/wlr/session.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'include') diff --git a/include/wlr/session.h b/include/wlr/session.h index 52dbf8ca..7961e620 100644 --- a/include/wlr/session.h +++ b/include/wlr/session.h @@ -9,6 +9,10 @@ struct session_impl; struct wlr_session { const struct session_impl *impl; + /* + * Signal for when the session becomes active/inactive. + * It's called when we swap virtual terminal. + */ struct wl_signal session_signal; bool active; @@ -17,10 +21,45 @@ struct wlr_session { char seat[8]; }; +/* + * Opens a session, taking control of the current virtual terminal. + * This should not be called if another program is already in control + * of the terminal (Xorg, another Wayland compositor, etc.). + * + * If logind support is not enabled, you must have CAP_SYS_ADMIN or be root. + * It is safe to drop priviledges after this is called. + * + * Returns NULL on error. + */ struct wlr_session *wlr_session_start(struct wl_display *disp); + +/* + * Closes a previously opened session and restores the virtual terminal. + * You should call wlr_session_close_file on each files you opened + * with wlr_session_open_file before you call this. + */ void wlr_session_finish(struct wlr_session *session); + +/* + * Opens the file at path. + * This can only be used to open DRM or evdev (input) devices. + * + * When the session becomes inactive: + * - DRM files lose their DRM master status + * - evdev files become invalid and should be closed + * + * Returns -errno on error. + */ int wlr_session_open_file(struct wlr_session *session, const char *path); + +/* + * Closes a file previously opened with wlr_session_open_file. + */ void wlr_session_close_file(struct wlr_session *session, int fd); + +/* + * Changes the virtual terminal. + */ bool wlr_session_change_vt(struct wlr_session *session, unsigned vt); #endif -- cgit v1.2.3