blob: 6205e5f44d5fd22dd41a955e2bf7e93a136e10ec [file] [log] [blame]
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001/*
2 * Copyright © 2012 Benjamin Franzke
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -07003 * Copyright © 2013 Intel Corporation
Benjamin Franzkebfeda132012-01-30 14:04:04 +01004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Benjamin Franzkebfeda132012-01-30 14:04:04 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Benjamin Franzkebfeda132012-01-30 14:04:04 +010025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
28
Benjamin Franzkebfeda132012-01-30 14:04:04 +010029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include <errno.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070034#include <signal.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010035#include <sys/socket.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/uio.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070039#include <sys/ioctl.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010040#include <fcntl.h>
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070041#include <unistd.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070042#include <linux/vt.h>
43#include <linux/kd.h>
44#include <linux/major.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010045
Benjamin Franzkebfeda132012-01-30 14:04:04 +010046#include "compositor.h"
47#include "launcher-util.h"
David Herrmanncc5b2ed2013-10-22 00:28:09 +020048#include "logind-util.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010049#include "weston-launch.h"
50
Kristian Høgsberg3f495872013-09-18 23:00:17 -070051#define DRM_MAJOR 226
52
53#ifndef KDSKBMUTE
54#define KDSKBMUTE 0x4B51
55#endif
56
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -080057#ifdef HAVE_LIBDRM
Kristian Høgsberg9e140912012-04-10 01:26:18 -040058
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -080059#include <xf86drm.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070060
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -080061static inline int
62is_drm_master(int drm_fd)
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020063{
64 drm_magic_t magic;
Kristian Høgsberg891a16d2013-10-14 13:59:53 -070065
66 return drmGetMagic(drm_fd, &magic) == 0 &&
67 drmAuthMagic(drm_fd, magic) == 0;
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020068}
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -080069
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070070#else
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -080071
72static inline int
73drmDropMaster(int drm_fd)
74{
75 return 0;
76}
77
78static inline int
79drmSetMaster(int drm_fd)
80{
81 return 0;
82}
83
84static inline int
85is_drm_master(int drm_fd)
86{
87 return 0;
88}
89
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070090#endif
91
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -080092
93union cmsg_data { unsigned char b[4]; int fd; };
94
95struct weston_launcher {
96 struct weston_compositor *compositor;
97 struct weston_logind *logind;
98 struct wl_event_loop *loop;
99 int fd;
100 struct wl_event_source *source;
101
102 int kb_mode, tty, drm_fd;
103 struct wl_event_source *vt_source;
104};
105
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100106int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700107weston_launcher_open(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100108 const char *path, int flags)
109{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700110 int n, fd, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100111 struct msghdr msg;
112 struct cmsghdr *cmsg;
113 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400114 union cmsg_data *data;
115 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100116 ssize_t len;
117 struct weston_launcher_open *message;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700118 struct stat s;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100119
Derek Foreman2663c682015-05-01 11:46:36 -0500120 /* We really don't want to be leaking fds to child processes so
121 * we force this flag here. If someone comes up with a legitimate
122 * reason to not CLOEXEC they'll need to unset the flag manually.
123 */
124 flags |= O_CLOEXEC;
125
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200126 if (launcher->logind)
127 return weston_logind_open(launcher->logind, path, flags);
128
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700129 if (launcher->fd == -1) {
Derek Foreman2663c682015-05-01 11:46:36 -0500130 fd = open(path, flags);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700131 if (fd == -1)
132 return -1;
133
134 if (fstat(fd, &s) == -1) {
135 close(fd);
136 return -1;
137 }
138
Kristian Høgsberg57a10e42013-10-01 15:37:09 -0700139 if (major(s.st_rdev) == DRM_MAJOR) {
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700140 launcher->drm_fd = fd;
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -0800141 if (!is_drm_master(fd)) {
Kristian Høgsberg1468e602013-10-02 10:49:05 -0700142 weston_log("drm fd not master\n");
Kristian Høgsberg57a10e42013-10-01 15:37:09 -0700143 close(fd);
144 return -1;
145 }
146 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700147
148 return fd;
149 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100150
151 n = sizeof(*message) + strlen(path) + 1;
152 message = malloc(n);
153 if (!message)
154 return -1;
155
156 message->header.opcode = WESTON_LAUNCHER_OPEN;
157 message->flags = flags;
158 strcpy(message->path, path);
159
160 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700161 len = send(launcher->fd, message, n, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100162 } while (len < 0 && errno == EINTR);
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400163 free(message);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100164
165 memset(&msg, 0, sizeof msg);
166 iov.iov_base = &ret;
167 iov.iov_len = sizeof ret;
168 msg.msg_iov = &iov;
169 msg.msg_iovlen = 1;
170 msg.msg_control = control;
171 msg.msg_controllen = sizeof control;
172
173 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700174 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100175 } while (len < 0 && errno == EINTR);
176
177 if (len != sizeof ret ||
178 ret < 0)
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400179 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100180
181 cmsg = CMSG_FIRSTHDR(&msg);
182 if (!cmsg ||
183 cmsg->cmsg_level != SOL_SOCKET ||
184 cmsg->cmsg_type != SCM_RIGHTS) {
185 fprintf(stderr, "invalid control message\n");
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400186 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100187 }
188
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400189 data = (union cmsg_data *) CMSG_DATA(cmsg);
190 if (data->fd == -1) {
Martin Andersson566b4642013-02-13 00:11:12 +0100191 fprintf(stderr, "missing drm fd in socket request\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100192 return -1;
193 }
194
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400195 return data->fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100196}
197
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700198void
David Herrmanne461f852013-10-22 00:28:08 +0200199weston_launcher_close(struct weston_launcher *launcher, int fd)
200{
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200201 if (launcher->logind)
Derek Foreman8f5acc22015-04-30 14:37:03 -0500202 weston_logind_close(launcher->logind, fd);
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200203
David Herrmanne461f852013-10-22 00:28:08 +0200204 close(fd);
205}
206
207void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700208weston_launcher_restore(struct weston_launcher *launcher)
209{
210 struct vt_mode mode = { 0 };
211
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200212 if (launcher->logind)
213 return weston_logind_restore(launcher->logind);
214
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700215 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
216 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
217 weston_log("failed to restore kb mode: %m\n");
218
219 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
220 weston_log("failed to set KD_TEXT mode on tty: %m\n");
221
Kristian Høgsberga28ba552013-10-30 16:27:16 -0700222 /* We have to drop master before we switch the VT back in
223 * VT_AUTO, so we don't risk switching to a VT with another
224 * display server, that will then fail to set drm master. */
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -0800225 drmDropMaster(launcher->drm_fd);
Kristian Høgsberga28ba552013-10-30 16:27:16 -0700226
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700227 mode.mode = VT_AUTO;
228 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
229 weston_log("could not reset vt handling\n");
230}
231
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700232static int
233weston_launcher_data(int fd, uint32_t mask, void *data)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100234{
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700235 struct weston_launcher *launcher = data;
236 int len, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100237
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700238 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
239 weston_log("launcher socket closed, exiting\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700240 /* Normally the weston-launch will reset the tty, but
241 * in this case it died or something, so do it here so
242 * we don't end up with a stuck vt. */
243 weston_launcher_restore(launcher);
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700244 exit(-1);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100245 }
246
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100247 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700248 len = recv(launcher->fd, &ret, sizeof ret, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100249 } while (len < 0 && errno == EINTR);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100250
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700251 switch (ret) {
252 case WESTON_LAUNCHER_ACTIVATE:
253 launcher->compositor->session_active = 1;
254 wl_signal_emit(&launcher->compositor->session_signal,
255 launcher->compositor);
256 break;
257 case WESTON_LAUNCHER_DEACTIVATE:
258 launcher->compositor->session_active = 0;
259 wl_signal_emit(&launcher->compositor->session_signal,
260 launcher->compositor);
261 break;
262 default:
263 weston_log("unexpected event from weston-launch\n");
264 break;
265 }
266
267 return 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100268}
269
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700270static int
271vt_handler(int signal_number, void *data)
272{
273 struct weston_launcher *launcher = data;
274 struct weston_compositor *compositor = launcher->compositor;
275
276 if (compositor->session_active) {
277 compositor->session_active = 0;
278 wl_signal_emit(&compositor->session_signal, compositor);
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -0800279 drmDropMaster(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700280 ioctl(launcher->tty, VT_RELDISP, 1);
281 } else {
282 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -0800283 drmSetMaster(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700284 compositor->session_active = 1;
285 wl_signal_emit(&compositor->session_signal, compositor);
286 }
287
288 return 1;
289}
290
291static int
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700292setup_tty(struct weston_launcher *launcher, int tty)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700293{
294 struct wl_event_loop *loop;
295 struct vt_mode mode = { 0 };
296 struct stat buf;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700297 char tty_device[32] ="<stdin>";
298 int ret, kd_mode;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700299
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700300 if (tty == 0) {
Kristian Høgsberg325390e2013-10-09 11:03:39 -0700301 launcher->tty = dup(tty);
302 if (launcher->tty == -1) {
303 weston_log("couldn't dup stdin: %m\n");
304 return -1;
305 }
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700306 } else {
307 snprintf(tty_device, sizeof tty_device, "/dev/tty%d", tty);
308 launcher->tty = open(tty_device, O_RDWR | O_CLOEXEC);
309 if (launcher->tty == -1) {
310 weston_log("couldn't open tty %s: %m\n", tty_device);
311 return -1;
312 }
313 }
314
315 if (fstat(launcher->tty, &buf) == -1 ||
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700316 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700317 weston_log("%s not a vt\n", tty_device);
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700318 weston_log("if running weston from ssh, "
319 "use --tty to specify a tty\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700320 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700321 }
322
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700323 ret = ioctl(launcher->tty, KDGETMODE, &kd_mode);
324 if (ret) {
325 weston_log("failed to get VT mode: %m\n");
326 return -1;
327 }
328 if (kd_mode != KD_TEXT) {
329 weston_log("%s is already in graphics mode, "
330 "is another display server running?\n", tty_device);
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700331 goto err_close;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700332 }
333
Kristian Høgsberg74b0d722013-10-09 13:10:42 -0700334 ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev));
335 ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev));
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700336
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700337 if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
338 weston_log("failed to read keyboard mode: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700339 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700340 }
341
342 if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
343 ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
344 weston_log("failed to set K_OFF keyboard mode: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700345 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700346 }
347
348 ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
349 if (ret) {
350 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700351 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700352 }
353
David Herrmann541b6042014-12-30 14:33:21 +0100354 /*
355 * SIGRTMIN is used as global VT-acquire+release signal. Note that
356 * SIGRT* must be tested on runtime, as their exact values are not
357 * known at compile-time. POSIX requires 32 of them to be available.
358 */
359 if (SIGRTMIN > SIGRTMAX) {
360 weston_log("not enough RT signals available: %u-%u\n",
361 SIGRTMIN, SIGRTMAX);
362 ret = -EINVAL;
363 goto err_close;
364 }
365
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700366 mode.mode = VT_PROCESS;
David Herrmann541b6042014-12-30 14:33:21 +0100367 mode.relsig = SIGRTMIN;
368 mode.acqsig = SIGRTMIN;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700369 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
370 weston_log("failed to take control of vt handling\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700371 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700372 }
373
374 loop = wl_display_get_event_loop(launcher->compositor->wl_display);
375 launcher->vt_source =
David Herrmann541b6042014-12-30 14:33:21 +0100376 wl_event_loop_add_signal(loop, SIGRTMIN, vt_handler, launcher);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700377 if (!launcher->vt_source)
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700378 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700379
380 return 0;
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700381
382 err_close:
383 close(launcher->tty);
384 return -1;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700385}
386
387int
388weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
389{
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200390 if (launcher->logind)
391 return weston_logind_activate_vt(launcher->logind, vt);
392
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700393 return ioctl(launcher->tty, VT_ACTIVATE, vt);
394}
395
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700396struct weston_launcher *
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200397weston_launcher_connect(struct weston_compositor *compositor, int tty,
David Herrmann2ecb84a2014-12-30 14:33:22 +0100398 const char *seat_id, bool sync_drm)
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700399{
400 struct weston_launcher *launcher;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700401 struct wl_event_loop *loop;
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200402 int r;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700403
404 launcher = malloc(sizeof *launcher);
405 if (launcher == NULL)
406 return NULL;
407
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200408 launcher->logind = NULL;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700409 launcher->compositor = compositor;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700410 launcher->drm_fd = -1;
411 launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
412 if (launcher->fd != -1) {
413 launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
Kristian Høgsberg9a14b8f2014-04-30 10:40:39 -0700414 /* We don't get a chance to read out the original kb
415 * mode for the tty, so just hard code K_UNICODE here
416 * in case we have to clean if weston-launch dies. */
417 launcher->kb_mode = K_UNICODE;
418
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700419 loop = wl_display_get_event_loop(compositor->wl_display);
420 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
421 WL_EVENT_READABLE,
422 weston_launcher_data,
423 launcher);
424 if (launcher->source == NULL) {
425 free(launcher);
426 return NULL;
427 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700428 } else {
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200429 r = weston_logind_connect(&launcher->logind, compositor,
David Herrmann2ecb84a2014-12-30 14:33:22 +0100430 seat_id, tty, sync_drm);
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200431 if (r < 0) {
432 launcher->logind = NULL;
433 if (geteuid() == 0) {
434 if (setup_tty(launcher, tty) == -1) {
435 free(launcher);
436 return NULL;
437 }
438 } else {
439 free(launcher);
440 return NULL;
441 }
442 }
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700443 }
444
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700445 return launcher;
446}
447
448void
449weston_launcher_destroy(struct weston_launcher *launcher)
450{
Kristian Høgsbergd4c1cd72013-10-22 13:19:23 -0700451 if (launcher->logind) {
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200452 weston_logind_destroy(launcher->logind);
Kristian Høgsbergd4c1cd72013-10-22 13:19:23 -0700453 } else if (launcher->fd != -1) {
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700454 close(launcher->fd);
455 wl_event_source_remove(launcher->source);
456 } else {
457 weston_launcher_restore(launcher);
458 wl_event_source_remove(launcher->vt_source);
459 }
460
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200461 if (launcher->tty >= 0)
462 close(launcher->tty);
463
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700464 free(launcher);
465}