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 2.0
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 * signal "Disconnected" from the interface
103 * #DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, path
104 * #DBUS_PATH_ORG_FREEDESKTOP_LOCAL.
106 * You may not drop the last reference to a #DBusConnection
107 * until that connection has been disconnected.
109 * You may dispatch the unprocessed incoming message queue even if the
110 * connection is disconnected. However, "Disconnected" will always be
111 * the last message in the queue (obviously no messages are received
112 * after disconnection).
114 * #DBusConnection has thread locks and drops them when invoking user
115 * callbacks, so in general is transparently threadsafe. However,
116 * #DBusMessage does NOT have thread locks; you must not send the same
117 * message to multiple #DBusConnection that will be used from
122 * @defgroup DBusConnectionInternals DBusConnection implementation details
123 * @ingroup DBusInternals
124 * @brief Implementation details of DBusConnection
130 * Internal struct representing a message filter function
132 typedef struct DBusMessageFilter DBusMessageFilter;
135 * Internal struct representing a message filter function
137 struct DBusMessageFilter
139 DBusAtomic refcount; /**< Reference count */
140 DBusHandleMessageFunction function; /**< Function to call to filter */
141 void *user_data; /**< User data for the function */
142 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
147 * Internals of DBusPreallocatedSend
149 struct DBusPreallocatedSend
151 DBusConnection *connection; /**< Connection we'd send the message to */
152 DBusList *queue_link; /**< Preallocated link in the queue */
153 DBusList *counter_link; /**< Preallocated link in the resource counter */
156 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
159 * Implementation details of DBusConnection. All fields are private.
161 struct DBusConnection
163 DBusAtomic refcount; /**< Reference count. */
165 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
167 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */
168 DBusCondVar *dispatch_cond; /**< Protects dispatch() */
170 dbus_bool_t io_path_acquired; /**< Protects transport io path */
171 DBusCondVar *io_path_cond; /**< Protects transport io path */
173 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
174 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
176 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
177 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
179 int n_outgoing; /**< Length of outgoing queue. */
180 int n_incoming; /**< Length of incoming queue. */
182 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
184 DBusTransport *transport; /**< Object that sends/receives messages over network. */
185 DBusWatchList *watches; /**< Stores active watches. */
186 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
188 DBusList *filter_list; /**< List of filters. */
190 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
192 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
194 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
195 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
197 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
198 void *wakeup_main_data; /**< Application data for wakeup_main_function */
199 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
201 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
202 void *dispatch_status_data; /**< Application data for dispatch_status_function */
203 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
205 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
207 DBusList *link_cache; /**< A cache of linked list links to prevent contention
208 * for the global linked list mempool lock
210 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
212 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
215 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
216 DBusTimeout *timeout);
217 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
218 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
219 DBusDispatchStatus new_status);
220 static void _dbus_connection_last_unref (DBusConnection *connection);
222 static DBusMessageFilter *
223 _dbus_message_filter_ref (DBusMessageFilter *filter)
225 _dbus_assert (filter->refcount.value > 0);
226 _dbus_atomic_inc (&filter->refcount);
232 _dbus_message_filter_unref (DBusMessageFilter *filter)
234 _dbus_assert (filter->refcount.value > 0);
236 if (_dbus_atomic_dec (&filter->refcount) == 1)
238 if (filter->free_user_data_function)
239 (* filter->free_user_data_function) (filter->user_data);
246 * Acquires the connection lock.
248 * @param connection the connection.
251 _dbus_connection_lock (DBusConnection *connection)
253 CONNECTION_LOCK (connection);
257 * Releases the connection lock.
259 * @param connection the connection.
262 _dbus_connection_unlock (DBusConnection *connection)
264 CONNECTION_UNLOCK (connection);
268 * Wakes up the main loop if it is sleeping
269 * Needed if we're e.g. queueing outgoing messages
270 * on a thread while the mainloop sleeps.
272 * @param connection the connection.
275 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
277 if (connection->wakeup_main_function)
278 (*connection->wakeup_main_function) (connection->wakeup_main_data);
281 #ifdef DBUS_BUILD_TESTS
282 /* For now this function isn't used */
284 * Adds a message to the incoming message queue, returning #FALSE
285 * if there's insufficient memory to queue the message.
286 * Does not take over refcount of the message.
288 * @param connection the connection.
289 * @param message the message to queue.
290 * @returns #TRUE on success.
293 _dbus_connection_queue_received_message (DBusConnection *connection,
294 DBusMessage *message)
298 link = _dbus_list_alloc_link (message);
302 dbus_message_ref (message);
303 _dbus_connection_queue_received_message_link (connection, link);
310 * Adds a message-containing list link to the incoming message queue,
311 * taking ownership of the link and the message's current refcount.
312 * Cannot fail due to lack of memory.
314 * @param connection the connection.
315 * @param link the message link to queue.
318 _dbus_connection_queue_received_message_link (DBusConnection *connection,
321 DBusPendingCall *pending;
322 dbus_int32_t reply_serial;
323 DBusMessage *message;
325 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
327 _dbus_list_append_link (&connection->incoming_messages,
329 message = link->data;
331 /* If this is a reply we're waiting on, remove timeout for it */
332 reply_serial = dbus_message_get_reply_serial (message);
333 if (reply_serial != -1)
335 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
339 if (pending->timeout_added)
340 _dbus_connection_remove_timeout_locked (connection,
343 pending->timeout_added = FALSE;
347 connection->n_incoming += 1;
349 _dbus_connection_wakeup_mainloop (connection);
351 _dbus_verbose ("Message %p (%d %s '%s') added to incoming queue %p, %d incoming\n",
353 dbus_message_get_type (message),
354 dbus_message_get_interface (message) ?
355 dbus_message_get_interface (message) :
357 dbus_message_get_signature (message),
359 connection->n_incoming);
363 * Adds a link + message to the incoming message queue.
364 * Can't fail. Takes ownership of both link and message.
366 * @param connection the connection.
367 * @param link the list node and message to queue.
369 * @todo This needs to wake up the mainloop if it is in
370 * a poll/select and this is a multithreaded app.
373 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
376 _dbus_list_append_link (&connection->incoming_messages, link);
378 connection->n_incoming += 1;
380 _dbus_connection_wakeup_mainloop (connection);
382 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
383 link->data, connection, connection->n_incoming);
388 * Checks whether there are messages in the outgoing message queue.
390 * @param connection the connection.
391 * @returns #TRUE if the outgoing queue is non-empty.
394 _dbus_connection_have_messages_to_send (DBusConnection *connection)
396 return connection->outgoing_messages != NULL;
400 * Gets the next outgoing message. The message remains in the
401 * queue, and the caller does not own a reference to it.
403 * @param connection the connection.
404 * @returns the message to be sent.
407 _dbus_connection_get_message_to_send (DBusConnection *connection)
409 return _dbus_list_get_last (&connection->outgoing_messages);
413 * Notifies the connection that a message has been sent, so the
414 * message can be removed from the outgoing queue.
415 * Called with the connection lock held.
417 * @param connection the connection.
418 * @param message the message that was sent.
421 _dbus_connection_message_sent (DBusConnection *connection,
422 DBusMessage *message)
426 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
428 link = _dbus_list_get_last_link (&connection->outgoing_messages);
429 _dbus_assert (link != NULL);
430 _dbus_assert (link->data == message);
432 /* Save this link in the link cache */
433 _dbus_list_unlink (&connection->outgoing_messages,
435 _dbus_list_prepend_link (&connection->link_cache, link);
437 connection->n_outgoing -= 1;
439 _dbus_verbose ("Message %p (%d %s '%s') removed from outgoing queue %p, %d left to send\n",
441 dbus_message_get_type (message),
442 dbus_message_get_interface (message) ?
443 dbus_message_get_interface (message) :
445 dbus_message_get_signature (message),
446 connection, connection->n_outgoing);
448 /* Save this link in the link cache also */
449 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
451 _dbus_list_prepend_link (&connection->link_cache, link);
453 dbus_message_unref (message);
455 if (connection->n_outgoing == 0)
456 _dbus_transport_messages_pending (connection->transport,
457 connection->n_outgoing);
461 * Adds a watch using the connection's DBusAddWatchFunction if
462 * available. Otherwise records the watch to be added when said
463 * function is available. Also re-adds the watch if the
464 * DBusAddWatchFunction changes. May fail due to lack of memory.
466 * @param connection the connection.
467 * @param watch the watch to add.
468 * @returns #TRUE on success.
471 _dbus_connection_add_watch (DBusConnection *connection,
474 if (connection->watches) /* null during finalize */
475 return _dbus_watch_list_add_watch (connection->watches,
482 * Removes a watch using the connection's DBusRemoveWatchFunction
483 * if available. It's an error to call this function on a watch
484 * that was not previously added.
486 * @param connection the connection.
487 * @param watch the watch to remove.
490 _dbus_connection_remove_watch (DBusConnection *connection,
493 if (connection->watches) /* null during finalize */
494 _dbus_watch_list_remove_watch (connection->watches,
499 * Toggles a watch and notifies app via connection's
500 * DBusWatchToggledFunction if available. It's an error to call this
501 * function on a watch that was not previously added.
503 * @param connection the connection.
504 * @param watch the watch to toggle.
505 * @param enabled whether to enable or disable
508 _dbus_connection_toggle_watch (DBusConnection *connection,
512 if (connection->watches) /* null during finalize */
513 _dbus_watch_list_toggle_watch (connection->watches,
518 * Adds a timeout using the connection's DBusAddTimeoutFunction if
519 * available. Otherwise records the timeout to be added when said
520 * function is available. Also re-adds the timeout if the
521 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
522 * The timeout will fire repeatedly until removed.
524 * @param connection the connection.
525 * @param timeout the timeout to add.
526 * @returns #TRUE on success.
529 _dbus_connection_add_timeout (DBusConnection *connection,
530 DBusTimeout *timeout)
532 if (connection->timeouts) /* null during finalize */
533 return _dbus_timeout_list_add_timeout (connection->timeouts,
540 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
541 * if available. It's an error to call this function on a timeout
542 * that was not previously added.
544 * @param connection the connection.
545 * @param timeout the timeout to remove.
548 _dbus_connection_remove_timeout (DBusConnection *connection,
549 DBusTimeout *timeout)
551 if (connection->timeouts) /* null during finalize */
552 _dbus_timeout_list_remove_timeout (connection->timeouts,
557 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
558 DBusTimeout *timeout)
560 CONNECTION_LOCK (connection);
561 _dbus_connection_remove_timeout (connection, timeout);
562 CONNECTION_UNLOCK (connection);
566 * Toggles a timeout and notifies app via connection's
567 * DBusTimeoutToggledFunction if available. It's an error to call this
568 * function on a timeout that was not previously added.
570 * @param connection the connection.
571 * @param timeout the timeout to toggle.
572 * @param enabled whether to enable or disable
575 _dbus_connection_toggle_timeout (DBusConnection *connection,
576 DBusTimeout *timeout,
579 if (connection->timeouts) /* null during finalize */
580 _dbus_timeout_list_toggle_timeout (connection->timeouts,
585 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
586 DBusPendingCall *pending)
588 _dbus_assert (pending->reply_serial != 0);
590 if (!_dbus_connection_add_timeout (connection, pending->timeout))
593 if (!_dbus_hash_table_insert_int (connection->pending_replies,
594 pending->reply_serial,
597 _dbus_connection_remove_timeout (connection, pending->timeout);
601 pending->timeout_added = TRUE;
602 pending->connection = connection;
604 dbus_pending_call_ref (pending);
610 free_pending_call_on_hash_removal (void *data)
612 DBusPendingCall *pending;
619 if (pending->connection)
621 if (pending->timeout_added)
623 _dbus_connection_remove_timeout (pending->connection,
625 pending->timeout_added = FALSE;
628 pending->connection = NULL;
630 dbus_pending_call_unref (pending);
635 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
636 DBusPendingCall *pending)
638 /* The idea here is to avoid finalizing the pending call
639 * with the lock held, since there's a destroy notifier
640 * in pending call that goes out to application code.
642 dbus_pending_call_ref (pending);
643 _dbus_hash_table_remove_int (connection->pending_replies,
644 pending->reply_serial);
645 CONNECTION_UNLOCK (connection);
646 dbus_pending_call_unref (pending);
650 * Removes a pending call from the connection, such that
651 * the pending reply will be ignored. May drop the last
652 * reference to the pending call.
654 * @param connection the connection
655 * @param pending the pending call
658 _dbus_connection_remove_pending_call (DBusConnection *connection,
659 DBusPendingCall *pending)
661 CONNECTION_LOCK (connection);
662 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
666 * Completes a pending call with the given message,
667 * or if the message is #NULL, by timing out the pending call.
669 * @param pending the pending call
670 * @param message the message to complete the call with, or #NULL
671 * to time out the call
674 _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
675 DBusMessage *message)
679 message = pending->timeout_link->data;
680 _dbus_list_clear (&pending->timeout_link);
683 dbus_message_ref (message);
685 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
687 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
689 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
690 "error" : "other type",
691 pending->reply_serial);
693 _dbus_assert (pending->reply == NULL);
694 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
695 pending->reply = message;
697 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
698 _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending);
700 /* Must be called unlocked since it invokes app callback */
701 _dbus_pending_call_notify (pending);
702 dbus_pending_call_unref (pending);
706 * Acquire the transporter I/O path. This must be done before
707 * doing any I/O in the transporter. May sleep and drop the
708 * connection mutex while waiting for the I/O path.
710 * @param connection the connection.
711 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
712 * @returns TRUE if the I/O path was acquired.
715 _dbus_connection_acquire_io_path (DBusConnection *connection,
716 int timeout_milliseconds)
718 dbus_bool_t res = TRUE;
720 if (connection->io_path_acquired)
722 if (timeout_milliseconds != -1)
723 res = dbus_condvar_wait_timeout (connection->io_path_cond,
725 timeout_milliseconds);
727 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
732 _dbus_assert (!connection->io_path_acquired);
734 connection->io_path_acquired = TRUE;
741 * Release the I/O path when you're done with it. Only call
742 * after you've acquired the I/O. Wakes up at most one thread
743 * currently waiting to acquire the I/O path.
745 * @param connection the connection.
748 _dbus_connection_release_io_path (DBusConnection *connection)
750 _dbus_assert (connection->io_path_acquired);
752 connection->io_path_acquired = FALSE;
753 dbus_condvar_wake_one (connection->io_path_cond);
758 * Queues incoming messages and sends outgoing messages for this
759 * connection, optionally blocking in the process. Each call to
760 * _dbus_connection_do_iteration() will call select() or poll() one
761 * time and then read or write data if possible.
763 * The purpose of this function is to be able to flush outgoing
764 * messages or queue up incoming messages without returning
765 * control to the application and causing reentrancy weirdness.
767 * The flags parameter allows you to specify whether to
768 * read incoming messages, write outgoing messages, or both,
769 * and whether to block if no immediate action is possible.
771 * The timeout_milliseconds parameter does nothing unless the
772 * iteration is blocking.
774 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
775 * wasn't specified, then it's impossible to block, even if
776 * you specify DBUS_ITERATION_BLOCK; in that case the function
777 * returns immediately.
779 * @param connection the connection.
780 * @param flags iteration flags.
781 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
784 _dbus_connection_do_iteration (DBusConnection *connection,
786 int timeout_milliseconds)
788 if (connection->n_outgoing == 0)
789 flags &= ~DBUS_ITERATION_DO_WRITING;
791 if (_dbus_connection_acquire_io_path (connection,
792 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
794 _dbus_transport_do_iteration (connection->transport,
795 flags, timeout_milliseconds);
796 _dbus_connection_release_io_path (connection);
801 * Creates a new connection for the given transport. A transport
802 * represents a message stream that uses some concrete mechanism, such
803 * as UNIX domain sockets. May return #NULL if insufficient
804 * memory exists to create the connection.
806 * @param transport the transport.
807 * @returns the new connection, or #NULL on failure.
810 _dbus_connection_new_for_transport (DBusTransport *transport)
812 DBusConnection *connection;
813 DBusWatchList *watch_list;
814 DBusTimeoutList *timeout_list;
815 DBusHashTable *pending_replies;
817 DBusCondVar *message_returned_cond;
818 DBusCondVar *dispatch_cond;
819 DBusCondVar *io_path_cond;
820 DBusList *disconnect_link;
821 DBusMessage *disconnect_message;
822 DBusCounter *outgoing_counter;
823 DBusObjectTree *objects;
827 pending_replies = NULL;
830 message_returned_cond = NULL;
831 dispatch_cond = NULL;
833 disconnect_link = NULL;
834 disconnect_message = NULL;
835 outgoing_counter = NULL;
838 watch_list = _dbus_watch_list_new ();
839 if (watch_list == NULL)
842 timeout_list = _dbus_timeout_list_new ();
843 if (timeout_list == NULL)
847 _dbus_hash_table_new (DBUS_HASH_INT,
849 (DBusFreeFunction)free_pending_call_on_hash_removal);
850 if (pending_replies == NULL)
853 connection = dbus_new0 (DBusConnection, 1);
854 if (connection == NULL)
857 mutex = dbus_mutex_new ();
861 message_returned_cond = dbus_condvar_new ();
862 if (message_returned_cond == NULL)
865 dispatch_cond = dbus_condvar_new ();
866 if (dispatch_cond == NULL)
869 io_path_cond = dbus_condvar_new ();
870 if (io_path_cond == NULL)
873 disconnect_message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_LOCAL,
874 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
877 if (disconnect_message == NULL)
880 disconnect_link = _dbus_list_alloc_link (disconnect_message);
881 if (disconnect_link == NULL)
884 outgoing_counter = _dbus_counter_new ();
885 if (outgoing_counter == NULL)
888 objects = _dbus_object_tree_new (connection);
892 if (_dbus_modify_sigpipe)
893 _dbus_disable_sigpipe ();
895 connection->refcount.value = 1;
896 connection->mutex = mutex;
897 connection->dispatch_cond = dispatch_cond;
898 connection->io_path_cond = io_path_cond;
899 connection->message_returned_cond = message_returned_cond;
900 connection->transport = transport;
901 connection->watches = watch_list;
902 connection->timeouts = timeout_list;
903 connection->pending_replies = pending_replies;
904 connection->outgoing_counter = outgoing_counter;
905 connection->filter_list = NULL;
906 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
907 connection->objects = objects;
908 connection->exit_on_disconnect = FALSE;
910 _dbus_data_slot_list_init (&connection->slot_list);
912 connection->client_serial = 1;
914 connection->disconnect_message_link = disconnect_link;
916 if (!_dbus_transport_set_connection (transport, connection))
919 _dbus_transport_ref (transport);
924 if (disconnect_message != NULL)
925 dbus_message_unref (disconnect_message);
927 if (disconnect_link != NULL)
928 _dbus_list_free_link (disconnect_link);
930 if (io_path_cond != NULL)
931 dbus_condvar_free (io_path_cond);
933 if (dispatch_cond != NULL)
934 dbus_condvar_free (dispatch_cond);
936 if (message_returned_cond != NULL)
937 dbus_condvar_free (message_returned_cond);
940 dbus_mutex_free (mutex);
942 if (connection != NULL)
943 dbus_free (connection);
946 _dbus_hash_table_unref (pending_replies);
949 _dbus_watch_list_free (watch_list);
952 _dbus_timeout_list_free (timeout_list);
954 if (outgoing_counter)
955 _dbus_counter_unref (outgoing_counter);
958 _dbus_object_tree_unref (objects);
964 * Increments the reference count of a DBusConnection.
965 * Requires that the caller already holds the connection lock.
967 * @param connection the connection.
968 * @returns the connection.
971 _dbus_connection_ref_unlocked (DBusConnection *connection)
973 #ifdef DBUS_HAVE_ATOMIC_INT
974 _dbus_atomic_inc (&connection->refcount);
976 _dbus_assert (connection->refcount.value > 0);
977 connection->refcount.value += 1;
984 * Decrements the reference count of a DBusConnection.
985 * Requires that the caller already holds the connection lock.
987 * @param connection the connection.
990 _dbus_connection_unref_unlocked (DBusConnection *connection)
992 dbus_bool_t last_unref;
994 _dbus_return_if_fail (connection != NULL);
996 /* The connection lock is better than the global
997 * lock in the atomic increment fallback
1000 #ifdef DBUS_HAVE_ATOMIC_INT
1001 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1003 _dbus_assert (connection->refcount.value > 0);
1005 connection->refcount.value -= 1;
1006 last_unref = (connection->refcount.value == 0);
1008 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1013 _dbus_connection_last_unref (connection);
1016 static dbus_uint32_t
1017 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1021 serial = connection->client_serial++;
1023 if (connection->client_serial < 0)
1024 connection->client_serial = 1;
1030 * A callback for use with dbus_watch_new() to create a DBusWatch.
1032 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1033 * and the virtual handle_watch in DBusTransport if we got rid of it.
1034 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1037 * @param watch the watch.
1038 * @param condition the current condition of the file descriptors being watched.
1039 * @param data must be a pointer to a #DBusConnection
1040 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1043 _dbus_connection_handle_watch (DBusWatch *watch,
1044 unsigned int condition,
1047 DBusConnection *connection;
1049 DBusDispatchStatus status;
1053 CONNECTION_LOCK (connection);
1054 _dbus_connection_acquire_io_path (connection, -1);
1055 retval = _dbus_transport_handle_watch (connection->transport,
1057 _dbus_connection_release_io_path (connection);
1059 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1061 /* this calls out to user code */
1062 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1070 * @addtogroup DBusConnection
1076 * Opens a new connection to a remote address.
1078 * @todo specify what the address parameter is. Right now
1079 * it's just the name of a UNIX domain socket. It should be
1080 * something more complex that encodes which transport to use.
1082 * If the open fails, the function returns #NULL, and provides
1083 * a reason for the failure in the result parameter. Pass
1084 * #NULL for the result parameter if you aren't interested
1085 * in the reason for failure.
1087 * @param address the address.
1088 * @param error address where an error can be returned.
1089 * @returns new connection, or #NULL on failure.
1092 dbus_connection_open (const char *address,
1095 DBusConnection *connection;
1096 DBusTransport *transport;
1098 _dbus_return_val_if_fail (address != NULL, NULL);
1099 _dbus_return_val_if_error_is_set (error, NULL);
1101 transport = _dbus_transport_open (address, error);
1102 if (transport == NULL)
1104 _DBUS_ASSERT_ERROR_IS_SET (error);
1108 connection = _dbus_connection_new_for_transport (transport);
1110 _dbus_transport_unref (transport);
1112 if (connection == NULL)
1114 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1122 * Increments the reference count of a DBusConnection.
1124 * @param connection the connection.
1125 * @returns the connection.
1128 dbus_connection_ref (DBusConnection *connection)
1130 _dbus_return_val_if_fail (connection != NULL, NULL);
1132 /* The connection lock is better than the global
1133 * lock in the atomic increment fallback
1136 #ifdef DBUS_HAVE_ATOMIC_INT
1137 _dbus_atomic_inc (&connection->refcount);
1139 CONNECTION_LOCK (connection);
1140 _dbus_assert (connection->refcount.value > 0);
1142 connection->refcount.value += 1;
1143 CONNECTION_UNLOCK (connection);
1150 free_outgoing_message (void *element,
1153 DBusMessage *message = element;
1154 DBusConnection *connection = data;
1156 _dbus_message_remove_size_counter (message,
1157 connection->outgoing_counter,
1159 dbus_message_unref (message);
1162 /* This is run without the mutex held, but after the last reference
1163 * to the connection has been dropped we should have no thread-related
1167 _dbus_connection_last_unref (DBusConnection *connection)
1171 _dbus_verbose ("Finalizing connection %p\n", connection);
1173 _dbus_assert (connection->refcount.value == 0);
1175 /* You have to disconnect the connection before unref:ing it. Otherwise
1176 * you won't get the disconnected message.
1178 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1180 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1181 _dbus_object_tree_free_all_unlocked (connection->objects);
1183 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1184 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1185 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1187 _dbus_watch_list_free (connection->watches);
1188 connection->watches = NULL;
1190 _dbus_timeout_list_free (connection->timeouts);
1191 connection->timeouts = NULL;
1193 _dbus_data_slot_list_free (&connection->slot_list);
1195 link = _dbus_list_get_first_link (&connection->filter_list);
1196 while (link != NULL)
1198 DBusMessageFilter *filter = link->data;
1199 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1201 filter->function = NULL;
1202 _dbus_message_filter_unref (filter); /* calls app callback */
1207 _dbus_list_clear (&connection->filter_list);
1209 /* ---- Done with stuff that invokes application callbacks */
1211 _dbus_object_tree_unref (connection->objects);
1213 _dbus_hash_table_unref (connection->pending_replies);
1214 connection->pending_replies = NULL;
1216 _dbus_list_clear (&connection->filter_list);
1218 _dbus_list_foreach (&connection->outgoing_messages,
1219 free_outgoing_message,
1221 _dbus_list_clear (&connection->outgoing_messages);
1223 _dbus_list_foreach (&connection->incoming_messages,
1224 (DBusForeachFunction) dbus_message_unref,
1226 _dbus_list_clear (&connection->incoming_messages);
1228 _dbus_counter_unref (connection->outgoing_counter);
1230 _dbus_transport_unref (connection->transport);
1232 if (connection->disconnect_message_link)
1234 DBusMessage *message = connection->disconnect_message_link->data;
1235 dbus_message_unref (message);
1236 _dbus_list_free_link (connection->disconnect_message_link);
1239 _dbus_list_clear (&connection->link_cache);
1241 dbus_condvar_free (connection->dispatch_cond);
1242 dbus_condvar_free (connection->io_path_cond);
1243 dbus_condvar_free (connection->message_returned_cond);
1245 dbus_mutex_free (connection->mutex);
1247 dbus_free (connection);
1251 * Decrements the reference count of a DBusConnection, and finalizes
1252 * it if the count reaches zero. It is a bug to drop the last reference
1253 * to a connection that has not been disconnected.
1255 * @todo in practice it can be quite tricky to never unref a connection
1256 * that's still connected; maybe there's some way we could avoid
1259 * @param connection the connection.
1262 dbus_connection_unref (DBusConnection *connection)
1264 dbus_bool_t last_unref;
1266 _dbus_return_if_fail (connection != NULL);
1268 /* The connection lock is better than the global
1269 * lock in the atomic increment fallback
1272 #ifdef DBUS_HAVE_ATOMIC_INT
1273 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1275 CONNECTION_LOCK (connection);
1277 _dbus_assert (connection->refcount.value > 0);
1279 connection->refcount.value -= 1;
1280 last_unref = (connection->refcount.value == 0);
1283 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1286 CONNECTION_UNLOCK (connection);
1290 _dbus_connection_last_unref (connection);
1294 * Closes the connection, so no further data can be sent or received.
1295 * Any further attempts to send data will result in errors. This
1296 * function does not affect the connection's reference count. It's
1297 * safe to disconnect a connection more than once; all calls after the
1298 * first do nothing. It's impossible to "reconnect" a connection, a
1299 * new connection must be created. This function may result in a call
1300 * to the DBusDispatchStatusFunction set with
1301 * dbus_connection_set_dispatch_status_function(), as the disconnect
1302 * message it generates needs to be dispatched.
1304 * @param connection the connection.
1307 dbus_connection_disconnect (DBusConnection *connection)
1309 DBusDispatchStatus status;
1311 _dbus_return_if_fail (connection != NULL);
1313 CONNECTION_LOCK (connection);
1314 _dbus_transport_disconnect (connection->transport);
1316 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1318 /* this calls out to user code */
1319 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
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");
1755 reply_link = _dbus_list_alloc_link (reply);
1756 if (reply_link == NULL)
1758 CONNECTION_UNLOCK (connection);
1759 dbus_message_unref (reply);
1760 goto error_unlocked;
1763 pending->timeout_link = reply_link;
1765 /* Insert the serial in the pending replies hash;
1766 * hash takes a refcount on DBusPendingCall.
1767 * Also, add the timeout.
1769 if (!_dbus_connection_attach_pending_call_unlocked (connection,
1773 if (!_dbus_connection_send_unlocked (connection, message, NULL))
1775 _dbus_connection_detach_pending_call_and_unlock (connection,
1777 goto error_unlocked;
1781 *pending_return = pending;
1783 dbus_pending_call_unref (pending);
1785 CONNECTION_UNLOCK (connection);
1790 CONNECTION_UNLOCK (connection);
1792 dbus_pending_call_unref (pending);
1797 check_for_reply_unlocked (DBusConnection *connection,
1798 dbus_uint32_t client_serial)
1802 link = _dbus_list_get_first_link (&connection->incoming_messages);
1804 while (link != NULL)
1806 DBusMessage *reply = link->data;
1808 if (dbus_message_get_reply_serial (reply) == client_serial)
1810 _dbus_list_remove_link (&connection->incoming_messages, link);
1811 connection->n_incoming -= 1;
1814 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1821 * Blocks a certain time period while waiting for a reply.
1822 * If no reply arrives, returns #NULL.
1824 * @todo could use performance improvements (it keeps scanning
1825 * the whole message queue for example) and has thread issues,
1826 * see comments in source
1828 * Does not re-enter the main loop or run filter/path-registered
1829 * callbacks. The reply to the message will not be seen by
1832 * @param connection the connection
1833 * @param client_serial the reply serial to wait for
1834 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1835 * @returns the message that is the reply or #NULL if no reply
1838 _dbus_connection_block_for_reply (DBusConnection *connection,
1839 dbus_uint32_t client_serial,
1840 int timeout_milliseconds)
1842 long start_tv_sec, start_tv_usec;
1843 long end_tv_sec, end_tv_usec;
1844 long tv_sec, tv_usec;
1845 DBusDispatchStatus status;
1847 _dbus_return_val_if_fail (connection != NULL, NULL);
1848 _dbus_return_val_if_fail (client_serial != 0, NULL);
1849 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1851 if (timeout_milliseconds == -1)
1852 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
1854 /* it would probably seem logical to pass in _DBUS_INT_MAX
1855 * for infinite timeout, but then math below would get
1856 * all overflow-prone, so smack that down.
1858 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1859 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1861 /* Flush message queue */
1862 dbus_connection_flush (connection);
1864 CONNECTION_LOCK (connection);
1866 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1867 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1868 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1869 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1870 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1872 _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",
1873 timeout_milliseconds,
1875 start_tv_sec, start_tv_usec,
1876 end_tv_sec, end_tv_usec);
1878 /* Now we wait... */
1879 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1880 * gets the message before we do?
1882 /* always block at least once as we know we don't have the reply yet */
1883 _dbus_connection_do_iteration (connection,
1884 DBUS_ITERATION_DO_READING |
1885 DBUS_ITERATION_BLOCK,
1886 timeout_milliseconds);
1890 /* queue messages and get status */
1891 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1893 if (status == DBUS_DISPATCH_DATA_REMAINS)
1897 reply = check_for_reply_unlocked (connection, client_serial);
1900 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1902 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
1904 /* Unlocks, and calls out to user code */
1905 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1911 _dbus_get_current_time (&tv_sec, &tv_usec);
1913 if (!_dbus_connection_get_is_connected_unlocked (connection))
1915 else if (tv_sec < start_tv_sec)
1916 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
1917 else if (connection->disconnect_message_link == NULL)
1918 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
1919 else if (tv_sec < end_tv_sec ||
1920 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1922 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1923 (end_tv_usec - tv_usec) / 1000;
1924 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1925 _dbus_assert (timeout_milliseconds >= 0);
1927 if (status == DBUS_DISPATCH_NEED_MEMORY)
1929 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1930 * we may already have a reply in the buffer and just can't process
1933 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1935 if (timeout_milliseconds < 100)
1936 ; /* just busy loop */
1937 else if (timeout_milliseconds <= 1000)
1938 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1940 _dbus_sleep_milliseconds (1000);
1944 /* block again, we don't have the reply buffered yet. */
1945 _dbus_connection_do_iteration (connection,
1946 DBUS_ITERATION_DO_READING |
1947 DBUS_ITERATION_BLOCK,
1948 timeout_milliseconds);
1951 goto recheck_status;
1954 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
1955 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
1957 /* unlocks and calls out to user code */
1958 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1964 * Sends a message and blocks a certain time period while waiting for
1965 * a reply. This function does not reenter the main loop,
1966 * i.e. messages other than the reply are queued up but not
1967 * processed. This function is used to do non-reentrant "method
1970 * If a normal reply is received, it is returned, and removed from the
1971 * incoming message queue. If it is not received, #NULL is returned
1972 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
1973 * received, it is converted to a #DBusError and returned as an error,
1974 * then the reply message is deleted. If something else goes wrong,
1975 * result is set to whatever is appropriate, such as
1976 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1978 * @param connection the connection
1979 * @param message the message to send
1980 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1981 * @param error return location for error message
1982 * @returns the message that is the reply or #NULL with an error code if the
1986 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1987 DBusMessage *message,
1988 int timeout_milliseconds,
1991 dbus_uint32_t client_serial;
1994 _dbus_return_val_if_fail (connection != NULL, NULL);
1995 _dbus_return_val_if_fail (message != NULL, NULL);
1996 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1997 _dbus_return_val_if_error_is_set (error, NULL);
1999 if (!dbus_connection_send (connection, message, &client_serial))
2001 _DBUS_SET_OOM (error);
2005 reply = _dbus_connection_block_for_reply (connection,
2007 timeout_milliseconds);
2011 if (dbus_connection_get_is_connected (connection))
2012 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
2014 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
2018 else if (dbus_set_error_from_message (error, reply))
2020 dbus_message_unref (reply);
2028 * Blocks until the outgoing message queue is empty.
2030 * @param connection the connection.
2033 dbus_connection_flush (DBusConnection *connection)
2035 /* We have to specify DBUS_ITERATION_DO_READING here because
2036 * otherwise we could have two apps deadlock if they are both doing
2037 * a flush(), and the kernel buffers fill up. This could change the
2040 DBusDispatchStatus status;
2042 _dbus_return_if_fail (connection != NULL);
2044 CONNECTION_LOCK (connection);
2045 while (connection->n_outgoing > 0 &&
2046 _dbus_connection_get_is_connected_unlocked (connection))
2047 _dbus_connection_do_iteration (connection,
2048 DBUS_ITERATION_DO_READING |
2049 DBUS_ITERATION_DO_WRITING |
2050 DBUS_ITERATION_BLOCK,
2053 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2055 /* Unlocks and calls out to user code */
2056 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2059 /* Call with mutex held. Will drop it while waiting and re-acquire
2063 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
2065 _dbus_assert (connection->message_borrowed != NULL);
2067 while (connection->message_borrowed != NULL)
2068 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
2072 * Returns the first-received message from the incoming message queue,
2073 * leaving it in the queue. If the queue is empty, returns #NULL.
2075 * The caller does not own a reference to the returned message, and
2076 * must either return it using dbus_connection_return_message() or
2077 * keep it after calling dbus_connection_steal_borrowed_message(). No
2078 * one can get at the message while its borrowed, so return it as
2079 * quickly as possible and don't keep a reference to it after
2080 * returning it. If you need to keep the message, make a copy of it.
2082 * @param connection the connection.
2083 * @returns next message in the incoming queue.
2086 dbus_connection_borrow_message (DBusConnection *connection)
2088 DBusMessage *message;
2089 DBusDispatchStatus status;
2091 _dbus_return_val_if_fail (connection != NULL, NULL);
2092 /* can't borrow during dispatch */
2093 _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL);
2095 /* this is called for the side effect that it queues
2096 * up any messages from the transport
2098 status = dbus_connection_get_dispatch_status (connection);
2099 if (status != DBUS_DISPATCH_DATA_REMAINS)
2102 CONNECTION_LOCK (connection);
2104 if (connection->message_borrowed != NULL)
2105 _dbus_connection_wait_for_borrowed (connection);
2107 message = _dbus_list_get_first (&connection->incoming_messages);
2110 connection->message_borrowed = message;
2112 CONNECTION_UNLOCK (connection);
2117 * Used to return a message after peeking at it using
2118 * dbus_connection_borrow_message().
2120 * @param connection the connection
2121 * @param message the message from dbus_connection_borrow_message()
2124 dbus_connection_return_message (DBusConnection *connection,
2125 DBusMessage *message)
2127 _dbus_return_if_fail (connection != NULL);
2128 _dbus_return_if_fail (message != NULL);
2129 /* can't borrow during dispatch */
2130 _dbus_return_if_fail (!connection->dispatch_acquired);
2132 CONNECTION_LOCK (connection);
2134 _dbus_assert (message == connection->message_borrowed);
2136 connection->message_borrowed = NULL;
2137 dbus_condvar_wake_all (connection->message_returned_cond);
2139 CONNECTION_UNLOCK (connection);
2143 * Used to keep a message after peeking at it using
2144 * dbus_connection_borrow_message(). Before using this function, see
2145 * the caveats/warnings in the documentation for
2146 * dbus_connection_pop_message().
2148 * @param connection the connection
2149 * @param message the message from dbus_connection_borrow_message()
2152 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2153 DBusMessage *message)
2155 DBusMessage *pop_message;
2157 _dbus_return_if_fail (connection != NULL);
2158 _dbus_return_if_fail (message != NULL);
2159 /* can't borrow during dispatch */
2160 _dbus_return_if_fail (!connection->dispatch_acquired);
2162 CONNECTION_LOCK (connection);
2164 _dbus_assert (message == connection->message_borrowed);
2166 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
2167 _dbus_assert (message == pop_message);
2169 connection->n_incoming -= 1;
2171 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
2172 message, connection->n_incoming);
2174 connection->message_borrowed = NULL;
2175 dbus_condvar_wake_all (connection->message_returned_cond);
2177 CONNECTION_UNLOCK (connection);
2180 /* See dbus_connection_pop_message, but requires the caller to own
2181 * the lock before calling. May drop the lock while running.
2184 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
2186 if (connection->message_borrowed != NULL)
2187 _dbus_connection_wait_for_borrowed (connection);
2189 if (connection->n_incoming > 0)
2193 link = _dbus_list_pop_first_link (&connection->incoming_messages);
2194 connection->n_incoming -= 1;
2196 _dbus_verbose ("Message %p (%d %s '%s') removed from incoming queue %p, %d incoming\n",
2198 dbus_message_get_type (link->data),
2199 dbus_message_get_interface (link->data) ?
2200 dbus_message_get_interface (link->data) :
2202 dbus_message_get_signature (link->data),
2203 connection, connection->n_incoming);
2211 /* See dbus_connection_pop_message, but requires the caller to own
2212 * the lock before calling. May drop the lock while running.
2215 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
2219 link = _dbus_connection_pop_message_link_unlocked (connection);
2223 DBusMessage *message;
2225 message = link->data;
2227 _dbus_list_free_link (link);
2236 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
2237 DBusList *message_link)
2239 _dbus_assert (message_link != NULL);
2240 /* You can't borrow a message while a link is outstanding */
2241 _dbus_assert (connection->message_borrowed == NULL);
2243 _dbus_list_prepend_link (&connection->incoming_messages,
2245 connection->n_incoming += 1;
2247 _dbus_verbose ("Message %p (%d %s '%s') put back into queue %p, %d incoming\n",
2249 dbus_message_get_type (message_link->data),
2250 dbus_message_get_interface (message_link->data) ?
2251 dbus_message_get_interface (message_link->data) :
2253 dbus_message_get_signature (message_link->data),
2254 connection, connection->n_incoming);
2258 * Returns the first-received message from the incoming message queue,
2259 * removing it from the queue. The caller owns a reference to the
2260 * returned message. If the queue is empty, returns #NULL.
2262 * This function bypasses any message handlers that are registered,
2263 * and so using it is usually wrong. Instead, let the main loop invoke
2264 * dbus_connection_dispatch(). Popping messages manually is only
2265 * useful in very simple programs that don't share a #DBusConnection
2266 * with any libraries or other modules.
2268 * @param connection the connection.
2269 * @returns next message in the incoming queue.
2272 dbus_connection_pop_message (DBusConnection *connection)
2274 DBusMessage *message;
2275 DBusDispatchStatus status;
2277 /* this is called for the side effect that it queues
2278 * up any messages from the transport
2280 status = dbus_connection_get_dispatch_status (connection);
2281 if (status != DBUS_DISPATCH_DATA_REMAINS)
2284 CONNECTION_LOCK (connection);
2286 message = _dbus_connection_pop_message_unlocked (connection);
2288 _dbus_verbose ("Returning popped message %p\n", message);
2290 CONNECTION_UNLOCK (connection);
2296 * Acquire the dispatcher. This must be done before dispatching
2297 * messages in order to guarantee the right order of
2298 * message delivery. May sleep and drop the connection mutex
2299 * while waiting for the dispatcher.
2301 * @param connection the connection.
2304 _dbus_connection_acquire_dispatch (DBusConnection *connection)
2306 if (connection->dispatch_acquired)
2307 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
2308 _dbus_assert (!connection->dispatch_acquired);
2310 connection->dispatch_acquired = TRUE;
2314 * Release the dispatcher when you're done with it. Only call
2315 * after you've acquired the dispatcher. Wakes up at most one
2316 * thread currently waiting to acquire the dispatcher.
2318 * @param connection the connection.
2321 _dbus_connection_release_dispatch (DBusConnection *connection)
2323 _dbus_assert (connection->dispatch_acquired);
2325 connection->dispatch_acquired = FALSE;
2326 dbus_condvar_wake_one (connection->dispatch_cond);
2330 _dbus_connection_failed_pop (DBusConnection *connection,
2331 DBusList *message_link)
2333 _dbus_list_prepend_link (&connection->incoming_messages,
2335 connection->n_incoming += 1;
2338 static DBusDispatchStatus
2339 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
2341 if (connection->n_incoming > 0)
2342 return DBUS_DISPATCH_DATA_REMAINS;
2343 else if (!_dbus_transport_queue_messages (connection->transport))
2344 return DBUS_DISPATCH_NEED_MEMORY;
2347 DBusDispatchStatus status;
2349 status = _dbus_transport_get_dispatch_status (connection->transport);
2351 if (status == DBUS_DISPATCH_COMPLETE &&
2352 connection->disconnect_message_link &&
2353 !_dbus_transport_get_is_connected (connection->transport))
2355 /* We haven't sent the disconnect message already,
2356 * and all real messages have been queued up.
2358 _dbus_connection_queue_synthesized_message_link (connection,
2359 connection->disconnect_message_link);
2360 connection->disconnect_message_link = NULL;
2363 if (status != DBUS_DISPATCH_COMPLETE)
2365 else if (connection->n_incoming > 0)
2366 return DBUS_DISPATCH_DATA_REMAINS;
2368 return DBUS_DISPATCH_COMPLETE;
2373 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
2374 DBusDispatchStatus new_status)
2376 dbus_bool_t changed;
2377 DBusDispatchStatusFunction function;
2380 /* We have the lock */
2382 _dbus_connection_ref_unlocked (connection);
2384 changed = new_status != connection->last_dispatch_status;
2386 connection->last_dispatch_status = new_status;
2388 function = connection->dispatch_status_function;
2389 data = connection->dispatch_status_data;
2391 /* We drop the lock */
2392 CONNECTION_UNLOCK (connection);
2394 if (changed && function)
2396 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
2397 connection, new_status,
2398 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
2399 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
2400 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
2402 (* function) (connection, new_status, data);
2405 dbus_connection_unref (connection);
2409 * Gets the current state (what we would currently return
2410 * from dbus_connection_dispatch()) but doesn't actually
2411 * dispatch any messages.
2413 * @param connection the connection.
2414 * @returns current dispatch status
2417 dbus_connection_get_dispatch_status (DBusConnection *connection)
2419 DBusDispatchStatus status;
2421 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2423 CONNECTION_LOCK (connection);
2425 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2427 CONNECTION_UNLOCK (connection);
2433 * Processes data buffered while handling watches, queueing zero or
2434 * more incoming messages. Then pops the first-received message from
2435 * the current incoming message queue, runs any handlers for it, and
2436 * unrefs the message. Returns a status indicating whether messages/data
2437 * remain, more memory is needed, or all data has been processed.
2439 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
2440 * does not necessarily dispatch a message, as the data may
2441 * be part of authentication or the like.
2443 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
2445 * @todo right now a message filter gets run on replies to a pending
2446 * call in here, but not in the case where we block without entering
2447 * the main loop. Simple solution might be to just have the pending
2448 * call stuff run before the filters.
2450 * @todo FIXME what if we call out to application code to handle a
2451 * message, holding the dispatch lock, and the application code runs
2452 * the main loop and dispatches again? Probably deadlocks at the
2453 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
2454 * and then the GSource etc. could handle the situation?
2456 * @param connection the connection
2457 * @returns dispatch status
2460 dbus_connection_dispatch (DBusConnection *connection)
2462 DBusMessage *message;
2463 DBusList *link, *filter_list_copy, *message_link;
2464 DBusHandlerResult result;
2465 DBusPendingCall *pending;
2466 dbus_int32_t reply_serial;
2467 DBusDispatchStatus status;
2469 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2471 CONNECTION_LOCK (connection);
2472 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2473 if (status != DBUS_DISPATCH_DATA_REMAINS)
2475 /* unlocks and calls out to user code */
2476 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2480 /* We need to ref the connection since the callback could potentially
2481 * drop the last ref to it
2483 _dbus_connection_ref_unlocked (connection);
2485 _dbus_connection_acquire_dispatch (connection);
2487 /* This call may drop the lock during the execution (if waiting for
2488 * borrowed messages to be returned) but the order of message
2489 * dispatch if several threads call dispatch() is still
2490 * protected by the lock, since only one will get the lock, and that
2491 * one will finish the message dispatching
2493 message_link = _dbus_connection_pop_message_link_unlocked (connection);
2494 if (message_link == NULL)
2496 /* another thread dispatched our stuff */
2498 _dbus_connection_release_dispatch (connection);
2500 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2502 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2504 dbus_connection_unref (connection);
2509 message = message_link->data;
2511 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2513 reply_serial = dbus_message_get_reply_serial (message);
2514 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
2517 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
2519 _dbus_connection_release_dispatch (connection);
2521 _dbus_connection_failed_pop (connection, message_link);
2523 /* unlocks and calls user code */
2524 _dbus_connection_update_dispatch_status_and_unlock (connection,
2525 DBUS_DISPATCH_NEED_MEMORY);
2527 dbus_connection_unref (connection);
2529 return DBUS_DISPATCH_NEED_MEMORY;
2532 _dbus_list_foreach (&filter_list_copy,
2533 (DBusForeachFunction)_dbus_message_filter_ref,
2536 /* We're still protected from dispatch() reentrancy here
2537 * since we acquired the dispatcher
2539 CONNECTION_UNLOCK (connection);
2541 link = _dbus_list_get_first_link (&filter_list_copy);
2542 while (link != NULL)
2544 DBusMessageFilter *filter = link->data;
2545 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2547 _dbus_verbose (" running filter on message %p\n", message);
2548 result = (* filter->function) (connection, message, filter->user_data);
2550 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2556 _dbus_list_foreach (&filter_list_copy,
2557 (DBusForeachFunction)_dbus_message_filter_unref,
2559 _dbus_list_clear (&filter_list_copy);
2561 CONNECTION_LOCK (connection);
2563 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2566 /* Did a reply we were waiting on get filtered? */
2567 if (pending && result == DBUS_HANDLER_RESULT_HANDLED)
2569 /* Queue the timeout immediately! */
2570 if (pending->timeout_link)
2572 _dbus_connection_queue_synthesized_message_link (connection,
2573 pending->timeout_link);
2574 pending->timeout_link = NULL;
2578 /* We already queued the timeout? Then it was filtered! */
2579 _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial);
2583 if (result == DBUS_HANDLER_RESULT_HANDLED)
2588 _dbus_pending_call_complete_and_unlock (pending, message);
2592 CONNECTION_LOCK (connection);
2596 /* We're still protected from dispatch() reentrancy here
2597 * since we acquired the dispatcher
2599 _dbus_verbose (" running object path dispatch on message %p (%d %s '%s')\n",
2601 dbus_message_get_type (message),
2602 dbus_message_get_interface (message) ?
2603 dbus_message_get_interface (message) :
2605 dbus_message_get_signature (message));
2607 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
2610 CONNECTION_LOCK (connection);
2612 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2615 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
2619 DBusPreallocatedSend *preallocated;
2621 _dbus_verbose (" sending error %s\n",
2622 DBUS_ERROR_UNKNOWN_METHOD);
2624 if (!_dbus_string_init (&str))
2626 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2630 if (!_dbus_string_append_printf (&str,
2631 "Method \"%s\" on interface \"%s\" doesn't exist\n",
2632 dbus_message_get_member (message),
2633 dbus_message_get_interface (message)))
2635 _dbus_string_free (&str);
2636 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2640 reply = dbus_message_new_error (message,
2641 DBUS_ERROR_UNKNOWN_METHOD,
2642 _dbus_string_get_const_data (&str));
2643 _dbus_string_free (&str);
2647 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2651 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2653 if (preallocated == NULL)
2655 dbus_message_unref (reply);
2656 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2660 _dbus_connection_send_preallocated_unlocked (connection, preallocated,
2663 dbus_message_unref (reply);
2665 result = DBUS_HANDLER_RESULT_HANDLED;
2668 _dbus_verbose (" done dispatching %p (%d %s '%s') on connection %p\n", message,
2669 dbus_message_get_type (message),
2670 dbus_message_get_interface (message) ?
2671 dbus_message_get_interface (message) :
2673 dbus_message_get_signature (message),
2677 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2679 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
2681 /* Put message back, and we'll start over.
2682 * Yes this means handlers must be idempotent if they
2683 * don't return HANDLED; c'est la vie.
2685 _dbus_connection_putback_message_link_unlocked (connection,
2690 _dbus_verbose ("Done with message in %s\n", _DBUS_FUNCTION_NAME);
2692 if (connection->exit_on_disconnect &&
2693 dbus_message_is_signal (message,
2694 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
2697 _dbus_verbose ("Exiting on Disconnected signal\n");
2698 CONNECTION_UNLOCK (connection);
2700 _dbus_assert_not_reached ("Call to exit() returned");
2703 _dbus_list_free_link (message_link);
2704 dbus_message_unref (message); /* don't want the message to count in max message limits
2705 * in computing dispatch status below
2709 _dbus_connection_release_dispatch (connection);
2711 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2713 /* unlocks and calls user code */
2714 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2716 dbus_connection_unref (connection);
2722 * Sets the watch functions for the connection. These functions are
2723 * responsible for making the application's main loop aware of file
2724 * descriptors that need to be monitored for events, using select() or
2725 * poll(). When using Qt, typically the DBusAddWatchFunction would
2726 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2727 * could call g_io_add_watch(), or could be used as part of a more
2728 * elaborate GSource. Note that when a watch is added, it may
2731 * The DBusWatchToggledFunction notifies the application that the
2732 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2733 * to check this. A disabled watch should have no effect, and enabled
2734 * watch should be added to the main loop. This feature is used
2735 * instead of simply adding/removing the watch because
2736 * enabling/disabling can be done without memory allocation. The
2737 * toggled function may be NULL if a main loop re-queries
2738 * dbus_watch_get_enabled() every time anyway.
2740 * The DBusWatch can be queried for the file descriptor to watch using
2741 * dbus_watch_get_fd(), and for the events to watch for using
2742 * dbus_watch_get_flags(). The flags returned by
2743 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2744 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2745 * all watches implicitly include a watch for hangups, errors, and
2746 * other exceptional conditions.
2748 * Once a file descriptor becomes readable or writable, or an exception
2749 * occurs, dbus_watch_handle() should be called to
2750 * notify the connection of the file descriptor's condition.
2752 * dbus_watch_handle() cannot be called during the
2753 * DBusAddWatchFunction, as the connection will not be ready to handle
2756 * It is not allowed to reference a DBusWatch after it has been passed
2757 * to remove_function.
2759 * If #FALSE is returned due to lack of memory, the failure may be due
2760 * to a #FALSE return from the new add_function. If so, the
2761 * add_function may have been called successfully one or more times,
2762 * but the remove_function will also have been called to remove any
2763 * successful adds. i.e. if #FALSE is returned the net result
2764 * should be that dbus_connection_set_watch_functions() has no effect,
2765 * but the add_function and remove_function may have been called.
2767 * @todo We need to drop the lock when we call the
2768 * add/remove/toggled functions which can be a side effect
2769 * of setting the watch functions.
2771 * @param connection the connection.
2772 * @param add_function function to begin monitoring a new descriptor.
2773 * @param remove_function function to stop monitoring a descriptor.
2774 * @param toggled_function function to notify of enable/disable
2775 * @param data data to pass to add_function and remove_function.
2776 * @param free_data_function function to be called to free the data.
2777 * @returns #FALSE on failure (no memory)
2780 dbus_connection_set_watch_functions (DBusConnection *connection,
2781 DBusAddWatchFunction add_function,
2782 DBusRemoveWatchFunction remove_function,
2783 DBusWatchToggledFunction toggled_function,
2785 DBusFreeFunction free_data_function)
2789 _dbus_return_val_if_fail (connection != NULL, FALSE);
2791 CONNECTION_LOCK (connection);
2792 /* ref connection for slightly better reentrancy */
2793 _dbus_connection_ref_unlocked (connection);
2795 /* FIXME this can call back into user code, and we need to drop the
2796 * connection lock when it does.
2798 retval = _dbus_watch_list_set_functions (connection->watches,
2799 add_function, remove_function,
2801 data, free_data_function);
2803 CONNECTION_UNLOCK (connection);
2804 /* drop our paranoid refcount */
2805 dbus_connection_unref (connection);
2811 * Sets the timeout functions for the connection. These functions are
2812 * responsible for making the application's main loop aware of timeouts.
2813 * When using Qt, typically the DBusAddTimeoutFunction would create a
2814 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2817 * The DBusTimeoutToggledFunction notifies the application that the
2818 * timeout has been enabled or disabled. Call
2819 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2820 * have no effect, and enabled timeout should be added to the main
2821 * loop. This feature is used instead of simply adding/removing the
2822 * timeout because enabling/disabling can be done without memory
2823 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2824 * to enable and disable. The toggled function may be NULL if a main
2825 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2826 * Whenever a timeout is toggled, its interval may change.
2828 * The DBusTimeout can be queried for the timer interval using
2829 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
2830 * repeatedly, each time the interval elapses, starting after it has
2831 * elapsed once. The timeout stops firing when it is removed with the
2832 * given remove_function. The timer interval may change whenever the
2833 * timeout is added, removed, or toggled.
2835 * @param connection the connection.
2836 * @param add_function function to add a timeout.
2837 * @param remove_function function to remove a timeout.
2838 * @param toggled_function function to notify of enable/disable
2839 * @param data data to pass to add_function and remove_function.
2840 * @param free_data_function function to be called to free the data.
2841 * @returns #FALSE on failure (no memory)
2844 dbus_connection_set_timeout_functions (DBusConnection *connection,
2845 DBusAddTimeoutFunction add_function,
2846 DBusRemoveTimeoutFunction remove_function,
2847 DBusTimeoutToggledFunction toggled_function,
2849 DBusFreeFunction free_data_function)
2853 _dbus_return_val_if_fail (connection != NULL, FALSE);
2855 CONNECTION_LOCK (connection);
2856 /* ref connection for slightly better reentrancy */
2857 _dbus_connection_ref_unlocked (connection);
2859 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2860 add_function, remove_function,
2862 data, free_data_function);
2864 CONNECTION_UNLOCK (connection);
2865 /* drop our paranoid refcount */
2866 dbus_connection_unref (connection);
2872 * Sets the mainloop wakeup function for the connection. Thi function is
2873 * responsible for waking up the main loop (if its sleeping) when some some
2874 * change has happened to the connection that the mainloop needs to reconsiders
2875 * (e.g. a message has been queued for writing).
2876 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2877 * When using GLib, it would call g_main_context_wakeup().
2880 * @param connection the connection.
2881 * @param wakeup_main_function function to wake up the mainloop
2882 * @param data data to pass wakeup_main_function
2883 * @param free_data_function function to be called to free the data.
2886 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2887 DBusWakeupMainFunction wakeup_main_function,
2889 DBusFreeFunction free_data_function)
2892 DBusFreeFunction old_free_data;
2894 _dbus_return_if_fail (connection != NULL);
2896 CONNECTION_LOCK (connection);
2897 old_data = connection->wakeup_main_data;
2898 old_free_data = connection->free_wakeup_main_data;
2900 connection->wakeup_main_function = wakeup_main_function;
2901 connection->wakeup_main_data = data;
2902 connection->free_wakeup_main_data = free_data_function;
2904 CONNECTION_UNLOCK (connection);
2906 /* Callback outside the lock */
2908 (*old_free_data) (old_data);
2912 * Set a function to be invoked when the dispatch status changes.
2913 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
2914 * dbus_connection_dispatch() needs to be called to process incoming
2915 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
2916 * from inside the DBusDispatchStatusFunction. Indeed, almost
2917 * any reentrancy in this function is a bad idea. Instead,
2918 * the DBusDispatchStatusFunction should simply save an indication
2919 * that messages should be dispatched later, when the main loop
2922 * @param connection the connection
2923 * @param function function to call on dispatch status changes
2924 * @param data data for function
2925 * @param free_data_function free the function data
2928 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
2929 DBusDispatchStatusFunction function,
2931 DBusFreeFunction free_data_function)
2934 DBusFreeFunction old_free_data;
2936 _dbus_return_if_fail (connection != NULL);
2938 CONNECTION_LOCK (connection);
2939 old_data = connection->dispatch_status_data;
2940 old_free_data = connection->free_dispatch_status_data;
2942 connection->dispatch_status_function = function;
2943 connection->dispatch_status_data = data;
2944 connection->free_dispatch_status_data = free_data_function;
2946 CONNECTION_UNLOCK (connection);
2948 /* Callback outside the lock */
2950 (*old_free_data) (old_data);
2954 * Gets the UNIX user ID of the connection if any.
2955 * Returns #TRUE if the uid is filled in.
2956 * Always returns #FALSE on non-UNIX platforms.
2957 * Always returns #FALSE prior to authenticating the
2960 * @param connection the connection
2961 * @param uid return location for the user ID
2962 * @returns #TRUE if uid is filled in with a valid user ID
2965 dbus_connection_get_unix_user (DBusConnection *connection,
2970 _dbus_return_val_if_fail (connection != NULL, FALSE);
2971 _dbus_return_val_if_fail (uid != NULL, FALSE);
2973 CONNECTION_LOCK (connection);
2975 if (!_dbus_transport_get_is_authenticated (connection->transport))
2978 result = _dbus_transport_get_unix_user (connection->transport,
2980 CONNECTION_UNLOCK (connection);
2986 * Sets a predicate function used to determine whether a given user ID
2987 * is allowed to connect. When an incoming connection has
2988 * authenticated with a particular user ID, this function is called;
2989 * if it returns #TRUE, the connection is allowed to proceed,
2990 * otherwise the connection is disconnected.
2992 * If the function is set to #NULL (as it is by default), then
2993 * only the same UID as the server process will be allowed to
2996 * @param connection the connection
2997 * @param function the predicate
2998 * @param data data to pass to the predicate
2999 * @param free_data_function function to free the data
3002 dbus_connection_set_unix_user_function (DBusConnection *connection,
3003 DBusAllowUnixUserFunction function,
3005 DBusFreeFunction free_data_function)
3007 void *old_data = NULL;
3008 DBusFreeFunction old_free_function = NULL;
3010 _dbus_return_if_fail (connection != NULL);
3012 CONNECTION_LOCK (connection);
3013 _dbus_transport_set_unix_user_function (connection->transport,
3014 function, data, free_data_function,
3015 &old_data, &old_free_function);
3016 CONNECTION_UNLOCK (connection);
3018 if (old_free_function != NULL)
3019 (* old_free_function) (old_data);
3023 * Adds a message filter. Filters are handlers that are run on all
3024 * incoming messages, prior to the objects registered with
3025 * dbus_connection_register_object_path(). Filters are run in the
3026 * order that they were added. The same handler can be added as a
3027 * filter more than once, in which case it will be run more than once.
3028 * Filters added during a filter callback won't be run on the message
3031 * @todo we don't run filters on messages while blocking without
3032 * entering the main loop, since filters are run as part of
3033 * dbus_connection_dispatch(). This is probably a feature, as filters
3034 * could create arbitrary reentrancy. But kind of sucks if you're
3035 * trying to filter METHOD_RETURN for some reason.
3037 * @param connection the connection
3038 * @param function function to handle messages
3039 * @param user_data user data to pass to the function
3040 * @param free_data_function function to use for freeing user data
3041 * @returns #TRUE on success, #FALSE if not enough memory.
3044 dbus_connection_add_filter (DBusConnection *connection,
3045 DBusHandleMessageFunction function,
3047 DBusFreeFunction free_data_function)
3049 DBusMessageFilter *filter;
3051 _dbus_return_val_if_fail (connection != NULL, FALSE);
3052 _dbus_return_val_if_fail (function != NULL, FALSE);
3054 filter = dbus_new0 (DBusMessageFilter, 1);
3058 filter->refcount.value = 1;
3060 CONNECTION_LOCK (connection);
3062 if (!_dbus_list_append (&connection->filter_list,
3065 _dbus_message_filter_unref (filter);
3066 CONNECTION_UNLOCK (connection);
3070 /* Fill in filter after all memory allocated,
3071 * so we don't run the free_user_data_function
3072 * if the add_filter() fails
3075 filter->function = function;
3076 filter->user_data = user_data;
3077 filter->free_user_data_function = free_data_function;
3079 CONNECTION_UNLOCK (connection);
3084 * Removes a previously-added message filter. It is a programming
3085 * error to call this function for a handler that has not been added
3086 * as a filter. If the given handler was added more than once, only
3087 * one instance of it will be removed (the most recently-added
3090 * @param connection the connection
3091 * @param function the handler to remove
3092 * @param user_data user data for the handler to remove
3096 dbus_connection_remove_filter (DBusConnection *connection,
3097 DBusHandleMessageFunction function,
3101 DBusMessageFilter *filter;
3103 _dbus_return_if_fail (connection != NULL);
3104 _dbus_return_if_fail (function != NULL);
3106 CONNECTION_LOCK (connection);
3110 link = _dbus_list_get_last_link (&connection->filter_list);
3111 while (link != NULL)
3113 filter = link->data;
3115 if (filter->function == function &&
3116 filter->user_data == user_data)
3118 _dbus_list_remove_link (&connection->filter_list, link);
3119 filter->function = NULL;
3124 link = _dbus_list_get_prev_link (&connection->filter_list, link);
3127 CONNECTION_UNLOCK (connection);
3129 #ifndef DBUS_DISABLE_CHECKS
3132 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
3133 function, user_data);
3138 /* Call application code */
3139 if (filter->free_user_data_function)
3140 (* filter->free_user_data_function) (filter->user_data);
3142 filter->free_user_data_function = NULL;
3143 filter->user_data = NULL;
3145 _dbus_message_filter_unref (filter);
3149 * Registers a handler for a given path in the object hierarchy.
3150 * The given vtable handles messages sent to exactly the given path.
3153 * @param connection the connection
3154 * @param path #NULL-terminated array of path elements
3155 * @param vtable the virtual table
3156 * @param user_data data to pass to functions in the vtable
3157 * @returns #FALSE if not enough memory
3160 dbus_connection_register_object_path (DBusConnection *connection,
3162 const DBusObjectPathVTable *vtable,
3167 _dbus_return_val_if_fail (connection != NULL, FALSE);
3168 _dbus_return_val_if_fail (path != NULL, FALSE);
3169 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3170 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3172 CONNECTION_LOCK (connection);
3174 retval = _dbus_object_tree_register (connection->objects,
3179 CONNECTION_UNLOCK (connection);
3185 * Registers a fallback handler for a given subsection of the object
3186 * hierarchy. The given vtable handles messages at or below the given
3187 * path. You can use this to establish a default message handling
3188 * policy for a whole "subdirectory."
3190 * @param connection the connection
3191 * @param path #NULL-terminated array of path elements
3192 * @param vtable the virtual table
3193 * @param user_data data to pass to functions in the vtable
3194 * @returns #FALSE if not enough memory
3197 dbus_connection_register_fallback (DBusConnection *connection,
3199 const DBusObjectPathVTable *vtable,
3204 _dbus_return_val_if_fail (connection != NULL, FALSE);
3205 _dbus_return_val_if_fail (path != NULL, FALSE);
3206 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3207 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3209 CONNECTION_LOCK (connection);
3211 retval = _dbus_object_tree_register (connection->objects,
3216 CONNECTION_UNLOCK (connection);
3222 * Unregisters the handler registered with exactly the given path.
3223 * It's a bug to call this function for a path that isn't registered.
3224 * Can unregister both fallback paths and object paths.
3226 * @param connection the connection
3227 * @param path the #NULL-terminated array of path elements
3230 dbus_connection_unregister_object_path (DBusConnection *connection,
3233 _dbus_return_if_fail (connection != NULL);
3234 _dbus_return_if_fail (path != NULL);
3235 _dbus_return_if_fail (path[0] != NULL);
3237 CONNECTION_LOCK (connection);
3239 _dbus_object_tree_unregister_and_unlock (connection->objects, path);
3243 * Lists the registered fallback handlers and object path handlers at
3244 * the given parent_path. The returned array should be freed with
3245 * dbus_free_string_array().
3247 * @param connection the connection
3248 * @param parent_path the path to list the child handlers of
3249 * @param child_entries returns #NULL-terminated array of children
3250 * @returns #FALSE if no memory to allocate the child entries
3253 dbus_connection_list_registered (DBusConnection *connection,
3254 const char **parent_path,
3255 char ***child_entries)
3257 _dbus_return_val_if_fail (connection != NULL, FALSE);
3258 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
3259 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
3261 CONNECTION_LOCK (connection);
3263 return _dbus_object_tree_list_registered_and_unlock (connection->objects,
3268 static DBusDataSlotAllocator slot_allocator;
3269 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
3272 * Allocates an integer ID to be used for storing application-specific
3273 * data on any DBusConnection. The allocated ID may then be used
3274 * with dbus_connection_set_data() and dbus_connection_get_data().
3275 * The passed-in slot must be initialized to -1, and is filled in
3276 * with the slot ID. If the passed-in slot is not -1, it's assumed
3277 * to be already allocated, and its refcount is incremented.
3279 * The allocated slot is global, i.e. all DBusConnection objects will
3280 * have a slot with the given integer ID reserved.
3282 * @param slot_p address of a global variable storing the slot
3283 * @returns #FALSE on failure (no memory)
3286 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
3288 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3289 _DBUS_LOCK_NAME (connection_slots),
3294 * Deallocates a global ID for connection data slots.
3295 * dbus_connection_get_data() and dbus_connection_set_data() may no
3296 * longer be used with this slot. Existing data stored on existing
3297 * DBusConnection objects will be freed when the connection is
3298 * finalized, but may not be retrieved (and may only be replaced if
3299 * someone else reallocates the slot). When the refcount on the
3300 * passed-in slot reaches 0, it is set to -1.
3302 * @param slot_p address storing the slot to deallocate
3305 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
3307 _dbus_return_if_fail (*slot_p >= 0);
3309 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3313 * Stores a pointer on a DBusConnection, along
3314 * with an optional function to be used for freeing
3315 * the data when the data is set again, or when
3316 * the connection is finalized. The slot number
3317 * must have been allocated with dbus_connection_allocate_data_slot().
3319 * @param connection the connection
3320 * @param slot the slot number
3321 * @param data the data to store
3322 * @param free_data_func finalizer function for the data
3323 * @returns #TRUE if there was enough memory to store the data
3326 dbus_connection_set_data (DBusConnection *connection,
3329 DBusFreeFunction free_data_func)
3331 DBusFreeFunction old_free_func;
3335 _dbus_return_val_if_fail (connection != NULL, FALSE);
3336 _dbus_return_val_if_fail (slot >= 0, FALSE);
3338 CONNECTION_LOCK (connection);
3340 retval = _dbus_data_slot_list_set (&slot_allocator,
3341 &connection->slot_list,
3342 slot, data, free_data_func,
3343 &old_free_func, &old_data);
3345 CONNECTION_UNLOCK (connection);
3349 /* Do the actual free outside the connection lock */
3351 (* old_free_func) (old_data);
3358 * Retrieves data previously set with dbus_connection_set_data().
3359 * The slot must still be allocated (must not have been freed).
3361 * @param connection the connection
3362 * @param slot the slot to get data from
3363 * @returns the data, or #NULL if not found
3366 dbus_connection_get_data (DBusConnection *connection,
3371 _dbus_return_val_if_fail (connection != NULL, NULL);
3373 CONNECTION_LOCK (connection);
3375 res = _dbus_data_slot_list_get (&slot_allocator,
3376 &connection->slot_list,
3379 CONNECTION_UNLOCK (connection);
3385 * This function sets a global flag for whether dbus_connection_new()
3386 * will set SIGPIPE behavior to SIG_IGN.
3388 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
3391 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
3393 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
3397 * Specifies the maximum size message this connection is allowed to
3398 * receive. Larger messages will result in disconnecting the
3401 * @param connection a #DBusConnection
3402 * @param size maximum message size the connection can receive, in bytes
3405 dbus_connection_set_max_message_size (DBusConnection *connection,
3408 _dbus_return_if_fail (connection != NULL);
3410 CONNECTION_LOCK (connection);
3411 _dbus_transport_set_max_message_size (connection->transport,
3413 CONNECTION_UNLOCK (connection);
3417 * Gets the value set by dbus_connection_set_max_message_size().
3419 * @param connection the connection
3420 * @returns the max size of a single message
3423 dbus_connection_get_max_message_size (DBusConnection *connection)
3427 _dbus_return_val_if_fail (connection != NULL, 0);
3429 CONNECTION_LOCK (connection);
3430 res = _dbus_transport_get_max_message_size (connection->transport);
3431 CONNECTION_UNLOCK (connection);
3436 * Sets the maximum total number of bytes that can be used for all messages
3437 * received on this connection. Messages count toward the maximum until
3438 * they are finalized. When the maximum is reached, the connection will
3439 * not read more data until some messages are finalized.
3441 * The semantics of the maximum are: if outstanding messages are
3442 * already above the maximum, additional messages will not be read.
3443 * The semantics are not: if the next message would cause us to exceed
3444 * the maximum, we don't read it. The reason is that we don't know the
3445 * size of a message until after we read it.
3447 * Thus, the max live messages size can actually be exceeded
3448 * by up to the maximum size of a single message.
3450 * Also, if we read say 1024 bytes off the wire in a single read(),
3451 * and that contains a half-dozen small messages, we may exceed the
3452 * size max by that amount. But this should be inconsequential.
3454 * This does imply that we can't call read() with a buffer larger
3455 * than we're willing to exceed this limit by.
3457 * @param connection the connection
3458 * @param size the maximum size in bytes of all outstanding messages
3461 dbus_connection_set_max_received_size (DBusConnection *connection,
3464 _dbus_return_if_fail (connection != NULL);
3466 CONNECTION_LOCK (connection);
3467 _dbus_transport_set_max_received_size (connection->transport,
3469 CONNECTION_UNLOCK (connection);
3473 * Gets the value set by dbus_connection_set_max_received_size().
3475 * @param connection the connection
3476 * @returns the max size of all live messages
3479 dbus_connection_get_max_received_size (DBusConnection *connection)
3483 _dbus_return_val_if_fail (connection != NULL, 0);
3485 CONNECTION_LOCK (connection);
3486 res = _dbus_transport_get_max_received_size (connection->transport);
3487 CONNECTION_UNLOCK (connection);
3492 * Gets the approximate size in bytes of all messages in the outgoing
3493 * message queue. The size is approximate in that you shouldn't use
3494 * it to decide how many bytes to read off the network or anything
3495 * of that nature, as optimizations may choose to tell small white lies
3496 * to avoid performance overhead.
3498 * @param connection the connection
3499 * @returns the number of bytes that have been queued up but not sent
3502 dbus_connection_get_outgoing_size (DBusConnection *connection)
3506 _dbus_return_val_if_fail (connection != NULL, 0);
3508 CONNECTION_LOCK (connection);
3509 res = _dbus_counter_get_value (connection->outgoing_counter);
3510 CONNECTION_UNLOCK (connection);