2005-04-09 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-connection.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
3  *
4  * Copyright (C) 2002, 2003, 2004, 2005  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <config.h>
25 #include "dbus-shared.h"
26 #include "dbus-connection.h"
27 #include "dbus-list.h"
28 #include "dbus-timeout.h"
29 #include "dbus-transport.h"
30 #include "dbus-watch.h"
31 #include "dbus-connection-internal.h"
32 #include "dbus-list.h"
33 #include "dbus-hash.h"
34 #include "dbus-message-internal.h"
35 #include "dbus-threads.h"
36 #include "dbus-protocol.h"
37 #include "dbus-dataslot.h"
38 #include "dbus-string.h"
39 #include "dbus-pending-call.h"
40 #include "dbus-object-tree.h"
41 #include "dbus-threads-internal.h"
42
43 #ifdef DBUS_DISABLE_CHECKS
44 #define TOOK_LOCK_CHECK(connection)
45 #define RELEASING_LOCK_CHECK(connection)
46 #define HAVE_LOCK_CHECK(connection)
47 #else
48 #define TOOK_LOCK_CHECK(connection) do {                \
49     _dbus_assert (!(connection)->have_connection_lock); \
50     (connection)->have_connection_lock = TRUE;          \
51   } while (0)
52 #define RELEASING_LOCK_CHECK(connection) do {            \
53     _dbus_assert ((connection)->have_connection_lock);   \
54     (connection)->have_connection_lock = FALSE;          \
55   } while (0)
56 #define HAVE_LOCK_CHECK(connection)        _dbus_assert ((connection)->have_connection_lock)
57 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
58 #endif
59
60 #define TRACE_LOCKS 1
61
62 #define CONNECTION_LOCK(connection)   do {                                      \
63     if (TRACE_LOCKS) { _dbus_verbose ("  LOCK: %s\n", _DBUS_FUNCTION_NAME); }   \
64     _dbus_mutex_lock ((connection)->mutex);                                      \
65     TOOK_LOCK_CHECK (connection);                                               \
66   } while (0)
67
68 #define CONNECTION_UNLOCK(connection) do {                                              \
69     if (TRACE_LOCKS) { _dbus_verbose ("  UNLOCK: %s\n", _DBUS_FUNCTION_NAME);  }        \
70     RELEASING_LOCK_CHECK (connection);                                                  \
71     _dbus_mutex_unlock ((connection)->mutex);                                            \
72   } while (0)
73
74 #define DISPATCH_STATUS_NAME(s)                                            \
75                      ((s) == DBUS_DISPATCH_COMPLETE ? "complete" :         \
76                       (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
77                       (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :   \
78                       "???")
79
80 /**
81  * @defgroup DBusConnection DBusConnection
82  * @ingroup  DBus
83  * @brief Connection to another application
84  *
85  * A DBusConnection represents a connection to another
86  * application. Messages can be sent and received via this connection.
87  * The other application may be a message bus; for convenience, the
88  * function dbus_bus_get() is provided to automatically open a
89  * connection to the well-known message buses.
90  * 
91  * In brief a DBusConnection is a message queue associated with some
92  * message transport mechanism such as a socket.  The connection
93  * maintains a queue of incoming messages and a queue of outgoing
94  * messages.
95  *
96  * Incoming messages are normally processed by calling
97  * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
98  * handlers registered for the topmost message in the message queue,
99  * then discards the message, then returns.
100  * 
101  * dbus_connection_get_dispatch_status() indicates whether
102  * messages are currently in the queue that need dispatching.
103  * dbus_connection_set_dispatch_status_function() allows
104  * you to set a function to be used to monitor the dispatch status.
105  *
106  * If you're using GLib or Qt add-on libraries for D-BUS, there are
107  * special convenience APIs in those libraries that hide
108  * all the details of dispatch and watch/timeout monitoring.
109  * For example, dbus_connection_setup_with_g_main().
110  *
111  * If you aren't using these add-on libraries, you have to manually
112  * call dbus_connection_set_dispatch_status_function(),
113  * dbus_connection_set_watch_functions(),
114  * dbus_connection_set_timeout_functions() providing appropriate
115  * functions to integrate the connection with your application's main
116  * loop.
117  *
118  * When you use dbus_connection_send() or one of its variants to send
119  * a message, the message is added to the outgoing queue.  It's
120  * actually written to the network later; either in
121  * dbus_watch_handle() invoked by your main loop, or in
122  * dbus_connection_flush() which blocks until it can write out the
123  * entire outgoing queue. The GLib/Qt add-on libraries again
124  * handle the details here for you by setting up watch functions.
125  *
126  * When a connection is disconnected, you are guaranteed to get a
127  * signal "Disconnected" from the interface
128  * #DBUS_INTERFACE_LOCAL, path
129  * #DBUS_PATH_LOCAL.
130  *
131  * You may not drop the last reference to a #DBusConnection
132  * until that connection has been disconnected.
133  *
134  * You may dispatch the unprocessed incoming message queue even if the
135  * connection is disconnected. However, "Disconnected" will always be
136  * the last message in the queue (obviously no messages are received
137  * after disconnection).
138  *
139  * #DBusConnection has thread locks and drops them when invoking user
140  * callbacks, so in general is transparently threadsafe. However,
141  * #DBusMessage does NOT have thread locks; you must not send the same
142  * message to multiple #DBusConnection that will be used from
143  * different threads.
144  */
145
146 /**
147  * @defgroup DBusConnectionInternals DBusConnection implementation details
148  * @ingroup  DBusInternals
149  * @brief Implementation details of DBusConnection
150  *
151  * @{
152  */
153
154 /**
155  * Internal struct representing a message filter function 
156  */
157 typedef struct DBusMessageFilter DBusMessageFilter;
158
159 /**
160  * Internal struct representing a message filter function 
161  */
162 struct DBusMessageFilter
163 {
164   DBusAtomic refcount; /**< Reference count */
165   DBusHandleMessageFunction function; /**< Function to call to filter */
166   void *user_data; /**< User data for the function */
167   DBusFreeFunction free_user_data_function; /**< Function to free the user data */
168 };
169
170
171 /**
172  * Internals of DBusPreallocatedSend
173  */
174 struct DBusPreallocatedSend
175 {
176   DBusConnection *connection; /**< Connection we'd send the message to */
177   DBusList *queue_link;       /**< Preallocated link in the queue */
178   DBusList *counter_link;     /**< Preallocated link in the resource counter */
179 };
180
181 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
182
183 /**
184  * Implementation details of DBusConnection. All fields are private.
185  */
186 struct DBusConnection
187 {
188   DBusAtomic refcount; /**< Reference count. */
189
190   DBusMutex *mutex; /**< Lock on the entire DBusConnection */
191
192   DBusMutex *dispatch_mutex;     /**< Protects dispatch_acquired */
193   DBusCondVar *dispatch_cond;    /**< Notify when dispatch_acquired is available */
194   DBusMutex *io_path_mutex;      /**< Protects io_path_acquired */
195   DBusCondVar *io_path_cond;     /**< Notify when io_path_acquired is available */
196   
197   DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
198   DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
199
200   DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
201                                   *   dispatch_acquired will be set by the borrower
202                                   */
203   
204   int n_outgoing;              /**< Length of outgoing queue. */
205   int n_incoming;              /**< Length of incoming queue. */
206
207   DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
208   
209   DBusTransport *transport;    /**< Object that sends/receives messages over network. */
210   DBusWatchList *watches;      /**< Stores active watches. */
211   DBusTimeoutList *timeouts;   /**< Stores active timeouts. */
212   
213   DBusList *filter_list;        /**< List of filters. */
214
215   DBusDataSlotList slot_list;   /**< Data stored by allocated integer ID */
216
217   DBusHashTable *pending_replies;  /**< Hash of message serials to #DBusPendingCall. */  
218   
219   dbus_uint32_t client_serial;       /**< Client serial. Increments each time a message is sent  */
220   DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
221
222   DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop  */
223   void *wakeup_main_data; /**< Application data for wakeup_main_function */
224   DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
225
226   DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes  */
227   void *dispatch_status_data; /**< Application data for dispatch_status_function */
228   DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
229
230   DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
231
232   DBusList *link_cache; /**< A cache of linked list links to prevent contention
233                          *   for the global linked list mempool lock
234                          */
235   DBusObjectTree *objects; /**< Object path handlers registered with this connection */
236
237   char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
238
239   unsigned int shareable : 1; /**< #TRUE if connection can go in shared_connections once we know the GUID */
240   
241   unsigned int dispatch_acquired : 1; /**< Someone has dispatch path (can drain incoming queue) */
242   unsigned int io_path_acquired : 1;  /**< Someone has transport io path (can use the transport to read/write messages) */
243   
244   unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
245   
246 #ifndef DBUS_DISABLE_CHECKS
247   unsigned int have_connection_lock : 1; /**< Used to check locking */
248 #endif
249   
250 #ifndef DBUS_DISABLE_CHECKS
251   int generation; /**< _dbus_current_generation that should correspond to this connection */
252 #endif 
253 };
254
255 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked      (DBusConnection     *connection);
256 static void               _dbus_connection_update_dispatch_status_and_unlock (DBusConnection     *connection,
257                                                                               DBusDispatchStatus  new_status);
258 static void               _dbus_connection_last_unref                        (DBusConnection     *connection);
259 static void               _dbus_connection_acquire_dispatch                  (DBusConnection     *connection);
260 static void               _dbus_connection_release_dispatch                  (DBusConnection     *connection);
261
262 static DBusMessageFilter *
263 _dbus_message_filter_ref (DBusMessageFilter *filter)
264 {
265   _dbus_assert (filter->refcount.value > 0);
266   _dbus_atomic_inc (&filter->refcount);
267
268   return filter;
269 }
270
271 static void
272 _dbus_message_filter_unref (DBusMessageFilter *filter)
273 {
274   _dbus_assert (filter->refcount.value > 0);
275
276   if (_dbus_atomic_dec (&filter->refcount) == 1)
277     {
278       if (filter->free_user_data_function)
279         (* filter->free_user_data_function) (filter->user_data);
280       
281       dbus_free (filter);
282     }
283 }
284
285 /**
286  * Acquires the connection lock.
287  *
288  * @param connection the connection.
289  */
290 void
291 _dbus_connection_lock (DBusConnection *connection)
292 {
293   CONNECTION_LOCK (connection);
294 }
295
296 /**
297  * Releases the connection lock.
298  *
299  * @param connection the connection.
300  */
301 void
302 _dbus_connection_unlock (DBusConnection *connection)
303 {
304   CONNECTION_UNLOCK (connection);
305 }
306
307 /**
308  * Wakes up the main loop if it is sleeping
309  * Needed if we're e.g. queueing outgoing messages
310  * on a thread while the mainloop sleeps.
311  *
312  * @param connection the connection.
313  */
314 static void
315 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
316 {
317   if (connection->wakeup_main_function)
318     (*connection->wakeup_main_function) (connection->wakeup_main_data);
319 }
320
321 #ifdef DBUS_BUILD_TESTS
322 /* For now this function isn't used */
323 /**
324  * Adds a message to the incoming message queue, returning #FALSE
325  * if there's insufficient memory to queue the message.
326  * Does not take over refcount of the message.
327  *
328  * @param connection the connection.
329  * @param message the message to queue.
330  * @returns #TRUE on success.
331  */
332 dbus_bool_t
333 _dbus_connection_queue_received_message (DBusConnection *connection,
334                                          DBusMessage    *message)
335 {
336   DBusList *link;
337
338   link = _dbus_list_alloc_link (message);
339   if (link == NULL)
340     return FALSE;
341
342   dbus_message_ref (message);
343   _dbus_connection_queue_received_message_link (connection, link);
344
345   return TRUE;
346 }
347 #endif
348
349 /**
350  * Adds a message-containing list link to the incoming message queue,
351  * taking ownership of the link and the message's current refcount.
352  * Cannot fail due to lack of memory.
353  *
354  * @param connection the connection.
355  * @param link the message link to queue.
356  */
357 void
358 _dbus_connection_queue_received_message_link (DBusConnection  *connection,
359                                               DBusList        *link)
360 {
361   DBusPendingCall *pending;
362   dbus_int32_t reply_serial;
363   DBusMessage *message;
364   
365   _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
366   
367   _dbus_list_append_link (&connection->incoming_messages,
368                           link);
369   message = link->data;
370
371   /* If this is a reply we're waiting on, remove timeout for it */
372   reply_serial = dbus_message_get_reply_serial (message);
373   if (reply_serial != -1)
374     {
375       pending = _dbus_hash_table_lookup_int (connection->pending_replies,
376                                              reply_serial);
377       if (pending != NULL)
378         {
379           if (pending->timeout_added)
380             _dbus_connection_remove_timeout (connection,
381                                              pending->timeout);
382
383           pending->timeout_added = FALSE;
384         }
385     }
386   
387   connection->n_incoming += 1;
388
389   _dbus_connection_wakeup_mainloop (connection);
390   
391   _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
392                  message,
393                  dbus_message_get_type (message),
394                  dbus_message_get_path (message),
395                  dbus_message_get_interface (message) ?
396                  dbus_message_get_interface (message) :
397                  "no interface",
398                  dbus_message_get_member (message) ?
399                  dbus_message_get_member (message) :
400                  "no member",
401                  dbus_message_get_signature (message),
402                  dbus_message_get_reply_serial (message),
403                  connection,
404                  connection->n_incoming);
405 }
406
407 /**
408  * Adds a link + message to the incoming message queue.
409  * Can't fail. Takes ownership of both link and message.
410  *
411  * @param connection the connection.
412  * @param link the list node and message to queue.
413  *
414  * @todo This needs to wake up the mainloop if it is in
415  * a poll/select and this is a multithreaded app.
416  */
417 static void
418 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
419                                                  DBusList *link)
420 {
421   HAVE_LOCK_CHECK (connection);
422   
423   _dbus_list_append_link (&connection->incoming_messages, link);
424
425   connection->n_incoming += 1;
426
427   _dbus_connection_wakeup_mainloop (connection);
428   
429   _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
430                  link->data, connection, connection->n_incoming);
431 }
432
433
434 /**
435  * Checks whether there are messages in the outgoing message queue.
436  * Called with connection lock held.
437  *
438  * @param connection the connection.
439  * @returns #TRUE if the outgoing queue is non-empty.
440  */
441 dbus_bool_t
442 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
443 {
444   HAVE_LOCK_CHECK (connection);
445   return connection->outgoing_messages != NULL;
446 }
447
448 /**
449  * Checks whether there are messages in the outgoing message queue.
450  *
451  * @param connection the connection.
452  * @returns #TRUE if the outgoing queue is non-empty.
453  */
454 dbus_bool_t
455 dbus_connection_has_messages_to_send (DBusConnection *connection)
456 {
457   dbus_bool_t v;
458   
459   _dbus_return_val_if_fail (connection != NULL, FALSE);
460
461   CONNECTION_LOCK (connection);
462   v = _dbus_connection_has_messages_to_send_unlocked (connection);
463   CONNECTION_UNLOCK (connection);
464
465   return v;
466 }
467
468 /**
469  * Gets the next outgoing message. The message remains in the
470  * queue, and the caller does not own a reference to it.
471  *
472  * @param connection the connection.
473  * @returns the message to be sent.
474  */ 
475 DBusMessage*
476 _dbus_connection_get_message_to_send (DBusConnection *connection)
477 {
478   HAVE_LOCK_CHECK (connection);
479   
480   return _dbus_list_get_last (&connection->outgoing_messages);
481 }
482
483 /**
484  * Notifies the connection that a message has been sent, so the
485  * message can be removed from the outgoing queue.
486  * Called with the connection lock held.
487  *
488  * @param connection the connection.
489  * @param message the message that was sent.
490  */
491 void
492 _dbus_connection_message_sent (DBusConnection *connection,
493                                DBusMessage    *message)
494 {
495   DBusList *link;
496
497   HAVE_LOCK_CHECK (connection);
498   
499   /* This can be called before we even complete authentication, since
500    * it's called on disconnect to clean up the outgoing queue.
501    * It's also called as we successfully send each message.
502    */
503   
504   link = _dbus_list_get_last_link (&connection->outgoing_messages);
505   _dbus_assert (link != NULL);
506   _dbus_assert (link->data == message);
507
508   /* Save this link in the link cache */
509   _dbus_list_unlink (&connection->outgoing_messages,
510                      link);
511   _dbus_list_prepend_link (&connection->link_cache, link);
512   
513   connection->n_outgoing -= 1;
514
515   _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
516                  message,
517                  dbus_message_get_type (message),
518                  dbus_message_get_path (message),
519                  dbus_message_get_interface (message) ?
520                  dbus_message_get_interface (message) :
521                  "no interface",
522                  dbus_message_get_member (message) ?
523                  dbus_message_get_member (message) :
524                  "no member",
525                  dbus_message_get_signature (message),
526                  connection, connection->n_outgoing);
527
528   /* Save this link in the link cache also */
529   _dbus_message_remove_size_counter (message, connection->outgoing_counter,
530                                      &link);
531   _dbus_list_prepend_link (&connection->link_cache, link);
532   
533   dbus_message_unref (message);
534 }
535
536 typedef dbus_bool_t (* DBusWatchAddFunction)     (DBusWatchList *list,
537                                                   DBusWatch     *watch);
538 typedef void        (* DBusWatchRemoveFunction)  (DBusWatchList *list,
539                                                   DBusWatch     *watch);
540 typedef void        (* DBusWatchToggleFunction)  (DBusWatchList *list,
541                                                   DBusWatch     *watch,
542                                                   dbus_bool_t    enabled);
543
544 static dbus_bool_t
545 protected_change_watch (DBusConnection         *connection,
546                         DBusWatch              *watch,
547                         DBusWatchAddFunction    add_function,
548                         DBusWatchRemoveFunction remove_function,
549                         DBusWatchToggleFunction toggle_function,
550                         dbus_bool_t             enabled)
551 {
552   DBusWatchList *watches;
553   dbus_bool_t retval;
554   
555   HAVE_LOCK_CHECK (connection);
556
557   /* This isn't really safe or reasonable; a better pattern is the "do everything, then
558    * drop lock and call out" one; but it has to be propagated up through all callers
559    */
560   
561   watches = connection->watches;
562   if (watches)
563     {
564       connection->watches = NULL;
565       _dbus_connection_ref_unlocked (connection);
566       CONNECTION_UNLOCK (connection);
567
568       if (add_function)
569         retval = (* add_function) (watches, watch);
570       else if (remove_function)
571         {
572           retval = TRUE;
573           (* remove_function) (watches, watch);
574         }
575       else
576         {
577           retval = TRUE;
578           (* toggle_function) (watches, watch, enabled);
579         }
580       
581       CONNECTION_LOCK (connection);
582       connection->watches = watches;
583       _dbus_connection_unref_unlocked (connection);
584
585       return retval;
586     }
587   else
588     return FALSE;
589 }
590      
591
592 /**
593  * Adds a watch using the connection's DBusAddWatchFunction if
594  * available. Otherwise records the watch to be added when said
595  * function is available. Also re-adds the watch if the
596  * DBusAddWatchFunction changes. May fail due to lack of memory.
597  *
598  * @param connection the connection.
599  * @param watch the watch to add.
600  * @returns #TRUE on success.
601  */
602 dbus_bool_t
603 _dbus_connection_add_watch (DBusConnection *connection,
604                             DBusWatch      *watch)
605 {
606   return protected_change_watch (connection, watch,
607                                  _dbus_watch_list_add_watch,
608                                  NULL, NULL, FALSE);
609 }
610
611 /**
612  * Removes a watch using the connection's DBusRemoveWatchFunction
613  * if available. It's an error to call this function on a watch
614  * that was not previously added.
615  *
616  * @param connection the connection.
617  * @param watch the watch to remove.
618  */
619 void
620 _dbus_connection_remove_watch (DBusConnection *connection,
621                                DBusWatch      *watch)
622 {
623   protected_change_watch (connection, watch,
624                           NULL,
625                           _dbus_watch_list_remove_watch,
626                           NULL, FALSE);
627 }
628
629 /**
630  * Toggles a watch and notifies app via connection's
631  * DBusWatchToggledFunction if available. It's an error to call this
632  * function on a watch that was not previously added.
633  * Connection lock should be held when calling this.
634  *
635  * @param connection the connection.
636  * @param watch the watch to toggle.
637  * @param enabled whether to enable or disable
638  */
639 void
640 _dbus_connection_toggle_watch (DBusConnection *connection,
641                                DBusWatch      *watch,
642                                dbus_bool_t     enabled)
643 {
644   _dbus_assert (watch != NULL);
645
646   protected_change_watch (connection, watch,
647                           NULL, NULL,
648                           _dbus_watch_list_toggle_watch,
649                           enabled);
650 }
651
652 typedef dbus_bool_t (* DBusTimeoutAddFunction)    (DBusTimeoutList *list,
653                                                    DBusTimeout     *timeout);
654 typedef void        (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
655                                                    DBusTimeout     *timeout);
656 typedef void        (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
657                                                    DBusTimeout     *timeout,
658                                                    dbus_bool_t      enabled);
659
660 static dbus_bool_t
661 protected_change_timeout (DBusConnection           *connection,
662                           DBusTimeout              *timeout,
663                           DBusTimeoutAddFunction    add_function,
664                           DBusTimeoutRemoveFunction remove_function,
665                           DBusTimeoutToggleFunction toggle_function,
666                           dbus_bool_t               enabled)
667 {
668   DBusTimeoutList *timeouts;
669   dbus_bool_t retval;
670   
671   HAVE_LOCK_CHECK (connection);
672
673   /* This isn't really safe or reasonable; a better pattern is the "do everything, then
674    * drop lock and call out" one; but it has to be propagated up through all callers
675    */
676   
677   timeouts = connection->timeouts;
678   if (timeouts)
679     {
680       connection->timeouts = NULL;
681       _dbus_connection_ref_unlocked (connection);
682       CONNECTION_UNLOCK (connection);
683
684       if (add_function)
685         retval = (* add_function) (timeouts, timeout);
686       else if (remove_function)
687         {
688           retval = TRUE;
689           (* remove_function) (timeouts, timeout);
690         }
691       else
692         {
693           retval = TRUE;
694           (* toggle_function) (timeouts, timeout, enabled);
695         }
696       
697       CONNECTION_LOCK (connection);
698       connection->timeouts = timeouts;
699       _dbus_connection_unref_unlocked (connection);
700
701       return retval;
702     }
703   else
704     return FALSE;
705 }
706
707 /**
708  * Adds a timeout using the connection's DBusAddTimeoutFunction if
709  * available. Otherwise records the timeout to be added when said
710  * function is available. Also re-adds the timeout if the
711  * DBusAddTimeoutFunction changes. May fail due to lack of memory.
712  * The timeout will fire repeatedly until removed.
713  *
714  * @param connection the connection.
715  * @param timeout the timeout to add.
716  * @returns #TRUE on success.
717  */
718 dbus_bool_t
719 _dbus_connection_add_timeout (DBusConnection *connection,
720                               DBusTimeout    *timeout)
721 {
722   return protected_change_timeout (connection, timeout,
723                                    _dbus_timeout_list_add_timeout,
724                                    NULL, NULL, FALSE);
725 }
726
727 /**
728  * Removes a timeout using the connection's DBusRemoveTimeoutFunction
729  * if available. It's an error to call this function on a timeout
730  * that was not previously added.
731  *
732  * @param connection the connection.
733  * @param timeout the timeout to remove.
734  */
735 void
736 _dbus_connection_remove_timeout (DBusConnection *connection,
737                                  DBusTimeout    *timeout)
738 {
739   protected_change_timeout (connection, timeout,
740                             NULL,
741                             _dbus_timeout_list_remove_timeout,
742                             NULL, FALSE);
743 }
744
745 /**
746  * Toggles a timeout and notifies app via connection's
747  * DBusTimeoutToggledFunction if available. It's an error to call this
748  * function on a timeout that was not previously added.
749  *
750  * @param connection the connection.
751  * @param timeout the timeout to toggle.
752  * @param enabled whether to enable or disable
753  */
754 void
755 _dbus_connection_toggle_timeout (DBusConnection   *connection,
756                                  DBusTimeout      *timeout,
757                                  dbus_bool_t       enabled)
758 {
759   protected_change_timeout (connection, timeout,
760                             NULL, NULL,
761                             _dbus_timeout_list_toggle_timeout,
762                             enabled);
763 }
764
765 static dbus_bool_t
766 _dbus_connection_attach_pending_call_unlocked (DBusConnection  *connection,
767                                                DBusPendingCall *pending)
768 {
769   HAVE_LOCK_CHECK (connection);
770   
771   _dbus_assert (pending->reply_serial != 0);
772
773   if (!_dbus_connection_add_timeout (connection, pending->timeout))
774     return FALSE;
775   
776   if (!_dbus_hash_table_insert_int (connection->pending_replies,
777                                     pending->reply_serial,
778                                     pending))
779     {
780       _dbus_connection_remove_timeout (connection, pending->timeout);
781
782       HAVE_LOCK_CHECK (connection);
783       return FALSE;
784     }
785   
786   pending->timeout_added = TRUE;
787   pending->connection = connection;
788
789   dbus_pending_call_ref (pending);
790
791   HAVE_LOCK_CHECK (connection);
792   
793   return TRUE;
794 }
795
796 static void
797 free_pending_call_on_hash_removal (void *data)
798 {
799   DBusPendingCall *pending;
800   
801   if (data == NULL)
802     return;
803
804   pending = data;
805
806   if (pending->connection)
807     {
808       if (pending->timeout_added)
809         {
810           _dbus_connection_remove_timeout (pending->connection,
811                                            pending->timeout);
812           pending->timeout_added = FALSE;
813         }
814
815       pending->connection = NULL;
816       
817       dbus_pending_call_unref (pending);
818     }
819 }
820
821 static void
822 _dbus_connection_detach_pending_call_unlocked (DBusConnection  *connection,
823                                                DBusPendingCall *pending)
824 {
825   /* Can't have a destroy notifier on the pending call if we're going to do this */
826
827   dbus_pending_call_ref (pending);
828   _dbus_hash_table_remove_int (connection->pending_replies,
829                                pending->reply_serial);
830   _dbus_assert (pending->connection == NULL);
831   dbus_pending_call_unref (pending);
832 }
833
834 static void
835 _dbus_connection_detach_pending_call_and_unlock (DBusConnection  *connection,
836                                                  DBusPendingCall *pending)
837 {
838   /* The idea here is to avoid finalizing the pending call
839    * with the lock held, since there's a destroy notifier
840    * in pending call that goes out to application code.
841    */
842   dbus_pending_call_ref (pending);
843   _dbus_hash_table_remove_int (connection->pending_replies,
844                                pending->reply_serial);
845   _dbus_assert (pending->connection == NULL);
846   CONNECTION_UNLOCK (connection);
847   dbus_pending_call_unref (pending);
848 }
849
850 /**
851  * Removes a pending call from the connection, such that
852  * the pending reply will be ignored. May drop the last
853  * reference to the pending call.
854  *
855  * @param connection the connection
856  * @param pending the pending call
857  */
858 void
859 _dbus_connection_remove_pending_call (DBusConnection  *connection,
860                                       DBusPendingCall *pending)
861 {
862   CONNECTION_LOCK (connection);
863   _dbus_connection_detach_pending_call_and_unlock (connection, pending);
864 }
865
866 /**
867  * Completes a pending call with the given message,
868  * or if the message is #NULL, by timing out the pending call.
869  * 
870  * @param pending the pending call
871  * @param message the message to complete the call with, or #NULL
872  *  to time out the call
873  */
874 void
875 _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
876                                         DBusMessage     *message)
877 {
878   if (message == NULL)
879     {
880       message = pending->timeout_link->data;
881       _dbus_list_clear (&pending->timeout_link);
882     }
883   else
884     dbus_message_ref (message);
885
886   _dbus_verbose ("  handing message %p (%s) to pending call serial %u\n",
887                  message,
888                  dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
889                  "method return" :
890                  dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
891                  "error" : "other type",
892                  pending->reply_serial);
893   
894   _dbus_assert (pending->reply == NULL);
895   _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
896   pending->reply = message;
897   
898   dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
899   _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending);
900   
901   /* Must be called unlocked since it invokes app callback */
902   _dbus_pending_call_notify (pending);
903   dbus_pending_call_unref (pending);
904 }
905
906 /**
907  * Acquire the transporter I/O path. This must be done before
908  * doing any I/O in the transporter. May sleep and drop the
909  * IO path mutex while waiting for the I/O path.
910  *
911  * @param connection the connection.
912  * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
913  * @returns TRUE if the I/O path was acquired.
914  */
915 static dbus_bool_t
916 _dbus_connection_acquire_io_path (DBusConnection *connection,
917                                   int timeout_milliseconds)
918 {
919   dbus_bool_t we_acquired;
920   
921   HAVE_LOCK_CHECK (connection);
922
923   /* We don't want the connection to vanish */
924   _dbus_connection_ref_unlocked (connection);
925
926   /* We will only touch io_path_acquired which is protected by our mutex */
927   CONNECTION_UNLOCK (connection);
928   
929   _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
930   _dbus_mutex_lock (connection->io_path_mutex);
931
932   _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
933                  _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
934
935   we_acquired = FALSE;
936   
937   if (connection->io_path_acquired)
938     {
939       if (timeout_milliseconds != -1)
940         {
941           _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
942                          _DBUS_FUNCTION_NAME, timeout_milliseconds);
943           _dbus_condvar_wait_timeout (connection->io_path_cond,
944                                       connection->io_path_mutex,
945                                       timeout_milliseconds);
946         }
947       else
948         {
949           while (connection->io_path_acquired)
950             {
951               _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
952               _dbus_condvar_wait (connection->io_path_cond, connection->io_path_mutex);
953             }
954         }
955     }
956   
957   if (!connection->io_path_acquired)
958     {
959       we_acquired = TRUE;
960       connection->io_path_acquired = TRUE;
961     }
962   
963   _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
964                  _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
965
966   _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
967   _dbus_mutex_unlock (connection->io_path_mutex);
968
969   CONNECTION_LOCK (connection);
970   
971   HAVE_LOCK_CHECK (connection);
972
973   _dbus_connection_unref_unlocked (connection);
974   
975   return we_acquired;
976 }
977
978 /**
979  * Release the I/O path when you're done with it. Only call
980  * after you've acquired the I/O. Wakes up at most one thread
981  * currently waiting to acquire the I/O path.
982  *
983  * @param connection the connection.
984  */
985 static void
986 _dbus_connection_release_io_path (DBusConnection *connection)
987 {
988   HAVE_LOCK_CHECK (connection);
989   
990   _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
991   _dbus_mutex_lock (connection->io_path_mutex);
992   
993   _dbus_assert (connection->io_path_acquired);
994
995   _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
996                  _DBUS_FUNCTION_NAME, connection->io_path_acquired);
997   
998   connection->io_path_acquired = FALSE;
999   _dbus_condvar_wake_one (connection->io_path_cond);
1000
1001   _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1002   _dbus_mutex_unlock (connection->io_path_mutex);
1003 }
1004
1005 /**
1006  * Queues incoming messages and sends outgoing messages for this
1007  * connection, optionally blocking in the process. Each call to
1008  * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1009  * time and then read or write data if possible.
1010  *
1011  * The purpose of this function is to be able to flush outgoing
1012  * messages or queue up incoming messages without returning
1013  * control to the application and causing reentrancy weirdness.
1014  *
1015  * The flags parameter allows you to specify whether to
1016  * read incoming messages, write outgoing messages, or both,
1017  * and whether to block if no immediate action is possible.
1018  *
1019  * The timeout_milliseconds parameter does nothing unless the
1020  * iteration is blocking.
1021  *
1022  * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1023  * wasn't specified, then it's impossible to block, even if
1024  * you specify DBUS_ITERATION_BLOCK; in that case the function
1025  * returns immediately.
1026  *
1027  * Called with connection lock held.
1028  * 
1029  * @param connection the connection.
1030  * @param flags iteration flags.
1031  * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1032  */
1033 void
1034 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1035                                         unsigned int    flags,
1036                                         int             timeout_milliseconds)
1037 {
1038   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1039   
1040   HAVE_LOCK_CHECK (connection);
1041   
1042   if (connection->n_outgoing == 0)
1043     flags &= ~DBUS_ITERATION_DO_WRITING;
1044
1045   if (_dbus_connection_acquire_io_path (connection,
1046                                         (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1047     {
1048       HAVE_LOCK_CHECK (connection);
1049       
1050       _dbus_transport_do_iteration (connection->transport,
1051                                     flags, timeout_milliseconds);
1052       _dbus_connection_release_io_path (connection);
1053     }
1054
1055   HAVE_LOCK_CHECK (connection);
1056
1057   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1058 }
1059
1060 /**
1061  * Creates a new connection for the given transport.  A transport
1062  * represents a message stream that uses some concrete mechanism, such
1063  * as UNIX domain sockets. May return #NULL if insufficient
1064  * memory exists to create the connection.
1065  *
1066  * @param transport the transport.
1067  * @returns the new connection, or #NULL on failure.
1068  */
1069 DBusConnection*
1070 _dbus_connection_new_for_transport (DBusTransport *transport)
1071 {
1072   DBusConnection *connection;
1073   DBusWatchList *watch_list;
1074   DBusTimeoutList *timeout_list;
1075   DBusHashTable *pending_replies;
1076   DBusMutex *mutex;
1077   DBusMutex *io_path_mutex;
1078   DBusMutex *dispatch_mutex;
1079   DBusCondVar *message_returned_cond;
1080   DBusCondVar *dispatch_cond;
1081   DBusCondVar *io_path_cond;
1082   DBusList *disconnect_link;
1083   DBusMessage *disconnect_message;
1084   DBusCounter *outgoing_counter;
1085   DBusObjectTree *objects;
1086   
1087   watch_list = NULL;
1088   connection = NULL;
1089   pending_replies = NULL;
1090   timeout_list = NULL;
1091   mutex = NULL;
1092   io_path_mutex = NULL;
1093   dispatch_mutex = NULL;
1094   message_returned_cond = NULL;
1095   dispatch_cond = NULL;
1096   io_path_cond = NULL;
1097   disconnect_link = NULL;
1098   disconnect_message = NULL;
1099   outgoing_counter = NULL;
1100   objects = NULL;
1101   
1102   watch_list = _dbus_watch_list_new ();
1103   if (watch_list == NULL)
1104     goto error;
1105
1106   timeout_list = _dbus_timeout_list_new ();
1107   if (timeout_list == NULL)
1108     goto error;  
1109
1110   pending_replies =
1111     _dbus_hash_table_new (DBUS_HASH_INT,
1112                           NULL,
1113                           (DBusFreeFunction)free_pending_call_on_hash_removal);
1114   if (pending_replies == NULL)
1115     goto error;
1116   
1117   connection = dbus_new0 (DBusConnection, 1);
1118   if (connection == NULL)
1119     goto error;
1120
1121   mutex = _dbus_mutex_new ();
1122   if (mutex == NULL)
1123     goto error;
1124
1125   io_path_mutex = _dbus_mutex_new ();
1126   if (io_path_mutex == NULL)
1127     goto error;
1128
1129   dispatch_mutex = _dbus_mutex_new ();
1130   if (dispatch_mutex == NULL)
1131     goto error;
1132   
1133   message_returned_cond = _dbus_condvar_new ();
1134   if (message_returned_cond == NULL)
1135     goto error;
1136   
1137   dispatch_cond = _dbus_condvar_new ();
1138   if (dispatch_cond == NULL)
1139     goto error;
1140   
1141   io_path_cond = _dbus_condvar_new ();
1142   if (io_path_cond == NULL)
1143     goto error;
1144
1145   disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1146                                                 DBUS_INTERFACE_LOCAL,
1147                                                 "Disconnected");
1148   
1149   if (disconnect_message == NULL)
1150     goto error;
1151
1152   disconnect_link = _dbus_list_alloc_link (disconnect_message);
1153   if (disconnect_link == NULL)
1154     goto error;
1155
1156   outgoing_counter = _dbus_counter_new ();
1157   if (outgoing_counter == NULL)
1158     goto error;
1159
1160   objects = _dbus_object_tree_new (connection);
1161   if (objects == NULL)
1162     goto error;
1163   
1164   if (_dbus_modify_sigpipe)
1165     _dbus_disable_sigpipe ();
1166   
1167   connection->refcount.value = 1;
1168   connection->mutex = mutex;
1169   connection->dispatch_cond = dispatch_cond;
1170   connection->dispatch_mutex = dispatch_mutex;
1171   connection->io_path_cond = io_path_cond;
1172   connection->io_path_mutex = io_path_mutex;
1173   connection->transport = transport;
1174   connection->watches = watch_list;
1175   connection->timeouts = timeout_list;
1176   connection->pending_replies = pending_replies;
1177   connection->outgoing_counter = outgoing_counter;
1178   connection->filter_list = NULL;
1179   connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1180   connection->objects = objects;
1181   connection->exit_on_disconnect = FALSE;
1182   connection->shareable = FALSE;
1183 #ifndef DBUS_DISABLE_CHECKS
1184   connection->generation = _dbus_current_generation;
1185 #endif
1186   
1187   _dbus_data_slot_list_init (&connection->slot_list);
1188
1189   connection->client_serial = 1;
1190
1191   connection->disconnect_message_link = disconnect_link;
1192
1193   CONNECTION_LOCK (connection);
1194   
1195   if (!_dbus_transport_set_connection (transport, connection))
1196     goto error;
1197
1198   _dbus_transport_ref (transport);
1199
1200   CONNECTION_UNLOCK (connection);
1201   
1202   return connection;
1203   
1204  error:
1205   if (disconnect_message != NULL)
1206     dbus_message_unref (disconnect_message);
1207   
1208   if (disconnect_link != NULL)
1209     _dbus_list_free_link (disconnect_link);
1210   
1211   if (io_path_cond != NULL)
1212     _dbus_condvar_free (io_path_cond);
1213   
1214   if (dispatch_cond != NULL)
1215     _dbus_condvar_free (dispatch_cond);
1216   
1217   if (message_returned_cond != NULL)
1218     _dbus_condvar_free (message_returned_cond);
1219   
1220   if (mutex != NULL)
1221     _dbus_mutex_free (mutex);
1222
1223   if (io_path_mutex != NULL)
1224     _dbus_mutex_free (io_path_mutex);
1225
1226   if (dispatch_mutex != NULL)
1227     _dbus_mutex_free (dispatch_mutex);
1228   
1229   if (connection != NULL)
1230     dbus_free (connection);
1231
1232   if (pending_replies)
1233     _dbus_hash_table_unref (pending_replies);
1234   
1235   if (watch_list)
1236     _dbus_watch_list_free (watch_list);
1237
1238   if (timeout_list)
1239     _dbus_timeout_list_free (timeout_list);
1240
1241   if (outgoing_counter)
1242     _dbus_counter_unref (outgoing_counter);
1243
1244   if (objects)
1245     _dbus_object_tree_unref (objects);
1246   
1247   return NULL;
1248 }
1249
1250 /**
1251  * Increments the reference count of a DBusConnection.
1252  * Requires that the caller already holds the connection lock.
1253  *
1254  * @param connection the connection.
1255  * @returns the connection.
1256  */
1257 DBusConnection *
1258 _dbus_connection_ref_unlocked (DBusConnection *connection)
1259 {  
1260   _dbus_assert (connection != NULL);
1261   _dbus_assert (connection->generation == _dbus_current_generation);
1262
1263   HAVE_LOCK_CHECK (connection);
1264   
1265 #ifdef DBUS_HAVE_ATOMIC_INT
1266   _dbus_atomic_inc (&connection->refcount);
1267 #else
1268   _dbus_assert (connection->refcount.value > 0);
1269   connection->refcount.value += 1;
1270 #endif
1271
1272   return connection;
1273 }
1274
1275 /**
1276  * Decrements the reference count of a DBusConnection.
1277  * Requires that the caller already holds the connection lock.
1278  *
1279  * @param connection the connection.
1280  */
1281 void
1282 _dbus_connection_unref_unlocked (DBusConnection *connection)
1283 {
1284   dbus_bool_t last_unref;
1285
1286   HAVE_LOCK_CHECK (connection);
1287   
1288   _dbus_assert (connection != NULL);
1289
1290   /* The connection lock is better than the global
1291    * lock in the atomic increment fallback
1292    */
1293   
1294 #ifdef DBUS_HAVE_ATOMIC_INT
1295   last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1296 #else
1297   _dbus_assert (connection->refcount.value > 0);
1298
1299   connection->refcount.value -= 1;
1300   last_unref = (connection->refcount.value == 0);  
1301 #if 0
1302   printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1303 #endif
1304 #endif
1305   
1306   if (last_unref)
1307     _dbus_connection_last_unref (connection);
1308 }
1309
1310 static dbus_uint32_t
1311 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1312 {
1313   int serial;
1314
1315   serial = connection->client_serial++;
1316
1317   if (connection->client_serial < 0)
1318     connection->client_serial = 1;
1319   
1320   return serial;
1321 }
1322
1323 /**
1324  * A callback for use with dbus_watch_new() to create a DBusWatch.
1325  * 
1326  * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1327  * and the virtual handle_watch in DBusTransport if we got rid of it.
1328  * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1329  * implementation.
1330  *
1331  * @param watch the watch.
1332  * @param condition the current condition of the file descriptors being watched.
1333  * @param data must be a pointer to a #DBusConnection
1334  * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1335  */
1336 dbus_bool_t
1337 _dbus_connection_handle_watch (DBusWatch                   *watch,
1338                                unsigned int                 condition,
1339                                void                        *data)
1340 {
1341   DBusConnection *connection;
1342   dbus_bool_t retval;
1343   DBusDispatchStatus status;
1344
1345   connection = data;
1346
1347   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1348   
1349   CONNECTION_LOCK (connection);
1350   _dbus_connection_acquire_io_path (connection, -1);
1351   HAVE_LOCK_CHECK (connection);
1352   retval = _dbus_transport_handle_watch (connection->transport,
1353                                          watch, condition);
1354
1355   _dbus_connection_release_io_path (connection);
1356
1357   HAVE_LOCK_CHECK (connection);
1358
1359   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1360   
1361   status = _dbus_connection_get_dispatch_status_unlocked (connection);
1362
1363   /* this calls out to user code */
1364   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1365
1366   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1367   
1368   return retval;
1369 }
1370
1371 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1372 static DBusHashTable *shared_connections = NULL;
1373
1374 static void
1375 shared_connections_shutdown (void *data)
1376 {
1377   _DBUS_LOCK (shared_connections);
1378
1379   _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1380   _dbus_hash_table_unref (shared_connections);
1381   shared_connections = NULL;
1382   
1383   _DBUS_UNLOCK (shared_connections);
1384 }
1385
1386 static dbus_bool_t
1387 connection_lookup_shared (DBusAddressEntry  *entry,
1388                           DBusConnection   **result)
1389 {
1390   _dbus_verbose ("checking for existing connection\n");
1391   
1392   *result = NULL;
1393   
1394   _DBUS_LOCK (shared_connections);
1395
1396   if (shared_connections == NULL)
1397     {
1398       _dbus_verbose ("creating shared_connections hash table\n");
1399       
1400       shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1401                                                  dbus_free,
1402                                                  NULL);
1403       if (shared_connections == NULL)
1404         {
1405           _DBUS_UNLOCK (shared_connections);
1406           return FALSE;
1407         }
1408
1409       if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1410         {
1411           _dbus_hash_table_unref (shared_connections);
1412           shared_connections = NULL;
1413           _DBUS_UNLOCK (shared_connections);
1414           return FALSE;
1415         }
1416
1417       _dbus_verbose ("  successfully created shared_connections\n");
1418       
1419       _DBUS_UNLOCK (shared_connections);
1420       return TRUE; /* no point looking up in the hash we just made */
1421     }
1422   else
1423     {
1424       const char *guid;
1425
1426       guid = dbus_address_entry_get_value (entry, "guid");
1427       
1428       if (guid != NULL)
1429         {
1430           *result = _dbus_hash_table_lookup_string (shared_connections,
1431                                                     guid);
1432
1433           if (*result)
1434             {
1435               /* The DBusConnection can't have been disconnected
1436                * between the lookup and this code, because the
1437                * disconnection will take the shared_connections lock to
1438                * remove the connection. It can't have been finalized
1439                * since you have to disconnect prior to finalize.
1440                *
1441                * Thus it's safe to ref the connection.
1442                */
1443               dbus_connection_ref (*result);
1444
1445               _dbus_verbose ("looked up existing connection to server guid %s\n",
1446                              guid);
1447             }
1448         }
1449       
1450       _DBUS_UNLOCK (shared_connections);
1451       return TRUE;
1452     }
1453 }
1454
1455 static dbus_bool_t
1456 connection_record_shared_unlocked (DBusConnection *connection,
1457                                    const char     *guid)
1458 {
1459   char *guid_key;
1460   char *guid_in_connection;
1461
1462   /* A separate copy of the key is required in the hash table, because
1463    * we don't have a lock on the connection when we are doing a hash
1464    * lookup.
1465    */
1466   
1467   _dbus_assert (connection->server_guid == NULL);
1468   _dbus_assert (connection->shareable);
1469   
1470   guid_key = _dbus_strdup (guid);
1471   if (guid_key == NULL)
1472     return FALSE;
1473
1474   guid_in_connection = _dbus_strdup (guid);
1475   if (guid_in_connection == NULL)
1476     {
1477       dbus_free (guid_key);
1478       return FALSE;
1479     }
1480   
1481   _DBUS_LOCK (shared_connections);
1482   _dbus_assert (shared_connections != NULL);
1483   
1484   if (!_dbus_hash_table_insert_string (shared_connections,
1485                                        guid_key, connection))
1486     {
1487       dbus_free (guid_key);
1488       dbus_free (guid_in_connection);
1489       _DBUS_UNLOCK (shared_connections);
1490       return FALSE;
1491     }
1492
1493   connection->server_guid = guid_in_connection;
1494
1495   _dbus_verbose ("stored connection to %s to be shared\n",
1496                  connection->server_guid);
1497   
1498   _DBUS_UNLOCK (shared_connections);
1499
1500   _dbus_assert (connection->server_guid != NULL);
1501   
1502   return TRUE;
1503 }
1504
1505 static void
1506 connection_forget_shared_unlocked (DBusConnection *connection)
1507 {
1508   HAVE_LOCK_CHECK (connection);
1509   
1510   if (connection->server_guid == NULL)
1511     return;
1512
1513   _dbus_verbose ("dropping connection to %s out of the shared table\n",
1514                  connection->server_guid);
1515   
1516   _DBUS_LOCK (shared_connections);
1517
1518   if (!_dbus_hash_table_remove_string (shared_connections,
1519                                        connection->server_guid))
1520     _dbus_assert_not_reached ("connection was not in the shared table");
1521   
1522   dbus_free (connection->server_guid);
1523   connection->server_guid = NULL;
1524
1525   _DBUS_UNLOCK (shared_connections);
1526 }
1527
1528 static DBusConnection*
1529 connection_try_from_address_entry (DBusAddressEntry *entry,
1530                                    DBusError        *error)
1531 {
1532   DBusTransport *transport;
1533   DBusConnection *connection;
1534
1535   transport = _dbus_transport_open (entry, error);
1536
1537   if (transport == NULL)
1538     {
1539       _DBUS_ASSERT_ERROR_IS_SET (error);
1540       return NULL;
1541     }
1542
1543   connection = _dbus_connection_new_for_transport (transport);
1544
1545   _dbus_transport_unref (transport);
1546   
1547   if (connection == NULL)
1548     {
1549       _DBUS_SET_OOM (error);
1550       return NULL;
1551     }
1552
1553 #ifndef DBUS_DISABLE_CHECKS
1554   _dbus_assert (!connection->have_connection_lock);
1555 #endif
1556   return connection;
1557 }
1558
1559 /*
1560  * If the shared parameter is true, then any existing connection will
1561  * be used (and if a new connection is created, it will be available
1562  * for use by others). If the shared parameter is false, a new
1563  * connection will always be created, and the new connection will
1564  * never be returned to other callers.
1565  *
1566  * @param address the address
1567  * @param shared whether the connection is shared or private
1568  * @param error error return
1569  * @returns the connection or #NULL on error
1570  */
1571 static DBusConnection*
1572 _dbus_connection_open_internal (const char     *address,
1573                                 dbus_bool_t     shared,
1574                                 DBusError      *error)
1575 {
1576   DBusConnection *connection;
1577   DBusAddressEntry **entries;
1578   DBusError tmp_error;
1579   DBusError first_error;
1580   int len, i;
1581
1582   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1583
1584   _dbus_verbose ("opening %s connection to: %s\n",
1585                  shared ? "shared" : "private", address);
1586   
1587   if (!dbus_parse_address (address, &entries, &len, error))
1588     return NULL;
1589
1590   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1591   
1592   connection = NULL;
1593
1594   dbus_error_init (&tmp_error);
1595   dbus_error_init (&first_error);
1596   for (i = 0; i < len; i++)
1597     {
1598       if (shared)
1599         {
1600           if (!connection_lookup_shared (entries[i], &connection))
1601             _DBUS_SET_OOM (&tmp_error);
1602         }
1603
1604       if (connection == NULL)
1605         {
1606           connection = connection_try_from_address_entry (entries[i],
1607                                                           &tmp_error);
1608           
1609           if (connection != NULL && shared)
1610             {
1611               const char *guid;
1612
1613               connection->shareable = TRUE;
1614               
1615               guid = dbus_address_entry_get_value (entries[i], "guid");
1616
1617               /* we don't have a connection lock but we know nobody
1618                * else has a handle to the connection
1619                */
1620               
1621               if (guid &&
1622                   !connection_record_shared_unlocked (connection, guid))
1623                 {
1624                   _DBUS_SET_OOM (&tmp_error);
1625                   dbus_connection_disconnect (connection);
1626                   dbus_connection_unref (connection);
1627                   connection = NULL;
1628                 }
1629
1630               /* but as of now the connection is possibly shared
1631                * since another thread could have pulled it from the table
1632                */
1633             }
1634         }
1635       
1636       if (connection)
1637         break;
1638
1639       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1640       
1641       if (i == 0)
1642         dbus_move_error (&tmp_error, &first_error);
1643       else
1644         dbus_error_free (&tmp_error);
1645     }
1646
1647   /* NOTE we don't have a lock on a possibly-shared connection object */
1648   
1649   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1650   _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1651   
1652   if (connection == NULL)
1653     {
1654       _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1655       dbus_move_error (&first_error, error);
1656     }
1657   else
1658     {
1659       dbus_error_free (&first_error);
1660     }
1661   
1662   dbus_address_entries_free (entries);
1663   return connection;
1664 }
1665
1666 /** @} */
1667
1668 /**
1669  * @addtogroup DBusConnection
1670  *
1671  * @{
1672  */
1673
1674 /**
1675  * Gets a connection to a remote address. If a connection to the given
1676  * address already exists, returns the existing connection with its
1677  * reference count incremented.  Otherwise, returns a new connection
1678  * and saves the new connection for possible re-use if a future call
1679  * to dbus_connection_open() asks to connect to the same server.
1680  *
1681  * Use dbus_connection_open_private() to get a dedicated connection
1682  * not shared with other callers of dbus_connection_open().
1683  *
1684  * If the open fails, the function returns #NULL, and provides a
1685  * reason for the failure in the error parameter. Pass #NULL for the
1686  * error parameter if you aren't interested in the reason for
1687  * failure.
1688  * 
1689  * @param address the address.
1690  * @param error address where an error can be returned.
1691  * @returns new connection, or #NULL on failure.
1692  */
1693 DBusConnection*
1694 dbus_connection_open (const char     *address,
1695                       DBusError      *error)
1696 {
1697   DBusConnection *connection;
1698
1699   _dbus_return_val_if_fail (address != NULL, NULL);
1700   _dbus_return_val_if_error_is_set (error, NULL);
1701
1702   connection = _dbus_connection_open_internal (address,
1703                                                TRUE,
1704                                                error);
1705
1706   return connection;
1707 }
1708
1709 /**
1710  * Opens a new, dedicated connection to a remote address. Unlike
1711  * dbus_connection_open(), always creates a new connection.
1712  * This connection will not be saved or recycled by libdbus.
1713  *
1714  * If the open fails, the function returns #NULL, and provides a
1715  * reason for the failure in the error parameter. Pass #NULL for the
1716  * error parameter if you aren't interested in the reason for
1717  * failure.
1718  * 
1719  * @param address the address.
1720  * @param error address where an error can be returned.
1721  * @returns new connection, or #NULL on failure.
1722  */
1723 DBusConnection*
1724 dbus_connection_open_private (const char     *address,
1725                               DBusError      *error)
1726 {
1727   DBusConnection *connection;
1728
1729   _dbus_return_val_if_fail (address != NULL, NULL);
1730   _dbus_return_val_if_error_is_set (error, NULL);
1731
1732   connection = _dbus_connection_open_internal (address,
1733                                                FALSE,
1734                                                error);
1735
1736   return connection;
1737 }
1738
1739 /**
1740  * Increments the reference count of a DBusConnection.
1741  *
1742  * @param connection the connection.
1743  * @returns the connection.
1744  */
1745 DBusConnection *
1746 dbus_connection_ref (DBusConnection *connection)
1747 {
1748   _dbus_return_val_if_fail (connection != NULL, NULL);
1749   _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
1750   
1751   /* The connection lock is better than the global
1752    * lock in the atomic increment fallback
1753    */
1754   
1755 #ifdef DBUS_HAVE_ATOMIC_INT
1756   _dbus_atomic_inc (&connection->refcount);
1757 #else
1758   CONNECTION_LOCK (connection);
1759   _dbus_assert (connection->refcount.value > 0);
1760
1761   connection->refcount.value += 1;
1762   CONNECTION_UNLOCK (connection);
1763 #endif
1764
1765   return connection;
1766 }
1767
1768 static void
1769 free_outgoing_message (void *element,
1770                        void *data)
1771 {
1772   DBusMessage *message = element;
1773   DBusConnection *connection = data;
1774
1775   _dbus_message_remove_size_counter (message,
1776                                      connection->outgoing_counter,
1777                                      NULL);
1778   dbus_message_unref (message);
1779 }
1780
1781 /* This is run without the mutex held, but after the last reference
1782  * to the connection has been dropped we should have no thread-related
1783  * problems
1784  */
1785 static void
1786 _dbus_connection_last_unref (DBusConnection *connection)
1787 {
1788   DBusList *link;
1789
1790   _dbus_verbose ("Finalizing connection %p\n", connection);
1791   
1792   _dbus_assert (connection->refcount.value == 0);
1793   
1794   /* You have to disconnect the connection before unref:ing it. Otherwise
1795    * you won't get the disconnected message.
1796    */
1797   _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1798   _dbus_assert (connection->server_guid == NULL);
1799   
1800   /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1801   _dbus_object_tree_free_all_unlocked (connection->objects);
1802   
1803   dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1804   dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1805   dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1806   
1807   _dbus_watch_list_free (connection->watches);
1808   connection->watches = NULL;
1809   
1810   _dbus_timeout_list_free (connection->timeouts);
1811   connection->timeouts = NULL;
1812
1813   _dbus_data_slot_list_free (&connection->slot_list);
1814   
1815   link = _dbus_list_get_first_link (&connection->filter_list);
1816   while (link != NULL)
1817     {
1818       DBusMessageFilter *filter = link->data;
1819       DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1820
1821       filter->function = NULL;
1822       _dbus_message_filter_unref (filter); /* calls app callback */
1823       link->data = NULL;
1824       
1825       link = next;
1826     }
1827   _dbus_list_clear (&connection->filter_list);
1828   
1829   /* ---- Done with stuff that invokes application callbacks */
1830
1831   _dbus_object_tree_unref (connection->objects);  
1832
1833   _dbus_hash_table_unref (connection->pending_replies);
1834   connection->pending_replies = NULL;
1835   
1836   _dbus_list_clear (&connection->filter_list);
1837   
1838   _dbus_list_foreach (&connection->outgoing_messages,
1839                       free_outgoing_message,
1840                       connection);
1841   _dbus_list_clear (&connection->outgoing_messages);
1842   
1843   _dbus_list_foreach (&connection->incoming_messages,
1844                       (DBusForeachFunction) dbus_message_unref,
1845                       NULL);
1846   _dbus_list_clear (&connection->incoming_messages);
1847
1848   _dbus_counter_unref (connection->outgoing_counter);
1849
1850   _dbus_transport_unref (connection->transport);
1851
1852   if (connection->disconnect_message_link)
1853     {
1854       DBusMessage *message = connection->disconnect_message_link->data;
1855       dbus_message_unref (message);
1856       _dbus_list_free_link (connection->disconnect_message_link);
1857     }
1858
1859   _dbus_list_clear (&connection->link_cache);
1860   
1861   _dbus_condvar_free (connection->dispatch_cond);
1862   _dbus_condvar_free (connection->io_path_cond);
1863
1864   _dbus_mutex_free (connection->io_path_mutex);
1865   _dbus_mutex_free (connection->dispatch_mutex);
1866
1867   _dbus_mutex_free (connection->mutex);
1868   
1869   dbus_free (connection);
1870 }
1871
1872 /**
1873  * Decrements the reference count of a DBusConnection, and finalizes
1874  * it if the count reaches zero.  It is a bug to drop the last reference
1875  * to a connection that has not been disconnected.
1876  *
1877  * @todo in practice it can be quite tricky to never unref a connection
1878  * that's still connected; maybe there's some way we could avoid
1879  * the requirement.
1880  *
1881  * @param connection the connection.
1882  */
1883 void
1884 dbus_connection_unref (DBusConnection *connection)
1885 {
1886   dbus_bool_t last_unref;
1887
1888   _dbus_return_if_fail (connection != NULL);
1889   _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1890   
1891   /* The connection lock is better than the global
1892    * lock in the atomic increment fallback
1893    */
1894   
1895 #ifdef DBUS_HAVE_ATOMIC_INT
1896   last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1897 #else
1898   CONNECTION_LOCK (connection);
1899   
1900   _dbus_assert (connection->refcount.value > 0);
1901
1902   connection->refcount.value -= 1;
1903   last_unref = (connection->refcount.value == 0);
1904
1905 #if 0
1906   printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1907 #endif
1908   
1909   CONNECTION_UNLOCK (connection);
1910 #endif
1911   
1912   if (last_unref)
1913     _dbus_connection_last_unref (connection);
1914 }
1915
1916 /**
1917  * Closes the connection, so no further data can be sent or received.
1918  * Any further attempts to send data will result in errors.  This
1919  * function does not affect the connection's reference count.  It's
1920  * safe to disconnect a connection more than once; all calls after the
1921  * first do nothing. It's impossible to "reconnect" a connection, a
1922  * new connection must be created. This function may result in a call
1923  * to the DBusDispatchStatusFunction set with
1924  * dbus_connection_set_dispatch_status_function(), as the disconnect
1925  * message it generates needs to be dispatched.
1926  *
1927  * @param connection the connection.
1928  */
1929 void
1930 dbus_connection_disconnect (DBusConnection *connection)
1931 {
1932   DBusDispatchStatus status;
1933   
1934   _dbus_return_if_fail (connection != NULL);
1935   _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1936
1937   _dbus_verbose ("Disconnecting %p\n", connection);
1938   
1939   CONNECTION_LOCK (connection);
1940   
1941   _dbus_transport_disconnect (connection->transport);
1942
1943   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1944   status = _dbus_connection_get_dispatch_status_unlocked (connection);
1945
1946   /* this calls out to user code */
1947   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1948 }
1949
1950 static dbus_bool_t
1951 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1952 {
1953   HAVE_LOCK_CHECK (connection);
1954   return _dbus_transport_get_is_connected (connection->transport);
1955 }
1956
1957 /**
1958  * Gets whether the connection is currently connected.  All
1959  * connections are connected when they are opened.  A connection may
1960  * become disconnected when the remote application closes its end, or
1961  * exits; a connection may also be disconnected with
1962  * dbus_connection_disconnect().
1963  *
1964  * @param connection the connection.
1965  * @returns #TRUE if the connection is still alive.
1966  */
1967 dbus_bool_t
1968 dbus_connection_get_is_connected (DBusConnection *connection)
1969 {
1970   dbus_bool_t res;
1971
1972   _dbus_return_val_if_fail (connection != NULL, FALSE);
1973   
1974   CONNECTION_LOCK (connection);
1975   res = _dbus_connection_get_is_connected_unlocked (connection);
1976   CONNECTION_UNLOCK (connection);
1977   
1978   return res;
1979 }
1980
1981 /**
1982  * Gets whether the connection was authenticated. (Note that
1983  * if the connection was authenticated then disconnected,
1984  * this function still returns #TRUE)
1985  *
1986  * @param connection the connection
1987  * @returns #TRUE if the connection was ever authenticated
1988  */
1989 dbus_bool_t
1990 dbus_connection_get_is_authenticated (DBusConnection *connection)
1991 {
1992   dbus_bool_t res;
1993
1994   _dbus_return_val_if_fail (connection != NULL, FALSE);
1995   
1996   CONNECTION_LOCK (connection);
1997   res = _dbus_transport_get_is_authenticated (connection->transport);
1998   CONNECTION_UNLOCK (connection);
1999   
2000   return res;
2001 }
2002
2003 /**
2004  * Set whether _exit() should be called when the connection receives a
2005  * disconnect signal. The call to _exit() comes after any handlers for
2006  * the disconnect signal run; handlers can cancel the exit by calling
2007  * this function.
2008  *
2009  * By default, exit_on_disconnect is #FALSE; but for message bus
2010  * connections returned from dbus_bus_get() it will be toggled on
2011  * by default.
2012  *
2013  * @param connection the connection
2014  * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
2015  */
2016 void
2017 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
2018                                         dbus_bool_t     exit_on_disconnect)
2019 {
2020   _dbus_return_if_fail (connection != NULL);
2021
2022   CONNECTION_LOCK (connection);
2023   connection->exit_on_disconnect = exit_on_disconnect != FALSE;
2024   CONNECTION_UNLOCK (connection);
2025 }
2026
2027 static DBusPreallocatedSend*
2028 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
2029 {
2030   DBusPreallocatedSend *preallocated;
2031
2032   HAVE_LOCK_CHECK (connection);
2033   
2034   _dbus_assert (connection != NULL);
2035   
2036   preallocated = dbus_new (DBusPreallocatedSend, 1);
2037   if (preallocated == NULL)
2038     return NULL;
2039
2040   if (connection->link_cache != NULL)
2041     {
2042       preallocated->queue_link =
2043         _dbus_list_pop_first_link (&connection->link_cache);
2044       preallocated->queue_link->data = NULL;
2045     }
2046   else
2047     {
2048       preallocated->queue_link = _dbus_list_alloc_link (NULL);
2049       if (preallocated->queue_link == NULL)
2050         goto failed_0;
2051     }
2052   
2053   if (connection->link_cache != NULL)
2054     {
2055       preallocated->counter_link =
2056         _dbus_list_pop_first_link (&connection->link_cache);
2057       preallocated->counter_link->data = connection->outgoing_counter;
2058     }
2059   else
2060     {
2061       preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
2062       if (preallocated->counter_link == NULL)
2063         goto failed_1;
2064     }
2065
2066   _dbus_counter_ref (preallocated->counter_link->data);
2067
2068   preallocated->connection = connection;
2069   
2070   return preallocated;
2071   
2072  failed_1:
2073   _dbus_list_free_link (preallocated->queue_link);
2074  failed_0:
2075   dbus_free (preallocated);
2076   
2077   return NULL;
2078 }
2079
2080 /**
2081  * Preallocates resources needed to send a message, allowing the message 
2082  * to be sent without the possibility of memory allocation failure.
2083  * Allows apps to create a future guarantee that they can send
2084  * a message regardless of memory shortages.
2085  *
2086  * @param connection the connection we're preallocating for.
2087  * @returns the preallocated resources, or #NULL
2088  */
2089 DBusPreallocatedSend*
2090 dbus_connection_preallocate_send (DBusConnection *connection)
2091 {
2092   DBusPreallocatedSend *preallocated;
2093
2094   _dbus_return_val_if_fail (connection != NULL, NULL);
2095
2096   CONNECTION_LOCK (connection);
2097   
2098   preallocated =
2099     _dbus_connection_preallocate_send_unlocked (connection);
2100
2101   CONNECTION_UNLOCK (connection);
2102
2103   return preallocated;
2104 }
2105
2106 /**
2107  * Frees preallocated message-sending resources from
2108  * dbus_connection_preallocate_send(). Should only
2109  * be called if the preallocated resources are not used
2110  * to send a message.
2111  *
2112  * @param connection the connection
2113  * @param preallocated the resources
2114  */
2115 void
2116 dbus_connection_free_preallocated_send (DBusConnection       *connection,
2117                                         DBusPreallocatedSend *preallocated)
2118 {
2119   _dbus_return_if_fail (connection != NULL);
2120   _dbus_return_if_fail (preallocated != NULL);  
2121   _dbus_return_if_fail (connection == preallocated->connection);
2122
2123   _dbus_list_free_link (preallocated->queue_link);
2124   _dbus_counter_unref (preallocated->counter_link->data);
2125   _dbus_list_free_link (preallocated->counter_link);
2126   dbus_free (preallocated);
2127 }
2128
2129 /* Called with lock held, does not update dispatch status */
2130 static void
2131 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection       *connection,
2132                                                        DBusPreallocatedSend *preallocated,
2133                                                        DBusMessage          *message,
2134                                                        dbus_uint32_t        *client_serial)
2135 {
2136   dbus_uint32_t serial;
2137   const char *sig;
2138
2139   preallocated->queue_link->data = message;
2140   _dbus_list_prepend_link (&connection->outgoing_messages,
2141                            preallocated->queue_link);
2142
2143   _dbus_message_add_size_counter_link (message,
2144                                        preallocated->counter_link);
2145
2146   dbus_free (preallocated);
2147   preallocated = NULL;
2148   
2149   dbus_message_ref (message);
2150   
2151   connection->n_outgoing += 1;
2152
2153   sig = dbus_message_get_signature (message);
2154   
2155   _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
2156                  message,
2157                  dbus_message_get_type (message),
2158                  dbus_message_get_path (message),
2159                  dbus_message_get_interface (message) ?
2160                  dbus_message_get_interface (message) :
2161                  "no interface",
2162                  dbus_message_get_member (message) ?
2163                  dbus_message_get_member (message) :
2164                  "no member",
2165                  sig,
2166                  dbus_message_get_destination (message) ?
2167                  dbus_message_get_destination (message) :
2168                  "null",
2169                  connection,
2170                  connection->n_outgoing);
2171
2172   if (dbus_message_get_serial (message) == 0)
2173     {
2174       serial = _dbus_connection_get_next_client_serial (connection);
2175       _dbus_message_set_serial (message, serial);
2176       if (client_serial)
2177         *client_serial = serial;
2178     }
2179   else
2180     {
2181       if (client_serial)
2182         *client_serial = dbus_message_get_serial (message);
2183     }
2184
2185   _dbus_verbose ("Message %p serial is %u\n",
2186                  message, dbus_message_get_serial (message));
2187   
2188   _dbus_message_lock (message);
2189
2190   /* Now we need to run an iteration to hopefully just write the messages
2191    * out immediately, and otherwise get them queued up
2192    */
2193   _dbus_connection_do_iteration_unlocked (connection,
2194                                           DBUS_ITERATION_DO_WRITING,
2195                                           -1);
2196
2197   /* If stuff is still queued up, be sure we wake up the main loop */
2198   if (connection->n_outgoing > 0)
2199     _dbus_connection_wakeup_mainloop (connection);
2200 }
2201
2202 static void
2203 _dbus_connection_send_preallocated_and_unlock (DBusConnection       *connection,
2204                                                DBusPreallocatedSend *preallocated,
2205                                                DBusMessage          *message,
2206                                                dbus_uint32_t        *client_serial)
2207 {
2208   DBusDispatchStatus status;
2209
2210   HAVE_LOCK_CHECK (connection);
2211   
2212   _dbus_connection_send_preallocated_unlocked_no_update (connection,
2213                                                          preallocated,
2214                                                          message, client_serial);
2215
2216   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2217   status = _dbus_connection_get_dispatch_status_unlocked (connection);
2218
2219   /* this calls out to user code */
2220   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2221 }
2222
2223 /**
2224  * Sends a message using preallocated resources. This function cannot fail.
2225  * It works identically to dbus_connection_send() in other respects.
2226  * Preallocated resources comes from dbus_connection_preallocate_send().
2227  * This function "consumes" the preallocated resources, they need not
2228  * be freed separately.
2229  *
2230  * @param connection the connection
2231  * @param preallocated the preallocated resources
2232  * @param message the message to send
2233  * @param client_serial return location for client serial assigned to the message
2234  */
2235 void
2236 dbus_connection_send_preallocated (DBusConnection       *connection,
2237                                    DBusPreallocatedSend *preallocated,
2238                                    DBusMessage          *message,
2239                                    dbus_uint32_t        *client_serial)
2240 {
2241   _dbus_return_if_fail (connection != NULL);
2242   _dbus_return_if_fail (preallocated != NULL);
2243   _dbus_return_if_fail (message != NULL);
2244   _dbus_return_if_fail (preallocated->connection == connection);
2245   _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
2246                         (dbus_message_get_interface (message) != NULL &&
2247                          dbus_message_get_member (message) != NULL));
2248   _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
2249                         (dbus_message_get_interface (message) != NULL &&
2250                          dbus_message_get_member (message) != NULL));
2251   
2252   CONNECTION_LOCK (connection);
2253   _dbus_connection_send_preallocated_and_unlock (connection,
2254                                                  preallocated,
2255                                                  message, client_serial);
2256 }
2257
2258 static dbus_bool_t
2259 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
2260                                           DBusMessage    *message,
2261                                           dbus_uint32_t  *client_serial)
2262 {
2263   DBusPreallocatedSend *preallocated;
2264
2265   _dbus_assert (connection != NULL);
2266   _dbus_assert (message != NULL);
2267   
2268   preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2269   if (preallocated == NULL)
2270     return FALSE;
2271
2272   _dbus_connection_send_preallocated_unlocked_no_update (connection,
2273                                                          preallocated,
2274                                                          message,
2275                                                          client_serial);
2276   return TRUE;
2277 }
2278
2279 dbus_bool_t
2280 _dbus_connection_send_and_unlock (DBusConnection *connection,
2281                                   DBusMessage    *message,
2282                                   dbus_uint32_t  *client_serial)
2283 {
2284   DBusPreallocatedSend *preallocated;
2285
2286   _dbus_assert (connection != NULL);
2287   _dbus_assert (message != NULL);
2288   
2289   preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2290   if (preallocated == NULL)
2291     {
2292       CONNECTION_UNLOCK (connection);
2293       return FALSE;
2294     }
2295
2296   _dbus_connection_send_preallocated_and_unlock (connection,
2297                                                  preallocated,
2298                                                  message,
2299                                                  client_serial);
2300   return TRUE;
2301 }
2302
2303 /**
2304  * Adds a message to the outgoing message queue. Does not block to
2305  * write the message to the network; that happens asynchronously. To
2306  * force the message to be written, call dbus_connection_flush().
2307  * Because this only queues the message, the only reason it can
2308  * fail is lack of memory. Even if the connection is disconnected,
2309  * no error will be returned.
2310  *
2311  * If the function fails due to lack of memory, it returns #FALSE.
2312  * The function will never fail for other reasons; even if the
2313  * connection is disconnected, you can queue an outgoing message,
2314  * though obviously it won't be sent.
2315  * 
2316  * @param connection the connection.
2317  * @param message the message to write.
2318  * @param client_serial return location for client serial.
2319  * @returns #TRUE on success.
2320  */
2321 dbus_bool_t
2322 dbus_connection_send (DBusConnection *connection,
2323                       DBusMessage    *message,
2324                       dbus_uint32_t  *client_serial)
2325 {
2326   _dbus_return_val_if_fail (connection != NULL, FALSE);
2327   _dbus_return_val_if_fail (message != NULL, FALSE);
2328
2329   CONNECTION_LOCK (connection);
2330
2331   return _dbus_connection_send_and_unlock (connection,
2332                                            message,
2333                                            client_serial);
2334 }
2335
2336 static dbus_bool_t
2337 reply_handler_timeout (void *data)
2338 {
2339   DBusConnection *connection;
2340   DBusDispatchStatus status;
2341   DBusPendingCall *pending = data;
2342
2343   connection = pending->connection;
2344   
2345   CONNECTION_LOCK (connection);
2346   if (pending->timeout_link)
2347     {
2348       _dbus_connection_queue_synthesized_message_link (connection,
2349                                                        pending->timeout_link);
2350       pending->timeout_link = NULL;
2351     }
2352
2353   _dbus_connection_remove_timeout (connection,
2354                                    pending->timeout);
2355   pending->timeout_added = FALSE;
2356
2357   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2358   status = _dbus_connection_get_dispatch_status_unlocked (connection);
2359
2360   /* Unlocks, and calls out to user code */
2361   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2362   
2363   return TRUE;
2364 }
2365
2366 /**
2367  * Queues a message to send, as with dbus_connection_send_message(),
2368  * but also returns a #DBusPendingCall used to receive a reply to the
2369  * message. If no reply is received in the given timeout_milliseconds,
2370  * this function expires the pending reply and generates a synthetic
2371  * error reply (generated in-process, not by the remote application)
2372  * indicating that a timeout occurred.
2373  *
2374  * A #DBusPendingCall will see a reply message after any filters, but
2375  * before any object instances or other handlers. A #DBusPendingCall
2376  * will always see exactly one reply message, unless it's cancelled
2377  * with dbus_pending_call_cancel().
2378  * 
2379  * If a filter filters out the reply before the handler sees it, the
2380  * reply is immediately timed out and a timeout error reply is
2381  * generated. If a filter removes the timeout error reply then the
2382  * #DBusPendingCall will get confused. Filtering the timeout error
2383  * is thus considered a bug and will print a warning.
2384  * 
2385  * If #NULL is passed for the pending_return, the #DBusPendingCall
2386  * will still be generated internally, and used to track
2387  * the message reply timeout. This means a timeout error will
2388  * occur if no reply arrives, unlike with dbus_connection_send().
2389  *
2390  * If -1 is passed for the timeout, a sane default timeout is used. -1
2391  * is typically the best value for the timeout for this reason, unless
2392  * you want a very short or very long timeout.  There is no way to
2393  * avoid a timeout entirely, other than passing INT_MAX for the
2394  * timeout to postpone it indefinitely.
2395  * 
2396  * @param connection the connection
2397  * @param message the message to send
2398  * @param pending_return return location for a #DBusPendingCall object, or #NULL
2399  * @param timeout_milliseconds timeout in milliseconds or -1 for default
2400  * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
2401  *
2402  */
2403 dbus_bool_t
2404 dbus_connection_send_with_reply (DBusConnection     *connection,
2405                                  DBusMessage        *message,
2406                                  DBusPendingCall   **pending_return,
2407                                  int                 timeout_milliseconds)
2408 {
2409   DBusPendingCall *pending;
2410   DBusMessage *reply;
2411   DBusList *reply_link;
2412   dbus_int32_t serial = -1;
2413   DBusDispatchStatus status;
2414
2415   _dbus_return_val_if_fail (connection != NULL, FALSE);
2416   _dbus_return_val_if_fail (message != NULL, FALSE);
2417   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2418
2419   if (pending_return)
2420     *pending_return = NULL;
2421   
2422   pending = _dbus_pending_call_new (connection,
2423                                     timeout_milliseconds,
2424                                     reply_handler_timeout);
2425
2426   if (pending == NULL)
2427     return FALSE;
2428
2429   CONNECTION_LOCK (connection);
2430   
2431   /* Assign a serial to the message */
2432   if (dbus_message_get_serial (message) == 0)
2433     {
2434       serial = _dbus_connection_get_next_client_serial (connection);
2435       _dbus_message_set_serial (message, serial);
2436     }
2437
2438   pending->reply_serial = serial;
2439
2440   reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
2441                                   "No reply within specified time");
2442   if (reply == NULL)
2443     goto error;
2444
2445   reply_link = _dbus_list_alloc_link (reply);
2446   if (reply_link == NULL)
2447     {
2448       CONNECTION_UNLOCK (connection);
2449       dbus_message_unref (reply);
2450       goto error_unlocked;
2451     }
2452
2453   pending->timeout_link = reply_link;
2454
2455   /* Insert the serial in the pending replies hash;
2456    * hash takes a refcount on DBusPendingCall.
2457    * Also, add the timeout.
2458    */
2459   if (!_dbus_connection_attach_pending_call_unlocked (connection,
2460                                                       pending))
2461     goto error;
2462  
2463   if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
2464     {
2465       _dbus_connection_detach_pending_call_and_unlock (connection,
2466                                                        pending);
2467       goto error_unlocked;
2468     }
2469
2470   if (pending_return)
2471     *pending_return = pending;
2472   else
2473     {
2474       _dbus_connection_detach_pending_call_unlocked (connection, pending);
2475       dbus_pending_call_unref (pending);
2476     }
2477
2478   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2479   status = _dbus_connection_get_dispatch_status_unlocked (connection);
2480
2481   /* this calls out to user code */
2482   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2483
2484   return TRUE;
2485
2486  error:
2487   CONNECTION_UNLOCK (connection);
2488  error_unlocked:
2489   dbus_pending_call_unref (pending);
2490   return FALSE;
2491 }
2492
2493 /* This is slightly strange since we can pop a message here without
2494  * the dispatch lock.
2495  */
2496 static DBusMessage*
2497 check_for_reply_unlocked (DBusConnection *connection,
2498                           dbus_uint32_t   client_serial)
2499 {
2500   DBusList *link;
2501
2502   HAVE_LOCK_CHECK (connection);
2503   
2504   link = _dbus_list_get_first_link (&connection->incoming_messages);
2505
2506   while (link != NULL)
2507     {
2508       DBusMessage *reply = link->data;
2509
2510       if (dbus_message_get_reply_serial (reply) == client_serial)
2511         {
2512           _dbus_list_remove_link (&connection->incoming_messages, link);
2513           connection->n_incoming  -= 1;
2514           return reply;
2515         }
2516       link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2517     }
2518
2519   return NULL;
2520 }
2521
2522 /**
2523  * When a function that blocks has been called with a timeout, and we
2524  * run out of memory, the time to wait for memory is based on the
2525  * timeout. If the caller was willing to block a long time we wait a
2526  * relatively long time for memory, if they were only willing to block
2527  * briefly then we retry for memory at a rapid rate.
2528  *
2529  * @timeout_milliseconds the timeout requested for blocking
2530  */
2531 static void
2532 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2533 {
2534   if (timeout_milliseconds == -1)
2535     _dbus_sleep_milliseconds (1000);
2536   else if (timeout_milliseconds < 100)
2537     ; /* just busy loop */
2538   else if (timeout_milliseconds <= 1000)
2539     _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2540   else
2541     _dbus_sleep_milliseconds (1000);
2542 }
2543
2544 /**
2545  * Blocks until a pending call times out or gets a reply.
2546  *
2547  * Does not re-enter the main loop or run filter/path-registered
2548  * callbacks. The reply to the message will not be seen by
2549  * filter callbacks.
2550  *
2551  * Returns immediately if pending call already got a reply.
2552  * 
2553  * @todo could use performance improvements (it keeps scanning
2554  * the whole message queue for example)
2555  *
2556  * @param pending the pending call we block for a reply on
2557  */
2558 void
2559 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2560 {
2561   long start_tv_sec, start_tv_usec;
2562   long end_tv_sec, end_tv_usec;
2563   long tv_sec, tv_usec;
2564   DBusDispatchStatus status;
2565   DBusConnection *connection;
2566   dbus_uint32_t client_serial;
2567   int timeout_milliseconds;
2568
2569   _dbus_assert (pending != NULL);
2570
2571   if (dbus_pending_call_get_completed (pending))
2572     return;
2573
2574   if (pending->connection == NULL)
2575     return; /* call already detached */
2576
2577   dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2578   
2579   connection = pending->connection;
2580   client_serial = pending->reply_serial;
2581
2582   /* note that timeout_milliseconds is limited to a smallish value
2583    * in _dbus_pending_call_new() so overflows aren't possible
2584    * below
2585    */
2586   timeout_milliseconds = dbus_timeout_get_interval (pending->timeout);
2587
2588   /* Flush message queue */
2589   dbus_connection_flush (connection);
2590
2591   CONNECTION_LOCK (connection);
2592
2593   _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2594   end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2595   end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2596   end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2597   end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2598
2599   _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec to %ld sec %ld usec\n",
2600                  timeout_milliseconds,
2601                  client_serial,
2602                  start_tv_sec, start_tv_usec,
2603                  end_tv_sec, end_tv_usec);
2604
2605   /* Now we wait... */
2606   /* always block at least once as we know we don't have the reply yet */
2607   _dbus_connection_do_iteration_unlocked (connection,
2608                                           DBUS_ITERATION_DO_READING |
2609                                           DBUS_ITERATION_BLOCK,
2610                                           timeout_milliseconds);
2611
2612  recheck_status:
2613
2614   _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2615   
2616   HAVE_LOCK_CHECK (connection);
2617   
2618   /* queue messages and get status */
2619
2620   status = _dbus_connection_get_dispatch_status_unlocked (connection);
2621
2622   /* the get_completed() is in case a dispatch() while we were blocking
2623    * got the reply instead of us.
2624    */
2625   if (dbus_pending_call_get_completed (pending))
2626     {
2627       _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2628       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2629       dbus_pending_call_unref (pending);
2630       return;
2631     }
2632   
2633   if (status == DBUS_DISPATCH_DATA_REMAINS)
2634     {
2635       DBusMessage *reply;
2636       
2637       reply = check_for_reply_unlocked (connection, client_serial);
2638       if (reply != NULL)
2639         {
2640           _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2641
2642           _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2643           
2644           _dbus_pending_call_complete_and_unlock (pending, reply);
2645           dbus_message_unref (reply);
2646
2647           CONNECTION_LOCK (connection);
2648           status = _dbus_connection_get_dispatch_status_unlocked (connection);
2649           _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2650           dbus_pending_call_unref (pending);
2651           
2652           return;
2653         }
2654     }
2655   
2656   _dbus_get_current_time (&tv_sec, &tv_usec);
2657   
2658   if (!_dbus_connection_get_is_connected_unlocked (connection))
2659     {
2660       /* FIXME send a "DBUS_ERROR_DISCONNECTED" instead, just to help
2661        * programmers understand what went wrong since the timeout is
2662        * confusing
2663        */
2664       
2665       _dbus_pending_call_complete_and_unlock (pending, NULL);
2666       dbus_pending_call_unref (pending);
2667       return;
2668     }
2669   else if (tv_sec < start_tv_sec)
2670     _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2671   else if (connection->disconnect_message_link == NULL)
2672     _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2673   else if (tv_sec < end_tv_sec ||
2674            (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2675     {
2676       timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2677         (end_tv_usec - tv_usec) / 1000;
2678       _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2679       _dbus_assert (timeout_milliseconds >= 0);
2680       
2681       if (status == DBUS_DISPATCH_NEED_MEMORY)
2682         {
2683           /* Try sleeping a bit, as we aren't sure we need to block for reading,
2684            * we may already have a reply in the buffer and just can't process
2685            * it.
2686            */
2687           _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2688
2689           _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2690         }
2691       else
2692         {          
2693           /* block again, we don't have the reply buffered yet. */
2694           _dbus_connection_do_iteration_unlocked (connection,
2695                                                   DBUS_ITERATION_DO_READING |
2696                                                   DBUS_ITERATION_BLOCK,
2697                                                   timeout_milliseconds);
2698         }
2699
2700       goto recheck_status;
2701     }
2702
2703   _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2704                  (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2705
2706   _dbus_assert (!dbus_pending_call_get_completed (pending));
2707   
2708   /* unlock and call user code */
2709   _dbus_pending_call_complete_and_unlock (pending, NULL);
2710
2711   /* update user code on dispatch status */
2712   CONNECTION_LOCK (connection);
2713   status = _dbus_connection_get_dispatch_status_unlocked (connection);
2714   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2715   dbus_pending_call_unref (pending);
2716 }
2717
2718 /**
2719  * Sends a message and blocks a certain time period while waiting for
2720  * a reply.  This function does not reenter the main loop,
2721  * i.e. messages other than the reply are queued up but not
2722  * processed. This function is used to do non-reentrant "method
2723  * calls."
2724  * 
2725  * If a normal reply is received, it is returned, and removed from the
2726  * incoming message queue. If it is not received, #NULL is returned
2727  * and the error is set to #DBUS_ERROR_NO_REPLY.  If an error reply is
2728  * received, it is converted to a #DBusError and returned as an error,
2729  * then the reply message is deleted. If something else goes wrong,
2730  * result is set to whatever is appropriate, such as
2731  * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
2732  *
2733  * @param connection the connection
2734  * @param message the message to send
2735  * @param timeout_milliseconds timeout in milliseconds or -1 for default
2736  * @param error return location for error message
2737  * @returns the message that is the reply or #NULL with an error code if the
2738  * function fails.
2739  */
2740 DBusMessage*
2741 dbus_connection_send_with_reply_and_block (DBusConnection     *connection,
2742                                            DBusMessage        *message,
2743                                            int                 timeout_milliseconds,
2744                                            DBusError          *error)
2745 {
2746   DBusMessage *reply;
2747   DBusPendingCall *pending;
2748   
2749   _dbus_return_val_if_fail (connection != NULL, NULL);
2750   _dbus_return_val_if_fail (message != NULL, NULL);
2751   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);  
2752   _dbus_return_val_if_error_is_set (error, NULL);
2753   
2754   if (!dbus_connection_send_with_reply (connection, message,
2755                                         &pending, timeout_milliseconds))
2756     {
2757       _DBUS_SET_OOM (error);
2758       return NULL;
2759     }
2760
2761   _dbus_assert (pending != NULL);
2762   
2763   dbus_pending_call_block (pending);
2764
2765   reply = dbus_pending_call_steal_reply (pending);
2766   dbus_pending_call_unref (pending);
2767
2768   /* call_complete_and_unlock() called from pending_call_block() should
2769    * always fill this in.
2770    */
2771   _dbus_assert (reply != NULL);
2772   
2773    if (dbus_set_error_from_message (error, reply))
2774     {
2775       dbus_message_unref (reply);
2776       return NULL;
2777     }
2778   else
2779     return reply;
2780 }
2781
2782 /**
2783  * Blocks until the outgoing message queue is empty.
2784  *
2785  * @param connection the connection.
2786  */
2787 void
2788 dbus_connection_flush (DBusConnection *connection)
2789 {
2790   /* We have to specify DBUS_ITERATION_DO_READING here because
2791    * otherwise we could have two apps deadlock if they are both doing
2792    * a flush(), and the kernel buffers fill up. This could change the
2793    * dispatch status.
2794    */
2795   DBusDispatchStatus status;
2796
2797   _dbus_return_if_fail (connection != NULL);
2798   
2799   CONNECTION_LOCK (connection);
2800   while (connection->n_outgoing > 0 &&
2801          _dbus_connection_get_is_connected_unlocked (connection))
2802     {
2803       _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
2804       HAVE_LOCK_CHECK (connection);
2805       _dbus_connection_do_iteration_unlocked (connection,
2806                                               DBUS_ITERATION_DO_READING |
2807                                               DBUS_ITERATION_DO_WRITING |
2808                                               DBUS_ITERATION_BLOCK,
2809                                               -1);
2810     }
2811
2812   HAVE_LOCK_CHECK (connection);
2813   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2814   status = _dbus_connection_get_dispatch_status_unlocked (connection);
2815
2816   HAVE_LOCK_CHECK (connection);
2817   /* Unlocks and calls out to user code */
2818   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2819
2820   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
2821 }
2822
2823 /**
2824  * This function is intended for use with applications that don't want
2825  * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
2826  * example usage would be:
2827  * 
2828  * @code
2829  *   while (dbus_connection_read_write_dispatch (connection, -1))
2830  *     ; // empty loop body
2831  * @endcode
2832  * 
2833  * In this usage you would normally have set up a filter function to look
2834  * at each message as it is dispatched. The loop terminates when the last
2835  * message from the connection (the disconnected signal) is processed.
2836  * 
2837  * If there are messages to dispatch, this function will
2838  * dbus_connection_dispatch() once, and return. If there are no
2839  * messages to dispatch, this function will block until it can read or
2840  * write, then read or write, then return.
2841  *
2842  * The way to think of this function is that it either makes some sort
2843  * of progress, or it blocks.
2844  *
2845  * The return value indicates whether the disconnect message has been
2846  * processed, NOT whether the connection is connected. This is
2847  * important because even after disconnecting, you want to process any
2848  * messages you received prior to the disconnect.
2849  *
2850  * @param connection the connection
2851  * @param timeout_milliseconds max time to block or -1 for infinite
2852  * @returns #TRUE if the disconnect message has not been processed
2853  */
2854 dbus_bool_t
2855 dbus_connection_read_write_dispatch (DBusConnection *connection,
2856                                      int             timeout_milliseconds)
2857 {
2858   DBusDispatchStatus dstatus;
2859   dbus_bool_t dispatched_disconnected;
2860   
2861   _dbus_return_val_if_fail (connection != NULL, FALSE);
2862   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2863   dstatus = dbus_connection_get_dispatch_status (connection);
2864
2865   if (dstatus == DBUS_DISPATCH_DATA_REMAINS)
2866     {
2867       _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
2868       dbus_connection_dispatch (connection);
2869       CONNECTION_LOCK (connection);
2870     }
2871   else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
2872     {
2873       _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
2874       _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2875       CONNECTION_LOCK (connection);
2876     }
2877   else
2878     {
2879       CONNECTION_LOCK (connection);
2880       if (_dbus_connection_get_is_connected_unlocked (connection))
2881         {
2882           _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
2883           _dbus_connection_do_iteration_unlocked (connection,
2884                                                   DBUS_ITERATION_DO_READING |
2885                                                   DBUS_ITERATION_DO_WRITING |
2886                                                   DBUS_ITERATION_BLOCK,
2887                                                   timeout_milliseconds);
2888         }
2889     }
2890   
2891   HAVE_LOCK_CHECK (connection);
2892   dispatched_disconnected = connection->n_incoming == 0 &&
2893     connection->disconnect_message_link == NULL;
2894   CONNECTION_UNLOCK (connection);
2895   return !dispatched_disconnected; /* TRUE if we have not processed disconnected */
2896 }
2897
2898 /**
2899  * Returns the first-received message from the incoming message queue,
2900  * leaving it in the queue. If the queue is empty, returns #NULL.
2901  * 
2902  * The caller does not own a reference to the returned message, and
2903  * must either return it using dbus_connection_return_message() or
2904  * keep it after calling dbus_connection_steal_borrowed_message(). No
2905  * one can get at the message while its borrowed, so return it as
2906  * quickly as possible and don't keep a reference to it after
2907  * returning it. If you need to keep the message, make a copy of it.
2908  *
2909  * dbus_connection_dispatch() will block if called while a borrowed
2910  * message is outstanding; only one piece of code can be playing with
2911  * the incoming queue at a time. This function will block if called
2912  * during a dbus_connection_dispatch().
2913  *
2914  * @param connection the connection.
2915  * @returns next message in the incoming queue.
2916  */
2917 DBusMessage*
2918 dbus_connection_borrow_message (DBusConnection *connection)
2919 {
2920   DBusDispatchStatus status;
2921   DBusMessage *message;
2922
2923   _dbus_return_val_if_fail (connection != NULL, NULL);
2924
2925   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
2926   
2927   /* this is called for the side effect that it queues
2928    * up any messages from the transport
2929    */
2930   status = dbus_connection_get_dispatch_status (connection);
2931   if (status != DBUS_DISPATCH_DATA_REMAINS)
2932     return NULL;
2933   
2934   CONNECTION_LOCK (connection);
2935
2936   _dbus_connection_acquire_dispatch (connection);
2937
2938   /* While a message is outstanding, the dispatch lock is held */
2939   _dbus_assert (connection->message_borrowed == NULL);
2940
2941   connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
2942   
2943   message = connection->message_borrowed;
2944
2945   /* Note that we KEEP the dispatch lock until the message is returned */
2946   if (message == NULL)
2947     _dbus_connection_release_dispatch (connection);
2948
2949   CONNECTION_UNLOCK (connection);
2950   
2951   return message;
2952 }
2953
2954 /**
2955  * Used to return a message after peeking at it using
2956  * dbus_connection_borrow_message(). Only called if
2957  * message from dbus_connection_borrow_message() was non-#NULL.
2958  *
2959  * @param connection the connection
2960  * @param message the message from dbus_connection_borrow_message()
2961  */
2962 void
2963 dbus_connection_return_message (DBusConnection *connection,
2964                                 DBusMessage    *message)
2965 {
2966   _dbus_return_if_fail (connection != NULL);
2967   _dbus_return_if_fail (message != NULL);
2968   _dbus_return_if_fail (message == connection->message_borrowed);
2969   _dbus_return_if_fail (connection->dispatch_acquired);
2970   
2971   CONNECTION_LOCK (connection);
2972   
2973   _dbus_assert (message == connection->message_borrowed);
2974   
2975   connection->message_borrowed = NULL;
2976
2977   _dbus_connection_release_dispatch (connection);
2978   
2979   CONNECTION_UNLOCK (connection);
2980 }
2981
2982 /**
2983  * Used to keep a message after peeking at it using
2984  * dbus_connection_borrow_message(). Before using this function, see
2985  * the caveats/warnings in the documentation for
2986  * dbus_connection_pop_message().
2987  *
2988  * @param connection the connection
2989  * @param message the message from dbus_connection_borrow_message()
2990  */
2991 void
2992 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2993                                         DBusMessage    *message)
2994 {
2995   DBusMessage *pop_message;
2996
2997   _dbus_return_if_fail (connection != NULL);
2998   _dbus_return_if_fail (message != NULL);
2999   _dbus_return_if_fail (message == connection->message_borrowed);
3000   _dbus_return_if_fail (connection->dispatch_acquired);
3001   
3002   CONNECTION_LOCK (connection);
3003  
3004   _dbus_assert (message == connection->message_borrowed);
3005
3006   pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3007   _dbus_assert (message == pop_message);
3008   
3009   connection->n_incoming -= 1;
3010  
3011   _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3012                  message, connection->n_incoming);
3013  
3014   connection->message_borrowed = NULL;
3015
3016   _dbus_connection_release_dispatch (connection);
3017   
3018   CONNECTION_UNLOCK (connection);
3019 }
3020
3021 /* See dbus_connection_pop_message, but requires the caller to own
3022  * the lock before calling. May drop the lock while running.
3023  */
3024 static DBusList*
3025 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3026 {
3027   HAVE_LOCK_CHECK (connection);
3028   
3029   _dbus_assert (connection->message_borrowed == NULL);
3030   
3031   if (connection->n_incoming > 0)
3032     {
3033       DBusList *link;
3034
3035       link = _dbus_list_pop_first_link (&connection->incoming_messages);
3036       connection->n_incoming -= 1;
3037
3038       _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3039                      link->data,
3040                      dbus_message_get_type (link->data),
3041                      dbus_message_get_path (link->data), 
3042                      dbus_message_get_interface (link->data) ?
3043                      dbus_message_get_interface (link->data) :
3044                      "no interface",
3045                      dbus_message_get_member (link->data) ?
3046                      dbus_message_get_member (link->data) :
3047                      "no member",
3048                      dbus_message_get_signature (link->data),
3049                      connection, connection->n_incoming);
3050
3051       return link;
3052     }
3053   else
3054     return NULL;
3055 }
3056
3057 /* See dbus_connection_pop_message, but requires the caller to own
3058  * the lock before calling. May drop the lock while running.
3059  */
3060 static DBusMessage*
3061 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3062 {
3063   DBusList *link;
3064
3065   HAVE_LOCK_CHECK (connection);
3066   
3067   link = _dbus_connection_pop_message_link_unlocked (connection);
3068
3069   if (link != NULL)
3070     {
3071       DBusMessage *message;
3072       
3073       message = link->data;
3074       
3075       _dbus_list_free_link (link);
3076       
3077       return message;
3078     }
3079   else
3080     return NULL;
3081 }
3082
3083 static void
3084 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3085                                                 DBusList       *message_link)
3086 {
3087   HAVE_LOCK_CHECK (connection);
3088   
3089   _dbus_assert (message_link != NULL);
3090   /* You can't borrow a message while a link is outstanding */
3091   _dbus_assert (connection->message_borrowed == NULL);
3092   /* We had to have the dispatch lock across the pop/putback */
3093   _dbus_assert (connection->dispatch_acquired);
3094
3095   _dbus_list_prepend_link (&connection->incoming_messages,
3096                            message_link);
3097   connection->n_incoming += 1;
3098
3099   _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3100                  message_link->data,
3101                  dbus_message_get_type (message_link->data),
3102                  dbus_message_get_interface (message_link->data) ?
3103                  dbus_message_get_interface (message_link->data) :
3104                  "no interface",
3105                  dbus_message_get_member (message_link->data) ?
3106                  dbus_message_get_member (message_link->data) :
3107                  "no member",
3108                  dbus_message_get_signature (message_link->data),
3109                  connection, connection->n_incoming);
3110 }
3111
3112 /**
3113  * Returns the first-received message from the incoming message queue,
3114  * removing it from the queue. The caller owns a reference to the
3115  * returned message. If the queue is empty, returns #NULL.
3116  *
3117  * This function bypasses any message handlers that are registered,
3118  * and so using it is usually wrong. Instead, let the main loop invoke
3119  * dbus_connection_dispatch(). Popping messages manually is only
3120  * useful in very simple programs that don't share a #DBusConnection
3121  * with any libraries or other modules.
3122  *
3123  * There is a lock that covers all ways of accessing the incoming message
3124  * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3125  * dbus_connection_borrow_message(), etc. will all block while one of the others
3126  * in the group is running.
3127  * 
3128  * @param connection the connection.
3129  * @returns next message in the incoming queue.
3130  */
3131 DBusMessage*
3132 dbus_connection_pop_message (DBusConnection *connection)
3133 {
3134   DBusMessage *message;
3135   DBusDispatchStatus status;
3136
3137   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3138   
3139   /* this is called for the side effect that it queues
3140    * up any messages from the transport
3141    */
3142   status = dbus_connection_get_dispatch_status (connection);
3143   if (status != DBUS_DISPATCH_DATA_REMAINS)
3144     return NULL;
3145   
3146   CONNECTION_LOCK (connection);
3147   _dbus_connection_acquire_dispatch (connection);
3148   HAVE_LOCK_CHECK (connection);
3149   
3150   message = _dbus_connection_pop_message_unlocked (connection);
3151
3152   _dbus_verbose ("Returning popped message %p\n", message);    
3153
3154   _dbus_connection_release_dispatch (connection);
3155   CONNECTION_UNLOCK (connection);
3156   
3157   return message;
3158 }
3159
3160 /**
3161  * Acquire the dispatcher. This is a separate lock so the main
3162  * connection lock can be dropped to call out to application dispatch
3163  * handlers.
3164  *
3165  * @param connection the connection.
3166  */
3167 static void
3168 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3169 {
3170   HAVE_LOCK_CHECK (connection);
3171
3172   _dbus_connection_ref_unlocked (connection);
3173   CONNECTION_UNLOCK (connection);
3174   
3175   _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3176   _dbus_mutex_lock (connection->dispatch_mutex);
3177
3178   while (connection->dispatch_acquired)
3179     {
3180       _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3181       _dbus_condvar_wait (connection->dispatch_cond, connection->dispatch_mutex);
3182     }
3183   
3184   _dbus_assert (!connection->dispatch_acquired);
3185
3186   connection->dispatch_acquired = TRUE;
3187
3188   _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3189   _dbus_mutex_unlock (connection->dispatch_mutex);
3190   
3191   CONNECTION_LOCK (connection);
3192   _dbus_connection_unref_unlocked (connection);
3193 }
3194
3195 /**
3196  * Release the dispatcher when you're done with it. Only call
3197  * after you've acquired the dispatcher. Wakes up at most one
3198  * thread currently waiting to acquire the dispatcher.
3199  *
3200  * @param connection the connection.
3201  */
3202 static void
3203 _dbus_connection_release_dispatch (DBusConnection *connection)
3204 {
3205   HAVE_LOCK_CHECK (connection);
3206   
3207   _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3208   _dbus_mutex_lock (connection->dispatch_mutex);
3209   
3210   _dbus_assert (connection->dispatch_acquired);
3211
3212   connection->dispatch_acquired = FALSE;
3213   _dbus_condvar_wake_one (connection->dispatch_cond);
3214
3215   _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3216   _dbus_mutex_unlock (connection->dispatch_mutex);
3217 }
3218
3219 static void
3220 _dbus_connection_failed_pop (DBusConnection *connection,
3221                              DBusList       *message_link)
3222 {
3223   _dbus_list_prepend_link (&connection->incoming_messages,
3224                            message_link);
3225   connection->n_incoming += 1;
3226 }
3227
3228 static DBusDispatchStatus
3229 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3230 {
3231   HAVE_LOCK_CHECK (connection);
3232   
3233   if (connection->n_incoming > 0)
3234     return DBUS_DISPATCH_DATA_REMAINS;
3235   else if (!_dbus_transport_queue_messages (connection->transport))
3236     return DBUS_DISPATCH_NEED_MEMORY;
3237   else
3238     {
3239       DBusDispatchStatus status;
3240       dbus_bool_t is_connected;
3241       
3242       status = _dbus_transport_get_dispatch_status (connection->transport);
3243       is_connected = _dbus_transport_get_is_connected (connection->transport);
3244
3245       _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3246                      DISPATCH_STATUS_NAME (status), is_connected);
3247       
3248       if (!is_connected)
3249         {
3250           if (status == DBUS_DISPATCH_COMPLETE &&
3251               connection->disconnect_message_link)
3252             {
3253               _dbus_verbose ("Sending disconnect message from %s\n",
3254                              _DBUS_FUNCTION_NAME);
3255
3256               connection_forget_shared_unlocked (connection);
3257               
3258               /* We haven't sent the disconnect message already,
3259                * and all real messages have been queued up.
3260                */
3261               _dbus_connection_queue_synthesized_message_link (connection,
3262                                                                connection->disconnect_message_link);
3263               connection->disconnect_message_link = NULL;
3264             }
3265
3266           /* Dump the outgoing queue, we aren't going to be able to
3267            * send it now, and we'd like accessors like
3268            * dbus_connection_get_outgoing_size() to be accurate.
3269            */
3270           if (connection->n_outgoing > 0)
3271             {
3272               DBusList *link;
3273               
3274               _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3275                              connection->n_outgoing);
3276               
3277               while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3278                 {
3279                   _dbus_connection_message_sent (connection, link->data);
3280                 }
3281             }
3282         }
3283       
3284       if (status != DBUS_DISPATCH_COMPLETE)
3285         return status;
3286       else if (connection->n_incoming > 0)
3287         return DBUS_DISPATCH_DATA_REMAINS;
3288       else
3289         return DBUS_DISPATCH_COMPLETE;
3290     }
3291 }
3292
3293 static void
3294 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection    *connection,
3295                                                     DBusDispatchStatus new_status)
3296 {
3297   dbus_bool_t changed;
3298   DBusDispatchStatusFunction function;
3299   void *data;
3300
3301   HAVE_LOCK_CHECK (connection);
3302
3303   _dbus_connection_ref_unlocked (connection);
3304
3305   changed = new_status != connection->last_dispatch_status;
3306
3307   connection->last_dispatch_status = new_status;
3308
3309   function = connection->dispatch_status_function;
3310   data = connection->dispatch_status_data;
3311
3312   /* We drop the lock */
3313   CONNECTION_UNLOCK (connection);
3314   
3315   if (changed && function)
3316     {
3317       _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
3318                      connection, new_status,
3319                      DISPATCH_STATUS_NAME (new_status));
3320       (* function) (connection, new_status, data);      
3321     }
3322   
3323   dbus_connection_unref (connection);
3324 }
3325
3326 /**
3327  * Gets the current state (what we would currently return
3328  * from dbus_connection_dispatch()) but doesn't actually
3329  * dispatch any messages.
3330  * 
3331  * @param connection the connection.
3332  * @returns current dispatch status
3333  */
3334 DBusDispatchStatus
3335 dbus_connection_get_dispatch_status (DBusConnection *connection)
3336 {
3337   DBusDispatchStatus status;
3338
3339   _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3340
3341   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3342   
3343   CONNECTION_LOCK (connection);
3344
3345   status = _dbus_connection_get_dispatch_status_unlocked (connection);
3346   
3347   CONNECTION_UNLOCK (connection);
3348
3349   return status;
3350 }
3351
3352 /**
3353  * Processes data buffered while handling watches, queueing zero or
3354  * more incoming messages. Then pops the first-received message from
3355  * the current incoming message queue, runs any handlers for it, and
3356  * unrefs the message. Returns a status indicating whether messages/data
3357  * remain, more memory is needed, or all data has been processed.
3358  * 
3359  * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
3360  * does not necessarily dispatch a message, as the data may
3361  * be part of authentication or the like.
3362  *
3363  * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
3364  *
3365  * @todo FIXME what if we call out to application code to handle a
3366  * message, holding the dispatch lock, and the application code runs
3367  * the main loop and dispatches again? Probably deadlocks at the
3368  * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
3369  * and then the GSource etc. could handle the situation? Right now
3370  * our GSource is NO_RECURSE
3371  * 
3372  * @param connection the connection
3373  * @returns dispatch status
3374  */
3375 DBusDispatchStatus
3376 dbus_connection_dispatch (DBusConnection *connection)
3377 {
3378   DBusMessage *message;
3379   DBusList *link, *filter_list_copy, *message_link;
3380   DBusHandlerResult result;
3381   DBusPendingCall *pending;
3382   dbus_int32_t reply_serial;
3383   DBusDispatchStatus status;
3384
3385   _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3386
3387   _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3388   
3389   CONNECTION_LOCK (connection);
3390   status = _dbus_connection_get_dispatch_status_unlocked (connection);
3391   if (status != DBUS_DISPATCH_DATA_REMAINS)
3392     {
3393       /* unlocks and calls out to user code */
3394       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3395       return status;
3396     }
3397   
3398   /* We need to ref the connection since the callback could potentially
3399    * drop the last ref to it
3400    */
3401   _dbus_connection_ref_unlocked (connection);
3402
3403   _dbus_connection_acquire_dispatch (connection);
3404   HAVE_LOCK_CHECK (connection);
3405
3406   message_link = _dbus_connection_pop_message_link_unlocked (connection);
3407   if (message_link == NULL)
3408     {
3409       /* another thread dispatched our stuff */
3410
3411       _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
3412       
3413       _dbus_connection_release_dispatch (connection);
3414
3415       status = _dbus_connection_get_dispatch_status_unlocked (connection);
3416
3417       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3418       
3419       dbus_connection_unref (connection);
3420       
3421       return status;
3422     }
3423
3424   message = message_link->data;
3425
3426   _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
3427                  message,
3428                  dbus_message_get_type (message),
3429                  dbus_message_get_interface (message) ?
3430                  dbus_message_get_interface (message) :
3431                  "no interface",
3432                  dbus_message_get_member (message) ?
3433                  dbus_message_get_member (message) :
3434                  "no member",
3435                  dbus_message_get_signature (message));
3436
3437   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3438   
3439   /* Pending call handling must be first, because if you do
3440    * dbus_connection_send_with_reply_and_block() or
3441    * dbus_pending_call_block() then no handlers/filters will be run on
3442    * the reply. We want consistent semantics in the case where we
3443    * dbus_connection_dispatch() the reply.
3444    */
3445   
3446   reply_serial = dbus_message_get_reply_serial (message);
3447   pending = _dbus_hash_table_lookup_int (connection->pending_replies,
3448                                          reply_serial);
3449   if (pending)
3450     {
3451       _dbus_verbose ("Dispatching a pending reply\n");
3452       _dbus_pending_call_complete_and_unlock (pending, message);
3453       pending = NULL; /* it's probably unref'd */
3454       
3455       CONNECTION_LOCK (connection);
3456       _dbus_verbose ("pending call completed in dispatch\n");
3457       result = DBUS_HANDLER_RESULT_HANDLED;
3458       goto out;
3459     }
3460   
3461   if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
3462     {
3463       _dbus_connection_release_dispatch (connection);
3464       HAVE_LOCK_CHECK (connection);
3465       
3466       _dbus_connection_failed_pop (connection, message_link);
3467
3468       /* unlocks and calls user code */
3469       _dbus_connection_update_dispatch_status_and_unlock (connection,
3470                                                           DBUS_DISPATCH_NEED_MEMORY);
3471
3472       if (pending)
3473         dbus_pending_call_unref (pending);
3474       dbus_connection_unref (connection);
3475       
3476       return DBUS_DISPATCH_NEED_MEMORY;
3477     }
3478   
3479   _dbus_list_foreach (&filter_list_copy,
3480                       (DBusForeachFunction)_dbus_message_filter_ref,
3481                       NULL);
3482
3483   /* We're still protected from dispatch() reentrancy here
3484    * since we acquired the dispatcher
3485    */
3486   CONNECTION_UNLOCK (connection);
3487   
3488   link = _dbus_list_get_first_link (&filter_list_copy);
3489   while (link != NULL)
3490     {
3491       DBusMessageFilter *filter = link->data;
3492       DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
3493
3494       _dbus_verbose ("  running filter on message %p\n", message);
3495       result = (* filter->function) (connection, message, filter->user_data);
3496
3497       if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3498         break;
3499
3500       link = next;
3501     }
3502
3503   _dbus_list_foreach (&filter_list_copy,
3504                       (DBusForeachFunction)_dbus_message_filter_unref,
3505                       NULL);
3506   _dbus_list_clear (&filter_list_copy);
3507   
3508   CONNECTION_LOCK (connection);
3509
3510   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
3511     {
3512       _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
3513       goto out;
3514     }
3515   else if (result == DBUS_HANDLER_RESULT_HANDLED)
3516     {
3517       _dbus_verbose ("filter handled message in dispatch\n");
3518       goto out;
3519     }
3520
3521   /* We're still protected from dispatch() reentrancy here
3522    * since we acquired the dispatcher
3523    */
3524   _dbus_verbose ("  running object path dispatch on message %p (%d %s %s '%s')\n",
3525                  message,
3526                  dbus_message_get_type (message),
3527                  dbus_message_get_interface (message) ?
3528                  dbus_message_get_interface (message) :
3529                  "no interface",
3530                  dbus_message_get_member (message) ?
3531                  dbus_message_get_member (message) :
3532                  "no member",
3533                  dbus_message_get_signature (message));
3534
3535   HAVE_LOCK_CHECK (connection);
3536   result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
3537                                                   message);
3538   
3539   CONNECTION_LOCK (connection);
3540
3541   if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3542     {
3543       _dbus_verbose ("object tree handled message in dispatch\n");
3544       goto out;
3545     }
3546
3547   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
3548     {
3549       DBusMessage *reply;
3550       DBusString str;
3551       DBusPreallocatedSend *preallocated;
3552
3553       _dbus_verbose ("  sending error %s\n",
3554                      DBUS_ERROR_UNKNOWN_METHOD);
3555       
3556       if (!_dbus_string_init (&str))
3557         {
3558           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3559           _dbus_verbose ("no memory for error string in dispatch\n");
3560           goto out;
3561         }
3562               
3563       if (!_dbus_string_append_printf (&str,
3564                                        "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
3565                                        dbus_message_get_member (message),
3566                                        dbus_message_get_signature (message),
3567                                        dbus_message_get_interface (message)))
3568         {
3569           _dbus_string_free (&str);
3570           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3571           _dbus_verbose ("no memory for error string in dispatch\n");
3572           goto out;
3573         }
3574       
3575       reply = dbus_message_new_error (message,
3576                                       DBUS_ERROR_UNKNOWN_METHOD,
3577                                       _dbus_string_get_const_data (&str));
3578       _dbus_string_free (&str);
3579
3580       if (reply == NULL)
3581         {
3582           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3583           _dbus_verbose ("no memory for error reply in dispatch\n");
3584           goto out;
3585         }
3586       
3587       preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3588
3589       if (preallocated == NULL)
3590         {
3591           dbus_message_unref (reply);
3592           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3593           _dbus_verbose ("no memory for error send in dispatch\n");
3594           goto out;
3595         }
3596
3597       _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
3598                                                              reply, NULL);
3599
3600       dbus_message_unref (reply);
3601       
3602       result = DBUS_HANDLER_RESULT_HANDLED;
3603     }
3604   
3605   _dbus_verbose ("  done dispatching %p (%d %s %s '%s') on connection %p\n", message,
3606                  dbus_message_get_type (message),
3607                  dbus_message_get_interface (message) ?
3608                  dbus_message_get_interface (message) :
3609                  "no interface",
3610                  dbus_message_get_member (message) ?
3611                  dbus_message_get_member (message) :
3612                  "no member",
3613                  dbus_message_get_signature (message),
3614                  connection);
3615   
3616  out:
3617   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
3618     {
3619       _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
3620       
3621       /* Put message back, and we'll start over.
3622        * Yes this means handlers must be idempotent if they
3623        * don't return HANDLED; c'est la vie.
3624        */
3625       _dbus_connection_putback_message_link_unlocked (connection,
3626                                                       message_link);
3627     }
3628   else
3629     {
3630       _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
3631       
3632       if (connection->exit_on_disconnect &&
3633           dbus_message_is_signal (message,
3634                                   DBUS_INTERFACE_LOCAL,
3635                                   "Disconnected"))
3636         {
3637           _dbus_verbose ("Exiting on Disconnected signal\n");
3638           CONNECTION_UNLOCK (connection);
3639           _dbus_exit (1);
3640           _dbus_assert_not_reached ("Call to exit() returned");
3641         }
3642       
3643       _dbus_list_free_link (message_link);
3644       dbus_message_unref (message); /* don't want the message to count in max message limits
3645                                      * in computing dispatch status below
3646                                      */
3647     }
3648   
3649   _dbus_connection_release_dispatch (connection);
3650   HAVE_LOCK_CHECK (connection);
3651
3652   _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
3653   status = _dbus_connection_get_dispatch_status_unlocked (connection);
3654
3655   /* unlocks and calls user code */
3656   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3657   
3658   dbus_connection_unref (connection);
3659   
3660   return status;
3661 }
3662
3663 /**
3664  * Sets the watch functions for the connection. These functions are
3665  * responsible for making the application's main loop aware of file
3666  * descriptors that need to be monitored for events, using select() or
3667  * poll(). When using Qt, typically the DBusAddWatchFunction would
3668  * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
3669  * could call g_io_add_watch(), or could be used as part of a more
3670  * elaborate GSource. Note that when a watch is added, it may
3671  * not be enabled.
3672  *
3673  * The DBusWatchToggledFunction notifies the application that the
3674  * watch has been enabled or disabled. Call dbus_watch_get_enabled()
3675  * to check this. A disabled watch should have no effect, and enabled
3676  * watch should be added to the main loop. This feature is used
3677  * instead of simply adding/removing the watch because
3678  * enabling/disabling can be done without memory allocation.  The
3679  * toggled function may be NULL if a main loop re-queries
3680  * dbus_watch_get_enabled() every time anyway.
3681  * 
3682  * The DBusWatch can be queried for the file descriptor to watch using
3683  * dbus_watch_get_fd(), and for the events to watch for using
3684  * dbus_watch_get_flags(). The flags returned by
3685  * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
3686  * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
3687  * all watches implicitly include a watch for hangups, errors, and
3688  * other exceptional conditions.
3689  *
3690  * Once a file descriptor becomes readable or writable, or an exception
3691  * occurs, dbus_watch_handle() should be called to
3692  * notify the connection of the file descriptor's condition.
3693  *
3694  * dbus_watch_handle() cannot be called during the
3695  * DBusAddWatchFunction, as the connection will not be ready to handle
3696  * that watch yet.
3697  * 
3698  * It is not allowed to reference a DBusWatch after it has been passed
3699  * to remove_function.
3700  *
3701  * If #FALSE is returned due to lack of memory, the failure may be due
3702  * to a #FALSE return from the new add_function. If so, the
3703  * add_function may have been called successfully one or more times,
3704  * but the remove_function will also have been called to remove any
3705  * successful adds. i.e. if #FALSE is returned the net result
3706  * should be that dbus_connection_set_watch_functions() has no effect,
3707  * but the add_function and remove_function may have been called.
3708  *
3709  * @todo We need to drop the lock when we call the
3710  * add/remove/toggled functions which can be a side effect
3711  * of setting the watch functions.
3712  * 
3713  * @param connection the connection.
3714  * @param add_function function to begin monitoring a new descriptor.
3715  * @param remove_function function to stop monitoring a descriptor.
3716  * @param toggled_function function to notify of enable/disable
3717  * @param data data to pass to add_function and remove_function.
3718  * @param free_data_function function to be called to free the data.
3719  * @returns #FALSE on failure (no memory)
3720  */
3721 dbus_bool_t
3722 dbus_connection_set_watch_functions (DBusConnection              *connection,
3723                                      DBusAddWatchFunction         add_function,
3724                                      DBusRemoveWatchFunction      remove_function,
3725                                      DBusWatchToggledFunction     toggled_function,
3726                                      void                        *data,
3727                                      DBusFreeFunction             free_data_function)
3728 {
3729   dbus_bool_t retval;
3730   DBusWatchList *watches;
3731
3732   _dbus_return_val_if_fail (connection != NULL, FALSE);
3733   
3734   CONNECTION_LOCK (connection);
3735
3736 #ifndef DBUS_DISABLE_CHECKS
3737   if (connection->watches == NULL)
3738     {
3739       _dbus_warn ("Re-entrant call to %s is not allowed\n",
3740                   _DBUS_FUNCTION_NAME);
3741       return FALSE;
3742     }
3743 #endif
3744   
3745   /* ref connection for slightly better reentrancy */
3746   _dbus_connection_ref_unlocked (connection);
3747
3748   /* This can call back into user code, and we need to drop the
3749    * connection lock when it does. This is kind of a lame
3750    * way to do it.
3751    */
3752   watches = connection->watches;
3753   connection->watches = NULL;
3754   CONNECTION_UNLOCK (connection);
3755
3756   retval = _dbus_watch_list_set_functions (watches,
3757                                            add_function, remove_function,
3758                                            toggled_function,
3759                                            data, free_data_function);
3760   CONNECTION_LOCK (connection);
3761   connection->watches = watches;
3762   
3763   CONNECTION_UNLOCK (connection);
3764   /* drop our paranoid refcount */
3765   dbus_connection_unref (connection);
3766   
3767   return retval;
3768 }
3769
3770 /**
3771  * Sets the timeout functions for the connection. These functions are
3772  * responsible for making the application's main loop aware of timeouts.
3773  * When using Qt, typically the DBusAddTimeoutFunction would create a
3774  * QTimer. When using GLib, the DBusAddTimeoutFunction would call
3775  * g_timeout_add.
3776  * 
3777  * The DBusTimeoutToggledFunction notifies the application that the
3778  * timeout has been enabled or disabled. Call
3779  * dbus_timeout_get_enabled() to check this. A disabled timeout should
3780  * have no effect, and enabled timeout should be added to the main
3781  * loop. This feature is used instead of simply adding/removing the
3782  * timeout because enabling/disabling can be done without memory
3783  * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
3784  * to enable and disable. The toggled function may be NULL if a main
3785  * loop re-queries dbus_timeout_get_enabled() every time anyway.
3786  * Whenever a timeout is toggled, its interval may change.
3787  *
3788  * The DBusTimeout can be queried for the timer interval using
3789  * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
3790  * repeatedly, each time the interval elapses, starting after it has
3791  * elapsed once. The timeout stops firing when it is removed with the
3792  * given remove_function.  The timer interval may change whenever the
3793  * timeout is added, removed, or toggled.
3794  *
3795  * @param connection the connection.
3796  * @param add_function function to add a timeout.
3797  * @param remove_function function to remove a timeout.
3798  * @param toggled_function function to notify of enable/disable
3799  * @param data data to pass to add_function and remove_function.
3800  * @param free_data_function function to be called to free the data.
3801  * @returns #FALSE on failure (no memory)
3802  */
3803 dbus_bool_t
3804 dbus_connection_set_timeout_functions   (DBusConnection            *connection,
3805                                          DBusAddTimeoutFunction     add_function,
3806                                          DBusRemoveTimeoutFunction  remove_function,
3807                                          DBusTimeoutToggledFunction toggled_function,
3808                                          void                      *data,
3809                                          DBusFreeFunction           free_data_function)
3810 {
3811   dbus_bool_t retval;
3812   DBusTimeoutList *timeouts;
3813
3814   _dbus_return_val_if_fail (connection != NULL, FALSE);
3815   
3816   CONNECTION_LOCK (connection);
3817
3818 #ifndef DBUS_DISABLE_CHECKS
3819   if (connection->timeouts == NULL)
3820     {
3821       _dbus_warn ("Re-entrant call to %s is not allowed\n",
3822                   _DBUS_FUNCTION_NAME);
3823       return FALSE;
3824     }
3825 #endif
3826   
3827   /* ref connection for slightly better reentrancy */
3828   _dbus_connection_ref_unlocked (connection);
3829
3830   timeouts = connection->timeouts;
3831   connection->timeouts = NULL;
3832   CONNECTION_UNLOCK (connection);
3833   
3834   retval = _dbus_timeout_list_set_functions (timeouts,
3835                                              add_function, remove_function,
3836                                              toggled_function,
3837                                              data, free_data_function);
3838   CONNECTION_LOCK (connection);
3839   connection->timeouts = timeouts;
3840   
3841   CONNECTION_UNLOCK (connection);
3842   /* drop our paranoid refcount */
3843   dbus_connection_unref (connection);
3844
3845   return retval;
3846 }
3847
3848 /**
3849  * Sets the mainloop wakeup function for the connection. Thi function is
3850  * responsible for waking up the main loop (if its sleeping) when some some
3851  * change has happened to the connection that the mainloop needs to reconsiders
3852  * (e.g. a message has been queued for writing).
3853  * When using Qt, this typically results in a call to QEventLoop::wakeUp().
3854  * When using GLib, it would call g_main_context_wakeup().
3855  *
3856  *
3857  * @param connection the connection.
3858  * @param wakeup_main_function function to wake up the mainloop
3859  * @param data data to pass wakeup_main_function
3860  * @param free_data_function function to be called to free the data.
3861  */
3862 void
3863 dbus_connection_set_wakeup_main_function (DBusConnection            *connection,
3864                                           DBusWakeupMainFunction     wakeup_main_function,
3865                                           void                      *data,
3866                                           DBusFreeFunction           free_data_function)
3867 {
3868   void *old_data;
3869   DBusFreeFunction old_free_data;
3870
3871   _dbus_return_if_fail (connection != NULL);
3872   
3873   CONNECTION_LOCK (connection);
3874   old_data = connection->wakeup_main_data;
3875   old_free_data = connection->free_wakeup_main_data;
3876
3877   connection->wakeup_main_function = wakeup_main_function;
3878   connection->wakeup_main_data = data;
3879   connection->free_wakeup_main_data = free_data_function;
3880   
3881   CONNECTION_UNLOCK (connection);
3882
3883   /* Callback outside the lock */
3884   if (old_free_data)
3885     (*old_free_data) (old_data);
3886 }
3887
3888 /**
3889  * Set a function to be invoked when the dispatch status changes.
3890  * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
3891  * dbus_connection_dispatch() needs to be called to process incoming
3892  * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
3893  * from inside the DBusDispatchStatusFunction. Indeed, almost
3894  * any reentrancy in this function is a bad idea. Instead,
3895  * the DBusDispatchStatusFunction should simply save an indication
3896  * that messages should be dispatched later, when the main loop
3897  * is re-entered.
3898  *
3899  * @param connection the connection
3900  * @param function function to call on dispatch status changes
3901  * @param data data for function
3902  * @param free_data_function free the function data
3903  */
3904 void
3905 dbus_connection_set_dispatch_status_function (DBusConnection             *connection,
3906                                               DBusDispatchStatusFunction  function,
3907                                               void                       *data,
3908                                               DBusFreeFunction            free_data_function)
3909 {
3910   void *old_data;
3911   DBusFreeFunction old_free_data;
3912
3913   _dbus_return_if_fail (connection != NULL);
3914   
3915   CONNECTION_LOCK (connection);
3916   old_data = connection->dispatch_status_data;
3917   old_free_data = connection->free_dispatch_status_data;
3918
3919   connection->dispatch_status_function = function;
3920   connection->dispatch_status_data = data;
3921   connection->free_dispatch_status_data = free_data_function;
3922   
3923   CONNECTION_UNLOCK (connection);
3924
3925   /* Callback outside the lock */
3926   if (old_free_data)
3927     (*old_free_data) (old_data);
3928 }
3929
3930 /**
3931  * Get the UNIX file descriptor of the connection, if any.  This can
3932  * be used for SELinux access control checks with getpeercon() for
3933  * example. DO NOT read or write to the file descriptor, or try to
3934  * select() on it; use DBusWatch for main loop integration. Not all
3935  * connections will have a file descriptor. So for adding descriptors
3936  * to the main loop, use dbus_watch_get_fd() and so forth.
3937  *
3938  * @param connection the connection
3939  * @param fd return location for the file descriptor.
3940  * @returns #TRUE if fd is successfully obtained.
3941  */
3942 dbus_bool_t
3943 dbus_connection_get_unix_fd (DBusConnection *connection,
3944                              int            *fd)
3945 {
3946   dbus_bool_t retval;
3947
3948   _dbus_return_val_if_fail (connection != NULL, FALSE);
3949   _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
3950   
3951   CONNECTION_LOCK (connection);
3952   
3953   retval = _dbus_transport_get_unix_fd (connection->transport,
3954                                         fd);
3955
3956   CONNECTION_UNLOCK (connection);
3957
3958   return retval;
3959 }
3960
3961 /**
3962  * Gets the UNIX user ID of the connection if any.
3963  * Returns #TRUE if the uid is filled in.
3964  * Always returns #FALSE on non-UNIX platforms.
3965  * Always returns #FALSE prior to authenticating the
3966  * connection.
3967  *
3968  * @param connection the connection
3969  * @param uid return location for the user ID
3970  * @returns #TRUE if uid is filled in with a valid user ID
3971  */
3972 dbus_bool_t
3973 dbus_connection_get_unix_user (DBusConnection *connection,
3974                                unsigned long  *uid)
3975 {
3976   dbus_bool_t result;
3977
3978   _dbus_return_val_if_fail (connection != NULL, FALSE);
3979   _dbus_return_val_if_fail (uid != NULL, FALSE);
3980   
3981   CONNECTION_LOCK (connection);
3982
3983   if (!_dbus_transport_get_is_authenticated (connection->transport))
3984     result = FALSE;
3985   else
3986     result = _dbus_transport_get_unix_user (connection->transport,
3987                                             uid);
3988   CONNECTION_UNLOCK (connection);
3989
3990   return result;
3991 }
3992
3993 /**
3994  * Gets the process ID of the connection if any.
3995  * Returns #TRUE if the uid is filled in.
3996  * Always returns #FALSE prior to authenticating the
3997  * connection.
3998  *
3999  * @param connection the connection
4000  * @param pid return location for the process ID
4001  * @returns #TRUE if uid is filled in with a valid process ID
4002  */
4003 dbus_bool_t
4004 dbus_connection_get_unix_process_id (DBusConnection *connection,
4005                                      unsigned long  *pid)
4006 {
4007   dbus_bool_t result;
4008
4009   _dbus_return_val_if_fail (connection != NULL, FALSE);
4010   _dbus_return_val_if_fail (pid != NULL, FALSE);
4011   
4012   CONNECTION_LOCK (connection);
4013
4014   if (!_dbus_transport_get_is_authenticated (connection->transport))
4015     result = FALSE;
4016   else
4017     result = _dbus_transport_get_unix_process_id (connection->transport,
4018                                                   pid);
4019   CONNECTION_UNLOCK (connection);
4020
4021   return result;
4022 }
4023
4024 /**
4025  * Sets a predicate function used to determine whether a given user ID
4026  * is allowed to connect. When an incoming connection has
4027  * authenticated with a particular user ID, this function is called;
4028  * if it returns #TRUE, the connection is allowed to proceed,
4029  * otherwise the connection is disconnected.
4030  *
4031  * If the function is set to #NULL (as it is by default), then
4032  * only the same UID as the server process will be allowed to
4033  * connect.
4034  *
4035  * @param connection the connection
4036  * @param function the predicate
4037  * @param data data to pass to the predicate
4038  * @param free_data_function function to free the data
4039  */
4040 void
4041 dbus_connection_set_unix_user_function (DBusConnection             *connection,
4042                                         DBusAllowUnixUserFunction   function,
4043                                         void                       *data,
4044                                         DBusFreeFunction            free_data_function)
4045 {
4046   void *old_data = NULL;
4047   DBusFreeFunction old_free_function = NULL;
4048
4049   _dbus_return_if_fail (connection != NULL);
4050   
4051   CONNECTION_LOCK (connection);
4052   _dbus_transport_set_unix_user_function (connection->transport,
4053                                           function, data, free_data_function,
4054                                           &old_data, &old_free_function);
4055   CONNECTION_UNLOCK (connection);
4056
4057   if (old_free_function != NULL)
4058     (* old_free_function) (old_data);    
4059 }
4060
4061 /**
4062  * Adds a message filter. Filters are handlers that are run on all
4063  * incoming messages, prior to the objects registered with
4064  * dbus_connection_register_object_path().  Filters are run in the
4065  * order that they were added.  The same handler can be added as a
4066  * filter more than once, in which case it will be run more than once.
4067  * Filters added during a filter callback won't be run on the message
4068  * being processed.
4069  *
4070  * @todo we don't run filters on messages while blocking without
4071  * entering the main loop, since filters are run as part of
4072  * dbus_connection_dispatch(). This is probably a feature, as filters
4073  * could create arbitrary reentrancy. But kind of sucks if you're
4074  * trying to filter METHOD_RETURN for some reason.
4075  *
4076  * @param connection the connection
4077  * @param function function to handle messages
4078  * @param user_data user data to pass to the function
4079  * @param free_data_function function to use for freeing user data
4080  * @returns #TRUE on success, #FALSE if not enough memory.
4081  */
4082 dbus_bool_t
4083 dbus_connection_add_filter (DBusConnection            *connection,
4084                             DBusHandleMessageFunction  function,
4085                             void                      *user_data,
4086                             DBusFreeFunction           free_data_function)
4087 {
4088   DBusMessageFilter *filter;
4089   
4090   _dbus_return_val_if_fail (connection != NULL, FALSE);
4091   _dbus_return_val_if_fail (function != NULL, FALSE);
4092
4093   filter = dbus_new0 (DBusMessageFilter, 1);
4094   if (filter == NULL)
4095     return FALSE;
4096
4097   filter->refcount.value = 1;
4098   
4099   CONNECTION_LOCK (connection);
4100
4101   if (!_dbus_list_append (&connection->filter_list,
4102                           filter))
4103     {
4104       _dbus_message_filter_unref (filter);
4105       CONNECTION_UNLOCK (connection);
4106       return FALSE;
4107     }
4108
4109   /* Fill in filter after all memory allocated,
4110    * so we don't run the free_user_data_function
4111    * if the add_filter() fails
4112    */
4113   
4114   filter->function = function;
4115   filter->user_data = user_data;
4116   filter->free_user_data_function = free_data_function;
4117         
4118   CONNECTION_UNLOCK (connection);
4119   return TRUE;
4120 }
4121
4122 /**
4123  * Removes a previously-added message filter. It is a programming
4124  * error to call this function for a handler that has not been added
4125  * as a filter. If the given handler was added more than once, only
4126  * one instance of it will be removed (the most recently-added
4127  * instance).
4128  *
4129  * @param connection the connection
4130  * @param function the handler to remove
4131  * @param user_data user data for the handler to remove
4132  *
4133  */
4134 void
4135 dbus_connection_remove_filter (DBusConnection            *connection,
4136                                DBusHandleMessageFunction  function,
4137                                void                      *user_data)
4138 {
4139   DBusList *link;
4140   DBusMessageFilter *filter;
4141   
4142   _dbus_return_if_fail (connection != NULL);
4143   _dbus_return_if_fail (function != NULL);
4144   
4145   CONNECTION_LOCK (connection);
4146
4147   filter = NULL;
4148   
4149   link = _dbus_list_get_last_link (&connection->filter_list);
4150   while (link != NULL)
4151     {
4152       filter = link->data;
4153
4154       if (filter->function == function &&
4155           filter->user_data == user_data)
4156         {
4157           _dbus_list_remove_link (&connection->filter_list, link);
4158           filter->function = NULL;
4159           
4160           break;
4161         }
4162         
4163       link = _dbus_list_get_prev_link (&connection->filter_list, link);
4164     }
4165   
4166   CONNECTION_UNLOCK (connection);
4167
4168 #ifndef DBUS_DISABLE_CHECKS
4169   if (filter == NULL)
4170     {
4171       _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
4172                   function, user_data);
4173       return;
4174     }
4175 #endif
4176   
4177   /* Call application code */
4178   if (filter->free_user_data_function)
4179     (* filter->free_user_data_function) (filter->user_data);
4180
4181   filter->free_user_data_function = NULL;
4182   filter->user_data = NULL;
4183   
4184   _dbus_message_filter_unref (filter);
4185 }
4186
4187 /**
4188  * Registers a handler for a given path in the object hierarchy.
4189  * The given vtable handles messages sent to exactly the given path.
4190  *
4191  *
4192  * @param connection the connection
4193  * @param path a '/' delimited string of path elements
4194  * @param vtable the virtual table
4195  * @param user_data data to pass to functions in the vtable
4196  * @returns #FALSE if not enough memory
4197  */
4198 dbus_bool_t
4199 dbus_connection_register_object_path (DBusConnection              *connection,
4200                                       const char                  *path,
4201                                       const DBusObjectPathVTable  *vtable,
4202                                       void                        *user_data)
4203 {
4204   char **decomposed_path;
4205   dbus_bool_t retval;
4206   
4207   _dbus_return_val_if_fail (connection != NULL, FALSE);
4208   _dbus_return_val_if_fail (path != NULL, FALSE);
4209   _dbus_return_val_if_fail (path[0] == '/', FALSE);
4210   _dbus_return_val_if_fail (vtable != NULL, FALSE);
4211
4212   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4213     return FALSE;
4214
4215   CONNECTION_LOCK (connection);
4216
4217   retval = _dbus_object_tree_register (connection->objects,
4218                                        FALSE,
4219                                        (const char **) decomposed_path, vtable,
4220                                        user_data);
4221
4222   CONNECTION_UNLOCK (connection);
4223
4224   dbus_free_string_array (decomposed_path);
4225
4226   return retval;
4227 }
4228
4229 /**
4230  * Registers a fallback handler for a given subsection of the object
4231  * hierarchy.  The given vtable handles messages at or below the given
4232  * path. You can use this to establish a default message handling
4233  * policy for a whole "subdirectory."
4234  *
4235  * @param connection the connection
4236  * @param path a '/' delimited string of path elements
4237  * @param vtable the virtual table
4238  * @param user_data data to pass to functions in the vtable
4239  * @returns #FALSE if not enough memory
4240  */
4241 dbus_bool_t
4242 dbus_connection_register_fallback (DBusConnection              *connection,
4243                                    const char                  *path,
4244                                    const DBusObjectPathVTable  *vtable,
4245                                    void                        *user_data)
4246 {
4247   char **decomposed_path;
4248   dbus_bool_t retval;
4249   
4250   _dbus_return_val_if_fail (connection != NULL, FALSE);
4251   _dbus_return_val_if_fail (path != NULL, FALSE);
4252   _dbus_return_val_if_fail (path[0] == '/', FALSE);
4253   _dbus_return_val_if_fail (vtable != NULL, FALSE);
4254
4255   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4256     return FALSE;
4257
4258   CONNECTION_LOCK (connection);
4259
4260   retval = _dbus_object_tree_register (connection->objects,
4261                                        TRUE,
4262                                        (const char **) decomposed_path, vtable,
4263                                        user_data);
4264
4265   CONNECTION_UNLOCK (connection);
4266
4267   dbus_free_string_array (decomposed_path);
4268
4269   return retval;
4270 }
4271
4272 /**
4273  * Unregisters the handler registered with exactly the given path.
4274  * It's a bug to call this function for a path that isn't registered.
4275  * Can unregister both fallback paths and object paths.
4276  *
4277  * @param connection the connection
4278  * @param path a '/' delimited string of path elements
4279  * @returns #FALSE if not enough memory
4280  */
4281 dbus_bool_t
4282 dbus_connection_unregister_object_path (DBusConnection              *connection,
4283                                         const char                  *path)
4284 {
4285   char **decomposed_path;
4286
4287   _dbus_return_val_if_fail (connection != NULL, FALSE);
4288   _dbus_return_val_if_fail (path != NULL, FALSE);
4289   _dbus_return_val_if_fail (path[0] == '/', FALSE);
4290
4291   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4292       return FALSE;
4293
4294   CONNECTION_LOCK (connection);
4295
4296   _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
4297
4298   dbus_free_string_array (decomposed_path);
4299
4300   return TRUE;
4301 }
4302
4303 /**
4304  * Lists the registered fallback handlers and object path handlers at
4305  * the given parent_path. The returned array should be freed with
4306  * dbus_free_string_array().
4307  *
4308  * @param connection the connection
4309  * @param parent_path the path to list the child handlers of
4310  * @param child_entries returns #NULL-terminated array of children
4311  * @returns #FALSE if no memory to allocate the child entries
4312  */
4313 dbus_bool_t
4314 dbus_connection_list_registered (DBusConnection              *connection,
4315                                  const char                  *parent_path,
4316                                  char                      ***child_entries)
4317 {
4318   char **decomposed_path;
4319   dbus_bool_t retval;
4320   _dbus_return_val_if_fail (connection != NULL, FALSE);
4321   _dbus_return_val_if_fail (parent_path != NULL, FALSE);
4322   _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
4323   _dbus_return_val_if_fail (child_entries != NULL, FALSE);
4324
4325   if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
4326     return FALSE;
4327
4328   CONNECTION_LOCK (connection);
4329
4330   retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
4331                                                          (const char **) decomposed_path,
4332                                                          child_entries);
4333   dbus_free_string_array (decomposed_path);
4334
4335   return retval;
4336 }
4337
4338 static DBusDataSlotAllocator slot_allocator;
4339 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
4340
4341 /**
4342  * Allocates an integer ID to be used for storing application-specific
4343  * data on any DBusConnection. The allocated ID may then be used
4344  * with dbus_connection_set_data() and dbus_connection_get_data().
4345  * The passed-in slot must be initialized to -1, and is filled in
4346  * with the slot ID. If the passed-in slot is not -1, it's assumed
4347  * to be already allocated, and its refcount is incremented.
4348  * 
4349  * The allocated slot is global, i.e. all DBusConnection objects will
4350  * have a slot with the given integer ID reserved.
4351  *
4352  * @param slot_p address of a global variable storing the slot
4353  * @returns #FALSE on failure (no memory)
4354  */
4355 dbus_bool_t
4356 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
4357 {
4358   return _dbus_data_slot_allocator_alloc (&slot_allocator,
4359                                           _DBUS_LOCK_NAME (connection_slots),
4360                                           slot_p);
4361 }
4362
4363 /**
4364  * Deallocates a global ID for connection data slots.
4365  * dbus_connection_get_data() and dbus_connection_set_data() may no
4366  * longer be used with this slot.  Existing data stored on existing
4367  * DBusConnection objects will be freed when the connection is
4368  * finalized, but may not be retrieved (and may only be replaced if
4369  * someone else reallocates the slot).  When the refcount on the
4370  * passed-in slot reaches 0, it is set to -1.
4371  *
4372  * @param slot_p address storing the slot to deallocate
4373  */
4374 void
4375 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
4376 {
4377   _dbus_return_if_fail (*slot_p >= 0);
4378   
4379   _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
4380 }
4381
4382 /**
4383  * Stores a pointer on a DBusConnection, along
4384  * with an optional function to be used for freeing
4385  * the data when the data is set again, or when
4386  * the connection is finalized. The slot number
4387  * must have been allocated with dbus_connection_allocate_data_slot().
4388  *
4389  * @param connection the connection
4390  * @param slot the slot number
4391  * @param data the data to store
4392  * @param free_data_func finalizer function for the data
4393  * @returns #TRUE if there was enough memory to store the data
4394  */
4395 dbus_bool_t
4396 dbus_connection_set_data (DBusConnection   *connection,
4397                           dbus_int32_t      slot,
4398                           void             *data,
4399                           DBusFreeFunction  free_data_func)
4400 {
4401   DBusFreeFunction old_free_func;
4402   void *old_data;
4403   dbus_bool_t retval;
4404
4405   _dbus_return_val_if_fail (connection != NULL, FALSE);
4406   _dbus_return_val_if_fail (slot >= 0, FALSE);
4407   
4408   CONNECTION_LOCK (connection);
4409
4410   retval = _dbus_data_slot_list_set (&slot_allocator,
4411                                      &connection->slot_list,
4412                                      slot, data, free_data_func,
4413                                      &old_free_func, &old_data);
4414   
4415   CONNECTION_UNLOCK (connection);
4416
4417   if (retval)
4418     {
4419       /* Do the actual free outside the connection lock */
4420       if (old_free_func)
4421         (* old_free_func) (old_data);
4422     }
4423
4424   return retval;
4425 }
4426
4427 /**
4428  * Retrieves data previously set with dbus_connection_set_data().
4429  * The slot must still be allocated (must not have been freed).
4430  *
4431  * @param connection the connection
4432  * @param slot the slot to get data from
4433  * @returns the data, or #NULL if not found
4434  */
4435 void*
4436 dbus_connection_get_data (DBusConnection   *connection,
4437                           dbus_int32_t      slot)
4438 {
4439   void *res;
4440
4441   _dbus_return_val_if_fail (connection != NULL, NULL);
4442   
4443   CONNECTION_LOCK (connection);
4444
4445   res = _dbus_data_slot_list_get (&slot_allocator,
4446                                   &connection->slot_list,
4447                                   slot);
4448   
4449   CONNECTION_UNLOCK (connection);
4450
4451   return res;
4452 }
4453
4454 /**
4455  * This function sets a global flag for whether dbus_connection_new()
4456  * will set SIGPIPE behavior to SIG_IGN.
4457  *
4458  * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
4459  */
4460 void
4461 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
4462 {  
4463   _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
4464 }
4465
4466 /**
4467  * Specifies the maximum size message this connection is allowed to
4468  * receive. Larger messages will result in disconnecting the
4469  * connection.
4470  * 
4471  * @param connection a #DBusConnection
4472  * @param size maximum message size the connection can receive, in bytes
4473  */
4474 void
4475 dbus_connection_set_max_message_size (DBusConnection *connection,
4476                                       long            size)
4477 {
4478   _dbus_return_if_fail (connection != NULL);
4479   
4480   CONNECTION_LOCK (connection);
4481   _dbus_transport_set_max_message_size (connection->transport,
4482                                         size);
4483   CONNECTION_UNLOCK (connection);
4484 }
4485
4486 /**
4487  * Gets the value set by dbus_connection_set_max_message_size().
4488  *
4489  * @param connection the connection
4490  * @returns the max size of a single message
4491  */
4492 long
4493 dbus_connection_get_max_message_size (DBusConnection *connection)
4494 {
4495   long res;
4496
4497   _dbus_return_val_if_fail (connection != NULL, 0);
4498   
4499   CONNECTION_LOCK (connection);
4500   res = _dbus_transport_get_max_message_size (connection->transport);
4501   CONNECTION_UNLOCK (connection);
4502   return res;
4503 }
4504
4505 /**
4506  * Sets the maximum total number of bytes that can be used for all messages
4507  * received on this connection. Messages count toward the maximum until
4508  * they are finalized. When the maximum is reached, the connection will
4509  * not read more data until some messages are finalized.
4510  *
4511  * The semantics of the maximum are: if outstanding messages are
4512  * already above the maximum, additional messages will not be read.
4513  * The semantics are not: if the next message would cause us to exceed
4514  * the maximum, we don't read it. The reason is that we don't know the
4515  * size of a message until after we read it.
4516  *
4517  * Thus, the max live messages size can actually be exceeded
4518  * by up to the maximum size of a single message.
4519  * 
4520  * Also, if we read say 1024 bytes off the wire in a single read(),
4521  * and that contains a half-dozen small messages, we may exceed the
4522  * size max by that amount. But this should be inconsequential.
4523  *
4524  * This does imply that we can't call read() with a buffer larger
4525  * than we're willing to exceed this limit by.
4526  *
4527  * @param connection the connection
4528  * @param size the maximum size in bytes of all outstanding messages
4529  */
4530 void
4531 dbus_connection_set_max_received_size (DBusConnection *connection,
4532                                        long            size)
4533 {
4534   _dbus_return_if_fail (connection != NULL);
4535   
4536   CONNECTION_LOCK (connection);
4537   _dbus_transport_set_max_received_size (connection->transport,
4538                                          size);
4539   CONNECTION_UNLOCK (connection);
4540 }
4541
4542 /**
4543  * Gets the value set by dbus_connection_set_max_received_size().
4544  *
4545  * @param connection the connection
4546  * @returns the max size of all live messages
4547  */
4548 long
4549 dbus_connection_get_max_received_size (DBusConnection *connection)
4550 {
4551   long res;
4552
4553   _dbus_return_val_if_fail (connection != NULL, 0);
4554   
4555   CONNECTION_LOCK (connection);
4556   res = _dbus_transport_get_max_received_size (connection->transport);
4557   CONNECTION_UNLOCK (connection);
4558   return res;
4559 }
4560
4561 /**
4562  * Gets the approximate size in bytes of all messages in the outgoing
4563  * message queue. The size is approximate in that you shouldn't use
4564  * it to decide how many bytes to read off the network or anything
4565  * of that nature, as optimizations may choose to tell small white lies
4566  * to avoid performance overhead.
4567  *
4568  * @param connection the connection
4569  * @returns the number of bytes that have been queued up but not sent
4570  */
4571 long
4572 dbus_connection_get_outgoing_size (DBusConnection *connection)
4573 {
4574   long res;
4575
4576   _dbus_return_val_if_fail (connection != NULL, 0);
4577   
4578   CONNECTION_LOCK (connection);
4579   res = _dbus_counter_get_value (connection->outgoing_counter);
4580   CONNECTION_UNLOCK (connection);
4581   return res;
4582 }
4583
4584 /** @} */