blob: acfcc3ee610dea3957bff1c9e30fee20d144600b [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
40int
41weston_launcher_open(struct weston_compositor *compositor,
42 const char *path, int flags)
43{
44 int sock = compositor->launcher_sock;
45 int fd, n, ret = -1;
46 struct msghdr msg;
47 struct cmsghdr *cmsg;
48 struct iovec iov;
49 char control[CMSG_SPACE(sizeof fd)];
50 ssize_t len;
51 struct weston_launcher_open *message;
52
53 if (sock == -1)
54 return open(path, flags);
55
56 n = sizeof(*message) + strlen(path) + 1;
57 message = malloc(n);
58 if (!message)
59 return -1;
60
61 message->header.opcode = WESTON_LAUNCHER_OPEN;
62 message->flags = flags;
63 strcpy(message->path, path);
64
65 do {
66 len = send(sock, message, n, 0);
67 } while (len < 0 && errno == EINTR);
68
69 memset(&msg, 0, sizeof msg);
70 iov.iov_base = &ret;
71 iov.iov_len = sizeof ret;
72 msg.msg_iov = &iov;
73 msg.msg_iovlen = 1;
74 msg.msg_control = control;
75 msg.msg_controllen = sizeof control;
76
77 do {
78 len = recvmsg(sock, &msg, MSG_CMSG_CLOEXEC);
79 } while (len < 0 && errno == EINTR);
80
81 if (len != sizeof ret ||
82 ret < 0)
83 goto out;
84
85 cmsg = CMSG_FIRSTHDR(&msg);
86 if (!cmsg ||
87 cmsg->cmsg_level != SOL_SOCKET ||
88 cmsg->cmsg_type != SCM_RIGHTS) {
89 fprintf(stderr, "invalid control message\n");
90 goto out;
91 }
92
93 fd = *(int *) CMSG_DATA(cmsg);
94 if (fd == -1) {
95 fprintf(stderr, "missing drm fd in socket request");
96 return -1;
97 }
98
99out:
100 free(message);
101 return ret < 0 ? ret : fd;
102}
103
104int
105weston_launcher_drm_set_master(struct weston_compositor *compositor,
106 int drm_fd, char master)
107{
108 struct msghdr msg;
109 struct cmsghdr *cmsg;
110 struct iovec iov;
111 char control[CMSG_SPACE(sizeof(drm_fd))];
112 int ret;
113 ssize_t len;
114 struct weston_launcher_set_master message;
115
116 if (compositor->launcher_sock == -1) {
117 if (master)
118 return drmSetMaster(drm_fd);
119 else
120 return drmDropMaster(drm_fd);
121 }
122
123 memset(&msg, 0, sizeof msg);
124 msg.msg_iov = &iov;
125 msg.msg_iovlen = 1;
126 msg.msg_control = control;
127 msg.msg_controllen = sizeof control;
128 cmsg = CMSG_FIRSTHDR(&msg);
129 cmsg->cmsg_level = SOL_SOCKET;
130 cmsg->cmsg_type = SCM_RIGHTS;
131 cmsg->cmsg_len = CMSG_LEN(sizeof(drm_fd));
132
133 *(int *) CMSG_DATA(cmsg) = drm_fd;
134 msg.msg_controllen = cmsg->cmsg_len;
135
136 iov.iov_base = &message;
137 iov.iov_len = sizeof message;
138
139 message.header.opcode = WESTON_LAUNCHER_DRM_SET_MASTER;
140 message.set_master = master;
141
142 do {
143 len = sendmsg(compositor->launcher_sock, &msg, 0);
144 } while (len < 0 && errno == EINTR);
145 if (len < 0)
146 return -1;
147
148 do {
149 len = recv(compositor->launcher_sock, &ret, sizeof ret, 0);
150 } while (len < 0 && errno == EINTR);
151 if (len < 0)
152 return -1;
153
154 return ret;
155}
156