aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/wayland/wayland-client-helper.c
blob: 0af6940383208d18eba1e3afeabf62e291b912d7 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "wayland-client-helper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include "security-context-v1-protocol.h"
#include <wayland-client.h>

static void registry_handle_global(void *data, struct wl_registry *registry,
				   uint32_t name, const char *interface,
				   uint32_t version)
{
	struct wp_security_context_manager_v1 **out = data;

	if (strcmp(interface, wp_security_context_manager_v1_interface.name) ==
	    0)
		*out = wl_registry_bind(
			registry, name,
			&wp_security_context_manager_v1_interface, 1);
}

static void registry_handle_global_remove(void *data,
					  struct wl_registry *registry,
					  uint32_t name)
{
} /* no-op */

static const struct wl_registry_listener registry_listener = {
	.global = registry_handle_global,
	.global_remove = registry_handle_global_remove,
};

hakurei_wayland_res hakurei_security_context_bind(char *socket_path,
						  int server_fd,
						  const char *app_id,
						  const char *instance_id,
						  int close_fd)
{
	hakurei_wayland_res res =
		HAKUREI_WAYLAND_SUCCESS; /* see wayland.go for handling */

	struct wl_display *display = NULL;
	struct wl_registry *registry;
	struct wp_security_context_manager_v1 *security_context_manager = NULL;
	int event_cnt;
	int listen_fd = -1;
	struct sockaddr_un sockaddr = { 0 };
	struct wp_security_context_v1 *security_context;

	display = wl_display_connect_to_fd(server_fd);
	if (display == NULL) {
		res = HAKUREI_WAYLAND_CONNECT;
		goto out;
	};

	registry = wl_display_get_registry(display);
	if (wl_registry_add_listener(registry, &registry_listener,
				     &security_context_manager) < 0) {
		res = HAKUREI_WAYLAND_LISTENER;
		goto out;
	}
	event_cnt = wl_display_roundtrip(display);
	wl_registry_destroy(registry);
	if (event_cnt < 0) {
		res = HAKUREI_WAYLAND_ROUNDTRIP;
		goto out;
	}

	if (security_context_manager == NULL) {
		res = HAKUREI_WAYLAND_NOT_AVAIL;
		goto out;
	}

	listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (listen_fd < 0) {
		res = HAKUREI_WAYLAND_SOCKET;
		goto out;
	}

	sockaddr.sun_family = AF_UNIX;
	snprintf(sockaddr.sun_path, sizeof(sockaddr.sun_path), "%s",
		 socket_path);
	if (bind(listen_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) !=
	    0) {
		res = HAKUREI_WAYLAND_BIND;
		goto out;
	}

	if (listen(listen_fd, 0) != 0) {
		res = HAKUREI_WAYLAND_LISTEN;
		goto out;
	}

	security_context = wp_security_context_manager_v1_create_listener(
		security_context_manager, listen_fd, close_fd);
	if (security_context == NULL) { /* not reached */
		res = HAKUREI_WAYLAND_NOT_AVAIL;
		goto out;
	}
	wp_security_context_v1_set_sandbox_engine(security_context,
						  "app.hakurei");
	wp_security_context_v1_set_app_id(security_context, app_id);
	wp_security_context_v1_set_instance_id(security_context, instance_id);
	wp_security_context_v1_commit(security_context);
	wp_security_context_v1_destroy(security_context);
	if (wl_display_roundtrip(display) < 0) {
		res = HAKUREI_WAYLAND_ROUNDTRIP;
		goto out;
	}

out:
	if (listen_fd >= 0)
		close(listen_fd);
	if (security_context_manager != NULL)
		wp_security_context_manager_v1_destroy(
			security_context_manager);
	if (display != NULL)
		wl_display_disconnect(display);

	free((void *)socket_path);
	free((void *)app_id);
	free((void *)instance_id);
	return res;
}