<commit_before>#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <libudev.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <wayland-server-core.h>
#include <wlr/backend/session.h>
#include <wlr/backend/session/interface.h>
#include <wlr/config.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include "backend/session/session.h"
#include "util/signal.h"

extern const struct session_impl session_libseat;
extern const struct session_impl session_logind;
extern const struct session_impl session_direct;
extern const struct session_impl session_noop;

static const struct session_impl *impls[] = {
#if WLR_HAS_LIBSEAT
	&session_libseat,
#endif
#if WLR_HAS_SYSTEMD || WLR_HAS_ELOGIND
	&session_logind,
#endif
	&session_direct,
	NULL,
};

static int udev_event(int fd, uint32_t mask, void *data) {
	struct wlr_session *session = data;

	struct udev_device *udev_dev = udev_monitor_receive_device(session->mon);
	if (!udev_dev) {
		return 1;
	}

	const char *action = udev_device_get_action(udev_dev);

	wlr_log(WLR_DEBUG, "udev event for %s (%s)",
		udev_device_get_sysname(udev_dev), action);

	if (!action || strcmp(action, "change") != 0) {
		goto out;
	}

	dev_t devnum = udev_device_get_devnum(udev_dev);
	struct wlr_device *dev;

	wl_list_for_each(dev, &session->devices, link) {
		if (dev->dev == devnum) {
			wlr_signal_emit_safe(&dev->signal, session);
			break;
		}
	}

out:
	udev_device_unref(udev_dev);
	return 1;
}

static void handle_display_destroy(struct wl_listener *listener, void *data) {
	struct wlr_session *session =
		wl_container_of(listener, session, display_destroy);
	wlr_session_destroy(session);
}

void session_init(struct wlr_session *session) {
	wl_signal_init(&session->session_signal);
	wl_signal_init(&session->events.destroy);
	wl_list_init(&session->devices);
}

struct wlr_session *wlr_session_create(struct wl_display *disp) {
	struct wlr_session *session = NULL;

	const char *env_wlr_session = getenv("WLR_SESSION");
	if (env_wlr_session) {
		if (strcmp(env_wlr_session, "libseat") == 0) {
#if WLR_HAS_LIBSEAT
			session = session_libseat.create(disp);
#else
			wlr_log(WLR_ERROR, "wlroots is not compiled with libseat support");
#endif
		} else if (strcmp(env_wlr_session, "logind") == 0 ||
				strcmp(env_wlr_session, "systemd") == 0) {
#if WLR_HAS_SYSTEMD || WLR_HAS_ELOGIND
			session = session_logind.create(disp);
#else
			wlr_log(WLR_ERROR, "wlroots is not compiled with logind support");
#endif
		} else if (strcmp(env_wlr_session, "direct") == 0) {
			session = session_direct.create(disp);
		} else if (strcmp(env_wlr_session, "noop") == 0) {
			session = session_noop.create(disp);
		} else {
			wlr_log(WLR_ERROR, "Unsupported WLR_SESSION: %s",
				env_wlr_session);
		}
	} else {
		const struct session_impl **iter;
		for (iter = impls; !session && *iter; ++iter) {
			session = (*iter)->create(disp);
		}
	}

	if (!session) {
		wlr_log(WLR_ERROR, "Failed to load session backend");
		return NULL;
	}

	session->udev = udev_new();
	if (!session->udev) {
		wlr_log_errno(WLR_ERROR, "Failed to create udev context");
		goto error_session;
	}

	session->mon = udev_monitor_new_from_netlink(session->udev, "udev");
	if (!session->mon) {
		wlr_log_errno(WLR_ERROR, "Failed to create udev monitor");
		goto error_udev;
	}

	udev_monitor_filter_add_match_subsystem_devtype(session->mon, "drm", NULL);
	udev_monitor_enable_receiving(session->mon);

	struct wl_event_loop *event_loop = wl_display_get_event_loop(disp);
	int fd = udev_monitor_get_fd(session->mon);

	session->udev_event = wl_event_loop_add_fd(event_loop, fd,
		WL_EVENT_READABLE, udev_event, session);
	if (!session->udev_event) {
		wlr_log_errno(WLR_ERROR, "Failed to create udev event source");
		goto error_mon;
	}

	session->display_destroy.notify = handle_display_destroy;
	wl_display_add_destroy_listener(disp, &session->display_destroy);

	return session;

error_mon:
	udev_monitor_unref(session->mon);
error_udev:
	udev_unref(session->udev);
error_session:
	session->impl->destroy(session);
	return NULL;
}

void wlr_session_destroy(struct wlr_session *session) {
	if (!session) {
		return;
	}

	wlr_signal_emit_safe(&session->events.destroy, session);
	wl_list_remove(&session->display_destroy.link);

	wl_event_source_remove(session->udev_event);
	udev_monitor_unref(session->mon);
	udev_unref(session->udev);

	session->impl->destroy(session);
}

int wlr_session_open_file(struct wlr_session *session, const char *path) {
	int fd = session->impl->open(session, path);
	if (fd < 0) {
		return fd;
	}

	struct wlr_device *dev = malloc(sizeof(*dev));
	if (!dev) {
		wlr_log_errno(WLR_ERROR, "Allocation failed");
		goto error;
	}

	struct stat st;
	if (fstat(fd, &st) < 0) {
		wlr_log_errno(WLR_ERROR, "Stat failed");
		goto error;
	}

	dev->fd = fd;
	dev->dev = st.st_rdev;
	wl_signal_init(&dev->signal);
	wl_list_insert(&session->devices, &dev->link);

	return fd;

error:
	free(dev);
	return fd;
}

static struct wlr_device *find_device(struct wlr_session *session, int fd) {
	struct wlr_device *dev;

	wl_list_for_each(dev, &session->devices, link) {
		if (dev->fd == fd) {
			return dev;
		}
	}

	wlr_log(WLR_ERROR, "Tried to use fd %d not opened by session", fd);
	assert(0);
	return NULL;
}

void wlr_session_close_file(struct wlr_session *session, int fd) {
	struct wlr_device *dev = find_device(session, fd);

	session->impl->close(session, fd);
	wl_list_remove(&dev->link);
	free(dev);
}

void wlr_session_signal_add(struct wlr_session *session, int fd,
		struct wl_listener *listener) {
	struct wlr_device *dev = find_device(session, fd);

	wl_signal_add(&dev->signal, listener);
}
<commit_msg>backend/session: don't return FD on failure in open_file<commit_after>#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <libudev.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wayland-server-core.h>
#include <wlr/backend/session.h>
#include <wlr/backend/session/interface.h>
#include <wlr/config.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include "backend/session/session.h"
#include "util/signal.h"

extern const struct session_impl session_libseat;
extern const struct session_impl session_logind;
extern const struct session_impl session_direct;
extern const struct session_impl session_noop;

static const struct session_impl *impls[] = {
#if WLR_HAS_LIBSEAT
	&session_libseat,
#endif
#if WLR_HAS_SYSTEMD || WLR_HAS_ELOGIND
	&session_logind,
#endif
	&session_direct,
	NULL,
};

static int udev_event(int fd, uint32_t mask, void *data) {
	struct wlr_session *session = data;

	struct udev_device *udev_dev = udev_monitor_receive_device(session->mon);
	if (!udev_dev) {
		return 1;
	}

	const char *action = udev_device_get_action(udev_dev);

	wlr_log(WLR_DEBUG, "udev event for %s (%s)",
		udev_device_get_sysname(udev_dev), action);

	if (!action || strcmp(action, "change") != 0) {
		goto out;
	}

	dev_t devnum = udev_device_get_devnum(udev_dev);
	struct wlr_device *dev;

	wl_list_for_each(dev, &session->devices, link) {
		if (dev->dev == devnum) {
			wlr_signal_emit_safe(&dev->signal, session);
			break;
		}
	}

out:
	udev_device_unref(udev_dev);
	return 1;
}

static void handle_display_destroy(struct wl_listener *listener, void *data) {
	struct wlr_session *session =
		wl_container_of(listener, session, display_destroy);
	wlr_session_destroy(session);
}

void session_init(struct wlr_session *session) {
	wl_signal_init(&session->session_signal);
	wl_signal_init(&session->events.destroy);
	wl_list_init(&session->devices);
}

struct wlr_session *wlr_session_create(struct wl_display *disp) {
	struct wlr_session *session = NULL;

	const char *env_wlr_session = getenv("WLR_SESSION");
	if (env_wlr_session) {
		if (strcmp(env_wlr_session, "libseat") == 0) {
#if WLR_HAS_LIBSEAT
			session = session_libseat.create(disp);
#else
			wlr_log(WLR_ERROR, "wlroots is not compiled with libseat support");
#endif
		} else if (strcmp(env_wlr_session, "logind") == 0 ||
				strcmp(env_wlr_session, "systemd") == 0) {
#if WLR_HAS_SYSTEMD || WLR_HAS_ELOGIND
			session = session_logind.create(disp);
#else
			wlr_log(WLR_ERROR, "wlroots is not compiled with logind support");
#endif
		} else if (strcmp(env_wlr_session, "direct") == 0) {
			session = session_direct.create(disp);
		} else if (strcmp(env_wlr_session, "noop") == 0) {
			session = session_noop.create(disp);
		} else {
			wlr_log(WLR_ERROR, "Unsupported WLR_SESSION: %s",
				env_wlr_session);
		}
	} else {
		const struct session_impl **iter;
		for (iter = impls; !session && *iter; ++iter) {
			session = (*iter)->create(disp);
		}
	}

	if (!session) {
		wlr_log(WLR_ERROR, "Failed to load session backend");
		return NULL;
	}

	session->udev = udev_new();
	if (!session->udev) {
		wlr_log_errno(WLR_ERROR, "Failed to create udev context");
		goto error_session;
	}

	session->mon = udev_monitor_new_from_netlink(session->udev, "udev");
	if (!session->mon) {
		wlr_log_errno(WLR_ERROR, "Failed to create udev monitor");
		goto error_udev;
	}

	udev_monitor_filter_add_match_subsystem_devtype(session->mon, "drm", NULL);
	udev_monitor_enable_receiving(session->mon);

	struct wl_event_loop *event_loop = wl_display_get_event_loop(disp);
	int fd = udev_monitor_get_fd(session->mon);

	session->udev_event = wl_event_loop_add_fd(event_loop, fd,
		WL_EVENT_READABLE, udev_event, session);
	if (!session->udev_event) {
		wlr_log_errno(WLR_ERROR, "Failed to create udev event source");
		goto error_mon;
	}

	session->display_destroy.notify = handle_display_destroy;
	wl_display_add_destroy_listener(disp, &session->display_destroy);

	return session;

error_mon:
	udev_monitor_unref(session->mon);
error_udev:
	udev_unref(session->udev);
error_session:
	session->impl->destroy(session);
	return NULL;
}

void wlr_session_destroy(struct wlr_session *session) {
	if (!session) {
		return;
	}

	wlr_signal_emit_safe(&session->events.destroy, session);
	wl_list_remove(&session->display_destroy.link);

	wl_event_source_remove(session->udev_event);
	udev_monitor_unref(session->mon);
	udev_unref(session->udev);

	session->impl->destroy(session);
}

int wlr_session_open_file(struct wlr_session *session, const char *path) {
	int fd = session->impl->open(session, path);
	if (fd < 0) {
		return fd;
	}

	struct wlr_device *dev = malloc(sizeof(*dev));
	if (!dev) {
		wlr_log_errno(WLR_ERROR, "Allocation failed");
		goto error;
	}

	struct stat st;
	if (fstat(fd, &st) < 0) {
		wlr_log_errno(WLR_ERROR, "Stat failed");
		goto error;
	}

	dev->fd = fd;
	dev->dev = st.st_rdev;
	wl_signal_init(&dev->signal);
	wl_list_insert(&session->devices, &dev->link);

	return fd;

error:
	free(dev);
	close(fd);
	return -1;
}

static struct wlr_device *find_device(struct wlr_session *session, int fd) {
	struct wlr_device *dev;

	wl_list_for_each(dev, &session->devices, link) {
		if (dev->fd == fd) {
			return dev;
		}
	}

	wlr_log(WLR_ERROR, "Tried to use fd %d not opened by session", fd);
	assert(0);
	return NULL;
}

void wlr_session_close_file(struct wlr_session *session, int fd) {
	struct wlr_device *dev = find_device(session, fd);

	session->impl->close(session, fd);
	wl_list_remove(&dev->link);
	free(dev);
}

void wlr_session_signal_add(struct wlr_session *session, int fd,
		struct wl_listener *listener) {
	struct wlr_device *dev = find_device(session, fd);

	wl_signal_add(&dev->signal, listener);
}

{'fraction_non_alphanumeric': 0.13262208344175558, 'fraction_numerical': 0.004076676207823749, 'mean_word_length': 5.584808680753855, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 120, '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': '86799', 'n_tokens_mistral': 4653, 'n_tokens_neox': 4388, 'n_words': 902}