<commit_before>/*
 * Copyright © 2012 <NAME>
 * Copyright © 2013 Intel Corporation
 *
 * 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 "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <errno.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/vt.h>
#include <linux/kd.h>
#include <linux/major.h>

#ifdef BUILD_DRM_COMPOSITOR
#include <xf86drm.h>
#endif

#include "compositor.h"
#include "launcher-util.h"
#include "weston-launch.h"

#define DRM_MAJOR 226

#ifndef KDSKBMUTE
#define KDSKBMUTE	0x4B51
#endif

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

struct weston_launcher {
	struct weston_compositor *compositor;
	int fd;
	struct wl_event_source *source;

	int kb_mode, tty, drm_fd;
	struct wl_event_source *vt_source;
};

#ifdef BUILD_DRM_COMPOSITOR
static int
drm_drop_master(int drm_fd)
{
	if (drm_fd != -1)
		return drmDropMaster(drm_fd);
	return -EBADF;
}
static int
drm_set_master(int drm_fd)
{
	if (drm_fd != -1)
		return drmSetMaster(drm_fd);
	return -EBADF;
}
#else
static int drm_drop_master(int drm_fd) {return 0;}
static int drm_set_master(int drm_fd) {return 0;}
#endif

int
weston_launcher_open(struct weston_launcher *launcher,
		     const char *path, int flags)
{
	int n, fd, ret = -1;
	struct msghdr msg;
	struct cmsghdr *cmsg;
	struct iovec iov;
	union cmsg_data *data;
	char control[CMSG_SPACE(sizeof data->fd)];
	ssize_t len;
	struct weston_launcher_open *message;
	struct stat s;

	if (launcher->fd == -1) {
		fd = open(path, flags | O_CLOEXEC);
		if (fd == -1)
			return -1;

		if (fstat(fd, &s) == -1) {
			close(fd);
			return -1;
		}

		if (major(s.st_rdev) == DRM_MAJOR) {
			launcher->drm_fd = fd;
			if (drm_set_master(fd) == -1) {
				weston_log("could not set master on drm fd\n");
				close(fd);
				return -1;
			}
		}

		return fd;
	}

	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(launcher->fd, message, n, 0);
	} while (len < 0 && errno == EINTR);
	free(message);
<commit_msg>launcher: Bail out if we can't be drm master<commit_after>/*
 * Copyright © 2012 <NAME>
 * Copyright © 2013 Intel Corporation
 *
 * 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 "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <errno.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/vt.h>
#include <linux/kd.h>
#include <linux/major.h>

#ifdef BUILD_DRM_COMPOSITOR
#include <xf86drm.h>
#endif

#include "compositor.h"
#include "launcher-util.h"
#include "weston-launch.h"

#define DRM_MAJOR 226

#ifndef KDSKBMUTE
#define KDSKBMUTE	0x4B51
#endif

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

struct weston_launcher {
	struct weston_compositor *compositor;
	int fd;
	struct wl_event_source *source;

	int kb_mode, tty, drm_fd;
	struct wl_event_source *vt_source;
};

#ifdef BUILD_DRM_COMPOSITOR
static int
drm_drop_master(int drm_fd)
{
	if (drm_fd != -1)
		return drmDropMaster(drm_fd);
	return -EBADF;
}
static int
drm_set_master(int drm_fd)
{
	if (drm_fd != -1)
		return drmSetMaster(drm_fd);
	return -EBADF;
}
#else
static int drm_drop_master(int drm_fd) {return 0;}
static int drm_set_master(int drm_fd) {return 0;}
#endif

int
weston_launcher_open(struct weston_launcher *launcher,
		     const char *path, int flags)
{
	int n, fd, ret = -1;
	struct msghdr msg;
	struct cmsghdr *cmsg;
	struct iovec iov;
	union cmsg_data *data;
	char control[CMSG_SPACE(sizeof data->fd)];
	ssize_t len;
	struct weston_launcher_open *message;
	struct stat s;

	if (launcher->fd == -1) {
		fd = open(path, flags | O_CLOEXEC);
		if (fd == -1)
			return -1;

		if (fstat(fd, &s) == -1) {
			close(fd);
			return -1;
		}

		if (major(s.st_rdev) == DRM_MAJOR) {
			drm_magic_t magic;
			launcher->drm_fd = fd;
			if (drmGetMagic(fd, &magic) != 0 ||
			    drmAuthMagic(fd, magic) != 0) {
				weston_log("drm fd not master\n");
				close(fd);
				return -1;
			}
		}

		return fd;
	}

	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(launcher->fd, message, n, 0);
	} while (len < 0 && errno == EINTR);
	free(message);

{'fraction_non_alphanumeric': 0.10827838827838827, 'fraction_numerical': 0.01010989010989011, 'mean_word_length': 4.190874524714829, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3175940', 'n_tokens_mistral': 2601, 'n_tokens_neox': 2361, 'n_words': 803}