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
24 #include "dbus-connection.h"
25 #include "dbus-list.h"
26 #include "dbus-timeout.h"
27 #include "dbus-transport.h"
28 #include "dbus-watch.h"
29 #include "dbus-connection-internal.h"
30 #include "dbus-list.h"
31 #include "dbus-hash.h"
32 #include "dbus-message-internal.h"
33 #include "dbus-message-handler.h"
34 #include "dbus-threads.h"
35 #include "dbus-protocol.h"
36 #include "dbus-dataslot.h"
39 * @defgroup DBusConnection DBusConnection
41 * @brief Connection to another application
43 * A DBusConnection represents a connection to another
44 * application. Messages can be sent and received via this connection.
46 * The connection maintains a queue of incoming messages and a queue
47 * of outgoing messages. dbus_connection_pop_message() and friends
48 * can be used to read incoming messages from the queue.
49 * Outgoing messages are automatically discarded as they are
50 * written to the network.
52 * In brief a DBusConnection is a message queue associated with some
53 * message transport mechanism such as a socket.
58 * @defgroup DBusConnectionInternals DBusConnection implementation details
59 * @ingroup DBusInternals
60 * @brief Implementation details of DBusConnection
65 /** default timeout value when waiting for a message reply */
66 #define DEFAULT_TIMEOUT_VALUE (15 * 1000)
68 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
71 * Implementation details of DBusConnection. All fields are private.
75 int refcount; /**< Reference count. */
77 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
79 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */
80 DBusCondVar *dispatch_cond; /**< Protects dispatch() */
82 dbus_bool_t io_path_acquired; /**< Protects transport io path */
83 DBusCondVar *io_path_cond; /**< Protects transport io path */
85 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
86 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
88 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
89 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
91 int n_outgoing; /**< Length of outgoing queue. */
92 int n_incoming; /**< Length of incoming queue. */
94 DBusTransport *transport; /**< Object that sends/receives messages over network. */
95 DBusWatchList *watches; /**< Stores active watches. */
96 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
98 DBusHashTable *handler_table; /**< Table of registered DBusMessageHandler */
99 DBusList *filter_list; /**< List of filters. */
101 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
103 DBusHashTable *pending_replies; /**< Hash of message serials and their message handlers. */
104 DBusCounter *connection_counter; /**< Counter that we decrement when finalized */
106 int client_serial; /**< Client serial. Increments each time a message is sent */
107 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
109 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
110 void *wakeup_main_data; /**< Application data for wakeup_main_function */
111 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
113 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
114 void *dispatch_status_data; /**< Application data for dispatch_status_function */
115 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
117 DBusDispatchStatus last_dispatch_status;
122 DBusConnection *connection;
123 DBusMessageHandler *handler;
124 DBusTimeout *timeout;
127 DBusList *timeout_link; /* Preallocated timeout response */
129 dbus_bool_t timeout_added;
130 dbus_bool_t connection_added;
133 static void reply_handler_data_free (ReplyHandlerData *data);
135 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
136 DBusTimeout *timeout);
137 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
138 static void _dbus_connection_update_dispatch_status_locked (DBusConnection *connection,
139 DBusDispatchStatus new_status);
143 * Acquires the connection lock.
145 * @param connection the connection.
148 _dbus_connection_lock (DBusConnection *connection)
150 dbus_mutex_lock (connection->mutex);
154 * Releases the connection lock.
156 * @param connection the connection.
159 _dbus_connection_unlock (DBusConnection *connection)
161 dbus_mutex_unlock (connection->mutex);
165 * Wakes up the main loop if it is sleeping
166 * Needed if we're e.g. queueing outgoing messages
167 * on a thread while the mainloop sleeps.
169 * @param connection the connection.
172 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
174 if (connection->wakeup_main_function)
175 (*connection->wakeup_main_function) (connection->wakeup_main_data);
179 * Adds a message to the incoming message queue, returning #FALSE
180 * if there's insufficient memory to queue the message.
181 * Does not take over refcount of the message.
183 * @param connection the connection.
184 * @param message the message to queue.
185 * @returns #TRUE on success.
188 _dbus_connection_queue_received_message (DBusConnection *connection,
189 DBusMessage *message)
193 link = _dbus_list_alloc_link (message);
197 dbus_message_ref (message);
198 _dbus_connection_queue_received_message_link (connection, link);
204 * Adds a message-containing list link to the incoming message queue,
205 * taking ownership of the link and the message's current refcount.
206 * Cannot fail due to lack of memory.
208 * @param connection the connection.
209 * @param link the message link to queue.
212 _dbus_connection_queue_received_message_link (DBusConnection *connection,
215 ReplyHandlerData *reply_handler_data;
216 dbus_int32_t reply_serial;
217 DBusMessage *message;
219 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
221 _dbus_list_append_link (&connection->incoming_messages,
223 message = link->data;
225 /* If this is a reply we're waiting on, remove timeout for it */
226 reply_serial = dbus_message_get_reply_serial (message);
227 if (reply_serial != -1)
229 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
231 if (reply_handler_data != NULL)
233 if (reply_handler_data->timeout_added)
234 _dbus_connection_remove_timeout_locked (connection,
235 reply_handler_data->timeout);
236 reply_handler_data->timeout_added = FALSE;
240 connection->n_incoming += 1;
242 _dbus_connection_wakeup_mainloop (connection);
244 _dbus_assert (dbus_message_get_name (message) != NULL);
245 _dbus_verbose ("Message %p (%s) added to incoming queue %p, %d incoming\n",
246 message, dbus_message_get_name (message),
248 connection->n_incoming);
252 * Adds a link + message to the incoming message queue.
253 * Can't fail. Takes ownership of both link and message.
255 * @param connection the connection.
256 * @param link the list node and message to queue.
258 * @todo This needs to wake up the mainloop if it is in
259 * a poll/select and this is a multithreaded app.
262 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
265 _dbus_list_append_link (&connection->incoming_messages, link);
267 connection->n_incoming += 1;
269 _dbus_connection_wakeup_mainloop (connection);
271 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
272 link->data, connection, connection->n_incoming);
277 * Checks whether there are messages in the outgoing message queue.
279 * @param connection the connection.
280 * @returns #TRUE if the outgoing queue is non-empty.
283 _dbus_connection_have_messages_to_send (DBusConnection *connection)
285 return connection->outgoing_messages != NULL;
289 * Gets the next outgoing message. The message remains in the
290 * queue, and the caller does not own a reference to it.
292 * @param connection the connection.
293 * @returns the message to be sent.
296 _dbus_connection_get_message_to_send (DBusConnection *connection)
298 return _dbus_list_get_last (&connection->outgoing_messages);
302 * Notifies the connection that a message has been sent, so the
303 * message can be removed from the outgoing queue.
305 * @param connection the connection.
306 * @param message the message that was sent.
309 _dbus_connection_message_sent (DBusConnection *connection,
310 DBusMessage *message)
312 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
313 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
315 _dbus_list_pop_last (&connection->outgoing_messages);
317 connection->n_outgoing -= 1;
319 _dbus_verbose ("Message %p (%s) removed from outgoing queue %p, %d left to send\n",
320 message, dbus_message_get_name (message),
321 connection, connection->n_outgoing);
323 dbus_message_unref (message);
325 if (connection->n_outgoing == 0)
326 _dbus_transport_messages_pending (connection->transport,
327 connection->n_outgoing);
331 * Adds a watch using the connection's DBusAddWatchFunction if
332 * available. Otherwise records the watch to be added when said
333 * function is available. Also re-adds the watch if the
334 * DBusAddWatchFunction changes. May fail due to lack of memory.
336 * @param connection the connection.
337 * @param watch the watch to add.
338 * @returns #TRUE on success.
341 _dbus_connection_add_watch (DBusConnection *connection,
344 if (connection->watches) /* null during finalize */
345 return _dbus_watch_list_add_watch (connection->watches,
352 * Removes a watch using the connection's DBusRemoveWatchFunction
353 * if available. It's an error to call this function on a watch
354 * that was not previously added.
356 * @param connection the connection.
357 * @param watch the watch to remove.
360 _dbus_connection_remove_watch (DBusConnection *connection,
363 if (connection->watches) /* null during finalize */
364 _dbus_watch_list_remove_watch (connection->watches,
369 * Toggles a watch and notifies app via connection's
370 * DBusWatchToggledFunction if available. It's an error to call this
371 * function on a watch that was not previously added.
373 * @param connection the connection.
374 * @param watch the watch to toggle.
375 * @param enabled whether to enable or disable
378 _dbus_connection_toggle_watch (DBusConnection *connection,
382 if (connection->watches) /* null during finalize */
383 _dbus_watch_list_toggle_watch (connection->watches,
388 * Adds a timeout using the connection's DBusAddTimeoutFunction if
389 * available. Otherwise records the timeout to be added when said
390 * function is available. Also re-adds the timeout if the
391 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
392 * The timeout will fire repeatedly until removed.
394 * @param connection the connection.
395 * @param timeout the timeout to add.
396 * @returns #TRUE on success.
399 _dbus_connection_add_timeout (DBusConnection *connection,
400 DBusTimeout *timeout)
402 if (connection->timeouts) /* null during finalize */
403 return _dbus_timeout_list_add_timeout (connection->timeouts,
410 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
411 * if available. It's an error to call this function on a timeout
412 * that was not previously added.
414 * @param connection the connection.
415 * @param timeout the timeout to remove.
418 _dbus_connection_remove_timeout (DBusConnection *connection,
419 DBusTimeout *timeout)
421 if (connection->timeouts) /* null during finalize */
422 _dbus_timeout_list_remove_timeout (connection->timeouts,
427 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
428 DBusTimeout *timeout)
430 dbus_mutex_lock (connection->mutex);
431 _dbus_connection_remove_timeout (connection, timeout);
432 dbus_mutex_unlock (connection->mutex);
436 * Toggles a timeout and notifies app via connection's
437 * DBusTimeoutToggledFunction if available. It's an error to call this
438 * function on a timeout that was not previously added.
440 * @param connection the connection.
441 * @param timeout the timeout to toggle.
442 * @param enabled whether to enable or disable
445 _dbus_connection_toggle_timeout (DBusConnection *connection,
446 DBusTimeout *timeout,
449 if (connection->timeouts) /* null during finalize */
450 _dbus_timeout_list_toggle_timeout (connection->timeouts,
455 * Tells the connection that the transport has been disconnected.
456 * Results in posting a disconnect message on the incoming message
457 * queue. Only has an effect the first time it's called.
459 * @param connection the connection
462 _dbus_connection_notify_disconnected (DBusConnection *connection)
464 if (connection->disconnect_message_link)
466 /* We haven't sent the disconnect message already */
467 _dbus_connection_queue_synthesized_message_link (connection,
468 connection->disconnect_message_link);
469 connection->disconnect_message_link = NULL;
475 * Acquire the transporter I/O path. This must be done before
476 * doing any I/O in the transporter. May sleep and drop the
477 * connection mutex while waiting for the I/O path.
479 * @param connection the connection.
480 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
481 * @returns TRUE if the I/O path was acquired.
484 _dbus_connection_acquire_io_path (DBusConnection *connection,
485 int timeout_milliseconds)
487 dbus_bool_t res = TRUE;
489 if (connection->io_path_acquired)
491 if (timeout_milliseconds != -1)
492 res = dbus_condvar_wait_timeout (connection->io_path_cond,
494 timeout_milliseconds);
496 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
501 _dbus_assert (!connection->io_path_acquired);
503 connection->io_path_acquired = TRUE;
510 * Release the I/O path when you're done with it. Only call
511 * after you've acquired the I/O. Wakes up at most one thread
512 * currently waiting to acquire the I/O path.
514 * @param connection the connection.
517 _dbus_connection_release_io_path (DBusConnection *connection)
519 _dbus_assert (connection->io_path_acquired);
521 connection->io_path_acquired = FALSE;
522 dbus_condvar_wake_one (connection->io_path_cond);
527 * Queues incoming messages and sends outgoing messages for this
528 * connection, optionally blocking in the process. Each call to
529 * _dbus_connection_do_iteration() will call select() or poll() one
530 * time and then read or write data if possible.
532 * The purpose of this function is to be able to flush outgoing
533 * messages or queue up incoming messages without returning
534 * control to the application and causing reentrancy weirdness.
536 * The flags parameter allows you to specify whether to
537 * read incoming messages, write outgoing messages, or both,
538 * and whether to block if no immediate action is possible.
540 * The timeout_milliseconds parameter does nothing unless the
541 * iteration is blocking.
543 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
544 * wasn't specified, then it's impossible to block, even if
545 * you specify DBUS_ITERATION_BLOCK; in that case the function
546 * returns immediately.
548 * @param connection the connection.
549 * @param flags iteration flags.
550 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
553 _dbus_connection_do_iteration (DBusConnection *connection,
555 int timeout_milliseconds)
557 if (connection->n_outgoing == 0)
558 flags &= ~DBUS_ITERATION_DO_WRITING;
560 if (_dbus_connection_acquire_io_path (connection,
561 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
563 _dbus_transport_do_iteration (connection->transport,
564 flags, timeout_milliseconds);
565 _dbus_connection_release_io_path (connection);
570 * Creates a new connection for the given transport. A transport
571 * represents a message stream that uses some concrete mechanism, such
572 * as UNIX domain sockets. May return #NULL if insufficient
573 * memory exists to create the connection.
575 * @param transport the transport.
576 * @returns the new connection, or #NULL on failure.
579 _dbus_connection_new_for_transport (DBusTransport *transport)
581 DBusConnection *connection;
582 DBusWatchList *watch_list;
583 DBusTimeoutList *timeout_list;
584 DBusHashTable *handler_table, *pending_replies;
586 DBusCondVar *message_returned_cond;
587 DBusCondVar *dispatch_cond;
588 DBusCondVar *io_path_cond;
589 DBusList *disconnect_link;
590 DBusMessage *disconnect_message;
594 handler_table = NULL;
595 pending_replies = NULL;
598 message_returned_cond = NULL;
599 dispatch_cond = NULL;
601 disconnect_link = NULL;
602 disconnect_message = NULL;
604 watch_list = _dbus_watch_list_new ();
605 if (watch_list == NULL)
608 timeout_list = _dbus_timeout_list_new ();
609 if (timeout_list == NULL)
613 _dbus_hash_table_new (DBUS_HASH_STRING,
615 if (handler_table == NULL)
619 _dbus_hash_table_new (DBUS_HASH_INT,
620 NULL, (DBusFreeFunction)reply_handler_data_free);
621 if (pending_replies == NULL)
624 connection = dbus_new0 (DBusConnection, 1);
625 if (connection == NULL)
628 mutex = dbus_mutex_new ();
632 message_returned_cond = dbus_condvar_new ();
633 if (message_returned_cond == NULL)
636 dispatch_cond = dbus_condvar_new ();
637 if (dispatch_cond == NULL)
640 io_path_cond = dbus_condvar_new ();
641 if (io_path_cond == NULL)
644 disconnect_message = dbus_message_new (NULL, DBUS_MESSAGE_LOCAL_DISCONNECT);
645 if (disconnect_message == NULL)
648 disconnect_link = _dbus_list_alloc_link (disconnect_message);
649 if (disconnect_link == NULL)
652 if (_dbus_modify_sigpipe)
653 _dbus_disable_sigpipe ();
655 connection->refcount = 1;
656 connection->mutex = mutex;
657 connection->dispatch_cond = dispatch_cond;
658 connection->io_path_cond = io_path_cond;
659 connection->message_returned_cond = message_returned_cond;
660 connection->transport = transport;
661 connection->watches = watch_list;
662 connection->timeouts = timeout_list;
663 connection->handler_table = handler_table;
664 connection->pending_replies = pending_replies;
665 connection->filter_list = NULL;
666 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
668 _dbus_data_slot_list_init (&connection->slot_list);
670 connection->client_serial = 1;
672 connection->disconnect_message_link = disconnect_link;
674 if (!_dbus_transport_set_connection (transport, connection))
677 _dbus_transport_ref (transport);
682 if (disconnect_message != NULL)
683 dbus_message_unref (disconnect_message);
685 if (disconnect_link != NULL)
686 _dbus_list_free_link (disconnect_link);
688 if (io_path_cond != NULL)
689 dbus_condvar_free (io_path_cond);
691 if (dispatch_cond != NULL)
692 dbus_condvar_free (dispatch_cond);
694 if (message_returned_cond != NULL)
695 dbus_condvar_free (message_returned_cond);
698 dbus_mutex_free (mutex);
700 if (connection != NULL)
701 dbus_free (connection);
704 _dbus_hash_table_unref (handler_table);
707 _dbus_hash_table_unref (pending_replies);
710 _dbus_watch_list_free (watch_list);
713 _dbus_timeout_list_free (timeout_list);
719 _dbus_connection_get_next_client_serial (DBusConnection *connection)
723 serial = connection->client_serial++;
725 if (connection->client_serial < 0)
726 connection->client_serial = 1;
732 * Used to notify a connection when a DBusMessageHandler is
733 * destroyed, so the connection can drop any reference
734 * to the handler. This is a private function, but still
735 * takes the connection lock. Don't call it with the lock held.
737 * @todo needs to check in pending_replies too.
739 * @param connection the connection
740 * @param handler the handler
743 _dbus_connection_handler_destroyed_locked (DBusConnection *connection,
744 DBusMessageHandler *handler)
749 dbus_mutex_lock (connection->mutex);
751 _dbus_hash_iter_init (connection->handler_table, &iter);
752 while (_dbus_hash_iter_next (&iter))
754 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
757 _dbus_hash_iter_remove_entry (&iter);
760 link = _dbus_list_get_first_link (&connection->filter_list);
763 DBusMessageHandler *h = link->data;
764 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
767 _dbus_list_remove_link (&connection->filter_list,
772 dbus_mutex_unlock (connection->mutex);
776 * Adds the counter used to count the number of open connections.
777 * Increments the counter by one, and saves it to be decremented
778 * again when this connection is finalized.
780 * @param connection a #DBusConnection
781 * @param counter counter that tracks number of connections
784 _dbus_connection_set_connection_counter (DBusConnection *connection,
785 DBusCounter *counter)
787 _dbus_assert (connection->connection_counter == NULL);
789 connection->connection_counter = counter;
790 _dbus_counter_ref (connection->connection_counter);
791 _dbus_counter_adjust (connection->connection_counter, 1);
797 * @addtogroup DBusConnection
803 * Opens a new connection to a remote address.
805 * @todo specify what the address parameter is. Right now
806 * it's just the name of a UNIX domain socket. It should be
807 * something more complex that encodes which transport to use.
809 * If the open fails, the function returns #NULL, and provides
810 * a reason for the failure in the result parameter. Pass
811 * #NULL for the result parameter if you aren't interested
812 * in the reason for failure.
814 * @param address the address.
815 * @param error address where an error can be returned.
816 * @returns new connection, or #NULL on failure.
819 dbus_connection_open (const char *address,
822 DBusConnection *connection;
823 DBusTransport *transport;
825 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
827 transport = _dbus_transport_open (address, error);
828 if (transport == NULL)
830 _DBUS_ASSERT_ERROR_IS_SET (error);
834 connection = _dbus_connection_new_for_transport (transport);
836 _dbus_transport_unref (transport);
838 if (connection == NULL)
840 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
848 * Increments the reference count of a DBusConnection.
850 * @param connection the connection.
853 dbus_connection_ref (DBusConnection *connection)
855 dbus_mutex_lock (connection->mutex);
856 _dbus_assert (connection->refcount > 0);
858 connection->refcount += 1;
859 dbus_mutex_unlock (connection->mutex);
863 * Increments the reference count of a DBusConnection.
864 * Requires that the caller already holds the connection lock.
866 * @param connection the connection.
869 _dbus_connection_ref_unlocked (DBusConnection *connection)
871 _dbus_assert (connection->refcount > 0);
872 connection->refcount += 1;
876 /* This is run without the mutex held, but after the last reference
877 * to the connection has been dropped we should have no thread-related
881 _dbus_connection_last_unref (DBusConnection *connection)
886 _dbus_verbose ("Finalizing connection %p\n", connection);
888 _dbus_assert (connection->refcount == 0);
890 /* You have to disconnect the connection before unref:ing it. Otherwise
891 * you won't get the disconnected message.
893 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
895 if (connection->connection_counter != NULL)
897 /* subtract ourselves from the counter */
898 _dbus_counter_adjust (connection->connection_counter, - 1);
899 _dbus_counter_unref (connection->connection_counter);
900 connection->connection_counter = NULL;
903 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
904 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
905 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
906 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
908 _dbus_watch_list_free (connection->watches);
909 connection->watches = NULL;
911 _dbus_timeout_list_free (connection->timeouts);
912 connection->timeouts = NULL;
914 _dbus_data_slot_list_free (&connection->slot_list);
915 /* ---- Done with stuff that invokes application callbacks */
917 _dbus_hash_iter_init (connection->handler_table, &iter);
918 while (_dbus_hash_iter_next (&iter))
920 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
922 _dbus_message_handler_remove_connection (h, connection);
925 link = _dbus_list_get_first_link (&connection->filter_list);
928 DBusMessageHandler *h = link->data;
929 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
931 _dbus_message_handler_remove_connection (h, connection);
936 _dbus_hash_table_unref (connection->handler_table);
937 connection->handler_table = NULL;
939 _dbus_hash_table_unref (connection->pending_replies);
940 connection->pending_replies = NULL;
942 _dbus_list_clear (&connection->filter_list);
944 _dbus_list_foreach (&connection->outgoing_messages,
945 (DBusForeachFunction) dbus_message_unref,
947 _dbus_list_clear (&connection->outgoing_messages);
949 _dbus_list_foreach (&connection->incoming_messages,
950 (DBusForeachFunction) dbus_message_unref,
952 _dbus_list_clear (&connection->incoming_messages);
954 _dbus_transport_unref (connection->transport);
956 if (connection->disconnect_message_link)
958 DBusMessage *message = connection->disconnect_message_link->data;
959 dbus_message_unref (message);
960 _dbus_list_free_link (connection->disconnect_message_link);
963 dbus_condvar_free (connection->dispatch_cond);
964 dbus_condvar_free (connection->io_path_cond);
965 dbus_condvar_free (connection->message_returned_cond);
967 dbus_mutex_free (connection->mutex);
969 dbus_free (connection);
973 * Decrements the reference count of a DBusConnection, and finalizes
974 * it if the count reaches zero. It is a bug to drop the last reference
975 * to a connection that has not been disconnected.
977 * @todo in practice it can be quite tricky to never unref a connection
978 * that's still connected; maybe there's some way we could avoid
981 * @param connection the connection.
984 dbus_connection_unref (DBusConnection *connection)
986 dbus_bool_t last_unref;
988 dbus_mutex_lock (connection->mutex);
990 _dbus_assert (connection != NULL);
991 _dbus_assert (connection->refcount > 0);
993 connection->refcount -= 1;
994 last_unref = (connection->refcount == 0);
997 printf ("unref() connection %p count = %d\n", connection, connection->refcount);
1000 dbus_mutex_unlock (connection->mutex);
1003 _dbus_connection_last_unref (connection);
1007 * Closes the connection, so no further data can be sent or received.
1008 * Any further attempts to send data will result in errors. This
1009 * function does not affect the connection's reference count. It's
1010 * safe to disconnect a connection more than once; all calls after the
1011 * first do nothing. It's impossible to "reconnect" a connection, a
1012 * new connection must be created.
1014 * @param connection the connection.
1017 dbus_connection_disconnect (DBusConnection *connection)
1019 dbus_mutex_lock (connection->mutex);
1020 _dbus_transport_disconnect (connection->transport);
1021 dbus_mutex_unlock (connection->mutex);
1025 * Gets whether the connection is currently connected. All
1026 * connections are connected when they are opened. A connection may
1027 * become disconnected when the remote application closes its end, or
1028 * exits; a connection may also be disconnected with
1029 * dbus_connection_disconnect().
1031 * @param connection the connection.
1032 * @returns #TRUE if the connection is still alive.
1035 dbus_connection_get_is_connected (DBusConnection *connection)
1039 dbus_mutex_lock (connection->mutex);
1040 res = _dbus_transport_get_is_connected (connection->transport);
1041 dbus_mutex_unlock (connection->mutex);
1047 * Gets whether the connection was authenticated. (Note that
1048 * if the connection was authenticated then disconnected,
1049 * this function still returns #TRUE)
1051 * @param connection the connection
1052 * @returns #TRUE if the connection was ever authenticated
1055 dbus_connection_get_is_authenticated (DBusConnection *connection)
1059 dbus_mutex_lock (connection->mutex);
1060 res = _dbus_transport_get_is_authenticated (connection->transport);
1061 dbus_mutex_unlock (connection->mutex);
1067 * Preallocates resources needed to send a message, allowing the message
1068 * to be sent without the possibility of memory allocation failure.
1069 * Allows apps to create a future guarantee that they can send
1070 * a message regardless of memory shortages.
1072 * @param connection the connection we're preallocating for.
1073 * @returns the preallocated resources, or #NULL
1075 DBusPreallocatedSend*
1076 dbus_connection_preallocate_send (DBusConnection *connection)
1078 /* we store "connection" in the link just to enforce via
1079 * assertion that preallocated links are only used
1080 * with the connection they were created for.
1082 return (DBusPreallocatedSend*) _dbus_list_alloc_link (connection);
1086 * Frees preallocated message-sending resources from
1087 * dbus_connection_preallocate_send(). Should only
1088 * be called if the preallocated resources are not used
1089 * to send a message.
1091 * @param connection the connection
1092 * @param preallocated the resources
1095 dbus_connection_free_preallocated_send (DBusConnection *connection,
1096 DBusPreallocatedSend *preallocated)
1098 DBusList *link = (DBusList*) preallocated;
1099 _dbus_assert (link->data == connection);
1100 _dbus_list_free_link (link);
1104 * Sends a message using preallocated resources. This function cannot fail.
1105 * It works identically to dbus_connection_send() in other respects.
1106 * Preallocated resources comes from dbus_connection_preallocate_send().
1107 * This function "consumes" the preallocated resources, they need not
1108 * be freed separately.
1110 * @param connection the connection
1111 * @param preallocated the preallocated resources
1112 * @param message the message to send
1113 * @param client_serial return location for client serial assigned to the message
1116 dbus_connection_send_preallocated (DBusConnection *connection,
1117 DBusPreallocatedSend *preallocated,
1118 DBusMessage *message,
1119 dbus_int32_t *client_serial)
1121 DBusList *link = (DBusList*) preallocated;
1122 dbus_int32_t serial;
1124 _dbus_assert (link->data == connection);
1125 _dbus_assert (dbus_message_get_name (message) != NULL);
1127 dbus_mutex_lock (connection->mutex);
1129 link->data = message;
1130 _dbus_list_prepend_link (&connection->outgoing_messages,
1133 dbus_message_ref (message);
1134 connection->n_outgoing += 1;
1136 _dbus_verbose ("Message %p (%s) added to outgoing queue %p, %d pending to send\n",
1138 dbus_message_get_name (message),
1140 connection->n_outgoing);
1142 if (dbus_message_get_serial (message) == -1)
1144 serial = _dbus_connection_get_next_client_serial (connection);
1145 _dbus_message_set_serial (message, serial);
1149 *client_serial = dbus_message_get_serial (message);
1151 _dbus_message_lock (message);
1153 if (connection->n_outgoing == 1)
1154 _dbus_transport_messages_pending (connection->transport,
1155 connection->n_outgoing);
1157 _dbus_connection_wakeup_mainloop (connection);
1159 dbus_mutex_unlock (connection->mutex);
1163 * Adds a message to the outgoing message queue. Does not block to
1164 * write the message to the network; that happens asynchronously. To
1165 * force the message to be written, call dbus_connection_flush().
1166 * Because this only queues the message, the only reason it can
1167 * fail is lack of memory. Even if the connection is disconnected,
1168 * no error will be returned.
1170 * If the function fails due to lack of memory, it returns #FALSE.
1171 * The function will never fail for other reasons; even if the
1172 * connection is disconnected, you can queue an outgoing message,
1173 * though obviously it won't be sent.
1175 * @param connection the connection.
1176 * @param message the message to write.
1177 * @param client_serial return location for client serial.
1178 * @returns #TRUE on success.
1181 dbus_connection_send (DBusConnection *connection,
1182 DBusMessage *message,
1183 dbus_int32_t *client_serial)
1185 DBusPreallocatedSend *preallocated;
1187 preallocated = dbus_connection_preallocate_send (connection);
1188 if (preallocated == NULL)
1194 dbus_connection_send_preallocated (connection, preallocated, message, client_serial);
1200 reply_handler_timeout (void *data)
1202 DBusConnection *connection;
1203 ReplyHandlerData *reply_handler_data = data;
1204 DBusDispatchStatus status;
1206 connection = reply_handler_data->connection;
1208 dbus_mutex_lock (connection->mutex);
1209 if (reply_handler_data->timeout_link)
1211 _dbus_connection_queue_synthesized_message_link (connection,
1212 reply_handler_data->timeout_link);
1213 reply_handler_data->timeout_link = NULL;
1216 _dbus_connection_remove_timeout (connection,
1217 reply_handler_data->timeout);
1218 reply_handler_data->timeout_added = FALSE;
1220 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1222 dbus_mutex_unlock (connection->mutex);
1224 _dbus_connection_update_dispatch_status_locked (connection, status);
1230 reply_handler_data_free (ReplyHandlerData *data)
1235 if (data->timeout_added)
1236 _dbus_connection_remove_timeout_locked (data->connection,
1239 if (data->connection_added)
1240 _dbus_message_handler_remove_connection (data->handler,
1243 if (data->timeout_link)
1245 dbus_message_unref ((DBusMessage *)data->timeout_link->data);
1246 _dbus_list_free_link (data->timeout_link);
1249 dbus_message_handler_unref (data->handler);
1255 * Queues a message to send, as with dbus_connection_send_message(),
1256 * but also sets up a DBusMessageHandler to receive a reply to the
1257 * message. If no reply is received in the given timeout_milliseconds,
1258 * expires the pending reply and sends the DBusMessageHandler a
1259 * synthetic error reply (generated in-process, not by the remote
1260 * application) indicating that a timeout occurred.
1262 * Reply handlers see their replies after message filters see them,
1263 * but before message handlers added with
1264 * dbus_connection_register_handler() see them, regardless of the
1265 * reply message's name. Reply handlers are only handed a single
1266 * message as a reply, after one reply has been seen the handler is
1267 * removed. If a filter filters out the reply before the handler sees
1268 * it, the reply is immediately timed out and a timeout error reply is
1269 * generated. If a filter removes the timeout error reply then the
1270 * reply handler will never be called. Filters should not do this.
1272 * If #NULL is passed for the reply_handler, the timeout reply will
1273 * still be generated and placed into the message queue, but no
1274 * specific message handler will receive the reply.
1276 * If -1 is passed for the timeout, a sane default timeout is used. -1
1277 * is typically the best value for the timeout for this reason, unless
1278 * you want a very short or very long timeout. There is no way to
1279 * avoid a timeout entirely, other than passing INT_MAX for the
1280 * timeout to postpone it indefinitely.
1282 * @param connection the connection
1283 * @param message the message to send
1284 * @param reply_handler message handler expecting the reply, or #NULL
1285 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1286 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1290 dbus_connection_send_with_reply (DBusConnection *connection,
1291 DBusMessage *message,
1292 DBusMessageHandler *reply_handler,
1293 int timeout_milliseconds)
1295 DBusTimeout *timeout;
1296 ReplyHandlerData *data;
1298 DBusList *reply_link;
1299 dbus_int32_t serial = -1;
1301 if (timeout_milliseconds == -1)
1302 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
1304 data = dbus_new0 (ReplyHandlerData, 1);
1309 timeout = _dbus_timeout_new (timeout_milliseconds, reply_handler_timeout,
1314 reply_handler_data_free (data);
1318 dbus_mutex_lock (connection->mutex);
1321 if (!_dbus_connection_add_timeout (connection, timeout))
1323 reply_handler_data_free (data);
1324 _dbus_timeout_unref (timeout);
1325 dbus_mutex_unlock (connection->mutex);
1329 /* The connection now owns the reference to the timeout. */
1330 _dbus_timeout_unref (timeout);
1332 data->timeout_added = TRUE;
1333 data->timeout = timeout;
1334 data->connection = connection;
1336 if (!_dbus_message_handler_add_connection (reply_handler, connection))
1338 dbus_mutex_unlock (connection->mutex);
1339 reply_handler_data_free (data);
1342 data->connection_added = TRUE;
1344 /* Assign a serial to the message */
1345 if (dbus_message_get_serial (message) == -1)
1347 serial = _dbus_connection_get_next_client_serial (connection);
1348 _dbus_message_set_serial (message, serial);
1351 data->handler = reply_handler;
1352 data->serial = serial;
1354 dbus_message_handler_ref (reply_handler);
1356 reply = dbus_message_new_error_reply (message, DBUS_ERROR_NO_REPLY,
1357 "No reply within specified time");
1360 dbus_mutex_unlock (connection->mutex);
1361 reply_handler_data_free (data);
1365 reply_link = _dbus_list_alloc_link (reply);
1368 dbus_mutex_unlock (connection->mutex);
1369 dbus_message_unref (reply);
1370 reply_handler_data_free (data);
1374 data->timeout_link = reply_link;
1376 /* Insert the serial in the pending replies hash. */
1377 if (!_dbus_hash_table_insert_int (connection->pending_replies, serial, data))
1379 dbus_mutex_unlock (connection->mutex);
1380 reply_handler_data_free (data);
1384 dbus_mutex_unlock (connection->mutex);
1386 if (!dbus_connection_send (connection, message, NULL))
1388 /* This will free the handler data too */
1389 _dbus_hash_table_remove_int (connection->pending_replies, serial);
1398 check_for_reply_unlocked (DBusConnection *connection,
1399 dbus_int32_t client_serial)
1403 link = _dbus_list_get_first_link (&connection->incoming_messages);
1405 while (link != NULL)
1407 DBusMessage *reply = link->data;
1409 if (dbus_message_get_reply_serial (reply) == client_serial)
1411 _dbus_list_remove_link (&connection->incoming_messages, link);
1412 connection->n_incoming -= 1;
1413 dbus_message_ref (reply);
1416 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1423 * Sends a message and blocks a certain time period while waiting for a reply.
1424 * This function does not dispatch any message handlers until the main loop
1425 * has been reached. This function is used to do non-reentrant "method calls."
1426 * If a reply is received, it is returned, and removed from the incoming
1427 * message queue. If it is not received, #NULL is returned and the
1428 * error is set to #DBUS_ERROR_NO_REPLY. If something else goes
1429 * wrong, result is set to whatever is appropriate, such as
1430 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1432 * @todo could use performance improvements (it keeps scanning
1433 * the whole message queue for example) and has thread issues,
1434 * see comments in source
1436 * @param connection the connection
1437 * @param message the message to send
1438 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1439 * @param error return location for error message
1440 * @returns the message that is the reply or #NULL with an error code if the
1444 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1445 DBusMessage *message,
1446 int timeout_milliseconds,
1449 dbus_int32_t client_serial;
1450 long start_tv_sec, start_tv_usec;
1451 long end_tv_sec, end_tv_usec;
1452 long tv_sec, tv_usec;
1453 DBusDispatchStatus status;
1455 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1457 if (timeout_milliseconds == -1)
1458 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
1460 /* it would probably seem logical to pass in _DBUS_INT_MAX
1461 * for infinite timeout, but then math below would get
1462 * all overflow-prone, so smack that down.
1464 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1465 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1467 if (!dbus_connection_send (connection, message, &client_serial))
1469 _DBUS_SET_OOM (error);
1475 /* Flush message queue */
1476 dbus_connection_flush (connection);
1478 dbus_mutex_lock (connection->mutex);
1480 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1481 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1482 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1483 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1484 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1486 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %d from %ld sec %ld usec to %ld sec %ld usec\n",
1487 timeout_milliseconds,
1489 start_tv_sec, start_tv_usec,
1490 end_tv_sec, end_tv_usec);
1492 /* Now we wait... */
1493 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1494 * gets the message before we do?
1496 /* always block at least once as we know we don't have the reply yet */
1497 _dbus_connection_do_iteration (connection,
1498 DBUS_ITERATION_DO_READING |
1499 DBUS_ITERATION_BLOCK,
1500 timeout_milliseconds);
1504 /* queue messages and get status */
1505 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1507 if (status == DBUS_DISPATCH_DATA_REMAINS)
1511 reply = check_for_reply_unlocked (connection, client_serial);
1514 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1516 dbus_mutex_unlock (connection->mutex);
1518 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply %s\n",
1519 dbus_message_get_name (reply));
1521 _dbus_connection_update_dispatch_status_locked (connection, status);
1527 _dbus_get_current_time (&tv_sec, &tv_usec);
1529 if (tv_sec < start_tv_sec)
1530 ; /* clock set backward, bail out */
1531 else if (connection->disconnect_message_link == NULL)
1532 ; /* we're disconnected, bail out */
1533 else if (tv_sec < end_tv_sec ||
1534 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1536 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1537 (end_tv_usec - tv_usec) / 1000;
1538 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1539 _dbus_assert (timeout_milliseconds >= 0);
1541 if (status == DBUS_DISPATCH_NEED_MEMORY)
1543 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1544 * we may already have a reply in the buffer and just can't process
1547 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1549 if (timeout_milliseconds < 100)
1550 ; /* just busy loop */
1551 else if (timeout_milliseconds <= 1000)
1552 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1554 _dbus_sleep_milliseconds (1000);
1558 /* block again, we don't have the reply buffered yet. */
1559 _dbus_connection_do_iteration (connection,
1560 DBUS_ITERATION_DO_READING |
1561 DBUS_ITERATION_BLOCK,
1562 timeout_milliseconds);
1565 goto recheck_status;
1568 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
1569 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
1571 if (dbus_connection_get_is_connected (connection))
1572 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
1574 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
1576 dbus_mutex_unlock (connection->mutex);
1578 _dbus_connection_update_dispatch_status_locked (connection, status);
1584 * Blocks until the outgoing message queue is empty.
1586 * @param connection the connection.
1589 dbus_connection_flush (DBusConnection *connection)
1591 /* We have to specify DBUS_ITERATION_DO_READING here because
1592 * otherwise we could have two apps deadlock if they are both doing
1593 * a flush(), and the kernel buffers fill up. This could change the
1596 DBusDispatchStatus status;
1598 dbus_mutex_lock (connection->mutex);
1599 while (connection->n_outgoing > 0 &&
1600 dbus_connection_get_is_connected (connection))
1601 _dbus_connection_do_iteration (connection,
1602 DBUS_ITERATION_DO_READING |
1603 DBUS_ITERATION_DO_WRITING |
1604 DBUS_ITERATION_BLOCK,
1607 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1609 dbus_mutex_unlock (connection->mutex);
1611 _dbus_connection_update_dispatch_status_locked (connection, status);
1614 /* Call with mutex held. Will drop it while waiting and re-acquire
1618 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
1620 _dbus_assert (connection->message_borrowed != NULL);
1622 while (connection->message_borrowed != NULL)
1623 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
1627 * Returns the first-received message from the incoming message queue,
1628 * leaving it in the queue. If the queue is empty, returns #NULL.
1630 * The caller does not own a reference to the returned message, and must
1631 * either return it using dbus_connection_return_message or keep it after
1632 * calling dbus_connection_steal_borrowed_message. No one can get at the
1633 * message while its borrowed, so return it as quickly as possible and
1634 * don't keep a reference to it after returning it. If you need to keep
1635 * the message, make a copy of it.
1637 * @param connection the connection.
1638 * @returns next message in the incoming queue.
1641 dbus_connection_borrow_message (DBusConnection *connection)
1643 DBusMessage *message;
1644 DBusDispatchStatus status;
1646 /* this is called for the side effect that it queues
1647 * up any messages from the transport
1649 status = dbus_connection_get_dispatch_status (connection);
1650 if (status != DBUS_DISPATCH_DATA_REMAINS)
1653 dbus_mutex_lock (connection->mutex);
1655 if (connection->message_borrowed != NULL)
1656 _dbus_connection_wait_for_borrowed (connection);
1658 message = _dbus_list_get_first (&connection->incoming_messages);
1661 connection->message_borrowed = message;
1663 dbus_mutex_unlock (connection->mutex);
1671 dbus_connection_return_message (DBusConnection *connection,
1672 DBusMessage *message)
1674 dbus_mutex_lock (connection->mutex);
1676 _dbus_assert (message == connection->message_borrowed);
1678 connection->message_borrowed = NULL;
1679 dbus_condvar_wake_all (connection->message_returned_cond);
1681 dbus_mutex_unlock (connection->mutex);
1688 dbus_connection_steal_borrowed_message (DBusConnection *connection,
1689 DBusMessage *message)
1691 DBusMessage *pop_message;
1693 dbus_mutex_lock (connection->mutex);
1695 _dbus_assert (message == connection->message_borrowed);
1697 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
1698 _dbus_assert (message == pop_message);
1700 connection->n_incoming -= 1;
1702 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
1703 message, connection->n_incoming);
1705 connection->message_borrowed = NULL;
1706 dbus_condvar_wake_all (connection->message_returned_cond);
1708 dbus_mutex_unlock (connection->mutex);
1711 /* See dbus_connection_pop_message, but requires the caller to own
1712 * the lock before calling. May drop the lock while running.
1715 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
1717 if (connection->message_borrowed != NULL)
1718 _dbus_connection_wait_for_borrowed (connection);
1720 if (connection->n_incoming > 0)
1724 link = _dbus_list_pop_first_link (&connection->incoming_messages);
1725 connection->n_incoming -= 1;
1727 _dbus_verbose ("Message %p (%s) removed from incoming queue %p, %d incoming\n",
1728 link->data, dbus_message_get_name (link->data),
1729 connection, connection->n_incoming);
1737 /* See dbus_connection_pop_message, but requires the caller to own
1738 * the lock before calling. May drop the lock while running.
1741 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
1745 link = _dbus_connection_pop_message_link_unlocked (connection);
1749 DBusMessage *message;
1751 message = link->data;
1753 _dbus_list_free_link (link);
1763 * Returns the first-received message from the incoming message queue,
1764 * removing it from the queue. The caller owns a reference to the
1765 * returned message. If the queue is empty, returns #NULL.
1767 * @param connection the connection.
1768 * @returns next message in the incoming queue.
1771 dbus_connection_pop_message (DBusConnection *connection)
1773 DBusMessage *message;
1774 DBusDispatchStatus status;
1776 /* this is called for the side effect that it queues
1777 * up any messages from the transport
1779 status = dbus_connection_get_dispatch_status (connection);
1780 if (status != DBUS_DISPATCH_DATA_REMAINS)
1783 dbus_mutex_lock (connection->mutex);
1785 message = _dbus_connection_pop_message_unlocked (connection);
1787 _dbus_verbose ("Returning popped message %p\n", message);
1789 dbus_mutex_unlock (connection->mutex);
1795 * Acquire the dispatcher. This must be done before dispatching
1796 * messages in order to guarantee the right order of
1797 * message delivery. May sleep and drop the connection mutex
1798 * while waiting for the dispatcher.
1800 * @param connection the connection.
1803 _dbus_connection_acquire_dispatch (DBusConnection *connection)
1805 if (connection->dispatch_acquired)
1806 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
1807 _dbus_assert (!connection->dispatch_acquired);
1809 connection->dispatch_acquired = TRUE;
1813 * Release the dispatcher when you're done with it. Only call
1814 * after you've acquired the dispatcher. Wakes up at most one
1815 * thread currently waiting to acquire the dispatcher.
1817 * @param connection the connection.
1820 _dbus_connection_release_dispatch (DBusConnection *connection)
1822 _dbus_assert (connection->dispatch_acquired);
1824 connection->dispatch_acquired = FALSE;
1825 dbus_condvar_wake_one (connection->dispatch_cond);
1829 _dbus_connection_failed_pop (DBusConnection *connection,
1830 DBusList *message_link)
1832 _dbus_list_prepend_link (&connection->incoming_messages,
1834 connection->n_incoming += 1;
1837 static DBusDispatchStatus
1838 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
1840 if (connection->n_incoming > 0)
1841 return DBUS_DISPATCH_DATA_REMAINS;
1842 else if (!_dbus_transport_queue_messages (connection->transport))
1843 return DBUS_DISPATCH_NEED_MEMORY;
1846 DBusDispatchStatus status;
1848 status = _dbus_transport_get_dispatch_status (connection->transport);
1850 if (status != DBUS_DISPATCH_COMPLETE)
1852 else if (connection->n_incoming > 0)
1853 return DBUS_DISPATCH_DATA_REMAINS;
1855 return DBUS_DISPATCH_COMPLETE;
1860 _dbus_connection_update_dispatch_status_locked (DBusConnection *connection,
1861 DBusDispatchStatus new_status)
1863 dbus_bool_t changed;
1864 DBusDispatchStatusFunction function;
1867 dbus_mutex_lock (connection->mutex);
1868 _dbus_connection_ref_unlocked (connection);
1870 changed = new_status != connection->last_dispatch_status;
1872 connection->last_dispatch_status = new_status;
1874 function = connection->dispatch_status_function;
1875 data = connection->dispatch_status_data;
1877 dbus_mutex_unlock (connection->mutex);
1879 if (changed && function)
1881 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
1882 connection, new_status,
1883 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
1884 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
1885 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
1887 (* function) (connection, new_status, data);
1890 dbus_connection_unref (connection);
1894 * Gets the current state (what we would currently return
1895 * from dbus_connection_dispatch()) but doesn't actually
1896 * dispatch any messages.
1898 * @param connection the connection.
1899 * @returns current dispatch status
1902 dbus_connection_get_dispatch_status (DBusConnection *connection)
1904 DBusDispatchStatus status;
1906 dbus_mutex_lock (connection->mutex);
1908 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1910 dbus_mutex_unlock (connection->mutex);
1916 * Processes data buffered while handling watches, queueing zero or
1917 * more incoming messages. Then pops the first-received message from
1918 * the current incoming message queue, runs any handlers for it, and
1919 * unrefs the message. Returns a status indicating whether messages/data
1920 * remain, more memory is needed, or all data has been processed.
1922 * @param connection the connection
1923 * @returns dispatch status
1926 dbus_connection_dispatch (DBusConnection *connection)
1928 DBusMessageHandler *handler;
1929 DBusMessage *message;
1930 DBusList *link, *filter_list_copy, *message_link;
1931 DBusHandlerResult result;
1932 ReplyHandlerData *reply_handler_data;
1934 dbus_int32_t reply_serial;
1935 DBusDispatchStatus status;
1937 status = dbus_connection_get_dispatch_status (connection);
1938 if (status != DBUS_DISPATCH_DATA_REMAINS)
1940 _dbus_connection_update_dispatch_status_locked (connection, status);
1944 dbus_mutex_lock (connection->mutex);
1946 /* We need to ref the connection since the callback could potentially
1947 * drop the last ref to it
1949 _dbus_connection_ref_unlocked (connection);
1951 _dbus_connection_acquire_dispatch (connection);
1953 /* This call may drop the lock during the execution (if waiting for
1954 * borrowed messages to be returned) but the order of message
1955 * dispatch if several threads call dispatch() is still
1956 * protected by the lock, since only one will get the lock, and that
1957 * one will finish the message dispatching
1959 message_link = _dbus_connection_pop_message_link_unlocked (connection);
1960 if (message_link == NULL)
1962 /* another thread dispatched our stuff */
1964 _dbus_connection_release_dispatch (connection);
1965 dbus_mutex_unlock (connection->mutex);
1967 status = dbus_connection_get_dispatch_status (connection);
1969 _dbus_connection_update_dispatch_status_locked (connection, status);
1971 dbus_connection_unref (connection);
1976 message = message_link->data;
1978 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
1980 reply_serial = dbus_message_get_reply_serial (message);
1981 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
1984 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
1986 _dbus_connection_release_dispatch (connection);
1987 dbus_mutex_unlock (connection->mutex);
1988 _dbus_connection_failed_pop (connection, message_link);
1990 _dbus_connection_update_dispatch_status_locked (connection, DBUS_DISPATCH_NEED_MEMORY);
1992 dbus_connection_unref (connection);
1994 return DBUS_DISPATCH_NEED_MEMORY;
1997 _dbus_list_foreach (&filter_list_copy,
1998 (DBusForeachFunction)dbus_message_handler_ref,
2001 /* We're still protected from dispatch() reentrancy here
2002 * since we acquired the dispatcher
2004 dbus_mutex_unlock (connection->mutex);
2006 link = _dbus_list_get_first_link (&filter_list_copy);
2007 while (link != NULL)
2009 DBusMessageHandler *handler = link->data;
2010 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2012 _dbus_verbose (" running filter on message %p\n", message);
2013 result = _dbus_message_handler_handle_message (handler, connection,
2016 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2022 _dbus_list_foreach (&filter_list_copy,
2023 (DBusForeachFunction)dbus_message_handler_unref,
2025 _dbus_list_clear (&filter_list_copy);
2027 dbus_mutex_lock (connection->mutex);
2029 /* Did a reply we were waiting on get filtered? */
2030 if (reply_handler_data && result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2032 /* Queue the timeout immediately! */
2033 if (reply_handler_data->timeout_link)
2035 _dbus_connection_queue_synthesized_message_link (connection,
2036 reply_handler_data->timeout_link);
2037 reply_handler_data->timeout_link = NULL;
2041 /* We already queued the timeout? Then it was filtered! */
2042 _dbus_warn ("The timeout error with reply serial %d was filtered, so the reply handler will never be called.\n", reply_serial);
2046 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2049 if (reply_handler_data)
2051 dbus_mutex_unlock (connection->mutex);
2053 _dbus_verbose (" running reply handler on message %p\n", message);
2055 result = _dbus_message_handler_handle_message (reply_handler_data->handler,
2056 connection, message);
2057 reply_handler_data_free (reply_handler_data);
2058 dbus_mutex_lock (connection->mutex);
2062 name = dbus_message_get_name (message);
2065 handler = _dbus_hash_table_lookup_string (connection->handler_table,
2067 if (handler != NULL)
2069 /* We're still protected from dispatch() reentrancy here
2070 * since we acquired the dispatcher
2072 dbus_mutex_unlock (connection->mutex);
2074 _dbus_verbose (" running app handler on message %p (%s)\n",
2075 message, dbus_message_get_name (message));
2077 result = _dbus_message_handler_handle_message (handler, connection,
2079 dbus_mutex_lock (connection->mutex);
2080 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2085 _dbus_verbose (" done dispatching %p (%s)\n", message,
2086 dbus_message_get_name (message));
2089 _dbus_connection_release_dispatch (connection);
2090 dbus_mutex_unlock (connection->mutex);
2091 _dbus_list_free_link (message_link);
2092 dbus_message_unref (message); /* don't want the message to count in max message limits
2093 * in computing dispatch status
2096 status = dbus_connection_get_dispatch_status (connection);
2098 _dbus_connection_update_dispatch_status_locked (connection, status);
2100 dbus_connection_unref (connection);
2106 * Sets the watch functions for the connection. These functions are
2107 * responsible for making the application's main loop aware of file
2108 * descriptors that need to be monitored for events, using select() or
2109 * poll(). When using Qt, typically the DBusAddWatchFunction would
2110 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2111 * could call g_io_add_watch(), or could be used as part of a more
2112 * elaborate GSource. Note that when a watch is added, it may
2115 * The DBusWatchToggledFunction notifies the application that the
2116 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2117 * to check this. A disabled watch should have no effect, and enabled
2118 * watch should be added to the main loop. This feature is used
2119 * instead of simply adding/removing the watch because
2120 * enabling/disabling can be done without memory allocation. The
2121 * toggled function may be NULL if a main loop re-queries
2122 * dbus_watch_get_enabled() every time anyway.
2124 * The DBusWatch can be queried for the file descriptor to watch using
2125 * dbus_watch_get_fd(), and for the events to watch for using
2126 * dbus_watch_get_flags(). The flags returned by
2127 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2128 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2129 * all watches implicitly include a watch for hangups, errors, and
2130 * other exceptional conditions.
2132 * Once a file descriptor becomes readable or writable, or an exception
2133 * occurs, dbus_connection_handle_watch() should be called to
2134 * notify the connection of the file descriptor's condition.
2136 * dbus_connection_handle_watch() cannot be called during the
2137 * DBusAddWatchFunction, as the connection will not be ready to handle
2140 * It is not allowed to reference a DBusWatch after it has been passed
2141 * to remove_function.
2143 * If #FALSE is returned due to lack of memory, the failure may be due
2144 * to a #FALSE return from the new add_function. If so, the
2145 * add_function may have been called successfully one or more times,
2146 * but the remove_function will also have been called to remove any
2147 * successful adds. i.e. if #FALSE is returned the net result
2148 * should be that dbus_connection_set_watch_functions() has no effect,
2149 * but the add_function and remove_function may have been called.
2151 * @todo We need to drop the lock when we call the
2152 * add/remove/toggled functions which can be a side effect
2153 * of setting the watch functions.
2155 * @param connection the connection.
2156 * @param add_function function to begin monitoring a new descriptor.
2157 * @param remove_function function to stop monitoring a descriptor.
2158 * @param toggled_function function to notify of enable/disable
2159 * @param data data to pass to add_function and remove_function.
2160 * @param free_data_function function to be called to free the data.
2161 * @returns #FALSE on failure (no memory)
2164 dbus_connection_set_watch_functions (DBusConnection *connection,
2165 DBusAddWatchFunction add_function,
2166 DBusRemoveWatchFunction remove_function,
2167 DBusWatchToggledFunction toggled_function,
2169 DBusFreeFunction free_data_function)
2173 dbus_mutex_lock (connection->mutex);
2174 /* ref connection for slightly better reentrancy */
2175 _dbus_connection_ref_unlocked (connection);
2177 /* FIXME this can call back into user code, and we need to drop the
2178 * connection lock when it does.
2180 retval = _dbus_watch_list_set_functions (connection->watches,
2181 add_function, remove_function,
2183 data, free_data_function);
2185 dbus_mutex_unlock (connection->mutex);
2186 /* drop our paranoid refcount */
2187 dbus_connection_unref (connection);
2193 * Sets the timeout functions for the connection. These functions are
2194 * responsible for making the application's main loop aware of timeouts.
2195 * When using Qt, typically the DBusAddTimeoutFunction would create a
2196 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2199 * The DBusTimeoutToggledFunction notifies the application that the
2200 * timeout has been enabled or disabled. Call
2201 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2202 * have no effect, and enabled timeout should be added to the main
2203 * loop. This feature is used instead of simply adding/removing the
2204 * timeout because enabling/disabling can be done without memory
2205 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2206 * to enable and disable. The toggled function may be NULL if a main
2207 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2209 * The DBusTimeout can be queried for the timer interval using
2210 * dbus_timeout_get_interval(). dbus_timeout_handle() should
2211 * be called repeatedly, each time the interval elapses, starting
2212 * after it has elapsed once. The timeout stops firing when
2213 * it is removed with the given remove_function.
2215 * @param connection the connection.
2216 * @param add_function function to add a timeout.
2217 * @param remove_function function to remove a timeout.
2218 * @param toggled_function function to notify of enable/disable
2219 * @param data data to pass to add_function and remove_function.
2220 * @param free_data_function function to be called to free the data.
2221 * @returns #FALSE on failure (no memory)
2224 dbus_connection_set_timeout_functions (DBusConnection *connection,
2225 DBusAddTimeoutFunction add_function,
2226 DBusRemoveTimeoutFunction remove_function,
2227 DBusTimeoutToggledFunction toggled_function,
2229 DBusFreeFunction free_data_function)
2233 dbus_mutex_lock (connection->mutex);
2234 /* ref connection for slightly better reentrancy */
2235 _dbus_connection_ref_unlocked (connection);
2237 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2238 add_function, remove_function,
2240 data, free_data_function);
2242 dbus_mutex_unlock (connection->mutex);
2243 /* drop our paranoid refcount */
2244 dbus_connection_unref (connection);
2250 * Sets the mainloop wakeup function for the connection. Thi function is
2251 * responsible for waking up the main loop (if its sleeping) when some some
2252 * change has happened to the connection that the mainloop needs to reconsiders
2253 * (e.g. a message has been queued for writing).
2254 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2255 * When using GLib, it would call g_main_context_wakeup().
2258 * @param connection the connection.
2259 * @param wakeup_main_function function to wake up the mainloop
2260 * @param data data to pass wakeup_main_function
2261 * @param free_data_function function to be called to free the data.
2264 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2265 DBusWakeupMainFunction wakeup_main_function,
2267 DBusFreeFunction free_data_function)
2270 DBusFreeFunction old_free_data;
2272 dbus_mutex_lock (connection->mutex);
2273 old_data = connection->wakeup_main_data;
2274 old_free_data = connection->free_wakeup_main_data;
2276 connection->wakeup_main_function = wakeup_main_function;
2277 connection->wakeup_main_data = data;
2278 connection->free_wakeup_main_data = free_data_function;
2280 dbus_mutex_unlock (connection->mutex);
2282 /* Callback outside the lock */
2284 (*old_free_data) (old_data);
2288 * Set a function to be invoked when the dispatch status changes.
2289 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
2290 * dbus_connection_dispatch() needs to be called to process incoming
2291 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
2292 * from inside the DBusDispatchStatusFunction. Indeed, almost
2293 * any reentrancy in this function is a bad idea. Instead,
2294 * the DBusDispatchStatusFunction should simply save an indication
2295 * that messages should be dispatched later, when the main loop
2298 * @param connection the connection
2299 * @param function function to call on dispatch status changes
2300 * @param data data for function
2301 * @param free_data_function free the function data
2304 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
2305 DBusDispatchStatusFunction function,
2307 DBusFreeFunction free_data_function)
2310 DBusFreeFunction old_free_data;
2312 dbus_mutex_lock (connection->mutex);
2313 old_data = connection->dispatch_status_data;
2314 old_free_data = connection->free_dispatch_status_data;
2316 connection->dispatch_status_function = function;
2317 connection->dispatch_status_data = data;
2318 connection->free_dispatch_status_data = free_data_function;
2320 dbus_mutex_unlock (connection->mutex);
2322 /* Callback outside the lock */
2324 (*old_free_data) (old_data);
2328 * Called to notify the connection when a previously-added watch
2329 * is ready for reading or writing, or has an exception such
2332 * If this function returns #FALSE, then the file descriptor may still
2333 * be ready for reading or writing, but more memory is needed in order
2334 * to do the reading or writing. If you ignore the #FALSE return, your
2335 * application may spin in a busy loop on the file descriptor until
2336 * memory becomes available, but nothing more catastrophic should
2339 * @param connection the connection.
2340 * @param watch the watch.
2341 * @param condition the current condition of the file descriptors being watched.
2342 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
2345 dbus_connection_handle_watch (DBusConnection *connection,
2347 unsigned int condition)
2350 DBusDispatchStatus status;
2352 dbus_mutex_lock (connection->mutex);
2353 _dbus_connection_acquire_io_path (connection, -1);
2354 retval = _dbus_transport_handle_watch (connection->transport,
2356 _dbus_connection_release_io_path (connection);
2358 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2360 dbus_mutex_unlock (connection->mutex);
2362 _dbus_connection_update_dispatch_status_locked (connection, status);
2368 * Gets the UNIX user ID of the connection if any.
2369 * Returns #TRUE if the uid is filled in.
2370 * Always returns #FALSE on non-UNIX platforms.
2371 * Always returns #FALSE prior to authenticating the
2374 * @param connection the connection
2375 * @param uid return location for the user ID
2376 * @returns #TRUE if uid is filled in with a valid user ID
2379 dbus_connection_get_unix_user (DBusConnection *connection,
2384 dbus_mutex_lock (connection->mutex);
2386 if (!_dbus_transport_get_is_authenticated (connection->transport))
2389 result = _dbus_transport_get_unix_user (connection->transport,
2391 dbus_mutex_unlock (connection->mutex);
2397 * Sets a predicate function used to determine whether a given user ID
2398 * is allowed to connect. When an incoming connection has
2399 * authenticated with a particular user ID, this function is called;
2400 * if it returns #TRUE, the connection is allowed to proceed,
2401 * otherwise the connection is disconnected.
2403 * If the function is set to #NULL (as it is by default), then
2404 * only the same UID as the server process will be allowed to
2407 * @param connection the connection
2408 * @param function the predicate
2409 * @param data data to pass to the predicate
2410 * @param free_data_function function to free the data
2413 dbus_connection_set_unix_user_function (DBusConnection *connection,
2414 DBusAllowUnixUserFunction function,
2416 DBusFreeFunction free_data_function)
2418 void *old_data = NULL;
2419 DBusFreeFunction old_free_function = NULL;
2421 dbus_mutex_lock (connection->mutex);
2422 _dbus_transport_set_unix_user_function (connection->transport,
2423 function, data, free_data_function,
2424 &old_data, &old_free_function);
2425 dbus_mutex_unlock (connection->mutex);
2427 if (old_free_function != NULL)
2428 (* old_free_function) (old_data);
2432 * Adds a message filter. Filters are handlers that are run on
2433 * all incoming messages, prior to the normal handlers
2434 * registered with dbus_connection_register_handler().
2435 * Filters are run in the order that they were added.
2436 * The same handler can be added as a filter more than once, in
2437 * which case it will be run more than once.
2438 * Filters added during a filter callback won't be run on the
2439 * message being processed.
2441 * @param connection the connection
2442 * @param handler the handler
2443 * @returns #TRUE on success, #FALSE if not enough memory.
2446 dbus_connection_add_filter (DBusConnection *connection,
2447 DBusMessageHandler *handler)
2449 dbus_mutex_lock (connection->mutex);
2450 if (!_dbus_message_handler_add_connection (handler, connection))
2452 dbus_mutex_unlock (connection->mutex);
2456 if (!_dbus_list_append (&connection->filter_list,
2459 _dbus_message_handler_remove_connection (handler, connection);
2460 dbus_mutex_unlock (connection->mutex);
2464 dbus_mutex_unlock (connection->mutex);
2469 * Removes a previously-added message filter. It is a programming
2470 * error to call this function for a handler that has not
2471 * been added as a filter. If the given handler was added
2472 * more than once, only one instance of it will be removed
2473 * (the most recently-added instance).
2475 * @param connection the connection
2476 * @param handler the handler to remove
2480 dbus_connection_remove_filter (DBusConnection *connection,
2481 DBusMessageHandler *handler)
2483 dbus_mutex_lock (connection->mutex);
2484 if (!_dbus_list_remove_last (&connection->filter_list, handler))
2486 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
2487 dbus_mutex_unlock (connection->mutex);
2491 _dbus_message_handler_remove_connection (handler, connection);
2493 dbus_mutex_unlock (connection->mutex);
2497 * Registers a handler for a list of message names. A single handler
2498 * can be registered for any number of message names, but each message
2499 * name can only have one handler at a time. It's not allowed to call
2500 * this function with the name of a message that already has a
2501 * handler. If the function returns #FALSE, the handlers were not
2502 * registered due to lack of memory.
2504 * @todo the messages_to_handle arg may be more convenient if it's a
2505 * single string instead of an array. Though right now MessageHandler
2506 * is sort of designed to say be associated with an entire object with
2507 * multiple methods, that's why for example the connection only
2508 * weakrefs it. So maybe the "manual" API should be different.
2510 * @param connection the connection
2511 * @param handler the handler
2512 * @param messages_to_handle the messages to handle
2513 * @param n_messages the number of message names in messages_to_handle
2514 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
2518 dbus_connection_register_handler (DBusConnection *connection,
2519 DBusMessageHandler *handler,
2520 const char **messages_to_handle,
2525 dbus_mutex_lock (connection->mutex);
2527 while (i < n_messages)
2532 key = _dbus_strdup (messages_to_handle[i]);
2536 if (!_dbus_hash_iter_lookup (connection->handler_table,
2544 if (_dbus_hash_iter_get_value (&iter) != NULL)
2546 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
2547 messages_to_handle[i]);
2548 dbus_free (key); /* won't have replaced the old key with the new one */
2552 if (!_dbus_message_handler_add_connection (handler, connection))
2554 _dbus_hash_iter_remove_entry (&iter);
2555 /* key has freed on nuking the entry */
2559 _dbus_hash_iter_set_value (&iter, handler);
2564 dbus_mutex_unlock (connection->mutex);
2568 /* unregister everything registered so far,
2569 * so we don't fail partially
2571 dbus_connection_unregister_handler (connection,
2576 dbus_mutex_unlock (connection->mutex);
2581 * Unregisters a handler for a list of message names. The handlers
2582 * must have been previously registered.
2584 * @param connection the connection
2585 * @param handler the handler
2586 * @param messages_to_handle the messages to handle
2587 * @param n_messages the number of message names in messages_to_handle
2591 dbus_connection_unregister_handler (DBusConnection *connection,
2592 DBusMessageHandler *handler,
2593 const char **messages_to_handle,
2598 dbus_mutex_lock (connection->mutex);
2600 while (i < n_messages)
2604 if (!_dbus_hash_iter_lookup (connection->handler_table,
2605 (char*) messages_to_handle[i], FALSE,
2608 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
2609 messages_to_handle[i]);
2611 else if (_dbus_hash_iter_get_value (&iter) != handler)
2613 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
2614 messages_to_handle[i]);
2618 _dbus_hash_iter_remove_entry (&iter);
2619 _dbus_message_handler_remove_connection (handler, connection);
2625 dbus_mutex_unlock (connection->mutex);
2628 static DBusDataSlotAllocator slot_allocator;
2629 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
2632 * Allocates an integer ID to be used for storing application-specific
2633 * data on any DBusConnection. The allocated ID may then be used
2634 * with dbus_connection_set_data() and dbus_connection_get_data().
2635 * If allocation fails, -1 is returned. Again, the allocated
2636 * slot is global, i.e. all DBusConnection objects will
2637 * have a slot with the given integer ID reserved.
2639 * @returns -1 on failure, otherwise the data slot ID
2642 dbus_connection_allocate_data_slot (void)
2644 return _dbus_data_slot_allocator_alloc (&slot_allocator,
2645 _DBUS_LOCK_NAME (connection_slots));
2649 * Deallocates a global ID for connection data slots.
2650 * dbus_connection_get_data() and dbus_connection_set_data()
2651 * may no longer be used with this slot.
2652 * Existing data stored on existing DBusConnection objects
2653 * will be freed when the connection is finalized,
2654 * but may not be retrieved (and may only be replaced
2655 * if someone else reallocates the slot).
2657 * @param slot the slot to deallocate
2660 dbus_connection_free_data_slot (int slot)
2662 _dbus_data_slot_allocator_free (&slot_allocator, slot);
2666 * Stores a pointer on a DBusConnection, along
2667 * with an optional function to be used for freeing
2668 * the data when the data is set again, or when
2669 * the connection is finalized. The slot number
2670 * must have been allocated with dbus_connection_allocate_data_slot().
2672 * @param connection the connection
2673 * @param slot the slot number
2674 * @param data the data to store
2675 * @param free_data_func finalizer function for the data
2676 * @returns #TRUE if there was enough memory to store the data
2679 dbus_connection_set_data (DBusConnection *connection,
2682 DBusFreeFunction free_data_func)
2684 DBusFreeFunction old_free_func;
2688 dbus_mutex_lock (connection->mutex);
2690 retval = _dbus_data_slot_list_set (&slot_allocator,
2691 &connection->slot_list,
2692 slot, data, free_data_func,
2693 &old_free_func, &old_data);
2695 dbus_mutex_unlock (connection->mutex);
2699 /* Do the actual free outside the connection lock */
2701 (* old_free_func) (old_data);
2708 * Retrieves data previously set with dbus_connection_set_data().
2709 * The slot must still be allocated (must not have been freed).
2711 * @param connection the connection
2712 * @param slot the slot to get data from
2713 * @returns the data, or #NULL if not found
2716 dbus_connection_get_data (DBusConnection *connection,
2721 dbus_mutex_lock (connection->mutex);
2723 res = _dbus_data_slot_list_get (&slot_allocator,
2724 &connection->slot_list,
2727 dbus_mutex_unlock (connection->mutex);
2733 * This function sets a global flag for whether dbus_connection_new()
2734 * will set SIGPIPE behavior to SIG_IGN.
2736 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
2739 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
2741 _dbus_modify_sigpipe = will_modify_sigpipe;
2745 * Specifies the maximum size message this connection is allowed to
2746 * receive. Larger messages will result in disconnecting the
2749 * @param connection a #DBusConnection
2750 * @param size maximum message size the connection can receive, in bytes
2753 dbus_connection_set_max_message_size (DBusConnection *connection,
2756 dbus_mutex_lock (connection->mutex);
2757 _dbus_transport_set_max_message_size (connection->transport,
2759 dbus_mutex_unlock (connection->mutex);
2763 * Gets the value set by dbus_connection_set_max_message_size().
2765 * @param connection the connection
2766 * @returns the max size of a single message
2769 dbus_connection_get_max_message_size (DBusConnection *connection)
2772 dbus_mutex_lock (connection->mutex);
2773 res = _dbus_transport_get_max_message_size (connection->transport);
2774 dbus_mutex_unlock (connection->mutex);
2779 * Sets the maximum total number of bytes that can be used for all messages
2780 * received on this connection. Messages count toward the maximum until
2781 * they are finalized. When the maximum is reached, the connection will
2782 * not read more data until some messages are finalized.
2784 * The semantics of the maximum are: if outstanding messages are
2785 * already above the maximum, additional messages will not be read.
2786 * The semantics are not: if the next message would cause us to exceed
2787 * the maximum, we don't read it. The reason is that we don't know the
2788 * size of a message until after we read it.
2790 * Thus, the max live messages size can actually be exceeded
2791 * by up to the maximum size of a single message.
2793 * Also, if we read say 1024 bytes off the wire in a single read(),
2794 * and that contains a half-dozen small messages, we may exceed the
2795 * size max by that amount. But this should be inconsequential.
2797 * This does imply that we can't call read() with a buffer larger
2798 * than we're willing to exceed this limit by.
2800 * @param connection the connection
2801 * @param size the maximum size in bytes of all outstanding messages
2804 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
2807 dbus_mutex_lock (connection->mutex);
2808 _dbus_transport_set_max_live_messages_size (connection->transport,
2810 dbus_mutex_unlock (connection->mutex);
2814 * Gets the value set by dbus_connection_set_max_live_messages_size().
2816 * @param connection the connection
2817 * @returns the max size of all live messages
2820 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
2823 dbus_mutex_lock (connection->mutex);
2824 res = _dbus_transport_get_max_live_messages_size (connection->transport);
2825 dbus_mutex_unlock (connection->mutex);