1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002-2006 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-shared.h"
26 #include "dbus-connection.h"
27 #include "dbus-list.h"
28 #include "dbus-timeout.h"
29 #include "dbus-transport.h"
30 #include "dbus-watch.h"
31 #include "dbus-connection-internal.h"
32 #include "dbus-pending-call-internal.h"
33 #include "dbus-list.h"
34 #include "dbus-hash.h"
35 #include "dbus-message-internal.h"
36 #include "dbus-threads.h"
37 #include "dbus-protocol.h"
38 #include "dbus-dataslot.h"
39 #include "dbus-string.h"
40 #include "dbus-pending-call.h"
41 #include "dbus-object-tree.h"
42 #include "dbus-threads-internal.h"
45 #ifdef DBUS_DISABLE_CHECKS
46 #define TOOK_LOCK_CHECK(connection)
47 #define RELEASING_LOCK_CHECK(connection)
48 #define HAVE_LOCK_CHECK(connection)
50 #define TOOK_LOCK_CHECK(connection) do { \
51 _dbus_assert (!(connection)->have_connection_lock); \
52 (connection)->have_connection_lock = TRUE; \
54 #define RELEASING_LOCK_CHECK(connection) do { \
55 _dbus_assert ((connection)->have_connection_lock); \
56 (connection)->have_connection_lock = FALSE; \
58 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
59 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
64 #define CONNECTION_LOCK(connection) do { \
65 if (TRACE_LOCKS) { _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); } \
66 _dbus_mutex_lock ((connection)->mutex); \
67 TOOK_LOCK_CHECK (connection); \
70 #define CONNECTION_UNLOCK(connection) do { \
71 if (TRACE_LOCKS) { _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); } \
72 RELEASING_LOCK_CHECK (connection); \
73 _dbus_mutex_unlock ((connection)->mutex); \
76 #define DISPATCH_STATUS_NAME(s) \
77 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
78 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
79 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
83 * @defgroup DBusConnection DBusConnection
85 * @brief Connection to another application
87 * A DBusConnection represents a connection to another
88 * application. Messages can be sent and received via this connection.
89 * The other application may be a message bus; for convenience, the
90 * function dbus_bus_get() is provided to automatically open a
91 * connection to the well-known message buses.
93 * In brief a DBusConnection is a message queue associated with some
94 * message transport mechanism such as a socket. The connection
95 * maintains a queue of incoming messages and a queue of outgoing
98 * Several functions use the following terms:
100 * <li><b>read</b> means to fill the incoming message queue by reading from the socket</li>
101 * <li><b>write</b> means to drain the outgoing queue by writing to the socket</li>
102 * <li><b>dispatch</b> means to drain the incoming queue by invoking application-provided message handlers</li>
105 * The function dbus_connection_read_write_dispatch() for example does all
106 * three of these things, offering a simple alternative to a main loop.
108 * In an application with a main loop, the read/write/dispatch
109 * operations are usually separate.
111 * The connection provides #DBusWatch and #DBusTimeout objects to
112 * the main loop. These are used to know when reading, writing, or
113 * dispatching should be performed.
115 * Incoming messages are processed
116 * by calling dbus_connection_dispatch(). dbus_connection_dispatch()
117 * runs any handlers registered for the topmost message in the message
118 * queue, then discards the message, then returns.
120 * dbus_connection_get_dispatch_status() indicates whether
121 * messages are currently in the queue that need dispatching.
122 * dbus_connection_set_dispatch_status_function() allows
123 * you to set a function to be used to monitor the dispatch status.
125 * If you're using GLib or Qt add-on libraries for D-Bus, there are
126 * special convenience APIs in those libraries that hide
127 * all the details of dispatch and watch/timeout monitoring.
128 * For example, dbus_connection_setup_with_g_main().
130 * If you aren't using these add-on libraries, but want to process
131 * messages asynchronously, you must manually call
132 * dbus_connection_set_dispatch_status_function(),
133 * dbus_connection_set_watch_functions(),
134 * dbus_connection_set_timeout_functions() providing appropriate
135 * functions to integrate the connection with your application's main
136 * loop. This can be tricky to get right; main loops are not simple.
138 * If you don't need to be asynchronous, you can ignore #DBusWatch,
139 * #DBusTimeout, and dbus_connection_dispatch(). Instead,
140 * dbus_connection_read_write_dispatch() can be used.
142 * Or, in <em>very</em> simple applications,
143 * dbus_connection_pop_message() may be all you need, allowing you to
144 * avoid setting up any handler functions (see
145 * dbus_connection_add_filter(),
146 * dbus_connection_register_object_path() for more on handlers).
148 * When you use dbus_connection_send() or one of its variants to send
149 * a message, the message is added to the outgoing queue. It's
150 * actually written to the network later; either in
151 * dbus_watch_handle() invoked by your main loop, or in
152 * dbus_connection_flush() which blocks until it can write out the
153 * entire outgoing queue. The GLib/Qt add-on libraries again
154 * handle the details here for you by setting up watch functions.
156 * When a connection is disconnected, you are guaranteed to get a
157 * signal "Disconnected" from the interface
158 * #DBUS_INTERFACE_LOCAL, path
161 * You may not drop the last reference to a #DBusConnection
162 * until that connection has been disconnected.
164 * You may dispatch the unprocessed incoming message queue even if the
165 * connection is disconnected. However, "Disconnected" will always be
166 * the last message in the queue (obviously no messages are received
167 * after disconnection).
169 * After calling dbus_threads_init(), #DBusConnection has thread
170 * locks and drops them when invoking user callbacks, so in general is
171 * transparently threadsafe. However, #DBusMessage does NOT have
172 * thread locks; you must not send the same message to multiple
173 * #DBusConnection if those connections will be used from different threads,
176 * Also, if you dispatch or pop messages from multiple threads, it
177 * may work in the sense that it won't crash, but it's tough to imagine
178 * sane results; it will be completely unpredictable which messages
179 * go to which threads.
181 * It's recommended to dispatch from a single thread.
183 * The most useful function to call from multiple threads at once
184 * is dbus_connection_send_with_reply_and_block(). That is,
185 * multiple threads can make method calls at the same time.
187 * If you aren't using threads, you can use a main loop and
188 * dbus_pending_call_set_notify() to achieve a similar result.
192 * @defgroup DBusConnectionInternals DBusConnection implementation details
193 * @ingroup DBusInternals
194 * @brief Implementation details of DBusConnection
200 * Internal struct representing a message filter function
202 typedef struct DBusMessageFilter DBusMessageFilter;
205 * Internal struct representing a message filter function
207 struct DBusMessageFilter
209 DBusAtomic refcount; /**< Reference count */
210 DBusHandleMessageFunction function; /**< Function to call to filter */
211 void *user_data; /**< User data for the function */
212 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
217 * Internals of DBusPreallocatedSend
219 struct DBusPreallocatedSend
221 DBusConnection *connection; /**< Connection we'd send the message to */
222 DBusList *queue_link; /**< Preallocated link in the queue */
223 DBusList *counter_link; /**< Preallocated link in the resource counter */
226 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
229 * Implementation details of DBusConnection. All fields are private.
231 struct DBusConnection
233 DBusAtomic refcount; /**< Reference count. */
235 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
237 DBusMutex *dispatch_mutex; /**< Protects dispatch_acquired */
238 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */
239 DBusMutex *io_path_mutex; /**< Protects io_path_acquired */
240 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */
242 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
243 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
245 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
246 * dispatch_acquired will be set by the borrower
249 int n_outgoing; /**< Length of outgoing queue. */
250 int n_incoming; /**< Length of incoming queue. */
252 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
254 DBusTransport *transport; /**< Object that sends/receives messages over network. */
255 DBusWatchList *watches; /**< Stores active watches. */
256 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
258 DBusList *filter_list; /**< List of filters. */
260 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
262 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
264 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
265 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
267 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
268 void *wakeup_main_data; /**< Application data for wakeup_main_function */
269 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
271 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
272 void *dispatch_status_data; /**< Application data for dispatch_status_function */
273 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
275 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
277 DBusList *link_cache; /**< A cache of linked list links to prevent contention
278 * for the global linked list mempool lock
280 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
282 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
284 /* These two MUST be bools and not bitfields, because they are protected by a separate lock
285 * from connection->mutex and all bitfields in a word have to be read/written together.
286 * So you can't have a different lock for different bitfields in the same word.
288 dbus_bool_t dispatch_acquired; /**< Someone has dispatch path (can drain incoming queue) */
289 dbus_bool_t io_path_acquired; /**< Someone has transport io path (can use the transport to read/write messages) */
291 unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */
293 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
295 unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */
297 unsigned int disconnected_message_arrived : 1; /**< We popped or are dispatching the disconnected message.
298 * if the disconnect_message_link is NULL then we queued it, but
299 * this flag is whether it got to the head of the queue.
301 unsigned int disconnected_message_processed : 1; /**< We did our default handling of the disconnected message,
302 * such as closing the connection.
305 #ifndef DBUS_DISABLE_CHECKS
306 unsigned int have_connection_lock : 1; /**< Used to check locking */
309 #ifndef DBUS_DISABLE_CHECKS
310 int generation; /**< _dbus_current_generation that should correspond to this connection */
314 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
315 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
316 DBusDispatchStatus new_status);
317 static void _dbus_connection_last_unref (DBusConnection *connection);
318 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
319 static void _dbus_connection_release_dispatch (DBusConnection *connection);
320 static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
321 static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection);
322 static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection);
324 static DBusMessageFilter *
325 _dbus_message_filter_ref (DBusMessageFilter *filter)
327 _dbus_assert (filter->refcount.value > 0);
328 _dbus_atomic_inc (&filter->refcount);
334 _dbus_message_filter_unref (DBusMessageFilter *filter)
336 _dbus_assert (filter->refcount.value > 0);
338 if (_dbus_atomic_dec (&filter->refcount) == 1)
340 if (filter->free_user_data_function)
341 (* filter->free_user_data_function) (filter->user_data);
348 * Acquires the connection lock.
350 * @param connection the connection.
353 _dbus_connection_lock (DBusConnection *connection)
355 CONNECTION_LOCK (connection);
359 * Releases the connection lock.
361 * @param connection the connection.
364 _dbus_connection_unlock (DBusConnection *connection)
366 CONNECTION_UNLOCK (connection);
370 * Wakes up the main loop if it is sleeping
371 * Needed if we're e.g. queueing outgoing messages
372 * on a thread while the mainloop sleeps.
374 * @param connection the connection.
377 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
379 if (connection->wakeup_main_function)
380 (*connection->wakeup_main_function) (connection->wakeup_main_data);
383 #ifdef DBUS_BUILD_TESTS
384 /* For now this function isn't used */
386 * Adds a message to the incoming message queue, returning #FALSE
387 * if there's insufficient memory to queue the message.
388 * Does not take over refcount of the message.
390 * @param connection the connection.
391 * @param message the message to queue.
392 * @returns #TRUE on success.
395 _dbus_connection_queue_received_message (DBusConnection *connection,
396 DBusMessage *message)
400 link = _dbus_list_alloc_link (message);
404 dbus_message_ref (message);
405 _dbus_connection_queue_received_message_link (connection, link);
411 * Gets the locks so we can examine them
413 * @param connection the connection.
414 * @param mutex_loc return for the location of the main mutex pointer
415 * @param dispatch_mutex_loc return location of the dispatch mutex pointer
416 * @param io_path_mutex_loc return location of the io_path mutex pointer
417 * @param dispatch_cond_loc return location of the dispatch conditional
419 * @param io_path_cond_loc return location of the io_path conditional
423 _dbus_connection_test_get_locks (DBusConnection *connection,
424 DBusMutex **mutex_loc,
425 DBusMutex **dispatch_mutex_loc,
426 DBusMutex **io_path_mutex_loc,
427 DBusCondVar **dispatch_cond_loc,
428 DBusCondVar **io_path_cond_loc)
430 *mutex_loc = connection->mutex;
431 *dispatch_mutex_loc = connection->dispatch_mutex;
432 *io_path_mutex_loc = connection->io_path_mutex;
433 *dispatch_cond_loc = connection->dispatch_cond;
434 *io_path_cond_loc = connection->io_path_cond;
439 * Adds a message-containing list link to the incoming message queue,
440 * taking ownership of the link and the message's current refcount.
441 * Cannot fail due to lack of memory.
443 * @param connection the connection.
444 * @param link the message link to queue.
447 _dbus_connection_queue_received_message_link (DBusConnection *connection,
450 DBusPendingCall *pending;
451 dbus_int32_t reply_serial;
452 DBusMessage *message;
454 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
456 _dbus_list_append_link (&connection->incoming_messages,
458 message = link->data;
460 /* If this is a reply we're waiting on, remove timeout for it */
461 reply_serial = dbus_message_get_reply_serial (message);
462 if (reply_serial != -1)
464 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
468 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
469 _dbus_connection_remove_timeout_unlocked (connection,
470 _dbus_pending_call_get_timeout_unlocked (pending));
472 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
478 connection->n_incoming += 1;
480 _dbus_connection_wakeup_mainloop (connection);
482 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
484 dbus_message_get_type (message),
485 dbus_message_get_path (message) ?
486 dbus_message_get_path (message) :
488 dbus_message_get_interface (message) ?
489 dbus_message_get_interface (message) :
491 dbus_message_get_member (message) ?
492 dbus_message_get_member (message) :
494 dbus_message_get_signature (message),
495 dbus_message_get_reply_serial (message),
497 connection->n_incoming);}
500 * Adds a link + message to the incoming message queue.
501 * Can't fail. Takes ownership of both link and message.
503 * @param connection the connection.
504 * @param link the list node and message to queue.
508 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
511 HAVE_LOCK_CHECK (connection);
513 _dbus_list_append_link (&connection->incoming_messages, link);
515 connection->n_incoming += 1;
517 _dbus_connection_wakeup_mainloop (connection);
519 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
520 link->data, connection, connection->n_incoming);
525 * Checks whether there are messages in the outgoing message queue.
526 * Called with connection lock held.
528 * @param connection the connection.
529 * @returns #TRUE if the outgoing queue is non-empty.
532 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
534 HAVE_LOCK_CHECK (connection);
535 return connection->outgoing_messages != NULL;
539 * Checks whether there are messages in the outgoing message queue.
540 * Use dbus_connection_flush() to block until all outgoing
541 * messages have been written to the underlying transport
542 * (such as a socket).
544 * @param connection the connection.
545 * @returns #TRUE if the outgoing queue is non-empty.
548 dbus_connection_has_messages_to_send (DBusConnection *connection)
552 _dbus_return_val_if_fail (connection != NULL, FALSE);
554 CONNECTION_LOCK (connection);
555 v = _dbus_connection_has_messages_to_send_unlocked (connection);
556 CONNECTION_UNLOCK (connection);
562 * Gets the next outgoing message. The message remains in the
563 * queue, and the caller does not own a reference to it.
565 * @param connection the connection.
566 * @returns the message to be sent.
569 _dbus_connection_get_message_to_send (DBusConnection *connection)
571 HAVE_LOCK_CHECK (connection);
573 return _dbus_list_get_last (&connection->outgoing_messages);
577 * Notifies the connection that a message has been sent, so the
578 * message can be removed from the outgoing queue.
579 * Called with the connection lock held.
581 * @param connection the connection.
582 * @param message the message that was sent.
585 _dbus_connection_message_sent (DBusConnection *connection,
586 DBusMessage *message)
590 HAVE_LOCK_CHECK (connection);
592 /* This can be called before we even complete authentication, since
593 * it's called on disconnect to clean up the outgoing queue.
594 * It's also called as we successfully send each message.
597 link = _dbus_list_get_last_link (&connection->outgoing_messages);
598 _dbus_assert (link != NULL);
599 _dbus_assert (link->data == message);
601 /* Save this link in the link cache */
602 _dbus_list_unlink (&connection->outgoing_messages,
604 _dbus_list_prepend_link (&connection->link_cache, link);
606 connection->n_outgoing -= 1;
608 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
610 dbus_message_get_type (message),
611 dbus_message_get_path (message) ?
612 dbus_message_get_path (message) :
614 dbus_message_get_interface (message) ?
615 dbus_message_get_interface (message) :
617 dbus_message_get_member (message) ?
618 dbus_message_get_member (message) :
620 dbus_message_get_signature (message),
621 connection, connection->n_outgoing);
623 /* Save this link in the link cache also */
624 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
626 _dbus_list_prepend_link (&connection->link_cache, link);
628 dbus_message_unref (message);
631 /** Function to be called in protected_change_watch() with refcount held */
632 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
634 /** Function to be called in protected_change_watch() with refcount held */
635 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
637 /** Function to be called in protected_change_watch() with refcount held */
638 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
640 dbus_bool_t enabled);
643 protected_change_watch (DBusConnection *connection,
645 DBusWatchAddFunction add_function,
646 DBusWatchRemoveFunction remove_function,
647 DBusWatchToggleFunction toggle_function,
650 DBusWatchList *watches;
653 HAVE_LOCK_CHECK (connection);
655 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
656 * drop lock and call out" one; but it has to be propagated up through all callers
659 watches = connection->watches;
662 connection->watches = NULL;
663 _dbus_connection_ref_unlocked (connection);
664 CONNECTION_UNLOCK (connection);
667 retval = (* add_function) (watches, watch);
668 else if (remove_function)
671 (* remove_function) (watches, watch);
676 (* toggle_function) (watches, watch, enabled);
679 CONNECTION_LOCK (connection);
680 connection->watches = watches;
681 _dbus_connection_unref_unlocked (connection);
691 * Adds a watch using the connection's DBusAddWatchFunction if
692 * available. Otherwise records the watch to be added when said
693 * function is available. Also re-adds the watch if the
694 * DBusAddWatchFunction changes. May fail due to lack of memory.
695 * Connection lock should be held when calling this.
697 * @param connection the connection.
698 * @param watch the watch to add.
699 * @returns #TRUE on success.
702 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
705 return protected_change_watch (connection, watch,
706 _dbus_watch_list_add_watch,
711 * Removes a watch using the connection's DBusRemoveWatchFunction
712 * if available. It's an error to call this function on a watch
713 * that was not previously added.
714 * Connection lock should be held when calling this.
716 * @param connection the connection.
717 * @param watch the watch to remove.
720 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
723 protected_change_watch (connection, watch,
725 _dbus_watch_list_remove_watch,
730 * Toggles a watch and notifies app via connection's
731 * DBusWatchToggledFunction if available. It's an error to call this
732 * function on a watch that was not previously added.
733 * Connection lock should be held when calling this.
735 * @param connection the connection.
736 * @param watch the watch to toggle.
737 * @param enabled whether to enable or disable
740 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
744 _dbus_assert (watch != NULL);
746 protected_change_watch (connection, watch,
748 _dbus_watch_list_toggle_watch,
752 /** Function to be called in protected_change_timeout() with refcount held */
753 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
754 DBusTimeout *timeout);
755 /** Function to be called in protected_change_timeout() with refcount held */
756 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
757 DBusTimeout *timeout);
758 /** Function to be called in protected_change_timeout() with refcount held */
759 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
760 DBusTimeout *timeout,
761 dbus_bool_t enabled);
764 protected_change_timeout (DBusConnection *connection,
765 DBusTimeout *timeout,
766 DBusTimeoutAddFunction add_function,
767 DBusTimeoutRemoveFunction remove_function,
768 DBusTimeoutToggleFunction toggle_function,
771 DBusTimeoutList *timeouts;
774 HAVE_LOCK_CHECK (connection);
776 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
777 * drop lock and call out" one; but it has to be propagated up through all callers
780 timeouts = connection->timeouts;
783 connection->timeouts = NULL;
784 _dbus_connection_ref_unlocked (connection);
785 CONNECTION_UNLOCK (connection);
788 retval = (* add_function) (timeouts, timeout);
789 else if (remove_function)
792 (* remove_function) (timeouts, timeout);
797 (* toggle_function) (timeouts, timeout, enabled);
800 CONNECTION_LOCK (connection);
801 connection->timeouts = timeouts;
802 _dbus_connection_unref_unlocked (connection);
811 * Adds a timeout using the connection's DBusAddTimeoutFunction if
812 * available. Otherwise records the timeout to be added when said
813 * function is available. Also re-adds the timeout if the
814 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
815 * The timeout will fire repeatedly until removed.
816 * Connection lock should be held when calling this.
818 * @param connection the connection.
819 * @param timeout the timeout to add.
820 * @returns #TRUE on success.
823 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
824 DBusTimeout *timeout)
826 return protected_change_timeout (connection, timeout,
827 _dbus_timeout_list_add_timeout,
832 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
833 * if available. It's an error to call this function on a timeout
834 * that was not previously added.
835 * Connection lock should be held when calling this.
837 * @param connection the connection.
838 * @param timeout the timeout to remove.
841 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
842 DBusTimeout *timeout)
844 protected_change_timeout (connection, timeout,
846 _dbus_timeout_list_remove_timeout,
851 * Toggles a timeout and notifies app via connection's
852 * DBusTimeoutToggledFunction if available. It's an error to call this
853 * function on a timeout that was not previously added.
854 * Connection lock should be held when calling this.
856 * @param connection the connection.
857 * @param timeout the timeout to toggle.
858 * @param enabled whether to enable or disable
861 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
862 DBusTimeout *timeout,
865 protected_change_timeout (connection, timeout,
867 _dbus_timeout_list_toggle_timeout,
872 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
873 DBusPendingCall *pending)
875 dbus_uint32_t reply_serial;
876 DBusTimeout *timeout;
878 HAVE_LOCK_CHECK (connection);
880 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
882 _dbus_assert (reply_serial != 0);
884 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
886 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
889 if (!_dbus_hash_table_insert_int (connection->pending_replies,
893 _dbus_connection_remove_timeout_unlocked (connection, timeout);
895 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
896 HAVE_LOCK_CHECK (connection);
900 _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
902 _dbus_pending_call_ref_unlocked (pending);
904 HAVE_LOCK_CHECK (connection);
910 free_pending_call_on_hash_removal (void *data)
912 DBusPendingCall *pending;
913 DBusConnection *connection;
920 connection = _dbus_pending_call_get_connection_unlocked (pending);
922 HAVE_LOCK_CHECK (connection);
924 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
926 _dbus_connection_remove_timeout_unlocked (connection,
927 _dbus_pending_call_get_timeout_unlocked (pending));
929 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
932 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
933 * here, but the pending call finalizer could in principle call out to
934 * application code so we pretty much have to... some larger code reorg
937 _dbus_connection_ref_unlocked (connection);
938 _dbus_pending_call_unref_and_unlock (pending);
939 CONNECTION_LOCK (connection);
940 _dbus_connection_unref_unlocked (connection);
944 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
945 DBusPendingCall *pending)
947 /* This ends up unlocking to call the pending call finalizer, which is unexpected to
950 _dbus_hash_table_remove_int (connection->pending_replies,
951 _dbus_pending_call_get_reply_serial_unlocked (pending));
955 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
956 DBusPendingCall *pending)
958 /* The idea here is to avoid finalizing the pending call
959 * with the lock held, since there's a destroy notifier
960 * in pending call that goes out to application code.
962 * There's an extra unlock inside the hash table
963 * "free pending call" function FIXME...
965 _dbus_pending_call_ref_unlocked (pending);
966 _dbus_hash_table_remove_int (connection->pending_replies,
967 _dbus_pending_call_get_reply_serial_unlocked (pending));
968 _dbus_pending_call_unref_and_unlock (pending);
972 * Removes a pending call from the connection, such that
973 * the pending reply will be ignored. May drop the last
974 * reference to the pending call.
976 * @param connection the connection
977 * @param pending the pending call
980 _dbus_connection_remove_pending_call (DBusConnection *connection,
981 DBusPendingCall *pending)
983 CONNECTION_LOCK (connection);
984 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
988 * Acquire the transporter I/O path. This must be done before
989 * doing any I/O in the transporter. May sleep and drop the
990 * IO path mutex while waiting for the I/O path.
992 * @param connection the connection.
993 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
994 * @returns TRUE if the I/O path was acquired.
997 _dbus_connection_acquire_io_path (DBusConnection *connection,
998 int timeout_milliseconds)
1000 dbus_bool_t we_acquired;
1002 HAVE_LOCK_CHECK (connection);
1004 /* We don't want the connection to vanish */
1005 _dbus_connection_ref_unlocked (connection);
1007 /* We will only touch io_path_acquired which is protected by our mutex */
1008 CONNECTION_UNLOCK (connection);
1010 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1011 _dbus_mutex_lock (connection->io_path_mutex);
1013 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
1014 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
1016 we_acquired = FALSE;
1018 if (connection->io_path_acquired)
1020 if (timeout_milliseconds != -1)
1022 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
1023 _DBUS_FUNCTION_NAME, timeout_milliseconds);
1025 if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1026 connection->io_path_mutex,
1027 timeout_milliseconds))
1029 /* We timed out before anyone signaled. */
1030 /* (writing the loop to handle the !timedout case by
1031 * waiting longer if needed is a pain since dbus
1032 * wraps pthread_cond_timedwait to take a relative
1033 * time instead of absolute, something kind of stupid
1034 * on our part. for now it doesn't matter, we will just
1035 * end up back here eventually.)
1041 while (connection->io_path_acquired)
1043 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
1044 _dbus_condvar_wait (connection->io_path_cond,
1045 connection->io_path_mutex);
1050 if (!connection->io_path_acquired)
1053 connection->io_path_acquired = TRUE;
1056 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
1057 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
1059 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1060 _dbus_mutex_unlock (connection->io_path_mutex);
1062 CONNECTION_LOCK (connection);
1064 HAVE_LOCK_CHECK (connection);
1066 _dbus_connection_unref_unlocked (connection);
1072 * Release the I/O path when you're done with it. Only call
1073 * after you've acquired the I/O. Wakes up at most one thread
1074 * currently waiting to acquire the I/O path.
1076 * @param connection the connection.
1079 _dbus_connection_release_io_path (DBusConnection *connection)
1081 HAVE_LOCK_CHECK (connection);
1083 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1084 _dbus_mutex_lock (connection->io_path_mutex);
1086 _dbus_assert (connection->io_path_acquired);
1088 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
1089 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
1091 connection->io_path_acquired = FALSE;
1092 _dbus_condvar_wake_one (connection->io_path_cond);
1094 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1095 _dbus_mutex_unlock (connection->io_path_mutex);
1099 * Queues incoming messages and sends outgoing messages for this
1100 * connection, optionally blocking in the process. Each call to
1101 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1102 * time and then read or write data if possible.
1104 * The purpose of this function is to be able to flush outgoing
1105 * messages or queue up incoming messages without returning
1106 * control to the application and causing reentrancy weirdness.
1108 * The flags parameter allows you to specify whether to
1109 * read incoming messages, write outgoing messages, or both,
1110 * and whether to block if no immediate action is possible.
1112 * The timeout_milliseconds parameter does nothing unless the
1113 * iteration is blocking.
1115 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1116 * wasn't specified, then it's impossible to block, even if
1117 * you specify DBUS_ITERATION_BLOCK; in that case the function
1118 * returns immediately.
1120 * Called with connection lock held.
1122 * @param connection the connection.
1123 * @param flags iteration flags.
1124 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1127 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1129 int timeout_milliseconds)
1131 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1133 HAVE_LOCK_CHECK (connection);
1135 if (connection->n_outgoing == 0)
1136 flags &= ~DBUS_ITERATION_DO_WRITING;
1138 if (_dbus_connection_acquire_io_path (connection,
1139 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1141 HAVE_LOCK_CHECK (connection);
1143 _dbus_transport_do_iteration (connection->transport,
1144 flags, timeout_milliseconds);
1145 _dbus_connection_release_io_path (connection);
1148 HAVE_LOCK_CHECK (connection);
1150 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1154 * Creates a new connection for the given transport. A transport
1155 * represents a message stream that uses some concrete mechanism, such
1156 * as UNIX domain sockets. May return #NULL if insufficient
1157 * memory exists to create the connection.
1159 * @param transport the transport.
1160 * @returns the new connection, or #NULL on failure.
1163 _dbus_connection_new_for_transport (DBusTransport *transport)
1165 DBusConnection *connection;
1166 DBusWatchList *watch_list;
1167 DBusTimeoutList *timeout_list;
1168 DBusHashTable *pending_replies;
1169 DBusList *disconnect_link;
1170 DBusMessage *disconnect_message;
1171 DBusCounter *outgoing_counter;
1172 DBusObjectTree *objects;
1176 pending_replies = NULL;
1177 timeout_list = NULL;
1178 disconnect_link = NULL;
1179 disconnect_message = NULL;
1180 outgoing_counter = NULL;
1183 watch_list = _dbus_watch_list_new ();
1184 if (watch_list == NULL)
1187 timeout_list = _dbus_timeout_list_new ();
1188 if (timeout_list == NULL)
1192 _dbus_hash_table_new (DBUS_HASH_INT,
1194 (DBusFreeFunction)free_pending_call_on_hash_removal);
1195 if (pending_replies == NULL)
1198 connection = dbus_new0 (DBusConnection, 1);
1199 if (connection == NULL)
1202 _dbus_mutex_new_at_location (&connection->mutex);
1203 if (connection->mutex == NULL)
1206 _dbus_mutex_new_at_location (&connection->io_path_mutex);
1207 if (connection->io_path_mutex == NULL)
1210 _dbus_mutex_new_at_location (&connection->dispatch_mutex);
1211 if (connection->dispatch_mutex == NULL)
1214 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1215 if (connection->dispatch_cond == NULL)
1218 _dbus_condvar_new_at_location (&connection->io_path_cond);
1219 if (connection->io_path_cond == NULL)
1222 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1223 DBUS_INTERFACE_LOCAL,
1226 if (disconnect_message == NULL)
1229 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1230 if (disconnect_link == NULL)
1233 outgoing_counter = _dbus_counter_new ();
1234 if (outgoing_counter == NULL)
1237 objects = _dbus_object_tree_new (connection);
1238 if (objects == NULL)
1241 if (_dbus_modify_sigpipe)
1242 _dbus_disable_sigpipe ();
1244 connection->refcount.value = 1;
1245 connection->transport = transport;
1246 connection->watches = watch_list;
1247 connection->timeouts = timeout_list;
1248 connection->pending_replies = pending_replies;
1249 connection->outgoing_counter = outgoing_counter;
1250 connection->filter_list = NULL;
1251 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1252 connection->objects = objects;
1253 connection->exit_on_disconnect = FALSE;
1254 connection->shareable = FALSE;
1255 connection->route_peer_messages = FALSE;
1256 connection->disconnected_message_arrived = FALSE;
1257 connection->disconnected_message_processed = FALSE;
1259 #ifndef DBUS_DISABLE_CHECKS
1260 connection->generation = _dbus_current_generation;
1263 _dbus_data_slot_list_init (&connection->slot_list);
1265 connection->client_serial = 1;
1267 connection->disconnect_message_link = disconnect_link;
1269 CONNECTION_LOCK (connection);
1271 if (!_dbus_transport_set_connection (transport, connection))
1273 CONNECTION_UNLOCK (connection);
1278 _dbus_transport_ref (transport);
1280 CONNECTION_UNLOCK (connection);
1285 if (disconnect_message != NULL)
1286 dbus_message_unref (disconnect_message);
1288 if (disconnect_link != NULL)
1289 _dbus_list_free_link (disconnect_link);
1291 if (connection != NULL)
1293 _dbus_condvar_free_at_location (&connection->io_path_cond);
1294 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1295 _dbus_mutex_free_at_location (&connection->mutex);
1296 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1297 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1298 dbus_free (connection);
1300 if (pending_replies)
1301 _dbus_hash_table_unref (pending_replies);
1304 _dbus_watch_list_free (watch_list);
1307 _dbus_timeout_list_free (timeout_list);
1309 if (outgoing_counter)
1310 _dbus_counter_unref (outgoing_counter);
1313 _dbus_object_tree_unref (objects);
1319 * Increments the reference count of a DBusConnection.
1320 * Requires that the caller already holds the connection lock.
1322 * @param connection the connection.
1323 * @returns the connection.
1326 _dbus_connection_ref_unlocked (DBusConnection *connection)
1328 _dbus_assert (connection != NULL);
1329 _dbus_assert (connection->generation == _dbus_current_generation);
1331 HAVE_LOCK_CHECK (connection);
1333 #ifdef DBUS_HAVE_ATOMIC_INT
1334 _dbus_atomic_inc (&connection->refcount);
1336 _dbus_assert (connection->refcount.value > 0);
1337 connection->refcount.value += 1;
1344 * Decrements the reference count of a DBusConnection.
1345 * Requires that the caller already holds the connection lock.
1347 * @param connection the connection.
1350 _dbus_connection_unref_unlocked (DBusConnection *connection)
1352 dbus_bool_t last_unref;
1354 HAVE_LOCK_CHECK (connection);
1356 _dbus_assert (connection != NULL);
1358 /* The connection lock is better than the global
1359 * lock in the atomic increment fallback
1362 #ifdef DBUS_HAVE_ATOMIC_INT
1363 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1365 _dbus_assert (connection->refcount.value > 0);
1367 connection->refcount.value -= 1;
1368 last_unref = (connection->refcount.value == 0);
1370 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1375 _dbus_connection_last_unref (connection);
1378 static dbus_uint32_t
1379 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1383 serial = connection->client_serial++;
1385 if (connection->client_serial < 0)
1386 connection->client_serial = 1;
1392 * A callback for use with dbus_watch_new() to create a DBusWatch.
1394 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1395 * and the virtual handle_watch in DBusTransport if we got rid of it.
1396 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1399 * @param watch the watch.
1400 * @param condition the current condition of the file descriptors being watched.
1401 * @param data must be a pointer to a #DBusConnection
1402 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1405 _dbus_connection_handle_watch (DBusWatch *watch,
1406 unsigned int condition,
1409 DBusConnection *connection;
1411 DBusDispatchStatus status;
1415 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1417 CONNECTION_LOCK (connection);
1418 _dbus_connection_acquire_io_path (connection, -1);
1419 HAVE_LOCK_CHECK (connection);
1420 retval = _dbus_transport_handle_watch (connection->transport,
1423 _dbus_connection_release_io_path (connection);
1425 HAVE_LOCK_CHECK (connection);
1427 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1429 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1431 /* this calls out to user code */
1432 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1434 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1439 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1440 static DBusHashTable *shared_connections = NULL;
1443 shared_connections_shutdown (void *data)
1447 _DBUS_LOCK (shared_connections);
1449 /* This is a little bit unpleasant... better ideas? */
1450 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
1452 DBusConnection *connection;
1453 DBusMessage *message;
1456 _dbus_hash_iter_init (shared_connections, &iter);
1457 _dbus_hash_iter_next (&iter);
1459 connection = _dbus_hash_iter_get_value (&iter);
1461 _DBUS_UNLOCK (shared_connections);
1463 dbus_connection_ref (connection);
1464 _dbus_connection_close_possibly_shared (connection);
1466 /* Churn through to the Disconnected message */
1467 while ((message = dbus_connection_pop_message (connection)))
1469 dbus_message_unref (message);
1471 dbus_connection_unref (connection);
1473 _DBUS_LOCK (shared_connections);
1475 /* The connection should now be dead and not in our hash ... */
1476 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
1479 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1481 _dbus_hash_table_unref (shared_connections);
1482 shared_connections = NULL;
1484 _DBUS_UNLOCK (shared_connections);
1488 connection_lookup_shared (DBusAddressEntry *entry,
1489 DBusConnection **result)
1491 _dbus_verbose ("checking for existing connection\n");
1495 _DBUS_LOCK (shared_connections);
1497 if (shared_connections == NULL)
1499 _dbus_verbose ("creating shared_connections hash table\n");
1501 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1504 if (shared_connections == NULL)
1506 _DBUS_UNLOCK (shared_connections);
1510 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1512 _dbus_hash_table_unref (shared_connections);
1513 shared_connections = NULL;
1514 _DBUS_UNLOCK (shared_connections);
1518 _dbus_verbose (" successfully created shared_connections\n");
1520 _DBUS_UNLOCK (shared_connections);
1521 return TRUE; /* no point looking up in the hash we just made */
1527 guid = dbus_address_entry_get_value (entry, "guid");
1531 DBusConnection *connection;
1533 connection = _dbus_hash_table_lookup_string (shared_connections,
1538 /* The DBusConnection can't be finalized without taking
1539 * the shared_connections lock to remove it from the
1540 * hash. So it's safe to ref the connection here.
1541 * However, it may be disconnected if the Disconnected
1542 * message hasn't been processed yet, in which case we
1543 * want to pretend it isn't in the hash and avoid
1546 * The idea is to avoid ever returning a disconnected connection
1547 * from dbus_connection_open(). We could just synchronously
1548 * drop our shared ref to the connection on connection disconnect,
1549 * and then assert here that the connection is connected, but
1550 * that causes reentrancy headaches.
1552 CONNECTION_LOCK (connection);
1553 if (_dbus_connection_get_is_connected_unlocked (connection))
1555 _dbus_connection_ref_unlocked (connection);
1556 *result = connection;
1557 _dbus_verbose ("looked up existing connection to server guid %s\n",
1562 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1565 CONNECTION_UNLOCK (connection);
1569 _DBUS_UNLOCK (shared_connections);
1575 connection_record_shared_unlocked (DBusConnection *connection,
1579 char *guid_in_connection;
1581 HAVE_LOCK_CHECK (connection);
1582 _dbus_assert (connection->server_guid == NULL);
1583 _dbus_assert (connection->shareable);
1585 /* get a hard ref on this connection, even if
1586 * we won't in fact store it in the hash, we still
1587 * need to hold a ref on it until it's disconnected.
1589 _dbus_connection_ref_unlocked (connection);
1592 return TRUE; /* don't store in the hash */
1594 /* A separate copy of the key is required in the hash table, because
1595 * we don't have a lock on the connection when we are doing a hash
1599 guid_key = _dbus_strdup (guid);
1600 if (guid_key == NULL)
1603 guid_in_connection = _dbus_strdup (guid);
1604 if (guid_in_connection == NULL)
1606 dbus_free (guid_key);
1610 _DBUS_LOCK (shared_connections);
1611 _dbus_assert (shared_connections != NULL);
1613 if (!_dbus_hash_table_insert_string (shared_connections,
1614 guid_key, connection))
1616 dbus_free (guid_key);
1617 dbus_free (guid_in_connection);
1618 _DBUS_UNLOCK (shared_connections);
1622 connection->server_guid = guid_in_connection;
1624 _dbus_verbose ("stored connection to %s to be shared\n",
1625 connection->server_guid);
1627 _DBUS_UNLOCK (shared_connections);
1629 _dbus_assert (connection->server_guid != NULL);
1635 connection_forget_shared_unlocked (DBusConnection *connection)
1637 HAVE_LOCK_CHECK (connection);
1639 if (!connection->shareable)
1642 if (connection->server_guid != NULL)
1644 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1645 connection->server_guid);
1647 _DBUS_LOCK (shared_connections);
1649 if (!_dbus_hash_table_remove_string (shared_connections,
1650 connection->server_guid))
1651 _dbus_assert_not_reached ("connection was not in the shared table");
1653 dbus_free (connection->server_guid);
1654 connection->server_guid = NULL;
1655 _DBUS_UNLOCK (shared_connections);
1658 /* remove our reference held on all shareable connections */
1659 _dbus_connection_unref_unlocked (connection);
1662 static DBusConnection*
1663 connection_try_from_address_entry (DBusAddressEntry *entry,
1666 DBusTransport *transport;
1667 DBusConnection *connection;
1669 transport = _dbus_transport_open (entry, error);
1671 if (transport == NULL)
1673 _DBUS_ASSERT_ERROR_IS_SET (error);
1677 connection = _dbus_connection_new_for_transport (transport);
1679 _dbus_transport_unref (transport);
1681 if (connection == NULL)
1683 _DBUS_SET_OOM (error);
1687 #ifndef DBUS_DISABLE_CHECKS
1688 _dbus_assert (!connection->have_connection_lock);
1694 * If the shared parameter is true, then any existing connection will
1695 * be used (and if a new connection is created, it will be available
1696 * for use by others). If the shared parameter is false, a new
1697 * connection will always be created, and the new connection will
1698 * never be returned to other callers.
1700 * @param address the address
1701 * @param shared whether the connection is shared or private
1702 * @param error error return
1703 * @returns the connection or #NULL on error
1705 static DBusConnection*
1706 _dbus_connection_open_internal (const char *address,
1710 DBusConnection *connection;
1711 DBusAddressEntry **entries;
1712 DBusError tmp_error;
1713 DBusError first_error;
1716 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1718 _dbus_verbose ("opening %s connection to: %s\n",
1719 shared ? "shared" : "private", address);
1721 if (!dbus_parse_address (address, &entries, &len, error))
1724 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1728 dbus_error_init (&tmp_error);
1729 dbus_error_init (&first_error);
1730 for (i = 0; i < len; i++)
1734 if (!connection_lookup_shared (entries[i], &connection))
1735 _DBUS_SET_OOM (&tmp_error);
1738 if (connection == NULL)
1740 connection = connection_try_from_address_entry (entries[i],
1743 if (connection != NULL && shared)
1747 connection->shareable = TRUE;
1749 /* guid may be NULL */
1750 guid = dbus_address_entry_get_value (entries[i], "guid");
1752 CONNECTION_LOCK (connection);
1754 if (!connection_record_shared_unlocked (connection, guid))
1756 _DBUS_SET_OOM (&tmp_error);
1757 _dbus_connection_close_possibly_shared_and_unlock (connection);
1758 dbus_connection_unref (connection);
1762 CONNECTION_UNLOCK (connection);
1769 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1772 dbus_move_error (&tmp_error, &first_error);
1774 dbus_error_free (&tmp_error);
1777 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1778 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1780 if (connection == NULL)
1782 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1783 dbus_move_error (&first_error, error);
1786 dbus_error_free (&first_error);
1788 dbus_address_entries_free (entries);
1793 * Closes a shared OR private connection, while dbus_connection_close() can
1794 * only be used on private connections. Should only be called by the
1795 * dbus code that owns the connection - an owner must be known,
1796 * the open/close state is like malloc/free, not like ref/unref.
1798 * @param connection the connection
1801 _dbus_connection_close_possibly_shared (DBusConnection *connection)
1803 _dbus_assert (connection != NULL);
1804 _dbus_assert (connection->generation == _dbus_current_generation);
1806 CONNECTION_LOCK (connection);
1807 _dbus_connection_close_possibly_shared_and_unlock (connection);
1810 static DBusPreallocatedSend*
1811 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1813 DBusPreallocatedSend *preallocated;
1815 HAVE_LOCK_CHECK (connection);
1817 _dbus_assert (connection != NULL);
1819 preallocated = dbus_new (DBusPreallocatedSend, 1);
1820 if (preallocated == NULL)
1823 if (connection->link_cache != NULL)
1825 preallocated->queue_link =
1826 _dbus_list_pop_first_link (&connection->link_cache);
1827 preallocated->queue_link->data = NULL;
1831 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1832 if (preallocated->queue_link == NULL)
1836 if (connection->link_cache != NULL)
1838 preallocated->counter_link =
1839 _dbus_list_pop_first_link (&connection->link_cache);
1840 preallocated->counter_link->data = connection->outgoing_counter;
1844 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1845 if (preallocated->counter_link == NULL)
1849 _dbus_counter_ref (preallocated->counter_link->data);
1851 preallocated->connection = connection;
1853 return preallocated;
1856 _dbus_list_free_link (preallocated->queue_link);
1858 dbus_free (preallocated);
1863 /* Called with lock held, does not update dispatch status */
1865 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
1866 DBusPreallocatedSend *preallocated,
1867 DBusMessage *message,
1868 dbus_uint32_t *client_serial)
1870 dbus_uint32_t serial;
1873 preallocated->queue_link->data = message;
1874 _dbus_list_prepend_link (&connection->outgoing_messages,
1875 preallocated->queue_link);
1877 _dbus_message_add_size_counter_link (message,
1878 preallocated->counter_link);
1880 dbus_free (preallocated);
1881 preallocated = NULL;
1883 dbus_message_ref (message);
1885 connection->n_outgoing += 1;
1887 sig = dbus_message_get_signature (message);
1889 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
1891 dbus_message_get_type (message),
1892 dbus_message_get_path (message) ?
1893 dbus_message_get_path (message) :
1895 dbus_message_get_interface (message) ?
1896 dbus_message_get_interface (message) :
1898 dbus_message_get_member (message) ?
1899 dbus_message_get_member (message) :
1902 dbus_message_get_destination (message) ?
1903 dbus_message_get_destination (message) :
1906 connection->n_outgoing);
1908 if (dbus_message_get_serial (message) == 0)
1910 serial = _dbus_connection_get_next_client_serial (connection);
1911 _dbus_message_set_serial (message, serial);
1913 *client_serial = serial;
1918 *client_serial = dbus_message_get_serial (message);
1921 _dbus_verbose ("Message %p serial is %u\n",
1922 message, dbus_message_get_serial (message));
1924 _dbus_message_lock (message);
1926 /* Now we need to run an iteration to hopefully just write the messages
1927 * out immediately, and otherwise get them queued up
1929 _dbus_connection_do_iteration_unlocked (connection,
1930 DBUS_ITERATION_DO_WRITING,
1933 /* If stuff is still queued up, be sure we wake up the main loop */
1934 if (connection->n_outgoing > 0)
1935 _dbus_connection_wakeup_mainloop (connection);
1939 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
1940 DBusPreallocatedSend *preallocated,
1941 DBusMessage *message,
1942 dbus_uint32_t *client_serial)
1944 DBusDispatchStatus status;
1946 HAVE_LOCK_CHECK (connection);
1948 _dbus_connection_send_preallocated_unlocked_no_update (connection,
1950 message, client_serial);
1952 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1953 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1955 /* this calls out to user code */
1956 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1960 * Like dbus_connection_send(), but assumes the connection
1961 * is already locked on function entry, and unlocks before returning.
1963 * @param connection the connection
1964 * @param message the message to send
1965 * @param client_serial return location for client serial of sent message
1966 * @returns #FALSE on out-of-memory
1969 _dbus_connection_send_and_unlock (DBusConnection *connection,
1970 DBusMessage *message,
1971 dbus_uint32_t *client_serial)
1973 DBusPreallocatedSend *preallocated;
1975 _dbus_assert (connection != NULL);
1976 _dbus_assert (message != NULL);
1978 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
1979 if (preallocated == NULL)
1981 CONNECTION_UNLOCK (connection);
1985 _dbus_connection_send_preallocated_and_unlock (connection,
1993 * Used internally to handle the semantics of dbus_server_set_new_connection_function().
1994 * If the new connection function does not ref the connection, we want to close it.
1996 * A bit of a hack, probably the new connection function should have returned a value
1997 * for whether to close, or should have had to close the connection itself if it
2000 * But, this works OK as long as the new connection function doesn't do anything
2001 * crazy like keep the connection around without ref'ing it.
2003 * We have to lock the connection across refcount check and close in case
2004 * the new connection function spawns a thread that closes and unrefs.
2005 * In that case, if the app thread
2006 * closes and unrefs first, we'll harmlessly close again; if the app thread
2007 * still has the ref, we'll close and then the app will close harmlessly.
2008 * If the app unrefs without closing, the app is broken since if the
2009 * app refs from the new connection function it is supposed to also close.
2011 * If we didn't atomically check the refcount and close with the lock held
2012 * though, we could screw this up.
2014 * @param connection the connection
2017 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
2019 CONNECTION_LOCK (connection);
2021 _dbus_assert (connection->refcount.value > 0);
2023 if (connection->refcount.value == 1)
2024 _dbus_connection_close_possibly_shared_and_unlock (connection);
2026 CONNECTION_UNLOCK (connection);
2031 * When a function that blocks has been called with a timeout, and we
2032 * run out of memory, the time to wait for memory is based on the
2033 * timeout. If the caller was willing to block a long time we wait a
2034 * relatively long time for memory, if they were only willing to block
2035 * briefly then we retry for memory at a rapid rate.
2037 * @timeout_milliseconds the timeout requested for blocking
2040 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2042 if (timeout_milliseconds == -1)
2043 _dbus_sleep_milliseconds (1000);
2044 else if (timeout_milliseconds < 100)
2045 ; /* just busy loop */
2046 else if (timeout_milliseconds <= 1000)
2047 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2049 _dbus_sleep_milliseconds (1000);
2052 static DBusMessage *
2053 generate_local_error_message (dbus_uint32_t serial,
2057 DBusMessage *message;
2058 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
2062 if (!dbus_message_set_error_name (message, error_name))
2064 dbus_message_unref (message);
2069 dbus_message_set_no_reply (message, TRUE);
2071 if (!dbus_message_set_reply_serial (message,
2074 dbus_message_unref (message);
2079 if (error_msg != NULL)
2081 DBusMessageIter iter;
2083 dbus_message_iter_init_append (message, &iter);
2084 if (!dbus_message_iter_append_basic (&iter,
2088 dbus_message_unref (message);
2099 /* This is slightly strange since we can pop a message here without
2100 * the dispatch lock.
2103 check_for_reply_unlocked (DBusConnection *connection,
2104 dbus_uint32_t client_serial)
2108 HAVE_LOCK_CHECK (connection);
2110 link = _dbus_list_get_first_link (&connection->incoming_messages);
2112 while (link != NULL)
2114 DBusMessage *reply = link->data;
2116 if (dbus_message_get_reply_serial (reply) == client_serial)
2118 _dbus_list_remove_link (&connection->incoming_messages, link);
2119 connection->n_incoming -= 1;
2122 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2129 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2131 /* We can't iterate over the hash in the normal way since we'll be
2132 * dropping the lock for each item. So we restart the
2133 * iter each time as we drain the hash table.
2136 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2138 DBusPendingCall *pending;
2141 _dbus_hash_iter_init (connection->pending_replies, &iter);
2142 _dbus_hash_iter_next (&iter);
2144 pending = _dbus_hash_iter_get_value (&iter);
2145 _dbus_pending_call_ref_unlocked (pending);
2147 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2149 _dbus_connection_remove_timeout_unlocked (connection,
2150 _dbus_pending_call_get_timeout_unlocked (pending));
2151 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2152 _dbus_hash_iter_remove_entry (&iter);
2154 _dbus_pending_call_unref_and_unlock (pending);
2155 CONNECTION_LOCK (connection);
2157 HAVE_LOCK_CHECK (connection);
2161 complete_pending_call_and_unlock (DBusConnection *connection,
2162 DBusPendingCall *pending,
2163 DBusMessage *message)
2165 _dbus_pending_call_set_reply_unlocked (pending, message);
2166 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2167 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2169 /* Must be called unlocked since it invokes app callback */
2170 _dbus_pending_call_complete (pending);
2171 dbus_pending_call_unref (pending);
2175 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2176 DBusPendingCall *pending)
2179 DBusDispatchStatus status;
2181 reply = check_for_reply_unlocked (connection,
2182 _dbus_pending_call_get_reply_serial_unlocked (pending));
2185 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2187 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2189 complete_pending_call_and_unlock (connection, pending, reply);
2190 dbus_message_unref (reply);
2192 CONNECTION_LOCK (connection);
2193 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2194 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2195 dbus_pending_call_unref (pending);
2204 * Blocks until a pending call times out or gets a reply.
2206 * Does not re-enter the main loop or run filter/path-registered
2207 * callbacks. The reply to the message will not be seen by
2210 * Returns immediately if pending call already got a reply.
2212 * @todo could use performance improvements (it keeps scanning
2213 * the whole message queue for example)
2215 * @param pending the pending call we block for a reply on
2218 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2220 long start_tv_sec, start_tv_usec;
2221 long end_tv_sec, end_tv_usec;
2222 long tv_sec, tv_usec;
2223 DBusDispatchStatus status;
2224 DBusConnection *connection;
2225 dbus_uint32_t client_serial;
2226 int timeout_milliseconds;
2228 _dbus_assert (pending != NULL);
2230 if (dbus_pending_call_get_completed (pending))
2233 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2235 connection = _dbus_pending_call_get_connection_and_lock (pending);
2237 /* Flush message queue - note, can affect dispatch status */
2238 _dbus_connection_flush_unlocked (connection);
2240 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2242 /* note that timeout_milliseconds is limited to a smallish value
2243 * in _dbus_pending_call_new() so overflows aren't possible
2246 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending));
2248 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2249 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2250 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2251 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2252 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2254 _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",
2255 timeout_milliseconds,
2257 start_tv_sec, start_tv_usec,
2258 end_tv_sec, end_tv_usec);
2260 /* check to see if we already got the data off the socket */
2261 /* from another blocked pending call */
2262 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2265 /* Now we wait... */
2266 /* always block at least once as we know we don't have the reply yet */
2267 _dbus_connection_do_iteration_unlocked (connection,
2268 DBUS_ITERATION_DO_READING |
2269 DBUS_ITERATION_BLOCK,
2270 timeout_milliseconds);
2274 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2276 HAVE_LOCK_CHECK (connection);
2278 /* queue messages and get status */
2280 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2282 /* the get_completed() is in case a dispatch() while we were blocking
2283 * got the reply instead of us.
2285 if (_dbus_pending_call_get_completed_unlocked (pending))
2287 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2288 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2289 dbus_pending_call_unref (pending);
2293 if (status == DBUS_DISPATCH_DATA_REMAINS) {
2294 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2298 _dbus_get_current_time (&tv_sec, &tv_usec);
2300 if (!_dbus_connection_get_is_connected_unlocked (connection))
2302 DBusMessage *error_msg;
2304 error_msg = generate_local_error_message (client_serial,
2305 DBUS_ERROR_DISCONNECTED,
2306 "Connection was disconnected before a reply was received");
2308 /* on OOM error_msg is set to NULL */
2309 complete_pending_call_and_unlock (connection, pending, error_msg);
2310 dbus_pending_call_unref (pending);
2313 else if (tv_sec < start_tv_sec)
2314 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2315 else if (connection->disconnect_message_link == NULL)
2316 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2317 else if (tv_sec < end_tv_sec ||
2318 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2320 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2321 (end_tv_usec - tv_usec) / 1000;
2322 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2323 _dbus_assert (timeout_milliseconds >= 0);
2325 if (status == DBUS_DISPATCH_NEED_MEMORY)
2327 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2328 * we may already have a reply in the buffer and just can't process
2331 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2333 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2337 /* block again, we don't have the reply buffered yet. */
2338 _dbus_connection_do_iteration_unlocked (connection,
2339 DBUS_ITERATION_DO_READING |
2340 DBUS_ITERATION_BLOCK,
2341 timeout_milliseconds);
2344 goto recheck_status;
2347 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2348 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2350 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
2352 /* unlock and call user code */
2353 complete_pending_call_and_unlock (connection, pending, NULL);
2355 /* update user code on dispatch status */
2356 CONNECTION_LOCK (connection);
2357 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2358 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2359 dbus_pending_call_unref (pending);
2365 * @addtogroup DBusConnection
2371 * Gets a connection to a remote address. If a connection to the given
2372 * address already exists, returns the existing connection with its
2373 * reference count incremented. Otherwise, returns a new connection
2374 * and saves the new connection for possible re-use if a future call
2375 * to dbus_connection_open() asks to connect to the same server.
2377 * Use dbus_connection_open_private() to get a dedicated connection
2378 * not shared with other callers of dbus_connection_open().
2380 * If the open fails, the function returns #NULL, and provides a
2381 * reason for the failure in the error parameter. Pass #NULL for the
2382 * error parameter if you aren't interested in the reason for
2385 * Because this connection is shared, no user of the connection
2386 * may call dbus_connection_close(). However, when you are done with the
2387 * connection you should call dbus_connection_unref().
2389 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2390 * unless you have good reason; connections are expensive enough
2391 * that it's wasteful to create lots of connections to the same
2394 * @param address the address.
2395 * @param error address where an error can be returned.
2396 * @returns new connection, or #NULL on failure.
2399 dbus_connection_open (const char *address,
2402 DBusConnection *connection;
2404 _dbus_return_val_if_fail (address != NULL, NULL);
2405 _dbus_return_val_if_error_is_set (error, NULL);
2407 connection = _dbus_connection_open_internal (address,
2415 * Opens a new, dedicated connection to a remote address. Unlike
2416 * dbus_connection_open(), always creates a new connection.
2417 * This connection will not be saved or recycled by libdbus.
2419 * If the open fails, the function returns #NULL, and provides a
2420 * reason for the failure in the error parameter. Pass #NULL for the
2421 * error parameter if you aren't interested in the reason for
2424 * When you are done with this connection, you must
2425 * dbus_connection_close() to disconnect it,
2426 * and dbus_connection_unref() to free the connection object.
2428 * (The dbus_connection_close() can be skipped if the
2429 * connection is already known to be disconnected, for example
2430 * if you are inside a handler for the Disconnected signal.)
2432 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2433 * unless you have good reason; connections are expensive enough
2434 * that it's wasteful to create lots of connections to the same
2437 * @param address the address.
2438 * @param error address where an error can be returned.
2439 * @returns new connection, or #NULL on failure.
2442 dbus_connection_open_private (const char *address,
2445 DBusConnection *connection;
2447 _dbus_return_val_if_fail (address != NULL, NULL);
2448 _dbus_return_val_if_error_is_set (error, NULL);
2450 connection = _dbus_connection_open_internal (address,
2458 * Increments the reference count of a DBusConnection.
2460 * @param connection the connection.
2461 * @returns the connection.
2464 dbus_connection_ref (DBusConnection *connection)
2466 _dbus_return_val_if_fail (connection != NULL, NULL);
2467 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
2469 /* The connection lock is better than the global
2470 * lock in the atomic increment fallback
2473 #ifdef DBUS_HAVE_ATOMIC_INT
2474 _dbus_atomic_inc (&connection->refcount);
2476 CONNECTION_LOCK (connection);
2477 _dbus_assert (connection->refcount.value > 0);
2479 connection->refcount.value += 1;
2480 CONNECTION_UNLOCK (connection);
2487 free_outgoing_message (void *element,
2490 DBusMessage *message = element;
2491 DBusConnection *connection = data;
2493 _dbus_message_remove_size_counter (message,
2494 connection->outgoing_counter,
2496 dbus_message_unref (message);
2499 /* This is run without the mutex held, but after the last reference
2500 * to the connection has been dropped we should have no thread-related
2504 _dbus_connection_last_unref (DBusConnection *connection)
2508 _dbus_verbose ("Finalizing connection %p\n", connection);
2510 _dbus_assert (connection->refcount.value == 0);
2512 /* You have to disconnect the connection before unref:ing it. Otherwise
2513 * you won't get the disconnected message.
2515 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
2516 _dbus_assert (connection->server_guid == NULL);
2518 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
2519 _dbus_object_tree_free_all_unlocked (connection->objects);
2521 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
2522 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
2523 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
2525 _dbus_watch_list_free (connection->watches);
2526 connection->watches = NULL;
2528 _dbus_timeout_list_free (connection->timeouts);
2529 connection->timeouts = NULL;
2531 _dbus_data_slot_list_free (&connection->slot_list);
2533 link = _dbus_list_get_first_link (&connection->filter_list);
2534 while (link != NULL)
2536 DBusMessageFilter *filter = link->data;
2537 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
2539 filter->function = NULL;
2540 _dbus_message_filter_unref (filter); /* calls app callback */
2545 _dbus_list_clear (&connection->filter_list);
2547 /* ---- Done with stuff that invokes application callbacks */
2549 _dbus_object_tree_unref (connection->objects);
2551 _dbus_hash_table_unref (connection->pending_replies);
2552 connection->pending_replies = NULL;
2554 _dbus_list_clear (&connection->filter_list);
2556 _dbus_list_foreach (&connection->outgoing_messages,
2557 free_outgoing_message,
2559 _dbus_list_clear (&connection->outgoing_messages);
2561 _dbus_list_foreach (&connection->incoming_messages,
2562 (DBusForeachFunction) dbus_message_unref,
2564 _dbus_list_clear (&connection->incoming_messages);
2566 _dbus_counter_unref (connection->outgoing_counter);
2568 _dbus_transport_unref (connection->transport);
2570 if (connection->disconnect_message_link)
2572 DBusMessage *message = connection->disconnect_message_link->data;
2573 dbus_message_unref (message);
2574 _dbus_list_free_link (connection->disconnect_message_link);
2577 _dbus_list_clear (&connection->link_cache);
2579 _dbus_condvar_free_at_location (&connection->dispatch_cond);
2580 _dbus_condvar_free_at_location (&connection->io_path_cond);
2582 _dbus_mutex_free_at_location (&connection->io_path_mutex);
2583 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
2585 _dbus_mutex_free_at_location (&connection->mutex);
2587 dbus_free (connection);
2591 * Decrements the reference count of a DBusConnection, and finalizes
2592 * it if the count reaches zero.
2594 * Note: it is a bug to drop the last reference to a connection that
2595 * is still connected.
2597 * For shared connections, libdbus will own a reference
2598 * as long as the connection is connected, so you can know that either
2599 * you don't have the last reference, or it's OK to drop the last reference.
2600 * Most connections are shared. dbus_connection_open() and dbus_bus_get()
2601 * return shared connections.
2603 * For private connections, the creator of the connection must arrange for
2604 * dbus_connection_close() to be called prior to dropping the last reference.
2605 * Private connections come from dbus_connection_open_private() or dbus_bus_get_private().
2607 * @param connection the connection.
2610 dbus_connection_unref (DBusConnection *connection)
2612 dbus_bool_t last_unref;
2614 _dbus_return_if_fail (connection != NULL);
2615 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2617 /* The connection lock is better than the global
2618 * lock in the atomic increment fallback
2621 #ifdef DBUS_HAVE_ATOMIC_INT
2622 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
2624 CONNECTION_LOCK (connection);
2626 _dbus_assert (connection->refcount.value > 0);
2628 connection->refcount.value -= 1;
2629 last_unref = (connection->refcount.value == 0);
2632 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
2635 CONNECTION_UNLOCK (connection);
2640 #ifndef DBUS_DISABLE_CHECKS
2641 if (_dbus_transport_get_is_connected (connection->transport))
2643 _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",
2644 connection->shareable ?
2645 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" :
2646 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
2650 _dbus_connection_last_unref (connection);
2655 * Note that the transport can disconnect itself (other end drops us)
2656 * and in that case this function never runs. So this function must
2657 * not do anything more than disconnect the transport and update the
2660 * If the transport self-disconnects, then we assume someone will
2661 * dispatch the connection to cause the dispatch status update.
2664 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2666 DBusDispatchStatus status;
2668 HAVE_LOCK_CHECK (connection);
2670 _dbus_verbose ("Disconnecting %p\n", connection);
2672 /* We need to ref because update_dispatch_status_and_unlock will unref
2673 * the connection if it was shared and libdbus was the only remaining
2676 _dbus_connection_ref_unlocked (connection);
2678 _dbus_transport_disconnect (connection->transport);
2680 /* This has the side effect of queuing the disconnect message link
2681 * (unless we don't have enough memory, possibly, so don't assert it).
2682 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
2683 * should never again return the newly-disconnected connection.
2685 * However, we only unref the shared connection and exit_on_disconnect when
2686 * the disconnect message reaches the head of the message queue,
2687 * NOT when it's first queued.
2689 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2691 /* This calls out to user code */
2692 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2694 /* Could also call out to user code */
2695 dbus_connection_unref (connection);
2699 * Closes a private connection, so no further data can be sent or received.
2700 * This disconnects the transport (such as a socket) underlying the
2703 * Attempts to send messages after closing a connection are safe, but will result in
2704 * error replies generated locally in libdbus.
2706 * This function does not affect the connection's reference count. It's
2707 * safe to close a connection more than once; all calls after the
2708 * first do nothing. It's impossible to "reopen" a connection, a
2709 * new connection must be created. This function may result in a call
2710 * to the DBusDispatchStatusFunction set with
2711 * dbus_connection_set_dispatch_status_function(), as the disconnect
2712 * message it generates needs to be dispatched.
2714 * If a connection is dropped by the remote application, it will
2717 * You must close a connection prior to releasing the last reference to
2718 * the connection. If you dbus_connection_unref() for the last time
2719 * without closing the connection, the results are undefined; it
2720 * is a bug in your program and libdbus will try to print a warning.
2722 * You may not close a shared connection. Connections created with
2723 * dbus_connection_open() or dbus_bus_get() are shared.
2724 * These connections are owned by libdbus, and applications should
2725 * only unref them, never close them. Applications can know it is
2726 * safe to unref these connections because libdbus will be holding a
2727 * reference as long as the connection is open. Thus, either the
2728 * connection is closed and it is OK to drop the last reference,
2729 * or the connection is open and the app knows it does not have the
2732 * Connections created with dbus_connection_open_private() or
2733 * dbus_bus_get_private() are not kept track of or referenced by
2734 * libdbus. The creator of these connections is responsible for
2735 * calling dbus_connection_close() prior to releasing the last
2736 * reference, if the connection is not already disconnected.
2738 * @param connection the private (unshared) connection to close
2741 dbus_connection_close (DBusConnection *connection)
2743 _dbus_return_if_fail (connection != NULL);
2744 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2746 CONNECTION_LOCK (connection);
2748 #ifndef DBUS_DISABLE_CHECKS
2749 if (connection->shareable)
2751 CONNECTION_UNLOCK (connection);
2753 _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
2758 _dbus_connection_close_possibly_shared_and_unlock (connection);
2762 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2764 HAVE_LOCK_CHECK (connection);
2765 return _dbus_transport_get_is_connected (connection->transport);
2769 * Gets whether the connection is currently open. A connection may
2770 * become disconnected when the remote application closes its end, or
2771 * exits; a connection may also be disconnected with
2772 * dbus_connection_close().
2774 * There are not separate states for "closed" and "disconnected," the two
2775 * terms are synonymous. This function should really be called
2776 * get_is_open() but for historical reasons is not.
2778 * @param connection the connection.
2779 * @returns #TRUE if the connection is still alive.
2782 dbus_connection_get_is_connected (DBusConnection *connection)
2786 _dbus_return_val_if_fail (connection != NULL, FALSE);
2788 CONNECTION_LOCK (connection);
2789 res = _dbus_connection_get_is_connected_unlocked (connection);
2790 CONNECTION_UNLOCK (connection);
2796 * Gets whether the connection was authenticated. (Note that
2797 * if the connection was authenticated then disconnected,
2798 * this function still returns #TRUE)
2800 * @param connection the connection
2801 * @returns #TRUE if the connection was ever authenticated
2804 dbus_connection_get_is_authenticated (DBusConnection *connection)
2808 _dbus_return_val_if_fail (connection != NULL, FALSE);
2810 CONNECTION_LOCK (connection);
2811 res = _dbus_transport_get_is_authenticated (connection->transport);
2812 CONNECTION_UNLOCK (connection);
2818 * Gets whether the connection is not authenticated as a specific
2819 * user. If the connection is not authenticated, this function
2820 * returns #TRUE, and if it is authenticated but as an anonymous user,
2821 * it returns #TRUE. If it is authenticated as a specific user, then
2822 * this returns #FALSE. (Note that if the connection was authenticated
2823 * as anonymous then disconnected, this function still returns #TRUE.)
2825 * If the connection is not anonymous, you can use
2826 * dbus_connection_get_unix_user() and
2827 * dbus_connection_get_windows_user() to see who it's authorized as.
2829 * If you want to prevent non-anonymous authorization, use
2830 * dbus_server_set_auth_mechanisms() to remove the mechanisms that
2831 * allow proving user identity (i.e. only allow the ANONYMOUS
2834 * @param connection the connection
2835 * @returns #TRUE if not authenticated or authenticated as anonymous
2838 dbus_connection_get_is_anonymous (DBusConnection *connection)
2842 _dbus_return_val_if_fail (connection != NULL, FALSE);
2844 CONNECTION_LOCK (connection);
2845 res = _dbus_transport_get_is_anonymous (connection->transport);
2846 CONNECTION_UNLOCK (connection);
2852 * Gets the ID of the server address we are authenticated to, if this
2853 * connection is on the client side. If the connection is on the
2854 * server side, this will always return #NULL - use dbus_server_get_id()
2855 * to get the ID of your own server, if you are the server side.
2857 * If a client-side connection is not authenticated yet, the ID may be
2858 * available if it was included in the server address, but may not be
2859 * available. The only way to be sure the server ID is available
2860 * is to wait for authentication to complete.
2862 * In general, each mode of connecting to a given server will have
2863 * its own ID. So for example, if the session bus daemon is listening
2864 * on UNIX domain sockets and on TCP, then each of those modalities
2865 * will have its own server ID.
2867 * If you want an ID that identifies an entire session bus, look at
2868 * dbus_bus_get_id() instead (which is just a convenience wrapper
2869 * around the org.freedesktop.DBus.GetId method invoked on the bus).
2871 * You can also get a machine ID; see dbus_get_local_machine_id() to
2872 * get the machine you are on. There isn't a convenience wrapper, but
2873 * you can invoke org.freedesktop.DBus.Peer.GetMachineId on any peer
2874 * to get the machine ID on the other end.
2876 * The D-Bus specification describes the server ID and other IDs in a
2879 * @param connection the connection
2880 * @returns the server ID or #NULL if no memory or the connection is server-side
2883 dbus_connection_get_server_id (DBusConnection *connection)
2887 _dbus_return_val_if_fail (connection != NULL, FALSE);
2889 CONNECTION_LOCK (connection);
2890 id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
2891 CONNECTION_UNLOCK (connection);
2897 * Set whether _exit() should be called when the connection receives a
2898 * disconnect signal. The call to _exit() comes after any handlers for
2899 * the disconnect signal run; handlers can cancel the exit by calling
2902 * By default, exit_on_disconnect is #FALSE; but for message bus
2903 * connections returned from dbus_bus_get() it will be toggled on
2906 * @param connection the connection
2907 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
2910 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
2911 dbus_bool_t exit_on_disconnect)
2913 _dbus_return_if_fail (connection != NULL);
2915 CONNECTION_LOCK (connection);
2916 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
2917 CONNECTION_UNLOCK (connection);
2921 * Preallocates resources needed to send a message, allowing the message
2922 * to be sent without the possibility of memory allocation failure.
2923 * Allows apps to create a future guarantee that they can send
2924 * a message regardless of memory shortages.
2926 * @param connection the connection we're preallocating for.
2927 * @returns the preallocated resources, or #NULL
2929 DBusPreallocatedSend*
2930 dbus_connection_preallocate_send (DBusConnection *connection)
2932 DBusPreallocatedSend *preallocated;
2934 _dbus_return_val_if_fail (connection != NULL, NULL);
2936 CONNECTION_LOCK (connection);
2939 _dbus_connection_preallocate_send_unlocked (connection);
2941 CONNECTION_UNLOCK (connection);
2943 return preallocated;
2947 * Frees preallocated message-sending resources from
2948 * dbus_connection_preallocate_send(). Should only
2949 * be called if the preallocated resources are not used
2950 * to send a message.
2952 * @param connection the connection
2953 * @param preallocated the resources
2956 dbus_connection_free_preallocated_send (DBusConnection *connection,
2957 DBusPreallocatedSend *preallocated)
2959 _dbus_return_if_fail (connection != NULL);
2960 _dbus_return_if_fail (preallocated != NULL);
2961 _dbus_return_if_fail (connection == preallocated->connection);
2963 _dbus_list_free_link (preallocated->queue_link);
2964 _dbus_counter_unref (preallocated->counter_link->data);
2965 _dbus_list_free_link (preallocated->counter_link);
2966 dbus_free (preallocated);
2970 * Sends a message using preallocated resources. This function cannot fail.
2971 * It works identically to dbus_connection_send() in other respects.
2972 * Preallocated resources comes from dbus_connection_preallocate_send().
2973 * This function "consumes" the preallocated resources, they need not
2974 * be freed separately.
2976 * @param connection the connection
2977 * @param preallocated the preallocated resources
2978 * @param message the message to send
2979 * @param client_serial return location for client serial assigned to the message
2982 dbus_connection_send_preallocated (DBusConnection *connection,
2983 DBusPreallocatedSend *preallocated,
2984 DBusMessage *message,
2985 dbus_uint32_t *client_serial)
2987 _dbus_return_if_fail (connection != NULL);
2988 _dbus_return_if_fail (preallocated != NULL);
2989 _dbus_return_if_fail (message != NULL);
2990 _dbus_return_if_fail (preallocated->connection == connection);
2991 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
2992 dbus_message_get_member (message) != NULL);
2993 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
2994 (dbus_message_get_interface (message) != NULL &&
2995 dbus_message_get_member (message) != NULL));
2997 CONNECTION_LOCK (connection);
2998 _dbus_connection_send_preallocated_and_unlock (connection,
3000 message, client_serial);
3004 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
3005 DBusMessage *message,
3006 dbus_uint32_t *client_serial)
3008 DBusPreallocatedSend *preallocated;
3010 _dbus_assert (connection != NULL);
3011 _dbus_assert (message != NULL);
3013 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3014 if (preallocated == NULL)
3017 _dbus_connection_send_preallocated_unlocked_no_update (connection,
3025 * Adds a message to the outgoing message queue. Does not block to
3026 * write the message to the network; that happens asynchronously. To
3027 * force the message to be written, call dbus_connection_flush().
3028 * Because this only queues the message, the only reason it can
3029 * fail is lack of memory. Even if the connection is disconnected,
3030 * no error will be returned.
3032 * If the function fails due to lack of memory, it returns #FALSE.
3033 * The function will never fail for other reasons; even if the
3034 * connection is disconnected, you can queue an outgoing message,
3035 * though obviously it won't be sent.
3037 * The message serial is used by the remote application to send a
3038 * reply; see dbus_message_get_serial() or the D-Bus specification.
3040 * @param connection the connection.
3041 * @param message the message to write.
3042 * @param serial return location for message serial, or #NULL if you don't care
3043 * @returns #TRUE on success.
3046 dbus_connection_send (DBusConnection *connection,
3047 DBusMessage *message,
3048 dbus_uint32_t *serial)
3050 _dbus_return_val_if_fail (connection != NULL, FALSE);
3051 _dbus_return_val_if_fail (message != NULL, FALSE);
3053 CONNECTION_LOCK (connection);
3055 return _dbus_connection_send_and_unlock (connection,
3061 reply_handler_timeout (void *data)
3063 DBusConnection *connection;
3064 DBusDispatchStatus status;
3065 DBusPendingCall *pending = data;
3067 connection = _dbus_pending_call_get_connection_and_lock (pending);
3069 _dbus_pending_call_queue_timeout_error_unlocked (pending,
3071 _dbus_connection_remove_timeout_unlocked (connection,
3072 _dbus_pending_call_get_timeout_unlocked (pending));
3073 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
3075 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3076 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3078 /* Unlocks, and calls out to user code */
3079 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3085 * Queues a message to send, as with dbus_connection_send(),
3086 * but also returns a #DBusPendingCall used to receive a reply to the
3087 * message. If no reply is received in the given timeout_milliseconds,
3088 * this function expires the pending reply and generates a synthetic
3089 * error reply (generated in-process, not by the remote application)
3090 * indicating that a timeout occurred.
3092 * A #DBusPendingCall will see a reply message before any filters or
3093 * registered object path handlers. See dbus_connection_dispatch() for
3094 * details on when handlers are run.
3096 * A #DBusPendingCall will always see exactly one reply message,
3097 * unless it's cancelled with dbus_pending_call_cancel().
3099 * If #NULL is passed for the pending_return, the #DBusPendingCall
3100 * will still be generated internally, and used to track
3101 * the message reply timeout. This means a timeout error will
3102 * occur if no reply arrives, unlike with dbus_connection_send().
3104 * If -1 is passed for the timeout, a sane default timeout is used. -1
3105 * is typically the best value for the timeout for this reason, unless
3106 * you want a very short or very long timeout. There is no way to
3107 * avoid a timeout entirely, other than passing INT_MAX for the
3108 * timeout to mean "very long timeout." libdbus clamps an INT_MAX
3109 * timeout down to a few hours timeout though.
3111 * @warning if the connection is disconnected, the #DBusPendingCall
3112 * will be set to #NULL, so be careful with this.
3114 * @param connection the connection
3115 * @param message the message to send
3116 * @param pending_return return location for a #DBusPendingCall object, or #NULL if connection is disconnected
3117 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3118 * @returns #FALSE if no memory, #TRUE otherwise.
3122 dbus_connection_send_with_reply (DBusConnection *connection,
3123 DBusMessage *message,
3124 DBusPendingCall **pending_return,
3125 int timeout_milliseconds)
3127 DBusPendingCall *pending;
3128 dbus_int32_t serial = -1;
3129 DBusDispatchStatus status;
3131 _dbus_return_val_if_fail (connection != NULL, FALSE);
3132 _dbus_return_val_if_fail (message != NULL, FALSE);
3133 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3136 *pending_return = NULL;
3138 CONNECTION_LOCK (connection);
3140 if (!_dbus_connection_get_is_connected_unlocked (connection))
3142 CONNECTION_UNLOCK (connection);
3144 *pending_return = NULL;
3149 pending = _dbus_pending_call_new_unlocked (connection,
3150 timeout_milliseconds,
3151 reply_handler_timeout);
3153 if (pending == NULL)
3155 CONNECTION_UNLOCK (connection);
3159 /* Assign a serial to the message */
3160 serial = dbus_message_get_serial (message);
3163 serial = _dbus_connection_get_next_client_serial (connection);
3164 _dbus_message_set_serial (message, serial);
3167 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
3170 /* Insert the serial in the pending replies hash;
3171 * hash takes a refcount on DBusPendingCall.
3172 * Also, add the timeout.
3174 if (!_dbus_connection_attach_pending_call_unlocked (connection,
3178 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
3180 _dbus_connection_detach_pending_call_and_unlock (connection,
3182 goto error_unlocked;
3186 *pending_return = pending; /* hand off refcount */
3189 _dbus_connection_detach_pending_call_unlocked (connection, pending);
3190 /* we still have a ref to the pending call in this case, we unref
3191 * after unlocking, below
3195 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3197 /* this calls out to user code */
3198 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3200 if (pending_return == NULL)
3201 dbus_pending_call_unref (pending);
3206 CONNECTION_UNLOCK (connection);
3208 dbus_pending_call_unref (pending);
3213 * Sends a message and blocks a certain time period while waiting for
3214 * a reply. This function does not reenter the main loop,
3215 * i.e. messages other than the reply are queued up but not
3216 * processed. This function is used to invoke method calls on a
3219 * If a normal reply is received, it is returned, and removed from the
3220 * incoming message queue. If it is not received, #NULL is returned
3221 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
3222 * received, it is converted to a #DBusError and returned as an error,
3223 * then the reply message is deleted and #NULL is returned. If
3224 * something else goes wrong, result is set to whatever is
3225 * appropriate, such as #DBUS_ERROR_NO_MEMORY or
3226 * #DBUS_ERROR_DISCONNECTED.
3228 * @warning While this function blocks the calling thread will not be
3229 * processing the incoming message queue. This means you can end up
3230 * deadlocked if the application you're talking to needs you to reply
3231 * to a method. To solve this, either avoid the situation, block in a
3232 * separate thread from the main connection-dispatching thread, or use
3233 * dbus_pending_call_set_notify() to avoid blocking.
3235 * @param connection the connection
3236 * @param message the message to send
3237 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3238 * @param error return location for error message
3239 * @returns the message that is the reply or #NULL with an error code if the
3243 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
3244 DBusMessage *message,
3245 int timeout_milliseconds,
3249 DBusPendingCall *pending;
3251 _dbus_return_val_if_fail (connection != NULL, NULL);
3252 _dbus_return_val_if_fail (message != NULL, NULL);
3253 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3254 _dbus_return_val_if_error_is_set (error, NULL);
3256 if (!dbus_connection_send_with_reply (connection, message,
3257 &pending, timeout_milliseconds))
3259 _DBUS_SET_OOM (error);
3263 if (pending == NULL)
3265 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
3269 dbus_pending_call_block (pending);
3271 reply = dbus_pending_call_steal_reply (pending);
3272 dbus_pending_call_unref (pending);
3274 /* call_complete_and_unlock() called from pending_call_block() should
3275 * always fill this in.
3277 _dbus_assert (reply != NULL);
3279 if (dbus_set_error_from_message (error, reply))
3281 dbus_message_unref (reply);
3289 * Blocks until the outgoing message queue is empty.
3290 * Assumes connection lock already held.
3292 * If you call this, you MUST call update_dispatch_status afterword...
3294 * @param connection the connection.
3297 _dbus_connection_flush_unlocked (DBusConnection *connection)
3299 /* We have to specify DBUS_ITERATION_DO_READING here because
3300 * otherwise we could have two apps deadlock if they are both doing
3301 * a flush(), and the kernel buffers fill up. This could change the
3304 DBusDispatchStatus status;
3306 HAVE_LOCK_CHECK (connection);
3308 while (connection->n_outgoing > 0 &&
3309 _dbus_connection_get_is_connected_unlocked (connection))
3311 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3312 HAVE_LOCK_CHECK (connection);
3313 _dbus_connection_do_iteration_unlocked (connection,
3314 DBUS_ITERATION_DO_READING |
3315 DBUS_ITERATION_DO_WRITING |
3316 DBUS_ITERATION_BLOCK,
3320 HAVE_LOCK_CHECK (connection);
3321 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3322 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3324 HAVE_LOCK_CHECK (connection);
3329 * Blocks until the outgoing message queue is empty.
3331 * @param connection the connection.
3334 dbus_connection_flush (DBusConnection *connection)
3336 /* We have to specify DBUS_ITERATION_DO_READING here because
3337 * otherwise we could have two apps deadlock if they are both doing
3338 * a flush(), and the kernel buffers fill up. This could change the
3341 DBusDispatchStatus status;
3343 _dbus_return_if_fail (connection != NULL);
3345 CONNECTION_LOCK (connection);
3347 status = _dbus_connection_flush_unlocked (connection);
3349 HAVE_LOCK_CHECK (connection);
3350 /* Unlocks and calls out to user code */
3351 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3353 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
3357 * This function implements dbus_connection_read_write_dispatch() and
3358 * dbus_connection_read_write() (they pass a different value for the
3359 * dispatch parameter).
3361 * @param connection the connection
3362 * @param timeout_milliseconds max time to block or -1 for infinite
3363 * @param dispatch dispatch new messages or leave them on the incoming queue
3364 * @returns #TRUE if the disconnect message has not been processed
3367 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3368 int timeout_milliseconds,
3369 dbus_bool_t dispatch)
3371 DBusDispatchStatus dstatus;
3372 dbus_bool_t no_progress_possible;
3374 dstatus = dbus_connection_get_dispatch_status (connection);
3376 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3378 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
3379 dbus_connection_dispatch (connection);
3380 CONNECTION_LOCK (connection);
3382 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3384 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
3385 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3386 CONNECTION_LOCK (connection);
3390 CONNECTION_LOCK (connection);
3391 if (_dbus_connection_get_is_connected_unlocked (connection))
3393 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3394 _dbus_connection_do_iteration_unlocked (connection,
3395 DBUS_ITERATION_DO_READING |
3396 DBUS_ITERATION_DO_WRITING |
3397 DBUS_ITERATION_BLOCK,
3398 timeout_milliseconds);
3402 HAVE_LOCK_CHECK (connection);
3403 /* If we can dispatch, we can make progress until the Disconnected message
3404 * has been processed; if we can only read/write, we can make progress
3405 * as long as the transport is open.
3408 no_progress_possible = connection->n_incoming == 0 &&
3409 connection->disconnect_message_link == NULL;
3411 no_progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
3412 CONNECTION_UNLOCK (connection);
3413 return !no_progress_possible; /* TRUE if we can make more progress */
3418 * This function is intended for use with applications that don't want
3419 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3420 * example usage would be:
3423 * while (dbus_connection_read_write_dispatch (connection, -1))
3424 * ; // empty loop body
3427 * In this usage you would normally have set up a filter function to look
3428 * at each message as it is dispatched. The loop terminates when the last
3429 * message from the connection (the disconnected signal) is processed.
3431 * If there are messages to dispatch, this function will
3432 * dbus_connection_dispatch() once, and return. If there are no
3433 * messages to dispatch, this function will block until it can read or
3434 * write, then read or write, then return.
3436 * The way to think of this function is that it either makes some sort
3437 * of progress, or it blocks. Note that, while it is blocked on I/O, it
3438 * cannot be interrupted (even by other threads), which makes this function
3439 * unsuitable for applications that do more than just react to received
3442 * The return value indicates whether the disconnect message has been
3443 * processed, NOT whether the connection is connected. This is
3444 * important because even after disconnecting, you want to process any
3445 * messages you received prior to the disconnect.
3447 * @param connection the connection
3448 * @param timeout_milliseconds max time to block or -1 for infinite
3449 * @returns #TRUE if the disconnect message has not been processed
3452 dbus_connection_read_write_dispatch (DBusConnection *connection,
3453 int timeout_milliseconds)
3455 _dbus_return_val_if_fail (connection != NULL, FALSE);
3456 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3457 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3461 * This function is intended for use with applications that don't want to
3462 * write a main loop and deal with #DBusWatch and #DBusTimeout. See also
3463 * dbus_connection_read_write_dispatch().
3465 * As long as the connection is open, this function will block until it can
3466 * read or write, then read or write, then return #TRUE.
3468 * If the connection is closed, the function returns #FALSE.
3470 * The return value indicates whether reading or writing is still
3471 * possible, i.e. whether the connection is connected.
3473 * Note that even after disconnection, messages may remain in the
3474 * incoming queue that need to be
3475 * processed. dbus_connection_read_write_dispatch() dispatches
3476 * incoming messages for you; with dbus_connection_read_write() you
3477 * have to arrange to drain the incoming queue yourself.
3479 * @param connection the connection
3480 * @param timeout_milliseconds max time to block or -1 for infinite
3481 * @returns #TRUE if still connected
3484 dbus_connection_read_write (DBusConnection *connection,
3485 int timeout_milliseconds)
3487 _dbus_return_val_if_fail (connection != NULL, FALSE);
3488 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3489 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3492 /* We need to call this anytime we pop the head of the queue, and then
3493 * update_dispatch_status_and_unlock needs to be called afterward
3494 * which will "process" the disconnected message and set
3495 * disconnected_message_processed.
3498 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3499 DBusMessage *head_of_queue)
3501 HAVE_LOCK_CHECK (connection);
3503 /* checking that the link is NULL is an optimization to avoid the is_signal call */
3504 if (connection->disconnect_message_link == NULL &&
3505 dbus_message_is_signal (head_of_queue,
3506 DBUS_INTERFACE_LOCAL,
3509 connection->disconnected_message_arrived = TRUE;
3514 * Returns the first-received message from the incoming message queue,
3515 * leaving it in the queue. If the queue is empty, returns #NULL.
3517 * The caller does not own a reference to the returned message, and
3518 * must either return it using dbus_connection_return_message() or
3519 * keep it after calling dbus_connection_steal_borrowed_message(). No
3520 * one can get at the message while its borrowed, so return it as
3521 * quickly as possible and don't keep a reference to it after
3522 * returning it. If you need to keep the message, make a copy of it.
3524 * dbus_connection_dispatch() will block if called while a borrowed
3525 * message is outstanding; only one piece of code can be playing with
3526 * the incoming queue at a time. This function will block if called
3527 * during a dbus_connection_dispatch().
3529 * @param connection the connection.
3530 * @returns next message in the incoming queue.
3533 dbus_connection_borrow_message (DBusConnection *connection)
3535 DBusDispatchStatus status;
3536 DBusMessage *message;
3538 _dbus_return_val_if_fail (connection != NULL, NULL);
3540 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3542 /* this is called for the side effect that it queues
3543 * up any messages from the transport
3545 status = dbus_connection_get_dispatch_status (connection);
3546 if (status != DBUS_DISPATCH_DATA_REMAINS)
3549 CONNECTION_LOCK (connection);
3551 _dbus_connection_acquire_dispatch (connection);
3553 /* While a message is outstanding, the dispatch lock is held */
3554 _dbus_assert (connection->message_borrowed == NULL);
3556 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3558 message = connection->message_borrowed;
3560 check_disconnected_message_arrived_unlocked (connection, message);
3562 /* Note that we KEEP the dispatch lock until the message is returned */
3563 if (message == NULL)
3564 _dbus_connection_release_dispatch (connection);
3566 CONNECTION_UNLOCK (connection);
3568 /* We don't update dispatch status until it's returned or stolen */
3574 * Used to return a message after peeking at it using
3575 * dbus_connection_borrow_message(). Only called if
3576 * message from dbus_connection_borrow_message() was non-#NULL.
3578 * @param connection the connection
3579 * @param message the message from dbus_connection_borrow_message()
3582 dbus_connection_return_message (DBusConnection *connection,
3583 DBusMessage *message)
3585 DBusDispatchStatus status;
3587 _dbus_return_if_fail (connection != NULL);
3588 _dbus_return_if_fail (message != NULL);
3589 _dbus_return_if_fail (message == connection->message_borrowed);
3590 _dbus_return_if_fail (connection->dispatch_acquired);
3592 CONNECTION_LOCK (connection);
3594 _dbus_assert (message == connection->message_borrowed);
3596 connection->message_borrowed = NULL;
3598 _dbus_connection_release_dispatch (connection);
3600 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3601 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3605 * Used to keep a message after peeking at it using
3606 * dbus_connection_borrow_message(). Before using this function, see
3607 * the caveats/warnings in the documentation for
3608 * dbus_connection_pop_message().
3610 * @param connection the connection
3611 * @param message the message from dbus_connection_borrow_message()
3614 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3615 DBusMessage *message)
3617 DBusMessage *pop_message;
3618 DBusDispatchStatus status;
3620 _dbus_return_if_fail (connection != NULL);
3621 _dbus_return_if_fail (message != NULL);
3622 _dbus_return_if_fail (message == connection->message_borrowed);
3623 _dbus_return_if_fail (connection->dispatch_acquired);
3625 CONNECTION_LOCK (connection);
3627 _dbus_assert (message == connection->message_borrowed);
3629 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3630 _dbus_assert (message == pop_message);
3632 connection->n_incoming -= 1;
3634 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3635 message, connection->n_incoming);
3637 connection->message_borrowed = NULL;
3639 _dbus_connection_release_dispatch (connection);
3641 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3642 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3645 /* See dbus_connection_pop_message, but requires the caller to own
3646 * the lock before calling. May drop the lock while running.
3649 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3651 HAVE_LOCK_CHECK (connection);
3653 _dbus_assert (connection->message_borrowed == NULL);
3655 if (connection->n_incoming > 0)
3659 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3660 connection->n_incoming -= 1;
3662 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3664 dbus_message_get_type (link->data),
3665 dbus_message_get_path (link->data) ?
3666 dbus_message_get_path (link->data) :
3668 dbus_message_get_interface (link->data) ?
3669 dbus_message_get_interface (link->data) :
3671 dbus_message_get_member (link->data) ?
3672 dbus_message_get_member (link->data) :
3674 dbus_message_get_signature (link->data),
3675 connection, connection->n_incoming);
3677 check_disconnected_message_arrived_unlocked (connection, link->data);
3685 /* See dbus_connection_pop_message, but requires the caller to own
3686 * the lock before calling. May drop the lock while running.
3689 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3693 HAVE_LOCK_CHECK (connection);
3695 link = _dbus_connection_pop_message_link_unlocked (connection);
3699 DBusMessage *message;
3701 message = link->data;
3703 _dbus_list_free_link (link);
3712 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3713 DBusList *message_link)
3715 HAVE_LOCK_CHECK (connection);
3717 _dbus_assert (message_link != NULL);
3718 /* You can't borrow a message while a link is outstanding */
3719 _dbus_assert (connection->message_borrowed == NULL);
3720 /* We had to have the dispatch lock across the pop/putback */
3721 _dbus_assert (connection->dispatch_acquired);
3723 _dbus_list_prepend_link (&connection->incoming_messages,
3725 connection->n_incoming += 1;
3727 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3729 dbus_message_get_type (message_link->data),
3730 dbus_message_get_interface (message_link->data) ?
3731 dbus_message_get_interface (message_link->data) :
3733 dbus_message_get_member (message_link->data) ?
3734 dbus_message_get_member (message_link->data) :
3736 dbus_message_get_signature (message_link->data),
3737 connection, connection->n_incoming);
3741 * Returns the first-received message from the incoming message queue,
3742 * removing it from the queue. The caller owns a reference to the
3743 * returned message. If the queue is empty, returns #NULL.
3745 * This function bypasses any message handlers that are registered,
3746 * and so using it is usually wrong. Instead, let the main loop invoke
3747 * dbus_connection_dispatch(). Popping messages manually is only
3748 * useful in very simple programs that don't share a #DBusConnection
3749 * with any libraries or other modules.
3751 * There is a lock that covers all ways of accessing the incoming message
3752 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3753 * dbus_connection_borrow_message(), etc. will all block while one of the others
3754 * in the group is running.
3756 * @param connection the connection.
3757 * @returns next message in the incoming queue.
3760 dbus_connection_pop_message (DBusConnection *connection)
3762 DBusMessage *message;
3763 DBusDispatchStatus status;
3765 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3767 /* this is called for the side effect that it queues
3768 * up any messages from the transport
3770 status = dbus_connection_get_dispatch_status (connection);
3771 if (status != DBUS_DISPATCH_DATA_REMAINS)
3774 CONNECTION_LOCK (connection);
3775 _dbus_connection_acquire_dispatch (connection);
3776 HAVE_LOCK_CHECK (connection);
3778 message = _dbus_connection_pop_message_unlocked (connection);
3780 _dbus_verbose ("Returning popped message %p\n", message);
3782 _dbus_connection_release_dispatch (connection);
3784 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3785 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3791 * Acquire the dispatcher. This is a separate lock so the main
3792 * connection lock can be dropped to call out to application dispatch
3795 * @param connection the connection.
3798 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3800 HAVE_LOCK_CHECK (connection);
3802 _dbus_connection_ref_unlocked (connection);
3803 CONNECTION_UNLOCK (connection);
3805 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3806 _dbus_mutex_lock (connection->dispatch_mutex);
3808 while (connection->dispatch_acquired)
3810 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3811 _dbus_condvar_wait (connection->dispatch_cond,
3812 connection->dispatch_mutex);
3815 _dbus_assert (!connection->dispatch_acquired);
3817 connection->dispatch_acquired = TRUE;
3819 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3820 _dbus_mutex_unlock (connection->dispatch_mutex);
3822 CONNECTION_LOCK (connection);
3823 _dbus_connection_unref_unlocked (connection);
3827 * Release the dispatcher when you're done with it. Only call
3828 * after you've acquired the dispatcher. Wakes up at most one
3829 * thread currently waiting to acquire the dispatcher.
3831 * @param connection the connection.
3834 _dbus_connection_release_dispatch (DBusConnection *connection)
3836 HAVE_LOCK_CHECK (connection);
3838 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3839 _dbus_mutex_lock (connection->dispatch_mutex);
3841 _dbus_assert (connection->dispatch_acquired);
3843 connection->dispatch_acquired = FALSE;
3844 _dbus_condvar_wake_one (connection->dispatch_cond);
3846 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3847 _dbus_mutex_unlock (connection->dispatch_mutex);
3851 _dbus_connection_failed_pop (DBusConnection *connection,
3852 DBusList *message_link)
3854 _dbus_list_prepend_link (&connection->incoming_messages,
3856 connection->n_incoming += 1;
3859 /* Note this may be called multiple times since we don't track whether we already did it */
3861 notify_disconnected_unlocked (DBusConnection *connection)
3863 HAVE_LOCK_CHECK (connection);
3865 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
3866 * connection from dbus_bus_get(). We make the same guarantee for
3867 * dbus_connection_open() but in a different way since we don't want to
3868 * unref right here; we instead check for connectedness before returning
3869 * the connection from the hash.
3871 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
3873 /* Dump the outgoing queue, we aren't going to be able to
3874 * send it now, and we'd like accessors like
3875 * dbus_connection_get_outgoing_size() to be accurate.
3877 if (connection->n_outgoing > 0)
3881 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3882 connection->n_outgoing);
3884 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3886 _dbus_connection_message_sent (connection, link->data);
3891 /* Note this may be called multiple times since we don't track whether we already did it */
3892 static DBusDispatchStatus
3893 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
3895 HAVE_LOCK_CHECK (connection);
3897 if (connection->disconnect_message_link != NULL)
3899 _dbus_verbose ("Sending disconnect message from %s\n",
3900 _DBUS_FUNCTION_NAME);
3902 /* If we have pending calls, queue their timeouts - we want the Disconnected
3903 * to be the last message, after these timeouts.
3905 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
3907 /* We haven't sent the disconnect message already,
3908 * and all real messages have been queued up.
3910 _dbus_connection_queue_synthesized_message_link (connection,
3911 connection->disconnect_message_link);
3912 connection->disconnect_message_link = NULL;
3914 return DBUS_DISPATCH_DATA_REMAINS;
3917 return DBUS_DISPATCH_COMPLETE;
3920 static DBusDispatchStatus
3921 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3923 HAVE_LOCK_CHECK (connection);
3925 if (connection->n_incoming > 0)
3926 return DBUS_DISPATCH_DATA_REMAINS;
3927 else if (!_dbus_transport_queue_messages (connection->transport))
3928 return DBUS_DISPATCH_NEED_MEMORY;
3931 DBusDispatchStatus status;
3932 dbus_bool_t is_connected;
3934 status = _dbus_transport_get_dispatch_status (connection->transport);
3935 is_connected = _dbus_transport_get_is_connected (connection->transport);
3937 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3938 DISPATCH_STATUS_NAME (status), is_connected);
3942 /* It's possible this would be better done by having an explicit
3943 * notification from _dbus_transport_disconnect() that would
3944 * synchronously do this, instead of waiting for the next dispatch
3945 * status check. However, probably not good to change until it causes
3948 notify_disconnected_unlocked (connection);
3950 /* I'm not sure this is needed; the idea is that we want to
3951 * queue the Disconnected only after we've read all the
3952 * messages, but if we're disconnected maybe we are guaranteed
3953 * to have read them all ?
3955 if (status == DBUS_DISPATCH_COMPLETE)
3956 status = notify_disconnected_and_dispatch_complete_unlocked (connection);
3959 if (status != DBUS_DISPATCH_COMPLETE)
3961 else if (connection->n_incoming > 0)
3962 return DBUS_DISPATCH_DATA_REMAINS;
3964 return DBUS_DISPATCH_COMPLETE;
3969 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
3970 DBusDispatchStatus new_status)
3972 dbus_bool_t changed;
3973 DBusDispatchStatusFunction function;
3976 HAVE_LOCK_CHECK (connection);
3978 _dbus_connection_ref_unlocked (connection);
3980 changed = new_status != connection->last_dispatch_status;
3982 connection->last_dispatch_status = new_status;
3984 function = connection->dispatch_status_function;
3985 data = connection->dispatch_status_data;
3987 if (connection->disconnected_message_arrived &&
3988 !connection->disconnected_message_processed)
3990 connection->disconnected_message_processed = TRUE;
3992 /* this does an unref, but we have a ref
3993 * so we should not run the finalizer here
3996 connection_forget_shared_unlocked (connection);
3998 if (connection->exit_on_disconnect)
4000 CONNECTION_UNLOCK (connection);
4002 _dbus_verbose ("Exiting on Disconnected signal\n");
4004 _dbus_assert_not_reached ("Call to exit() returned");
4008 /* We drop the lock */
4009 CONNECTION_UNLOCK (connection);
4011 if (changed && function)
4013 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
4014 connection, new_status,
4015 DISPATCH_STATUS_NAME (new_status));
4016 (* function) (connection, new_status, data);
4019 dbus_connection_unref (connection);
4023 * Gets the current state of the incoming message queue.
4024 * #DBUS_DISPATCH_DATA_REMAINS indicates that the message queue
4025 * may contain messages. #DBUS_DISPATCH_COMPLETE indicates that the
4026 * incoming queue is empty. #DBUS_DISPATCH_NEED_MEMORY indicates that
4027 * there could be data, but we can't know for sure without more
4030 * To process the incoming message queue, use dbus_connection_dispatch()
4031 * or (in rare cases) dbus_connection_pop_message().
4033 * Note, #DBUS_DISPATCH_DATA_REMAINS really means that either we
4034 * have messages in the queue, or we have raw bytes buffered up
4035 * that need to be parsed. When these bytes are parsed, they
4036 * may not add up to an entire message. Thus, it's possible
4037 * to see a status of #DBUS_DISPATCH_DATA_REMAINS but not
4038 * have a message yet.
4040 * In particular this happens on initial connection, because all sorts
4041 * of authentication protocol stuff has to be parsed before the
4042 * first message arrives.
4044 * @param connection the connection.
4045 * @returns current dispatch status
4048 dbus_connection_get_dispatch_status (DBusConnection *connection)
4050 DBusDispatchStatus status;
4052 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4054 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
4056 CONNECTION_LOCK (connection);
4058 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4060 CONNECTION_UNLOCK (connection);
4066 * Filter funtion for handling the Peer standard interface.
4068 static DBusHandlerResult
4069 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
4070 DBusMessage *message)
4072 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
4074 /* This means we're letting the bus route this message */
4075 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4077 else if (dbus_message_is_method_call (message,
4078 DBUS_INTERFACE_PEER,
4084 ret = dbus_message_new_method_return (message);
4086 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4088 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4090 dbus_message_unref (ret);
4093 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4095 return DBUS_HANDLER_RESULT_HANDLED;
4097 else if (dbus_message_is_method_call (message,
4098 DBUS_INTERFACE_PEER,
4105 ret = dbus_message_new_method_return (message);
4107 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4110 _dbus_string_init (&uuid);
4111 if (_dbus_get_local_machine_uuid_encoded (&uuid))
4113 const char *v_STRING = _dbus_string_get_const_data (&uuid);
4114 if (dbus_message_append_args (ret,
4115 DBUS_TYPE_STRING, &v_STRING,
4118 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4121 _dbus_string_free (&uuid);
4123 dbus_message_unref (ret);
4126 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4128 return DBUS_HANDLER_RESULT_HANDLED;
4130 else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
4132 /* We need to bounce anything else with this interface, otherwise apps
4133 * could start extending the interface and when we added extensions
4134 * here to DBusConnection we'd break those apps.
4140 ret = dbus_message_new_error (message,
4141 DBUS_ERROR_UNKNOWN_METHOD,
4142 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
4144 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4146 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4148 dbus_message_unref (ret);
4151 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4153 return DBUS_HANDLER_RESULT_HANDLED;
4157 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4162 * Processes all builtin filter functions
4164 * If the spec specifies a standard interface
4165 * they should be processed from this method
4167 static DBusHandlerResult
4168 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
4169 DBusMessage *message)
4171 /* We just run one filter for now but have the option to run more
4172 if the spec calls for it in the future */
4174 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
4178 * Processes any incoming data.
4180 * If there's incoming raw data that has not yet been parsed, it is
4181 * parsed, which may or may not result in adding messages to the
4184 * The incoming data buffer is filled when the connection reads from
4185 * its underlying transport (such as a socket). Reading usually
4186 * happens in dbus_watch_handle() or dbus_connection_read_write().
4188 * If there are complete messages in the incoming queue,
4189 * dbus_connection_dispatch() removes one message from the queue and
4190 * processes it. Processing has three steps.
4192 * First, any method replies are passed to #DBusPendingCall or
4193 * dbus_connection_send_with_reply_and_block() in order to
4194 * complete the pending method call.
4196 * Second, any filters registered with dbus_connection_add_filter()
4197 * are run. If any filter returns #DBUS_HANDLER_RESULT_HANDLED
4198 * then processing stops after that filter.
4200 * Third, if the message is a method call it is forwarded to
4201 * any registered object path handlers added with
4202 * dbus_connection_register_object_path() or
4203 * dbus_connection_register_fallback().
4205 * A single call to dbus_connection_dispatch() will process at most
4206 * one message; it will not clear the entire message queue.
4208 * Be careful about calling dbus_connection_dispatch() from inside a
4209 * message handler, i.e. calling dbus_connection_dispatch()
4210 * recursively. If threads have been initialized with a recursive
4211 * mutex function, then this will not deadlock; however, it can
4212 * certainly confuse your application.
4214 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
4216 * @param connection the connection
4217 * @returns dispatch status, see dbus_connection_get_dispatch_status()
4220 dbus_connection_dispatch (DBusConnection *connection)
4222 DBusMessage *message;
4223 DBusList *link, *filter_list_copy, *message_link;
4224 DBusHandlerResult result;
4225 DBusPendingCall *pending;
4226 dbus_int32_t reply_serial;
4227 DBusDispatchStatus status;
4229 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4231 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
4233 CONNECTION_LOCK (connection);
4234 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4235 if (status != DBUS_DISPATCH_DATA_REMAINS)
4237 /* unlocks and calls out to user code */
4238 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4242 /* We need to ref the connection since the callback could potentially
4243 * drop the last ref to it
4245 _dbus_connection_ref_unlocked (connection);
4247 _dbus_connection_acquire_dispatch (connection);
4248 HAVE_LOCK_CHECK (connection);
4250 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4251 if (message_link == NULL)
4253 /* another thread dispatched our stuff */
4255 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
4257 _dbus_connection_release_dispatch (connection);
4259 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4261 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4263 dbus_connection_unref (connection);
4268 message = message_link->data;
4270 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
4272 dbus_message_get_type (message),
4273 dbus_message_get_interface (message) ?
4274 dbus_message_get_interface (message) :
4276 dbus_message_get_member (message) ?
4277 dbus_message_get_member (message) :
4279 dbus_message_get_signature (message));
4281 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4283 /* Pending call handling must be first, because if you do
4284 * dbus_connection_send_with_reply_and_block() or
4285 * dbus_pending_call_block() then no handlers/filters will be run on
4286 * the reply. We want consistent semantics in the case where we
4287 * dbus_connection_dispatch() the reply.
4290 reply_serial = dbus_message_get_reply_serial (message);
4291 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4295 _dbus_verbose ("Dispatching a pending reply\n");
4296 complete_pending_call_and_unlock (connection, pending, message);
4297 pending = NULL; /* it's probably unref'd */
4299 CONNECTION_LOCK (connection);
4300 _dbus_verbose ("pending call completed in dispatch\n");
4301 result = DBUS_HANDLER_RESULT_HANDLED;
4305 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4306 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4309 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4311 _dbus_connection_release_dispatch (connection);
4312 HAVE_LOCK_CHECK (connection);
4314 _dbus_connection_failed_pop (connection, message_link);
4316 /* unlocks and calls user code */
4317 _dbus_connection_update_dispatch_status_and_unlock (connection,
4318 DBUS_DISPATCH_NEED_MEMORY);
4321 dbus_pending_call_unref (pending);
4322 dbus_connection_unref (connection);
4324 return DBUS_DISPATCH_NEED_MEMORY;
4327 _dbus_list_foreach (&filter_list_copy,
4328 (DBusForeachFunction)_dbus_message_filter_ref,
4331 /* We're still protected from dispatch() reentrancy here
4332 * since we acquired the dispatcher
4334 CONNECTION_UNLOCK (connection);
4336 link = _dbus_list_get_first_link (&filter_list_copy);
4337 while (link != NULL)
4339 DBusMessageFilter *filter = link->data;
4340 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
4342 if (filter->function == NULL)
4344 _dbus_verbose (" filter was removed in a callback function\n");
4349 _dbus_verbose (" running filter on message %p\n", message);
4350 result = (* filter->function) (connection, message, filter->user_data);
4352 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4358 _dbus_list_foreach (&filter_list_copy,
4359 (DBusForeachFunction)_dbus_message_filter_unref,
4361 _dbus_list_clear (&filter_list_copy);
4363 CONNECTION_LOCK (connection);
4365 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4367 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
4370 else if (result == DBUS_HANDLER_RESULT_HANDLED)
4372 _dbus_verbose ("filter handled message in dispatch\n");
4376 /* We're still protected from dispatch() reentrancy here
4377 * since we acquired the dispatcher
4379 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
4381 dbus_message_get_type (message),
4382 dbus_message_get_interface (message) ?
4383 dbus_message_get_interface (message) :
4385 dbus_message_get_member (message) ?
4386 dbus_message_get_member (message) :
4388 dbus_message_get_signature (message));
4390 HAVE_LOCK_CHECK (connection);
4391 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4394 CONNECTION_LOCK (connection);
4396 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4398 _dbus_verbose ("object tree handled message in dispatch\n");
4402 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
4406 DBusPreallocatedSend *preallocated;
4408 _dbus_verbose (" sending error %s\n",
4409 DBUS_ERROR_UNKNOWN_METHOD);
4411 if (!_dbus_string_init (&str))
4413 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4414 _dbus_verbose ("no memory for error string in dispatch\n");
4418 if (!_dbus_string_append_printf (&str,
4419 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
4420 dbus_message_get_member (message),
4421 dbus_message_get_signature (message),
4422 dbus_message_get_interface (message)))
4424 _dbus_string_free (&str);
4425 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4426 _dbus_verbose ("no memory for error string in dispatch\n");
4430 reply = dbus_message_new_error (message,
4431 DBUS_ERROR_UNKNOWN_METHOD,
4432 _dbus_string_get_const_data (&str));
4433 _dbus_string_free (&str);
4437 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4438 _dbus_verbose ("no memory for error reply in dispatch\n");
4442 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4444 if (preallocated == NULL)
4446 dbus_message_unref (reply);
4447 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4448 _dbus_verbose ("no memory for error send in dispatch\n");
4452 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4455 dbus_message_unref (reply);
4457 result = DBUS_HANDLER_RESULT_HANDLED;
4460 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
4461 dbus_message_get_type (message),
4462 dbus_message_get_interface (message) ?
4463 dbus_message_get_interface (message) :
4465 dbus_message_get_member (message) ?
4466 dbus_message_get_member (message) :
4468 dbus_message_get_signature (message),
4472 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4474 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
4476 /* Put message back, and we'll start over.
4477 * Yes this means handlers must be idempotent if they
4478 * don't return HANDLED; c'est la vie.
4480 _dbus_connection_putback_message_link_unlocked (connection,
4485 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
4487 _dbus_list_free_link (message_link);
4488 dbus_message_unref (message); /* don't want the message to count in max message limits
4489 * in computing dispatch status below
4493 _dbus_connection_release_dispatch (connection);
4494 HAVE_LOCK_CHECK (connection);
4496 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
4497 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4499 /* unlocks and calls user code */
4500 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4502 dbus_connection_unref (connection);
4508 * Sets the watch functions for the connection. These functions are
4509 * responsible for making the application's main loop aware of file
4510 * descriptors that need to be monitored for events, using select() or
4511 * poll(). When using Qt, typically the DBusAddWatchFunction would
4512 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
4513 * could call g_io_add_watch(), or could be used as part of a more
4514 * elaborate GSource. Note that when a watch is added, it may
4517 * The DBusWatchToggledFunction notifies the application that the
4518 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
4519 * to check this. A disabled watch should have no effect, and enabled
4520 * watch should be added to the main loop. This feature is used
4521 * instead of simply adding/removing the watch because
4522 * enabling/disabling can be done without memory allocation. The
4523 * toggled function may be NULL if a main loop re-queries
4524 * dbus_watch_get_enabled() every time anyway.
4526 * The DBusWatch can be queried for the file descriptor to watch using
4527 * dbus_watch_get_unix_fd() or dbus_watch_get_socket(), and for the
4528 * events to watch for using dbus_watch_get_flags(). The flags
4529 * returned by dbus_watch_get_flags() will only contain
4530 * DBUS_WATCH_READABLE and DBUS_WATCH_WRITABLE, never
4531 * DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; all watches implicitly
4532 * include a watch for hangups, errors, and other exceptional
4535 * Once a file descriptor becomes readable or writable, or an exception
4536 * occurs, dbus_watch_handle() should be called to
4537 * notify the connection of the file descriptor's condition.
4539 * dbus_watch_handle() cannot be called during the
4540 * DBusAddWatchFunction, as the connection will not be ready to handle
4543 * It is not allowed to reference a DBusWatch after it has been passed
4544 * to remove_function.
4546 * If #FALSE is returned due to lack of memory, the failure may be due
4547 * to a #FALSE return from the new add_function. If so, the
4548 * add_function may have been called successfully one or more times,
4549 * but the remove_function will also have been called to remove any
4550 * successful adds. i.e. if #FALSE is returned the net result
4551 * should be that dbus_connection_set_watch_functions() has no effect,
4552 * but the add_function and remove_function may have been called.
4554 * @todo We need to drop the lock when we call the
4555 * add/remove/toggled functions which can be a side effect
4556 * of setting the watch functions.
4558 * @param connection the connection.
4559 * @param add_function function to begin monitoring a new descriptor.
4560 * @param remove_function function to stop monitoring a descriptor.
4561 * @param toggled_function function to notify of enable/disable
4562 * @param data data to pass to add_function and remove_function.
4563 * @param free_data_function function to be called to free the data.
4564 * @returns #FALSE on failure (no memory)
4567 dbus_connection_set_watch_functions (DBusConnection *connection,
4568 DBusAddWatchFunction add_function,
4569 DBusRemoveWatchFunction remove_function,
4570 DBusWatchToggledFunction toggled_function,
4572 DBusFreeFunction free_data_function)
4575 DBusWatchList *watches;
4577 _dbus_return_val_if_fail (connection != NULL, FALSE);
4579 CONNECTION_LOCK (connection);
4581 #ifndef DBUS_DISABLE_CHECKS
4582 if (connection->watches == NULL)
4584 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4585 _DBUS_FUNCTION_NAME);
4590 /* ref connection for slightly better reentrancy */
4591 _dbus_connection_ref_unlocked (connection);
4593 /* This can call back into user code, and we need to drop the
4594 * connection lock when it does. This is kind of a lame
4597 watches = connection->watches;
4598 connection->watches = NULL;
4599 CONNECTION_UNLOCK (connection);
4601 retval = _dbus_watch_list_set_functions (watches,
4602 add_function, remove_function,
4604 data, free_data_function);
4605 CONNECTION_LOCK (connection);
4606 connection->watches = watches;
4608 CONNECTION_UNLOCK (connection);
4609 /* drop our paranoid refcount */
4610 dbus_connection_unref (connection);
4616 * Sets the timeout functions for the connection. These functions are
4617 * responsible for making the application's main loop aware of timeouts.
4618 * When using Qt, typically the DBusAddTimeoutFunction would create a
4619 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
4622 * The DBusTimeoutToggledFunction notifies the application that the
4623 * timeout has been enabled or disabled. Call
4624 * dbus_timeout_get_enabled() to check this. A disabled timeout should
4625 * have no effect, and enabled timeout should be added to the main
4626 * loop. This feature is used instead of simply adding/removing the
4627 * timeout because enabling/disabling can be done without memory
4628 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
4629 * to enable and disable. The toggled function may be NULL if a main
4630 * loop re-queries dbus_timeout_get_enabled() every time anyway.
4631 * Whenever a timeout is toggled, its interval may change.
4633 * The DBusTimeout can be queried for the timer interval using
4634 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
4635 * repeatedly, each time the interval elapses, starting after it has
4636 * elapsed once. The timeout stops firing when it is removed with the
4637 * given remove_function. The timer interval may change whenever the
4638 * timeout is added, removed, or toggled.
4640 * @param connection the connection.
4641 * @param add_function function to add a timeout.
4642 * @param remove_function function to remove a timeout.
4643 * @param toggled_function function to notify of enable/disable
4644 * @param data data to pass to add_function and remove_function.
4645 * @param free_data_function function to be called to free the data.
4646 * @returns #FALSE on failure (no memory)
4649 dbus_connection_set_timeout_functions (DBusConnection *connection,
4650 DBusAddTimeoutFunction add_function,
4651 DBusRemoveTimeoutFunction remove_function,
4652 DBusTimeoutToggledFunction toggled_function,
4654 DBusFreeFunction free_data_function)
4657 DBusTimeoutList *timeouts;
4659 _dbus_return_val_if_fail (connection != NULL, FALSE);
4661 CONNECTION_LOCK (connection);
4663 #ifndef DBUS_DISABLE_CHECKS
4664 if (connection->timeouts == NULL)
4666 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4667 _DBUS_FUNCTION_NAME);
4672 /* ref connection for slightly better reentrancy */
4673 _dbus_connection_ref_unlocked (connection);
4675 timeouts = connection->timeouts;
4676 connection->timeouts = NULL;
4677 CONNECTION_UNLOCK (connection);
4679 retval = _dbus_timeout_list_set_functions (timeouts,
4680 add_function, remove_function,
4682 data, free_data_function);
4683 CONNECTION_LOCK (connection);
4684 connection->timeouts = timeouts;
4686 CONNECTION_UNLOCK (connection);
4687 /* drop our paranoid refcount */
4688 dbus_connection_unref (connection);
4694 * Sets the mainloop wakeup function for the connection. This function
4695 * is responsible for waking up the main loop (if its sleeping in
4696 * another thread) when some some change has happened to the
4697 * connection that the mainloop needs to reconsider (e.g. a message
4698 * has been queued for writing). When using Qt, this typically
4699 * results in a call to QEventLoop::wakeUp(). When using GLib, it
4700 * would call g_main_context_wakeup().
4702 * @param connection the connection.
4703 * @param wakeup_main_function function to wake up the mainloop
4704 * @param data data to pass wakeup_main_function
4705 * @param free_data_function function to be called to free the data.
4708 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
4709 DBusWakeupMainFunction wakeup_main_function,
4711 DBusFreeFunction free_data_function)
4714 DBusFreeFunction old_free_data;
4716 _dbus_return_if_fail (connection != NULL);
4718 CONNECTION_LOCK (connection);
4719 old_data = connection->wakeup_main_data;
4720 old_free_data = connection->free_wakeup_main_data;
4722 connection->wakeup_main_function = wakeup_main_function;
4723 connection->wakeup_main_data = data;
4724 connection->free_wakeup_main_data = free_data_function;
4726 CONNECTION_UNLOCK (connection);
4728 /* Callback outside the lock */
4730 (*old_free_data) (old_data);
4734 * Set a function to be invoked when the dispatch status changes.
4735 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
4736 * dbus_connection_dispatch() needs to be called to process incoming
4737 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
4738 * from inside the DBusDispatchStatusFunction. Indeed, almost
4739 * any reentrancy in this function is a bad idea. Instead,
4740 * the DBusDispatchStatusFunction should simply save an indication
4741 * that messages should be dispatched later, when the main loop
4744 * If you don't set a dispatch status function, you have to be sure to
4745 * dispatch on every iteration of your main loop, especially if
4746 * dbus_watch_handle() or dbus_timeout_handle() were called.
4748 * @param connection the connection
4749 * @param function function to call on dispatch status changes
4750 * @param data data for function
4751 * @param free_data_function free the function data
4754 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
4755 DBusDispatchStatusFunction function,
4757 DBusFreeFunction free_data_function)
4760 DBusFreeFunction old_free_data;
4762 _dbus_return_if_fail (connection != NULL);
4764 CONNECTION_LOCK (connection);
4765 old_data = connection->dispatch_status_data;
4766 old_free_data = connection->free_dispatch_status_data;
4768 connection->dispatch_status_function = function;
4769 connection->dispatch_status_data = data;
4770 connection->free_dispatch_status_data = free_data_function;
4772 CONNECTION_UNLOCK (connection);
4774 /* Callback outside the lock */
4776 (*old_free_data) (old_data);
4780 * Get the UNIX file descriptor of the connection, if any. This can
4781 * be used for SELinux access control checks with getpeercon() for
4782 * example. DO NOT read or write to the file descriptor, or try to
4783 * select() on it; use DBusWatch for main loop integration. Not all
4784 * connections will have a file descriptor. So for adding descriptors
4785 * to the main loop, use dbus_watch_get_unix_fd() and so forth.
4787 * If the connection is socket-based, you can also use
4788 * dbus_connection_get_socket(), which will work on Windows too.
4789 * This function always fails on Windows.
4791 * Right now the returned descriptor is always a socket, but
4792 * that is not guaranteed.
4794 * @param connection the connection
4795 * @param fd return location for the file descriptor.
4796 * @returns #TRUE if fd is successfully obtained.
4799 dbus_connection_get_unix_fd (DBusConnection *connection,
4802 _dbus_return_val_if_fail (connection != NULL, FALSE);
4803 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4806 /* FIXME do this on a lower level */
4810 return dbus_connection_get_socket(connection, fd);
4814 * Gets the underlying Windows or UNIX socket file descriptor
4815 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
4816 * select() on it; use DBusWatch for main loop integration. Not all
4817 * connections will have a socket. So for adding descriptors
4818 * to the main loop, use dbus_watch_get_socket() and so forth.
4820 * If the connection is not socket-based, this function will return FALSE,
4821 * even if the connection does have a file descriptor of some kind.
4822 * i.e. this function always returns specifically a socket file descriptor.
4824 * @param connection the connection
4825 * @param fd return location for the file descriptor.
4826 * @returns #TRUE if fd is successfully obtained.
4829 dbus_connection_get_socket(DBusConnection *connection,
4834 _dbus_return_val_if_fail (connection != NULL, FALSE);
4835 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4837 CONNECTION_LOCK (connection);
4839 retval = _dbus_transport_get_socket_fd (connection->transport,
4842 CONNECTION_UNLOCK (connection);
4849 * Gets the UNIX user ID of the connection if known. Returns #TRUE if
4850 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
4851 * for now, though in theory someone could hook Windows to NIS or
4852 * something. Always returns #FALSE prior to authenticating the
4855 * The UID is only read by servers from clients; clients can't usually
4856 * get the UID of servers, because servers do not authenticate to
4857 * clients. The returned UID is the UID the connection authenticated
4860 * The message bus is a server and the apps connecting to the bus
4863 * You can ask the bus to tell you the UID of another connection though
4864 * if you like; this is done with dbus_bus_get_unix_user().
4866 * @param connection the connection
4867 * @param uid return location for the user ID
4868 * @returns #TRUE if uid is filled in with a valid user ID
4871 dbus_connection_get_unix_user (DBusConnection *connection,
4876 _dbus_return_val_if_fail (connection != NULL, FALSE);
4877 _dbus_return_val_if_fail (uid != NULL, FALSE);
4879 CONNECTION_LOCK (connection);
4881 if (!_dbus_transport_get_is_authenticated (connection->transport))
4884 result = _dbus_transport_get_unix_user (connection->transport,
4888 _dbus_assert (!result);
4891 CONNECTION_UNLOCK (connection);
4897 * Gets the process ID of the connection if any.
4898 * Returns #TRUE if the pid is filled in.
4899 * Always returns #FALSE prior to authenticating the
4902 * @param connection the connection
4903 * @param pid return location for the process ID
4904 * @returns #TRUE if uid is filled in with a valid process ID
4907 dbus_connection_get_unix_process_id (DBusConnection *connection,
4912 _dbus_return_val_if_fail (connection != NULL, FALSE);
4913 _dbus_return_val_if_fail (pid != NULL, FALSE);
4915 CONNECTION_LOCK (connection);
4917 if (!_dbus_transport_get_is_authenticated (connection->transport))
4920 result = _dbus_transport_get_unix_process_id (connection->transport,
4923 _dbus_assert (!result);
4926 CONNECTION_UNLOCK (connection);
4932 * Sets a predicate function used to determine whether a given user ID
4933 * is allowed to connect. When an incoming connection has
4934 * authenticated with a particular user ID, this function is called;
4935 * if it returns #TRUE, the connection is allowed to proceed,
4936 * otherwise the connection is disconnected.
4938 * If the function is set to #NULL (as it is by default), then
4939 * only the same UID as the server process will be allowed to
4940 * connect. Also, root is always allowed to connect.
4942 * On Windows, the function will be set and its free_data_function will
4943 * be invoked when the connection is freed or a new function is set.
4944 * However, the function will never be called, because there are
4945 * no UNIX user ids to pass to it, or at least none of the existing
4946 * auth protocols would allow authenticating as a UNIX user on Windows.
4948 * @param connection the connection
4949 * @param function the predicate
4950 * @param data data to pass to the predicate
4951 * @param free_data_function function to free the data
4954 dbus_connection_set_unix_user_function (DBusConnection *connection,
4955 DBusAllowUnixUserFunction function,
4957 DBusFreeFunction free_data_function)
4959 void *old_data = NULL;
4960 DBusFreeFunction old_free_function = NULL;
4962 _dbus_return_if_fail (connection != NULL);
4964 CONNECTION_LOCK (connection);
4965 _dbus_transport_set_unix_user_function (connection->transport,
4966 function, data, free_data_function,
4967 &old_data, &old_free_function);
4968 CONNECTION_UNLOCK (connection);
4970 if (old_free_function != NULL)
4971 (* old_free_function) (old_data);
4975 * Gets the Windows user SID of the connection if known. Returns
4976 * #TRUE if the ID is filled in. Always returns #FALSE on non-Windows
4977 * platforms for now, though in theory someone could hook UNIX to
4978 * Active Directory or something. Always returns #FALSE prior to
4979 * authenticating the connection.
4981 * The user is only read by servers from clients; clients can't usually
4982 * get the user of servers, because servers do not authenticate to
4983 * clients. The returned user is the user the connection authenticated
4986 * The message bus is a server and the apps connecting to the bus
4989 * The returned user string has to be freed with dbus_free().
4991 * The return value indicates whether the user SID is available;
4992 * if it's available but we don't have the memory to copy it,
4993 * then the return value is #TRUE and #NULL is given as the SID.
4995 * @todo We would like to be able to say "You can ask the bus to tell
4996 * you the user of another connection though if you like; this is done
4997 * with dbus_bus_get_windows_user()." But this has to be implemented
4998 * in bus/driver.c and dbus/dbus-bus.c, and is pointless anyway
4999 * since on Windows we only use the session bus for now.
5001 * @param connection the connection
5002 * @param windows_sid_p return location for an allocated copy of the user ID, or #NULL if no memory
5003 * @returns #TRUE if user is available (returned value may be #NULL anyway if no memory)
5006 dbus_connection_get_windows_user (DBusConnection *connection,
5007 char **windows_sid_p)
5011 _dbus_return_val_if_fail (connection != NULL, FALSE);
5012 _dbus_return_val_if_fail (windows_sid_p != NULL, FALSE);
5014 CONNECTION_LOCK (connection);
5016 if (!_dbus_transport_get_is_authenticated (connection->transport))
5019 result = _dbus_transport_get_windows_user (connection->transport,
5023 _dbus_assert (!result);
5026 CONNECTION_UNLOCK (connection);
5032 * Sets a predicate function used to determine whether a given user ID
5033 * is allowed to connect. When an incoming connection has
5034 * authenticated with a particular user ID, this function is called;
5035 * if it returns #TRUE, the connection is allowed to proceed,
5036 * otherwise the connection is disconnected.
5038 * If the function is set to #NULL (as it is by default), then
5039 * only the same user owning the server process will be allowed to
5042 * On UNIX, the function will be set and its free_data_function will
5043 * be invoked when the connection is freed or a new function is set.
5044 * However, the function will never be called, because there is no
5045 * way right now to authenticate as a Windows user on UNIX.
5047 * @param connection the connection
5048 * @param function the predicate
5049 * @param data data to pass to the predicate
5050 * @param free_data_function function to free the data
5053 dbus_connection_set_windows_user_function (DBusConnection *connection,
5054 DBusAllowWindowsUserFunction function,
5056 DBusFreeFunction free_data_function)
5058 void *old_data = NULL;
5059 DBusFreeFunction old_free_function = NULL;
5061 _dbus_return_if_fail (connection != NULL);
5063 CONNECTION_LOCK (connection);
5064 _dbus_transport_set_windows_user_function (connection->transport,
5065 function, data, free_data_function,
5066 &old_data, &old_free_function);
5067 CONNECTION_UNLOCK (connection);
5069 if (old_free_function != NULL)
5070 (* old_free_function) (old_data);
5074 * This function must be called on the server side of a connection when the
5075 * connection is first seen in the #DBusNewConnectionFunction. If set to
5076 * #TRUE (the default is #FALSE), then the connection can proceed even if
5077 * the client does not authenticate as some user identity, i.e. clients
5078 * can connect anonymously.
5080 * This setting interacts with the available authorization mechanisms
5081 * (see dbus_server_set_auth_mechanisms()). Namely, an auth mechanism
5082 * such as ANONYMOUS that supports anonymous auth must be included in
5083 * the list of available mechanisms for anonymous login to work.
5085 * This setting also changes the default rule for connections
5086 * authorized as a user; normally, if a connection authorizes as
5087 * a user identity, it is permitted if the user identity is
5088 * root or the user identity matches the user identity of the server
5089 * process. If anonymous connections are allowed, however,
5090 * then any user identity is allowed.
5092 * You can override the rules for connections authorized as a
5093 * user identity with dbus_connection_set_unix_user_function()
5094 * and dbus_connection_set_windows_user_function().
5096 * @param connection the connection
5097 * @param value whether to allow authentication as an anonymous user
5100 dbus_connection_set_allow_anonymous (DBusConnection *connection,
5103 _dbus_return_if_fail (connection != NULL);
5105 CONNECTION_LOCK (connection);
5106 _dbus_transport_set_allow_anonymous (connection->transport, value);
5107 CONNECTION_UNLOCK (connection);
5112 * Normally #DBusConnection automatically handles all messages to the
5113 * org.freedesktop.DBus.Peer interface. However, the message bus wants
5114 * to be able to route methods on that interface through the bus and
5115 * to other applications. If routing peer messages is enabled, then
5116 * messages with the org.freedesktop.DBus.Peer interface that also
5117 * have a bus destination name set will not be automatically
5118 * handled by the #DBusConnection and instead will be dispatched
5119 * normally to the application.
5121 * If a normal application sets this flag, it can break things badly.
5122 * So don't set this unless you are the message bus.
5124 * @param connection the connection
5125 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
5128 dbus_connection_set_route_peer_messages (DBusConnection *connection,
5131 _dbus_return_if_fail (connection != NULL);
5133 CONNECTION_LOCK (connection);
5134 connection->route_peer_messages = TRUE;
5135 CONNECTION_UNLOCK (connection);
5139 * Adds a message filter. Filters are handlers that are run on all
5140 * incoming messages, prior to the objects registered with
5141 * dbus_connection_register_object_path(). Filters are run in the
5142 * order that they were added. The same handler can be added as a
5143 * filter more than once, in which case it will be run more than once.
5144 * Filters added during a filter callback won't be run on the message
5147 * @todo we don't run filters on messages while blocking without
5148 * entering the main loop, since filters are run as part of
5149 * dbus_connection_dispatch(). This is probably a feature, as filters
5150 * could create arbitrary reentrancy. But kind of sucks if you're
5151 * trying to filter METHOD_RETURN for some reason.
5153 * @param connection the connection
5154 * @param function function to handle messages
5155 * @param user_data user data to pass to the function
5156 * @param free_data_function function to use for freeing user data
5157 * @returns #TRUE on success, #FALSE if not enough memory.
5160 dbus_connection_add_filter (DBusConnection *connection,
5161 DBusHandleMessageFunction function,
5163 DBusFreeFunction free_data_function)
5165 DBusMessageFilter *filter;
5167 _dbus_return_val_if_fail (connection != NULL, FALSE);
5168 _dbus_return_val_if_fail (function != NULL, FALSE);
5170 filter = dbus_new0 (DBusMessageFilter, 1);
5174 filter->refcount.value = 1;
5176 CONNECTION_LOCK (connection);
5178 if (!_dbus_list_append (&connection->filter_list,
5181 _dbus_message_filter_unref (filter);
5182 CONNECTION_UNLOCK (connection);
5186 /* Fill in filter after all memory allocated,
5187 * so we don't run the free_user_data_function
5188 * if the add_filter() fails
5191 filter->function = function;
5192 filter->user_data = user_data;
5193 filter->free_user_data_function = free_data_function;
5195 CONNECTION_UNLOCK (connection);
5200 * Removes a previously-added message filter. It is a programming
5201 * error to call this function for a handler that has not been added
5202 * as a filter. If the given handler was added more than once, only
5203 * one instance of it will be removed (the most recently-added
5206 * @param connection the connection
5207 * @param function the handler to remove
5208 * @param user_data user data for the handler to remove
5212 dbus_connection_remove_filter (DBusConnection *connection,
5213 DBusHandleMessageFunction function,
5217 DBusMessageFilter *filter;
5219 _dbus_return_if_fail (connection != NULL);
5220 _dbus_return_if_fail (function != NULL);
5222 CONNECTION_LOCK (connection);
5226 link = _dbus_list_get_last_link (&connection->filter_list);
5227 while (link != NULL)
5229 filter = link->data;
5231 if (filter->function == function &&
5232 filter->user_data == user_data)
5234 _dbus_list_remove_link (&connection->filter_list, link);
5235 filter->function = NULL;
5240 link = _dbus_list_get_prev_link (&connection->filter_list, link);
5243 CONNECTION_UNLOCK (connection);
5245 #ifndef DBUS_DISABLE_CHECKS
5248 _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
5249 function, user_data);
5254 /* Call application code */
5255 if (filter->free_user_data_function)
5256 (* filter->free_user_data_function) (filter->user_data);
5258 filter->free_user_data_function = NULL;
5259 filter->user_data = NULL;
5261 _dbus_message_filter_unref (filter);
5265 * Registers a handler for a given path in the object hierarchy.
5266 * The given vtable handles messages sent to exactly the given path.
5268 * @param connection the connection
5269 * @param path a '/' delimited string of path elements
5270 * @param vtable the virtual table
5271 * @param user_data data to pass to functions in the vtable
5272 * @param error address where an error can be returned
5273 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5274 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
5277 dbus_connection_try_register_object_path (DBusConnection *connection,
5279 const DBusObjectPathVTable *vtable,
5283 char **decomposed_path;
5286 _dbus_return_val_if_fail (connection != NULL, FALSE);
5287 _dbus_return_val_if_fail (path != NULL, FALSE);
5288 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5289 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5291 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5294 CONNECTION_LOCK (connection);
5296 retval = _dbus_object_tree_register (connection->objects,
5298 (const char **) decomposed_path, vtable,
5301 CONNECTION_UNLOCK (connection);
5303 dbus_free_string_array (decomposed_path);
5309 * Registers a handler for a given path in the object hierarchy.
5310 * The given vtable handles messages sent to exactly the given path.
5312 * It is a bug to call this function for object paths which already
5313 * have a handler. Use dbus_connection_try_register_object_path() if this
5314 * might be the case.
5316 * @param connection the connection
5317 * @param path a '/' delimited string of path elements
5318 * @param vtable the virtual table
5319 * @param user_data data to pass to functions in the vtable
5320 * @returns #FALSE if not enough memory
5323 dbus_connection_register_object_path (DBusConnection *connection,
5325 const DBusObjectPathVTable *vtable,
5328 char **decomposed_path;
5332 _dbus_return_val_if_fail (connection != NULL, FALSE);
5333 _dbus_return_val_if_fail (path != NULL, FALSE);
5334 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5335 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5337 dbus_error_init (&error);
5339 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5342 CONNECTION_LOCK (connection);
5344 retval = _dbus_object_tree_register (connection->objects,
5346 (const char **) decomposed_path, vtable,
5349 CONNECTION_UNLOCK (connection);
5351 dbus_free_string_array (decomposed_path);
5353 if (dbus_error_has_name (&error, DBUS_ERROR_ADDRESS_IN_USE))
5355 _dbus_warn ("%s\n", error.message);
5356 dbus_error_free (&error);
5364 * Registers a fallback handler for a given subsection of the object
5365 * hierarchy. The given vtable handles messages at or below the given
5366 * path. You can use this to establish a default message handling
5367 * policy for a whole "subdirectory."
5369 * @param connection the connection
5370 * @param path a '/' delimited string of path elements
5371 * @param vtable the virtual table
5372 * @param user_data data to pass to functions in the vtable
5373 * @param error address where an error can be returned
5374 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5375 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
5378 dbus_connection_try_register_fallback (DBusConnection *connection,
5380 const DBusObjectPathVTable *vtable,
5384 char **decomposed_path;
5387 _dbus_return_val_if_fail (connection != NULL, FALSE);
5388 _dbus_return_val_if_fail (path != NULL, FALSE);
5389 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5390 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5392 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5395 CONNECTION_LOCK (connection);
5397 retval = _dbus_object_tree_register (connection->objects,
5399 (const char **) decomposed_path, vtable,
5402 CONNECTION_UNLOCK (connection);
5404 dbus_free_string_array (decomposed_path);
5410 * Registers a fallback handler for a given subsection of the object
5411 * hierarchy. The given vtable handles messages at or below the given
5412 * path. You can use this to establish a default message handling
5413 * policy for a whole "subdirectory."
5415 * It is a bug to call this function for object paths which already
5416 * have a handler. Use dbus_connection_try_register_fallback() if this
5417 * might be the case.
5419 * @param connection the connection
5420 * @param path a '/' delimited string of path elements
5421 * @param vtable the virtual table
5422 * @param user_data data to pass to functions in the vtable
5423 * @returns #FALSE if not enough memory
5426 dbus_connection_register_fallback (DBusConnection *connection,
5428 const DBusObjectPathVTable *vtable,
5431 char **decomposed_path;
5435 _dbus_return_val_if_fail (connection != NULL, FALSE);
5436 _dbus_return_val_if_fail (path != NULL, FALSE);
5437 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5438 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5440 dbus_error_init (&error);
5442 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5445 CONNECTION_LOCK (connection);
5447 retval = _dbus_object_tree_register (connection->objects,
5449 (const char **) decomposed_path, vtable,
5452 CONNECTION_UNLOCK (connection);
5454 dbus_free_string_array (decomposed_path);
5456 if (dbus_error_has_name (&error, DBUS_ERROR_ADDRESS_IN_USE))
5458 _dbus_warn ("%s\n", error.message);
5459 dbus_error_free (&error);
5467 * Unregisters the handler registered with exactly the given path.
5468 * It's a bug to call this function for a path that isn't registered.
5469 * Can unregister both fallback paths and object paths.
5471 * @param connection the connection
5472 * @param path a '/' delimited string of path elements
5473 * @returns #FALSE if not enough memory
5476 dbus_connection_unregister_object_path (DBusConnection *connection,
5479 char **decomposed_path;
5481 _dbus_return_val_if_fail (connection != NULL, FALSE);
5482 _dbus_return_val_if_fail (path != NULL, FALSE);
5483 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5485 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5488 CONNECTION_LOCK (connection);
5490 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
5492 dbus_free_string_array (decomposed_path);
5498 * Gets the user data passed to dbus_connection_register_object_path()
5499 * or dbus_connection_register_fallback(). If nothing was registered
5500 * at this path, the data is filled in with #NULL.
5502 * @param connection the connection
5503 * @param path the path you registered with
5504 * @param data_p location to store the user data, or #NULL
5505 * @returns #FALSE if not enough memory
5508 dbus_connection_get_object_path_data (DBusConnection *connection,
5512 char **decomposed_path;
5514 _dbus_return_val_if_fail (connection != NULL, FALSE);
5515 _dbus_return_val_if_fail (path != NULL, FALSE);
5516 _dbus_return_val_if_fail (data_p != NULL, FALSE);
5520 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5523 CONNECTION_LOCK (connection);
5525 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
5527 CONNECTION_UNLOCK (connection);
5529 dbus_free_string_array (decomposed_path);
5535 * Lists the registered fallback handlers and object path handlers at
5536 * the given parent_path. The returned array should be freed with
5537 * dbus_free_string_array().
5539 * @param connection the connection
5540 * @param parent_path the path to list the child handlers of
5541 * @param child_entries returns #NULL-terminated array of children
5542 * @returns #FALSE if no memory to allocate the child entries
5545 dbus_connection_list_registered (DBusConnection *connection,
5546 const char *parent_path,
5547 char ***child_entries)
5549 char **decomposed_path;
5551 _dbus_return_val_if_fail (connection != NULL, FALSE);
5552 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
5553 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
5554 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
5556 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
5559 CONNECTION_LOCK (connection);
5561 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
5562 (const char **) decomposed_path,
5564 dbus_free_string_array (decomposed_path);
5569 static DBusDataSlotAllocator slot_allocator;
5570 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
5573 * Allocates an integer ID to be used for storing application-specific
5574 * data on any DBusConnection. The allocated ID may then be used
5575 * with dbus_connection_set_data() and dbus_connection_get_data().
5576 * The passed-in slot must be initialized to -1, and is filled in
5577 * with the slot ID. If the passed-in slot is not -1, it's assumed
5578 * to be already allocated, and its refcount is incremented.
5580 * The allocated slot is global, i.e. all DBusConnection objects will
5581 * have a slot with the given integer ID reserved.
5583 * @param slot_p address of a global variable storing the slot
5584 * @returns #FALSE on failure (no memory)
5587 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
5589 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5590 &_DBUS_LOCK_NAME (connection_slots),
5595 * Deallocates a global ID for connection data slots.
5596 * dbus_connection_get_data() and dbus_connection_set_data() may no
5597 * longer be used with this slot. Existing data stored on existing
5598 * DBusConnection objects will be freed when the connection is
5599 * finalized, but may not be retrieved (and may only be replaced if
5600 * someone else reallocates the slot). When the refcount on the
5601 * passed-in slot reaches 0, it is set to -1.
5603 * @param slot_p address storing the slot to deallocate
5606 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
5608 _dbus_return_if_fail (*slot_p >= 0);
5610 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5614 * Stores a pointer on a DBusConnection, along
5615 * with an optional function to be used for freeing
5616 * the data when the data is set again, or when
5617 * the connection is finalized. The slot number
5618 * must have been allocated with dbus_connection_allocate_data_slot().
5620 * @param connection the connection
5621 * @param slot the slot number
5622 * @param data the data to store
5623 * @param free_data_func finalizer function for the data
5624 * @returns #TRUE if there was enough memory to store the data
5627 dbus_connection_set_data (DBusConnection *connection,
5630 DBusFreeFunction free_data_func)
5632 DBusFreeFunction old_free_func;
5636 _dbus_return_val_if_fail (connection != NULL, FALSE);
5637 _dbus_return_val_if_fail (slot >= 0, FALSE);
5639 CONNECTION_LOCK (connection);
5641 retval = _dbus_data_slot_list_set (&slot_allocator,
5642 &connection->slot_list,
5643 slot, data, free_data_func,
5644 &old_free_func, &old_data);
5646 CONNECTION_UNLOCK (connection);
5650 /* Do the actual free outside the connection lock */
5652 (* old_free_func) (old_data);
5659 * Retrieves data previously set with dbus_connection_set_data().
5660 * The slot must still be allocated (must not have been freed).
5662 * @param connection the connection
5663 * @param slot the slot to get data from
5664 * @returns the data, or #NULL if not found
5667 dbus_connection_get_data (DBusConnection *connection,
5672 _dbus_return_val_if_fail (connection != NULL, NULL);
5674 CONNECTION_LOCK (connection);
5676 res = _dbus_data_slot_list_get (&slot_allocator,
5677 &connection->slot_list,
5680 CONNECTION_UNLOCK (connection);
5686 * This function sets a global flag for whether dbus_connection_new()
5687 * will set SIGPIPE behavior to SIG_IGN.
5689 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
5692 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
5694 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
5698 * Specifies the maximum size message this connection is allowed to
5699 * receive. Larger messages will result in disconnecting the
5702 * @param connection a #DBusConnection
5703 * @param size maximum message size the connection can receive, in bytes
5706 dbus_connection_set_max_message_size (DBusConnection *connection,
5709 _dbus_return_if_fail (connection != NULL);
5711 CONNECTION_LOCK (connection);
5712 _dbus_transport_set_max_message_size (connection->transport,
5714 CONNECTION_UNLOCK (connection);
5718 * Gets the value set by dbus_connection_set_max_message_size().
5720 * @param connection the connection
5721 * @returns the max size of a single message
5724 dbus_connection_get_max_message_size (DBusConnection *connection)
5728 _dbus_return_val_if_fail (connection != NULL, 0);
5730 CONNECTION_LOCK (connection);
5731 res = _dbus_transport_get_max_message_size (connection->transport);
5732 CONNECTION_UNLOCK (connection);
5737 * Sets the maximum total number of bytes that can be used for all messages
5738 * received on this connection. Messages count toward the maximum until
5739 * they are finalized. When the maximum is reached, the connection will
5740 * not read more data until some messages are finalized.
5742 * The semantics of the maximum are: if outstanding messages are
5743 * already above the maximum, additional messages will not be read.
5744 * The semantics are not: if the next message would cause us to exceed
5745 * the maximum, we don't read it. The reason is that we don't know the
5746 * size of a message until after we read it.
5748 * Thus, the max live messages size can actually be exceeded
5749 * by up to the maximum size of a single message.
5751 * Also, if we read say 1024 bytes off the wire in a single read(),
5752 * and that contains a half-dozen small messages, we may exceed the
5753 * size max by that amount. But this should be inconsequential.
5755 * This does imply that we can't call read() with a buffer larger
5756 * than we're willing to exceed this limit by.
5758 * @param connection the connection
5759 * @param size the maximum size in bytes of all outstanding messages
5762 dbus_connection_set_max_received_size (DBusConnection *connection,
5765 _dbus_return_if_fail (connection != NULL);
5767 CONNECTION_LOCK (connection);
5768 _dbus_transport_set_max_received_size (connection->transport,
5770 CONNECTION_UNLOCK (connection);
5774 * Gets the value set by dbus_connection_set_max_received_size().
5776 * @param connection the connection
5777 * @returns the max size of all live messages
5780 dbus_connection_get_max_received_size (DBusConnection *connection)
5784 _dbus_return_val_if_fail (connection != NULL, 0);
5786 CONNECTION_LOCK (connection);
5787 res = _dbus_transport_get_max_received_size (connection->transport);
5788 CONNECTION_UNLOCK (connection);
5793 * Gets the approximate size in bytes of all messages in the outgoing
5794 * message queue. The size is approximate in that you shouldn't use
5795 * it to decide how many bytes to read off the network or anything
5796 * of that nature, as optimizations may choose to tell small white lies
5797 * to avoid performance overhead.
5799 * @param connection the connection
5800 * @returns the number of bytes that have been queued up but not sent
5803 dbus_connection_get_outgoing_size (DBusConnection *connection)
5807 _dbus_return_val_if_fail (connection != NULL, 0);
5809 CONNECTION_LOCK (connection);
5810 res = _dbus_counter_get_value (connection->outgoing_counter);
5811 CONNECTION_UNLOCK (connection);