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