Commit bfeda130 authored by Benjamin Franzke's avatar Benjamin Franzke 😀

Introduce weston-launch

weston-launch starts weston and provides mechanism
for weston to set/drop drm master, open a tty,
and read input devices without being root.

Execution is allowed for local-active sessions
or users in the group weston-launch.
parent 1a361569
......@@ -151,6 +151,22 @@ if test x$enable_clients = xyes; then
[AC_MSG_WARN([Cairo-EGL not found - clients will use cairo image])])
fi
AC_ARG_ENABLE(weston-launch, [ --enable-weston-launch],, enable_weston_launch=yes)
AM_CONDITIONAL(BUILD_WESTON_LAUNCH, test x$enable_weston_launch == xyes)
if test x$enable_weston_launch == xyes; then
PKG_CHECK_MODULES(WESTON_LAUNCH, [libdrm])
PKG_CHECK_MODULES(SYSTEMD_LOGIN, [libsystemd-login],
[have_systemd_login=yes], [have_systemd_login=no])
AS_IF([test "x$have_systemd_login" = "xyes"],
[AC_DEFINE([HAVE_SYSTEMD_LOGIN], [1], [Have systemd-login])])
AC_CHECK_LIB([pam], [pam_open_session], [have_pam=yes], [have_pam=no])
if test x$have_pam == xno; then
AC_ERROR([weston-launch requires pam])
fi
WESTON_LAUNCH_LIBS="$WESTON_LAUNCH_LIBS -lpam"
fi
AM_CONDITIONAL(HAVE_POPPLER, test "x$have_poppler" = "xyes")
AM_CONDITIONAL(BUILD_FULL_GL_CLIENTS,
......
weston
weston-launch
screenshooter-protocol.c
screenshooter-server-protocol.h
tablet-shell-protocol.c
......
bin_PROGRAMS = weston
bin_PROGRAMS = weston \
$(weston_launch)
AM_CPPFLAGS = \
-DDATADIR='"$(datadir)"' \
......@@ -22,14 +23,33 @@ weston_SOURCES = \
util.c \
matrix.c \
matrix.h \
weston-launch.h \
$(xserver_launcher_sources)
if BUILD_WESTON_LAUNCH
weston_launch = weston-launch
weston_launch_SOURCES = weston-launch.c weston-launch.h
weston_launch_CFLAGS= $(GCC_CFLAGS)
weston_launch_CPPFLAGS = $(WESTON_LAUNCH_CFLAGS) $(SYSTEMD_LOGIN_CFLAGS) \
-DBINDIR='"$(bindir)"'
weston_launch_LDADD = $(WESTON_LAUNCH_LIBS) $(SYSTEMD_LOGIN_LIBS)
if ENABLE_SETUID_INSTALL
install-exec-hook:
chown root $(DESTDIR)$(bindir)/weston-launch
chmod u+s $(DESTDIR)$(bindir)/weston-launch
endif
else # BUILD_WESTON_LAUNCH
if ENABLE_SETUID_INSTALL
install-exec-hook:
chown root $(DESTDIR)$(bindir)/weston
chmod u+s $(DESTDIR)$(bindir)/weston
endif
endif # BUILD_WESTON_LAUNCH
if ENABLE_XSERVER_LAUNCHER
xserver_launcher_sources = \
xserver-launcher.c \
......@@ -67,6 +87,8 @@ drm_backend_la_SOURCES = \
tty.c \
evdev.c \
evdev.h \
launcher-util.c \
launcher-util.h \
libbacklight.c \
libbacklight.h
endif
......
......@@ -39,6 +39,7 @@
#include "compositor.h"
#include "evdev.h"
#include "launcher-util.h"
struct drm_compositor {
struct weston_compositor base;
......@@ -1449,7 +1450,8 @@ drm_destroy(struct weston_compositor *ec)
gbm_device_destroy(d->gbm);
destroy_sprites(d);
drmDropMaster(d->drm.fd);
if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
fprintf(stderr, "failed to drop master: %m\n");
tty_destroy(d->tty);
free(d);
......@@ -1489,7 +1491,7 @@ vt_func(struct weston_compositor *compositor, int event)
switch (event) {
case TTY_ENTER_VT:
compositor->focus = 1;
if (drmSetMaster(ec->drm.fd)) {
if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
fprintf(stderr, "failed to set master: %m\n");
wl_display_terminate(compositor->wl_display);
}
......@@ -1529,7 +1531,7 @@ vt_func(struct weston_compositor *compositor, int event)
wl_list_for_each(input, &compositor->input_device_list, link)
evdev_remove_devices(input);
if (drmDropMaster(ec->drm.fd) < 0)
if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
fprintf(stderr, "failed to drop master: %m\n");
break;
......
......@@ -2331,6 +2331,7 @@ weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
const char *extensions;
ec->wl_display = display;
ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
if (!wl_display_add_global(display, &wl_compositor_interface,
ec, compositor_bind))
......
......@@ -231,6 +231,7 @@ struct weston_compositor {
int (*authenticate)(struct weston_compositor *c, uint32_t id);
struct screenshooter *screenshooter;
int launcher_sock;
};
#define MODIFIER_CTRL (1 << 8)
......@@ -443,6 +444,9 @@ weston_compositor_run_binding(struct weston_compositor *compositor,
struct weston_input_device *device,
uint32_t time,
uint32_t key, uint32_t button, uint32_t axis, int32_t state);
int
weston_environment_get_fd(const char *env);
struct wl_list *
weston_compositor_top(struct weston_compositor *compositor);
......
......@@ -30,6 +30,7 @@
#include "compositor.h"
#include "evdev.h"
#include "launcher-util.h"
struct evdev_input {
struct weston_input_device base;
......@@ -492,7 +493,7 @@ evdev_input_device_create(struct evdev_input *master,
/* Use non-blocking mode so that we can loop on read on
* evdev_input_device_data() until all events on the fd are
* read. mtdev_get() also expects this. */
device->fd = open(path, O_RDONLY | O_NONBLOCK);
device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK);
if (device->fd < 0)
goto err0;
......
/*
* Copyright © 2012 Benjamin Franzke
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <xf86drm.h>
#include "compositor.h"
#include "launcher-util.h"
#include "weston-launch.h"
int
weston_launcher_open(struct weston_compositor *compositor,
const char *path, int flags)
{
int sock = compositor->launcher_sock;
int fd, n, ret = -1;
struct msghdr msg;
struct cmsghdr *cmsg;
struct iovec iov;
char control[CMSG_SPACE(sizeof fd)];
ssize_t len;
struct weston_launcher_open *message;
if (sock == -1)
return open(path, flags);
n = sizeof(*message) + strlen(path) + 1;
message = malloc(n);
if (!message)
return -1;
message->header.opcode = WESTON_LAUNCHER_OPEN;
message->flags = flags;
strcpy(message->path, path);
do {
len = send(sock, message, n, 0);
} while (len < 0 && errno == EINTR);
memset(&msg, 0, sizeof msg);
iov.iov_base = &ret;
iov.iov_len = sizeof ret;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control;
msg.msg_controllen = sizeof control;
do {
len = recvmsg(sock, &msg, MSG_CMSG_CLOEXEC);
} while (len < 0 && errno == EINTR);
if (len != sizeof ret ||
ret < 0)
goto out;
cmsg = CMSG_FIRSTHDR(&msg);
if (!cmsg ||
cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_RIGHTS) {
fprintf(stderr, "invalid control message\n");
goto out;
}
fd = *(int *) CMSG_DATA(cmsg);
if (fd == -1) {
fprintf(stderr, "missing drm fd in socket request");
return -1;
}
out:
free(message);
return ret < 0 ? ret : fd;
}
int
weston_launcher_drm_set_master(struct weston_compositor *compositor,
int drm_fd, char master)
{
struct msghdr msg;
struct cmsghdr *cmsg;
struct iovec iov;
char control[CMSG_SPACE(sizeof(drm_fd))];
int ret;
ssize_t len;
struct weston_launcher_set_master message;
if (compositor->launcher_sock == -1) {
if (master)
return drmSetMaster(drm_fd);
else
return drmDropMaster(drm_fd);
}
memset(&msg, 0, sizeof msg);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control;
msg.msg_controllen = sizeof control;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(drm_fd));
*(int *) CMSG_DATA(cmsg) = drm_fd;
msg.msg_controllen = cmsg->cmsg_len;
iov.iov_base = &message;
iov.iov_len = sizeof message;
message.header.opcode = WESTON_LAUNCHER_DRM_SET_MASTER;
message.set_master = master;
do {
len = sendmsg(compositor->launcher_sock, &msg, 0);
} while (len < 0 && errno == EINTR);
if (len < 0)
return -1;
do {
len = recv(compositor->launcher_sock, &ret, sizeof ret, 0);
} while (len < 0 && errno == EINTR);
if (len < 0)
return -1;
return ret;
}
/*
* Copyright © 2012 Benjamin Franzke
*
* 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.
*/
#ifndef _WESTON_LAUNCHER_UTIL_H_
#define _WESTON_LAUNCHER_UTIL_H_
#include "compositor.h"
int
weston_launcher_open(struct weston_compositor *compositor,
const char *path, int flags);
int
weston_launcher_drm_set_master(struct weston_compositor *compositor,
int drm_fd, char master);
#endif
......@@ -81,7 +81,6 @@ try_open_vt(struct tty *tty)
{
int tty0, fd;
char filename[16];
struct vt_stat vts;
tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
if (tty0 < 0) {
......@@ -102,18 +101,6 @@ try_open_vt(struct tty *tty)
if (fd < 0)
return fd;
if (ioctl(fd, VT_GETSTATE, &vts) == 0)
tty->starting_vt = vts.v_active;
else
tty->starting_vt = tty->vt;
if (ioctl(fd, VT_ACTIVATE, tty->vt) < 0 ||
ioctl(fd, VT_WAITACTIVE, tty->vt) < 0) {
fprintf(stderr, "failed to swtich to new vt\n");
close(fd);
return -1;
}
return fd;
}
......@@ -128,6 +115,7 @@ tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
struct wl_event_loop *loop;
struct stat buf;
char filename[16];
struct vt_stat vts;
tty = malloc(sizeof *tty);
if (tty == NULL)
......@@ -136,14 +124,22 @@ tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
memset(tty, 0, sizeof *tty);
tty->compositor = compositor;
tty->vt_func = vt_func;
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);
fprintf(stderr, "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) {
tty->fd = fcntl(0, F_DUPFD_CLOEXEC, 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. */
......@@ -156,6 +152,19 @@ tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
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) {
fprintf(stderr, "failed to swtich to new vt\n");
return NULL;
}
}
if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
fprintf(stderr, "could not get terminal attributes: %m\n");
goto err;
......
......@@ -25,6 +25,9 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include "compositor.h"
WL_EXPORT void
......@@ -296,3 +299,26 @@ weston_compositor_run_binding(struct weston_compositor *compositor,
}
}
}
WL_EXPORT int
weston_environment_get_fd(const char *env)
{
char *e, *end;
int fd, flags;
e = getenv(env);
if (!e)
return -1;
fd = strtol(e, &end, 0);
if (*end != '\0')
return -1;
flags = fcntl(fd, F_GETFD);
if (flags == -1)
return -1;
fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
unsetenv(env);
return fd;
}
This diff is collapsed.
/*
* Copyright © 2012 Benjamin Franzke
*
* 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.
*/
#ifndef _WESTON_LAUNCH_H_
#define _WESTON_LAUNCH_H_
enum weston_launcher_opcode {
WESTON_LAUNCHER_OPEN,
WESTON_LAUNCHER_DRM_SET_MASTER
};
struct weston_launcher_message {
int opcode;
};
struct weston_launcher_open {
struct weston_launcher_message header;
int flags;
char path[0];
};
struct weston_launcher_set_master {
struct weston_launcher_message header;
int set_master;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment