1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-connection.h"
26 #include "dbus-list.h"
27 #include "dbus-timeout.h"
28 #include "dbus-transport.h"
29 #include "dbus-watch.h"
30 #include "dbus-connection-internal.h"
31 #include "dbus-list.h"
32 #include "dbus-hash.h"
33 #include "dbus-message-internal.h"
34 #include "dbus-threads.h"
35 #include "dbus-protocol.h"
36 #include "dbus-dataslot.h"
37 #include "dbus-string.h"
38 #include "dbus-pending-call.h"
39 #include "dbus-object-tree.h"
42 #define CONNECTION_LOCK(connection) do { \
43 _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); \
44 dbus_mutex_lock ((connection)->mutex); \
46 #define CONNECTION_UNLOCK(connection) do { \
47 _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); \
48 dbus_mutex_unlock ((connection)->mutex); \
51 #define CONNECTION_LOCK(connection) dbus_mutex_lock ((connection)->mutex)
52 #define CONNECTION_UNLOCK(connection) dbus_mutex_unlock ((connection)->mutex)
56 * @defgroup DBusConnection DBusConnection
58 * @brief Connection to another application
60 * A DBusConnection represents a connection to another
61 * application. Messages can be sent and received via this connection.
62 * The other application may be a message bus; for convenience, the
63 * function dbus_bus_get() is provided to automatically open a
64 * connection to the well-known message buses.
66 * In brief a DBusConnection is a message queue associated with some
67 * message transport mechanism such as a socket. The connection
68 * maintains a queue of incoming messages and a queue of outgoing
71 * Incoming messages are normally processed by calling
72 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
73 * handlers registered for the topmost message in the message queue,
74 * then discards the message, then returns.
76 * dbus_connection_get_dispatch_status() indicates whether
77 * messages are currently in the queue that need dispatching.
78 * dbus_connection_set_dispatch_status_function() allows
79 * you to set a function to be used to monitor the dispatch status.
81 * If you're using GLib or Qt add-on libraries for D-BUS, there are
82 * special convenience APIs in those libraries that hide
83 * all the details of dispatch and watch/timeout monitoring.
84 * For example, dbus_connection_setup_with_g_main().
86 * If you aren't using these add-on libraries, you have to manually
87 * call dbus_connection_set_dispatch_status_function(),
88 * dbus_connection_set_watch_functions(),
89 * dbus_connection_set_timeout_functions() providing appropriate
90 * functions to integrate the connection with your application's main
93 * When you use dbus_connection_send() or one of its variants to send
94 * a message, the message is added to the outgoing queue. It's
95 * actually written to the network later; either in
96 * dbus_watch_handle() invoked by your main loop, or in
97 * dbus_connection_flush() which blocks until it can write out the
98 * entire outgoing queue. The GLib/Qt add-on libraries again
99 * handle the details here for you by setting up watch functions.
101 * When a connection is disconnected, you are guaranteed to get a
102 * signal "Disconnected" from the interface
103 * #DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, path
104 * #DBUS_PATH_ORG_FREEDESKTOP_LOCAL.
106 * You may not drop the last reference to a #DBusConnection
107 * until that connection has been disconnected.
109 * You may dispatch the unprocessed incoming message queue even if the
110 * connection is disconnected. However, "Disconnected" will always be
111 * the last message in the queue (obviously no messages are received
112 * after disconnection).
114 * #DBusConnection has thread locks and drops them when invoking user
115 * callbacks, so in general is transparently threadsafe. However,
116 * #DBusMessage does NOT have thread locks; you must not send the same
117 * message to multiple #DBusConnection that will be used from
122 * @defgroup DBusConnectionInternals DBusConnection implementation details
123 * @ingroup DBusInternals
124 * @brief Implementation details of DBusConnection
130 * Internal struct representing a message filter function
132 typedef struct DBusMessageFilter DBusMessageFilter;
135 * Internal struct representing a message filter function
137 struct DBusMessageFilter
139 DBusAtomic refcount; /**< Reference count */
140 DBusHandleMessageFunction function; /**< Function to call to filter */
141 void *user_data; /**< User data for the function */
142 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
147 * Internals of DBusPreallocatedSend
149 struct DBusPreallocatedSend
151 DBusConnection *connection; /**< Connection we'd send the message to */
152 DBusList *queue_link; /**< Preallocated link in the queue */
153 DBusList *counter_link; /**< Preallocated link in the resource counter */
156 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
159 * Implementation details of DBusConnection. All fields are private.
161 struct DBusConnection
163 DBusAtomic refcount; /**< Reference count. */
165 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
167 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */
168 DBusCondVar *dispatch_cond; /**< Protects dispatch() */
170 dbus_bool_t io_path_acquired; /**< Protects transport io path */
171 DBusCondVar *io_path_cond; /**< Protects transport io path */
173 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
174 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
176 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
177 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
179 int n_outgoing; /**< Length of outgoing queue. */
180 int n_incoming; /**< Length of incoming queue. */
182 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
184 DBusTransport *transport; /**< Object that sends/receives messages over network. */
185 DBusWatchList *watches; /**< Stores active watches. */
186 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
188 DBusList *filter_list; /**< List of filters. */
190 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
192 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
194 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
195 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
197 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
198 void *wakeup_main_data; /**< Application data for wakeup_main_function */
199 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
201 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
202 void *dispatch_status_data; /**< Application data for dispatch_status_function */
203 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
205 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
207 DBusList *link_cache; /**< A cache of linked list links to prevent contention
208 * for the global linked list mempool lock
210 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
212 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
215 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
216 DBusTimeout *timeout);
217 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
218 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
219 DBusDispatchStatus new_status);
220 static void _dbus_connection_last_unref (DBusConnection *connection);
223 _dbus_message_filter_ref (DBusMessageFilter *filter)
225 _dbus_assert (filter->refcount.value > 0);
226 _dbus_atomic_inc (&filter->refcount);
230 _dbus_message_filter_unref (DBusMessageFilter *filter)
232 _dbus_assert (filter->refcount.value > 0);
234 if (_dbus_atomic_dec (&filter->refcount) == 1)
236 if (filter->free_user_data_function)
237 (* filter->free_user_data_function) (filter->user_data);
244 * Acquires the connection lock.
246 * @param connection the connection.
249 _dbus_connection_lock (DBusConnection *connection)
251 CONNECTION_LOCK (connection);
255 * Releases the connection lock.
257 * @param connection the connection.
260 _dbus_connection_unlock (DBusConnection *connection)
262 CONNECTION_UNLOCK (connection);
266 * Wakes up the main loop if it is sleeping
267 * Needed if we're e.g. queueing outgoing messages
268 * on a thread while the mainloop sleeps.
270 * @param connection the connection.
273 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
275 if (connection->wakeup_main_function)
276 (*connection->wakeup_main_function) (connection->wakeup_main_data);
279 #ifdef DBUS_BUILD_TESTS
280 /* For now this function isn't used */
282 * Adds a message to the incoming message queue, returning #FALSE
283 * if there's insufficient memory to queue the message.
284 * Does not take over refcount of the message.
286 * @param connection the connection.
287 * @param message the message to queue.
288 * @returns #TRUE on success.
291 _dbus_connection_queue_received_message (DBusConnection *connection,
292 DBusMessage *message)
296 link = _dbus_list_alloc_link (message);
300 dbus_message_ref (message);
301 _dbus_connection_queue_received_message_link (connection, link);
308 * Adds a message-containing list link to the incoming message queue,
309 * taking ownership of the link and the message's current refcount.
310 * Cannot fail due to lack of memory.
312 * @param connection the connection.
313 * @param link the message link to queue.
316 _dbus_connection_queue_received_message_link (DBusConnection *connection,
319 DBusPendingCall *pending;
320 dbus_int32_t reply_serial;
321 DBusMessage *message;
323 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
325 _dbus_list_append_link (&connection->incoming_messages,
327 message = link->data;
329 /* If this is a reply we're waiting on, remove timeout for it */
330 reply_serial = dbus_message_get_reply_serial (message);
331 if (reply_serial != -1)
333 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
337 if (pending->timeout_added)
338 _dbus_connection_remove_timeout_locked (connection,
341 pending->timeout_added = FALSE;
345 connection->n_incoming += 1;
347 _dbus_connection_wakeup_mainloop (connection);
349 _dbus_verbose ("Message %p (%d %s '%s') added to incoming queue %p, %d incoming\n",
351 dbus_message_get_type (message),
352 dbus_message_get_interface (message) ?
353 dbus_message_get_interface (message) :
355 dbus_message_get_signature (message),
357 connection->n_incoming);
361 * Adds a link + message to the incoming message queue.
362 * Can't fail. Takes ownership of both link and message.
364 * @param connection the connection.
365 * @param link the list node and message to queue.
367 * @todo This needs to wake up the mainloop if it is in
368 * a poll/select and this is a multithreaded app.
371 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
374 _dbus_list_append_link (&connection->incoming_messages, link);
376 connection->n_incoming += 1;
378 _dbus_connection_wakeup_mainloop (connection);
380 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
381 link->data, connection, connection->n_incoming);
386 * Checks whether there are messages in the outgoing message queue.
388 * @param connection the connection.
389 * @returns #TRUE if the outgoing queue is non-empty.
392 _dbus_connection_have_messages_to_send (DBusConnection *connection)
394 return connection->outgoing_messages != NULL;
398 * Gets the next outgoing message. The message remains in the
399 * queue, and the caller does not own a reference to it.
401 * @param connection the connection.
402 * @returns the message to be sent.
405 _dbus_connection_get_message_to_send (DBusConnection *connection)
407 return _dbus_list_get_last (&connection->outgoing_messages);
411 * Notifies the connection that a message has been sent, so the
412 * message can be removed from the outgoing queue.
413 * Called with the connection lock held.
415 * @param connection the connection.
416 * @param message the message that was sent.
419 _dbus_connection_message_sent (DBusConnection *connection,
420 DBusMessage *message)
424 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
426 link = _dbus_list_get_last_link (&connection->outgoing_messages);
427 _dbus_assert (link != NULL);
428 _dbus_assert (link->data == message);
430 /* Save this link in the link cache */
431 _dbus_list_unlink (&connection->outgoing_messages,
433 _dbus_list_prepend_link (&connection->link_cache, link);
435 connection->n_outgoing -= 1;
437 _dbus_verbose ("Message %p (%d %s '%s') removed from outgoing queue %p, %d left to send\n",
439 dbus_message_get_type (message),
440 dbus_message_get_interface (message) ?
441 dbus_message_get_interface (message) :
443 dbus_message_get_signature (message),
444 connection, connection->n_outgoing);
446 /* Save this link in the link cache also */
447 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
449 _dbus_list_prepend_link (&connection->link_cache, link);
451 dbus_message_unref (message);
453 if (connection->n_outgoing == 0)
454 _dbus_transport_messages_pending (connection->transport,
455 connection->n_outgoing);
459 * Adds a watch using the connection's DBusAddWatchFunction if
460 * available. Otherwise records the watch to be added when said
461 * function is available. Also re-adds the watch if the
462 * DBusAddWatchFunction changes. May fail due to lack of memory.
464 * @param connection the connection.
465 * @param watch the watch to add.
466 * @returns #TRUE on success.
469 _dbus_connection_add_watch (DBusConnection *connection,
472 if (connection->watches) /* null during finalize */
473 return _dbus_watch_list_add_watch (connection->watches,
480 * Removes a watch using the connection's DBusRemoveWatchFunction
481 * if available. It's an error to call this function on a watch
482 * that was not previously added.
484 * @param connection the connection.
485 * @param watch the watch to remove.
488 _dbus_connection_remove_watch (DBusConnection *connection,
491 if (connection->watches) /* null during finalize */
492 _dbus_watch_list_remove_watch (connection->watches,
497 * Toggles a watch and notifies app via connection's
498 * DBusWatchToggledFunction if available. It's an error to call this
499 * function on a watch that was not previously added.
501 * @param connection the connection.
502 * @param watch the watch to toggle.
503 * @param enabled whether to enable or disable
506 _dbus_connection_toggle_watch (DBusConnection *connection,
510 if (connection->watches) /* null during finalize */
511 _dbus_watch_list_toggle_watch (connection->watches,
516 * Adds a timeout using the connection's DBusAddTimeoutFunction if
517 * available. Otherwise records the timeout to be added when said
518 * function is available. Also re-adds the timeout if the
519 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
520 * The timeout will fire repeatedly until removed.
522 * @param connection the connection.
523 * @param timeout the timeout to add.
524 * @returns #TRUE on success.
527 _dbus_connection_add_timeout (DBusConnection *connection,
528 DBusTimeout *timeout)
530 if (connection->timeouts) /* null during finalize */
531 return _dbus_timeout_list_add_timeout (connection->timeouts,
538 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
539 * if available. It's an error to call this function on a timeout
540 * that was not previously added.
542 * @param connection the connection.
543 * @param timeout the timeout to remove.
546 _dbus_connection_remove_timeout (DBusConnection *connection,
547 DBusTimeout *timeout)
549 if (connection->timeouts) /* null during finalize */
550 _dbus_timeout_list_remove_timeout (connection->timeouts,
555 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
556 DBusTimeout *timeout)
558 CONNECTION_LOCK (connection);
559 _dbus_connection_remove_timeout (connection, timeout);
560 CONNECTION_UNLOCK (connection);
564 * Toggles a timeout and notifies app via connection's
565 * DBusTimeoutToggledFunction if available. It's an error to call this
566 * function on a timeout that was not previously added.
568 * @param connection the connection.
569 * @param timeout the timeout to toggle.
570 * @param enabled whether to enable or disable
573 _dbus_connection_toggle_timeout (DBusConnection *connection,
574 DBusTimeout *timeout,
577 if (connection->timeouts) /* null during finalize */
578 _dbus_timeout_list_toggle_timeout (connection->timeouts,
583 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
584 DBusPendingCall *pending)
586 _dbus_assert (pending->reply_serial != 0);
588 if (!_dbus_connection_add_timeout (connection, pending->timeout))
591 if (!_dbus_hash_table_insert_int (connection->pending_replies,
592 pending->reply_serial,
595 _dbus_connection_remove_timeout (connection, pending->timeout);
599 pending->timeout_added = TRUE;
600 pending->connection = connection;
602 dbus_pending_call_ref (pending);
608 free_pending_call_on_hash_removal (void *data)
610 DBusPendingCall *pending;
617 if (pending->connection)
619 if (pending->timeout_added)
621 _dbus_connection_remove_timeout (pending->connection,
623 pending->timeout_added = FALSE;
626 pending->connection = NULL;
628 dbus_pending_call_unref (pending);
633 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
634 DBusPendingCall *pending)
636 /* The idea here is to avoid finalizing the pending call
637 * with the lock held, since there's a destroy notifier
638 * in pending call that goes out to application code.
640 dbus_pending_call_ref (pending);
641 _dbus_hash_table_remove_int (connection->pending_replies,
642 pending->reply_serial);
643 CONNECTION_UNLOCK (connection);
644 dbus_pending_call_unref (pending);
648 * Removes a pending call from the connection, such that
649 * the pending reply will be ignored. May drop the last
650 * reference to the pending call.
652 * @param connection the connection
653 * @param pending the pending call
656 _dbus_connection_remove_pending_call (DBusConnection *connection,
657 DBusPendingCall *pending)
659 CONNECTION_LOCK (connection);
660 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
664 * Completes a pending call with the given message,
665 * or if the message is #NULL, by timing out the pending call.
667 * @param pending the pending call
668 * @param message the message to complete the call with, or #NULL
669 * to time out the call
672 _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
673 DBusMessage *message)
677 message = pending->timeout_link->data;
678 _dbus_list_clear (&pending->timeout_link);
681 dbus_message_ref (message);
683 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
685 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
687 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
688 "error" : "other type",
689 pending->reply_serial);
691 _dbus_assert (pending->reply == NULL);
692 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
693 pending->reply = message;
695 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
696 _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending);
698 /* Must be called unlocked since it invokes app callback */
699 _dbus_pending_call_notify (pending);
700 dbus_pending_call_unref (pending);
704 * Acquire the transporter I/O path. This must be done before
705 * doing any I/O in the transporter. May sleep and drop the
706 * connection mutex while waiting for the I/O path.
708 * @param connection the connection.
709 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
710 * @returns TRUE if the I/O path was acquired.
713 _dbus_connection_acquire_io_path (DBusConnection *connection,
714 int timeout_milliseconds)
716 dbus_bool_t res = TRUE;
718 if (connection->io_path_acquired)
720 if (timeout_milliseconds != -1)
721 res = dbus_condvar_wait_timeout (connection->io_path_cond,
723 timeout_milliseconds);
725 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
730 _dbus_assert (!connection->io_path_acquired);
732 connection->io_path_acquired = TRUE;
739 * Release the I/O path when you're done with it. Only call
740 * after you've acquired the I/O. Wakes up at most one thread
741 * currently waiting to acquire the I/O path.
743 * @param connection the connection.
746 _dbus_connection_release_io_path (DBusConnection *connection)
748 _dbus_assert (connection->io_path_acquired);
750 connection->io_path_acquired = FALSE;
751 dbus_condvar_wake_one (connection->io_path_cond);
756 * Queues incoming messages and sends outgoing messages for this
757 * connection, optionally blocking in the process. Each call to
758 * _dbus_connection_do_iteration() will call select() or poll() one
759 * time and then read or write data if possible.
761 * The purpose of this function is to be able to flush outgoing
762 * messages or queue up incoming messages without returning
763 * control to the application and causing reentrancy weirdness.
765 * The flags parameter allows you to specify whether to
766 * read incoming messages, write outgoing messages, or both,
767 * and whether to block if no immediate action is possible.
769 * The timeout_milliseconds parameter does nothing unless the
770 * iteration is blocking.
772 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
773 * wasn't specified, then it's impossible to block, even if
774 * you specify DBUS_ITERATION_BLOCK; in that case the function
775 * returns immediately.
777 * @param connection the connection.
778 * @param flags iteration flags.
779 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
782 _dbus_connection_do_iteration (DBusConnection *connection,
784 int timeout_milliseconds)
786 if (connection->n_outgoing == 0)
787 flags &= ~DBUS_ITERATION_DO_WRITING;
789 if (_dbus_connection_acquire_io_path (connection,
790 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
792 _dbus_transport_do_iteration (connection->transport,
793 flags, timeout_milliseconds);
794 _dbus_connection_release_io_path (connection);
799 * Creates a new connection for the given transport. A transport
800 * represents a message stream that uses some concrete mechanism, such
801 * as UNIX domain sockets. May return #NULL if insufficient
802 * memory exists to create the connection.
804 * @param transport the transport.
805 * @returns the new connection, or #NULL on failure.
808 _dbus_connection_new_for_transport (DBusTransport *transport)
810 DBusConnection *connection;
811 DBusWatchList *watch_list;
812 DBusTimeoutList *timeout_list;
813 DBusHashTable *pending_replies;
815 DBusCondVar *message_returned_cond;
816 DBusCondVar *dispatch_cond;
817 DBusCondVar *io_path_cond;
818 DBusList *disconnect_link;
819 DBusMessage *disconnect_message;
820 DBusCounter *outgoing_counter;
821 DBusObjectTree *objects;
825 pending_replies = NULL;
828 message_returned_cond = NULL;
829 dispatch_cond = NULL;
831 disconnect_link = NULL;
832 disconnect_message = NULL;
833 outgoing_counter = NULL;
836 watch_list = _dbus_watch_list_new ();
837 if (watch_list == NULL)
840 timeout_list = _dbus_timeout_list_new ();
841 if (timeout_list == NULL)
845 _dbus_hash_table_new (DBUS_HASH_INT,
847 (DBusFreeFunction)free_pending_call_on_hash_removal);
848 if (pending_replies == NULL)
851 connection = dbus_new0 (DBusConnection, 1);
852 if (connection == NULL)
855 mutex = dbus_mutex_new ();
859 message_returned_cond = dbus_condvar_new ();
860 if (message_returned_cond == NULL)
863 dispatch_cond = dbus_condvar_new ();
864 if (dispatch_cond == NULL)
867 io_path_cond = dbus_condvar_new ();
868 if (io_path_cond == NULL)
871 disconnect_message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_LOCAL,
872 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
875 if (disconnect_message == NULL)
878 disconnect_link = _dbus_list_alloc_link (disconnect_message);
879 if (disconnect_link == NULL)
882 outgoing_counter = _dbus_counter_new ();
883 if (outgoing_counter == NULL)
886 objects = _dbus_object_tree_new (connection);
890 if (_dbus_modify_sigpipe)
891 _dbus_disable_sigpipe ();
893 connection->refcount.value = 1;
894 connection->mutex = mutex;
895 connection->dispatch_cond = dispatch_cond;
896 connection->io_path_cond = io_path_cond;
897 connection->message_returned_cond = message_returned_cond;
898 connection->transport = transport;
899 connection->watches = watch_list;
900 connection->timeouts = timeout_list;
901 connection->pending_replies = pending_replies;
902 connection->outgoing_counter = outgoing_counter;
903 connection->filter_list = NULL;
904 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
905 connection->objects = objects;
906 connection->exit_on_disconnect = FALSE;
908 _dbus_data_slot_list_init (&connection->slot_list);
910 connection->client_serial = 1;
912 connection->disconnect_message_link = disconnect_link;
914 if (!_dbus_transport_set_connection (transport, connection))
917 _dbus_transport_ref (transport);
922 if (disconnect_message != NULL)
923 dbus_message_unref (disconnect_message);
925 if (disconnect_link != NULL)
926 _dbus_list_free_link (disconnect_link);
928 if (io_path_cond != NULL)
929 dbus_condvar_free (io_path_cond);
931 if (dispatch_cond != NULL)
932 dbus_condvar_free (dispatch_cond);
934 if (message_returned_cond != NULL)
935 dbus_condvar_free (message_returned_cond);
938 dbus_mutex_free (mutex);
940 if (connection != NULL)
941 dbus_free (connection);
944 _dbus_hash_table_unref (pending_replies);
947 _dbus_watch_list_free (watch_list);
950 _dbus_timeout_list_free (timeout_list);
952 if (outgoing_counter)
953 _dbus_counter_unref (outgoing_counter);
956 _dbus_object_tree_unref (objects);
962 * Increments the reference count of a DBusConnection.
963 * Requires that the caller already holds the connection lock.
965 * @param connection the connection.
968 _dbus_connection_ref_unlocked (DBusConnection *connection)
970 #ifdef DBUS_HAVE_ATOMIC_INT
971 _dbus_atomic_inc (&connection->refcount);
973 _dbus_assert (connection->refcount.value > 0);
974 connection->refcount.value += 1;
979 * Decrements the reference count of a DBusConnection.
980 * Requires that the caller already holds the connection lock.
982 * @param connection the connection.
985 _dbus_connection_unref_unlocked (DBusConnection *connection)
987 dbus_bool_t last_unref;
989 _dbus_return_if_fail (connection != NULL);
991 /* The connection lock is better than the global
992 * lock in the atomic increment fallback
995 #ifdef DBUS_HAVE_ATOMIC_INT
996 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
998 _dbus_assert (connection->refcount.value > 0);
1000 connection->refcount.value -= 1;
1001 last_unref = (connection->refcount.value == 0);
1003 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1008 _dbus_connection_last_unref (connection);
1011 static dbus_uint32_t
1012 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1016 serial = connection->client_serial++;
1018 if (connection->client_serial < 0)
1019 connection->client_serial = 1;
1025 * A callback for use with dbus_watch_new() to create a DBusWatch.
1027 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1028 * and the virtual handle_watch in DBusTransport if we got rid of it.
1029 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1032 * @param watch the watch.
1033 * @param condition the current condition of the file descriptors being watched.
1034 * @param data must be a pointer to a #DBusConnection
1035 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1038 _dbus_connection_handle_watch (DBusWatch *watch,
1039 unsigned int condition,
1042 DBusConnection *connection;
1044 DBusDispatchStatus status;
1048 CONNECTION_LOCK (connection);
1049 _dbus_connection_acquire_io_path (connection, -1);
1050 retval = _dbus_transport_handle_watch (connection->transport,
1052 _dbus_connection_release_io_path (connection);
1054 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1056 /* this calls out to user code */
1057 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1065 * @addtogroup DBusConnection
1071 * Opens a new connection to a remote address.
1073 * @todo specify what the address parameter is. Right now
1074 * it's just the name of a UNIX domain socket. It should be
1075 * something more complex that encodes which transport to use.
1077 * If the open fails, the function returns #NULL, and provides
1078 * a reason for the failure in the result parameter. Pass
1079 * #NULL for the result parameter if you aren't interested
1080 * in the reason for failure.
1082 * @param address the address.
1083 * @param error address where an error can be returned.
1084 * @returns new connection, or #NULL on failure.
1087 dbus_connection_open (const char *address,
1090 DBusConnection *connection;
1091 DBusTransport *transport;
1093 _dbus_return_val_if_fail (address != NULL, NULL);
1094 _dbus_return_val_if_error_is_set (error, NULL);
1096 transport = _dbus_transport_open (address, error);
1097 if (transport == NULL)
1099 _DBUS_ASSERT_ERROR_IS_SET (error);
1103 connection = _dbus_connection_new_for_transport (transport);
1105 _dbus_transport_unref (transport);
1107 if (connection == NULL)
1109 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1117 * Increments the reference count of a DBusConnection.
1119 * @param connection the connection.
1122 dbus_connection_ref (DBusConnection *connection)
1124 _dbus_return_if_fail (connection != NULL);
1126 /* The connection lock is better than the global
1127 * lock in the atomic increment fallback
1130 #ifdef DBUS_HAVE_ATOMIC_INT
1131 _dbus_atomic_inc (&connection->refcount);
1133 CONNECTION_LOCK (connection);
1134 _dbus_assert (connection->refcount.value > 0);
1136 connection->refcount.value += 1;
1137 CONNECTION_UNLOCK (connection);
1142 free_outgoing_message (void *element,
1145 DBusMessage *message = element;
1146 DBusConnection *connection = data;
1148 _dbus_message_remove_size_counter (message,
1149 connection->outgoing_counter,
1151 dbus_message_unref (message);
1154 /* This is run without the mutex held, but after the last reference
1155 * to the connection has been dropped we should have no thread-related
1159 _dbus_connection_last_unref (DBusConnection *connection)
1163 _dbus_verbose ("Finalizing connection %p\n", connection);
1165 _dbus_assert (connection->refcount.value == 0);
1167 /* You have to disconnect the connection before unref:ing it. Otherwise
1168 * you won't get the disconnected message.
1170 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1172 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1173 _dbus_object_tree_free_all_unlocked (connection->objects);
1175 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1176 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1177 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1179 _dbus_watch_list_free (connection->watches);
1180 connection->watches = NULL;
1182 _dbus_timeout_list_free (connection->timeouts);
1183 connection->timeouts = NULL;
1185 _dbus_data_slot_list_free (&connection->slot_list);
1187 link = _dbus_list_get_first_link (&connection->filter_list);
1188 while (link != NULL)
1190 DBusMessageFilter *filter = link->data;
1191 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1193 filter->function = NULL;
1194 _dbus_message_filter_unref (filter); /* calls app callback */
1199 _dbus_list_clear (&connection->filter_list);
1201 /* ---- Done with stuff that invokes application callbacks */
1203 _dbus_object_tree_unref (connection->objects);
1205 _dbus_hash_table_unref (connection->pending_replies);
1206 connection->pending_replies = NULL;
1208 _dbus_list_clear (&connection->filter_list);
1210 _dbus_list_foreach (&connection->outgoing_messages,
1211 free_outgoing_message,
1213 _dbus_list_clear (&connection->outgoing_messages);
1215 _dbus_list_foreach (&connection->incoming_messages,
1216 (DBusForeachFunction) dbus_message_unref,
1218 _dbus_list_clear (&connection->incoming_messages);
1220 _dbus_counter_unref (connection->outgoing_counter);
1222 _dbus_transport_unref (connection->transport);
1224 if (connection->disconnect_message_link)
1226 DBusMessage *message = connection->disconnect_message_link->data;
1227 dbus_message_unref (message);
1228 _dbus_list_free_link (connection->disconnect_message_link);
1231 _dbus_list_clear (&connection->link_cache);
1233 dbus_condvar_free (connection->dispatch_cond);
1234 dbus_condvar_free (connection->io_path_cond);
1235 dbus_condvar_free (connection->message_returned_cond);
1237 dbus_mutex_free (connection->mutex);
1239 dbus_free (connection);
1243 * Decrements the reference count of a DBusConnection, and finalizes
1244 * it if the count reaches zero. It is a bug to drop the last reference
1245 * to a connection that has not been disconnected.
1247 * @todo in practice it can be quite tricky to never unref a connection
1248 * that's still connected; maybe there's some way we could avoid
1251 * @param connection the connection.
1254 dbus_connection_unref (DBusConnection *connection)
1256 dbus_bool_t last_unref;
1258 _dbus_return_if_fail (connection != NULL);
1260 /* The connection lock is better than the global
1261 * lock in the atomic increment fallback
1264 #ifdef DBUS_HAVE_ATOMIC_INT
1265 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1267 CONNECTION_LOCK (connection);
1269 _dbus_assert (connection->refcount.value > 0);
1271 connection->refcount.value -= 1;
1272 last_unref = (connection->refcount.value == 0);
1275 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1278 CONNECTION_UNLOCK (connection);
1282 _dbus_connection_last_unref (connection);
1286 * Closes the connection, so no further data can be sent or received.
1287 * Any further attempts to send data will result in errors. This
1288 * function does not affect the connection's reference count. It's
1289 * safe to disconnect a connection more than once; all calls after the
1290 * first do nothing. It's impossible to "reconnect" a connection, a
1291 * new connection must be created. This function may result in a call
1292 * to the DBusDispatchStatusFunction set with
1293 * dbus_connection_set_dispatch_status_function(), as the disconnect
1294 * message it generates needs to be dispatched.
1296 * @param connection the connection.
1299 dbus_connection_disconnect (DBusConnection *connection)
1301 DBusDispatchStatus status;
1303 _dbus_return_if_fail (connection != NULL);
1305 CONNECTION_LOCK (connection);
1306 _dbus_transport_disconnect (connection->transport);
1308 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1310 /* this calls out to user code */
1311 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1315 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1317 return _dbus_transport_get_is_connected (connection->transport);
1321 * Gets whether the connection is currently connected. All
1322 * connections are connected when they are opened. A connection may
1323 * become disconnected when the remote application closes its end, or
1324 * exits; a connection may also be disconnected with
1325 * dbus_connection_disconnect().
1327 * @param connection the connection.
1328 * @returns #TRUE if the connection is still alive.
1331 dbus_connection_get_is_connected (DBusConnection *connection)
1335 _dbus_return_val_if_fail (connection != NULL, FALSE);
1337 CONNECTION_LOCK (connection);
1338 res = _dbus_connection_get_is_connected_unlocked (connection);
1339 CONNECTION_UNLOCK (connection);
1345 * Gets whether the connection was authenticated. (Note that
1346 * if the connection was authenticated then disconnected,
1347 * this function still returns #TRUE)
1349 * @param connection the connection
1350 * @returns #TRUE if the connection was ever authenticated
1353 dbus_connection_get_is_authenticated (DBusConnection *connection)
1357 _dbus_return_val_if_fail (connection != NULL, FALSE);
1359 CONNECTION_LOCK (connection);
1360 res = _dbus_transport_get_is_authenticated (connection->transport);
1361 CONNECTION_UNLOCK (connection);
1367 * Set whether _exit() should be called when the connection receives a
1368 * disconnect signal. The call to _exit() comes after any handlers for
1369 * the disconnect signal run; handlers can cancel the exit by calling
1372 * By default, exit_on_disconnect is #FALSE; but for message bus
1373 * connections returned from dbus_bus_get() it will be toggled on
1376 * @param connection the connection
1377 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal
1380 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
1381 dbus_bool_t exit_on_disconnect)
1383 _dbus_return_if_fail (connection != NULL);
1385 CONNECTION_LOCK (connection);
1386 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
1387 CONNECTION_UNLOCK (connection);
1390 static DBusPreallocatedSend*
1391 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1393 DBusPreallocatedSend *preallocated;
1395 _dbus_return_val_if_fail (connection != NULL, NULL);
1397 preallocated = dbus_new (DBusPreallocatedSend, 1);
1398 if (preallocated == NULL)
1401 if (connection->link_cache != NULL)
1403 preallocated->queue_link =
1404 _dbus_list_pop_first_link (&connection->link_cache);
1405 preallocated->queue_link->data = NULL;
1409 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1410 if (preallocated->queue_link == NULL)
1414 if (connection->link_cache != NULL)
1416 preallocated->counter_link =
1417 _dbus_list_pop_first_link (&connection->link_cache);
1418 preallocated->counter_link->data = connection->outgoing_counter;
1422 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1423 if (preallocated->counter_link == NULL)
1427 _dbus_counter_ref (preallocated->counter_link->data);
1429 preallocated->connection = connection;
1431 return preallocated;
1434 _dbus_list_free_link (preallocated->queue_link);
1436 dbus_free (preallocated);
1442 * Preallocates resources needed to send a message, allowing the message
1443 * to be sent without the possibility of memory allocation failure.
1444 * Allows apps to create a future guarantee that they can send
1445 * a message regardless of memory shortages.
1447 * @param connection the connection we're preallocating for.
1448 * @returns the preallocated resources, or #NULL
1450 DBusPreallocatedSend*
1451 dbus_connection_preallocate_send (DBusConnection *connection)
1453 DBusPreallocatedSend *preallocated;
1455 _dbus_return_val_if_fail (connection != NULL, NULL);
1457 CONNECTION_LOCK (connection);
1460 _dbus_connection_preallocate_send_unlocked (connection);
1462 CONNECTION_UNLOCK (connection);
1464 return preallocated;
1468 * Frees preallocated message-sending resources from
1469 * dbus_connection_preallocate_send(). Should only
1470 * be called if the preallocated resources are not used
1471 * to send a message.
1473 * @param connection the connection
1474 * @param preallocated the resources
1477 dbus_connection_free_preallocated_send (DBusConnection *connection,
1478 DBusPreallocatedSend *preallocated)
1480 _dbus_return_if_fail (connection != NULL);
1481 _dbus_return_if_fail (preallocated != NULL);
1482 _dbus_return_if_fail (connection == preallocated->connection);
1484 _dbus_list_free_link (preallocated->queue_link);
1485 _dbus_counter_unref (preallocated->counter_link->data);
1486 _dbus_list_free_link (preallocated->counter_link);
1487 dbus_free (preallocated);
1491 _dbus_connection_send_preallocated_unlocked (DBusConnection *connection,
1492 DBusPreallocatedSend *preallocated,
1493 DBusMessage *message,
1494 dbus_uint32_t *client_serial)
1496 dbus_uint32_t serial;
1498 preallocated->queue_link->data = message;
1499 _dbus_list_prepend_link (&connection->outgoing_messages,
1500 preallocated->queue_link);
1502 _dbus_message_add_size_counter_link (message,
1503 preallocated->counter_link);
1505 dbus_free (preallocated);
1506 preallocated = NULL;
1508 dbus_message_ref (message);
1510 connection->n_outgoing += 1;
1512 _dbus_verbose ("Message %p (%d %s '%s') added to outgoing queue %p, %d pending to send\n",
1514 dbus_message_get_type (message),
1515 dbus_message_get_interface (message) ?
1516 dbus_message_get_interface (message) :
1518 dbus_message_get_signature (message),
1520 connection->n_outgoing);
1522 if (dbus_message_get_serial (message) == 0)
1524 serial = _dbus_connection_get_next_client_serial (connection);
1525 _dbus_message_set_serial (message, serial);
1527 *client_serial = serial;
1532 *client_serial = dbus_message_get_serial (message);
1535 _dbus_message_lock (message);
1537 if (connection->n_outgoing == 1)
1538 _dbus_transport_messages_pending (connection->transport,
1539 connection->n_outgoing);
1541 _dbus_connection_wakeup_mainloop (connection);
1545 * Sends a message using preallocated resources. This function cannot fail.
1546 * It works identically to dbus_connection_send() in other respects.
1547 * Preallocated resources comes from dbus_connection_preallocate_send().
1548 * This function "consumes" the preallocated resources, they need not
1549 * be freed separately.
1551 * @param connection the connection
1552 * @param preallocated the preallocated resources
1553 * @param message the message to send
1554 * @param client_serial return location for client serial assigned to the message
1557 dbus_connection_send_preallocated (DBusConnection *connection,
1558 DBusPreallocatedSend *preallocated,
1559 DBusMessage *message,
1560 dbus_uint32_t *client_serial)
1562 _dbus_return_if_fail (connection != NULL);
1563 _dbus_return_if_fail (preallocated != NULL);
1564 _dbus_return_if_fail (message != NULL);
1565 _dbus_return_if_fail (preallocated->connection == connection);
1566 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
1567 (dbus_message_get_interface (message) != NULL &&
1568 dbus_message_get_member (message) != NULL));
1569 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
1570 (dbus_message_get_interface (message) != NULL &&
1571 dbus_message_get_member (message) != NULL));
1573 CONNECTION_LOCK (connection);
1574 _dbus_connection_send_preallocated_unlocked (connection,
1576 message, client_serial);
1577 CONNECTION_UNLOCK (connection);
1581 _dbus_connection_send_unlocked (DBusConnection *connection,
1582 DBusMessage *message,
1583 dbus_uint32_t *client_serial)
1585 DBusPreallocatedSend *preallocated;
1587 _dbus_assert (connection != NULL);
1588 _dbus_assert (message != NULL);
1590 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
1591 if (preallocated == NULL)
1595 _dbus_connection_send_preallocated_unlocked (connection,
1603 * Adds a message to the outgoing message queue. Does not block to
1604 * write the message to the network; that happens asynchronously. To
1605 * force the message to be written, call dbus_connection_flush().
1606 * Because this only queues the message, the only reason it can
1607 * fail is lack of memory. Even if the connection is disconnected,
1608 * no error will be returned.
1610 * If the function fails due to lack of memory, it returns #FALSE.
1611 * The function will never fail for other reasons; even if the
1612 * connection is disconnected, you can queue an outgoing message,
1613 * though obviously it won't be sent.
1615 * @param connection the connection.
1616 * @param message the message to write.
1617 * @param client_serial return location for client serial.
1618 * @returns #TRUE on success.
1621 dbus_connection_send (DBusConnection *connection,
1622 DBusMessage *message,
1623 dbus_uint32_t *client_serial)
1625 _dbus_return_val_if_fail (connection != NULL, FALSE);
1626 _dbus_return_val_if_fail (message != NULL, FALSE);
1628 CONNECTION_LOCK (connection);
1630 if (!_dbus_connection_send_unlocked (connection, message, client_serial))
1632 CONNECTION_UNLOCK (connection);
1636 CONNECTION_UNLOCK (connection);
1641 reply_handler_timeout (void *data)
1643 DBusConnection *connection;
1644 DBusDispatchStatus status;
1645 DBusPendingCall *pending = data;
1647 connection = pending->connection;
1649 CONNECTION_LOCK (connection);
1650 if (pending->timeout_link)
1652 _dbus_connection_queue_synthesized_message_link (connection,
1653 pending->timeout_link);
1654 pending->timeout_link = NULL;
1657 _dbus_connection_remove_timeout (connection,
1659 pending->timeout_added = FALSE;
1661 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1663 /* Unlocks, and calls out to user code */
1664 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1670 * Queues a message to send, as with dbus_connection_send_message(),
1671 * but also returns a #DBusPendingCall used to receive a reply to the
1672 * message. If no reply is received in the given timeout_milliseconds,
1673 * this function expires the pending reply and generates a synthetic
1674 * error reply (generated in-process, not by the remote application)
1675 * indicating that a timeout occurred.
1677 * A #DBusPendingCall will see a reply message after any filters, but
1678 * before any object instances or other handlers. A #DBusPendingCall
1679 * will always see exactly one reply message, unless it's cancelled
1680 * with dbus_pending_call_cancel().
1682 * If a filter filters out the reply before the handler sees it, the
1683 * reply is immediately timed out and a timeout error reply is
1684 * generated. If a filter removes the timeout error reply then the
1685 * #DBusPendingCall will get confused. Filtering the timeout error
1686 * is thus considered a bug and will print a warning.
1688 * If #NULL is passed for the pending_return, the #DBusPendingCall
1689 * will still be generated internally, and used to track
1690 * the message reply timeout. This means a timeout error will
1691 * occur if no reply arrives, unlike with dbus_connection_send().
1693 * If -1 is passed for the timeout, a sane default timeout is used. -1
1694 * is typically the best value for the timeout for this reason, unless
1695 * you want a very short or very long timeout. There is no way to
1696 * avoid a timeout entirely, other than passing INT_MAX for the
1697 * timeout to postpone it indefinitely.
1699 * @param connection the connection
1700 * @param message the message to send
1701 * @param pending_return return location for a #DBusPendingCall object, or #NULL
1702 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1703 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1707 dbus_connection_send_with_reply (DBusConnection *connection,
1708 DBusMessage *message,
1709 DBusPendingCall **pending_return,
1710 int timeout_milliseconds)
1712 DBusPendingCall *pending;
1714 DBusList *reply_link;
1715 dbus_int32_t serial = -1;
1717 _dbus_return_val_if_fail (connection != NULL, FALSE);
1718 _dbus_return_val_if_fail (message != NULL, FALSE);
1719 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1722 *pending_return = NULL;
1724 pending = _dbus_pending_call_new (connection,
1725 timeout_milliseconds,
1726 reply_handler_timeout);
1728 if (pending == NULL)
1731 CONNECTION_LOCK (connection);
1733 /* Assign a serial to the message */
1734 if (dbus_message_get_serial (message) == 0)
1736 serial = _dbus_connection_get_next_client_serial (connection);
1737 _dbus_message_set_serial (message, serial);
1740 pending->reply_serial = serial;
1742 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
1743 "No reply within specified time");
1746 CONNECTION_UNLOCK (connection);
1747 dbus_pending_call_unref (pending);
1751 reply_link = _dbus_list_alloc_link (reply);
1754 CONNECTION_UNLOCK (connection);
1755 dbus_message_unref (reply);
1756 dbus_pending_call_unref (pending);
1760 pending->timeout_link = reply_link;
1762 /* Insert the serial in the pending replies hash;
1763 * hash takes a refcount on DBusPendingCall.
1764 * Also, add the timeout.
1766 if (!_dbus_connection_attach_pending_call_unlocked (connection,
1769 CONNECTION_UNLOCK (connection);
1770 dbus_pending_call_unref (pending);
1774 if (!_dbus_connection_send_unlocked (connection, message, NULL))
1776 _dbus_connection_detach_pending_call_and_unlock (connection,
1783 dbus_pending_call_ref (pending);
1784 *pending_return = pending;
1787 CONNECTION_UNLOCK (connection);
1793 check_for_reply_unlocked (DBusConnection *connection,
1794 dbus_uint32_t client_serial)
1798 link = _dbus_list_get_first_link (&connection->incoming_messages);
1800 while (link != NULL)
1802 DBusMessage *reply = link->data;
1804 if (dbus_message_get_reply_serial (reply) == client_serial)
1806 _dbus_list_remove_link (&connection->incoming_messages, link);
1807 connection->n_incoming -= 1;
1808 dbus_message_ref (reply);
1811 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1818 * Blocks a certain time period while waiting for a reply.
1819 * If no reply arrives, returns #NULL.
1821 * @todo could use performance improvements (it keeps scanning
1822 * the whole message queue for example) and has thread issues,
1823 * see comments in source
1825 * Does not re-enter the main loop or run filter/path-registered
1826 * callbacks. The reply to the message will not be seen by
1829 * @param connection the connection
1830 * @param client_serial the reply serial to wait for
1831 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1832 * @returns the message that is the reply or #NULL if no reply
1835 _dbus_connection_block_for_reply (DBusConnection *connection,
1836 dbus_uint32_t client_serial,
1837 int timeout_milliseconds)
1839 long start_tv_sec, start_tv_usec;
1840 long end_tv_sec, end_tv_usec;
1841 long tv_sec, tv_usec;
1842 DBusDispatchStatus status;
1844 _dbus_return_val_if_fail (connection != NULL, NULL);
1845 _dbus_return_val_if_fail (client_serial != 0, NULL);
1846 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1848 if (timeout_milliseconds == -1)
1849 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
1851 /* it would probably seem logical to pass in _DBUS_INT_MAX
1852 * for infinite timeout, but then math below would get
1853 * all overflow-prone, so smack that down.
1855 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1856 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1858 /* Flush message queue */
1859 dbus_connection_flush (connection);
1861 CONNECTION_LOCK (connection);
1863 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1864 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1865 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1866 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1867 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1869 _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",
1870 timeout_milliseconds,
1872 start_tv_sec, start_tv_usec,
1873 end_tv_sec, end_tv_usec);
1875 /* Now we wait... */
1876 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1877 * gets the message before we do?
1879 /* always block at least once as we know we don't have the reply yet */
1880 _dbus_connection_do_iteration (connection,
1881 DBUS_ITERATION_DO_READING |
1882 DBUS_ITERATION_BLOCK,
1883 timeout_milliseconds);
1887 /* queue messages and get status */
1888 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1890 if (status == DBUS_DISPATCH_DATA_REMAINS)
1894 reply = check_for_reply_unlocked (connection, client_serial);
1897 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1899 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
1901 /* Unlocks, and calls out to user code */
1902 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1908 _dbus_get_current_time (&tv_sec, &tv_usec);
1910 if (tv_sec < start_tv_sec)
1911 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
1912 else if (connection->disconnect_message_link == NULL)
1913 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
1914 else if (tv_sec < end_tv_sec ||
1915 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1917 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1918 (end_tv_usec - tv_usec) / 1000;
1919 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1920 _dbus_assert (timeout_milliseconds >= 0);
1922 if (status == DBUS_DISPATCH_NEED_MEMORY)
1924 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1925 * we may already have a reply in the buffer and just can't process
1928 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1930 if (timeout_milliseconds < 100)
1931 ; /* just busy loop */
1932 else if (timeout_milliseconds <= 1000)
1933 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1935 _dbus_sleep_milliseconds (1000);
1939 /* block again, we don't have the reply buffered yet. */
1940 _dbus_connection_do_iteration (connection,
1941 DBUS_ITERATION_DO_READING |
1942 DBUS_ITERATION_BLOCK,
1943 timeout_milliseconds);
1946 goto recheck_status;
1949 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
1950 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
1952 /* unlocks and calls out to user code */
1953 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1959 * Sends a message and blocks a certain time period while waiting for
1960 * a reply. This function does not reenter the main loop,
1961 * i.e. messages other than the reply are queued up but not
1962 * processed. This function is used to do non-reentrant "method
1965 * If a normal reply is received, it is returned, and removed from the
1966 * incoming message queue. If it is not received, #NULL is returned
1967 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
1968 * received, it is converted to a #DBusError and returned as an error,
1969 * then the reply message is deleted. If something else goes wrong,
1970 * result is set to whatever is appropriate, such as
1971 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1973 * @param connection the connection
1974 * @param message the message to send
1975 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1976 * @param error return location for error message
1977 * @returns the message that is the reply or #NULL with an error code if the
1981 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1982 DBusMessage *message,
1983 int timeout_milliseconds,
1986 dbus_uint32_t client_serial;
1989 _dbus_return_val_if_fail (connection != NULL, NULL);
1990 _dbus_return_val_if_fail (message != NULL, NULL);
1991 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1992 _dbus_return_val_if_error_is_set (error, NULL);
1994 if (!dbus_connection_send (connection, message, &client_serial))
1996 _DBUS_SET_OOM (error);
2000 reply = _dbus_connection_block_for_reply (connection,
2002 timeout_milliseconds);
2006 if (dbus_connection_get_is_connected (connection))
2007 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
2009 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
2013 else if (dbus_set_error_from_message (error, reply))
2015 dbus_message_unref (reply);
2023 * Blocks until the outgoing message queue is empty.
2025 * @param connection the connection.
2028 dbus_connection_flush (DBusConnection *connection)
2030 /* We have to specify DBUS_ITERATION_DO_READING here because
2031 * otherwise we could have two apps deadlock if they are both doing
2032 * a flush(), and the kernel buffers fill up. This could change the
2035 DBusDispatchStatus status;
2037 _dbus_return_if_fail (connection != NULL);
2039 CONNECTION_LOCK (connection);
2040 while (connection->n_outgoing > 0 &&
2041 _dbus_connection_get_is_connected_unlocked (connection))
2042 _dbus_connection_do_iteration (connection,
2043 DBUS_ITERATION_DO_READING |
2044 DBUS_ITERATION_DO_WRITING |
2045 DBUS_ITERATION_BLOCK,
2048 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2050 /* Unlocks and calls out to user code */
2051 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2054 /* Call with mutex held. Will drop it while waiting and re-acquire
2058 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
2060 _dbus_assert (connection->message_borrowed != NULL);
2062 while (connection->message_borrowed != NULL)
2063 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
2067 * Returns the first-received message from the incoming message queue,
2068 * leaving it in the queue. If the queue is empty, returns #NULL.
2070 * The caller does not own a reference to the returned message, and
2071 * must either return it using dbus_connection_return_message() or
2072 * keep it after calling dbus_connection_steal_borrowed_message(). No
2073 * one can get at the message while its borrowed, so return it as
2074 * quickly as possible and don't keep a reference to it after
2075 * returning it. If you need to keep the message, make a copy of it.
2077 * @param connection the connection.
2078 * @returns next message in the incoming queue.
2081 dbus_connection_borrow_message (DBusConnection *connection)
2083 DBusMessage *message;
2084 DBusDispatchStatus status;
2086 _dbus_return_val_if_fail (connection != NULL, NULL);
2087 /* can't borrow during dispatch */
2088 _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL);
2090 /* this is called for the side effect that it queues
2091 * up any messages from the transport
2093 status = dbus_connection_get_dispatch_status (connection);
2094 if (status != DBUS_DISPATCH_DATA_REMAINS)
2097 CONNECTION_LOCK (connection);
2099 if (connection->message_borrowed != NULL)
2100 _dbus_connection_wait_for_borrowed (connection);
2102 message = _dbus_list_get_first (&connection->incoming_messages);
2105 connection->message_borrowed = message;
2107 CONNECTION_UNLOCK (connection);
2112 * Used to return a message after peeking at it using
2113 * dbus_connection_borrow_message().
2115 * @param connection the connection
2116 * @param message the message from dbus_connection_borrow_message()
2119 dbus_connection_return_message (DBusConnection *connection,
2120 DBusMessage *message)
2122 _dbus_return_if_fail (connection != NULL);
2123 _dbus_return_if_fail (message != NULL);
2124 /* can't borrow during dispatch */
2125 _dbus_return_if_fail (!connection->dispatch_acquired);
2127 CONNECTION_LOCK (connection);
2129 _dbus_assert (message == connection->message_borrowed);
2131 connection->message_borrowed = NULL;
2132 dbus_condvar_wake_all (connection->message_returned_cond);
2134 CONNECTION_UNLOCK (connection);
2138 * Used to keep a message after peeking at it using
2139 * dbus_connection_borrow_message(). Before using this function, see
2140 * the caveats/warnings in the documentation for
2141 * dbus_connection_pop_message().
2143 * @param connection the connection
2144 * @param message the message from dbus_connection_borrow_message()
2147 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2148 DBusMessage *message)
2150 DBusMessage *pop_message;
2152 _dbus_return_if_fail (connection != NULL);
2153 _dbus_return_if_fail (message != NULL);
2154 /* can't borrow during dispatch */
2155 _dbus_return_if_fail (!connection->dispatch_acquired);
2157 CONNECTION_LOCK (connection);
2159 _dbus_assert (message == connection->message_borrowed);
2161 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
2162 _dbus_assert (message == pop_message);
2164 connection->n_incoming -= 1;
2166 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
2167 message, connection->n_incoming);
2169 connection->message_borrowed = NULL;
2170 dbus_condvar_wake_all (connection->message_returned_cond);
2172 CONNECTION_UNLOCK (connection);
2175 /* See dbus_connection_pop_message, but requires the caller to own
2176 * the lock before calling. May drop the lock while running.
2179 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
2181 if (connection->message_borrowed != NULL)
2182 _dbus_connection_wait_for_borrowed (connection);
2184 if (connection->n_incoming > 0)
2188 link = _dbus_list_pop_first_link (&connection->incoming_messages);
2189 connection->n_incoming -= 1;
2191 _dbus_verbose ("Message %p (%d %s '%s') removed from incoming queue %p, %d incoming\n",
2193 dbus_message_get_type (link->data),
2194 dbus_message_get_interface (link->data) ?
2195 dbus_message_get_interface (link->data) :
2197 dbus_message_get_signature (link->data),
2198 connection, connection->n_incoming);
2206 /* See dbus_connection_pop_message, but requires the caller to own
2207 * the lock before calling. May drop the lock while running.
2210 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
2214 link = _dbus_connection_pop_message_link_unlocked (connection);
2218 DBusMessage *message;
2220 message = link->data;
2222 _dbus_list_free_link (link);
2231 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
2232 DBusList *message_link)
2234 _dbus_assert (message_link != NULL);
2235 /* You can't borrow a message while a link is outstanding */
2236 _dbus_assert (connection->message_borrowed == NULL);
2238 _dbus_list_prepend_link (&connection->incoming_messages,
2240 connection->n_incoming += 1;
2242 _dbus_verbose ("Message %p (%d %s '%s') put back into queue %p, %d incoming\n",
2244 dbus_message_get_type (message_link->data),
2245 dbus_message_get_interface (message_link->data) ?
2246 dbus_message_get_interface (message_link->data) :
2248 dbus_message_get_signature (message_link->data),
2249 connection, connection->n_incoming);
2253 * Returns the first-received message from the incoming message queue,
2254 * removing it from the queue. The caller owns a reference to the
2255 * returned message. If the queue is empty, returns #NULL.
2257 * This function bypasses any message handlers that are registered,
2258 * and so using it is usually wrong. Instead, let the main loop invoke
2259 * dbus_connection_dispatch(). Popping messages manually is only
2260 * useful in very simple programs that don't share a #DBusConnection
2261 * with any libraries or other modules.
2263 * @param connection the connection.
2264 * @returns next message in the incoming queue.
2267 dbus_connection_pop_message (DBusConnection *connection)
2269 DBusMessage *message;
2270 DBusDispatchStatus status;
2272 /* this is called for the side effect that it queues
2273 * up any messages from the transport
2275 status = dbus_connection_get_dispatch_status (connection);
2276 if (status != DBUS_DISPATCH_DATA_REMAINS)
2279 CONNECTION_LOCK (connection);
2281 message = _dbus_connection_pop_message_unlocked (connection);
2283 _dbus_verbose ("Returning popped message %p\n", message);
2285 CONNECTION_UNLOCK (connection);
2291 * Acquire the dispatcher. This must be done before dispatching
2292 * messages in order to guarantee the right order of
2293 * message delivery. May sleep and drop the connection mutex
2294 * while waiting for the dispatcher.
2296 * @param connection the connection.
2299 _dbus_connection_acquire_dispatch (DBusConnection *connection)
2301 if (connection->dispatch_acquired)
2302 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
2303 _dbus_assert (!connection->dispatch_acquired);
2305 connection->dispatch_acquired = TRUE;
2309 * Release the dispatcher when you're done with it. Only call
2310 * after you've acquired the dispatcher. Wakes up at most one
2311 * thread currently waiting to acquire the dispatcher.
2313 * @param connection the connection.
2316 _dbus_connection_release_dispatch (DBusConnection *connection)
2318 _dbus_assert (connection->dispatch_acquired);
2320 connection->dispatch_acquired = FALSE;
2321 dbus_condvar_wake_one (connection->dispatch_cond);
2325 _dbus_connection_failed_pop (DBusConnection *connection,
2326 DBusList *message_link)
2328 _dbus_list_prepend_link (&connection->incoming_messages,
2330 connection->n_incoming += 1;
2333 static DBusDispatchStatus
2334 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
2336 if (connection->n_incoming > 0)
2337 return DBUS_DISPATCH_DATA_REMAINS;
2338 else if (!_dbus_transport_queue_messages (connection->transport))
2339 return DBUS_DISPATCH_NEED_MEMORY;
2342 DBusDispatchStatus status;
2344 status = _dbus_transport_get_dispatch_status (connection->transport);
2346 if (status == DBUS_DISPATCH_COMPLETE &&
2347 connection->disconnect_message_link &&
2348 !_dbus_transport_get_is_connected (connection->transport))
2350 /* We haven't sent the disconnect message already,
2351 * and all real messages have been queued up.
2353 _dbus_connection_queue_synthesized_message_link (connection,
2354 connection->disconnect_message_link);
2355 connection->disconnect_message_link = NULL;
2358 if (status != DBUS_DISPATCH_COMPLETE)
2360 else if (connection->n_incoming > 0)
2361 return DBUS_DISPATCH_DATA_REMAINS;
2363 return DBUS_DISPATCH_COMPLETE;
2368 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
2369 DBusDispatchStatus new_status)
2371 dbus_bool_t changed;
2372 DBusDispatchStatusFunction function;
2375 /* We have the lock */
2377 _dbus_connection_ref_unlocked (connection);
2379 changed = new_status != connection->last_dispatch_status;
2381 connection->last_dispatch_status = new_status;
2383 function = connection->dispatch_status_function;
2384 data = connection->dispatch_status_data;
2386 /* We drop the lock */
2387 CONNECTION_UNLOCK (connection);
2389 if (changed && function)
2391 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
2392 connection, new_status,
2393 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
2394 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
2395 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
2397 (* function) (connection, new_status, data);
2400 dbus_connection_unref (connection);
2404 * Gets the current state (what we would currently return
2405 * from dbus_connection_dispatch()) but doesn't actually
2406 * dispatch any messages.
2408 * @param connection the connection.
2409 * @returns current dispatch status
2412 dbus_connection_get_dispatch_status (DBusConnection *connection)
2414 DBusDispatchStatus status;
2416 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2418 CONNECTION_LOCK (connection);
2420 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2422 CONNECTION_UNLOCK (connection);
2428 * Processes data buffered while handling watches, queueing zero or
2429 * more incoming messages. Then pops the first-received message from
2430 * the current incoming message queue, runs any handlers for it, and
2431 * unrefs the message. Returns a status indicating whether messages/data
2432 * remain, more memory is needed, or all data has been processed.
2434 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
2435 * does not necessarily dispatch a message, as the data may
2436 * be part of authentication or the like.
2438 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
2440 * @todo right now a message filter gets run on replies to a pending
2441 * call in here, but not in the case where we block without entering
2442 * the main loop. Simple solution might be to just have the pending
2443 * call stuff run before the filters.
2445 * @todo FIXME what if we call out to application code to handle a
2446 * message, holding the dispatch lock, and the application code runs
2447 * the main loop and dispatches again? Probably deadlocks at the
2448 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
2449 * and then the GSource etc. could handle the situation?
2451 * @param connection the connection
2452 * @returns dispatch status
2455 dbus_connection_dispatch (DBusConnection *connection)
2457 DBusMessage *message;
2458 DBusList *link, *filter_list_copy, *message_link;
2459 DBusHandlerResult result;
2460 DBusPendingCall *pending;
2461 dbus_int32_t reply_serial;
2462 DBusDispatchStatus status;
2464 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2466 CONNECTION_LOCK (connection);
2467 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2468 if (status != DBUS_DISPATCH_DATA_REMAINS)
2470 /* unlocks and calls out to user code */
2471 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2475 /* We need to ref the connection since the callback could potentially
2476 * drop the last ref to it
2478 _dbus_connection_ref_unlocked (connection);
2480 _dbus_connection_acquire_dispatch (connection);
2482 /* This call may drop the lock during the execution (if waiting for
2483 * borrowed messages to be returned) but the order of message
2484 * dispatch if several threads call dispatch() is still
2485 * protected by the lock, since only one will get the lock, and that
2486 * one will finish the message dispatching
2488 message_link = _dbus_connection_pop_message_link_unlocked (connection);
2489 if (message_link == NULL)
2491 /* another thread dispatched our stuff */
2493 _dbus_connection_release_dispatch (connection);
2495 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2497 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2499 dbus_connection_unref (connection);
2504 message = message_link->data;
2506 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2508 reply_serial = dbus_message_get_reply_serial (message);
2509 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
2512 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
2514 _dbus_connection_release_dispatch (connection);
2516 _dbus_connection_failed_pop (connection, message_link);
2518 /* unlocks and calls user code */
2519 _dbus_connection_update_dispatch_status_and_unlock (connection,
2520 DBUS_DISPATCH_NEED_MEMORY);
2522 dbus_connection_unref (connection);
2524 return DBUS_DISPATCH_NEED_MEMORY;
2527 _dbus_list_foreach (&filter_list_copy,
2528 (DBusForeachFunction)_dbus_message_filter_ref,
2531 /* We're still protected from dispatch() reentrancy here
2532 * since we acquired the dispatcher
2534 CONNECTION_UNLOCK (connection);
2536 link = _dbus_list_get_first_link (&filter_list_copy);
2537 while (link != NULL)
2539 DBusMessageFilter *filter = link->data;
2540 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2542 _dbus_verbose (" running filter on message %p\n", message);
2543 result = (* filter->function) (connection, message, filter->user_data);
2545 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2551 _dbus_list_foreach (&filter_list_copy,
2552 (DBusForeachFunction)_dbus_message_filter_unref,
2554 _dbus_list_clear (&filter_list_copy);
2556 CONNECTION_LOCK (connection);
2558 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2561 /* Did a reply we were waiting on get filtered? */
2562 if (pending && result == DBUS_HANDLER_RESULT_HANDLED)
2564 /* Queue the timeout immediately! */
2565 if (pending->timeout_link)
2567 _dbus_connection_queue_synthesized_message_link (connection,
2568 pending->timeout_link);
2569 pending->timeout_link = NULL;
2573 /* We already queued the timeout? Then it was filtered! */
2574 _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial);
2578 if (result == DBUS_HANDLER_RESULT_HANDLED)
2583 _dbus_pending_call_complete_and_unlock (pending, message);
2587 CONNECTION_LOCK (connection);
2591 /* We're still protected from dispatch() reentrancy here
2592 * since we acquired the dispatcher
2594 _dbus_verbose (" running object path dispatch on message %p (%d %s '%s')\n",
2596 dbus_message_get_type (message),
2597 dbus_message_get_interface (message) ?
2598 dbus_message_get_interface (message) :
2600 dbus_message_get_signature (message));
2602 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
2605 CONNECTION_LOCK (connection);
2607 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2610 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
2614 DBusPreallocatedSend *preallocated;
2616 _dbus_verbose (" sending error %s\n",
2617 DBUS_ERROR_UNKNOWN_METHOD);
2619 if (!_dbus_string_init (&str))
2621 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2625 if (!_dbus_string_append_printf (&str,
2626 "Method \"%s\" on interface \"%s\" doesn't exist\n",
2627 dbus_message_get_member (message),
2628 dbus_message_get_interface (message)))
2630 _dbus_string_free (&str);
2631 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2635 reply = dbus_message_new_error (message,
2636 DBUS_ERROR_UNKNOWN_METHOD,
2637 _dbus_string_get_const_data (&str));
2638 _dbus_string_free (&str);
2642 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2646 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2648 if (preallocated == NULL)
2650 dbus_message_unref (reply);
2651 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2655 _dbus_connection_send_preallocated_unlocked (connection, preallocated,
2658 dbus_message_unref (reply);
2660 result = DBUS_HANDLER_RESULT_HANDLED;
2663 _dbus_verbose (" done dispatching %p (%d %s '%s') on connection %p\n", message,
2664 dbus_message_get_type (message),
2665 dbus_message_get_interface (message) ?
2666 dbus_message_get_interface (message) :
2668 dbus_message_get_signature (message),
2672 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2674 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
2676 /* Put message back, and we'll start over.
2677 * Yes this means handlers must be idempotent if they
2678 * don't return HANDLED; c'est la vie.
2680 _dbus_connection_putback_message_link_unlocked (connection,
2685 _dbus_verbose ("Done with message in %s\n", _DBUS_FUNCTION_NAME);
2687 if (connection->exit_on_disconnect &&
2688 dbus_message_is_signal (message,
2689 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
2692 _dbus_verbose ("Exiting on Disconnected signal\n");
2693 CONNECTION_UNLOCK (connection);
2695 _dbus_assert_not_reached ("Call to exit() returned");
2698 _dbus_list_free_link (message_link);
2699 dbus_message_unref (message); /* don't want the message to count in max message limits
2700 * in computing dispatch status below
2704 _dbus_connection_release_dispatch (connection);
2706 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2708 /* unlocks and calls user code */
2709 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2711 dbus_connection_unref (connection);
2717 * Sets the watch functions for the connection. These functions are
2718 * responsible for making the application's main loop aware of file
2719 * descriptors that need to be monitored for events, using select() or
2720 * poll(). When using Qt, typically the DBusAddWatchFunction would
2721 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2722 * could call g_io_add_watch(), or could be used as part of a more
2723 * elaborate GSource. Note that when a watch is added, it may
2726 * The DBusWatchToggledFunction notifies the application that the
2727 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2728 * to check this. A disabled watch should have no effect, and enabled
2729 * watch should be added to the main loop. This feature is used
2730 * instead of simply adding/removing the watch because
2731 * enabling/disabling can be done without memory allocation. The
2732 * toggled function may be NULL if a main loop re-queries
2733 * dbus_watch_get_enabled() every time anyway.
2735 * The DBusWatch can be queried for the file descriptor to watch using
2736 * dbus_watch_get_fd(), and for the events to watch for using
2737 * dbus_watch_get_flags(). The flags returned by
2738 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2739 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2740 * all watches implicitly include a watch for hangups, errors, and
2741 * other exceptional conditions.
2743 * Once a file descriptor becomes readable or writable, or an exception
2744 * occurs, dbus_watch_handle() should be called to
2745 * notify the connection of the file descriptor's condition.
2747 * dbus_watch_handle() cannot be called during the
2748 * DBusAddWatchFunction, as the connection will not be ready to handle
2751 * It is not allowed to reference a DBusWatch after it has been passed
2752 * to remove_function.
2754 * If #FALSE is returned due to lack of memory, the failure may be due
2755 * to a #FALSE return from the new add_function. If so, the
2756 * add_function may have been called successfully one or more times,
2757 * but the remove_function will also have been called to remove any
2758 * successful adds. i.e. if #FALSE is returned the net result
2759 * should be that dbus_connection_set_watch_functions() has no effect,
2760 * but the add_function and remove_function may have been called.
2762 * @todo We need to drop the lock when we call the
2763 * add/remove/toggled functions which can be a side effect
2764 * of setting the watch functions.
2766 * @param connection the connection.
2767 * @param add_function function to begin monitoring a new descriptor.
2768 * @param remove_function function to stop monitoring a descriptor.
2769 * @param toggled_function function to notify of enable/disable
2770 * @param data data to pass to add_function and remove_function.
2771 * @param free_data_function function to be called to free the data.
2772 * @returns #FALSE on failure (no memory)
2775 dbus_connection_set_watch_functions (DBusConnection *connection,
2776 DBusAddWatchFunction add_function,
2777 DBusRemoveWatchFunction remove_function,
2778 DBusWatchToggledFunction toggled_function,
2780 DBusFreeFunction free_data_function)
2784 _dbus_return_val_if_fail (connection != NULL, FALSE);
2786 CONNECTION_LOCK (connection);
2787 /* ref connection for slightly better reentrancy */
2788 _dbus_connection_ref_unlocked (connection);
2790 /* FIXME this can call back into user code, and we need to drop the
2791 * connection lock when it does.
2793 retval = _dbus_watch_list_set_functions (connection->watches,
2794 add_function, remove_function,
2796 data, free_data_function);
2798 CONNECTION_UNLOCK (connection);
2799 /* drop our paranoid refcount */
2800 dbus_connection_unref (connection);
2806 * Sets the timeout functions for the connection. These functions are
2807 * responsible for making the application's main loop aware of timeouts.
2808 * When using Qt, typically the DBusAddTimeoutFunction would create a
2809 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2812 * The DBusTimeoutToggledFunction notifies the application that the
2813 * timeout has been enabled or disabled. Call
2814 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2815 * have no effect, and enabled timeout should be added to the main
2816 * loop. This feature is used instead of simply adding/removing the
2817 * timeout because enabling/disabling can be done without memory
2818 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2819 * to enable and disable. The toggled function may be NULL if a main
2820 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2821 * Whenever a timeout is toggled, its interval may change.
2823 * The DBusTimeout can be queried for the timer interval using
2824 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
2825 * repeatedly, each time the interval elapses, starting after it has
2826 * elapsed once. The timeout stops firing when it is removed with the
2827 * given remove_function. The timer interval may change whenever the
2828 * timeout is added, removed, or toggled.
2830 * @param connection the connection.
2831 * @param add_function function to add a timeout.
2832 * @param remove_function function to remove a timeout.
2833 * @param toggled_function function to notify of enable/disable
2834 * @param data data to pass to add_function and remove_function.
2835 * @param free_data_function function to be called to free the data.
2836 * @returns #FALSE on failure (no memory)
2839 dbus_connection_set_timeout_functions (DBusConnection *connection,
2840 DBusAddTimeoutFunction add_function,
2841 DBusRemoveTimeoutFunction remove_function,
2842 DBusTimeoutToggledFunction toggled_function,
2844 DBusFreeFunction free_data_function)
2848 _dbus_return_val_if_fail (connection != NULL, FALSE);
2850 CONNECTION_LOCK (connection);
2851 /* ref connection for slightly better reentrancy */
2852 _dbus_connection_ref_unlocked (connection);
2854 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2855 add_function, remove_function,
2857 data, free_data_function);
2859 CONNECTION_UNLOCK (connection);
2860 /* drop our paranoid refcount */
2861 dbus_connection_unref (connection);
2867 * Sets the mainloop wakeup function for the connection. Thi function is
2868 * responsible for waking up the main loop (if its sleeping) when some some
2869 * change has happened to the connection that the mainloop needs to reconsiders
2870 * (e.g. a message has been queued for writing).
2871 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2872 * When using GLib, it would call g_main_context_wakeup().
2875 * @param connection the connection.
2876 * @param wakeup_main_function function to wake up the mainloop
2877 * @param data data to pass wakeup_main_function
2878 * @param free_data_function function to be called to free the data.
2881 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2882 DBusWakeupMainFunction wakeup_main_function,
2884 DBusFreeFunction free_data_function)
2887 DBusFreeFunction old_free_data;
2889 _dbus_return_if_fail (connection != NULL);
2891 CONNECTION_LOCK (connection);
2892 old_data = connection->wakeup_main_data;
2893 old_free_data = connection->free_wakeup_main_data;
2895 connection->wakeup_main_function = wakeup_main_function;
2896 connection->wakeup_main_data = data;
2897 connection->free_wakeup_main_data = free_data_function;
2899 CONNECTION_UNLOCK (connection);
2901 /* Callback outside the lock */
2903 (*old_free_data) (old_data);
2907 * Set a function to be invoked when the dispatch status changes.
2908 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
2909 * dbus_connection_dispatch() needs to be called to process incoming
2910 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
2911 * from inside the DBusDispatchStatusFunction. Indeed, almost
2912 * any reentrancy in this function is a bad idea. Instead,
2913 * the DBusDispatchStatusFunction should simply save an indication
2914 * that messages should be dispatched later, when the main loop
2917 * @param connection the connection
2918 * @param function function to call on dispatch status changes
2919 * @param data data for function
2920 * @param free_data_function free the function data
2923 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
2924 DBusDispatchStatusFunction function,
2926 DBusFreeFunction free_data_function)
2929 DBusFreeFunction old_free_data;
2931 _dbus_return_if_fail (connection != NULL);
2933 CONNECTION_LOCK (connection);
2934 old_data = connection->dispatch_status_data;
2935 old_free_data = connection->free_dispatch_status_data;
2937 connection->dispatch_status_function = function;
2938 connection->dispatch_status_data = data;
2939 connection->free_dispatch_status_data = free_data_function;
2941 CONNECTION_UNLOCK (connection);
2943 /* Callback outside the lock */
2945 (*old_free_data) (old_data);
2949 * Gets the UNIX user ID of the connection if any.
2950 * Returns #TRUE if the uid is filled in.
2951 * Always returns #FALSE on non-UNIX platforms.
2952 * Always returns #FALSE prior to authenticating the
2955 * @param connection the connection
2956 * @param uid return location for the user ID
2957 * @returns #TRUE if uid is filled in with a valid user ID
2960 dbus_connection_get_unix_user (DBusConnection *connection,
2965 _dbus_return_val_if_fail (connection != NULL, FALSE);
2966 _dbus_return_val_if_fail (uid != NULL, FALSE);
2968 CONNECTION_LOCK (connection);
2970 if (!_dbus_transport_get_is_authenticated (connection->transport))
2973 result = _dbus_transport_get_unix_user (connection->transport,
2975 CONNECTION_UNLOCK (connection);
2981 * Sets a predicate function used to determine whether a given user ID
2982 * is allowed to connect. When an incoming connection has
2983 * authenticated with a particular user ID, this function is called;
2984 * if it returns #TRUE, the connection is allowed to proceed,
2985 * otherwise the connection is disconnected.
2987 * If the function is set to #NULL (as it is by default), then
2988 * only the same UID as the server process will be allowed to
2991 * @param connection the connection
2992 * @param function the predicate
2993 * @param data data to pass to the predicate
2994 * @param free_data_function function to free the data
2997 dbus_connection_set_unix_user_function (DBusConnection *connection,
2998 DBusAllowUnixUserFunction function,
3000 DBusFreeFunction free_data_function)
3002 void *old_data = NULL;
3003 DBusFreeFunction old_free_function = NULL;
3005 _dbus_return_if_fail (connection != NULL);
3007 CONNECTION_LOCK (connection);
3008 _dbus_transport_set_unix_user_function (connection->transport,
3009 function, data, free_data_function,
3010 &old_data, &old_free_function);
3011 CONNECTION_UNLOCK (connection);
3013 if (old_free_function != NULL)
3014 (* old_free_function) (old_data);
3018 * Adds a message filter. Filters are handlers that are run on all
3019 * incoming messages, prior to the objects registered with
3020 * dbus_connection_register_object_path(). Filters are run in the
3021 * order that they were added. The same handler can be added as a
3022 * filter more than once, in which case it will be run more than once.
3023 * Filters added during a filter callback won't be run on the message
3026 * @todo we don't run filters on messages while blocking without
3027 * entering the main loop, since filters are run as part of
3028 * dbus_connection_dispatch(). This is probably a feature, as filters
3029 * could create arbitrary reentrancy. But kind of sucks if you're
3030 * trying to filter METHOD_RETURN for some reason.
3032 * @param connection the connection
3033 * @param function function to handle messages
3034 * @param user_data user data to pass to the function
3035 * @param free_data_function function to use for freeing user data
3036 * @returns #TRUE on success, #FALSE if not enough memory.
3039 dbus_connection_add_filter (DBusConnection *connection,
3040 DBusHandleMessageFunction function,
3042 DBusFreeFunction free_data_function)
3044 DBusMessageFilter *filter;
3046 _dbus_return_val_if_fail (connection != NULL, FALSE);
3047 _dbus_return_val_if_fail (function != NULL, FALSE);
3049 filter = dbus_new0 (DBusMessageFilter, 1);
3053 filter->refcount.value = 1;
3055 CONNECTION_LOCK (connection);
3057 if (!_dbus_list_append (&connection->filter_list,
3060 _dbus_message_filter_unref (filter);
3061 CONNECTION_UNLOCK (connection);
3065 /* Fill in filter after all memory allocated,
3066 * so we don't run the free_user_data_function
3067 * if the add_filter() fails
3070 filter->function = function;
3071 filter->user_data = user_data;
3072 filter->free_user_data_function = free_data_function;
3074 CONNECTION_UNLOCK (connection);
3079 * Removes a previously-added message filter. It is a programming
3080 * error to call this function for a handler that has not been added
3081 * as a filter. If the given handler was added more than once, only
3082 * one instance of it will be removed (the most recently-added
3085 * @param connection the connection
3086 * @param function the handler to remove
3087 * @param user_data user data for the handler to remove
3091 dbus_connection_remove_filter (DBusConnection *connection,
3092 DBusHandleMessageFunction function,
3096 DBusMessageFilter *filter;
3098 _dbus_return_if_fail (connection != NULL);
3099 _dbus_return_if_fail (function != NULL);
3101 CONNECTION_LOCK (connection);
3105 link = _dbus_list_get_last_link (&connection->filter_list);
3106 while (link != NULL)
3108 filter = link->data;
3110 if (filter->function == function &&
3111 filter->user_data == user_data)
3113 _dbus_list_remove_link (&connection->filter_list, link);
3114 filter->function = NULL;
3119 link = _dbus_list_get_prev_link (&connection->filter_list, link);
3122 CONNECTION_UNLOCK (connection);
3124 #ifndef DBUS_DISABLE_CHECKS
3127 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
3128 function, user_data);
3133 /* Call application code */
3134 if (filter->free_user_data_function)
3135 (* filter->free_user_data_function) (filter->user_data);
3137 filter->free_user_data_function = NULL;
3138 filter->user_data = NULL;
3140 _dbus_message_filter_unref (filter);
3144 * Registers a handler for a given path in the object hierarchy.
3145 * The given vtable handles messages sent to exactly the given path.
3148 * @param connection the connection
3149 * @param path #NULL-terminated array of path elements
3150 * @param vtable the virtual table
3151 * @param user_data data to pass to functions in the vtable
3152 * @returns #FALSE if not enough memory
3155 dbus_connection_register_object_path (DBusConnection *connection,
3157 const DBusObjectPathVTable *vtable,
3162 _dbus_return_val_if_fail (connection != NULL, FALSE);
3163 _dbus_return_val_if_fail (path != NULL, FALSE);
3164 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3165 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3167 CONNECTION_LOCK (connection);
3169 retval = _dbus_object_tree_register (connection->objects,
3174 CONNECTION_UNLOCK (connection);
3180 * Registers a fallback handler for a given subsection of the object
3181 * hierarchy. The given vtable handles messages at or below the given
3182 * path. You can use this to establish a default message handling
3183 * policy for a whole "subdirectory."
3185 * @param connection the connection
3186 * @param path #NULL-terminated array of path elements
3187 * @param vtable the virtual table
3188 * @param user_data data to pass to functions in the vtable
3189 * @returns #FALSE if not enough memory
3192 dbus_connection_register_fallback (DBusConnection *connection,
3194 const DBusObjectPathVTable *vtable,
3199 _dbus_return_val_if_fail (connection != NULL, FALSE);
3200 _dbus_return_val_if_fail (path != NULL, FALSE);
3201 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3202 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3204 CONNECTION_LOCK (connection);
3206 retval = _dbus_object_tree_register (connection->objects,
3211 CONNECTION_UNLOCK (connection);
3217 * Unregisters the handler registered with exactly the given path.
3218 * It's a bug to call this function for a path that isn't registered.
3219 * Can unregister both fallback paths and object paths.
3221 * @param connection the connection
3222 * @param path the #NULL-terminated array of path elements
3225 dbus_connection_unregister_object_path (DBusConnection *connection,
3228 _dbus_return_if_fail (connection != NULL);
3229 _dbus_return_if_fail (path != NULL);
3230 _dbus_return_if_fail (path[0] != NULL);
3232 CONNECTION_LOCK (connection);
3234 return _dbus_object_tree_unregister_and_unlock (connection->objects,
3239 * Lists the registered fallback handlers and object path handlers at
3240 * the given parent_path. The returned array should be freed with
3241 * dbus_free_string_array().
3243 * @param connection the connection
3244 * @param parent_path the path to list the child handlers of
3245 * @param child_entries returns #NULL-terminated array of children
3246 * @returns #FALSE if no memory to allocate the child entries
3249 dbus_connection_list_registered (DBusConnection *connection,
3250 const char **parent_path,
3251 char ***child_entries)
3253 _dbus_return_val_if_fail (connection != NULL, FALSE);
3254 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
3255 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
3257 CONNECTION_LOCK (connection);
3259 return _dbus_object_tree_list_registered_and_unlock (connection->objects,
3264 static DBusDataSlotAllocator slot_allocator;
3265 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
3268 * Allocates an integer ID to be used for storing application-specific
3269 * data on any DBusConnection. The allocated ID may then be used
3270 * with dbus_connection_set_data() and dbus_connection_get_data().
3271 * The passed-in slot must be initialized to -1, and is filled in
3272 * with the slot ID. If the passed-in slot is not -1, it's assumed
3273 * to be already allocated, and its refcount is incremented.
3275 * The allocated slot is global, i.e. all DBusConnection objects will
3276 * have a slot with the given integer ID reserved.
3278 * @param slot_p address of a global variable storing the slot
3279 * @returns #FALSE on failure (no memory)
3282 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
3284 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3285 _DBUS_LOCK_NAME (connection_slots),
3290 * Deallocates a global ID for connection data slots.
3291 * dbus_connection_get_data() and dbus_connection_set_data() may no
3292 * longer be used with this slot. Existing data stored on existing
3293 * DBusConnection objects will be freed when the connection is
3294 * finalized, but may not be retrieved (and may only be replaced if
3295 * someone else reallocates the slot). When the refcount on the
3296 * passed-in slot reaches 0, it is set to -1.
3298 * @param slot_p address storing the slot to deallocate
3301 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
3303 _dbus_return_if_fail (*slot_p >= 0);
3305 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3309 * Stores a pointer on a DBusConnection, along
3310 * with an optional function to be used for freeing
3311 * the data when the data is set again, or when
3312 * the connection is finalized. The slot number
3313 * must have been allocated with dbus_connection_allocate_data_slot().
3315 * @param connection the connection
3316 * @param slot the slot number
3317 * @param data the data to store
3318 * @param free_data_func finalizer function for the data
3319 * @returns #TRUE if there was enough memory to store the data
3322 dbus_connection_set_data (DBusConnection *connection,
3325 DBusFreeFunction free_data_func)
3327 DBusFreeFunction old_free_func;
3331 _dbus_return_val_if_fail (connection != NULL, FALSE);
3332 _dbus_return_val_if_fail (slot >= 0, FALSE);
3334 CONNECTION_LOCK (connection);
3336 retval = _dbus_data_slot_list_set (&slot_allocator,
3337 &connection->slot_list,
3338 slot, data, free_data_func,
3339 &old_free_func, &old_data);
3341 CONNECTION_UNLOCK (connection);
3345 /* Do the actual free outside the connection lock */
3347 (* old_free_func) (old_data);
3354 * Retrieves data previously set with dbus_connection_set_data().
3355 * The slot must still be allocated (must not have been freed).
3357 * @param connection the connection
3358 * @param slot the slot to get data from
3359 * @returns the data, or #NULL if not found
3362 dbus_connection_get_data (DBusConnection *connection,
3367 _dbus_return_val_if_fail (connection != NULL, NULL);
3369 CONNECTION_LOCK (connection);
3371 res = _dbus_data_slot_list_get (&slot_allocator,
3372 &connection->slot_list,
3375 CONNECTION_UNLOCK (connection);
3381 * This function sets a global flag for whether dbus_connection_new()
3382 * will set SIGPIPE behavior to SIG_IGN.
3384 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
3387 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
3389 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
3393 * Specifies the maximum size message this connection is allowed to
3394 * receive. Larger messages will result in disconnecting the
3397 * @param connection a #DBusConnection
3398 * @param size maximum message size the connection can receive, in bytes
3401 dbus_connection_set_max_message_size (DBusConnection *connection,
3404 _dbus_return_if_fail (connection != NULL);
3406 CONNECTION_LOCK (connection);
3407 _dbus_transport_set_max_message_size (connection->transport,
3409 CONNECTION_UNLOCK (connection);
3413 * Gets the value set by dbus_connection_set_max_message_size().
3415 * @param connection the connection
3416 * @returns the max size of a single message
3419 dbus_connection_get_max_message_size (DBusConnection *connection)
3423 _dbus_return_val_if_fail (connection != NULL, 0);
3425 CONNECTION_LOCK (connection);
3426 res = _dbus_transport_get_max_message_size (connection->transport);
3427 CONNECTION_UNLOCK (connection);
3432 * Sets the maximum total number of bytes that can be used for all messages
3433 * received on this connection. Messages count toward the maximum until
3434 * they are finalized. When the maximum is reached, the connection will
3435 * not read more data until some messages are finalized.
3437 * The semantics of the maximum are: if outstanding messages are
3438 * already above the maximum, additional messages will not be read.
3439 * The semantics are not: if the next message would cause us to exceed
3440 * the maximum, we don't read it. The reason is that we don't know the
3441 * size of a message until after we read it.
3443 * Thus, the max live messages size can actually be exceeded
3444 * by up to the maximum size of a single message.
3446 * Also, if we read say 1024 bytes off the wire in a single read(),
3447 * and that contains a half-dozen small messages, we may exceed the
3448 * size max by that amount. But this should be inconsequential.
3450 * This does imply that we can't call read() with a buffer larger
3451 * than we're willing to exceed this limit by.
3453 * @param connection the connection
3454 * @param size the maximum size in bytes of all outstanding messages
3457 dbus_connection_set_max_received_size (DBusConnection *connection,
3460 _dbus_return_if_fail (connection != NULL);
3462 CONNECTION_LOCK (connection);
3463 _dbus_transport_set_max_received_size (connection->transport,
3465 CONNECTION_UNLOCK (connection);
3469 * Gets the value set by dbus_connection_set_max_received_size().
3471 * @param connection the connection
3472 * @returns the max size of all live messages
3475 dbus_connection_get_max_received_size (DBusConnection *connection)
3479 _dbus_return_val_if_fail (connection != NULL, 0);
3481 CONNECTION_LOCK (connection);
3482 res = _dbus_transport_get_max_received_size (connection->transport);
3483 CONNECTION_UNLOCK (connection);
3488 * Gets the approximate size in bytes of all messages in the outgoing
3489 * message queue. The size is approximate in that you shouldn't use
3490 * it to decide how many bytes to read off the network or anything
3491 * of that nature, as optimizations may choose to tell small white lies
3492 * to avoid performance overhead.
3494 * @param connection the connection
3495 * @returns the number of bytes that have been queued up but not sent
3498 dbus_connection_get_outgoing_size (DBusConnection *connection)
3502 _dbus_return_val_if_fail (connection != NULL, 0);
3504 CONNECTION_LOCK (connection);
3505 res = _dbus_counter_get_value (connection->outgoing_counter);
3506 CONNECTION_UNLOCK (connection);