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; |
| 49 | }; |
| 50 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 51 | int |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 52 | weston_launcher_open(struct weston_launcher *launcher, |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 53 | const char *path, int flags) |
| 54 | { |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 55 | int n, ret = -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 56 | struct msghdr msg; |
| 57 | struct cmsghdr *cmsg; |
| 58 | struct iovec iov; |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 59 | union cmsg_data *data; |
| 60 | char control[CMSG_SPACE(sizeof data->fd)]; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 61 | ssize_t len; |
| 62 | struct weston_launcher_open *message; |
| 63 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 64 | if (launcher == NULL) |
Pekka Paalanen | 27979b0 | 2012-07-31 13:21:06 +0300 | [diff] [blame] | 65 | return open(path, flags | O_CLOEXEC); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 66 | |
| 67 | n = sizeof(*message) + strlen(path) + 1; |
| 68 | message = malloc(n); |
| 69 | if (!message) |
| 70 | return -1; |
| 71 | |
| 72 | message->header.opcode = WESTON_LAUNCHER_OPEN; |
| 73 | message->flags = flags; |
| 74 | strcpy(message->path, path); |
| 75 | |
| 76 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 77 | len = send(launcher->fd, message, n, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 78 | } while (len < 0 && errno == EINTR); |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 79 | free(message); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 80 | |
| 81 | memset(&msg, 0, sizeof msg); |
| 82 | iov.iov_base = &ret; |
| 83 | iov.iov_len = sizeof ret; |
| 84 | msg.msg_iov = &iov; |
| 85 | msg.msg_iovlen = 1; |
| 86 | msg.msg_control = control; |
| 87 | msg.msg_controllen = sizeof control; |
| 88 | |
| 89 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 90 | len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 91 | } while (len < 0 && errno == EINTR); |
| 92 | |
| 93 | if (len != sizeof ret || |
| 94 | ret < 0) |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 95 | return -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 96 | |
| 97 | cmsg = CMSG_FIRSTHDR(&msg); |
| 98 | if (!cmsg || |
| 99 | cmsg->cmsg_level != SOL_SOCKET || |
| 100 | cmsg->cmsg_type != SCM_RIGHTS) { |
| 101 | fprintf(stderr, "invalid control message\n"); |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 102 | return -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 105 | data = (union cmsg_data *) CMSG_DATA(cmsg); |
| 106 | if (data->fd == -1) { |
Martin Andersson | 566b464 | 2013-02-13 00:11:12 +0100 | [diff] [blame] | 107 | fprintf(stderr, "missing drm fd in socket request\n"); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 108 | return -1; |
| 109 | } |
| 110 | |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 111 | return data->fd; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 115 | weston_launcher_drm_set_master(struct weston_launcher *launcher, |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 116 | int drm_fd, char master) |
| 117 | { |
| 118 | struct msghdr msg; |
| 119 | struct cmsghdr *cmsg; |
| 120 | struct iovec iov; |
| 121 | char control[CMSG_SPACE(sizeof(drm_fd))]; |
| 122 | int ret; |
| 123 | ssize_t len; |
| 124 | struct weston_launcher_set_master message; |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 125 | union cmsg_data *data; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 126 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 127 | if (launcher == NULL) { |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 128 | if (master) |
| 129 | return drmSetMaster(drm_fd); |
| 130 | else |
| 131 | return drmDropMaster(drm_fd); |
| 132 | } |
| 133 | |
| 134 | memset(&msg, 0, sizeof msg); |
| 135 | msg.msg_iov = &iov; |
| 136 | msg.msg_iovlen = 1; |
| 137 | msg.msg_control = control; |
| 138 | msg.msg_controllen = sizeof control; |
| 139 | cmsg = CMSG_FIRSTHDR(&msg); |
| 140 | cmsg->cmsg_level = SOL_SOCKET; |
| 141 | cmsg->cmsg_type = SCM_RIGHTS; |
| 142 | cmsg->cmsg_len = CMSG_LEN(sizeof(drm_fd)); |
| 143 | |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 144 | data = (union cmsg_data *) CMSG_DATA(cmsg); |
| 145 | data->fd = drm_fd; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 146 | msg.msg_controllen = cmsg->cmsg_len; |
| 147 | |
| 148 | iov.iov_base = &message; |
| 149 | iov.iov_len = sizeof message; |
| 150 | |
| 151 | message.header.opcode = WESTON_LAUNCHER_DRM_SET_MASTER; |
| 152 | message.set_master = master; |
| 153 | |
| 154 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 155 | len = sendmsg(launcher->fd, &msg, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 156 | } while (len < 0 && errno == EINTR); |
| 157 | if (len < 0) |
| 158 | return -1; |
| 159 | |
| 160 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 161 | len = recv(launcher->fd, &ret, sizeof ret, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 162 | } while (len < 0 && errno == EINTR); |
| 163 | if (len < 0) |
| 164 | return -1; |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame^] | 169 | struct weston_launcher * |
| 170 | weston_launcher_connect(struct weston_compositor *compositor) |
| 171 | { |
| 172 | struct weston_launcher *launcher; |
| 173 | int fd; |
| 174 | |
| 175 | fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK"); |
| 176 | if (fd == -1) |
| 177 | return NULL; |
| 178 | |
| 179 | launcher = malloc(sizeof *launcher); |
| 180 | if (launcher == NULL) |
| 181 | return NULL; |
| 182 | |
| 183 | launcher->compositor = compositor; |
| 184 | launcher->fd = fd; |
| 185 | |
| 186 | return launcher; |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | weston_launcher_destroy(struct weston_launcher *launcher) |
| 191 | { |
| 192 | close(launcher->fd); |
| 193 | free(launcher); |
| 194 | } |