1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002-2006 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-shared.h"
26 #include "dbus-connection.h"
27 #include "dbus-list.h"
28 #include "dbus-timeout.h"
29 #include "dbus-transport.h"
30 #include "dbus-watch.h"
31 #include "dbus-connection-internal.h"
32 #include "dbus-pending-call-internal.h"
33 #include "dbus-list.h"
34 #include "dbus-hash.h"
35 #include "dbus-message-internal.h"
36 #include "dbus-threads.h"
37 #include "dbus-protocol.h"
38 #include "dbus-dataslot.h"
39 #include "dbus-string.h"
40 #include "dbus-pending-call.h"
41 #include "dbus-object-tree.h"
42 #include "dbus-threads-internal.h"
45 #ifdef DBUS_DISABLE_CHECKS
46 #define TOOK_LOCK_CHECK(connection)
47 #define RELEASING_LOCK_CHECK(connection)
48 #define HAVE_LOCK_CHECK(connection)
50 #define TOOK_LOCK_CHECK(connection) do { \
51 _dbus_assert (!(connection)->have_connection_lock); \
52 (connection)->have_connection_lock = TRUE; \
54 #define RELEASING_LOCK_CHECK(connection) do { \
55 _dbus_assert ((connection)->have_connection_lock); \
56 (connection)->have_connection_lock = FALSE; \
58 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
59 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
64 #define CONNECTION_LOCK(connection) do { \
65 if (TRACE_LOCKS) { _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); } \
66 _dbus_mutex_lock ((connection)->mutex); \
67 TOOK_LOCK_CHECK (connection); \
70 #define CONNECTION_UNLOCK(connection) do { \
71 if (TRACE_LOCKS) { _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); } \
72 RELEASING_LOCK_CHECK (connection); \
73 _dbus_mutex_unlock ((connection)->mutex); \
76 #define DISPATCH_STATUS_NAME(s) \
77 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
78 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
79 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
83 * @defgroup DBusConnection DBusConnection
85 * @brief Connection to another application
87 * A DBusConnection represents a connection to another
88 * application. Messages can be sent and received via this connection.
89 * The other application may be a message bus; for convenience, the
90 * function dbus_bus_get() is provided to automatically open a
91 * connection to the well-known message buses.
93 * In brief a DBusConnection is a message queue associated with some
94 * message transport mechanism such as a socket. The connection
95 * maintains a queue of incoming messages and a queue of outgoing
98 * Incoming messages are normally processed by calling
99 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
100 * handlers registered for the topmost message in the message queue,
101 * then discards the message, then returns.
103 * dbus_connection_get_dispatch_status() indicates whether
104 * messages are currently in the queue that need dispatching.
105 * dbus_connection_set_dispatch_status_function() allows
106 * you to set a function to be used to monitor the dispatch status.
108 * If you're using GLib or Qt add-on libraries for D-Bus, there are
109 * special convenience APIs in those libraries that hide
110 * all the details of dispatch and watch/timeout monitoring.
111 * For example, dbus_connection_setup_with_g_main().
113 * If you aren't using these add-on libraries, you have to manually
114 * call dbus_connection_set_dispatch_status_function(),
115 * dbus_connection_set_watch_functions(),
116 * dbus_connection_set_timeout_functions() providing appropriate
117 * functions to integrate the connection with your application's main
120 * When you use dbus_connection_send() or one of its variants to send
121 * a message, the message is added to the outgoing queue. It's
122 * actually written to the network later; either in
123 * dbus_watch_handle() invoked by your main loop, or in
124 * dbus_connection_flush() which blocks until it can write out the
125 * entire outgoing queue. The GLib/Qt add-on libraries again
126 * handle the details here for you by setting up watch functions.
128 * When a connection is disconnected, you are guaranteed to get a
129 * signal "Disconnected" from the interface
130 * #DBUS_INTERFACE_LOCAL, path
133 * You may not drop the last reference to a #DBusConnection
134 * until that connection has been disconnected.
136 * You may dispatch the unprocessed incoming message queue even if the
137 * connection is disconnected. However, "Disconnected" will always be
138 * the last message in the queue (obviously no messages are received
139 * after disconnection).
141 * #DBusConnection has thread locks and drops them when invoking user
142 * callbacks, so in general is transparently threadsafe. However,
143 * #DBusMessage does NOT have thread locks; you must not send the same
144 * message to multiple #DBusConnection that will be used from
149 * @defgroup DBusConnectionInternals DBusConnection implementation details
150 * @ingroup DBusInternals
151 * @brief Implementation details of DBusConnection
157 * Internal struct representing a message filter function
159 typedef struct DBusMessageFilter DBusMessageFilter;
162 * Internal struct representing a message filter function
164 struct DBusMessageFilter
166 DBusAtomic refcount; /**< Reference count */
167 DBusHandleMessageFunction function; /**< Function to call to filter */
168 void *user_data; /**< User data for the function */
169 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
174 * Internals of DBusPreallocatedSend
176 struct DBusPreallocatedSend
178 DBusConnection *connection; /**< Connection we'd send the message to */
179 DBusList *queue_link; /**< Preallocated link in the queue */
180 DBusList *counter_link; /**< Preallocated link in the resource counter */
183 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
186 * Implementation details of DBusConnection. All fields are private.
188 struct DBusConnection
190 DBusAtomic refcount; /**< Reference count. */
192 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
194 DBusMutex *dispatch_mutex; /**< Protects dispatch_acquired */
195 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */
196 DBusMutex *io_path_mutex; /**< Protects io_path_acquired */
197 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */
199 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
200 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
202 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
203 * dispatch_acquired will be set by the borrower
206 int n_outgoing; /**< Length of outgoing queue. */
207 int n_incoming; /**< Length of incoming queue. */
209 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
211 DBusTransport *transport; /**< Object that sends/receives messages over network. */
212 DBusWatchList *watches; /**< Stores active watches. */
213 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
215 DBusList *filter_list; /**< List of filters. */
217 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
219 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
221 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
222 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
224 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
225 void *wakeup_main_data; /**< Application data for wakeup_main_function */
226 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
228 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
229 void *dispatch_status_data; /**< Application data for dispatch_status_function */
230 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
232 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
234 DBusList *link_cache; /**< A cache of linked list links to prevent contention
235 * for the global linked list mempool lock
237 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
239 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
241 unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */
243 unsigned int dispatch_acquired : 1; /**< Someone has dispatch path (can drain incoming queue) */
244 unsigned int io_path_acquired : 1; /**< Someone has transport io path (can use the transport to read/write messages) */
246 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
248 unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */
250 unsigned int disconnected_message_arrived : 1; /**< We popped or are dispatching the disconnected message.
251 * if the disconnect_message_link is NULL then we queued it, but
252 * this flag is whether it got to the head of the queue.
254 unsigned int disconnected_message_processed : 1; /**< We did our default handling of the disconnected message,
255 * such as closing the connection.
258 #ifndef DBUS_DISABLE_CHECKS
259 unsigned int have_connection_lock : 1; /**< Used to check locking */
262 #ifndef DBUS_DISABLE_CHECKS
263 int generation; /**< _dbus_current_generation that should correspond to this connection */
267 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
268 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
269 DBusDispatchStatus new_status);
270 static void _dbus_connection_last_unref (DBusConnection *connection);
271 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
272 static void _dbus_connection_release_dispatch (DBusConnection *connection);
273 static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
274 static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection);
275 static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection);
277 static DBusMessageFilter *
278 _dbus_message_filter_ref (DBusMessageFilter *filter)
280 _dbus_assert (filter->refcount.value > 0);
281 _dbus_atomic_inc (&filter->refcount);
287 _dbus_message_filter_unref (DBusMessageFilter *filter)
289 _dbus_assert (filter->refcount.value > 0);
291 if (_dbus_atomic_dec (&filter->refcount) == 1)
293 if (filter->free_user_data_function)
294 (* filter->free_user_data_function) (filter->user_data);
301 * Acquires the connection lock.
303 * @param connection the connection.
306 _dbus_connection_lock (DBusConnection *connection)
308 CONNECTION_LOCK (connection);
312 * Releases the connection lock.
314 * @param connection the connection.
317 _dbus_connection_unlock (DBusConnection *connection)
319 CONNECTION_UNLOCK (connection);
323 * Wakes up the main loop if it is sleeping
324 * Needed if we're e.g. queueing outgoing messages
325 * on a thread while the mainloop sleeps.
327 * @param connection the connection.
330 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
332 if (connection->wakeup_main_function)
333 (*connection->wakeup_main_function) (connection->wakeup_main_data);
336 #ifdef DBUS_BUILD_TESTS
337 /* For now this function isn't used */
339 * Adds a message to the incoming message queue, returning #FALSE
340 * if there's insufficient memory to queue the message.
341 * Does not take over refcount of the message.
343 * @param connection the connection.
344 * @param message the message to queue.
345 * @returns #TRUE on success.
348 _dbus_connection_queue_received_message (DBusConnection *connection,
349 DBusMessage *message)
353 link = _dbus_list_alloc_link (message);
357 dbus_message_ref (message);
358 _dbus_connection_queue_received_message_link (connection, link);
364 * Gets the locks so we can examine them
366 * @param connection the connection.
367 * @param mutex_loc return for the location of the main mutex pointer
368 * @param dispatch_mutex_loc return location of the dispatch mutex pointer
369 * @param io_path_mutex_loc return location of the io_path mutex pointer
370 * @param dispatch_cond_loc return location of the dispatch conditional
372 * @param io_path_cond_loc return location of the io_path conditional
376 _dbus_connection_test_get_locks (DBusConnection *conn,
377 DBusMutex **mutex_loc,
378 DBusMutex **dispatch_mutex_loc,
379 DBusMutex **io_path_mutex_loc,
380 DBusCondVar **dispatch_cond_loc,
381 DBusCondVar **io_path_cond_loc)
383 *mutex_loc = conn->mutex;
384 *dispatch_mutex_loc = conn->dispatch_mutex;
385 *io_path_mutex_loc = conn->io_path_mutex;
386 *dispatch_cond_loc = conn->dispatch_cond;
387 *io_path_cond_loc = conn->io_path_cond;
392 * Adds a message-containing list link to the incoming message queue,
393 * taking ownership of the link and the message's current refcount.
394 * Cannot fail due to lack of memory.
396 * @param connection the connection.
397 * @param link the message link to queue.
400 _dbus_connection_queue_received_message_link (DBusConnection *connection,
403 DBusPendingCall *pending;
404 dbus_int32_t reply_serial;
405 DBusMessage *message;
407 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
409 _dbus_list_append_link (&connection->incoming_messages,
411 message = link->data;
413 /* If this is a reply we're waiting on, remove timeout for it */
414 reply_serial = dbus_message_get_reply_serial (message);
415 if (reply_serial != -1)
417 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
421 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
422 _dbus_connection_remove_timeout_unlocked (connection,
423 _dbus_pending_call_get_timeout_unlocked (pending));
425 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
431 connection->n_incoming += 1;
433 _dbus_connection_wakeup_mainloop (connection);
435 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
437 dbus_message_get_type (message),
438 dbus_message_get_path (message) ?
439 dbus_message_get_path (message) :
441 dbus_message_get_interface (message) ?
442 dbus_message_get_interface (message) :
444 dbus_message_get_member (message) ?
445 dbus_message_get_member (message) :
447 dbus_message_get_signature (message),
448 dbus_message_get_reply_serial (message),
450 connection->n_incoming);}
453 * Adds a link + message to the incoming message queue.
454 * Can't fail. Takes ownership of both link and message.
456 * @param connection the connection.
457 * @param link the list node and message to queue.
461 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
464 HAVE_LOCK_CHECK (connection);
466 _dbus_list_append_link (&connection->incoming_messages, link);
468 connection->n_incoming += 1;
470 _dbus_connection_wakeup_mainloop (connection);
472 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
473 link->data, connection, connection->n_incoming);
478 * Checks whether there are messages in the outgoing message queue.
479 * Called with connection lock held.
481 * @param connection the connection.
482 * @returns #TRUE if the outgoing queue is non-empty.
485 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
487 HAVE_LOCK_CHECK (connection);
488 return connection->outgoing_messages != NULL;
492 * Checks whether there are messages in the outgoing message queue.
494 * @param connection the connection.
495 * @returns #TRUE if the outgoing queue is non-empty.
498 dbus_connection_has_messages_to_send (DBusConnection *connection)
502 _dbus_return_val_if_fail (connection != NULL, FALSE);
504 CONNECTION_LOCK (connection);
505 v = _dbus_connection_has_messages_to_send_unlocked (connection);
506 CONNECTION_UNLOCK (connection);
512 * Gets the next outgoing message. The message remains in the
513 * queue, and the caller does not own a reference to it.
515 * @param connection the connection.
516 * @returns the message to be sent.
519 _dbus_connection_get_message_to_send (DBusConnection *connection)
521 HAVE_LOCK_CHECK (connection);
523 return _dbus_list_get_last (&connection->outgoing_messages);
527 * Notifies the connection that a message has been sent, so the
528 * message can be removed from the outgoing queue.
529 * Called with the connection lock held.
531 * @param connection the connection.
532 * @param message the message that was sent.
535 _dbus_connection_message_sent (DBusConnection *connection,
536 DBusMessage *message)
540 HAVE_LOCK_CHECK (connection);
542 /* This can be called before we even complete authentication, since
543 * it's called on disconnect to clean up the outgoing queue.
544 * It's also called as we successfully send each message.
547 link = _dbus_list_get_last_link (&connection->outgoing_messages);
548 _dbus_assert (link != NULL);
549 _dbus_assert (link->data == message);
551 /* Save this link in the link cache */
552 _dbus_list_unlink (&connection->outgoing_messages,
554 _dbus_list_prepend_link (&connection->link_cache, link);
556 connection->n_outgoing -= 1;
558 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
560 dbus_message_get_type (message),
561 dbus_message_get_path (message) ?
562 dbus_message_get_path (message) :
564 dbus_message_get_interface (message) ?
565 dbus_message_get_interface (message) :
567 dbus_message_get_member (message) ?
568 dbus_message_get_member (message) :
570 dbus_message_get_signature (message),
571 connection, connection->n_outgoing);
573 /* Save this link in the link cache also */
574 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
576 _dbus_list_prepend_link (&connection->link_cache, link);
578 dbus_message_unref (message);
581 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
583 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
585 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
587 dbus_bool_t enabled);
590 protected_change_watch (DBusConnection *connection,
592 DBusWatchAddFunction add_function,
593 DBusWatchRemoveFunction remove_function,
594 DBusWatchToggleFunction toggle_function,
597 DBusWatchList *watches;
600 HAVE_LOCK_CHECK (connection);
602 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
603 * drop lock and call out" one; but it has to be propagated up through all callers
606 watches = connection->watches;
609 connection->watches = NULL;
610 _dbus_connection_ref_unlocked (connection);
611 CONNECTION_UNLOCK (connection);
614 retval = (* add_function) (watches, watch);
615 else if (remove_function)
618 (* remove_function) (watches, watch);
623 (* toggle_function) (watches, watch, enabled);
626 CONNECTION_LOCK (connection);
627 connection->watches = watches;
628 _dbus_connection_unref_unlocked (connection);
638 * Adds a watch using the connection's DBusAddWatchFunction if
639 * available. Otherwise records the watch to be added when said
640 * function is available. Also re-adds the watch if the
641 * DBusAddWatchFunction changes. May fail due to lack of memory.
642 * Connection lock should be held when calling this.
644 * @param connection the connection.
645 * @param watch the watch to add.
646 * @returns #TRUE on success.
649 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
652 return protected_change_watch (connection, watch,
653 _dbus_watch_list_add_watch,
658 * Removes a watch using the connection's DBusRemoveWatchFunction
659 * if available. It's an error to call this function on a watch
660 * that was not previously added.
661 * Connection lock should be held when calling this.
663 * @param connection the connection.
664 * @param watch the watch to remove.
667 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
670 protected_change_watch (connection, watch,
672 _dbus_watch_list_remove_watch,
677 * Toggles a watch and notifies app via connection's
678 * DBusWatchToggledFunction if available. It's an error to call this
679 * function on a watch that was not previously added.
680 * Connection lock should be held when calling this.
682 * @param connection the connection.
683 * @param watch the watch to toggle.
684 * @param enabled whether to enable or disable
687 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
691 _dbus_assert (watch != NULL);
693 protected_change_watch (connection, watch,
695 _dbus_watch_list_toggle_watch,
699 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
700 DBusTimeout *timeout);
701 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
702 DBusTimeout *timeout);
703 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
704 DBusTimeout *timeout,
705 dbus_bool_t enabled);
708 protected_change_timeout (DBusConnection *connection,
709 DBusTimeout *timeout,
710 DBusTimeoutAddFunction add_function,
711 DBusTimeoutRemoveFunction remove_function,
712 DBusTimeoutToggleFunction toggle_function,
715 DBusTimeoutList *timeouts;
718 HAVE_LOCK_CHECK (connection);
720 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
721 * drop lock and call out" one; but it has to be propagated up through all callers
724 timeouts = connection->timeouts;
727 connection->timeouts = NULL;
728 _dbus_connection_ref_unlocked (connection);
729 CONNECTION_UNLOCK (connection);
732 retval = (* add_function) (timeouts, timeout);
733 else if (remove_function)
736 (* remove_function) (timeouts, timeout);
741 (* toggle_function) (timeouts, timeout, enabled);
744 CONNECTION_LOCK (connection);
745 connection->timeouts = timeouts;
746 _dbus_connection_unref_unlocked (connection);
755 * Adds a timeout using the connection's DBusAddTimeoutFunction if
756 * available. Otherwise records the timeout to be added when said
757 * function is available. Also re-adds the timeout if the
758 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
759 * The timeout will fire repeatedly until removed.
760 * Connection lock should be held when calling this.
762 * @param connection the connection.
763 * @param timeout the timeout to add.
764 * @returns #TRUE on success.
767 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
768 DBusTimeout *timeout)
770 return protected_change_timeout (connection, timeout,
771 _dbus_timeout_list_add_timeout,
776 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
777 * if available. It's an error to call this function on a timeout
778 * that was not previously added.
779 * Connection lock should be held when calling this.
781 * @param connection the connection.
782 * @param timeout the timeout to remove.
785 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
786 DBusTimeout *timeout)
788 protected_change_timeout (connection, timeout,
790 _dbus_timeout_list_remove_timeout,
795 * Toggles a timeout and notifies app via connection's
796 * DBusTimeoutToggledFunction if available. It's an error to call this
797 * function on a timeout that was not previously added.
798 * Connection lock should be held when calling this.
800 * @param connection the connection.
801 * @param timeout the timeout to toggle.
802 * @param enabled whether to enable or disable
805 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
806 DBusTimeout *timeout,
809 protected_change_timeout (connection, timeout,
811 _dbus_timeout_list_toggle_timeout,
816 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
817 DBusPendingCall *pending)
819 dbus_uint32_t reply_serial;
820 DBusTimeout *timeout;
822 HAVE_LOCK_CHECK (connection);
824 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
826 _dbus_assert (reply_serial != 0);
828 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
830 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
833 if (!_dbus_hash_table_insert_int (connection->pending_replies,
837 _dbus_connection_remove_timeout_unlocked (connection, timeout);
839 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
840 HAVE_LOCK_CHECK (connection);
844 _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
846 _dbus_pending_call_ref_unlocked (pending);
848 HAVE_LOCK_CHECK (connection);
854 free_pending_call_on_hash_removal (void *data)
856 DBusPendingCall *pending;
857 DBusConnection *connection;
864 connection = _dbus_pending_call_get_connection_unlocked (pending);
866 HAVE_LOCK_CHECK (connection);
868 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
870 _dbus_connection_remove_timeout_unlocked (connection,
871 _dbus_pending_call_get_timeout_unlocked (pending));
873 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
876 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
877 * here, but the pending call finalizer could in principle call out to
878 * application code so we pretty much have to... some larger code reorg
881 _dbus_connection_ref_unlocked (connection);
882 _dbus_pending_call_unref_and_unlock (pending);
883 CONNECTION_LOCK (connection);
884 _dbus_connection_unref_unlocked (connection);
888 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
889 DBusPendingCall *pending)
891 /* This ends up unlocking to call the pending call finalizer, which is unexpected to
894 _dbus_hash_table_remove_int (connection->pending_replies,
895 _dbus_pending_call_get_reply_serial_unlocked (pending));
899 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
900 DBusPendingCall *pending)
902 /* The idea here is to avoid finalizing the pending call
903 * with the lock held, since there's a destroy notifier
904 * in pending call that goes out to application code.
906 * There's an extra unlock inside the hash table
907 * "free pending call" function FIXME...
909 _dbus_pending_call_ref_unlocked (pending);
910 _dbus_hash_table_remove_int (connection->pending_replies,
911 _dbus_pending_call_get_reply_serial_unlocked (pending));
912 _dbus_pending_call_unref_and_unlock (pending);
916 * Removes a pending call from the connection, such that
917 * the pending reply will be ignored. May drop the last
918 * reference to the pending call.
920 * @param connection the connection
921 * @param pending the pending call
924 _dbus_connection_remove_pending_call (DBusConnection *connection,
925 DBusPendingCall *pending)
927 CONNECTION_LOCK (connection);
928 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
932 * Acquire the transporter I/O path. This must be done before
933 * doing any I/O in the transporter. May sleep and drop the
934 * IO path mutex while waiting for the I/O path.
936 * @param connection the connection.
937 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
938 * @returns TRUE if the I/O path was acquired.
941 _dbus_connection_acquire_io_path (DBusConnection *connection,
942 int timeout_milliseconds)
944 dbus_bool_t we_acquired;
946 HAVE_LOCK_CHECK (connection);
948 /* We don't want the connection to vanish */
949 _dbus_connection_ref_unlocked (connection);
951 /* We will only touch io_path_acquired which is protected by our mutex */
952 CONNECTION_UNLOCK (connection);
954 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
955 _dbus_mutex_lock (connection->io_path_mutex);
957 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
958 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
962 if (connection->io_path_acquired)
964 if (timeout_milliseconds != -1)
966 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
967 _DBUS_FUNCTION_NAME, timeout_milliseconds);
968 _dbus_condvar_wait_timeout (connection->io_path_cond,
969 connection->io_path_mutex,
970 timeout_milliseconds);
974 while (connection->io_path_acquired)
976 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
977 _dbus_condvar_wait (connection->io_path_cond,
978 connection->io_path_mutex);
983 if (!connection->io_path_acquired)
986 connection->io_path_acquired = TRUE;
989 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
990 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
992 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
993 _dbus_mutex_unlock (connection->io_path_mutex);
995 CONNECTION_LOCK (connection);
997 HAVE_LOCK_CHECK (connection);
999 _dbus_connection_unref_unlocked (connection);
1005 * Release the I/O path when you're done with it. Only call
1006 * after you've acquired the I/O. Wakes up at most one thread
1007 * currently waiting to acquire the I/O path.
1009 * @param connection the connection.
1012 _dbus_connection_release_io_path (DBusConnection *connection)
1014 HAVE_LOCK_CHECK (connection);
1016 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1017 _dbus_mutex_lock (connection->io_path_mutex);
1019 _dbus_assert (connection->io_path_acquired);
1021 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
1022 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
1024 connection->io_path_acquired = FALSE;
1025 _dbus_condvar_wake_one (connection->io_path_cond);
1027 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1028 _dbus_mutex_unlock (connection->io_path_mutex);
1032 * Queues incoming messages and sends outgoing messages for this
1033 * connection, optionally blocking in the process. Each call to
1034 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1035 * time and then read or write data if possible.
1037 * The purpose of this function is to be able to flush outgoing
1038 * messages or queue up incoming messages without returning
1039 * control to the application and causing reentrancy weirdness.
1041 * The flags parameter allows you to specify whether to
1042 * read incoming messages, write outgoing messages, or both,
1043 * and whether to block if no immediate action is possible.
1045 * The timeout_milliseconds parameter does nothing unless the
1046 * iteration is blocking.
1048 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1049 * wasn't specified, then it's impossible to block, even if
1050 * you specify DBUS_ITERATION_BLOCK; in that case the function
1051 * returns immediately.
1053 * Called with connection lock held.
1055 * @param connection the connection.
1056 * @param flags iteration flags.
1057 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1060 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1062 int timeout_milliseconds)
1064 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1066 HAVE_LOCK_CHECK (connection);
1068 if (connection->n_outgoing == 0)
1069 flags &= ~DBUS_ITERATION_DO_WRITING;
1071 if (_dbus_connection_acquire_io_path (connection,
1072 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1074 HAVE_LOCK_CHECK (connection);
1076 _dbus_transport_do_iteration (connection->transport,
1077 flags, timeout_milliseconds);
1078 _dbus_connection_release_io_path (connection);
1081 HAVE_LOCK_CHECK (connection);
1083 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1087 * Creates a new connection for the given transport. A transport
1088 * represents a message stream that uses some concrete mechanism, such
1089 * as UNIX domain sockets. May return #NULL if insufficient
1090 * memory exists to create the connection.
1092 * @param transport the transport.
1093 * @returns the new connection, or #NULL on failure.
1096 _dbus_connection_new_for_transport (DBusTransport *transport)
1098 DBusConnection *connection;
1099 DBusWatchList *watch_list;
1100 DBusTimeoutList *timeout_list;
1101 DBusHashTable *pending_replies;
1102 DBusList *disconnect_link;
1103 DBusMessage *disconnect_message;
1104 DBusCounter *outgoing_counter;
1105 DBusObjectTree *objects;
1109 pending_replies = NULL;
1110 timeout_list = NULL;
1111 disconnect_link = NULL;
1112 disconnect_message = NULL;
1113 outgoing_counter = NULL;
1116 watch_list = _dbus_watch_list_new ();
1117 if (watch_list == NULL)
1120 timeout_list = _dbus_timeout_list_new ();
1121 if (timeout_list == NULL)
1125 _dbus_hash_table_new (DBUS_HASH_INT,
1127 (DBusFreeFunction)free_pending_call_on_hash_removal);
1128 if (pending_replies == NULL)
1131 connection = dbus_new0 (DBusConnection, 1);
1132 if (connection == NULL)
1135 _dbus_mutex_new_at_location (&connection->mutex);
1136 if (connection->mutex == NULL)
1139 _dbus_mutex_new_at_location (&connection->io_path_mutex);
1140 if (connection->io_path_mutex == NULL)
1143 _dbus_mutex_new_at_location (&connection->dispatch_mutex);
1144 if (connection->dispatch_mutex == NULL)
1147 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1148 if (connection->dispatch_cond == NULL)
1151 _dbus_condvar_new_at_location (&connection->io_path_cond);
1152 if (connection->io_path_cond == NULL)
1155 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1156 DBUS_INTERFACE_LOCAL,
1159 if (disconnect_message == NULL)
1162 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1163 if (disconnect_link == NULL)
1166 outgoing_counter = _dbus_counter_new ();
1167 if (outgoing_counter == NULL)
1170 objects = _dbus_object_tree_new (connection);
1171 if (objects == NULL)
1174 if (_dbus_modify_sigpipe)
1175 _dbus_disable_sigpipe ();
1177 connection->refcount.value = 1;
1178 connection->transport = transport;
1179 connection->watches = watch_list;
1180 connection->timeouts = timeout_list;
1181 connection->pending_replies = pending_replies;
1182 connection->outgoing_counter = outgoing_counter;
1183 connection->filter_list = NULL;
1184 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1185 connection->objects = objects;
1186 connection->exit_on_disconnect = FALSE;
1187 connection->shareable = FALSE;
1188 connection->route_peer_messages = FALSE;
1189 connection->disconnected_message_arrived = FALSE;
1190 connection->disconnected_message_processed = FALSE;
1192 #ifndef DBUS_DISABLE_CHECKS
1193 connection->generation = _dbus_current_generation;
1196 _dbus_data_slot_list_init (&connection->slot_list);
1198 connection->client_serial = 1;
1200 connection->disconnect_message_link = disconnect_link;
1202 CONNECTION_LOCK (connection);
1204 if (!_dbus_transport_set_connection (transport, connection))
1206 CONNECTION_UNLOCK (connection);
1211 _dbus_transport_ref (transport);
1213 CONNECTION_UNLOCK (connection);
1218 if (disconnect_message != NULL)
1219 dbus_message_unref (disconnect_message);
1221 if (disconnect_link != NULL)
1222 _dbus_list_free_link (disconnect_link);
1224 if (connection != NULL)
1226 _dbus_condvar_free_at_location (&connection->io_path_cond);
1227 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1228 _dbus_mutex_free_at_location (&connection->mutex);
1229 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1230 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1231 dbus_free (connection);
1233 if (pending_replies)
1234 _dbus_hash_table_unref (pending_replies);
1237 _dbus_watch_list_free (watch_list);
1240 _dbus_timeout_list_free (timeout_list);
1242 if (outgoing_counter)
1243 _dbus_counter_unref (outgoing_counter);
1246 _dbus_object_tree_unref (objects);
1252 * Increments the reference count of a DBusConnection.
1253 * Requires that the caller already holds the connection lock.
1255 * @param connection the connection.
1256 * @returns the connection.
1259 _dbus_connection_ref_unlocked (DBusConnection *connection)
1261 _dbus_assert (connection != NULL);
1262 _dbus_assert (connection->generation == _dbus_current_generation);
1264 HAVE_LOCK_CHECK (connection);
1266 #ifdef DBUS_HAVE_ATOMIC_INT
1267 _dbus_atomic_inc (&connection->refcount);
1269 _dbus_assert (connection->refcount.value > 0);
1270 connection->refcount.value += 1;
1277 * Decrements the reference count of a DBusConnection.
1278 * Requires that the caller already holds the connection lock.
1280 * @param connection the connection.
1283 _dbus_connection_unref_unlocked (DBusConnection *connection)
1285 dbus_bool_t last_unref;
1287 HAVE_LOCK_CHECK (connection);
1289 _dbus_assert (connection != NULL);
1291 /* The connection lock is better than the global
1292 * lock in the atomic increment fallback
1295 #ifdef DBUS_HAVE_ATOMIC_INT
1296 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1298 _dbus_assert (connection->refcount.value > 0);
1300 connection->refcount.value -= 1;
1301 last_unref = (connection->refcount.value == 0);
1303 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1308 _dbus_connection_last_unref (connection);
1311 static dbus_uint32_t
1312 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1316 serial = connection->client_serial++;
1318 if (connection->client_serial < 0)
1319 connection->client_serial = 1;
1325 * A callback for use with dbus_watch_new() to create a DBusWatch.
1327 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1328 * and the virtual handle_watch in DBusTransport if we got rid of it.
1329 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1332 * @param watch the watch.
1333 * @param condition the current condition of the file descriptors being watched.
1334 * @param data must be a pointer to a #DBusConnection
1335 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1338 _dbus_connection_handle_watch (DBusWatch *watch,
1339 unsigned int condition,
1342 DBusConnection *connection;
1344 DBusDispatchStatus status;
1348 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1350 CONNECTION_LOCK (connection);
1351 _dbus_connection_acquire_io_path (connection, -1);
1352 HAVE_LOCK_CHECK (connection);
1353 retval = _dbus_transport_handle_watch (connection->transport,
1356 _dbus_connection_release_io_path (connection);
1358 HAVE_LOCK_CHECK (connection);
1360 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1362 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1364 /* this calls out to user code */
1365 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1367 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1372 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1373 static DBusHashTable *shared_connections = NULL;
1376 shared_connections_shutdown (void *data)
1380 _DBUS_LOCK (shared_connections);
1382 /* This is a little bit unpleasant... better ideas? */
1383 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
1385 DBusConnection *connection;
1386 DBusMessage *message;
1389 _dbus_hash_iter_init (shared_connections, &iter);
1390 _dbus_hash_iter_next (&iter);
1392 connection = _dbus_hash_iter_get_value (&iter);
1394 _DBUS_UNLOCK (shared_connections);
1396 dbus_connection_ref (connection);
1397 _dbus_connection_close_possibly_shared (connection);
1399 /* Churn through to the Disconnected message */
1400 while ((message = dbus_connection_pop_message (connection)))
1402 dbus_message_unref (message);
1404 dbus_connection_unref (connection);
1406 _DBUS_LOCK (shared_connections);
1408 /* The connection should now be dead and not in our hash ... */
1409 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
1412 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1414 _dbus_hash_table_unref (shared_connections);
1415 shared_connections = NULL;
1417 _DBUS_UNLOCK (shared_connections);
1421 connection_lookup_shared (DBusAddressEntry *entry,
1422 DBusConnection **result)
1424 _dbus_verbose ("checking for existing connection\n");
1428 _DBUS_LOCK (shared_connections);
1430 if (shared_connections == NULL)
1432 _dbus_verbose ("creating shared_connections hash table\n");
1434 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1437 if (shared_connections == NULL)
1439 _DBUS_UNLOCK (shared_connections);
1443 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1445 _dbus_hash_table_unref (shared_connections);
1446 shared_connections = NULL;
1447 _DBUS_UNLOCK (shared_connections);
1451 _dbus_verbose (" successfully created shared_connections\n");
1453 _DBUS_UNLOCK (shared_connections);
1454 return TRUE; /* no point looking up in the hash we just made */
1460 guid = dbus_address_entry_get_value (entry, "guid");
1464 DBusConnection *connection;
1466 connection = _dbus_hash_table_lookup_string (shared_connections,
1471 /* The DBusConnection can't be finalized without taking
1472 * the shared_connections lock to remove it from the
1473 * hash. So it's safe to ref the connection here.
1474 * However, it may be disconnected if the Disconnected
1475 * message hasn't been processed yet, in which case we
1476 * want to pretend it isn't in the hash and avoid
1479 * The idea is to avoid ever returning a disconnected connection
1480 * from dbus_connection_open(). We could just synchronously
1481 * drop our shared ref to the connection on connection disconnect,
1482 * and then assert here that the connection is connected, but
1483 * that causes reentrancy headaches.
1485 CONNECTION_LOCK (connection);
1486 if (_dbus_connection_get_is_connected_unlocked (connection))
1488 _dbus_connection_ref_unlocked (connection);
1489 *result = connection;
1490 _dbus_verbose ("looked up existing connection to server guid %s\n",
1495 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1498 CONNECTION_UNLOCK (connection);
1502 _DBUS_UNLOCK (shared_connections);
1508 connection_record_shared_unlocked (DBusConnection *connection,
1512 char *guid_in_connection;
1514 HAVE_LOCK_CHECK (connection);
1515 _dbus_assert (connection->server_guid == NULL);
1516 _dbus_assert (connection->shareable);
1518 /* get a hard ref on this connection, even if
1519 * we won't in fact store it in the hash, we still
1520 * need to hold a ref on it until it's disconnected.
1522 _dbus_connection_ref_unlocked (connection);
1525 return TRUE; /* don't store in the hash */
1527 /* A separate copy of the key is required in the hash table, because
1528 * we don't have a lock on the connection when we are doing a hash
1532 guid_key = _dbus_strdup (guid);
1533 if (guid_key == NULL)
1536 guid_in_connection = _dbus_strdup (guid);
1537 if (guid_in_connection == NULL)
1539 dbus_free (guid_key);
1543 _DBUS_LOCK (shared_connections);
1544 _dbus_assert (shared_connections != NULL);
1546 if (!_dbus_hash_table_insert_string (shared_connections,
1547 guid_key, connection))
1549 dbus_free (guid_key);
1550 dbus_free (guid_in_connection);
1551 _DBUS_UNLOCK (shared_connections);
1555 connection->server_guid = guid_in_connection;
1557 _dbus_verbose ("stored connection to %s to be shared\n",
1558 connection->server_guid);
1560 _DBUS_UNLOCK (shared_connections);
1562 _dbus_assert (connection->server_guid != NULL);
1568 connection_forget_shared_unlocked (DBusConnection *connection)
1570 HAVE_LOCK_CHECK (connection);
1572 if (!connection->shareable)
1575 if (connection->server_guid != NULL)
1577 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1578 connection->server_guid);
1580 _DBUS_LOCK (shared_connections);
1582 if (!_dbus_hash_table_remove_string (shared_connections,
1583 connection->server_guid))
1584 _dbus_assert_not_reached ("connection was not in the shared table");
1586 dbus_free (connection->server_guid);
1587 connection->server_guid = NULL;
1588 _DBUS_UNLOCK (shared_connections);
1591 /* remove our reference held on all shareable connections */
1592 _dbus_connection_unref_unlocked (connection);
1595 static DBusConnection*
1596 connection_try_from_address_entry (DBusAddressEntry *entry,
1599 DBusTransport *transport;
1600 DBusConnection *connection;
1602 transport = _dbus_transport_open (entry, error);
1604 if (transport == NULL)
1606 _DBUS_ASSERT_ERROR_IS_SET (error);
1610 connection = _dbus_connection_new_for_transport (transport);
1612 _dbus_transport_unref (transport);
1614 if (connection == NULL)
1616 _DBUS_SET_OOM (error);
1620 #ifndef DBUS_DISABLE_CHECKS
1621 _dbus_assert (!connection->have_connection_lock);
1627 * If the shared parameter is true, then any existing connection will
1628 * be used (and if a new connection is created, it will be available
1629 * for use by others). If the shared parameter is false, a new
1630 * connection will always be created, and the new connection will
1631 * never be returned to other callers.
1633 * @param address the address
1634 * @param shared whether the connection is shared or private
1635 * @param error error return
1636 * @returns the connection or #NULL on error
1638 static DBusConnection*
1639 _dbus_connection_open_internal (const char *address,
1643 DBusConnection *connection;
1644 DBusAddressEntry **entries;
1645 DBusError tmp_error;
1646 DBusError first_error;
1649 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1651 _dbus_verbose ("opening %s connection to: %s\n",
1652 shared ? "shared" : "private", address);
1654 if (!dbus_parse_address (address, &entries, &len, error))
1657 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1661 dbus_error_init (&tmp_error);
1662 dbus_error_init (&first_error);
1663 for (i = 0; i < len; i++)
1667 if (!connection_lookup_shared (entries[i], &connection))
1668 _DBUS_SET_OOM (&tmp_error);
1671 if (connection == NULL)
1673 connection = connection_try_from_address_entry (entries[i],
1676 if (connection != NULL)
1678 CONNECTION_LOCK (connection);
1684 connection->shareable = TRUE;
1686 /* guid may be NULL */
1687 guid = dbus_address_entry_get_value (entries[i], "guid");
1689 if (!connection_record_shared_unlocked (connection, guid))
1691 _DBUS_SET_OOM (&tmp_error);
1692 _dbus_connection_close_possibly_shared_and_unlock (connection);
1693 dbus_connection_unref (connection);
1697 /* Note: as of now the connection is possibly shared
1698 * since another thread could have pulled it from the table.
1699 * However, we still have it locked so that thread isn't
1700 * doing anything more than blocking on the lock.
1708 HAVE_LOCK_CHECK (connection);
1712 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1715 dbus_move_error (&tmp_error, &first_error);
1717 dbus_error_free (&tmp_error);
1720 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1721 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1723 if (connection == NULL)
1725 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1726 dbus_move_error (&first_error, error);
1730 dbus_error_free (&first_error);
1732 CONNECTION_UNLOCK (connection);
1735 dbus_address_entries_free (entries);
1742 * @addtogroup DBusConnection
1748 * Gets a connection to a remote address. If a connection to the given
1749 * address already exists, returns the existing connection with its
1750 * reference count incremented. Otherwise, returns a new connection
1751 * and saves the new connection for possible re-use if a future call
1752 * to dbus_connection_open() asks to connect to the same server.
1754 * Use dbus_connection_open_private() to get a dedicated connection
1755 * not shared with other callers of dbus_connection_open().
1757 * If the open fails, the function returns #NULL, and provides a
1758 * reason for the failure in the error parameter. Pass #NULL for the
1759 * error parameter if you aren't interested in the reason for
1762 * Because this connection is shared, no user of the connection
1763 * may call dbus_connection_close(). However, when you are done with the
1764 * connection you should call dbus_connection_unref().
1766 * @param address the address.
1767 * @param error address where an error can be returned.
1768 * @returns new connection, or #NULL on failure.
1771 dbus_connection_open (const char *address,
1774 DBusConnection *connection;
1776 _dbus_return_val_if_fail (address != NULL, NULL);
1777 _dbus_return_val_if_error_is_set (error, NULL);
1779 connection = _dbus_connection_open_internal (address,
1787 * Opens a new, dedicated connection to a remote address. Unlike
1788 * dbus_connection_open(), always creates a new connection.
1789 * This connection will not be saved or recycled by libdbus.
1791 * If the open fails, the function returns #NULL, and provides a
1792 * reason for the failure in the error parameter. Pass #NULL for the
1793 * error parameter if you aren't interested in the reason for
1796 * When you are done with this connection, you must
1797 * dbus_connection_close() to disconnect it,
1798 * and dbus_connection_unref() to free the connection object.
1800 * (The dbus_connection_close() can be skipped if the
1801 * connection is already known to be disconnected, for example
1802 * if you are inside a handler for the Disconnected signal.)
1804 * @param address the address.
1805 * @param error address where an error can be returned.
1806 * @returns new connection, or #NULL on failure.
1809 dbus_connection_open_private (const char *address,
1812 DBusConnection *connection;
1814 _dbus_return_val_if_fail (address != NULL, NULL);
1815 _dbus_return_val_if_error_is_set (error, NULL);
1817 connection = _dbus_connection_open_internal (address,
1825 * Increments the reference count of a DBusConnection.
1827 * @param connection the connection.
1828 * @returns the connection.
1831 dbus_connection_ref (DBusConnection *connection)
1833 _dbus_return_val_if_fail (connection != NULL, NULL);
1834 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
1836 /* The connection lock is better than the global
1837 * lock in the atomic increment fallback
1840 #ifdef DBUS_HAVE_ATOMIC_INT
1841 _dbus_atomic_inc (&connection->refcount);
1843 CONNECTION_LOCK (connection);
1844 _dbus_assert (connection->refcount.value > 0);
1846 connection->refcount.value += 1;
1847 CONNECTION_UNLOCK (connection);
1854 free_outgoing_message (void *element,
1857 DBusMessage *message = element;
1858 DBusConnection *connection = data;
1860 _dbus_message_remove_size_counter (message,
1861 connection->outgoing_counter,
1863 dbus_message_unref (message);
1866 /* This is run without the mutex held, but after the last reference
1867 * to the connection has been dropped we should have no thread-related
1871 _dbus_connection_last_unref (DBusConnection *connection)
1875 _dbus_verbose ("Finalizing connection %p\n", connection);
1877 _dbus_assert (connection->refcount.value == 0);
1879 /* You have to disconnect the connection before unref:ing it. Otherwise
1880 * you won't get the disconnected message.
1882 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1883 _dbus_assert (connection->server_guid == NULL);
1885 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1886 _dbus_object_tree_free_all_unlocked (connection->objects);
1888 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1889 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1890 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1892 _dbus_watch_list_free (connection->watches);
1893 connection->watches = NULL;
1895 _dbus_timeout_list_free (connection->timeouts);
1896 connection->timeouts = NULL;
1898 _dbus_data_slot_list_free (&connection->slot_list);
1900 link = _dbus_list_get_first_link (&connection->filter_list);
1901 while (link != NULL)
1903 DBusMessageFilter *filter = link->data;
1904 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1906 filter->function = NULL;
1907 _dbus_message_filter_unref (filter); /* calls app callback */
1912 _dbus_list_clear (&connection->filter_list);
1914 /* ---- Done with stuff that invokes application callbacks */
1916 _dbus_object_tree_unref (connection->objects);
1918 _dbus_hash_table_unref (connection->pending_replies);
1919 connection->pending_replies = NULL;
1921 _dbus_list_clear (&connection->filter_list);
1923 _dbus_list_foreach (&connection->outgoing_messages,
1924 free_outgoing_message,
1926 _dbus_list_clear (&connection->outgoing_messages);
1928 _dbus_list_foreach (&connection->incoming_messages,
1929 (DBusForeachFunction) dbus_message_unref,
1931 _dbus_list_clear (&connection->incoming_messages);
1933 _dbus_counter_unref (connection->outgoing_counter);
1935 _dbus_transport_unref (connection->transport);
1937 if (connection->disconnect_message_link)
1939 DBusMessage *message = connection->disconnect_message_link->data;
1940 dbus_message_unref (message);
1941 _dbus_list_free_link (connection->disconnect_message_link);
1944 _dbus_list_clear (&connection->link_cache);
1946 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1947 _dbus_condvar_free_at_location (&connection->io_path_cond);
1949 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1950 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1952 _dbus_mutex_free_at_location (&connection->mutex);
1954 dbus_free (connection);
1958 * Decrements the reference count of a DBusConnection, and finalizes
1959 * it if the count reaches zero.
1961 * Note: it is a bug to drop the last reference to a connection that
1962 * is still connected.
1964 * For shared connections, libdbus will own a reference
1965 * as long as the connection is connected, so you can know that either
1966 * you don't have the last reference, or it's OK to drop the last reference.
1967 * Most connections are shared.
1969 * For private connections, the creator of the connection must arrange for
1970 * dbus_connection_close() to be called prior to dropping the last reference.
1971 * Private connections come from dbus_connection_open_private() or dbus_bus_get_private().
1973 * @param connection the connection.
1976 dbus_connection_unref (DBusConnection *connection)
1978 dbus_bool_t last_unref;
1980 _dbus_return_if_fail (connection != NULL);
1981 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1983 /* The connection lock is better than the global
1984 * lock in the atomic increment fallback
1987 #ifdef DBUS_HAVE_ATOMIC_INT
1988 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1990 CONNECTION_LOCK (connection);
1992 _dbus_assert (connection->refcount.value > 0);
1994 connection->refcount.value -= 1;
1995 last_unref = (connection->refcount.value == 0);
1998 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
2001 CONNECTION_UNLOCK (connection);
2005 _dbus_connection_last_unref (connection);
2009 * Note that the transport can disconnect itself (other end drops us)
2010 * and in that case this function never runs. So this function must
2011 * not do anything more than disconnect the transport and update the
2014 * If the transport self-disconnects, then we assume someone will
2015 * dispatch the connection to cause the dispatch status update.
2018 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2020 DBusDispatchStatus status;
2022 HAVE_LOCK_CHECK (connection);
2024 _dbus_verbose ("Disconnecting %p\n", connection);
2026 /* We need to ref because update_dispatch_status_and_unlock will unref
2027 * the connection if it was shared and libdbus was the only remaining
2030 _dbus_connection_ref_unlocked (connection);
2032 _dbus_transport_disconnect (connection->transport);
2034 /* This has the side effect of queuing the disconnect message link
2035 * (unless we don't have enough memory, possibly, so don't assert it).
2036 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
2037 * should never again return the newly-disconnected connection.
2039 * However, we only unref the shared connection and exit_on_disconnect when
2040 * the disconnect message reaches the head of the message queue,
2041 * NOT when it's first queued.
2043 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2045 /* This calls out to user code */
2046 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2048 /* Could also call out to user code */
2049 dbus_connection_unref (connection);
2053 _dbus_connection_close_possibly_shared (DBusConnection *connection)
2055 _dbus_assert (connection != NULL);
2056 _dbus_assert (connection->generation == _dbus_current_generation);
2058 CONNECTION_LOCK (connection);
2059 _dbus_connection_close_possibly_shared_and_unlock (connection);
2063 * Closes a private connection, so no further data can be sent or received.
2064 * This disconnects the transport (such as a socket) underlying the
2067 * Attempts to send messages after closing a connection are safe, but will result in
2068 * error replies generated locally in libdbus.
2070 * This function does not affect the connection's reference count. It's
2071 * safe to close a connection more than once; all calls after the
2072 * first do nothing. It's impossible to "reopen" a connection, a
2073 * new connection must be created. This function may result in a call
2074 * to the DBusDispatchStatusFunction set with
2075 * dbus_connection_set_dispatch_status_function(), as the disconnect
2076 * message it generates needs to be dispatched.
2078 * If a connection is dropped by the remote application, it will
2081 * You must close a connection prior to releasing the last reference to
2082 * the connection. If you dbus_connection_unref() for the last time
2083 * without closing the connection, the results are undefined; it
2084 * is a bug in your program and libdbus will try to print a warning.
2086 * You may not close a shared connection. Connections created with
2087 * dbus_connection_open() or dbus_bus_get() are shared.
2088 * These connections are owned by libdbus, and applications should
2089 * only unref them, never close them. Applications can know it is
2090 * safe to unref these connections because libdbus will be holding a
2091 * reference as long as the connection is open. Thus, either the
2092 * connection is closed and it is OK to drop the last reference,
2093 * or the connection is open and the app knows it does not have the
2096 * Connections created with dbus_connection_open_private() or
2097 * dbus_bus_get_private() are not kept track of or referenced by
2098 * libdbus. The creator of these connections is responsible for
2099 * calling dbus_connection_close() prior to releasing the last
2100 * reference, if the connection is not already disconnected.
2102 * @param connection the private (unshared) connection to close
2105 dbus_connection_close (DBusConnection *connection)
2107 _dbus_return_if_fail (connection != NULL);
2108 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2110 CONNECTION_LOCK (connection);
2112 if (connection->shareable)
2114 CONNECTION_UNLOCK (connection);
2116 _dbus_warn ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
2120 _dbus_connection_close_possibly_shared_and_unlock (connection);
2124 * Used internally to handle the semantics of dbus_server_set_new_connection_function().
2125 * If the new connection function does not ref the connection, we want to close it.
2127 * A bit of a hack, probably the new connection function should have returned a value
2128 * for whether to close, or should have had to close the connection itself if it
2131 * But, this works OK as long as the new connection function doesn't do anything
2132 * crazy like keep the connection around without ref'ing it.
2134 * We have to lock the connection across refcount check and close in case
2135 * the new connection function spawns a thread that closes and unrefs.
2136 * In that case, if the app thread
2137 * closes and unrefs first, we'll harmlessly close again; if the app thread
2138 * still has the ref, we'll close and then the app will close harmlessly.
2139 * If the app unrefs without closing, the app is broken since if the
2140 * app refs from the new connection function it is supposed to also close.
2142 * If we didn't atomically check the refcount and close with the lock held
2143 * though, we could screw this up.
2145 * @param connection the connection
2148 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
2150 CONNECTION_LOCK (connection);
2152 _dbus_assert (connection->refcount.value > 0);
2154 if (connection->refcount.value == 1)
2155 _dbus_connection_close_possibly_shared_and_unlock (connection);
2157 CONNECTION_UNLOCK (connection);
2161 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2163 HAVE_LOCK_CHECK (connection);
2164 return _dbus_transport_get_is_connected (connection->transport);
2168 * Gets whether the connection is currently open. A connection may
2169 * become disconnected when the remote application closes its end, or
2170 * exits; a connection may also be disconnected with
2171 * dbus_connection_close().
2173 * There are not separate states for "closed" and "disconnected," the two
2174 * terms are synonymous. This function should really be called
2175 * get_is_open() but for historical reasons is not.
2177 * @param connection the connection.
2178 * @returns #TRUE if the connection is still alive.
2181 dbus_connection_get_is_connected (DBusConnection *connection)
2185 _dbus_return_val_if_fail (connection != NULL, FALSE);
2187 CONNECTION_LOCK (connection);
2188 res = _dbus_connection_get_is_connected_unlocked (connection);
2189 CONNECTION_UNLOCK (connection);
2195 * Gets whether the connection was authenticated. (Note that
2196 * if the connection was authenticated then disconnected,
2197 * this function still returns #TRUE)
2199 * @param connection the connection
2200 * @returns #TRUE if the connection was ever authenticated
2203 dbus_connection_get_is_authenticated (DBusConnection *connection)
2207 _dbus_return_val_if_fail (connection != NULL, FALSE);
2209 CONNECTION_LOCK (connection);
2210 res = _dbus_transport_get_is_authenticated (connection->transport);
2211 CONNECTION_UNLOCK (connection);
2217 * Set whether _exit() should be called when the connection receives a
2218 * disconnect signal. The call to _exit() comes after any handlers for
2219 * the disconnect signal run; handlers can cancel the exit by calling
2222 * By default, exit_on_disconnect is #FALSE; but for message bus
2223 * connections returned from dbus_bus_get() it will be toggled on
2226 * @param connection the connection
2227 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
2230 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
2231 dbus_bool_t exit_on_disconnect)
2233 _dbus_return_if_fail (connection != NULL);
2235 CONNECTION_LOCK (connection);
2236 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
2237 CONNECTION_UNLOCK (connection);
2240 static DBusPreallocatedSend*
2241 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
2243 DBusPreallocatedSend *preallocated;
2245 HAVE_LOCK_CHECK (connection);
2247 _dbus_assert (connection != NULL);
2249 preallocated = dbus_new (DBusPreallocatedSend, 1);
2250 if (preallocated == NULL)
2253 if (connection->link_cache != NULL)
2255 preallocated->queue_link =
2256 _dbus_list_pop_first_link (&connection->link_cache);
2257 preallocated->queue_link->data = NULL;
2261 preallocated->queue_link = _dbus_list_alloc_link (NULL);
2262 if (preallocated->queue_link == NULL)
2266 if (connection->link_cache != NULL)
2268 preallocated->counter_link =
2269 _dbus_list_pop_first_link (&connection->link_cache);
2270 preallocated->counter_link->data = connection->outgoing_counter;
2274 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
2275 if (preallocated->counter_link == NULL)
2279 _dbus_counter_ref (preallocated->counter_link->data);
2281 preallocated->connection = connection;
2283 return preallocated;
2286 _dbus_list_free_link (preallocated->queue_link);
2288 dbus_free (preallocated);
2294 * Preallocates resources needed to send a message, allowing the message
2295 * to be sent without the possibility of memory allocation failure.
2296 * Allows apps to create a future guarantee that they can send
2297 * a message regardless of memory shortages.
2299 * @param connection the connection we're preallocating for.
2300 * @returns the preallocated resources, or #NULL
2302 DBusPreallocatedSend*
2303 dbus_connection_preallocate_send (DBusConnection *connection)
2305 DBusPreallocatedSend *preallocated;
2307 _dbus_return_val_if_fail (connection != NULL, NULL);
2309 CONNECTION_LOCK (connection);
2312 _dbus_connection_preallocate_send_unlocked (connection);
2314 CONNECTION_UNLOCK (connection);
2316 return preallocated;
2320 * Frees preallocated message-sending resources from
2321 * dbus_connection_preallocate_send(). Should only
2322 * be called if the preallocated resources are not used
2323 * to send a message.
2325 * @param connection the connection
2326 * @param preallocated the resources
2329 dbus_connection_free_preallocated_send (DBusConnection *connection,
2330 DBusPreallocatedSend *preallocated)
2332 _dbus_return_if_fail (connection != NULL);
2333 _dbus_return_if_fail (preallocated != NULL);
2334 _dbus_return_if_fail (connection == preallocated->connection);
2336 _dbus_list_free_link (preallocated->queue_link);
2337 _dbus_counter_unref (preallocated->counter_link->data);
2338 _dbus_list_free_link (preallocated->counter_link);
2339 dbus_free (preallocated);
2342 /* Called with lock held, does not update dispatch status */
2344 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
2345 DBusPreallocatedSend *preallocated,
2346 DBusMessage *message,
2347 dbus_uint32_t *client_serial)
2349 dbus_uint32_t serial;
2352 preallocated->queue_link->data = message;
2353 _dbus_list_prepend_link (&connection->outgoing_messages,
2354 preallocated->queue_link);
2356 _dbus_message_add_size_counter_link (message,
2357 preallocated->counter_link);
2359 dbus_free (preallocated);
2360 preallocated = NULL;
2362 dbus_message_ref (message);
2364 connection->n_outgoing += 1;
2366 sig = dbus_message_get_signature (message);
2368 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
2370 dbus_message_get_type (message),
2371 dbus_message_get_path (message) ?
2372 dbus_message_get_path (message) :
2374 dbus_message_get_interface (message) ?
2375 dbus_message_get_interface (message) :
2377 dbus_message_get_member (message) ?
2378 dbus_message_get_member (message) :
2381 dbus_message_get_destination (message) ?
2382 dbus_message_get_destination (message) :
2385 connection->n_outgoing);
2387 if (dbus_message_get_serial (message) == 0)
2389 serial = _dbus_connection_get_next_client_serial (connection);
2390 _dbus_message_set_serial (message, serial);
2392 *client_serial = serial;
2397 *client_serial = dbus_message_get_serial (message);
2400 _dbus_verbose ("Message %p serial is %u\n",
2401 message, dbus_message_get_serial (message));
2403 _dbus_message_lock (message);
2405 /* Now we need to run an iteration to hopefully just write the messages
2406 * out immediately, and otherwise get them queued up
2408 _dbus_connection_do_iteration_unlocked (connection,
2409 DBUS_ITERATION_DO_WRITING,
2412 /* If stuff is still queued up, be sure we wake up the main loop */
2413 if (connection->n_outgoing > 0)
2414 _dbus_connection_wakeup_mainloop (connection);
2418 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
2419 DBusPreallocatedSend *preallocated,
2420 DBusMessage *message,
2421 dbus_uint32_t *client_serial)
2423 DBusDispatchStatus status;
2425 HAVE_LOCK_CHECK (connection);
2427 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2429 message, client_serial);
2431 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2432 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2434 /* this calls out to user code */
2435 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2439 * Sends a message using preallocated resources. This function cannot fail.
2440 * It works identically to dbus_connection_send() in other respects.
2441 * Preallocated resources comes from dbus_connection_preallocate_send().
2442 * This function "consumes" the preallocated resources, they need not
2443 * be freed separately.
2445 * @param connection the connection
2446 * @param preallocated the preallocated resources
2447 * @param message the message to send
2448 * @param client_serial return location for client serial assigned to the message
2451 dbus_connection_send_preallocated (DBusConnection *connection,
2452 DBusPreallocatedSend *preallocated,
2453 DBusMessage *message,
2454 dbus_uint32_t *client_serial)
2456 _dbus_return_if_fail (connection != NULL);
2457 _dbus_return_if_fail (preallocated != NULL);
2458 _dbus_return_if_fail (message != NULL);
2459 _dbus_return_if_fail (preallocated->connection == connection);
2460 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
2461 dbus_message_get_member (message) != NULL);
2462 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
2463 (dbus_message_get_interface (message) != NULL &&
2464 dbus_message_get_member (message) != NULL));
2466 CONNECTION_LOCK (connection);
2467 _dbus_connection_send_preallocated_and_unlock (connection,
2469 message, client_serial);
2473 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
2474 DBusMessage *message,
2475 dbus_uint32_t *client_serial)
2477 DBusPreallocatedSend *preallocated;
2479 _dbus_assert (connection != NULL);
2480 _dbus_assert (message != NULL);
2482 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2483 if (preallocated == NULL)
2486 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2494 _dbus_connection_send_and_unlock (DBusConnection *connection,
2495 DBusMessage *message,
2496 dbus_uint32_t *client_serial)
2498 DBusPreallocatedSend *preallocated;
2500 _dbus_assert (connection != NULL);
2501 _dbus_assert (message != NULL);
2503 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2504 if (preallocated == NULL)
2506 CONNECTION_UNLOCK (connection);
2510 _dbus_connection_send_preallocated_and_unlock (connection,
2518 * Adds a message to the outgoing message queue. Does not block to
2519 * write the message to the network; that happens asynchronously. To
2520 * force the message to be written, call dbus_connection_flush().
2521 * Because this only queues the message, the only reason it can
2522 * fail is lack of memory. Even if the connection is disconnected,
2523 * no error will be returned.
2525 * If the function fails due to lack of memory, it returns #FALSE.
2526 * The function will never fail for other reasons; even if the
2527 * connection is disconnected, you can queue an outgoing message,
2528 * though obviously it won't be sent.
2530 * @param connection the connection.
2531 * @param message the message to write.
2532 * @param client_serial return location for client serial.
2533 * @returns #TRUE on success.
2536 dbus_connection_send (DBusConnection *connection,
2537 DBusMessage *message,
2538 dbus_uint32_t *client_serial)
2540 _dbus_return_val_if_fail (connection != NULL, FALSE);
2541 _dbus_return_val_if_fail (message != NULL, FALSE);
2543 CONNECTION_LOCK (connection);
2545 return _dbus_connection_send_and_unlock (connection,
2551 reply_handler_timeout (void *data)
2553 DBusConnection *connection;
2554 DBusDispatchStatus status;
2555 DBusPendingCall *pending = data;
2557 connection = _dbus_pending_call_get_connection_and_lock (pending);
2559 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2561 _dbus_connection_remove_timeout_unlocked (connection,
2562 _dbus_pending_call_get_timeout_unlocked (pending));
2563 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2565 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2566 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2568 /* Unlocks, and calls out to user code */
2569 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2575 * Queues a message to send, as with dbus_connection_send_message(),
2576 * but also returns a #DBusPendingCall used to receive a reply to the
2577 * message. If no reply is received in the given timeout_milliseconds,
2578 * this function expires the pending reply and generates a synthetic
2579 * error reply (generated in-process, not by the remote application)
2580 * indicating that a timeout occurred.
2582 * A #DBusPendingCall will see a reply message after any filters, but
2583 * before any object instances or other handlers. A #DBusPendingCall
2584 * will always see exactly one reply message, unless it's cancelled
2585 * with dbus_pending_call_cancel().
2587 * If a filter filters out the reply before the handler sees it, the
2588 * reply is immediately timed out and a timeout error reply is
2589 * generated. If a filter removes the timeout error reply then the
2590 * #DBusPendingCall will get confused. Filtering the timeout error
2591 * is thus considered a bug and will print a warning.
2593 * If #NULL is passed for the pending_return, the #DBusPendingCall
2594 * will still be generated internally, and used to track
2595 * the message reply timeout. This means a timeout error will
2596 * occur if no reply arrives, unlike with dbus_connection_send().
2598 * If -1 is passed for the timeout, a sane default timeout is used. -1
2599 * is typically the best value for the timeout for this reason, unless
2600 * you want a very short or very long timeout. There is no way to
2601 * avoid a timeout entirely, other than passing INT_MAX for the
2602 * timeout to postpone it indefinitely.
2604 * @param connection the connection
2605 * @param message the message to send
2606 * @param pending_return return location for a #DBusPendingCall object, or #NULLif connection is disconnected
2607 * @param timeout_milliseconds timeout in milliseconds or -1 for default
2608 * @returns #FALSE if no memory, #TRUE otherwise.
2612 dbus_connection_send_with_reply (DBusConnection *connection,
2613 DBusMessage *message,
2614 DBusPendingCall **pending_return,
2615 int timeout_milliseconds)
2617 DBusPendingCall *pending;
2618 dbus_int32_t serial = -1;
2619 DBusDispatchStatus status;
2621 _dbus_return_val_if_fail (connection != NULL, FALSE);
2622 _dbus_return_val_if_fail (message != NULL, FALSE);
2623 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2626 *pending_return = NULL;
2628 CONNECTION_LOCK (connection);
2630 if (!_dbus_connection_get_is_connected_unlocked (connection))
2632 CONNECTION_UNLOCK (connection);
2634 *pending_return = NULL;
2639 pending = _dbus_pending_call_new_unlocked (connection,
2640 timeout_milliseconds,
2641 reply_handler_timeout);
2643 if (pending == NULL)
2645 CONNECTION_UNLOCK (connection);
2649 /* Assign a serial to the message */
2650 serial = dbus_message_get_serial (message);
2653 serial = _dbus_connection_get_next_client_serial (connection);
2654 _dbus_message_set_serial (message, serial);
2657 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
2660 /* Insert the serial in the pending replies hash;
2661 * hash takes a refcount on DBusPendingCall.
2662 * Also, add the timeout.
2664 if (!_dbus_connection_attach_pending_call_unlocked (connection,
2668 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
2670 _dbus_connection_detach_pending_call_and_unlock (connection,
2672 goto error_unlocked;
2676 *pending_return = pending; /* hand off refcount */
2679 _dbus_connection_detach_pending_call_unlocked (connection, pending);
2680 /* we still have a ref to the pending call in this case, we unref
2681 * after unlocking, below
2685 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2687 /* this calls out to user code */
2688 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2690 if (pending_return == NULL)
2691 dbus_pending_call_unref (pending);
2696 CONNECTION_UNLOCK (connection);
2698 dbus_pending_call_unref (pending);
2702 /* This is slightly strange since we can pop a message here without
2703 * the dispatch lock.
2706 check_for_reply_unlocked (DBusConnection *connection,
2707 dbus_uint32_t client_serial)
2711 HAVE_LOCK_CHECK (connection);
2713 link = _dbus_list_get_first_link (&connection->incoming_messages);
2715 while (link != NULL)
2717 DBusMessage *reply = link->data;
2719 if (dbus_message_get_reply_serial (reply) == client_serial)
2721 _dbus_list_remove_link (&connection->incoming_messages, link);
2722 connection->n_incoming -= 1;
2725 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2732 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2734 /* We can't iterate over the hash in the normal way since we'll be
2735 * dropping the lock for each item. So we restart the
2736 * iter each time as we drain the hash table.
2739 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2741 DBusPendingCall *pending;
2744 _dbus_hash_iter_init (connection->pending_replies, &iter);
2745 _dbus_hash_iter_next (&iter);
2747 pending = _dbus_hash_iter_get_value (&iter);
2748 _dbus_pending_call_ref_unlocked (pending);
2750 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2752 _dbus_connection_remove_timeout_unlocked (connection,
2753 _dbus_pending_call_get_timeout_unlocked (pending));
2754 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2755 _dbus_hash_iter_remove_entry (&iter);
2757 _dbus_pending_call_unref_and_unlock (pending);
2758 CONNECTION_LOCK (connection);
2760 HAVE_LOCK_CHECK (connection);
2764 complete_pending_call_and_unlock (DBusConnection *connection,
2765 DBusPendingCall *pending,
2766 DBusMessage *message)
2768 _dbus_pending_call_set_reply_unlocked (pending, message);
2769 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2770 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2772 /* Must be called unlocked since it invokes app callback */
2773 _dbus_pending_call_complete (pending);
2774 dbus_pending_call_unref (pending);
2778 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2779 DBusPendingCall *pending)
2782 DBusDispatchStatus status;
2784 reply = check_for_reply_unlocked (connection,
2785 _dbus_pending_call_get_reply_serial_unlocked (pending));
2788 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2790 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2792 complete_pending_call_and_unlock (connection, pending, reply);
2793 dbus_message_unref (reply);
2795 CONNECTION_LOCK (connection);
2796 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2797 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2798 dbus_pending_call_unref (pending);
2807 * When a function that blocks has been called with a timeout, and we
2808 * run out of memory, the time to wait for memory is based on the
2809 * timeout. If the caller was willing to block a long time we wait a
2810 * relatively long time for memory, if they were only willing to block
2811 * briefly then we retry for memory at a rapid rate.
2813 * @timeout_milliseconds the timeout requested for blocking
2816 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2818 if (timeout_milliseconds == -1)
2819 _dbus_sleep_milliseconds (1000);
2820 else if (timeout_milliseconds < 100)
2821 ; /* just busy loop */
2822 else if (timeout_milliseconds <= 1000)
2823 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2825 _dbus_sleep_milliseconds (1000);
2828 static DBusMessage *
2829 generate_local_error_message (dbus_uint32_t serial,
2833 DBusMessage *message;
2834 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
2838 if (!dbus_message_set_error_name (message, error_name))
2840 dbus_message_unref (message);
2845 dbus_message_set_no_reply (message, TRUE);
2847 if (!dbus_message_set_reply_serial (message,
2850 dbus_message_unref (message);
2855 if (error_msg != NULL)
2857 DBusMessageIter iter;
2859 dbus_message_iter_init_append (message, &iter);
2860 if (!dbus_message_iter_append_basic (&iter,
2864 dbus_message_unref (message);
2875 * Blocks until a pending call times out or gets a reply.
2877 * Does not re-enter the main loop or run filter/path-registered
2878 * callbacks. The reply to the message will not be seen by
2881 * Returns immediately if pending call already got a reply.
2883 * @todo could use performance improvements (it keeps scanning
2884 * the whole message queue for example)
2886 * @param pending the pending call we block for a reply on
2889 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2891 long start_tv_sec, start_tv_usec;
2892 long end_tv_sec, end_tv_usec;
2893 long tv_sec, tv_usec;
2894 DBusDispatchStatus status;
2895 DBusConnection *connection;
2896 dbus_uint32_t client_serial;
2897 int timeout_milliseconds;
2899 _dbus_assert (pending != NULL);
2901 if (dbus_pending_call_get_completed (pending))
2904 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2906 connection = _dbus_pending_call_get_connection_and_lock (pending);
2908 /* Flush message queue - note, can affect dispatch status */
2909 _dbus_connection_flush_unlocked (connection);
2911 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2913 /* note that timeout_milliseconds is limited to a smallish value
2914 * in _dbus_pending_call_new() so overflows aren't possible
2917 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending));
2919 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2920 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2921 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2922 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2923 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2925 _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",
2926 timeout_milliseconds,
2928 start_tv_sec, start_tv_usec,
2929 end_tv_sec, end_tv_usec);
2931 /* check to see if we already got the data off the socket */
2932 /* from another blocked pending call */
2933 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2936 /* Now we wait... */
2937 /* always block at least once as we know we don't have the reply yet */
2938 _dbus_connection_do_iteration_unlocked (connection,
2939 DBUS_ITERATION_DO_READING |
2940 DBUS_ITERATION_BLOCK,
2941 timeout_milliseconds);
2945 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2947 HAVE_LOCK_CHECK (connection);
2949 /* queue messages and get status */
2951 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2953 /* the get_completed() is in case a dispatch() while we were blocking
2954 * got the reply instead of us.
2956 if (_dbus_pending_call_get_completed_unlocked (pending))
2958 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2959 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2960 dbus_pending_call_unref (pending);
2964 if (status == DBUS_DISPATCH_DATA_REMAINS) {
2965 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2969 _dbus_get_current_time (&tv_sec, &tv_usec);
2971 if (!_dbus_connection_get_is_connected_unlocked (connection))
2973 DBusMessage *error_msg;
2975 error_msg = generate_local_error_message (client_serial,
2976 DBUS_ERROR_DISCONNECTED,
2977 "Connection was disconnected before a reply was received");
2979 /* on OOM error_msg is set to NULL */
2980 complete_pending_call_and_unlock (connection, pending, error_msg);
2981 dbus_pending_call_unref (pending);
2984 else if (tv_sec < start_tv_sec)
2985 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2986 else if (connection->disconnect_message_link == NULL)
2987 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2988 else if (tv_sec < end_tv_sec ||
2989 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2991 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2992 (end_tv_usec - tv_usec) / 1000;
2993 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2994 _dbus_assert (timeout_milliseconds >= 0);
2996 if (status == DBUS_DISPATCH_NEED_MEMORY)
2998 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2999 * we may already have a reply in the buffer and just can't process
3002 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
3004 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3008 /* block again, we don't have the reply buffered yet. */
3009 _dbus_connection_do_iteration_unlocked (connection,
3010 DBUS_ITERATION_DO_READING |
3011 DBUS_ITERATION_BLOCK,
3012 timeout_milliseconds);
3015 goto recheck_status;
3018 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
3019 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
3021 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
3023 /* unlock and call user code */
3024 complete_pending_call_and_unlock (connection, pending, NULL);
3026 /* update user code on dispatch status */
3027 CONNECTION_LOCK (connection);
3028 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3029 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3030 dbus_pending_call_unref (pending);
3034 * Sends a message and blocks a certain time period while waiting for
3035 * a reply. This function does not reenter the main loop,
3036 * i.e. messages other than the reply are queued up but not
3037 * processed. This function is used to do non-reentrant "method
3040 * If a normal reply is received, it is returned, and removed from the
3041 * incoming message queue. If it is not received, #NULL is returned
3042 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
3043 * received, it is converted to a #DBusError and returned as an error,
3044 * then the reply message is deleted. If something else goes wrong,
3045 * result is set to whatever is appropriate, such as
3046 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
3048 * @param connection the connection
3049 * @param message the message to send
3050 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3051 * @param error return location for error message
3052 * @returns the message that is the reply or #NULL with an error code if the
3056 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
3057 DBusMessage *message,
3058 int timeout_milliseconds,
3062 DBusPendingCall *pending;
3064 _dbus_return_val_if_fail (connection != NULL, NULL);
3065 _dbus_return_val_if_fail (message != NULL, NULL);
3066 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3067 _dbus_return_val_if_error_is_set (error, NULL);
3069 if (!dbus_connection_send_with_reply (connection, message,
3070 &pending, timeout_milliseconds))
3072 _DBUS_SET_OOM (error);
3076 _dbus_assert (pending != NULL);
3078 dbus_pending_call_block (pending);
3080 reply = dbus_pending_call_steal_reply (pending);
3081 dbus_pending_call_unref (pending);
3083 /* call_complete_and_unlock() called from pending_call_block() should
3084 * always fill this in.
3086 _dbus_assert (reply != NULL);
3088 if (dbus_set_error_from_message (error, reply))
3090 dbus_message_unref (reply);
3098 * Blocks until the outgoing message queue is empty.
3099 * Assumes connection lock already held.
3101 * If you call this, you MUST call update_dispatch_status afterword...
3103 * @param connection the connection.
3106 _dbus_connection_flush_unlocked (DBusConnection *connection)
3108 /* We have to specify DBUS_ITERATION_DO_READING here because
3109 * otherwise we could have two apps deadlock if they are both doing
3110 * a flush(), and the kernel buffers fill up. This could change the
3113 DBusDispatchStatus status;
3115 HAVE_LOCK_CHECK (connection);
3117 while (connection->n_outgoing > 0 &&
3118 _dbus_connection_get_is_connected_unlocked (connection))
3120 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3121 HAVE_LOCK_CHECK (connection);
3122 _dbus_connection_do_iteration_unlocked (connection,
3123 DBUS_ITERATION_DO_READING |
3124 DBUS_ITERATION_DO_WRITING |
3125 DBUS_ITERATION_BLOCK,
3129 HAVE_LOCK_CHECK (connection);
3130 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3131 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3133 HAVE_LOCK_CHECK (connection);
3138 * Blocks until the outgoing message queue is empty.
3140 * @param connection the connection.
3143 dbus_connection_flush (DBusConnection *connection)
3145 /* We have to specify DBUS_ITERATION_DO_READING here because
3146 * otherwise we could have two apps deadlock if they are both doing
3147 * a flush(), and the kernel buffers fill up. This could change the
3150 DBusDispatchStatus status;
3152 _dbus_return_if_fail (connection != NULL);
3154 CONNECTION_LOCK (connection);
3156 status = _dbus_connection_flush_unlocked (connection);
3158 HAVE_LOCK_CHECK (connection);
3159 /* Unlocks and calls out to user code */
3160 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3162 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
3166 * This function is intended for use with applications that don't want
3167 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3168 * example usage would be:
3171 * while (dbus_connection_read_write_dispatch (connection, -1))
3172 * ; // empty loop body
3175 * In this usage you would normally have set up a filter function to look
3176 * at each message as it is dispatched. The loop terminates when the last
3177 * message from the connection (the disconnected signal) is processed.
3179 * If there are messages to dispatch and the dispatch flag is set, this
3180 * function will dbus_connection_dispatch() once, and return. If there are no
3181 * messages to dispatch, this function will block until it can read or write,
3182 * then read or write, then return.
3184 * The way to think of this function is that it either makes some sort
3185 * of progress, or it blocks.
3187 * The return value indicates whether the disconnect message has been
3188 * processed, NOT whether the connection is connected. This is
3189 * important because even after disconnecting, you want to process any
3190 * messages you received prior to the disconnect.
3192 * @param connection the connection
3193 * @param timeout_milliseconds max time to block or -1 for infinite
3194 * @param dispatch dispatch new messages or leave them on the incoming queue
3195 * @returns #TRUE if the disconnect message has not been processed
3198 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3199 int timeout_milliseconds,
3200 dbus_bool_t dispatch)
3202 DBusDispatchStatus dstatus;
3203 dbus_bool_t dispatched_disconnected;
3205 dstatus = dbus_connection_get_dispatch_status (connection);
3207 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3209 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
3210 dbus_connection_dispatch (connection);
3211 CONNECTION_LOCK (connection);
3213 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3215 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
3216 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3217 CONNECTION_LOCK (connection);
3221 CONNECTION_LOCK (connection);
3222 if (_dbus_connection_get_is_connected_unlocked (connection))
3224 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3225 _dbus_connection_do_iteration_unlocked (connection,
3226 DBUS_ITERATION_DO_READING |
3227 DBUS_ITERATION_DO_WRITING |
3228 DBUS_ITERATION_BLOCK,
3229 timeout_milliseconds);
3233 HAVE_LOCK_CHECK (connection);
3234 dispatched_disconnected = connection->n_incoming == 0 &&
3235 connection->disconnect_message_link == NULL;
3236 CONNECTION_UNLOCK (connection);
3237 return !dispatched_disconnected; /* TRUE if we have not processed disconnected */
3242 * This function is intended for use with applications that don't want
3243 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3244 * example usage would be:
3247 * while (dbus_connection_read_write_dispatch (connection, -1))
3248 * ; // empty loop body
3251 * In this usage you would normally have set up a filter function to look
3252 * at each message as it is dispatched. The loop terminates when the last
3253 * message from the connection (the disconnected signal) is processed.
3255 * If there are messages to dispatch, this function will
3256 * dbus_connection_dispatch() once, and return. If there are no
3257 * messages to dispatch, this function will block until it can read or
3258 * write, then read or write, then return.
3260 * The way to think of this function is that it either makes some sort
3261 * of progress, or it blocks.
3263 * The return value indicates whether the disconnect message has been
3264 * processed, NOT whether the connection is connected. This is
3265 * important because even after disconnecting, you want to process any
3266 * messages you received prior to the disconnect.
3268 * @param connection the connection
3269 * @param timeout_milliseconds max time to block or -1 for infinite
3270 * @returns #TRUE if the disconnect message has not been processed
3273 dbus_connection_read_write_dispatch (DBusConnection *connection,
3274 int timeout_milliseconds)
3276 _dbus_return_val_if_fail (connection != NULL, FALSE);
3277 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3278 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3282 * This function is intended for use with applications that don't want to
3283 * write a main loop and deal with #DBusWatch and #DBusTimeout.
3285 * If there are no messages to dispatch, this function will block until it can
3286 * read or write, then read or write, then return.
3288 * The return value indicates whether the disconnect message has been
3289 * processed, NOT whether the connection is connected. This is important
3290 * because even after disconnecting, you want to process any messages you
3291 * received prior to the disconnect.
3293 * @param connection the connection
3294 * @param timeout_milliseconds max time to block or -1 for infinite
3295 * @returns #TRUE if the disconnect message has not been processed
3298 dbus_connection_read_write (DBusConnection *connection,
3299 int timeout_milliseconds)
3301 _dbus_return_val_if_fail (connection != NULL, FALSE);
3302 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3303 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3306 /* We need to call this anytime we pop the head of the queue, and then
3307 * update_dispatch_status_and_unlock needs to be called afterward
3308 * which will "process" the disconnected message and set
3309 * disconnected_message_processed.
3312 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3313 DBusMessage *head_of_queue)
3315 HAVE_LOCK_CHECK (connection);
3317 /* checking that the link is NULL is an optimization to avoid the is_signal call */
3318 if (connection->disconnect_message_link == NULL &&
3319 dbus_message_is_signal (head_of_queue,
3320 DBUS_INTERFACE_LOCAL,
3323 connection->disconnected_message_arrived = TRUE;
3328 * Returns the first-received message from the incoming message queue,
3329 * leaving it in the queue. If the queue is empty, returns #NULL.
3331 * The caller does not own a reference to the returned message, and
3332 * must either return it using dbus_connection_return_message() or
3333 * keep it after calling dbus_connection_steal_borrowed_message(). No
3334 * one can get at the message while its borrowed, so return it as
3335 * quickly as possible and don't keep a reference to it after
3336 * returning it. If you need to keep the message, make a copy of it.
3338 * dbus_connection_dispatch() will block if called while a borrowed
3339 * message is outstanding; only one piece of code can be playing with
3340 * the incoming queue at a time. This function will block if called
3341 * during a dbus_connection_dispatch().
3343 * @param connection the connection.
3344 * @returns next message in the incoming queue.
3347 dbus_connection_borrow_message (DBusConnection *connection)
3349 DBusDispatchStatus status;
3350 DBusMessage *message;
3352 _dbus_return_val_if_fail (connection != NULL, NULL);
3354 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3356 /* this is called for the side effect that it queues
3357 * up any messages from the transport
3359 status = dbus_connection_get_dispatch_status (connection);
3360 if (status != DBUS_DISPATCH_DATA_REMAINS)
3363 CONNECTION_LOCK (connection);
3365 _dbus_connection_acquire_dispatch (connection);
3367 /* While a message is outstanding, the dispatch lock is held */
3368 _dbus_assert (connection->message_borrowed == NULL);
3370 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3372 message = connection->message_borrowed;
3374 check_disconnected_message_arrived_unlocked (connection, message);
3376 /* Note that we KEEP the dispatch lock until the message is returned */
3377 if (message == NULL)
3378 _dbus_connection_release_dispatch (connection);
3380 CONNECTION_UNLOCK (connection);
3382 /* We don't update dispatch status until it's returned or stolen */
3388 * Used to return a message after peeking at it using
3389 * dbus_connection_borrow_message(). Only called if
3390 * message from dbus_connection_borrow_message() was non-#NULL.
3392 * @param connection the connection
3393 * @param message the message from dbus_connection_borrow_message()
3396 dbus_connection_return_message (DBusConnection *connection,
3397 DBusMessage *message)
3399 DBusDispatchStatus status;
3401 _dbus_return_if_fail (connection != NULL);
3402 _dbus_return_if_fail (message != NULL);
3403 _dbus_return_if_fail (message == connection->message_borrowed);
3404 _dbus_return_if_fail (connection->dispatch_acquired);
3406 CONNECTION_LOCK (connection);
3408 _dbus_assert (message == connection->message_borrowed);
3410 connection->message_borrowed = NULL;
3412 _dbus_connection_release_dispatch (connection);
3414 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3415 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3419 * Used to keep a message after peeking at it using
3420 * dbus_connection_borrow_message(). Before using this function, see
3421 * the caveats/warnings in the documentation for
3422 * dbus_connection_pop_message().
3424 * @param connection the connection
3425 * @param message the message from dbus_connection_borrow_message()
3428 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3429 DBusMessage *message)
3431 DBusMessage *pop_message;
3432 DBusDispatchStatus status;
3434 _dbus_return_if_fail (connection != NULL);
3435 _dbus_return_if_fail (message != NULL);
3436 _dbus_return_if_fail (message == connection->message_borrowed);
3437 _dbus_return_if_fail (connection->dispatch_acquired);
3439 CONNECTION_LOCK (connection);
3441 _dbus_assert (message == connection->message_borrowed);
3443 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3444 _dbus_assert (message == pop_message);
3446 connection->n_incoming -= 1;
3448 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3449 message, connection->n_incoming);
3451 connection->message_borrowed = NULL;
3453 _dbus_connection_release_dispatch (connection);
3455 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3456 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3459 /* See dbus_connection_pop_message, but requires the caller to own
3460 * the lock before calling. May drop the lock while running.
3463 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3465 HAVE_LOCK_CHECK (connection);
3467 _dbus_assert (connection->message_borrowed == NULL);
3469 if (connection->n_incoming > 0)
3473 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3474 connection->n_incoming -= 1;
3476 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3478 dbus_message_get_type (link->data),
3479 dbus_message_get_path (link->data) ?
3480 dbus_message_get_path (link->data) :
3482 dbus_message_get_interface (link->data) ?
3483 dbus_message_get_interface (link->data) :
3485 dbus_message_get_member (link->data) ?
3486 dbus_message_get_member (link->data) :
3488 dbus_message_get_signature (link->data),
3489 connection, connection->n_incoming);
3491 check_disconnected_message_arrived_unlocked (connection, link->data);
3499 /* See dbus_connection_pop_message, but requires the caller to own
3500 * the lock before calling. May drop the lock while running.
3503 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3507 HAVE_LOCK_CHECK (connection);
3509 link = _dbus_connection_pop_message_link_unlocked (connection);
3513 DBusMessage *message;
3515 message = link->data;
3517 _dbus_list_free_link (link);
3526 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3527 DBusList *message_link)
3529 HAVE_LOCK_CHECK (connection);
3531 _dbus_assert (message_link != NULL);
3532 /* You can't borrow a message while a link is outstanding */
3533 _dbus_assert (connection->message_borrowed == NULL);
3534 /* We had to have the dispatch lock across the pop/putback */
3535 _dbus_assert (connection->dispatch_acquired);
3537 _dbus_list_prepend_link (&connection->incoming_messages,
3539 connection->n_incoming += 1;
3541 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3543 dbus_message_get_type (message_link->data),
3544 dbus_message_get_interface (message_link->data) ?
3545 dbus_message_get_interface (message_link->data) :
3547 dbus_message_get_member (message_link->data) ?
3548 dbus_message_get_member (message_link->data) :
3550 dbus_message_get_signature (message_link->data),
3551 connection, connection->n_incoming);
3555 * Returns the first-received message from the incoming message queue,
3556 * removing it from the queue. The caller owns a reference to the
3557 * returned message. If the queue is empty, returns #NULL.
3559 * This function bypasses any message handlers that are registered,
3560 * and so using it is usually wrong. Instead, let the main loop invoke
3561 * dbus_connection_dispatch(). Popping messages manually is only
3562 * useful in very simple programs that don't share a #DBusConnection
3563 * with any libraries or other modules.
3565 * There is a lock that covers all ways of accessing the incoming message
3566 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3567 * dbus_connection_borrow_message(), etc. will all block while one of the others
3568 * in the group is running.
3570 * @param connection the connection.
3571 * @returns next message in the incoming queue.
3574 dbus_connection_pop_message (DBusConnection *connection)
3576 DBusMessage *message;
3577 DBusDispatchStatus status;
3579 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3581 /* this is called for the side effect that it queues
3582 * up any messages from the transport
3584 status = dbus_connection_get_dispatch_status (connection);
3585 if (status != DBUS_DISPATCH_DATA_REMAINS)
3588 CONNECTION_LOCK (connection);
3589 _dbus_connection_acquire_dispatch (connection);
3590 HAVE_LOCK_CHECK (connection);
3592 message = _dbus_connection_pop_message_unlocked (connection);
3594 _dbus_verbose ("Returning popped message %p\n", message);
3596 _dbus_connection_release_dispatch (connection);
3598 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3599 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3605 * Acquire the dispatcher. This is a separate lock so the main
3606 * connection lock can be dropped to call out to application dispatch
3609 * @param connection the connection.
3612 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3614 HAVE_LOCK_CHECK (connection);
3616 _dbus_connection_ref_unlocked (connection);
3617 CONNECTION_UNLOCK (connection);
3619 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3620 _dbus_mutex_lock (connection->dispatch_mutex);
3622 while (connection->dispatch_acquired)
3624 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3625 _dbus_condvar_wait (connection->dispatch_cond,
3626 connection->dispatch_mutex);
3629 _dbus_assert (!connection->dispatch_acquired);
3631 connection->dispatch_acquired = TRUE;
3633 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3634 _dbus_mutex_unlock (connection->dispatch_mutex);
3636 CONNECTION_LOCK (connection);
3637 _dbus_connection_unref_unlocked (connection);
3641 * Release the dispatcher when you're done with it. Only call
3642 * after you've acquired the dispatcher. Wakes up at most one
3643 * thread currently waiting to acquire the dispatcher.
3645 * @param connection the connection.
3648 _dbus_connection_release_dispatch (DBusConnection *connection)
3650 HAVE_LOCK_CHECK (connection);
3652 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3653 _dbus_mutex_lock (connection->dispatch_mutex);
3655 _dbus_assert (connection->dispatch_acquired);
3657 connection->dispatch_acquired = FALSE;
3658 _dbus_condvar_wake_one (connection->dispatch_cond);
3660 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3661 _dbus_mutex_unlock (connection->dispatch_mutex);
3665 _dbus_connection_failed_pop (DBusConnection *connection,
3666 DBusList *message_link)
3668 _dbus_list_prepend_link (&connection->incoming_messages,
3670 connection->n_incoming += 1;
3673 static DBusDispatchStatus
3674 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3676 HAVE_LOCK_CHECK (connection);
3678 if (connection->n_incoming > 0)
3679 return DBUS_DISPATCH_DATA_REMAINS;
3680 else if (!_dbus_transport_queue_messages (connection->transport))
3681 return DBUS_DISPATCH_NEED_MEMORY;
3684 DBusDispatchStatus status;
3685 dbus_bool_t is_connected;
3687 status = _dbus_transport_get_dispatch_status (connection->transport);
3688 is_connected = _dbus_transport_get_is_connected (connection->transport);
3690 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3691 DISPATCH_STATUS_NAME (status), is_connected);
3695 /* It's possible this would be better done by having an
3696 * explicit notification from
3697 * _dbus_transport_disconnect() that would synchronously
3698 * do this, instead of waiting for the next dispatch
3699 * status check. However, probably not good to change
3700 * until it causes a problem.
3703 if (status == DBUS_DISPATCH_COMPLETE &&
3704 connection->disconnect_message_link)
3706 _dbus_verbose ("Sending disconnect message from %s\n",
3707 _DBUS_FUNCTION_NAME);
3709 /* If we have pending calls, queue their timeouts - we want the Disconnected
3710 * to be the last message, after these timeouts.
3712 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
3714 /* We haven't sent the disconnect message already,
3715 * and all real messages have been queued up.
3717 _dbus_connection_queue_synthesized_message_link (connection,
3718 connection->disconnect_message_link);
3719 connection->disconnect_message_link = NULL;
3721 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
3722 * connection from dbus_bus_get(). We make the same guarantee for
3723 * dbus_connection_open() but in a different way since we don't want to
3724 * unref right here; we instead check for connectedness before returning
3725 * the connection from the hash.
3727 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
3729 status = DBUS_DISPATCH_DATA_REMAINS;
3732 /* Dump the outgoing queue, we aren't going to be able to
3733 * send it now, and we'd like accessors like
3734 * dbus_connection_get_outgoing_size() to be accurate.
3736 if (connection->n_outgoing > 0)
3740 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3741 connection->n_outgoing);
3743 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3745 _dbus_connection_message_sent (connection, link->data);
3750 if (status != DBUS_DISPATCH_COMPLETE)
3752 else if (connection->n_incoming > 0)
3753 return DBUS_DISPATCH_DATA_REMAINS;
3755 return DBUS_DISPATCH_COMPLETE;
3760 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
3761 DBusDispatchStatus new_status)
3763 dbus_bool_t changed;
3764 DBusDispatchStatusFunction function;
3767 HAVE_LOCK_CHECK (connection);
3769 _dbus_connection_ref_unlocked (connection);
3771 changed = new_status != connection->last_dispatch_status;
3773 connection->last_dispatch_status = new_status;
3775 function = connection->dispatch_status_function;
3776 data = connection->dispatch_status_data;
3778 if (connection->disconnected_message_arrived &&
3779 !connection->disconnected_message_processed)
3781 connection->disconnected_message_processed = TRUE;
3783 /* this does an unref, but we have a ref
3784 * so we should not run the finalizer here
3787 connection_forget_shared_unlocked (connection);
3789 if (connection->exit_on_disconnect)
3791 CONNECTION_UNLOCK (connection);
3793 _dbus_verbose ("Exiting on Disconnected signal\n");
3795 _dbus_assert_not_reached ("Call to exit() returned");
3799 /* We drop the lock */
3800 CONNECTION_UNLOCK (connection);
3802 if (changed && function)
3804 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
3805 connection, new_status,
3806 DISPATCH_STATUS_NAME (new_status));
3807 (* function) (connection, new_status, data);
3810 dbus_connection_unref (connection);
3814 * Gets the current state (what we would currently return
3815 * from dbus_connection_dispatch()) but doesn't actually
3816 * dispatch any messages.
3818 * @param connection the connection.
3819 * @returns current dispatch status
3822 dbus_connection_get_dispatch_status (DBusConnection *connection)
3824 DBusDispatchStatus status;
3826 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3828 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3830 CONNECTION_LOCK (connection);
3832 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3834 CONNECTION_UNLOCK (connection);
3840 * Filter funtion for handling the Peer standard interface.
3842 static DBusHandlerResult
3843 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
3844 DBusMessage *message)
3846 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
3848 /* This means we're letting the bus route this message */
3849 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3851 else if (dbus_message_is_method_call (message,
3852 DBUS_INTERFACE_PEER,
3858 ret = dbus_message_new_method_return (message);
3860 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3862 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
3864 dbus_message_unref (ret);
3867 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3869 return DBUS_HANDLER_RESULT_HANDLED;
3871 else if (dbus_message_is_method_call (message,
3872 DBUS_INTERFACE_PEER,
3879 ret = dbus_message_new_method_return (message);
3881 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3884 _dbus_string_init (&uuid);
3885 if (_dbus_get_local_machine_uuid_encoded (&uuid))
3887 const char *v_STRING = _dbus_string_get_const_data (&uuid);
3888 if (dbus_message_append_args (ret,
3889 DBUS_TYPE_STRING, &v_STRING,
3892 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
3895 _dbus_string_free (&uuid);
3897 dbus_message_unref (ret);
3900 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3902 return DBUS_HANDLER_RESULT_HANDLED;
3904 else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
3906 /* We need to bounce anything else with this interface, otherwise apps
3907 * could start extending the interface and when we added extensions
3908 * here to DBusConnection we'd break those apps.
3914 ret = dbus_message_new_error (message,
3915 DBUS_ERROR_UNKNOWN_METHOD,
3916 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
3918 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3920 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
3922 dbus_message_unref (ret);
3925 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3927 return DBUS_HANDLER_RESULT_HANDLED;
3931 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3936 * Processes all builtin filter functions
3938 * If the spec specifies a standard interface
3939 * they should be processed from this method
3941 static DBusHandlerResult
3942 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
3943 DBusMessage *message)
3945 /* We just run one filter for now but have the option to run more
3946 if the spec calls for it in the future */
3948 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
3952 * Processes data buffered while handling watches, queueing zero or
3953 * more incoming messages. Then pops the first-received message from
3954 * the current incoming message queue, runs any handlers for it, and
3955 * unrefs the message. Returns a status indicating whether messages/data
3956 * remain, more memory is needed, or all data has been processed.
3958 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
3959 * does not necessarily dispatch a message, as the data may
3960 * be part of authentication or the like.
3962 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
3964 * @todo FIXME what if we call out to application code to handle a
3965 * message, holding the dispatch lock, and the application code runs
3966 * the main loop and dispatches again? Probably deadlocks at the
3967 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
3968 * and then the GSource etc. could handle the situation? Right now
3969 * our GSource is NO_RECURSE
3971 * @param connection the connection
3972 * @returns dispatch status
3975 dbus_connection_dispatch (DBusConnection *connection)
3977 DBusMessage *message;
3978 DBusList *link, *filter_list_copy, *message_link;
3979 DBusHandlerResult result;
3980 DBusPendingCall *pending;
3981 dbus_int32_t reply_serial;
3982 DBusDispatchStatus status;
3984 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3986 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3988 CONNECTION_LOCK (connection);
3989 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3990 if (status != DBUS_DISPATCH_DATA_REMAINS)
3992 /* unlocks and calls out to user code */
3993 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3997 /* We need to ref the connection since the callback could potentially
3998 * drop the last ref to it
4000 _dbus_connection_ref_unlocked (connection);
4002 _dbus_connection_acquire_dispatch (connection);
4003 HAVE_LOCK_CHECK (connection);
4005 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4006 if (message_link == NULL)
4008 /* another thread dispatched our stuff */
4010 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
4012 _dbus_connection_release_dispatch (connection);
4014 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4016 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4018 dbus_connection_unref (connection);
4023 message = message_link->data;
4025 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
4027 dbus_message_get_type (message),
4028 dbus_message_get_interface (message) ?
4029 dbus_message_get_interface (message) :
4031 dbus_message_get_member (message) ?
4032 dbus_message_get_member (message) :
4034 dbus_message_get_signature (message));
4036 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4038 /* Pending call handling must be first, because if you do
4039 * dbus_connection_send_with_reply_and_block() or
4040 * dbus_pending_call_block() then no handlers/filters will be run on
4041 * the reply. We want consistent semantics in the case where we
4042 * dbus_connection_dispatch() the reply.
4045 reply_serial = dbus_message_get_reply_serial (message);
4046 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4050 _dbus_verbose ("Dispatching a pending reply\n");
4051 complete_pending_call_and_unlock (connection, pending, message);
4052 pending = NULL; /* it's probably unref'd */
4054 CONNECTION_LOCK (connection);
4055 _dbus_verbose ("pending call completed in dispatch\n");
4056 result = DBUS_HANDLER_RESULT_HANDLED;
4060 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4061 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4064 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4066 _dbus_connection_release_dispatch (connection);
4067 HAVE_LOCK_CHECK (connection);
4069 _dbus_connection_failed_pop (connection, message_link);
4071 /* unlocks and calls user code */
4072 _dbus_connection_update_dispatch_status_and_unlock (connection,
4073 DBUS_DISPATCH_NEED_MEMORY);
4076 dbus_pending_call_unref (pending);
4077 dbus_connection_unref (connection);
4079 return DBUS_DISPATCH_NEED_MEMORY;
4082 _dbus_list_foreach (&filter_list_copy,
4083 (DBusForeachFunction)_dbus_message_filter_ref,
4086 /* We're still protected from dispatch() reentrancy here
4087 * since we acquired the dispatcher
4089 CONNECTION_UNLOCK (connection);
4091 link = _dbus_list_get_first_link (&filter_list_copy);
4092 while (link != NULL)
4094 DBusMessageFilter *filter = link->data;
4095 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
4097 if (filter->function == NULL)
4099 _dbus_verbose (" filter was removed in a callback function\n");
4104 _dbus_verbose (" running filter on message %p\n", message);
4105 result = (* filter->function) (connection, message, filter->user_data);
4107 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4113 _dbus_list_foreach (&filter_list_copy,
4114 (DBusForeachFunction)_dbus_message_filter_unref,
4116 _dbus_list_clear (&filter_list_copy);
4118 CONNECTION_LOCK (connection);
4120 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4122 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
4125 else if (result == DBUS_HANDLER_RESULT_HANDLED)
4127 _dbus_verbose ("filter handled message in dispatch\n");
4131 /* We're still protected from dispatch() reentrancy here
4132 * since we acquired the dispatcher
4134 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
4136 dbus_message_get_type (message),
4137 dbus_message_get_interface (message) ?
4138 dbus_message_get_interface (message) :
4140 dbus_message_get_member (message) ?
4141 dbus_message_get_member (message) :
4143 dbus_message_get_signature (message));
4145 HAVE_LOCK_CHECK (connection);
4146 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4149 CONNECTION_LOCK (connection);
4151 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4153 _dbus_verbose ("object tree handled message in dispatch\n");
4157 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
4161 DBusPreallocatedSend *preallocated;
4163 _dbus_verbose (" sending error %s\n",
4164 DBUS_ERROR_UNKNOWN_METHOD);
4166 if (!_dbus_string_init (&str))
4168 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4169 _dbus_verbose ("no memory for error string in dispatch\n");
4173 if (!_dbus_string_append_printf (&str,
4174 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
4175 dbus_message_get_member (message),
4176 dbus_message_get_signature (message),
4177 dbus_message_get_interface (message)))
4179 _dbus_string_free (&str);
4180 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4181 _dbus_verbose ("no memory for error string in dispatch\n");
4185 reply = dbus_message_new_error (message,
4186 DBUS_ERROR_UNKNOWN_METHOD,
4187 _dbus_string_get_const_data (&str));
4188 _dbus_string_free (&str);
4192 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4193 _dbus_verbose ("no memory for error reply in dispatch\n");
4197 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4199 if (preallocated == NULL)
4201 dbus_message_unref (reply);
4202 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4203 _dbus_verbose ("no memory for error send in dispatch\n");
4207 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4210 dbus_message_unref (reply);
4212 result = DBUS_HANDLER_RESULT_HANDLED;
4215 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
4216 dbus_message_get_type (message),
4217 dbus_message_get_interface (message) ?
4218 dbus_message_get_interface (message) :
4220 dbus_message_get_member (message) ?
4221 dbus_message_get_member (message) :
4223 dbus_message_get_signature (message),
4227 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4229 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
4231 /* Put message back, and we'll start over.
4232 * Yes this means handlers must be idempotent if they
4233 * don't return HANDLED; c'est la vie.
4235 _dbus_connection_putback_message_link_unlocked (connection,
4240 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
4242 _dbus_list_free_link (message_link);
4243 dbus_message_unref (message); /* don't want the message to count in max message limits
4244 * in computing dispatch status below
4248 _dbus_connection_release_dispatch (connection);
4249 HAVE_LOCK_CHECK (connection);
4251 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
4252 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4254 /* unlocks and calls user code */
4255 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4257 dbus_connection_unref (connection);
4263 * Sets the watch functions for the connection. These functions are
4264 * responsible for making the application's main loop aware of file
4265 * descriptors that need to be monitored for events, using select() or
4266 * poll(). When using Qt, typically the DBusAddWatchFunction would
4267 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
4268 * could call g_io_add_watch(), or could be used as part of a more
4269 * elaborate GSource. Note that when a watch is added, it may
4272 * The DBusWatchToggledFunction notifies the application that the
4273 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
4274 * to check this. A disabled watch should have no effect, and enabled
4275 * watch should be added to the main loop. This feature is used
4276 * instead of simply adding/removing the watch because
4277 * enabling/disabling can be done without memory allocation. The
4278 * toggled function may be NULL if a main loop re-queries
4279 * dbus_watch_get_enabled() every time anyway.
4281 * The DBusWatch can be queried for the file descriptor to watch using
4282 * dbus_watch_get_fd(), and for the events to watch for using
4283 * dbus_watch_get_flags(). The flags returned by
4284 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
4285 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
4286 * all watches implicitly include a watch for hangups, errors, and
4287 * other exceptional conditions.
4289 * Once a file descriptor becomes readable or writable, or an exception
4290 * occurs, dbus_watch_handle() should be called to
4291 * notify the connection of the file descriptor's condition.
4293 * dbus_watch_handle() cannot be called during the
4294 * DBusAddWatchFunction, as the connection will not be ready to handle
4297 * It is not allowed to reference a DBusWatch after it has been passed
4298 * to remove_function.
4300 * If #FALSE is returned due to lack of memory, the failure may be due
4301 * to a #FALSE return from the new add_function. If so, the
4302 * add_function may have been called successfully one or more times,
4303 * but the remove_function will also have been called to remove any
4304 * successful adds. i.e. if #FALSE is returned the net result
4305 * should be that dbus_connection_set_watch_functions() has no effect,
4306 * but the add_function and remove_function may have been called.
4308 * @todo We need to drop the lock when we call the
4309 * add/remove/toggled functions which can be a side effect
4310 * of setting the watch functions.
4312 * @param connection the connection.
4313 * @param add_function function to begin monitoring a new descriptor.
4314 * @param remove_function function to stop monitoring a descriptor.
4315 * @param toggled_function function to notify of enable/disable
4316 * @param data data to pass to add_function and remove_function.
4317 * @param free_data_function function to be called to free the data.
4318 * @returns #FALSE on failure (no memory)
4321 dbus_connection_set_watch_functions (DBusConnection *connection,
4322 DBusAddWatchFunction add_function,
4323 DBusRemoveWatchFunction remove_function,
4324 DBusWatchToggledFunction toggled_function,
4326 DBusFreeFunction free_data_function)
4329 DBusWatchList *watches;
4331 _dbus_return_val_if_fail (connection != NULL, FALSE);
4333 CONNECTION_LOCK (connection);
4335 #ifndef DBUS_DISABLE_CHECKS
4336 if (connection->watches == NULL)
4338 _dbus_warn ("Re-entrant call to %s is not allowed\n",
4339 _DBUS_FUNCTION_NAME);
4344 /* ref connection for slightly better reentrancy */
4345 _dbus_connection_ref_unlocked (connection);
4347 /* This can call back into user code, and we need to drop the
4348 * connection lock when it does. This is kind of a lame
4351 watches = connection->watches;
4352 connection->watches = NULL;
4353 CONNECTION_UNLOCK (connection);
4355 retval = _dbus_watch_list_set_functions (watches,
4356 add_function, remove_function,
4358 data, free_data_function);
4359 CONNECTION_LOCK (connection);
4360 connection->watches = watches;
4362 CONNECTION_UNLOCK (connection);
4363 /* drop our paranoid refcount */
4364 dbus_connection_unref (connection);
4370 * Sets the timeout functions for the connection. These functions are
4371 * responsible for making the application's main loop aware of timeouts.
4372 * When using Qt, typically the DBusAddTimeoutFunction would create a
4373 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
4376 * The DBusTimeoutToggledFunction notifies the application that the
4377 * timeout has been enabled or disabled. Call
4378 * dbus_timeout_get_enabled() to check this. A disabled timeout should
4379 * have no effect, and enabled timeout should be added to the main
4380 * loop. This feature is used instead of simply adding/removing the
4381 * timeout because enabling/disabling can be done without memory
4382 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
4383 * to enable and disable. The toggled function may be NULL if a main
4384 * loop re-queries dbus_timeout_get_enabled() every time anyway.
4385 * Whenever a timeout is toggled, its interval may change.
4387 * The DBusTimeout can be queried for the timer interval using
4388 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
4389 * repeatedly, each time the interval elapses, starting after it has
4390 * elapsed once. The timeout stops firing when it is removed with the
4391 * given remove_function. The timer interval may change whenever the
4392 * timeout is added, removed, or toggled.
4394 * @param connection the connection.
4395 * @param add_function function to add a timeout.
4396 * @param remove_function function to remove a timeout.
4397 * @param toggled_function function to notify of enable/disable
4398 * @param data data to pass to add_function and remove_function.
4399 * @param free_data_function function to be called to free the data.
4400 * @returns #FALSE on failure (no memory)
4403 dbus_connection_set_timeout_functions (DBusConnection *connection,
4404 DBusAddTimeoutFunction add_function,
4405 DBusRemoveTimeoutFunction remove_function,
4406 DBusTimeoutToggledFunction toggled_function,
4408 DBusFreeFunction free_data_function)
4411 DBusTimeoutList *timeouts;
4413 _dbus_return_val_if_fail (connection != NULL, FALSE);
4415 CONNECTION_LOCK (connection);
4417 #ifndef DBUS_DISABLE_CHECKS
4418 if (connection->timeouts == NULL)
4420 _dbus_warn ("Re-entrant call to %s is not allowed\n",
4421 _DBUS_FUNCTION_NAME);
4426 /* ref connection for slightly better reentrancy */
4427 _dbus_connection_ref_unlocked (connection);
4429 timeouts = connection->timeouts;
4430 connection->timeouts = NULL;
4431 CONNECTION_UNLOCK (connection);
4433 retval = _dbus_timeout_list_set_functions (timeouts,
4434 add_function, remove_function,
4436 data, free_data_function);
4437 CONNECTION_LOCK (connection);
4438 connection->timeouts = timeouts;
4440 CONNECTION_UNLOCK (connection);
4441 /* drop our paranoid refcount */
4442 dbus_connection_unref (connection);
4448 * Sets the mainloop wakeup function for the connection. This function is
4449 * responsible for waking up the main loop (if its sleeping) when some some
4450 * change has happened to the connection that the mainloop needs to reconsider
4451 * (e.g. a message has been queued for writing).
4452 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
4453 * When using GLib, it would call g_main_context_wakeup().
4456 * @param connection the connection.
4457 * @param wakeup_main_function function to wake up the mainloop
4458 * @param data data to pass wakeup_main_function
4459 * @param free_data_function function to be called to free the data.
4462 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
4463 DBusWakeupMainFunction wakeup_main_function,
4465 DBusFreeFunction free_data_function)
4468 DBusFreeFunction old_free_data;
4470 _dbus_return_if_fail (connection != NULL);
4472 CONNECTION_LOCK (connection);
4473 old_data = connection->wakeup_main_data;
4474 old_free_data = connection->free_wakeup_main_data;
4476 connection->wakeup_main_function = wakeup_main_function;
4477 connection->wakeup_main_data = data;
4478 connection->free_wakeup_main_data = free_data_function;
4480 CONNECTION_UNLOCK (connection);
4482 /* Callback outside the lock */
4484 (*old_free_data) (old_data);
4488 * Set a function to be invoked when the dispatch status changes.
4489 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
4490 * dbus_connection_dispatch() needs to be called to process incoming
4491 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
4492 * from inside the DBusDispatchStatusFunction. Indeed, almost
4493 * any reentrancy in this function is a bad idea. Instead,
4494 * the DBusDispatchStatusFunction should simply save an indication
4495 * that messages should be dispatched later, when the main loop
4498 * @param connection the connection
4499 * @param function function to call on dispatch status changes
4500 * @param data data for function
4501 * @param free_data_function free the function data
4504 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
4505 DBusDispatchStatusFunction function,
4507 DBusFreeFunction free_data_function)
4510 DBusFreeFunction old_free_data;
4512 _dbus_return_if_fail (connection != NULL);
4514 CONNECTION_LOCK (connection);
4515 old_data = connection->dispatch_status_data;
4516 old_free_data = connection->free_dispatch_status_data;
4518 connection->dispatch_status_function = function;
4519 connection->dispatch_status_data = data;
4520 connection->free_dispatch_status_data = free_data_function;
4522 CONNECTION_UNLOCK (connection);
4524 /* Callback outside the lock */
4526 (*old_free_data) (old_data);
4530 * Get the UNIX file descriptor of the connection, if any. This can
4531 * be used for SELinux access control checks with getpeercon() for
4532 * example. DO NOT read or write to the file descriptor, or try to
4533 * select() on it; use DBusWatch for main loop integration. Not all
4534 * connections will have a file descriptor. So for adding descriptors
4535 * to the main loop, use dbus_watch_get_fd() and so forth.
4537 * If the connection is socket-based, you can also use
4538 * dbus_connection_get_socket(), which will work on Windows too.
4539 * This function always fails on Windows.
4541 * Right now the returned descriptor is always a socket, but
4542 * that is not guaranteed.
4544 * @param connection the connection
4545 * @param fd return location for the file descriptor.
4546 * @returns #TRUE if fd is successfully obtained.
4549 dbus_connection_get_unix_fd (DBusConnection *connection,
4552 _dbus_return_val_if_fail (connection != NULL, FALSE);
4553 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4556 /* FIXME do this on a lower level */
4560 return dbus_connection_get_socket(connection, fd);
4564 * Gets the underlying Windows or UNIX socket file descriptor
4565 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
4566 * select() on it; use DBusWatch for main loop integration. Not all
4567 * connections will have a socket. So for adding descriptors
4568 * to the main loop, use dbus_watch_get_fd() and so forth.
4570 * If the connection is not socket-based, this function will return FALSE,
4571 * even if the connection does have a file descriptor of some kind.
4572 * i.e. this function always returns specifically a socket file descriptor.
4574 * @param connection the connection
4575 * @param fd return location for the file descriptor.
4576 * @returns #TRUE if fd is successfully obtained.
4579 dbus_connection_get_socket(DBusConnection *connection,
4584 _dbus_return_val_if_fail (connection != NULL, FALSE);
4585 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4587 CONNECTION_LOCK (connection);
4589 retval = _dbus_transport_get_socket_fd (connection->transport,
4592 CONNECTION_UNLOCK (connection);
4599 * Gets the UNIX user ID of the connection if any.
4600 * Returns #TRUE if the uid is filled in.
4601 * Always returns #FALSE on non-UNIX platforms.
4602 * Always returns #FALSE prior to authenticating the
4605 * @param connection the connection
4606 * @param uid return location for the user ID
4607 * @returns #TRUE if uid is filled in with a valid user ID
4610 dbus_connection_get_unix_user (DBusConnection *connection,
4615 _dbus_return_val_if_fail (connection != NULL, FALSE);
4616 _dbus_return_val_if_fail (uid != NULL, FALSE);
4619 /* FIXME this should be done at a lower level, but it's kind of hard,
4620 * just want to be sure we don't ship with this API returning
4621 * some weird internal fake uid for 1.0
4626 CONNECTION_LOCK (connection);
4628 if (!_dbus_transport_get_is_authenticated (connection->transport))
4631 result = _dbus_transport_get_unix_user (connection->transport,
4633 CONNECTION_UNLOCK (connection);
4639 * Gets the process ID of the connection if any.
4640 * Returns #TRUE if the uid is filled in.
4641 * Always returns #FALSE prior to authenticating the
4644 * @param connection the connection
4645 * @param pid return location for the process ID
4646 * @returns #TRUE if uid is filled in with a valid process ID
4649 dbus_connection_get_unix_process_id (DBusConnection *connection,
4654 _dbus_return_val_if_fail (connection != NULL, FALSE);
4655 _dbus_return_val_if_fail (pid != NULL, FALSE);
4658 /* FIXME this should be done at a lower level, but it's kind of hard,
4659 * just want to be sure we don't ship with this API returning
4660 * some weird internal fake uid for 1.0
4665 CONNECTION_LOCK (connection);
4667 if (!_dbus_transport_get_is_authenticated (connection->transport))
4670 result = _dbus_transport_get_unix_process_id (connection->transport,
4672 CONNECTION_UNLOCK (connection);
4678 * Sets a predicate function used to determine whether a given user ID
4679 * is allowed to connect. When an incoming connection has
4680 * authenticated with a particular user ID, this function is called;
4681 * if it returns #TRUE, the connection is allowed to proceed,
4682 * otherwise the connection is disconnected.
4684 * If the function is set to #NULL (as it is by default), then
4685 * only the same UID as the server process will be allowed to
4688 * On Windows, the function will be set and its free_data_function will
4689 * be invoked when the connection is freed or a new function is set.
4690 * However, the function will never be called, because there are
4691 * no UNIX user ids to pass to it.
4693 * @todo add a Windows API analogous to dbus_connection_set_unix_user_function()
4695 * @param connection the connection
4696 * @param function the predicate
4697 * @param data data to pass to the predicate
4698 * @param free_data_function function to free the data
4701 dbus_connection_set_unix_user_function (DBusConnection *connection,
4702 DBusAllowUnixUserFunction function,
4704 DBusFreeFunction free_data_function)
4706 void *old_data = NULL;
4707 DBusFreeFunction old_free_function = NULL;
4709 _dbus_return_if_fail (connection != NULL);
4711 CONNECTION_LOCK (connection);
4712 _dbus_transport_set_unix_user_function (connection->transport,
4713 function, data, free_data_function,
4714 &old_data, &old_free_function);
4715 CONNECTION_UNLOCK (connection);
4717 if (old_free_function != NULL)
4718 (* old_free_function) (old_data);
4723 * Normally #DBusConnection automatically handles all messages to the
4724 * org.freedesktop.DBus.Peer interface. However, the message bus wants
4725 * to be able to route methods on that interface through the bus and
4726 * to other applications. If routing peer messages is enabled, then
4727 * messages with the org.freedesktop.DBus.Peer interface that also
4728 * have a bus destination name set will not be automatically
4729 * handled by the #DBusConnection and instead will be dispatched
4730 * normally to the application.
4733 * If a normal application sets this flag, it can break things badly.
4734 * So don't set this unless you are the message bus.
4736 * @param connection the connection
4737 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
4740 dbus_connection_set_route_peer_messages (DBusConnection *connection,
4743 _dbus_return_if_fail (connection != NULL);
4745 CONNECTION_LOCK (connection);
4746 connection->route_peer_messages = TRUE;
4747 CONNECTION_UNLOCK (connection);
4751 * Adds a message filter. Filters are handlers that are run on all
4752 * incoming messages, prior to the objects registered with
4753 * dbus_connection_register_object_path(). Filters are run in the
4754 * order that they were added. The same handler can be added as a
4755 * filter more than once, in which case it will be run more than once.
4756 * Filters added during a filter callback won't be run on the message
4759 * @todo we don't run filters on messages while blocking without
4760 * entering the main loop, since filters are run as part of
4761 * dbus_connection_dispatch(). This is probably a feature, as filters
4762 * could create arbitrary reentrancy. But kind of sucks if you're
4763 * trying to filter METHOD_RETURN for some reason.
4765 * @param connection the connection
4766 * @param function function to handle messages
4767 * @param user_data user data to pass to the function
4768 * @param free_data_function function to use for freeing user data
4769 * @returns #TRUE on success, #FALSE if not enough memory.
4772 dbus_connection_add_filter (DBusConnection *connection,
4773 DBusHandleMessageFunction function,
4775 DBusFreeFunction free_data_function)
4777 DBusMessageFilter *filter;
4779 _dbus_return_val_if_fail (connection != NULL, FALSE);
4780 _dbus_return_val_if_fail (function != NULL, FALSE);
4782 filter = dbus_new0 (DBusMessageFilter, 1);
4786 filter->refcount.value = 1;
4788 CONNECTION_LOCK (connection);
4790 if (!_dbus_list_append (&connection->filter_list,
4793 _dbus_message_filter_unref (filter);
4794 CONNECTION_UNLOCK (connection);
4798 /* Fill in filter after all memory allocated,
4799 * so we don't run the free_user_data_function
4800 * if the add_filter() fails
4803 filter->function = function;
4804 filter->user_data = user_data;
4805 filter->free_user_data_function = free_data_function;
4807 CONNECTION_UNLOCK (connection);
4812 * Removes a previously-added message filter. It is a programming
4813 * error to call this function for a handler that has not been added
4814 * as a filter. If the given handler was added more than once, only
4815 * one instance of it will be removed (the most recently-added
4818 * @param connection the connection
4819 * @param function the handler to remove
4820 * @param user_data user data for the handler to remove
4824 dbus_connection_remove_filter (DBusConnection *connection,
4825 DBusHandleMessageFunction function,
4829 DBusMessageFilter *filter;
4831 _dbus_return_if_fail (connection != NULL);
4832 _dbus_return_if_fail (function != NULL);
4834 CONNECTION_LOCK (connection);
4838 link = _dbus_list_get_last_link (&connection->filter_list);
4839 while (link != NULL)
4841 filter = link->data;
4843 if (filter->function == function &&
4844 filter->user_data == user_data)
4846 _dbus_list_remove_link (&connection->filter_list, link);
4847 filter->function = NULL;
4852 link = _dbus_list_get_prev_link (&connection->filter_list, link);
4855 CONNECTION_UNLOCK (connection);
4857 #ifndef DBUS_DISABLE_CHECKS
4860 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
4861 function, user_data);
4866 /* Call application code */
4867 if (filter->free_user_data_function)
4868 (* filter->free_user_data_function) (filter->user_data);
4870 filter->free_user_data_function = NULL;
4871 filter->user_data = NULL;
4873 _dbus_message_filter_unref (filter);
4877 * Registers a handler for a given path in the object hierarchy.
4878 * The given vtable handles messages sent to exactly the given path.
4881 * @param connection the connection
4882 * @param path a '/' delimited string of path elements
4883 * @param vtable the virtual table
4884 * @param user_data data to pass to functions in the vtable
4885 * @returns #FALSE if not enough memory
4888 dbus_connection_register_object_path (DBusConnection *connection,
4890 const DBusObjectPathVTable *vtable,
4893 char **decomposed_path;
4896 _dbus_return_val_if_fail (connection != NULL, FALSE);
4897 _dbus_return_val_if_fail (path != NULL, FALSE);
4898 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4899 _dbus_return_val_if_fail (vtable != NULL, FALSE);
4901 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4904 CONNECTION_LOCK (connection);
4906 retval = _dbus_object_tree_register (connection->objects,
4908 (const char **) decomposed_path, vtable,
4911 CONNECTION_UNLOCK (connection);
4913 dbus_free_string_array (decomposed_path);
4919 * Registers a fallback handler for a given subsection of the object
4920 * hierarchy. The given vtable handles messages at or below the given
4921 * path. You can use this to establish a default message handling
4922 * policy for a whole "subdirectory."
4924 * @param connection the connection
4925 * @param path a '/' delimited string of path elements
4926 * @param vtable the virtual table
4927 * @param user_data data to pass to functions in the vtable
4928 * @returns #FALSE if not enough memory
4931 dbus_connection_register_fallback (DBusConnection *connection,
4933 const DBusObjectPathVTable *vtable,
4936 char **decomposed_path;
4939 _dbus_return_val_if_fail (connection != NULL, FALSE);
4940 _dbus_return_val_if_fail (path != NULL, FALSE);
4941 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4942 _dbus_return_val_if_fail (vtable != NULL, FALSE);
4944 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4947 CONNECTION_LOCK (connection);
4949 retval = _dbus_object_tree_register (connection->objects,
4951 (const char **) decomposed_path, vtable,
4954 CONNECTION_UNLOCK (connection);
4956 dbus_free_string_array (decomposed_path);
4962 * Unregisters the handler registered with exactly the given path.
4963 * It's a bug to call this function for a path that isn't registered.
4964 * Can unregister both fallback paths and object paths.
4966 * @param connection the connection
4967 * @param path a '/' delimited string of path elements
4968 * @returns #FALSE if not enough memory
4971 dbus_connection_unregister_object_path (DBusConnection *connection,
4974 char **decomposed_path;
4976 _dbus_return_val_if_fail (connection != NULL, FALSE);
4977 _dbus_return_val_if_fail (path != NULL, FALSE);
4978 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4980 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4983 CONNECTION_LOCK (connection);
4985 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
4987 dbus_free_string_array (decomposed_path);
4993 * Gets the user data passed to dbus_connection_register_object_path()
4994 * or dbus_connection_register_fallback(). If nothing was registered
4995 * at this path, the data is filled in with #NULL.
4997 * @param connection the connection
4998 * @param path the path you registered with
4999 * @param data_p location to store the user data, or #NULL
5000 * @returns #FALSE if not enough memory
5003 dbus_connection_get_object_path_data (DBusConnection *connection,
5007 char **decomposed_path;
5009 _dbus_return_val_if_fail (connection != NULL, FALSE);
5010 _dbus_return_val_if_fail (path != NULL, FALSE);
5011 _dbus_return_val_if_fail (data_p != NULL, FALSE);
5015 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5018 CONNECTION_LOCK (connection);
5020 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
5022 CONNECTION_UNLOCK (connection);
5024 dbus_free_string_array (decomposed_path);
5030 * Lists the registered fallback handlers and object path handlers at
5031 * the given parent_path. The returned array should be freed with
5032 * dbus_free_string_array().
5034 * @param connection the connection
5035 * @param parent_path the path to list the child handlers of
5036 * @param child_entries returns #NULL-terminated array of children
5037 * @returns #FALSE if no memory to allocate the child entries
5040 dbus_connection_list_registered (DBusConnection *connection,
5041 const char *parent_path,
5042 char ***child_entries)
5044 char **decomposed_path;
5046 _dbus_return_val_if_fail (connection != NULL, FALSE);
5047 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
5048 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
5049 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
5051 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
5054 CONNECTION_LOCK (connection);
5056 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
5057 (const char **) decomposed_path,
5059 dbus_free_string_array (decomposed_path);
5064 static DBusDataSlotAllocator slot_allocator;
5065 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
5068 * Allocates an integer ID to be used for storing application-specific
5069 * data on any DBusConnection. The allocated ID may then be used
5070 * with dbus_connection_set_data() and dbus_connection_get_data().
5071 * The passed-in slot must be initialized to -1, and is filled in
5072 * with the slot ID. If the passed-in slot is not -1, it's assumed
5073 * to be already allocated, and its refcount is incremented.
5075 * The allocated slot is global, i.e. all DBusConnection objects will
5076 * have a slot with the given integer ID reserved.
5078 * @param slot_p address of a global variable storing the slot
5079 * @returns #FALSE on failure (no memory)
5082 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
5084 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5085 &_DBUS_LOCK_NAME (connection_slots),
5090 * Deallocates a global ID for connection data slots.
5091 * dbus_connection_get_data() and dbus_connection_set_data() may no
5092 * longer be used with this slot. Existing data stored on existing
5093 * DBusConnection objects will be freed when the connection is
5094 * finalized, but may not be retrieved (and may only be replaced if
5095 * someone else reallocates the slot). When the refcount on the
5096 * passed-in slot reaches 0, it is set to -1.
5098 * @param slot_p address storing the slot to deallocate
5101 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
5103 _dbus_return_if_fail (*slot_p >= 0);
5105 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5109 * Stores a pointer on a DBusConnection, along
5110 * with an optional function to be used for freeing
5111 * the data when the data is set again, or when
5112 * the connection is finalized. The slot number
5113 * must have been allocated with dbus_connection_allocate_data_slot().
5115 * @param connection the connection
5116 * @param slot the slot number
5117 * @param data the data to store
5118 * @param free_data_func finalizer function for the data
5119 * @returns #TRUE if there was enough memory to store the data
5122 dbus_connection_set_data (DBusConnection *connection,
5125 DBusFreeFunction free_data_func)
5127 DBusFreeFunction old_free_func;
5131 _dbus_return_val_if_fail (connection != NULL, FALSE);
5132 _dbus_return_val_if_fail (slot >= 0, FALSE);
5134 CONNECTION_LOCK (connection);
5136 retval = _dbus_data_slot_list_set (&slot_allocator,
5137 &connection->slot_list,
5138 slot, data, free_data_func,
5139 &old_free_func, &old_data);
5141 CONNECTION_UNLOCK (connection);
5145 /* Do the actual free outside the connection lock */
5147 (* old_free_func) (old_data);
5154 * Retrieves data previously set with dbus_connection_set_data().
5155 * The slot must still be allocated (must not have been freed).
5157 * @param connection the connection
5158 * @param slot the slot to get data from
5159 * @returns the data, or #NULL if not found
5162 dbus_connection_get_data (DBusConnection *connection,
5167 _dbus_return_val_if_fail (connection != NULL, NULL);
5169 CONNECTION_LOCK (connection);
5171 res = _dbus_data_slot_list_get (&slot_allocator,
5172 &connection->slot_list,
5175 CONNECTION_UNLOCK (connection);
5181 * This function sets a global flag for whether dbus_connection_new()
5182 * will set SIGPIPE behavior to SIG_IGN.
5184 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
5187 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
5189 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
5193 * Specifies the maximum size message this connection is allowed to
5194 * receive. Larger messages will result in disconnecting the
5197 * @param connection a #DBusConnection
5198 * @param size maximum message size the connection can receive, in bytes
5201 dbus_connection_set_max_message_size (DBusConnection *connection,
5204 _dbus_return_if_fail (connection != NULL);
5206 CONNECTION_LOCK (connection);
5207 _dbus_transport_set_max_message_size (connection->transport,
5209 CONNECTION_UNLOCK (connection);
5213 * Gets the value set by dbus_connection_set_max_message_size().
5215 * @param connection the connection
5216 * @returns the max size of a single message
5219 dbus_connection_get_max_message_size (DBusConnection *connection)
5223 _dbus_return_val_if_fail (connection != NULL, 0);
5225 CONNECTION_LOCK (connection);
5226 res = _dbus_transport_get_max_message_size (connection->transport);
5227 CONNECTION_UNLOCK (connection);
5232 * Sets the maximum total number of bytes that can be used for all messages
5233 * received on this connection. Messages count toward the maximum until
5234 * they are finalized. When the maximum is reached, the connection will
5235 * not read more data until some messages are finalized.
5237 * The semantics of the maximum are: if outstanding messages are
5238 * already above the maximum, additional messages will not be read.
5239 * The semantics are not: if the next message would cause us to exceed
5240 * the maximum, we don't read it. The reason is that we don't know the
5241 * size of a message until after we read it.
5243 * Thus, the max live messages size can actually be exceeded
5244 * by up to the maximum size of a single message.
5246 * Also, if we read say 1024 bytes off the wire in a single read(),
5247 * and that contains a half-dozen small messages, we may exceed the
5248 * size max by that amount. But this should be inconsequential.
5250 * This does imply that we can't call read() with a buffer larger
5251 * than we're willing to exceed this limit by.
5253 * @param connection the connection
5254 * @param size the maximum size in bytes of all outstanding messages
5257 dbus_connection_set_max_received_size (DBusConnection *connection,
5260 _dbus_return_if_fail (connection != NULL);
5262 CONNECTION_LOCK (connection);
5263 _dbus_transport_set_max_received_size (connection->transport,
5265 CONNECTION_UNLOCK (connection);
5269 * Gets the value set by dbus_connection_set_max_received_size().
5271 * @param connection the connection
5272 * @returns the max size of all live messages
5275 dbus_connection_get_max_received_size (DBusConnection *connection)
5279 _dbus_return_val_if_fail (connection != NULL, 0);
5281 CONNECTION_LOCK (connection);
5282 res = _dbus_transport_get_max_received_size (connection->transport);
5283 CONNECTION_UNLOCK (connection);
5288 * Gets the approximate size in bytes of all messages in the outgoing
5289 * message queue. The size is approximate in that you shouldn't use
5290 * it to decide how many bytes to read off the network or anything
5291 * of that nature, as optimizations may choose to tell small white lies
5292 * to avoid performance overhead.
5294 * @param connection the connection
5295 * @returns the number of bytes that have been queued up but not sent
5298 dbus_connection_get_outgoing_size (DBusConnection *connection)
5302 _dbus_return_val_if_fail (connection != NULL, 0);
5304 CONNECTION_LOCK (connection);
5305 res = _dbus_counter_get_value (connection->outgoing_counter);
5306 CONNECTION_UNLOCK (connection);
5311 * Obtains the machine UUID of the machine this process is running on.
5313 * The returned string must be freed with dbus_free().
5315 * This UUID is guaranteed to remain the same until the next reboot
5316 * (unless the sysadmin foolishly changes it and screws themselves).
5317 * It will usually remain the same across reboots also, but hardware
5318 * configuration changes or rebuilding the machine could break that.
5320 * The idea is that two processes with the same machine ID should be
5321 * able to use shared memory, UNIX domain sockets, process IDs, and other
5322 * features of the OS that require both processes to be running
5323 * on the same OS kernel instance.
5325 * The machine ID can also be used to create unique per-machine
5326 * instances. For example, you could use it in bus names or
5327 * X selection names.
5329 * The machine ID is preferred over the machine hostname, because
5330 * the hostname is frequently set to "localhost.localdomain" and
5331 * may also change at runtime.
5333 * You can get the machine ID of a remote application by invoking the
5334 * method GetMachineId from interface org.freedesktop.DBus.Peer.
5336 * If the remote application has the same machine ID as the one
5337 * returned by this function, then the remote application is on the
5338 * same machine as your application.
5340 * @returns a 32-byte-long hex-encoded UUID string, or #NULL if insufficient memory
5343 dbus_get_local_machine_id (void)
5349 _dbus_string_init (&uuid);
5350 if (!_dbus_get_local_machine_uuid_encoded (&uuid) ||
5351 !_dbus_string_steal_data (&uuid, &s))
5353 _dbus_string_free (&uuid);
5358 _dbus_string_free (&uuid);