1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002, 2003, 2004 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-list.h"
33 #include "dbus-hash.h"
34 #include "dbus-message-internal.h"
35 #include "dbus-threads.h"
36 #include "dbus-protocol.h"
37 #include "dbus-dataslot.h"
38 #include "dbus-string.h"
39 #include "dbus-pending-call.h"
40 #include "dbus-object-tree.h"
43 #define CONNECTION_LOCK(connection) do { \
44 _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); \
45 dbus_mutex_lock ((connection)->mutex); \
47 #define CONNECTION_UNLOCK(connection) do { \
48 _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); \
49 dbus_mutex_unlock ((connection)->mutex); \
52 #define CONNECTION_LOCK(connection) dbus_mutex_lock ((connection)->mutex)
53 #define CONNECTION_UNLOCK(connection) dbus_mutex_unlock ((connection)->mutex)
56 #define DISPATCH_STATUS_NAME(s) \
57 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
58 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
59 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
63 * @defgroup DBusConnection DBusConnection
65 * @brief Connection to another application
67 * A DBusConnection represents a connection to another
68 * application. Messages can be sent and received via this connection.
69 * The other application may be a message bus; for convenience, the
70 * function dbus_bus_get() is provided to automatically open a
71 * connection to the well-known message buses.
73 * In brief a DBusConnection is a message queue associated with some
74 * message transport mechanism such as a socket. The connection
75 * maintains a queue of incoming messages and a queue of outgoing
78 * Incoming messages are normally processed by calling
79 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
80 * handlers registered for the topmost message in the message queue,
81 * then discards the message, then returns.
83 * dbus_connection_get_dispatch_status() indicates whether
84 * messages are currently in the queue that need dispatching.
85 * dbus_connection_set_dispatch_status_function() allows
86 * you to set a function to be used to monitor the dispatch status.
88 * If you're using GLib or Qt add-on libraries for D-BUS, there are
89 * special convenience APIs in those libraries that hide
90 * all the details of dispatch and watch/timeout monitoring.
91 * For example, dbus_connection_setup_with_g_main().
93 * If you aren't using these add-on libraries, you have to manually
94 * call dbus_connection_set_dispatch_status_function(),
95 * dbus_connection_set_watch_functions(),
96 * dbus_connection_set_timeout_functions() providing appropriate
97 * functions to integrate the connection with your application's main
100 * When you use dbus_connection_send() or one of its variants to send
101 * a message, the message is added to the outgoing queue. It's
102 * actually written to the network later; either in
103 * dbus_watch_handle() invoked by your main loop, or in
104 * dbus_connection_flush() which blocks until it can write out the
105 * entire outgoing queue. The GLib/Qt add-on libraries again
106 * handle the details here for you by setting up watch functions.
108 * When a connection is disconnected, you are guaranteed to get a
109 * signal "Disconnected" from the interface
110 * #DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, path
111 * #DBUS_PATH_ORG_FREEDESKTOP_LOCAL.
113 * You may not drop the last reference to a #DBusConnection
114 * until that connection has been disconnected.
116 * You may dispatch the unprocessed incoming message queue even if the
117 * connection is disconnected. However, "Disconnected" will always be
118 * the last message in the queue (obviously no messages are received
119 * after disconnection).
121 * #DBusConnection has thread locks and drops them when invoking user
122 * callbacks, so in general is transparently threadsafe. However,
123 * #DBusMessage does NOT have thread locks; you must not send the same
124 * message to multiple #DBusConnection that will be used from
129 * @defgroup DBusConnectionInternals DBusConnection implementation details
130 * @ingroup DBusInternals
131 * @brief Implementation details of DBusConnection
137 * Internal struct representing a message filter function
139 typedef struct DBusMessageFilter DBusMessageFilter;
142 * Internal struct representing a message filter function
144 struct DBusMessageFilter
146 DBusAtomic refcount; /**< Reference count */
147 DBusHandleMessageFunction function; /**< Function to call to filter */
148 void *user_data; /**< User data for the function */
149 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
154 * Internals of DBusPreallocatedSend
156 struct DBusPreallocatedSend
158 DBusConnection *connection; /**< Connection we'd send the message to */
159 DBusList *queue_link; /**< Preallocated link in the queue */
160 DBusList *counter_link; /**< Preallocated link in the resource counter */
163 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
166 * Implementation details of DBusConnection. All fields are private.
168 struct DBusConnection
170 DBusAtomic refcount; /**< Reference count. */
172 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
174 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */
175 DBusCondVar *dispatch_cond; /**< Protects dispatch() */
177 dbus_bool_t io_path_acquired; /**< Protects transport io path */
178 DBusCondVar *io_path_cond; /**< Protects transport io path */
180 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
181 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
183 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
184 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
186 int n_outgoing; /**< Length of outgoing queue. */
187 int n_incoming; /**< Length of incoming queue. */
189 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
191 DBusTransport *transport; /**< Object that sends/receives messages over network. */
192 DBusWatchList *watches; /**< Stores active watches. */
193 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
195 DBusList *filter_list; /**< List of filters. */
197 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
199 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
201 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
202 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
204 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
205 void *wakeup_main_data; /**< Application data for wakeup_main_function */
206 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
208 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
209 void *dispatch_status_data; /**< Application data for dispatch_status_function */
210 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
212 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
214 DBusList *link_cache; /**< A cache of linked list links to prevent contention
215 * for the global linked list mempool lock
217 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
219 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
221 #ifndef DBUS_DISABLE_CHECKS
222 int generation; /**< _dbus_current_generation that should correspond to this connection */
226 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
227 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
228 DBusDispatchStatus new_status);
229 static void _dbus_connection_last_unref (DBusConnection *connection);
231 static DBusMessageFilter *
232 _dbus_message_filter_ref (DBusMessageFilter *filter)
234 _dbus_assert (filter->refcount.value > 0);
235 _dbus_atomic_inc (&filter->refcount);
241 _dbus_message_filter_unref (DBusMessageFilter *filter)
243 _dbus_assert (filter->refcount.value > 0);
245 if (_dbus_atomic_dec (&filter->refcount) == 1)
247 if (filter->free_user_data_function)
248 (* filter->free_user_data_function) (filter->user_data);
255 * Acquires the connection lock.
257 * @param connection the connection.
260 _dbus_connection_lock (DBusConnection *connection)
262 CONNECTION_LOCK (connection);
266 * Releases the connection lock.
268 * @param connection the connection.
271 _dbus_connection_unlock (DBusConnection *connection)
273 CONNECTION_UNLOCK (connection);
277 * Wakes up the main loop if it is sleeping
278 * Needed if we're e.g. queueing outgoing messages
279 * on a thread while the mainloop sleeps.
281 * @param connection the connection.
284 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
286 if (connection->wakeup_main_function)
287 (*connection->wakeup_main_function) (connection->wakeup_main_data);
290 #ifdef DBUS_BUILD_TESTS
291 /* For now this function isn't used */
293 * Adds a message to the incoming message queue, returning #FALSE
294 * if there's insufficient memory to queue the message.
295 * Does not take over refcount of the message.
297 * @param connection the connection.
298 * @param message the message to queue.
299 * @returns #TRUE on success.
302 _dbus_connection_queue_received_message (DBusConnection *connection,
303 DBusMessage *message)
307 link = _dbus_list_alloc_link (message);
311 dbus_message_ref (message);
312 _dbus_connection_queue_received_message_link (connection, link);
319 * Adds a message-containing list link to the incoming message queue,
320 * taking ownership of the link and the message's current refcount.
321 * Cannot fail due to lack of memory.
323 * @param connection the connection.
324 * @param link the message link to queue.
327 _dbus_connection_queue_received_message_link (DBusConnection *connection,
330 DBusPendingCall *pending;
331 dbus_int32_t reply_serial;
332 DBusMessage *message;
334 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
336 _dbus_list_append_link (&connection->incoming_messages,
338 message = link->data;
340 /* If this is a reply we're waiting on, remove timeout for it */
341 reply_serial = dbus_message_get_reply_serial (message);
342 if (reply_serial != -1)
344 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
348 if (pending->timeout_added)
349 _dbus_connection_remove_timeout (connection,
352 pending->timeout_added = FALSE;
356 connection->n_incoming += 1;
358 _dbus_connection_wakeup_mainloop (connection);
360 _dbus_verbose ("Message %p (%d %s %s '%s') added to incoming queue %p, %d incoming\n",
362 dbus_message_get_type (message),
363 dbus_message_get_interface (message) ?
364 dbus_message_get_interface (message) :
366 dbus_message_get_member (message) ?
367 dbus_message_get_member (message) :
369 dbus_message_get_signature (message),
371 connection->n_incoming);
375 * Adds a link + message to the incoming message queue.
376 * Can't fail. Takes ownership of both link and message.
378 * @param connection the connection.
379 * @param link the list node and message to queue.
381 * @todo This needs to wake up the mainloop if it is in
382 * a poll/select and this is a multithreaded app.
385 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
388 _dbus_list_append_link (&connection->incoming_messages, link);
390 connection->n_incoming += 1;
392 _dbus_connection_wakeup_mainloop (connection);
394 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
395 link->data, connection, connection->n_incoming);
400 * Checks whether there are messages in the outgoing message queue.
401 * Called with connection lock held.
403 * @param connection the connection.
404 * @returns #TRUE if the outgoing queue is non-empty.
407 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
409 return connection->outgoing_messages != NULL;
413 * Checks whether there are messages in the outgoing message queue.
415 * @param connection the connection.
416 * @returns #TRUE if the outgoing queue is non-empty.
419 dbus_connection_has_messages_to_send (DBusConnection *connection)
423 _dbus_return_val_if_fail (connection != NULL, FALSE);
425 CONNECTION_LOCK (connection);
426 v = _dbus_connection_has_messages_to_send_unlocked (connection);
427 CONNECTION_UNLOCK (connection);
433 * Gets the next outgoing message. The message remains in the
434 * queue, and the caller does not own a reference to it.
436 * @param connection the connection.
437 * @returns the message to be sent.
440 _dbus_connection_get_message_to_send (DBusConnection *connection)
442 return _dbus_list_get_last (&connection->outgoing_messages);
446 * Notifies the connection that a message has been sent, so the
447 * message can be removed from the outgoing queue.
448 * Called with the connection lock held.
450 * @param connection the connection.
451 * @param message the message that was sent.
454 _dbus_connection_message_sent (DBusConnection *connection,
455 DBusMessage *message)
459 /* This can be called before we even complete authentication, since
460 * it's called on disconnect to clean up the outgoing queue.
461 * It's also called as we successfully send each message.
464 link = _dbus_list_get_last_link (&connection->outgoing_messages);
465 _dbus_assert (link != NULL);
466 _dbus_assert (link->data == message);
468 /* Save this link in the link cache */
469 _dbus_list_unlink (&connection->outgoing_messages,
471 _dbus_list_prepend_link (&connection->link_cache, link);
473 connection->n_outgoing -= 1;
475 _dbus_verbose ("Message %p (%d %s %s '%s') removed from outgoing queue %p, %d left to send\n",
477 dbus_message_get_type (message),
478 dbus_message_get_interface (message) ?
479 dbus_message_get_interface (message) :
481 dbus_message_get_member (message) ?
482 dbus_message_get_member (message) :
484 dbus_message_get_signature (message),
485 connection, connection->n_outgoing);
487 /* Save this link in the link cache also */
488 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
490 _dbus_list_prepend_link (&connection->link_cache, link);
492 dbus_message_unref (message);
496 * Adds a watch using the connection's DBusAddWatchFunction if
497 * available. Otherwise records the watch to be added when said
498 * function is available. Also re-adds the watch if the
499 * DBusAddWatchFunction changes. May fail due to lack of memory.
501 * @param connection the connection.
502 * @param watch the watch to add.
503 * @returns #TRUE on success.
506 _dbus_connection_add_watch (DBusConnection *connection,
509 if (connection->watches) /* null during finalize */
510 return _dbus_watch_list_add_watch (connection->watches,
517 * Removes a watch using the connection's DBusRemoveWatchFunction
518 * if available. It's an error to call this function on a watch
519 * that was not previously added.
521 * @param connection the connection.
522 * @param watch the watch to remove.
525 _dbus_connection_remove_watch (DBusConnection *connection,
528 if (connection->watches) /* null during finalize */
529 _dbus_watch_list_remove_watch (connection->watches,
534 * Toggles a watch and notifies app via connection's
535 * DBusWatchToggledFunction if available. It's an error to call this
536 * function on a watch that was not previously added.
537 * Connection lock should be held when calling this.
539 * @param connection the connection.
540 * @param watch the watch to toggle.
541 * @param enabled whether to enable or disable
544 _dbus_connection_toggle_watch (DBusConnection *connection,
548 _dbus_assert (watch != NULL);
550 if (connection->watches) /* null during finalize */
551 _dbus_watch_list_toggle_watch (connection->watches,
556 * Adds a timeout using the connection's DBusAddTimeoutFunction if
557 * available. Otherwise records the timeout to be added when said
558 * function is available. Also re-adds the timeout if the
559 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
560 * The timeout will fire repeatedly until removed.
562 * @param connection the connection.
563 * @param timeout the timeout to add.
564 * @returns #TRUE on success.
567 _dbus_connection_add_timeout (DBusConnection *connection,
568 DBusTimeout *timeout)
570 if (connection->timeouts) /* null during finalize */
571 return _dbus_timeout_list_add_timeout (connection->timeouts,
578 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
579 * if available. It's an error to call this function on a timeout
580 * that was not previously added.
582 * @param connection the connection.
583 * @param timeout the timeout to remove.
586 _dbus_connection_remove_timeout (DBusConnection *connection,
587 DBusTimeout *timeout)
589 if (connection->timeouts) /* null during finalize */
590 _dbus_timeout_list_remove_timeout (connection->timeouts,
595 * Toggles a timeout and notifies app via connection's
596 * DBusTimeoutToggledFunction if available. It's an error to call this
597 * function on a timeout that was not previously added.
599 * @param connection the connection.
600 * @param timeout the timeout to toggle.
601 * @param enabled whether to enable or disable
604 _dbus_connection_toggle_timeout (DBusConnection *connection,
605 DBusTimeout *timeout,
608 if (connection->timeouts) /* null during finalize */
609 _dbus_timeout_list_toggle_timeout (connection->timeouts,
614 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
615 DBusPendingCall *pending)
617 _dbus_assert (pending->reply_serial != 0);
619 if (!_dbus_connection_add_timeout (connection, pending->timeout))
622 if (!_dbus_hash_table_insert_int (connection->pending_replies,
623 pending->reply_serial,
626 _dbus_connection_remove_timeout (connection, pending->timeout);
630 pending->timeout_added = TRUE;
631 pending->connection = connection;
633 dbus_pending_call_ref (pending);
639 free_pending_call_on_hash_removal (void *data)
641 DBusPendingCall *pending;
648 if (pending->connection)
650 if (pending->timeout_added)
652 _dbus_connection_remove_timeout (pending->connection,
654 pending->timeout_added = FALSE;
657 pending->connection = NULL;
659 dbus_pending_call_unref (pending);
664 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
665 DBusPendingCall *pending)
667 /* The idea here is to avoid finalizing the pending call
668 * with the lock held, since there's a destroy notifier
669 * in pending call that goes out to application code.
671 dbus_pending_call_ref (pending);
672 _dbus_hash_table_remove_int (connection->pending_replies,
673 pending->reply_serial);
674 CONNECTION_UNLOCK (connection);
675 dbus_pending_call_unref (pending);
679 * Removes a pending call from the connection, such that
680 * the pending reply will be ignored. May drop the last
681 * reference to the pending call.
683 * @param connection the connection
684 * @param pending the pending call
687 _dbus_connection_remove_pending_call (DBusConnection *connection,
688 DBusPendingCall *pending)
690 CONNECTION_LOCK (connection);
691 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
695 * Completes a pending call with the given message,
696 * or if the message is #NULL, by timing out the pending call.
698 * @param pending the pending call
699 * @param message the message to complete the call with, or #NULL
700 * to time out the call
703 _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
704 DBusMessage *message)
708 message = pending->timeout_link->data;
709 _dbus_list_clear (&pending->timeout_link);
712 dbus_message_ref (message);
714 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
716 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
718 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
719 "error" : "other type",
720 pending->reply_serial);
722 _dbus_assert (pending->reply == NULL);
723 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
724 pending->reply = message;
726 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
727 _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending);
729 /* Must be called unlocked since it invokes app callback */
730 _dbus_pending_call_notify (pending);
731 dbus_pending_call_unref (pending);
735 * Acquire the transporter I/O path. This must be done before
736 * doing any I/O in the transporter. May sleep and drop the
737 * connection mutex while waiting for the I/O path.
739 * @param connection the connection.
740 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
741 * @returns TRUE if the I/O path was acquired.
744 _dbus_connection_acquire_io_path (DBusConnection *connection,
745 int timeout_milliseconds)
747 dbus_bool_t res = TRUE;
749 if (connection->io_path_acquired)
751 if (timeout_milliseconds != -1)
752 res = dbus_condvar_wait_timeout (connection->io_path_cond,
754 timeout_milliseconds);
756 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
761 _dbus_assert (!connection->io_path_acquired);
763 connection->io_path_acquired = TRUE;
770 * Release the I/O path when you're done with it. Only call
771 * after you've acquired the I/O. Wakes up at most one thread
772 * currently waiting to acquire the I/O path.
774 * @param connection the connection.
777 _dbus_connection_release_io_path (DBusConnection *connection)
779 _dbus_assert (connection->io_path_acquired);
781 connection->io_path_acquired = FALSE;
782 dbus_condvar_wake_one (connection->io_path_cond);
787 * Queues incoming messages and sends outgoing messages for this
788 * connection, optionally blocking in the process. Each call to
789 * _dbus_connection_do_iteration() will call select() or poll() one
790 * time and then read or write data if possible.
792 * The purpose of this function is to be able to flush outgoing
793 * messages or queue up incoming messages without returning
794 * control to the application and causing reentrancy weirdness.
796 * The flags parameter allows you to specify whether to
797 * read incoming messages, write outgoing messages, or both,
798 * and whether to block if no immediate action is possible.
800 * The timeout_milliseconds parameter does nothing unless the
801 * iteration is blocking.
803 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
804 * wasn't specified, then it's impossible to block, even if
805 * you specify DBUS_ITERATION_BLOCK; in that case the function
806 * returns immediately.
808 * Called with connection lock held.
810 * @param connection the connection.
811 * @param flags iteration flags.
812 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
815 _dbus_connection_do_iteration (DBusConnection *connection,
817 int timeout_milliseconds)
819 if (connection->n_outgoing == 0)
820 flags &= ~DBUS_ITERATION_DO_WRITING;
822 if (_dbus_connection_acquire_io_path (connection,
823 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
825 _dbus_transport_do_iteration (connection->transport,
826 flags, timeout_milliseconds);
827 _dbus_connection_release_io_path (connection);
832 * Creates a new connection for the given transport. A transport
833 * represents a message stream that uses some concrete mechanism, such
834 * as UNIX domain sockets. May return #NULL if insufficient
835 * memory exists to create the connection.
837 * @param transport the transport.
838 * @returns the new connection, or #NULL on failure.
841 _dbus_connection_new_for_transport (DBusTransport *transport)
843 DBusConnection *connection;
844 DBusWatchList *watch_list;
845 DBusTimeoutList *timeout_list;
846 DBusHashTable *pending_replies;
848 DBusCondVar *message_returned_cond;
849 DBusCondVar *dispatch_cond;
850 DBusCondVar *io_path_cond;
851 DBusList *disconnect_link;
852 DBusMessage *disconnect_message;
853 DBusCounter *outgoing_counter;
854 DBusObjectTree *objects;
858 pending_replies = NULL;
861 message_returned_cond = NULL;
862 dispatch_cond = NULL;
864 disconnect_link = NULL;
865 disconnect_message = NULL;
866 outgoing_counter = NULL;
869 watch_list = _dbus_watch_list_new ();
870 if (watch_list == NULL)
873 timeout_list = _dbus_timeout_list_new ();
874 if (timeout_list == NULL)
878 _dbus_hash_table_new (DBUS_HASH_INT,
880 (DBusFreeFunction)free_pending_call_on_hash_removal);
881 if (pending_replies == NULL)
884 connection = dbus_new0 (DBusConnection, 1);
885 if (connection == NULL)
888 mutex = dbus_mutex_new ();
892 message_returned_cond = dbus_condvar_new ();
893 if (message_returned_cond == NULL)
896 dispatch_cond = dbus_condvar_new ();
897 if (dispatch_cond == NULL)
900 io_path_cond = dbus_condvar_new ();
901 if (io_path_cond == NULL)
904 disconnect_message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_LOCAL,
905 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
908 if (disconnect_message == NULL)
911 disconnect_link = _dbus_list_alloc_link (disconnect_message);
912 if (disconnect_link == NULL)
915 outgoing_counter = _dbus_counter_new ();
916 if (outgoing_counter == NULL)
919 objects = _dbus_object_tree_new (connection);
923 if (_dbus_modify_sigpipe)
924 _dbus_disable_sigpipe ();
926 connection->refcount.value = 1;
927 connection->mutex = mutex;
928 connection->dispatch_cond = dispatch_cond;
929 connection->io_path_cond = io_path_cond;
930 connection->message_returned_cond = message_returned_cond;
931 connection->transport = transport;
932 connection->watches = watch_list;
933 connection->timeouts = timeout_list;
934 connection->pending_replies = pending_replies;
935 connection->outgoing_counter = outgoing_counter;
936 connection->filter_list = NULL;
937 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
938 connection->objects = objects;
939 connection->exit_on_disconnect = FALSE;
940 #ifndef DBUS_DISABLE_CHECKS
941 connection->generation = _dbus_current_generation;
944 _dbus_data_slot_list_init (&connection->slot_list);
946 connection->client_serial = 1;
948 connection->disconnect_message_link = disconnect_link;
950 if (!_dbus_transport_set_connection (transport, connection))
953 _dbus_transport_ref (transport);
958 if (disconnect_message != NULL)
959 dbus_message_unref (disconnect_message);
961 if (disconnect_link != NULL)
962 _dbus_list_free_link (disconnect_link);
964 if (io_path_cond != NULL)
965 dbus_condvar_free (io_path_cond);
967 if (dispatch_cond != NULL)
968 dbus_condvar_free (dispatch_cond);
970 if (message_returned_cond != NULL)
971 dbus_condvar_free (message_returned_cond);
974 dbus_mutex_free (mutex);
976 if (connection != NULL)
977 dbus_free (connection);
980 _dbus_hash_table_unref (pending_replies);
983 _dbus_watch_list_free (watch_list);
986 _dbus_timeout_list_free (timeout_list);
988 if (outgoing_counter)
989 _dbus_counter_unref (outgoing_counter);
992 _dbus_object_tree_unref (objects);
998 * Increments the reference count of a DBusConnection.
999 * Requires that the caller already holds the connection lock.
1001 * @param connection the connection.
1002 * @returns the connection.
1005 _dbus_connection_ref_unlocked (DBusConnection *connection)
1007 _dbus_assert (connection != NULL);
1008 _dbus_assert (connection->generation == _dbus_current_generation);
1010 #ifdef DBUS_HAVE_ATOMIC_INT
1011 _dbus_atomic_inc (&connection->refcount);
1013 _dbus_assert (connection->refcount.value > 0);
1014 connection->refcount.value += 1;
1021 * Decrements the reference count of a DBusConnection.
1022 * Requires that the caller already holds the connection lock.
1024 * @param connection the connection.
1027 _dbus_connection_unref_unlocked (DBusConnection *connection)
1029 dbus_bool_t last_unref;
1031 _dbus_return_if_fail (connection != NULL);
1033 /* The connection lock is better than the global
1034 * lock in the atomic increment fallback
1037 #ifdef DBUS_HAVE_ATOMIC_INT
1038 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1040 _dbus_assert (connection->refcount.value > 0);
1042 connection->refcount.value -= 1;
1043 last_unref = (connection->refcount.value == 0);
1045 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1050 _dbus_connection_last_unref (connection);
1053 static dbus_uint32_t
1054 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1058 serial = connection->client_serial++;
1060 if (connection->client_serial < 0)
1061 connection->client_serial = 1;
1067 * A callback for use with dbus_watch_new() to create a DBusWatch.
1069 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1070 * and the virtual handle_watch in DBusTransport if we got rid of it.
1071 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1074 * @param watch the watch.
1075 * @param condition the current condition of the file descriptors being watched.
1076 * @param data must be a pointer to a #DBusConnection
1077 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1080 _dbus_connection_handle_watch (DBusWatch *watch,
1081 unsigned int condition,
1084 DBusConnection *connection;
1086 DBusDispatchStatus status;
1090 CONNECTION_LOCK (connection);
1091 _dbus_connection_acquire_io_path (connection, -1);
1092 retval = _dbus_transport_handle_watch (connection->transport,
1094 _dbus_connection_release_io_path (connection);
1096 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1098 /* this calls out to user code */
1099 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1107 * @addtogroup DBusConnection
1113 * Opens a new connection to a remote address.
1115 * @todo specify what the address parameter is. Right now
1116 * it's just the name of a UNIX domain socket. It should be
1117 * something more complex that encodes which transport to use.
1119 * If the open fails, the function returns #NULL, and provides
1120 * a reason for the failure in the result parameter. Pass
1121 * #NULL for the result parameter if you aren't interested
1122 * in the reason for failure.
1124 * @param address the address.
1125 * @param error address where an error can be returned.
1126 * @returns new connection, or #NULL on failure.
1129 dbus_connection_open (const char *address,
1132 DBusConnection *connection;
1133 DBusTransport *transport;
1135 _dbus_return_val_if_fail (address != NULL, NULL);
1136 _dbus_return_val_if_error_is_set (error, NULL);
1138 transport = _dbus_transport_open (address, error);
1139 if (transport == NULL)
1141 _DBUS_ASSERT_ERROR_IS_SET (error);
1145 connection = _dbus_connection_new_for_transport (transport);
1147 _dbus_transport_unref (transport);
1149 if (connection == NULL)
1151 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1159 * Increments the reference count of a DBusConnection.
1161 * @param connection the connection.
1162 * @returns the connection.
1165 dbus_connection_ref (DBusConnection *connection)
1167 _dbus_return_val_if_fail (connection != NULL, NULL);
1168 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
1170 /* The connection lock is better than the global
1171 * lock in the atomic increment fallback
1174 #ifdef DBUS_HAVE_ATOMIC_INT
1175 _dbus_atomic_inc (&connection->refcount);
1177 CONNECTION_LOCK (connection);
1178 _dbus_assert (connection->refcount.value > 0);
1180 connection->refcount.value += 1;
1181 CONNECTION_UNLOCK (connection);
1188 free_outgoing_message (void *element,
1191 DBusMessage *message = element;
1192 DBusConnection *connection = data;
1194 _dbus_message_remove_size_counter (message,
1195 connection->outgoing_counter,
1197 dbus_message_unref (message);
1200 /* This is run without the mutex held, but after the last reference
1201 * to the connection has been dropped we should have no thread-related
1205 _dbus_connection_last_unref (DBusConnection *connection)
1209 _dbus_verbose ("Finalizing connection %p\n", connection);
1211 _dbus_assert (connection->refcount.value == 0);
1213 /* You have to disconnect the connection before unref:ing it. Otherwise
1214 * you won't get the disconnected message.
1216 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1218 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1219 _dbus_object_tree_free_all_unlocked (connection->objects);
1221 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1222 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1223 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1225 _dbus_watch_list_free (connection->watches);
1226 connection->watches = NULL;
1228 _dbus_timeout_list_free (connection->timeouts);
1229 connection->timeouts = NULL;
1231 _dbus_data_slot_list_free (&connection->slot_list);
1233 link = _dbus_list_get_first_link (&connection->filter_list);
1234 while (link != NULL)
1236 DBusMessageFilter *filter = link->data;
1237 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1239 filter->function = NULL;
1240 _dbus_message_filter_unref (filter); /* calls app callback */
1245 _dbus_list_clear (&connection->filter_list);
1247 /* ---- Done with stuff that invokes application callbacks */
1249 _dbus_object_tree_unref (connection->objects);
1251 _dbus_hash_table_unref (connection->pending_replies);
1252 connection->pending_replies = NULL;
1254 _dbus_list_clear (&connection->filter_list);
1256 _dbus_list_foreach (&connection->outgoing_messages,
1257 free_outgoing_message,
1259 _dbus_list_clear (&connection->outgoing_messages);
1261 _dbus_list_foreach (&connection->incoming_messages,
1262 (DBusForeachFunction) dbus_message_unref,
1264 _dbus_list_clear (&connection->incoming_messages);
1266 _dbus_counter_unref (connection->outgoing_counter);
1268 _dbus_transport_unref (connection->transport);
1270 if (connection->disconnect_message_link)
1272 DBusMessage *message = connection->disconnect_message_link->data;
1273 dbus_message_unref (message);
1274 _dbus_list_free_link (connection->disconnect_message_link);
1277 _dbus_list_clear (&connection->link_cache);
1279 dbus_condvar_free (connection->dispatch_cond);
1280 dbus_condvar_free (connection->io_path_cond);
1281 dbus_condvar_free (connection->message_returned_cond);
1283 dbus_mutex_free (connection->mutex);
1285 dbus_free (connection);
1289 * Decrements the reference count of a DBusConnection, and finalizes
1290 * it if the count reaches zero. It is a bug to drop the last reference
1291 * to a connection that has not been disconnected.
1293 * @todo in practice it can be quite tricky to never unref a connection
1294 * that's still connected; maybe there's some way we could avoid
1297 * @param connection the connection.
1300 dbus_connection_unref (DBusConnection *connection)
1302 dbus_bool_t last_unref;
1304 _dbus_return_if_fail (connection != NULL);
1305 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1307 /* The connection lock is better than the global
1308 * lock in the atomic increment fallback
1311 #ifdef DBUS_HAVE_ATOMIC_INT
1312 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1314 CONNECTION_LOCK (connection);
1316 _dbus_assert (connection->refcount.value > 0);
1318 connection->refcount.value -= 1;
1319 last_unref = (connection->refcount.value == 0);
1322 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1325 CONNECTION_UNLOCK (connection);
1329 _dbus_connection_last_unref (connection);
1333 * Closes the connection, so no further data can be sent or received.
1334 * Any further attempts to send data will result in errors. This
1335 * function does not affect the connection's reference count. It's
1336 * safe to disconnect a connection more than once; all calls after the
1337 * first do nothing. It's impossible to "reconnect" a connection, a
1338 * new connection must be created. This function may result in a call
1339 * to the DBusDispatchStatusFunction set with
1340 * dbus_connection_set_dispatch_status_function(), as the disconnect
1341 * message it generates needs to be dispatched.
1343 * @param connection the connection.
1346 dbus_connection_disconnect (DBusConnection *connection)
1348 DBusDispatchStatus status;
1350 _dbus_return_if_fail (connection != NULL);
1351 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
1353 _dbus_verbose ("Disconnecting %p\n", connection);
1355 CONNECTION_LOCK (connection);
1356 _dbus_transport_disconnect (connection->transport);
1358 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1360 /* this calls out to user code */
1361 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1365 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1367 return _dbus_transport_get_is_connected (connection->transport);
1371 * Gets whether the connection is currently connected. All
1372 * connections are connected when they are opened. A connection may
1373 * become disconnected when the remote application closes its end, or
1374 * exits; a connection may also be disconnected with
1375 * dbus_connection_disconnect().
1377 * @param connection the connection.
1378 * @returns #TRUE if the connection is still alive.
1381 dbus_connection_get_is_connected (DBusConnection *connection)
1385 _dbus_return_val_if_fail (connection != NULL, FALSE);
1387 CONNECTION_LOCK (connection);
1388 res = _dbus_connection_get_is_connected_unlocked (connection);
1389 CONNECTION_UNLOCK (connection);
1395 * Gets whether the connection was authenticated. (Note that
1396 * if the connection was authenticated then disconnected,
1397 * this function still returns #TRUE)
1399 * @param connection the connection
1400 * @returns #TRUE if the connection was ever authenticated
1403 dbus_connection_get_is_authenticated (DBusConnection *connection)
1407 _dbus_return_val_if_fail (connection != NULL, FALSE);
1409 CONNECTION_LOCK (connection);
1410 res = _dbus_transport_get_is_authenticated (connection->transport);
1411 CONNECTION_UNLOCK (connection);
1417 * Set whether _exit() should be called when the connection receives a
1418 * disconnect signal. The call to _exit() comes after any handlers for
1419 * the disconnect signal run; handlers can cancel the exit by calling
1422 * By default, exit_on_disconnect is #FALSE; but for message bus
1423 * connections returned from dbus_bus_get() it will be toggled on
1426 * @param connection the connection
1427 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
1430 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
1431 dbus_bool_t exit_on_disconnect)
1433 _dbus_return_if_fail (connection != NULL);
1435 CONNECTION_LOCK (connection);
1436 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
1437 CONNECTION_UNLOCK (connection);
1440 static DBusPreallocatedSend*
1441 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1443 DBusPreallocatedSend *preallocated;
1445 _dbus_assert (connection != NULL);
1447 preallocated = dbus_new (DBusPreallocatedSend, 1);
1448 if (preallocated == NULL)
1451 if (connection->link_cache != NULL)
1453 preallocated->queue_link =
1454 _dbus_list_pop_first_link (&connection->link_cache);
1455 preallocated->queue_link->data = NULL;
1459 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1460 if (preallocated->queue_link == NULL)
1464 if (connection->link_cache != NULL)
1466 preallocated->counter_link =
1467 _dbus_list_pop_first_link (&connection->link_cache);
1468 preallocated->counter_link->data = connection->outgoing_counter;
1472 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1473 if (preallocated->counter_link == NULL)
1477 _dbus_counter_ref (preallocated->counter_link->data);
1479 preallocated->connection = connection;
1481 return preallocated;
1484 _dbus_list_free_link (preallocated->queue_link);
1486 dbus_free (preallocated);
1492 * Preallocates resources needed to send a message, allowing the message
1493 * to be sent without the possibility of memory allocation failure.
1494 * Allows apps to create a future guarantee that they can send
1495 * a message regardless of memory shortages.
1497 * @param connection the connection we're preallocating for.
1498 * @returns the preallocated resources, or #NULL
1500 DBusPreallocatedSend*
1501 dbus_connection_preallocate_send (DBusConnection *connection)
1503 DBusPreallocatedSend *preallocated;
1505 _dbus_return_val_if_fail (connection != NULL, NULL);
1507 CONNECTION_LOCK (connection);
1510 _dbus_connection_preallocate_send_unlocked (connection);
1512 CONNECTION_UNLOCK (connection);
1514 return preallocated;
1518 * Frees preallocated message-sending resources from
1519 * dbus_connection_preallocate_send(). Should only
1520 * be called if the preallocated resources are not used
1521 * to send a message.
1523 * @param connection the connection
1524 * @param preallocated the resources
1527 dbus_connection_free_preallocated_send (DBusConnection *connection,
1528 DBusPreallocatedSend *preallocated)
1530 _dbus_return_if_fail (connection != NULL);
1531 _dbus_return_if_fail (preallocated != NULL);
1532 _dbus_return_if_fail (connection == preallocated->connection);
1534 _dbus_list_free_link (preallocated->queue_link);
1535 _dbus_counter_unref (preallocated->counter_link->data);
1536 _dbus_list_free_link (preallocated->counter_link);
1537 dbus_free (preallocated);
1541 _dbus_connection_send_preallocated_unlocked (DBusConnection *connection,
1542 DBusPreallocatedSend *preallocated,
1543 DBusMessage *message,
1544 dbus_uint32_t *client_serial)
1546 dbus_uint32_t serial;
1549 preallocated->queue_link->data = message;
1550 _dbus_list_prepend_link (&connection->outgoing_messages,
1551 preallocated->queue_link);
1553 _dbus_message_add_size_counter_link (message,
1554 preallocated->counter_link);
1556 dbus_free (preallocated);
1557 preallocated = NULL;
1559 dbus_message_ref (message);
1561 connection->n_outgoing += 1;
1563 sig = dbus_message_get_signature (message);
1565 _dbus_verbose ("Message %p (%d %s %s '%s') added to outgoing queue %p, %d pending to send\n",
1567 dbus_message_get_type (message),
1568 dbus_message_get_interface (message) ?
1569 dbus_message_get_interface (message) :
1571 dbus_message_get_member (message) ?
1572 dbus_message_get_member (message) :
1576 connection->n_outgoing);
1578 if (dbus_message_get_serial (message) == 0)
1580 serial = _dbus_connection_get_next_client_serial (connection);
1581 _dbus_message_set_serial (message, serial);
1583 *client_serial = serial;
1588 *client_serial = dbus_message_get_serial (message);
1591 _dbus_message_lock (message);
1593 /* Now we need to run an iteration to hopefully just write the messages
1594 * out immediately, and otherwise get them queued up
1596 _dbus_connection_do_iteration (connection,
1597 DBUS_ITERATION_DO_WRITING,
1600 /* If stuff is still queued up, be sure we wake up the main loop */
1601 if (connection->n_outgoing > 0)
1602 _dbus_connection_wakeup_mainloop (connection);
1606 * Sends a message using preallocated resources. This function cannot fail.
1607 * It works identically to dbus_connection_send() in other respects.
1608 * Preallocated resources comes from dbus_connection_preallocate_send().
1609 * This function "consumes" the preallocated resources, they need not
1610 * be freed separately.
1612 * @param connection the connection
1613 * @param preallocated the preallocated resources
1614 * @param message the message to send
1615 * @param client_serial return location for client serial assigned to the message
1618 dbus_connection_send_preallocated (DBusConnection *connection,
1619 DBusPreallocatedSend *preallocated,
1620 DBusMessage *message,
1621 dbus_uint32_t *client_serial)
1623 _dbus_return_if_fail (connection != NULL);
1624 _dbus_return_if_fail (preallocated != NULL);
1625 _dbus_return_if_fail (message != NULL);
1626 _dbus_return_if_fail (preallocated->connection == connection);
1627 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
1628 (dbus_message_get_interface (message) != NULL &&
1629 dbus_message_get_member (message) != NULL));
1630 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
1631 (dbus_message_get_interface (message) != NULL &&
1632 dbus_message_get_member (message) != NULL));
1634 CONNECTION_LOCK (connection);
1635 _dbus_connection_send_preallocated_unlocked (connection,
1637 message, client_serial);
1638 CONNECTION_UNLOCK (connection);
1642 _dbus_connection_send_unlocked (DBusConnection *connection,
1643 DBusMessage *message,
1644 dbus_uint32_t *client_serial)
1646 DBusPreallocatedSend *preallocated;
1648 _dbus_assert (connection != NULL);
1649 _dbus_assert (message != NULL);
1651 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
1652 if (preallocated == NULL)
1656 _dbus_connection_send_preallocated_unlocked (connection,
1664 * Adds a message to the outgoing message queue. Does not block to
1665 * write the message to the network; that happens asynchronously. To
1666 * force the message to be written, call dbus_connection_flush().
1667 * Because this only queues the message, the only reason it can
1668 * fail is lack of memory. Even if the connection is disconnected,
1669 * no error will be returned.
1671 * If the function fails due to lack of memory, it returns #FALSE.
1672 * The function will never fail for other reasons; even if the
1673 * connection is disconnected, you can queue an outgoing message,
1674 * though obviously it won't be sent.
1676 * @param connection the connection.
1677 * @param message the message to write.
1678 * @param client_serial return location for client serial.
1679 * @returns #TRUE on success.
1682 dbus_connection_send (DBusConnection *connection,
1683 DBusMessage *message,
1684 dbus_uint32_t *client_serial)
1686 _dbus_return_val_if_fail (connection != NULL, FALSE);
1687 _dbus_return_val_if_fail (message != NULL, FALSE);
1689 CONNECTION_LOCK (connection);
1691 if (!_dbus_connection_send_unlocked (connection, message, client_serial))
1693 CONNECTION_UNLOCK (connection);
1697 CONNECTION_UNLOCK (connection);
1702 reply_handler_timeout (void *data)
1704 DBusConnection *connection;
1705 DBusDispatchStatus status;
1706 DBusPendingCall *pending = data;
1708 connection = pending->connection;
1710 CONNECTION_LOCK (connection);
1711 if (pending->timeout_link)
1713 _dbus_connection_queue_synthesized_message_link (connection,
1714 pending->timeout_link);
1715 pending->timeout_link = NULL;
1718 _dbus_connection_remove_timeout (connection,
1720 pending->timeout_added = FALSE;
1722 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1724 /* Unlocks, and calls out to user code */
1725 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1731 * Queues a message to send, as with dbus_connection_send_message(),
1732 * but also returns a #DBusPendingCall used to receive a reply to the
1733 * message. If no reply is received in the given timeout_milliseconds,
1734 * this function expires the pending reply and generates a synthetic
1735 * error reply (generated in-process, not by the remote application)
1736 * indicating that a timeout occurred.
1738 * A #DBusPendingCall will see a reply message after any filters, but
1739 * before any object instances or other handlers. A #DBusPendingCall
1740 * will always see exactly one reply message, unless it's cancelled
1741 * with dbus_pending_call_cancel().
1743 * If a filter filters out the reply before the handler sees it, the
1744 * reply is immediately timed out and a timeout error reply is
1745 * generated. If a filter removes the timeout error reply then the
1746 * #DBusPendingCall will get confused. Filtering the timeout error
1747 * is thus considered a bug and will print a warning.
1749 * If #NULL is passed for the pending_return, the #DBusPendingCall
1750 * will still be generated internally, and used to track
1751 * the message reply timeout. This means a timeout error will
1752 * occur if no reply arrives, unlike with dbus_connection_send().
1754 * If -1 is passed for the timeout, a sane default timeout is used. -1
1755 * is typically the best value for the timeout for this reason, unless
1756 * you want a very short or very long timeout. There is no way to
1757 * avoid a timeout entirely, other than passing INT_MAX for the
1758 * timeout to postpone it indefinitely.
1760 * @param connection the connection
1761 * @param message the message to send
1762 * @param pending_return return location for a #DBusPendingCall object, or #NULL
1763 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1764 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1768 dbus_connection_send_with_reply (DBusConnection *connection,
1769 DBusMessage *message,
1770 DBusPendingCall **pending_return,
1771 int timeout_milliseconds)
1773 DBusPendingCall *pending;
1775 DBusList *reply_link;
1776 dbus_int32_t serial = -1;
1778 _dbus_return_val_if_fail (connection != NULL, FALSE);
1779 _dbus_return_val_if_fail (message != NULL, FALSE);
1780 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1783 *pending_return = NULL;
1785 pending = _dbus_pending_call_new (connection,
1786 timeout_milliseconds,
1787 reply_handler_timeout);
1789 if (pending == NULL)
1792 CONNECTION_LOCK (connection);
1794 /* Assign a serial to the message */
1795 if (dbus_message_get_serial (message) == 0)
1797 serial = _dbus_connection_get_next_client_serial (connection);
1798 _dbus_message_set_serial (message, serial);
1801 pending->reply_serial = serial;
1803 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
1804 "No reply within specified time");
1808 reply_link = _dbus_list_alloc_link (reply);
1809 if (reply_link == NULL)
1811 CONNECTION_UNLOCK (connection);
1812 dbus_message_unref (reply);
1813 goto error_unlocked;
1816 pending->timeout_link = reply_link;
1818 /* Insert the serial in the pending replies hash;
1819 * hash takes a refcount on DBusPendingCall.
1820 * Also, add the timeout.
1822 if (!_dbus_connection_attach_pending_call_unlocked (connection,
1826 if (!_dbus_connection_send_unlocked (connection, message, NULL))
1828 _dbus_connection_detach_pending_call_and_unlock (connection,
1830 goto error_unlocked;
1834 *pending_return = pending;
1836 dbus_pending_call_unref (pending);
1838 CONNECTION_UNLOCK (connection);
1843 CONNECTION_UNLOCK (connection);
1845 dbus_pending_call_unref (pending);
1850 check_for_reply_unlocked (DBusConnection *connection,
1851 dbus_uint32_t client_serial)
1855 link = _dbus_list_get_first_link (&connection->incoming_messages);
1857 while (link != NULL)
1859 DBusMessage *reply = link->data;
1861 if (dbus_message_get_reply_serial (reply) == client_serial)
1863 _dbus_list_remove_link (&connection->incoming_messages, link);
1864 connection->n_incoming -= 1;
1867 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1874 * Blocks a certain time period while waiting for a reply.
1875 * If no reply arrives, returns #NULL.
1877 * @todo could use performance improvements (it keeps scanning
1878 * the whole message queue for example) and has thread issues,
1879 * see comments in source
1881 * Does not re-enter the main loop or run filter/path-registered
1882 * callbacks. The reply to the message will not be seen by
1885 * @param connection the connection
1886 * @param client_serial the reply serial to wait for
1887 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1888 * @returns the message that is the reply or #NULL if no reply
1891 _dbus_connection_block_for_reply (DBusConnection *connection,
1892 dbus_uint32_t client_serial,
1893 int timeout_milliseconds)
1895 long start_tv_sec, start_tv_usec;
1896 long end_tv_sec, end_tv_usec;
1897 long tv_sec, tv_usec;
1898 DBusDispatchStatus status;
1900 _dbus_assert (connection != NULL);
1901 _dbus_assert (client_serial != 0);
1902 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
1904 if (timeout_milliseconds == -1)
1905 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
1907 /* it would probably seem logical to pass in _DBUS_INT_MAX
1908 * for infinite timeout, but then math below would get
1909 * all overflow-prone, so smack that down.
1911 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1912 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1914 /* Flush message queue */
1915 dbus_connection_flush (connection);
1917 CONNECTION_LOCK (connection);
1919 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1920 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1921 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1922 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1923 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1925 _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",
1926 timeout_milliseconds,
1928 start_tv_sec, start_tv_usec,
1929 end_tv_sec, end_tv_usec);
1931 /* Now we wait... */
1932 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1933 * gets the message before we do?
1935 /* always block at least once as we know we don't have the reply yet */
1936 _dbus_connection_do_iteration (connection,
1937 DBUS_ITERATION_DO_READING |
1938 DBUS_ITERATION_BLOCK,
1939 timeout_milliseconds);
1943 /* queue messages and get status */
1944 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1946 if (status == DBUS_DISPATCH_DATA_REMAINS)
1950 reply = check_for_reply_unlocked (connection, client_serial);
1953 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1955 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
1957 /* Unlocks, and calls out to user code */
1958 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1964 _dbus_get_current_time (&tv_sec, &tv_usec);
1966 if (!_dbus_connection_get_is_connected_unlocked (connection))
1968 else if (tv_sec < start_tv_sec)
1969 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
1970 else if (connection->disconnect_message_link == NULL)
1971 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
1972 else if (tv_sec < end_tv_sec ||
1973 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1975 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1976 (end_tv_usec - tv_usec) / 1000;
1977 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1978 _dbus_assert (timeout_milliseconds >= 0);
1980 if (status == DBUS_DISPATCH_NEED_MEMORY)
1982 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1983 * we may already have a reply in the buffer and just can't process
1986 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1988 if (timeout_milliseconds < 100)
1989 ; /* just busy loop */
1990 else if (timeout_milliseconds <= 1000)
1991 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1993 _dbus_sleep_milliseconds (1000);
1997 /* block again, we don't have the reply buffered yet. */
1998 _dbus_connection_do_iteration (connection,
1999 DBUS_ITERATION_DO_READING |
2000 DBUS_ITERATION_BLOCK,
2001 timeout_milliseconds);
2004 goto recheck_status;
2007 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
2008 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
2010 /* unlocks and calls out to user code */
2011 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2017 * Sends a message and blocks a certain time period while waiting for
2018 * a reply. This function does not reenter the main loop,
2019 * i.e. messages other than the reply are queued up but not
2020 * processed. This function is used to do non-reentrant "method
2023 * If a normal reply is received, it is returned, and removed from the
2024 * incoming message queue. If it is not received, #NULL is returned
2025 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
2026 * received, it is converted to a #DBusError and returned as an error,
2027 * then the reply message is deleted. If something else goes wrong,
2028 * result is set to whatever is appropriate, such as
2029 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
2031 * @param connection the connection
2032 * @param message the message to send
2033 * @param timeout_milliseconds timeout in milliseconds or -1 for default
2034 * @param error return location for error message
2035 * @returns the message that is the reply or #NULL with an error code if the
2039 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
2040 DBusMessage *message,
2041 int timeout_milliseconds,
2044 dbus_uint32_t client_serial;
2047 _dbus_return_val_if_fail (connection != NULL, NULL);
2048 _dbus_return_val_if_fail (message != NULL, NULL);
2049 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2050 _dbus_return_val_if_error_is_set (error, NULL);
2052 if (!dbus_connection_send (connection, message, &client_serial))
2054 _DBUS_SET_OOM (error);
2058 reply = _dbus_connection_block_for_reply (connection,
2060 timeout_milliseconds);
2064 if (dbus_connection_get_is_connected (connection))
2065 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
2067 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
2071 else if (dbus_set_error_from_message (error, reply))
2073 dbus_message_unref (reply);
2081 * Blocks until the outgoing message queue is empty.
2083 * @param connection the connection.
2086 dbus_connection_flush (DBusConnection *connection)
2088 /* We have to specify DBUS_ITERATION_DO_READING here because
2089 * otherwise we could have two apps deadlock if they are both doing
2090 * a flush(), and the kernel buffers fill up. This could change the
2093 DBusDispatchStatus status;
2095 _dbus_return_if_fail (connection != NULL);
2097 CONNECTION_LOCK (connection);
2098 while (connection->n_outgoing > 0 &&
2099 _dbus_connection_get_is_connected_unlocked (connection))
2100 _dbus_connection_do_iteration (connection,
2101 DBUS_ITERATION_DO_READING |
2102 DBUS_ITERATION_DO_WRITING |
2103 DBUS_ITERATION_BLOCK,
2106 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2108 /* Unlocks and calls out to user code */
2109 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2112 /* Call with mutex held. Will drop it while waiting and re-acquire
2116 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
2118 _dbus_assert (connection->message_borrowed != NULL);
2120 while (connection->message_borrowed != NULL)
2121 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
2125 * Returns the first-received message from the incoming message queue,
2126 * leaving it in the queue. If the queue is empty, returns #NULL.
2128 * The caller does not own a reference to the returned message, and
2129 * must either return it using dbus_connection_return_message() or
2130 * keep it after calling dbus_connection_steal_borrowed_message(). No
2131 * one can get at the message while its borrowed, so return it as
2132 * quickly as possible and don't keep a reference to it after
2133 * returning it. If you need to keep the message, make a copy of it.
2135 * @param connection the connection.
2136 * @returns next message in the incoming queue.
2139 dbus_connection_borrow_message (DBusConnection *connection)
2141 DBusMessage *message;
2142 DBusDispatchStatus status;
2144 _dbus_return_val_if_fail (connection != NULL, NULL);
2145 /* can't borrow during dispatch */
2146 _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL);
2148 /* this is called for the side effect that it queues
2149 * up any messages from the transport
2151 status = dbus_connection_get_dispatch_status (connection);
2152 if (status != DBUS_DISPATCH_DATA_REMAINS)
2155 CONNECTION_LOCK (connection);
2157 if (connection->message_borrowed != NULL)
2158 _dbus_connection_wait_for_borrowed (connection);
2160 message = _dbus_list_get_first (&connection->incoming_messages);
2163 connection->message_borrowed = message;
2165 CONNECTION_UNLOCK (connection);
2170 * Used to return a message after peeking at it using
2171 * dbus_connection_borrow_message().
2173 * @param connection the connection
2174 * @param message the message from dbus_connection_borrow_message()
2177 dbus_connection_return_message (DBusConnection *connection,
2178 DBusMessage *message)
2180 _dbus_return_if_fail (connection != NULL);
2181 _dbus_return_if_fail (message != NULL);
2182 /* can't borrow during dispatch */
2183 _dbus_return_if_fail (!connection->dispatch_acquired);
2185 CONNECTION_LOCK (connection);
2187 _dbus_assert (message == connection->message_borrowed);
2189 connection->message_borrowed = NULL;
2190 dbus_condvar_wake_all (connection->message_returned_cond);
2192 CONNECTION_UNLOCK (connection);
2196 * Used to keep a message after peeking at it using
2197 * dbus_connection_borrow_message(). Before using this function, see
2198 * the caveats/warnings in the documentation for
2199 * dbus_connection_pop_message().
2201 * @param connection the connection
2202 * @param message the message from dbus_connection_borrow_message()
2205 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2206 DBusMessage *message)
2208 DBusMessage *pop_message;
2210 _dbus_return_if_fail (connection != NULL);
2211 _dbus_return_if_fail (message != NULL);
2212 /* can't borrow during dispatch */
2213 _dbus_return_if_fail (!connection->dispatch_acquired);
2215 CONNECTION_LOCK (connection);
2217 _dbus_assert (message == connection->message_borrowed);
2219 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
2220 _dbus_assert (message == pop_message);
2222 connection->n_incoming -= 1;
2224 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
2225 message, connection->n_incoming);
2227 connection->message_borrowed = NULL;
2228 dbus_condvar_wake_all (connection->message_returned_cond);
2230 CONNECTION_UNLOCK (connection);
2233 /* See dbus_connection_pop_message, but requires the caller to own
2234 * the lock before calling. May drop the lock while running.
2237 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
2239 if (connection->message_borrowed != NULL)
2240 _dbus_connection_wait_for_borrowed (connection);
2242 if (connection->n_incoming > 0)
2246 link = _dbus_list_pop_first_link (&connection->incoming_messages);
2247 connection->n_incoming -= 1;
2249 _dbus_verbose ("Message %p (%d %s %s '%s') removed from incoming queue %p, %d incoming\n",
2251 dbus_message_get_type (link->data),
2252 dbus_message_get_interface (link->data) ?
2253 dbus_message_get_interface (link->data) :
2255 dbus_message_get_member (link->data) ?
2256 dbus_message_get_member (link->data) :
2258 dbus_message_get_signature (link->data),
2259 connection, connection->n_incoming);
2267 /* See dbus_connection_pop_message, but requires the caller to own
2268 * the lock before calling. May drop the lock while running.
2271 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
2275 link = _dbus_connection_pop_message_link_unlocked (connection);
2279 DBusMessage *message;
2281 message = link->data;
2283 _dbus_list_free_link (link);
2292 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
2293 DBusList *message_link)
2295 _dbus_assert (message_link != NULL);
2296 /* You can't borrow a message while a link is outstanding */
2297 _dbus_assert (connection->message_borrowed == NULL);
2299 _dbus_list_prepend_link (&connection->incoming_messages,
2301 connection->n_incoming += 1;
2303 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
2305 dbus_message_get_type (message_link->data),
2306 dbus_message_get_interface (message_link->data) ?
2307 dbus_message_get_interface (message_link->data) :
2309 dbus_message_get_member (message_link->data) ?
2310 dbus_message_get_member (message_link->data) :
2312 dbus_message_get_signature (message_link->data),
2313 connection, connection->n_incoming);
2317 * Returns the first-received message from the incoming message queue,
2318 * removing it from the queue. The caller owns a reference to the
2319 * returned message. If the queue is empty, returns #NULL.
2321 * This function bypasses any message handlers that are registered,
2322 * and so using it is usually wrong. Instead, let the main loop invoke
2323 * dbus_connection_dispatch(). Popping messages manually is only
2324 * useful in very simple programs that don't share a #DBusConnection
2325 * with any libraries or other modules.
2327 * @param connection the connection.
2328 * @returns next message in the incoming queue.
2331 dbus_connection_pop_message (DBusConnection *connection)
2333 DBusMessage *message;
2334 DBusDispatchStatus status;
2336 /* this is called for the side effect that it queues
2337 * up any messages from the transport
2339 status = dbus_connection_get_dispatch_status (connection);
2340 if (status != DBUS_DISPATCH_DATA_REMAINS)
2343 CONNECTION_LOCK (connection);
2345 message = _dbus_connection_pop_message_unlocked (connection);
2347 _dbus_verbose ("Returning popped message %p\n", message);
2349 CONNECTION_UNLOCK (connection);
2355 * Acquire the dispatcher. This must be done before dispatching
2356 * messages in order to guarantee the right order of
2357 * message delivery. May sleep and drop the connection mutex
2358 * while waiting for the dispatcher.
2360 * @param connection the connection.
2363 _dbus_connection_acquire_dispatch (DBusConnection *connection)
2365 if (connection->dispatch_acquired)
2366 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
2367 _dbus_assert (!connection->dispatch_acquired);
2369 connection->dispatch_acquired = TRUE;
2373 * Release the dispatcher when you're done with it. Only call
2374 * after you've acquired the dispatcher. Wakes up at most one
2375 * thread currently waiting to acquire the dispatcher.
2377 * @param connection the connection.
2380 _dbus_connection_release_dispatch (DBusConnection *connection)
2382 _dbus_assert (connection->dispatch_acquired);
2384 connection->dispatch_acquired = FALSE;
2385 dbus_condvar_wake_one (connection->dispatch_cond);
2389 _dbus_connection_failed_pop (DBusConnection *connection,
2390 DBusList *message_link)
2392 _dbus_list_prepend_link (&connection->incoming_messages,
2394 connection->n_incoming += 1;
2397 static DBusDispatchStatus
2398 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
2400 if (connection->n_incoming > 0)
2401 return DBUS_DISPATCH_DATA_REMAINS;
2402 else if (!_dbus_transport_queue_messages (connection->transport))
2403 return DBUS_DISPATCH_NEED_MEMORY;
2406 DBusDispatchStatus status;
2407 dbus_bool_t is_connected;
2409 status = _dbus_transport_get_dispatch_status (connection->transport);
2410 is_connected = _dbus_transport_get_is_connected (connection->transport);
2412 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
2413 DISPATCH_STATUS_NAME (status), is_connected);
2417 if (status == DBUS_DISPATCH_COMPLETE &&
2418 connection->disconnect_message_link)
2420 _dbus_verbose ("Sending disconnect message from %s\n",
2421 _DBUS_FUNCTION_NAME);
2423 /* We haven't sent the disconnect message already,
2424 * and all real messages have been queued up.
2426 _dbus_connection_queue_synthesized_message_link (connection,
2427 connection->disconnect_message_link);
2428 connection->disconnect_message_link = NULL;
2431 /* Dump the outgoing queue, we aren't going to be able to
2432 * send it now, and we'd like accessors like
2433 * dbus_connection_get_outgoing_size() to be accurate.
2435 if (connection->n_outgoing > 0)
2439 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
2440 connection->n_outgoing);
2442 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
2444 _dbus_connection_message_sent (connection, link->data);
2449 if (status != DBUS_DISPATCH_COMPLETE)
2451 else if (connection->n_incoming > 0)
2452 return DBUS_DISPATCH_DATA_REMAINS;
2454 return DBUS_DISPATCH_COMPLETE;
2459 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
2460 DBusDispatchStatus new_status)
2462 dbus_bool_t changed;
2463 DBusDispatchStatusFunction function;
2466 /* We have the lock */
2468 _dbus_connection_ref_unlocked (connection);
2470 changed = new_status != connection->last_dispatch_status;
2472 connection->last_dispatch_status = new_status;
2474 function = connection->dispatch_status_function;
2475 data = connection->dispatch_status_data;
2477 /* We drop the lock */
2478 CONNECTION_UNLOCK (connection);
2480 if (changed && function)
2482 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
2483 connection, new_status,
2484 DISPATCH_STATUS_NAME (new_status));
2485 (* function) (connection, new_status, data);
2488 dbus_connection_unref (connection);
2492 * Gets the current state (what we would currently return
2493 * from dbus_connection_dispatch()) but doesn't actually
2494 * dispatch any messages.
2496 * @param connection the connection.
2497 * @returns current dispatch status
2500 dbus_connection_get_dispatch_status (DBusConnection *connection)
2502 DBusDispatchStatus status;
2504 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2506 CONNECTION_LOCK (connection);
2508 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2510 CONNECTION_UNLOCK (connection);
2516 * Processes data buffered while handling watches, queueing zero or
2517 * more incoming messages. Then pops the first-received message from
2518 * the current incoming message queue, runs any handlers for it, and
2519 * unrefs the message. Returns a status indicating whether messages/data
2520 * remain, more memory is needed, or all data has been processed.
2522 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
2523 * does not necessarily dispatch a message, as the data may
2524 * be part of authentication or the like.
2526 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
2528 * @todo right now a message filter gets run on replies to a pending
2529 * call in here, but not in the case where we block without entering
2530 * the main loop. Simple solution might be to just have the pending
2531 * call stuff run before the filters.
2533 * @todo FIXME what if we call out to application code to handle a
2534 * message, holding the dispatch lock, and the application code runs
2535 * the main loop and dispatches again? Probably deadlocks at the
2536 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
2537 * and then the GSource etc. could handle the situation?
2539 * @param connection the connection
2540 * @returns dispatch status
2543 dbus_connection_dispatch (DBusConnection *connection)
2545 DBusMessage *message;
2546 DBusList *link, *filter_list_copy, *message_link;
2547 DBusHandlerResult result;
2548 DBusPendingCall *pending;
2549 dbus_int32_t reply_serial;
2550 DBusDispatchStatus status;
2552 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2554 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
2556 CONNECTION_LOCK (connection);
2557 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2558 if (status != DBUS_DISPATCH_DATA_REMAINS)
2560 /* unlocks and calls out to user code */
2561 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2565 /* We need to ref the connection since the callback could potentially
2566 * drop the last ref to it
2568 _dbus_connection_ref_unlocked (connection);
2570 _dbus_connection_acquire_dispatch (connection);
2572 /* This call may drop the lock during the execution (if waiting for
2573 * borrowed messages to be returned) but the order of message
2574 * dispatch if several threads call dispatch() is still
2575 * protected by the lock, since only one will get the lock, and that
2576 * one will finish the message dispatching
2578 message_link = _dbus_connection_pop_message_link_unlocked (connection);
2579 if (message_link == NULL)
2581 /* another thread dispatched our stuff */
2583 _dbus_verbose ("another thread dispatched message\n");
2585 _dbus_connection_release_dispatch (connection);
2587 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2589 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2591 dbus_connection_unref (connection);
2596 message = message_link->data;
2598 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
2600 dbus_message_get_type (message),
2601 dbus_message_get_interface (message) ?
2602 dbus_message_get_interface (message) :
2604 dbus_message_get_member (message) ?
2605 dbus_message_get_member (message) :
2607 dbus_message_get_signature (message));
2609 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2611 reply_serial = dbus_message_get_reply_serial (message);
2612 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
2615 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
2617 _dbus_connection_release_dispatch (connection);
2619 _dbus_connection_failed_pop (connection, message_link);
2621 /* unlocks and calls user code */
2622 _dbus_connection_update_dispatch_status_and_unlock (connection,
2623 DBUS_DISPATCH_NEED_MEMORY);
2625 dbus_connection_unref (connection);
2627 return DBUS_DISPATCH_NEED_MEMORY;
2630 _dbus_list_foreach (&filter_list_copy,
2631 (DBusForeachFunction)_dbus_message_filter_ref,
2634 /* We're still protected from dispatch() reentrancy here
2635 * since we acquired the dispatcher
2637 CONNECTION_UNLOCK (connection);
2639 link = _dbus_list_get_first_link (&filter_list_copy);
2640 while (link != NULL)
2642 DBusMessageFilter *filter = link->data;
2643 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2645 _dbus_verbose (" running filter on message %p\n", message);
2646 result = (* filter->function) (connection, message, filter->user_data);
2648 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2654 _dbus_list_foreach (&filter_list_copy,
2655 (DBusForeachFunction)_dbus_message_filter_unref,
2657 _dbus_list_clear (&filter_list_copy);
2659 CONNECTION_LOCK (connection);
2661 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2663 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
2667 /* Did a reply we were waiting on get filtered? */
2668 if (pending && result == DBUS_HANDLER_RESULT_HANDLED)
2670 /* Queue the timeout immediately! */
2671 if (pending->timeout_link)
2673 _dbus_connection_queue_synthesized_message_link (connection,
2674 pending->timeout_link);
2675 pending->timeout_link = NULL;
2679 /* We already queued the timeout? Then it was filtered! */
2680 _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial);
2684 if (result == DBUS_HANDLER_RESULT_HANDLED)
2686 _dbus_verbose ("filter handled message in dispatch\n");
2692 _dbus_pending_call_complete_and_unlock (pending, message);
2696 CONNECTION_LOCK (connection);
2697 _dbus_verbose ("pending call completed in dispatch\n");
2701 /* We're still protected from dispatch() reentrancy here
2702 * since we acquired the dispatcher
2704 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n",
2706 dbus_message_get_type (message),
2707 dbus_message_get_interface (message) ?
2708 dbus_message_get_interface (message) :
2710 dbus_message_get_member (message) ?
2711 dbus_message_get_member (message) :
2713 dbus_message_get_signature (message));
2715 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
2718 CONNECTION_LOCK (connection);
2720 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2722 _dbus_verbose ("object tree handled message in dispatch\n");
2726 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
2730 DBusPreallocatedSend *preallocated;
2732 _dbus_verbose (" sending error %s\n",
2733 DBUS_ERROR_UNKNOWN_METHOD);
2735 if (!_dbus_string_init (&str))
2737 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2738 _dbus_verbose ("no memory for error string in dispatch\n");
2742 if (!_dbus_string_append_printf (&str,
2743 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
2744 dbus_message_get_member (message),
2745 dbus_message_get_signature (message),
2746 dbus_message_get_interface (message)))
2748 _dbus_string_free (&str);
2749 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2750 _dbus_verbose ("no memory for error string in dispatch\n");
2754 reply = dbus_message_new_error (message,
2755 DBUS_ERROR_UNKNOWN_METHOD,
2756 _dbus_string_get_const_data (&str));
2757 _dbus_string_free (&str);
2761 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2762 _dbus_verbose ("no memory for error reply in dispatch\n");
2766 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2768 if (preallocated == NULL)
2770 dbus_message_unref (reply);
2771 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2772 _dbus_verbose ("no memory for error send in dispatch\n");
2776 _dbus_connection_send_preallocated_unlocked (connection, preallocated,
2779 dbus_message_unref (reply);
2781 result = DBUS_HANDLER_RESULT_HANDLED;
2784 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message,
2785 dbus_message_get_type (message),
2786 dbus_message_get_interface (message) ?
2787 dbus_message_get_interface (message) :
2789 dbus_message_get_member (message) ?
2790 dbus_message_get_member (message) :
2792 dbus_message_get_signature (message),
2796 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2798 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
2800 /* Put message back, and we'll start over.
2801 * Yes this means handlers must be idempotent if they
2802 * don't return HANDLED; c'est la vie.
2804 _dbus_connection_putback_message_link_unlocked (connection,
2809 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
2811 if (connection->exit_on_disconnect &&
2812 dbus_message_is_signal (message,
2813 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
2816 _dbus_verbose ("Exiting on Disconnected signal\n");
2817 CONNECTION_UNLOCK (connection);
2819 _dbus_assert_not_reached ("Call to exit() returned");
2822 _dbus_list_free_link (message_link);
2823 dbus_message_unref (message); /* don't want the message to count in max message limits
2824 * in computing dispatch status below
2828 _dbus_connection_release_dispatch (connection);
2830 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2832 /* unlocks and calls user code */
2833 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2835 dbus_connection_unref (connection);
2841 * Sets the watch functions for the connection. These functions are
2842 * responsible for making the application's main loop aware of file
2843 * descriptors that need to be monitored for events, using select() or
2844 * poll(). When using Qt, typically the DBusAddWatchFunction would
2845 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2846 * could call g_io_add_watch(), or could be used as part of a more
2847 * elaborate GSource. Note that when a watch is added, it may
2850 * The DBusWatchToggledFunction notifies the application that the
2851 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2852 * to check this. A disabled watch should have no effect, and enabled
2853 * watch should be added to the main loop. This feature is used
2854 * instead of simply adding/removing the watch because
2855 * enabling/disabling can be done without memory allocation. The
2856 * toggled function may be NULL if a main loop re-queries
2857 * dbus_watch_get_enabled() every time anyway.
2859 * The DBusWatch can be queried for the file descriptor to watch using
2860 * dbus_watch_get_fd(), and for the events to watch for using
2861 * dbus_watch_get_flags(). The flags returned by
2862 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2863 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2864 * all watches implicitly include a watch for hangups, errors, and
2865 * other exceptional conditions.
2867 * Once a file descriptor becomes readable or writable, or an exception
2868 * occurs, dbus_watch_handle() should be called to
2869 * notify the connection of the file descriptor's condition.
2871 * dbus_watch_handle() cannot be called during the
2872 * DBusAddWatchFunction, as the connection will not be ready to handle
2875 * It is not allowed to reference a DBusWatch after it has been passed
2876 * to remove_function.
2878 * If #FALSE is returned due to lack of memory, the failure may be due
2879 * to a #FALSE return from the new add_function. If so, the
2880 * add_function may have been called successfully one or more times,
2881 * but the remove_function will also have been called to remove any
2882 * successful adds. i.e. if #FALSE is returned the net result
2883 * should be that dbus_connection_set_watch_functions() has no effect,
2884 * but the add_function and remove_function may have been called.
2886 * @todo We need to drop the lock when we call the
2887 * add/remove/toggled functions which can be a side effect
2888 * of setting the watch functions.
2890 * @param connection the connection.
2891 * @param add_function function to begin monitoring a new descriptor.
2892 * @param remove_function function to stop monitoring a descriptor.
2893 * @param toggled_function function to notify of enable/disable
2894 * @param data data to pass to add_function and remove_function.
2895 * @param free_data_function function to be called to free the data.
2896 * @returns #FALSE on failure (no memory)
2899 dbus_connection_set_watch_functions (DBusConnection *connection,
2900 DBusAddWatchFunction add_function,
2901 DBusRemoveWatchFunction remove_function,
2902 DBusWatchToggledFunction toggled_function,
2904 DBusFreeFunction free_data_function)
2908 _dbus_return_val_if_fail (connection != NULL, FALSE);
2910 CONNECTION_LOCK (connection);
2911 /* ref connection for slightly better reentrancy */
2912 _dbus_connection_ref_unlocked (connection);
2914 /* FIXME this can call back into user code, and we need to drop the
2915 * connection lock when it does.
2917 retval = _dbus_watch_list_set_functions (connection->watches,
2918 add_function, remove_function,
2920 data, free_data_function);
2922 CONNECTION_UNLOCK (connection);
2923 /* drop our paranoid refcount */
2924 dbus_connection_unref (connection);
2930 * Sets the timeout functions for the connection. These functions are
2931 * responsible for making the application's main loop aware of timeouts.
2932 * When using Qt, typically the DBusAddTimeoutFunction would create a
2933 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2936 * The DBusTimeoutToggledFunction notifies the application that the
2937 * timeout has been enabled or disabled. Call
2938 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2939 * have no effect, and enabled timeout should be added to the main
2940 * loop. This feature is used instead of simply adding/removing the
2941 * timeout because enabling/disabling can be done without memory
2942 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2943 * to enable and disable. The toggled function may be NULL if a main
2944 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2945 * Whenever a timeout is toggled, its interval may change.
2947 * The DBusTimeout can be queried for the timer interval using
2948 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
2949 * repeatedly, each time the interval elapses, starting after it has
2950 * elapsed once. The timeout stops firing when it is removed with the
2951 * given remove_function. The timer interval may change whenever the
2952 * timeout is added, removed, or toggled.
2954 * @param connection the connection.
2955 * @param add_function function to add a timeout.
2956 * @param remove_function function to remove a timeout.
2957 * @param toggled_function function to notify of enable/disable
2958 * @param data data to pass to add_function and remove_function.
2959 * @param free_data_function function to be called to free the data.
2960 * @returns #FALSE on failure (no memory)
2963 dbus_connection_set_timeout_functions (DBusConnection *connection,
2964 DBusAddTimeoutFunction add_function,
2965 DBusRemoveTimeoutFunction remove_function,
2966 DBusTimeoutToggledFunction toggled_function,
2968 DBusFreeFunction free_data_function)
2972 _dbus_return_val_if_fail (connection != NULL, FALSE);
2974 CONNECTION_LOCK (connection);
2975 /* ref connection for slightly better reentrancy */
2976 _dbus_connection_ref_unlocked (connection);
2978 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2979 add_function, remove_function,
2981 data, free_data_function);
2983 CONNECTION_UNLOCK (connection);
2984 /* drop our paranoid refcount */
2985 dbus_connection_unref (connection);
2991 * Sets the mainloop wakeup function for the connection. Thi function is
2992 * responsible for waking up the main loop (if its sleeping) when some some
2993 * change has happened to the connection that the mainloop needs to reconsiders
2994 * (e.g. a message has been queued for writing).
2995 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2996 * When using GLib, it would call g_main_context_wakeup().
2999 * @param connection the connection.
3000 * @param wakeup_main_function function to wake up the mainloop
3001 * @param data data to pass wakeup_main_function
3002 * @param free_data_function function to be called to free the data.
3005 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
3006 DBusWakeupMainFunction wakeup_main_function,
3008 DBusFreeFunction free_data_function)
3011 DBusFreeFunction old_free_data;
3013 _dbus_return_if_fail (connection != NULL);
3015 CONNECTION_LOCK (connection);
3016 old_data = connection->wakeup_main_data;
3017 old_free_data = connection->free_wakeup_main_data;
3019 connection->wakeup_main_function = wakeup_main_function;
3020 connection->wakeup_main_data = data;
3021 connection->free_wakeup_main_data = free_data_function;
3023 CONNECTION_UNLOCK (connection);
3025 /* Callback outside the lock */
3027 (*old_free_data) (old_data);
3031 * Set a function to be invoked when the dispatch status changes.
3032 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
3033 * dbus_connection_dispatch() needs to be called to process incoming
3034 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
3035 * from inside the DBusDispatchStatusFunction. Indeed, almost
3036 * any reentrancy in this function is a bad idea. Instead,
3037 * the DBusDispatchStatusFunction should simply save an indication
3038 * that messages should be dispatched later, when the main loop
3041 * @param connection the connection
3042 * @param function function to call on dispatch status changes
3043 * @param data data for function
3044 * @param free_data_function free the function data
3047 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
3048 DBusDispatchStatusFunction function,
3050 DBusFreeFunction free_data_function)
3053 DBusFreeFunction old_free_data;
3055 _dbus_return_if_fail (connection != NULL);
3057 CONNECTION_LOCK (connection);
3058 old_data = connection->dispatch_status_data;
3059 old_free_data = connection->free_dispatch_status_data;
3061 connection->dispatch_status_function = function;
3062 connection->dispatch_status_data = data;
3063 connection->free_dispatch_status_data = free_data_function;
3065 CONNECTION_UNLOCK (connection);
3067 /* Callback outside the lock */
3069 (*old_free_data) (old_data);
3073 * Get the UNIX file descriptor of the connection, if any. This can
3074 * be used for SELinux access control checks with getpeercon() for
3075 * example. DO NOT read or write to the file descriptor, or try to
3076 * select() on it; use DBusWatch for main loop integration. Not all
3077 * connections will have a file descriptor. So for adding descriptors
3078 * to the main loop, use dbus_watch_get_fd() and so forth.
3080 * @param connection the connection
3081 * @param fd return location for the file descriptor.
3082 * @returns #TRUE if fd is successfully obtained.
3085 dbus_connection_get_unix_fd (DBusConnection *connection,
3090 _dbus_return_val_if_fail (connection != NULL, FALSE);
3091 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
3093 CONNECTION_LOCK (connection);
3095 retval = _dbus_transport_get_unix_fd (connection->transport,
3098 CONNECTION_UNLOCK (connection);
3104 * Gets the UNIX user ID of the connection if any.
3105 * Returns #TRUE if the uid is filled in.
3106 * Always returns #FALSE on non-UNIX platforms.
3107 * Always returns #FALSE prior to authenticating the
3110 * @param connection the connection
3111 * @param uid return location for the user ID
3112 * @returns #TRUE if uid is filled in with a valid user ID
3115 dbus_connection_get_unix_user (DBusConnection *connection,
3120 _dbus_return_val_if_fail (connection != NULL, FALSE);
3121 _dbus_return_val_if_fail (uid != NULL, FALSE);
3123 CONNECTION_LOCK (connection);
3125 if (!_dbus_transport_get_is_authenticated (connection->transport))
3128 result = _dbus_transport_get_unix_user (connection->transport,
3130 CONNECTION_UNLOCK (connection);
3136 * Gets the process ID of the connection if any.
3137 * Returns #TRUE if the uid is filled in.
3138 * Always returns #FALSE prior to authenticating the
3141 * @param connection the connection
3142 * @param pid return location for the process ID
3143 * @returns #TRUE if uid is filled in with a valid process ID
3146 dbus_connection_get_unix_process_id (DBusConnection *connection,
3151 _dbus_return_val_if_fail (connection != NULL, FALSE);
3152 _dbus_return_val_if_fail (pid != NULL, FALSE);
3154 CONNECTION_LOCK (connection);
3156 if (!_dbus_transport_get_is_authenticated (connection->transport))
3159 result = _dbus_transport_get_unix_process_id (connection->transport,
3161 CONNECTION_UNLOCK (connection);
3167 * Sets a predicate function used to determine whether a given user ID
3168 * is allowed to connect. When an incoming connection has
3169 * authenticated with a particular user ID, this function is called;
3170 * if it returns #TRUE, the connection is allowed to proceed,
3171 * otherwise the connection is disconnected.
3173 * If the function is set to #NULL (as it is by default), then
3174 * only the same UID as the server process will be allowed to
3177 * @param connection the connection
3178 * @param function the predicate
3179 * @param data data to pass to the predicate
3180 * @param free_data_function function to free the data
3183 dbus_connection_set_unix_user_function (DBusConnection *connection,
3184 DBusAllowUnixUserFunction function,
3186 DBusFreeFunction free_data_function)
3188 void *old_data = NULL;
3189 DBusFreeFunction old_free_function = NULL;
3191 _dbus_return_if_fail (connection != NULL);
3193 CONNECTION_LOCK (connection);
3194 _dbus_transport_set_unix_user_function (connection->transport,
3195 function, data, free_data_function,
3196 &old_data, &old_free_function);
3197 CONNECTION_UNLOCK (connection);
3199 if (old_free_function != NULL)
3200 (* old_free_function) (old_data);
3204 * Adds a message filter. Filters are handlers that are run on all
3205 * incoming messages, prior to the objects registered with
3206 * dbus_connection_register_object_path(). Filters are run in the
3207 * order that they were added. The same handler can be added as a
3208 * filter more than once, in which case it will be run more than once.
3209 * Filters added during a filter callback won't be run on the message
3212 * @todo we don't run filters on messages while blocking without
3213 * entering the main loop, since filters are run as part of
3214 * dbus_connection_dispatch(). This is probably a feature, as filters
3215 * could create arbitrary reentrancy. But kind of sucks if you're
3216 * trying to filter METHOD_RETURN for some reason.
3218 * @param connection the connection
3219 * @param function function to handle messages
3220 * @param user_data user data to pass to the function
3221 * @param free_data_function function to use for freeing user data
3222 * @returns #TRUE on success, #FALSE if not enough memory.
3225 dbus_connection_add_filter (DBusConnection *connection,
3226 DBusHandleMessageFunction function,
3228 DBusFreeFunction free_data_function)
3230 DBusMessageFilter *filter;
3232 _dbus_return_val_if_fail (connection != NULL, FALSE);
3233 _dbus_return_val_if_fail (function != NULL, FALSE);
3235 filter = dbus_new0 (DBusMessageFilter, 1);
3239 filter->refcount.value = 1;
3241 CONNECTION_LOCK (connection);
3243 if (!_dbus_list_append (&connection->filter_list,
3246 _dbus_message_filter_unref (filter);
3247 CONNECTION_UNLOCK (connection);
3251 /* Fill in filter after all memory allocated,
3252 * so we don't run the free_user_data_function
3253 * if the add_filter() fails
3256 filter->function = function;
3257 filter->user_data = user_data;
3258 filter->free_user_data_function = free_data_function;
3260 CONNECTION_UNLOCK (connection);
3265 * Removes a previously-added message filter. It is a programming
3266 * error to call this function for a handler that has not been added
3267 * as a filter. If the given handler was added more than once, only
3268 * one instance of it will be removed (the most recently-added
3271 * @param connection the connection
3272 * @param function the handler to remove
3273 * @param user_data user data for the handler to remove
3277 dbus_connection_remove_filter (DBusConnection *connection,
3278 DBusHandleMessageFunction function,
3282 DBusMessageFilter *filter;
3284 _dbus_return_if_fail (connection != NULL);
3285 _dbus_return_if_fail (function != NULL);
3287 CONNECTION_LOCK (connection);
3291 link = _dbus_list_get_last_link (&connection->filter_list);
3292 while (link != NULL)
3294 filter = link->data;
3296 if (filter->function == function &&
3297 filter->user_data == user_data)
3299 _dbus_list_remove_link (&connection->filter_list, link);
3300 filter->function = NULL;
3305 link = _dbus_list_get_prev_link (&connection->filter_list, link);
3308 CONNECTION_UNLOCK (connection);
3310 #ifndef DBUS_DISABLE_CHECKS
3313 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
3314 function, user_data);
3319 /* Call application code */
3320 if (filter->free_user_data_function)
3321 (* filter->free_user_data_function) (filter->user_data);
3323 filter->free_user_data_function = NULL;
3324 filter->user_data = NULL;
3326 _dbus_message_filter_unref (filter);
3330 * Registers a handler for a given path in the object hierarchy.
3331 * The given vtable handles messages sent to exactly the given path.
3334 * @param connection the connection
3335 * @param path a '/' delimited string of path elements
3336 * @param vtable the virtual table
3337 * @param user_data data to pass to functions in the vtable
3338 * @returns #FALSE if not enough memory
3341 dbus_connection_register_object_path (DBusConnection *connection,
3343 const DBusObjectPathVTable *vtable,
3346 char **decomposed_path;
3349 _dbus_return_val_if_fail (connection != NULL, FALSE);
3350 _dbus_return_val_if_fail (path != NULL, FALSE);
3351 _dbus_return_val_if_fail (path[0] == '/', FALSE);
3352 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3354 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
3357 CONNECTION_LOCK (connection);
3359 retval = _dbus_object_tree_register (connection->objects,
3361 (const char **) decomposed_path, vtable,
3364 CONNECTION_UNLOCK (connection);
3366 dbus_free_string_array (decomposed_path);
3372 * Registers a fallback handler for a given subsection of the object
3373 * hierarchy. The given vtable handles messages at or below the given
3374 * path. You can use this to establish a default message handling
3375 * policy for a whole "subdirectory."
3377 * @param connection the connection
3378 * @param path a '/' delimited string of path elements
3379 * @param vtable the virtual table
3380 * @param user_data data to pass to functions in the vtable
3381 * @returns #FALSE if not enough memory
3384 dbus_connection_register_fallback (DBusConnection *connection,
3386 const DBusObjectPathVTable *vtable,
3389 char **decomposed_path;
3392 _dbus_return_val_if_fail (connection != NULL, FALSE);
3393 _dbus_return_val_if_fail (path != NULL, FALSE);
3394 _dbus_return_val_if_fail (path[0] == '/', FALSE);
3395 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3397 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
3400 CONNECTION_LOCK (connection);
3402 retval = _dbus_object_tree_register (connection->objects,
3404 (const char **) decomposed_path, vtable,
3407 CONNECTION_UNLOCK (connection);
3409 dbus_free_string_array (decomposed_path);
3415 * Unregisters the handler registered with exactly the given path.
3416 * It's a bug to call this function for a path that isn't registered.
3417 * Can unregister both fallback paths and object paths.
3419 * @param connection the connection
3420 * @param path a '/' delimited string of path elements
3421 * @returns #FALSE if not enough memory
3424 dbus_connection_unregister_object_path (DBusConnection *connection,
3427 char **decomposed_path;
3429 _dbus_return_val_if_fail (connection != NULL, FALSE);
3430 _dbus_return_val_if_fail (path != NULL, FALSE);
3431 _dbus_return_val_if_fail (path[0] == '/', FALSE);
3433 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
3436 CONNECTION_LOCK (connection);
3438 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
3440 dbus_free_string_array (decomposed_path);
3446 * Lists the registered fallback handlers and object path handlers at
3447 * the given parent_path. The returned array should be freed with
3448 * dbus_free_string_array().
3450 * @param connection the connection
3451 * @param parent_path the path to list the child handlers of
3452 * @param child_entries returns #NULL-terminated array of children
3453 * @returns #FALSE if no memory to allocate the child entries
3456 dbus_connection_list_registered (DBusConnection *connection,
3457 const char *parent_path,
3458 char ***child_entries)
3460 char **decomposed_path;
3462 _dbus_return_val_if_fail (connection != NULL, FALSE);
3463 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
3464 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
3465 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
3467 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
3470 CONNECTION_LOCK (connection);
3472 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
3473 (const char **) decomposed_path,
3475 dbus_free_string_array (decomposed_path);
3480 static DBusDataSlotAllocator slot_allocator;
3481 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
3484 * Allocates an integer ID to be used for storing application-specific
3485 * data on any DBusConnection. The allocated ID may then be used
3486 * with dbus_connection_set_data() and dbus_connection_get_data().
3487 * The passed-in slot must be initialized to -1, and is filled in
3488 * with the slot ID. If the passed-in slot is not -1, it's assumed
3489 * to be already allocated, and its refcount is incremented.
3491 * The allocated slot is global, i.e. all DBusConnection objects will
3492 * have a slot with the given integer ID reserved.
3494 * @param slot_p address of a global variable storing the slot
3495 * @returns #FALSE on failure (no memory)
3498 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
3500 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3501 _DBUS_LOCK_NAME (connection_slots),
3506 * Deallocates a global ID for connection data slots.
3507 * dbus_connection_get_data() and dbus_connection_set_data() may no
3508 * longer be used with this slot. Existing data stored on existing
3509 * DBusConnection objects will be freed when the connection is
3510 * finalized, but may not be retrieved (and may only be replaced if
3511 * someone else reallocates the slot). When the refcount on the
3512 * passed-in slot reaches 0, it is set to -1.
3514 * @param slot_p address storing the slot to deallocate
3517 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
3519 _dbus_return_if_fail (*slot_p >= 0);
3521 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3525 * Stores a pointer on a DBusConnection, along
3526 * with an optional function to be used for freeing
3527 * the data when the data is set again, or when
3528 * the connection is finalized. The slot number
3529 * must have been allocated with dbus_connection_allocate_data_slot().
3531 * @param connection the connection
3532 * @param slot the slot number
3533 * @param data the data to store
3534 * @param free_data_func finalizer function for the data
3535 * @returns #TRUE if there was enough memory to store the data
3538 dbus_connection_set_data (DBusConnection *connection,
3541 DBusFreeFunction free_data_func)
3543 DBusFreeFunction old_free_func;
3547 _dbus_return_val_if_fail (connection != NULL, FALSE);
3548 _dbus_return_val_if_fail (slot >= 0, FALSE);
3550 CONNECTION_LOCK (connection);
3552 retval = _dbus_data_slot_list_set (&slot_allocator,
3553 &connection->slot_list,
3554 slot, data, free_data_func,
3555 &old_free_func, &old_data);
3557 CONNECTION_UNLOCK (connection);
3561 /* Do the actual free outside the connection lock */
3563 (* old_free_func) (old_data);
3570 * Retrieves data previously set with dbus_connection_set_data().
3571 * The slot must still be allocated (must not have been freed).
3573 * @param connection the connection
3574 * @param slot the slot to get data from
3575 * @returns the data, or #NULL if not found
3578 dbus_connection_get_data (DBusConnection *connection,
3583 _dbus_return_val_if_fail (connection != NULL, NULL);
3585 CONNECTION_LOCK (connection);
3587 res = _dbus_data_slot_list_get (&slot_allocator,
3588 &connection->slot_list,
3591 CONNECTION_UNLOCK (connection);
3597 * This function sets a global flag for whether dbus_connection_new()
3598 * will set SIGPIPE behavior to SIG_IGN.
3600 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
3603 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
3605 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
3609 * Specifies the maximum size message this connection is allowed to
3610 * receive. Larger messages will result in disconnecting the
3613 * @param connection a #DBusConnection
3614 * @param size maximum message size the connection can receive, in bytes
3617 dbus_connection_set_max_message_size (DBusConnection *connection,
3620 _dbus_return_if_fail (connection != NULL);
3622 CONNECTION_LOCK (connection);
3623 _dbus_transport_set_max_message_size (connection->transport,
3625 CONNECTION_UNLOCK (connection);
3629 * Gets the value set by dbus_connection_set_max_message_size().
3631 * @param connection the connection
3632 * @returns the max size of a single message
3635 dbus_connection_get_max_message_size (DBusConnection *connection)
3639 _dbus_return_val_if_fail (connection != NULL, 0);
3641 CONNECTION_LOCK (connection);
3642 res = _dbus_transport_get_max_message_size (connection->transport);
3643 CONNECTION_UNLOCK (connection);
3648 * Sets the maximum total number of bytes that can be used for all messages
3649 * received on this connection. Messages count toward the maximum until
3650 * they are finalized. When the maximum is reached, the connection will
3651 * not read more data until some messages are finalized.
3653 * The semantics of the maximum are: if outstanding messages are
3654 * already above the maximum, additional messages will not be read.
3655 * The semantics are not: if the next message would cause us to exceed
3656 * the maximum, we don't read it. The reason is that we don't know the
3657 * size of a message until after we read it.
3659 * Thus, the max live messages size can actually be exceeded
3660 * by up to the maximum size of a single message.
3662 * Also, if we read say 1024 bytes off the wire in a single read(),
3663 * and that contains a half-dozen small messages, we may exceed the
3664 * size max by that amount. But this should be inconsequential.
3666 * This does imply that we can't call read() with a buffer larger
3667 * than we're willing to exceed this limit by.
3669 * @param connection the connection
3670 * @param size the maximum size in bytes of all outstanding messages
3673 dbus_connection_set_max_received_size (DBusConnection *connection,
3676 _dbus_return_if_fail (connection != NULL);
3678 CONNECTION_LOCK (connection);
3679 _dbus_transport_set_max_received_size (connection->transport,
3681 CONNECTION_UNLOCK (connection);
3685 * Gets the value set by dbus_connection_set_max_received_size().
3687 * @param connection the connection
3688 * @returns the max size of all live messages
3691 dbus_connection_get_max_received_size (DBusConnection *connection)
3695 _dbus_return_val_if_fail (connection != NULL, 0);
3697 CONNECTION_LOCK (connection);
3698 res = _dbus_transport_get_max_received_size (connection->transport);
3699 CONNECTION_UNLOCK (connection);
3704 * Gets the approximate size in bytes of all messages in the outgoing
3705 * message queue. The size is approximate in that you shouldn't use
3706 * it to decide how many bytes to read off the network or anything
3707 * of that nature, as optimizations may choose to tell small white lies
3708 * to avoid performance overhead.
3710 * @param connection the connection
3711 * @returns the number of bytes that have been queued up but not sent
3714 dbus_connection_get_outgoing_size (DBusConnection *connection)
3718 _dbus_return_val_if_fail (connection != NULL, 0);
3720 CONNECTION_LOCK (connection);
3721 res = _dbus_counter_get_value (connection->outgoing_counter);
3722 CONNECTION_UNLOCK (connection);