launcher: Move rest of tty object into struct weston_launcher

The struct weston_launcher object will now either handle tty and vt switching
details in-process (when running weston directly as root) or talk to
the weston-launch process.
dev
Kristian Høgsberg 11 years ago
parent 6a7c8492cc
commit 3f4958755d
  1. 8
      src/Makefile.am
  2. 41
      src/compositor-drm.c
  3. 27
      src/compositor-fbdev.c
  4. 24
      src/compositor-rpi.c
  5. 158
      src/launcher-util.c
  6. 6
      src/launcher-util.h
  7. 294
      src/tty.c
  8. 44
      src/weston-launch.c

@ -144,7 +144,6 @@ drm_backend_la_CFLAGS = \
$(GCC_CFLAGS) $(GCC_CFLAGS)
drm_backend_la_SOURCES = \ drm_backend_la_SOURCES = \
compositor-drm.c \ compositor-drm.c \
tty.c \
udev-seat.c \ udev-seat.c \
udev-seat.h \ udev-seat.h \
evdev.c \ evdev.c \
@ -193,7 +192,8 @@ rpi_backend_la_SOURCES = \
rpi-renderer.c \ rpi-renderer.c \
rpi-renderer.h \ rpi-renderer.h \
rpi-bcm-stubs.h \ rpi-bcm-stubs.h \
tty.c \ launcher-util.c \
launcher-util.h \
evdev.c \ evdev.c \
evdev.h \ evdev.h \
evdev-touchpad.c evdev-touchpad.c
@ -224,13 +224,13 @@ fbdev_backend_la_CFLAGS = \
$(GCC_CFLAGS) $(GCC_CFLAGS)
fbdev_backend_la_SOURCES = \ fbdev_backend_la_SOURCES = \
compositor-fbdev.c \ compositor-fbdev.c \
tty.c \
udev-seat.c \ udev-seat.c \
udev-seat.h \ udev-seat.h \
evdev.c \ evdev.c \
evdev.h \ evdev.h \
evdev-touchpad.c \ evdev-touchpad.c \
launcher-util.c launcher-util.c \
launcher-util.h
endif endif
if ENABLE_RDP_COMPOSITOR if ENABLE_RDP_COMPOSITOR

@ -30,6 +30,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/vt.h>
#include <assert.h> #include <assert.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <time.h> #include <time.h>
@ -83,7 +84,6 @@ struct drm_compositor {
int num_crtcs; int num_crtcs;
uint32_t crtc_allocator; uint32_t crtc_allocator;
uint32_t connector_allocator; uint32_t connector_allocator;
struct tty *tty;
struct wl_listener session_listener; struct wl_listener session_listener;
/* we need these parameters in order to not fail drmModeAddFB2() /* we need these parameters in order to not fail drmModeAddFB2()
@ -2240,13 +2240,7 @@ udev_drm_event(int fd, uint32_t mask, void *data)
static void static void
drm_restore(struct weston_compositor *ec) drm_restore(struct weston_compositor *ec)
{ {
struct drm_compositor *d = (struct drm_compositor *) ec; weston_launcher_restore(ec->launcher);
if (ec->launcher == NULL) {
if (drmDropMaster(d->drm.fd) < 0)
weston_log("failed to drop master: %m\n");
tty_reset(d->tty);
}
} }
static void static void
@ -2268,11 +2262,7 @@ drm_destroy(struct weston_compositor *ec)
if (d->gbm) if (d->gbm)
gbm_device_destroy(d->gbm); gbm_device_destroy(d->gbm);
if (d->base.launcher == NULL) { weston_launcher_destroy(d->base.launcher);
if (drmDropMaster(d->drm.fd) < 0)
weston_log("failed to drop master: %m\n");
tty_destroy(d->tty);
}
close(d->drm.fd); close(d->drm.fd);
@ -2368,10 +2358,9 @@ session_notify(struct wl_listener *listener, void *data)
static void static void
switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data) switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
{ {
struct drm_compositor *ec = data; struct weston_compositor *compositor = data;
if (ec->tty) weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
tty_activate_vt(ec->tty, key - KEY_F1 + 1);
} }
/* /*
@ -2575,7 +2564,7 @@ drm_compositor_create(struct wl_display *display,
/* Check if we run drm-backend using weston-launch */ /* Check if we run drm-backend using weston-launch */
ec->base.launcher = weston_launcher_connect(&ec->base); ec->base.launcher = weston_launcher_connect(&ec->base);
if (ec->base.launcher == NULL && geteuid() != 0) { if (ec->base.launcher == NULL) {
weston_log("fatal: drm backend should be run " weston_log("fatal: drm backend should be run "
"using weston-launch binary or as root\n"); "using weston-launch binary or as root\n");
goto err_compositor; goto err_compositor;
@ -2584,24 +2573,17 @@ drm_compositor_create(struct wl_display *display,
ec->udev = udev_new(); ec->udev = udev_new();
if (ec->udev == NULL) { if (ec->udev == NULL) {
weston_log("failed to initialize udev context\n"); weston_log("failed to initialize udev context\n");
goto err_compositor; goto err_launcher;
} }
ec->base.wl_display = display; ec->base.wl_display = display;
ec->session_listener.notify = session_notify; ec->session_listener.notify = session_notify;
wl_signal_add(&ec->base.session_signal, &ec->session_listener); wl_signal_add(&ec->base.session_signal, &ec->session_listener);
if (ec->base.launcher == NULL) {
ec->tty = tty_create(&ec->base, tty);
if (!ec->tty) {
weston_log("failed to initialize tty\n");
goto err_udev;
}
}
drm_device = find_primary_gpu(ec, seat_id); drm_device = find_primary_gpu(ec, seat_id);
if (drm_device == NULL) { if (drm_device == NULL) {
weston_log("no drm device found\n"); weston_log("no drm device found\n");
goto err_tty; goto err_udev;
} }
path = udev_device_get_syspath(drm_device); path = udev_device_get_syspath(drm_device);
@ -2696,11 +2678,8 @@ err_sprite:
destroy_sprites(ec); destroy_sprites(ec);
err_udev_dev: err_udev_dev:
udev_device_unref(drm_device); udev_device_unref(drm_device);
err_tty: err_launcher:
if (ec->base.launcher == NULL && drmDropMaster(ec->drm.fd) < 0) weston_launcher_destroy(ec->base.launcher);
weston_log("failed to drop master: %m\n");
if (ec->tty)
tty_destroy(ec->tty);
err_udev: err_udev:
udev_unref(ec->udev); udev_unref(ec->udev);
err_compositor: err_compositor:

@ -50,7 +50,6 @@ struct fbdev_compositor {
uint32_t prev_state; uint32_t prev_state;
struct udev *udev; struct udev *udev;
struct tty *tty;
struct udev_input input; struct udev_input input;
int use_pixman; int use_pixman;
struct wl_listener session_listener; struct wl_listener session_listener;
@ -799,7 +798,7 @@ fbdev_compositor_destroy(struct weston_compositor *base)
/* Chain up. */ /* Chain up. */
compositor->base.renderer->destroy(&compositor->base); compositor->base.renderer->destroy(&compositor->base);
tty_destroy(compositor->tty); weston_launcher_destroy(compositor->base.launcher);
free(compositor); free(compositor);
} }
@ -849,19 +848,17 @@ session_notify(struct wl_listener *listener, void *data)
} }
static void static void
fbdev_restore(struct weston_compositor *base) fbdev_restore(struct weston_compositor *compositor)
{ {
struct fbdev_compositor *compositor = to_fbdev_compositor(base); weston_launcher_restore(compositor->launcher);
tty_reset(compositor->tty);
} }
static void static void
switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data) switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
{ {
struct fbdev_compositor *ec = data; struct weston_compositor *compositor = data;
tty_activate_vt(ec->tty, key - KEY_F1 + 1); weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
} }
static struct weston_compositor * static struct weston_compositor *
@ -901,9 +898,9 @@ fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
compositor->session_listener.notify = session_notify; compositor->session_listener.notify = session_notify;
wl_signal_add(&compositor->base.session_signal, wl_signal_add(&compositor->base.session_signal,
&compositor->session_listener); &compositor->session_listener);
compositor->tty = tty_create(&compositor->base, param->tty); compositor->base.launcher = weston_launcher_connect(&compositor->base);
if (!compositor->tty) { if (!compositor->base.launcher) {
weston_log("Failed to initialize tty.\n"); weston_log("Failed to set up launcher.\n");
goto out_udev; goto out_udev;
} }
@ -921,12 +918,12 @@ fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
compositor); compositor);
if (compositor->use_pixman) { if (compositor->use_pixman) {
if (pixman_renderer_init(&compositor->base) < 0) if (pixman_renderer_init(&compositor->base) < 0)
goto out_tty; goto out_launcher;
} else { } else {
if (gl_renderer_create(&compositor->base, EGL_DEFAULT_DISPLAY, if (gl_renderer_create(&compositor->base, EGL_DEFAULT_DISPLAY,
gl_renderer_opaque_attribs, NULL) < 0) { gl_renderer_opaque_attribs, NULL) < 0) {
weston_log("gl_renderer_create failed.\n"); weston_log("gl_renderer_create failed.\n");
goto out_tty; goto out_launcher;
} }
} }
@ -940,8 +937,8 @@ fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
out_pixman: out_pixman:
compositor->base.renderer->destroy(&compositor->base); compositor->base.renderer->destroy(&compositor->base);
out_tty: out_launcher:
tty_destroy(compositor->tty); weston_launcher_destroy(compositor->base.launcher);
out_udev: out_udev:
udev_unref(compositor->udev); udev_unref(compositor->udev);

@ -44,6 +44,7 @@
#include "compositor.h" #include "compositor.h"
#include "rpi-renderer.h" #include "rpi-renderer.h"
#include "evdev.h" #include "evdev.h"
#include "launcher-util.h"
#if 0 #if 0
#define DBG(...) \ #define DBG(...) \
@ -86,7 +87,6 @@ struct rpi_compositor {
uint32_t prev_state; uint32_t prev_state;
struct udev *udev; struct udev *udev;
struct tty *tty;
struct wl_listener session_listener; struct wl_listener session_listener;
int single_buffer; int single_buffer;
@ -656,7 +656,7 @@ rpi_compositor_destroy(struct weston_compositor *base)
weston_compositor_shutdown(&compositor->base); weston_compositor_shutdown(&compositor->base);
compositor->base.renderer->destroy(&compositor->base); compositor->base.renderer->destroy(&compositor->base);
tty_destroy(compositor->tty); weston_launcher_destroy(compositor->base.launcher);
bcm_host_deinit(); bcm_host_deinit();
free(compositor); free(compositor);
@ -705,19 +705,17 @@ session_notify(struct wl_listener *listener, void *data)
} }
static void static void
rpi_restore(struct weston_compositor *base) rpi_restore(struct weston_compositor *compositor)
{ {
struct rpi_compositor *compositor = to_rpi_compositor(base); weston_launcher_restore(compositor->launcher);
tty_reset(compositor->tty);
} }
static void static void
switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data) switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
{ {
struct rpi_compositor *ec = data; struct weston_compositor *compositor = data;
tty_activate_vt(ec->tty, key - KEY_F1 + 1); weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
} }
struct rpi_parameters { struct rpi_parameters {
@ -754,8 +752,8 @@ rpi_compositor_create(struct wl_display *display, int *argc, char *argv[],
compositor->session_listener.notify = session_notify; compositor->session_listener.notify = session_notify;
wl_signal_add(&compositor->base.session_signal, wl_signal_add(&compositor->base.session_signal,
&compositor ->session_listener); &compositor ->session_listener);
compositor->tty = tty_create(&compositor->base, param->tty); compositor->base.launcher = weston_launcher_connect(&compositor->base);
if (!compositor->tty) { if (!compositor->base.launcher) {
weston_log("Failed to initialize tty.\n"); weston_log("Failed to initialize tty.\n");
goto out_udev; goto out_udev;
} }
@ -785,7 +783,7 @@ rpi_compositor_create(struct wl_display *display, int *argc, char *argv[],
bcm_host_init(); bcm_host_init();
if (rpi_renderer_create(&compositor->base, &param->renderer) < 0) if (rpi_renderer_create(&compositor->base, &param->renderer) < 0)
goto out_tty; goto out_launcher;
if (rpi_output_create(compositor, param->output_transform) < 0) if (rpi_output_create(compositor, param->output_transform) < 0)
goto out_renderer; goto out_renderer;
@ -797,8 +795,8 @@ rpi_compositor_create(struct wl_display *display, int *argc, char *argv[],
out_renderer: out_renderer:
compositor->base.renderer->destroy(&compositor->base); compositor->base.renderer->destroy(&compositor->base);
out_tty: out_launcher:
tty_destroy(compositor->tty); weston_launcher_destroy(compositor->base.launcher);
out_udev: out_udev:
udev_unref(compositor->udev); udev_unref(compositor->udev);

@ -28,12 +28,17 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <signal.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/ioctl.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <linux/vt.h>
#include <linux/kd.h>
#include <linux/major.h>
#include <xf86drm.h> #include <xf86drm.h>
@ -41,19 +46,28 @@
#include "launcher-util.h" #include "launcher-util.h"
#include "weston-launch.h" #include "weston-launch.h"
#define DRM_MAJOR 226
#ifndef KDSKBMUTE
#define KDSKBMUTE 0x4B51
#endif
union cmsg_data { unsigned char b[4]; int fd; }; union cmsg_data { unsigned char b[4]; int fd; };
struct weston_launcher { struct weston_launcher {
struct weston_compositor *compositor; struct weston_compositor *compositor;
int fd; int fd;
struct wl_event_source *source; struct wl_event_source *source;
int kb_mode, tty, drm_fd;
struct wl_event_source *vt_source;
}; };
int int
weston_launcher_open(struct weston_launcher *launcher, weston_launcher_open(struct weston_launcher *launcher,
const char *path, int flags) const char *path, int flags)
{ {
int n, ret = -1; int n, fd, ret = -1;
struct msghdr msg; struct msghdr msg;
struct cmsghdr *cmsg; struct cmsghdr *cmsg;
struct iovec iov; struct iovec iov;
@ -61,9 +75,24 @@ weston_launcher_open(struct weston_launcher *launcher,
char control[CMSG_SPACE(sizeof data->fd)]; char control[CMSG_SPACE(sizeof data->fd)];
ssize_t len; ssize_t len;
struct weston_launcher_open *message; struct weston_launcher_open *message;
struct stat s;
if (launcher == NULL) if (launcher->fd == -1) {
return open(path, flags | O_CLOEXEC); fd = open(path, flags | O_CLOEXEC);
if (fd == -1)
return -1;
if (fstat(fd, &s) == -1) {
close(fd);
return -1;
}
if (major(s.st_rdev) == DRM_MAJOR)
launcher->drm_fd = fd;
return fd;
}
n = sizeof(*message) + strlen(path) + 1; n = sizeof(*message) + strlen(path) + 1;
message = malloc(n); message = malloc(n);
@ -112,6 +141,23 @@ weston_launcher_open(struct weston_launcher *launcher,
return data->fd; return data->fd;
} }
void
weston_launcher_restore(struct weston_launcher *launcher)
{
struct vt_mode mode = { 0 };
if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
weston_log("failed to restore kb mode: %m\n");
if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
weston_log("failed to set KD_TEXT mode on tty: %m\n");
mode.mode = VT_AUTO;
if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
weston_log("could not reset vt handling\n");
}
static int static int
weston_launcher_data(int fd, uint32_t mask, void *data) weston_launcher_data(int fd, uint32_t mask, void *data)
{ {
@ -120,6 +166,10 @@ weston_launcher_data(int fd, uint32_t mask, void *data)
if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) { if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
weston_log("launcher socket closed, exiting\n"); weston_log("launcher socket closed, exiting\n");
/* Normally the weston-launch will reset the tty, but
* in this case it died or something, so do it here so
* we don't end up with a stuck vt. */
weston_launcher_restore(launcher);
exit(-1); exit(-1);
} }
@ -146,24 +196,99 @@ weston_launcher_data(int fd, uint32_t mask, void *data)
return 1; return 1;
} }
static int
vt_handler(int signal_number, void *data)
{
struct weston_launcher *launcher = data;
struct weston_compositor *compositor = launcher->compositor;
if (compositor->session_active) {
compositor->session_active = 0;
wl_signal_emit(&compositor->session_signal, compositor);
if (launcher->drm_fd != -1)
drmDropMaster(launcher->drm_fd);
ioctl(launcher->tty, VT_RELDISP, 1);
} else {
ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
if (launcher->drm_fd != -1)
drmSetMaster(launcher->drm_fd);
compositor->session_active = 1;
wl_signal_emit(&compositor->session_signal, compositor);
}
return 1;
}
static int
setup_tty(struct weston_launcher *launcher)
{
struct wl_event_loop *loop;
struct vt_mode mode = { 0 };
struct stat buf;
int ret;
if (fstat(STDIN_FILENO, &buf) == -1 ||
major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
weston_log("stdin not a vt\n");
return -1;
}
launcher->tty = STDIN_FILENO;
if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
weston_log("failed to read keyboard mode: %m\n");
return -1;
}
if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
weston_log("failed to set K_OFF keyboard mode: %m\n");
return -1;
}
ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
if (ret) {
weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
return -1;
}
mode.mode = VT_PROCESS;
mode.relsig = SIGUSR1;
mode.acqsig = SIGUSR1;
if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
weston_log("failed to take control of vt handling\n");
return -1;
}
loop = wl_display_get_event_loop(launcher->compositor->wl_display);
launcher->vt_source =
wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, launcher);
if (!launcher->vt_source)
return -1;
return 0;
}
int
weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
{
return ioctl(launcher->tty, VT_ACTIVATE, vt);
}
struct weston_launcher * struct weston_launcher *
weston_launcher_connect(struct weston_compositor *compositor) weston_launcher_connect(struct weston_compositor *compositor)
{ {
struct weston_launcher *launcher; struct weston_launcher *launcher;
struct wl_event_loop *loop; struct wl_event_loop *loop;
int fd;
fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
if (fd == -1)
return NULL;
launcher = malloc(sizeof *launcher); launcher = malloc(sizeof *launcher);
if (launcher == NULL) if (launcher == NULL)
return NULL; return NULL;
launcher->compositor = compositor; launcher->compositor = compositor;
launcher->fd = fd; launcher->drm_fd = -1;
launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
if (launcher->fd != -1) {
launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
loop = wl_display_get_event_loop(compositor->wl_display); loop = wl_display_get_event_loop(compositor->wl_display);
launcher->source = wl_event_loop_add_fd(loop, launcher->fd, launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
WL_EVENT_READABLE, WL_EVENT_READABLE,
@ -173,6 +298,12 @@ weston_launcher_connect(struct weston_compositor *compositor)
free(launcher); free(launcher);
return NULL; return NULL;
} }
} else if (geteuid() == 0) {
setup_tty(launcher);
} else {
free(launcher);
return NULL;
}
return launcher; return launcher;
} }
@ -180,6 +311,13 @@ weston_launcher_connect(struct weston_compositor *compositor)
void void
weston_launcher_destroy(struct weston_launcher *launcher) weston_launcher_destroy(struct weston_launcher *launcher)
{ {
if (launcher->fd != -1) {
close(launcher->fd); close(launcher->fd);
wl_event_source_remove(launcher->source);
} else {
weston_launcher_restore(launcher);
wl_event_source_remove(launcher->vt_source);
}
free(launcher); free(launcher);
} }

@ -39,4 +39,10 @@ int
weston_launcher_open(struct weston_launcher *launcher, weston_launcher_open(struct weston_launcher *launcher,
const char *path, int flags); const char *path, int flags);
int
weston_launcher_activate_vt(struct weston_launcher *launcher, int vt);
void
weston_launcher_restore(struct weston_launcher *launcher);
#endif #endif

@ -1,294 +0,0 @@
/*
* Copyright © 2010 Intel Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. The copyright holders make
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <linux/kd.h>
#include <linux/vt.h>
#include <linux/major.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "compositor.h"
/* Introduced in 2.6.38 */
#ifndef K_OFF
#define K_OFF 0x04
#endif
struct tty {
struct weston_compositor *compositor;
int fd;
struct termios terminal_attributes;
struct wl_event_source *input_source;
struct wl_event_source *vt_source;
int vt, starting_vt;
int kb_mode;
};
static int vt_handler(int signal_number, void *data)
{
struct tty *tty = data;
struct weston_compositor *compositor = tty->compositor;
if (compositor->session_active) {
compositor->session_active = 0;
wl_signal_emit(&tty->compositor->session_signal,
tty->compositor);
ioctl(tty->fd, VT_RELDISP, 1);
} else {
ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
compositor->session_active = 1;
wl_signal_emit(&tty->compositor->session_signal,
tty->compositor);
}
return 1;
}
static int
on_tty_input(int fd, uint32_t mask, void *data)
{
struct tty *tty = data;
/* Ignore input to tty. We get keyboard events from evdev */
tcflush(tty->fd, TCIFLUSH);
return 1;
}
static int
try_open_vt(struct tty *tty)
{
int tty0, fd;
char filename[16];
tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
if (tty0 < 0) {
weston_log("could not open tty0: %m\n");
return -1;
}
if (ioctl(tty0, VT_OPENQRY, &tty->vt) < 0 || tty->vt == -1) {
weston_log("could not open tty0: %m\n");
close(tty0);
return -1;
}
close(tty0);
snprintf(filename, sizeof filename, "/dev/tty%d", tty->vt);
weston_log("compositor: using new vt %s\n", filename);
fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
if (fd < 0)
return fd;
return fd;
}
int
tty_activate_vt(struct tty *tty, int vt)
{
return ioctl(tty->fd, VT_ACTIVATE, vt);
}
struct tty *
tty_create(struct weston_compositor *compositor, int tty_nr)
{
struct termios raw_attributes;
struct vt_mode mode = { 0 };
int ret;
struct tty *tty;
struct wl_event_loop *loop;
struct stat buf;
char filename[16];
struct vt_stat vts;
tty = zalloc(sizeof *tty);
if (tty == NULL)
return NULL;
tty->compositor = compositor;
tty->fd = weston_environment_get_fd("WESTON_TTY_FD");
if (tty->fd < 0)
tty->fd = STDIN_FILENO;
if (tty_nr > 0) {
snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
weston_log("compositor: using %s\n", filename);
tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
tty->vt = tty_nr;
} else if (fstat(tty->fd, &buf) == 0 &&
major(buf.st_rdev) == TTY_MAJOR &&
minor(buf.st_rdev) > 0) {
if (tty->fd == STDIN_FILENO)
tty->fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
tty->vt = minor(buf.st_rdev);
} else {
/* Fall back to try opening a new VT. This typically
* requires root. */
tty->fd = try_open_vt(tty);
}
if (tty->fd < 0) {
weston_log("failed to open tty: %m\n");
free(tty);
return NULL;
}
if (ioctl(tty->fd, VT_GETSTATE, &vts) == 0)
tty->starting_vt = vts.v_active;
else
tty->starting_vt = tty->vt;
if (tty->starting_vt != tty->vt) {
if (ioctl(tty->fd, VT_ACTIVATE, tty->vt) < 0 ||
ioctl(tty->fd, VT_WAITACTIVE, tty->vt) < 0) {
weston_log("failed to switch to new vt\n");
goto err;
}
}
if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
weston_log("could not get terminal attributes: %m\n");
goto err;
}
/* Ignore control characters and disable echo */
raw_attributes = tty->terminal_attributes;
cfmakeraw(&raw_attributes);
/* Fix up line endings to be normal (cfmakeraw hoses them) */
raw_attributes.c_oflag |= OPOST | OCRNL;
if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
weston_log("could not put terminal into raw mode: %m\n");
loop = wl_display_get_event_loop(compositor->wl_display);
ioctl(tty->fd, KDGKBMODE, &tty->kb_mode);
ret = ioctl(tty->fd, KDSKBMODE, K_OFF);
if (ret) {
ret = ioctl(tty->fd, KDSKBMODE, K_RAW);
if (ret) {
weston_log("failed to set keyboard mode on tty: %m\n");
goto err_attr;
}
tty->input_source = wl_event_loop_add_fd(loop, tty->fd,
WL_EVENT_READABLE,
on_tty_input, tty);
if (!tty->input_source)
goto err_kdkbmode;
}
ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
if (ret) {
weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
goto err_kdkbmode;
}
mode.mode = VT_PROCESS;
mode.relsig = SIGUSR1;
mode.acqsig = SIGUSR1;
if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
weston_log("failed to take control of vt handling\n");
goto err_kdmode;
}
tty->vt_source =
wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, tty);
if (!tty->vt_source)
goto err_vtmode;
return tty;
err_vtmode:
ioctl(tty->fd, VT_SETMODE, &mode);
err_kdmode:
ioctl(tty->fd, KDSETMODE, KD_TEXT);
err_kdkbmode:
if (tty->input_source)
wl_event_source_remove(tty->input_source);
ioctl(tty->fd, KDSKBMODE, tty->kb_mode);
err_attr:
tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes);
err:
close(tty->fd);
free(tty);
return NULL;
}
void tty_reset(struct tty *tty)
{
struct vt_mode mode = { 0 };
if (ioctl(tty->fd, KDSKBMODE, tty->kb_mode))
weston_log("failed to restore keyboard mode: %m\n");
if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
weston_log("failed to set KD_TEXT mode on tty: %m\n");
if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
weston_log("could not restore terminal to canonical mode\n");
mode.mode = VT_AUTO;
if (ioctl(tty->fd, VT_SETMODE, &mode) < 0)
weston_log("could not reset vt handling\n");
if (tty->compositor->session_active && tty->vt != tty->starting_vt) {
ioctl(tty->fd, VT_ACTIVATE, tty->starting_vt);
ioctl(tty->fd, VT_WAITACTIVE, tty->starting_vt);
}
}
void
tty_destroy(struct tty *tty)
{
if (tty->input_source)
wl_event_source_remove(tty->input_source);
if (tty->vt_source)
wl_event_source_remove(tty->vt_source);
tty_reset(tty);
close(tty->fd);
free(tty);
}

@ -28,7 +28,6 @@
#include <assert.h> #include <assert.h>
#include <poll.h> #include <poll.h>
#include <errno.h> #include <errno.h>
#include <termios.h>
#include <error.h> #include <error.h>
#include <getopt.h> #include <getopt.h>
@ -43,7 +42,6 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h>
#include <linux/vt.h> #include <linux/vt.h>
#include <linux/major.h> #include <linux/major.h>
#include <linux/kd.h> #include <linux/kd.h>
@ -62,6 +60,10 @@
#define DRM_MAJOR 226 #define DRM_MAJOR 226
#ifndef KDSKBMUTE
#define KDSKBMUTE 0x4B51
#endif
#define MAX_ARGV_SIZE 256 #define MAX_ARGV_SIZE 256
struct weston_launch { struct weston_launch {
@ -71,7 +73,6 @@ struct weston_launch {
int ttynr; int ttynr;
int sock[2]; int sock[2];
int drm_fd; int drm_fd;
struct termios terminal_attributes;
int kb_mode; int kb_mode;
struct passwd *pw; struct passwd *pw;
@ -391,16 +392,13 @@ quit(struct weston_launch *wl, int status)
pam_end(wl->ph, err); pam_end(wl->ph, err);
} }
if (ioctl(wl->tty, KDSKBMODE, wl->kb_mode)) if (ioctl(wl->tty, KDSKBMUTE, 0) &&
ioctl(wl->tty, KDSKBMODE, wl->kb_mode))
fprintf(stderr, "failed to restore keyboard mode: %m\n"); fprintf(stderr, "failed to restore keyboard mode: %m\n");
if (ioctl(wl->tty, KDSETMODE, KD_TEXT)) if (ioctl(wl->tty, KDSETMODE, KD_TEXT))
fprintf(stderr, "failed to set KD_TEXT mode on tty: %m\n"); fprintf(stderr, "failed to set KD_TEXT mode on tty: %m\n");
if (tcsetattr(wl->tty, TCSANOW, &wl->terminal_attributes) < 0)
fprintf(stderr,
"could not restore terminal to canonical mode\n");
mode.mode = VT_AUTO; mode.mode = VT_AUTO;
if (ioctl(wl->tty, VT_SETMODE, &mode) < 0) if (ioctl(wl->tty, VT_SETMODE, &mode) < 0)
fprintf(stderr, "could not reset vt handling\n"); fprintf(stderr, "could not reset vt handling\n");
@ -464,11 +462,9 @@ handle_signal(struct weston_launch *wl)
static int static int
setup_tty(struct weston_launch *wl, const char *tty) setup_tty(struct weston_launch *wl, const char *tty)
{ {
struct termios raw_attributes;
struct stat buf; struct stat buf;
struct vt_mode mode = { 0 }; struct vt_mode mode = { 0 };
char *t; char *t;
int ret;
if (!wl->new_user) { if (!wl->new_user) {
wl->tty = STDIN_FILENO; wl->tty = STDIN_FILENO;
@ -506,28 +502,14 @@ setup_tty(struct weston_launch *wl, const char *tty)
wl->ttynr = minor(buf.st_rdev); wl->ttynr = minor(buf.st_rdev);
} }
if (tcgetattr(wl->tty, &wl->terminal_attributes) < 0) if (ioctl(wl->tty, KDGKBMODE, &wl->kb_mode))
error(1, errno, "could not get terminal attributes: %m\n"); error(1, errno, "failed to get current keyboard mode: %m\n");
/* Ignore control characters and disable echo */ if (ioctl(wl->tty, KDSKBMUTE, 1) &&
raw_attributes = wl->terminal_attributes; ioctl(wl->tty, KDSKBMODE, K_OFF))
cfmakeraw(&raw_attributes); error(1, errno, "failed to set K_OFF keyboard mode: %m\n");
/* Fix up line endings to be normal (cfmakeraw hoses them) */ if (ioctl(wl->tty, KDSETMODE, KD_GRAPHICS))
raw_attributes.c_oflag |= OPOST | OCRNL;
if (tcsetattr(wl->tty, TCSANOW, &raw_attributes) < 0)
error(1, errno, "could not put terminal into raw mode: %m\n");
ioctl(wl->tty, KDGKBMODE, &wl->kb_mode);
ret = ioctl(wl->tty, KDSKBMODE, K_OFF);
if (ret)
ret = ioctl(wl->tty, KDSKBMODE, K_RAW);
if (ret)
error(1, errno, "failed to set keyboard mode on tty: %m\n");
ret = ioctl(wl->tty, KDSETMODE, KD_GRAPHICS);
if (ret)
error(1, errno, "failed to set KD_GRAPHICS mode on tty: %m\n"); error(1, errno, "failed to set KD_GRAPHICS mode on tty: %m\n");
mode.mode = VT_PROCESS; mode.mode = VT_PROCESS;
@ -597,9 +579,7 @@ launch_compositor(struct weston_launch *wl, int argc, char *argv[])
drop_privileges(wl); drop_privileges(wl);
if (wl->tty != STDIN_FILENO)
setenv_fd("WESTON_TTY_FD", wl->tty); setenv_fd("WESTON_TTY_FD", wl->tty);
setenv_fd("WESTON_LAUNCHER_SOCK", wl->sock[1]); setenv_fd("WESTON_LAUNCHER_SOCK", wl->sock[1]);
unsetenv("DISPLAY"); unsetenv("DISPLAY");

Loading…
Cancel
Save