"Fossies" - the Fresh Open Source Software Archive

Member "wayland-1.18.0/src/wayland-server.c" (11 Feb 2020, 61341 Bytes) of package /linux/misc/wayland-1.18.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "wayland-server.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.17.0_vs_1.18.0.

    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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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 *
  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 *
  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
  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
  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 *
  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
  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
  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
  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  * defition, 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 compatiblity.
  698  */
  699 static bool
  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
  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
  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
  755 wl_resource_get_id(struct wl_resource *resource)
  756 {
  757     return resource->object.id;
  758 }
  759 
  760 WL_EXPORT struct wl_list *
  761 wl_resource_get_link(struct wl_resource *resource)
  762 {
  763     return &resource->link;
  764 }
  765 
  766 WL_EXPORT struct wl_resource *
  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 *
  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 *
  791 wl_resource_get_client(struct wl_resource *resource)
  792 {
  793     return resource->client;
  794 }
  795 
  796 WL_EXPORT void
  797 wl_resource_set_user_data(struct wl_resource *resource, void *data)
  798 {
  799     resource->data = data;
  800 }
  801 
  802 WL_EXPORT void *
  803 wl_resource_get_user_data(struct wl_resource *resource)
  804 {
  805     return resource->data;
  806 }
  807 
  808 WL_EXPORT int
  809 wl_resource_get_version(struct wl_resource *resource)
  810 {
  811     return resource->version;
  812 }
  813 
  814 WL_EXPORT void
  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
  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
  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 *
  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 *
  856 wl_resource_get_class(struct wl_resource *resource)
  857 {
  858     return resource->object.interface->name;
  859 }
  860 
  861 WL_EXPORT void
  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 *
  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
  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 funtion will
  895  * return true, allowing the wl_global to be visible to the wl_client
  896  */
  897 static bool
  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
  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
  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
  970 unbind_resource(struct wl_resource *resource)
  971 {
  972     wl_list_remove(&resource->link);
  973 }
  974 
  975 static void
  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                        &registry_interface,
  992                        display, unbind_resource);
  993 
  994     wl_list_insert(&display->registry_resource_list,
  995                &registry_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
 1012 destroy_client_display_resource(struct wl_resource *resource)
 1013 {
 1014     resource->client->display_resource = NULL;
 1015 }
 1016 
 1017 static int
 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 *
 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
 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 *
 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
 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 funtion.
 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 advertisment 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
 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 *
 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
 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
 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 *
 1276 wl_global_get_interface(const struct wl_global *global)
 1277 {
 1278     return global->interface;
 1279 }
 1280 
 1281 WL_EXPORT void *
 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
 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
 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
 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 *
 1333 wl_display_get_event_loop(struct wl_display *display)
 1334 {
 1335     return display->loop;
 1336 }
 1337 
 1338 WL_EXPORT void
 1339 wl_display_terminate(struct wl_display *display)
 1340 {
 1341     display->run = 0;
 1342 }
 1343 
 1344 WL_EXPORT void
 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
 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
 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
 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
 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
 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 
 1486     runtime_dir = getenv("XDG_RUNTIME_DIR");
 1487     if (!runtime_dir) {
 1488         wl_log("error: XDG_RUNTIME_DIR not set in the environment\n");
 1489 
 1490         /* to prevent programs reporting
 1491          * "failed to add socket: Success" */
 1492         errno = ENOENT;
 1493         return -1;
 1494     }
 1495 
 1496     s->addr.sun_family = AF_LOCAL;
 1497     name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
 1498                  "%s/%s", runtime_dir, name) + 1;
 1499 
 1500     s->display_name = (s->addr.sun_path + name_size - 1) - strlen(name);
 1501 
 1502     assert(name_size > 0);
 1503     if (name_size > (int)sizeof s->addr.sun_path) {
 1504         wl_log("error: socket path \"%s/%s\" plus null terminator"
 1505                " exceeds 108 bytes\n", runtime_dir, name);
 1506         *s->addr.sun_path = 0;
 1507         /* to prevent programs reporting
 1508          * "failed to add socket: Success" */
 1509         errno = ENAMETOOLONG;
 1510         return -1;
 1511     }
 1512 
 1513     return 0;
 1514 }
 1515 
 1516 static int
 1517 _wl_display_add_socket(struct wl_display *display, struct wl_socket *s)
 1518 {
 1519     socklen_t size;
 1520 
 1521     s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
 1522     if (s->fd < 0) {
 1523         return -1;
 1524     }
 1525 
 1526     size = offsetof (struct sockaddr_un, sun_path) + strlen(s->addr.sun_path);
 1527     if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
 1528         wl_log("bind() failed with error: %s\n", strerror(errno));
 1529         return -1;
 1530     }
 1531 
 1532     if (listen(s->fd, 128) < 0) {
 1533         wl_log("listen() failed with error: %s\n", strerror(errno));
 1534         return -1;
 1535     }
 1536 
 1537     s->source = wl_event_loop_add_fd(display->loop, s->fd,
 1538                      WL_EVENT_READABLE,
 1539                      socket_data, display);
 1540     if (s->source == NULL) {
 1541         return -1;
 1542     }
 1543 
 1544     wl_list_insert(display->socket_list.prev, &s->link);
 1545     return 0;
 1546 }
 1547 
 1548 WL_EXPORT const char *
 1549 wl_display_add_socket_auto(struct wl_display *display)
 1550 {
 1551     struct wl_socket *s;
 1552     int displayno = 0;
 1553     char display_name[16] = "";
 1554 
 1555     /* A reasonable number of maximum default sockets. If
 1556      * you need more than this, use the explicit add_socket API. */
 1557     const int MAX_DISPLAYNO = 32;
 1558 
 1559     s = wl_socket_alloc();
 1560     if (s == NULL)
 1561         return NULL;
 1562 
 1563     do {
 1564         snprintf(display_name, sizeof display_name, "wayland-%d", displayno);
 1565         if (wl_socket_init_for_display_name(s, display_name) < 0) {
 1566             wl_socket_destroy(s);
 1567             return NULL;
 1568         }
 1569 
 1570         if (wl_socket_lock(s) < 0)
 1571             continue;
 1572 
 1573         if (_wl_display_add_socket(display, s) < 0) {
 1574             wl_socket_destroy(s);
 1575             return NULL;
 1576         }
 1577 
 1578         return s->display_name;
 1579     } while (displayno++ < MAX_DISPLAYNO);
 1580 
 1581     /* Ran out of display names. */
 1582     wl_socket_destroy(s);
 1583     errno = EINVAL;
 1584     return NULL;
 1585 }
 1586 
 1587 /**  Add a socket with an existing fd to Wayland display for the clients to connect.
 1588  *
 1589  * \param display Wayland display to which the socket should be added.
 1590  * \param sock_fd The existing socket file descriptor to be used
 1591  * \return 0 if success. -1 if failed.
 1592  *
 1593  * The existing socket fd must already be created, opened, and locked.
 1594  * The fd must be properly set to CLOEXEC and bound to a socket file
 1595  * with both bind() and listen() already called.
 1596  *
 1597  * \memberof wl_display
 1598  */
 1599 WL_EXPORT int
 1600 wl_display_add_socket_fd(struct wl_display *display, int sock_fd)
 1601 {
 1602     struct wl_socket *s;
 1603     struct stat buf;
 1604 
 1605     /* Require a valid fd or fail */
 1606     if (sock_fd < 0 || fstat(sock_fd, &buf) < 0 || !S_ISSOCK(buf.st_mode)) {
 1607         return -1;
 1608     }
 1609 
 1610     s = wl_socket_alloc();
 1611     if (s == NULL)
 1612         return -1;
 1613 
 1614     s->source = wl_event_loop_add_fd(display->loop, sock_fd,
 1615                      WL_EVENT_READABLE,
 1616                      socket_data, display);
 1617     if (s->source == NULL) {
 1618         wl_log("failed to establish event source\n");
 1619         wl_socket_destroy(s);
 1620         return -1;
 1621     }
 1622 
 1623     /* Reuse the existing fd */
 1624     s->fd = sock_fd;
 1625 
 1626     wl_list_insert(display->socket_list.prev, &s->link);
 1627 
 1628     return 0;
 1629 }
 1630 
 1631 /** Add a socket to Wayland display for the clients to connect.
 1632  *
 1633  * \param display Wayland display to which the socket should be added.
 1634  * \param name Name of the Unix socket.
 1635  * \return 0 if success. -1 if failed.
 1636  *
 1637  * This adds a Unix socket to Wayland display which can be used by clients to
 1638  * connect to Wayland display.
 1639  *
 1640  * If NULL is passed as name, then it would look for WAYLAND_DISPLAY env
 1641  * variable for the socket name. If WAYLAND_DISPLAY is not set, then default
 1642  * wayland-0 is used.
 1643  *
 1644  * The Unix socket will be created in the directory pointed to by environment
 1645  * variable XDG_RUNTIME_DIR. If XDG_RUNTIME_DIR is not set, then this function
 1646  * fails and returns -1.
 1647  *
 1648  * The length of socket path, i.e., the path set in XDG_RUNTIME_DIR and the
 1649  * socket name, must not exceed the maximum length of a Unix socket path.
 1650  * The function also fails if the user do not have write permission in the
 1651  * XDG_RUNTIME_DIR path or if the socket name is already in use.
 1652  *
 1653  * \memberof wl_display
 1654  */
 1655 WL_EXPORT int
 1656 wl_display_add_socket(struct wl_display *display, const char *name)
 1657 {
 1658     struct wl_socket *s;
 1659 
 1660     s = wl_socket_alloc();
 1661     if (s == NULL)
 1662         return -1;
 1663 
 1664     if (name == NULL)
 1665         name = getenv("WAYLAND_DISPLAY");
 1666     if (name == NULL)
 1667         name = "wayland-0";
 1668 
 1669     if (wl_socket_init_for_display_name(s, name) < 0) {
 1670         wl_socket_destroy(s);
 1671         return -1;
 1672     }
 1673 
 1674     if (wl_socket_lock(s) < 0) {
 1675         wl_socket_destroy(s);
 1676         return -1;
 1677     }
 1678 
 1679     if (_wl_display_add_socket(display, s) < 0) {
 1680         wl_socket_destroy(s);
 1681         return -1;
 1682     }
 1683 
 1684     return 0;
 1685 }
 1686 
 1687 WL_EXPORT void
 1688 wl_display_add_destroy_listener(struct wl_display *display,
 1689                 struct wl_listener *listener)
 1690 {
 1691     wl_priv_signal_add(&display->destroy_signal, listener);
 1692 }
 1693 
 1694 /** Registers a listener for the client connection signal.
 1695  *  When a new client object is created, \a listener will be notified, carrying
 1696  *  a pointer to the new wl_client object.
 1697  *
 1698  *  \ref wl_client_create
 1699  *  \ref wl_display
 1700  *  \ref wl_listener
 1701  *
 1702  * \param display The display object
 1703  * \param listener Signal handler object
 1704  */
 1705 WL_EXPORT void
 1706 wl_display_add_client_created_listener(struct wl_display *display,
 1707                     struct wl_listener *listener)
 1708 {
 1709     wl_priv_signal_add(&display->create_client_signal, listener);
 1710 }
 1711 
 1712 WL_EXPORT struct wl_listener *
 1713 wl_display_get_destroy_listener(struct wl_display *display,
 1714                 wl_notify_func_t notify)
 1715 {
 1716     return wl_priv_signal_get(&display->destroy_signal, notify);
 1717 }
 1718 
 1719 WL_EXPORT void
 1720 wl_resource_set_implementation(struct wl_resource *resource,
 1721                    const void *implementation,
 1722                    void *data, wl_resource_destroy_func_t destroy)
 1723 {
 1724     resource->object.implementation = implementation;
 1725     resource->data = data;
 1726     resource->destroy = destroy;
 1727     resource->dispatcher = NULL;
 1728 }
 1729 
 1730 WL_EXPORT void
 1731 wl_resource_set_dispatcher(struct wl_resource *resource,
 1732                wl_dispatcher_func_t dispatcher,
 1733                const void *implementation,
 1734                void *data, wl_resource_destroy_func_t destroy)
 1735 {
 1736     resource->dispatcher = dispatcher;
 1737     resource->object.implementation = implementation;
 1738     resource->data = data;
 1739     resource->destroy = destroy;
 1740 }
 1741 
 1742 /** Create a new resource object
 1743  *
 1744  * \param client The client owner of the new resource.
 1745  * \param interface The interface of the new resource.
 1746  * \param version The version of the new resource.
 1747  * \param id The id of the new resource. If 0, an available id will be used.
 1748  *
 1749  * Listeners added with \a wl_client_add_resource_created_listener will be
 1750  * notified at the end of this function.
 1751  *
 1752  * \memberof wl_resource
 1753  */
 1754 WL_EXPORT struct wl_resource *
 1755 wl_resource_create(struct wl_client *client,
 1756            const struct wl_interface *interface,
 1757            int version, uint32_t id)
 1758 {
 1759     struct wl_resource *resource;
 1760 
 1761     resource = malloc(sizeof *resource);
 1762     if (resource == NULL)
 1763         return NULL;
 1764 
 1765     if (id == 0)
 1766         id = wl_map_insert_new(&client->objects, 0, NULL);
 1767 
 1768     resource->object.id = id;
 1769     resource->object.interface = interface;
 1770     resource->object.implementation = NULL;
 1771 
 1772     wl_signal_init(&resource->deprecated_destroy_signal);
 1773     wl_priv_signal_init(&resource->destroy_signal);
 1774 
 1775     resource->destroy = NULL;
 1776     resource->client = client;
 1777     resource->data = NULL;
 1778     resource->version = version;
 1779     resource->dispatcher = NULL;
 1780 
 1781     if (wl_map_insert_at(&client->objects, 0, id, resource) < 0) {
 1782         wl_resource_post_error(client->display_resource,
 1783                        WL_DISPLAY_ERROR_INVALID_OBJECT,
 1784                        "invalid new id %d", id);
 1785         free(resource);
 1786         return NULL;
 1787     }
 1788 
 1789     wl_priv_signal_emit(&client->resource_created_signal, resource);
 1790     return resource;
 1791 }
 1792 
 1793 WL_EXPORT void
 1794 wl_log_set_handler_server(wl_log_func_t handler)
 1795 {
 1796     wl_log_handler = handler;
 1797 }
 1798 
 1799 /** Adds a new protocol logger.
 1800  *
 1801  * When a new protocol message arrives or is sent from the server
 1802  * all the protocol logger functions will be called, carrying the
 1803  * \a user_data pointer, the type of the message (request or
 1804  * event) and the actual message.
 1805  * The lifetime of the messages passed to the logger function ends
 1806  * when they return so the messages cannot be stored and accessed
 1807  * later.
 1808  *
 1809  * \a errno is set on error.
 1810  *
 1811  * \param display The display object
 1812  * \param func The function to call to log a new protocol message
 1813  * \param user_data The user data pointer to pass to \a func
 1814  *
 1815  * \return The protol logger object on success, NULL on failure.
 1816  *
 1817  * \sa wl_protocol_logger_destroy
 1818  *
 1819  * \memberof wl_display
 1820  */
 1821 WL_EXPORT struct wl_protocol_logger *
 1822 wl_display_add_protocol_logger(struct wl_display *display,
 1823                    wl_protocol_logger_func_t func, void *user_data)
 1824 {
 1825     struct wl_protocol_logger *logger;
 1826 
 1827     logger = malloc(sizeof *logger);
 1828     if (!logger)
 1829         return NULL;
 1830 
 1831     logger->func = func;
 1832     logger->user_data = user_data;
 1833     wl_list_insert(&display->protocol_loggers, &logger->link);
 1834 
 1835     return logger;
 1836 }
 1837 
 1838 /** Destroys a protocol logger.
 1839  *
 1840  * This function destroys a protocol logger and removes it from the display
 1841  * it was added to with \a wl_display_add_protocol_logger.
 1842  * The \a logger object becomes invalid after calling this function.
 1843  *
 1844  * \sa wl_display_add_protocol_logger
 1845  *
 1846  * \memberof wl_protocol_logger
 1847  */
 1848 WL_EXPORT void
 1849 wl_protocol_logger_destroy(struct wl_protocol_logger *logger)
 1850 {
 1851     wl_list_remove(&logger->link);
 1852     free(logger);
 1853 }
 1854 
 1855 /** Add support for a wl_shm pixel format
 1856  *
 1857  * \param display The display object
 1858  * \param format The wl_shm pixel format to advertise
 1859  * \return A pointer to the wl_shm format that was added to the list
 1860  * or NULL if adding it to the list failed.
 1861  *
 1862  * Add the specified wl_shm format to the list of formats the wl_shm
 1863  * object advertises when a client binds to it.  Adding a format to
 1864  * the list means that clients will know that the compositor supports
 1865  * this format and may use it for creating wl_shm buffers.  The
 1866  * compositor must be able to handle the pixel format when a client
 1867  * requests it.
 1868  *
 1869  * The compositor by default supports WL_SHM_FORMAT_ARGB8888 and
 1870  * WL_SHM_FORMAT_XRGB8888.
 1871  *
 1872  * \memberof wl_display
 1873  */
 1874 WL_EXPORT uint32_t *
 1875 wl_display_add_shm_format(struct wl_display *display, uint32_t format)
 1876 {
 1877     uint32_t *p = NULL;
 1878 
 1879     p = wl_array_add(&display->additional_shm_formats, sizeof *p);
 1880 
 1881     if (p != NULL)
 1882         *p = format;
 1883     return p;
 1884 }
 1885 
 1886 /**
 1887  * Get list of additional wl_shm pixel formats
 1888  *
 1889  * \param display The display object
 1890  *
 1891  * This function returns the list of addition wl_shm pixel formats
 1892  * that the compositor supports.  WL_SHM_FORMAT_ARGB8888 and
 1893  * WL_SHM_FORMAT_XRGB8888 are always supported and not included in the
 1894  * array, but all formats added through wl_display_add_shm_format()
 1895  * will be in the array.
 1896  *
 1897  * \sa wl_display_add_shm_format()
 1898  *
 1899  * \private
 1900  *
 1901  * \memberof wl_display
 1902  */
 1903 struct wl_array *
 1904 wl_display_get_additional_shm_formats(struct wl_display *display)
 1905 {
 1906     return &display->additional_shm_formats;
 1907 }
 1908 
 1909 /** Get the list of currently connected clients
 1910  *
 1911  * \param display The display object
 1912  *
 1913  * This function returns a pointer to the list of clients currently
 1914  * connected to the display. You can iterate on the list by using
 1915  * the \a wl_client_for_each macro.
 1916  * The returned value is valid for the lifetime of the \a display.
 1917  * You must not modify the returned list, but only access it.
 1918  *
 1919  * \sa wl_client_for_each()
 1920  * \sa wl_client_get_link()
 1921  * \sa wl_client_from_link()
 1922  *
 1923  * \memberof wl_display
 1924  */
 1925 WL_EXPORT struct wl_list *
 1926 wl_display_get_client_list(struct wl_display *display)
 1927 {
 1928     return &display->client_list;
 1929 }
 1930 
 1931 /** Get the link by which a client is inserted in the client list
 1932  *
 1933  * \param client The client object
 1934  *
 1935  * \sa wl_client_for_each()
 1936  * \sa wl_display_get_client_list()
 1937  * \sa wl_client_from_link()
 1938  *
 1939  * \memberof wl_client
 1940  */
 1941 WL_EXPORT struct wl_list *
 1942 wl_client_get_link(struct wl_client *client)
 1943 {
 1944     return &client->link;
 1945 }
 1946 
 1947 /** Get a wl_client by its link
 1948  *
 1949  * \param link The link of a wl_client
 1950  *
 1951  * \sa wl_client_for_each()
 1952  * \sa wl_display_get_client_list()
 1953  * \sa wl_client_get_link()
 1954  *
 1955  * \memberof wl_client
 1956  */
 1957 WL_EXPORT struct wl_client *
 1958 wl_client_from_link(struct wl_list *link)
 1959 {
 1960     struct wl_client *client;
 1961 
 1962     return wl_container_of(link, client, link);
 1963 }
 1964 
 1965 /** Add a listener for the client's resource creation signal
 1966  *
 1967  * \param client The client object
 1968  * \param listener The listener to be added
 1969  *
 1970  * When a new resource is created for this client the listener
 1971  * will be notified, carrying the new resource as the data argument.
 1972  *
 1973  * \memberof wl_client
 1974  */
 1975 WL_EXPORT void
 1976 wl_client_add_resource_created_listener(struct wl_client *client,
 1977                     struct wl_listener *listener)
 1978 {
 1979     wl_priv_signal_add(&client->resource_created_signal, listener);
 1980 }
 1981 
 1982 struct wl_resource_iterator_context {
 1983     void *user_data;
 1984     wl_client_for_each_resource_iterator_func_t it;
 1985 };
 1986 
 1987 static enum wl_iterator_result
 1988 resource_iterator_helper(void *res, void *user_data, uint32_t flags)
 1989 {
 1990     struct wl_resource_iterator_context *context = user_data;
 1991     struct wl_resource *resource = res;
 1992 
 1993     return context->it(resource, context->user_data);
 1994 }
 1995 
 1996 /** Iterate over all the resources of a client
 1997  *
 1998  * \param client The client object
 1999  * \param iterator The iterator function
 2000  * \param user_data The user data pointer
 2001  *
 2002  * The function pointed by \a iterator will be called for each
 2003  * resource owned by the client. The \a user_data will be passed
 2004  * as the second argument of the iterator function.
 2005  * If the \a iterator function returns \a WL_ITERATOR_CONTINUE the iteration
 2006  * will continue, if it returns \a WL_ITERATOR_STOP it will stop.
 2007  *
 2008  * Creating and destroying resources while iterating is safe, but new
 2009  * resources may or may not be picked up by the iterator.
 2010  *
 2011  * \sa wl_iterator_result
 2012  *
 2013  * \memberof wl_client
 2014  */
 2015 WL_EXPORT void
 2016 wl_client_for_each_resource(struct wl_client *client,
 2017                 wl_client_for_each_resource_iterator_func_t iterator,
 2018                 void *user_data)
 2019 {
 2020     struct wl_resource_iterator_context context = {
 2021         .user_data = user_data,
 2022         .it = iterator,
 2023     };
 2024 
 2025     wl_map_for_each(&client->objects, resource_iterator_helper, &context);
 2026 }
 2027 
 2028 /** \cond INTERNAL */
 2029 
 2030 /** Initialize a wl_priv_signal object
 2031  *
 2032  * wl_priv_signal is a safer implementation of a signal type, with the same API
 2033  * as wl_signal, but kept as a private utility of libwayland-server.
 2034  * It is safer because listeners can be removed from within wl_priv_signal_emit()
 2035  * without corrupting the signal's list.
 2036  *
 2037  * Before passing a wl_priv_signal object to any other function it must be
 2038  * initialized by useing wl_priv_signal_init().
 2039  *
 2040  * \memberof wl_priv_signal
 2041  */
 2042 void
 2043 wl_priv_signal_init(struct wl_priv_signal *signal)
 2044 {
 2045     wl_list_init(&signal->listener_list);
 2046     wl_list_init(&signal->emit_list);
 2047 }
 2048 
 2049 /** Add a listener to a signal
 2050  *
 2051  * The new listener will be called when calling wl_signal_emit(). If a listener is
 2052  * added to the signal while wl_signal_emit() is running it will be called from
 2053  * the next time wl_priv_signal_emit() is called.
 2054  * To remove a listener call wl_list_remove() on its link member.
 2055  *
 2056  * \memberof wl_priv_signal
 2057  */
 2058 void
 2059 wl_priv_signal_add(struct wl_priv_signal *signal, struct wl_listener *listener)
 2060 {
 2061     wl_list_insert(signal->listener_list.prev, &listener->link);
 2062 }
 2063 
 2064 /** Get a listener added to a signal
 2065  *
 2066  * Returns the listener added to the given \a signal and with the given
 2067  * \a notify function, or NULL if there isn't any.
 2068  * Calling this function from withing wl_priv_signal_emit() is safe and will
 2069  * return the correct value.
 2070  *
 2071  * \memberof wl_priv_signal
 2072  */
 2073 struct wl_listener *
 2074 wl_priv_signal_get(struct wl_priv_signal *signal, wl_notify_func_t notify)
 2075 {
 2076     struct wl_listener *l;
 2077 
 2078     wl_list_for_each(l, &signal->listener_list, link)
 2079         if (l->notify == notify)
 2080             return l;
 2081     wl_list_for_each(l, &signal->emit_list, link)
 2082         if (l->notify == notify)
 2083             return l;
 2084 
 2085     return NULL;
 2086 }
 2087 
 2088 /** Emit the signal, calling all the installed listeners
 2089  *
 2090  * Iterate over all the listeners added to this \a signal and call
 2091  * their \a notify function pointer, passing on the given \a data.
 2092  * Removing or adding a listener from within wl_priv_signal_emit()
 2093  * is safe.
 2094  */
 2095 void
 2096 wl_priv_signal_emit(struct wl_priv_signal *signal, void *data)
 2097 {
 2098     struct wl_listener *l;
 2099     struct wl_list *pos;
 2100 
 2101     wl_list_insert_list(&signal->emit_list, &signal->listener_list);
 2102     wl_list_init(&signal->listener_list);
 2103 
 2104     /* Take every element out of the list and put them in a temporary list.
 2105      * This way, the 'it' func can remove any element it wants from the list
 2106      * without troubles, because we always get the first element, not the
 2107      * one after the current, which may be invalid.
 2108      * wl_list_for_each_safe tries to be safe but it fails: it works fine
 2109      * if the current item is removed, but not if the next one is. */
 2110     while (!wl_list_empty(&signal->emit_list)) {
 2111         pos = signal->emit_list.next;
 2112         l = wl_container_of(pos, l, link);
 2113 
 2114         wl_list_remove(pos);
 2115         wl_list_insert(&signal->listener_list, pos);
 2116 
 2117         l->notify(l, data);
 2118     }
 2119 }
 2120 
 2121 /** Emit the signal for the last time, calling all the installed listeners
 2122  *
 2123  * Iterate over all the listeners added to this \a signal and call
 2124  * their \a notify function pointer, passing on the given \a data.
 2125  * Removing or adding a listener from within wl_priv_signal_emit()
 2126  * is safe, as is freeing the structure containing the listener.
 2127  *
 2128  * A large body of external code assumes it's ok to free a destruction
 2129  * listener without removing that listener from the list.  Mixing code
 2130  * that acts like this and code that doesn't will result in list
 2131  * corruption.
 2132  *
 2133  * We resolve this by removing each item from the list and isolating it
 2134  * in another list.  We discard it completely after firing the notifier.
 2135  * This should allow interoperability between code that unlinks its
 2136  * destruction listeners and code that just frees structures they're in.
 2137  *
 2138  */
 2139 void
 2140 wl_priv_signal_final_emit(struct wl_priv_signal *signal, void *data)
 2141 {
 2142     struct wl_listener *l;
 2143     struct wl_list *pos;
 2144 
 2145     /* During a destructor notifier isolate every list item before
 2146      * notifying.  This renders harmless the long standing misuse
 2147      * of freeing listeners without removing them, but allows
 2148      * callers that do choose to remove them to interoperate with
 2149      * ones that don't. */
 2150     while (!wl_list_empty(&signal->listener_list)) {
 2151         pos = signal->listener_list.next;
 2152         l = wl_container_of(pos, l, link);
 2153 
 2154         wl_list_remove(pos);
 2155         wl_list_init(pos);
 2156 
 2157         l->notify(l, data);
 2158     }
 2159 }
 2160 
 2161 /** \endcond INTERNAL */
 2162 
 2163 /** \cond */ /* Deprecated functions below. */
 2164 
 2165 uint32_t
 2166 wl_client_add_resource(struct wl_client *client,
 2167                struct wl_resource *resource) WL_DEPRECATED;
 2168 
 2169 WL_EXPORT uint32_t
 2170 wl_client_add_resource(struct wl_client *client,
 2171                struct wl_resource *resource)
 2172 {
 2173     if (resource->object.id == 0) {
 2174         resource->object.id =
 2175             wl_map_insert_new(&client->objects,
 2176                       WL_MAP_ENTRY_LEGACY, resource);
 2177     } else if (wl_map_insert_at(&client->objects, WL_MAP_ENTRY_LEGACY,
 2178                   resource->object.id, resource) < 0) {
 2179         wl_resource_post_error(client->display_resource,
 2180                        WL_DISPLAY_ERROR_INVALID_OBJECT,
 2181                        "invalid new id %d",
 2182                        resource->object.id);
 2183         return 0;
 2184     }
 2185 
 2186     resource->client = client;
 2187     wl_signal_init(&resource->deprecated_destroy_signal);
 2188 
 2189     return resource->object.id;
 2190 }
 2191 
 2192 struct wl_resource *
 2193 wl_client_add_object(struct wl_client *client,
 2194              const struct wl_interface *interface,
 2195              const void *implementation,
 2196              uint32_t id, void *data) WL_DEPRECATED;
 2197 
 2198 WL_EXPORT struct wl_resource *
 2199 wl_client_add_object(struct wl_client *client,
 2200              const struct wl_interface *interface,
 2201              const void *implementation, uint32_t id, void *data)
 2202 {
 2203     struct wl_resource *resource;
 2204 
 2205     resource = wl_resource_create(client, interface, -1, id);
 2206     if (resource == NULL)
 2207         wl_client_post_no_memory(client);
 2208     else
 2209         wl_resource_set_implementation(resource,
 2210                            implementation, data, NULL);
 2211 
 2212     return resource;
 2213 }
 2214 
 2215 struct wl_resource *
 2216 wl_client_new_object(struct wl_client *client,
 2217              const struct wl_interface *interface,
 2218              const void *implementation, void *data) WL_DEPRECATED;
 2219 
 2220 WL_EXPORT struct wl_resource *
 2221 wl_client_new_object(struct wl_client *client,
 2222              const struct wl_interface *interface,
 2223              const void *implementation, void *data)
 2224 {
 2225     struct wl_resource *resource;
 2226 
 2227     resource = wl_resource_create(client, interface, -1, 0);
 2228     if (resource == NULL)
 2229         wl_client_post_no_memory(client);
 2230     else
 2231         wl_resource_set_implementation(resource,
 2232                            implementation, data, NULL);
 2233 
 2234     return resource;
 2235 }
 2236 
 2237 struct wl_global *
 2238 wl_display_add_global(struct wl_display *display,
 2239               const struct wl_interface *interface,
 2240               void *data, wl_global_bind_func_t bind) WL_DEPRECATED;
 2241 
 2242 WL_EXPORT struct wl_global *
 2243 wl_display_add_global(struct wl_display *display,
 2244               const struct wl_interface *interface,
 2245               void *data, wl_global_bind_func_t bind)
 2246 {
 2247     return wl_global_create(display, interface, interface->version, data, bind);
 2248 }
 2249 
 2250 void
 2251 wl_display_remove_global(struct wl_display *display,
 2252              struct wl_global *global) WL_DEPRECATED;
 2253 
 2254 WL_EXPORT void
 2255 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
 2256 {
 2257     wl_global_destroy(global);
 2258 }
 2259 
 2260 /** \endcond */
 2261 
 2262 /* Functions at the end of this file are deprecated.  Instead of adding new
 2263  * code here, add it before the comment above that states:
 2264  * Deprecated functions below.
 2265  */