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-shared.h"
26 #include "dbus-connection.h"
27 #include "dbus-list.h"
28 #include "dbus-timeout.h"
29 #include "dbus-transport.h"
30 #include "dbus-watch.h"
31 #include "dbus-connection-internal.h"
32 #include "dbus-list.h"
33 #include "dbus-hash.h"
34 #include "dbus-message-internal.h"
35 #include "dbus-threads.h"
36 #include "dbus-protocol.h"
37 #include "dbus-dataslot.h"
38 #include "dbus-string.h"
39 #include "dbus-pending-call.h"
40 #include "dbus-object-tree.h"
41 #include "dbus-marshal.h"
44 #define CONNECTION_LOCK(connection) do { \
45 _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); \
46 dbus_mutex_lock ((connection)->mutex); \
48 #define CONNECTION_UNLOCK(connection) do { \
49 _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); \
50 dbus_mutex_unlock ((connection)->mutex); \
53 #define CONNECTION_LOCK(connection) dbus_mutex_lock ((connection)->mutex)
54 #define CONNECTION_UNLOCK(connection) dbus_mutex_unlock ((connection)->mutex)
58 * @defgroup DBusConnection DBusConnection
60 * @brief Connection to another application
62 * A DBusConnection represents a connection to another
63 * application. Messages can be sent and received via this connection.
64 * The other application may be a message bus; for convenience, the
65 * function dbus_bus_get() is provided to automatically open a
66 * connection to the well-known message buses.
68 * In brief a DBusConnection is a message queue associated with some
69 * message transport mechanism such as a socket. The connection
70 * maintains a queue of incoming messages and a queue of outgoing
73 * Incoming messages are normally processed by calling
74 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
75 * handlers registered for the topmost message in the message queue,
76 * then discards the message, then returns.
78 * dbus_connection_get_dispatch_status() indicates whether
79 * messages are currently in the queue that need dispatching.
80 * dbus_connection_set_dispatch_status_function() allows
81 * you to set a function to be used to monitor the dispatch status.
83 * If you're using GLib or Qt add-on libraries for D-BUS, there are
84 * special convenience APIs in those libraries that hide
85 * all the details of dispatch and watch/timeout monitoring.
86 * For example, dbus_connection_setup_with_g_main().
88 * If you aren't using these add-on libraries, you have to manually
89 * call dbus_connection_set_dispatch_status_function(),
90 * dbus_connection_set_watch_functions(),
91 * dbus_connection_set_timeout_functions() providing appropriate
92 * functions to integrate the connection with your application's main
95 * When you use dbus_connection_send() or one of its variants to send
96 * a message, the message is added to the outgoing queue. It's
97 * actually written to the network later; either in
98 * dbus_watch_handle() invoked by your main loop, or in
99 * dbus_connection_flush() which blocks until it can write out the
100 * entire outgoing queue. The GLib/Qt add-on libraries again
101 * handle the details here for you by setting up watch functions.
103 * When a connection is disconnected, you are guaranteed to get a
104 * signal "Disconnected" from the interface
105 * #DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, path
106 * #DBUS_PATH_ORG_FREEDESKTOP_LOCAL.
108 * You may not drop the last reference to a #DBusConnection
109 * until that connection has been disconnected.
111 * You may dispatch the unprocessed incoming message queue even if the
112 * connection is disconnected. However, "Disconnected" will always be
113 * the last message in the queue (obviously no messages are received
114 * after disconnection).
116 * #DBusConnection has thread locks and drops them when invoking user
117 * callbacks, so in general is transparently threadsafe. However,
118 * #DBusMessage does NOT have thread locks; you must not send the same
119 * message to multiple #DBusConnection that will be used from
124 * @defgroup DBusConnectionInternals DBusConnection implementation details
125 * @ingroup DBusInternals
126 * @brief Implementation details of DBusConnection
132 * Internal struct representing a message filter function
134 typedef struct DBusMessageFilter DBusMessageFilter;
137 * Internal struct representing a message filter function
139 struct DBusMessageFilter
141 DBusAtomic refcount; /**< Reference count */
142 DBusHandleMessageFunction function; /**< Function to call to filter */
143 void *user_data; /**< User data for the function */
144 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
149 * Internals of DBusPreallocatedSend
151 struct DBusPreallocatedSend
153 DBusConnection *connection; /**< Connection we'd send the message to */
154 DBusList *queue_link; /**< Preallocated link in the queue */
155 DBusList *counter_link; /**< Preallocated link in the resource counter */
158 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
161 * Implementation details of DBusConnection. All fields are private.
163 struct DBusConnection
165 DBusAtomic refcount; /**< Reference count. */
167 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
169 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */
170 DBusCondVar *dispatch_cond; /**< Protects dispatch() */
172 dbus_bool_t io_path_acquired; /**< Protects transport io path */
173 DBusCondVar *io_path_cond; /**< Protects transport io path */
175 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
176 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
178 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
179 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
181 int n_outgoing; /**< Length of outgoing queue. */
182 int n_incoming; /**< Length of incoming queue. */
184 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
186 DBusTransport *transport; /**< Object that sends/receives messages over network. */
187 DBusWatchList *watches; /**< Stores active watches. */
188 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
190 DBusList *filter_list; /**< List of filters. */
192 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
194 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
196 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
197 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
199 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
200 void *wakeup_main_data; /**< Application data for wakeup_main_function */
201 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
203 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
204 void *dispatch_status_data; /**< Application data for dispatch_status_function */
205 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
207 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
209 DBusList *link_cache; /**< A cache of linked list links to prevent contention
210 * for the global linked list mempool lock
212 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
214 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
217 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
218 DBusTimeout *timeout);
219 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
220 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
221 DBusDispatchStatus new_status);
222 static void _dbus_connection_last_unref (DBusConnection *connection);
224 static DBusMessageFilter *
225 _dbus_message_filter_ref (DBusMessageFilter *filter)
227 _dbus_assert (filter->refcount.value > 0);
228 _dbus_atomic_inc (&filter->refcount);
234 _dbus_message_filter_unref (DBusMessageFilter *filter)
236 _dbus_assert (filter->refcount.value > 0);
238 if (_dbus_atomic_dec (&filter->refcount) == 1)
240 if (filter->free_user_data_function)
241 (* filter->free_user_data_function) (filter->user_data);
248 * Acquires the connection lock.
250 * @param connection the connection.
253 _dbus_connection_lock (DBusConnection *connection)
255 CONNECTION_LOCK (connection);
259 * Releases the connection lock.
261 * @param connection the connection.
264 _dbus_connection_unlock (DBusConnection *connection)
266 CONNECTION_UNLOCK (connection);
270 * Wakes up the main loop if it is sleeping
271 * Needed if we're e.g. queueing outgoing messages
272 * on a thread while the mainloop sleeps.
274 * @param connection the connection.
277 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
279 if (connection->wakeup_main_function)
280 (*connection->wakeup_main_function) (connection->wakeup_main_data);
283 #ifdef DBUS_BUILD_TESTS
284 /* For now this function isn't used */
286 * Adds a message to the incoming message queue, returning #FALSE
287 * if there's insufficient memory to queue the message.
288 * Does not take over refcount of the message.
290 * @param connection the connection.
291 * @param message the message to queue.
292 * @returns #TRUE on success.
295 _dbus_connection_queue_received_message (DBusConnection *connection,
296 DBusMessage *message)
300 link = _dbus_list_alloc_link (message);
304 dbus_message_ref (message);
305 _dbus_connection_queue_received_message_link (connection, link);
312 * Adds a message-containing list link to the incoming message queue,
313 * taking ownership of the link and the message's current refcount.
314 * Cannot fail due to lack of memory.
316 * @param connection the connection.
317 * @param link the message link to queue.
320 _dbus_connection_queue_received_message_link (DBusConnection *connection,
323 DBusPendingCall *pending;
324 dbus_int32_t reply_serial;
325 DBusMessage *message;
327 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
329 _dbus_list_append_link (&connection->incoming_messages,
331 message = link->data;
333 /* If this is a reply we're waiting on, remove timeout for it */
334 reply_serial = dbus_message_get_reply_serial (message);
335 if (reply_serial != -1)
337 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
341 if (pending->timeout_added)
342 _dbus_connection_remove_timeout_locked (connection,
345 pending->timeout_added = FALSE;
349 connection->n_incoming += 1;
351 _dbus_connection_wakeup_mainloop (connection);
353 _dbus_verbose ("Message %p (%d %s '%s') added to incoming queue %p, %d incoming\n",
355 dbus_message_get_type (message),
356 dbus_message_get_interface (message) ?
357 dbus_message_get_interface (message) :
359 dbus_message_get_signature (message),
361 connection->n_incoming);
365 * Adds a link + message to the incoming message queue.
366 * Can't fail. Takes ownership of both link and message.
368 * @param connection the connection.
369 * @param link the list node and message to queue.
371 * @todo This needs to wake up the mainloop if it is in
372 * a poll/select and this is a multithreaded app.
375 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
378 _dbus_list_append_link (&connection->incoming_messages, link);
380 connection->n_incoming += 1;
382 _dbus_connection_wakeup_mainloop (connection);
384 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
385 link->data, connection, connection->n_incoming);
390 * Checks whether there are messages in the outgoing message queue.
392 * @param connection the connection.
393 * @returns #TRUE if the outgoing queue is non-empty.
396 _dbus_connection_have_messages_to_send (DBusConnection *connection)
398 return connection->outgoing_messages != NULL;
402 * Gets the next outgoing message. The message remains in the
403 * queue, and the caller does not own a reference to it.
405 * @param connection the connection.
406 * @returns the message to be sent.
409 _dbus_connection_get_message_to_send (DBusConnection *connection)
411 return _dbus_list_get_last (&connection->outgoing_messages);
415 * Notifies the connection that a message has been sent, so the
416 * message can be removed from the outgoing queue.
417 * Called with the connection lock held.
419 * @param connection the connection.
420 * @param message the message that was sent.
423 _dbus_connection_message_sent (DBusConnection *connection,
424 DBusMessage *message)
428 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
430 link = _dbus_list_get_last_link (&connection->outgoing_messages);
431 _dbus_assert (link != NULL);
432 _dbus_assert (link->data == message);
434 /* Save this link in the link cache */
435 _dbus_list_unlink (&connection->outgoing_messages,
437 _dbus_list_prepend_link (&connection->link_cache, link);
439 connection->n_outgoing -= 1;
441 _dbus_verbose ("Message %p (%d %s '%s') removed from outgoing queue %p, %d left to send\n",
443 dbus_message_get_type (message),
444 dbus_message_get_interface (message) ?
445 dbus_message_get_interface (message) :
447 dbus_message_get_signature (message),
448 connection, connection->n_outgoing);
450 /* Save this link in the link cache also */
451 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
453 _dbus_list_prepend_link (&connection->link_cache, link);
455 dbus_message_unref (message);
457 if (connection->n_outgoing == 0)
458 _dbus_transport_messages_pending (connection->transport,
459 connection->n_outgoing);
463 * Adds a watch using the connection's DBusAddWatchFunction if
464 * available. Otherwise records the watch to be added when said
465 * function is available. Also re-adds the watch if the
466 * DBusAddWatchFunction changes. May fail due to lack of memory.
468 * @param connection the connection.
469 * @param watch the watch to add.
470 * @returns #TRUE on success.
473 _dbus_connection_add_watch (DBusConnection *connection,
476 if (connection->watches) /* null during finalize */
477 return _dbus_watch_list_add_watch (connection->watches,
484 * Removes a watch using the connection's DBusRemoveWatchFunction
485 * if available. It's an error to call this function on a watch
486 * that was not previously added.
488 * @param connection the connection.
489 * @param watch the watch to remove.
492 _dbus_connection_remove_watch (DBusConnection *connection,
495 if (connection->watches) /* null during finalize */
496 _dbus_watch_list_remove_watch (connection->watches,
501 * Toggles a watch and notifies app via connection's
502 * DBusWatchToggledFunction if available. It's an error to call this
503 * function on a watch that was not previously added.
505 * @param connection the connection.
506 * @param watch the watch to toggle.
507 * @param enabled whether to enable or disable
510 _dbus_connection_toggle_watch (DBusConnection *connection,
514 if (connection->watches) /* null during finalize */
515 _dbus_watch_list_toggle_watch (connection->watches,
520 * Adds a timeout using the connection's DBusAddTimeoutFunction if
521 * available. Otherwise records the timeout to be added when said
522 * function is available. Also re-adds the timeout if the
523 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
524 * The timeout will fire repeatedly until removed.
526 * @param connection the connection.
527 * @param timeout the timeout to add.
528 * @returns #TRUE on success.
531 _dbus_connection_add_timeout (DBusConnection *connection,
532 DBusTimeout *timeout)
534 if (connection->timeouts) /* null during finalize */
535 return _dbus_timeout_list_add_timeout (connection->timeouts,
542 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
543 * if available. It's an error to call this function on a timeout
544 * that was not previously added.
546 * @param connection the connection.
547 * @param timeout the timeout to remove.
550 _dbus_connection_remove_timeout (DBusConnection *connection,
551 DBusTimeout *timeout)
553 if (connection->timeouts) /* null during finalize */
554 _dbus_timeout_list_remove_timeout (connection->timeouts,
559 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
560 DBusTimeout *timeout)
562 CONNECTION_LOCK (connection);
563 _dbus_connection_remove_timeout (connection, timeout);
564 CONNECTION_UNLOCK (connection);
568 * Toggles a timeout and notifies app via connection's
569 * DBusTimeoutToggledFunction if available. It's an error to call this
570 * function on a timeout that was not previously added.
572 * @param connection the connection.
573 * @param timeout the timeout to toggle.
574 * @param enabled whether to enable or disable
577 _dbus_connection_toggle_timeout (DBusConnection *connection,
578 DBusTimeout *timeout,
581 if (connection->timeouts) /* null during finalize */
582 _dbus_timeout_list_toggle_timeout (connection->timeouts,
587 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
588 DBusPendingCall *pending)
590 _dbus_assert (pending->reply_serial != 0);
592 if (!_dbus_connection_add_timeout (connection, pending->timeout))
595 if (!_dbus_hash_table_insert_int (connection->pending_replies,
596 pending->reply_serial,
599 _dbus_connection_remove_timeout (connection, pending->timeout);
603 pending->timeout_added = TRUE;
604 pending->connection = connection;
606 dbus_pending_call_ref (pending);
612 free_pending_call_on_hash_removal (void *data)
614 DBusPendingCall *pending;
621 if (pending->connection)
623 if (pending->timeout_added)
625 _dbus_connection_remove_timeout (pending->connection,
627 pending->timeout_added = FALSE;
630 pending->connection = NULL;
632 dbus_pending_call_unref (pending);
637 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
638 DBusPendingCall *pending)
640 /* The idea here is to avoid finalizing the pending call
641 * with the lock held, since there's a destroy notifier
642 * in pending call that goes out to application code.
644 dbus_pending_call_ref (pending);
645 _dbus_hash_table_remove_int (connection->pending_replies,
646 pending->reply_serial);
647 CONNECTION_UNLOCK (connection);
648 dbus_pending_call_unref (pending);
652 * Removes a pending call from the connection, such that
653 * the pending reply will be ignored. May drop the last
654 * reference to the pending call.
656 * @param connection the connection
657 * @param pending the pending call
660 _dbus_connection_remove_pending_call (DBusConnection *connection,
661 DBusPendingCall *pending)
663 CONNECTION_LOCK (connection);
664 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
668 * Completes a pending call with the given message,
669 * or if the message is #NULL, by timing out the pending call.
671 * @param pending the pending call
672 * @param message the message to complete the call with, or #NULL
673 * to time out the call
676 _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
677 DBusMessage *message)
681 message = pending->timeout_link->data;
682 _dbus_list_clear (&pending->timeout_link);
685 dbus_message_ref (message);
687 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
689 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
691 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
692 "error" : "other type",
693 pending->reply_serial);
695 _dbus_assert (pending->reply == NULL);
696 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
697 pending->reply = message;
699 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
700 _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending);
702 /* Must be called unlocked since it invokes app callback */
703 _dbus_pending_call_notify (pending);
704 dbus_pending_call_unref (pending);
708 * Acquire the transporter I/O path. This must be done before
709 * doing any I/O in the transporter. May sleep and drop the
710 * connection mutex while waiting for the I/O path.
712 * @param connection the connection.
713 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
714 * @returns TRUE if the I/O path was acquired.
717 _dbus_connection_acquire_io_path (DBusConnection *connection,
718 int timeout_milliseconds)
720 dbus_bool_t res = TRUE;
722 if (connection->io_path_acquired)
724 if (timeout_milliseconds != -1)
725 res = dbus_condvar_wait_timeout (connection->io_path_cond,
727 timeout_milliseconds);
729 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
734 _dbus_assert (!connection->io_path_acquired);
736 connection->io_path_acquired = TRUE;
743 * Release the I/O path when you're done with it. Only call
744 * after you've acquired the I/O. Wakes up at most one thread
745 * currently waiting to acquire the I/O path.
747 * @param connection the connection.
750 _dbus_connection_release_io_path (DBusConnection *connection)
752 _dbus_assert (connection->io_path_acquired);
754 connection->io_path_acquired = FALSE;
755 dbus_condvar_wake_one (connection->io_path_cond);
760 * Queues incoming messages and sends outgoing messages for this
761 * connection, optionally blocking in the process. Each call to
762 * _dbus_connection_do_iteration() will call select() or poll() one
763 * time and then read or write data if possible.
765 * The purpose of this function is to be able to flush outgoing
766 * messages or queue up incoming messages without returning
767 * control to the application and causing reentrancy weirdness.
769 * The flags parameter allows you to specify whether to
770 * read incoming messages, write outgoing messages, or both,
771 * and whether to block if no immediate action is possible.
773 * The timeout_milliseconds parameter does nothing unless the
774 * iteration is blocking.
776 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
777 * wasn't specified, then it's impossible to block, even if
778 * you specify DBUS_ITERATION_BLOCK; in that case the function
779 * returns immediately.
781 * @param connection the connection.
782 * @param flags iteration flags.
783 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
786 _dbus_connection_do_iteration (DBusConnection *connection,
788 int timeout_milliseconds)
790 if (connection->n_outgoing == 0)
791 flags &= ~DBUS_ITERATION_DO_WRITING;
793 if (_dbus_connection_acquire_io_path (connection,
794 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
796 _dbus_transport_do_iteration (connection->transport,
797 flags, timeout_milliseconds);
798 _dbus_connection_release_io_path (connection);
803 * Creates a new connection for the given transport. A transport
804 * represents a message stream that uses some concrete mechanism, such
805 * as UNIX domain sockets. May return #NULL if insufficient
806 * memory exists to create the connection.
808 * @param transport the transport.
809 * @returns the new connection, or #NULL on failure.
812 _dbus_connection_new_for_transport (DBusTransport *transport)
814 DBusConnection *connection;
815 DBusWatchList *watch_list;
816 DBusTimeoutList *timeout_list;
817 DBusHashTable *pending_replies;
819 DBusCondVar *message_returned_cond;
820 DBusCondVar *dispatch_cond;
821 DBusCondVar *io_path_cond;
822 DBusList *disconnect_link;
823 DBusMessage *disconnect_message;
824 DBusCounter *outgoing_counter;
825 DBusObjectTree *objects;
829 pending_replies = NULL;
832 message_returned_cond = NULL;
833 dispatch_cond = NULL;
835 disconnect_link = NULL;
836 disconnect_message = NULL;
837 outgoing_counter = NULL;
840 watch_list = _dbus_watch_list_new ();
841 if (watch_list == NULL)
844 timeout_list = _dbus_timeout_list_new ();
845 if (timeout_list == NULL)
849 _dbus_hash_table_new (DBUS_HASH_INT,
851 (DBusFreeFunction)free_pending_call_on_hash_removal);
852 if (pending_replies == NULL)
855 connection = dbus_new0 (DBusConnection, 1);
856 if (connection == NULL)
859 mutex = dbus_mutex_new ();
863 message_returned_cond = dbus_condvar_new ();
864 if (message_returned_cond == NULL)
867 dispatch_cond = dbus_condvar_new ();
868 if (dispatch_cond == NULL)
871 io_path_cond = dbus_condvar_new ();
872 if (io_path_cond == NULL)
875 disconnect_message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_LOCAL,
876 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
879 if (disconnect_message == NULL)
882 disconnect_link = _dbus_list_alloc_link (disconnect_message);
883 if (disconnect_link == NULL)
886 outgoing_counter = _dbus_counter_new ();
887 if (outgoing_counter == NULL)
890 objects = _dbus_object_tree_new (connection);
894 if (_dbus_modify_sigpipe)
895 _dbus_disable_sigpipe ();
897 connection->refcount.value = 1;
898 connection->mutex = mutex;
899 connection->dispatch_cond = dispatch_cond;
900 connection->io_path_cond = io_path_cond;
901 connection->message_returned_cond = message_returned_cond;
902 connection->transport = transport;
903 connection->watches = watch_list;
904 connection->timeouts = timeout_list;
905 connection->pending_replies = pending_replies;
906 connection->outgoing_counter = outgoing_counter;
907 connection->filter_list = NULL;
908 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
909 connection->objects = objects;
910 connection->exit_on_disconnect = FALSE;
912 _dbus_data_slot_list_init (&connection->slot_list);
914 connection->client_serial = 1;
916 connection->disconnect_message_link = disconnect_link;
918 if (!_dbus_transport_set_connection (transport, connection))
921 _dbus_transport_ref (transport);
926 if (disconnect_message != NULL)
927 dbus_message_unref (disconnect_message);
929 if (disconnect_link != NULL)
930 _dbus_list_free_link (disconnect_link);
932 if (io_path_cond != NULL)
933 dbus_condvar_free (io_path_cond);
935 if (dispatch_cond != NULL)
936 dbus_condvar_free (dispatch_cond);
938 if (message_returned_cond != NULL)
939 dbus_condvar_free (message_returned_cond);
942 dbus_mutex_free (mutex);
944 if (connection != NULL)
945 dbus_free (connection);
948 _dbus_hash_table_unref (pending_replies);
951 _dbus_watch_list_free (watch_list);
954 _dbus_timeout_list_free (timeout_list);
956 if (outgoing_counter)
957 _dbus_counter_unref (outgoing_counter);
960 _dbus_object_tree_unref (objects);
966 * Increments the reference count of a DBusConnection.
967 * Requires that the caller already holds the connection lock.
969 * @param connection the connection.
970 * @returns the connection.
973 _dbus_connection_ref_unlocked (DBusConnection *connection)
975 #ifdef DBUS_HAVE_ATOMIC_INT
976 _dbus_atomic_inc (&connection->refcount);
978 _dbus_assert (connection->refcount.value > 0);
979 connection->refcount.value += 1;
986 * Decrements the reference count of a DBusConnection.
987 * Requires that the caller already holds the connection lock.
989 * @param connection the connection.
992 _dbus_connection_unref_unlocked (DBusConnection *connection)
994 dbus_bool_t last_unref;
996 _dbus_return_if_fail (connection != NULL);
998 /* The connection lock is better than the global
999 * lock in the atomic increment fallback
1002 #ifdef DBUS_HAVE_ATOMIC_INT
1003 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1005 _dbus_assert (connection->refcount.value > 0);
1007 connection->refcount.value -= 1;
1008 last_unref = (connection->refcount.value == 0);
1010 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1015 _dbus_connection_last_unref (connection);
1018 static dbus_uint32_t
1019 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1023 serial = connection->client_serial++;
1025 if (connection->client_serial < 0)
1026 connection->client_serial = 1;
1032 * A callback for use with dbus_watch_new() to create a DBusWatch.
1034 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1035 * and the virtual handle_watch in DBusTransport if we got rid of it.
1036 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1039 * @param watch the watch.
1040 * @param condition the current condition of the file descriptors being watched.
1041 * @param data must be a pointer to a #DBusConnection
1042 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1045 _dbus_connection_handle_watch (DBusWatch *watch,
1046 unsigned int condition,
1049 DBusConnection *connection;
1051 DBusDispatchStatus status;
1055 CONNECTION_LOCK (connection);
1056 _dbus_connection_acquire_io_path (connection, -1);
1057 retval = _dbus_transport_handle_watch (connection->transport,
1059 _dbus_connection_release_io_path (connection);
1061 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1063 /* this calls out to user code */
1064 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1072 * @addtogroup DBusConnection
1078 * Opens a new connection to a remote address.
1080 * @todo specify what the address parameter is. Right now
1081 * it's just the name of a UNIX domain socket. It should be
1082 * something more complex that encodes which transport to use.
1084 * If the open fails, the function returns #NULL, and provides
1085 * a reason for the failure in the result parameter. Pass
1086 * #NULL for the result parameter if you aren't interested
1087 * in the reason for failure.
1089 * @param address the address.
1090 * @param error address where an error can be returned.
1091 * @returns new connection, or #NULL on failure.
1094 dbus_connection_open (const char *address,
1097 DBusConnection *connection;
1098 DBusTransport *transport;
1100 _dbus_return_val_if_fail (address != NULL, NULL);
1101 _dbus_return_val_if_error_is_set (error, NULL);
1103 transport = _dbus_transport_open (address, error);
1104 if (transport == NULL)
1106 _DBUS_ASSERT_ERROR_IS_SET (error);
1110 connection = _dbus_connection_new_for_transport (transport);
1112 _dbus_transport_unref (transport);
1114 if (connection == NULL)
1116 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1124 * Increments the reference count of a DBusConnection.
1126 * @param connection the connection.
1127 * @returns the connection.
1130 dbus_connection_ref (DBusConnection *connection)
1132 _dbus_return_val_if_fail (connection != NULL, NULL);
1134 /* The connection lock is better than the global
1135 * lock in the atomic increment fallback
1138 #ifdef DBUS_HAVE_ATOMIC_INT
1139 _dbus_atomic_inc (&connection->refcount);
1141 CONNECTION_LOCK (connection);
1142 _dbus_assert (connection->refcount.value > 0);
1144 connection->refcount.value += 1;
1145 CONNECTION_UNLOCK (connection);
1152 free_outgoing_message (void *element,
1155 DBusMessage *message = element;
1156 DBusConnection *connection = data;
1158 _dbus_message_remove_size_counter (message,
1159 connection->outgoing_counter,
1161 dbus_message_unref (message);
1164 /* This is run without the mutex held, but after the last reference
1165 * to the connection has been dropped we should have no thread-related
1169 _dbus_connection_last_unref (DBusConnection *connection)
1173 _dbus_verbose ("Finalizing connection %p\n", connection);
1175 _dbus_assert (connection->refcount.value == 0);
1177 /* You have to disconnect the connection before unref:ing it. Otherwise
1178 * you won't get the disconnected message.
1180 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1182 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1183 _dbus_object_tree_free_all_unlocked (connection->objects);
1185 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1186 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1187 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1189 _dbus_watch_list_free (connection->watches);
1190 connection->watches = NULL;
1192 _dbus_timeout_list_free (connection->timeouts);
1193 connection->timeouts = NULL;
1195 _dbus_data_slot_list_free (&connection->slot_list);
1197 link = _dbus_list_get_first_link (&connection->filter_list);
1198 while (link != NULL)
1200 DBusMessageFilter *filter = link->data;
1201 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1203 filter->function = NULL;
1204 _dbus_message_filter_unref (filter); /* calls app callback */
1209 _dbus_list_clear (&connection->filter_list);
1211 /* ---- Done with stuff that invokes application callbacks */
1213 _dbus_object_tree_unref (connection->objects);
1215 _dbus_hash_table_unref (connection->pending_replies);
1216 connection->pending_replies = NULL;
1218 _dbus_list_clear (&connection->filter_list);
1220 _dbus_list_foreach (&connection->outgoing_messages,
1221 free_outgoing_message,
1223 _dbus_list_clear (&connection->outgoing_messages);
1225 _dbus_list_foreach (&connection->incoming_messages,
1226 (DBusForeachFunction) dbus_message_unref,
1228 _dbus_list_clear (&connection->incoming_messages);
1230 _dbus_counter_unref (connection->outgoing_counter);
1232 _dbus_transport_unref (connection->transport);
1234 if (connection->disconnect_message_link)
1236 DBusMessage *message = connection->disconnect_message_link->data;
1237 dbus_message_unref (message);
1238 _dbus_list_free_link (connection->disconnect_message_link);
1241 _dbus_list_clear (&connection->link_cache);
1243 dbus_condvar_free (connection->dispatch_cond);
1244 dbus_condvar_free (connection->io_path_cond);
1245 dbus_condvar_free (connection->message_returned_cond);
1247 dbus_mutex_free (connection->mutex);
1249 dbus_free (connection);
1253 * Decrements the reference count of a DBusConnection, and finalizes
1254 * it if the count reaches zero. It is a bug to drop the last reference
1255 * to a connection that has not been disconnected.
1257 * @todo in practice it can be quite tricky to never unref a connection
1258 * that's still connected; maybe there's some way we could avoid
1261 * @param connection the connection.
1264 dbus_connection_unref (DBusConnection *connection)
1266 dbus_bool_t last_unref;
1268 _dbus_return_if_fail (connection != NULL);
1270 /* The connection lock is better than the global
1271 * lock in the atomic increment fallback
1274 #ifdef DBUS_HAVE_ATOMIC_INT
1275 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1277 CONNECTION_LOCK (connection);
1279 _dbus_assert (connection->refcount.value > 0);
1281 connection->refcount.value -= 1;
1282 last_unref = (connection->refcount.value == 0);
1285 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1288 CONNECTION_UNLOCK (connection);
1292 _dbus_connection_last_unref (connection);
1296 * Closes the connection, so no further data can be sent or received.
1297 * Any further attempts to send data will result in errors. This
1298 * function does not affect the connection's reference count. It's
1299 * safe to disconnect a connection more than once; all calls after the
1300 * first do nothing. It's impossible to "reconnect" a connection, a
1301 * new connection must be created. This function may result in a call
1302 * to the DBusDispatchStatusFunction set with
1303 * dbus_connection_set_dispatch_status_function(), as the disconnect
1304 * message it generates needs to be dispatched.
1306 * @param connection the connection.
1309 dbus_connection_disconnect (DBusConnection *connection)
1311 DBusDispatchStatus status;
1313 _dbus_return_if_fail (connection != NULL);
1315 CONNECTION_LOCK (connection);
1316 _dbus_transport_disconnect (connection->transport);
1318 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1320 /* this calls out to user code */
1321 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1325 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1327 return _dbus_transport_get_is_connected (connection->transport);
1331 * Gets whether the connection is currently connected. All
1332 * connections are connected when they are opened. A connection may
1333 * become disconnected when the remote application closes its end, or
1334 * exits; a connection may also be disconnected with
1335 * dbus_connection_disconnect().
1337 * @param connection the connection.
1338 * @returns #TRUE if the connection is still alive.
1341 dbus_connection_get_is_connected (DBusConnection *connection)
1345 _dbus_return_val_if_fail (connection != NULL, FALSE);
1347 CONNECTION_LOCK (connection);
1348 res = _dbus_connection_get_is_connected_unlocked (connection);
1349 CONNECTION_UNLOCK (connection);
1355 * Gets whether the connection was authenticated. (Note that
1356 * if the connection was authenticated then disconnected,
1357 * this function still returns #TRUE)
1359 * @param connection the connection
1360 * @returns #TRUE if the connection was ever authenticated
1363 dbus_connection_get_is_authenticated (DBusConnection *connection)
1367 _dbus_return_val_if_fail (connection != NULL, FALSE);
1369 CONNECTION_LOCK (connection);
1370 res = _dbus_transport_get_is_authenticated (connection->transport);
1371 CONNECTION_UNLOCK (connection);
1377 * Set whether _exit() should be called when the connection receives a
1378 * disconnect signal. The call to _exit() comes after any handlers for
1379 * the disconnect signal run; handlers can cancel the exit by calling
1382 * By default, exit_on_disconnect is #FALSE; but for message bus
1383 * connections returned from dbus_bus_get() it will be toggled on
1386 * @param connection the connection
1387 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
1390 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
1391 dbus_bool_t exit_on_disconnect)
1393 _dbus_return_if_fail (connection != NULL);
1395 CONNECTION_LOCK (connection);
1396 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
1397 CONNECTION_UNLOCK (connection);
1400 static DBusPreallocatedSend*
1401 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1403 DBusPreallocatedSend *preallocated;
1405 _dbus_return_val_if_fail (connection != NULL, NULL);
1407 preallocated = dbus_new (DBusPreallocatedSend, 1);
1408 if (preallocated == NULL)
1411 if (connection->link_cache != NULL)
1413 preallocated->queue_link =
1414 _dbus_list_pop_first_link (&connection->link_cache);
1415 preallocated->queue_link->data = NULL;
1419 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1420 if (preallocated->queue_link == NULL)
1424 if (connection->link_cache != NULL)
1426 preallocated->counter_link =
1427 _dbus_list_pop_first_link (&connection->link_cache);
1428 preallocated->counter_link->data = connection->outgoing_counter;
1432 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1433 if (preallocated->counter_link == NULL)
1437 _dbus_counter_ref (preallocated->counter_link->data);
1439 preallocated->connection = connection;
1441 return preallocated;
1444 _dbus_list_free_link (preallocated->queue_link);
1446 dbus_free (preallocated);
1452 * Preallocates resources needed to send a message, allowing the message
1453 * to be sent without the possibility of memory allocation failure.
1454 * Allows apps to create a future guarantee that they can send
1455 * a message regardless of memory shortages.
1457 * @param connection the connection we're preallocating for.
1458 * @returns the preallocated resources, or #NULL
1460 DBusPreallocatedSend*
1461 dbus_connection_preallocate_send (DBusConnection *connection)
1463 DBusPreallocatedSend *preallocated;
1465 _dbus_return_val_if_fail (connection != NULL, NULL);
1467 CONNECTION_LOCK (connection);
1470 _dbus_connection_preallocate_send_unlocked (connection);
1472 CONNECTION_UNLOCK (connection);
1474 return preallocated;
1478 * Frees preallocated message-sending resources from
1479 * dbus_connection_preallocate_send(). Should only
1480 * be called if the preallocated resources are not used
1481 * to send a message.
1483 * @param connection the connection
1484 * @param preallocated the resources
1487 dbus_connection_free_preallocated_send (DBusConnection *connection,
1488 DBusPreallocatedSend *preallocated)
1490 _dbus_return_if_fail (connection != NULL);
1491 _dbus_return_if_fail (preallocated != NULL);
1492 _dbus_return_if_fail (connection == preallocated->connection);
1494 _dbus_list_free_link (preallocated->queue_link);
1495 _dbus_counter_unref (preallocated->counter_link->data);
1496 _dbus_list_free_link (preallocated->counter_link);
1497 dbus_free (preallocated);
1501 _dbus_connection_send_preallocated_unlocked (DBusConnection *connection,
1502 DBusPreallocatedSend *preallocated,
1503 DBusMessage *message,
1504 dbus_uint32_t *client_serial)
1506 dbus_uint32_t serial;
1508 preallocated->queue_link->data = message;
1509 _dbus_list_prepend_link (&connection->outgoing_messages,
1510 preallocated->queue_link);
1512 _dbus_message_add_size_counter_link (message,
1513 preallocated->counter_link);
1515 dbus_free (preallocated);
1516 preallocated = NULL;
1518 dbus_message_ref (message);
1520 connection->n_outgoing += 1;
1522 _dbus_verbose ("Message %p (%d %s '%s') added to outgoing queue %p, %d pending to send\n",
1524 dbus_message_get_type (message),
1525 dbus_message_get_interface (message) ?
1526 dbus_message_get_interface (message) :
1528 dbus_message_get_signature (message),
1530 connection->n_outgoing);
1532 if (dbus_message_get_serial (message) == 0)
1534 serial = _dbus_connection_get_next_client_serial (connection);
1535 _dbus_message_set_serial (message, serial);
1537 *client_serial = serial;
1542 *client_serial = dbus_message_get_serial (message);
1545 _dbus_message_lock (message);
1547 if (connection->n_outgoing == 1)
1548 _dbus_transport_messages_pending (connection->transport,
1549 connection->n_outgoing);
1551 _dbus_connection_wakeup_mainloop (connection);
1555 * Sends a message using preallocated resources. This function cannot fail.
1556 * It works identically to dbus_connection_send() in other respects.
1557 * Preallocated resources comes from dbus_connection_preallocate_send().
1558 * This function "consumes" the preallocated resources, they need not
1559 * be freed separately.
1561 * @param connection the connection
1562 * @param preallocated the preallocated resources
1563 * @param message the message to send
1564 * @param client_serial return location for client serial assigned to the message
1567 dbus_connection_send_preallocated (DBusConnection *connection,
1568 DBusPreallocatedSend *preallocated,
1569 DBusMessage *message,
1570 dbus_uint32_t *client_serial)
1572 _dbus_return_if_fail (connection != NULL);
1573 _dbus_return_if_fail (preallocated != NULL);
1574 _dbus_return_if_fail (message != NULL);
1575 _dbus_return_if_fail (preallocated->connection == connection);
1576 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
1577 (dbus_message_get_interface (message) != NULL &&
1578 dbus_message_get_member (message) != NULL));
1579 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
1580 (dbus_message_get_interface (message) != NULL &&
1581 dbus_message_get_member (message) != NULL));
1583 CONNECTION_LOCK (connection);
1584 _dbus_connection_send_preallocated_unlocked (connection,
1586 message, client_serial);
1587 CONNECTION_UNLOCK (connection);
1591 _dbus_connection_send_unlocked (DBusConnection *connection,
1592 DBusMessage *message,
1593 dbus_uint32_t *client_serial)
1595 DBusPreallocatedSend *preallocated;
1597 _dbus_assert (connection != NULL);
1598 _dbus_assert (message != NULL);
1600 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
1601 if (preallocated == NULL)
1605 _dbus_connection_send_preallocated_unlocked (connection,
1613 * Adds a message to the outgoing message queue. Does not block to
1614 * write the message to the network; that happens asynchronously. To
1615 * force the message to be written, call dbus_connection_flush().
1616 * Because this only queues the message, the only reason it can
1617 * fail is lack of memory. Even if the connection is disconnected,
1618 * no error will be returned.
1620 * If the function fails due to lack of memory, it returns #FALSE.
1621 * The function will never fail for other reasons; even if the
1622 * connection is disconnected, you can queue an outgoing message,
1623 * though obviously it won't be sent.
1625 * @param connection the connection.
1626 * @param message the message to write.
1627 * @param client_serial return location for client serial.
1628 * @returns #TRUE on success.
1631 dbus_connection_send (DBusConnection *connection,
1632 DBusMessage *message,
1633 dbus_uint32_t *client_serial)
1635 _dbus_return_val_if_fail (connection != NULL, FALSE);
1636 _dbus_return_val_if_fail (message != NULL, FALSE);
1638 CONNECTION_LOCK (connection);
1640 if (!_dbus_connection_send_unlocked (connection, message, client_serial))
1642 CONNECTION_UNLOCK (connection);
1646 CONNECTION_UNLOCK (connection);
1651 reply_handler_timeout (void *data)
1653 DBusConnection *connection;
1654 DBusDispatchStatus status;
1655 DBusPendingCall *pending = data;
1657 connection = pending->connection;
1659 CONNECTION_LOCK (connection);
1660 if (pending->timeout_link)
1662 _dbus_connection_queue_synthesized_message_link (connection,
1663 pending->timeout_link);
1664 pending->timeout_link = NULL;
1667 _dbus_connection_remove_timeout (connection,
1669 pending->timeout_added = FALSE;
1671 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1673 /* Unlocks, and calls out to user code */
1674 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1680 * Queues a message to send, as with dbus_connection_send_message(),
1681 * but also returns a #DBusPendingCall used to receive a reply to the
1682 * message. If no reply is received in the given timeout_milliseconds,
1683 * this function expires the pending reply and generates a synthetic
1684 * error reply (generated in-process, not by the remote application)
1685 * indicating that a timeout occurred.
1687 * A #DBusPendingCall will see a reply message after any filters, but
1688 * before any object instances or other handlers. A #DBusPendingCall
1689 * will always see exactly one reply message, unless it's cancelled
1690 * with dbus_pending_call_cancel().
1692 * If a filter filters out the reply before the handler sees it, the
1693 * reply is immediately timed out and a timeout error reply is
1694 * generated. If a filter removes the timeout error reply then the
1695 * #DBusPendingCall will get confused. Filtering the timeout error
1696 * is thus considered a bug and will print a warning.
1698 * If #NULL is passed for the pending_return, the #DBusPendingCall
1699 * will still be generated internally, and used to track
1700 * the message reply timeout. This means a timeout error will
1701 * occur if no reply arrives, unlike with dbus_connection_send().
1703 * If -1 is passed for the timeout, a sane default timeout is used. -1
1704 * is typically the best value for the timeout for this reason, unless
1705 * you want a very short or very long timeout. There is no way to
1706 * avoid a timeout entirely, other than passing INT_MAX for the
1707 * timeout to postpone it indefinitely.
1709 * @param connection the connection
1710 * @param message the message to send
1711 * @param pending_return return location for a #DBusPendingCall object, or #NULL
1712 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1713 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1717 dbus_connection_send_with_reply (DBusConnection *connection,
1718 DBusMessage *message,
1719 DBusPendingCall **pending_return,
1720 int timeout_milliseconds)
1722 DBusPendingCall *pending;
1724 DBusList *reply_link;
1725 dbus_int32_t serial = -1;
1727 _dbus_return_val_if_fail (connection != NULL, FALSE);
1728 _dbus_return_val_if_fail (message != NULL, FALSE);
1729 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1732 *pending_return = NULL;
1734 pending = _dbus_pending_call_new (connection,
1735 timeout_milliseconds,
1736 reply_handler_timeout);
1738 if (pending == NULL)
1741 CONNECTION_LOCK (connection);
1743 /* Assign a serial to the message */
1744 if (dbus_message_get_serial (message) == 0)
1746 serial = _dbus_connection_get_next_client_serial (connection);
1747 _dbus_message_set_serial (message, serial);
1750 pending->reply_serial = serial;
1752 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
1753 "No reply within specified time");
1757 reply_link = _dbus_list_alloc_link (reply);
1758 if (reply_link == NULL)
1760 CONNECTION_UNLOCK (connection);
1761 dbus_message_unref (reply);
1762 goto error_unlocked;
1765 pending->timeout_link = reply_link;
1767 /* Insert the serial in the pending replies hash;
1768 * hash takes a refcount on DBusPendingCall.
1769 * Also, add the timeout.
1771 if (!_dbus_connection_attach_pending_call_unlocked (connection,
1775 if (!_dbus_connection_send_unlocked (connection, message, NULL))
1777 _dbus_connection_detach_pending_call_and_unlock (connection,
1779 goto error_unlocked;
1783 *pending_return = pending;
1785 dbus_pending_call_unref (pending);
1787 CONNECTION_UNLOCK (connection);
1792 CONNECTION_UNLOCK (connection);
1794 dbus_pending_call_unref (pending);
1799 check_for_reply_unlocked (DBusConnection *connection,
1800 dbus_uint32_t client_serial)
1804 link = _dbus_list_get_first_link (&connection->incoming_messages);
1806 while (link != NULL)
1808 DBusMessage *reply = link->data;
1810 if (dbus_message_get_reply_serial (reply) == client_serial)
1812 _dbus_list_remove_link (&connection->incoming_messages, link);
1813 connection->n_incoming -= 1;
1816 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1823 * Blocks a certain time period while waiting for a reply.
1824 * If no reply arrives, returns #NULL.
1826 * @todo could use performance improvements (it keeps scanning
1827 * the whole message queue for example) and has thread issues,
1828 * see comments in source
1830 * Does not re-enter the main loop or run filter/path-registered
1831 * callbacks. The reply to the message will not be seen by
1834 * @param connection the connection
1835 * @param client_serial the reply serial to wait for
1836 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1837 * @returns the message that is the reply or #NULL if no reply
1840 _dbus_connection_block_for_reply (DBusConnection *connection,
1841 dbus_uint32_t client_serial,
1842 int timeout_milliseconds)
1844 long start_tv_sec, start_tv_usec;
1845 long end_tv_sec, end_tv_usec;
1846 long tv_sec, tv_usec;
1847 DBusDispatchStatus status;
1849 _dbus_return_val_if_fail (connection != NULL, NULL);
1850 _dbus_return_val_if_fail (client_serial != 0, NULL);
1851 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1853 if (timeout_milliseconds == -1)
1854 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
1856 /* it would probably seem logical to pass in _DBUS_INT_MAX
1857 * for infinite timeout, but then math below would get
1858 * all overflow-prone, so smack that down.
1860 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1861 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1863 /* Flush message queue */
1864 dbus_connection_flush (connection);
1866 CONNECTION_LOCK (connection);
1868 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1869 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1870 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1871 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1872 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1874 _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",
1875 timeout_milliseconds,
1877 start_tv_sec, start_tv_usec,
1878 end_tv_sec, end_tv_usec);
1880 /* Now we wait... */
1881 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1882 * gets the message before we do?
1884 /* always block at least once as we know we don't have the reply yet */
1885 _dbus_connection_do_iteration (connection,
1886 DBUS_ITERATION_DO_READING |
1887 DBUS_ITERATION_BLOCK,
1888 timeout_milliseconds);
1892 /* queue messages and get status */
1893 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1895 if (status == DBUS_DISPATCH_DATA_REMAINS)
1899 reply = check_for_reply_unlocked (connection, client_serial);
1902 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1904 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
1906 /* Unlocks, and calls out to user code */
1907 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1913 _dbus_get_current_time (&tv_sec, &tv_usec);
1915 if (!_dbus_connection_get_is_connected_unlocked (connection))
1917 else if (tv_sec < start_tv_sec)
1918 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
1919 else if (connection->disconnect_message_link == NULL)
1920 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
1921 else if (tv_sec < end_tv_sec ||
1922 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1924 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1925 (end_tv_usec - tv_usec) / 1000;
1926 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1927 _dbus_assert (timeout_milliseconds >= 0);
1929 if (status == DBUS_DISPATCH_NEED_MEMORY)
1931 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1932 * we may already have a reply in the buffer and just can't process
1935 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1937 if (timeout_milliseconds < 100)
1938 ; /* just busy loop */
1939 else if (timeout_milliseconds <= 1000)
1940 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1942 _dbus_sleep_milliseconds (1000);
1946 /* block again, we don't have the reply buffered yet. */
1947 _dbus_connection_do_iteration (connection,
1948 DBUS_ITERATION_DO_READING |
1949 DBUS_ITERATION_BLOCK,
1950 timeout_milliseconds);
1953 goto recheck_status;
1956 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
1957 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
1959 /* unlocks and calls out to user code */
1960 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1966 * Sends a message and blocks a certain time period while waiting for
1967 * a reply. This function does not reenter the main loop,
1968 * i.e. messages other than the reply are queued up but not
1969 * processed. This function is used to do non-reentrant "method
1972 * If a normal reply is received, it is returned, and removed from the
1973 * incoming message queue. If it is not received, #NULL is returned
1974 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
1975 * received, it is converted to a #DBusError and returned as an error,
1976 * then the reply message is deleted. If something else goes wrong,
1977 * result is set to whatever is appropriate, such as
1978 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1980 * @param connection the connection
1981 * @param message the message to send
1982 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1983 * @param error return location for error message
1984 * @returns the message that is the reply or #NULL with an error code if the
1988 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1989 DBusMessage *message,
1990 int timeout_milliseconds,
1993 dbus_uint32_t client_serial;
1996 _dbus_return_val_if_fail (connection != NULL, NULL);
1997 _dbus_return_val_if_fail (message != NULL, NULL);
1998 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1999 _dbus_return_val_if_error_is_set (error, NULL);
2001 if (!dbus_connection_send (connection, message, &client_serial))
2003 _DBUS_SET_OOM (error);
2007 reply = _dbus_connection_block_for_reply (connection,
2009 timeout_milliseconds);
2013 if (dbus_connection_get_is_connected (connection))
2014 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
2016 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
2020 else if (dbus_set_error_from_message (error, reply))
2022 dbus_message_unref (reply);
2030 * Blocks until the outgoing message queue is empty.
2032 * @param connection the connection.
2035 dbus_connection_flush (DBusConnection *connection)
2037 /* We have to specify DBUS_ITERATION_DO_READING here because
2038 * otherwise we could have two apps deadlock if they are both doing
2039 * a flush(), and the kernel buffers fill up. This could change the
2042 DBusDispatchStatus status;
2044 _dbus_return_if_fail (connection != NULL);
2046 CONNECTION_LOCK (connection);
2047 while (connection->n_outgoing > 0 &&
2048 _dbus_connection_get_is_connected_unlocked (connection))
2049 _dbus_connection_do_iteration (connection,
2050 DBUS_ITERATION_DO_READING |
2051 DBUS_ITERATION_DO_WRITING |
2052 DBUS_ITERATION_BLOCK,
2055 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2057 /* Unlocks and calls out to user code */
2058 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2061 /* Call with mutex held. Will drop it while waiting and re-acquire
2065 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
2067 _dbus_assert (connection->message_borrowed != NULL);
2069 while (connection->message_borrowed != NULL)
2070 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
2074 * Returns the first-received message from the incoming message queue,
2075 * leaving it in the queue. If the queue is empty, returns #NULL.
2077 * The caller does not own a reference to the returned message, and
2078 * must either return it using dbus_connection_return_message() or
2079 * keep it after calling dbus_connection_steal_borrowed_message(). No
2080 * one can get at the message while its borrowed, so return it as
2081 * quickly as possible and don't keep a reference to it after
2082 * returning it. If you need to keep the message, make a copy of it.
2084 * @param connection the connection.
2085 * @returns next message in the incoming queue.
2088 dbus_connection_borrow_message (DBusConnection *connection)
2090 DBusMessage *message;
2091 DBusDispatchStatus status;
2093 _dbus_return_val_if_fail (connection != NULL, NULL);
2094 /* can't borrow during dispatch */
2095 _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL);
2097 /* this is called for the side effect that it queues
2098 * up any messages from the transport
2100 status = dbus_connection_get_dispatch_status (connection);
2101 if (status != DBUS_DISPATCH_DATA_REMAINS)
2104 CONNECTION_LOCK (connection);
2106 if (connection->message_borrowed != NULL)
2107 _dbus_connection_wait_for_borrowed (connection);
2109 message = _dbus_list_get_first (&connection->incoming_messages);
2112 connection->message_borrowed = message;
2114 CONNECTION_UNLOCK (connection);
2119 * Used to return a message after peeking at it using
2120 * dbus_connection_borrow_message().
2122 * @param connection the connection
2123 * @param message the message from dbus_connection_borrow_message()
2126 dbus_connection_return_message (DBusConnection *connection,
2127 DBusMessage *message)
2129 _dbus_return_if_fail (connection != NULL);
2130 _dbus_return_if_fail (message != NULL);
2131 /* can't borrow during dispatch */
2132 _dbus_return_if_fail (!connection->dispatch_acquired);
2134 CONNECTION_LOCK (connection);
2136 _dbus_assert (message == connection->message_borrowed);
2138 connection->message_borrowed = NULL;
2139 dbus_condvar_wake_all (connection->message_returned_cond);
2141 CONNECTION_UNLOCK (connection);
2145 * Used to keep a message after peeking at it using
2146 * dbus_connection_borrow_message(). Before using this function, see
2147 * the caveats/warnings in the documentation for
2148 * dbus_connection_pop_message().
2150 * @param connection the connection
2151 * @param message the message from dbus_connection_borrow_message()
2154 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2155 DBusMessage *message)
2157 DBusMessage *pop_message;
2159 _dbus_return_if_fail (connection != NULL);
2160 _dbus_return_if_fail (message != NULL);
2161 /* can't borrow during dispatch */
2162 _dbus_return_if_fail (!connection->dispatch_acquired);
2164 CONNECTION_LOCK (connection);
2166 _dbus_assert (message == connection->message_borrowed);
2168 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
2169 _dbus_assert (message == pop_message);
2171 connection->n_incoming -= 1;
2173 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
2174 message, connection->n_incoming);
2176 connection->message_borrowed = NULL;
2177 dbus_condvar_wake_all (connection->message_returned_cond);
2179 CONNECTION_UNLOCK (connection);
2182 /* See dbus_connection_pop_message, but requires the caller to own
2183 * the lock before calling. May drop the lock while running.
2186 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
2188 if (connection->message_borrowed != NULL)
2189 _dbus_connection_wait_for_borrowed (connection);
2191 if (connection->n_incoming > 0)
2195 link = _dbus_list_pop_first_link (&connection->incoming_messages);
2196 connection->n_incoming -= 1;
2198 _dbus_verbose ("Message %p (%d %s '%s') removed from incoming queue %p, %d incoming\n",
2200 dbus_message_get_type (link->data),
2201 dbus_message_get_interface (link->data) ?
2202 dbus_message_get_interface (link->data) :
2204 dbus_message_get_signature (link->data),
2205 connection, connection->n_incoming);
2213 /* See dbus_connection_pop_message, but requires the caller to own
2214 * the lock before calling. May drop the lock while running.
2217 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
2221 link = _dbus_connection_pop_message_link_unlocked (connection);
2225 DBusMessage *message;
2227 message = link->data;
2229 _dbus_list_free_link (link);
2238 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
2239 DBusList *message_link)
2241 _dbus_assert (message_link != NULL);
2242 /* You can't borrow a message while a link is outstanding */
2243 _dbus_assert (connection->message_borrowed == NULL);
2245 _dbus_list_prepend_link (&connection->incoming_messages,
2247 connection->n_incoming += 1;
2249 _dbus_verbose ("Message %p (%d %s '%s') put back into queue %p, %d incoming\n",
2251 dbus_message_get_type (message_link->data),
2252 dbus_message_get_interface (message_link->data) ?
2253 dbus_message_get_interface (message_link->data) :
2255 dbus_message_get_signature (message_link->data),
2256 connection, connection->n_incoming);
2260 * Returns the first-received message from the incoming message queue,
2261 * removing it from the queue. The caller owns a reference to the
2262 * returned message. If the queue is empty, returns #NULL.
2264 * This function bypasses any message handlers that are registered,
2265 * and so using it is usually wrong. Instead, let the main loop invoke
2266 * dbus_connection_dispatch(). Popping messages manually is only
2267 * useful in very simple programs that don't share a #DBusConnection
2268 * with any libraries or other modules.
2270 * @param connection the connection.
2271 * @returns next message in the incoming queue.
2274 dbus_connection_pop_message (DBusConnection *connection)
2276 DBusMessage *message;
2277 DBusDispatchStatus status;
2279 /* this is called for the side effect that it queues
2280 * up any messages from the transport
2282 status = dbus_connection_get_dispatch_status (connection);
2283 if (status != DBUS_DISPATCH_DATA_REMAINS)
2286 CONNECTION_LOCK (connection);
2288 message = _dbus_connection_pop_message_unlocked (connection);
2290 _dbus_verbose ("Returning popped message %p\n", message);
2292 CONNECTION_UNLOCK (connection);
2298 * Acquire the dispatcher. This must be done before dispatching
2299 * messages in order to guarantee the right order of
2300 * message delivery. May sleep and drop the connection mutex
2301 * while waiting for the dispatcher.
2303 * @param connection the connection.
2306 _dbus_connection_acquire_dispatch (DBusConnection *connection)
2308 if (connection->dispatch_acquired)
2309 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
2310 _dbus_assert (!connection->dispatch_acquired);
2312 connection->dispatch_acquired = TRUE;
2316 * Release the dispatcher when you're done with it. Only call
2317 * after you've acquired the dispatcher. Wakes up at most one
2318 * thread currently waiting to acquire the dispatcher.
2320 * @param connection the connection.
2323 _dbus_connection_release_dispatch (DBusConnection *connection)
2325 _dbus_assert (connection->dispatch_acquired);
2327 connection->dispatch_acquired = FALSE;
2328 dbus_condvar_wake_one (connection->dispatch_cond);
2332 _dbus_connection_failed_pop (DBusConnection *connection,
2333 DBusList *message_link)
2335 _dbus_list_prepend_link (&connection->incoming_messages,
2337 connection->n_incoming += 1;
2340 static DBusDispatchStatus
2341 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
2343 if (connection->n_incoming > 0)
2344 return DBUS_DISPATCH_DATA_REMAINS;
2345 else if (!_dbus_transport_queue_messages (connection->transport))
2346 return DBUS_DISPATCH_NEED_MEMORY;
2349 DBusDispatchStatus status;
2351 status = _dbus_transport_get_dispatch_status (connection->transport);
2353 if (status == DBUS_DISPATCH_COMPLETE &&
2354 connection->disconnect_message_link &&
2355 !_dbus_transport_get_is_connected (connection->transport))
2357 /* We haven't sent the disconnect message already,
2358 * and all real messages have been queued up.
2360 _dbus_connection_queue_synthesized_message_link (connection,
2361 connection->disconnect_message_link);
2362 connection->disconnect_message_link = NULL;
2365 if (status != DBUS_DISPATCH_COMPLETE)
2367 else if (connection->n_incoming > 0)
2368 return DBUS_DISPATCH_DATA_REMAINS;
2370 return DBUS_DISPATCH_COMPLETE;
2375 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
2376 DBusDispatchStatus new_status)
2378 dbus_bool_t changed;
2379 DBusDispatchStatusFunction function;
2382 /* We have the lock */
2384 _dbus_connection_ref_unlocked (connection);
2386 changed = new_status != connection->last_dispatch_status;
2388 connection->last_dispatch_status = new_status;
2390 function = connection->dispatch_status_function;
2391 data = connection->dispatch_status_data;
2393 /* We drop the lock */
2394 CONNECTION_UNLOCK (connection);
2396 if (changed && function)
2398 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
2399 connection, new_status,
2400 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
2401 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
2402 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
2404 (* function) (connection, new_status, data);
2407 dbus_connection_unref (connection);
2411 * Gets the current state (what we would currently return
2412 * from dbus_connection_dispatch()) but doesn't actually
2413 * dispatch any messages.
2415 * @param connection the connection.
2416 * @returns current dispatch status
2419 dbus_connection_get_dispatch_status (DBusConnection *connection)
2421 DBusDispatchStatus status;
2423 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2425 CONNECTION_LOCK (connection);
2427 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2429 CONNECTION_UNLOCK (connection);
2435 * Processes data buffered while handling watches, queueing zero or
2436 * more incoming messages. Then pops the first-received message from
2437 * the current incoming message queue, runs any handlers for it, and
2438 * unrefs the message. Returns a status indicating whether messages/data
2439 * remain, more memory is needed, or all data has been processed.
2441 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
2442 * does not necessarily dispatch a message, as the data may
2443 * be part of authentication or the like.
2445 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
2447 * @todo right now a message filter gets run on replies to a pending
2448 * call in here, but not in the case where we block without entering
2449 * the main loop. Simple solution might be to just have the pending
2450 * call stuff run before the filters.
2452 * @todo FIXME what if we call out to application code to handle a
2453 * message, holding the dispatch lock, and the application code runs
2454 * the main loop and dispatches again? Probably deadlocks at the
2455 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
2456 * and then the GSource etc. could handle the situation?
2458 * @param connection the connection
2459 * @returns dispatch status
2462 dbus_connection_dispatch (DBusConnection *connection)
2464 DBusMessage *message;
2465 DBusList *link, *filter_list_copy, *message_link;
2466 DBusHandlerResult result;
2467 DBusPendingCall *pending;
2468 dbus_int32_t reply_serial;
2469 DBusDispatchStatus status;
2471 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2473 CONNECTION_LOCK (connection);
2474 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2475 if (status != DBUS_DISPATCH_DATA_REMAINS)
2477 /* unlocks and calls out to user code */
2478 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2482 /* We need to ref the connection since the callback could potentially
2483 * drop the last ref to it
2485 _dbus_connection_ref_unlocked (connection);
2487 _dbus_connection_acquire_dispatch (connection);
2489 /* This call may drop the lock during the execution (if waiting for
2490 * borrowed messages to be returned) but the order of message
2491 * dispatch if several threads call dispatch() is still
2492 * protected by the lock, since only one will get the lock, and that
2493 * one will finish the message dispatching
2495 message_link = _dbus_connection_pop_message_link_unlocked (connection);
2496 if (message_link == NULL)
2498 /* another thread dispatched our stuff */
2500 _dbus_connection_release_dispatch (connection);
2502 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2504 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2506 dbus_connection_unref (connection);
2511 message = message_link->data;
2513 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2515 reply_serial = dbus_message_get_reply_serial (message);
2516 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
2519 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
2521 _dbus_connection_release_dispatch (connection);
2523 _dbus_connection_failed_pop (connection, message_link);
2525 /* unlocks and calls user code */
2526 _dbus_connection_update_dispatch_status_and_unlock (connection,
2527 DBUS_DISPATCH_NEED_MEMORY);
2529 dbus_connection_unref (connection);
2531 return DBUS_DISPATCH_NEED_MEMORY;
2534 _dbus_list_foreach (&filter_list_copy,
2535 (DBusForeachFunction)_dbus_message_filter_ref,
2538 /* We're still protected from dispatch() reentrancy here
2539 * since we acquired the dispatcher
2541 CONNECTION_UNLOCK (connection);
2543 link = _dbus_list_get_first_link (&filter_list_copy);
2544 while (link != NULL)
2546 DBusMessageFilter *filter = link->data;
2547 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2549 _dbus_verbose (" running filter on message %p\n", message);
2550 result = (* filter->function) (connection, message, filter->user_data);
2552 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2558 _dbus_list_foreach (&filter_list_copy,
2559 (DBusForeachFunction)_dbus_message_filter_unref,
2561 _dbus_list_clear (&filter_list_copy);
2563 CONNECTION_LOCK (connection);
2565 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2568 /* Did a reply we were waiting on get filtered? */
2569 if (pending && result == DBUS_HANDLER_RESULT_HANDLED)
2571 /* Queue the timeout immediately! */
2572 if (pending->timeout_link)
2574 _dbus_connection_queue_synthesized_message_link (connection,
2575 pending->timeout_link);
2576 pending->timeout_link = NULL;
2580 /* We already queued the timeout? Then it was filtered! */
2581 _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial);
2585 if (result == DBUS_HANDLER_RESULT_HANDLED)
2590 _dbus_pending_call_complete_and_unlock (pending, message);
2594 CONNECTION_LOCK (connection);
2598 /* We're still protected from dispatch() reentrancy here
2599 * since we acquired the dispatcher
2601 _dbus_verbose (" running object path dispatch on message %p (%d %s '%s')\n",
2603 dbus_message_get_type (message),
2604 dbus_message_get_interface (message) ?
2605 dbus_message_get_interface (message) :
2607 dbus_message_get_signature (message));
2609 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
2612 CONNECTION_LOCK (connection);
2614 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2617 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
2621 DBusPreallocatedSend *preallocated;
2623 _dbus_verbose (" sending error %s\n",
2624 DBUS_ERROR_UNKNOWN_METHOD);
2626 if (!_dbus_string_init (&str))
2628 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2632 if (!_dbus_string_append_printf (&str,
2633 "Method \"%s\" on interface \"%s\" doesn't exist\n",
2634 dbus_message_get_member (message),
2635 dbus_message_get_interface (message)))
2637 _dbus_string_free (&str);
2638 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2642 reply = dbus_message_new_error (message,
2643 DBUS_ERROR_UNKNOWN_METHOD,
2644 _dbus_string_get_const_data (&str));
2645 _dbus_string_free (&str);
2649 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2653 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2655 if (preallocated == NULL)
2657 dbus_message_unref (reply);
2658 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2662 _dbus_connection_send_preallocated_unlocked (connection, preallocated,
2665 dbus_message_unref (reply);
2667 result = DBUS_HANDLER_RESULT_HANDLED;
2670 _dbus_verbose (" done dispatching %p (%d %s '%s') on connection %p\n", message,
2671 dbus_message_get_type (message),
2672 dbus_message_get_interface (message) ?
2673 dbus_message_get_interface (message) :
2675 dbus_message_get_signature (message),
2679 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2681 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
2683 /* Put message back, and we'll start over.
2684 * Yes this means handlers must be idempotent if they
2685 * don't return HANDLED; c'est la vie.
2687 _dbus_connection_putback_message_link_unlocked (connection,
2692 _dbus_verbose ("Done with message in %s\n", _DBUS_FUNCTION_NAME);
2694 if (connection->exit_on_disconnect &&
2695 dbus_message_is_signal (message,
2696 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
2699 _dbus_verbose ("Exiting on Disconnected signal\n");
2700 CONNECTION_UNLOCK (connection);
2702 _dbus_assert_not_reached ("Call to exit() returned");
2705 _dbus_list_free_link (message_link);
2706 dbus_message_unref (message); /* don't want the message to count in max message limits
2707 * in computing dispatch status below
2711 _dbus_connection_release_dispatch (connection);
2713 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2715 /* unlocks and calls user code */
2716 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2718 dbus_connection_unref (connection);
2724 * Sets the watch functions for the connection. These functions are
2725 * responsible for making the application's main loop aware of file
2726 * descriptors that need to be monitored for events, using select() or
2727 * poll(). When using Qt, typically the DBusAddWatchFunction would
2728 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2729 * could call g_io_add_watch(), or could be used as part of a more
2730 * elaborate GSource. Note that when a watch is added, it may
2733 * The DBusWatchToggledFunction notifies the application that the
2734 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2735 * to check this. A disabled watch should have no effect, and enabled
2736 * watch should be added to the main loop. This feature is used
2737 * instead of simply adding/removing the watch because
2738 * enabling/disabling can be done without memory allocation. The
2739 * toggled function may be NULL if a main loop re-queries
2740 * dbus_watch_get_enabled() every time anyway.
2742 * The DBusWatch can be queried for the file descriptor to watch using
2743 * dbus_watch_get_fd(), and for the events to watch for using
2744 * dbus_watch_get_flags(). The flags returned by
2745 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2746 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2747 * all watches implicitly include a watch for hangups, errors, and
2748 * other exceptional conditions.
2750 * Once a file descriptor becomes readable or writable, or an exception
2751 * occurs, dbus_watch_handle() should be called to
2752 * notify the connection of the file descriptor's condition.
2754 * dbus_watch_handle() cannot be called during the
2755 * DBusAddWatchFunction, as the connection will not be ready to handle
2758 * It is not allowed to reference a DBusWatch after it has been passed
2759 * to remove_function.
2761 * If #FALSE is returned due to lack of memory, the failure may be due
2762 * to a #FALSE return from the new add_function. If so, the
2763 * add_function may have been called successfully one or more times,
2764 * but the remove_function will also have been called to remove any
2765 * successful adds. i.e. if #FALSE is returned the net result
2766 * should be that dbus_connection_set_watch_functions() has no effect,
2767 * but the add_function and remove_function may have been called.
2769 * @todo We need to drop the lock when we call the
2770 * add/remove/toggled functions which can be a side effect
2771 * of setting the watch functions.
2773 * @param connection the connection.
2774 * @param add_function function to begin monitoring a new descriptor.
2775 * @param remove_function function to stop monitoring a descriptor.
2776 * @param toggled_function function to notify of enable/disable
2777 * @param data data to pass to add_function and remove_function.
2778 * @param free_data_function function to be called to free the data.
2779 * @returns #FALSE on failure (no memory)
2782 dbus_connection_set_watch_functions (DBusConnection *connection,
2783 DBusAddWatchFunction add_function,
2784 DBusRemoveWatchFunction remove_function,
2785 DBusWatchToggledFunction toggled_function,
2787 DBusFreeFunction free_data_function)
2791 _dbus_return_val_if_fail (connection != NULL, FALSE);
2793 CONNECTION_LOCK (connection);
2794 /* ref connection for slightly better reentrancy */
2795 _dbus_connection_ref_unlocked (connection);
2797 /* FIXME this can call back into user code, and we need to drop the
2798 * connection lock when it does.
2800 retval = _dbus_watch_list_set_functions (connection->watches,
2801 add_function, remove_function,
2803 data, free_data_function);
2805 CONNECTION_UNLOCK (connection);
2806 /* drop our paranoid refcount */
2807 dbus_connection_unref (connection);
2813 * Sets the timeout functions for the connection. These functions are
2814 * responsible for making the application's main loop aware of timeouts.
2815 * When using Qt, typically the DBusAddTimeoutFunction would create a
2816 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2819 * The DBusTimeoutToggledFunction notifies the application that the
2820 * timeout has been enabled or disabled. Call
2821 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2822 * have no effect, and enabled timeout should be added to the main
2823 * loop. This feature is used instead of simply adding/removing the
2824 * timeout because enabling/disabling can be done without memory
2825 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2826 * to enable and disable. The toggled function may be NULL if a main
2827 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2828 * Whenever a timeout is toggled, its interval may change.
2830 * The DBusTimeout can be queried for the timer interval using
2831 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
2832 * repeatedly, each time the interval elapses, starting after it has
2833 * elapsed once. The timeout stops firing when it is removed with the
2834 * given remove_function. The timer interval may change whenever the
2835 * timeout is added, removed, or toggled.
2837 * @param connection the connection.
2838 * @param add_function function to add a timeout.
2839 * @param remove_function function to remove a timeout.
2840 * @param toggled_function function to notify of enable/disable
2841 * @param data data to pass to add_function and remove_function.
2842 * @param free_data_function function to be called to free the data.
2843 * @returns #FALSE on failure (no memory)
2846 dbus_connection_set_timeout_functions (DBusConnection *connection,
2847 DBusAddTimeoutFunction add_function,
2848 DBusRemoveTimeoutFunction remove_function,
2849 DBusTimeoutToggledFunction toggled_function,
2851 DBusFreeFunction free_data_function)
2855 _dbus_return_val_if_fail (connection != NULL, FALSE);
2857 CONNECTION_LOCK (connection);
2858 /* ref connection for slightly better reentrancy */
2859 _dbus_connection_ref_unlocked (connection);
2861 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2862 add_function, remove_function,
2864 data, free_data_function);
2866 CONNECTION_UNLOCK (connection);
2867 /* drop our paranoid refcount */
2868 dbus_connection_unref (connection);
2874 * Sets the mainloop wakeup function for the connection. Thi function is
2875 * responsible for waking up the main loop (if its sleeping) when some some
2876 * change has happened to the connection that the mainloop needs to reconsiders
2877 * (e.g. a message has been queued for writing).
2878 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2879 * When using GLib, it would call g_main_context_wakeup().
2882 * @param connection the connection.
2883 * @param wakeup_main_function function to wake up the mainloop
2884 * @param data data to pass wakeup_main_function
2885 * @param free_data_function function to be called to free the data.
2888 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2889 DBusWakeupMainFunction wakeup_main_function,
2891 DBusFreeFunction free_data_function)
2894 DBusFreeFunction old_free_data;
2896 _dbus_return_if_fail (connection != NULL);
2898 CONNECTION_LOCK (connection);
2899 old_data = connection->wakeup_main_data;
2900 old_free_data = connection->free_wakeup_main_data;
2902 connection->wakeup_main_function = wakeup_main_function;
2903 connection->wakeup_main_data = data;
2904 connection->free_wakeup_main_data = free_data_function;
2906 CONNECTION_UNLOCK (connection);
2908 /* Callback outside the lock */
2910 (*old_free_data) (old_data);
2914 * Set a function to be invoked when the dispatch status changes.
2915 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
2916 * dbus_connection_dispatch() needs to be called to process incoming
2917 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
2918 * from inside the DBusDispatchStatusFunction. Indeed, almost
2919 * any reentrancy in this function is a bad idea. Instead,
2920 * the DBusDispatchStatusFunction should simply save an indication
2921 * that messages should be dispatched later, when the main loop
2924 * @param connection the connection
2925 * @param function function to call on dispatch status changes
2926 * @param data data for function
2927 * @param free_data_function free the function data
2930 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
2931 DBusDispatchStatusFunction function,
2933 DBusFreeFunction free_data_function)
2936 DBusFreeFunction old_free_data;
2938 _dbus_return_if_fail (connection != NULL);
2940 CONNECTION_LOCK (connection);
2941 old_data = connection->dispatch_status_data;
2942 old_free_data = connection->free_dispatch_status_data;
2944 connection->dispatch_status_function = function;
2945 connection->dispatch_status_data = data;
2946 connection->free_dispatch_status_data = free_data_function;
2948 CONNECTION_UNLOCK (connection);
2950 /* Callback outside the lock */
2952 (*old_free_data) (old_data);
2956 * Get the UNIX file descriptor of the connection, if any. This can
2957 * be used for SELinux access control checks with getpeercon() for
2958 * example. DO NOT read or write to the file descriptor, or try to
2959 * select() on it; use DBusWatch for main loop integration. Not all
2960 * connections will have a file descriptor. So for adding descriptors
2961 * to the main loop, use dbus_watch_get_fd() and so forth.
2963 * @param connection the connection
2964 * @param fd return location for the file descriptor.
2965 * @returns #TRUE if fd is successfully obtained.
2968 dbus_connection_get_unix_fd (DBusConnection *connection,
2973 _dbus_return_val_if_fail (connection != NULL, FALSE);
2974 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
2976 CONNECTION_LOCK (connection);
2978 retval = _dbus_transport_get_unix_fd (connection->transport,
2981 CONNECTION_UNLOCK (connection);
2987 * Gets the UNIX user ID of the connection if any.
2988 * Returns #TRUE if the uid is filled in.
2989 * Always returns #FALSE on non-UNIX platforms.
2990 * Always returns #FALSE prior to authenticating the
2993 * @param connection the connection
2994 * @param uid return location for the user ID
2995 * @returns #TRUE if uid is filled in with a valid user ID
2998 dbus_connection_get_unix_user (DBusConnection *connection,
3003 _dbus_return_val_if_fail (connection != NULL, FALSE);
3004 _dbus_return_val_if_fail (uid != NULL, FALSE);
3006 CONNECTION_LOCK (connection);
3008 if (!_dbus_transport_get_is_authenticated (connection->transport))
3011 result = _dbus_transport_get_unix_user (connection->transport,
3013 CONNECTION_UNLOCK (connection);
3019 * Gets the process ID of the connection if any.
3020 * Returns #TRUE if the uid is filled in.
3021 * Always returns #FALSE prior to authenticating the
3024 * @param connection the connection
3025 * @param pid return location for the process ID
3026 * @returns #TRUE if uid is filled in with a valid process ID
3029 dbus_connection_get_unix_process_id (DBusConnection *connection,
3034 _dbus_return_val_if_fail (connection != NULL, FALSE);
3035 _dbus_return_val_if_fail (pid != NULL, FALSE);
3037 CONNECTION_LOCK (connection);
3039 if (!_dbus_transport_get_is_authenticated (connection->transport))
3042 result = _dbus_transport_get_unix_process_id (connection->transport,
3044 CONNECTION_UNLOCK (connection);
3050 * Sets a predicate function used to determine whether a given user ID
3051 * is allowed to connect. When an incoming connection has
3052 * authenticated with a particular user ID, this function is called;
3053 * if it returns #TRUE, the connection is allowed to proceed,
3054 * otherwise the connection is disconnected.
3056 * If the function is set to #NULL (as it is by default), then
3057 * only the same UID as the server process will be allowed to
3060 * @param connection the connection
3061 * @param function the predicate
3062 * @param data data to pass to the predicate
3063 * @param free_data_function function to free the data
3066 dbus_connection_set_unix_user_function (DBusConnection *connection,
3067 DBusAllowUnixUserFunction function,
3069 DBusFreeFunction free_data_function)
3071 void *old_data = NULL;
3072 DBusFreeFunction old_free_function = NULL;
3074 _dbus_return_if_fail (connection != NULL);
3076 CONNECTION_LOCK (connection);
3077 _dbus_transport_set_unix_user_function (connection->transport,
3078 function, data, free_data_function,
3079 &old_data, &old_free_function);
3080 CONNECTION_UNLOCK (connection);
3082 if (old_free_function != NULL)
3083 (* old_free_function) (old_data);
3087 * Adds a message filter. Filters are handlers that are run on all
3088 * incoming messages, prior to the objects registered with
3089 * dbus_connection_register_object_path(). Filters are run in the
3090 * order that they were added. The same handler can be added as a
3091 * filter more than once, in which case it will be run more than once.
3092 * Filters added during a filter callback won't be run on the message
3095 * @todo we don't run filters on messages while blocking without
3096 * entering the main loop, since filters are run as part of
3097 * dbus_connection_dispatch(). This is probably a feature, as filters
3098 * could create arbitrary reentrancy. But kind of sucks if you're
3099 * trying to filter METHOD_RETURN for some reason.
3101 * @param connection the connection
3102 * @param function function to handle messages
3103 * @param user_data user data to pass to the function
3104 * @param free_data_function function to use for freeing user data
3105 * @returns #TRUE on success, #FALSE if not enough memory.
3108 dbus_connection_add_filter (DBusConnection *connection,
3109 DBusHandleMessageFunction function,
3111 DBusFreeFunction free_data_function)
3113 DBusMessageFilter *filter;
3115 _dbus_return_val_if_fail (connection != NULL, FALSE);
3116 _dbus_return_val_if_fail (function != NULL, FALSE);
3118 filter = dbus_new0 (DBusMessageFilter, 1);
3122 filter->refcount.value = 1;
3124 CONNECTION_LOCK (connection);
3126 if (!_dbus_list_append (&connection->filter_list,
3129 _dbus_message_filter_unref (filter);
3130 CONNECTION_UNLOCK (connection);
3134 /* Fill in filter after all memory allocated,
3135 * so we don't run the free_user_data_function
3136 * if the add_filter() fails
3139 filter->function = function;
3140 filter->user_data = user_data;
3141 filter->free_user_data_function = free_data_function;
3143 CONNECTION_UNLOCK (connection);
3148 * Removes a previously-added message filter. It is a programming
3149 * error to call this function for a handler that has not been added
3150 * as a filter. If the given handler was added more than once, only
3151 * one instance of it will be removed (the most recently-added
3154 * @param connection the connection
3155 * @param function the handler to remove
3156 * @param user_data user data for the handler to remove
3160 dbus_connection_remove_filter (DBusConnection *connection,
3161 DBusHandleMessageFunction function,
3165 DBusMessageFilter *filter;
3167 _dbus_return_if_fail (connection != NULL);
3168 _dbus_return_if_fail (function != NULL);
3170 CONNECTION_LOCK (connection);
3174 link = _dbus_list_get_last_link (&connection->filter_list);
3175 while (link != NULL)
3177 filter = link->data;
3179 if (filter->function == function &&
3180 filter->user_data == user_data)
3182 _dbus_list_remove_link (&connection->filter_list, link);
3183 filter->function = NULL;
3188 link = _dbus_list_get_prev_link (&connection->filter_list, link);
3191 CONNECTION_UNLOCK (connection);
3193 #ifndef DBUS_DISABLE_CHECKS
3196 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
3197 function, user_data);
3202 /* Call application code */
3203 if (filter->free_user_data_function)
3204 (* filter->free_user_data_function) (filter->user_data);
3206 filter->free_user_data_function = NULL;
3207 filter->user_data = NULL;
3209 _dbus_message_filter_unref (filter);
3213 * Registers a handler for a given path in the object hierarchy.
3214 * The given vtable handles messages sent to exactly the given path.
3217 * @param connection the connection
3218 * @param path a '/' delimited string of path elements
3219 * @param vtable the virtual table
3220 * @param user_data data to pass to functions in the vtable
3221 * @returns #FALSE if not enough memory
3224 dbus_connection_register_object_path (DBusConnection *connection,
3226 const DBusObjectPathVTable *vtable,
3229 char **decomposed_path;
3232 _dbus_return_val_if_fail (connection != NULL, FALSE);
3233 _dbus_return_val_if_fail (path != NULL, FALSE);
3234 _dbus_return_val_if_fail (path[0] == '/', FALSE);
3235 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3237 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
3240 CONNECTION_LOCK (connection);
3242 retval = _dbus_object_tree_register (connection->objects,
3244 (const char **) decomposed_path, vtable,
3247 CONNECTION_UNLOCK (connection);
3249 dbus_free_string_array (decomposed_path);
3255 * Registers a fallback handler for a given subsection of the object
3256 * hierarchy. The given vtable handles messages at or below the given
3257 * path. You can use this to establish a default message handling
3258 * policy for a whole "subdirectory."
3260 * @param connection the connection
3261 * @param path a '/' delimited string of path elements
3262 * @param vtable the virtual table
3263 * @param user_data data to pass to functions in the vtable
3264 * @returns #FALSE if not enough memory
3267 dbus_connection_register_fallback (DBusConnection *connection,
3269 const DBusObjectPathVTable *vtable,
3272 char **decomposed_path;
3275 _dbus_return_val_if_fail (connection != NULL, FALSE);
3276 _dbus_return_val_if_fail (path != NULL, FALSE);
3277 _dbus_return_val_if_fail (path[0] == '/', FALSE);
3278 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3280 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
3283 CONNECTION_LOCK (connection);
3285 retval = _dbus_object_tree_register (connection->objects,
3287 (const char **) decomposed_path, vtable,
3290 CONNECTION_UNLOCK (connection);
3292 dbus_free_string_array (decomposed_path);
3298 * Unregisters the handler registered with exactly the given path.
3299 * It's a bug to call this function for a path that isn't registered.
3300 * Can unregister both fallback paths and object paths.
3302 * @param connection the connection
3303 * @param path a '/' delimited string of path elements
3304 * @returns #FALSE if not enough memory
3307 dbus_connection_unregister_object_path (DBusConnection *connection,
3310 char **decomposed_path;
3312 _dbus_return_val_if_fail (connection != NULL, FALSE);
3313 _dbus_return_val_if_fail (path != NULL, FALSE);
3314 _dbus_return_val_if_fail (path[0] == '/', FALSE);
3316 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
3319 CONNECTION_LOCK (connection);
3321 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
3323 dbus_free_string_array (decomposed_path);
3329 * Lists the registered fallback handlers and object path handlers at
3330 * the given parent_path. The returned array should be freed with
3331 * dbus_free_string_array().
3333 * @param connection the connection
3334 * @param parent_path the path to list the child handlers of
3335 * @param child_entries returns #NULL-terminated array of children
3336 * @returns #FALSE if no memory to allocate the child entries
3339 dbus_connection_list_registered (DBusConnection *connection,
3340 const char *parent_path,
3341 char ***child_entries)
3343 char **decomposed_path;
3345 _dbus_return_val_if_fail (connection != NULL, FALSE);
3346 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
3347 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
3348 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
3350 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
3353 CONNECTION_LOCK (connection);
3355 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
3356 (const char **) decomposed_path,
3358 dbus_free_string_array (decomposed_path);
3363 static DBusDataSlotAllocator slot_allocator;
3364 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
3367 * Allocates an integer ID to be used for storing application-specific
3368 * data on any DBusConnection. The allocated ID may then be used
3369 * with dbus_connection_set_data() and dbus_connection_get_data().
3370 * The passed-in slot must be initialized to -1, and is filled in
3371 * with the slot ID. If the passed-in slot is not -1, it's assumed
3372 * to be already allocated, and its refcount is incremented.
3374 * The allocated slot is global, i.e. all DBusConnection objects will
3375 * have a slot with the given integer ID reserved.
3377 * @param slot_p address of a global variable storing the slot
3378 * @returns #FALSE on failure (no memory)
3381 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
3383 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3384 _DBUS_LOCK_NAME (connection_slots),
3389 * Deallocates a global ID for connection data slots.
3390 * dbus_connection_get_data() and dbus_connection_set_data() may no
3391 * longer be used with this slot. Existing data stored on existing
3392 * DBusConnection objects will be freed when the connection is
3393 * finalized, but may not be retrieved (and may only be replaced if
3394 * someone else reallocates the slot). When the refcount on the
3395 * passed-in slot reaches 0, it is set to -1.
3397 * @param slot_p address storing the slot to deallocate
3400 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
3402 _dbus_return_if_fail (*slot_p >= 0);
3404 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3408 * Stores a pointer on a DBusConnection, along
3409 * with an optional function to be used for freeing
3410 * the data when the data is set again, or when
3411 * the connection is finalized. The slot number
3412 * must have been allocated with dbus_connection_allocate_data_slot().
3414 * @param connection the connection
3415 * @param slot the slot number
3416 * @param data the data to store
3417 * @param free_data_func finalizer function for the data
3418 * @returns #TRUE if there was enough memory to store the data
3421 dbus_connection_set_data (DBusConnection *connection,
3424 DBusFreeFunction free_data_func)
3426 DBusFreeFunction old_free_func;
3430 _dbus_return_val_if_fail (connection != NULL, FALSE);
3431 _dbus_return_val_if_fail (slot >= 0, FALSE);
3433 CONNECTION_LOCK (connection);
3435 retval = _dbus_data_slot_list_set (&slot_allocator,
3436 &connection->slot_list,
3437 slot, data, free_data_func,
3438 &old_free_func, &old_data);
3440 CONNECTION_UNLOCK (connection);
3444 /* Do the actual free outside the connection lock */
3446 (* old_free_func) (old_data);
3453 * Retrieves data previously set with dbus_connection_set_data().
3454 * The slot must still be allocated (must not have been freed).
3456 * @param connection the connection
3457 * @param slot the slot to get data from
3458 * @returns the data, or #NULL if not found
3461 dbus_connection_get_data (DBusConnection *connection,
3466 _dbus_return_val_if_fail (connection != NULL, NULL);
3468 CONNECTION_LOCK (connection);
3470 res = _dbus_data_slot_list_get (&slot_allocator,
3471 &connection->slot_list,
3474 CONNECTION_UNLOCK (connection);
3480 * This function sets a global flag for whether dbus_connection_new()
3481 * will set SIGPIPE behavior to SIG_IGN.
3483 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
3486 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
3488 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
3492 * Specifies the maximum size message this connection is allowed to
3493 * receive. Larger messages will result in disconnecting the
3496 * @param connection a #DBusConnection
3497 * @param size maximum message size the connection can receive, in bytes
3500 dbus_connection_set_max_message_size (DBusConnection *connection,
3503 _dbus_return_if_fail (connection != NULL);
3505 CONNECTION_LOCK (connection);
3506 _dbus_transport_set_max_message_size (connection->transport,
3508 CONNECTION_UNLOCK (connection);
3512 * Gets the value set by dbus_connection_set_max_message_size().
3514 * @param connection the connection
3515 * @returns the max size of a single message
3518 dbus_connection_get_max_message_size (DBusConnection *connection)
3522 _dbus_return_val_if_fail (connection != NULL, 0);
3524 CONNECTION_LOCK (connection);
3525 res = _dbus_transport_get_max_message_size (connection->transport);
3526 CONNECTION_UNLOCK (connection);
3531 * Sets the maximum total number of bytes that can be used for all messages
3532 * received on this connection. Messages count toward the maximum until
3533 * they are finalized. When the maximum is reached, the connection will
3534 * not read more data until some messages are finalized.
3536 * The semantics of the maximum are: if outstanding messages are
3537 * already above the maximum, additional messages will not be read.
3538 * The semantics are not: if the next message would cause us to exceed
3539 * the maximum, we don't read it. The reason is that we don't know the
3540 * size of a message until after we read it.
3542 * Thus, the max live messages size can actually be exceeded
3543 * by up to the maximum size of a single message.
3545 * Also, if we read say 1024 bytes off the wire in a single read(),
3546 * and that contains a half-dozen small messages, we may exceed the
3547 * size max by that amount. But this should be inconsequential.
3549 * This does imply that we can't call read() with a buffer larger
3550 * than we're willing to exceed this limit by.
3552 * @param connection the connection
3553 * @param size the maximum size in bytes of all outstanding messages
3556 dbus_connection_set_max_received_size (DBusConnection *connection,
3559 _dbus_return_if_fail (connection != NULL);
3561 CONNECTION_LOCK (connection);
3562 _dbus_transport_set_max_received_size (connection->transport,
3564 CONNECTION_UNLOCK (connection);
3568 * Gets the value set by dbus_connection_set_max_received_size().
3570 * @param connection the connection
3571 * @returns the max size of all live messages
3574 dbus_connection_get_max_received_size (DBusConnection *connection)
3578 _dbus_return_val_if_fail (connection != NULL, 0);
3580 CONNECTION_LOCK (connection);
3581 res = _dbus_transport_get_max_received_size (connection->transport);
3582 CONNECTION_UNLOCK (connection);
3587 * Gets the approximate size in bytes of all messages in the outgoing
3588 * message queue. The size is approximate in that you shouldn't use
3589 * it to decide how many bytes to read off the network or anything
3590 * of that nature, as optimizations may choose to tell small white lies
3591 * to avoid performance overhead.
3593 * @param connection the connection
3594 * @returns the number of bytes that have been queued up but not sent
3597 dbus_connection_get_outgoing_size (DBusConnection *connection)
3601 _dbus_return_val_if_fail (connection != NULL, 0);
3603 CONNECTION_LOCK (connection);
3604 res = _dbus_counter_get_value (connection->outgoing_counter);
3605 CONNECTION_UNLOCK (connection);