blob: 519cd9dffa2517b383c2cdf93b3af51d23e823a4 [file] [log] [blame]
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001/*
2 * Copyright © 2012 Benjamin Franzke
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <errno.h>
28#include <sys/socket.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/uio.h>
32#include <fcntl.h>
33
34#include <xf86drm.h>
35
36#include "compositor.h"
37#include "launcher-util.h"
38#include "weston-launch.h"
39
Kristian Høgsberg9e140912012-04-10 01:26:18 -040040union cmsg_data { unsigned char b[4]; int fd; };
41
Benjamin Franzkebfeda132012-01-30 14:04:04 +010042int
43weston_launcher_open(struct weston_compositor *compositor,
44 const char *path, int flags)
45{
46 int sock = compositor->launcher_sock;
Kristian Høgsberg9e140912012-04-10 01:26:18 -040047 int n, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010048 struct msghdr msg;
49 struct cmsghdr *cmsg;
50 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -040051 union cmsg_data *data;
52 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +010053 ssize_t len;
54 struct weston_launcher_open *message;
55
56 if (sock == -1)
57 return open(path, flags);
58
59 n = sizeof(*message) + strlen(path) + 1;
60 message = malloc(n);
61 if (!message)
62 return -1;
63
64 message->header.opcode = WESTON_LAUNCHER_OPEN;
65 message->flags = flags;
66 strcpy(message->path, path);
67
68 do {
69 len = send(sock, message, n, 0);
70 } while (len < 0 && errno == EINTR);
71
72 memset(&msg, 0, sizeof msg);
73 iov.iov_base = &ret;
74 iov.iov_len = sizeof ret;
75 msg.msg_iov = &iov;
76 msg.msg_iovlen = 1;
77 msg.msg_control = control;
78 msg.msg_controllen = sizeof control;
79
80 do {
81 len = recvmsg(sock, &msg, MSG_CMSG_CLOEXEC);
82 } while (len < 0 && errno == EINTR);
83
84 if (len != sizeof ret ||
85 ret < 0)
86 goto out;
87
88 cmsg = CMSG_FIRSTHDR(&msg);
89 if (!cmsg ||
90 cmsg->cmsg_level != SOL_SOCKET ||
91 cmsg->cmsg_type != SCM_RIGHTS) {
92 fprintf(stderr, "invalid control message\n");
93 goto out;
94 }
95
Kristian Høgsberg9e140912012-04-10 01:26:18 -040096 data = (union cmsg_data *) CMSG_DATA(cmsg);
97 if (data->fd == -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +010098 fprintf(stderr, "missing drm fd in socket request");
99 return -1;
100 }
101
102out:
103 free(message);
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400104 return ret < 0 ? ret : data->fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100105}
106
107int
108weston_launcher_drm_set_master(struct weston_compositor *compositor,
109 int drm_fd, char master)
110{
111 struct msghdr msg;
112 struct cmsghdr *cmsg;
113 struct iovec iov;
114 char control[CMSG_SPACE(sizeof(drm_fd))];
115 int ret;
116 ssize_t len;
117 struct weston_launcher_set_master message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400118 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100119
120 if (compositor->launcher_sock == -1) {
121 if (master)
122 return drmSetMaster(drm_fd);
123 else
124 return drmDropMaster(drm_fd);
125 }
126
127 memset(&msg, 0, sizeof msg);
128 msg.msg_iov = &iov;
129 msg.msg_iovlen = 1;
130 msg.msg_control = control;
131 msg.msg_controllen = sizeof control;
132 cmsg = CMSG_FIRSTHDR(&msg);
133 cmsg->cmsg_level = SOL_SOCKET;
134 cmsg->cmsg_type = SCM_RIGHTS;
135 cmsg->cmsg_len = CMSG_LEN(sizeof(drm_fd));
136
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400137 data = (union cmsg_data *) CMSG_DATA(cmsg);
138 data->fd = drm_fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100139 msg.msg_controllen = cmsg->cmsg_len;
140
141 iov.iov_base = &message;
142 iov.iov_len = sizeof message;
143
144 message.header.opcode = WESTON_LAUNCHER_DRM_SET_MASTER;
145 message.set_master = master;
146
147 do {
148 len = sendmsg(compositor->launcher_sock, &msg, 0);
149 } while (len < 0 && errno == EINTR);
150 if (len < 0)
151 return -1;
152
153 do {
154 len = recv(compositor->launcher_sock, &ret, sizeof ret, 0);
155 } while (len < 0 && errno == EINTR);
156 if (len < 0)
157 return -1;
158
159 return ret;
160}
161