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));
969 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
970 _dbus_connection_remove_timeout_unlocked (connection,
971 _dbus_pending_call_get_timeout_unlocked (pending));
973 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
975 _dbus_pending_call_unref_and_unlock (pending);
979 * Removes a pending call from the connection, such that
980 * the pending reply will be ignored. May drop the last
981 * reference to the pending call.
983 * @param connection the connection
984 * @param pending the pending call
987 _dbus_connection_remove_pending_call (DBusConnection *connection,
988 DBusPendingCall *pending)
990 CONNECTION_LOCK (connection);
991 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
995 * Acquire the transporter I/O path. This must be done before
996 * doing any I/O in the transporter. May sleep and drop the
997 * IO path mutex while waiting for the I/O path.
999 * @param connection the connection.
1000 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1001 * @returns TRUE if the I/O path was acquired.
1004 _dbus_connection_acquire_io_path (DBusConnection *connection,
1005 int timeout_milliseconds)
1007 dbus_bool_t we_acquired;
1009 HAVE_LOCK_CHECK (connection);
1011 /* We don't want the connection to vanish */
1012 _dbus_connection_ref_unlocked (connection);
1014 /* We will only touch io_path_acquired which is protected by our mutex */
1015 CONNECTION_UNLOCK (connection);
1017 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1018 _dbus_mutex_lock (connection->io_path_mutex);
1020 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
1021 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
1023 we_acquired = FALSE;
1025 if (connection->io_path_acquired)
1027 if (timeout_milliseconds != -1)
1029 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
1030 _DBUS_FUNCTION_NAME, timeout_milliseconds);
1032 if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1033 connection->io_path_mutex,
1034 timeout_milliseconds))
1036 /* We timed out before anyone signaled. */
1037 /* (writing the loop to handle the !timedout case by
1038 * waiting longer if needed is a pain since dbus
1039 * wraps pthread_cond_timedwait to take a relative
1040 * time instead of absolute, something kind of stupid
1041 * on our part. for now it doesn't matter, we will just
1042 * end up back here eventually.)
1048 while (connection->io_path_acquired)
1050 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
1051 _dbus_condvar_wait (connection->io_path_cond,
1052 connection->io_path_mutex);
1057 if (!connection->io_path_acquired)
1060 connection->io_path_acquired = TRUE;
1063 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
1064 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
1066 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1067 _dbus_mutex_unlock (connection->io_path_mutex);
1069 CONNECTION_LOCK (connection);
1071 HAVE_LOCK_CHECK (connection);
1073 _dbus_connection_unref_unlocked (connection);
1079 * Release the I/O path when you're done with it. Only call
1080 * after you've acquired the I/O. Wakes up at most one thread
1081 * currently waiting to acquire the I/O path.
1083 * @param connection the connection.
1086 _dbus_connection_release_io_path (DBusConnection *connection)
1088 HAVE_LOCK_CHECK (connection);
1090 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1091 _dbus_mutex_lock (connection->io_path_mutex);
1093 _dbus_assert (connection->io_path_acquired);
1095 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
1096 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
1098 connection->io_path_acquired = FALSE;
1099 _dbus_condvar_wake_one (connection->io_path_cond);
1101 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1102 _dbus_mutex_unlock (connection->io_path_mutex);
1106 * Queues incoming messages and sends outgoing messages for this
1107 * connection, optionally blocking in the process. Each call to
1108 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1109 * time and then read or write data if possible.
1111 * The purpose of this function is to be able to flush outgoing
1112 * messages or queue up incoming messages without returning
1113 * control to the application and causing reentrancy weirdness.
1115 * The flags parameter allows you to specify whether to
1116 * read incoming messages, write outgoing messages, or both,
1117 * and whether to block if no immediate action is possible.
1119 * The timeout_milliseconds parameter does nothing unless the
1120 * iteration is blocking.
1122 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1123 * wasn't specified, then it's impossible to block, even if
1124 * you specify DBUS_ITERATION_BLOCK; in that case the function
1125 * returns immediately.
1127 * Called with connection lock held.
1129 * @param connection the connection.
1130 * @param flags iteration flags.
1131 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1134 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1136 int timeout_milliseconds)
1138 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1140 HAVE_LOCK_CHECK (connection);
1142 if (connection->n_outgoing == 0)
1143 flags &= ~DBUS_ITERATION_DO_WRITING;
1145 if (_dbus_connection_acquire_io_path (connection,
1146 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1148 HAVE_LOCK_CHECK (connection);
1150 _dbus_transport_do_iteration (connection->transport,
1151 flags, timeout_milliseconds);
1152 _dbus_connection_release_io_path (connection);
1155 HAVE_LOCK_CHECK (connection);
1157 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1161 * Creates a new connection for the given transport. A transport
1162 * represents a message stream that uses some concrete mechanism, such
1163 * as UNIX domain sockets. May return #NULL if insufficient
1164 * memory exists to create the connection.
1166 * @param transport the transport.
1167 * @returns the new connection, or #NULL on failure.
1170 _dbus_connection_new_for_transport (DBusTransport *transport)
1172 DBusConnection *connection;
1173 DBusWatchList *watch_list;
1174 DBusTimeoutList *timeout_list;
1175 DBusHashTable *pending_replies;
1176 DBusList *disconnect_link;
1177 DBusMessage *disconnect_message;
1178 DBusCounter *outgoing_counter;
1179 DBusObjectTree *objects;
1183 pending_replies = NULL;
1184 timeout_list = NULL;
1185 disconnect_link = NULL;
1186 disconnect_message = NULL;
1187 outgoing_counter = NULL;
1190 watch_list = _dbus_watch_list_new ();
1191 if (watch_list == NULL)
1194 timeout_list = _dbus_timeout_list_new ();
1195 if (timeout_list == NULL)
1199 _dbus_hash_table_new (DBUS_HASH_INT,
1201 (DBusFreeFunction)free_pending_call_on_hash_removal);
1202 if (pending_replies == NULL)
1205 connection = dbus_new0 (DBusConnection, 1);
1206 if (connection == NULL)
1209 _dbus_mutex_new_at_location (&connection->mutex);
1210 if (connection->mutex == NULL)
1213 _dbus_mutex_new_at_location (&connection->io_path_mutex);
1214 if (connection->io_path_mutex == NULL)
1217 _dbus_mutex_new_at_location (&connection->dispatch_mutex);
1218 if (connection->dispatch_mutex == NULL)
1221 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1222 if (connection->dispatch_cond == NULL)
1225 _dbus_condvar_new_at_location (&connection->io_path_cond);
1226 if (connection->io_path_cond == NULL)
1229 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1230 DBUS_INTERFACE_LOCAL,
1233 if (disconnect_message == NULL)
1236 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1237 if (disconnect_link == NULL)
1240 outgoing_counter = _dbus_counter_new ();
1241 if (outgoing_counter == NULL)
1244 objects = _dbus_object_tree_new (connection);
1245 if (objects == NULL)
1248 if (_dbus_modify_sigpipe)
1249 _dbus_disable_sigpipe ();
1251 connection->refcount.value = 1;
1252 connection->transport = transport;
1253 connection->watches = watch_list;
1254 connection->timeouts = timeout_list;
1255 connection->pending_replies = pending_replies;
1256 connection->outgoing_counter = outgoing_counter;
1257 connection->filter_list = NULL;
1258 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1259 connection->objects = objects;
1260 connection->exit_on_disconnect = FALSE;
1261 connection->shareable = FALSE;
1262 connection->route_peer_messages = FALSE;
1263 connection->disconnected_message_arrived = FALSE;
1264 connection->disconnected_message_processed = FALSE;
1266 #ifndef DBUS_DISABLE_CHECKS
1267 connection->generation = _dbus_current_generation;
1270 _dbus_data_slot_list_init (&connection->slot_list);
1272 connection->client_serial = 1;
1274 connection->disconnect_message_link = disconnect_link;
1276 CONNECTION_LOCK (connection);
1278 if (!_dbus_transport_set_connection (transport, connection))
1280 CONNECTION_UNLOCK (connection);
1285 _dbus_transport_ref (transport);
1287 CONNECTION_UNLOCK (connection);
1292 if (disconnect_message != NULL)
1293 dbus_message_unref (disconnect_message);
1295 if (disconnect_link != NULL)
1296 _dbus_list_free_link (disconnect_link);
1298 if (connection != NULL)
1300 _dbus_condvar_free_at_location (&connection->io_path_cond);
1301 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1302 _dbus_mutex_free_at_location (&connection->mutex);
1303 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1304 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1305 dbus_free (connection);
1307 if (pending_replies)
1308 _dbus_hash_table_unref (pending_replies);
1311 _dbus_watch_list_free (watch_list);
1314 _dbus_timeout_list_free (timeout_list);
1316 if (outgoing_counter)
1317 _dbus_counter_unref (outgoing_counter);
1320 _dbus_object_tree_unref (objects);
1326 * Increments the reference count of a DBusConnection.
1327 * Requires that the caller already holds the connection lock.
1329 * @param connection the connection.
1330 * @returns the connection.
1333 _dbus_connection_ref_unlocked (DBusConnection *connection)
1335 _dbus_assert (connection != NULL);
1336 _dbus_assert (connection->generation == _dbus_current_generation);
1338 HAVE_LOCK_CHECK (connection);
1340 #ifdef DBUS_HAVE_ATOMIC_INT
1341 _dbus_atomic_inc (&connection->refcount);
1343 _dbus_assert (connection->refcount.value > 0);
1344 connection->refcount.value += 1;
1351 * Decrements the reference count of a DBusConnection.
1352 * Requires that the caller already holds the connection lock.
1354 * @param connection the connection.
1357 _dbus_connection_unref_unlocked (DBusConnection *connection)
1359 dbus_bool_t last_unref;
1361 HAVE_LOCK_CHECK (connection);
1363 _dbus_assert (connection != NULL);
1365 /* The connection lock is better than the global
1366 * lock in the atomic increment fallback
1369 #ifdef DBUS_HAVE_ATOMIC_INT
1370 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1372 _dbus_assert (connection->refcount.value > 0);
1374 connection->refcount.value -= 1;
1375 last_unref = (connection->refcount.value == 0);
1377 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1382 _dbus_connection_last_unref (connection);
1385 static dbus_uint32_t
1386 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1388 dbus_uint32_t serial;
1390 serial = connection->client_serial++;
1392 if (connection->client_serial == 0)
1393 connection->client_serial = 1;
1399 * A callback for use with dbus_watch_new() to create a DBusWatch.
1401 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1402 * and the virtual handle_watch in DBusTransport if we got rid of it.
1403 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1406 * @param watch the watch.
1407 * @param condition the current condition of the file descriptors being watched.
1408 * @param data must be a pointer to a #DBusConnection
1409 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1412 _dbus_connection_handle_watch (DBusWatch *watch,
1413 unsigned int condition,
1416 DBusConnection *connection;
1418 DBusDispatchStatus status;
1422 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1424 CONNECTION_LOCK (connection);
1425 _dbus_connection_acquire_io_path (connection, -1);
1426 HAVE_LOCK_CHECK (connection);
1427 retval = _dbus_transport_handle_watch (connection->transport,
1430 _dbus_connection_release_io_path (connection);
1432 HAVE_LOCK_CHECK (connection);
1434 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1436 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1438 /* this calls out to user code */
1439 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1441 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1446 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1447 static DBusHashTable *shared_connections = NULL;
1448 static DBusList *shared_connections_no_guid = NULL;
1451 close_connection_on_shutdown (DBusConnection *connection)
1453 DBusMessage *message;
1455 dbus_connection_ref (connection);
1456 _dbus_connection_close_possibly_shared (connection);
1458 /* Churn through to the Disconnected message */
1459 while ((message = dbus_connection_pop_message (connection)))
1461 dbus_message_unref (message);
1463 dbus_connection_unref (connection);
1467 shared_connections_shutdown (void *data)
1471 _DBUS_LOCK (shared_connections);
1473 /* This is a little bit unpleasant... better ideas? */
1474 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
1476 DBusConnection *connection;
1479 _dbus_hash_iter_init (shared_connections, &iter);
1480 _dbus_hash_iter_next (&iter);
1482 connection = _dbus_hash_iter_get_value (&iter);
1484 _DBUS_UNLOCK (shared_connections);
1485 close_connection_on_shutdown (connection);
1486 _DBUS_LOCK (shared_connections);
1488 /* The connection should now be dead and not in our hash ... */
1489 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
1492 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1494 _dbus_hash_table_unref (shared_connections);
1495 shared_connections = NULL;
1497 if (shared_connections_no_guid != NULL)
1499 DBusConnection *connection;
1500 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1501 while (connection != NULL)
1503 _DBUS_UNLOCK (shared_connections);
1504 close_connection_on_shutdown (connection);
1505 _DBUS_LOCK (shared_connections);
1506 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1510 shared_connections_no_guid = NULL;
1512 _DBUS_UNLOCK (shared_connections);
1516 connection_lookup_shared (DBusAddressEntry *entry,
1517 DBusConnection **result)
1519 _dbus_verbose ("checking for existing connection\n");
1523 _DBUS_LOCK (shared_connections);
1525 if (shared_connections == NULL)
1527 _dbus_verbose ("creating shared_connections hash table\n");
1529 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1532 if (shared_connections == NULL)
1534 _DBUS_UNLOCK (shared_connections);
1538 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1540 _dbus_hash_table_unref (shared_connections);
1541 shared_connections = NULL;
1542 _DBUS_UNLOCK (shared_connections);
1546 _dbus_verbose (" successfully created shared_connections\n");
1548 _DBUS_UNLOCK (shared_connections);
1549 return TRUE; /* no point looking up in the hash we just made */
1555 guid = dbus_address_entry_get_value (entry, "guid");
1559 DBusConnection *connection;
1561 connection = _dbus_hash_table_lookup_string (shared_connections,
1566 /* The DBusConnection can't be finalized without taking
1567 * the shared_connections lock to remove it from the
1568 * hash. So it's safe to ref the connection here.
1569 * However, it may be disconnected if the Disconnected
1570 * message hasn't been processed yet, in which case we
1571 * want to pretend it isn't in the hash and avoid
1574 * The idea is to avoid ever returning a disconnected connection
1575 * from dbus_connection_open(). We could just synchronously
1576 * drop our shared ref to the connection on connection disconnect,
1577 * and then assert here that the connection is connected, but
1578 * that causes reentrancy headaches.
1580 CONNECTION_LOCK (connection);
1581 if (_dbus_connection_get_is_connected_unlocked (connection))
1583 _dbus_connection_ref_unlocked (connection);
1584 *result = connection;
1585 _dbus_verbose ("looked up existing connection to server guid %s\n",
1590 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1593 CONNECTION_UNLOCK (connection);
1597 _DBUS_UNLOCK (shared_connections);
1603 connection_record_shared_unlocked (DBusConnection *connection,
1607 char *guid_in_connection;
1609 HAVE_LOCK_CHECK (connection);
1610 _dbus_assert (connection->server_guid == NULL);
1611 _dbus_assert (connection->shareable);
1613 /* get a hard ref on this connection, even if
1614 * we won't in fact store it in the hash, we still
1615 * need to hold a ref on it until it's disconnected.
1617 _dbus_connection_ref_unlocked (connection);
1621 _DBUS_LOCK (shared_connections);
1623 if (!_dbus_list_prepend (&shared_connections_no_guid, connection))
1625 _DBUS_UNLOCK (shared_connections);
1629 _DBUS_UNLOCK (shared_connections);
1630 return TRUE; /* don't store in the hash */
1633 /* A separate copy of the key is required in the hash table, because
1634 * we don't have a lock on the connection when we are doing a hash
1638 guid_key = _dbus_strdup (guid);
1639 if (guid_key == NULL)
1642 guid_in_connection = _dbus_strdup (guid);
1643 if (guid_in_connection == NULL)
1645 dbus_free (guid_key);
1649 _DBUS_LOCK (shared_connections);
1650 _dbus_assert (shared_connections != NULL);
1652 if (!_dbus_hash_table_insert_string (shared_connections,
1653 guid_key, connection))
1655 dbus_free (guid_key);
1656 dbus_free (guid_in_connection);
1657 _DBUS_UNLOCK (shared_connections);
1661 connection->server_guid = guid_in_connection;
1663 _dbus_verbose ("stored connection to %s to be shared\n",
1664 connection->server_guid);
1666 _DBUS_UNLOCK (shared_connections);
1668 _dbus_assert (connection->server_guid != NULL);
1674 connection_forget_shared_unlocked (DBusConnection *connection)
1676 HAVE_LOCK_CHECK (connection);
1678 if (!connection->shareable)
1681 if (connection->server_guid != NULL)
1683 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1684 connection->server_guid);
1686 _DBUS_LOCK (shared_connections);
1688 if (!_dbus_hash_table_remove_string (shared_connections,
1689 connection->server_guid))
1690 _dbus_assert_not_reached ("connection was not in the shared table");
1692 dbus_free (connection->server_guid);
1693 connection->server_guid = NULL;
1694 _DBUS_UNLOCK (shared_connections);
1697 /* remove our reference held on all shareable connections */
1698 _dbus_connection_unref_unlocked (connection);
1701 static DBusConnection*
1702 connection_try_from_address_entry (DBusAddressEntry *entry,
1705 DBusTransport *transport;
1706 DBusConnection *connection;
1708 transport = _dbus_transport_open (entry, error);
1710 if (transport == NULL)
1712 _DBUS_ASSERT_ERROR_IS_SET (error);
1716 connection = _dbus_connection_new_for_transport (transport);
1718 _dbus_transport_unref (transport);
1720 if (connection == NULL)
1722 _DBUS_SET_OOM (error);
1726 #ifndef DBUS_DISABLE_CHECKS
1727 _dbus_assert (!connection->have_connection_lock);
1733 * If the shared parameter is true, then any existing connection will
1734 * be used (and if a new connection is created, it will be available
1735 * for use by others). If the shared parameter is false, a new
1736 * connection will always be created, and the new connection will
1737 * never be returned to other callers.
1739 * @param address the address
1740 * @param shared whether the connection is shared or private
1741 * @param error error return
1742 * @returns the connection or #NULL on error
1744 static DBusConnection*
1745 _dbus_connection_open_internal (const char *address,
1749 DBusConnection *connection;
1750 DBusAddressEntry **entries;
1751 DBusError tmp_error = DBUS_ERROR_INIT;
1752 DBusError first_error = DBUS_ERROR_INIT;
1755 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1757 _dbus_verbose ("opening %s connection to: %s\n",
1758 shared ? "shared" : "private", address);
1760 if (!dbus_parse_address (address, &entries, &len, error))
1763 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1767 for (i = 0; i < len; i++)
1771 if (!connection_lookup_shared (entries[i], &connection))
1772 _DBUS_SET_OOM (&tmp_error);
1775 if (connection == NULL)
1777 connection = connection_try_from_address_entry (entries[i],
1780 if (connection != NULL && shared)
1784 connection->shareable = TRUE;
1786 /* guid may be NULL */
1787 guid = dbus_address_entry_get_value (entries[i], "guid");
1789 CONNECTION_LOCK (connection);
1791 if (!connection_record_shared_unlocked (connection, guid))
1793 _DBUS_SET_OOM (&tmp_error);
1794 _dbus_connection_close_possibly_shared_and_unlock (connection);
1795 dbus_connection_unref (connection);
1799 CONNECTION_UNLOCK (connection);
1806 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1809 dbus_move_error (&tmp_error, &first_error);
1811 dbus_error_free (&tmp_error);
1814 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1815 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1817 if (connection == NULL)
1819 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1820 dbus_move_error (&first_error, error);
1823 dbus_error_free (&first_error);
1825 dbus_address_entries_free (entries);
1830 * Closes a shared OR private connection, while dbus_connection_close() can
1831 * only be used on private connections. Should only be called by the
1832 * dbus code that owns the connection - an owner must be known,
1833 * the open/close state is like malloc/free, not like ref/unref.
1835 * @param connection the connection
1838 _dbus_connection_close_possibly_shared (DBusConnection *connection)
1840 _dbus_assert (connection != NULL);
1841 _dbus_assert (connection->generation == _dbus_current_generation);
1843 CONNECTION_LOCK (connection);
1844 _dbus_connection_close_possibly_shared_and_unlock (connection);
1847 static DBusPreallocatedSend*
1848 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1850 DBusPreallocatedSend *preallocated;
1852 HAVE_LOCK_CHECK (connection);
1854 _dbus_assert (connection != NULL);
1856 preallocated = dbus_new (DBusPreallocatedSend, 1);
1857 if (preallocated == NULL)
1860 if (connection->link_cache != NULL)
1862 preallocated->queue_link =
1863 _dbus_list_pop_first_link (&connection->link_cache);
1864 preallocated->queue_link->data = NULL;
1868 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1869 if (preallocated->queue_link == NULL)
1873 if (connection->link_cache != NULL)
1875 preallocated->counter_link =
1876 _dbus_list_pop_first_link (&connection->link_cache);
1877 preallocated->counter_link->data = connection->outgoing_counter;
1881 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1882 if (preallocated->counter_link == NULL)
1886 _dbus_counter_ref (preallocated->counter_link->data);
1888 preallocated->connection = connection;
1890 return preallocated;
1893 _dbus_list_free_link (preallocated->queue_link);
1895 dbus_free (preallocated);
1900 /* Called with lock held, does not update dispatch status */
1902 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
1903 DBusPreallocatedSend *preallocated,
1904 DBusMessage *message,
1905 dbus_uint32_t *client_serial)
1907 dbus_uint32_t serial;
1910 preallocated->queue_link->data = message;
1911 _dbus_list_prepend_link (&connection->outgoing_messages,
1912 preallocated->queue_link);
1914 _dbus_message_add_size_counter_link (message,
1915 preallocated->counter_link);
1917 dbus_free (preallocated);
1918 preallocated = NULL;
1920 dbus_message_ref (message);
1922 connection->n_outgoing += 1;
1924 sig = dbus_message_get_signature (message);
1926 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
1928 dbus_message_get_type (message),
1929 dbus_message_get_path (message) ?
1930 dbus_message_get_path (message) :
1932 dbus_message_get_interface (message) ?
1933 dbus_message_get_interface (message) :
1935 dbus_message_get_member (message) ?
1936 dbus_message_get_member (message) :
1939 dbus_message_get_destination (message) ?
1940 dbus_message_get_destination (message) :
1943 connection->n_outgoing);
1945 if (dbus_message_get_serial (message) == 0)
1947 serial = _dbus_connection_get_next_client_serial (connection);
1948 _dbus_message_set_serial (message, serial);
1950 *client_serial = serial;
1955 *client_serial = dbus_message_get_serial (message);
1958 _dbus_verbose ("Message %p serial is %u\n",
1959 message, dbus_message_get_serial (message));
1961 _dbus_message_lock (message);
1963 /* Now we need to run an iteration to hopefully just write the messages
1964 * out immediately, and otherwise get them queued up
1966 _dbus_connection_do_iteration_unlocked (connection,
1967 DBUS_ITERATION_DO_WRITING,
1970 /* If stuff is still queued up, be sure we wake up the main loop */
1971 if (connection->n_outgoing > 0)
1972 _dbus_connection_wakeup_mainloop (connection);
1976 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
1977 DBusPreallocatedSend *preallocated,
1978 DBusMessage *message,
1979 dbus_uint32_t *client_serial)
1981 DBusDispatchStatus status;
1983 HAVE_LOCK_CHECK (connection);
1985 _dbus_connection_send_preallocated_unlocked_no_update (connection,
1987 message, client_serial);
1989 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1990 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1992 /* this calls out to user code */
1993 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1997 * Like dbus_connection_send(), but assumes the connection
1998 * is already locked on function entry, and unlocks before returning.
2000 * @param connection the connection
2001 * @param message the message to send
2002 * @param client_serial return location for client serial of sent message
2003 * @returns #FALSE on out-of-memory
2006 _dbus_connection_send_and_unlock (DBusConnection *connection,
2007 DBusMessage *message,
2008 dbus_uint32_t *client_serial)
2010 DBusPreallocatedSend *preallocated;
2012 _dbus_assert (connection != NULL);
2013 _dbus_assert (message != NULL);
2015 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2016 if (preallocated == NULL)
2018 CONNECTION_UNLOCK (connection);
2022 _dbus_connection_send_preallocated_and_unlock (connection,
2030 * Used internally to handle the semantics of dbus_server_set_new_connection_function().
2031 * If the new connection function does not ref the connection, we want to close it.
2033 * A bit of a hack, probably the new connection function should have returned a value
2034 * for whether to close, or should have had to close the connection itself if it
2037 * But, this works OK as long as the new connection function doesn't do anything
2038 * crazy like keep the connection around without ref'ing it.
2040 * We have to lock the connection across refcount check and close in case
2041 * the new connection function spawns a thread that closes and unrefs.
2042 * In that case, if the app thread
2043 * closes and unrefs first, we'll harmlessly close again; if the app thread
2044 * still has the ref, we'll close and then the app will close harmlessly.
2045 * If the app unrefs without closing, the app is broken since if the
2046 * app refs from the new connection function it is supposed to also close.
2048 * If we didn't atomically check the refcount and close with the lock held
2049 * though, we could screw this up.
2051 * @param connection the connection
2054 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
2056 CONNECTION_LOCK (connection);
2058 _dbus_assert (connection->refcount.value > 0);
2060 if (connection->refcount.value == 1)
2061 _dbus_connection_close_possibly_shared_and_unlock (connection);
2063 CONNECTION_UNLOCK (connection);
2068 * When a function that blocks has been called with a timeout, and we
2069 * run out of memory, the time to wait for memory is based on the
2070 * timeout. If the caller was willing to block a long time we wait a
2071 * relatively long time for memory, if they were only willing to block
2072 * briefly then we retry for memory at a rapid rate.
2074 * @timeout_milliseconds the timeout requested for blocking
2077 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2079 if (timeout_milliseconds == -1)
2080 _dbus_sleep_milliseconds (1000);
2081 else if (timeout_milliseconds < 100)
2082 ; /* just busy loop */
2083 else if (timeout_milliseconds <= 1000)
2084 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2086 _dbus_sleep_milliseconds (1000);
2089 static DBusMessage *
2090 generate_local_error_message (dbus_uint32_t serial,
2094 DBusMessage *message;
2095 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
2099 if (!dbus_message_set_error_name (message, error_name))
2101 dbus_message_unref (message);
2106 dbus_message_set_no_reply (message, TRUE);
2108 if (!dbus_message_set_reply_serial (message,
2111 dbus_message_unref (message);
2116 if (error_msg != NULL)
2118 DBusMessageIter iter;
2120 dbus_message_iter_init_append (message, &iter);
2121 if (!dbus_message_iter_append_basic (&iter,
2125 dbus_message_unref (message);
2136 /* This is slightly strange since we can pop a message here without
2137 * the dispatch lock.
2140 check_for_reply_unlocked (DBusConnection *connection,
2141 dbus_uint32_t client_serial)
2145 HAVE_LOCK_CHECK (connection);
2147 link = _dbus_list_get_first_link (&connection->incoming_messages);
2149 while (link != NULL)
2151 DBusMessage *reply = link->data;
2153 if (dbus_message_get_reply_serial (reply) == client_serial)
2155 _dbus_list_remove_link (&connection->incoming_messages, link);
2156 connection->n_incoming -= 1;
2159 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2166 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2168 /* We can't iterate over the hash in the normal way since we'll be
2169 * dropping the lock for each item. So we restart the
2170 * iter each time as we drain the hash table.
2173 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2175 DBusPendingCall *pending;
2178 _dbus_hash_iter_init (connection->pending_replies, &iter);
2179 _dbus_hash_iter_next (&iter);
2181 pending = _dbus_hash_iter_get_value (&iter);
2182 _dbus_pending_call_ref_unlocked (pending);
2184 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2186 _dbus_connection_remove_timeout_unlocked (connection,
2187 _dbus_pending_call_get_timeout_unlocked (pending));
2188 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2189 _dbus_hash_iter_remove_entry (&iter);
2191 _dbus_pending_call_unref_and_unlock (pending);
2192 CONNECTION_LOCK (connection);
2194 HAVE_LOCK_CHECK (connection);
2198 complete_pending_call_and_unlock (DBusConnection *connection,
2199 DBusPendingCall *pending,
2200 DBusMessage *message)
2202 _dbus_pending_call_set_reply_unlocked (pending, message);
2203 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2204 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2206 /* Must be called unlocked since it invokes app callback */
2207 _dbus_pending_call_complete (pending);
2208 dbus_pending_call_unref (pending);
2212 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2213 DBusPendingCall *pending)
2216 DBusDispatchStatus status;
2218 reply = check_for_reply_unlocked (connection,
2219 _dbus_pending_call_get_reply_serial_unlocked (pending));
2222 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2224 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2226 complete_pending_call_and_unlock (connection, pending, reply);
2227 dbus_message_unref (reply);
2229 CONNECTION_LOCK (connection);
2230 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2231 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2232 dbus_pending_call_unref (pending);
2241 * Blocks until a pending call times out or gets a reply.
2243 * Does not re-enter the main loop or run filter/path-registered
2244 * callbacks. The reply to the message will not be seen by
2247 * Returns immediately if pending call already got a reply.
2249 * @todo could use performance improvements (it keeps scanning
2250 * the whole message queue for example)
2252 * @param pending the pending call we block for a reply on
2255 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2257 long start_tv_sec, start_tv_usec;
2258 long end_tv_sec, end_tv_usec;
2259 long tv_sec, tv_usec;
2260 DBusDispatchStatus status;
2261 DBusConnection *connection;
2262 dbus_uint32_t client_serial;
2263 int timeout_milliseconds;
2265 _dbus_assert (pending != NULL);
2267 if (dbus_pending_call_get_completed (pending))
2270 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2272 connection = _dbus_pending_call_get_connection_and_lock (pending);
2274 /* Flush message queue - note, can affect dispatch status */
2275 _dbus_connection_flush_unlocked (connection);
2277 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2279 /* note that timeout_milliseconds is limited to a smallish value
2280 * in _dbus_pending_call_new() so overflows aren't possible
2283 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending));
2285 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2286 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2287 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2288 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2289 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2291 _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",
2292 timeout_milliseconds,
2294 start_tv_sec, start_tv_usec,
2295 end_tv_sec, end_tv_usec);
2297 /* check to see if we already got the data off the socket */
2298 /* from another blocked pending call */
2299 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2302 /* Now we wait... */
2303 /* always block at least once as we know we don't have the reply yet */
2304 _dbus_connection_do_iteration_unlocked (connection,
2305 DBUS_ITERATION_DO_READING |
2306 DBUS_ITERATION_BLOCK,
2307 timeout_milliseconds);
2311 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2313 HAVE_LOCK_CHECK (connection);
2315 /* queue messages and get status */
2317 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2319 /* the get_completed() is in case a dispatch() while we were blocking
2320 * got the reply instead of us.
2322 if (_dbus_pending_call_get_completed_unlocked (pending))
2324 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2325 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2326 dbus_pending_call_unref (pending);
2330 if (status == DBUS_DISPATCH_DATA_REMAINS)
2332 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2336 _dbus_get_current_time (&tv_sec, &tv_usec);
2338 if (!_dbus_connection_get_is_connected_unlocked (connection))
2340 DBusMessage *error_msg;
2342 error_msg = generate_local_error_message (client_serial,
2343 DBUS_ERROR_DISCONNECTED,
2344 "Connection was disconnected before a reply was received");
2346 /* on OOM error_msg is set to NULL */
2347 complete_pending_call_and_unlock (connection, pending, error_msg);
2348 dbus_pending_call_unref (pending);
2351 else if (tv_sec < start_tv_sec)
2352 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2353 else if (connection->disconnect_message_link == NULL)
2354 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2355 else if (tv_sec < end_tv_sec ||
2356 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2358 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2359 (end_tv_usec - tv_usec) / 1000;
2360 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2361 _dbus_assert (timeout_milliseconds >= 0);
2363 if (status == DBUS_DISPATCH_NEED_MEMORY)
2365 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2366 * we may already have a reply in the buffer and just can't process
2369 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2371 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2375 /* block again, we don't have the reply buffered yet. */
2376 _dbus_connection_do_iteration_unlocked (connection,
2377 DBUS_ITERATION_DO_READING |
2378 DBUS_ITERATION_BLOCK,
2379 timeout_milliseconds);
2382 goto recheck_status;
2385 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2386 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2388 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
2390 /* unlock and call user code */
2391 complete_pending_call_and_unlock (connection, pending, NULL);
2393 /* update user code on dispatch status */
2394 CONNECTION_LOCK (connection);
2395 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2396 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2397 dbus_pending_call_unref (pending);
2403 * @addtogroup DBusConnection
2409 * Gets a connection to a remote address. If a connection to the given
2410 * address already exists, returns the existing connection with its
2411 * reference count incremented. Otherwise, returns a new connection
2412 * and saves the new connection for possible re-use if a future call
2413 * to dbus_connection_open() asks to connect to the same server.
2415 * Use dbus_connection_open_private() to get a dedicated connection
2416 * not shared with other callers of dbus_connection_open().
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 * Because this connection is shared, no user of the connection
2424 * may call dbus_connection_close(). However, when you are done with the
2425 * connection you should call dbus_connection_unref().
2427 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2428 * unless you have good reason; connections are expensive enough
2429 * that it's wasteful to create lots of connections to the same
2432 * @param address the address.
2433 * @param error address where an error can be returned.
2434 * @returns new connection, or #NULL on failure.
2437 dbus_connection_open (const char *address,
2440 DBusConnection *connection;
2442 _dbus_return_val_if_fail (address != NULL, NULL);
2443 _dbus_return_val_if_error_is_set (error, NULL);
2445 connection = _dbus_connection_open_internal (address,
2453 * Opens a new, dedicated connection to a remote address. Unlike
2454 * dbus_connection_open(), always creates a new connection.
2455 * This connection will not be saved or recycled by libdbus.
2457 * If the open fails, the function returns #NULL, and provides a
2458 * reason for the failure in the error parameter. Pass #NULL for the
2459 * error parameter if you aren't interested in the reason for
2462 * When you are done with this connection, you must
2463 * dbus_connection_close() to disconnect it,
2464 * and dbus_connection_unref() to free the connection object.
2466 * (The dbus_connection_close() can be skipped if the
2467 * connection is already known to be disconnected, for example
2468 * if you are inside a handler for the Disconnected signal.)
2470 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2471 * unless you have good reason; connections are expensive enough
2472 * that it's wasteful to create lots of connections to the same
2475 * @param address the address.
2476 * @param error address where an error can be returned.
2477 * @returns new connection, or #NULL on failure.
2480 dbus_connection_open_private (const char *address,
2483 DBusConnection *connection;
2485 _dbus_return_val_if_fail (address != NULL, NULL);
2486 _dbus_return_val_if_error_is_set (error, NULL);
2488 connection = _dbus_connection_open_internal (address,
2496 * Increments the reference count of a DBusConnection.
2498 * @param connection the connection.
2499 * @returns the connection.
2502 dbus_connection_ref (DBusConnection *connection)
2504 _dbus_return_val_if_fail (connection != NULL, NULL);
2505 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
2507 /* The connection lock is better than the global
2508 * lock in the atomic increment fallback
2511 #ifdef DBUS_HAVE_ATOMIC_INT
2512 _dbus_atomic_inc (&connection->refcount);
2514 CONNECTION_LOCK (connection);
2515 _dbus_assert (connection->refcount.value > 0);
2517 connection->refcount.value += 1;
2518 CONNECTION_UNLOCK (connection);
2525 free_outgoing_message (void *element,
2528 DBusMessage *message = element;
2529 DBusConnection *connection = data;
2531 _dbus_message_remove_size_counter (message,
2532 connection->outgoing_counter,
2534 dbus_message_unref (message);
2537 /* This is run without the mutex held, but after the last reference
2538 * to the connection has been dropped we should have no thread-related
2542 _dbus_connection_last_unref (DBusConnection *connection)
2546 _dbus_verbose ("Finalizing connection %p\n", connection);
2548 _dbus_assert (connection->refcount.value == 0);
2550 /* You have to disconnect the connection before unref:ing it. Otherwise
2551 * you won't get the disconnected message.
2553 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
2554 _dbus_assert (connection->server_guid == NULL);
2556 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
2557 _dbus_object_tree_free_all_unlocked (connection->objects);
2559 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
2560 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
2561 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
2563 _dbus_watch_list_free (connection->watches);
2564 connection->watches = NULL;
2566 _dbus_timeout_list_free (connection->timeouts);
2567 connection->timeouts = NULL;
2569 _dbus_data_slot_list_free (&connection->slot_list);
2571 link = _dbus_list_get_first_link (&connection->filter_list);
2572 while (link != NULL)
2574 DBusMessageFilter *filter = link->data;
2575 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
2577 filter->function = NULL;
2578 _dbus_message_filter_unref (filter); /* calls app callback */
2583 _dbus_list_clear (&connection->filter_list);
2585 /* ---- Done with stuff that invokes application callbacks */
2587 _dbus_object_tree_unref (connection->objects);
2589 _dbus_hash_table_unref (connection->pending_replies);
2590 connection->pending_replies = NULL;
2592 _dbus_list_clear (&connection->filter_list);
2594 _dbus_list_foreach (&connection->outgoing_messages,
2595 free_outgoing_message,
2597 _dbus_list_clear (&connection->outgoing_messages);
2599 _dbus_list_foreach (&connection->incoming_messages,
2600 (DBusForeachFunction) dbus_message_unref,
2602 _dbus_list_clear (&connection->incoming_messages);
2604 _dbus_counter_unref (connection->outgoing_counter);
2606 _dbus_transport_unref (connection->transport);
2608 if (connection->disconnect_message_link)
2610 DBusMessage *message = connection->disconnect_message_link->data;
2611 dbus_message_unref (message);
2612 _dbus_list_free_link (connection->disconnect_message_link);
2615 _dbus_list_clear (&connection->link_cache);
2617 _dbus_condvar_free_at_location (&connection->dispatch_cond);
2618 _dbus_condvar_free_at_location (&connection->io_path_cond);
2620 _dbus_mutex_free_at_location (&connection->io_path_mutex);
2621 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
2623 _dbus_mutex_free_at_location (&connection->mutex);
2625 dbus_free (connection);
2629 * Decrements the reference count of a DBusConnection, and finalizes
2630 * it if the count reaches zero.
2632 * Note: it is a bug to drop the last reference to a connection that
2633 * is still connected.
2635 * For shared connections, libdbus will own a reference
2636 * as long as the connection is connected, so you can know that either
2637 * you don't have the last reference, or it's OK to drop the last reference.
2638 * Most connections are shared. dbus_connection_open() and dbus_bus_get()
2639 * return shared connections.
2641 * For private connections, the creator of the connection must arrange for
2642 * dbus_connection_close() to be called prior to dropping the last reference.
2643 * Private connections come from dbus_connection_open_private() or dbus_bus_get_private().
2645 * @param connection the connection.
2648 dbus_connection_unref (DBusConnection *connection)
2650 dbus_bool_t last_unref;
2652 _dbus_return_if_fail (connection != NULL);
2653 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2655 /* The connection lock is better than the global
2656 * lock in the atomic increment fallback
2659 #ifdef DBUS_HAVE_ATOMIC_INT
2660 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
2662 CONNECTION_LOCK (connection);
2664 _dbus_assert (connection->refcount.value > 0);
2666 connection->refcount.value -= 1;
2667 last_unref = (connection->refcount.value == 0);
2670 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
2673 CONNECTION_UNLOCK (connection);
2678 #ifndef DBUS_DISABLE_CHECKS
2679 if (_dbus_transport_get_is_connected (connection->transport))
2681 _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",
2682 connection->shareable ?
2683 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" :
2684 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
2688 _dbus_connection_last_unref (connection);
2693 * Note that the transport can disconnect itself (other end drops us)
2694 * and in that case this function never runs. So this function must
2695 * not do anything more than disconnect the transport and update the
2698 * If the transport self-disconnects, then we assume someone will
2699 * dispatch the connection to cause the dispatch status update.
2702 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2704 DBusDispatchStatus status;
2706 HAVE_LOCK_CHECK (connection);
2708 _dbus_verbose ("Disconnecting %p\n", connection);
2710 /* We need to ref because update_dispatch_status_and_unlock will unref
2711 * the connection if it was shared and libdbus was the only remaining
2714 _dbus_connection_ref_unlocked (connection);
2716 _dbus_transport_disconnect (connection->transport);
2718 /* This has the side effect of queuing the disconnect message link
2719 * (unless we don't have enough memory, possibly, so don't assert it).
2720 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
2721 * should never again return the newly-disconnected connection.
2723 * However, we only unref the shared connection and exit_on_disconnect when
2724 * the disconnect message reaches the head of the message queue,
2725 * NOT when it's first queued.
2727 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2729 /* This calls out to user code */
2730 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2732 /* Could also call out to user code */
2733 dbus_connection_unref (connection);
2737 * Closes a private connection, so no further data can be sent or received.
2738 * This disconnects the transport (such as a socket) underlying the
2741 * Attempts to send messages after closing a connection are safe, but will result in
2742 * error replies generated locally in libdbus.
2744 * This function does not affect the connection's reference count. It's
2745 * safe to close a connection more than once; all calls after the
2746 * first do nothing. It's impossible to "reopen" a connection, a
2747 * new connection must be created. This function may result in a call
2748 * to the DBusDispatchStatusFunction set with
2749 * dbus_connection_set_dispatch_status_function(), as the disconnect
2750 * message it generates needs to be dispatched.
2752 * If a connection is dropped by the remote application, it will
2755 * You must close a connection prior to releasing the last reference to
2756 * the connection. If you dbus_connection_unref() for the last time
2757 * without closing the connection, the results are undefined; it
2758 * is a bug in your program and libdbus will try to print a warning.
2760 * You may not close a shared connection. Connections created with
2761 * dbus_connection_open() or dbus_bus_get() are shared.
2762 * These connections are owned by libdbus, and applications should
2763 * only unref them, never close them. Applications can know it is
2764 * safe to unref these connections because libdbus will be holding a
2765 * reference as long as the connection is open. Thus, either the
2766 * connection is closed and it is OK to drop the last reference,
2767 * or the connection is open and the app knows it does not have the
2770 * Connections created with dbus_connection_open_private() or
2771 * dbus_bus_get_private() are not kept track of or referenced by
2772 * libdbus. The creator of these connections is responsible for
2773 * calling dbus_connection_close() prior to releasing the last
2774 * reference, if the connection is not already disconnected.
2776 * @param connection the private (unshared) connection to close
2779 dbus_connection_close (DBusConnection *connection)
2781 _dbus_return_if_fail (connection != NULL);
2782 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2784 CONNECTION_LOCK (connection);
2786 #ifndef DBUS_DISABLE_CHECKS
2787 if (connection->shareable)
2789 CONNECTION_UNLOCK (connection);
2791 _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
2796 _dbus_connection_close_possibly_shared_and_unlock (connection);
2800 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2802 HAVE_LOCK_CHECK (connection);
2803 return _dbus_transport_get_is_connected (connection->transport);
2807 * Gets whether the connection is currently open. A connection may
2808 * become disconnected when the remote application closes its end, or
2809 * exits; a connection may also be disconnected with
2810 * dbus_connection_close().
2812 * There are not separate states for "closed" and "disconnected," the two
2813 * terms are synonymous. This function should really be called
2814 * get_is_open() but for historical reasons is not.
2816 * @param connection the connection.
2817 * @returns #TRUE if the connection is still alive.
2820 dbus_connection_get_is_connected (DBusConnection *connection)
2824 _dbus_return_val_if_fail (connection != NULL, FALSE);
2826 CONNECTION_LOCK (connection);
2827 res = _dbus_connection_get_is_connected_unlocked (connection);
2828 CONNECTION_UNLOCK (connection);
2834 * Gets whether the connection was authenticated. (Note that
2835 * if the connection was authenticated then disconnected,
2836 * this function still returns #TRUE)
2838 * @param connection the connection
2839 * @returns #TRUE if the connection was ever authenticated
2842 dbus_connection_get_is_authenticated (DBusConnection *connection)
2846 _dbus_return_val_if_fail (connection != NULL, FALSE);
2848 CONNECTION_LOCK (connection);
2849 res = _dbus_transport_get_is_authenticated (connection->transport);
2850 CONNECTION_UNLOCK (connection);
2856 * Gets whether the connection is not authenticated as a specific
2857 * user. If the connection is not authenticated, this function
2858 * returns #TRUE, and if it is authenticated but as an anonymous user,
2859 * it returns #TRUE. If it is authenticated as a specific user, then
2860 * this returns #FALSE. (Note that if the connection was authenticated
2861 * as anonymous then disconnected, this function still returns #TRUE.)
2863 * If the connection is not anonymous, you can use
2864 * dbus_connection_get_unix_user() and
2865 * dbus_connection_get_windows_user() to see who it's authorized as.
2867 * If you want to prevent non-anonymous authorization, use
2868 * dbus_server_set_auth_mechanisms() to remove the mechanisms that
2869 * allow proving user identity (i.e. only allow the ANONYMOUS
2872 * @param connection the connection
2873 * @returns #TRUE if not authenticated or authenticated as anonymous
2876 dbus_connection_get_is_anonymous (DBusConnection *connection)
2880 _dbus_return_val_if_fail (connection != NULL, FALSE);
2882 CONNECTION_LOCK (connection);
2883 res = _dbus_transport_get_is_anonymous (connection->transport);
2884 CONNECTION_UNLOCK (connection);
2890 * Gets the ID of the server address we are authenticated to, if this
2891 * connection is on the client side. If the connection is on the
2892 * server side, this will always return #NULL - use dbus_server_get_id()
2893 * to get the ID of your own server, if you are the server side.
2895 * If a client-side connection is not authenticated yet, the ID may be
2896 * available if it was included in the server address, but may not be
2897 * available. The only way to be sure the server ID is available
2898 * is to wait for authentication to complete.
2900 * In general, each mode of connecting to a given server will have
2901 * its own ID. So for example, if the session bus daemon is listening
2902 * on UNIX domain sockets and on TCP, then each of those modalities
2903 * will have its own server ID.
2905 * If you want an ID that identifies an entire session bus, look at
2906 * dbus_bus_get_id() instead (which is just a convenience wrapper
2907 * around the org.freedesktop.DBus.GetId method invoked on the bus).
2909 * You can also get a machine ID; see dbus_get_local_machine_id() to
2910 * get the machine you are on. There isn't a convenience wrapper, but
2911 * you can invoke org.freedesktop.DBus.Peer.GetMachineId on any peer
2912 * to get the machine ID on the other end.
2914 * The D-Bus specification describes the server ID and other IDs in a
2917 * @param connection the connection
2918 * @returns the server ID or #NULL if no memory or the connection is server-side
2921 dbus_connection_get_server_id (DBusConnection *connection)
2925 _dbus_return_val_if_fail (connection != NULL, FALSE);
2927 CONNECTION_LOCK (connection);
2928 id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
2929 CONNECTION_UNLOCK (connection);
2935 * Set whether _exit() should be called when the connection receives a
2936 * disconnect signal. The call to _exit() comes after any handlers for
2937 * the disconnect signal run; handlers can cancel the exit by calling
2940 * By default, exit_on_disconnect is #FALSE; but for message bus
2941 * connections returned from dbus_bus_get() it will be toggled on
2944 * @param connection the connection
2945 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
2948 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
2949 dbus_bool_t exit_on_disconnect)
2951 _dbus_return_if_fail (connection != NULL);
2953 CONNECTION_LOCK (connection);
2954 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
2955 CONNECTION_UNLOCK (connection);
2959 * Preallocates resources needed to send a message, allowing the message
2960 * to be sent without the possibility of memory allocation failure.
2961 * Allows apps to create a future guarantee that they can send
2962 * a message regardless of memory shortages.
2964 * @param connection the connection we're preallocating for.
2965 * @returns the preallocated resources, or #NULL
2967 DBusPreallocatedSend*
2968 dbus_connection_preallocate_send (DBusConnection *connection)
2970 DBusPreallocatedSend *preallocated;
2972 _dbus_return_val_if_fail (connection != NULL, NULL);
2974 CONNECTION_LOCK (connection);
2977 _dbus_connection_preallocate_send_unlocked (connection);
2979 CONNECTION_UNLOCK (connection);
2981 return preallocated;
2985 * Frees preallocated message-sending resources from
2986 * dbus_connection_preallocate_send(). Should only
2987 * be called if the preallocated resources are not used
2988 * to send a message.
2990 * @param connection the connection
2991 * @param preallocated the resources
2994 dbus_connection_free_preallocated_send (DBusConnection *connection,
2995 DBusPreallocatedSend *preallocated)
2997 _dbus_return_if_fail (connection != NULL);
2998 _dbus_return_if_fail (preallocated != NULL);
2999 _dbus_return_if_fail (connection == preallocated->connection);
3001 _dbus_list_free_link (preallocated->queue_link);
3002 _dbus_counter_unref (preallocated->counter_link->data);
3003 _dbus_list_free_link (preallocated->counter_link);
3004 dbus_free (preallocated);
3008 * Sends a message using preallocated resources. This function cannot fail.
3009 * It works identically to dbus_connection_send() in other respects.
3010 * Preallocated resources comes from dbus_connection_preallocate_send().
3011 * This function "consumes" the preallocated resources, they need not
3012 * be freed separately.
3014 * @param connection the connection
3015 * @param preallocated the preallocated resources
3016 * @param message the message to send
3017 * @param client_serial return location for client serial assigned to the message
3020 dbus_connection_send_preallocated (DBusConnection *connection,
3021 DBusPreallocatedSend *preallocated,
3022 DBusMessage *message,
3023 dbus_uint32_t *client_serial)
3025 _dbus_return_if_fail (connection != NULL);
3026 _dbus_return_if_fail (preallocated != NULL);
3027 _dbus_return_if_fail (message != NULL);
3028 _dbus_return_if_fail (preallocated->connection == connection);
3029 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
3030 dbus_message_get_member (message) != NULL);
3031 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
3032 (dbus_message_get_interface (message) != NULL &&
3033 dbus_message_get_member (message) != NULL));
3035 CONNECTION_LOCK (connection);
3036 _dbus_connection_send_preallocated_and_unlock (connection,
3038 message, client_serial);
3042 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
3043 DBusMessage *message,
3044 dbus_uint32_t *client_serial)
3046 DBusPreallocatedSend *preallocated;
3048 _dbus_assert (connection != NULL);
3049 _dbus_assert (message != NULL);
3051 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3052 if (preallocated == NULL)
3055 _dbus_connection_send_preallocated_unlocked_no_update (connection,
3063 * Adds a message to the outgoing message queue. Does not block to
3064 * write the message to the network; that happens asynchronously. To
3065 * force the message to be written, call dbus_connection_flush() however
3066 * it is not necessary to call dbus_connection_flush() by hand; the
3067 * message will be sent the next time the main loop is run.
3068 * dbus_connection_flush() should only be used, for example, if
3069 * the application was expected to exit before running the main loop.
3071 * Because this only queues the message, the only reason it can
3072 * fail is lack of memory. Even if the connection is disconnected,
3073 * no error will be returned. If the function fails due to lack of memory,
3074 * it returns #FALSE. The function will never fail for other reasons; even
3075 * if the connection is disconnected, you can queue an outgoing message,
3076 * though obviously it won't be sent.
3078 * The message serial is used by the remote application to send a
3079 * reply; see dbus_message_get_serial() or the D-Bus specification.
3081 * dbus_message_unref() can be called as soon as this method returns
3082 * as the message queue will hold its own ref until the message is sent.
3084 * @param connection the connection.
3085 * @param message the message to write.
3086 * @param serial return location for message serial, or #NULL if you don't care
3087 * @returns #TRUE on success.
3090 dbus_connection_send (DBusConnection *connection,
3091 DBusMessage *message,
3092 dbus_uint32_t *serial)
3094 _dbus_return_val_if_fail (connection != NULL, FALSE);
3095 _dbus_return_val_if_fail (message != NULL, FALSE);
3097 CONNECTION_LOCK (connection);
3099 return _dbus_connection_send_and_unlock (connection,
3105 reply_handler_timeout (void *data)
3107 DBusConnection *connection;
3108 DBusDispatchStatus status;
3109 DBusPendingCall *pending = data;
3111 connection = _dbus_pending_call_get_connection_and_lock (pending);
3113 _dbus_pending_call_queue_timeout_error_unlocked (pending,
3115 _dbus_connection_remove_timeout_unlocked (connection,
3116 _dbus_pending_call_get_timeout_unlocked (pending));
3117 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
3119 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3120 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3122 /* Unlocks, and calls out to user code */
3123 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3129 * Queues a message to send, as with dbus_connection_send(),
3130 * but also returns a #DBusPendingCall used to receive a reply to the
3131 * message. If no reply is received in the given timeout_milliseconds,
3132 * this function expires the pending reply and generates a synthetic
3133 * error reply (generated in-process, not by the remote application)
3134 * indicating that a timeout occurred.
3136 * A #DBusPendingCall will see a reply message before any filters or
3137 * registered object path handlers. See dbus_connection_dispatch() for
3138 * details on when handlers are run.
3140 * A #DBusPendingCall will always see exactly one reply message,
3141 * unless it's cancelled with dbus_pending_call_cancel().
3143 * If #NULL is passed for the pending_return, the #DBusPendingCall
3144 * will still be generated internally, and used to track
3145 * the message reply timeout. This means a timeout error will
3146 * occur if no reply arrives, unlike with dbus_connection_send().
3148 * If -1 is passed for the timeout, a sane default timeout is used. -1
3149 * is typically the best value for the timeout for this reason, unless
3150 * you want a very short or very long timeout. There is no way to
3151 * avoid a timeout entirely, other than passing INT_MAX for the
3152 * timeout to mean "very long timeout." libdbus clamps an INT_MAX
3153 * timeout down to a few hours timeout though.
3155 * @warning if the connection is disconnected, the #DBusPendingCall
3156 * will be set to #NULL, so be careful with this.
3158 * @param connection the connection
3159 * @param message the message to send
3160 * @param pending_return return location for a #DBusPendingCall object, or #NULL if connection is disconnected
3161 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3162 * @returns #FALSE if no memory, #TRUE otherwise.
3166 dbus_connection_send_with_reply (DBusConnection *connection,
3167 DBusMessage *message,
3168 DBusPendingCall **pending_return,
3169 int timeout_milliseconds)
3171 DBusPendingCall *pending;
3172 dbus_int32_t serial = -1;
3173 DBusDispatchStatus status;
3175 _dbus_return_val_if_fail (connection != NULL, FALSE);
3176 _dbus_return_val_if_fail (message != NULL, FALSE);
3177 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3180 *pending_return = NULL;
3182 CONNECTION_LOCK (connection);
3184 if (!_dbus_connection_get_is_connected_unlocked (connection))
3186 CONNECTION_UNLOCK (connection);
3191 pending = _dbus_pending_call_new_unlocked (connection,
3192 timeout_milliseconds,
3193 reply_handler_timeout);
3195 if (pending == NULL)
3197 CONNECTION_UNLOCK (connection);
3201 /* Assign a serial to the message */
3202 serial = dbus_message_get_serial (message);
3205 serial = _dbus_connection_get_next_client_serial (connection);
3206 _dbus_message_set_serial (message, serial);
3209 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
3212 /* Insert the serial in the pending replies hash;
3213 * hash takes a refcount on DBusPendingCall.
3214 * Also, add the timeout.
3216 if (!_dbus_connection_attach_pending_call_unlocked (connection,
3220 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
3222 _dbus_connection_detach_pending_call_and_unlock (connection,
3224 goto error_unlocked;
3228 *pending_return = pending; /* hand off refcount */
3231 _dbus_connection_detach_pending_call_unlocked (connection, pending);
3232 /* we still have a ref to the pending call in this case, we unref
3233 * after unlocking, below
3237 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3239 /* this calls out to user code */
3240 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3242 if (pending_return == NULL)
3243 dbus_pending_call_unref (pending);
3248 CONNECTION_UNLOCK (connection);
3250 dbus_pending_call_unref (pending);
3255 * Sends a message and blocks a certain time period while waiting for
3256 * a reply. This function does not reenter the main loop,
3257 * i.e. messages other than the reply are queued up but not
3258 * processed. This function is used to invoke method calls on a
3261 * If a normal reply is received, it is returned, and removed from the
3262 * incoming message queue. If it is not received, #NULL is returned
3263 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
3264 * received, it is converted to a #DBusError and returned as an error,
3265 * then the reply message is deleted and #NULL is returned. If
3266 * something else goes wrong, result is set to whatever is
3267 * appropriate, such as #DBUS_ERROR_NO_MEMORY or
3268 * #DBUS_ERROR_DISCONNECTED.
3270 * @warning While this function blocks the calling thread will not be
3271 * processing the incoming message queue. This means you can end up
3272 * deadlocked if the application you're talking to needs you to reply
3273 * to a method. To solve this, either avoid the situation, block in a
3274 * separate thread from the main connection-dispatching thread, or use
3275 * dbus_pending_call_set_notify() to avoid blocking.
3277 * @param connection the connection
3278 * @param message the message to send
3279 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3280 * @param error return location for error message
3281 * @returns the message that is the reply or #NULL with an error code if the
3285 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
3286 DBusMessage *message,
3287 int timeout_milliseconds,
3291 DBusPendingCall *pending;
3293 _dbus_return_val_if_fail (connection != NULL, NULL);
3294 _dbus_return_val_if_fail (message != NULL, NULL);
3295 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3296 _dbus_return_val_if_error_is_set (error, NULL);
3298 if (!dbus_connection_send_with_reply (connection, message,
3299 &pending, timeout_milliseconds))
3301 _DBUS_SET_OOM (error);
3305 if (pending == NULL)
3307 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
3311 dbus_pending_call_block (pending);
3313 reply = dbus_pending_call_steal_reply (pending);
3314 dbus_pending_call_unref (pending);
3316 /* call_complete_and_unlock() called from pending_call_block() should
3317 * always fill this in.
3319 _dbus_assert (reply != NULL);
3321 if (dbus_set_error_from_message (error, reply))
3323 dbus_message_unref (reply);
3331 * Blocks until the outgoing message queue is empty.
3332 * Assumes connection lock already held.
3334 * If you call this, you MUST call update_dispatch_status afterword...
3336 * @param connection the connection.
3339 _dbus_connection_flush_unlocked (DBusConnection *connection)
3341 /* We have to specify DBUS_ITERATION_DO_READING here because
3342 * otherwise we could have two apps deadlock if they are both doing
3343 * a flush(), and the kernel buffers fill up. This could change the
3346 DBusDispatchStatus status;
3348 HAVE_LOCK_CHECK (connection);
3350 while (connection->n_outgoing > 0 &&
3351 _dbus_connection_get_is_connected_unlocked (connection))
3353 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3354 HAVE_LOCK_CHECK (connection);
3355 _dbus_connection_do_iteration_unlocked (connection,
3356 DBUS_ITERATION_DO_READING |
3357 DBUS_ITERATION_DO_WRITING |
3358 DBUS_ITERATION_BLOCK,
3362 HAVE_LOCK_CHECK (connection);
3363 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3364 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3366 HAVE_LOCK_CHECK (connection);
3371 * Blocks until the outgoing message queue is empty.
3373 * @param connection the connection.
3376 dbus_connection_flush (DBusConnection *connection)
3378 /* We have to specify DBUS_ITERATION_DO_READING here because
3379 * otherwise we could have two apps deadlock if they are both doing
3380 * a flush(), and the kernel buffers fill up. This could change the
3383 DBusDispatchStatus status;
3385 _dbus_return_if_fail (connection != NULL);
3387 CONNECTION_LOCK (connection);
3389 status = _dbus_connection_flush_unlocked (connection);
3391 HAVE_LOCK_CHECK (connection);
3392 /* Unlocks and calls out to user code */
3393 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3395 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
3399 * This function implements dbus_connection_read_write_dispatch() and
3400 * dbus_connection_read_write() (they pass a different value for the
3401 * dispatch parameter).
3403 * @param connection the connection
3404 * @param timeout_milliseconds max time to block or -1 for infinite
3405 * @param dispatch dispatch new messages or leave them on the incoming queue
3406 * @returns #TRUE if the disconnect message has not been processed
3409 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3410 int timeout_milliseconds,
3411 dbus_bool_t dispatch)
3413 DBusDispatchStatus dstatus;
3414 dbus_bool_t no_progress_possible;
3416 dstatus = dbus_connection_get_dispatch_status (connection);
3418 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3420 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
3421 dbus_connection_dispatch (connection);
3422 CONNECTION_LOCK (connection);
3424 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3426 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
3427 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3428 CONNECTION_LOCK (connection);
3432 CONNECTION_LOCK (connection);
3433 if (_dbus_connection_get_is_connected_unlocked (connection))
3435 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3436 _dbus_connection_do_iteration_unlocked (connection,
3437 DBUS_ITERATION_DO_READING |
3438 DBUS_ITERATION_DO_WRITING |
3439 DBUS_ITERATION_BLOCK,
3440 timeout_milliseconds);
3444 HAVE_LOCK_CHECK (connection);
3445 /* If we can dispatch, we can make progress until the Disconnected message
3446 * has been processed; if we can only read/write, we can make progress
3447 * as long as the transport is open.
3450 no_progress_possible = connection->n_incoming == 0 &&
3451 connection->disconnect_message_link == NULL;
3453 no_progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
3454 CONNECTION_UNLOCK (connection);
3455 return !no_progress_possible; /* TRUE if we can make more progress */
3460 * This function is intended for use with applications that don't want
3461 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3462 * example usage would be:
3465 * while (dbus_connection_read_write_dispatch (connection, -1))
3466 * ; // empty loop body
3469 * In this usage you would normally have set up a filter function to look
3470 * at each message as it is dispatched. The loop terminates when the last
3471 * message from the connection (the disconnected signal) is processed.
3473 * If there are messages to dispatch, this function will
3474 * dbus_connection_dispatch() once, and return. If there are no
3475 * messages to dispatch, this function will block until it can read or
3476 * write, then read or write, then return.
3478 * The way to think of this function is that it either makes some sort
3479 * of progress, or it blocks. Note that, while it is blocked on I/O, it
3480 * cannot be interrupted (even by other threads), which makes this function
3481 * unsuitable for applications that do more than just react to received
3484 * The return value indicates whether the disconnect message has been
3485 * processed, NOT whether the connection is connected. This is
3486 * important because even after disconnecting, you want to process any
3487 * messages you received prior to the disconnect.
3489 * @param connection the connection
3490 * @param timeout_milliseconds max time to block or -1 for infinite
3491 * @returns #TRUE if the disconnect message has not been processed
3494 dbus_connection_read_write_dispatch (DBusConnection *connection,
3495 int timeout_milliseconds)
3497 _dbus_return_val_if_fail (connection != NULL, FALSE);
3498 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3499 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3503 * This function is intended for use with applications that don't want to
3504 * write a main loop and deal with #DBusWatch and #DBusTimeout. See also
3505 * dbus_connection_read_write_dispatch().
3507 * As long as the connection is open, this function will block until it can
3508 * read or write, then read or write, then return #TRUE.
3510 * If the connection is closed, the function returns #FALSE.
3512 * The return value indicates whether reading or writing is still
3513 * possible, i.e. whether the connection is connected.
3515 * Note that even after disconnection, messages may remain in the
3516 * incoming queue that need to be
3517 * processed. dbus_connection_read_write_dispatch() dispatches
3518 * incoming messages for you; with dbus_connection_read_write() you
3519 * have to arrange to drain the incoming queue yourself.
3521 * @param connection the connection
3522 * @param timeout_milliseconds max time to block or -1 for infinite
3523 * @returns #TRUE if still connected
3526 dbus_connection_read_write (DBusConnection *connection,
3527 int timeout_milliseconds)
3529 _dbus_return_val_if_fail (connection != NULL, FALSE);
3530 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3531 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3534 /* We need to call this anytime we pop the head of the queue, and then
3535 * update_dispatch_status_and_unlock needs to be called afterward
3536 * which will "process" the disconnected message and set
3537 * disconnected_message_processed.
3540 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3541 DBusMessage *head_of_queue)
3543 HAVE_LOCK_CHECK (connection);
3545 /* checking that the link is NULL is an optimization to avoid the is_signal call */
3546 if (connection->disconnect_message_link == NULL &&
3547 dbus_message_is_signal (head_of_queue,
3548 DBUS_INTERFACE_LOCAL,
3551 connection->disconnected_message_arrived = TRUE;
3556 * Returns the first-received message from the incoming message queue,
3557 * leaving it in the queue. If the queue is empty, returns #NULL.
3559 * The caller does not own a reference to the returned message, and
3560 * must either return it using dbus_connection_return_message() or
3561 * keep it after calling dbus_connection_steal_borrowed_message(). No
3562 * one can get at the message while its borrowed, so return it as
3563 * quickly as possible and don't keep a reference to it after
3564 * returning it. If you need to keep the message, make a copy of it.
3566 * dbus_connection_dispatch() will block if called while a borrowed
3567 * message is outstanding; only one piece of code can be playing with
3568 * the incoming queue at a time. This function will block if called
3569 * during a dbus_connection_dispatch().
3571 * @param connection the connection.
3572 * @returns next message in the incoming queue.
3575 dbus_connection_borrow_message (DBusConnection *connection)
3577 DBusDispatchStatus status;
3578 DBusMessage *message;
3580 _dbus_return_val_if_fail (connection != NULL, NULL);
3582 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3584 /* this is called for the side effect that it queues
3585 * up any messages from the transport
3587 status = dbus_connection_get_dispatch_status (connection);
3588 if (status != DBUS_DISPATCH_DATA_REMAINS)
3591 CONNECTION_LOCK (connection);
3593 _dbus_connection_acquire_dispatch (connection);
3595 /* While a message is outstanding, the dispatch lock is held */
3596 _dbus_assert (connection->message_borrowed == NULL);
3598 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3600 message = connection->message_borrowed;
3602 check_disconnected_message_arrived_unlocked (connection, message);
3604 /* Note that we KEEP the dispatch lock until the message is returned */
3605 if (message == NULL)
3606 _dbus_connection_release_dispatch (connection);
3608 CONNECTION_UNLOCK (connection);
3610 /* We don't update dispatch status until it's returned or stolen */
3616 * Used to return a message after peeking at it using
3617 * dbus_connection_borrow_message(). Only called if
3618 * message from dbus_connection_borrow_message() was non-#NULL.
3620 * @param connection the connection
3621 * @param message the message from dbus_connection_borrow_message()
3624 dbus_connection_return_message (DBusConnection *connection,
3625 DBusMessage *message)
3627 DBusDispatchStatus status;
3629 _dbus_return_if_fail (connection != NULL);
3630 _dbus_return_if_fail (message != NULL);
3631 _dbus_return_if_fail (message == connection->message_borrowed);
3632 _dbus_return_if_fail (connection->dispatch_acquired);
3634 CONNECTION_LOCK (connection);
3636 _dbus_assert (message == connection->message_borrowed);
3638 connection->message_borrowed = NULL;
3640 _dbus_connection_release_dispatch (connection);
3642 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3643 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3647 * Used to keep a message after peeking at it using
3648 * dbus_connection_borrow_message(). Before using this function, see
3649 * the caveats/warnings in the documentation for
3650 * dbus_connection_pop_message().
3652 * @param connection the connection
3653 * @param message the message from dbus_connection_borrow_message()
3656 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3657 DBusMessage *message)
3659 DBusMessage *pop_message;
3660 DBusDispatchStatus status;
3662 _dbus_return_if_fail (connection != NULL);
3663 _dbus_return_if_fail (message != NULL);
3664 _dbus_return_if_fail (message == connection->message_borrowed);
3665 _dbus_return_if_fail (connection->dispatch_acquired);
3667 CONNECTION_LOCK (connection);
3669 _dbus_assert (message == connection->message_borrowed);
3671 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3672 _dbus_assert (message == pop_message);
3674 connection->n_incoming -= 1;
3676 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3677 message, connection->n_incoming);
3679 connection->message_borrowed = NULL;
3681 _dbus_connection_release_dispatch (connection);
3683 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3684 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3687 /* See dbus_connection_pop_message, but requires the caller to own
3688 * the lock before calling. May drop the lock while running.
3691 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3693 HAVE_LOCK_CHECK (connection);
3695 _dbus_assert (connection->message_borrowed == NULL);
3697 if (connection->n_incoming > 0)
3701 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3702 connection->n_incoming -= 1;
3704 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3706 dbus_message_get_type (link->data),
3707 dbus_message_get_path (link->data) ?
3708 dbus_message_get_path (link->data) :
3710 dbus_message_get_interface (link->data) ?
3711 dbus_message_get_interface (link->data) :
3713 dbus_message_get_member (link->data) ?
3714 dbus_message_get_member (link->data) :
3716 dbus_message_get_signature (link->data),
3717 connection, connection->n_incoming);
3719 check_disconnected_message_arrived_unlocked (connection, link->data);
3727 /* See dbus_connection_pop_message, but requires the caller to own
3728 * the lock before calling. May drop the lock while running.
3731 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3735 HAVE_LOCK_CHECK (connection);
3737 link = _dbus_connection_pop_message_link_unlocked (connection);
3741 DBusMessage *message;
3743 message = link->data;
3745 _dbus_list_free_link (link);
3754 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3755 DBusList *message_link)
3757 HAVE_LOCK_CHECK (connection);
3759 _dbus_assert (message_link != NULL);
3760 /* You can't borrow a message while a link is outstanding */
3761 _dbus_assert (connection->message_borrowed == NULL);
3762 /* We had to have the dispatch lock across the pop/putback */
3763 _dbus_assert (connection->dispatch_acquired);
3765 _dbus_list_prepend_link (&connection->incoming_messages,
3767 connection->n_incoming += 1;
3769 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3771 dbus_message_get_type (message_link->data),
3772 dbus_message_get_interface (message_link->data) ?
3773 dbus_message_get_interface (message_link->data) :
3775 dbus_message_get_member (message_link->data) ?
3776 dbus_message_get_member (message_link->data) :
3778 dbus_message_get_signature (message_link->data),
3779 connection, connection->n_incoming);
3783 * Returns the first-received message from the incoming message queue,
3784 * removing it from the queue. The caller owns a reference to the
3785 * returned message. If the queue is empty, returns #NULL.
3787 * This function bypasses any message handlers that are registered,
3788 * and so using it is usually wrong. Instead, let the main loop invoke
3789 * dbus_connection_dispatch(). Popping messages manually is only
3790 * useful in very simple programs that don't share a #DBusConnection
3791 * with any libraries or other modules.
3793 * There is a lock that covers all ways of accessing the incoming message
3794 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3795 * dbus_connection_borrow_message(), etc. will all block while one of the others
3796 * in the group is running.
3798 * @param connection the connection.
3799 * @returns next message in the incoming queue.
3802 dbus_connection_pop_message (DBusConnection *connection)
3804 DBusMessage *message;
3805 DBusDispatchStatus status;
3807 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3809 /* this is called for the side effect that it queues
3810 * up any messages from the transport
3812 status = dbus_connection_get_dispatch_status (connection);
3813 if (status != DBUS_DISPATCH_DATA_REMAINS)
3816 CONNECTION_LOCK (connection);
3817 _dbus_connection_acquire_dispatch (connection);
3818 HAVE_LOCK_CHECK (connection);
3820 message = _dbus_connection_pop_message_unlocked (connection);
3822 _dbus_verbose ("Returning popped message %p\n", message);
3824 _dbus_connection_release_dispatch (connection);
3826 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3827 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3833 * Acquire the dispatcher. This is a separate lock so the main
3834 * connection lock can be dropped to call out to application dispatch
3837 * @param connection the connection.
3840 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3842 HAVE_LOCK_CHECK (connection);
3844 _dbus_connection_ref_unlocked (connection);
3845 CONNECTION_UNLOCK (connection);
3847 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3848 _dbus_mutex_lock (connection->dispatch_mutex);
3850 while (connection->dispatch_acquired)
3852 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3853 _dbus_condvar_wait (connection->dispatch_cond,
3854 connection->dispatch_mutex);
3857 _dbus_assert (!connection->dispatch_acquired);
3859 connection->dispatch_acquired = TRUE;
3861 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3862 _dbus_mutex_unlock (connection->dispatch_mutex);
3864 CONNECTION_LOCK (connection);
3865 _dbus_connection_unref_unlocked (connection);
3869 * Release the dispatcher when you're done with it. Only call
3870 * after you've acquired the dispatcher. Wakes up at most one
3871 * thread currently waiting to acquire the dispatcher.
3873 * @param connection the connection.
3876 _dbus_connection_release_dispatch (DBusConnection *connection)
3878 HAVE_LOCK_CHECK (connection);
3880 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3881 _dbus_mutex_lock (connection->dispatch_mutex);
3883 _dbus_assert (connection->dispatch_acquired);
3885 connection->dispatch_acquired = FALSE;
3886 _dbus_condvar_wake_one (connection->dispatch_cond);
3888 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3889 _dbus_mutex_unlock (connection->dispatch_mutex);
3893 _dbus_connection_failed_pop (DBusConnection *connection,
3894 DBusList *message_link)
3896 _dbus_list_prepend_link (&connection->incoming_messages,
3898 connection->n_incoming += 1;
3901 /* Note this may be called multiple times since we don't track whether we already did it */
3903 notify_disconnected_unlocked (DBusConnection *connection)
3905 HAVE_LOCK_CHECK (connection);
3907 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
3908 * connection from dbus_bus_get(). We make the same guarantee for
3909 * dbus_connection_open() but in a different way since we don't want to
3910 * unref right here; we instead check for connectedness before returning
3911 * the connection from the hash.
3913 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
3915 /* Dump the outgoing queue, we aren't going to be able to
3916 * send it now, and we'd like accessors like
3917 * dbus_connection_get_outgoing_size() to be accurate.
3919 if (connection->n_outgoing > 0)
3923 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3924 connection->n_outgoing);
3926 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3928 _dbus_connection_message_sent (connection, link->data);
3933 /* Note this may be called multiple times since we don't track whether we already did it */
3934 static DBusDispatchStatus
3935 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
3937 HAVE_LOCK_CHECK (connection);
3939 if (connection->disconnect_message_link != NULL)
3941 _dbus_verbose ("Sending disconnect message from %s\n",
3942 _DBUS_FUNCTION_NAME);
3944 /* If we have pending calls, queue their timeouts - we want the Disconnected
3945 * to be the last message, after these timeouts.
3947 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
3949 /* We haven't sent the disconnect message already,
3950 * and all real messages have been queued up.
3952 _dbus_connection_queue_synthesized_message_link (connection,
3953 connection->disconnect_message_link);
3954 connection->disconnect_message_link = NULL;
3956 return DBUS_DISPATCH_DATA_REMAINS;
3959 return DBUS_DISPATCH_COMPLETE;
3962 static DBusDispatchStatus
3963 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3965 HAVE_LOCK_CHECK (connection);
3967 if (connection->n_incoming > 0)
3968 return DBUS_DISPATCH_DATA_REMAINS;
3969 else if (!_dbus_transport_queue_messages (connection->transport))
3970 return DBUS_DISPATCH_NEED_MEMORY;
3973 DBusDispatchStatus status;
3974 dbus_bool_t is_connected;
3976 status = _dbus_transport_get_dispatch_status (connection->transport);
3977 is_connected = _dbus_transport_get_is_connected (connection->transport);
3979 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3980 DISPATCH_STATUS_NAME (status), is_connected);
3984 /* It's possible this would be better done by having an explicit
3985 * notification from _dbus_transport_disconnect() that would
3986 * synchronously do this, instead of waiting for the next dispatch
3987 * status check. However, probably not good to change until it causes
3990 notify_disconnected_unlocked (connection);
3992 /* I'm not sure this is needed; the idea is that we want to
3993 * queue the Disconnected only after we've read all the
3994 * messages, but if we're disconnected maybe we are guaranteed
3995 * to have read them all ?
3997 if (status == DBUS_DISPATCH_COMPLETE)
3998 status = notify_disconnected_and_dispatch_complete_unlocked (connection);
4001 if (status != DBUS_DISPATCH_COMPLETE)
4003 else if (connection->n_incoming > 0)
4004 return DBUS_DISPATCH_DATA_REMAINS;
4006 return DBUS_DISPATCH_COMPLETE;
4011 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
4012 DBusDispatchStatus new_status)
4014 dbus_bool_t changed;
4015 DBusDispatchStatusFunction function;
4018 HAVE_LOCK_CHECK (connection);
4020 _dbus_connection_ref_unlocked (connection);
4022 changed = new_status != connection->last_dispatch_status;
4024 connection->last_dispatch_status = new_status;
4026 function = connection->dispatch_status_function;
4027 data = connection->dispatch_status_data;
4029 if (connection->disconnected_message_arrived &&
4030 !connection->disconnected_message_processed)
4032 connection->disconnected_message_processed = TRUE;
4034 /* this does an unref, but we have a ref
4035 * so we should not run the finalizer here
4038 connection_forget_shared_unlocked (connection);
4040 if (connection->exit_on_disconnect)
4042 CONNECTION_UNLOCK (connection);
4044 _dbus_verbose ("Exiting on Disconnected signal\n");
4046 _dbus_assert_not_reached ("Call to exit() returned");
4050 /* We drop the lock */
4051 CONNECTION_UNLOCK (connection);
4053 if (changed && function)
4055 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
4056 connection, new_status,
4057 DISPATCH_STATUS_NAME (new_status));
4058 (* function) (connection, new_status, data);
4061 dbus_connection_unref (connection);
4065 * Gets the current state of the incoming message queue.
4066 * #DBUS_DISPATCH_DATA_REMAINS indicates that the message queue
4067 * may contain messages. #DBUS_DISPATCH_COMPLETE indicates that the
4068 * incoming queue is empty. #DBUS_DISPATCH_NEED_MEMORY indicates that
4069 * there could be data, but we can't know for sure without more
4072 * To process the incoming message queue, use dbus_connection_dispatch()
4073 * or (in rare cases) dbus_connection_pop_message().
4075 * Note, #DBUS_DISPATCH_DATA_REMAINS really means that either we
4076 * have messages in the queue, or we have raw bytes buffered up
4077 * that need to be parsed. When these bytes are parsed, they
4078 * may not add up to an entire message. Thus, it's possible
4079 * to see a status of #DBUS_DISPATCH_DATA_REMAINS but not
4080 * have a message yet.
4082 * In particular this happens on initial connection, because all sorts
4083 * of authentication protocol stuff has to be parsed before the
4084 * first message arrives.
4086 * @param connection the connection.
4087 * @returns current dispatch status
4090 dbus_connection_get_dispatch_status (DBusConnection *connection)
4092 DBusDispatchStatus status;
4094 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4096 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
4098 CONNECTION_LOCK (connection);
4100 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4102 CONNECTION_UNLOCK (connection);
4108 * Filter funtion for handling the Peer standard interface.
4110 static DBusHandlerResult
4111 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
4112 DBusMessage *message)
4114 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
4116 /* This means we're letting the bus route this message */
4117 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4119 else if (dbus_message_is_method_call (message,
4120 DBUS_INTERFACE_PEER,
4126 ret = dbus_message_new_method_return (message);
4128 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4130 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4132 dbus_message_unref (ret);
4135 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4137 return DBUS_HANDLER_RESULT_HANDLED;
4139 else if (dbus_message_is_method_call (message,
4140 DBUS_INTERFACE_PEER,
4147 ret = dbus_message_new_method_return (message);
4149 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4152 _dbus_string_init (&uuid);
4153 if (_dbus_get_local_machine_uuid_encoded (&uuid))
4155 const char *v_STRING = _dbus_string_get_const_data (&uuid);
4156 if (dbus_message_append_args (ret,
4157 DBUS_TYPE_STRING, &v_STRING,
4160 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4163 _dbus_string_free (&uuid);
4165 dbus_message_unref (ret);
4168 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4170 return DBUS_HANDLER_RESULT_HANDLED;
4172 else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
4174 /* We need to bounce anything else with this interface, otherwise apps
4175 * could start extending the interface and when we added extensions
4176 * here to DBusConnection we'd break those apps.
4182 ret = dbus_message_new_error (message,
4183 DBUS_ERROR_UNKNOWN_METHOD,
4184 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
4186 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4188 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4190 dbus_message_unref (ret);
4193 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4195 return DBUS_HANDLER_RESULT_HANDLED;
4199 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4204 * Processes all builtin filter functions
4206 * If the spec specifies a standard interface
4207 * they should be processed from this method
4209 static DBusHandlerResult
4210 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
4211 DBusMessage *message)
4213 /* We just run one filter for now but have the option to run more
4214 if the spec calls for it in the future */
4216 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
4220 * Processes any incoming data.
4222 * If there's incoming raw data that has not yet been parsed, it is
4223 * parsed, which may or may not result in adding messages to the
4226 * The incoming data buffer is filled when the connection reads from
4227 * its underlying transport (such as a socket). Reading usually
4228 * happens in dbus_watch_handle() or dbus_connection_read_write().
4230 * If there are complete messages in the incoming queue,
4231 * dbus_connection_dispatch() removes one message from the queue and
4232 * processes it. Processing has three steps.
4234 * First, any method replies are passed to #DBusPendingCall or
4235 * dbus_connection_send_with_reply_and_block() in order to
4236 * complete the pending method call.
4238 * Second, any filters registered with dbus_connection_add_filter()
4239 * are run. If any filter returns #DBUS_HANDLER_RESULT_HANDLED
4240 * then processing stops after that filter.
4242 * Third, if the message is a method call it is forwarded to
4243 * any registered object path handlers added with
4244 * dbus_connection_register_object_path() or
4245 * dbus_connection_register_fallback().
4247 * A single call to dbus_connection_dispatch() will process at most
4248 * one message; it will not clear the entire message queue.
4250 * Be careful about calling dbus_connection_dispatch() from inside a
4251 * message handler, i.e. calling dbus_connection_dispatch()
4252 * recursively. If threads have been initialized with a recursive
4253 * mutex function, then this will not deadlock; however, it can
4254 * certainly confuse your application.
4256 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
4258 * @param connection the connection
4259 * @returns dispatch status, see dbus_connection_get_dispatch_status()
4262 dbus_connection_dispatch (DBusConnection *connection)
4264 DBusMessage *message;
4265 DBusList *link, *filter_list_copy, *message_link;
4266 DBusHandlerResult result;
4267 DBusPendingCall *pending;
4268 dbus_int32_t reply_serial;
4269 DBusDispatchStatus status;
4271 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4273 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
4275 CONNECTION_LOCK (connection);
4276 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4277 if (status != DBUS_DISPATCH_DATA_REMAINS)
4279 /* unlocks and calls out to user code */
4280 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4284 /* We need to ref the connection since the callback could potentially
4285 * drop the last ref to it
4287 _dbus_connection_ref_unlocked (connection);
4289 _dbus_connection_acquire_dispatch (connection);
4290 HAVE_LOCK_CHECK (connection);
4292 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4293 if (message_link == NULL)
4295 /* another thread dispatched our stuff */
4297 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
4299 _dbus_connection_release_dispatch (connection);
4301 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4303 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4305 dbus_connection_unref (connection);
4310 message = message_link->data;
4312 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
4314 dbus_message_get_type (message),
4315 dbus_message_get_interface (message) ?
4316 dbus_message_get_interface (message) :
4318 dbus_message_get_member (message) ?
4319 dbus_message_get_member (message) :
4321 dbus_message_get_signature (message));
4323 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4325 /* Pending call handling must be first, because if you do
4326 * dbus_connection_send_with_reply_and_block() or
4327 * dbus_pending_call_block() then no handlers/filters will be run on
4328 * the reply. We want consistent semantics in the case where we
4329 * dbus_connection_dispatch() the reply.
4332 reply_serial = dbus_message_get_reply_serial (message);
4333 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4337 _dbus_verbose ("Dispatching a pending reply\n");
4338 complete_pending_call_and_unlock (connection, pending, message);
4339 pending = NULL; /* it's probably unref'd */
4341 CONNECTION_LOCK (connection);
4342 _dbus_verbose ("pending call completed in dispatch\n");
4343 result = DBUS_HANDLER_RESULT_HANDLED;
4347 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4348 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4351 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4353 _dbus_connection_release_dispatch (connection);
4354 HAVE_LOCK_CHECK (connection);
4356 _dbus_connection_failed_pop (connection, message_link);
4358 /* unlocks and calls user code */
4359 _dbus_connection_update_dispatch_status_and_unlock (connection,
4360 DBUS_DISPATCH_NEED_MEMORY);
4363 dbus_pending_call_unref (pending);
4364 dbus_connection_unref (connection);
4366 return DBUS_DISPATCH_NEED_MEMORY;
4369 _dbus_list_foreach (&filter_list_copy,
4370 (DBusForeachFunction)_dbus_message_filter_ref,
4373 /* We're still protected from dispatch() reentrancy here
4374 * since we acquired the dispatcher
4376 CONNECTION_UNLOCK (connection);
4378 link = _dbus_list_get_first_link (&filter_list_copy);
4379 while (link != NULL)
4381 DBusMessageFilter *filter = link->data;
4382 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
4384 if (filter->function == NULL)
4386 _dbus_verbose (" filter was removed in a callback function\n");
4391 _dbus_verbose (" running filter on message %p\n", message);
4392 result = (* filter->function) (connection, message, filter->user_data);
4394 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4400 _dbus_list_foreach (&filter_list_copy,
4401 (DBusForeachFunction)_dbus_message_filter_unref,
4403 _dbus_list_clear (&filter_list_copy);
4405 CONNECTION_LOCK (connection);
4407 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4409 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
4412 else if (result == DBUS_HANDLER_RESULT_HANDLED)
4414 _dbus_verbose ("filter handled message in dispatch\n");
4418 /* We're still protected from dispatch() reentrancy here
4419 * since we acquired the dispatcher
4421 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
4423 dbus_message_get_type (message),
4424 dbus_message_get_interface (message) ?
4425 dbus_message_get_interface (message) :
4427 dbus_message_get_member (message) ?
4428 dbus_message_get_member (message) :
4430 dbus_message_get_signature (message));
4432 HAVE_LOCK_CHECK (connection);
4433 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4436 CONNECTION_LOCK (connection);
4438 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4440 _dbus_verbose ("object tree handled message in dispatch\n");
4444 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
4448 DBusPreallocatedSend *preallocated;
4450 _dbus_verbose (" sending error %s\n",
4451 DBUS_ERROR_UNKNOWN_METHOD);
4453 if (!_dbus_string_init (&str))
4455 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4456 _dbus_verbose ("no memory for error string in dispatch\n");
4460 if (!_dbus_string_append_printf (&str,
4461 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
4462 dbus_message_get_member (message),
4463 dbus_message_get_signature (message),
4464 dbus_message_get_interface (message)))
4466 _dbus_string_free (&str);
4467 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4468 _dbus_verbose ("no memory for error string in dispatch\n");
4472 reply = dbus_message_new_error (message,
4473 DBUS_ERROR_UNKNOWN_METHOD,
4474 _dbus_string_get_const_data (&str));
4475 _dbus_string_free (&str);
4479 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4480 _dbus_verbose ("no memory for error reply in dispatch\n");
4484 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4486 if (preallocated == NULL)
4488 dbus_message_unref (reply);
4489 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4490 _dbus_verbose ("no memory for error send in dispatch\n");
4494 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4497 dbus_message_unref (reply);
4499 result = DBUS_HANDLER_RESULT_HANDLED;
4502 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
4503 dbus_message_get_type (message),
4504 dbus_message_get_interface (message) ?
4505 dbus_message_get_interface (message) :
4507 dbus_message_get_member (message) ?
4508 dbus_message_get_member (message) :
4510 dbus_message_get_signature (message),
4514 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4516 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
4518 /* Put message back, and we'll start over.
4519 * Yes this means handlers must be idempotent if they
4520 * don't return HANDLED; c'est la vie.
4522 _dbus_connection_putback_message_link_unlocked (connection,
4527 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
4529 _dbus_list_free_link (message_link);
4530 dbus_message_unref (message); /* don't want the message to count in max message limits
4531 * in computing dispatch status below
4535 _dbus_connection_release_dispatch (connection);
4536 HAVE_LOCK_CHECK (connection);
4538 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
4539 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4541 /* unlocks and calls user code */
4542 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4544 dbus_connection_unref (connection);
4550 * Sets the watch functions for the connection. These functions are
4551 * responsible for making the application's main loop aware of file
4552 * descriptors that need to be monitored for events, using select() or
4553 * poll(). When using Qt, typically the DBusAddWatchFunction would
4554 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
4555 * could call g_io_add_watch(), or could be used as part of a more
4556 * elaborate GSource. Note that when a watch is added, it may
4559 * The DBusWatchToggledFunction notifies the application that the
4560 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
4561 * to check this. A disabled watch should have no effect, and enabled
4562 * watch should be added to the main loop. This feature is used
4563 * instead of simply adding/removing the watch because
4564 * enabling/disabling can be done without memory allocation. The
4565 * toggled function may be NULL if a main loop re-queries
4566 * dbus_watch_get_enabled() every time anyway.
4568 * The DBusWatch can be queried for the file descriptor to watch using
4569 * dbus_watch_get_unix_fd() or dbus_watch_get_socket(), and for the
4570 * events to watch for using dbus_watch_get_flags(). The flags
4571 * returned by dbus_watch_get_flags() will only contain
4572 * DBUS_WATCH_READABLE and DBUS_WATCH_WRITABLE, never
4573 * DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; all watches implicitly
4574 * include a watch for hangups, errors, and other exceptional
4577 * Once a file descriptor becomes readable or writable, or an exception
4578 * occurs, dbus_watch_handle() should be called to
4579 * notify the connection of the file descriptor's condition.
4581 * dbus_watch_handle() cannot be called during the
4582 * DBusAddWatchFunction, as the connection will not be ready to handle
4585 * It is not allowed to reference a DBusWatch after it has been passed
4586 * to remove_function.
4588 * If #FALSE is returned due to lack of memory, the failure may be due
4589 * to a #FALSE return from the new add_function. If so, the
4590 * add_function may have been called successfully one or more times,
4591 * but the remove_function will also have been called to remove any
4592 * successful adds. i.e. if #FALSE is returned the net result
4593 * should be that dbus_connection_set_watch_functions() has no effect,
4594 * but the add_function and remove_function may have been called.
4596 * @todo We need to drop the lock when we call the
4597 * add/remove/toggled functions which can be a side effect
4598 * of setting the watch functions.
4600 * @param connection the connection.
4601 * @param add_function function to begin monitoring a new descriptor.
4602 * @param remove_function function to stop monitoring a descriptor.
4603 * @param toggled_function function to notify of enable/disable
4604 * @param data data to pass to add_function and remove_function.
4605 * @param free_data_function function to be called to free the data.
4606 * @returns #FALSE on failure (no memory)
4609 dbus_connection_set_watch_functions (DBusConnection *connection,
4610 DBusAddWatchFunction add_function,
4611 DBusRemoveWatchFunction remove_function,
4612 DBusWatchToggledFunction toggled_function,
4614 DBusFreeFunction free_data_function)
4617 DBusWatchList *watches;
4619 _dbus_return_val_if_fail (connection != NULL, FALSE);
4621 CONNECTION_LOCK (connection);
4623 #ifndef DBUS_DISABLE_CHECKS
4624 if (connection->watches == NULL)
4626 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4627 _DBUS_FUNCTION_NAME);
4632 /* ref connection for slightly better reentrancy */
4633 _dbus_connection_ref_unlocked (connection);
4635 /* This can call back into user code, and we need to drop the
4636 * connection lock when it does. This is kind of a lame
4639 watches = connection->watches;
4640 connection->watches = NULL;
4641 CONNECTION_UNLOCK (connection);
4643 retval = _dbus_watch_list_set_functions (watches,
4644 add_function, remove_function,
4646 data, free_data_function);
4647 CONNECTION_LOCK (connection);
4648 connection->watches = watches;
4650 CONNECTION_UNLOCK (connection);
4651 /* drop our paranoid refcount */
4652 dbus_connection_unref (connection);
4658 * Sets the timeout functions for the connection. These functions are
4659 * responsible for making the application's main loop aware of timeouts.
4660 * When using Qt, typically the DBusAddTimeoutFunction would create a
4661 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
4664 * The DBusTimeoutToggledFunction notifies the application that the
4665 * timeout has been enabled or disabled. Call
4666 * dbus_timeout_get_enabled() to check this. A disabled timeout should
4667 * have no effect, and enabled timeout should be added to the main
4668 * loop. This feature is used instead of simply adding/removing the
4669 * timeout because enabling/disabling can be done without memory
4670 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
4671 * to enable and disable. The toggled function may be NULL if a main
4672 * loop re-queries dbus_timeout_get_enabled() every time anyway.
4673 * Whenever a timeout is toggled, its interval may change.
4675 * The DBusTimeout can be queried for the timer interval using
4676 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
4677 * repeatedly, each time the interval elapses, starting after it has
4678 * elapsed once. The timeout stops firing when it is removed with the
4679 * given remove_function. The timer interval may change whenever the
4680 * timeout is added, removed, or toggled.
4682 * @param connection the connection.
4683 * @param add_function function to add a timeout.
4684 * @param remove_function function to remove a timeout.
4685 * @param toggled_function function to notify of enable/disable
4686 * @param data data to pass to add_function and remove_function.
4687 * @param free_data_function function to be called to free the data.
4688 * @returns #FALSE on failure (no memory)
4691 dbus_connection_set_timeout_functions (DBusConnection *connection,
4692 DBusAddTimeoutFunction add_function,
4693 DBusRemoveTimeoutFunction remove_function,
4694 DBusTimeoutToggledFunction toggled_function,
4696 DBusFreeFunction free_data_function)
4699 DBusTimeoutList *timeouts;
4701 _dbus_return_val_if_fail (connection != NULL, FALSE);
4703 CONNECTION_LOCK (connection);
4705 #ifndef DBUS_DISABLE_CHECKS
4706 if (connection->timeouts == NULL)
4708 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4709 _DBUS_FUNCTION_NAME);
4714 /* ref connection for slightly better reentrancy */
4715 _dbus_connection_ref_unlocked (connection);
4717 timeouts = connection->timeouts;
4718 connection->timeouts = NULL;
4719 CONNECTION_UNLOCK (connection);
4721 retval = _dbus_timeout_list_set_functions (timeouts,
4722 add_function, remove_function,
4724 data, free_data_function);
4725 CONNECTION_LOCK (connection);
4726 connection->timeouts = timeouts;
4728 CONNECTION_UNLOCK (connection);
4729 /* drop our paranoid refcount */
4730 dbus_connection_unref (connection);
4736 * Sets the mainloop wakeup function for the connection. This function
4737 * is responsible for waking up the main loop (if its sleeping in
4738 * another thread) when some some change has happened to the
4739 * connection that the mainloop needs to reconsider (e.g. a message
4740 * has been queued for writing). When using Qt, this typically
4741 * results in a call to QEventLoop::wakeUp(). When using GLib, it
4742 * would call g_main_context_wakeup().
4744 * @param connection the connection.
4745 * @param wakeup_main_function function to wake up the mainloop
4746 * @param data data to pass wakeup_main_function
4747 * @param free_data_function function to be called to free the data.
4750 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
4751 DBusWakeupMainFunction wakeup_main_function,
4753 DBusFreeFunction free_data_function)
4756 DBusFreeFunction old_free_data;
4758 _dbus_return_if_fail (connection != NULL);
4760 CONNECTION_LOCK (connection);
4761 old_data = connection->wakeup_main_data;
4762 old_free_data = connection->free_wakeup_main_data;
4764 connection->wakeup_main_function = wakeup_main_function;
4765 connection->wakeup_main_data = data;
4766 connection->free_wakeup_main_data = free_data_function;
4768 CONNECTION_UNLOCK (connection);
4770 /* Callback outside the lock */
4772 (*old_free_data) (old_data);
4776 * Set a function to be invoked when the dispatch status changes.
4777 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
4778 * dbus_connection_dispatch() needs to be called to process incoming
4779 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
4780 * from inside the DBusDispatchStatusFunction. Indeed, almost
4781 * any reentrancy in this function is a bad idea. Instead,
4782 * the DBusDispatchStatusFunction should simply save an indication
4783 * that messages should be dispatched later, when the main loop
4786 * If you don't set a dispatch status function, you have to be sure to
4787 * dispatch on every iteration of your main loop, especially if
4788 * dbus_watch_handle() or dbus_timeout_handle() were called.
4790 * @param connection the connection
4791 * @param function function to call on dispatch status changes
4792 * @param data data for function
4793 * @param free_data_function free the function data
4796 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
4797 DBusDispatchStatusFunction function,
4799 DBusFreeFunction free_data_function)
4802 DBusFreeFunction old_free_data;
4804 _dbus_return_if_fail (connection != NULL);
4806 CONNECTION_LOCK (connection);
4807 old_data = connection->dispatch_status_data;
4808 old_free_data = connection->free_dispatch_status_data;
4810 connection->dispatch_status_function = function;
4811 connection->dispatch_status_data = data;
4812 connection->free_dispatch_status_data = free_data_function;
4814 CONNECTION_UNLOCK (connection);
4816 /* Callback outside the lock */
4818 (*old_free_data) (old_data);
4822 * Get the UNIX file descriptor of the connection, if any. This can
4823 * be used for SELinux access control checks with getpeercon() for
4824 * example. DO NOT read or write to the file descriptor, or try to
4825 * select() on it; use DBusWatch for main loop integration. Not all
4826 * connections will have a file descriptor. So for adding descriptors
4827 * to the main loop, use dbus_watch_get_unix_fd() and so forth.
4829 * If the connection is socket-based, you can also use
4830 * dbus_connection_get_socket(), which will work on Windows too.
4831 * This function always fails on Windows.
4833 * Right now the returned descriptor is always a socket, but
4834 * that is not guaranteed.
4836 * @param connection the connection
4837 * @param fd return location for the file descriptor.
4838 * @returns #TRUE if fd is successfully obtained.
4841 dbus_connection_get_unix_fd (DBusConnection *connection,
4844 _dbus_return_val_if_fail (connection != NULL, FALSE);
4845 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4848 /* FIXME do this on a lower level */
4852 return dbus_connection_get_socket(connection, fd);
4856 * Gets the underlying Windows or UNIX socket file descriptor
4857 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
4858 * select() on it; use DBusWatch for main loop integration. Not all
4859 * connections will have a socket. So for adding descriptors
4860 * to the main loop, use dbus_watch_get_socket() and so forth.
4862 * If the connection is not socket-based, this function will return FALSE,
4863 * even if the connection does have a file descriptor of some kind.
4864 * i.e. this function always returns specifically a socket file descriptor.
4866 * @param connection the connection
4867 * @param fd return location for the file descriptor.
4868 * @returns #TRUE if fd is successfully obtained.
4871 dbus_connection_get_socket(DBusConnection *connection,
4876 _dbus_return_val_if_fail (connection != NULL, FALSE);
4877 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4879 CONNECTION_LOCK (connection);
4881 retval = _dbus_transport_get_socket_fd (connection->transport,
4884 CONNECTION_UNLOCK (connection);
4891 * Gets the UNIX user ID of the connection if known. Returns #TRUE if
4892 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
4893 * for now, though in theory someone could hook Windows to NIS or
4894 * something. Always returns #FALSE prior to authenticating the
4897 * The UID is only read by servers from clients; clients can't usually
4898 * get the UID of servers, because servers do not authenticate to
4899 * clients. The returned UID is the UID the connection authenticated
4902 * The message bus is a server and the apps connecting to the bus
4905 * You can ask the bus to tell you the UID of another connection though
4906 * if you like; this is done with dbus_bus_get_unix_user().
4908 * @param connection the connection
4909 * @param uid return location for the user ID
4910 * @returns #TRUE if uid is filled in with a valid user ID
4913 dbus_connection_get_unix_user (DBusConnection *connection,
4918 _dbus_return_val_if_fail (connection != NULL, FALSE);
4919 _dbus_return_val_if_fail (uid != NULL, FALSE);
4921 CONNECTION_LOCK (connection);
4923 if (!_dbus_transport_get_is_authenticated (connection->transport))
4926 result = _dbus_transport_get_unix_user (connection->transport,
4930 _dbus_assert (!result);
4933 CONNECTION_UNLOCK (connection);
4939 * Gets the process ID of the connection if any.
4940 * Returns #TRUE if the pid is filled in.
4941 * Always returns #FALSE prior to authenticating the
4944 * @param connection the connection
4945 * @param pid return location for the process ID
4946 * @returns #TRUE if uid is filled in with a valid process ID
4949 dbus_connection_get_unix_process_id (DBusConnection *connection,
4954 _dbus_return_val_if_fail (connection != NULL, FALSE);
4955 _dbus_return_val_if_fail (pid != NULL, FALSE);
4957 CONNECTION_LOCK (connection);
4959 if (!_dbus_transport_get_is_authenticated (connection->transport))
4962 result = _dbus_transport_get_unix_process_id (connection->transport,
4965 _dbus_assert (!result);
4968 CONNECTION_UNLOCK (connection);
4974 * Sets a predicate function used to determine whether a given user ID
4975 * is allowed to connect. When an incoming connection has
4976 * authenticated with a particular user ID, this function is called;
4977 * if it returns #TRUE, the connection is allowed to proceed,
4978 * otherwise the connection is disconnected.
4980 * If the function is set to #NULL (as it is by default), then
4981 * only the same UID as the server process will be allowed to
4982 * connect. Also, root is always allowed to connect.
4984 * On Windows, the function will be set and its free_data_function will
4985 * be invoked when the connection is freed or a new function is set.
4986 * However, the function will never be called, because there are
4987 * no UNIX user ids to pass to it, or at least none of the existing
4988 * auth protocols would allow authenticating as a UNIX user on Windows.
4990 * @param connection the connection
4991 * @param function the predicate
4992 * @param data data to pass to the predicate
4993 * @param free_data_function function to free the data
4996 dbus_connection_set_unix_user_function (DBusConnection *connection,
4997 DBusAllowUnixUserFunction function,
4999 DBusFreeFunction free_data_function)
5001 void *old_data = NULL;
5002 DBusFreeFunction old_free_function = NULL;
5004 _dbus_return_if_fail (connection != NULL);
5006 CONNECTION_LOCK (connection);
5007 _dbus_transport_set_unix_user_function (connection->transport,
5008 function, data, free_data_function,
5009 &old_data, &old_free_function);
5010 CONNECTION_UNLOCK (connection);
5012 if (old_free_function != NULL)
5013 (* old_free_function) (old_data);
5017 * Gets the Windows user SID of the connection if known. Returns
5018 * #TRUE if the ID is filled in. Always returns #FALSE on non-Windows
5019 * platforms for now, though in theory someone could hook UNIX to
5020 * Active Directory or something. Always returns #FALSE prior to
5021 * authenticating the connection.
5023 * The user is only read by servers from clients; clients can't usually
5024 * get the user of servers, because servers do not authenticate to
5025 * clients. The returned user is the user the connection authenticated
5028 * The message bus is a server and the apps connecting to the bus
5031 * The returned user string has to be freed with dbus_free().
5033 * The return value indicates whether the user SID is available;
5034 * if it's available but we don't have the memory to copy it,
5035 * then the return value is #TRUE and #NULL is given as the SID.
5037 * @todo We would like to be able to say "You can ask the bus to tell
5038 * you the user of another connection though if you like; this is done
5039 * with dbus_bus_get_windows_user()." But this has to be implemented
5040 * in bus/driver.c and dbus/dbus-bus.c, and is pointless anyway
5041 * since on Windows we only use the session bus for now.
5043 * @param connection the connection
5044 * @param windows_sid_p return location for an allocated copy of the user ID, or #NULL if no memory
5045 * @returns #TRUE if user is available (returned value may be #NULL anyway if no memory)
5048 dbus_connection_get_windows_user (DBusConnection *connection,
5049 char **windows_sid_p)
5053 _dbus_return_val_if_fail (connection != NULL, FALSE);
5054 _dbus_return_val_if_fail (windows_sid_p != NULL, FALSE);
5056 CONNECTION_LOCK (connection);
5058 if (!_dbus_transport_get_is_authenticated (connection->transport))
5061 result = _dbus_transport_get_windows_user (connection->transport,
5065 _dbus_assert (!result);
5068 CONNECTION_UNLOCK (connection);
5074 * Sets a predicate function used to determine whether a given user ID
5075 * is allowed to connect. When an incoming connection has
5076 * authenticated with a particular user ID, this function is called;
5077 * if it returns #TRUE, the connection is allowed to proceed,
5078 * otherwise the connection is disconnected.
5080 * If the function is set to #NULL (as it is by default), then
5081 * only the same user owning the server process will be allowed to
5084 * On UNIX, the function will be set and its free_data_function will
5085 * be invoked when the connection is freed or a new function is set.
5086 * However, the function will never be called, because there is no
5087 * way right now to authenticate as a Windows user on UNIX.
5089 * @param connection the connection
5090 * @param function the predicate
5091 * @param data data to pass to the predicate
5092 * @param free_data_function function to free the data
5095 dbus_connection_set_windows_user_function (DBusConnection *connection,
5096 DBusAllowWindowsUserFunction function,
5098 DBusFreeFunction free_data_function)
5100 void *old_data = NULL;
5101 DBusFreeFunction old_free_function = NULL;
5103 _dbus_return_if_fail (connection != NULL);
5105 CONNECTION_LOCK (connection);
5106 _dbus_transport_set_windows_user_function (connection->transport,
5107 function, data, free_data_function,
5108 &old_data, &old_free_function);
5109 CONNECTION_UNLOCK (connection);
5111 if (old_free_function != NULL)
5112 (* old_free_function) (old_data);
5116 * This function must be called on the server side of a connection when the
5117 * connection is first seen in the #DBusNewConnectionFunction. If set to
5118 * #TRUE (the default is #FALSE), then the connection can proceed even if
5119 * the client does not authenticate as some user identity, i.e. clients
5120 * can connect anonymously.
5122 * This setting interacts with the available authorization mechanisms
5123 * (see dbus_server_set_auth_mechanisms()). Namely, an auth mechanism
5124 * such as ANONYMOUS that supports anonymous auth must be included in
5125 * the list of available mechanisms for anonymous login to work.
5127 * This setting also changes the default rule for connections
5128 * authorized as a user; normally, if a connection authorizes as
5129 * a user identity, it is permitted if the user identity is
5130 * root or the user identity matches the user identity of the server
5131 * process. If anonymous connections are allowed, however,
5132 * then any user identity is allowed.
5134 * You can override the rules for connections authorized as a
5135 * user identity with dbus_connection_set_unix_user_function()
5136 * and dbus_connection_set_windows_user_function().
5138 * @param connection the connection
5139 * @param value whether to allow authentication as an anonymous user
5142 dbus_connection_set_allow_anonymous (DBusConnection *connection,
5145 _dbus_return_if_fail (connection != NULL);
5147 CONNECTION_LOCK (connection);
5148 _dbus_transport_set_allow_anonymous (connection->transport, value);
5149 CONNECTION_UNLOCK (connection);
5154 * Normally #DBusConnection automatically handles all messages to the
5155 * org.freedesktop.DBus.Peer interface. However, the message bus wants
5156 * to be able to route methods on that interface through the bus and
5157 * to other applications. If routing peer messages is enabled, then
5158 * messages with the org.freedesktop.DBus.Peer interface that also
5159 * have a bus destination name set will not be automatically
5160 * handled by the #DBusConnection and instead will be dispatched
5161 * normally to the application.
5163 * If a normal application sets this flag, it can break things badly.
5164 * So don't set this unless you are the message bus.
5166 * @param connection the connection
5167 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
5170 dbus_connection_set_route_peer_messages (DBusConnection *connection,
5173 _dbus_return_if_fail (connection != NULL);
5175 CONNECTION_LOCK (connection);
5176 connection->route_peer_messages = TRUE;
5177 CONNECTION_UNLOCK (connection);
5181 * Adds a message filter. Filters are handlers that are run on all
5182 * incoming messages, prior to the objects registered with
5183 * dbus_connection_register_object_path(). Filters are run in the
5184 * order that they were added. The same handler can be added as a
5185 * filter more than once, in which case it will be run more than once.
5186 * Filters added during a filter callback won't be run on the message
5189 * @todo we don't run filters on messages while blocking without
5190 * entering the main loop, since filters are run as part of
5191 * dbus_connection_dispatch(). This is probably a feature, as filters
5192 * could create arbitrary reentrancy. But kind of sucks if you're
5193 * trying to filter METHOD_RETURN for some reason.
5195 * @param connection the connection
5196 * @param function function to handle messages
5197 * @param user_data user data to pass to the function
5198 * @param free_data_function function to use for freeing user data
5199 * @returns #TRUE on success, #FALSE if not enough memory.
5202 dbus_connection_add_filter (DBusConnection *connection,
5203 DBusHandleMessageFunction function,
5205 DBusFreeFunction free_data_function)
5207 DBusMessageFilter *filter;
5209 _dbus_return_val_if_fail (connection != NULL, FALSE);
5210 _dbus_return_val_if_fail (function != NULL, FALSE);
5212 filter = dbus_new0 (DBusMessageFilter, 1);
5216 filter->refcount.value = 1;
5218 CONNECTION_LOCK (connection);
5220 if (!_dbus_list_append (&connection->filter_list,
5223 _dbus_message_filter_unref (filter);
5224 CONNECTION_UNLOCK (connection);
5228 /* Fill in filter after all memory allocated,
5229 * so we don't run the free_user_data_function
5230 * if the add_filter() fails
5233 filter->function = function;
5234 filter->user_data = user_data;
5235 filter->free_user_data_function = free_data_function;
5237 CONNECTION_UNLOCK (connection);
5242 * Removes a previously-added message filter. It is a programming
5243 * error to call this function for a handler that has not been added
5244 * as a filter. If the given handler was added more than once, only
5245 * one instance of it will be removed (the most recently-added
5248 * @param connection the connection
5249 * @param function the handler to remove
5250 * @param user_data user data for the handler to remove
5254 dbus_connection_remove_filter (DBusConnection *connection,
5255 DBusHandleMessageFunction function,
5259 DBusMessageFilter *filter;
5261 _dbus_return_if_fail (connection != NULL);
5262 _dbus_return_if_fail (function != NULL);
5264 CONNECTION_LOCK (connection);
5268 link = _dbus_list_get_last_link (&connection->filter_list);
5269 while (link != NULL)
5271 filter = link->data;
5273 if (filter->function == function &&
5274 filter->user_data == user_data)
5276 _dbus_list_remove_link (&connection->filter_list, link);
5277 filter->function = NULL;
5282 link = _dbus_list_get_prev_link (&connection->filter_list, link);
5285 CONNECTION_UNLOCK (connection);
5287 #ifndef DBUS_DISABLE_CHECKS
5290 _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
5291 function, user_data);
5296 /* Call application code */
5297 if (filter->free_user_data_function)
5298 (* filter->free_user_data_function) (filter->user_data);
5300 filter->free_user_data_function = NULL;
5301 filter->user_data = NULL;
5303 _dbus_message_filter_unref (filter);
5307 * Registers a handler for a given path in the object hierarchy.
5308 * The given vtable handles messages sent to exactly the given path.
5310 * @param connection the connection
5311 * @param path a '/' delimited string of path elements
5312 * @param vtable the virtual table
5313 * @param user_data data to pass to functions in the vtable
5314 * @param error address where an error can be returned
5315 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5316 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
5319 dbus_connection_try_register_object_path (DBusConnection *connection,
5321 const DBusObjectPathVTable *vtable,
5325 char **decomposed_path;
5328 _dbus_return_val_if_fail (connection != NULL, FALSE);
5329 _dbus_return_val_if_fail (path != NULL, FALSE);
5330 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5331 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5333 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5336 CONNECTION_LOCK (connection);
5338 retval = _dbus_object_tree_register (connection->objects,
5340 (const char **) decomposed_path, vtable,
5343 CONNECTION_UNLOCK (connection);
5345 dbus_free_string_array (decomposed_path);
5351 * Registers a handler for a given path in the object hierarchy.
5352 * The given vtable handles messages sent to exactly the given path.
5354 * It is a bug to call this function for object paths which already
5355 * have a handler. Use dbus_connection_try_register_object_path() if this
5356 * might be the case.
5358 * @param connection the connection
5359 * @param path a '/' delimited string of path elements
5360 * @param vtable the virtual table
5361 * @param user_data data to pass to functions in the vtable
5362 * @returns #FALSE if not enough memory
5365 dbus_connection_register_object_path (DBusConnection *connection,
5367 const DBusObjectPathVTable *vtable,
5370 char **decomposed_path;
5372 DBusError error = DBUS_ERROR_INIT;
5374 _dbus_return_val_if_fail (connection != NULL, FALSE);
5375 _dbus_return_val_if_fail (path != NULL, FALSE);
5376 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5377 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5379 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5382 CONNECTION_LOCK (connection);
5384 retval = _dbus_object_tree_register (connection->objects,
5386 (const char **) decomposed_path, vtable,
5389 CONNECTION_UNLOCK (connection);
5391 dbus_free_string_array (decomposed_path);
5393 if (dbus_error_has_name (&error, DBUS_ERROR_ADDRESS_IN_USE))
5395 _dbus_warn ("%s\n", error.message);
5396 dbus_error_free (&error);
5404 * Registers a fallback handler for a given subsection of the object
5405 * hierarchy. The given vtable handles messages at or below the given
5406 * path. You can use this to establish a default message handling
5407 * policy for a whole "subdirectory."
5409 * @param connection the connection
5410 * @param path a '/' delimited string of path elements
5411 * @param vtable the virtual table
5412 * @param user_data data to pass to functions in the vtable
5413 * @param error address where an error can be returned
5414 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5415 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
5418 dbus_connection_try_register_fallback (DBusConnection *connection,
5420 const DBusObjectPathVTable *vtable,
5424 char **decomposed_path;
5427 _dbus_return_val_if_fail (connection != NULL, FALSE);
5428 _dbus_return_val_if_fail (path != NULL, FALSE);
5429 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5430 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5432 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5435 CONNECTION_LOCK (connection);
5437 retval = _dbus_object_tree_register (connection->objects,
5439 (const char **) decomposed_path, vtable,
5442 CONNECTION_UNLOCK (connection);
5444 dbus_free_string_array (decomposed_path);
5450 * Registers a fallback handler for a given subsection of the object
5451 * hierarchy. The given vtable handles messages at or below the given
5452 * path. You can use this to establish a default message handling
5453 * policy for a whole "subdirectory."
5455 * It is a bug to call this function for object paths which already
5456 * have a handler. Use dbus_connection_try_register_fallback() if this
5457 * might be the case.
5459 * @param connection the connection
5460 * @param path a '/' delimited string of path elements
5461 * @param vtable the virtual table
5462 * @param user_data data to pass to functions in the vtable
5463 * @returns #FALSE if not enough memory
5466 dbus_connection_register_fallback (DBusConnection *connection,
5468 const DBusObjectPathVTable *vtable,
5471 char **decomposed_path;
5473 DBusError error = DBUS_ERROR_INIT;
5475 _dbus_return_val_if_fail (connection != NULL, FALSE);
5476 _dbus_return_val_if_fail (path != NULL, FALSE);
5477 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5478 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5480 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5483 CONNECTION_LOCK (connection);
5485 retval = _dbus_object_tree_register (connection->objects,
5487 (const char **) decomposed_path, vtable,
5490 CONNECTION_UNLOCK (connection);
5492 dbus_free_string_array (decomposed_path);
5494 if (dbus_error_has_name (&error, DBUS_ERROR_ADDRESS_IN_USE))
5496 _dbus_warn ("%s\n", error.message);
5497 dbus_error_free (&error);
5505 * Unregisters the handler registered with exactly the given path.
5506 * It's a bug to call this function for a path that isn't registered.
5507 * Can unregister both fallback paths and object paths.
5509 * @param connection the connection
5510 * @param path a '/' delimited string of path elements
5511 * @returns #FALSE if not enough memory
5514 dbus_connection_unregister_object_path (DBusConnection *connection,
5517 char **decomposed_path;
5519 _dbus_return_val_if_fail (connection != NULL, FALSE);
5520 _dbus_return_val_if_fail (path != NULL, FALSE);
5521 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5523 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5526 CONNECTION_LOCK (connection);
5528 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
5530 dbus_free_string_array (decomposed_path);
5536 * Gets the user data passed to dbus_connection_register_object_path()
5537 * or dbus_connection_register_fallback(). If nothing was registered
5538 * at this path, the data is filled in with #NULL.
5540 * @param connection the connection
5541 * @param path the path you registered with
5542 * @param data_p location to store the user data, or #NULL
5543 * @returns #FALSE if not enough memory
5546 dbus_connection_get_object_path_data (DBusConnection *connection,
5550 char **decomposed_path;
5552 _dbus_return_val_if_fail (connection != NULL, FALSE);
5553 _dbus_return_val_if_fail (path != NULL, FALSE);
5554 _dbus_return_val_if_fail (data_p != NULL, FALSE);
5558 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5561 CONNECTION_LOCK (connection);
5563 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
5565 CONNECTION_UNLOCK (connection);
5567 dbus_free_string_array (decomposed_path);
5573 * Lists the registered fallback handlers and object path handlers at
5574 * the given parent_path. The returned array should be freed with
5575 * dbus_free_string_array().
5577 * @param connection the connection
5578 * @param parent_path the path to list the child handlers of
5579 * @param child_entries returns #NULL-terminated array of children
5580 * @returns #FALSE if no memory to allocate the child entries
5583 dbus_connection_list_registered (DBusConnection *connection,
5584 const char *parent_path,
5585 char ***child_entries)
5587 char **decomposed_path;
5589 _dbus_return_val_if_fail (connection != NULL, FALSE);
5590 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
5591 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
5592 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
5594 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
5597 CONNECTION_LOCK (connection);
5599 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
5600 (const char **) decomposed_path,
5602 dbus_free_string_array (decomposed_path);
5607 static DBusDataSlotAllocator slot_allocator;
5608 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
5611 * Allocates an integer ID to be used for storing application-specific
5612 * data on any DBusConnection. The allocated ID may then be used
5613 * with dbus_connection_set_data() and dbus_connection_get_data().
5614 * The passed-in slot must be initialized to -1, and is filled in
5615 * with the slot ID. If the passed-in slot is not -1, it's assumed
5616 * to be already allocated, and its refcount is incremented.
5618 * The allocated slot is global, i.e. all DBusConnection objects will
5619 * have a slot with the given integer ID reserved.
5621 * @param slot_p address of a global variable storing the slot
5622 * @returns #FALSE on failure (no memory)
5625 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
5627 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5628 &_DBUS_LOCK_NAME (connection_slots),
5633 * Deallocates a global ID for connection data slots.
5634 * dbus_connection_get_data() and dbus_connection_set_data() may no
5635 * longer be used with this slot. Existing data stored on existing
5636 * DBusConnection objects will be freed when the connection is
5637 * finalized, but may not be retrieved (and may only be replaced if
5638 * someone else reallocates the slot). When the refcount on the
5639 * passed-in slot reaches 0, it is set to -1.
5641 * @param slot_p address storing the slot to deallocate
5644 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
5646 _dbus_return_if_fail (*slot_p >= 0);
5648 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5652 * Stores a pointer on a DBusConnection, along
5653 * with an optional function to be used for freeing
5654 * the data when the data is set again, or when
5655 * the connection is finalized. The slot number
5656 * must have been allocated with dbus_connection_allocate_data_slot().
5658 * @param connection the connection
5659 * @param slot the slot number
5660 * @param data the data to store
5661 * @param free_data_func finalizer function for the data
5662 * @returns #TRUE if there was enough memory to store the data
5665 dbus_connection_set_data (DBusConnection *connection,
5668 DBusFreeFunction free_data_func)
5670 DBusFreeFunction old_free_func;
5674 _dbus_return_val_if_fail (connection != NULL, FALSE);
5675 _dbus_return_val_if_fail (slot >= 0, FALSE);
5677 CONNECTION_LOCK (connection);
5679 retval = _dbus_data_slot_list_set (&slot_allocator,
5680 &connection->slot_list,
5681 slot, data, free_data_func,
5682 &old_free_func, &old_data);
5684 CONNECTION_UNLOCK (connection);
5688 /* Do the actual free outside the connection lock */
5690 (* old_free_func) (old_data);
5697 * Retrieves data previously set with dbus_connection_set_data().
5698 * The slot must still be allocated (must not have been freed).
5700 * @param connection the connection
5701 * @param slot the slot to get data from
5702 * @returns the data, or #NULL if not found
5705 dbus_connection_get_data (DBusConnection *connection,
5710 _dbus_return_val_if_fail (connection != NULL, NULL);
5712 CONNECTION_LOCK (connection);
5714 res = _dbus_data_slot_list_get (&slot_allocator,
5715 &connection->slot_list,
5718 CONNECTION_UNLOCK (connection);
5724 * This function sets a global flag for whether dbus_connection_new()
5725 * will set SIGPIPE behavior to SIG_IGN.
5727 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
5730 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
5732 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
5736 * Specifies the maximum size message this connection is allowed to
5737 * receive. Larger messages will result in disconnecting the
5740 * @param connection a #DBusConnection
5741 * @param size maximum message size the connection can receive, in bytes
5744 dbus_connection_set_max_message_size (DBusConnection *connection,
5747 _dbus_return_if_fail (connection != NULL);
5749 CONNECTION_LOCK (connection);
5750 _dbus_transport_set_max_message_size (connection->transport,
5752 CONNECTION_UNLOCK (connection);
5756 * Gets the value set by dbus_connection_set_max_message_size().
5758 * @param connection the connection
5759 * @returns the max size of a single message
5762 dbus_connection_get_max_message_size (DBusConnection *connection)
5766 _dbus_return_val_if_fail (connection != NULL, 0);
5768 CONNECTION_LOCK (connection);
5769 res = _dbus_transport_get_max_message_size (connection->transport);
5770 CONNECTION_UNLOCK (connection);
5775 * Sets the maximum total number of bytes that can be used for all messages
5776 * received on this connection. Messages count toward the maximum until
5777 * they are finalized. When the maximum is reached, the connection will
5778 * not read more data until some messages are finalized.
5780 * The semantics of the maximum are: if outstanding messages are
5781 * already above the maximum, additional messages will not be read.
5782 * The semantics are not: if the next message would cause us to exceed
5783 * the maximum, we don't read it. The reason is that we don't know the
5784 * size of a message until after we read it.
5786 * Thus, the max live messages size can actually be exceeded
5787 * by up to the maximum size of a single message.
5789 * Also, if we read say 1024 bytes off the wire in a single read(),
5790 * and that contains a half-dozen small messages, we may exceed the
5791 * size max by that amount. But this should be inconsequential.
5793 * This does imply that we can't call read() with a buffer larger
5794 * than we're willing to exceed this limit by.
5796 * @param connection the connection
5797 * @param size the maximum size in bytes of all outstanding messages
5800 dbus_connection_set_max_received_size (DBusConnection *connection,
5803 _dbus_return_if_fail (connection != NULL);
5805 CONNECTION_LOCK (connection);
5806 _dbus_transport_set_max_received_size (connection->transport,
5808 CONNECTION_UNLOCK (connection);
5812 * Gets the value set by dbus_connection_set_max_received_size().
5814 * @param connection the connection
5815 * @returns the max size of all live messages
5818 dbus_connection_get_max_received_size (DBusConnection *connection)
5822 _dbus_return_val_if_fail (connection != NULL, 0);
5824 CONNECTION_LOCK (connection);
5825 res = _dbus_transport_get_max_received_size (connection->transport);
5826 CONNECTION_UNLOCK (connection);
5831 * Gets the approximate size in bytes of all messages in the outgoing
5832 * message queue. The size is approximate in that you shouldn't use
5833 * it to decide how many bytes to read off the network or anything
5834 * of that nature, as optimizations may choose to tell small white lies
5835 * to avoid performance overhead.
5837 * @param connection the connection
5838 * @returns the number of bytes that have been queued up but not sent
5841 dbus_connection_get_outgoing_size (DBusConnection *connection)
5845 _dbus_return_val_if_fail (connection != NULL, 0);
5847 CONNECTION_LOCK (connection);
5848 res = _dbus_counter_get_value (connection->outgoing_counter);
5849 CONNECTION_UNLOCK (connection);