1 #define _POSIX_C_SOURCE 200809L
2 #include <assert.h>
3 #include <libudev.h>
4 #include <stddef.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <wayland-server-core.h>
10 #include <wlr/backend/session.h>
11 #include <wlr/backend/session/interface.h>
12 #include <wlr/config.h>
13 #include <wlr/util/log.h>
14 #include <xf86drm.h>
15 #include <xf86drmMode.h>
16 #include "backend/session/session.h"
17 #include "util/signal.h"
18 
19 extern const struct session_impl session_libseat;
20 extern const struct session_impl session_logind;
21 extern const struct session_impl session_direct;
22 extern const struct session_impl session_noop;
23 
24 static const struct session_impl *impls[] = {
25 #if WLR_HAS_LIBSEAT
26 	&session_libseat,
27 #endif
28 #if WLR_HAS_SYSTEMD || WLR_HAS_ELOGIND
29 	&session_logind,
30 #endif
31 	&session_direct,
32 	NULL,
33 };
34 
udev_event(int fd,uint32_t mask,void * data)35 static int udev_event(int fd, uint32_t mask, void *data) {
36 	struct wlr_session *session = data;
37 
38 	struct udev_device *udev_dev = udev_monitor_receive_device(session->mon);
39 	if (!udev_dev) {
40 		return 1;
41 	}
42 
43 	const char *action = udev_device_get_action(udev_dev);
44 
45 	wlr_log(WLR_DEBUG, "udev event for %s (%s)",
46 		udev_device_get_sysname(udev_dev), action);
47 
48 	if (!action || strcmp(action, "change") != 0) {
49 		goto out;
50 	}
51 
52 	dev_t devnum = udev_device_get_devnum(udev_dev);
53 	struct wlr_device *dev;
54 
55 	wl_list_for_each(dev, &session->devices, link) {
56 		if (dev->dev == devnum) {
57 			wlr_signal_emit_safe(&dev->signal, session);
58 			break;
59 		}
60 	}
61 
62 out:
63 	udev_device_unref(udev_dev);
64 	return 1;
65 }
66 
handle_display_destroy(struct wl_listener * listener,void * data)67 static void handle_display_destroy(struct wl_listener *listener, void *data) {
68 	struct wlr_session *session =
69 		wl_container_of(listener, session, display_destroy);
70 	wlr_session_destroy(session);
71 }
72 
session_init(struct wlr_session * session)73 void session_init(struct wlr_session *session) {
74 	wl_signal_init(&session->session_signal);
75 	wl_signal_init(&session->events.destroy);
76 	wl_list_init(&session->devices);
77 }
78 
wlr_session_create(struct wl_display * disp)79 struct wlr_session *wlr_session_create(struct wl_display *disp) {
80 	struct wlr_session *session = NULL;
81 
82 	const char *env_wlr_session = getenv("WLR_SESSION");
83 	if (env_wlr_session) {
84 		if (strcmp(env_wlr_session, "libseat") == 0) {
85 #if WLR_HAS_LIBSEAT
86 			session = session_libseat.create(disp);
87 #else
88 			wlr_log(WLR_ERROR, "wlroots is not compiled with libseat support");
89 #endif
90 		} else if (strcmp(env_wlr_session, "logind") == 0 ||
91 				strcmp(env_wlr_session, "systemd") == 0) {
92 #if WLR_HAS_SYSTEMD || WLR_HAS_ELOGIND
93 			session = session_logind.create(disp);
94 #else
95 			wlr_log(WLR_ERROR, "wlroots is not compiled with logind support");
96 #endif
97 		} else if (strcmp(env_wlr_session, "direct") == 0) {
98 			session = session_direct.create(disp);
99 		} else if (strcmp(env_wlr_session, "noop") == 0) {
100 			session = session_noop.create(disp);
101 		} else {
102 			wlr_log(WLR_ERROR, "Unsupported WLR_SESSION: %s",
103 				env_wlr_session);
104 		}
105 	} else {
106 		const struct session_impl **iter;
107 		for (iter = impls; !session && *iter; ++iter) {
108 			session = (*iter)->create(disp);
109 		}
110 	}
111 
112 	if (!session) {
113 		wlr_log(WLR_ERROR, "Failed to load session backend");
114 		return NULL;
115 	}
116 
117 	session->udev = udev_new();
118 	if (!session->udev) {
119 		wlr_log_errno(WLR_ERROR, "Failed to create udev context");
120 		goto error_session;
121 	}
122 
123 	session->mon = udev_monitor_new_from_netlink(session->udev, "udev");
124 	if (!session->mon) {
125 		wlr_log_errno(WLR_ERROR, "Failed to create udev monitor");
126 		goto error_udev;
127 	}
128 
129 	udev_monitor_filter_add_match_subsystem_devtype(session->mon, "drm", NULL);
130 	udev_monitor_enable_receiving(session->mon);
131 
132 	struct wl_event_loop *event_loop = wl_display_get_event_loop(disp);
133 	int fd = udev_monitor_get_fd(session->mon);
134 
135 	session->udev_event = wl_event_loop_add_fd(event_loop, fd,
136 		WL_EVENT_READABLE, udev_event, session);
137 	if (!session->udev_event) {
138 		wlr_log_errno(WLR_ERROR, "Failed to create udev event source");
139 		goto error_mon;
140 	}
141 
142 	session->display_destroy.notify = handle_display_destroy;
143 	wl_display_add_destroy_listener(disp, &session->display_destroy);
144 
145 	return session;
146 
147 error_mon:
148 	udev_monitor_unref(session->mon);
149 error_udev:
150 	udev_unref(session->udev);
151 error_session:
152 	session->impl->destroy(session);
153 	return NULL;
154 }
155 
wlr_session_destroy(struct wlr_session * session)156 void wlr_session_destroy(struct wlr_session *session) {
157 	if (!session) {
158 		return;
159 	}
160 
161 	wlr_signal_emit_safe(&session->events.destroy, session);
162 	wl_list_remove(&session->display_destroy.link);
163 
164 	wl_event_source_remove(session->udev_event);
165 	udev_monitor_unref(session->mon);
166 	udev_unref(session->udev);
167 
168 	session->impl->destroy(session);
169 }
170 
wlr_session_open_file(struct wlr_session * session,const char * path)171 int wlr_session_open_file(struct wlr_session *session, const char *path) {
172 	int fd = session->impl->open(session, path);
173 	if (fd < 0) {
174 		return fd;
175 	}
176 
177 	struct wlr_device *dev = malloc(sizeof(*dev));
178 	if (!dev) {
179 		wlr_log_errno(WLR_ERROR, "Allocation failed");
180 		goto error;
181 	}
182 
183 	struct stat st;
184 	if (fstat(fd, &st) < 0) {
185 		wlr_log_errno(WLR_ERROR, "Stat failed");
186 		goto error;
187 	}
188 
189 	dev->fd = fd;
190 	dev->dev = st.st_rdev;
191 	wl_signal_init(&dev->signal);
192 	wl_list_insert(&session->devices, &dev->link);
193 
194 	return fd;
195 
196 error:
197 	free(dev);
198 	return fd;
199 }
200 
find_device(struct wlr_session * session,int fd)201 static struct wlr_device *find_device(struct wlr_session *session, int fd) {
202 	struct wlr_device *dev;
203 
204 	wl_list_for_each(dev, &session->devices, link) {
205 		if (dev->fd == fd) {
206 			return dev;
207 		}
208 	}
209 
210 	wlr_log(WLR_ERROR, "Tried to use fd %d not opened by session", fd);
211 	assert(0);
212 	return NULL;
213 }
214 
wlr_session_close_file(struct wlr_session * session,int fd)215 void wlr_session_close_file(struct wlr_session *session, int fd) {
216 	struct wlr_device *dev = find_device(session, fd);
217 
218 	session->impl->close(session, fd);
219 	wl_list_remove(&dev->link);
220 	free(dev);
221 }
222 
wlr_session_signal_add(struct wlr_session * session,int fd,struct wl_listener * listener)223 void wlr_session_signal_add(struct wlr_session *session, int fd,
224 		struct wl_listener *listener) {
225 	struct wlr_device *dev = find_device(session, fd);
226 
227 	wl_signal_add(&dev->signal, listener);
228 }
229 
wlr_session_change_vt(struct wlr_session * session,unsigned vt)230 bool wlr_session_change_vt(struct wlr_session *session, unsigned vt) {
231 	if (!session) {
232 		return false;
233 	}
234 
235 	return session->impl->change_vt(session, vt);
236 }
237 
238 /* Tests if 'path' is KMS compatible by trying to open it.
239  * It leaves the open device in *fd_out it it succeeds.
240  */
open_if_kms(struct wlr_session * restrict session,const char * restrict path)241 static int open_if_kms(struct wlr_session *restrict session,
242 		const char *restrict path) {
243 	if (!path) {
244 		return -1;
245 	}
246 
247 	int fd = wlr_session_open_file(session, path);
248 	if (fd < 0) {
249 		return -1;
250 	}
251 
252 	drmVersion *ver = drmGetVersion(fd);
253 	if (!ver) {
254 		goto out_fd;
255 	}
256 
257 	drmFreeVersion(ver);
258 	return fd;
259 
260 out_fd:
261 	wlr_session_close_file(session, fd);
262 	return -1;
263 }
264 
explicit_find_gpus(struct wlr_session * session,size_t ret_len,int ret[static ret_len],const char * str)265 static size_t explicit_find_gpus(struct wlr_session *session,
266 		size_t ret_len, int ret[static ret_len], const char *str) {
267 	char *gpus = strdup(str);
268 	if (!gpus) {
269 		wlr_log_errno(WLR_ERROR, "Allocation failed");
270 		return 0;
271 	}
272 
273 	size_t i = 0;
274 	char *save;
275 	char *ptr = strtok_r(gpus, ":", &save);
276 	do {
277 		if (i >= ret_len) {
278 			break;
279 		}
280 
281 		ret[i] = open_if_kms(session, ptr);
282 		if (ret[i] < 0) {
283 			wlr_log(WLR_ERROR, "Unable to open %s as DRM device", ptr);
284 		} else {
285 			++i;
286 		}
287 	} while ((ptr = strtok_r(NULL, ":", &save)));
288 
289 	free(gpus);
290 	return i;
291 }
292 
293 /* Tries to find the primary GPU by checking for the "boot_vga" attribute.
294  * If it's not found, it returns the first valid GPU it finds.
295  */
wlr_session_find_gpus(struct wlr_session * session,size_t ret_len,int * ret)296 size_t wlr_session_find_gpus(struct wlr_session *session,
297 		size_t ret_len, int *ret) {
298 	const char *explicit = getenv("WLR_DRM_DEVICES");
299 	if (explicit) {
300 		return explicit_find_gpus(session, ret_len, ret, explicit);
301 	}
302 
303 	struct udev_enumerate *en = udev_enumerate_new(session->udev);
304 	if (!en) {
305 		wlr_log(WLR_ERROR, "Failed to create udev enumeration");
306 		return -1;
307 	}
308 
309 	udev_enumerate_add_match_subsystem(en, "drm");
310 	udev_enumerate_add_match_sysname(en, "card[0-9]*");
311 	udev_enumerate_scan_devices(en);
312 
313 	struct udev_list_entry *entry;
314 	size_t i = 0;
315 
316 	udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(en)) {
317 		if (i == ret_len) {
318 			break;
319 		}
320 
321 		bool is_boot_vga = false;
322 
323 		const char *path = udev_list_entry_get_name(entry);
324 		struct udev_device *dev = udev_device_new_from_syspath(session->udev, path);
325 		if (!dev) {
326 			continue;
327 		}
328 
329 		const char *seat = udev_device_get_property_value(dev, "ID_SEAT");
330 		if (!seat) {
331 			seat = "seat0";
332 		}
333 		if (session->seat[0] && strcmp(session->seat, seat) != 0) {
334 			udev_device_unref(dev);
335 			continue;
336 		}
337 
338 		// This is owned by 'dev', so we don't need to free it
339 		struct udev_device *pci =
340 			udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
341 
342 		if (pci) {
343 			const char *id = udev_device_get_sysattr_value(pci, "boot_vga");
344 			if (id && strcmp(id, "1") == 0) {
345 				is_boot_vga = true;
346 			}
347 		}
348 
349 		int fd = open_if_kms(session, udev_device_get_devnode(dev));
350 		if (fd < 0) {
351 			udev_device_unref(dev);
352 			continue;
353 		}
354 
355 		udev_device_unref(dev);
356 
357 		ret[i] = fd;
358 		if (is_boot_vga) {
359 			int tmp = ret[0];
360 			ret[0] = ret[i];
361 			ret[i] = tmp;
362 		}
363 
364 		++i;
365 	}
366 
367 	udev_enumerate_unref(en);
368 
369 	return i;
370 }
371