Lines Matching refs:connection

2 /* dbus-connection.c DBusConnection object
27 #include "dbus-connection.h"
32 #include "dbus-connection-internal.h"
50 #define TOOK_LOCK_CHECK(connection)
51 #define RELEASING_LOCK_CHECK(connection)
52 #define HAVE_LOCK_CHECK(connection)
54 #define TOOK_LOCK_CHECK(connection) do { \
55 _dbus_assert (!(connection)->have_connection_lock); \
56 (connection)->have_connection_lock = TRUE; \
58 #define RELEASING_LOCK_CHECK(connection) do { \
59 _dbus_assert ((connection)->have_connection_lock); \
60 (connection)->have_connection_lock = FALSE; \
62 #define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
68 #define CONNECTION_LOCK(connection) do { \
70 _dbus_rmutex_lock ((connection)->mutex); \
71 TOOK_LOCK_CHECK (connection); \
74 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock (connection)
76 #define SLOTS_LOCK(connection) do { \
77 _dbus_rmutex_lock ((connection)->slot_mutex); \
80 #define SLOTS_UNLOCK(connection) do { \
81 _dbus_rmutex_unlock ((connection)->slot_mutex); \
95 * A DBusConnection represents a connection to another
96 * application. Messages can be sent and received via this connection.
99 * connection to the well-known message buses.
102 * message transport mechanism such as a socket. The connection
119 * The connection provides #DBusWatch and #DBusTimeout objects to
143 * functions to integrate the connection with your application's main
164 * When a connection is disconnected, you are guaranteed to get a
170 * until that connection has been disconnected.
173 * connection is disconnected. However, "Disconnected" will always be
209 _dbus_connection_trace_ref (DBusConnection *connection,
216 _dbus_trace_ref ("DBusConnection", connection, old_refcount, new_refcount,
250 DBusConnection *connection; /**< Connection we'd send the message to */
294 DBusRMutex *slot_mutex; /**< Lock on slot_list so overall connection lock need not be taken */
312 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
314 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
317 * from connection->mutex and all bitfields in a word have to be read/written together.
323 unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */
334 * such as closing the connection.
342 int generation; /**< _dbus_current_generation that should correspond to this connection */
346 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
347 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
349 static void _dbus_connection_last_unref (DBusConnection *connection);
350 static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
351 static void _dbus_connection_release_dispatch (DBusConnection *connection);
352 static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
353 static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection);
354 static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection);
355 static dbus_bool_t _dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
391 * Acquires the connection lock.
393 * @param connection the connection.
396 _dbus_connection_lock (DBusConnection *connection)
398 CONNECTION_LOCK (connection);
402 * Releases the connection lock.
404 * @param connection the connection.
407 _dbus_connection_unlock (DBusConnection *connection)
419 expired_messages = connection->expired_messages;
420 connection->expired_messages = NULL;
422 RELEASING_LOCK_CHECK (connection);
423 _dbus_rmutex_unlock (connection->mutex);
441 * @param connection the connection.
444 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
446 if (connection->wakeup_main_function)
447 (*connection->wakeup_main_function) (connection->wakeup_main_data);
454 * @param connection the connection.
464 _dbus_connection_test_get_locks (DBusConnection *connection,
471 *mutex_loc = (DBusMutex *) connection->mutex;
472 *dispatch_mutex_loc = (DBusMutex *) connection->dispatch_mutex;
473 *io_path_mutex_loc = (DBusMutex *) connection->io_path_mutex;
474 *dispatch_cond_loc = connection->dispatch_cond;
475 *io_path_cond_loc = connection->io_path_cond;
484 * @param connection the connection.
488 _dbus_connection_queue_received_message_link (DBusConnection *connection,
495 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
497 _dbus_list_append_link (&connection->incoming_messages,
505 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
510 _dbus_connection_remove_timeout_unlocked (connection,
519 connection->n_incoming += 1;
521 _dbus_connection_wakeup_mainloop (connection);
537 connection,
538 connection->n_incoming);
548 * @param connection the connection.
553 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
556 HAVE_LOCK_CHECK (connection);
558 _dbus_list_append_link (&connection->incoming_messages, link);
560 connection->n_incoming += 1;
562 _dbus_connection_wakeup_mainloop (connection);
568 link->data, connection, connection->n_incoming);
574 * Called with connection lock held.
576 * @param connection the connection.
580 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
582 HAVE_LOCK_CHECK (connection);
583 return connection->outgoing_messages != NULL;
592 * @param connection the connection.
596 dbus_connection_has_messages_to_send (DBusConnection *connection)
600 _dbus_return_val_if_fail (connection != NULL, FALSE);
602 CONNECTION_LOCK (connection);
603 v = _dbus_connection_has_messages_to_send_unlocked (connection);
604 CONNECTION_UNLOCK (connection);
613 * @param connection the connection.
617 _dbus_connection_get_message_to_send (DBusConnection *connection)
619 HAVE_LOCK_CHECK (connection);
621 return _dbus_list_get_last (&connection->outgoing_messages);
625 * Notifies the connection that a message has been sent, so the
627 * Called with the connection lock held.
629 * @param connection the connection.
633 _dbus_connection_message_sent_unlocked (DBusConnection *connection,
638 HAVE_LOCK_CHECK (connection);
645 link = _dbus_list_get_last_link (&connection->outgoing_messages);
649 _dbus_list_unlink (&connection->outgoing_messages,
651 _dbus_list_prepend_link (&connection->expired_messages, link);
653 connection->n_outgoing -= 1;
668 connection, connection->n_outgoing);
672 _dbus_message_remove_counter (message, connection->outgoing_counter);
689 protected_change_watch (DBusConnection *connection,
698 HAVE_LOCK_CHECK (connection);
701 * ref on the connection while dropping the connection lock, then
703 * work, since the connection was in a hosed state (no WatchList
718 if (connection->watches)
721 retval = (* add_function) (connection->watches, watch);
725 (* remove_function) (connection->watches, watch);
730 (* toggle_function) (connection->watches, watch, enabled);
740 * Adds a watch using the connection's DBusAddWatchFunction if
746 * @param connection the connection.
751 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
754 return protected_change_watch (connection, watch,
760 * Removes a watch using the connection's DBusRemoveWatchFunction
765 * @param connection the connection.
769 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
772 protected_change_watch (connection, watch,
779 * Toggles a watch and notifies app via connection's
784 * @param connection the connection.
789 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
795 protected_change_watch (connection, watch,
813 protected_change_timeout (DBusConnection *connection,
822 HAVE_LOCK_CHECK (connection);
825 * ref on the connection while dropping the connection lock, then
827 * work, since the connection was in a hosed state (no TimeoutList
842 if (connection->timeouts)
845 retval = (* add_function) (connection->timeouts, timeout);
849 (* remove_function) (connection->timeouts, timeout);
854 (* toggle_function) (connection->timeouts, timeout, enabled);
863 * Adds a timeout using the connection's DBusAddTimeoutFunction if
870 * @param connection the connection.
875 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
878 return protected_change_timeout (connection, timeout,
884 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
889 * @param connection the connection.
893 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
896 protected_change_timeout (connection, timeout,
903 * Toggles a timeout and notifies app via connection's
908 * @param connection the connection.
913 _dbus_connection_toggle_timeout_unlocked (DBusConnection *connection,
917 protected_change_timeout (connection, timeout,
924 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
930 HAVE_LOCK_CHECK (connection);
940 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
943 if (!_dbus_hash_table_insert_int (connection->pending_replies,
947 _dbus_connection_remove_timeout_unlocked (connection, timeout);
950 HAVE_LOCK_CHECK (connection);
958 if (!_dbus_hash_table_insert_int (connection->pending_replies,
962 HAVE_LOCK_CHECK (connection);
969 HAVE_LOCK_CHECK (connection);
978 DBusConnection *connection;
985 connection = _dbus_pending_call_get_connection_unlocked (pending);
987 HAVE_LOCK_CHECK (connection);
991 _dbus_connection_remove_timeout_unlocked (connection,
1002 _dbus_connection_ref_unlocked (connection);
1004 CONNECTION_LOCK (connection);
1005 _dbus_connection_unref_unlocked (connection);
1009 _dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
1015 _dbus_hash_table_remove_int (connection->pending_replies,
1020 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
1031 _dbus_hash_table_remove_int (connection->pending_replies,
1035 _dbus_connection_remove_timeout_unlocked (connection,
1044 * Removes a pending call from the connection, such that
1048 * @param connection the connection
1052 _dbus_connection_remove_pending_call (DBusConnection *connection,
1055 CONNECTION_LOCK (connection);
1056 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
1064 * @param connection the connection.
1069 _dbus_connection_acquire_io_path (DBusConnection *connection,
1074 HAVE_LOCK_CHECK (connection);
1076 /* We don't want the connection to vanish */
1077 _dbus_connection_ref_unlocked (connection);
1080 CONNECTION_UNLOCK (connection);
1083 _dbus_cmutex_lock (connection->io_path_mutex);
1085 _dbus_verbose ("start connection->io_path_acquired = %d timeout = %d\n",
1086 connection->io_path_acquired, timeout_milliseconds);
1090 if (connection->io_path_acquired)
1097 if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1098 connection->io_path_mutex,
1113 while (connection->io_path_acquired)
1116 _dbus_condvar_wait (connection->io_path_cond,
1117 connection->io_path_mutex);
1122 if (!connection->io_path_acquired)
1125 connection->io_path_acquired = TRUE;
1128 _dbus_verbose ("end connection->io_path_acquired = %d we_acquired = %d\n",
1129 connection->io_path_acquired, we_acquired);
1132 _dbus_cmutex_unlock (connection->io_path_mutex);
1134 CONNECTION_LOCK (connection);
1136 HAVE_LOCK_CHECK (connection);
1138 _dbus_connection_unref_unlocked (connection);
1148 * @param connection the connection.
1151 _dbus_connection_release_io_path (DBusConnection *connection)
1153 HAVE_LOCK_CHECK (connection);
1156 _dbus_cmutex_lock (connection->io_path_mutex);
1158 _dbus_assert (connection->io_path_acquired);
1160 _dbus_verbose ("start connection->io_path_acquired = %d\n",
1161 connection->io_path_acquired);
1163 connection->io_path_acquired = FALSE;
1164 _dbus_condvar_wake_one (connection->io_path_cond);
1167 _dbus_cmutex_unlock (connection->io_path_mutex);
1172 * connection, optionally blocking in the process. Each call to
1195 * the _dbus_connection_acquire_io_path releases the connection
1198 * Called with connection lock held.
1200 * @param connection the connection.
1206 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1213 HAVE_LOCK_CHECK (connection);
1215 if (connection->n_outgoing == 0)
1218 if (_dbus_connection_acquire_io_path (connection,
1221 HAVE_LOCK_CHECK (connection);
1228 _dbus_connection_peek_for_reply_unlocked (connection,
1235 _dbus_transport_do_iteration (connection->transport,
1239 _dbus_connection_release_io_path (connection);
1242 HAVE_LOCK_CHECK (connection);
1248 * Creates a new connection for the given transport. A transport
1251 * memory exists to create the connection.
1254 * @returns the new connection, or #NULL on failure.
1259 DBusConnection *connection;
1269 connection = NULL;
1292 connection = dbus_new0 (DBusConnection, 1);
1293 if (connection == NULL)
1296 _dbus_rmutex_new_at_location (&connection->mutex);
1297 if (connection->mutex == NULL)
1300 _dbus_cmutex_new_at_location (&connection->io_path_mutex);
1301 if (connection->io_path_mutex == NULL)
1304 _dbus_cmutex_new_at_location (&connection->dispatch_mutex);
1305 if (connection->dispatch_mutex == NULL)
1308 _dbus_condvar_new_at_location (&connection->dispatch_cond);
1309 if (connection->dispatch_cond == NULL)
1312 _dbus_condvar_new_at_location (&connection->io_path_cond);
1313 if (connection->io_path_cond == NULL)
1316 _dbus_rmutex_new_at_location (&connection->slot_mutex);
1317 if (connection->slot_mutex == NULL)
1335 objects = _dbus_object_tree_new (connection);
1343 _dbus_atomic_inc (&connection->refcount);
1344 connection->transport = transport;
1345 connection->watches = watch_list;
1346 connection->timeouts = timeout_list;
1347 connection->pending_replies = pending_replies;
1348 connection->outgoing_counter = outgoing_counter;
1349 connection->filter_list = NULL;
1350 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1351 connection->objects = objects;
1352 connection->exit_on_disconnect = FALSE;
1353 connection->shareable = FALSE;
1354 connection->route_peer_messages = FALSE;
1355 connection->disconnected_message_arrived = FALSE;
1356 connection->disconnected_message_processed = FALSE;
1359 connection->generation = _dbus_current_generation;
1362 _dbus_data_slot_list_init (&connection->slot_list);
1364 connection->client_serial = 1;
1366 connection->disconnect_message_link = disconnect_link;
1368 CONNECTION_LOCK (connection);
1370 if (!_dbus_transport_set_connection (transport, connection))
1372 CONNECTION_UNLOCK (connection);
1379 CONNECTION_UNLOCK (connection);
1381 _dbus_connection_trace_ref (connection, 0, 1, "new_for_transport");
1382 return connection;
1391 if (connection != NULL)
1393 _dbus_condvar_free_at_location (&connection->io_path_cond);
1394 _dbus_condvar_free_at_location (&connection->dispatch_cond);
1395 _dbus_rmutex_free_at_location (&connection->mutex);
1396 _dbus_cmutex_free_at_location (&connection->io_path_mutex);
1397 _dbus_cmutex_free_at_location (&connection->dispatch_mutex);
1398 _dbus_rmutex_free_at_location (&connection->slot_mutex);
1399 dbus_free (connection);
1421 * Requires that the caller already holds the connection lock.
1423 * @param connection the connection.
1424 * @returns the connection.
1427 _dbus_connection_ref_unlocked (DBusConnection *connection)
1431 _dbus_assert (connection != NULL);
1432 _dbus_assert (connection->generation == _dbus_current_generation);
1434 HAVE_LOCK_CHECK (connection);
1436 old_refcount = _dbus_atomic_inc (&connection->refcount);
1437 _dbus_connection_trace_ref (connection, old_refcount, old_refcount + 1,
1440 return connection;
1445 * Requires that the caller already holds the connection lock.
1447 * @param connection the connection.
1450 _dbus_connection_unref_unlocked (DBusConnection *connection)
1454 HAVE_LOCK_CHECK (connection);
1456 _dbus_assert (connection != NULL);
1458 old_refcount = _dbus_atomic_dec (&connection->refcount);
1460 _dbus_connection_trace_ref (connection, old_refcount, old_refcount - 1,
1464 _dbus_connection_last_unref (connection);
1468 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1472 serial = connection->client_serial++;
1474 if (connection->client_serial == 0)
1475 connection->client_serial = 1;
1498 DBusConnection *connection;
1502 connection = data;
1506 CONNECTION_LOCK (connection);
1508 if (!_dbus_connection_acquire_io_path (connection, 1))
1511 CONNECTION_UNLOCK (connection);
1515 HAVE_LOCK_CHECK (connection);
1516 retval = _dbus_transport_handle_watch (connection->transport,
1519 _dbus_connection_release_io_path (connection);
1521 HAVE_LOCK_CHECK (connection);
1525 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1528 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1540 close_connection_on_shutdown (DBusConnection *connection)
1544 dbus_connection_ref (connection);
1545 _dbus_connection_close_possibly_shared (connection);
1548 while ((message = dbus_connection_pop_message (connection)))
1552 dbus_connection_unref (connection);
1565 DBusConnection *connection;
1571 connection = _dbus_hash_iter_get_value (&iter);
1574 close_connection_on_shutdown (connection);
1577 /* The connection should now be dead and not in our hash ... */
1588 DBusConnection *connection;
1589 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1590 while (connection != NULL)
1593 close_connection_on_shutdown (connection);
1595 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1608 _dbus_verbose ("checking for existing connection\n");
1648 DBusConnection *connection;
1650 connection = _dbus_hash_table_lookup_string (shared_connections,
1653 if (connection)
1657 * hash. So it's safe to ref the connection here.
1663 * The idea is to avoid ever returning a disconnected connection
1665 * drop our shared ref to the connection on connection disconnect,
1666 * and then assert here that the connection is connected, but
1669 CONNECTION_LOCK (connection);
1670 if (_dbus_connection_get_is_connected_unlocked (connection))
1672 _dbus_connection_ref_unlocked (connection);
1673 *result = connection;
1674 _dbus_verbose ("looked up existing connection to server guid %s\n",
1679 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1682 CONNECTION_UNLOCK (connection);
1692 connection_record_shared_unlocked (DBusConnection *connection,
1698 HAVE_LOCK_CHECK (connection);
1699 _dbus_assert (connection->server_guid == NULL);
1700 _dbus_assert (connection->shareable);
1702 /* get a hard ref on this connection, even if
1706 _dbus_connection_ref_unlocked (connection);
1712 if (!_dbus_list_prepend (&shared_connections_no_guid, connection))
1723 * we don't have a lock on the connection when we are doing a hash
1742 guid_key, connection))
1750 connection->server_guid = guid_in_connection;
1752 _dbus_verbose ("stored connection to %s to be shared\n",
1753 connection->server_guid);
1757 _dbus_assert (connection->server_guid != NULL);
1763 connection_forget_shared_unlocked (DBusConnection *connection)
1765 HAVE_LOCK_CHECK (connection);
1767 if (!connection->shareable)
1772 if (connection->server_guid != NULL)
1774 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1775 connection->server_guid);
1778 connection->server_guid))
1779 _dbus_assert_not_reached ("connection was not in the shared table");
1781 dbus_free (connection->server_guid);
1782 connection->server_guid = NULL;
1786 _dbus_list_remove (&shared_connections_no_guid, connection);
1792 _dbus_connection_unref_unlocked (connection);
1800 DBusConnection *connection;
1810 connection = _dbus_connection_new_for_transport (transport);
1814 if (connection == NULL)
1821 _dbus_assert (!connection->have_connection_lock);
1823 return connection;
1827 * If the shared parameter is true, then any existing connection will
1828 * be used (and if a new connection is created, it will be available
1830 * connection will always be created, and the new connection will
1834 * @param shared whether the connection is shared or private
1836 * @returns the connection or #NULL on error
1843 DBusConnection *connection;
1851 _dbus_verbose ("opening %s connection to: %s\n",
1859 connection = NULL;
1865 if (!connection_lookup_shared (entries[i], &connection))
1869 if (connection == NULL)
1871 connection = connection_try_from_address_entry (entries[i],
1874 if (connection != NULL && shared)
1878 connection->shareable = TRUE;
1883 CONNECTION_LOCK (connection);
1885 if (!connection_record_shared_unlocked (connection, guid))
1888 _dbus_connection_close_possibly_shared_and_unlock (connection);
1889 dbus_connection_unref (connection);
1890 connection = NULL;
1893 CONNECTION_UNLOCK (connection);
1897 if (connection)
1911 if (connection == NULL)
1920 return connection;
1924 * Closes a shared OR private connection, while dbus_connection_close() can
1926 * dbus code that owns the connection - an owner must be known,
1929 * @param connection the connection
1932 _dbus_connection_close_possibly_shared (DBusConnection *connection)
1934 _dbus_assert (connection != NULL);
1935 _dbus_assert (connection->generation == _dbus_current_generation);
1937 CONNECTION_LOCK (connection);
1938 _dbus_connection_close_possibly_shared_and_unlock (connection);
1942 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1946 HAVE_LOCK_CHECK (connection);
1948 _dbus_assert (connection != NULL);
1958 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1964 preallocated->connection = connection;
1978 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
1986 _dbus_list_prepend_link (&connection->outgoing_messages,
1999 connection->n_outgoing += 1;
2017 connection,
2018 connection->n_outgoing);
2022 serial = _dbus_connection_get_next_client_serial (connection);
2041 _dbus_connection_do_iteration_unlocked (connection,
2047 if (connection->n_outgoing > 0)
2048 _dbus_connection_wakeup_mainloop (connection);
2052 _dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
2059 HAVE_LOCK_CHECK (connection);
2061 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2066 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2069 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2073 * Like dbus_connection_send(), but assumes the connection
2076 * @param connection the connection
2082 _dbus_connection_send_and_unlock (DBusConnection *connection,
2088 _dbus_assert (connection != NULL);
2091 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2094 CONNECTION_UNLOCK (connection);
2098 _dbus_connection_send_preallocated_and_unlock (connection,
2107 * If the new connection function does not ref the connection, we want to close it.
2109 * A bit of a hack, probably the new connection function should have returned a value
2110 * for whether to close, or should have had to close the connection itself if it
2113 * But, this works OK as long as the new connection function doesn't do anything
2114 * crazy like keep the connection around without ref'ing it.
2116 * We have to lock the connection across refcount check and close in case
2117 * the new connection function spawns a thread that closes and unrefs.
2122 * app refs from the new connection function it is supposed to also close.
2127 * @param connection the connection
2130 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
2134 CONNECTION_LOCK (connection);
2136 refcount = _dbus_atomic_get (&connection->refcount);
2141 _dbus_connection_close_possibly_shared_and_unlock (connection);
2143 CONNECTION_UNLOCK (connection);
2219 _dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
2223 HAVE_LOCK_CHECK (connection);
2225 link = _dbus_list_get_first_link (&connection->incoming_messages);
2236 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2246 check_for_reply_unlocked (DBusConnection *connection,
2251 HAVE_LOCK_CHECK (connection);
2253 link = _dbus_list_get_first_link (&connection->incoming_messages);
2261 _dbus_list_remove_link (&connection->incoming_messages, link);
2262 connection->n_incoming -= 1;
2265 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2272 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2279 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2284 _dbus_hash_iter_init (connection->pending_replies, &iter);
2291 connection);
2294 _dbus_connection_remove_timeout_unlocked (connection,
2300 CONNECTION_LOCK (connection);
2302 HAVE_LOCK_CHECK (connection);
2306 complete_pending_call_and_unlock (DBusConnection *connection,
2312 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2320 check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2326 reply = check_for_reply_unlocked (connection,
2334 complete_pending_call_and_unlock (connection, pending, reply);
2337 CONNECTION_LOCK (connection);
2338 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2339 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2368 DBusConnection *connection;
2380 connection = _dbus_pending_call_get_connection_and_lock (pending);
2383 _dbus_connection_flush_unlocked (connection);
2411 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2416 _dbus_connection_do_iteration_unlocked (connection,
2426 HAVE_LOCK_CHECK (connection);
2430 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2438 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2445 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2453 if (!_dbus_connection_get_is_connected_unlocked (connection))
2462 complete_pending_call_and_unlock (connection, pending, error_msg);
2466 else if (connection->disconnect_message_link == NULL)
2483 _dbus_connection_do_iteration_unlocked (connection,
2511 _dbus_connection_do_iteration_unlocked (connection,
2527 complete_pending_call_and_unlock (connection, pending, NULL);
2530 CONNECTION_LOCK (connection);
2531 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2532 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2545 * Gets a connection to a remote address. If a connection to the given
2546 * address already exists, returns the existing connection with its
2547 * reference count incremented. Otherwise, returns a new connection
2548 * and saves the new connection for possible re-use if a future call
2551 * Use dbus_connection_open_private() to get a dedicated connection
2559 * Because this connection is shared, no user of the connection
2561 * connection you should call dbus_connection_unref().
2570 * @returns new connection, or #NULL on failure.
2576 DBusConnection *connection;
2581 connection = _dbus_connection_open_internal (address,
2585 return connection;
2589 * Opens a new, dedicated connection to a remote address. Unlike
2590 * dbus_connection_open(), always creates a new connection.
2591 * This connection will not be saved or recycled by libdbus.
2598 * When you are done with this connection, you must
2600 * and dbus_connection_unref() to free the connection object.
2603 * connection is already known to be disconnected, for example
2613 * @returns new connection, or #NULL on failure.
2619 DBusConnection *connection;
2624 connection = _dbus_connection_open_internal (address,
2628 return connection;
2634 * @param connection the connection.
2635 * @returns the connection.
2638 dbus_connection_ref (DBusConnection *connection)
2642 _dbus_return_val_if_fail (connection != NULL, NULL);
2643 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
2644 old_refcount = _dbus_atomic_inc (&connection->refcount);
2645 _dbus_connection_trace_ref (connection, old_refcount, old_refcount + 1,
2648 return connection;
2656 DBusConnection *connection = data;
2658 _dbus_message_remove_counter (message, connection->outgoing_counter);
2663 * to the connection has been dropped we should have no thread-related
2667 _dbus_connection_last_unref (DBusConnection *connection)
2671 _dbus_verbose ("Finalizing connection %p\n", connection);
2673 _dbus_assert (_dbus_atomic_get (&connection->refcount) == 0);
2675 /* You have to disconnect the connection before unref:ing it. Otherwise
2678 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
2679 _dbus_assert (connection->server_guid == NULL);
2682 _dbus_object_tree_free_all_unlocked (connection->objects);
2684 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
2685 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
2686 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
2688 _dbus_watch_list_free (connection->watches);
2689 connection->watches = NULL;
2691 _dbus_timeout_list_free (connection->timeouts);
2692 connection->timeouts = NULL;
2694 _dbus_data_slot_list_free (&connection->slot_list);
2696 link = _dbus_list_get_first_link (&connection->filter_list);
2700 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
2708 _dbus_list_clear (&connection->filter_list);
2712 _dbus_object_tree_unref (connection->objects);
2714 _dbus_hash_table_unref (connection->pending_replies);
2715 connection->pending_replies = NULL;
2717 _dbus_list_clear (&connection->filter_list);
2719 _dbus_list_foreach (&connection->outgoing_messages,
2721 connection);
2722 _dbus_list_clear (&connection->outgoing_messages);
2724 _dbus_list_foreach (&connection->incoming_messages,
2727 _dbus_list_clear (&connection->incoming_messages);
2729 _dbus_counter_unref (connection->outgoing_counter);
2731 _dbus_transport_unref (connection->transport);
2733 if (connection->disconnect_message_link)
2735 DBusMessage *message = connection->disconnect_message_link->data;
2737 _dbus_list_free_link (connection->disconnect_message_link);
2740 _dbus_condvar_free_at_location (&connection->dispatch_cond);
2741 _dbus_condvar_free_at_location (&connection->io_path_cond);
2743 _dbus_cmutex_free_at_location (&connection->io_path_mutex);
2744 _dbus_cmutex_free_at_location (&connection->dispatch_mutex);
2746 _dbus_rmutex_free_at_location (&connection->slot_mutex);
2748 _dbus_rmutex_free_at_location (&connection->mutex);
2750 dbus_free (connection);
2757 * Note: it is a bug to drop the last reference to a connection that
2761 * as long as the connection is connected, so you can know that either
2766 * For private connections, the creator of the connection must arrange for
2770 * @param connection the connection.
2773 dbus_connection_unref (DBusConnection *connection)
2777 _dbus_return_if_fail (connection != NULL);
2778 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2780 old_refcount = _dbus_atomic_dec (&connection->refcount);
2782 _dbus_connection_trace_ref (connection, old_refcount, old_refcount - 1,
2788 if (_dbus_transport_get_is_connected (connection->transport))
2790 _dbus_warn_check_failed ("The last reference on a connection was dropped without closing the connection. This is a bug in an application. See dbus_connection_unref() documentation for details.\n%s",
2791 connection->shareable ?
2792 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" :
2793 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
2797 _dbus_connection_last_unref (connection);
2808 * dispatch the connection to cause the dispatch status update.
2811 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2815 HAVE_LOCK_CHECK (connection);
2817 _dbus_verbose ("Disconnecting %p\n", connection);
2820 * the connection if it was shared and libdbus was the only remaining
2823 _dbus_connection_ref_unlocked (connection);
2825 _dbus_transport_disconnect (connection->transport);
2830 * should never again return the newly-disconnected connection.
2832 * However, we only unref the shared connection and exit_on_disconnect when
2836 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2839 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2842 dbus_connection_unref (connection);
2846 * Closes a private connection, so no further data can be sent or received.
2848 * connection.
2850 * Attempts to send messages after closing a connection are safe, but will result in
2853 * This function does not affect the connection's reference count. It's
2854 * safe to close a connection more than once; all calls after the
2855 * first do nothing. It's impossible to "reopen" a connection, a
2856 * new connection must be created. This function may result in a call
2861 * If a connection is dropped by the remote application, it will
2864 * You must close a connection prior to releasing the last reference to
2865 * the connection. If you dbus_connection_unref() for the last time
2866 * without closing the connection, the results are undefined; it
2869 * You may not close a shared connection. Connections created with
2874 * reference as long as the connection is open. Thus, either the
2875 * connection is closed and it is OK to drop the last reference,
2876 * or the connection is open and the app knows it does not have the
2883 * reference, if the connection is not already disconnected.
2885 * @param connection the private (unshared) connection to close
2888 dbus_connection_close (DBusConnection *connection)
2890 _dbus_return_if_fail (connection != NULL);
2891 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2893 CONNECTION_LOCK (connection);
2896 if (connection->shareable)
2898 CONNECTION_UNLOCK (connection);
2905 _dbus_connection_close_possibly_shared_and_unlock (connection);
2909 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2911 HAVE_LOCK_CHECK (connection);
2912 return _dbus_transport_get_is_connected (connection->transport);
2916 * Gets whether the connection is currently open. A connection may
2918 * exits; a connection may also be disconnected with
2925 * @param connection the connection.
2926 * @returns #TRUE if the connection is still alive.
2929 dbus_connection_get_is_connected (DBusConnection *connection)
2933 _dbus_return_val_if_fail (connection != NULL, FALSE);
2935 CONNECTION_LOCK (connection);
2936 res = _dbus_connection_get_is_connected_unlocked (connection);
2937 CONNECTION_UNLOCK (connection);
2943 * Gets whether the connection was authenticated. (Note that
2944 * if the connection was authenticated then disconnected,
2947 * @param connection the connection
2948 * @returns #TRUE if the connection was ever authenticated
2951 dbus_connection_get_is_authenticated (DBusConnection *connection)
2955 _dbus_return_val_if_fail (connection != NULL, FALSE);
2957 CONNECTION_LOCK (connection);
2958 res = _dbus_transport_get_is_authenticated (connection->transport);
2959 CONNECTION_UNLOCK (connection);
2965 * Gets whether the connection is not authenticated as a specific
2966 * user. If the connection is not authenticated, this function
2969 * this returns #FALSE. (Note that if the connection was authenticated
2972 * If the connection is not anonymous, you can use
2981 * @param connection the connection
2985 dbus_connection_get_is_anonymous (DBusConnection *connection)
2989 _dbus_return_val_if_fail (connection != NULL, FALSE);
2991 CONNECTION_LOCK (connection);
2992 res = _dbus_transport_get_is_anonymous (connection->transport);
2993 CONNECTION_UNLOCK (connection);
3000 * connection is on the client side. If the connection is on the
3004 * If a client-side connection is not authenticated yet, the ID may be
3026 * @param connection the connection
3027 * @returns the server ID or #NULL if no memory or the connection is server-side
3030 dbus_connection_get_server_id (DBusConnection *connection)
3034 _dbus_return_val_if_fail (connection != NULL, NULL);
3036 CONNECTION_LOCK (connection);
3037 id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
3038 CONNECTION_UNLOCK (connection);
3044 * Tests whether a certain type can be send via the connection. This
3056 * @param connection the connection
3058 * @returns TRUE if the type may be send via the connection
3061 dbus_connection_can_send_type(DBusConnection *connection,
3064 _dbus_return_val_if_fail (connection != NULL, FALSE);
3076 CONNECTION_LOCK(connection);
3077 b = _dbus_transport_can_pass_unix_fd(connection->transport);
3078 CONNECTION_UNLOCK(connection);
3088 * Set whether _exit() should be called when the connection receives a
3097 * @param connection the connection
3101 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
3104 _dbus_return_if_fail (connection != NULL);
3106 CONNECTION_LOCK (connection);
3107 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
3108 CONNECTION_UNLOCK (connection);
3117 * @param connection the connection we're preallocating for.
3121 dbus_connection_preallocate_send (DBusConnection *connection)
3125 _dbus_return_val_if_fail (connection != NULL, NULL);
3127 CONNECTION_LOCK (connection);
3130 _dbus_connection_preallocate_send_unlocked (connection);
3132 CONNECTION_UNLOCK (connection);
3143 * @param connection the connection
3147 dbus_connection_free_preallocated_send (DBusConnection *connection,
3150 _dbus_return_if_fail (connection != NULL);
3152 _dbus_return_if_fail (connection == preallocated->connection);
3167 * @param connection the connection
3173 dbus_connection_send_preallocated (DBusConnection *connection,
3178 _dbus_return_if_fail (connection != NULL);
3181 _dbus_return_if_fail (preallocated->connection == connection);
3188 CONNECTION_LOCK (connection);
3192 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3195 /* Refuse to send fds on a connection that cannot handle
3198 CONNECTION_UNLOCK (connection);
3204 _dbus_connection_send_preallocated_and_unlock (connection,
3210 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
3216 _dbus_assert (connection != NULL);
3219 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3223 _dbus_connection_send_preallocated_unlocked_no_update (connection,
3240 * fail is lack of memory. Even if the connection is disconnected,
3243 * if the connection is disconnected, you can queue an outgoing message,
3252 * @param connection the connection.
3258 dbus_connection_send (DBusConnection *connection,
3262 _dbus_return_val_if_fail (connection != NULL, FALSE);
3265 CONNECTION_LOCK (connection);
3269 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3272 /* Refuse to send fds on a connection that cannot handle
3275 CONNECTION_UNLOCK (connection);
3281 return _dbus_connection_send_and_unlock (connection,
3289 DBusConnection *connection;
3293 connection = _dbus_pending_call_get_connection_and_lock (pending);
3294 _dbus_connection_ref_unlocked (connection);
3297 connection);
3298 _dbus_connection_remove_timeout_unlocked (connection,
3303 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3306 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3307 dbus_connection_unref (connection);
3338 * @warning if the connection is disconnected or you try to send Unix
3339 * file descriptors on a connection that does not support them, the
3342 * @param connection the connection
3345 * object, or #NULL if connection is disconnected or when you try to
3346 * send Unix file descriptors on a connection that does not support
3355 dbus_connection_send_with_reply (DBusConnection *connection,
3364 _dbus_return_val_if_fail (connection != NULL, FALSE);
3371 CONNECTION_LOCK (connection);
3375 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3378 /* Refuse to send fds on a connection that cannot handle
3382 CONNECTION_UNLOCK (connection);
3388 if (!_dbus_connection_get_is_connected_unlocked (connection))
3390 CONNECTION_UNLOCK (connection);
3395 pending = _dbus_pending_call_new_unlocked (connection,
3401 CONNECTION_UNLOCK (connection);
3409 serial = _dbus_connection_get_next_client_serial (connection);
3420 if (!_dbus_connection_attach_pending_call_unlocked (connection,
3424 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
3426 _dbus_connection_detach_pending_call_and_unlock (connection,
3435 _dbus_connection_detach_pending_call_unlocked (connection, pending);
3441 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3444 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3452 CONNECTION_UNLOCK (connection);
3478 * separate thread from the main connection-dispatching thread, or use
3481 * @param connection the connection
3491 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
3499 _dbus_return_val_if_fail (connection != NULL, NULL);
3506 CONNECTION_LOCK (connection);
3507 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3510 CONNECTION_UNLOCK (connection);
3511 dbus_set_error(error, DBUS_ERROR_FAILED, "Cannot send file descriptors on this connection.");
3514 CONNECTION_UNLOCK (connection);
3518 if (!dbus_connection_send_with_reply (connection, message,
3552 * Assumes connection lock already held.
3556 * @param connection the connection.
3559 _dbus_connection_flush_unlocked (DBusConnection *connection)
3568 HAVE_LOCK_CHECK (connection);
3570 while (connection->n_outgoing > 0 &&
3571 _dbus_connection_get_is_connected_unlocked (connection))
3574 HAVE_LOCK_CHECK (connection);
3575 _dbus_connection_do_iteration_unlocked (connection,
3583 HAVE_LOCK_CHECK (connection);
3585 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3587 HAVE_LOCK_CHECK (connection);
3594 * @param connection the connection.
3597 dbus_connection_flush (DBusConnection *connection)
3606 _dbus_return_if_fail (connection != NULL);
3608 CONNECTION_LOCK (connection);
3610 status = _dbus_connection_flush_unlocked (connection);
3612 HAVE_LOCK_CHECK (connection);
3614 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3624 * @param connection the connection
3630 _dbus_connection_read_write_dispatch (DBusConnection *connection,
3637 /* Need to grab a ref here in case we're a private connection and
3641 dbus_connection_ref (connection);
3642 dstatus = dbus_connection_get_dispatch_status (connection);
3647 dbus_connection_dispatch (connection);
3648 CONNECTION_LOCK (connection);
3654 CONNECTION_LOCK (connection);
3658 CONNECTION_LOCK (connection);
3659 if (_dbus_connection_get_is_connected_unlocked (connection))
3662 _dbus_connection_do_iteration_unlocked (connection,
3671 HAVE_LOCK_CHECK (connection);
3677 progress_possible = connection->n_incoming != 0 ||
3678 connection->disconnect_message_link != NULL;
3680 progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
3682 CONNECTION_UNLOCK (connection);
3684 dbus_connection_unref (connection);
3696 * while (dbus_connection_read_write_dispatch (connection, -1))
3702 * message from the connection (the disconnected signal) is processed.
3716 * processed, NOT whether the connection is connected. This is
3720 * @param connection the connection
3725 dbus_connection_read_write_dispatch (DBusConnection *connection,
3728 _dbus_return_val_if_fail (connection != NULL, FALSE);
3730 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3738 * As long as the connection is open, this function will block until it can
3741 * If the connection is closed, the function returns #FALSE.
3744 * possible, i.e. whether the connection is connected.
3752 * @param connection the connection
3757 dbus_connection_read_write (DBusConnection *connection,
3760 _dbus_return_val_if_fail (connection != NULL, FALSE);
3762 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3771 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3774 HAVE_LOCK_CHECK (connection);
3777 if (connection->disconnect_message_link == NULL &&
3782 connection->disconnected_message_arrived = TRUE;
3802 * @param connection the connection.
3806 dbus_connection_borrow_message (DBusConnection *connection)
3811 _dbus_return_val_if_fail (connection != NULL, NULL);
3818 status = dbus_connection_get_dispatch_status (connection);
3822 CONNECTION_LOCK (connection);
3824 _dbus_connection_acquire_dispatch (connection);
3827 _dbus_assert (connection->message_borrowed == NULL);
3829 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3831 message = connection->message_borrowed;
3833 check_disconnected_message_arrived_unlocked (connection, message);
3837 _dbus_connection_release_dispatch (connection);
3839 CONNECTION_UNLOCK (connection);
3853 * @param connection the connection
3857 dbus_connection_return_message (DBusConnection *connection,
3862 _dbus_return_if_fail (connection != NULL);
3864 _dbus_return_if_fail (message == connection->message_borrowed);
3865 _dbus_return_if_fail (connection->dispatch_acquired);
3867 CONNECTION_LOCK (connection);
3869 _dbus_assert (message == connection->message_borrowed);
3871 connection->message_borrowed = NULL;
3873 _dbus_connection_release_dispatch (connection);
3875 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3876 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3887 * @param connection the connection
3891 dbus_connection_steal_borrowed_message (DBusConnection *connection,
3897 _dbus_return_if_fail (connection != NULL);
3899 _dbus_return_if_fail (message == connection->message_borrowed);
3900 _dbus_return_if_fail (connection->dispatch_acquired);
3902 CONNECTION_LOCK (connection);
3904 _dbus_assert (message == connection->message_borrowed);
3906 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3910 connection->n_incoming -= 1;
3913 message, connection->n_incoming);
3915 connection->message_borrowed = NULL;
3917 _dbus_connection_release_dispatch (connection);
3919 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3920 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3929 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3931 HAVE_LOCK_CHECK (connection);
3933 _dbus_assert (connection->message_borrowed == NULL);
3935 if (connection->n_incoming > 0)
3939 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3940 connection->n_incoming -= 1;
3955 connection, connection->n_incoming);
3960 check_disconnected_message_arrived_unlocked (connection, link->data);
3972 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
3976 HAVE_LOCK_CHECK (connection);
3978 link = _dbus_connection_pop_message_link_unlocked (connection);
3995 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
3998 HAVE_LOCK_CHECK (connection);
4002 _dbus_assert (connection->message_borrowed == NULL);
4004 _dbus_assert (connection->dispatch_acquired);
4006 _dbus_list_prepend_link (&connection->incoming_messages,
4008 connection->n_incoming += 1;
4020 connection, connection->n_incoming);
4042 * @param connection the connection.
4046 dbus_connection_pop_message (DBusConnection *connection)
4056 status = dbus_connection_get_dispatch_status (connection);
4060 CONNECTION_LOCK (connection);
4061 _dbus_connection_acquire_dispatch (connection);
4062 HAVE_LOCK_CHECK (connection);
4064 message = _dbus_connection_pop_message_unlocked (connection);
4068 _dbus_connection_release_dispatch (connection);
4070 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4071 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4078 * connection lock can be dropped to call out to application dispatch
4081 * @param connection the connection.
4084 _dbus_connection_acquire_dispatch (DBusConnection *connection)
4086 HAVE_LOCK_CHECK (connection);
4088 _dbus_connection_ref_unlocked (connection);
4089 CONNECTION_UNLOCK (connection);
4092 _dbus_cmutex_lock (connection->dispatch_mutex);
4094 while (connection->dispatch_acquired)
4097 _dbus_condvar_wait (connection->dispatch_cond,
4098 connection->dispatch_mutex);
4101 _dbus_assert (!connection->dispatch_acquired);
4103 connection->dispatch_acquired = TRUE;
4106 _dbus_cmutex_unlock (connection->dispatch_mutex);
4108 CONNECTION_LOCK (connection);
4109 _dbus_connection_unref_unlocked (connection);
4117 * @param connection the connection.
4120 _dbus_connection_release_dispatch (DBusConnection *connection)
4122 HAVE_LOCK_CHECK (connection);
4125 _dbus_cmutex_lock (connection->dispatch_mutex);
4127 _dbus_assert (connection->dispatch_acquired);
4129 connection->dispatch_acquired = FALSE;
4130 _dbus_condvar_wake_one (connection->dispatch_cond);
4133 _dbus_cmutex_unlock (connection->dispatch_mutex);
4137 _dbus_connection_failed_pop (DBusConnection *connection,
4140 _dbus_list_prepend_link (&connection->incoming_messages,
4142 connection->n_incoming += 1;
4147 notify_disconnected_unlocked (DBusConnection *connection)
4149 HAVE_LOCK_CHECK (connection);
4152 * connection from dbus_bus_get(). We make the same guarantee for
4155 * the connection from the hash.
4157 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
4163 if (connection->n_outgoing > 0)
4168 connection->n_outgoing);
4170 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
4172 _dbus_connection_message_sent_unlocked (connection, link->data);
4179 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
4181 HAVE_LOCK_CHECK (connection);
4183 if (connection->disconnect_message_link != NULL)
4190 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
4195 _dbus_connection_queue_synthesized_message_link (connection,
4196 connection->disconnect_message_link);
4197 connection->disconnect_message_link = NULL;
4206 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
4208 HAVE_LOCK_CHECK (connection);
4210 if (connection->n_incoming > 0)
4212 else if (!_dbus_transport_queue_messages (connection->transport))
4219 status = _dbus_transport_get_dispatch_status (connection->transport);
4220 is_connected = _dbus_transport_get_is_connected (connection->transport);
4233 notify_disconnected_unlocked (connection);
4241 status = notify_disconnected_and_dispatch_complete_unlocked (connection);
4246 else if (connection->n_incoming > 0)
4254 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
4261 HAVE_LOCK_CHECK (connection);
4263 _dbus_connection_ref_unlocked (connection);
4265 changed = new_status != connection->last_dispatch_status;
4267 connection->last_dispatch_status = new_status;
4269 function = connection->dispatch_status_function;
4270 data = connection->dispatch_status_data;
4272 if (connection->disconnected_message_arrived &&
4273 !connection->disconnected_message_processed)
4275 connection->disconnected_message_processed = TRUE;
4281 connection_forget_shared_unlocked (connection);
4283 if (connection->exit_on_disconnect)
4285 CONNECTION_UNLOCK (connection);
4299 CONNECTION_UNLOCK (connection);
4304 connection, new_status,
4306 (* function) (connection, new_status, data);
4309 dbus_connection_unref (connection);
4330 * In particular this happens on initial connection, because all sorts
4334 * @param connection the connection.
4338 dbus_connection_get_dispatch_status (DBusConnection *connection)
4342 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4346 CONNECTION_LOCK (connection);
4348 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4350 CONNECTION_UNLOCK (connection);
4359 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
4366 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
4392 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4412 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4429 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4441 _dbus_list_prepend_link (&connection->expired_messages, expire_link);
4457 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
4463 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
4473 * The incoming data buffer is filled when the connection reads from
4505 * @param connection the connection
4509 dbus_connection_dispatch (DBusConnection *connection)
4519 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4523 CONNECTION_LOCK (connection);
4524 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4528 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4532 /* We need to ref the connection since the callback could potentially
4535 _dbus_connection_ref_unlocked (connection);
4537 _dbus_connection_acquire_dispatch (connection);
4538 HAVE_LOCK_CHECK (connection);
4540 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4547 _dbus_connection_release_dispatch (connection);
4549 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4551 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4553 dbus_connection_unref (connection);
4581 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4586 complete_pending_call_and_unlock (connection, pending, message);
4589 CONNECTION_LOCK (connection);
4595 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4599 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4601 _dbus_connection_release_dispatch (connection);
4602 HAVE_LOCK_CHECK (connection);
4604 _dbus_connection_failed_pop (connection, message_link);
4607 _dbus_connection_update_dispatch_status_and_unlock (connection,
4609 dbus_connection_unref (connection);
4621 CONNECTION_UNLOCK (connection);
4637 result = (* filter->function) (connection, message, filter->user_data);
4650 CONNECTION_LOCK (connection);
4677 HAVE_LOCK_CHECK (connection);
4678 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4682 CONNECTION_LOCK (connection);
4741 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4754 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4757 _dbus_list_prepend_link (&connection->expired_messages, expire_link);
4762 _dbus_verbose (" done dispatching %p (%s %s %s '%s') on connection %p\n", message,
4771 connection);
4782 _dbus_connection_putback_message_link_unlocked (connection,
4793 _dbus_connection_release_dispatch (connection);
4794 HAVE_LOCK_CHECK (connection);
4802 * We have a reference to the connection, and we don't use any cached
4803 * pointers to the connection's internals below this point, so it should
4805 CONNECTION_UNLOCK (connection);
4807 CONNECTION_LOCK (connection);
4814 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4817 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4819 dbus_connection_unref (connection);
4825 * Sets the watch functions for the connection. These functions are
4854 * notify the connection of the file descriptor's condition.
4857 * DBusAddWatchFunction, as the connection will not be ready to handle
4877 * @param connection the connection.
4886 dbus_connection_set_watch_functions (DBusConnection *connection,
4895 _dbus_return_val_if_fail (connection != NULL, FALSE);
4897 CONNECTION_LOCK (connection);
4899 retval = _dbus_watch_list_set_functions (connection->watches,
4904 CONNECTION_UNLOCK (connection);
4910 * Sets the timeout functions for the connection. These functions are
4940 * @param connection the connection.
4949 dbus_connection_set_timeout_functions (DBusConnection *connection,
4958 _dbus_return_val_if_fail (connection != NULL, FALSE);
4960 CONNECTION_LOCK (connection);
4962 retval = _dbus_timeout_list_set_functions (connection->timeouts,
4967 CONNECTION_UNLOCK (connection);
4973 * Sets the mainloop wakeup function for the connection. This function
4976 * connection that the mainloop needs to reconsider (e.g. a message
4981 * @param connection the connection.
4987 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
4995 _dbus_return_if_fail (connection != NULL);
4997 CONNECTION_LOCK (connection);
4998 old_data = connection->wakeup_main_data;
4999 old_free_data = connection->free_wakeup_main_data;
5001 connection->wakeup_main_function = wakeup_main_function;
5002 connection->wakeup_main_data = data;
5003 connection->free_wakeup_main_data = free_data_function;
5005 CONNECTION_UNLOCK (connection);
5027 * @param connection the connection
5033 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
5041 _dbus_return_if_fail (connection != NULL);
5043 CONNECTION_LOCK (connection);
5044 old_data = connection->dispatch_status_data;
5045 old_free_data = connection->free_dispatch_status_data;
5047 connection->dispatch_status_function = function;
5048 connection->dispatch_status_data = data;
5049 connection->free_dispatch_status_data = free_data_function;
5051 CONNECTION_UNLOCK (connection);
5059 * Get the UNIX file descriptor of the connection, if any. This can
5066 * If the connection is socket-based, you can also use
5073 * @param connection the connection
5078 dbus_connection_get_unix_fd (DBusConnection *connection,
5081 _dbus_return_val_if_fail (connection != NULL, FALSE);
5082 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
5089 return dbus_connection_get_socket(connection, fd);
5094 * of the connection, if any. DO NOT read or write to the file descriptor, or try to
5099 * If the connection is not socket-based, this function will return FALSE,
5100 * even if the connection does have a file descriptor of some kind.
5103 * @param connection the connection
5108 dbus_connection_get_socket(DBusConnection *connection,
5113 _dbus_return_val_if_fail (connection != NULL, FALSE);
5114 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
5116 CONNECTION_LOCK (connection);
5118 retval = _dbus_transport_get_socket_fd (connection->transport,
5121 CONNECTION_UNLOCK (connection);
5128 * Gets the UNIX user ID of the connection if known. Returns #TRUE if
5132 * connection.
5136 * clients. The returned UID is the UID the connection authenticated
5142 * You can ask the bus to tell you the UID of another connection though
5145 * @param connection the connection
5150 dbus_connection_get_unix_user (DBusConnection *connection,
5155 _dbus_return_val_if_fail (connection != NULL, FALSE);
5158 CONNECTION_LOCK (connection);
5160 if (!_dbus_transport_get_is_authenticated (connection->transport))
5163 result = _dbus_transport_get_unix_user (connection->transport,
5170 CONNECTION_UNLOCK (connection);
5176 * Gets the process ID of the connection if any.
5179 * connection.
5181 * @param connection the connection
5186 dbus_connection_get_unix_process_id (DBusConnection *connection,
5191 _dbus_return_val_if_fail (connection != NULL, FALSE);
5194 CONNECTION_LOCK (connection);
5196 if (!_dbus_transport_get_is_authenticated (connection->transport))
5199 result = _dbus_transport_get_unix_process_id (connection->transport,
5202 CONNECTION_UNLOCK (connection);
5208 * Gets the ADT audit data of the connection if any.
5211 * connection.
5213 * @param connection the connection
5218 dbus_connection_get_adt_audit_session_data (DBusConnection *connection,
5224 _dbus_return_val_if_fail (connection != NULL, FALSE);
5228 CONNECTION_LOCK (connection);
5230 if (!_dbus_transport_get_is_authenticated (connection->transport))
5233 result = _dbus_transport_get_adt_audit_session_data (connection->transport,
5236 CONNECTION_UNLOCK (connection);
5243 * is allowed to connect. When an incoming connection has
5245 * if it returns #TRUE, the connection is allowed to proceed,
5246 * otherwise the connection is disconnected.
5253 * be invoked when the connection is freed or a new function is set.
5258 * @param connection the connection
5264 dbus_connection_set_unix_user_function (DBusConnection *connection,
5272 _dbus_return_if_fail (connection != NULL);
5274 CONNECTION_LOCK (connection);
5275 _dbus_transport_set_unix_user_function (connection->transport,
5278 CONNECTION_UNLOCK (connection);
5285 * Gets the Windows user SID of the connection if known. Returns
5289 * authenticating the connection.
5293 * clients. The returned user is the user the connection authenticated
5306 * you the user of another connection though if you like; this is done
5311 * @param connection the connection
5316 dbus_connection_get_windows_user (DBusConnection *connection,
5321 _dbus_return_val_if_fail (connection != NULL, FALSE);
5324 CONNECTION_LOCK (connection);
5326 if (!_dbus_transport_get_is_authenticated (connection->transport))
5329 result = _dbus_transport_get_windows_user (connection->transport,
5336 CONNECTION_UNLOCK (connection);
5343 * is allowed to connect. When an incoming connection has
5345 * if it returns #TRUE, the connection is allowed to proceed,
5346 * otherwise the connection is disconnected.
5353 * be invoked when the connection is freed or a new function is set.
5357 * @param connection the connection
5363 dbus_connection_set_windows_user_function (DBusConnection *connection,
5371 _dbus_return_if_fail (connection != NULL);
5373 CONNECTION_LOCK (connection);
5374 _dbus_transport_set_windows_user_function (connection->transport,
5377 CONNECTION_UNLOCK (connection);
5384 * This function must be called on the server side of a connection when the
5385 * connection is first seen in the #DBusNewConnectionFunction. If set to
5386 * #TRUE (the default is #FALSE), then the connection can proceed even if
5396 * authorized as a user; normally, if a connection authorizes as
5406 * @param connection the connection
5410 dbus_connection_set_allow_anonymous (DBusConnection *connection,
5413 _dbus_return_if_fail (connection != NULL);
5415 CONNECTION_LOCK (connection);
5416 _dbus_transport_set_allow_anonymous (connection->transport, value);
5417 CONNECTION_UNLOCK (connection);
5434 * @param connection the connection
5438 dbus_connection_set_route_peer_messages (DBusConnection *connection,
5441 _dbus_return_if_fail (connection != NULL);
5443 CONNECTION_LOCK (connection);
5444 connection->route_peer_messages = TRUE;
5445 CONNECTION_UNLOCK (connection);
5463 * @param connection the connection
5470 dbus_connection_add_filter (DBusConnection *connection,
5477 _dbus_return_val_if_fail (connection != NULL, FALSE);
5486 CONNECTION_LOCK (connection);
5488 if (!_dbus_list_append (&connection->filter_list,
5492 CONNECTION_UNLOCK (connection);
5505 CONNECTION_UNLOCK (connection);
5516 * @param connection the connection
5522 dbus_connection_remove_filter (DBusConnection *connection,
5529 _dbus_return_if_fail (connection != NULL);
5532 CONNECTION_LOCK (connection);
5536 link = _dbus_list_get_last_link (&connection->filter_list);
5544 _dbus_list_remove_link (&connection->filter_list, link);
5550 link = _dbus_list_get_prev_link (&connection->filter_list, link);
5554 CONNECTION_UNLOCK (connection);
5581 * @param connection the connection
5591 _dbus_connection_register_object_path (DBusConnection *connection,
5604 CONNECTION_LOCK (connection);
5606 retval = _dbus_object_tree_register (connection->objects,
5611 CONNECTION_UNLOCK (connection);
5622 * @param connection the connection
5631 dbus_connection_try_register_object_path (DBusConnection *connection,
5637 _dbus_return_val_if_fail (connection != NULL, FALSE);
5642 return _dbus_connection_register_object_path (connection, FALSE, path, vtable, user_data, error);
5653 * @param connection the connection
5661 dbus_connection_register_object_path (DBusConnection *connection,
5669 _dbus_return_val_if_fail (connection != NULL, FALSE);
5674 retval = _dbus_connection_register_object_path (connection, FALSE, path, vtable, user_data, &error);
5692 * @param connection the connection
5701 dbus_connection_try_register_fallback (DBusConnection *connection,
5707 _dbus_return_val_if_fail (connection != NULL, FALSE);
5712 return _dbus_connection_register_object_path (connection, TRUE, path, vtable, user_data, error);
5725 * @param connection the connection
5733 dbus_connection_register_fallback (DBusConnection *connection,
5741 _dbus_return_val_if_fail (connection != NULL, FALSE);
5746 retval = _dbus_connection_register_object_path (connection, TRUE, path, vtable, user_data, &error);
5763 * @param connection the connection
5768 dbus_connection_unregister_object_path (DBusConnection *connection,
5773 _dbus_return_val_if_fail (connection != NULL, FALSE);
5780 CONNECTION_LOCK (connection);
5782 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
5794 * @param connection the connection
5800 dbus_connection_get_object_path_data (DBusConnection *connection,
5806 _dbus_return_val_if_fail (connection != NULL, FALSE);
5815 CONNECTION_LOCK (connection);
5817 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
5819 CONNECTION_UNLOCK (connection);
5831 * @param connection the connection
5837 dbus_connection_list_registered (DBusConnection *connection,
5843 _dbus_return_val_if_fail (connection != NULL, FALSE);
5851 CONNECTION_LOCK (connection);
5853 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
5887 * Deallocates a global ID for connection data slots.
5890 * DBusConnection objects will be freed when the connection is
5909 * the connection is finalized. The slot number
5917 * a reference held on the connection while invoking
5918 * dbus_connection_set_data(), or the connection could be
5921 * @param connection the connection
5928 dbus_connection_set_data (DBusConnection *connection,
5937 _dbus_return_val_if_fail (connection != NULL, FALSE);
5940 SLOTS_LOCK (connection);
5943 &connection->slot_list,
5947 SLOTS_UNLOCK (connection);
5951 /* Do the actual free outside the connection lock */
5968 * a reference held on the connection while invoking
5969 * dbus_connection_get_data(), or the connection could be
5972 * @param connection the connection
5977 dbus_connection_get_data (DBusConnection *connection,
5982 _dbus_return_val_if_fail (connection != NULL, NULL);
5984 SLOTS_LOCK (connection);
5987 &connection->slot_list,
5990 SLOTS_UNLOCK (connection);
6008 * Specifies the maximum size message this connection is allowed to
6010 * connection.
6012 * @param connection a #DBusConnection
6013 * @param size maximum message size the connection can receive, in bytes
6016 dbus_connection_set_max_message_size (DBusConnection *connection,
6019 _dbus_return_if_fail (connection != NULL);
6021 CONNECTION_LOCK (connection);
6022 _dbus_transport_set_max_message_size (connection->transport,
6024 CONNECTION_UNLOCK (connection);
6030 * @param connection the connection
6034 dbus_connection_get_max_message_size (DBusConnection *connection)
6038 _dbus_return_val_if_fail (connection != NULL, 0);
6040 CONNECTION_LOCK (connection);
6041 res = _dbus_transport_get_max_message_size (connection->transport);
6042 CONNECTION_UNLOCK (connection);
6048 * connection is allowed to receive. Messages with more unix fds will
6049 * result in disconnecting the connection.
6051 * @param connection a #DBusConnection
6052 * @param size maximum message unix fds the connection can receive
6055 dbus_connection_set_max_message_unix_fds (DBusConnection *connection,
6058 _dbus_return_if_fail (connection != NULL);
6060 CONNECTION_LOCK (connection);
6061 _dbus_transport_set_max_message_unix_fds (connection->transport,
6063 CONNECTION_UNLOCK (connection);
6069 * @param connection the connection
6073 dbus_connection_get_max_message_unix_fds (DBusConnection *connection)
6077 _dbus_return_val_if_fail (connection != NULL, 0);
6079 CONNECTION_LOCK (connection);
6080 res = _dbus_transport_get_max_message_unix_fds (connection->transport);
6081 CONNECTION_UNLOCK (connection);
6087 * received on this connection. Messages count toward the maximum until
6088 * they are finalized. When the maximum is reached, the connection will
6107 * @param connection the connection
6111 dbus_connection_set_max_received_size (DBusConnection *connection,
6114 _dbus_return_if_fail (connection != NULL);
6116 CONNECTION_LOCK (connection);
6117 _dbus_transport_set_max_received_size (connection->transport,
6119 CONNECTION_UNLOCK (connection);
6125 * @param connection the connection
6129 dbus_connection_get_max_received_size (DBusConnection *connection)
6133 _dbus_return_val_if_fail (connection != NULL, 0);
6135 CONNECTION_LOCK (connection);
6136 res = _dbus_transport_get_max_received_size (connection->transport);
6137 CONNECTION_UNLOCK (connection);
6143 * received on this connection. Messages count toward the maximum until
6144 * they are finalized. When the maximum is reached, the connection will
6149 * @param connection the connection
6153 dbus_connection_set_max_received_unix_fds (DBusConnection *connection,
6156 _dbus_return_if_fail (connection != NULL);
6158 CONNECTION_LOCK (connection);
6159 _dbus_transport_set_max_received_unix_fds (connection->transport,
6161 CONNECTION_UNLOCK (connection);
6167 * @param connection the connection
6171 dbus_connection_get_max_received_unix_fds (DBusConnection *connection)
6175 _dbus_return_val_if_fail (connection != NULL, 0);
6177 CONNECTION_LOCK (connection);
6178 res = _dbus_transport_get_max_received_unix_fds (connection->transport);
6179 CONNECTION_UNLOCK (connection);
6190 * @param connection the connection
6194 dbus_connection_get_outgoing_size (DBusConnection *connection)
6198 _dbus_return_val_if_fail (connection != NULL, 0);
6200 CONNECTION_LOCK (connection);
6201 res = _dbus_counter_get_size_value (connection->outgoing_counter);
6202 CONNECTION_UNLOCK (connection);
6208 _dbus_connection_get_stats (DBusConnection *connection,
6220 CONNECTION_LOCK (connection);
6223 *in_messages = connection->n_incoming;
6225 _dbus_transport_get_stats (connection->transport,
6229 *out_messages = connection->n_outgoing;
6232 *out_bytes = _dbus_counter_get_size_value (connection->outgoing_counter);
6235 *out_fds = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
6238 *out_peak_bytes = _dbus_counter_get_peak_size_value (connection->outgoing_counter);
6241 *out_peak_fds = _dbus_counter_get_peak_unix_fd_value (connection->outgoing_counter);
6243 CONNECTION_UNLOCK (connection);
6251 * @param connection the connection
6255 dbus_connection_get_outgoing_unix_fds (DBusConnection *connection)
6259 _dbus_return_val_if_fail (connection != NULL, 0);
6261 CONNECTION_LOCK (connection);
6262 res = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
6263 CONNECTION_UNLOCK (connection);
6269 * Returns the address of the transport object of this connection
6271 * @param connection the connection
6275 _dbus_connection_get_address (DBusConnection *connection)
6277 return _dbus_transport_get_address (connection->transport);