1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-shared.h"
26 #include "dbus-connection.h"
27 #include "dbus-list.h"
28 #include "dbus-timeout.h"
29 #include "dbus-transport.h"
30 #include "dbus-watch.h"
31 #include "dbus-connection-internal.h"
32 #include "dbus-pending-call-internal.h"
33 #include "dbus-list.h"
34 #include "dbus-hash.h"
35 #include "dbus-message-internal.h"
36 #include "dbus-threads.h"
37 #include "dbus-protocol.h"
38 #include "dbus-dataslot.h"
39 #include "dbus-string.h"
40 #include "dbus-pending-call.h"
41 #include "dbus-object-tree.h"
42 #include "dbus-threads-internal.h"
45 #ifdef DBUS_DISABLE_CHECKS
46 #define TOOK_LOCK_CHECK(connection)
47 #define RELEASING_LOCK_CHECK(connection)
48 #define HAVE_LOCK_CHECK(connection)
50 #define TOOK_LOCK_CHECK(connection) do { \
51 _dbus_assert (!(connection)->have_connection_lock); \
52 (connection)->have_connection_lock = TRUE; \
54 #define RELEASING_LOCK_CHECK(connection) do { \
55 _dbus_assert ((connection)->have_connection_lock); \
56 (connection)->have_connection_lock = FALSE; \
58 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
59 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
64 #define CONNECTION_LOCK(connection) do { \
65 if (TRACE_LOCKS) { _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); } \
66 _dbus_mutex_lock ((connection)->mutex); \
67 TOOK_LOCK_CHECK (connection); \
70 #define CONNECTION_UNLOCK(connection) do { \
71 if (TRACE_LOCKS) { _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); } \
72 RELEASING_LOCK_CHECK (connection); \
73 _dbus_mutex_unlock ((connection)->mutex); \
76 #define DISPATCH_STATUS_NAME(s) \
77 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
78 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
79 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
83 * @defgroup DBusConnection DBusConnection
85 * @brief Connection to another application
87 * A DBusConnection represents a connection to another
88 * application. Messages can be sent and received via this connection.
89 * The other application may be a message bus; for convenience, the
90 * function dbus_bus_get() is provided to automatically open a
91 * connection to the well-known message buses.
93 * In brief a DBusConnection is a message queue associated with some
94 * message transport mechanism such as a socket. The connection
95 * maintains a queue of incoming messages and a queue of outgoing
98 * Incoming messages are normally processed by calling
99 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
100 * handlers registered for the topmost message in the message queue,
101 * then discards the message, then returns.
103 * dbus_connection_get_dispatch_status() indicates whether
104 * messages are currently in the queue that need dispatching.
105 * dbus_connection_set_dispatch_status_function() allows
106 * you to set a function to be used to monitor the dispatch status.
108 * If you're using GLib or Qt add-on libraries for D-BUS, there are
109 * special convenience APIs in those libraries that hide
110 * all the details of dispatch and watch/timeout monitoring.
111 * For example, dbus_connection_setup_with_g_main().
113 * If you aren't using these add-on libraries, you have to manually
114 * call dbus_connection_set_dispatch_status_function(),
115 * dbus_connection_set_watch_functions(),
116 * dbus_connection_set_timeout_functions() providing appropriate
117 * functions to integrate the connection with your application's main
120 * When you use dbus_connection_send() or one of its variants to send
121 * a message, the message is added to the outgoing queue. It's
122 * actually written to the network later; either in
123 * dbus_watch_handle() invoked by your main loop, or in
124 * dbus_connection_flush() which blocks until it can write out the
125 * entire outgoing queue. The GLib/Qt add-on libraries again
126 * handle the details here for you by setting up watch functions.
128 * When a connection is disconnected, you are guaranteed to get a
129 * signal "Disconnected" from the interface
130 * #DBUS_INTERFACE_LOCAL, path
133 * You may not drop the last reference to a #DBusConnection
134 * until that connection has been disconnected.
136 * You may dispatch the unprocessed incoming message queue even if the
137 * connection is disconnected. However, "Disconnected" will always be
138 * the last message in the queue (obviously no messages are received
139 * after disconnection).
141 * #DBusConnection has thread locks and drops them when invoking user
142 * callbacks, so in general is transparently threadsafe. However,
143 * #DBusMessage does NOT have thread locks; you must not send the same
144 * message to multiple #DBusConnection that will be used from
149 * @defgroup DBusConnectionInternals DBusConnection implementation details
150 * @ingroup DBusInternals
151 * @brief Implementation details of DBusConnection
157 * Internal struct representing a message filter function
159 typedef struct DBusMessageFilter DBusMessageFilter;
162 * Internal struct representing a message filter function
164 struct DBusMessageFilter
166 DBusAtomic refcount; /**< Reference count */
167 DBusHandleMessageFunction function; /**< Function to call to filter */
168 void *user_data; /**< User data for the function */
169 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
174 * Internals of DBusPreallocatedSend
176 struct DBusPreallocatedSend
178 DBusConnection *connection; /**< Connection we'd send the message to */
179 DBusList *queue_link; /**< Preallocated link in the queue */
180 DBusList *counter_link; /**< Preallocated link in the resource counter */
183 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
186 * Implementation details of DBusConnection. All fields are private.
188 struct DBusConnection
190 DBusAtomic refcount; /**< Reference count. */
192 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
194 DBusMutex *dispatch_mutex; /**< Protects dispatch_acquired */
195 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */
196 DBusMutex *io_path_mutex; /**< Protects io_path_acquired */
197 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */
199 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
200 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
202 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
203 * dispatch_acquired will be set by the borrower
206 int n_outgoing; /**< Length of outgoing queue. */
207 int n_incoming; /**< Length of incoming queue. */
209 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
211 DBusTransport *transport; /**< Object that sends/receives messages over network. */
212 DBusWatchList *watches; /**< Stores active watches. */
213 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
215 DBusList *filter_list; /**< List of filters. */
217 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
219 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
221 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
222 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
224 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
225 void *wakeup_main_data; /**< Application data for wakeup_main_function */
226 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
228 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
229 void *dispatch_status_data; /**< Application data for dispatch_status_function */
230 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
232 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
234 DBusList *link_cache; /**< A cache of linked list links to prevent contention
235 * for the global linked list mempool lock
237 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
239 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
241 unsigned int shareable : 1; /**< #TRUE if connection can go in shared_connections once we know the GUID */
243 unsigned int dispatch_acquired : 1; /**< Someone has dispatch path (can drain incoming queue) */
244 unsigned int io_path_acquired : 1; /**< Someone has transport io path (can use the transport to read/write messages) */
246 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
248 #ifndef DBUS_DISABLE_CHECKS
249 unsigned int have_connection_lock : 1; /**< Used to check locking */
252 #ifndef DBUS_DISABLE_CHECKS
253 int generation; /**< _dbus_current_generation that should correspond to this connection */
257 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
258 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
259 DBusDispatchStatus new_status);
260 static void _dbus_connection_last_unref (DBusConnection *connection);
261 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
262 static void _dbus_connection_release_dispatch (DBusConnection *connection);
264 static DBusMessageFilter *
265 _dbus_message_filter_ref (DBusMessageFilter *filter)
267 _dbus_assert (filter->refcount.value > 0);
268 _dbus_atomic_inc (&filter->refcount);
274 _dbus_message_filter_unref (DBusMessageFilter *filter)
276 _dbus_assert (filter->refcount.value > 0);
278 if (_dbus_atomic_dec (&filter->refcount) == 1)
280 if (filter->free_user_data_function)
281 (* filter->free_user_data_function) (filter->user_data);
288 * Acquires the connection lock.
290 * @param connection the connection.
293 _dbus_connection_lock (DBusConnection *connection)
295 CONNECTION_LOCK (connection);
299 * Releases the connection lock.
301 * @param connection the connection.
304 _dbus_connection_unlock (DBusConnection *connection)
306 CONNECTION_UNLOCK (connection);
310 * Wakes up the main loop if it is sleeping
311 * Needed if we're e.g. queueing outgoing messages
312 * on a thread while the mainloop sleeps.
314 * @param connection the connection.
317 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
319 if (connection->wakeup_main_function)
320 (*connection->wakeup_main_function) (connection->wakeup_main_data);
323 #ifdef DBUS_BUILD_TESTS
324 /* For now this function isn't used */
326 * Adds a message to the incoming message queue, returning #FALSE
327 * if there's insufficient memory to queue the message.
328 * Does not take over refcount of the message.
330 * @param connection the connection.
331 * @param message the message to queue.
332 * @returns #TRUE on success.
335 _dbus_connection_queue_received_message (DBusConnection *connection,
336 DBusMessage *message)
340 link = _dbus_list_alloc_link (message);
344 dbus_message_ref (message);
345 _dbus_connection_queue_received_message_link (connection, link);
352 * Adds a message-containing list link to the incoming message queue,
353 * taking ownership of the link and the message's current refcount.
354 * Cannot fail due to lack of memory.
356 * @param connection the connection.
357 * @param link the message link to queue.
360 _dbus_connection_queue_received_message_link (DBusConnection *connection,
363 DBusPendingCall *pending;
364 dbus_int32_t reply_serial;
365 DBusMessage *message;
367 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
369 _dbus_list_append_link (&connection->incoming_messages,
371 message = link->data;
373 /* If this is a reply we're waiting on, remove timeout for it */
374 reply_serial = dbus_message_get_reply_serial (message);
375 if (reply_serial != -1)
377 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
381 if (_dbus_pending_call_is_timeout_added (pending))
382 _dbus_connection_remove_timeout_unlocked (connection,
383 _dbus_pending_call_get_timeout (pending));
385 _dbus_pending_call_set_timeout_added (pending, FALSE);
389 connection->n_incoming += 1;
391 _dbus_connection_wakeup_mainloop (connection);
393 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
395 dbus_message_get_type (message),
396 dbus_message_get_path (message) ?
397 dbus_message_get_path (message) :
399 dbus_message_get_interface (message) ?
400 dbus_message_get_interface (message) :
402 dbus_message_get_member (message) ?
403 dbus_message_get_member (message) :
405 dbus_message_get_signature (message),
406 dbus_message_get_reply_serial (message),
408 connection->n_incoming);}
411 * Adds a link + message to the incoming message queue.
412 * Can't fail. Takes ownership of both link and message.
414 * @param connection the connection.
415 * @param link the list node and message to queue.
417 * @todo This needs to wake up the mainloop if it is in
418 * a poll/select and this is a multithreaded app.
421 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
424 HAVE_LOCK_CHECK (connection);
426 _dbus_list_append_link (&connection->incoming_messages, link);
428 connection->n_incoming += 1;
430 _dbus_connection_wakeup_mainloop (connection);
432 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
433 link->data, connection, connection->n_incoming);
438 * Checks whether there are messages in the outgoing message queue.
439 * Called with connection lock held.
441 * @param connection the connection.
442 * @returns #TRUE if the outgoing queue is non-empty.
445 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
447 HAVE_LOCK_CHECK (connection);
448 return connection->outgoing_messages != NULL;
452 * Checks whether there are messages in the outgoing message queue.
454 * @param connection the connection.
455 * @returns #TRUE if the outgoing queue is non-empty.
458 dbus_connection_has_messages_to_send (DBusConnection *connection)
462 _dbus_return_val_if_fail (connection != NULL, FALSE);
464 CONNECTION_LOCK (connection);
465 v = _dbus_connection_has_messages_to_send_unlocked (connection);
466 CONNECTION_UNLOCK (connection);
472 * Gets the next outgoing message. The message remains in the
473 * queue, and the caller does not own a reference to it.
475 * @param connection the connection.
476 * @returns the message to be sent.
479 _dbus_connection_get_message_to_send (DBusConnection *connection)
481 HAVE_LOCK_CHECK (connection);
483 return _dbus_list_get_last (&connection->outgoing_messages);
487 * Notifies the connection that a message has been sent, so the
488 * message can be removed from the outgoing queue.
489 * Called with the connection lock held.
491 * @param connection the connection.
492 * @param message the message that was sent.
495 _dbus_connection_message_sent (DBusConnection *connection,
496 DBusMessage *message)
500 HAVE_LOCK_CHECK (connection);
502 /* This can be called before we even complete authentication, since
503 * it's called on disconnect to clean up the outgoing queue.
504 * It's also called as we successfully send each message.
507 link = _dbus_list_get_last_link (&connection->outgoing_messages);
508 _dbus_assert (link != NULL);
509 _dbus_assert (link->data == message);
511 /* Save this link in the link cache */
512 _dbus_list_unlink (&connection->outgoing_messages,
514 _dbus_list_prepend_link (&connection->link_cache, link);
516 connection->n_outgoing -= 1;
518 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
520 dbus_message_get_type (message),
521 dbus_message_get_path (message) ?
522 dbus_message_get_path (message) :
524 dbus_message_get_interface (message) ?
525 dbus_message_get_interface (message) :
527 dbus_message_get_member (message) ?
528 dbus_message_get_member (message) :
530 dbus_message_get_signature (message),
531 connection, connection->n_outgoing);
533 /* Save this link in the link cache also */
534 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
536 _dbus_list_prepend_link (&connection->link_cache, link);
538 dbus_message_unref (message);
541 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
543 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
545 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
547 dbus_bool_t enabled);
550 protected_change_watch (DBusConnection *connection,
552 DBusWatchAddFunction add_function,
553 DBusWatchRemoveFunction remove_function,
554 DBusWatchToggleFunction toggle_function,
557 DBusWatchList *watches;
560 HAVE_LOCK_CHECK (connection);
562 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
563 * drop lock and call out" one; but it has to be propagated up through all callers
566 watches = connection->watches;
569 connection->watches = NULL;
570 _dbus_connection_ref_unlocked (connection);
571 CONNECTION_UNLOCK (connection);
574 retval = (* add_function) (watches, watch);
575 else if (remove_function)
578 (* remove_function) (watches, watch);
583 (* toggle_function) (watches, watch, enabled);
586 CONNECTION_LOCK (connection);
587 connection->watches = watches;
588 _dbus_connection_unref_unlocked (connection);
598 * Adds a watch using the connection's DBusAddWatchFunction if
599 * available. Otherwise records the watch to be added when said
600 * function is available. Also re-adds the watch if the
601 * DBusAddWatchFunction changes. May fail due to lack of memory.
602 * Connection lock should be held when calling this.
604 * @param connection the connection.
605 * @param watch the watch to add.
606 * @returns #TRUE on success.
609 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
612 return protected_change_watch (connection, watch,
613 _dbus_watch_list_add_watch,
618 * Removes a watch using the connection's DBusRemoveWatchFunction
619 * if available. It's an error to call this function on a watch
620 * that was not previously added.
621 * Connection lock should be held when calling this.
623 * @param connection the connection.
624 * @param watch the watch to remove.
627 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
630 protected_change_watch (connection, watch,
632 _dbus_watch_list_remove_watch,
637 * Toggles a watch and notifies app via connection's
638 * DBusWatchToggledFunction if available. It's an error to call this
639 * function on a watch that was not previously added.
640 * Connection lock should be held when calling this.
642 * @param connection the connection.
643 * @param watch the watch to toggle.
644 * @param enabled whether to enable or disable
647 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
651 _dbus_assert (watch != NULL);
653 protected_change_watch (connection, watch,
655 _dbus_watch_list_toggle_watch,
659 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
660 DBusTimeout *timeout);
661 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
662 DBusTimeout *timeout);
663 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
664 DBusTimeout *timeout,
665 dbus_bool_t enabled);
668 protected_change_timeout (DBusConnection *connection,
669 DBusTimeout *timeout,
670 DBusTimeoutAddFunction add_function,
671 DBusTimeoutRemoveFunction remove_function,
672 DBusTimeoutToggleFunction toggle_function,
675 DBusTimeoutList *timeouts;
678 HAVE_LOCK_CHECK (connection);
680 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
681 * drop lock and call out" one; but it has to be propagated up through all callers
684 timeouts = connection->timeouts;
687 connection->timeouts = NULL;
688 _dbus_connection_ref_unlocked (connection);
689 CONNECTION_UNLOCK (connection);
692 retval = (* add_function) (timeouts, timeout);
693 else if (remove_function)
696 (* remove_function) (timeouts, timeout);
701 (* toggle_function) (timeouts, timeout, enabled);
704 CONNECTION_LOCK (connection);
705 connection->timeouts = timeouts;
706 _dbus_connection_unref_unlocked (connection);
715 * Adds a timeout using the connection's DBusAddTimeoutFunction if
716 * available. Otherwise records the timeout to be added when said
717 * function is available. Also re-adds the timeout if the
718 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
719 * The timeout will fire repeatedly until removed.
720 * Connection lock should be held when calling this.
722 * @param connection the connection.
723 * @param timeout the timeout to add.
724 * @returns #TRUE on success.
727 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
728 DBusTimeout *timeout)
730 return protected_change_timeout (connection, timeout,
731 _dbus_timeout_list_add_timeout,
736 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
737 * if available. It's an error to call this function on a timeout
738 * that was not previously added.
739 * Connection lock should be held when calling this.
741 * @param connection the connection.
742 * @param timeout the timeout to remove.
745 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
746 DBusTimeout *timeout)
748 protected_change_timeout (connection, timeout,
750 _dbus_timeout_list_remove_timeout,
755 * Toggles a timeout and notifies app via connection's
756 * DBusTimeoutToggledFunction if available. It's an error to call this
757 * function on a timeout that was not previously added.
758 * Connection lock should be held when calling this.
760 * @param connection the connection.
761 * @param timeout the timeout to toggle.
762 * @param enabled whether to enable or disable
765 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
766 DBusTimeout *timeout,
769 protected_change_timeout (connection, timeout,
771 _dbus_timeout_list_toggle_timeout,
776 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
777 DBusPendingCall *pending)
779 dbus_uint32_t reply_serial;
780 DBusTimeout *timeout;
782 HAVE_LOCK_CHECK (connection);
784 reply_serial = _dbus_pending_call_get_reply_serial (pending);
786 _dbus_assert (reply_serial != 0);
788 timeout = _dbus_pending_call_get_timeout (pending);
790 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
793 if (!_dbus_hash_table_insert_int (connection->pending_replies,
797 _dbus_connection_remove_timeout_unlocked (connection, timeout);
799 HAVE_LOCK_CHECK (connection);
803 _dbus_pending_call_set_timeout_added (pending, TRUE);
805 dbus_pending_call_ref (pending);
807 HAVE_LOCK_CHECK (connection);
813 free_pending_call_on_hash_removal (void *data)
815 DBusPendingCall *pending;
816 DBusConnection *connection;
823 connection = _dbus_pending_call_get_connection (pending);
827 if (_dbus_pending_call_is_timeout_added (pending))
829 _dbus_connection_remove_timeout_unlocked (connection,
830 _dbus_pending_call_get_timeout (pending));
832 _dbus_pending_call_set_timeout_added (pending, FALSE);
835 _dbus_pending_call_clear_connection (pending);
836 dbus_pending_call_unref (pending);
841 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
842 DBusPendingCall *pending)
844 /* Can't have a destroy notifier on the pending call if we're going to do this */
846 dbus_pending_call_ref (pending);
847 _dbus_hash_table_remove_int (connection->pending_replies,
848 _dbus_pending_call_get_reply_serial (pending));
849 dbus_pending_call_unref (pending);
853 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
854 DBusPendingCall *pending)
856 /* The idea here is to avoid finalizing the pending call
857 * with the lock held, since there's a destroy notifier
858 * in pending call that goes out to application code.
860 dbus_pending_call_ref (pending);
861 _dbus_hash_table_remove_int (connection->pending_replies,
862 _dbus_pending_call_get_reply_serial (pending));
863 CONNECTION_UNLOCK (connection);
864 dbus_pending_call_unref (pending);
868 * Removes a pending call from the connection, such that
869 * the pending reply will be ignored. May drop the last
870 * reference to the pending call.
872 * @param connection the connection
873 * @param pending the pending call
876 _dbus_connection_remove_pending_call (DBusConnection *connection,
877 DBusPendingCall *pending)
879 CONNECTION_LOCK (connection);
880 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
884 * Acquire the transporter I/O path. This must be done before
885 * doing any I/O in the transporter. May sleep and drop the
886 * IO path mutex while waiting for the I/O path.
888 * @param connection the connection.
889 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
890 * @returns TRUE if the I/O path was acquired.
893 _dbus_connection_acquire_io_path (DBusConnection *connection,
894 int timeout_milliseconds)
896 dbus_bool_t we_acquired;
898 HAVE_LOCK_CHECK (connection);
900 /* We don't want the connection to vanish */
901 _dbus_connection_ref_unlocked (connection);
903 /* We will only touch io_path_acquired which is protected by our mutex */
904 CONNECTION_UNLOCK (connection);
906 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
907 _dbus_mutex_lock (connection->io_path_mutex);
909 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
910 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
914 if (connection->io_path_acquired)
916 if (timeout_milliseconds != -1)
918 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
919 _DBUS_FUNCTION_NAME, timeout_milliseconds);
920 _dbus_condvar_wait_timeout (connection->io_path_cond,
921 connection->io_path_mutex,
922 timeout_milliseconds);
926 while (connection->io_path_acquired)
928 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
929 _dbus_condvar_wait (connection->io_path_cond, connection->io_path_mutex);
934 if (!connection->io_path_acquired)
937 connection->io_path_acquired = TRUE;
940 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
941 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
943 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
944 _dbus_mutex_unlock (connection->io_path_mutex);
946 CONNECTION_LOCK (connection);
948 HAVE_LOCK_CHECK (connection);
950 _dbus_connection_unref_unlocked (connection);
956 * Release the I/O path when you're done with it. Only call
957 * after you've acquired the I/O. Wakes up at most one thread
958 * currently waiting to acquire the I/O path.
960 * @param connection the connection.
963 _dbus_connection_release_io_path (DBusConnection *connection)
965 HAVE_LOCK_CHECK (connection);
967 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
968 _dbus_mutex_lock (connection->io_path_mutex);
970 _dbus_assert (connection->io_path_acquired);
972 _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
973 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
975 connection->io_path_acquired = FALSE;
976 _dbus_condvar_wake_one (connection->io_path_cond);
978 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
979 _dbus_mutex_unlock (connection->io_path_mutex);
983 * Queues incoming messages and sends outgoing messages for this
984 * connection, optionally blocking in the process. Each call to
985 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one
986 * time and then read or write data if possible.
988 * The purpose of this function is to be able to flush outgoing
989 * messages or queue up incoming messages without returning
990 * control to the application and causing reentrancy weirdness.
992 * The flags parameter allows you to specify whether to
993 * read incoming messages, write outgoing messages, or both,
994 * and whether to block if no immediate action is possible.
996 * The timeout_milliseconds parameter does nothing unless the
997 * iteration is blocking.
999 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
1000 * wasn't specified, then it's impossible to block, even if
1001 * you specify DBUS_ITERATION_BLOCK; in that case the function
1002 * returns immediately.
1004 * Called with connection lock held.
1006 * @param connection the connection.
1007 * @param flags iteration flags.
1008 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1011 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1013 int timeout_milliseconds)
1015 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1017 HAVE_LOCK_CHECK (connection);
1019 if (connection->n_outgoing == 0)
1020 flags &= ~DBUS_ITERATION_DO_WRITING;
1022 if (_dbus_connection_acquire_io_path (connection,
1023 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1025 HAVE_LOCK_CHECK (connection);
1027 _dbus_transport_do_iteration (connection->transport,
1028 flags, timeout_milliseconds);
1029 _dbus_connection_release_io_path (connection);
1032 HAVE_LOCK_CHECK (connection);
1034 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1038 * Creates a new connection for the given transport. A transport
1039 * represents a message stream that uses some concrete mechanism, such
1040 * as UNIX domain sockets. May return #NULL if insufficient
1041 * memory exists to create the connection.
1043 * @param transport the transport.
1044 * @returns the new connection, or #NULL on failure.
1047 _dbus_connection_new_for_transport (DBusTransport *transport)
1049 DBusConnection *connection;
1050 DBusWatchList *watch_list;
1051 DBusTimeoutList *timeout_list;
1052 DBusHashTable *pending_replies;
1054 DBusMutex *io_path_mutex;
1055 DBusMutex *dispatch_mutex;
1056 DBusCondVar *dispatch_cond;
1057 DBusCondVar *io_path_cond;
1058 DBusList *disconnect_link;
1059 DBusMessage *disconnect_message;
1060 DBusCounter *outgoing_counter;
1061 DBusObjectTree *objects;
1065 pending_replies = NULL;
1066 timeout_list = NULL;
1068 io_path_mutex = NULL;
1069 dispatch_mutex = NULL;
1070 dispatch_cond = NULL;
1071 io_path_cond = NULL;
1072 disconnect_link = NULL;
1073 disconnect_message = NULL;
1074 outgoing_counter = NULL;
1077 watch_list = _dbus_watch_list_new ();
1078 if (watch_list == NULL)
1081 timeout_list = _dbus_timeout_list_new ();
1082 if (timeout_list == NULL)
1086 _dbus_hash_table_new (DBUS_HASH_INT,
1088 (DBusFreeFunction)free_pending_call_on_hash_removal);
1089 if (pending_replies == NULL)
1092 connection = dbus_new0 (DBusConnection, 1);
1093 if (connection == NULL)
1096 mutex = _dbus_mutex_new ();
1100 io_path_mutex = _dbus_mutex_new ();
1101 if (io_path_mutex == NULL)
1104 dispatch_mutex = _dbus_mutex_new ();
1105 if (dispatch_mutex == NULL)
1108 dispatch_cond = _dbus_condvar_new ();
1109 if (dispatch_cond == NULL)
1112 io_path_cond = _dbus_condvar_new ();
1113 if (io_path_cond == NULL)
1116 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1117 DBUS_INTERFACE_LOCAL,
1120 if (disconnect_message == NULL)
1123 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1124 if (disconnect_link == NULL)
1127 outgoing_counter = _dbus_counter_new ();
1128 if (outgoing_counter == NULL)
1131 objects = _dbus_object_tree_new (connection);
1132 if (objects == NULL)
1135 if (_dbus_modify_sigpipe)
1136 _dbus_disable_sigpipe ();
1138 connection->refcount.value = 1;
1139 connection->mutex = mutex;
1140 connection->dispatch_cond = dispatch_cond;
1141 connection->dispatch_mutex = dispatch_mutex;
1142 connection->io_path_cond = io_path_cond;
1143 connection->io_path_mutex = io_path_mutex;
1144 connection->transport = transport;
1145 connection->watches = watch_list;
1146 connection->timeouts = timeout_list;
1147 connection->pending_replies = pending_replies;
1148 connection->outgoing_counter = outgoing_counter;
1149 connection->filter_list = NULL;
1150 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1151 connection->objects = objects;
1152 connection->exit_on_disconnect = FALSE;
1153 connection->shareable = FALSE;
1154 #ifndef DBUS_DISABLE_CHECKS
1155 connection->generation = _dbus_current_generation;
1158 _dbus_data_slot_list_init (&connection->slot_list);
1160 connection->client_serial = 1;
1162 connection->disconnect_message_link = disconnect_link;
1164 CONNECTION_LOCK (connection);
1166 if (!_dbus_transport_set_connection (transport, connection))
1169 _dbus_transport_ref (transport);
1171 CONNECTION_UNLOCK (connection);
1176 if (disconnect_message != NULL)
1177 dbus_message_unref (disconnect_message);
1179 if (disconnect_link != NULL)
1180 _dbus_list_free_link (disconnect_link);
1182 if (io_path_cond != NULL)
1183 _dbus_condvar_free (io_path_cond);
1185 if (dispatch_cond != NULL)
1186 _dbus_condvar_free (dispatch_cond);
1189 _dbus_mutex_free (mutex);
1191 if (io_path_mutex != NULL)
1192 _dbus_mutex_free (io_path_mutex);
1194 if (dispatch_mutex != NULL)
1195 _dbus_mutex_free (dispatch_mutex);
1197 if (connection != NULL)
1198 dbus_free (connection);
1200 if (pending_replies)
1201 _dbus_hash_table_unref (pending_replies);
1204 _dbus_watch_list_free (watch_list);
1207 _dbus_timeout_list_free (timeout_list);
1209 if (outgoing_counter)
1210 _dbus_counter_unref (outgoing_counter);
1213 _dbus_object_tree_unref (objects);
1219 * Increments the reference count of a DBusConnection.
1220 * Requires that the caller already holds the connection lock.
1222 * @param connection the connection.
1223 * @returns the connection.
1226 _dbus_connection_ref_unlocked (DBusConnection *connection)
1228 _dbus_assert (connection != NULL);
1229 _dbus_assert (connection->generation == _dbus_current_generation);
1231 HAVE_LOCK_CHECK (connection);
1233 #ifdef DBUS_HAVE_ATOMIC_INT
1234 _dbus_atomic_inc (&connection->refcount);
1236 _dbus_assert (connection->refcount.value > 0);
1237 connection->refcount.value += 1;
1244 * Decrements the reference count of a DBusConnection.
1245 * Requires that the caller already holds the connection lock.
1247 * @param connection the connection.
1250 _dbus_connection_unref_unlocked (DBusConnection *connection)
1252 dbus_bool_t last_unref;
1254 HAVE_LOCK_CHECK (connection);
1256 _dbus_assert (connection != NULL);
1258 /* The connection lock is better than the global
1259 * lock in the atomic increment fallback
1262 #ifdef DBUS_HAVE_ATOMIC_INT
1263 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1265 _dbus_assert (connection->refcount.value > 0);
1267 connection->refcount.value -= 1;
1268 last_unref = (connection->refcount.value == 0);
1270 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1275 _dbus_connection_last_unref (connection);
1278 static dbus_uint32_t
1279 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1283 serial = connection->client_serial++;
1285 if (connection->client_serial < 0)
1286 connection->client_serial = 1;
1292 * A callback for use with dbus_watch_new() to create a DBusWatch.
1294 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1295 * and the virtual handle_watch in DBusTransport if we got rid of it.
1296 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1299 * @param watch the watch.
1300 * @param condition the current condition of the file descriptors being watched.
1301 * @param data must be a pointer to a #DBusConnection
1302 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1305 _dbus_connection_handle_watch (DBusWatch *watch,
1306 unsigned int condition,
1309 DBusConnection *connection;
1311 DBusDispatchStatus status;
1315 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1317 CONNECTION_LOCK (connection);
1318 _dbus_connection_acquire_io_path (connection, -1);
1319 HAVE_LOCK_CHECK (connection);
1320 retval = _dbus_transport_handle_watch (connection->transport,
1323 _dbus_connection_release_io_path (connection);
1325 HAVE_LOCK_CHECK (connection);
1327 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1329 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1331 /* this calls out to user code */
1332 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1334 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1339 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
1340 static DBusHashTable *shared_connections = NULL;
1343 shared_connections_shutdown (void *data)
1345 _DBUS_LOCK (shared_connections);
1347 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1348 _dbus_hash_table_unref (shared_connections);
1349 shared_connections = NULL;
1351 _DBUS_UNLOCK (shared_connections);
1355 connection_lookup_shared (DBusAddressEntry *entry,
1356 DBusConnection **result)
1358 _dbus_verbose ("checking for existing connection\n");
1362 _DBUS_LOCK (shared_connections);
1364 if (shared_connections == NULL)
1366 _dbus_verbose ("creating shared_connections hash table\n");
1368 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1371 if (shared_connections == NULL)
1373 _DBUS_UNLOCK (shared_connections);
1377 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1379 _dbus_hash_table_unref (shared_connections);
1380 shared_connections = NULL;
1381 _DBUS_UNLOCK (shared_connections);
1385 _dbus_verbose (" successfully created shared_connections\n");
1387 _DBUS_UNLOCK (shared_connections);
1388 return TRUE; /* no point looking up in the hash we just made */
1394 guid = dbus_address_entry_get_value (entry, "guid");
1398 *result = _dbus_hash_table_lookup_string (shared_connections,
1403 /* The DBusConnection can't have been disconnected
1404 * between the lookup and this code, because the
1405 * disconnection will take the shared_connections lock to
1406 * remove the connection. It can't have been finalized
1407 * since you have to disconnect prior to finalize.
1409 * Thus it's safe to ref the connection.
1411 dbus_connection_ref (*result);
1413 _dbus_verbose ("looked up existing connection to server guid %s\n",
1418 _DBUS_UNLOCK (shared_connections);
1424 connection_record_shared_unlocked (DBusConnection *connection,
1428 char *guid_in_connection;
1430 /* A separate copy of the key is required in the hash table, because
1431 * we don't have a lock on the connection when we are doing a hash
1435 _dbus_assert (connection->server_guid == NULL);
1436 _dbus_assert (connection->shareable);
1438 guid_key = _dbus_strdup (guid);
1439 if (guid_key == NULL)
1442 guid_in_connection = _dbus_strdup (guid);
1443 if (guid_in_connection == NULL)
1445 dbus_free (guid_key);
1449 _DBUS_LOCK (shared_connections);
1450 _dbus_assert (shared_connections != NULL);
1452 if (!_dbus_hash_table_insert_string (shared_connections,
1453 guid_key, connection))
1455 dbus_free (guid_key);
1456 dbus_free (guid_in_connection);
1457 _DBUS_UNLOCK (shared_connections);
1461 connection->server_guid = guid_in_connection;
1463 _dbus_verbose ("stored connection to %s to be shared\n",
1464 connection->server_guid);
1466 _DBUS_UNLOCK (shared_connections);
1468 _dbus_assert (connection->server_guid != NULL);
1474 connection_forget_shared_unlocked (DBusConnection *connection)
1476 HAVE_LOCK_CHECK (connection);
1478 if (connection->server_guid == NULL)
1481 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1482 connection->server_guid);
1484 _DBUS_LOCK (shared_connections);
1486 if (!_dbus_hash_table_remove_string (shared_connections,
1487 connection->server_guid))
1488 _dbus_assert_not_reached ("connection was not in the shared table");
1490 dbus_free (connection->server_guid);
1491 connection->server_guid = NULL;
1493 _DBUS_UNLOCK (shared_connections);
1496 static DBusConnection*
1497 connection_try_from_address_entry (DBusAddressEntry *entry,
1500 DBusTransport *transport;
1501 DBusConnection *connection;
1503 transport = _dbus_transport_open (entry, error);
1505 if (transport == NULL)
1507 _DBUS_ASSERT_ERROR_IS_SET (error);
1511 connection = _dbus_connection_new_for_transport (transport);
1513 _dbus_transport_unref (transport);
1515 if (connection == NULL)
1517 _DBUS_SET_OOM (error);
1521 #ifndef DBUS_DISABLE_CHECKS
1522 _dbus_assert (!connection->have_connection_lock);
1528 * If the shared parameter is true, then any existing connection will
1529 * be used (and if a new connection is created, it will be available
1530 * for use by others). If the shared parameter is false, a new
1531 * connection will always be created, and the new connection will
1532 * never be returned to other callers.
1534 * @param address the address
1535 * @param shared whether the connection is shared or private
1536 * @param error error return
1537 * @returns the connection or #NULL on error
1539 static DBusConnection*
1540 _dbus_connection_open_internal (const char *address,
1544 DBusConnection *connection;
1545 DBusAddressEntry **entries;
1546 DBusError tmp_error;
1547 DBusError first_error;
1550 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1552 _dbus_verbose ("opening %s connection to: %s\n",
1553 shared ? "shared" : "private", address);
1555 if (!dbus_parse_address (address, &entries, &len, error))
1558 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1562 dbus_error_init (&tmp_error);
1563 dbus_error_init (&first_error);
1564 for (i = 0; i < len; i++)
1568 if (!connection_lookup_shared (entries[i], &connection))
1569 _DBUS_SET_OOM (&tmp_error);
1572 if (connection == NULL)
1574 connection = connection_try_from_address_entry (entries[i],
1577 if (connection != NULL && shared)
1581 connection->shareable = TRUE;
1583 guid = dbus_address_entry_get_value (entries[i], "guid");
1585 /* we don't have a connection lock but we know nobody
1586 * else has a handle to the connection
1590 !connection_record_shared_unlocked (connection, guid))
1592 _DBUS_SET_OOM (&tmp_error);
1593 dbus_connection_close (connection);
1594 dbus_connection_unref (connection);
1598 /* but as of now the connection is possibly shared
1599 * since another thread could have pulled it from the table
1607 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1610 dbus_move_error (&tmp_error, &first_error);
1612 dbus_error_free (&tmp_error);
1615 /* NOTE we don't have a lock on a possibly-shared connection object */
1617 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1618 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1620 if (connection == NULL)
1622 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1623 dbus_move_error (&first_error, error);
1627 dbus_error_free (&first_error);
1630 dbus_address_entries_free (entries);
1637 * @addtogroup DBusConnection
1643 * Gets a connection to a remote address. If a connection to the given
1644 * address already exists, returns the existing connection with its
1645 * reference count incremented. Otherwise, returns a new connection
1646 * and saves the new connection for possible re-use if a future call
1647 * to dbus_connection_open() asks to connect to the same server.
1649 * Use dbus_connection_open_private() to get a dedicated connection
1650 * not shared with other callers of dbus_connection_open().
1652 * If the open fails, the function returns #NULL, and provides a
1653 * reason for the failure in the error parameter. Pass #NULL for the
1654 * error parameter if you aren't interested in the reason for
1657 * @param address the address.
1658 * @param error address where an error can be returned.
1659 * @returns new connection, or #NULL on failure.
1662 dbus_connection_open (const char *address,
1665 DBusConnection *connection;
1667 _dbus_return_val_if_fail (address != NULL, NULL);
1668 _dbus_return_val_if_error_is_set (error, NULL);
1670 connection = _dbus_connection_open_internal (address,
1678 * Opens a new, dedicated connection to a remote address. Unlike
1679 * dbus_connection_open(), always creates a new connection.
1680 * This connection will not be saved or recycled by libdbus.
1682 * If the open fails, the function returns #NULL, and provides a
1683 * reason for the failure in the error parameter. Pass #NULL for the
1684 * error parameter if you aren't interested in the reason for
1687 * @param address the address.
1688 * @param error address where an error can be returned.
1689 * @returns new connection, or #NULL on failure.
1692 dbus_connection_open_private (const char *address,
1695 DBusConnection *connection;
1697 _dbus_return_val_if_fail (address != NULL, NULL);
1698 _dbus_return_val_if_error_is_set (error, NULL);
1700 connection = _dbus_connection_open_internal (address,
1708 * Increments the reference count of a DBusConnection.
1710 * @param connection the connection.
1711 * @returns the connection.
1714 dbus_connection_ref (DBusConnection *connection)
1716 _dbus_return_val_if_fail (connection != NULL, NULL);
1717 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
1719 /* The connection lock is better than the global
1720 * lock in the atomic increment fallback
1723 #ifdef DBUS_HAVE_ATOMIC_INT
1724 _dbus_atomic_inc (&connection->refcount);
1726 CONNECTION_LOCK (connection);
1727 _dbus_assert (connection->refcount.value > 0);
1729 connection->refcount.value += 1;
1730 CONNECTION_UNLOCK (connection);
1737 free_outgoing_message (void *element,
1740 DBusMessage *message = element;
1741 DBusConnection *connection = data;
1743 _dbus_message_remove_size_counter (message,
1744 connection->outgoing_counter,
1746 dbus_message_unref (message);
1749 /* This is run without the mutex held, but after the last reference
1750 * to the connection has been dropped we should have no thread-related
1754 _dbus_connection_last_unref (DBusConnection *connection)
1758 _dbus_verbose ("Finalizing connection %p\n", connection);
1760 _dbus_assert (connection->refcount.value == 0);
1762 /* You have to disconnect the connection before unref:ing it. Otherwise
1763 * you won't get the disconnected message.
1765 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1766 _dbus_assert (connection->server_guid == NULL);
1768 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1769 _dbus_object_tree_free_all_unlocked (connection->objects);
1771 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1772 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1773 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1775 _dbus_watch_list_free (connection->watches);
1776 connection->watches = NULL;
1778 _dbus_timeout_list_free (connection->timeouts);
1779 connection->timeouts = NULL;
1781 _dbus_data_slot_list_free (&connection->slot_list);
1783 link = _dbus_list_get_first_link (&connection->filter_list);
1784 while (link != NULL)
1786 DBusMessageFilter *filter = link->data;
1787 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1789 filter->function = NULL;
1790 _dbus_message_filter_unref (filter); /* calls app callback */
1795 _dbus_list_clear (&connection->filter_list);
1797 /* ---- Done with stuff that invokes application callbacks */
1799 _dbus_object_tree_unref (connection->objects);
1801 _dbus_hash_table_unref (connection->pending_replies);
1802 connection->pending_replies = NULL;
1804 _dbus_list_clear (&connection->filter_list);
1806 _dbus_list_foreach (&connection->outgoing_messages,
1807 free_outgoing_message,
1809 _dbus_list_clear (&connection->outgoing_messages);
1811 _dbus_list_foreach (&connection->incoming_messages,
1812 (DBusForeachFunction) dbus_message_unref,
1814 _dbus_list_clear (&connection->incoming_messages);
1816 _dbus_counter_unref (connection->outgoing_counter);
1818 _dbus_transport_unref (connection->transport);
1820 if (connection->disconnect_message_link)
1822 DBusMessage *message = connection->disconnect_message_link->data;
1823 dbus_message_unref (message);
1824 _dbus_list_free_link (connection->disconnect_message_link);
1827 _dbus_list_clear (&connection->link_cache);
1829 _dbus_condvar_free (connection->dispatch_cond);
1830 _dbus_condvar_free (connection->io_path_cond);
1832 _dbus_mutex_free (connection->io_path_mutex);
1833 _dbus_mutex_free (connection->dispatch_mutex);
1835 _dbus_mutex_free (connection->mutex);
1837 dbus_free (connection);
1841 * Decrements the reference count of a DBusConnection, and finalizes
1842 * it if the count reaches zero. It is a bug to drop the last reference
1843 * to a connection that has not been disconnected.
1845 * @todo in practice it can be quite tricky to never unref a connection
1846 * that's still connected; maybe there's some way we could avoid
1849 * @param connection the connection.
1852 dbus_connection_unref (DBusConnection *connection)
1854 dbus_bool_t last_unref;
1856 _dbus_return_if_fail (connection != NULL);
1857 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1859 /* The connection lock is better than the global
1860 * lock in the atomic increment fallback
1863 #ifdef DBUS_HAVE_ATOMIC_INT
1864 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1866 CONNECTION_LOCK (connection);
1868 _dbus_assert (connection->refcount.value > 0);
1870 connection->refcount.value -= 1;
1871 last_unref = (connection->refcount.value == 0);
1874 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1877 CONNECTION_UNLOCK (connection);
1881 _dbus_connection_last_unref (connection);
1885 * Closes the connection, so no further data can be sent or received.
1886 * Any further attempts to send data will result in errors. This
1887 * function does not affect the connection's reference count. It's
1888 * safe to disconnect a connection more than once; all calls after the
1889 * first do nothing. It's impossible to "reopen" a connection, a
1890 * new connection must be created. This function may result in a call
1891 * to the DBusDispatchStatusFunction set with
1892 * dbus_connection_set_dispatch_status_function(), as the disconnect
1893 * message it generates needs to be dispatched.
1895 * @param connection the connection.
1898 dbus_connection_close (DBusConnection *connection)
1900 DBusDispatchStatus status;
1902 _dbus_return_if_fail (connection != NULL);
1903 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1905 _dbus_verbose ("Disconnecting %p\n", connection);
1907 CONNECTION_LOCK (connection);
1909 _dbus_transport_disconnect (connection->transport);
1911 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1912 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1914 /* this calls out to user code */
1915 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1919 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1921 HAVE_LOCK_CHECK (connection);
1922 return _dbus_transport_get_is_connected (connection->transport);
1926 * Gets whether the connection is currently connected. All
1927 * connections are connected when they are opened. A connection may
1928 * become disconnected when the remote application closes its end, or
1929 * exits; a connection may also be disconnected with
1930 * dbus_connection_close().
1932 * @param connection the connection.
1933 * @returns #TRUE if the connection is still alive.
1936 dbus_connection_get_is_connected (DBusConnection *connection)
1940 _dbus_return_val_if_fail (connection != NULL, FALSE);
1942 CONNECTION_LOCK (connection);
1943 res = _dbus_connection_get_is_connected_unlocked (connection);
1944 CONNECTION_UNLOCK (connection);
1950 * Gets whether the connection was authenticated. (Note that
1951 * if the connection was authenticated then disconnected,
1952 * this function still returns #TRUE)
1954 * @param connection the connection
1955 * @returns #TRUE if the connection was ever authenticated
1958 dbus_connection_get_is_authenticated (DBusConnection *connection)
1962 _dbus_return_val_if_fail (connection != NULL, FALSE);
1964 CONNECTION_LOCK (connection);
1965 res = _dbus_transport_get_is_authenticated (connection->transport);
1966 CONNECTION_UNLOCK (connection);
1972 * Set whether _exit() should be called when the connection receives a
1973 * disconnect signal. The call to _exit() comes after any handlers for
1974 * the disconnect signal run; handlers can cancel the exit by calling
1977 * By default, exit_on_disconnect is #FALSE; but for message bus
1978 * connections returned from dbus_bus_get() it will be toggled on
1981 * @param connection the connection
1982 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
1985 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
1986 dbus_bool_t exit_on_disconnect)
1988 _dbus_return_if_fail (connection != NULL);
1990 CONNECTION_LOCK (connection);
1991 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
1992 CONNECTION_UNLOCK (connection);
1995 static DBusPreallocatedSend*
1996 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1998 DBusPreallocatedSend *preallocated;
2000 HAVE_LOCK_CHECK (connection);
2002 _dbus_assert (connection != NULL);
2004 preallocated = dbus_new (DBusPreallocatedSend, 1);
2005 if (preallocated == NULL)
2008 if (connection->link_cache != NULL)
2010 preallocated->queue_link =
2011 _dbus_list_pop_first_link (&connection->link_cache);
2012 preallocated->queue_link->data = NULL;
2016 preallocated->queue_link = _dbus_list_alloc_link (NULL);
2017 if (preallocated->queue_link == NULL)
2021 if (connection->link_cache != NULL)
2023 preallocated->counter_link =
2024 _dbus_list_pop_first_link (&connection->link_cache);
2025 preallocated->counter_link->data = connection->outgoing_counter;
2029 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
2030 if (preallocated->counter_link == NULL)
2034 _dbus_counter_ref (preallocated->counter_link->data);
2036 preallocated->connection = connection;
2038 return preallocated;
2041 _dbus_list_free_link (preallocated->queue_link);
2043 dbus_free (preallocated);
2049 * Preallocates resources needed to send a message, allowing the message
2050 * to be sent without the possibility of memory allocation failure.
2051 * Allows apps to create a future guarantee that they can send
2052 * a message regardless of memory shortages.
2054 * @param connection the connection we're preallocating for.
2055 * @returns the preallocated resources, or #NULL
2057 DBusPreallocatedSend*
2058 dbus_connection_preallocate_send (DBusConnection *connection)
2060 DBusPreallocatedSend *preallocated;
2062 _dbus_return_val_if_fail (connection != NULL, NULL);
2064 CONNECTION_LOCK (connection);
2067 _dbus_connection_preallocate_send_unlocked (connection);
2069 CONNECTION_UNLOCK (connection);
2071 return preallocated;
2075 * Frees preallocated message-sending resources from
2076 * dbus_connection_preallocate_send(). Should only
2077 * be called if the preallocated resources are not used
2078 * to send a message.
2080 * @param connection the connection
2081 * @param preallocated the resources
2084 dbus_connection_free_preallocated_send (DBusConnection *connection,
2085 DBusPreallocatedSend *preallocated)
2087 _dbus_return_if_fail (connection != NULL);
2088 _dbus_return_if_fail (preallocated != NULL);
2089 _dbus_return_if_fail (connection == preallocated->connection);
2091 _dbus_list_free_link (preallocated->queue_link);
2092 _dbus_counter_unref (preallocated->counter_link->data);
2093 _dbus_list_free_link (preallocated->counter_link);
2094 dbus_free (preallocated);
2097 /* Called with lock held, does not update dispatch status */
2099 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
2100 DBusPreallocatedSend *preallocated,
2101 DBusMessage *message,
2102 dbus_uint32_t *client_serial)
2104 dbus_uint32_t serial;
2107 preallocated->queue_link->data = message;
2108 _dbus_list_prepend_link (&connection->outgoing_messages,
2109 preallocated->queue_link);
2111 _dbus_message_add_size_counter_link (message,
2112 preallocated->counter_link);
2114 dbus_free (preallocated);
2115 preallocated = NULL;
2117 dbus_message_ref (message);
2119 connection->n_outgoing += 1;
2121 sig = dbus_message_get_signature (message);
2123 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
2125 dbus_message_get_type (message),
2126 dbus_message_get_path (message) ?
2127 dbus_message_get_path (message) :
2129 dbus_message_get_interface (message) ?
2130 dbus_message_get_interface (message) :
2132 dbus_message_get_member (message) ?
2133 dbus_message_get_member (message) :
2136 dbus_message_get_destination (message) ?
2137 dbus_message_get_destination (message) :
2140 connection->n_outgoing);
2142 if (dbus_message_get_serial (message) == 0)
2144 serial = _dbus_connection_get_next_client_serial (connection);
2145 _dbus_message_set_serial (message, serial);
2147 *client_serial = serial;
2152 *client_serial = dbus_message_get_serial (message);
2155 _dbus_verbose ("Message %p serial is %u\n",
2156 message, dbus_message_get_serial (message));
2158 _dbus_message_lock (message);
2160 /* Now we need to run an iteration to hopefully just write the messages
2161 * out immediately, and otherwise get them queued up
2163 _dbus_connection_do_iteration_unlocked (connection,
2164 DBUS_ITERATION_DO_WRITING,
2167 /* If stuff is still queued up, be sure we wake up the main loop */
2168 if (connection->n_outgoing > 0)
2169 _dbus_connection_wakeup_mainloop (connection);
2173 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
2174 DBusPreallocatedSend *preallocated,
2175 DBusMessage *message,
2176 dbus_uint32_t *client_serial)
2178 DBusDispatchStatus status;
2180 HAVE_LOCK_CHECK (connection);
2182 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2184 message, client_serial);
2186 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2187 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2189 /* this calls out to user code */
2190 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2194 * Sends a message using preallocated resources. This function cannot fail.
2195 * It works identically to dbus_connection_send() in other respects.
2196 * Preallocated resources comes from dbus_connection_preallocate_send().
2197 * This function "consumes" the preallocated resources, they need not
2198 * be freed separately.
2200 * @param connection the connection
2201 * @param preallocated the preallocated resources
2202 * @param message the message to send
2203 * @param client_serial return location for client serial assigned to the message
2206 dbus_connection_send_preallocated (DBusConnection *connection,
2207 DBusPreallocatedSend *preallocated,
2208 DBusMessage *message,
2209 dbus_uint32_t *client_serial)
2211 _dbus_return_if_fail (connection != NULL);
2212 _dbus_return_if_fail (preallocated != NULL);
2213 _dbus_return_if_fail (message != NULL);
2214 _dbus_return_if_fail (preallocated->connection == connection);
2215 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
2216 dbus_message_get_member (message) != NULL);
2217 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
2218 (dbus_message_get_interface (message) != NULL &&
2219 dbus_message_get_member (message) != NULL));
2221 CONNECTION_LOCK (connection);
2222 _dbus_connection_send_preallocated_and_unlock (connection,
2224 message, client_serial);
2228 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
2229 DBusMessage *message,
2230 dbus_uint32_t *client_serial)
2232 DBusPreallocatedSend *preallocated;
2234 _dbus_assert (connection != NULL);
2235 _dbus_assert (message != NULL);
2237 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2238 if (preallocated == NULL)
2241 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2249 _dbus_connection_send_and_unlock (DBusConnection *connection,
2250 DBusMessage *message,
2251 dbus_uint32_t *client_serial)
2253 DBusPreallocatedSend *preallocated;
2255 _dbus_assert (connection != NULL);
2256 _dbus_assert (message != NULL);
2258 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2259 if (preallocated == NULL)
2261 CONNECTION_UNLOCK (connection);
2265 _dbus_connection_send_preallocated_and_unlock (connection,
2273 * Adds a message to the outgoing message queue. Does not block to
2274 * write the message to the network; that happens asynchronously. To
2275 * force the message to be written, call dbus_connection_flush().
2276 * Because this only queues the message, the only reason it can
2277 * fail is lack of memory. Even if the connection is disconnected,
2278 * no error will be returned.
2280 * If the function fails due to lack of memory, it returns #FALSE.
2281 * The function will never fail for other reasons; even if the
2282 * connection is disconnected, you can queue an outgoing message,
2283 * though obviously it won't be sent.
2285 * @param connection the connection.
2286 * @param message the message to write.
2287 * @param client_serial return location for client serial.
2288 * @returns #TRUE on success.
2291 dbus_connection_send (DBusConnection *connection,
2292 DBusMessage *message,
2293 dbus_uint32_t *client_serial)
2295 _dbus_return_val_if_fail (connection != NULL, FALSE);
2296 _dbus_return_val_if_fail (message != NULL, FALSE);
2298 CONNECTION_LOCK (connection);
2300 return _dbus_connection_send_and_unlock (connection,
2306 reply_handler_timeout (void *data)
2308 DBusConnection *connection;
2309 DBusDispatchStatus status;
2310 DBusPendingCall *pending = data;
2312 connection = _dbus_pending_call_get_connection (pending);
2314 CONNECTION_LOCK (connection);
2315 _dbus_pending_call_queue_timeout_error (pending,
2317 _dbus_connection_remove_timeout_unlocked (connection,
2318 _dbus_pending_call_get_timeout (pending));
2319 _dbus_pending_call_set_timeout_added (pending, FALSE);
2321 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2322 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2324 /* Unlocks, and calls out to user code */
2325 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2331 * Queues a message to send, as with dbus_connection_send_message(),
2332 * but also returns a #DBusPendingCall used to receive a reply to the
2333 * message. If no reply is received in the given timeout_milliseconds,
2334 * this function expires the pending reply and generates a synthetic
2335 * error reply (generated in-process, not by the remote application)
2336 * indicating that a timeout occurred.
2338 * A #DBusPendingCall will see a reply message after any filters, but
2339 * before any object instances or other handlers. A #DBusPendingCall
2340 * will always see exactly one reply message, unless it's cancelled
2341 * with dbus_pending_call_cancel().
2343 * If a filter filters out the reply before the handler sees it, the
2344 * reply is immediately timed out and a timeout error reply is
2345 * generated. If a filter removes the timeout error reply then the
2346 * #DBusPendingCall will get confused. Filtering the timeout error
2347 * is thus considered a bug and will print a warning.
2349 * If #NULL is passed for the pending_return, the #DBusPendingCall
2350 * will still be generated internally, and used to track
2351 * the message reply timeout. This means a timeout error will
2352 * occur if no reply arrives, unlike with dbus_connection_send().
2354 * If -1 is passed for the timeout, a sane default timeout is used. -1
2355 * is typically the best value for the timeout for this reason, unless
2356 * you want a very short or very long timeout. There is no way to
2357 * avoid a timeout entirely, other than passing INT_MAX for the
2358 * timeout to postpone it indefinitely.
2360 * @param connection the connection
2361 * @param message the message to send
2362 * @param pending_return return location for a #DBusPendingCall object, or #NULL
2363 * @param timeout_milliseconds timeout in milliseconds or -1 for default
2364 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
2368 dbus_connection_send_with_reply (DBusConnection *connection,
2369 DBusMessage *message,
2370 DBusPendingCall **pending_return,
2371 int timeout_milliseconds)
2373 DBusPendingCall *pending;
2374 dbus_int32_t serial = -1;
2375 DBusDispatchStatus status;
2377 _dbus_return_val_if_fail (connection != NULL, FALSE);
2378 _dbus_return_val_if_fail (message != NULL, FALSE);
2379 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2382 *pending_return = NULL;
2384 pending = _dbus_pending_call_new (connection,
2385 timeout_milliseconds,
2386 reply_handler_timeout);
2388 if (pending == NULL)
2391 CONNECTION_LOCK (connection);
2393 /* Assign a serial to the message */
2394 serial = dbus_message_get_serial (message);
2397 serial = _dbus_connection_get_next_client_serial (connection);
2398 _dbus_message_set_serial (message, serial);
2401 if (!_dbus_pending_call_set_timeout_error (pending, message, serial))
2404 /* Insert the serial in the pending replies hash;
2405 * hash takes a refcount on DBusPendingCall.
2406 * Also, add the timeout.
2408 if (!_dbus_connection_attach_pending_call_unlocked (connection,
2412 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
2414 _dbus_connection_detach_pending_call_and_unlock (connection,
2416 goto error_unlocked;
2420 *pending_return = pending;
2423 _dbus_connection_detach_pending_call_unlocked (connection, pending);
2424 dbus_pending_call_unref (pending);
2427 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2428 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2430 /* this calls out to user code */
2431 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2436 CONNECTION_UNLOCK (connection);
2438 dbus_pending_call_unref (pending);
2442 /* This is slightly strange since we can pop a message here without
2443 * the dispatch lock.
2446 check_for_reply_unlocked (DBusConnection *connection,
2447 dbus_uint32_t client_serial)
2451 HAVE_LOCK_CHECK (connection);
2453 link = _dbus_list_get_first_link (&connection->incoming_messages);
2455 while (link != NULL)
2457 DBusMessage *reply = link->data;
2459 if (dbus_message_get_reply_serial (reply) == client_serial)
2461 _dbus_list_remove_link (&connection->incoming_messages, link);
2462 connection->n_incoming -= 1;
2465 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2472 complete_pending_call_and_unlock (DBusPendingCall *pending,
2473 DBusMessage *message)
2475 _dbus_pending_call_set_reply (pending, message);
2476 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
2477 _dbus_connection_detach_pending_call_and_unlock (_dbus_pending_call_get_connection (pending), pending);
2479 /* Must be called unlocked since it invokes app callback */
2480 _dbus_pending_call_complete (pending);
2481 dbus_pending_call_unref (pending);
2485 check_for_reply_and_update_dispatch_unlocked (DBusPendingCall *pending)
2488 DBusDispatchStatus status;
2489 DBusConnection *connection;
2491 connection = _dbus_pending_call_get_connection (pending);
2493 reply = check_for_reply_unlocked (connection,
2494 _dbus_pending_call_get_reply_serial (pending));
2497 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2499 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2501 complete_pending_call_and_unlock (pending, reply);
2502 dbus_message_unref (reply);
2504 CONNECTION_LOCK (connection);
2505 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2506 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2507 dbus_pending_call_unref (pending);
2516 * When a function that blocks has been called with a timeout, and we
2517 * run out of memory, the time to wait for memory is based on the
2518 * timeout. If the caller was willing to block a long time we wait a
2519 * relatively long time for memory, if they were only willing to block
2520 * briefly then we retry for memory at a rapid rate.
2522 * @timeout_milliseconds the timeout requested for blocking
2525 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2527 if (timeout_milliseconds == -1)
2528 _dbus_sleep_milliseconds (1000);
2529 else if (timeout_milliseconds < 100)
2530 ; /* just busy loop */
2531 else if (timeout_milliseconds <= 1000)
2532 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2534 _dbus_sleep_milliseconds (1000);
2538 * Blocks until a pending call times out or gets a reply.
2540 * Does not re-enter the main loop or run filter/path-registered
2541 * callbacks. The reply to the message will not be seen by
2544 * Returns immediately if pending call already got a reply.
2546 * @todo could use performance improvements (it keeps scanning
2547 * the whole message queue for example)
2549 * @param pending the pending call we block for a reply on
2552 _dbus_connection_block_pending_call (DBusPendingCall *pending)
2554 long start_tv_sec, start_tv_usec;
2555 long end_tv_sec, end_tv_usec;
2556 long tv_sec, tv_usec;
2557 DBusDispatchStatus status;
2558 DBusConnection *connection;
2559 dbus_uint32_t client_serial;
2560 int timeout_milliseconds;
2562 _dbus_assert (pending != NULL);
2564 if (dbus_pending_call_get_completed (pending))
2567 connection = _dbus_pending_call_get_connection (pending);
2568 if (connection == NULL)
2569 return; /* call already detached */
2571 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2572 client_serial = _dbus_pending_call_get_reply_serial (pending);
2574 /* note that timeout_milliseconds is limited to a smallish value
2575 * in _dbus_pending_call_new() so overflows aren't possible
2578 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout (pending));
2580 /* Flush message queue */
2581 dbus_connection_flush (connection);
2583 CONNECTION_LOCK (connection);
2585 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
2586 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
2587 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
2588 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
2589 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
2591 _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",
2592 timeout_milliseconds,
2594 start_tv_sec, start_tv_usec,
2595 end_tv_sec, end_tv_usec);
2597 /* check to see if we already got the data off the socket */
2598 /* from another blocked pending call */
2599 if (check_for_reply_and_update_dispatch_unlocked (pending))
2602 /* Now we wait... */
2603 /* always block at least once as we know we don't have the reply yet */
2604 _dbus_connection_do_iteration_unlocked (connection,
2605 DBUS_ITERATION_DO_READING |
2606 DBUS_ITERATION_BLOCK,
2607 timeout_milliseconds);
2611 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2613 HAVE_LOCK_CHECK (connection);
2615 /* queue messages and get status */
2617 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2619 /* the get_completed() is in case a dispatch() while we were blocking
2620 * got the reply instead of us.
2622 if (dbus_pending_call_get_completed (pending))
2624 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
2625 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2626 dbus_pending_call_unref (pending);
2630 if (status == DBUS_DISPATCH_DATA_REMAINS)
2631 if (check_for_reply_and_update_dispatch_unlocked (pending))
2634 _dbus_get_current_time (&tv_sec, &tv_usec);
2636 if (!_dbus_connection_get_is_connected_unlocked (connection))
2638 /* FIXME send a "DBUS_ERROR_DISCONNECTED" instead, just to help
2639 * programmers understand what went wrong since the timeout is
2643 complete_pending_call_and_unlock (pending, NULL);
2644 dbus_pending_call_unref (pending);
2647 else if (tv_sec < start_tv_sec)
2648 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2649 else if (connection->disconnect_message_link == NULL)
2650 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2651 else if (tv_sec < end_tv_sec ||
2652 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
2654 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
2655 (end_tv_usec - tv_usec) / 1000;
2656 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
2657 _dbus_assert (timeout_milliseconds >= 0);
2659 if (status == DBUS_DISPATCH_NEED_MEMORY)
2661 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2662 * we may already have a reply in the buffer and just can't process
2665 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2667 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2671 /* block again, we don't have the reply buffered yet. */
2672 _dbus_connection_do_iteration_unlocked (connection,
2673 DBUS_ITERATION_DO_READING |
2674 DBUS_ITERATION_BLOCK,
2675 timeout_milliseconds);
2678 goto recheck_status;
2681 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2682 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2684 _dbus_assert (!dbus_pending_call_get_completed (pending));
2686 /* unlock and call user code */
2687 complete_pending_call_and_unlock (pending, NULL);
2689 /* update user code on dispatch status */
2690 CONNECTION_LOCK (connection);
2691 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2692 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2693 dbus_pending_call_unref (pending);
2697 * Sends a message and blocks a certain time period while waiting for
2698 * a reply. This function does not reenter the main loop,
2699 * i.e. messages other than the reply are queued up but not
2700 * processed. This function is used to do non-reentrant "method
2703 * If a normal reply is received, it is returned, and removed from the
2704 * incoming message queue. If it is not received, #NULL is returned
2705 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
2706 * received, it is converted to a #DBusError and returned as an error,
2707 * then the reply message is deleted. If something else goes wrong,
2708 * result is set to whatever is appropriate, such as
2709 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
2711 * @param connection the connection
2712 * @param message the message to send
2713 * @param timeout_milliseconds timeout in milliseconds or -1 for default
2714 * @param error return location for error message
2715 * @returns the message that is the reply or #NULL with an error code if the
2719 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
2720 DBusMessage *message,
2721 int timeout_milliseconds,
2725 DBusPendingCall *pending;
2727 _dbus_return_val_if_fail (connection != NULL, NULL);
2728 _dbus_return_val_if_fail (message != NULL, NULL);
2729 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
2730 _dbus_return_val_if_error_is_set (error, NULL);
2732 if (!dbus_connection_send_with_reply (connection, message,
2733 &pending, timeout_milliseconds))
2735 _DBUS_SET_OOM (error);
2739 _dbus_assert (pending != NULL);
2741 dbus_pending_call_block (pending);
2743 reply = dbus_pending_call_steal_reply (pending);
2744 dbus_pending_call_unref (pending);
2746 /* call_complete_and_unlock() called from pending_call_block() should
2747 * always fill this in.
2749 _dbus_assert (reply != NULL);
2751 if (dbus_set_error_from_message (error, reply))
2753 dbus_message_unref (reply);
2761 * Blocks until the outgoing message queue is empty.
2763 * @param connection the connection.
2766 dbus_connection_flush (DBusConnection *connection)
2768 /* We have to specify DBUS_ITERATION_DO_READING here because
2769 * otherwise we could have two apps deadlock if they are both doing
2770 * a flush(), and the kernel buffers fill up. This could change the
2773 DBusDispatchStatus status;
2775 _dbus_return_if_fail (connection != NULL);
2777 CONNECTION_LOCK (connection);
2778 while (connection->n_outgoing > 0 &&
2779 _dbus_connection_get_is_connected_unlocked (connection))
2781 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
2782 HAVE_LOCK_CHECK (connection);
2783 _dbus_connection_do_iteration_unlocked (connection,
2784 DBUS_ITERATION_DO_READING |
2785 DBUS_ITERATION_DO_WRITING |
2786 DBUS_ITERATION_BLOCK,
2790 HAVE_LOCK_CHECK (connection);
2791 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2792 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2794 HAVE_LOCK_CHECK (connection);
2795 /* Unlocks and calls out to user code */
2796 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2798 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
2802 * This function is intended for use with applications that don't want
2803 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
2804 * example usage would be:
2807 * while (dbus_connection_read_write_dispatch (connection, -1))
2808 * ; // empty loop body
2811 * In this usage you would normally have set up a filter function to look
2812 * at each message as it is dispatched. The loop terminates when the last
2813 * message from the connection (the disconnected signal) is processed.
2815 * If there are messages to dispatch and the dispatch flag is set, this
2816 * function will dbus_connection_dispatch() once, and return. If there are no
2817 * messages to dispatch, this function will block until it can read or write,
2818 * then read or write, then return.
2820 * The way to think of this function is that it either makes some sort
2821 * of progress, or it blocks.
2823 * The return value indicates whether the disconnect message has been
2824 * processed, NOT whether the connection is connected. This is
2825 * important because even after disconnecting, you want to process any
2826 * messages you received prior to the disconnect.
2828 * @param connection the connection
2829 * @param timeout_milliseconds max time to block or -1 for infinite
2830 * @param dispatch dispatch new messages or leave them on the incoming queue
2831 * @returns #TRUE if the disconnect message has not been processed
2834 _dbus_connection_read_write_dispatch (DBusConnection *connection,
2835 int timeout_milliseconds,
2836 dbus_bool_t dispatch)
2838 DBusDispatchStatus dstatus;
2839 dbus_bool_t dispatched_disconnected;
2841 dstatus = dbus_connection_get_dispatch_status (connection);
2843 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
2845 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
2846 dbus_connection_dispatch (connection);
2847 CONNECTION_LOCK (connection);
2849 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
2851 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
2852 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
2853 CONNECTION_LOCK (connection);
2857 CONNECTION_LOCK (connection);
2858 if (_dbus_connection_get_is_connected_unlocked (connection))
2860 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
2861 _dbus_connection_do_iteration_unlocked (connection,
2862 DBUS_ITERATION_DO_READING |
2863 DBUS_ITERATION_DO_WRITING |
2864 DBUS_ITERATION_BLOCK,
2865 timeout_milliseconds);
2869 HAVE_LOCK_CHECK (connection);
2870 dispatched_disconnected = connection->n_incoming == 0 &&
2871 connection->disconnect_message_link == NULL;
2872 CONNECTION_UNLOCK (connection);
2873 return !dispatched_disconnected; /* TRUE if we have not processed disconnected */
2878 * This function is intended for use with applications that don't want
2879 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
2880 * example usage would be:
2883 * while (dbus_connection_read_write_dispatch (connection, -1))
2884 * ; // empty loop body
2887 * In this usage you would normally have set up a filter function to look
2888 * at each message as it is dispatched. The loop terminates when the last
2889 * message from the connection (the disconnected signal) is processed.
2891 * If there are messages to dispatch, this function will
2892 * dbus_connection_dispatch() once, and return. If there are no
2893 * messages to dispatch, this function will block until it can read or
2894 * write, then read or write, then return.
2896 * The way to think of this function is that it either makes some sort
2897 * of progress, or it blocks.
2899 * The return value indicates whether the disconnect message has been
2900 * processed, NOT whether the connection is connected. This is
2901 * important because even after disconnecting, you want to process any
2902 * messages you received prior to the disconnect.
2904 * @param connection the connection
2905 * @param timeout_milliseconds max time to block or -1 for infinite
2906 * @returns #TRUE if the disconnect message has not been processed
2909 dbus_connection_read_write_dispatch (DBusConnection *connection,
2910 int timeout_milliseconds)
2912 _dbus_return_val_if_fail (connection != NULL, FALSE);
2913 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2914 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
2918 * This function is intended for use with applications that don't want to
2919 * write a main loop and deal with #DBusWatch and #DBusTimeout.
2921 * If there are no messages to dispatch, this function will block until it can
2922 * read or write, then read or write, then return.
2924 * The return value indicates whether the disconnect message has been
2925 * processed, NOT whether the connection is connected. This is important
2926 * because even after disconnecting, you want to process any messages you
2927 * received prior to the disconnect.
2929 * @param connection the connection
2930 * @param timeout_milliseconds max time to block or -1 for infinite
2931 * @returns #TRUE if the disconnect message has not been processed
2934 dbus_connection_read_write (DBusConnection *connection,
2935 int timeout_milliseconds)
2937 _dbus_return_val_if_fail (connection != NULL, FALSE);
2938 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2939 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
2943 * Returns the first-received message from the incoming message queue,
2944 * leaving it in the queue. If the queue is empty, returns #NULL.
2946 * The caller does not own a reference to the returned message, and
2947 * must either return it using dbus_connection_return_message() or
2948 * keep it after calling dbus_connection_steal_borrowed_message(). No
2949 * one can get at the message while its borrowed, so return it as
2950 * quickly as possible and don't keep a reference to it after
2951 * returning it. If you need to keep the message, make a copy of it.
2953 * dbus_connection_dispatch() will block if called while a borrowed
2954 * message is outstanding; only one piece of code can be playing with
2955 * the incoming queue at a time. This function will block if called
2956 * during a dbus_connection_dispatch().
2958 * @param connection the connection.
2959 * @returns next message in the incoming queue.
2962 dbus_connection_borrow_message (DBusConnection *connection)
2964 DBusDispatchStatus status;
2965 DBusMessage *message;
2967 _dbus_return_val_if_fail (connection != NULL, NULL);
2969 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
2971 /* this is called for the side effect that it queues
2972 * up any messages from the transport
2974 status = dbus_connection_get_dispatch_status (connection);
2975 if (status != DBUS_DISPATCH_DATA_REMAINS)
2978 CONNECTION_LOCK (connection);
2980 _dbus_connection_acquire_dispatch (connection);
2982 /* While a message is outstanding, the dispatch lock is held */
2983 _dbus_assert (connection->message_borrowed == NULL);
2985 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
2987 message = connection->message_borrowed;
2989 /* Note that we KEEP the dispatch lock until the message is returned */
2990 if (message == NULL)
2991 _dbus_connection_release_dispatch (connection);
2993 CONNECTION_UNLOCK (connection);
2999 * Used to return a message after peeking at it using
3000 * dbus_connection_borrow_message(). Only called if
3001 * message from dbus_connection_borrow_message() was non-#NULL.
3003 * @param connection the connection
3004 * @param message the message from dbus_connection_borrow_message()
3007 dbus_connection_return_message (DBusConnection *connection,
3008 DBusMessage *message)
3010 _dbus_return_if_fail (connection != NULL);
3011 _dbus_return_if_fail (message != NULL);
3012 _dbus_return_if_fail (message == connection->message_borrowed);
3013 _dbus_return_if_fail (connection->dispatch_acquired);
3015 CONNECTION_LOCK (connection);
3017 _dbus_assert (message == connection->message_borrowed);
3019 connection->message_borrowed = NULL;
3021 _dbus_connection_release_dispatch (connection);
3023 CONNECTION_UNLOCK (connection);
3027 * Used to keep a message after peeking at it using
3028 * dbus_connection_borrow_message(). Before using this function, see
3029 * the caveats/warnings in the documentation for
3030 * dbus_connection_pop_message().
3032 * @param connection the connection
3033 * @param message the message from dbus_connection_borrow_message()
3036 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3037 DBusMessage *message)
3039 DBusMessage *pop_message;
3041 _dbus_return_if_fail (connection != NULL);
3042 _dbus_return_if_fail (message != NULL);
3043 _dbus_return_if_fail (message == connection->message_borrowed);
3044 _dbus_return_if_fail (connection->dispatch_acquired);
3046 CONNECTION_LOCK (connection);
3048 _dbus_assert (message == connection->message_borrowed);
3050 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3051 _dbus_assert (message == pop_message);
3053 connection->n_incoming -= 1;
3055 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3056 message, connection->n_incoming);
3058 connection->message_borrowed = NULL;
3060 _dbus_connection_release_dispatch (connection);
3062 CONNECTION_UNLOCK (connection);
3065 /* See dbus_connection_pop_message, but requires the caller to own
3066 * the lock before calling. May drop the lock while running.
3069 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3071 HAVE_LOCK_CHECK (connection);
3073 _dbus_assert (connection->message_borrowed == NULL);
3075 if (connection->n_incoming > 0)
3079 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3080 connection->n_incoming -= 1;
3082 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3084 dbus_message_get_type (link->data),
3085 dbus_message_get_path (link->data) ?
3086 dbus_message_get_path (link->data) :
3088 dbus_message_get_interface (link->data) ?
3089 dbus_message_get_interface (link->data) :
3091 dbus_message_get_member (link->data) ?
3092 dbus_message_get_member (link->data) :
3094 dbus_message_get_signature (link->data),
3095 connection, connection->n_incoming);
3103 /* See dbus_connection_pop_message, but requires the caller to own
3104 * the lock before calling. May drop the lock while running.
3107 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3111 HAVE_LOCK_CHECK (connection);
3113 link = _dbus_connection_pop_message_link_unlocked (connection);
3117 DBusMessage *message;
3119 message = link->data;
3121 _dbus_list_free_link (link);
3130 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3131 DBusList *message_link)
3133 HAVE_LOCK_CHECK (connection);
3135 _dbus_assert (message_link != NULL);
3136 /* You can't borrow a message while a link is outstanding */
3137 _dbus_assert (connection->message_borrowed == NULL);
3138 /* We had to have the dispatch lock across the pop/putback */
3139 _dbus_assert (connection->dispatch_acquired);
3141 _dbus_list_prepend_link (&connection->incoming_messages,
3143 connection->n_incoming += 1;
3145 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
3147 dbus_message_get_type (message_link->data),
3148 dbus_message_get_interface (message_link->data) ?
3149 dbus_message_get_interface (message_link->data) :
3151 dbus_message_get_member (message_link->data) ?
3152 dbus_message_get_member (message_link->data) :
3154 dbus_message_get_signature (message_link->data),
3155 connection, connection->n_incoming);
3159 * Returns the first-received message from the incoming message queue,
3160 * removing it from the queue. The caller owns a reference to the
3161 * returned message. If the queue is empty, returns #NULL.
3163 * This function bypasses any message handlers that are registered,
3164 * and so using it is usually wrong. Instead, let the main loop invoke
3165 * dbus_connection_dispatch(). Popping messages manually is only
3166 * useful in very simple programs that don't share a #DBusConnection
3167 * with any libraries or other modules.
3169 * There is a lock that covers all ways of accessing the incoming message
3170 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
3171 * dbus_connection_borrow_message(), etc. will all block while one of the others
3172 * in the group is running.
3174 * @param connection the connection.
3175 * @returns next message in the incoming queue.
3178 dbus_connection_pop_message (DBusConnection *connection)
3180 DBusMessage *message;
3181 DBusDispatchStatus status;
3183 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3185 /* this is called for the side effect that it queues
3186 * up any messages from the transport
3188 status = dbus_connection_get_dispatch_status (connection);
3189 if (status != DBUS_DISPATCH_DATA_REMAINS)
3192 CONNECTION_LOCK (connection);
3193 _dbus_connection_acquire_dispatch (connection);
3194 HAVE_LOCK_CHECK (connection);
3196 message = _dbus_connection_pop_message_unlocked (connection);
3198 _dbus_verbose ("Returning popped message %p\n", message);
3200 _dbus_connection_release_dispatch (connection);
3201 CONNECTION_UNLOCK (connection);
3207 * Acquire the dispatcher. This is a separate lock so the main
3208 * connection lock can be dropped to call out to application dispatch
3211 * @param connection the connection.
3214 _dbus_connection_acquire_dispatch (DBusConnection *connection)
3216 HAVE_LOCK_CHECK (connection);
3218 _dbus_connection_ref_unlocked (connection);
3219 CONNECTION_UNLOCK (connection);
3221 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3222 _dbus_mutex_lock (connection->dispatch_mutex);
3224 while (connection->dispatch_acquired)
3226 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
3227 _dbus_condvar_wait (connection->dispatch_cond, connection->dispatch_mutex);
3230 _dbus_assert (!connection->dispatch_acquired);
3232 connection->dispatch_acquired = TRUE;
3234 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3235 _dbus_mutex_unlock (connection->dispatch_mutex);
3237 CONNECTION_LOCK (connection);
3238 _dbus_connection_unref_unlocked (connection);
3242 * Release the dispatcher when you're done with it. Only call
3243 * after you've acquired the dispatcher. Wakes up at most one
3244 * thread currently waiting to acquire the dispatcher.
3246 * @param connection the connection.
3249 _dbus_connection_release_dispatch (DBusConnection *connection)
3251 HAVE_LOCK_CHECK (connection);
3253 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3254 _dbus_mutex_lock (connection->dispatch_mutex);
3256 _dbus_assert (connection->dispatch_acquired);
3258 connection->dispatch_acquired = FALSE;
3259 _dbus_condvar_wake_one (connection->dispatch_cond);
3261 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
3262 _dbus_mutex_unlock (connection->dispatch_mutex);
3266 _dbus_connection_failed_pop (DBusConnection *connection,
3267 DBusList *message_link)
3269 _dbus_list_prepend_link (&connection->incoming_messages,
3271 connection->n_incoming += 1;
3274 static DBusDispatchStatus
3275 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
3277 HAVE_LOCK_CHECK (connection);
3279 if (connection->n_incoming > 0)
3280 return DBUS_DISPATCH_DATA_REMAINS;
3281 else if (!_dbus_transport_queue_messages (connection->transport))
3282 return DBUS_DISPATCH_NEED_MEMORY;
3285 DBusDispatchStatus status;
3286 dbus_bool_t is_connected;
3288 status = _dbus_transport_get_dispatch_status (connection->transport);
3289 is_connected = _dbus_transport_get_is_connected (connection->transport);
3291 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
3292 DISPATCH_STATUS_NAME (status), is_connected);
3296 if (status == DBUS_DISPATCH_COMPLETE &&
3297 connection->disconnect_message_link)
3299 _dbus_verbose ("Sending disconnect message from %s\n",
3300 _DBUS_FUNCTION_NAME);
3302 connection_forget_shared_unlocked (connection);
3304 /* We haven't sent the disconnect message already,
3305 * and all real messages have been queued up.
3307 _dbus_connection_queue_synthesized_message_link (connection,
3308 connection->disconnect_message_link);
3309 connection->disconnect_message_link = NULL;
3311 status = DBUS_DISPATCH_DATA_REMAINS;
3314 /* Dump the outgoing queue, we aren't going to be able to
3315 * send it now, and we'd like accessors like
3316 * dbus_connection_get_outgoing_size() to be accurate.
3318 if (connection->n_outgoing > 0)
3322 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
3323 connection->n_outgoing);
3325 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
3327 _dbus_connection_message_sent (connection, link->data);
3332 if (status != DBUS_DISPATCH_COMPLETE)
3334 else if (connection->n_incoming > 0)
3335 return DBUS_DISPATCH_DATA_REMAINS;
3337 return DBUS_DISPATCH_COMPLETE;
3342 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
3343 DBusDispatchStatus new_status)
3345 dbus_bool_t changed;
3346 DBusDispatchStatusFunction function;
3349 HAVE_LOCK_CHECK (connection);
3351 _dbus_connection_ref_unlocked (connection);
3353 changed = new_status != connection->last_dispatch_status;
3355 connection->last_dispatch_status = new_status;
3357 function = connection->dispatch_status_function;
3358 data = connection->dispatch_status_data;
3360 /* We drop the lock */
3361 CONNECTION_UNLOCK (connection);
3363 if (changed && function)
3365 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
3366 connection, new_status,
3367 DISPATCH_STATUS_NAME (new_status));
3368 (* function) (connection, new_status, data);
3371 dbus_connection_unref (connection);
3375 * Gets the current state (what we would currently return
3376 * from dbus_connection_dispatch()) but doesn't actually
3377 * dispatch any messages.
3379 * @param connection the connection.
3380 * @returns current dispatch status
3383 dbus_connection_get_dispatch_status (DBusConnection *connection)
3385 DBusDispatchStatus status;
3387 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3389 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3391 CONNECTION_LOCK (connection);
3393 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3395 CONNECTION_UNLOCK (connection);
3401 * Filter funtion for handling the Peer standard interface
3403 static DBusHandlerResult
3404 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
3405 DBusMessage *message)
3407 if (dbus_message_is_method_call (message,
3408 DBUS_INTERFACE_PEER,
3414 ret = dbus_message_new_method_return (message);
3416 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3418 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
3420 dbus_message_unref (ret);
3423 return DBUS_HANDLER_RESULT_NEED_MEMORY;
3425 return DBUS_HANDLER_RESULT_HANDLED;
3429 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3433 * Processes all builtin filter functions
3435 * If the spec specifies a standard interface
3436 * they should be processed from this method
3438 static DBusHandlerResult
3439 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
3440 DBusMessage *message)
3442 /* We just run one filter for now but have the option to run more
3443 if the spec calls for it in the future */
3445 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
3449 * Processes data buffered while handling watches, queueing zero or
3450 * more incoming messages. Then pops the first-received message from
3451 * the current incoming message queue, runs any handlers for it, and
3452 * unrefs the message. Returns a status indicating whether messages/data
3453 * remain, more memory is needed, or all data has been processed.
3455 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
3456 * does not necessarily dispatch a message, as the data may
3457 * be part of authentication or the like.
3459 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
3461 * @todo FIXME what if we call out to application code to handle a
3462 * message, holding the dispatch lock, and the application code runs
3463 * the main loop and dispatches again? Probably deadlocks at the
3464 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
3465 * and then the GSource etc. could handle the situation? Right now
3466 * our GSource is NO_RECURSE
3468 * @param connection the connection
3469 * @returns dispatch status
3472 dbus_connection_dispatch (DBusConnection *connection)
3474 DBusMessage *message;
3475 DBusList *link, *filter_list_copy, *message_link;
3476 DBusHandlerResult result;
3477 DBusPendingCall *pending;
3478 dbus_int32_t reply_serial;
3479 DBusDispatchStatus status;
3481 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
3483 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3485 CONNECTION_LOCK (connection);
3486 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3487 if (status != DBUS_DISPATCH_DATA_REMAINS)
3489 /* unlocks and calls out to user code */
3490 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3494 /* We need to ref the connection since the callback could potentially
3495 * drop the last ref to it
3497 _dbus_connection_ref_unlocked (connection);
3499 _dbus_connection_acquire_dispatch (connection);
3500 HAVE_LOCK_CHECK (connection);
3502 message_link = _dbus_connection_pop_message_link_unlocked (connection);
3503 if (message_link == NULL)
3505 /* another thread dispatched our stuff */
3507 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
3509 _dbus_connection_release_dispatch (connection);
3511 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3513 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3515 dbus_connection_unref (connection);
3520 message = message_link->data;
3522 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
3524 dbus_message_get_type (message),
3525 dbus_message_get_interface (message) ?
3526 dbus_message_get_interface (message) :
3528 dbus_message_get_member (message) ?
3529 dbus_message_get_member (message) :
3531 dbus_message_get_signature (message));
3533 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3535 /* Pending call handling must be first, because if you do
3536 * dbus_connection_send_with_reply_and_block() or
3537 * dbus_pending_call_block() then no handlers/filters will be run on
3538 * the reply. We want consistent semantics in the case where we
3539 * dbus_connection_dispatch() the reply.
3542 reply_serial = dbus_message_get_reply_serial (message);
3543 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
3547 _dbus_verbose ("Dispatching a pending reply\n");
3548 complete_pending_call_and_unlock (pending, message);
3549 pending = NULL; /* it's probably unref'd */
3551 CONNECTION_LOCK (connection);
3552 _dbus_verbose ("pending call completed in dispatch\n");
3553 result = DBUS_HANDLER_RESULT_HANDLED;
3557 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
3558 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3561 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
3563 _dbus_connection_release_dispatch (connection);
3564 HAVE_LOCK_CHECK (connection);
3566 _dbus_connection_failed_pop (connection, message_link);
3568 /* unlocks and calls user code */
3569 _dbus_connection_update_dispatch_status_and_unlock (connection,
3570 DBUS_DISPATCH_NEED_MEMORY);
3573 dbus_pending_call_unref (pending);
3574 dbus_connection_unref (connection);
3576 return DBUS_DISPATCH_NEED_MEMORY;
3579 _dbus_list_foreach (&filter_list_copy,
3580 (DBusForeachFunction)_dbus_message_filter_ref,
3583 /* We're still protected from dispatch() reentrancy here
3584 * since we acquired the dispatcher
3586 CONNECTION_UNLOCK (connection);
3588 link = _dbus_list_get_first_link (&filter_list_copy);
3589 while (link != NULL)
3591 DBusMessageFilter *filter = link->data;
3592 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
3594 _dbus_verbose (" running filter on message %p\n", message);
3595 result = (* filter->function) (connection, message, filter->user_data);
3597 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3603 _dbus_list_foreach (&filter_list_copy,
3604 (DBusForeachFunction)_dbus_message_filter_unref,
3606 _dbus_list_clear (&filter_list_copy);
3608 CONNECTION_LOCK (connection);
3610 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
3612 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
3615 else if (result == DBUS_HANDLER_RESULT_HANDLED)
3617 _dbus_verbose ("filter handled message in dispatch\n");
3621 /* We're still protected from dispatch() reentrancy here
3622 * since we acquired the dispatcher
3624 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
3626 dbus_message_get_type (message),
3627 dbus_message_get_interface (message) ?
3628 dbus_message_get_interface (message) :
3630 dbus_message_get_member (message) ?
3631 dbus_message_get_member (message) :
3633 dbus_message_get_signature (message));
3635 HAVE_LOCK_CHECK (connection);
3636 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
3639 CONNECTION_LOCK (connection);
3641 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
3643 _dbus_verbose ("object tree handled message in dispatch\n");
3647 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
3651 DBusPreallocatedSend *preallocated;
3653 _dbus_verbose (" sending error %s\n",
3654 DBUS_ERROR_UNKNOWN_METHOD);
3656 if (!_dbus_string_init (&str))
3658 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3659 _dbus_verbose ("no memory for error string in dispatch\n");
3663 if (!_dbus_string_append_printf (&str,
3664 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
3665 dbus_message_get_member (message),
3666 dbus_message_get_signature (message),
3667 dbus_message_get_interface (message)))
3669 _dbus_string_free (&str);
3670 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3671 _dbus_verbose ("no memory for error string in dispatch\n");
3675 reply = dbus_message_new_error (message,
3676 DBUS_ERROR_UNKNOWN_METHOD,
3677 _dbus_string_get_const_data (&str));
3678 _dbus_string_free (&str);
3682 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3683 _dbus_verbose ("no memory for error reply in dispatch\n");
3687 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3689 if (preallocated == NULL)
3691 dbus_message_unref (reply);
3692 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
3693 _dbus_verbose ("no memory for error send in dispatch\n");
3697 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
3700 dbus_message_unref (reply);
3702 result = DBUS_HANDLER_RESULT_HANDLED;
3705 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
3706 dbus_message_get_type (message),
3707 dbus_message_get_interface (message) ?
3708 dbus_message_get_interface (message) :
3710 dbus_message_get_member (message) ?
3711 dbus_message_get_member (message) :
3713 dbus_message_get_signature (message),
3717 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
3719 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
3721 /* Put message back, and we'll start over.
3722 * Yes this means handlers must be idempotent if they
3723 * don't return HANDLED; c'est la vie.
3725 _dbus_connection_putback_message_link_unlocked (connection,
3730 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
3732 if (dbus_message_is_signal (message,
3733 DBUS_INTERFACE_LOCAL,
3736 _dbus_bus_check_connection_and_unref (connection);
3737 if (connection->exit_on_disconnect)
3739 _dbus_verbose ("Exiting on Disconnected signal\n");
3740 CONNECTION_UNLOCK (connection);
3742 _dbus_assert_not_reached ("Call to exit() returned");
3746 _dbus_list_free_link (message_link);
3747 dbus_message_unref (message); /* don't want the message to count in max message limits
3748 * in computing dispatch status below
3752 _dbus_connection_release_dispatch (connection);
3753 HAVE_LOCK_CHECK (connection);
3755 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
3756 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3758 /* unlocks and calls user code */
3759 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3761 dbus_connection_unref (connection);
3767 * Sets the watch functions for the connection. These functions are
3768 * responsible for making the application's main loop aware of file
3769 * descriptors that need to be monitored for events, using select() or
3770 * poll(). When using Qt, typically the DBusAddWatchFunction would
3771 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
3772 * could call g_io_add_watch(), or could be used as part of a more
3773 * elaborate GSource. Note that when a watch is added, it may
3776 * The DBusWatchToggledFunction notifies the application that the
3777 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
3778 * to check this. A disabled watch should have no effect, and enabled
3779 * watch should be added to the main loop. This feature is used
3780 * instead of simply adding/removing the watch because
3781 * enabling/disabling can be done without memory allocation. The
3782 * toggled function may be NULL if a main loop re-queries
3783 * dbus_watch_get_enabled() every time anyway.
3785 * The DBusWatch can be queried for the file descriptor to watch using
3786 * dbus_watch_get_fd(), and for the events to watch for using
3787 * dbus_watch_get_flags(). The flags returned by
3788 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
3789 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
3790 * all watches implicitly include a watch for hangups, errors, and
3791 * other exceptional conditions.
3793 * Once a file descriptor becomes readable or writable, or an exception
3794 * occurs, dbus_watch_handle() should be called to
3795 * notify the connection of the file descriptor's condition.
3797 * dbus_watch_handle() cannot be called during the
3798 * DBusAddWatchFunction, as the connection will not be ready to handle
3801 * It is not allowed to reference a DBusWatch after it has been passed
3802 * to remove_function.
3804 * If #FALSE is returned due to lack of memory, the failure may be due
3805 * to a #FALSE return from the new add_function. If so, the
3806 * add_function may have been called successfully one or more times,
3807 * but the remove_function will also have been called to remove any
3808 * successful adds. i.e. if #FALSE is returned the net result
3809 * should be that dbus_connection_set_watch_functions() has no effect,
3810 * but the add_function and remove_function may have been called.
3812 * @todo We need to drop the lock when we call the
3813 * add/remove/toggled functions which can be a side effect
3814 * of setting the watch functions.
3816 * @param connection the connection.
3817 * @param add_function function to begin monitoring a new descriptor.
3818 * @param remove_function function to stop monitoring a descriptor.
3819 * @param toggled_function function to notify of enable/disable
3820 * @param data data to pass to add_function and remove_function.
3821 * @param free_data_function function to be called to free the data.
3822 * @returns #FALSE on failure (no memory)
3825 dbus_connection_set_watch_functions (DBusConnection *connection,
3826 DBusAddWatchFunction add_function,
3827 DBusRemoveWatchFunction remove_function,
3828 DBusWatchToggledFunction toggled_function,
3830 DBusFreeFunction free_data_function)
3833 DBusWatchList *watches;
3835 _dbus_return_val_if_fail (connection != NULL, FALSE);
3837 CONNECTION_LOCK (connection);
3839 #ifndef DBUS_DISABLE_CHECKS
3840 if (connection->watches == NULL)
3842 _dbus_warn ("Re-entrant call to %s is not allowed\n",
3843 _DBUS_FUNCTION_NAME);
3848 /* ref connection for slightly better reentrancy */
3849 _dbus_connection_ref_unlocked (connection);
3851 /* This can call back into user code, and we need to drop the
3852 * connection lock when it does. This is kind of a lame
3855 watches = connection->watches;
3856 connection->watches = NULL;
3857 CONNECTION_UNLOCK (connection);
3859 retval = _dbus_watch_list_set_functions (watches,
3860 add_function, remove_function,
3862 data, free_data_function);
3863 CONNECTION_LOCK (connection);
3864 connection->watches = watches;
3866 CONNECTION_UNLOCK (connection);
3867 /* drop our paranoid refcount */
3868 dbus_connection_unref (connection);
3874 * Sets the timeout functions for the connection. These functions are
3875 * responsible for making the application's main loop aware of timeouts.
3876 * When using Qt, typically the DBusAddTimeoutFunction would create a
3877 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
3880 * The DBusTimeoutToggledFunction notifies the application that the
3881 * timeout has been enabled or disabled. Call
3882 * dbus_timeout_get_enabled() to check this. A disabled timeout should
3883 * have no effect, and enabled timeout should be added to the main
3884 * loop. This feature is used instead of simply adding/removing the
3885 * timeout because enabling/disabling can be done without memory
3886 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
3887 * to enable and disable. The toggled function may be NULL if a main
3888 * loop re-queries dbus_timeout_get_enabled() every time anyway.
3889 * Whenever a timeout is toggled, its interval may change.
3891 * The DBusTimeout can be queried for the timer interval using
3892 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
3893 * repeatedly, each time the interval elapses, starting after it has
3894 * elapsed once. The timeout stops firing when it is removed with the
3895 * given remove_function. The timer interval may change whenever the
3896 * timeout is added, removed, or toggled.
3898 * @param connection the connection.
3899 * @param add_function function to add a timeout.
3900 * @param remove_function function to remove a timeout.
3901 * @param toggled_function function to notify of enable/disable
3902 * @param data data to pass to add_function and remove_function.
3903 * @param free_data_function function to be called to free the data.
3904 * @returns #FALSE on failure (no memory)
3907 dbus_connection_set_timeout_functions (DBusConnection *connection,
3908 DBusAddTimeoutFunction add_function,
3909 DBusRemoveTimeoutFunction remove_function,
3910 DBusTimeoutToggledFunction toggled_function,
3912 DBusFreeFunction free_data_function)
3915 DBusTimeoutList *timeouts;
3917 _dbus_return_val_if_fail (connection != NULL, FALSE);
3919 CONNECTION_LOCK (connection);
3921 #ifndef DBUS_DISABLE_CHECKS
3922 if (connection->timeouts == NULL)
3924 _dbus_warn ("Re-entrant call to %s is not allowed\n",
3925 _DBUS_FUNCTION_NAME);
3930 /* ref connection for slightly better reentrancy */
3931 _dbus_connection_ref_unlocked (connection);
3933 timeouts = connection->timeouts;
3934 connection->timeouts = NULL;
3935 CONNECTION_UNLOCK (connection);
3937 retval = _dbus_timeout_list_set_functions (timeouts,
3938 add_function, remove_function,
3940 data, free_data_function);
3941 CONNECTION_LOCK (connection);
3942 connection->timeouts = timeouts;
3944 CONNECTION_UNLOCK (connection);
3945 /* drop our paranoid refcount */
3946 dbus_connection_unref (connection);
3952 * Sets the mainloop wakeup function for the connection. This function is
3953 * responsible for waking up the main loop (if its sleeping) when some some
3954 * change has happened to the connection that the mainloop needs to reconsider
3955 * (e.g. a message has been queued for writing).
3956 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
3957 * When using GLib, it would call g_main_context_wakeup().
3960 * @param connection the connection.
3961 * @param wakeup_main_function function to wake up the mainloop
3962 * @param data data to pass wakeup_main_function
3963 * @param free_data_function function to be called to free the data.
3966 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
3967 DBusWakeupMainFunction wakeup_main_function,
3969 DBusFreeFunction free_data_function)
3972 DBusFreeFunction old_free_data;
3974 _dbus_return_if_fail (connection != NULL);
3976 CONNECTION_LOCK (connection);
3977 old_data = connection->wakeup_main_data;
3978 old_free_data = connection->free_wakeup_main_data;
3980 connection->wakeup_main_function = wakeup_main_function;
3981 connection->wakeup_main_data = data;
3982 connection->free_wakeup_main_data = free_data_function;
3984 CONNECTION_UNLOCK (connection);
3986 /* Callback outside the lock */
3988 (*old_free_data) (old_data);
3992 * Set a function to be invoked when the dispatch status changes.
3993 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
3994 * dbus_connection_dispatch() needs to be called to process incoming
3995 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
3996 * from inside the DBusDispatchStatusFunction. Indeed, almost
3997 * any reentrancy in this function is a bad idea. Instead,
3998 * the DBusDispatchStatusFunction should simply save an indication
3999 * that messages should be dispatched later, when the main loop
4002 * @param connection the connection
4003 * @param function function to call on dispatch status changes
4004 * @param data data for function
4005 * @param free_data_function free the function data
4008 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
4009 DBusDispatchStatusFunction function,
4011 DBusFreeFunction free_data_function)
4014 DBusFreeFunction old_free_data;
4016 _dbus_return_if_fail (connection != NULL);
4018 CONNECTION_LOCK (connection);
4019 old_data = connection->dispatch_status_data;
4020 old_free_data = connection->free_dispatch_status_data;
4022 connection->dispatch_status_function = function;
4023 connection->dispatch_status_data = data;
4024 connection->free_dispatch_status_data = free_data_function;
4026 CONNECTION_UNLOCK (connection);
4028 /* Callback outside the lock */
4030 (*old_free_data) (old_data);
4034 * Get the UNIX file descriptor of the connection, if any. This can
4035 * be used for SELinux access control checks with getpeercon() for
4036 * example. DO NOT read or write to the file descriptor, or try to
4037 * select() on it; use DBusWatch for main loop integration. Not all
4038 * connections will have a file descriptor. So for adding descriptors
4039 * to the main loop, use dbus_watch_get_fd() and so forth.
4041 * @param connection the connection
4042 * @param fd return location for the file descriptor.
4043 * @returns #TRUE if fd is successfully obtained.
4046 dbus_connection_get_unix_fd (DBusConnection *connection,
4051 _dbus_return_val_if_fail (connection != NULL, FALSE);
4052 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
4054 CONNECTION_LOCK (connection);
4056 retval = _dbus_transport_get_unix_fd (connection->transport,
4059 CONNECTION_UNLOCK (connection);
4065 * Gets the UNIX user ID of the connection if any.
4066 * Returns #TRUE if the uid is filled in.
4067 * Always returns #FALSE on non-UNIX platforms.
4068 * Always returns #FALSE prior to authenticating the
4071 * @param connection the connection
4072 * @param uid return location for the user ID
4073 * @returns #TRUE if uid is filled in with a valid user ID
4076 dbus_connection_get_unix_user (DBusConnection *connection,
4081 _dbus_return_val_if_fail (connection != NULL, FALSE);
4082 _dbus_return_val_if_fail (uid != NULL, FALSE);
4084 CONNECTION_LOCK (connection);
4086 if (!_dbus_transport_get_is_authenticated (connection->transport))
4089 result = _dbus_transport_get_unix_user (connection->transport,
4091 CONNECTION_UNLOCK (connection);
4097 * Gets the process ID of the connection if any.
4098 * Returns #TRUE if the uid is filled in.
4099 * Always returns #FALSE prior to authenticating the
4102 * @param connection the connection
4103 * @param pid return location for the process ID
4104 * @returns #TRUE if uid is filled in with a valid process ID
4107 dbus_connection_get_unix_process_id (DBusConnection *connection,
4112 _dbus_return_val_if_fail (connection != NULL, FALSE);
4113 _dbus_return_val_if_fail (pid != NULL, FALSE);
4115 CONNECTION_LOCK (connection);
4117 if (!_dbus_transport_get_is_authenticated (connection->transport))
4120 result = _dbus_transport_get_unix_process_id (connection->transport,
4122 CONNECTION_UNLOCK (connection);
4128 * Sets a predicate function used to determine whether a given user ID
4129 * is allowed to connect. When an incoming connection has
4130 * authenticated with a particular user ID, this function is called;
4131 * if it returns #TRUE, the connection is allowed to proceed,
4132 * otherwise the connection is disconnected.
4134 * If the function is set to #NULL (as it is by default), then
4135 * only the same UID as the server process will be allowed to
4138 * @param connection the connection
4139 * @param function the predicate
4140 * @param data data to pass to the predicate
4141 * @param free_data_function function to free the data
4144 dbus_connection_set_unix_user_function (DBusConnection *connection,
4145 DBusAllowUnixUserFunction function,
4147 DBusFreeFunction free_data_function)
4149 void *old_data = NULL;
4150 DBusFreeFunction old_free_function = NULL;
4152 _dbus_return_if_fail (connection != NULL);
4154 CONNECTION_LOCK (connection);
4155 _dbus_transport_set_unix_user_function (connection->transport,
4156 function, data, free_data_function,
4157 &old_data, &old_free_function);
4158 CONNECTION_UNLOCK (connection);
4160 if (old_free_function != NULL)
4161 (* old_free_function) (old_data);
4165 * Adds a message filter. Filters are handlers that are run on all
4166 * incoming messages, prior to the objects registered with
4167 * dbus_connection_register_object_path(). Filters are run in the
4168 * order that they were added. The same handler can be added as a
4169 * filter more than once, in which case it will be run more than once.
4170 * Filters added during a filter callback won't be run on the message
4173 * @todo we don't run filters on messages while blocking without
4174 * entering the main loop, since filters are run as part of
4175 * dbus_connection_dispatch(). This is probably a feature, as filters
4176 * could create arbitrary reentrancy. But kind of sucks if you're
4177 * trying to filter METHOD_RETURN for some reason.
4179 * @param connection the connection
4180 * @param function function to handle messages
4181 * @param user_data user data to pass to the function
4182 * @param free_data_function function to use for freeing user data
4183 * @returns #TRUE on success, #FALSE if not enough memory.
4186 dbus_connection_add_filter (DBusConnection *connection,
4187 DBusHandleMessageFunction function,
4189 DBusFreeFunction free_data_function)
4191 DBusMessageFilter *filter;
4193 _dbus_return_val_if_fail (connection != NULL, FALSE);
4194 _dbus_return_val_if_fail (function != NULL, FALSE);
4196 filter = dbus_new0 (DBusMessageFilter, 1);
4200 filter->refcount.value = 1;
4202 CONNECTION_LOCK (connection);
4204 if (!_dbus_list_append (&connection->filter_list,
4207 _dbus_message_filter_unref (filter);
4208 CONNECTION_UNLOCK (connection);
4212 /* Fill in filter after all memory allocated,
4213 * so we don't run the free_user_data_function
4214 * if the add_filter() fails
4217 filter->function = function;
4218 filter->user_data = user_data;
4219 filter->free_user_data_function = free_data_function;
4221 CONNECTION_UNLOCK (connection);
4226 * Removes a previously-added message filter. It is a programming
4227 * error to call this function for a handler that has not been added
4228 * as a filter. If the given handler was added more than once, only
4229 * one instance of it will be removed (the most recently-added
4232 * @param connection the connection
4233 * @param function the handler to remove
4234 * @param user_data user data for the handler to remove
4238 dbus_connection_remove_filter (DBusConnection *connection,
4239 DBusHandleMessageFunction function,
4243 DBusMessageFilter *filter;
4245 _dbus_return_if_fail (connection != NULL);
4246 _dbus_return_if_fail (function != NULL);
4248 CONNECTION_LOCK (connection);
4252 link = _dbus_list_get_last_link (&connection->filter_list);
4253 while (link != NULL)
4255 filter = link->data;
4257 if (filter->function == function &&
4258 filter->user_data == user_data)
4260 _dbus_list_remove_link (&connection->filter_list, link);
4261 filter->function = NULL;
4266 link = _dbus_list_get_prev_link (&connection->filter_list, link);
4269 CONNECTION_UNLOCK (connection);
4271 #ifndef DBUS_DISABLE_CHECKS
4274 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
4275 function, user_data);
4280 /* Call application code */
4281 if (filter->free_user_data_function)
4282 (* filter->free_user_data_function) (filter->user_data);
4284 filter->free_user_data_function = NULL;
4285 filter->user_data = NULL;
4287 _dbus_message_filter_unref (filter);
4291 * Registers a handler for a given path in the object hierarchy.
4292 * The given vtable handles messages sent to exactly the given path.
4295 * @param connection the connection
4296 * @param path a '/' delimited string of path elements
4297 * @param vtable the virtual table
4298 * @param user_data data to pass to functions in the vtable
4299 * @returns #FALSE if not enough memory
4302 dbus_connection_register_object_path (DBusConnection *connection,
4304 const DBusObjectPathVTable *vtable,
4307 char **decomposed_path;
4310 _dbus_return_val_if_fail (connection != NULL, FALSE);
4311 _dbus_return_val_if_fail (path != NULL, FALSE);
4312 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4313 _dbus_return_val_if_fail (vtable != NULL, FALSE);
4315 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4318 CONNECTION_LOCK (connection);
4320 retval = _dbus_object_tree_register (connection->objects,
4322 (const char **) decomposed_path, vtable,
4325 CONNECTION_UNLOCK (connection);
4327 dbus_free_string_array (decomposed_path);
4333 * Registers a fallback handler for a given subsection of the object
4334 * hierarchy. The given vtable handles messages at or below the given
4335 * path. You can use this to establish a default message handling
4336 * policy for a whole "subdirectory."
4338 * @param connection the connection
4339 * @param path a '/' delimited string of path elements
4340 * @param vtable the virtual table
4341 * @param user_data data to pass to functions in the vtable
4342 * @returns #FALSE if not enough memory
4345 dbus_connection_register_fallback (DBusConnection *connection,
4347 const DBusObjectPathVTable *vtable,
4350 char **decomposed_path;
4353 _dbus_return_val_if_fail (connection != NULL, FALSE);
4354 _dbus_return_val_if_fail (path != NULL, FALSE);
4355 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4356 _dbus_return_val_if_fail (vtable != NULL, FALSE);
4358 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4361 CONNECTION_LOCK (connection);
4363 retval = _dbus_object_tree_register (connection->objects,
4365 (const char **) decomposed_path, vtable,
4368 CONNECTION_UNLOCK (connection);
4370 dbus_free_string_array (decomposed_path);
4376 * Unregisters the handler registered with exactly the given path.
4377 * It's a bug to call this function for a path that isn't registered.
4378 * Can unregister both fallback paths and object paths.
4380 * @param connection the connection
4381 * @param path a '/' delimited string of path elements
4382 * @returns #FALSE if not enough memory
4385 dbus_connection_unregister_object_path (DBusConnection *connection,
4388 char **decomposed_path;
4390 _dbus_return_val_if_fail (connection != NULL, FALSE);
4391 _dbus_return_val_if_fail (path != NULL, FALSE);
4392 _dbus_return_val_if_fail (path[0] == '/', FALSE);
4394 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4397 CONNECTION_LOCK (connection);
4399 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
4401 dbus_free_string_array (decomposed_path);
4407 * Gets the user data passed to dbus_connection_register_object_path()
4408 * or dbus_connection_register_fallback(). If nothing was registered
4409 * at this path, the data is filled in with #NULL.
4411 * @param connection the connection
4412 * @param path the path you registered with
4413 * @param data_p location to store the user data, or #NULL
4414 * @returns #FALSE if not enough memory
4417 dbus_connection_get_object_path_data (DBusConnection *connection,
4421 char **decomposed_path;
4423 _dbus_return_val_if_fail (connection != NULL, FALSE);
4424 _dbus_return_val_if_fail (path != NULL, FALSE);
4425 _dbus_return_val_if_fail (data_p != NULL, FALSE);
4429 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
4432 CONNECTION_LOCK (connection);
4434 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
4436 CONNECTION_UNLOCK (connection);
4438 dbus_free_string_array (decomposed_path);
4444 * Lists the registered fallback handlers and object path handlers at
4445 * the given parent_path. The returned array should be freed with
4446 * dbus_free_string_array().
4448 * @param connection the connection
4449 * @param parent_path the path to list the child handlers of
4450 * @param child_entries returns #NULL-terminated array of children
4451 * @returns #FALSE if no memory to allocate the child entries
4454 dbus_connection_list_registered (DBusConnection *connection,
4455 const char *parent_path,
4456 char ***child_entries)
4458 char **decomposed_path;
4460 _dbus_return_val_if_fail (connection != NULL, FALSE);
4461 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
4462 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
4463 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
4465 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
4468 CONNECTION_LOCK (connection);
4470 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
4471 (const char **) decomposed_path,
4473 dbus_free_string_array (decomposed_path);
4478 static DBusDataSlotAllocator slot_allocator;
4479 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
4482 * Allocates an integer ID to be used for storing application-specific
4483 * data on any DBusConnection. The allocated ID may then be used
4484 * with dbus_connection_set_data() and dbus_connection_get_data().
4485 * The passed-in slot must be initialized to -1, and is filled in
4486 * with the slot ID. If the passed-in slot is not -1, it's assumed
4487 * to be already allocated, and its refcount is incremented.
4489 * The allocated slot is global, i.e. all DBusConnection objects will
4490 * have a slot with the given integer ID reserved.
4492 * @param slot_p address of a global variable storing the slot
4493 * @returns #FALSE on failure (no memory)
4496 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
4498 return _dbus_data_slot_allocator_alloc (&slot_allocator,
4499 _DBUS_LOCK_NAME (connection_slots),
4504 * Deallocates a global ID for connection data slots.
4505 * dbus_connection_get_data() and dbus_connection_set_data() may no
4506 * longer be used with this slot. Existing data stored on existing
4507 * DBusConnection objects will be freed when the connection is
4508 * finalized, but may not be retrieved (and may only be replaced if
4509 * someone else reallocates the slot). When the refcount on the
4510 * passed-in slot reaches 0, it is set to -1.
4512 * @param slot_p address storing the slot to deallocate
4515 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
4517 _dbus_return_if_fail (*slot_p >= 0);
4519 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
4523 * Stores a pointer on a DBusConnection, along
4524 * with an optional function to be used for freeing
4525 * the data when the data is set again, or when
4526 * the connection is finalized. The slot number
4527 * must have been allocated with dbus_connection_allocate_data_slot().
4529 * @param connection the connection
4530 * @param slot the slot number
4531 * @param data the data to store
4532 * @param free_data_func finalizer function for the data
4533 * @returns #TRUE if there was enough memory to store the data
4536 dbus_connection_set_data (DBusConnection *connection,
4539 DBusFreeFunction free_data_func)
4541 DBusFreeFunction old_free_func;
4545 _dbus_return_val_if_fail (connection != NULL, FALSE);
4546 _dbus_return_val_if_fail (slot >= 0, FALSE);
4548 CONNECTION_LOCK (connection);
4550 retval = _dbus_data_slot_list_set (&slot_allocator,
4551 &connection->slot_list,
4552 slot, data, free_data_func,
4553 &old_free_func, &old_data);
4555 CONNECTION_UNLOCK (connection);
4559 /* Do the actual free outside the connection lock */
4561 (* old_free_func) (old_data);
4568 * Retrieves data previously set with dbus_connection_set_data().
4569 * The slot must still be allocated (must not have been freed).
4571 * @param connection the connection
4572 * @param slot the slot to get data from
4573 * @returns the data, or #NULL if not found
4576 dbus_connection_get_data (DBusConnection *connection,
4581 _dbus_return_val_if_fail (connection != NULL, NULL);
4583 CONNECTION_LOCK (connection);
4585 res = _dbus_data_slot_list_get (&slot_allocator,
4586 &connection->slot_list,
4589 CONNECTION_UNLOCK (connection);
4595 * This function sets a global flag for whether dbus_connection_new()
4596 * will set SIGPIPE behavior to SIG_IGN.
4598 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
4601 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
4603 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
4607 * Specifies the maximum size message this connection is allowed to
4608 * receive. Larger messages will result in disconnecting the
4611 * @param connection a #DBusConnection
4612 * @param size maximum message size the connection can receive, in bytes
4615 dbus_connection_set_max_message_size (DBusConnection *connection,
4618 _dbus_return_if_fail (connection != NULL);
4620 CONNECTION_LOCK (connection);
4621 _dbus_transport_set_max_message_size (connection->transport,
4623 CONNECTION_UNLOCK (connection);
4627 * Gets the value set by dbus_connection_set_max_message_size().
4629 * @param connection the connection
4630 * @returns the max size of a single message
4633 dbus_connection_get_max_message_size (DBusConnection *connection)
4637 _dbus_return_val_if_fail (connection != NULL, 0);
4639 CONNECTION_LOCK (connection);
4640 res = _dbus_transport_get_max_message_size (connection->transport);
4641 CONNECTION_UNLOCK (connection);
4646 * Sets the maximum total number of bytes that can be used for all messages
4647 * received on this connection. Messages count toward the maximum until
4648 * they are finalized. When the maximum is reached, the connection will
4649 * not read more data until some messages are finalized.
4651 * The semantics of the maximum are: if outstanding messages are
4652 * already above the maximum, additional messages will not be read.
4653 * The semantics are not: if the next message would cause us to exceed
4654 * the maximum, we don't read it. The reason is that we don't know the
4655 * size of a message until after we read it.
4657 * Thus, the max live messages size can actually be exceeded
4658 * by up to the maximum size of a single message.
4660 * Also, if we read say 1024 bytes off the wire in a single read(),
4661 * and that contains a half-dozen small messages, we may exceed the
4662 * size max by that amount. But this should be inconsequential.
4664 * This does imply that we can't call read() with a buffer larger
4665 * than we're willing to exceed this limit by.
4667 * @param connection the connection
4668 * @param size the maximum size in bytes of all outstanding messages
4671 dbus_connection_set_max_received_size (DBusConnection *connection,
4674 _dbus_return_if_fail (connection != NULL);
4676 CONNECTION_LOCK (connection);
4677 _dbus_transport_set_max_received_size (connection->transport,
4679 CONNECTION_UNLOCK (connection);
4683 * Gets the value set by dbus_connection_set_max_received_size().
4685 * @param connection the connection
4686 * @returns the max size of all live messages
4689 dbus_connection_get_max_received_size (DBusConnection *connection)
4693 _dbus_return_val_if_fail (connection != NULL, 0);
4695 CONNECTION_LOCK (connection);
4696 res = _dbus_transport_get_max_received_size (connection->transport);
4697 CONNECTION_UNLOCK (connection);
4702 * Gets the approximate size in bytes of all messages in the outgoing
4703 * message queue. The size is approximate in that you shouldn't use
4704 * it to decide how many bytes to read off the network or anything
4705 * of that nature, as optimizations may choose to tell small white lies
4706 * to avoid performance overhead.
4708 * @param connection the connection
4709 * @returns the number of bytes that have been queued up but not sent
4712 dbus_connection_get_outgoing_size (DBusConnection *connection)
4716 _dbus_return_val_if_fail (connection != NULL, 0);
4718 CONNECTION_LOCK (connection);
4719 res = _dbus_counter_get_value (connection->outgoing_counter);
4720 CONNECTION_UNLOCK (connection);