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-message-private.h"
37 #include "dbus-threads.h"
38 #include "dbus-protocol.h"
39 #include "dbus-dataslot.h"
40 #include "dbus-string.h"
41 #include "dbus-pending-call.h"
42 #include "dbus-object-tree.h"
43 #include "dbus-threads-internal.h"
45 #include "dbus-marshal-basic.h"
47 #ifdef DBUS_DISABLE_CHECKS
48 #define TOOK_LOCK_CHECK(connection)
49 #define RELEASING_LOCK_CHECK(connection)
50 #define HAVE_LOCK_CHECK(connection)
52 #define TOOK_LOCK_CHECK(connection) do { \
53 _dbus_assert (!(connection)->have_connection_lock); \
54 (connection)->have_connection_lock = TRUE; \
56 #define RELEASING_LOCK_CHECK(connection) do { \
57 _dbus_assert ((connection)->have_connection_lock); \
58 (connection)->have_connection_lock = FALSE; \
60 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
61 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
66 #define CONNECTION_LOCK(connection) do { \
67 if (TRACE_LOCKS) { _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); } \
68 _dbus_mutex_lock ((connection)->mutex); \
69 TOOK_LOCK_CHECK (connection); \
72 #define CONNECTION_UNLOCK(connection) do { \
73 if (TRACE_LOCKS) { _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); } \
74 RELEASING_LOCK_CHECK (connection); \
75 _dbus_mutex_unlock ((connection)->mutex); \
78 #define DISPATCH_STATUS_NAME(s) \
79 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
80 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
81 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
85 * @defgroup DBusConnection DBusConnection
87 * @brief Connection to another application
89 * A DBusConnection represents a connection to another
90 * application. Messages can be sent and received via this connection.
91 * The other application may be a message bus; for convenience, the
92 * function dbus_bus_get() is provided to automatically open a
93 * connection to the well-known message buses.
95 * In brief a DBusConnection is a message queue associated with some
96 * message transport mechanism such as a socket. The connection
97 * maintains a queue of incoming messages and a queue of outgoing
100 * Several functions use the following terms:
102 * <li><b>read</b> means to fill the incoming message queue by reading from the socket</li>
103 * <li><b>write</b> means to drain the outgoing queue by writing to the socket</li>
104 * <li><b>dispatch</b> means to drain the incoming queue by invoking application-provided message handlers</li>
107 * The function dbus_connection_read_write_dispatch() for example does all
108 * three of these things, offering a simple alternative to a main loop.
110 * In an application with a main loop, the read/write/dispatch
111 * operations are usually separate.
113 * The connection provides #DBusWatch and #DBusTimeout objects to
114 * the main loop. These are used to know when reading, writing, or
115 * dispatching should be performed.
117 * Incoming messages are processed
118 * by calling dbus_connection_dispatch(). dbus_connection_dispatch()
119 * runs any handlers registered for the topmost message in the message
120 * queue, then discards the message, then returns.
122 * dbus_connection_get_dispatch_status() indicates whether
123 * messages are currently in the queue that need dispatching.
124 * dbus_connection_set_dispatch_status_function() allows
125 * you to set a function to be used to monitor the dispatch status.
127 * If you're using GLib or Qt add-on libraries for D-Bus, there are
128 * special convenience APIs in those libraries that hide
129 * all the details of dispatch and watch/timeout monitoring.
130 * For example, dbus_connection_setup_with_g_main().
132 * If you aren't using these add-on libraries, but want to process
133 * messages asynchronously, you must manually call
134 * dbus_connection_set_dispatch_status_function(),
135 * dbus_connection_set_watch_functions(),
136 * dbus_connection_set_timeout_functions() providing appropriate
137 * functions to integrate the connection with your application's main
138 * loop. This can be tricky to get right; main loops are not simple.
140 * If you don't need to be asynchronous, you can ignore #DBusWatch,
141 * #DBusTimeout, and dbus_connection_dispatch(). Instead,
142 * dbus_connection_read_write_dispatch() can be used.
144 * Or, in <em>very</em> simple applications,
145 * dbus_connection_pop_message() may be all you need, allowing you to
146 * avoid setting up any handler functions (see
147 * dbus_connection_add_filter(),
148 * dbus_connection_register_object_path() for more on handlers).
150 * When you use dbus_connection_send() or one of its variants to send
151 * a message, the message is added to the outgoing queue. It's
152 * actually written to the network later; either in
153 * dbus_watch_handle() invoked by your main loop, or in
154 * dbus_connection_flush() which blocks until it can write out the
155 * entire outgoing queue. The GLib/Qt add-on libraries again
156 * handle the details here for you by setting up watch functions.
158 * When a connection is disconnected, you are guaranteed to get a
159 * signal "Disconnected" from the interface
160 * #DBUS_INTERFACE_LOCAL, path
163 * You may not drop the last reference to a #DBusConnection
164 * until that connection has been disconnected.
166 * You may dispatch the unprocessed incoming message queue even if the
167 * connection is disconnected. However, "Disconnected" will always be
168 * the last message in the queue (obviously no messages are received
169 * after disconnection).
171 * After calling dbus_threads_init(), #DBusConnection has thread
172 * locks and drops them when invoking user callbacks, so in general is
173 * transparently threadsafe. However, #DBusMessage does NOT have
174 * thread locks; you must not send the same message to multiple
175 * #DBusConnection if those connections will be used from different threads,
178 * Also, if you dispatch or pop messages from multiple threads, it
179 * may work in the sense that it won't crash, but it's tough to imagine
180 * sane results; it will be completely unpredictable which messages
181 * go to which threads.
183 * It's recommended to dispatch from a single thread.
185 * The most useful function to call from multiple threads at once
186 * is dbus_connection_send_with_reply_and_block(). That is,
187 * multiple threads can make method calls at the same time.
189 * If you aren't using threads, you can use a main loop and
190 * dbus_pending_call_set_notify() to achieve a similar result.
194 * @defgroup DBusConnectionInternals DBusConnection implementation details
195 * @ingroup DBusInternals
196 * @brief Implementation details of DBusConnection
202 * Internal struct representing a message filter function
204 typedef struct DBusMessageFilter DBusMessageFilter;
207 * Internal struct representing a message filter function
209 struct DBusMessageFilter
211 DBusAtomic refcount; /**< Reference count */
212 DBusHandleMessageFunction function; /**< Function to call to filter */
213 void *user_data; /**< User data for the function */
214 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
219 * Internals of DBusPreallocatedSend
221 struct DBusPreallocatedSend
223 DBusConnection *connection; /**< Connection we'd send the message to */
224 DBusList *queue_link; /**< Preallocated link in the queue */
225 DBusList *counter_link; /**< Preallocated link in the resource counter */
228 #ifdef HAVE_DECL_MSG_NOSIGNAL
229 static dbus_bool_t _dbus_modify_sigpipe = FALSE;
231 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
235 * Implementation details of DBusConnection. All fields are private.
237 struct DBusConnection
239 DBusAtomic refcount; /**< Reference count. */
241 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
243 DBusMutex *dispatch_mutex; /**< Protects dispatch_acquired */
244 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */
245 DBusMutex *io_path_mutex; /**< Protects io_path_acquired */
246 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */
248 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
249 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
251 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
252 * dispatch_acquired will be set by the borrower
255 int n_outgoing; /**< Length of outgoing queue. */
256 int n_incoming; /**< Length of incoming queue. */
258 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
260 DBusTransport *transport; /**< Object that sends/receives messages over network. */
261 DBusWatchList *watches; /**< Stores active watches. */
262 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
264 DBusList *filter_list; /**< List of filters. */
266 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
268 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
270 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
271 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
273 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
274 void *wakeup_main_data; /**< Application data for wakeup_main_function */
275 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
277 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
278 void *dispatch_status_data; /**< Application data for dispatch_status_function */
279 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
281 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
283 DBusList *link_cache; /**< A cache of linked list links to prevent contention
284 * for the global linked list mempool lock
286 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
288 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
290 /* These two MUST be bools and not bitfields, because they are protected by a separate lock
291 * from connection->mutex and all bitfields in a word have to be read/written together.
292 * So you can't have a different lock for different bitfields in the same word.
294 dbus_bool_t dispatch_acquired; /**< Someone has dispatch path (can drain incoming queue) */
295 dbus_bool_t io_path_acquired; /**< Someone has transport io path (can use the transport to read/write messages) */
297 unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */
299 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
301 unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */
303 unsigned int disconnected_message_arrived : 1; /**< We popped or are dispatching the disconnected message.
304 * if the disconnect_message_link is NULL then we queued it, but
305 * this flag is whether it got to the head of the queue.
307 unsigned int disconnected_message_processed : 1; /**< We did our default handling of the disconnected message,
308 * such as closing the connection.
311 #ifndef DBUS_DISABLE_CHECKS
312 unsigned int have_connection_lock : 1; /**< Used to check locking */
315 #ifndef DBUS_DISABLE_CHECKS
316 int generation; /**< _dbus_current_generation that should correspond to this connection */
320 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
321 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
322 DBusDispatchStatus new_status);
323 static void _dbus_connection_last_unref (DBusConnection *connection);
324 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
325 static void _dbus_connection_release_dispatch (DBusConnection *connection);
326 static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
327 static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection);
328 static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection);
330 static DBusMessageFilter *
331 _dbus_message_filter_ref (DBusMessageFilter *filter)
333 _dbus_assert (filter->refcount.value > 0);
334 _dbus_atomic_inc (&filter->refcount);
340 _dbus_message_filter_unref (DBusMessageFilter *filter)
342 _dbus_assert (filter->refcount.value > 0);
344 if (_dbus_atomic_dec (&filter->refcount) == 1)
346 if (filter->free_user_data_function)
347 (* filter->free_user_data_function) (filter->user_data);
354 * Acquires the connection lock.
356 * @param connection the connection.
359 _dbus_connection_lock (DBusConnection *connection)
361 CONNECTION_LOCK (connection);
365 * Releases the connection lock.
367 * @param connection the connection.
370 _dbus_connection_unlock (DBusConnection *connection)
372 CONNECTION_UNLOCK (connection);
376 * Wakes up the main loop if it is sleeping
377 * Needed if we're e.g. queueing outgoing messages
378 * on a thread while the mainloop sleeps.
380 * @param connection the connection.
383 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
385 if (connection->wakeup_main_function)
386 (*connection->wakeup_main_function) (connection->wakeup_main_data);
389 #ifdef DBUS_BUILD_TESTS
390 /* For now this function isn't used */
392 * Adds a message to the incoming message queue, returning #FALSE
393 * if there's insufficient memory to queue the message.
394 * Does not take over refcount of the message.
396 * @param connection the connection.
397 * @param message the message to queue.
398 * @returns #TRUE on success.
401 _dbus_connection_queue_received_message (DBusConnection *connection,
402 DBusMessage *message)
406 link = _dbus_list_alloc_link (message);
410 dbus_message_ref (message);
411 _dbus_connection_queue_received_message_link (connection, link);
417 * Gets the locks so we can examine them
419 * @param connection the connection.
420 * @param mutex_loc return for the location of the main mutex pointer
421 * @param dispatch_mutex_loc return location of the dispatch mutex pointer
422 * @param io_path_mutex_loc return location of the io_path mutex pointer
423 * @param dispatch_cond_loc return location of the dispatch conditional
425 * @param io_path_cond_loc return location of the io_path conditional
429 _dbus_connection_test_get_locks (DBusConnection *connection,
430 DBusMutex **mutex_loc,
431 DBusMutex **dispatch_mutex_loc,
432 DBusMutex **io_path_mutex_loc,
433 DBusCondVar **dispatch_cond_loc,
434 DBusCondVar **io_path_cond_loc)
436 *mutex_loc = connection->mutex;
437 *dispatch_mutex_loc = connection->dispatch_mutex;
438 *io_path_mutex_loc = connection->io_path_mutex;
439 *dispatch_cond_loc = connection->dispatch_cond;
440 *io_path_cond_loc = connection->io_path_cond;
445 * Adds a message-containing list link to the incoming message queue,
446 * taking ownership of the link and the message's current refcount.
447 * Cannot fail due to lack of memory.
449 * @param connection the connection.
450 * @param link the message link to queue.
453 _dbus_connection_queue_received_message_link (DBusConnection *connection,
456 DBusPendingCall *pending;
457 dbus_uint32_t reply_serial;
458 DBusMessage *message;
460 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
462 _dbus_list_append_link (&connection->incoming_messages,
464 message = link->data;
466 /* If this is a reply we're waiting on, remove timeout for it */
467 reply_serial = dbus_message_get_reply_serial (message);
468 if (reply_serial != 0)
470 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
474 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
475 _dbus_connection_remove_timeout_unlocked (connection,
476 _dbus_pending_call_get_timeout_unlocked (pending));
478 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
484 connection->n_incoming += 1;
486 _dbus_connection_wakeup_mainloop (connection);
488 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
490 dbus_message_get_type (message),
491 dbus_message_get_path (message) ?
492 dbus_message_get_path (message) :
494 dbus_message_get_interface (message) ?
495 dbus_message_get_interface (message) :
497 dbus_message_get_member (message) ?
498 dbus_message_get_member (message) :
500 dbus_message_get_signature (message),
501 dbus_message_get_reply_serial (message),
503 connection->n_incoming);}
506 * Adds a link + message to the incoming message queue.
507 * Can't fail. Takes ownership of both link and message.
509 * @param connection the connection.
510 * @param link the list node and message to queue.
514 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
517 HAVE_LOCK_CHECK (connection);
519 _dbus_list_append_link (&connection->incoming_messages, link);
521 connection->n_incoming += 1;
523 _dbus_connection_wakeup_mainloop (connection);
525 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
526 link->data, connection, connection->n_incoming);
531 * Checks whether there are messages in the outgoing message queue.
532 * Called with connection lock held.
534 * @param connection the connection.
535 * @returns #TRUE if the outgoing queue is non-empty.
538 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
540 HAVE_LOCK_CHECK (connection);
541 return connection->outgoing_messages != NULL;
545 * Checks whether there are messages in the outgoing message queue.
546 * Use dbus_connection_flush() to block until all outgoing
547 * messages have been written to the underlying transport
548 * (such as a socket).
550 * @param connection the connection.
551 * @returns #TRUE if the outgoing queue is non-empty.
554 dbus_connection_has_messages_to_send (DBusConnection *connection)
558 _dbus_return_val_if_fail (connection != NULL, FALSE);
560 CONNECTION_LOCK (connection);
561 v = _dbus_connection_has_messages_to_send_unlocked (connection);
562 CONNECTION_UNLOCK (connection);
568 * Gets the next outgoing message. The message remains in the
569 * queue, and the caller does not own a reference to it.
571 * @param connection the connection.
572 * @returns the message to be sent.
575 _dbus_connection_get_message_to_send (DBusConnection *connection)
577 HAVE_LOCK_CHECK (connection);
579 return _dbus_list_get_last (&connection->outgoing_messages);
583 * Notifies the connection that a message has been sent, so the
584 * message can be removed from the outgoing queue.
585 * Called with the connection lock held.
587 * @param connection the connection.
588 * @param message the message that was sent.
591 _dbus_connection_message_sent (DBusConnection *connection,
592 DBusMessage *message)
596 HAVE_LOCK_CHECK (connection);
598 /* This can be called before we even complete authentication, since
599 * it's called on disconnect to clean up the outgoing queue.
600 * It's also called as we successfully send each message.
603 link = _dbus_list_get_last_link (&connection->outgoing_messages);
604 _dbus_assert (link != NULL);
605 _dbus_assert (link->data == message);
607 /* Save this link in the link cache */
608 _dbus_list_unlink (&connection->outgoing_messages,
610 _dbus_list_prepend_link (&connection->link_cache, link);
612 connection->n_outgoing -= 1;
614 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
616 dbus_message_get_type (message),
617 dbus_message_get_path (message) ?
618 dbus_message_get_path (message) :
620 dbus_message_get_interface (message) ?
621 dbus_message_get_interface (message) :
623 dbus_message_get_member (message) ?
624 dbus_message_get_member (message) :
626 dbus_message_get_signature (message),
627 connection, connection->n_outgoing);
629 /* Save this link in the link cache also */
630 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
632 _dbus_list_prepend_link (&connection->link_cache, link);
634 dbus_message_unref (message);
637 /** Function to be called in protected_change_watch() with refcount held */
638 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
640 /** Function to be called in protected_change_watch() with refcount held */
641 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
643 /** Function to be called in protected_change_watch() with refcount held */
644 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
646 dbus_bool_t enabled);
649 protected_change_watch (DBusConnection *connection,
651 DBusWatchAddFunction add_function,
652 DBusWatchRemoveFunction remove_function,
653 DBusWatchToggleFunction toggle_function,
656 DBusWatchList *watches;
659 HAVE_LOCK_CHECK (connection);
661 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
662 * drop lock and call out" one; but it has to be propagated up through all callers
665 watches = connection->watches;
668 connection->watches = NULL;
669 _dbus_connection_ref_unlocked (connection);
670 CONNECTION_UNLOCK (connection);
673 retval = (* add_function) (watches, watch);
674 else if (remove_function)
677 (* remove_function) (watches, watch);
682 (* toggle_function) (watches, watch, enabled);
685 CONNECTION_LOCK (connection);
686 connection->watches = watches;
687 _dbus_connection_unref_unlocked (connection);
697 * Adds a watch using the connection's DBusAddWatchFunction if
698 * available. Otherwise records the watch to be added when said
699 * function is available. Also re-adds the watch if the
700 * DBusAddWatchFunction changes. May fail due to lack of memory.
701 * Connection lock should be held when calling this.
703 * @param connection the connection.
704 * @param watch the watch to add.
705 * @returns #TRUE on success.
708 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
711 return protected_change_watch (connection, watch,
712 _dbus_watch_list_add_watch,
717 * Removes a watch using the connection's DBusRemoveWatchFunction
718 * if available. It's an error to call this function on a watch
719 * that was not previously added.
720 * Connection lock should be held when calling this.
722 * @param connection the connection.
723 * @param watch the watch to remove.
726 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
729 protected_change_watch (connection, watch,
731 _dbus_watch_list_remove_watch,
736 * Toggles a watch and notifies app via connection's
737 * DBusWatchToggledFunction if available. It's an error to call this
738 * function on a watch that was not previously added.
739 * Connection lock should be held when calling this.
741 * @param connection the connection.
742 * @param watch the watch to toggle.
743 * @param enabled whether to enable or disable
746 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
750 _dbus_assert (watch != NULL);
752 protected_change_watch (connection, watch,
754 _dbus_watch_list_toggle_watch,
758 /** Function to be called in protected_change_timeout() with refcount held */
759 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
760 DBusTimeout *timeout);
761 /** Function to be called in protected_change_timeout() with refcount held */
762 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
763 DBusTimeout *timeout);
764 /** Function to be called in protected_change_timeout() with refcount held */
765 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
766 DBusTimeout *timeout,
767 dbus_bool_t enabled);
770 protected_change_timeout (DBusConnection *connection,
771 DBusTimeout *timeout,
772 DBusTimeoutAddFunction add_function,
773 DBusTimeoutRemoveFunction remove_function,
774 DBusTimeoutToggleFunction toggle_function,
777 DBusTimeoutList *timeouts;
780 HAVE_LOCK_CHECK (connection);
782 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
783 * drop lock and call out" one; but it has to be propagated up through all callers
786 timeouts = connection->timeouts;
789 connection->timeouts = NULL;
790 _dbus_connection_ref_unlocked (connection);
791 CONNECTION_UNLOCK (connection);
794 retval = (* add_function) (timeouts, timeout);
795 else if (remove_function)
798 (* remove_function) (timeouts, timeout);
803 (* toggle_function) (timeouts, timeout, enabled);
806 CONNECTION_LOCK (connection);
807 connection->timeouts = timeouts;
808 _dbus_connection_unref_unlocked (connection);
817 * Adds a timeout using the connection's DBusAddTimeoutFunction if
818 * available. Otherwise records the timeout to be added when said
819 * function is available. Also re-adds the timeout if the
820 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
821 * The timeout will fire repeatedly until removed.
822 * Connection lock should be held when calling this.
824 * @param connection the connection.
825 * @param timeout the timeout to add.
826 * @returns #TRUE on success.
829 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
830 DBusTimeout *timeout)
832 return protected_change_timeout (connection, timeout,
833 _dbus_timeout_list_add_timeout,
838 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
839 * if available. It's an error to call this function on a timeout
840 * that was not previously added.
841 * Connection lock should be held when calling this.
843 * @param connection the connection.
844 * @param timeout the timeout to remove.
847 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
848 DBusTimeout *timeout)
850 protected_change_timeout (connection, timeout,
852 _dbus_timeout_list_remove_timeout,
857 * Toggles a timeout and notifies app via connection's
858 * DBusTimeoutToggledFunction if available. It's an error to call this
859 * function on a timeout that was not previously added.
860 * Connection lock should be held when calling this.
862 * @param connection the connection.
863 * @param timeout the timeout to toggle.
864 * @param enabled whether to enable or disable
867 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
868 DBusTimeout *timeout,
871 protected_change_timeout (connection, timeout,
873 _dbus_timeout_list_toggle_timeout,
878 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
879 DBusPendingCall *pending)
881 dbus_uint32_t reply_serial;
882 DBusTimeout *timeout;
884 HAVE_LOCK_CHECK (connection);
886 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
888 _dbus_assert (reply_serial != 0);
890 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
892 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
895 if (!_dbus_hash_table_insert_int (connection->pending_replies,
899 _dbus_connection_remove_timeout_unlocked (connection, timeout);
901 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
902 HAVE_LOCK_CHECK (connection);
906 _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
908 _dbus_pending_call_ref_unlocked (pending);
910 HAVE_LOCK_CHECK (connection);
916 free_pending_call_on_hash_removal (void *data)
918 DBusPendingCall *pending;
919 DBusConnection *connection;
926 connection = _dbus_pending_call_get_connection_unlocked (pending);
928 HAVE_LOCK_CHECK (connection);
930 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
932 _dbus_connection_remove_timeout_unlocked (connection,
933 _dbus_pending_call_get_timeout_unlocked (pending));
935 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
938 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
939 * here, but the pending call finalizer could in principle call out to
940 * application code so we pretty much have to... some larger code reorg
943 _dbus_connection_ref_unlocked (connection);
944 _dbus_pending_call_unref_and_unlock (pending);
945 CONNECTION_LOCK (connection);
946 _dbus_connection_unref_unlocked (connection);
950 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
951 DBusPendingCall *pending)
953 /* This ends up unlocking to call the pending call finalizer, which is unexpected to
956 _dbus_hash_table_remove_int (connection->pending_replies,
957 _dbus_pending_call_get_reply_serial_unlocked (pending));
961 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
962 DBusPendingCall *pending)
964 /* The idea here is to avoid finalizing the pending call
965 * with the lock held, since there's a destroy notifier
966 * in pending call that goes out to application code.
968 * There's an extra unlock inside the hash table
969 * "free pending call" function FIXME...
971 _dbus_pending_call_ref_unlocked (pending);
972 _dbus_hash_table_remove_int (connection->pending_replies,
973 _dbus_pending_call_get_reply_serial_unlocked (pending));
975 if (_dbus_pending_call_is_timeout_added_unlocked (pending))
976 _dbus_connection_remove_timeout_unlocked (connection,
977 _dbus_pending_call_get_timeout_unlocked (pending));
979 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
981 _dbus_pending_call_unref_and_unlock (pending);
985 * Removes a pending call from the connection, such that
986 * the pending reply will be ignored. May drop the last
987 * reference to the pending call.
989 * @param connection the connection
990 * @param pending the pending call
993 _dbus_connection_remove_pending_call (DBusConnection *connection,
994 DBusPendingCall *pending)
996 CONNECTION_LOCK (connection);
997 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
1001 * Acquire the transporter I/O path. This must be done before
1002 * doing any I/O in the transporter. May sleep and drop the
1003 * IO path mutex while waiting for the I/O path.
1005 * @param connection the connection.
1006 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1007 * @returns TRUE if the I/O path was acquired.
1010 _dbus_connection_acquire_io_path (DBusConnection *connection,
1011 int timeout_milliseconds)
1013 dbus_bool_t we_acquired;
1015 HAVE_LOCK_CHECK (connection);
1017 /* We don't want the connection to vanish */
1018 _dbus_connection_ref_unlocked (connection);
1020 /* We will only touch io_path_acquired which is protected by our mutex */
1021 CONNECTION_UNLOCK (connection);
1023 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1024 _dbus_mutex_lock (connection->io_path_mutex);
1026 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
1027 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
1029 we_acquired = FALSE;
1031 if (connection->io_path_acquired)
1033 if (timeout_milliseconds != -1)
1035 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
1036 _DBUS_FUNCTION_NAME, timeout_milliseconds);
1038 if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1039 connection->io_path_mutex,
1040 timeout_milliseconds))
1042 /* We timed out before anyone signaled. */
1043 /* (writing the loop to handle the !timedout case by
1044 * waiting longer if needed is a pain since dbus
1045 * wraps pthread_cond_timedwait to take a relative
1046 * time instead of absolute, something kind of stupid
1047 * on our part. for now it doesn't matter, we will just
1048 * end up back here eventually.)
1054 while (connection->io_path_acquired)
1056 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
1057 _dbus_condvar_wait (connection->io_path_cond,
1058 connection->io_path_mutex);
1063 if (!connection->io_path_acquired)
1066 connection->io_path_acquired = TRUE;
1069 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
1070 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
1072 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1073 _dbus_mutex_unlock (connection->io_path_mutex);
1075 CONNECTION_LOCK (connection);
1077 HAVE_LOCK_CHECK (connection);
1079 _dbus_connection_unref_unlocked (connection);
1085 * Release the I/O path when you're done with it. Only call
1086 * after you've acquired the I/O. Wakes up at most one thread
1087 * currently waiting to acquire the I/O path.
1089 * @param connection the connection.
1092 _dbus_connection_release_io_path (DBusConnection *connection)
1094 HAVE_LOCK_CHECK (connection);
1096 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1097 _dbus_mutex_lock (connection->io_path_mutex);
1099 _dbus_assert (connection->io_path_acquired);
1101 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
1102 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
1104 connection->io_path_acquired = FALSE;
1105 _dbus_condvar_wake_one (connection->io_path_cond);
1107 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
1108 _dbus_mutex_unlock (connection->io_path_mutex);
1112 * Queues incoming messages and sends outgoing messages for this
1113 * connection, optionally blocking in the process. Each call to
1114 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
1115 * time and then read or write data if possible.
1117 * The purpose of this function is to be able to flush outgoing
1118 * messages or queue up incoming messages without returning
1119 * control to the application and causing reentrancy weirdness.
1121 * The flags parameter allows you to specify whether to
1122 * read incoming messages, write outgoing messages, or both,
1123 * and whether to block if no immediate action is possible.
1125 * The timeout_milliseconds parameter does nothing unless the
1126 * iteration is blocking.
1128 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1129 * wasn't specified, then it's impossible to block, even if
1130 * you specify DBUS_ITERATION_BLOCK; in that case the function
1131 * returns immediately.
1133 * Called with connection lock held.
1135 * @param connection the connection.
1136 * @param flags iteration flags.
1137 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1140 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1142 int timeout_milliseconds)
1144 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1146 HAVE_LOCK_CHECK (connection);
1148 if (connection->n_outgoing == 0)
1149 flags &= ~DBUS_ITERATION_DO_WRITING;
1151 if (_dbus_connection_acquire_io_path (connection,
1152 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1154 HAVE_LOCK_CHECK (connection);
1156 _dbus_transport_do_iteration (connection->transport,
1157 flags, timeout_milliseconds);
1158 _dbus_connection_release_io_path (connection);
1161 HAVE_LOCK_CHECK (connection);
1163 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1167 * Creates a new connection for the given transport. A transport
1168 * represents a message stream that uses some concrete mechanism, such
1169 * as UNIX domain sockets. May return #NULL if insufficient
1170 * memory exists to create the connection.
1172 * @param transport the transport.
1173 * @returns the new connection, or #NULL on failure.
1176 _dbus_connection_new_for_transport (DBusTransport *transport)
1178 DBusConnection *connection;
1179 DBusWatchList *watch_list;
1180 DBusTimeoutList *timeout_list;
1181 DBusHashTable *pending_replies;
1182 DBusList *disconnect_link;
1183 DBusMessage *disconnect_message;
1184 DBusCounter *outgoing_counter;
1185 DBusObjectTree *objects;
1189 pending_replies = NULL;
1190 timeout_list = NULL;
1191 disconnect_link = NULL;
1192 disconnect_message = NULL;
1193 outgoing_counter = NULL;
1196 watch_list = _dbus_watch_list_new ();
1197 if (watch_list == NULL)
1200 timeout_list = _dbus_timeout_list_new ();
1201 if (timeout_list == NULL)
1205 _dbus_hash_table_new (DBUS_HASH_INT,
1207 (DBusFreeFunction)free_pending_call_on_hash_removal);
1208 if (pending_replies == NULL)
1211 connection = dbus_new0 (DBusConnection, 1);
1212 if (connection == NULL)
1215 _dbus_mutex_new_at_location (&connection->mutex);
1216 if (connection->mutex == NULL)
1219 _dbus_mutex_new_at_location (&connection->io_path_mutex);
1220 if (connection->io_path_mutex == NULL)
1223 _dbus_mutex_new_at_location (&connection->dispatch_mutex);
1224 if (connection->dispatch_mutex == NULL)
1227 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1228 if (connection->dispatch_cond == NULL)
1231 _dbus_condvar_new_at_location (&connection->io_path_cond);
1232 if (connection->io_path_cond == NULL)
1235 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1236 DBUS_INTERFACE_LOCAL,
1239 if (disconnect_message == NULL)
1242 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1243 if (disconnect_link == NULL)
1246 outgoing_counter = _dbus_counter_new ();
1247 if (outgoing_counter == NULL)
1250 objects = _dbus_object_tree_new (connection);
1251 if (objects == NULL)
1254 if (_dbus_modify_sigpipe)
1255 _dbus_disable_sigpipe ();
1257 connection->refcount.value = 1;
1258 connection->transport = transport;
1259 connection->watches = watch_list;
1260 connection->timeouts = timeout_list;
1261 connection->pending_replies = pending_replies;
1262 connection->outgoing_counter = outgoing_counter;
1263 connection->filter_list = NULL;
1264 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1265 connection->objects = objects;
1266 connection->exit_on_disconnect = FALSE;
1267 connection->shareable = FALSE;
1268 connection->route_peer_messages = FALSE;
1269 connection->disconnected_message_arrived = FALSE;
1270 connection->disconnected_message_processed = FALSE;
1272 #ifndef DBUS_DISABLE_CHECKS
1273 connection->generation = _dbus_current_generation;
1276 _dbus_data_slot_list_init (&connection->slot_list);
1278 connection->client_serial = 1;
1280 connection->disconnect_message_link = disconnect_link;
1282 CONNECTION_LOCK (connection);
1284 if (!_dbus_transport_set_connection (transport, connection))
1286 CONNECTION_UNLOCK (connection);
1291 _dbus_transport_ref (transport);
1293 CONNECTION_UNLOCK (connection);
1298 if (disconnect_message != NULL)
1299 dbus_message_unref (disconnect_message);
1301 if (disconnect_link != NULL)
1302 _dbus_list_free_link (disconnect_link);
1304 if (connection != NULL)
1306 _dbus_condvar_free_at_location (&connection->io_path_cond);
1307 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1308 _dbus_mutex_free_at_location (&connection->mutex);
1309 _dbus_mutex_free_at_location (&connection->io_path_mutex);
1310 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
1311 dbus_free (connection);
1313 if (pending_replies)
1314 _dbus_hash_table_unref (pending_replies);
1317 _dbus_watch_list_free (watch_list);
1320 _dbus_timeout_list_free (timeout_list);
1322 if (outgoing_counter)
1323 _dbus_counter_unref (outgoing_counter);
1326 _dbus_object_tree_unref (objects);
1332 * Increments the reference count of a DBusConnection.
1333 * Requires that the caller already holds the connection lock.
1335 * @param connection the connection.
1336 * @returns the connection.
1339 _dbus_connection_ref_unlocked (DBusConnection *connection)
1341 _dbus_assert (connection != NULL);
1342 _dbus_assert (connection->generation == _dbus_current_generation);
1344 HAVE_LOCK_CHECK (connection);
1346 #ifdef DBUS_HAVE_ATOMIC_INT
1347 _dbus_atomic_inc (&connection->refcount);
1349 _dbus_assert (connection->refcount.value > 0);
1350 connection->refcount.value += 1;
1357 * Decrements the reference count of a DBusConnection.
1358 * Requires that the caller already holds the connection lock.
1360 * @param connection the connection.
1363 _dbus_connection_unref_unlocked (DBusConnection *connection)
1365 dbus_bool_t last_unref;
1367 HAVE_LOCK_CHECK (connection);
1369 _dbus_assert (connection != NULL);
1371 /* The connection lock is better than the global
1372 * lock in the atomic increment fallback
1375 #ifdef DBUS_HAVE_ATOMIC_INT
1376 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1378 _dbus_assert (connection->refcount.value > 0);
1380 connection->refcount.value -= 1;
1381 last_unref = (connection->refcount.value == 0);
1383 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1388 _dbus_connection_last_unref (connection);
1391 static dbus_uint32_t
1392 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1394 dbus_uint32_t serial;
1396 serial = connection->client_serial++;
1398 if (connection->client_serial == 0)
1399 connection->client_serial = 1;
1405 * A callback for use with dbus_watch_new() to create a DBusWatch.
1407 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1408 * and the virtual handle_watch in DBusTransport if we got rid of it.
1409 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1412 * @param watch the watch.
1413 * @param condition the current condition of the file descriptors being watched.
1414 * @param data must be a pointer to a #DBusConnection
1415 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1418 _dbus_connection_handle_watch (DBusWatch *watch,
1419 unsigned int condition,
1422 DBusConnection *connection;
1424 DBusDispatchStatus status;
1428 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1430 CONNECTION_LOCK (connection);
1431 _dbus_connection_acquire_io_path (connection, -1);
1432 HAVE_LOCK_CHECK (connection);
1433 retval = _dbus_transport_handle_watch (connection->transport,
1436 _dbus_connection_release_io_path (connection);
1438 HAVE_LOCK_CHECK (connection);
1440 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1442 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1444 /* this calls out to user code */
1445 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1447 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1452 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1453 static DBusHashTable *shared_connections = NULL;
1454 static DBusList *shared_connections_no_guid = NULL;
1457 close_connection_on_shutdown (DBusConnection *connection)
1459 DBusMessage *message;
1461 dbus_connection_ref (connection);
1462 _dbus_connection_close_possibly_shared (connection);
1464 /* Churn through to the Disconnected message */
1465 while ((message = dbus_connection_pop_message (connection)))
1467 dbus_message_unref (message);
1469 dbus_connection_unref (connection);
1473 shared_connections_shutdown (void *data)
1477 _DBUS_LOCK (shared_connections);
1479 /* This is a little bit unpleasant... better ideas? */
1480 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
1482 DBusConnection *connection;
1485 _dbus_hash_iter_init (shared_connections, &iter);
1486 _dbus_hash_iter_next (&iter);
1488 connection = _dbus_hash_iter_get_value (&iter);
1490 _DBUS_UNLOCK (shared_connections);
1491 close_connection_on_shutdown (connection);
1492 _DBUS_LOCK (shared_connections);
1494 /* The connection should now be dead and not in our hash ... */
1495 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
1498 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1500 _dbus_hash_table_unref (shared_connections);
1501 shared_connections = NULL;
1503 if (shared_connections_no_guid != NULL)
1505 DBusConnection *connection;
1506 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1507 while (connection != NULL)
1509 _DBUS_UNLOCK (shared_connections);
1510 close_connection_on_shutdown (connection);
1511 _DBUS_LOCK (shared_connections);
1512 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1516 shared_connections_no_guid = NULL;
1518 _DBUS_UNLOCK (shared_connections);
1522 connection_lookup_shared (DBusAddressEntry *entry,
1523 DBusConnection **result)
1525 _dbus_verbose ("checking for existing connection\n");
1529 _DBUS_LOCK (shared_connections);
1531 if (shared_connections == NULL)
1533 _dbus_verbose ("creating shared_connections hash table\n");
1535 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1538 if (shared_connections == NULL)
1540 _DBUS_UNLOCK (shared_connections);
1544 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1546 _dbus_hash_table_unref (shared_connections);
1547 shared_connections = NULL;
1548 _DBUS_UNLOCK (shared_connections);
1552 _dbus_verbose (" successfully created shared_connections\n");
1554 _DBUS_UNLOCK (shared_connections);
1555 return TRUE; /* no point looking up in the hash we just made */
1561 guid = dbus_address_entry_get_value (entry, "guid");
1565 DBusConnection *connection;
1567 connection = _dbus_hash_table_lookup_string (shared_connections,
1572 /* The DBusConnection can't be finalized without taking
1573 * the shared_connections lock to remove it from the
1574 * hash. So it's safe to ref the connection here.
1575 * However, it may be disconnected if the Disconnected
1576 * message hasn't been processed yet, in which case we
1577 * want to pretend it isn't in the hash and avoid
1580 * The idea is to avoid ever returning a disconnected connection
1581 * from dbus_connection_open(). We could just synchronously
1582 * drop our shared ref to the connection on connection disconnect,
1583 * and then assert here that the connection is connected, but
1584 * that causes reentrancy headaches.
1586 CONNECTION_LOCK (connection);
1587 if (_dbus_connection_get_is_connected_unlocked (connection))
1589 _dbus_connection_ref_unlocked (connection);
1590 *result = connection;
1591 _dbus_verbose ("looked up existing connection to server guid %s\n",
1596 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1599 CONNECTION_UNLOCK (connection);
1603 _DBUS_UNLOCK (shared_connections);
1609 connection_record_shared_unlocked (DBusConnection *connection,
1613 char *guid_in_connection;
1615 HAVE_LOCK_CHECK (connection);
1616 _dbus_assert (connection->server_guid == NULL);
1617 _dbus_assert (connection->shareable);
1619 /* get a hard ref on this connection, even if
1620 * we won't in fact store it in the hash, we still
1621 * need to hold a ref on it until it's disconnected.
1623 _dbus_connection_ref_unlocked (connection);
1627 _DBUS_LOCK (shared_connections);
1629 if (!_dbus_list_prepend (&shared_connections_no_guid, connection))
1631 _DBUS_UNLOCK (shared_connections);
1635 _DBUS_UNLOCK (shared_connections);
1636 return TRUE; /* don't store in the hash */
1639 /* A separate copy of the key is required in the hash table, because
1640 * we don't have a lock on the connection when we are doing a hash
1644 guid_key = _dbus_strdup (guid);
1645 if (guid_key == NULL)
1648 guid_in_connection = _dbus_strdup (guid);
1649 if (guid_in_connection == NULL)
1651 dbus_free (guid_key);
1655 _DBUS_LOCK (shared_connections);
1656 _dbus_assert (shared_connections != NULL);
1658 if (!_dbus_hash_table_insert_string (shared_connections,
1659 guid_key, connection))
1661 dbus_free (guid_key);
1662 dbus_free (guid_in_connection);
1663 _DBUS_UNLOCK (shared_connections);
1667 connection->server_guid = guid_in_connection;
1669 _dbus_verbose ("stored connection to %s to be shared\n",
1670 connection->server_guid);
1672 _DBUS_UNLOCK (shared_connections);
1674 _dbus_assert (connection->server_guid != NULL);
1680 connection_forget_shared_unlocked (DBusConnection *connection)
1682 HAVE_LOCK_CHECK (connection);
1684 if (!connection->shareable)
1687 _DBUS_LOCK (shared_connections);
1689 if (connection->server_guid != NULL)
1691 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1692 connection->server_guid);
1694 if (!_dbus_hash_table_remove_string (shared_connections,
1695 connection->server_guid))
1696 _dbus_assert_not_reached ("connection was not in the shared table");
1698 dbus_free (connection->server_guid);
1699 connection->server_guid = NULL;
1703 _dbus_list_remove (&shared_connections_no_guid, connection);
1706 _DBUS_UNLOCK (shared_connections);
1708 /* remove our reference held on all shareable connections */
1709 _dbus_connection_unref_unlocked (connection);
1712 static DBusConnection*
1713 connection_try_from_address_entry (DBusAddressEntry *entry,
1716 DBusTransport *transport;
1717 DBusConnection *connection;
1719 transport = _dbus_transport_open (entry, error);
1721 if (transport == NULL)
1723 _DBUS_ASSERT_ERROR_IS_SET (error);
1727 connection = _dbus_connection_new_for_transport (transport);
1729 _dbus_transport_unref (transport);
1731 if (connection == NULL)
1733 _DBUS_SET_OOM (error);
1737 #ifndef DBUS_DISABLE_CHECKS
1738 _dbus_assert (!connection->have_connection_lock);
1744 * If the shared parameter is true, then any existing connection will
1745 * be used (and if a new connection is created, it will be available
1746 * for use by others). If the shared parameter is false, a new
1747 * connection will always be created, and the new connection will
1748 * never be returned to other callers.
1750 * @param address the address
1751 * @param shared whether the connection is shared or private
1752 * @param error error return
1753 * @returns the connection or #NULL on error
1755 static DBusConnection*
1756 _dbus_connection_open_internal (const char *address,
1760 DBusConnection *connection;
1761 DBusAddressEntry **entries;
1762 DBusError tmp_error = DBUS_ERROR_INIT;
1763 DBusError first_error = DBUS_ERROR_INIT;
1766 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1768 _dbus_verbose ("opening %s connection to: %s\n",
1769 shared ? "shared" : "private", address);
1771 if (!dbus_parse_address (address, &entries, &len, error))
1774 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1778 for (i = 0; i < len; i++)
1782 if (!connection_lookup_shared (entries[i], &connection))
1783 _DBUS_SET_OOM (&tmp_error);
1786 if (connection == NULL)
1788 connection = connection_try_from_address_entry (entries[i],
1791 if (connection != NULL && shared)
1795 connection->shareable = TRUE;
1797 /* guid may be NULL */
1798 guid = dbus_address_entry_get_value (entries[i], "guid");
1800 CONNECTION_LOCK (connection);
1802 if (!connection_record_shared_unlocked (connection, guid))
1804 _DBUS_SET_OOM (&tmp_error);
1805 _dbus_connection_close_possibly_shared_and_unlock (connection);
1806 dbus_connection_unref (connection);
1810 CONNECTION_UNLOCK (connection);
1817 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1820 dbus_move_error (&tmp_error, &first_error);
1822 dbus_error_free (&tmp_error);
1825 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1826 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1828 if (connection == NULL)
1830 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1831 dbus_move_error (&first_error, error);
1834 dbus_error_free (&first_error);
1836 dbus_address_entries_free (entries);
1841 * Closes a shared OR private connection, while dbus_connection_close() can
1842 * only be used on private connections. Should only be called by the
1843 * dbus code that owns the connection - an owner must be known,
1844 * the open/close state is like malloc/free, not like ref/unref.
1846 * @param connection the connection
1849 _dbus_connection_close_possibly_shared (DBusConnection *connection)
1851 _dbus_assert (connection != NULL);
1852 _dbus_assert (connection->generation == _dbus_current_generation);
1854 CONNECTION_LOCK (connection);
1855 _dbus_connection_close_possibly_shared_and_unlock (connection);
1858 static DBusPreallocatedSend*
1859 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1861 DBusPreallocatedSend *preallocated;
1863 HAVE_LOCK_CHECK (connection);
1865 _dbus_assert (connection != NULL);
1867 preallocated = dbus_new (DBusPreallocatedSend, 1);
1868 if (preallocated == NULL)
1871 if (connection->link_cache != NULL)
1873 preallocated->queue_link =
1874 _dbus_list_pop_first_link (&connection->link_cache);
1875 preallocated->queue_link->data = NULL;
1879 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1880 if (preallocated->queue_link == NULL)
1884 if (connection->link_cache != NULL)
1886 preallocated->counter_link =
1887 _dbus_list_pop_first_link (&connection->link_cache);
1888 preallocated->counter_link->data = connection->outgoing_counter;
1892 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1893 if (preallocated->counter_link == NULL)
1897 _dbus_counter_ref (preallocated->counter_link->data);
1899 preallocated->connection = connection;
1901 return preallocated;
1904 _dbus_list_free_link (preallocated->queue_link);
1906 dbus_free (preallocated);
1911 /* Called with lock held, does not update dispatch status */
1913 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
1914 DBusPreallocatedSend *preallocated,
1915 DBusMessage *message,
1916 dbus_uint32_t *client_serial)
1918 dbus_uint32_t serial;
1921 preallocated->queue_link->data = message;
1922 _dbus_list_prepend_link (&connection->outgoing_messages,
1923 preallocated->queue_link);
1925 _dbus_message_add_size_counter_link (message,
1926 preallocated->counter_link);
1928 dbus_free (preallocated);
1929 preallocated = NULL;
1931 dbus_message_ref (message);
1933 connection->n_outgoing += 1;
1935 sig = dbus_message_get_signature (message);
1937 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
1939 dbus_message_get_type (message),
1940 dbus_message_get_path (message) ?
1941 dbus_message_get_path (message) :
1943 dbus_message_get_interface (message) ?
1944 dbus_message_get_interface (message) :
1946 dbus_message_get_member (message) ?
1947 dbus_message_get_member (message) :
1950 dbus_message_get_destination (message) ?
1951 dbus_message_get_destination (message) :
1954 connection->n_outgoing);
1956 if (dbus_message_get_serial (message) == 0)
1958 serial = _dbus_connection_get_next_client_serial (connection);
1959 dbus_message_set_serial (message, serial);
1961 *client_serial = serial;
1966 *client_serial = dbus_message_get_serial (message);
1969 _dbus_verbose ("Message %p serial is %u\n",
1970 message, dbus_message_get_serial (message));
1972 dbus_message_lock (message);
1974 /* Now we need to run an iteration to hopefully just write the messages
1975 * out immediately, and otherwise get them queued up
1977 _dbus_connection_do_iteration_unlocked (connection,
1978 DBUS_ITERATION_DO_WRITING,
1981 /* If stuff is still queued up, be sure we wake up the main loop */
1982 if (connection->n_outgoing > 0)
1983 _dbus_connection_wakeup_mainloop (connection);
1987 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
1988 DBusPreallocatedSend *preallocated,
1989 DBusMessage *message,
1990 dbus_uint32_t *client_serial)
1992 DBusDispatchStatus status;
1994 HAVE_LOCK_CHECK (connection);
1996 _dbus_connection_send_preallocated_unlocked_no_update (connection,
1998 message, client_serial);
2000 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2001 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2003 /* this calls out to user code */
2004 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2008 * Like dbus_connection_send(), but assumes the connection
2009 * is already locked on function entry, and unlocks before returning.
2011 * @param connection the connection
2012 * @param message the message to send
2013 * @param client_serial return location for client serial of sent message
2014 * @returns #FALSE on out-of-memory
2017 _dbus_connection_send_and_unlock (DBusConnection *connection,
2018 DBusMessage *message,
2019 dbus_uint32_t *client_serial)
2021 DBusPreallocatedSend *preallocated;
2023 _dbus_assert (connection != NULL);
2024 _dbus_assert (message != NULL);
2026 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2027 if (preallocated == NULL)
2029 CONNECTION_UNLOCK (connection);
2033 _dbus_connection_send_preallocated_and_unlock (connection,
2041 * Used internally to handle the semantics of dbus_server_set_new_connection_function().
2042 * If the new connection function does not ref the connection, we want to close it.
2044 * A bit of a hack, probably the new connection function should have returned a value
2045 * for whether to close, or should have had to close the connection itself if it
2048 * But, this works OK as long as the new connection function doesn't do anything
2049 * crazy like keep the connection around without ref'ing it.
2051 * We have to lock the connection across refcount check and close in case
2052 * the new connection function spawns a thread that closes and unrefs.
2053 * In that case, if the app thread
2054 * closes and unrefs first, we'll harmlessly close again; if the app thread
2055 * still has the ref, we'll close and then the app will close harmlessly.
2056 * If the app unrefs without closing, the app is broken since if the
2057 * app refs from the new connection function it is supposed to also close.
2059 * If we didn't atomically check the refcount and close with the lock held
2060 * though, we could screw this up.
2062 * @param connection the connection
2065 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
2067 CONNECTION_LOCK (connection);
2069 _dbus_assert (connection->refcount.value > 0);
2071 if (connection->refcount.value == 1)
2072 _dbus_connection_close_possibly_shared_and_unlock (connection);
2074 CONNECTION_UNLOCK (connection);
2079 * When a function that blocks has been called with a timeout, and we
2080 * run out of memory, the time to wait for memory is based on the
2081 * timeout. If the caller was willing to block a long time we wait a
2082 * relatively long time for memory, if they were only willing to block
2083 * briefly then we retry for memory at a rapid rate.
2085 * @timeout_milliseconds the timeout requested for blocking
2088 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2090 if (timeout_milliseconds == -1)
2091 _dbus_sleep_milliseconds (1000);
2092 else if (timeout_milliseconds < 100)
2093 ; /* just busy loop */
2094 else if (timeout_milliseconds <= 1000)
2095 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2097 _dbus_sleep_milliseconds (1000);
2100 static DBusMessage *
2101 generate_local_error_message (dbus_uint32_t serial,
2105 DBusMessage *message;
2106 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
2110 if (!dbus_message_set_error_name (message, error_name))
2112 dbus_message_unref (message);
2117 dbus_message_set_no_reply (message, TRUE);
2119 if (!dbus_message_set_reply_serial (message,
2122 dbus_message_unref (message);
2127 if (error_msg != NULL)
2129 DBusMessageIter iter;
2131 dbus_message_iter_init_append (message, &iter);
2132 if (!dbus_message_iter_append_basic (&iter,
2136 dbus_message_unref (message);
2147 /* This is slightly strange since we can pop a message here without
2148 * the dispatch lock.
2151 check_for_reply_unlocked (DBusConnection *connection,
2152 dbus_uint32_t client_serial)
2156 HAVE_LOCK_CHECK (connection);
2158 link = _dbus_list_get_first_link (&connection->incoming_messages);
2160 while (link != NULL)
2162 DBusMessage *reply = link->data;
2164 if (dbus_message_get_reply_serial (reply) == client_serial)
2166 _dbus_list_remove_link (&connection->incoming_messages, link);
2167 connection->n_incoming -= 1;
2170 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2177 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2179 /* We can't iterate over the hash in the normal way since we'll be
2180 * dropping the lock for each item. So we restart the
2181 * iter each time as we drain the hash table.
2184 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2186 DBusPendingCall *pending;
2189 _dbus_hash_iter_init (connection->pending_replies, &iter);
2190 _dbus_hash_iter_next (&iter);
2192 pending = _dbus_hash_iter_get_value (&iter);
2193 _dbus_pending_call_ref_unlocked (pending);
2195 _dbus_pending_call_queue_timeout_error_unlocked (pending,
2197 _dbus_connection_remove_timeout_unlocked (connection,
2198 _dbus_pending_call_get_timeout_unlocked (pending));
2199 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
2200 _dbus_hash_iter_remove_entry (&iter);
2202 _dbus_pending_call_unref_and_unlock (pending);
2203 CONNECTION_LOCK (connection);
2205 HAVE_LOCK_CHECK (connection);
2209 complete_pending_call_and_unlock (DBusConnection *connection,
2210 DBusPendingCall *pending,
2211 DBusMessage *message)
2213 _dbus_pending_call_set_reply_unlocked (pending, message);
2214 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2215 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2217 /* Must be called unlocked since it invokes app callback */
2218 _dbus_pending_call_complete (pending);
2219 dbus_pending_call_unref (pending);
2223 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2224 DBusPendingCall *pending)
2227 DBusDispatchStatus status;
2229 reply = check_for_reply_unlocked (connection,
2230 _dbus_pending_call_get_reply_serial_unlocked (pending));
2233 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2235 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2237 complete_pending_call_and_unlock (connection, pending, reply);
2238 dbus_message_unref (reply);
2240 CONNECTION_LOCK (connection);
2241 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2242 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2243 dbus_pending_call_unref (pending);
2252 * Blocks until a pending call times out or gets a reply.
2254 * Does not re-enter the main loop or run filter/path-registered
2255 * callbacks. The reply to the message will not be seen by
2258 * Returns immediately if pending call already got a reply.
2260 * @todo could use performance improvements (it keeps scanning
2261 * the whole message queue for example)
2263 * @param pending the pending call we block for a reply on
2266 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2268 long start_tv_sec, start_tv_usec;
2269 long end_tv_sec, end_tv_usec;
2270 long tv_sec, tv_usec;
2271 DBusDispatchStatus status;
2272 DBusConnection *connection;
2273 dbus_uint32_t client_serial;
2274 int timeout_milliseconds;
2276 _dbus_assert (pending != NULL);
2278 if (dbus_pending_call_get_completed (pending))
2281 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2283 connection = _dbus_pending_call_get_connection_and_lock (pending);
2285 /* Flush message queue - note, can affect dispatch status */
2286 _dbus_connection_flush_unlocked (connection);
2288 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2290 /* note that timeout_milliseconds is limited to a smallish value
2291 * in _dbus_pending_call_new() so overflows aren't possible
2294 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending));
2296 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2297 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2298 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2299 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2300 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2302 _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",
2303 timeout_milliseconds,
2305 start_tv_sec, start_tv_usec,
2306 end_tv_sec, end_tv_usec);
2308 /* check to see if we already got the data off the socket */
2309 /* from another blocked pending call */
2310 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2313 /* Now we wait... */
2314 /* always block at least once as we know we don't have the reply yet */
2315 _dbus_connection_do_iteration_unlocked (connection,
2316 DBUS_ITERATION_DO_READING |
2317 DBUS_ITERATION_BLOCK,
2318 timeout_milliseconds);
2322 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2324 HAVE_LOCK_CHECK (connection);
2326 /* queue messages and get status */
2328 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2330 /* the get_completed() is in case a dispatch() while we were blocking
2331 * got the reply instead of us.
2333 if (_dbus_pending_call_get_completed_unlocked (pending))
2335 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2336 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2337 dbus_pending_call_unref (pending);
2341 if (status == DBUS_DISPATCH_DATA_REMAINS)
2343 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2347 _dbus_get_current_time (&tv_sec, &tv_usec);
2349 if (!_dbus_connection_get_is_connected_unlocked (connection))
2351 DBusMessage *error_msg;
2353 error_msg = generate_local_error_message (client_serial,
2354 DBUS_ERROR_DISCONNECTED,
2355 "Connection was disconnected before a reply was received");
2357 /* on OOM error_msg is set to NULL */
2358 complete_pending_call_and_unlock (connection, pending, error_msg);
2359 dbus_pending_call_unref (pending);
2362 else if (tv_sec < start_tv_sec)
2363 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2364 else if (connection->disconnect_message_link == NULL)
2365 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2366 else if (tv_sec < end_tv_sec ||
2367 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2369 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2370 (end_tv_usec - tv_usec) / 1000;
2371 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2372 _dbus_assert (timeout_milliseconds >= 0);
2374 if (status == DBUS_DISPATCH_NEED_MEMORY)
2376 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2377 * we may already have a reply in the buffer and just can't process
2380 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2382 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2386 /* block again, we don't have the reply buffered yet. */
2387 _dbus_connection_do_iteration_unlocked (connection,
2388 DBUS_ITERATION_DO_READING |
2389 DBUS_ITERATION_BLOCK,
2390 timeout_milliseconds);
2393 goto recheck_status;
2396 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2397 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2399 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
2401 /* unlock and call user code */
2402 complete_pending_call_and_unlock (connection, pending, NULL);
2404 /* update user code on dispatch status */
2405 CONNECTION_LOCK (connection);
2406 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2407 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2408 dbus_pending_call_unref (pending);
2414 * @addtogroup DBusConnection
2420 * Gets a connection to a remote address. If a connection to the given
2421 * address already exists, returns the existing connection with its
2422 * reference count incremented. Otherwise, returns a new connection
2423 * and saves the new connection for possible re-use if a future call
2424 * to dbus_connection_open() asks to connect to the same server.
2426 * Use dbus_connection_open_private() to get a dedicated connection
2427 * not shared with other callers of dbus_connection_open().
2429 * If the open fails, the function returns #NULL, and provides a
2430 * reason for the failure in the error parameter. Pass #NULL for the
2431 * error parameter if you aren't interested in the reason for
2434 * Because this connection is shared, no user of the connection
2435 * may call dbus_connection_close(). However, when you are done with the
2436 * connection you should call dbus_connection_unref().
2438 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2439 * unless you have good reason; connections are expensive enough
2440 * that it's wasteful to create lots of connections to the same
2443 * @param address the address.
2444 * @param error address where an error can be returned.
2445 * @returns new connection, or #NULL on failure.
2448 dbus_connection_open (const char *address,
2451 DBusConnection *connection;
2453 _dbus_return_val_if_fail (address != NULL, NULL);
2454 _dbus_return_val_if_error_is_set (error, NULL);
2456 connection = _dbus_connection_open_internal (address,
2464 * Opens a new, dedicated connection to a remote address. Unlike
2465 * dbus_connection_open(), always creates a new connection.
2466 * This connection will not be saved or recycled by libdbus.
2468 * If the open fails, the function returns #NULL, and provides a
2469 * reason for the failure in the error parameter. Pass #NULL for the
2470 * error parameter if you aren't interested in the reason for
2473 * When you are done with this connection, you must
2474 * dbus_connection_close() to disconnect it,
2475 * and dbus_connection_unref() to free the connection object.
2477 * (The dbus_connection_close() can be skipped if the
2478 * connection is already known to be disconnected, for example
2479 * if you are inside a handler for the Disconnected signal.)
2481 * @note Prefer dbus_connection_open() to dbus_connection_open_private()
2482 * unless you have good reason; connections are expensive enough
2483 * that it's wasteful to create lots of connections to the same
2486 * @param address the address.
2487 * @param error address where an error can be returned.
2488 * @returns new connection, or #NULL on failure.
2491 dbus_connection_open_private (const char *address,
2494 DBusConnection *connection;
2496 _dbus_return_val_if_fail (address != NULL, NULL);
2497 _dbus_return_val_if_error_is_set (error, NULL);
2499 connection = _dbus_connection_open_internal (address,
2507 * Increments the reference count of a DBusConnection.
2509 * @param connection the connection.
2510 * @returns the connection.
2513 dbus_connection_ref (DBusConnection *connection)
2515 _dbus_return_val_if_fail (connection != NULL, NULL);
2516 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
2518 /* The connection lock is better than the global
2519 * lock in the atomic increment fallback
2522 #ifdef DBUS_HAVE_ATOMIC_INT
2523 _dbus_atomic_inc (&connection->refcount);
2525 CONNECTION_LOCK (connection);
2526 _dbus_assert (connection->refcount.value > 0);
2528 connection->refcount.value += 1;
2529 CONNECTION_UNLOCK (connection);
2536 free_outgoing_message (void *element,
2539 DBusMessage *message = element;
2540 DBusConnection *connection = data;
2542 _dbus_message_remove_size_counter (message,
2543 connection->outgoing_counter,
2545 dbus_message_unref (message);
2548 /* This is run without the mutex held, but after the last reference
2549 * to the connection has been dropped we should have no thread-related
2553 _dbus_connection_last_unref (DBusConnection *connection)
2557 _dbus_verbose ("Finalizing connection %p\n", connection);
2559 _dbus_assert (connection->refcount.value == 0);
2561 /* You have to disconnect the connection before unref:ing it. Otherwise
2562 * you won't get the disconnected message.
2564 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
2565 _dbus_assert (connection->server_guid == NULL);
2567 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
2568 _dbus_object_tree_free_all_unlocked (connection->objects);
2570 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
2571 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
2572 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
2574 _dbus_watch_list_free (connection->watches);
2575 connection->watches = NULL;
2577 _dbus_timeout_list_free (connection->timeouts);
2578 connection->timeouts = NULL;
2580 _dbus_data_slot_list_free (&connection->slot_list);
2582 link = _dbus_list_get_first_link (&connection->filter_list);
2583 while (link != NULL)
2585 DBusMessageFilter *filter = link->data;
2586 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
2588 filter->function = NULL;
2589 _dbus_message_filter_unref (filter); /* calls app callback */
2594 _dbus_list_clear (&connection->filter_list);
2596 /* ---- Done with stuff that invokes application callbacks */
2598 _dbus_object_tree_unref (connection->objects);
2600 _dbus_hash_table_unref (connection->pending_replies);
2601 connection->pending_replies = NULL;
2603 _dbus_list_clear (&connection->filter_list);
2605 _dbus_list_foreach (&connection->outgoing_messages,
2606 free_outgoing_message,
2608 _dbus_list_clear (&connection->outgoing_messages);
2610 _dbus_list_foreach (&connection->incoming_messages,
2611 (DBusForeachFunction) dbus_message_unref,
2613 _dbus_list_clear (&connection->incoming_messages);
2615 _dbus_counter_unref (connection->outgoing_counter);
2617 _dbus_transport_unref (connection->transport);
2619 if (connection->disconnect_message_link)
2621 DBusMessage *message = connection->disconnect_message_link->data;
2622 dbus_message_unref (message);
2623 _dbus_list_free_link (connection->disconnect_message_link);
2626 _dbus_list_clear (&connection->link_cache);
2628 _dbus_condvar_free_at_location (&connection->dispatch_cond);
2629 _dbus_condvar_free_at_location (&connection->io_path_cond);
2631 _dbus_mutex_free_at_location (&connection->io_path_mutex);
2632 _dbus_mutex_free_at_location (&connection->dispatch_mutex);
2634 _dbus_mutex_free_at_location (&connection->mutex);
2636 dbus_free (connection);
2640 * Decrements the reference count of a DBusConnection, and finalizes
2641 * it if the count reaches zero.
2643 * Note: it is a bug to drop the last reference to a connection that
2644 * is still connected.
2646 * For shared connections, libdbus will own a reference
2647 * as long as the connection is connected, so you can know that either
2648 * you don't have the last reference, or it's OK to drop the last reference.
2649 * Most connections are shared. dbus_connection_open() and dbus_bus_get()
2650 * return shared connections.
2652 * For private connections, the creator of the connection must arrange for
2653 * dbus_connection_close() to be called prior to dropping the last reference.
2654 * Private connections come from dbus_connection_open_private() or dbus_bus_get_private().
2656 * @param connection the connection.
2659 dbus_connection_unref (DBusConnection *connection)
2661 dbus_bool_t last_unref;
2663 _dbus_return_if_fail (connection != NULL);
2664 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2666 /* The connection lock is better than the global
2667 * lock in the atomic increment fallback
2670 #ifdef DBUS_HAVE_ATOMIC_INT
2671 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
2673 CONNECTION_LOCK (connection);
2675 _dbus_assert (connection->refcount.value > 0);
2677 connection->refcount.value -= 1;
2678 last_unref = (connection->refcount.value == 0);
2681 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
2684 CONNECTION_UNLOCK (connection);
2689 #ifndef DBUS_DISABLE_CHECKS
2690 if (_dbus_transport_get_is_connected (connection->transport))
2692 _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",
2693 connection->shareable ?
2694 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" :
2695 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
2699 _dbus_connection_last_unref (connection);
2704 * Note that the transport can disconnect itself (other end drops us)
2705 * and in that case this function never runs. So this function must
2706 * not do anything more than disconnect the transport and update the
2709 * If the transport self-disconnects, then we assume someone will
2710 * dispatch the connection to cause the dispatch status update.
2713 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2715 DBusDispatchStatus status;
2717 HAVE_LOCK_CHECK (connection);
2719 _dbus_verbose ("Disconnecting %p\n", connection);
2721 /* We need to ref because update_dispatch_status_and_unlock will unref
2722 * the connection if it was shared and libdbus was the only remaining
2725 _dbus_connection_ref_unlocked (connection);
2727 _dbus_transport_disconnect (connection->transport);
2729 /* This has the side effect of queuing the disconnect message link
2730 * (unless we don't have enough memory, possibly, so don't assert it).
2731 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
2732 * should never again return the newly-disconnected connection.
2734 * However, we only unref the shared connection and exit_on_disconnect when
2735 * the disconnect message reaches the head of the message queue,
2736 * NOT when it's first queued.
2738 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2740 /* This calls out to user code */
2741 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2743 /* Could also call out to user code */
2744 dbus_connection_unref (connection);
2748 * Closes a private connection, so no further data can be sent or received.
2749 * This disconnects the transport (such as a socket) underlying the
2752 * Attempts to send messages after closing a connection are safe, but will result in
2753 * error replies generated locally in libdbus.
2755 * This function does not affect the connection's reference count. It's
2756 * safe to close a connection more than once; all calls after the
2757 * first do nothing. It's impossible to "reopen" a connection, a
2758 * new connection must be created. This function may result in a call
2759 * to the DBusDispatchStatusFunction set with
2760 * dbus_connection_set_dispatch_status_function(), as the disconnect
2761 * message it generates needs to be dispatched.
2763 * If a connection is dropped by the remote application, it will
2766 * You must close a connection prior to releasing the last reference to
2767 * the connection. If you dbus_connection_unref() for the last time
2768 * without closing the connection, the results are undefined; it
2769 * is a bug in your program and libdbus will try to print a warning.
2771 * You may not close a shared connection. Connections created with
2772 * dbus_connection_open() or dbus_bus_get() are shared.
2773 * These connections are owned by libdbus, and applications should
2774 * only unref them, never close them. Applications can know it is
2775 * safe to unref these connections because libdbus will be holding a
2776 * reference as long as the connection is open. Thus, either the
2777 * connection is closed and it is OK to drop the last reference,
2778 * or the connection is open and the app knows it does not have the
2781 * Connections created with dbus_connection_open_private() or
2782 * dbus_bus_get_private() are not kept track of or referenced by
2783 * libdbus. The creator of these connections is responsible for
2784 * calling dbus_connection_close() prior to releasing the last
2785 * reference, if the connection is not already disconnected.
2787 * @param connection the private (unshared) connection to close
2790 dbus_connection_close (DBusConnection *connection)
2792 _dbus_return_if_fail (connection != NULL);
2793 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2795 CONNECTION_LOCK (connection);
2797 #ifndef DBUS_DISABLE_CHECKS
2798 if (connection->shareable)
2800 CONNECTION_UNLOCK (connection);
2802 _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
2807 _dbus_connection_close_possibly_shared_and_unlock (connection);
2811 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2813 HAVE_LOCK_CHECK (connection);
2814 return _dbus_transport_get_is_connected (connection->transport);
2818 * Gets whether the connection is currently open. A connection may
2819 * become disconnected when the remote application closes its end, or
2820 * exits; a connection may also be disconnected with
2821 * dbus_connection_close().
2823 * There are not separate states for "closed" and "disconnected," the two
2824 * terms are synonymous. This function should really be called
2825 * get_is_open() but for historical reasons is not.
2827 * @param connection the connection.
2828 * @returns #TRUE if the connection is still alive.
2831 dbus_connection_get_is_connected (DBusConnection *connection)
2835 _dbus_return_val_if_fail (connection != NULL, FALSE);
2837 CONNECTION_LOCK (connection);
2838 res = _dbus_connection_get_is_connected_unlocked (connection);
2839 CONNECTION_UNLOCK (connection);
2845 * Gets whether the connection was authenticated. (Note that
2846 * if the connection was authenticated then disconnected,
2847 * this function still returns #TRUE)
2849 * @param connection the connection
2850 * @returns #TRUE if the connection was ever authenticated
2853 dbus_connection_get_is_authenticated (DBusConnection *connection)
2857 _dbus_return_val_if_fail (connection != NULL, FALSE);
2859 CONNECTION_LOCK (connection);
2860 res = _dbus_transport_get_is_authenticated (connection->transport);
2861 CONNECTION_UNLOCK (connection);
2867 * Gets whether the connection is not authenticated as a specific
2868 * user. If the connection is not authenticated, this function
2869 * returns #TRUE, and if it is authenticated but as an anonymous user,
2870 * it returns #TRUE. If it is authenticated as a specific user, then
2871 * this returns #FALSE. (Note that if the connection was authenticated
2872 * as anonymous then disconnected, this function still returns #TRUE.)
2874 * If the connection is not anonymous, you can use
2875 * dbus_connection_get_unix_user() and
2876 * dbus_connection_get_windows_user() to see who it's authorized as.
2878 * If you want to prevent non-anonymous authorization, use
2879 * dbus_server_set_auth_mechanisms() to remove the mechanisms that
2880 * allow proving user identity (i.e. only allow the ANONYMOUS
2883 * @param connection the connection
2884 * @returns #TRUE if not authenticated or authenticated as anonymous
2887 dbus_connection_get_is_anonymous (DBusConnection *connection)
2891 _dbus_return_val_if_fail (connection != NULL, FALSE);
2893 CONNECTION_LOCK (connection);
2894 res = _dbus_transport_get_is_anonymous (connection->transport);
2895 CONNECTION_UNLOCK (connection);
2901 * Gets the ID of the server address we are authenticated to, if this
2902 * connection is on the client side. If the connection is on the
2903 * server side, this will always return #NULL - use dbus_server_get_id()
2904 * to get the ID of your own server, if you are the server side.
2906 * If a client-side connection is not authenticated yet, the ID may be
2907 * available if it was included in the server address, but may not be
2908 * available. The only way to be sure the server ID is available
2909 * is to wait for authentication to complete.
2911 * In general, each mode of connecting to a given server will have
2912 * its own ID. So for example, if the session bus daemon is listening
2913 * on UNIX domain sockets and on TCP, then each of those modalities
2914 * will have its own server ID.
2916 * If you want an ID that identifies an entire session bus, look at
2917 * dbus_bus_get_id() instead (which is just a convenience wrapper
2918 * around the org.freedesktop.DBus.GetId method invoked on the bus).
2920 * You can also get a machine ID; see dbus_get_local_machine_id() to
2921 * get the machine you are on. There isn't a convenience wrapper, but
2922 * you can invoke org.freedesktop.DBus.Peer.GetMachineId on any peer
2923 * to get the machine ID on the other end.
2925 * The D-Bus specification describes the server ID and other IDs in a
2928 * @param connection the connection
2929 * @returns the server ID or #NULL if no memory or the connection is server-side
2932 dbus_connection_get_server_id (DBusConnection *connection)
2936 _dbus_return_val_if_fail (connection != NULL, NULL);
2938 CONNECTION_LOCK (connection);
2939 id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
2940 CONNECTION_UNLOCK (connection);
2946 * Tests whether a certain type can be send via the connection. This
2947 * will always return TRUE for all types, with the exception of
2948 * DBUS_TYPE_UNIX_FD. The function will return TRUE for
2949 * DBUS_TYPE_UNIX_FD only on systems that know Unix file descriptors
2950 * and can send them via the chosen transport and when the remote side
2953 * This function can be used to do runtime checking for types that
2954 * might be unknown to the specific D-Bus client implementation
2955 * version, i.e. it will return FALSE for all types this
2956 * implementation does not know.
2958 * @param connection the connection
2959 * @param type the type to check
2960 * @returns TRUE if the type may be send via the connection
2963 dbus_connection_can_send_type(DBusConnection *connection,
2966 _dbus_return_val_if_fail (connection != NULL, FALSE);
2968 if (!_dbus_type_is_valid(type))
2971 if (type != DBUS_TYPE_UNIX_FD)
2974 #ifdef HAVE_UNIX_FD_PASSING
2978 CONNECTION_LOCK(connection);
2979 b = _dbus_transport_can_pass_unix_fd(connection->transport);
2980 CONNECTION_UNLOCK(connection);
2990 * Set whether _exit() should be called when the connection receives a
2991 * disconnect signal. The call to _exit() comes after any handlers for
2992 * the disconnect signal run; handlers can cancel the exit by calling
2995 * By default, exit_on_disconnect is #FALSE; but for message bus
2996 * connections returned from dbus_bus_get() it will be toggled on
2999 * @param connection the connection
3000 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
3003 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
3004 dbus_bool_t exit_on_disconnect)
3006 _dbus_return_if_fail (connection != NULL);
3008 CONNECTION_LOCK (connection);
3009 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
3010 CONNECTION_UNLOCK (connection);
3014 * Preallocates resources needed to send a message, allowing the message
3015 * to be sent without the possibility of memory allocation failure.
3016 * Allows apps to create a future guarantee that they can send
3017 * a message regardless of memory shortages.
3019 * @param connection the connection we're preallocating for.
3020 * @returns the preallocated resources, or #NULL
3022 DBusPreallocatedSend*
3023 dbus_connection_preallocate_send (DBusConnection *connection)
3025 DBusPreallocatedSend *preallocated;
3027 _dbus_return_val_if_fail (connection != NULL, NULL);
3029 CONNECTION_LOCK (connection);
3032 _dbus_connection_preallocate_send_unlocked (connection);
3034 CONNECTION_UNLOCK (connection);
3036 return preallocated;
3040 * Frees preallocated message-sending resources from
3041 * dbus_connection_preallocate_send(). Should only
3042 * be called if the preallocated resources are not used
3043 * to send a message.
3045 * @param connection the connection
3046 * @param preallocated the resources
3049 dbus_connection_free_preallocated_send (DBusConnection *connection,
3050 DBusPreallocatedSend *preallocated)
3052 _dbus_return_if_fail (connection != NULL);
3053 _dbus_return_if_fail (preallocated != NULL);
3054 _dbus_return_if_fail (connection == preallocated->connection);
3056 _dbus_list_free_link (preallocated->queue_link);
3057 _dbus_counter_unref (preallocated->counter_link->data);
3058 _dbus_list_free_link (preallocated->counter_link);
3059 dbus_free (preallocated);
3063 * Sends a message using preallocated resources. This function cannot fail.
3064 * It works identically to dbus_connection_send() in other respects.
3065 * Preallocated resources comes from dbus_connection_preallocate_send().
3066 * This function "consumes" the preallocated resources, they need not
3067 * be freed separately.
3069 * @param connection the connection
3070 * @param preallocated the preallocated resources
3071 * @param message the message to send
3072 * @param client_serial return location for client serial assigned to the message
3075 dbus_connection_send_preallocated (DBusConnection *connection,
3076 DBusPreallocatedSend *preallocated,
3077 DBusMessage *message,
3078 dbus_uint32_t *client_serial)
3080 _dbus_return_if_fail (connection != NULL);
3081 _dbus_return_if_fail (preallocated != NULL);
3082 _dbus_return_if_fail (message != NULL);
3083 _dbus_return_if_fail (preallocated->connection == connection);
3084 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
3085 dbus_message_get_member (message) != NULL);
3086 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
3087 (dbus_message_get_interface (message) != NULL &&
3088 dbus_message_get_member (message) != NULL));
3090 CONNECTION_LOCK (connection);
3092 #ifdef HAVE_UNIX_FD_PASSING
3094 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3095 message->n_unix_fds > 0)
3097 /* Refuse to send fds on a connection that cannot handle
3098 them. Unfortunately we cannot return a proper error here, so
3099 the best we can is just return. */
3100 CONNECTION_UNLOCK (connection);
3106 _dbus_connection_send_preallocated_and_unlock (connection,
3108 message, client_serial);
3112 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
3113 DBusMessage *message,
3114 dbus_uint32_t *client_serial)
3116 DBusPreallocatedSend *preallocated;
3118 _dbus_assert (connection != NULL);
3119 _dbus_assert (message != NULL);
3121 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3122 if (preallocated == NULL)
3125 _dbus_connection_send_preallocated_unlocked_no_update (connection,
3133 * Adds a message to the outgoing message queue. Does not block to
3134 * write the message to the network; that happens asynchronously. To
3135 * force the message to be written, call dbus_connection_flush() however
3136 * it is not necessary to call dbus_connection_flush() by hand; the
3137 * message will be sent the next time the main loop is run.
3138 * dbus_connection_flush() should only be used, for example, if
3139 * the application was expected to exit before running the main loop.
3141 * Because this only queues the message, the only reason it can
3142 * fail is lack of memory. Even if the connection is disconnected,
3143 * no error will be returned. If the function fails due to lack of memory,
3144 * it returns #FALSE. The function will never fail for other reasons; even
3145 * if the connection is disconnected, you can queue an outgoing message,
3146 * though obviously it won't be sent.
3148 * The message serial is used by the remote application to send a
3149 * reply; see dbus_message_get_serial() or the D-Bus specification.
3151 * dbus_message_unref() can be called as soon as this method returns
3152 * as the message queue will hold its own ref until the message is sent.
3154 * @param connection the connection.
3155 * @param message the message to write.
3156 * @param serial return location for message serial, or #NULL if you don't care
3157 * @returns #TRUE on success.
3160 dbus_connection_send (DBusConnection *connection,
3161 DBusMessage *message,
3162 dbus_uint32_t *serial)
3164 _dbus_return_val_if_fail (connection != NULL, FALSE);
3165 _dbus_return_val_if_fail (message != NULL, FALSE);
3167 CONNECTION_LOCK (connection);
3169 #ifdef HAVE_UNIX_FD_PASSING
3171 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3172 message->n_unix_fds > 0)
3174 /* Refuse to send fds on a connection that cannot handle
3175 them. Unfortunately we cannot return a proper error here, so
3176 the best we can is just return. */
3177 CONNECTION_UNLOCK (connection);
3183 return _dbus_connection_send_and_unlock (connection,
3189 reply_handler_timeout (void *data)
3191 DBusConnection *connection;
3192 DBusDispatchStatus status;
3193 DBusPendingCall *pending = data;
3195 connection = _dbus_pending_call_get_connection_and_lock (pending);
3197 _dbus_pending_call_queue_timeout_error_unlocked (pending,
3199 _dbus_connection_remove_timeout_unlocked (connection,
3200 _dbus_pending_call_get_timeout_unlocked (pending));
3201 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
3203 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3204 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3206 /* Unlocks, and calls out to user code */
3207 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3213 * Queues a message to send, as with dbus_connection_send(),
3214 * but also returns a #DBusPendingCall used to receive a reply to the
3215 * message. If no reply is received in the given timeout_milliseconds,
3216 * this function expires the pending reply and generates a synthetic
3217 * error reply (generated in-process, not by the remote application)
3218 * indicating that a timeout occurred.
3220 * A #DBusPendingCall will see a reply message before any filters or
3221 * registered object path handlers. See dbus_connection_dispatch() for
3222 * details on when handlers are run.
3224 * A #DBusPendingCall will always see exactly one reply message,
3225 * unless it's cancelled with dbus_pending_call_cancel().
3227 * If #NULL is passed for the pending_return, the #DBusPendingCall
3228 * will still be generated internally, and used to track
3229 * the message reply timeout. This means a timeout error will
3230 * occur if no reply arrives, unlike with dbus_connection_send().
3232 * If -1 is passed for the timeout, a sane default timeout is used. -1
3233 * is typically the best value for the timeout for this reason, unless
3234 * you want a very short or very long timeout. There is no way to
3235 * avoid a timeout entirely, other than passing INT_MAX for the
3236 * timeout to mean "very long timeout." libdbus clamps an INT_MAX
3237 * timeout down to a few hours timeout though.
3239 * @warning if the connection is disconnected or you try to send Unix
3240 * file descriptors on a connection that does not support them, the
3241 * #DBusPendingCall will be set to #NULL, so be careful with this.
3243 * @param connection the connection
3244 * @param message the message to send
3245 * @param pending_return return location for a #DBusPendingCall
3246 * object, or #NULL if connection is disconnected or when you try to
3247 * send Unix file descriptors on a connection that does not support
3249 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3250 * @returns #FALSE if no memory, #TRUE otherwise.
3254 dbus_connection_send_with_reply (DBusConnection *connection,
3255 DBusMessage *message,
3256 DBusPendingCall **pending_return,
3257 int timeout_milliseconds)
3259 DBusPendingCall *pending;
3260 dbus_int32_t serial = -1;
3261 DBusDispatchStatus status;
3263 _dbus_return_val_if_fail (connection != NULL, FALSE);
3264 _dbus_return_val_if_fail (message != NULL, FALSE);
3265 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3268 *pending_return = NULL;
3270 CONNECTION_LOCK (connection);
3272 #ifdef HAVE_UNIX_FD_PASSING
3274 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3275 message->n_unix_fds > 0)
3277 /* Refuse to send fds on a connection that cannot handle
3278 them. Unfortunately we cannot return a proper error here, so
3279 the best we can do is return TRUE but leave *pending_return
3281 CONNECTION_UNLOCK (connection);
3287 if (!_dbus_connection_get_is_connected_unlocked (connection))
3289 CONNECTION_UNLOCK (connection);
3294 pending = _dbus_pending_call_new_unlocked (connection,
3295 timeout_milliseconds,
3296 reply_handler_timeout);
3298 if (pending == NULL)
3300 CONNECTION_UNLOCK (connection);
3304 /* Assign a serial to the message */
3305 serial = dbus_message_get_serial (message);
3308 serial = _dbus_connection_get_next_client_serial (connection);
3309 dbus_message_set_serial (message, serial);
3312 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
3315 /* Insert the serial in the pending replies hash;
3316 * hash takes a refcount on DBusPendingCall.
3317 * Also, add the timeout.
3319 if (!_dbus_connection_attach_pending_call_unlocked (connection,
3323 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
3325 _dbus_connection_detach_pending_call_and_unlock (connection,
3327 goto error_unlocked;
3331 *pending_return = pending; /* hand off refcount */
3334 _dbus_connection_detach_pending_call_unlocked (connection, pending);
3335 /* we still have a ref to the pending call in this case, we unref
3336 * after unlocking, below
3340 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3342 /* this calls out to user code */
3343 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3345 if (pending_return == NULL)
3346 dbus_pending_call_unref (pending);
3351 CONNECTION_UNLOCK (connection);
3353 dbus_pending_call_unref (pending);
3358 * Sends a message and blocks a certain time period while waiting for
3359 * a reply. This function does not reenter the main loop,
3360 * i.e. messages other than the reply are queued up but not
3361 * processed. This function is used to invoke method calls on a
3364 * If a normal reply is received, it is returned, and removed from the
3365 * incoming message queue. If it is not received, #NULL is returned
3366 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
3367 * received, it is converted to a #DBusError and returned as an error,
3368 * then the reply message is deleted and #NULL is returned. If
3369 * something else goes wrong, result is set to whatever is
3370 * appropriate, such as #DBUS_ERROR_NO_MEMORY or
3371 * #DBUS_ERROR_DISCONNECTED.
3373 * @warning While this function blocks the calling thread will not be
3374 * processing the incoming message queue. This means you can end up
3375 * deadlocked if the application you're talking to needs you to reply
3376 * to a method. To solve this, either avoid the situation, block in a
3377 * separate thread from the main connection-dispatching thread, or use
3378 * dbus_pending_call_set_notify() to avoid blocking.
3380 * @param connection the connection
3381 * @param message the message to send
3382 * @param timeout_milliseconds timeout in milliseconds or -1 for default
3383 * @param error return location for error message
3384 * @returns the message that is the reply or #NULL with an error code if the
3388 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
3389 DBusMessage *message,
3390 int timeout_milliseconds,
3394 DBusPendingCall *pending;
3396 _dbus_return_val_if_fail (connection != NULL, NULL);
3397 _dbus_return_val_if_fail (message != NULL, NULL);
3398 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3399 _dbus_return_val_if_error_is_set (error, NULL);
3401 #ifdef HAVE_UNIX_FD_PASSING
3403 CONNECTION_LOCK (connection);
3404 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3405 message->n_unix_fds > 0)
3407 CONNECTION_UNLOCK (connection);
3408 dbus_set_error(error, DBUS_ERROR_FAILED, "Cannot send file descriptors on this connection.");
3411 CONNECTION_UNLOCK (connection);
3415 if (!dbus_connection_send_with_reply (connection, message,
3416 &pending, timeout_milliseconds))
3418 _DBUS_SET_OOM (error);
3422 if (pending == NULL)
3424 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
3428 dbus_pending_call_block (pending);
3430 reply = dbus_pending_call_steal_reply (pending);
3431 dbus_pending_call_unref (pending);
3433 /* call_complete_and_unlock() called from pending_call_block() should
3434 * always fill this in.
3436 _dbus_assert (reply != NULL);
3438 if (dbus_set_error_from_message (error, reply))
3440 dbus_message_unref (reply);
3448 * Blocks until the outgoing message queue is empty.
3449 * Assumes connection lock already held.
3451 * If you call this, you MUST call update_dispatch_status afterword...
3453 * @param connection the connection.
3455 static DBusDispatchStatus
3456 _dbus_connection_flush_unlocked (DBusConnection *connection)
3458 /* We have to specify DBUS_ITERATION_DO_READING here because
3459 * otherwise we could have two apps deadlock if they are both doing
3460 * a flush(), and the kernel buffers fill up. This could change the
3463 DBusDispatchStatus status;
3465 HAVE_LOCK_CHECK (connection);
3467 while (connection->n_outgoing > 0 &&
3468 _dbus_connection_get_is_connected_unlocked (connection))
3470 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3471 HAVE_LOCK_CHECK (connection);
3472 _dbus_connection_do_iteration_unlocked (connection,
3473 DBUS_ITERATION_DO_READING |
3474 DBUS_ITERATION_DO_WRITING |
3475 DBUS_ITERATION_BLOCK,
3479 HAVE_LOCK_CHECK (connection);
3480 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3481 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3483 HAVE_LOCK_CHECK (connection);
3488 * Blocks until the outgoing message queue is empty.
3490 * @param connection the connection.
3493 dbus_connection_flush (DBusConnection *connection)
3495 /* We have to specify DBUS_ITERATION_DO_READING here because
3496 * otherwise we could have two apps deadlock if they are both doing
3497 * a flush(), and the kernel buffers fill up. This could change the
3500 DBusDispatchStatus status;
3502 _dbus_return_if_fail (connection != NULL);
3504 CONNECTION_LOCK (connection);
3506 status = _dbus_connection_flush_unlocked (connection);
3508 HAVE_LOCK_CHECK (connection);
3509 /* Unlocks and calls out to user code */
3510 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3512 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
3516 * This function implements dbus_connection_read_write_dispatch() and
3517 * dbus_connection_read_write() (they pass a different value for the
3518 * dispatch parameter).
3520 * @param connection the connection
3521 * @param timeout_milliseconds max time to block or -1 for infinite
3522 * @param dispatch dispatch new messages or leave them on the incoming queue
3523 * @returns #TRUE if the disconnect message has not been processed
3526 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3527 int timeout_milliseconds,
3528 dbus_bool_t dispatch)
3530 DBusDispatchStatus dstatus;
3531 dbus_bool_t progress_possible;
3533 /* Need to grab a ref here in case we're a private connection and
3534 * the user drops the last ref in a handler we call; see bug
3535 * https://bugs.freedesktop.org/show_bug.cgi?id=15635
3537 dbus_connection_ref (connection);
3538 dstatus = dbus_connection_get_dispatch_status (connection);
3540 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3542 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
3543 dbus_connection_dispatch (connection);
3544 CONNECTION_LOCK (connection);
3546 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3548 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
3549 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3550 CONNECTION_LOCK (connection);
3554 CONNECTION_LOCK (connection);
3555 if (_dbus_connection_get_is_connected_unlocked (connection))
3557 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
3558 _dbus_connection_do_iteration_unlocked (connection,
3559 DBUS_ITERATION_DO_READING |
3560 DBUS_ITERATION_DO_WRITING |
3561 DBUS_ITERATION_BLOCK,
3562 timeout_milliseconds);
3566 HAVE_LOCK_CHECK (connection);
3567 /* If we can dispatch, we can make progress until the Disconnected message
3568 * has been processed; if we can only read/write, we can make progress
3569 * as long as the transport is open.
3572 progress_possible = connection->n_incoming != 0 ||
3573 connection->disconnect_message_link != NULL;
3575 progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
3577 CONNECTION_UNLOCK (connection);
3579 dbus_connection_unref (connection);
3581 return progress_possible; /* TRUE if we can make more progress */
3586 * This function is intended for use with applications that don't want
3587 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
3588 * example usage would be:
3591 * while (dbus_connection_read_write_dispatch (connection, -1))
3592 * ; // empty loop body
3595 * In this usage you would normally have set up a filter function to look
3596 * at each message as it is dispatched. The loop terminates when the last
3597 * message from the connection (the disconnected signal) is processed.
3599 * If there are messages to dispatch, this function will
3600 * dbus_connection_dispatch() once, and return. If there are no
3601 * messages to dispatch, this function will block until it can read or
3602 * write, then read or write, then return.
3604 * The way to think of this function is that it either makes some sort
3605 * of progress, or it blocks. Note that, while it is blocked on I/O, it
3606 * cannot be interrupted (even by other threads), which makes this function
3607 * unsuitable for applications that do more than just react to received
3610 * The return value indicates whether the disconnect message has been
3611 * processed, NOT whether the connection is connected. This is
3612 * important because even after disconnecting, you want to process any
3613 * messages you received prior to the disconnect.
3615 * @param connection the connection
3616 * @param timeout_milliseconds max time to block or -1 for infinite
3617 * @returns #TRUE if the disconnect message has not been processed
3620 dbus_connection_read_write_dispatch (DBusConnection *connection,
3621 int timeout_milliseconds)
3623 _dbus_return_val_if_fail (connection != NULL, FALSE);
3624 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3625 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3629 * This function is intended for use with applications that don't want to
3630 * write a main loop and deal with #DBusWatch and #DBusTimeout. See also
3631 * dbus_connection_read_write_dispatch().
3633 * As long as the connection is open, this function will block until it can
3634 * read or write, then read or write, then return #TRUE.
3636 * If the connection is closed, the function returns #FALSE.
3638 * The return value indicates whether reading or writing is still
3639 * possible, i.e. whether the connection is connected.
3641 * Note that even after disconnection, messages may remain in the
3642 * incoming queue that need to be
3643 * processed. dbus_connection_read_write_dispatch() dispatches
3644 * incoming messages for you; with dbus_connection_read_write() you
3645 * have to arrange to drain the incoming queue yourself.
3647 * @param connection the connection
3648 * @param timeout_milliseconds max time to block or -1 for infinite
3649 * @returns #TRUE if still connected
3652 dbus_connection_read_write (DBusConnection *connection,
3653 int timeout_milliseconds)
3655 _dbus_return_val_if_fail (connection != NULL, FALSE);
3656 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3657 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3660 /* We need to call this anytime we pop the head of the queue, and then
3661 * update_dispatch_status_and_unlock needs to be called afterward
3662 * which will "process" the disconnected message and set
3663 * disconnected_message_processed.
3666 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3667 DBusMessage *head_of_queue)
3669 HAVE_LOCK_CHECK (connection);
3671 /* checking that the link is NULL is an optimization to avoid the is_signal call */
3672 if (connection->disconnect_message_link == NULL &&
3673 dbus_message_is_signal (head_of_queue,
3674 DBUS_INTERFACE_LOCAL,
3677 connection->disconnected_message_arrived = TRUE;
3682 * Returns the first-received message from the incoming message queue,
3683 * leaving it in the queue. If the queue is empty, returns #NULL.
3685 * The caller does not own a reference to the returned message, and
3686 * must either return it using dbus_connection_return_message() or
3687 * keep it after calling dbus_connection_steal_borrowed_message(). No
3688 * one can get at the message while its borrowed, so return it as
3689 * quickly as possible and don't keep a reference to it after
3690 * returning it. If you need to keep the message, make a copy of it.
3692 * dbus_connection_dispatch() will block if called while a borrowed
3693 * message is outstanding; only one piece of code can be playing with
3694 * the incoming queue at a time. This function will block if called
3695 * during a dbus_connection_dispatch().
3697 * @param connection the connection.
3698 * @returns next message in the incoming queue.
3701 dbus_connection_borrow_message (DBusConnection *connection)
3703 DBusDispatchStatus status;
3704 DBusMessage *message;
3706 _dbus_return_val_if_fail (connection != NULL, NULL);
3708 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3710 /* this is called for the side effect that it queues
3711 * up any messages from the transport
3713 status = dbus_connection_get_dispatch_status (connection);
3714 if (status != DBUS_DISPATCH_DATA_REMAINS)
3717 CONNECTION_LOCK (connection);
3719 _dbus_connection_acquire_dispatch (connection);
3721 /* While a message is outstanding, the dispatch lock is held */
3722 _dbus_assert (connection->message_borrowed == NULL);
3724 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3726 message = connection->message_borrowed;
3728 check_disconnected_message_arrived_unlocked (connection, message);
3730 /* Note that we KEEP the dispatch lock until the message is returned */
3731 if (message == NULL)
3732 _dbus_connection_release_dispatch (connection);
3734 CONNECTION_UNLOCK (connection);
3736 /* We don't update dispatch status until it's returned or stolen */
3742 * Used to return a message after peeking at it using
3743 * dbus_connection_borrow_message(). Only called if
3744 * message from dbus_connection_borrow_message() was non-#NULL.
3746 * @param connection the connection
3747 * @param message the message from dbus_connection_borrow_message()
3750 dbus_connection_return_message (DBusConnection *connection,
3751 DBusMessage *message)
3753 DBusDispatchStatus status;
3755 _dbus_return_if_fail (connection != NULL);
3756 _dbus_return_if_fail (message != NULL);
3757 _dbus_return_if_fail (message == connection->message_borrowed);
3758 _dbus_return_if_fail (connection->dispatch_acquired);
3760 CONNECTION_LOCK (connection);
3762 _dbus_assert (message == connection->message_borrowed);
3764 connection->message_borrowed = NULL;
3766 _dbus_connection_release_dispatch (connection);
3768 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3769 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3773 * Used to keep a message after peeking at it using
3774 * dbus_connection_borrow_message(). Before using this function, see
3775 * the caveats/warnings in the documentation for
3776 * dbus_connection_pop_message().
3778 * @param connection the connection
3779 * @param message the message from dbus_connection_borrow_message()
3782 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3783 DBusMessage *message)
3785 DBusMessage *pop_message;
3786 DBusDispatchStatus status;
3788 _dbus_return_if_fail (connection != NULL);
3789 _dbus_return_if_fail (message != NULL);
3790 _dbus_return_if_fail (message == connection->message_borrowed);
3791 _dbus_return_if_fail (connection->dispatch_acquired);
3793 CONNECTION_LOCK (connection);
3795 _dbus_assert (message == connection->message_borrowed);
3797 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3798 _dbus_assert (message == pop_message);
3800 connection->n_incoming -= 1;
3802 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3803 message, connection->n_incoming);
3805 connection->message_borrowed = NULL;
3807 _dbus_connection_release_dispatch (connection);
3809 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3810 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3813 /* See dbus_connection_pop_message, but requires the caller to own
3814 * the lock before calling. May drop the lock while running.
3817 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3819 HAVE_LOCK_CHECK (connection);
3821 _dbus_assert (connection->message_borrowed == NULL);
3823 if (connection->n_incoming > 0)
3827 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3828 connection->n_incoming -= 1;
3830 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3832 dbus_message_get_type (link->data),
3833 dbus_message_get_path (link->data) ?
3834 dbus_message_get_path (link->data) :
3836 dbus_message_get_interface (link->data) ?
3837 dbus_message_get_interface (link->data) :
3839 dbus_message_get_member (link->data) ?
3840 dbus_message_get_member (link->data) :
3842 dbus_message_get_signature (link->data),
3843 connection, connection->n_incoming);
3845 check_disconnected_message_arrived_unlocked (connection, link->data);
3853 /* See dbus_connection_pop_message, but requires the caller to own
3854 * the lock before calling. May drop the lock while running.
3857 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3861 HAVE_LOCK_CHECK (connection);
3863 link = _dbus_connection_pop_message_link_unlocked (connection);
3867 DBusMessage *message;
3869 message = link->data;
3871 _dbus_list_free_link (link);
3880 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3881 DBusList *message_link)
3883 HAVE_LOCK_CHECK (connection);
3885 _dbus_assert (message_link != NULL);
3886 /* You can't borrow a message while a link is outstanding */
3887 _dbus_assert (connection->message_borrowed == NULL);
3888 /* We had to have the dispatch lock across the pop/putback */
3889 _dbus_assert (connection->dispatch_acquired);
3891 _dbus_list_prepend_link (&connection->incoming_messages,
3893 connection->n_incoming += 1;
3895 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3897 dbus_message_get_type (message_link->data),
3898 dbus_message_get_interface (message_link->data) ?
3899 dbus_message_get_interface (message_link->data) :
3901 dbus_message_get_member (message_link->data) ?
3902 dbus_message_get_member (message_link->data) :
3904 dbus_message_get_signature (message_link->data),
3905 connection, connection->n_incoming);
3909 * Returns the first-received message from the incoming message queue,
3910 * removing it from the queue. The caller owns a reference to the
3911 * returned message. If the queue is empty, returns #NULL.
3913 * This function bypasses any message handlers that are registered,
3914 * and so using it is usually wrong. Instead, let the main loop invoke
3915 * dbus_connection_dispatch(). Popping messages manually is only
3916 * useful in very simple programs that don't share a #DBusConnection
3917 * with any libraries or other modules.
3919 * There is a lock that covers all ways of accessing the incoming message
3920 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3921 * dbus_connection_borrow_message(), etc. will all block while one of the others
3922 * in the group is running.
3924 * @param connection the connection.
3925 * @returns next message in the incoming queue.
3928 dbus_connection_pop_message (DBusConnection *connection)
3930 DBusMessage *message;
3931 DBusDispatchStatus status;
3933 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3935 /* this is called for the side effect that it queues
3936 * up any messages from the transport
3938 status = dbus_connection_get_dispatch_status (connection);
3939 if (status != DBUS_DISPATCH_DATA_REMAINS)
3942 CONNECTION_LOCK (connection);
3943 _dbus_connection_acquire_dispatch (connection);
3944 HAVE_LOCK_CHECK (connection);
3946 message = _dbus_connection_pop_message_unlocked (connection);
3948 _dbus_verbose ("Returning popped message %p\n", message);
3950 _dbus_connection_release_dispatch (connection);
3952 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3953 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3959 * Acquire the dispatcher. This is a separate lock so the main
3960 * connection lock can be dropped to call out to application dispatch
3963 * @param connection the connection.
3966 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3968 HAVE_LOCK_CHECK (connection);
3970 _dbus_connection_ref_unlocked (connection);
3971 CONNECTION_UNLOCK (connection);
3973 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3974 _dbus_mutex_lock (connection->dispatch_mutex);
3976 while (connection->dispatch_acquired)
3978 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3979 _dbus_condvar_wait (connection->dispatch_cond,
3980 connection->dispatch_mutex);
3983 _dbus_assert (!connection->dispatch_acquired);
3985 connection->dispatch_acquired = TRUE;
3987 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3988 _dbus_mutex_unlock (connection->dispatch_mutex);
3990 CONNECTION_LOCK (connection);
3991 _dbus_connection_unref_unlocked (connection);
3995 * Release the dispatcher when you're done with it. Only call
3996 * after you've acquired the dispatcher. Wakes up at most one
3997 * thread currently waiting to acquire the dispatcher.
3999 * @param connection the connection.
4002 _dbus_connection_release_dispatch (DBusConnection *connection)
4004 HAVE_LOCK_CHECK (connection);
4006 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
4007 _dbus_mutex_lock (connection->dispatch_mutex);
4009 _dbus_assert (connection->dispatch_acquired);
4011 connection->dispatch_acquired = FALSE;
4012 _dbus_condvar_wake_one (connection->dispatch_cond);
4014 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
4015 _dbus_mutex_unlock (connection->dispatch_mutex);
4019 _dbus_connection_failed_pop (DBusConnection *connection,
4020 DBusList *message_link)
4022 _dbus_list_prepend_link (&connection->incoming_messages,
4024 connection->n_incoming += 1;
4027 /* Note this may be called multiple times since we don't track whether we already did it */
4029 notify_disconnected_unlocked (DBusConnection *connection)
4031 HAVE_LOCK_CHECK (connection);
4033 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
4034 * connection from dbus_bus_get(). We make the same guarantee for
4035 * dbus_connection_open() but in a different way since we don't want to
4036 * unref right here; we instead check for connectedness before returning
4037 * the connection from the hash.
4039 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
4041 /* Dump the outgoing queue, we aren't going to be able to
4042 * send it now, and we'd like accessors like
4043 * dbus_connection_get_outgoing_size() to be accurate.
4045 if (connection->n_outgoing > 0)
4049 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
4050 connection->n_outgoing);
4052 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
4054 _dbus_connection_message_sent (connection, link->data);
4059 /* Note this may be called multiple times since we don't track whether we already did it */
4060 static DBusDispatchStatus
4061 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
4063 HAVE_LOCK_CHECK (connection);
4065 if (connection->disconnect_message_link != NULL)
4067 _dbus_verbose ("Sending disconnect message from %s\n",
4068 _DBUS_FUNCTION_NAME);
4070 /* If we have pending calls, queue their timeouts - we want the Disconnected
4071 * to be the last message, after these timeouts.
4073 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
4075 /* We haven't sent the disconnect message already,
4076 * and all real messages have been queued up.
4078 _dbus_connection_queue_synthesized_message_link (connection,
4079 connection->disconnect_message_link);
4080 connection->disconnect_message_link = NULL;
4082 return DBUS_DISPATCH_DATA_REMAINS;
4085 return DBUS_DISPATCH_COMPLETE;
4088 static DBusDispatchStatus
4089 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
4091 HAVE_LOCK_CHECK (connection);
4093 if (connection->n_incoming > 0)
4094 return DBUS_DISPATCH_DATA_REMAINS;
4095 else if (!_dbus_transport_queue_messages (connection->transport))
4096 return DBUS_DISPATCH_NEED_MEMORY;
4099 DBusDispatchStatus status;
4100 dbus_bool_t is_connected;
4102 status = _dbus_transport_get_dispatch_status (connection->transport);
4103 is_connected = _dbus_transport_get_is_connected (connection->transport);
4105 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
4106 DISPATCH_STATUS_NAME (status), is_connected);
4110 /* It's possible this would be better done by having an explicit
4111 * notification from _dbus_transport_disconnect() that would
4112 * synchronously do this, instead of waiting for the next dispatch
4113 * status check. However, probably not good to change until it causes
4116 notify_disconnected_unlocked (connection);
4118 /* I'm not sure this is needed; the idea is that we want to
4119 * queue the Disconnected only after we've read all the
4120 * messages, but if we're disconnected maybe we are guaranteed
4121 * to have read them all ?
4123 if (status == DBUS_DISPATCH_COMPLETE)
4124 status = notify_disconnected_and_dispatch_complete_unlocked (connection);
4127 if (status != DBUS_DISPATCH_COMPLETE)
4129 else if (connection->n_incoming > 0)
4130 return DBUS_DISPATCH_DATA_REMAINS;
4132 return DBUS_DISPATCH_COMPLETE;
4137 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
4138 DBusDispatchStatus new_status)
4140 dbus_bool_t changed;
4141 DBusDispatchStatusFunction function;
4144 HAVE_LOCK_CHECK (connection);
4146 _dbus_connection_ref_unlocked (connection);
4148 changed = new_status != connection->last_dispatch_status;
4150 connection->last_dispatch_status = new_status;
4152 function = connection->dispatch_status_function;
4153 data = connection->dispatch_status_data;
4155 if (connection->disconnected_message_arrived &&
4156 !connection->disconnected_message_processed)
4158 connection->disconnected_message_processed = TRUE;
4160 /* this does an unref, but we have a ref
4161 * so we should not run the finalizer here
4164 connection_forget_shared_unlocked (connection);
4166 if (connection->exit_on_disconnect)
4168 CONNECTION_UNLOCK (connection);
4170 _dbus_verbose ("Exiting on Disconnected signal\n");
4172 _dbus_assert_not_reached ("Call to exit() returned");
4176 /* We drop the lock */
4177 CONNECTION_UNLOCK (connection);
4179 if (changed && function)
4181 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
4182 connection, new_status,
4183 DISPATCH_STATUS_NAME (new_status));
4184 (* function) (connection, new_status, data);
4187 dbus_connection_unref (connection);
4191 * Gets the current state of the incoming message queue.
4192 * #DBUS_DISPATCH_DATA_REMAINS indicates that the message queue
4193 * may contain messages. #DBUS_DISPATCH_COMPLETE indicates that the
4194 * incoming queue is empty. #DBUS_DISPATCH_NEED_MEMORY indicates that
4195 * there could be data, but we can't know for sure without more
4198 * To process the incoming message queue, use dbus_connection_dispatch()
4199 * or (in rare cases) dbus_connection_pop_message().
4201 * Note, #DBUS_DISPATCH_DATA_REMAINS really means that either we
4202 * have messages in the queue, or we have raw bytes buffered up
4203 * that need to be parsed. When these bytes are parsed, they
4204 * may not add up to an entire message. Thus, it's possible
4205 * to see a status of #DBUS_DISPATCH_DATA_REMAINS but not
4206 * have a message yet.
4208 * In particular this happens on initial connection, because all sorts
4209 * of authentication protocol stuff has to be parsed before the
4210 * first message arrives.
4212 * @param connection the connection.
4213 * @returns current dispatch status
4216 dbus_connection_get_dispatch_status (DBusConnection *connection)
4218 DBusDispatchStatus status;
4220 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4222 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
4224 CONNECTION_LOCK (connection);
4226 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4228 CONNECTION_UNLOCK (connection);
4234 * Filter funtion for handling the Peer standard interface.
4236 static DBusHandlerResult
4237 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
4238 DBusMessage *message)
4240 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
4242 /* This means we're letting the bus route this message */
4243 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4245 else if (dbus_message_is_method_call (message,
4246 DBUS_INTERFACE_PEER,
4252 ret = dbus_message_new_method_return (message);
4254 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4256 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4258 dbus_message_unref (ret);
4261 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4263 return DBUS_HANDLER_RESULT_HANDLED;
4265 else if (dbus_message_is_method_call (message,
4266 DBUS_INTERFACE_PEER,
4273 ret = dbus_message_new_method_return (message);
4275 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4278 _dbus_string_init (&uuid);
4279 if (_dbus_get_local_machine_uuid_encoded (&uuid))
4281 const char *v_STRING = _dbus_string_get_const_data (&uuid);
4282 if (dbus_message_append_args (ret,
4283 DBUS_TYPE_STRING, &v_STRING,
4286 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4289 _dbus_string_free (&uuid);
4291 dbus_message_unref (ret);
4294 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4296 return DBUS_HANDLER_RESULT_HANDLED;
4298 else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
4300 /* We need to bounce anything else with this interface, otherwise apps
4301 * could start extending the interface and when we added extensions
4302 * here to DBusConnection we'd break those apps.
4308 ret = dbus_message_new_error (message,
4309 DBUS_ERROR_UNKNOWN_METHOD,
4310 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
4312 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4314 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4316 dbus_message_unref (ret);
4319 return DBUS_HANDLER_RESULT_NEED_MEMORY;
4321 return DBUS_HANDLER_RESULT_HANDLED;
4325 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4330 * Processes all builtin filter functions
4332 * If the spec specifies a standard interface
4333 * they should be processed from this method
4335 static DBusHandlerResult
4336 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
4337 DBusMessage *message)
4339 /* We just run one filter for now but have the option to run more
4340 if the spec calls for it in the future */
4342 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
4346 * Processes any incoming data.
4348 * If there's incoming raw data that has not yet been parsed, it is
4349 * parsed, which may or may not result in adding messages to the
4352 * The incoming data buffer is filled when the connection reads from
4353 * its underlying transport (such as a socket). Reading usually
4354 * happens in dbus_watch_handle() or dbus_connection_read_write().
4356 * If there are complete messages in the incoming queue,
4357 * dbus_connection_dispatch() removes one message from the queue and
4358 * processes it. Processing has three steps.
4360 * First, any method replies are passed to #DBusPendingCall or
4361 * dbus_connection_send_with_reply_and_block() in order to
4362 * complete the pending method call.
4364 * Second, any filters registered with dbus_connection_add_filter()
4365 * are run. If any filter returns #DBUS_HANDLER_RESULT_HANDLED
4366 * then processing stops after that filter.
4368 * Third, if the message is a method call it is forwarded to
4369 * any registered object path handlers added with
4370 * dbus_connection_register_object_path() or
4371 * dbus_connection_register_fallback().
4373 * A single call to dbus_connection_dispatch() will process at most
4374 * one message; it will not clear the entire message queue.
4376 * Be careful about calling dbus_connection_dispatch() from inside a
4377 * message handler, i.e. calling dbus_connection_dispatch()
4378 * recursively. If threads have been initialized with a recursive
4379 * mutex function, then this will not deadlock; however, it can
4380 * certainly confuse your application.
4382 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
4384 * @param connection the connection
4385 * @returns dispatch status, see dbus_connection_get_dispatch_status()
4388 dbus_connection_dispatch (DBusConnection *connection)
4390 DBusMessage *message;
4391 DBusList *link, *filter_list_copy, *message_link;
4392 DBusHandlerResult result;
4393 DBusPendingCall *pending;
4394 dbus_int32_t reply_serial;
4395 DBusDispatchStatus status;
4397 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4399 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
4401 CONNECTION_LOCK (connection);
4402 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4403 if (status != DBUS_DISPATCH_DATA_REMAINS)
4405 /* unlocks and calls out to user code */
4406 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4410 /* We need to ref the connection since the callback could potentially
4411 * drop the last ref to it
4413 _dbus_connection_ref_unlocked (connection);
4415 _dbus_connection_acquire_dispatch (connection);
4416 HAVE_LOCK_CHECK (connection);
4418 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4419 if (message_link == NULL)
4421 /* another thread dispatched our stuff */
4423 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
4425 _dbus_connection_release_dispatch (connection);
4427 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4429 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4431 dbus_connection_unref (connection);
4436 message = message_link->data;
4438 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
4440 dbus_message_get_type (message),
4441 dbus_message_get_interface (message) ?
4442 dbus_message_get_interface (message) :
4444 dbus_message_get_member (message) ?
4445 dbus_message_get_member (message) :
4447 dbus_message_get_signature (message));
4449 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4451 /* Pending call handling must be first, because if you do
4452 * dbus_connection_send_with_reply_and_block() or
4453 * dbus_pending_call_block() then no handlers/filters will be run on
4454 * the reply. We want consistent semantics in the case where we
4455 * dbus_connection_dispatch() the reply.
4458 reply_serial = dbus_message_get_reply_serial (message);
4459 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4463 _dbus_verbose ("Dispatching a pending reply\n");
4464 complete_pending_call_and_unlock (connection, pending, message);
4465 pending = NULL; /* it's probably unref'd */
4467 CONNECTION_LOCK (connection);
4468 _dbus_verbose ("pending call completed in dispatch\n");
4469 result = DBUS_HANDLER_RESULT_HANDLED;
4473 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4474 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4477 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4479 _dbus_connection_release_dispatch (connection);
4480 HAVE_LOCK_CHECK (connection);
4482 _dbus_connection_failed_pop (connection, message_link);
4484 /* unlocks and calls user code */
4485 _dbus_connection_update_dispatch_status_and_unlock (connection,
4486 DBUS_DISPATCH_NEED_MEMORY);
4489 dbus_pending_call_unref (pending);
4490 dbus_connection_unref (connection);
4492 return DBUS_DISPATCH_NEED_MEMORY;
4495 _dbus_list_foreach (&filter_list_copy,
4496 (DBusForeachFunction)_dbus_message_filter_ref,
4499 /* We're still protected from dispatch() reentrancy here
4500 * since we acquired the dispatcher
4502 CONNECTION_UNLOCK (connection);
4504 link = _dbus_list_get_first_link (&filter_list_copy);
4505 while (link != NULL)
4507 DBusMessageFilter *filter = link->data;
4508 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
4510 if (filter->function == NULL)
4512 _dbus_verbose (" filter was removed in a callback function\n");
4517 _dbus_verbose (" running filter on message %p\n", message);
4518 result = (* filter->function) (connection, message, filter->user_data);
4520 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4526 _dbus_list_foreach (&filter_list_copy,
4527 (DBusForeachFunction)_dbus_message_filter_unref,
4529 _dbus_list_clear (&filter_list_copy);
4531 CONNECTION_LOCK (connection);
4533 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4535 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
4538 else if (result == DBUS_HANDLER_RESULT_HANDLED)
4540 _dbus_verbose ("filter handled message in dispatch\n");
4544 /* We're still protected from dispatch() reentrancy here
4545 * since we acquired the dispatcher
4547 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
4549 dbus_message_get_type (message),
4550 dbus_message_get_interface (message) ?
4551 dbus_message_get_interface (message) :
4553 dbus_message_get_member (message) ?
4554 dbus_message_get_member (message) :
4556 dbus_message_get_signature (message));
4558 HAVE_LOCK_CHECK (connection);
4559 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4562 CONNECTION_LOCK (connection);
4564 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
4566 _dbus_verbose ("object tree handled message in dispatch\n");
4570 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
4574 DBusPreallocatedSend *preallocated;
4576 _dbus_verbose (" sending error %s\n",
4577 DBUS_ERROR_UNKNOWN_METHOD);
4579 if (!_dbus_string_init (&str))
4581 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4582 _dbus_verbose ("no memory for error string in dispatch\n");
4586 if (!_dbus_string_append_printf (&str,
4587 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
4588 dbus_message_get_member (message),
4589 dbus_message_get_signature (message),
4590 dbus_message_get_interface (message)))
4592 _dbus_string_free (&str);
4593 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4594 _dbus_verbose ("no memory for error string in dispatch\n");
4598 reply = dbus_message_new_error (message,
4599 DBUS_ERROR_UNKNOWN_METHOD,
4600 _dbus_string_get_const_data (&str));
4601 _dbus_string_free (&str);
4605 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4606 _dbus_verbose ("no memory for error reply in dispatch\n");
4610 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4612 if (preallocated == NULL)
4614 dbus_message_unref (reply);
4615 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
4616 _dbus_verbose ("no memory for error send in dispatch\n");
4620 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4623 dbus_message_unref (reply);
4625 result = DBUS_HANDLER_RESULT_HANDLED;
4628 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
4629 dbus_message_get_type (message),
4630 dbus_message_get_interface (message) ?
4631 dbus_message_get_interface (message) :
4633 dbus_message_get_member (message) ?
4634 dbus_message_get_member (message) :
4636 dbus_message_get_signature (message),
4640 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4642 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
4644 /* Put message back, and we'll start over.
4645 * Yes this means handlers must be idempotent if they
4646 * don't return HANDLED; c'est la vie.
4648 _dbus_connection_putback_message_link_unlocked (connection,
4653 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
4655 _dbus_list_free_link (message_link);
4656 dbus_message_unref (message); /* don't want the message to count in max message limits
4657 * in computing dispatch status below
4661 _dbus_connection_release_dispatch (connection);
4662 HAVE_LOCK_CHECK (connection);
4664 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
4665 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4667 /* unlocks and calls user code */
4668 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4670 dbus_connection_unref (connection);
4676 * Sets the watch functions for the connection. These functions are
4677 * responsible for making the application's main loop aware of file
4678 * descriptors that need to be monitored for events, using select() or
4679 * poll(). When using Qt, typically the DBusAddWatchFunction would
4680 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
4681 * could call g_io_add_watch(), or could be used as part of a more
4682 * elaborate GSource. Note that when a watch is added, it may
4685 * The DBusWatchToggledFunction notifies the application that the
4686 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
4687 * to check this. A disabled watch should have no effect, and enabled
4688 * watch should be added to the main loop. This feature is used
4689 * instead of simply adding/removing the watch because
4690 * enabling/disabling can be done without memory allocation. The
4691 * toggled function may be NULL if a main loop re-queries
4692 * dbus_watch_get_enabled() every time anyway.
4694 * The DBusWatch can be queried for the file descriptor to watch using
4695 * dbus_watch_get_unix_fd() or dbus_watch_get_socket(), and for the
4696 * events to watch for using dbus_watch_get_flags(). The flags
4697 * returned by dbus_watch_get_flags() will only contain
4698 * DBUS_WATCH_READABLE and DBUS_WATCH_WRITABLE, never
4699 * DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; all watches implicitly
4700 * include a watch for hangups, errors, and other exceptional
4703 * Once a file descriptor becomes readable or writable, or an exception
4704 * occurs, dbus_watch_handle() should be called to
4705 * notify the connection of the file descriptor's condition.
4707 * dbus_watch_handle() cannot be called during the
4708 * DBusAddWatchFunction, as the connection will not be ready to handle
4711 * It is not allowed to reference a DBusWatch after it has been passed
4712 * to remove_function.
4714 * If #FALSE is returned due to lack of memory, the failure may be due
4715 * to a #FALSE return from the new add_function. If so, the
4716 * add_function may have been called successfully one or more times,
4717 * but the remove_function will also have been called to remove any
4718 * successful adds. i.e. if #FALSE is returned the net result
4719 * should be that dbus_connection_set_watch_functions() has no effect,
4720 * but the add_function and remove_function may have been called.
4722 * @todo We need to drop the lock when we call the
4723 * add/remove/toggled functions which can be a side effect
4724 * of setting the watch functions.
4726 * @param connection the connection.
4727 * @param add_function function to begin monitoring a new descriptor.
4728 * @param remove_function function to stop monitoring a descriptor.
4729 * @param toggled_function function to notify of enable/disable
4730 * @param data data to pass to add_function and remove_function.
4731 * @param free_data_function function to be called to free the data.
4732 * @returns #FALSE on failure (no memory)
4735 dbus_connection_set_watch_functions (DBusConnection *connection,
4736 DBusAddWatchFunction add_function,
4737 DBusRemoveWatchFunction remove_function,
4738 DBusWatchToggledFunction toggled_function,
4740 DBusFreeFunction free_data_function)
4743 DBusWatchList *watches;
4745 _dbus_return_val_if_fail (connection != NULL, FALSE);
4747 CONNECTION_LOCK (connection);
4749 #ifndef DBUS_DISABLE_CHECKS
4750 if (connection->watches == NULL)
4752 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4753 _DBUS_FUNCTION_NAME);
4758 /* ref connection for slightly better reentrancy */
4759 _dbus_connection_ref_unlocked (connection);
4761 /* This can call back into user code, and we need to drop the
4762 * connection lock when it does. This is kind of a lame
4765 watches = connection->watches;
4766 connection->watches = NULL;
4767 CONNECTION_UNLOCK (connection);
4769 retval = _dbus_watch_list_set_functions (watches,
4770 add_function, remove_function,
4772 data, free_data_function);
4773 CONNECTION_LOCK (connection);
4774 connection->watches = watches;
4776 CONNECTION_UNLOCK (connection);
4777 /* drop our paranoid refcount */
4778 dbus_connection_unref (connection);
4784 * Sets the timeout functions for the connection. These functions are
4785 * responsible for making the application's main loop aware of timeouts.
4786 * When using Qt, typically the DBusAddTimeoutFunction would create a
4787 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
4790 * The DBusTimeoutToggledFunction notifies the application that the
4791 * timeout has been enabled or disabled. Call
4792 * dbus_timeout_get_enabled() to check this. A disabled timeout should
4793 * have no effect, and enabled timeout should be added to the main
4794 * loop. This feature is used instead of simply adding/removing the
4795 * timeout because enabling/disabling can be done without memory
4796 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
4797 * to enable and disable. The toggled function may be NULL if a main
4798 * loop re-queries dbus_timeout_get_enabled() every time anyway.
4799 * Whenever a timeout is toggled, its interval may change.
4801 * The DBusTimeout can be queried for the timer interval using
4802 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
4803 * repeatedly, each time the interval elapses, starting after it has
4804 * elapsed once. The timeout stops firing when it is removed with the
4805 * given remove_function. The timer interval may change whenever the
4806 * timeout is added, removed, or toggled.
4808 * @param connection the connection.
4809 * @param add_function function to add a timeout.
4810 * @param remove_function function to remove a timeout.
4811 * @param toggled_function function to notify of enable/disable
4812 * @param data data to pass to add_function and remove_function.
4813 * @param free_data_function function to be called to free the data.
4814 * @returns #FALSE on failure (no memory)
4817 dbus_connection_set_timeout_functions (DBusConnection *connection,
4818 DBusAddTimeoutFunction add_function,
4819 DBusRemoveTimeoutFunction remove_function,
4820 DBusTimeoutToggledFunction toggled_function,
4822 DBusFreeFunction free_data_function)
4825 DBusTimeoutList *timeouts;
4827 _dbus_return_val_if_fail (connection != NULL, FALSE);
4829 CONNECTION_LOCK (connection);
4831 #ifndef DBUS_DISABLE_CHECKS
4832 if (connection->timeouts == NULL)
4834 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4835 _DBUS_FUNCTION_NAME);
4840 /* ref connection for slightly better reentrancy */
4841 _dbus_connection_ref_unlocked (connection);
4843 timeouts = connection->timeouts;
4844 connection->timeouts = NULL;
4845 CONNECTION_UNLOCK (connection);
4847 retval = _dbus_timeout_list_set_functions (timeouts,
4848 add_function, remove_function,
4850 data, free_data_function);
4851 CONNECTION_LOCK (connection);
4852 connection->timeouts = timeouts;
4854 CONNECTION_UNLOCK (connection);
4855 /* drop our paranoid refcount */
4856 dbus_connection_unref (connection);
4862 * Sets the mainloop wakeup function for the connection. This function
4863 * is responsible for waking up the main loop (if its sleeping in
4864 * another thread) when some some change has happened to the
4865 * connection that the mainloop needs to reconsider (e.g. a message
4866 * has been queued for writing). When using Qt, this typically
4867 * results in a call to QEventLoop::wakeUp(). When using GLib, it
4868 * would call g_main_context_wakeup().
4870 * @param connection the connection.
4871 * @param wakeup_main_function function to wake up the mainloop
4872 * @param data data to pass wakeup_main_function
4873 * @param free_data_function function to be called to free the data.
4876 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
4877 DBusWakeupMainFunction wakeup_main_function,
4879 DBusFreeFunction free_data_function)
4882 DBusFreeFunction old_free_data;
4884 _dbus_return_if_fail (connection != NULL);
4886 CONNECTION_LOCK (connection);
4887 old_data = connection->wakeup_main_data;
4888 old_free_data = connection->free_wakeup_main_data;
4890 connection->wakeup_main_function = wakeup_main_function;
4891 connection->wakeup_main_data = data;
4892 connection->free_wakeup_main_data = free_data_function;
4894 CONNECTION_UNLOCK (connection);
4896 /* Callback outside the lock */
4898 (*old_free_data) (old_data);
4902 * Set a function to be invoked when the dispatch status changes.
4903 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
4904 * dbus_connection_dispatch() needs to be called to process incoming
4905 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
4906 * from inside the DBusDispatchStatusFunction. Indeed, almost
4907 * any reentrancy in this function is a bad idea. Instead,
4908 * the DBusDispatchStatusFunction should simply save an indication
4909 * that messages should be dispatched later, when the main loop
4912 * If you don't set a dispatch status function, you have to be sure to
4913 * dispatch on every iteration of your main loop, especially if
4914 * dbus_watch_handle() or dbus_timeout_handle() were called.
4916 * @param connection the connection
4917 * @param function function to call on dispatch status changes
4918 * @param data data for function
4919 * @param free_data_function free the function data
4922 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
4923 DBusDispatchStatusFunction function,
4925 DBusFreeFunction free_data_function)
4928 DBusFreeFunction old_free_data;
4930 _dbus_return_if_fail (connection != NULL);
4932 CONNECTION_LOCK (connection);
4933 old_data = connection->dispatch_status_data;
4934 old_free_data = connection->free_dispatch_status_data;
4936 connection->dispatch_status_function = function;
4937 connection->dispatch_status_data = data;
4938 connection->free_dispatch_status_data = free_data_function;
4940 CONNECTION_UNLOCK (connection);
4942 /* Callback outside the lock */
4944 (*old_free_data) (old_data);
4948 * Get the UNIX file descriptor of the connection, if any. This can
4949 * be used for SELinux access control checks with getpeercon() for
4950 * example. DO NOT read or write to the file descriptor, or try to
4951 * select() on it; use DBusWatch for main loop integration. Not all
4952 * connections will have a file descriptor. So for adding descriptors
4953 * to the main loop, use dbus_watch_get_unix_fd() and so forth.
4955 * If the connection is socket-based, you can also use
4956 * dbus_connection_get_socket(), which will work on Windows too.
4957 * This function always fails on Windows.
4959 * Right now the returned descriptor is always a socket, but
4960 * that is not guaranteed.
4962 * @param connection the connection
4963 * @param fd return location for the file descriptor.
4964 * @returns #TRUE if fd is successfully obtained.
4967 dbus_connection_get_unix_fd (DBusConnection *connection,
4970 _dbus_return_val_if_fail (connection != NULL, FALSE);
4971 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4974 /* FIXME do this on a lower level */
4978 return dbus_connection_get_socket(connection, fd);
4982 * Gets the underlying Windows or UNIX socket file descriptor
4983 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
4984 * select() on it; use DBusWatch for main loop integration. Not all
4985 * connections will have a socket. So for adding descriptors
4986 * to the main loop, use dbus_watch_get_socket() and so forth.
4988 * If the connection is not socket-based, this function will return FALSE,
4989 * even if the connection does have a file descriptor of some kind.
4990 * i.e. this function always returns specifically a socket file descriptor.
4992 * @param connection the connection
4993 * @param fd return location for the file descriptor.
4994 * @returns #TRUE if fd is successfully obtained.
4997 dbus_connection_get_socket(DBusConnection *connection,
5002 _dbus_return_val_if_fail (connection != NULL, FALSE);
5003 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
5005 CONNECTION_LOCK (connection);
5007 retval = _dbus_transport_get_socket_fd (connection->transport,
5010 CONNECTION_UNLOCK (connection);
5017 * Gets the UNIX user ID of the connection if known. Returns #TRUE if
5018 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
5019 * for now, though in theory someone could hook Windows to NIS or
5020 * something. Always returns #FALSE prior to authenticating the
5023 * The UID is only read by servers from clients; clients can't usually
5024 * get the UID of servers, because servers do not authenticate to
5025 * clients. The returned UID is the UID the connection authenticated
5028 * The message bus is a server and the apps connecting to the bus
5031 * You can ask the bus to tell you the UID of another connection though
5032 * if you like; this is done with dbus_bus_get_unix_user().
5034 * @param connection the connection
5035 * @param uid return location for the user ID
5036 * @returns #TRUE if uid is filled in with a valid user ID
5039 dbus_connection_get_unix_user (DBusConnection *connection,
5044 _dbus_return_val_if_fail (connection != NULL, FALSE);
5045 _dbus_return_val_if_fail (uid != NULL, FALSE);
5047 CONNECTION_LOCK (connection);
5049 if (!_dbus_transport_get_is_authenticated (connection->transport))
5052 result = _dbus_transport_get_unix_user (connection->transport,
5056 _dbus_assert (!result);
5059 CONNECTION_UNLOCK (connection);
5065 * Gets the process ID of the connection if any.
5066 * Returns #TRUE if the pid is filled in.
5067 * Always returns #FALSE prior to authenticating the
5070 * @param connection the connection
5071 * @param pid return location for the process ID
5072 * @returns #TRUE if uid is filled in with a valid process ID
5075 dbus_connection_get_unix_process_id (DBusConnection *connection,
5080 _dbus_return_val_if_fail (connection != NULL, FALSE);
5081 _dbus_return_val_if_fail (pid != NULL, FALSE);
5083 CONNECTION_LOCK (connection);
5085 if (!_dbus_transport_get_is_authenticated (connection->transport))
5088 result = _dbus_transport_get_unix_process_id (connection->transport,
5091 _dbus_assert (!result);
5094 CONNECTION_UNLOCK (connection);
5100 * Gets the ADT audit data of the connection if any.
5101 * Returns #TRUE if the structure pointer is returned.
5102 * Always returns #FALSE prior to authenticating the
5105 * @param connection the connection
5106 * @param data return location for audit data
5107 * @returns #TRUE if audit data is filled in with a valid ucred pointer
5110 dbus_connection_get_adt_audit_session_data (DBusConnection *connection,
5112 dbus_int32_t *data_size)
5116 _dbus_return_val_if_fail (connection != NULL, FALSE);
5117 _dbus_return_val_if_fail (data != NULL, FALSE);
5118 _dbus_return_val_if_fail (data_size != NULL, FALSE);
5120 CONNECTION_LOCK (connection);
5122 if (!_dbus_transport_get_is_authenticated (connection->transport))
5125 result = _dbus_transport_get_adt_audit_session_data (connection->transport,
5128 CONNECTION_UNLOCK (connection);
5134 * Sets a predicate function used to determine whether a given user ID
5135 * is allowed to connect. When an incoming connection has
5136 * authenticated with a particular user ID, this function is called;
5137 * if it returns #TRUE, the connection is allowed to proceed,
5138 * otherwise the connection is disconnected.
5140 * If the function is set to #NULL (as it is by default), then
5141 * only the same UID as the server process will be allowed to
5142 * connect. Also, root is always allowed to connect.
5144 * On Windows, the function will be set and its free_data_function will
5145 * be invoked when the connection is freed or a new function is set.
5146 * However, the function will never be called, because there are
5147 * no UNIX user ids to pass to it, or at least none of the existing
5148 * auth protocols would allow authenticating as a UNIX user on Windows.
5150 * @param connection the connection
5151 * @param function the predicate
5152 * @param data data to pass to the predicate
5153 * @param free_data_function function to free the data
5156 dbus_connection_set_unix_user_function (DBusConnection *connection,
5157 DBusAllowUnixUserFunction function,
5159 DBusFreeFunction free_data_function)
5161 void *old_data = NULL;
5162 DBusFreeFunction old_free_function = NULL;
5164 _dbus_return_if_fail (connection != NULL);
5166 CONNECTION_LOCK (connection);
5167 _dbus_transport_set_unix_user_function (connection->transport,
5168 function, data, free_data_function,
5169 &old_data, &old_free_function);
5170 CONNECTION_UNLOCK (connection);
5172 if (old_free_function != NULL)
5173 (* old_free_function) (old_data);
5177 * Gets the Windows user SID of the connection if known. Returns
5178 * #TRUE if the ID is filled in. Always returns #FALSE on non-Windows
5179 * platforms for now, though in theory someone could hook UNIX to
5180 * Active Directory or something. Always returns #FALSE prior to
5181 * authenticating the connection.
5183 * The user is only read by servers from clients; clients can't usually
5184 * get the user of servers, because servers do not authenticate to
5185 * clients. The returned user is the user the connection authenticated
5188 * The message bus is a server and the apps connecting to the bus
5191 * The returned user string has to be freed with dbus_free().
5193 * The return value indicates whether the user SID is available;
5194 * if it's available but we don't have the memory to copy it,
5195 * then the return value is #TRUE and #NULL is given as the SID.
5197 * @todo We would like to be able to say "You can ask the bus to tell
5198 * you the user of another connection though if you like; this is done
5199 * with dbus_bus_get_windows_user()." But this has to be implemented
5200 * in bus/driver.c and dbus/dbus-bus.c, and is pointless anyway
5201 * since on Windows we only use the session bus for now.
5203 * @param connection the connection
5204 * @param windows_sid_p return location for an allocated copy of the user ID, or #NULL if no memory
5205 * @returns #TRUE if user is available (returned value may be #NULL anyway if no memory)
5208 dbus_connection_get_windows_user (DBusConnection *connection,
5209 char **windows_sid_p)
5213 _dbus_return_val_if_fail (connection != NULL, FALSE);
5214 _dbus_return_val_if_fail (windows_sid_p != NULL, FALSE);
5216 CONNECTION_LOCK (connection);
5218 if (!_dbus_transport_get_is_authenticated (connection->transport))
5221 result = _dbus_transport_get_windows_user (connection->transport,
5225 _dbus_assert (!result);
5228 CONNECTION_UNLOCK (connection);
5234 * Sets a predicate function used to determine whether a given user ID
5235 * is allowed to connect. When an incoming connection has
5236 * authenticated with a particular user ID, this function is called;
5237 * if it returns #TRUE, the connection is allowed to proceed,
5238 * otherwise the connection is disconnected.
5240 * If the function is set to #NULL (as it is by default), then
5241 * only the same user owning the server process will be allowed to
5244 * On UNIX, the function will be set and its free_data_function will
5245 * be invoked when the connection is freed or a new function is set.
5246 * However, the function will never be called, because there is no
5247 * way right now to authenticate as a Windows user on UNIX.
5249 * @param connection the connection
5250 * @param function the predicate
5251 * @param data data to pass to the predicate
5252 * @param free_data_function function to free the data
5255 dbus_connection_set_windows_user_function (DBusConnection *connection,
5256 DBusAllowWindowsUserFunction function,
5258 DBusFreeFunction free_data_function)
5260 void *old_data = NULL;
5261 DBusFreeFunction old_free_function = NULL;
5263 _dbus_return_if_fail (connection != NULL);
5265 CONNECTION_LOCK (connection);
5266 _dbus_transport_set_windows_user_function (connection->transport,
5267 function, data, free_data_function,
5268 &old_data, &old_free_function);
5269 CONNECTION_UNLOCK (connection);
5271 if (old_free_function != NULL)
5272 (* old_free_function) (old_data);
5276 * This function must be called on the server side of a connection when the
5277 * connection is first seen in the #DBusNewConnectionFunction. If set to
5278 * #TRUE (the default is #FALSE), then the connection can proceed even if
5279 * the client does not authenticate as some user identity, i.e. clients
5280 * can connect anonymously.
5282 * This setting interacts with the available authorization mechanisms
5283 * (see dbus_server_set_auth_mechanisms()). Namely, an auth mechanism
5284 * such as ANONYMOUS that supports anonymous auth must be included in
5285 * the list of available mechanisms for anonymous login to work.
5287 * This setting also changes the default rule for connections
5288 * authorized as a user; normally, if a connection authorizes as
5289 * a user identity, it is permitted if the user identity is
5290 * root or the user identity matches the user identity of the server
5291 * process. If anonymous connections are allowed, however,
5292 * then any user identity is allowed.
5294 * You can override the rules for connections authorized as a
5295 * user identity with dbus_connection_set_unix_user_function()
5296 * and dbus_connection_set_windows_user_function().
5298 * @param connection the connection
5299 * @param value whether to allow authentication as an anonymous user
5302 dbus_connection_set_allow_anonymous (DBusConnection *connection,
5305 _dbus_return_if_fail (connection != NULL);
5307 CONNECTION_LOCK (connection);
5308 _dbus_transport_set_allow_anonymous (connection->transport, value);
5309 CONNECTION_UNLOCK (connection);
5314 * Normally #DBusConnection automatically handles all messages to the
5315 * org.freedesktop.DBus.Peer interface. However, the message bus wants
5316 * to be able to route methods on that interface through the bus and
5317 * to other applications. If routing peer messages is enabled, then
5318 * messages with the org.freedesktop.DBus.Peer interface that also
5319 * have a bus destination name set will not be automatically
5320 * handled by the #DBusConnection and instead will be dispatched
5321 * normally to the application.
5323 * If a normal application sets this flag, it can break things badly.
5324 * So don't set this unless you are the message bus.
5326 * @param connection the connection
5327 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set
5330 dbus_connection_set_route_peer_messages (DBusConnection *connection,
5333 _dbus_return_if_fail (connection != NULL);
5335 CONNECTION_LOCK (connection);
5336 connection->route_peer_messages = TRUE;
5337 CONNECTION_UNLOCK (connection);
5341 * Adds a message filter. Filters are handlers that are run on all
5342 * incoming messages, prior to the objects registered with
5343 * dbus_connection_register_object_path(). Filters are run in the
5344 * order that they were added. The same handler can be added as a
5345 * filter more than once, in which case it will be run more than once.
5346 * Filters added during a filter callback won't be run on the message
5349 * @todo we don't run filters on messages while blocking without
5350 * entering the main loop, since filters are run as part of
5351 * dbus_connection_dispatch(). This is probably a feature, as filters
5352 * could create arbitrary reentrancy. But kind of sucks if you're
5353 * trying to filter METHOD_RETURN for some reason.
5355 * @param connection the connection
5356 * @param function function to handle messages
5357 * @param user_data user data to pass to the function
5358 * @param free_data_function function to use for freeing user data
5359 * @returns #TRUE on success, #FALSE if not enough memory.
5362 dbus_connection_add_filter (DBusConnection *connection,
5363 DBusHandleMessageFunction function,
5365 DBusFreeFunction free_data_function)
5367 DBusMessageFilter *filter;
5369 _dbus_return_val_if_fail (connection != NULL, FALSE);
5370 _dbus_return_val_if_fail (function != NULL, FALSE);
5372 filter = dbus_new0 (DBusMessageFilter, 1);
5376 filter->refcount.value = 1;
5378 CONNECTION_LOCK (connection);
5380 if (!_dbus_list_append (&connection->filter_list,
5383 _dbus_message_filter_unref (filter);
5384 CONNECTION_UNLOCK (connection);
5388 /* Fill in filter after all memory allocated,
5389 * so we don't run the free_user_data_function
5390 * if the add_filter() fails
5393 filter->function = function;
5394 filter->user_data = user_data;
5395 filter->free_user_data_function = free_data_function;
5397 CONNECTION_UNLOCK (connection);
5402 * Removes a previously-added message filter. It is a programming
5403 * error to call this function for a handler that has not been added
5404 * as a filter. If the given handler was added more than once, only
5405 * one instance of it will be removed (the most recently-added
5408 * @param connection the connection
5409 * @param function the handler to remove
5410 * @param user_data user data for the handler to remove
5414 dbus_connection_remove_filter (DBusConnection *connection,
5415 DBusHandleMessageFunction function,
5419 DBusMessageFilter *filter;
5421 _dbus_return_if_fail (connection != NULL);
5422 _dbus_return_if_fail (function != NULL);
5424 CONNECTION_LOCK (connection);
5428 link = _dbus_list_get_last_link (&connection->filter_list);
5429 while (link != NULL)
5431 filter = link->data;
5433 if (filter->function == function &&
5434 filter->user_data == user_data)
5436 _dbus_list_remove_link (&connection->filter_list, link);
5437 filter->function = NULL;
5442 link = _dbus_list_get_prev_link (&connection->filter_list, link);
5445 CONNECTION_UNLOCK (connection);
5447 #ifndef DBUS_DISABLE_CHECKS
5450 _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
5451 function, user_data);
5456 /* Call application code */
5457 if (filter->free_user_data_function)
5458 (* filter->free_user_data_function) (filter->user_data);
5460 filter->free_user_data_function = NULL;
5461 filter->user_data = NULL;
5463 _dbus_message_filter_unref (filter);
5467 * Registers a handler for a given path in the object hierarchy.
5468 * The given vtable handles messages sent to exactly the given path.
5470 * @param connection the connection
5471 * @param path a '/' delimited string of path elements
5472 * @param vtable the virtual table
5473 * @param user_data data to pass to functions in the vtable
5474 * @param error address where an error can be returned
5475 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5476 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
5479 dbus_connection_try_register_object_path (DBusConnection *connection,
5481 const DBusObjectPathVTable *vtable,
5485 char **decomposed_path;
5488 _dbus_return_val_if_fail (connection != NULL, FALSE);
5489 _dbus_return_val_if_fail (path != NULL, FALSE);
5490 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5491 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5493 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5496 CONNECTION_LOCK (connection);
5498 retval = _dbus_object_tree_register (connection->objects,
5500 (const char **) decomposed_path, vtable,
5503 CONNECTION_UNLOCK (connection);
5505 dbus_free_string_array (decomposed_path);
5511 * Registers a handler for a given path in the object hierarchy.
5512 * The given vtable handles messages sent to exactly the given path.
5514 * It is a bug to call this function for object paths which already
5515 * have a handler. Use dbus_connection_try_register_object_path() if this
5516 * might be the case.
5518 * @param connection the connection
5519 * @param path a '/' delimited string of path elements
5520 * @param vtable the virtual table
5521 * @param user_data data to pass to functions in the vtable
5522 * @returns #FALSE if not enough memory
5525 dbus_connection_register_object_path (DBusConnection *connection,
5527 const DBusObjectPathVTable *vtable,
5530 char **decomposed_path;
5532 DBusError error = DBUS_ERROR_INIT;
5534 _dbus_return_val_if_fail (connection != NULL, FALSE);
5535 _dbus_return_val_if_fail (path != NULL, FALSE);
5536 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5537 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5539 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5542 CONNECTION_LOCK (connection);
5544 retval = _dbus_object_tree_register (connection->objects,
5546 (const char **) decomposed_path, vtable,
5549 CONNECTION_UNLOCK (connection);
5551 dbus_free_string_array (decomposed_path);
5553 if (dbus_error_has_name (&error, DBUS_ERROR_ADDRESS_IN_USE))
5555 _dbus_warn ("%s\n", error.message);
5556 dbus_error_free (&error);
5564 * Registers a fallback handler for a given subsection of the object
5565 * hierarchy. The given vtable handles messages at or below the given
5566 * path. You can use this to establish a default message handling
5567 * policy for a whole "subdirectory."
5569 * @param connection the connection
5570 * @param path a '/' delimited string of path elements
5571 * @param vtable the virtual table
5572 * @param user_data data to pass to functions in the vtable
5573 * @param error address where an error can be returned
5574 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
5575 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
5578 dbus_connection_try_register_fallback (DBusConnection *connection,
5580 const DBusObjectPathVTable *vtable,
5584 char **decomposed_path;
5587 _dbus_return_val_if_fail (connection != NULL, FALSE);
5588 _dbus_return_val_if_fail (path != NULL, FALSE);
5589 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5590 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5592 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5595 CONNECTION_LOCK (connection);
5597 retval = _dbus_object_tree_register (connection->objects,
5599 (const char **) decomposed_path, vtable,
5602 CONNECTION_UNLOCK (connection);
5604 dbus_free_string_array (decomposed_path);
5610 * Registers a fallback handler for a given subsection of the object
5611 * hierarchy. The given vtable handles messages at or below the given
5612 * path. You can use this to establish a default message handling
5613 * policy for a whole "subdirectory."
5615 * It is a bug to call this function for object paths which already
5616 * have a handler. Use dbus_connection_try_register_fallback() if this
5617 * might be the case.
5619 * @param connection the connection
5620 * @param path a '/' delimited string of path elements
5621 * @param vtable the virtual table
5622 * @param user_data data to pass to functions in the vtable
5623 * @returns #FALSE if not enough memory
5626 dbus_connection_register_fallback (DBusConnection *connection,
5628 const DBusObjectPathVTable *vtable,
5631 char **decomposed_path;
5633 DBusError error = DBUS_ERROR_INIT;
5635 _dbus_return_val_if_fail (connection != NULL, FALSE);
5636 _dbus_return_val_if_fail (path != NULL, FALSE);
5637 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5638 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5640 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5643 CONNECTION_LOCK (connection);
5645 retval = _dbus_object_tree_register (connection->objects,
5647 (const char **) decomposed_path, vtable,
5650 CONNECTION_UNLOCK (connection);
5652 dbus_free_string_array (decomposed_path);
5654 if (dbus_error_has_name (&error, DBUS_ERROR_ADDRESS_IN_USE))
5656 _dbus_warn ("%s\n", error.message);
5657 dbus_error_free (&error);
5665 * Unregisters the handler registered with exactly the given path.
5666 * It's a bug to call this function for a path that isn't registered.
5667 * Can unregister both fallback paths and object paths.
5669 * @param connection the connection
5670 * @param path a '/' delimited string of path elements
5671 * @returns #FALSE if not enough memory
5674 dbus_connection_unregister_object_path (DBusConnection *connection,
5677 char **decomposed_path;
5679 _dbus_return_val_if_fail (connection != NULL, FALSE);
5680 _dbus_return_val_if_fail (path != NULL, FALSE);
5681 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5683 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5686 CONNECTION_LOCK (connection);
5688 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
5690 dbus_free_string_array (decomposed_path);
5696 * Gets the user data passed to dbus_connection_register_object_path()
5697 * or dbus_connection_register_fallback(). If nothing was registered
5698 * at this path, the data is filled in with #NULL.
5700 * @param connection the connection
5701 * @param path the path you registered with
5702 * @param data_p location to store the user data, or #NULL
5703 * @returns #FALSE if not enough memory
5706 dbus_connection_get_object_path_data (DBusConnection *connection,
5710 char **decomposed_path;
5712 _dbus_return_val_if_fail (connection != NULL, FALSE);
5713 _dbus_return_val_if_fail (path != NULL, FALSE);
5714 _dbus_return_val_if_fail (data_p != NULL, FALSE);
5718 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5721 CONNECTION_LOCK (connection);
5723 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
5725 CONNECTION_UNLOCK (connection);
5727 dbus_free_string_array (decomposed_path);
5733 * Lists the registered fallback handlers and object path handlers at
5734 * the given parent_path. The returned array should be freed with
5735 * dbus_free_string_array().
5737 * @param connection the connection
5738 * @param parent_path the path to list the child handlers of
5739 * @param child_entries returns #NULL-terminated array of children
5740 * @returns #FALSE if no memory to allocate the child entries
5743 dbus_connection_list_registered (DBusConnection *connection,
5744 const char *parent_path,
5745 char ***child_entries)
5747 char **decomposed_path;
5749 _dbus_return_val_if_fail (connection != NULL, FALSE);
5750 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
5751 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
5752 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
5754 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
5757 CONNECTION_LOCK (connection);
5759 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
5760 (const char **) decomposed_path,
5762 dbus_free_string_array (decomposed_path);
5767 static DBusDataSlotAllocator slot_allocator;
5768 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
5771 * Allocates an integer ID to be used for storing application-specific
5772 * data on any DBusConnection. The allocated ID may then be used
5773 * with dbus_connection_set_data() and dbus_connection_get_data().
5774 * The passed-in slot must be initialized to -1, and is filled in
5775 * with the slot ID. If the passed-in slot is not -1, it's assumed
5776 * to be already allocated, and its refcount is incremented.
5778 * The allocated slot is global, i.e. all DBusConnection objects will
5779 * have a slot with the given integer ID reserved.
5781 * @param slot_p address of a global variable storing the slot
5782 * @returns #FALSE on failure (no memory)
5785 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
5787 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5788 &_DBUS_LOCK_NAME (connection_slots),
5793 * Deallocates a global ID for connection data slots.
5794 * dbus_connection_get_data() and dbus_connection_set_data() may no
5795 * longer be used with this slot. Existing data stored on existing
5796 * DBusConnection objects will be freed when the connection is
5797 * finalized, but may not be retrieved (and may only be replaced if
5798 * someone else reallocates the slot). When the refcount on the
5799 * passed-in slot reaches 0, it is set to -1.
5801 * @param slot_p address storing the slot to deallocate
5804 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
5806 _dbus_return_if_fail (*slot_p >= 0);
5808 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5812 * Stores a pointer on a DBusConnection, along
5813 * with an optional function to be used for freeing
5814 * the data when the data is set again, or when
5815 * the connection is finalized. The slot number
5816 * must have been allocated with dbus_connection_allocate_data_slot().
5818 * @param connection the connection
5819 * @param slot the slot number
5820 * @param data the data to store
5821 * @param free_data_func finalizer function for the data
5822 * @returns #TRUE if there was enough memory to store the data
5825 dbus_connection_set_data (DBusConnection *connection,
5828 DBusFreeFunction free_data_func)
5830 DBusFreeFunction old_free_func;
5834 _dbus_return_val_if_fail (connection != NULL, FALSE);
5835 _dbus_return_val_if_fail (slot >= 0, FALSE);
5837 CONNECTION_LOCK (connection);
5839 retval = _dbus_data_slot_list_set (&slot_allocator,
5840 &connection->slot_list,
5841 slot, data, free_data_func,
5842 &old_free_func, &old_data);
5844 CONNECTION_UNLOCK (connection);
5848 /* Do the actual free outside the connection lock */
5850 (* old_free_func) (old_data);
5857 * Retrieves data previously set with dbus_connection_set_data().
5858 * The slot must still be allocated (must not have been freed).
5860 * @param connection the connection
5861 * @param slot the slot to get data from
5862 * @returns the data, or #NULL if not found
5865 dbus_connection_get_data (DBusConnection *connection,
5870 _dbus_return_val_if_fail (connection != NULL, NULL);
5872 CONNECTION_LOCK (connection);
5874 res = _dbus_data_slot_list_get (&slot_allocator,
5875 &connection->slot_list,
5878 CONNECTION_UNLOCK (connection);
5884 * This function sets a global flag for whether dbus_connection_new()
5885 * will set SIGPIPE behavior to SIG_IGN.
5887 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
5890 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
5892 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
5896 * Specifies the maximum size message this connection is allowed to
5897 * receive. Larger messages will result in disconnecting the
5900 * @param connection a #DBusConnection
5901 * @param size maximum message size the connection can receive, in bytes
5904 dbus_connection_set_max_message_size (DBusConnection *connection,
5907 _dbus_return_if_fail (connection != NULL);
5909 CONNECTION_LOCK (connection);
5910 _dbus_transport_set_max_message_size (connection->transport,
5912 CONNECTION_UNLOCK (connection);
5916 * Gets the value set by dbus_connection_set_max_message_size().
5918 * @param connection the connection
5919 * @returns the max size of a single message
5922 dbus_connection_get_max_message_size (DBusConnection *connection)
5926 _dbus_return_val_if_fail (connection != NULL, 0);
5928 CONNECTION_LOCK (connection);
5929 res = _dbus_transport_get_max_message_size (connection->transport);
5930 CONNECTION_UNLOCK (connection);
5935 * Sets the maximum total number of bytes that can be used for all messages
5936 * received on this connection. Messages count toward the maximum until
5937 * they are finalized. When the maximum is reached, the connection will
5938 * not read more data until some messages are finalized.
5940 * The semantics of the maximum are: if outstanding messages are
5941 * already above the maximum, additional messages will not be read.
5942 * The semantics are not: if the next message would cause us to exceed
5943 * the maximum, we don't read it. The reason is that we don't know the
5944 * size of a message until after we read it.
5946 * Thus, the max live messages size can actually be exceeded
5947 * by up to the maximum size of a single message.
5949 * Also, if we read say 1024 bytes off the wire in a single read(),
5950 * and that contains a half-dozen small messages, we may exceed the
5951 * size max by that amount. But this should be inconsequential.
5953 * This does imply that we can't call read() with a buffer larger
5954 * than we're willing to exceed this limit by.
5956 * @param connection the connection
5957 * @param size the maximum size in bytes of all outstanding messages
5960 dbus_connection_set_max_received_size (DBusConnection *connection,
5963 _dbus_return_if_fail (connection != NULL);
5965 CONNECTION_LOCK (connection);
5966 _dbus_transport_set_max_received_size (connection->transport,
5968 CONNECTION_UNLOCK (connection);
5972 * Gets the value set by dbus_connection_set_max_received_size().
5974 * @param connection the connection
5975 * @returns the max size of all live messages
5978 dbus_connection_get_max_received_size (DBusConnection *connection)
5982 _dbus_return_val_if_fail (connection != NULL, 0);
5984 CONNECTION_LOCK (connection);
5985 res = _dbus_transport_get_max_received_size (connection->transport);
5986 CONNECTION_UNLOCK (connection);
5991 * Gets the approximate size in bytes of all messages in the outgoing
5992 * message queue. The size is approximate in that you shouldn't use
5993 * it to decide how many bytes to read off the network or anything
5994 * of that nature, as optimizations may choose to tell small white lies
5995 * to avoid performance overhead.
5997 * @param connection the connection
5998 * @returns the number of bytes that have been queued up but not sent
6001 dbus_connection_get_outgoing_size (DBusConnection *connection)
6005 _dbus_return_val_if_fail (connection != NULL, 0);
6007 CONNECTION_LOCK (connection);
6008 res = _dbus_counter_get_value (connection->outgoing_counter);
6009 CONNECTION_UNLOCK (connection);