1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002, 2003, 2004, 2005 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-list.h"
33 #include "dbus-hash.h"
34 #include "dbus-message-internal.h"
35 #include "dbus-threads.h"
36 #include "dbus-protocol.h"
37 #include "dbus-dataslot.h"
38 #include "dbus-string.h"
39 #include "dbus-pending-call.h"
40 #include "dbus-object-tree.h"
41 #include "dbus-threads-internal.h"
43 #ifdef DBUS_DISABLE_CHECKS
44 #define TOOK_LOCK_CHECK(connection)
45 #define RELEASING_LOCK_CHECK(connection)
46 #define HAVE_LOCK_CHECK(connection)
48 #define TOOK_LOCK_CHECK(connection) do { \
49 _dbus_assert (!(connection)->have_connection_lock); \
50 (connection)->have_connection_lock = TRUE; \
52 #define RELEASING_LOCK_CHECK(connection) do { \
53 _dbus_assert ((connection)->have_connection_lock); \
54 (connection)->have_connection_lock = FALSE; \
56 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
57 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
62 #define CONNECTION_LOCK(connection) do { \
63 if (TRACE_LOCKS) { _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); } \
64 _dbus_mutex_lock ((connection)->mutex); \
65 TOOK_LOCK_CHECK (connection); \
68 #define CONNECTION_UNLOCK(connection) do { \
69 if (TRACE_LOCKS) { _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); } \
70 RELEASING_LOCK_CHECK (connection); \
71 _dbus_mutex_unlock ((connection)->mutex); \
74 #define DISPATCH_STATUS_NAME(s) \
75 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
76 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
77 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
81 * @defgroup DBusConnection DBusConnection
83 * @brief Connection to another application
85 * A DBusConnection represents a connection to another
86 * application. Messages can be sent and received via this connection.
87 * The other application may be a message bus; for convenience, the
88 * function dbus_bus_get() is provided to automatically open a
89 * connection to the well-known message buses.
91 * In brief a DBusConnection is a message queue associated with some
92 * message transport mechanism such as a socket. The connection
93 * maintains a queue of incoming messages and a queue of outgoing
96 * Incoming messages are normally processed by calling
97 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
98 * handlers registered for the topmost message in the message queue,
99 * then discards the message, then returns.
101 * dbus_connection_get_dispatch_status() indicates whether
102 * messages are currently in the queue that need dispatching.
103 * dbus_connection_set_dispatch_status_function() allows
104 * you to set a function to be used to monitor the dispatch status.
106 * If you're using GLib or Qt add-on libraries for D-BUS, there are
107 * special convenience APIs in those libraries that hide
108 * all the details of dispatch and watch/timeout monitoring.
109 * For example, dbus_connection_setup_with_g_main().
111 * If you aren't using these add-on libraries, you have to manually
112 * call dbus_connection_set_dispatch_status_function(),
113 * dbus_connection_set_watch_functions(),
114 * dbus_connection_set_timeout_functions() providing appropriate
115 * functions to integrate the connection with your application's main
118 * When you use dbus_connection_send() or one of its variants to send
119 * a message, the message is added to the outgoing queue. It's
120 * actually written to the network later; either in
121 * dbus_watch_handle() invoked by your main loop, or in
122 * dbus_connection_flush() which blocks until it can write out the
123 * entire outgoing queue. The GLib/Qt add-on libraries again
124 * handle the details here for you by setting up watch functions.
126 * When a connection is disconnected, you are guaranteed to get a
127 * signal "Disconnected" from the interface
128 * #DBUS_INTERFACE_LOCAL, path
131 * You may not drop the last reference to a #DBusConnection
132 * until that connection has been disconnected.
134 * You may dispatch the unprocessed incoming message queue even if the
135 * connection is disconnected. However, "Disconnected" will always be
136 * the last message in the queue (obviously no messages are received
137 * after disconnection).
139 * #DBusConnection has thread locks and drops them when invoking user
140 * callbacks, so in general is transparently threadsafe. However,
141 * #DBusMessage does NOT have thread locks; you must not send the same
142 * message to multiple #DBusConnection that will be used from
147 * @defgroup DBusConnectionInternals DBusConnection implementation details
148 * @ingroup DBusInternals
149 * @brief Implementation details of DBusConnection
155 * Internal struct representing a message filter function
157 typedef struct DBusMessageFilter DBusMessageFilter;
160 * Internal struct representing a message filter function
162 struct DBusMessageFilter
164 DBusAtomic refcount; /**< Reference count */
165 DBusHandleMessageFunction function; /**< Function to call to filter */
166 void *user_data; /**< User data for the function */
167 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
172 * Internals of DBusPreallocatedSend
174 struct DBusPreallocatedSend
176 DBusConnection *connection; /**< Connection we'd send the message to */
177 DBusList *queue_link; /**< Preallocated link in the queue */
178 DBusList *counter_link; /**< Preallocated link in the resource counter */
181 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
184 * Implementation details of DBusConnection. All fields are private.
186 struct DBusConnection
188 DBusAtomic refcount; /**< Reference count. */
190 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
192 DBusMutex *dispatch_mutex; /**< Protects dispatch_acquired */
193 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */
194 DBusMutex *io_path_mutex; /**< Protects io_path_acquired */
195 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */
197 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
198 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
200 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
201 * dispatch_acquired will be set by the borrower
204 int n_outgoing; /**< Length of outgoing queue. */
205 int n_incoming; /**< Length of incoming queue. */
207 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
209 DBusTransport *transport; /**< Object that sends/receives messages over network. */
210 DBusWatchList *watches; /**< Stores active watches. */
211 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
213 DBusList *filter_list; /**< List of filters. */
215 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
217 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
219 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
220 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
222 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
223 void *wakeup_main_data; /**< Application data for wakeup_main_function */
224 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
226 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
227 void *dispatch_status_data; /**< Application data for dispatch_status_function */
228 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
230 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
232 DBusList *link_cache; /**< A cache of linked list links to prevent contention
233 * for the global linked list mempool lock
235 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
237 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
239 unsigned int shareable : 1; /**< #TRUE if connection can go in shared_connections once we know the GUID */
241 unsigned int dispatch_acquired : 1; /**< Someone has dispatch path (can drain incoming queue) */
242 unsigned int io_path_acquired : 1; /**< Someone has transport io path (can use the transport to read/write messages) */
244 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
246 #ifndef DBUS_DISABLE_CHECKS
247 unsigned int have_connection_lock : 1; /**< Used to check locking */
250 #ifndef DBUS_DISABLE_CHECKS
251 int generation; /**< _dbus_current_generation that should correspond to this connection */
255 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
256 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
257 DBusDispatchStatus new_status);
258 static void _dbus_connection_last_unref (DBusConnection *connection);
259 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
260 static void _dbus_connection_release_dispatch (DBusConnection *connection);
262 static DBusMessageFilter *
263 _dbus_message_filter_ref (DBusMessageFilter *filter)
265 _dbus_assert (filter->refcount.value > 0);
266 _dbus_atomic_inc (&filter->refcount);
272 _dbus_message_filter_unref (DBusMessageFilter *filter)
274 _dbus_assert (filter->refcount.value > 0);
276 if (_dbus_atomic_dec (&filter->refcount) == 1)
278 if (filter->free_user_data_function)
279 (* filter->free_user_data_function) (filter->user_data);
286 * Acquires the connection lock.
288 * @param connection the connection.
291 _dbus_connection_lock (DBusConnection *connection)
293 CONNECTION_LOCK (connection);
297 * Releases the connection lock.
299 * @param connection the connection.
302 _dbus_connection_unlock (DBusConnection *connection)
304 CONNECTION_UNLOCK (connection);
308 * Wakes up the main loop if it is sleeping
309 * Needed if we're e.g. queueing outgoing messages
310 * on a thread while the mainloop sleeps.
312 * @param connection the connection.
315 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
317 if (connection->wakeup_main_function)
318 (*connection->wakeup_main_function) (connection->wakeup_main_data);
321 #ifdef DBUS_BUILD_TESTS
322 /* For now this function isn't used */
324 * Adds a message to the incoming message queue, returning #FALSE
325 * if there's insufficient memory to queue the message.
326 * Does not take over refcount of the message.
328 * @param connection the connection.
329 * @param message the message to queue.
330 * @returns #TRUE on success.
333 _dbus_connection_queue_received_message (DBusConnection *connection,
334 DBusMessage *message)
338 link = _dbus_list_alloc_link (message);
342 dbus_message_ref (message);
343 _dbus_connection_queue_received_message_link (connection, link);
350 * Adds a message-containing list link to the incoming message queue,
351 * taking ownership of the link and the message's current refcount.
352 * Cannot fail due to lack of memory.
354 * @param connection the connection.
355 * @param link the message link to queue.
358 _dbus_connection_queue_received_message_link (DBusConnection *connection,
361 DBusPendingCall *pending;
362 dbus_int32_t reply_serial;
363 DBusMessage *message;
365 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
367 _dbus_list_append_link (&connection->incoming_messages,
369 message = link->data;
371 /* If this is a reply we're waiting on, remove timeout for it */
372 reply_serial = dbus_message_get_reply_serial (message);
373 if (reply_serial != -1)
375 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
379 if (pending->timeout_added)
380 _dbus_connection_remove_timeout (connection,
383 pending->timeout_added = FALSE;
387 connection->n_incoming += 1;
389 _dbus_connection_wakeup_mainloop (connection);
391 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
393 dbus_message_get_type (message),
394 dbus_message_get_path (message),
395 dbus_message_get_interface (message) ?
396 dbus_message_get_interface (message) :
398 dbus_message_get_member (message) ?
399 dbus_message_get_member (message) :
401 dbus_message_get_signature (message),
402 dbus_message_get_reply_serial (message),
404 connection->n_incoming);
408 * Adds a link + message to the incoming message queue.
409 * Can't fail. Takes ownership of both link and message.
411 * @param connection the connection.
412 * @param link the list node and message to queue.
414 * @todo This needs to wake up the mainloop if it is in
415 * a poll/select and this is a multithreaded app.
418 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
421 HAVE_LOCK_CHECK (connection);
423 _dbus_list_append_link (&connection->incoming_messages, link);
425 connection->n_incoming += 1;
427 _dbus_connection_wakeup_mainloop (connection);
429 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
430 link->data, connection, connection->n_incoming);
435 * Checks whether there are messages in the outgoing message queue.
436 * Called with connection lock held.
438 * @param connection the connection.
439 * @returns #TRUE if the outgoing queue is non-empty.
442 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
444 HAVE_LOCK_CHECK (connection);
445 return connection->outgoing_messages != NULL;
449 * Checks whether there are messages in the outgoing message queue.
451 * @param connection the connection.
452 * @returns #TRUE if the outgoing queue is non-empty.
455 dbus_connection_has_messages_to_send (DBusConnection *connection)
459 _dbus_return_val_if_fail (connection != NULL, FALSE);
461 CONNECTION_LOCK (connection);
462 v = _dbus_connection_has_messages_to_send_unlocked (connection);
463 CONNECTION_UNLOCK (connection);
469 * Gets the next outgoing message. The message remains in the
470 * queue, and the caller does not own a reference to it.
472 * @param connection the connection.
473 * @returns the message to be sent.
476 _dbus_connection_get_message_to_send (DBusConnection *connection)
478 HAVE_LOCK_CHECK (connection);
480 return _dbus_list_get_last (&connection->outgoing_messages);
484 * Notifies the connection that a message has been sent, so the
485 * message can be removed from the outgoing queue.
486 * Called with the connection lock held.
488 * @param connection the connection.
489 * @param message the message that was sent.
492 _dbus_connection_message_sent (DBusConnection *connection,
493 DBusMessage *message)
497 HAVE_LOCK_CHECK (connection);
499 /* This can be called before we even complete authentication, since
500 * it's called on disconnect to clean up the outgoing queue.
501 * It's also called as we successfully send each message.
504 link = _dbus_list_get_last_link (&connection->outgoing_messages);
505 _dbus_assert (link != NULL);
506 _dbus_assert (link->data == message);
508 /* Save this link in the link cache */
509 _dbus_list_unlink (&connection->outgoing_messages,
511 _dbus_list_prepend_link (&connection->link_cache, link);
513 connection->n_outgoing -= 1;
515 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
517 dbus_message_get_type (message),
518 dbus_message_get_path (message),
519 dbus_message_get_interface (message) ?
520 dbus_message_get_interface (message) :
522 dbus_message_get_member (message) ?
523 dbus_message_get_member (message) :
525 dbus_message_get_signature (message),
526 connection, connection->n_outgoing);
528 /* Save this link in the link cache also */
529 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
531 _dbus_list_prepend_link (&connection->link_cache, link);
533 dbus_message_unref (message);
536 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
538 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
540 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
542 dbus_bool_t enabled);
545 protected_change_watch (DBusConnection *connection,
547 DBusWatchAddFunction add_function,
548 DBusWatchRemoveFunction remove_function,
549 DBusWatchToggleFunction toggle_function,
552 DBusWatchList *watches;
555 HAVE_LOCK_CHECK (connection);
557 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
558 * drop lock and call out" one; but it has to be propagated up through all callers
561 watches = connection->watches;
564 connection->watches = NULL;
565 _dbus_connection_ref_unlocked (connection);
566 CONNECTION_UNLOCK (connection);
569 retval = (* add_function) (watches, watch);
570 else if (remove_function)
573 (* remove_function) (watches, watch);
578 (* toggle_function) (watches, watch, enabled);
581 CONNECTION_LOCK (connection);
582 connection->watches = watches;
583 _dbus_connection_unref_unlocked (connection);
593 * Adds a watch using the connection's DBusAddWatchFunction if
594 * available. Otherwise records the watch to be added when said
595 * function is available. Also re-adds the watch if the
596 * DBusAddWatchFunction changes. May fail due to lack of memory.
598 * @param connection the connection.
599 * @param watch the watch to add.
600 * @returns #TRUE on success.
603 _dbus_connection_add_watch (DBusConnection *connection,
606 return protected_change_watch (connection, watch,
607 _dbus_watch_list_add_watch,
612 * Removes a watch using the connection's DBusRemoveWatchFunction
613 * if available. It's an error to call this function on a watch
614 * that was not previously added.
616 * @param connection the connection.
617 * @param watch the watch to remove.
620 _dbus_connection_remove_watch (DBusConnection *connection,
623 protected_change_watch (connection, watch,
625 _dbus_watch_list_remove_watch,
630 * Toggles a watch and notifies app via connection's
631 * DBusWatchToggledFunction if available. It's an error to call this
632 * function on a watch that was not previously added.
633 * Connection lock should be held when calling this.
635 * @param connection the connection.
636 * @param watch the watch to toggle.
637 * @param enabled whether to enable or disable
640 _dbus_connection_toggle_watch (DBusConnection *connection,
644 _dbus_assert (watch != NULL);
646 protected_change_watch (connection, watch,
648 _dbus_watch_list_toggle_watch,
652 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
653 DBusTimeout *timeout);
654 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
655 DBusTimeout *timeout);
656 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
657 DBusTimeout *timeout,
658 dbus_bool_t enabled);
661 protected_change_timeout (DBusConnection *connection,
662 DBusTimeout *timeout,
663 DBusTimeoutAddFunction add_function,
664 DBusTimeoutRemoveFunction remove_function,
665 DBusTimeoutToggleFunction toggle_function,
668 DBusTimeoutList *timeouts;
671 HAVE_LOCK_CHECK (connection);
673 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
674 * drop lock and call out" one; but it has to be propagated up through all callers
677 timeouts = connection->timeouts;
680 connection->timeouts = NULL;
681 _dbus_connection_ref_unlocked (connection);
682 CONNECTION_UNLOCK (connection);
685 retval = (* add_function) (timeouts, timeout);
686 else if (remove_function)
689 (* remove_function) (timeouts, timeout);
694 (* toggle_function) (timeouts, timeout, enabled);
697 CONNECTION_LOCK (connection);
698 connection->timeouts = timeouts;
699 _dbus_connection_unref_unlocked (connection);
708 * Adds a timeout using the connection's DBusAddTimeoutFunction if
709 * available. Otherwise records the timeout to be added when said
710 * function is available. Also re-adds the timeout if the
711 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
712 * The timeout will fire repeatedly until removed.
714 * @param connection the connection.
715 * @param timeout the timeout to add.
716 * @returns #TRUE on success.
719 _dbus_connection_add_timeout (DBusConnection *connection,
720 DBusTimeout *timeout)
722 return protected_change_timeout (connection, timeout,
723 _dbus_timeout_list_add_timeout,
728 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
729 * if available. It's an error to call this function on a timeout
730 * that was not previously added.
732 * @param connection the connection.
733 * @param timeout the timeout to remove.
736 _dbus_connection_remove_timeout (DBusConnection *connection,
737 DBusTimeout *timeout)
739 protected_change_timeout (connection, timeout,
741 _dbus_timeout_list_remove_timeout,
746 * Toggles a timeout and notifies app via connection's
747 * DBusTimeoutToggledFunction if available. It's an error to call this
748 * function on a timeout that was not previously added.
750 * @param connection the connection.
751 * @param timeout the timeout to toggle.
752 * @param enabled whether to enable or disable
755 _dbus_connection_toggle_timeout (DBusConnection *connection,
756 DBusTimeout *timeout,
759 protected_change_timeout (connection, timeout,
761 _dbus_timeout_list_toggle_timeout,
766 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
767 DBusPendingCall *pending)
769 HAVE_LOCK_CHECK (connection);
771 _dbus_assert (pending->reply_serial != 0);
773 if (!_dbus_connection_add_timeout (connection, pending->timeout))
776 if (!_dbus_hash_table_insert_int (connection->pending_replies,
777 pending->reply_serial,
780 _dbus_connection_remove_timeout (connection, pending->timeout);
782 HAVE_LOCK_CHECK (connection);
786 pending->timeout_added = TRUE;
787 pending->connection = connection;
789 dbus_pending_call_ref (pending);
791 HAVE_LOCK_CHECK (connection);
797 free_pending_call_on_hash_removal (void *data)
799 DBusPendingCall *pending;
806 if (pending->connection)
808 if (pending->timeout_added)
810 _dbus_connection_remove_timeout (pending->connection,
812 pending->timeout_added = FALSE;
815 pending->connection = NULL;
817 dbus_pending_call_unref (pending);
822 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
823 DBusPendingCall *pending)
825 /* Can't have a destroy notifier on the pending call if we're going to do this */
827 dbus_pending_call_ref (pending);
828 _dbus_hash_table_remove_int (connection->pending_replies,
829 pending->reply_serial);
830 _dbus_assert (pending->connection == NULL);
831 dbus_pending_call_unref (pending);
835 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
836 DBusPendingCall *pending)
838 /* The idea here is to avoid finalizing the pending call
839 * with the lock held, since there's a destroy notifier
840 * in pending call that goes out to application code.
842 dbus_pending_call_ref (pending);
843 _dbus_hash_table_remove_int (connection->pending_replies,
844 pending->reply_serial);
845 _dbus_assert (pending->connection == NULL);
846 CONNECTION_UNLOCK (connection);
847 dbus_pending_call_unref (pending);
851 * Removes a pending call from the connection, such that
852 * the pending reply will be ignored. May drop the last
853 * reference to the pending call.
855 * @param connection the connection
856 * @param pending the pending call
859 _dbus_connection_remove_pending_call (DBusConnection *connection,
860 DBusPendingCall *pending)
862 CONNECTION_LOCK (connection);
863 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
867 * Completes a pending call with the given message,
868 * or if the message is #NULL, by timing out the pending call.
870 * @param pending the pending call
871 * @param message the message to complete the call with, or #NULL
872 * to time out the call
875 _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
876 DBusMessage *message)
880 message = pending->timeout_link->data;
881 _dbus_list_clear (&pending->timeout_link);
884 dbus_message_ref (message);
886 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
888 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
890 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
891 "error" : "other type",
892 pending->reply_serial);
894 _dbus_assert (pending->reply == NULL);
895 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
896 pending->reply = message;
898 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
899 _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending);
901 /* Must be called unlocked since it invokes app callback */
902 _dbus_pending_call_notify (pending);
903 dbus_pending_call_unref (pending);
907 * Acquire the transporter I/O path. This must be done before
908 * doing any I/O in the transporter. May sleep and drop the
909 * IO path mutex while waiting for the I/O path.
911 * @param connection the connection.
912 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
913 * @returns TRUE if the I/O path was acquired.
916 _dbus_connection_acquire_io_path (DBusConnection *connection,
917 int timeout_milliseconds)
919 dbus_bool_t we_acquired;
921 HAVE_LOCK_CHECK (connection);
923 /* We don't want the connection to vanish */
924 _dbus_connection_ref_unlocked (connection);
926 /* We will only touch io_path_acquired which is protected by our mutex */
927 CONNECTION_UNLOCK (connection);
929 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
930 _dbus_mutex_lock (connection->io_path_mutex);
932 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
933 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
937 if (connection->io_path_acquired)
939 if (timeout_milliseconds != -1)
941 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
942 _DBUS_FUNCTION_NAME, timeout_milliseconds);
943 _dbus_condvar_wait_timeout (connection->io_path_cond,
944 connection->io_path_mutex,
945 timeout_milliseconds);
949 while (connection->io_path_acquired)
951 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
952 _dbus_condvar_wait (connection->io_path_cond, connection->io_path_mutex);
957 if (!connection->io_path_acquired)
960 connection->io_path_acquired = TRUE;
963 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
964 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
966 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
967 _dbus_mutex_unlock (connection->io_path_mutex);
969 CONNECTION_LOCK (connection);
971 HAVE_LOCK_CHECK (connection);
973 _dbus_connection_unref_unlocked (connection);
979 * Release the I/O path when you're done with it. Only call
980 * after you've acquired the I/O. Wakes up at most one thread
981 * currently waiting to acquire the I/O path.
983 * @param connection the connection.
986 _dbus_connection_release_io_path (DBusConnection *connection)
988 HAVE_LOCK_CHECK (connection);
990 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
991 _dbus_mutex_lock (connection->io_path_mutex);
993 _dbus_assert (connection->io_path_acquired);
995 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
996 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
998 connection->io_path_acquired = FALSE;
999 _dbus_condvar_wake_one (connection->io_path_cond);
1001 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1002 _dbus_mutex_unlock (connection->io_path_mutex);
1006 * Queues incoming messages and sends outgoing messages for this
1007 * connection, optionally blocking in the process. Each call to
1008 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1009 * time and then read or write data if possible.
1011 * The purpose of this function is to be able to flush outgoing
1012 * messages or queue up incoming messages without returning
1013 * control to the application and causing reentrancy weirdness.
1015 * The flags parameter allows you to specify whether to
1016 * read incoming messages, write outgoing messages, or both,
1017 * and whether to block if no immediate action is possible.
1019 * The timeout_milliseconds parameter does nothing unless the
1020 * iteration is blocking.
1022 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1023 * wasn't specified, then it's impossible to block, even if
1024 * you specify DBUS_ITERATION_BLOCK; in that case the function
1025 * returns immediately.
1027 * Called with connection lock held.
1029 * @param connection the connection.
1030 * @param flags iteration flags.
1031 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1034 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1036 int timeout_milliseconds)
1038 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1040 HAVE_LOCK_CHECK (connection);
1042 if (connection->n_outgoing == 0)
1043 flags &= ~DBUS_ITERATION_DO_WRITING;
1045 if (_dbus_connection_acquire_io_path (connection,
1046 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1048 HAVE_LOCK_CHECK (connection);
1050 _dbus_transport_do_iteration (connection->transport,
1051 flags, timeout_milliseconds);
1052 _dbus_connection_release_io_path (connection);
1055 HAVE_LOCK_CHECK (connection);
1057 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1061 * Creates a new connection for the given transport. A transport
1062 * represents a message stream that uses some concrete mechanism, such
1063 * as UNIX domain sockets. May return #NULL if insufficient
1064 * memory exists to create the connection.
1066 * @param transport the transport.
1067 * @returns the new connection, or #NULL on failure.
1070 _dbus_connection_new_for_transport (DBusTransport *transport)
1072 DBusConnection *connection;
1073 DBusWatchList *watch_list;
1074 DBusTimeoutList *timeout_list;
1075 DBusHashTable *pending_replies;
1077 DBusMutex *io_path_mutex;
1078 DBusMutex *dispatch_mutex;
1079 DBusCondVar *message_returned_cond;
1080 DBusCondVar *dispatch_cond;
1081 DBusCondVar *io_path_cond;
1082 DBusList *disconnect_link;
1083 DBusMessage *disconnect_message;
1084 DBusCounter *outgoing_counter;
1085 DBusObjectTree *objects;
1089 pending_replies = NULL;
1090 timeout_list = NULL;
1092 io_path_mutex = NULL;
1093 dispatch_mutex = NULL;
1094 message_returned_cond = NULL;
1095 dispatch_cond = NULL;
1096 io_path_cond = NULL;
1097 disconnect_link = NULL;
1098 disconnect_message = NULL;
1099 outgoing_counter = NULL;
1102 watch_list = _dbus_watch_list_new ();
1103 if (watch_list == NULL)
1106 timeout_list = _dbus_timeout_list_new ();
1107 if (timeout_list == NULL)
1111 _dbus_hash_table_new (DBUS_HASH_INT,
1113 (DBusFreeFunction)free_pending_call_on_hash_removal);
1114 if (pending_replies == NULL)
1117 connection = dbus_new0 (DBusConnection, 1);
1118 if (connection == NULL)
1121 mutex = _dbus_mutex_new ();
1125 io_path_mutex = _dbus_mutex_new ();
1126 if (io_path_mutex == NULL)
1129 dispatch_mutex = _dbus_mutex_new ();
1130 if (dispatch_mutex == NULL)
1133 message_returned_cond = _dbus_condvar_new ();
1134 if (message_returned_cond == NULL)
1137 dispatch_cond = _dbus_condvar_new ();
1138 if (dispatch_cond == NULL)
1141 io_path_cond = _dbus_condvar_new ();
1142 if (io_path_cond == NULL)
1145 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1146 DBUS_INTERFACE_LOCAL,
1149 if (disconnect_message == NULL)
1152 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1153 if (disconnect_link == NULL)
1156 outgoing_counter = _dbus_counter_new ();
1157 if (outgoing_counter == NULL)
1160 objects = _dbus_object_tree_new (connection);
1161 if (objects == NULL)
1164 if (_dbus_modify_sigpipe)
1165 _dbus_disable_sigpipe ();
1167 connection->refcount.value = 1;
1168 connection->mutex = mutex;
1169 connection->dispatch_cond = dispatch_cond;
1170 connection->dispatch_mutex = dispatch_mutex;
1171 connection->io_path_cond = io_path_cond;
1172 connection->io_path_mutex = io_path_mutex;
1173 connection->transport = transport;
1174 connection->watches = watch_list;
1175 connection->timeouts = timeout_list;
1176 connection->pending_replies = pending_replies;
1177 connection->outgoing_counter = outgoing_counter;
1178 connection->filter_list = NULL;
1179 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1180 connection->objects = objects;
1181 connection->exit_on_disconnect = FALSE;
1182 connection->shareable = FALSE;
1183 #ifndef DBUS_DISABLE_CHECKS
1184 connection->generation = _dbus_current_generation;
1187 _dbus_data_slot_list_init (&connection->slot_list);
1189 connection->client_serial = 1;
1191 connection->disconnect_message_link = disconnect_link;
1193 CONNECTION_LOCK (connection);
1195 if (!_dbus_transport_set_connection (transport, connection))
1198 _dbus_transport_ref (transport);
1200 CONNECTION_UNLOCK (connection);
1205 if (disconnect_message != NULL)
1206 dbus_message_unref (disconnect_message);
1208 if (disconnect_link != NULL)
1209 _dbus_list_free_link (disconnect_link);
1211 if (io_path_cond != NULL)
1212 _dbus_condvar_free (io_path_cond);
1214 if (dispatch_cond != NULL)
1215 _dbus_condvar_free (dispatch_cond);
1217 if (message_returned_cond != NULL)
1218 _dbus_condvar_free (message_returned_cond);
1221 _dbus_mutex_free (mutex);
1223 if (io_path_mutex != NULL)
1224 _dbus_mutex_free (io_path_mutex);
1226 if (dispatch_mutex != NULL)
1227 _dbus_mutex_free (dispatch_mutex);
1229 if (connection != NULL)
1230 dbus_free (connection);
1232 if (pending_replies)
1233 _dbus_hash_table_unref (pending_replies);
1236 _dbus_watch_list_free (watch_list);
1239 _dbus_timeout_list_free (timeout_list);
1241 if (outgoing_counter)
1242 _dbus_counter_unref (outgoing_counter);
1245 _dbus_object_tree_unref (objects);
1251 * Increments the reference count of a DBusConnection.
1252 * Requires that the caller already holds the connection lock.
1254 * @param connection the connection.
1255 * @returns the connection.
1258 _dbus_connection_ref_unlocked (DBusConnection *connection)
1260 _dbus_assert (connection != NULL);
1261 _dbus_assert (connection->generation == _dbus_current_generation);
1263 HAVE_LOCK_CHECK (connection);
1265 #ifdef DBUS_HAVE_ATOMIC_INT
1266 _dbus_atomic_inc (&connection->refcount);
1268 _dbus_assert (connection->refcount.value > 0);
1269 connection->refcount.value += 1;
1276 * Decrements the reference count of a DBusConnection.
1277 * Requires that the caller already holds the connection lock.
1279 * @param connection the connection.
1282 _dbus_connection_unref_unlocked (DBusConnection *connection)
1284 dbus_bool_t last_unref;
1286 HAVE_LOCK_CHECK (connection);
1288 _dbus_assert (connection != NULL);
1290 /* The connection lock is better than the global
1291 * lock in the atomic increment fallback
1294 #ifdef DBUS_HAVE_ATOMIC_INT
1295 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1297 _dbus_assert (connection->refcount.value > 0);
1299 connection->refcount.value -= 1;
1300 last_unref = (connection->refcount.value == 0);
1302 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1307 _dbus_connection_last_unref (connection);
1310 static dbus_uint32_t
1311 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1315 serial = connection->client_serial++;
1317 if (connection->client_serial < 0)
1318 connection->client_serial = 1;
1324 * A callback for use with dbus_watch_new() to create a DBusWatch.
1326 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1327 * and the virtual handle_watch in DBusTransport if we got rid of it.
1328 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1331 * @param watch the watch.
1332 * @param condition the current condition of the file descriptors being watched.
1333 * @param data must be a pointer to a #DBusConnection
1334 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1337 _dbus_connection_handle_watch (DBusWatch *watch,
1338 unsigned int condition,
1341 DBusConnection *connection;
1343 DBusDispatchStatus status;
1347 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1349 CONNECTION_LOCK (connection);
1350 _dbus_connection_acquire_io_path (connection, -1);
1351 HAVE_LOCK_CHECK (connection);
1352 retval = _dbus_transport_handle_watch (connection->transport,
1355 _dbus_connection_release_io_path (connection);
1357 HAVE_LOCK_CHECK (connection);
1359 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1361 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1363 /* this calls out to user code */
1364 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1366 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1371 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1372 static DBusHashTable *shared_connections = NULL;
1375 shared_connections_shutdown (void *data)
1377 _DBUS_LOCK (shared_connections);
1379 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1380 _dbus_hash_table_unref (shared_connections);
1381 shared_connections = NULL;
1383 _DBUS_UNLOCK (shared_connections);
1387 connection_lookup_shared (DBusAddressEntry *entry,
1388 DBusConnection **result)
1390 _dbus_verbose ("checking for existing connection\n");
1394 _DBUS_LOCK (shared_connections);
1396 if (shared_connections == NULL)
1398 _dbus_verbose ("creating shared_connections hash table\n");
1400 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1403 if (shared_connections == NULL)
1405 _DBUS_UNLOCK (shared_connections);
1409 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1411 _dbus_hash_table_unref (shared_connections);
1412 shared_connections = NULL;
1413 _DBUS_UNLOCK (shared_connections);
1417 _dbus_verbose (" successfully created shared_connections\n");
1419 _DBUS_UNLOCK (shared_connections);
1420 return TRUE; /* no point looking up in the hash we just made */
1426 guid = dbus_address_entry_get_value (entry, "guid");
1430 *result = _dbus_hash_table_lookup_string (shared_connections,
1435 /* The DBusConnection can't have been disconnected
1436 * between the lookup and this code, because the
1437 * disconnection will take the shared_connections lock to
1438 * remove the connection. It can't have been finalized
1439 * since you have to disconnect prior to finalize.
1441 * Thus it's safe to ref the connection.
1443 dbus_connection_ref (*result);
1445 _dbus_verbose ("looked up existing connection to server guid %s\n",
1450 _DBUS_UNLOCK (shared_connections);
1456 connection_record_shared_unlocked (DBusConnection *connection,
1460 char *guid_in_connection;
1462 /* A separate copy of the key is required in the hash table, because
1463 * we don't have a lock on the connection when we are doing a hash
1467 _dbus_assert (connection->server_guid == NULL);
1468 _dbus_assert (connection->shareable);
1470 guid_key = _dbus_strdup (guid);
1471 if (guid_key == NULL)
1474 guid_in_connection = _dbus_strdup (guid);
1475 if (guid_in_connection == NULL)
1477 dbus_free (guid_key);
1481 _DBUS_LOCK (shared_connections);
1482 _dbus_assert (shared_connections != NULL);
1484 if (!_dbus_hash_table_insert_string (shared_connections,
1485 guid_key, connection))
1487 dbus_free (guid_key);
1488 dbus_free (guid_in_connection);
1489 _DBUS_UNLOCK (shared_connections);
1493 connection->server_guid = guid_in_connection;
1495 _dbus_verbose ("stored connection to %s to be shared\n",
1496 connection->server_guid);
1498 _DBUS_UNLOCK (shared_connections);
1500 _dbus_assert (connection->server_guid != NULL);
1506 connection_forget_shared_unlocked (DBusConnection *connection)
1508 HAVE_LOCK_CHECK (connection);
1510 if (connection->server_guid == NULL)
1513 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1514 connection->server_guid);
1516 _DBUS_LOCK (shared_connections);
1518 if (!_dbus_hash_table_remove_string (shared_connections,
1519 connection->server_guid))
1520 _dbus_assert_not_reached ("connection was not in the shared table");
1522 dbus_free (connection->server_guid);
1523 connection->server_guid = NULL;
1525 _DBUS_UNLOCK (shared_connections);
1528 static DBusConnection*
1529 connection_try_from_address_entry (DBusAddressEntry *entry,
1532 DBusTransport *transport;
1533 DBusConnection *connection;
1535 transport = _dbus_transport_open (entry, error);
1537 if (transport == NULL)
1539 _DBUS_ASSERT_ERROR_IS_SET (error);
1543 connection = _dbus_connection_new_for_transport (transport);
1545 _dbus_transport_unref (transport);
1547 if (connection == NULL)
1549 _DBUS_SET_OOM (error);
1553 #ifndef DBUS_DISABLE_CHECKS
1554 _dbus_assert (!connection->have_connection_lock);
1560 * If the shared parameter is true, then any existing connection will
1561 * be used (and if a new connection is created, it will be available
1562 * for use by others). If the shared parameter is false, a new
1563 * connection will always be created, and the new connection will
1564 * never be returned to other callers.
1566 * @param address the address
1567 * @param shared whether the connection is shared or private
1568 * @param error error return
1569 * @returns the connection or #NULL on error
1571 static DBusConnection*
1572 _dbus_connection_open_internal (const char *address,
1576 DBusConnection *connection;
1577 DBusAddressEntry **entries;
1578 DBusError tmp_error;
1579 DBusError first_error;
1582 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1584 _dbus_verbose ("opening %s connection to: %s\n",
1585 shared ? "shared" : "private", address);
1587 if (!dbus_parse_address (address, &entries, &len, error))
1590 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1594 dbus_error_init (&tmp_error);
1595 dbus_error_init (&first_error);
1596 for (i = 0; i < len; i++)
1600 if (!connection_lookup_shared (entries[i], &connection))
1601 _DBUS_SET_OOM (&tmp_error);
1604 if (connection == NULL)
1606 connection = connection_try_from_address_entry (entries[i],
1609 if (connection != NULL && shared)
1613 connection->shareable = TRUE;
1615 guid = dbus_address_entry_get_value (entries[i], "guid");
1617 /* we don't have a connection lock but we know nobody
1618 * else has a handle to the connection
1622 !connection_record_shared_unlocked (connection, guid))
1624 _DBUS_SET_OOM (&tmp_error);
1625 dbus_connection_disconnect (connection);
1626 dbus_connection_unref (connection);
1630 /* but as of now the connection is possibly shared
1631 * since another thread could have pulled it from the table
1639 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1642 dbus_move_error (&tmp_error, &first_error);
1644 dbus_error_free (&tmp_error);
1647 /* NOTE we don't have a lock on a possibly-shared connection object */
1649 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1650 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1652 if (connection == NULL)
1654 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1655 dbus_move_error (&first_error, error);
1659 dbus_error_free (&first_error);
1662 dbus_address_entries_free (entries);
1669 * @addtogroup DBusConnection
1675 * Gets a connection to a remote address. If a connection to the given
1676 * address already exists, returns the existing connection with its
1677 * reference count incremented. Otherwise, returns a new connection
1678 * and saves the new connection for possible re-use if a future call
1679 * to dbus_connection_open() asks to connect to the same server.
1681 * Use dbus_connection_open_private() to get a dedicated connection
1682 * not shared with other callers of dbus_connection_open().
1684 * If the open fails, the function returns #NULL, and provides a
1685 * reason for the failure in the error parameter. Pass #NULL for the
1686 * error parameter if you aren't interested in the reason for
1689 * @param address the address.
1690 * @param error address where an error can be returned.
1691 * @returns new connection, or #NULL on failure.
1694 dbus_connection_open (const char *address,
1697 DBusConnection *connection;
1699 _dbus_return_val_if_fail (address != NULL, NULL);
1700 _dbus_return_val_if_error_is_set (error, NULL);
1702 connection = _dbus_connection_open_internal (address,
1710 * Opens a new, dedicated connection to a remote address. Unlike
1711 * dbus_connection_open(), always creates a new connection.
1712 * This connection will not be saved or recycled by libdbus.
1714 * If the open fails, the function returns #NULL, and provides a
1715 * reason for the failure in the error parameter. Pass #NULL for the
1716 * error parameter if you aren't interested in the reason for
1719 * @param address the address.
1720 * @param error address where an error can be returned.
1721 * @returns new connection, or #NULL on failure.
1724 dbus_connection_open_private (const char *address,
1727 DBusConnection *connection;
1729 _dbus_return_val_if_fail (address != NULL, NULL);
1730 _dbus_return_val_if_error_is_set (error, NULL);
1732 connection = _dbus_connection_open_internal (address,
1740 * Increments the reference count of a DBusConnection.
1742 * @param connection the connection.
1743 * @returns the connection.
1746 dbus_connection_ref (DBusConnection *connection)
1748 _dbus_return_val_if_fail (connection != NULL, NULL);
1749 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
1751 /* The connection lock is better than the global
1752 * lock in the atomic increment fallback
1755 #ifdef DBUS_HAVE_ATOMIC_INT
1756 _dbus_atomic_inc (&connection->refcount);
1758 CONNECTION_LOCK (connection);
1759 _dbus_assert (connection->refcount.value > 0);
1761 connection->refcount.value += 1;
1762 CONNECTION_UNLOCK (connection);
1769 free_outgoing_message (void *element,
1772 DBusMessage *message = element;
1773 DBusConnection *connection = data;
1775 _dbus_message_remove_size_counter (message,
1776 connection->outgoing_counter,
1778 dbus_message_unref (message);
1781 /* This is run without the mutex held, but after the last reference
1782 * to the connection has been dropped we should have no thread-related
1786 _dbus_connection_last_unref (DBusConnection *connection)
1790 _dbus_verbose ("Finalizing connection %p\n", connection);
1792 _dbus_assert (connection->refcount.value == 0);
1794 /* You have to disconnect the connection before unref:ing it. Otherwise
1795 * you won't get the disconnected message.
1797 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1798 _dbus_assert (connection->server_guid == NULL);
1800 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1801 _dbus_object_tree_free_all_unlocked (connection->objects);
1803 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1804 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1805 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1807 _dbus_watch_list_free (connection->watches);
1808 connection->watches = NULL;
1810 _dbus_timeout_list_free (connection->timeouts);
1811 connection->timeouts = NULL;
1813 _dbus_data_slot_list_free (&connection->slot_list);
1815 link = _dbus_list_get_first_link (&connection->filter_list);
1816 while (link != NULL)
1818 DBusMessageFilter *filter = link->data;
1819 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1821 filter->function = NULL;
1822 _dbus_message_filter_unref (filter); /* calls app callback */
1827 _dbus_list_clear (&connection->filter_list);
1829 /* ---- Done with stuff that invokes application callbacks */
1831 _dbus_object_tree_unref (connection->objects);
1833 _dbus_hash_table_unref (connection->pending_replies);
1834 connection->pending_replies = NULL;
1836 _dbus_list_clear (&connection->filter_list);
1838 _dbus_list_foreach (&connection->outgoing_messages,
1839 free_outgoing_message,
1841 _dbus_list_clear (&connection->outgoing_messages);
1843 _dbus_list_foreach (&connection->incoming_messages,
1844 (DBusForeachFunction) dbus_message_unref,
1846 _dbus_list_clear (&connection->incoming_messages);
1848 _dbus_counter_unref (connection->outgoing_counter);
1850 _dbus_transport_unref (connection->transport);
1852 if (connection->disconnect_message_link)
1854 DBusMessage *message = connection->disconnect_message_link->data;
1855 dbus_message_unref (message);
1856 _dbus_list_free_link (connection->disconnect_message_link);
1859 _dbus_list_clear (&connection->link_cache);
1861 _dbus_condvar_free (connection->dispatch_cond);
1862 _dbus_condvar_free (connection->io_path_cond);
1864 _dbus_mutex_free (connection->io_path_mutex);
1865 _dbus_mutex_free (connection->dispatch_mutex);
1867 _dbus_mutex_free (connection->mutex);
1869 dbus_free (connection);
1873 * Decrements the reference count of a DBusConnection, and finalizes
1874 * it if the count reaches zero. It is a bug to drop the last reference
1875 * to a connection that has not been disconnected.
1877 * @todo in practice it can be quite tricky to never unref a connection
1878 * that's still connected; maybe there's some way we could avoid
1881 * @param connection the connection.
1884 dbus_connection_unref (DBusConnection *connection)
1886 dbus_bool_t last_unref;
1888 _dbus_return_if_fail (connection != NULL);
1889 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1891 /* The connection lock is better than the global
1892 * lock in the atomic increment fallback
1895 #ifdef DBUS_HAVE_ATOMIC_INT
1896 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1898 CONNECTION_LOCK (connection);
1900 _dbus_assert (connection->refcount.value > 0);
1902 connection->refcount.value -= 1;
1903 last_unref = (connection->refcount.value == 0);
1906 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1909 CONNECTION_UNLOCK (connection);
1913 _dbus_connection_last_unref (connection);
1917 * Closes the connection, so no further data can be sent or received.
1918 * Any further attempts to send data will result in errors. This
1919 * function does not affect the connection's reference count. It's
1920 * safe to disconnect a connection more than once; all calls after the
1921 * first do nothing. It's impossible to "reconnect" a connection, a
1922 * new connection must be created. This function may result in a call
1923 * to the DBusDispatchStatusFunction set with
1924 * dbus_connection_set_dispatch_status_function(), as the disconnect
1925 * message it generates needs to be dispatched.
1927 * @param connection the connection.
1930 dbus_connection_disconnect (DBusConnection *connection)
1932 DBusDispatchStatus status;
1934 _dbus_return_if_fail (connection != NULL);
1935 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1937 _dbus_verbose ("Disconnecting %p\n", connection);
1939 CONNECTION_LOCK (connection);
1941 _dbus_transport_disconnect (connection->transport);
1943 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1944 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1946 /* this calls out to user code */
1947 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1951 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1953 HAVE_LOCK_CHECK (connection);
1954 return _dbus_transport_get_is_connected (connection->transport);
1958 * Gets whether the connection is currently connected. All
1959 * connections are connected when they are opened. A connection may
1960 * become disconnected when the remote application closes its end, or
1961 * exits; a connection may also be disconnected with
1962 * dbus_connection_disconnect().
1964 * @param connection the connection.
1965 * @returns #TRUE if the connection is still alive.
1968 dbus_connection_get_is_connected (DBusConnection *connection)
1972 _dbus_return_val_if_fail (connection != NULL, FALSE);
1974 CONNECTION_LOCK (connection);
1975 res = _dbus_connection_get_is_connected_unlocked (connection);
1976 CONNECTION_UNLOCK (connection);
1982 * Gets whether the connection was authenticated. (Note that
1983 * if the connection was authenticated then disconnected,
1984 * this function still returns #TRUE)
1986 * @param connection the connection
1987 * @returns #TRUE if the connection was ever authenticated
1990 dbus_connection_get_is_authenticated (DBusConnection *connection)
1994 _dbus_return_val_if_fail (connection != NULL, FALSE);
1996 CONNECTION_LOCK (connection);
1997 res = _dbus_transport_get_is_authenticated (connection->transport);
1998 CONNECTION_UNLOCK (connection);
2004 * Set whether _exit() should be called when the connection receives a
2005 * disconnect signal. The call to _exit() comes after any handlers for
2006 * the disconnect signal run; handlers can cancel the exit by calling
2009 * By default, exit_on_disconnect is #FALSE; but for message bus
2010 * connections returned from dbus_bus_get() it will be toggled on
2013 * @param connection the connection
2014 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
2017 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
2018 dbus_bool_t exit_on_disconnect)
2020 _dbus_return_if_fail (connection != NULL);
2022 CONNECTION_LOCK (connection);
2023 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
2024 CONNECTION_UNLOCK (connection);
2027 static DBusPreallocatedSend*
2028 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
2030 DBusPreallocatedSend *preallocated;
2032 HAVE_LOCK_CHECK (connection);
2034 _dbus_assert (connection != NULL);
2036 preallocated = dbus_new (DBusPreallocatedSend, 1);
2037 if (preallocated == NULL)
2040 if (connection->link_cache != NULL)
2042 preallocated->queue_link =
2043 _dbus_list_pop_first_link (&connection->link_cache);
2044 preallocated->queue_link->data = NULL;
2048 preallocated->queue_link = _dbus_list_alloc_link (NULL);
2049 if (preallocated->queue_link == NULL)
2053 if (connection->link_cache != NULL)
2055 preallocated->counter_link =
2056 _dbus_list_pop_first_link (&connection->link_cache);
2057 preallocated->counter_link->data = connection->outgoing_counter;
2061 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
2062 if (preallocated->counter_link == NULL)
2066 _dbus_counter_ref (preallocated->counter_link->data);
2068 preallocated->connection = connection;
2070 return preallocated;
2073 _dbus_list_free_link (preallocated->queue_link);
2075 dbus_free (preallocated);
2081 * Preallocates resources needed to send a message, allowing the message
2082 * to be sent without the possibility of memory allocation failure.
2083 * Allows apps to create a future guarantee that they can send
2084 * a message regardless of memory shortages.
2086 * @param connection the connection we're preallocating for.
2087 * @returns the preallocated resources, or #NULL
2089 DBusPreallocatedSend*
2090 dbus_connection_preallocate_send (DBusConnection *connection)
2092 DBusPreallocatedSend *preallocated;
2094 _dbus_return_val_if_fail (connection != NULL, NULL);
2096 CONNECTION_LOCK (connection);
2099 _dbus_connection_preallocate_send_unlocked (connection);
2101 CONNECTION_UNLOCK (connection);
2103 return preallocated;
2107 * Frees preallocated message-sending resources from
2108 * dbus_connection_preallocate_send(). Should only
2109 * be called if the preallocated resources are not used
2110 * to send a message.
2112 * @param connection the connection
2113 * @param preallocated the resources
2116 dbus_connection_free_preallocated_send (DBusConnection *connection,
2117 DBusPreallocatedSend *preallocated)
2119 _dbus_return_if_fail (connection != NULL);
2120 _dbus_return_if_fail (preallocated != NULL);
2121 _dbus_return_if_fail (connection == preallocated->connection);
2123 _dbus_list_free_link (preallocated->queue_link);
2124 _dbus_counter_unref (preallocated->counter_link->data);
2125 _dbus_list_free_link (preallocated->counter_link);
2126 dbus_free (preallocated);
2129 /* Called with lock held, does not update dispatch status */
2131 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
2132 DBusPreallocatedSend *preallocated,
2133 DBusMessage *message,
2134 dbus_uint32_t *client_serial)
2136 dbus_uint32_t serial;
2139 preallocated->queue_link->data = message;
2140 _dbus_list_prepend_link (&connection->outgoing_messages,
2141 preallocated->queue_link);
2143 _dbus_message_add_size_counter_link (message,
2144 preallocated->counter_link);
2146 dbus_free (preallocated);
2147 preallocated = NULL;
2149 dbus_message_ref (message);
2151 connection->n_outgoing += 1;
2153 sig = dbus_message_get_signature (message);
2155 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
2157 dbus_message_get_type (message),
2158 dbus_message_get_path (message),
2159 dbus_message_get_interface (message) ?
2160 dbus_message_get_interface (message) :
2162 dbus_message_get_member (message) ?
2163 dbus_message_get_member (message) :
2166 dbus_message_get_destination (message) ?
2167 dbus_message_get_destination (message) :
2170 connection->n_outgoing);
2172 if (dbus_message_get_serial (message) == 0)
2174 serial = _dbus_connection_get_next_client_serial (connection);
2175 _dbus_message_set_serial (message, serial);
2177 *client_serial = serial;
2182 *client_serial = dbus_message_get_serial (message);
2185 _dbus_verbose ("Message %p serial is %u\n",
2186 message, dbus_message_get_serial (message));
2188 _dbus_message_lock (message);
2190 /* Now we need to run an iteration to hopefully just write the messages
2191 * out immediately, and otherwise get them queued up
2193 _dbus_connection_do_iteration_unlocked (connection,
2194 DBUS_ITERATION_DO_WRITING,
2197 /* If stuff is still queued up, be sure we wake up the main loop */
2198 if (connection->n_outgoing > 0)
2199 _dbus_connection_wakeup_mainloop (connection);
2203 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
2204 DBusPreallocatedSend *preallocated,
2205 DBusMessage *message,
2206 dbus_uint32_t *client_serial)
2208 DBusDispatchStatus status;
2210 HAVE_LOCK_CHECK (connection);
2212 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2214 message, client_serial);
2216 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2217 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2219 /* this calls out to user code */
2220 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2224 * Sends a message using preallocated resources. This function cannot fail.
2225 * It works identically to dbus_connection_send() in other respects.
2226 * Preallocated resources comes from dbus_connection_preallocate_send().
2227 * This function "consumes" the preallocated resources, they need not
2228 * be freed separately.
2230 * @param connection the connection
2231 * @param preallocated the preallocated resources
2232 * @param message the message to send
2233 * @param client_serial return location for client serial assigned to the message
2236 dbus_connection_send_preallocated (DBusConnection *connection,
2237 DBusPreallocatedSend *preallocated,
2238 DBusMessage *message,
2239 dbus_uint32_t *client_serial)
2241 _dbus_return_if_fail (connection != NULL);
2242 _dbus_return_if_fail (preallocated != NULL);
2243 _dbus_return_if_fail (message != NULL);
2244 _dbus_return_if_fail (preallocated->connection == connection);
2245 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
2246 (dbus_message_get_interface (message) != NULL &&
2247 dbus_message_get_member (message) != NULL));
2248 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
2249 (dbus_message_get_interface (message) != NULL &&
2250 dbus_message_get_member (message) != NULL));
2252 CONNECTION_LOCK (connection);
2253 _dbus_connection_send_preallocated_and_unlock (connection,
2255 message, client_serial);
2259 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
2260 DBusMessage *message,
2261 dbus_uint32_t *client_serial)
2263 DBusPreallocatedSend *preallocated;
2265 _dbus_assert (connection != NULL);
2266 _dbus_assert (message != NULL);
2268 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2269 if (preallocated == NULL)
2272 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2280 _dbus_connection_send_and_unlock (DBusConnection *connection,
2281 DBusMessage *message,
2282 dbus_uint32_t *client_serial)
2284 DBusPreallocatedSend *preallocated;
2286 _dbus_assert (connection != NULL);
2287 _dbus_assert (message != NULL);
2289 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2290 if (preallocated == NULL)
2292 CONNECTION_UNLOCK (connection);
2296 _dbus_connection_send_preallocated_and_unlock (connection,
2304 * Adds a message to the outgoing message queue. Does not block to
2305 * write the message to the network; that happens asynchronously. To
2306 * force the message to be written, call dbus_connection_flush().
2307 * Because this only queues the message, the only reason it can
2308 * fail is lack of memory. Even if the connection is disconnected,
2309 * no error will be returned.
2311 * If the function fails due to lack of memory, it returns #FALSE.
2312 * The function will never fail for other reasons; even if the
2313 * connection is disconnected, you can queue an outgoing message,
2314 * though obviously it won't be sent.
2316 * @param connection the connection.
2317 * @param message the message to write.
2318 * @param client_serial return location for client serial.
2319 * @returns #TRUE on success.
2322 dbus_connection_send (DBusConnection *connection,
2323 DBusMessage *message,
2324 dbus_uint32_t *client_serial)
2326 _dbus_return_val_if_fail (connection != NULL, FALSE);
2327 _dbus_return_val_if_fail (message != NULL, FALSE);
2329 CONNECTION_LOCK (connection);
2331 return _dbus_connection_send_and_unlock (connection,
2337 reply_handler_timeout (void *data)
2339 DBusConnection *connection;
2340 DBusDispatchStatus status;
2341 DBusPendingCall *pending = data;
2343 connection = pending->connection;
2345 CONNECTION_LOCK (connection);
2346 if (pending->timeout_link)
2348 _dbus_connection_queue_synthesized_message_link (connection,
2349 pending->timeout_link);
2350 pending->timeout_link = NULL;
2353 _dbus_connection_remove_timeout (connection,
2355 pending->timeout_added = FALSE;
2357 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2358 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2360 /* Unlocks, and calls out to user code */
2361 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2367 * Queues a message to send, as with dbus_connection_send_message(),
2368 * but also returns a #DBusPendingCall used to receive a reply to the
2369 * message. If no reply is received in the given timeout_milliseconds,
2370 * this function expires the pending reply and generates a synthetic
2371 * error reply (generated in-process, not by the remote application)
2372 * indicating that a timeout occurred.
2374 * A #DBusPendingCall will see a reply message after any filters, but
2375 * before any object instances or other handlers. A #DBusPendingCall
2376 * will always see exactly one reply message, unless it's cancelled
2377 * with dbus_pending_call_cancel().
2379 * If a filter filters out the reply before the handler sees it, the
2380 * reply is immediately timed out and a timeout error reply is
2381 * generated. If a filter removes the timeout error reply then the
2382 * #DBusPendingCall will get confused. Filtering the timeout error
2383 * is thus considered a bug and will print a warning.
2385 * If #NULL is passed for the pending_return, the #DBusPendingCall
2386 * will still be generated internally, and used to track
2387 * the message reply timeout. This means a timeout error will
2388 * occur if no reply arrives, unlike with dbus_connection_send().
2390 * If -1 is passed for the timeout, a sane default timeout is used. -1
2391 * is typically the best value for the timeout for this reason, unless
2392 * you want a very short or very long timeout. There is no way to
2393 * avoid a timeout entirely, other than passing INT_MAX for the
2394 * timeout to postpone it indefinitely.
2396 * @param connection the connection
2397 * @param message the message to send
2398 * @param pending_return return location for a #DBusPendingCall object, or #NULL
2399 * @param timeout_milliseconds timeout in milliseconds or -1 for default
2400 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
2404 dbus_connection_send_with_reply (DBusConnection *connection,
2405 DBusMessage *message,
2406 DBusPendingCall **pending_return,
2407 int timeout_milliseconds)
2409 DBusPendingCall *pending;
2411 DBusList *reply_link;
2412 dbus_int32_t serial = -1;
2413 DBusDispatchStatus status;
2415 _dbus_return_val_if_fail (connection != NULL, FALSE);
2416 _dbus_return_val_if_fail (message != NULL, FALSE);
2417 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2420 *pending_return = NULL;
2422 pending = _dbus_pending_call_new (connection,
2423 timeout_milliseconds,
2424 reply_handler_timeout);
2426 if (pending == NULL)
2429 CONNECTION_LOCK (connection);
2431 /* Assign a serial to the message */
2432 if (dbus_message_get_serial (message) == 0)
2434 serial = _dbus_connection_get_next_client_serial (connection);
2435 _dbus_message_set_serial (message, serial);
2438 pending->reply_serial = serial;
2440 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
2441 "No reply within specified time");
2445 reply_link = _dbus_list_alloc_link (reply);
2446 if (reply_link == NULL)
2448 CONNECTION_UNLOCK (connection);
2449 dbus_message_unref (reply);
2450 goto error_unlocked;
2453 pending->timeout_link = reply_link;
2455 /* Insert the serial in the pending replies hash;
2456 * hash takes a refcount on DBusPendingCall.
2457 * Also, add the timeout.
2459 if (!_dbus_connection_attach_pending_call_unlocked (connection,
2463 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
2465 _dbus_connection_detach_pending_call_and_unlock (connection,
2467 goto error_unlocked;
2471 *pending_return = pending;
2474 _dbus_connection_detach_pending_call_unlocked (connection, pending);
2475 dbus_pending_call_unref (pending);
2478 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2479 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2481 /* this calls out to user code */
2482 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2487 CONNECTION_UNLOCK (connection);
2489 dbus_pending_call_unref (pending);
2493 /* This is slightly strange since we can pop a message here without
2494 * the dispatch lock.
2497 check_for_reply_unlocked (DBusConnection *connection,
2498 dbus_uint32_t client_serial)
2502 HAVE_LOCK_CHECK (connection);
2504 link = _dbus_list_get_first_link (&connection->incoming_messages);
2506 while (link != NULL)
2508 DBusMessage *reply = link->data;
2510 if (dbus_message_get_reply_serial (reply) == client_serial)
2512 _dbus_list_remove_link (&connection->incoming_messages, link);
2513 connection->n_incoming -= 1;
2516 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2523 * Blocks until a pending call times out or gets a reply.
2525 * Does not re-enter the main loop or run filter/path-registered
2526 * callbacks. The reply to the message will not be seen by
2529 * Returns immediately if pending call already got a reply.
2531 * @todo could use performance improvements (it keeps scanning
2532 * the whole message queue for example)
2534 * @param pending the pending call we block for a reply on
2537 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2539 long start_tv_sec, start_tv_usec;
2540 long end_tv_sec, end_tv_usec;
2541 long tv_sec, tv_usec;
2542 DBusDispatchStatus status;
2543 DBusConnection *connection;
2544 dbus_uint32_t client_serial;
2545 int timeout_milliseconds;
2547 _dbus_assert (pending != NULL);
2549 if (dbus_pending_call_get_completed (pending))
2552 if (pending->connection == NULL)
2553 return; /* call already detached */
2555 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2557 connection = pending->connection;
2558 client_serial = pending->reply_serial;
2560 /* note that timeout_milliseconds is limited to a smallish value
2561 * in _dbus_pending_call_new() so overflows aren't possible
2564 timeout_milliseconds = dbus_timeout_get_interval (pending->timeout);
2566 /* Flush message queue */
2567 dbus_connection_flush (connection);
2569 CONNECTION_LOCK (connection);
2571 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2572 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2573 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2574 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2575 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2577 _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",
2578 timeout_milliseconds,
2580 start_tv_sec, start_tv_usec,
2581 end_tv_sec, end_tv_usec);
2583 /* Now we wait... */
2584 /* always block at least once as we know we don't have the reply yet */
2585 _dbus_connection_do_iteration_unlocked (connection,
2586 DBUS_ITERATION_DO_READING |
2587 DBUS_ITERATION_BLOCK,
2588 timeout_milliseconds);
2592 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2594 HAVE_LOCK_CHECK (connection);
2596 /* queue messages and get status */
2598 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2600 /* the get_completed() is in case a dispatch() while we were blocking
2601 * got the reply instead of us.
2603 if (dbus_pending_call_get_completed (pending))
2605 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2606 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2607 dbus_pending_call_unref (pending);
2611 if (status == DBUS_DISPATCH_DATA_REMAINS)
2615 reply = check_for_reply_unlocked (connection, client_serial);
2618 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2620 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2622 _dbus_pending_call_complete_and_unlock (pending, reply);
2623 dbus_message_unref (reply);
2625 CONNECTION_LOCK (connection);
2626 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2627 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2628 dbus_pending_call_unref (pending);
2634 _dbus_get_current_time (&tv_sec, &tv_usec);
2636 if (!_dbus_connection_get_is_connected_unlocked (connection))
2638 /* FIXME send a "DBUS_ERROR_DISCONNECTED" instead, just to help
2639 * programmers understand what went wrong since the timeout is
2643 _dbus_pending_call_complete_and_unlock (pending, NULL);
2644 dbus_pending_call_unref (pending);
2647 else if (tv_sec < start_tv_sec)
2648 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2649 else if (connection->disconnect_message_link == NULL)
2650 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2651 else if (tv_sec < end_tv_sec ||
2652 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2654 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2655 (end_tv_usec - tv_usec) / 1000;
2656 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2657 _dbus_assert (timeout_milliseconds >= 0);
2659 if (status == DBUS_DISPATCH_NEED_MEMORY)
2661 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2662 * we may already have a reply in the buffer and just can't process
2665 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2667 if (timeout_milliseconds < 100)
2668 ; /* just busy loop */
2669 else if (timeout_milliseconds <= 1000)
2670 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2672 _dbus_sleep_milliseconds (1000);
2676 /* block again, we don't have the reply buffered yet. */
2677 _dbus_connection_do_iteration_unlocked (connection,
2678 DBUS_ITERATION_DO_READING |
2679 DBUS_ITERATION_BLOCK,
2680 timeout_milliseconds);
2683 goto recheck_status;
2686 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2687 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2689 _dbus_assert (!dbus_pending_call_get_completed (pending));
2691 /* unlock and call user code */
2692 _dbus_pending_call_complete_and_unlock (pending, NULL);
2694 /* update user code on dispatch status */
2695 CONNECTION_LOCK (connection);
2696 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2697 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2698 dbus_pending_call_unref (pending);
2702 * Sends a message and blocks a certain time period while waiting for
2703 * a reply. This function does not reenter the main loop,
2704 * i.e. messages other than the reply are queued up but not
2705 * processed. This function is used to do non-reentrant "method
2708 * If a normal reply is received, it is returned, and removed from the
2709 * incoming message queue. If it is not received, #NULL is returned
2710 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
2711 * received, it is converted to a #DBusError and returned as an error,
2712 * then the reply message is deleted. If something else goes wrong,
2713 * result is set to whatever is appropriate, such as
2714 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
2716 * @param connection the connection
2717 * @param message the message to send
2718 * @param timeout_milliseconds timeout in milliseconds or -1 for default
2719 * @param error return location for error message
2720 * @returns the message that is the reply or #NULL with an error code if the
2724 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
2725 DBusMessage *message,
2726 int timeout_milliseconds,
2730 DBusPendingCall *pending;
2732 _dbus_return_val_if_fail (connection != NULL, NULL);
2733 _dbus_return_val_if_fail (message != NULL, NULL);
2734 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2735 _dbus_return_val_if_error_is_set (error, NULL);
2737 if (!dbus_connection_send_with_reply (connection, message,
2738 &pending, timeout_milliseconds))
2740 _DBUS_SET_OOM (error);
2744 _dbus_assert (pending != NULL);
2746 dbus_pending_call_block (pending);
2748 reply = dbus_pending_call_steal_reply (pending);
2749 dbus_pending_call_unref (pending);
2751 /* call_complete_and_unlock() called from pending_call_block() should
2752 * always fill this in.
2754 _dbus_assert (reply != NULL);
2756 if (dbus_set_error_from_message (error, reply))
2758 dbus_message_unref (reply);
2766 * Blocks until the outgoing message queue is empty.
2768 * @param connection the connection.
2771 dbus_connection_flush (DBusConnection *connection)
2773 /* We have to specify DBUS_ITERATION_DO_READING here because
2774 * otherwise we could have two apps deadlock if they are both doing
2775 * a flush(), and the kernel buffers fill up. This could change the
2778 DBusDispatchStatus status;
2780 _dbus_return_if_fail (connection != NULL);
2782 CONNECTION_LOCK (connection);
2783 while (connection->n_outgoing > 0 &&
2784 _dbus_connection_get_is_connected_unlocked (connection))
2786 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
2787 HAVE_LOCK_CHECK (connection);
2788 _dbus_connection_do_iteration_unlocked (connection,
2789 DBUS_ITERATION_DO_READING |
2790 DBUS_ITERATION_DO_WRITING |
2791 DBUS_ITERATION_BLOCK,
2795 HAVE_LOCK_CHECK (connection);
2796 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2797 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2799 HAVE_LOCK_CHECK (connection);
2800 /* Unlocks and calls out to user code */
2801 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2803 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
2807 * Returns the first-received message from the incoming message queue,
2808 * leaving it in the queue. If the queue is empty, returns #NULL.
2810 * The caller does not own a reference to the returned message, and
2811 * must either return it using dbus_connection_return_message() or
2812 * keep it after calling dbus_connection_steal_borrowed_message(). No
2813 * one can get at the message while its borrowed, so return it as
2814 * quickly as possible and don't keep a reference to it after
2815 * returning it. If you need to keep the message, make a copy of it.
2817 * dbus_connection_dispatch() will block if called while a borrowed
2818 * message is outstanding; only one piece of code can be playing with
2819 * the incoming queue at a time. This function will block if called
2820 * during a dbus_connection_dispatch().
2822 * @param connection the connection.
2823 * @returns next message in the incoming queue.
2826 dbus_connection_borrow_message (DBusConnection *connection)
2828 DBusDispatchStatus status;
2829 DBusMessage *message;
2831 _dbus_return_val_if_fail (connection != NULL, NULL);
2833 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
2835 /* this is called for the side effect that it queues
2836 * up any messages from the transport
2838 status = dbus_connection_get_dispatch_status (connection);
2839 if (status != DBUS_DISPATCH_DATA_REMAINS)
2842 CONNECTION_LOCK (connection);
2844 _dbus_connection_acquire_dispatch (connection);
2846 /* While a message is outstanding, the dispatch lock is held */
2847 _dbus_assert (connection->message_borrowed == NULL);
2849 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
2851 message = connection->message_borrowed;
2853 /* Note that we KEEP the dispatch lock until the message is returned */
2854 if (message == NULL)
2855 _dbus_connection_release_dispatch (connection);
2857 CONNECTION_UNLOCK (connection);
2863 * Used to return a message after peeking at it using
2864 * dbus_connection_borrow_message(). Only called if
2865 * message from dbus_connection_borrow_message() was non-#NULL.
2867 * @param connection the connection
2868 * @param message the message from dbus_connection_borrow_message()
2871 dbus_connection_return_message (DBusConnection *connection,
2872 DBusMessage *message)
2874 _dbus_return_if_fail (connection != NULL);
2875 _dbus_return_if_fail (message != NULL);
2876 _dbus_return_if_fail (message == connection->message_borrowed);
2877 _dbus_return_if_fail (connection->dispatch_acquired);
2879 CONNECTION_LOCK (connection);
2881 _dbus_assert (message == connection->message_borrowed);
2883 connection->message_borrowed = NULL;
2885 _dbus_connection_release_dispatch (connection);
2887 CONNECTION_UNLOCK (connection);
2891 * Used to keep a message after peeking at it using
2892 * dbus_connection_borrow_message(). Before using this function, see
2893 * the caveats/warnings in the documentation for
2894 * dbus_connection_pop_message().
2896 * @param connection the connection
2897 * @param message the message from dbus_connection_borrow_message()
2900 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2901 DBusMessage *message)
2903 DBusMessage *pop_message;
2905 _dbus_return_if_fail (connection != NULL);
2906 _dbus_return_if_fail (message != NULL);
2907 _dbus_return_if_fail (message == connection->message_borrowed);
2908 _dbus_return_if_fail (connection->dispatch_acquired);
2910 CONNECTION_LOCK (connection);
2912 _dbus_assert (message == connection->message_borrowed);
2914 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
2915 _dbus_assert (message == pop_message);
2917 connection->n_incoming -= 1;
2919 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
2920 message, connection->n_incoming);
2922 connection->message_borrowed = NULL;
2924 _dbus_connection_release_dispatch (connection);
2926 CONNECTION_UNLOCK (connection);
2929 /* See dbus_connection_pop_message, but requires the caller to own
2930 * the lock before calling. May drop the lock while running.
2933 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
2935 HAVE_LOCK_CHECK (connection);
2937 _dbus_assert (connection->message_borrowed == NULL);
2939 if (connection->n_incoming > 0)
2943 link = _dbus_list_pop_first_link (&connection->incoming_messages);
2944 connection->n_incoming -= 1;
2946 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
2948 dbus_message_get_type (link->data),
2949 dbus_message_get_path (link->data),
2950 dbus_message_get_interface (link->data) ?
2951 dbus_message_get_interface (link->data) :
2953 dbus_message_get_member (link->data) ?
2954 dbus_message_get_member (link->data) :
2956 dbus_message_get_signature (link->data),
2957 connection, connection->n_incoming);
2965 /* See dbus_connection_pop_message, but requires the caller to own
2966 * the lock before calling. May drop the lock while running.
2969 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
2973 HAVE_LOCK_CHECK (connection);
2975 link = _dbus_connection_pop_message_link_unlocked (connection);
2979 DBusMessage *message;
2981 message = link->data;
2983 _dbus_list_free_link (link);
2992 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
2993 DBusList *message_link)
2995 HAVE_LOCK_CHECK (connection);
2997 _dbus_assert (message_link != NULL);
2998 /* You can't borrow a message while a link is outstanding */
2999 _dbus_assert (connection->message_borrowed == NULL);
3000 /* We had to have the dispatch lock across the pop/putback */
3001 _dbus_assert (connection->dispatch_acquired);
3003 _dbus_list_prepend_link (&connection->incoming_messages,
3005 connection->n_incoming += 1;
3007 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3009 dbus_message_get_type (message_link->data),
3010 dbus_message_get_interface (message_link->data) ?
3011 dbus_message_get_interface (message_link->data) :
3013 dbus_message_get_member (message_link->data) ?
3014 dbus_message_get_member (message_link->data) :
3016 dbus_message_get_signature (message_link->data),
3017 connection, connection->n_incoming);
3021 * Returns the first-received message from the incoming message queue,
3022 * removing it from the queue. The caller owns a reference to the
3023 * returned message. If the queue is empty, returns #NULL.
3025 * This function bypasses any message handlers that are registered,
3026 * and so using it is usually wrong. Instead, let the main loop invoke
3027 * dbus_connection_dispatch(). Popping messages manually is only
3028 * useful in very simple programs that don't share a #DBusConnection
3029 * with any libraries or other modules.
3031 * There is a lock that covers all ways of accessing the incoming message
3032 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3033 * dbus_connection_borrow_message(), etc. will all block while one of the others
3034 * in the group is running.
3036 * @param connection the connection.
3037 * @returns next message in the incoming queue.
3040 dbus_connection_pop_message (DBusConnection *connection)
3042 DBusMessage *message;
3043 DBusDispatchStatus status;
3045 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3047 /* this is called for the side effect that it queues
3048 * up any messages from the transport
3050 status = dbus_connection_get_dispatch_status (connection);
3051 if (status != DBUS_DISPATCH_DATA_REMAINS)
3054 CONNECTION_LOCK (connection);
3055 _dbus_connection_acquire_dispatch (connection);
3056 HAVE_LOCK_CHECK (connection);
3058 message = _dbus_connection_pop_message_unlocked (connection);
3060 _dbus_verbose ("Returning popped message %p\n", message);
3062 _dbus_connection_release_dispatch (connection);
3063 CONNECTION_UNLOCK (connection);
3069 * Acquire the dispatcher. This is a separate lock so the main
3070 * connection lock can be dropped to call out to application dispatch
3073 * @param connection the connection.
3076 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3078 HAVE_LOCK_CHECK (connection);
3080 _dbus_connection_ref_unlocked (connection);
3081 CONNECTION_UNLOCK (connection);
3083 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3084 _dbus_mutex_lock (connection->dispatch_mutex);
3086 while (connection->dispatch_acquired)
3088 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3089 _dbus_condvar_wait (connection->dispatch_cond, connection->dispatch_mutex);
3092 _dbus_assert (!connection->dispatch_acquired);
3094 connection->dispatch_acquired = TRUE;
3096 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3097 _dbus_mutex_unlock (connection->dispatch_mutex);
3099 CONNECTION_LOCK (connection);
3100 _dbus_connection_unref_unlocked (connection);
3104 * Release the dispatcher when you're done with it. Only call
3105 * after you've acquired the dispatcher. Wakes up at most one
3106 * thread currently waiting to acquire the dispatcher.
3108 * @param connection the connection.
3111 _dbus_connection_release_dispatch (DBusConnection *connection)
3113 HAVE_LOCK_CHECK (connection);
3115 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3116 _dbus_mutex_lock (connection->dispatch_mutex);
3118 _dbus_assert (connection->dispatch_acquired);
3120 connection->dispatch_acquired = FALSE;
3121 _dbus_condvar_wake_one (connection->dispatch_cond);
3123 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3124 _dbus_mutex_unlock (connection->dispatch_mutex);
3128 _dbus_connection_failed_pop (DBusConnection *connection,
3129 DBusList *message_link)
3131 _dbus_list_prepend_link (&connection->incoming_messages,
3133 connection->n_incoming += 1;
3136 static DBusDispatchStatus
3137 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3139 HAVE_LOCK_CHECK (connection);
3141 if (connection->n_incoming > 0)
3142 return DBUS_DISPATCH_DATA_REMAINS;
3143 else if (!_dbus_transport_queue_messages (connection->transport))
3144 return DBUS_DISPATCH_NEED_MEMORY;
3147 DBusDispatchStatus status;
3148 dbus_bool_t is_connected;
3150 status = _dbus_transport_get_dispatch_status (connection->transport);
3151 is_connected = _dbus_transport_get_is_connected (connection->transport);
3153 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3154 DISPATCH_STATUS_NAME (status), is_connected);
3158 if (status == DBUS_DISPATCH_COMPLETE &&
3159 connection->disconnect_message_link)
3161 _dbus_verbose ("Sending disconnect message from %s\n",
3162 _DBUS_FUNCTION_NAME);
3164 connection_forget_shared_unlocked (connection);
3166 /* We haven't sent the disconnect message already,
3167 * and all real messages have been queued up.
3169 _dbus_connection_queue_synthesized_message_link (connection,
3170 connection->disconnect_message_link);
3171 connection->disconnect_message_link = NULL;
3174 /* Dump the outgoing queue, we aren't going to be able to
3175 * send it now, and we'd like accessors like
3176 * dbus_connection_get_outgoing_size() to be accurate.
3178 if (connection->n_outgoing > 0)
3182 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3183 connection->n_outgoing);
3185 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3187 _dbus_connection_message_sent (connection, link->data);
3192 if (status != DBUS_DISPATCH_COMPLETE)
3194 else if (connection->n_incoming > 0)
3195 return DBUS_DISPATCH_DATA_REMAINS;
3197 return DBUS_DISPATCH_COMPLETE;
3202 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
3203 DBusDispatchStatus new_status)
3205 dbus_bool_t changed;
3206 DBusDispatchStatusFunction function;
3209 HAVE_LOCK_CHECK (connection);
3211 _dbus_connection_ref_unlocked (connection);
3213 changed = new_status != connection->last_dispatch_status;
3215 connection->last_dispatch_status = new_status;
3217 function = connection->dispatch_status_function;
3218 data = connection->dispatch_status_data;
3220 /* We drop the lock */
3221 CONNECTION_UNLOCK (connection);
3223 if (changed && function)
3225 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
3226 connection, new_status,
3227 DISPATCH_STATUS_NAME (new_status));
3228 (* function) (connection, new_status, data);
3231 dbus_connection_unref (connection);
3235 * Gets the current state (what we would currently return
3236 * from dbus_connection_dispatch()) but doesn't actually
3237 * dispatch any messages.
3239 * @param connection the connection.
3240 * @returns current dispatch status
3243 dbus_connection_get_dispatch_status (DBusConnection *connection)
3245 DBusDispatchStatus status;
3247 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3249 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3251 CONNECTION_LOCK (connection);
3253 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3255 CONNECTION_UNLOCK (connection);
3261 * Processes data buffered while handling watches, queueing zero or
3262 * more incoming messages. Then pops the first-received message from
3263 * the current incoming message queue, runs any handlers for it, and
3264 * unrefs the message. Returns a status indicating whether messages/data
3265 * remain, more memory is needed, or all data has been processed.
3267 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
3268 * does not necessarily dispatch a message, as the data may
3269 * be part of authentication or the like.
3271 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
3273 * @todo FIXME what if we call out to application code to handle a
3274 * message, holding the dispatch lock, and the application code runs
3275 * the main loop and dispatches again? Probably deadlocks at the
3276 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
3277 * and then the GSource etc. could handle the situation? Right now
3278 * our GSource is NO_RECURSE
3280 * @param connection the connection
3281 * @returns dispatch status
3284 dbus_connection_dispatch (DBusConnection *connection)
3286 DBusMessage *message;
3287 DBusList *link, *filter_list_copy, *message_link;
3288 DBusHandlerResult result;
3289 DBusPendingCall *pending;
3290 dbus_int32_t reply_serial;
3291 DBusDispatchStatus status;
3293 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3295 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3297 CONNECTION_LOCK (connection);
3298 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3299 if (status != DBUS_DISPATCH_DATA_REMAINS)
3301 /* unlocks and calls out to user code */
3302 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3306 /* We need to ref the connection since the callback could potentially
3307 * drop the last ref to it
3309 _dbus_connection_ref_unlocked (connection);
3311 _dbus_connection_acquire_dispatch (connection);
3312 HAVE_LOCK_CHECK (connection);
3314 message_link = _dbus_connection_pop_message_link_unlocked (connection);
3315 if (message_link == NULL)
3317 /* another thread dispatched our stuff */
3319 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
3321 _dbus_connection_release_dispatch (connection);
3323 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3325 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3327 dbus_connection_unref (connection);
3332 message = message_link->data;
3334 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
3336 dbus_message_get_type (message),
3337 dbus_message_get_interface (message) ?
3338 dbus_message_get_interface (message) :
3340 dbus_message_get_member (message) ?
3341 dbus_message_get_member (message) :
3343 dbus_message_get_signature (message));
3345 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3347 /* Pending call handling must be first, because if you do
3348 * dbus_connection_send_with_reply_and_block() or
3349 * dbus_pending_call_block() then no handlers/filters will be run on
3350 * the reply. We want consistent semantics in the case where we
3351 * dbus_connection_dispatch() the reply.
3354 reply_serial = dbus_message_get_reply_serial (message);
3355 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
3359 _dbus_verbose ("Dispatching a pending reply\n");
3360 _dbus_pending_call_complete_and_unlock (pending, message);
3361 pending = NULL; /* it's probably unref'd */
3363 CONNECTION_LOCK (connection);
3364 _dbus_verbose ("pending call completed in dispatch\n");
3365 result = DBUS_HANDLER_RESULT_HANDLED;
3369 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
3371 _dbus_connection_release_dispatch (connection);
3372 HAVE_LOCK_CHECK (connection);
3374 _dbus_connection_failed_pop (connection, message_link);
3376 /* unlocks and calls user code */
3377 _dbus_connection_update_dispatch_status_and_unlock (connection,
3378 DBUS_DISPATCH_NEED_MEMORY);
3381 dbus_pending_call_unref (pending);
3382 dbus_connection_unref (connection);
3384 return DBUS_DISPATCH_NEED_MEMORY;
3387 _dbus_list_foreach (&filter_list_copy,
3388 (DBusForeachFunction)_dbus_message_filter_ref,
3391 /* We're still protected from dispatch() reentrancy here
3392 * since we acquired the dispatcher
3394 CONNECTION_UNLOCK (connection);
3396 link = _dbus_list_get_first_link (&filter_list_copy);
3397 while (link != NULL)
3399 DBusMessageFilter *filter = link->data;
3400 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
3402 _dbus_verbose (" running filter on message %p\n", message);
3403 result = (* filter->function) (connection, message, filter->user_data);
3405 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3411 _dbus_list_foreach (&filter_list_copy,
3412 (DBusForeachFunction)_dbus_message_filter_unref,
3414 _dbus_list_clear (&filter_list_copy);
3416 CONNECTION_LOCK (connection);
3418 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
3420 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
3423 else if (result == DBUS_HANDLER_RESULT_HANDLED)
3425 _dbus_verbose ("filter handled message in dispatch\n");
3429 /* We're still protected from dispatch() reentrancy here
3430 * since we acquired the dispatcher
3432 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
3434 dbus_message_get_type (message),
3435 dbus_message_get_interface (message) ?
3436 dbus_message_get_interface (message) :
3438 dbus_message_get_member (message) ?
3439 dbus_message_get_member (message) :
3441 dbus_message_get_signature (message));
3443 HAVE_LOCK_CHECK (connection);
3444 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
3447 CONNECTION_LOCK (connection);
3449 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3451 _dbus_verbose ("object tree handled message in dispatch\n");
3455 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
3459 DBusPreallocatedSend *preallocated;
3461 _dbus_verbose (" sending error %s\n",
3462 DBUS_ERROR_UNKNOWN_METHOD);
3464 if (!_dbus_string_init (&str))
3466 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3467 _dbus_verbose ("no memory for error string in dispatch\n");
3471 if (!_dbus_string_append_printf (&str,
3472 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
3473 dbus_message_get_member (message),
3474 dbus_message_get_signature (message),
3475 dbus_message_get_interface (message)))
3477 _dbus_string_free (&str);
3478 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3479 _dbus_verbose ("no memory for error string in dispatch\n");
3483 reply = dbus_message_new_error (message,
3484 DBUS_ERROR_UNKNOWN_METHOD,
3485 _dbus_string_get_const_data (&str));
3486 _dbus_string_free (&str);
3490 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3491 _dbus_verbose ("no memory for error reply in dispatch\n");
3495 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3497 if (preallocated == NULL)
3499 dbus_message_unref (reply);
3500 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3501 _dbus_verbose ("no memory for error send in dispatch\n");
3505 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
3508 dbus_message_unref (reply);
3510 result = DBUS_HANDLER_RESULT_HANDLED;
3513 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
3514 dbus_message_get_type (message),
3515 dbus_message_get_interface (message) ?
3516 dbus_message_get_interface (message) :
3518 dbus_message_get_member (message) ?
3519 dbus_message_get_member (message) :
3521 dbus_message_get_signature (message),
3525 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
3527 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
3529 /* Put message back, and we'll start over.
3530 * Yes this means handlers must be idempotent if they
3531 * don't return HANDLED; c'est la vie.
3533 _dbus_connection_putback_message_link_unlocked (connection,
3538 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
3540 if (connection->exit_on_disconnect &&
3541 dbus_message_is_signal (message,
3542 DBUS_INTERFACE_LOCAL,
3545 _dbus_verbose ("Exiting on Disconnected signal\n");
3546 CONNECTION_UNLOCK (connection);
3548 _dbus_assert_not_reached ("Call to exit() returned");
3551 _dbus_list_free_link (message_link);
3552 dbus_message_unref (message); /* don't want the message to count in max message limits
3553 * in computing dispatch status below
3557 _dbus_connection_release_dispatch (connection);
3558 HAVE_LOCK_CHECK (connection);
3560 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
3561 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3563 /* unlocks and calls user code */
3564 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3566 dbus_connection_unref (connection);
3572 * Sets the watch functions for the connection. These functions are
3573 * responsible for making the application's main loop aware of file
3574 * descriptors that need to be monitored for events, using select() or
3575 * poll(). When using Qt, typically the DBusAddWatchFunction would
3576 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
3577 * could call g_io_add_watch(), or could be used as part of a more
3578 * elaborate GSource. Note that when a watch is added, it may
3581 * The DBusWatchToggledFunction notifies the application that the
3582 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
3583 * to check this. A disabled watch should have no effect, and enabled
3584 * watch should be added to the main loop. This feature is used
3585 * instead of simply adding/removing the watch because
3586 * enabling/disabling can be done without memory allocation. The
3587 * toggled function may be NULL if a main loop re-queries
3588 * dbus_watch_get_enabled() every time anyway.
3590 * The DBusWatch can be queried for the file descriptor to watch using
3591 * dbus_watch_get_fd(), and for the events to watch for using
3592 * dbus_watch_get_flags(). The flags returned by
3593 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
3594 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
3595 * all watches implicitly include a watch for hangups, errors, and
3596 * other exceptional conditions.
3598 * Once a file descriptor becomes readable or writable, or an exception
3599 * occurs, dbus_watch_handle() should be called to
3600 * notify the connection of the file descriptor's condition.
3602 * dbus_watch_handle() cannot be called during the
3603 * DBusAddWatchFunction, as the connection will not be ready to handle
3606 * It is not allowed to reference a DBusWatch after it has been passed
3607 * to remove_function.
3609 * If #FALSE is returned due to lack of memory, the failure may be due
3610 * to a #FALSE return from the new add_function. If so, the
3611 * add_function may have been called successfully one or more times,
3612 * but the remove_function will also have been called to remove any
3613 * successful adds. i.e. if #FALSE is returned the net result
3614 * should be that dbus_connection_set_watch_functions() has no effect,
3615 * but the add_function and remove_function may have been called.
3617 * @todo We need to drop the lock when we call the
3618 * add/remove/toggled functions which can be a side effect
3619 * of setting the watch functions.
3621 * @param connection the connection.
3622 * @param add_function function to begin monitoring a new descriptor.
3623 * @param remove_function function to stop monitoring a descriptor.
3624 * @param toggled_function function to notify of enable/disable
3625 * @param data data to pass to add_function and remove_function.
3626 * @param free_data_function function to be called to free the data.
3627 * @returns #FALSE on failure (no memory)
3630 dbus_connection_set_watch_functions (DBusConnection *connection,
3631 DBusAddWatchFunction add_function,
3632 DBusRemoveWatchFunction remove_function,
3633 DBusWatchToggledFunction toggled_function,
3635 DBusFreeFunction free_data_function)
3638 DBusWatchList *watches;
3640 _dbus_return_val_if_fail (connection != NULL, FALSE);
3642 CONNECTION_LOCK (connection);
3644 #ifndef DBUS_DISABLE_CHECKS
3645 if (connection->watches == NULL)
3647 _dbus_warn ("Re-entrant call to %s is not allowed\n",
3648 _DBUS_FUNCTION_NAME);
3653 /* ref connection for slightly better reentrancy */
3654 _dbus_connection_ref_unlocked (connection);
3656 /* This can call back into user code, and we need to drop the
3657 * connection lock when it does. This is kind of a lame
3660 watches = connection->watches;
3661 connection->watches = NULL;
3662 CONNECTION_UNLOCK (connection);
3664 retval = _dbus_watch_list_set_functions (watches,
3665 add_function, remove_function,
3667 data, free_data_function);
3668 CONNECTION_LOCK (connection);
3669 connection->watches = watches;
3671 CONNECTION_UNLOCK (connection);
3672 /* drop our paranoid refcount */
3673 dbus_connection_unref (connection);
3679 * Sets the timeout functions for the connection. These functions are
3680 * responsible for making the application's main loop aware of timeouts.
3681 * When using Qt, typically the DBusAddTimeoutFunction would create a
3682 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
3685 * The DBusTimeoutToggledFunction notifies the application that the
3686 * timeout has been enabled or disabled. Call
3687 * dbus_timeout_get_enabled() to check this. A disabled timeout should
3688 * have no effect, and enabled timeout should be added to the main
3689 * loop. This feature is used instead of simply adding/removing the
3690 * timeout because enabling/disabling can be done without memory
3691 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
3692 * to enable and disable. The toggled function may be NULL if a main
3693 * loop re-queries dbus_timeout_get_enabled() every time anyway.
3694 * Whenever a timeout is toggled, its interval may change.
3696 * The DBusTimeout can be queried for the timer interval using
3697 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
3698 * repeatedly, each time the interval elapses, starting after it has
3699 * elapsed once. The timeout stops firing when it is removed with the
3700 * given remove_function. The timer interval may change whenever the
3701 * timeout is added, removed, or toggled.
3703 * @param connection the connection.
3704 * @param add_function function to add a timeout.
3705 * @param remove_function function to remove a timeout.
3706 * @param toggled_function function to notify of enable/disable
3707 * @param data data to pass to add_function and remove_function.
3708 * @param free_data_function function to be called to free the data.
3709 * @returns #FALSE on failure (no memory)
3712 dbus_connection_set_timeout_functions (DBusConnection *connection,
3713 DBusAddTimeoutFunction add_function,
3714 DBusRemoveTimeoutFunction remove_function,
3715 DBusTimeoutToggledFunction toggled_function,
3717 DBusFreeFunction free_data_function)
3720 DBusTimeoutList *timeouts;
3722 _dbus_return_val_if_fail (connection != NULL, FALSE);
3724 CONNECTION_LOCK (connection);
3726 #ifndef DBUS_DISABLE_CHECKS
3727 if (connection->timeouts == NULL)
3729 _dbus_warn ("Re-entrant call to %s is not allowed\n",
3730 _DBUS_FUNCTION_NAME);
3735 /* ref connection for slightly better reentrancy */
3736 _dbus_connection_ref_unlocked (connection);
3738 timeouts = connection->timeouts;
3739 connection->timeouts = NULL;
3740 CONNECTION_UNLOCK (connection);
3742 retval = _dbus_timeout_list_set_functions (timeouts,
3743 add_function, remove_function,
3745 data, free_data_function);
3746 CONNECTION_LOCK (connection);
3747 connection->timeouts = timeouts;
3749 CONNECTION_UNLOCK (connection);
3750 /* drop our paranoid refcount */
3751 dbus_connection_unref (connection);
3757 * Sets the mainloop wakeup function for the connection. Thi function is
3758 * responsible for waking up the main loop (if its sleeping) when some some
3759 * change has happened to the connection that the mainloop needs to reconsiders
3760 * (e.g. a message has been queued for writing).
3761 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
3762 * When using GLib, it would call g_main_context_wakeup().
3765 * @param connection the connection.
3766 * @param wakeup_main_function function to wake up the mainloop
3767 * @param data data to pass wakeup_main_function
3768 * @param free_data_function function to be called to free the data.
3771 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
3772 DBusWakeupMainFunction wakeup_main_function,
3774 DBusFreeFunction free_data_function)
3777 DBusFreeFunction old_free_data;
3779 _dbus_return_if_fail (connection != NULL);
3781 CONNECTION_LOCK (connection);
3782 old_data = connection->wakeup_main_data;
3783 old_free_data = connection->free_wakeup_main_data;
3785 connection->wakeup_main_function = wakeup_main_function;
3786 connection->wakeup_main_data = data;
3787 connection->free_wakeup_main_data = free_data_function;
3789 CONNECTION_UNLOCK (connection);
3791 /* Callback outside the lock */
3793 (*old_free_data) (old_data);
3797 * Set a function to be invoked when the dispatch status changes.
3798 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
3799 * dbus_connection_dispatch() needs to be called to process incoming
3800 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
3801 * from inside the DBusDispatchStatusFunction. Indeed, almost
3802 * any reentrancy in this function is a bad idea. Instead,
3803 * the DBusDispatchStatusFunction should simply save an indication
3804 * that messages should be dispatched later, when the main loop
3807 * @param connection the connection
3808 * @param function function to call on dispatch status changes
3809 * @param data data for function
3810 * @param free_data_function free the function data
3813 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
3814 DBusDispatchStatusFunction function,
3816 DBusFreeFunction free_data_function)
3819 DBusFreeFunction old_free_data;
3821 _dbus_return_if_fail (connection != NULL);
3823 CONNECTION_LOCK (connection);
3824 old_data = connection->dispatch_status_data;
3825 old_free_data = connection->free_dispatch_status_data;
3827 connection->dispatch_status_function = function;
3828 connection->dispatch_status_data = data;
3829 connection->free_dispatch_status_data = free_data_function;
3831 CONNECTION_UNLOCK (connection);
3833 /* Callback outside the lock */
3835 (*old_free_data) (old_data);
3839 * Get the UNIX file descriptor of the connection, if any. This can
3840 * be used for SELinux access control checks with getpeercon() for
3841 * example. DO NOT read or write to the file descriptor, or try to
3842 * select() on it; use DBusWatch for main loop integration. Not all
3843 * connections will have a file descriptor. So for adding descriptors
3844 * to the main loop, use dbus_watch_get_fd() and so forth.
3846 * @param connection the connection
3847 * @param fd return location for the file descriptor.
3848 * @returns #TRUE if fd is successfully obtained.
3851 dbus_connection_get_unix_fd (DBusConnection *connection,
3856 _dbus_return_val_if_fail (connection != NULL, FALSE);
3857 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
3859 CONNECTION_LOCK (connection);
3861 retval = _dbus_transport_get_unix_fd (connection->transport,
3864 CONNECTION_UNLOCK (connection);
3870 * Gets the UNIX user ID of the connection if any.
3871 * Returns #TRUE if the uid is filled in.
3872 * Always returns #FALSE on non-UNIX platforms.
3873 * Always returns #FALSE prior to authenticating the
3876 * @param connection the connection
3877 * @param uid return location for the user ID
3878 * @returns #TRUE if uid is filled in with a valid user ID
3881 dbus_connection_get_unix_user (DBusConnection *connection,
3886 _dbus_return_val_if_fail (connection != NULL, FALSE);
3887 _dbus_return_val_if_fail (uid != NULL, FALSE);
3889 CONNECTION_LOCK (connection);
3891 if (!_dbus_transport_get_is_authenticated (connection->transport))
3894 result = _dbus_transport_get_unix_user (connection->transport,
3896 CONNECTION_UNLOCK (connection);
3902 * Gets the process ID of the connection if any.
3903 * Returns #TRUE if the uid is filled in.
3904 * Always returns #FALSE prior to authenticating the
3907 * @param connection the connection
3908 * @param pid return location for the process ID
3909 * @returns #TRUE if uid is filled in with a valid process ID
3912 dbus_connection_get_unix_process_id (DBusConnection *connection,
3917 _dbus_return_val_if_fail (connection != NULL, FALSE);
3918 _dbus_return_val_if_fail (pid != NULL, FALSE);
3920 CONNECTION_LOCK (connection);
3922 if (!_dbus_transport_get_is_authenticated (connection->transport))
3925 result = _dbus_transport_get_unix_process_id (connection->transport,
3927 CONNECTION_UNLOCK (connection);
3933 * Sets a predicate function used to determine whether a given user ID
3934 * is allowed to connect. When an incoming connection has
3935 * authenticated with a particular user ID, this function is called;
3936 * if it returns #TRUE, the connection is allowed to proceed,
3937 * otherwise the connection is disconnected.
3939 * If the function is set to #NULL (as it is by default), then
3940 * only the same UID as the server process will be allowed to
3943 * @param connection the connection
3944 * @param function the predicate
3945 * @param data data to pass to the predicate
3946 * @param free_data_function function to free the data
3949 dbus_connection_set_unix_user_function (DBusConnection *connection,
3950 DBusAllowUnixUserFunction function,
3952 DBusFreeFunction free_data_function)
3954 void *old_data = NULL;
3955 DBusFreeFunction old_free_function = NULL;
3957 _dbus_return_if_fail (connection != NULL);
3959 CONNECTION_LOCK (connection);
3960 _dbus_transport_set_unix_user_function (connection->transport,
3961 function, data, free_data_function,
3962 &old_data, &old_free_function);
3963 CONNECTION_UNLOCK (connection);
3965 if (old_free_function != NULL)
3966 (* old_free_function) (old_data);
3970 * Adds a message filter. Filters are handlers that are run on all
3971 * incoming messages, prior to the objects registered with
3972 * dbus_connection_register_object_path(). Filters are run in the
3973 * order that they were added. The same handler can be added as a
3974 * filter more than once, in which case it will be run more than once.
3975 * Filters added during a filter callback won't be run on the message
3978 * @todo we don't run filters on messages while blocking without
3979 * entering the main loop, since filters are run as part of
3980 * dbus_connection_dispatch(). This is probably a feature, as filters
3981 * could create arbitrary reentrancy. But kind of sucks if you're
3982 * trying to filter METHOD_RETURN for some reason.
3984 * @param connection the connection
3985 * @param function function to handle messages
3986 * @param user_data user data to pass to the function
3987 * @param free_data_function function to use for freeing user data
3988 * @returns #TRUE on success, #FALSE if not enough memory.
3991 dbus_connection_add_filter (DBusConnection *connection,
3992 DBusHandleMessageFunction function,
3994 DBusFreeFunction free_data_function)
3996 DBusMessageFilter *filter;
3998 _dbus_return_val_if_fail (connection != NULL, FALSE);
3999 _dbus_return_val_if_fail (function != NULL, FALSE);
4001 filter = dbus_new0 (DBusMessageFilter, 1);
4005 filter->refcount.value = 1;
4007 CONNECTION_LOCK (connection);
4009 if (!_dbus_list_append (&connection->filter_list,
4012 _dbus_message_filter_unref (filter);
4013 CONNECTION_UNLOCK (connection);
4017 /* Fill in filter after all memory allocated,
4018 * so we don't run the free_user_data_function
4019 * if the add_filter() fails
4022 filter->function = function;
4023 filter->user_data = user_data;
4024 filter->free_user_data_function = free_data_function;
4026 CONNECTION_UNLOCK (connection);
4031 * Removes a previously-added message filter. It is a programming
4032 * error to call this function for a handler that has not been added
4033 * as a filter. If the given handler was added more than once, only
4034 * one instance of it will be removed (the most recently-added
4037 * @param connection the connection
4038 * @param function the handler to remove
4039 * @param user_data user data for the handler to remove
4043 dbus_connection_remove_filter (DBusConnection *connection,
4044 DBusHandleMessageFunction function,
4048 DBusMessageFilter *filter;
4050 _dbus_return_if_fail (connection != NULL);
4051 _dbus_return_if_fail (function != NULL);
4053 CONNECTION_LOCK (connection);
4057 link = _dbus_list_get_last_link (&connection->filter_list);
4058 while (link != NULL)
4060 filter = link->data;
4062 if (filter->function == function &&
4063 filter->user_data == user_data)
4065 _dbus_list_remove_link (&connection->filter_list, link);
4066 filter->function = NULL;
4071 link = _dbus_list_get_prev_link (&connection->filter_list, link);
4074 CONNECTION_UNLOCK (connection);
4076 #ifndef DBUS_DISABLE_CHECKS
4079 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
4080 function, user_data);
4085 /* Call application code */
4086 if (filter->free_user_data_function)
4087 (* filter->free_user_data_function) (filter->user_data);
4089 filter->free_user_data_function = NULL;
4090 filter->user_data = NULL;
4092 _dbus_message_filter_unref (filter);
4096 * Registers a handler for a given path in the object hierarchy.
4097 * The given vtable handles messages sent to exactly the given path.
4100 * @param connection the connection
4101 * @param path a '/' delimited string of path elements
4102 * @param vtable the virtual table
4103 * @param user_data data to pass to functions in the vtable
4104 * @returns #FALSE if not enough memory
4107 dbus_connection_register_object_path (DBusConnection *connection,
4109 const DBusObjectPathVTable *vtable,
4112 char **decomposed_path;
4115 _dbus_return_val_if_fail (connection != NULL, FALSE);
4116 _dbus_return_val_if_fail (path != NULL, FALSE);
4117 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4118 _dbus_return_val_if_fail (vtable != NULL, FALSE);
4120 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4123 CONNECTION_LOCK (connection);
4125 retval = _dbus_object_tree_register (connection->objects,
4127 (const char **) decomposed_path, vtable,
4130 CONNECTION_UNLOCK (connection);
4132 dbus_free_string_array (decomposed_path);
4138 * Registers a fallback handler for a given subsection of the object
4139 * hierarchy. The given vtable handles messages at or below the given
4140 * path. You can use this to establish a default message handling
4141 * policy for a whole "subdirectory."
4143 * @param connection the connection
4144 * @param path a '/' delimited string of path elements
4145 * @param vtable the virtual table
4146 * @param user_data data to pass to functions in the vtable
4147 * @returns #FALSE if not enough memory
4150 dbus_connection_register_fallback (DBusConnection *connection,
4152 const DBusObjectPathVTable *vtable,
4155 char **decomposed_path;
4158 _dbus_return_val_if_fail (connection != NULL, FALSE);
4159 _dbus_return_val_if_fail (path != NULL, FALSE);
4160 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4161 _dbus_return_val_if_fail (vtable != NULL, FALSE);
4163 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4166 CONNECTION_LOCK (connection);
4168 retval = _dbus_object_tree_register (connection->objects,
4170 (const char **) decomposed_path, vtable,
4173 CONNECTION_UNLOCK (connection);
4175 dbus_free_string_array (decomposed_path);
4181 * Unregisters the handler registered with exactly the given path.
4182 * It's a bug to call this function for a path that isn't registered.
4183 * Can unregister both fallback paths and object paths.
4185 * @param connection the connection
4186 * @param path a '/' delimited string of path elements
4187 * @returns #FALSE if not enough memory
4190 dbus_connection_unregister_object_path (DBusConnection *connection,
4193 char **decomposed_path;
4195 _dbus_return_val_if_fail (connection != NULL, FALSE);
4196 _dbus_return_val_if_fail (path != NULL, FALSE);
4197 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4199 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4202 CONNECTION_LOCK (connection);
4204 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
4206 dbus_free_string_array (decomposed_path);
4212 * Lists the registered fallback handlers and object path handlers at
4213 * the given parent_path. The returned array should be freed with
4214 * dbus_free_string_array().
4216 * @param connection the connection
4217 * @param parent_path the path to list the child handlers of
4218 * @param child_entries returns #NULL-terminated array of children
4219 * @returns #FALSE if no memory to allocate the child entries
4222 dbus_connection_list_registered (DBusConnection *connection,
4223 const char *parent_path,
4224 char ***child_entries)
4226 char **decomposed_path;
4228 _dbus_return_val_if_fail (connection != NULL, FALSE);
4229 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
4230 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
4231 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
4233 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
4236 CONNECTION_LOCK (connection);
4238 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
4239 (const char **) decomposed_path,
4241 dbus_free_string_array (decomposed_path);
4246 static DBusDataSlotAllocator slot_allocator;
4247 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
4250 * Allocates an integer ID to be used for storing application-specific
4251 * data on any DBusConnection. The allocated ID may then be used
4252 * with dbus_connection_set_data() and dbus_connection_get_data().
4253 * The passed-in slot must be initialized to -1, and is filled in
4254 * with the slot ID. If the passed-in slot is not -1, it's assumed
4255 * to be already allocated, and its refcount is incremented.
4257 * The allocated slot is global, i.e. all DBusConnection objects will
4258 * have a slot with the given integer ID reserved.
4260 * @param slot_p address of a global variable storing the slot
4261 * @returns #FALSE on failure (no memory)
4264 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
4266 return _dbus_data_slot_allocator_alloc (&slot_allocator,
4267 _DBUS_LOCK_NAME (connection_slots),
4272 * Deallocates a global ID for connection data slots.
4273 * dbus_connection_get_data() and dbus_connection_set_data() may no
4274 * longer be used with this slot. Existing data stored on existing
4275 * DBusConnection objects will be freed when the connection is
4276 * finalized, but may not be retrieved (and may only be replaced if
4277 * someone else reallocates the slot). When the refcount on the
4278 * passed-in slot reaches 0, it is set to -1.
4280 * @param slot_p address storing the slot to deallocate
4283 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
4285 _dbus_return_if_fail (*slot_p >= 0);
4287 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
4291 * Stores a pointer on a DBusConnection, along
4292 * with an optional function to be used for freeing
4293 * the data when the data is set again, or when
4294 * the connection is finalized. The slot number
4295 * must have been allocated with dbus_connection_allocate_data_slot().
4297 * @param connection the connection
4298 * @param slot the slot number
4299 * @param data the data to store
4300 * @param free_data_func finalizer function for the data
4301 * @returns #TRUE if there was enough memory to store the data
4304 dbus_connection_set_data (DBusConnection *connection,
4307 DBusFreeFunction free_data_func)
4309 DBusFreeFunction old_free_func;
4313 _dbus_return_val_if_fail (connection != NULL, FALSE);
4314 _dbus_return_val_if_fail (slot >= 0, FALSE);
4316 CONNECTION_LOCK (connection);
4318 retval = _dbus_data_slot_list_set (&slot_allocator,
4319 &connection->slot_list,
4320 slot, data, free_data_func,
4321 &old_free_func, &old_data);
4323 CONNECTION_UNLOCK (connection);
4327 /* Do the actual free outside the connection lock */
4329 (* old_free_func) (old_data);
4336 * Retrieves data previously set with dbus_connection_set_data().
4337 * The slot must still be allocated (must not have been freed).
4339 * @param connection the connection
4340 * @param slot the slot to get data from
4341 * @returns the data, or #NULL if not found
4344 dbus_connection_get_data (DBusConnection *connection,
4349 _dbus_return_val_if_fail (connection != NULL, NULL);
4351 CONNECTION_LOCK (connection);
4353 res = _dbus_data_slot_list_get (&slot_allocator,
4354 &connection->slot_list,
4357 CONNECTION_UNLOCK (connection);
4363 * This function sets a global flag for whether dbus_connection_new()
4364 * will set SIGPIPE behavior to SIG_IGN.
4366 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
4369 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
4371 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
4375 * Specifies the maximum size message this connection is allowed to
4376 * receive. Larger messages will result in disconnecting the
4379 * @param connection a #DBusConnection
4380 * @param size maximum message size the connection can receive, in bytes
4383 dbus_connection_set_max_message_size (DBusConnection *connection,
4386 _dbus_return_if_fail (connection != NULL);
4388 CONNECTION_LOCK (connection);
4389 _dbus_transport_set_max_message_size (connection->transport,
4391 CONNECTION_UNLOCK (connection);
4395 * Gets the value set by dbus_connection_set_max_message_size().
4397 * @param connection the connection
4398 * @returns the max size of a single message
4401 dbus_connection_get_max_message_size (DBusConnection *connection)
4405 _dbus_return_val_if_fail (connection != NULL, 0);
4407 CONNECTION_LOCK (connection);
4408 res = _dbus_transport_get_max_message_size (connection->transport);
4409 CONNECTION_UNLOCK (connection);
4414 * Sets the maximum total number of bytes that can be used for all messages
4415 * received on this connection. Messages count toward the maximum until
4416 * they are finalized. When the maximum is reached, the connection will
4417 * not read more data until some messages are finalized.
4419 * The semantics of the maximum are: if outstanding messages are
4420 * already above the maximum, additional messages will not be read.
4421 * The semantics are not: if the next message would cause us to exceed
4422 * the maximum, we don't read it. The reason is that we don't know the
4423 * size of a message until after we read it.
4425 * Thus, the max live messages size can actually be exceeded
4426 * by up to the maximum size of a single message.
4428 * Also, if we read say 1024 bytes off the wire in a single read(),
4429 * and that contains a half-dozen small messages, we may exceed the
4430 * size max by that amount. But this should be inconsequential.
4432 * This does imply that we can't call read() with a buffer larger
4433 * than we're willing to exceed this limit by.
4435 * @param connection the connection
4436 * @param size the maximum size in bytes of all outstanding messages
4439 dbus_connection_set_max_received_size (DBusConnection *connection,
4442 _dbus_return_if_fail (connection != NULL);
4444 CONNECTION_LOCK (connection);
4445 _dbus_transport_set_max_received_size (connection->transport,
4447 CONNECTION_UNLOCK (connection);
4451 * Gets the value set by dbus_connection_set_max_received_size().
4453 * @param connection the connection
4454 * @returns the max size of all live messages
4457 dbus_connection_get_max_received_size (DBusConnection *connection)
4461 _dbus_return_val_if_fail (connection != NULL, 0);
4463 CONNECTION_LOCK (connection);
4464 res = _dbus_transport_get_max_received_size (connection->transport);
4465 CONNECTION_UNLOCK (connection);
4470 * Gets the approximate size in bytes of all messages in the outgoing
4471 * message queue. The size is approximate in that you shouldn't use
4472 * it to decide how many bytes to read off the network or anything
4473 * of that nature, as optimizations may choose to tell small white lies
4474 * to avoid performance overhead.
4476 * @param connection the connection
4477 * @returns the number of bytes that have been queued up but not sent
4480 dbus_connection_get_outgoing_size (DBusConnection *connection)
4484 _dbus_return_val_if_fail (connection != NULL, 0);
4486 CONNECTION_LOCK (connection);
4487 res = _dbus_counter_get_value (connection->outgoing_counter);
4488 CONNECTION_UNLOCK (connection);