1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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-connection.h"
26 #include "dbus-list.h"
27 #include "dbus-timeout.h"
28 #include "dbus-transport.h"
29 #include "dbus-watch.h"
30 #include "dbus-connection-internal.h"
31 #include "dbus-list.h"
32 #include "dbus-hash.h"
33 #include "dbus-message-internal.h"
34 #include "dbus-threads.h"
35 #include "dbus-protocol.h"
36 #include "dbus-dataslot.h"
37 #include "dbus-string.h"
38 #include "dbus-pending-call.h"
39 #include "dbus-object-tree.h"
42 #define CONNECTION_LOCK(connection) do { \
43 _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); \
44 dbus_mutex_lock ((connection)->mutex); \
46 #define CONNECTION_UNLOCK(connection) do { \
47 _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); \
48 dbus_mutex_unlock ((connection)->mutex); \
51 #define CONNECTION_LOCK(connection) dbus_mutex_lock ((connection)->mutex)
52 #define CONNECTION_UNLOCK(connection) dbus_mutex_unlock ((connection)->mutex)
56 * @defgroup DBusConnection DBusConnection
58 * @brief Connection to another application
60 * A DBusConnection represents a connection to another
61 * application. Messages can be sent and received via this connection.
62 * The other application may be a message bus; for convenience, the
63 * function dbus_bus_get() is provided to automatically open a
64 * connection to the well-known message buses.
66 * In brief a DBusConnection is a message queue associated with some
67 * message transport mechanism such as a socket. The connection
68 * maintains a queue of incoming messages and a queue of outgoing
71 * Incoming messages are normally processed by calling
72 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
73 * handlers registered for the topmost message in the message queue,
74 * then discards the message, then returns.
76 * dbus_connection_get_dispatch_status() indicates whether
77 * messages are currently in the queue that need dispatching.
78 * dbus_connection_set_dispatch_status_function() allows
79 * you to set a function to be used to monitor the dispatch status.
81 * If you're using GLib or Qt add-on libraries for D-BUS, there are
82 * special convenience APIs in those libraries that hide
83 * all the details of dispatch and watch/timeout monitoring.
84 * For example, dbus_connection_setup_with_g_main().
86 * If you aren't using these add-on libraries, you have to manually
87 * call dbus_connection_set_dispatch_status_function(),
88 * dbus_connection_set_watch_functions(),
89 * dbus_connection_set_timeout_functions() providing appropriate
90 * functions to integrate the connection with your application's main
93 * When you use dbus_connection_send() or one of its variants to send
94 * a message, the message is added to the outgoing queue. It's
95 * actually written to the network later; either in
96 * dbus_watch_handle() invoked by your main loop, or in
97 * dbus_connection_flush() which blocks until it can write out the
98 * entire outgoing queue. The GLib/Qt add-on libraries again
99 * handle the details here for you by setting up watch functions.
101 * When a connection is disconnected, you are guaranteed to get a
102 * message with the name #DBUS_MESSAGE_LOCAL_DISCONNECT.
104 * You may not drop the last reference to a #DBusConnection
105 * until that connection has been disconnected.
107 * You may dispatch the unprocessed incoming message queue even if the
108 * connection is disconnected. However, #DBUS_MESSAGE_LOCAL_DISCONNECT
109 * will always be the last message in the queue (obviously no messages
110 * are received after disconnection).
112 * #DBusConnection has thread locks and drops them when invoking user
113 * callbacks, so in general is transparently threadsafe. However,
114 * #DBusMessage does NOT have thread locks; you must not send the same
115 * message to multiple #DBusConnection that will be used from
120 * @defgroup DBusConnectionInternals DBusConnection implementation details
121 * @ingroup DBusInternals
122 * @brief Implementation details of DBusConnection
128 * Internal struct representing a message filter function
130 typedef struct DBusMessageFilter DBusMessageFilter;
133 * Internal struct representing a message filter function
135 struct DBusMessageFilter
137 DBusAtomic refcount; /**< Reference count */
138 DBusHandleMessageFunction function; /**< Function to call to filter */
139 void *user_data; /**< User data for the function */
140 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
145 * Internals of DBusPreallocatedSend
147 struct DBusPreallocatedSend
149 DBusConnection *connection; /**< Connection we'd send the message to */
150 DBusList *queue_link; /**< Preallocated link in the queue */
151 DBusList *counter_link; /**< Preallocated link in the resource counter */
154 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
157 * Implementation details of DBusConnection. All fields are private.
159 struct DBusConnection
161 DBusAtomic refcount; /**< Reference count. */
163 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
165 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */
166 DBusCondVar *dispatch_cond; /**< Protects dispatch() */
168 dbus_bool_t io_path_acquired; /**< Protects transport io path */
169 DBusCondVar *io_path_cond; /**< Protects transport io path */
171 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
172 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
174 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
175 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
177 int n_outgoing; /**< Length of outgoing queue. */
178 int n_incoming; /**< Length of incoming queue. */
180 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
182 DBusTransport *transport; /**< Object that sends/receives messages over network. */
183 DBusWatchList *watches; /**< Stores active watches. */
184 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
186 DBusList *filter_list; /**< List of filters. */
188 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
190 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
192 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
193 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
195 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
196 void *wakeup_main_data; /**< Application data for wakeup_main_function */
197 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
199 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
200 void *dispatch_status_data; /**< Application data for dispatch_status_function */
201 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
203 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
205 DBusList *link_cache; /**< A cache of linked list links to prevent contention
206 * for the global linked list mempool lock
208 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
210 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
213 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
214 DBusTimeout *timeout);
215 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
216 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
217 DBusDispatchStatus new_status);
218 static void _dbus_connection_last_unref (DBusConnection *connection);
221 _dbus_message_filter_ref (DBusMessageFilter *filter)
223 _dbus_assert (filter->refcount.value > 0);
224 _dbus_atomic_inc (&filter->refcount);
228 _dbus_message_filter_unref (DBusMessageFilter *filter)
230 _dbus_assert (filter->refcount.value > 0);
232 if (_dbus_atomic_dec (&filter->refcount) == 1)
234 if (filter->free_user_data_function)
235 (* filter->free_user_data_function) (filter->user_data);
242 * Acquires the connection lock.
244 * @param connection the connection.
247 _dbus_connection_lock (DBusConnection *connection)
249 CONNECTION_LOCK (connection);
253 * Releases the connection lock.
255 * @param connection the connection.
258 _dbus_connection_unlock (DBusConnection *connection)
260 CONNECTION_UNLOCK (connection);
264 * Wakes up the main loop if it is sleeping
265 * Needed if we're e.g. queueing outgoing messages
266 * on a thread while the mainloop sleeps.
268 * @param connection the connection.
271 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
273 if (connection->wakeup_main_function)
274 (*connection->wakeup_main_function) (connection->wakeup_main_data);
277 #ifdef DBUS_BUILD_TESTS
278 /* For now this function isn't used */
280 * Adds a message to the incoming message queue, returning #FALSE
281 * if there's insufficient memory to queue the message.
282 * Does not take over refcount of the message.
284 * @param connection the connection.
285 * @param message the message to queue.
286 * @returns #TRUE on success.
289 _dbus_connection_queue_received_message (DBusConnection *connection,
290 DBusMessage *message)
294 link = _dbus_list_alloc_link (message);
298 dbus_message_ref (message);
299 _dbus_connection_queue_received_message_link (connection, link);
306 * Adds a message-containing list link to the incoming message queue,
307 * taking ownership of the link and the message's current refcount.
308 * Cannot fail due to lack of memory.
310 * @param connection the connection.
311 * @param link the message link to queue.
314 _dbus_connection_queue_received_message_link (DBusConnection *connection,
317 DBusPendingCall *pending;
318 dbus_int32_t reply_serial;
319 DBusMessage *message;
321 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
323 _dbus_list_append_link (&connection->incoming_messages,
325 message = link->data;
327 /* If this is a reply we're waiting on, remove timeout for it */
328 reply_serial = dbus_message_get_reply_serial (message);
329 if (reply_serial != -1)
331 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
335 if (pending->timeout_added)
336 _dbus_connection_remove_timeout_locked (connection,
339 pending->timeout_added = FALSE;
343 connection->n_incoming += 1;
345 _dbus_connection_wakeup_mainloop (connection);
347 _dbus_verbose ("Message %p (%d %s '%s') added to incoming queue %p, %d incoming\n",
349 dbus_message_get_type (message),
350 dbus_message_get_interface (message) ?
351 dbus_message_get_interface (message) :
353 dbus_message_get_signature (message),
355 connection->n_incoming);
359 * Adds a link + message to the incoming message queue.
360 * Can't fail. Takes ownership of both link and message.
362 * @param connection the connection.
363 * @param link the list node and message to queue.
365 * @todo This needs to wake up the mainloop if it is in
366 * a poll/select and this is a multithreaded app.
369 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
372 _dbus_list_append_link (&connection->incoming_messages, link);
374 connection->n_incoming += 1;
376 _dbus_connection_wakeup_mainloop (connection);
378 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
379 link->data, connection, connection->n_incoming);
384 * Checks whether there are messages in the outgoing message queue.
386 * @param connection the connection.
387 * @returns #TRUE if the outgoing queue is non-empty.
390 _dbus_connection_have_messages_to_send (DBusConnection *connection)
392 return connection->outgoing_messages != NULL;
396 * Gets the next outgoing message. The message remains in the
397 * queue, and the caller does not own a reference to it.
399 * @param connection the connection.
400 * @returns the message to be sent.
403 _dbus_connection_get_message_to_send (DBusConnection *connection)
405 return _dbus_list_get_last (&connection->outgoing_messages);
409 * Notifies the connection that a message has been sent, so the
410 * message can be removed from the outgoing queue.
411 * Called with the connection lock held.
413 * @param connection the connection.
414 * @param message the message that was sent.
417 _dbus_connection_message_sent (DBusConnection *connection,
418 DBusMessage *message)
422 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
424 link = _dbus_list_get_last_link (&connection->outgoing_messages);
425 _dbus_assert (link != NULL);
426 _dbus_assert (link->data == message);
428 /* Save this link in the link cache */
429 _dbus_list_unlink (&connection->outgoing_messages,
431 _dbus_list_prepend_link (&connection->link_cache, link);
433 connection->n_outgoing -= 1;
435 _dbus_verbose ("Message %p (%d %s '%s') removed from outgoing queue %p, %d left to send\n",
437 dbus_message_get_type (message),
438 dbus_message_get_interface (message) ?
439 dbus_message_get_interface (message) :
441 dbus_message_get_signature (message),
442 connection, connection->n_outgoing);
444 /* Save this link in the link cache also */
445 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
447 _dbus_list_prepend_link (&connection->link_cache, link);
449 dbus_message_unref (message);
451 if (connection->n_outgoing == 0)
452 _dbus_transport_messages_pending (connection->transport,
453 connection->n_outgoing);
457 * Adds a watch using the connection's DBusAddWatchFunction if
458 * available. Otherwise records the watch to be added when said
459 * function is available. Also re-adds the watch if the
460 * DBusAddWatchFunction changes. May fail due to lack of memory.
462 * @param connection the connection.
463 * @param watch the watch to add.
464 * @returns #TRUE on success.
467 _dbus_connection_add_watch (DBusConnection *connection,
470 if (connection->watches) /* null during finalize */
471 return _dbus_watch_list_add_watch (connection->watches,
478 * Removes a watch using the connection's DBusRemoveWatchFunction
479 * if available. It's an error to call this function on a watch
480 * that was not previously added.
482 * @param connection the connection.
483 * @param watch the watch to remove.
486 _dbus_connection_remove_watch (DBusConnection *connection,
489 if (connection->watches) /* null during finalize */
490 _dbus_watch_list_remove_watch (connection->watches,
495 * Toggles a watch and notifies app via connection's
496 * DBusWatchToggledFunction if available. It's an error to call this
497 * function on a watch that was not previously added.
499 * @param connection the connection.
500 * @param watch the watch to toggle.
501 * @param enabled whether to enable or disable
504 _dbus_connection_toggle_watch (DBusConnection *connection,
508 if (connection->watches) /* null during finalize */
509 _dbus_watch_list_toggle_watch (connection->watches,
514 * Adds a timeout using the connection's DBusAddTimeoutFunction if
515 * available. Otherwise records the timeout to be added when said
516 * function is available. Also re-adds the timeout if the
517 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
518 * The timeout will fire repeatedly until removed.
520 * @param connection the connection.
521 * @param timeout the timeout to add.
522 * @returns #TRUE on success.
525 _dbus_connection_add_timeout (DBusConnection *connection,
526 DBusTimeout *timeout)
528 if (connection->timeouts) /* null during finalize */
529 return _dbus_timeout_list_add_timeout (connection->timeouts,
536 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
537 * if available. It's an error to call this function on a timeout
538 * that was not previously added.
540 * @param connection the connection.
541 * @param timeout the timeout to remove.
544 _dbus_connection_remove_timeout (DBusConnection *connection,
545 DBusTimeout *timeout)
547 if (connection->timeouts) /* null during finalize */
548 _dbus_timeout_list_remove_timeout (connection->timeouts,
553 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
554 DBusTimeout *timeout)
556 CONNECTION_LOCK (connection);
557 _dbus_connection_remove_timeout (connection, timeout);
558 CONNECTION_UNLOCK (connection);
562 * Toggles a timeout and notifies app via connection's
563 * DBusTimeoutToggledFunction if available. It's an error to call this
564 * function on a timeout that was not previously added.
566 * @param connection the connection.
567 * @param timeout the timeout to toggle.
568 * @param enabled whether to enable or disable
571 _dbus_connection_toggle_timeout (DBusConnection *connection,
572 DBusTimeout *timeout,
575 if (connection->timeouts) /* null during finalize */
576 _dbus_timeout_list_toggle_timeout (connection->timeouts,
581 * Tells the connection that the transport has been disconnected.
582 * Results in posting a disconnect message on the incoming message
583 * queue. Only has an effect the first time it's called.
585 * @param connection the connection
588 _dbus_connection_notify_disconnected (DBusConnection *connection)
590 if (connection->disconnect_message_link)
592 /* We haven't sent the disconnect message already */
593 _dbus_connection_queue_synthesized_message_link (connection,
594 connection->disconnect_message_link);
595 connection->disconnect_message_link = NULL;
600 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
601 DBusPendingCall *pending)
603 _dbus_assert (pending->reply_serial != 0);
605 if (!_dbus_connection_add_timeout (connection, pending->timeout))
608 if (!_dbus_hash_table_insert_int (connection->pending_replies,
609 pending->reply_serial,
612 _dbus_connection_remove_timeout (connection, pending->timeout);
616 pending->timeout_added = TRUE;
617 pending->connection = connection;
619 dbus_pending_call_ref (pending);
625 free_pending_call_on_hash_removal (void *data)
627 DBusPendingCall *pending;
634 if (pending->connection)
636 if (pending->timeout_added)
638 _dbus_connection_remove_timeout (pending->connection,
640 pending->timeout_added = FALSE;
643 pending->connection = NULL;
645 dbus_pending_call_unref (pending);
650 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
651 DBusPendingCall *pending)
653 /* The idea here is to avoid finalizing the pending call
654 * with the lock held, since there's a destroy notifier
655 * in pending call that goes out to application code.
657 dbus_pending_call_ref (pending);
658 _dbus_hash_table_remove_int (connection->pending_replies,
659 pending->reply_serial);
660 CONNECTION_UNLOCK (connection);
661 dbus_pending_call_unref (pending);
665 * Removes a pending call from the connection, such that
666 * the pending reply will be ignored. May drop the last
667 * reference to the pending call.
669 * @param connection the connection
670 * @param pending the pending call
673 _dbus_connection_remove_pending_call (DBusConnection *connection,
674 DBusPendingCall *pending)
676 CONNECTION_LOCK (connection);
677 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
681 * Completes a pending call with the given message,
682 * or if the message is #NULL, by timing out the pending call.
684 * @param pending the pending call
685 * @param message the message to complete the call with, or #NULL
686 * to time out the call
689 _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
690 DBusMessage *message)
694 message = pending->timeout_link->data;
695 _dbus_list_clear (&pending->timeout_link);
698 dbus_message_ref (message);
700 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
702 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
704 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
705 "error" : "other type",
706 pending->reply_serial);
708 _dbus_assert (pending->reply == NULL);
709 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
710 pending->reply = message;
712 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
713 _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending);
715 /* Must be called unlocked since it invokes app callback */
716 _dbus_pending_call_notify (pending);
717 dbus_pending_call_unref (pending);
721 * Acquire the transporter I/O path. This must be done before
722 * doing any I/O in the transporter. May sleep and drop the
723 * connection mutex while waiting for the I/O path.
725 * @param connection the connection.
726 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
727 * @returns TRUE if the I/O path was acquired.
730 _dbus_connection_acquire_io_path (DBusConnection *connection,
731 int timeout_milliseconds)
733 dbus_bool_t res = TRUE;
735 if (connection->io_path_acquired)
737 if (timeout_milliseconds != -1)
738 res = dbus_condvar_wait_timeout (connection->io_path_cond,
740 timeout_milliseconds);
742 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
747 _dbus_assert (!connection->io_path_acquired);
749 connection->io_path_acquired = TRUE;
756 * Release the I/O path when you're done with it. Only call
757 * after you've acquired the I/O. Wakes up at most one thread
758 * currently waiting to acquire the I/O path.
760 * @param connection the connection.
763 _dbus_connection_release_io_path (DBusConnection *connection)
765 _dbus_assert (connection->io_path_acquired);
767 connection->io_path_acquired = FALSE;
768 dbus_condvar_wake_one (connection->io_path_cond);
773 * Queues incoming messages and sends outgoing messages for this
774 * connection, optionally blocking in the process. Each call to
775 * _dbus_connection_do_iteration() will call select() or poll() one
776 * time and then read or write data if possible.
778 * The purpose of this function is to be able to flush outgoing
779 * messages or queue up incoming messages without returning
780 * control to the application and causing reentrancy weirdness.
782 * The flags parameter allows you to specify whether to
783 * read incoming messages, write outgoing messages, or both,
784 * and whether to block if no immediate action is possible.
786 * The timeout_milliseconds parameter does nothing unless the
787 * iteration is blocking.
789 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
790 * wasn't specified, then it's impossible to block, even if
791 * you specify DBUS_ITERATION_BLOCK; in that case the function
792 * returns immediately.
794 * @param connection the connection.
795 * @param flags iteration flags.
796 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
799 _dbus_connection_do_iteration (DBusConnection *connection,
801 int timeout_milliseconds)
803 if (connection->n_outgoing == 0)
804 flags &= ~DBUS_ITERATION_DO_WRITING;
806 if (_dbus_connection_acquire_io_path (connection,
807 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
809 _dbus_transport_do_iteration (connection->transport,
810 flags, timeout_milliseconds);
811 _dbus_connection_release_io_path (connection);
816 * Creates a new connection for the given transport. A transport
817 * represents a message stream that uses some concrete mechanism, such
818 * as UNIX domain sockets. May return #NULL if insufficient
819 * memory exists to create the connection.
821 * @param transport the transport.
822 * @returns the new connection, or #NULL on failure.
825 _dbus_connection_new_for_transport (DBusTransport *transport)
827 DBusConnection *connection;
828 DBusWatchList *watch_list;
829 DBusTimeoutList *timeout_list;
830 DBusHashTable *pending_replies;
832 DBusCondVar *message_returned_cond;
833 DBusCondVar *dispatch_cond;
834 DBusCondVar *io_path_cond;
835 DBusList *disconnect_link;
836 DBusMessage *disconnect_message;
837 DBusCounter *outgoing_counter;
838 DBusObjectTree *objects;
842 pending_replies = NULL;
845 message_returned_cond = NULL;
846 dispatch_cond = NULL;
848 disconnect_link = NULL;
849 disconnect_message = NULL;
850 outgoing_counter = NULL;
853 watch_list = _dbus_watch_list_new ();
854 if (watch_list == NULL)
857 timeout_list = _dbus_timeout_list_new ();
858 if (timeout_list == NULL)
862 _dbus_hash_table_new (DBUS_HASH_INT,
864 (DBusFreeFunction)free_pending_call_on_hash_removal);
865 if (pending_replies == NULL)
868 connection = dbus_new0 (DBusConnection, 1);
869 if (connection == NULL)
872 mutex = dbus_mutex_new ();
876 message_returned_cond = dbus_condvar_new ();
877 if (message_returned_cond == NULL)
880 dispatch_cond = dbus_condvar_new ();
881 if (dispatch_cond == NULL)
884 io_path_cond = dbus_condvar_new ();
885 if (io_path_cond == NULL)
888 disconnect_message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_LOCAL,
889 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
892 if (disconnect_message == NULL)
895 disconnect_link = _dbus_list_alloc_link (disconnect_message);
896 if (disconnect_link == NULL)
899 outgoing_counter = _dbus_counter_new ();
900 if (outgoing_counter == NULL)
903 objects = _dbus_object_tree_new (connection);
907 if (_dbus_modify_sigpipe)
908 _dbus_disable_sigpipe ();
910 connection->refcount.value = 1;
911 connection->mutex = mutex;
912 connection->dispatch_cond = dispatch_cond;
913 connection->io_path_cond = io_path_cond;
914 connection->message_returned_cond = message_returned_cond;
915 connection->transport = transport;
916 connection->watches = watch_list;
917 connection->timeouts = timeout_list;
918 connection->pending_replies = pending_replies;
919 connection->outgoing_counter = outgoing_counter;
920 connection->filter_list = NULL;
921 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
922 connection->objects = objects;
923 connection->exit_on_disconnect = FALSE;
925 _dbus_data_slot_list_init (&connection->slot_list);
927 connection->client_serial = 1;
929 connection->disconnect_message_link = disconnect_link;
931 if (!_dbus_transport_set_connection (transport, connection))
934 _dbus_transport_ref (transport);
939 if (disconnect_message != NULL)
940 dbus_message_unref (disconnect_message);
942 if (disconnect_link != NULL)
943 _dbus_list_free_link (disconnect_link);
945 if (io_path_cond != NULL)
946 dbus_condvar_free (io_path_cond);
948 if (dispatch_cond != NULL)
949 dbus_condvar_free (dispatch_cond);
951 if (message_returned_cond != NULL)
952 dbus_condvar_free (message_returned_cond);
955 dbus_mutex_free (mutex);
957 if (connection != NULL)
958 dbus_free (connection);
961 _dbus_hash_table_unref (pending_replies);
964 _dbus_watch_list_free (watch_list);
967 _dbus_timeout_list_free (timeout_list);
969 if (outgoing_counter)
970 _dbus_counter_unref (outgoing_counter);
973 _dbus_object_tree_unref (objects);
979 * Increments the reference count of a DBusConnection.
980 * Requires that the caller already holds the connection lock.
982 * @param connection the connection.
985 _dbus_connection_ref_unlocked (DBusConnection *connection)
987 #ifdef DBUS_HAVE_ATOMIC_INT
988 _dbus_atomic_inc (&connection->refcount);
990 _dbus_assert (connection->refcount.value > 0);
991 connection->refcount.value += 1;
996 * Decrements the reference count of a DBusConnection.
997 * Requires that the caller already holds the connection lock.
999 * @param connection the connection.
1002 _dbus_connection_unref_unlocked (DBusConnection *connection)
1004 dbus_bool_t last_unref;
1006 _dbus_return_if_fail (connection != NULL);
1008 /* The connection lock is better than the global
1009 * lock in the atomic increment fallback
1012 #ifdef DBUS_HAVE_ATOMIC_INT
1013 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1015 _dbus_assert (connection->refcount.value > 0);
1017 connection->refcount.value -= 1;
1018 last_unref = (connection->refcount.value == 0);
1020 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1025 _dbus_connection_last_unref (connection);
1028 static dbus_uint32_t
1029 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1033 serial = connection->client_serial++;
1035 if (connection->client_serial < 0)
1036 connection->client_serial = 1;
1042 * A callback for use with dbus_watch_new() to create a DBusWatch.
1044 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1045 * and the virtual handle_watch in DBusTransport if we got rid of it.
1046 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1049 * @param watch the watch.
1050 * @param condition the current condition of the file descriptors being watched.
1051 * @param data must be a pointer to a #DBusConnection
1052 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1055 _dbus_connection_handle_watch (DBusWatch *watch,
1056 unsigned int condition,
1059 DBusConnection *connection;
1061 DBusDispatchStatus status;
1065 CONNECTION_LOCK (connection);
1066 _dbus_connection_acquire_io_path (connection, -1);
1067 retval = _dbus_transport_handle_watch (connection->transport,
1069 _dbus_connection_release_io_path (connection);
1071 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1073 /* this calls out to user code */
1074 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1082 * @addtogroup DBusConnection
1088 * Opens a new connection to a remote address.
1090 * @todo specify what the address parameter is. Right now
1091 * it's just the name of a UNIX domain socket. It should be
1092 * something more complex that encodes which transport to use.
1094 * If the open fails, the function returns #NULL, and provides
1095 * a reason for the failure in the result parameter. Pass
1096 * #NULL for the result parameter if you aren't interested
1097 * in the reason for failure.
1099 * @param address the address.
1100 * @param error address where an error can be returned.
1101 * @returns new connection, or #NULL on failure.
1104 dbus_connection_open (const char *address,
1107 DBusConnection *connection;
1108 DBusTransport *transport;
1110 _dbus_return_val_if_fail (address != NULL, NULL);
1111 _dbus_return_val_if_error_is_set (error, NULL);
1113 transport = _dbus_transport_open (address, error);
1114 if (transport == NULL)
1116 _DBUS_ASSERT_ERROR_IS_SET (error);
1120 connection = _dbus_connection_new_for_transport (transport);
1122 _dbus_transport_unref (transport);
1124 if (connection == NULL)
1126 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1134 * Increments the reference count of a DBusConnection.
1136 * @param connection the connection.
1139 dbus_connection_ref (DBusConnection *connection)
1141 _dbus_return_if_fail (connection != NULL);
1143 /* The connection lock is better than the global
1144 * lock in the atomic increment fallback
1147 #ifdef DBUS_HAVE_ATOMIC_INT
1148 _dbus_atomic_inc (&connection->refcount);
1150 CONNECTION_LOCK (connection);
1151 _dbus_assert (connection->refcount.value > 0);
1153 connection->refcount.value += 1;
1154 CONNECTION_UNLOCK (connection);
1159 free_outgoing_message (void *element,
1162 DBusMessage *message = element;
1163 DBusConnection *connection = data;
1165 _dbus_message_remove_size_counter (message,
1166 connection->outgoing_counter,
1168 dbus_message_unref (message);
1171 /* This is run without the mutex held, but after the last reference
1172 * to the connection has been dropped we should have no thread-related
1176 _dbus_connection_last_unref (DBusConnection *connection)
1180 _dbus_verbose ("Finalizing connection %p\n", connection);
1182 _dbus_assert (connection->refcount.value == 0);
1184 /* You have to disconnect the connection before unref:ing it. Otherwise
1185 * you won't get the disconnected message.
1187 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1189 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1190 _dbus_object_tree_free_all_unlocked (connection->objects);
1192 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1193 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1194 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1196 _dbus_watch_list_free (connection->watches);
1197 connection->watches = NULL;
1199 _dbus_timeout_list_free (connection->timeouts);
1200 connection->timeouts = NULL;
1202 _dbus_data_slot_list_free (&connection->slot_list);
1204 link = _dbus_list_get_first_link (&connection->filter_list);
1205 while (link != NULL)
1207 DBusMessageFilter *filter = link->data;
1208 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1210 filter->function = NULL;
1211 _dbus_message_filter_unref (filter); /* calls app callback */
1216 _dbus_list_clear (&connection->filter_list);
1218 /* ---- Done with stuff that invokes application callbacks */
1220 _dbus_object_tree_unref (connection->objects);
1222 _dbus_hash_table_unref (connection->pending_replies);
1223 connection->pending_replies = NULL;
1225 _dbus_list_clear (&connection->filter_list);
1227 _dbus_list_foreach (&connection->outgoing_messages,
1228 free_outgoing_message,
1230 _dbus_list_clear (&connection->outgoing_messages);
1232 _dbus_list_foreach (&connection->incoming_messages,
1233 (DBusForeachFunction) dbus_message_unref,
1235 _dbus_list_clear (&connection->incoming_messages);
1237 _dbus_counter_unref (connection->outgoing_counter);
1239 _dbus_transport_unref (connection->transport);
1241 if (connection->disconnect_message_link)
1243 DBusMessage *message = connection->disconnect_message_link->data;
1244 dbus_message_unref (message);
1245 _dbus_list_free_link (connection->disconnect_message_link);
1248 _dbus_list_clear (&connection->link_cache);
1250 dbus_condvar_free (connection->dispatch_cond);
1251 dbus_condvar_free (connection->io_path_cond);
1252 dbus_condvar_free (connection->message_returned_cond);
1254 dbus_mutex_free (connection->mutex);
1256 dbus_free (connection);
1260 * Decrements the reference count of a DBusConnection, and finalizes
1261 * it if the count reaches zero. It is a bug to drop the last reference
1262 * to a connection that has not been disconnected.
1264 * @todo in practice it can be quite tricky to never unref a connection
1265 * that's still connected; maybe there's some way we could avoid
1268 * @param connection the connection.
1271 dbus_connection_unref (DBusConnection *connection)
1273 dbus_bool_t last_unref;
1275 _dbus_return_if_fail (connection != NULL);
1277 /* The connection lock is better than the global
1278 * lock in the atomic increment fallback
1281 #ifdef DBUS_HAVE_ATOMIC_INT
1282 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1284 CONNECTION_LOCK (connection);
1286 _dbus_assert (connection->refcount.value > 0);
1288 connection->refcount.value -= 1;
1289 last_unref = (connection->refcount.value == 0);
1292 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1295 CONNECTION_UNLOCK (connection);
1299 _dbus_connection_last_unref (connection);
1303 * Closes the connection, so no further data can be sent or received.
1304 * Any further attempts to send data will result in errors. This
1305 * function does not affect the connection's reference count. It's
1306 * safe to disconnect a connection more than once; all calls after the
1307 * first do nothing. It's impossible to "reconnect" a connection, a
1308 * new connection must be created.
1310 * @param connection the connection.
1313 dbus_connection_disconnect (DBusConnection *connection)
1315 _dbus_return_if_fail (connection != NULL);
1317 CONNECTION_LOCK (connection);
1318 _dbus_transport_disconnect (connection->transport);
1319 CONNECTION_UNLOCK (connection);
1323 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1325 return _dbus_transport_get_is_connected (connection->transport);
1329 * Gets whether the connection is currently connected. All
1330 * connections are connected when they are opened. A connection may
1331 * become disconnected when the remote application closes its end, or
1332 * exits; a connection may also be disconnected with
1333 * dbus_connection_disconnect().
1335 * @param connection the connection.
1336 * @returns #TRUE if the connection is still alive.
1339 dbus_connection_get_is_connected (DBusConnection *connection)
1343 _dbus_return_val_if_fail (connection != NULL, FALSE);
1345 CONNECTION_LOCK (connection);
1346 res = _dbus_connection_get_is_connected_unlocked (connection);
1347 CONNECTION_UNLOCK (connection);
1353 * Gets whether the connection was authenticated. (Note that
1354 * if the connection was authenticated then disconnected,
1355 * this function still returns #TRUE)
1357 * @param connection the connection
1358 * @returns #TRUE if the connection was ever authenticated
1361 dbus_connection_get_is_authenticated (DBusConnection *connection)
1365 _dbus_return_val_if_fail (connection != NULL, FALSE);
1367 CONNECTION_LOCK (connection);
1368 res = _dbus_transport_get_is_authenticated (connection->transport);
1369 CONNECTION_UNLOCK (connection);
1375 * Set whether _exit() should be called when the connection receives a
1376 * disconnect signal. The call to _exit() comes after any handlers for
1377 * the disconnect signal run; handlers can cancel the exit by calling
1380 * By default, exit_on_disconnect is #FALSE; but for message bus
1381 * connections returned from dbus_bus_get() it will be toggled on
1384 * @param connection the connection
1385 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
1388 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
1389 dbus_bool_t exit_on_disconnect)
1391 _dbus_return_if_fail (connection != NULL);
1393 CONNECTION_LOCK (connection);
1394 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
1395 CONNECTION_UNLOCK (connection);
1398 static DBusPreallocatedSend*
1399 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1401 DBusPreallocatedSend *preallocated;
1403 _dbus_return_val_if_fail (connection != NULL, NULL);
1405 preallocated = dbus_new (DBusPreallocatedSend, 1);
1406 if (preallocated == NULL)
1409 if (connection->link_cache != NULL)
1411 preallocated->queue_link =
1412 _dbus_list_pop_first_link (&connection->link_cache);
1413 preallocated->queue_link->data = NULL;
1417 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1418 if (preallocated->queue_link == NULL)
1422 if (connection->link_cache != NULL)
1424 preallocated->counter_link =
1425 _dbus_list_pop_first_link (&connection->link_cache);
1426 preallocated->counter_link->data = connection->outgoing_counter;
1430 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1431 if (preallocated->counter_link == NULL)
1435 _dbus_counter_ref (preallocated->counter_link->data);
1437 preallocated->connection = connection;
1439 return preallocated;
1442 _dbus_list_free_link (preallocated->queue_link);
1444 dbus_free (preallocated);
1450 * Preallocates resources needed to send a message, allowing the message
1451 * to be sent without the possibility of memory allocation failure.
1452 * Allows apps to create a future guarantee that they can send
1453 * a message regardless of memory shortages.
1455 * @param connection the connection we're preallocating for.
1456 * @returns the preallocated resources, or #NULL
1458 DBusPreallocatedSend*
1459 dbus_connection_preallocate_send (DBusConnection *connection)
1461 DBusPreallocatedSend *preallocated;
1463 _dbus_return_val_if_fail (connection != NULL, NULL);
1465 CONNECTION_LOCK (connection);
1468 _dbus_connection_preallocate_send_unlocked (connection);
1470 CONNECTION_UNLOCK (connection);
1472 return preallocated;
1476 * Frees preallocated message-sending resources from
1477 * dbus_connection_preallocate_send(). Should only
1478 * be called if the preallocated resources are not used
1479 * to send a message.
1481 * @param connection the connection
1482 * @param preallocated the resources
1485 dbus_connection_free_preallocated_send (DBusConnection *connection,
1486 DBusPreallocatedSend *preallocated)
1488 _dbus_return_if_fail (connection != NULL);
1489 _dbus_return_if_fail (preallocated != NULL);
1490 _dbus_return_if_fail (connection == preallocated->connection);
1492 _dbus_list_free_link (preallocated->queue_link);
1493 _dbus_counter_unref (preallocated->counter_link->data);
1494 _dbus_list_free_link (preallocated->counter_link);
1495 dbus_free (preallocated);
1499 _dbus_connection_send_preallocated_unlocked (DBusConnection *connection,
1500 DBusPreallocatedSend *preallocated,
1501 DBusMessage *message,
1502 dbus_uint32_t *client_serial)
1504 dbus_uint32_t serial;
1506 preallocated->queue_link->data = message;
1507 _dbus_list_prepend_link (&connection->outgoing_messages,
1508 preallocated->queue_link);
1510 _dbus_message_add_size_counter_link (message,
1511 preallocated->counter_link);
1513 dbus_free (preallocated);
1514 preallocated = NULL;
1516 dbus_message_ref (message);
1518 connection->n_outgoing += 1;
1520 _dbus_verbose ("Message %p (%d %s '%s') added to outgoing queue %p, %d pending to send\n",
1522 dbus_message_get_type (message),
1523 dbus_message_get_interface (message) ?
1524 dbus_message_get_interface (message) :
1526 dbus_message_get_signature (message),
1528 connection->n_outgoing);
1530 if (dbus_message_get_serial (message) == 0)
1532 serial = _dbus_connection_get_next_client_serial (connection);
1533 _dbus_message_set_serial (message, serial);
1535 *client_serial = serial;
1540 *client_serial = dbus_message_get_serial (message);
1543 _dbus_message_lock (message);
1545 if (connection->n_outgoing == 1)
1546 _dbus_transport_messages_pending (connection->transport,
1547 connection->n_outgoing);
1549 _dbus_connection_wakeup_mainloop (connection);
1553 * Sends a message using preallocated resources. This function cannot fail.
1554 * It works identically to dbus_connection_send() in other respects.
1555 * Preallocated resources comes from dbus_connection_preallocate_send().
1556 * This function "consumes" the preallocated resources, they need not
1557 * be freed separately.
1559 * @param connection the connection
1560 * @param preallocated the preallocated resources
1561 * @param message the message to send
1562 * @param client_serial return location for client serial assigned to the message
1565 dbus_connection_send_preallocated (DBusConnection *connection,
1566 DBusPreallocatedSend *preallocated,
1567 DBusMessage *message,
1568 dbus_uint32_t *client_serial)
1570 _dbus_return_if_fail (connection != NULL);
1571 _dbus_return_if_fail (preallocated != NULL);
1572 _dbus_return_if_fail (message != NULL);
1573 _dbus_return_if_fail (preallocated->connection == connection);
1574 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
1575 (dbus_message_get_interface (message) != NULL &&
1576 dbus_message_get_member (message) != NULL));
1577 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
1578 (dbus_message_get_interface (message) != NULL &&
1579 dbus_message_get_member (message) != NULL));
1581 CONNECTION_LOCK (connection);
1582 _dbus_connection_send_preallocated_unlocked (connection,
1584 message, client_serial);
1585 CONNECTION_UNLOCK (connection);
1589 _dbus_connection_send_unlocked (DBusConnection *connection,
1590 DBusMessage *message,
1591 dbus_uint32_t *client_serial)
1593 DBusPreallocatedSend *preallocated;
1595 _dbus_assert (connection != NULL);
1596 _dbus_assert (message != NULL);
1598 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
1599 if (preallocated == NULL)
1603 _dbus_connection_send_preallocated_unlocked (connection,
1611 * Adds a message to the outgoing message queue. Does not block to
1612 * write the message to the network; that happens asynchronously. To
1613 * force the message to be written, call dbus_connection_flush().
1614 * Because this only queues the message, the only reason it can
1615 * fail is lack of memory. Even if the connection is disconnected,
1616 * no error will be returned.
1618 * If the function fails due to lack of memory, it returns #FALSE.
1619 * The function will never fail for other reasons; even if the
1620 * connection is disconnected, you can queue an outgoing message,
1621 * though obviously it won't be sent.
1623 * @param connection the connection.
1624 * @param message the message to write.
1625 * @param client_serial return location for client serial.
1626 * @returns #TRUE on success.
1629 dbus_connection_send (DBusConnection *connection,
1630 DBusMessage *message,
1631 dbus_uint32_t *client_serial)
1633 _dbus_return_val_if_fail (connection != NULL, FALSE);
1634 _dbus_return_val_if_fail (message != NULL, FALSE);
1636 CONNECTION_LOCK (connection);
1638 if (!_dbus_connection_send_unlocked (connection, message, client_serial))
1640 CONNECTION_UNLOCK (connection);
1644 CONNECTION_UNLOCK (connection);
1649 reply_handler_timeout (void *data)
1651 DBusConnection *connection;
1652 DBusDispatchStatus status;
1653 DBusPendingCall *pending = data;
1655 connection = pending->connection;
1657 CONNECTION_LOCK (connection);
1658 if (pending->timeout_link)
1660 _dbus_connection_queue_synthesized_message_link (connection,
1661 pending->timeout_link);
1662 pending->timeout_link = NULL;
1665 _dbus_connection_remove_timeout (connection,
1667 pending->timeout_added = FALSE;
1669 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1671 /* Unlocks, and calls out to user code */
1672 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1678 * Queues a message to send, as with dbus_connection_send_message(),
1679 * but also returns a #DBusPendingCall used to receive a reply to the
1680 * message. If no reply is received in the given timeout_milliseconds,
1681 * this function expires the pending reply and generates a synthetic
1682 * error reply (generated in-process, not by the remote application)
1683 * indicating that a timeout occurred.
1685 * A #DBusPendingCall will see a reply message after any filters, but
1686 * before any object instances or other handlers. A #DBusPendingCall
1687 * will always see exactly one reply message, unless it's cancelled
1688 * with dbus_pending_call_cancel().
1690 * If a filter filters out the reply before the handler sees it, the
1691 * reply is immediately timed out and a timeout error reply is
1692 * generated. If a filter removes the timeout error reply then the
1693 * #DBusPendingCall will get confused. Filtering the timeout error
1694 * is thus considered a bug and will print a warning.
1696 * If #NULL is passed for the pending_return, the #DBusPendingCall
1697 * will still be generated internally, and used to track
1698 * the message reply timeout. This means a timeout error will
1699 * occur if no reply arrives, unlike with dbus_connection_send().
1701 * If -1 is passed for the timeout, a sane default timeout is used. -1
1702 * is typically the best value for the timeout for this reason, unless
1703 * you want a very short or very long timeout. There is no way to
1704 * avoid a timeout entirely, other than passing INT_MAX for the
1705 * timeout to postpone it indefinitely.
1707 * @param connection the connection
1708 * @param message the message to send
1709 * @param pending_return return location for a #DBusPendingCall object, or #NULL
1710 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1711 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1715 dbus_connection_send_with_reply (DBusConnection *connection,
1716 DBusMessage *message,
1717 DBusPendingCall **pending_return,
1718 int timeout_milliseconds)
1720 DBusPendingCall *pending;
1722 DBusList *reply_link;
1723 dbus_int32_t serial = -1;
1725 _dbus_return_val_if_fail (connection != NULL, FALSE);
1726 _dbus_return_val_if_fail (message != NULL, FALSE);
1727 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1730 *pending_return = NULL;
1732 pending = _dbus_pending_call_new (connection,
1733 timeout_milliseconds,
1734 reply_handler_timeout);
1736 if (pending == NULL)
1739 CONNECTION_LOCK (connection);
1741 /* Assign a serial to the message */
1742 if (dbus_message_get_serial (message) == 0)
1744 serial = _dbus_connection_get_next_client_serial (connection);
1745 _dbus_message_set_serial (message, serial);
1748 pending->reply_serial = serial;
1750 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
1751 "No reply within specified time");
1754 CONNECTION_UNLOCK (connection);
1755 dbus_pending_call_unref (pending);
1759 reply_link = _dbus_list_alloc_link (reply);
1762 CONNECTION_UNLOCK (connection);
1763 dbus_message_unref (reply);
1764 dbus_pending_call_unref (pending);
1768 pending->timeout_link = reply_link;
1770 /* Insert the serial in the pending replies hash;
1771 * hash takes a refcount on DBusPendingCall.
1772 * Also, add the timeout.
1774 if (!_dbus_connection_attach_pending_call_unlocked (connection,
1777 CONNECTION_UNLOCK (connection);
1778 dbus_pending_call_unref (pending);
1782 if (!_dbus_connection_send_unlocked (connection, message, NULL))
1784 _dbus_connection_detach_pending_call_and_unlock (connection,
1791 dbus_pending_call_ref (pending);
1792 *pending_return = pending;
1795 CONNECTION_UNLOCK (connection);
1801 check_for_reply_unlocked (DBusConnection *connection,
1802 dbus_uint32_t client_serial)
1806 link = _dbus_list_get_first_link (&connection->incoming_messages);
1808 while (link != NULL)
1810 DBusMessage *reply = link->data;
1812 if (dbus_message_get_reply_serial (reply) == client_serial)
1814 _dbus_list_remove_link (&connection->incoming_messages, link);
1815 connection->n_incoming -= 1;
1816 dbus_message_ref (reply);
1819 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1826 * Blocks a certain time period while waiting for a reply.
1827 * If no reply arrives, returns #NULL.
1829 * @todo could use performance improvements (it keeps scanning
1830 * the whole message queue for example) and has thread issues,
1831 * see comments in source
1833 * Does not re-enter the main loop or run filter/path-registered
1834 * callbacks. The reply to the message will not be seen by
1837 * @param connection the connection
1838 * @param client_serial the reply serial to wait for
1839 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1840 * @returns the message that is the reply or #NULL if no reply
1843 _dbus_connection_block_for_reply (DBusConnection *connection,
1844 dbus_uint32_t client_serial,
1845 int timeout_milliseconds)
1847 long start_tv_sec, start_tv_usec;
1848 long end_tv_sec, end_tv_usec;
1849 long tv_sec, tv_usec;
1850 DBusDispatchStatus status;
1852 _dbus_return_val_if_fail (connection != NULL, NULL);
1853 _dbus_return_val_if_fail (client_serial != 0, NULL);
1854 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1856 if (timeout_milliseconds == -1)
1857 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
1859 /* it would probably seem logical to pass in _DBUS_INT_MAX
1860 * for infinite timeout, but then math below would get
1861 * all overflow-prone, so smack that down.
1863 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1864 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1866 /* Flush message queue */
1867 dbus_connection_flush (connection);
1869 CONNECTION_LOCK (connection);
1871 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1872 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1873 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1874 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1875 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1877 _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",
1878 timeout_milliseconds,
1880 start_tv_sec, start_tv_usec,
1881 end_tv_sec, end_tv_usec);
1883 /* Now we wait... */
1884 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1885 * gets the message before we do?
1887 /* always block at least once as we know we don't have the reply yet */
1888 _dbus_connection_do_iteration (connection,
1889 DBUS_ITERATION_DO_READING |
1890 DBUS_ITERATION_BLOCK,
1891 timeout_milliseconds);
1895 /* queue messages and get status */
1896 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1898 if (status == DBUS_DISPATCH_DATA_REMAINS)
1902 reply = check_for_reply_unlocked (connection, client_serial);
1905 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1907 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
1909 /* Unlocks, and calls out to user code */
1910 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1916 _dbus_get_current_time (&tv_sec, &tv_usec);
1918 if (tv_sec < start_tv_sec)
1919 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
1920 else if (connection->disconnect_message_link == NULL)
1921 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
1922 else if (tv_sec < end_tv_sec ||
1923 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1925 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1926 (end_tv_usec - tv_usec) / 1000;
1927 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1928 _dbus_assert (timeout_milliseconds >= 0);
1930 if (status == DBUS_DISPATCH_NEED_MEMORY)
1932 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1933 * we may already have a reply in the buffer and just can't process
1936 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1938 if (timeout_milliseconds < 100)
1939 ; /* just busy loop */
1940 else if (timeout_milliseconds <= 1000)
1941 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1943 _dbus_sleep_milliseconds (1000);
1947 /* block again, we don't have the reply buffered yet. */
1948 _dbus_connection_do_iteration (connection,
1949 DBUS_ITERATION_DO_READING |
1950 DBUS_ITERATION_BLOCK,
1951 timeout_milliseconds);
1954 goto recheck_status;
1957 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
1958 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
1960 /* unlocks and calls out to user code */
1961 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1967 * Sends a message and blocks a certain time period while waiting for
1968 * a reply. This function does not reenter the main loop,
1969 * i.e. messages other than the reply are queued up but not
1970 * processed. This function is used to do non-reentrant "method
1973 * If a normal reply is received, it is returned, and removed from the
1974 * incoming message queue. If it is not received, #NULL is returned
1975 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
1976 * received, it is converted to a #DBusError and returned as an error,
1977 * then the reply message is deleted. If something else goes wrong,
1978 * result is set to whatever is appropriate, such as
1979 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1981 * @param connection the connection
1982 * @param message the message to send
1983 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1984 * @param error return location for error message
1985 * @returns the message that is the reply or #NULL with an error code if the
1989 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1990 DBusMessage *message,
1991 int timeout_milliseconds,
1994 dbus_uint32_t client_serial;
1997 _dbus_return_val_if_fail (connection != NULL, NULL);
1998 _dbus_return_val_if_fail (message != NULL, NULL);
1999 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2000 _dbus_return_val_if_error_is_set (error, NULL);
2002 if (!dbus_connection_send (connection, message, &client_serial))
2004 _DBUS_SET_OOM (error);
2008 reply = _dbus_connection_block_for_reply (connection,
2010 timeout_milliseconds);
2014 if (dbus_connection_get_is_connected (connection))
2015 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
2017 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
2021 else if (dbus_set_error_from_message (error, reply))
2023 dbus_message_unref (reply);
2031 * Blocks until the outgoing message queue is empty.
2033 * @param connection the connection.
2036 dbus_connection_flush (DBusConnection *connection)
2038 /* We have to specify DBUS_ITERATION_DO_READING here because
2039 * otherwise we could have two apps deadlock if they are both doing
2040 * a flush(), and the kernel buffers fill up. This could change the
2043 DBusDispatchStatus status;
2045 _dbus_return_if_fail (connection != NULL);
2047 CONNECTION_LOCK (connection);
2048 while (connection->n_outgoing > 0 &&
2049 _dbus_connection_get_is_connected_unlocked (connection))
2050 _dbus_connection_do_iteration (connection,
2051 DBUS_ITERATION_DO_READING |
2052 DBUS_ITERATION_DO_WRITING |
2053 DBUS_ITERATION_BLOCK,
2056 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2058 /* Unlocks and calls out to user code */
2059 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2062 /* Call with mutex held. Will drop it while waiting and re-acquire
2066 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
2068 _dbus_assert (connection->message_borrowed != NULL);
2070 while (connection->message_borrowed != NULL)
2071 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
2075 * Returns the first-received message from the incoming message queue,
2076 * leaving it in the queue. If the queue is empty, returns #NULL.
2078 * The caller does not own a reference to the returned message, and
2079 * must either return it using dbus_connection_return_message() or
2080 * keep it after calling dbus_connection_steal_borrowed_message(). No
2081 * one can get at the message while its borrowed, so return it as
2082 * quickly as possible and don't keep a reference to it after
2083 * returning it. If you need to keep the message, make a copy of it.
2085 * @param connection the connection.
2086 * @returns next message in the incoming queue.
2089 dbus_connection_borrow_message (DBusConnection *connection)
2091 DBusMessage *message;
2092 DBusDispatchStatus status;
2094 _dbus_return_val_if_fail (connection != NULL, NULL);
2095 /* can't borrow during dispatch */
2096 _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL);
2098 /* this is called for the side effect that it queues
2099 * up any messages from the transport
2101 status = dbus_connection_get_dispatch_status (connection);
2102 if (status != DBUS_DISPATCH_DATA_REMAINS)
2105 CONNECTION_LOCK (connection);
2107 if (connection->message_borrowed != NULL)
2108 _dbus_connection_wait_for_borrowed (connection);
2110 message = _dbus_list_get_first (&connection->incoming_messages);
2113 connection->message_borrowed = message;
2115 CONNECTION_UNLOCK (connection);
2120 * Used to return a message after peeking at it using
2121 * dbus_connection_borrow_message().
2123 * @param connection the connection
2124 * @param message the message from dbus_connection_borrow_message()
2127 dbus_connection_return_message (DBusConnection *connection,
2128 DBusMessage *message)
2130 _dbus_return_if_fail (connection != NULL);
2131 _dbus_return_if_fail (message != NULL);
2132 /* can't borrow during dispatch */
2133 _dbus_return_if_fail (!connection->dispatch_acquired);
2135 CONNECTION_LOCK (connection);
2137 _dbus_assert (message == connection->message_borrowed);
2139 connection->message_borrowed = NULL;
2140 dbus_condvar_wake_all (connection->message_returned_cond);
2142 CONNECTION_UNLOCK (connection);
2146 * Used to keep a message after peeking at it using
2147 * dbus_connection_borrow_message(). Before using this function, see
2148 * the caveats/warnings in the documentation for
2149 * dbus_connection_pop_message().
2151 * @param connection the connection
2152 * @param message the message from dbus_connection_borrow_message()
2155 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2156 DBusMessage *message)
2158 DBusMessage *pop_message;
2160 _dbus_return_if_fail (connection != NULL);
2161 _dbus_return_if_fail (message != NULL);
2162 /* can't borrow during dispatch */
2163 _dbus_return_if_fail (!connection->dispatch_acquired);
2165 CONNECTION_LOCK (connection);
2167 _dbus_assert (message == connection->message_borrowed);
2169 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
2170 _dbus_assert (message == pop_message);
2172 connection->n_incoming -= 1;
2174 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
2175 message, connection->n_incoming);
2177 connection->message_borrowed = NULL;
2178 dbus_condvar_wake_all (connection->message_returned_cond);
2180 CONNECTION_UNLOCK (connection);
2183 /* See dbus_connection_pop_message, but requires the caller to own
2184 * the lock before calling. May drop the lock while running.
2187 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
2189 if (connection->message_borrowed != NULL)
2190 _dbus_connection_wait_for_borrowed (connection);
2192 if (connection->n_incoming > 0)
2196 link = _dbus_list_pop_first_link (&connection->incoming_messages);
2197 connection->n_incoming -= 1;
2199 _dbus_verbose ("Message %p (%d %s '%s') removed from incoming queue %p, %d incoming\n",
2201 dbus_message_get_type (link->data),
2202 dbus_message_get_interface (link->data) ?
2203 dbus_message_get_interface (link->data) :
2205 dbus_message_get_signature (link->data),
2206 connection, connection->n_incoming);
2214 /* See dbus_connection_pop_message, but requires the caller to own
2215 * the lock before calling. May drop the lock while running.
2218 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
2222 link = _dbus_connection_pop_message_link_unlocked (connection);
2226 DBusMessage *message;
2228 message = link->data;
2230 _dbus_list_free_link (link);
2239 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
2240 DBusList *message_link)
2242 _dbus_assert (message_link != NULL);
2243 /* You can't borrow a message while a link is outstanding */
2244 _dbus_assert (connection->message_borrowed == NULL);
2246 _dbus_list_prepend_link (&connection->incoming_messages,
2248 connection->n_incoming += 1;
2250 _dbus_verbose ("Message %p (%d %s '%s') put back into queue %p, %d incoming\n",
2252 dbus_message_get_type (message_link->data),
2253 dbus_message_get_interface (message_link->data) ?
2254 dbus_message_get_interface (message_link->data) :
2256 dbus_message_get_signature (message_link->data),
2257 connection, connection->n_incoming);
2261 * Returns the first-received message from the incoming message queue,
2262 * removing it from the queue. The caller owns a reference to the
2263 * returned message. If the queue is empty, returns #NULL.
2265 * This function bypasses any message handlers that are registered,
2266 * and so using it is usually wrong. Instead, let the main loop invoke
2267 * dbus_connection_dispatch(). Popping messages manually is only
2268 * useful in very simple programs that don't share a #DBusConnection
2269 * with any libraries or other modules.
2271 * @param connection the connection.
2272 * @returns next message in the incoming queue.
2275 dbus_connection_pop_message (DBusConnection *connection)
2277 DBusMessage *message;
2278 DBusDispatchStatus status;
2280 /* this is called for the side effect that it queues
2281 * up any messages from the transport
2283 status = dbus_connection_get_dispatch_status (connection);
2284 if (status != DBUS_DISPATCH_DATA_REMAINS)
2287 CONNECTION_LOCK (connection);
2289 message = _dbus_connection_pop_message_unlocked (connection);
2291 _dbus_verbose ("Returning popped message %p\n", message);
2293 CONNECTION_UNLOCK (connection);
2299 * Acquire the dispatcher. This must be done before dispatching
2300 * messages in order to guarantee the right order of
2301 * message delivery. May sleep and drop the connection mutex
2302 * while waiting for the dispatcher.
2304 * @param connection the connection.
2307 _dbus_connection_acquire_dispatch (DBusConnection *connection)
2309 if (connection->dispatch_acquired)
2310 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
2311 _dbus_assert (!connection->dispatch_acquired);
2313 connection->dispatch_acquired = TRUE;
2317 * Release the dispatcher when you're done with it. Only call
2318 * after you've acquired the dispatcher. Wakes up at most one
2319 * thread currently waiting to acquire the dispatcher.
2321 * @param connection the connection.
2324 _dbus_connection_release_dispatch (DBusConnection *connection)
2326 _dbus_assert (connection->dispatch_acquired);
2328 connection->dispatch_acquired = FALSE;
2329 dbus_condvar_wake_one (connection->dispatch_cond);
2333 _dbus_connection_failed_pop (DBusConnection *connection,
2334 DBusList *message_link)
2336 _dbus_list_prepend_link (&connection->incoming_messages,
2338 connection->n_incoming += 1;
2341 static DBusDispatchStatus
2342 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
2344 if (connection->n_incoming > 0)
2345 return DBUS_DISPATCH_DATA_REMAINS;
2346 else if (!_dbus_transport_queue_messages (connection->transport))
2347 return DBUS_DISPATCH_NEED_MEMORY;
2350 DBusDispatchStatus status;
2352 status = _dbus_transport_get_dispatch_status (connection->transport);
2354 if (status != DBUS_DISPATCH_COMPLETE)
2356 else if (connection->n_incoming > 0)
2357 return DBUS_DISPATCH_DATA_REMAINS;
2359 return DBUS_DISPATCH_COMPLETE;
2364 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
2365 DBusDispatchStatus new_status)
2367 dbus_bool_t changed;
2368 DBusDispatchStatusFunction function;
2371 /* We have the lock */
2373 _dbus_connection_ref_unlocked (connection);
2375 changed = new_status != connection->last_dispatch_status;
2377 connection->last_dispatch_status = new_status;
2379 function = connection->dispatch_status_function;
2380 data = connection->dispatch_status_data;
2382 /* We drop the lock */
2383 CONNECTION_UNLOCK (connection);
2385 if (changed && function)
2387 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
2388 connection, new_status,
2389 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
2390 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
2391 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
2393 (* function) (connection, new_status, data);
2396 dbus_connection_unref (connection);
2400 * Gets the current state (what we would currently return
2401 * from dbus_connection_dispatch()) but doesn't actually
2402 * dispatch any messages.
2404 * @param connection the connection.
2405 * @returns current dispatch status
2408 dbus_connection_get_dispatch_status (DBusConnection *connection)
2410 DBusDispatchStatus status;
2412 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2414 CONNECTION_LOCK (connection);
2416 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2418 CONNECTION_UNLOCK (connection);
2424 * Processes data buffered while handling watches, queueing zero or
2425 * more incoming messages. Then pops the first-received message from
2426 * the current incoming message queue, runs any handlers for it, and
2427 * unrefs the message. Returns a status indicating whether messages/data
2428 * remain, more memory is needed, or all data has been processed.
2430 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
2431 * does not necessarily dispatch a message, as the data may
2432 * be part of authentication or the like.
2434 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
2436 * @todo right now a message filter gets run on replies to a pending
2437 * call in here, but not in the case where we block without entering
2438 * the main loop. Simple solution might be to just have the pending
2439 * call stuff run before the filters.
2441 * @todo FIXME what if we call out to application code to handle a
2442 * message, holding the dispatch lock, and the application code runs
2443 * the main loop and dispatches again? Probably deadlocks at the
2444 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
2445 * and then the GSource etc. could handle the situation?
2447 * @param connection the connection
2448 * @returns dispatch status
2451 dbus_connection_dispatch (DBusConnection *connection)
2453 DBusMessage *message;
2454 DBusList *link, *filter_list_copy, *message_link;
2455 DBusHandlerResult result;
2456 DBusPendingCall *pending;
2457 dbus_int32_t reply_serial;
2458 DBusDispatchStatus status;
2460 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2462 CONNECTION_LOCK (connection);
2463 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2464 if (status != DBUS_DISPATCH_DATA_REMAINS)
2466 /* unlocks and calls out to user code */
2467 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2471 /* We need to ref the connection since the callback could potentially
2472 * drop the last ref to it
2474 _dbus_connection_ref_unlocked (connection);
2476 _dbus_connection_acquire_dispatch (connection);
2478 /* This call may drop the lock during the execution (if waiting for
2479 * borrowed messages to be returned) but the order of message
2480 * dispatch if several threads call dispatch() is still
2481 * protected by the lock, since only one will get the lock, and that
2482 * one will finish the message dispatching
2484 message_link = _dbus_connection_pop_message_link_unlocked (connection);
2485 if (message_link == NULL)
2487 /* another thread dispatched our stuff */
2489 _dbus_connection_release_dispatch (connection);
2491 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2493 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2495 dbus_connection_unref (connection);
2500 message = message_link->data;
2502 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2504 reply_serial = dbus_message_get_reply_serial (message);
2505 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
2508 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
2510 _dbus_connection_release_dispatch (connection);
2512 _dbus_connection_failed_pop (connection, message_link);
2514 /* unlocks and calls user code */
2515 _dbus_connection_update_dispatch_status_and_unlock (connection,
2516 DBUS_DISPATCH_NEED_MEMORY);
2518 dbus_connection_unref (connection);
2520 return DBUS_DISPATCH_NEED_MEMORY;
2523 _dbus_list_foreach (&filter_list_copy,
2524 (DBusForeachFunction)_dbus_message_filter_ref,
2527 /* We're still protected from dispatch() reentrancy here
2528 * since we acquired the dispatcher
2530 CONNECTION_UNLOCK (connection);
2532 link = _dbus_list_get_first_link (&filter_list_copy);
2533 while (link != NULL)
2535 DBusMessageFilter *filter = link->data;
2536 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2538 _dbus_verbose (" running filter on message %p\n", message);
2539 result = (* filter->function) (connection, message, filter->user_data);
2541 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2547 _dbus_list_foreach (&filter_list_copy,
2548 (DBusForeachFunction)_dbus_message_filter_unref,
2550 _dbus_list_clear (&filter_list_copy);
2552 CONNECTION_LOCK (connection);
2554 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2557 /* Did a reply we were waiting on get filtered? */
2558 if (pending && result == DBUS_HANDLER_RESULT_HANDLED)
2560 /* Queue the timeout immediately! */
2561 if (pending->timeout_link)
2563 _dbus_connection_queue_synthesized_message_link (connection,
2564 pending->timeout_link);
2565 pending->timeout_link = NULL;
2569 /* We already queued the timeout? Then it was filtered! */
2570 _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial);
2574 if (result == DBUS_HANDLER_RESULT_HANDLED)
2579 _dbus_pending_call_complete_and_unlock (pending, message);
2583 CONNECTION_LOCK (connection);
2587 /* We're still protected from dispatch() reentrancy here
2588 * since we acquired the dispatcher
2590 _dbus_verbose (" running object path dispatch on message %p (%d %s '%s')\n",
2592 dbus_message_get_type (message),
2593 dbus_message_get_interface (message) ?
2594 dbus_message_get_interface (message) :
2596 dbus_message_get_signature (message));
2598 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
2601 CONNECTION_LOCK (connection);
2603 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2606 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
2610 DBusPreallocatedSend *preallocated;
2612 _dbus_verbose (" sending error %s\n",
2613 DBUS_ERROR_UNKNOWN_METHOD);
2615 if (!_dbus_string_init (&str))
2617 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2621 if (!_dbus_string_append_printf (&str,
2622 "Method \"%s\" on interface \"%s\" doesn't exist\n",
2623 dbus_message_get_member (message),
2624 dbus_message_get_interface (message)))
2626 _dbus_string_free (&str);
2627 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2631 reply = dbus_message_new_error (message,
2632 DBUS_ERROR_UNKNOWN_METHOD,
2633 _dbus_string_get_const_data (&str));
2634 _dbus_string_free (&str);
2638 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2642 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2644 if (preallocated == NULL)
2646 dbus_message_unref (reply);
2647 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2651 _dbus_connection_send_preallocated_unlocked (connection, preallocated,
2654 dbus_message_unref (reply);
2656 result = DBUS_HANDLER_RESULT_HANDLED;
2659 _dbus_verbose (" done dispatching %p (%d %s '%s') on connection %p\n", message,
2660 dbus_message_get_type (message),
2661 dbus_message_get_interface (message) ?
2662 dbus_message_get_interface (message) :
2664 dbus_message_get_signature (message),
2668 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2670 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
2672 /* Put message back, and we'll start over.
2673 * Yes this means handlers must be idempotent if they
2674 * don't return HANDLED; c'est la vie.
2676 _dbus_connection_putback_message_link_unlocked (connection,
2681 _dbus_verbose ("Done with message in %s\n", _DBUS_FUNCTION_NAME);
2683 if (connection->exit_on_disconnect &&
2684 dbus_message_is_signal (message,
2685 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
2688 _dbus_verbose ("Exiting on Disconnected signal\n");
2689 CONNECTION_UNLOCK (connection);
2691 _dbus_assert_not_reached ("Call to exit() returned");
2694 _dbus_list_free_link (message_link);
2695 dbus_message_unref (message); /* don't want the message to count in max message limits
2696 * in computing dispatch status below
2700 _dbus_connection_release_dispatch (connection);
2702 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2704 /* unlocks and calls user code */
2705 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2707 dbus_connection_unref (connection);
2713 * Sets the watch functions for the connection. These functions are
2714 * responsible for making the application's main loop aware of file
2715 * descriptors that need to be monitored for events, using select() or
2716 * poll(). When using Qt, typically the DBusAddWatchFunction would
2717 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2718 * could call g_io_add_watch(), or could be used as part of a more
2719 * elaborate GSource. Note that when a watch is added, it may
2722 * The DBusWatchToggledFunction notifies the application that the
2723 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2724 * to check this. A disabled watch should have no effect, and enabled
2725 * watch should be added to the main loop. This feature is used
2726 * instead of simply adding/removing the watch because
2727 * enabling/disabling can be done without memory allocation. The
2728 * toggled function may be NULL if a main loop re-queries
2729 * dbus_watch_get_enabled() every time anyway.
2731 * The DBusWatch can be queried for the file descriptor to watch using
2732 * dbus_watch_get_fd(), and for the events to watch for using
2733 * dbus_watch_get_flags(). The flags returned by
2734 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2735 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2736 * all watches implicitly include a watch for hangups, errors, and
2737 * other exceptional conditions.
2739 * Once a file descriptor becomes readable or writable, or an exception
2740 * occurs, dbus_watch_handle() should be called to
2741 * notify the connection of the file descriptor's condition.
2743 * dbus_watch_handle() cannot be called during the
2744 * DBusAddWatchFunction, as the connection will not be ready to handle
2747 * It is not allowed to reference a DBusWatch after it has been passed
2748 * to remove_function.
2750 * If #FALSE is returned due to lack of memory, the failure may be due
2751 * to a #FALSE return from the new add_function. If so, the
2752 * add_function may have been called successfully one or more times,
2753 * but the remove_function will also have been called to remove any
2754 * successful adds. i.e. if #FALSE is returned the net result
2755 * should be that dbus_connection_set_watch_functions() has no effect,
2756 * but the add_function and remove_function may have been called.
2758 * @todo We need to drop the lock when we call the
2759 * add/remove/toggled functions which can be a side effect
2760 * of setting the watch functions.
2762 * @param connection the connection.
2763 * @param add_function function to begin monitoring a new descriptor.
2764 * @param remove_function function to stop monitoring a descriptor.
2765 * @param toggled_function function to notify of enable/disable
2766 * @param data data to pass to add_function and remove_function.
2767 * @param free_data_function function to be called to free the data.
2768 * @returns #FALSE on failure (no memory)
2771 dbus_connection_set_watch_functions (DBusConnection *connection,
2772 DBusAddWatchFunction add_function,
2773 DBusRemoveWatchFunction remove_function,
2774 DBusWatchToggledFunction toggled_function,
2776 DBusFreeFunction free_data_function)
2780 _dbus_return_val_if_fail (connection != NULL, FALSE);
2782 CONNECTION_LOCK (connection);
2783 /* ref connection for slightly better reentrancy */
2784 _dbus_connection_ref_unlocked (connection);
2786 /* FIXME this can call back into user code, and we need to drop the
2787 * connection lock when it does.
2789 retval = _dbus_watch_list_set_functions (connection->watches,
2790 add_function, remove_function,
2792 data, free_data_function);
2794 CONNECTION_UNLOCK (connection);
2795 /* drop our paranoid refcount */
2796 dbus_connection_unref (connection);
2802 * Sets the timeout functions for the connection. These functions are
2803 * responsible for making the application's main loop aware of timeouts.
2804 * When using Qt, typically the DBusAddTimeoutFunction would create a
2805 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2808 * The DBusTimeoutToggledFunction notifies the application that the
2809 * timeout has been enabled or disabled. Call
2810 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2811 * have no effect, and enabled timeout should be added to the main
2812 * loop. This feature is used instead of simply adding/removing the
2813 * timeout because enabling/disabling can be done without memory
2814 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2815 * to enable and disable. The toggled function may be NULL if a main
2816 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2817 * Whenever a timeout is toggled, its interval may change.
2819 * The DBusTimeout can be queried for the timer interval using
2820 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
2821 * repeatedly, each time the interval elapses, starting after it has
2822 * elapsed once. The timeout stops firing when it is removed with the
2823 * given remove_function. The timer interval may change whenever the
2824 * timeout is added, removed, or toggled.
2826 * @param connection the connection.
2827 * @param add_function function to add a timeout.
2828 * @param remove_function function to remove a timeout.
2829 * @param toggled_function function to notify of enable/disable
2830 * @param data data to pass to add_function and remove_function.
2831 * @param free_data_function function to be called to free the data.
2832 * @returns #FALSE on failure (no memory)
2835 dbus_connection_set_timeout_functions (DBusConnection *connection,
2836 DBusAddTimeoutFunction add_function,
2837 DBusRemoveTimeoutFunction remove_function,
2838 DBusTimeoutToggledFunction toggled_function,
2840 DBusFreeFunction free_data_function)
2844 _dbus_return_val_if_fail (connection != NULL, FALSE);
2846 CONNECTION_LOCK (connection);
2847 /* ref connection for slightly better reentrancy */
2848 _dbus_connection_ref_unlocked (connection);
2850 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2851 add_function, remove_function,
2853 data, free_data_function);
2855 CONNECTION_UNLOCK (connection);
2856 /* drop our paranoid refcount */
2857 dbus_connection_unref (connection);
2863 * Sets the mainloop wakeup function for the connection. Thi function is
2864 * responsible for waking up the main loop (if its sleeping) when some some
2865 * change has happened to the connection that the mainloop needs to reconsiders
2866 * (e.g. a message has been queued for writing).
2867 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2868 * When using GLib, it would call g_main_context_wakeup().
2871 * @param connection the connection.
2872 * @param wakeup_main_function function to wake up the mainloop
2873 * @param data data to pass wakeup_main_function
2874 * @param free_data_function function to be called to free the data.
2877 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2878 DBusWakeupMainFunction wakeup_main_function,
2880 DBusFreeFunction free_data_function)
2883 DBusFreeFunction old_free_data;
2885 _dbus_return_if_fail (connection != NULL);
2887 CONNECTION_LOCK (connection);
2888 old_data = connection->wakeup_main_data;
2889 old_free_data = connection->free_wakeup_main_data;
2891 connection->wakeup_main_function = wakeup_main_function;
2892 connection->wakeup_main_data = data;
2893 connection->free_wakeup_main_data = free_data_function;
2895 CONNECTION_UNLOCK (connection);
2897 /* Callback outside the lock */
2899 (*old_free_data) (old_data);
2903 * Set a function to be invoked when the dispatch status changes.
2904 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
2905 * dbus_connection_dispatch() needs to be called to process incoming
2906 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
2907 * from inside the DBusDispatchStatusFunction. Indeed, almost
2908 * any reentrancy in this function is a bad idea. Instead,
2909 * the DBusDispatchStatusFunction should simply save an indication
2910 * that messages should be dispatched later, when the main loop
2913 * @param connection the connection
2914 * @param function function to call on dispatch status changes
2915 * @param data data for function
2916 * @param free_data_function free the function data
2919 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
2920 DBusDispatchStatusFunction function,
2922 DBusFreeFunction free_data_function)
2925 DBusFreeFunction old_free_data;
2927 _dbus_return_if_fail (connection != NULL);
2929 CONNECTION_LOCK (connection);
2930 old_data = connection->dispatch_status_data;
2931 old_free_data = connection->free_dispatch_status_data;
2933 connection->dispatch_status_function = function;
2934 connection->dispatch_status_data = data;
2935 connection->free_dispatch_status_data = free_data_function;
2937 CONNECTION_UNLOCK (connection);
2939 /* Callback outside the lock */
2941 (*old_free_data) (old_data);
2945 * Gets the UNIX user ID of the connection if any.
2946 * Returns #TRUE if the uid is filled in.
2947 * Always returns #FALSE on non-UNIX platforms.
2948 * Always returns #FALSE prior to authenticating the
2951 * @param connection the connection
2952 * @param uid return location for the user ID
2953 * @returns #TRUE if uid is filled in with a valid user ID
2956 dbus_connection_get_unix_user (DBusConnection *connection,
2961 _dbus_return_val_if_fail (connection != NULL, FALSE);
2962 _dbus_return_val_if_fail (uid != NULL, FALSE);
2964 CONNECTION_LOCK (connection);
2966 if (!_dbus_transport_get_is_authenticated (connection->transport))
2969 result = _dbus_transport_get_unix_user (connection->transport,
2971 CONNECTION_UNLOCK (connection);
2977 * Sets a predicate function used to determine whether a given user ID
2978 * is allowed to connect. When an incoming connection has
2979 * authenticated with a particular user ID, this function is called;
2980 * if it returns #TRUE, the connection is allowed to proceed,
2981 * otherwise the connection is disconnected.
2983 * If the function is set to #NULL (as it is by default), then
2984 * only the same UID as the server process will be allowed to
2987 * @param connection the connection
2988 * @param function the predicate
2989 * @param data data to pass to the predicate
2990 * @param free_data_function function to free the data
2993 dbus_connection_set_unix_user_function (DBusConnection *connection,
2994 DBusAllowUnixUserFunction function,
2996 DBusFreeFunction free_data_function)
2998 void *old_data = NULL;
2999 DBusFreeFunction old_free_function = NULL;
3001 _dbus_return_if_fail (connection != NULL);
3003 CONNECTION_LOCK (connection);
3004 _dbus_transport_set_unix_user_function (connection->transport,
3005 function, data, free_data_function,
3006 &old_data, &old_free_function);
3007 CONNECTION_UNLOCK (connection);
3009 if (old_free_function != NULL)
3010 (* old_free_function) (old_data);
3014 * Adds a message filter. Filters are handlers that are run on all
3015 * incoming messages, prior to the objects registered with
3016 * dbus_connection_register_object_path(). Filters are run in the
3017 * order that they were added. The same handler can be added as a
3018 * filter more than once, in which case it will be run more than once.
3019 * Filters added during a filter callback won't be run on the message
3022 * @todo we don't run filters on messages while blocking without
3023 * entering the main loop, since filters are run as part of
3024 * dbus_connection_dispatch(). This is probably a feature, as filters
3025 * could create arbitrary reentrancy. But kind of sucks if you're
3026 * trying to filter METHOD_RETURN for some reason.
3028 * @param connection the connection
3029 * @param function function to handle messages
3030 * @param user_data user data to pass to the function
3031 * @param free_data_function function to use for freeing user data
3032 * @returns #TRUE on success, #FALSE if not enough memory.
3035 dbus_connection_add_filter (DBusConnection *connection,
3036 DBusHandleMessageFunction function,
3038 DBusFreeFunction free_data_function)
3040 DBusMessageFilter *filter;
3042 _dbus_return_val_if_fail (connection != NULL, FALSE);
3043 _dbus_return_val_if_fail (function != NULL, FALSE);
3045 filter = dbus_new0 (DBusMessageFilter, 1);
3049 filter->refcount.value = 1;
3051 CONNECTION_LOCK (connection);
3053 if (!_dbus_list_append (&connection->filter_list,
3056 _dbus_message_filter_unref (filter);
3057 CONNECTION_UNLOCK (connection);
3061 /* Fill in filter after all memory allocated,
3062 * so we don't run the free_user_data_function
3063 * if the add_filter() fails
3066 filter->function = function;
3067 filter->user_data = user_data;
3068 filter->free_user_data_function = free_data_function;
3070 CONNECTION_UNLOCK (connection);
3075 * Removes a previously-added message filter. It is a programming
3076 * error to call this function for a handler that has not been added
3077 * as a filter. If the given handler was added more than once, only
3078 * one instance of it will be removed (the most recently-added
3081 * @param connection the connection
3082 * @param function the handler to remove
3083 * @param user_data user data for the handler to remove
3087 dbus_connection_remove_filter (DBusConnection *connection,
3088 DBusHandleMessageFunction function,
3092 DBusMessageFilter *filter;
3094 _dbus_return_if_fail (connection != NULL);
3095 _dbus_return_if_fail (function != NULL);
3097 CONNECTION_LOCK (connection);
3101 link = _dbus_list_get_last_link (&connection->filter_list);
3102 while (link != NULL)
3104 filter = link->data;
3106 if (filter->function == function &&
3107 filter->user_data == user_data)
3109 _dbus_list_remove_link (&connection->filter_list, link);
3110 filter->function = NULL;
3115 link = _dbus_list_get_prev_link (&connection->filter_list, link);
3118 CONNECTION_UNLOCK (connection);
3120 #ifndef DBUS_DISABLE_CHECKS
3123 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
3124 function, user_data);
3129 /* Call application code */
3130 if (filter->free_user_data_function)
3131 (* filter->free_user_data_function) (filter->user_data);
3133 filter->free_user_data_function = NULL;
3134 filter->user_data = NULL;
3136 _dbus_message_filter_unref (filter);
3140 * Registers a handler for a given path in the object hierarchy.
3141 * The given vtable handles messages sent to exactly the given path.
3144 * @param connection the connection
3145 * @param path #NULL-terminated array of path elements
3146 * @param vtable the virtual table
3147 * @param user_data data to pass to functions in the vtable
3148 * @returns #FALSE if not enough memory
3151 dbus_connection_register_object_path (DBusConnection *connection,
3153 const DBusObjectPathVTable *vtable,
3158 _dbus_return_val_if_fail (connection != NULL, FALSE);
3159 _dbus_return_val_if_fail (path != NULL, FALSE);
3160 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3161 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3163 CONNECTION_LOCK (connection);
3165 retval = _dbus_object_tree_register (connection->objects,
3170 CONNECTION_UNLOCK (connection);
3176 * Registers a fallback handler for a given subsection of the object
3177 * hierarchy. The given vtable handles messages at or below the given
3178 * path. You can use this to establish a default message handling
3179 * policy for a whole "subdirectory."
3181 * @param connection the connection
3182 * @param path #NULL-terminated array of path elements
3183 * @param vtable the virtual table
3184 * @param user_data data to pass to functions in the vtable
3185 * @returns #FALSE if not enough memory
3188 dbus_connection_register_fallback (DBusConnection *connection,
3190 const DBusObjectPathVTable *vtable,
3195 _dbus_return_val_if_fail (connection != NULL, FALSE);
3196 _dbus_return_val_if_fail (path != NULL, FALSE);
3197 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3198 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3200 CONNECTION_LOCK (connection);
3202 retval = _dbus_object_tree_register (connection->objects,
3207 CONNECTION_UNLOCK (connection);
3213 * Unregisters the handler registered with exactly the given path.
3214 * It's a bug to call this function for a path that isn't registered.
3215 * Can unregister both fallback paths and object paths.
3217 * @param connection the connection
3218 * @param path the #NULL-terminated array of path elements
3221 dbus_connection_unregister_object_path (DBusConnection *connection,
3224 _dbus_return_if_fail (connection != NULL);
3225 _dbus_return_if_fail (path != NULL);
3226 _dbus_return_if_fail (path[0] != NULL);
3228 CONNECTION_LOCK (connection);
3230 return _dbus_object_tree_unregister_and_unlock (connection->objects,
3235 * Lists the registered fallback handlers and object path handlers at
3236 * the given parent_path. The returned array should be freed with
3237 * dbus_free_string_array().
3239 * @param connection the connection
3240 * @param parent_path the path to list the child handlers of
3241 * @param child_entries returns #NULL-terminated array of children
3242 * @returns #FALSE if no memory to allocate the child entries
3245 dbus_connection_list_registered (DBusConnection *connection,
3246 const char **parent_path,
3247 char ***child_entries)
3249 _dbus_return_val_if_fail (connection != NULL, FALSE);
3250 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
3251 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
3253 CONNECTION_LOCK (connection);
3255 return _dbus_object_tree_list_registered_and_unlock (connection->objects,
3260 static DBusDataSlotAllocator slot_allocator;
3261 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
3264 * Allocates an integer ID to be used for storing application-specific
3265 * data on any DBusConnection. The allocated ID may then be used
3266 * with dbus_connection_set_data() and dbus_connection_get_data().
3267 * The passed-in slot must be initialized to -1, and is filled in
3268 * with the slot ID. If the passed-in slot is not -1, it's assumed
3269 * to be already allocated, and its refcount is incremented.
3271 * The allocated slot is global, i.e. all DBusConnection objects will
3272 * have a slot with the given integer ID reserved.
3274 * @param slot_p address of a global variable storing the slot
3275 * @returns #FALSE on failure (no memory)
3278 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
3280 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3281 _DBUS_LOCK_NAME (connection_slots),
3286 * Deallocates a global ID for connection data slots.
3287 * dbus_connection_get_data() and dbus_connection_set_data() may no
3288 * longer be used with this slot. Existing data stored on existing
3289 * DBusConnection objects will be freed when the connection is
3290 * finalized, but may not be retrieved (and may only be replaced if
3291 * someone else reallocates the slot). When the refcount on the
3292 * passed-in slot reaches 0, it is set to -1.
3294 * @param slot_p address storing the slot to deallocate
3297 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
3299 _dbus_return_if_fail (*slot_p >= 0);
3301 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3305 * Stores a pointer on a DBusConnection, along
3306 * with an optional function to be used for freeing
3307 * the data when the data is set again, or when
3308 * the connection is finalized. The slot number
3309 * must have been allocated with dbus_connection_allocate_data_slot().
3311 * @param connection the connection
3312 * @param slot the slot number
3313 * @param data the data to store
3314 * @param free_data_func finalizer function for the data
3315 * @returns #TRUE if there was enough memory to store the data
3318 dbus_connection_set_data (DBusConnection *connection,
3321 DBusFreeFunction free_data_func)
3323 DBusFreeFunction old_free_func;
3327 _dbus_return_val_if_fail (connection != NULL, FALSE);
3328 _dbus_return_val_if_fail (slot >= 0, FALSE);
3330 CONNECTION_LOCK (connection);
3332 retval = _dbus_data_slot_list_set (&slot_allocator,
3333 &connection->slot_list,
3334 slot, data, free_data_func,
3335 &old_free_func, &old_data);
3337 CONNECTION_UNLOCK (connection);
3341 /* Do the actual free outside the connection lock */
3343 (* old_free_func) (old_data);
3350 * Retrieves data previously set with dbus_connection_set_data().
3351 * The slot must still be allocated (must not have been freed).
3353 * @param connection the connection
3354 * @param slot the slot to get data from
3355 * @returns the data, or #NULL if not found
3358 dbus_connection_get_data (DBusConnection *connection,
3363 _dbus_return_val_if_fail (connection != NULL, NULL);
3365 CONNECTION_LOCK (connection);
3367 res = _dbus_data_slot_list_get (&slot_allocator,
3368 &connection->slot_list,
3371 CONNECTION_UNLOCK (connection);
3377 * This function sets a global flag for whether dbus_connection_new()
3378 * will set SIGPIPE behavior to SIG_IGN.
3380 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
3383 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
3385 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
3389 * Specifies the maximum size message this connection is allowed to
3390 * receive. Larger messages will result in disconnecting the
3393 * @param connection a #DBusConnection
3394 * @param size maximum message size the connection can receive, in bytes
3397 dbus_connection_set_max_message_size (DBusConnection *connection,
3400 _dbus_return_if_fail (connection != NULL);
3402 CONNECTION_LOCK (connection);
3403 _dbus_transport_set_max_message_size (connection->transport,
3405 CONNECTION_UNLOCK (connection);
3409 * Gets the value set by dbus_connection_set_max_message_size().
3411 * @param connection the connection
3412 * @returns the max size of a single message
3415 dbus_connection_get_max_message_size (DBusConnection *connection)
3419 _dbus_return_val_if_fail (connection != NULL, 0);
3421 CONNECTION_LOCK (connection);
3422 res = _dbus_transport_get_max_message_size (connection->transport);
3423 CONNECTION_UNLOCK (connection);
3428 * Sets the maximum total number of bytes that can be used for all messages
3429 * received on this connection. Messages count toward the maximum until
3430 * they are finalized. When the maximum is reached, the connection will
3431 * not read more data until some messages are finalized.
3433 * The semantics of the maximum are: if outstanding messages are
3434 * already above the maximum, additional messages will not be read.
3435 * The semantics are not: if the next message would cause us to exceed
3436 * the maximum, we don't read it. The reason is that we don't know the
3437 * size of a message until after we read it.
3439 * Thus, the max live messages size can actually be exceeded
3440 * by up to the maximum size of a single message.
3442 * Also, if we read say 1024 bytes off the wire in a single read(),
3443 * and that contains a half-dozen small messages, we may exceed the
3444 * size max by that amount. But this should be inconsequential.
3446 * This does imply that we can't call read() with a buffer larger
3447 * than we're willing to exceed this limit by.
3449 * @param connection the connection
3450 * @param size the maximum size in bytes of all outstanding messages
3453 dbus_connection_set_max_received_size (DBusConnection *connection,
3456 _dbus_return_if_fail (connection != NULL);
3458 CONNECTION_LOCK (connection);
3459 _dbus_transport_set_max_received_size (connection->transport,
3461 CONNECTION_UNLOCK (connection);
3465 * Gets the value set by dbus_connection_set_max_received_size().
3467 * @param connection the connection
3468 * @returns the max size of all live messages
3471 dbus_connection_get_max_received_size (DBusConnection *connection)
3475 _dbus_return_val_if_fail (connection != NULL, 0);
3477 CONNECTION_LOCK (connection);
3478 res = _dbus_transport_get_max_received_size (connection->transport);
3479 CONNECTION_UNLOCK (connection);
3484 * Gets the approximate size in bytes of all messages in the outgoing
3485 * message queue. The size is approximate in that you shouldn't use
3486 * it to decide how many bytes to read off the network or anything
3487 * of that nature, as optimizations may choose to tell small white lies
3488 * to avoid performance overhead.
3490 * @param connection the connection
3491 * @returns the number of bytes that have been queued up but not sent
3494 dbus_connection_get_outgoing_size (DBusConnection *connection)
3498 _dbus_return_val_if_fail (connection != NULL, 0);
3500 CONNECTION_LOCK (connection);
3501 res = _dbus_counter_get_value (connection->outgoing_counter);
3502 CONNECTION_UNLOCK (connection);