launcher-util.c 4.05 KB
Newer Older
Benjamin Franzke's avatar
Benjamin Franzke committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * 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"

40 41
union cmsg_data { unsigned char b[4]; int fd; };

Benjamin Franzke's avatar
Benjamin Franzke committed
42 43 44 45 46
int
weston_launcher_open(struct weston_compositor *compositor,
		     const char *path, int flags)
{
	int sock = compositor->launcher_sock;
47
	int n, ret = -1;
Benjamin Franzke's avatar
Benjamin Franzke committed
48 49 50
	struct msghdr msg;
	struct cmsghdr *cmsg;
	struct iovec iov;
51 52
	union cmsg_data *data;
	char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzke's avatar
Benjamin Franzke committed
53 54 55 56
	ssize_t len;
	struct weston_launcher_open *message;

	if (sock == -1)
57
		return open(path, flags | O_CLOEXEC);
Benjamin Franzke's avatar
Benjamin Franzke committed
58 59 60 61 62 63 64 65 66 67 68 69 70

	n = sizeof(*message) + strlen(path) + 1;
	message = malloc(n);
	if (!message)
		return -1;

	message->header.opcode = WESTON_LAUNCHER_OPEN;
	message->flags = flags;
	strcpy(message->path, path);

	do {
		len = send(sock, message, n, 0);
	} while (len < 0 && errno == EINTR);
71
	free(message);
Benjamin Franzke's avatar
Benjamin Franzke committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

	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, MSG_CMSG_CLOEXEC);
	} while (len < 0 && errno == EINTR);

	if (len != sizeof ret ||
	    ret < 0)
87
		return -1;
Benjamin Franzke's avatar
Benjamin Franzke committed
88 89 90 91 92 93

	cmsg = CMSG_FIRSTHDR(&msg);
	if (!cmsg ||
	    cmsg->cmsg_level != SOL_SOCKET ||
	    cmsg->cmsg_type != SCM_RIGHTS) {
		fprintf(stderr, "invalid control message\n");
94
		return -1;
Benjamin Franzke's avatar
Benjamin Franzke committed
95 96
	}

97 98
	data = (union cmsg_data *) CMSG_DATA(cmsg);
	if (data->fd == -1) {
Benjamin Franzke's avatar
Benjamin Franzke committed
99 100 101 102
		fprintf(stderr, "missing drm fd in socket request");
		return -1;
	}

103
	return data->fd;
Benjamin Franzke's avatar
Benjamin Franzke committed
104 105 106 107 108 109 110 111 112 113 114 115 116
}

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;
	struct weston_launcher_set_master message;
117
	union cmsg_data *data;
Benjamin Franzke's avatar
Benjamin Franzke committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

	if (compositor->launcher_sock == -1) {
		if (master)
			return drmSetMaster(drm_fd);
		else
			return drmDropMaster(drm_fd);
	}

	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));

136 137
	data = (union cmsg_data *) CMSG_DATA(cmsg);
	data->fd = drm_fd;
Benjamin Franzke's avatar
Benjamin Franzke committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	msg.msg_controllen = cmsg->cmsg_len;

	iov.iov_base = &message;
	iov.iov_len = sizeof message;

	message.header.opcode = WESTON_LAUNCHER_DRM_SET_MASTER;
	message.set_master = 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;
}