• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #ifndef _GNU_SOURCE
27 #define _GNU_SOURCE
28 #endif
29 
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <stddef.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stdbool.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <dlfcn.h>
43 #include <assert.h>
44 #include <sys/time.h>
45 #include <fcntl.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 
49 #include "wayland-util.h"
50 #include "wayland-private.h"
51 #include "wayland-server-private.h"
52 #include "wayland-server.h"
53 #include "wayland-os.h"
54 
55 /* This is the size of the char array in struct sock_addr_un.
56  * No Wayland socket can be created with a path longer than this,
57  * including the null terminator.
58  */
59 #ifndef UNIX_PATH_MAX
60 #define UNIX_PATH_MAX	108
61 #endif
62 
63 #define LOCK_SUFFIX	".lock"
64 #define LOCK_SUFFIXLEN	5
65 
66 struct wl_socket {
67 	int fd;
68 	int fd_lock;
69 	struct sockaddr_un addr;
70 	char lock_addr[UNIX_PATH_MAX + LOCK_SUFFIXLEN];
71 	struct wl_list link;
72 	struct wl_event_source *source;
73 	char *display_name;
74 };
75 
76 struct wl_client {
77 	struct wl_connection *connection;
78 	struct wl_event_source *source;
79 	struct wl_display *display;
80 	struct wl_resource *display_resource;
81 	struct wl_list link;
82 	struct wl_map objects;
83 	struct wl_priv_signal destroy_signal;
84 	struct ucred ucred;
85 	int error;
86 	struct wl_priv_signal resource_created_signal;
87 };
88 
89 struct wl_display {
90 	struct wl_event_loop *loop;
91 	int run;
92 
93 	uint32_t id;
94 	uint32_t serial;
95 
96 	struct wl_list registry_resource_list;
97 	struct wl_list global_list;
98 	struct wl_list socket_list;
99 	struct wl_list client_list;
100 	struct wl_list protocol_loggers;
101 
102 	struct wl_priv_signal destroy_signal;
103 	struct wl_priv_signal create_client_signal;
104 
105 	struct wl_array additional_shm_formats;
106 
107 	wl_display_global_filter_func_t global_filter;
108 	void *global_filter_data;
109 };
110 
111 struct wl_global {
112 	struct wl_display *display;
113 	const struct wl_interface *interface;
114 	uint32_t name;
115 	uint32_t version;
116 	void *data;
117 	wl_global_bind_func_t bind;
118 	struct wl_list link;
119 	bool removed;
120 };
121 
122 struct wl_resource {
123 	struct wl_object object;
124 	wl_resource_destroy_func_t destroy;
125 	struct wl_list link;
126 	/* Unfortunately some users of libwayland (e.g. mesa) still use the
127 	 * deprecated wl_resource struct, even if creating it with the new
128 	 * wl_resource_create(). So we cannot change the layout of the struct
129 	 * unless after the data field. */
130 	struct wl_signal deprecated_destroy_signal;
131 	struct wl_client *client;
132 	void *data;
133 	int version;
134 	wl_dispatcher_func_t dispatcher;
135 	struct wl_priv_signal destroy_signal;
136 };
137 
138 struct wl_protocol_logger {
139 	struct wl_list link;
140 	wl_protocol_logger_func_t func;
141 	void *user_data;
142 };
143 
144 static int debug_server = 0;
145 
146 static void
log_closure(struct wl_resource * resource,struct wl_closure * closure,int send)147 log_closure(struct wl_resource *resource,
148 	    struct wl_closure *closure, int send)
149 {
150 	struct wl_object *object = &resource->object;
151 	struct wl_display *display = resource->client->display;
152 	struct wl_protocol_logger *protocol_logger;
153 	struct wl_protocol_logger_message message;
154 
155 	if (debug_server)
156 		wl_closure_print(closure, object, send);
157 
158 	if (!wl_list_empty(&display->protocol_loggers)) {
159 		message.resource = resource;
160 		message.message_opcode = closure->opcode;
161 		message.message = closure->message;
162 		message.arguments_count = closure->count;
163 		message.arguments = closure->args;
164 		wl_list_for_each(protocol_logger,
165 				 &display->protocol_loggers, link) {
166 			protocol_logger->func(protocol_logger->user_data,
167 					      send ? WL_PROTOCOL_LOGGER_EVENT :
168 						     WL_PROTOCOL_LOGGER_REQUEST,
169 					      &message);
170 		}
171 	}
172 }
173 
174 static bool
verify_objects(struct wl_resource * resource,uint32_t opcode,union wl_argument * args)175 verify_objects(struct wl_resource *resource, uint32_t opcode,
176 	       union wl_argument *args)
177 {
178 	struct wl_object *object = &resource->object;
179 	const char *signature = object->interface->events[opcode].signature;
180 	struct argument_details arg;
181 	struct wl_resource *res;
182 	int count, i;
183 
184 	count = arg_count_for_signature(signature);
185 	for (i = 0; i < count; i++) {
186 		signature = get_next_argument(signature, &arg);
187 		switch (arg.type) {
188 		case 'n':
189 		case 'o':
190 			res = (struct wl_resource *) (args[i].o);
191 			if (res && res->client != resource->client) {
192 				wl_log("compositor bug: The compositor "
193 				       "tried to use an object from one "
194 				       "client in a '%s.%s' for a different "
195 				       "client.\n", object->interface->name,
196 				       object->interface->events[opcode].name);
197 				return false;
198 			}
199 		}
200 	}
201 	return true;
202 }
203 
204 static void
handle_array(struct wl_resource * resource,uint32_t opcode,union wl_argument * args,int (* send_func)(struct wl_closure *,struct wl_connection *))205 handle_array(struct wl_resource *resource, uint32_t opcode,
206 	     union wl_argument *args,
207 	     int (*send_func)(struct wl_closure *, struct wl_connection *))
208 {
209 	struct wl_closure *closure;
210 	struct wl_object *object = &resource->object;
211 
212 	if (resource->client->error)
213 		return;
214 
215 	if (!verify_objects(resource, opcode, args)) {
216 		resource->client->error = 1;
217 		return;
218 	}
219 
220 	closure = wl_closure_marshal(object, opcode, args,
221 				     &object->interface->events[opcode]);
222 
223 	if (closure == NULL) {
224 		resource->client->error = 1;
225 		return;
226 	}
227 
228 	log_closure(resource, closure, true);
229 
230 	if (send_func(closure, resource->client->connection))
231 		resource->client->error = 1;
232 
233 	wl_closure_destroy(closure);
234 }
235 
236 WL_EXPORT void
wl_resource_post_event_array(struct wl_resource * resource,uint32_t opcode,union wl_argument * args)237 wl_resource_post_event_array(struct wl_resource *resource, uint32_t opcode,
238 			     union wl_argument *args)
239 {
240 	handle_array(resource, opcode, args, wl_closure_send);
241 }
242 
243 WL_EXPORT void
wl_resource_post_event(struct wl_resource * resource,uint32_t opcode,...)244 wl_resource_post_event(struct wl_resource *resource, uint32_t opcode, ...)
245 {
246 	union wl_argument args[WL_CLOSURE_MAX_ARGS];
247 	struct wl_object *object = &resource->object;
248 	va_list ap;
249 
250 	va_start(ap, opcode);
251 	wl_argument_from_va_list(object->interface->events[opcode].signature,
252 				 args, WL_CLOSURE_MAX_ARGS, ap);
253 	va_end(ap);
254 
255 	wl_resource_post_event_array(resource, opcode, args);
256 }
257 
258 
259 WL_EXPORT void
wl_resource_queue_event_array(struct wl_resource * resource,uint32_t opcode,union wl_argument * args)260 wl_resource_queue_event_array(struct wl_resource *resource, uint32_t opcode,
261 			      union wl_argument *args)
262 {
263 	handle_array(resource, opcode, args, wl_closure_queue);
264 }
265 
266 WL_EXPORT void
wl_resource_queue_event(struct wl_resource * resource,uint32_t opcode,...)267 wl_resource_queue_event(struct wl_resource *resource, uint32_t opcode, ...)
268 {
269 	union wl_argument args[WL_CLOSURE_MAX_ARGS];
270 	struct wl_object *object = &resource->object;
271 	va_list ap;
272 
273 	va_start(ap, opcode);
274 	wl_argument_from_va_list(object->interface->events[opcode].signature,
275 				 args, WL_CLOSURE_MAX_ARGS, ap);
276 	va_end(ap);
277 
278 	wl_resource_queue_event_array(resource, opcode, args);
279 }
280 
281 static void
wl_resource_post_error_vargs(struct wl_resource * resource,uint32_t code,const char * msg,va_list argp)282 wl_resource_post_error_vargs(struct wl_resource *resource,
283 			     uint32_t code, const char *msg, va_list argp)
284 {
285 	struct wl_client *client = resource->client;
286 	char buffer[128];
287 
288 	vsnprintf(buffer, sizeof buffer, msg, argp);
289 
290 	/*
291 	 * When a client aborts, its resources are destroyed in id order,
292 	 * which means the display resource is destroyed first. If destruction
293 	 * of any later resources results in a protocol error, we end up here
294 	 * with a NULL display_resource. Do not try to send errors to an
295 	 * already dead client.
296 	 */
297 	if (client->error || !client->display_resource)
298 		return;
299 
300 	wl_resource_post_event(client->display_resource,
301 			       WL_DISPLAY_ERROR, resource, code, buffer);
302 	client->error = 1;
303 
304 }
305 
306 WL_EXPORT void
wl_resource_post_error(struct wl_resource * resource,uint32_t code,const char * msg,...)307 wl_resource_post_error(struct wl_resource *resource,
308 		       uint32_t code, const char *msg, ...)
309 {
310 	va_list ap;
311 
312 	va_start(ap, msg);
313 	wl_resource_post_error_vargs(resource, code, msg, ap);
314 	va_end(ap);
315 }
316 
317 static void
destroy_client_with_error(struct wl_client * client,const char * reason)318 destroy_client_with_error(struct wl_client *client, const char *reason)
319 {
320 	wl_log("%s (pid %u)\n", reason, client->ucred.pid);
321 	wl_client_destroy(client);
322 }
323 
324 static int
wl_client_connection_data(int fd,uint32_t mask,void * data)325 wl_client_connection_data(int fd, uint32_t mask, void *data)
326 {
327 	struct wl_client *client = data;
328 	struct wl_connection *connection = client->connection;
329 	struct wl_resource *resource;
330 	struct wl_object *object;
331 	struct wl_closure *closure;
332 	const struct wl_message *message;
333 	uint32_t p[2];
334 	uint32_t resource_flags;
335 	int opcode, size, since;
336 	int len;
337 
338 	if (mask & WL_EVENT_HANGUP) {
339 		wl_client_destroy(client);
340 		return 1;
341 	}
342 
343 	if (mask & WL_EVENT_ERROR) {
344 		destroy_client_with_error(client, "socket error");
345 		return 1;
346 	}
347 
348 	if (mask & WL_EVENT_WRITABLE) {
349 		len = wl_connection_flush(connection);
350 		if (len < 0 && errno != EAGAIN) {
351 			destroy_client_with_error(
352 			    client, "failed to flush client connection");
353 			return 1;
354 		} else if (len >= 0) {
355 			wl_event_source_fd_update(client->source,
356 						  WL_EVENT_READABLE);
357 		}
358 	}
359 
360 	len = 0;
361 	if (mask & WL_EVENT_READABLE) {
362 		len = wl_connection_read(connection);
363 		if (len == 0 || (len < 0 && errno != EAGAIN)) {
364 			destroy_client_with_error(
365 			    client, "failed to read client connection");
366 			return 1;
367 		}
368 	}
369 
370 	while (len >= 0 && (size_t) len >= sizeof p) {
371 		wl_connection_copy(connection, p, sizeof p);
372 		opcode = p[1] & 0xffff;
373 		size = p[1] >> 16;
374 		if (len < size)
375 			break;
376 
377 		resource = wl_map_lookup(&client->objects, p[0]);
378 		resource_flags = wl_map_lookup_flags(&client->objects, p[0]);
379 		if (resource == NULL) {
380 			wl_resource_post_error(client->display_resource,
381 					       WL_DISPLAY_ERROR_INVALID_OBJECT,
382 					       "invalid object %u", p[0]);
383 			break;
384 		}
385 
386 		object = &resource->object;
387 		if (opcode >= object->interface->method_count) {
388 			wl_resource_post_error(client->display_resource,
389 					       WL_DISPLAY_ERROR_INVALID_METHOD,
390 					       "invalid method %d, object %s@%u",
391 					       opcode,
392 					       object->interface->name,
393 					       object->id);
394 			break;
395 		}
396 
397 		message = &object->interface->methods[opcode];
398 		since = wl_message_get_since(message);
399 		if (!(resource_flags & WL_MAP_ENTRY_LEGACY) &&
400 		    resource->version > 0 && resource->version < since) {
401 			wl_resource_post_error(client->display_resource,
402 					       WL_DISPLAY_ERROR_INVALID_METHOD,
403 					       "invalid method %d (since %d < %d)"
404 					       ", object %s@%u",
405 					       opcode, resource->version, since,
406 					       object->interface->name,
407 					       object->id);
408 			break;
409 		}
410 
411 
412 		closure = wl_connection_demarshal(client->connection, size,
413 						  &client->objects, message);
414 
415 		if (closure == NULL && errno == ENOMEM) {
416 			wl_resource_post_no_memory(resource);
417 			break;
418 		} else if (closure == NULL ||
419 			   wl_closure_lookup_objects(closure, &client->objects) < 0) {
420 			wl_resource_post_error(client->display_resource,
421 					       WL_DISPLAY_ERROR_INVALID_METHOD,
422 					       "invalid arguments for %s@%u.%s",
423 					       object->interface->name,
424 					       object->id,
425 					       message->name);
426 			wl_closure_destroy(closure);
427 			break;
428 		}
429 
430 		log_closure(resource, closure, false);
431 
432 		if ((resource_flags & WL_MAP_ENTRY_LEGACY) ||
433 		    resource->dispatcher == NULL) {
434 			wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER,
435 					  object, opcode, client);
436 		} else {
437 			wl_closure_dispatch(closure, resource->dispatcher,
438 					    object, opcode);
439 		}
440 
441 		wl_closure_destroy(closure);
442 
443 		if (client->error)
444 			break;
445 
446 		len = wl_connection_pending_input(connection);
447 	}
448 
449 	if (client->error) {
450 		destroy_client_with_error(client,
451 					  "error in client communication");
452 	}
453 
454 	return 1;
455 }
456 
457 /** Flush pending events to the client
458  *
459  * \param client The client object
460  *
461  * Events sent to clients are queued in a buffer and written to the
462  * socket later - typically when the compositor has handled all
463  * requests and goes back to block in the event loop.  This function
464  * flushes all queued up events for a client immediately.
465  *
466  * \memberof wl_client
467  */
468 WL_EXPORT void
wl_client_flush(struct wl_client * client)469 wl_client_flush(struct wl_client *client)
470 {
471 	wl_connection_flush(client->connection);
472 }
473 
474 /** Get the display object for the given client
475  *
476  * \param client The client object
477  * \return The display object the client is associated with.
478  *
479  * \memberof wl_client
480  */
481 WL_EXPORT struct wl_display *
wl_client_get_display(struct wl_client * client)482 wl_client_get_display(struct wl_client *client)
483 {
484 	return client->display;
485 }
486 
487 static int
488 bind_display(struct wl_client *client, struct wl_display *display);
489 
490 /** Create a client for the given file descriptor
491  *
492  * \param display The display object
493  * \param fd The file descriptor for the socket to the client
494  * \return The new client object or NULL on failure.
495  *
496  * Given a file descriptor corresponding to one end of a socket, this
497  * function will create a wl_client struct and add the new client to
498  * the compositors client list.  At that point, the client is
499  * initialized and ready to run, as if the client had connected to the
500  * servers listening socket.  When the client eventually sends
501  * requests to the compositor, the wl_client argument to the request
502  * handler will be the wl_client returned from this function.
503  *
504  * The other end of the socket can be passed to
505  * wl_display_connect_to_fd() on the client side or used with the
506  * WAYLAND_SOCKET environment variable on the client side.
507  *
508  * Listeners added with wl_display_add_client_created_listener() will
509  * be notified by this function after the client is fully constructed.
510  *
511  * On failure this function sets errno accordingly and returns NULL.
512  *
513  * \memberof wl_display
514  */
515 WL_EXPORT struct wl_client *
wl_client_create(struct wl_display * display,int fd)516 wl_client_create(struct wl_display *display, int fd)
517 {
518 	struct wl_client *client;
519 	socklen_t len;
520 
521 	client = zalloc(sizeof *client);
522 	if (client == NULL)
523 		return NULL;
524 
525 	wl_priv_signal_init(&client->resource_created_signal);
526 	client->display = display;
527 	client->source = wl_event_loop_add_fd(display->loop, fd,
528 					      WL_EVENT_READABLE,
529 					      wl_client_connection_data, client);
530 
531 	if (!client->source)
532 		goto err_client;
533 
534 	len = sizeof client->ucred;
535 	if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
536 		       &client->ucred, &len) < 0)
537 		goto err_source;
538 
539 	client->connection = wl_connection_create(fd);
540 	if (client->connection == NULL)
541 		goto err_source;
542 
543 	wl_map_init(&client->objects, WL_MAP_SERVER_SIDE);
544 
545 	if (wl_map_insert_at(&client->objects, 0, 0, NULL) < 0)
546 		goto err_map;
547 
548 	wl_priv_signal_init(&client->destroy_signal);
549 	if (bind_display(client, display) < 0)
550 		goto err_map;
551 
552 	wl_list_insert(display->client_list.prev, &client->link);
553 
554 	wl_priv_signal_emit(&display->create_client_signal, client);
555 
556 	return client;
557 
558 err_map:
559 	wl_map_release(&client->objects);
560 	wl_connection_destroy(client->connection);
561 err_source:
562 	wl_event_source_remove(client->source);
563 err_client:
564 	free(client);
565 	return NULL;
566 }
567 
568 /** Return Unix credentials for the client
569  *
570  * \param client The display object
571  * \param pid Returns the process ID
572  * \param uid Returns the user ID
573  * \param gid Returns the group ID
574  *
575  * This function returns the process ID, the user ID and the group ID
576  * for the given client.  The credentials come from getsockopt() with
577  * SO_PEERCRED, on the client socket fd.  All the pointers can be
578  * NULL, if the caller is not interested in a particular ID.
579  *
580  * Be aware that for clients that a compositor forks and execs and
581  * then connects using socketpair(), this function will return the
582  * credentials for the compositor.  The credentials for the socketpair
583  * are set at creation time in the compositor.
584  *
585  * \memberof wl_client
586  */
587 WL_EXPORT void
wl_client_get_credentials(struct wl_client * client,pid_t * pid,uid_t * uid,gid_t * gid)588 wl_client_get_credentials(struct wl_client *client,
589 			  pid_t *pid, uid_t *uid, gid_t *gid)
590 {
591 	if (pid)
592 		*pid = client->ucred.pid;
593 	if (uid)
594 		*uid = client->ucred.uid;
595 	if (gid)
596 		*gid = client->ucred.gid;
597 }
598 
599 /** Get the file descriptor for the client
600  *
601  * \param client The display object
602  * \return The file descriptor to use for the connection
603  *
604  * This function returns the file descriptor for the given client.
605  *
606  * Be sure to use the file descriptor from the client for inspection only.
607  * If the caller does anything to the file descriptor that changes its state,
608  * it will likely cause problems.
609  *
610  * See also wl_client_get_credentials().
611  * It is recommended that you evaluate whether wl_client_get_credentials()
612  * can be applied to your use case instead of this function.
613  *
614  * If you would like to distinguish just between the client and the compositor
615  * itself from the client's request, it can be done by getting the client
616  * credentials and by checking the PID of the client and the compositor's PID.
617  * Regarding the case in which the socketpair() is being used, you need to be
618  * careful. Please note the documentation for wl_client_get_credentials().
619  *
620  * This function can be used for a compositor to validate a request from
621  * a client if there are additional information provided from the client's
622  * file descriptor. For instance, suppose you can get the security contexts
623  * from the client's file descriptor. The compositor can validate the client's
624  * request with the contexts and make a decision whether it permits or deny it.
625  *
626  * \memberof wl_client
627  */
628 WL_EXPORT int
wl_client_get_fd(struct wl_client * client)629 wl_client_get_fd(struct wl_client *client)
630 {
631 	return wl_connection_get_fd(client->connection);
632 }
633 
634 /** Look up an object in the client name space
635  *
636  * \param client The client object
637  * \param id The object id
638  * \return The object or NULL if there is not object for the given ID
639  *
640  * This looks up an object in the client object name space by its
641  * object ID.
642  *
643  * \memberof wl_client
644  */
645 WL_EXPORT struct wl_resource *
wl_client_get_object(struct wl_client * client,uint32_t id)646 wl_client_get_object(struct wl_client *client, uint32_t id)
647 {
648 	return wl_map_lookup(&client->objects, id);
649 }
650 
651 WL_EXPORT void
wl_client_post_no_memory(struct wl_client * client)652 wl_client_post_no_memory(struct wl_client *client)
653 {
654 	wl_resource_post_error(client->display_resource,
655 			       WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
656 }
657 
658 /** Report an internal server error
659  *
660  * \param client The client object
661  * \param msg A printf-style format string
662  * \param ... Format string arguments
663  *
664  * Report an unspecified internal implementation error and disconnect
665  * the client.
666  *
667  * \memberof wl_client
668  */
669 WL_EXPORT void
wl_client_post_implementation_error(struct wl_client * client,char const * msg,...)670 wl_client_post_implementation_error(struct wl_client *client,
671 				    char const *msg, ...)
672 {
673 	va_list ap;
674 
675 	va_start(ap, msg);
676 	wl_resource_post_error_vargs(client->display_resource,
677 				     WL_DISPLAY_ERROR_IMPLEMENTATION,
678 				     msg, ap);
679 	va_end(ap);
680 }
681 
682 WL_EXPORT void
wl_resource_post_no_memory(struct wl_resource * resource)683 wl_resource_post_no_memory(struct wl_resource *resource)
684 {
685 	wl_resource_post_error(resource->client->display_resource,
686 			       WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
687 }
688 
689 /** Detect if a wl_resource uses the deprecated public definition.
690  *
691  * Before Wayland 1.2.0, the definition of struct wl_resource was public.
692  * It was made opaque just before 1.2.0, and later new fields were added.
693  * The new fields cannot be accessed if a program is using the deprecated
694  * definition, as there would not be memory allocated for them.
695  *
696  * The creation pattern for the deprecated definition was wl_resource_init()
697  * followed by wl_client_add_resource(). wl_resource_init() was an inline
698  * function and no longer exists, but binaries might still carry it.
699  * wl_client_add_resource() still exists for ABI compatibility.
700  */
701 static bool
resource_is_deprecated(struct wl_resource * resource)702 resource_is_deprecated(struct wl_resource *resource)
703 {
704 	struct wl_map *map = &resource->client->objects;
705 	int id = resource->object.id;
706 
707 	/* wl_client_add_resource() marks deprecated resources with the flag. */
708 	if (wl_map_lookup_flags(map, id) & WL_MAP_ENTRY_LEGACY)
709 		return true;
710 
711 	return false;
712 }
713 
714 static enum wl_iterator_result
destroy_resource(void * element,void * data,uint32_t flags)715 destroy_resource(void *element, void *data, uint32_t flags)
716 {
717 	struct wl_resource *resource = element;
718 
719 	wl_signal_emit(&resource->deprecated_destroy_signal, resource);
720 	/* Don't emit the new signal for deprecated resources, as that would
721 	 * access memory outside the bounds of the deprecated struct */
722 	if (!resource_is_deprecated(resource))
723 		wl_priv_signal_final_emit(&resource->destroy_signal, resource);
724 
725 	if (resource->destroy)
726 		resource->destroy(resource);
727 
728 	if (!(flags & WL_MAP_ENTRY_LEGACY))
729 		free(resource);
730 
731 	return WL_ITERATOR_CONTINUE;
732 }
733 
734 WL_EXPORT void
wl_resource_destroy(struct wl_resource * resource)735 wl_resource_destroy(struct wl_resource *resource)
736 {
737 	struct wl_client *client = resource->client;
738 	uint32_t id;
739 	uint32_t flags;
740 
741 	id = resource->object.id;
742 	flags = wl_map_lookup_flags(&client->objects, id);
743 	destroy_resource(resource, NULL, flags);
744 
745 	if (id < WL_SERVER_ID_START) {
746 		if (client->display_resource) {
747 			wl_resource_queue_event(client->display_resource,
748 						WL_DISPLAY_DELETE_ID, id);
749 		}
750 		wl_map_insert_at(&client->objects, 0, id, NULL);
751 	} else {
752 		wl_map_remove(&client->objects, id);
753 	}
754 }
755 
756 WL_EXPORT uint32_t
wl_resource_get_id(struct wl_resource * resource)757 wl_resource_get_id(struct wl_resource *resource)
758 {
759 	return resource->object.id;
760 }
761 
762 WL_EXPORT struct wl_list *
wl_resource_get_link(struct wl_resource * resource)763 wl_resource_get_link(struct wl_resource *resource)
764 {
765 	return &resource->link;
766 }
767 
768 WL_EXPORT struct wl_resource *
wl_resource_from_link(struct wl_list * link)769 wl_resource_from_link(struct wl_list *link)
770 {
771 	struct wl_resource *resource;
772 
773 	return wl_container_of(link, resource, link);
774 }
775 
776 WL_EXPORT struct wl_resource *
wl_resource_find_for_client(struct wl_list * list,struct wl_client * client)777 wl_resource_find_for_client(struct wl_list *list, struct wl_client *client)
778 {
779 	struct wl_resource *resource;
780 
781 	if (client == NULL)
782 		return NULL;
783 
784 	wl_list_for_each(resource, list, link) {
785 		if (resource->client == client)
786 			return resource;
787 	}
788 
789 	return NULL;
790 }
791 
792 WL_EXPORT struct wl_client *
wl_resource_get_client(struct wl_resource * resource)793 wl_resource_get_client(struct wl_resource *resource)
794 {
795 	return resource->client;
796 }
797 
798 WL_EXPORT void
wl_resource_set_user_data(struct wl_resource * resource,void * data)799 wl_resource_set_user_data(struct wl_resource *resource, void *data)
800 {
801 	resource->data = data;
802 }
803 
804 WL_EXPORT void *
wl_resource_get_user_data(struct wl_resource * resource)805 wl_resource_get_user_data(struct wl_resource *resource)
806 {
807 	return resource->data;
808 }
809 
810 WL_EXPORT int
wl_resource_get_version(struct wl_resource * resource)811 wl_resource_get_version(struct wl_resource *resource)
812 {
813 	return resource->version;
814 }
815 
816 WL_EXPORT void
wl_resource_set_destructor(struct wl_resource * resource,wl_resource_destroy_func_t destroy)817 wl_resource_set_destructor(struct wl_resource *resource,
818 			   wl_resource_destroy_func_t destroy)
819 {
820 	resource->destroy = destroy;
821 }
822 
823 WL_EXPORT int
wl_resource_instance_of(struct wl_resource * resource,const struct wl_interface * interface,const void * implementation)824 wl_resource_instance_of(struct wl_resource *resource,
825 			const struct wl_interface *interface,
826 			const void *implementation)
827 {
828 	return wl_interface_equal(resource->object.interface, interface) &&
829 		resource->object.implementation == implementation;
830 }
831 
832 WL_EXPORT void
wl_resource_add_destroy_listener(struct wl_resource * resource,struct wl_listener * listener)833 wl_resource_add_destroy_listener(struct wl_resource *resource,
834 				 struct wl_listener * listener)
835 {
836 	if (resource_is_deprecated(resource))
837 		wl_signal_add(&resource->deprecated_destroy_signal, listener);
838 	else
839 		wl_priv_signal_add(&resource->destroy_signal, listener);
840 }
841 
842 WL_EXPORT struct wl_listener *
wl_resource_get_destroy_listener(struct wl_resource * resource,wl_notify_func_t notify)843 wl_resource_get_destroy_listener(struct wl_resource *resource,
844 				 wl_notify_func_t notify)
845 {
846 	if (resource_is_deprecated(resource))
847 		return wl_signal_get(&resource->deprecated_destroy_signal, notify);
848 	return wl_priv_signal_get(&resource->destroy_signal, notify);
849 }
850 
851 /** Retrieve the interface name (class) of a resource object.
852  *
853  * \param resource The resource object
854  *
855  * \memberof wl_resource
856  */
857 WL_EXPORT const char *
wl_resource_get_class(struct wl_resource * resource)858 wl_resource_get_class(struct wl_resource *resource)
859 {
860 	return resource->object.interface->name;
861 }
862 
863 WL_EXPORT void
wl_client_add_destroy_listener(struct wl_client * client,struct wl_listener * listener)864 wl_client_add_destroy_listener(struct wl_client *client,
865 			       struct wl_listener *listener)
866 {
867 	wl_priv_signal_add(&client->destroy_signal, listener);
868 }
869 
870 WL_EXPORT struct wl_listener *
wl_client_get_destroy_listener(struct wl_client * client,wl_notify_func_t notify)871 wl_client_get_destroy_listener(struct wl_client *client,
872 			       wl_notify_func_t notify)
873 {
874 	return wl_priv_signal_get(&client->destroy_signal, notify);
875 }
876 
877 WL_EXPORT void
wl_client_destroy(struct wl_client * client)878 wl_client_destroy(struct wl_client *client)
879 {
880 	uint32_t serial = 0;
881 
882 	wl_priv_signal_final_emit(&client->destroy_signal, client);
883 
884 	wl_client_flush(client);
885 	wl_map_for_each(&client->objects, destroy_resource, &serial);
886 	wl_map_release(&client->objects);
887 	wl_event_source_remove(client->source);
888 	close(wl_connection_destroy(client->connection));
889 	wl_list_remove(&client->link);
890 	wl_list_remove(&client->resource_created_signal.listener_list);
891 	free(client);
892 }
893 
894 /* Check if a global filter is registered and use it if any.
895  *
896  * If no wl_global filter has been registered, this function will
897  * return true, allowing the wl_global to be visible to the wl_client
898  */
899 static bool
wl_global_is_visible(const struct wl_client * client,const struct wl_global * global)900 wl_global_is_visible(const struct wl_client *client,
901 	      const struct wl_global *global)
902 {
903 	struct wl_display *display = client->display;
904 
905 	return (display->global_filter == NULL ||
906 		display->global_filter(client, global, display->global_filter_data));
907 }
908 
909 static void
registry_bind(struct wl_client * client,struct wl_resource * resource,uint32_t name,const char * interface,uint32_t version,uint32_t id)910 registry_bind(struct wl_client *client,
911 	      struct wl_resource *resource, uint32_t name,
912 	      const char *interface, uint32_t version, uint32_t id)
913 {
914 	struct wl_global *global;
915 	struct wl_display *display = resource->data;
916 
917 	wl_list_for_each(global, &display->global_list, link)
918 		if (global->name == name)
919 			break;
920 
921 	if (&global->link == &display->global_list)
922 		wl_resource_post_error(resource,
923 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
924 				       "invalid global %s (%d)", interface, name);
925 	else if (strcmp(global->interface->name, interface) != 0)
926 		wl_resource_post_error(resource,
927 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
928 				       "invalid interface for global %u: "
929 				       "have %s, wanted %s",
930 				       name, interface, global->interface->name);
931 	else if (version == 0)
932 		wl_resource_post_error(resource,
933 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
934 				       "invalid version for global %s (%d): 0 is not a valid version",
935 				       interface, name);
936 	else if (global->version < version)
937 		wl_resource_post_error(resource,
938 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
939 				       "invalid version for global %s (%d): have %d, wanted %d",
940 				       interface, name, global->version, version);
941 	else if (!wl_global_is_visible(client, global))
942 		wl_resource_post_error(resource,
943 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
944 				       "invalid global %s (%d)", interface, name);
945 	else
946 		global->bind(client, global->data, version, id);
947 }
948 
949 static const struct wl_registry_interface registry_interface = {
950 	registry_bind
951 };
952 
953 static void
display_sync(struct wl_client * client,struct wl_resource * resource,uint32_t id)954 display_sync(struct wl_client *client,
955 	     struct wl_resource *resource, uint32_t id)
956 {
957 	struct wl_resource *callback;
958 	uint32_t serial;
959 
960 	callback = wl_resource_create(client, &wl_callback_interface, 1, id);
961 	if (callback == NULL) {
962 		wl_client_post_no_memory(client);
963 		return;
964 	}
965 
966 	serial = wl_display_get_serial(client->display);
967 	wl_callback_send_done(callback, serial);
968 	wl_resource_destroy(callback);
969 }
970 
971 static void
unbind_resource(struct wl_resource * resource)972 unbind_resource(struct wl_resource *resource)
973 {
974 	wl_list_remove(&resource->link);
975 }
976 
977 static void
display_get_registry(struct wl_client * client,struct wl_resource * resource,uint32_t id)978 display_get_registry(struct wl_client *client,
979 		     struct wl_resource *resource, uint32_t id)
980 {
981 	struct wl_display *display = resource->data;
982 	struct wl_resource *registry_resource;
983 	struct wl_global *global;
984 
985 	registry_resource =
986 		wl_resource_create(client, &wl_registry_interface, 1, id);
987 	if (registry_resource == NULL) {
988 		wl_client_post_no_memory(client);
989 		return;
990 	}
991 
992 	wl_resource_set_implementation(registry_resource,
993 				       &registry_interface,
994 				       display, unbind_resource);
995 
996 	wl_list_insert(&display->registry_resource_list,
997 		       &registry_resource->link);
998 
999 	wl_list_for_each(global, &display->global_list, link)
1000 		if (wl_global_is_visible(client, global) && !global->removed)
1001 			wl_resource_post_event(registry_resource,
1002 					       WL_REGISTRY_GLOBAL,
1003 					       global->name,
1004 					       global->interface->name,
1005 					       global->version);
1006 }
1007 
1008 static const struct wl_display_interface display_interface = {
1009 	display_sync,
1010 	display_get_registry
1011 };
1012 
1013 static void
destroy_client_display_resource(struct wl_resource * resource)1014 destroy_client_display_resource(struct wl_resource *resource)
1015 {
1016 	resource->client->display_resource = NULL;
1017 }
1018 
1019 static int
bind_display(struct wl_client * client,struct wl_display * display)1020 bind_display(struct wl_client *client, struct wl_display *display)
1021 {
1022 	client->display_resource =
1023 		wl_resource_create(client, &wl_display_interface, 1, 1);
1024 	if (client->display_resource == NULL) {
1025 		/* DON'T send no-memory error to client - it has no
1026 		 * resource to which it could post the event */
1027 		return -1;
1028 	}
1029 
1030 	wl_resource_set_implementation(client->display_resource,
1031 				       &display_interface, display,
1032 				       destroy_client_display_resource);
1033 	return 0;
1034 }
1035 
1036 /** Create Wayland display object.
1037  *
1038  * \return The Wayland display object. Null if failed to create
1039  *
1040  * This creates the wl_display object.
1041  *
1042  * \memberof wl_display
1043  */
1044 WL_EXPORT struct wl_display *
wl_display_create(void)1045 wl_display_create(void)
1046 {
1047 	struct wl_display *display;
1048 	const char *debug;
1049 
1050 	debug = getenv("WAYLAND_DEBUG");
1051 	if (debug && (strstr(debug, "server") || strstr(debug, "1")))
1052 		debug_server = 1;
1053 
1054 	display = malloc(sizeof *display);
1055 	if (display == NULL)
1056 		return NULL;
1057 
1058 	display->loop = wl_event_loop_create();
1059 	if (display->loop == NULL) {
1060 		free(display);
1061 		return NULL;
1062 	}
1063 
1064 	wl_list_init(&display->global_list);
1065 	wl_list_init(&display->socket_list);
1066 	wl_list_init(&display->client_list);
1067 	wl_list_init(&display->registry_resource_list);
1068 	wl_list_init(&display->protocol_loggers);
1069 
1070 	wl_priv_signal_init(&display->destroy_signal);
1071 	wl_priv_signal_init(&display->create_client_signal);
1072 
1073 	display->id = 1;
1074 	display->serial = 0;
1075 
1076 	display->global_filter = NULL;
1077 	display->global_filter_data = NULL;
1078 
1079 	wl_array_init(&display->additional_shm_formats);
1080 
1081 	return display;
1082 }
1083 
1084 static void
wl_socket_destroy(struct wl_socket * s)1085 wl_socket_destroy(struct wl_socket *s)
1086 {
1087 	if (s->source)
1088 		wl_event_source_remove(s->source);
1089 	if (s->addr.sun_path[0])
1090 		unlink(s->addr.sun_path);
1091 	if (s->fd >= 0)
1092 		close(s->fd);
1093 	if (s->lock_addr[0])
1094 		unlink(s->lock_addr);
1095 	if (s->fd_lock >= 0)
1096 		close(s->fd_lock);
1097 
1098 	free(s);
1099 }
1100 
1101 static struct wl_socket *
wl_socket_alloc(void)1102 wl_socket_alloc(void)
1103 {
1104 	struct wl_socket *s;
1105 
1106 	s = zalloc(sizeof *s);
1107 	if (!s)
1108 		return NULL;
1109 
1110 	s->fd = -1;
1111 	s->fd_lock = -1;
1112 
1113 	return s;
1114 }
1115 
1116 /** Destroy Wayland display object.
1117  *
1118  * \param display The Wayland display object which should be destroyed.
1119  * \return None.
1120  *
1121  * This function emits the wl_display destroy signal, releases
1122  * all the sockets added to this display, free's all the globals associated
1123  * with this display, free's memory of additional shared memory formats and
1124  * destroy the display object.
1125  *
1126  * \sa wl_display_add_destroy_listener
1127  *
1128  * \memberof wl_display
1129  */
1130 WL_EXPORT void
wl_display_destroy(struct wl_display * display)1131 wl_display_destroy(struct wl_display *display)
1132 {
1133 	struct wl_socket *s, *next;
1134 	struct wl_global *global, *gnext;
1135 
1136 	wl_priv_signal_final_emit(&display->destroy_signal, display);
1137 
1138 	wl_list_for_each_safe(s, next, &display->socket_list, link) {
1139 		wl_socket_destroy(s);
1140 	}
1141 	wl_event_loop_destroy(display->loop);
1142 
1143 	wl_list_for_each_safe(global, gnext, &display->global_list, link)
1144 		free(global);
1145 
1146 	wl_array_release(&display->additional_shm_formats);
1147 
1148 	wl_list_remove(&display->protocol_loggers);
1149 
1150 	free(display);
1151 }
1152 
1153 /** Set a filter function for global objects
1154  *
1155  * \param display The Wayland display object.
1156  * \param filter  The global filter function.
1157  * \param data User data to be associated with the global filter.
1158  * \return None.
1159  *
1160  * Set a filter for the wl_display to advertise or hide global objects
1161  * to clients.
1162  * The set filter will be used during wl_global advertisement to
1163  * determine whether a global object should be advertised to a
1164  * given client, and during wl_global binding to determine whether
1165  * a given client should be allowed to bind to a global.
1166  *
1167  * Clients that try to bind to a global that was filtered out will
1168  * have an error raised.
1169  *
1170  * Setting the filter NULL will result in all globals being
1171  * advertised to all clients. The default is no filter.
1172  *
1173  * \memberof wl_display
1174  */
1175 WL_EXPORT void
wl_display_set_global_filter(struct wl_display * display,wl_display_global_filter_func_t filter,void * data)1176 wl_display_set_global_filter(struct wl_display *display,
1177 			     wl_display_global_filter_func_t filter,
1178 			     void *data)
1179 {
1180 	display->global_filter = filter;
1181 	display->global_filter_data = data;
1182 }
1183 
1184 WL_EXPORT struct wl_global *
wl_global_create(struct wl_display * display,const struct wl_interface * interface,int version,void * data,wl_global_bind_func_t bind)1185 wl_global_create(struct wl_display *display,
1186 		 const struct wl_interface *interface, int version,
1187 		 void *data, wl_global_bind_func_t bind)
1188 {
1189 	struct wl_global *global;
1190 	struct wl_resource *resource;
1191 
1192 	if (version < 1) {
1193 		wl_log("wl_global_create: failing to create interface "
1194 		       "'%s' with version %d because it is less than 1\n",
1195 			interface->name, version);
1196 		return NULL;
1197 	}
1198 
1199 	if (version > interface->version) {
1200 		wl_log("wl_global_create: implemented version for '%s' "
1201 		       "higher than interface version (%d > %d)\n",
1202 		       interface->name, version, interface->version);
1203 		return NULL;
1204 	}
1205 
1206 	global = malloc(sizeof *global);
1207 	if (global == NULL)
1208 		return NULL;
1209 
1210 	global->display = display;
1211 	global->name = display->id++;
1212 	global->interface = interface;
1213 	global->version = version;
1214 	global->data = data;
1215 	global->bind = bind;
1216 	global->removed = false;
1217 	wl_list_insert(display->global_list.prev, &global->link);
1218 
1219 	wl_list_for_each(resource, &display->registry_resource_list, link)
1220 		wl_resource_post_event(resource,
1221 				       WL_REGISTRY_GLOBAL,
1222 				       global->name,
1223 				       global->interface->name,
1224 				       global->version);
1225 
1226 	return global;
1227 }
1228 
1229 /** Remove the global
1230  *
1231  * \param global The Wayland global.
1232  *
1233  * Broadcast a global remove event to all clients without destroying the
1234  * global. This function can only be called once per global.
1235  *
1236  * wl_global_destroy() removes the global and immediately destroys it. On
1237  * the other end, this function only removes the global, allowing clients
1238  * that have not yet received the global remove event to continue to bind to
1239  * it.
1240  *
1241  * This can be used by compositors to mitigate clients being disconnected
1242  * because a global has been added and removed too quickly. Compositors can call
1243  * wl_global_remove(), then wait an implementation-defined amount of time, then
1244  * call wl_global_destroy(). Note that the destruction of a global is still
1245  * racy, since clients have no way to acknowledge that they received the remove
1246  * event.
1247  *
1248  * \since 1.17.90
1249  */
1250 WL_EXPORT void
wl_global_remove(struct wl_global * global)1251 wl_global_remove(struct wl_global *global)
1252 {
1253 	struct wl_display *display = global->display;
1254 	struct wl_resource *resource;
1255 
1256 	if (global->removed)
1257 		wl_abort("wl_global_remove: called twice on the same "
1258 			 "global '%s@%"PRIu32"'", global->interface->name,
1259 			 global->name);
1260 
1261 	wl_list_for_each(resource, &display->registry_resource_list, link)
1262 		wl_resource_post_event(resource, WL_REGISTRY_GLOBAL_REMOVE,
1263 				       global->name);
1264 
1265 	global->removed = true;
1266 }
1267 
1268 WL_EXPORT void
wl_global_destroy(struct wl_global * global)1269 wl_global_destroy(struct wl_global *global)
1270 {
1271 	if (!global->removed)
1272 		wl_global_remove(global);
1273 	wl_list_remove(&global->link);
1274 	free(global);
1275 }
1276 
1277 WL_EXPORT const struct wl_interface *
wl_global_get_interface(const struct wl_global * global)1278 wl_global_get_interface(const struct wl_global *global)
1279 {
1280 	return global->interface;
1281 }
1282 
1283 WL_EXPORT void *
wl_global_get_user_data(const struct wl_global * global)1284 wl_global_get_user_data(const struct wl_global *global)
1285 {
1286 	return global->data;
1287 }
1288 
1289 /** Set the global's user data
1290  *
1291  * \param global The global object
1292  * \param data The user data pointer
1293  *
1294  * \since 1.17.90
1295  */
1296 WL_EXPORT void
wl_global_set_user_data(struct wl_global * global,void * data)1297 wl_global_set_user_data(struct wl_global *global, void *data)
1298 {
1299 	global->data = data;
1300 }
1301 
1302 /** Get the current serial number
1303  *
1304  * \param display The display object
1305  *
1306  * This function returns the most recent serial number, but does not
1307  * increment it.
1308  *
1309  * \memberof wl_display
1310  */
1311 WL_EXPORT uint32_t
wl_display_get_serial(struct wl_display * display)1312 wl_display_get_serial(struct wl_display *display)
1313 {
1314 	return display->serial;
1315 }
1316 
1317 /** Get the next serial number
1318  *
1319  * \param display The display object
1320  *
1321  * This function increments the display serial number and returns the
1322  * new value.
1323  *
1324  * \memberof wl_display
1325  */
1326 WL_EXPORT uint32_t
wl_display_next_serial(struct wl_display * display)1327 wl_display_next_serial(struct wl_display *display)
1328 {
1329 	display->serial++;
1330 
1331 	return display->serial;
1332 }
1333 
1334 WL_EXPORT struct wl_event_loop *
wl_display_get_event_loop(struct wl_display * display)1335 wl_display_get_event_loop(struct wl_display *display)
1336 {
1337 	return display->loop;
1338 }
1339 
1340 WL_EXPORT void
wl_display_terminate(struct wl_display * display)1341 wl_display_terminate(struct wl_display *display)
1342 {
1343 	display->run = 0;
1344 }
1345 
1346 WL_EXPORT void
wl_display_run(struct wl_display * display)1347 wl_display_run(struct wl_display *display)
1348 {
1349 	display->run = 1;
1350 
1351 	while (display->run) {
1352 		wl_display_flush_clients(display);
1353 		wl_event_loop_dispatch(display->loop, -1);
1354 	}
1355 }
1356 
1357 WL_EXPORT void
wl_display_flush_clients(struct wl_display * display)1358 wl_display_flush_clients(struct wl_display *display)
1359 {
1360 	struct wl_client *client, *next;
1361 	int ret;
1362 
1363 	wl_list_for_each_safe(client, next, &display->client_list, link) {
1364 		ret = wl_connection_flush(client->connection);
1365 		if (ret < 0 && errno == EAGAIN) {
1366 			wl_event_source_fd_update(client->source,
1367 						  WL_EVENT_WRITABLE |
1368 						  WL_EVENT_READABLE);
1369 		} else if (ret < 0) {
1370 			wl_client_destroy(client);
1371 		}
1372 	}
1373 }
1374 
1375 /** Destroy all clients connected to the display
1376  *
1377  * \param display The display object
1378  *
1379  * This function should be called right before wl_display_destroy() to ensure
1380  * all client resources are closed properly. Destroying a client from within
1381  * wl_display_destroy_clients() is safe, but creating one will leak resources
1382  * and raise a warning.
1383  *
1384  * \memberof wl_display
1385  */
1386 WL_EXPORT void
wl_display_destroy_clients(struct wl_display * display)1387 wl_display_destroy_clients(struct wl_display *display)
1388 {
1389 	struct wl_list tmp_client_list, *pos;
1390 	struct wl_client *client;
1391 
1392 	/* Move the whole client list to a temporary head because some new clients
1393 	 * might be added to the original head. */
1394 	wl_list_init(&tmp_client_list);
1395 	wl_list_insert_list(&tmp_client_list, &display->client_list);
1396 	wl_list_init(&display->client_list);
1397 
1398 	/* wl_list_for_each_safe isn't enough here: it fails if the next client is
1399 	 * destroyed by the destroy handler of the current one. */
1400 	while (!wl_list_empty(&tmp_client_list)) {
1401 		pos = tmp_client_list.next;
1402 		client = wl_container_of(pos, client, link);
1403 
1404 		wl_client_destroy(client);
1405 	}
1406 
1407 	if (!wl_list_empty(&display->client_list)) {
1408 		wl_log("wl_display_destroy_clients: cannot destroy all clients because "
1409 			   "new ones were created by destroy callbacks\n");
1410 	}
1411 }
1412 
1413 static int
socket_data(int fd,uint32_t mask,void * data)1414 socket_data(int fd, uint32_t mask, void *data)
1415 {
1416 	struct wl_display *display = data;
1417 	struct sockaddr_un name;
1418 	socklen_t length;
1419 	int client_fd;
1420 
1421 	length = sizeof name;
1422 	client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name,
1423 					 &length);
1424 	if (client_fd < 0)
1425 		wl_log("failed to accept: %s\n", strerror(errno));
1426 	else
1427 		if (!wl_client_create(display, client_fd))
1428 			close(client_fd);
1429 
1430 	return 1;
1431 }
1432 
1433 static int
wl_socket_lock(struct wl_socket * socket)1434 wl_socket_lock(struct wl_socket *socket)
1435 {
1436 	struct stat socket_stat;
1437 
1438 	snprintf(socket->lock_addr, sizeof socket->lock_addr,
1439 		 "%s%s", socket->addr.sun_path, LOCK_SUFFIX);
1440 
1441 	socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC | O_RDWR,
1442 			       (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
1443 
1444 	if (socket->fd_lock < 0) {
1445 		wl_log("unable to open lockfile %s check permissions\n",
1446 			socket->lock_addr);
1447 		goto err;
1448 	}
1449 
1450 	if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
1451 		wl_log("unable to lock lockfile %s, maybe another compositor is running\n",
1452 			socket->lock_addr);
1453 		goto err_fd;
1454 	}
1455 
1456 	if (lstat(socket->addr.sun_path, &socket_stat) < 0 ) {
1457 		if (errno != ENOENT) {
1458 			wl_log("did not manage to stat file %s\n",
1459 				socket->addr.sun_path);
1460 			goto err_fd;
1461 		}
1462 	} else if (socket_stat.st_mode & S_IWUSR ||
1463 		   socket_stat.st_mode & S_IWGRP) {
1464 		unlink(socket->addr.sun_path);
1465 	}
1466 
1467 	return 0;
1468 err_fd:
1469 	close(socket->fd_lock);
1470 	socket->fd_lock = -1;
1471 err:
1472 	*socket->lock_addr = 0;
1473 	/* we did not set this value here, but without lock the
1474 	 * socket won't be created anyway. This prevents the
1475 	 * wl_socket_destroy from unlinking already existing socket
1476 	 * created by other compositor */
1477 	*socket->addr.sun_path = 0;
1478 
1479 	return -1;
1480 }
1481 
1482 static int
wl_socket_init_for_display_name(struct wl_socket * s,const char * name)1483 wl_socket_init_for_display_name(struct wl_socket *s, const char *name)
1484 {
1485 	int name_size;
1486 	const char *runtime_dir = "";
1487 	const char *separator = "";
1488 
1489 	if (name[0] != '/') {
1490 		runtime_dir = getenv("XDG_RUNTIME_DIR");
1491 		if (!runtime_dir) {
1492 			wl_log("error: XDG_RUNTIME_DIR not set in the environment\n");
1493 
1494 			/* to prevent programs reporting
1495 			 * "failed to add socket: Success" */
1496 			errno = ENOENT;
1497 			return -1;
1498 		}
1499 		separator = "/";
1500 	}
1501 
1502 	s->addr.sun_family = AF_LOCAL;
1503 	name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
1504 			     "%s%s%s", runtime_dir, separator, name) + 1;
1505 
1506 	s->display_name = (s->addr.sun_path + name_size - 1) - strlen(name);
1507 
1508 	assert(name_size > 0);
1509 	if (name_size > (int)sizeof s->addr.sun_path) {
1510 		wl_log("error: socket path \"%s%s%s\" plus null terminator"
1511 		       " exceeds 108 bytes\n", runtime_dir, separator, name);
1512 		*s->addr.sun_path = 0;
1513 		/* to prevent programs reporting
1514 		 * "failed to add socket: Success" */
1515 		errno = ENAMETOOLONG;
1516 		return -1;
1517 	}
1518 
1519 	return 0;
1520 }
1521 
1522 static int
_wl_display_add_socket(struct wl_display * display,struct wl_socket * s)1523 _wl_display_add_socket(struct wl_display *display, struct wl_socket *s)
1524 {
1525 	socklen_t size;
1526 
1527 	s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
1528 	if (s->fd < 0) {
1529 		return -1;
1530 	}
1531 
1532 	size = offsetof (struct sockaddr_un, sun_path) + strlen(s->addr.sun_path);
1533 	if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
1534 		wl_log("bind() failed with error: %s\n", strerror(errno));
1535 		return -1;
1536 	}
1537 
1538 	chmod(s->addr.sun_path, 0777);
1539 
1540 	if (listen(s->fd, 128) < 0) {
1541 		wl_log("listen() failed with error: %s\n", strerror(errno));
1542 		return -1;
1543 	}
1544 
1545 	s->source = wl_event_loop_add_fd(display->loop, s->fd,
1546 					 WL_EVENT_READABLE,
1547 					 socket_data, display);
1548 	if (s->source == NULL) {
1549 		return -1;
1550 	}
1551 
1552 	wl_list_insert(display->socket_list.prev, &s->link);
1553 	return 0;
1554 }
1555 
1556 WL_EXPORT const char *
wl_display_add_socket_auto(struct wl_display * display)1557 wl_display_add_socket_auto(struct wl_display *display)
1558 {
1559 	struct wl_socket *s;
1560 	int displayno = 0;
1561 	char display_name[16] = "";
1562 
1563 	/* A reasonable number of maximum default sockets. If
1564 	 * you need more than this, use the explicit add_socket API. */
1565 	const int MAX_DISPLAYNO = 32;
1566 
1567 	s = wl_socket_alloc();
1568 	if (s == NULL)
1569 		return NULL;
1570 
1571 	do {
1572 		snprintf(display_name, sizeof display_name, "wayland-%d", displayno);
1573 		if (wl_socket_init_for_display_name(s, display_name) < 0) {
1574 			wl_socket_destroy(s);
1575 			return NULL;
1576 		}
1577 
1578 		if (wl_socket_lock(s) < 0)
1579 			continue;
1580 
1581 		if (_wl_display_add_socket(display, s) < 0) {
1582 			wl_socket_destroy(s);
1583 			return NULL;
1584 		}
1585 
1586 		return s->display_name;
1587 	} while (displayno++ < MAX_DISPLAYNO);
1588 
1589 	/* Ran out of display names. */
1590 	wl_socket_destroy(s);
1591 	errno = EINVAL;
1592 	return NULL;
1593 }
1594 
1595 /**  Add a socket with an existing fd to Wayland display for the clients to connect.
1596  *
1597  * \param display Wayland display to which the socket should be added.
1598  * \param sock_fd The existing socket file descriptor to be used
1599  * \return 0 if success. -1 if failed.
1600  *
1601  * The existing socket fd must already be created, opened, and locked.
1602  * The fd must be properly set to CLOEXEC and bound to a socket file
1603  * with both bind() and listen() already called.
1604  *
1605  * \memberof wl_display
1606  */
1607 WL_EXPORT int
wl_display_add_socket_fd(struct wl_display * display,int sock_fd)1608 wl_display_add_socket_fd(struct wl_display *display, int sock_fd)
1609 {
1610 	struct wl_socket *s;
1611 	struct stat buf;
1612 
1613 	/* Require a valid fd or fail */
1614 	if (sock_fd < 0 || fstat(sock_fd, &buf) < 0 || !S_ISSOCK(buf.st_mode)) {
1615 		return -1;
1616 	}
1617 
1618 	s = wl_socket_alloc();
1619 	if (s == NULL)
1620 		return -1;
1621 
1622 	s->source = wl_event_loop_add_fd(display->loop, sock_fd,
1623 					 WL_EVENT_READABLE,
1624 					 socket_data, display);
1625 	if (s->source == NULL) {
1626 		wl_log("failed to establish event source\n");
1627 		wl_socket_destroy(s);
1628 		return -1;
1629 	}
1630 
1631 	/* Reuse the existing fd */
1632 	s->fd = sock_fd;
1633 
1634 	wl_list_insert(display->socket_list.prev, &s->link);
1635 
1636 	return 0;
1637 }
1638 
1639 /** Add a socket to Wayland display for the clients to connect.
1640  *
1641  * \param display Wayland display to which the socket should be added.
1642  * \param name Name of the Unix socket.
1643  * \return 0 if success. -1 if failed.
1644  *
1645  * This adds a Unix socket to Wayland display which can be used by clients to
1646  * connect to Wayland display.
1647  *
1648  * If NULL is passed as name, then it would look for WAYLAND_DISPLAY env
1649  * variable for the socket name. If WAYLAND_DISPLAY is not set, then default
1650  * wayland-0 is used.
1651  *
1652  * If the socket name is a relative path, the Unix socket will be created in
1653  * the directory pointed to by environment variable XDG_RUNTIME_DIR. If
1654  * XDG_RUNTIME_DIR is not set, then this function fails and returns -1.
1655  *
1656  * If the socket name is an absolute path, then it is used as-is for the
1657  * the Unix socket.
1658  *
1659  * The length of the computed socket path must not exceed the maximum length
1660  * of a Unix socket path.
1661  * The function also fails if the user does not have write permission in the
1662  * directory or if the path is already in use.
1663  *
1664  * \memberof wl_display
1665  */
1666 WL_EXPORT int
wl_display_add_socket(struct wl_display * display,const char * name)1667 wl_display_add_socket(struct wl_display *display, const char *name)
1668 {
1669 	struct wl_socket *s;
1670 
1671 	s = wl_socket_alloc();
1672 	if (s == NULL)
1673 		return -1;
1674 
1675 	if (name == NULL)
1676 		name = getenv("WAYLAND_DISPLAY");
1677 	if (name == NULL)
1678 		name = "wayland-0";
1679 
1680 	if (wl_socket_init_for_display_name(s, name) < 0) {
1681 		wl_socket_destroy(s);
1682 		return -1;
1683 	}
1684 
1685 	if (wl_socket_lock(s) < 0) {
1686 		wl_socket_destroy(s);
1687 		return -1;
1688 	}
1689 
1690 	if (_wl_display_add_socket(display, s) < 0) {
1691 		wl_socket_destroy(s);
1692 		return -1;
1693 	}
1694 
1695 	return 0;
1696 }
1697 
1698 WL_EXPORT void
wl_display_add_destroy_listener(struct wl_display * display,struct wl_listener * listener)1699 wl_display_add_destroy_listener(struct wl_display *display,
1700 				struct wl_listener *listener)
1701 {
1702 	wl_priv_signal_add(&display->destroy_signal, listener);
1703 }
1704 
1705 /** Registers a listener for the client connection signal.
1706  *  When a new client object is created, \a listener will be notified, carrying
1707  *  a pointer to the new wl_client object.
1708  *
1709  *  \ref wl_client_create
1710  *  \ref wl_display
1711  *  \ref wl_listener
1712  *
1713  * \param display The display object
1714  * \param listener Signal handler object
1715  */
1716 WL_EXPORT void
wl_display_add_client_created_listener(struct wl_display * display,struct wl_listener * listener)1717 wl_display_add_client_created_listener(struct wl_display *display,
1718 					struct wl_listener *listener)
1719 {
1720 	wl_priv_signal_add(&display->create_client_signal, listener);
1721 }
1722 
1723 WL_EXPORT struct wl_listener *
wl_display_get_destroy_listener(struct wl_display * display,wl_notify_func_t notify)1724 wl_display_get_destroy_listener(struct wl_display *display,
1725 				wl_notify_func_t notify)
1726 {
1727 	return wl_priv_signal_get(&display->destroy_signal, notify);
1728 }
1729 
1730 WL_EXPORT void
wl_resource_set_implementation(struct wl_resource * resource,const void * implementation,void * data,wl_resource_destroy_func_t destroy)1731 wl_resource_set_implementation(struct wl_resource *resource,
1732 			       const void *implementation,
1733 			       void *data, wl_resource_destroy_func_t destroy)
1734 {
1735 	resource->object.implementation = implementation;
1736 	resource->data = data;
1737 	resource->destroy = destroy;
1738 	resource->dispatcher = NULL;
1739 }
1740 
1741 WL_EXPORT void
wl_resource_set_dispatcher(struct wl_resource * resource,wl_dispatcher_func_t dispatcher,const void * implementation,void * data,wl_resource_destroy_func_t destroy)1742 wl_resource_set_dispatcher(struct wl_resource *resource,
1743 			   wl_dispatcher_func_t dispatcher,
1744 			   const void *implementation,
1745 			   void *data, wl_resource_destroy_func_t destroy)
1746 {
1747 	resource->dispatcher = dispatcher;
1748 	resource->object.implementation = implementation;
1749 	resource->data = data;
1750 	resource->destroy = destroy;
1751 }
1752 
1753 /** Create a new resource object
1754  *
1755  * \param client The client owner of the new resource.
1756  * \param interface The interface of the new resource.
1757  * \param version The version of the new resource.
1758  * \param id The id of the new resource. If 0, an available id will be used.
1759  *
1760  * Listeners added with \a wl_client_add_resource_created_listener will be
1761  * notified at the end of this function.
1762  *
1763  * \memberof wl_resource
1764  */
1765 WL_EXPORT struct wl_resource *
wl_resource_create(struct wl_client * client,const struct wl_interface * interface,int version,uint32_t id)1766 wl_resource_create(struct wl_client *client,
1767 		   const struct wl_interface *interface,
1768 		   int version, uint32_t id)
1769 {
1770 	struct wl_resource *resource;
1771 
1772 	resource = malloc(sizeof *resource);
1773 	if (resource == NULL)
1774 		return NULL;
1775 
1776 	if (id == 0)
1777 		id = wl_map_insert_new(&client->objects, 0, NULL);
1778 
1779 	resource->object.id = id;
1780 	resource->object.interface = interface;
1781 	resource->object.implementation = NULL;
1782 
1783 	wl_signal_init(&resource->deprecated_destroy_signal);
1784 	wl_priv_signal_init(&resource->destroy_signal);
1785 
1786 	resource->destroy = NULL;
1787 	resource->client = client;
1788 	resource->data = NULL;
1789 	resource->version = version;
1790 	resource->dispatcher = NULL;
1791 
1792 	if (wl_map_insert_at(&client->objects, 0, id, resource) < 0) {
1793 		wl_resource_post_error(client->display_resource,
1794 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
1795 				       "invalid new id %d", id);
1796 		free(resource);
1797 		return NULL;
1798 	}
1799 
1800 	wl_priv_signal_emit(&client->resource_created_signal, resource);
1801 	return resource;
1802 }
1803 
1804 WL_EXPORT void
wl_log_set_handler_server(wl_log_func_t handler)1805 wl_log_set_handler_server(wl_log_func_t handler)
1806 {
1807 	wl_log_handler = handler;
1808 }
1809 
1810 /** Adds a new protocol logger.
1811  *
1812  * When a new protocol message arrives or is sent from the server
1813  * all the protocol logger functions will be called, carrying the
1814  * \a user_data pointer, the type of the message (request or
1815  * event) and the actual message.
1816  * The lifetime of the messages passed to the logger function ends
1817  * when they return so the messages cannot be stored and accessed
1818  * later.
1819  *
1820  * \a errno is set on error.
1821  *
1822  * \param display The display object
1823  * \param func The function to call to log a new protocol message
1824  * \param user_data The user data pointer to pass to \a func
1825  *
1826  * \return The protol logger object on success, NULL on failure.
1827  *
1828  * \sa wl_protocol_logger_destroy
1829  *
1830  * \memberof wl_display
1831  */
1832 WL_EXPORT struct wl_protocol_logger *
wl_display_add_protocol_logger(struct wl_display * display,wl_protocol_logger_func_t func,void * user_data)1833 wl_display_add_protocol_logger(struct wl_display *display,
1834 			       wl_protocol_logger_func_t func, void *user_data)
1835 {
1836 	struct wl_protocol_logger *logger;
1837 
1838 	logger = malloc(sizeof *logger);
1839 	if (!logger)
1840 		return NULL;
1841 
1842 	logger->func = func;
1843 	logger->user_data = user_data;
1844 	wl_list_insert(&display->protocol_loggers, &logger->link);
1845 
1846 	return logger;
1847 }
1848 
1849 /** Destroys a protocol logger.
1850  *
1851  * This function destroys a protocol logger and removes it from the display
1852  * it was added to with \a wl_display_add_protocol_logger.
1853  * The \a logger object becomes invalid after calling this function.
1854  *
1855  * \sa wl_display_add_protocol_logger
1856  *
1857  * \memberof wl_protocol_logger
1858  */
1859 WL_EXPORT void
wl_protocol_logger_destroy(struct wl_protocol_logger * logger)1860 wl_protocol_logger_destroy(struct wl_protocol_logger *logger)
1861 {
1862 	wl_list_remove(&logger->link);
1863 	free(logger);
1864 }
1865 
1866 /** Add support for a wl_shm pixel format
1867  *
1868  * \param display The display object
1869  * \param format The wl_shm pixel format to advertise
1870  * \return A pointer to the wl_shm format that was added to the list
1871  * or NULL if adding it to the list failed.
1872  *
1873  * Add the specified wl_shm format to the list of formats the wl_shm
1874  * object advertises when a client binds to it.  Adding a format to
1875  * the list means that clients will know that the compositor supports
1876  * this format and may use it for creating wl_shm buffers.  The
1877  * compositor must be able to handle the pixel format when a client
1878  * requests it.
1879  *
1880  * The compositor by default supports WL_SHM_FORMAT_ARGB8888 and
1881  * WL_SHM_FORMAT_XRGB8888.
1882  *
1883  * \memberof wl_display
1884  */
1885 WL_EXPORT uint32_t *
wl_display_add_shm_format(struct wl_display * display,uint32_t format)1886 wl_display_add_shm_format(struct wl_display *display, uint32_t format)
1887 {
1888 	uint32_t *p = NULL;
1889 
1890 	p = wl_array_add(&display->additional_shm_formats, sizeof *p);
1891 
1892 	if (p != NULL)
1893 		*p = format;
1894 	return p;
1895 }
1896 
1897 /**
1898  * Get list of additional wl_shm pixel formats
1899  *
1900  * \param display The display object
1901  *
1902  * This function returns the list of addition wl_shm pixel formats
1903  * that the compositor supports.  WL_SHM_FORMAT_ARGB8888 and
1904  * WL_SHM_FORMAT_XRGB8888 are always supported and not included in the
1905  * array, but all formats added through wl_display_add_shm_format()
1906  * will be in the array.
1907  *
1908  * \sa wl_display_add_shm_format()
1909  *
1910  * \private
1911  *
1912  * \memberof wl_display
1913  */
1914 struct wl_array *
wl_display_get_additional_shm_formats(struct wl_display * display)1915 wl_display_get_additional_shm_formats(struct wl_display *display)
1916 {
1917 	return &display->additional_shm_formats;
1918 }
1919 
1920 /** Get the list of currently connected clients
1921  *
1922  * \param display The display object
1923  *
1924  * This function returns a pointer to the list of clients currently
1925  * connected to the display. You can iterate on the list by using
1926  * the \a wl_client_for_each macro.
1927  * The returned value is valid for the lifetime of the \a display.
1928  * You must not modify the returned list, but only access it.
1929  *
1930  * \sa wl_client_for_each()
1931  * \sa wl_client_get_link()
1932  * \sa wl_client_from_link()
1933  *
1934  * \memberof wl_display
1935  */
1936 WL_EXPORT struct wl_list *
wl_display_get_client_list(struct wl_display * display)1937 wl_display_get_client_list(struct wl_display *display)
1938 {
1939 	return &display->client_list;
1940 }
1941 
1942 /** Get the link by which a client is inserted in the client list
1943  *
1944  * \param client The client object
1945  *
1946  * \sa wl_client_for_each()
1947  * \sa wl_display_get_client_list()
1948  * \sa wl_client_from_link()
1949  *
1950  * \memberof wl_client
1951  */
1952 WL_EXPORT struct wl_list *
wl_client_get_link(struct wl_client * client)1953 wl_client_get_link(struct wl_client *client)
1954 {
1955 	return &client->link;
1956 }
1957 
1958 /** Get a wl_client by its link
1959  *
1960  * \param link The link of a wl_client
1961  *
1962  * \sa wl_client_for_each()
1963  * \sa wl_display_get_client_list()
1964  * \sa wl_client_get_link()
1965  *
1966  * \memberof wl_client
1967  */
1968 WL_EXPORT struct wl_client *
wl_client_from_link(struct wl_list * link)1969 wl_client_from_link(struct wl_list *link)
1970 {
1971 	struct wl_client *client;
1972 
1973 	return wl_container_of(link, client, link);
1974 }
1975 
1976 /** Add a listener for the client's resource creation signal
1977  *
1978  * \param client The client object
1979  * \param listener The listener to be added
1980  *
1981  * When a new resource is created for this client the listener
1982  * will be notified, carrying the new resource as the data argument.
1983  *
1984  * \memberof wl_client
1985  */
1986 WL_EXPORT void
wl_client_add_resource_created_listener(struct wl_client * client,struct wl_listener * listener)1987 wl_client_add_resource_created_listener(struct wl_client *client,
1988 					struct wl_listener *listener)
1989 {
1990 	wl_priv_signal_add(&client->resource_created_signal, listener);
1991 }
1992 
1993 struct wl_resource_iterator_context {
1994 	void *user_data;
1995 	wl_client_for_each_resource_iterator_func_t it;
1996 };
1997 
1998 static enum wl_iterator_result
resource_iterator_helper(void * res,void * user_data,uint32_t flags)1999 resource_iterator_helper(void *res, void *user_data, uint32_t flags)
2000 {
2001 	struct wl_resource_iterator_context *context = user_data;
2002 	struct wl_resource *resource = res;
2003 
2004 	return context->it(resource, context->user_data);
2005 }
2006 
2007 /** Iterate over all the resources of a client
2008  *
2009  * \param client The client object
2010  * \param iterator The iterator function
2011  * \param user_data The user data pointer
2012  *
2013  * The function pointed by \a iterator will be called for each
2014  * resource owned by the client. The \a user_data will be passed
2015  * as the second argument of the iterator function.
2016  * If the \a iterator function returns \a WL_ITERATOR_CONTINUE the iteration
2017  * will continue, if it returns \a WL_ITERATOR_STOP it will stop.
2018  *
2019  * Creating and destroying resources while iterating is safe, but new
2020  * resources may or may not be picked up by the iterator.
2021  *
2022  * \sa wl_iterator_result
2023  *
2024  * \memberof wl_client
2025  */
2026 WL_EXPORT void
wl_client_for_each_resource(struct wl_client * client,wl_client_for_each_resource_iterator_func_t iterator,void * user_data)2027 wl_client_for_each_resource(struct wl_client *client,
2028 			    wl_client_for_each_resource_iterator_func_t iterator,
2029 			    void *user_data)
2030 {
2031 	struct wl_resource_iterator_context context = {
2032 		.user_data = user_data,
2033 		.it = iterator,
2034 	};
2035 
2036 	wl_map_for_each(&client->objects, resource_iterator_helper, &context);
2037 }
2038 
2039 /** \cond INTERNAL */
2040 
2041 /** Initialize a wl_priv_signal object
2042  *
2043  * wl_priv_signal is a safer implementation of a signal type, with the same API
2044  * as wl_signal, but kept as a private utility of libwayland-server.
2045  * It is safer because listeners can be removed from within wl_priv_signal_emit()
2046  * without corrupting the signal's list.
2047  *
2048  * Before passing a wl_priv_signal object to any other function it must be
2049  * initialized by using wl_priv_signal_init().
2050  *
2051  * \memberof wl_priv_signal
2052  */
2053 void
wl_priv_signal_init(struct wl_priv_signal * signal)2054 wl_priv_signal_init(struct wl_priv_signal *signal)
2055 {
2056 	wl_list_init(&signal->listener_list);
2057 	wl_list_init(&signal->emit_list);
2058 }
2059 
2060 /** Add a listener to a signal
2061  *
2062  * The new listener will be called when calling wl_signal_emit(). If a listener is
2063  * added to the signal while wl_signal_emit() is running it will be called from
2064  * the next time wl_priv_signal_emit() is called.
2065  * To remove a listener call wl_list_remove() on its link member.
2066  *
2067  * \memberof wl_priv_signal
2068  */
2069 void
wl_priv_signal_add(struct wl_priv_signal * signal,struct wl_listener * listener)2070 wl_priv_signal_add(struct wl_priv_signal *signal, struct wl_listener *listener)
2071 {
2072 	wl_list_insert(signal->listener_list.prev, &listener->link);
2073 }
2074 
2075 /** Get a listener added to a signal
2076  *
2077  * Returns the listener added to the given \a signal and with the given
2078  * \a notify function, or NULL if there isn't any.
2079  * Calling this function from within wl_priv_signal_emit() is safe and will
2080  * return the correct value.
2081  *
2082  * \memberof wl_priv_signal
2083  */
2084 struct wl_listener *
wl_priv_signal_get(struct wl_priv_signal * signal,wl_notify_func_t notify)2085 wl_priv_signal_get(struct wl_priv_signal *signal, wl_notify_func_t notify)
2086 {
2087 	struct wl_listener *l;
2088 
2089 	wl_list_for_each(l, &signal->listener_list, link)
2090 		if (l->notify == notify)
2091 			return l;
2092 	wl_list_for_each(l, &signal->emit_list, link)
2093 		if (l->notify == notify)
2094 			return l;
2095 
2096 	return NULL;
2097 }
2098 
2099 /** Emit the signal, calling all the installed listeners
2100  *
2101  * Iterate over all the listeners added to this \a signal and call
2102  * their \a notify function pointer, passing on the given \a data.
2103  * Removing or adding a listener from within wl_priv_signal_emit()
2104  * is safe.
2105  */
2106 void
wl_priv_signal_emit(struct wl_priv_signal * signal,void * data)2107 wl_priv_signal_emit(struct wl_priv_signal *signal, void *data)
2108 {
2109 	struct wl_listener *l;
2110 	struct wl_list *pos;
2111 
2112 	wl_list_insert_list(&signal->emit_list, &signal->listener_list);
2113 	wl_list_init(&signal->listener_list);
2114 
2115 	/* Take every element out of the list and put them in a temporary list.
2116 	 * This way, the 'it' func can remove any element it wants from the list
2117 	 * without troubles, because we always get the first element, not the
2118 	 * one after the current, which may be invalid.
2119 	 * wl_list_for_each_safe tries to be safe but it fails: it works fine
2120 	 * if the current item is removed, but not if the next one is. */
2121 	while (!wl_list_empty(&signal->emit_list)) {
2122 		pos = signal->emit_list.next;
2123 		l = wl_container_of(pos, l, link);
2124 
2125 		wl_list_remove(pos);
2126 		wl_list_insert(&signal->listener_list, pos);
2127 
2128 		l->notify(l, data);
2129 	}
2130 }
2131 
2132 /** Emit the signal for the last time, calling all the installed listeners
2133  *
2134  * Iterate over all the listeners added to this \a signal and call
2135  * their \a notify function pointer, passing on the given \a data.
2136  * Removing or adding a listener from within wl_priv_signal_emit()
2137  * is safe, as is freeing the structure containing the listener.
2138  *
2139  * A large body of external code assumes it's ok to free a destruction
2140  * listener without removing that listener from the list.  Mixing code
2141  * that acts like this and code that doesn't will result in list
2142  * corruption.
2143  *
2144  * We resolve this by removing each item from the list and isolating it
2145  * in another list.  We discard it completely after firing the notifier.
2146  * This should allow interoperability between code that unlinks its
2147  * destruction listeners and code that just frees structures they're in.
2148  *
2149  */
2150 void
wl_priv_signal_final_emit(struct wl_priv_signal * signal,void * data)2151 wl_priv_signal_final_emit(struct wl_priv_signal *signal, void *data)
2152 {
2153 	struct wl_listener *l;
2154 	struct wl_list *pos;
2155 
2156 	/* During a destructor notifier isolate every list item before
2157 	 * notifying.  This renders harmless the long standing misuse
2158 	 * of freeing listeners without removing them, but allows
2159 	 * callers that do choose to remove them to interoperate with
2160 	 * ones that don't. */
2161 	while (!wl_list_empty(&signal->listener_list)) {
2162 		pos = signal->listener_list.next;
2163 		l = wl_container_of(pos, l, link);
2164 
2165 		wl_list_remove(pos);
2166 		wl_list_init(pos);
2167 
2168 		l->notify(l, data);
2169 	}
2170 }
2171 
2172 /** \endcond INTERNAL */
2173 
2174 /** \cond */ /* Deprecated functions below. */
2175 
2176 uint32_t
2177 wl_client_add_resource(struct wl_client *client,
2178 		       struct wl_resource *resource) WL_DEPRECATED;
2179 
2180 WL_EXPORT uint32_t
wl_client_add_resource(struct wl_client * client,struct wl_resource * resource)2181 wl_client_add_resource(struct wl_client *client,
2182 		       struct wl_resource *resource)
2183 {
2184 	if (resource->object.id == 0) {
2185 		resource->object.id =
2186 			wl_map_insert_new(&client->objects,
2187 					  WL_MAP_ENTRY_LEGACY, resource);
2188 	} else if (wl_map_insert_at(&client->objects, WL_MAP_ENTRY_LEGACY,
2189 				  resource->object.id, resource) < 0) {
2190 		wl_resource_post_error(client->display_resource,
2191 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
2192 				       "invalid new id %d",
2193 				       resource->object.id);
2194 		return 0;
2195 	}
2196 
2197 	resource->client = client;
2198 	wl_signal_init(&resource->deprecated_destroy_signal);
2199 
2200 	return resource->object.id;
2201 }
2202 
2203 struct wl_resource *
2204 wl_client_add_object(struct wl_client *client,
2205 		     const struct wl_interface *interface,
2206 		     const void *implementation,
2207 		     uint32_t id, void *data) WL_DEPRECATED;
2208 
2209 WL_EXPORT struct wl_resource *
wl_client_add_object(struct wl_client * client,const struct wl_interface * interface,const void * implementation,uint32_t id,void * data)2210 wl_client_add_object(struct wl_client *client,
2211 		     const struct wl_interface *interface,
2212 		     const void *implementation, uint32_t id, void *data)
2213 {
2214 	struct wl_resource *resource;
2215 
2216 	resource = wl_resource_create(client, interface, -1, id);
2217 	if (resource == NULL)
2218 		wl_client_post_no_memory(client);
2219 	else
2220 		wl_resource_set_implementation(resource,
2221 					       implementation, data, NULL);
2222 
2223 	return resource;
2224 }
2225 
2226 struct wl_resource *
2227 wl_client_new_object(struct wl_client *client,
2228 		     const struct wl_interface *interface,
2229 		     const void *implementation, void *data) WL_DEPRECATED;
2230 
2231 WL_EXPORT struct wl_resource *
wl_client_new_object(struct wl_client * client,const struct wl_interface * interface,const void * implementation,void * data)2232 wl_client_new_object(struct wl_client *client,
2233 		     const struct wl_interface *interface,
2234 		     const void *implementation, void *data)
2235 {
2236 	struct wl_resource *resource;
2237 
2238 	resource = wl_resource_create(client, interface, -1, 0);
2239 	if (resource == NULL)
2240 		wl_client_post_no_memory(client);
2241 	else
2242 		wl_resource_set_implementation(resource,
2243 					       implementation, data, NULL);
2244 
2245 	return resource;
2246 }
2247 
2248 struct wl_global *
2249 wl_display_add_global(struct wl_display *display,
2250 		      const struct wl_interface *interface,
2251 		      void *data, wl_global_bind_func_t bind) WL_DEPRECATED;
2252 
2253 WL_EXPORT struct wl_global *
wl_display_add_global(struct wl_display * display,const struct wl_interface * interface,void * data,wl_global_bind_func_t bind)2254 wl_display_add_global(struct wl_display *display,
2255 		      const struct wl_interface *interface,
2256 		      void *data, wl_global_bind_func_t bind)
2257 {
2258 	return wl_global_create(display, interface, interface->version, data, bind);
2259 }
2260 
2261 void
2262 wl_display_remove_global(struct wl_display *display,
2263 			 struct wl_global *global) WL_DEPRECATED;
2264 
2265 WL_EXPORT void
wl_display_remove_global(struct wl_display * display,struct wl_global * global)2266 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
2267 {
2268 	wl_global_destroy(global);
2269 }
2270 
2271 /** \endcond */
2272 
2273 /* Functions at the end of this file are deprecated.  Instead of adding new
2274  * code here, add it before the comment above that states:
2275  * Deprecated functions below.
2276  */
2277