summaryrefslogtreecommitdiff
path: root/src/launcher_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/launcher_util.c')
-rw-r--r--src/launcher_util.c164
1 files changed, 164 insertions, 0 deletions
diff --git a/src/launcher_util.c b/src/launcher_util.c
new file mode 100644
index 0000000..90b57d6
--- /dev/null
+++ b/src/launcher_util.c
@@ -0,0 +1,164 @@
+/*
+ * Copyright © 2012 Benjamin Franzke
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission. The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose. It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <fcntl.h>
+
+#include <xf86drm.h>
+
+#include "compositor.h"
+#include "launcher_util.h"
+#include "weston-launch.h"
+
+int
+weston_launcher_open(struct weston_compositor *compositor,
+ const char *path, char flags)
+{
+ int sock = compositor->launcher_sock;
+ int fd, n, ret = -1;
+ struct msghdr msg;
+ struct cmsghdr *cmsg;
+ struct iovec iov;
+ char control[CMSG_SPACE(sizeof fd)];
+ char *buf;
+ ssize_t len;
+
+ fd = open(path, flags);
+ if (fd > 0)
+ return fd;
+
+ if (sock == -1)
+ return -1;
+
+ n = 2 + strlen(path) + 1;
+ buf = malloc(n);
+ if (!buf)
+ return -1;
+
+ buf[0] = WESTON_LAUNCHER_OPEN;
+ buf[1] = flags;
+ strcpy(&buf[2], path);
+
+ do {
+ len = send(sock, buf, n, 0);
+ } while (len < 0 && errno == EINTR);
+
+ memset(&msg, 0, sizeof msg);
+ iov.iov_base = &ret;
+ iov.iov_len = sizeof ret;
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof control;
+
+ do {
+ len = recvmsg(sock, &msg, 0);
+ } while (len < 0 && errno == EINTR);
+
+ if (len != sizeof ret ||
+ ret < 0)
+ goto out;
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ if (!cmsg ||
+ cmsg->cmsg_level != SOL_SOCKET ||
+ cmsg->cmsg_type != SCM_RIGHTS) {
+ fprintf(stderr, "invalid control message\n");
+ goto out;
+ }
+
+ fd = *(int *) CMSG_DATA(cmsg);
+ if (fd == -1) {
+ fprintf(stderr, "missing drm fd in socket request");
+ return -1;
+ }
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
+
+out:
+ free(buf);
+ return ret < 0 ? ret : fd;
+}
+
+int
+weston_launcher_drm_set_master(struct weston_compositor *compositor,
+ int drm_fd, char master)
+{
+ struct msghdr msg;
+ struct cmsghdr *cmsg;
+ struct iovec iov;
+ char control[CMSG_SPACE(sizeof(drm_fd))];
+ int ret;
+ ssize_t len;
+ char buf[2];
+
+ if (master)
+ ret = drmSetMaster(drm_fd);
+ else
+ ret = drmDropMaster(drm_fd);
+ if (ret == 0)
+ return 0;
+
+ if (compositor->launcher_sock == -1)
+ return -1;
+
+ memset(&msg, 0, sizeof msg);
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof control;
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(drm_fd));
+
+ *(int *) CMSG_DATA(cmsg) = drm_fd;
+ msg.msg_controllen = cmsg->cmsg_len;
+
+ iov.iov_base = buf;
+ iov.iov_len = sizeof buf;
+
+ buf[0] = WESTON_LAUNCHER_DRM_SET_MASTER;
+ buf[1] = master;
+
+ do {
+ len = sendmsg(compositor->launcher_sock, &msg, 0);
+ } while (len < 0 && errno == EINTR);
+ if (len < 0)
+ return -1;
+
+ do {
+ len = recv(compositor->launcher_sock, &ret, sizeof ret, 0);
+ } while (len < 0 && errno == EINTR);
+ if (len < 0)
+ return -1;
+
+ return ret;
+}
+