blob: 8c3b496f907872e7fb0616c88e9e68228dc6ffdd [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 *
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 Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Benjamin Franzkebfeda132012-01-30 14:04:04 +010026#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øgsberg05ad1e42013-09-17 14:41:03 -070036#include <unistd.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010037
38#include <xf86drm.h>
39
40#include "compositor.h"
41#include "launcher-util.h"
42#include "weston-launch.h"
43
Kristian Høgsberg9e140912012-04-10 01:26:18 -040044union cmsg_data { unsigned char b[4]; int fd; };
45
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070046struct weston_launcher {
47 struct weston_compositor *compositor;
48 int fd;
49};
50
Benjamin Franzkebfeda132012-01-30 14:04:04 +010051int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070052weston_launcher_open(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +010053 const char *path, int flags)
54{
Kristian Høgsberg9e140912012-04-10 01:26:18 -040055 int n, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010056 struct msghdr msg;
57 struct cmsghdr *cmsg;
58 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -040059 union cmsg_data *data;
60 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +010061 ssize_t len;
62 struct weston_launcher_open *message;
63
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070064 if (launcher == NULL)
Pekka Paalanen27979b02012-07-31 13:21:06 +030065 return open(path, flags | O_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +010066
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øgsberg05ad1e42013-09-17 14:41:03 -070077 len = send(launcher->fd, message, n, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +010078 } while (len < 0 && errno == EINTR);
Kristian Høgsbergba25bd72012-04-10 01:31:09 -040079 free(message);
Benjamin Franzkebfeda132012-01-30 14:04:04 +010080
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øgsberg05ad1e42013-09-17 14:41:03 -070090 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +010091 } while (len < 0 && errno == EINTR);
92
93 if (len != sizeof ret ||
94 ret < 0)
Kristian Høgsbergba25bd72012-04-10 01:31:09 -040095 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010096
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øgsbergba25bd72012-04-10 01:31:09 -0400102 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100103 }
104
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400105 data = (union cmsg_data *) CMSG_DATA(cmsg);
106 if (data->fd == -1) {
Martin Andersson566b4642013-02-13 00:11:12 +0100107 fprintf(stderr, "missing drm fd in socket request\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100108 return -1;
109 }
110
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400111 return data->fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100112}
113
114int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700115weston_launcher_drm_set_master(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100116 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øgsberg9e140912012-04-10 01:26:18 -0400125 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100126
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700127 if (launcher == NULL) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100128 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øgsberg9e140912012-04-10 01:26:18 -0400144 data = (union cmsg_data *) CMSG_DATA(cmsg);
145 data->fd = drm_fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100146 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øgsberg05ad1e42013-09-17 14:41:03 -0700155 len = sendmsg(launcher->fd, &msg, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100156 } while (len < 0 && errno == EINTR);
157 if (len < 0)
158 return -1;
159
160 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700161 len = recv(launcher->fd, &ret, sizeof ret, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100162 } while (len < 0 && errno == EINTR);
163 if (len < 0)
164 return -1;
165
166 return ret;
167}
168
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700169struct weston_launcher *
170weston_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
189void
190weston_launcher_destroy(struct weston_launcher *launcher)
191{
192 close(launcher->fd);
193 free(launcher);
194}