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_do_iteration (connection,
1601 DBUS_ITERATION_DO_READING |
1602 DBUS_ITERATION_DO_WRITING |
1603 DBUS_ITERATION_BLOCK,
1606 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1608 dbus_mutex_unlock (connection->mutex);
1610 _dbus_connection_update_dispatch_status_locked (connection, status);
1613 /* Call with mutex held. Will drop it while waiting and re-acquire
1617 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
1619 _dbus_assert (connection->message_borrowed != NULL);
1621 while (connection->message_borrowed != NULL)
1622 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
1626 * Returns the first-received message from the incoming message queue,
1627 * leaving it in the queue. If the queue is empty, returns #NULL.
1629 * The caller does not own a reference to the returned message, and must
1630 * either return it using dbus_connection_return_message or keep it after
1631 * calling dbus_connection_steal_borrowed_message. No one can get at the
1632 * message while its borrowed, so return it as quickly as possible and
1633 * don't keep a reference to it after returning it. If you need to keep
1634 * the message, make a copy of it.
1636 * @param connection the connection.
1637 * @returns next message in the incoming queue.
1640 dbus_connection_borrow_message (DBusConnection *connection)
1642 DBusMessage *message;
1643 DBusDispatchStatus status;
1645 /* this is called for the side effect that it queues
1646 * up any messages from the transport
1648 status = dbus_connection_get_dispatch_status (connection);
1649 if (status != DBUS_DISPATCH_DATA_REMAINS)
1652 dbus_mutex_lock (connection->mutex);
1654 if (connection->message_borrowed != NULL)
1655 _dbus_connection_wait_for_borrowed (connection);
1657 message = _dbus_list_get_first (&connection->incoming_messages);
1660 connection->message_borrowed = message;
1662 dbus_mutex_unlock (connection->mutex);
1670 dbus_connection_return_message (DBusConnection *connection,
1671 DBusMessage *message)
1673 dbus_mutex_lock (connection->mutex);
1675 _dbus_assert (message == connection->message_borrowed);
1677 connection->message_borrowed = NULL;
1678 dbus_condvar_wake_all (connection->message_returned_cond);
1680 dbus_mutex_unlock (connection->mutex);
1687 dbus_connection_steal_borrowed_message (DBusConnection *connection,
1688 DBusMessage *message)
1690 DBusMessage *pop_message;
1692 dbus_mutex_lock (connection->mutex);
1694 _dbus_assert (message == connection->message_borrowed);
1696 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
1697 _dbus_assert (message == pop_message);
1699 connection->n_incoming -= 1;
1701 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
1702 message, connection->n_incoming);
1704 connection->message_borrowed = NULL;
1705 dbus_condvar_wake_all (connection->message_returned_cond);
1707 dbus_mutex_unlock (connection->mutex);
1710 /* See dbus_connection_pop_message, but requires the caller to own
1711 * the lock before calling. May drop the lock while running.
1714 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
1716 if (connection->message_borrowed != NULL)
1717 _dbus_connection_wait_for_borrowed (connection);
1719 if (connection->n_incoming > 0)
1723 link = _dbus_list_pop_first_link (&connection->incoming_messages);
1724 connection->n_incoming -= 1;
1726 _dbus_verbose ("Message %p (%s) removed from incoming queue %p, %d incoming\n",
1727 link->data, dbus_message_get_name (link->data),
1728 connection, connection->n_incoming);
1736 /* See dbus_connection_pop_message, but requires the caller to own
1737 * the lock before calling. May drop the lock while running.
1740 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
1744 link = _dbus_connection_pop_message_link_unlocked (connection);
1748 DBusMessage *message;
1750 message = link->data;
1752 _dbus_list_free_link (link);
1762 * Returns the first-received message from the incoming message queue,
1763 * removing it from the queue. The caller owns a reference to the
1764 * returned message. If the queue is empty, returns #NULL.
1766 * @param connection the connection.
1767 * @returns next message in the incoming queue.
1770 dbus_connection_pop_message (DBusConnection *connection)
1772 DBusMessage *message;
1773 DBusDispatchStatus status;
1775 /* this is called for the side effect that it queues
1776 * up any messages from the transport
1778 status = dbus_connection_get_dispatch_status (connection);
1779 if (status != DBUS_DISPATCH_DATA_REMAINS)
1782 dbus_mutex_lock (connection->mutex);
1784 message = _dbus_connection_pop_message_unlocked (connection);
1786 _dbus_verbose ("Returning popped message %p\n", message);
1788 dbus_mutex_unlock (connection->mutex);
1794 * Acquire the dispatcher. This must be done before dispatching
1795 * messages in order to guarantee the right order of
1796 * message delivery. May sleep and drop the connection mutex
1797 * while waiting for the dispatcher.
1799 * @param connection the connection.
1802 _dbus_connection_acquire_dispatch (DBusConnection *connection)
1804 if (connection->dispatch_acquired)
1805 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
1806 _dbus_assert (!connection->dispatch_acquired);
1808 connection->dispatch_acquired = TRUE;
1812 * Release the dispatcher when you're done with it. Only call
1813 * after you've acquired the dispatcher. Wakes up at most one
1814 * thread currently waiting to acquire the dispatcher.
1816 * @param connection the connection.
1819 _dbus_connection_release_dispatch (DBusConnection *connection)
1821 _dbus_assert (connection->dispatch_acquired);
1823 connection->dispatch_acquired = FALSE;
1824 dbus_condvar_wake_one (connection->dispatch_cond);
1828 _dbus_connection_failed_pop (DBusConnection *connection,
1829 DBusList *message_link)
1831 _dbus_list_prepend_link (&connection->incoming_messages,
1833 connection->n_incoming += 1;
1836 static DBusDispatchStatus
1837 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
1839 if (connection->n_incoming > 0)
1840 return DBUS_DISPATCH_DATA_REMAINS;
1841 else if (!_dbus_transport_queue_messages (connection->transport))
1842 return DBUS_DISPATCH_NEED_MEMORY;
1845 DBusDispatchStatus status;
1847 status = _dbus_transport_get_dispatch_status (connection->transport);
1849 if (status != DBUS_DISPATCH_COMPLETE)
1851 else if (connection->n_incoming > 0)
1852 return DBUS_DISPATCH_DATA_REMAINS;
1854 return DBUS_DISPATCH_COMPLETE;
1859 _dbus_connection_update_dispatch_status_locked (DBusConnection *connection,
1860 DBusDispatchStatus new_status)
1862 dbus_bool_t changed;
1863 DBusDispatchStatusFunction function;
1866 dbus_mutex_lock (connection->mutex);
1867 _dbus_connection_ref_unlocked (connection);
1869 changed = new_status != connection->last_dispatch_status;
1871 connection->last_dispatch_status = new_status;
1873 function = connection->dispatch_status_function;
1874 data = connection->dispatch_status_data;
1876 dbus_mutex_unlock (connection->mutex);
1878 if (changed && function)
1880 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
1881 connection, new_status,
1882 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
1883 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
1884 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
1886 (* function) (connection, new_status, data);
1889 dbus_connection_unref (connection);
1893 * Gets the current state (what we would currently return
1894 * from dbus_connection_dispatch()) but doesn't actually
1895 * dispatch any messages.
1897 * @param connection the connection.
1898 * @returns current dispatch status
1901 dbus_connection_get_dispatch_status (DBusConnection *connection)
1903 DBusDispatchStatus status;
1905 dbus_mutex_lock (connection->mutex);
1907 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1909 dbus_mutex_unlock (connection->mutex);
1915 * Processes data buffered while handling watches, queueing zero or
1916 * more incoming messages. Then pops the first-received message from
1917 * the current incoming message queue, runs any handlers for it, and
1918 * unrefs the message. Returns a status indicating whether messages/data
1919 * remain, more memory is needed, or all data has been processed.
1921 * @param connection the connection
1922 * @returns dispatch status
1925 dbus_connection_dispatch (DBusConnection *connection)
1927 DBusMessageHandler *handler;
1928 DBusMessage *message;
1929 DBusList *link, *filter_list_copy, *message_link;
1930 DBusHandlerResult result;
1931 ReplyHandlerData *reply_handler_data;
1933 dbus_int32_t reply_serial;
1934 DBusDispatchStatus status;
1936 status = dbus_connection_get_dispatch_status (connection);
1937 if (status != DBUS_DISPATCH_DATA_REMAINS)
1939 _dbus_connection_update_dispatch_status_locked (connection, status);
1943 dbus_mutex_lock (connection->mutex);
1945 /* We need to ref the connection since the callback could potentially
1946 * drop the last ref to it
1948 _dbus_connection_ref_unlocked (connection);
1950 _dbus_connection_acquire_dispatch (connection);
1952 /* This call may drop the lock during the execution (if waiting for
1953 * borrowed messages to be returned) but the order of message
1954 * dispatch if several threads call dispatch() is still
1955 * protected by the lock, since only one will get the lock, and that
1956 * one will finish the message dispatching
1958 message_link = _dbus_connection_pop_message_link_unlocked (connection);
1959 if (message_link == NULL)
1961 /* another thread dispatched our stuff */
1963 _dbus_connection_release_dispatch (connection);
1964 dbus_mutex_unlock (connection->mutex);
1966 status = dbus_connection_get_dispatch_status (connection);
1968 _dbus_connection_update_dispatch_status_locked (connection, status);
1970 dbus_connection_unref (connection);
1975 message = message_link->data;
1977 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
1979 reply_serial = dbus_message_get_reply_serial (message);
1980 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
1983 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
1985 _dbus_connection_release_dispatch (connection);
1986 dbus_mutex_unlock (connection->mutex);
1987 _dbus_connection_failed_pop (connection, message_link);
1989 _dbus_connection_update_dispatch_status_locked (connection, DBUS_DISPATCH_NEED_MEMORY);
1991 dbus_connection_unref (connection);
1993 return DBUS_DISPATCH_NEED_MEMORY;
1996 _dbus_list_foreach (&filter_list_copy,
1997 (DBusForeachFunction)dbus_message_handler_ref,
2000 /* We're still protected from dispatch() reentrancy here
2001 * since we acquired the dispatcher
2003 dbus_mutex_unlock (connection->mutex);
2005 link = _dbus_list_get_first_link (&filter_list_copy);
2006 while (link != NULL)
2008 DBusMessageHandler *handler = link->data;
2009 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2011 _dbus_verbose (" running filter on message %p\n", message);
2012 result = _dbus_message_handler_handle_message (handler, connection,
2015 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2021 _dbus_list_foreach (&filter_list_copy,
2022 (DBusForeachFunction)dbus_message_handler_unref,
2024 _dbus_list_clear (&filter_list_copy);
2026 dbus_mutex_lock (connection->mutex);
2028 /* Did a reply we were waiting on get filtered? */
2029 if (reply_handler_data && result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2031 /* Queue the timeout immediately! */
2032 if (reply_handler_data->timeout_link)
2034 _dbus_connection_queue_synthesized_message_link (connection,
2035 reply_handler_data->timeout_link);
2036 reply_handler_data->timeout_link = NULL;
2040 /* We already queued the timeout? Then it was filtered! */
2041 _dbus_warn ("The timeout error with reply serial %d was filtered, so the reply handler will never be called.\n", reply_serial);
2045 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2048 if (reply_handler_data)
2050 dbus_mutex_unlock (connection->mutex);
2052 _dbus_verbose (" running reply handler on message %p\n", message);
2054 result = _dbus_message_handler_handle_message (reply_handler_data->handler,
2055 connection, message);
2056 reply_handler_data_free (reply_handler_data);
2057 dbus_mutex_lock (connection->mutex);
2061 name = dbus_message_get_name (message);
2064 handler = _dbus_hash_table_lookup_string (connection->handler_table,
2066 if (handler != NULL)
2068 /* We're still protected from dispatch() reentrancy here
2069 * since we acquired the dispatcher
2071 dbus_mutex_unlock (connection->mutex);
2073 _dbus_verbose (" running app handler on message %p (%s)\n",
2074 message, dbus_message_get_name (message));
2076 result = _dbus_message_handler_handle_message (handler, connection,
2078 dbus_mutex_lock (connection->mutex);
2079 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2084 _dbus_verbose (" done dispatching %p (%s)\n", message,
2085 dbus_message_get_name (message));
2088 _dbus_connection_release_dispatch (connection);
2089 dbus_mutex_unlock (connection->mutex);
2090 _dbus_list_free_link (message_link);
2091 dbus_message_unref (message); /* don't want the message to count in max message limits
2092 * in computing dispatch status
2095 status = dbus_connection_get_dispatch_status (connection);
2097 _dbus_connection_update_dispatch_status_locked (connection, status);
2099 dbus_connection_unref (connection);
2105 * Sets the watch functions for the connection. These functions are
2106 * responsible for making the application's main loop aware of file
2107 * descriptors that need to be monitored for events, using select() or
2108 * poll(). When using Qt, typically the DBusAddWatchFunction would
2109 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2110 * could call g_io_add_watch(), or could be used as part of a more
2111 * elaborate GSource. Note that when a watch is added, it may
2114 * The DBusWatchToggledFunction notifies the application that the
2115 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2116 * to check this. A disabled watch should have no effect, and enabled
2117 * watch should be added to the main loop. This feature is used
2118 * instead of simply adding/removing the watch because
2119 * enabling/disabling can be done without memory allocation. The
2120 * toggled function may be NULL if a main loop re-queries
2121 * dbus_watch_get_enabled() every time anyway.
2123 * The DBusWatch can be queried for the file descriptor to watch using
2124 * dbus_watch_get_fd(), and for the events to watch for using
2125 * dbus_watch_get_flags(). The flags returned by
2126 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2127 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2128 * all watches implicitly include a watch for hangups, errors, and
2129 * other exceptional conditions.
2131 * Once a file descriptor becomes readable or writable, or an exception
2132 * occurs, dbus_connection_handle_watch() should be called to
2133 * notify the connection of the file descriptor's condition.
2135 * dbus_connection_handle_watch() cannot be called during the
2136 * DBusAddWatchFunction, as the connection will not be ready to handle
2139 * It is not allowed to reference a DBusWatch after it has been passed
2140 * to remove_function.
2142 * If #FALSE is returned due to lack of memory, the failure may be due
2143 * to a #FALSE return from the new add_function. If so, the
2144 * add_function may have been called successfully one or more times,
2145 * but the remove_function will also have been called to remove any
2146 * successful adds. i.e. if #FALSE is returned the net result
2147 * should be that dbus_connection_set_watch_functions() has no effect,
2148 * but the add_function and remove_function may have been called.
2150 * @todo We need to drop the lock when we call the
2151 * add/remove/toggled functions which can be a side effect
2152 * of setting the watch functions.
2154 * @param connection the connection.
2155 * @param add_function function to begin monitoring a new descriptor.
2156 * @param remove_function function to stop monitoring a descriptor.
2157 * @param toggled_function function to notify of enable/disable
2158 * @param data data to pass to add_function and remove_function.
2159 * @param free_data_function function to be called to free the data.
2160 * @returns #FALSE on failure (no memory)
2163 dbus_connection_set_watch_functions (DBusConnection *connection,
2164 DBusAddWatchFunction add_function,
2165 DBusRemoveWatchFunction remove_function,
2166 DBusWatchToggledFunction toggled_function,
2168 DBusFreeFunction free_data_function)
2172 dbus_mutex_lock (connection->mutex);
2173 /* ref connection for slightly better reentrancy */
2174 _dbus_connection_ref_unlocked (connection);
2176 /* FIXME this can call back into user code, and we need to drop the
2177 * connection lock when it does.
2179 retval = _dbus_watch_list_set_functions (connection->watches,
2180 add_function, remove_function,
2182 data, free_data_function);
2184 dbus_mutex_unlock (connection->mutex);
2185 /* drop our paranoid refcount */
2186 dbus_connection_unref (connection);
2192 * Sets the timeout functions for the connection. These functions are
2193 * responsible for making the application's main loop aware of timeouts.
2194 * When using Qt, typically the DBusAddTimeoutFunction would create a
2195 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2198 * The DBusTimeoutToggledFunction notifies the application that the
2199 * timeout has been enabled or disabled. Call
2200 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2201 * have no effect, and enabled timeout should be added to the main
2202 * loop. This feature is used instead of simply adding/removing the
2203 * timeout because enabling/disabling can be done without memory
2204 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2205 * to enable and disable. The toggled function may be NULL if a main
2206 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2208 * The DBusTimeout can be queried for the timer interval using
2209 * dbus_timeout_get_interval(). dbus_timeout_handle() should
2210 * be called repeatedly, each time the interval elapses, starting
2211 * after it has elapsed once. The timeout stops firing when
2212 * it is removed with the given remove_function.
2214 * @param connection the connection.
2215 * @param add_function function to add a timeout.
2216 * @param remove_function function to remove a timeout.
2217 * @param toggled_function function to notify of enable/disable
2218 * @param data data to pass to add_function and remove_function.
2219 * @param free_data_function function to be called to free the data.
2220 * @returns #FALSE on failure (no memory)
2223 dbus_connection_set_timeout_functions (DBusConnection *connection,
2224 DBusAddTimeoutFunction add_function,
2225 DBusRemoveTimeoutFunction remove_function,
2226 DBusTimeoutToggledFunction toggled_function,
2228 DBusFreeFunction free_data_function)
2232 dbus_mutex_lock (connection->mutex);
2233 /* ref connection for slightly better reentrancy */
2234 _dbus_connection_ref_unlocked (connection);
2236 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2237 add_function, remove_function,
2239 data, free_data_function);
2241 dbus_mutex_unlock (connection->mutex);
2242 /* drop our paranoid refcount */
2243 dbus_connection_unref (connection);
2249 * Sets the mainloop wakeup function for the connection. Thi function is
2250 * responsible for waking up the main loop (if its sleeping) when some some
2251 * change has happened to the connection that the mainloop needs to reconsiders
2252 * (e.g. a message has been queued for writing).
2253 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2254 * When using GLib, it would call g_main_context_wakeup().
2257 * @param connection the connection.
2258 * @param wakeup_main_function function to wake up the mainloop
2259 * @param data data to pass wakeup_main_function
2260 * @param free_data_function function to be called to free the data.
2263 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2264 DBusWakeupMainFunction wakeup_main_function,
2266 DBusFreeFunction free_data_function)
2269 DBusFreeFunction old_free_data;
2271 dbus_mutex_lock (connection->mutex);
2272 old_data = connection->wakeup_main_data;
2273 old_free_data = connection->free_wakeup_main_data;
2275 connection->wakeup_main_function = wakeup_main_function;
2276 connection->wakeup_main_data = data;
2277 connection->free_wakeup_main_data = free_data_function;
2279 dbus_mutex_unlock (connection->mutex);
2281 /* Callback outside the lock */
2283 (*old_free_data) (old_data);
2287 * Set a function to be invoked when the dispatch status changes.
2288 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
2289 * dbus_connection_dispatch() needs to be called to process incoming
2290 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
2291 * from inside the DBusDispatchStatusFunction. Indeed, almost
2292 * any reentrancy in this function is a bad idea. Instead,
2293 * the DBusDispatchStatusFunction should simply save an indication
2294 * that messages should be dispatched later, when the main loop
2297 * @param connection the connection
2298 * @param function function to call on dispatch status changes
2299 * @param data data for function
2300 * @param free_data_function free the function data
2303 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
2304 DBusDispatchStatusFunction function,
2306 DBusFreeFunction free_data_function)
2309 DBusFreeFunction old_free_data;
2311 dbus_mutex_lock (connection->mutex);
2312 old_data = connection->dispatch_status_data;
2313 old_free_data = connection->free_dispatch_status_data;
2315 connection->dispatch_status_function = function;
2316 connection->dispatch_status_data = data;
2317 connection->free_dispatch_status_data = free_data_function;
2319 dbus_mutex_unlock (connection->mutex);
2321 /* Callback outside the lock */
2323 (*old_free_data) (old_data);
2327 * Called to notify the connection when a previously-added watch
2328 * is ready for reading or writing, or has an exception such
2331 * If this function returns #FALSE, then the file descriptor may still
2332 * be ready for reading or writing, but more memory is needed in order
2333 * to do the reading or writing. If you ignore the #FALSE return, your
2334 * application may spin in a busy loop on the file descriptor until
2335 * memory becomes available, but nothing more catastrophic should
2338 * @param connection the connection.
2339 * @param watch the watch.
2340 * @param condition the current condition of the file descriptors being watched.
2341 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
2344 dbus_connection_handle_watch (DBusConnection *connection,
2346 unsigned int condition)
2349 DBusDispatchStatus status;
2351 dbus_mutex_lock (connection->mutex);
2352 _dbus_connection_acquire_io_path (connection, -1);
2353 retval = _dbus_transport_handle_watch (connection->transport,
2355 _dbus_connection_release_io_path (connection);
2357 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2359 dbus_mutex_unlock (connection->mutex);
2361 _dbus_connection_update_dispatch_status_locked (connection, status);
2367 * Gets the UNIX user ID of the connection if any.
2368 * Returns #TRUE if the uid is filled in.
2369 * Always returns #FALSE on non-UNIX platforms.
2370 * Always returns #FALSE prior to authenticating the
2373 * @param connection the connection
2374 * @param uid return location for the user ID
2375 * @returns #TRUE if uid is filled in with a valid user ID
2378 dbus_connection_get_unix_user (DBusConnection *connection,
2383 dbus_mutex_lock (connection->mutex);
2385 if (!_dbus_transport_get_is_authenticated (connection->transport))
2388 result = _dbus_transport_get_unix_user (connection->transport,
2390 dbus_mutex_unlock (connection->mutex);
2396 * Sets a predicate function used to determine whether a given user ID
2397 * is allowed to connect. When an incoming connection has
2398 * authenticated with a particular user ID, this function is called;
2399 * if it returns #TRUE, the connection is allowed to proceed,
2400 * otherwise the connection is disconnected.
2402 * If the function is set to #NULL (as it is by default), then
2403 * only the same UID as the server process will be allowed to
2406 * @param connection the connection
2407 * @param function the predicate
2408 * @param data data to pass to the predicate
2409 * @param free_data_function function to free the data
2412 dbus_connection_set_unix_user_function (DBusConnection *connection,
2413 DBusAllowUnixUserFunction function,
2415 DBusFreeFunction free_data_function)
2417 void *old_data = NULL;
2418 DBusFreeFunction old_free_function = NULL;
2420 dbus_mutex_lock (connection->mutex);
2421 _dbus_transport_set_unix_user_function (connection->transport,
2422 function, data, free_data_function,
2423 &old_data, &old_free_function);
2424 dbus_mutex_unlock (connection->mutex);
2426 if (old_free_function != NULL)
2427 (* old_free_function) (old_data);
2431 * Adds a message filter. Filters are handlers that are run on
2432 * all incoming messages, prior to the normal handlers
2433 * registered with dbus_connection_register_handler().
2434 * Filters are run in the order that they were added.
2435 * The same handler can be added as a filter more than once, in
2436 * which case it will be run more than once.
2437 * Filters added during a filter callback won't be run on the
2438 * message being processed.
2440 * @param connection the connection
2441 * @param handler the handler
2442 * @returns #TRUE on success, #FALSE if not enough memory.
2445 dbus_connection_add_filter (DBusConnection *connection,
2446 DBusMessageHandler *handler)
2448 dbus_mutex_lock (connection->mutex);
2449 if (!_dbus_message_handler_add_connection (handler, connection))
2451 dbus_mutex_unlock (connection->mutex);
2455 if (!_dbus_list_append (&connection->filter_list,
2458 _dbus_message_handler_remove_connection (handler, connection);
2459 dbus_mutex_unlock (connection->mutex);
2463 dbus_mutex_unlock (connection->mutex);
2468 * Removes a previously-added message filter. It is a programming
2469 * error to call this function for a handler that has not
2470 * been added as a filter. If the given handler was added
2471 * more than once, only one instance of it will be removed
2472 * (the most recently-added instance).
2474 * @param connection the connection
2475 * @param handler the handler to remove
2479 dbus_connection_remove_filter (DBusConnection *connection,
2480 DBusMessageHandler *handler)
2482 dbus_mutex_lock (connection->mutex);
2483 if (!_dbus_list_remove_last (&connection->filter_list, handler))
2485 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
2486 dbus_mutex_unlock (connection->mutex);
2490 _dbus_message_handler_remove_connection (handler, connection);
2492 dbus_mutex_unlock (connection->mutex);
2496 * Registers a handler for a list of message names. A single handler
2497 * can be registered for any number of message names, but each message
2498 * name can only have one handler at a time. It's not allowed to call
2499 * this function with the name of a message that already has a
2500 * handler. If the function returns #FALSE, the handlers were not
2501 * registered due to lack of memory.
2503 * @todo the messages_to_handle arg may be more convenient if it's a
2504 * single string instead of an array. Though right now MessageHandler
2505 * is sort of designed to say be associated with an entire object with
2506 * multiple methods, that's why for example the connection only
2507 * weakrefs it. So maybe the "manual" API should be different.
2509 * @param connection the connection
2510 * @param handler the handler
2511 * @param messages_to_handle the messages to handle
2512 * @param n_messages the number of message names in messages_to_handle
2513 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
2517 dbus_connection_register_handler (DBusConnection *connection,
2518 DBusMessageHandler *handler,
2519 const char **messages_to_handle,
2524 dbus_mutex_lock (connection->mutex);
2526 while (i < n_messages)
2531 key = _dbus_strdup (messages_to_handle[i]);
2535 if (!_dbus_hash_iter_lookup (connection->handler_table,
2543 if (_dbus_hash_iter_get_value (&iter) != NULL)
2545 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
2546 messages_to_handle[i]);
2547 dbus_free (key); /* won't have replaced the old key with the new one */
2551 if (!_dbus_message_handler_add_connection (handler, connection))
2553 _dbus_hash_iter_remove_entry (&iter);
2554 /* key has freed on nuking the entry */
2558 _dbus_hash_iter_set_value (&iter, handler);
2563 dbus_mutex_unlock (connection->mutex);
2567 /* unregister everything registered so far,
2568 * so we don't fail partially
2570 dbus_connection_unregister_handler (connection,
2575 dbus_mutex_unlock (connection->mutex);
2580 * Unregisters a handler for a list of message names. The handlers
2581 * must have been previously registered.
2583 * @param connection the connection
2584 * @param handler the handler
2585 * @param messages_to_handle the messages to handle
2586 * @param n_messages the number of message names in messages_to_handle
2590 dbus_connection_unregister_handler (DBusConnection *connection,
2591 DBusMessageHandler *handler,
2592 const char **messages_to_handle,
2597 dbus_mutex_lock (connection->mutex);
2599 while (i < n_messages)
2603 if (!_dbus_hash_iter_lookup (connection->handler_table,
2604 (char*) messages_to_handle[i], FALSE,
2607 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
2608 messages_to_handle[i]);
2610 else if (_dbus_hash_iter_get_value (&iter) != handler)
2612 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
2613 messages_to_handle[i]);
2617 _dbus_hash_iter_remove_entry (&iter);
2618 _dbus_message_handler_remove_connection (handler, connection);
2624 dbus_mutex_unlock (connection->mutex);
2627 static DBusDataSlotAllocator slot_allocator;
2628 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
2631 * Allocates an integer ID to be used for storing application-specific
2632 * data on any DBusConnection. The allocated ID may then be used
2633 * with dbus_connection_set_data() and dbus_connection_get_data().
2634 * If allocation fails, -1 is returned. Again, the allocated
2635 * slot is global, i.e. all DBusConnection objects will
2636 * have a slot with the given integer ID reserved.
2638 * @returns -1 on failure, otherwise the data slot ID
2641 dbus_connection_allocate_data_slot (void)
2643 return _dbus_data_slot_allocator_alloc (&slot_allocator,
2644 _DBUS_LOCK_NAME (connection_slots));
2648 * Deallocates a global ID for connection data slots.
2649 * dbus_connection_get_data() and dbus_connection_set_data()
2650 * may no longer be used with this slot.
2651 * Existing data stored on existing DBusConnection objects
2652 * will be freed when the connection is finalized,
2653 * but may not be retrieved (and may only be replaced
2654 * if someone else reallocates the slot).
2656 * @param slot the slot to deallocate
2659 dbus_connection_free_data_slot (int slot)
2661 _dbus_data_slot_allocator_free (&slot_allocator, slot);
2665 * Stores a pointer on a DBusConnection, along
2666 * with an optional function to be used for freeing
2667 * the data when the data is set again, or when
2668 * the connection is finalized. The slot number
2669 * must have been allocated with dbus_connection_allocate_data_slot().
2671 * @param connection the connection
2672 * @param slot the slot number
2673 * @param data the data to store
2674 * @param free_data_func finalizer function for the data
2675 * @returns #TRUE if there was enough memory to store the data
2678 dbus_connection_set_data (DBusConnection *connection,
2681 DBusFreeFunction free_data_func)
2683 DBusFreeFunction old_free_func;
2687 dbus_mutex_lock (connection->mutex);
2689 retval = _dbus_data_slot_list_set (&slot_allocator,
2690 &connection->slot_list,
2691 slot, data, free_data_func,
2692 &old_free_func, &old_data);
2694 dbus_mutex_unlock (connection->mutex);
2698 /* Do the actual free outside the connection lock */
2700 (* old_free_func) (old_data);
2707 * Retrieves data previously set with dbus_connection_set_data().
2708 * The slot must still be allocated (must not have been freed).
2710 * @param connection the connection
2711 * @param slot the slot to get data from
2712 * @returns the data, or #NULL if not found
2715 dbus_connection_get_data (DBusConnection *connection,
2720 dbus_mutex_lock (connection->mutex);
2722 res = _dbus_data_slot_list_get (&slot_allocator,
2723 &connection->slot_list,
2726 dbus_mutex_unlock (connection->mutex);
2732 * This function sets a global flag for whether dbus_connection_new()
2733 * will set SIGPIPE behavior to SIG_IGN.
2735 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
2738 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
2740 _dbus_modify_sigpipe = will_modify_sigpipe;
2744 * Specifies the maximum size message this connection is allowed to
2745 * receive. Larger messages will result in disconnecting the
2748 * @param connection a #DBusConnection
2749 * @param size maximum message size the connection can receive, in bytes
2752 dbus_connection_set_max_message_size (DBusConnection *connection,
2755 dbus_mutex_lock (connection->mutex);
2756 _dbus_transport_set_max_message_size (connection->transport,
2758 dbus_mutex_unlock (connection->mutex);
2762 * Gets the value set by dbus_connection_set_max_message_size().
2764 * @param connection the connection
2765 * @returns the max size of a single message
2768 dbus_connection_get_max_message_size (DBusConnection *connection)
2771 dbus_mutex_lock (connection->mutex);
2772 res = _dbus_transport_get_max_message_size (connection->transport);
2773 dbus_mutex_unlock (connection->mutex);
2778 * Sets the maximum total number of bytes that can be used for all messages
2779 * received on this connection. Messages count toward the maximum until
2780 * they are finalized. When the maximum is reached, the connection will
2781 * not read more data until some messages are finalized.
2783 * The semantics of the maximum are: if outstanding messages are
2784 * already above the maximum, additional messages will not be read.
2785 * The semantics are not: if the next message would cause us to exceed
2786 * the maximum, we don't read it. The reason is that we don't know the
2787 * size of a message until after we read it.
2789 * Thus, the max live messages size can actually be exceeded
2790 * by up to the maximum size of a single message.
2792 * Also, if we read say 1024 bytes off the wire in a single read(),
2793 * and that contains a half-dozen small messages, we may exceed the
2794 * size max by that amount. But this should be inconsequential.
2796 * This does imply that we can't call read() with a buffer larger
2797 * than we're willing to exceed this limit by.
2799 * @param connection the connection
2800 * @param size the maximum size in bytes of all outstanding messages
2803 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
2806 dbus_mutex_lock (connection->mutex);
2807 _dbus_transport_set_max_live_messages_size (connection->transport,
2809 dbus_mutex_unlock (connection->mutex);
2813 * Gets the value set by dbus_connection_set_max_live_messages_size().
2815 * @param connection the connection
2816 * @returns the max size of all live messages
2819 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
2822 dbus_mutex_lock (connection->mutex);
2823 res = _dbus_transport_get_max_live_messages_size (connection->transport);
2824 dbus_mutex_unlock (connection->mutex);