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_message */
80 DBusCondVar *dispatch_cond; /**< Protects dispatch_message */
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 */
116 DBusConnection *connection;
117 DBusMessageHandler *handler;
118 DBusTimeout *timeout;
121 DBusList *timeout_link; /* Preallocated timeout response */
123 dbus_bool_t timeout_added;
124 dbus_bool_t connection_added;
127 static void reply_handler_data_free (ReplyHandlerData *data);
129 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
130 DBusTimeout *timeout);
133 * Acquires the connection lock.
135 * @param connection the connection.
138 _dbus_connection_lock (DBusConnection *connection)
140 dbus_mutex_lock (connection->mutex);
144 * Releases the connection lock.
146 * @param connection the connection.
149 _dbus_connection_unlock (DBusConnection *connection)
151 dbus_mutex_unlock (connection->mutex);
155 * Wakes up the main loop if it is sleeping
156 * Needed if we're e.g. queueing outgoing messages
157 * on a thread while the mainloop sleeps.
159 * @param connection the connection.
162 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
164 if (connection->wakeup_main_function)
165 (*connection->wakeup_main_function) (connection->wakeup_main_data);
169 * Adds a message to the incoming message queue, returning #FALSE
170 * if there's insufficient memory to queue the message.
172 * @param connection the connection.
173 * @param message the message to queue.
174 * @returns #TRUE on success.
177 _dbus_connection_queue_received_message (DBusConnection *connection,
178 DBusMessage *message)
180 ReplyHandlerData *reply_handler_data;
181 dbus_int32_t reply_serial;
183 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
185 if (!_dbus_list_append (&connection->incoming_messages,
189 /* If this is a reply we're waiting on, remove timeout for it */
190 reply_serial = dbus_message_get_reply_serial (message);
191 if (reply_serial != -1)
193 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
195 if (reply_handler_data != NULL)
197 if (reply_handler_data->timeout_added)
198 _dbus_connection_remove_timeout_locked (connection,
199 reply_handler_data->timeout);
200 reply_handler_data->timeout_added = FALSE;
204 dbus_message_ref (message);
205 connection->n_incoming += 1;
207 _dbus_connection_wakeup_mainloop (connection);
209 _dbus_assert (dbus_message_get_name (message) != NULL);
210 _dbus_verbose ("Message %p (%s) added to incoming queue %p, %d incoming\n",
211 message, dbus_message_get_name (message),
213 connection->n_incoming);
219 * Adds a link + message to the incoming message queue.
220 * Can't fail. Takes ownership of both link and message.
222 * @param connection the connection.
223 * @param link the list node and message to queue.
225 * @todo This needs to wake up the mainloop if it is in
226 * a poll/select and this is a multithreaded app.
229 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
232 _dbus_list_append_link (&connection->incoming_messages, link);
234 connection->n_incoming += 1;
236 _dbus_connection_wakeup_mainloop (connection);
238 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
239 link->data, connection, connection->n_incoming);
244 * Checks whether there are messages in the outgoing message queue.
246 * @param connection the connection.
247 * @returns #TRUE if the outgoing queue is non-empty.
250 _dbus_connection_have_messages_to_send (DBusConnection *connection)
252 return connection->outgoing_messages != NULL;
256 * Gets the next outgoing message. The message remains in the
257 * queue, and the caller does not own a reference to it.
259 * @param connection the connection.
260 * @returns the message to be sent.
263 _dbus_connection_get_message_to_send (DBusConnection *connection)
265 return _dbus_list_get_last (&connection->outgoing_messages);
269 * Notifies the connection that a message has been sent, so the
270 * message can be removed from the outgoing queue.
272 * @param connection the connection.
273 * @param message the message that was sent.
276 _dbus_connection_message_sent (DBusConnection *connection,
277 DBusMessage *message)
279 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
280 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
282 _dbus_list_pop_last (&connection->outgoing_messages);
283 dbus_message_unref (message);
285 connection->n_outgoing -= 1;
287 _dbus_verbose ("Message %p removed from outgoing queue %p, %d left to send\n",
288 message, connection, connection->n_outgoing);
290 if (connection->n_outgoing == 0)
291 _dbus_transport_messages_pending (connection->transport,
292 connection->n_outgoing);
296 * Adds a watch using the connection's DBusAddWatchFunction if
297 * available. Otherwise records the watch to be added when said
298 * function is available. Also re-adds the watch if the
299 * DBusAddWatchFunction changes. May fail due to lack of memory.
301 * @param connection the connection.
302 * @param watch the watch to add.
303 * @returns #TRUE on success.
306 _dbus_connection_add_watch (DBusConnection *connection,
309 if (connection->watches) /* null during finalize */
310 return _dbus_watch_list_add_watch (connection->watches,
317 * Removes a watch using the connection's DBusRemoveWatchFunction
318 * if available. It's an error to call this function on a watch
319 * that was not previously added.
321 * @param connection the connection.
322 * @param watch the watch to remove.
325 _dbus_connection_remove_watch (DBusConnection *connection,
328 if (connection->watches) /* null during finalize */
329 _dbus_watch_list_remove_watch (connection->watches,
334 * Adds a timeout using the connection's DBusAddTimeoutFunction if
335 * available. Otherwise records the timeout to be added when said
336 * function is available. Also re-adds the timeout if the
337 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
338 * The timeout will fire repeatedly until removed.
340 * @param connection the connection.
341 * @param timeout the timeout to add.
342 * @returns #TRUE on success.
345 _dbus_connection_add_timeout (DBusConnection *connection,
346 DBusTimeout *timeout)
348 if (connection->timeouts) /* null during finalize */
349 return _dbus_timeout_list_add_timeout (connection->timeouts,
356 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
357 * if available. It's an error to call this function on a timeout
358 * that was not previously added.
360 * @param connection the connection.
361 * @param timeout the timeout to remove.
364 _dbus_connection_remove_timeout (DBusConnection *connection,
365 DBusTimeout *timeout)
367 if (connection->timeouts) /* null during finalize */
368 _dbus_timeout_list_remove_timeout (connection->timeouts,
373 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
374 DBusTimeout *timeout)
376 dbus_mutex_lock (connection->mutex);
377 _dbus_connection_remove_timeout (connection, timeout);
378 dbus_mutex_unlock (connection->mutex);
383 * Tells the connection that the transport has been disconnected.
384 * Results in posting a disconnect message on the incoming message
385 * queue. Only has an effect the first time it's called.
387 * @param connection the connection
390 _dbus_connection_notify_disconnected (DBusConnection *connection)
392 if (connection->disconnect_message_link)
394 /* We haven't sent the disconnect message already */
395 _dbus_connection_queue_synthesized_message_link (connection,
396 connection->disconnect_message_link);
397 connection->disconnect_message_link = NULL;
403 * Acquire the transporter I/O path. This must be done before
404 * doing any I/O in the transporter. May sleep and drop the
405 * connection mutex while waiting for the I/O path.
407 * @param connection the connection.
408 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
409 * @returns TRUE if the I/O path was acquired.
412 _dbus_connection_acquire_io_path (DBusConnection *connection,
413 int timeout_milliseconds)
415 dbus_bool_t res = TRUE;
417 if (connection->io_path_acquired)
419 if (timeout_milliseconds != -1)
420 res = dbus_condvar_wait_timeout (connection->io_path_cond,
422 timeout_milliseconds);
424 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
429 _dbus_assert (!connection->io_path_acquired);
431 connection->io_path_acquired = TRUE;
438 * Release the I/O path when you're done with it. Only call
439 * after you've acquired the I/O. Wakes up at most one thread
440 * currently waiting to acquire the I/O path.
442 * @param connection the connection.
445 _dbus_connection_release_io_path (DBusConnection *connection)
447 _dbus_assert (connection->io_path_acquired);
449 connection->io_path_acquired = FALSE;
450 dbus_condvar_wake_one (connection->io_path_cond);
455 * Queues incoming messages and sends outgoing messages for this
456 * connection, optionally blocking in the process. Each call to
457 * _dbus_connection_do_iteration() will call select() or poll() one
458 * time and then read or write data if possible.
460 * The purpose of this function is to be able to flush outgoing
461 * messages or queue up incoming messages without returning
462 * control to the application and causing reentrancy weirdness.
464 * The flags parameter allows you to specify whether to
465 * read incoming messages, write outgoing messages, or both,
466 * and whether to block if no immediate action is possible.
468 * The timeout_milliseconds parameter does nothing unless the
469 * iteration is blocking.
471 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
472 * wasn't specified, then it's impossible to block, even if
473 * you specify DBUS_ITERATION_BLOCK; in that case the function
474 * returns immediately.
476 * @param connection the connection.
477 * @param flags iteration flags.
478 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
481 _dbus_connection_do_iteration (DBusConnection *connection,
483 int timeout_milliseconds)
485 if (connection->n_outgoing == 0)
486 flags &= ~DBUS_ITERATION_DO_WRITING;
488 if (_dbus_connection_acquire_io_path (connection,
489 (flags & DBUS_ITERATION_BLOCK)?timeout_milliseconds:0))
491 _dbus_transport_do_iteration (connection->transport,
492 flags, timeout_milliseconds);
493 _dbus_connection_release_io_path (connection);
498 * Creates a new connection for the given transport. A transport
499 * represents a message stream that uses some concrete mechanism, such
500 * as UNIX domain sockets. May return #NULL if insufficient
501 * memory exists to create the connection.
503 * @param transport the transport.
504 * @returns the new connection, or #NULL on failure.
507 _dbus_connection_new_for_transport (DBusTransport *transport)
509 DBusConnection *connection;
510 DBusWatchList *watch_list;
511 DBusTimeoutList *timeout_list;
512 DBusHashTable *handler_table, *pending_replies;
514 DBusCondVar *message_returned_cond;
515 DBusCondVar *dispatch_cond;
516 DBusCondVar *io_path_cond;
517 DBusList *disconnect_link;
518 DBusMessage *disconnect_message;
522 handler_table = NULL;
523 pending_replies = NULL;
526 message_returned_cond = NULL;
527 dispatch_cond = NULL;
529 disconnect_link = NULL;
530 disconnect_message = NULL;
532 watch_list = _dbus_watch_list_new ();
533 if (watch_list == NULL)
536 timeout_list = _dbus_timeout_list_new ();
537 if (timeout_list == NULL)
541 _dbus_hash_table_new (DBUS_HASH_STRING,
543 if (handler_table == NULL)
547 _dbus_hash_table_new (DBUS_HASH_INT,
548 NULL, (DBusFreeFunction)reply_handler_data_free);
549 if (pending_replies == NULL)
552 connection = dbus_new0 (DBusConnection, 1);
553 if (connection == NULL)
556 mutex = dbus_mutex_new ();
560 message_returned_cond = dbus_condvar_new ();
561 if (message_returned_cond == NULL)
564 dispatch_cond = dbus_condvar_new ();
565 if (dispatch_cond == NULL)
568 io_path_cond = dbus_condvar_new ();
569 if (io_path_cond == NULL)
572 disconnect_message = dbus_message_new (NULL, DBUS_MESSAGE_LOCAL_DISCONNECT);
573 if (disconnect_message == NULL)
576 disconnect_link = _dbus_list_alloc_link (disconnect_message);
577 if (disconnect_link == NULL)
580 if (_dbus_modify_sigpipe)
581 _dbus_disable_sigpipe ();
583 connection->refcount = 1;
584 connection->mutex = mutex;
585 connection->dispatch_cond = dispatch_cond;
586 connection->io_path_cond = io_path_cond;
587 connection->message_returned_cond = message_returned_cond;
588 connection->transport = transport;
589 connection->watches = watch_list;
590 connection->timeouts = timeout_list;
591 connection->handler_table = handler_table;
592 connection->pending_replies = pending_replies;
593 connection->filter_list = NULL;
595 _dbus_data_slot_list_init (&connection->slot_list);
597 connection->client_serial = 1;
599 connection->disconnect_message_link = disconnect_link;
601 _dbus_transport_ref (transport);
602 _dbus_transport_set_connection (transport, connection);
607 if (disconnect_message != NULL)
608 dbus_message_unref (disconnect_message);
610 if (disconnect_link != NULL)
611 _dbus_list_free_link (disconnect_link);
613 if (io_path_cond != NULL)
614 dbus_condvar_free (io_path_cond);
616 if (dispatch_cond != NULL)
617 dbus_condvar_free (dispatch_cond);
619 if (message_returned_cond != NULL)
620 dbus_condvar_free (message_returned_cond);
623 dbus_mutex_free (mutex);
625 if (connection != NULL)
626 dbus_free (connection);
629 _dbus_hash_table_unref (handler_table);
632 _dbus_hash_table_unref (pending_replies);
635 _dbus_watch_list_free (watch_list);
638 _dbus_timeout_list_free (timeout_list);
644 _dbus_connection_get_next_client_serial (DBusConnection *connection)
648 serial = connection->client_serial++;
650 if (connection->client_serial < 0)
651 connection->client_serial = 1;
657 * Used to notify a connection when a DBusMessageHandler is
658 * destroyed, so the connection can drop any reference
659 * to the handler. This is a private function, but still
660 * takes the connection lock. Don't call it with the lock held.
662 * @todo needs to check in pending_replies too.
664 * @param connection the connection
665 * @param handler the handler
668 _dbus_connection_handler_destroyed_locked (DBusConnection *connection,
669 DBusMessageHandler *handler)
674 dbus_mutex_lock (connection->mutex);
676 _dbus_hash_iter_init (connection->handler_table, &iter);
677 while (_dbus_hash_iter_next (&iter))
679 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
682 _dbus_hash_iter_remove_entry (&iter);
685 link = _dbus_list_get_first_link (&connection->filter_list);
688 DBusMessageHandler *h = link->data;
689 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
692 _dbus_list_remove_link (&connection->filter_list,
697 dbus_mutex_unlock (connection->mutex);
701 * Adds the counter used to count the number of open connections.
702 * Increments the counter by one, and saves it to be decremented
703 * again when this connection is finalized.
705 * @param connection a #DBusConnection
706 * @param counter counter that tracks number of connections
709 _dbus_connection_set_connection_counter (DBusConnection *connection,
710 DBusCounter *counter)
712 _dbus_assert (connection->connection_counter == NULL);
714 connection->connection_counter = counter;
715 _dbus_counter_ref (connection->connection_counter);
716 _dbus_counter_adjust (connection->connection_counter, 1);
722 * @addtogroup DBusConnection
728 * Opens a new connection to a remote address.
730 * @todo specify what the address parameter is. Right now
731 * it's just the name of a UNIX domain socket. It should be
732 * something more complex that encodes which transport to use.
734 * If the open fails, the function returns #NULL, and provides
735 * a reason for the failure in the result parameter. Pass
736 * #NULL for the result parameter if you aren't interested
737 * in the reason for failure.
739 * @param address the address.
740 * @param result address where a result code can be returned.
741 * @returns new connection, or #NULL on failure.
744 dbus_connection_open (const char *address,
745 DBusResultCode *result)
747 DBusConnection *connection;
748 DBusTransport *transport;
750 transport = _dbus_transport_open (address, result);
751 if (transport == NULL)
754 connection = _dbus_connection_new_for_transport (transport);
756 _dbus_transport_unref (transport);
758 if (connection == NULL)
760 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
768 * Increments the reference count of a DBusConnection.
770 * @param connection the connection.
773 dbus_connection_ref (DBusConnection *connection)
775 dbus_mutex_lock (connection->mutex);
776 _dbus_assert (connection->refcount > 0);
778 connection->refcount += 1;
779 dbus_mutex_unlock (connection->mutex);
783 * Increments the reference count of a DBusConnection.
784 * Requires that the caller already holds the connection lock.
786 * @param connection the connection.
789 _dbus_connection_ref_unlocked (DBusConnection *connection)
791 _dbus_assert (connection->refcount > 0);
792 connection->refcount += 1;
796 /* This is run without the mutex held, but after the last reference
797 * to the connection has been dropped we should have no thread-related
801 _dbus_connection_last_unref (DBusConnection *connection)
806 /* You have to disconnect the connection before unref:ing it. Otherwise
807 * you won't get the disconnected message.
809 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
811 if (connection->connection_counter != NULL)
813 /* subtract ourselves from the counter */
814 _dbus_counter_adjust (connection->connection_counter, - 1);
815 _dbus_counter_unref (connection->connection_counter);
816 connection->connection_counter = NULL;
819 _dbus_watch_list_free (connection->watches);
820 connection->watches = NULL;
822 _dbus_timeout_list_free (connection->timeouts);
823 connection->timeouts = NULL;
825 /* calls out to application code... */
826 _dbus_data_slot_list_free (&connection->slot_list);
828 _dbus_hash_iter_init (connection->handler_table, &iter);
829 while (_dbus_hash_iter_next (&iter))
831 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
833 _dbus_message_handler_remove_connection (h, connection);
836 link = _dbus_list_get_first_link (&connection->filter_list);
839 DBusMessageHandler *h = link->data;
840 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
842 _dbus_message_handler_remove_connection (h, connection);
847 _dbus_hash_table_unref (connection->handler_table);
848 connection->handler_table = NULL;
850 _dbus_hash_table_unref (connection->pending_replies);
851 connection->pending_replies = NULL;
853 _dbus_list_clear (&connection->filter_list);
855 _dbus_list_foreach (&connection->outgoing_messages,
856 (DBusForeachFunction) dbus_message_unref,
858 _dbus_list_clear (&connection->outgoing_messages);
860 _dbus_list_foreach (&connection->incoming_messages,
861 (DBusForeachFunction) dbus_message_unref,
863 _dbus_list_clear (&connection->incoming_messages);
865 _dbus_transport_unref (connection->transport);
867 if (connection->disconnect_message_link)
869 DBusMessage *message = connection->disconnect_message_link->data;
870 dbus_message_unref (message);
871 _dbus_list_free_link (connection->disconnect_message_link);
874 dbus_condvar_free (connection->dispatch_cond);
875 dbus_condvar_free (connection->io_path_cond);
876 dbus_condvar_free (connection->message_returned_cond);
878 dbus_mutex_free (connection->mutex);
880 dbus_free (connection);
884 * Decrements the reference count of a DBusConnection, and finalizes
885 * it if the count reaches zero. It is a bug to drop the last reference
886 * to a connection that has not been disconnected.
888 * @param connection the connection.
891 dbus_connection_unref (DBusConnection *connection)
893 dbus_bool_t last_unref;
895 dbus_mutex_lock (connection->mutex);
897 _dbus_assert (connection != NULL);
898 _dbus_assert (connection->refcount > 0);
900 connection->refcount -= 1;
901 last_unref = (connection->refcount == 0);
903 dbus_mutex_unlock (connection->mutex);
906 _dbus_connection_last_unref (connection);
910 * Closes the connection, so no further data can be sent or received.
911 * Any further attempts to send data will result in errors. This
912 * function does not affect the connection's reference count. It's
913 * safe to disconnect a connection more than once; all calls after the
914 * first do nothing. It's impossible to "reconnect" a connection, a
915 * new connection must be created.
917 * @param connection the connection.
920 dbus_connection_disconnect (DBusConnection *connection)
922 dbus_mutex_lock (connection->mutex);
923 _dbus_transport_disconnect (connection->transport);
924 dbus_mutex_unlock (connection->mutex);
928 * Gets whether the connection is currently connected. All
929 * connections are connected when they are opened. A connection may
930 * become disconnected when the remote application closes its end, or
931 * exits; a connection may also be disconnected with
932 * dbus_connection_disconnect().
934 * @param connection the connection.
935 * @returns #TRUE if the connection is still alive.
938 dbus_connection_get_is_connected (DBusConnection *connection)
942 dbus_mutex_lock (connection->mutex);
943 res = _dbus_transport_get_is_connected (connection->transport);
944 dbus_mutex_unlock (connection->mutex);
950 * Gets whether the connection was authenticated. (Note that
951 * if the connection was authenticated then disconnected,
952 * this function still returns #TRUE)
954 * @param connection the connection
955 * @returns #TRUE if the connection was ever authenticated
958 dbus_connection_get_is_authenticated (DBusConnection *connection)
962 dbus_mutex_lock (connection->mutex);
963 res = _dbus_transport_get_is_authenticated (connection->transport);
964 dbus_mutex_unlock (connection->mutex);
970 * Preallocates resources needed to send a message, allowing the message
971 * to be sent without the possibility of memory allocation failure.
972 * Allows apps to create a future guarantee that they can send
973 * a message regardless of memory shortages.
975 * @param connection the connection we're preallocating for.
976 * @returns the preallocated resources, or #NULL
978 DBusPreallocatedSend*
979 dbus_connection_preallocate_send (DBusConnection *connection)
981 /* we store "connection" in the link just to enforce via
982 * assertion that preallocated links are only used
983 * with the connection they were created for.
985 return (DBusPreallocatedSend*) _dbus_list_alloc_link (connection);
989 * Frees preallocated message-sending resources from
990 * dbus_connection_preallocate_send(). Should only
991 * be called if the preallocated resources are not used
994 * @param connection the connection
995 * @param preallocated the resources
998 dbus_connection_free_preallocated_send (DBusConnection *connection,
999 DBusPreallocatedSend *preallocated)
1001 DBusList *link = (DBusList*) preallocated;
1002 _dbus_assert (link->data == connection);
1003 _dbus_list_free_link (link);
1007 * Sends a message using preallocated resources. This function cannot fail.
1008 * It works identically to dbus_connection_send() in other respects.
1009 * Preallocated resources comes from dbus_connection_preallocate_send().
1010 * This function "consumes" the preallocated resources, they need not
1011 * be freed separately.
1013 * @param connection the connection
1014 * @param preallocated the preallocated resources
1015 * @param message the message to send
1016 * @param client_serial return location for client serial assigned to the message
1019 dbus_connection_send_preallocated (DBusConnection *connection,
1020 DBusPreallocatedSend *preallocated,
1021 DBusMessage *message,
1022 dbus_int32_t *client_serial)
1024 DBusList *link = (DBusList*) preallocated;
1025 dbus_int32_t serial;
1027 _dbus_assert (link->data == connection);
1028 _dbus_assert (dbus_message_get_name (message) != NULL);
1030 dbus_mutex_lock (connection->mutex);
1032 link->data = message;
1033 _dbus_list_prepend_link (&connection->outgoing_messages,
1036 dbus_message_ref (message);
1037 connection->n_outgoing += 1;
1039 _dbus_verbose ("Message %p (%s) added to outgoing queue %p, %d pending to send\n",
1041 dbus_message_get_name (message),
1043 connection->n_outgoing);
1045 if (dbus_message_get_serial (message) == -1)
1047 serial = _dbus_connection_get_next_client_serial (connection);
1048 _dbus_message_set_serial (message, serial);
1052 *client_serial = dbus_message_get_serial (message);
1054 _dbus_message_lock (message);
1056 if (connection->n_outgoing == 1)
1057 _dbus_transport_messages_pending (connection->transport,
1058 connection->n_outgoing);
1060 _dbus_connection_wakeup_mainloop (connection);
1062 dbus_mutex_unlock (connection->mutex);
1066 * Adds a message to the outgoing message queue. Does not block to
1067 * write the message to the network; that happens asynchronously. To
1068 * force the message to be written, call dbus_connection_flush().
1069 * Because this only queues the message, the only reason it can
1070 * fail is lack of memory. Even if the connection is disconnected,
1071 * no error will be returned.
1073 * If the function fails, it returns #FALSE and returns the
1074 * reason for failure via the result parameter.
1075 * The result parameter can be #NULL if you aren't interested
1076 * in the reason for the failure.
1078 * @param connection the connection.
1079 * @param message the message to write.
1080 * @param client_serial return location for client serial.
1081 * @returns #TRUE on success.
1084 dbus_connection_send (DBusConnection *connection,
1085 DBusMessage *message,
1086 dbus_int32_t *client_serial)
1088 DBusPreallocatedSend *preallocated;
1090 preallocated = dbus_connection_preallocate_send (connection);
1091 if (preallocated == NULL)
1097 dbus_connection_send_preallocated (connection, preallocated, message, client_serial);
1103 reply_handler_timeout (void *data)
1105 DBusConnection *connection;
1106 ReplyHandlerData *reply_handler_data = data;
1108 connection = reply_handler_data->connection;
1110 dbus_mutex_lock (connection->mutex);
1111 if (reply_handler_data->timeout_link)
1113 _dbus_connection_queue_synthesized_message_link (connection,
1114 reply_handler_data->timeout_link);
1115 reply_handler_data->timeout_link = NULL;
1118 _dbus_connection_remove_timeout (connection,
1119 reply_handler_data->timeout);
1120 reply_handler_data->timeout_added = FALSE;
1122 dbus_mutex_unlock (connection->mutex);
1126 reply_handler_data_free (ReplyHandlerData *data)
1131 if (data->timeout_added)
1132 _dbus_connection_remove_timeout_locked (data->connection,
1135 if (data->connection_added)
1136 _dbus_message_handler_remove_connection (data->handler,
1139 if (data->timeout_link)
1141 dbus_message_unref ((DBusMessage *)data->timeout_link->data);
1142 _dbus_list_free_link (data->timeout_link);
1145 dbus_message_handler_unref (data->handler);
1151 * Queues a message to send, as with dbus_connection_send_message(),
1152 * but also sets up a DBusMessageHandler to receive a reply to the
1153 * message. If no reply is received in the given timeout_milliseconds,
1154 * expires the pending reply and sends the DBusMessageHandler a
1155 * synthetic error reply (generated in-process, not by the remote
1156 * application) indicating that a timeout occurred.
1158 * Reply handlers see their replies after message filters see them,
1159 * but before message handlers added with
1160 * dbus_connection_register_handler() see them, regardless of the
1161 * reply message's name. Reply handlers are only handed a single
1162 * message as a reply, after one reply has been seen the handler is
1163 * removed. If a filter filters out the reply before the handler sees
1164 * it, the reply is immediately timed out and a timeout error reply is
1165 * generated. If a filter removes the timeout error reply then the
1166 * reply handler will never be called. Filters should not do this.
1168 * If #NULL is passed for the reply_handler, the timeout reply will
1169 * still be generated and placed into the message queue, but no
1170 * specific message handler will receive the reply.
1172 * If -1 is passed for the timeout, a sane default timeout is used. -1
1173 * is typically the best value for the timeout for this reason, unless
1174 * you want a very short or very long timeout. There is no way to
1175 * avoid a timeout entirely, other than passing INT_MAX for the
1176 * timeout to postpone it indefinitely.
1178 * @param connection the connection
1179 * @param message the message to send
1180 * @param reply_handler message handler expecting the reply, or #NULL
1181 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1182 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1186 dbus_connection_send_with_reply (DBusConnection *connection,
1187 DBusMessage *message,
1188 DBusMessageHandler *reply_handler,
1189 int timeout_milliseconds)
1191 DBusTimeout *timeout;
1192 ReplyHandlerData *data;
1194 DBusList *reply_link;
1195 dbus_int32_t serial = -1;
1197 if (timeout_milliseconds == -1)
1198 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
1200 data = dbus_new0 (ReplyHandlerData, 1);
1205 timeout = _dbus_timeout_new (timeout_milliseconds, reply_handler_timeout,
1210 reply_handler_data_free (data);
1214 dbus_mutex_lock (connection->mutex);
1217 if (!_dbus_connection_add_timeout (connection, timeout))
1219 reply_handler_data_free (data);
1220 _dbus_timeout_unref (timeout);
1221 dbus_mutex_unlock (connection->mutex);
1225 /* The connection now owns the reference to the timeout. */
1226 _dbus_timeout_unref (timeout);
1228 data->timeout_added = TRUE;
1229 data->timeout = timeout;
1230 data->connection = connection;
1232 if (!_dbus_message_handler_add_connection (reply_handler, connection))
1234 dbus_mutex_unlock (connection->mutex);
1235 reply_handler_data_free (data);
1238 data->connection_added = TRUE;
1240 /* Assign a serial to the message */
1241 if (dbus_message_get_serial (message) == -1)
1243 serial = _dbus_connection_get_next_client_serial (connection);
1244 _dbus_message_set_serial (message, serial);
1247 data->handler = reply_handler;
1248 data->serial = serial;
1250 dbus_message_handler_ref (reply_handler);
1252 reply = dbus_message_new_error_reply (message, DBUS_ERROR_NO_REPLY,
1253 "No reply within specified time");
1256 dbus_mutex_unlock (connection->mutex);
1257 reply_handler_data_free (data);
1261 reply_link = _dbus_list_alloc_link (reply);
1264 dbus_mutex_unlock (connection->mutex);
1265 dbus_message_unref (reply);
1266 reply_handler_data_free (data);
1270 data->timeout_link = reply_link;
1272 /* Insert the serial in the pending replies hash. */
1273 if (!_dbus_hash_table_insert_int (connection->pending_replies, serial, data))
1275 dbus_mutex_unlock (connection->mutex);
1276 reply_handler_data_free (data);
1280 dbus_mutex_unlock (connection->mutex);
1282 if (!dbus_connection_send (connection, message, NULL))
1284 /* This will free the handler data too */
1285 _dbus_hash_table_remove_int (connection->pending_replies, serial);
1293 * Sends a message and blocks a certain time period while waiting for a reply.
1294 * This function does not dispatch any message handlers until the main loop
1295 * has been reached. This function is used to do non-reentrant "method calls."
1296 * If a reply is received, it is returned, and removed from the incoming
1297 * message queue. If it is not received, #NULL is returned and the
1298 * error is set to #DBUS_ERROR_NO_REPLY. If something else goes
1299 * wrong, result is set to whatever is appropriate, such as
1300 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1302 * @todo could use performance improvements (it keeps scanning
1303 * the whole message queue for example) and has thread issues,
1304 * see comments in source
1306 * @param connection the connection
1307 * @param message the message to send
1308 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1309 * @param error return location for error message
1310 * @returns the message that is the reply or #NULL with an error code if the
1314 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1315 DBusMessage *message,
1316 int timeout_milliseconds,
1319 dbus_int32_t client_serial;
1321 long start_tv_sec, start_tv_usec;
1322 long end_tv_sec, end_tv_usec;
1323 long tv_sec, tv_usec;
1325 if (timeout_milliseconds == -1)
1326 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
1328 /* it would probably seem logical to pass in _DBUS_INT_MAX
1329 * for infinite timeout, but then math below would get
1330 * all overflow-prone, so smack that down.
1332 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1333 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1335 if (!dbus_connection_send (connection, message, &client_serial))
1337 _DBUS_SET_OOM (error);
1343 /* Flush message queue */
1344 dbus_connection_flush (connection);
1346 dbus_mutex_lock (connection->mutex);
1348 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1349 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1350 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1351 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1352 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1354 _dbus_verbose ("will block %d milliseconds from %ld sec %ld usec to %ld sec %ld usec\n",
1355 timeout_milliseconds,
1356 start_tv_sec, start_tv_usec,
1357 end_tv_sec, end_tv_usec);
1359 /* Now we wait... */
1360 /* THREAD TODO: This is busted. What if a dispatch_message or pop_message
1361 * gets the message before we do?
1365 _dbus_connection_do_iteration (connection,
1366 DBUS_ITERATION_DO_READING |
1367 DBUS_ITERATION_BLOCK,
1368 timeout_milliseconds);
1370 /* Check if we've gotten a reply */
1371 link = _dbus_list_get_first_link (&connection->incoming_messages);
1373 while (link != NULL)
1375 DBusMessage *reply = link->data;
1377 if (dbus_message_get_reply_serial (reply) == client_serial)
1379 _dbus_list_remove_link (&connection->incoming_messages, link);
1380 dbus_message_ref (reply);
1382 dbus_mutex_unlock (connection->mutex);
1385 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1388 _dbus_get_current_time (&tv_sec, &tv_usec);
1390 if (tv_sec < start_tv_sec)
1391 ; /* clock set backward, bail out */
1392 else if (connection->disconnect_message_link == NULL)
1393 ; /* we're disconnected, bail out */
1394 else if (tv_sec < end_tv_sec ||
1395 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1397 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1398 (end_tv_usec - tv_usec) / 1000;
1399 _dbus_verbose ("%d milliseconds remain\n", timeout_milliseconds);
1400 _dbus_assert (timeout_milliseconds > 0);
1402 goto block_again; /* not expired yet */
1405 if (dbus_connection_get_is_connected (connection))
1406 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
1408 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
1410 dbus_mutex_unlock (connection->mutex);
1416 * Blocks until the outgoing message queue is empty.
1418 * @param connection the connection.
1421 dbus_connection_flush (DBusConnection *connection)
1423 /* We have to specify DBUS_ITERATION_DO_READING here
1424 * because otherwise we could have two apps deadlock
1425 * if they are both doing a flush(), and the kernel
1429 dbus_mutex_lock (connection->mutex);
1430 while (connection->n_outgoing > 0)
1431 _dbus_connection_do_iteration (connection,
1432 DBUS_ITERATION_DO_READING |
1433 DBUS_ITERATION_DO_WRITING |
1434 DBUS_ITERATION_BLOCK,
1436 dbus_mutex_unlock (connection->mutex);
1440 * Gets the number of messages in the incoming message queue.
1442 * @param connection the connection.
1443 * @returns the number of messages in the queue.
1446 dbus_connection_get_n_messages (DBusConnection *connection)
1450 dbus_mutex_lock (connection->mutex);
1451 res = connection->n_incoming;
1452 dbus_mutex_unlock (connection->mutex);
1457 /* Call with mutex held. Will drop it while waiting and re-acquire
1461 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
1463 _dbus_assert (connection->message_borrowed != NULL);
1465 while (connection->message_borrowed != NULL)
1466 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
1470 * Returns the first-received message from the incoming message queue,
1471 * leaving it in the queue. If the queue is empty, returns #NULL.
1473 * The caller does not own a reference to the returned message, and must
1474 * either return it using dbus_connection_return_message or keep it after
1475 * calling dbus_connection_steal_borrowed_message. No one can get at the
1476 * message while its borrowed, so return it as quickly as possible and
1477 * don't keep a reference to it after returning it. If you need to keep
1478 * the message, make a copy of it.
1480 * @param connection the connection.
1481 * @returns next message in the incoming queue.
1484 dbus_connection_borrow_message (DBusConnection *connection)
1486 DBusMessage *message;
1488 dbus_mutex_lock (connection->mutex);
1490 if (connection->message_borrowed != NULL)
1491 _dbus_connection_wait_for_borrowed (connection);
1493 message = _dbus_list_get_first (&connection->incoming_messages);
1496 connection->message_borrowed = message;
1498 dbus_mutex_unlock (connection->mutex);
1506 dbus_connection_return_message (DBusConnection *connection,
1507 DBusMessage *message)
1509 dbus_mutex_lock (connection->mutex);
1511 _dbus_assert (message == connection->message_borrowed);
1513 connection->message_borrowed = NULL;
1514 dbus_condvar_wake_all (connection->message_returned_cond);
1516 dbus_mutex_unlock (connection->mutex);
1523 dbus_connection_steal_borrowed_message (DBusConnection *connection,
1524 DBusMessage *message)
1526 DBusMessage *pop_message;
1528 dbus_mutex_lock (connection->mutex);
1530 _dbus_assert (message == connection->message_borrowed);
1532 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
1533 _dbus_assert (message == pop_message);
1535 connection->n_incoming -= 1;
1537 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
1538 message, connection->n_incoming);
1540 connection->message_borrowed = NULL;
1541 dbus_condvar_wake_all (connection->message_returned_cond);
1543 dbus_mutex_unlock (connection->mutex);
1547 /* See dbus_connection_pop_message, but requires the caller to own
1548 * the lock before calling. May drop the lock while running.
1551 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
1553 if (connection->message_borrowed != NULL)
1554 _dbus_connection_wait_for_borrowed (connection);
1556 if (connection->n_incoming > 0)
1558 DBusMessage *message;
1560 message = _dbus_list_pop_first (&connection->incoming_messages);
1561 connection->n_incoming -= 1;
1563 _dbus_verbose ("Message %p removed from incoming queue %p, %d incoming\n",
1564 message, connection, connection->n_incoming);
1574 * Returns the first-received message from the incoming message queue,
1575 * removing it from the queue. The caller owns a reference to the
1576 * returned message. If the queue is empty, returns #NULL.
1578 * @param connection the connection.
1579 * @returns next message in the incoming queue.
1582 dbus_connection_pop_message (DBusConnection *connection)
1584 DBusMessage *message;
1585 dbus_mutex_lock (connection->mutex);
1587 message = _dbus_connection_pop_message_unlocked (connection);
1589 dbus_mutex_unlock (connection->mutex);
1595 * Acquire the dispatcher. This must be done before dispatching
1596 * messages in order to guarantee the right order of
1597 * message delivery. May sleep and drop the connection mutex
1598 * while waiting for the dispatcher.
1600 * @param connection the connection.
1603 _dbus_connection_acquire_dispatch (DBusConnection *connection)
1605 if (connection->dispatch_acquired)
1606 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
1607 _dbus_assert (!connection->dispatch_acquired);
1609 connection->dispatch_acquired = TRUE;
1613 * Release the dispatcher when you're done with it. Only call
1614 * after you've acquired the dispatcher. Wakes up at most one
1615 * thread currently waiting to acquire the dispatcher.
1617 * @param connection the connection.
1620 _dbus_connection_release_dispatch (DBusConnection *connection)
1622 _dbus_assert (connection->dispatch_acquired);
1624 connection->dispatch_acquired = FALSE;
1625 dbus_condvar_wake_one (connection->dispatch_cond);
1629 _dbus_connection_failed_pop (DBusConnection *connection,
1630 DBusList *message_link)
1632 _dbus_list_prepend_link (&connection->incoming_messages,
1634 connection->n_incoming += 1;
1638 * Pops the first-received message from the current incoming message
1639 * queue, runs any handlers for it, then unrefs the message.
1641 * @param connection the connection
1642 * @returns #TRUE if the queue is not empty after dispatch
1645 dbus_connection_dispatch_message (DBusConnection *connection)
1647 DBusMessageHandler *handler;
1648 DBusMessage *message;
1649 DBusList *link, *filter_list_copy, *message_link;
1650 DBusHandlerResult result;
1651 ReplyHandlerData *reply_handler_data;
1653 dbus_int32_t reply_serial;
1655 /* Preallocate link so we can put the message back on failure */
1656 message_link = _dbus_list_alloc_link (NULL);
1657 if (message_link == NULL)
1660 dbus_mutex_lock (connection->mutex);
1662 /* We need to ref the connection since the callback could potentially
1663 * drop the last ref to it */
1664 _dbus_connection_ref_unlocked (connection);
1666 _dbus_connection_acquire_dispatch (connection);
1668 /* This call may drop the lock during the execution (if waiting for
1669 * borrowed messages to be returned) but the order of message
1670 * dispatch if several threads call dispatch_message is still
1671 * protected by the lock, since only one will get the lock, and that
1672 * one will finish the message dispatching
1674 message = _dbus_connection_pop_message_unlocked (connection);
1675 if (message == NULL)
1677 _dbus_connection_release_dispatch (connection);
1678 dbus_mutex_unlock (connection->mutex);
1679 dbus_connection_unref (connection);
1683 message_link->data = message;
1685 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
1687 reply_serial = dbus_message_get_reply_serial (message);
1688 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
1691 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
1693 _dbus_connection_release_dispatch (connection);
1694 dbus_mutex_unlock (connection->mutex);
1695 _dbus_connection_failed_pop (connection, message_link);
1696 dbus_connection_unref (connection);
1700 _dbus_list_foreach (&filter_list_copy,
1701 (DBusForeachFunction)dbus_message_handler_ref,
1704 /* We're still protected from dispatch_message reentrancy here
1705 * since we acquired the dispatcher
1707 dbus_mutex_unlock (connection->mutex);
1709 link = _dbus_list_get_first_link (&filter_list_copy);
1710 while (link != NULL)
1712 DBusMessageHandler *handler = link->data;
1713 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
1715 result = _dbus_message_handler_handle_message (handler, connection,
1718 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1724 _dbus_list_foreach (&filter_list_copy,
1725 (DBusForeachFunction)dbus_message_handler_unref,
1727 _dbus_list_clear (&filter_list_copy);
1729 dbus_mutex_lock (connection->mutex);
1731 /* Did a reply we were waiting on get filtered? */
1732 if (reply_handler_data && result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1734 /* Queue the timeout immediately! */
1735 if (reply_handler_data->timeout_link)
1737 _dbus_connection_queue_synthesized_message_link (connection,
1738 reply_handler_data->timeout_link);
1739 reply_handler_data->timeout_link = NULL;
1743 /* We already queued the timeout? Then it was filtered! */
1744 _dbus_warn ("The timeout error with reply serial %d was filtered, so the reply handler will never be called.\n", reply_serial);
1748 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1751 if (reply_handler_data)
1753 dbus_mutex_unlock (connection->mutex);
1754 result = _dbus_message_handler_handle_message (reply_handler_data->handler,
1755 connection, message);
1756 reply_handler_data_free (reply_handler_data);
1757 dbus_mutex_lock (connection->mutex);
1761 name = dbus_message_get_name (message);
1764 handler = _dbus_hash_table_lookup_string (connection->handler_table,
1766 if (handler != NULL)
1768 /* We're still protected from dispatch_message reentrancy here
1769 * since we acquired the dispatcher */
1770 dbus_mutex_unlock (connection->mutex);
1771 result = _dbus_message_handler_handle_message (handler, connection,
1773 dbus_mutex_lock (connection->mutex);
1774 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1780 _dbus_connection_release_dispatch (connection);
1781 dbus_mutex_unlock (connection->mutex);
1782 _dbus_list_free_link (message_link);
1783 dbus_connection_unref (connection);
1784 dbus_message_unref (message);
1786 return connection->n_incoming > 0;
1790 * Sets the watch functions for the connection. These functions are
1791 * responsible for making the application's main loop aware of file
1792 * descriptors that need to be monitored for events, using select() or
1793 * poll(). When using Qt, typically the DBusAddWatchFunction would
1794 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
1795 * could call g_io_add_watch(), or could be used as part of a more
1796 * elaborate GSource.
1798 * The DBusWatch can be queried for the file descriptor to watch using
1799 * dbus_watch_get_fd(), and for the events to watch for using
1800 * dbus_watch_get_flags(). The flags returned by
1801 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
1802 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
1803 * all watches implicitly include a watch for hangups, errors, and
1804 * other exceptional conditions.
1806 * Once a file descriptor becomes readable or writable, or an exception
1807 * occurs, dbus_connection_handle_watch() should be called to
1808 * notify the connection of the file descriptor's condition.
1810 * dbus_connection_handle_watch() cannot be called during the
1811 * DBusAddWatchFunction, as the connection will not be ready to handle
1814 * It is not allowed to reference a DBusWatch after it has been passed
1815 * to remove_function.
1817 * If #FALSE is returned due to lack of memory, the failure may be due
1818 * to a #FALSE return from the new add_function. If so, the
1819 * add_function may have been called successfully one or more times,
1820 * but the remove_function will also have been called to remove any
1821 * successful adds. i.e. if #FALSE is returned the net result
1822 * should be that dbus_connection_set_watch_functions() has no effect,
1823 * but the add_function and remove_function may have been called.
1825 * @param connection the connection.
1826 * @param add_function function to begin monitoring a new descriptor.
1827 * @param remove_function function to stop monitoring a descriptor.
1828 * @param data data to pass to add_function and remove_function.
1829 * @param free_data_function function to be called to free the data.
1830 * @returns #FALSE on failure (no memory)
1833 dbus_connection_set_watch_functions (DBusConnection *connection,
1834 DBusAddWatchFunction add_function,
1835 DBusRemoveWatchFunction remove_function,
1837 DBusFreeFunction free_data_function)
1841 dbus_mutex_lock (connection->mutex);
1842 /* ref connection for slightly better reentrancy */
1843 _dbus_connection_ref_unlocked (connection);
1845 retval = _dbus_watch_list_set_functions (connection->watches,
1846 add_function, remove_function,
1847 data, free_data_function);
1849 dbus_mutex_unlock (connection->mutex);
1850 /* drop our paranoid refcount */
1851 dbus_connection_unref (connection);
1857 * Sets the timeout functions for the connection. These functions are
1858 * responsible for making the application's main loop aware of timeouts.
1859 * When using Qt, typically the DBusAddTimeoutFunction would create a
1860 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
1863 * The DBusTimeout can be queried for the timer interval using
1864 * dbus_timeout_get_interval(). dbus_timeout_handle() should
1865 * be called repeatedly, each time the interval elapses, starting
1866 * after it has elapsed once. The timeout stops firing when
1867 * it is removed with the given remove_function.
1869 * @param connection the connection.
1870 * @param add_function function to add a timeout.
1871 * @param remove_function function to remove a timeout.
1872 * @param data data to pass to add_function and remove_function.
1873 * @param free_data_function function to be called to free the data.
1874 * @returns #FALSE on failure (no memory)
1877 dbus_connection_set_timeout_functions (DBusConnection *connection,
1878 DBusAddTimeoutFunction add_function,
1879 DBusRemoveTimeoutFunction remove_function,
1881 DBusFreeFunction free_data_function)
1885 dbus_mutex_lock (connection->mutex);
1886 /* ref connection for slightly better reentrancy */
1887 _dbus_connection_ref_unlocked (connection);
1889 retval = _dbus_timeout_list_set_functions (connection->timeouts,
1890 add_function, remove_function,
1891 data, free_data_function);
1893 dbus_mutex_unlock (connection->mutex);
1894 /* drop our paranoid refcount */
1895 dbus_connection_unref (connection);
1901 * Sets the mainloop wakeup function for the connection. Thi function is
1902 * responsible for waking up the main loop (if its sleeping) when some some
1903 * change has happened to the connection that the mainloop needs to reconsiders
1904 * (e.g. a message has been queued for writing).
1905 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
1906 * When using GLib, it would call g_main_context_wakeup().
1909 * @param connection the connection.
1910 * @param wakeup_main_function function to wake up the mainloop
1911 * @param data data to pass wakeup_main_function
1912 * @param free_data_function function to be called to free the data.
1915 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
1916 DBusWakeupMainFunction wakeup_main_function,
1918 DBusFreeFunction free_data_function)
1921 DBusFreeFunction old_free_data;
1923 dbus_mutex_lock (connection->mutex);
1924 old_data = connection->wakeup_main_data;
1925 old_free_data = connection->free_wakeup_main_data;
1927 connection->wakeup_main_function = wakeup_main_function;
1928 connection->wakeup_main_data = data;
1929 connection->free_wakeup_main_data = free_data_function;
1931 dbus_mutex_unlock (connection->mutex);
1933 /* Callback outside the lock */
1935 (*old_free_data) (old_data);
1939 * Called to notify the connection when a previously-added watch
1940 * is ready for reading or writing, or has an exception such
1943 * @param connection the connection.
1944 * @param watch the watch.
1945 * @param condition the current condition of the file descriptors being watched.
1948 dbus_connection_handle_watch (DBusConnection *connection,
1950 unsigned int condition)
1952 dbus_mutex_lock (connection->mutex);
1953 _dbus_connection_acquire_io_path (connection, -1);
1954 _dbus_transport_handle_watch (connection->transport,
1956 _dbus_connection_release_io_path (connection);
1957 dbus_mutex_unlock (connection->mutex);
1961 * Adds a message filter. Filters are handlers that are run on
1962 * all incoming messages, prior to the normal handlers
1963 * registered with dbus_connection_register_handler().
1964 * Filters are run in the order that they were added.
1965 * The same handler can be added as a filter more than once, in
1966 * which case it will be run more than once.
1967 * Filters added during a filter callback won't be run on the
1968 * message being processed.
1970 * @param connection the connection
1971 * @param handler the handler
1972 * @returns #TRUE on success, #FALSE if not enough memory.
1975 dbus_connection_add_filter (DBusConnection *connection,
1976 DBusMessageHandler *handler)
1978 dbus_mutex_lock (connection->mutex);
1979 if (!_dbus_message_handler_add_connection (handler, connection))
1981 dbus_mutex_unlock (connection->mutex);
1985 if (!_dbus_list_append (&connection->filter_list,
1988 _dbus_message_handler_remove_connection (handler, connection);
1989 dbus_mutex_unlock (connection->mutex);
1993 dbus_mutex_unlock (connection->mutex);
1998 * Removes a previously-added message filter. It is a programming
1999 * error to call this function for a handler that has not
2000 * been added as a filter. If the given handler was added
2001 * more than once, only one instance of it will be removed
2002 * (the most recently-added instance).
2004 * @param connection the connection
2005 * @param handler the handler to remove
2009 dbus_connection_remove_filter (DBusConnection *connection,
2010 DBusMessageHandler *handler)
2012 dbus_mutex_lock (connection->mutex);
2013 if (!_dbus_list_remove_last (&connection->filter_list, handler))
2015 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
2016 dbus_mutex_unlock (connection->mutex);
2020 _dbus_message_handler_remove_connection (handler, connection);
2022 dbus_mutex_unlock (connection->mutex);
2026 * Registers a handler for a list of message names. A single handler
2027 * can be registered for any number of message names, but each message
2028 * name can only have one handler at a time. It's not allowed to call
2029 * this function with the name of a message that already has a
2030 * handler. If the function returns #FALSE, the handlers were not
2031 * registered due to lack of memory.
2033 * @todo the messages_to_handle arg may be more convenient if it's a
2034 * single string instead of an array. Though right now MessageHandler
2035 * is sort of designed to say be associated with an entire object with
2036 * multiple methods, that's why for example the connection only
2037 * weakrefs it. So maybe the "manual" API should be different.
2039 * @param connection the connection
2040 * @param handler the handler
2041 * @param messages_to_handle the messages to handle
2042 * @param n_messages the number of message names in messages_to_handle
2043 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
2047 dbus_connection_register_handler (DBusConnection *connection,
2048 DBusMessageHandler *handler,
2049 const char **messages_to_handle,
2054 dbus_mutex_lock (connection->mutex);
2056 while (i < n_messages)
2061 key = _dbus_strdup (messages_to_handle[i]);
2065 if (!_dbus_hash_iter_lookup (connection->handler_table,
2073 if (_dbus_hash_iter_get_value (&iter) != NULL)
2075 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
2076 messages_to_handle[i]);
2077 dbus_free (key); /* won't have replaced the old key with the new one */
2081 if (!_dbus_message_handler_add_connection (handler, connection))
2083 _dbus_hash_iter_remove_entry (&iter);
2084 /* key has freed on nuking the entry */
2088 _dbus_hash_iter_set_value (&iter, handler);
2093 dbus_mutex_unlock (connection->mutex);
2097 /* unregister everything registered so far,
2098 * so we don't fail partially
2100 dbus_connection_unregister_handler (connection,
2105 dbus_mutex_unlock (connection->mutex);
2110 * Unregisters a handler for a list of message names. The handlers
2111 * must have been previously registered.
2113 * @param connection the connection
2114 * @param handler the handler
2115 * @param messages_to_handle the messages to handle
2116 * @param n_messages the number of message names in messages_to_handle
2120 dbus_connection_unregister_handler (DBusConnection *connection,
2121 DBusMessageHandler *handler,
2122 const char **messages_to_handle,
2127 dbus_mutex_lock (connection->mutex);
2129 while (i < n_messages)
2133 if (!_dbus_hash_iter_lookup (connection->handler_table,
2134 (char*) messages_to_handle[i], FALSE,
2137 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
2138 messages_to_handle[i]);
2140 else if (_dbus_hash_iter_get_value (&iter) != handler)
2142 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
2143 messages_to_handle[i]);
2147 _dbus_hash_iter_remove_entry (&iter);
2148 _dbus_message_handler_remove_connection (handler, connection);
2154 dbus_mutex_unlock (connection->mutex);
2157 static DBusDataSlotAllocator slot_allocator;
2160 * Initialize the mutex used for #DBusConnection data
2161 * slot reservations.
2163 * @returns the mutex
2166 _dbus_connection_slots_init_lock (void)
2168 if (!_dbus_data_slot_allocator_init (&slot_allocator))
2171 return slot_allocator.lock;
2175 * Allocates an integer ID to be used for storing application-specific
2176 * data on any DBusConnection. The allocated ID may then be used
2177 * with dbus_connection_set_data() and dbus_connection_get_data().
2178 * If allocation fails, -1 is returned. Again, the allocated
2179 * slot is global, i.e. all DBusConnection objects will
2180 * have a slot with the given integer ID reserved.
2182 * @returns -1 on failure, otherwise the data slot ID
2185 dbus_connection_allocate_data_slot (void)
2187 return _dbus_data_slot_allocator_alloc (&slot_allocator);
2191 * Deallocates a global ID for connection data slots.
2192 * dbus_connection_get_data() and dbus_connection_set_data()
2193 * may no longer be used with this slot.
2194 * Existing data stored on existing DBusConnection objects
2195 * will be freed when the connection is finalized,
2196 * but may not be retrieved (and may only be replaced
2197 * if someone else reallocates the slot).
2199 * @param slot the slot to deallocate
2202 dbus_connection_free_data_slot (int slot)
2204 _dbus_data_slot_allocator_free (&slot_allocator, slot);
2208 * Stores a pointer on a DBusConnection, along
2209 * with an optional function to be used for freeing
2210 * the data when the data is set again, or when
2211 * the connection is finalized. The slot number
2212 * must have been allocated with dbus_connection_allocate_data_slot().
2214 * @param connection the connection
2215 * @param slot the slot number
2216 * @param data the data to store
2217 * @param free_data_func finalizer function for the data
2218 * @returns #TRUE if there was enough memory to store the data
2221 dbus_connection_set_data (DBusConnection *connection,
2224 DBusFreeFunction free_data_func)
2226 DBusFreeFunction old_free_func;
2230 dbus_mutex_lock (connection->mutex);
2232 retval = _dbus_data_slot_list_set (&slot_allocator,
2233 &connection->slot_list,
2234 slot, data, free_data_func,
2235 &old_free_func, &old_data);
2237 dbus_mutex_unlock (connection->mutex);
2241 /* Do the actual free outside the connection lock */
2243 (* old_free_func) (old_data);
2250 * Retrieves data previously set with dbus_connection_set_data().
2251 * The slot must still be allocated (must not have been freed).
2253 * @param connection the connection
2254 * @param slot the slot to get data from
2255 * @returns the data, or #NULL if not found
2258 dbus_connection_get_data (DBusConnection *connection,
2263 dbus_mutex_lock (connection->mutex);
2265 res = _dbus_data_slot_list_get (&slot_allocator,
2266 &connection->slot_list,
2269 dbus_mutex_unlock (connection->mutex);
2275 * This function sets a global flag for whether dbus_connection_new()
2276 * will set SIGPIPE behavior to SIG_IGN.
2278 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
2281 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
2283 _dbus_modify_sigpipe = will_modify_sigpipe;
2287 * Specifies the maximum size message this connection is allowed to
2288 * receive. Larger messages will result in disconnecting the
2291 * @param connection a #DBusConnection
2292 * @param size maximum message size the connection can receive, in bytes
2295 dbus_connection_set_max_message_size (DBusConnection *connection,
2298 dbus_mutex_lock (connection->mutex);
2299 _dbus_transport_set_max_message_size (connection->transport,
2301 dbus_mutex_unlock (connection->mutex);
2305 * Gets the value set by dbus_connection_set_max_message_size().
2307 * @param connection the connection
2308 * @returns the max size of a single message
2311 dbus_connection_get_max_message_size (DBusConnection *connection)
2314 dbus_mutex_lock (connection->mutex);
2315 res = _dbus_transport_get_max_message_size (connection->transport);
2316 dbus_mutex_unlock (connection->mutex);
2321 * Sets the maximum total number of bytes that can be used for all messages
2322 * received on this connection. Messages count toward the maximum until
2323 * they are finalized. When the maximum is reached, the connection will
2324 * not read more data until some messages are finalized.
2326 * The semantics of the maximum are: if outstanding messages are
2327 * already above the maximum, additional messages will not be read.
2328 * The semantics are not: if the next message would cause us to exceed
2329 * the maximum, we don't read it. The reason is that we don't know the
2330 * size of a message until after we read it.
2332 * Thus, the max live messages size can actually be exceeded
2333 * by up to the maximum size of a single message.
2335 * Also, if we read say 1024 bytes off the wire in a single read(),
2336 * and that contains a half-dozen small messages, we may exceed the
2337 * size max by that amount. But this should be inconsequential.
2339 * @param connection the connection
2340 * @param size the maximum size in bytes of all outstanding messages
2343 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
2346 dbus_mutex_lock (connection->mutex);
2347 _dbus_transport_set_max_live_messages_size (connection->transport,
2349 dbus_mutex_unlock (connection->mutex);
2353 * Gets the value set by dbus_connection_set_max_live_messages_size().
2355 * @param connection the connection
2356 * @returns the max size of all live messages
2359 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
2362 dbus_mutex_lock (connection->mutex);
2363 res = _dbus_transport_get_max_live_messages_size (connection->transport);
2364 dbus_mutex_unlock (connection->mutex);