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 connection can go in shared_connections once we know the GUID */
243 unsigned int shared : 1; /** < #TRUE if connection is shared and we hold a ref to it */
245 unsigned int dispatch_acquired : 1; /**< Someone has dispatch path (can drain incoming queue) */
246 unsigned int io_path_acquired : 1; /**< Someone has transport io path (can use the transport to read/write messages) */
248 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
250 unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */
252 #ifndef DBUS_DISABLE_CHECKS
253 unsigned int have_connection_lock : 1; /**< Used to check locking */
256 #ifndef DBUS_DISABLE_CHECKS
257 int generation; /**< _dbus_current_generation that should correspond to this connection */
261 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
262 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
263 DBusDispatchStatus new_status);
264 static void _dbus_connection_last_unref (DBusConnection *connection);
265 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
266 static void _dbus_connection_release_dispatch (DBusConnection *connection);
267 static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
269 static DBusMessageFilter *
270 _dbus_message_filter_ref (DBusMessageFilter *filter)
272 _dbus_assert (filter->refcount.value > 0);
273 _dbus_atomic_inc (&filter->refcount);
279 _dbus_message_filter_unref (DBusMessageFilter *filter)
281 _dbus_assert (filter->refcount.value > 0);
283 if (_dbus_atomic_dec (&filter->refcount) == 1)
285 if (filter->free_user_data_function)
286 (* filter->free_user_data_function) (filter->user_data);
293 * Acquires the connection lock.
295 * @param connection the connection.
298 _dbus_connection_lock (DBusConnection *connection)
300 CONNECTION_LOCK (connection);
304 * Releases the connection lock.
306 * @param connection the connection.
309 _dbus_connection_unlock (DBusConnection *connection)
311 CONNECTION_UNLOCK (connection);
315 * Wakes up the main loop if it is sleeping
316 * Needed if we're e.g. queueing outgoing messages
317 * on a thread while the mainloop sleeps.
319 * @param connection the connection.
322 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
324 if (connection->wakeup_main_function)
325 (*connection->wakeup_main_function) (connection->wakeup_main_data);
328 #ifdef DBUS_BUILD_TESTS
329 /* For now this function isn't used */
331 * Adds a message to the incoming message queue, returning #FALSE
332 * if there's insufficient memory to queue the message.
333 * Does not take over refcount of the message.
335 * @param connection the connection.
336 * @param message the message to queue.
337 * @returns #TRUE on success.
340 _dbus_connection_queue_received_message (DBusConnection *connection,
341 DBusMessage *message)
345 link = _dbus_list_alloc_link (message);
349 dbus_message_ref (message);
350 _dbus_connection_queue_received_message_link (connection, link);
356 * Gets the locks so we can examine them
358 * @param connection the connection.
359 * @param mutex_loc return for the location of the main mutex pointer
360 * @param dispatch_mutex_loc return location of the dispatch mutex pointer
361 * @param io_path_mutex_loc return location of the io_path mutex pointer
362 * @param dispatch_cond_loc return location of the dispatch conditional
364 * @param io_path_cond_loc return location of the io_path conditional
368 _dbus_connection_test_get_locks (DBusConnection *conn,
369 DBusMutex **mutex_loc,
370 DBusMutex **dispatch_mutex_loc,
371 DBusMutex **io_path_mutex_loc,
372 DBusCondVar **dispatch_cond_loc,
373 DBusCondVar **io_path_cond_loc)
375 *mutex_loc = conn->mutex;
376 *dispatch_mutex_loc = conn->dispatch_mutex;
377 *io_path_mutex_loc = conn->io_path_mutex;
378 *dispatch_cond_loc = conn->dispatch_cond;
379 *io_path_cond_loc = conn->io_path_cond;
384 * Adds a message-containing list link to the incoming message queue,
385 * taking ownership of the link and the message's current refcount.
386 * Cannot fail due to lack of memory.
388 * @param connection the connection.
389 * @param link the message link to queue.
392 _dbus_connection_queue_received_message_link (DBusConnection *connection,
395 DBusPendingCall *pending;
396 dbus_int32_t reply_serial;
397 DBusMessage *message;
399 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
401 _dbus_list_append_link (&connection->incoming_messages,
403 message = link->data;
405 /* If this is a reply we're waiting on, remove timeout for it */
406 reply_serial = dbus_message_get_reply_serial (message);
407 if (reply_serial != -1)
409 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
413 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
414 _dbus_connection_remove_timeout_unlocked (connection,
415 _dbus_pending_call_get_timeout_unlocked (pending));
417 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
423 connection->n_incoming += 1;
425 _dbus_connection_wakeup_mainloop (connection);
427 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
429 dbus_message_get_type (message),
430 dbus_message_get_path (message) ?
431 dbus_message_get_path (message) :
433 dbus_message_get_interface (message) ?
434 dbus_message_get_interface (message) :
436 dbus_message_get_member (message) ?
437 dbus_message_get_member (message) :
439 dbus_message_get_signature (message),
440 dbus_message_get_reply_serial (message),
442 connection->n_incoming);}
445 * Adds a link + message to the incoming message queue.
446 * Can't fail. Takes ownership of both link and message.
448 * @param connection the connection.
449 * @param link the list node and message to queue.
453 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
456 HAVE_LOCK_CHECK (connection);
458 _dbus_list_append_link (&connection->incoming_messages, link);
460 connection->n_incoming += 1;
462 _dbus_connection_wakeup_mainloop (connection);
464 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
465 link->data, connection, connection->n_incoming);
470 * Checks whether there are messages in the outgoing message queue.
471 * Called with connection lock held.
473 * @param connection the connection.
474 * @returns #TRUE if the outgoing queue is non-empty.
477 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
479 HAVE_LOCK_CHECK (connection);
480 return connection->outgoing_messages != NULL;
484 * Checks whether there are messages in the outgoing message queue.
486 * @param connection the connection.
487 * @returns #TRUE if the outgoing queue is non-empty.
490 dbus_connection_has_messages_to_send (DBusConnection *connection)
494 _dbus_return_val_if_fail (connection != NULL, FALSE);
496 CONNECTION_LOCK (connection);
497 v = _dbus_connection_has_messages_to_send_unlocked (connection);
498 CONNECTION_UNLOCK (connection);
504 * Gets the next outgoing message. The message remains in the
505 * queue, and the caller does not own a reference to it.
507 * @param connection the connection.
508 * @returns the message to be sent.
511 _dbus_connection_get_message_to_send (DBusConnection *connection)
513 HAVE_LOCK_CHECK (connection);
515 return _dbus_list_get_last (&connection->outgoing_messages);
519 * Notifies the connection that a message has been sent, so the
520 * message can be removed from the outgoing queue.
521 * Called with the connection lock held.
523 * @param connection the connection.
524 * @param message the message that was sent.
527 _dbus_connection_message_sent (DBusConnection *connection,
528 DBusMessage *message)
532 HAVE_LOCK_CHECK (connection);
534 /* This can be called before we even complete authentication, since
535 * it's called on disconnect to clean up the outgoing queue.
536 * It's also called as we successfully send each message.
539 link = _dbus_list_get_last_link (&connection->outgoing_messages);
540 _dbus_assert (link != NULL);
541 _dbus_assert (link->data == message);
543 /* Save this link in the link cache */
544 _dbus_list_unlink (&connection->outgoing_messages,
546 _dbus_list_prepend_link (&connection->link_cache, link);
548 connection->n_outgoing -= 1;
550 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
552 dbus_message_get_type (message),
553 dbus_message_get_path (message) ?
554 dbus_message_get_path (message) :
556 dbus_message_get_interface (message) ?
557 dbus_message_get_interface (message) :
559 dbus_message_get_member (message) ?
560 dbus_message_get_member (message) :
562 dbus_message_get_signature (message),
563 connection, connection->n_outgoing);
565 /* Save this link in the link cache also */
566 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
568 _dbus_list_prepend_link (&connection->link_cache, link);
570 dbus_message_unref (message);
573 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
575 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
577 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
579 dbus_bool_t enabled);
582 protected_change_watch (DBusConnection *connection,
584 DBusWatchAddFunction add_function,
585 DBusWatchRemoveFunction remove_function,
586 DBusWatchToggleFunction toggle_function,
589 DBusWatchList *watches;
592 HAVE_LOCK_CHECK (connection);
594 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
595 * drop lock and call out" one; but it has to be propagated up through all callers
598 watches = connection->watches;
601 connection->watches = NULL;
602 _dbus_connection_ref_unlocked (connection);
603 CONNECTION_UNLOCK (connection);
606 retval = (* add_function) (watches, watch);
607 else if (remove_function)
610 (* remove_function) (watches, watch);
615 (* toggle_function) (watches, watch, enabled);
618 CONNECTION_LOCK (connection);
619 connection->watches = watches;
620 _dbus_connection_unref_unlocked (connection);
630 * Adds a watch using the connection's DBusAddWatchFunction if
631 * available. Otherwise records the watch to be added when said
632 * function is available. Also re-adds the watch if the
633 * DBusAddWatchFunction changes. May fail due to lack of memory.
634 * Connection lock should be held when calling this.
636 * @param connection the connection.
637 * @param watch the watch to add.
638 * @returns #TRUE on success.
641 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
644 return protected_change_watch (connection, watch,
645 _dbus_watch_list_add_watch,
650 * Removes a watch using the connection's DBusRemoveWatchFunction
651 * if available. It's an error to call this function on a watch
652 * that was not previously added.
653 * Connection lock should be held when calling this.
655 * @param connection the connection.
656 * @param watch the watch to remove.
659 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
662 protected_change_watch (connection, watch,
664 _dbus_watch_list_remove_watch,
669 * Toggles a watch and notifies app via connection's
670 * DBusWatchToggledFunction if available. It's an error to call this
671 * function on a watch that was not previously added.
672 * Connection lock should be held when calling this.
674 * @param connection the connection.
675 * @param watch the watch to toggle.
676 * @param enabled whether to enable or disable
679 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
683 _dbus_assert (watch != NULL);
685 protected_change_watch (connection, watch,
687 _dbus_watch_list_toggle_watch,
691 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
692 DBusTimeout *timeout);
693 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
694 DBusTimeout *timeout);
695 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
696 DBusTimeout *timeout,
697 dbus_bool_t enabled);
700 protected_change_timeout (DBusConnection *connection,
701 DBusTimeout *timeout,
702 DBusTimeoutAddFunction add_function,
703 DBusTimeoutRemoveFunction remove_function,
704 DBusTimeoutToggleFunction toggle_function,
707 DBusTimeoutList *timeouts;
710 HAVE_LOCK_CHECK (connection);
712 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
713 * drop lock and call out" one; but it has to be propagated up through all callers
716 timeouts = connection->timeouts;
719 connection->timeouts = NULL;
720 _dbus_connection_ref_unlocked (connection);
721 CONNECTION_UNLOCK (connection);
724 retval = (* add_function) (timeouts, timeout);
725 else if (remove_function)
728 (* remove_function) (timeouts, timeout);
733 (* toggle_function) (timeouts, timeout, enabled);
736 CONNECTION_LOCK (connection);
737 connection->timeouts = timeouts;
738 _dbus_connection_unref_unlocked (connection);
747 * Adds a timeout using the connection's DBusAddTimeoutFunction if
748 * available. Otherwise records the timeout to be added when said
749 * function is available. Also re-adds the timeout if the
750 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
751 * The timeout will fire repeatedly until removed.
752 * Connection lock should be held when calling this.
754 * @param connection the connection.
755 * @param timeout the timeout to add.
756 * @returns #TRUE on success.
759 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
760 DBusTimeout *timeout)
762 return protected_change_timeout (connection, timeout,
763 _dbus_timeout_list_add_timeout,
768 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
769 * if available. It's an error to call this function on a timeout
770 * that was not previously added.
771 * Connection lock should be held when calling this.
773 * @param connection the connection.
774 * @param timeout the timeout to remove.
777 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
778 DBusTimeout *timeout)
780 protected_change_timeout (connection, timeout,
782 _dbus_timeout_list_remove_timeout,
787 * Toggles a timeout and notifies app via connection's
788 * DBusTimeoutToggledFunction if available. It's an error to call this
789 * function on a timeout that was not previously added.
790 * Connection lock should be held when calling this.
792 * @param connection the connection.
793 * @param timeout the timeout to toggle.
794 * @param enabled whether to enable or disable
797 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
798 DBusTimeout *timeout,
801 protected_change_timeout (connection, timeout,
803 _dbus_timeout_list_toggle_timeout,
808 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
809 DBusPendingCall *pending)
811 dbus_uint32_t reply_serial;
812 DBusTimeout *timeout;
814 HAVE_LOCK_CHECK (connection);
816 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
818 _dbus_assert (reply_serial != 0);
820 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
822 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
825 if (!_dbus_hash_table_insert_int (connection->pending_replies,
829 _dbus_connection_remove_timeout_unlocked (connection, timeout);
831 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
832 HAVE_LOCK_CHECK (connection);
836 _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
838 _dbus_pending_call_ref_unlocked (pending);
840 HAVE_LOCK_CHECK (connection);
846 free_pending_call_on_hash_removal (void *data)
848 DBusPendingCall *pending;
849 DBusConnection *connection;
856 connection = _dbus_pending_call_get_connection_unlocked (pending);
858 HAVE_LOCK_CHECK (connection);
860 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
862 _dbus_connection_remove_timeout_unlocked (connection,
863 _dbus_pending_call_get_timeout_unlocked (pending));
865 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
868 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
869 * here, but the pending call finalizer could in principle call out to
870 * application code so we pretty much have to... some larger code reorg
873 _dbus_connection_ref_unlocked (connection);
874 _dbus_pending_call_unref_and_unlock (pending);
875 CONNECTION_LOCK (connection);
876 _dbus_connection_unref_unlocked (connection);
880 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
881 DBusPendingCall *pending)
883 /* This ends up unlocking to call the pending call finalizer, which is unexpected to
886 _dbus_hash_table_remove_int (connection->pending_replies,
887 _dbus_pending_call_get_reply_serial_unlocked (pending));
891 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
892 DBusPendingCall *pending)
894 /* The idea here is to avoid finalizing the pending call
895 * with the lock held, since there's a destroy notifier
896 * in pending call that goes out to application code.
898 * There's an extra unlock inside the hash table
899 * "free pending call" function FIXME...
901 _dbus_pending_call_ref_unlocked (pending);
902 _dbus_hash_table_remove_int (connection->pending_replies,
903 _dbus_pending_call_get_reply_serial_unlocked (pending));
904 _dbus_pending_call_unref_and_unlock (pending);
908 * Removes a pending call from the connection, such that
909 * the pending reply will be ignored. May drop the last
910 * reference to the pending call.
912 * @param connection the connection
913 * @param pending the pending call
916 _dbus_connection_remove_pending_call (DBusConnection *connection,
917 DBusPendingCall *pending)
919 CONNECTION_LOCK (connection);
920 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
924 * Acquire the transporter I/O path. This must be done before
925 * doing any I/O in the transporter. May sleep and drop the
926 * IO path mutex while waiting for the I/O path.
928 * @param connection the connection.
929 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
930 * @returns TRUE if the I/O path was acquired.
933 _dbus_connection_acquire_io_path (DBusConnection *connection,
934 int timeout_milliseconds)
936 dbus_bool_t we_acquired;
938 HAVE_LOCK_CHECK (connection);
940 /* We don't want the connection to vanish */
941 _dbus_connection_ref_unlocked (connection);
943 /* We will only touch io_path_acquired which is protected by our mutex */
944 CONNECTION_UNLOCK (connection);
946 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
947 _dbus_mutex_lock (connection->io_path_mutex);
949 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
950 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
954 if (connection->io_path_acquired)
956 if (timeout_milliseconds != -1)
958 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
959 _DBUS_FUNCTION_NAME, timeout_milliseconds);
960 _dbus_condvar_wait_timeout (connection->io_path_cond,
961 connection->io_path_mutex,
962 timeout_milliseconds);
966 while (connection->io_path_acquired)
968 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
969 _dbus_condvar_wait (connection->io_path_cond,
970 connection->io_path_mutex);
975 if (!connection->io_path_acquired)
978 connection->io_path_acquired = TRUE;
981 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
982 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
984 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
985 _dbus_mutex_unlock (connection->io_path_mutex);
987 CONNECTION_LOCK (connection);
989 HAVE_LOCK_CHECK (connection);
991 _dbus_connection_unref_unlocked (connection);
997 * Release the I/O path when you're done with it. Only call
998 * after you've acquired the I/O. Wakes up at most one thread
999 * currently waiting to acquire the I/O path.
1001 * @param connection the connection.
1004 _dbus_connection_release_io_path (DBusConnection *connection)
1006 HAVE_LOCK_CHECK (connection);
1008 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1009 _dbus_mutex_lock (connection->io_path_mutex);
1011 _dbus_assert (connection->io_path_acquired);
1013 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
1014 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
1016 connection->io_path_acquired = FALSE;
1017 _dbus_condvar_wake_one (connection->io_path_cond);
1019 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1020 _dbus_mutex_unlock (connection->io_path_mutex);
1024 * Queues incoming messages and sends outgoing messages for this
1025 * connection, optionally blocking in the process. Each call to
1026 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1027 * time and then read or write data if possible.
1029 * The purpose of this function is to be able to flush outgoing
1030 * messages or queue up incoming messages without returning
1031 * control to the application and causing reentrancy weirdness.
1033 * The flags parameter allows you to specify whether to
1034 * read incoming messages, write outgoing messages, or both,
1035 * and whether to block if no immediate action is possible.
1037 * The timeout_milliseconds parameter does nothing unless the
1038 * iteration is blocking.
1040 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1041 * wasn't specified, then it's impossible to block, even if
1042 * you specify DBUS_ITERATION_BLOCK; in that case the function
1043 * returns immediately.
1045 * Called with connection lock held.
1047 * @param connection the connection.
1048 * @param flags iteration flags.
1049 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1052 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1054 int timeout_milliseconds)
1056 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1058 HAVE_LOCK_CHECK (connection);
1060 if (connection->n_outgoing == 0)
1061 flags &= ~DBUS_ITERATION_DO_WRITING;
1063 if (_dbus_connection_acquire_io_path (connection,
1064 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1066 HAVE_LOCK_CHECK (connection);
1068 _dbus_transport_do_iteration (connection->transport,
1069 flags, timeout_milliseconds);
1070 _dbus_connection_release_io_path (connection);
1073 HAVE_LOCK_CHECK (connection);
1075 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1079 * Creates a new connection for the given transport. A transport
1080 * represents a message stream that uses some concrete mechanism, such
1081 * as UNIX domain sockets. May return #NULL if insufficient
1082 * memory exists to create the connection.
1084 * @param transport the transport.
1085 * @returns the new connection, or #NULL on failure.
1088 _dbus_connection_new_for_transport (DBusTransport *transport)
1090 DBusConnection *connection;
1091 DBusWatchList *watch_list;
1092 DBusTimeoutList *timeout_list;
1093 DBusHashTable *pending_replies;
1094 DBusList *disconnect_link;
1095 DBusMessage *disconnect_message;
1096 DBusCounter *outgoing_counter;
1097 DBusObjectTree *objects;
1101 pending_replies = NULL;
1102 timeout_list = NULL;
1103 disconnect_link = NULL;
1104 disconnect_message = NULL;
1105 outgoing_counter = NULL;
1108 watch_list = _dbus_watch_list_new ();
1109 if (watch_list == NULL)
1112 timeout_list = _dbus_timeout_list_new ();
1113 if (timeout_list == NULL)
1117 _dbus_hash_table_new (DBUS_HASH_INT,
1119 (DBusFreeFunction)free_pending_call_on_hash_removal);
1120 if (pending_replies == NULL)
1123 connection = dbus_new0 (DBusConnection, 1);
1124 if (connection == NULL)
1127 _dbus_mutex_new_at_location (&connection->mutex);
1128 if (connection->mutex == NULL)
1131 _dbus_mutex_new_at_location (&connection->io_path_mutex);
1132 if (connection->io_path_mutex == NULL)
1135 _dbus_mutex_new_at_location (&connection->dispatch_mutex);
1136 if (connection->dispatch_mutex == NULL)
1139 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1140 if (connection->dispatch_cond == NULL)
1143 _dbus_condvar_new_at_location (&connection->io_path_cond);
1144 if (connection->io_path_cond == NULL)
1147 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1148 DBUS_INTERFACE_LOCAL,
1151 if (disconnect_message == NULL)
1154 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1155 if (disconnect_link == NULL)
1158 outgoing_counter = _dbus_counter_new ();
1159 if (outgoing_counter == NULL)
1162 objects = _dbus_object_tree_new (connection);
1163 if (objects == NULL)
1166 if (_dbus_modify_sigpipe)
1167 _dbus_disable_sigpipe ();
1169 connection->refcount.value = 1;
1170 connection->transport = transport;
1171 connection->watches = watch_list;
1172 connection->timeouts = timeout_list;
1173 connection->pending_replies = pending_replies;
1174 connection->outgoing_counter = outgoing_counter;
1175 connection->filter_list = NULL;
1176 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1177 connection->objects = objects;
1178 connection->exit_on_disconnect = FALSE;
1179 connection->shareable = FALSE;
1180 connection->route_peer_messages = FALSE;
1182 #ifndef DBUS_DISABLE_CHECKS
1183 connection->generation = _dbus_current_generation;
1186 _dbus_data_slot_list_init (&connection->slot_list);
1188 connection->client_serial = 1;
1190 connection->disconnect_message_link = disconnect_link;
1192 CONNECTION_LOCK (connection);
1194 if (!_dbus_transport_set_connection (transport, connection))
1196 CONNECTION_UNLOCK (connection);
1201 _dbus_transport_ref (transport);
1203 CONNECTION_UNLOCK (connection);
1208 if (disconnect_message != NULL)
1209 dbus_message_unref (disconnect_message);
1211 if (disconnect_link != NULL)
1212 _dbus_list_free_link (disconnect_link);
1214 if (connection != NULL)
1216 _dbus_condvar_free_at_location (&connection->io_path_cond);
1217 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1218 _dbus_mutex_free_at_location (&connection->mutex);
1219 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1220 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1221 dbus_free (connection);
1223 if (pending_replies)
1224 _dbus_hash_table_unref (pending_replies);
1227 _dbus_watch_list_free (watch_list);
1230 _dbus_timeout_list_free (timeout_list);
1232 if (outgoing_counter)
1233 _dbus_counter_unref (outgoing_counter);
1236 _dbus_object_tree_unref (objects);
1242 * Increments the reference count of a DBusConnection.
1243 * Requires that the caller already holds the connection lock.
1245 * @param connection the connection.
1246 * @returns the connection.
1249 _dbus_connection_ref_unlocked (DBusConnection *connection)
1251 _dbus_assert (connection != NULL);
1252 _dbus_assert (connection->generation == _dbus_current_generation);
1254 HAVE_LOCK_CHECK (connection);
1256 #ifdef DBUS_HAVE_ATOMIC_INT
1257 _dbus_atomic_inc (&connection->refcount);
1259 _dbus_assert (connection->refcount.value > 0);
1260 connection->refcount.value += 1;
1267 * Decrements the reference count of a DBusConnection.
1268 * Requires that the caller already holds the connection lock.
1270 * @param connection the connection.
1273 _dbus_connection_unref_unlocked (DBusConnection *connection)
1275 dbus_bool_t last_unref;
1277 HAVE_LOCK_CHECK (connection);
1279 _dbus_assert (connection != NULL);
1281 /* The connection lock is better than the global
1282 * lock in the atomic increment fallback
1285 #ifdef DBUS_HAVE_ATOMIC_INT
1286 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1288 _dbus_assert (connection->refcount.value > 0);
1290 connection->refcount.value -= 1;
1291 last_unref = (connection->refcount.value == 0);
1293 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1298 _dbus_connection_last_unref (connection);
1301 static dbus_uint32_t
1302 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1306 serial = connection->client_serial++;
1308 if (connection->client_serial < 0)
1309 connection->client_serial = 1;
1315 * A callback for use with dbus_watch_new() to create a DBusWatch.
1317 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1318 * and the virtual handle_watch in DBusTransport if we got rid of it.
1319 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1322 * @param watch the watch.
1323 * @param condition the current condition of the file descriptors being watched.
1324 * @param data must be a pointer to a #DBusConnection
1325 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1328 _dbus_connection_handle_watch (DBusWatch *watch,
1329 unsigned int condition,
1332 DBusConnection *connection;
1334 DBusDispatchStatus status;
1338 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1340 CONNECTION_LOCK (connection);
1341 _dbus_connection_acquire_io_path (connection, -1);
1342 HAVE_LOCK_CHECK (connection);
1343 retval = _dbus_transport_handle_watch (connection->transport,
1346 _dbus_connection_release_io_path (connection);
1348 HAVE_LOCK_CHECK (connection);
1350 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1352 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1354 /* this calls out to user code */
1355 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1357 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1362 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1363 static DBusHashTable *shared_connections = NULL;
1366 shared_connections_shutdown (void *data)
1368 _DBUS_LOCK (shared_connections);
1370 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1372 _dbus_hash_table_unref (shared_connections);
1373 shared_connections = NULL;
1375 _DBUS_UNLOCK (shared_connections);
1379 connection_lookup_shared (DBusAddressEntry *entry,
1380 DBusConnection **result)
1382 _dbus_verbose ("checking for existing connection\n");
1386 _DBUS_LOCK (shared_connections);
1388 if (shared_connections == NULL)
1390 _dbus_verbose ("creating shared_connections hash table\n");
1392 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1395 if (shared_connections == NULL)
1397 _DBUS_UNLOCK (shared_connections);
1401 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1403 _dbus_hash_table_unref (shared_connections);
1404 shared_connections = NULL;
1405 _DBUS_UNLOCK (shared_connections);
1409 _dbus_verbose (" successfully created shared_connections\n");
1411 _DBUS_UNLOCK (shared_connections);
1412 return TRUE; /* no point looking up in the hash we just made */
1418 guid = dbus_address_entry_get_value (entry, "guid");
1422 *result = _dbus_hash_table_lookup_string (shared_connections,
1427 /* The DBusConnection can't have been disconnected
1428 * between the lookup and this code, because the
1429 * disconnection will take the shared_connections lock to
1430 * remove the connection. It can't have been finalized
1431 * since you have to disconnect prior to finalize.
1433 * Thus it's safe to ref the connection.
1435 dbus_connection_ref (*result);
1437 _dbus_verbose ("looked up existing connection to server guid %s\n",
1442 _DBUS_UNLOCK (shared_connections);
1448 connection_record_shared_unlocked (DBusConnection *connection,
1452 char *guid_in_connection;
1454 /* A separate copy of the key is required in the hash table, because
1455 * we don't have a lock on the connection when we are doing a hash
1459 _dbus_assert (connection->server_guid == NULL);
1460 _dbus_assert (connection->shareable);
1462 guid_key = _dbus_strdup (guid);
1463 if (guid_key == NULL)
1466 guid_in_connection = _dbus_strdup (guid);
1467 if (guid_in_connection == NULL)
1469 dbus_free (guid_key);
1473 _DBUS_LOCK (shared_connections);
1474 _dbus_assert (shared_connections != NULL);
1476 if (!_dbus_hash_table_insert_string (shared_connections,
1477 guid_key, connection))
1479 dbus_free (guid_key);
1480 dbus_free (guid_in_connection);
1481 _DBUS_UNLOCK (shared_connections);
1485 connection->server_guid = guid_in_connection;
1486 connection->shared = TRUE;
1488 /* get a hard ref on this connection */
1489 dbus_connection_ref (connection);
1491 _dbus_verbose ("stored connection to %s to be shared\n",
1492 connection->server_guid);
1494 _DBUS_UNLOCK (shared_connections);
1496 _dbus_assert (connection->server_guid != NULL);
1502 connection_forget_shared_unlocked (DBusConnection *connection)
1504 HAVE_LOCK_CHECK (connection);
1506 if (connection->server_guid == NULL)
1509 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1510 connection->server_guid);
1512 _DBUS_LOCK (shared_connections);
1514 if (!_dbus_hash_table_remove_string (shared_connections,
1515 connection->server_guid))
1516 _dbus_assert_not_reached ("connection was not in the shared table");
1518 dbus_free (connection->server_guid);
1519 connection->server_guid = NULL;
1521 /* remove the hash ref */
1522 _dbus_connection_unref_unlocked (connection);
1523 _DBUS_UNLOCK (shared_connections);
1526 static DBusConnection*
1527 connection_try_from_address_entry (DBusAddressEntry *entry,
1530 DBusTransport *transport;
1531 DBusConnection *connection;
1533 transport = _dbus_transport_open (entry, error);
1535 if (transport == NULL)
1537 _DBUS_ASSERT_ERROR_IS_SET (error);
1541 connection = _dbus_connection_new_for_transport (transport);
1543 _dbus_transport_unref (transport);
1545 if (connection == NULL)
1547 _DBUS_SET_OOM (error);
1551 #ifndef DBUS_DISABLE_CHECKS
1552 _dbus_assert (!connection->have_connection_lock);
1558 * If the shared parameter is true, then any existing connection will
1559 * be used (and if a new connection is created, it will be available
1560 * for use by others). If the shared parameter is false, a new
1561 * connection will always be created, and the new connection will
1562 * never be returned to other callers.
1564 * @param address the address
1565 * @param shared whether the connection is shared or private
1566 * @param error error return
1567 * @returns the connection or #NULL on error
1569 static DBusConnection*
1570 _dbus_connection_open_internal (const char *address,
1574 DBusConnection *connection;
1575 DBusAddressEntry **entries;
1576 DBusError tmp_error;
1577 DBusError first_error;
1580 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1582 _dbus_verbose ("opening %s connection to: %s\n",
1583 shared ? "shared" : "private", address);
1585 if (!dbus_parse_address (address, &entries, &len, error))
1588 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1592 dbus_error_init (&tmp_error);
1593 dbus_error_init (&first_error);
1594 for (i = 0; i < len; i++)
1598 if (!connection_lookup_shared (entries[i], &connection))
1599 _DBUS_SET_OOM (&tmp_error);
1602 if (connection == NULL)
1604 connection = connection_try_from_address_entry (entries[i],
1607 if (connection != NULL && shared)
1611 connection->shareable = TRUE;
1613 guid = dbus_address_entry_get_value (entries[i], "guid");
1615 /* we don't have a connection lock but we know nobody
1616 * else has a handle to the connection
1620 !connection_record_shared_unlocked (connection, guid))
1622 _DBUS_SET_OOM (&tmp_error);
1623 _dbus_connection_close_internal (connection);
1624 dbus_connection_unref (connection);
1628 /* but as of now the connection is possibly shared
1629 * since another thread could have pulled it from the table
1637 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1640 dbus_move_error (&tmp_error, &first_error);
1642 dbus_error_free (&tmp_error);
1645 /* NOTE we don't have a lock on a possibly-shared connection object */
1647 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1648 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1650 if (connection == NULL)
1652 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1653 dbus_move_error (&first_error, error);
1657 dbus_error_free (&first_error);
1660 dbus_address_entries_free (entries);
1667 * @addtogroup DBusConnection
1673 * Gets a connection to a remote address. If a connection to the given
1674 * address already exists, returns the existing connection with its
1675 * reference count incremented. Otherwise, returns a new connection
1676 * and saves the new connection for possible re-use if a future call
1677 * to dbus_connection_open() asks to connect to the same server.
1679 * Use dbus_connection_open_private() to get a dedicated connection
1680 * not shared with other callers of dbus_connection_open().
1682 * If the open fails, the function returns #NULL, and provides a
1683 * reason for the failure in the error parameter. Pass #NULL for the
1684 * error parameter if you aren't interested in the reason for
1687 * @param address the address.
1688 * @param error address where an error can be returned.
1689 * @returns new connection, or #NULL on failure.
1692 dbus_connection_open (const char *address,
1695 DBusConnection *connection;
1697 _dbus_return_val_if_fail (address != NULL, NULL);
1698 _dbus_return_val_if_error_is_set (error, NULL);
1700 connection = _dbus_connection_open_internal (address,
1708 * Opens a new, dedicated connection to a remote address. Unlike
1709 * dbus_connection_open(), always creates a new connection.
1710 * This connection will not be saved or recycled by libdbus.
1712 * If the open fails, the function returns #NULL, and provides a
1713 * reason for the failure in the error parameter. Pass #NULL for the
1714 * error parameter if you aren't interested in the reason for
1717 * @param address the address.
1718 * @param error address where an error can be returned.
1719 * @returns new connection, or #NULL on failure.
1722 dbus_connection_open_private (const char *address,
1725 DBusConnection *connection;
1727 _dbus_return_val_if_fail (address != NULL, NULL);
1728 _dbus_return_val_if_error_is_set (error, NULL);
1730 connection = _dbus_connection_open_internal (address,
1738 * Increments the reference count of a DBusConnection.
1740 * @param connection the connection.
1741 * @returns the connection.
1744 dbus_connection_ref (DBusConnection *connection)
1746 _dbus_return_val_if_fail (connection != NULL, NULL);
1747 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
1749 /* The connection lock is better than the global
1750 * lock in the atomic increment fallback
1753 #ifdef DBUS_HAVE_ATOMIC_INT
1754 _dbus_atomic_inc (&connection->refcount);
1756 CONNECTION_LOCK (connection);
1757 _dbus_assert (connection->refcount.value > 0);
1759 connection->refcount.value += 1;
1760 CONNECTION_UNLOCK (connection);
1767 free_outgoing_message (void *element,
1770 DBusMessage *message = element;
1771 DBusConnection *connection = data;
1773 _dbus_message_remove_size_counter (message,
1774 connection->outgoing_counter,
1776 dbus_message_unref (message);
1779 /* This is run without the mutex held, but after the last reference
1780 * to the connection has been dropped we should have no thread-related
1784 _dbus_connection_last_unref (DBusConnection *connection)
1788 _dbus_verbose ("Finalizing connection %p\n", connection);
1790 _dbus_assert (connection->refcount.value == 0);
1792 /* You have to disconnect the connection before unref:ing it. Otherwise
1793 * you won't get the disconnected message.
1795 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1796 _dbus_assert (connection->server_guid == NULL);
1798 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1799 _dbus_object_tree_free_all_unlocked (connection->objects);
1801 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1802 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1803 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1805 _dbus_watch_list_free (connection->watches);
1806 connection->watches = NULL;
1808 _dbus_timeout_list_free (connection->timeouts);
1809 connection->timeouts = NULL;
1811 _dbus_data_slot_list_free (&connection->slot_list);
1813 link = _dbus_list_get_first_link (&connection->filter_list);
1814 while (link != NULL)
1816 DBusMessageFilter *filter = link->data;
1817 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1819 filter->function = NULL;
1820 _dbus_message_filter_unref (filter); /* calls app callback */
1825 _dbus_list_clear (&connection->filter_list);
1827 /* ---- Done with stuff that invokes application callbacks */
1829 _dbus_object_tree_unref (connection->objects);
1831 _dbus_hash_table_unref (connection->pending_replies);
1832 connection->pending_replies = NULL;
1834 _dbus_list_clear (&connection->filter_list);
1836 _dbus_list_foreach (&connection->outgoing_messages,
1837 free_outgoing_message,
1839 _dbus_list_clear (&connection->outgoing_messages);
1841 _dbus_list_foreach (&connection->incoming_messages,
1842 (DBusForeachFunction) dbus_message_unref,
1844 _dbus_list_clear (&connection->incoming_messages);
1846 _dbus_counter_unref (connection->outgoing_counter);
1848 _dbus_transport_unref (connection->transport);
1850 if (connection->disconnect_message_link)
1852 DBusMessage *message = connection->disconnect_message_link->data;
1853 dbus_message_unref (message);
1854 _dbus_list_free_link (connection->disconnect_message_link);
1857 _dbus_list_clear (&connection->link_cache);
1859 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1860 _dbus_condvar_free_at_location (&connection->io_path_cond);
1862 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1863 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1865 _dbus_mutex_free_at_location (&connection->mutex);
1867 dbus_free (connection);
1871 * Decrements the reference count of a DBusConnection, and finalizes
1872 * it if the count reaches zero. It is a bug to drop the last reference
1873 * to a connection that has not been disconnected.
1875 * @todo in practice it can be quite tricky to never unref a connection
1876 * that's still connected; maybe there's some way we could avoid
1879 * @param connection the connection.
1882 dbus_connection_unref (DBusConnection *connection)
1884 dbus_bool_t last_unref;
1886 _dbus_return_if_fail (connection != NULL);
1887 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1889 /* The connection lock is better than the global
1890 * lock in the atomic increment fallback
1893 #ifdef DBUS_HAVE_ATOMIC_INT
1894 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1896 CONNECTION_LOCK (connection);
1898 _dbus_assert (connection->refcount.value > 0);
1900 connection->refcount.value -= 1;
1901 last_unref = (connection->refcount.value == 0);
1904 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1907 CONNECTION_UNLOCK (connection);
1911 _dbus_connection_last_unref (connection);
1915 _dbus_connection_close_internal_and_unlock (DBusConnection *connection)
1917 DBusDispatchStatus status;
1919 _dbus_verbose ("Disconnecting %p\n", connection);
1921 _dbus_transport_disconnect (connection->transport);
1923 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1924 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1926 /* this calls out to user code */
1927 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1931 _dbus_connection_close_internal (DBusConnection *connection)
1933 _dbus_assert (connection != NULL);
1934 _dbus_assert (connection->generation == _dbus_current_generation);
1936 CONNECTION_LOCK (connection);
1937 _dbus_connection_close_internal_and_unlock (connection);
1941 * Closes the connection, so no further data can be sent or received.
1942 * Any further attempts to send data will result in errors. This
1943 * function does not affect the connection's reference count. It's
1944 * safe to disconnect a connection more than once; all calls after the
1945 * first do nothing. It's impossible to "reopen" a connection, a
1946 * new connection must be created. This function may result in a call
1947 * to the DBusDispatchStatusFunction set with
1948 * dbus_connection_set_dispatch_status_function(), as the disconnect
1949 * message it generates needs to be dispatched.
1951 * If the connection is a shared connection we print out a warning that
1952 * you can not close shared connection and we return. Internal calls
1953 * should use _dbus_connection_close_internal() to close shared connections.
1955 * @param connection the connection.
1958 dbus_connection_close (DBusConnection *connection)
1960 _dbus_return_if_fail (connection != NULL);
1961 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1963 CONNECTION_LOCK (connection);
1965 if (connection->shared)
1967 CONNECTION_UNLOCK (connection);
1969 _dbus_warn ("Applications can not close shared connections. Please fix this in your app. Ignoring close request and continuing.");
1973 _dbus_connection_close_internal_and_unlock (connection);
1977 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1979 HAVE_LOCK_CHECK (connection);
1980 return _dbus_transport_get_is_connected (connection->transport);
1984 * Gets whether the connection is currently connected. All
1985 * connections are connected when they are opened. A connection may
1986 * become disconnected when the remote application closes its end, or
1987 * exits; a connection may also be disconnected with
1988 * dbus_connection_close().
1990 * @param connection the connection.
1991 * @returns #TRUE if the connection is still alive.
1994 dbus_connection_get_is_connected (DBusConnection *connection)
1998 _dbus_return_val_if_fail (connection != NULL, FALSE);
2000 CONNECTION_LOCK (connection);
2001 res = _dbus_connection_get_is_connected_unlocked (connection);
2002 CONNECTION_UNLOCK (connection);
2008 * Gets whether the connection was authenticated. (Note that
2009 * if the connection was authenticated then disconnected,
2010 * this function still returns #TRUE)
2012 * @param connection the connection
2013 * @returns #TRUE if the connection was ever authenticated
2016 dbus_connection_get_is_authenticated (DBusConnection *connection)
2020 _dbus_return_val_if_fail (connection != NULL, FALSE);
2022 CONNECTION_LOCK (connection);
2023 res = _dbus_transport_get_is_authenticated (connection->transport);
2024 CONNECTION_UNLOCK (connection);
2030 * Set whether _exit() should be called when the connection receives a
2031 * disconnect signal. The call to _exit() comes after any handlers for
2032 * the disconnect signal run; handlers can cancel the exit by calling
2035 * By default, exit_on_disconnect is #FALSE; but for message bus
2036 * connections returned from dbus_bus_get() it will be toggled on
2039 * @param connection the connection
2040 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
2043 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
2044 dbus_bool_t exit_on_disconnect)
2046 _dbus_return_if_fail (connection != NULL);
2048 CONNECTION_LOCK (connection);
2049 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
2050 CONNECTION_UNLOCK (connection);
2053 static DBusPreallocatedSend*
2054 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
2056 DBusPreallocatedSend *preallocated;
2058 HAVE_LOCK_CHECK (connection);
2060 _dbus_assert (connection != NULL);
2062 preallocated = dbus_new (DBusPreallocatedSend, 1);
2063 if (preallocated == NULL)
2066 if (connection->link_cache != NULL)
2068 preallocated->queue_link =
2069 _dbus_list_pop_first_link (&connection->link_cache);
2070 preallocated->queue_link->data = NULL;
2074 preallocated->queue_link = _dbus_list_alloc_link (NULL);
2075 if (preallocated->queue_link == NULL)
2079 if (connection->link_cache != NULL)
2081 preallocated->counter_link =
2082 _dbus_list_pop_first_link (&connection->link_cache);
2083 preallocated->counter_link->data = connection->outgoing_counter;
2087 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
2088 if (preallocated->counter_link == NULL)
2092 _dbus_counter_ref (preallocated->counter_link->data);
2094 preallocated->connection = connection;
2096 return preallocated;
2099 _dbus_list_free_link (preallocated->queue_link);
2101 dbus_free (preallocated);
2107 * Preallocates resources needed to send a message, allowing the message
2108 * to be sent without the possibility of memory allocation failure.
2109 * Allows apps to create a future guarantee that they can send
2110 * a message regardless of memory shortages.
2112 * @param connection the connection we're preallocating for.
2113 * @returns the preallocated resources, or #NULL
2115 DBusPreallocatedSend*
2116 dbus_connection_preallocate_send (DBusConnection *connection)
2118 DBusPreallocatedSend *preallocated;
2120 _dbus_return_val_if_fail (connection != NULL, NULL);
2122 CONNECTION_LOCK (connection);
2125 _dbus_connection_preallocate_send_unlocked (connection);
2127 CONNECTION_UNLOCK (connection);
2129 return preallocated;
2133 * Frees preallocated message-sending resources from
2134 * dbus_connection_preallocate_send(). Should only
2135 * be called if the preallocated resources are not used
2136 * to send a message.
2138 * @param connection the connection
2139 * @param preallocated the resources
2142 dbus_connection_free_preallocated_send (DBusConnection *connection,
2143 DBusPreallocatedSend *preallocated)
2145 _dbus_return_if_fail (connection != NULL);
2146 _dbus_return_if_fail (preallocated != NULL);
2147 _dbus_return_if_fail (connection == preallocated->connection);
2149 _dbus_list_free_link (preallocated->queue_link);
2150 _dbus_counter_unref (preallocated->counter_link->data);
2151 _dbus_list_free_link (preallocated->counter_link);
2152 dbus_free (preallocated);
2155 /* Called with lock held, does not update dispatch status */
2157 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
2158 DBusPreallocatedSend *preallocated,
2159 DBusMessage *message,
2160 dbus_uint32_t *client_serial)
2162 dbus_uint32_t serial;
2165 preallocated->queue_link->data = message;
2166 _dbus_list_prepend_link (&connection->outgoing_messages,
2167 preallocated->queue_link);
2169 _dbus_message_add_size_counter_link (message,
2170 preallocated->counter_link);
2172 dbus_free (preallocated);
2173 preallocated = NULL;
2175 dbus_message_ref (message);
2177 connection->n_outgoing += 1;
2179 sig = dbus_message_get_signature (message);
2181 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
2183 dbus_message_get_type (message),
2184 dbus_message_get_path (message) ?
2185 dbus_message_get_path (message) :
2187 dbus_message_get_interface (message) ?
2188 dbus_message_get_interface (message) :
2190 dbus_message_get_member (message) ?
2191 dbus_message_get_member (message) :
2194 dbus_message_get_destination (message) ?
2195 dbus_message_get_destination (message) :
2198 connection->n_outgoing);
2200 if (dbus_message_get_serial (message) == 0)
2202 serial = _dbus_connection_get_next_client_serial (connection);
2203 _dbus_message_set_serial (message, serial);
2205 *client_serial = serial;
2210 *client_serial = dbus_message_get_serial (message);
2213 _dbus_verbose ("Message %p serial is %u\n",
2214 message, dbus_message_get_serial (message));
2216 _dbus_message_lock (message);
2218 /* Now we need to run an iteration to hopefully just write the messages
2219 * out immediately, and otherwise get them queued up
2221 _dbus_connection_do_iteration_unlocked (connection,
2222 DBUS_ITERATION_DO_WRITING,
2225 /* If stuff is still queued up, be sure we wake up the main loop */
2226 if (connection->n_outgoing > 0)
2227 _dbus_connection_wakeup_mainloop (connection);
2231 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
2232 DBusPreallocatedSend *preallocated,
2233 DBusMessage *message,
2234 dbus_uint32_t *client_serial)
2236 DBusDispatchStatus status;
2238 HAVE_LOCK_CHECK (connection);
2240 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2242 message, client_serial);
2244 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2245 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2247 /* this calls out to user code */
2248 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2252 * Sends a message using preallocated resources. This function cannot fail.
2253 * It works identically to dbus_connection_send() in other respects.
2254 * Preallocated resources comes from dbus_connection_preallocate_send().
2255 * This function "consumes" the preallocated resources, they need not
2256 * be freed separately.
2258 * @param connection the connection
2259 * @param preallocated the preallocated resources
2260 * @param message the message to send
2261 * @param client_serial return location for client serial assigned to the message
2264 dbus_connection_send_preallocated (DBusConnection *connection,
2265 DBusPreallocatedSend *preallocated,
2266 DBusMessage *message,
2267 dbus_uint32_t *client_serial)
2269 _dbus_return_if_fail (connection != NULL);
2270 _dbus_return_if_fail (preallocated != NULL);
2271 _dbus_return_if_fail (message != NULL);
2272 _dbus_return_if_fail (preallocated->connection == connection);
2273 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
2274 dbus_message_get_member (message) != NULL);
2275 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
2276 (dbus_message_get_interface (message) != NULL &&
2277 dbus_message_get_member (message) != NULL));
2279 CONNECTION_LOCK (connection);
2280 _dbus_connection_send_preallocated_and_unlock (connection,
2282 message, client_serial);
2286 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
2287 DBusMessage *message,
2288 dbus_uint32_t *client_serial)
2290 DBusPreallocatedSend *preallocated;
2292 _dbus_assert (connection != NULL);
2293 _dbus_assert (message != NULL);
2295 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2296 if (preallocated == NULL)
2299 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2307 _dbus_connection_send_and_unlock (DBusConnection *connection,
2308 DBusMessage *message,
2309 dbus_uint32_t *client_serial)
2311 DBusPreallocatedSend *preallocated;
2313 _dbus_assert (connection != NULL);
2314 _dbus_assert (message != NULL);
2316 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2317 if (preallocated == NULL)
2319 CONNECTION_UNLOCK (connection);
2323 _dbus_connection_send_preallocated_and_unlock (connection,
2331 * Adds a message to the outgoing message queue. Does not block to
2332 * write the message to the network; that happens asynchronously. To
2333 * force the message to be written, call dbus_connection_flush().
2334 * Because this only queues the message, the only reason it can
2335 * fail is lack of memory. Even if the connection is disconnected,
2336 * no error will be returned.
2338 * If the function fails due to lack of memory, it returns #FALSE.
2339 * The function will never fail for other reasons; even if the
2340 * connection is disconnected, you can queue an outgoing message,
2341 * though obviously it won't be sent.
2343 * @param connection the connection.
2344 * @param message the message to write.
2345 * @param client_serial return location for client serial.
2346 * @returns #TRUE on success.
2349 dbus_connection_send (DBusConnection *connection,
2350 DBusMessage *message,
2351 dbus_uint32_t *client_serial)
2353 _dbus_return_val_if_fail (connection != NULL, FALSE);
2354 _dbus_return_val_if_fail (message != NULL, FALSE);
2356 CONNECTION_LOCK (connection);
2358 return _dbus_connection_send_and_unlock (connection,
2364 reply_handler_timeout (void *data)
2366 DBusConnection *connection;
2367 DBusDispatchStatus status;
2368 DBusPendingCall *pending = data;
2370 connection = _dbus_pending_call_get_connection_and_lock (pending);
2372 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2374 _dbus_connection_remove_timeout_unlocked (connection,
2375 _dbus_pending_call_get_timeout_unlocked (pending));
2376 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2378 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2379 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2381 /* Unlocks, and calls out to user code */
2382 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2388 * Queues a message to send, as with dbus_connection_send_message(),
2389 * but also returns a #DBusPendingCall used to receive a reply to the
2390 * message. If no reply is received in the given timeout_milliseconds,
2391 * this function expires the pending reply and generates a synthetic
2392 * error reply (generated in-process, not by the remote application)
2393 * indicating that a timeout occurred.
2395 * A #DBusPendingCall will see a reply message after any filters, but
2396 * before any object instances or other handlers. A #DBusPendingCall
2397 * will always see exactly one reply message, unless it's cancelled
2398 * with dbus_pending_call_cancel().
2400 * If a filter filters out the reply before the handler sees it, the
2401 * reply is immediately timed out and a timeout error reply is
2402 * generated. If a filter removes the timeout error reply then the
2403 * #DBusPendingCall will get confused. Filtering the timeout error
2404 * is thus considered a bug and will print a warning.
2406 * If #NULL is passed for the pending_return, the #DBusPendingCall
2407 * will still be generated internally, and used to track
2408 * the message reply timeout. This means a timeout error will
2409 * occur if no reply arrives, unlike with dbus_connection_send().
2411 * If -1 is passed for the timeout, a sane default timeout is used. -1
2412 * is typically the best value for the timeout for this reason, unless
2413 * you want a very short or very long timeout. There is no way to
2414 * avoid a timeout entirely, other than passing INT_MAX for the
2415 * timeout to postpone it indefinitely.
2417 * @param connection the connection
2418 * @param message the message to send
2419 * @param pending_return return location for a #DBusPendingCall object, or #NULLif connection is disconnected
2420 * @param timeout_milliseconds timeout in milliseconds or -1 for default
2421 * @returns #FALSE if no memory, #TRUE otherwise.
2425 dbus_connection_send_with_reply (DBusConnection *connection,
2426 DBusMessage *message,
2427 DBusPendingCall **pending_return,
2428 int timeout_milliseconds)
2430 DBusPendingCall *pending;
2431 dbus_int32_t serial = -1;
2432 DBusDispatchStatus status;
2434 _dbus_return_val_if_fail (connection != NULL, FALSE);
2435 _dbus_return_val_if_fail (message != NULL, FALSE);
2436 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2439 *pending_return = NULL;
2441 CONNECTION_LOCK (connection);
2443 if (!_dbus_connection_get_is_connected_unlocked (connection))
2445 CONNECTION_UNLOCK (connection);
2447 *pending_return = NULL;
2452 pending = _dbus_pending_call_new_unlocked (connection,
2453 timeout_milliseconds,
2454 reply_handler_timeout);
2456 if (pending == NULL)
2458 CONNECTION_UNLOCK (connection);
2462 /* Assign a serial to the message */
2463 serial = dbus_message_get_serial (message);
2466 serial = _dbus_connection_get_next_client_serial (connection);
2467 _dbus_message_set_serial (message, serial);
2470 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
2473 /* Insert the serial in the pending replies hash;
2474 * hash takes a refcount on DBusPendingCall.
2475 * Also, add the timeout.
2477 if (!_dbus_connection_attach_pending_call_unlocked (connection,
2481 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
2483 _dbus_connection_detach_pending_call_and_unlock (connection,
2485 goto error_unlocked;
2489 *pending_return = pending; /* hand off refcount */
2492 _dbus_connection_detach_pending_call_unlocked (connection, pending);
2493 /* we still have a ref to the pending call in this case, we unref
2494 * after unlocking, below
2498 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2500 /* this calls out to user code */
2501 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2503 if (pending_return == NULL)
2504 dbus_pending_call_unref (pending);
2509 CONNECTION_UNLOCK (connection);
2511 dbus_pending_call_unref (pending);
2515 /* This is slightly strange since we can pop a message here without
2516 * the dispatch lock.
2519 check_for_reply_unlocked (DBusConnection *connection,
2520 dbus_uint32_t client_serial)
2524 HAVE_LOCK_CHECK (connection);
2526 link = _dbus_list_get_first_link (&connection->incoming_messages);
2528 while (link != NULL)
2530 DBusMessage *reply = link->data;
2532 if (dbus_message_get_reply_serial (reply) == client_serial)
2534 _dbus_list_remove_link (&connection->incoming_messages, link);
2535 connection->n_incoming -= 1;
2538 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2545 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2547 /* We can't iterate over the hash in the normal way since we'll be
2548 * dropping the lock for each item. So we restart the
2549 * iter each time as we drain the hash table.
2552 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2554 DBusPendingCall *pending;
2557 _dbus_hash_iter_init (connection->pending_replies, &iter);
2558 _dbus_hash_iter_next (&iter);
2560 pending = (DBusPendingCall *) _dbus_hash_iter_get_value (&iter);
2561 _dbus_pending_call_ref_unlocked (pending);
2563 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2565 _dbus_connection_remove_timeout_unlocked (connection,
2566 _dbus_pending_call_get_timeout_unlocked (pending));
2567 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2568 _dbus_hash_iter_remove_entry (&iter);
2570 _dbus_pending_call_unref_and_unlock (pending);
2571 CONNECTION_LOCK (connection);
2573 HAVE_LOCK_CHECK (connection);
2577 complete_pending_call_and_unlock (DBusConnection *connection,
2578 DBusPendingCall *pending,
2579 DBusMessage *message)
2581 _dbus_pending_call_set_reply_unlocked (pending, message);
2582 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2583 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2585 /* Must be called unlocked since it invokes app callback */
2586 _dbus_pending_call_complete (pending);
2587 dbus_pending_call_unref (pending);
2591 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2592 DBusPendingCall *pending)
2595 DBusDispatchStatus status;
2597 reply = check_for_reply_unlocked (connection,
2598 _dbus_pending_call_get_reply_serial_unlocked (pending));
2601 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2603 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2605 complete_pending_call_and_unlock (connection, pending, reply);
2606 dbus_message_unref (reply);
2608 CONNECTION_LOCK (connection);
2609 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2610 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2611 dbus_pending_call_unref (pending);
2620 * When a function that blocks has been called with a timeout, and we
2621 * run out of memory, the time to wait for memory is based on the
2622 * timeout. If the caller was willing to block a long time we wait a
2623 * relatively long time for memory, if they were only willing to block
2624 * briefly then we retry for memory at a rapid rate.
2626 * @timeout_milliseconds the timeout requested for blocking
2629 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2631 if (timeout_milliseconds == -1)
2632 _dbus_sleep_milliseconds (1000);
2633 else if (timeout_milliseconds < 100)
2634 ; /* just busy loop */
2635 else if (timeout_milliseconds <= 1000)
2636 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2638 _dbus_sleep_milliseconds (1000);
2641 static DBusMessage *
2642 generate_local_error_message (dbus_uint32_t serial,
2646 DBusMessage *message;
2647 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
2651 if (!dbus_message_set_error_name (message, error_name))
2653 dbus_message_unref (message);
2658 dbus_message_set_no_reply (message, TRUE);
2660 if (!dbus_message_set_reply_serial (message,
2663 dbus_message_unref (message);
2668 if (error_msg != NULL)
2670 DBusMessageIter iter;
2672 dbus_message_iter_init_append (message, &iter);
2673 if (!dbus_message_iter_append_basic (&iter,
2677 dbus_message_unref (message);
2688 * Blocks until a pending call times out or gets a reply.
2690 * Does not re-enter the main loop or run filter/path-registered
2691 * callbacks. The reply to the message will not be seen by
2694 * Returns immediately if pending call already got a reply.
2696 * @todo could use performance improvements (it keeps scanning
2697 * the whole message queue for example)
2699 * @param pending the pending call we block for a reply on
2702 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2704 long start_tv_sec, start_tv_usec;
2705 long end_tv_sec, end_tv_usec;
2706 long tv_sec, tv_usec;
2707 DBusDispatchStatus status;
2708 DBusConnection *connection;
2709 dbus_uint32_t client_serial;
2710 int timeout_milliseconds;
2712 _dbus_assert (pending != NULL);
2714 if (dbus_pending_call_get_completed (pending))
2717 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2719 connection = _dbus_pending_call_get_connection_and_lock (pending);
2721 /* Flush message queue - note, can affect dispatch status */
2722 _dbus_connection_flush_unlocked (connection);
2724 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2726 /* note that timeout_milliseconds is limited to a smallish value
2727 * in _dbus_pending_call_new() so overflows aren't possible
2730 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending));
2732 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2733 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2734 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2735 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2736 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2738 _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",
2739 timeout_milliseconds,
2741 start_tv_sec, start_tv_usec,
2742 end_tv_sec, end_tv_usec);
2744 /* check to see if we already got the data off the socket */
2745 /* from another blocked pending call */
2746 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2749 /* Now we wait... */
2750 /* always block at least once as we know we don't have the reply yet */
2751 _dbus_connection_do_iteration_unlocked (connection,
2752 DBUS_ITERATION_DO_READING |
2753 DBUS_ITERATION_BLOCK,
2754 timeout_milliseconds);
2758 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2760 HAVE_LOCK_CHECK (connection);
2762 /* queue messages and get status */
2764 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2766 /* the get_completed() is in case a dispatch() while we were blocking
2767 * got the reply instead of us.
2769 if (_dbus_pending_call_get_completed_unlocked (pending))
2771 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2772 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2773 dbus_pending_call_unref (pending);
2777 if (status == DBUS_DISPATCH_DATA_REMAINS) {
2778 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2782 _dbus_get_current_time (&tv_sec, &tv_usec);
2784 if (!_dbus_connection_get_is_connected_unlocked (connection))
2786 DBusMessage *error_msg;
2788 error_msg = generate_local_error_message (client_serial,
2789 DBUS_ERROR_DISCONNECTED,
2790 "Connection was disconnected before a reply was received");
2792 /* on OOM error_msg is set to NULL */
2793 complete_pending_call_and_unlock (connection, pending, error_msg);
2794 dbus_pending_call_unref (pending);
2797 else if (tv_sec < start_tv_sec)
2798 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2799 else if (connection->disconnect_message_link == NULL)
2800 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2801 else if (tv_sec < end_tv_sec ||
2802 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2804 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2805 (end_tv_usec - tv_usec) / 1000;
2806 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2807 _dbus_assert (timeout_milliseconds >= 0);
2809 if (status == DBUS_DISPATCH_NEED_MEMORY)
2811 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2812 * we may already have a reply in the buffer and just can't process
2815 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2817 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2821 /* block again, we don't have the reply buffered yet. */
2822 _dbus_connection_do_iteration_unlocked (connection,
2823 DBUS_ITERATION_DO_READING |
2824 DBUS_ITERATION_BLOCK,
2825 timeout_milliseconds);
2828 goto recheck_status;
2831 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2832 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2834 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
2836 /* unlock and call user code */
2837 complete_pending_call_and_unlock (connection, pending, NULL);
2839 /* update user code on dispatch status */
2840 CONNECTION_LOCK (connection);
2841 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2842 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2843 dbus_pending_call_unref (pending);
2847 * Sends a message and blocks a certain time period while waiting for
2848 * a reply. This function does not reenter the main loop,
2849 * i.e. messages other than the reply are queued up but not
2850 * processed. This function is used to do non-reentrant "method
2853 * If a normal reply is received, it is returned, and removed from the
2854 * incoming message queue. If it is not received, #NULL is returned
2855 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
2856 * received, it is converted to a #DBusError and returned as an error,
2857 * then the reply message is deleted. If something else goes wrong,
2858 * result is set to whatever is appropriate, such as
2859 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
2861 * @param connection the connection
2862 * @param message the message to send
2863 * @param timeout_milliseconds timeout in milliseconds or -1 for default
2864 * @param error return location for error message
2865 * @returns the message that is the reply or #NULL with an error code if the
2869 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
2870 DBusMessage *message,
2871 int timeout_milliseconds,
2875 DBusPendingCall *pending;
2877 _dbus_return_val_if_fail (connection != NULL, NULL);
2878 _dbus_return_val_if_fail (message != NULL, NULL);
2879 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
2880 _dbus_return_val_if_error_is_set (error, NULL);
2882 if (!dbus_connection_send_with_reply (connection, message,
2883 &pending, timeout_milliseconds))
2885 _DBUS_SET_OOM (error);
2889 _dbus_assert (pending != NULL);
2891 dbus_pending_call_block (pending);
2893 reply = dbus_pending_call_steal_reply (pending);
2894 dbus_pending_call_unref (pending);
2896 /* call_complete_and_unlock() called from pending_call_block() should
2897 * always fill this in.
2899 _dbus_assert (reply != NULL);
2901 if (dbus_set_error_from_message (error, reply))
2903 dbus_message_unref (reply);
2911 * Blocks until the outgoing message queue is empty.
2912 * Assumes connection lock already held.
2914 * If you call this, you MUST call update_dispatch_status afterword...
2916 * @param connection the connection.
2919 _dbus_connection_flush_unlocked (DBusConnection *connection)
2921 /* We have to specify DBUS_ITERATION_DO_READING here because
2922 * otherwise we could have two apps deadlock if they are both doing
2923 * a flush(), and the kernel buffers fill up. This could change the
2926 DBusDispatchStatus status;
2928 HAVE_LOCK_CHECK (connection);
2930 while (connection->n_outgoing > 0 &&
2931 _dbus_connection_get_is_connected_unlocked (connection))
2933 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
2934 HAVE_LOCK_CHECK (connection);
2935 _dbus_connection_do_iteration_unlocked (connection,
2936 DBUS_ITERATION_DO_READING |
2937 DBUS_ITERATION_DO_WRITING |
2938 DBUS_ITERATION_BLOCK,
2942 HAVE_LOCK_CHECK (connection);
2943 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2944 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2946 HAVE_LOCK_CHECK (connection);
2951 * Blocks until the outgoing message queue is empty.
2953 * @param connection the connection.
2956 dbus_connection_flush (DBusConnection *connection)
2958 /* We have to specify DBUS_ITERATION_DO_READING here because
2959 * otherwise we could have two apps deadlock if they are both doing
2960 * a flush(), and the kernel buffers fill up. This could change the
2963 DBusDispatchStatus status;
2965 _dbus_return_if_fail (connection != NULL);
2967 CONNECTION_LOCK (connection);
2969 status = _dbus_connection_flush_unlocked (connection);
2971 HAVE_LOCK_CHECK (connection);
2972 /* Unlocks and calls out to user code */
2973 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2975 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
2979 * This function is intended for use with applications that don't want
2980 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
2981 * example usage would be:
2984 * while (dbus_connection_read_write_dispatch (connection, -1))
2985 * ; // empty loop body
2988 * In this usage you would normally have set up a filter function to look
2989 * at each message as it is dispatched. The loop terminates when the last
2990 * message from the connection (the disconnected signal) is processed.
2992 * If there are messages to dispatch and the dispatch flag is set, this
2993 * function will dbus_connection_dispatch() once, and return. If there are no
2994 * messages to dispatch, this function will block until it can read or write,
2995 * then read or write, then return.
2997 * The way to think of this function is that it either makes some sort
2998 * of progress, or it blocks.
3000 * The return value indicates whether the disconnect message has been
3001 * processed, NOT whether the connection is connected. This is
3002 * important because even after disconnecting, you want to process any
3003 * messages you received prior to the disconnect.
3005 * @param connection the connection
3006 * @param timeout_milliseconds max time to block or -1 for infinite
3007 * @param dispatch dispatch new messages or leave them on the incoming queue
3008 * @returns #TRUE if the disconnect message has not been processed
3011 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3012 int timeout_milliseconds,
3013 dbus_bool_t dispatch)
3015 DBusDispatchStatus dstatus;
3016 dbus_bool_t dispatched_disconnected;
3018 dstatus = dbus_connection_get_dispatch_status (connection);
3020 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3022 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
3023 dbus_connection_dispatch (connection);
3024 CONNECTION_LOCK (connection);
3026 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3028 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
3029 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3030 CONNECTION_LOCK (connection);
3034 CONNECTION_LOCK (connection);
3035 if (_dbus_connection_get_is_connected_unlocked (connection))
3037 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3038 _dbus_connection_do_iteration_unlocked (connection,
3039 DBUS_ITERATION_DO_READING |
3040 DBUS_ITERATION_DO_WRITING |
3041 DBUS_ITERATION_BLOCK,
3042 timeout_milliseconds);
3046 HAVE_LOCK_CHECK (connection);
3047 dispatched_disconnected = connection->n_incoming == 0 &&
3048 connection->disconnect_message_link == NULL;
3049 CONNECTION_UNLOCK (connection);
3050 return !dispatched_disconnected; /* TRUE if we have not processed disconnected */
3055 * This function is intended for use with applications that don't want
3056 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3057 * example usage would be:
3060 * while (dbus_connection_read_write_dispatch (connection, -1))
3061 * ; // empty loop body
3064 * In this usage you would normally have set up a filter function to look
3065 * at each message as it is dispatched. The loop terminates when the last
3066 * message from the connection (the disconnected signal) is processed.
3068 * If there are messages to dispatch, this function will
3069 * dbus_connection_dispatch() once, and return. If there are no
3070 * messages to dispatch, this function will block until it can read or
3071 * write, then read or write, then return.
3073 * The way to think of this function is that it either makes some sort
3074 * of progress, or it blocks.
3076 * The return value indicates whether the disconnect message has been
3077 * processed, NOT whether the connection is connected. This is
3078 * important because even after disconnecting, you want to process any
3079 * messages you received prior to the disconnect.
3081 * @param connection the connection
3082 * @param timeout_milliseconds max time to block or -1 for infinite
3083 * @returns #TRUE if the disconnect message has not been processed
3086 dbus_connection_read_write_dispatch (DBusConnection *connection,
3087 int timeout_milliseconds)
3089 _dbus_return_val_if_fail (connection != NULL, FALSE);
3090 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3091 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3095 * This function is intended for use with applications that don't want to
3096 * write a main loop and deal with #DBusWatch and #DBusTimeout.
3098 * If there are no messages to dispatch, this function will block until it can
3099 * read or write, then read or write, then return.
3101 * The return value indicates whether the disconnect message has been
3102 * processed, NOT whether the connection is connected. This is important
3103 * because even after disconnecting, you want to process any messages you
3104 * received prior to the disconnect.
3106 * @param connection the connection
3107 * @param timeout_milliseconds max time to block or -1 for infinite
3108 * @returns #TRUE if the disconnect message has not been processed
3111 dbus_connection_read_write (DBusConnection *connection,
3112 int timeout_milliseconds)
3114 _dbus_return_val_if_fail (connection != NULL, FALSE);
3115 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3116 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3120 * Returns the first-received message from the incoming message queue,
3121 * leaving it in the queue. If the queue is empty, returns #NULL.
3123 * The caller does not own a reference to the returned message, and
3124 * must either return it using dbus_connection_return_message() or
3125 * keep it after calling dbus_connection_steal_borrowed_message(). No
3126 * one can get at the message while its borrowed, so return it as
3127 * quickly as possible and don't keep a reference to it after
3128 * returning it. If you need to keep the message, make a copy of it.
3130 * dbus_connection_dispatch() will block if called while a borrowed
3131 * message is outstanding; only one piece of code can be playing with
3132 * the incoming queue at a time. This function will block if called
3133 * during a dbus_connection_dispatch().
3135 * @param connection the connection.
3136 * @returns next message in the incoming queue.
3139 dbus_connection_borrow_message (DBusConnection *connection)
3141 DBusDispatchStatus status;
3142 DBusMessage *message;
3144 _dbus_return_val_if_fail (connection != NULL, NULL);
3146 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3148 /* this is called for the side effect that it queues
3149 * up any messages from the transport
3151 status = dbus_connection_get_dispatch_status (connection);
3152 if (status != DBUS_DISPATCH_DATA_REMAINS)
3155 CONNECTION_LOCK (connection);
3157 _dbus_connection_acquire_dispatch (connection);
3159 /* While a message is outstanding, the dispatch lock is held */
3160 _dbus_assert (connection->message_borrowed == NULL);
3162 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3164 message = connection->message_borrowed;
3166 /* Note that we KEEP the dispatch lock until the message is returned */
3167 if (message == NULL)
3168 _dbus_connection_release_dispatch (connection);
3170 CONNECTION_UNLOCK (connection);
3176 * Used to return a message after peeking at it using
3177 * dbus_connection_borrow_message(). Only called if
3178 * message from dbus_connection_borrow_message() was non-#NULL.
3180 * @param connection the connection
3181 * @param message the message from dbus_connection_borrow_message()
3184 dbus_connection_return_message (DBusConnection *connection,
3185 DBusMessage *message)
3187 _dbus_return_if_fail (connection != NULL);
3188 _dbus_return_if_fail (message != NULL);
3189 _dbus_return_if_fail (message == connection->message_borrowed);
3190 _dbus_return_if_fail (connection->dispatch_acquired);
3192 CONNECTION_LOCK (connection);
3194 _dbus_assert (message == connection->message_borrowed);
3196 connection->message_borrowed = NULL;
3198 _dbus_connection_release_dispatch (connection);
3200 CONNECTION_UNLOCK (connection);
3204 * Used to keep a message after peeking at it using
3205 * dbus_connection_borrow_message(). Before using this function, see
3206 * the caveats/warnings in the documentation for
3207 * dbus_connection_pop_message().
3209 * @param connection the connection
3210 * @param message the message from dbus_connection_borrow_message()
3213 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3214 DBusMessage *message)
3216 DBusMessage *pop_message;
3218 _dbus_return_if_fail (connection != NULL);
3219 _dbus_return_if_fail (message != NULL);
3220 _dbus_return_if_fail (message == connection->message_borrowed);
3221 _dbus_return_if_fail (connection->dispatch_acquired);
3223 CONNECTION_LOCK (connection);
3225 _dbus_assert (message == connection->message_borrowed);
3227 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3228 _dbus_assert (message == pop_message);
3230 connection->n_incoming -= 1;
3232 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3233 message, connection->n_incoming);
3235 connection->message_borrowed = NULL;
3237 _dbus_connection_release_dispatch (connection);
3239 CONNECTION_UNLOCK (connection);
3242 /* See dbus_connection_pop_message, but requires the caller to own
3243 * the lock before calling. May drop the lock while running.
3246 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3248 HAVE_LOCK_CHECK (connection);
3250 _dbus_assert (connection->message_borrowed == NULL);
3252 if (connection->n_incoming > 0)
3256 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3257 connection->n_incoming -= 1;
3259 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3261 dbus_message_get_type (link->data),
3262 dbus_message_get_path (link->data) ?
3263 dbus_message_get_path (link->data) :
3265 dbus_message_get_interface (link->data) ?
3266 dbus_message_get_interface (link->data) :
3268 dbus_message_get_member (link->data) ?
3269 dbus_message_get_member (link->data) :
3271 dbus_message_get_signature (link->data),
3272 connection, connection->n_incoming);
3280 /* See dbus_connection_pop_message, but requires the caller to own
3281 * the lock before calling. May drop the lock while running.
3284 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3288 HAVE_LOCK_CHECK (connection);
3290 link = _dbus_connection_pop_message_link_unlocked (connection);
3294 DBusMessage *message;
3296 message = link->data;
3298 _dbus_list_free_link (link);
3307 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3308 DBusList *message_link)
3310 HAVE_LOCK_CHECK (connection);
3312 _dbus_assert (message_link != NULL);
3313 /* You can't borrow a message while a link is outstanding */
3314 _dbus_assert (connection->message_borrowed == NULL);
3315 /* We had to have the dispatch lock across the pop/putback */
3316 _dbus_assert (connection->dispatch_acquired);
3318 _dbus_list_prepend_link (&connection->incoming_messages,
3320 connection->n_incoming += 1;
3322 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3324 dbus_message_get_type (message_link->data),
3325 dbus_message_get_interface (message_link->data) ?
3326 dbus_message_get_interface (message_link->data) :
3328 dbus_message_get_member (message_link->data) ?
3329 dbus_message_get_member (message_link->data) :
3331 dbus_message_get_signature (message_link->data),
3332 connection, connection->n_incoming);
3336 * Returns the first-received message from the incoming message queue,
3337 * removing it from the queue. The caller owns a reference to the
3338 * returned message. If the queue is empty, returns #NULL.
3340 * This function bypasses any message handlers that are registered,
3341 * and so using it is usually wrong. Instead, let the main loop invoke
3342 * dbus_connection_dispatch(). Popping messages manually is only
3343 * useful in very simple programs that don't share a #DBusConnection
3344 * with any libraries or other modules.
3346 * There is a lock that covers all ways of accessing the incoming message
3347 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3348 * dbus_connection_borrow_message(), etc. will all block while one of the others
3349 * in the group is running.
3351 * @param connection the connection.
3352 * @returns next message in the incoming queue.
3355 dbus_connection_pop_message (DBusConnection *connection)
3357 DBusMessage *message;
3358 DBusDispatchStatus status;
3360 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3362 /* this is called for the side effect that it queues
3363 * up any messages from the transport
3365 status = dbus_connection_get_dispatch_status (connection);
3366 if (status != DBUS_DISPATCH_DATA_REMAINS)
3369 CONNECTION_LOCK (connection);
3370 _dbus_connection_acquire_dispatch (connection);
3371 HAVE_LOCK_CHECK (connection);
3373 message = _dbus_connection_pop_message_unlocked (connection);
3375 _dbus_verbose ("Returning popped message %p\n", message);
3377 _dbus_connection_release_dispatch (connection);
3378 CONNECTION_UNLOCK (connection);
3384 * Acquire the dispatcher. This is a separate lock so the main
3385 * connection lock can be dropped to call out to application dispatch
3388 * @param connection the connection.
3391 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3393 HAVE_LOCK_CHECK (connection);
3395 _dbus_connection_ref_unlocked (connection);
3396 CONNECTION_UNLOCK (connection);
3398 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3399 _dbus_mutex_lock (connection->dispatch_mutex);
3401 while (connection->dispatch_acquired)
3403 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3404 _dbus_condvar_wait (connection->dispatch_cond,
3405 connection->dispatch_mutex);
3408 _dbus_assert (!connection->dispatch_acquired);
3410 connection->dispatch_acquired = TRUE;
3412 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3413 _dbus_mutex_unlock (connection->dispatch_mutex);
3415 CONNECTION_LOCK (connection);
3416 _dbus_connection_unref_unlocked (connection);
3420 * Release the dispatcher when you're done with it. Only call
3421 * after you've acquired the dispatcher. Wakes up at most one
3422 * thread currently waiting to acquire the dispatcher.
3424 * @param connection the connection.
3427 _dbus_connection_release_dispatch (DBusConnection *connection)
3429 HAVE_LOCK_CHECK (connection);
3431 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3432 _dbus_mutex_lock (connection->dispatch_mutex);
3434 _dbus_assert (connection->dispatch_acquired);
3436 connection->dispatch_acquired = FALSE;
3437 _dbus_condvar_wake_one (connection->dispatch_cond);
3439 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3440 _dbus_mutex_unlock (connection->dispatch_mutex);
3444 _dbus_connection_failed_pop (DBusConnection *connection,
3445 DBusList *message_link)
3447 _dbus_list_prepend_link (&connection->incoming_messages,
3449 connection->n_incoming += 1;
3452 static DBusDispatchStatus
3453 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3455 HAVE_LOCK_CHECK (connection);
3457 if (connection->n_incoming > 0)
3458 return DBUS_DISPATCH_DATA_REMAINS;
3459 else if (!_dbus_transport_queue_messages (connection->transport))
3460 return DBUS_DISPATCH_NEED_MEMORY;
3463 DBusDispatchStatus status;
3464 dbus_bool_t is_connected;
3466 status = _dbus_transport_get_dispatch_status (connection->transport);
3467 is_connected = _dbus_transport_get_is_connected (connection->transport);
3469 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3470 DISPATCH_STATUS_NAME (status), is_connected);
3474 if (status == DBUS_DISPATCH_COMPLETE &&
3475 connection->disconnect_message_link)
3477 _dbus_verbose ("Sending disconnect message from %s\n",
3478 _DBUS_FUNCTION_NAME);
3480 connection_forget_shared_unlocked (connection);
3482 /* If we have pending calls queued timeouts on disconnect */
3483 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
3485 /* We haven't sent the disconnect message already,
3486 * and all real messages have been queued up.
3488 _dbus_connection_queue_synthesized_message_link (connection,
3489 connection->disconnect_message_link);
3490 connection->disconnect_message_link = NULL;
3492 status = DBUS_DISPATCH_DATA_REMAINS;
3495 /* Dump the outgoing queue, we aren't going to be able to
3496 * send it now, and we'd like accessors like
3497 * dbus_connection_get_outgoing_size() to be accurate.
3499 if (connection->n_outgoing > 0)
3503 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3504 connection->n_outgoing);
3506 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3508 _dbus_connection_message_sent (connection, link->data);
3513 if (status != DBUS_DISPATCH_COMPLETE)
3515 else if (connection->n_incoming > 0)
3516 return DBUS_DISPATCH_DATA_REMAINS;
3518 return DBUS_DISPATCH_COMPLETE;
3523 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
3524 DBusDispatchStatus new_status)
3526 dbus_bool_t changed;
3527 DBusDispatchStatusFunction function;
3530 HAVE_LOCK_CHECK (connection);
3532 _dbus_connection_ref_unlocked (connection);
3534 changed = new_status != connection->last_dispatch_status;
3536 connection->last_dispatch_status = new_status;
3538 function = connection->dispatch_status_function;
3539 data = connection->dispatch_status_data;
3541 /* We drop the lock */
3542 CONNECTION_UNLOCK (connection);
3544 if (changed && function)
3546 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
3547 connection, new_status,
3548 DISPATCH_STATUS_NAME (new_status));
3549 (* function) (connection, new_status, data);
3552 dbus_connection_unref (connection);
3556 * Gets the current state (what we would currently return
3557 * from dbus_connection_dispatch()) but doesn't actually
3558 * dispatch any messages.
3560 * @param connection the connection.
3561 * @returns current dispatch status
3564 dbus_connection_get_dispatch_status (DBusConnection *connection)
3566 DBusDispatchStatus status;
3568 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3570 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3572 CONNECTION_LOCK (connection);
3574 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3576 CONNECTION_UNLOCK (connection);
3582 * Filter funtion for handling the Peer standard interface.
3584 static DBusHandlerResult
3585 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
3586 DBusMessage *message)
3588 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
3590 /* This means we're letting the bus route this message */
3591 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3593 else if (dbus_message_is_method_call (message,
3594 DBUS_INTERFACE_PEER,
3600 ret = dbus_message_new_method_return (message);
3602 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3604 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
3606 dbus_message_unref (ret);
3609 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3611 return DBUS_HANDLER_RESULT_HANDLED;
3613 else if (dbus_message_is_method_call (message,
3614 DBUS_INTERFACE_PEER,
3621 ret = dbus_message_new_method_return (message);
3623 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3626 _dbus_string_init (&uuid);
3627 if (_dbus_get_local_machine_uuid_encoded (&uuid))
3629 const char *v_STRING = _dbus_string_get_const_data (&uuid);
3630 if (dbus_message_append_args (ret,
3631 DBUS_TYPE_STRING, &v_STRING,
3634 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
3637 _dbus_string_free (&uuid);
3639 dbus_message_unref (ret);
3642 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3644 return DBUS_HANDLER_RESULT_HANDLED;
3646 else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
3648 /* We need to bounce anything else with this interface, otherwise apps
3649 * could start extending the interface and when we added extensions
3650 * here to DBusConnection we'd break those apps.
3656 ret = dbus_message_new_error (message,
3657 DBUS_ERROR_UNKNOWN_METHOD,
3658 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
3660 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3662 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
3664 dbus_message_unref (ret);
3667 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3669 return DBUS_HANDLER_RESULT_HANDLED;
3673 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3678 * Processes all builtin filter functions
3680 * If the spec specifies a standard interface
3681 * they should be processed from this method
3683 static DBusHandlerResult
3684 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
3685 DBusMessage *message)
3687 /* We just run one filter for now but have the option to run more
3688 if the spec calls for it in the future */
3690 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
3694 * Processes data buffered while handling watches, queueing zero or
3695 * more incoming messages. Then pops the first-received message from
3696 * the current incoming message queue, runs any handlers for it, and
3697 * unrefs the message. Returns a status indicating whether messages/data
3698 * remain, more memory is needed, or all data has been processed.
3700 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
3701 * does not necessarily dispatch a message, as the data may
3702 * be part of authentication or the like.
3704 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
3706 * @todo FIXME what if we call out to application code to handle a
3707 * message, holding the dispatch lock, and the application code runs
3708 * the main loop and dispatches again? Probably deadlocks at the
3709 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
3710 * and then the GSource etc. could handle the situation? Right now
3711 * our GSource is NO_RECURSE
3713 * @param connection the connection
3714 * @returns dispatch status
3717 dbus_connection_dispatch (DBusConnection *connection)
3719 DBusMessage *message;
3720 DBusList *link, *filter_list_copy, *message_link;
3721 DBusHandlerResult result;
3722 DBusPendingCall *pending;
3723 dbus_int32_t reply_serial;
3724 DBusDispatchStatus status;
3726 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3728 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3730 CONNECTION_LOCK (connection);
3731 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3732 if (status != DBUS_DISPATCH_DATA_REMAINS)
3734 /* unlocks and calls out to user code */
3735 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3739 /* We need to ref the connection since the callback could potentially
3740 * drop the last ref to it
3742 _dbus_connection_ref_unlocked (connection);
3744 _dbus_connection_acquire_dispatch (connection);
3745 HAVE_LOCK_CHECK (connection);
3747 message_link = _dbus_connection_pop_message_link_unlocked (connection);
3748 if (message_link == NULL)
3750 /* another thread dispatched our stuff */
3752 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
3754 _dbus_connection_release_dispatch (connection);
3756 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3758 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3760 dbus_connection_unref (connection);
3765 message = message_link->data;
3767 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
3769 dbus_message_get_type (message),
3770 dbus_message_get_interface (message) ?
3771 dbus_message_get_interface (message) :
3773 dbus_message_get_member (message) ?
3774 dbus_message_get_member (message) :
3776 dbus_message_get_signature (message));
3778 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3780 /* Pending call handling must be first, because if you do
3781 * dbus_connection_send_with_reply_and_block() or
3782 * dbus_pending_call_block() then no handlers/filters will be run on
3783 * the reply. We want consistent semantics in the case where we
3784 * dbus_connection_dispatch() the reply.
3787 reply_serial = dbus_message_get_reply_serial (message);
3788 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
3792 _dbus_verbose ("Dispatching a pending reply\n");
3793 complete_pending_call_and_unlock (connection, pending, message);
3794 pending = NULL; /* it's probably unref'd */
3796 CONNECTION_LOCK (connection);
3797 _dbus_verbose ("pending call completed in dispatch\n");
3798 result = DBUS_HANDLER_RESULT_HANDLED;
3802 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
3803 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3806 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
3808 _dbus_connection_release_dispatch (connection);
3809 HAVE_LOCK_CHECK (connection);
3811 _dbus_connection_failed_pop (connection, message_link);
3813 /* unlocks and calls user code */
3814 _dbus_connection_update_dispatch_status_and_unlock (connection,
3815 DBUS_DISPATCH_NEED_MEMORY);
3818 dbus_pending_call_unref (pending);
3819 dbus_connection_unref (connection);
3821 return DBUS_DISPATCH_NEED_MEMORY;
3824 _dbus_list_foreach (&filter_list_copy,
3825 (DBusForeachFunction)_dbus_message_filter_ref,
3828 /* We're still protected from dispatch() reentrancy here
3829 * since we acquired the dispatcher
3831 CONNECTION_UNLOCK (connection);
3833 link = _dbus_list_get_first_link (&filter_list_copy);
3834 while (link != NULL)
3836 DBusMessageFilter *filter = link->data;
3837 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
3839 if (filter->function == NULL)
3841 _dbus_verbose (" filter was removed in a callback function\n");
3846 _dbus_verbose (" running filter on message %p\n", message);
3847 result = (* filter->function) (connection, message, filter->user_data);
3849 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3855 _dbus_list_foreach (&filter_list_copy,
3856 (DBusForeachFunction)_dbus_message_filter_unref,
3858 _dbus_list_clear (&filter_list_copy);
3860 CONNECTION_LOCK (connection);
3862 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
3864 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
3867 else if (result == DBUS_HANDLER_RESULT_HANDLED)
3869 _dbus_verbose ("filter handled message in dispatch\n");
3873 /* We're still protected from dispatch() reentrancy here
3874 * since we acquired the dispatcher
3876 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
3878 dbus_message_get_type (message),
3879 dbus_message_get_interface (message) ?
3880 dbus_message_get_interface (message) :
3882 dbus_message_get_member (message) ?
3883 dbus_message_get_member (message) :
3885 dbus_message_get_signature (message));
3887 HAVE_LOCK_CHECK (connection);
3888 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
3891 CONNECTION_LOCK (connection);
3893 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3895 _dbus_verbose ("object tree handled message in dispatch\n");
3899 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
3903 DBusPreallocatedSend *preallocated;
3905 _dbus_verbose (" sending error %s\n",
3906 DBUS_ERROR_UNKNOWN_METHOD);
3908 if (!_dbus_string_init (&str))
3910 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3911 _dbus_verbose ("no memory for error string in dispatch\n");
3915 if (!_dbus_string_append_printf (&str,
3916 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
3917 dbus_message_get_member (message),
3918 dbus_message_get_signature (message),
3919 dbus_message_get_interface (message)))
3921 _dbus_string_free (&str);
3922 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3923 _dbus_verbose ("no memory for error string in dispatch\n");
3927 reply = dbus_message_new_error (message,
3928 DBUS_ERROR_UNKNOWN_METHOD,
3929 _dbus_string_get_const_data (&str));
3930 _dbus_string_free (&str);
3934 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3935 _dbus_verbose ("no memory for error reply in dispatch\n");
3939 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3941 if (preallocated == NULL)
3943 dbus_message_unref (reply);
3944 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3945 _dbus_verbose ("no memory for error send in dispatch\n");
3949 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
3952 dbus_message_unref (reply);
3954 result = DBUS_HANDLER_RESULT_HANDLED;
3957 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
3958 dbus_message_get_type (message),
3959 dbus_message_get_interface (message) ?
3960 dbus_message_get_interface (message) :
3962 dbus_message_get_member (message) ?
3963 dbus_message_get_member (message) :
3965 dbus_message_get_signature (message),
3969 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
3971 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
3973 /* Put message back, and we'll start over.
3974 * Yes this means handlers must be idempotent if they
3975 * don't return HANDLED; c'est la vie.
3977 _dbus_connection_putback_message_link_unlocked (connection,
3982 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
3984 if (dbus_message_is_signal (message,
3985 DBUS_INTERFACE_LOCAL,
3988 _dbus_bus_check_connection_and_unref_unlocked (connection);
3990 if (connection->exit_on_disconnect)
3992 CONNECTION_UNLOCK (connection);
3994 _dbus_verbose ("Exiting on Disconnected signal\n");
3996 _dbus_assert_not_reached ("Call to exit() returned");
4000 _dbus_list_free_link (message_link);
4001 dbus_message_unref (message); /* don't want the message to count in max message limits
4002 * in computing dispatch status below
4006 _dbus_connection_release_dispatch (connection);
4007 HAVE_LOCK_CHECK (connection);
4009 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
4010 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4012 /* unlocks and calls user code */
4013 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4015 dbus_connection_unref (connection);
4021 * Sets the watch functions for the connection. These functions are
4022 * responsible for making the application's main loop aware of file
4023 * descriptors that need to be monitored for events, using select() or
4024 * poll(). When using Qt, typically the DBusAddWatchFunction would
4025 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
4026 * could call g_io_add_watch(), or could be used as part of a more
4027 * elaborate GSource. Note that when a watch is added, it may
4030 * The DBusWatchToggledFunction notifies the application that the
4031 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
4032 * to check this. A disabled watch should have no effect, and enabled
4033 * watch should be added to the main loop. This feature is used
4034 * instead of simply adding/removing the watch because
4035 * enabling/disabling can be done without memory allocation. The
4036 * toggled function may be NULL if a main loop re-queries
4037 * dbus_watch_get_enabled() every time anyway.
4039 * The DBusWatch can be queried for the file descriptor to watch using
4040 * dbus_watch_get_fd(), and for the events to watch for using
4041 * dbus_watch_get_flags(). The flags returned by
4042 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
4043 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
4044 * all watches implicitly include a watch for hangups, errors, and
4045 * other exceptional conditions.
4047 * Once a file descriptor becomes readable or writable, or an exception
4048 * occurs, dbus_watch_handle() should be called to
4049 * notify the connection of the file descriptor's condition.
4051 * dbus_watch_handle() cannot be called during the
4052 * DBusAddWatchFunction, as the connection will not be ready to handle
4055 * It is not allowed to reference a DBusWatch after it has been passed
4056 * to remove_function.
4058 * If #FALSE is returned due to lack of memory, the failure may be due
4059 * to a #FALSE return from the new add_function. If so, the
4060 * add_function may have been called successfully one or more times,
4061 * but the remove_function will also have been called to remove any
4062 * successful adds. i.e. if #FALSE is returned the net result
4063 * should be that dbus_connection_set_watch_functions() has no effect,
4064 * but the add_function and remove_function may have been called.
4066 * @todo We need to drop the lock when we call the
4067 * add/remove/toggled functions which can be a side effect
4068 * of setting the watch functions.
4070 * @param connection the connection.
4071 * @param add_function function to begin monitoring a new descriptor.
4072 * @param remove_function function to stop monitoring a descriptor.
4073 * @param toggled_function function to notify of enable/disable
4074 * @param data data to pass to add_function and remove_function.
4075 * @param free_data_function function to be called to free the data.
4076 * @returns #FALSE on failure (no memory)
4079 dbus_connection_set_watch_functions (DBusConnection *connection,
4080 DBusAddWatchFunction add_function,
4081 DBusRemoveWatchFunction remove_function,
4082 DBusWatchToggledFunction toggled_function,
4084 DBusFreeFunction free_data_function)
4087 DBusWatchList *watches;
4089 _dbus_return_val_if_fail (connection != NULL, FALSE);
4091 CONNECTION_LOCK (connection);
4093 #ifndef DBUS_DISABLE_CHECKS
4094 if (connection->watches == NULL)
4096 _dbus_warn ("Re-entrant call to %s is not allowed\n",
4097 _DBUS_FUNCTION_NAME);
4102 /* ref connection for slightly better reentrancy */
4103 _dbus_connection_ref_unlocked (connection);
4105 /* This can call back into user code, and we need to drop the
4106 * connection lock when it does. This is kind of a lame
4109 watches = connection->watches;
4110 connection->watches = NULL;
4111 CONNECTION_UNLOCK (connection);
4113 retval = _dbus_watch_list_set_functions (watches,
4114 add_function, remove_function,
4116 data, free_data_function);
4117 CONNECTION_LOCK (connection);
4118 connection->watches = watches;
4120 CONNECTION_UNLOCK (connection);
4121 /* drop our paranoid refcount */
4122 dbus_connection_unref (connection);
4128 * Sets the timeout functions for the connection. These functions are
4129 * responsible for making the application's main loop aware of timeouts.
4130 * When using Qt, typically the DBusAddTimeoutFunction would create a
4131 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
4134 * The DBusTimeoutToggledFunction notifies the application that the
4135 * timeout has been enabled or disabled. Call
4136 * dbus_timeout_get_enabled() to check this. A disabled timeout should
4137 * have no effect, and enabled timeout should be added to the main
4138 * loop. This feature is used instead of simply adding/removing the
4139 * timeout because enabling/disabling can be done without memory
4140 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
4141 * to enable and disable. The toggled function may be NULL if a main
4142 * loop re-queries dbus_timeout_get_enabled() every time anyway.
4143 * Whenever a timeout is toggled, its interval may change.
4145 * The DBusTimeout can be queried for the timer interval using
4146 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
4147 * repeatedly, each time the interval elapses, starting after it has
4148 * elapsed once. The timeout stops firing when it is removed with the
4149 * given remove_function. The timer interval may change whenever the
4150 * timeout is added, removed, or toggled.
4152 * @param connection the connection.
4153 * @param add_function function to add a timeout.
4154 * @param remove_function function to remove a timeout.
4155 * @param toggled_function function to notify of enable/disable
4156 * @param data data to pass to add_function and remove_function.
4157 * @param free_data_function function to be called to free the data.
4158 * @returns #FALSE on failure (no memory)
4161 dbus_connection_set_timeout_functions (DBusConnection *connection,
4162 DBusAddTimeoutFunction add_function,
4163 DBusRemoveTimeoutFunction remove_function,
4164 DBusTimeoutToggledFunction toggled_function,
4166 DBusFreeFunction free_data_function)
4169 DBusTimeoutList *timeouts;
4171 _dbus_return_val_if_fail (connection != NULL, FALSE);
4173 CONNECTION_LOCK (connection);
4175 #ifndef DBUS_DISABLE_CHECKS
4176 if (connection->timeouts == NULL)
4178 _dbus_warn ("Re-entrant call to %s is not allowed\n",
4179 _DBUS_FUNCTION_NAME);
4184 /* ref connection for slightly better reentrancy */
4185 _dbus_connection_ref_unlocked (connection);
4187 timeouts = connection->timeouts;
4188 connection->timeouts = NULL;
4189 CONNECTION_UNLOCK (connection);
4191 retval = _dbus_timeout_list_set_functions (timeouts,
4192 add_function, remove_function,
4194 data, free_data_function);
4195 CONNECTION_LOCK (connection);
4196 connection->timeouts = timeouts;
4198 CONNECTION_UNLOCK (connection);
4199 /* drop our paranoid refcount */
4200 dbus_connection_unref (connection);
4206 * Sets the mainloop wakeup function for the connection. This function is
4207 * responsible for waking up the main loop (if its sleeping) when some some
4208 * change has happened to the connection that the mainloop needs to reconsider
4209 * (e.g. a message has been queued for writing).
4210 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
4211 * When using GLib, it would call g_main_context_wakeup().
4214 * @param connection the connection.
4215 * @param wakeup_main_function function to wake up the mainloop
4216 * @param data data to pass wakeup_main_function
4217 * @param free_data_function function to be called to free the data.
4220 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
4221 DBusWakeupMainFunction wakeup_main_function,
4223 DBusFreeFunction free_data_function)
4226 DBusFreeFunction old_free_data;
4228 _dbus_return_if_fail (connection != NULL);
4230 CONNECTION_LOCK (connection);
4231 old_data = connection->wakeup_main_data;
4232 old_free_data = connection->free_wakeup_main_data;
4234 connection->wakeup_main_function = wakeup_main_function;
4235 connection->wakeup_main_data = data;
4236 connection->free_wakeup_main_data = free_data_function;
4238 CONNECTION_UNLOCK (connection);
4240 /* Callback outside the lock */
4242 (*old_free_data) (old_data);
4246 * Set a function to be invoked when the dispatch status changes.
4247 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
4248 * dbus_connection_dispatch() needs to be called to process incoming
4249 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
4250 * from inside the DBusDispatchStatusFunction. Indeed, almost
4251 * any reentrancy in this function is a bad idea. Instead,
4252 * the DBusDispatchStatusFunction should simply save an indication
4253 * that messages should be dispatched later, when the main loop
4256 * @param connection the connection
4257 * @param function function to call on dispatch status changes
4258 * @param data data for function
4259 * @param free_data_function free the function data
4262 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
4263 DBusDispatchStatusFunction function,
4265 DBusFreeFunction free_data_function)
4268 DBusFreeFunction old_free_data;
4270 _dbus_return_if_fail (connection != NULL);
4272 CONNECTION_LOCK (connection);
4273 old_data = connection->dispatch_status_data;
4274 old_free_data = connection->free_dispatch_status_data;
4276 connection->dispatch_status_function = function;
4277 connection->dispatch_status_data = data;
4278 connection->free_dispatch_status_data = free_data_function;
4280 CONNECTION_UNLOCK (connection);
4282 /* Callback outside the lock */
4284 (*old_free_data) (old_data);
4288 * Get the UNIX file descriptor of the connection, if any. This can
4289 * be used for SELinux access control checks with getpeercon() for
4290 * example. DO NOT read or write to the file descriptor, or try to
4291 * select() on it; use DBusWatch for main loop integration. Not all
4292 * connections will have a file descriptor. So for adding descriptors
4293 * to the main loop, use dbus_watch_get_fd() and so forth.
4295 * If the connection is socket-based, you can also use
4296 * dbus_connection_get_socket(), which will work on Windows too.
4297 * This function always fails on Windows.
4299 * Right now the returned descriptor is always a socket, but
4300 * that is not guaranteed.
4302 * @param connection the connection
4303 * @param fd return location for the file descriptor.
4304 * @returns #TRUE if fd is successfully obtained.
4307 dbus_connection_get_unix_fd (DBusConnection *connection,
4310 _dbus_return_val_if_fail (connection != NULL, FALSE);
4311 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4314 /* FIXME do this on a lower level */
4318 return dbus_connection_get_socket(connection, fd);
4322 * Gets the underlying Windows or UNIX socket file descriptor
4323 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
4324 * select() on it; use DBusWatch for main loop integration. Not all
4325 * connections will have a socket. So for adding descriptors
4326 * to the main loop, use dbus_watch_get_fd() and so forth.
4328 * If the connection is not socket-based, this function will return FALSE,
4329 * even if the connection does have a file descriptor of some kind.
4330 * i.e. this function always returns specifically a socket file descriptor.
4332 * @param connection the connection
4333 * @param fd return location for the file descriptor.
4334 * @returns #TRUE if fd is successfully obtained.
4337 dbus_connection_get_socket(DBusConnection *connection,
4342 _dbus_return_val_if_fail (connection != NULL, FALSE);
4343 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4345 CONNECTION_LOCK (connection);
4347 retval = _dbus_transport_get_socket_fd (connection->transport,
4350 CONNECTION_UNLOCK (connection);
4357 * Gets the UNIX user ID of the connection if any.
4358 * Returns #TRUE if the uid is filled in.
4359 * Always returns #FALSE on non-UNIX platforms.
4360 * Always returns #FALSE prior to authenticating the
4363 * @param connection the connection
4364 * @param uid return location for the user ID
4365 * @returns #TRUE if uid is filled in with a valid user ID
4368 dbus_connection_get_unix_user (DBusConnection *connection,
4373 _dbus_return_val_if_fail (connection != NULL, FALSE);
4374 _dbus_return_val_if_fail (uid != NULL, FALSE);
4377 /* FIXME this should be done at a lower level, but it's kind of hard,
4378 * just want to be sure we don't ship with this API returning
4379 * some weird internal fake uid for 1.0
4384 CONNECTION_LOCK (connection);
4386 if (!_dbus_transport_get_is_authenticated (connection->transport))
4389 result = _dbus_transport_get_unix_user (connection->transport,
4391 CONNECTION_UNLOCK (connection);
4397 * Gets the process ID of the connection if any.
4398 * Returns #TRUE if the uid is filled in.
4399 * Always returns #FALSE prior to authenticating the
4402 * @param connection the connection
4403 * @param pid return location for the process ID
4404 * @returns #TRUE if uid is filled in with a valid process ID
4407 dbus_connection_get_unix_process_id (DBusConnection *connection,
4412 _dbus_return_val_if_fail (connection != NULL, FALSE);
4413 _dbus_return_val_if_fail (pid != NULL, FALSE);
4416 /* FIXME this should be done at a lower level, but it's kind of hard,
4417 * just want to be sure we don't ship with this API returning
4418 * some weird internal fake uid for 1.0
4423 CONNECTION_LOCK (connection);
4425 if (!_dbus_transport_get_is_authenticated (connection->transport))
4428 result = _dbus_transport_get_unix_process_id (connection->transport,
4430 CONNECTION_UNLOCK (connection);
4436 * Sets a predicate function used to determine whether a given user ID
4437 * is allowed to connect. When an incoming connection has
4438 * authenticated with a particular user ID, this function is called;
4439 * if it returns #TRUE, the connection is allowed to proceed,
4440 * otherwise the connection is disconnected.
4442 * If the function is set to #NULL (as it is by default), then
4443 * only the same UID as the server process will be allowed to
4446 * On Windows, the function will be set and its free_data_function will
4447 * be invoked when the connection is freed or a new function is set.
4448 * However, the function will never be called, because there are
4449 * no UNIX user ids to pass to it.
4451 * @todo add a Windows API analogous to dbus_connection_set_unix_user_function()
4453 * @param connection the connection
4454 * @param function the predicate
4455 * @param data data to pass to the predicate
4456 * @param free_data_function function to free the data
4459 dbus_connection_set_unix_user_function (DBusConnection *connection,
4460 DBusAllowUnixUserFunction function,
4462 DBusFreeFunction free_data_function)
4464 void *old_data = NULL;
4465 DBusFreeFunction old_free_function = NULL;
4467 _dbus_return_if_fail (connection != NULL);
4469 CONNECTION_LOCK (connection);
4470 _dbus_transport_set_unix_user_function (connection->transport,
4471 function, data, free_data_function,
4472 &old_data, &old_free_function);
4473 CONNECTION_UNLOCK (connection);
4475 if (old_free_function != NULL)
4476 (* old_free_function) (old_data);
4481 * Normally #DBusConnection automatically handles all messages to the
4482 * org.freedesktop.DBus.Peer interface. However, the message bus wants
4483 * to be able to route methods on that interface through the bus and
4484 * to other applications. If routing peer messages is enabled, then
4485 * messages with the org.freedesktop.DBus.Peer interface that also
4486 * have a bus destination name set will not be automatically
4487 * handled by the #DBusConnection and instead will be dispatched
4488 * normally to the application.
4491 * If a normal application sets this flag, it can break things badly.
4492 * So don't set this unless you are the message bus.
4494 * @param connection the connection
4495 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
4498 dbus_connection_set_route_peer_messages (DBusConnection *connection,
4501 _dbus_return_if_fail (connection != NULL);
4503 CONNECTION_LOCK (connection);
4504 connection->route_peer_messages = TRUE;
4505 CONNECTION_UNLOCK (connection);
4509 * Adds a message filter. Filters are handlers that are run on all
4510 * incoming messages, prior to the objects registered with
4511 * dbus_connection_register_object_path(). Filters are run in the
4512 * order that they were added. The same handler can be added as a
4513 * filter more than once, in which case it will be run more than once.
4514 * Filters added during a filter callback won't be run on the message
4517 * @todo we don't run filters on messages while blocking without
4518 * entering the main loop, since filters are run as part of
4519 * dbus_connection_dispatch(). This is probably a feature, as filters
4520 * could create arbitrary reentrancy. But kind of sucks if you're
4521 * trying to filter METHOD_RETURN for some reason.
4523 * @param connection the connection
4524 * @param function function to handle messages
4525 * @param user_data user data to pass to the function
4526 * @param free_data_function function to use for freeing user data
4527 * @returns #TRUE on success, #FALSE if not enough memory.
4530 dbus_connection_add_filter (DBusConnection *connection,
4531 DBusHandleMessageFunction function,
4533 DBusFreeFunction free_data_function)
4535 DBusMessageFilter *filter;
4537 _dbus_return_val_if_fail (connection != NULL, FALSE);
4538 _dbus_return_val_if_fail (function != NULL, FALSE);
4540 filter = dbus_new0 (DBusMessageFilter, 1);
4544 filter->refcount.value = 1;
4546 CONNECTION_LOCK (connection);
4548 if (!_dbus_list_append (&connection->filter_list,
4551 _dbus_message_filter_unref (filter);
4552 CONNECTION_UNLOCK (connection);
4556 /* Fill in filter after all memory allocated,
4557 * so we don't run the free_user_data_function
4558 * if the add_filter() fails
4561 filter->function = function;
4562 filter->user_data = user_data;
4563 filter->free_user_data_function = free_data_function;
4565 CONNECTION_UNLOCK (connection);
4570 * Removes a previously-added message filter. It is a programming
4571 * error to call this function for a handler that has not been added
4572 * as a filter. If the given handler was added more than once, only
4573 * one instance of it will be removed (the most recently-added
4576 * @param connection the connection
4577 * @param function the handler to remove
4578 * @param user_data user data for the handler to remove
4582 dbus_connection_remove_filter (DBusConnection *connection,
4583 DBusHandleMessageFunction function,
4587 DBusMessageFilter *filter;
4589 _dbus_return_if_fail (connection != NULL);
4590 _dbus_return_if_fail (function != NULL);
4592 CONNECTION_LOCK (connection);
4596 link = _dbus_list_get_last_link (&connection->filter_list);
4597 while (link != NULL)
4599 filter = link->data;
4601 if (filter->function == function &&
4602 filter->user_data == user_data)
4604 _dbus_list_remove_link (&connection->filter_list, link);
4605 filter->function = NULL;
4610 link = _dbus_list_get_prev_link (&connection->filter_list, link);
4613 CONNECTION_UNLOCK (connection);
4615 #ifndef DBUS_DISABLE_CHECKS
4618 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
4619 function, user_data);
4624 /* Call application code */
4625 if (filter->free_user_data_function)
4626 (* filter->free_user_data_function) (filter->user_data);
4628 filter->free_user_data_function = NULL;
4629 filter->user_data = NULL;
4631 _dbus_message_filter_unref (filter);
4635 * Registers a handler for a given path in the object hierarchy.
4636 * The given vtable handles messages sent to exactly the given path.
4639 * @param connection the connection
4640 * @param path a '/' delimited string of path elements
4641 * @param vtable the virtual table
4642 * @param user_data data to pass to functions in the vtable
4643 * @returns #FALSE if not enough memory
4646 dbus_connection_register_object_path (DBusConnection *connection,
4648 const DBusObjectPathVTable *vtable,
4651 char **decomposed_path;
4654 _dbus_return_val_if_fail (connection != NULL, FALSE);
4655 _dbus_return_val_if_fail (path != NULL, FALSE);
4656 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4657 _dbus_return_val_if_fail (vtable != NULL, FALSE);
4659 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4662 CONNECTION_LOCK (connection);
4664 retval = _dbus_object_tree_register (connection->objects,
4666 (const char **) decomposed_path, vtable,
4669 CONNECTION_UNLOCK (connection);
4671 dbus_free_string_array (decomposed_path);
4677 * Registers a fallback handler for a given subsection of the object
4678 * hierarchy. The given vtable handles messages at or below the given
4679 * path. You can use this to establish a default message handling
4680 * policy for a whole "subdirectory."
4682 * @param connection the connection
4683 * @param path a '/' delimited string of path elements
4684 * @param vtable the virtual table
4685 * @param user_data data to pass to functions in the vtable
4686 * @returns #FALSE if not enough memory
4689 dbus_connection_register_fallback (DBusConnection *connection,
4691 const DBusObjectPathVTable *vtable,
4694 char **decomposed_path;
4697 _dbus_return_val_if_fail (connection != NULL, FALSE);
4698 _dbus_return_val_if_fail (path != NULL, FALSE);
4699 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4700 _dbus_return_val_if_fail (vtable != NULL, FALSE);
4702 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4705 CONNECTION_LOCK (connection);
4707 retval = _dbus_object_tree_register (connection->objects,
4709 (const char **) decomposed_path, vtable,
4712 CONNECTION_UNLOCK (connection);
4714 dbus_free_string_array (decomposed_path);
4720 * Unregisters the handler registered with exactly the given path.
4721 * It's a bug to call this function for a path that isn't registered.
4722 * Can unregister both fallback paths and object paths.
4724 * @param connection the connection
4725 * @param path a '/' delimited string of path elements
4726 * @returns #FALSE if not enough memory
4729 dbus_connection_unregister_object_path (DBusConnection *connection,
4732 char **decomposed_path;
4734 _dbus_return_val_if_fail (connection != NULL, FALSE);
4735 _dbus_return_val_if_fail (path != NULL, FALSE);
4736 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4738 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4741 CONNECTION_LOCK (connection);
4743 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
4745 dbus_free_string_array (decomposed_path);
4751 * Gets the user data passed to dbus_connection_register_object_path()
4752 * or dbus_connection_register_fallback(). If nothing was registered
4753 * at this path, the data is filled in with #NULL.
4755 * @param connection the connection
4756 * @param path the path you registered with
4757 * @param data_p location to store the user data, or #NULL
4758 * @returns #FALSE if not enough memory
4761 dbus_connection_get_object_path_data (DBusConnection *connection,
4765 char **decomposed_path;
4767 _dbus_return_val_if_fail (connection != NULL, FALSE);
4768 _dbus_return_val_if_fail (path != NULL, FALSE);
4769 _dbus_return_val_if_fail (data_p != NULL, FALSE);
4773 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4776 CONNECTION_LOCK (connection);
4778 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
4780 CONNECTION_UNLOCK (connection);
4782 dbus_free_string_array (decomposed_path);
4788 * Lists the registered fallback handlers and object path handlers at
4789 * the given parent_path. The returned array should be freed with
4790 * dbus_free_string_array().
4792 * @param connection the connection
4793 * @param parent_path the path to list the child handlers of
4794 * @param child_entries returns #NULL-terminated array of children
4795 * @returns #FALSE if no memory to allocate the child entries
4798 dbus_connection_list_registered (DBusConnection *connection,
4799 const char *parent_path,
4800 char ***child_entries)
4802 char **decomposed_path;
4804 _dbus_return_val_if_fail (connection != NULL, FALSE);
4805 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
4806 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
4807 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
4809 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
4812 CONNECTION_LOCK (connection);
4814 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
4815 (const char **) decomposed_path,
4817 dbus_free_string_array (decomposed_path);
4822 static DBusDataSlotAllocator slot_allocator;
4823 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
4826 * Allocates an integer ID to be used for storing application-specific
4827 * data on any DBusConnection. The allocated ID may then be used
4828 * with dbus_connection_set_data() and dbus_connection_get_data().
4829 * The passed-in slot must be initialized to -1, and is filled in
4830 * with the slot ID. If the passed-in slot is not -1, it's assumed
4831 * to be already allocated, and its refcount is incremented.
4833 * The allocated slot is global, i.e. all DBusConnection objects will
4834 * have a slot with the given integer ID reserved.
4836 * @param slot_p address of a global variable storing the slot
4837 * @returns #FALSE on failure (no memory)
4840 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
4842 return _dbus_data_slot_allocator_alloc (&slot_allocator,
4843 &_DBUS_LOCK_NAME (connection_slots),
4848 * Deallocates a global ID for connection data slots.
4849 * dbus_connection_get_data() and dbus_connection_set_data() may no
4850 * longer be used with this slot. Existing data stored on existing
4851 * DBusConnection objects will be freed when the connection is
4852 * finalized, but may not be retrieved (and may only be replaced if
4853 * someone else reallocates the slot). When the refcount on the
4854 * passed-in slot reaches 0, it is set to -1.
4856 * @param slot_p address storing the slot to deallocate
4859 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
4861 _dbus_return_if_fail (*slot_p >= 0);
4863 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
4867 * Stores a pointer on a DBusConnection, along
4868 * with an optional function to be used for freeing
4869 * the data when the data is set again, or when
4870 * the connection is finalized. The slot number
4871 * must have been allocated with dbus_connection_allocate_data_slot().
4873 * @param connection the connection
4874 * @param slot the slot number
4875 * @param data the data to store
4876 * @param free_data_func finalizer function for the data
4877 * @returns #TRUE if there was enough memory to store the data
4880 dbus_connection_set_data (DBusConnection *connection,
4883 DBusFreeFunction free_data_func)
4885 DBusFreeFunction old_free_func;
4889 _dbus_return_val_if_fail (connection != NULL, FALSE);
4890 _dbus_return_val_if_fail (slot >= 0, FALSE);
4892 CONNECTION_LOCK (connection);
4894 retval = _dbus_data_slot_list_set (&slot_allocator,
4895 &connection->slot_list,
4896 slot, data, free_data_func,
4897 &old_free_func, &old_data);
4899 CONNECTION_UNLOCK (connection);
4903 /* Do the actual free outside the connection lock */
4905 (* old_free_func) (old_data);
4912 * Retrieves data previously set with dbus_connection_set_data().
4913 * The slot must still be allocated (must not have been freed).
4915 * @param connection the connection
4916 * @param slot the slot to get data from
4917 * @returns the data, or #NULL if not found
4920 dbus_connection_get_data (DBusConnection *connection,
4925 _dbus_return_val_if_fail (connection != NULL, NULL);
4927 CONNECTION_LOCK (connection);
4929 res = _dbus_data_slot_list_get (&slot_allocator,
4930 &connection->slot_list,
4933 CONNECTION_UNLOCK (connection);
4939 * This function sets a global flag for whether dbus_connection_new()
4940 * will set SIGPIPE behavior to SIG_IGN.
4942 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
4945 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
4947 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
4951 * Specifies the maximum size message this connection is allowed to
4952 * receive. Larger messages will result in disconnecting the
4955 * @param connection a #DBusConnection
4956 * @param size maximum message size the connection can receive, in bytes
4959 dbus_connection_set_max_message_size (DBusConnection *connection,
4962 _dbus_return_if_fail (connection != NULL);
4964 CONNECTION_LOCK (connection);
4965 _dbus_transport_set_max_message_size (connection->transport,
4967 CONNECTION_UNLOCK (connection);
4971 * Gets the value set by dbus_connection_set_max_message_size().
4973 * @param connection the connection
4974 * @returns the max size of a single message
4977 dbus_connection_get_max_message_size (DBusConnection *connection)
4981 _dbus_return_val_if_fail (connection != NULL, 0);
4983 CONNECTION_LOCK (connection);
4984 res = _dbus_transport_get_max_message_size (connection->transport);
4985 CONNECTION_UNLOCK (connection);
4990 * Sets the maximum total number of bytes that can be used for all messages
4991 * received on this connection. Messages count toward the maximum until
4992 * they are finalized. When the maximum is reached, the connection will
4993 * not read more data until some messages are finalized.
4995 * The semantics of the maximum are: if outstanding messages are
4996 * already above the maximum, additional messages will not be read.
4997 * The semantics are not: if the next message would cause us to exceed
4998 * the maximum, we don't read it. The reason is that we don't know the
4999 * size of a message until after we read it.
5001 * Thus, the max live messages size can actually be exceeded
5002 * by up to the maximum size of a single message.
5004 * Also, if we read say 1024 bytes off the wire in a single read(),
5005 * and that contains a half-dozen small messages, we may exceed the
5006 * size max by that amount. But this should be inconsequential.
5008 * This does imply that we can't call read() with a buffer larger
5009 * than we're willing to exceed this limit by.
5011 * @param connection the connection
5012 * @param size the maximum size in bytes of all outstanding messages
5015 dbus_connection_set_max_received_size (DBusConnection *connection,
5018 _dbus_return_if_fail (connection != NULL);
5020 CONNECTION_LOCK (connection);
5021 _dbus_transport_set_max_received_size (connection->transport,
5023 CONNECTION_UNLOCK (connection);
5027 * Gets the value set by dbus_connection_set_max_received_size().
5029 * @param connection the connection
5030 * @returns the max size of all live messages
5033 dbus_connection_get_max_received_size (DBusConnection *connection)
5037 _dbus_return_val_if_fail (connection != NULL, 0);
5039 CONNECTION_LOCK (connection);
5040 res = _dbus_transport_get_max_received_size (connection->transport);
5041 CONNECTION_UNLOCK (connection);
5046 * Gets the approximate size in bytes of all messages in the outgoing
5047 * message queue. The size is approximate in that you shouldn't use
5048 * it to decide how many bytes to read off the network or anything
5049 * of that nature, as optimizations may choose to tell small white lies
5050 * to avoid performance overhead.
5052 * @param connection the connection
5053 * @returns the number of bytes that have been queued up but not sent
5056 dbus_connection_get_outgoing_size (DBusConnection *connection)
5060 _dbus_return_val_if_fail (connection != NULL, 0);
5062 CONNECTION_LOCK (connection);
5063 res = _dbus_counter_get_value (connection->outgoing_counter);
5064 CONNECTION_UNLOCK (connection);
5069 * Obtains the machine UUID of the machine this process is running on.
5071 * The returned string must be freed with dbus_free().
5073 * This UUID is guaranteed to remain the same until the next reboot
5074 * (unless the sysadmin foolishly changes it and screws themselves).
5075 * It will usually remain the same across reboots also, but hardware
5076 * configuration changes or rebuilding the machine could break that.
5078 * The idea is that two processes with the same machine ID should be
5079 * able to use shared memory, UNIX domain sockets, process IDs, and other
5080 * features of the OS that require both processes to be running
5081 * on the same OS kernel instance.
5083 * The machine ID can also be used to create unique per-machine
5084 * instances. For example, you could use it in bus names or
5085 * X selection names.
5087 * The machine ID is preferred over the machine hostname, because
5088 * the hostname is frequently set to "localhost.localdomain" and
5089 * may also change at runtime.
5091 * You can get the machine ID of a remote application by invoking the
5092 * method GetMachineId from interface org.freedesktop.DBus.Peer.
5094 * If the remote application has the same machine ID as the one
5095 * returned by this function, then the remote application is on the
5096 * same machine as your application.
5098 * @returns a 32-byte-long hex-encoded UUID string, or #NULL if insufficient memory
5101 dbus_get_local_machine_id (void)
5107 _dbus_string_init (&uuid);
5108 if (!_dbus_get_local_machine_uuid_encoded (&uuid) ||
5109 !_dbus_string_steal_data (&uuid, &s))
5111 _dbus_string_free (&uuid);
5116 _dbus_string_free (&uuid);