Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Benjamin Franzke |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 3 | * Copyright © 2013 Intel Corporation |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 24 | #include "config.h" |
| 25 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/uio.h> |
| 35 | #include <fcntl.h> |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 36 | #include <unistd.h> |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 37 | |
| 38 | #include <xf86drm.h> |
| 39 | |
| 40 | #include "compositor.h" |
| 41 | #include "launcher-util.h" |
| 42 | #include "weston-launch.h" |
| 43 | |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 44 | union cmsg_data { unsigned char b[4]; int fd; }; |
| 45 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 46 | struct weston_launcher { |
| 47 | struct weston_compositor *compositor; |
| 48 | int fd; |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame^] | 49 | struct wl_event_source *source; |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 52 | int |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 53 | weston_launcher_open(struct weston_launcher *launcher, |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 54 | const char *path, int flags) |
| 55 | { |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 56 | int n, ret = -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 57 | struct msghdr msg; |
| 58 | struct cmsghdr *cmsg; |
| 59 | struct iovec iov; |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 60 | union cmsg_data *data; |
| 61 | char control[CMSG_SPACE(sizeof data->fd)]; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 62 | ssize_t len; |
| 63 | struct weston_launcher_open *message; |
| 64 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 65 | if (launcher == NULL) |
Pekka Paalanen | 27979b0 | 2012-07-31 13:21:06 +0300 | [diff] [blame] | 66 | return open(path, flags | O_CLOEXEC); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 67 | |
| 68 | n = sizeof(*message) + strlen(path) + 1; |
| 69 | message = malloc(n); |
| 70 | if (!message) |
| 71 | return -1; |
| 72 | |
| 73 | message->header.opcode = WESTON_LAUNCHER_OPEN; |
| 74 | message->flags = flags; |
| 75 | strcpy(message->path, path); |
| 76 | |
| 77 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 78 | len = send(launcher->fd, message, n, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 79 | } while (len < 0 && errno == EINTR); |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 80 | free(message); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 81 | |
| 82 | memset(&msg, 0, sizeof msg); |
| 83 | iov.iov_base = &ret; |
| 84 | iov.iov_len = sizeof ret; |
| 85 | msg.msg_iov = &iov; |
| 86 | msg.msg_iovlen = 1; |
| 87 | msg.msg_control = control; |
| 88 | msg.msg_controllen = sizeof control; |
| 89 | |
| 90 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 91 | len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 92 | } while (len < 0 && errno == EINTR); |
| 93 | |
| 94 | if (len != sizeof ret || |
| 95 | ret < 0) |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 96 | return -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 97 | |
| 98 | cmsg = CMSG_FIRSTHDR(&msg); |
| 99 | if (!cmsg || |
| 100 | cmsg->cmsg_level != SOL_SOCKET || |
| 101 | cmsg->cmsg_type != SCM_RIGHTS) { |
| 102 | fprintf(stderr, "invalid control message\n"); |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 103 | return -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 106 | data = (union cmsg_data *) CMSG_DATA(cmsg); |
| 107 | if (data->fd == -1) { |
Martin Andersson | 566b464 | 2013-02-13 00:11:12 +0100 | [diff] [blame] | 108 | fprintf(stderr, "missing drm fd in socket request\n"); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 112 | return data->fd; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame^] | 115 | static int |
| 116 | weston_launcher_data(int fd, uint32_t mask, void *data) |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 117 | { |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame^] | 118 | struct weston_launcher *launcher = data; |
| 119 | int len, ret; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 120 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame^] | 121 | if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) { |
| 122 | weston_log("launcher socket closed, exiting\n"); |
| 123 | exit(-1); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 126 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 127 | len = recv(launcher->fd, &ret, sizeof ret, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 128 | } while (len < 0 && errno == EINTR); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 129 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame^] | 130 | switch (ret) { |
| 131 | case WESTON_LAUNCHER_ACTIVATE: |
| 132 | launcher->compositor->session_active = 1; |
| 133 | wl_signal_emit(&launcher->compositor->session_signal, |
| 134 | launcher->compositor); |
| 135 | break; |
| 136 | case WESTON_LAUNCHER_DEACTIVATE: |
| 137 | launcher->compositor->session_active = 0; |
| 138 | wl_signal_emit(&launcher->compositor->session_signal, |
| 139 | launcher->compositor); |
| 140 | break; |
| 141 | default: |
| 142 | weston_log("unexpected event from weston-launch\n"); |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | return 1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 149 | struct weston_launcher * |
| 150 | weston_launcher_connect(struct weston_compositor *compositor) |
| 151 | { |
| 152 | struct weston_launcher *launcher; |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame^] | 153 | struct wl_event_loop *loop; |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 154 | int fd; |
| 155 | |
| 156 | fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK"); |
| 157 | if (fd == -1) |
| 158 | return NULL; |
| 159 | |
| 160 | launcher = malloc(sizeof *launcher); |
| 161 | if (launcher == NULL) |
| 162 | return NULL; |
| 163 | |
| 164 | launcher->compositor = compositor; |
| 165 | launcher->fd = fd; |
| 166 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame^] | 167 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 168 | launcher->source = wl_event_loop_add_fd(loop, launcher->fd, |
| 169 | WL_EVENT_READABLE, |
| 170 | weston_launcher_data, |
| 171 | launcher); |
| 172 | if (launcher->source == NULL) { |
| 173 | free(launcher); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 177 | return launcher; |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | weston_launcher_destroy(struct weston_launcher *launcher) |
| 182 | { |
| 183 | close(launcher->fd); |
| 184 | free(launcher); |
| 185 | } |