1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002-2006 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
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-pending-call-internal.h"
33 #include "dbus-list.h"
34 #include "dbus-hash.h"
35 #include "dbus-message-internal.h"
36 #include "dbus-threads.h"
37 #include "dbus-protocol.h"
38 #include "dbus-dataslot.h"
39 #include "dbus-string.h"
40 #include "dbus-pending-call.h"
41 #include "dbus-object-tree.h"
42 #include "dbus-threads-internal.h"
45 #ifdef DBUS_DISABLE_CHECKS
46 #define TOOK_LOCK_CHECK(connection)
47 #define RELEASING_LOCK_CHECK(connection)
48 #define HAVE_LOCK_CHECK(connection)
50 #define TOOK_LOCK_CHECK(connection) do { \
51 _dbus_assert (!(connection)->have_connection_lock); \
52 (connection)->have_connection_lock = TRUE; \
54 #define RELEASING_LOCK_CHECK(connection) do { \
55 _dbus_assert ((connection)->have_connection_lock); \
56 (connection)->have_connection_lock = FALSE; \
58 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
59 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
64 #define CONNECTION_LOCK(connection) do { \
65 if (TRACE_LOCKS) { _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); } \
66 _dbus_mutex_lock ((connection)->mutex); \
67 TOOK_LOCK_CHECK (connection); \
70 #define CONNECTION_UNLOCK(connection) do { \
71 if (TRACE_LOCKS) { _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); } \
72 RELEASING_LOCK_CHECK (connection); \
73 _dbus_mutex_unlock ((connection)->mutex); \
76 #define DISPATCH_STATUS_NAME(s) \
77 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
78 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
79 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
83 * @defgroup DBusConnection DBusConnection
85 * @brief Connection to another application
87 * A DBusConnection represents a connection to another
88 * application. Messages can be sent and received via this connection.
89 * The other application may be a message bus; for convenience, the
90 * function dbus_bus_get() is provided to automatically open a
91 * connection to the well-known message buses.
93 * In brief a DBusConnection is a message queue associated with some
94 * message transport mechanism such as a socket. The connection
95 * maintains a queue of incoming messages and a queue of outgoing
98 * Several functions use the following terms:
100 * <li><b>read</b> means to fill the incoming message queue by reading from the socket</li>
101 * <li><b>write</b> means to drain the outgoing queue by writing to the socket</li>
102 * <li><b>dispatch</b> means to drain the incoming queue by invoking application-provided message handlers</li>
105 * The function dbus_connection_read_write_dispatch() for example does all
106 * three of these things, offering a simple alternative to a main loop.
108 * In an application with a main loop, the read/write/dispatch
109 * operations are usually separate.
111 * The connection provides #DBusWatch and #DBusTimeout objects to
112 * the main loop. These are used to know when reading, writing, or
113 * dispatching should be performed.
115 * Incoming messages are processed
116 * by calling dbus_connection_dispatch(). dbus_connection_dispatch()
117 * runs any handlers registered for the topmost message in the message
118 * queue, then discards the message, then returns.
120 * dbus_connection_get_dispatch_status() indicates whether
121 * messages are currently in the queue that need dispatching.
122 * dbus_connection_set_dispatch_status_function() allows
123 * you to set a function to be used to monitor the dispatch status.
125 * If you're using GLib or Qt add-on libraries for D-Bus, there are
126 * special convenience APIs in those libraries that hide
127 * all the details of dispatch and watch/timeout monitoring.
128 * For example, dbus_connection_setup_with_g_main().
130 * If you aren't using these add-on libraries, but want to process
131 * messages asynchronously, you must manually call
132 * dbus_connection_set_dispatch_status_function(),
133 * dbus_connection_set_watch_functions(),
134 * dbus_connection_set_timeout_functions() providing appropriate
135 * functions to integrate the connection with your application's main
136 * loop. This can be tricky to get right; main loops are not simple.
138 * If you don't need to be asynchronous, you can ignore #DBusWatch,
139 * #DBusTimeout, and dbus_connection_dispatch(). Instead,
140 * dbus_connection_read_write_dispatch() can be used.
142 * Or, in <em>very</em> simple applications,
143 * dbus_connection_pop_message() may be all you need, allowing you to
144 * avoid setting up any handler functions (see
145 * dbus_connection_add_filter(),
146 * dbus_connection_register_object_path() for more on handlers).
148 * When you use dbus_connection_send() or one of its variants to send
149 * a message, the message is added to the outgoing queue. It's
150 * actually written to the network later; either in
151 * dbus_watch_handle() invoked by your main loop, or in
152 * dbus_connection_flush() which blocks until it can write out the
153 * entire outgoing queue. The GLib/Qt add-on libraries again
154 * handle the details here for you by setting up watch functions.
156 * When a connection is disconnected, you are guaranteed to get a
157 * signal "Disconnected" from the interface
158 * #DBUS_INTERFACE_LOCAL, path
161 * You may not drop the last reference to a #DBusConnection
162 * until that connection has been disconnected.
164 * You may dispatch the unprocessed incoming message queue even if the
165 * connection is disconnected. However, "Disconnected" will always be
166 * the last message in the queue (obviously no messages are received
167 * after disconnection).
169 * After calling dbus_threads_init(), #DBusConnection has thread
170 * locks and drops them when invoking user callbacks, so in general is
171 * transparently threadsafe. However, #DBusMessage does NOT have
172 * thread locks; you must not send the same message to multiple
173 * #DBusConnection if those connections will be used from different threads,
176 * Also, if you dispatch or pop messages from multiple threads, it
177 * may work in the sense that it won't crash, but it's tough to imagine
178 * sane results; it will be completely unpredictable which messages
179 * go to which threads.
181 * It's recommended to dispatch from a single thread.
183 * The most useful function to call from multiple threads at once
184 * is dbus_connection_send_with_reply_and_block(). That is,
185 * multiple threads can make method calls at the same time.
187 * If you aren't using threads, you can use a main loop and
188 * dbus_pending_call_set_notify() to achieve a similar result.
192 * @defgroup DBusConnectionInternals DBusConnection implementation details
193 * @ingroup DBusInternals
194 * @brief Implementation details of DBusConnection
200 * Internal struct representing a message filter function
202 typedef struct DBusMessageFilter DBusMessageFilter;
205 * Internal struct representing a message filter function
207 struct DBusMessageFilter
209 DBusAtomic refcount; /**< Reference count */
210 DBusHandleMessageFunction function; /**< Function to call to filter */
211 void *user_data; /**< User data for the function */
212 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
217 * Internals of DBusPreallocatedSend
219 struct DBusPreallocatedSend
221 DBusConnection *connection; /**< Connection we'd send the message to */
222 DBusList *queue_link; /**< Preallocated link in the queue */
223 DBusList *counter_link; /**< Preallocated link in the resource counter */
226 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
229 * Implementation details of DBusConnection. All fields are private.
231 struct DBusConnection
233 DBusAtomic refcount; /**< Reference count. */
235 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
237 DBusMutex *dispatch_mutex; /**< Protects dispatch_acquired */
238 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */
239 DBusMutex *io_path_mutex; /**< Protects io_path_acquired */
240 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */
242 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
243 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
245 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
246 * dispatch_acquired will be set by the borrower
249 int n_outgoing; /**< Length of outgoing queue. */
250 int n_incoming; /**< Length of incoming queue. */
252 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
254 DBusTransport *transport; /**< Object that sends/receives messages over network. */
255 DBusWatchList *watches; /**< Stores active watches. */
256 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
258 DBusList *filter_list; /**< List of filters. */
260 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
262 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
264 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
265 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
267 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
268 void *wakeup_main_data; /**< Application data for wakeup_main_function */
269 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
271 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
272 void *dispatch_status_data; /**< Application data for dispatch_status_function */
273 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
275 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
277 DBusList *link_cache; /**< A cache of linked list links to prevent contention
278 * for the global linked list mempool lock
280 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
282 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
284 unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */
286 unsigned int dispatch_acquired : 1; /**< Someone has dispatch path (can drain incoming queue) */
287 unsigned int io_path_acquired : 1; /**< Someone has transport io path (can use the transport to read/write messages) */
289 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
291 unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */
293 unsigned int disconnected_message_arrived : 1; /**< We popped or are dispatching the disconnected message.
294 * if the disconnect_message_link is NULL then we queued it, but
295 * this flag is whether it got to the head of the queue.
297 unsigned int disconnected_message_processed : 1; /**< We did our default handling of the disconnected message,
298 * such as closing the connection.
301 #ifndef DBUS_DISABLE_CHECKS
302 unsigned int have_connection_lock : 1; /**< Used to check locking */
305 #ifndef DBUS_DISABLE_CHECKS
306 int generation; /**< _dbus_current_generation that should correspond to this connection */
310 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
311 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
312 DBusDispatchStatus new_status);
313 static void _dbus_connection_last_unref (DBusConnection *connection);
314 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
315 static void _dbus_connection_release_dispatch (DBusConnection *connection);
316 static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
317 static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection);
318 static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection);
320 static DBusMessageFilter *
321 _dbus_message_filter_ref (DBusMessageFilter *filter)
323 _dbus_assert (filter->refcount.value > 0);
324 _dbus_atomic_inc (&filter->refcount);
330 _dbus_message_filter_unref (DBusMessageFilter *filter)
332 _dbus_assert (filter->refcount.value > 0);
334 if (_dbus_atomic_dec (&filter->refcount) == 1)
336 if (filter->free_user_data_function)
337 (* filter->free_user_data_function) (filter->user_data);
344 * Acquires the connection lock.
346 * @param connection the connection.
349 _dbus_connection_lock (DBusConnection *connection)
351 CONNECTION_LOCK (connection);
355 * Releases the connection lock.
357 * @param connection the connection.
360 _dbus_connection_unlock (DBusConnection *connection)
362 CONNECTION_UNLOCK (connection);
366 * Wakes up the main loop if it is sleeping
367 * Needed if we're e.g. queueing outgoing messages
368 * on a thread while the mainloop sleeps.
370 * @param connection the connection.
373 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
375 if (connection->wakeup_main_function)
376 (*connection->wakeup_main_function) (connection->wakeup_main_data);
379 #ifdef DBUS_BUILD_TESTS
380 /* For now this function isn't used */
382 * Adds a message to the incoming message queue, returning #FALSE
383 * if there's insufficient memory to queue the message.
384 * Does not take over refcount of the message.
386 * @param connection the connection.
387 * @param message the message to queue.
388 * @returns #TRUE on success.
391 _dbus_connection_queue_received_message (DBusConnection *connection,
392 DBusMessage *message)
396 link = _dbus_list_alloc_link (message);
400 dbus_message_ref (message);
401 _dbus_connection_queue_received_message_link (connection, link);
407 * Gets the locks so we can examine them
409 * @param connection the connection.
410 * @param mutex_loc return for the location of the main mutex pointer
411 * @param dispatch_mutex_loc return location of the dispatch mutex pointer
412 * @param io_path_mutex_loc return location of the io_path mutex pointer
413 * @param dispatch_cond_loc return location of the dispatch conditional
415 * @param io_path_cond_loc return location of the io_path conditional
419 _dbus_connection_test_get_locks (DBusConnection *connection,
420 DBusMutex **mutex_loc,
421 DBusMutex **dispatch_mutex_loc,
422 DBusMutex **io_path_mutex_loc,
423 DBusCondVar **dispatch_cond_loc,
424 DBusCondVar **io_path_cond_loc)
426 *mutex_loc = connection->mutex;
427 *dispatch_mutex_loc = connection->dispatch_mutex;
428 *io_path_mutex_loc = connection->io_path_mutex;
429 *dispatch_cond_loc = connection->dispatch_cond;
430 *io_path_cond_loc = connection->io_path_cond;
435 * Adds a message-containing list link to the incoming message queue,
436 * taking ownership of the link and the message's current refcount.
437 * Cannot fail due to lack of memory.
439 * @param connection the connection.
440 * @param link the message link to queue.
443 _dbus_connection_queue_received_message_link (DBusConnection *connection,
446 DBusPendingCall *pending;
447 dbus_int32_t reply_serial;
448 DBusMessage *message;
450 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
452 _dbus_list_append_link (&connection->incoming_messages,
454 message = link->data;
456 /* If this is a reply we're waiting on, remove timeout for it */
457 reply_serial = dbus_message_get_reply_serial (message);
458 if (reply_serial != -1)
460 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
464 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
465 _dbus_connection_remove_timeout_unlocked (connection,
466 _dbus_pending_call_get_timeout_unlocked (pending));
468 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
474 connection->n_incoming += 1;
476 _dbus_connection_wakeup_mainloop (connection);
478 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
480 dbus_message_get_type (message),
481 dbus_message_get_path (message) ?
482 dbus_message_get_path (message) :
484 dbus_message_get_interface (message) ?
485 dbus_message_get_interface (message) :
487 dbus_message_get_member (message) ?
488 dbus_message_get_member (message) :
490 dbus_message_get_signature (message),
491 dbus_message_get_reply_serial (message),
493 connection->n_incoming);}
496 * Adds a link + message to the incoming message queue.
497 * Can't fail. Takes ownership of both link and message.
499 * @param connection the connection.
500 * @param link the list node and message to queue.
504 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
507 HAVE_LOCK_CHECK (connection);
509 _dbus_list_append_link (&connection->incoming_messages, link);
511 connection->n_incoming += 1;
513 _dbus_connection_wakeup_mainloop (connection);
515 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
516 link->data, connection, connection->n_incoming);
521 * Checks whether there are messages in the outgoing message queue.
522 * Called with connection lock held.
524 * @param connection the connection.
525 * @returns #TRUE if the outgoing queue is non-empty.
528 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
530 HAVE_LOCK_CHECK (connection);
531 return connection->outgoing_messages != NULL;
535 * Checks whether there are messages in the outgoing message queue.
536 * Use dbus_connection_flush() to block until all outgoing
537 * messages have been written to the underlying transport
538 * (such as a socket).
540 * @param connection the connection.
541 * @returns #TRUE if the outgoing queue is non-empty.
544 dbus_connection_has_messages_to_send (DBusConnection *connection)
548 _dbus_return_val_if_fail (connection != NULL, FALSE);
550 CONNECTION_LOCK (connection);
551 v = _dbus_connection_has_messages_to_send_unlocked (connection);
552 CONNECTION_UNLOCK (connection);
558 * Gets the next outgoing message. The message remains in the
559 * queue, and the caller does not own a reference to it.
561 * @param connection the connection.
562 * @returns the message to be sent.
565 _dbus_connection_get_message_to_send (DBusConnection *connection)
567 HAVE_LOCK_CHECK (connection);
569 return _dbus_list_get_last (&connection->outgoing_messages);
573 * Notifies the connection that a message has been sent, so the
574 * message can be removed from the outgoing queue.
575 * Called with the connection lock held.
577 * @param connection the connection.
578 * @param message the message that was sent.
581 _dbus_connection_message_sent (DBusConnection *connection,
582 DBusMessage *message)
586 HAVE_LOCK_CHECK (connection);
588 /* This can be called before we even complete authentication, since
589 * it's called on disconnect to clean up the outgoing queue.
590 * It's also called as we successfully send each message.
593 link = _dbus_list_get_last_link (&connection->outgoing_messages);
594 _dbus_assert (link != NULL);
595 _dbus_assert (link->data == message);
597 /* Save this link in the link cache */
598 _dbus_list_unlink (&connection->outgoing_messages,
600 _dbus_list_prepend_link (&connection->link_cache, link);
602 connection->n_outgoing -= 1;
604 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
606 dbus_message_get_type (message),
607 dbus_message_get_path (message) ?
608 dbus_message_get_path (message) :
610 dbus_message_get_interface (message) ?
611 dbus_message_get_interface (message) :
613 dbus_message_get_member (message) ?
614 dbus_message_get_member (message) :
616 dbus_message_get_signature (message),
617 connection, connection->n_outgoing);
619 /* Save this link in the link cache also */
620 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
622 _dbus_list_prepend_link (&connection->link_cache, link);
624 dbus_message_unref (message);
627 /** Function to be called in protected_change_watch() with refcount held */
628 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
630 /** Function to be called in protected_change_watch() with refcount held */
631 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
633 /** Function to be called in protected_change_watch() with refcount held */
634 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
636 dbus_bool_t enabled);
639 protected_change_watch (DBusConnection *connection,
641 DBusWatchAddFunction add_function,
642 DBusWatchRemoveFunction remove_function,
643 DBusWatchToggleFunction toggle_function,
646 DBusWatchList *watches;
649 HAVE_LOCK_CHECK (connection);
651 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
652 * drop lock and call out" one; but it has to be propagated up through all callers
655 watches = connection->watches;
658 connection->watches = NULL;
659 _dbus_connection_ref_unlocked (connection);
660 CONNECTION_UNLOCK (connection);
663 retval = (* add_function) (watches, watch);
664 else if (remove_function)
667 (* remove_function) (watches, watch);
672 (* toggle_function) (watches, watch, enabled);
675 CONNECTION_LOCK (connection);
676 connection->watches = watches;
677 _dbus_connection_unref_unlocked (connection);
687 * Adds a watch using the connection's DBusAddWatchFunction if
688 * available. Otherwise records the watch to be added when said
689 * function is available. Also re-adds the watch if the
690 * DBusAddWatchFunction changes. May fail due to lack of memory.
691 * Connection lock should be held when calling this.
693 * @param connection the connection.
694 * @param watch the watch to add.
695 * @returns #TRUE on success.
698 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
701 return protected_change_watch (connection, watch,
702 _dbus_watch_list_add_watch,
707 * Removes a watch using the connection's DBusRemoveWatchFunction
708 * if available. It's an error to call this function on a watch
709 * that was not previously added.
710 * Connection lock should be held when calling this.
712 * @param connection the connection.
713 * @param watch the watch to remove.
716 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
719 protected_change_watch (connection, watch,
721 _dbus_watch_list_remove_watch,
726 * Toggles a watch and notifies app via connection's
727 * DBusWatchToggledFunction if available. It's an error to call this
728 * function on a watch that was not previously added.
729 * Connection lock should be held when calling this.
731 * @param connection the connection.
732 * @param watch the watch to toggle.
733 * @param enabled whether to enable or disable
736 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
740 _dbus_assert (watch != NULL);
742 protected_change_watch (connection, watch,
744 _dbus_watch_list_toggle_watch,
748 /** Function to be called in protected_change_timeout() with refcount held */
749 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
750 DBusTimeout *timeout);
751 /** Function to be called in protected_change_timeout() with refcount held */
752 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
753 DBusTimeout *timeout);
754 /** Function to be called in protected_change_timeout() with refcount held */
755 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
756 DBusTimeout *timeout,
757 dbus_bool_t enabled);
760 protected_change_timeout (DBusConnection *connection,
761 DBusTimeout *timeout,
762 DBusTimeoutAddFunction add_function,
763 DBusTimeoutRemoveFunction remove_function,
764 DBusTimeoutToggleFunction toggle_function,
767 DBusTimeoutList *timeouts;
770 HAVE_LOCK_CHECK (connection);
772 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
773 * drop lock and call out" one; but it has to be propagated up through all callers
776 timeouts = connection->timeouts;
779 connection->timeouts = NULL;
780 _dbus_connection_ref_unlocked (connection);
781 CONNECTION_UNLOCK (connection);
784 retval = (* add_function) (timeouts, timeout);
785 else if (remove_function)
788 (* remove_function) (timeouts, timeout);
793 (* toggle_function) (timeouts, timeout, enabled);
796 CONNECTION_LOCK (connection);
797 connection->timeouts = timeouts;
798 _dbus_connection_unref_unlocked (connection);
807 * Adds a timeout using the connection's DBusAddTimeoutFunction if
808 * available. Otherwise records the timeout to be added when said
809 * function is available. Also re-adds the timeout if the
810 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
811 * The timeout will fire repeatedly until removed.
812 * Connection lock should be held when calling this.
814 * @param connection the connection.
815 * @param timeout the timeout to add.
816 * @returns #TRUE on success.
819 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
820 DBusTimeout *timeout)
822 return protected_change_timeout (connection, timeout,
823 _dbus_timeout_list_add_timeout,
828 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
829 * if available. It's an error to call this function on a timeout
830 * that was not previously added.
831 * Connection lock should be held when calling this.
833 * @param connection the connection.
834 * @param timeout the timeout to remove.
837 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
838 DBusTimeout *timeout)
840 protected_change_timeout (connection, timeout,
842 _dbus_timeout_list_remove_timeout,
847 * Toggles a timeout and notifies app via connection's
848 * DBusTimeoutToggledFunction if available. It's an error to call this
849 * function on a timeout that was not previously added.
850 * Connection lock should be held when calling this.
852 * @param connection the connection.
853 * @param timeout the timeout to toggle.
854 * @param enabled whether to enable or disable
857 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
858 DBusTimeout *timeout,
861 protected_change_timeout (connection, timeout,
863 _dbus_timeout_list_toggle_timeout,
868 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
869 DBusPendingCall *pending)
871 dbus_uint32_t reply_serial;
872 DBusTimeout *timeout;
874 HAVE_LOCK_CHECK (connection);
876 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
878 _dbus_assert (reply_serial != 0);
880 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
882 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
885 if (!_dbus_hash_table_insert_int (connection->pending_replies,
889 _dbus_connection_remove_timeout_unlocked (connection, timeout);
891 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
892 HAVE_LOCK_CHECK (connection);
896 _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
898 _dbus_pending_call_ref_unlocked (pending);
900 HAVE_LOCK_CHECK (connection);
906 free_pending_call_on_hash_removal (void *data)
908 DBusPendingCall *pending;
909 DBusConnection *connection;
916 connection = _dbus_pending_call_get_connection_unlocked (pending);
918 HAVE_LOCK_CHECK (connection);
920 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
922 _dbus_connection_remove_timeout_unlocked (connection,
923 _dbus_pending_call_get_timeout_unlocked (pending));
925 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
928 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
929 * here, but the pending call finalizer could in principle call out to
930 * application code so we pretty much have to... some larger code reorg
933 _dbus_connection_ref_unlocked (connection);
934 _dbus_pending_call_unref_and_unlock (pending);
935 CONNECTION_LOCK (connection);
936 _dbus_connection_unref_unlocked (connection);
940 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
941 DBusPendingCall *pending)
943 /* This ends up unlocking to call the pending call finalizer, which is unexpected to
946 _dbus_hash_table_remove_int (connection->pending_replies,
947 _dbus_pending_call_get_reply_serial_unlocked (pending));
951 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
952 DBusPendingCall *pending)
954 /* The idea here is to avoid finalizing the pending call
955 * with the lock held, since there's a destroy notifier
956 * in pending call that goes out to application code.
958 * There's an extra unlock inside the hash table
959 * "free pending call" function FIXME...
961 _dbus_pending_call_ref_unlocked (pending);
962 _dbus_hash_table_remove_int (connection->pending_replies,
963 _dbus_pending_call_get_reply_serial_unlocked (pending));
964 _dbus_pending_call_unref_and_unlock (pending);
968 * Removes a pending call from the connection, such that
969 * the pending reply will be ignored. May drop the last
970 * reference to the pending call.
972 * @param connection the connection
973 * @param pending the pending call
976 _dbus_connection_remove_pending_call (DBusConnection *connection,
977 DBusPendingCall *pending)
979 CONNECTION_LOCK (connection);
980 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
984 * Acquire the transporter I/O path. This must be done before
985 * doing any I/O in the transporter. May sleep and drop the
986 * IO path mutex while waiting for the I/O path.
988 * @param connection the connection.
989 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
990 * @returns TRUE if the I/O path was acquired.
993 _dbus_connection_acquire_io_path (DBusConnection *connection,
994 int timeout_milliseconds)
996 dbus_bool_t we_acquired;
998 HAVE_LOCK_CHECK (connection);
1000 /* We don't want the connection to vanish */
1001 _dbus_connection_ref_unlocked (connection);
1003 /* We will only touch io_path_acquired which is protected by our mutex */
1004 CONNECTION_UNLOCK (connection);
1006 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1007 _dbus_mutex_lock (connection->io_path_mutex);
1009 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
1010 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
1012 we_acquired = FALSE;
1014 if (connection->io_path_acquired)
1016 if (timeout_milliseconds != -1)
1018 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
1019 _DBUS_FUNCTION_NAME, timeout_milliseconds);
1021 if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1022 connection->io_path_mutex,
1023 timeout_milliseconds))
1025 /* We timed out before anyone signaled. */
1026 /* (writing the loop to handle the !timedout case by
1027 * waiting longer if needed is a pain since dbus
1028 * wraps pthread_cond_timedwait to take a relative
1029 * time instead of absolute, something kind of stupid
1030 * on our part. for now it doesn't matter, we will just
1031 * end up back here eventually.)
1037 while (connection->io_path_acquired)
1039 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
1040 _dbus_condvar_wait (connection->io_path_cond,
1041 connection->io_path_mutex);
1046 if (!connection->io_path_acquired)
1049 connection->io_path_acquired = TRUE;
1052 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
1053 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
1055 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1056 _dbus_mutex_unlock (connection->io_path_mutex);
1058 CONNECTION_LOCK (connection);
1060 HAVE_LOCK_CHECK (connection);
1062 _dbus_connection_unref_unlocked (connection);
1068 * Release the I/O path when you're done with it. Only call
1069 * after you've acquired the I/O. Wakes up at most one thread
1070 * currently waiting to acquire the I/O path.
1072 * @param connection the connection.
1075 _dbus_connection_release_io_path (DBusConnection *connection)
1077 HAVE_LOCK_CHECK (connection);
1079 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1080 _dbus_mutex_lock (connection->io_path_mutex);
1082 _dbus_assert (connection->io_path_acquired);
1084 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
1085 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
1087 connection->io_path_acquired = FALSE;
1088 _dbus_condvar_wake_one (connection->io_path_cond);
1090 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1091 _dbus_mutex_unlock (connection->io_path_mutex);
1095 * Queues incoming messages and sends outgoing messages for this
1096 * connection, optionally blocking in the process. Each call to
1097 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1098 * time and then read or write data if possible.
1100 * The purpose of this function is to be able to flush outgoing
1101 * messages or queue up incoming messages without returning
1102 * control to the application and causing reentrancy weirdness.
1104 * The flags parameter allows you to specify whether to
1105 * read incoming messages, write outgoing messages, or both,
1106 * and whether to block if no immediate action is possible.
1108 * The timeout_milliseconds parameter does nothing unless the
1109 * iteration is blocking.
1111 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1112 * wasn't specified, then it's impossible to block, even if
1113 * you specify DBUS_ITERATION_BLOCK; in that case the function
1114 * returns immediately.
1116 * Called with connection lock held.
1118 * @param connection the connection.
1119 * @param flags iteration flags.
1120 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1123 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1125 int timeout_milliseconds)
1127 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1129 HAVE_LOCK_CHECK (connection);
1131 if (connection->n_outgoing == 0)
1132 flags &= ~DBUS_ITERATION_DO_WRITING;
1134 if (_dbus_connection_acquire_io_path (connection,
1135 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1137 HAVE_LOCK_CHECK (connection);
1139 _dbus_transport_do_iteration (connection->transport,
1140 flags, timeout_milliseconds);
1141 _dbus_connection_release_io_path (connection);
1144 HAVE_LOCK_CHECK (connection);
1146 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1150 * Creates a new connection for the given transport. A transport
1151 * represents a message stream that uses some concrete mechanism, such
1152 * as UNIX domain sockets. May return #NULL if insufficient
1153 * memory exists to create the connection.
1155 * @param transport the transport.
1156 * @returns the new connection, or #NULL on failure.
1159 _dbus_connection_new_for_transport (DBusTransport *transport)
1161 DBusConnection *connection;
1162 DBusWatchList *watch_list;
1163 DBusTimeoutList *timeout_list;
1164 DBusHashTable *pending_replies;
1165 DBusList *disconnect_link;
1166 DBusMessage *disconnect_message;
1167 DBusCounter *outgoing_counter;
1168 DBusObjectTree *objects;
1172 pending_replies = NULL;
1173 timeout_list = NULL;
1174 disconnect_link = NULL;
1175 disconnect_message = NULL;
1176 outgoing_counter = NULL;
1179 watch_list = _dbus_watch_list_new ();
1180 if (watch_list == NULL)
1183 timeout_list = _dbus_timeout_list_new ();
1184 if (timeout_list == NULL)
1188 _dbus_hash_table_new (DBUS_HASH_INT,
1190 (DBusFreeFunction)free_pending_call_on_hash_removal);
1191 if (pending_replies == NULL)
1194 connection = dbus_new0 (DBusConnection, 1);
1195 if (connection == NULL)
1198 _dbus_mutex_new_at_location (&connection->mutex);
1199 if (connection->mutex == NULL)
1202 _dbus_mutex_new_at_location (&connection->io_path_mutex);
1203 if (connection->io_path_mutex == NULL)
1206 _dbus_mutex_new_at_location (&connection->dispatch_mutex);
1207 if (connection->dispatch_mutex == NULL)
1210 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1211 if (connection->dispatch_cond == NULL)
1214 _dbus_condvar_new_at_location (&connection->io_path_cond);
1215 if (connection->io_path_cond == NULL)
1218 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1219 DBUS_INTERFACE_LOCAL,
1222 if (disconnect_message == NULL)
1225 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1226 if (disconnect_link == NULL)
1229 outgoing_counter = _dbus_counter_new ();
1230 if (outgoing_counter == NULL)
1233 objects = _dbus_object_tree_new (connection);
1234 if (objects == NULL)
1237 if (_dbus_modify_sigpipe)
1238 _dbus_disable_sigpipe ();
1240 connection->refcount.value = 1;
1241 connection->transport = transport;
1242 connection->watches = watch_list;
1243 connection->timeouts = timeout_list;
1244 connection->pending_replies = pending_replies;
1245 connection->outgoing_counter = outgoing_counter;
1246 connection->filter_list = NULL;
1247 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1248 connection->objects = objects;
1249 connection->exit_on_disconnect = FALSE;
1250 connection->shareable = FALSE;
1251 connection->route_peer_messages = FALSE;
1252 connection->disconnected_message_arrived = FALSE;
1253 connection->disconnected_message_processed = FALSE;
1255 #ifndef DBUS_DISABLE_CHECKS
1256 connection->generation = _dbus_current_generation;
1259 _dbus_data_slot_list_init (&connection->slot_list);
1261 connection->client_serial = 1;
1263 connection->disconnect_message_link = disconnect_link;
1265 CONNECTION_LOCK (connection);
1267 if (!_dbus_transport_set_connection (transport, connection))
1269 CONNECTION_UNLOCK (connection);
1274 _dbus_transport_ref (transport);
1276 CONNECTION_UNLOCK (connection);
1281 if (disconnect_message != NULL)
1282 dbus_message_unref (disconnect_message);
1284 if (disconnect_link != NULL)
1285 _dbus_list_free_link (disconnect_link);
1287 if (connection != NULL)
1289 _dbus_condvar_free_at_location (&connection->io_path_cond);
1290 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1291 _dbus_mutex_free_at_location (&connection->mutex);
1292 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1293 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1294 dbus_free (connection);
1296 if (pending_replies)
1297 _dbus_hash_table_unref (pending_replies);
1300 _dbus_watch_list_free (watch_list);
1303 _dbus_timeout_list_free (timeout_list);
1305 if (outgoing_counter)
1306 _dbus_counter_unref (outgoing_counter);
1309 _dbus_object_tree_unref (objects);
1315 * Increments the reference count of a DBusConnection.
1316 * Requires that the caller already holds the connection lock.
1318 * @param connection the connection.
1319 * @returns the connection.
1322 _dbus_connection_ref_unlocked (DBusConnection *connection)
1324 _dbus_assert (connection != NULL);
1325 _dbus_assert (connection->generation == _dbus_current_generation);
1327 HAVE_LOCK_CHECK (connection);
1329 #ifdef DBUS_HAVE_ATOMIC_INT
1330 _dbus_atomic_inc (&connection->refcount);
1332 _dbus_assert (connection->refcount.value > 0);
1333 connection->refcount.value += 1;
1340 * Decrements the reference count of a DBusConnection.
1341 * Requires that the caller already holds the connection lock.
1343 * @param connection the connection.
1346 _dbus_connection_unref_unlocked (DBusConnection *connection)
1348 dbus_bool_t last_unref;
1350 HAVE_LOCK_CHECK (connection);
1352 _dbus_assert (connection != NULL);
1354 /* The connection lock is better than the global
1355 * lock in the atomic increment fallback
1358 #ifdef DBUS_HAVE_ATOMIC_INT
1359 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1361 _dbus_assert (connection->refcount.value > 0);
1363 connection->refcount.value -= 1;
1364 last_unref = (connection->refcount.value == 0);
1366 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1371 _dbus_connection_last_unref (connection);
1374 static dbus_uint32_t
1375 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1379 serial = connection->client_serial++;
1381 if (connection->client_serial < 0)
1382 connection->client_serial = 1;
1388 * A callback for use with dbus_watch_new() to create a DBusWatch.
1390 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1391 * and the virtual handle_watch in DBusTransport if we got rid of it.
1392 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1395 * @param watch the watch.
1396 * @param condition the current condition of the file descriptors being watched.
1397 * @param data must be a pointer to a #DBusConnection
1398 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1401 _dbus_connection_handle_watch (DBusWatch *watch,
1402 unsigned int condition,
1405 DBusConnection *connection;
1407 DBusDispatchStatus status;
1411 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1413 CONNECTION_LOCK (connection);
1414 _dbus_connection_acquire_io_path (connection, -1);
1415 HAVE_LOCK_CHECK (connection);
1416 retval = _dbus_transport_handle_watch (connection->transport,
1419 _dbus_connection_release_io_path (connection);
1421 HAVE_LOCK_CHECK (connection);
1423 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1425 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1427 /* this calls out to user code */
1428 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1430 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1435 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1436 static DBusHashTable *shared_connections = NULL;
1439 shared_connections_shutdown (void *data)
1443 _DBUS_LOCK (shared_connections);
1445 /* This is a little bit unpleasant... better ideas? */
1446 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
1448 DBusConnection *connection;
1449 DBusMessage *message;
1452 _dbus_hash_iter_init (shared_connections, &iter);
1453 _dbus_hash_iter_next (&iter);
1455 connection = _dbus_hash_iter_get_value (&iter);
1457 _DBUS_UNLOCK (shared_connections);
1459 dbus_connection_ref (connection);
1460 _dbus_connection_close_possibly_shared (connection);
1462 /* Churn through to the Disconnected message */
1463 while ((message = dbus_connection_pop_message (connection)))
1465 dbus_message_unref (message);
1467 dbus_connection_unref (connection);
1469 _DBUS_LOCK (shared_connections);
1471 /* The connection should now be dead and not in our hash ... */
1472 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
1475 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1477 _dbus_hash_table_unref (shared_connections);
1478 shared_connections = NULL;
1480 _DBUS_UNLOCK (shared_connections);
1484 connection_lookup_shared (DBusAddressEntry *entry,
1485 DBusConnection **result)
1487 _dbus_verbose ("checking for existing connection\n");
1491 _DBUS_LOCK (shared_connections);
1493 if (shared_connections == NULL)
1495 _dbus_verbose ("creating shared_connections hash table\n");
1497 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1500 if (shared_connections == NULL)
1502 _DBUS_UNLOCK (shared_connections);
1506 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1508 _dbus_hash_table_unref (shared_connections);
1509 shared_connections = NULL;
1510 _DBUS_UNLOCK (shared_connections);
1514 _dbus_verbose (" successfully created shared_connections\n");
1516 _DBUS_UNLOCK (shared_connections);
1517 return TRUE; /* no point looking up in the hash we just made */
1523 guid = dbus_address_entry_get_value (entry, "guid");
1527 DBusConnection *connection;
1529 connection = _dbus_hash_table_lookup_string (shared_connections,
1534 /* The DBusConnection can't be finalized without taking
1535 * the shared_connections lock to remove it from the
1536 * hash. So it's safe to ref the connection here.
1537 * However, it may be disconnected if the Disconnected
1538 * message hasn't been processed yet, in which case we
1539 * want to pretend it isn't in the hash and avoid
1542 * The idea is to avoid ever returning a disconnected connection
1543 * from dbus_connection_open(). We could just synchronously
1544 * drop our shared ref to the connection on connection disconnect,
1545 * and then assert here that the connection is connected, but
1546 * that causes reentrancy headaches.
1548 CONNECTION_LOCK (connection);
1549 if (_dbus_connection_get_is_connected_unlocked (connection))
1551 _dbus_connection_ref_unlocked (connection);
1552 *result = connection;
1553 _dbus_verbose ("looked up existing connection to server guid %s\n",
1558 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1561 CONNECTION_UNLOCK (connection);
1565 _DBUS_UNLOCK (shared_connections);
1571 connection_record_shared_unlocked (DBusConnection *connection,
1575 char *guid_in_connection;
1577 HAVE_LOCK_CHECK (connection);
1578 _dbus_assert (connection->server_guid == NULL);
1579 _dbus_assert (connection->shareable);
1581 /* get a hard ref on this connection, even if
1582 * we won't in fact store it in the hash, we still
1583 * need to hold a ref on it until it's disconnected.
1585 _dbus_connection_ref_unlocked (connection);
1588 return TRUE; /* don't store in the hash */
1590 /* A separate copy of the key is required in the hash table, because
1591 * we don't have a lock on the connection when we are doing a hash
1595 guid_key = _dbus_strdup (guid);
1596 if (guid_key == NULL)
1599 guid_in_connection = _dbus_strdup (guid);
1600 if (guid_in_connection == NULL)
1602 dbus_free (guid_key);
1606 _DBUS_LOCK (shared_connections);
1607 _dbus_assert (shared_connections != NULL);
1609 if (!_dbus_hash_table_insert_string (shared_connections,
1610 guid_key, connection))
1612 dbus_free (guid_key);
1613 dbus_free (guid_in_connection);
1614 _DBUS_UNLOCK (shared_connections);
1618 connection->server_guid = guid_in_connection;
1620 _dbus_verbose ("stored connection to %s to be shared\n",
1621 connection->server_guid);
1623 _DBUS_UNLOCK (shared_connections);
1625 _dbus_assert (connection->server_guid != NULL);
1631 connection_forget_shared_unlocked (DBusConnection *connection)
1633 HAVE_LOCK_CHECK (connection);
1635 if (!connection->shareable)
1638 if (connection->server_guid != NULL)
1640 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1641 connection->server_guid);
1643 _DBUS_LOCK (shared_connections);
1645 if (!_dbus_hash_table_remove_string (shared_connections,
1646 connection->server_guid))
1647 _dbus_assert_not_reached ("connection was not in the shared table");
1649 dbus_free (connection->server_guid);
1650 connection->server_guid = NULL;
1651 _DBUS_UNLOCK (shared_connections);
1654 /* remove our reference held on all shareable connections */
1655 _dbus_connection_unref_unlocked (connection);
1658 static DBusConnection*
1659 connection_try_from_address_entry (DBusAddressEntry *entry,
1662 DBusTransport *transport;
1663 DBusConnection *connection;
1665 transport = _dbus_transport_open (entry, error);
1667 if (transport == NULL)
1669 _DBUS_ASSERT_ERROR_IS_SET (error);
1673 connection = _dbus_connection_new_for_transport (transport);
1675 _dbus_transport_unref (transport);
1677 if (connection == NULL)
1679 _DBUS_SET_OOM (error);
1683 #ifndef DBUS_DISABLE_CHECKS
1684 _dbus_assert (!connection->have_connection_lock);
1690 * If the shared parameter is true, then any existing connection will
1691 * be used (and if a new connection is created, it will be available
1692 * for use by others). If the shared parameter is false, a new
1693 * connection will always be created, and the new connection will
1694 * never be returned to other callers.
1696 * @param address the address
1697 * @param shared whether the connection is shared or private
1698 * @param error error return
1699 * @returns the connection or #NULL on error
1701 static DBusConnection*
1702 _dbus_connection_open_internal (const char *address,
1706 DBusConnection *connection;
1707 DBusAddressEntry **entries;
1708 DBusError tmp_error;
1709 DBusError first_error;
1712 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1714 _dbus_verbose ("opening %s connection to: %s\n",
1715 shared ? "shared" : "private", address);
1717 if (!dbus_parse_address (address, &entries, &len, error))
1720 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1724 dbus_error_init (&tmp_error);
1725 dbus_error_init (&first_error);
1726 for (i = 0; i < len; i++)
1730 if (!connection_lookup_shared (entries[i], &connection))
1731 _DBUS_SET_OOM (&tmp_error);
1734 if (connection == NULL)
1736 connection = connection_try_from_address_entry (entries[i],
1739 if (connection != NULL && shared)
1743 connection->shareable = TRUE;
1745 /* guid may be NULL */
1746 guid = dbus_address_entry_get_value (entries[i], "guid");
1748 CONNECTION_LOCK (connection);
1750 if (!connection_record_shared_unlocked (connection, guid))
1752 _DBUS_SET_OOM (&tmp_error);
1753 _dbus_connection_close_possibly_shared_and_unlock (connection);
1754 dbus_connection_unref (connection);
1758 CONNECTION_UNLOCK (connection);
1765 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1768 dbus_move_error (&tmp_error, &first_error);
1770 dbus_error_free (&tmp_error);
1773 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1774 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1776 if (connection == NULL)
1778 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1779 dbus_move_error (&first_error, error);
1782 dbus_error_free (&first_error);
1784 dbus_address_entries_free (entries);
1789 * Closes a shared OR private connection, while dbus_connection_close() can
1790 * only be used on private connections. Should only be called by the
1791 * dbus code that owns the connection - an owner must be known,
1792 * the open/close state is like malloc/free, not like ref/unref.
1794 * @param connection the connection
1797 _dbus_connection_close_possibly_shared (DBusConnection *connection)
1799 _dbus_assert (connection != NULL);
1800 _dbus_assert (connection->generation == _dbus_current_generation);
1802 CONNECTION_LOCK (connection);
1803 _dbus_connection_close_possibly_shared_and_unlock (connection);
1806 static DBusPreallocatedSend*
1807 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1809 DBusPreallocatedSend *preallocated;
1811 HAVE_LOCK_CHECK (connection);
1813 _dbus_assert (connection != NULL);
1815 preallocated = dbus_new (DBusPreallocatedSend, 1);
1816 if (preallocated == NULL)
1819 if (connection->link_cache != NULL)
1821 preallocated->queue_link =
1822 _dbus_list_pop_first_link (&connection->link_cache);
1823 preallocated->queue_link->data = NULL;
1827 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1828 if (preallocated->queue_link == NULL)
1832 if (connection->link_cache != NULL)
1834 preallocated->counter_link =
1835 _dbus_list_pop_first_link (&connection->link_cache);
1836 preallocated->counter_link->data = connection->outgoing_counter;
1840 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1841 if (preallocated->counter_link == NULL)
1845 _dbus_counter_ref (preallocated->counter_link->data);
1847 preallocated->connection = connection;
1849 return preallocated;
1852 _dbus_list_free_link (preallocated->queue_link);
1854 dbus_free (preallocated);
1859 /* Called with lock held, does not update dispatch status */
1861 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
1862 DBusPreallocatedSend *preallocated,
1863 DBusMessage *message,
1864 dbus_uint32_t *client_serial)
1866 dbus_uint32_t serial;
1869 preallocated->queue_link->data = message;
1870 _dbus_list_prepend_link (&connection->outgoing_messages,
1871 preallocated->queue_link);
1873 _dbus_message_add_size_counter_link (message,
1874 preallocated->counter_link);
1876 dbus_free (preallocated);
1877 preallocated = NULL;
1879 dbus_message_ref (message);
1881 connection->n_outgoing += 1;
1883 sig = dbus_message_get_signature (message);
1885 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
1887 dbus_message_get_type (message),
1888 dbus_message_get_path (message) ?
1889 dbus_message_get_path (message) :
1891 dbus_message_get_interface (message) ?
1892 dbus_message_get_interface (message) :
1894 dbus_message_get_member (message) ?
1895 dbus_message_get_member (message) :
1898 dbus_message_get_destination (message) ?
1899 dbus_message_get_destination (message) :
1902 connection->n_outgoing);
1904 if (dbus_message_get_serial (message) == 0)
1906 serial = _dbus_connection_get_next_client_serial (connection);
1907 _dbus_message_set_serial (message, serial);
1909 *client_serial = serial;
1914 *client_serial = dbus_message_get_serial (message);
1917 _dbus_verbose ("Message %p serial is %u\n",
1918 message, dbus_message_get_serial (message));
1920 _dbus_message_lock (message);
1922 /* Now we need to run an iteration to hopefully just write the messages
1923 * out immediately, and otherwise get them queued up
1925 _dbus_connection_do_iteration_unlocked (connection,
1926 DBUS_ITERATION_DO_WRITING,
1929 /* If stuff is still queued up, be sure we wake up the main loop */
1930 if (connection->n_outgoing > 0)
1931 _dbus_connection_wakeup_mainloop (connection);
1935 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
1936 DBusPreallocatedSend *preallocated,
1937 DBusMessage *message,
1938 dbus_uint32_t *client_serial)
1940 DBusDispatchStatus status;
1942 HAVE_LOCK_CHECK (connection);
1944 _dbus_connection_send_preallocated_unlocked_no_update (connection,
1946 message, client_serial);
1948 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1949 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1951 /* this calls out to user code */
1952 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1956 * Like dbus_connection_send(), but assumes the connection
1957 * is already locked on function entry, and unlocks before returning.
1959 * @param connection the connection
1960 * @param message the message to send
1961 * @param client_serial return location for client serial of sent message
1962 * @returns #FALSE on out-of-memory
1965 _dbus_connection_send_and_unlock (DBusConnection *connection,
1966 DBusMessage *message,
1967 dbus_uint32_t *client_serial)
1969 DBusPreallocatedSend *preallocated;
1971 _dbus_assert (connection != NULL);
1972 _dbus_assert (message != NULL);
1974 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
1975 if (preallocated == NULL)
1977 CONNECTION_UNLOCK (connection);
1981 _dbus_connection_send_preallocated_and_unlock (connection,
1989 * Used internally to handle the semantics of dbus_server_set_new_connection_function().
1990 * If the new connection function does not ref the connection, we want to close it.
1992 * A bit of a hack, probably the new connection function should have returned a value
1993 * for whether to close, or should have had to close the connection itself if it
1996 * But, this works OK as long as the new connection function doesn't do anything
1997 * crazy like keep the connection around without ref'ing it.
1999 * We have to lock the connection across refcount check and close in case
2000 * the new connection function spawns a thread that closes and unrefs.
2001 * In that case, if the app thread
2002 * closes and unrefs first, we'll harmlessly close again; if the app thread
2003 * still has the ref, we'll close and then the app will close harmlessly.
2004 * If the app unrefs without closing, the app is broken since if the
2005 * app refs from the new connection function it is supposed to also close.
2007 * If we didn't atomically check the refcount and close with the lock held
2008 * though, we could screw this up.
2010 * @param connection the connection
2013 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
2015 CONNECTION_LOCK (connection);
2017 _dbus_assert (connection->refcount.value > 0);
2019 if (connection->refcount.value == 1)
2020 _dbus_connection_close_possibly_shared_and_unlock (connection);
2022 CONNECTION_UNLOCK (connection);
2027 * When a function that blocks has been called with a timeout, and we
2028 * run out of memory, the time to wait for memory is based on the
2029 * timeout. If the caller was willing to block a long time we wait a
2030 * relatively long time for memory, if they were only willing to block
2031 * briefly then we retry for memory at a rapid rate.
2033 * @timeout_milliseconds the timeout requested for blocking
2036 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2038 if (timeout_milliseconds == -1)
2039 _dbus_sleep_milliseconds (1000);
2040 else if (timeout_milliseconds < 100)
2041 ; /* just busy loop */
2042 else if (timeout_milliseconds <= 1000)
2043 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2045 _dbus_sleep_milliseconds (1000);
2048 static DBusMessage *
2049 generate_local_error_message (dbus_uint32_t serial,
2053 DBusMessage *message;
2054 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
2058 if (!dbus_message_set_error_name (message, error_name))
2060 dbus_message_unref (message);
2065 dbus_message_set_no_reply (message, TRUE);
2067 if (!dbus_message_set_reply_serial (message,
2070 dbus_message_unref (message);
2075 if (error_msg != NULL)
2077 DBusMessageIter iter;
2079 dbus_message_iter_init_append (message, &iter);
2080 if (!dbus_message_iter_append_basic (&iter,
2084 dbus_message_unref (message);
2095 /* This is slightly strange since we can pop a message here without
2096 * the dispatch lock.
2099 check_for_reply_unlocked (DBusConnection *connection,
2100 dbus_uint32_t client_serial)
2104 HAVE_LOCK_CHECK (connection);
2106 link = _dbus_list_get_first_link (&connection->incoming_messages);
2108 while (link != NULL)
2110 DBusMessage *reply = link->data;
2112 if (dbus_message_get_reply_serial (reply) == client_serial)
2114 _dbus_list_remove_link (&connection->incoming_messages, link);
2115 connection->n_incoming -= 1;
2118 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2125 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2127 /* We can't iterate over the hash in the normal way since we'll be
2128 * dropping the lock for each item. So we restart the
2129 * iter each time as we drain the hash table.
2132 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2134 DBusPendingCall *pending;
2137 _dbus_hash_iter_init (connection->pending_replies, &iter);
2138 _dbus_hash_iter_next (&iter);
2140 pending = _dbus_hash_iter_get_value (&iter);
2141 _dbus_pending_call_ref_unlocked (pending);
2143 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2145 _dbus_connection_remove_timeout_unlocked (connection,
2146 _dbus_pending_call_get_timeout_unlocked (pending));
2147 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2148 _dbus_hash_iter_remove_entry (&iter);
2150 _dbus_pending_call_unref_and_unlock (pending);
2151 CONNECTION_LOCK (connection);
2153 HAVE_LOCK_CHECK (connection);
2157 complete_pending_call_and_unlock (DBusConnection *connection,
2158 DBusPendingCall *pending,
2159 DBusMessage *message)
2161 _dbus_pending_call_set_reply_unlocked (pending, message);
2162 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2163 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2165 /* Must be called unlocked since it invokes app callback */
2166 _dbus_pending_call_complete (pending);
2167 dbus_pending_call_unref (pending);
2171 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2172 DBusPendingCall *pending)
2175 DBusDispatchStatus status;
2177 reply = check_for_reply_unlocked (connection,
2178 _dbus_pending_call_get_reply_serial_unlocked (pending));
2181 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2183 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2185 complete_pending_call_and_unlock (connection, pending, reply);
2186 dbus_message_unref (reply);
2188 CONNECTION_LOCK (connection);
2189 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2190 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2191 dbus_pending_call_unref (pending);
2200 * Blocks until a pending call times out or gets a reply.
2202 * Does not re-enter the main loop or run filter/path-registered
2203 * callbacks. The reply to the message will not be seen by
2206 * Returns immediately if pending call already got a reply.
2208 * @todo could use performance improvements (it keeps scanning
2209 * the whole message queue for example)
2211 * @param pending the pending call we block for a reply on
2214 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2216 long start_tv_sec, start_tv_usec;
2217 long end_tv_sec, end_tv_usec;
2218 long tv_sec, tv_usec;
2219 DBusDispatchStatus status;
2220 DBusConnection *connection;
2221 dbus_uint32_t client_serial;
2222 int timeout_milliseconds;
2224 _dbus_assert (pending != NULL);
2226 if (dbus_pending_call_get_completed (pending))
2229 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2231 connection = _dbus_pending_call_get_connection_and_lock (pending);
2233 /* Flush message queue - note, can affect dispatch status */
2234 _dbus_connection_flush_unlocked (connection);
2236 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2238 /* note that timeout_milliseconds is limited to a smallish value
2239 * in _dbus_pending_call_new() so overflows aren't possible
2242 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending));
2244 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2245 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2246 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2247 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2248 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2250 _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",
2251 timeout_milliseconds,
2253 start_tv_sec, start_tv_usec,
2254 end_tv_sec, end_tv_usec);
2256 /* check to see if we already got the data off the socket */
2257 /* from another blocked pending call */
2258 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2261 /* Now we wait... */
2262 /* always block at least once as we know we don't have the reply yet */
2263 _dbus_connection_do_iteration_unlocked (connection,
2264 DBUS_ITERATION_DO_READING |
2265 DBUS_ITERATION_BLOCK,
2266 timeout_milliseconds);
2270 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2272 HAVE_LOCK_CHECK (connection);
2274 /* queue messages and get status */
2276 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2278 /* the get_completed() is in case a dispatch() while we were blocking
2279 * got the reply instead of us.
2281 if (_dbus_pending_call_get_completed_unlocked (pending))
2283 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2284 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2285 dbus_pending_call_unref (pending);
2289 if (status == DBUS_DISPATCH_DATA_REMAINS) {
2290 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2294 _dbus_get_current_time (&tv_sec, &tv_usec);
2296 if (!_dbus_connection_get_is_connected_unlocked (connection))
2298 DBusMessage *error_msg;
2300 error_msg = generate_local_error_message (client_serial,
2301 DBUS_ERROR_DISCONNECTED,
2302 "Connection was disconnected before a reply was received");
2304 /* on OOM error_msg is set to NULL */
2305 complete_pending_call_and_unlock (connection, pending, error_msg);
2306 dbus_pending_call_unref (pending);
2309 else if (tv_sec < start_tv_sec)
2310 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2311 else if (connection->disconnect_message_link == NULL)
2312 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2313 else if (tv_sec < end_tv_sec ||
2314 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2316 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2317 (end_tv_usec - tv_usec) / 1000;
2318 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2319 _dbus_assert (timeout_milliseconds >= 0);
2321 if (status == DBUS_DISPATCH_NEED_MEMORY)
2323 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2324 * we may already have a reply in the buffer and just can't process
2327 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2329 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2333 /* block again, we don't have the reply buffered yet. */
2334 _dbus_connection_do_iteration_unlocked (connection,
2335 DBUS_ITERATION_DO_READING |
2336 DBUS_ITERATION_BLOCK,
2337 timeout_milliseconds);
2340 goto recheck_status;
2343 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2344 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2346 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
2348 /* unlock and call user code */
2349 complete_pending_call_and_unlock (connection, pending, NULL);
2351 /* update user code on dispatch status */
2352 CONNECTION_LOCK (connection);
2353 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2354 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2355 dbus_pending_call_unref (pending);
2361 * @addtogroup DBusConnection
2367 * Gets a connection to a remote address. If a connection to the given
2368 * address already exists, returns the existing connection with its
2369 * reference count incremented. Otherwise, returns a new connection
2370 * and saves the new connection for possible re-use if a future call
2371 * to dbus_connection_open() asks to connect to the same server.
2373 * Use dbus_connection_open_private() to get a dedicated connection
2374 * not shared with other callers of dbus_connection_open().
2376 * If the open fails, the function returns #NULL, and provides a
2377 * reason for the failure in the error parameter. Pass #NULL for the
2378 * error parameter if you aren't interested in the reason for
2381 * Because this connection is shared, no user of the connection
2382 * may call dbus_connection_close(). However, when you are done with the
2383 * connection you should call dbus_connection_unref().
2385 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2386 * unless you have good reason; connections are expensive enough
2387 * that it's wasteful to create lots of connections to the same
2390 * @param address the address.
2391 * @param error address where an error can be returned.
2392 * @returns new connection, or #NULL on failure.
2395 dbus_connection_open (const char *address,
2398 DBusConnection *connection;
2400 _dbus_return_val_if_fail (address != NULL, NULL);
2401 _dbus_return_val_if_error_is_set (error, NULL);
2403 connection = _dbus_connection_open_internal (address,
2411 * Opens a new, dedicated connection to a remote address. Unlike
2412 * dbus_connection_open(), always creates a new connection.
2413 * This connection will not be saved or recycled by libdbus.
2415 * If the open fails, the function returns #NULL, and provides a
2416 * reason for the failure in the error parameter. Pass #NULL for the
2417 * error parameter if you aren't interested in the reason for
2420 * When you are done with this connection, you must
2421 * dbus_connection_close() to disconnect it,
2422 * and dbus_connection_unref() to free the connection object.
2424 * (The dbus_connection_close() can be skipped if the
2425 * connection is already known to be disconnected, for example
2426 * if you are inside a handler for the Disconnected signal.)
2428 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2429 * unless you have good reason; connections are expensive enough
2430 * that it's wasteful to create lots of connections to the same
2433 * @param address the address.
2434 * @param error address where an error can be returned.
2435 * @returns new connection, or #NULL on failure.
2438 dbus_connection_open_private (const char *address,
2441 DBusConnection *connection;
2443 _dbus_return_val_if_fail (address != NULL, NULL);
2444 _dbus_return_val_if_error_is_set (error, NULL);
2446 connection = _dbus_connection_open_internal (address,
2454 * Increments the reference count of a DBusConnection.
2456 * @param connection the connection.
2457 * @returns the connection.
2460 dbus_connection_ref (DBusConnection *connection)
2462 _dbus_return_val_if_fail (connection != NULL, NULL);
2463 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
2465 /* The connection lock is better than the global
2466 * lock in the atomic increment fallback
2469 #ifdef DBUS_HAVE_ATOMIC_INT
2470 _dbus_atomic_inc (&connection->refcount);
2472 CONNECTION_LOCK (connection);
2473 _dbus_assert (connection->refcount.value > 0);
2475 connection->refcount.value += 1;
2476 CONNECTION_UNLOCK (connection);
2483 free_outgoing_message (void *element,
2486 DBusMessage *message = element;
2487 DBusConnection *connection = data;
2489 _dbus_message_remove_size_counter (message,
2490 connection->outgoing_counter,
2492 dbus_message_unref (message);
2495 /* This is run without the mutex held, but after the last reference
2496 * to the connection has been dropped we should have no thread-related
2500 _dbus_connection_last_unref (DBusConnection *connection)
2504 _dbus_verbose ("Finalizing connection %p\n", connection);
2506 _dbus_assert (connection->refcount.value == 0);
2508 /* You have to disconnect the connection before unref:ing it. Otherwise
2509 * you won't get the disconnected message.
2511 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
2512 _dbus_assert (connection->server_guid == NULL);
2514 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
2515 _dbus_object_tree_free_all_unlocked (connection->objects);
2517 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
2518 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
2519 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
2521 _dbus_watch_list_free (connection->watches);
2522 connection->watches = NULL;
2524 _dbus_timeout_list_free (connection->timeouts);
2525 connection->timeouts = NULL;
2527 _dbus_data_slot_list_free (&connection->slot_list);
2529 link = _dbus_list_get_first_link (&connection->filter_list);
2530 while (link != NULL)
2532 DBusMessageFilter *filter = link->data;
2533 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
2535 filter->function = NULL;
2536 _dbus_message_filter_unref (filter); /* calls app callback */
2541 _dbus_list_clear (&connection->filter_list);
2543 /* ---- Done with stuff that invokes application callbacks */
2545 _dbus_object_tree_unref (connection->objects);
2547 _dbus_hash_table_unref (connection->pending_replies);
2548 connection->pending_replies = NULL;
2550 _dbus_list_clear (&connection->filter_list);
2552 _dbus_list_foreach (&connection->outgoing_messages,
2553 free_outgoing_message,
2555 _dbus_list_clear (&connection->outgoing_messages);
2557 _dbus_list_foreach (&connection->incoming_messages,
2558 (DBusForeachFunction) dbus_message_unref,
2560 _dbus_list_clear (&connection->incoming_messages);
2562 _dbus_counter_unref (connection->outgoing_counter);
2564 _dbus_transport_unref (connection->transport);
2566 if (connection->disconnect_message_link)
2568 DBusMessage *message = connection->disconnect_message_link->data;
2569 dbus_message_unref (message);
2570 _dbus_list_free_link (connection->disconnect_message_link);
2573 _dbus_list_clear (&connection->link_cache);
2575 _dbus_condvar_free_at_location (&connection->dispatch_cond);
2576 _dbus_condvar_free_at_location (&connection->io_path_cond);
2578 _dbus_mutex_free_at_location (&connection->io_path_mutex);
2579 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
2581 _dbus_mutex_free_at_location (&connection->mutex);
2583 dbus_free (connection);
2587 * Decrements the reference count of a DBusConnection, and finalizes
2588 * it if the count reaches zero.
2590 * Note: it is a bug to drop the last reference to a connection that
2591 * is still connected.
2593 * For shared connections, libdbus will own a reference
2594 * as long as the connection is connected, so you can know that either
2595 * you don't have the last reference, or it's OK to drop the last reference.
2596 * Most connections are shared. dbus_connection_open() and dbus_bus_get()
2597 * return shared connections.
2599 * For private connections, the creator of the connection must arrange for
2600 * dbus_connection_close() to be called prior to dropping the last reference.
2601 * Private connections come from dbus_connection_open_private() or dbus_bus_get_private().
2603 * @param connection the connection.
2606 dbus_connection_unref (DBusConnection *connection)
2608 dbus_bool_t last_unref;
2610 _dbus_return_if_fail (connection != NULL);
2611 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2613 /* The connection lock is better than the global
2614 * lock in the atomic increment fallback
2617 #ifdef DBUS_HAVE_ATOMIC_INT
2618 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
2620 CONNECTION_LOCK (connection);
2622 _dbus_assert (connection->refcount.value > 0);
2624 connection->refcount.value -= 1;
2625 last_unref = (connection->refcount.value == 0);
2628 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
2631 CONNECTION_UNLOCK (connection);
2636 #ifndef DBUS_DISABLE_CHECKS
2637 if (_dbus_transport_get_is_connected (connection->transport))
2639 _dbus_warn_check_failed ("The last reference on a connection was dropped without closing the connection. This is a bug in an application. See dbus_connection_unref() documentation for details.\n%s",
2640 connection->shareable ?
2641 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" :
2642 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
2646 _dbus_connection_last_unref (connection);
2651 * Note that the transport can disconnect itself (other end drops us)
2652 * and in that case this function never runs. So this function must
2653 * not do anything more than disconnect the transport and update the
2656 * If the transport self-disconnects, then we assume someone will
2657 * dispatch the connection to cause the dispatch status update.
2660 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2662 DBusDispatchStatus status;
2664 HAVE_LOCK_CHECK (connection);
2666 _dbus_verbose ("Disconnecting %p\n", connection);
2668 /* We need to ref because update_dispatch_status_and_unlock will unref
2669 * the connection if it was shared and libdbus was the only remaining
2672 _dbus_connection_ref_unlocked (connection);
2674 _dbus_transport_disconnect (connection->transport);
2676 /* This has the side effect of queuing the disconnect message link
2677 * (unless we don't have enough memory, possibly, so don't assert it).
2678 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
2679 * should never again return the newly-disconnected connection.
2681 * However, we only unref the shared connection and exit_on_disconnect when
2682 * the disconnect message reaches the head of the message queue,
2683 * NOT when it's first queued.
2685 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2687 /* This calls out to user code */
2688 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2690 /* Could also call out to user code */
2691 dbus_connection_unref (connection);
2695 * Closes a private connection, so no further data can be sent or received.
2696 * This disconnects the transport (such as a socket) underlying the
2699 * Attempts to send messages after closing a connection are safe, but will result in
2700 * error replies generated locally in libdbus.
2702 * This function does not affect the connection's reference count. It's
2703 * safe to close a connection more than once; all calls after the
2704 * first do nothing. It's impossible to "reopen" a connection, a
2705 * new connection must be created. This function may result in a call
2706 * to the DBusDispatchStatusFunction set with
2707 * dbus_connection_set_dispatch_status_function(), as the disconnect
2708 * message it generates needs to be dispatched.
2710 * If a connection is dropped by the remote application, it will
2713 * You must close a connection prior to releasing the last reference to
2714 * the connection. If you dbus_connection_unref() for the last time
2715 * without closing the connection, the results are undefined; it
2716 * is a bug in your program and libdbus will try to print a warning.
2718 * You may not close a shared connection. Connections created with
2719 * dbus_connection_open() or dbus_bus_get() are shared.
2720 * These connections are owned by libdbus, and applications should
2721 * only unref them, never close them. Applications can know it is
2722 * safe to unref these connections because libdbus will be holding a
2723 * reference as long as the connection is open. Thus, either the
2724 * connection is closed and it is OK to drop the last reference,
2725 * or the connection is open and the app knows it does not have the
2728 * Connections created with dbus_connection_open_private() or
2729 * dbus_bus_get_private() are not kept track of or referenced by
2730 * libdbus. The creator of these connections is responsible for
2731 * calling dbus_connection_close() prior to releasing the last
2732 * reference, if the connection is not already disconnected.
2734 * @param connection the private (unshared) connection to close
2737 dbus_connection_close (DBusConnection *connection)
2739 _dbus_return_if_fail (connection != NULL);
2740 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2742 CONNECTION_LOCK (connection);
2744 #ifndef DBUS_DISABLE_CHECKS
2745 if (connection->shareable)
2747 CONNECTION_UNLOCK (connection);
2749 _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
2754 _dbus_connection_close_possibly_shared_and_unlock (connection);
2758 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2760 HAVE_LOCK_CHECK (connection);
2761 return _dbus_transport_get_is_connected (connection->transport);
2765 * Gets whether the connection is currently open. A connection may
2766 * become disconnected when the remote application closes its end, or
2767 * exits; a connection may also be disconnected with
2768 * dbus_connection_close().
2770 * There are not separate states for "closed" and "disconnected," the two
2771 * terms are synonymous. This function should really be called
2772 * get_is_open() but for historical reasons is not.
2774 * @param connection the connection.
2775 * @returns #TRUE if the connection is still alive.
2778 dbus_connection_get_is_connected (DBusConnection *connection)
2782 _dbus_return_val_if_fail (connection != NULL, FALSE);
2784 CONNECTION_LOCK (connection);
2785 res = _dbus_connection_get_is_connected_unlocked (connection);
2786 CONNECTION_UNLOCK (connection);
2792 * Gets whether the connection was authenticated. (Note that
2793 * if the connection was authenticated then disconnected,
2794 * this function still returns #TRUE)
2796 * @param connection the connection
2797 * @returns #TRUE if the connection was ever authenticated
2800 dbus_connection_get_is_authenticated (DBusConnection *connection)
2804 _dbus_return_val_if_fail (connection != NULL, FALSE);
2806 CONNECTION_LOCK (connection);
2807 res = _dbus_transport_get_is_authenticated (connection->transport);
2808 CONNECTION_UNLOCK (connection);
2814 * Gets whether the connection is not authenticated as a specific
2815 * user. If the connection is not authenticated, this function
2816 * returns #TRUE, and if it is authenticated but as an anonymous user,
2817 * it returns #TRUE. If it is authenticated as a specific user, then
2818 * this returns #FALSE. (Note that if the connection was authenticated
2819 * as anonymous then disconnected, this function still returns #TRUE.)
2821 * If the connection is not anonymous, you can use
2822 * dbus_connection_get_unix_user() and
2823 * dbus_connection_get_windows_user() to see who it's authorized as.
2825 * If you want to prevent non-anonymous authorization, use
2826 * dbus_server_set_auth_mechanisms() to remove the mechanisms that
2827 * allow proving user identity (i.e. only allow the ANONYMOUS
2830 * @param connection the connection
2831 * @returns #TRUE if not authenticated or authenticated as anonymous
2834 dbus_connection_get_is_anonymous (DBusConnection *connection)
2838 _dbus_return_val_if_fail (connection != NULL, FALSE);
2840 CONNECTION_LOCK (connection);
2841 res = _dbus_transport_get_is_anonymous (connection->transport);
2842 CONNECTION_UNLOCK (connection);
2848 * Gets the ID of the server address we are authenticated to, if this
2849 * connection is on the client side. If the connection is on the
2850 * server side, this will always return #NULL - use dbus_server_get_id()
2851 * to get the ID of your own server, if you are the server side.
2853 * If a client-side connection is not authenticated yet, the ID may be
2854 * available if it was included in the server address, but may not be
2855 * available. The only way to be sure the server ID is available
2856 * is to wait for authentication to complete.
2858 * In general, each mode of connecting to a given server will have
2859 * its own ID. So for example, if the session bus daemon is listening
2860 * on UNIX domain sockets and on TCP, then each of those modalities
2861 * will have its own server ID.
2863 * If you want an ID that identifies an entire session bus, look at
2864 * dbus_bus_get_id() instead (which is just a convenience wrapper
2865 * around the org.freedesktop.DBus.GetId method invoked on the bus).
2867 * You can also get a machine ID; see dbus_get_local_machine_id() to
2868 * get the machine you are on. There isn't a convenience wrapper, but
2869 * you can invoke org.freedesktop.DBus.Peer.GetMachineId on any peer
2870 * to get the machine ID on the other end.
2872 * The D-Bus specification describes the server ID and other IDs in a
2875 * @param connection the connection
2876 * @returns the server ID or #NULL if no memory or the connection is server-side
2879 dbus_connection_get_server_id (DBusConnection *connection)
2883 _dbus_return_val_if_fail (connection != NULL, FALSE);
2885 CONNECTION_LOCK (connection);
2886 id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
2887 CONNECTION_UNLOCK (connection);
2893 * Set whether _exit() should be called when the connection receives a
2894 * disconnect signal. The call to _exit() comes after any handlers for
2895 * the disconnect signal run; handlers can cancel the exit by calling
2898 * By default, exit_on_disconnect is #FALSE; but for message bus
2899 * connections returned from dbus_bus_get() it will be toggled on
2902 * @param connection the connection
2903 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
2906 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
2907 dbus_bool_t exit_on_disconnect)
2909 _dbus_return_if_fail (connection != NULL);
2911 CONNECTION_LOCK (connection);
2912 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
2913 CONNECTION_UNLOCK (connection);
2917 * Preallocates resources needed to send a message, allowing the message
2918 * to be sent without the possibility of memory allocation failure.
2919 * Allows apps to create a future guarantee that they can send
2920 * a message regardless of memory shortages.
2922 * @param connection the connection we're preallocating for.
2923 * @returns the preallocated resources, or #NULL
2925 DBusPreallocatedSend*
2926 dbus_connection_preallocate_send (DBusConnection *connection)
2928 DBusPreallocatedSend *preallocated;
2930 _dbus_return_val_if_fail (connection != NULL, NULL);
2932 CONNECTION_LOCK (connection);
2935 _dbus_connection_preallocate_send_unlocked (connection);
2937 CONNECTION_UNLOCK (connection);
2939 return preallocated;
2943 * Frees preallocated message-sending resources from
2944 * dbus_connection_preallocate_send(). Should only
2945 * be called if the preallocated resources are not used
2946 * to send a message.
2948 * @param connection the connection
2949 * @param preallocated the resources
2952 dbus_connection_free_preallocated_send (DBusConnection *connection,
2953 DBusPreallocatedSend *preallocated)
2955 _dbus_return_if_fail (connection != NULL);
2956 _dbus_return_if_fail (preallocated != NULL);
2957 _dbus_return_if_fail (connection == preallocated->connection);
2959 _dbus_list_free_link (preallocated->queue_link);
2960 _dbus_counter_unref (preallocated->counter_link->data);
2961 _dbus_list_free_link (preallocated->counter_link);
2962 dbus_free (preallocated);
2966 * Sends a message using preallocated resources. This function cannot fail.
2967 * It works identically to dbus_connection_send() in other respects.
2968 * Preallocated resources comes from dbus_connection_preallocate_send().
2969 * This function "consumes" the preallocated resources, they need not
2970 * be freed separately.
2972 * @param connection the connection
2973 * @param preallocated the preallocated resources
2974 * @param message the message to send
2975 * @param client_serial return location for client serial assigned to the message
2978 dbus_connection_send_preallocated (DBusConnection *connection,
2979 DBusPreallocatedSend *preallocated,
2980 DBusMessage *message,
2981 dbus_uint32_t *client_serial)
2983 _dbus_return_if_fail (connection != NULL);
2984 _dbus_return_if_fail (preallocated != NULL);
2985 _dbus_return_if_fail (message != NULL);
2986 _dbus_return_if_fail (preallocated->connection == connection);
2987 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
2988 dbus_message_get_member (message) != NULL);
2989 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
2990 (dbus_message_get_interface (message) != NULL &&
2991 dbus_message_get_member (message) != NULL));
2993 CONNECTION_LOCK (connection);
2994 _dbus_connection_send_preallocated_and_unlock (connection,
2996 message, client_serial);
3000 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
3001 DBusMessage *message,
3002 dbus_uint32_t *client_serial)
3004 DBusPreallocatedSend *preallocated;
3006 _dbus_assert (connection != NULL);
3007 _dbus_assert (message != NULL);
3009 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3010 if (preallocated == NULL)
3013 _dbus_connection_send_preallocated_unlocked_no_update (connection,
3021 * Adds a message to the outgoing message queue. Does not block to
3022 * write the message to the network; that happens asynchronously. To
3023 * force the message to be written, call dbus_connection_flush().
3024 * Because this only queues the message, the only reason it can
3025 * fail is lack of memory. Even if the connection is disconnected,
3026 * no error will be returned.
3028 * If the function fails due to lack of memory, it returns #FALSE.
3029 * The function will never fail for other reasons; even if the
3030 * connection is disconnected, you can queue an outgoing message,
3031 * though obviously it won't be sent.
3033 * The message serial is used by the remote application to send a
3034 * reply; see dbus_message_get_serial() or the D-Bus specification.
3036 * @param connection the connection.
3037 * @param message the message to write.
3038 * @param serial return location for message serial, or #NULL if you don't care
3039 * @returns #TRUE on success.
3042 dbus_connection_send (DBusConnection *connection,
3043 DBusMessage *message,
3044 dbus_uint32_t *serial)
3046 _dbus_return_val_if_fail (connection != NULL, FALSE);
3047 _dbus_return_val_if_fail (message != NULL, FALSE);
3049 CONNECTION_LOCK (connection);
3051 return _dbus_connection_send_and_unlock (connection,
3057 reply_handler_timeout (void *data)
3059 DBusConnection *connection;
3060 DBusDispatchStatus status;
3061 DBusPendingCall *pending = data;
3063 connection = _dbus_pending_call_get_connection_and_lock (pending);
3065 _dbus_pending_call_queue_timeout_error_unlocked (pending,
3067 _dbus_connection_remove_timeout_unlocked (connection,
3068 _dbus_pending_call_get_timeout_unlocked (pending));
3069 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
3071 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3072 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3074 /* Unlocks, and calls out to user code */
3075 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3081 * Queues a message to send, as with dbus_connection_send(),
3082 * but also returns a #DBusPendingCall used to receive a reply to the
3083 * message. If no reply is received in the given timeout_milliseconds,
3084 * this function expires the pending reply and generates a synthetic
3085 * error reply (generated in-process, not by the remote application)
3086 * indicating that a timeout occurred.
3088 * A #DBusPendingCall will see a reply message before any filters or
3089 * registered object path handlers. See dbus_connection_dispatch() for
3090 * details on when handlers are run.
3092 * A #DBusPendingCall will always see exactly one reply message,
3093 * unless it's cancelled with dbus_pending_call_cancel().
3095 * If #NULL is passed for the pending_return, the #DBusPendingCall
3096 * will still be generated internally, and used to track
3097 * the message reply timeout. This means a timeout error will
3098 * occur if no reply arrives, unlike with dbus_connection_send().
3100 * If -1 is passed for the timeout, a sane default timeout is used. -1
3101 * is typically the best value for the timeout for this reason, unless
3102 * you want a very short or very long timeout. There is no way to
3103 * avoid a timeout entirely, other than passing INT_MAX for the
3104 * timeout to mean "very long timeout." libdbus clamps an INT_MAX
3105 * timeout down to a few hours timeout though.
3107 * @warning if the connection is disconnected, the #DBusPendingCall
3108 * will be set to #NULL, so be careful with this.
3110 * @param connection the connection
3111 * @param message the message to send
3112 * @param pending_return return location for a #DBusPendingCall object, or #NULL if connection is disconnected
3113 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3114 * @returns #FALSE if no memory, #TRUE otherwise.
3118 dbus_connection_send_with_reply (DBusConnection *connection,
3119 DBusMessage *message,
3120 DBusPendingCall **pending_return,
3121 int timeout_milliseconds)
3123 DBusPendingCall *pending;
3124 dbus_int32_t serial = -1;
3125 DBusDispatchStatus status;
3127 _dbus_return_val_if_fail (connection != NULL, FALSE);
3128 _dbus_return_val_if_fail (message != NULL, FALSE);
3129 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3132 *pending_return = NULL;
3134 CONNECTION_LOCK (connection);
3136 if (!_dbus_connection_get_is_connected_unlocked (connection))
3138 CONNECTION_UNLOCK (connection);
3140 *pending_return = NULL;
3145 pending = _dbus_pending_call_new_unlocked (connection,
3146 timeout_milliseconds,
3147 reply_handler_timeout);
3149 if (pending == NULL)
3151 CONNECTION_UNLOCK (connection);
3155 /* Assign a serial to the message */
3156 serial = dbus_message_get_serial (message);
3159 serial = _dbus_connection_get_next_client_serial (connection);
3160 _dbus_message_set_serial (message, serial);
3163 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
3166 /* Insert the serial in the pending replies hash;
3167 * hash takes a refcount on DBusPendingCall.
3168 * Also, add the timeout.
3170 if (!_dbus_connection_attach_pending_call_unlocked (connection,
3174 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
3176 _dbus_connection_detach_pending_call_and_unlock (connection,
3178 goto error_unlocked;
3182 *pending_return = pending; /* hand off refcount */
3185 _dbus_connection_detach_pending_call_unlocked (connection, pending);
3186 /* we still have a ref to the pending call in this case, we unref
3187 * after unlocking, below
3191 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3193 /* this calls out to user code */
3194 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3196 if (pending_return == NULL)
3197 dbus_pending_call_unref (pending);
3202 CONNECTION_UNLOCK (connection);
3204 dbus_pending_call_unref (pending);
3209 * Sends a message and blocks a certain time period while waiting for
3210 * a reply. This function does not reenter the main loop,
3211 * i.e. messages other than the reply are queued up but not
3212 * processed. This function is used to invoke method calls on a
3215 * If a normal reply is received, it is returned, and removed from the
3216 * incoming message queue. If it is not received, #NULL is returned
3217 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
3218 * received, it is converted to a #DBusError and returned as an error,
3219 * then the reply message is deleted and #NULL is returned. If
3220 * something else goes wrong, result is set to whatever is
3221 * appropriate, such as #DBUS_ERROR_NO_MEMORY or
3222 * #DBUS_ERROR_DISCONNECTED.
3224 * @warning While this function blocks the calling thread will not be
3225 * processing the incoming message queue. This means you can end up
3226 * deadlocked if the application you're talking to needs you to reply
3227 * to a method. To solve this, either avoid the situation, block in a
3228 * separate thread from the main connection-dispatching thread, or use
3229 * dbus_pending_call_set_notify() to avoid blocking.
3231 * @param connection the connection
3232 * @param message the message to send
3233 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3234 * @param error return location for error message
3235 * @returns the message that is the reply or #NULL with an error code if the
3239 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
3240 DBusMessage *message,
3241 int timeout_milliseconds,
3245 DBusPendingCall *pending;
3247 _dbus_return_val_if_fail (connection != NULL, NULL);
3248 _dbus_return_val_if_fail (message != NULL, NULL);
3249 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3250 _dbus_return_val_if_error_is_set (error, NULL);
3252 if (!dbus_connection_send_with_reply (connection, message,
3253 &pending, timeout_milliseconds))
3255 _DBUS_SET_OOM (error);
3259 if (pending == NULL)
3261 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
3265 dbus_pending_call_block (pending);
3267 reply = dbus_pending_call_steal_reply (pending);
3268 dbus_pending_call_unref (pending);
3270 /* call_complete_and_unlock() called from pending_call_block() should
3271 * always fill this in.
3273 _dbus_assert (reply != NULL);
3275 if (dbus_set_error_from_message (error, reply))
3277 dbus_message_unref (reply);
3285 * Blocks until the outgoing message queue is empty.
3286 * Assumes connection lock already held.
3288 * If you call this, you MUST call update_dispatch_status afterword...
3290 * @param connection the connection.
3293 _dbus_connection_flush_unlocked (DBusConnection *connection)
3295 /* We have to specify DBUS_ITERATION_DO_READING here because
3296 * otherwise we could have two apps deadlock if they are both doing
3297 * a flush(), and the kernel buffers fill up. This could change the
3300 DBusDispatchStatus status;
3302 HAVE_LOCK_CHECK (connection);
3304 while (connection->n_outgoing > 0 &&
3305 _dbus_connection_get_is_connected_unlocked (connection))
3307 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3308 HAVE_LOCK_CHECK (connection);
3309 _dbus_connection_do_iteration_unlocked (connection,
3310 DBUS_ITERATION_DO_READING |
3311 DBUS_ITERATION_DO_WRITING |
3312 DBUS_ITERATION_BLOCK,
3316 HAVE_LOCK_CHECK (connection);
3317 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3318 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3320 HAVE_LOCK_CHECK (connection);
3325 * Blocks until the outgoing message queue is empty.
3327 * @param connection the connection.
3330 dbus_connection_flush (DBusConnection *connection)
3332 /* We have to specify DBUS_ITERATION_DO_READING here because
3333 * otherwise we could have two apps deadlock if they are both doing
3334 * a flush(), and the kernel buffers fill up. This could change the
3337 DBusDispatchStatus status;
3339 _dbus_return_if_fail (connection != NULL);
3341 CONNECTION_LOCK (connection);
3343 status = _dbus_connection_flush_unlocked (connection);
3345 HAVE_LOCK_CHECK (connection);
3346 /* Unlocks and calls out to user code */
3347 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3349 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
3353 * This function implements dbus_connection_read_write_dispatch() and
3354 * dbus_connection_read_write() (they pass a different value for the
3355 * dispatch parameter).
3357 * @param connection the connection
3358 * @param timeout_milliseconds max time to block or -1 for infinite
3359 * @param dispatch dispatch new messages or leave them on the incoming queue
3360 * @returns #TRUE if the disconnect message has not been processed
3363 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3364 int timeout_milliseconds,
3365 dbus_bool_t dispatch)
3367 DBusDispatchStatus dstatus;
3368 dbus_bool_t no_progress_possible;
3370 dstatus = dbus_connection_get_dispatch_status (connection);
3372 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3374 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
3375 dbus_connection_dispatch (connection);
3376 CONNECTION_LOCK (connection);
3378 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3380 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
3381 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3382 CONNECTION_LOCK (connection);
3386 CONNECTION_LOCK (connection);
3387 if (_dbus_connection_get_is_connected_unlocked (connection))
3389 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3390 _dbus_connection_do_iteration_unlocked (connection,
3391 DBUS_ITERATION_DO_READING |
3392 DBUS_ITERATION_DO_WRITING |
3393 DBUS_ITERATION_BLOCK,
3394 timeout_milliseconds);
3398 HAVE_LOCK_CHECK (connection);
3399 /* If we can dispatch, we can make progress until the Disconnected message
3400 * has been processed; if we can only read/write, we can make progress
3401 * as long as the transport is open.
3404 no_progress_possible = connection->n_incoming == 0 &&
3405 connection->disconnect_message_link == NULL;
3407 no_progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
3408 CONNECTION_UNLOCK (connection);
3409 return !no_progress_possible; /* TRUE if we can make more progress */
3414 * This function is intended for use with applications that don't want
3415 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3416 * example usage would be:
3419 * while (dbus_connection_read_write_dispatch (connection, -1))
3420 * ; // empty loop body
3423 * In this usage you would normally have set up a filter function to look
3424 * at each message as it is dispatched. The loop terminates when the last
3425 * message from the connection (the disconnected signal) is processed.
3427 * If there are messages to dispatch, this function will
3428 * dbus_connection_dispatch() once, and return. If there are no
3429 * messages to dispatch, this function will block until it can read or
3430 * write, then read or write, then return.
3432 * The way to think of this function is that it either makes some sort
3433 * of progress, or it blocks. Note that, while it is blocked on I/O, it
3434 * cannot be interrupted (even by other threads), which makes this function
3435 * unsuitable for applications that do more than just react to received
3438 * The return value indicates whether the disconnect message has been
3439 * processed, NOT whether the connection is connected. This is
3440 * important because even after disconnecting, you want to process any
3441 * messages you received prior to the disconnect.
3443 * @param connection the connection
3444 * @param timeout_milliseconds max time to block or -1 for infinite
3445 * @returns #TRUE if the disconnect message has not been processed
3448 dbus_connection_read_write_dispatch (DBusConnection *connection,
3449 int timeout_milliseconds)
3451 _dbus_return_val_if_fail (connection != NULL, FALSE);
3452 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3453 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3457 * This function is intended for use with applications that don't want to
3458 * write a main loop and deal with #DBusWatch and #DBusTimeout. See also
3459 * dbus_connection_read_write_dispatch().
3461 * As long as the connection is open, this function will block until it can
3462 * read or write, then read or write, then return #TRUE.
3464 * If the connection is closed, the function returns #FALSE.
3466 * The return value indicates whether reading or writing is still
3467 * possible, i.e. whether the connection is connected.
3469 * Note that even after disconnection, messages may remain in the
3470 * incoming queue that need to be
3471 * processed. dbus_connection_read_write_dispatch() dispatches
3472 * incoming messages for you; with dbus_connection_read_write() you
3473 * have to arrange to drain the incoming queue yourself.
3475 * @param connection the connection
3476 * @param timeout_milliseconds max time to block or -1 for infinite
3477 * @returns #TRUE if still connected
3480 dbus_connection_read_write (DBusConnection *connection,
3481 int timeout_milliseconds)
3483 _dbus_return_val_if_fail (connection != NULL, FALSE);
3484 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3485 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3488 /* We need to call this anytime we pop the head of the queue, and then
3489 * update_dispatch_status_and_unlock needs to be called afterward
3490 * which will "process" the disconnected message and set
3491 * disconnected_message_processed.
3494 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3495 DBusMessage *head_of_queue)
3497 HAVE_LOCK_CHECK (connection);
3499 /* checking that the link is NULL is an optimization to avoid the is_signal call */
3500 if (connection->disconnect_message_link == NULL &&
3501 dbus_message_is_signal (head_of_queue,
3502 DBUS_INTERFACE_LOCAL,
3505 connection->disconnected_message_arrived = TRUE;
3510 * Returns the first-received message from the incoming message queue,
3511 * leaving it in the queue. If the queue is empty, returns #NULL.
3513 * The caller does not own a reference to the returned message, and
3514 * must either return it using dbus_connection_return_message() or
3515 * keep it after calling dbus_connection_steal_borrowed_message(). No
3516 * one can get at the message while its borrowed, so return it as
3517 * quickly as possible and don't keep a reference to it after
3518 * returning it. If you need to keep the message, make a copy of it.
3520 * dbus_connection_dispatch() will block if called while a borrowed
3521 * message is outstanding; only one piece of code can be playing with
3522 * the incoming queue at a time. This function will block if called
3523 * during a dbus_connection_dispatch().
3525 * @param connection the connection.
3526 * @returns next message in the incoming queue.
3529 dbus_connection_borrow_message (DBusConnection *connection)
3531 DBusDispatchStatus status;
3532 DBusMessage *message;
3534 _dbus_return_val_if_fail (connection != NULL, NULL);
3536 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3538 /* this is called for the side effect that it queues
3539 * up any messages from the transport
3541 status = dbus_connection_get_dispatch_status (connection);
3542 if (status != DBUS_DISPATCH_DATA_REMAINS)
3545 CONNECTION_LOCK (connection);
3547 _dbus_connection_acquire_dispatch (connection);
3549 /* While a message is outstanding, the dispatch lock is held */
3550 _dbus_assert (connection->message_borrowed == NULL);
3552 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3554 message = connection->message_borrowed;
3556 check_disconnected_message_arrived_unlocked (connection, message);
3558 /* Note that we KEEP the dispatch lock until the message is returned */
3559 if (message == NULL)
3560 _dbus_connection_release_dispatch (connection);
3562 CONNECTION_UNLOCK (connection);
3564 /* We don't update dispatch status until it's returned or stolen */
3570 * Used to return a message after peeking at it using
3571 * dbus_connection_borrow_message(). Only called if
3572 * message from dbus_connection_borrow_message() was non-#NULL.
3574 * @param connection the connection
3575 * @param message the message from dbus_connection_borrow_message()
3578 dbus_connection_return_message (DBusConnection *connection,
3579 DBusMessage *message)
3581 DBusDispatchStatus status;
3583 _dbus_return_if_fail (connection != NULL);
3584 _dbus_return_if_fail (message != NULL);
3585 _dbus_return_if_fail (message == connection->message_borrowed);
3586 _dbus_return_if_fail (connection->dispatch_acquired);
3588 CONNECTION_LOCK (connection);
3590 _dbus_assert (message == connection->message_borrowed);
3592 connection->message_borrowed = NULL;
3594 _dbus_connection_release_dispatch (connection);
3596 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3597 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3601 * Used to keep a message after peeking at it using
3602 * dbus_connection_borrow_message(). Before using this function, see
3603 * the caveats/warnings in the documentation for
3604 * dbus_connection_pop_message().
3606 * @param connection the connection
3607 * @param message the message from dbus_connection_borrow_message()
3610 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3611 DBusMessage *message)
3613 DBusMessage *pop_message;
3614 DBusDispatchStatus status;
3616 _dbus_return_if_fail (connection != NULL);
3617 _dbus_return_if_fail (message != NULL);
3618 _dbus_return_if_fail (message == connection->message_borrowed);
3619 _dbus_return_if_fail (connection->dispatch_acquired);
3621 CONNECTION_LOCK (connection);
3623 _dbus_assert (message == connection->message_borrowed);
3625 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3626 _dbus_assert (message == pop_message);
3628 connection->n_incoming -= 1;
3630 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3631 message, connection->n_incoming);
3633 connection->message_borrowed = NULL;
3635 _dbus_connection_release_dispatch (connection);
3637 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3638 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3641 /* See dbus_connection_pop_message, but requires the caller to own
3642 * the lock before calling. May drop the lock while running.
3645 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3647 HAVE_LOCK_CHECK (connection);
3649 _dbus_assert (connection->message_borrowed == NULL);
3651 if (connection->n_incoming > 0)
3655 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3656 connection->n_incoming -= 1;
3658 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3660 dbus_message_get_type (link->data),
3661 dbus_message_get_path (link->data) ?
3662 dbus_message_get_path (link->data) :
3664 dbus_message_get_interface (link->data) ?
3665 dbus_message_get_interface (link->data) :
3667 dbus_message_get_member (link->data) ?
3668 dbus_message_get_member (link->data) :
3670 dbus_message_get_signature (link->data),
3671 connection, connection->n_incoming);
3673 check_disconnected_message_arrived_unlocked (connection, link->data);
3681 /* See dbus_connection_pop_message, but requires the caller to own
3682 * the lock before calling. May drop the lock while running.
3685 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3689 HAVE_LOCK_CHECK (connection);
3691 link = _dbus_connection_pop_message_link_unlocked (connection);
3695 DBusMessage *message;
3697 message = link->data;
3699 _dbus_list_free_link (link);
3708 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3709 DBusList *message_link)
3711 HAVE_LOCK_CHECK (connection);
3713 _dbus_assert (message_link != NULL);
3714 /* You can't borrow a message while a link is outstanding */
3715 _dbus_assert (connection->message_borrowed == NULL);
3716 /* We had to have the dispatch lock across the pop/putback */
3717 _dbus_assert (connection->dispatch_acquired);
3719 _dbus_list_prepend_link (&connection->incoming_messages,
3721 connection->n_incoming += 1;
3723 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3725 dbus_message_get_type (message_link->data),
3726 dbus_message_get_interface (message_link->data) ?
3727 dbus_message_get_interface (message_link->data) :
3729 dbus_message_get_member (message_link->data) ?
3730 dbus_message_get_member (message_link->data) :
3732 dbus_message_get_signature (message_link->data),
3733 connection, connection->n_incoming);
3737 * Returns the first-received message from the incoming message queue,
3738 * removing it from the queue. The caller owns a reference to the
3739 * returned message. If the queue is empty, returns #NULL.
3741 * This function bypasses any message handlers that are registered,
3742 * and so using it is usually wrong. Instead, let the main loop invoke
3743 * dbus_connection_dispatch(). Popping messages manually is only
3744 * useful in very simple programs that don't share a #DBusConnection
3745 * with any libraries or other modules.
3747 * There is a lock that covers all ways of accessing the incoming message
3748 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3749 * dbus_connection_borrow_message(), etc. will all block while one of the others
3750 * in the group is running.
3752 * @param connection the connection.
3753 * @returns next message in the incoming queue.
3756 dbus_connection_pop_message (DBusConnection *connection)
3758 DBusMessage *message;
3759 DBusDispatchStatus status;
3761 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3763 /* this is called for the side effect that it queues
3764 * up any messages from the transport
3766 status = dbus_connection_get_dispatch_status (connection);
3767 if (status != DBUS_DISPATCH_DATA_REMAINS)
3770 CONNECTION_LOCK (connection);
3771 _dbus_connection_acquire_dispatch (connection);
3772 HAVE_LOCK_CHECK (connection);
3774 message = _dbus_connection_pop_message_unlocked (connection);
3776 _dbus_verbose ("Returning popped message %p\n", message);
3778 _dbus_connection_release_dispatch (connection);
3780 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3781 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3787 * Acquire the dispatcher. This is a separate lock so the main
3788 * connection lock can be dropped to call out to application dispatch
3791 * @param connection the connection.
3794 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3796 HAVE_LOCK_CHECK (connection);
3798 _dbus_connection_ref_unlocked (connection);
3799 CONNECTION_UNLOCK (connection);
3801 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3802 _dbus_mutex_lock (connection->dispatch_mutex);
3804 while (connection->dispatch_acquired)
3806 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3807 _dbus_condvar_wait (connection->dispatch_cond,
3808 connection->dispatch_mutex);
3811 _dbus_assert (!connection->dispatch_acquired);
3813 connection->dispatch_acquired = TRUE;
3815 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3816 _dbus_mutex_unlock (connection->dispatch_mutex);
3818 CONNECTION_LOCK (connection);
3819 _dbus_connection_unref_unlocked (connection);
3823 * Release the dispatcher when you're done with it. Only call
3824 * after you've acquired the dispatcher. Wakes up at most one
3825 * thread currently waiting to acquire the dispatcher.
3827 * @param connection the connection.
3830 _dbus_connection_release_dispatch (DBusConnection *connection)
3832 HAVE_LOCK_CHECK (connection);
3834 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3835 _dbus_mutex_lock (connection->dispatch_mutex);
3837 _dbus_assert (connection->dispatch_acquired);
3839 connection->dispatch_acquired = FALSE;
3840 _dbus_condvar_wake_one (connection->dispatch_cond);
3842 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3843 _dbus_mutex_unlock (connection->dispatch_mutex);
3847 _dbus_connection_failed_pop (DBusConnection *connection,
3848 DBusList *message_link)
3850 _dbus_list_prepend_link (&connection->incoming_messages,
3852 connection->n_incoming += 1;
3855 /* Note this may be called multiple times since we don't track whether we already did it */
3857 notify_disconnected_unlocked (DBusConnection *connection)
3859 HAVE_LOCK_CHECK (connection);
3861 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
3862 * connection from dbus_bus_get(). We make the same guarantee for
3863 * dbus_connection_open() but in a different way since we don't want to
3864 * unref right here; we instead check for connectedness before returning
3865 * the connection from the hash.
3867 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
3869 /* Dump the outgoing queue, we aren't going to be able to
3870 * send it now, and we'd like accessors like
3871 * dbus_connection_get_outgoing_size() to be accurate.
3873 if (connection->n_outgoing > 0)
3877 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3878 connection->n_outgoing);
3880 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3882 _dbus_connection_message_sent (connection, link->data);
3887 /* Note this may be called multiple times since we don't track whether we already did it */
3888 static DBusDispatchStatus
3889 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
3891 HAVE_LOCK_CHECK (connection);
3893 if (connection->disconnect_message_link != NULL)
3895 _dbus_verbose ("Sending disconnect message from %s\n",
3896 _DBUS_FUNCTION_NAME);
3898 /* If we have pending calls, queue their timeouts - we want the Disconnected
3899 * to be the last message, after these timeouts.
3901 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
3903 /* We haven't sent the disconnect message already,
3904 * and all real messages have been queued up.
3906 _dbus_connection_queue_synthesized_message_link (connection,
3907 connection->disconnect_message_link);
3908 connection->disconnect_message_link = NULL;
3910 return DBUS_DISPATCH_DATA_REMAINS;
3913 return DBUS_DISPATCH_COMPLETE;
3916 static DBusDispatchStatus
3917 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3919 HAVE_LOCK_CHECK (connection);
3921 if (connection->n_incoming > 0)
3922 return DBUS_DISPATCH_DATA_REMAINS;
3923 else if (!_dbus_transport_queue_messages (connection->transport))
3924 return DBUS_DISPATCH_NEED_MEMORY;
3927 DBusDispatchStatus status;
3928 dbus_bool_t is_connected;
3930 status = _dbus_transport_get_dispatch_status (connection->transport);
3931 is_connected = _dbus_transport_get_is_connected (connection->transport);
3933 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3934 DISPATCH_STATUS_NAME (status), is_connected);
3938 /* It's possible this would be better done by having an explicit
3939 * notification from _dbus_transport_disconnect() that would
3940 * synchronously do this, instead of waiting for the next dispatch
3941 * status check. However, probably not good to change until it causes
3944 notify_disconnected_unlocked (connection);
3946 /* I'm not sure this is needed; the idea is that we want to
3947 * queue the Disconnected only after we've read all the
3948 * messages, but if we're disconnected maybe we are guaranteed
3949 * to have read them all ?
3951 if (status == DBUS_DISPATCH_COMPLETE)
3952 status = notify_disconnected_and_dispatch_complete_unlocked (connection);
3955 if (status != DBUS_DISPATCH_COMPLETE)
3957 else if (connection->n_incoming > 0)
3958 return DBUS_DISPATCH_DATA_REMAINS;
3960 return DBUS_DISPATCH_COMPLETE;
3965 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
3966 DBusDispatchStatus new_status)
3968 dbus_bool_t changed;
3969 DBusDispatchStatusFunction function;
3972 HAVE_LOCK_CHECK (connection);
3974 _dbus_connection_ref_unlocked (connection);
3976 changed = new_status != connection->last_dispatch_status;
3978 connection->last_dispatch_status = new_status;
3980 function = connection->dispatch_status_function;
3981 data = connection->dispatch_status_data;
3983 if (connection->disconnected_message_arrived &&
3984 !connection->disconnected_message_processed)
3986 connection->disconnected_message_processed = TRUE;
3988 /* this does an unref, but we have a ref
3989 * so we should not run the finalizer here
3992 connection_forget_shared_unlocked (connection);
3994 if (connection->exit_on_disconnect)
3996 CONNECTION_UNLOCK (connection);
3998 _dbus_verbose ("Exiting on Disconnected signal\n");
4000 _dbus_assert_not_reached ("Call to exit() returned");
4004 /* We drop the lock */
4005 CONNECTION_UNLOCK (connection);
4007 if (changed && function)
4009 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
4010 connection, new_status,
4011 DISPATCH_STATUS_NAME (new_status));
4012 (* function) (connection, new_status, data);
4015 dbus_connection_unref (connection);
4019 * Gets the current state of the incoming message queue.
4020 * #DBUS_DISPATCH_DATA_REMAINS indicates that the message queue
4021 * may contain messages. #DBUS_DISPATCH_COMPLETE indicates that the
4022 * incoming queue is empty. #DBUS_DISPATCH_NEED_MEMORY indicates that
4023 * there could be data, but we can't know for sure without more
4026 * To process the incoming message queue, use dbus_connection_dispatch()
4027 * or (in rare cases) dbus_connection_pop_message().
4029 * Note, #DBUS_DISPATCH_DATA_REMAINS really means that either we
4030 * have messages in the queue, or we have raw bytes buffered up
4031 * that need to be parsed. When these bytes are parsed, they
4032 * may not add up to an entire message. Thus, it's possible
4033 * to see a status of #DBUS_DISPATCH_DATA_REMAINS but not
4034 * have a message yet.
4036 * In particular this happens on initial connection, because all sorts
4037 * of authentication protocol stuff has to be parsed before the
4038 * first message arrives.
4040 * @param connection the connection.
4041 * @returns current dispatch status
4044 dbus_connection_get_dispatch_status (DBusConnection *connection)
4046 DBusDispatchStatus status;
4048 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4050 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
4052 CONNECTION_LOCK (connection);
4054 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4056 CONNECTION_UNLOCK (connection);
4062 * Filter funtion for handling the Peer standard interface.
4064 static DBusHandlerResult
4065 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
4066 DBusMessage *message)
4068 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
4070 /* This means we're letting the bus route this message */
4071 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4073 else if (dbus_message_is_method_call (message,
4074 DBUS_INTERFACE_PEER,
4080 ret = dbus_message_new_method_return (message);
4082 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4084 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4086 dbus_message_unref (ret);
4089 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4091 return DBUS_HANDLER_RESULT_HANDLED;
4093 else if (dbus_message_is_method_call (message,
4094 DBUS_INTERFACE_PEER,
4101 ret = dbus_message_new_method_return (message);
4103 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4106 _dbus_string_init (&uuid);
4107 if (_dbus_get_local_machine_uuid_encoded (&uuid))
4109 const char *v_STRING = _dbus_string_get_const_data (&uuid);
4110 if (dbus_message_append_args (ret,
4111 DBUS_TYPE_STRING, &v_STRING,
4114 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4117 _dbus_string_free (&uuid);
4119 dbus_message_unref (ret);
4122 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4124 return DBUS_HANDLER_RESULT_HANDLED;
4126 else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
4128 /* We need to bounce anything else with this interface, otherwise apps
4129 * could start extending the interface and when we added extensions
4130 * here to DBusConnection we'd break those apps.
4136 ret = dbus_message_new_error (message,
4137 DBUS_ERROR_UNKNOWN_METHOD,
4138 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
4140 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4142 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4144 dbus_message_unref (ret);
4147 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4149 return DBUS_HANDLER_RESULT_HANDLED;
4153 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4158 * Processes all builtin filter functions
4160 * If the spec specifies a standard interface
4161 * they should be processed from this method
4163 static DBusHandlerResult
4164 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
4165 DBusMessage *message)
4167 /* We just run one filter for now but have the option to run more
4168 if the spec calls for it in the future */
4170 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
4174 * Processes any incoming data.
4176 * If there's incoming raw data that has not yet been parsed, it is
4177 * parsed, which may or may not result in adding messages to the
4180 * The incoming data buffer is filled when the connection reads from
4181 * its underlying transport (such as a socket). Reading usually
4182 * happens in dbus_watch_handle() or dbus_connection_read_write().
4184 * If there are complete messages in the incoming queue,
4185 * dbus_connection_dispatch() removes one message from the queue and
4186 * processes it. Processing has three steps.
4188 * First, any method replies are passed to #DBusPendingCall or
4189 * dbus_connection_send_with_reply_and_block() in order to
4190 * complete the pending method call.
4192 * Second, any filters registered with dbus_connection_add_filter()
4193 * are run. If any filter returns #DBUS_HANDLER_RESULT_HANDLED
4194 * then processing stops after that filter.
4196 * Third, if the message is a method call it is forwarded to
4197 * any registered object path handlers added with
4198 * dbus_connection_register_object_path() or
4199 * dbus_connection_register_fallback().
4201 * A single call to dbus_connection_dispatch() will process at most
4202 * one message; it will not clear the entire message queue.
4204 * Be careful about calling dbus_connection_dispatch() from inside a
4205 * message handler, i.e. calling dbus_connection_dispatch()
4206 * recursively. If threads have been initialized with a recursive
4207 * mutex function, then this will not deadlock; however, it can
4208 * certainly confuse your application.
4210 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
4212 * @param connection the connection
4213 * @returns dispatch status, see dbus_connection_get_dispatch_status()
4216 dbus_connection_dispatch (DBusConnection *connection)
4218 DBusMessage *message;
4219 DBusList *link, *filter_list_copy, *message_link;
4220 DBusHandlerResult result;
4221 DBusPendingCall *pending;
4222 dbus_int32_t reply_serial;
4223 DBusDispatchStatus status;
4225 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4227 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
4229 CONNECTION_LOCK (connection);
4230 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4231 if (status != DBUS_DISPATCH_DATA_REMAINS)
4233 /* unlocks and calls out to user code */
4234 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4238 /* We need to ref the connection since the callback could potentially
4239 * drop the last ref to it
4241 _dbus_connection_ref_unlocked (connection);
4243 _dbus_connection_acquire_dispatch (connection);
4244 HAVE_LOCK_CHECK (connection);
4246 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4247 if (message_link == NULL)
4249 /* another thread dispatched our stuff */
4251 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
4253 _dbus_connection_release_dispatch (connection);
4255 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4257 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4259 dbus_connection_unref (connection);
4264 message = message_link->data;
4266 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
4268 dbus_message_get_type (message),
4269 dbus_message_get_interface (message) ?
4270 dbus_message_get_interface (message) :
4272 dbus_message_get_member (message) ?
4273 dbus_message_get_member (message) :
4275 dbus_message_get_signature (message));
4277 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4279 /* Pending call handling must be first, because if you do
4280 * dbus_connection_send_with_reply_and_block() or
4281 * dbus_pending_call_block() then no handlers/filters will be run on
4282 * the reply. We want consistent semantics in the case where we
4283 * dbus_connection_dispatch() the reply.
4286 reply_serial = dbus_message_get_reply_serial (message);
4287 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4291 _dbus_verbose ("Dispatching a pending reply\n");
4292 complete_pending_call_and_unlock (connection, pending, message);
4293 pending = NULL; /* it's probably unref'd */
4295 CONNECTION_LOCK (connection);
4296 _dbus_verbose ("pending call completed in dispatch\n");
4297 result = DBUS_HANDLER_RESULT_HANDLED;
4301 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4302 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4305 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4307 _dbus_connection_release_dispatch (connection);
4308 HAVE_LOCK_CHECK (connection);
4310 _dbus_connection_failed_pop (connection, message_link);
4312 /* unlocks and calls user code */
4313 _dbus_connection_update_dispatch_status_and_unlock (connection,
4314 DBUS_DISPATCH_NEED_MEMORY);
4317 dbus_pending_call_unref (pending);
4318 dbus_connection_unref (connection);
4320 return DBUS_DISPATCH_NEED_MEMORY;
4323 _dbus_list_foreach (&filter_list_copy,
4324 (DBusForeachFunction)_dbus_message_filter_ref,
4327 /* We're still protected from dispatch() reentrancy here
4328 * since we acquired the dispatcher
4330 CONNECTION_UNLOCK (connection);
4332 link = _dbus_list_get_first_link (&filter_list_copy);
4333 while (link != NULL)
4335 DBusMessageFilter *filter = link->data;
4336 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
4338 if (filter->function == NULL)
4340 _dbus_verbose (" filter was removed in a callback function\n");
4345 _dbus_verbose (" running filter on message %p\n", message);
4346 result = (* filter->function) (connection, message, filter->user_data);
4348 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4354 _dbus_list_foreach (&filter_list_copy,
4355 (DBusForeachFunction)_dbus_message_filter_unref,
4357 _dbus_list_clear (&filter_list_copy);
4359 CONNECTION_LOCK (connection);
4361 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4363 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
4366 else if (result == DBUS_HANDLER_RESULT_HANDLED)
4368 _dbus_verbose ("filter handled message in dispatch\n");
4372 /* We're still protected from dispatch() reentrancy here
4373 * since we acquired the dispatcher
4375 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
4377 dbus_message_get_type (message),
4378 dbus_message_get_interface (message) ?
4379 dbus_message_get_interface (message) :
4381 dbus_message_get_member (message) ?
4382 dbus_message_get_member (message) :
4384 dbus_message_get_signature (message));
4386 HAVE_LOCK_CHECK (connection);
4387 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4390 CONNECTION_LOCK (connection);
4392 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4394 _dbus_verbose ("object tree handled message in dispatch\n");
4398 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
4402 DBusPreallocatedSend *preallocated;
4404 _dbus_verbose (" sending error %s\n",
4405 DBUS_ERROR_UNKNOWN_METHOD);
4407 if (!_dbus_string_init (&str))
4409 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4410 _dbus_verbose ("no memory for error string in dispatch\n");
4414 if (!_dbus_string_append_printf (&str,
4415 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
4416 dbus_message_get_member (message),
4417 dbus_message_get_signature (message),
4418 dbus_message_get_interface (message)))
4420 _dbus_string_free (&str);
4421 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4422 _dbus_verbose ("no memory for error string in dispatch\n");
4426 reply = dbus_message_new_error (message,
4427 DBUS_ERROR_UNKNOWN_METHOD,
4428 _dbus_string_get_const_data (&str));
4429 _dbus_string_free (&str);
4433 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4434 _dbus_verbose ("no memory for error reply in dispatch\n");
4438 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4440 if (preallocated == NULL)
4442 dbus_message_unref (reply);
4443 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4444 _dbus_verbose ("no memory for error send in dispatch\n");
4448 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4451 dbus_message_unref (reply);
4453 result = DBUS_HANDLER_RESULT_HANDLED;
4456 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
4457 dbus_message_get_type (message),
4458 dbus_message_get_interface (message) ?
4459 dbus_message_get_interface (message) :
4461 dbus_message_get_member (message) ?
4462 dbus_message_get_member (message) :
4464 dbus_message_get_signature (message),
4468 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4470 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
4472 /* Put message back, and we'll start over.
4473 * Yes this means handlers must be idempotent if they
4474 * don't return HANDLED; c'est la vie.
4476 _dbus_connection_putback_message_link_unlocked (connection,
4481 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
4483 _dbus_list_free_link (message_link);
4484 dbus_message_unref (message); /* don't want the message to count in max message limits
4485 * in computing dispatch status below
4489 _dbus_connection_release_dispatch (connection);
4490 HAVE_LOCK_CHECK (connection);
4492 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
4493 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4495 /* unlocks and calls user code */
4496 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4498 dbus_connection_unref (connection);
4504 * Sets the watch functions for the connection. These functions are
4505 * responsible for making the application's main loop aware of file
4506 * descriptors that need to be monitored for events, using select() or
4507 * poll(). When using Qt, typically the DBusAddWatchFunction would
4508 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
4509 * could call g_io_add_watch(), or could be used as part of a more
4510 * elaborate GSource. Note that when a watch is added, it may
4513 * The DBusWatchToggledFunction notifies the application that the
4514 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
4515 * to check this. A disabled watch should have no effect, and enabled
4516 * watch should be added to the main loop. This feature is used
4517 * instead of simply adding/removing the watch because
4518 * enabling/disabling can be done without memory allocation. The
4519 * toggled function may be NULL if a main loop re-queries
4520 * dbus_watch_get_enabled() every time anyway.
4522 * The DBusWatch can be queried for the file descriptor to watch using
4523 * dbus_watch_get_unix_fd() or dbus_watch_get_socket(), and for the
4524 * events to watch for using dbus_watch_get_flags(). The flags
4525 * returned by dbus_watch_get_flags() will only contain
4526 * DBUS_WATCH_READABLE and DBUS_WATCH_WRITABLE, never
4527 * DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; all watches implicitly
4528 * include a watch for hangups, errors, and other exceptional
4531 * Once a file descriptor becomes readable or writable, or an exception
4532 * occurs, dbus_watch_handle() should be called to
4533 * notify the connection of the file descriptor's condition.
4535 * dbus_watch_handle() cannot be called during the
4536 * DBusAddWatchFunction, as the connection will not be ready to handle
4539 * It is not allowed to reference a DBusWatch after it has been passed
4540 * to remove_function.
4542 * If #FALSE is returned due to lack of memory, the failure may be due
4543 * to a #FALSE return from the new add_function. If so, the
4544 * add_function may have been called successfully one or more times,
4545 * but the remove_function will also have been called to remove any
4546 * successful adds. i.e. if #FALSE is returned the net result
4547 * should be that dbus_connection_set_watch_functions() has no effect,
4548 * but the add_function and remove_function may have been called.
4550 * @todo We need to drop the lock when we call the
4551 * add/remove/toggled functions which can be a side effect
4552 * of setting the watch functions.
4554 * @param connection the connection.
4555 * @param add_function function to begin monitoring a new descriptor.
4556 * @param remove_function function to stop monitoring a descriptor.
4557 * @param toggled_function function to notify of enable/disable
4558 * @param data data to pass to add_function and remove_function.
4559 * @param free_data_function function to be called to free the data.
4560 * @returns #FALSE on failure (no memory)
4563 dbus_connection_set_watch_functions (DBusConnection *connection,
4564 DBusAddWatchFunction add_function,
4565 DBusRemoveWatchFunction remove_function,
4566 DBusWatchToggledFunction toggled_function,
4568 DBusFreeFunction free_data_function)
4571 DBusWatchList *watches;
4573 _dbus_return_val_if_fail (connection != NULL, FALSE);
4575 CONNECTION_LOCK (connection);
4577 #ifndef DBUS_DISABLE_CHECKS
4578 if (connection->watches == NULL)
4580 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4581 _DBUS_FUNCTION_NAME);
4586 /* ref connection for slightly better reentrancy */
4587 _dbus_connection_ref_unlocked (connection);
4589 /* This can call back into user code, and we need to drop the
4590 * connection lock when it does. This is kind of a lame
4593 watches = connection->watches;
4594 connection->watches = NULL;
4595 CONNECTION_UNLOCK (connection);
4597 retval = _dbus_watch_list_set_functions (watches,
4598 add_function, remove_function,
4600 data, free_data_function);
4601 CONNECTION_LOCK (connection);
4602 connection->watches = watches;
4604 CONNECTION_UNLOCK (connection);
4605 /* drop our paranoid refcount */
4606 dbus_connection_unref (connection);
4612 * Sets the timeout functions for the connection. These functions are
4613 * responsible for making the application's main loop aware of timeouts.
4614 * When using Qt, typically the DBusAddTimeoutFunction would create a
4615 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
4618 * The DBusTimeoutToggledFunction notifies the application that the
4619 * timeout has been enabled or disabled. Call
4620 * dbus_timeout_get_enabled() to check this. A disabled timeout should
4621 * have no effect, and enabled timeout should be added to the main
4622 * loop. This feature is used instead of simply adding/removing the
4623 * timeout because enabling/disabling can be done without memory
4624 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
4625 * to enable and disable. The toggled function may be NULL if a main
4626 * loop re-queries dbus_timeout_get_enabled() every time anyway.
4627 * Whenever a timeout is toggled, its interval may change.
4629 * The DBusTimeout can be queried for the timer interval using
4630 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
4631 * repeatedly, each time the interval elapses, starting after it has
4632 * elapsed once. The timeout stops firing when it is removed with the
4633 * given remove_function. The timer interval may change whenever the
4634 * timeout is added, removed, or toggled.
4636 * @param connection the connection.
4637 * @param add_function function to add a timeout.
4638 * @param remove_function function to remove a timeout.
4639 * @param toggled_function function to notify of enable/disable
4640 * @param data data to pass to add_function and remove_function.
4641 * @param free_data_function function to be called to free the data.
4642 * @returns #FALSE on failure (no memory)
4645 dbus_connection_set_timeout_functions (DBusConnection *connection,
4646 DBusAddTimeoutFunction add_function,
4647 DBusRemoveTimeoutFunction remove_function,
4648 DBusTimeoutToggledFunction toggled_function,
4650 DBusFreeFunction free_data_function)
4653 DBusTimeoutList *timeouts;
4655 _dbus_return_val_if_fail (connection != NULL, FALSE);
4657 CONNECTION_LOCK (connection);
4659 #ifndef DBUS_DISABLE_CHECKS
4660 if (connection->timeouts == NULL)
4662 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4663 _DBUS_FUNCTION_NAME);
4668 /* ref connection for slightly better reentrancy */
4669 _dbus_connection_ref_unlocked (connection);
4671 timeouts = connection->timeouts;
4672 connection->timeouts = NULL;
4673 CONNECTION_UNLOCK (connection);
4675 retval = _dbus_timeout_list_set_functions (timeouts,
4676 add_function, remove_function,
4678 data, free_data_function);
4679 CONNECTION_LOCK (connection);
4680 connection->timeouts = timeouts;
4682 CONNECTION_UNLOCK (connection);
4683 /* drop our paranoid refcount */
4684 dbus_connection_unref (connection);
4690 * Sets the mainloop wakeup function for the connection. This function
4691 * is responsible for waking up the main loop (if its sleeping in
4692 * another thread) when some some change has happened to the
4693 * connection that the mainloop needs to reconsider (e.g. a message
4694 * has been queued for writing). When using Qt, this typically
4695 * results in a call to QEventLoop::wakeUp(). When using GLib, it
4696 * would call g_main_context_wakeup().
4698 * @param connection the connection.
4699 * @param wakeup_main_function function to wake up the mainloop
4700 * @param data data to pass wakeup_main_function
4701 * @param free_data_function function to be called to free the data.
4704 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
4705 DBusWakeupMainFunction wakeup_main_function,
4707 DBusFreeFunction free_data_function)
4710 DBusFreeFunction old_free_data;
4712 _dbus_return_if_fail (connection != NULL);
4714 CONNECTION_LOCK (connection);
4715 old_data = connection->wakeup_main_data;
4716 old_free_data = connection->free_wakeup_main_data;
4718 connection->wakeup_main_function = wakeup_main_function;
4719 connection->wakeup_main_data = data;
4720 connection->free_wakeup_main_data = free_data_function;
4722 CONNECTION_UNLOCK (connection);
4724 /* Callback outside the lock */
4726 (*old_free_data) (old_data);
4730 * Set a function to be invoked when the dispatch status changes.
4731 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
4732 * dbus_connection_dispatch() needs to be called to process incoming
4733 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
4734 * from inside the DBusDispatchStatusFunction. Indeed, almost
4735 * any reentrancy in this function is a bad idea. Instead,
4736 * the DBusDispatchStatusFunction should simply save an indication
4737 * that messages should be dispatched later, when the main loop
4740 * If you don't set a dispatch status function, you have to be sure to
4741 * dispatch on every iteration of your main loop, especially if
4742 * dbus_watch_handle() or dbus_timeout_handle() were called.
4744 * @param connection the connection
4745 * @param function function to call on dispatch status changes
4746 * @param data data for function
4747 * @param free_data_function free the function data
4750 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
4751 DBusDispatchStatusFunction function,
4753 DBusFreeFunction free_data_function)
4756 DBusFreeFunction old_free_data;
4758 _dbus_return_if_fail (connection != NULL);
4760 CONNECTION_LOCK (connection);
4761 old_data = connection->dispatch_status_data;
4762 old_free_data = connection->free_dispatch_status_data;
4764 connection->dispatch_status_function = function;
4765 connection->dispatch_status_data = data;
4766 connection->free_dispatch_status_data = free_data_function;
4768 CONNECTION_UNLOCK (connection);
4770 /* Callback outside the lock */
4772 (*old_free_data) (old_data);
4776 * Get the UNIX file descriptor of the connection, if any. This can
4777 * be used for SELinux access control checks with getpeercon() for
4778 * example. DO NOT read or write to the file descriptor, or try to
4779 * select() on it; use DBusWatch for main loop integration. Not all
4780 * connections will have a file descriptor. So for adding descriptors
4781 * to the main loop, use dbus_watch_get_unix_fd() and so forth.
4783 * If the connection is socket-based, you can also use
4784 * dbus_connection_get_socket(), which will work on Windows too.
4785 * This function always fails on Windows.
4787 * Right now the returned descriptor is always a socket, but
4788 * that is not guaranteed.
4790 * @param connection the connection
4791 * @param fd return location for the file descriptor.
4792 * @returns #TRUE if fd is successfully obtained.
4795 dbus_connection_get_unix_fd (DBusConnection *connection,
4798 _dbus_return_val_if_fail (connection != NULL, FALSE);
4799 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4802 /* FIXME do this on a lower level */
4806 return dbus_connection_get_socket(connection, fd);
4810 * Gets the underlying Windows or UNIX socket file descriptor
4811 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
4812 * select() on it; use DBusWatch for main loop integration. Not all
4813 * connections will have a socket. So for adding descriptors
4814 * to the main loop, use dbus_watch_get_socket() and so forth.
4816 * If the connection is not socket-based, this function will return FALSE,
4817 * even if the connection does have a file descriptor of some kind.
4818 * i.e. this function always returns specifically a socket file descriptor.
4820 * @param connection the connection
4821 * @param fd return location for the file descriptor.
4822 * @returns #TRUE if fd is successfully obtained.
4825 dbus_connection_get_socket(DBusConnection *connection,
4830 _dbus_return_val_if_fail (connection != NULL, FALSE);
4831 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4833 CONNECTION_LOCK (connection);
4835 retval = _dbus_transport_get_socket_fd (connection->transport,
4838 CONNECTION_UNLOCK (connection);
4845 * Gets the UNIX user ID of the connection if known. Returns #TRUE if
4846 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
4847 * for now, though in theory someone could hook Windows to NIS or
4848 * something. Always returns #FALSE prior to authenticating the
4851 * The UID is only read by servers from clients; clients can't usually
4852 * get the UID of servers, because servers do not authenticate to
4853 * clients. The returned UID is the UID the connection authenticated
4856 * The message bus is a server and the apps connecting to the bus
4859 * You can ask the bus to tell you the UID of another connection though
4860 * if you like; this is done with dbus_bus_get_unix_user().
4862 * @param connection the connection
4863 * @param uid return location for the user ID
4864 * @returns #TRUE if uid is filled in with a valid user ID
4867 dbus_connection_get_unix_user (DBusConnection *connection,
4872 _dbus_return_val_if_fail (connection != NULL, FALSE);
4873 _dbus_return_val_if_fail (uid != NULL, FALSE);
4875 CONNECTION_LOCK (connection);
4877 if (!_dbus_transport_get_is_authenticated (connection->transport))
4880 result = _dbus_transport_get_unix_user (connection->transport,
4884 _dbus_assert (!result);
4887 CONNECTION_UNLOCK (connection);
4893 * Gets the process ID of the connection if any.
4894 * Returns #TRUE if the pid is filled in.
4895 * Always returns #FALSE prior to authenticating the
4898 * @param connection the connection
4899 * @param pid return location for the process ID
4900 * @returns #TRUE if uid is filled in with a valid process ID
4903 dbus_connection_get_unix_process_id (DBusConnection *connection,
4908 _dbus_return_val_if_fail (connection != NULL, FALSE);
4909 _dbus_return_val_if_fail (pid != NULL, FALSE);
4911 CONNECTION_LOCK (connection);
4913 if (!_dbus_transport_get_is_authenticated (connection->transport))
4916 result = _dbus_transport_get_unix_process_id (connection->transport,
4919 _dbus_assert (!result);
4922 CONNECTION_UNLOCK (connection);
4928 * Sets a predicate function used to determine whether a given user ID
4929 * is allowed to connect. When an incoming connection has
4930 * authenticated with a particular user ID, this function is called;
4931 * if it returns #TRUE, the connection is allowed to proceed,
4932 * otherwise the connection is disconnected.
4934 * If the function is set to #NULL (as it is by default), then
4935 * only the same UID as the server process will be allowed to
4936 * connect. Also, root is always allowed to connect.
4938 * On Windows, the function will be set and its free_data_function will
4939 * be invoked when the connection is freed or a new function is set.
4940 * However, the function will never be called, because there are
4941 * no UNIX user ids to pass to it, or at least none of the existing
4942 * auth protocols would allow authenticating as a UNIX user on Windows.
4944 * @param connection the connection
4945 * @param function the predicate
4946 * @param data data to pass to the predicate
4947 * @param free_data_function function to free the data
4950 dbus_connection_set_unix_user_function (DBusConnection *connection,
4951 DBusAllowUnixUserFunction function,
4953 DBusFreeFunction free_data_function)
4955 void *old_data = NULL;
4956 DBusFreeFunction old_free_function = NULL;
4958 _dbus_return_if_fail (connection != NULL);
4960 CONNECTION_LOCK (connection);
4961 _dbus_transport_set_unix_user_function (connection->transport,
4962 function, data, free_data_function,
4963 &old_data, &old_free_function);
4964 CONNECTION_UNLOCK (connection);
4966 if (old_free_function != NULL)
4967 (* old_free_function) (old_data);
4971 * Gets the Windows user SID of the connection if known. Returns
4972 * #TRUE if the ID is filled in. Always returns #FALSE on non-Windows
4973 * platforms for now, though in theory someone could hook UNIX to
4974 * Active Directory or something. Always returns #FALSE prior to
4975 * authenticating the connection.
4977 * The user is only read by servers from clients; clients can't usually
4978 * get the user of servers, because servers do not authenticate to
4979 * clients. The returned user is the user the connection authenticated
4982 * The message bus is a server and the apps connecting to the bus
4985 * The returned user string has to be freed with dbus_free().
4987 * The return value indicates whether the user SID is available;
4988 * if it's available but we don't have the memory to copy it,
4989 * then the return value is #TRUE and #NULL is given as the SID.
4991 * @todo We would like to be able to say "You can ask the bus to tell
4992 * you the user of another connection though if you like; this is done
4993 * with dbus_bus_get_windows_user()." But this has to be implemented
4994 * in bus/driver.c and dbus/dbus-bus.c, and is pointless anyway
4995 * since on Windows we only use the session bus for now.
4997 * @param connection the connection
4998 * @param windows_sid_p return location for an allocated copy of the user ID, or #NULL if no memory
4999 * @returns #TRUE if user is available (returned value may be #NULL anyway if no memory)
5002 dbus_connection_get_windows_user (DBusConnection *connection,
5003 char **windows_sid_p)
5007 _dbus_return_val_if_fail (connection != NULL, FALSE);
5008 _dbus_return_val_if_fail (windows_sid_p != NULL, FALSE);
5010 CONNECTION_LOCK (connection);
5012 if (!_dbus_transport_get_is_authenticated (connection->transport))
5015 result = _dbus_transport_get_windows_user (connection->transport,
5019 _dbus_assert (!result);
5022 CONNECTION_UNLOCK (connection);
5028 * Sets a predicate function used to determine whether a given user ID
5029 * is allowed to connect. When an incoming connection has
5030 * authenticated with a particular user ID, this function is called;
5031 * if it returns #TRUE, the connection is allowed to proceed,
5032 * otherwise the connection is disconnected.
5034 * If the function is set to #NULL (as it is by default), then
5035 * only the same user owning the server process will be allowed to
5038 * On UNIX, the function will be set and its free_data_function will
5039 * be invoked when the connection is freed or a new function is set.
5040 * However, the function will never be called, because there is no
5041 * way right now to authenticate as a Windows user on UNIX.
5043 * @param connection the connection
5044 * @param function the predicate
5045 * @param data data to pass to the predicate
5046 * @param free_data_function function to free the data
5049 dbus_connection_set_windows_user_function (DBusConnection *connection,
5050 DBusAllowWindowsUserFunction function,
5052 DBusFreeFunction free_data_function)
5054 void *old_data = NULL;
5055 DBusFreeFunction old_free_function = NULL;
5057 _dbus_return_if_fail (connection != NULL);
5059 CONNECTION_LOCK (connection);
5060 _dbus_transport_set_windows_user_function (connection->transport,
5061 function, data, free_data_function,
5062 &old_data, &old_free_function);
5063 CONNECTION_UNLOCK (connection);
5065 if (old_free_function != NULL)
5066 (* old_free_function) (old_data);
5070 * This function must be called on the server side of a connection when the
5071 * connection is first seen in the #DBusNewConnectionFunction. If set to
5072 * #TRUE (the default is #FALSE), then the connection can proceed even if
5073 * the client does not authenticate as some user identity, i.e. clients
5074 * can connect anonymously.
5076 * This setting interacts with the available authorization mechanisms
5077 * (see dbus_server_set_auth_mechanisms()). Namely, an auth mechanism
5078 * such as ANONYMOUS that supports anonymous auth must be included in
5079 * the list of available mechanisms for anonymous login to work.
5081 * This setting also changes the default rule for connections
5082 * authorized as a user; normally, if a connection authorizes as
5083 * a user identity, it is permitted if the user identity is
5084 * root or the user identity matches the user identity of the server
5085 * process. If anonymous connections are allowed, however,
5086 * then any user identity is allowed.
5088 * You can override the rules for connections authorized as a
5089 * user identity with dbus_connection_set_unix_user_function()
5090 * and dbus_connection_set_windows_user_function().
5092 * @param connection the connection
5093 * @param value whether to allow authentication as an anonymous user
5096 dbus_connection_set_allow_anonymous (DBusConnection *connection,
5099 _dbus_return_if_fail (connection != NULL);
5101 CONNECTION_LOCK (connection);
5102 _dbus_transport_set_allow_anonymous (connection->transport, value);
5103 CONNECTION_UNLOCK (connection);
5108 * Normally #DBusConnection automatically handles all messages to the
5109 * org.freedesktop.DBus.Peer interface. However, the message bus wants
5110 * to be able to route methods on that interface through the bus and
5111 * to other applications. If routing peer messages is enabled, then
5112 * messages with the org.freedesktop.DBus.Peer interface that also
5113 * have a bus destination name set will not be automatically
5114 * handled by the #DBusConnection and instead will be dispatched
5115 * normally to the application.
5117 * If a normal application sets this flag, it can break things badly.
5118 * So don't set this unless you are the message bus.
5120 * @param connection the connection
5121 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
5124 dbus_connection_set_route_peer_messages (DBusConnection *connection,
5127 _dbus_return_if_fail (connection != NULL);
5129 CONNECTION_LOCK (connection);
5130 connection->route_peer_messages = TRUE;
5131 CONNECTION_UNLOCK (connection);
5135 * Adds a message filter. Filters are handlers that are run on all
5136 * incoming messages, prior to the objects registered with
5137 * dbus_connection_register_object_path(). Filters are run in the
5138 * order that they were added. The same handler can be added as a
5139 * filter more than once, in which case it will be run more than once.
5140 * Filters added during a filter callback won't be run on the message
5143 * @todo we don't run filters on messages while blocking without
5144 * entering the main loop, since filters are run as part of
5145 * dbus_connection_dispatch(). This is probably a feature, as filters
5146 * could create arbitrary reentrancy. But kind of sucks if you're
5147 * trying to filter METHOD_RETURN for some reason.
5149 * @param connection the connection
5150 * @param function function to handle messages
5151 * @param user_data user data to pass to the function
5152 * @param free_data_function function to use for freeing user data
5153 * @returns #TRUE on success, #FALSE if not enough memory.
5156 dbus_connection_add_filter (DBusConnection *connection,
5157 DBusHandleMessageFunction function,
5159 DBusFreeFunction free_data_function)
5161 DBusMessageFilter *filter;
5163 _dbus_return_val_if_fail (connection != NULL, FALSE);
5164 _dbus_return_val_if_fail (function != NULL, FALSE);
5166 filter = dbus_new0 (DBusMessageFilter, 1);
5170 filter->refcount.value = 1;
5172 CONNECTION_LOCK (connection);
5174 if (!_dbus_list_append (&connection->filter_list,
5177 _dbus_message_filter_unref (filter);
5178 CONNECTION_UNLOCK (connection);
5182 /* Fill in filter after all memory allocated,
5183 * so we don't run the free_user_data_function
5184 * if the add_filter() fails
5187 filter->function = function;
5188 filter->user_data = user_data;
5189 filter->free_user_data_function = free_data_function;
5191 CONNECTION_UNLOCK (connection);
5196 * Removes a previously-added message filter. It is a programming
5197 * error to call this function for a handler that has not been added
5198 * as a filter. If the given handler was added more than once, only
5199 * one instance of it will be removed (the most recently-added
5202 * @param connection the connection
5203 * @param function the handler to remove
5204 * @param user_data user data for the handler to remove
5208 dbus_connection_remove_filter (DBusConnection *connection,
5209 DBusHandleMessageFunction function,
5213 DBusMessageFilter *filter;
5215 _dbus_return_if_fail (connection != NULL);
5216 _dbus_return_if_fail (function != NULL);
5218 CONNECTION_LOCK (connection);
5222 link = _dbus_list_get_last_link (&connection->filter_list);
5223 while (link != NULL)
5225 filter = link->data;
5227 if (filter->function == function &&
5228 filter->user_data == user_data)
5230 _dbus_list_remove_link (&connection->filter_list, link);
5231 filter->function = NULL;
5236 link = _dbus_list_get_prev_link (&connection->filter_list, link);
5239 CONNECTION_UNLOCK (connection);
5241 #ifndef DBUS_DISABLE_CHECKS
5244 _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
5245 function, user_data);
5250 /* Call application code */
5251 if (filter->free_user_data_function)
5252 (* filter->free_user_data_function) (filter->user_data);
5254 filter->free_user_data_function = NULL;
5255 filter->user_data = NULL;
5257 _dbus_message_filter_unref (filter);
5261 * Registers a handler for a given path in the object hierarchy.
5262 * The given vtable handles messages sent to exactly the given path.
5265 * @param connection the connection
5266 * @param path a '/' delimited string of path elements
5267 * @param vtable the virtual table
5268 * @param user_data data to pass to functions in the vtable
5269 * @returns #FALSE if not enough memory
5272 dbus_connection_register_object_path (DBusConnection *connection,
5274 const DBusObjectPathVTable *vtable,
5277 char **decomposed_path;
5280 _dbus_return_val_if_fail (connection != NULL, FALSE);
5281 _dbus_return_val_if_fail (path != NULL, FALSE);
5282 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5283 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5285 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5288 CONNECTION_LOCK (connection);
5290 retval = _dbus_object_tree_register (connection->objects,
5292 (const char **) decomposed_path, vtable,
5295 CONNECTION_UNLOCK (connection);
5297 dbus_free_string_array (decomposed_path);
5303 * Registers a fallback handler for a given subsection of the object
5304 * hierarchy. The given vtable handles messages at or below the given
5305 * path. You can use this to establish a default message handling
5306 * policy for a whole "subdirectory."
5308 * @param connection the connection
5309 * @param path a '/' delimited string of path elements
5310 * @param vtable the virtual table
5311 * @param user_data data to pass to functions in the vtable
5312 * @returns #FALSE if not enough memory
5315 dbus_connection_register_fallback (DBusConnection *connection,
5317 const DBusObjectPathVTable *vtable,
5320 char **decomposed_path;
5323 _dbus_return_val_if_fail (connection != NULL, FALSE);
5324 _dbus_return_val_if_fail (path != NULL, FALSE);
5325 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5326 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5328 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5331 CONNECTION_LOCK (connection);
5333 retval = _dbus_object_tree_register (connection->objects,
5335 (const char **) decomposed_path, vtable,
5338 CONNECTION_UNLOCK (connection);
5340 dbus_free_string_array (decomposed_path);
5346 * Unregisters the handler registered with exactly the given path.
5347 * It's a bug to call this function for a path that isn't registered.
5348 * Can unregister both fallback paths and object paths.
5350 * @param connection the connection
5351 * @param path a '/' delimited string of path elements
5352 * @returns #FALSE if not enough memory
5355 dbus_connection_unregister_object_path (DBusConnection *connection,
5358 char **decomposed_path;
5360 _dbus_return_val_if_fail (connection != NULL, FALSE);
5361 _dbus_return_val_if_fail (path != NULL, FALSE);
5362 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5364 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5367 CONNECTION_LOCK (connection);
5369 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
5371 dbus_free_string_array (decomposed_path);
5377 * Gets the user data passed to dbus_connection_register_object_path()
5378 * or dbus_connection_register_fallback(). If nothing was registered
5379 * at this path, the data is filled in with #NULL.
5381 * @param connection the connection
5382 * @param path the path you registered with
5383 * @param data_p location to store the user data, or #NULL
5384 * @returns #FALSE if not enough memory
5387 dbus_connection_get_object_path_data (DBusConnection *connection,
5391 char **decomposed_path;
5393 _dbus_return_val_if_fail (connection != NULL, FALSE);
5394 _dbus_return_val_if_fail (path != NULL, FALSE);
5395 _dbus_return_val_if_fail (data_p != NULL, FALSE);
5399 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5402 CONNECTION_LOCK (connection);
5404 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
5406 CONNECTION_UNLOCK (connection);
5408 dbus_free_string_array (decomposed_path);
5414 * Lists the registered fallback handlers and object path handlers at
5415 * the given parent_path. The returned array should be freed with
5416 * dbus_free_string_array().
5418 * @param connection the connection
5419 * @param parent_path the path to list the child handlers of
5420 * @param child_entries returns #NULL-terminated array of children
5421 * @returns #FALSE if no memory to allocate the child entries
5424 dbus_connection_list_registered (DBusConnection *connection,
5425 const char *parent_path,
5426 char ***child_entries)
5428 char **decomposed_path;
5430 _dbus_return_val_if_fail (connection != NULL, FALSE);
5431 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
5432 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
5433 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
5435 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
5438 CONNECTION_LOCK (connection);
5440 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
5441 (const char **) decomposed_path,
5443 dbus_free_string_array (decomposed_path);
5448 static DBusDataSlotAllocator slot_allocator;
5449 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
5452 * Allocates an integer ID to be used for storing application-specific
5453 * data on any DBusConnection. The allocated ID may then be used
5454 * with dbus_connection_set_data() and dbus_connection_get_data().
5455 * The passed-in slot must be initialized to -1, and is filled in
5456 * with the slot ID. If the passed-in slot is not -1, it's assumed
5457 * to be already allocated, and its refcount is incremented.
5459 * The allocated slot is global, i.e. all DBusConnection objects will
5460 * have a slot with the given integer ID reserved.
5462 * @param slot_p address of a global variable storing the slot
5463 * @returns #FALSE on failure (no memory)
5466 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
5468 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5469 &_DBUS_LOCK_NAME (connection_slots),
5474 * Deallocates a global ID for connection data slots.
5475 * dbus_connection_get_data() and dbus_connection_set_data() may no
5476 * longer be used with this slot. Existing data stored on existing
5477 * DBusConnection objects will be freed when the connection is
5478 * finalized, but may not be retrieved (and may only be replaced if
5479 * someone else reallocates the slot). When the refcount on the
5480 * passed-in slot reaches 0, it is set to -1.
5482 * @param slot_p address storing the slot to deallocate
5485 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
5487 _dbus_return_if_fail (*slot_p >= 0);
5489 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5493 * Stores a pointer on a DBusConnection, along
5494 * with an optional function to be used for freeing
5495 * the data when the data is set again, or when
5496 * the connection is finalized. The slot number
5497 * must have been allocated with dbus_connection_allocate_data_slot().
5499 * @param connection the connection
5500 * @param slot the slot number
5501 * @param data the data to store
5502 * @param free_data_func finalizer function for the data
5503 * @returns #TRUE if there was enough memory to store the data
5506 dbus_connection_set_data (DBusConnection *connection,
5509 DBusFreeFunction free_data_func)
5511 DBusFreeFunction old_free_func;
5515 _dbus_return_val_if_fail (connection != NULL, FALSE);
5516 _dbus_return_val_if_fail (slot >= 0, FALSE);
5518 CONNECTION_LOCK (connection);
5520 retval = _dbus_data_slot_list_set (&slot_allocator,
5521 &connection->slot_list,
5522 slot, data, free_data_func,
5523 &old_free_func, &old_data);
5525 CONNECTION_UNLOCK (connection);
5529 /* Do the actual free outside the connection lock */
5531 (* old_free_func) (old_data);
5538 * Retrieves data previously set with dbus_connection_set_data().
5539 * The slot must still be allocated (must not have been freed).
5541 * @param connection the connection
5542 * @param slot the slot to get data from
5543 * @returns the data, or #NULL if not found
5546 dbus_connection_get_data (DBusConnection *connection,
5551 _dbus_return_val_if_fail (connection != NULL, NULL);
5553 CONNECTION_LOCK (connection);
5555 res = _dbus_data_slot_list_get (&slot_allocator,
5556 &connection->slot_list,
5559 CONNECTION_UNLOCK (connection);
5565 * This function sets a global flag for whether dbus_connection_new()
5566 * will set SIGPIPE behavior to SIG_IGN.
5568 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
5571 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
5573 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
5577 * Specifies the maximum size message this connection is allowed to
5578 * receive. Larger messages will result in disconnecting the
5581 * @param connection a #DBusConnection
5582 * @param size maximum message size the connection can receive, in bytes
5585 dbus_connection_set_max_message_size (DBusConnection *connection,
5588 _dbus_return_if_fail (connection != NULL);
5590 CONNECTION_LOCK (connection);
5591 _dbus_transport_set_max_message_size (connection->transport,
5593 CONNECTION_UNLOCK (connection);
5597 * Gets the value set by dbus_connection_set_max_message_size().
5599 * @param connection the connection
5600 * @returns the max size of a single message
5603 dbus_connection_get_max_message_size (DBusConnection *connection)
5607 _dbus_return_val_if_fail (connection != NULL, 0);
5609 CONNECTION_LOCK (connection);
5610 res = _dbus_transport_get_max_message_size (connection->transport);
5611 CONNECTION_UNLOCK (connection);
5616 * Sets the maximum total number of bytes that can be used for all messages
5617 * received on this connection. Messages count toward the maximum until
5618 * they are finalized. When the maximum is reached, the connection will
5619 * not read more data until some messages are finalized.
5621 * The semantics of the maximum are: if outstanding messages are
5622 * already above the maximum, additional messages will not be read.
5623 * The semantics are not: if the next message would cause us to exceed
5624 * the maximum, we don't read it. The reason is that we don't know the
5625 * size of a message until after we read it.
5627 * Thus, the max live messages size can actually be exceeded
5628 * by up to the maximum size of a single message.
5630 * Also, if we read say 1024 bytes off the wire in a single read(),
5631 * and that contains a half-dozen small messages, we may exceed the
5632 * size max by that amount. But this should be inconsequential.
5634 * This does imply that we can't call read() with a buffer larger
5635 * than we're willing to exceed this limit by.
5637 * @param connection the connection
5638 * @param size the maximum size in bytes of all outstanding messages
5641 dbus_connection_set_max_received_size (DBusConnection *connection,
5644 _dbus_return_if_fail (connection != NULL);
5646 CONNECTION_LOCK (connection);
5647 _dbus_transport_set_max_received_size (connection->transport,
5649 CONNECTION_UNLOCK (connection);
5653 * Gets the value set by dbus_connection_set_max_received_size().
5655 * @param connection the connection
5656 * @returns the max size of all live messages
5659 dbus_connection_get_max_received_size (DBusConnection *connection)
5663 _dbus_return_val_if_fail (connection != NULL, 0);
5665 CONNECTION_LOCK (connection);
5666 res = _dbus_transport_get_max_received_size (connection->transport);
5667 CONNECTION_UNLOCK (connection);
5672 * Gets the approximate size in bytes of all messages in the outgoing
5673 * message queue. The size is approximate in that you shouldn't use
5674 * it to decide how many bytes to read off the network or anything
5675 * of that nature, as optimizations may choose to tell small white lies
5676 * to avoid performance overhead.
5678 * @param connection the connection
5679 * @returns the number of bytes that have been queued up but not sent
5682 dbus_connection_get_outgoing_size (DBusConnection *connection)
5686 _dbus_return_val_if_fail (connection != NULL, 0);
5688 CONNECTION_LOCK (connection);
5689 res = _dbus_counter_get_value (connection->outgoing_counter);
5690 CONNECTION_UNLOCK (connection);