1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "connection.h"
31 #include "expirelist.h"
35 #include <dbus/dbus-list.h>
36 #include <dbus/dbus-hash.h>
37 #include <dbus/dbus-timeout.h>
38 #include <dbus/dbus-connection-internal.h>
39 #include <dbus/dbus-internals.h>
40 #include <dbus/dbus-message-internal.h>
41 #ifdef DBUS_ENABLE_CYNARA
43 #include <cynara-session.h>
47 /* Trim executed commands to this length; we want to keep logs readable */
48 #define MAX_LOG_COMMAND_LEN 50
50 static void bus_connection_remove_transactions (DBusConnection *connection);
54 BusExpireItem expire_item;
56 DBusConnection *will_get_reply;
57 DBusConnection *will_send_reply;
59 dbus_uint32_t reply_serial;
66 DBusList *completed; /**< List of all completed connections */
67 int n_completed; /**< Length of completed list */
68 DBusList *incomplete; /**< List of all not-yet-active connections */
69 int n_incomplete; /**< Length of incomplete list */
71 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
72 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
73 int stamp; /**< Incrementing number */
74 BusExpireList *pending_replies; /**< List of pending replies */
76 /** List of all monitoring connections, a subset of completed.
77 * Each member is a #DBusConnection. */
79 BusMatchmaker *monitor_matchmaker;
81 #ifdef DBUS_ENABLE_STATS
82 int total_match_rules;
84 int peak_match_rules_per_conn;
88 int peak_bus_names_per_conn;
92 static dbus_int32_t connection_data_slot = -1;
96 BusConnections *connections;
97 DBusList *link_in_connection_list;
98 DBusConnection *connection;
99 DBusList *services_owned;
100 int n_services_owned;
101 DBusList *match_rules;
104 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
105 DBusMessage *oom_message;
106 DBusPreallocatedSend *oom_preallocated;
107 BusClientPolicy *policy;
108 DBusList *deferred_messages; /**< Queue of messages deferred due to pending policy check */
110 char *cached_loginfo_string;
111 BusSELinuxID *selinux_id;
112 BusAppArmorConfinement *apparmor_confinement;
114 long connection_tv_sec; /**< Time when we connected (seconds component) */
115 long connection_tv_usec; /**< Time when we connected (microsec component) */
116 int stamp; /**< connections->stamp last time we were traversed */
118 #ifdef DBUS_ENABLE_STATS
119 int peak_match_rules;
121 int peak_pending_replies;
123 int n_pending_unix_fds;
124 DBusTimeout *pending_unix_fds_timeout;
126 /** non-NULL if and only if this is a monitor */
127 DBusList *link_in_monitors;
128 #ifdef DBUS_ENABLE_CYNARA
129 char *cynara_session_id;
133 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
137 static void bus_connection_drop_pending_replies (BusConnections *connections,
138 DBusConnection *connection);
140 static dbus_bool_t expire_incomplete_timeout (void *data);
142 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
145 bus_connection_get_loop (DBusConnection *connection)
147 BusConnectionData *d;
149 d = BUS_CONNECTION_DATA (connection);
150 _dbus_assert(d != NULL);
152 return bus_context_get_loop (d->connections->context);
157 get_connections_for_uid (BusConnections *connections,
163 /* val is NULL is 0 when it isn't in the hash yet */
165 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
168 current_count = _DBUS_POINTER_TO_INT (val);
170 return current_count;
174 adjust_connections_for_uid (BusConnections *connections,
180 current_count = get_connections_for_uid (connections, uid);
182 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
183 ": was %d adjustment %d making %d\n",
184 uid, current_count, adjustment, current_count + adjustment);
186 _dbus_assert (current_count >= 0);
188 current_count += adjustment;
190 _dbus_assert (current_count >= 0);
192 if (current_count == 0)
194 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
201 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
202 uid, _DBUS_INT_TO_POINTER (current_count));
204 /* only positive adjustment can fail as otherwise
205 * a hash entry should already exist
207 _dbus_assert (adjustment > 0 ||
208 (adjustment <= 0 && retval));
215 bus_connection_disconnected (DBusConnection *connection)
217 BusConnectionData *d;
219 BusMatchmaker *matchmaker;
221 d = BUS_CONNECTION_DATA (connection);
222 _dbus_assert (d != NULL);
224 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
225 d->name ? d->name : "(inactive)");
227 /* Delete our match rules */
228 if (d->n_match_rules > 0)
230 matchmaker = bus_context_get_matchmaker (d->connections->context);
231 bus_matchmaker_disconnected (matchmaker, connection);
234 /* Drop any service ownership. Unfortunately, this requires
235 * memory allocation and there doesn't seem to be a good way to
236 * handle it other than sleeping; we can't "fail" the operation of
237 * disconnecting a client, and preallocating a broadcast "service is
238 * now gone" message for every client-service pair seems kind of
241 while ((service = _dbus_list_get_last (&d->services_owned)))
243 BusTransaction *transaction;
248 dbus_error_init (&error);
250 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
251 _dbus_wait_for_memory ();
253 if (!bus_service_remove_owner (service, connection,
254 transaction, &error))
256 _DBUS_ASSERT_ERROR_IS_SET (&error);
258 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
260 dbus_error_free (&error);
261 bus_transaction_cancel_and_free (transaction);
262 _dbus_wait_for_memory ();
267 _dbus_verbose ("Failed to remove service owner: %s %s\n",
268 error.name, error.message);
269 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
273 bus_transaction_execute_and_free (transaction);
276 bus_connection_clear_deferred_messages(connection);
278 bus_dispatch_remove_connection (connection);
280 /* no more watching */
281 if (!dbus_connection_set_watch_functions (connection,
285 _dbus_assert_not_reached ("setting watch functions to NULL failed");
287 if (!dbus_connection_set_timeout_functions (connection,
291 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
293 dbus_connection_set_unix_user_function (connection,
295 dbus_connection_set_windows_user_function (connection,
298 dbus_connection_set_dispatch_status_function (connection,
301 if (d->pending_unix_fds_timeout)
303 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
304 d->pending_unix_fds_timeout);
305 _dbus_timeout_unref (d->pending_unix_fds_timeout);
307 d->pending_unix_fds_timeout = NULL;
308 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
310 bus_connection_remove_transactions (connection);
312 if (d->link_in_monitors != NULL)
314 BusMatchmaker *mm = d->connections->monitor_matchmaker;
317 bus_matchmaker_disconnected (mm, connection);
319 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
320 d->link_in_monitors = NULL;
323 if (d->link_in_connection_list != NULL)
329 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
330 d->link_in_connection_list = NULL;
331 d->connections->n_completed -= 1;
333 if (dbus_connection_get_unix_user (connection, &uid))
335 if (!adjust_connections_for_uid (d->connections,
337 _dbus_assert_not_reached ("adjusting downward should never fail");
342 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
343 d->link_in_connection_list = NULL;
344 d->connections->n_incomplete -= 1;
346 /* If we have dropped below the max. number of incomplete
347 * connections, start accept()ing again */
348 bus_context_check_all_watches (d->connections->context);
351 _dbus_assert (d->connections->n_incomplete >= 0);
352 _dbus_assert (d->connections->n_completed >= 0);
355 bus_connection_drop_pending_replies (d->connections, connection);
357 /* frees "d" as side effect */
358 dbus_connection_set_data (connection,
359 connection_data_slot,
362 dbus_connection_unref (connection);
366 add_connection_watch (DBusWatch *watch,
369 DBusConnection *connection = data;
371 return _dbus_loop_add_watch (bus_connection_get_loop (connection), watch);
375 remove_connection_watch (DBusWatch *watch,
378 DBusConnection *connection = data;
380 _dbus_loop_remove_watch (bus_connection_get_loop (connection), watch);
384 toggle_connection_watch (DBusWatch *watch,
387 DBusConnection *connection = data;
389 _dbus_loop_toggle_watch (bus_connection_get_loop (connection), watch);
393 add_connection_timeout (DBusTimeout *timeout,
396 DBusConnection *connection = data;
398 return _dbus_loop_add_timeout (bus_connection_get_loop (connection), timeout);
402 remove_connection_timeout (DBusTimeout *timeout,
405 DBusConnection *connection = data;
407 _dbus_loop_remove_timeout (bus_connection_get_loop (connection), timeout);
411 dispatch_status_function (DBusConnection *connection,
412 DBusDispatchStatus new_status,
415 DBusLoop *loop = data;
417 if (new_status != DBUS_DISPATCH_COMPLETE)
419 while (!_dbus_loop_queue_dispatch (loop, connection))
420 _dbus_wait_for_memory ();
425 allow_unix_user_function (DBusConnection *connection,
429 BusConnectionData *d;
431 d = BUS_CONNECTION_DATA (connection);
433 _dbus_assert (d != NULL);
435 return bus_context_allow_unix_user (d->connections->context, uid);
439 free_connection_data (void *data)
441 BusConnectionData *d = data;
443 /* services_owned should be NULL since we should be disconnected */
444 _dbus_assert (d->services_owned == NULL);
445 _dbus_assert (d->n_services_owned == 0);
447 _dbus_assert (d->transaction_messages == NULL);
449 if (d->oom_preallocated)
450 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
453 dbus_message_unref (d->oom_message);
456 bus_client_policy_unref (d->policy);
458 if (d->apparmor_confinement)
459 bus_apparmor_confinement_unref (d->apparmor_confinement);
461 dbus_free (d->cached_loginfo_string);
465 #ifdef DBUS_ENABLE_CYNARA
466 free (d->cynara_session_id);
473 bus_connections_new (BusContext *context)
475 BusConnections *connections;
477 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
480 connections = dbus_new0 (BusConnections, 1);
481 if (connections == NULL)
484 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
486 if (connections->completed_by_user == NULL)
489 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
490 expire_incomplete_timeout,
492 if (connections->expire_timeout == NULL)
495 _dbus_timeout_disable (connections->expire_timeout);
497 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
498 bus_context_get_reply_timeout (context),
499 bus_pending_reply_expired,
501 if (connections->pending_replies == NULL)
504 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
505 connections->expire_timeout))
508 connections->refcount = 1;
509 connections->context = context;
514 bus_expire_list_free (connections->pending_replies);
516 _dbus_timeout_unref (connections->expire_timeout);
518 _dbus_hash_table_unref (connections->completed_by_user);
520 dbus_free (connections);
522 dbus_connection_free_data_slot (&connection_data_slot);
528 bus_connections_ref (BusConnections *connections)
530 _dbus_assert (connections->refcount > 0);
531 connections->refcount += 1;
537 bus_connections_unref (BusConnections *connections)
539 _dbus_assert (connections->refcount > 0);
540 connections->refcount -= 1;
541 if (connections->refcount == 0)
543 /* drop all incomplete */
544 while (connections->incomplete != NULL)
546 DBusConnection *connection;
548 connection = connections->incomplete->data;
550 dbus_connection_ref (connection);
551 dbus_connection_close (connection);
552 bus_connection_disconnected (connection);
553 dbus_connection_unref (connection);
556 _dbus_assert (connections->n_incomplete == 0);
558 /* drop all monitors */
559 _dbus_list_clear (&connections->monitors);
561 /* drop all real connections */
562 while (connections->completed != NULL)
564 DBusConnection *connection;
566 connection = connections->completed->data;
568 dbus_connection_ref (connection);
569 dbus_connection_close (connection);
570 bus_connection_disconnected (connection);
571 dbus_connection_unref (connection);
574 _dbus_assert (connections->n_completed == 0);
576 bus_expire_list_free (connections->pending_replies);
578 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
579 connections->expire_timeout);
581 _dbus_timeout_unref (connections->expire_timeout);
583 _dbus_hash_table_unref (connections->completed_by_user);
585 if (connections->monitor_matchmaker != NULL)
586 bus_matchmaker_unref (connections->monitor_matchmaker);
588 dbus_free (connections);
590 dbus_connection_free_data_slot (&connection_data_slot);
594 /* Used for logging */
596 cache_peer_loginfo_string (BusConnectionData *d,
597 DBusConnection *connection)
599 DBusString loginfo_buf;
602 char *windows_sid = NULL, *security_label = NULL;
603 dbus_bool_t prev_added;
605 if (!_dbus_string_init (&loginfo_buf))
609 if (dbus_connection_get_unix_user (connection, &uid))
611 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
617 if (dbus_connection_get_unix_process_id (connection, &pid))
621 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
624 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
626 /* Ignore errors here; we may not have permissions to read the
628 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
629 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
635 if (dbus_connection_get_windows_user (connection, &windows_sid))
637 dbus_bool_t did_append;
641 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
645 did_append = _dbus_string_append_printf (&loginfo_buf,
646 "sid=\"%s\"", windows_sid);
647 dbus_free (windows_sid);
655 if (_dbus_connection_get_linux_security_label (connection, &security_label))
657 dbus_bool_t did_append;
661 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
665 did_append = _dbus_string_append_printf (&loginfo_buf,
666 "label=\"%s\"", security_label);
667 dbus_free (security_label);
668 security_label = NULL;
675 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
678 _dbus_string_free (&loginfo_buf);
682 _dbus_string_free (&loginfo_buf);
683 if (security_label != NULL)
684 dbus_free (security_label);
685 if (windows_sid != NULL)
686 dbus_free (windows_sid);
692 check_pending_fds_cb (DBusConnection *connection)
694 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
695 int n_pending_unix_fds_old;
696 int n_pending_unix_fds_new;
698 _dbus_assert(d != NULL);
700 n_pending_unix_fds_old = d->n_pending_unix_fds;
701 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
703 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
704 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
706 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
708 _dbus_timeout_restart (d->pending_unix_fds_timeout,
709 bus_context_get_pending_fd_timeout (d->connections->context));
712 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
714 _dbus_timeout_disable (d->pending_unix_fds_timeout);
718 d->n_pending_unix_fds = n_pending_unix_fds_new;
722 pending_unix_fds_timeout_cb (void *data)
724 DBusConnection *connection = data;
725 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
728 _dbus_assert (d != NULL);
729 limit = bus_context_get_pending_fd_timeout (d->connections->context);
730 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
731 "Connection \"%s\" (%s) has had Unix fds pending for too long, "
732 "closing it (pending_fd_timeout=%d ms)",
733 d->name != NULL ? d->name : "(null)",
734 bus_connection_get_loginfo (connection),
737 dbus_connection_close (connection);
742 bus_connections_setup_connection (BusConnections *connections,
743 DBusConnection *connection)
746 BusConnectionData *d = NULL;
749 d = dbus_new0 (BusConnectionData, 1);
754 d->connections = connections;
755 d->connection = connection;
757 _dbus_get_monotonic_time (&d->connection_tv_sec,
758 &d->connection_tv_usec);
760 _dbus_assert (connection_data_slot >= 0);
762 if (!dbus_connection_set_data (connection,
763 connection_data_slot,
764 d, free_connection_data))
766 /* We have to free d explicitly, because this is the only code
767 * path where it's non-NULL but dbus_connection_set_data() hasn't
768 * taken responsibility for freeing it. */
774 dbus_connection_set_route_peer_messages (connection, TRUE);
776 dbus_error_init (&error);
777 d->selinux_id = bus_selinux_init_connection_id (connection,
779 if (dbus_error_is_set (&error))
781 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
782 "Unable to set up new connection: %s", error.message);
783 dbus_error_free (&error);
787 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
789 if (dbus_error_is_set (&error))
791 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
792 "Unable to set up new connection: %s", error.message);
793 dbus_error_free (&error);
797 if (!dbus_connection_set_watch_functions (connection,
798 add_connection_watch,
799 remove_connection_watch,
800 toggle_connection_watch,
805 if (!dbus_connection_set_timeout_functions (connection,
806 add_connection_timeout,
807 remove_connection_timeout,
812 /* For now we don't need to set a Windows user function because
813 * there are no policies in the config file controlling what
814 * Windows users can connect. The default 'same user that owns the
815 * bus can connect' behavior of DBusConnection is fine on Windows.
817 dbus_connection_set_unix_user_function (connection,
818 allow_unix_user_function,
821 dbus_connection_set_dispatch_status_function (connection,
822 dispatch_status_function,
823 bus_context_get_loop (connections->context),
826 d->link_in_connection_list = _dbus_list_alloc_link (connection);
827 if (d->link_in_connection_list == NULL)
830 /* Setup the connection with the dispatcher */
831 if (!bus_dispatch_add_connection (connection))
834 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
836 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
838 bus_dispatch_remove_connection (connection);
843 /* Setup pending fds timeout (see #80559) */
844 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
845 pending_unix_fds_timeout_cb,
847 if (d->pending_unix_fds_timeout == NULL)
850 _dbus_timeout_disable (d->pending_unix_fds_timeout);
851 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
852 d->pending_unix_fds_timeout))
855 _dbus_connection_set_pending_fds_function (connection,
856 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
859 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
860 connections->n_incomplete += 1;
862 dbus_connection_ref (connection);
864 bus_connections_expire_incomplete (connections);
866 /* The listening socket is removed from the main loop,
867 * i.e. does not accept(), while n_incomplete is at its
868 * maximum value; so we shouldn't get here in that case */
869 _dbus_assert (connections->n_incomplete <=
870 bus_context_get_max_incomplete_connections (connections->context));
872 /* If we have the maximum number of incomplete connections,
873 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
874 bus_context_check_all_watches (d->connections->context);
879 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
880 "No memory to set up new connection");
885 d->selinux_id = NULL;
887 if (d->apparmor_confinement)
888 bus_apparmor_confinement_unref (d->apparmor_confinement);
889 d->apparmor_confinement = NULL;
891 if (!dbus_connection_set_watch_functions (connection,
895 _dbus_assert_not_reached ("setting watch functions to NULL failed");
897 if (!dbus_connection_set_timeout_functions (connection,
901 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
903 dbus_connection_set_unix_user_function (connection,
906 dbus_connection_set_windows_user_function (connection,
909 dbus_connection_set_dispatch_status_function (connection,
912 if (d->pending_unix_fds_timeout)
913 _dbus_timeout_unref (d->pending_unix_fds_timeout);
915 d->pending_unix_fds_timeout = NULL;
917 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
919 if (d->link_in_connection_list != NULL)
921 _dbus_assert (d->link_in_connection_list->next == NULL);
922 _dbus_assert (d->link_in_connection_list->prev == NULL);
923 _dbus_list_free_link (d->link_in_connection_list);
924 d->link_in_connection_list = NULL;
927 if (!dbus_connection_set_data (connection,
928 connection_data_slot,
930 _dbus_assert_not_reached ("failed to set connection data to null");
932 /* "d" has now been freed */
939 bus_connections_expire_incomplete (BusConnections *connections)
945 if (connections->incomplete != NULL)
947 long tv_sec, tv_usec;
951 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
952 auth_timeout = bus_context_get_auth_timeout (connections->context);
954 link = _dbus_list_get_first_link (&connections->incomplete);
957 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
958 DBusConnection *connection;
959 BusConnectionData *d;
962 connection = link->data;
964 d = BUS_CONNECTION_DATA (connection);
966 _dbus_assert (d != NULL);
968 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
969 d->connection_tv_usec,
972 if (elapsed >= (double) auth_timeout)
974 /* Unfortunately, we can't identify the connection: it doesn't
975 * have a unique name yet, we don't know its uid/pid yet,
977 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
978 "Connection has not authenticated soon enough, closing it "
979 "(auth_timeout=%dms, elapsed: %.0fms)",
980 auth_timeout, elapsed);
982 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
983 dbus_connection_close (connection);
987 /* We can end the loop, since the connections are in oldest-first order */
988 next_interval = ((double)auth_timeout) - elapsed;
989 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
990 connection, next_interval);
999 bus_expire_timeout_set_interval (connections->expire_timeout,
1004 expire_incomplete_timeout (void *data)
1006 BusConnections *connections = data;
1008 _dbus_verbose ("Running\n");
1010 /* note that this may remove the timeout */
1011 bus_connections_expire_incomplete (connections);
1017 bus_connection_get_unix_groups (DBusConnection *connection,
1018 unsigned long **groups,
1027 if (dbus_connection_get_unix_user (connection, &uid))
1029 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
1031 _dbus_verbose ("Did not get any groups for UID %lu\n",
1037 _dbus_verbose ("Got %d groups for UID %lu\n",
1043 return TRUE; /* successfully got 0 groups */
1047 bus_connection_is_in_unix_group (DBusConnection *connection,
1051 unsigned long *group_ids;
1054 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1059 while (i < n_group_ids)
1061 if (group_ids[i] == gid)
1063 dbus_free (group_ids);
1069 dbus_free (group_ids);
1074 bus_connection_get_loginfo (DBusConnection *connection)
1076 BusConnectionData *d;
1078 d = BUS_CONNECTION_DATA (connection);
1079 _dbus_assert(d != NULL);
1081 if (!bus_connection_is_active (connection))
1083 return d->cached_loginfo_string;
1087 bus_connection_get_policy (DBusConnection *connection)
1089 BusConnectionData *d;
1091 d = BUS_CONNECTION_DATA (connection);
1093 _dbus_assert (d != NULL);
1094 _dbus_assert (d->policy != NULL);
1099 #ifdef DBUS_ENABLE_CYNARA
1100 const char *bus_connection_get_cynara_session_id (DBusConnection *connection)
1102 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
1103 _dbus_assert (d != NULL);
1105 if (d->cynara_session_id == NULL)
1108 if (dbus_connection_get_unix_process_id(connection, &pid))
1109 d->cynara_session_id = cynara_session_from_pid(pid);
1111 /* If client exits as soon as async call, cynara_session_from_pid() returns null.
1112 cynara_session_from_pid checks /proc/pid to verify process */
1113 if (d->cynara_session_id == NULL)
1114 asprintf (&d->cynara_session_id, "/proc/%ld", pid);
1116 return d->cynara_session_id;
1121 foreach_active (BusConnections *connections,
1122 BusConnectionForeachFunction function,
1127 link = _dbus_list_get_first_link (&connections->completed);
1128 while (link != NULL)
1130 DBusConnection *connection = link->data;
1131 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1133 if (!(* function) (connection, data))
1143 foreach_inactive (BusConnections *connections,
1144 BusConnectionForeachFunction function,
1149 link = _dbus_list_get_first_link (&connections->incomplete);
1150 while (link != NULL)
1152 DBusConnection *connection = link->data;
1153 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1155 if (!(* function) (connection, data))
1165 * Calls function on each active connection; if the function returns
1166 * #FALSE, stops iterating. Active connections are authenticated
1167 * and have sent a Hello message.
1169 * @param connections the connections object
1170 * @param function the function
1171 * @param data data to pass to it as a second arg
1174 bus_connections_foreach_active (BusConnections *connections,
1175 BusConnectionForeachFunction function,
1178 foreach_active (connections, function, data);
1182 * Calls function on each connection; if the function returns
1183 * #FALSE, stops iterating.
1185 * @param connections the connections object
1186 * @param function the function
1187 * @param data data to pass to it as a second arg
1190 bus_connections_foreach (BusConnections *connections,
1191 BusConnectionForeachFunction function,
1194 if (!foreach_active (connections, function, data))
1197 foreach_inactive (connections, function, data);
1201 bus_connections_get_context (BusConnections *connections)
1203 return connections->context;
1207 * This is used to avoid covering the same connection twice when
1208 * traversing connections. Note that it assumes we will
1209 * bus_connection_mark_stamp() each connection at least once per
1210 * INT_MAX increments of the global stamp, or wraparound would break
1214 bus_connections_increment_stamp (BusConnections *connections)
1216 connections->stamp += 1;
1219 /* Mark connection with current stamp, return TRUE if it
1220 * didn't already have that stamp
1223 bus_connection_mark_stamp (DBusConnection *connection)
1225 BusConnectionData *d;
1227 d = BUS_CONNECTION_DATA (connection);
1229 _dbus_assert (d != NULL);
1231 if (d->stamp == d->connections->stamp)
1235 d->stamp = d->connections->stamp;
1241 bus_connection_get_context (DBusConnection *connection)
1243 BusConnectionData *d;
1245 d = BUS_CONNECTION_DATA (connection);
1247 _dbus_assert (d != NULL);
1249 return d->connections->context;
1253 bus_connection_get_connections (DBusConnection *connection)
1255 BusConnectionData *d;
1257 d = BUS_CONNECTION_DATA (connection);
1259 _dbus_assert (d != NULL);
1261 return d->connections;
1265 bus_connection_get_registry (DBusConnection *connection)
1267 BusConnectionData *d;
1269 d = BUS_CONNECTION_DATA (connection);
1271 _dbus_assert (d != NULL);
1273 return bus_context_get_registry (d->connections->context);
1277 bus_connection_get_activation (DBusConnection *connection)
1279 BusConnectionData *d;
1281 d = BUS_CONNECTION_DATA (connection);
1283 _dbus_assert (d != NULL);
1285 return bus_context_get_activation (d->connections->context);
1289 bus_connection_get_matchmaker (DBusConnection *connection)
1291 BusConnectionData *d;
1293 d = BUS_CONNECTION_DATA (connection);
1295 _dbus_assert (d != NULL);
1297 return bus_context_get_matchmaker (d->connections->context);
1301 bus_connection_get_selinux_id (DBusConnection *connection)
1303 BusConnectionData *d;
1305 d = BUS_CONNECTION_DATA (connection);
1307 _dbus_assert (d != NULL);
1309 return d->selinux_id;
1312 BusAppArmorConfinement*
1313 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1315 BusConnectionData *d;
1317 d = BUS_CONNECTION_DATA (connection);
1319 _dbus_assert (d != NULL);
1321 bus_apparmor_confinement_ref (d->apparmor_confinement);
1322 return d->apparmor_confinement;
1326 * Checks whether the connection is registered with the message bus.
1328 * @param connection the connection
1329 * @returns #TRUE if we're an active message bus participant
1332 bus_connection_is_active (DBusConnection *connection)
1334 BusConnectionData *d;
1336 d = BUS_CONNECTION_DATA (connection);
1337 _dbus_assert(d != NULL);
1339 return d->name != NULL;
1343 bus_connection_preallocate_oom_error (DBusConnection *connection)
1345 DBusMessage *message;
1346 DBusPreallocatedSend *preallocated;
1347 BusConnectionData *d;
1349 d = BUS_CONNECTION_DATA (connection);
1351 _dbus_assert (d != NULL);
1353 if (d->oom_preallocated != NULL)
1356 preallocated = dbus_connection_preallocate_send (connection);
1357 if (preallocated == NULL)
1360 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1362 if (message == NULL)
1364 dbus_connection_free_preallocated_send (connection, preallocated);
1368 /* d->name may be NULL, but that is OK */
1369 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1370 !dbus_message_set_destination (message, d->name) ||
1371 !dbus_message_set_sender (message,
1374 dbus_connection_free_preallocated_send (connection, preallocated);
1375 dbus_message_unref (message);
1379 /* set reply serial to placeholder value just so space is already allocated
1382 if (!dbus_message_set_reply_serial (message, 14))
1384 dbus_connection_free_preallocated_send (connection, preallocated);
1385 dbus_message_unref (message);
1389 d->oom_message = message;
1390 d->oom_preallocated = preallocated;
1396 bus_connection_send_oom_error (DBusConnection *connection,
1397 DBusMessage *in_reply_to)
1399 BusConnectionData *d;
1401 d = BUS_CONNECTION_DATA (connection);
1403 _dbus_assert (d != NULL);
1404 _dbus_assert (d->oom_message != NULL);
1406 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1407 "dbus-daemon transaction failed (OOM), sending error to "
1408 "sender %s", bus_connection_get_loginfo (connection));
1410 /* should always succeed since we set it to a placeholder earlier */
1411 if (!dbus_message_set_reply_serial (d->oom_message,
1412 dbus_message_get_serial (in_reply_to)))
1413 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1415 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1417 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1418 d->oom_message, NULL);
1420 dbus_message_unref (d->oom_message);
1421 d->oom_message = NULL;
1422 d->oom_preallocated = NULL;
1425 #ifdef DBUS_ENABLE_STATS
1427 update_peak (int *peak,
1436 bus_connection_add_match_rule_link (DBusConnection *connection,
1439 BusConnectionData *d;
1441 d = BUS_CONNECTION_DATA (connection);
1442 _dbus_assert (d != NULL);
1444 _dbus_list_append_link (&d->match_rules, link);
1446 d->n_match_rules += 1;
1448 #ifdef DBUS_ENABLE_STATS
1449 update_peak (&d->peak_match_rules, d->n_match_rules);
1450 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1452 d->connections->total_match_rules += 1;
1453 update_peak (&d->connections->peak_match_rules,
1454 d->connections->total_match_rules);
1459 bus_connection_add_match_rule (DBusConnection *connection,
1464 link = _dbus_list_alloc_link (rule);
1469 bus_connection_add_match_rule_link (connection, link);
1475 bus_connection_remove_match_rule (DBusConnection *connection,
1478 BusConnectionData *d;
1480 d = BUS_CONNECTION_DATA (connection);
1481 _dbus_assert (d != NULL);
1483 _dbus_list_remove_last (&d->match_rules, rule);
1485 d->n_match_rules -= 1;
1486 _dbus_assert (d->n_match_rules >= 0);
1488 #ifdef DBUS_ENABLE_STATS
1489 d->connections->total_match_rules -= 1;
1494 bus_connection_get_n_match_rules (DBusConnection *connection)
1496 BusConnectionData *d;
1498 d = BUS_CONNECTION_DATA (connection);
1499 _dbus_assert (d != NULL);
1501 return d->n_match_rules;
1505 bus_connection_is_service_owner_by_prefix (DBusConnection *connection,
1506 const char *name_prefix)
1508 BusConnectionData *d;
1511 d = BUS_CONNECTION_DATA (connection);
1512 _dbus_assert (d != NULL);
1514 link = _dbus_list_get_first_link (&d->services_owned);
1515 while (link != NULL)
1517 BusService *service = link->data;
1520 _dbus_string_init_const (&str, bus_service_get_name (service));
1522 if (_dbus_string_starts_with_words_c_str (&str, name_prefix, '.'))
1525 link = _dbus_list_get_next_link (&d->services_owned, link);
1532 bus_connection_get_owned_services_list (DBusConnection *connection)
1534 BusConnectionData *d;
1536 d = BUS_CONNECTION_DATA (connection);
1537 return d->services_owned;
1541 bus_connection_add_owned_service_link (DBusConnection *connection,
1544 BusConnectionData *d;
1546 d = BUS_CONNECTION_DATA (connection);
1547 _dbus_assert (d != NULL);
1549 _dbus_list_append_link (&d->services_owned, link);
1551 d->n_services_owned += 1;
1553 #ifdef DBUS_ENABLE_STATS
1554 update_peak (&d->peak_bus_names, d->n_services_owned);
1555 update_peak (&d->connections->peak_bus_names_per_conn,
1556 d->n_services_owned);
1558 d->connections->total_bus_names += 1;
1559 update_peak (&d->connections->peak_bus_names,
1560 d->connections->total_bus_names);
1565 bus_connection_add_owned_service (DBusConnection *connection,
1566 BusService *service)
1570 link = _dbus_list_alloc_link (service);
1575 bus_connection_add_owned_service_link (connection, link);
1581 bus_connection_remove_owned_service (DBusConnection *connection,
1582 BusService *service)
1584 BusConnectionData *d;
1586 d = BUS_CONNECTION_DATA (connection);
1587 _dbus_assert (d != NULL);
1589 _dbus_list_remove_last (&d->services_owned, service);
1591 d->n_services_owned -= 1;
1592 _dbus_assert (d->n_services_owned >= 0);
1594 #ifdef DBUS_ENABLE_STATS
1595 d->connections->total_bus_names -= 1;
1600 bus_connection_get_n_services_owned (DBusConnection *connection)
1602 BusConnectionData *d;
1604 d = BUS_CONNECTION_DATA (connection);
1605 _dbus_assert (d != NULL);
1607 return d->n_services_owned;
1611 bus_connection_complete (DBusConnection *connection,
1612 const DBusString *name,
1615 BusConnectionData *d;
1618 d = BUS_CONNECTION_DATA (connection);
1619 _dbus_assert (d != NULL);
1620 _dbus_assert (d->name == NULL);
1621 _dbus_assert (d->policy == NULL);
1623 _dbus_assert (!bus_connection_is_active (connection));
1625 if (!_dbus_string_copy_data (name, &d->name))
1627 BUS_SET_OOM (error);
1631 _dbus_assert (d->name != NULL);
1633 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1635 d->policy = bus_context_create_client_policy (d->connections->context,
1639 /* we may have a NULL policy on OOM or error getting list of
1640 * groups for a user. In the latter case we don't handle it so
1641 * well currently, as it will just keep failing over and over.
1644 if (d->policy == NULL)
1646 _dbus_verbose ("Failed to create security policy for connection %p\n",
1648 _DBUS_ASSERT_ERROR_IS_SET (error);
1649 dbus_free (d->name);
1654 if (dbus_connection_get_unix_user (connection, &uid))
1656 if (!adjust_connections_for_uid (d->connections,
1661 /* Create and cache a string which holds information about the
1662 * peer process; used for logging purposes.
1664 if (!cache_peer_loginfo_string (d, connection))
1667 /* Now the connection is active, move it between lists */
1668 _dbus_list_unlink (&d->connections->incomplete,
1669 d->link_in_connection_list);
1670 d->connections->n_incomplete -= 1;
1671 _dbus_list_append_link (&d->connections->completed,
1672 d->link_in_connection_list);
1673 d->connections->n_completed += 1;
1675 _dbus_assert (d->connections->n_incomplete >= 0);
1676 _dbus_assert (d->connections->n_completed > 0);
1678 /* If we have dropped below the max. number of incomplete
1679 * connections, start accept()ing again */
1680 bus_context_check_all_watches (d->connections->context);
1682 /* See if we can remove the timeout */
1683 bus_connections_expire_incomplete (d->connections);
1685 _dbus_assert (bus_connection_is_active (connection));
1689 BUS_SET_OOM (error);
1690 dbus_free (d->name);
1693 bus_client_policy_unref (d->policy);
1699 bus_connections_reload_policy (BusConnections *connections,
1702 BusConnectionData *d;
1703 DBusConnection *connection;
1706 _dbus_assert (connections != NULL);
1707 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1709 for (link = _dbus_list_get_first_link (&(connections->completed));
1711 link = _dbus_list_get_next_link (&(connections->completed), link))
1713 connection = link->data;
1714 d = BUS_CONNECTION_DATA (connection);
1715 _dbus_assert (d != NULL);
1716 _dbus_assert (d->policy != NULL);
1718 bus_client_policy_unref (d->policy);
1719 d->policy = bus_context_create_client_policy (connections->context,
1722 if (d->policy == NULL)
1724 _dbus_verbose ("Failed to create security policy for connection %p\n",
1726 _DBUS_ASSERT_ERROR_IS_SET (error);
1735 bus_connection_get_name (DBusConnection *connection)
1737 BusConnectionData *d;
1739 d = BUS_CONNECTION_DATA (connection);
1740 _dbus_assert (d != NULL);
1746 * Check whether completing the passed-in connection would
1747 * exceed limits, and if so set error and return #FALSE
1750 bus_connections_check_limits (BusConnections *connections,
1751 DBusConnection *requesting_completion,
1752 const char **limit_name_out,
1759 limit = bus_context_get_max_completed_connections (connections->context);
1761 if (connections->n_completed >= limit)
1763 if (limit_name_out != NULL)
1764 *limit_name_out = "max_completed_connections";
1766 if (limit_out != NULL)
1769 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1770 "The maximum number of active connections has been reached");
1774 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1776 limit = bus_context_get_max_connections_per_user (connections->context);
1778 if (get_connections_for_uid (connections, uid) >= limit)
1780 if (limit_name_out != NULL)
1781 *limit_name_out = "max_connections_per_user";
1783 if (limit_out != NULL)
1786 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1787 "The maximum number of active connections for UID %lu has been reached",
1797 bus_pending_reply_free (BusPendingReply *pending)
1799 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1801 pending->will_send_reply,
1802 pending->will_get_reply,
1803 pending->reply_serial);
1805 dbus_free (pending);
1809 bus_pending_reply_send_no_reply (BusConnections *connections,
1810 BusTransaction *transaction,
1811 BusPendingReply *pending)
1813 DBusMessage *message;
1814 DBusMessageIter iter;
1820 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1821 if (message == NULL)
1824 dbus_message_set_no_reply (message, TRUE);
1826 if (!dbus_message_set_reply_serial (message,
1827 pending->reply_serial))
1830 if (!dbus_message_set_error_name (message,
1831 DBUS_ERROR_NO_REPLY))
1834 /* If you change these messages, adjust test/dbus-daemon.c to match */
1835 if (pending->will_send_reply == NULL)
1836 errmsg = "Message recipient disconnected from message bus without replying";
1838 errmsg = "Message did not receive a reply (timeout by message bus)";
1840 dbus_message_iter_init_append (message, &iter);
1841 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1844 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1851 dbus_message_unref (message);
1856 bus_pending_reply_expired (BusExpireList *list,
1860 BusPendingReply *pending = link->data;
1861 BusConnections *connections = data;
1862 BusTransaction *transaction;
1864 /* No reply is forthcoming. So nuke it if we can. If not,
1865 * leave it in the list to try expiring again later when we
1869 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1871 pending->will_send_reply,
1872 pending->will_get_reply,
1873 pending->reply_serial);
1875 transaction = bus_transaction_new (connections->context);
1876 if (transaction == NULL)
1879 if (!bus_pending_reply_send_no_reply (connections,
1883 bus_transaction_cancel_and_free (transaction);
1887 bus_expire_list_remove_link (connections->pending_replies, link);
1889 bus_pending_reply_free (pending);
1890 bus_transaction_execute_and_free (transaction);
1896 bus_connection_drop_pending_replies (BusConnections *connections,
1897 DBusConnection *connection)
1899 /* The DBusConnection is almost 100% finalized here, so you can't
1900 * do anything with it except check for pointer equality
1904 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1907 link = bus_expire_list_get_first_link (connections->pending_replies);
1908 while (link != NULL)
1911 BusPendingReply *pending;
1913 next = bus_expire_list_get_next_link (connections->pending_replies,
1915 pending = link->data;
1917 if (pending->will_get_reply == connection)
1919 /* We don't need to track this pending reply anymore */
1921 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1923 pending->will_send_reply,
1924 pending->will_get_reply,
1925 pending->reply_serial);
1927 bus_expire_list_remove_link (connections->pending_replies,
1929 bus_pending_reply_free (pending);
1931 else if (pending->will_send_reply == connection)
1933 /* The reply isn't going to be sent, so set things
1934 * up so it will be expired right away
1936 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1938 pending->will_send_reply,
1939 pending->will_get_reply,
1940 pending->reply_serial);
1942 pending->will_send_reply = NULL;
1943 pending->expire_item.added_tv_sec = 0;
1944 pending->expire_item.added_tv_usec = 0;
1946 bus_expire_list_recheck_immediately (connections->pending_replies);
1956 BusPendingReply *pending;
1957 BusConnections *connections;
1958 } CancelPendingReplyData;
1961 cancel_pending_reply (void *data)
1963 CancelPendingReplyData *d = data;
1965 _dbus_verbose ("d = %p\n", d);
1967 if (!bus_expire_list_remove (d->connections->pending_replies,
1968 &d->pending->expire_item))
1969 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1971 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1975 cancel_pending_reply_data_free (void *data)
1977 CancelPendingReplyData *d = data;
1979 _dbus_verbose ("d = %p\n", d);
1981 /* d->pending should be either freed or still
1982 * in the list of pending replies (owned by someone
1990 * Record that a reply is allowed; return TRUE on success.
1993 bus_connections_expect_reply (BusConnections *connections,
1994 BusTransaction *transaction,
1995 DBusConnection *will_get_reply,
1996 DBusConnection *will_send_reply,
1997 DBusMessage *reply_to_this,
2000 BusPendingReply *pending;
2001 dbus_uint32_t reply_serial;
2003 CancelPendingReplyData *cprd;
2007 _dbus_assert (will_get_reply != NULL);
2008 _dbus_assert (will_send_reply != NULL);
2009 _dbus_assert (reply_to_this != NULL);
2011 if (dbus_message_get_no_reply (reply_to_this))
2012 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
2014 reply_serial = dbus_message_get_serial (reply_to_this);
2016 link = bus_expire_list_get_first_link (connections->pending_replies);
2018 while (link != NULL)
2020 pending = link->data;
2022 if (pending->reply_serial == reply_serial &&
2023 pending->will_get_reply == will_get_reply &&
2024 pending->will_send_reply == will_send_reply)
2026 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
2027 "Message has the same reply serial as a currently-outstanding existing method call");
2031 link = bus_expire_list_get_next_link (connections->pending_replies,
2033 if (pending->will_get_reply == will_get_reply)
2037 limit = bus_context_get_max_replies_per_connection (connections->context);
2041 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
2042 "The maximum number of pending replies for "
2043 "\"%s\" (%s) has been reached "
2044 "(max_replies_per_connection=%d)",
2045 bus_connection_get_name (will_get_reply),
2046 bus_connection_get_loginfo (will_get_reply),
2049 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
2050 "The maximum number of pending replies per connection has been reached");
2054 #ifdef DBUS_ENABLE_STATS
2056 BusConnectionData *d;
2058 d = BUS_CONNECTION_DATA (will_get_reply);
2059 _dbus_assert (d != NULL);
2061 update_peak(&d->peak_pending_replies, count);
2065 pending = dbus_new0 (BusPendingReply, 1);
2066 if (pending == NULL)
2068 BUS_SET_OOM (error);
2072 #ifdef DBUS_ENABLE_VERBOSE_MODE
2073 /* so we can see a not-yet-added pending reply */
2074 pending->expire_item.added_tv_sec = 1;
2075 pending->expire_item.added_tv_usec = 1;
2078 pending->will_get_reply = will_get_reply;
2079 pending->will_send_reply = will_send_reply;
2080 pending->reply_serial = reply_serial;
2082 cprd = dbus_new0 (CancelPendingReplyData, 1);
2085 BUS_SET_OOM (error);
2086 bus_pending_reply_free (pending);
2090 if (!bus_expire_list_add (connections->pending_replies,
2091 &pending->expire_item))
2093 BUS_SET_OOM (error);
2095 bus_pending_reply_free (pending);
2099 if (!bus_transaction_add_cancel_hook (transaction,
2100 cancel_pending_reply,
2102 cancel_pending_reply_data_free))
2104 BUS_SET_OOM (error);
2105 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
2107 bus_pending_reply_free (pending);
2111 cprd->pending = pending;
2112 cprd->connections = connections;
2114 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
2115 &pending->expire_item.added_tv_usec);
2117 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2119 pending->will_send_reply,
2120 pending->will_get_reply,
2121 pending->reply_serial);
2129 BusConnections *connections;
2130 } CheckPendingReplyData;
2133 cancel_check_pending_reply (void *data)
2135 CheckPendingReplyData *d = data;
2137 _dbus_verbose ("d = %p\n",d);
2139 bus_expire_list_add_link (d->connections->pending_replies,
2145 check_pending_reply_data_free (void *data)
2147 CheckPendingReplyData *d = data;
2149 _dbus_verbose ("d = %p\n",d);
2151 if (d->link != NULL)
2153 BusPendingReply *pending = d->link->data;
2155 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2156 &pending->expire_item));
2158 bus_pending_reply_free (pending);
2159 _dbus_list_free_link (d->link);
2166 * Check whether a reply is allowed, remove BusPendingReply
2167 * if so, return TRUE if so.
2170 bus_connections_check_reply (BusConnections *connections,
2171 BusTransaction *transaction,
2172 DBusConnection *sending_reply,
2173 DBusConnection *receiving_reply,
2177 CheckPendingReplyData *cprd;
2179 dbus_uint32_t reply_serial;
2181 _dbus_assert (sending_reply != NULL);
2182 _dbus_assert (receiving_reply != NULL);
2184 reply_serial = dbus_message_get_reply_serial (reply);
2186 link = bus_expire_list_get_first_link (connections->pending_replies);
2187 while (link != NULL)
2189 BusPendingReply *pending = link->data;
2191 if (pending->reply_serial == reply_serial &&
2192 pending->will_get_reply == receiving_reply &&
2193 pending->will_send_reply == sending_reply)
2195 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2199 link = bus_expire_list_get_next_link (connections->pending_replies,
2205 _dbus_verbose ("No pending reply expected\n");
2210 cprd = dbus_new0 (CheckPendingReplyData, 1);
2213 BUS_SET_OOM (error);
2217 if (!bus_transaction_add_cancel_hook (transaction,
2218 cancel_check_pending_reply,
2220 check_pending_reply_data_free))
2222 BUS_SET_OOM (error);
2228 cprd->connections = connections;
2230 bus_expire_list_unlink (connections->pending_replies,
2233 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2241 * Note that this is fairly fragile; in particular, don't try to use
2242 * one transaction across any main loop iterations.
2247 BusTransaction *transaction;
2248 DBusMessage *message;
2249 DBusPreallocatedSend *preallocated;
2254 BusTransactionCancelFunction cancel_function;
2255 DBusFreeFunction free_data_function;
2259 struct BusTransaction
2261 DBusList *connections;
2262 BusContext *context;
2263 DBusList *cancel_hooks;
2267 message_to_send_free (DBusConnection *connection,
2268 MessageToSend *to_send)
2270 if (to_send->message)
2271 dbus_message_unref (to_send->message);
2273 if (to_send->preallocated)
2274 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2276 dbus_free (to_send);
2280 cancel_hook_cancel (void *element,
2283 CancelHook *ch = element;
2285 _dbus_verbose ("Running transaction cancel hook\n");
2287 if (ch->cancel_function)
2288 (* ch->cancel_function) (ch->data);
2292 cancel_hook_free (void *element,
2295 CancelHook *ch = element;
2297 if (ch->free_data_function)
2298 (* ch->free_data_function) (ch->data);
2304 free_cancel_hooks (BusTransaction *transaction)
2306 _dbus_list_foreach (&transaction->cancel_hooks,
2307 cancel_hook_free, NULL);
2309 _dbus_list_clear (&transaction->cancel_hooks);
2313 bus_transaction_new (BusContext *context)
2315 BusTransaction *transaction;
2317 transaction = dbus_new0 (BusTransaction, 1);
2318 if (transaction == NULL)
2321 transaction->context = context;
2327 bus_transaction_get_context (BusTransaction *transaction)
2329 return transaction->context;
2333 * Reserve enough memory to capture the given message if the
2334 * transaction goes through.
2337 bus_transaction_capture (BusTransaction *transaction,
2338 DBusConnection *sender,
2339 DBusConnection *addressed_recipient,
2340 DBusMessage *message)
2342 BusConnections *connections;
2345 DBusList *recipients = NULL;
2346 dbus_bool_t ret = FALSE;
2348 connections = bus_context_get_connections (transaction->context);
2350 /* shortcut: don't compose the message unless someone wants it */
2351 if (connections->monitors == NULL)
2354 mm = connections->monitor_matchmaker;
2355 /* This is non-null if there has ever been a monitor - we don't GC it.
2356 * There's little point, since there is up to 1 per process. */
2357 _dbus_assert (mm != NULL);
2359 if (!bus_matchmaker_get_recipients (mm, connections, sender,
2360 addressed_recipient, message, &recipients))
2363 for (link = _dbus_list_get_first_link (&recipients);
2365 link = _dbus_list_get_next_link (&recipients, link))
2367 DBusConnection *recipient = link->data;
2369 if (!bus_transaction_send (transaction, recipient, message, FALSE))
2376 _dbus_list_clear (&recipients);
2381 bus_transaction_capture_error_reply (BusTransaction *transaction,
2382 DBusConnection *addressed_recipient,
2383 const DBusError *error,
2384 DBusMessage *in_reply_to)
2386 BusConnections *connections;
2388 dbus_bool_t ret = FALSE;
2390 _dbus_assert (error != NULL);
2391 _DBUS_ASSERT_ERROR_IS_SET (error);
2393 connections = bus_context_get_connections (transaction->context);
2395 /* shortcut: don't compose the message unless someone wants it */
2396 if (connections->monitors == NULL)
2399 reply = dbus_message_new_error (in_reply_to,
2406 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2409 ret = bus_transaction_capture (transaction, NULL, addressed_recipient, reply);
2412 dbus_message_unref (reply);
2417 bus_transaction_send_from_driver (BusTransaction *transaction,
2418 DBusConnection *connection,
2419 DBusMessage *message)
2421 DBusError error = DBUS_ERROR_INIT;
2422 BusDeferredMessage *deferred_message;
2424 /* We have to set the sender to the driver, and have
2425 * to check security policy since it was not done in
2428 _dbus_verbose ("Sending %s %s %s from driver\n",
2429 dbus_message_get_interface (message) ?
2430 dbus_message_get_interface (message) : "(no interface)",
2431 dbus_message_get_member (message) ?
2432 dbus_message_get_member (message) : "(no member)",
2433 dbus_message_get_error_name (message) ?
2434 dbus_message_get_error_name (message) : "(no error name)");
2436 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2439 if (bus_connection_is_active (connection))
2441 if (!dbus_message_set_destination (message,
2442 bus_connection_get_name (connection)))
2446 /* bus driver never wants a reply */
2447 dbus_message_set_no_reply (message, TRUE);
2449 /* Capture it for monitors, even if the real recipient's receive policy
2450 * does not allow it to receive this message from us (which would be odd).
2452 if (!bus_transaction_capture (transaction, NULL, connection, message))
2455 /* If security policy doesn't allow the message, we would silently
2456 * eat it; the driver doesn't care about getting a reply. However,
2457 * if we're actively capturing messages, it's nice to log that we
2458 * tried to send it and did not allow ourselves to do so.
2460 switch (bus_context_check_security_policy (bus_transaction_get_context (transaction),
2462 NULL, connection, connection,
2463 message, NULL, &error,
2466 case BUS_RESULT_TRUE:
2468 case BUS_RESULT_FALSE:
2469 if (!bus_transaction_capture_error_reply (transaction, connection,
2472 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2473 "message from dbus-daemon rejected but not enough "
2474 "memory to capture it");
2477 /* This is not fatal to the transaction so silently eat the disallowed
2478 * message (see reasoning above) */
2479 dbus_error_free (&error);
2482 case BUS_RESULT_LATER:
2483 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
2485 return TRUE; /* pretend to have sent it */
2488 return bus_transaction_send (transaction, connection, message, FALSE);
2492 bus_transaction_send (BusTransaction *transaction,
2493 DBusConnection *connection,
2494 DBusMessage *message,
2495 dbus_bool_t deferred_dispatch)
2497 MessageToSend *to_send;
2498 BusConnectionData *d;
2501 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2502 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2503 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2505 dbus_message_get_interface (message) ?
2506 dbus_message_get_interface (message) : "(unset)",
2507 dbus_message_get_member (message) ?
2508 dbus_message_get_member (message) : "(unset)",
2509 dbus_message_get_error_name (message) ?
2510 dbus_message_get_error_name (message) : "(unset)",
2511 dbus_connection_get_is_connected (connection) ?
2512 "" : " (disconnected)");
2514 _dbus_assert (dbus_message_get_sender (message) != NULL);
2516 if (!dbus_connection_get_is_connected (connection))
2517 return TRUE; /* silently ignore disconnected connections */
2519 d = BUS_CONNECTION_DATA (connection);
2520 _dbus_assert (d != NULL);
2522 if (!deferred_dispatch && d->deferred_messages != NULL)
2524 BusDeferredMessage *deferred_message;
2525 dbus_bool_t success;
2526 /* sender and addressed recipient are not required at this point as we only need to send message
2527 * to a single recipient without performing policy check. */
2528 deferred_message = bus_deferred_message_new (message,
2533 if (deferred_message == NULL)
2536 success = bus_deferred_message_queue_at_recipient(deferred_message, transaction,
2538 bus_deferred_message_unref(deferred_message);
2543 to_send = dbus_new (MessageToSend, 1);
2544 if (to_send == NULL)
2549 to_send->preallocated = dbus_connection_preallocate_send (connection);
2550 if (to_send->preallocated == NULL)
2552 dbus_free (to_send);
2556 dbus_message_ref (message);
2557 to_send->message = message;
2558 to_send->transaction = transaction;
2560 _dbus_verbose ("about to prepend message\n");
2562 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2564 message_to_send_free (connection, to_send);
2568 _dbus_verbose ("prepended message\n");
2570 /* See if we already had this connection in the list
2571 * for this transaction. If we have a pending message,
2572 * then we should already be in transaction->connections
2574 link = _dbus_list_get_first_link (&d->transaction_messages);
2575 _dbus_assert (link->data == to_send);
2576 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2577 while (link != NULL)
2579 MessageToSend *m = link->data;
2580 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2582 if (m->transaction == transaction)
2590 if (!_dbus_list_prepend (&transaction->connections, connection))
2592 _dbus_list_remove (&d->transaction_messages, to_send);
2593 message_to_send_free (connection, to_send);
2602 transaction_free (BusTransaction *transaction)
2604 _dbus_assert (transaction->connections == NULL);
2606 free_cancel_hooks (transaction);
2608 dbus_free (transaction);
2612 connection_cancel_transaction (DBusConnection *connection,
2613 BusTransaction *transaction)
2616 BusConnectionData *d;
2618 d = BUS_CONNECTION_DATA (connection);
2619 _dbus_assert (d != NULL);
2621 link = _dbus_list_get_first_link (&d->transaction_messages);
2622 while (link != NULL)
2624 MessageToSend *m = link->data;
2625 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2627 if (m->transaction == transaction)
2629 _dbus_list_remove_link (&d->transaction_messages,
2632 message_to_send_free (connection, m);
2640 bus_transaction_cancel_and_free (BusTransaction *transaction)
2642 DBusConnection *connection;
2644 _dbus_verbose ("TRANSACTION: cancelled\n");
2646 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2647 connection_cancel_transaction (connection, transaction);
2649 _dbus_list_foreach (&transaction->cancel_hooks,
2650 cancel_hook_cancel, NULL);
2652 transaction_free (transaction);
2656 connection_execute_transaction (DBusConnection *connection,
2657 BusTransaction *transaction)
2660 BusConnectionData *d;
2662 d = BUS_CONNECTION_DATA (connection);
2663 _dbus_assert (d != NULL);
2665 /* Send the queue in order (FIFO) */
2666 link = _dbus_list_get_last_link (&d->transaction_messages);
2667 while (link != NULL)
2669 MessageToSend *m = link->data;
2670 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2672 if (m->transaction == transaction)
2674 _dbus_list_remove_link (&d->transaction_messages,
2677 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2679 dbus_connection_send_preallocated (connection,
2684 m->preallocated = NULL; /* so we don't double-free it */
2686 message_to_send_free (connection, m);
2694 bus_transaction_execute_and_free (BusTransaction *transaction)
2696 /* For each connection in transaction->connections
2699 DBusConnection *connection;
2701 _dbus_verbose ("TRANSACTION: executing\n");
2703 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2704 connection_execute_transaction (connection, transaction);
2706 transaction_free (transaction);
2710 bus_connection_remove_transactions (DBusConnection *connection)
2712 MessageToSend *to_send;
2713 BusConnectionData *d;
2715 d = BUS_CONNECTION_DATA (connection);
2716 _dbus_assert (d != NULL);
2718 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2720 /* only has an effect for the first MessageToSend listing this transaction */
2721 _dbus_list_remove (&to_send->transaction->connections,
2724 _dbus_list_remove (&d->transaction_messages, to_send);
2725 message_to_send_free (connection, to_send);
2730 * Converts the DBusError to a message reply
2733 bus_transaction_send_error_reply (BusTransaction *transaction,
2734 DBusConnection *connection,
2735 const DBusError *error,
2736 DBusMessage *in_reply_to)
2740 _dbus_assert (error != NULL);
2741 _DBUS_ASSERT_ERROR_IS_SET (error);
2743 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2744 error->name, error->message);
2746 reply = dbus_message_new_error (in_reply_to,
2752 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2754 dbus_message_unref (reply);
2758 dbus_message_unref (reply);
2764 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2765 BusTransactionCancelFunction cancel_function,
2767 DBusFreeFunction free_data_function)
2771 ch = dbus_new (CancelHook, 1);
2775 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2776 cancel_function, data);
2778 ch->cancel_function = cancel_function;
2780 ch->free_data_function = free_data_function;
2782 /* It's important that the hooks get run in reverse order that they
2785 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2795 bus_connection_dispatch_deferred (DBusConnection *connection)
2797 BusDeferredMessage *message;
2799 _dbus_return_if_fail (connection != NULL);
2801 while ((message = bus_connection_pop_deferred_message(connection)) != NULL)
2803 bus_deferred_message_dispatch(message);
2804 bus_deferred_message_unref(message);
2809 bus_connection_has_deferred_messages (DBusConnection *connection)
2811 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2812 return d->deferred_messages != NULL ? TRUE : FALSE;
2816 bus_connection_queue_deferred_message (DBusConnection *connection,
2817 BusDeferredMessage *message,
2818 dbus_bool_t prepend)
2820 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2821 dbus_bool_t success;
2823 success = _dbus_list_prepend(&d->deferred_messages, message);
2825 success = _dbus_list_append(&d->deferred_messages, message);
2829 bus_deferred_message_ref(message);
2837 bus_connection_replace_deferred_message (DBusConnection *connection,
2838 BusDeferredMessage *oldMessage,
2839 BusDeferredMessage *newMessage)
2842 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2844 link = _dbus_list_find_first(&d->deferred_messages, oldMessage);
2848 if (!_dbus_list_insert_after(&d->deferred_messages, link, newMessage))
2851 bus_deferred_message_ref(newMessage);
2852 _dbus_list_remove_link(&d->deferred_messages, link);
2853 bus_deferred_message_unref(oldMessage);
2857 BusDeferredMessage *
2858 bus_connection_pop_deferred_message (DBusConnection *connection)
2861 BusDeferredMessage *message;
2862 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2864 link =_dbus_list_get_first_link(&d->deferred_messages);
2867 message = link->data;
2868 if (!bus_deferred_message_waits_for_check(message))
2870 _dbus_list_remove_link(&d->deferred_messages, link);
2879 bus_connection_putback_deferred_message (DBusConnection *connection, BusDeferredMessage *message)
2881 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2882 if (_dbus_list_prepend(&d->deferred_messages, message))
2890 bus_connection_clear_deferred_messages (DBusConnection *connection)
2892 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2895 BusDeferredMessage *message;
2897 link =_dbus_list_get_first_link(&d->deferred_messages);
2898 while (link != NULL)
2900 next = _dbus_list_get_next_link (&d->deferred_messages, link);
2901 message = link->data;
2903 bus_deferred_message_unref(message);
2904 _dbus_list_remove_link(&d->deferred_messages, link);
2911 bus_connection_remove_deferred_message (DBusConnection *connection,
2912 BusDeferredMessage *message)
2914 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2915 if (_dbus_list_remove(&d->deferred_messages, message))
2916 bus_deferred_message_unref(message);
2920 bus_connections_get_n_active (BusConnections *connections)
2922 return connections->n_completed;
2926 bus_connections_get_n_incomplete (BusConnections *connections)
2928 return connections->n_incomplete;
2931 #ifdef DBUS_ENABLE_STATS
2933 bus_connections_get_total_match_rules (BusConnections *connections)
2935 return connections->total_match_rules;
2939 bus_connections_get_peak_match_rules (BusConnections *connections)
2941 return connections->peak_match_rules;
2945 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2947 return connections->peak_match_rules_per_conn;
2951 bus_connections_get_total_bus_names (BusConnections *connections)
2953 return connections->total_bus_names;
2957 bus_connections_get_peak_bus_names (BusConnections *connections)
2959 return connections->peak_bus_names;
2963 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2965 return connections->peak_bus_names_per_conn;
2969 bus_connection_get_peak_match_rules (DBusConnection *connection)
2971 BusConnectionData *d;
2973 d = BUS_CONNECTION_DATA (connection);
2974 _dbus_assert(d != NULL);
2976 return d->peak_match_rules;
2980 bus_connection_get_peak_bus_names (DBusConnection *connection)
2982 BusConnectionData *d;
2984 d = BUS_CONNECTION_DATA (connection);
2985 _dbus_assert(d != NULL);
2987 return d->peak_bus_names;
2990 int bus_connection_get_n_pending_replies (DBusConnection *connection)
2992 BusConnectionData *d;
2994 BusPendingReply *pending;
2997 d = BUS_CONNECTION_DATA (connection);
2998 _dbus_assert(d != NULL);
3000 link = bus_expire_list_get_first_link(d->connections->pending_replies);
3001 while (link != NULL)
3003 pending = link->data;
3004 link = bus_expire_list_get_next_link(d->connections->pending_replies,
3006 if (pending->will_get_reply == connection)
3014 bus_connection_get_peak_pending_replies (DBusConnection *connection)
3016 BusConnectionData *d;
3018 d = BUS_CONNECTION_DATA (connection);
3019 _dbus_assert (d != NULL);
3021 return d->peak_pending_replies;
3023 #endif /* DBUS_ENABLE_STATS */
3026 bus_connection_is_monitor (DBusConnection *connection)
3028 BusConnectionData *d;
3030 d = BUS_CONNECTION_DATA (connection);
3031 _dbus_assert(d != NULL);
3033 return d->link_in_monitors != NULL;
3037 bcd_add_monitor_rules (BusConnectionData *d,
3038 DBusConnection *connection,
3041 BusMatchmaker *mm = d->connections->monitor_matchmaker;
3046 mm = bus_matchmaker_new ();
3051 d->connections->monitor_matchmaker = mm;
3054 for (iter = _dbus_list_get_first_link (rules);
3056 iter = _dbus_list_get_next_link (rules, iter))
3058 if (!bus_matchmaker_add_rule (mm, iter->data))
3060 bus_matchmaker_disconnected (mm, connection);
3069 bcd_drop_monitor_rules (BusConnectionData *d,
3070 DBusConnection *connection)
3072 BusMatchmaker *mm = d->connections->monitor_matchmaker;
3075 bus_matchmaker_disconnected (mm, connection);
3079 bus_connection_be_monitor (DBusConnection *connection,
3080 BusTransaction *transaction,
3084 BusConnectionData *d;
3089 d = BUS_CONNECTION_DATA (connection);
3090 _dbus_assert (d != NULL);
3092 link = _dbus_list_alloc_link (connection);
3096 BUS_SET_OOM (error);
3100 if (!bcd_add_monitor_rules (d, connection, rules))
3102 _dbus_list_free_link (link);
3103 BUS_SET_OOM (error);
3107 /* release all its names */
3108 if (!_dbus_list_copy (&d->services_owned, &tmp))
3110 bcd_drop_monitor_rules (d, connection);
3111 _dbus_list_free_link (link);
3112 BUS_SET_OOM (error);
3116 for (iter = _dbus_list_get_first_link (&tmp);
3118 iter = _dbus_list_get_next_link (&tmp, iter))
3120 BusService *service = iter->data;
3122 /* This call is transactional: if there isn't enough memory to
3123 * do everything, then the service gets all its names back when
3124 * the transaction is cancelled due to OOM. */
3125 if (!bus_service_remove_owner (service, connection, transaction, error))
3127 bcd_drop_monitor_rules (d, connection);
3128 _dbus_list_free_link (link);
3129 _dbus_list_clear (&tmp);
3134 /* We have now done everything that can fail, so there is no problem
3135 * with doing the irrevocable stuff. */
3137 _dbus_list_clear (&tmp);
3139 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
3140 "Connection %s (%s) became a monitor.", d->name,
3141 d->cached_loginfo_string);
3143 if (d->n_match_rules > 0)
3147 mm = bus_context_get_matchmaker (d->connections->context);
3148 bus_matchmaker_disconnected (mm, connection);
3151 /* flag it as a monitor */
3152 d->link_in_monitors = link;
3153 _dbus_list_append_link (&d->connections->monitors, link);
3155 /* it isn't allowed to reply, and it is no longer relevant whether it
3156 * receives replies */
3157 bus_connection_drop_pending_replies (d->connections, connection);