1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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-message-private.h"
37 #include "dbus-threads.h"
38 #include "dbus-protocol.h"
39 #include "dbus-dataslot.h"
40 #include "dbus-string.h"
41 #include "dbus-signature.h"
42 #include "dbus-pending-call.h"
43 #include "dbus-object-tree.h"
44 #include "dbus-threads-internal.h"
46 #include "dbus-marshal-basic.h"
48 #ifdef DBUS_DISABLE_CHECKS
49 #define TOOK_LOCK_CHECK(connection)
50 #define RELEASING_LOCK_CHECK(connection)
51 #define HAVE_LOCK_CHECK(connection)
53 #define TOOK_LOCK_CHECK(connection) do { \
54 _dbus_assert (!(connection)->have_connection_lock); \
55 (connection)->have_connection_lock = TRUE; \
57 #define RELEASING_LOCK_CHECK(connection) do { \
58 _dbus_assert ((connection)->have_connection_lock); \
59 (connection)->have_connection_lock = FALSE; \
61 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
62 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
67 #define CONNECTION_LOCK(connection) do { \
68 if (TRACE_LOCKS) { _dbus_verbose ("LOCK\n"); } \
69 _dbus_rmutex_lock ((connection)->mutex); \
70 TOOK_LOCK_CHECK (connection); \
73 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock (connection)
75 #define SLOTS_LOCK(connection) do { \
76 _dbus_rmutex_lock ((connection)->slot_mutex); \
79 #define SLOTS_UNLOCK(connection) do { \
80 _dbus_rmutex_unlock ((connection)->slot_mutex); \
83 #define DISPATCH_STATUS_NAME(s) \
84 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
85 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
86 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
90 * @defgroup DBusConnection DBusConnection
92 * @brief Connection to another application
94 * A DBusConnection represents a connection to another
95 * application. Messages can be sent and received via this connection.
96 * The other application may be a message bus; for convenience, the
97 * function dbus_bus_get() is provided to automatically open a
98 * connection to the well-known message buses.
100 * In brief a DBusConnection is a message queue associated with some
101 * message transport mechanism such as a socket. The connection
102 * maintains a queue of incoming messages and a queue of outgoing
105 * Several functions use the following terms:
107 * <li><b>read</b> means to fill the incoming message queue by reading from the socket</li>
108 * <li><b>write</b> means to drain the outgoing queue by writing to the socket</li>
109 * <li><b>dispatch</b> means to drain the incoming queue by invoking application-provided message handlers</li>
112 * The function dbus_connection_read_write_dispatch() for example does all
113 * three of these things, offering a simple alternative to a main loop.
115 * In an application with a main loop, the read/write/dispatch
116 * operations are usually separate.
118 * The connection provides #DBusWatch and #DBusTimeout objects to
119 * the main loop. These are used to know when reading, writing, or
120 * dispatching should be performed.
122 * Incoming messages are processed
123 * by calling dbus_connection_dispatch(). dbus_connection_dispatch()
124 * runs any handlers registered for the topmost message in the message
125 * queue, then discards the message, then returns.
127 * dbus_connection_get_dispatch_status() indicates whether
128 * messages are currently in the queue that need dispatching.
129 * dbus_connection_set_dispatch_status_function() allows
130 * you to set a function to be used to monitor the dispatch status.
132 * If you're using GLib or Qt add-on libraries for D-Bus, there are
133 * special convenience APIs in those libraries that hide
134 * all the details of dispatch and watch/timeout monitoring.
135 * For example, dbus_connection_setup_with_g_main().
137 * If you aren't using these add-on libraries, but want to process
138 * messages asynchronously, you must manually call
139 * dbus_connection_set_dispatch_status_function(),
140 * dbus_connection_set_watch_functions(),
141 * dbus_connection_set_timeout_functions() providing appropriate
142 * functions to integrate the connection with your application's main
143 * loop. This can be tricky to get right; main loops are not simple.
145 * If you don't need to be asynchronous, you can ignore #DBusWatch,
146 * #DBusTimeout, and dbus_connection_dispatch(). Instead,
147 * dbus_connection_read_write_dispatch() can be used.
149 * Or, in <em>very</em> simple applications,
150 * dbus_connection_pop_message() may be all you need, allowing you to
151 * avoid setting up any handler functions (see
152 * dbus_connection_add_filter(),
153 * dbus_connection_register_object_path() for more on handlers).
155 * When you use dbus_connection_send() or one of its variants to send
156 * a message, the message is added to the outgoing queue. It's
157 * actually written to the network later; either in
158 * dbus_watch_handle() invoked by your main loop, or in
159 * dbus_connection_flush() which blocks until it can write out the
160 * entire outgoing queue. The GLib/Qt add-on libraries again
161 * handle the details here for you by setting up watch functions.
163 * When a connection is disconnected, you are guaranteed to get a
164 * signal "Disconnected" from the interface
165 * #DBUS_INTERFACE_LOCAL, path
168 * You may not drop the last reference to a #DBusConnection
169 * until that connection has been disconnected.
171 * You may dispatch the unprocessed incoming message queue even if the
172 * connection is disconnected. However, "Disconnected" will always be
173 * the last message in the queue (obviously no messages are received
174 * after disconnection).
176 * After calling dbus_threads_init(), #DBusConnection has thread
177 * locks and drops them when invoking user callbacks, so in general is
178 * transparently threadsafe. However, #DBusMessage does NOT have
179 * thread locks; you must not send the same message to multiple
180 * #DBusConnection if those connections will be used from different threads,
183 * Also, if you dispatch or pop messages from multiple threads, it
184 * may work in the sense that it won't crash, but it's tough to imagine
185 * sane results; it will be completely unpredictable which messages
186 * go to which threads.
188 * It's recommended to dispatch from a single thread.
190 * The most useful function to call from multiple threads at once
191 * is dbus_connection_send_with_reply_and_block(). That is,
192 * multiple threads can make method calls at the same time.
194 * If you aren't using threads, you can use a main loop and
195 * dbus_pending_call_set_notify() to achieve a similar result.
199 * @defgroup DBusConnectionInternals DBusConnection implementation details
200 * @ingroup DBusInternals
201 * @brief Implementation details of DBusConnection
207 _dbus_connection_trace_ref (DBusConnection *connection,
212 #ifdef DBUS_ENABLE_VERBOSE_MODE
213 static int enabled = -1;
215 _dbus_trace_ref ("DBusConnection", connection, old_refcount, new_refcount,
216 why, "DBUS_CONNECTION_TRACE", &enabled);
221 * Internal struct representing a message filter function
223 typedef struct DBusMessageFilter DBusMessageFilter;
226 * Internal struct representing a message filter function
228 struct DBusMessageFilter
230 DBusAtomic refcount; /**< Reference count */
231 DBusHandleMessageFunction function; /**< Function to call to filter */
232 void *user_data; /**< User data for the function */
233 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
238 * Internals of DBusPreallocatedSend
240 struct DBusPreallocatedSend
242 DBusConnection *connection; /**< Connection we'd send the message to */
243 DBusList *queue_link; /**< Preallocated link in the queue */
244 DBusList *counter_link; /**< Preallocated link in the resource counter */
247 #if HAVE_DECL_MSG_NOSIGNAL
248 static dbus_bool_t _dbus_modify_sigpipe = FALSE;
250 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
254 * Implementation details of DBusConnection. All fields are private.
256 struct DBusConnection
258 DBusAtomic refcount; /**< Reference count. */
260 DBusRMutex *mutex; /**< Lock on the entire DBusConnection */
262 DBusCMutex *dispatch_mutex; /**< Protects dispatch_acquired */
263 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */
264 DBusCMutex *io_path_mutex; /**< Protects io_path_acquired */
265 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */
267 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
268 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
269 DBusList *expired_messages; /**< Messages that will be released when we next unlock. */
271 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
272 * dispatch_acquired will be set by the borrower
275 int n_outgoing; /**< Length of outgoing queue. */
276 int n_incoming; /**< Length of incoming queue. */
278 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
280 DBusTransport *transport; /**< Object that sends/receives messages over network. */
281 DBusWatchList *watches; /**< Stores active watches. */
282 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
284 DBusList *filter_list; /**< List of filters. */
286 DBusRMutex *slot_mutex; /**< Lock on slot_list so overall connection lock need not be taken */
287 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
289 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
291 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
292 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
294 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
295 void *wakeup_main_data; /**< Application data for wakeup_main_function */
296 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
298 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
299 void *dispatch_status_data; /**< Application data for dispatch_status_function */
300 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
302 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
304 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
306 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
308 /* These two MUST be bools and not bitfields, because they are protected by a separate lock
309 * from connection->mutex and all bitfields in a word have to be read/written together.
310 * So you can't have a different lock for different bitfields in the same word.
312 dbus_bool_t dispatch_acquired; /**< Someone has dispatch path (can drain incoming queue) */
313 dbus_bool_t io_path_acquired; /**< Someone has transport io path (can use the transport to read/write messages) */
315 unsigned int dispatch_disabled : 1; /**< if true, then dispatching incoming messages is stopped until enabled again */
316 unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */
318 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
320 unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */
322 unsigned int disconnected_message_arrived : 1; /**< We popped or are dispatching the disconnected message.
323 * if the disconnect_message_link is NULL then we queued it, but
324 * this flag is whether it got to the head of the queue.
326 unsigned int disconnected_message_processed : 1; /**< We did our default handling of the disconnected message,
327 * such as closing the connection.
330 #ifndef DBUS_DISABLE_CHECKS
331 unsigned int have_connection_lock : 1; /**< Used to check locking */
334 #if defined(DBUS_ENABLE_CHECKS) || defined(DBUS_ENABLE_ASSERT)
335 int generation; /**< _dbus_current_generation that should correspond to this connection */
339 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
340 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
341 DBusDispatchStatus new_status);
342 static void _dbus_connection_last_unref (DBusConnection *connection);
343 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
344 static void _dbus_connection_release_dispatch (DBusConnection *connection);
345 static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
346 static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection);
347 static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection);
348 static dbus_bool_t _dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
349 dbus_uint32_t client_serial);
351 static DBusMessageFilter *
352 _dbus_message_filter_ref (DBusMessageFilter *filter)
354 #ifdef DBUS_DISABLE_ASSERT
355 _dbus_atomic_inc (&filter->refcount);
357 dbus_int32_t old_value;
359 old_value = _dbus_atomic_inc (&filter->refcount);
360 _dbus_assert (old_value > 0);
367 _dbus_message_filter_unref (DBusMessageFilter *filter)
369 dbus_int32_t old_value;
371 old_value = _dbus_atomic_dec (&filter->refcount);
372 _dbus_assert (old_value > 0);
376 if (filter->free_user_data_function)
377 (* filter->free_user_data_function) (filter->user_data);
384 * Acquires the connection lock.
386 * @param connection the connection.
389 _dbus_connection_lock (DBusConnection *connection)
391 CONNECTION_LOCK (connection);
395 * Releases the connection lock.
397 * @param connection the connection.
400 _dbus_connection_unlock (DBusConnection *connection)
402 DBusList *expired_messages;
407 _dbus_verbose ("UNLOCK\n");
410 /* If we had messages that expired (fell off the incoming or outgoing
411 * queues) while we were locked, actually release them now */
412 expired_messages = connection->expired_messages;
413 connection->expired_messages = NULL;
415 RELEASING_LOCK_CHECK (connection);
416 _dbus_rmutex_unlock (connection->mutex);
418 for (iter = _dbus_list_pop_first_link (&expired_messages);
420 iter = _dbus_list_pop_first_link (&expired_messages))
422 DBusMessage *message = iter->data;
424 dbus_message_unref (message);
425 _dbus_list_free_link (iter);
430 * Wakes up the main loop if it is sleeping
431 * Needed if we're e.g. queueing outgoing messages
432 * on a thread while the mainloop sleeps.
434 * @param connection the connection.
437 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
439 if (connection->wakeup_main_function)
440 (*connection->wakeup_main_function) (connection->wakeup_main_data);
444 _dbus_connection_set_dispatch(DBusConnection *connection,
445 dbus_bool_t disabled)
447 CONNECTION_LOCK (connection);
448 if (connection->dispatch_disabled != disabled)
450 DBusDispatchStatus status;
452 connection->dispatch_disabled = disabled;
453 status = _dbus_connection_get_dispatch_status_unlocked (connection);
454 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
458 CONNECTION_UNLOCK (connection);
464 _dbus_connection_enable_dispatch (DBusConnection *connection)
466 _dbus_connection_set_dispatch (connection, FALSE);
470 _dbus_connection_disable_dispatch (DBusConnection *connection)
472 _dbus_connection_set_dispatch (connection, TRUE);
476 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
478 * Gets the locks so we can examine them
480 * @param connection the connection.
481 * @param mutex_loc return for the location of the main mutex pointer
482 * @param dispatch_mutex_loc return location of the dispatch mutex pointer
483 * @param io_path_mutex_loc return location of the io_path mutex pointer
484 * @param dispatch_cond_loc return location of the dispatch conditional
486 * @param io_path_cond_loc return location of the io_path conditional
490 _dbus_connection_test_get_locks (DBusConnection *connection,
491 DBusMutex **mutex_loc,
492 DBusMutex **dispatch_mutex_loc,
493 DBusMutex **io_path_mutex_loc,
494 DBusCondVar **dispatch_cond_loc,
495 DBusCondVar **io_path_cond_loc)
497 *mutex_loc = (DBusMutex *) connection->mutex;
498 *dispatch_mutex_loc = (DBusMutex *) connection->dispatch_mutex;
499 *io_path_mutex_loc = (DBusMutex *) connection->io_path_mutex;
500 *dispatch_cond_loc = connection->dispatch_cond;
501 *io_path_cond_loc = connection->io_path_cond;
506 * Adds a message-containing list link to the incoming message queue,
507 * taking ownership of the link and the message's current refcount.
508 * Cannot fail due to lack of memory.
510 * @param connection the connection.
511 * @param link the message link to queue.
514 _dbus_connection_queue_received_message_link (DBusConnection *connection,
517 DBusPendingCall *pending;
518 dbus_uint32_t reply_serial;
519 DBusMessage *message;
521 _dbus_assert (_dbus_transport_peek_is_authenticated (connection->transport));
523 _dbus_list_append_link (&connection->incoming_messages,
525 message = link->data;
527 /* If this is a reply we're waiting on, remove timeout for it */
528 reply_serial = dbus_message_get_reply_serial (message);
529 if (reply_serial != 0)
531 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
535 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
536 _dbus_connection_remove_timeout_unlocked (connection,
537 _dbus_pending_call_get_timeout_unlocked (pending));
539 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
545 connection->n_incoming += 1;
547 _dbus_connection_wakeup_mainloop (connection);
549 _dbus_verbose ("Message %p (%s %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
551 dbus_message_type_to_string (dbus_message_get_type (message)),
552 dbus_message_get_path (message) ?
553 dbus_message_get_path (message) :
555 dbus_message_get_interface (message) ?
556 dbus_message_get_interface (message) :
558 dbus_message_get_member (message) ?
559 dbus_message_get_member (message) :
561 dbus_message_get_signature (message),
562 dbus_message_get_reply_serial (message),
564 connection->n_incoming);
566 _dbus_message_trace_ref (message, -1, -1,
567 "_dbus_conection_queue_received_message_link");
571 * Adds a link + message to the incoming message queue.
572 * Can't fail. Takes ownership of both link and message.
574 * @param connection the connection.
575 * @param link the list node and message to queue.
579 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
582 DBusMessage *msg, *rmsg;
584 HAVE_LOCK_CHECK (connection);
586 msg = (DBusMessage *)link->data;
589 if (_dbus_transport_assure_protocol_version (connection->transport, &rmsg))
591 /* If the message is converted, then we don't need the old format anymore */
592 _dbus_list_free_link(link);
593 link = _dbus_list_alloc_link (rmsg);
594 dbus_message_unref (msg);
597 _dbus_list_append_link (&connection->incoming_messages, link);
599 connection->n_incoming += 1;
601 _dbus_connection_wakeup_mainloop (connection);
603 _dbus_message_trace_ref (link->data, -1, -1,
604 "_dbus_connection_queue_synthesized_message_link");
606 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
607 link->data, connection, connection->n_incoming);
612 * Checks whether there are messages in the outgoing message queue.
613 * Called with connection lock held.
615 * @param connection the connection.
616 * @returns #TRUE if the outgoing queue is non-empty.
619 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
621 HAVE_LOCK_CHECK (connection);
622 return connection->outgoing_messages != NULL;
626 * Checks whether there are messages in the outgoing message queue.
627 * Use dbus_connection_flush() to block until all outgoing
628 * messages have been written to the underlying transport
629 * (such as a socket).
631 * @param connection the connection.
632 * @returns #TRUE if the outgoing queue is non-empty.
635 dbus_connection_has_messages_to_send (DBusConnection *connection)
639 _dbus_return_val_if_fail (connection != NULL, FALSE);
641 CONNECTION_LOCK (connection);
642 v = _dbus_connection_has_messages_to_send_unlocked (connection);
643 CONNECTION_UNLOCK (connection);
649 * Gets the next outgoing message. The message remains in the
650 * queue, and the caller does not own a reference to it.
652 * @param connection the connection.
653 * @returns the message to be sent.
656 _dbus_connection_get_message_to_send (DBusConnection *connection)
658 HAVE_LOCK_CHECK (connection);
660 return _dbus_list_get_last (&connection->outgoing_messages);
664 * Notifies the connection that a message has been sent, so the
665 * message can be removed from the outgoing queue.
666 * Called with the connection lock held.
668 * @param connection the connection.
669 * @param message the message that was sent.
672 _dbus_connection_message_sent_unlocked (DBusConnection *connection,
673 DBusMessage *message)
677 HAVE_LOCK_CHECK (connection);
679 /* This can be called before we even complete authentication, since
680 * it's called on disconnect to clean up the outgoing queue.
681 * It's also called as we successfully send each message.
684 link = _dbus_list_get_last_link (&connection->outgoing_messages);
685 _dbus_assert (link != NULL);
686 _dbus_assert (link->data == message);
688 _dbus_list_unlink (&connection->outgoing_messages,
690 _dbus_list_prepend_link (&connection->expired_messages, link);
692 connection->n_outgoing -= 1;
694 _dbus_verbose ("Message %p (%s %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
696 dbus_message_type_to_string (dbus_message_get_type (message)),
697 dbus_message_get_path (message) ?
698 dbus_message_get_path (message) :
700 dbus_message_get_interface (message) ?
701 dbus_message_get_interface (message) :
703 dbus_message_get_member (message) ?
704 dbus_message_get_member (message) :
706 dbus_message_get_signature (message),
707 connection, connection->n_outgoing);
709 /* It's OK that in principle we call the notify function, because for the
710 * outgoing limit, there isn't one */
711 _dbus_message_remove_counter (message, connection->outgoing_counter);
713 /* The message will actually be unreffed when we unlock */
716 /** Function to be called in protected_change_watch() with refcount held */
717 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
719 /** Function to be called in protected_change_watch() with refcount held */
720 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
722 /** Function to be called in protected_change_watch() with refcount held */
723 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
725 dbus_bool_t enabled);
728 protected_change_watch (DBusConnection *connection,
730 DBusWatchAddFunction add_function,
731 DBusWatchRemoveFunction remove_function,
732 DBusWatchToggleFunction toggle_function,
737 HAVE_LOCK_CHECK (connection);
739 /* The original purpose of protected_change_watch() was to hold a
740 * ref on the connection while dropping the connection lock, then
741 * calling out to the app. This was a broken hack that did not
742 * work, since the connection was in a hosed state (no WatchList
743 * field) while calling out.
745 * So for now we'll just keep the lock while calling out. This means
746 * apps are not allowed to call DBusConnection methods inside a
747 * watch function or they will deadlock.
749 * The "real fix" is to use the _and_unlock() pattern found
750 * elsewhere in the code, to defer calling out to the app until
751 * we're about to drop locks and return flow of control to the app
754 * See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
757 if (connection->watches)
760 retval = (* add_function) (connection->watches, watch);
761 else if (remove_function)
764 (* remove_function) (connection->watches, watch);
769 (* toggle_function) (connection->watches, watch, enabled);
779 * Adds a watch using the connection's DBusAddWatchFunction if
780 * available. Otherwise records the watch to be added when said
781 * function is available. Also re-adds the watch if the
782 * DBusAddWatchFunction changes. May fail due to lack of memory.
783 * Connection lock should be held when calling this.
785 * @param connection the connection.
786 * @param watch the watch to add.
787 * @returns #TRUE on success.
790 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
793 return protected_change_watch (connection, watch,
794 _dbus_watch_list_add_watch,
799 * Removes a watch using the connection's DBusRemoveWatchFunction
800 * if available. It's an error to call this function on a watch
801 * that was not previously added.
802 * Connection lock should be held when calling this.
804 * @param connection the connection.
805 * @param watch the watch to remove.
808 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
811 protected_change_watch (connection, watch,
813 _dbus_watch_list_remove_watch,
818 * Toggles a watch and notifies app via connection's
819 * DBusWatchToggledFunction if available. It's an error to call this
820 * function on a watch that was not previously added.
821 * Connection lock should be held when calling this.
823 * @param connection the connection.
824 * @param watch the watch to toggle.
825 * @param enabled whether to enable or disable
828 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
832 _dbus_assert (watch != NULL);
834 protected_change_watch (connection, watch,
836 _dbus_watch_list_toggle_watch,
840 /** Function to be called in protected_change_timeout() with refcount held */
841 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
842 DBusTimeout *timeout);
843 /** Function to be called in protected_change_timeout() with refcount held */
844 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
845 DBusTimeout *timeout);
846 /** Function to be called in protected_change_timeout() with refcount held */
847 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
848 DBusTimeout *timeout,
849 dbus_bool_t enabled);
852 protected_change_timeout (DBusConnection *connection,
853 DBusTimeout *timeout,
854 DBusTimeoutAddFunction add_function,
855 DBusTimeoutRemoveFunction remove_function,
856 DBusTimeoutToggleFunction toggle_function,
861 HAVE_LOCK_CHECK (connection);
863 /* The original purpose of protected_change_timeout() was to hold a
864 * ref on the connection while dropping the connection lock, then
865 * calling out to the app. This was a broken hack that did not
866 * work, since the connection was in a hosed state (no TimeoutList
867 * field) while calling out.
869 * So for now we'll just keep the lock while calling out. This means
870 * apps are not allowed to call DBusConnection methods inside a
871 * timeout function or they will deadlock.
873 * The "real fix" is to use the _and_unlock() pattern found
874 * elsewhere in the code, to defer calling out to the app until
875 * we're about to drop locks and return flow of control to the app
878 * See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
881 if (connection->timeouts)
884 retval = (* add_function) (connection->timeouts, timeout);
885 else if (remove_function)
888 (* remove_function) (connection->timeouts, timeout);
893 (* toggle_function) (connection->timeouts, timeout, enabled);
902 * Adds a timeout using the connection's DBusAddTimeoutFunction if
903 * available. Otherwise records the timeout to be added when said
904 * function is available. Also re-adds the timeout if the
905 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
906 * The timeout will fire repeatedly until removed.
907 * Connection lock should be held when calling this.
909 * @param connection the connection.
910 * @param timeout the timeout to add.
911 * @returns #TRUE on success.
914 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
915 DBusTimeout *timeout)
917 return protected_change_timeout (connection, timeout,
918 _dbus_timeout_list_add_timeout,
923 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
924 * if available. It's an error to call this function on a timeout
925 * that was not previously added.
926 * Connection lock should be held when calling this.
928 * @param connection the connection.
929 * @param timeout the timeout to remove.
932 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
933 DBusTimeout *timeout)
935 protected_change_timeout (connection, timeout,
937 _dbus_timeout_list_remove_timeout,
942 * Toggles a timeout and notifies app via connection's
943 * DBusTimeoutToggledFunction if available. It's an error to call this
944 * function on a timeout that was not previously added.
945 * Connection lock should be held when calling this.
947 * @param connection the connection.
948 * @param timeout the timeout to toggle.
949 * @param enabled whether to enable or disable
952 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
953 DBusTimeout *timeout,
956 protected_change_timeout (connection, timeout,
958 _dbus_timeout_list_toggle_timeout,
963 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
964 DBusPendingCall *pending)
966 dbus_uint32_t reply_serial;
967 DBusTimeout *timeout;
969 HAVE_LOCK_CHECK (connection);
971 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
973 _dbus_assert (reply_serial != 0);
975 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
979 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
982 if (!_dbus_hash_table_insert_int (connection->pending_replies,
986 _dbus_connection_remove_timeout_unlocked (connection, timeout);
988 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
989 HAVE_LOCK_CHECK (connection);
993 _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
997 if (!_dbus_hash_table_insert_int (connection->pending_replies,
1001 HAVE_LOCK_CHECK (connection);
1006 _dbus_pending_call_ref_unlocked (pending);
1008 HAVE_LOCK_CHECK (connection);
1014 free_pending_call_on_hash_removal (void *data)
1016 DBusPendingCall *pending;
1017 DBusConnection *connection;
1024 connection = _dbus_pending_call_get_connection_unlocked (pending);
1026 HAVE_LOCK_CHECK (connection);
1028 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
1030 _dbus_connection_remove_timeout_unlocked (connection,
1031 _dbus_pending_call_get_timeout_unlocked (pending));
1033 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
1036 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
1037 * here, but the pending call finalizer could in principle call out to
1038 * application code so we pretty much have to... some larger code reorg
1041 _dbus_connection_ref_unlocked (connection);
1042 _dbus_pending_call_unref_and_unlock (pending);
1043 CONNECTION_LOCK (connection);
1044 _dbus_connection_unref_unlocked (connection);
1048 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
1049 DBusPendingCall *pending)
1051 /* This ends up unlocking to call the pending call finalizer, which is unexpected to
1054 _dbus_hash_table_remove_int (connection->pending_replies,
1055 _dbus_pending_call_get_reply_serial_unlocked (pending));
1059 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
1060 DBusPendingCall *pending)
1062 /* The idea here is to avoid finalizing the pending call
1063 * with the lock held, since there's a destroy notifier
1064 * in pending call that goes out to application code.
1066 * There's an extra unlock inside the hash table
1067 * "free pending call" function FIXME...
1069 _dbus_pending_call_ref_unlocked (pending);
1070 _dbus_hash_table_remove_int (connection->pending_replies,
1071 _dbus_pending_call_get_reply_serial_unlocked (pending));
1073 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
1074 _dbus_connection_remove_timeout_unlocked (connection,
1075 _dbus_pending_call_get_timeout_unlocked (pending));
1077 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
1079 _dbus_pending_call_unref_and_unlock (pending);
1083 * Removes a pending call from the connection, such that
1084 * the pending reply will be ignored. May drop the last
1085 * reference to the pending call.
1087 * @param connection the connection
1088 * @param pending the pending call
1091 _dbus_connection_remove_pending_call (DBusConnection *connection,
1092 DBusPendingCall *pending)
1094 CONNECTION_LOCK (connection);
1095 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
1099 * Acquire the transporter I/O path. This must be done before
1100 * doing any I/O in the transporter. May sleep and drop the
1101 * IO path mutex while waiting for the I/O path.
1103 * @param connection the connection.
1104 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1105 * @returns TRUE if the I/O path was acquired.
1108 _dbus_connection_acquire_io_path (DBusConnection *connection,
1109 int timeout_milliseconds)
1111 dbus_bool_t we_acquired;
1113 HAVE_LOCK_CHECK (connection);
1115 /* We don't want the connection to vanish */
1116 _dbus_connection_ref_unlocked (connection);
1118 /* We will only touch io_path_acquired which is protected by our mutex */
1119 CONNECTION_UNLOCK (connection);
1121 _dbus_verbose ("locking io_path_mutex\n");
1122 _dbus_cmutex_lock (connection->io_path_mutex);
1124 _dbus_verbose ("start connection->io_path_acquired = %d timeout = %d\n",
1125 connection->io_path_acquired, timeout_milliseconds);
1127 we_acquired = FALSE;
1129 if (connection->io_path_acquired)
1131 if (timeout_milliseconds != -1)
1133 _dbus_verbose ("waiting %d for IO path to be acquirable\n",
1134 timeout_milliseconds);
1136 if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1137 connection->io_path_mutex,
1138 timeout_milliseconds))
1140 /* We timed out before anyone signaled. */
1141 /* (writing the loop to handle the !timedout case by
1142 * waiting longer if needed is a pain since dbus
1143 * wraps pthread_cond_timedwait to take a relative
1144 * time instead of absolute, something kind of stupid
1145 * on our part. for now it doesn't matter, we will just
1146 * end up back here eventually.)
1152 while (connection->io_path_acquired)
1154 _dbus_verbose ("waiting for IO path to be acquirable\n");
1155 _dbus_condvar_wait (connection->io_path_cond,
1156 connection->io_path_mutex);
1161 if (!connection->io_path_acquired)
1164 connection->io_path_acquired = TRUE;
1167 _dbus_verbose ("end connection->io_path_acquired = %d we_acquired = %d\n",
1168 connection->io_path_acquired, we_acquired);
1170 _dbus_verbose ("unlocking io_path_mutex\n");
1171 _dbus_cmutex_unlock (connection->io_path_mutex);
1173 CONNECTION_LOCK (connection);
1175 HAVE_LOCK_CHECK (connection);
1177 _dbus_connection_unref_unlocked (connection);
1183 * Release the I/O path when you're done with it. Only call
1184 * after you've acquired the I/O. Wakes up at most one thread
1185 * currently waiting to acquire the I/O path.
1187 * @param connection the connection.
1190 _dbus_connection_release_io_path (DBusConnection *connection)
1192 HAVE_LOCK_CHECK (connection);
1194 _dbus_verbose ("locking io_path_mutex\n");
1195 _dbus_cmutex_lock (connection->io_path_mutex);
1197 _dbus_assert (connection->io_path_acquired);
1199 _dbus_verbose ("start connection->io_path_acquired = %d\n",
1200 connection->io_path_acquired);
1202 connection->io_path_acquired = FALSE;
1203 _dbus_condvar_wake_one (connection->io_path_cond);
1205 _dbus_verbose ("unlocking io_path_mutex\n");
1206 _dbus_cmutex_unlock (connection->io_path_mutex);
1210 * Queues incoming messages and sends outgoing messages for this
1211 * connection, optionally blocking in the process. Each call to
1212 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1213 * time and then read or write data if possible.
1215 * The purpose of this function is to be able to flush outgoing
1216 * messages or queue up incoming messages without returning
1217 * control to the application and causing reentrancy weirdness.
1219 * The flags parameter allows you to specify whether to
1220 * read incoming messages, write outgoing messages, or both,
1221 * and whether to block if no immediate action is possible.
1223 * The timeout_milliseconds parameter does nothing unless the
1224 * iteration is blocking.
1226 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1227 * wasn't specified, then it's impossible to block, even if
1228 * you specify DBUS_ITERATION_BLOCK; in that case the function
1229 * returns immediately.
1231 * If pending is not NULL then a check is made if the pending call
1232 * is completed after the io path has been required. If the call
1233 * has been completed nothing is done. This must be done since
1234 * the _dbus_connection_acquire_io_path releases the connection
1237 * Called with connection lock held.
1239 * @param connection the connection.
1240 * @param pending the pending call that should be checked or NULL
1241 * @param flags iteration flags.
1242 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1245 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1246 DBusPendingCall *pending,
1248 int timeout_milliseconds)
1250 _dbus_verbose ("start\n");
1252 HAVE_LOCK_CHECK (connection);
1254 if (connection->n_outgoing == 0)
1255 flags &= ~DBUS_ITERATION_DO_WRITING;
1257 if (_dbus_connection_acquire_io_path (connection,
1258 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1260 HAVE_LOCK_CHECK (connection);
1262 if ( (pending != NULL) && _dbus_pending_call_get_completed_unlocked(pending))
1264 _dbus_verbose ("pending call completed while acquiring I/O path");
1266 else if ( (pending != NULL) &&
1267 _dbus_connection_peek_for_reply_unlocked (connection,
1268 _dbus_pending_call_get_reply_serial_unlocked (pending)))
1270 _dbus_verbose ("pending call completed while acquiring I/O path (reply found in queue)");
1274 _dbus_transport_do_iteration (connection->transport,
1275 flags, timeout_milliseconds);
1278 _dbus_connection_release_io_path (connection);
1281 HAVE_LOCK_CHECK (connection);
1283 _dbus_verbose ("end\n");
1287 * Creates a new connection for the given transport. A transport
1288 * represents a message stream that uses some concrete mechanism, such
1289 * as UNIX domain sockets. May return #NULL if insufficient
1290 * memory exists to create the connection.
1292 * @param transport the transport.
1293 * @returns the new connection, or #NULL on failure.
1296 _dbus_connection_new_for_transport (DBusTransport *transport)
1298 DBusConnection *connection;
1299 DBusWatchList *watch_list;
1300 DBusTimeoutList *timeout_list;
1301 DBusHashTable *pending_replies;
1302 DBusList *disconnect_link;
1303 DBusMessage *disconnect_message;
1304 DBusCounter *outgoing_counter;
1305 DBusObjectTree *objects;
1309 pending_replies = NULL;
1310 timeout_list = NULL;
1311 disconnect_link = NULL;
1312 disconnect_message = NULL;
1313 outgoing_counter = NULL;
1316 watch_list = _dbus_watch_list_new ();
1317 if (watch_list == NULL)
1320 timeout_list = _dbus_timeout_list_new ();
1321 if (timeout_list == NULL)
1325 _dbus_hash_table_new (DBUS_HASH_INT,
1327 (DBusFreeFunction)free_pending_call_on_hash_removal);
1328 if (pending_replies == NULL)
1331 connection = dbus_new0 (DBusConnection, 1);
1332 if (connection == NULL)
1335 _dbus_rmutex_new_at_location (&connection->mutex);
1336 if (connection->mutex == NULL)
1339 _dbus_cmutex_new_at_location (&connection->io_path_mutex);
1340 if (connection->io_path_mutex == NULL)
1343 _dbus_cmutex_new_at_location (&connection->dispatch_mutex);
1344 if (connection->dispatch_mutex == NULL)
1347 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1348 if (connection->dispatch_cond == NULL)
1351 _dbus_condvar_new_at_location (&connection->io_path_cond);
1352 if (connection->io_path_cond == NULL)
1355 _dbus_rmutex_new_at_location (&connection->slot_mutex);
1356 if (connection->slot_mutex == NULL)
1359 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1360 DBUS_INTERFACE_LOCAL,
1363 if (disconnect_message == NULL)
1366 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1367 if (disconnect_link == NULL)
1370 outgoing_counter = _dbus_counter_new ();
1371 if (outgoing_counter == NULL)
1374 objects = _dbus_object_tree_new (connection);
1375 if (objects == NULL)
1378 if (_dbus_modify_sigpipe)
1379 _dbus_disable_sigpipe ();
1381 /* initialized to 0: use atomic op to avoid mixing atomic and non-atomic */
1382 _dbus_atomic_inc (&connection->refcount);
1383 connection->transport = transport;
1384 connection->watches = watch_list;
1385 connection->timeouts = timeout_list;
1386 connection->pending_replies = pending_replies;
1387 connection->outgoing_counter = outgoing_counter;
1388 connection->filter_list = NULL;
1389 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1390 connection->objects = objects;
1391 connection->exit_on_disconnect = FALSE;
1392 connection->shareable = FALSE;
1393 connection->route_peer_messages = FALSE;
1394 connection->disconnected_message_arrived = FALSE;
1395 connection->disconnected_message_processed = FALSE;
1397 #if defined(DBUS_ENABLE_CHECKS) || defined(DBUS_ENABLE_ASSERT)
1398 connection->generation = _dbus_current_generation;
1401 _dbus_data_slot_list_init (&connection->slot_list);
1403 connection->client_serial = 1;
1405 connection->disconnect_message_link = disconnect_link;
1407 CONNECTION_LOCK (connection);
1409 if (!_dbus_transport_set_connection (transport, connection))
1411 CONNECTION_UNLOCK (connection);
1416 _dbus_transport_ref (transport);
1418 CONNECTION_UNLOCK (connection);
1420 _dbus_connection_trace_ref (connection, 0, 1, "new_for_transport");
1424 if (disconnect_message != NULL)
1425 dbus_message_unref (disconnect_message);
1427 if (disconnect_link != NULL)
1428 _dbus_list_free_link (disconnect_link);
1430 if (connection != NULL)
1432 _dbus_condvar_free_at_location (&connection->io_path_cond);
1433 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1434 _dbus_rmutex_free_at_location (&connection->mutex);
1435 _dbus_cmutex_free_at_location (&connection->io_path_mutex);
1436 _dbus_cmutex_free_at_location (&connection->dispatch_mutex);
1437 _dbus_rmutex_free_at_location (&connection->slot_mutex);
1438 dbus_free (connection);
1440 if (pending_replies)
1441 _dbus_hash_table_unref (pending_replies);
1444 _dbus_watch_list_free (watch_list);
1447 _dbus_timeout_list_free (timeout_list);
1449 if (outgoing_counter)
1450 _dbus_counter_unref (outgoing_counter);
1453 _dbus_object_tree_unref (objects);
1459 * Increments the reference count of a DBusConnection.
1460 * Requires that the caller already holds the connection lock.
1462 * @param connection the connection.
1463 * @returns the connection.
1466 _dbus_connection_ref_unlocked (DBusConnection *connection)
1468 dbus_int32_t old_refcount;
1470 _dbus_assert (connection != NULL);
1471 _dbus_assert (connection->generation == _dbus_current_generation);
1473 HAVE_LOCK_CHECK (connection);
1475 old_refcount = _dbus_atomic_inc (&connection->refcount);
1476 _dbus_connection_trace_ref (connection, old_refcount, old_refcount + 1,
1483 * Decrements the reference count of a DBusConnection.
1484 * Requires that the caller already holds the connection lock.
1486 * @param connection the connection.
1489 _dbus_connection_unref_unlocked (DBusConnection *connection)
1491 dbus_int32_t old_refcount;
1493 HAVE_LOCK_CHECK (connection);
1495 _dbus_assert (connection != NULL);
1497 old_refcount = _dbus_atomic_dec (&connection->refcount);
1499 _dbus_connection_trace_ref (connection, old_refcount, old_refcount - 1,
1502 if (old_refcount == 1)
1503 _dbus_connection_last_unref (connection);
1506 static dbus_uint32_t
1507 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1509 dbus_uint32_t serial;
1511 serial = connection->client_serial++;
1513 if (connection->client_serial == 0)
1514 connection->client_serial = 1;
1520 * A callback for use with dbus_watch_new() to create a DBusWatch.
1522 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1523 * and the virtual handle_watch in DBusTransport if we got rid of it.
1524 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1527 * @param watch the watch.
1528 * @param condition the current condition of the file descriptors being watched.
1529 * @param data must be a pointer to a #DBusConnection
1530 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1533 _dbus_connection_handle_watch (DBusWatch *watch,
1534 unsigned int condition,
1537 DBusConnection *connection;
1539 DBusDispatchStatus status;
1543 _dbus_verbose ("start\n");
1545 CONNECTION_LOCK (connection);
1547 if (!_dbus_connection_acquire_io_path (connection, 1))
1549 /* another thread is handling the message */
1550 CONNECTION_UNLOCK (connection);
1554 HAVE_LOCK_CHECK (connection);
1555 retval = _dbus_transport_handle_watch (connection->transport,
1558 _dbus_connection_release_io_path (connection);
1560 HAVE_LOCK_CHECK (connection);
1562 _dbus_verbose ("middle\n");
1564 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1566 /* this calls out to user code */
1567 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1569 _dbus_verbose ("end\n");
1574 /* Protected by _DBUS_LOCK (shared_connections) */
1575 static DBusHashTable *shared_connections = NULL;
1576 static DBusList *shared_connections_no_guid = NULL;
1579 close_connection_on_shutdown (DBusConnection *connection)
1581 DBusMessage *message;
1583 dbus_connection_ref (connection);
1584 _dbus_connection_close_possibly_shared (connection);
1586 /* Churn through to the Disconnected message */
1587 while ((message = dbus_connection_pop_message (connection)))
1589 dbus_message_unref (message);
1591 dbus_connection_unref (connection);
1595 shared_connections_shutdown (void *data)
1599 if (!_DBUS_LOCK (shared_connections))
1601 /* We'd have initialized locks before adding anything, so there
1602 * can't be anything there. */
1606 /* This is a little bit unpleasant... better ideas? */
1607 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
1609 DBusConnection *connection;
1612 _dbus_hash_iter_init (shared_connections, &iter);
1613 _dbus_hash_iter_next (&iter);
1615 connection = _dbus_hash_iter_get_value (&iter);
1617 _DBUS_UNLOCK (shared_connections);
1618 close_connection_on_shutdown (connection);
1619 if (!_DBUS_LOCK (shared_connections))
1620 _dbus_assert_not_reached ("global locks were already initialized");
1622 /* The connection should now be dead and not in our hash ... */
1623 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
1626 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1628 _dbus_hash_table_unref (shared_connections);
1629 shared_connections = NULL;
1631 if (shared_connections_no_guid != NULL)
1633 DBusConnection *connection;
1634 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1635 while (connection != NULL)
1637 _DBUS_UNLOCK (shared_connections);
1638 close_connection_on_shutdown (connection);
1639 if (!_DBUS_LOCK (shared_connections))
1640 _dbus_assert_not_reached ("global locks were already initialized");
1641 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1645 shared_connections_no_guid = NULL;
1647 _DBUS_UNLOCK (shared_connections);
1651 connection_lookup_shared (DBusAddressEntry *entry,
1652 DBusConnection **result)
1654 _dbus_verbose ("checking for existing connection\n");
1658 if (!_DBUS_LOCK (shared_connections))
1660 /* If it was shared, we'd have initialized global locks when we put
1661 * it in shared_connections. */
1665 if (shared_connections == NULL)
1667 _dbus_verbose ("creating shared_connections hash table\n");
1669 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1672 if (shared_connections == NULL)
1674 _DBUS_UNLOCK (shared_connections);
1678 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1680 _dbus_hash_table_unref (shared_connections);
1681 shared_connections = NULL;
1682 _DBUS_UNLOCK (shared_connections);
1686 _dbus_verbose (" successfully created shared_connections\n");
1688 _DBUS_UNLOCK (shared_connections);
1689 return TRUE; /* no point looking up in the hash we just made */
1695 guid = dbus_address_entry_get_value (entry, "guid");
1699 DBusConnection *connection;
1701 connection = _dbus_hash_table_lookup_string (shared_connections,
1706 /* The DBusConnection can't be finalized without taking
1707 * the shared_connections lock to remove it from the
1708 * hash. So it's safe to ref the connection here.
1709 * However, it may be disconnected if the Disconnected
1710 * message hasn't been processed yet, in which case we
1711 * want to pretend it isn't in the hash and avoid
1714 * The idea is to avoid ever returning a disconnected connection
1715 * from dbus_connection_open(). We could just synchronously
1716 * drop our shared ref to the connection on connection disconnect,
1717 * and then assert here that the connection is connected, but
1718 * that causes reentrancy headaches.
1720 CONNECTION_LOCK (connection);
1721 if (_dbus_connection_get_is_connected_unlocked (connection))
1723 _dbus_connection_ref_unlocked (connection);
1724 *result = connection;
1725 _dbus_verbose ("looked up existing connection to server guid %s\n",
1730 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1733 CONNECTION_UNLOCK (connection);
1737 _DBUS_UNLOCK (shared_connections);
1743 connection_record_shared_unlocked (DBusConnection *connection,
1747 char *guid_in_connection;
1749 HAVE_LOCK_CHECK (connection);
1750 _dbus_assert (connection->server_guid == NULL);
1751 _dbus_assert (connection->shareable);
1753 /* get a hard ref on this connection, even if
1754 * we won't in fact store it in the hash, we still
1755 * need to hold a ref on it until it's disconnected.
1757 _dbus_connection_ref_unlocked (connection);
1761 if (!_DBUS_LOCK (shared_connections))
1764 if (!_dbus_list_prepend (&shared_connections_no_guid, connection))
1766 _DBUS_UNLOCK (shared_connections);
1770 _DBUS_UNLOCK (shared_connections);
1771 return TRUE; /* don't store in the hash */
1774 /* A separate copy of the key is required in the hash table, because
1775 * we don't have a lock on the connection when we are doing a hash
1779 guid_key = _dbus_strdup (guid);
1780 if (guid_key == NULL)
1783 guid_in_connection = _dbus_strdup (guid);
1784 if (guid_in_connection == NULL)
1786 dbus_free (guid_key);
1790 if (!_DBUS_LOCK (shared_connections))
1792 dbus_free (guid_in_connection);
1793 dbus_free (guid_key);
1797 _dbus_assert (shared_connections != NULL);
1799 if (!_dbus_hash_table_insert_string (shared_connections,
1800 guid_key, connection))
1802 dbus_free (guid_key);
1803 dbus_free (guid_in_connection);
1804 _DBUS_UNLOCK (shared_connections);
1808 connection->server_guid = guid_in_connection;
1810 _dbus_verbose ("stored connection to %s to be shared\n",
1811 connection->server_guid);
1813 _DBUS_UNLOCK (shared_connections);
1815 _dbus_assert (connection->server_guid != NULL);
1821 connection_forget_shared_unlocked (DBusConnection *connection)
1823 HAVE_LOCK_CHECK (connection);
1825 if (!connection->shareable)
1828 if (!_DBUS_LOCK (shared_connections))
1830 /* If it was shared, we'd have initialized global locks when we put
1831 * it in the table; so it can't be there. */
1835 if (connection->server_guid != NULL)
1837 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1838 connection->server_guid);
1840 if (!_dbus_hash_table_remove_string (shared_connections,
1841 connection->server_guid))
1842 _dbus_assert_not_reached ("connection was not in the shared table");
1844 dbus_free (connection->server_guid);
1845 connection->server_guid = NULL;
1849 _dbus_list_remove (&shared_connections_no_guid, connection);
1852 _DBUS_UNLOCK (shared_connections);
1854 /* remove our reference held on all shareable connections */
1855 _dbus_connection_unref_unlocked (connection);
1858 static DBusConnection*
1859 connection_try_from_address_entry (DBusAddressEntry *entry,
1862 DBusTransport *transport;
1863 DBusConnection *connection;
1865 transport = _dbus_transport_open (entry, error);
1867 if (transport == NULL)
1869 _DBUS_ASSERT_ERROR_IS_SET (error);
1873 connection = _dbus_connection_new_for_transport (transport);
1875 _dbus_transport_unref (transport);
1877 if (connection == NULL)
1879 _DBUS_SET_OOM (error);
1883 #ifndef DBUS_DISABLE_CHECKS
1884 _dbus_assert (!connection->have_connection_lock);
1890 * If the shared parameter is true, then any existing connection will
1891 * be used (and if a new connection is created, it will be available
1892 * for use by others). If the shared parameter is false, a new
1893 * connection will always be created, and the new connection will
1894 * never be returned to other callers.
1896 * @param address the address
1897 * @param shared whether the connection is shared or private
1898 * @param error error return
1899 * @returns the connection or #NULL on error
1901 static DBusConnection*
1902 _dbus_connection_open_internal (const char *address,
1906 DBusConnection *connection;
1907 DBusAddressEntry **entries;
1908 DBusError tmp_error = DBUS_ERROR_INIT;
1909 DBusError first_error = DBUS_ERROR_INIT;
1912 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1914 _dbus_verbose ("opening %s connection to: %s\n",
1915 shared ? "shared" : "private", address);
1917 if (!dbus_parse_address (address, &entries, &len, error))
1920 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1924 for (i = 0; i < len; i++)
1928 if (!connection_lookup_shared (entries[i], &connection))
1929 _DBUS_SET_OOM (&tmp_error);
1932 if (connection == NULL)
1934 connection = connection_try_from_address_entry (entries[i],
1937 if (connection != NULL && shared)
1941 connection->shareable = TRUE;
1943 /* guid may be NULL */
1944 guid = dbus_address_entry_get_value (entries[i], "guid");
1946 CONNECTION_LOCK (connection);
1948 if (!connection_record_shared_unlocked (connection, guid))
1950 _DBUS_SET_OOM (&tmp_error);
1951 _dbus_connection_close_possibly_shared_and_unlock (connection);
1952 dbus_connection_unref (connection);
1956 CONNECTION_UNLOCK (connection);
1963 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1966 dbus_move_error (&tmp_error, &first_error);
1968 dbus_error_free (&tmp_error);
1971 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1972 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1974 if (connection == NULL)
1976 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1977 dbus_move_error (&first_error, error);
1980 dbus_error_free (&first_error);
1982 dbus_address_entries_free (entries);
1987 * Closes a shared OR private connection, while dbus_connection_close() can
1988 * only be used on private connections. Should only be called by the
1989 * dbus code that owns the connection - an owner must be known,
1990 * the open/close state is like malloc/free, not like ref/unref.
1992 * @param connection the connection
1995 _dbus_connection_close_possibly_shared (DBusConnection *connection)
1997 _dbus_assert (connection != NULL);
1998 _dbus_assert (connection->generation == _dbus_current_generation);
2000 CONNECTION_LOCK (connection);
2001 _dbus_connection_close_possibly_shared_and_unlock (connection);
2004 static DBusPreallocatedSend*
2005 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
2007 DBusPreallocatedSend *preallocated;
2009 HAVE_LOCK_CHECK (connection);
2011 _dbus_assert (connection != NULL);
2013 preallocated = dbus_new (DBusPreallocatedSend, 1);
2014 if (preallocated == NULL)
2017 preallocated->queue_link = _dbus_list_alloc_link (NULL);
2018 if (preallocated->queue_link == NULL)
2021 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
2022 if (preallocated->counter_link == NULL)
2025 _dbus_counter_ref (preallocated->counter_link->data);
2027 preallocated->connection = connection;
2029 return preallocated;
2032 _dbus_list_free_link (preallocated->queue_link);
2034 dbus_free (preallocated);
2039 /* Called with lock held, does not update dispatch status */
2041 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
2042 DBusPreallocatedSend *preallocated,
2043 DBusMessage *message,
2044 dbus_uint32_t *client_serial)
2046 dbus_uint32_t serial;
2048 /* Finish preparing the message */
2049 if (dbus_message_get_serial (message) == 0)
2051 serial = _dbus_connection_get_next_client_serial (connection);
2052 dbus_message_set_serial (message, serial);
2054 *client_serial = serial;
2059 *client_serial = dbus_message_get_serial (message);
2062 _dbus_verbose ("Message %p serial is %u\n",
2063 message, dbus_message_get_serial (message));
2065 dbus_message_lock (message);
2067 /* This converts message if neccessary */
2068 if (!_dbus_transport_assure_protocol_version (connection->transport, &message))
2070 /* Only non-converted messages must be refed.
2071 * Converted messages are local anyway.
2073 dbus_message_ref (message);
2076 preallocated->queue_link->data = message;
2077 _dbus_list_prepend_link (&connection->outgoing_messages,
2078 preallocated->queue_link);
2080 /* It's OK that we'll never call the notify function, because for the
2081 * outgoing limit, there isn't one */
2082 _dbus_message_add_counter_link (message,
2083 preallocated->counter_link);
2085 dbus_free (preallocated);
2086 preallocated = NULL;
2088 connection->n_outgoing += 1;
2090 _dbus_verbose ("Message %p (%s %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
2092 dbus_message_type_to_string (dbus_message_get_type (message)),
2093 dbus_message_get_path (message) ?
2094 dbus_message_get_path (message) :
2096 dbus_message_get_interface (message) ?
2097 dbus_message_get_interface (message) :
2099 dbus_message_get_member (message) ?
2100 dbus_message_get_member (message) :
2102 dbus_message_get_signature (message),
2103 dbus_message_get_destination (message) ?
2104 dbus_message_get_destination (message) :
2107 connection->n_outgoing);
2109 /* Now we need to run an iteration to hopefully just write the messages
2110 * out immediately, and otherwise get them queued up
2112 _dbus_connection_do_iteration_unlocked (connection,
2114 DBUS_ITERATION_DO_WRITING,
2117 /* If stuff is still queued up, be sure we wake up the main loop */
2118 if (connection->n_outgoing > 0)
2119 _dbus_connection_wakeup_mainloop (connection);
2123 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
2124 DBusPreallocatedSend *preallocated,
2125 DBusMessage *message,
2126 dbus_uint32_t *client_serial)
2128 DBusDispatchStatus status;
2130 HAVE_LOCK_CHECK (connection);
2132 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2134 message, client_serial);
2136 _dbus_verbose ("middle\n");
2137 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2139 /* this calls out to user code */
2140 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2144 * Like dbus_connection_send(), but assumes the connection
2145 * is already locked on function entry, and unlocks before returning.
2147 * @param connection the connection
2148 * @param message the message to send
2149 * @param client_serial return location for client serial of sent message
2150 * @returns #FALSE on out-of-memory
2153 _dbus_connection_send_and_unlock (DBusConnection *connection,
2154 DBusMessage *message,
2155 dbus_uint32_t *client_serial)
2157 DBusPreallocatedSend *preallocated;
2159 _dbus_assert (connection != NULL);
2160 _dbus_assert (message != NULL);
2162 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2163 if (preallocated == NULL)
2165 CONNECTION_UNLOCK (connection);
2169 _dbus_connection_send_preallocated_and_unlock (connection,
2177 * Used internally to handle the semantics of dbus_server_set_new_connection_function().
2178 * If the new connection function does not ref the connection, we want to close it.
2180 * A bit of a hack, probably the new connection function should have returned a value
2181 * for whether to close, or should have had to close the connection itself if it
2184 * But, this works OK as long as the new connection function doesn't do anything
2185 * crazy like keep the connection around without ref'ing it.
2187 * We have to lock the connection across refcount check and close in case
2188 * the new connection function spawns a thread that closes and unrefs.
2189 * In that case, if the app thread
2190 * closes and unrefs first, we'll harmlessly close again; if the app thread
2191 * still has the ref, we'll close and then the app will close harmlessly.
2192 * If the app unrefs without closing, the app is broken since if the
2193 * app refs from the new connection function it is supposed to also close.
2195 * If we didn't atomically check the refcount and close with the lock held
2196 * though, we could screw this up.
2198 * @param connection the connection
2201 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
2203 dbus_int32_t refcount;
2205 CONNECTION_LOCK (connection);
2207 refcount = _dbus_atomic_get (&connection->refcount);
2208 /* The caller should have at least one ref */
2209 _dbus_assert (refcount >= 1);
2212 _dbus_connection_close_possibly_shared_and_unlock (connection);
2214 CONNECTION_UNLOCK (connection);
2219 * When a function that blocks has been called with a timeout, and we
2220 * run out of memory, the time to wait for memory is based on the
2221 * timeout. If the caller was willing to block a long time we wait a
2222 * relatively long time for memory, if they were only willing to block
2223 * briefly then we retry for memory at a rapid rate.
2225 * @param timeout_milliseconds the timeout requested for blocking
2228 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2230 if (timeout_milliseconds == -1)
2231 _dbus_sleep_milliseconds (1000);
2232 else if (timeout_milliseconds < 100)
2233 ; /* just busy loop */
2234 else if (timeout_milliseconds <= 1000)
2235 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2237 _dbus_sleep_milliseconds (1000);
2241 * Peek the incoming queue to see if we got reply for a specific serial
2244 _dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
2245 dbus_uint32_t client_serial)
2248 HAVE_LOCK_CHECK (connection);
2250 link = _dbus_list_get_first_link (&connection->incoming_messages);
2252 while (link != NULL)
2254 DBusMessage *reply = link->data;
2256 if (dbus_message_get_reply_serial (reply) == client_serial)
2258 _dbus_verbose ("%s reply to %d found in queue\n", _DBUS_FUNCTION_NAME, client_serial);
2261 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2267 /* This is slightly strange since we can pop a message here without
2268 * the dispatch lock.
2271 check_for_reply_unlocked (DBusConnection *connection,
2272 dbus_uint32_t client_serial)
2276 HAVE_LOCK_CHECK (connection);
2278 link = _dbus_list_get_first_link (&connection->incoming_messages);
2280 while (link != NULL)
2282 DBusMessage *reply = link->data;
2284 if (dbus_message_get_reply_serial (reply) == client_serial)
2286 _dbus_list_remove_link (&connection->incoming_messages, link);
2287 connection->n_incoming -= 1;
2290 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2297 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2299 /* We can't iterate over the hash in the normal way since we'll be
2300 * dropping the lock for each item. So we restart the
2301 * iter each time as we drain the hash table.
2304 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2306 DBusPendingCall *pending;
2309 _dbus_hash_iter_init (connection->pending_replies, &iter);
2310 _dbus_hash_iter_next (&iter);
2312 pending = _dbus_hash_iter_get_value (&iter);
2313 _dbus_pending_call_ref_unlocked (pending);
2315 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2318 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
2319 _dbus_connection_remove_timeout_unlocked (connection,
2320 _dbus_pending_call_get_timeout_unlocked (pending));
2321 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2322 _dbus_hash_iter_remove_entry (&iter);
2324 _dbus_pending_call_unref_and_unlock (pending);
2325 CONNECTION_LOCK (connection);
2327 HAVE_LOCK_CHECK (connection);
2331 complete_pending_call_and_unlock (DBusConnection *connection,
2332 DBusPendingCall *pending,
2333 DBusMessage *message)
2335 _dbus_pending_call_set_reply_unlocked (pending, message);
2336 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2337 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2339 /* Must be called unlocked since it invokes app callback */
2340 _dbus_pending_call_complete (pending);
2341 dbus_pending_call_unref (pending);
2345 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2346 DBusPendingCall *pending)
2349 DBusDispatchStatus status;
2351 reply = check_for_reply_unlocked (connection,
2352 _dbus_pending_call_get_reply_serial_unlocked (pending));
2355 _dbus_verbose ("checked for reply\n");
2357 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2359 complete_pending_call_and_unlock (connection, pending, reply);
2360 dbus_message_unref (reply);
2362 CONNECTION_LOCK (connection);
2363 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2364 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2365 dbus_pending_call_unref (pending);
2374 * Blocks until a pending call times out or gets a reply.
2376 * Does not re-enter the main loop or run filter/path-registered
2377 * callbacks. The reply to the message will not be seen by
2380 * Returns immediately if pending call already got a reply.
2382 * @todo could use performance improvements (it keeps scanning
2383 * the whole message queue for example)
2385 * @param pending the pending call we block for a reply on
2388 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2390 long start_tv_sec, start_tv_usec;
2391 long tv_sec, tv_usec;
2392 DBusDispatchStatus status;
2393 DBusConnection *connection;
2394 dbus_uint32_t client_serial;
2395 DBusTimeout *timeout;
2396 int timeout_milliseconds, elapsed_milliseconds;
2398 _dbus_assert (pending != NULL);
2400 if (dbus_pending_call_get_completed (pending))
2403 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2405 connection = _dbus_pending_call_get_connection_and_lock (pending);
2407 /* Flush message queue - note, can affect dispatch status */
2408 _dbus_connection_flush_unlocked (connection);
2410 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2412 /* note that timeout_milliseconds is limited to a smallish value
2413 * in _dbus_pending_call_new() so overflows aren't possible
2416 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
2417 _dbus_get_monotonic_time (&start_tv_sec, &start_tv_usec);
2420 timeout_milliseconds = dbus_timeout_get_interval (timeout);
2422 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec\n",
2423 timeout_milliseconds,
2425 start_tv_sec, start_tv_usec);
2429 timeout_milliseconds = -1;
2431 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block for reply serial %u\n", client_serial);
2434 /* check to see if we already got the data off the socket */
2435 /* from another blocked pending call */
2436 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2439 /* Now we wait... */
2440 /* always block at least once as we know we don't have the reply yet */
2441 _dbus_connection_do_iteration_unlocked (connection,
2443 DBUS_ITERATION_DO_READING |
2444 DBUS_ITERATION_BLOCK,
2445 timeout_milliseconds);
2449 _dbus_verbose ("top of recheck\n");
2451 HAVE_LOCK_CHECK (connection);
2453 /* queue messages and get status */
2455 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2457 /* the get_completed() is in case a dispatch() while we were blocking
2458 * got the reply instead of us.
2460 if (_dbus_pending_call_get_completed_unlocked (pending))
2462 _dbus_verbose ("Pending call completed by dispatch\n");
2463 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2464 dbus_pending_call_unref (pending);
2468 if (status == DBUS_DISPATCH_DATA_REMAINS)
2470 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2474 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
2475 elapsed_milliseconds = (tv_sec - start_tv_sec) * 1000 +
2476 (tv_usec - start_tv_usec) / 1000;
2478 if (!_dbus_connection_get_is_connected_unlocked (connection))
2480 DBusMessage *error_msg;
2482 error_msg = _dbus_generate_local_error_message (client_serial,
2483 DBUS_ERROR_DISCONNECTED,
2484 "Connection was disconnected before a reply was received");
2486 /* on OOM error_msg is set to NULL */
2487 complete_pending_call_and_unlock (connection, pending, error_msg);
2488 if (error_msg != NULL)
2489 dbus_message_unref (error_msg);
2490 dbus_pending_call_unref (pending);
2493 else if (connection->disconnect_message_link == NULL)
2494 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2495 else if (timeout == NULL)
2497 if (status == DBUS_DISPATCH_NEED_MEMORY)
2499 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2500 * we may already have a reply in the buffer and just can't process
2503 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2505 _dbus_memory_pause_based_on_timeout (timeout_milliseconds - elapsed_milliseconds);
2509 /* block again, we don't have the reply buffered yet. */
2510 _dbus_connection_do_iteration_unlocked (connection,
2512 DBUS_ITERATION_DO_READING |
2513 DBUS_ITERATION_BLOCK,
2514 timeout_milliseconds - elapsed_milliseconds);
2517 goto recheck_status;
2519 else if (tv_sec < start_tv_sec)
2520 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2521 else if (elapsed_milliseconds < timeout_milliseconds)
2523 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds - elapsed_milliseconds);
2525 if (status == DBUS_DISPATCH_NEED_MEMORY)
2527 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2528 * we may already have a reply in the buffer and just can't process
2531 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2533 _dbus_memory_pause_based_on_timeout (timeout_milliseconds - elapsed_milliseconds);
2537 /* block again, we don't have the reply buffered yet. */
2538 _dbus_connection_do_iteration_unlocked (connection,
2540 DBUS_ITERATION_DO_READING |
2541 DBUS_ITERATION_BLOCK,
2542 timeout_milliseconds - elapsed_milliseconds);
2545 goto recheck_status;
2548 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %d milliseconds and got no reply\n",
2549 elapsed_milliseconds);
2551 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
2553 /* unlock and call user code */
2554 complete_pending_call_and_unlock (connection, pending, NULL);
2556 /* update user code on dispatch status */
2557 CONNECTION_LOCK (connection);
2558 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2559 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2560 dbus_pending_call_unref (pending);
2564 * Return how many file descriptors are pending in the loader
2566 * @param connection the connection
2569 _dbus_connection_get_pending_fds_count (DBusConnection *connection)
2571 return _dbus_transport_get_pending_fds_count (connection->transport);
2575 * Register a function to be called whenever the number of pending file
2576 * descriptors in the loader change.
2578 * @param connection the connection
2579 * @param callback the callback
2582 _dbus_connection_set_pending_fds_function (DBusConnection *connection,
2583 DBusPendingFdsChangeFunction callback,
2586 _dbus_transport_set_pending_fds_function (connection->transport,
2593 * @addtogroup DBusConnection
2599 * Gets a connection to a remote address. If a connection to the given
2600 * address already exists, returns the existing connection with its
2601 * reference count incremented. Otherwise, returns a new connection
2602 * and saves the new connection for possible re-use if a future call
2603 * to dbus_connection_open() asks to connect to the same server.
2605 * Use dbus_connection_open_private() to get a dedicated connection
2606 * not shared with other callers of dbus_connection_open().
2608 * If the open fails, the function returns #NULL, and provides a
2609 * reason for the failure in the error parameter. Pass #NULL for the
2610 * error parameter if you aren't interested in the reason for
2613 * Because this connection is shared, no user of the connection
2614 * may call dbus_connection_close(). However, when you are done with the
2615 * connection you should call dbus_connection_unref().
2617 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2618 * unless you have good reason; connections are expensive enough
2619 * that it's wasteful to create lots of connections to the same
2622 * @param address the address.
2623 * @param error address where an error can be returned.
2624 * @returns new connection, or #NULL on failure.
2627 dbus_connection_open (const char *address,
2630 DBusConnection *connection;
2632 _dbus_return_val_if_fail (address != NULL, NULL);
2633 _dbus_return_val_if_error_is_set (error, NULL);
2635 connection = _dbus_connection_open_internal (address,
2643 * Opens a new, dedicated connection to a remote address. Unlike
2644 * dbus_connection_open(), always creates a new connection.
2645 * This connection will not be saved or recycled by libdbus.
2647 * If the open fails, the function returns #NULL, and provides a
2648 * reason for the failure in the error parameter. Pass #NULL for the
2649 * error parameter if you aren't interested in the reason for
2652 * When you are done with this connection, you must
2653 * dbus_connection_close() to disconnect it,
2654 * and dbus_connection_unref() to free the connection object.
2656 * (The dbus_connection_close() can be skipped if the
2657 * connection is already known to be disconnected, for example
2658 * if you are inside a handler for the Disconnected signal.)
2660 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2661 * unless you have good reason; connections are expensive enough
2662 * that it's wasteful to create lots of connections to the same
2665 * @param address the address.
2666 * @param error address where an error can be returned.
2667 * @returns new connection, or #NULL on failure.
2670 dbus_connection_open_private (const char *address,
2673 DBusConnection *connection;
2675 _dbus_return_val_if_fail (address != NULL, NULL);
2676 _dbus_return_val_if_error_is_set (error, NULL);
2678 connection = _dbus_connection_open_internal (address,
2686 * Increments the reference count of a DBusConnection.
2688 * @param connection the connection.
2689 * @returns the connection.
2692 dbus_connection_ref (DBusConnection *connection)
2694 dbus_int32_t old_refcount;
2696 _dbus_return_val_if_fail (connection != NULL, NULL);
2697 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
2698 old_refcount = _dbus_atomic_inc (&connection->refcount);
2699 _dbus_connection_trace_ref (connection, old_refcount, old_refcount + 1,
2706 free_outgoing_message (void *element,
2709 DBusMessage *message = element;
2710 DBusConnection *connection = data;
2712 _dbus_message_remove_counter (message, connection->outgoing_counter);
2713 dbus_message_unref (message);
2716 /* This is run without the mutex held, but after the last reference
2717 * to the connection has been dropped we should have no thread-related
2721 _dbus_connection_last_unref (DBusConnection *connection)
2725 _dbus_verbose ("Finalizing connection %p\n", connection);
2727 _dbus_assert (_dbus_atomic_get (&connection->refcount) == 0);
2729 /* You have to disconnect the connection before unref:ing it. Otherwise
2730 * you won't get the disconnected message.
2732 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
2733 _dbus_assert (connection->server_guid == NULL);
2735 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
2736 _dbus_object_tree_free_all_unlocked (connection->objects);
2738 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
2739 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
2740 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
2741 dbus_connection_set_windows_user_function (connection, NULL, NULL, NULL);
2743 _dbus_watch_list_free (connection->watches);
2744 connection->watches = NULL;
2746 _dbus_timeout_list_free (connection->timeouts);
2747 connection->timeouts = NULL;
2749 _dbus_data_slot_list_free (&connection->slot_list);
2751 link = _dbus_list_get_first_link (&connection->filter_list);
2752 while (link != NULL)
2754 DBusMessageFilter *filter = link->data;
2755 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
2757 filter->function = NULL;
2758 _dbus_message_filter_unref (filter); /* calls app callback */
2763 _dbus_list_clear (&connection->filter_list);
2765 /* ---- Done with stuff that invokes application callbacks */
2767 _dbus_object_tree_unref (connection->objects);
2769 _dbus_hash_table_unref (connection->pending_replies);
2770 connection->pending_replies = NULL;
2772 _dbus_list_foreach (&connection->outgoing_messages,
2773 free_outgoing_message,
2775 _dbus_list_clear (&connection->outgoing_messages);
2777 _dbus_list_foreach (&connection->incoming_messages,
2778 (DBusForeachFunction) dbus_message_unref,
2780 _dbus_list_clear (&connection->incoming_messages);
2782 _dbus_counter_unref (connection->outgoing_counter);
2784 _dbus_transport_unref (connection->transport);
2786 if (connection->disconnect_message_link)
2788 DBusMessage *message = connection->disconnect_message_link->data;
2789 dbus_message_unref (message);
2790 _dbus_list_free_link (connection->disconnect_message_link);
2793 _dbus_condvar_free_at_location (&connection->dispatch_cond);
2794 _dbus_condvar_free_at_location (&connection->io_path_cond);
2796 _dbus_cmutex_free_at_location (&connection->io_path_mutex);
2797 _dbus_cmutex_free_at_location (&connection->dispatch_mutex);
2799 _dbus_rmutex_free_at_location (&connection->slot_mutex);
2801 _dbus_rmutex_free_at_location (&connection->mutex);
2803 dbus_free (connection);
2807 * Decrements the reference count of a DBusConnection, and finalizes
2808 * it if the count reaches zero.
2810 * Note: it is a bug to drop the last reference to a connection that
2811 * is still connected.
2813 * For shared connections, libdbus will own a reference
2814 * as long as the connection is connected, so you can know that either
2815 * you don't have the last reference, or it's OK to drop the last reference.
2816 * Most connections are shared. dbus_connection_open() and dbus_bus_get()
2817 * return shared connections.
2819 * For private connections, the creator of the connection must arrange for
2820 * dbus_connection_close() to be called prior to dropping the last reference.
2821 * Private connections come from dbus_connection_open_private() or dbus_bus_get_private().
2823 * @param connection the connection.
2826 dbus_connection_unref (DBusConnection *connection)
2828 dbus_int32_t old_refcount;
2830 _dbus_return_if_fail (connection != NULL);
2831 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2833 old_refcount = _dbus_atomic_dec (&connection->refcount);
2835 _dbus_connection_trace_ref (connection, old_refcount, old_refcount - 1,
2838 if (old_refcount == 1)
2840 #ifndef DBUS_DISABLE_CHECKS
2841 if (_dbus_transport_get_is_connected (connection->transport))
2843 _dbus_warn_check_failed ("The last reference on a connection was dropped without closing the connection. This is a bug in an application. See dbus_connection_unref() documentation for details.\n%s",
2844 connection->shareable ?
2845 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" :
2846 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
2850 _dbus_connection_last_unref (connection);
2855 * Note that the transport can disconnect itself (other end drops us)
2856 * and in that case this function never runs. So this function must
2857 * not do anything more than disconnect the transport and update the
2860 * If the transport self-disconnects, then we assume someone will
2861 * dispatch the connection to cause the dispatch status update.
2864 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2866 DBusDispatchStatus status;
2868 HAVE_LOCK_CHECK (connection);
2870 _dbus_verbose ("Disconnecting %p\n", connection);
2872 /* We need to ref because update_dispatch_status_and_unlock will unref
2873 * the connection if it was shared and libdbus was the only remaining
2876 _dbus_connection_ref_unlocked (connection);
2878 _dbus_transport_disconnect (connection->transport);
2880 /* This has the side effect of queuing the disconnect message link
2881 * (unless we don't have enough memory, possibly, so don't assert it).
2882 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
2883 * should never again return the newly-disconnected connection.
2885 * However, we only unref the shared connection and exit_on_disconnect when
2886 * the disconnect message reaches the head of the message queue,
2887 * NOT when it's first queued.
2889 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2891 /* This calls out to user code */
2892 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2894 /* Could also call out to user code */
2895 dbus_connection_unref (connection);
2899 * Closes a private connection, so no further data can be sent or received.
2900 * This disconnects the transport (such as a socket) underlying the
2903 * Attempts to send messages after closing a connection are safe, but will result in
2904 * error replies generated locally in libdbus.
2906 * This function does not affect the connection's reference count. It's
2907 * safe to close a connection more than once; all calls after the
2908 * first do nothing. It's impossible to "reopen" a connection, a
2909 * new connection must be created. This function may result in a call
2910 * to the DBusDispatchStatusFunction set with
2911 * dbus_connection_set_dispatch_status_function(), as the disconnect
2912 * message it generates needs to be dispatched.
2914 * If a connection is dropped by the remote application, it will
2917 * You must close a connection prior to releasing the last reference to
2918 * the connection. If you dbus_connection_unref() for the last time
2919 * without closing the connection, the results are undefined; it
2920 * is a bug in your program and libdbus will try to print a warning.
2922 * You may not close a shared connection. Connections created with
2923 * dbus_connection_open() or dbus_bus_get() are shared.
2924 * These connections are owned by libdbus, and applications should
2925 * only unref them, never close them. Applications can know it is
2926 * safe to unref these connections because libdbus will be holding a
2927 * reference as long as the connection is open. Thus, either the
2928 * connection is closed and it is OK to drop the last reference,
2929 * or the connection is open and the app knows it does not have the
2932 * Connections created with dbus_connection_open_private() or
2933 * dbus_bus_get_private() are not kept track of or referenced by
2934 * libdbus. The creator of these connections is responsible for
2935 * calling dbus_connection_close() prior to releasing the last
2936 * reference, if the connection is not already disconnected.
2938 * @param connection the private (unshared) connection to close
2941 dbus_connection_close (DBusConnection *connection)
2943 _dbus_return_if_fail (connection != NULL);
2944 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2946 CONNECTION_LOCK (connection);
2948 #ifndef DBUS_DISABLE_CHECKS
2949 if (connection->shareable)
2951 CONNECTION_UNLOCK (connection);
2953 _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
2958 _dbus_connection_close_possibly_shared_and_unlock (connection);
2962 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2964 HAVE_LOCK_CHECK (connection);
2965 return _dbus_transport_get_is_connected (connection->transport);
2969 * Gets whether the connection is currently open. A connection may
2970 * become disconnected when the remote application closes its end, or
2971 * exits; a connection may also be disconnected with
2972 * dbus_connection_close().
2974 * There are not separate states for "closed" and "disconnected," the two
2975 * terms are synonymous. This function should really be called
2976 * get_is_open() but for historical reasons is not.
2978 * @param connection the connection.
2979 * @returns #TRUE if the connection is still alive.
2982 dbus_connection_get_is_connected (DBusConnection *connection)
2986 _dbus_return_val_if_fail (connection != NULL, FALSE);
2988 CONNECTION_LOCK (connection);
2989 res = _dbus_connection_get_is_connected_unlocked (connection);
2990 CONNECTION_UNLOCK (connection);
2996 * Gets whether the connection was authenticated. (Note that
2997 * if the connection was authenticated then disconnected,
2998 * this function still returns #TRUE)
3000 * @param connection the connection
3001 * @returns #TRUE if the connection was ever authenticated
3004 dbus_connection_get_is_authenticated (DBusConnection *connection)
3008 _dbus_return_val_if_fail (connection != NULL, FALSE);
3010 CONNECTION_LOCK (connection);
3011 res = _dbus_transport_try_to_authenticate (connection->transport);
3012 CONNECTION_UNLOCK (connection);
3018 * Gets whether the connection is not authenticated as a specific
3019 * user. If the connection is not authenticated, this function
3020 * returns #TRUE, and if it is authenticated but as an anonymous user,
3021 * it returns #TRUE. If it is authenticated as a specific user, then
3022 * this returns #FALSE. (Note that if the connection was authenticated
3023 * as anonymous then disconnected, this function still returns #TRUE.)
3025 * If the connection is not anonymous, you can use
3026 * dbus_connection_get_unix_user() and
3027 * dbus_connection_get_windows_user() to see who it's authorized as.
3029 * If you want to prevent non-anonymous authorization, use
3030 * dbus_server_set_auth_mechanisms() to remove the mechanisms that
3031 * allow proving user identity (i.e. only allow the ANONYMOUS
3034 * @param connection the connection
3035 * @returns #TRUE if not authenticated or authenticated as anonymous
3038 dbus_connection_get_is_anonymous (DBusConnection *connection)
3042 _dbus_return_val_if_fail (connection != NULL, FALSE);
3044 CONNECTION_LOCK (connection);
3045 res = _dbus_transport_get_is_anonymous (connection->transport);
3046 CONNECTION_UNLOCK (connection);
3052 * Gets the ID of the server address we are authenticated to, if this
3053 * connection is on the client side. If the connection is on the
3054 * server side, this will always return #NULL - use dbus_server_get_id()
3055 * to get the ID of your own server, if you are the server side.
3057 * If a client-side connection is not authenticated yet, the ID may be
3058 * available if it was included in the server address, but may not be
3059 * available. The only way to be sure the server ID is available
3060 * is to wait for authentication to complete.
3062 * In general, each mode of connecting to a given server will have
3063 * its own ID. So for example, if the session bus daemon is listening
3064 * on UNIX domain sockets and on TCP, then each of those modalities
3065 * will have its own server ID.
3067 * If you want an ID that identifies an entire session bus, look at
3068 * dbus_bus_get_id() instead (which is just a convenience wrapper
3069 * around the org.freedesktop.DBus.GetId method invoked on the bus).
3071 * You can also get a machine ID; see dbus_get_local_machine_id() to
3072 * get the machine you are on. There isn't a convenience wrapper, but
3073 * you can invoke org.freedesktop.DBus.Peer.GetMachineId on any peer
3074 * to get the machine ID on the other end.
3076 * The D-Bus specification describes the server ID and other IDs in a
3079 * @param connection the connection
3080 * @returns the server ID or #NULL if no memory or the connection is server-side
3083 dbus_connection_get_server_id (DBusConnection *connection)
3087 _dbus_return_val_if_fail (connection != NULL, NULL);
3089 CONNECTION_LOCK (connection);
3090 id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
3091 CONNECTION_UNLOCK (connection);
3097 * Tests whether a certain type can be send via the connection. This
3098 * will always return TRUE for all types, with the exception of
3099 * DBUS_TYPE_UNIX_FD. The function will return TRUE for
3100 * DBUS_TYPE_UNIX_FD only on systems that know Unix file descriptors
3101 * and can send them via the chosen transport and when the remote side
3104 * This function can be used to do runtime checking for types that
3105 * might be unknown to the specific D-Bus client implementation
3106 * version, i.e. it will return FALSE for all types this
3107 * implementation does not know, including invalid or reserved types.
3109 * @param connection the connection
3110 * @param type the type to check
3111 * @returns TRUE if the type may be send via the connection
3114 dbus_connection_can_send_type(DBusConnection *connection,
3117 _dbus_return_val_if_fail (connection != NULL, FALSE);
3119 if (!dbus_type_is_valid (type))
3122 if (type != DBUS_TYPE_UNIX_FD)
3125 #ifdef HAVE_UNIX_FD_PASSING
3129 CONNECTION_LOCK(connection);
3130 b = _dbus_transport_can_pass_unix_fd(connection->transport);
3131 CONNECTION_UNLOCK(connection);
3141 * Set whether _exit() should be called when the connection receives a
3142 * disconnect signal. The call to _exit() comes after any handlers for
3143 * the disconnect signal run; handlers can cancel the exit by calling
3146 * By default, exit_on_disconnect is #FALSE; but for message bus
3147 * connections returned from dbus_bus_get() it will be toggled on
3150 * @param connection the connection
3151 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
3154 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
3155 dbus_bool_t exit_on_disconnect)
3157 _dbus_return_if_fail (connection != NULL);
3159 CONNECTION_LOCK (connection);
3160 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
3161 CONNECTION_UNLOCK (connection);
3165 * Preallocates resources needed to send a message, allowing the message
3166 * to be sent without the possibility of memory allocation failure.
3167 * Allows apps to create a future guarantee that they can send
3168 * a message regardless of memory shortages.
3170 * @param connection the connection we're preallocating for.
3171 * @returns the preallocated resources, or #NULL
3173 DBusPreallocatedSend*
3174 dbus_connection_preallocate_send (DBusConnection *connection)
3176 DBusPreallocatedSend *preallocated;
3178 _dbus_return_val_if_fail (connection != NULL, NULL);
3180 CONNECTION_LOCK (connection);
3183 _dbus_connection_preallocate_send_unlocked (connection);
3185 CONNECTION_UNLOCK (connection);
3187 return preallocated;
3191 * Frees preallocated message-sending resources from
3192 * dbus_connection_preallocate_send(). Should only
3193 * be called if the preallocated resources are not used
3194 * to send a message.
3196 * @param connection the connection
3197 * @param preallocated the resources
3200 dbus_connection_free_preallocated_send (DBusConnection *connection,
3201 DBusPreallocatedSend *preallocated)
3203 _dbus_return_if_fail (connection != NULL);
3204 _dbus_return_if_fail (preallocated != NULL);
3205 _dbus_return_if_fail (connection == preallocated->connection);
3207 _dbus_list_free_link (preallocated->queue_link);
3208 _dbus_counter_unref (preallocated->counter_link->data);
3209 _dbus_list_free_link (preallocated->counter_link);
3210 dbus_free (preallocated);
3214 * Sends a message using preallocated resources. This function cannot fail.
3215 * It works identically to dbus_connection_send() in other respects.
3216 * Preallocated resources comes from dbus_connection_preallocate_send().
3217 * This function "consumes" the preallocated resources, they need not
3218 * be freed separately.
3220 * @param connection the connection
3221 * @param preallocated the preallocated resources
3222 * @param message the message to send
3223 * @param client_serial return location for client serial assigned to the message
3226 dbus_connection_send_preallocated (DBusConnection *connection,
3227 DBusPreallocatedSend *preallocated,
3228 DBusMessage *message,
3229 dbus_uint32_t *client_serial)
3231 _dbus_return_if_fail (connection != NULL);
3232 _dbus_return_if_fail (preallocated != NULL);
3233 _dbus_return_if_fail (message != NULL);
3234 _dbus_return_if_fail (preallocated->connection == connection);
3235 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
3236 dbus_message_get_member (message) != NULL);
3237 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
3238 (dbus_message_get_interface (message) != NULL &&
3239 dbus_message_get_member (message) != NULL));
3241 CONNECTION_LOCK (connection);
3243 #ifdef HAVE_UNIX_FD_PASSING
3245 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3246 message->n_unix_fds > 0)
3248 /* Refuse to send fds on a connection that cannot handle
3249 them. Unfortunately we cannot return a proper error here, so
3250 the best we can is just return. */
3251 CONNECTION_UNLOCK (connection);
3257 _dbus_connection_send_preallocated_and_unlock (connection,
3259 message, client_serial);
3263 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
3264 DBusMessage *message,
3265 dbus_uint32_t *client_serial)
3267 DBusPreallocatedSend *preallocated;
3269 _dbus_assert (connection != NULL);
3270 _dbus_assert (message != NULL);
3272 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3273 if (preallocated == NULL)
3276 _dbus_connection_send_preallocated_unlocked_no_update (connection,
3284 * Adds a message to the outgoing message queue. Does not block to
3285 * write the message to the network; that happens asynchronously. To
3286 * force the message to be written, call dbus_connection_flush() however
3287 * it is not necessary to call dbus_connection_flush() by hand; the
3288 * message will be sent the next time the main loop is run.
3289 * dbus_connection_flush() should only be used, for example, if
3290 * the application was expected to exit before running the main loop.
3292 * Because this only queues the message, the only reason it can
3293 * fail is lack of memory. Even if the connection is disconnected,
3294 * no error will be returned. If the function fails due to lack of memory,
3295 * it returns #FALSE. The function will never fail for other reasons; even
3296 * if the connection is disconnected, you can queue an outgoing message,
3297 * though obviously it won't be sent.
3299 * The message serial is used by the remote application to send a
3300 * reply; see dbus_message_get_serial() or the D-Bus specification.
3302 * dbus_message_unref() can be called as soon as this method returns
3303 * as the message queue will hold its own ref until the message is sent.
3305 * @param connection the connection.
3306 * @param message the message to write.
3307 * @param serial return location for message serial, or #NULL if you don't care
3308 * @returns #TRUE on success.
3311 dbus_connection_send (DBusConnection *connection,
3312 DBusMessage *message,
3313 dbus_uint32_t *serial)
3315 _dbus_return_val_if_fail (connection != NULL, FALSE);
3316 _dbus_return_val_if_fail (message != NULL, FALSE);
3318 CONNECTION_LOCK (connection);
3320 #ifdef HAVE_UNIX_FD_PASSING
3322 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3323 message->n_unix_fds > 0)
3325 /* Refuse to send fds on a connection that cannot handle
3326 them. Unfortunately we cannot return a proper error here, so
3327 the best we can is just return. */
3328 CONNECTION_UNLOCK (connection);
3334 return _dbus_connection_send_and_unlock (connection,
3340 reply_handler_timeout (void *data)
3342 DBusConnection *connection;
3343 DBusDispatchStatus status;
3344 DBusPendingCall *pending = data;
3346 connection = _dbus_pending_call_get_connection_and_lock (pending);
3347 _dbus_connection_ref_unlocked (connection);
3349 _dbus_pending_call_queue_timeout_error_unlocked (pending,
3351 _dbus_connection_remove_timeout_unlocked (connection,
3352 _dbus_pending_call_get_timeout_unlocked (pending));
3353 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
3355 _dbus_verbose ("middle\n");
3356 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3358 /* Unlocks, and calls out to user code */
3359 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3360 dbus_connection_unref (connection);
3366 * Queues a message to send, as with dbus_connection_send(),
3367 * but also returns a #DBusPendingCall used to receive a reply to the
3368 * message. If no reply is received in the given timeout_milliseconds,
3369 * this function expires the pending reply and generates a synthetic
3370 * error reply (generated in-process, not by the remote application)
3371 * indicating that a timeout occurred.
3373 * A #DBusPendingCall will see a reply message before any filters or
3374 * registered object path handlers. See dbus_connection_dispatch() for
3375 * details on when handlers are run.
3377 * A #DBusPendingCall will always see exactly one reply message,
3378 * unless it's cancelled with dbus_pending_call_cancel().
3380 * If #NULL is passed for the pending_return, the #DBusPendingCall
3381 * will still be generated internally, and used to track
3382 * the message reply timeout. This means a timeout error will
3383 * occur if no reply arrives, unlike with dbus_connection_send().
3385 * If -1 is passed for the timeout, a sane default timeout is used. -1
3386 * is typically the best value for the timeout for this reason, unless
3387 * you want a very short or very long timeout. If #DBUS_TIMEOUT_INFINITE is
3388 * passed for the timeout, no timeout will be set and the call will block
3391 * @warning if the connection is disconnected or you try to send Unix
3392 * file descriptors on a connection that does not support them, the
3393 * #DBusPendingCall will be set to #NULL, so be careful with this.
3395 * @param connection the connection
3396 * @param message the message to send
3397 * @param pending_return return location for a #DBusPendingCall
3398 * object, or #NULL if connection is disconnected or when you try to
3399 * send Unix file descriptors on a connection that does not support
3401 * @param timeout_milliseconds timeout in milliseconds, -1 (or
3402 * #DBUS_TIMEOUT_USE_DEFAULT) for default or #DBUS_TIMEOUT_INFINITE for no
3404 * @returns #FALSE if no memory, #TRUE otherwise.
3408 dbus_connection_send_with_reply (DBusConnection *connection,
3409 DBusMessage *message,
3410 DBusPendingCall **pending_return,
3411 int timeout_milliseconds)
3413 DBusPendingCall *pending;
3414 dbus_int32_t serial = -1;
3415 DBusDispatchStatus status;
3417 _dbus_return_val_if_fail (connection != NULL, FALSE);
3418 _dbus_return_val_if_fail (message != NULL, FALSE);
3419 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3422 *pending_return = NULL;
3424 CONNECTION_LOCK (connection);
3426 #ifdef HAVE_UNIX_FD_PASSING
3428 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3429 message->n_unix_fds > 0)
3431 /* Refuse to send fds on a connection that cannot handle
3432 them. Unfortunately we cannot return a proper error here, so
3433 the best we can do is return TRUE but leave *pending_return
3435 CONNECTION_UNLOCK (connection);
3441 if (!_dbus_connection_get_is_connected_unlocked (connection))
3443 CONNECTION_UNLOCK (connection);
3448 _dbus_message_set_timeout_ms(message, timeout_milliseconds);
3449 pending = _dbus_pending_call_new_unlocked (connection,
3450 timeout_milliseconds,
3451 reply_handler_timeout);
3453 if (pending == NULL)
3455 CONNECTION_UNLOCK (connection);
3459 /* Assign a serial to the message */
3460 serial = dbus_message_get_serial (message);
3463 serial = _dbus_connection_get_next_client_serial (connection);
3464 dbus_message_set_serial (message, serial);
3467 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
3470 /* Insert the serial in the pending replies hash;
3471 * hash takes a refcount on DBusPendingCall.
3472 * Also, add the timeout.
3474 if (!_dbus_connection_attach_pending_call_unlocked (connection,
3478 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
3480 _dbus_connection_detach_pending_call_and_unlock (connection,
3482 goto error_unlocked;
3486 *pending_return = pending; /* hand off refcount */
3489 _dbus_connection_detach_pending_call_unlocked (connection, pending);
3490 /* we still have a ref to the pending call in this case, we unref
3491 * after unlocking, below
3495 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3497 /* this calls out to user code */
3498 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3500 if (pending_return == NULL)
3501 dbus_pending_call_unref (pending);
3506 CONNECTION_UNLOCK (connection);
3508 dbus_pending_call_unref (pending);
3513 * Sends a message and blocks a certain time period while waiting for
3514 * a reply. This function does not reenter the main loop,
3515 * i.e. messages other than the reply are queued up but not
3516 * processed. This function is used to invoke method calls on a
3519 * If a normal reply is received, it is returned, and removed from the
3520 * incoming message queue. If it is not received, #NULL is returned
3521 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
3522 * received, it is converted to a #DBusError and returned as an error,
3523 * then the reply message is deleted and #NULL is returned. If
3524 * something else goes wrong, result is set to whatever is
3525 * appropriate, such as #DBUS_ERROR_NO_MEMORY or
3526 * #DBUS_ERROR_DISCONNECTED.
3528 * @warning While this function blocks the calling thread will not be
3529 * processing the incoming message queue. This means you can end up
3530 * deadlocked if the application you're talking to needs you to reply
3531 * to a method. To solve this, either avoid the situation, block in a
3532 * separate thread from the main connection-dispatching thread, or use
3533 * dbus_pending_call_set_notify() to avoid blocking.
3535 * @param connection the connection
3536 * @param message the message to send
3537 * @param timeout_milliseconds timeout in milliseconds, -1 (or
3538 * #DBUS_TIMEOUT_USE_DEFAULT) for default or #DBUS_TIMEOUT_INFINITE for no
3540 * @param error return location for error message
3541 * @returns the message that is the reply or #NULL with an error code if the
3545 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
3546 DBusMessage *message,
3547 int timeout_milliseconds,
3551 DBusPendingCall *pending;
3553 _dbus_return_val_if_fail (connection != NULL, NULL);
3554 _dbus_return_val_if_fail (message != NULL, NULL);
3555 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3556 _dbus_return_val_if_error_is_set (error, NULL);
3558 if (_dbus_transport_can_send_sync_call (connection->transport))
3560 dbus_int32_t serial;
3563 serial = dbus_message_get_serial (message);
3566 serial = _dbus_connection_get_next_client_serial (connection);
3567 dbus_message_set_serial (message, serial);
3570 reply = _dbus_transport_send_sync_call (connection->transport, message);
3574 #ifdef HAVE_UNIX_FD_PASSING
3576 CONNECTION_LOCK (connection);
3577 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3578 message->n_unix_fds > 0)
3580 CONNECTION_UNLOCK (connection);
3581 dbus_set_error(error, DBUS_ERROR_FAILED, "Cannot send file descriptors on this connection.");
3584 CONNECTION_UNLOCK (connection);
3588 if (!dbus_connection_send_with_reply (connection, message,
3589 &pending, timeout_milliseconds))
3591 _DBUS_SET_OOM (error);
3595 if (pending == NULL)
3597 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
3601 dbus_pending_call_block (pending);
3603 reply = dbus_pending_call_steal_reply (pending);
3604 dbus_pending_call_unref (pending);
3606 /* call_complete_and_unlock() called from pending_call_block() should
3607 * always fill this in.
3611 _dbus_assert (reply != NULL);
3613 if (dbus_set_error_from_message (error, reply))
3615 dbus_message_unref (reply);
3623 * Blocks until the outgoing message queue is empty.
3624 * Assumes connection lock already held.
3626 * If you call this, you MUST call update_dispatch_status afterword...
3628 * @param connection the connection.
3630 static DBusDispatchStatus
3631 _dbus_connection_flush_unlocked (DBusConnection *connection)
3633 /* We have to specify DBUS_ITERATION_DO_READING here because
3634 * otherwise we could have two apps deadlock if they are both doing
3635 * a flush(), and the kernel buffers fill up. This could change the
3638 DBusDispatchStatus status;
3640 HAVE_LOCK_CHECK (connection);
3642 while (connection->n_outgoing > 0 &&
3643 _dbus_connection_get_is_connected_unlocked (connection))
3645 _dbus_verbose ("doing iteration in\n");
3646 HAVE_LOCK_CHECK (connection);
3647 _dbus_connection_do_iteration_unlocked (connection,
3649 DBUS_ITERATION_DO_READING |
3650 DBUS_ITERATION_DO_WRITING |
3651 DBUS_ITERATION_BLOCK,
3655 HAVE_LOCK_CHECK (connection);
3656 _dbus_verbose ("middle\n");
3657 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3659 HAVE_LOCK_CHECK (connection);
3664 * Blocks until the outgoing message queue is empty.
3666 * @param connection the connection.
3669 dbus_connection_flush (DBusConnection *connection)
3671 /* We have to specify DBUS_ITERATION_DO_READING here because
3672 * otherwise we could have two apps deadlock if they are both doing
3673 * a flush(), and the kernel buffers fill up. This could change the
3676 DBusDispatchStatus status;
3678 _dbus_return_if_fail (connection != NULL);
3680 CONNECTION_LOCK (connection);
3682 status = _dbus_connection_flush_unlocked (connection);
3684 HAVE_LOCK_CHECK (connection);
3685 /* Unlocks and calls out to user code */
3686 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3688 _dbus_verbose ("end\n");
3692 * This function implements dbus_connection_read_write_dispatch() and
3693 * dbus_connection_read_write() (they pass a different value for the
3694 * dispatch parameter).
3696 * @param connection the connection
3697 * @param timeout_milliseconds max time to block or -1 for infinite
3698 * @param dispatch dispatch new messages or leave them on the incoming queue
3699 * @returns #TRUE if the disconnect message has not been processed
3702 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3703 int timeout_milliseconds,
3704 dbus_bool_t dispatch)
3706 DBusDispatchStatus dstatus;
3707 dbus_bool_t progress_possible;
3709 /* Need to grab a ref here in case we're a private connection and
3710 * the user drops the last ref in a handler we call; see bug
3711 * https://bugs.freedesktop.org/show_bug.cgi?id=15635
3713 dbus_connection_ref (connection);
3714 dstatus = dbus_connection_get_dispatch_status (connection);
3716 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3718 _dbus_verbose ("doing dispatch\n");
3719 dbus_connection_dispatch (connection);
3720 CONNECTION_LOCK (connection);
3722 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3724 _dbus_verbose ("pausing for memory\n");
3725 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3726 CONNECTION_LOCK (connection);
3730 CONNECTION_LOCK (connection);
3731 if (_dbus_connection_get_is_connected_unlocked (connection))
3733 _dbus_verbose ("doing iteration\n");
3734 _dbus_connection_do_iteration_unlocked (connection,
3736 DBUS_ITERATION_DO_READING |
3737 DBUS_ITERATION_DO_WRITING |
3738 DBUS_ITERATION_BLOCK,
3739 timeout_milliseconds);
3743 HAVE_LOCK_CHECK (connection);
3744 /* If we can dispatch, we can make progress until the Disconnected message
3745 * has been processed; if we can only read/write, we can make progress
3746 * as long as the transport is open.
3749 progress_possible = connection->n_incoming != 0 ||
3750 connection->disconnect_message_link != NULL;
3752 progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
3754 CONNECTION_UNLOCK (connection);
3756 dbus_connection_unref (connection);
3758 return progress_possible; /* TRUE if we can make more progress */
3763 * This function is intended for use with applications that don't want
3764 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3765 * example usage would be:
3768 * while (dbus_connection_read_write_dispatch (connection, -1))
3769 * ; // empty loop body
3772 * In this usage you would normally have set up a filter function to look
3773 * at each message as it is dispatched. The loop terminates when the last
3774 * message from the connection (the disconnected signal) is processed.
3776 * If there are messages to dispatch, this function will
3777 * dbus_connection_dispatch() once, and return. If there are no
3778 * messages to dispatch, this function will block until it can read or
3779 * write, then read or write, then return.
3781 * The way to think of this function is that it either makes some sort
3782 * of progress, or it blocks. Note that, while it is blocked on I/O, it
3783 * cannot be interrupted (even by other threads), which makes this function
3784 * unsuitable for applications that do more than just react to received
3787 * The return value indicates whether the disconnect message has been
3788 * processed, NOT whether the connection is connected. This is
3789 * important because even after disconnecting, you want to process any
3790 * messages you received prior to the disconnect.
3792 * @param connection the connection
3793 * @param timeout_milliseconds max time to block or -1 for infinite
3794 * @returns #TRUE if the disconnect message has not been processed
3797 dbus_connection_read_write_dispatch (DBusConnection *connection,
3798 int timeout_milliseconds)
3800 _dbus_return_val_if_fail (connection != NULL, FALSE);
3801 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3802 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3806 * This function is intended for use with applications that don't want to
3807 * write a main loop and deal with #DBusWatch and #DBusTimeout. See also
3808 * dbus_connection_read_write_dispatch().
3810 * As long as the connection is open, this function will block until it can
3811 * read or write, then read or write, then return #TRUE.
3813 * If the connection is closed, the function returns #FALSE.
3815 * The return value indicates whether reading or writing is still
3816 * possible, i.e. whether the connection is connected.
3818 * Note that even after disconnection, messages may remain in the
3819 * incoming queue that need to be
3820 * processed. dbus_connection_read_write_dispatch() dispatches
3821 * incoming messages for you; with dbus_connection_read_write() you
3822 * have to arrange to drain the incoming queue yourself.
3824 * @param connection the connection
3825 * @param timeout_milliseconds max time to block or -1 for infinite
3826 * @returns #TRUE if still connected
3829 dbus_connection_read_write (DBusConnection *connection,
3830 int timeout_milliseconds)
3832 _dbus_return_val_if_fail (connection != NULL, FALSE);
3833 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3834 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3837 /* We need to call this anytime we pop the head of the queue, and then
3838 * update_dispatch_status_and_unlock needs to be called afterward
3839 * which will "process" the disconnected message and set
3840 * disconnected_message_processed.
3843 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3844 DBusMessage *head_of_queue)
3846 HAVE_LOCK_CHECK (connection);
3848 /* checking that the link is NULL is an optimization to avoid the is_signal call */
3849 if (connection->disconnect_message_link == NULL &&
3850 dbus_message_is_signal (head_of_queue,
3851 DBUS_INTERFACE_LOCAL,
3854 connection->disconnected_message_arrived = TRUE;
3859 * Returns the first-received message from the incoming message queue,
3860 * leaving it in the queue. If the queue is empty, returns #NULL.
3862 * The caller does not own a reference to the returned message, and
3863 * must either return it using dbus_connection_return_message() or
3864 * keep it after calling dbus_connection_steal_borrowed_message(). No
3865 * one can get at the message while its borrowed, so return it as
3866 * quickly as possible and don't keep a reference to it after
3867 * returning it. If you need to keep the message, make a copy of it.
3869 * dbus_connection_dispatch() will block if called while a borrowed
3870 * message is outstanding; only one piece of code can be playing with
3871 * the incoming queue at a time. This function will block if called
3872 * during a dbus_connection_dispatch().
3874 * @param connection the connection.
3875 * @returns next message in the incoming queue.
3878 dbus_connection_borrow_message (DBusConnection *connection)
3880 DBusDispatchStatus status;
3881 DBusMessage *message;
3883 _dbus_return_val_if_fail (connection != NULL, NULL);
3885 _dbus_verbose ("start\n");
3887 /* this is called for the side effect that it queues
3888 * up any messages from the transport
3890 status = dbus_connection_get_dispatch_status (connection);
3891 if (status != DBUS_DISPATCH_DATA_REMAINS)
3894 CONNECTION_LOCK (connection);
3896 _dbus_connection_acquire_dispatch (connection);
3898 /* While a message is outstanding, the dispatch lock is held */
3899 _dbus_assert (connection->message_borrowed == NULL);
3901 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3903 message = connection->message_borrowed;
3905 check_disconnected_message_arrived_unlocked (connection, message);
3907 /* Note that we KEEP the dispatch lock until the message is returned */
3908 if (message == NULL)
3909 _dbus_connection_release_dispatch (connection);
3911 CONNECTION_UNLOCK (connection);
3913 _dbus_message_trace_ref (message, -1, -1, "dbus_connection_borrow_message");
3915 /* We don't update dispatch status until it's returned or stolen */
3921 * Used to return a message after peeking at it using
3922 * dbus_connection_borrow_message(). Only called if
3923 * message from dbus_connection_borrow_message() was non-#NULL.
3925 * @param connection the connection
3926 * @param message the message from dbus_connection_borrow_message()
3929 dbus_connection_return_message (DBusConnection *connection,
3930 DBusMessage *message)
3932 DBusDispatchStatus status;
3934 _dbus_return_if_fail (connection != NULL);
3935 _dbus_return_if_fail (message != NULL);
3936 _dbus_return_if_fail (message == connection->message_borrowed);
3937 _dbus_return_if_fail (connection->dispatch_acquired);
3939 CONNECTION_LOCK (connection);
3941 _dbus_assert (message == connection->message_borrowed);
3943 connection->message_borrowed = NULL;
3945 _dbus_connection_release_dispatch (connection);
3947 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3948 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3950 _dbus_message_trace_ref (message, -1, -1, "dbus_connection_return_message");
3954 * Used to keep a message after peeking at it using
3955 * dbus_connection_borrow_message(). Before using this function, see
3956 * the caveats/warnings in the documentation for
3957 * dbus_connection_pop_message().
3959 * @param connection the connection
3960 * @param message the message from dbus_connection_borrow_message()
3963 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3964 DBusMessage *message)
3966 DBusMessage *pop_message;
3967 DBusDispatchStatus status;
3969 _dbus_return_if_fail (connection != NULL);
3970 _dbus_return_if_fail (message != NULL);
3971 _dbus_return_if_fail (message == connection->message_borrowed);
3972 _dbus_return_if_fail (connection->dispatch_acquired);
3974 CONNECTION_LOCK (connection);
3976 _dbus_assert (message == connection->message_borrowed);
3978 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3979 _dbus_assert (message == pop_message);
3980 (void) pop_message; /* unused unless asserting */
3982 connection->n_incoming -= 1;
3984 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3985 message, connection->n_incoming);
3987 connection->message_borrowed = NULL;
3989 _dbus_connection_release_dispatch (connection);
3991 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3992 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3993 _dbus_message_trace_ref (message, -1, -1,
3994 "dbus_connection_steal_borrowed_message");
3997 /* See dbus_connection_pop_message, but requires the caller to own
3998 * the lock before calling. May drop the lock while running.
4001 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
4003 HAVE_LOCK_CHECK (connection);
4005 _dbus_assert (connection->message_borrowed == NULL);
4007 if (connection->n_incoming > 0)
4011 link = _dbus_list_pop_first_link (&connection->incoming_messages);
4012 connection->n_incoming -= 1;
4014 _dbus_verbose ("Message %p (%s %s %s %s sig:'%s' serial:%u) removed from incoming queue %p, %d incoming\n",
4016 dbus_message_type_to_string (dbus_message_get_type (link->data)),
4017 dbus_message_get_path (link->data) ?
4018 dbus_message_get_path (link->data) :
4020 dbus_message_get_interface (link->data) ?
4021 dbus_message_get_interface (link->data) :
4023 dbus_message_get_member (link->data) ?
4024 dbus_message_get_member (link->data) :
4026 dbus_message_get_signature (link->data),
4027 dbus_message_get_serial (link->data),
4028 connection, connection->n_incoming);
4030 _dbus_message_trace_ref (link->data, -1, -1,
4031 "_dbus_connection_pop_message_link_unlocked");
4033 check_disconnected_message_arrived_unlocked (connection, link->data);
4041 /* See dbus_connection_pop_message, but requires the caller to own
4042 * the lock before calling. May drop the lock while running.
4045 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
4049 HAVE_LOCK_CHECK (connection);
4051 link = _dbus_connection_pop_message_link_unlocked (connection);
4055 DBusMessage *message;
4057 message = link->data;
4059 _dbus_list_free_link (link);
4068 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
4069 DBusList *message_link)
4071 HAVE_LOCK_CHECK (connection);
4073 _dbus_assert (message_link != NULL);
4074 /* You can't borrow a message while a link is outstanding */
4075 _dbus_assert (connection->message_borrowed == NULL);
4076 /* We had to have the dispatch lock across the pop/putback */
4077 _dbus_assert (connection->dispatch_acquired);
4079 _dbus_list_prepend_link (&connection->incoming_messages,
4081 connection->n_incoming += 1;
4083 _dbus_verbose ("Message %p (%s %s %s '%s') put back into queue %p, %d incoming\n",
4085 dbus_message_type_to_string (dbus_message_get_type (message_link->data)),
4086 dbus_message_get_interface (message_link->data) ?
4087 dbus_message_get_interface (message_link->data) :
4089 dbus_message_get_member (message_link->data) ?
4090 dbus_message_get_member (message_link->data) :
4092 dbus_message_get_signature (message_link->data),
4093 connection, connection->n_incoming);
4095 _dbus_message_trace_ref (message_link->data, -1, -1,
4096 "_dbus_connection_putback_message_link_unlocked");
4100 _dbus_connection_putback_message (DBusConnection *connection,
4101 DBusMessage *after_message,
4102 DBusMessage *message,
4105 DBusDispatchStatus status;
4106 DBusList *message_link = _dbus_list_alloc_link (message);
4107 DBusList *after_link;
4108 if (message_link == NULL)
4110 _DBUS_SET_OOM (error);
4113 dbus_message_ref (message);
4115 CONNECTION_LOCK (connection);
4116 _dbus_connection_acquire_dispatch (connection);
4117 HAVE_LOCK_CHECK (connection);
4119 after_link = _dbus_list_find_first(&connection->incoming_messages, after_message);
4120 _dbus_list_insert_after_link (&connection->incoming_messages, after_link, message_link);
4121 connection->n_incoming += 1;
4123 _dbus_verbose ("Message %p (%s %s %s '%s') put back into queue %p, %d incoming\n",
4125 dbus_message_type_to_string (dbus_message_get_type (message_link->data)),
4126 dbus_message_get_interface (message_link->data) ?
4127 dbus_message_get_interface (message_link->data) :
4129 dbus_message_get_member (message_link->data) ?
4130 dbus_message_get_member (message_link->data) :
4132 dbus_message_get_signature (message_link->data),
4133 connection, connection->n_incoming);
4135 _dbus_message_trace_ref (message_link->data, -1, -1,
4136 "_dbus_connection_putback_message");
4138 _dbus_connection_release_dispatch (connection);
4140 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4141 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4147 _dbus_connection_remove_message (DBusConnection *connection,
4148 DBusMessage *message)
4150 DBusDispatchStatus status;
4151 dbus_bool_t removed;
4153 CONNECTION_LOCK (connection);
4154 _dbus_connection_acquire_dispatch (connection);
4155 HAVE_LOCK_CHECK (connection);
4157 removed = _dbus_list_remove(&connection->incoming_messages, message);
4161 connection->n_incoming -= 1;
4162 dbus_message_unref(message);
4163 _dbus_verbose ("Message %p removed from incoming queue\n", message);
4166 _dbus_verbose ("Message %p not found in the incoming queue\n", message);
4168 _dbus_connection_release_dispatch (connection);
4170 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4171 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4176 * Returns the first-received message from the incoming message queue,
4177 * removing it from the queue. The caller owns a reference to the
4178 * returned message. If the queue is empty, returns #NULL.
4180 * This function bypasses any message handlers that are registered,
4181 * and so using it is usually wrong. Instead, let the main loop invoke
4182 * dbus_connection_dispatch(). Popping messages manually is only
4183 * useful in very simple programs that don't share a #DBusConnection
4184 * with any libraries or other modules.
4186 * There is a lock that covers all ways of accessing the incoming message
4187 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
4188 * dbus_connection_borrow_message(), etc. will all block while one of the others
4189 * in the group is running.
4191 * @param connection the connection.
4192 * @returns next message in the incoming queue.
4195 dbus_connection_pop_message (DBusConnection *connection)
4197 DBusMessage *message;
4198 DBusDispatchStatus status;
4200 _dbus_verbose ("start\n");
4202 /* this is called for the side effect that it queues
4203 * up any messages from the transport
4205 status = dbus_connection_get_dispatch_status (connection);
4206 if (status != DBUS_DISPATCH_DATA_REMAINS)
4209 CONNECTION_LOCK (connection);
4210 _dbus_connection_acquire_dispatch (connection);
4211 HAVE_LOCK_CHECK (connection);
4213 message = _dbus_connection_pop_message_unlocked (connection);
4215 _dbus_verbose ("Returning popped message %p\n", message);
4217 _dbus_connection_release_dispatch (connection);
4219 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4220 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4226 * Acquire the dispatcher. This is a separate lock so the main
4227 * connection lock can be dropped to call out to application dispatch
4230 * @param connection the connection.
4233 _dbus_connection_acquire_dispatch (DBusConnection *connection)
4235 HAVE_LOCK_CHECK (connection);
4237 _dbus_connection_ref_unlocked (connection);
4238 CONNECTION_UNLOCK (connection);
4240 _dbus_verbose ("locking dispatch_mutex\n");
4241 _dbus_cmutex_lock (connection->dispatch_mutex);
4243 while (connection->dispatch_acquired)
4245 _dbus_verbose ("waiting for dispatch to be acquirable\n");
4246 _dbus_condvar_wait (connection->dispatch_cond,
4247 connection->dispatch_mutex);
4250 _dbus_assert (!connection->dispatch_acquired);
4252 connection->dispatch_acquired = TRUE;
4254 _dbus_verbose ("unlocking dispatch_mutex\n");
4255 _dbus_cmutex_unlock (connection->dispatch_mutex);
4257 CONNECTION_LOCK (connection);
4258 _dbus_connection_unref_unlocked (connection);
4262 * Release the dispatcher when you're done with it. Only call
4263 * after you've acquired the dispatcher. Wakes up at most one
4264 * thread currently waiting to acquire the dispatcher.
4266 * @param connection the connection.
4269 _dbus_connection_release_dispatch (DBusConnection *connection)
4271 HAVE_LOCK_CHECK (connection);
4273 _dbus_verbose ("locking dispatch_mutex\n");
4274 _dbus_cmutex_lock (connection->dispatch_mutex);
4276 _dbus_assert (connection->dispatch_acquired);
4278 connection->dispatch_acquired = FALSE;
4279 _dbus_condvar_wake_one (connection->dispatch_cond);
4281 _dbus_verbose ("unlocking dispatch_mutex\n");
4282 _dbus_cmutex_unlock (connection->dispatch_mutex);
4286 _dbus_connection_failed_pop (DBusConnection *connection,
4287 DBusList *message_link)
4289 _dbus_list_prepend_link (&connection->incoming_messages,
4291 connection->n_incoming += 1;
4294 /* Note this may be called multiple times since we don't track whether we already did it */
4296 notify_disconnected_unlocked (DBusConnection *connection)
4298 HAVE_LOCK_CHECK (connection);
4300 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
4301 * connection from dbus_bus_get(). We make the same guarantee for
4302 * dbus_connection_open() but in a different way since we don't want to
4303 * unref right here; we instead check for connectedness before returning
4304 * the connection from the hash.
4306 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
4308 /* Dump the outgoing queue, we aren't going to be able to
4309 * send it now, and we'd like accessors like
4310 * dbus_connection_get_outgoing_size() to be accurate.
4312 if (connection->n_outgoing > 0)
4316 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
4317 connection->n_outgoing);
4319 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
4321 _dbus_connection_message_sent_unlocked (connection, link->data);
4326 /* Note this may be called multiple times since we don't track whether we already did it */
4327 static DBusDispatchStatus
4328 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
4330 HAVE_LOCK_CHECK (connection);
4332 if (connection->disconnect_message_link != NULL)
4334 _dbus_verbose ("Sending disconnect message\n");
4336 /* If we have pending calls, queue their timeouts - we want the Disconnected
4337 * to be the last message, after these timeouts.
4339 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
4341 /* We haven't sent the disconnect message already,
4342 * and all real messages have been queued up.
4344 _dbus_connection_queue_synthesized_message_link (connection,
4345 connection->disconnect_message_link);
4346 connection->disconnect_message_link = NULL;
4348 return DBUS_DISPATCH_DATA_REMAINS;
4351 return DBUS_DISPATCH_COMPLETE;
4354 static DBusDispatchStatus
4355 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
4357 HAVE_LOCK_CHECK (connection);
4358 if (connection->dispatch_disabled)
4359 return DBUS_DISPATCH_COMPLETE;
4360 else if (connection->n_incoming > 0)
4361 return DBUS_DISPATCH_DATA_REMAINS;
4362 else if (!_dbus_transport_queue_messages (connection->transport))
4363 return DBUS_DISPATCH_NEED_MEMORY;
4366 DBusDispatchStatus status;
4367 dbus_bool_t is_connected;
4369 status = _dbus_transport_get_dispatch_status (connection->transport);
4370 is_connected = _dbus_transport_get_is_connected (connection->transport);
4372 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
4373 DISPATCH_STATUS_NAME (status), is_connected);
4377 /* It's possible this would be better done by having an explicit
4378 * notification from _dbus_transport_disconnect() that would
4379 * synchronously do this, instead of waiting for the next dispatch
4380 * status check. However, probably not good to change until it causes
4383 notify_disconnected_unlocked (connection);
4385 /* I'm not sure this is needed; the idea is that we want to
4386 * queue the Disconnected only after we've read all the
4387 * messages, but if we're disconnected maybe we are guaranteed
4388 * to have read them all ?
4390 if (status == DBUS_DISPATCH_COMPLETE)
4391 status = notify_disconnected_and_dispatch_complete_unlocked (connection);
4394 if (status != DBUS_DISPATCH_COMPLETE)
4396 else if (connection->n_incoming > 0)
4397 return DBUS_DISPATCH_DATA_REMAINS;
4399 return DBUS_DISPATCH_COMPLETE;
4404 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
4405 DBusDispatchStatus new_status)
4407 dbus_bool_t changed;
4408 DBusDispatchStatusFunction function;
4411 HAVE_LOCK_CHECK (connection);
4413 _dbus_connection_ref_unlocked (connection);
4415 changed = new_status != connection->last_dispatch_status;
4417 connection->last_dispatch_status = new_status;
4419 function = connection->dispatch_status_function;
4420 data = connection->dispatch_status_data;
4422 if (connection->disconnected_message_arrived &&
4423 !connection->disconnected_message_processed)
4425 connection->disconnected_message_processed = TRUE;
4427 /* this does an unref, but we have a ref
4428 * so we should not run the finalizer here
4431 connection_forget_shared_unlocked (connection);
4433 if (connection->exit_on_disconnect)
4435 CONNECTION_UNLOCK (connection);
4437 _dbus_verbose ("Exiting on Disconnected signal\n");
4439 _dbus_assert_not_reached ("Call to exit() returned");
4443 /* We drop the lock */
4444 CONNECTION_UNLOCK (connection);
4446 if (changed && function)
4448 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
4449 connection, new_status,
4450 DISPATCH_STATUS_NAME (new_status));
4451 (* function) (connection, new_status, data);
4454 dbus_connection_unref (connection);
4458 * Gets the current state of the incoming message queue.
4459 * #DBUS_DISPATCH_DATA_REMAINS indicates that the message queue
4460 * may contain messages. #DBUS_DISPATCH_COMPLETE indicates that the
4461 * incoming queue is empty. #DBUS_DISPATCH_NEED_MEMORY indicates that
4462 * there could be data, but we can't know for sure without more
4465 * To process the incoming message queue, use dbus_connection_dispatch()
4466 * or (in rare cases) dbus_connection_pop_message().
4468 * Note, #DBUS_DISPATCH_DATA_REMAINS really means that either we
4469 * have messages in the queue, or we have raw bytes buffered up
4470 * that need to be parsed. When these bytes are parsed, they
4471 * may not add up to an entire message. Thus, it's possible
4472 * to see a status of #DBUS_DISPATCH_DATA_REMAINS but not
4473 * have a message yet.
4475 * In particular this happens on initial connection, because all sorts
4476 * of authentication protocol stuff has to be parsed before the
4477 * first message arrives.
4479 * @param connection the connection.
4480 * @returns current dispatch status
4483 dbus_connection_get_dispatch_status (DBusConnection *connection)
4485 DBusDispatchStatus status;
4487 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4489 _dbus_verbose ("start\n");
4491 CONNECTION_LOCK (connection);
4493 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4495 CONNECTION_UNLOCK (connection);
4501 * Filter funtion for handling the Peer standard interface.
4503 static DBusHandlerResult
4504 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
4505 DBusMessage *message)
4507 dbus_bool_t sent = FALSE;
4508 DBusMessage *ret = NULL;
4509 DBusList *expire_link;
4511 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
4513 /* This means we're letting the bus route this message */
4514 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4517 if (!dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
4519 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4522 /* Preallocate a linked-list link, so that if we need to dispose of a
4523 * message, we can attach it to the expired list */
4524 expire_link = _dbus_list_alloc_link (NULL);
4527 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4529 if (dbus_message_is_method_call (message,
4530 DBUS_INTERFACE_PEER,
4533 ret = dbus_message_new_method_return (message);
4537 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4539 else if (dbus_message_is_method_call (message,
4540 DBUS_INTERFACE_PEER,
4544 DBusError error = DBUS_ERROR_INIT;
4546 if (!_dbus_string_init (&uuid))
4549 if (_dbus_get_local_machine_uuid_encoded (&uuid, &error))
4551 const char *v_STRING;
4553 ret = dbus_message_new_method_return (message);
4557 _dbus_string_free (&uuid);
4561 v_STRING = _dbus_string_get_const_data (&uuid);
4562 if (dbus_message_append_args (ret,
4563 DBUS_TYPE_STRING, &v_STRING,
4566 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4569 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4571 dbus_error_free (&error);
4572 _dbus_string_free (&uuid);
4577 ret = dbus_message_new_error (message, error.name, error.message);
4578 dbus_error_free (&error);
4579 _dbus_string_free (&uuid);
4584 sent = _dbus_connection_send_unlocked_no_update (connection, ret,
4588 _dbus_string_free (&uuid);
4592 /* We need to bounce anything else with this interface, otherwise apps
4593 * could start extending the interface and when we added extensions
4594 * here to DBusConnection we'd break those apps.
4596 ret = dbus_message_new_error (message,
4597 DBUS_ERROR_UNKNOWN_METHOD,
4598 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
4602 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4608 _dbus_list_free_link (expire_link);
4612 /* It'll be safe to unref the reply when we unlock */
4613 expire_link->data = ret;
4614 _dbus_list_prepend_link (&connection->expired_messages, expire_link);
4618 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4620 return DBUS_HANDLER_RESULT_HANDLED;
4624 * Processes all builtin filter functions
4626 * If the spec specifies a standard interface
4627 * they should be processed from this method
4629 static DBusHandlerResult
4630 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
4631 DBusMessage *message)
4633 /* We just run one filter for now but have the option to run more
4634 if the spec calls for it in the future */
4636 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
4640 * Processes any incoming data.
4642 * If there's incoming raw data that has not yet been parsed, it is
4643 * parsed, which may or may not result in adding messages to the
4646 * The incoming data buffer is filled when the connection reads from
4647 * its underlying transport (such as a socket). Reading usually
4648 * happens in dbus_watch_handle() or dbus_connection_read_write().
4650 * If there are complete messages in the incoming queue,
4651 * dbus_connection_dispatch() removes one message from the queue and
4652 * processes it. Processing has three steps.
4654 * First, any method replies are passed to #DBusPendingCall or
4655 * dbus_connection_send_with_reply_and_block() in order to
4656 * complete the pending method call.
4658 * Second, any filters registered with dbus_connection_add_filter()
4659 * are run. If any filter returns #DBUS_HANDLER_RESULT_HANDLED
4660 * then processing stops after that filter.
4662 * Third, if the message is a method call it is forwarded to
4663 * any registered object path handlers added with
4664 * dbus_connection_register_object_path() or
4665 * dbus_connection_register_fallback().
4667 * A single call to dbus_connection_dispatch() will process at most
4668 * one message; it will not clear the entire message queue.
4670 * Be careful about calling dbus_connection_dispatch() from inside a
4671 * message handler, i.e. calling dbus_connection_dispatch()
4672 * recursively. If threads have been initialized with a recursive
4673 * mutex function, then this will not deadlock; however, it can
4674 * certainly confuse your application.
4676 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
4678 * @param connection the connection
4679 * @returns dispatch status, see dbus_connection_get_dispatch_status()
4682 dbus_connection_dispatch (DBusConnection *connection)
4684 DBusMessage *message;
4685 DBusList *link, *filter_list_copy, *message_link;
4686 DBusHandlerResult result;
4687 DBusPendingCall *pending;
4688 dbus_int32_t reply_serial;
4689 DBusDispatchStatus status;
4690 dbus_bool_t found_object;
4692 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4694 _dbus_verbose ("\n");
4696 CONNECTION_LOCK (connection);
4697 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4698 if (status != DBUS_DISPATCH_DATA_REMAINS)
4700 /* unlocks and calls out to user code */
4701 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4705 /* We need to ref the connection since the callback could potentially
4706 * drop the last ref to it
4708 _dbus_connection_ref_unlocked (connection);
4710 _dbus_connection_acquire_dispatch (connection);
4711 HAVE_LOCK_CHECK (connection);
4713 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4714 if (message_link == NULL)
4716 /* another thread dispatched our stuff */
4718 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
4720 _dbus_connection_release_dispatch (connection);
4722 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4724 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4726 dbus_connection_unref (connection);
4731 message = message_link->data;
4733 _dbus_verbose (" dispatching message %p (%s %s %s '%s')\n",
4735 dbus_message_type_to_string (dbus_message_get_type (message)),
4736 dbus_message_get_interface (message) ?
4737 dbus_message_get_interface (message) :
4739 dbus_message_get_member (message) ?
4740 dbus_message_get_member (message) :
4742 dbus_message_get_signature (message));
4744 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4746 /* Pending call handling must be first, because if you do
4747 * dbus_connection_send_with_reply_and_block() or
4748 * dbus_pending_call_block() then no handlers/filters will be run on
4749 * the reply. We want consistent semantics in the case where we
4750 * dbus_connection_dispatch() the reply.
4753 reply_serial = dbus_message_get_reply_serial (message);
4754 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4758 _dbus_verbose ("Dispatching a pending reply\n");
4759 complete_pending_call_and_unlock (connection, pending, message);
4760 pending = NULL; /* it's probably unref'd */
4762 CONNECTION_LOCK (connection);
4763 _dbus_verbose ("pending call completed in dispatch\n");
4764 result = DBUS_HANDLER_RESULT_HANDLED;
4768 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4769 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4772 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4774 _dbus_connection_release_dispatch (connection);
4775 HAVE_LOCK_CHECK (connection);
4777 _dbus_connection_failed_pop (connection, message_link);
4779 /* unlocks and calls user code */
4780 _dbus_connection_update_dispatch_status_and_unlock (connection,
4781 DBUS_DISPATCH_NEED_MEMORY);
4782 dbus_connection_unref (connection);
4784 return DBUS_DISPATCH_NEED_MEMORY;
4787 _dbus_list_foreach (&filter_list_copy,
4788 (DBusForeachFunction)_dbus_message_filter_ref,
4791 /* We're still protected from dispatch() reentrancy here
4792 * since we acquired the dispatcher
4794 CONNECTION_UNLOCK (connection);
4796 link = _dbus_list_get_first_link (&filter_list_copy);
4797 while (link != NULL)
4799 DBusMessageFilter *filter = link->data;
4800 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
4802 if (filter->function == NULL)
4804 _dbus_verbose (" filter was removed in a callback function\n");
4809 _dbus_verbose (" running filter on message %p\n", message);
4810 result = (* filter->function) (connection, message, filter->user_data);
4812 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4818 _dbus_list_foreach (&filter_list_copy,
4819 (DBusForeachFunction)_dbus_message_filter_unref,
4821 _dbus_list_clear (&filter_list_copy);
4823 CONNECTION_LOCK (connection);
4825 if (result == DBUS_HANDLER_RESULT_LATER)
4827 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4829 _dbus_verbose ("No memory\n");
4832 else if (result == DBUS_HANDLER_RESULT_HANDLED)
4834 _dbus_verbose ("filter handled message in dispatch\n");
4838 /* We're still protected from dispatch() reentrancy here
4839 * since we acquired the dispatcher
4841 _dbus_verbose (" running object path dispatch on message %p (%s %s %s '%s')\n",
4843 dbus_message_type_to_string (dbus_message_get_type (message)),
4844 dbus_message_get_interface (message) ?
4845 dbus_message_get_interface (message) :
4847 dbus_message_get_member (message) ?
4848 dbus_message_get_member (message) :
4850 dbus_message_get_signature (message));
4852 HAVE_LOCK_CHECK (connection);
4853 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4857 CONNECTION_LOCK (connection);
4859 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4861 _dbus_verbose ("object tree handled message in dispatch\n");
4865 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
4869 DBusPreallocatedSend *preallocated;
4870 DBusList *expire_link;
4872 _dbus_verbose (" sending error %s\n",
4873 DBUS_ERROR_UNKNOWN_METHOD);
4875 if (!_dbus_string_init (&str))
4877 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4878 _dbus_verbose ("no memory for error string in dispatch\n");
4882 if (!_dbus_string_append_printf (&str,
4883 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
4884 dbus_message_get_member (message),
4885 dbus_message_get_signature (message),
4886 dbus_message_get_interface (message)))
4888 _dbus_string_free (&str);
4889 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4890 _dbus_verbose ("no memory for error string in dispatch\n");
4894 reply = dbus_message_new_error (message,
4895 found_object ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_OBJECT,
4896 _dbus_string_get_const_data (&str));
4897 _dbus_string_free (&str);
4901 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4902 _dbus_verbose ("no memory for error reply in dispatch\n");
4906 expire_link = _dbus_list_alloc_link (reply);
4908 if (expire_link == NULL)
4910 dbus_message_unref (reply);
4911 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4912 _dbus_verbose ("no memory for error send in dispatch\n");
4916 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4918 if (preallocated == NULL)
4920 _dbus_list_free_link (expire_link);
4921 /* It's OK that this is finalized, because it hasn't been seen by
4922 * anything that could attach user callbacks */
4923 dbus_message_unref (reply);
4924 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4925 _dbus_verbose ("no memory for error send in dispatch\n");
4929 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4931 /* reply will be freed when we release the lock */
4932 _dbus_list_prepend_link (&connection->expired_messages, expire_link);
4934 result = DBUS_HANDLER_RESULT_HANDLED;
4937 _dbus_verbose (" done dispatching %p (%s %s %s '%s') on connection %p\n", message,
4938 dbus_message_type_to_string (dbus_message_get_type (message)),
4939 dbus_message_get_interface (message) ?
4940 dbus_message_get_interface (message) :
4942 dbus_message_get_member (message) ?
4943 dbus_message_get_member (message) :
4945 dbus_message_get_signature (message),
4949 if (result == DBUS_HANDLER_RESULT_LATER ||
4950 result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4952 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4953 _dbus_verbose ("out of memory\n");
4955 /* Put message back, and we'll start over.
4956 * Yes this means handlers must be idempotent if they
4957 * don't return HANDLED; c'est la vie.
4959 _dbus_connection_putback_message_link_unlocked (connection,
4961 /* now we don't want to free them */
4962 message_link = NULL;
4967 _dbus_verbose (" ... done dispatching\n");
4970 _dbus_connection_release_dispatch (connection);
4971 HAVE_LOCK_CHECK (connection);
4973 if (message != NULL)
4975 /* We don't want this message to count in maximum message limits when
4976 * computing the dispatch status, below. We have to drop the lock
4977 * temporarily, because finalizing a message can trigger callbacks.
4979 * We have a reference to the connection, and we don't use any cached
4980 * pointers to the connection's internals below this point, so it should
4981 * be safe to drop the lock and take it back. */
4982 CONNECTION_UNLOCK (connection);
4983 dbus_message_unref (message);
4984 CONNECTION_LOCK (connection);
4987 if (message_link != NULL)
4988 _dbus_list_free_link (message_link);
4990 _dbus_verbose ("before final status update\n");
4991 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4993 /* unlocks and calls user code */
4994 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4996 dbus_connection_unref (connection);
5002 * Sets the watch functions for the connection. These functions are
5003 * responsible for making the application's main loop aware of file
5004 * descriptors that need to be monitored for events, using select() or
5005 * poll(). When using Qt, typically the DBusAddWatchFunction would
5006 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
5007 * could call g_io_add_watch(), or could be used as part of a more
5008 * elaborate GSource. Note that when a watch is added, it may
5011 * The DBusWatchToggledFunction notifies the application that the
5012 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
5013 * to check this. A disabled watch should have no effect, and enabled
5014 * watch should be added to the main loop. This feature is used
5015 * instead of simply adding/removing the watch because
5016 * enabling/disabling can be done without memory allocation. The
5017 * toggled function may be NULL if a main loop re-queries
5018 * dbus_watch_get_enabled() every time anyway.
5020 * The DBusWatch can be queried for the file descriptor to watch using
5021 * dbus_watch_get_unix_fd() or dbus_watch_get_socket(), and for the
5022 * events to watch for using dbus_watch_get_flags(). The flags
5023 * returned by dbus_watch_get_flags() will only contain
5024 * DBUS_WATCH_READABLE and DBUS_WATCH_WRITABLE, never
5025 * DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; all watches implicitly
5026 * include a watch for hangups, errors, and other exceptional
5029 * Once a file descriptor becomes readable or writable, or an exception
5030 * occurs, dbus_watch_handle() should be called to
5031 * notify the connection of the file descriptor's condition.
5033 * dbus_watch_handle() cannot be called during the
5034 * DBusAddWatchFunction, as the connection will not be ready to handle
5037 * It is not allowed to reference a DBusWatch after it has been passed
5038 * to remove_function.
5040 * If #FALSE is returned due to lack of memory, the failure may be due
5041 * to a #FALSE return from the new add_function. If so, the
5042 * add_function may have been called successfully one or more times,
5043 * but the remove_function will also have been called to remove any
5044 * successful adds. i.e. if #FALSE is returned the net result
5045 * should be that dbus_connection_set_watch_functions() has no effect,
5046 * but the add_function and remove_function may have been called.
5048 * @note The thread lock on DBusConnection is held while
5049 * watch functions are invoked, so inside these functions you
5050 * may not invoke any methods on DBusConnection or it will deadlock.
5051 * See the comments in the code or http://lists.freedesktop.org/archives/dbus/2007-July/tread.html#8144
5052 * if you encounter this issue and want to attempt writing a patch.
5054 * @param connection the connection.
5055 * @param add_function function to begin monitoring a new descriptor.
5056 * @param remove_function function to stop monitoring a descriptor.
5057 * @param toggled_function function to notify of enable/disable
5058 * @param data data to pass to add_function and remove_function.
5059 * @param free_data_function function to be called to free the data.
5060 * @returns #FALSE on failure (no memory)
5063 dbus_connection_set_watch_functions (DBusConnection *connection,
5064 DBusAddWatchFunction add_function,
5065 DBusRemoveWatchFunction remove_function,
5066 DBusWatchToggledFunction toggled_function,
5068 DBusFreeFunction free_data_function)
5072 _dbus_return_val_if_fail (connection != NULL, FALSE);
5074 CONNECTION_LOCK (connection);
5076 retval = _dbus_watch_list_set_functions (connection->watches,
5077 add_function, remove_function,
5079 data, free_data_function);
5081 CONNECTION_UNLOCK (connection);
5087 * Sets the timeout functions for the connection. These functions are
5088 * responsible for making the application's main loop aware of timeouts.
5089 * When using Qt, typically the DBusAddTimeoutFunction would create a
5090 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
5093 * The DBusTimeoutToggledFunction notifies the application that the
5094 * timeout has been enabled or disabled. Call
5095 * dbus_timeout_get_enabled() to check this. A disabled timeout should
5096 * have no effect, and enabled timeout should be added to the main
5097 * loop. This feature is used instead of simply adding/removing the
5098 * timeout because enabling/disabling can be done without memory
5099 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
5100 * to enable and disable. The toggled function may be NULL if a main
5101 * loop re-queries dbus_timeout_get_enabled() every time anyway.
5102 * Whenever a timeout is toggled, its interval may change.
5104 * The DBusTimeout can be queried for the timer interval using
5105 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
5106 * repeatedly, each time the interval elapses, starting after it has
5107 * elapsed once. The timeout stops firing when it is removed with the
5108 * given remove_function. The timer interval may change whenever the
5109 * timeout is added, removed, or toggled.
5111 * @note The thread lock on DBusConnection is held while
5112 * timeout functions are invoked, so inside these functions you
5113 * may not invoke any methods on DBusConnection or it will deadlock.
5114 * See the comments in the code or http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
5115 * if you encounter this issue and want to attempt writing a patch.
5117 * @param connection the connection.
5118 * @param add_function function to add a timeout.
5119 * @param remove_function function to remove a timeout.
5120 * @param toggled_function function to notify of enable/disable
5121 * @param data data to pass to add_function and remove_function.
5122 * @param free_data_function function to be called to free the data.
5123 * @returns #FALSE on failure (no memory)
5126 dbus_connection_set_timeout_functions (DBusConnection *connection,
5127 DBusAddTimeoutFunction add_function,
5128 DBusRemoveTimeoutFunction remove_function,
5129 DBusTimeoutToggledFunction toggled_function,
5131 DBusFreeFunction free_data_function)
5135 _dbus_return_val_if_fail (connection != NULL, FALSE);
5137 CONNECTION_LOCK (connection);
5139 retval = _dbus_timeout_list_set_functions (connection->timeouts,
5140 add_function, remove_function,
5142 data, free_data_function);
5144 CONNECTION_UNLOCK (connection);
5150 * Sets the mainloop wakeup function for the connection. This function
5151 * is responsible for waking up the main loop (if its sleeping in
5152 * another thread) when some some change has happened to the
5153 * connection that the mainloop needs to reconsider (e.g. a message
5154 * has been queued for writing). When using Qt, this typically
5155 * results in a call to QEventLoop::wakeUp(). When using GLib, it
5156 * would call g_main_context_wakeup().
5158 * @param connection the connection.
5159 * @param wakeup_main_function function to wake up the mainloop
5160 * @param data data to pass wakeup_main_function
5161 * @param free_data_function function to be called to free the data.
5164 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
5165 DBusWakeupMainFunction wakeup_main_function,
5167 DBusFreeFunction free_data_function)
5170 DBusFreeFunction old_free_data;
5172 _dbus_return_if_fail (connection != NULL);
5174 CONNECTION_LOCK (connection);
5175 old_data = connection->wakeup_main_data;
5176 old_free_data = connection->free_wakeup_main_data;
5178 connection->wakeup_main_function = wakeup_main_function;
5179 connection->wakeup_main_data = data;
5180 connection->free_wakeup_main_data = free_data_function;
5182 CONNECTION_UNLOCK (connection);
5184 /* Callback outside the lock */
5186 (*old_free_data) (old_data);
5190 * Set a function to be invoked when the dispatch status changes.
5191 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
5192 * dbus_connection_dispatch() needs to be called to process incoming
5193 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
5194 * from inside the DBusDispatchStatusFunction. Indeed, almost
5195 * any reentrancy in this function is a bad idea. Instead,
5196 * the DBusDispatchStatusFunction should simply save an indication
5197 * that messages should be dispatched later, when the main loop
5200 * If you don't set a dispatch status function, you have to be sure to
5201 * dispatch on every iteration of your main loop, especially if
5202 * dbus_watch_handle() or dbus_timeout_handle() were called.
5204 * @param connection the connection
5205 * @param function function to call on dispatch status changes
5206 * @param data data for function
5207 * @param free_data_function free the function data
5210 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
5211 DBusDispatchStatusFunction function,
5213 DBusFreeFunction free_data_function)
5216 DBusFreeFunction old_free_data;
5218 _dbus_return_if_fail (connection != NULL);
5220 CONNECTION_LOCK (connection);
5221 old_data = connection->dispatch_status_data;
5222 old_free_data = connection->free_dispatch_status_data;
5224 connection->dispatch_status_function = function;
5225 connection->dispatch_status_data = data;
5226 connection->free_dispatch_status_data = free_data_function;
5228 CONNECTION_UNLOCK (connection);
5230 /* Callback outside the lock */
5232 (*old_free_data) (old_data);
5236 * Get the UNIX file descriptor of the connection, if any. This can
5237 * be used for SELinux access control checks with getpeercon() for
5238 * example. DO NOT read or write to the file descriptor, or try to
5239 * select() on it; use DBusWatch for main loop integration. Not all
5240 * connections will have a file descriptor. So for adding descriptors
5241 * to the main loop, use dbus_watch_get_unix_fd() and so forth.
5243 * If the connection is socket-based, you can also use
5244 * dbus_connection_get_socket(), which will work on Windows too.
5245 * This function always fails on Windows.
5247 * Right now the returned descriptor is always a socket, but
5248 * that is not guaranteed.
5250 * @param connection the connection
5251 * @param fd return location for the file descriptor.
5252 * @returns #TRUE if fd is successfully obtained.
5255 dbus_connection_get_unix_fd (DBusConnection *connection,
5258 _dbus_return_val_if_fail (connection != NULL, FALSE);
5259 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
5262 /* FIXME do this on a lower level */
5266 return dbus_connection_get_socket(connection, fd);
5270 * Gets the underlying Windows or UNIX socket file descriptor
5271 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
5272 * select() on it; use DBusWatch for main loop integration. Not all
5273 * connections will have a socket. So for adding descriptors
5274 * to the main loop, use dbus_watch_get_socket() and so forth.
5276 * If the connection is not socket-based, this function will return FALSE,
5277 * even if the connection does have a file descriptor of some kind.
5278 * i.e. this function always returns specifically a socket file descriptor.
5280 * @param connection the connection
5281 * @param fd return location for the file descriptor.
5282 * @returns #TRUE if fd is successfully obtained.
5285 dbus_connection_get_socket(DBusConnection *connection,
5289 DBusSocket s = DBUS_SOCKET_INIT;
5291 _dbus_return_val_if_fail (connection != NULL, FALSE);
5292 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
5294 CONNECTION_LOCK (connection);
5296 retval = _dbus_transport_get_socket_fd (connection->transport, &s);
5300 *fd = _dbus_socket_get_int (s);
5303 CONNECTION_UNLOCK (connection);
5310 * Getter for number of messages in incoming queue.
5311 * Useful for sending reply to self (see kdbus_do_iteration)
5314 _dbus_connection_get_n_incoming (DBusConnection *connection)
5316 return connection->n_incoming;
5320 * Gets the UNIX user ID of the connection if known. Returns #TRUE if
5321 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
5322 * for now, though in theory someone could hook Windows to NIS or
5323 * something. Always returns #FALSE prior to authenticating the
5326 * The UID is only read by servers from clients; clients can't usually
5327 * get the UID of servers, because servers do not authenticate to
5328 * clients. The returned UID is the UID the connection authenticated
5331 * The message bus is a server and the apps connecting to the bus
5334 * You can ask the bus to tell you the UID of another connection though
5335 * if you like; this is done with dbus_bus_get_unix_user().
5337 * @param connection the connection
5338 * @param uid return location for the user ID
5339 * @returns #TRUE if uid is filled in with a valid user ID
5342 dbus_connection_get_unix_user (DBusConnection *connection,
5347 _dbus_return_val_if_fail (connection != NULL, FALSE);
5348 _dbus_return_val_if_fail (uid != NULL, FALSE);
5350 CONNECTION_LOCK (connection);
5352 if (!_dbus_transport_try_to_authenticate (connection->transport))
5355 result = _dbus_transport_get_unix_user (connection->transport,
5359 _dbus_assert (!result);
5362 CONNECTION_UNLOCK (connection);
5368 * Gets the process ID of the connection if any.
5369 * Returns #TRUE if the pid is filled in.
5370 * Always returns #FALSE prior to authenticating the
5373 * @param connection the connection
5374 * @param pid return location for the process ID
5375 * @returns #TRUE if uid is filled in with a valid process ID
5378 dbus_connection_get_unix_process_id (DBusConnection *connection,
5383 _dbus_return_val_if_fail (connection != NULL, FALSE);
5384 _dbus_return_val_if_fail (pid != NULL, FALSE);
5386 CONNECTION_LOCK (connection);
5388 if (!_dbus_transport_try_to_authenticate (connection->transport))
5391 result = _dbus_transport_get_unix_process_id (connection->transport,
5394 CONNECTION_UNLOCK (connection);
5400 * Gets the ADT audit data of the connection if any.
5401 * Returns #TRUE if the structure pointer is returned.
5402 * Always returns #FALSE prior to authenticating the
5405 * @param connection the connection
5406 * @param data return location for audit data
5407 * @param data_size return location for length of audit data
5408 * @returns #TRUE if audit data is filled in with a valid ucred pointer
5411 dbus_connection_get_adt_audit_session_data (DBusConnection *connection,
5413 dbus_int32_t *data_size)
5417 _dbus_return_val_if_fail (connection != NULL, FALSE);
5418 _dbus_return_val_if_fail (data != NULL, FALSE);
5419 _dbus_return_val_if_fail (data_size != NULL, FALSE);
5421 CONNECTION_LOCK (connection);
5423 if (!_dbus_transport_try_to_authenticate (connection->transport))
5426 result = _dbus_transport_get_adt_audit_session_data (connection->transport,
5429 CONNECTION_UNLOCK (connection);
5435 * Sets a predicate function used to determine whether a given user ID
5436 * is allowed to connect. When an incoming connection has
5437 * authenticated with a particular user ID, this function is called;
5438 * if it returns #TRUE, the connection is allowed to proceed,
5439 * otherwise the connection is disconnected.
5441 * If the function is set to #NULL (as it is by default), then
5442 * only the same UID as the server process will be allowed to
5443 * connect. Also, root is always allowed to connect.
5445 * On Windows, the function will be set and its free_data_function will
5446 * be invoked when the connection is freed or a new function is set.
5447 * However, the function will never be called, because there are
5448 * no UNIX user ids to pass to it, or at least none of the existing
5449 * auth protocols would allow authenticating as a UNIX user on Windows.
5451 * @param connection the connection
5452 * @param function the predicate
5453 * @param data data to pass to the predicate
5454 * @param free_data_function function to free the data
5457 dbus_connection_set_unix_user_function (DBusConnection *connection,
5458 DBusAllowUnixUserFunction function,
5460 DBusFreeFunction free_data_function)
5462 void *old_data = NULL;
5463 DBusFreeFunction old_free_function = NULL;
5465 _dbus_return_if_fail (connection != NULL);
5467 CONNECTION_LOCK (connection);
5468 _dbus_transport_set_unix_user_function (connection->transport,
5469 function, data, free_data_function,
5470 &old_data, &old_free_function);
5471 CONNECTION_UNLOCK (connection);
5473 if (old_free_function != NULL)
5474 (* old_free_function) (old_data);
5477 /* Same calling convention as dbus_connection_get_windows_user */
5479 _dbus_connection_get_linux_security_label (DBusConnection *connection,
5484 _dbus_assert (connection != NULL);
5485 _dbus_assert (label_p != NULL);
5487 CONNECTION_LOCK (connection);
5489 if (!_dbus_transport_try_to_authenticate (connection->transport))
5492 result = _dbus_transport_get_linux_security_label (connection->transport,
5495 _dbus_assert (!result);
5498 CONNECTION_UNLOCK (connection);
5504 * Gets the Windows user SID of the connection if known. Returns
5505 * #TRUE if the ID is filled in. Always returns #FALSE on non-Windows
5506 * platforms for now, though in theory someone could hook UNIX to
5507 * Active Directory or something. Always returns #FALSE prior to
5508 * authenticating the connection.
5510 * The user is only read by servers from clients; clients can't usually
5511 * get the user of servers, because servers do not authenticate to
5512 * clients. The returned user is the user the connection authenticated
5515 * The message bus is a server and the apps connecting to the bus
5518 * The returned user string has to be freed with dbus_free().
5520 * The return value indicates whether the user SID is available;
5521 * if it's available but we don't have the memory to copy it,
5522 * then the return value is #TRUE and #NULL is given as the SID.
5524 * @todo We would like to be able to say "You can ask the bus to tell
5525 * you the user of another connection though if you like; this is done
5526 * with dbus_bus_get_windows_user()." But this has to be implemented
5527 * in bus/driver.c and dbus/dbus-bus.c, and is pointless anyway
5528 * since on Windows we only use the session bus for now.
5530 * @param connection the connection
5531 * @param windows_sid_p return location for an allocated copy of the user ID, or #NULL if no memory
5532 * @returns #TRUE if user is available (returned value may be #NULL anyway if no memory)
5535 dbus_connection_get_windows_user (DBusConnection *connection,
5536 char **windows_sid_p)
5540 _dbus_return_val_if_fail (connection != NULL, FALSE);
5541 _dbus_return_val_if_fail (windows_sid_p != NULL, FALSE);
5543 CONNECTION_LOCK (connection);
5545 if (!_dbus_transport_try_to_authenticate (connection->transport))
5548 result = _dbus_transport_get_windows_user (connection->transport,
5552 _dbus_assert (!result);
5555 CONNECTION_UNLOCK (connection);
5561 * Sets a predicate function used to determine whether a given user ID
5562 * is allowed to connect. When an incoming connection has
5563 * authenticated with a particular user ID, this function is called;
5564 * if it returns #TRUE, the connection is allowed to proceed,
5565 * otherwise the connection is disconnected.
5567 * If the function is set to #NULL (as it is by default), then
5568 * only the same user owning the server process will be allowed to
5571 * On UNIX, the function will be set and its free_data_function will
5572 * be invoked when the connection is freed or a new function is set.
5573 * However, the function will never be called, because there is no
5574 * way right now to authenticate as a Windows user on UNIX.
5576 * @param connection the connection
5577 * @param function the predicate
5578 * @param data data to pass to the predicate
5579 * @param free_data_function function to free the data
5582 dbus_connection_set_windows_user_function (DBusConnection *connection,
5583 DBusAllowWindowsUserFunction function,
5585 DBusFreeFunction free_data_function)
5587 void *old_data = NULL;
5588 DBusFreeFunction old_free_function = NULL;
5590 _dbus_return_if_fail (connection != NULL);
5592 CONNECTION_LOCK (connection);
5593 _dbus_transport_set_windows_user_function (connection->transport,
5594 function, data, free_data_function,
5595 &old_data, &old_free_function);
5596 CONNECTION_UNLOCK (connection);
5598 if (old_free_function != NULL)
5599 (* old_free_function) (old_data);
5603 * This function must be called on the server side of a connection when the
5604 * connection is first seen in the #DBusNewConnectionFunction. If set to
5605 * #TRUE (the default is #FALSE), then the connection can proceed even if
5606 * the client does not authenticate as some user identity, i.e. clients
5607 * can connect anonymously.
5609 * This setting interacts with the available authorization mechanisms
5610 * (see dbus_server_set_auth_mechanisms()). Namely, an auth mechanism
5611 * such as ANONYMOUS that supports anonymous auth must be included in
5612 * the list of available mechanisms for anonymous login to work.
5614 * This setting also changes the default rule for connections
5615 * authorized as a user; normally, if a connection authorizes as
5616 * a user identity, it is permitted if the user identity is
5617 * root or the user identity matches the user identity of the server
5618 * process. If anonymous connections are allowed, however,
5619 * then any user identity is allowed.
5621 * You can override the rules for connections authorized as a
5622 * user identity with dbus_connection_set_unix_user_function()
5623 * and dbus_connection_set_windows_user_function().
5625 * @param connection the connection
5626 * @param value whether to allow authentication as an anonymous user
5629 dbus_connection_set_allow_anonymous (DBusConnection *connection,
5632 _dbus_return_if_fail (connection != NULL);
5634 CONNECTION_LOCK (connection);
5635 _dbus_transport_set_allow_anonymous (connection->transport, value);
5636 CONNECTION_UNLOCK (connection);
5641 * Normally #DBusConnection automatically handles all messages to the
5642 * org.freedesktop.DBus.Peer interface. However, the message bus wants
5643 * to be able to route methods on that interface through the bus and
5644 * to other applications. If routing peer messages is enabled, then
5645 * messages with the org.freedesktop.DBus.Peer interface that also
5646 * have a bus destination name set will not be automatically
5647 * handled by the #DBusConnection and instead will be dispatched
5648 * normally to the application.
5650 * If a normal application sets this flag, it can break things badly.
5651 * So don't set this unless you are the message bus.
5653 * @param connection the connection
5654 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
5657 dbus_connection_set_route_peer_messages (DBusConnection *connection,
5660 _dbus_return_if_fail (connection != NULL);
5662 CONNECTION_LOCK (connection);
5663 connection->route_peer_messages = value;
5664 CONNECTION_UNLOCK (connection);
5668 * Adds a message filter. Filters are handlers that are run on all
5669 * incoming messages, prior to the objects registered with
5670 * dbus_connection_register_object_path(). Filters are run in the
5671 * order that they were added. The same handler can be added as a
5672 * filter more than once, in which case it will be run more than once.
5673 * Filters added during a filter callback won't be run on the message
5676 * @todo we don't run filters on messages while blocking without
5677 * entering the main loop, since filters are run as part of
5678 * dbus_connection_dispatch(). This is probably a feature, as filters
5679 * could create arbitrary reentrancy. But kind of sucks if you're
5680 * trying to filter METHOD_RETURN for some reason.
5682 * @param connection the connection
5683 * @param function function to handle messages
5684 * @param user_data user data to pass to the function
5685 * @param free_data_function function to use for freeing user data
5686 * @returns #TRUE on success, #FALSE if not enough memory.
5689 dbus_connection_add_filter (DBusConnection *connection,
5690 DBusHandleMessageFunction function,
5692 DBusFreeFunction free_data_function)
5694 DBusMessageFilter *filter;
5696 _dbus_return_val_if_fail (connection != NULL, FALSE);
5697 _dbus_return_val_if_fail (function != NULL, FALSE);
5699 filter = dbus_new0 (DBusMessageFilter, 1);
5703 _dbus_atomic_inc (&filter->refcount);
5705 CONNECTION_LOCK (connection);
5707 if (!_dbus_list_append (&connection->filter_list,
5710 _dbus_message_filter_unref (filter);
5711 CONNECTION_UNLOCK (connection);
5715 /* Fill in filter after all memory allocated,
5716 * so we don't run the free_user_data_function
5717 * if the add_filter() fails
5720 filter->function = function;
5721 filter->user_data = user_data;
5722 filter->free_user_data_function = free_data_function;
5724 CONNECTION_UNLOCK (connection);
5729 * Removes a previously-added message filter. It is a programming
5730 * error to call this function for a handler that has not been added
5731 * as a filter. If the given handler was added more than once, only
5732 * one instance of it will be removed (the most recently-added
5735 * @param connection the connection
5736 * @param function the handler to remove
5737 * @param user_data user data for the handler to remove
5741 dbus_connection_remove_filter (DBusConnection *connection,
5742 DBusHandleMessageFunction function,
5746 DBusMessageFilter *filter;
5748 _dbus_return_if_fail (connection != NULL);
5749 _dbus_return_if_fail (function != NULL);
5751 CONNECTION_LOCK (connection);
5755 link = _dbus_list_get_last_link (&connection->filter_list);
5756 while (link != NULL)
5758 filter = link->data;
5760 if (filter->function == function &&
5761 filter->user_data == user_data)
5763 _dbus_list_remove_link (&connection->filter_list, link);
5764 filter->function = NULL;
5769 link = _dbus_list_get_prev_link (&connection->filter_list, link);
5773 CONNECTION_UNLOCK (connection);
5775 #ifndef DBUS_DISABLE_CHECKS
5778 _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
5779 function, user_data);
5784 /* Call application code */
5785 if (filter->free_user_data_function)
5786 (* filter->free_user_data_function) (filter->user_data);
5788 filter->free_user_data_function = NULL;
5789 filter->user_data = NULL;
5791 _dbus_message_filter_unref (filter);
5795 * Registers a handler for a given path or subsection in the object
5796 * hierarchy. The given vtable handles messages sent to exactly the
5797 * given path or also for paths bellow that, depending on fallback
5800 * @param connection the connection
5801 * @param fallback whether to handle messages also for "subdirectory"
5802 * @param path a '/' delimited string of path elements
5803 * @param vtable the virtual table
5804 * @param user_data data to pass to functions in the vtable
5805 * @param error address where an error can be returned
5806 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5807 * #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
5810 _dbus_connection_register_object_path (DBusConnection *connection,
5811 dbus_bool_t fallback,
5813 const DBusObjectPathVTable *vtable,
5817 char **decomposed_path;
5820 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5823 CONNECTION_LOCK (connection);
5825 retval = _dbus_object_tree_register (connection->objects,
5827 (const char **) decomposed_path, vtable,
5830 CONNECTION_UNLOCK (connection);
5832 dbus_free_string_array (decomposed_path);
5838 * Registers a handler for a given path in the object hierarchy.
5839 * The given vtable handles messages sent to exactly the given path.
5841 * @param connection the connection
5842 * @param path a '/' delimited string of path elements
5843 * @param vtable the virtual table
5844 * @param user_data data to pass to functions in the vtable
5845 * @param error address where an error can be returned
5846 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5847 * #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
5850 dbus_connection_try_register_object_path (DBusConnection *connection,
5852 const DBusObjectPathVTable *vtable,
5856 _dbus_return_val_if_fail (connection != NULL, FALSE);
5857 _dbus_return_val_if_fail (path != NULL, FALSE);
5858 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5859 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5861 return _dbus_connection_register_object_path (connection, FALSE, path, vtable, user_data, error);
5865 * Registers a handler for a given path in the object hierarchy.
5866 * The given vtable handles messages sent to exactly the given path.
5868 * It is a bug to call this function for object paths which already
5869 * have a handler. Use dbus_connection_try_register_object_path() if this
5870 * might be the case.
5872 * @param connection the connection
5873 * @param path a '/' delimited string of path elements
5874 * @param vtable the virtual table
5875 * @param user_data data to pass to functions in the vtable
5876 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5877 * #DBUS_ERROR_OBJECT_PATH_IN_USE) ocurred
5880 dbus_connection_register_object_path (DBusConnection *connection,
5882 const DBusObjectPathVTable *vtable,
5886 DBusError error = DBUS_ERROR_INIT;
5888 _dbus_return_val_if_fail (connection != NULL, FALSE);
5889 _dbus_return_val_if_fail (path != NULL, FALSE);
5890 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5891 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5893 retval = _dbus_connection_register_object_path (connection, FALSE, path, vtable, user_data, &error);
5895 if (dbus_error_has_name (&error, DBUS_ERROR_OBJECT_PATH_IN_USE))
5897 _dbus_warn ("%s\n", error.message);
5898 dbus_error_free (&error);
5906 * Registers a fallback handler for a given subsection of the object
5907 * hierarchy. The given vtable handles messages at or below the given
5908 * path. You can use this to establish a default message handling
5909 * policy for a whole "subdirectory."
5911 * @param connection the connection
5912 * @param path a '/' delimited string of path elements
5913 * @param vtable the virtual table
5914 * @param user_data data to pass to functions in the vtable
5915 * @param error address where an error can be returned
5916 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5917 * #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
5920 dbus_connection_try_register_fallback (DBusConnection *connection,
5922 const DBusObjectPathVTable *vtable,
5926 _dbus_return_val_if_fail (connection != NULL, FALSE);
5927 _dbus_return_val_if_fail (path != NULL, FALSE);
5928 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5929 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5931 return _dbus_connection_register_object_path (connection, TRUE, path, vtable, user_data, error);
5935 * Registers a fallback handler for a given subsection of the object
5936 * hierarchy. The given vtable handles messages at or below the given
5937 * path. You can use this to establish a default message handling
5938 * policy for a whole "subdirectory."
5940 * It is a bug to call this function for object paths which already
5941 * have a handler. Use dbus_connection_try_register_fallback() if this
5942 * might be the case.
5944 * @param connection the connection
5945 * @param path a '/' delimited string of path elements
5946 * @param vtable the virtual table
5947 * @param user_data data to pass to functions in the vtable
5948 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5949 * #DBUS_ERROR_OBJECT_PATH_IN_USE) occured
5952 dbus_connection_register_fallback (DBusConnection *connection,
5954 const DBusObjectPathVTable *vtable,
5958 DBusError error = DBUS_ERROR_INIT;
5960 _dbus_return_val_if_fail (connection != NULL, FALSE);
5961 _dbus_return_val_if_fail (path != NULL, FALSE);
5962 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5963 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5965 retval = _dbus_connection_register_object_path (connection, TRUE, path, vtable, user_data, &error);
5967 if (dbus_error_has_name (&error, DBUS_ERROR_OBJECT_PATH_IN_USE))
5969 _dbus_warn ("%s\n", error.message);
5970 dbus_error_free (&error);
5978 * Unregisters the handler registered with exactly the given path.
5979 * It's a bug to call this function for a path that isn't registered.
5980 * Can unregister both fallback paths and object paths.
5982 * @param connection the connection
5983 * @param path a '/' delimited string of path elements
5984 * @returns #FALSE if not enough memory
5987 dbus_connection_unregister_object_path (DBusConnection *connection,
5990 char **decomposed_path;
5992 _dbus_return_val_if_fail (connection != NULL, FALSE);
5993 _dbus_return_val_if_fail (path != NULL, FALSE);
5994 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5996 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5999 CONNECTION_LOCK (connection);
6001 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
6003 dbus_free_string_array (decomposed_path);
6009 * Gets the user data passed to dbus_connection_register_object_path()
6010 * or dbus_connection_register_fallback(). If nothing was registered
6011 * at this path, the data is filled in with #NULL.
6013 * @param connection the connection
6014 * @param path the path you registered with
6015 * @param data_p location to store the user data, or #NULL
6016 * @returns #FALSE if not enough memory
6019 dbus_connection_get_object_path_data (DBusConnection *connection,
6023 char **decomposed_path;
6025 _dbus_return_val_if_fail (connection != NULL, FALSE);
6026 _dbus_return_val_if_fail (path != NULL, FALSE);
6027 _dbus_return_val_if_fail (data_p != NULL, FALSE);
6031 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
6034 CONNECTION_LOCK (connection);
6036 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
6038 CONNECTION_UNLOCK (connection);
6040 dbus_free_string_array (decomposed_path);
6046 * Lists the registered fallback handlers and object path handlers at
6047 * the given parent_path. The returned array should be freed with
6048 * dbus_free_string_array().
6050 * @param connection the connection
6051 * @param parent_path the path to list the child handlers of
6052 * @param child_entries returns #NULL-terminated array of children
6053 * @returns #FALSE if no memory to allocate the child entries
6056 dbus_connection_list_registered (DBusConnection *connection,
6057 const char *parent_path,
6058 char ***child_entries)
6060 char **decomposed_path;
6062 _dbus_return_val_if_fail (connection != NULL, FALSE);
6063 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
6064 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
6065 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
6067 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
6070 CONNECTION_LOCK (connection);
6072 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
6073 (const char **) decomposed_path,
6075 dbus_free_string_array (decomposed_path);
6080 static DBusDataSlotAllocator slot_allocator =
6081 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (connection_slots));
6084 * Allocates an integer ID to be used for storing application-specific
6085 * data on any DBusConnection. The allocated ID may then be used
6086 * with dbus_connection_set_data() and dbus_connection_get_data().
6087 * The passed-in slot must be initialized to -1, and is filled in
6088 * with the slot ID. If the passed-in slot is not -1, it's assumed
6089 * to be already allocated, and its refcount is incremented.
6091 * The allocated slot is global, i.e. all DBusConnection objects will
6092 * have a slot with the given integer ID reserved.
6094 * @param slot_p address of a global variable storing the slot
6095 * @returns #FALSE on failure (no memory)
6098 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
6100 return _dbus_data_slot_allocator_alloc (&slot_allocator,
6105 * Deallocates a global ID for connection data slots.
6106 * dbus_connection_get_data() and dbus_connection_set_data() may no
6107 * longer be used with this slot. Existing data stored on existing
6108 * DBusConnection objects will be freed when the connection is
6109 * finalized, but may not be retrieved (and may only be replaced if
6110 * someone else reallocates the slot). When the refcount on the
6111 * passed-in slot reaches 0, it is set to -1.
6113 * @param slot_p address storing the slot to deallocate
6116 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
6118 _dbus_return_if_fail (*slot_p >= 0);
6120 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
6124 * Stores a pointer on a DBusConnection, along
6125 * with an optional function to be used for freeing
6126 * the data when the data is set again, or when
6127 * the connection is finalized. The slot number
6128 * must have been allocated with dbus_connection_allocate_data_slot().
6130 * @note This function does not take the
6131 * main thread lock on DBusConnection, which allows it to be
6132 * used from inside watch and timeout functions. (See the
6133 * note in docs for dbus_connection_set_watch_functions().)
6134 * A side effect of this is that you need to know there's
6135 * a reference held on the connection while invoking
6136 * dbus_connection_set_data(), or the connection could be
6137 * finalized during dbus_connection_set_data().
6139 * @param connection the connection
6140 * @param slot the slot number
6141 * @param data the data to store
6142 * @param free_data_func finalizer function for the data
6143 * @returns #TRUE if there was enough memory to store the data
6146 dbus_connection_set_data (DBusConnection *connection,
6149 DBusFreeFunction free_data_func)
6151 DBusFreeFunction old_free_func;
6155 _dbus_return_val_if_fail (connection != NULL, FALSE);
6156 _dbus_return_val_if_fail (slot >= 0, FALSE);
6158 SLOTS_LOCK (connection);
6160 retval = _dbus_data_slot_list_set (&slot_allocator,
6161 &connection->slot_list,
6162 slot, data, free_data_func,
6163 &old_free_func, &old_data);
6165 SLOTS_UNLOCK (connection);
6169 /* Do the actual free outside the connection lock */
6171 (* old_free_func) (old_data);
6178 * Retrieves data previously set with dbus_connection_set_data().
6179 * The slot must still be allocated (must not have been freed).
6181 * @note This function does not take the
6182 * main thread lock on DBusConnection, which allows it to be
6183 * used from inside watch and timeout functions. (See the
6184 * note in docs for dbus_connection_set_watch_functions().)
6185 * A side effect of this is that you need to know there's
6186 * a reference held on the connection while invoking
6187 * dbus_connection_get_data(), or the connection could be
6188 * finalized during dbus_connection_get_data().
6190 * @param connection the connection
6191 * @param slot the slot to get data from
6192 * @returns the data, or #NULL if not found
6195 dbus_connection_get_data (DBusConnection *connection,
6200 _dbus_return_val_if_fail (connection != NULL, NULL);
6201 _dbus_return_val_if_fail (slot >= 0, NULL);
6203 SLOTS_LOCK (connection);
6205 res = _dbus_data_slot_list_get (&slot_allocator,
6206 &connection->slot_list,
6209 SLOTS_UNLOCK (connection);
6215 * This function sets a global flag for whether dbus_connection_new()
6216 * will set SIGPIPE behavior to SIG_IGN.
6218 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
6221 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
6223 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
6227 * Specifies the maximum size message this connection is allowed to
6228 * receive. Larger messages will result in disconnecting the
6231 * @param connection a #DBusConnection
6232 * @param size maximum message size the connection can receive, in bytes
6235 dbus_connection_set_max_message_size (DBusConnection *connection,
6238 _dbus_return_if_fail (connection != NULL);
6240 CONNECTION_LOCK (connection);
6241 _dbus_transport_set_max_message_size (connection->transport,
6243 CONNECTION_UNLOCK (connection);
6247 * Gets the value set by dbus_connection_set_max_message_size().
6249 * @param connection the connection
6250 * @returns the max size of a single message
6253 dbus_connection_get_max_message_size (DBusConnection *connection)
6257 _dbus_return_val_if_fail (connection != NULL, 0);
6259 CONNECTION_LOCK (connection);
6260 res = _dbus_transport_get_max_message_size (connection->transport);
6261 CONNECTION_UNLOCK (connection);
6266 * Specifies the maximum number of unix fds a message on this
6267 * connection is allowed to receive. Messages with more unix fds will
6268 * result in disconnecting the connection.
6270 * @param connection a #DBusConnection
6271 * @param n maximum message unix fds the connection can receive
6274 dbus_connection_set_max_message_unix_fds (DBusConnection *connection,
6277 _dbus_return_if_fail (connection != NULL);
6279 CONNECTION_LOCK (connection);
6280 _dbus_transport_set_max_message_unix_fds (connection->transport,
6282 CONNECTION_UNLOCK (connection);
6286 * Gets the value set by dbus_connection_set_max_message_unix_fds().
6288 * @param connection the connection
6289 * @returns the max numer of unix fds of a single message
6292 dbus_connection_get_max_message_unix_fds (DBusConnection *connection)
6296 _dbus_return_val_if_fail (connection != NULL, 0);
6298 CONNECTION_LOCK (connection);
6299 res = _dbus_transport_get_max_message_unix_fds (connection->transport);
6300 CONNECTION_UNLOCK (connection);
6305 * Sets the maximum total number of bytes that can be used for all messages
6306 * received on this connection. Messages count toward the maximum until
6307 * they are finalized. When the maximum is reached, the connection will
6308 * not read more data until some messages are finalized.
6310 * The semantics of the maximum are: if outstanding messages are
6311 * already above the maximum, additional messages will not be read.
6312 * The semantics are not: if the next message would cause us to exceed
6313 * the maximum, we don't read it. The reason is that we don't know the
6314 * size of a message until after we read it.
6316 * Thus, the max live messages size can actually be exceeded
6317 * by up to the maximum size of a single message.
6319 * Also, if we read say 1024 bytes off the wire in a single read(),
6320 * and that contains a half-dozen small messages, we may exceed the
6321 * size max by that amount. But this should be inconsequential.
6323 * This does imply that we can't call read() with a buffer larger
6324 * than we're willing to exceed this limit by.
6326 * @param connection the connection
6327 * @param size the maximum size in bytes of all outstanding messages
6330 dbus_connection_set_max_received_size (DBusConnection *connection,
6333 _dbus_return_if_fail (connection != NULL);
6335 CONNECTION_LOCK (connection);
6336 _dbus_transport_set_max_received_size (connection->transport,
6338 CONNECTION_UNLOCK (connection);
6342 * Gets the value set by dbus_connection_set_max_received_size().
6344 * @param connection the connection
6345 * @returns the max size of all live messages
6348 dbus_connection_get_max_received_size (DBusConnection *connection)
6352 _dbus_return_val_if_fail (connection != NULL, 0);
6354 CONNECTION_LOCK (connection);
6355 res = _dbus_transport_get_max_received_size (connection->transport);
6356 CONNECTION_UNLOCK (connection);
6361 * Sets the maximum total number of unix fds that can be used for all messages
6362 * received on this connection. Messages count toward the maximum until
6363 * they are finalized. When the maximum is reached, the connection will
6364 * not read more data until some messages are finalized.
6366 * The semantics are analogous to those of dbus_connection_set_max_received_size().
6368 * @param connection the connection
6369 * @param n the maximum size in bytes of all outstanding messages
6372 dbus_connection_set_max_received_unix_fds (DBusConnection *connection,
6375 _dbus_return_if_fail (connection != NULL);
6377 CONNECTION_LOCK (connection);
6378 _dbus_transport_set_max_received_unix_fds (connection->transport,
6380 CONNECTION_UNLOCK (connection);
6384 * Gets the value set by dbus_connection_set_max_received_unix_fds().
6386 * @param connection the connection
6387 * @returns the max unix fds of all live messages
6390 dbus_connection_get_max_received_unix_fds (DBusConnection *connection)
6394 _dbus_return_val_if_fail (connection != NULL, 0);
6396 CONNECTION_LOCK (connection);
6397 res = _dbus_transport_get_max_received_unix_fds (connection->transport);
6398 CONNECTION_UNLOCK (connection);
6403 * Gets the approximate size in bytes of all messages in the outgoing
6404 * message queue. The size is approximate in that you shouldn't use
6405 * it to decide how many bytes to read off the network or anything
6406 * of that nature, as optimizations may choose to tell small white lies
6407 * to avoid performance overhead.
6409 * @param connection the connection
6410 * @returns the number of bytes that have been queued up but not sent
6413 dbus_connection_get_outgoing_size (DBusConnection *connection)
6417 _dbus_return_val_if_fail (connection != NULL, 0);
6419 CONNECTION_LOCK (connection);
6420 res = _dbus_counter_get_size_value (connection->outgoing_counter);
6421 CONNECTION_UNLOCK (connection);
6425 #ifdef DBUS_ENABLE_STATS
6427 _dbus_connection_get_stats (DBusConnection *connection,
6428 dbus_uint32_t *in_messages,
6429 dbus_uint32_t *in_bytes,
6430 dbus_uint32_t *in_fds,
6431 dbus_uint32_t *in_peak_bytes,
6432 dbus_uint32_t *in_peak_fds,
6433 dbus_uint32_t *out_messages,
6434 dbus_uint32_t *out_bytes,
6435 dbus_uint32_t *out_fds,
6436 dbus_uint32_t *out_peak_bytes,
6437 dbus_uint32_t *out_peak_fds)
6439 CONNECTION_LOCK (connection);
6441 if (in_messages != NULL)
6442 *in_messages = connection->n_incoming;
6444 _dbus_transport_get_stats (connection->transport,
6445 in_bytes, in_fds, in_peak_bytes, in_peak_fds);
6447 if (out_messages != NULL)
6448 *out_messages = connection->n_outgoing;
6450 if (out_bytes != NULL)
6451 *out_bytes = _dbus_counter_get_size_value (connection->outgoing_counter);
6453 if (out_fds != NULL)
6454 *out_fds = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
6456 if (out_peak_bytes != NULL)
6457 *out_peak_bytes = _dbus_counter_get_peak_size_value (connection->outgoing_counter);
6459 if (out_peak_fds != NULL)
6460 *out_peak_fds = _dbus_counter_get_peak_unix_fd_value (connection->outgoing_counter);
6462 CONNECTION_UNLOCK (connection);
6464 #endif /* DBUS_ENABLE_STATS */
6467 * Gets the approximate number of uni fds of all messages in the
6468 * outgoing message queue.
6470 * @param connection the connection
6471 * @returns the number of unix fds that have been queued up but not sent
6474 dbus_connection_get_outgoing_unix_fds (DBusConnection *connection)
6478 _dbus_return_val_if_fail (connection != NULL, 0);
6480 CONNECTION_LOCK (connection);
6481 res = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
6482 CONNECTION_UNLOCK (connection);
6486 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
6488 * Returns the address of the transport object of this connection
6490 * @param connection the connection
6491 * @returns the address string
6494 _dbus_connection_get_address (DBusConnection *connection)
6496 return _dbus_transport_get_address (connection->transport);