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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-shared.h"
26 #include "dbus-connection.h"
27 #include "dbus-list.h"
28 #include "dbus-timeout.h"
29 #include "dbus-transport.h"
30 #include "dbus-watch.h"
31 #include "dbus-connection-internal.h"
32 #include "dbus-pending-call-internal.h"
33 #include "dbus-list.h"
34 #include "dbus-hash.h"
35 #include "dbus-message-internal.h"
36 #include "dbus-threads.h"
37 #include "dbus-protocol.h"
38 #include "dbus-dataslot.h"
39 #include "dbus-string.h"
40 #include "dbus-pending-call.h"
41 #include "dbus-object-tree.h"
42 #include "dbus-threads-internal.h"
45 #ifdef DBUS_DISABLE_CHECKS
46 #define TOOK_LOCK_CHECK(connection)
47 #define RELEASING_LOCK_CHECK(connection)
48 #define HAVE_LOCK_CHECK(connection)
50 #define TOOK_LOCK_CHECK(connection) do { \
51 _dbus_assert (!(connection)->have_connection_lock); \
52 (connection)->have_connection_lock = TRUE; \
54 #define RELEASING_LOCK_CHECK(connection) do { \
55 _dbus_assert ((connection)->have_connection_lock); \
56 (connection)->have_connection_lock = FALSE; \
58 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
59 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
64 #define CONNECTION_LOCK(connection) do { \
65 if (TRACE_LOCKS) { _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); } \
66 _dbus_mutex_lock ((connection)->mutex); \
67 TOOK_LOCK_CHECK (connection); \
70 #define CONNECTION_UNLOCK(connection) do { \
71 if (TRACE_LOCKS) { _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); } \
72 RELEASING_LOCK_CHECK (connection); \
73 _dbus_mutex_unlock ((connection)->mutex); \
76 #define DISPATCH_STATUS_NAME(s) \
77 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
78 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
79 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
83 * @defgroup DBusConnection DBusConnection
85 * @brief Connection to another application
87 * A DBusConnection represents a connection to another
88 * application. Messages can be sent and received via this connection.
89 * The other application may be a message bus; for convenience, the
90 * function dbus_bus_get() is provided to automatically open a
91 * connection to the well-known message buses.
93 * In brief a DBusConnection is a message queue associated with some
94 * message transport mechanism such as a socket. The connection
95 * maintains a queue of incoming messages and a queue of outgoing
98 * Several functions use the following terms:
100 * <li><b>read</b> means to fill the incoming message queue by reading from the socket</li>
101 * <li><b>write</b> means to drain the outgoing queue by writing to the socket</li>
102 * <li><b>dispatch</b> means to drain the incoming queue by invoking application-provided message handlers</li>
105 * The function dbus_connection_read_write_dispatch() for example does all
106 * three of these things, offering a simple alternative to a main loop.
108 * In an application with a main loop, the read/write/dispatch
109 * operations are usually separate.
111 * The connection provides #DBusWatch and #DBusTimeout objects to
112 * the main loop. These are used to know when reading, writing, or
113 * dispatching should be performed.
115 * Incoming messages are processed
116 * by calling dbus_connection_dispatch(). dbus_connection_dispatch()
117 * runs any handlers registered for the topmost message in the message
118 * queue, then discards the message, then returns.
120 * dbus_connection_get_dispatch_status() indicates whether
121 * messages are currently in the queue that need dispatching.
122 * dbus_connection_set_dispatch_status_function() allows
123 * you to set a function to be used to monitor the dispatch status.
125 * If you're using GLib or Qt add-on libraries for D-Bus, there are
126 * special convenience APIs in those libraries that hide
127 * all the details of dispatch and watch/timeout monitoring.
128 * For example, dbus_connection_setup_with_g_main().
130 * If you aren't using these add-on libraries, but want to process
131 * messages asynchronously, you must manually call
132 * dbus_connection_set_dispatch_status_function(),
133 * dbus_connection_set_watch_functions(),
134 * dbus_connection_set_timeout_functions() providing appropriate
135 * functions to integrate the connection with your application's main
136 * loop. This can be tricky to get right; main loops are not simple.
138 * If you don't need to be asynchronous, you can ignore #DBusWatch,
139 * #DBusTimeout, and dbus_connection_dispatch(). Instead,
140 * dbus_connection_read_write_dispatch() can be used.
142 * Or, in <em>very</em> simple applications,
143 * dbus_connection_pop_message() may be all you need, allowing you to
144 * avoid setting up any handler functions (see
145 * dbus_connection_add_filter(),
146 * dbus_connection_register_object_path() for more on handlers).
148 * When you use dbus_connection_send() or one of its variants to send
149 * a message, the message is added to the outgoing queue. It's
150 * actually written to the network later; either in
151 * dbus_watch_handle() invoked by your main loop, or in
152 * dbus_connection_flush() which blocks until it can write out the
153 * entire outgoing queue. The GLib/Qt add-on libraries again
154 * handle the details here for you by setting up watch functions.
156 * When a connection is disconnected, you are guaranteed to get a
157 * signal "Disconnected" from the interface
158 * #DBUS_INTERFACE_LOCAL, path
161 * You may not drop the last reference to a #DBusConnection
162 * until that connection has been disconnected.
164 * You may dispatch the unprocessed incoming message queue even if the
165 * connection is disconnected. However, "Disconnected" will always be
166 * the last message in the queue (obviously no messages are received
167 * after disconnection).
169 * After calling dbus_threads_init(), #DBusConnection has thread
170 * locks and drops them when invoking user callbacks, so in general is
171 * transparently threadsafe. However, #DBusMessage does NOT have
172 * thread locks; you must not send the same message to multiple
173 * #DBusConnection if those connections will be used from different threads,
176 * Also, if you dispatch or pop messages from multiple threads, it
177 * may work in the sense that it won't crash, but it's tough to imagine
178 * sane results; it will be completely unpredictable which messages
179 * go to which threads.
181 * It's recommended to dispatch from a single thread.
183 * The most useful function to call from multiple threads at once
184 * is dbus_connection_send_with_reply_and_block(). That is,
185 * multiple threads can make method calls at the same time.
187 * If you aren't using threads, you can use a main loop and
188 * dbus_pending_call_set_notify() to achieve a similar result.
192 * @defgroup DBusConnectionInternals DBusConnection implementation details
193 * @ingroup DBusInternals
194 * @brief Implementation details of DBusConnection
200 * Internal struct representing a message filter function
202 typedef struct DBusMessageFilter DBusMessageFilter;
205 * Internal struct representing a message filter function
207 struct DBusMessageFilter
209 DBusAtomic refcount; /**< Reference count */
210 DBusHandleMessageFunction function; /**< Function to call to filter */
211 void *user_data; /**< User data for the function */
212 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
217 * Internals of DBusPreallocatedSend
219 struct DBusPreallocatedSend
221 DBusConnection *connection; /**< Connection we'd send the message to */
222 DBusList *queue_link; /**< Preallocated link in the queue */
223 DBusList *counter_link; /**< Preallocated link in the resource counter */
226 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
229 * Implementation details of DBusConnection. All fields are private.
231 struct DBusConnection
233 DBusAtomic refcount; /**< Reference count. */
235 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
237 DBusMutex *dispatch_mutex; /**< Protects dispatch_acquired */
238 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */
239 DBusMutex *io_path_mutex; /**< Protects io_path_acquired */
240 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */
242 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
243 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
245 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
246 * dispatch_acquired will be set by the borrower
249 int n_outgoing; /**< Length of outgoing queue. */
250 int n_incoming; /**< Length of incoming queue. */
252 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
254 DBusTransport *transport; /**< Object that sends/receives messages over network. */
255 DBusWatchList *watches; /**< Stores active watches. */
256 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
258 DBusList *filter_list; /**< List of filters. */
260 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
262 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
264 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
265 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
267 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
268 void *wakeup_main_data; /**< Application data for wakeup_main_function */
269 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
271 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
272 void *dispatch_status_data; /**< Application data for dispatch_status_function */
273 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
275 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
277 DBusList *link_cache; /**< A cache of linked list links to prevent contention
278 * for the global linked list mempool lock
280 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
282 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
284 /* These two MUST be bools and not bitfields, because they are protected by a separate lock
285 * from connection->mutex and all bitfields in a word have to be read/written together.
286 * So you can't have a different lock for different bitfields in the same word.
288 dbus_bool_t dispatch_acquired; /**< Someone has dispatch path (can drain incoming queue) */
289 dbus_bool_t io_path_acquired; /**< Someone has transport io path (can use the transport to read/write messages) */
291 unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */
293 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
295 unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */
297 unsigned int disconnected_message_arrived : 1; /**< We popped or are dispatching the disconnected message.
298 * if the disconnect_message_link is NULL then we queued it, but
299 * this flag is whether it got to the head of the queue.
301 unsigned int disconnected_message_processed : 1; /**< We did our default handling of the disconnected message,
302 * such as closing the connection.
305 #ifndef DBUS_DISABLE_CHECKS
306 unsigned int have_connection_lock : 1; /**< Used to check locking */
309 #ifndef DBUS_DISABLE_CHECKS
310 int generation; /**< _dbus_current_generation that should correspond to this connection */
314 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
315 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
316 DBusDispatchStatus new_status);
317 static void _dbus_connection_last_unref (DBusConnection *connection);
318 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
319 static void _dbus_connection_release_dispatch (DBusConnection *connection);
320 static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
321 static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection);
322 static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection);
324 static DBusMessageFilter *
325 _dbus_message_filter_ref (DBusMessageFilter *filter)
327 _dbus_assert (filter->refcount.value > 0);
328 _dbus_atomic_inc (&filter->refcount);
334 _dbus_message_filter_unref (DBusMessageFilter *filter)
336 _dbus_assert (filter->refcount.value > 0);
338 if (_dbus_atomic_dec (&filter->refcount) == 1)
340 if (filter->free_user_data_function)
341 (* filter->free_user_data_function) (filter->user_data);
348 * Acquires the connection lock.
350 * @param connection the connection.
353 _dbus_connection_lock (DBusConnection *connection)
355 CONNECTION_LOCK (connection);
359 * Releases the connection lock.
361 * @param connection the connection.
364 _dbus_connection_unlock (DBusConnection *connection)
366 CONNECTION_UNLOCK (connection);
370 * Wakes up the main loop if it is sleeping
371 * Needed if we're e.g. queueing outgoing messages
372 * on a thread while the mainloop sleeps.
374 * @param connection the connection.
377 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
379 if (connection->wakeup_main_function)
380 (*connection->wakeup_main_function) (connection->wakeup_main_data);
383 #ifdef DBUS_BUILD_TESTS
384 /* For now this function isn't used */
386 * Adds a message to the incoming message queue, returning #FALSE
387 * if there's insufficient memory to queue the message.
388 * Does not take over refcount of the message.
390 * @param connection the connection.
391 * @param message the message to queue.
392 * @returns #TRUE on success.
395 _dbus_connection_queue_received_message (DBusConnection *connection,
396 DBusMessage *message)
400 link = _dbus_list_alloc_link (message);
404 dbus_message_ref (message);
405 _dbus_connection_queue_received_message_link (connection, link);
411 * Gets the locks so we can examine them
413 * @param connection the connection.
414 * @param mutex_loc return for the location of the main mutex pointer
415 * @param dispatch_mutex_loc return location of the dispatch mutex pointer
416 * @param io_path_mutex_loc return location of the io_path mutex pointer
417 * @param dispatch_cond_loc return location of the dispatch conditional
419 * @param io_path_cond_loc return location of the io_path conditional
423 _dbus_connection_test_get_locks (DBusConnection *connection,
424 DBusMutex **mutex_loc,
425 DBusMutex **dispatch_mutex_loc,
426 DBusMutex **io_path_mutex_loc,
427 DBusCondVar **dispatch_cond_loc,
428 DBusCondVar **io_path_cond_loc)
430 *mutex_loc = connection->mutex;
431 *dispatch_mutex_loc = connection->dispatch_mutex;
432 *io_path_mutex_loc = connection->io_path_mutex;
433 *dispatch_cond_loc = connection->dispatch_cond;
434 *io_path_cond_loc = connection->io_path_cond;
439 * Adds a message-containing list link to the incoming message queue,
440 * taking ownership of the link and the message's current refcount.
441 * Cannot fail due to lack of memory.
443 * @param connection the connection.
444 * @param link the message link to queue.
447 _dbus_connection_queue_received_message_link (DBusConnection *connection,
450 DBusPendingCall *pending;
451 dbus_int32_t reply_serial;
452 DBusMessage *message;
454 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
456 _dbus_list_append_link (&connection->incoming_messages,
458 message = link->data;
460 /* If this is a reply we're waiting on, remove timeout for it */
461 reply_serial = dbus_message_get_reply_serial (message);
462 if (reply_serial != -1)
464 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
468 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
469 _dbus_connection_remove_timeout_unlocked (connection,
470 _dbus_pending_call_get_timeout_unlocked (pending));
472 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
478 connection->n_incoming += 1;
480 _dbus_connection_wakeup_mainloop (connection);
482 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
484 dbus_message_get_type (message),
485 dbus_message_get_path (message) ?
486 dbus_message_get_path (message) :
488 dbus_message_get_interface (message) ?
489 dbus_message_get_interface (message) :
491 dbus_message_get_member (message) ?
492 dbus_message_get_member (message) :
494 dbus_message_get_signature (message),
495 dbus_message_get_reply_serial (message),
497 connection->n_incoming);}
500 * Adds a link + message to the incoming message queue.
501 * Can't fail. Takes ownership of both link and message.
503 * @param connection the connection.
504 * @param link the list node and message to queue.
508 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
511 HAVE_LOCK_CHECK (connection);
513 _dbus_list_append_link (&connection->incoming_messages, link);
515 connection->n_incoming += 1;
517 _dbus_connection_wakeup_mainloop (connection);
519 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
520 link->data, connection, connection->n_incoming);
525 * Checks whether there are messages in the outgoing message queue.
526 * Called with connection lock held.
528 * @param connection the connection.
529 * @returns #TRUE if the outgoing queue is non-empty.
532 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
534 HAVE_LOCK_CHECK (connection);
535 return connection->outgoing_messages != NULL;
539 * Checks whether there are messages in the outgoing message queue.
540 * Use dbus_connection_flush() to block until all outgoing
541 * messages have been written to the underlying transport
542 * (such as a socket).
544 * @param connection the connection.
545 * @returns #TRUE if the outgoing queue is non-empty.
548 dbus_connection_has_messages_to_send (DBusConnection *connection)
552 _dbus_return_val_if_fail (connection != NULL, FALSE);
554 CONNECTION_LOCK (connection);
555 v = _dbus_connection_has_messages_to_send_unlocked (connection);
556 CONNECTION_UNLOCK (connection);
562 * Gets the next outgoing message. The message remains in the
563 * queue, and the caller does not own a reference to it.
565 * @param connection the connection.
566 * @returns the message to be sent.
569 _dbus_connection_get_message_to_send (DBusConnection *connection)
571 HAVE_LOCK_CHECK (connection);
573 return _dbus_list_get_last (&connection->outgoing_messages);
577 * Notifies the connection that a message has been sent, so the
578 * message can be removed from the outgoing queue.
579 * Called with the connection lock held.
581 * @param connection the connection.
582 * @param message the message that was sent.
585 _dbus_connection_message_sent (DBusConnection *connection,
586 DBusMessage *message)
590 HAVE_LOCK_CHECK (connection);
592 /* This can be called before we even complete authentication, since
593 * it's called on disconnect to clean up the outgoing queue.
594 * It's also called as we successfully send each message.
597 link = _dbus_list_get_last_link (&connection->outgoing_messages);
598 _dbus_assert (link != NULL);
599 _dbus_assert (link->data == message);
601 /* Save this link in the link cache */
602 _dbus_list_unlink (&connection->outgoing_messages,
604 _dbus_list_prepend_link (&connection->link_cache, link);
606 connection->n_outgoing -= 1;
608 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
610 dbus_message_get_type (message),
611 dbus_message_get_path (message) ?
612 dbus_message_get_path (message) :
614 dbus_message_get_interface (message) ?
615 dbus_message_get_interface (message) :
617 dbus_message_get_member (message) ?
618 dbus_message_get_member (message) :
620 dbus_message_get_signature (message),
621 connection, connection->n_outgoing);
623 /* Save this link in the link cache also */
624 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
626 _dbus_list_prepend_link (&connection->link_cache, link);
628 dbus_message_unref (message);
631 /** Function to be called in protected_change_watch() with refcount held */
632 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
634 /** Function to be called in protected_change_watch() with refcount held */
635 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
637 /** Function to be called in protected_change_watch() with refcount held */
638 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
640 dbus_bool_t enabled);
643 protected_change_watch (DBusConnection *connection,
645 DBusWatchAddFunction add_function,
646 DBusWatchRemoveFunction remove_function,
647 DBusWatchToggleFunction toggle_function,
650 DBusWatchList *watches;
653 HAVE_LOCK_CHECK (connection);
655 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
656 * drop lock and call out" one; but it has to be propagated up through all callers
659 watches = connection->watches;
662 connection->watches = NULL;
663 _dbus_connection_ref_unlocked (connection);
664 CONNECTION_UNLOCK (connection);
667 retval = (* add_function) (watches, watch);
668 else if (remove_function)
671 (* remove_function) (watches, watch);
676 (* toggle_function) (watches, watch, enabled);
679 CONNECTION_LOCK (connection);
680 connection->watches = watches;
681 _dbus_connection_unref_unlocked (connection);
691 * Adds a watch using the connection's DBusAddWatchFunction if
692 * available. Otherwise records the watch to be added when said
693 * function is available. Also re-adds the watch if the
694 * DBusAddWatchFunction changes. May fail due to lack of memory.
695 * Connection lock should be held when calling this.
697 * @param connection the connection.
698 * @param watch the watch to add.
699 * @returns #TRUE on success.
702 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
705 return protected_change_watch (connection, watch,
706 _dbus_watch_list_add_watch,
711 * Removes a watch using the connection's DBusRemoveWatchFunction
712 * if available. It's an error to call this function on a watch
713 * that was not previously added.
714 * Connection lock should be held when calling this.
716 * @param connection the connection.
717 * @param watch the watch to remove.
720 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
723 protected_change_watch (connection, watch,
725 _dbus_watch_list_remove_watch,
730 * Toggles a watch and notifies app via connection's
731 * DBusWatchToggledFunction if available. It's an error to call this
732 * function on a watch that was not previously added.
733 * Connection lock should be held when calling this.
735 * @param connection the connection.
736 * @param watch the watch to toggle.
737 * @param enabled whether to enable or disable
740 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
744 _dbus_assert (watch != NULL);
746 protected_change_watch (connection, watch,
748 _dbus_watch_list_toggle_watch,
752 /** Function to be called in protected_change_timeout() with refcount held */
753 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
754 DBusTimeout *timeout);
755 /** Function to be called in protected_change_timeout() with refcount held */
756 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
757 DBusTimeout *timeout);
758 /** Function to be called in protected_change_timeout() with refcount held */
759 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
760 DBusTimeout *timeout,
761 dbus_bool_t enabled);
764 protected_change_timeout (DBusConnection *connection,
765 DBusTimeout *timeout,
766 DBusTimeoutAddFunction add_function,
767 DBusTimeoutRemoveFunction remove_function,
768 DBusTimeoutToggleFunction toggle_function,
771 DBusTimeoutList *timeouts;
774 HAVE_LOCK_CHECK (connection);
776 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
777 * drop lock and call out" one; but it has to be propagated up through all callers
780 timeouts = connection->timeouts;
783 connection->timeouts = NULL;
784 _dbus_connection_ref_unlocked (connection);
785 CONNECTION_UNLOCK (connection);
788 retval = (* add_function) (timeouts, timeout);
789 else if (remove_function)
792 (* remove_function) (timeouts, timeout);
797 (* toggle_function) (timeouts, timeout, enabled);
800 CONNECTION_LOCK (connection);
801 connection->timeouts = timeouts;
802 _dbus_connection_unref_unlocked (connection);
811 * Adds a timeout using the connection's DBusAddTimeoutFunction if
812 * available. Otherwise records the timeout to be added when said
813 * function is available. Also re-adds the timeout if the
814 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
815 * The timeout will fire repeatedly until removed.
816 * Connection lock should be held when calling this.
818 * @param connection the connection.
819 * @param timeout the timeout to add.
820 * @returns #TRUE on success.
823 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
824 DBusTimeout *timeout)
826 return protected_change_timeout (connection, timeout,
827 _dbus_timeout_list_add_timeout,
832 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
833 * if available. It's an error to call this function on a timeout
834 * that was not previously added.
835 * Connection lock should be held when calling this.
837 * @param connection the connection.
838 * @param timeout the timeout to remove.
841 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
842 DBusTimeout *timeout)
844 protected_change_timeout (connection, timeout,
846 _dbus_timeout_list_remove_timeout,
851 * Toggles a timeout and notifies app via connection's
852 * DBusTimeoutToggledFunction if available. It's an error to call this
853 * function on a timeout that was not previously added.
854 * Connection lock should be held when calling this.
856 * @param connection the connection.
857 * @param timeout the timeout to toggle.
858 * @param enabled whether to enable or disable
861 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
862 DBusTimeout *timeout,
865 protected_change_timeout (connection, timeout,
867 _dbus_timeout_list_toggle_timeout,
872 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
873 DBusPendingCall *pending)
875 dbus_uint32_t reply_serial;
876 DBusTimeout *timeout;
878 HAVE_LOCK_CHECK (connection);
880 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
882 _dbus_assert (reply_serial != 0);
884 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
886 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
889 if (!_dbus_hash_table_insert_int (connection->pending_replies,
893 _dbus_connection_remove_timeout_unlocked (connection, timeout);
895 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
896 HAVE_LOCK_CHECK (connection);
900 _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
902 _dbus_pending_call_ref_unlocked (pending);
904 HAVE_LOCK_CHECK (connection);
910 free_pending_call_on_hash_removal (void *data)
912 DBusPendingCall *pending;
913 DBusConnection *connection;
920 connection = _dbus_pending_call_get_connection_unlocked (pending);
922 HAVE_LOCK_CHECK (connection);
924 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
926 _dbus_connection_remove_timeout_unlocked (connection,
927 _dbus_pending_call_get_timeout_unlocked (pending));
929 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
932 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
933 * here, but the pending call finalizer could in principle call out to
934 * application code so we pretty much have to... some larger code reorg
937 _dbus_connection_ref_unlocked (connection);
938 _dbus_pending_call_unref_and_unlock (pending);
939 CONNECTION_LOCK (connection);
940 _dbus_connection_unref_unlocked (connection);
944 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
945 DBusPendingCall *pending)
947 /* This ends up unlocking to call the pending call finalizer, which is unexpected to
950 _dbus_hash_table_remove_int (connection->pending_replies,
951 _dbus_pending_call_get_reply_serial_unlocked (pending));
955 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
956 DBusPendingCall *pending)
958 /* The idea here is to avoid finalizing the pending call
959 * with the lock held, since there's a destroy notifier
960 * in pending call that goes out to application code.
962 * There's an extra unlock inside the hash table
963 * "free pending call" function FIXME...
965 _dbus_pending_call_ref_unlocked (pending);
966 _dbus_hash_table_remove_int (connection->pending_replies,
967 _dbus_pending_call_get_reply_serial_unlocked (pending));
968 _dbus_pending_call_unref_and_unlock (pending);
972 * Removes a pending call from the connection, such that
973 * the pending reply will be ignored. May drop the last
974 * reference to the pending call.
976 * @param connection the connection
977 * @param pending the pending call
980 _dbus_connection_remove_pending_call (DBusConnection *connection,
981 DBusPendingCall *pending)
983 CONNECTION_LOCK (connection);
984 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
988 * Acquire the transporter I/O path. This must be done before
989 * doing any I/O in the transporter. May sleep and drop the
990 * IO path mutex while waiting for the I/O path.
992 * @param connection the connection.
993 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
994 * @returns TRUE if the I/O path was acquired.
997 _dbus_connection_acquire_io_path (DBusConnection *connection,
998 int timeout_milliseconds)
1000 dbus_bool_t we_acquired;
1002 HAVE_LOCK_CHECK (connection);
1004 /* We don't want the connection to vanish */
1005 _dbus_connection_ref_unlocked (connection);
1007 /* We will only touch io_path_acquired which is protected by our mutex */
1008 CONNECTION_UNLOCK (connection);
1010 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1011 _dbus_mutex_lock (connection->io_path_mutex);
1013 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
1014 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
1016 we_acquired = FALSE;
1018 if (connection->io_path_acquired)
1020 if (timeout_milliseconds != -1)
1022 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
1023 _DBUS_FUNCTION_NAME, timeout_milliseconds);
1025 if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1026 connection->io_path_mutex,
1027 timeout_milliseconds))
1029 /* We timed out before anyone signaled. */
1030 /* (writing the loop to handle the !timedout case by
1031 * waiting longer if needed is a pain since dbus
1032 * wraps pthread_cond_timedwait to take a relative
1033 * time instead of absolute, something kind of stupid
1034 * on our part. for now it doesn't matter, we will just
1035 * end up back here eventually.)
1041 while (connection->io_path_acquired)
1043 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
1044 _dbus_condvar_wait (connection->io_path_cond,
1045 connection->io_path_mutex);
1050 if (!connection->io_path_acquired)
1053 connection->io_path_acquired = TRUE;
1056 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
1057 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
1059 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1060 _dbus_mutex_unlock (connection->io_path_mutex);
1062 CONNECTION_LOCK (connection);
1064 HAVE_LOCK_CHECK (connection);
1066 _dbus_connection_unref_unlocked (connection);
1072 * Release the I/O path when you're done with it. Only call
1073 * after you've acquired the I/O. Wakes up at most one thread
1074 * currently waiting to acquire the I/O path.
1076 * @param connection the connection.
1079 _dbus_connection_release_io_path (DBusConnection *connection)
1081 HAVE_LOCK_CHECK (connection);
1083 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1084 _dbus_mutex_lock (connection->io_path_mutex);
1086 _dbus_assert (connection->io_path_acquired);
1088 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
1089 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
1091 connection->io_path_acquired = FALSE;
1092 _dbus_condvar_wake_one (connection->io_path_cond);
1094 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1095 _dbus_mutex_unlock (connection->io_path_mutex);
1099 * Queues incoming messages and sends outgoing messages for this
1100 * connection, optionally blocking in the process. Each call to
1101 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1102 * time and then read or write data if possible.
1104 * The purpose of this function is to be able to flush outgoing
1105 * messages or queue up incoming messages without returning
1106 * control to the application and causing reentrancy weirdness.
1108 * The flags parameter allows you to specify whether to
1109 * read incoming messages, write outgoing messages, or both,
1110 * and whether to block if no immediate action is possible.
1112 * The timeout_milliseconds parameter does nothing unless the
1113 * iteration is blocking.
1115 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1116 * wasn't specified, then it's impossible to block, even if
1117 * you specify DBUS_ITERATION_BLOCK; in that case the function
1118 * returns immediately.
1120 * Called with connection lock held.
1122 * @param connection the connection.
1123 * @param flags iteration flags.
1124 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1127 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1129 int timeout_milliseconds)
1131 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1133 HAVE_LOCK_CHECK (connection);
1135 if (connection->n_outgoing == 0)
1136 flags &= ~DBUS_ITERATION_DO_WRITING;
1138 if (_dbus_connection_acquire_io_path (connection,
1139 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1141 HAVE_LOCK_CHECK (connection);
1143 _dbus_transport_do_iteration (connection->transport,
1144 flags, timeout_milliseconds);
1145 _dbus_connection_release_io_path (connection);
1148 HAVE_LOCK_CHECK (connection);
1150 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1154 * Creates a new connection for the given transport. A transport
1155 * represents a message stream that uses some concrete mechanism, such
1156 * as UNIX domain sockets. May return #NULL if insufficient
1157 * memory exists to create the connection.
1159 * @param transport the transport.
1160 * @returns the new connection, or #NULL on failure.
1163 _dbus_connection_new_for_transport (DBusTransport *transport)
1165 DBusConnection *connection;
1166 DBusWatchList *watch_list;
1167 DBusTimeoutList *timeout_list;
1168 DBusHashTable *pending_replies;
1169 DBusList *disconnect_link;
1170 DBusMessage *disconnect_message;
1171 DBusCounter *outgoing_counter;
1172 DBusObjectTree *objects;
1176 pending_replies = NULL;
1177 timeout_list = NULL;
1178 disconnect_link = NULL;
1179 disconnect_message = NULL;
1180 outgoing_counter = NULL;
1183 watch_list = _dbus_watch_list_new ();
1184 if (watch_list == NULL)
1187 timeout_list = _dbus_timeout_list_new ();
1188 if (timeout_list == NULL)
1192 _dbus_hash_table_new (DBUS_HASH_INT,
1194 (DBusFreeFunction)free_pending_call_on_hash_removal);
1195 if (pending_replies == NULL)
1198 connection = dbus_new0 (DBusConnection, 1);
1199 if (connection == NULL)
1202 _dbus_mutex_new_at_location (&connection->mutex);
1203 if (connection->mutex == NULL)
1206 _dbus_mutex_new_at_location (&connection->io_path_mutex);
1207 if (connection->io_path_mutex == NULL)
1210 _dbus_mutex_new_at_location (&connection->dispatch_mutex);
1211 if (connection->dispatch_mutex == NULL)
1214 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1215 if (connection->dispatch_cond == NULL)
1218 _dbus_condvar_new_at_location (&connection->io_path_cond);
1219 if (connection->io_path_cond == NULL)
1222 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1223 DBUS_INTERFACE_LOCAL,
1226 if (disconnect_message == NULL)
1229 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1230 if (disconnect_link == NULL)
1233 outgoing_counter = _dbus_counter_new ();
1234 if (outgoing_counter == NULL)
1237 objects = _dbus_object_tree_new (connection);
1238 if (objects == NULL)
1241 if (_dbus_modify_sigpipe)
1242 _dbus_disable_sigpipe ();
1244 connection->refcount.value = 1;
1245 connection->transport = transport;
1246 connection->watches = watch_list;
1247 connection->timeouts = timeout_list;
1248 connection->pending_replies = pending_replies;
1249 connection->outgoing_counter = outgoing_counter;
1250 connection->filter_list = NULL;
1251 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1252 connection->objects = objects;
1253 connection->exit_on_disconnect = FALSE;
1254 connection->shareable = FALSE;
1255 connection->route_peer_messages = FALSE;
1256 connection->disconnected_message_arrived = FALSE;
1257 connection->disconnected_message_processed = FALSE;
1259 #ifndef DBUS_DISABLE_CHECKS
1260 connection->generation = _dbus_current_generation;
1263 _dbus_data_slot_list_init (&connection->slot_list);
1265 connection->client_serial = 1;
1267 connection->disconnect_message_link = disconnect_link;
1269 CONNECTION_LOCK (connection);
1271 if (!_dbus_transport_set_connection (transport, connection))
1273 CONNECTION_UNLOCK (connection);
1278 _dbus_transport_ref (transport);
1280 CONNECTION_UNLOCK (connection);
1285 if (disconnect_message != NULL)
1286 dbus_message_unref (disconnect_message);
1288 if (disconnect_link != NULL)
1289 _dbus_list_free_link (disconnect_link);
1291 if (connection != NULL)
1293 _dbus_condvar_free_at_location (&connection->io_path_cond);
1294 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1295 _dbus_mutex_free_at_location (&connection->mutex);
1296 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1297 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1298 dbus_free (connection);
1300 if (pending_replies)
1301 _dbus_hash_table_unref (pending_replies);
1304 _dbus_watch_list_free (watch_list);
1307 _dbus_timeout_list_free (timeout_list);
1309 if (outgoing_counter)
1310 _dbus_counter_unref (outgoing_counter);
1313 _dbus_object_tree_unref (objects);
1319 * Increments the reference count of a DBusConnection.
1320 * Requires that the caller already holds the connection lock.
1322 * @param connection the connection.
1323 * @returns the connection.
1326 _dbus_connection_ref_unlocked (DBusConnection *connection)
1328 _dbus_assert (connection != NULL);
1329 _dbus_assert (connection->generation == _dbus_current_generation);
1331 HAVE_LOCK_CHECK (connection);
1333 #ifdef DBUS_HAVE_ATOMIC_INT
1334 _dbus_atomic_inc (&connection->refcount);
1336 _dbus_assert (connection->refcount.value > 0);
1337 connection->refcount.value += 1;
1344 * Decrements the reference count of a DBusConnection.
1345 * Requires that the caller already holds the connection lock.
1347 * @param connection the connection.
1350 _dbus_connection_unref_unlocked (DBusConnection *connection)
1352 dbus_bool_t last_unref;
1354 HAVE_LOCK_CHECK (connection);
1356 _dbus_assert (connection != NULL);
1358 /* The connection lock is better than the global
1359 * lock in the atomic increment fallback
1362 #ifdef DBUS_HAVE_ATOMIC_INT
1363 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1365 _dbus_assert (connection->refcount.value > 0);
1367 connection->refcount.value -= 1;
1368 last_unref = (connection->refcount.value == 0);
1370 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1375 _dbus_connection_last_unref (connection);
1378 static dbus_uint32_t
1379 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1383 serial = connection->client_serial++;
1385 if (connection->client_serial < 0)
1386 connection->client_serial = 1;
1392 * A callback for use with dbus_watch_new() to create a DBusWatch.
1394 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1395 * and the virtual handle_watch in DBusTransport if we got rid of it.
1396 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1399 * @param watch the watch.
1400 * @param condition the current condition of the file descriptors being watched.
1401 * @param data must be a pointer to a #DBusConnection
1402 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1405 _dbus_connection_handle_watch (DBusWatch *watch,
1406 unsigned int condition,
1409 DBusConnection *connection;
1411 DBusDispatchStatus status;
1415 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1417 CONNECTION_LOCK (connection);
1418 _dbus_connection_acquire_io_path (connection, -1);
1419 HAVE_LOCK_CHECK (connection);
1420 retval = _dbus_transport_handle_watch (connection->transport,
1423 _dbus_connection_release_io_path (connection);
1425 HAVE_LOCK_CHECK (connection);
1427 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1429 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1431 /* this calls out to user code */
1432 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1434 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1439 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1440 static DBusHashTable *shared_connections = NULL;
1443 shared_connections_shutdown (void *data)
1447 _DBUS_LOCK (shared_connections);
1449 /* This is a little bit unpleasant... better ideas? */
1450 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
1452 DBusConnection *connection;
1453 DBusMessage *message;
1456 _dbus_hash_iter_init (shared_connections, &iter);
1457 _dbus_hash_iter_next (&iter);
1459 connection = _dbus_hash_iter_get_value (&iter);
1461 _DBUS_UNLOCK (shared_connections);
1463 dbus_connection_ref (connection);
1464 _dbus_connection_close_possibly_shared (connection);
1466 /* Churn through to the Disconnected message */
1467 while ((message = dbus_connection_pop_message (connection)))
1469 dbus_message_unref (message);
1471 dbus_connection_unref (connection);
1473 _DBUS_LOCK (shared_connections);
1475 /* The connection should now be dead and not in our hash ... */
1476 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
1479 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1481 _dbus_hash_table_unref (shared_connections);
1482 shared_connections = NULL;
1484 _DBUS_UNLOCK (shared_connections);
1488 connection_lookup_shared (DBusAddressEntry *entry,
1489 DBusConnection **result)
1491 _dbus_verbose ("checking for existing connection\n");
1495 _DBUS_LOCK (shared_connections);
1497 if (shared_connections == NULL)
1499 _dbus_verbose ("creating shared_connections hash table\n");
1501 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1504 if (shared_connections == NULL)
1506 _DBUS_UNLOCK (shared_connections);
1510 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1512 _dbus_hash_table_unref (shared_connections);
1513 shared_connections = NULL;
1514 _DBUS_UNLOCK (shared_connections);
1518 _dbus_verbose (" successfully created shared_connections\n");
1520 _DBUS_UNLOCK (shared_connections);
1521 return TRUE; /* no point looking up in the hash we just made */
1527 guid = dbus_address_entry_get_value (entry, "guid");
1531 DBusConnection *connection;
1533 connection = _dbus_hash_table_lookup_string (shared_connections,
1538 /* The DBusConnection can't be finalized without taking
1539 * the shared_connections lock to remove it from the
1540 * hash. So it's safe to ref the connection here.
1541 * However, it may be disconnected if the Disconnected
1542 * message hasn't been processed yet, in which case we
1543 * want to pretend it isn't in the hash and avoid
1546 * The idea is to avoid ever returning a disconnected connection
1547 * from dbus_connection_open(). We could just synchronously
1548 * drop our shared ref to the connection on connection disconnect,
1549 * and then assert here that the connection is connected, but
1550 * that causes reentrancy headaches.
1552 CONNECTION_LOCK (connection);
1553 if (_dbus_connection_get_is_connected_unlocked (connection))
1555 _dbus_connection_ref_unlocked (connection);
1556 *result = connection;
1557 _dbus_verbose ("looked up existing connection to server guid %s\n",
1562 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1565 CONNECTION_UNLOCK (connection);
1569 _DBUS_UNLOCK (shared_connections);
1575 connection_record_shared_unlocked (DBusConnection *connection,
1579 char *guid_in_connection;
1581 HAVE_LOCK_CHECK (connection);
1582 _dbus_assert (connection->server_guid == NULL);
1583 _dbus_assert (connection->shareable);
1585 /* get a hard ref on this connection, even if
1586 * we won't in fact store it in the hash, we still
1587 * need to hold a ref on it until it's disconnected.
1589 _dbus_connection_ref_unlocked (connection);
1592 return TRUE; /* don't store in the hash */
1594 /* A separate copy of the key is required in the hash table, because
1595 * we don't have a lock on the connection when we are doing a hash
1599 guid_key = _dbus_strdup (guid);
1600 if (guid_key == NULL)
1603 guid_in_connection = _dbus_strdup (guid);
1604 if (guid_in_connection == NULL)
1606 dbus_free (guid_key);
1610 _DBUS_LOCK (shared_connections);
1611 _dbus_assert (shared_connections != NULL);
1613 if (!_dbus_hash_table_insert_string (shared_connections,
1614 guid_key, connection))
1616 dbus_free (guid_key);
1617 dbus_free (guid_in_connection);
1618 _DBUS_UNLOCK (shared_connections);
1622 connection->server_guid = guid_in_connection;
1624 _dbus_verbose ("stored connection to %s to be shared\n",
1625 connection->server_guid);
1627 _DBUS_UNLOCK (shared_connections);
1629 _dbus_assert (connection->server_guid != NULL);
1635 connection_forget_shared_unlocked (DBusConnection *connection)
1637 HAVE_LOCK_CHECK (connection);
1639 if (!connection->shareable)
1642 if (connection->server_guid != NULL)
1644 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1645 connection->server_guid);
1647 _DBUS_LOCK (shared_connections);
1649 if (!_dbus_hash_table_remove_string (shared_connections,
1650 connection->server_guid))
1651 _dbus_assert_not_reached ("connection was not in the shared table");
1653 dbus_free (connection->server_guid);
1654 connection->server_guid = NULL;
1655 _DBUS_UNLOCK (shared_connections);
1658 /* remove our reference held on all shareable connections */
1659 _dbus_connection_unref_unlocked (connection);
1662 static DBusConnection*
1663 connection_try_from_address_entry (DBusAddressEntry *entry,
1666 DBusTransport *transport;
1667 DBusConnection *connection;
1669 transport = _dbus_transport_open (entry, error);
1671 if (transport == NULL)
1673 _DBUS_ASSERT_ERROR_IS_SET (error);
1677 connection = _dbus_connection_new_for_transport (transport);
1679 _dbus_transport_unref (transport);
1681 if (connection == NULL)
1683 _DBUS_SET_OOM (error);
1687 #ifndef DBUS_DISABLE_CHECKS
1688 _dbus_assert (!connection->have_connection_lock);
1694 * If the shared parameter is true, then any existing connection will
1695 * be used (and if a new connection is created, it will be available
1696 * for use by others). If the shared parameter is false, a new
1697 * connection will always be created, and the new connection will
1698 * never be returned to other callers.
1700 * @param address the address
1701 * @param shared whether the connection is shared or private
1702 * @param error error return
1703 * @returns the connection or #NULL on error
1705 static DBusConnection*
1706 _dbus_connection_open_internal (const char *address,
1710 DBusConnection *connection;
1711 DBusAddressEntry **entries;
1712 DBusError tmp_error = DBUS_ERROR_INIT;
1713 DBusError first_error = DBUS_ERROR_INIT;
1716 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1718 _dbus_verbose ("opening %s connection to: %s\n",
1719 shared ? "shared" : "private", address);
1721 if (!dbus_parse_address (address, &entries, &len, error))
1724 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1728 for (i = 0; i < len; i++)
1732 if (!connection_lookup_shared (entries[i], &connection))
1733 _DBUS_SET_OOM (&tmp_error);
1736 if (connection == NULL)
1738 connection = connection_try_from_address_entry (entries[i],
1741 if (connection != NULL && shared)
1745 connection->shareable = TRUE;
1747 /* guid may be NULL */
1748 guid = dbus_address_entry_get_value (entries[i], "guid");
1750 CONNECTION_LOCK (connection);
1752 if (!connection_record_shared_unlocked (connection, guid))
1754 _DBUS_SET_OOM (&tmp_error);
1755 _dbus_connection_close_possibly_shared_and_unlock (connection);
1756 dbus_connection_unref (connection);
1760 CONNECTION_UNLOCK (connection);
1767 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1770 dbus_move_error (&tmp_error, &first_error);
1772 dbus_error_free (&tmp_error);
1775 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1776 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1778 if (connection == NULL)
1780 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1781 dbus_move_error (&first_error, error);
1784 dbus_error_free (&first_error);
1786 dbus_address_entries_free (entries);
1791 * Closes a shared OR private connection, while dbus_connection_close() can
1792 * only be used on private connections. Should only be called by the
1793 * dbus code that owns the connection - an owner must be known,
1794 * the open/close state is like malloc/free, not like ref/unref.
1796 * @param connection the connection
1799 _dbus_connection_close_possibly_shared (DBusConnection *connection)
1801 _dbus_assert (connection != NULL);
1802 _dbus_assert (connection->generation == _dbus_current_generation);
1804 CONNECTION_LOCK (connection);
1805 _dbus_connection_close_possibly_shared_and_unlock (connection);
1808 static DBusPreallocatedSend*
1809 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1811 DBusPreallocatedSend *preallocated;
1813 HAVE_LOCK_CHECK (connection);
1815 _dbus_assert (connection != NULL);
1817 preallocated = dbus_new (DBusPreallocatedSend, 1);
1818 if (preallocated == NULL)
1821 if (connection->link_cache != NULL)
1823 preallocated->queue_link =
1824 _dbus_list_pop_first_link (&connection->link_cache);
1825 preallocated->queue_link->data = NULL;
1829 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1830 if (preallocated->queue_link == NULL)
1834 if (connection->link_cache != NULL)
1836 preallocated->counter_link =
1837 _dbus_list_pop_first_link (&connection->link_cache);
1838 preallocated->counter_link->data = connection->outgoing_counter;
1842 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1843 if (preallocated->counter_link == NULL)
1847 _dbus_counter_ref (preallocated->counter_link->data);
1849 preallocated->connection = connection;
1851 return preallocated;
1854 _dbus_list_free_link (preallocated->queue_link);
1856 dbus_free (preallocated);
1861 /* Called with lock held, does not update dispatch status */
1863 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
1864 DBusPreallocatedSend *preallocated,
1865 DBusMessage *message,
1866 dbus_uint32_t *client_serial)
1868 dbus_uint32_t serial;
1871 preallocated->queue_link->data = message;
1872 _dbus_list_prepend_link (&connection->outgoing_messages,
1873 preallocated->queue_link);
1875 _dbus_message_add_size_counter_link (message,
1876 preallocated->counter_link);
1878 dbus_free (preallocated);
1879 preallocated = NULL;
1881 dbus_message_ref (message);
1883 connection->n_outgoing += 1;
1885 sig = dbus_message_get_signature (message);
1887 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
1889 dbus_message_get_type (message),
1890 dbus_message_get_path (message) ?
1891 dbus_message_get_path (message) :
1893 dbus_message_get_interface (message) ?
1894 dbus_message_get_interface (message) :
1896 dbus_message_get_member (message) ?
1897 dbus_message_get_member (message) :
1900 dbus_message_get_destination (message) ?
1901 dbus_message_get_destination (message) :
1904 connection->n_outgoing);
1906 if (dbus_message_get_serial (message) == 0)
1908 serial = _dbus_connection_get_next_client_serial (connection);
1909 _dbus_message_set_serial (message, serial);
1911 *client_serial = serial;
1916 *client_serial = dbus_message_get_serial (message);
1919 _dbus_verbose ("Message %p serial is %u\n",
1920 message, dbus_message_get_serial (message));
1922 _dbus_message_lock (message);
1924 /* Now we need to run an iteration to hopefully just write the messages
1925 * out immediately, and otherwise get them queued up
1927 _dbus_connection_do_iteration_unlocked (connection,
1928 DBUS_ITERATION_DO_WRITING,
1931 /* If stuff is still queued up, be sure we wake up the main loop */
1932 if (connection->n_outgoing > 0)
1933 _dbus_connection_wakeup_mainloop (connection);
1937 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
1938 DBusPreallocatedSend *preallocated,
1939 DBusMessage *message,
1940 dbus_uint32_t *client_serial)
1942 DBusDispatchStatus status;
1944 HAVE_LOCK_CHECK (connection);
1946 _dbus_connection_send_preallocated_unlocked_no_update (connection,
1948 message, client_serial);
1950 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1951 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1953 /* this calls out to user code */
1954 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1958 * Like dbus_connection_send(), but assumes the connection
1959 * is already locked on function entry, and unlocks before returning.
1961 * @param connection the connection
1962 * @param message the message to send
1963 * @param client_serial return location for client serial of sent message
1964 * @returns #FALSE on out-of-memory
1967 _dbus_connection_send_and_unlock (DBusConnection *connection,
1968 DBusMessage *message,
1969 dbus_uint32_t *client_serial)
1971 DBusPreallocatedSend *preallocated;
1973 _dbus_assert (connection != NULL);
1974 _dbus_assert (message != NULL);
1976 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
1977 if (preallocated == NULL)
1979 CONNECTION_UNLOCK (connection);
1983 _dbus_connection_send_preallocated_and_unlock (connection,
1991 * Used internally to handle the semantics of dbus_server_set_new_connection_function().
1992 * If the new connection function does not ref the connection, we want to close it.
1994 * A bit of a hack, probably the new connection function should have returned a value
1995 * for whether to close, or should have had to close the connection itself if it
1998 * But, this works OK as long as the new connection function doesn't do anything
1999 * crazy like keep the connection around without ref'ing it.
2001 * We have to lock the connection across refcount check and close in case
2002 * the new connection function spawns a thread that closes and unrefs.
2003 * In that case, if the app thread
2004 * closes and unrefs first, we'll harmlessly close again; if the app thread
2005 * still has the ref, we'll close and then the app will close harmlessly.
2006 * If the app unrefs without closing, the app is broken since if the
2007 * app refs from the new connection function it is supposed to also close.
2009 * If we didn't atomically check the refcount and close with the lock held
2010 * though, we could screw this up.
2012 * @param connection the connection
2015 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
2017 CONNECTION_LOCK (connection);
2019 _dbus_assert (connection->refcount.value > 0);
2021 if (connection->refcount.value == 1)
2022 _dbus_connection_close_possibly_shared_and_unlock (connection);
2024 CONNECTION_UNLOCK (connection);
2029 * When a function that blocks has been called with a timeout, and we
2030 * run out of memory, the time to wait for memory is based on the
2031 * timeout. If the caller was willing to block a long time we wait a
2032 * relatively long time for memory, if they were only willing to block
2033 * briefly then we retry for memory at a rapid rate.
2035 * @timeout_milliseconds the timeout requested for blocking
2038 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2040 if (timeout_milliseconds == -1)
2041 _dbus_sleep_milliseconds (1000);
2042 else if (timeout_milliseconds < 100)
2043 ; /* just busy loop */
2044 else if (timeout_milliseconds <= 1000)
2045 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2047 _dbus_sleep_milliseconds (1000);
2050 static DBusMessage *
2051 generate_local_error_message (dbus_uint32_t serial,
2055 DBusMessage *message;
2056 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
2060 if (!dbus_message_set_error_name (message, error_name))
2062 dbus_message_unref (message);
2067 dbus_message_set_no_reply (message, TRUE);
2069 if (!dbus_message_set_reply_serial (message,
2072 dbus_message_unref (message);
2077 if (error_msg != NULL)
2079 DBusMessageIter iter;
2081 dbus_message_iter_init_append (message, &iter);
2082 if (!dbus_message_iter_append_basic (&iter,
2086 dbus_message_unref (message);
2097 /* This is slightly strange since we can pop a message here without
2098 * the dispatch lock.
2101 check_for_reply_unlocked (DBusConnection *connection,
2102 dbus_uint32_t client_serial)
2106 HAVE_LOCK_CHECK (connection);
2108 link = _dbus_list_get_first_link (&connection->incoming_messages);
2110 while (link != NULL)
2112 DBusMessage *reply = link->data;
2114 if (dbus_message_get_reply_serial (reply) == client_serial)
2116 _dbus_list_remove_link (&connection->incoming_messages, link);
2117 connection->n_incoming -= 1;
2120 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2127 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2129 /* We can't iterate over the hash in the normal way since we'll be
2130 * dropping the lock for each item. So we restart the
2131 * iter each time as we drain the hash table.
2134 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2136 DBusPendingCall *pending;
2139 _dbus_hash_iter_init (connection->pending_replies, &iter);
2140 _dbus_hash_iter_next (&iter);
2142 pending = _dbus_hash_iter_get_value (&iter);
2143 _dbus_pending_call_ref_unlocked (pending);
2145 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2147 _dbus_connection_remove_timeout_unlocked (connection,
2148 _dbus_pending_call_get_timeout_unlocked (pending));
2149 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2150 _dbus_hash_iter_remove_entry (&iter);
2152 _dbus_pending_call_unref_and_unlock (pending);
2153 CONNECTION_LOCK (connection);
2155 HAVE_LOCK_CHECK (connection);
2159 complete_pending_call_and_unlock (DBusConnection *connection,
2160 DBusPendingCall *pending,
2161 DBusMessage *message)
2163 _dbus_pending_call_set_reply_unlocked (pending, message);
2164 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2165 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2167 /* Must be called unlocked since it invokes app callback */
2168 _dbus_pending_call_complete (pending);
2169 dbus_pending_call_unref (pending);
2173 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2174 DBusPendingCall *pending)
2177 DBusDispatchStatus status;
2179 reply = check_for_reply_unlocked (connection,
2180 _dbus_pending_call_get_reply_serial_unlocked (pending));
2183 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2185 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2187 complete_pending_call_and_unlock (connection, pending, reply);
2188 dbus_message_unref (reply);
2190 CONNECTION_LOCK (connection);
2191 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2192 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2193 dbus_pending_call_unref (pending);
2202 * Blocks until a pending call times out or gets a reply.
2204 * Does not re-enter the main loop or run filter/path-registered
2205 * callbacks. The reply to the message will not be seen by
2208 * Returns immediately if pending call already got a reply.
2210 * @todo could use performance improvements (it keeps scanning
2211 * the whole message queue for example)
2213 * @param pending the pending call we block for a reply on
2216 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2218 long start_tv_sec, start_tv_usec;
2219 long end_tv_sec, end_tv_usec;
2220 long tv_sec, tv_usec;
2221 DBusDispatchStatus status;
2222 DBusConnection *connection;
2223 dbus_uint32_t client_serial;
2224 int timeout_milliseconds;
2226 _dbus_assert (pending != NULL);
2228 if (dbus_pending_call_get_completed (pending))
2231 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2233 connection = _dbus_pending_call_get_connection_and_lock (pending);
2235 /* Flush message queue - note, can affect dispatch status */
2236 _dbus_connection_flush_unlocked (connection);
2238 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2240 /* note that timeout_milliseconds is limited to a smallish value
2241 * in _dbus_pending_call_new() so overflows aren't possible
2244 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending));
2246 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2247 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2248 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2249 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2250 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2252 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec to %ld sec %ld usec\n",
2253 timeout_milliseconds,
2255 start_tv_sec, start_tv_usec,
2256 end_tv_sec, end_tv_usec);
2258 /* check to see if we already got the data off the socket */
2259 /* from another blocked pending call */
2260 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2263 /* Now we wait... */
2264 /* always block at least once as we know we don't have the reply yet */
2265 _dbus_connection_do_iteration_unlocked (connection,
2266 DBUS_ITERATION_DO_READING |
2267 DBUS_ITERATION_BLOCK,
2268 timeout_milliseconds);
2272 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2274 HAVE_LOCK_CHECK (connection);
2276 /* queue messages and get status */
2278 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2280 /* the get_completed() is in case a dispatch() while we were blocking
2281 * got the reply instead of us.
2283 if (_dbus_pending_call_get_completed_unlocked (pending))
2285 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2286 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2287 dbus_pending_call_unref (pending);
2291 if (status == DBUS_DISPATCH_DATA_REMAINS)
2293 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2297 _dbus_get_current_time (&tv_sec, &tv_usec);
2299 if (!_dbus_connection_get_is_connected_unlocked (connection))
2301 DBusMessage *error_msg;
2303 error_msg = generate_local_error_message (client_serial,
2304 DBUS_ERROR_DISCONNECTED,
2305 "Connection was disconnected before a reply was received");
2307 /* on OOM error_msg is set to NULL */
2308 complete_pending_call_and_unlock (connection, pending, error_msg);
2309 dbus_pending_call_unref (pending);
2312 else if (tv_sec < start_tv_sec)
2313 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2314 else if (connection->disconnect_message_link == NULL)
2315 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2316 else if (tv_sec < end_tv_sec ||
2317 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2319 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2320 (end_tv_usec - tv_usec) / 1000;
2321 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2322 _dbus_assert (timeout_milliseconds >= 0);
2324 if (status == DBUS_DISPATCH_NEED_MEMORY)
2326 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2327 * we may already have a reply in the buffer and just can't process
2330 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2332 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2336 /* block again, we don't have the reply buffered yet. */
2337 _dbus_connection_do_iteration_unlocked (connection,
2338 DBUS_ITERATION_DO_READING |
2339 DBUS_ITERATION_BLOCK,
2340 timeout_milliseconds);
2343 goto recheck_status;
2346 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2347 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2349 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
2351 /* unlock and call user code */
2352 complete_pending_call_and_unlock (connection, pending, NULL);
2354 /* update user code on dispatch status */
2355 CONNECTION_LOCK (connection);
2356 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2357 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2358 dbus_pending_call_unref (pending);
2364 * @addtogroup DBusConnection
2370 * Gets a connection to a remote address. If a connection to the given
2371 * address already exists, returns the existing connection with its
2372 * reference count incremented. Otherwise, returns a new connection
2373 * and saves the new connection for possible re-use if a future call
2374 * to dbus_connection_open() asks to connect to the same server.
2376 * Use dbus_connection_open_private() to get a dedicated connection
2377 * not shared with other callers of dbus_connection_open().
2379 * If the open fails, the function returns #NULL, and provides a
2380 * reason for the failure in the error parameter. Pass #NULL for the
2381 * error parameter if you aren't interested in the reason for
2384 * Because this connection is shared, no user of the connection
2385 * may call dbus_connection_close(). However, when you are done with the
2386 * connection you should call dbus_connection_unref().
2388 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2389 * unless you have good reason; connections are expensive enough
2390 * that it's wasteful to create lots of connections to the same
2393 * @param address the address.
2394 * @param error address where an error can be returned.
2395 * @returns new connection, or #NULL on failure.
2398 dbus_connection_open (const char *address,
2401 DBusConnection *connection;
2403 _dbus_return_val_if_fail (address != NULL, NULL);
2404 _dbus_return_val_if_error_is_set (error, NULL);
2406 connection = _dbus_connection_open_internal (address,
2414 * Opens a new, dedicated connection to a remote address. Unlike
2415 * dbus_connection_open(), always creates a new connection.
2416 * This connection will not be saved or recycled by libdbus.
2418 * If the open fails, the function returns #NULL, and provides a
2419 * reason for the failure in the error parameter. Pass #NULL for the
2420 * error parameter if you aren't interested in the reason for
2423 * When you are done with this connection, you must
2424 * dbus_connection_close() to disconnect it,
2425 * and dbus_connection_unref() to free the connection object.
2427 * (The dbus_connection_close() can be skipped if the
2428 * connection is already known to be disconnected, for example
2429 * if you are inside a handler for the Disconnected signal.)
2431 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2432 * unless you have good reason; connections are expensive enough
2433 * that it's wasteful to create lots of connections to the same
2436 * @param address the address.
2437 * @param error address where an error can be returned.
2438 * @returns new connection, or #NULL on failure.
2441 dbus_connection_open_private (const char *address,
2444 DBusConnection *connection;
2446 _dbus_return_val_if_fail (address != NULL, NULL);
2447 _dbus_return_val_if_error_is_set (error, NULL);
2449 connection = _dbus_connection_open_internal (address,
2457 * Increments the reference count of a DBusConnection.
2459 * @param connection the connection.
2460 * @returns the connection.
2463 dbus_connection_ref (DBusConnection *connection)
2465 _dbus_return_val_if_fail (connection != NULL, NULL);
2466 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
2468 /* The connection lock is better than the global
2469 * lock in the atomic increment fallback
2472 #ifdef DBUS_HAVE_ATOMIC_INT
2473 _dbus_atomic_inc (&connection->refcount);
2475 CONNECTION_LOCK (connection);
2476 _dbus_assert (connection->refcount.value > 0);
2478 connection->refcount.value += 1;
2479 CONNECTION_UNLOCK (connection);
2486 free_outgoing_message (void *element,
2489 DBusMessage *message = element;
2490 DBusConnection *connection = data;
2492 _dbus_message_remove_size_counter (message,
2493 connection->outgoing_counter,
2495 dbus_message_unref (message);
2498 /* This is run without the mutex held, but after the last reference
2499 * to the connection has been dropped we should have no thread-related
2503 _dbus_connection_last_unref (DBusConnection *connection)
2507 _dbus_verbose ("Finalizing connection %p\n", connection);
2509 _dbus_assert (connection->refcount.value == 0);
2511 /* You have to disconnect the connection before unref:ing it. Otherwise
2512 * you won't get the disconnected message.
2514 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
2515 _dbus_assert (connection->server_guid == NULL);
2517 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
2518 _dbus_object_tree_free_all_unlocked (connection->objects);
2520 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
2521 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
2522 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
2524 _dbus_watch_list_free (connection->watches);
2525 connection->watches = NULL;
2527 _dbus_timeout_list_free (connection->timeouts);
2528 connection->timeouts = NULL;
2530 _dbus_data_slot_list_free (&connection->slot_list);
2532 link = _dbus_list_get_first_link (&connection->filter_list);
2533 while (link != NULL)
2535 DBusMessageFilter *filter = link->data;
2536 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
2538 filter->function = NULL;
2539 _dbus_message_filter_unref (filter); /* calls app callback */
2544 _dbus_list_clear (&connection->filter_list);
2546 /* ---- Done with stuff that invokes application callbacks */
2548 _dbus_object_tree_unref (connection->objects);
2550 _dbus_hash_table_unref (connection->pending_replies);
2551 connection->pending_replies = NULL;
2553 _dbus_list_clear (&connection->filter_list);
2555 _dbus_list_foreach (&connection->outgoing_messages,
2556 free_outgoing_message,
2558 _dbus_list_clear (&connection->outgoing_messages);
2560 _dbus_list_foreach (&connection->incoming_messages,
2561 (DBusForeachFunction) dbus_message_unref,
2563 _dbus_list_clear (&connection->incoming_messages);
2565 _dbus_counter_unref (connection->outgoing_counter);
2567 _dbus_transport_unref (connection->transport);
2569 if (connection->disconnect_message_link)
2571 DBusMessage *message = connection->disconnect_message_link->data;
2572 dbus_message_unref (message);
2573 _dbus_list_free_link (connection->disconnect_message_link);
2576 _dbus_list_clear (&connection->link_cache);
2578 _dbus_condvar_free_at_location (&connection->dispatch_cond);
2579 _dbus_condvar_free_at_location (&connection->io_path_cond);
2581 _dbus_mutex_free_at_location (&connection->io_path_mutex);
2582 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
2584 _dbus_mutex_free_at_location (&connection->mutex);
2586 dbus_free (connection);
2590 * Decrements the reference count of a DBusConnection, and finalizes
2591 * it if the count reaches zero.
2593 * Note: it is a bug to drop the last reference to a connection that
2594 * is still connected.
2596 * For shared connections, libdbus will own a reference
2597 * as long as the connection is connected, so you can know that either
2598 * you don't have the last reference, or it's OK to drop the last reference.
2599 * Most connections are shared. dbus_connection_open() and dbus_bus_get()
2600 * return shared connections.
2602 * For private connections, the creator of the connection must arrange for
2603 * dbus_connection_close() to be called prior to dropping the last reference.
2604 * Private connections come from dbus_connection_open_private() or dbus_bus_get_private().
2606 * @param connection the connection.
2609 dbus_connection_unref (DBusConnection *connection)
2611 dbus_bool_t last_unref;
2613 _dbus_return_if_fail (connection != NULL);
2614 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2616 /* The connection lock is better than the global
2617 * lock in the atomic increment fallback
2620 #ifdef DBUS_HAVE_ATOMIC_INT
2621 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
2623 CONNECTION_LOCK (connection);
2625 _dbus_assert (connection->refcount.value > 0);
2627 connection->refcount.value -= 1;
2628 last_unref = (connection->refcount.value == 0);
2631 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
2634 CONNECTION_UNLOCK (connection);
2639 #ifndef DBUS_DISABLE_CHECKS
2640 if (_dbus_transport_get_is_connected (connection->transport))
2642 _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",
2643 connection->shareable ?
2644 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" :
2645 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
2649 _dbus_connection_last_unref (connection);
2654 * Note that the transport can disconnect itself (other end drops us)
2655 * and in that case this function never runs. So this function must
2656 * not do anything more than disconnect the transport and update the
2659 * If the transport self-disconnects, then we assume someone will
2660 * dispatch the connection to cause the dispatch status update.
2663 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2665 DBusDispatchStatus status;
2667 HAVE_LOCK_CHECK (connection);
2669 _dbus_verbose ("Disconnecting %p\n", connection);
2671 /* We need to ref because update_dispatch_status_and_unlock will unref
2672 * the connection if it was shared and libdbus was the only remaining
2675 _dbus_connection_ref_unlocked (connection);
2677 _dbus_transport_disconnect (connection->transport);
2679 /* This has the side effect of queuing the disconnect message link
2680 * (unless we don't have enough memory, possibly, so don't assert it).
2681 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
2682 * should never again return the newly-disconnected connection.
2684 * However, we only unref the shared connection and exit_on_disconnect when
2685 * the disconnect message reaches the head of the message queue,
2686 * NOT when it's first queued.
2688 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2690 /* This calls out to user code */
2691 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2693 /* Could also call out to user code */
2694 dbus_connection_unref (connection);
2698 * Closes a private connection, so no further data can be sent or received.
2699 * This disconnects the transport (such as a socket) underlying the
2702 * Attempts to send messages after closing a connection are safe, but will result in
2703 * error replies generated locally in libdbus.
2705 * This function does not affect the connection's reference count. It's
2706 * safe to close a connection more than once; all calls after the
2707 * first do nothing. It's impossible to "reopen" a connection, a
2708 * new connection must be created. This function may result in a call
2709 * to the DBusDispatchStatusFunction set with
2710 * dbus_connection_set_dispatch_status_function(), as the disconnect
2711 * message it generates needs to be dispatched.
2713 * If a connection is dropped by the remote application, it will
2716 * You must close a connection prior to releasing the last reference to
2717 * the connection. If you dbus_connection_unref() for the last time
2718 * without closing the connection, the results are undefined; it
2719 * is a bug in your program and libdbus will try to print a warning.
2721 * You may not close a shared connection. Connections created with
2722 * dbus_connection_open() or dbus_bus_get() are shared.
2723 * These connections are owned by libdbus, and applications should
2724 * only unref them, never close them. Applications can know it is
2725 * safe to unref these connections because libdbus will be holding a
2726 * reference as long as the connection is open. Thus, either the
2727 * connection is closed and it is OK to drop the last reference,
2728 * or the connection is open and the app knows it does not have the
2731 * Connections created with dbus_connection_open_private() or
2732 * dbus_bus_get_private() are not kept track of or referenced by
2733 * libdbus. The creator of these connections is responsible for
2734 * calling dbus_connection_close() prior to releasing the last
2735 * reference, if the connection is not already disconnected.
2737 * @param connection the private (unshared) connection to close
2740 dbus_connection_close (DBusConnection *connection)
2742 _dbus_return_if_fail (connection != NULL);
2743 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2745 CONNECTION_LOCK (connection);
2747 #ifndef DBUS_DISABLE_CHECKS
2748 if (connection->shareable)
2750 CONNECTION_UNLOCK (connection);
2752 _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
2757 _dbus_connection_close_possibly_shared_and_unlock (connection);
2761 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2763 HAVE_LOCK_CHECK (connection);
2764 return _dbus_transport_get_is_connected (connection->transport);
2768 * Gets whether the connection is currently open. A connection may
2769 * become disconnected when the remote application closes its end, or
2770 * exits; a connection may also be disconnected with
2771 * dbus_connection_close().
2773 * There are not separate states for "closed" and "disconnected," the two
2774 * terms are synonymous. This function should really be called
2775 * get_is_open() but for historical reasons is not.
2777 * @param connection the connection.
2778 * @returns #TRUE if the connection is still alive.
2781 dbus_connection_get_is_connected (DBusConnection *connection)
2785 _dbus_return_val_if_fail (connection != NULL, FALSE);
2787 CONNECTION_LOCK (connection);
2788 res = _dbus_connection_get_is_connected_unlocked (connection);
2789 CONNECTION_UNLOCK (connection);
2795 * Gets whether the connection was authenticated. (Note that
2796 * if the connection was authenticated then disconnected,
2797 * this function still returns #TRUE)
2799 * @param connection the connection
2800 * @returns #TRUE if the connection was ever authenticated
2803 dbus_connection_get_is_authenticated (DBusConnection *connection)
2807 _dbus_return_val_if_fail (connection != NULL, FALSE);
2809 CONNECTION_LOCK (connection);
2810 res = _dbus_transport_get_is_authenticated (connection->transport);
2811 CONNECTION_UNLOCK (connection);
2817 * Gets whether the connection is not authenticated as a specific
2818 * user. If the connection is not authenticated, this function
2819 * returns #TRUE, and if it is authenticated but as an anonymous user,
2820 * it returns #TRUE. If it is authenticated as a specific user, then
2821 * this returns #FALSE. (Note that if the connection was authenticated
2822 * as anonymous then disconnected, this function still returns #TRUE.)
2824 * If the connection is not anonymous, you can use
2825 * dbus_connection_get_unix_user() and
2826 * dbus_connection_get_windows_user() to see who it's authorized as.
2828 * If you want to prevent non-anonymous authorization, use
2829 * dbus_server_set_auth_mechanisms() to remove the mechanisms that
2830 * allow proving user identity (i.e. only allow the ANONYMOUS
2833 * @param connection the connection
2834 * @returns #TRUE if not authenticated or authenticated as anonymous
2837 dbus_connection_get_is_anonymous (DBusConnection *connection)
2841 _dbus_return_val_if_fail (connection != NULL, FALSE);
2843 CONNECTION_LOCK (connection);
2844 res = _dbus_transport_get_is_anonymous (connection->transport);
2845 CONNECTION_UNLOCK (connection);
2851 * Gets the ID of the server address we are authenticated to, if this
2852 * connection is on the client side. If the connection is on the
2853 * server side, this will always return #NULL - use dbus_server_get_id()
2854 * to get the ID of your own server, if you are the server side.
2856 * If a client-side connection is not authenticated yet, the ID may be
2857 * available if it was included in the server address, but may not be
2858 * available. The only way to be sure the server ID is available
2859 * is to wait for authentication to complete.
2861 * In general, each mode of connecting to a given server will have
2862 * its own ID. So for example, if the session bus daemon is listening
2863 * on UNIX domain sockets and on TCP, then each of those modalities
2864 * will have its own server ID.
2866 * If you want an ID that identifies an entire session bus, look at
2867 * dbus_bus_get_id() instead (which is just a convenience wrapper
2868 * around the org.freedesktop.DBus.GetId method invoked on the bus).
2870 * You can also get a machine ID; see dbus_get_local_machine_id() to
2871 * get the machine you are on. There isn't a convenience wrapper, but
2872 * you can invoke org.freedesktop.DBus.Peer.GetMachineId on any peer
2873 * to get the machine ID on the other end.
2875 * The D-Bus specification describes the server ID and other IDs in a
2878 * @param connection the connection
2879 * @returns the server ID or #NULL if no memory or the connection is server-side
2882 dbus_connection_get_server_id (DBusConnection *connection)
2886 _dbus_return_val_if_fail (connection != NULL, FALSE);
2888 CONNECTION_LOCK (connection);
2889 id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
2890 CONNECTION_UNLOCK (connection);
2896 * Set whether _exit() should be called when the connection receives a
2897 * disconnect signal. The call to _exit() comes after any handlers for
2898 * the disconnect signal run; handlers can cancel the exit by calling
2901 * By default, exit_on_disconnect is #FALSE; but for message bus
2902 * connections returned from dbus_bus_get() it will be toggled on
2905 * @param connection the connection
2906 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
2909 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
2910 dbus_bool_t exit_on_disconnect)
2912 _dbus_return_if_fail (connection != NULL);
2914 CONNECTION_LOCK (connection);
2915 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
2916 CONNECTION_UNLOCK (connection);
2920 * Preallocates resources needed to send a message, allowing the message
2921 * to be sent without the possibility of memory allocation failure.
2922 * Allows apps to create a future guarantee that they can send
2923 * a message regardless of memory shortages.
2925 * @param connection the connection we're preallocating for.
2926 * @returns the preallocated resources, or #NULL
2928 DBusPreallocatedSend*
2929 dbus_connection_preallocate_send (DBusConnection *connection)
2931 DBusPreallocatedSend *preallocated;
2933 _dbus_return_val_if_fail (connection != NULL, NULL);
2935 CONNECTION_LOCK (connection);
2938 _dbus_connection_preallocate_send_unlocked (connection);
2940 CONNECTION_UNLOCK (connection);
2942 return preallocated;
2946 * Frees preallocated message-sending resources from
2947 * dbus_connection_preallocate_send(). Should only
2948 * be called if the preallocated resources are not used
2949 * to send a message.
2951 * @param connection the connection
2952 * @param preallocated the resources
2955 dbus_connection_free_preallocated_send (DBusConnection *connection,
2956 DBusPreallocatedSend *preallocated)
2958 _dbus_return_if_fail (connection != NULL);
2959 _dbus_return_if_fail (preallocated != NULL);
2960 _dbus_return_if_fail (connection == preallocated->connection);
2962 _dbus_list_free_link (preallocated->queue_link);
2963 _dbus_counter_unref (preallocated->counter_link->data);
2964 _dbus_list_free_link (preallocated->counter_link);
2965 dbus_free (preallocated);
2969 * Sends a message using preallocated resources. This function cannot fail.
2970 * It works identically to dbus_connection_send() in other respects.
2971 * Preallocated resources comes from dbus_connection_preallocate_send().
2972 * This function "consumes" the preallocated resources, they need not
2973 * be freed separately.
2975 * @param connection the connection
2976 * @param preallocated the preallocated resources
2977 * @param message the message to send
2978 * @param client_serial return location for client serial assigned to the message
2981 dbus_connection_send_preallocated (DBusConnection *connection,
2982 DBusPreallocatedSend *preallocated,
2983 DBusMessage *message,
2984 dbus_uint32_t *client_serial)
2986 _dbus_return_if_fail (connection != NULL);
2987 _dbus_return_if_fail (preallocated != NULL);
2988 _dbus_return_if_fail (message != NULL);
2989 _dbus_return_if_fail (preallocated->connection == connection);
2990 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
2991 dbus_message_get_member (message) != NULL);
2992 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
2993 (dbus_message_get_interface (message) != NULL &&
2994 dbus_message_get_member (message) != NULL));
2996 CONNECTION_LOCK (connection);
2997 _dbus_connection_send_preallocated_and_unlock (connection,
2999 message, client_serial);
3003 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
3004 DBusMessage *message,
3005 dbus_uint32_t *client_serial)
3007 DBusPreallocatedSend *preallocated;
3009 _dbus_assert (connection != NULL);
3010 _dbus_assert (message != NULL);
3012 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3013 if (preallocated == NULL)
3016 _dbus_connection_send_preallocated_unlocked_no_update (connection,
3024 * Adds a message to the outgoing message queue. Does not block to
3025 * write the message to the network; that happens asynchronously. To
3026 * force the message to be written, call dbus_connection_flush().
3027 * Because this only queues the message, the only reason it can
3028 * fail is lack of memory. Even if the connection is disconnected,
3029 * no error will be returned.
3031 * If the function fails due to lack of memory, it returns #FALSE.
3032 * The function will never fail for other reasons; even if the
3033 * connection is disconnected, you can queue an outgoing message,
3034 * though obviously it won't be sent.
3036 * The message serial is used by the remote application to send a
3037 * reply; see dbus_message_get_serial() or the D-Bus specification.
3039 * @param connection the connection.
3040 * @param message the message to write.
3041 * @param serial return location for message serial, or #NULL if you don't care
3042 * @returns #TRUE on success.
3045 dbus_connection_send (DBusConnection *connection,
3046 DBusMessage *message,
3047 dbus_uint32_t *serial)
3049 _dbus_return_val_if_fail (connection != NULL, FALSE);
3050 _dbus_return_val_if_fail (message != NULL, FALSE);
3052 CONNECTION_LOCK (connection);
3054 return _dbus_connection_send_and_unlock (connection,
3060 reply_handler_timeout (void *data)
3062 DBusConnection *connection;
3063 DBusDispatchStatus status;
3064 DBusPendingCall *pending = data;
3066 connection = _dbus_pending_call_get_connection_and_lock (pending);
3068 _dbus_pending_call_queue_timeout_error_unlocked (pending,
3070 _dbus_connection_remove_timeout_unlocked (connection,
3071 _dbus_pending_call_get_timeout_unlocked (pending));
3072 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
3074 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3075 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3077 /* Unlocks, and calls out to user code */
3078 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3084 * Queues a message to send, as with dbus_connection_send(),
3085 * but also returns a #DBusPendingCall used to receive a reply to the
3086 * message. If no reply is received in the given timeout_milliseconds,
3087 * this function expires the pending reply and generates a synthetic
3088 * error reply (generated in-process, not by the remote application)
3089 * indicating that a timeout occurred.
3091 * A #DBusPendingCall will see a reply message before any filters or
3092 * registered object path handlers. See dbus_connection_dispatch() for
3093 * details on when handlers are run.
3095 * A #DBusPendingCall will always see exactly one reply message,
3096 * unless it's cancelled with dbus_pending_call_cancel().
3098 * If #NULL is passed for the pending_return, the #DBusPendingCall
3099 * will still be generated internally, and used to track
3100 * the message reply timeout. This means a timeout error will
3101 * occur if no reply arrives, unlike with dbus_connection_send().
3103 * If -1 is passed for the timeout, a sane default timeout is used. -1
3104 * is typically the best value for the timeout for this reason, unless
3105 * you want a very short or very long timeout. There is no way to
3106 * avoid a timeout entirely, other than passing INT_MAX for the
3107 * timeout to mean "very long timeout." libdbus clamps an INT_MAX
3108 * timeout down to a few hours timeout though.
3110 * @warning if the connection is disconnected, the #DBusPendingCall
3111 * will be set to #NULL, so be careful with this.
3113 * @param connection the connection
3114 * @param message the message to send
3115 * @param pending_return return location for a #DBusPendingCall object, or #NULL if connection is disconnected
3116 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3117 * @returns #FALSE if no memory, #TRUE otherwise.
3121 dbus_connection_send_with_reply (DBusConnection *connection,
3122 DBusMessage *message,
3123 DBusPendingCall **pending_return,
3124 int timeout_milliseconds)
3126 DBusPendingCall *pending;
3127 dbus_int32_t serial = -1;
3128 DBusDispatchStatus status;
3130 _dbus_return_val_if_fail (connection != NULL, FALSE);
3131 _dbus_return_val_if_fail (message != NULL, FALSE);
3132 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3135 *pending_return = NULL;
3137 CONNECTION_LOCK (connection);
3139 if (!_dbus_connection_get_is_connected_unlocked (connection))
3141 CONNECTION_UNLOCK (connection);
3143 *pending_return = NULL;
3148 pending = _dbus_pending_call_new_unlocked (connection,
3149 timeout_milliseconds,
3150 reply_handler_timeout);
3152 if (pending == NULL)
3154 CONNECTION_UNLOCK (connection);
3158 /* Assign a serial to the message */
3159 serial = dbus_message_get_serial (message);
3162 serial = _dbus_connection_get_next_client_serial (connection);
3163 _dbus_message_set_serial (message, serial);
3166 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
3169 /* Insert the serial in the pending replies hash;
3170 * hash takes a refcount on DBusPendingCall.
3171 * Also, add the timeout.
3173 if (!_dbus_connection_attach_pending_call_unlocked (connection,
3177 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
3179 _dbus_connection_detach_pending_call_and_unlock (connection,
3181 goto error_unlocked;
3185 *pending_return = pending; /* hand off refcount */
3188 _dbus_connection_detach_pending_call_unlocked (connection, pending);
3189 /* we still have a ref to the pending call in this case, we unref
3190 * after unlocking, below
3194 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3196 /* this calls out to user code */
3197 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3199 if (pending_return == NULL)
3200 dbus_pending_call_unref (pending);
3205 CONNECTION_UNLOCK (connection);
3207 dbus_pending_call_unref (pending);
3212 * Sends a message and blocks a certain time period while waiting for
3213 * a reply. This function does not reenter the main loop,
3214 * i.e. messages other than the reply are queued up but not
3215 * processed. This function is used to invoke method calls on a
3218 * If a normal reply is received, it is returned, and removed from the
3219 * incoming message queue. If it is not received, #NULL is returned
3220 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
3221 * received, it is converted to a #DBusError and returned as an error,
3222 * then the reply message is deleted and #NULL is returned. If
3223 * something else goes wrong, result is set to whatever is
3224 * appropriate, such as #DBUS_ERROR_NO_MEMORY or
3225 * #DBUS_ERROR_DISCONNECTED.
3227 * @warning While this function blocks the calling thread will not be
3228 * processing the incoming message queue. This means you can end up
3229 * deadlocked if the application you're talking to needs you to reply
3230 * to a method. To solve this, either avoid the situation, block in a
3231 * separate thread from the main connection-dispatching thread, or use
3232 * dbus_pending_call_set_notify() to avoid blocking.
3234 * @param connection the connection
3235 * @param message the message to send
3236 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3237 * @param error return location for error message
3238 * @returns the message that is the reply or #NULL with an error code if the
3242 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
3243 DBusMessage *message,
3244 int timeout_milliseconds,
3248 DBusPendingCall *pending;
3250 _dbus_return_val_if_fail (connection != NULL, NULL);
3251 _dbus_return_val_if_fail (message != NULL, NULL);
3252 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3253 _dbus_return_val_if_error_is_set (error, NULL);
3255 if (!dbus_connection_send_with_reply (connection, message,
3256 &pending, timeout_milliseconds))
3258 _DBUS_SET_OOM (error);
3262 if (pending == NULL)
3264 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
3268 dbus_pending_call_block (pending);
3270 reply = dbus_pending_call_steal_reply (pending);
3271 dbus_pending_call_unref (pending);
3273 /* call_complete_and_unlock() called from pending_call_block() should
3274 * always fill this in.
3276 _dbus_assert (reply != NULL);
3278 if (dbus_set_error_from_message (error, reply))
3280 dbus_message_unref (reply);
3288 * Blocks until the outgoing message queue is empty.
3289 * Assumes connection lock already held.
3291 * If you call this, you MUST call update_dispatch_status afterword...
3293 * @param connection the connection.
3296 _dbus_connection_flush_unlocked (DBusConnection *connection)
3298 /* We have to specify DBUS_ITERATION_DO_READING here because
3299 * otherwise we could have two apps deadlock if they are both doing
3300 * a flush(), and the kernel buffers fill up. This could change the
3303 DBusDispatchStatus status;
3305 HAVE_LOCK_CHECK (connection);
3307 while (connection->n_outgoing > 0 &&
3308 _dbus_connection_get_is_connected_unlocked (connection))
3310 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3311 HAVE_LOCK_CHECK (connection);
3312 _dbus_connection_do_iteration_unlocked (connection,
3313 DBUS_ITERATION_DO_READING |
3314 DBUS_ITERATION_DO_WRITING |
3315 DBUS_ITERATION_BLOCK,
3319 HAVE_LOCK_CHECK (connection);
3320 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3321 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3323 HAVE_LOCK_CHECK (connection);
3328 * Blocks until the outgoing message queue is empty.
3330 * @param connection the connection.
3333 dbus_connection_flush (DBusConnection *connection)
3335 /* We have to specify DBUS_ITERATION_DO_READING here because
3336 * otherwise we could have two apps deadlock if they are both doing
3337 * a flush(), and the kernel buffers fill up. This could change the
3340 DBusDispatchStatus status;
3342 _dbus_return_if_fail (connection != NULL);
3344 CONNECTION_LOCK (connection);
3346 status = _dbus_connection_flush_unlocked (connection);
3348 HAVE_LOCK_CHECK (connection);
3349 /* Unlocks and calls out to user code */
3350 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3352 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
3356 * This function implements dbus_connection_read_write_dispatch() and
3357 * dbus_connection_read_write() (they pass a different value for the
3358 * dispatch parameter).
3360 * @param connection the connection
3361 * @param timeout_milliseconds max time to block or -1 for infinite
3362 * @param dispatch dispatch new messages or leave them on the incoming queue
3363 * @returns #TRUE if the disconnect message has not been processed
3366 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3367 int timeout_milliseconds,
3368 dbus_bool_t dispatch)
3370 DBusDispatchStatus dstatus;
3371 dbus_bool_t no_progress_possible;
3373 dstatus = dbus_connection_get_dispatch_status (connection);
3375 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3377 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
3378 dbus_connection_dispatch (connection);
3379 CONNECTION_LOCK (connection);
3381 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3383 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
3384 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3385 CONNECTION_LOCK (connection);
3389 CONNECTION_LOCK (connection);
3390 if (_dbus_connection_get_is_connected_unlocked (connection))
3392 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3393 _dbus_connection_do_iteration_unlocked (connection,
3394 DBUS_ITERATION_DO_READING |
3395 DBUS_ITERATION_DO_WRITING |
3396 DBUS_ITERATION_BLOCK,
3397 timeout_milliseconds);
3401 HAVE_LOCK_CHECK (connection);
3402 /* If we can dispatch, we can make progress until the Disconnected message
3403 * has been processed; if we can only read/write, we can make progress
3404 * as long as the transport is open.
3407 no_progress_possible = connection->n_incoming == 0 &&
3408 connection->disconnect_message_link == NULL;
3410 no_progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
3411 CONNECTION_UNLOCK (connection);
3412 return !no_progress_possible; /* TRUE if we can make more progress */
3417 * This function is intended for use with applications that don't want
3418 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3419 * example usage would be:
3422 * while (dbus_connection_read_write_dispatch (connection, -1))
3423 * ; // empty loop body
3426 * In this usage you would normally have set up a filter function to look
3427 * at each message as it is dispatched. The loop terminates when the last
3428 * message from the connection (the disconnected signal) is processed.
3430 * If there are messages to dispatch, this function will
3431 * dbus_connection_dispatch() once, and return. If there are no
3432 * messages to dispatch, this function will block until it can read or
3433 * write, then read or write, then return.
3435 * The way to think of this function is that it either makes some sort
3436 * of progress, or it blocks. Note that, while it is blocked on I/O, it
3437 * cannot be interrupted (even by other threads), which makes this function
3438 * unsuitable for applications that do more than just react to received
3441 * The return value indicates whether the disconnect message has been
3442 * processed, NOT whether the connection is connected. This is
3443 * important because even after disconnecting, you want to process any
3444 * messages you received prior to the disconnect.
3446 * @param connection the connection
3447 * @param timeout_milliseconds max time to block or -1 for infinite
3448 * @returns #TRUE if the disconnect message has not been processed
3451 dbus_connection_read_write_dispatch (DBusConnection *connection,
3452 int timeout_milliseconds)
3454 _dbus_return_val_if_fail (connection != NULL, FALSE);
3455 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3456 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3460 * This function is intended for use with applications that don't want to
3461 * write a main loop and deal with #DBusWatch and #DBusTimeout. See also
3462 * dbus_connection_read_write_dispatch().
3464 * As long as the connection is open, this function will block until it can
3465 * read or write, then read or write, then return #TRUE.
3467 * If the connection is closed, the function returns #FALSE.
3469 * The return value indicates whether reading or writing is still
3470 * possible, i.e. whether the connection is connected.
3472 * Note that even after disconnection, messages may remain in the
3473 * incoming queue that need to be
3474 * processed. dbus_connection_read_write_dispatch() dispatches
3475 * incoming messages for you; with dbus_connection_read_write() you
3476 * have to arrange to drain the incoming queue yourself.
3478 * @param connection the connection
3479 * @param timeout_milliseconds max time to block or -1 for infinite
3480 * @returns #TRUE if still connected
3483 dbus_connection_read_write (DBusConnection *connection,
3484 int timeout_milliseconds)
3486 _dbus_return_val_if_fail (connection != NULL, FALSE);
3487 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3488 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3491 /* We need to call this anytime we pop the head of the queue, and then
3492 * update_dispatch_status_and_unlock needs to be called afterward
3493 * which will "process" the disconnected message and set
3494 * disconnected_message_processed.
3497 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3498 DBusMessage *head_of_queue)
3500 HAVE_LOCK_CHECK (connection);
3502 /* checking that the link is NULL is an optimization to avoid the is_signal call */
3503 if (connection->disconnect_message_link == NULL &&
3504 dbus_message_is_signal (head_of_queue,
3505 DBUS_INTERFACE_LOCAL,
3508 connection->disconnected_message_arrived = TRUE;
3513 * Returns the first-received message from the incoming message queue,
3514 * leaving it in the queue. If the queue is empty, returns #NULL.
3516 * The caller does not own a reference to the returned message, and
3517 * must either return it using dbus_connection_return_message() or
3518 * keep it after calling dbus_connection_steal_borrowed_message(). No
3519 * one can get at the message while its borrowed, so return it as
3520 * quickly as possible and don't keep a reference to it after
3521 * returning it. If you need to keep the message, make a copy of it.
3523 * dbus_connection_dispatch() will block if called while a borrowed
3524 * message is outstanding; only one piece of code can be playing with
3525 * the incoming queue at a time. This function will block if called
3526 * during a dbus_connection_dispatch().
3528 * @param connection the connection.
3529 * @returns next message in the incoming queue.
3532 dbus_connection_borrow_message (DBusConnection *connection)
3534 DBusDispatchStatus status;
3535 DBusMessage *message;
3537 _dbus_return_val_if_fail (connection != NULL, NULL);
3539 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3541 /* this is called for the side effect that it queues
3542 * up any messages from the transport
3544 status = dbus_connection_get_dispatch_status (connection);
3545 if (status != DBUS_DISPATCH_DATA_REMAINS)
3548 CONNECTION_LOCK (connection);
3550 _dbus_connection_acquire_dispatch (connection);
3552 /* While a message is outstanding, the dispatch lock is held */
3553 _dbus_assert (connection->message_borrowed == NULL);
3555 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3557 message = connection->message_borrowed;
3559 check_disconnected_message_arrived_unlocked (connection, message);
3561 /* Note that we KEEP the dispatch lock until the message is returned */
3562 if (message == NULL)
3563 _dbus_connection_release_dispatch (connection);
3565 CONNECTION_UNLOCK (connection);
3567 /* We don't update dispatch status until it's returned or stolen */
3573 * Used to return a message after peeking at it using
3574 * dbus_connection_borrow_message(). Only called if
3575 * message from dbus_connection_borrow_message() was non-#NULL.
3577 * @param connection the connection
3578 * @param message the message from dbus_connection_borrow_message()
3581 dbus_connection_return_message (DBusConnection *connection,
3582 DBusMessage *message)
3584 DBusDispatchStatus status;
3586 _dbus_return_if_fail (connection != NULL);
3587 _dbus_return_if_fail (message != NULL);
3588 _dbus_return_if_fail (message == connection->message_borrowed);
3589 _dbus_return_if_fail (connection->dispatch_acquired);
3591 CONNECTION_LOCK (connection);
3593 _dbus_assert (message == connection->message_borrowed);
3595 connection->message_borrowed = NULL;
3597 _dbus_connection_release_dispatch (connection);
3599 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3600 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3604 * Used to keep a message after peeking at it using
3605 * dbus_connection_borrow_message(). Before using this function, see
3606 * the caveats/warnings in the documentation for
3607 * dbus_connection_pop_message().
3609 * @param connection the connection
3610 * @param message the message from dbus_connection_borrow_message()
3613 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3614 DBusMessage *message)
3616 DBusMessage *pop_message;
3617 DBusDispatchStatus status;
3619 _dbus_return_if_fail (connection != NULL);
3620 _dbus_return_if_fail (message != NULL);
3621 _dbus_return_if_fail (message == connection->message_borrowed);
3622 _dbus_return_if_fail (connection->dispatch_acquired);
3624 CONNECTION_LOCK (connection);
3626 _dbus_assert (message == connection->message_borrowed);
3628 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3629 _dbus_assert (message == pop_message);
3631 connection->n_incoming -= 1;
3633 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3634 message, connection->n_incoming);
3636 connection->message_borrowed = NULL;
3638 _dbus_connection_release_dispatch (connection);
3640 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3641 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3644 /* See dbus_connection_pop_message, but requires the caller to own
3645 * the lock before calling. May drop the lock while running.
3648 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3650 HAVE_LOCK_CHECK (connection);
3652 _dbus_assert (connection->message_borrowed == NULL);
3654 if (connection->n_incoming > 0)
3658 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3659 connection->n_incoming -= 1;
3661 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3663 dbus_message_get_type (link->data),
3664 dbus_message_get_path (link->data) ?
3665 dbus_message_get_path (link->data) :
3667 dbus_message_get_interface (link->data) ?
3668 dbus_message_get_interface (link->data) :
3670 dbus_message_get_member (link->data) ?
3671 dbus_message_get_member (link->data) :
3673 dbus_message_get_signature (link->data),
3674 connection, connection->n_incoming);
3676 check_disconnected_message_arrived_unlocked (connection, link->data);
3684 /* See dbus_connection_pop_message, but requires the caller to own
3685 * the lock before calling. May drop the lock while running.
3688 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3692 HAVE_LOCK_CHECK (connection);
3694 link = _dbus_connection_pop_message_link_unlocked (connection);
3698 DBusMessage *message;
3700 message = link->data;
3702 _dbus_list_free_link (link);
3711 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3712 DBusList *message_link)
3714 HAVE_LOCK_CHECK (connection);
3716 _dbus_assert (message_link != NULL);
3717 /* You can't borrow a message while a link is outstanding */
3718 _dbus_assert (connection->message_borrowed == NULL);
3719 /* We had to have the dispatch lock across the pop/putback */
3720 _dbus_assert (connection->dispatch_acquired);
3722 _dbus_list_prepend_link (&connection->incoming_messages,
3724 connection->n_incoming += 1;
3726 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3728 dbus_message_get_type (message_link->data),
3729 dbus_message_get_interface (message_link->data) ?
3730 dbus_message_get_interface (message_link->data) :
3732 dbus_message_get_member (message_link->data) ?
3733 dbus_message_get_member (message_link->data) :
3735 dbus_message_get_signature (message_link->data),
3736 connection, connection->n_incoming);
3740 * Returns the first-received message from the incoming message queue,
3741 * removing it from the queue. The caller owns a reference to the
3742 * returned message. If the queue is empty, returns #NULL.
3744 * This function bypasses any message handlers that are registered,
3745 * and so using it is usually wrong. Instead, let the main loop invoke
3746 * dbus_connection_dispatch(). Popping messages manually is only
3747 * useful in very simple programs that don't share a #DBusConnection
3748 * with any libraries or other modules.
3750 * There is a lock that covers all ways of accessing the incoming message
3751 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3752 * dbus_connection_borrow_message(), etc. will all block while one of the others
3753 * in the group is running.
3755 * @param connection the connection.
3756 * @returns next message in the incoming queue.
3759 dbus_connection_pop_message (DBusConnection *connection)
3761 DBusMessage *message;
3762 DBusDispatchStatus status;
3764 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3766 /* this is called for the side effect that it queues
3767 * up any messages from the transport
3769 status = dbus_connection_get_dispatch_status (connection);
3770 if (status != DBUS_DISPATCH_DATA_REMAINS)
3773 CONNECTION_LOCK (connection);
3774 _dbus_connection_acquire_dispatch (connection);
3775 HAVE_LOCK_CHECK (connection);
3777 message = _dbus_connection_pop_message_unlocked (connection);
3779 _dbus_verbose ("Returning popped message %p\n", message);
3781 _dbus_connection_release_dispatch (connection);
3783 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3784 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3790 * Acquire the dispatcher. This is a separate lock so the main
3791 * connection lock can be dropped to call out to application dispatch
3794 * @param connection the connection.
3797 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3799 HAVE_LOCK_CHECK (connection);
3801 _dbus_connection_ref_unlocked (connection);
3802 CONNECTION_UNLOCK (connection);
3804 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3805 _dbus_mutex_lock (connection->dispatch_mutex);
3807 while (connection->dispatch_acquired)
3809 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3810 _dbus_condvar_wait (connection->dispatch_cond,
3811 connection->dispatch_mutex);
3814 _dbus_assert (!connection->dispatch_acquired);
3816 connection->dispatch_acquired = TRUE;
3818 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3819 _dbus_mutex_unlock (connection->dispatch_mutex);
3821 CONNECTION_LOCK (connection);
3822 _dbus_connection_unref_unlocked (connection);
3826 * Release the dispatcher when you're done with it. Only call
3827 * after you've acquired the dispatcher. Wakes up at most one
3828 * thread currently waiting to acquire the dispatcher.
3830 * @param connection the connection.
3833 _dbus_connection_release_dispatch (DBusConnection *connection)
3835 HAVE_LOCK_CHECK (connection);
3837 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3838 _dbus_mutex_lock (connection->dispatch_mutex);
3840 _dbus_assert (connection->dispatch_acquired);
3842 connection->dispatch_acquired = FALSE;
3843 _dbus_condvar_wake_one (connection->dispatch_cond);
3845 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3846 _dbus_mutex_unlock (connection->dispatch_mutex);
3850 _dbus_connection_failed_pop (DBusConnection *connection,
3851 DBusList *message_link)
3853 _dbus_list_prepend_link (&connection->incoming_messages,
3855 connection->n_incoming += 1;
3858 /* Note this may be called multiple times since we don't track whether we already did it */
3860 notify_disconnected_unlocked (DBusConnection *connection)
3862 HAVE_LOCK_CHECK (connection);
3864 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
3865 * connection from dbus_bus_get(). We make the same guarantee for
3866 * dbus_connection_open() but in a different way since we don't want to
3867 * unref right here; we instead check for connectedness before returning
3868 * the connection from the hash.
3870 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
3872 /* Dump the outgoing queue, we aren't going to be able to
3873 * send it now, and we'd like accessors like
3874 * dbus_connection_get_outgoing_size() to be accurate.
3876 if (connection->n_outgoing > 0)
3880 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3881 connection->n_outgoing);
3883 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3885 _dbus_connection_message_sent (connection, link->data);
3890 /* Note this may be called multiple times since we don't track whether we already did it */
3891 static DBusDispatchStatus
3892 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
3894 HAVE_LOCK_CHECK (connection);
3896 if (connection->disconnect_message_link != NULL)
3898 _dbus_verbose ("Sending disconnect message from %s\n",
3899 _DBUS_FUNCTION_NAME);
3901 /* If we have pending calls, queue their timeouts - we want the Disconnected
3902 * to be the last message, after these timeouts.
3904 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
3906 /* We haven't sent the disconnect message already,
3907 * and all real messages have been queued up.
3909 _dbus_connection_queue_synthesized_message_link (connection,
3910 connection->disconnect_message_link);
3911 connection->disconnect_message_link = NULL;
3913 return DBUS_DISPATCH_DATA_REMAINS;
3916 return DBUS_DISPATCH_COMPLETE;
3919 static DBusDispatchStatus
3920 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3922 HAVE_LOCK_CHECK (connection);
3924 if (connection->n_incoming > 0)
3925 return DBUS_DISPATCH_DATA_REMAINS;
3926 else if (!_dbus_transport_queue_messages (connection->transport))
3927 return DBUS_DISPATCH_NEED_MEMORY;
3930 DBusDispatchStatus status;
3931 dbus_bool_t is_connected;
3933 status = _dbus_transport_get_dispatch_status (connection->transport);
3934 is_connected = _dbus_transport_get_is_connected (connection->transport);
3936 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3937 DISPATCH_STATUS_NAME (status), is_connected);
3941 /* It's possible this would be better done by having an explicit
3942 * notification from _dbus_transport_disconnect() that would
3943 * synchronously do this, instead of waiting for the next dispatch
3944 * status check. However, probably not good to change until it causes
3947 notify_disconnected_unlocked (connection);
3949 /* I'm not sure this is needed; the idea is that we want to
3950 * queue the Disconnected only after we've read all the
3951 * messages, but if we're disconnected maybe we are guaranteed
3952 * to have read them all ?
3954 if (status == DBUS_DISPATCH_COMPLETE)
3955 status = notify_disconnected_and_dispatch_complete_unlocked (connection);
3958 if (status != DBUS_DISPATCH_COMPLETE)
3960 else if (connection->n_incoming > 0)
3961 return DBUS_DISPATCH_DATA_REMAINS;
3963 return DBUS_DISPATCH_COMPLETE;
3968 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
3969 DBusDispatchStatus new_status)
3971 dbus_bool_t changed;
3972 DBusDispatchStatusFunction function;
3975 HAVE_LOCK_CHECK (connection);
3977 _dbus_connection_ref_unlocked (connection);
3979 changed = new_status != connection->last_dispatch_status;
3981 connection->last_dispatch_status = new_status;
3983 function = connection->dispatch_status_function;
3984 data = connection->dispatch_status_data;
3986 if (connection->disconnected_message_arrived &&
3987 !connection->disconnected_message_processed)
3989 connection->disconnected_message_processed = TRUE;
3991 /* this does an unref, but we have a ref
3992 * so we should not run the finalizer here
3995 connection_forget_shared_unlocked (connection);
3997 if (connection->exit_on_disconnect)
3999 CONNECTION_UNLOCK (connection);
4001 _dbus_verbose ("Exiting on Disconnected signal\n");
4003 _dbus_assert_not_reached ("Call to exit() returned");
4007 /* We drop the lock */
4008 CONNECTION_UNLOCK (connection);
4010 if (changed && function)
4012 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
4013 connection, new_status,
4014 DISPATCH_STATUS_NAME (new_status));
4015 (* function) (connection, new_status, data);
4018 dbus_connection_unref (connection);
4022 * Gets the current state of the incoming message queue.
4023 * #DBUS_DISPATCH_DATA_REMAINS indicates that the message queue
4024 * may contain messages. #DBUS_DISPATCH_COMPLETE indicates that the
4025 * incoming queue is empty. #DBUS_DISPATCH_NEED_MEMORY indicates that
4026 * there could be data, but we can't know for sure without more
4029 * To process the incoming message queue, use dbus_connection_dispatch()
4030 * or (in rare cases) dbus_connection_pop_message().
4032 * Note, #DBUS_DISPATCH_DATA_REMAINS really means that either we
4033 * have messages in the queue, or we have raw bytes buffered up
4034 * that need to be parsed. When these bytes are parsed, they
4035 * may not add up to an entire message. Thus, it's possible
4036 * to see a status of #DBUS_DISPATCH_DATA_REMAINS but not
4037 * have a message yet.
4039 * In particular this happens on initial connection, because all sorts
4040 * of authentication protocol stuff has to be parsed before the
4041 * first message arrives.
4043 * @param connection the connection.
4044 * @returns current dispatch status
4047 dbus_connection_get_dispatch_status (DBusConnection *connection)
4049 DBusDispatchStatus status;
4051 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4053 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
4055 CONNECTION_LOCK (connection);
4057 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4059 CONNECTION_UNLOCK (connection);
4065 * Filter funtion for handling the Peer standard interface.
4067 static DBusHandlerResult
4068 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
4069 DBusMessage *message)
4071 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
4073 /* This means we're letting the bus route this message */
4074 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4076 else if (dbus_message_is_method_call (message,
4077 DBUS_INTERFACE_PEER,
4083 ret = dbus_message_new_method_return (message);
4085 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4087 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4089 dbus_message_unref (ret);
4092 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4094 return DBUS_HANDLER_RESULT_HANDLED;
4096 else if (dbus_message_is_method_call (message,
4097 DBUS_INTERFACE_PEER,
4104 ret = dbus_message_new_method_return (message);
4106 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4109 _dbus_string_init (&uuid);
4110 if (_dbus_get_local_machine_uuid_encoded (&uuid))
4112 const char *v_STRING = _dbus_string_get_const_data (&uuid);
4113 if (dbus_message_append_args (ret,
4114 DBUS_TYPE_STRING, &v_STRING,
4117 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4120 _dbus_string_free (&uuid);
4122 dbus_message_unref (ret);
4125 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4127 return DBUS_HANDLER_RESULT_HANDLED;
4129 else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
4131 /* We need to bounce anything else with this interface, otherwise apps
4132 * could start extending the interface and when we added extensions
4133 * here to DBusConnection we'd break those apps.
4139 ret = dbus_message_new_error (message,
4140 DBUS_ERROR_UNKNOWN_METHOD,
4141 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
4143 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4145 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4147 dbus_message_unref (ret);
4150 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4152 return DBUS_HANDLER_RESULT_HANDLED;
4156 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4161 * Processes all builtin filter functions
4163 * If the spec specifies a standard interface
4164 * they should be processed from this method
4166 static DBusHandlerResult
4167 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
4168 DBusMessage *message)
4170 /* We just run one filter for now but have the option to run more
4171 if the spec calls for it in the future */
4173 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
4177 * Processes any incoming data.
4179 * If there's incoming raw data that has not yet been parsed, it is
4180 * parsed, which may or may not result in adding messages to the
4183 * The incoming data buffer is filled when the connection reads from
4184 * its underlying transport (such as a socket). Reading usually
4185 * happens in dbus_watch_handle() or dbus_connection_read_write().
4187 * If there are complete messages in the incoming queue,
4188 * dbus_connection_dispatch() removes one message from the queue and
4189 * processes it. Processing has three steps.
4191 * First, any method replies are passed to #DBusPendingCall or
4192 * dbus_connection_send_with_reply_and_block() in order to
4193 * complete the pending method call.
4195 * Second, any filters registered with dbus_connection_add_filter()
4196 * are run. If any filter returns #DBUS_HANDLER_RESULT_HANDLED
4197 * then processing stops after that filter.
4199 * Third, if the message is a method call it is forwarded to
4200 * any registered object path handlers added with
4201 * dbus_connection_register_object_path() or
4202 * dbus_connection_register_fallback().
4204 * A single call to dbus_connection_dispatch() will process at most
4205 * one message; it will not clear the entire message queue.
4207 * Be careful about calling dbus_connection_dispatch() from inside a
4208 * message handler, i.e. calling dbus_connection_dispatch()
4209 * recursively. If threads have been initialized with a recursive
4210 * mutex function, then this will not deadlock; however, it can
4211 * certainly confuse your application.
4213 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
4215 * @param connection the connection
4216 * @returns dispatch status, see dbus_connection_get_dispatch_status()
4219 dbus_connection_dispatch (DBusConnection *connection)
4221 DBusMessage *message;
4222 DBusList *link, *filter_list_copy, *message_link;
4223 DBusHandlerResult result;
4224 DBusPendingCall *pending;
4225 dbus_int32_t reply_serial;
4226 DBusDispatchStatus status;
4228 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4230 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
4232 CONNECTION_LOCK (connection);
4233 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4234 if (status != DBUS_DISPATCH_DATA_REMAINS)
4236 /* unlocks and calls out to user code */
4237 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4241 /* We need to ref the connection since the callback could potentially
4242 * drop the last ref to it
4244 _dbus_connection_ref_unlocked (connection);
4246 _dbus_connection_acquire_dispatch (connection);
4247 HAVE_LOCK_CHECK (connection);
4249 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4250 if (message_link == NULL)
4252 /* another thread dispatched our stuff */
4254 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
4256 _dbus_connection_release_dispatch (connection);
4258 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4260 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4262 dbus_connection_unref (connection);
4267 message = message_link->data;
4269 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
4271 dbus_message_get_type (message),
4272 dbus_message_get_interface (message) ?
4273 dbus_message_get_interface (message) :
4275 dbus_message_get_member (message) ?
4276 dbus_message_get_member (message) :
4278 dbus_message_get_signature (message));
4280 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4282 /* Pending call handling must be first, because if you do
4283 * dbus_connection_send_with_reply_and_block() or
4284 * dbus_pending_call_block() then no handlers/filters will be run on
4285 * the reply. We want consistent semantics in the case where we
4286 * dbus_connection_dispatch() the reply.
4289 reply_serial = dbus_message_get_reply_serial (message);
4290 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4294 _dbus_verbose ("Dispatching a pending reply\n");
4295 complete_pending_call_and_unlock (connection, pending, message);
4296 pending = NULL; /* it's probably unref'd */
4298 CONNECTION_LOCK (connection);
4299 _dbus_verbose ("pending call completed in dispatch\n");
4300 result = DBUS_HANDLER_RESULT_HANDLED;
4304 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4305 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4308 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4310 _dbus_connection_release_dispatch (connection);
4311 HAVE_LOCK_CHECK (connection);
4313 _dbus_connection_failed_pop (connection, message_link);
4315 /* unlocks and calls user code */
4316 _dbus_connection_update_dispatch_status_and_unlock (connection,
4317 DBUS_DISPATCH_NEED_MEMORY);
4320 dbus_pending_call_unref (pending);
4321 dbus_connection_unref (connection);
4323 return DBUS_DISPATCH_NEED_MEMORY;
4326 _dbus_list_foreach (&filter_list_copy,
4327 (DBusForeachFunction)_dbus_message_filter_ref,
4330 /* We're still protected from dispatch() reentrancy here
4331 * since we acquired the dispatcher
4333 CONNECTION_UNLOCK (connection);
4335 link = _dbus_list_get_first_link (&filter_list_copy);
4336 while (link != NULL)
4338 DBusMessageFilter *filter = link->data;
4339 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
4341 if (filter->function == NULL)
4343 _dbus_verbose (" filter was removed in a callback function\n");
4348 _dbus_verbose (" running filter on message %p\n", message);
4349 result = (* filter->function) (connection, message, filter->user_data);
4351 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4357 _dbus_list_foreach (&filter_list_copy,
4358 (DBusForeachFunction)_dbus_message_filter_unref,
4360 _dbus_list_clear (&filter_list_copy);
4362 CONNECTION_LOCK (connection);
4364 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4366 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
4369 else if (result == DBUS_HANDLER_RESULT_HANDLED)
4371 _dbus_verbose ("filter handled message in dispatch\n");
4375 /* We're still protected from dispatch() reentrancy here
4376 * since we acquired the dispatcher
4378 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
4380 dbus_message_get_type (message),
4381 dbus_message_get_interface (message) ?
4382 dbus_message_get_interface (message) :
4384 dbus_message_get_member (message) ?
4385 dbus_message_get_member (message) :
4387 dbus_message_get_signature (message));
4389 HAVE_LOCK_CHECK (connection);
4390 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4393 CONNECTION_LOCK (connection);
4395 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4397 _dbus_verbose ("object tree handled message in dispatch\n");
4401 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
4405 DBusPreallocatedSend *preallocated;
4407 _dbus_verbose (" sending error %s\n",
4408 DBUS_ERROR_UNKNOWN_METHOD);
4410 if (!_dbus_string_init (&str))
4412 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4413 _dbus_verbose ("no memory for error string in dispatch\n");
4417 if (!_dbus_string_append_printf (&str,
4418 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
4419 dbus_message_get_member (message),
4420 dbus_message_get_signature (message),
4421 dbus_message_get_interface (message)))
4423 _dbus_string_free (&str);
4424 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4425 _dbus_verbose ("no memory for error string in dispatch\n");
4429 reply = dbus_message_new_error (message,
4430 DBUS_ERROR_UNKNOWN_METHOD,
4431 _dbus_string_get_const_data (&str));
4432 _dbus_string_free (&str);
4436 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4437 _dbus_verbose ("no memory for error reply in dispatch\n");
4441 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4443 if (preallocated == NULL)
4445 dbus_message_unref (reply);
4446 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4447 _dbus_verbose ("no memory for error send in dispatch\n");
4451 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4454 dbus_message_unref (reply);
4456 result = DBUS_HANDLER_RESULT_HANDLED;
4459 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
4460 dbus_message_get_type (message),
4461 dbus_message_get_interface (message) ?
4462 dbus_message_get_interface (message) :
4464 dbus_message_get_member (message) ?
4465 dbus_message_get_member (message) :
4467 dbus_message_get_signature (message),
4471 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4473 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
4475 /* Put message back, and we'll start over.
4476 * Yes this means handlers must be idempotent if they
4477 * don't return HANDLED; c'est la vie.
4479 _dbus_connection_putback_message_link_unlocked (connection,
4484 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
4486 _dbus_list_free_link (message_link);
4487 dbus_message_unref (message); /* don't want the message to count in max message limits
4488 * in computing dispatch status below
4492 _dbus_connection_release_dispatch (connection);
4493 HAVE_LOCK_CHECK (connection);
4495 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
4496 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4498 /* unlocks and calls user code */
4499 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4501 dbus_connection_unref (connection);
4507 * Sets the watch functions for the connection. These functions are
4508 * responsible for making the application's main loop aware of file
4509 * descriptors that need to be monitored for events, using select() or
4510 * poll(). When using Qt, typically the DBusAddWatchFunction would
4511 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
4512 * could call g_io_add_watch(), or could be used as part of a more
4513 * elaborate GSource. Note that when a watch is added, it may
4516 * The DBusWatchToggledFunction notifies the application that the
4517 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
4518 * to check this. A disabled watch should have no effect, and enabled
4519 * watch should be added to the main loop. This feature is used
4520 * instead of simply adding/removing the watch because
4521 * enabling/disabling can be done without memory allocation. The
4522 * toggled function may be NULL if a main loop re-queries
4523 * dbus_watch_get_enabled() every time anyway.
4525 * The DBusWatch can be queried for the file descriptor to watch using
4526 * dbus_watch_get_unix_fd() or dbus_watch_get_socket(), and for the
4527 * events to watch for using dbus_watch_get_flags(). The flags
4528 * returned by dbus_watch_get_flags() will only contain
4529 * DBUS_WATCH_READABLE and DBUS_WATCH_WRITABLE, never
4530 * DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; all watches implicitly
4531 * include a watch for hangups, errors, and other exceptional
4534 * Once a file descriptor becomes readable or writable, or an exception
4535 * occurs, dbus_watch_handle() should be called to
4536 * notify the connection of the file descriptor's condition.
4538 * dbus_watch_handle() cannot be called during the
4539 * DBusAddWatchFunction, as the connection will not be ready to handle
4542 * It is not allowed to reference a DBusWatch after it has been passed
4543 * to remove_function.
4545 * If #FALSE is returned due to lack of memory, the failure may be due
4546 * to a #FALSE return from the new add_function. If so, the
4547 * add_function may have been called successfully one or more times,
4548 * but the remove_function will also have been called to remove any
4549 * successful adds. i.e. if #FALSE is returned the net result
4550 * should be that dbus_connection_set_watch_functions() has no effect,
4551 * but the add_function and remove_function may have been called.
4553 * @todo We need to drop the lock when we call the
4554 * add/remove/toggled functions which can be a side effect
4555 * of setting the watch functions.
4557 * @param connection the connection.
4558 * @param add_function function to begin monitoring a new descriptor.
4559 * @param remove_function function to stop monitoring a descriptor.
4560 * @param toggled_function function to notify of enable/disable
4561 * @param data data to pass to add_function and remove_function.
4562 * @param free_data_function function to be called to free the data.
4563 * @returns #FALSE on failure (no memory)
4566 dbus_connection_set_watch_functions (DBusConnection *connection,
4567 DBusAddWatchFunction add_function,
4568 DBusRemoveWatchFunction remove_function,
4569 DBusWatchToggledFunction toggled_function,
4571 DBusFreeFunction free_data_function)
4574 DBusWatchList *watches;
4576 _dbus_return_val_if_fail (connection != NULL, FALSE);
4578 CONNECTION_LOCK (connection);
4580 #ifndef DBUS_DISABLE_CHECKS
4581 if (connection->watches == NULL)
4583 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4584 _DBUS_FUNCTION_NAME);
4589 /* ref connection for slightly better reentrancy */
4590 _dbus_connection_ref_unlocked (connection);
4592 /* This can call back into user code, and we need to drop the
4593 * connection lock when it does. This is kind of a lame
4596 watches = connection->watches;
4597 connection->watches = NULL;
4598 CONNECTION_UNLOCK (connection);
4600 retval = _dbus_watch_list_set_functions (watches,
4601 add_function, remove_function,
4603 data, free_data_function);
4604 CONNECTION_LOCK (connection);
4605 connection->watches = watches;
4607 CONNECTION_UNLOCK (connection);
4608 /* drop our paranoid refcount */
4609 dbus_connection_unref (connection);
4615 * Sets the timeout functions for the connection. These functions are
4616 * responsible for making the application's main loop aware of timeouts.
4617 * When using Qt, typically the DBusAddTimeoutFunction would create a
4618 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
4621 * The DBusTimeoutToggledFunction notifies the application that the
4622 * timeout has been enabled or disabled. Call
4623 * dbus_timeout_get_enabled() to check this. A disabled timeout should
4624 * have no effect, and enabled timeout should be added to the main
4625 * loop. This feature is used instead of simply adding/removing the
4626 * timeout because enabling/disabling can be done without memory
4627 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
4628 * to enable and disable. The toggled function may be NULL if a main
4629 * loop re-queries dbus_timeout_get_enabled() every time anyway.
4630 * Whenever a timeout is toggled, its interval may change.
4632 * The DBusTimeout can be queried for the timer interval using
4633 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
4634 * repeatedly, each time the interval elapses, starting after it has
4635 * elapsed once. The timeout stops firing when it is removed with the
4636 * given remove_function. The timer interval may change whenever the
4637 * timeout is added, removed, or toggled.
4639 * @param connection the connection.
4640 * @param add_function function to add a timeout.
4641 * @param remove_function function to remove a timeout.
4642 * @param toggled_function function to notify of enable/disable
4643 * @param data data to pass to add_function and remove_function.
4644 * @param free_data_function function to be called to free the data.
4645 * @returns #FALSE on failure (no memory)
4648 dbus_connection_set_timeout_functions (DBusConnection *connection,
4649 DBusAddTimeoutFunction add_function,
4650 DBusRemoveTimeoutFunction remove_function,
4651 DBusTimeoutToggledFunction toggled_function,
4653 DBusFreeFunction free_data_function)
4656 DBusTimeoutList *timeouts;
4658 _dbus_return_val_if_fail (connection != NULL, FALSE);
4660 CONNECTION_LOCK (connection);
4662 #ifndef DBUS_DISABLE_CHECKS
4663 if (connection->timeouts == NULL)
4665 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4666 _DBUS_FUNCTION_NAME);
4671 /* ref connection for slightly better reentrancy */
4672 _dbus_connection_ref_unlocked (connection);
4674 timeouts = connection->timeouts;
4675 connection->timeouts = NULL;
4676 CONNECTION_UNLOCK (connection);
4678 retval = _dbus_timeout_list_set_functions (timeouts,
4679 add_function, remove_function,
4681 data, free_data_function);
4682 CONNECTION_LOCK (connection);
4683 connection->timeouts = timeouts;
4685 CONNECTION_UNLOCK (connection);
4686 /* drop our paranoid refcount */
4687 dbus_connection_unref (connection);
4693 * Sets the mainloop wakeup function for the connection. This function
4694 * is responsible for waking up the main loop (if its sleeping in
4695 * another thread) when some some change has happened to the
4696 * connection that the mainloop needs to reconsider (e.g. a message
4697 * has been queued for writing). When using Qt, this typically
4698 * results in a call to QEventLoop::wakeUp(). When using GLib, it
4699 * would call g_main_context_wakeup().
4701 * @param connection the connection.
4702 * @param wakeup_main_function function to wake up the mainloop
4703 * @param data data to pass wakeup_main_function
4704 * @param free_data_function function to be called to free the data.
4707 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
4708 DBusWakeupMainFunction wakeup_main_function,
4710 DBusFreeFunction free_data_function)
4713 DBusFreeFunction old_free_data;
4715 _dbus_return_if_fail (connection != NULL);
4717 CONNECTION_LOCK (connection);
4718 old_data = connection->wakeup_main_data;
4719 old_free_data = connection->free_wakeup_main_data;
4721 connection->wakeup_main_function = wakeup_main_function;
4722 connection->wakeup_main_data = data;
4723 connection->free_wakeup_main_data = free_data_function;
4725 CONNECTION_UNLOCK (connection);
4727 /* Callback outside the lock */
4729 (*old_free_data) (old_data);
4733 * Set a function to be invoked when the dispatch status changes.
4734 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
4735 * dbus_connection_dispatch() needs to be called to process incoming
4736 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
4737 * from inside the DBusDispatchStatusFunction. Indeed, almost
4738 * any reentrancy in this function is a bad idea. Instead,
4739 * the DBusDispatchStatusFunction should simply save an indication
4740 * that messages should be dispatched later, when the main loop
4743 * If you don't set a dispatch status function, you have to be sure to
4744 * dispatch on every iteration of your main loop, especially if
4745 * dbus_watch_handle() or dbus_timeout_handle() were called.
4747 * @param connection the connection
4748 * @param function function to call on dispatch status changes
4749 * @param data data for function
4750 * @param free_data_function free the function data
4753 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
4754 DBusDispatchStatusFunction function,
4756 DBusFreeFunction free_data_function)
4759 DBusFreeFunction old_free_data;
4761 _dbus_return_if_fail (connection != NULL);
4763 CONNECTION_LOCK (connection);
4764 old_data = connection->dispatch_status_data;
4765 old_free_data = connection->free_dispatch_status_data;
4767 connection->dispatch_status_function = function;
4768 connection->dispatch_status_data = data;
4769 connection->free_dispatch_status_data = free_data_function;
4771 CONNECTION_UNLOCK (connection);
4773 /* Callback outside the lock */
4775 (*old_free_data) (old_data);
4779 * Get the UNIX file descriptor of the connection, if any. This can
4780 * be used for SELinux access control checks with getpeercon() for
4781 * example. DO NOT read or write to the file descriptor, or try to
4782 * select() on it; use DBusWatch for main loop integration. Not all
4783 * connections will have a file descriptor. So for adding descriptors
4784 * to the main loop, use dbus_watch_get_unix_fd() and so forth.
4786 * If the connection is socket-based, you can also use
4787 * dbus_connection_get_socket(), which will work on Windows too.
4788 * This function always fails on Windows.
4790 * Right now the returned descriptor is always a socket, but
4791 * that is not guaranteed.
4793 * @param connection the connection
4794 * @param fd return location for the file descriptor.
4795 * @returns #TRUE if fd is successfully obtained.
4798 dbus_connection_get_unix_fd (DBusConnection *connection,
4801 _dbus_return_val_if_fail (connection != NULL, FALSE);
4802 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4805 /* FIXME do this on a lower level */
4809 return dbus_connection_get_socket(connection, fd);
4813 * Gets the underlying Windows or UNIX socket file descriptor
4814 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
4815 * select() on it; use DBusWatch for main loop integration. Not all
4816 * connections will have a socket. So for adding descriptors
4817 * to the main loop, use dbus_watch_get_socket() and so forth.
4819 * If the connection is not socket-based, this function will return FALSE,
4820 * even if the connection does have a file descriptor of some kind.
4821 * i.e. this function always returns specifically a socket file descriptor.
4823 * @param connection the connection
4824 * @param fd return location for the file descriptor.
4825 * @returns #TRUE if fd is successfully obtained.
4828 dbus_connection_get_socket(DBusConnection *connection,
4833 _dbus_return_val_if_fail (connection != NULL, FALSE);
4834 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4836 CONNECTION_LOCK (connection);
4838 retval = _dbus_transport_get_socket_fd (connection->transport,
4841 CONNECTION_UNLOCK (connection);
4848 * Gets the UNIX user ID of the connection if known. Returns #TRUE if
4849 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
4850 * for now, though in theory someone could hook Windows to NIS or
4851 * something. Always returns #FALSE prior to authenticating the
4854 * The UID is only read by servers from clients; clients can't usually
4855 * get the UID of servers, because servers do not authenticate to
4856 * clients. The returned UID is the UID the connection authenticated
4859 * The message bus is a server and the apps connecting to the bus
4862 * You can ask the bus to tell you the UID of another connection though
4863 * if you like; this is done with dbus_bus_get_unix_user().
4865 * @param connection the connection
4866 * @param uid return location for the user ID
4867 * @returns #TRUE if uid is filled in with a valid user ID
4870 dbus_connection_get_unix_user (DBusConnection *connection,
4875 _dbus_return_val_if_fail (connection != NULL, FALSE);
4876 _dbus_return_val_if_fail (uid != NULL, FALSE);
4878 CONNECTION_LOCK (connection);
4880 if (!_dbus_transport_get_is_authenticated (connection->transport))
4883 result = _dbus_transport_get_unix_user (connection->transport,
4887 _dbus_assert (!result);
4890 CONNECTION_UNLOCK (connection);
4896 * Gets the process ID of the connection if any.
4897 * Returns #TRUE if the pid is filled in.
4898 * Always returns #FALSE prior to authenticating the
4901 * @param connection the connection
4902 * @param pid return location for the process ID
4903 * @returns #TRUE if uid is filled in with a valid process ID
4906 dbus_connection_get_unix_process_id (DBusConnection *connection,
4911 _dbus_return_val_if_fail (connection != NULL, FALSE);
4912 _dbus_return_val_if_fail (pid != NULL, FALSE);
4914 CONNECTION_LOCK (connection);
4916 if (!_dbus_transport_get_is_authenticated (connection->transport))
4919 result = _dbus_transport_get_unix_process_id (connection->transport,
4922 _dbus_assert (!result);
4925 CONNECTION_UNLOCK (connection);
4931 * Sets a predicate function used to determine whether a given user ID
4932 * is allowed to connect. When an incoming connection has
4933 * authenticated with a particular user ID, this function is called;
4934 * if it returns #TRUE, the connection is allowed to proceed,
4935 * otherwise the connection is disconnected.
4937 * If the function is set to #NULL (as it is by default), then
4938 * only the same UID as the server process will be allowed to
4939 * connect. Also, root is always allowed to connect.
4941 * On Windows, the function will be set and its free_data_function will
4942 * be invoked when the connection is freed or a new function is set.
4943 * However, the function will never be called, because there are
4944 * no UNIX user ids to pass to it, or at least none of the existing
4945 * auth protocols would allow authenticating as a UNIX user on Windows.
4947 * @param connection the connection
4948 * @param function the predicate
4949 * @param data data to pass to the predicate
4950 * @param free_data_function function to free the data
4953 dbus_connection_set_unix_user_function (DBusConnection *connection,
4954 DBusAllowUnixUserFunction function,
4956 DBusFreeFunction free_data_function)
4958 void *old_data = NULL;
4959 DBusFreeFunction old_free_function = NULL;
4961 _dbus_return_if_fail (connection != NULL);
4963 CONNECTION_LOCK (connection);
4964 _dbus_transport_set_unix_user_function (connection->transport,
4965 function, data, free_data_function,
4966 &old_data, &old_free_function);
4967 CONNECTION_UNLOCK (connection);
4969 if (old_free_function != NULL)
4970 (* old_free_function) (old_data);
4974 * Gets the Windows user SID of the connection if known. Returns
4975 * #TRUE if the ID is filled in. Always returns #FALSE on non-Windows
4976 * platforms for now, though in theory someone could hook UNIX to
4977 * Active Directory or something. Always returns #FALSE prior to
4978 * authenticating the connection.
4980 * The user is only read by servers from clients; clients can't usually
4981 * get the user of servers, because servers do not authenticate to
4982 * clients. The returned user is the user the connection authenticated
4985 * The message bus is a server and the apps connecting to the bus
4988 * The returned user string has to be freed with dbus_free().
4990 * The return value indicates whether the user SID is available;
4991 * if it's available but we don't have the memory to copy it,
4992 * then the return value is #TRUE and #NULL is given as the SID.
4994 * @todo We would like to be able to say "You can ask the bus to tell
4995 * you the user of another connection though if you like; this is done
4996 * with dbus_bus_get_windows_user()." But this has to be implemented
4997 * in bus/driver.c and dbus/dbus-bus.c, and is pointless anyway
4998 * since on Windows we only use the session bus for now.
5000 * @param connection the connection
5001 * @param windows_sid_p return location for an allocated copy of the user ID, or #NULL if no memory
5002 * @returns #TRUE if user is available (returned value may be #NULL anyway if no memory)
5005 dbus_connection_get_windows_user (DBusConnection *connection,
5006 char **windows_sid_p)
5010 _dbus_return_val_if_fail (connection != NULL, FALSE);
5011 _dbus_return_val_if_fail (windows_sid_p != NULL, FALSE);
5013 CONNECTION_LOCK (connection);
5015 if (!_dbus_transport_get_is_authenticated (connection->transport))
5018 result = _dbus_transport_get_windows_user (connection->transport,
5022 _dbus_assert (!result);
5025 CONNECTION_UNLOCK (connection);
5031 * Sets a predicate function used to determine whether a given user ID
5032 * is allowed to connect. When an incoming connection has
5033 * authenticated with a particular user ID, this function is called;
5034 * if it returns #TRUE, the connection is allowed to proceed,
5035 * otherwise the connection is disconnected.
5037 * If the function is set to #NULL (as it is by default), then
5038 * only the same user owning the server process will be allowed to
5041 * On UNIX, the function will be set and its free_data_function will
5042 * be invoked when the connection is freed or a new function is set.
5043 * However, the function will never be called, because there is no
5044 * way right now to authenticate as a Windows user on UNIX.
5046 * @param connection the connection
5047 * @param function the predicate
5048 * @param data data to pass to the predicate
5049 * @param free_data_function function to free the data
5052 dbus_connection_set_windows_user_function (DBusConnection *connection,
5053 DBusAllowWindowsUserFunction function,
5055 DBusFreeFunction free_data_function)
5057 void *old_data = NULL;
5058 DBusFreeFunction old_free_function = NULL;
5060 _dbus_return_if_fail (connection != NULL);
5062 CONNECTION_LOCK (connection);
5063 _dbus_transport_set_windows_user_function (connection->transport,
5064 function, data, free_data_function,
5065 &old_data, &old_free_function);
5066 CONNECTION_UNLOCK (connection);
5068 if (old_free_function != NULL)
5069 (* old_free_function) (old_data);
5073 * This function must be called on the server side of a connection when the
5074 * connection is first seen in the #DBusNewConnectionFunction. If set to
5075 * #TRUE (the default is #FALSE), then the connection can proceed even if
5076 * the client does not authenticate as some user identity, i.e. clients
5077 * can connect anonymously.
5079 * This setting interacts with the available authorization mechanisms
5080 * (see dbus_server_set_auth_mechanisms()). Namely, an auth mechanism
5081 * such as ANONYMOUS that supports anonymous auth must be included in
5082 * the list of available mechanisms for anonymous login to work.
5084 * This setting also changes the default rule for connections
5085 * authorized as a user; normally, if a connection authorizes as
5086 * a user identity, it is permitted if the user identity is
5087 * root or the user identity matches the user identity of the server
5088 * process. If anonymous connections are allowed, however,
5089 * then any user identity is allowed.
5091 * You can override the rules for connections authorized as a
5092 * user identity with dbus_connection_set_unix_user_function()
5093 * and dbus_connection_set_windows_user_function().
5095 * @param connection the connection
5096 * @param value whether to allow authentication as an anonymous user
5099 dbus_connection_set_allow_anonymous (DBusConnection *connection,
5102 _dbus_return_if_fail (connection != NULL);
5104 CONNECTION_LOCK (connection);
5105 _dbus_transport_set_allow_anonymous (connection->transport, value);
5106 CONNECTION_UNLOCK (connection);
5111 * Normally #DBusConnection automatically handles all messages to the
5112 * org.freedesktop.DBus.Peer interface. However, the message bus wants
5113 * to be able to route methods on that interface through the bus and
5114 * to other applications. If routing peer messages is enabled, then
5115 * messages with the org.freedesktop.DBus.Peer interface that also
5116 * have a bus destination name set will not be automatically
5117 * handled by the #DBusConnection and instead will be dispatched
5118 * normally to the application.
5120 * If a normal application sets this flag, it can break things badly.
5121 * So don't set this unless you are the message bus.
5123 * @param connection the connection
5124 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
5127 dbus_connection_set_route_peer_messages (DBusConnection *connection,
5130 _dbus_return_if_fail (connection != NULL);
5132 CONNECTION_LOCK (connection);
5133 connection->route_peer_messages = TRUE;
5134 CONNECTION_UNLOCK (connection);
5138 * Adds a message filter. Filters are handlers that are run on all
5139 * incoming messages, prior to the objects registered with
5140 * dbus_connection_register_object_path(). Filters are run in the
5141 * order that they were added. The same handler can be added as a
5142 * filter more than once, in which case it will be run more than once.
5143 * Filters added during a filter callback won't be run on the message
5146 * @todo we don't run filters on messages while blocking without
5147 * entering the main loop, since filters are run as part of
5148 * dbus_connection_dispatch(). This is probably a feature, as filters
5149 * could create arbitrary reentrancy. But kind of sucks if you're
5150 * trying to filter METHOD_RETURN for some reason.
5152 * @param connection the connection
5153 * @param function function to handle messages
5154 * @param user_data user data to pass to the function
5155 * @param free_data_function function to use for freeing user data
5156 * @returns #TRUE on success, #FALSE if not enough memory.
5159 dbus_connection_add_filter (DBusConnection *connection,
5160 DBusHandleMessageFunction function,
5162 DBusFreeFunction free_data_function)
5164 DBusMessageFilter *filter;
5166 _dbus_return_val_if_fail (connection != NULL, FALSE);
5167 _dbus_return_val_if_fail (function != NULL, FALSE);
5169 filter = dbus_new0 (DBusMessageFilter, 1);
5173 filter->refcount.value = 1;
5175 CONNECTION_LOCK (connection);
5177 if (!_dbus_list_append (&connection->filter_list,
5180 _dbus_message_filter_unref (filter);
5181 CONNECTION_UNLOCK (connection);
5185 /* Fill in filter after all memory allocated,
5186 * so we don't run the free_user_data_function
5187 * if the add_filter() fails
5190 filter->function = function;
5191 filter->user_data = user_data;
5192 filter->free_user_data_function = free_data_function;
5194 CONNECTION_UNLOCK (connection);
5199 * Removes a previously-added message filter. It is a programming
5200 * error to call this function for a handler that has not been added
5201 * as a filter. If the given handler was added more than once, only
5202 * one instance of it will be removed (the most recently-added
5205 * @param connection the connection
5206 * @param function the handler to remove
5207 * @param user_data user data for the handler to remove
5211 dbus_connection_remove_filter (DBusConnection *connection,
5212 DBusHandleMessageFunction function,
5216 DBusMessageFilter *filter;
5218 _dbus_return_if_fail (connection != NULL);
5219 _dbus_return_if_fail (function != NULL);
5221 CONNECTION_LOCK (connection);
5225 link = _dbus_list_get_last_link (&connection->filter_list);
5226 while (link != NULL)
5228 filter = link->data;
5230 if (filter->function == function &&
5231 filter->user_data == user_data)
5233 _dbus_list_remove_link (&connection->filter_list, link);
5234 filter->function = NULL;
5239 link = _dbus_list_get_prev_link (&connection->filter_list, link);
5242 CONNECTION_UNLOCK (connection);
5244 #ifndef DBUS_DISABLE_CHECKS
5247 _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
5248 function, user_data);
5253 /* Call application code */
5254 if (filter->free_user_data_function)
5255 (* filter->free_user_data_function) (filter->user_data);
5257 filter->free_user_data_function = NULL;
5258 filter->user_data = NULL;
5260 _dbus_message_filter_unref (filter);
5264 * Registers a handler for a given path in the object hierarchy.
5265 * The given vtable handles messages sent to exactly the given path.
5267 * @param connection the connection
5268 * @param path a '/' delimited string of path elements
5269 * @param vtable the virtual table
5270 * @param user_data data to pass to functions in the vtable
5271 * @param error address where an error can be returned
5272 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5273 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
5276 dbus_connection_try_register_object_path (DBusConnection *connection,
5278 const DBusObjectPathVTable *vtable,
5282 char **decomposed_path;
5285 _dbus_return_val_if_fail (connection != NULL, FALSE);
5286 _dbus_return_val_if_fail (path != NULL, FALSE);
5287 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5288 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5290 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5293 CONNECTION_LOCK (connection);
5295 retval = _dbus_object_tree_register (connection->objects,
5297 (const char **) decomposed_path, vtable,
5300 CONNECTION_UNLOCK (connection);
5302 dbus_free_string_array (decomposed_path);
5308 * Registers a handler for a given path in the object hierarchy.
5309 * The given vtable handles messages sent to exactly the given path.
5311 * It is a bug to call this function for object paths which already
5312 * have a handler. Use dbus_connection_try_register_object_path() if this
5313 * might be the case.
5315 * @param connection the connection
5316 * @param path a '/' delimited string of path elements
5317 * @param vtable the virtual table
5318 * @param user_data data to pass to functions in the vtable
5319 * @returns #FALSE if not enough memory
5322 dbus_connection_register_object_path (DBusConnection *connection,
5324 const DBusObjectPathVTable *vtable,
5327 char **decomposed_path;
5329 DBusError error = DBUS_ERROR_INIT;
5331 _dbus_return_val_if_fail (connection != NULL, FALSE);
5332 _dbus_return_val_if_fail (path != NULL, FALSE);
5333 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5334 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5336 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5339 CONNECTION_LOCK (connection);
5341 retval = _dbus_object_tree_register (connection->objects,
5343 (const char **) decomposed_path, vtable,
5346 CONNECTION_UNLOCK (connection);
5348 dbus_free_string_array (decomposed_path);
5350 if (dbus_error_has_name (&error, DBUS_ERROR_ADDRESS_IN_USE))
5352 _dbus_warn ("%s\n", error.message);
5353 dbus_error_free (&error);
5361 * Registers a fallback handler for a given subsection of the object
5362 * hierarchy. The given vtable handles messages at or below the given
5363 * path. You can use this to establish a default message handling
5364 * policy for a whole "subdirectory."
5366 * @param connection the connection
5367 * @param path a '/' delimited string of path elements
5368 * @param vtable the virtual table
5369 * @param user_data data to pass to functions in the vtable
5370 * @param error address where an error can be returned
5371 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5372 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
5375 dbus_connection_try_register_fallback (DBusConnection *connection,
5377 const DBusObjectPathVTable *vtable,
5381 char **decomposed_path;
5384 _dbus_return_val_if_fail (connection != NULL, FALSE);
5385 _dbus_return_val_if_fail (path != NULL, FALSE);
5386 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5387 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5389 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5392 CONNECTION_LOCK (connection);
5394 retval = _dbus_object_tree_register (connection->objects,
5396 (const char **) decomposed_path, vtable,
5399 CONNECTION_UNLOCK (connection);
5401 dbus_free_string_array (decomposed_path);
5407 * Registers a fallback handler for a given subsection of the object
5408 * hierarchy. The given vtable handles messages at or below the given
5409 * path. You can use this to establish a default message handling
5410 * policy for a whole "subdirectory."
5412 * It is a bug to call this function for object paths which already
5413 * have a handler. Use dbus_connection_try_register_fallback() if this
5414 * might be the case.
5416 * @param connection the connection
5417 * @param path a '/' delimited string of path elements
5418 * @param vtable the virtual table
5419 * @param user_data data to pass to functions in the vtable
5420 * @returns #FALSE if not enough memory
5423 dbus_connection_register_fallback (DBusConnection *connection,
5425 const DBusObjectPathVTable *vtable,
5428 char **decomposed_path;
5430 DBusError error = DBUS_ERROR_INIT;
5432 _dbus_return_val_if_fail (connection != NULL, FALSE);
5433 _dbus_return_val_if_fail (path != NULL, FALSE);
5434 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5435 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5437 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5440 CONNECTION_LOCK (connection);
5442 retval = _dbus_object_tree_register (connection->objects,
5444 (const char **) decomposed_path, vtable,
5447 CONNECTION_UNLOCK (connection);
5449 dbus_free_string_array (decomposed_path);
5451 if (dbus_error_has_name (&error, DBUS_ERROR_ADDRESS_IN_USE))
5453 _dbus_warn ("%s\n", error.message);
5454 dbus_error_free (&error);
5462 * Unregisters the handler registered with exactly the given path.
5463 * It's a bug to call this function for a path that isn't registered.
5464 * Can unregister both fallback paths and object paths.
5466 * @param connection the connection
5467 * @param path a '/' delimited string of path elements
5468 * @returns #FALSE if not enough memory
5471 dbus_connection_unregister_object_path (DBusConnection *connection,
5474 char **decomposed_path;
5476 _dbus_return_val_if_fail (connection != NULL, FALSE);
5477 _dbus_return_val_if_fail (path != NULL, FALSE);
5478 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5480 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5483 CONNECTION_LOCK (connection);
5485 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
5487 dbus_free_string_array (decomposed_path);
5493 * Gets the user data passed to dbus_connection_register_object_path()
5494 * or dbus_connection_register_fallback(). If nothing was registered
5495 * at this path, the data is filled in with #NULL.
5497 * @param connection the connection
5498 * @param path the path you registered with
5499 * @param data_p location to store the user data, or #NULL
5500 * @returns #FALSE if not enough memory
5503 dbus_connection_get_object_path_data (DBusConnection *connection,
5507 char **decomposed_path;
5509 _dbus_return_val_if_fail (connection != NULL, FALSE);
5510 _dbus_return_val_if_fail (path != NULL, FALSE);
5511 _dbus_return_val_if_fail (data_p != NULL, FALSE);
5515 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5518 CONNECTION_LOCK (connection);
5520 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
5522 CONNECTION_UNLOCK (connection);
5524 dbus_free_string_array (decomposed_path);
5530 * Lists the registered fallback handlers and object path handlers at
5531 * the given parent_path. The returned array should be freed with
5532 * dbus_free_string_array().
5534 * @param connection the connection
5535 * @param parent_path the path to list the child handlers of
5536 * @param child_entries returns #NULL-terminated array of children
5537 * @returns #FALSE if no memory to allocate the child entries
5540 dbus_connection_list_registered (DBusConnection *connection,
5541 const char *parent_path,
5542 char ***child_entries)
5544 char **decomposed_path;
5546 _dbus_return_val_if_fail (connection != NULL, FALSE);
5547 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
5548 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
5549 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
5551 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
5554 CONNECTION_LOCK (connection);
5556 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
5557 (const char **) decomposed_path,
5559 dbus_free_string_array (decomposed_path);
5564 static DBusDataSlotAllocator slot_allocator;
5565 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
5568 * Allocates an integer ID to be used for storing application-specific
5569 * data on any DBusConnection. The allocated ID may then be used
5570 * with dbus_connection_set_data() and dbus_connection_get_data().
5571 * The passed-in slot must be initialized to -1, and is filled in
5572 * with the slot ID. If the passed-in slot is not -1, it's assumed
5573 * to be already allocated, and its refcount is incremented.
5575 * The allocated slot is global, i.e. all DBusConnection objects will
5576 * have a slot with the given integer ID reserved.
5578 * @param slot_p address of a global variable storing the slot
5579 * @returns #FALSE on failure (no memory)
5582 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
5584 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5585 &_DBUS_LOCK_NAME (connection_slots),
5590 * Deallocates a global ID for connection data slots.
5591 * dbus_connection_get_data() and dbus_connection_set_data() may no
5592 * longer be used with this slot. Existing data stored on existing
5593 * DBusConnection objects will be freed when the connection is
5594 * finalized, but may not be retrieved (and may only be replaced if
5595 * someone else reallocates the slot). When the refcount on the
5596 * passed-in slot reaches 0, it is set to -1.
5598 * @param slot_p address storing the slot to deallocate
5601 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
5603 _dbus_return_if_fail (*slot_p >= 0);
5605 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5609 * Stores a pointer on a DBusConnection, along
5610 * with an optional function to be used for freeing
5611 * the data when the data is set again, or when
5612 * the connection is finalized. The slot number
5613 * must have been allocated with dbus_connection_allocate_data_slot().
5615 * @param connection the connection
5616 * @param slot the slot number
5617 * @param data the data to store
5618 * @param free_data_func finalizer function for the data
5619 * @returns #TRUE if there was enough memory to store the data
5622 dbus_connection_set_data (DBusConnection *connection,
5625 DBusFreeFunction free_data_func)
5627 DBusFreeFunction old_free_func;
5631 _dbus_return_val_if_fail (connection != NULL, FALSE);
5632 _dbus_return_val_if_fail (slot >= 0, FALSE);
5634 CONNECTION_LOCK (connection);
5636 retval = _dbus_data_slot_list_set (&slot_allocator,
5637 &connection->slot_list,
5638 slot, data, free_data_func,
5639 &old_free_func, &old_data);
5641 CONNECTION_UNLOCK (connection);
5645 /* Do the actual free outside the connection lock */
5647 (* old_free_func) (old_data);
5654 * Retrieves data previously set with dbus_connection_set_data().
5655 * The slot must still be allocated (must not have been freed).
5657 * @param connection the connection
5658 * @param slot the slot to get data from
5659 * @returns the data, or #NULL if not found
5662 dbus_connection_get_data (DBusConnection *connection,
5667 _dbus_return_val_if_fail (connection != NULL, NULL);
5669 CONNECTION_LOCK (connection);
5671 res = _dbus_data_slot_list_get (&slot_allocator,
5672 &connection->slot_list,
5675 CONNECTION_UNLOCK (connection);
5681 * This function sets a global flag for whether dbus_connection_new()
5682 * will set SIGPIPE behavior to SIG_IGN.
5684 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
5687 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
5689 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
5693 * Specifies the maximum size message this connection is allowed to
5694 * receive. Larger messages will result in disconnecting the
5697 * @param connection a #DBusConnection
5698 * @param size maximum message size the connection can receive, in bytes
5701 dbus_connection_set_max_message_size (DBusConnection *connection,
5704 _dbus_return_if_fail (connection != NULL);
5706 CONNECTION_LOCK (connection);
5707 _dbus_transport_set_max_message_size (connection->transport,
5709 CONNECTION_UNLOCK (connection);
5713 * Gets the value set by dbus_connection_set_max_message_size().
5715 * @param connection the connection
5716 * @returns the max size of a single message
5719 dbus_connection_get_max_message_size (DBusConnection *connection)
5723 _dbus_return_val_if_fail (connection != NULL, 0);
5725 CONNECTION_LOCK (connection);
5726 res = _dbus_transport_get_max_message_size (connection->transport);
5727 CONNECTION_UNLOCK (connection);
5732 * Sets the maximum total number of bytes that can be used for all messages
5733 * received on this connection. Messages count toward the maximum until
5734 * they are finalized. When the maximum is reached, the connection will
5735 * not read more data until some messages are finalized.
5737 * The semantics of the maximum are: if outstanding messages are
5738 * already above the maximum, additional messages will not be read.
5739 * The semantics are not: if the next message would cause us to exceed
5740 * the maximum, we don't read it. The reason is that we don't know the
5741 * size of a message until after we read it.
5743 * Thus, the max live messages size can actually be exceeded
5744 * by up to the maximum size of a single message.
5746 * Also, if we read say 1024 bytes off the wire in a single read(),
5747 * and that contains a half-dozen small messages, we may exceed the
5748 * size max by that amount. But this should be inconsequential.
5750 * This does imply that we can't call read() with a buffer larger
5751 * than we're willing to exceed this limit by.
5753 * @param connection the connection
5754 * @param size the maximum size in bytes of all outstanding messages
5757 dbus_connection_set_max_received_size (DBusConnection *connection,
5760 _dbus_return_if_fail (connection != NULL);
5762 CONNECTION_LOCK (connection);
5763 _dbus_transport_set_max_received_size (connection->transport,
5765 CONNECTION_UNLOCK (connection);
5769 * Gets the value set by dbus_connection_set_max_received_size().
5771 * @param connection the connection
5772 * @returns the max size of all live messages
5775 dbus_connection_get_max_received_size (DBusConnection *connection)
5779 _dbus_return_val_if_fail (connection != NULL, 0);
5781 CONNECTION_LOCK (connection);
5782 res = _dbus_transport_get_max_received_size (connection->transport);
5783 CONNECTION_UNLOCK (connection);
5788 * Gets the approximate size in bytes of all messages in the outgoing
5789 * message queue. The size is approximate in that you shouldn't use
5790 * it to decide how many bytes to read off the network or anything
5791 * of that nature, as optimizations may choose to tell small white lies
5792 * to avoid performance overhead.
5794 * @param connection the connection
5795 * @returns the number of bytes that have been queued up but not sent
5798 dbus_connection_get_outgoing_size (DBusConnection *connection)
5802 _dbus_return_val_if_fail (connection != NULL, 0);
5804 CONNECTION_LOCK (connection);
5805 res = _dbus_counter_get_value (connection->outgoing_counter);
5806 CONNECTION_UNLOCK (connection);