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");
1754 CONNECTION_UNLOCK (connection);
1755 dbus_pending_call_unref (pending);
1759 reply_link = _dbus_list_alloc_link (reply);
1762 CONNECTION_UNLOCK (connection);
1763 dbus_message_unref (reply);
1764 dbus_pending_call_unref (pending);
1768 pending->timeout_link = reply_link;
1770 /* Insert the serial in the pending replies hash;
1771 * hash takes a refcount on DBusPendingCall.
1772 * Also, add the timeout.
1774 if (!_dbus_connection_attach_pending_call_unlocked (connection,
1777 CONNECTION_UNLOCK (connection);
1778 dbus_pending_call_unref (pending);
1782 if (!_dbus_connection_send_unlocked (connection, message, NULL))
1784 _dbus_connection_detach_pending_call_and_unlock (connection,
1791 dbus_pending_call_ref (pending);
1792 *pending_return = pending;
1795 CONNECTION_UNLOCK (connection);
1801 check_for_reply_unlocked (DBusConnection *connection,
1802 dbus_uint32_t client_serial)
1806 link = _dbus_list_get_first_link (&connection->incoming_messages);
1808 while (link != NULL)
1810 DBusMessage *reply = link->data;
1812 if (dbus_message_get_reply_serial (reply) == client_serial)
1814 _dbus_list_remove_link (&connection->incoming_messages, link);
1815 connection->n_incoming -= 1;
1816 dbus_message_ref (reply);
1819 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1826 * Blocks a certain time period while waiting for a reply.
1827 * If no reply arrives, returns #NULL.
1829 * @todo could use performance improvements (it keeps scanning
1830 * the whole message queue for example) and has thread issues,
1831 * see comments in source
1833 * Does not re-enter the main loop or run filter/path-registered
1834 * callbacks. The reply to the message will not be seen by
1837 * @param connection the connection
1838 * @param client_serial the reply serial to wait for
1839 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1840 * @returns the message that is the reply or #NULL if no reply
1843 _dbus_connection_block_for_reply (DBusConnection *connection,
1844 dbus_uint32_t client_serial,
1845 int timeout_milliseconds)
1847 long start_tv_sec, start_tv_usec;
1848 long end_tv_sec, end_tv_usec;
1849 long tv_sec, tv_usec;
1850 DBusDispatchStatus status;
1852 _dbus_return_val_if_fail (connection != NULL, NULL);
1853 _dbus_return_val_if_fail (client_serial != 0, NULL);
1854 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1856 if (timeout_milliseconds == -1)
1857 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
1859 /* it would probably seem logical to pass in _DBUS_INT_MAX
1860 * for infinite timeout, but then math below would get
1861 * all overflow-prone, so smack that down.
1863 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1864 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1866 /* Flush message queue */
1867 dbus_connection_flush (connection);
1869 CONNECTION_LOCK (connection);
1871 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1872 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1873 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1874 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1875 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1877 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec to %ld sec %ld usec\n",
1878 timeout_milliseconds,
1880 start_tv_sec, start_tv_usec,
1881 end_tv_sec, end_tv_usec);
1883 /* Now we wait... */
1884 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1885 * gets the message before we do?
1887 /* always block at least once as we know we don't have the reply yet */
1888 _dbus_connection_do_iteration (connection,
1889 DBUS_ITERATION_DO_READING |
1890 DBUS_ITERATION_BLOCK,
1891 timeout_milliseconds);
1895 /* queue messages and get status */
1896 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1898 if (status == DBUS_DISPATCH_DATA_REMAINS)
1902 reply = check_for_reply_unlocked (connection, client_serial);
1905 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1907 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
1909 /* Unlocks, and calls out to user code */
1910 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1916 _dbus_get_current_time (&tv_sec, &tv_usec);
1918 if (tv_sec < start_tv_sec)
1919 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
1920 else if (connection->disconnect_message_link == NULL)
1921 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
1922 else if (tv_sec < end_tv_sec ||
1923 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1925 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1926 (end_tv_usec - tv_usec) / 1000;
1927 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1928 _dbus_assert (timeout_milliseconds >= 0);
1930 if (status == DBUS_DISPATCH_NEED_MEMORY)
1932 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1933 * we may already have a reply in the buffer and just can't process
1936 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1938 if (timeout_milliseconds < 100)
1939 ; /* just busy loop */
1940 else if (timeout_milliseconds <= 1000)
1941 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1943 _dbus_sleep_milliseconds (1000);
1947 /* block again, we don't have the reply buffered yet. */
1948 _dbus_connection_do_iteration (connection,
1949 DBUS_ITERATION_DO_READING |
1950 DBUS_ITERATION_BLOCK,
1951 timeout_milliseconds);
1954 goto recheck_status;
1957 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
1958 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
1960 /* unlocks and calls out to user code */
1961 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1967 * Sends a message and blocks a certain time period while waiting for
1968 * a reply. This function does not reenter the main loop,
1969 * i.e. messages other than the reply are queued up but not
1970 * processed. This function is used to do non-reentrant "method
1973 * If a normal reply is received, it is returned, and removed from the
1974 * incoming message queue. If it is not received, #NULL is returned
1975 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
1976 * received, it is converted to a #DBusError and returned as an error,
1977 * then the reply message is deleted. If something else goes wrong,
1978 * result is set to whatever is appropriate, such as
1979 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1981 * @param connection the connection
1982 * @param message the message to send
1983 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1984 * @param error return location for error message
1985 * @returns the message that is the reply or #NULL with an error code if the
1989 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1990 DBusMessage *message,
1991 int timeout_milliseconds,
1994 dbus_uint32_t client_serial;
1997 _dbus_return_val_if_fail (connection != NULL, NULL);
1998 _dbus_return_val_if_fail (message != NULL, NULL);
1999 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
2000 _dbus_return_val_if_error_is_set (error, NULL);
2002 if (!dbus_connection_send (connection, message, &client_serial))
2004 _DBUS_SET_OOM (error);
2008 reply = _dbus_connection_block_for_reply (connection,
2010 timeout_milliseconds);
2014 if (dbus_connection_get_is_connected (connection))
2015 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
2017 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
2021 else if (dbus_set_error_from_message (error, reply))
2023 dbus_message_unref (reply);
2031 * Blocks until the outgoing message queue is empty.
2033 * @param connection the connection.
2036 dbus_connection_flush (DBusConnection *connection)
2038 /* We have to specify DBUS_ITERATION_DO_READING here because
2039 * otherwise we could have two apps deadlock if they are both doing
2040 * a flush(), and the kernel buffers fill up. This could change the
2043 DBusDispatchStatus status;
2045 _dbus_return_if_fail (connection != NULL);
2047 CONNECTION_LOCK (connection);
2048 while (connection->n_outgoing > 0 &&
2049 _dbus_connection_get_is_connected_unlocked (connection))
2050 _dbus_connection_do_iteration (connection,
2051 DBUS_ITERATION_DO_READING |
2052 DBUS_ITERATION_DO_WRITING |
2053 DBUS_ITERATION_BLOCK,
2056 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2058 /* Unlocks and calls out to user code */
2059 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2062 /* Call with mutex held. Will drop it while waiting and re-acquire
2066 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
2068 _dbus_assert (connection->message_borrowed != NULL);
2070 while (connection->message_borrowed != NULL)
2071 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
2075 * Returns the first-received message from the incoming message queue,
2076 * leaving it in the queue. If the queue is empty, returns #NULL.
2078 * The caller does not own a reference to the returned message, and
2079 * must either return it using dbus_connection_return_message() or
2080 * keep it after calling dbus_connection_steal_borrowed_message(). No
2081 * one can get at the message while its borrowed, so return it as
2082 * quickly as possible and don't keep a reference to it after
2083 * returning it. If you need to keep the message, make a copy of it.
2085 * @param connection the connection.
2086 * @returns next message in the incoming queue.
2089 dbus_connection_borrow_message (DBusConnection *connection)
2091 DBusMessage *message;
2092 DBusDispatchStatus status;
2094 _dbus_return_val_if_fail (connection != NULL, NULL);
2095 /* can't borrow during dispatch */
2096 _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL);
2098 /* this is called for the side effect that it queues
2099 * up any messages from the transport
2101 status = dbus_connection_get_dispatch_status (connection);
2102 if (status != DBUS_DISPATCH_DATA_REMAINS)
2105 CONNECTION_LOCK (connection);
2107 if (connection->message_borrowed != NULL)
2108 _dbus_connection_wait_for_borrowed (connection);
2110 message = _dbus_list_get_first (&connection->incoming_messages);
2113 connection->message_borrowed = message;
2115 CONNECTION_UNLOCK (connection);
2120 * Used to return a message after peeking at it using
2121 * dbus_connection_borrow_message().
2123 * @param connection the connection
2124 * @param message the message from dbus_connection_borrow_message()
2127 dbus_connection_return_message (DBusConnection *connection,
2128 DBusMessage *message)
2130 _dbus_return_if_fail (connection != NULL);
2131 _dbus_return_if_fail (message != NULL);
2132 /* can't borrow during dispatch */
2133 _dbus_return_if_fail (!connection->dispatch_acquired);
2135 CONNECTION_LOCK (connection);
2137 _dbus_assert (message == connection->message_borrowed);
2139 connection->message_borrowed = NULL;
2140 dbus_condvar_wake_all (connection->message_returned_cond);
2142 CONNECTION_UNLOCK (connection);
2146 * Used to keep a message after peeking at it using
2147 * dbus_connection_borrow_message(). Before using this function, see
2148 * the caveats/warnings in the documentation for
2149 * dbus_connection_pop_message().
2151 * @param connection the connection
2152 * @param message the message from dbus_connection_borrow_message()
2155 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2156 DBusMessage *message)
2158 DBusMessage *pop_message;
2160 _dbus_return_if_fail (connection != NULL);
2161 _dbus_return_if_fail (message != NULL);
2162 /* can't borrow during dispatch */
2163 _dbus_return_if_fail (!connection->dispatch_acquired);
2165 CONNECTION_LOCK (connection);
2167 _dbus_assert (message == connection->message_borrowed);
2169 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
2170 _dbus_assert (message == pop_message);
2172 connection->n_incoming -= 1;
2174 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
2175 message, connection->n_incoming);
2177 connection->message_borrowed = NULL;
2178 dbus_condvar_wake_all (connection->message_returned_cond);
2180 CONNECTION_UNLOCK (connection);
2183 /* See dbus_connection_pop_message, but requires the caller to own
2184 * the lock before calling. May drop the lock while running.
2187 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
2189 if (connection->message_borrowed != NULL)
2190 _dbus_connection_wait_for_borrowed (connection);
2192 if (connection->n_incoming > 0)
2196 link = _dbus_list_pop_first_link (&connection->incoming_messages);
2197 connection->n_incoming -= 1;
2199 _dbus_verbose ("Message %p (%d %s '%s') removed from incoming queue %p, %d incoming\n",
2201 dbus_message_get_type (link->data),
2202 dbus_message_get_interface (link->data) ?
2203 dbus_message_get_interface (link->data) :
2205 dbus_message_get_signature (link->data),
2206 connection, connection->n_incoming);
2214 /* See dbus_connection_pop_message, but requires the caller to own
2215 * the lock before calling. May drop the lock while running.
2218 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
2222 link = _dbus_connection_pop_message_link_unlocked (connection);
2226 DBusMessage *message;
2228 message = link->data;
2230 _dbus_list_free_link (link);
2239 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
2240 DBusList *message_link)
2242 _dbus_assert (message_link != NULL);
2243 /* You can't borrow a message while a link is outstanding */
2244 _dbus_assert (connection->message_borrowed == NULL);
2246 _dbus_list_prepend_link (&connection->incoming_messages,
2248 connection->n_incoming += 1;
2250 _dbus_verbose ("Message %p (%d %s '%s') put back into queue %p, %d incoming\n",
2252 dbus_message_get_type (message_link->data),
2253 dbus_message_get_interface (message_link->data) ?
2254 dbus_message_get_interface (message_link->data) :
2256 dbus_message_get_signature (message_link->data),
2257 connection, connection->n_incoming);
2261 * Returns the first-received message from the incoming message queue,
2262 * removing it from the queue. The caller owns a reference to the
2263 * returned message. If the queue is empty, returns #NULL.
2265 * This function bypasses any message handlers that are registered,
2266 * and so using it is usually wrong. Instead, let the main loop invoke
2267 * dbus_connection_dispatch(). Popping messages manually is only
2268 * useful in very simple programs that don't share a #DBusConnection
2269 * with any libraries or other modules.
2271 * @param connection the connection.
2272 * @returns next message in the incoming queue.
2275 dbus_connection_pop_message (DBusConnection *connection)
2277 DBusMessage *message;
2278 DBusDispatchStatus status;
2280 /* this is called for the side effect that it queues
2281 * up any messages from the transport
2283 status = dbus_connection_get_dispatch_status (connection);
2284 if (status != DBUS_DISPATCH_DATA_REMAINS)
2287 CONNECTION_LOCK (connection);
2289 message = _dbus_connection_pop_message_unlocked (connection);
2291 _dbus_verbose ("Returning popped message %p\n", message);
2293 CONNECTION_UNLOCK (connection);
2299 * Acquire the dispatcher. This must be done before dispatching
2300 * messages in order to guarantee the right order of
2301 * message delivery. May sleep and drop the connection mutex
2302 * while waiting for the dispatcher.
2304 * @param connection the connection.
2307 _dbus_connection_acquire_dispatch (DBusConnection *connection)
2309 if (connection->dispatch_acquired)
2310 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
2311 _dbus_assert (!connection->dispatch_acquired);
2313 connection->dispatch_acquired = TRUE;
2317 * Release the dispatcher when you're done with it. Only call
2318 * after you've acquired the dispatcher. Wakes up at most one
2319 * thread currently waiting to acquire the dispatcher.
2321 * @param connection the connection.
2324 _dbus_connection_release_dispatch (DBusConnection *connection)
2326 _dbus_assert (connection->dispatch_acquired);
2328 connection->dispatch_acquired = FALSE;
2329 dbus_condvar_wake_one (connection->dispatch_cond);
2333 _dbus_connection_failed_pop (DBusConnection *connection,
2334 DBusList *message_link)
2336 _dbus_list_prepend_link (&connection->incoming_messages,
2338 connection->n_incoming += 1;
2341 static DBusDispatchStatus
2342 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
2344 if (connection->n_incoming > 0)
2345 return DBUS_DISPATCH_DATA_REMAINS;
2346 else if (!_dbus_transport_queue_messages (connection->transport))
2347 return DBUS_DISPATCH_NEED_MEMORY;
2350 DBusDispatchStatus status;
2352 status = _dbus_transport_get_dispatch_status (connection->transport);
2354 if (status == DBUS_DISPATCH_COMPLETE &&
2355 connection->disconnect_message_link &&
2356 !_dbus_transport_get_is_connected (connection->transport))
2358 /* We haven't sent the disconnect message already,
2359 * and all real messages have been queued up.
2361 _dbus_connection_queue_synthesized_message_link (connection,
2362 connection->disconnect_message_link);
2363 connection->disconnect_message_link = NULL;
2366 if (status != DBUS_DISPATCH_COMPLETE)
2368 else if (connection->n_incoming > 0)
2369 return DBUS_DISPATCH_DATA_REMAINS;
2371 return DBUS_DISPATCH_COMPLETE;
2376 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
2377 DBusDispatchStatus new_status)
2379 dbus_bool_t changed;
2380 DBusDispatchStatusFunction function;
2383 /* We have the lock */
2385 _dbus_connection_ref_unlocked (connection);
2387 changed = new_status != connection->last_dispatch_status;
2389 connection->last_dispatch_status = new_status;
2391 function = connection->dispatch_status_function;
2392 data = connection->dispatch_status_data;
2394 /* We drop the lock */
2395 CONNECTION_UNLOCK (connection);
2397 if (changed && function)
2399 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
2400 connection, new_status,
2401 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
2402 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
2403 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
2405 (* function) (connection, new_status, data);
2408 dbus_connection_unref (connection);
2412 * Gets the current state (what we would currently return
2413 * from dbus_connection_dispatch()) but doesn't actually
2414 * dispatch any messages.
2416 * @param connection the connection.
2417 * @returns current dispatch status
2420 dbus_connection_get_dispatch_status (DBusConnection *connection)
2422 DBusDispatchStatus status;
2424 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2426 CONNECTION_LOCK (connection);
2428 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2430 CONNECTION_UNLOCK (connection);
2436 * Processes data buffered while handling watches, queueing zero or
2437 * more incoming messages. Then pops the first-received message from
2438 * the current incoming message queue, runs any handlers for it, and
2439 * unrefs the message. Returns a status indicating whether messages/data
2440 * remain, more memory is needed, or all data has been processed.
2442 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
2443 * does not necessarily dispatch a message, as the data may
2444 * be part of authentication or the like.
2446 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
2448 * @todo right now a message filter gets run on replies to a pending
2449 * call in here, but not in the case where we block without entering
2450 * the main loop. Simple solution might be to just have the pending
2451 * call stuff run before the filters.
2453 * @todo FIXME what if we call out to application code to handle a
2454 * message, holding the dispatch lock, and the application code runs
2455 * the main loop and dispatches again? Probably deadlocks at the
2456 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
2457 * and then the GSource etc. could handle the situation?
2459 * @param connection the connection
2460 * @returns dispatch status
2463 dbus_connection_dispatch (DBusConnection *connection)
2465 DBusMessage *message;
2466 DBusList *link, *filter_list_copy, *message_link;
2467 DBusHandlerResult result;
2468 DBusPendingCall *pending;
2469 dbus_int32_t reply_serial;
2470 DBusDispatchStatus status;
2472 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2474 CONNECTION_LOCK (connection);
2475 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2476 if (status != DBUS_DISPATCH_DATA_REMAINS)
2478 /* unlocks and calls out to user code */
2479 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2483 /* We need to ref the connection since the callback could potentially
2484 * drop the last ref to it
2486 _dbus_connection_ref_unlocked (connection);
2488 _dbus_connection_acquire_dispatch (connection);
2490 /* This call may drop the lock during the execution (if waiting for
2491 * borrowed messages to be returned) but the order of message
2492 * dispatch if several threads call dispatch() is still
2493 * protected by the lock, since only one will get the lock, and that
2494 * one will finish the message dispatching
2496 message_link = _dbus_connection_pop_message_link_unlocked (connection);
2497 if (message_link == NULL)
2499 /* another thread dispatched our stuff */
2501 _dbus_connection_release_dispatch (connection);
2503 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2505 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2507 dbus_connection_unref (connection);
2512 message = message_link->data;
2514 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2516 reply_serial = dbus_message_get_reply_serial (message);
2517 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
2520 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
2522 _dbus_connection_release_dispatch (connection);
2524 _dbus_connection_failed_pop (connection, message_link);
2526 /* unlocks and calls user code */
2527 _dbus_connection_update_dispatch_status_and_unlock (connection,
2528 DBUS_DISPATCH_NEED_MEMORY);
2530 dbus_connection_unref (connection);
2532 return DBUS_DISPATCH_NEED_MEMORY;
2535 _dbus_list_foreach (&filter_list_copy,
2536 (DBusForeachFunction)_dbus_message_filter_ref,
2539 /* We're still protected from dispatch() reentrancy here
2540 * since we acquired the dispatcher
2542 CONNECTION_UNLOCK (connection);
2544 link = _dbus_list_get_first_link (&filter_list_copy);
2545 while (link != NULL)
2547 DBusMessageFilter *filter = link->data;
2548 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2550 _dbus_verbose (" running filter on message %p\n", message);
2551 result = (* filter->function) (connection, message, filter->user_data);
2553 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2559 _dbus_list_foreach (&filter_list_copy,
2560 (DBusForeachFunction)_dbus_message_filter_unref,
2562 _dbus_list_clear (&filter_list_copy);
2564 CONNECTION_LOCK (connection);
2566 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2569 /* Did a reply we were waiting on get filtered? */
2570 if (pending && result == DBUS_HANDLER_RESULT_HANDLED)
2572 /* Queue the timeout immediately! */
2573 if (pending->timeout_link)
2575 _dbus_connection_queue_synthesized_message_link (connection,
2576 pending->timeout_link);
2577 pending->timeout_link = NULL;
2581 /* We already queued the timeout? Then it was filtered! */
2582 _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial);
2586 if (result == DBUS_HANDLER_RESULT_HANDLED)
2591 _dbus_pending_call_complete_and_unlock (pending, message);
2595 CONNECTION_LOCK (connection);
2599 /* We're still protected from dispatch() reentrancy here
2600 * since we acquired the dispatcher
2602 _dbus_verbose (" running object path dispatch on message %p (%d %s '%s')\n",
2604 dbus_message_get_type (message),
2605 dbus_message_get_interface (message) ?
2606 dbus_message_get_interface (message) :
2608 dbus_message_get_signature (message));
2610 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
2613 CONNECTION_LOCK (connection);
2615 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2618 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
2622 DBusPreallocatedSend *preallocated;
2624 _dbus_verbose (" sending error %s\n",
2625 DBUS_ERROR_UNKNOWN_METHOD);
2627 if (!_dbus_string_init (&str))
2629 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2633 if (!_dbus_string_append_printf (&str,
2634 "Method \"%s\" on interface \"%s\" doesn't exist\n",
2635 dbus_message_get_member (message),
2636 dbus_message_get_interface (message)))
2638 _dbus_string_free (&str);
2639 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2643 reply = dbus_message_new_error (message,
2644 DBUS_ERROR_UNKNOWN_METHOD,
2645 _dbus_string_get_const_data (&str));
2646 _dbus_string_free (&str);
2650 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2654 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2656 if (preallocated == NULL)
2658 dbus_message_unref (reply);
2659 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2663 _dbus_connection_send_preallocated_unlocked (connection, preallocated,
2666 dbus_message_unref (reply);
2668 result = DBUS_HANDLER_RESULT_HANDLED;
2671 _dbus_verbose (" done dispatching %p (%d %s '%s') on connection %p\n", message,
2672 dbus_message_get_type (message),
2673 dbus_message_get_interface (message) ?
2674 dbus_message_get_interface (message) :
2676 dbus_message_get_signature (message),
2680 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2682 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
2684 /* Put message back, and we'll start over.
2685 * Yes this means handlers must be idempotent if they
2686 * don't return HANDLED; c'est la vie.
2688 _dbus_connection_putback_message_link_unlocked (connection,
2693 _dbus_verbose ("Done with message in %s\n", _DBUS_FUNCTION_NAME);
2695 if (connection->exit_on_disconnect &&
2696 dbus_message_is_signal (message,
2697 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
2700 _dbus_verbose ("Exiting on Disconnected signal\n");
2701 CONNECTION_UNLOCK (connection);
2703 _dbus_assert_not_reached ("Call to exit() returned");
2706 _dbus_list_free_link (message_link);
2707 dbus_message_unref (message); /* don't want the message to count in max message limits
2708 * in computing dispatch status below
2712 _dbus_connection_release_dispatch (connection);
2714 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2716 /* unlocks and calls user code */
2717 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2719 dbus_connection_unref (connection);
2725 * Sets the watch functions for the connection. These functions are
2726 * responsible for making the application's main loop aware of file
2727 * descriptors that need to be monitored for events, using select() or
2728 * poll(). When using Qt, typically the DBusAddWatchFunction would
2729 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2730 * could call g_io_add_watch(), or could be used as part of a more
2731 * elaborate GSource. Note that when a watch is added, it may
2734 * The DBusWatchToggledFunction notifies the application that the
2735 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2736 * to check this. A disabled watch should have no effect, and enabled
2737 * watch should be added to the main loop. This feature is used
2738 * instead of simply adding/removing the watch because
2739 * enabling/disabling can be done without memory allocation. The
2740 * toggled function may be NULL if a main loop re-queries
2741 * dbus_watch_get_enabled() every time anyway.
2743 * The DBusWatch can be queried for the file descriptor to watch using
2744 * dbus_watch_get_fd(), and for the events to watch for using
2745 * dbus_watch_get_flags(). The flags returned by
2746 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2747 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2748 * all watches implicitly include a watch for hangups, errors, and
2749 * other exceptional conditions.
2751 * Once a file descriptor becomes readable or writable, or an exception
2752 * occurs, dbus_watch_handle() should be called to
2753 * notify the connection of the file descriptor's condition.
2755 * dbus_watch_handle() cannot be called during the
2756 * DBusAddWatchFunction, as the connection will not be ready to handle
2759 * It is not allowed to reference a DBusWatch after it has been passed
2760 * to remove_function.
2762 * If #FALSE is returned due to lack of memory, the failure may be due
2763 * to a #FALSE return from the new add_function. If so, the
2764 * add_function may have been called successfully one or more times,
2765 * but the remove_function will also have been called to remove any
2766 * successful adds. i.e. if #FALSE is returned the net result
2767 * should be that dbus_connection_set_watch_functions() has no effect,
2768 * but the add_function and remove_function may have been called.
2770 * @todo We need to drop the lock when we call the
2771 * add/remove/toggled functions which can be a side effect
2772 * of setting the watch functions.
2774 * @param connection the connection.
2775 * @param add_function function to begin monitoring a new descriptor.
2776 * @param remove_function function to stop monitoring a descriptor.
2777 * @param toggled_function function to notify of enable/disable
2778 * @param data data to pass to add_function and remove_function.
2779 * @param free_data_function function to be called to free the data.
2780 * @returns #FALSE on failure (no memory)
2783 dbus_connection_set_watch_functions (DBusConnection *connection,
2784 DBusAddWatchFunction add_function,
2785 DBusRemoveWatchFunction remove_function,
2786 DBusWatchToggledFunction toggled_function,
2788 DBusFreeFunction free_data_function)
2792 _dbus_return_val_if_fail (connection != NULL, FALSE);
2794 CONNECTION_LOCK (connection);
2795 /* ref connection for slightly better reentrancy */
2796 _dbus_connection_ref_unlocked (connection);
2798 /* FIXME this can call back into user code, and we need to drop the
2799 * connection lock when it does.
2801 retval = _dbus_watch_list_set_functions (connection->watches,
2802 add_function, remove_function,
2804 data, free_data_function);
2806 CONNECTION_UNLOCK (connection);
2807 /* drop our paranoid refcount */
2808 dbus_connection_unref (connection);
2814 * Sets the timeout functions for the connection. These functions are
2815 * responsible for making the application's main loop aware of timeouts.
2816 * When using Qt, typically the DBusAddTimeoutFunction would create a
2817 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2820 * The DBusTimeoutToggledFunction notifies the application that the
2821 * timeout has been enabled or disabled. Call
2822 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2823 * have no effect, and enabled timeout should be added to the main
2824 * loop. This feature is used instead of simply adding/removing the
2825 * timeout because enabling/disabling can be done without memory
2826 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2827 * to enable and disable. The toggled function may be NULL if a main
2828 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2829 * Whenever a timeout is toggled, its interval may change.
2831 * The DBusTimeout can be queried for the timer interval using
2832 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
2833 * repeatedly, each time the interval elapses, starting after it has
2834 * elapsed once. The timeout stops firing when it is removed with the
2835 * given remove_function. The timer interval may change whenever the
2836 * timeout is added, removed, or toggled.
2838 * @param connection the connection.
2839 * @param add_function function to add a timeout.
2840 * @param remove_function function to remove a timeout.
2841 * @param toggled_function function to notify of enable/disable
2842 * @param data data to pass to add_function and remove_function.
2843 * @param free_data_function function to be called to free the data.
2844 * @returns #FALSE on failure (no memory)
2847 dbus_connection_set_timeout_functions (DBusConnection *connection,
2848 DBusAddTimeoutFunction add_function,
2849 DBusRemoveTimeoutFunction remove_function,
2850 DBusTimeoutToggledFunction toggled_function,
2852 DBusFreeFunction free_data_function)
2856 _dbus_return_val_if_fail (connection != NULL, FALSE);
2858 CONNECTION_LOCK (connection);
2859 /* ref connection for slightly better reentrancy */
2860 _dbus_connection_ref_unlocked (connection);
2862 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2863 add_function, remove_function,
2865 data, free_data_function);
2867 CONNECTION_UNLOCK (connection);
2868 /* drop our paranoid refcount */
2869 dbus_connection_unref (connection);
2875 * Sets the mainloop wakeup function for the connection. Thi function is
2876 * responsible for waking up the main loop (if its sleeping) when some some
2877 * change has happened to the connection that the mainloop needs to reconsiders
2878 * (e.g. a message has been queued for writing).
2879 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2880 * When using GLib, it would call g_main_context_wakeup().
2883 * @param connection the connection.
2884 * @param wakeup_main_function function to wake up the mainloop
2885 * @param data data to pass wakeup_main_function
2886 * @param free_data_function function to be called to free the data.
2889 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2890 DBusWakeupMainFunction wakeup_main_function,
2892 DBusFreeFunction free_data_function)
2895 DBusFreeFunction old_free_data;
2897 _dbus_return_if_fail (connection != NULL);
2899 CONNECTION_LOCK (connection);
2900 old_data = connection->wakeup_main_data;
2901 old_free_data = connection->free_wakeup_main_data;
2903 connection->wakeup_main_function = wakeup_main_function;
2904 connection->wakeup_main_data = data;
2905 connection->free_wakeup_main_data = free_data_function;
2907 CONNECTION_UNLOCK (connection);
2909 /* Callback outside the lock */
2911 (*old_free_data) (old_data);
2915 * Set a function to be invoked when the dispatch status changes.
2916 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
2917 * dbus_connection_dispatch() needs to be called to process incoming
2918 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
2919 * from inside the DBusDispatchStatusFunction. Indeed, almost
2920 * any reentrancy in this function is a bad idea. Instead,
2921 * the DBusDispatchStatusFunction should simply save an indication
2922 * that messages should be dispatched later, when the main loop
2925 * @param connection the connection
2926 * @param function function to call on dispatch status changes
2927 * @param data data for function
2928 * @param free_data_function free the function data
2931 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
2932 DBusDispatchStatusFunction function,
2934 DBusFreeFunction free_data_function)
2937 DBusFreeFunction old_free_data;
2939 _dbus_return_if_fail (connection != NULL);
2941 CONNECTION_LOCK (connection);
2942 old_data = connection->dispatch_status_data;
2943 old_free_data = connection->free_dispatch_status_data;
2945 connection->dispatch_status_function = function;
2946 connection->dispatch_status_data = data;
2947 connection->free_dispatch_status_data = free_data_function;
2949 CONNECTION_UNLOCK (connection);
2951 /* Callback outside the lock */
2953 (*old_free_data) (old_data);
2957 * Gets the UNIX user ID of the connection if any.
2958 * Returns #TRUE if the uid is filled in.
2959 * Always returns #FALSE on non-UNIX platforms.
2960 * Always returns #FALSE prior to authenticating the
2963 * @param connection the connection
2964 * @param uid return location for the user ID
2965 * @returns #TRUE if uid is filled in with a valid user ID
2968 dbus_connection_get_unix_user (DBusConnection *connection,
2973 _dbus_return_val_if_fail (connection != NULL, FALSE);
2974 _dbus_return_val_if_fail (uid != NULL, FALSE);
2976 CONNECTION_LOCK (connection);
2978 if (!_dbus_transport_get_is_authenticated (connection->transport))
2981 result = _dbus_transport_get_unix_user (connection->transport,
2983 CONNECTION_UNLOCK (connection);
2989 * Sets a predicate function used to determine whether a given user ID
2990 * is allowed to connect. When an incoming connection has
2991 * authenticated with a particular user ID, this function is called;
2992 * if it returns #TRUE, the connection is allowed to proceed,
2993 * otherwise the connection is disconnected.
2995 * If the function is set to #NULL (as it is by default), then
2996 * only the same UID as the server process will be allowed to
2999 * @param connection the connection
3000 * @param function the predicate
3001 * @param data data to pass to the predicate
3002 * @param free_data_function function to free the data
3005 dbus_connection_set_unix_user_function (DBusConnection *connection,
3006 DBusAllowUnixUserFunction function,
3008 DBusFreeFunction free_data_function)
3010 void *old_data = NULL;
3011 DBusFreeFunction old_free_function = NULL;
3013 _dbus_return_if_fail (connection != NULL);
3015 CONNECTION_LOCK (connection);
3016 _dbus_transport_set_unix_user_function (connection->transport,
3017 function, data, free_data_function,
3018 &old_data, &old_free_function);
3019 CONNECTION_UNLOCK (connection);
3021 if (old_free_function != NULL)
3022 (* old_free_function) (old_data);
3026 * Adds a message filter. Filters are handlers that are run on all
3027 * incoming messages, prior to the objects registered with
3028 * dbus_connection_register_object_path(). Filters are run in the
3029 * order that they were added. The same handler can be added as a
3030 * filter more than once, in which case it will be run more than once.
3031 * Filters added during a filter callback won't be run on the message
3034 * @todo we don't run filters on messages while blocking without
3035 * entering the main loop, since filters are run as part of
3036 * dbus_connection_dispatch(). This is probably a feature, as filters
3037 * could create arbitrary reentrancy. But kind of sucks if you're
3038 * trying to filter METHOD_RETURN for some reason.
3040 * @param connection the connection
3041 * @param function function to handle messages
3042 * @param user_data user data to pass to the function
3043 * @param free_data_function function to use for freeing user data
3044 * @returns #TRUE on success, #FALSE if not enough memory.
3047 dbus_connection_add_filter (DBusConnection *connection,
3048 DBusHandleMessageFunction function,
3050 DBusFreeFunction free_data_function)
3052 DBusMessageFilter *filter;
3054 _dbus_return_val_if_fail (connection != NULL, FALSE);
3055 _dbus_return_val_if_fail (function != NULL, FALSE);
3057 filter = dbus_new0 (DBusMessageFilter, 1);
3061 filter->refcount.value = 1;
3063 CONNECTION_LOCK (connection);
3065 if (!_dbus_list_append (&connection->filter_list,
3068 _dbus_message_filter_unref (filter);
3069 CONNECTION_UNLOCK (connection);
3073 /* Fill in filter after all memory allocated,
3074 * so we don't run the free_user_data_function
3075 * if the add_filter() fails
3078 filter->function = function;
3079 filter->user_data = user_data;
3080 filter->free_user_data_function = free_data_function;
3082 CONNECTION_UNLOCK (connection);
3087 * Removes a previously-added message filter. It is a programming
3088 * error to call this function for a handler that has not been added
3089 * as a filter. If the given handler was added more than once, only
3090 * one instance of it will be removed (the most recently-added
3093 * @param connection the connection
3094 * @param function the handler to remove
3095 * @param user_data user data for the handler to remove
3099 dbus_connection_remove_filter (DBusConnection *connection,
3100 DBusHandleMessageFunction function,
3104 DBusMessageFilter *filter;
3106 _dbus_return_if_fail (connection != NULL);
3107 _dbus_return_if_fail (function != NULL);
3109 CONNECTION_LOCK (connection);
3113 link = _dbus_list_get_last_link (&connection->filter_list);
3114 while (link != NULL)
3116 filter = link->data;
3118 if (filter->function == function &&
3119 filter->user_data == user_data)
3121 _dbus_list_remove_link (&connection->filter_list, link);
3122 filter->function = NULL;
3127 link = _dbus_list_get_prev_link (&connection->filter_list, link);
3130 CONNECTION_UNLOCK (connection);
3132 #ifndef DBUS_DISABLE_CHECKS
3135 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
3136 function, user_data);
3141 /* Call application code */
3142 if (filter->free_user_data_function)
3143 (* filter->free_user_data_function) (filter->user_data);
3145 filter->free_user_data_function = NULL;
3146 filter->user_data = NULL;
3148 _dbus_message_filter_unref (filter);
3152 * Registers a handler for a given path in the object hierarchy.
3153 * The given vtable handles messages sent to exactly the given path.
3156 * @param connection the connection
3157 * @param path #NULL-terminated array of path elements
3158 * @param vtable the virtual table
3159 * @param user_data data to pass to functions in the vtable
3160 * @returns #FALSE if not enough memory
3163 dbus_connection_register_object_path (DBusConnection *connection,
3165 const DBusObjectPathVTable *vtable,
3170 _dbus_return_val_if_fail (connection != NULL, FALSE);
3171 _dbus_return_val_if_fail (path != NULL, FALSE);
3172 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3173 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3175 CONNECTION_LOCK (connection);
3177 retval = _dbus_object_tree_register (connection->objects,
3182 CONNECTION_UNLOCK (connection);
3188 * Registers a fallback handler for a given subsection of the object
3189 * hierarchy. The given vtable handles messages at or below the given
3190 * path. You can use this to establish a default message handling
3191 * policy for a whole "subdirectory."
3193 * @param connection the connection
3194 * @param path #NULL-terminated array of path elements
3195 * @param vtable the virtual table
3196 * @param user_data data to pass to functions in the vtable
3197 * @returns #FALSE if not enough memory
3200 dbus_connection_register_fallback (DBusConnection *connection,
3202 const DBusObjectPathVTable *vtable,
3207 _dbus_return_val_if_fail (connection != NULL, FALSE);
3208 _dbus_return_val_if_fail (path != NULL, FALSE);
3209 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3210 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3212 CONNECTION_LOCK (connection);
3214 retval = _dbus_object_tree_register (connection->objects,
3219 CONNECTION_UNLOCK (connection);
3225 * Unregisters the handler registered with exactly the given path.
3226 * It's a bug to call this function for a path that isn't registered.
3227 * Can unregister both fallback paths and object paths.
3229 * @param connection the connection
3230 * @param path the #NULL-terminated array of path elements
3233 dbus_connection_unregister_object_path (DBusConnection *connection,
3236 _dbus_return_if_fail (connection != NULL);
3237 _dbus_return_if_fail (path != NULL);
3238 _dbus_return_if_fail (path[0] != NULL);
3240 CONNECTION_LOCK (connection);
3242 return _dbus_object_tree_unregister_and_unlock (connection->objects,
3247 * Lists the registered fallback handlers and object path handlers at
3248 * the given parent_path. The returned array should be freed with
3249 * dbus_free_string_array().
3251 * @param connection the connection
3252 * @param parent_path the path to list the child handlers of
3253 * @param child_entries returns #NULL-terminated array of children
3254 * @returns #FALSE if no memory to allocate the child entries
3257 dbus_connection_list_registered (DBusConnection *connection,
3258 const char **parent_path,
3259 char ***child_entries)
3261 _dbus_return_val_if_fail (connection != NULL, FALSE);
3262 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
3263 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
3265 CONNECTION_LOCK (connection);
3267 return _dbus_object_tree_list_registered_and_unlock (connection->objects,
3272 static DBusDataSlotAllocator slot_allocator;
3273 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
3276 * Allocates an integer ID to be used for storing application-specific
3277 * data on any DBusConnection. The allocated ID may then be used
3278 * with dbus_connection_set_data() and dbus_connection_get_data().
3279 * The passed-in slot must be initialized to -1, and is filled in
3280 * with the slot ID. If the passed-in slot is not -1, it's assumed
3281 * to be already allocated, and its refcount is incremented.
3283 * The allocated slot is global, i.e. all DBusConnection objects will
3284 * have a slot with the given integer ID reserved.
3286 * @param slot_p address of a global variable storing the slot
3287 * @returns #FALSE on failure (no memory)
3290 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
3292 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3293 _DBUS_LOCK_NAME (connection_slots),
3298 * Deallocates a global ID for connection data slots.
3299 * dbus_connection_get_data() and dbus_connection_set_data() may no
3300 * longer be used with this slot. Existing data stored on existing
3301 * DBusConnection objects will be freed when the connection is
3302 * finalized, but may not be retrieved (and may only be replaced if
3303 * someone else reallocates the slot). When the refcount on the
3304 * passed-in slot reaches 0, it is set to -1.
3306 * @param slot_p address storing the slot to deallocate
3309 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
3311 _dbus_return_if_fail (*slot_p >= 0);
3313 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3317 * Stores a pointer on a DBusConnection, along
3318 * with an optional function to be used for freeing
3319 * the data when the data is set again, or when
3320 * the connection is finalized. The slot number
3321 * must have been allocated with dbus_connection_allocate_data_slot().
3323 * @param connection the connection
3324 * @param slot the slot number
3325 * @param data the data to store
3326 * @param free_data_func finalizer function for the data
3327 * @returns #TRUE if there was enough memory to store the data
3330 dbus_connection_set_data (DBusConnection *connection,
3333 DBusFreeFunction free_data_func)
3335 DBusFreeFunction old_free_func;
3339 _dbus_return_val_if_fail (connection != NULL, FALSE);
3340 _dbus_return_val_if_fail (slot >= 0, FALSE);
3342 CONNECTION_LOCK (connection);
3344 retval = _dbus_data_slot_list_set (&slot_allocator,
3345 &connection->slot_list,
3346 slot, data, free_data_func,
3347 &old_free_func, &old_data);
3349 CONNECTION_UNLOCK (connection);
3353 /* Do the actual free outside the connection lock */
3355 (* old_free_func) (old_data);
3362 * Retrieves data previously set with dbus_connection_set_data().
3363 * The slot must still be allocated (must not have been freed).
3365 * @param connection the connection
3366 * @param slot the slot to get data from
3367 * @returns the data, or #NULL if not found
3370 dbus_connection_get_data (DBusConnection *connection,
3375 _dbus_return_val_if_fail (connection != NULL, NULL);
3377 CONNECTION_LOCK (connection);
3379 res = _dbus_data_slot_list_get (&slot_allocator,
3380 &connection->slot_list,
3383 CONNECTION_UNLOCK (connection);
3389 * This function sets a global flag for whether dbus_connection_new()
3390 * will set SIGPIPE behavior to SIG_IGN.
3392 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
3395 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
3397 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
3401 * Specifies the maximum size message this connection is allowed to
3402 * receive. Larger messages will result in disconnecting the
3405 * @param connection a #DBusConnection
3406 * @param size maximum message size the connection can receive, in bytes
3409 dbus_connection_set_max_message_size (DBusConnection *connection,
3412 _dbus_return_if_fail (connection != NULL);
3414 CONNECTION_LOCK (connection);
3415 _dbus_transport_set_max_message_size (connection->transport,
3417 CONNECTION_UNLOCK (connection);
3421 * Gets the value set by dbus_connection_set_max_message_size().
3423 * @param connection the connection
3424 * @returns the max size of a single message
3427 dbus_connection_get_max_message_size (DBusConnection *connection)
3431 _dbus_return_val_if_fail (connection != NULL, 0);
3433 CONNECTION_LOCK (connection);
3434 res = _dbus_transport_get_max_message_size (connection->transport);
3435 CONNECTION_UNLOCK (connection);
3440 * Sets the maximum total number of bytes that can be used for all messages
3441 * received on this connection. Messages count toward the maximum until
3442 * they are finalized. When the maximum is reached, the connection will
3443 * not read more data until some messages are finalized.
3445 * The semantics of the maximum are: if outstanding messages are
3446 * already above the maximum, additional messages will not be read.
3447 * The semantics are not: if the next message would cause us to exceed
3448 * the maximum, we don't read it. The reason is that we don't know the
3449 * size of a message until after we read it.
3451 * Thus, the max live messages size can actually be exceeded
3452 * by up to the maximum size of a single message.
3454 * Also, if we read say 1024 bytes off the wire in a single read(),
3455 * and that contains a half-dozen small messages, we may exceed the
3456 * size max by that amount. But this should be inconsequential.
3458 * This does imply that we can't call read() with a buffer larger
3459 * than we're willing to exceed this limit by.
3461 * @param connection the connection
3462 * @param size the maximum size in bytes of all outstanding messages
3465 dbus_connection_set_max_received_size (DBusConnection *connection,
3468 _dbus_return_if_fail (connection != NULL);
3470 CONNECTION_LOCK (connection);
3471 _dbus_transport_set_max_received_size (connection->transport,
3473 CONNECTION_UNLOCK (connection);
3477 * Gets the value set by dbus_connection_set_max_received_size().
3479 * @param connection the connection
3480 * @returns the max size of all live messages
3483 dbus_connection_get_max_received_size (DBusConnection *connection)
3487 _dbus_return_val_if_fail (connection != NULL, 0);
3489 CONNECTION_LOCK (connection);
3490 res = _dbus_transport_get_max_received_size (connection->transport);
3491 CONNECTION_UNLOCK (connection);
3496 * Gets the approximate size in bytes of all messages in the outgoing
3497 * message queue. The size is approximate in that you shouldn't use
3498 * it to decide how many bytes to read off the network or anything
3499 * of that nature, as optimizations may choose to tell small white lies
3500 * to avoid performance overhead.
3502 * @param connection the connection
3503 * @returns the number of bytes that have been queued up but not sent
3506 dbus_connection_get_outgoing_size (DBusConnection *connection)
3510 _dbus_return_val_if_fail (connection != NULL, 0);
3512 CONNECTION_LOCK (connection);
3513 res = _dbus_counter_get_value (connection->outgoing_counter);
3514 CONNECTION_UNLOCK (connection);