Home | History | Annotate | Download | only in dbus
      1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
      2 /* dbus-connection.c DBusConnection object
      3  *
      4  * Copyright (C) 2002-2006  Red Hat Inc.
      5  *
      6  * Licensed under the Academic Free License version 2.1
      7  *
      8  * This program is free software; you can redistribute it and/or modify
      9  * it under the terms of the GNU General Public License as published by
     10  * the Free Software Foundation; either version 2 of the License, or
     11  * (at your option) any later version.
     12  *
     13  * This program is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  * GNU General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU General Public License
     19  * along with this program; if not, write to the Free Software
     20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     21  *
     22  */
     23 
     24 #include <config.h>
     25 #include <signal.h>
     26 #include "dbus-shared.h"
     27 #include "dbus-connection.h"
     28 #include "dbus-list.h"
     29 #include "dbus-timeout.h"
     30 #include "dbus-transport.h"
     31 #include "dbus-watch.h"
     32 #include "dbus-connection-internal.h"
     33 #include "dbus-pending-call-internal.h"
     34 #include "dbus-list.h"
     35 #include "dbus-hash.h"
     36 #include "dbus-message-internal.h"
     37 #include "dbus-message-private.h"
     38 #include "dbus-threads.h"
     39 #include "dbus-protocol.h"
     40 #include "dbus-dataslot.h"
     41 #include "dbus-string.h"
     42 #include "dbus-signature.h"
     43 #include "dbus-pending-call.h"
     44 #include "dbus-object-tree.h"
     45 #include "dbus-threads-internal.h"
     46 #include "dbus-bus.h"
     47 #include "dbus-marshal-basic.h"
     48 
     49 #ifdef DBUS_DISABLE_CHECKS
     50 #define TOOK_LOCK_CHECK(connection)
     51 #define RELEASING_LOCK_CHECK(connection)
     52 #define HAVE_LOCK_CHECK(connection)
     53 #else
     54 #define TOOK_LOCK_CHECK(connection) do {                \
     55     _dbus_assert (!(connection)->have_connection_lock); \
     56     (connection)->have_connection_lock = TRUE;          \
     57   } while (0)
     58 #define RELEASING_LOCK_CHECK(connection) do {            \
     59     _dbus_assert ((connection)->have_connection_lock);   \
     60     (connection)->have_connection_lock = FALSE;          \
     61   } while (0)
     62 #define HAVE_LOCK_CHECK(connection)        _dbus_assert ((connection)->have_connection_lock)
     63 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
     64 #endif
     65 
     66 #define TRACE_LOCKS 0
     67 
     68 #define CONNECTION_LOCK(connection)   do {                                      \
     69     if (TRACE_LOCKS) { _dbus_verbose ("LOCK\n"); }   \
     70     _dbus_rmutex_lock ((connection)->mutex);                                    \
     71     TOOK_LOCK_CHECK (connection);                                               \
     72   } while (0)
     73 
     74 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock (connection)
     75 
     76 #define SLOTS_LOCK(connection) do {                     \
     77     _dbus_rmutex_lock ((connection)->slot_mutex);       \
     78   } while (0)
     79 
     80 #define SLOTS_UNLOCK(connection) do {                   \
     81     _dbus_rmutex_unlock ((connection)->slot_mutex);     \
     82   } while (0)
     83 
     84 #define DISPATCH_STATUS_NAME(s)                                            \
     85                      ((s) == DBUS_DISPATCH_COMPLETE ? "complete" :         \
     86                       (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
     87                       (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :   \
     88                       "???")
     89 
     90 /**
     91  * @defgroup DBusConnection DBusConnection
     92  * @ingroup  DBus
     93  * @brief Connection to another application
     94  *
     95  * A DBusConnection represents a connection to another
     96  * application. Messages can be sent and received via this connection.
     97  * The other application may be a message bus; for convenience, the
     98  * function dbus_bus_get() is provided to automatically open a
     99  * connection to the well-known message buses.
    100  *
    101  * In brief a DBusConnection is a message queue associated with some
    102  * message transport mechanism such as a socket.  The connection
    103  * maintains a queue of incoming messages and a queue of outgoing
    104  * messages.
    105  *
    106  * Several functions use the following terms:
    107  * <ul>
    108  * <li><b>read</b> means to fill the incoming message queue by reading from the socket</li>
    109  * <li><b>write</b> means to drain the outgoing queue by writing to the socket</li>
    110  * <li><b>dispatch</b> means to drain the incoming queue by invoking application-provided message handlers</li>
    111  * </ul>
    112  *
    113  * The function dbus_connection_read_write_dispatch() for example does all
    114  * three of these things, offering a simple alternative to a main loop.
    115  *
    116  * In an application with a main loop, the read/write/dispatch
    117  * operations are usually separate.
    118  *
    119  * The connection provides #DBusWatch and #DBusTimeout objects to
    120  * the main loop. These are used to know when reading, writing, or
    121  * dispatching should be performed.
    122  *
    123  * Incoming messages are processed
    124  * by calling dbus_connection_dispatch(). dbus_connection_dispatch()
    125  * runs any handlers registered for the topmost message in the message
    126  * queue, then discards the message, then returns.
    127  *
    128  * dbus_connection_get_dispatch_status() indicates whether
    129  * messages are currently in the queue that need dispatching.
    130  * dbus_connection_set_dispatch_status_function() allows
    131  * you to set a function to be used to monitor the dispatch status.
    132  *
    133  * If you're using GLib or Qt add-on libraries for D-Bus, there are
    134  * special convenience APIs in those libraries that hide
    135  * all the details of dispatch and watch/timeout monitoring.
    136  * For example, dbus_connection_setup_with_g_main().
    137  *
    138  * If you aren't using these add-on libraries, but want to process
    139  * messages asynchronously, you must manually call
    140  * dbus_connection_set_dispatch_status_function(),
    141  * dbus_connection_set_watch_functions(),
    142  * dbus_connection_set_timeout_functions() providing appropriate
    143  * functions to integrate the connection with your application's main
    144  * loop. This can be tricky to get right; main loops are not simple.
    145  *
    146  * If you don't need to be asynchronous, you can ignore #DBusWatch,
    147  * #DBusTimeout, and dbus_connection_dispatch().  Instead,
    148  * dbus_connection_read_write_dispatch() can be used.
    149  *
    150  * Or, in <em>very</em> simple applications,
    151  * dbus_connection_pop_message() may be all you need, allowing you to
    152  * avoid setting up any handler functions (see
    153  * dbus_connection_add_filter(),
    154  * dbus_connection_register_object_path() for more on handlers).
    155  *
    156  * When you use dbus_connection_send() or one of its variants to send
    157  * a message, the message is added to the outgoing queue.  It's
    158  * actually written to the network later; either in
    159  * dbus_watch_handle() invoked by your main loop, or in
    160  * dbus_connection_flush() which blocks until it can write out the
    161  * entire outgoing queue. The GLib/Qt add-on libraries again
    162  * handle the details here for you by setting up watch functions.
    163  *
    164  * When a connection is disconnected, you are guaranteed to get a
    165  * signal "Disconnected" from the interface
    166  * #DBUS_INTERFACE_LOCAL, path
    167  * #DBUS_PATH_LOCAL.
    168  *
    169  * You may not drop the last reference to a #DBusConnection
    170  * until that connection has been disconnected.
    171  *
    172  * You may dispatch the unprocessed incoming message queue even if the
    173  * connection is disconnected. However, "Disconnected" will always be
    174  * the last message in the queue (obviously no messages are received
    175  * after disconnection).
    176  *
    177  * After calling dbus_threads_init(), #DBusConnection has thread
    178  * locks and drops them when invoking user callbacks, so in general is
    179  * transparently threadsafe. However, #DBusMessage does NOT have
    180  * thread locks; you must not send the same message to multiple
    181  * #DBusConnection if those connections will be used from different threads,
    182  * for example.
    183  *
    184  * Also, if you dispatch or pop messages from multiple threads, it
    185  * may work in the sense that it won't crash, but it's tough to imagine
    186  * sane results; it will be completely unpredictable which messages
    187  * go to which threads.
    188  *
    189  * It's recommended to dispatch from a single thread.
    190  *
    191  * The most useful function to call from multiple threads at once
    192  * is dbus_connection_send_with_reply_and_block(). That is,
    193  * multiple threads can make method calls at the same time.
    194  *
    195  * If you aren't using threads, you can use a main loop and
    196  * dbus_pending_call_set_notify() to achieve a similar result.
    197  */
    198 
    199 /**
    200  * @defgroup DBusConnectionInternals DBusConnection implementation details
    201  * @ingroup  DBusInternals
    202  * @brief Implementation details of DBusConnection
    203  *
    204  * @{
    205  */
    206 
    207 #ifdef DBUS_ENABLE_VERBOSE_MODE
    208 static void
    209 _dbus_connection_trace_ref (DBusConnection *connection,
    210     int old_refcount,
    211     int new_refcount,
    212     const char *why)
    213 {
    214   static int enabled = -1;
    215 
    216   _dbus_trace_ref ("DBusConnection", connection, old_refcount, new_refcount,
    217       why, "DBUS_CONNECTION_TRACE", &enabled);
    218 }
    219 #else
    220 #define _dbus_connection_trace_ref(c,o,n,w) \
    221   do \
    222   {\
    223     (void) (o); \
    224     (void) (n); \
    225   } while (0)
    226 #endif
    227 
    228 /**
    229  * Internal struct representing a message filter function
    230  */
    231 typedef struct DBusMessageFilter DBusMessageFilter;
    232 
    233 /**
    234  * Internal struct representing a message filter function
    235  */
    236 struct DBusMessageFilter
    237 {
    238   DBusAtomic refcount; /**< Reference count */
    239   DBusHandleMessageFunction function; /**< Function to call to filter */
    240   void *user_data; /**< User data for the function */
    241   DBusFreeFunction free_user_data_function; /**< Function to free the user data */
    242 };
    243 
    244 
    245 /**
    246  * Internals of DBusPreallocatedSend
    247  */
    248 struct DBusPreallocatedSend
    249 {
    250   DBusConnection *connection; /**< Connection we'd send the message to */
    251   DBusList *queue_link;       /**< Preallocated link in the queue */
    252   DBusList *counter_link;     /**< Preallocated link in the resource counter */
    253 };
    254 
    255 #if HAVE_DECL_MSG_NOSIGNAL
    256 static dbus_bool_t _dbus_modify_sigpipe = FALSE;
    257 #else
    258 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
    259 #endif
    260 
    261 /**
    262  * Implementation details of DBusConnection. All fields are private.
    263  */
    264 struct DBusConnection
    265 {
    266   DBusAtomic refcount; /**< Reference count. */
    267 
    268   DBusRMutex *mutex; /**< Lock on the entire DBusConnection */
    269 
    270   DBusCMutex *dispatch_mutex;     /**< Protects dispatch_acquired */
    271   DBusCondVar *dispatch_cond;    /**< Notify when dispatch_acquired is available */
    272   DBusCMutex *io_path_mutex;      /**< Protects io_path_acquired */
    273   DBusCondVar *io_path_cond;     /**< Notify when io_path_acquired is available */
    274 
    275   DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
    276   DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
    277   DBusList *expired_messages;  /**< Messages that will be released when we next unlock. */
    278 
    279   DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
    280                                   *   dispatch_acquired will be set by the borrower
    281                                   */
    282 
    283   int n_outgoing;              /**< Length of outgoing queue. */
    284   int n_incoming;              /**< Length of incoming queue. */
    285 
    286   DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
    287 
    288   DBusTransport *transport;    /**< Object that sends/receives messages over network. */
    289   DBusWatchList *watches;      /**< Stores active watches. */
    290   DBusTimeoutList *timeouts;   /**< Stores active timeouts. */
    291 
    292   DBusList *filter_list;        /**< List of filters. */
    293 
    294   DBusRMutex *slot_mutex;        /**< Lock on slot_list so overall connection lock need not be taken */
    295   DBusDataSlotList slot_list;   /**< Data stored by allocated integer ID */
    296 
    297   DBusHashTable *pending_replies;  /**< Hash of message serials to #DBusPendingCall. */
    298 
    299   dbus_uint32_t client_serial;       /**< Client serial. Increments each time a message is sent  */
    300   DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
    301 
    302   DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop  */
    303   void *wakeup_main_data; /**< Application data for wakeup_main_function */
    304   DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
    305 
    306   DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes  */
    307   void *dispatch_status_data; /**< Application data for dispatch_status_function */
    308   DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
    309 
    310   DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
    311 
    312   DBusObjectTree *objects; /**< Object path handlers registered with this connection */
    313 
    314   char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
    315 
    316   /* These two MUST be bools and not bitfields, because they are protected by a separate lock
    317    * from connection->mutex and all bitfields in a word have to be read/written together.
    318    * So you can't have a different lock for different bitfields in the same word.
    319    */
    320   dbus_bool_t dispatch_acquired; /**< Someone has dispatch path (can drain incoming queue) */
    321   dbus_bool_t io_path_acquired;  /**< Someone has transport io path (can use the transport to read/write messages) */
    322 
    323   unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */
    324 
    325   unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
    326 
    327   unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */
    328 
    329   unsigned int disconnected_message_arrived : 1;   /**< We popped or are dispatching the disconnected message.
    330                                                     * if the disconnect_message_link is NULL then we queued it, but
    331                                                     * this flag is whether it got to the head of the queue.
    332                                                     */
    333   unsigned int disconnected_message_processed : 1; /**< We did our default handling of the disconnected message,
    334                                                     * such as closing the connection.
    335                                                     */
    336 
    337 #ifndef DBUS_DISABLE_CHECKS
    338   unsigned int have_connection_lock : 1; /**< Used to check locking */
    339 #endif
    340 
    341 #ifndef DBUS_DISABLE_CHECKS
    342   int generation; /**< _dbus_current_generation that should correspond to this connection */
    343 #endif
    344 };
    345 
    346 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked      (DBusConnection     *connection);
    347 static void               _dbus_connection_update_dispatch_status_and_unlock (DBusConnection     *connection,
    348                                                                               DBusDispatchStatus  new_status);
    349 static void               _dbus_connection_last_unref                        (DBusConnection     *connection);
    350 static void               _dbus_connection_acquire_dispatch                  (DBusConnection     *connection);
    351 static void               _dbus_connection_release_dispatch                  (DBusConnection     *connection);
    352 static DBusDispatchStatus _dbus_connection_flush_unlocked                    (DBusConnection     *connection);
    353 static void               _dbus_connection_close_possibly_shared_and_unlock  (DBusConnection     *connection);
    354 static dbus_bool_t        _dbus_connection_get_is_connected_unlocked         (DBusConnection     *connection);
    355 static dbus_bool_t        _dbus_connection_peek_for_reply_unlocked           (DBusConnection     *connection,
    356                                                                               dbus_uint32_t       client_serial);
    357 
    358 static DBusMessageFilter *
    359 _dbus_message_filter_ref (DBusMessageFilter *filter)
    360 {
    361 #ifdef DBUS_DISABLE_ASSERT
    362   _dbus_atomic_inc (&filter->refcount);
    363 #else
    364   dbus_int32_t old_value;
    365 
    366   old_value = _dbus_atomic_inc (&filter->refcount);
    367   _dbus_assert (old_value > 0);
    368 #endif
    369 
    370   return filter;
    371 }
    372 
    373 static void
    374 _dbus_message_filter_unref (DBusMessageFilter *filter)
    375 {
    376   dbus_int32_t old_value;
    377 
    378   old_value = _dbus_atomic_dec (&filter->refcount);
    379   _dbus_assert (old_value > 0);
    380 
    381   if (old_value == 1)
    382     {
    383       if (filter->free_user_data_function)
    384         (* filter->free_user_data_function) (filter->user_data);
    385 
    386       dbus_free (filter);
    387     }
    388 }
    389 
    390 /**
    391  * Acquires the connection lock.
    392  *
    393  * @param connection the connection.
    394  */
    395 void
    396 _dbus_connection_lock (DBusConnection *connection)
    397 {
    398   CONNECTION_LOCK (connection);
    399 }
    400 
    401 /**
    402  * Releases the connection lock.
    403  *
    404  * @param connection the connection.
    405  */
    406 void
    407 _dbus_connection_unlock (DBusConnection *connection)
    408 {
    409   DBusList *expired_messages;
    410   DBusList *iter;
    411 
    412   if (TRACE_LOCKS)
    413     {
    414       _dbus_verbose ("UNLOCK\n");
    415     }
    416 
    417   /* If we had messages that expired (fell off the incoming or outgoing
    418    * queues) while we were locked, actually release them now */
    419   expired_messages = connection->expired_messages;
    420   connection->expired_messages = NULL;
    421 
    422   RELEASING_LOCK_CHECK (connection);
    423   _dbus_rmutex_unlock (connection->mutex);
    424 
    425   for (iter = _dbus_list_pop_first_link (&expired_messages);
    426       iter != NULL;
    427       iter = _dbus_list_pop_first_link (&expired_messages))
    428     {
    429       DBusMessage *message = iter->data;
    430 
    431       dbus_message_unref (message);
    432       _dbus_list_free_link (iter);
    433     }
    434 }
    435 
    436 /**
    437  * Wakes up the main loop if it is sleeping
    438  * Needed if we're e.g. queueing outgoing messages
    439  * on a thread while the mainloop sleeps.
    440  *
    441  * @param connection the connection.
    442  */
    443 static void
    444 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
    445 {
    446   if (connection->wakeup_main_function)
    447     (*connection->wakeup_main_function) (connection->wakeup_main_data);
    448 }
    449 
    450 #ifdef DBUS_BUILD_TESTS
    451 /**
    452  * Gets the locks so we can examine them
    453  *
    454  * @param connection the connection.
    455  * @param mutex_loc return for the location of the main mutex pointer
    456  * @param dispatch_mutex_loc return location of the dispatch mutex pointer
    457  * @param io_path_mutex_loc return location of the io_path mutex pointer
    458  * @param dispatch_cond_loc return location of the dispatch conditional
    459  *        variable pointer
    460  * @param io_path_cond_loc return location of the io_path conditional
    461  *        variable pointer
    462  */
    463 void
    464 _dbus_connection_test_get_locks (DBusConnection *connection,
    465                                  DBusMutex     **mutex_loc,
    466                                  DBusMutex     **dispatch_mutex_loc,
    467                                  DBusMutex     **io_path_mutex_loc,
    468                                  DBusCondVar   **dispatch_cond_loc,
    469                                  DBusCondVar   **io_path_cond_loc)
    470 {
    471   *mutex_loc = (DBusMutex *) connection->mutex;
    472   *dispatch_mutex_loc = (DBusMutex *) connection->dispatch_mutex;
    473   *io_path_mutex_loc = (DBusMutex *) connection->io_path_mutex;
    474   *dispatch_cond_loc = connection->dispatch_cond;
    475   *io_path_cond_loc = connection->io_path_cond;
    476 }
    477 #endif
    478 
    479 /**
    480  * Adds a message-containing list link to the incoming message queue,
    481  * taking ownership of the link and the message's current refcount.
    482  * Cannot fail due to lack of memory.
    483  *
    484  * @param connection the connection.
    485  * @param link the message link to queue.
    486  */
    487 void
    488 _dbus_connection_queue_received_message_link (DBusConnection  *connection,
    489                                               DBusList        *link)
    490 {
    491   DBusPendingCall *pending;
    492   dbus_uint32_t reply_serial;
    493   DBusMessage *message;
    494 
    495   _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
    496 
    497   _dbus_list_append_link (&connection->incoming_messages,
    498                           link);
    499   message = link->data;
    500 
    501   /* If this is a reply we're waiting on, remove timeout for it */
    502   reply_serial = dbus_message_get_reply_serial (message);
    503   if (reply_serial != 0)
    504     {
    505       pending = _dbus_hash_table_lookup_int (connection->pending_replies,
    506                                              reply_serial);
    507       if (pending != NULL)
    508 	{
    509 	  if (_dbus_pending_call_is_timeout_added_unlocked (pending))
    510             _dbus_connection_remove_timeout_unlocked (connection,
    511                                                       _dbus_pending_call_get_timeout_unlocked (pending));
    512 
    513 	  _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
    514 	}
    515     }
    516 
    517 
    518 
    519   connection->n_incoming += 1;
    520 
    521   _dbus_connection_wakeup_mainloop (connection);
    522 
    523   _dbus_verbose ("Message %p (%s %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
    524                  message,
    525                  dbus_message_type_to_string (dbus_message_get_type (message)),
    526                  dbus_message_get_path (message) ?
    527                  dbus_message_get_path (message) :
    528                  "no path",
    529                  dbus_message_get_interface (message) ?
    530                  dbus_message_get_interface (message) :
    531                  "no interface",
    532                  dbus_message_get_member (message) ?
    533                  dbus_message_get_member (message) :
    534                  "no member",
    535                  dbus_message_get_signature (message),
    536                  dbus_message_get_reply_serial (message),
    537                  connection,
    538                  connection->n_incoming);
    539 
    540   _dbus_message_trace_ref (message, -1, -1,
    541       "_dbus_conection_queue_received_message_link");
    542 }
    543 
    544 /**
    545  * Adds a link + message to the incoming message queue.
    546  * Can't fail. Takes ownership of both link and message.
    547  *
    548  * @param connection the connection.
    549  * @param link the list node and message to queue.
    550  *
    551  */
    552 void
    553 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
    554 						 DBusList *link)
    555 {
    556   HAVE_LOCK_CHECK (connection);
    557 
    558   _dbus_list_append_link (&connection->incoming_messages, link);
    559 
    560   connection->n_incoming += 1;
    561 
    562   _dbus_connection_wakeup_mainloop (connection);
    563 
    564   _dbus_message_trace_ref (link->data, -1, -1,
    565       "_dbus_connection_queue_synthesized_message_link");
    566 
    567   _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
    568                  link->data, connection, connection->n_incoming);
    569 }
    570 
    571 
    572 /**
    573  * Checks whether there are messages in the outgoing message queue.
    574  * Called with connection lock held.
    575  *
    576  * @param connection the connection.
    577  * @returns #TRUE if the outgoing queue is non-empty.
    578  */
    579 dbus_bool_t
    580 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
    581 {
    582   HAVE_LOCK_CHECK (connection);
    583   return connection->outgoing_messages != NULL;
    584 }
    585 
    586 /**
    587  * Checks whether there are messages in the outgoing message queue.
    588  * Use dbus_connection_flush() to block until all outgoing
    589  * messages have been written to the underlying transport
    590  * (such as a socket).
    591  *
    592  * @param connection the connection.
    593  * @returns #TRUE if the outgoing queue is non-empty.
    594  */
    595 dbus_bool_t
    596 dbus_connection_has_messages_to_send (DBusConnection *connection)
    597 {
    598   dbus_bool_t v;
    599 
    600   _dbus_return_val_if_fail (connection != NULL, FALSE);
    601 
    602   CONNECTION_LOCK (connection);
    603   v = _dbus_connection_has_messages_to_send_unlocked (connection);
    604   CONNECTION_UNLOCK (connection);
    605 
    606   return v;
    607 }
    608 
    609 /**
    610  * Gets the next outgoing message. The message remains in the
    611  * queue, and the caller does not own a reference to it.
    612  *
    613  * @param connection the connection.
    614  * @returns the message to be sent.
    615  */
    616 DBusMessage*
    617 _dbus_connection_get_message_to_send (DBusConnection *connection)
    618 {
    619   HAVE_LOCK_CHECK (connection);
    620 
    621   return _dbus_list_get_last (&connection->outgoing_messages);
    622 }
    623 
    624 /**
    625  * Notifies the connection that a message has been sent, so the
    626  * message can be removed from the outgoing queue.
    627  * Called with the connection lock held.
    628  *
    629  * @param connection the connection.
    630  * @param message the message that was sent.
    631  */
    632 void
    633 _dbus_connection_message_sent_unlocked (DBusConnection *connection,
    634                                         DBusMessage    *message)
    635 {
    636   DBusList *link;
    637 
    638   HAVE_LOCK_CHECK (connection);
    639 
    640   /* This can be called before we even complete authentication, since
    641    * it's called on disconnect to clean up the outgoing queue.
    642    * It's also called as we successfully send each message.
    643    */
    644 
    645   link = _dbus_list_get_last_link (&connection->outgoing_messages);
    646   _dbus_assert (link != NULL);
    647   _dbus_assert (link->data == message);
    648 
    649   _dbus_list_unlink (&connection->outgoing_messages,
    650                      link);
    651   _dbus_list_prepend_link (&connection->expired_messages, link);
    652 
    653   connection->n_outgoing -= 1;
    654 
    655   _dbus_verbose ("Message %p (%s %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
    656                  message,
    657                  dbus_message_type_to_string (dbus_message_get_type (message)),
    658                  dbus_message_get_path (message) ?
    659                  dbus_message_get_path (message) :
    660                  "no path",
    661                  dbus_message_get_interface (message) ?
    662                  dbus_message_get_interface (message) :
    663                  "no interface",
    664                  dbus_message_get_member (message) ?
    665                  dbus_message_get_member (message) :
    666                  "no member",
    667                  dbus_message_get_signature (message),
    668                  connection, connection->n_outgoing);
    669 
    670   /* It's OK that in principle we call the notify function, because for the
    671    * outgoing limit, there isn't one */
    672   _dbus_message_remove_counter (message, connection->outgoing_counter);
    673 
    674   /* The message will actually be unreffed when we unlock */
    675 }
    676 
    677 /** Function to be called in protected_change_watch() with refcount held */
    678 typedef dbus_bool_t (* DBusWatchAddFunction)     (DBusWatchList *list,
    679                                                   DBusWatch     *watch);
    680 /** Function to be called in protected_change_watch() with refcount held */
    681 typedef void        (* DBusWatchRemoveFunction)  (DBusWatchList *list,
    682                                                   DBusWatch     *watch);
    683 /** Function to be called in protected_change_watch() with refcount held */
    684 typedef void        (* DBusWatchToggleFunction)  (DBusWatchList *list,
    685                                                   DBusWatch     *watch,
    686                                                   dbus_bool_t    enabled);
    687 
    688 static dbus_bool_t
    689 protected_change_watch (DBusConnection         *connection,
    690                         DBusWatch              *watch,
    691                         DBusWatchAddFunction    add_function,
    692                         DBusWatchRemoveFunction remove_function,
    693                         DBusWatchToggleFunction toggle_function,
    694                         dbus_bool_t             enabled)
    695 {
    696   dbus_bool_t retval;
    697 
    698   HAVE_LOCK_CHECK (connection);
    699 
    700   /* The original purpose of protected_change_watch() was to hold a
    701    * ref on the connection while dropping the connection lock, then
    702    * calling out to the app.  This was a broken hack that did not
    703    * work, since the connection was in a hosed state (no WatchList
    704    * field) while calling out.
    705    *
    706    * So for now we'll just keep the lock while calling out. This means
    707    * apps are not allowed to call DBusConnection methods inside a
    708    * watch function or they will deadlock.
    709    *
    710    * The "real fix" is to use the _and_unlock() pattern found
    711    * elsewhere in the code, to defer calling out to the app until
    712    * we're about to drop locks and return flow of control to the app
    713    * anyway.
    714    *
    715    * See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
    716    */
    717 
    718   if (connection->watches)
    719     {
    720       if (add_function)
    721         retval = (* add_function) (connection->watches, watch);
    722       else if (remove_function)
    723         {
    724           retval = TRUE;
    725           (* remove_function) (connection->watches, watch);
    726         }
    727       else
    728         {
    729           retval = TRUE;
    730           (* toggle_function) (connection->watches, watch, enabled);
    731         }
    732       return retval;
    733     }
    734   else
    735     return FALSE;
    736 }
    737 
    738 
    739 /**
    740  * Adds a watch using the connection's DBusAddWatchFunction if
    741  * available. Otherwise records the watch to be added when said
    742  * function is available. Also re-adds the watch if the
    743  * DBusAddWatchFunction changes. May fail due to lack of memory.
    744  * Connection lock should be held when calling this.
    745  *
    746  * @param connection the connection.
    747  * @param watch the watch to add.
    748  * @returns #TRUE on success.
    749  */
    750 dbus_bool_t
    751 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
    752                                      DBusWatch      *watch)
    753 {
    754   return protected_change_watch (connection, watch,
    755                                  _dbus_watch_list_add_watch,
    756                                  NULL, NULL, FALSE);
    757 }
    758 
    759 /**
    760  * Removes a watch using the connection's DBusRemoveWatchFunction
    761  * if available. It's an error to call this function on a watch
    762  * that was not previously added.
    763  * Connection lock should be held when calling this.
    764  *
    765  * @param connection the connection.
    766  * @param watch the watch to remove.
    767  */
    768 void
    769 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
    770                                         DBusWatch      *watch)
    771 {
    772   protected_change_watch (connection, watch,
    773                           NULL,
    774                           _dbus_watch_list_remove_watch,
    775                           NULL, FALSE);
    776 }
    777 
    778 /**
    779  * Toggles a watch and notifies app via connection's
    780  * DBusWatchToggledFunction if available. It's an error to call this
    781  * function on a watch that was not previously added.
    782  * Connection lock should be held when calling this.
    783  *
    784  * @param connection the connection.
    785  * @param watch the watch to toggle.
    786  * @param enabled whether to enable or disable
    787  */
    788 void
    789 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
    790                                         DBusWatch      *watch,
    791                                         dbus_bool_t     enabled)
    792 {
    793   _dbus_assert (watch != NULL);
    794 
    795   protected_change_watch (connection, watch,
    796                           NULL, NULL,
    797                           _dbus_watch_list_toggle_watch,
    798                           enabled);
    799 }
    800 
    801 /** Function to be called in protected_change_timeout() with refcount held */
    802 typedef dbus_bool_t (* DBusTimeoutAddFunction)    (DBusTimeoutList *list,
    803                                                    DBusTimeout     *timeout);
    804 /** Function to be called in protected_change_timeout() with refcount held */
    805 typedef void        (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
    806                                                    DBusTimeout     *timeout);
    807 /** Function to be called in protected_change_timeout() with refcount held */
    808 typedef void        (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
    809                                                    DBusTimeout     *timeout,
    810                                                    dbus_bool_t      enabled);
    811 
    812 static dbus_bool_t
    813 protected_change_timeout (DBusConnection           *connection,
    814                           DBusTimeout              *timeout,
    815                           DBusTimeoutAddFunction    add_function,
    816                           DBusTimeoutRemoveFunction remove_function,
    817                           DBusTimeoutToggleFunction toggle_function,
    818                           dbus_bool_t               enabled)
    819 {
    820   dbus_bool_t retval;
    821 
    822   HAVE_LOCK_CHECK (connection);
    823 
    824   /* The original purpose of protected_change_timeout() was to hold a
    825    * ref on the connection while dropping the connection lock, then
    826    * calling out to the app.  This was a broken hack that did not
    827    * work, since the connection was in a hosed state (no TimeoutList
    828    * field) while calling out.
    829    *
    830    * So for now we'll just keep the lock while calling out. This means
    831    * apps are not allowed to call DBusConnection methods inside a
    832    * timeout function or they will deadlock.
    833    *
    834    * The "real fix" is to use the _and_unlock() pattern found
    835    * elsewhere in the code, to defer calling out to the app until
    836    * we're about to drop locks and return flow of control to the app
    837    * anyway.
    838    *
    839    * See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
    840    */
    841 
    842   if (connection->timeouts)
    843     {
    844       if (add_function)
    845         retval = (* add_function) (connection->timeouts, timeout);
    846       else if (remove_function)
    847         {
    848           retval = TRUE;
    849           (* remove_function) (connection->timeouts, timeout);
    850         }
    851       else
    852         {
    853           retval = TRUE;
    854           (* toggle_function) (connection->timeouts, timeout, enabled);
    855         }
    856       return retval;
    857     }
    858   else
    859     return FALSE;
    860 }
    861 
    862 /**
    863  * Adds a timeout using the connection's DBusAddTimeoutFunction if
    864  * available. Otherwise records the timeout to be added when said
    865  * function is available. Also re-adds the timeout if the
    866  * DBusAddTimeoutFunction changes. May fail due to lack of memory.
    867  * The timeout will fire repeatedly until removed.
    868  * Connection lock should be held when calling this.
    869  *
    870  * @param connection the connection.
    871  * @param timeout the timeout to add.
    872  * @returns #TRUE on success.
    873  */
    874 dbus_bool_t
    875 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
    876                                        DBusTimeout    *timeout)
    877 {
    878   return protected_change_timeout (connection, timeout,
    879                                    _dbus_timeout_list_add_timeout,
    880                                    NULL, NULL, FALSE);
    881 }
    882 
    883 /**
    884  * Removes a timeout using the connection's DBusRemoveTimeoutFunction
    885  * if available. It's an error to call this function on a timeout
    886  * that was not previously added.
    887  * Connection lock should be held when calling this.
    888  *
    889  * @param connection the connection.
    890  * @param timeout the timeout to remove.
    891  */
    892 void
    893 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
    894                                           DBusTimeout    *timeout)
    895 {
    896   protected_change_timeout (connection, timeout,
    897                             NULL,
    898                             _dbus_timeout_list_remove_timeout,
    899                             NULL, FALSE);
    900 }
    901 
    902 /**
    903  * Toggles a timeout and notifies app via connection's
    904  * DBusTimeoutToggledFunction if available. It's an error to call this
    905  * function on a timeout that was not previously added.
    906  * Connection lock should be held when calling this.
    907  *
    908  * @param connection the connection.
    909  * @param timeout the timeout to toggle.
    910  * @param enabled whether to enable or disable
    911  */
    912 void
    913 _dbus_connection_toggle_timeout_unlocked (DBusConnection   *connection,
    914                                           DBusTimeout      *timeout,
    915                                           dbus_bool_t       enabled)
    916 {
    917   protected_change_timeout (connection, timeout,
    918                             NULL, NULL,
    919                             _dbus_timeout_list_toggle_timeout,
    920                             enabled);
    921 }
    922 
    923 static dbus_bool_t
    924 _dbus_connection_attach_pending_call_unlocked (DBusConnection  *connection,
    925                                                DBusPendingCall *pending)
    926 {
    927   dbus_uint32_t reply_serial;
    928   DBusTimeout *timeout;
    929 
    930   HAVE_LOCK_CHECK (connection);
    931 
    932   reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
    933 
    934   _dbus_assert (reply_serial != 0);
    935 
    936   timeout = _dbus_pending_call_get_timeout_unlocked (pending);
    937 
    938   if (timeout)
    939     {
    940       if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
    941         return FALSE;
    942 
    943       if (!_dbus_hash_table_insert_int (connection->pending_replies,
    944                                         reply_serial,
    945                                         pending))
    946         {
    947           _dbus_connection_remove_timeout_unlocked (connection, timeout);
    948 
    949           _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
    950           HAVE_LOCK_CHECK (connection);
    951           return FALSE;
    952         }
    953 
    954       _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
    955     }
    956   else
    957     {
    958       if (!_dbus_hash_table_insert_int (connection->pending_replies,
    959                                         reply_serial,
    960                                         pending))
    961         {
    962           HAVE_LOCK_CHECK (connection);
    963           return FALSE;
    964         }
    965     }
    966 
    967   _dbus_pending_call_ref_unlocked (pending);
    968 
    969   HAVE_LOCK_CHECK (connection);
    970 
    971   return TRUE;
    972 }
    973 
    974 static void
    975 free_pending_call_on_hash_removal (void *data)
    976 {
    977   DBusPendingCall *pending;
    978   DBusConnection  *connection;
    979 
    980   if (data == NULL)
    981     return;
    982 
    983   pending = data;
    984 
    985   connection = _dbus_pending_call_get_connection_unlocked (pending);
    986 
    987   HAVE_LOCK_CHECK (connection);
    988 
    989   if (_dbus_pending_call_is_timeout_added_unlocked (pending))
    990     {
    991       _dbus_connection_remove_timeout_unlocked (connection,
    992                                                 _dbus_pending_call_get_timeout_unlocked (pending));
    993 
    994       _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
    995     }
    996 
    997   /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
    998    * here, but the pending call finalizer could in principle call out to
    999    * application code so we pretty much have to... some larger code reorg
   1000    * might be needed.
   1001    */
   1002   _dbus_connection_ref_unlocked (connection);
   1003   _dbus_pending_call_unref_and_unlock (pending);
   1004   CONNECTION_LOCK (connection);
   1005   _dbus_connection_unref_unlocked (connection);
   1006 }
   1007 
   1008 static void
   1009 _dbus_connection_detach_pending_call_unlocked (DBusConnection  *connection,
   1010                                                DBusPendingCall *pending)
   1011 {
   1012   /* This ends up unlocking to call the pending call finalizer, which is unexpected to
   1013    * say the least.
   1014    */
   1015   _dbus_hash_table_remove_int (connection->pending_replies,
   1016                                _dbus_pending_call_get_reply_serial_unlocked (pending));
   1017 }
   1018 
   1019 static void
   1020 _dbus_connection_detach_pending_call_and_unlock (DBusConnection  *connection,
   1021                                                  DBusPendingCall *pending)
   1022 {
   1023   /* The idea here is to avoid finalizing the pending call
   1024    * with the lock held, since there's a destroy notifier
   1025    * in pending call that goes out to application code.
   1026    *
   1027    * There's an extra unlock inside the hash table
   1028    * "free pending call" function FIXME...
   1029    */
   1030   _dbus_pending_call_ref_unlocked (pending);
   1031   _dbus_hash_table_remove_int (connection->pending_replies,
   1032                                _dbus_pending_call_get_reply_serial_unlocked (pending));
   1033 
   1034   if (_dbus_pending_call_is_timeout_added_unlocked (pending))
   1035       _dbus_connection_remove_timeout_unlocked (connection,
   1036               _dbus_pending_call_get_timeout_unlocked (pending));
   1037 
   1038   _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
   1039 
   1040   _dbus_pending_call_unref_and_unlock (pending);
   1041 }
   1042 
   1043 /**
   1044  * Removes a pending call from the connection, such that
   1045  * the pending reply will be ignored. May drop the last
   1046  * reference to the pending call.
   1047  *
   1048  * @param connection the connection
   1049  * @param pending the pending call
   1050  */
   1051 void
   1052 _dbus_connection_remove_pending_call (DBusConnection  *connection,
   1053                                       DBusPendingCall *pending)
   1054 {
   1055   CONNECTION_LOCK (connection);
   1056   _dbus_connection_detach_pending_call_and_unlock (connection, pending);
   1057 }
   1058 
   1059 /**
   1060  * Acquire the transporter I/O path. This must be done before
   1061  * doing any I/O in the transporter. May sleep and drop the
   1062  * IO path mutex while waiting for the I/O path.
   1063  *
   1064  * @param connection the connection.
   1065  * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
   1066  * @returns TRUE if the I/O path was acquired.
   1067  */
   1068 static dbus_bool_t
   1069 _dbus_connection_acquire_io_path (DBusConnection *connection,
   1070 				  int             timeout_milliseconds)
   1071 {
   1072   dbus_bool_t we_acquired;
   1073 
   1074   HAVE_LOCK_CHECK (connection);
   1075 
   1076   /* We don't want the connection to vanish */
   1077   _dbus_connection_ref_unlocked (connection);
   1078 
   1079   /* We will only touch io_path_acquired which is protected by our mutex */
   1080   CONNECTION_UNLOCK (connection);
   1081 
   1082   _dbus_verbose ("locking io_path_mutex\n");
   1083   _dbus_cmutex_lock (connection->io_path_mutex);
   1084 
   1085   _dbus_verbose ("start connection->io_path_acquired = %d timeout = %d\n",
   1086                  connection->io_path_acquired, timeout_milliseconds);
   1087 
   1088   we_acquired = FALSE;
   1089 
   1090   if (connection->io_path_acquired)
   1091     {
   1092       if (timeout_milliseconds != -1)
   1093         {
   1094           _dbus_verbose ("waiting %d for IO path to be acquirable\n",
   1095                          timeout_milliseconds);
   1096 
   1097           if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
   1098                                            connection->io_path_mutex,
   1099                                            timeout_milliseconds))
   1100             {
   1101               /* We timed out before anyone signaled. */
   1102               /* (writing the loop to handle the !timedout case by
   1103                * waiting longer if needed is a pain since dbus
   1104                * wraps pthread_cond_timedwait to take a relative
   1105                * time instead of absolute, something kind of stupid
   1106                * on our part. for now it doesn't matter, we will just
   1107                * end up back here eventually.)
   1108                */
   1109             }
   1110         }
   1111       else
   1112         {
   1113           while (connection->io_path_acquired)
   1114             {
   1115               _dbus_verbose ("waiting for IO path to be acquirable\n");
   1116               _dbus_condvar_wait (connection->io_path_cond,
   1117                                   connection->io_path_mutex);
   1118             }
   1119         }
   1120     }
   1121 
   1122   if (!connection->io_path_acquired)
   1123     {
   1124       we_acquired = TRUE;
   1125       connection->io_path_acquired = TRUE;
   1126     }
   1127 
   1128   _dbus_verbose ("end connection->io_path_acquired = %d we_acquired = %d\n",
   1129                  connection->io_path_acquired, we_acquired);
   1130 
   1131   _dbus_verbose ("unlocking io_path_mutex\n");
   1132   _dbus_cmutex_unlock (connection->io_path_mutex);
   1133 
   1134   CONNECTION_LOCK (connection);
   1135 
   1136   HAVE_LOCK_CHECK (connection);
   1137 
   1138   _dbus_connection_unref_unlocked (connection);
   1139 
   1140   return we_acquired;
   1141 }
   1142 
   1143 /**
   1144  * Release the I/O path when you're done with it. Only call
   1145  * after you've acquired the I/O. Wakes up at most one thread
   1146  * currently waiting to acquire the I/O path.
   1147  *
   1148  * @param connection the connection.
   1149  */
   1150 static void
   1151 _dbus_connection_release_io_path (DBusConnection *connection)
   1152 {
   1153   HAVE_LOCK_CHECK (connection);
   1154 
   1155   _dbus_verbose ("locking io_path_mutex\n");
   1156   _dbus_cmutex_lock (connection->io_path_mutex);
   1157 
   1158   _dbus_assert (connection->io_path_acquired);
   1159 
   1160   _dbus_verbose ("start connection->io_path_acquired = %d\n",
   1161                  connection->io_path_acquired);
   1162 
   1163   connection->io_path_acquired = FALSE;
   1164   _dbus_condvar_wake_one (connection->io_path_cond);
   1165 
   1166   _dbus_verbose ("unlocking io_path_mutex\n");
   1167   _dbus_cmutex_unlock (connection->io_path_mutex);
   1168 }
   1169 
   1170 /**
   1171  * Queues incoming messages and sends outgoing messages for this
   1172  * connection, optionally blocking in the process. Each call to
   1173  * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
   1174  * time and then read or write data if possible.
   1175  *
   1176  * The purpose of this function is to be able to flush outgoing
   1177  * messages or queue up incoming messages without returning
   1178  * control to the application and causing reentrancy weirdness.
   1179  *
   1180  * The flags parameter allows you to specify whether to
   1181  * read incoming messages, write outgoing messages, or both,
   1182  * and whether to block if no immediate action is possible.
   1183  *
   1184  * The timeout_milliseconds parameter does nothing unless the
   1185  * iteration is blocking.
   1186  *
   1187  * If there are no outgoing messages and DBUS_ITERATION_DO_READING
   1188  * wasn't specified, then it's impossible to block, even if
   1189  * you specify DBUS_ITERATION_BLOCK; in that case the function
   1190  * returns immediately.
   1191  *
   1192  * If pending is not NULL then a check is made if the pending call
   1193  * is completed after the io path has been required. If the call
   1194  * has been completed nothing is done. This must be done since
   1195  * the _dbus_connection_acquire_io_path releases the connection
   1196  * lock for a while.
   1197  *
   1198  * Called with connection lock held.
   1199  *
   1200  * @param connection the connection.
   1201  * @param pending the pending call that should be checked or NULL
   1202  * @param flags iteration flags.
   1203  * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
   1204  */
   1205 void
   1206 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
   1207                                         DBusPendingCall *pending,
   1208                                         unsigned int    flags,
   1209                                         int             timeout_milliseconds)
   1210 {
   1211   _dbus_verbose ("start\n");
   1212 
   1213   HAVE_LOCK_CHECK (connection);
   1214 
   1215   if (connection->n_outgoing == 0)
   1216     flags &= ~DBUS_ITERATION_DO_WRITING;
   1217 
   1218   if (_dbus_connection_acquire_io_path (connection,
   1219 					(flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
   1220     {
   1221       HAVE_LOCK_CHECK (connection);
   1222 
   1223       if ( (pending != NULL) && _dbus_pending_call_get_completed_unlocked(pending))
   1224         {
   1225           _dbus_verbose ("pending call completed while acquiring I/O path");
   1226         }
   1227       else if ( (pending != NULL) &&
   1228                 _dbus_connection_peek_for_reply_unlocked (connection,
   1229                                                           _dbus_pending_call_get_reply_serial_unlocked (pending)))
   1230         {
   1231           _dbus_verbose ("pending call completed while acquiring I/O path (reply found in queue)");
   1232         }
   1233       else
   1234         {
   1235           _dbus_transport_do_iteration (connection->transport,
   1236                                         flags, timeout_milliseconds);
   1237         }
   1238 
   1239       _dbus_connection_release_io_path (connection);
   1240     }
   1241 
   1242   HAVE_LOCK_CHECK (connection);
   1243 
   1244   _dbus_verbose ("end\n");
   1245 }
   1246 
   1247 /**
   1248  * Creates a new connection for the given transport.  A transport
   1249  * represents a message stream that uses some concrete mechanism, such
   1250  * as UNIX domain sockets. May return #NULL if insufficient
   1251  * memory exists to create the connection.
   1252  *
   1253  * @param transport the transport.
   1254  * @returns the new connection, or #NULL on failure.
   1255  */
   1256 DBusConnection*
   1257 _dbus_connection_new_for_transport (DBusTransport *transport)
   1258 {
   1259   DBusConnection *connection;
   1260   DBusWatchList *watch_list;
   1261   DBusTimeoutList *timeout_list;
   1262   DBusHashTable *pending_replies;
   1263   DBusList *disconnect_link;
   1264   DBusMessage *disconnect_message;
   1265   DBusCounter *outgoing_counter;
   1266   DBusObjectTree *objects;
   1267 
   1268   watch_list = NULL;
   1269   connection = NULL;
   1270   pending_replies = NULL;
   1271   timeout_list = NULL;
   1272   disconnect_link = NULL;
   1273   disconnect_message = NULL;
   1274   outgoing_counter = NULL;
   1275   objects = NULL;
   1276 
   1277   watch_list = _dbus_watch_list_new ();
   1278   if (watch_list == NULL)
   1279     goto error;
   1280 
   1281   timeout_list = _dbus_timeout_list_new ();
   1282   if (timeout_list == NULL)
   1283     goto error;
   1284 
   1285   pending_replies =
   1286     _dbus_hash_table_new (DBUS_HASH_INT,
   1287 			  NULL,
   1288                           (DBusFreeFunction)free_pending_call_on_hash_removal);
   1289   if (pending_replies == NULL)
   1290     goto error;
   1291 
   1292   connection = dbus_new0 (DBusConnection, 1);
   1293   if (connection == NULL)
   1294     goto error;
   1295 
   1296   _dbus_rmutex_new_at_location (&connection->mutex);
   1297   if (connection->mutex == NULL)
   1298     goto error;
   1299 
   1300   _dbus_cmutex_new_at_location (&connection->io_path_mutex);
   1301   if (connection->io_path_mutex == NULL)
   1302     goto error;
   1303 
   1304   _dbus_cmutex_new_at_location (&connection->dispatch_mutex);
   1305   if (connection->dispatch_mutex == NULL)
   1306     goto error;
   1307 
   1308   _dbus_condvar_new_at_location (&connection->dispatch_cond);
   1309   if (connection->dispatch_cond == NULL)
   1310     goto error;
   1311 
   1312   _dbus_condvar_new_at_location (&connection->io_path_cond);
   1313   if (connection->io_path_cond == NULL)
   1314     goto error;
   1315 
   1316   _dbus_rmutex_new_at_location (&connection->slot_mutex);
   1317   if (connection->slot_mutex == NULL)
   1318     goto error;
   1319 
   1320   disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
   1321                                                 DBUS_INTERFACE_LOCAL,
   1322                                                 "Disconnected");
   1323 
   1324   if (disconnect_message == NULL)
   1325     goto error;
   1326 
   1327   disconnect_link = _dbus_list_alloc_link (disconnect_message);
   1328   if (disconnect_link == NULL)
   1329     goto error;
   1330 
   1331   outgoing_counter = _dbus_counter_new ();
   1332   if (outgoing_counter == NULL)
   1333     goto error;
   1334 
   1335   objects = _dbus_object_tree_new (connection);
   1336   if (objects == NULL)
   1337     goto error;
   1338 
   1339   if (_dbus_modify_sigpipe)
   1340     _dbus_disable_sigpipe ();
   1341 
   1342   /* initialized to 0: use atomic op to avoid mixing atomic and non-atomic */
   1343   _dbus_atomic_inc (&connection->refcount);
   1344   connection->transport = transport;
   1345   connection->watches = watch_list;
   1346   connection->timeouts = timeout_list;
   1347   connection->pending_replies = pending_replies;
   1348   connection->outgoing_counter = outgoing_counter;
   1349   connection->filter_list = NULL;
   1350   connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
   1351   connection->objects = objects;
   1352   connection->exit_on_disconnect = FALSE;
   1353   connection->shareable = FALSE;
   1354   connection->route_peer_messages = FALSE;
   1355   connection->disconnected_message_arrived = FALSE;
   1356   connection->disconnected_message_processed = FALSE;
   1357 
   1358 #ifndef DBUS_DISABLE_CHECKS
   1359   connection->generation = _dbus_current_generation;
   1360 #endif
   1361 
   1362   _dbus_data_slot_list_init (&connection->slot_list);
   1363 
   1364   connection->client_serial = 1;
   1365 
   1366   connection->disconnect_message_link = disconnect_link;
   1367 
   1368   CONNECTION_LOCK (connection);
   1369 
   1370   if (!_dbus_transport_set_connection (transport, connection))
   1371     {
   1372       CONNECTION_UNLOCK (connection);
   1373 
   1374       goto error;
   1375     }
   1376 
   1377   _dbus_transport_ref (transport);
   1378 
   1379   CONNECTION_UNLOCK (connection);
   1380 
   1381   _dbus_connection_trace_ref (connection, 0, 1, "new_for_transport");
   1382   return connection;
   1383 
   1384  error:
   1385   if (disconnect_message != NULL)
   1386     dbus_message_unref (disconnect_message);
   1387 
   1388   if (disconnect_link != NULL)
   1389     _dbus_list_free_link (disconnect_link);
   1390 
   1391   if (connection != NULL)
   1392     {
   1393       _dbus_condvar_free_at_location (&connection->io_path_cond);
   1394       _dbus_condvar_free_at_location (&connection->dispatch_cond);
   1395       _dbus_rmutex_free_at_location (&connection->mutex);
   1396       _dbus_cmutex_free_at_location (&connection->io_path_mutex);
   1397       _dbus_cmutex_free_at_location (&connection->dispatch_mutex);
   1398       _dbus_rmutex_free_at_location (&connection->slot_mutex);
   1399       dbus_free (connection);
   1400     }
   1401   if (pending_replies)
   1402     _dbus_hash_table_unref (pending_replies);
   1403 
   1404   if (watch_list)
   1405     _dbus_watch_list_free (watch_list);
   1406 
   1407   if (timeout_list)
   1408     _dbus_timeout_list_free (timeout_list);
   1409 
   1410   if (outgoing_counter)
   1411     _dbus_counter_unref (outgoing_counter);
   1412 
   1413   if (objects)
   1414     _dbus_object_tree_unref (objects);
   1415 
   1416   return NULL;
   1417 }
   1418 
   1419 /**
   1420  * Increments the reference count of a DBusConnection.
   1421  * Requires that the caller already holds the connection lock.
   1422  *
   1423  * @param connection the connection.
   1424  * @returns the connection.
   1425  */
   1426 DBusConnection *
   1427 _dbus_connection_ref_unlocked (DBusConnection *connection)
   1428 {
   1429   dbus_int32_t old_refcount;
   1430 
   1431   _dbus_assert (connection != NULL);
   1432   _dbus_assert (connection->generation == _dbus_current_generation);
   1433 
   1434   HAVE_LOCK_CHECK (connection);
   1435 
   1436   old_refcount = _dbus_atomic_inc (&connection->refcount);
   1437   _dbus_connection_trace_ref (connection, old_refcount, old_refcount + 1,
   1438       "ref_unlocked");
   1439 
   1440   return connection;
   1441 }
   1442 
   1443 /**
   1444  * Decrements the reference count of a DBusConnection.
   1445  * Requires that the caller already holds the connection lock.
   1446  *
   1447  * @param connection the connection.
   1448  */
   1449 void
   1450 _dbus_connection_unref_unlocked (DBusConnection *connection)
   1451 {
   1452   dbus_int32_t old_refcount;
   1453 
   1454   HAVE_LOCK_CHECK (connection);
   1455 
   1456   _dbus_assert (connection != NULL);
   1457 
   1458   old_refcount = _dbus_atomic_dec (&connection->refcount);
   1459 
   1460   _dbus_connection_trace_ref (connection, old_refcount, old_refcount - 1,
   1461       "unref_unlocked");
   1462 
   1463   if (old_refcount == 1)
   1464     _dbus_connection_last_unref (connection);
   1465 }
   1466 
   1467 static dbus_uint32_t
   1468 _dbus_connection_get_next_client_serial (DBusConnection *connection)
   1469 {
   1470   dbus_uint32_t serial;
   1471 
   1472   serial = connection->client_serial++;
   1473 
   1474   if (connection->client_serial == 0)
   1475     connection->client_serial = 1;
   1476 
   1477   return serial;
   1478 }
   1479 
   1480 /**
   1481  * A callback for use with dbus_watch_new() to create a DBusWatch.
   1482  *
   1483  * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
   1484  * and the virtual handle_watch in DBusTransport if we got rid of it.
   1485  * The reason this is some work is threading, see the _dbus_connection_handle_watch()
   1486  * implementation.
   1487  *
   1488  * @param watch the watch.
   1489  * @param condition the current condition of the file descriptors being watched.
   1490  * @param data must be a pointer to a #DBusConnection
   1491  * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
   1492  */
   1493 dbus_bool_t
   1494 _dbus_connection_handle_watch (DBusWatch                   *watch,
   1495                                unsigned int                 condition,
   1496                                void                        *data)
   1497 {
   1498   DBusConnection *connection;
   1499   dbus_bool_t retval;
   1500   DBusDispatchStatus status;
   1501 
   1502   connection = data;
   1503 
   1504   _dbus_verbose ("start\n");
   1505 
   1506   CONNECTION_LOCK (connection);
   1507 
   1508   if (!_dbus_connection_acquire_io_path (connection, 1))
   1509     {
   1510       /* another thread is handling the message */
   1511       CONNECTION_UNLOCK (connection);
   1512       return TRUE;
   1513     }
   1514 
   1515   HAVE_LOCK_CHECK (connection);
   1516   retval = _dbus_transport_handle_watch (connection->transport,
   1517                                          watch, condition);
   1518 
   1519   _dbus_connection_release_io_path (connection);
   1520 
   1521   HAVE_LOCK_CHECK (connection);
   1522 
   1523   _dbus_verbose ("middle\n");
   1524 
   1525   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   1526 
   1527   /* this calls out to user code */
   1528   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   1529 
   1530   _dbus_verbose ("end\n");
   1531 
   1532   return retval;
   1533 }
   1534 
   1535 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
   1536 static DBusHashTable *shared_connections = NULL;
   1537 static DBusList *shared_connections_no_guid = NULL;
   1538 
   1539 static void
   1540 close_connection_on_shutdown (DBusConnection *connection)
   1541 {
   1542   DBusMessage *message;
   1543 
   1544   dbus_connection_ref (connection);
   1545   _dbus_connection_close_possibly_shared (connection);
   1546 
   1547   /* Churn through to the Disconnected message */
   1548   while ((message = dbus_connection_pop_message (connection)))
   1549     {
   1550       dbus_message_unref (message);
   1551     }
   1552   dbus_connection_unref (connection);
   1553 }
   1554 
   1555 static void
   1556 shared_connections_shutdown (void *data)
   1557 {
   1558   int n_entries;
   1559 
   1560   _DBUS_LOCK (shared_connections);
   1561 
   1562   /* This is a little bit unpleasant... better ideas? */
   1563   while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
   1564     {
   1565       DBusConnection *connection;
   1566       DBusHashIter iter;
   1567 
   1568       _dbus_hash_iter_init (shared_connections, &iter);
   1569       _dbus_hash_iter_next (&iter);
   1570 
   1571       connection = _dbus_hash_iter_get_value (&iter);
   1572 
   1573       _DBUS_UNLOCK (shared_connections);
   1574       close_connection_on_shutdown (connection);
   1575       _DBUS_LOCK (shared_connections);
   1576 
   1577       /* The connection should now be dead and not in our hash ... */
   1578       _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
   1579     }
   1580 
   1581   _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
   1582 
   1583   _dbus_hash_table_unref (shared_connections);
   1584   shared_connections = NULL;
   1585 
   1586   if (shared_connections_no_guid != NULL)
   1587     {
   1588       DBusConnection *connection;
   1589       connection = _dbus_list_pop_first (&shared_connections_no_guid);
   1590       while (connection != NULL)
   1591         {
   1592           _DBUS_UNLOCK (shared_connections);
   1593           close_connection_on_shutdown (connection);
   1594           _DBUS_LOCK (shared_connections);
   1595           connection = _dbus_list_pop_first (&shared_connections_no_guid);
   1596         }
   1597     }
   1598 
   1599   shared_connections_no_guid = NULL;
   1600 
   1601   _DBUS_UNLOCK (shared_connections);
   1602 }
   1603 
   1604 static dbus_bool_t
   1605 connection_lookup_shared (DBusAddressEntry  *entry,
   1606                           DBusConnection   **result)
   1607 {
   1608   _dbus_verbose ("checking for existing connection\n");
   1609 
   1610   *result = NULL;
   1611 
   1612   _DBUS_LOCK (shared_connections);
   1613 
   1614   if (shared_connections == NULL)
   1615     {
   1616       _dbus_verbose ("creating shared_connections hash table\n");
   1617 
   1618       shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
   1619                                                  dbus_free,
   1620                                                  NULL);
   1621       if (shared_connections == NULL)
   1622         {
   1623           _DBUS_UNLOCK (shared_connections);
   1624           return FALSE;
   1625         }
   1626 
   1627       if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
   1628         {
   1629           _dbus_hash_table_unref (shared_connections);
   1630           shared_connections = NULL;
   1631           _DBUS_UNLOCK (shared_connections);
   1632           return FALSE;
   1633         }
   1634 
   1635       _dbus_verbose ("  successfully created shared_connections\n");
   1636 
   1637       _DBUS_UNLOCK (shared_connections);
   1638       return TRUE; /* no point looking up in the hash we just made */
   1639     }
   1640   else
   1641     {
   1642       const char *guid;
   1643 
   1644       guid = dbus_address_entry_get_value (entry, "guid");
   1645 
   1646       if (guid != NULL)
   1647         {
   1648           DBusConnection *connection;
   1649 
   1650           connection = _dbus_hash_table_lookup_string (shared_connections,
   1651                                                        guid);
   1652 
   1653           if (connection)
   1654             {
   1655               /* The DBusConnection can't be finalized without taking
   1656                * the shared_connections lock to remove it from the
   1657                * hash.  So it's safe to ref the connection here.
   1658                * However, it may be disconnected if the Disconnected
   1659                * message hasn't been processed yet, in which case we
   1660                * want to pretend it isn't in the hash and avoid
   1661                * returning it.
   1662                *
   1663                * The idea is to avoid ever returning a disconnected connection
   1664                * from dbus_connection_open(). We could just synchronously
   1665                * drop our shared ref to the connection on connection disconnect,
   1666                * and then assert here that the connection is connected, but
   1667                * that causes reentrancy headaches.
   1668                */
   1669               CONNECTION_LOCK (connection);
   1670               if (_dbus_connection_get_is_connected_unlocked (connection))
   1671                 {
   1672                   _dbus_connection_ref_unlocked (connection);
   1673                   *result = connection;
   1674                   _dbus_verbose ("looked up existing connection to server guid %s\n",
   1675                                  guid);
   1676                 }
   1677               else
   1678                 {
   1679                   _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
   1680                                  guid);
   1681                 }
   1682               CONNECTION_UNLOCK (connection);
   1683             }
   1684         }
   1685 
   1686       _DBUS_UNLOCK (shared_connections);
   1687       return TRUE;
   1688     }
   1689 }
   1690 
   1691 static dbus_bool_t
   1692 connection_record_shared_unlocked (DBusConnection *connection,
   1693                                    const char     *guid)
   1694 {
   1695   char *guid_key;
   1696   char *guid_in_connection;
   1697 
   1698   HAVE_LOCK_CHECK (connection);
   1699   _dbus_assert (connection->server_guid == NULL);
   1700   _dbus_assert (connection->shareable);
   1701 
   1702   /* get a hard ref on this connection, even if
   1703    * we won't in fact store it in the hash, we still
   1704    * need to hold a ref on it until it's disconnected.
   1705    */
   1706   _dbus_connection_ref_unlocked (connection);
   1707 
   1708   if (guid == NULL)
   1709     {
   1710       _DBUS_LOCK (shared_connections);
   1711 
   1712       if (!_dbus_list_prepend (&shared_connections_no_guid, connection))
   1713         {
   1714           _DBUS_UNLOCK (shared_connections);
   1715           return FALSE;
   1716         }
   1717 
   1718       _DBUS_UNLOCK (shared_connections);
   1719       return TRUE; /* don't store in the hash */
   1720     }
   1721 
   1722   /* A separate copy of the key is required in the hash table, because
   1723    * we don't have a lock on the connection when we are doing a hash
   1724    * lookup.
   1725    */
   1726 
   1727   guid_key = _dbus_strdup (guid);
   1728   if (guid_key == NULL)
   1729     return FALSE;
   1730 
   1731   guid_in_connection = _dbus_strdup (guid);
   1732   if (guid_in_connection == NULL)
   1733     {
   1734       dbus_free (guid_key);
   1735       return FALSE;
   1736     }
   1737 
   1738   _DBUS_LOCK (shared_connections);
   1739   _dbus_assert (shared_connections != NULL);
   1740 
   1741   if (!_dbus_hash_table_insert_string (shared_connections,
   1742                                        guid_key, connection))
   1743     {
   1744       dbus_free (guid_key);
   1745       dbus_free (guid_in_connection);
   1746       _DBUS_UNLOCK (shared_connections);
   1747       return FALSE;
   1748     }
   1749 
   1750   connection->server_guid = guid_in_connection;
   1751 
   1752   _dbus_verbose ("stored connection to %s to be shared\n",
   1753                  connection->server_guid);
   1754 
   1755   _DBUS_UNLOCK (shared_connections);
   1756 
   1757   _dbus_assert (connection->server_guid != NULL);
   1758 
   1759   return TRUE;
   1760 }
   1761 
   1762 static void
   1763 connection_forget_shared_unlocked (DBusConnection *connection)
   1764 {
   1765   HAVE_LOCK_CHECK (connection);
   1766 
   1767   if (!connection->shareable)
   1768     return;
   1769 
   1770   _DBUS_LOCK (shared_connections);
   1771 
   1772   if (connection->server_guid != NULL)
   1773     {
   1774       _dbus_verbose ("dropping connection to %s out of the shared table\n",
   1775                      connection->server_guid);
   1776 
   1777       if (!_dbus_hash_table_remove_string (shared_connections,
   1778                                            connection->server_guid))
   1779         _dbus_assert_not_reached ("connection was not in the shared table");
   1780 
   1781       dbus_free (connection->server_guid);
   1782       connection->server_guid = NULL;
   1783     }
   1784   else
   1785     {
   1786       _dbus_list_remove (&shared_connections_no_guid, connection);
   1787     }
   1788 
   1789   _DBUS_UNLOCK (shared_connections);
   1790 
   1791   /* remove our reference held on all shareable connections */
   1792   _dbus_connection_unref_unlocked (connection);
   1793 }
   1794 
   1795 static DBusConnection*
   1796 connection_try_from_address_entry (DBusAddressEntry *entry,
   1797                                    DBusError        *error)
   1798 {
   1799   DBusTransport *transport;
   1800   DBusConnection *connection;
   1801 
   1802   transport = _dbus_transport_open (entry, error);
   1803 
   1804   if (transport == NULL)
   1805     {
   1806       _DBUS_ASSERT_ERROR_IS_SET (error);
   1807       return NULL;
   1808     }
   1809 
   1810   connection = _dbus_connection_new_for_transport (transport);
   1811 
   1812   _dbus_transport_unref (transport);
   1813 
   1814   if (connection == NULL)
   1815     {
   1816       _DBUS_SET_OOM (error);
   1817       return NULL;
   1818     }
   1819 
   1820 #ifndef DBUS_DISABLE_CHECKS
   1821   _dbus_assert (!connection->have_connection_lock);
   1822 #endif
   1823   return connection;
   1824 }
   1825 
   1826 /*
   1827  * If the shared parameter is true, then any existing connection will
   1828  * be used (and if a new connection is created, it will be available
   1829  * for use by others). If the shared parameter is false, a new
   1830  * connection will always be created, and the new connection will
   1831  * never be returned to other callers.
   1832  *
   1833  * @param address the address
   1834  * @param shared whether the connection is shared or private
   1835  * @param error error return
   1836  * @returns the connection or #NULL on error
   1837  */
   1838 static DBusConnection*
   1839 _dbus_connection_open_internal (const char     *address,
   1840                                 dbus_bool_t     shared,
   1841                                 DBusError      *error)
   1842 {
   1843   DBusConnection *connection;
   1844   DBusAddressEntry **entries;
   1845   DBusError tmp_error = DBUS_ERROR_INIT;
   1846   DBusError first_error = DBUS_ERROR_INIT;
   1847   int len, i;
   1848 
   1849   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   1850 
   1851   _dbus_verbose ("opening %s connection to: %s\n",
   1852                  shared ? "shared" : "private", address);
   1853 
   1854   if (!dbus_parse_address (address, &entries, &len, error))
   1855     return NULL;
   1856 
   1857   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   1858 
   1859   connection = NULL;
   1860 
   1861   for (i = 0; i < len; i++)
   1862     {
   1863       if (shared)
   1864         {
   1865           if (!connection_lookup_shared (entries[i], &connection))
   1866             _DBUS_SET_OOM (&tmp_error);
   1867         }
   1868 
   1869       if (connection == NULL)
   1870         {
   1871           connection = connection_try_from_address_entry (entries[i],
   1872                                                           &tmp_error);
   1873 
   1874           if (connection != NULL && shared)
   1875             {
   1876               const char *guid;
   1877 
   1878               connection->shareable = TRUE;
   1879 
   1880               /* guid may be NULL */
   1881               guid = dbus_address_entry_get_value (entries[i], "guid");
   1882 
   1883               CONNECTION_LOCK (connection);
   1884 
   1885               if (!connection_record_shared_unlocked (connection, guid))
   1886                 {
   1887                   _DBUS_SET_OOM (&tmp_error);
   1888                   _dbus_connection_close_possibly_shared_and_unlock (connection);
   1889                   dbus_connection_unref (connection);
   1890                   connection = NULL;
   1891                 }
   1892               else
   1893                 CONNECTION_UNLOCK (connection);
   1894             }
   1895         }
   1896 
   1897       if (connection)
   1898         break;
   1899 
   1900       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
   1901 
   1902       if (i == 0)
   1903         dbus_move_error (&tmp_error, &first_error);
   1904       else
   1905         dbus_error_free (&tmp_error);
   1906     }
   1907 
   1908   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   1909   _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
   1910 
   1911   if (connection == NULL)
   1912     {
   1913       _DBUS_ASSERT_ERROR_IS_SET (&first_error);
   1914       dbus_move_error (&first_error, error);
   1915     }
   1916   else
   1917     dbus_error_free (&first_error);
   1918 
   1919   dbus_address_entries_free (entries);
   1920   return connection;
   1921 }
   1922 
   1923 /**
   1924  * Closes a shared OR private connection, while dbus_connection_close() can
   1925  * only be used on private connections. Should only be called by the
   1926  * dbus code that owns the connection - an owner must be known,
   1927  * the open/close state is like malloc/free, not like ref/unref.
   1928  *
   1929  * @param connection the connection
   1930  */
   1931 void
   1932 _dbus_connection_close_possibly_shared (DBusConnection *connection)
   1933 {
   1934   _dbus_assert (connection != NULL);
   1935   _dbus_assert (connection->generation == _dbus_current_generation);
   1936 
   1937   CONNECTION_LOCK (connection);
   1938   _dbus_connection_close_possibly_shared_and_unlock (connection);
   1939 }
   1940 
   1941 static DBusPreallocatedSend*
   1942 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
   1943 {
   1944   DBusPreallocatedSend *preallocated;
   1945 
   1946   HAVE_LOCK_CHECK (connection);
   1947 
   1948   _dbus_assert (connection != NULL);
   1949 
   1950   preallocated = dbus_new (DBusPreallocatedSend, 1);
   1951   if (preallocated == NULL)
   1952     return NULL;
   1953 
   1954   preallocated->queue_link = _dbus_list_alloc_link (NULL);
   1955   if (preallocated->queue_link == NULL)
   1956     goto failed_0;
   1957 
   1958   preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
   1959   if (preallocated->counter_link == NULL)
   1960     goto failed_1;
   1961 
   1962   _dbus_counter_ref (preallocated->counter_link->data);
   1963 
   1964   preallocated->connection = connection;
   1965 
   1966   return preallocated;
   1967 
   1968  failed_1:
   1969   _dbus_list_free_link (preallocated->queue_link);
   1970  failed_0:
   1971   dbus_free (preallocated);
   1972 
   1973   return NULL;
   1974 }
   1975 
   1976 /* Called with lock held, does not update dispatch status */
   1977 static void
   1978 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection       *connection,
   1979                                                        DBusPreallocatedSend *preallocated,
   1980                                                        DBusMessage          *message,
   1981                                                        dbus_uint32_t        *client_serial)
   1982 {
   1983   dbus_uint32_t serial;
   1984 
   1985   preallocated->queue_link->data = message;
   1986   _dbus_list_prepend_link (&connection->outgoing_messages,
   1987                            preallocated->queue_link);
   1988 
   1989   /* It's OK that we'll never call the notify function, because for the
   1990    * outgoing limit, there isn't one */
   1991   _dbus_message_add_counter_link (message,
   1992                                   preallocated->counter_link);
   1993 
   1994   dbus_free (preallocated);
   1995   preallocated = NULL;
   1996 
   1997   dbus_message_ref (message);
   1998 
   1999   connection->n_outgoing += 1;
   2000 
   2001   _dbus_verbose ("Message %p (%s %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
   2002                  message,
   2003                  dbus_message_type_to_string (dbus_message_get_type (message)),
   2004                  dbus_message_get_path (message) ?
   2005                  dbus_message_get_path (message) :
   2006                  "no path",
   2007                  dbus_message_get_interface (message) ?
   2008                  dbus_message_get_interface (message) :
   2009                  "no interface",
   2010                  dbus_message_get_member (message) ?
   2011                  dbus_message_get_member (message) :
   2012                  "no member",
   2013                  dbus_message_get_signature (message),
   2014                  dbus_message_get_destination (message) ?
   2015                  dbus_message_get_destination (message) :
   2016                  "null",
   2017                  connection,
   2018                  connection->n_outgoing);
   2019 
   2020   if (dbus_message_get_serial (message) == 0)
   2021     {
   2022       serial = _dbus_connection_get_next_client_serial (connection);
   2023       dbus_message_set_serial (message, serial);
   2024       if (client_serial)
   2025         *client_serial = serial;
   2026     }
   2027   else
   2028     {
   2029       if (client_serial)
   2030         *client_serial = dbus_message_get_serial (message);
   2031     }
   2032 
   2033   _dbus_verbose ("Message %p serial is %u\n",
   2034                  message, dbus_message_get_serial (message));
   2035 
   2036   dbus_message_lock (message);
   2037 
   2038   /* Now we need to run an iteration to hopefully just write the messages
   2039    * out immediately, and otherwise get them queued up
   2040    */
   2041   _dbus_connection_do_iteration_unlocked (connection,
   2042                                           NULL,
   2043                                           DBUS_ITERATION_DO_WRITING,
   2044                                           -1);
   2045 
   2046   /* If stuff is still queued up, be sure we wake up the main loop */
   2047   if (connection->n_outgoing > 0)
   2048     _dbus_connection_wakeup_mainloop (connection);
   2049 }
   2050 
   2051 static void
   2052 _dbus_connection_send_preallocated_and_unlock (DBusConnection       *connection,
   2053 					       DBusPreallocatedSend *preallocated,
   2054 					       DBusMessage          *message,
   2055 					       dbus_uint32_t        *client_serial)
   2056 {
   2057   DBusDispatchStatus status;
   2058 
   2059   HAVE_LOCK_CHECK (connection);
   2060 
   2061   _dbus_connection_send_preallocated_unlocked_no_update (connection,
   2062                                                          preallocated,
   2063                                                          message, client_serial);
   2064 
   2065   _dbus_verbose ("middle\n");
   2066   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   2067 
   2068   /* this calls out to user code */
   2069   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   2070 }
   2071 
   2072 /**
   2073  * Like dbus_connection_send(), but assumes the connection
   2074  * is already locked on function entry, and unlocks before returning.
   2075  *
   2076  * @param connection the connection
   2077  * @param message the message to send
   2078  * @param client_serial return location for client serial of sent message
   2079  * @returns #FALSE on out-of-memory
   2080  */
   2081 dbus_bool_t
   2082 _dbus_connection_send_and_unlock (DBusConnection *connection,
   2083 				  DBusMessage    *message,
   2084 				  dbus_uint32_t  *client_serial)
   2085 {
   2086   DBusPreallocatedSend *preallocated;
   2087 
   2088   _dbus_assert (connection != NULL);
   2089   _dbus_assert (message != NULL);
   2090 
   2091   preallocated = _dbus_connection_preallocate_send_unlocked (connection);
   2092   if (preallocated == NULL)
   2093     {
   2094       CONNECTION_UNLOCK (connection);
   2095       return FALSE;
   2096     }
   2097 
   2098   _dbus_connection_send_preallocated_and_unlock (connection,
   2099 						 preallocated,
   2100 						 message,
   2101 						 client_serial);
   2102   return TRUE;
   2103 }
   2104 
   2105 /**
   2106  * Used internally to handle the semantics of dbus_server_set_new_connection_function().
   2107  * If the new connection function does not ref the connection, we want to close it.
   2108  *
   2109  * A bit of a hack, probably the new connection function should have returned a value
   2110  * for whether to close, or should have had to close the connection itself if it
   2111  * didn't want it.
   2112  *
   2113  * But, this works OK as long as the new connection function doesn't do anything
   2114  * crazy like keep the connection around without ref'ing it.
   2115  *
   2116  * We have to lock the connection across refcount check and close in case
   2117  * the new connection function spawns a thread that closes and unrefs.
   2118  * In that case, if the app thread
   2119  * closes and unrefs first, we'll harmlessly close again; if the app thread
   2120  * still has the ref, we'll close and then the app will close harmlessly.
   2121  * If the app unrefs without closing, the app is broken since if the
   2122  * app refs from the new connection function it is supposed to also close.
   2123  *
   2124  * If we didn't atomically check the refcount and close with the lock held
   2125  * though, we could screw this up.
   2126  *
   2127  * @param connection the connection
   2128  */
   2129 void
   2130 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
   2131 {
   2132   dbus_int32_t refcount;
   2133 
   2134   CONNECTION_LOCK (connection);
   2135 
   2136   refcount = _dbus_atomic_get (&connection->refcount);
   2137   /* The caller should have at least one ref */
   2138   _dbus_assert (refcount >= 1);
   2139 
   2140   if (refcount == 1)
   2141     _dbus_connection_close_possibly_shared_and_unlock (connection);
   2142   else
   2143     CONNECTION_UNLOCK (connection);
   2144 }
   2145 
   2146 
   2147 /**
   2148  * When a function that blocks has been called with a timeout, and we
   2149  * run out of memory, the time to wait for memory is based on the
   2150  * timeout. If the caller was willing to block a long time we wait a
   2151  * relatively long time for memory, if they were only willing to block
   2152  * briefly then we retry for memory at a rapid rate.
   2153  *
   2154  * @timeout_milliseconds the timeout requested for blocking
   2155  */
   2156 static void
   2157 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
   2158 {
   2159   if (timeout_milliseconds == -1)
   2160     _dbus_sleep_milliseconds (1000);
   2161   else if (timeout_milliseconds < 100)
   2162     ; /* just busy loop */
   2163   else if (timeout_milliseconds <= 1000)
   2164     _dbus_sleep_milliseconds (timeout_milliseconds / 3);
   2165   else
   2166     _dbus_sleep_milliseconds (1000);
   2167 }
   2168 
   2169 static DBusMessage *
   2170 generate_local_error_message (dbus_uint32_t serial,
   2171                               char *error_name,
   2172                               char *error_msg)
   2173 {
   2174   DBusMessage *message;
   2175   message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
   2176   if (!message)
   2177     goto out;
   2178 
   2179   if (!dbus_message_set_error_name (message, error_name))
   2180     {
   2181       dbus_message_unref (message);
   2182       message = NULL;
   2183       goto out;
   2184     }
   2185 
   2186   dbus_message_set_no_reply (message, TRUE);
   2187 
   2188   if (!dbus_message_set_reply_serial (message,
   2189                                       serial))
   2190     {
   2191       dbus_message_unref (message);
   2192       message = NULL;
   2193       goto out;
   2194     }
   2195 
   2196   if (error_msg != NULL)
   2197     {
   2198       DBusMessageIter iter;
   2199 
   2200       dbus_message_iter_init_append (message, &iter);
   2201       if (!dbus_message_iter_append_basic (&iter,
   2202                                            DBUS_TYPE_STRING,
   2203                                            &error_msg))
   2204         {
   2205           dbus_message_unref (message);
   2206           message = NULL;
   2207 	  goto out;
   2208         }
   2209     }
   2210 
   2211  out:
   2212   return message;
   2213 }
   2214 
   2215 /*
   2216  * Peek the incoming queue to see if we got reply for a specific serial
   2217  */
   2218 static dbus_bool_t
   2219 _dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
   2220                                           dbus_uint32_t   client_serial)
   2221 {
   2222   DBusList *link;
   2223   HAVE_LOCK_CHECK (connection);
   2224 
   2225   link = _dbus_list_get_first_link (&connection->incoming_messages);
   2226 
   2227   while (link != NULL)
   2228     {
   2229       DBusMessage *reply = link->data;
   2230 
   2231       if (dbus_message_get_reply_serial (reply) == client_serial)
   2232         {
   2233           _dbus_verbose ("%s reply to %d found in queue\n", _DBUS_FUNCTION_NAME, client_serial);
   2234           return TRUE;
   2235         }
   2236       link = _dbus_list_get_next_link (&connection->incoming_messages, link);
   2237     }
   2238 
   2239   return FALSE;
   2240 }
   2241 
   2242 /* This is slightly strange since we can pop a message here without
   2243  * the dispatch lock.
   2244  */
   2245 static DBusMessage*
   2246 check_for_reply_unlocked (DBusConnection *connection,
   2247                           dbus_uint32_t   client_serial)
   2248 {
   2249   DBusList *link;
   2250 
   2251   HAVE_LOCK_CHECK (connection);
   2252 
   2253   link = _dbus_list_get_first_link (&connection->incoming_messages);
   2254 
   2255   while (link != NULL)
   2256     {
   2257       DBusMessage *reply = link->data;
   2258 
   2259       if (dbus_message_get_reply_serial (reply) == client_serial)
   2260 	{
   2261 	  _dbus_list_remove_link (&connection->incoming_messages, link);
   2262 	  connection->n_incoming  -= 1;
   2263 	  return reply;
   2264 	}
   2265       link = _dbus_list_get_next_link (&connection->incoming_messages, link);
   2266     }
   2267 
   2268   return NULL;
   2269 }
   2270 
   2271 static void
   2272 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
   2273 {
   2274    /* We can't iterate over the hash in the normal way since we'll be
   2275     * dropping the lock for each item. So we restart the
   2276     * iter each time as we drain the hash table.
   2277     */
   2278 
   2279    while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
   2280     {
   2281       DBusPendingCall *pending;
   2282       DBusHashIter iter;
   2283 
   2284       _dbus_hash_iter_init (connection->pending_replies, &iter);
   2285       _dbus_hash_iter_next (&iter);
   2286 
   2287       pending = _dbus_hash_iter_get_value (&iter);
   2288       _dbus_pending_call_ref_unlocked (pending);
   2289 
   2290       _dbus_pending_call_queue_timeout_error_unlocked (pending,
   2291                                                        connection);
   2292 
   2293       if (_dbus_pending_call_is_timeout_added_unlocked (pending))
   2294           _dbus_connection_remove_timeout_unlocked (connection,
   2295                                                     _dbus_pending_call_get_timeout_unlocked (pending));
   2296       _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
   2297       _dbus_hash_iter_remove_entry (&iter);
   2298 
   2299       _dbus_pending_call_unref_and_unlock (pending);
   2300       CONNECTION_LOCK (connection);
   2301     }
   2302   HAVE_LOCK_CHECK (connection);
   2303 }
   2304 
   2305 static void
   2306 complete_pending_call_and_unlock (DBusConnection  *connection,
   2307                                   DBusPendingCall *pending,
   2308                                   DBusMessage     *message)
   2309 {
   2310   _dbus_pending_call_set_reply_unlocked (pending, message);
   2311   _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
   2312   _dbus_connection_detach_pending_call_and_unlock (connection, pending);
   2313 
   2314   /* Must be called unlocked since it invokes app callback */
   2315   _dbus_pending_call_complete (pending);
   2316   dbus_pending_call_unref (pending);
   2317 }
   2318 
   2319 static dbus_bool_t
   2320 check_for_reply_and_update_dispatch_unlocked (DBusConnection  *connection,
   2321                                               DBusPendingCall *pending)
   2322 {
   2323   DBusMessage *reply;
   2324   DBusDispatchStatus status;
   2325 
   2326   reply = check_for_reply_unlocked (connection,
   2327                                     _dbus_pending_call_get_reply_serial_unlocked (pending));
   2328   if (reply != NULL)
   2329     {
   2330       _dbus_verbose ("checked for reply\n");
   2331 
   2332       _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
   2333 
   2334       complete_pending_call_and_unlock (connection, pending, reply);
   2335       dbus_message_unref (reply);
   2336 
   2337       CONNECTION_LOCK (connection);
   2338       status = _dbus_connection_get_dispatch_status_unlocked (connection);
   2339       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   2340       dbus_pending_call_unref (pending);
   2341 
   2342       return TRUE;
   2343     }
   2344 
   2345   return FALSE;
   2346 }
   2347 
   2348 /**
   2349  * Blocks until a pending call times out or gets a reply.
   2350  *
   2351  * Does not re-enter the main loop or run filter/path-registered
   2352  * callbacks. The reply to the message will not be seen by
   2353  * filter callbacks.
   2354  *
   2355  * Returns immediately if pending call already got a reply.
   2356  *
   2357  * @todo could use performance improvements (it keeps scanning
   2358  * the whole message queue for example)
   2359  *
   2360  * @param pending the pending call we block for a reply on
   2361  */
   2362 void
   2363 _dbus_connection_block_pending_call (DBusPendingCall *pending)
   2364 {
   2365   long start_tv_sec, start_tv_usec;
   2366   long tv_sec, tv_usec;
   2367   DBusDispatchStatus status;
   2368   DBusConnection *connection;
   2369   dbus_uint32_t client_serial;
   2370   DBusTimeout *timeout;
   2371   int timeout_milliseconds, elapsed_milliseconds;
   2372 
   2373   _dbus_assert (pending != NULL);
   2374 
   2375   if (dbus_pending_call_get_completed (pending))
   2376     return;
   2377 
   2378   dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
   2379 
   2380   connection = _dbus_pending_call_get_connection_and_lock (pending);
   2381 
   2382   /* Flush message queue - note, can affect dispatch status */
   2383   _dbus_connection_flush_unlocked (connection);
   2384 
   2385   client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
   2386 
   2387   /* note that timeout_milliseconds is limited to a smallish value
   2388    * in _dbus_pending_call_new() so overflows aren't possible
   2389    * below
   2390    */
   2391   timeout = _dbus_pending_call_get_timeout_unlocked (pending);
   2392   _dbus_get_monotonic_time (&start_tv_sec, &start_tv_usec);
   2393   if (timeout)
   2394     {
   2395       timeout_milliseconds = dbus_timeout_get_interval (timeout);
   2396 
   2397       _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec\n",
   2398                      timeout_milliseconds,
   2399                      client_serial,
   2400                      start_tv_sec, start_tv_usec);
   2401     }
   2402   else
   2403     {
   2404       timeout_milliseconds = -1;
   2405 
   2406       _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block for reply serial %u\n", client_serial);
   2407     }
   2408 
   2409   /* check to see if we already got the data off the socket */
   2410   /* from another blocked pending call */
   2411   if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
   2412     return;
   2413 
   2414   /* Now we wait... */
   2415   /* always block at least once as we know we don't have the reply yet */
   2416   _dbus_connection_do_iteration_unlocked (connection,
   2417                                           pending,
   2418                                           DBUS_ITERATION_DO_READING |
   2419                                           DBUS_ITERATION_BLOCK,
   2420                                           timeout_milliseconds);
   2421 
   2422  recheck_status:
   2423 
   2424   _dbus_verbose ("top of recheck\n");
   2425 
   2426   HAVE_LOCK_CHECK (connection);
   2427 
   2428   /* queue messages and get status */
   2429 
   2430   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   2431 
   2432   /* the get_completed() is in case a dispatch() while we were blocking
   2433    * got the reply instead of us.
   2434    */
   2435   if (_dbus_pending_call_get_completed_unlocked (pending))
   2436     {
   2437       _dbus_verbose ("Pending call completed by dispatch\n");
   2438       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   2439       dbus_pending_call_unref (pending);
   2440       return;
   2441     }
   2442 
   2443   if (status == DBUS_DISPATCH_DATA_REMAINS)
   2444     {
   2445       if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
   2446         return;
   2447     }
   2448 
   2449   _dbus_get_monotonic_time (&tv_sec, &tv_usec);
   2450   elapsed_milliseconds = (tv_sec - start_tv_sec) * 1000 +
   2451 	  (tv_usec - start_tv_usec) / 1000;
   2452 
   2453   if (!_dbus_connection_get_is_connected_unlocked (connection))
   2454     {
   2455       DBusMessage *error_msg;
   2456 
   2457       error_msg = generate_local_error_message (client_serial,
   2458                                                 DBUS_ERROR_DISCONNECTED,
   2459                                                 "Connection was disconnected before a reply was received");
   2460 
   2461       /* on OOM error_msg is set to NULL */
   2462       complete_pending_call_and_unlock (connection, pending, error_msg);
   2463       dbus_pending_call_unref (pending);
   2464       return;
   2465     }
   2466   else if (connection->disconnect_message_link == NULL)
   2467     _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
   2468   else if (timeout == NULL)
   2469     {
   2470        if (status == DBUS_DISPATCH_NEED_MEMORY)
   2471         {
   2472           /* Try sleeping a bit, as we aren't sure we need to block for reading,
   2473            * we may already have a reply in the buffer and just can't process
   2474            * it.
   2475            */
   2476           _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
   2477 
   2478           _dbus_memory_pause_based_on_timeout (timeout_milliseconds - elapsed_milliseconds);
   2479         }
   2480       else
   2481         {
   2482           /* block again, we don't have the reply buffered yet. */
   2483           _dbus_connection_do_iteration_unlocked (connection,
   2484                                                   pending,
   2485                                                   DBUS_ITERATION_DO_READING |
   2486                                                   DBUS_ITERATION_BLOCK,
   2487                                                   timeout_milliseconds - elapsed_milliseconds);
   2488         }
   2489 
   2490       goto recheck_status;
   2491     }
   2492   else if (tv_sec < start_tv_sec)
   2493     _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
   2494   else if (elapsed_milliseconds < timeout_milliseconds)
   2495     {
   2496       _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds - elapsed_milliseconds);
   2497 
   2498       if (status == DBUS_DISPATCH_NEED_MEMORY)
   2499         {
   2500           /* Try sleeping a bit, as we aren't sure we need to block for reading,
   2501            * we may already have a reply in the buffer and just can't process
   2502            * it.
   2503            */
   2504           _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
   2505 
   2506           _dbus_memory_pause_based_on_timeout (timeout_milliseconds - elapsed_milliseconds);
   2507         }
   2508       else
   2509         {
   2510           /* block again, we don't have the reply buffered yet. */
   2511           _dbus_connection_do_iteration_unlocked (connection,
   2512                                                   NULL,
   2513                                                   DBUS_ITERATION_DO_READING |
   2514                                                   DBUS_ITERATION_BLOCK,
   2515                                                   timeout_milliseconds - elapsed_milliseconds);
   2516         }
   2517 
   2518       goto recheck_status;
   2519     }
   2520 
   2521   _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %d milliseconds and got no reply\n",
   2522                  elapsed_milliseconds);
   2523 
   2524   _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
   2525 
   2526   /* unlock and call user code */
   2527   complete_pending_call_and_unlock (connection, pending, NULL);
   2528 
   2529   /* update user code on dispatch status */
   2530   CONNECTION_LOCK (connection);
   2531   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   2532   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   2533   dbus_pending_call_unref (pending);
   2534 }
   2535 
   2536 /** @} */
   2537 
   2538 /**
   2539  * @addtogroup DBusConnection
   2540  *
   2541  * @{
   2542  */
   2543 
   2544 /**
   2545  * Gets a connection to a remote address. If a connection to the given
   2546  * address already exists, returns the existing connection with its
   2547  * reference count incremented.  Otherwise, returns a new connection
   2548  * and saves the new connection for possible re-use if a future call
   2549  * to dbus_connection_open() asks to connect to the same server.
   2550  *
   2551  * Use dbus_connection_open_private() to get a dedicated connection
   2552  * not shared with other callers of dbus_connection_open().
   2553  *
   2554  * If the open fails, the function returns #NULL, and provides a
   2555  * reason for the failure in the error parameter. Pass #NULL for the
   2556  * error parameter if you aren't interested in the reason for
   2557  * failure.
   2558  *
   2559  * Because this connection is shared, no user of the connection
   2560  * may call dbus_connection_close(). However, when you are done with the
   2561  * connection you should call dbus_connection_unref().
   2562  *
   2563  * @note Prefer dbus_connection_open() to dbus_connection_open_private()
   2564  * unless you have good reason; connections are expensive enough
   2565  * that it's wasteful to create lots of connections to the same
   2566  * server.
   2567  *
   2568  * @param address the address.
   2569  * @param error address where an error can be returned.
   2570  * @returns new connection, or #NULL on failure.
   2571  */
   2572 DBusConnection*
   2573 dbus_connection_open (const char     *address,
   2574                       DBusError      *error)
   2575 {
   2576   DBusConnection *connection;
   2577 
   2578   _dbus_return_val_if_fail (address != NULL, NULL);
   2579   _dbus_return_val_if_error_is_set (error, NULL);
   2580 
   2581   connection = _dbus_connection_open_internal (address,
   2582                                                TRUE,
   2583                                                error);
   2584 
   2585   return connection;
   2586 }
   2587 
   2588 /**
   2589  * Opens a new, dedicated connection to a remote address. Unlike
   2590  * dbus_connection_open(), always creates a new connection.
   2591  * This connection will not be saved or recycled by libdbus.
   2592  *
   2593  * If the open fails, the function returns #NULL, and provides a
   2594  * reason for the failure in the error parameter. Pass #NULL for the
   2595  * error parameter if you aren't interested in the reason for
   2596  * failure.
   2597  *
   2598  * When you are done with this connection, you must
   2599  * dbus_connection_close() to disconnect it,
   2600  * and dbus_connection_unref() to free the connection object.
   2601  *
   2602  * (The dbus_connection_close() can be skipped if the
   2603  * connection is already known to be disconnected, for example
   2604  * if you are inside a handler for the Disconnected signal.)
   2605  *
   2606  * @note Prefer dbus_connection_open() to dbus_connection_open_private()
   2607  * unless you have good reason; connections are expensive enough
   2608  * that it's wasteful to create lots of connections to the same
   2609  * server.
   2610  *
   2611  * @param address the address.
   2612  * @param error address where an error can be returned.
   2613  * @returns new connection, or #NULL on failure.
   2614  */
   2615 DBusConnection*
   2616 dbus_connection_open_private (const char     *address,
   2617                               DBusError      *error)
   2618 {
   2619   DBusConnection *connection;
   2620 
   2621   _dbus_return_val_if_fail (address != NULL, NULL);
   2622   _dbus_return_val_if_error_is_set (error, NULL);
   2623 
   2624   connection = _dbus_connection_open_internal (address,
   2625                                                FALSE,
   2626                                                error);
   2627 
   2628   return connection;
   2629 }
   2630 
   2631 /**
   2632  * Increments the reference count of a DBusConnection.
   2633  *
   2634  * @param connection the connection.
   2635  * @returns the connection.
   2636  */
   2637 DBusConnection *
   2638 dbus_connection_ref (DBusConnection *connection)
   2639 {
   2640   dbus_int32_t old_refcount;
   2641 
   2642   _dbus_return_val_if_fail (connection != NULL, NULL);
   2643   _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
   2644   old_refcount = _dbus_atomic_inc (&connection->refcount);
   2645   _dbus_connection_trace_ref (connection, old_refcount, old_refcount + 1,
   2646       "ref");
   2647 
   2648   return connection;
   2649 }
   2650 
   2651 static void
   2652 free_outgoing_message (void *element,
   2653                        void *data)
   2654 {
   2655   DBusMessage *message = element;
   2656   DBusConnection *connection = data;
   2657 
   2658   _dbus_message_remove_counter (message, connection->outgoing_counter);
   2659   dbus_message_unref (message);
   2660 }
   2661 
   2662 /* This is run without the mutex held, but after the last reference
   2663  * to the connection has been dropped we should have no thread-related
   2664  * problems
   2665  */
   2666 static void
   2667 _dbus_connection_last_unref (DBusConnection *connection)
   2668 {
   2669   DBusList *link;
   2670 
   2671   _dbus_verbose ("Finalizing connection %p\n", connection);
   2672 
   2673   _dbus_assert (_dbus_atomic_get (&connection->refcount) == 0);
   2674 
   2675   /* You have to disconnect the connection before unref:ing it. Otherwise
   2676    * you won't get the disconnected message.
   2677    */
   2678   _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
   2679   _dbus_assert (connection->server_guid == NULL);
   2680 
   2681   /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
   2682   _dbus_object_tree_free_all_unlocked (connection->objects);
   2683 
   2684   dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
   2685   dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
   2686   dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
   2687 
   2688   _dbus_watch_list_free (connection->watches);
   2689   connection->watches = NULL;
   2690 
   2691   _dbus_timeout_list_free (connection->timeouts);
   2692   connection->timeouts = NULL;
   2693 
   2694   _dbus_data_slot_list_free (&connection->slot_list);
   2695 
   2696   link = _dbus_list_get_first_link (&connection->filter_list);
   2697   while (link != NULL)
   2698     {
   2699       DBusMessageFilter *filter = link->data;
   2700       DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
   2701 
   2702       filter->function = NULL;
   2703       _dbus_message_filter_unref (filter); /* calls app callback */
   2704       link->data = NULL;
   2705 
   2706       link = next;
   2707     }
   2708   _dbus_list_clear (&connection->filter_list);
   2709 
   2710   /* ---- Done with stuff that invokes application callbacks */
   2711 
   2712   _dbus_object_tree_unref (connection->objects);
   2713 
   2714   _dbus_hash_table_unref (connection->pending_replies);
   2715   connection->pending_replies = NULL;
   2716 
   2717   _dbus_list_clear (&connection->filter_list);
   2718 
   2719   _dbus_list_foreach (&connection->outgoing_messages,
   2720                       free_outgoing_message,
   2721 		      connection);
   2722   _dbus_list_clear (&connection->outgoing_messages);
   2723 
   2724   _dbus_list_foreach (&connection->incoming_messages,
   2725 		      (DBusForeachFunction) dbus_message_unref,
   2726 		      NULL);
   2727   _dbus_list_clear (&connection->incoming_messages);
   2728 
   2729   _dbus_counter_unref (connection->outgoing_counter);
   2730 
   2731   _dbus_transport_unref (connection->transport);
   2732 
   2733   if (connection->disconnect_message_link)
   2734     {
   2735       DBusMessage *message = connection->disconnect_message_link->data;
   2736       dbus_message_unref (message);
   2737       _dbus_list_free_link (connection->disconnect_message_link);
   2738     }
   2739 
   2740   _dbus_condvar_free_at_location (&connection->dispatch_cond);
   2741   _dbus_condvar_free_at_location (&connection->io_path_cond);
   2742 
   2743   _dbus_cmutex_free_at_location (&connection->io_path_mutex);
   2744   _dbus_cmutex_free_at_location (&connection->dispatch_mutex);
   2745 
   2746   _dbus_rmutex_free_at_location (&connection->slot_mutex);
   2747 
   2748   _dbus_rmutex_free_at_location (&connection->mutex);
   2749 
   2750   dbus_free (connection);
   2751 }
   2752 
   2753 /**
   2754  * Decrements the reference count of a DBusConnection, and finalizes
   2755  * it if the count reaches zero.
   2756  *
   2757  * Note: it is a bug to drop the last reference to a connection that
   2758  * is still connected.
   2759  *
   2760  * For shared connections, libdbus will own a reference
   2761  * as long as the connection is connected, so you can know that either
   2762  * you don't have the last reference, or it's OK to drop the last reference.
   2763  * Most connections are shared. dbus_connection_open() and dbus_bus_get()
   2764  * return shared connections.
   2765  *
   2766  * For private connections, the creator of the connection must arrange for
   2767  * dbus_connection_close() to be called prior to dropping the last reference.
   2768  * Private connections come from dbus_connection_open_private() or dbus_bus_get_private().
   2769  *
   2770  * @param connection the connection.
   2771  */
   2772 void
   2773 dbus_connection_unref (DBusConnection *connection)
   2774 {
   2775   dbus_int32_t old_refcount;
   2776 
   2777   _dbus_return_if_fail (connection != NULL);
   2778   _dbus_return_if_fail (connection->generation == _dbus_current_generation);
   2779 
   2780   old_refcount = _dbus_atomic_dec (&connection->refcount);
   2781 
   2782   _dbus_connection_trace_ref (connection, old_refcount, old_refcount - 1,
   2783       "unref");
   2784 
   2785   if (old_refcount == 1)
   2786     {
   2787 #ifndef DBUS_DISABLE_CHECKS
   2788       if (_dbus_transport_get_is_connected (connection->transport))
   2789         {
   2790           _dbus_warn_check_failed ("The last reference on a connection was dropped without closing the connection. This is a bug in an application. See dbus_connection_unref() documentation for details.\n%s",
   2791                                    connection->shareable ?
   2792                                    "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" :
   2793                                     "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
   2794           return;
   2795         }
   2796 #endif
   2797       _dbus_connection_last_unref (connection);
   2798     }
   2799 }
   2800 
   2801 /*
   2802  * Note that the transport can disconnect itself (other end drops us)
   2803  * and in that case this function never runs. So this function must
   2804  * not do anything more than disconnect the transport and update the
   2805  * dispatch status.
   2806  *
   2807  * If the transport self-disconnects, then we assume someone will
   2808  * dispatch the connection to cause the dispatch status update.
   2809  */
   2810 static void
   2811 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
   2812 {
   2813   DBusDispatchStatus status;
   2814 
   2815   HAVE_LOCK_CHECK (connection);
   2816 
   2817   _dbus_verbose ("Disconnecting %p\n", connection);
   2818 
   2819   /* We need to ref because update_dispatch_status_and_unlock will unref
   2820    * the connection if it was shared and libdbus was the only remaining
   2821    * refcount holder.
   2822    */
   2823   _dbus_connection_ref_unlocked (connection);
   2824 
   2825   _dbus_transport_disconnect (connection->transport);
   2826 
   2827   /* This has the side effect of queuing the disconnect message link
   2828    * (unless we don't have enough memory, possibly, so don't assert it).
   2829    * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
   2830    * should never again return the newly-disconnected connection.
   2831    *
   2832    * However, we only unref the shared connection and exit_on_disconnect when
   2833    * the disconnect message reaches the head of the message queue,
   2834    * NOT when it's first queued.
   2835    */
   2836   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   2837 
   2838   /* This calls out to user code */
   2839   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   2840 
   2841   /* Could also call out to user code */
   2842   dbus_connection_unref (connection);
   2843 }
   2844 
   2845 /**
   2846  * Closes a private connection, so no further data can be sent or received.
   2847  * This disconnects the transport (such as a socket) underlying the
   2848  * connection.
   2849  *
   2850  * Attempts to send messages after closing a connection are safe, but will result in
   2851  * error replies generated locally in libdbus.
   2852  *
   2853  * This function does not affect the connection's reference count.  It's
   2854  * safe to close a connection more than once; all calls after the
   2855  * first do nothing. It's impossible to "reopen" a connection, a
   2856  * new connection must be created. This function may result in a call
   2857  * to the DBusDispatchStatusFunction set with
   2858  * dbus_connection_set_dispatch_status_function(), as the disconnect
   2859  * message it generates needs to be dispatched.
   2860  *
   2861  * If a connection is dropped by the remote application, it will
   2862  * close itself.
   2863  *
   2864  * You must close a connection prior to releasing the last reference to
   2865  * the connection. If you dbus_connection_unref() for the last time
   2866  * without closing the connection, the results are undefined; it
   2867  * is a bug in your program and libdbus will try to print a warning.
   2868  *
   2869  * You may not close a shared connection. Connections created with
   2870  * dbus_connection_open() or dbus_bus_get() are shared.
   2871  * These connections are owned by libdbus, and applications should
   2872  * only unref them, never close them. Applications can know it is
   2873  * safe to unref these connections because libdbus will be holding a
   2874  * reference as long as the connection is open. Thus, either the
   2875  * connection is closed and it is OK to drop the last reference,
   2876  * or the connection is open and the app knows it does not have the
   2877  * last reference.
   2878  *
   2879  * Connections created with dbus_connection_open_private() or
   2880  * dbus_bus_get_private() are not kept track of or referenced by
   2881  * libdbus. The creator of these connections is responsible for
   2882  * calling dbus_connection_close() prior to releasing the last
   2883  * reference, if the connection is not already disconnected.
   2884  *
   2885  * @param connection the private (unshared) connection to close
   2886  */
   2887 void
   2888 dbus_connection_close (DBusConnection *connection)
   2889 {
   2890   _dbus_return_if_fail (connection != NULL);
   2891   _dbus_return_if_fail (connection->generation == _dbus_current_generation);
   2892 
   2893   CONNECTION_LOCK (connection);
   2894 
   2895 #ifndef DBUS_DISABLE_CHECKS
   2896   if (connection->shareable)
   2897     {
   2898       CONNECTION_UNLOCK (connection);
   2899 
   2900       _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
   2901       return;
   2902     }
   2903 #endif
   2904 
   2905   _dbus_connection_close_possibly_shared_and_unlock (connection);
   2906 }
   2907 
   2908 static dbus_bool_t
   2909 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
   2910 {
   2911   HAVE_LOCK_CHECK (connection);
   2912   return _dbus_transport_get_is_connected (connection->transport);
   2913 }
   2914 
   2915 /**
   2916  * Gets whether the connection is currently open.  A connection may
   2917  * become disconnected when the remote application closes its end, or
   2918  * exits; a connection may also be disconnected with
   2919  * dbus_connection_close().
   2920  *
   2921  * There are not separate states for "closed" and "disconnected," the two
   2922  * terms are synonymous. This function should really be called
   2923  * get_is_open() but for historical reasons is not.
   2924  *
   2925  * @param connection the connection.
   2926  * @returns #TRUE if the connection is still alive.
   2927  */
   2928 dbus_bool_t
   2929 dbus_connection_get_is_connected (DBusConnection *connection)
   2930 {
   2931   dbus_bool_t res;
   2932 
   2933   _dbus_return_val_if_fail (connection != NULL, FALSE);
   2934 
   2935   CONNECTION_LOCK (connection);
   2936   res = _dbus_connection_get_is_connected_unlocked (connection);
   2937   CONNECTION_UNLOCK (connection);
   2938 
   2939   return res;
   2940 }
   2941 
   2942 /**
   2943  * Gets whether the connection was authenticated. (Note that
   2944  * if the connection was authenticated then disconnected,
   2945  * this function still returns #TRUE)
   2946  *
   2947  * @param connection the connection
   2948  * @returns #TRUE if the connection was ever authenticated
   2949  */
   2950 dbus_bool_t
   2951 dbus_connection_get_is_authenticated (DBusConnection *connection)
   2952 {
   2953   dbus_bool_t res;
   2954 
   2955   _dbus_return_val_if_fail (connection != NULL, FALSE);
   2956 
   2957   CONNECTION_LOCK (connection);
   2958   res = _dbus_transport_get_is_authenticated (connection->transport);
   2959   CONNECTION_UNLOCK (connection);
   2960 
   2961   return res;
   2962 }
   2963 
   2964 /**
   2965  * Gets whether the connection is not authenticated as a specific
   2966  * user.  If the connection is not authenticated, this function
   2967  * returns #TRUE, and if it is authenticated but as an anonymous user,
   2968  * it returns #TRUE.  If it is authenticated as a specific user, then
   2969  * this returns #FALSE. (Note that if the connection was authenticated
   2970  * as anonymous then disconnected, this function still returns #TRUE.)
   2971  *
   2972  * If the connection is not anonymous, you can use
   2973  * dbus_connection_get_unix_user() and
   2974  * dbus_connection_get_windows_user() to see who it's authorized as.
   2975  *
   2976  * If you want to prevent non-anonymous authorization, use
   2977  * dbus_server_set_auth_mechanisms() to remove the mechanisms that
   2978  * allow proving user identity (i.e. only allow the ANONYMOUS
   2979  * mechanism).
   2980  *
   2981  * @param connection the connection
   2982  * @returns #TRUE if not authenticated or authenticated as anonymous
   2983  */
   2984 dbus_bool_t
   2985 dbus_connection_get_is_anonymous (DBusConnection *connection)
   2986 {
   2987   dbus_bool_t res;
   2988 
   2989   _dbus_return_val_if_fail (connection != NULL, FALSE);
   2990 
   2991   CONNECTION_LOCK (connection);
   2992   res = _dbus_transport_get_is_anonymous (connection->transport);
   2993   CONNECTION_UNLOCK (connection);
   2994 
   2995   return res;
   2996 }
   2997 
   2998 /**
   2999  * Gets the ID of the server address we are authenticated to, if this
   3000  * connection is on the client side. If the connection is on the
   3001  * server side, this will always return #NULL - use dbus_server_get_id()
   3002  * to get the ID of your own server, if you are the server side.
   3003  *
   3004  * If a client-side connection is not authenticated yet, the ID may be
   3005  * available if it was included in the server address, but may not be
   3006  * available. The only way to be sure the server ID is available
   3007  * is to wait for authentication to complete.
   3008  *
   3009  * In general, each mode of connecting to a given server will have
   3010  * its own ID. So for example, if the session bus daemon is listening
   3011  * on UNIX domain sockets and on TCP, then each of those modalities
   3012  * will have its own server ID.
   3013  *
   3014  * If you want an ID that identifies an entire session bus, look at
   3015  * dbus_bus_get_id() instead (which is just a convenience wrapper
   3016  * around the org.freedesktop.DBus.GetId method invoked on the bus).
   3017  *
   3018  * You can also get a machine ID; see dbus_get_local_machine_id() to
   3019  * get the machine you are on.  There isn't a convenience wrapper, but
   3020  * you can invoke org.freedesktop.DBus.Peer.GetMachineId on any peer
   3021  * to get the machine ID on the other end.
   3022  *
   3023  * The D-Bus specification describes the server ID and other IDs in a
   3024  * bit more detail.
   3025  *
   3026  * @param connection the connection
   3027  * @returns the server ID or #NULL if no memory or the connection is server-side
   3028  */
   3029 char*
   3030 dbus_connection_get_server_id (DBusConnection *connection)
   3031 {
   3032   char *id;
   3033 
   3034   _dbus_return_val_if_fail (connection != NULL, NULL);
   3035 
   3036   CONNECTION_LOCK (connection);
   3037   id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
   3038   CONNECTION_UNLOCK (connection);
   3039 
   3040   return id;
   3041 }
   3042 
   3043 /**
   3044  * Tests whether a certain type can be send via the connection. This
   3045  * will always return TRUE for all types, with the exception of
   3046  * DBUS_TYPE_UNIX_FD. The function will return TRUE for
   3047  * DBUS_TYPE_UNIX_FD only on systems that know Unix file descriptors
   3048  * and can send them via the chosen transport and when the remote side
   3049  * supports this.
   3050  *
   3051  * This function can be used to do runtime checking for types that
   3052  * might be unknown to the specific D-Bus client implementation
   3053  * version, i.e. it will return FALSE for all types this
   3054  * implementation does not know, including invalid or reserved types.
   3055  *
   3056  * @param connection the connection
   3057  * @param type the type to check
   3058  * @returns TRUE if the type may be send via the connection
   3059  */
   3060 dbus_bool_t
   3061 dbus_connection_can_send_type(DBusConnection *connection,
   3062                                   int type)
   3063 {
   3064   _dbus_return_val_if_fail (connection != NULL, FALSE);
   3065 
   3066   if (!dbus_type_is_valid (type))
   3067     return FALSE;
   3068 
   3069   if (type != DBUS_TYPE_UNIX_FD)
   3070     return TRUE;
   3071 
   3072 #ifdef HAVE_UNIX_FD_PASSING
   3073   {
   3074     dbus_bool_t b;
   3075 
   3076     CONNECTION_LOCK(connection);
   3077     b = _dbus_transport_can_pass_unix_fd(connection->transport);
   3078     CONNECTION_UNLOCK(connection);
   3079 
   3080     return b;
   3081   }
   3082 #endif
   3083 
   3084   return FALSE;
   3085 }
   3086 
   3087 /**
   3088  * Set whether _exit() should be called when the connection receives a
   3089  * disconnect signal. The call to _exit() comes after any handlers for
   3090  * the disconnect signal run; handlers can cancel the exit by calling
   3091  * this function.
   3092  *
   3093  * By default, exit_on_disconnect is #FALSE; but for message bus
   3094  * connections returned from dbus_bus_get() it will be toggled on
   3095  * by default.
   3096  *
   3097  * @param connection the connection
   3098  * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
   3099  */
   3100 void
   3101 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
   3102                                         dbus_bool_t     exit_on_disconnect)
   3103 {
   3104   _dbus_return_if_fail (connection != NULL);
   3105 
   3106   CONNECTION_LOCK (connection);
   3107   connection->exit_on_disconnect = exit_on_disconnect != FALSE;
   3108   CONNECTION_UNLOCK (connection);
   3109 }
   3110 
   3111 /**
   3112  * Preallocates resources needed to send a message, allowing the message
   3113  * to be sent without the possibility of memory allocation failure.
   3114  * Allows apps to create a future guarantee that they can send
   3115  * a message regardless of memory shortages.
   3116  *
   3117  * @param connection the connection we're preallocating for.
   3118  * @returns the preallocated resources, or #NULL
   3119  */
   3120 DBusPreallocatedSend*
   3121 dbus_connection_preallocate_send (DBusConnection *connection)
   3122 {
   3123   DBusPreallocatedSend *preallocated;
   3124 
   3125   _dbus_return_val_if_fail (connection != NULL, NULL);
   3126 
   3127   CONNECTION_LOCK (connection);
   3128 
   3129   preallocated =
   3130     _dbus_connection_preallocate_send_unlocked (connection);
   3131 
   3132   CONNECTION_UNLOCK (connection);
   3133 
   3134   return preallocated;
   3135 }
   3136 
   3137 /**
   3138  * Frees preallocated message-sending resources from
   3139  * dbus_connection_preallocate_send(). Should only
   3140  * be called if the preallocated resources are not used
   3141  * to send a message.
   3142  *
   3143  * @param connection the connection
   3144  * @param preallocated the resources
   3145  */
   3146 void
   3147 dbus_connection_free_preallocated_send (DBusConnection       *connection,
   3148                                         DBusPreallocatedSend *preallocated)
   3149 {
   3150   _dbus_return_if_fail (connection != NULL);
   3151   _dbus_return_if_fail (preallocated != NULL);
   3152   _dbus_return_if_fail (connection == preallocated->connection);
   3153 
   3154   _dbus_list_free_link (preallocated->queue_link);
   3155   _dbus_counter_unref (preallocated->counter_link->data);
   3156   _dbus_list_free_link (preallocated->counter_link);
   3157   dbus_free (preallocated);
   3158 }
   3159 
   3160 /**
   3161  * Sends a message using preallocated resources. This function cannot fail.
   3162  * It works identically to dbus_connection_send() in other respects.
   3163  * Preallocated resources comes from dbus_connection_preallocate_send().
   3164  * This function "consumes" the preallocated resources, they need not
   3165  * be freed separately.
   3166  *
   3167  * @param connection the connection
   3168  * @param preallocated the preallocated resources
   3169  * @param message the message to send
   3170  * @param client_serial return location for client serial assigned to the message
   3171  */
   3172 void
   3173 dbus_connection_send_preallocated (DBusConnection       *connection,
   3174                                    DBusPreallocatedSend *preallocated,
   3175                                    DBusMessage          *message,
   3176                                    dbus_uint32_t        *client_serial)
   3177 {
   3178   _dbus_return_if_fail (connection != NULL);
   3179   _dbus_return_if_fail (preallocated != NULL);
   3180   _dbus_return_if_fail (message != NULL);
   3181   _dbus_return_if_fail (preallocated->connection == connection);
   3182   _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
   3183                         dbus_message_get_member (message) != NULL);
   3184   _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
   3185                         (dbus_message_get_interface (message) != NULL &&
   3186                          dbus_message_get_member (message) != NULL));
   3187 
   3188   CONNECTION_LOCK (connection);
   3189 
   3190 #ifdef HAVE_UNIX_FD_PASSING
   3191 
   3192   if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
   3193       message->n_unix_fds > 0)
   3194     {
   3195       /* Refuse to send fds on a connection that cannot handle
   3196          them. Unfortunately we cannot return a proper error here, so
   3197          the best we can is just return. */
   3198       CONNECTION_UNLOCK (connection);
   3199       return;
   3200     }
   3201 
   3202 #endif
   3203 
   3204   _dbus_connection_send_preallocated_and_unlock (connection,
   3205 						 preallocated,
   3206 						 message, client_serial);
   3207 }
   3208 
   3209 static dbus_bool_t
   3210 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
   3211                                           DBusMessage    *message,
   3212                                           dbus_uint32_t  *client_serial)
   3213 {
   3214   DBusPreallocatedSend *preallocated;
   3215 
   3216   _dbus_assert (connection != NULL);
   3217   _dbus_assert (message != NULL);
   3218 
   3219   preallocated = _dbus_connection_preallocate_send_unlocked (connection);
   3220   if (preallocated == NULL)
   3221     return FALSE;
   3222 
   3223   _dbus_connection_send_preallocated_unlocked_no_update (connection,
   3224                                                          preallocated,
   3225                                                          message,
   3226                                                          client_serial);
   3227   return TRUE;
   3228 }
   3229 
   3230 /**
   3231  * Adds a message to the outgoing message queue. Does not block to
   3232  * write the message to the network; that happens asynchronously. To
   3233  * force the message to be written, call dbus_connection_flush() however
   3234  * it is not necessary to call dbus_connection_flush() by hand; the
   3235  * message will be sent the next time the main loop is run.
   3236  * dbus_connection_flush() should only be used, for example, if
   3237  * the application was expected to exit before running the main loop.
   3238  *
   3239  * Because this only queues the message, the only reason it can
   3240  * fail is lack of memory. Even if the connection is disconnected,
   3241  * no error will be returned. If the function fails due to lack of memory,
   3242  * it returns #FALSE. The function will never fail for other reasons; even
   3243  * if the connection is disconnected, you can queue an outgoing message,
   3244  * though obviously it won't be sent.
   3245  *
   3246  * The message serial is used by the remote application to send a
   3247  * reply; see dbus_message_get_serial() or the D-Bus specification.
   3248  *
   3249  * dbus_message_unref() can be called as soon as this method returns
   3250  * as the message queue will hold its own ref until the message is sent.
   3251  *
   3252  * @param connection the connection.
   3253  * @param message the message to write.
   3254  * @param serial return location for message serial, or #NULL if you don't care
   3255  * @returns #TRUE on success.
   3256  */
   3257 dbus_bool_t
   3258 dbus_connection_send (DBusConnection *connection,
   3259                       DBusMessage    *message,
   3260                       dbus_uint32_t  *serial)
   3261 {
   3262   _dbus_return_val_if_fail (connection != NULL, FALSE);
   3263   _dbus_return_val_if_fail (message != NULL, FALSE);
   3264 
   3265   CONNECTION_LOCK (connection);
   3266 
   3267 #ifdef HAVE_UNIX_FD_PASSING
   3268 
   3269   if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
   3270       message->n_unix_fds > 0)
   3271     {
   3272       /* Refuse to send fds on a connection that cannot handle
   3273          them. Unfortunately we cannot return a proper error here, so
   3274          the best we can is just return. */
   3275       CONNECTION_UNLOCK (connection);
   3276       return FALSE;
   3277     }
   3278 
   3279 #endif
   3280 
   3281   return _dbus_connection_send_and_unlock (connection,
   3282 					   message,
   3283 					   serial);
   3284 }
   3285 
   3286 static dbus_bool_t
   3287 reply_handler_timeout (void *data)
   3288 {
   3289   DBusConnection *connection;
   3290   DBusDispatchStatus status;
   3291   DBusPendingCall *pending = data;
   3292 
   3293   connection = _dbus_pending_call_get_connection_and_lock (pending);
   3294   _dbus_connection_ref_unlocked (connection);
   3295 
   3296   _dbus_pending_call_queue_timeout_error_unlocked (pending,
   3297                                                    connection);
   3298   _dbus_connection_remove_timeout_unlocked (connection,
   3299 				            _dbus_pending_call_get_timeout_unlocked (pending));
   3300   _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
   3301 
   3302   _dbus_verbose ("middle\n");
   3303   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   3304 
   3305   /* Unlocks, and calls out to user code */
   3306   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   3307   dbus_connection_unref (connection);
   3308 
   3309   return TRUE;
   3310 }
   3311 
   3312 /**
   3313  * Queues a message to send, as with dbus_connection_send(),
   3314  * but also returns a #DBusPendingCall used to receive a reply to the
   3315  * message. If no reply is received in the given timeout_milliseconds,
   3316  * this function expires the pending reply and generates a synthetic
   3317  * error reply (generated in-process, not by the remote application)
   3318  * indicating that a timeout occurred.
   3319  *
   3320  * A #DBusPendingCall will see a reply message before any filters or
   3321  * registered object path handlers. See dbus_connection_dispatch() for
   3322  * details on when handlers are run.
   3323  *
   3324  * A #DBusPendingCall will always see exactly one reply message,
   3325  * unless it's cancelled with dbus_pending_call_cancel().
   3326  *
   3327  * If #NULL is passed for the pending_return, the #DBusPendingCall
   3328  * will still be generated internally, and used to track
   3329  * the message reply timeout. This means a timeout error will
   3330  * occur if no reply arrives, unlike with dbus_connection_send().
   3331  *
   3332  * If -1 is passed for the timeout, a sane default timeout is used. -1
   3333  * is typically the best value for the timeout for this reason, unless
   3334  * you want a very short or very long timeout.  If #DBUS_TIMEOUT_INFINITE is
   3335  * passed for the timeout, no timeout will be set and the call will block
   3336  * forever.
   3337  *
   3338  * @warning if the connection is disconnected or you try to send Unix
   3339  * file descriptors on a connection that does not support them, the
   3340  * #DBusPendingCall will be set to #NULL, so be careful with this.
   3341  *
   3342  * @param connection the connection
   3343  * @param message the message to send
   3344  * @param pending_return return location for a #DBusPendingCall
   3345  * object, or #NULL if connection is disconnected or when you try to
   3346  * send Unix file descriptors on a connection that does not support
   3347  * them.
   3348  * @param timeout_milliseconds timeout in milliseconds, -1 (or
   3349  *  #DBUS_TIMEOUT_USE_DEFAULT) for default or #DBUS_TIMEOUT_INFINITE for no
   3350  *  timeout
   3351  * @returns #FALSE if no memory, #TRUE otherwise.
   3352  *
   3353  */
   3354 dbus_bool_t
   3355 dbus_connection_send_with_reply (DBusConnection     *connection,
   3356                                  DBusMessage        *message,
   3357                                  DBusPendingCall   **pending_return,
   3358                                  int                 timeout_milliseconds)
   3359 {
   3360   DBusPendingCall *pending;
   3361   dbus_int32_t serial = -1;
   3362   DBusDispatchStatus status;
   3363 
   3364   _dbus_return_val_if_fail (connection != NULL, FALSE);
   3365   _dbus_return_val_if_fail (message != NULL, FALSE);
   3366   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
   3367 
   3368   if (pending_return)
   3369     *pending_return = NULL;
   3370 
   3371   CONNECTION_LOCK (connection);
   3372 
   3373 #ifdef HAVE_UNIX_FD_PASSING
   3374 
   3375   if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
   3376       message->n_unix_fds > 0)
   3377     {
   3378       /* Refuse to send fds on a connection that cannot handle
   3379          them. Unfortunately we cannot return a proper error here, so
   3380          the best we can do is return TRUE but leave *pending_return
   3381          as NULL. */
   3382       CONNECTION_UNLOCK (connection);
   3383       return TRUE;
   3384     }
   3385 
   3386 #endif
   3387 
   3388    if (!_dbus_connection_get_is_connected_unlocked (connection))
   3389     {
   3390       CONNECTION_UNLOCK (connection);
   3391 
   3392       return TRUE;
   3393     }
   3394 
   3395   pending = _dbus_pending_call_new_unlocked (connection,
   3396                                              timeout_milliseconds,
   3397                                              reply_handler_timeout);
   3398 
   3399   if (pending == NULL)
   3400     {
   3401       CONNECTION_UNLOCK (connection);
   3402       return FALSE;
   3403     }
   3404 
   3405   /* Assign a serial to the message */
   3406   serial = dbus_message_get_serial (message);
   3407   if (serial == 0)
   3408     {
   3409       serial = _dbus_connection_get_next_client_serial (connection);
   3410       dbus_message_set_serial (message, serial);
   3411     }
   3412 
   3413   if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
   3414     goto error;
   3415 
   3416   /* Insert the serial in the pending replies hash;
   3417    * hash takes a refcount on DBusPendingCall.
   3418    * Also, add the timeout.
   3419    */
   3420   if (!_dbus_connection_attach_pending_call_unlocked (connection,
   3421 						      pending))
   3422     goto error;
   3423 
   3424   if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
   3425     {
   3426       _dbus_connection_detach_pending_call_and_unlock (connection,
   3427 						       pending);
   3428       goto error_unlocked;
   3429     }
   3430 
   3431   if (pending_return)
   3432     *pending_return = pending; /* hand off refcount */
   3433   else
   3434     {
   3435       _dbus_connection_detach_pending_call_unlocked (connection, pending);
   3436       /* we still have a ref to the pending call in this case, we unref
   3437        * after unlocking, below
   3438        */
   3439     }
   3440 
   3441   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   3442 
   3443   /* this calls out to user code */
   3444   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   3445 
   3446   if (pending_return == NULL)
   3447     dbus_pending_call_unref (pending);
   3448 
   3449   return TRUE;
   3450 
   3451  error:
   3452   CONNECTION_UNLOCK (connection);
   3453  error_unlocked:
   3454   dbus_pending_call_unref (pending);
   3455   return FALSE;
   3456 }
   3457 
   3458 /**
   3459  * Sends a message and blocks a certain time period while waiting for
   3460  * a reply.  This function does not reenter the main loop,
   3461  * i.e. messages other than the reply are queued up but not
   3462  * processed. This function is used to invoke method calls on a
   3463  * remote object.
   3464  *
   3465  * If a normal reply is received, it is returned, and removed from the
   3466  * incoming message queue. If it is not received, #NULL is returned
   3467  * and the error is set to #DBUS_ERROR_NO_REPLY.  If an error reply is
   3468  * received, it is converted to a #DBusError and returned as an error,
   3469  * then the reply message is deleted and #NULL is returned. If
   3470  * something else goes wrong, result is set to whatever is
   3471  * appropriate, such as #DBUS_ERROR_NO_MEMORY or
   3472  * #DBUS_ERROR_DISCONNECTED.
   3473  *
   3474  * @warning While this function blocks the calling thread will not be
   3475  * processing the incoming message queue. This means you can end up
   3476  * deadlocked if the application you're talking to needs you to reply
   3477  * to a method. To solve this, either avoid the situation, block in a
   3478  * separate thread from the main connection-dispatching thread, or use
   3479  * dbus_pending_call_set_notify() to avoid blocking.
   3480  *
   3481  * @param connection the connection
   3482  * @param message the message to send
   3483  * @param timeout_milliseconds timeout in milliseconds, -1 (or
   3484  *  #DBUS_TIMEOUT_USE_DEFAULT) for default or #DBUS_TIMEOUT_INFINITE for no
   3485  *  timeout
   3486  * @param error return location for error message
   3487  * @returns the message that is the reply or #NULL with an error code if the
   3488  * function fails.
   3489  */
   3490 DBusMessage*
   3491 dbus_connection_send_with_reply_and_block (DBusConnection     *connection,
   3492                                            DBusMessage        *message,
   3493                                            int                 timeout_milliseconds,
   3494                                            DBusError          *error)
   3495 {
   3496   DBusMessage *reply;
   3497   DBusPendingCall *pending;
   3498 
   3499   _dbus_return_val_if_fail (connection != NULL, NULL);
   3500   _dbus_return_val_if_fail (message != NULL, NULL);
   3501   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
   3502   _dbus_return_val_if_error_is_set (error, NULL);
   3503 
   3504 #ifdef HAVE_UNIX_FD_PASSING
   3505 
   3506   CONNECTION_LOCK (connection);
   3507   if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
   3508       message->n_unix_fds > 0)
   3509     {
   3510       CONNECTION_UNLOCK (connection);
   3511       dbus_set_error(error, DBUS_ERROR_FAILED, "Cannot send file descriptors on this connection.");
   3512       return NULL;
   3513     }
   3514   CONNECTION_UNLOCK (connection);
   3515 
   3516 #endif
   3517 
   3518   if (!dbus_connection_send_with_reply (connection, message,
   3519                                         &pending, timeout_milliseconds))
   3520     {
   3521       _DBUS_SET_OOM (error);
   3522       return NULL;
   3523     }
   3524 
   3525   if (pending == NULL)
   3526     {
   3527       dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
   3528       return NULL;
   3529     }
   3530 
   3531   dbus_pending_call_block (pending);
   3532 
   3533   reply = dbus_pending_call_steal_reply (pending);
   3534   dbus_pending_call_unref (pending);
   3535 
   3536   /* call_complete_and_unlock() called from pending_call_block() should
   3537    * always fill this in.
   3538    */
   3539   _dbus_assert (reply != NULL);
   3540 
   3541    if (dbus_set_error_from_message (error, reply))
   3542     {
   3543       dbus_message_unref (reply);
   3544       return NULL;
   3545     }
   3546   else
   3547     return reply;
   3548 }
   3549 
   3550 /**
   3551  * Blocks until the outgoing message queue is empty.
   3552  * Assumes connection lock already held.
   3553  *
   3554  * If you call this, you MUST call update_dispatch_status afterword...
   3555  *
   3556  * @param connection the connection.
   3557  */
   3558 static DBusDispatchStatus
   3559 _dbus_connection_flush_unlocked (DBusConnection *connection)
   3560 {
   3561   /* We have to specify DBUS_ITERATION_DO_READING here because
   3562    * otherwise we could have two apps deadlock if they are both doing
   3563    * a flush(), and the kernel buffers fill up. This could change the
   3564    * dispatch status.
   3565    */
   3566   DBusDispatchStatus status;
   3567 
   3568   HAVE_LOCK_CHECK (connection);
   3569 
   3570   while (connection->n_outgoing > 0 &&
   3571          _dbus_connection_get_is_connected_unlocked (connection))
   3572     {
   3573       _dbus_verbose ("doing iteration in\n");
   3574       HAVE_LOCK_CHECK (connection);
   3575       _dbus_connection_do_iteration_unlocked (connection,
   3576                                               NULL,
   3577                                               DBUS_ITERATION_DO_READING |
   3578                                               DBUS_ITERATION_DO_WRITING |
   3579                                               DBUS_ITERATION_BLOCK,
   3580                                               -1);
   3581     }
   3582 
   3583   HAVE_LOCK_CHECK (connection);
   3584   _dbus_verbose ("middle\n");
   3585   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   3586 
   3587   HAVE_LOCK_CHECK (connection);
   3588   return status;
   3589 }
   3590 
   3591 /**
   3592  * Blocks until the outgoing message queue is empty.
   3593  *
   3594  * @param connection the connection.
   3595  */
   3596 void
   3597 dbus_connection_flush (DBusConnection *connection)
   3598 {
   3599   /* We have to specify DBUS_ITERATION_DO_READING here because
   3600    * otherwise we could have two apps deadlock if they are both doing
   3601    * a flush(), and the kernel buffers fill up. This could change the
   3602    * dispatch status.
   3603    */
   3604   DBusDispatchStatus status;
   3605 
   3606   _dbus_return_if_fail (connection != NULL);
   3607 
   3608   CONNECTION_LOCK (connection);
   3609 
   3610   status = _dbus_connection_flush_unlocked (connection);
   3611 
   3612   HAVE_LOCK_CHECK (connection);
   3613   /* Unlocks and calls out to user code */
   3614   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   3615 
   3616   _dbus_verbose ("end\n");
   3617 }
   3618 
   3619 /**
   3620  * This function implements dbus_connection_read_write_dispatch() and
   3621  * dbus_connection_read_write() (they pass a different value for the
   3622  * dispatch parameter).
   3623  *
   3624  * @param connection the connection
   3625  * @param timeout_milliseconds max time to block or -1 for infinite
   3626  * @param dispatch dispatch new messages or leave them on the incoming queue
   3627  * @returns #TRUE if the disconnect message has not been processed
   3628  */
   3629 static dbus_bool_t
   3630 _dbus_connection_read_write_dispatch (DBusConnection *connection,
   3631                                      int             timeout_milliseconds,
   3632                                      dbus_bool_t     dispatch)
   3633 {
   3634   DBusDispatchStatus dstatus;
   3635   dbus_bool_t progress_possible;
   3636 
   3637   /* Need to grab a ref here in case we're a private connection and
   3638    * the user drops the last ref in a handler we call; see bug
   3639    * https://bugs.freedesktop.org/show_bug.cgi?id=15635
   3640    */
   3641   dbus_connection_ref (connection);
   3642   dstatus = dbus_connection_get_dispatch_status (connection);
   3643 
   3644   if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
   3645     {
   3646       _dbus_verbose ("doing dispatch\n");
   3647       dbus_connection_dispatch (connection);
   3648       CONNECTION_LOCK (connection);
   3649     }
   3650   else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
   3651     {
   3652       _dbus_verbose ("pausing for memory\n");
   3653       _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
   3654       CONNECTION_LOCK (connection);
   3655     }
   3656   else
   3657     {
   3658       CONNECTION_LOCK (connection);
   3659       if (_dbus_connection_get_is_connected_unlocked (connection))
   3660         {
   3661           _dbus_verbose ("doing iteration\n");
   3662           _dbus_connection_do_iteration_unlocked (connection,
   3663                                                   NULL,
   3664                                                   DBUS_ITERATION_DO_READING |
   3665                                                   DBUS_ITERATION_DO_WRITING |
   3666                                                   DBUS_ITERATION_BLOCK,
   3667                                                   timeout_milliseconds);
   3668         }
   3669     }
   3670 
   3671   HAVE_LOCK_CHECK (connection);
   3672   /* If we can dispatch, we can make progress until the Disconnected message
   3673    * has been processed; if we can only read/write, we can make progress
   3674    * as long as the transport is open.
   3675    */
   3676   if (dispatch)
   3677     progress_possible = connection->n_incoming != 0 ||
   3678       connection->disconnect_message_link != NULL;
   3679   else
   3680     progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
   3681 
   3682   CONNECTION_UNLOCK (connection);
   3683 
   3684   dbus_connection_unref (connection);
   3685 
   3686   return progress_possible; /* TRUE if we can make more progress */
   3687 }
   3688 
   3689 
   3690 /**
   3691  * This function is intended for use with applications that don't want
   3692  * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
   3693  * example usage would be:
   3694  *
   3695  * @code
   3696  *   while (dbus_connection_read_write_dispatch (connection, -1))
   3697  *     ; // empty loop body
   3698  * @endcode
   3699  *
   3700  * In this usage you would normally have set up a filter function to look
   3701  * at each message as it is dispatched. The loop terminates when the last
   3702  * message from the connection (the disconnected signal) is processed.
   3703  *
   3704  * If there are messages to dispatch, this function will
   3705  * dbus_connection_dispatch() once, and return. If there are no
   3706  * messages to dispatch, this function will block until it can read or
   3707  * write, then read or write, then return.
   3708  *
   3709  * The way to think of this function is that it either makes some sort
   3710  * of progress, or it blocks. Note that, while it is blocked on I/O, it
   3711  * cannot be interrupted (even by other threads), which makes this function
   3712  * unsuitable for applications that do more than just react to received
   3713  * messages.
   3714  *
   3715  * The return value indicates whether the disconnect message has been
   3716  * processed, NOT whether the connection is connected. This is
   3717  * important because even after disconnecting, you want to process any
   3718  * messages you received prior to the disconnect.
   3719  *
   3720  * @param connection the connection
   3721  * @param timeout_milliseconds max time to block or -1 for infinite
   3722  * @returns #TRUE if the disconnect message has not been processed
   3723  */
   3724 dbus_bool_t
   3725 dbus_connection_read_write_dispatch (DBusConnection *connection,
   3726                                      int             timeout_milliseconds)
   3727 {
   3728   _dbus_return_val_if_fail (connection != NULL, FALSE);
   3729   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
   3730    return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
   3731 }
   3732 
   3733 /**
   3734  * This function is intended for use with applications that don't want to
   3735  * write a main loop and deal with #DBusWatch and #DBusTimeout. See also
   3736  * dbus_connection_read_write_dispatch().
   3737  *
   3738  * As long as the connection is open, this function will block until it can
   3739  * read or write, then read or write, then return #TRUE.
   3740  *
   3741  * If the connection is closed, the function returns #FALSE.
   3742  *
   3743  * The return value indicates whether reading or writing is still
   3744  * possible, i.e. whether the connection is connected.
   3745  *
   3746  * Note that even after disconnection, messages may remain in the
   3747  * incoming queue that need to be
   3748  * processed. dbus_connection_read_write_dispatch() dispatches
   3749  * incoming messages for you; with dbus_connection_read_write() you
   3750  * have to arrange to drain the incoming queue yourself.
   3751  *
   3752  * @param connection the connection
   3753  * @param timeout_milliseconds max time to block or -1 for infinite
   3754  * @returns #TRUE if still connected
   3755  */
   3756 dbus_bool_t
   3757 dbus_connection_read_write (DBusConnection *connection,
   3758                             int             timeout_milliseconds)
   3759 {
   3760   _dbus_return_val_if_fail (connection != NULL, FALSE);
   3761   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
   3762    return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
   3763 }
   3764 
   3765 /* We need to call this anytime we pop the head of the queue, and then
   3766  * update_dispatch_status_and_unlock needs to be called afterward
   3767  * which will "process" the disconnected message and set
   3768  * disconnected_message_processed.
   3769  */
   3770 static void
   3771 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
   3772                                              DBusMessage    *head_of_queue)
   3773 {
   3774   HAVE_LOCK_CHECK (connection);
   3775 
   3776   /* checking that the link is NULL is an optimization to avoid the is_signal call */
   3777   if (connection->disconnect_message_link == NULL &&
   3778       dbus_message_is_signal (head_of_queue,
   3779                               DBUS_INTERFACE_LOCAL,
   3780                               "Disconnected"))
   3781     {
   3782       connection->disconnected_message_arrived = TRUE;
   3783     }
   3784 }
   3785 
   3786 /**
   3787  * Returns the first-received message from the incoming message queue,
   3788  * leaving it in the queue. If the queue is empty, returns #NULL.
   3789  *
   3790  * The caller does not own a reference to the returned message, and
   3791  * must either return it using dbus_connection_return_message() or
   3792  * keep it after calling dbus_connection_steal_borrowed_message(). No
   3793  * one can get at the message while its borrowed, so return it as
   3794  * quickly as possible and don't keep a reference to it after
   3795  * returning it. If you need to keep the message, make a copy of it.
   3796  *
   3797  * dbus_connection_dispatch() will block if called while a borrowed
   3798  * message is outstanding; only one piece of code can be playing with
   3799  * the incoming queue at a time. This function will block if called
   3800  * during a dbus_connection_dispatch().
   3801  *
   3802  * @param connection the connection.
   3803  * @returns next message in the incoming queue.
   3804  */
   3805 DBusMessage*
   3806 dbus_connection_borrow_message (DBusConnection *connection)
   3807 {
   3808   DBusDispatchStatus status;
   3809   DBusMessage *message;
   3810 
   3811   _dbus_return_val_if_fail (connection != NULL, NULL);
   3812 
   3813   _dbus_verbose ("start\n");
   3814 
   3815   /* this is called for the side effect that it queues
   3816    * up any messages from the transport
   3817    */
   3818   status = dbus_connection_get_dispatch_status (connection);
   3819   if (status != DBUS_DISPATCH_DATA_REMAINS)
   3820     return NULL;
   3821 
   3822   CONNECTION_LOCK (connection);
   3823 
   3824   _dbus_connection_acquire_dispatch (connection);
   3825 
   3826   /* While a message is outstanding, the dispatch lock is held */
   3827   _dbus_assert (connection->message_borrowed == NULL);
   3828 
   3829   connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
   3830 
   3831   message = connection->message_borrowed;
   3832 
   3833   check_disconnected_message_arrived_unlocked (connection, message);
   3834 
   3835   /* Note that we KEEP the dispatch lock until the message is returned */
   3836   if (message == NULL)
   3837     _dbus_connection_release_dispatch (connection);
   3838 
   3839   CONNECTION_UNLOCK (connection);
   3840 
   3841   _dbus_message_trace_ref (message, -1, -1, "dbus_connection_borrow_message");
   3842 
   3843   /* We don't update dispatch status until it's returned or stolen */
   3844 
   3845   return message;
   3846 }
   3847 
   3848 /**
   3849  * Used to return a message after peeking at it using
   3850  * dbus_connection_borrow_message(). Only called if
   3851  * message from dbus_connection_borrow_message() was non-#NULL.
   3852  *
   3853  * @param connection the connection
   3854  * @param message the message from dbus_connection_borrow_message()
   3855  */
   3856 void
   3857 dbus_connection_return_message (DBusConnection *connection,
   3858 				DBusMessage    *message)
   3859 {
   3860   DBusDispatchStatus status;
   3861 
   3862   _dbus_return_if_fail (connection != NULL);
   3863   _dbus_return_if_fail (message != NULL);
   3864   _dbus_return_if_fail (message == connection->message_borrowed);
   3865   _dbus_return_if_fail (connection->dispatch_acquired);
   3866 
   3867   CONNECTION_LOCK (connection);
   3868 
   3869   _dbus_assert (message == connection->message_borrowed);
   3870 
   3871   connection->message_borrowed = NULL;
   3872 
   3873   _dbus_connection_release_dispatch (connection);
   3874 
   3875   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   3876   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   3877 
   3878   _dbus_message_trace_ref (message, -1, -1, "dbus_connection_return_message");
   3879 }
   3880 
   3881 /**
   3882  * Used to keep a message after peeking at it using
   3883  * dbus_connection_borrow_message(). Before using this function, see
   3884  * the caveats/warnings in the documentation for
   3885  * dbus_connection_pop_message().
   3886  *
   3887  * @param connection the connection
   3888  * @param message the message from dbus_connection_borrow_message()
   3889  */
   3890 void
   3891 dbus_connection_steal_borrowed_message (DBusConnection *connection,
   3892 					DBusMessage    *message)
   3893 {
   3894   DBusMessage *pop_message;
   3895   DBusDispatchStatus status;
   3896 
   3897   _dbus_return_if_fail (connection != NULL);
   3898   _dbus_return_if_fail (message != NULL);
   3899   _dbus_return_if_fail (message == connection->message_borrowed);
   3900   _dbus_return_if_fail (connection->dispatch_acquired);
   3901 
   3902   CONNECTION_LOCK (connection);
   3903 
   3904   _dbus_assert (message == connection->message_borrowed);
   3905 
   3906   pop_message = _dbus_list_pop_first (&connection->incoming_messages);
   3907   _dbus_assert (message == pop_message);
   3908   (void) pop_message; /* unused unless asserting */
   3909 
   3910   connection->n_incoming -= 1;
   3911 
   3912   _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
   3913 		 message, connection->n_incoming);
   3914 
   3915   connection->message_borrowed = NULL;
   3916 
   3917   _dbus_connection_release_dispatch (connection);
   3918 
   3919   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   3920   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   3921   _dbus_message_trace_ref (message, -1, -1,
   3922       "dbus_connection_steal_borrowed_message");
   3923 }
   3924 
   3925 /* See dbus_connection_pop_message, but requires the caller to own
   3926  * the lock before calling. May drop the lock while running.
   3927  */
   3928 static DBusList*
   3929 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
   3930 {
   3931   HAVE_LOCK_CHECK (connection);
   3932 
   3933   _dbus_assert (connection->message_borrowed == NULL);
   3934 
   3935   if (connection->n_incoming > 0)
   3936     {
   3937       DBusList *link;
   3938 
   3939       link = _dbus_list_pop_first_link (&connection->incoming_messages);
   3940       connection->n_incoming -= 1;
   3941 
   3942       _dbus_verbose ("Message %p (%s %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
   3943                      link->data,
   3944                      dbus_message_type_to_string (dbus_message_get_type (link->data)),
   3945                      dbus_message_get_path (link->data) ?
   3946                      dbus_message_get_path (link->data) :
   3947                      "no path",
   3948                      dbus_message_get_interface (link->data) ?
   3949                      dbus_message_get_interface (link->data) :
   3950                      "no interface",
   3951                      dbus_message_get_member (link->data) ?
   3952                      dbus_message_get_member (link->data) :
   3953                      "no member",
   3954                      dbus_message_get_signature (link->data),
   3955                      connection, connection->n_incoming);
   3956 
   3957       _dbus_message_trace_ref (link->data, -1, -1,
   3958           "_dbus_connection_pop_message_link_unlocked");
   3959 
   3960       check_disconnected_message_arrived_unlocked (connection, link->data);
   3961 
   3962       return link;
   3963     }
   3964   else
   3965     return NULL;
   3966 }
   3967 
   3968 /* See dbus_connection_pop_message, but requires the caller to own
   3969  * the lock before calling. May drop the lock while running.
   3970  */
   3971 static DBusMessage*
   3972 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
   3973 {
   3974   DBusList *link;
   3975 
   3976   HAVE_LOCK_CHECK (connection);
   3977 
   3978   link = _dbus_connection_pop_message_link_unlocked (connection);
   3979 
   3980   if (link != NULL)
   3981     {
   3982       DBusMessage *message;
   3983 
   3984       message = link->data;
   3985 
   3986       _dbus_list_free_link (link);
   3987 
   3988       return message;
   3989     }
   3990   else
   3991     return NULL;
   3992 }
   3993 
   3994 static void
   3995 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
   3996                                                 DBusList       *message_link)
   3997 {
   3998   HAVE_LOCK_CHECK (connection);
   3999 
   4000   _dbus_assert (message_link != NULL);
   4001   /* You can't borrow a message while a link is outstanding */
   4002   _dbus_assert (connection->message_borrowed == NULL);
   4003   /* We had to have the dispatch lock across the pop/putback */
   4004   _dbus_assert (connection->dispatch_acquired);
   4005 
   4006   _dbus_list_prepend_link (&connection->incoming_messages,
   4007                            message_link);
   4008   connection->n_incoming += 1;
   4009 
   4010   _dbus_verbose ("Message %p (%s %s %s '%s') put back into queue %p, %d incoming\n",
   4011                  message_link->data,
   4012                  dbus_message_type_to_string (dbus_message_get_type (message_link->data)),
   4013                  dbus_message_get_interface (message_link->data) ?
   4014                  dbus_message_get_interface (message_link->data) :
   4015                  "no interface",
   4016                  dbus_message_get_member (message_link->data) ?
   4017                  dbus_message_get_member (message_link->data) :
   4018                  "no member",
   4019                  dbus_message_get_signature (message_link->data),
   4020                  connection, connection->n_incoming);
   4021 
   4022   _dbus_message_trace_ref (message_link->data, -1, -1,
   4023       "_dbus_connection_putback_message_link_unlocked");
   4024 }
   4025 
   4026 /**
   4027  * Returns the first-received message from the incoming message queue,
   4028  * removing it from the queue. The caller owns a reference to the
   4029  * returned message. If the queue is empty, returns #NULL.
   4030  *
   4031  * This function bypasses any message handlers that are registered,
   4032  * and so using it is usually wrong. Instead, let the main loop invoke
   4033  * dbus_connection_dispatch(). Popping messages manually is only
   4034  * useful in very simple programs that don't share a #DBusConnection
   4035  * with any libraries or other modules.
   4036  *
   4037  * There is a lock that covers all ways of accessing the incoming message
   4038  * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
   4039  * dbus_connection_borrow_message(), etc. will all block while one of the others
   4040  * in the group is running.
   4041  *
   4042  * @param connection the connection.
   4043  * @returns next message in the incoming queue.
   4044  */
   4045 DBusMessage*
   4046 dbus_connection_pop_message (DBusConnection *connection)
   4047 {
   4048   DBusMessage *message;
   4049   DBusDispatchStatus status;
   4050 
   4051   _dbus_verbose ("start\n");
   4052 
   4053   /* this is called for the side effect that it queues
   4054    * up any messages from the transport
   4055    */
   4056   status = dbus_connection_get_dispatch_status (connection);
   4057   if (status != DBUS_DISPATCH_DATA_REMAINS)
   4058     return NULL;
   4059 
   4060   CONNECTION_LOCK (connection);
   4061   _dbus_connection_acquire_dispatch (connection);
   4062   HAVE_LOCK_CHECK (connection);
   4063 
   4064   message = _dbus_connection_pop_message_unlocked (connection);
   4065 
   4066   _dbus_verbose ("Returning popped message %p\n", message);
   4067 
   4068   _dbus_connection_release_dispatch (connection);
   4069 
   4070   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   4071   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   4072 
   4073   return message;
   4074 }
   4075 
   4076 /**
   4077  * Acquire the dispatcher. This is a separate lock so the main
   4078  * connection lock can be dropped to call out to application dispatch
   4079  * handlers.
   4080  *
   4081  * @param connection the connection.
   4082  */
   4083 static void
   4084 _dbus_connection_acquire_dispatch (DBusConnection *connection)
   4085 {
   4086   HAVE_LOCK_CHECK (connection);
   4087 
   4088   _dbus_connection_ref_unlocked (connection);
   4089   CONNECTION_UNLOCK (connection);
   4090 
   4091   _dbus_verbose ("locking dispatch_mutex\n");
   4092   _dbus_cmutex_lock (connection->dispatch_mutex);
   4093 
   4094   while (connection->dispatch_acquired)
   4095     {
   4096       _dbus_verbose ("waiting for dispatch to be acquirable\n");
   4097       _dbus_condvar_wait (connection->dispatch_cond,
   4098                           connection->dispatch_mutex);
   4099     }
   4100 
   4101   _dbus_assert (!connection->dispatch_acquired);
   4102 
   4103   connection->dispatch_acquired = TRUE;
   4104 
   4105   _dbus_verbose ("unlocking dispatch_mutex\n");
   4106   _dbus_cmutex_unlock (connection->dispatch_mutex);
   4107 
   4108   CONNECTION_LOCK (connection);
   4109   _dbus_connection_unref_unlocked (connection);
   4110 }
   4111 
   4112 /**
   4113  * Release the dispatcher when you're done with it. Only call
   4114  * after you've acquired the dispatcher. Wakes up at most one
   4115  * thread currently waiting to acquire the dispatcher.
   4116  *
   4117  * @param connection the connection.
   4118  */
   4119 static void
   4120 _dbus_connection_release_dispatch (DBusConnection *connection)
   4121 {
   4122   HAVE_LOCK_CHECK (connection);
   4123 
   4124   _dbus_verbose ("locking dispatch_mutex\n");
   4125   _dbus_cmutex_lock (connection->dispatch_mutex);
   4126 
   4127   _dbus_assert (connection->dispatch_acquired);
   4128 
   4129   connection->dispatch_acquired = FALSE;
   4130   _dbus_condvar_wake_one (connection->dispatch_cond);
   4131 
   4132   _dbus_verbose ("unlocking dispatch_mutex\n");
   4133   _dbus_cmutex_unlock (connection->dispatch_mutex);
   4134 }
   4135 
   4136 static void
   4137 _dbus_connection_failed_pop (DBusConnection *connection,
   4138 			     DBusList       *message_link)
   4139 {
   4140   _dbus_list_prepend_link (&connection->incoming_messages,
   4141 			   message_link);
   4142   connection->n_incoming += 1;
   4143 }
   4144 
   4145 /* Note this may be called multiple times since we don't track whether we already did it */
   4146 static void
   4147 notify_disconnected_unlocked (DBusConnection *connection)
   4148 {
   4149   HAVE_LOCK_CHECK (connection);
   4150 
   4151   /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
   4152    * connection from dbus_bus_get(). We make the same guarantee for
   4153    * dbus_connection_open() but in a different way since we don't want to
   4154    * unref right here; we instead check for connectedness before returning
   4155    * the connection from the hash.
   4156    */
   4157   _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
   4158 
   4159   /* Dump the outgoing queue, we aren't going to be able to
   4160    * send it now, and we'd like accessors like
   4161    * dbus_connection_get_outgoing_size() to be accurate.
   4162    */
   4163   if (connection->n_outgoing > 0)
   4164     {
   4165       DBusList *link;
   4166 
   4167       _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
   4168                      connection->n_outgoing);
   4169 
   4170       while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
   4171         {
   4172           _dbus_connection_message_sent_unlocked (connection, link->data);
   4173         }
   4174     }
   4175 }
   4176 
   4177 /* Note this may be called multiple times since we don't track whether we already did it */
   4178 static DBusDispatchStatus
   4179 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
   4180 {
   4181   HAVE_LOCK_CHECK (connection);
   4182 
   4183   if (connection->disconnect_message_link != NULL)
   4184     {
   4185       _dbus_verbose ("Sending disconnect message\n");
   4186 
   4187       /* If we have pending calls, queue their timeouts - we want the Disconnected
   4188        * to be the last message, after these timeouts.
   4189        */
   4190       connection_timeout_and_complete_all_pending_calls_unlocked (connection);
   4191 
   4192       /* We haven't sent the disconnect message already,
   4193        * and all real messages have been queued up.
   4194        */
   4195       _dbus_connection_queue_synthesized_message_link (connection,
   4196                                                        connection->disconnect_message_link);
   4197       connection->disconnect_message_link = NULL;
   4198 
   4199       return DBUS_DISPATCH_DATA_REMAINS;
   4200     }
   4201 
   4202   return DBUS_DISPATCH_COMPLETE;
   4203 }
   4204 
   4205 static DBusDispatchStatus
   4206 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
   4207 {
   4208   HAVE_LOCK_CHECK (connection);
   4209 
   4210   if (connection->n_incoming > 0)
   4211     return DBUS_DISPATCH_DATA_REMAINS;
   4212   else if (!_dbus_transport_queue_messages (connection->transport))
   4213     return DBUS_DISPATCH_NEED_MEMORY;
   4214   else
   4215     {
   4216       DBusDispatchStatus status;
   4217       dbus_bool_t is_connected;
   4218 
   4219       status = _dbus_transport_get_dispatch_status (connection->transport);
   4220       is_connected = _dbus_transport_get_is_connected (connection->transport);
   4221 
   4222       _dbus_verbose ("dispatch status = %s is_connected = %d\n",
   4223                      DISPATCH_STATUS_NAME (status), is_connected);
   4224 
   4225       if (!is_connected)
   4226         {
   4227           /* It's possible this would be better done by having an explicit
   4228            * notification from _dbus_transport_disconnect() that would
   4229            * synchronously do this, instead of waiting for the next dispatch
   4230            * status check. However, probably not good to change until it causes
   4231            * a problem.
   4232            */
   4233           notify_disconnected_unlocked (connection);
   4234 
   4235           /* I'm not sure this is needed; the idea is that we want to
   4236            * queue the Disconnected only after we've read all the
   4237            * messages, but if we're disconnected maybe we are guaranteed
   4238            * to have read them all ?
   4239            */
   4240           if (status == DBUS_DISPATCH_COMPLETE)
   4241             status = notify_disconnected_and_dispatch_complete_unlocked (connection);
   4242         }
   4243 
   4244       if (status != DBUS_DISPATCH_COMPLETE)
   4245         return status;
   4246       else if (connection->n_incoming > 0)
   4247         return DBUS_DISPATCH_DATA_REMAINS;
   4248       else
   4249         return DBUS_DISPATCH_COMPLETE;
   4250     }
   4251 }
   4252 
   4253 static void
   4254 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection    *connection,
   4255                                                     DBusDispatchStatus new_status)
   4256 {
   4257   dbus_bool_t changed;
   4258   DBusDispatchStatusFunction function;
   4259   void *data;
   4260 
   4261   HAVE_LOCK_CHECK (connection);
   4262 
   4263   _dbus_connection_ref_unlocked (connection);
   4264 
   4265   changed = new_status != connection->last_dispatch_status;
   4266 
   4267   connection->last_dispatch_status = new_status;
   4268 
   4269   function = connection->dispatch_status_function;
   4270   data = connection->dispatch_status_data;
   4271 
   4272   if (connection->disconnected_message_arrived &&
   4273       !connection->disconnected_message_processed)
   4274     {
   4275       connection->disconnected_message_processed = TRUE;
   4276 
   4277       /* this does an unref, but we have a ref
   4278        * so we should not run the finalizer here
   4279        * inside the lock.
   4280        */
   4281       connection_forget_shared_unlocked (connection);
   4282 
   4283       if (connection->exit_on_disconnect)
   4284         {
   4285           CONNECTION_UNLOCK (connection);
   4286 
   4287           _dbus_verbose ("Exiting on Disconnected signal\n");
   4288           if (raise (SIGTERM) != 0)
   4289             {
   4290               _dbus_verbose ("Failed to raise a SIGTERM signal. Exiting\n");
   4291               _dbus_exit (1);
   4292               _dbus_assert_not_reached ("Call to exit() returned");
   4293             }
   4294           return;
   4295         }
   4296     }
   4297 
   4298   /* We drop the lock */
   4299   CONNECTION_UNLOCK (connection);
   4300 
   4301   if (changed && function)
   4302     {
   4303       _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
   4304                      connection, new_status,
   4305                      DISPATCH_STATUS_NAME (new_status));
   4306       (* function) (connection, new_status, data);
   4307     }
   4308 
   4309   dbus_connection_unref (connection);
   4310 }
   4311 
   4312 /**
   4313  * Gets the current state of the incoming message queue.
   4314  * #DBUS_DISPATCH_DATA_REMAINS indicates that the message queue
   4315  * may contain messages. #DBUS_DISPATCH_COMPLETE indicates that the
   4316  * incoming queue is empty. #DBUS_DISPATCH_NEED_MEMORY indicates that
   4317  * there could be data, but we can't know for sure without more
   4318  * memory.
   4319  *
   4320  * To process the incoming message queue, use dbus_connection_dispatch()
   4321  * or (in rare cases) dbus_connection_pop_message().
   4322  *
   4323  * Note, #DBUS_DISPATCH_DATA_REMAINS really means that either we
   4324  * have messages in the queue, or we have raw bytes buffered up
   4325  * that need to be parsed. When these bytes are parsed, they
   4326  * may not add up to an entire message. Thus, it's possible
   4327  * to see a status of #DBUS_DISPATCH_DATA_REMAINS but not
   4328  * have a message yet.
   4329  *
   4330  * In particular this happens on initial connection, because all sorts
   4331  * of authentication protocol stuff has to be parsed before the
   4332  * first message arrives.
   4333  *
   4334  * @param connection the connection.
   4335  * @returns current dispatch status
   4336  */
   4337 DBusDispatchStatus
   4338 dbus_connection_get_dispatch_status (DBusConnection *connection)
   4339 {
   4340   DBusDispatchStatus status;
   4341 
   4342   _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
   4343 
   4344   _dbus_verbose ("start\n");
   4345 
   4346   CONNECTION_LOCK (connection);
   4347 
   4348   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   4349 
   4350   CONNECTION_UNLOCK (connection);
   4351 
   4352   return status;
   4353 }
   4354 
   4355 /**
   4356  * Filter funtion for handling the Peer standard interface.
   4357  */
   4358 static DBusHandlerResult
   4359 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
   4360                                                  DBusMessage    *message)
   4361 {
   4362   dbus_bool_t sent = FALSE;
   4363   DBusMessage *ret = NULL;
   4364   DBusList *expire_link;
   4365 
   4366   if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
   4367     {
   4368       /* This means we're letting the bus route this message */
   4369       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
   4370     }
   4371 
   4372   if (!dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
   4373     {
   4374       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
   4375     }
   4376 
   4377   /* Preallocate a linked-list link, so that if we need to dispose of a
   4378    * message, we can attach it to the expired list */
   4379   expire_link = _dbus_list_alloc_link (NULL);
   4380 
   4381   if (!expire_link)
   4382     return DBUS_HANDLER_RESULT_NEED_MEMORY;
   4383 
   4384   if (dbus_message_is_method_call (message,
   4385                                    DBUS_INTERFACE_PEER,
   4386                                    "Ping"))
   4387     {
   4388       ret = dbus_message_new_method_return (message);
   4389       if (ret == NULL)
   4390         goto out;
   4391 
   4392       sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
   4393     }
   4394   else if (dbus_message_is_method_call (message,
   4395                                         DBUS_INTERFACE_PEER,
   4396                                         "GetMachineId"))
   4397     {
   4398       DBusString uuid;
   4399 
   4400       ret = dbus_message_new_method_return (message);
   4401       if (ret == NULL)
   4402         goto out;
   4403 
   4404       _dbus_string_init (&uuid);
   4405       if (_dbus_get_local_machine_uuid_encoded (&uuid))
   4406         {
   4407           const char *v_STRING = _dbus_string_get_const_data (&uuid);
   4408           if (dbus_message_append_args (ret,
   4409                                         DBUS_TYPE_STRING, &v_STRING,
   4410                                         DBUS_TYPE_INVALID))
   4411             {
   4412               sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
   4413             }
   4414         }
   4415       _dbus_string_free (&uuid);
   4416     }
   4417   else
   4418     {
   4419       /* We need to bounce anything else with this interface, otherwise apps
   4420        * could start extending the interface and when we added extensions
   4421        * here to DBusConnection we'd break those apps.
   4422        */
   4423       ret = dbus_message_new_error (message,
   4424                                     DBUS_ERROR_UNKNOWN_METHOD,
   4425                                     "Unknown method invoked on org.freedesktop.DBus.Peer interface");
   4426       if (ret == NULL)
   4427         goto out;
   4428 
   4429       sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
   4430     }
   4431 
   4432 out:
   4433   if (ret == NULL)
   4434     {
   4435       _dbus_list_free_link (expire_link);
   4436     }
   4437   else
   4438     {
   4439       /* It'll be safe to unref the reply when we unlock */
   4440       expire_link->data = ret;
   4441       _dbus_list_prepend_link (&connection->expired_messages, expire_link);
   4442     }
   4443 
   4444   if (!sent)
   4445     return DBUS_HANDLER_RESULT_NEED_MEMORY;
   4446 
   4447   return DBUS_HANDLER_RESULT_HANDLED;
   4448 }
   4449 
   4450 /**
   4451 * Processes all builtin filter functions
   4452 *
   4453 * If the spec specifies a standard interface
   4454 * they should be processed from this method
   4455 **/
   4456 static DBusHandlerResult
   4457 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
   4458                                                            DBusMessage    *message)
   4459 {
   4460   /* We just run one filter for now but have the option to run more
   4461      if the spec calls for it in the future */
   4462 
   4463   return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
   4464 }
   4465 
   4466 /**
   4467  * Processes any incoming data.
   4468  *
   4469  * If there's incoming raw data that has not yet been parsed, it is
   4470  * parsed, which may or may not result in adding messages to the
   4471  * incoming queue.
   4472  *
   4473  * The incoming data buffer is filled when the connection reads from
   4474  * its underlying transport (such as a socket).  Reading usually
   4475  * happens in dbus_watch_handle() or dbus_connection_read_write().
   4476  *
   4477  * If there are complete messages in the incoming queue,
   4478  * dbus_connection_dispatch() removes one message from the queue and
   4479  * processes it. Processing has three steps.
   4480  *
   4481  * First, any method replies are passed to #DBusPendingCall or
   4482  * dbus_connection_send_with_reply_and_block() in order to
   4483  * complete the pending method call.
   4484  *
   4485  * Second, any filters registered with dbus_connection_add_filter()
   4486  * are run. If any filter returns #DBUS_HANDLER_RESULT_HANDLED
   4487  * then processing stops after that filter.
   4488  *
   4489  * Third, if the message is a method call it is forwarded to
   4490  * any registered object path handlers added with
   4491  * dbus_connection_register_object_path() or
   4492  * dbus_connection_register_fallback().
   4493  *
   4494  * A single call to dbus_connection_dispatch() will process at most
   4495  * one message; it will not clear the entire message queue.
   4496  *
   4497  * Be careful about calling dbus_connection_dispatch() from inside a
   4498  * message handler, i.e. calling dbus_connection_dispatch()
   4499  * recursively.  If threads have been initialized with a recursive
   4500  * mutex function, then this will not deadlock; however, it can
   4501  * certainly confuse your application.
   4502  *
   4503  * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
   4504  *
   4505  * @param connection the connection
   4506  * @returns dispatch status, see dbus_connection_get_dispatch_status()
   4507  */
   4508 DBusDispatchStatus
   4509 dbus_connection_dispatch (DBusConnection *connection)
   4510 {
   4511   DBusMessage *message;
   4512   DBusList *link, *filter_list_copy, *message_link;
   4513   DBusHandlerResult result;
   4514   DBusPendingCall *pending;
   4515   dbus_int32_t reply_serial;
   4516   DBusDispatchStatus status;
   4517   dbus_bool_t found_object;
   4518 
   4519   _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
   4520 
   4521   _dbus_verbose ("\n");
   4522 
   4523   CONNECTION_LOCK (connection);
   4524   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   4525   if (status != DBUS_DISPATCH_DATA_REMAINS)
   4526     {
   4527       /* unlocks and calls out to user code */
   4528       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   4529       return status;
   4530     }
   4531 
   4532   /* We need to ref the connection since the callback could potentially
   4533    * drop the last ref to it
   4534    */
   4535   _dbus_connection_ref_unlocked (connection);
   4536 
   4537   _dbus_connection_acquire_dispatch (connection);
   4538   HAVE_LOCK_CHECK (connection);
   4539 
   4540   message_link = _dbus_connection_pop_message_link_unlocked (connection);
   4541   if (message_link == NULL)
   4542     {
   4543       /* another thread dispatched our stuff */
   4544 
   4545       _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
   4546 
   4547       _dbus_connection_release_dispatch (connection);
   4548 
   4549       status = _dbus_connection_get_dispatch_status_unlocked (connection);
   4550 
   4551       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   4552 
   4553       dbus_connection_unref (connection);
   4554 
   4555       return status;
   4556     }
   4557 
   4558   message = message_link->data;
   4559 
   4560   _dbus_verbose (" dispatching message %p (%s %s %s '%s')\n",
   4561                  message,
   4562                  dbus_message_type_to_string (dbus_message_get_type (message)),
   4563                  dbus_message_get_interface (message) ?
   4564                  dbus_message_get_interface (message) :
   4565                  "no interface",
   4566                  dbus_message_get_member (message) ?
   4567                  dbus_message_get_member (message) :
   4568                  "no member",
   4569                  dbus_message_get_signature (message));
   4570 
   4571   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
   4572 
   4573   /* Pending call handling must be first, because if you do
   4574    * dbus_connection_send_with_reply_and_block() or
   4575    * dbus_pending_call_block() then no handlers/filters will be run on
   4576    * the reply. We want consistent semantics in the case where we
   4577    * dbus_connection_dispatch() the reply.
   4578    */
   4579 
   4580   reply_serial = dbus_message_get_reply_serial (message);
   4581   pending = _dbus_hash_table_lookup_int (connection->pending_replies,
   4582                                          reply_serial);
   4583   if (pending)
   4584     {
   4585       _dbus_verbose ("Dispatching a pending reply\n");
   4586       complete_pending_call_and_unlock (connection, pending, message);
   4587       pending = NULL; /* it's probably unref'd */
   4588 
   4589       CONNECTION_LOCK (connection);
   4590       _dbus_verbose ("pending call completed in dispatch\n");
   4591       result = DBUS_HANDLER_RESULT_HANDLED;
   4592       goto out;
   4593     }
   4594 
   4595   result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
   4596   if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
   4597     goto out;
   4598 
   4599   if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
   4600     {
   4601       _dbus_connection_release_dispatch (connection);
   4602       HAVE_LOCK_CHECK (connection);
   4603 
   4604       _dbus_connection_failed_pop (connection, message_link);
   4605 
   4606       /* unlocks and calls user code */
   4607       _dbus_connection_update_dispatch_status_and_unlock (connection,
   4608                                                           DBUS_DISPATCH_NEED_MEMORY);
   4609       dbus_connection_unref (connection);
   4610 
   4611       return DBUS_DISPATCH_NEED_MEMORY;
   4612     }
   4613 
   4614   _dbus_list_foreach (&filter_list_copy,
   4615 		      (DBusForeachFunction)_dbus_message_filter_ref,
   4616 		      NULL);
   4617 
   4618   /* We're still protected from dispatch() reentrancy here
   4619    * since we acquired the dispatcher
   4620    */
   4621   CONNECTION_UNLOCK (connection);
   4622 
   4623   link = _dbus_list_get_first_link (&filter_list_copy);
   4624   while (link != NULL)
   4625     {
   4626       DBusMessageFilter *filter = link->data;
   4627       DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
   4628 
   4629       if (filter->function == NULL)
   4630         {
   4631           _dbus_verbose ("  filter was removed in a callback function\n");
   4632           link = next;
   4633           continue;
   4634         }
   4635 
   4636       _dbus_verbose ("  running filter on message %p\n", message);
   4637       result = (* filter->function) (connection, message, filter->user_data);
   4638 
   4639       if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
   4640 	break;
   4641 
   4642       link = next;
   4643     }
   4644 
   4645   _dbus_list_foreach (&filter_list_copy,
   4646 		      (DBusForeachFunction)_dbus_message_filter_unref,
   4647 		      NULL);
   4648   _dbus_list_clear (&filter_list_copy);
   4649 
   4650   CONNECTION_LOCK (connection);
   4651 
   4652   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
   4653     {
   4654       _dbus_verbose ("No memory\n");
   4655       goto out;
   4656     }
   4657   else if (result == DBUS_HANDLER_RESULT_HANDLED)
   4658     {
   4659       _dbus_verbose ("filter handled message in dispatch\n");
   4660       goto out;
   4661     }
   4662 
   4663   /* We're still protected from dispatch() reentrancy here
   4664    * since we acquired the dispatcher
   4665    */
   4666   _dbus_verbose ("  running object path dispatch on message %p (%s %s %s '%s')\n",
   4667                  message,
   4668                  dbus_message_type_to_string (dbus_message_get_type (message)),
   4669                  dbus_message_get_interface (message) ?
   4670                  dbus_message_get_interface (message) :
   4671                  "no interface",
   4672                  dbus_message_get_member (message) ?
   4673                  dbus_message_get_member (message) :
   4674                  "no member",
   4675                  dbus_message_get_signature (message));
   4676 
   4677   HAVE_LOCK_CHECK (connection);
   4678   result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
   4679                                                   message,
   4680                                                   &found_object);
   4681 
   4682   CONNECTION_LOCK (connection);
   4683 
   4684   if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
   4685     {
   4686       _dbus_verbose ("object tree handled message in dispatch\n");
   4687       goto out;
   4688     }
   4689 
   4690   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
   4691     {
   4692       DBusMessage *reply;
   4693       DBusString str;
   4694       DBusPreallocatedSend *preallocated;
   4695       DBusList *expire_link;
   4696 
   4697       _dbus_verbose ("  sending error %s\n",
   4698                      DBUS_ERROR_UNKNOWN_METHOD);
   4699 
   4700       if (!_dbus_string_init (&str))
   4701         {
   4702           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
   4703           _dbus_verbose ("no memory for error string in dispatch\n");
   4704           goto out;
   4705         }
   4706 
   4707       if (!_dbus_string_append_printf (&str,
   4708                                        "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
   4709                                        dbus_message_get_member (message),
   4710                                        dbus_message_get_signature (message),
   4711                                        dbus_message_get_interface (message)))
   4712         {
   4713           _dbus_string_free (&str);
   4714           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
   4715           _dbus_verbose ("no memory for error string in dispatch\n");
   4716           goto out;
   4717         }
   4718 
   4719       reply = dbus_message_new_error (message,
   4720                                       found_object ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_OBJECT,
   4721                                       _dbus_string_get_const_data (&str));
   4722       _dbus_string_free (&str);
   4723 
   4724       if (reply == NULL)
   4725         {
   4726           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
   4727           _dbus_verbose ("no memory for error reply in dispatch\n");
   4728           goto out;
   4729         }
   4730 
   4731       expire_link = _dbus_list_alloc_link (reply);
   4732 
   4733       if (expire_link == NULL)
   4734         {
   4735           dbus_message_unref (reply);
   4736           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
   4737           _dbus_verbose ("no memory for error send in dispatch\n");
   4738           goto out;
   4739         }
   4740 
   4741       preallocated = _dbus_connection_preallocate_send_unlocked (connection);
   4742 
   4743       if (preallocated == NULL)
   4744         {
   4745           _dbus_list_free_link (expire_link);
   4746           /* It's OK that this is finalized, because it hasn't been seen by
   4747            * anything that could attach user callbacks */
   4748           dbus_message_unref (reply);
   4749           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
   4750           _dbus_verbose ("no memory for error send in dispatch\n");
   4751           goto out;
   4752         }
   4753 
   4754       _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
   4755                                                              reply, NULL);
   4756       /* reply will be freed when we release the lock */
   4757       _dbus_list_prepend_link (&connection->expired_messages, expire_link);
   4758 
   4759       result = DBUS_HANDLER_RESULT_HANDLED;
   4760     }
   4761 
   4762   _dbus_verbose ("  done dispatching %p (%s %s %s '%s') on connection %p\n", message,
   4763                  dbus_message_type_to_string (dbus_message_get_type (message)),
   4764                  dbus_message_get_interface (message) ?
   4765                  dbus_message_get_interface (message) :
   4766                  "no interface",
   4767                  dbus_message_get_member (message) ?
   4768                  dbus_message_get_member (message) :
   4769                  "no member",
   4770                  dbus_message_get_signature (message),
   4771                  connection);
   4772 
   4773  out:
   4774   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
   4775     {
   4776       _dbus_verbose ("out of memory\n");
   4777 
   4778       /* Put message back, and we'll start over.
   4779        * Yes this means handlers must be idempotent if they
   4780        * don't return HANDLED; c'est la vie.
   4781        */
   4782       _dbus_connection_putback_message_link_unlocked (connection,
   4783                                                       message_link);
   4784       /* now we don't want to free them */
   4785       message_link = NULL;
   4786       message = NULL;
   4787     }
   4788   else
   4789     {
   4790       _dbus_verbose (" ... done dispatching\n");
   4791     }
   4792 
   4793   _dbus_connection_release_dispatch (connection);
   4794   HAVE_LOCK_CHECK (connection);
   4795 
   4796   if (message != NULL)
   4797     {
   4798       /* We don't want this message to count in maximum message limits when
   4799        * computing the dispatch status, below. We have to drop the lock
   4800        * temporarily, because finalizing a message can trigger callbacks.
   4801        *
   4802        * We have a reference to the connection, and we don't use any cached
   4803        * pointers to the connection's internals below this point, so it should
   4804        * be safe to drop the lock and take it back. */
   4805       CONNECTION_UNLOCK (connection);
   4806       dbus_message_unref (message);
   4807       CONNECTION_LOCK (connection);
   4808     }
   4809 
   4810   if (message_link != NULL)
   4811     _dbus_list_free_link (message_link);
   4812 
   4813   _dbus_verbose ("before final status update\n");
   4814   status = _dbus_connection_get_dispatch_status_unlocked (connection);
   4815 
   4816   /* unlocks and calls user code */
   4817   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
   4818 
   4819   dbus_connection_unref (connection);
   4820 
   4821   return status;
   4822 }
   4823 
   4824 /**
   4825  * Sets the watch functions for the connection. These functions are
   4826  * responsible for making the application's main loop aware of file
   4827  * descriptors that need to be monitored for events, using select() or
   4828  * poll(). When using Qt, typically the DBusAddWatchFunction would
   4829  * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
   4830  * could call g_io_add_watch(), or could be used as part of a more
   4831  * elaborate GSource. Note that when a watch is added, it may
   4832  * not be enabled.
   4833  *
   4834  * The DBusWatchToggledFunction notifies the application that the
   4835  * watch has been enabled or disabled. Call dbus_watch_get_enabled()
   4836  * to check this. A disabled watch should have no effect, and enabled
   4837  * watch should be added to the main loop. This feature is used
   4838  * instead of simply adding/removing the watch because
   4839  * enabling/disabling can be done without memory allocation.  The
   4840  * toggled function may be NULL if a main loop re-queries
   4841  * dbus_watch_get_enabled() every time anyway.
   4842  *
   4843  * The DBusWatch can be queried for the file descriptor to watch using
   4844  * dbus_watch_get_unix_fd() or dbus_watch_get_socket(), and for the
   4845  * events to watch for using dbus_watch_get_flags(). The flags
   4846  * returned by dbus_watch_get_flags() will only contain
   4847  * DBUS_WATCH_READABLE and DBUS_WATCH_WRITABLE, never
   4848  * DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; all watches implicitly
   4849  * include a watch for hangups, errors, and other exceptional
   4850  * conditions.
   4851  *
   4852  * Once a file descriptor becomes readable or writable, or an exception
   4853  * occurs, dbus_watch_handle() should be called to
   4854  * notify the connection of the file descriptor's condition.
   4855  *
   4856  * dbus_watch_handle() cannot be called during the
   4857  * DBusAddWatchFunction, as the connection will not be ready to handle
   4858  * that watch yet.
   4859  *
   4860  * It is not allowed to reference a DBusWatch after it has been passed
   4861  * to remove_function.
   4862  *
   4863  * If #FALSE is returned due to lack of memory, the failure may be due
   4864  * to a #FALSE return from the new add_function. If so, the
   4865  * add_function may have been called successfully one or more times,
   4866  * but the remove_function will also have been called to remove any
   4867  * successful adds. i.e. if #FALSE is returned the net result
   4868  * should be that dbus_connection_set_watch_functions() has no effect,
   4869  * but the add_function and remove_function may have been called.
   4870  *
   4871  * @note The thread lock on DBusConnection is held while
   4872  * watch functions are invoked, so inside these functions you
   4873  * may not invoke any methods on DBusConnection or it will deadlock.
   4874  * See the comments in the code or http://lists.freedesktop.org/archives/dbus/2007-July/tread.html#8144
   4875  * if you encounter this issue and want to attempt writing a patch.
   4876  *
   4877  * @param connection the connection.
   4878  * @param add_function function to begin monitoring a new descriptor.
   4879  * @param remove_function function to stop monitoring a descriptor.
   4880  * @param toggled_function function to notify of enable/disable
   4881  * @param data data to pass to add_function and remove_function.
   4882  * @param free_data_function function to be called to free the data.
   4883  * @returns #FALSE on failure (no memory)
   4884  */
   4885 dbus_bool_t
   4886 dbus_connection_set_watch_functions (DBusConnection              *connection,
   4887                                      DBusAddWatchFunction         add_function,
   4888                                      DBusRemoveWatchFunction      remove_function,
   4889                                      DBusWatchToggledFunction     toggled_function,
   4890                                      void                        *data,
   4891                                      DBusFreeFunction             free_data_function)
   4892 {
   4893   dbus_bool_t retval;
   4894 
   4895   _dbus_return_val_if_fail (connection != NULL, FALSE);
   4896 
   4897   CONNECTION_LOCK (connection);
   4898 
   4899   retval = _dbus_watch_list_set_functions (connection->watches,
   4900                                            add_function, remove_function,
   4901                                            toggled_function,
   4902                                            data, free_data_function);
   4903 
   4904   CONNECTION_UNLOCK (connection);
   4905 
   4906   return retval;
   4907 }
   4908 
   4909 /**
   4910  * Sets the timeout functions for the connection. These functions are
   4911  * responsible for making the application's main loop aware of timeouts.
   4912  * When using Qt, typically the DBusAddTimeoutFunction would create a
   4913  * QTimer. When using GLib, the DBusAddTimeoutFunction would call
   4914  * g_timeout_add.
   4915  *
   4916  * The DBusTimeoutToggledFunction notifies the application that the
   4917  * timeout has been enabled or disabled. Call
   4918  * dbus_timeout_get_enabled() to check this. A disabled timeout should
   4919  * have no effect, and enabled timeout should be added to the main
   4920  * loop. This feature is used instead of simply adding/removing the
   4921  * timeout because enabling/disabling can be done without memory
   4922  * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
   4923  * to enable and disable. The toggled function may be NULL if a main
   4924  * loop re-queries dbus_timeout_get_enabled() every time anyway.
   4925  * Whenever a timeout is toggled, its interval may change.
   4926  *
   4927  * The DBusTimeout can be queried for the timer interval using
   4928  * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
   4929  * repeatedly, each time the interval elapses, starting after it has
   4930  * elapsed once. The timeout stops firing when it is removed with the
   4931  * given remove_function.  The timer interval may change whenever the
   4932  * timeout is added, removed, or toggled.
   4933  *
   4934  * @note The thread lock on DBusConnection is held while
   4935  * timeout functions are invoked, so inside these functions you
   4936  * may not invoke any methods on DBusConnection or it will deadlock.
   4937  * See the comments in the code or http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
   4938  * if you encounter this issue and want to attempt writing a patch.
   4939  *
   4940  * @param connection the connection.
   4941  * @param add_function function to add a timeout.
   4942  * @param remove_function function to remove a timeout.
   4943  * @param toggled_function function to notify of enable/disable
   4944  * @param data data to pass to add_function and remove_function.
   4945  * @param free_data_function function to be called to free the data.
   4946  * @returns #FALSE on failure (no memory)
   4947  */
   4948 dbus_bool_t
   4949 dbus_connection_set_timeout_functions   (DBusConnection            *connection,
   4950 					 DBusAddTimeoutFunction     add_function,
   4951 					 DBusRemoveTimeoutFunction  remove_function,
   4952                                          DBusTimeoutToggledFunction toggled_function,
   4953 					 void                      *data,
   4954 					 DBusFreeFunction           free_data_function)
   4955 {
   4956   dbus_bool_t retval;
   4957 
   4958   _dbus_return_val_if_fail (connection != NULL, FALSE);
   4959 
   4960   CONNECTION_LOCK (connection);
   4961 
   4962   retval = _dbus_timeout_list_set_functions (connection->timeouts,
   4963                                              add_function, remove_function,
   4964                                              toggled_function,
   4965                                              data, free_data_function);
   4966 
   4967   CONNECTION_UNLOCK (connection);
   4968 
   4969   return retval;
   4970 }
   4971 
   4972 /**
   4973  * Sets the mainloop wakeup function for the connection. This function
   4974  * is responsible for waking up the main loop (if its sleeping in
   4975  * another thread) when some some change has happened to the
   4976  * connection that the mainloop needs to reconsider (e.g. a message
   4977  * has been queued for writing).  When using Qt, this typically
   4978  * results in a call to QEventLoop::wakeUp().  When using GLib, it
   4979  * would call g_main_context_wakeup().
   4980  *
   4981  * @param connection the connection.
   4982  * @param wakeup_main_function function to wake up the mainloop
   4983  * @param data data to pass wakeup_main_function
   4984  * @param free_data_function function to be called to free the data.
   4985  */
   4986 void
   4987 dbus_connection_set_wakeup_main_function (DBusConnection            *connection,
   4988 					  DBusWakeupMainFunction     wakeup_main_function,
   4989 					  void                      *data,
   4990 					  DBusFreeFunction           free_data_function)
   4991 {
   4992   void *old_data;
   4993   DBusFreeFunction old_free_data;
   4994 
   4995   _dbus_return_if_fail (connection != NULL);
   4996 
   4997   CONNECTION_LOCK (connection);
   4998   old_data = connection->wakeup_main_data;
   4999   old_free_data = connection->free_wakeup_main_data;
   5000 
   5001   connection->wakeup_main_function = wakeup_main_function;
   5002   connection->wakeup_main_data = data;
   5003   connection->free_wakeup_main_data = free_data_function;
   5004 
   5005   CONNECTION_UNLOCK (connection);
   5006 
   5007   /* Callback outside the lock */
   5008   if (old_free_data)
   5009     (*old_free_data) (old_data);
   5010 }
   5011 
   5012 /**
   5013  * Set a function to be invoked when the dispatch status changes.
   5014  * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
   5015  * dbus_connection_dispatch() needs to be called to process incoming
   5016  * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
   5017  * from inside the DBusDispatchStatusFunction. Indeed, almost
   5018  * any reentrancy in this function is a bad idea. Instead,
   5019  * the DBusDispatchStatusFunction should simply save an indication
   5020  * that messages should be dispatched later, when the main loop
   5021  * is re-entered.
   5022  *
   5023  * If you don't set a dispatch status function, you have to be sure to
   5024  * dispatch on every iteration of your main loop, especially if
   5025  * dbus_watch_handle() or dbus_timeout_handle() were called.
   5026  *
   5027  * @param connection the connection
   5028  * @param function function to call on dispatch status changes
   5029  * @param data data for function
   5030  * @param free_data_function free the function data
   5031  */
   5032 void
   5033 dbus_connection_set_dispatch_status_function (DBusConnection             *connection,
   5034                                               DBusDispatchStatusFunction  function,
   5035                                               void                       *data,
   5036                                               DBusFreeFunction            free_data_function)
   5037 {
   5038   void *old_data;
   5039   DBusFreeFunction old_free_data;
   5040 
   5041   _dbus_return_if_fail (connection != NULL);
   5042 
   5043   CONNECTION_LOCK (connection);
   5044   old_data = connection->dispatch_status_data;
   5045   old_free_data = connection->free_dispatch_status_data;
   5046 
   5047   connection->dispatch_status_function = function;
   5048   connection->dispatch_status_data = data;
   5049   connection->free_dispatch_status_data = free_data_function;
   5050 
   5051   CONNECTION_UNLOCK (connection);
   5052 
   5053   /* Callback outside the lock */
   5054   if (old_free_data)
   5055     (*old_free_data) (old_data);
   5056 }
   5057 
   5058 /**
   5059  * Get the UNIX file descriptor of the connection, if any.  This can
   5060  * be used for SELinux access control checks with getpeercon() for
   5061  * example. DO NOT read or write to the file descriptor, or try to
   5062  * select() on it; use DBusWatch for main loop integration. Not all
   5063  * connections will have a file descriptor. So for adding descriptors
   5064  * to the main loop, use dbus_watch_get_unix_fd() and so forth.
   5065  *
   5066  * If the connection is socket-based, you can also use
   5067  * dbus_connection_get_socket(), which will work on Windows too.
   5068  * This function always fails on Windows.
   5069  *
   5070  * Right now the returned descriptor is always a socket, but
   5071  * that is not guaranteed.
   5072  *
   5073  * @param connection the connection
   5074  * @param fd return location for the file descriptor.
   5075  * @returns #TRUE if fd is successfully obtained.
   5076  */
   5077 dbus_bool_t
   5078 dbus_connection_get_unix_fd (DBusConnection *connection,
   5079                              int            *fd)
   5080 {
   5081   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5082   _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
   5083 
   5084 #ifdef DBUS_WIN
   5085   /* FIXME do this on a lower level */
   5086   return FALSE;
   5087 #endif
   5088 
   5089   return dbus_connection_get_socket(connection, fd);
   5090 }
   5091 
   5092 /**
   5093  * Gets the underlying Windows or UNIX socket file descriptor
   5094  * of the connection, if any. DO NOT read or write to the file descriptor, or try to
   5095  * select() on it; use DBusWatch for main loop integration. Not all
   5096  * connections will have a socket. So for adding descriptors
   5097  * to the main loop, use dbus_watch_get_socket() and so forth.
   5098  *
   5099  * If the connection is not socket-based, this function will return FALSE,
   5100  * even if the connection does have a file descriptor of some kind.
   5101  * i.e. this function always returns specifically a socket file descriptor.
   5102  *
   5103  * @param connection the connection
   5104  * @param fd return location for the file descriptor.
   5105  * @returns #TRUE if fd is successfully obtained.
   5106  */
   5107 dbus_bool_t
   5108 dbus_connection_get_socket(DBusConnection              *connection,
   5109                            int                         *fd)
   5110 {
   5111   dbus_bool_t retval;
   5112 
   5113   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5114   _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
   5115 
   5116   CONNECTION_LOCK (connection);
   5117 
   5118   retval = _dbus_transport_get_socket_fd (connection->transport,
   5119                                           fd);
   5120 
   5121   CONNECTION_UNLOCK (connection);
   5122 
   5123   return retval;
   5124 }
   5125 
   5126 
   5127 /**
   5128  * Gets the UNIX user ID of the connection if known.  Returns #TRUE if
   5129  * the uid is filled in.  Always returns #FALSE on non-UNIX platforms
   5130  * for now, though in theory someone could hook Windows to NIS or
   5131  * something.  Always returns #FALSE prior to authenticating the
   5132  * connection.
   5133  *
   5134  * The UID is only read by servers from clients; clients can't usually
   5135  * get the UID of servers, because servers do not authenticate to
   5136  * clients.  The returned UID is the UID the connection authenticated
   5137  * as.
   5138  *
   5139  * The message bus is a server and the apps connecting to the bus
   5140  * are clients.
   5141  *
   5142  * You can ask the bus to tell you the UID of another connection though
   5143  * if you like; this is done with dbus_bus_get_unix_user().
   5144  *
   5145  * @param connection the connection
   5146  * @param uid return location for the user ID
   5147  * @returns #TRUE if uid is filled in with a valid user ID
   5148  */
   5149 dbus_bool_t
   5150 dbus_connection_get_unix_user (DBusConnection *connection,
   5151                                unsigned long  *uid)
   5152 {
   5153   dbus_bool_t result;
   5154 
   5155   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5156   _dbus_return_val_if_fail (uid != NULL, FALSE);
   5157 
   5158   CONNECTION_LOCK (connection);
   5159 
   5160   if (!_dbus_transport_get_is_authenticated (connection->transport))
   5161     result = FALSE;
   5162   else
   5163     result = _dbus_transport_get_unix_user (connection->transport,
   5164                                             uid);
   5165 
   5166 #ifdef DBUS_WIN
   5167   _dbus_assert (!result);
   5168 #endif
   5169 
   5170   CONNECTION_UNLOCK (connection);
   5171 
   5172   return result;
   5173 }
   5174 
   5175 /**
   5176  * Gets the process ID of the connection if any.
   5177  * Returns #TRUE if the pid is filled in.
   5178  * Always returns #FALSE prior to authenticating the
   5179  * connection.
   5180  *
   5181  * @param connection the connection
   5182  * @param pid return location for the process ID
   5183  * @returns #TRUE if uid is filled in with a valid process ID
   5184  */
   5185 dbus_bool_t
   5186 dbus_connection_get_unix_process_id (DBusConnection *connection,
   5187 				     unsigned long  *pid)
   5188 {
   5189   dbus_bool_t result;
   5190 
   5191   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5192   _dbus_return_val_if_fail (pid != NULL, FALSE);
   5193 
   5194   CONNECTION_LOCK (connection);
   5195 
   5196   if (!_dbus_transport_get_is_authenticated (connection->transport))
   5197     result = FALSE;
   5198   else
   5199     result = _dbus_transport_get_unix_process_id (connection->transport,
   5200 						  pid);
   5201 
   5202   CONNECTION_UNLOCK (connection);
   5203 
   5204   return result;
   5205 }
   5206 
   5207 /**
   5208  * Gets the ADT audit data of the connection if any.
   5209  * Returns #TRUE if the structure pointer is returned.
   5210  * Always returns #FALSE prior to authenticating the
   5211  * connection.
   5212  *
   5213  * @param connection the connection
   5214  * @param data return location for audit data
   5215  * @returns #TRUE if audit data is filled in with a valid ucred pointer
   5216  */
   5217 dbus_bool_t
   5218 dbus_connection_get_adt_audit_session_data (DBusConnection *connection,
   5219 					    void          **data,
   5220 					    dbus_int32_t   *data_size)
   5221 {
   5222   dbus_bool_t result;
   5223 
   5224   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5225   _dbus_return_val_if_fail (data != NULL, FALSE);
   5226   _dbus_return_val_if_fail (data_size != NULL, FALSE);
   5227 
   5228   CONNECTION_LOCK (connection);
   5229 
   5230   if (!_dbus_transport_get_is_authenticated (connection->transport))
   5231     result = FALSE;
   5232   else
   5233     result = _dbus_transport_get_adt_audit_session_data (connection->transport,
   5234 					    	         data,
   5235 			  			         data_size);
   5236   CONNECTION_UNLOCK (connection);
   5237 
   5238   return result;
   5239 }
   5240 
   5241 /**
   5242  * Sets a predicate function used to determine whether a given user ID
   5243  * is allowed to connect. When an incoming connection has
   5244  * authenticated with a particular user ID, this function is called;
   5245  * if it returns #TRUE, the connection is allowed to proceed,
   5246  * otherwise the connection is disconnected.
   5247  *
   5248  * If the function is set to #NULL (as it is by default), then
   5249  * only the same UID as the server process will be allowed to
   5250  * connect. Also, root is always allowed to connect.
   5251  *
   5252  * On Windows, the function will be set and its free_data_function will
   5253  * be invoked when the connection is freed or a new function is set.
   5254  * However, the function will never be called, because there are
   5255  * no UNIX user ids to pass to it, or at least none of the existing
   5256  * auth protocols would allow authenticating as a UNIX user on Windows.
   5257  *
   5258  * @param connection the connection
   5259  * @param function the predicate
   5260  * @param data data to pass to the predicate
   5261  * @param free_data_function function to free the data
   5262  */
   5263 void
   5264 dbus_connection_set_unix_user_function (DBusConnection             *connection,
   5265                                         DBusAllowUnixUserFunction   function,
   5266                                         void                       *data,
   5267                                         DBusFreeFunction            free_data_function)
   5268 {
   5269   void *old_data = NULL;
   5270   DBusFreeFunction old_free_function = NULL;
   5271 
   5272   _dbus_return_if_fail (connection != NULL);
   5273 
   5274   CONNECTION_LOCK (connection);
   5275   _dbus_transport_set_unix_user_function (connection->transport,
   5276                                           function, data, free_data_function,
   5277                                           &old_data, &old_free_function);
   5278   CONNECTION_UNLOCK (connection);
   5279 
   5280   if (old_free_function != NULL)
   5281     (* old_free_function) (old_data);
   5282 }
   5283 
   5284 /**
   5285  * Gets the Windows user SID of the connection if known.  Returns
   5286  * #TRUE if the ID is filled in.  Always returns #FALSE on non-Windows
   5287  * platforms for now, though in theory someone could hook UNIX to
   5288  * Active Directory or something.  Always returns #FALSE prior to
   5289  * authenticating the connection.
   5290  *
   5291  * The user is only read by servers from clients; clients can't usually
   5292  * get the user of servers, because servers do not authenticate to
   5293  * clients. The returned user is the user the connection authenticated
   5294  * as.
   5295  *
   5296  * The message bus is a server and the apps connecting to the bus
   5297  * are clients.
   5298  *
   5299  * The returned user string has to be freed with dbus_free().
   5300  *
   5301  * The return value indicates whether the user SID is available;
   5302  * if it's available but we don't have the memory to copy it,
   5303  * then the return value is #TRUE and #NULL is given as the SID.
   5304  *
   5305  * @todo We would like to be able to say "You can ask the bus to tell
   5306  * you the user of another connection though if you like; this is done
   5307  * with dbus_bus_get_windows_user()." But this has to be implemented
   5308  * in bus/driver.c and dbus/dbus-bus.c, and is pointless anyway
   5309  * since on Windows we only use the session bus for now.
   5310  *
   5311  * @param connection the connection
   5312  * @param windows_sid_p return location for an allocated copy of the user ID, or #NULL if no memory
   5313  * @returns #TRUE if user is available (returned value may be #NULL anyway if no memory)
   5314  */
   5315 dbus_bool_t
   5316 dbus_connection_get_windows_user (DBusConnection             *connection,
   5317                                   char                      **windows_sid_p)
   5318 {
   5319   dbus_bool_t result;
   5320 
   5321   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5322   _dbus_return_val_if_fail (windows_sid_p != NULL, FALSE);
   5323 
   5324   CONNECTION_LOCK (connection);
   5325 
   5326   if (!_dbus_transport_get_is_authenticated (connection->transport))
   5327     result = FALSE;
   5328   else
   5329     result = _dbus_transport_get_windows_user (connection->transport,
   5330                                                windows_sid_p);
   5331 
   5332 #ifdef DBUS_UNIX
   5333   _dbus_assert (!result);
   5334 #endif
   5335 
   5336   CONNECTION_UNLOCK (connection);
   5337 
   5338   return result;
   5339 }
   5340 
   5341 /**
   5342  * Sets a predicate function used to determine whether a given user ID
   5343  * is allowed to connect. When an incoming connection has
   5344  * authenticated with a particular user ID, this function is called;
   5345  * if it returns #TRUE, the connection is allowed to proceed,
   5346  * otherwise the connection is disconnected.
   5347  *
   5348  * If the function is set to #NULL (as it is by default), then
   5349  * only the same user owning the server process will be allowed to
   5350  * connect.
   5351  *
   5352  * On UNIX, the function will be set and its free_data_function will
   5353  * be invoked when the connection is freed or a new function is set.
   5354  * However, the function will never be called, because there is no
   5355  * way right now to authenticate as a Windows user on UNIX.
   5356  *
   5357  * @param connection the connection
   5358  * @param function the predicate
   5359  * @param data data to pass to the predicate
   5360  * @param free_data_function function to free the data
   5361  */
   5362 void
   5363 dbus_connection_set_windows_user_function (DBusConnection              *connection,
   5364                                            DBusAllowWindowsUserFunction function,
   5365                                            void                        *data,
   5366                                            DBusFreeFunction             free_data_function)
   5367 {
   5368   void *old_data = NULL;
   5369   DBusFreeFunction old_free_function = NULL;
   5370 
   5371   _dbus_return_if_fail (connection != NULL);
   5372 
   5373   CONNECTION_LOCK (connection);
   5374   _dbus_transport_set_windows_user_function (connection->transport,
   5375                                              function, data, free_data_function,
   5376                                              &old_data, &old_free_function);
   5377   CONNECTION_UNLOCK (connection);
   5378 
   5379   if (old_free_function != NULL)
   5380     (* old_free_function) (old_data);
   5381 }
   5382 
   5383 /**
   5384  * This function must be called on the server side of a connection when the
   5385  * connection is first seen in the #DBusNewConnectionFunction. If set to
   5386  * #TRUE (the default is #FALSE), then the connection can proceed even if
   5387  * the client does not authenticate as some user identity, i.e. clients
   5388  * can connect anonymously.
   5389  *
   5390  * This setting interacts with the available authorization mechanisms
   5391  * (see dbus_server_set_auth_mechanisms()). Namely, an auth mechanism
   5392  * such as ANONYMOUS that supports anonymous auth must be included in
   5393  * the list of available mechanisms for anonymous login to work.
   5394  *
   5395  * This setting also changes the default rule for connections
   5396  * authorized as a user; normally, if a connection authorizes as
   5397  * a user identity, it is permitted if the user identity is
   5398  * root or the user identity matches the user identity of the server
   5399  * process. If anonymous connections are allowed, however,
   5400  * then any user identity is allowed.
   5401  *
   5402  * You can override the rules for connections authorized as a
   5403  * user identity with dbus_connection_set_unix_user_function()
   5404  * and dbus_connection_set_windows_user_function().
   5405  *
   5406  * @param connection the connection
   5407  * @param value whether to allow authentication as an anonymous user
   5408  */
   5409 void
   5410 dbus_connection_set_allow_anonymous (DBusConnection             *connection,
   5411                                      dbus_bool_t                 value)
   5412 {
   5413   _dbus_return_if_fail (connection != NULL);
   5414 
   5415   CONNECTION_LOCK (connection);
   5416   _dbus_transport_set_allow_anonymous (connection->transport, value);
   5417   CONNECTION_UNLOCK (connection);
   5418 }
   5419 
   5420 /**
   5421  *
   5422  * Normally #DBusConnection automatically handles all messages to the
   5423  * org.freedesktop.DBus.Peer interface. However, the message bus wants
   5424  * to be able to route methods on that interface through the bus and
   5425  * to other applications. If routing peer messages is enabled, then
   5426  * messages with the org.freedesktop.DBus.Peer interface that also
   5427  * have a bus destination name set will not be automatically
   5428  * handled by the #DBusConnection and instead will be dispatched
   5429  * normally to the application.
   5430  *
   5431  * If a normal application sets this flag, it can break things badly.
   5432  * So don't set this unless you are the message bus.
   5433  *
   5434  * @param connection the connection
   5435  * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
   5436  */
   5437 void
   5438 dbus_connection_set_route_peer_messages (DBusConnection             *connection,
   5439                                          dbus_bool_t                 value)
   5440 {
   5441   _dbus_return_if_fail (connection != NULL);
   5442 
   5443   CONNECTION_LOCK (connection);
   5444   connection->route_peer_messages = TRUE;
   5445   CONNECTION_UNLOCK (connection);
   5446 }
   5447 
   5448 /**
   5449  * Adds a message filter. Filters are handlers that are run on all
   5450  * incoming messages, prior to the objects registered with
   5451  * dbus_connection_register_object_path().  Filters are run in the
   5452  * order that they were added.  The same handler can be added as a
   5453  * filter more than once, in which case it will be run more than once.
   5454  * Filters added during a filter callback won't be run on the message
   5455  * being processed.
   5456  *
   5457  * @todo we don't run filters on messages while blocking without
   5458  * entering the main loop, since filters are run as part of
   5459  * dbus_connection_dispatch(). This is probably a feature, as filters
   5460  * could create arbitrary reentrancy. But kind of sucks if you're
   5461  * trying to filter METHOD_RETURN for some reason.
   5462  *
   5463  * @param connection the connection
   5464  * @param function function to handle messages
   5465  * @param user_data user data to pass to the function
   5466  * @param free_data_function function to use for freeing user data
   5467  * @returns #TRUE on success, #FALSE if not enough memory.
   5468  */
   5469 dbus_bool_t
   5470 dbus_connection_add_filter (DBusConnection            *connection,
   5471                             DBusHandleMessageFunction  function,
   5472                             void                      *user_data,
   5473                             DBusFreeFunction           free_data_function)
   5474 {
   5475   DBusMessageFilter *filter;
   5476 
   5477   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5478   _dbus_return_val_if_fail (function != NULL, FALSE);
   5479 
   5480   filter = dbus_new0 (DBusMessageFilter, 1);
   5481   if (filter == NULL)
   5482     return FALSE;
   5483 
   5484   _dbus_atomic_inc (&filter->refcount);
   5485 
   5486   CONNECTION_LOCK (connection);
   5487 
   5488   if (!_dbus_list_append (&connection->filter_list,
   5489                           filter))
   5490     {
   5491       _dbus_message_filter_unref (filter);
   5492       CONNECTION_UNLOCK (connection);
   5493       return FALSE;
   5494     }
   5495 
   5496   /* Fill in filter after all memory allocated,
   5497    * so we don't run the free_user_data_function
   5498    * if the add_filter() fails
   5499    */
   5500 
   5501   filter->function = function;
   5502   filter->user_data = user_data;
   5503   filter->free_user_data_function = free_data_function;
   5504 
   5505   CONNECTION_UNLOCK (connection);
   5506   return TRUE;
   5507 }
   5508 
   5509 /**
   5510  * Removes a previously-added message filter. It is a programming
   5511  * error to call this function for a handler that has not been added
   5512  * as a filter. If the given handler was added more than once, only
   5513  * one instance of it will be removed (the most recently-added
   5514  * instance).
   5515  *
   5516  * @param connection the connection
   5517  * @param function the handler to remove
   5518  * @param user_data user data for the handler to remove
   5519  *
   5520  */
   5521 void
   5522 dbus_connection_remove_filter (DBusConnection            *connection,
   5523                                DBusHandleMessageFunction  function,
   5524                                void                      *user_data)
   5525 {
   5526   DBusList *link;
   5527   DBusMessageFilter *filter;
   5528 
   5529   _dbus_return_if_fail (connection != NULL);
   5530   _dbus_return_if_fail (function != NULL);
   5531 
   5532   CONNECTION_LOCK (connection);
   5533 
   5534   filter = NULL;
   5535 
   5536   link = _dbus_list_get_last_link (&connection->filter_list);
   5537   while (link != NULL)
   5538     {
   5539       filter = link->data;
   5540 
   5541       if (filter->function == function &&
   5542           filter->user_data == user_data)
   5543         {
   5544           _dbus_list_remove_link (&connection->filter_list, link);
   5545           filter->function = NULL;
   5546 
   5547           break;
   5548         }
   5549 
   5550       link = _dbus_list_get_prev_link (&connection->filter_list, link);
   5551       filter = NULL;
   5552     }
   5553 
   5554   CONNECTION_UNLOCK (connection);
   5555 
   5556 #ifndef DBUS_DISABLE_CHECKS
   5557   if (filter == NULL)
   5558     {
   5559       _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
   5560                                function, user_data);
   5561       return;
   5562     }
   5563 #endif
   5564 
   5565   /* Call application code */
   5566   if (filter->free_user_data_function)
   5567     (* filter->free_user_data_function) (filter->user_data);
   5568 
   5569   filter->free_user_data_function = NULL;
   5570   filter->user_data = NULL;
   5571 
   5572   _dbus_message_filter_unref (filter);
   5573 }
   5574 
   5575 /**
   5576  * Registers a handler for a given path or subsection in the object
   5577  * hierarchy. The given vtable handles messages sent to exactly the
   5578  * given path or also for paths bellow that, depending on fallback
   5579  * parameter.
   5580  *
   5581  * @param connection the connection
   5582  * @param fallback whether to handle messages also for "subdirectory"
   5583  * @param path a '/' delimited string of path elements
   5584  * @param vtable the virtual table
   5585  * @param user_data data to pass to functions in the vtable
   5586  * @param error address where an error can be returned
   5587  * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
   5588  *    #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
   5589  */
   5590 static dbus_bool_t
   5591 _dbus_connection_register_object_path (DBusConnection              *connection,
   5592                                        dbus_bool_t                  fallback,
   5593                                        const char                  *path,
   5594                                        const DBusObjectPathVTable  *vtable,
   5595                                        void                        *user_data,
   5596                                        DBusError                   *error)
   5597 {
   5598   char **decomposed_path;
   5599   dbus_bool_t retval;
   5600 
   5601   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
   5602     return FALSE;
   5603 
   5604   CONNECTION_LOCK (connection);
   5605 
   5606   retval = _dbus_object_tree_register (connection->objects,
   5607                                        fallback,
   5608                                        (const char **) decomposed_path, vtable,
   5609                                        user_data, error);
   5610 
   5611   CONNECTION_UNLOCK (connection);
   5612 
   5613   dbus_free_string_array (decomposed_path);
   5614 
   5615   return retval;
   5616 }
   5617 
   5618 /**
   5619  * Registers a handler for a given path in the object hierarchy.
   5620  * The given vtable handles messages sent to exactly the given path.
   5621  *
   5622  * @param connection the connection
   5623  * @param path a '/' delimited string of path elements
   5624  * @param vtable the virtual table
   5625  * @param user_data data to pass to functions in the vtable
   5626  * @param error address where an error can be returned
   5627  * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
   5628  *    #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
   5629  */
   5630 dbus_bool_t
   5631 dbus_connection_try_register_object_path (DBusConnection              *connection,
   5632                                           const char                  *path,
   5633                                           const DBusObjectPathVTable  *vtable,
   5634                                           void                        *user_data,
   5635                                           DBusError                   *error)
   5636 {
   5637   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5638   _dbus_return_val_if_fail (path != NULL, FALSE);
   5639   _dbus_return_val_if_fail (path[0] == '/', FALSE);
   5640   _dbus_return_val_if_fail (vtable != NULL, FALSE);
   5641 
   5642   return _dbus_connection_register_object_path (connection, FALSE, path, vtable, user_data, error);
   5643 }
   5644 
   5645 /**
   5646  * Registers a handler for a given path in the object hierarchy.
   5647  * The given vtable handles messages sent to exactly the given path.
   5648  *
   5649  * It is a bug to call this function for object paths which already
   5650  * have a handler. Use dbus_connection_try_register_object_path() if this
   5651  * might be the case.
   5652  *
   5653  * @param connection the connection
   5654  * @param path a '/' delimited string of path elements
   5655  * @param vtable the virtual table
   5656  * @param user_data data to pass to functions in the vtable
   5657  * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
   5658  *    #DBUS_ERROR_OBJECT_PATH_IN_USE) ocurred
   5659  */
   5660 dbus_bool_t
   5661 dbus_connection_register_object_path (DBusConnection              *connection,
   5662                                       const char                  *path,
   5663                                       const DBusObjectPathVTable  *vtable,
   5664                                       void                        *user_data)
   5665 {
   5666   dbus_bool_t retval;
   5667   DBusError error = DBUS_ERROR_INIT;
   5668 
   5669   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5670   _dbus_return_val_if_fail (path != NULL, FALSE);
   5671   _dbus_return_val_if_fail (path[0] == '/', FALSE);
   5672   _dbus_return_val_if_fail (vtable != NULL, FALSE);
   5673 
   5674   retval = _dbus_connection_register_object_path (connection, FALSE, path, vtable, user_data, &error);
   5675 
   5676   if (dbus_error_has_name (&error, DBUS_ERROR_OBJECT_PATH_IN_USE))
   5677     {
   5678       _dbus_warn ("%s\n", error.message);
   5679       dbus_error_free (&error);
   5680       return FALSE;
   5681     }
   5682 
   5683   return retval;
   5684 }
   5685 
   5686 /**
   5687  * Registers a fallback handler for a given subsection of the object
   5688  * hierarchy.  The given vtable handles messages at or below the given
   5689  * path. You can use this to establish a default message handling
   5690  * policy for a whole "subdirectory."
   5691  *
   5692  * @param connection the connection
   5693  * @param path a '/' delimited string of path elements
   5694  * @param vtable the virtual table
   5695  * @param user_data data to pass to functions in the vtable
   5696  * @param error address where an error can be returned
   5697  * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
   5698  *    #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
   5699  */
   5700 dbus_bool_t
   5701 dbus_connection_try_register_fallback (DBusConnection              *connection,
   5702                                        const char                  *path,
   5703                                        const DBusObjectPathVTable  *vtable,
   5704                                        void                        *user_data,
   5705                                        DBusError                   *error)
   5706 {
   5707   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5708   _dbus_return_val_if_fail (path != NULL, FALSE);
   5709   _dbus_return_val_if_fail (path[0] == '/', FALSE);
   5710   _dbus_return_val_if_fail (vtable != NULL, FALSE);
   5711 
   5712   return _dbus_connection_register_object_path (connection, TRUE, path, vtable, user_data, error);
   5713 }
   5714 
   5715 /**
   5716  * Registers a fallback handler for a given subsection of the object
   5717  * hierarchy.  The given vtable handles messages at or below the given
   5718  * path. You can use this to establish a default message handling
   5719  * policy for a whole "subdirectory."
   5720  *
   5721  * It is a bug to call this function for object paths which already
   5722  * have a handler. Use dbus_connection_try_register_fallback() if this
   5723  * might be the case.
   5724  *
   5725  * @param connection the connection
   5726  * @param path a '/' delimited string of path elements
   5727  * @param vtable the virtual table
   5728  * @param user_data data to pass to functions in the vtable
   5729  * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
   5730  *    #DBUS_ERROR_OBJECT_PATH_IN_USE) occured
   5731  */
   5732 dbus_bool_t
   5733 dbus_connection_register_fallback (DBusConnection              *connection,
   5734                                    const char                  *path,
   5735                                    const DBusObjectPathVTable  *vtable,
   5736                                    void                        *user_data)
   5737 {
   5738   dbus_bool_t retval;
   5739   DBusError error = DBUS_ERROR_INIT;
   5740 
   5741   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5742   _dbus_return_val_if_fail (path != NULL, FALSE);
   5743   _dbus_return_val_if_fail (path[0] == '/', FALSE);
   5744   _dbus_return_val_if_fail (vtable != NULL, FALSE);
   5745 
   5746   retval = _dbus_connection_register_object_path (connection, TRUE, path, vtable, user_data, &error);
   5747 
   5748   if (dbus_error_has_name (&error, DBUS_ERROR_OBJECT_PATH_IN_USE))
   5749     {
   5750       _dbus_warn ("%s\n", error.message);
   5751       dbus_error_free (&error);
   5752       return FALSE;
   5753     }
   5754 
   5755   return retval;
   5756 }
   5757 
   5758 /**
   5759  * Unregisters the handler registered with exactly the given path.
   5760  * It's a bug to call this function for a path that isn't registered.
   5761  * Can unregister both fallback paths and object paths.
   5762  *
   5763  * @param connection the connection
   5764  * @param path a '/' delimited string of path elements
   5765  * @returns #FALSE if not enough memory
   5766  */
   5767 dbus_bool_t
   5768 dbus_connection_unregister_object_path (DBusConnection              *connection,
   5769                                         const char                  *path)
   5770 {
   5771   char **decomposed_path;
   5772 
   5773   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5774   _dbus_return_val_if_fail (path != NULL, FALSE);
   5775   _dbus_return_val_if_fail (path[0] == '/', FALSE);
   5776 
   5777   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
   5778       return FALSE;
   5779 
   5780   CONNECTION_LOCK (connection);
   5781 
   5782   _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
   5783 
   5784   dbus_free_string_array (decomposed_path);
   5785 
   5786   return TRUE;
   5787 }
   5788 
   5789 /**
   5790  * Gets the user data passed to dbus_connection_register_object_path()
   5791  * or dbus_connection_register_fallback(). If nothing was registered
   5792  * at this path, the data is filled in with #NULL.
   5793  *
   5794  * @param connection the connection
   5795  * @param path the path you registered with
   5796  * @param data_p location to store the user data, or #NULL
   5797  * @returns #FALSE if not enough memory
   5798  */
   5799 dbus_bool_t
   5800 dbus_connection_get_object_path_data (DBusConnection *connection,
   5801                                       const char     *path,
   5802                                       void          **data_p)
   5803 {
   5804   char **decomposed_path;
   5805 
   5806   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5807   _dbus_return_val_if_fail (path != NULL, FALSE);
   5808   _dbus_return_val_if_fail (data_p != NULL, FALSE);
   5809 
   5810   *data_p = NULL;
   5811 
   5812   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
   5813     return FALSE;
   5814 
   5815   CONNECTION_LOCK (connection);
   5816 
   5817   *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
   5818 
   5819   CONNECTION_UNLOCK (connection);
   5820 
   5821   dbus_free_string_array (decomposed_path);
   5822 
   5823   return TRUE;
   5824 }
   5825 
   5826 /**
   5827  * Lists the registered fallback handlers and object path handlers at
   5828  * the given parent_path. The returned array should be freed with
   5829  * dbus_free_string_array().
   5830  *
   5831  * @param connection the connection
   5832  * @param parent_path the path to list the child handlers of
   5833  * @param child_entries returns #NULL-terminated array of children
   5834  * @returns #FALSE if no memory to allocate the child entries
   5835  */
   5836 dbus_bool_t
   5837 dbus_connection_list_registered (DBusConnection              *connection,
   5838                                  const char                  *parent_path,
   5839                                  char                      ***child_entries)
   5840 {
   5841   char **decomposed_path;
   5842   dbus_bool_t retval;
   5843   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5844   _dbus_return_val_if_fail (parent_path != NULL, FALSE);
   5845   _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
   5846   _dbus_return_val_if_fail (child_entries != NULL, FALSE);
   5847 
   5848   if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
   5849     return FALSE;
   5850 
   5851   CONNECTION_LOCK (connection);
   5852 
   5853   retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
   5854 							 (const char **) decomposed_path,
   5855 							 child_entries);
   5856   dbus_free_string_array (decomposed_path);
   5857 
   5858   return retval;
   5859 }
   5860 
   5861 static DBusDataSlotAllocator slot_allocator;
   5862 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
   5863 
   5864 /**
   5865  * Allocates an integer ID to be used for storing application-specific
   5866  * data on any DBusConnection. The allocated ID may then be used
   5867  * with dbus_connection_set_data() and dbus_connection_get_data().
   5868  * The passed-in slot must be initialized to -1, and is filled in
   5869  * with the slot ID. If the passed-in slot is not -1, it's assumed
   5870  * to be already allocated, and its refcount is incremented.
   5871  *
   5872  * The allocated slot is global, i.e. all DBusConnection objects will
   5873  * have a slot with the given integer ID reserved.
   5874  *
   5875  * @param slot_p address of a global variable storing the slot
   5876  * @returns #FALSE on failure (no memory)
   5877  */
   5878 dbus_bool_t
   5879 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
   5880 {
   5881   return _dbus_data_slot_allocator_alloc (&slot_allocator,
   5882                                           &_DBUS_LOCK_NAME (connection_slots),
   5883                                           slot_p);
   5884 }
   5885 
   5886 /**
   5887  * Deallocates a global ID for connection data slots.
   5888  * dbus_connection_get_data() and dbus_connection_set_data() may no
   5889  * longer be used with this slot.  Existing data stored on existing
   5890  * DBusConnection objects will be freed when the connection is
   5891  * finalized, but may not be retrieved (and may only be replaced if
   5892  * someone else reallocates the slot).  When the refcount on the
   5893  * passed-in slot reaches 0, it is set to -1.
   5894  *
   5895  * @param slot_p address storing the slot to deallocate
   5896  */
   5897 void
   5898 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
   5899 {
   5900   _dbus_return_if_fail (*slot_p >= 0);
   5901 
   5902   _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
   5903 }
   5904 
   5905 /**
   5906  * Stores a pointer on a DBusConnection, along
   5907  * with an optional function to be used for freeing
   5908  * the data when the data is set again, or when
   5909  * the connection is finalized. The slot number
   5910  * must have been allocated with dbus_connection_allocate_data_slot().
   5911  *
   5912  * @note This function does not take the
   5913  * main thread lock on DBusConnection, which allows it to be
   5914  * used from inside watch and timeout functions. (See the
   5915  * note in docs for dbus_connection_set_watch_functions().)
   5916  * A side effect of this is that you need to know there's
   5917  * a reference held on the connection while invoking
   5918  * dbus_connection_set_data(), or the connection could be
   5919  * finalized during dbus_connection_set_data().
   5920  *
   5921  * @param connection the connection
   5922  * @param slot the slot number
   5923  * @param data the data to store
   5924  * @param free_data_func finalizer function for the data
   5925  * @returns #TRUE if there was enough memory to store the data
   5926  */
   5927 dbus_bool_t
   5928 dbus_connection_set_data (DBusConnection   *connection,
   5929                           dbus_int32_t      slot,
   5930                           void             *data,
   5931                           DBusFreeFunction  free_data_func)
   5932 {
   5933   DBusFreeFunction old_free_func;
   5934   void *old_data;
   5935   dbus_bool_t retval;
   5936 
   5937   _dbus_return_val_if_fail (connection != NULL, FALSE);
   5938   _dbus_return_val_if_fail (slot >= 0, FALSE);
   5939 
   5940   SLOTS_LOCK (connection);
   5941 
   5942   retval = _dbus_data_slot_list_set (&slot_allocator,
   5943                                      &connection->slot_list,
   5944                                      slot, data, free_data_func,
   5945                                      &old_free_func, &old_data);
   5946 
   5947   SLOTS_UNLOCK (connection);
   5948 
   5949   if (retval)
   5950     {
   5951       /* Do the actual free outside the connection lock */
   5952       if (old_free_func)
   5953         (* old_free_func) (old_data);
   5954     }
   5955 
   5956   return retval;
   5957 }
   5958 
   5959 /**
   5960  * Retrieves data previously set with dbus_connection_set_data().
   5961  * The slot must still be allocated (must not have been freed).
   5962  *
   5963  * @note This function does not take the
   5964  * main thread lock on DBusConnection, which allows it to be
   5965  * used from inside watch and timeout functions. (See the
   5966  * note in docs for dbus_connection_set_watch_functions().)
   5967  * A side effect of this is that you need to know there's
   5968  * a reference held on the connection while invoking
   5969  * dbus_connection_get_data(), or the connection could be
   5970  * finalized during dbus_connection_get_data().
   5971  *
   5972  * @param connection the connection
   5973  * @param slot the slot to get data from
   5974  * @returns the data, or #NULL if not found
   5975  */
   5976 void*
   5977 dbus_connection_get_data (DBusConnection   *connection,
   5978                           dbus_int32_t      slot)
   5979 {
   5980   void *res;
   5981 
   5982   _dbus_return_val_if_fail (connection != NULL, NULL);
   5983 
   5984   SLOTS_LOCK (connection);
   5985 
   5986   res = _dbus_data_slot_list_get (&slot_allocator,
   5987                                   &connection->slot_list,
   5988                                   slot);
   5989 
   5990   SLOTS_UNLOCK (connection);
   5991 
   5992   return res;
   5993 }
   5994 
   5995 /**
   5996  * This function sets a global flag for whether dbus_connection_new()
   5997  * will set SIGPIPE behavior to SIG_IGN.
   5998  *
   5999  * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
   6000  */
   6001 void
   6002 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
   6003 {
   6004   _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
   6005 }
   6006 
   6007 /**
   6008  * Specifies the maximum size message this connection is allowed to
   6009  * receive. Larger messages will result in disconnecting the
   6010  * connection.
   6011  *
   6012  * @param connection a #DBusConnection
   6013  * @param size maximum message size the connection can receive, in bytes
   6014  */
   6015 void
   6016 dbus_connection_set_max_message_size (DBusConnection *connection,
   6017                                       long            size)
   6018 {
   6019   _dbus_return_if_fail (connection != NULL);
   6020 
   6021   CONNECTION_LOCK (connection);
   6022   _dbus_transport_set_max_message_size (connection->transport,
   6023                                         size);
   6024   CONNECTION_UNLOCK (connection);
   6025 }
   6026 
   6027 /**
   6028  * Gets the value set by dbus_connection_set_max_message_size().
   6029  *
   6030  * @param connection the connection
   6031  * @returns the max size of a single message
   6032  */
   6033 long
   6034 dbus_connection_get_max_message_size (DBusConnection *connection)
   6035 {
   6036   long res;
   6037 
   6038   _dbus_return_val_if_fail (connection != NULL, 0);
   6039 
   6040   CONNECTION_LOCK (connection);
   6041   res = _dbus_transport_get_max_message_size (connection->transport);
   6042   CONNECTION_UNLOCK (connection);
   6043   return res;
   6044 }
   6045 
   6046 /**
   6047  * Specifies the maximum number of unix fds a message on this
   6048  * connection is allowed to receive. Messages with more unix fds will
   6049  * result in disconnecting the connection.
   6050  *
   6051  * @param connection a #DBusConnection
   6052  * @param size maximum message unix fds the connection can receive
   6053  */
   6054 void
   6055 dbus_connection_set_max_message_unix_fds (DBusConnection *connection,
   6056                                           long            n)
   6057 {
   6058   _dbus_return_if_fail (connection != NULL);
   6059 
   6060   CONNECTION_LOCK (connection);
   6061   _dbus_transport_set_max_message_unix_fds (connection->transport,
   6062                                             n);
   6063   CONNECTION_UNLOCK (connection);
   6064 }
   6065 
   6066 /**
   6067  * Gets the value set by dbus_connection_set_max_message_unix_fds().
   6068  *
   6069  * @param connection the connection
   6070  * @returns the max numer of unix fds of a single message
   6071  */
   6072 long
   6073 dbus_connection_get_max_message_unix_fds (DBusConnection *connection)
   6074 {
   6075   long res;
   6076 
   6077   _dbus_return_val_if_fail (connection != NULL, 0);
   6078 
   6079   CONNECTION_LOCK (connection);
   6080   res = _dbus_transport_get_max_message_unix_fds (connection->transport);
   6081   CONNECTION_UNLOCK (connection);
   6082   return res;
   6083 }
   6084 
   6085 /**
   6086  * Sets the maximum total number of bytes that can be used for all messages
   6087  * received on this connection. Messages count toward the maximum until
   6088  * they are finalized. When the maximum is reached, the connection will
   6089  * not read more data until some messages are finalized.
   6090  *
   6091  * The semantics of the maximum are: if outstanding messages are
   6092  * already above the maximum, additional messages will not be read.
   6093  * The semantics are not: if the next message would cause us to exceed
   6094  * the maximum, we don't read it. The reason is that we don't know the
   6095  * size of a message until after we read it.
   6096  *
   6097  * Thus, the max live messages size can actually be exceeded
   6098  * by up to the maximum size of a single message.
   6099  *
   6100  * Also, if we read say 1024 bytes off the wire in a single read(),
   6101  * and that contains a half-dozen small messages, we may exceed the
   6102  * size max by that amount. But this should be inconsequential.
   6103  *
   6104  * This does imply that we can't call read() with a buffer larger
   6105  * than we're willing to exceed this limit by.
   6106  *
   6107  * @param connection the connection
   6108  * @param size the maximum size in bytes of all outstanding messages
   6109  */
   6110 void
   6111 dbus_connection_set_max_received_size (DBusConnection *connection,
   6112                                        long            size)
   6113 {
   6114   _dbus_return_if_fail (connection != NULL);
   6115 
   6116   CONNECTION_LOCK (connection);
   6117   _dbus_transport_set_max_received_size (connection->transport,
   6118                                          size);
   6119   CONNECTION_UNLOCK (connection);
   6120 }
   6121 
   6122 /**
   6123  * Gets the value set by dbus_connection_set_max_received_size().
   6124  *
   6125  * @param connection the connection
   6126  * @returns the max size of all live messages
   6127  */
   6128 long
   6129 dbus_connection_get_max_received_size (DBusConnection *connection)
   6130 {
   6131   long res;
   6132 
   6133   _dbus_return_val_if_fail (connection != NULL, 0);
   6134 
   6135   CONNECTION_LOCK (connection);
   6136   res = _dbus_transport_get_max_received_size (connection->transport);
   6137   CONNECTION_UNLOCK (connection);
   6138   return res;
   6139 }
   6140 
   6141 /**
   6142  * Sets the maximum total number of unix fds that can be used for all messages
   6143  * received on this connection. Messages count toward the maximum until
   6144  * they are finalized. When the maximum is reached, the connection will
   6145  * not read more data until some messages are finalized.
   6146  *
   6147  * The semantics are analogous to those of dbus_connection_set_max_received_size().
   6148  *
   6149  * @param connection the connection
   6150  * @param size the maximum size in bytes of all outstanding messages
   6151  */
   6152 void
   6153 dbus_connection_set_max_received_unix_fds (DBusConnection *connection,
   6154                                            long            n)
   6155 {
   6156   _dbus_return_if_fail (connection != NULL);
   6157 
   6158   CONNECTION_LOCK (connection);
   6159   _dbus_transport_set_max_received_unix_fds (connection->transport,
   6160                                              n);
   6161   CONNECTION_UNLOCK (connection);
   6162 }
   6163 
   6164 /**
   6165  * Gets the value set by dbus_connection_set_max_received_unix_fds().
   6166  *
   6167  * @param connection the connection
   6168  * @returns the max unix fds of all live messages
   6169  */
   6170 long
   6171 dbus_connection_get_max_received_unix_fds (DBusConnection *connection)
   6172 {
   6173   long res;
   6174 
   6175   _dbus_return_val_if_fail (connection != NULL, 0);
   6176 
   6177   CONNECTION_LOCK (connection);
   6178   res = _dbus_transport_get_max_received_unix_fds (connection->transport);
   6179   CONNECTION_UNLOCK (connection);
   6180   return res;
   6181 }
   6182 
   6183 /**
   6184  * Gets the approximate size in bytes of all messages in the outgoing
   6185  * message queue. The size is approximate in that you shouldn't use
   6186  * it to decide how many bytes to read off the network or anything
   6187  * of that nature, as optimizations may choose to tell small white lies
   6188  * to avoid performance overhead.
   6189  *
   6190  * @param connection the connection
   6191  * @returns the number of bytes that have been queued up but not sent
   6192  */
   6193 long
   6194 dbus_connection_get_outgoing_size (DBusConnection *connection)
   6195 {
   6196   long res;
   6197 
   6198   _dbus_return_val_if_fail (connection != NULL, 0);
   6199 
   6200   CONNECTION_LOCK (connection);
   6201   res = _dbus_counter_get_size_value (connection->outgoing_counter);
   6202   CONNECTION_UNLOCK (connection);
   6203   return res;
   6204 }
   6205 
   6206 #ifdef DBUS_ENABLE_STATS
   6207 void
   6208 _dbus_connection_get_stats (DBusConnection *connection,
   6209                             dbus_uint32_t  *in_messages,
   6210                             dbus_uint32_t  *in_bytes,
   6211                             dbus_uint32_t  *in_fds,
   6212                             dbus_uint32_t  *in_peak_bytes,
   6213                             dbus_uint32_t  *in_peak_fds,
   6214                             dbus_uint32_t  *out_messages,
   6215                             dbus_uint32_t  *out_bytes,
   6216                             dbus_uint32_t  *out_fds,
   6217                             dbus_uint32_t  *out_peak_bytes,
   6218                             dbus_uint32_t  *out_peak_fds)
   6219 {
   6220   CONNECTION_LOCK (connection);
   6221 
   6222   if (in_messages != NULL)
   6223     *in_messages = connection->n_incoming;
   6224 
   6225   _dbus_transport_get_stats (connection->transport,
   6226                              in_bytes, in_fds, in_peak_bytes, in_peak_fds);
   6227 
   6228   if (out_messages != NULL)
   6229     *out_messages = connection->n_outgoing;
   6230 
   6231   if (out_bytes != NULL)
   6232     *out_bytes = _dbus_counter_get_size_value (connection->outgoing_counter);
   6233 
   6234   if (out_fds != NULL)
   6235     *out_fds = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
   6236 
   6237   if (out_peak_bytes != NULL)
   6238     *out_peak_bytes = _dbus_counter_get_peak_size_value (connection->outgoing_counter);
   6239 
   6240   if (out_peak_fds != NULL)
   6241     *out_peak_fds = _dbus_counter_get_peak_unix_fd_value (connection->outgoing_counter);
   6242 
   6243   CONNECTION_UNLOCK (connection);
   6244 }
   6245 #endif /* DBUS_ENABLE_STATS */
   6246 
   6247 /**
   6248  * Gets the approximate number of uni fds of all messages in the
   6249  * outgoing message queue.
   6250  *
   6251  * @param connection the connection
   6252  * @returns the number of unix fds that have been queued up but not sent
   6253  */
   6254 long
   6255 dbus_connection_get_outgoing_unix_fds (DBusConnection *connection)
   6256 {
   6257   long res;
   6258 
   6259   _dbus_return_val_if_fail (connection != NULL, 0);
   6260 
   6261   CONNECTION_LOCK (connection);
   6262   res = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
   6263   CONNECTION_UNLOCK (connection);
   6264   return res;
   6265 }
   6266 
   6267 #ifdef DBUS_BUILD_TESTS
   6268 /**
   6269  * Returns the address of the transport object of this connection
   6270  *
   6271  * @param connection the connection
   6272  * @returns the address string
   6273  */
   6274 const char*
   6275 _dbus_connection_get_address (DBusConnection *connection)
   6276 {
   6277   return _dbus_transport_get_address (connection->transport);
   6278 }
   6279 #endif
   6280 
   6281 /** @} */
   6282