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 real connections */
559 while (connections->completed != NULL)
561 DBusConnection *connection;
563 connection = connections->completed->data;
565 dbus_connection_ref (connection);
566 dbus_connection_close (connection);
567 bus_connection_disconnected (connection);
568 dbus_connection_unref (connection);
571 _dbus_assert (connections->n_completed == 0);
573 /* disconnecting all the connections should have emptied the list of
574 * monitors (each link is removed in bus_connection_disconnected) */
575 _dbus_assert (connections->monitors == NULL);
577 bus_expire_list_free (connections->pending_replies);
579 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
580 connections->expire_timeout);
582 _dbus_timeout_unref (connections->expire_timeout);
584 _dbus_hash_table_unref (connections->completed_by_user);
586 if (connections->monitor_matchmaker != NULL)
587 bus_matchmaker_unref (connections->monitor_matchmaker);
589 dbus_free (connections);
591 dbus_connection_free_data_slot (&connection_data_slot);
596 is_context_type_session (BusConnectionData *d)
598 const char *context_type = bus_context_get_type (d->connections->context);
599 return context_type && !strcmp (context_type, "session");
602 /* Used for logging */
604 cache_peer_loginfo_string (BusConnectionData *d,
605 DBusConnection *connection)
607 DBusString loginfo_buf;
610 char *windows_sid = NULL, *security_label = NULL;
611 dbus_bool_t prev_added;
613 if (!_dbus_string_init (&loginfo_buf))
617 if (dbus_connection_get_unix_user (connection, &uid))
619 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
625 if (dbus_connection_get_unix_process_id (connection, &pid))
629 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
632 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
634 /* Ignore errors here; we may not have permissions to read the
636 * Don't even try it for the session daemon, to avoid cluttering logs with security error logs for
637 * accessing the proc file.
639 if (!is_context_type_session(d))
641 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
645 if (!_dbus_string_append (&loginfo_buf, "<not-read>")) /* for session daemon just say that we didn't try */
648 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
654 if (dbus_connection_get_windows_user (connection, &windows_sid))
656 dbus_bool_t did_append;
660 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
664 did_append = _dbus_string_append_printf (&loginfo_buf,
665 "sid=\"%s\"", windows_sid);
666 dbus_free (windows_sid);
674 if (_dbus_connection_get_linux_security_label (connection, &security_label))
676 dbus_bool_t did_append;
680 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
684 did_append = _dbus_string_append_printf (&loginfo_buf,
685 "label=\"%s\"", security_label);
686 dbus_free (security_label);
687 security_label = NULL;
694 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
697 _dbus_string_free (&loginfo_buf);
701 _dbus_string_free (&loginfo_buf);
702 if (security_label != NULL)
703 dbus_free (security_label);
704 if (windows_sid != NULL)
705 dbus_free (windows_sid);
711 check_pending_fds_cb (DBusConnection *connection)
713 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
714 int n_pending_unix_fds_old;
715 int n_pending_unix_fds_new;
717 _dbus_assert(d != NULL);
719 n_pending_unix_fds_old = d->n_pending_unix_fds;
720 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
722 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
723 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
725 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
727 _dbus_timeout_restart (d->pending_unix_fds_timeout,
728 bus_context_get_pending_fd_timeout (d->connections->context));
731 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
733 _dbus_timeout_disable (d->pending_unix_fds_timeout);
737 d->n_pending_unix_fds = n_pending_unix_fds_new;
741 pending_unix_fds_timeout_cb (void *data)
743 DBusConnection *connection = data;
744 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
747 _dbus_assert (d != NULL);
748 limit = bus_context_get_pending_fd_timeout (d->connections->context);
749 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
750 "Connection \"%s\" (%s) has had Unix fds pending for too long, "
751 "closing it (pending_fd_timeout=%d ms)",
752 d->name != NULL ? d->name : "(null)",
753 bus_connection_get_loginfo (connection),
756 dbus_connection_close (connection);
761 bus_connections_setup_connection (BusConnections *connections,
762 DBusConnection *connection)
765 BusConnectionData *d = NULL;
768 d = dbus_new0 (BusConnectionData, 1);
773 d->connections = connections;
774 d->connection = connection;
776 _dbus_get_monotonic_time (&d->connection_tv_sec,
777 &d->connection_tv_usec);
779 _dbus_assert (connection_data_slot >= 0);
781 if (!dbus_connection_set_data (connection,
782 connection_data_slot,
783 d, free_connection_data))
785 /* We have to free d explicitly, because this is the only code
786 * path where it's non-NULL but dbus_connection_set_data() hasn't
787 * taken responsibility for freeing it. */
793 dbus_connection_set_route_peer_messages (connection, TRUE);
795 dbus_error_init (&error);
796 d->selinux_id = bus_selinux_init_connection_id (connection,
798 if (dbus_error_is_set (&error))
800 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
801 "Unable to set up new connection: %s", error.message);
802 dbus_error_free (&error);
806 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
808 if (dbus_error_is_set (&error))
810 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
811 "Unable to set up new connection: %s", error.message);
812 dbus_error_free (&error);
816 if (!dbus_connection_set_watch_functions (connection,
817 add_connection_watch,
818 remove_connection_watch,
819 toggle_connection_watch,
824 if (!dbus_connection_set_timeout_functions (connection,
825 add_connection_timeout,
826 remove_connection_timeout,
831 /* For now we don't need to set a Windows user function because
832 * there are no policies in the config file controlling what
833 * Windows users can connect. The default 'same user that owns the
834 * bus can connect' behavior of DBusConnection is fine on Windows.
836 dbus_connection_set_unix_user_function (connection,
837 allow_unix_user_function,
840 dbus_connection_set_dispatch_status_function (connection,
841 dispatch_status_function,
842 bus_context_get_loop (connections->context),
845 d->link_in_connection_list = _dbus_list_alloc_link (connection);
846 if (d->link_in_connection_list == NULL)
849 /* Setup the connection with the dispatcher */
850 if (!bus_dispatch_add_connection (connection))
853 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
855 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
857 bus_dispatch_remove_connection (connection);
862 /* Setup pending fds timeout (see #80559) */
863 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
864 pending_unix_fds_timeout_cb,
866 if (d->pending_unix_fds_timeout == NULL)
869 _dbus_timeout_disable (d->pending_unix_fds_timeout);
870 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
871 d->pending_unix_fds_timeout))
874 _dbus_connection_set_pending_fds_function (connection,
875 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
878 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
879 connections->n_incomplete += 1;
881 dbus_connection_ref (connection);
883 bus_connections_expire_incomplete (connections);
885 /* The listening socket is removed from the main loop,
886 * i.e. does not accept(), while n_incomplete is at its
887 * maximum value; so we shouldn't get here in that case */
888 _dbus_assert (connections->n_incomplete <=
889 bus_context_get_max_incomplete_connections (connections->context));
891 /* If we have the maximum number of incomplete connections,
892 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
893 bus_context_check_all_watches (d->connections->context);
898 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
899 "No memory to set up new connection");
904 d->selinux_id = NULL;
906 if (d->apparmor_confinement)
907 bus_apparmor_confinement_unref (d->apparmor_confinement);
908 d->apparmor_confinement = NULL;
910 if (!dbus_connection_set_watch_functions (connection,
914 _dbus_assert_not_reached ("setting watch functions to NULL failed");
916 if (!dbus_connection_set_timeout_functions (connection,
920 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
922 dbus_connection_set_unix_user_function (connection,
925 dbus_connection_set_windows_user_function (connection,
928 dbus_connection_set_dispatch_status_function (connection,
931 if (d->pending_unix_fds_timeout)
932 _dbus_timeout_unref (d->pending_unix_fds_timeout);
934 d->pending_unix_fds_timeout = NULL;
936 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
938 if (d->link_in_connection_list != NULL)
940 _dbus_assert (d->link_in_connection_list->next == NULL);
941 _dbus_assert (d->link_in_connection_list->prev == NULL);
942 _dbus_list_free_link (d->link_in_connection_list);
943 d->link_in_connection_list = NULL;
946 if (!dbus_connection_set_data (connection,
947 connection_data_slot,
949 _dbus_assert_not_reached ("failed to set connection data to null");
951 /* "d" has now been freed */
958 bus_connections_expire_incomplete (BusConnections *connections)
964 if (connections->incomplete != NULL)
966 long tv_sec, tv_usec;
970 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
971 auth_timeout = bus_context_get_auth_timeout (connections->context);
973 link = _dbus_list_get_first_link (&connections->incomplete);
976 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
977 DBusConnection *connection;
978 BusConnectionData *d;
981 connection = link->data;
983 d = BUS_CONNECTION_DATA (connection);
985 _dbus_assert (d != NULL);
987 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
988 d->connection_tv_usec,
991 if (elapsed >= (double) auth_timeout)
993 /* Unfortunately, we can't identify the connection: it doesn't
994 * have a unique name yet, we don't know its uid/pid yet,
996 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
997 "Connection has not authenticated soon enough, closing it "
998 "(auth_timeout=%dms, elapsed: %.0fms)",
999 auth_timeout, elapsed);
1001 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
1002 dbus_connection_close (connection);
1006 /* We can end the loop, since the connections are in oldest-first order */
1007 next_interval = ((double)auth_timeout) - elapsed;
1008 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
1009 connection, next_interval);
1018 bus_expire_timeout_set_interval (connections->expire_timeout,
1023 expire_incomplete_timeout (void *data)
1025 BusConnections *connections = data;
1027 _dbus_verbose ("Running\n");
1029 /* note that this may remove the timeout */
1030 bus_connections_expire_incomplete (connections);
1036 bus_connection_get_unix_groups (DBusConnection *connection,
1037 unsigned long **groups,
1046 if (dbus_connection_get_unix_user (connection, &uid))
1048 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
1050 _dbus_verbose ("Did not get any groups for UID %lu\n",
1056 _dbus_verbose ("Got %d groups for UID %lu\n",
1062 return TRUE; /* successfully got 0 groups */
1066 bus_connection_is_in_unix_group (DBusConnection *connection,
1070 unsigned long *group_ids;
1073 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1078 while (i < n_group_ids)
1080 if (group_ids[i] == gid)
1082 dbus_free (group_ids);
1088 dbus_free (group_ids);
1093 bus_connection_get_loginfo (DBusConnection *connection)
1095 BusConnectionData *d;
1097 d = BUS_CONNECTION_DATA (connection);
1098 _dbus_assert(d != NULL);
1100 if (!bus_connection_is_active (connection))
1102 return d->cached_loginfo_string;
1106 bus_connection_get_policy (DBusConnection *connection)
1108 BusConnectionData *d;
1110 d = BUS_CONNECTION_DATA (connection);
1112 _dbus_assert (d != NULL);
1113 _dbus_assert (d->policy != NULL);
1118 #ifdef DBUS_ENABLE_CYNARA
1119 const char *bus_connection_get_cynara_session_id (DBusConnection *connection)
1121 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
1122 _dbus_assert (d != NULL);
1124 if (d->cynara_session_id == NULL)
1127 if (dbus_connection_get_unix_process_id(connection, &pid))
1128 d->cynara_session_id = cynara_session_from_pid(pid);
1130 /* If client exits as soon as async call, cynara_session_from_pid() returns null.
1131 cynara_session_from_pid checks /proc/pid to verify process */
1132 if (d->cynara_session_id == NULL)
1133 asprintf (&d->cynara_session_id, "/proc/%ld", pid);
1135 return d->cynara_session_id;
1140 foreach_active (BusConnections *connections,
1141 BusConnectionForeachFunction function,
1146 link = _dbus_list_get_first_link (&connections->completed);
1147 while (link != NULL)
1149 DBusConnection *connection = link->data;
1150 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1152 if (!(* function) (connection, data))
1162 foreach_inactive (BusConnections *connections,
1163 BusConnectionForeachFunction function,
1168 link = _dbus_list_get_first_link (&connections->incomplete);
1169 while (link != NULL)
1171 DBusConnection *connection = link->data;
1172 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1174 if (!(* function) (connection, data))
1184 * Calls function on each active connection; if the function returns
1185 * #FALSE, stops iterating. Active connections are authenticated
1186 * and have sent a Hello message.
1188 * @param connections the connections object
1189 * @param function the function
1190 * @param data data to pass to it as a second arg
1193 bus_connections_foreach_active (BusConnections *connections,
1194 BusConnectionForeachFunction function,
1197 foreach_active (connections, function, data);
1201 * Calls function on each connection; if the function returns
1202 * #FALSE, stops iterating.
1204 * @param connections the connections object
1205 * @param function the function
1206 * @param data data to pass to it as a second arg
1209 bus_connections_foreach (BusConnections *connections,
1210 BusConnectionForeachFunction function,
1213 if (!foreach_active (connections, function, data))
1216 foreach_inactive (connections, function, data);
1220 bus_connections_get_context (BusConnections *connections)
1222 return connections->context;
1226 * This is used to avoid covering the same connection twice when
1227 * traversing connections. Note that it assumes we will
1228 * bus_connection_mark_stamp() each connection at least once per
1229 * INT_MAX increments of the global stamp, or wraparound would break
1233 bus_connections_increment_stamp (BusConnections *connections)
1235 connections->stamp += 1;
1238 /* Mark connection with current stamp, return TRUE if it
1239 * didn't already have that stamp
1242 bus_connection_mark_stamp (DBusConnection *connection)
1244 BusConnectionData *d;
1246 d = BUS_CONNECTION_DATA (connection);
1248 _dbus_assert (d != NULL);
1250 if (d->stamp == d->connections->stamp)
1254 d->stamp = d->connections->stamp;
1260 bus_connection_get_context (DBusConnection *connection)
1262 BusConnectionData *d;
1264 d = BUS_CONNECTION_DATA (connection);
1266 _dbus_assert (d != NULL);
1268 return d->connections->context;
1272 bus_connection_get_connections (DBusConnection *connection)
1274 BusConnectionData *d;
1276 d = BUS_CONNECTION_DATA (connection);
1278 _dbus_assert (d != NULL);
1280 return d->connections;
1284 bus_connection_get_registry (DBusConnection *connection)
1286 BusConnectionData *d;
1288 d = BUS_CONNECTION_DATA (connection);
1290 _dbus_assert (d != NULL);
1292 return bus_context_get_registry (d->connections->context);
1296 bus_connection_get_activation (DBusConnection *connection)
1298 BusConnectionData *d;
1300 d = BUS_CONNECTION_DATA (connection);
1302 _dbus_assert (d != NULL);
1304 return bus_context_get_activation (d->connections->context);
1308 bus_connection_get_matchmaker (DBusConnection *connection)
1310 BusConnectionData *d;
1312 d = BUS_CONNECTION_DATA (connection);
1314 _dbus_assert (d != NULL);
1316 return bus_context_get_matchmaker (d->connections->context);
1320 bus_connection_get_selinux_id (DBusConnection *connection)
1322 BusConnectionData *d;
1324 d = BUS_CONNECTION_DATA (connection);
1326 _dbus_assert (d != NULL);
1328 return d->selinux_id;
1331 BusAppArmorConfinement*
1332 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1334 BusConnectionData *d;
1336 d = BUS_CONNECTION_DATA (connection);
1338 _dbus_assert (d != NULL);
1340 bus_apparmor_confinement_ref (d->apparmor_confinement);
1341 return d->apparmor_confinement;
1345 * Checks whether the connection is registered with the message bus.
1347 * @param connection the connection
1348 * @returns #TRUE if we're an active message bus participant
1351 bus_connection_is_active (DBusConnection *connection)
1353 BusConnectionData *d;
1355 d = BUS_CONNECTION_DATA (connection);
1356 _dbus_assert(d != NULL);
1358 return d->name != NULL;
1362 bus_connection_preallocate_oom_error (DBusConnection *connection)
1364 DBusMessage *message;
1365 DBusPreallocatedSend *preallocated;
1366 BusConnectionData *d;
1368 d = BUS_CONNECTION_DATA (connection);
1370 _dbus_assert (d != NULL);
1372 if (d->oom_preallocated != NULL)
1375 preallocated = dbus_connection_preallocate_send (connection);
1376 if (preallocated == NULL)
1379 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1381 if (message == NULL)
1383 dbus_connection_free_preallocated_send (connection, preallocated);
1387 /* d->name may be NULL, but that is OK */
1388 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1389 !dbus_message_set_destination (message, d->name) ||
1390 !dbus_message_set_sender (message,
1393 dbus_connection_free_preallocated_send (connection, preallocated);
1394 dbus_message_unref (message);
1398 /* set reply serial to placeholder value just so space is already allocated
1401 if (!dbus_message_set_reply_serial (message, 14))
1403 dbus_connection_free_preallocated_send (connection, preallocated);
1404 dbus_message_unref (message);
1408 d->oom_message = message;
1409 d->oom_preallocated = preallocated;
1415 bus_connection_send_oom_error (DBusConnection *connection,
1416 DBusMessage *in_reply_to)
1418 BusConnectionData *d;
1420 d = BUS_CONNECTION_DATA (connection);
1422 _dbus_assert (d != NULL);
1423 _dbus_assert (d->oom_message != NULL);
1425 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1426 "dbus-daemon transaction failed (OOM), sending error to "
1427 "sender %s", bus_connection_get_loginfo (connection));
1429 /* should always succeed since we set it to a placeholder earlier */
1430 if (!dbus_message_set_reply_serial (d->oom_message,
1431 dbus_message_get_serial (in_reply_to)))
1432 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1434 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1436 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1437 d->oom_message, NULL);
1439 dbus_message_unref (d->oom_message);
1440 d->oom_message = NULL;
1441 d->oom_preallocated = NULL;
1444 #ifdef DBUS_ENABLE_STATS
1446 update_peak (int *peak,
1455 bus_connection_add_match_rule_link (DBusConnection *connection,
1458 BusConnectionData *d;
1460 d = BUS_CONNECTION_DATA (connection);
1461 _dbus_assert (d != NULL);
1463 _dbus_list_append_link (&d->match_rules, link);
1465 d->n_match_rules += 1;
1467 #ifdef DBUS_ENABLE_STATS
1468 update_peak (&d->peak_match_rules, d->n_match_rules);
1469 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1471 d->connections->total_match_rules += 1;
1472 update_peak (&d->connections->peak_match_rules,
1473 d->connections->total_match_rules);
1478 bus_connection_add_match_rule (DBusConnection *connection,
1483 link = _dbus_list_alloc_link (rule);
1488 bus_connection_add_match_rule_link (connection, link);
1494 bus_connection_remove_match_rule (DBusConnection *connection,
1497 BusConnectionData *d;
1499 d = BUS_CONNECTION_DATA (connection);
1500 _dbus_assert (d != NULL);
1502 _dbus_list_remove_last (&d->match_rules, rule);
1504 d->n_match_rules -= 1;
1505 _dbus_assert (d->n_match_rules >= 0);
1507 #ifdef DBUS_ENABLE_STATS
1508 d->connections->total_match_rules -= 1;
1513 bus_connection_get_n_match_rules (DBusConnection *connection)
1515 BusConnectionData *d;
1517 d = BUS_CONNECTION_DATA (connection);
1518 _dbus_assert (d != NULL);
1520 return d->n_match_rules;
1524 bus_connection_is_service_owner_by_prefix (DBusConnection *connection,
1525 const char *name_prefix)
1527 BusConnectionData *d;
1530 d = BUS_CONNECTION_DATA (connection);
1531 _dbus_assert (d != NULL);
1533 link = _dbus_list_get_first_link (&d->services_owned);
1534 while (link != NULL)
1536 BusService *service = link->data;
1539 _dbus_string_init_const (&str, bus_service_get_name (service));
1541 if (_dbus_string_starts_with_words_c_str (&str, name_prefix, '.'))
1544 link = _dbus_list_get_next_link (&d->services_owned, link);
1551 bus_connection_get_owned_services_list (DBusConnection *connection)
1553 BusConnectionData *d;
1555 d = BUS_CONNECTION_DATA (connection);
1557 _dbus_assert (d != NULL);
1559 return d->services_owned;
1563 bus_connection_add_owned_service_link (DBusConnection *connection,
1566 BusConnectionData *d;
1568 d = BUS_CONNECTION_DATA (connection);
1569 _dbus_assert (d != NULL);
1571 _dbus_list_append_link (&d->services_owned, link);
1573 d->n_services_owned += 1;
1575 #ifdef DBUS_ENABLE_STATS
1576 update_peak (&d->peak_bus_names, d->n_services_owned);
1577 update_peak (&d->connections->peak_bus_names_per_conn,
1578 d->n_services_owned);
1580 d->connections->total_bus_names += 1;
1581 update_peak (&d->connections->peak_bus_names,
1582 d->connections->total_bus_names);
1587 bus_connection_add_owned_service (DBusConnection *connection,
1588 BusService *service)
1592 link = _dbus_list_alloc_link (service);
1597 bus_connection_add_owned_service_link (connection, link);
1603 bus_connection_remove_owned_service (DBusConnection *connection,
1604 BusService *service)
1606 BusConnectionData *d;
1608 d = BUS_CONNECTION_DATA (connection);
1609 _dbus_assert (d != NULL);
1611 _dbus_list_remove_last (&d->services_owned, service);
1613 d->n_services_owned -= 1;
1614 _dbus_assert (d->n_services_owned >= 0);
1616 #ifdef DBUS_ENABLE_STATS
1617 d->connections->total_bus_names -= 1;
1622 bus_connection_get_n_services_owned (DBusConnection *connection)
1624 BusConnectionData *d;
1626 d = BUS_CONNECTION_DATA (connection);
1627 _dbus_assert (d != NULL);
1629 return d->n_services_owned;
1633 bus_connection_complete (DBusConnection *connection,
1634 const DBusString *name,
1637 BusConnectionData *d;
1640 d = BUS_CONNECTION_DATA (connection);
1641 _dbus_assert (d != NULL);
1642 _dbus_assert (d->name == NULL);
1643 _dbus_assert (d->policy == NULL);
1645 _dbus_assert (!bus_connection_is_active (connection));
1647 if (!_dbus_string_copy_data (name, &d->name))
1649 BUS_SET_OOM (error);
1653 _dbus_assert (d->name != NULL);
1655 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1657 d->policy = bus_context_create_client_policy (d->connections->context,
1661 /* we may have a NULL policy on OOM or error getting list of
1662 * groups for a user. In the latter case we don't handle it so
1663 * well currently, as it will just keep failing over and over.
1666 if (d->policy == NULL)
1668 _dbus_verbose ("Failed to create security policy for connection %p\n",
1670 _DBUS_ASSERT_ERROR_IS_SET (error);
1671 dbus_free (d->name);
1676 if (dbus_connection_get_unix_user (connection, &uid))
1678 if (!adjust_connections_for_uid (d->connections,
1683 /* Create and cache a string which holds information about the
1684 * peer process; used for logging purposes.
1686 if (!cache_peer_loginfo_string (d, connection))
1689 /* Now the connection is active, move it between lists */
1690 _dbus_list_unlink (&d->connections->incomplete,
1691 d->link_in_connection_list);
1692 d->connections->n_incomplete -= 1;
1693 _dbus_list_append_link (&d->connections->completed,
1694 d->link_in_connection_list);
1695 d->connections->n_completed += 1;
1697 _dbus_assert (d->connections->n_incomplete >= 0);
1698 _dbus_assert (d->connections->n_completed > 0);
1700 /* If we have dropped below the max. number of incomplete
1701 * connections, start accept()ing again */
1702 bus_context_check_all_watches (d->connections->context);
1704 /* See if we can remove the timeout */
1705 bus_connections_expire_incomplete (d->connections);
1707 _dbus_assert (bus_connection_is_active (connection));
1711 BUS_SET_OOM (error);
1712 dbus_free (d->name);
1715 bus_client_policy_unref (d->policy);
1721 bus_connections_reload_policy (BusConnections *connections,
1724 BusConnectionData *d;
1725 DBusConnection *connection;
1728 _dbus_assert (connections != NULL);
1729 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1731 for (link = _dbus_list_get_first_link (&(connections->completed));
1733 link = _dbus_list_get_next_link (&(connections->completed), link))
1735 connection = link->data;
1736 d = BUS_CONNECTION_DATA (connection);
1737 _dbus_assert (d != NULL);
1738 _dbus_assert (d->policy != NULL);
1740 bus_client_policy_unref (d->policy);
1741 d->policy = bus_context_create_client_policy (connections->context,
1744 if (d->policy == NULL)
1746 _dbus_verbose ("Failed to create security policy for connection %p\n",
1748 _DBUS_ASSERT_ERROR_IS_SET (error);
1757 bus_connection_get_name (DBusConnection *connection)
1759 BusConnectionData *d;
1761 d = BUS_CONNECTION_DATA (connection);
1762 _dbus_assert (d != NULL);
1768 * Check whether completing the passed-in connection would
1769 * exceed limits, and if so set error and return #FALSE
1772 bus_connections_check_limits (BusConnections *connections,
1773 DBusConnection *requesting_completion,
1774 const char **limit_name_out,
1781 limit = bus_context_get_max_completed_connections (connections->context);
1783 if (connections->n_completed >= limit)
1785 if (limit_name_out != NULL)
1786 *limit_name_out = "max_completed_connections";
1788 if (limit_out != NULL)
1791 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1792 "The maximum number of active connections has been reached");
1796 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1798 limit = bus_context_get_max_connections_per_user (connections->context);
1800 if (get_connections_for_uid (connections, uid) >= limit)
1802 if (limit_name_out != NULL)
1803 *limit_name_out = "max_connections_per_user";
1805 if (limit_out != NULL)
1808 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1809 "The maximum number of active connections for UID %lu has been reached",
1819 bus_pending_reply_free (BusPendingReply *pending)
1821 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1823 pending->will_send_reply,
1824 pending->will_get_reply,
1825 pending->reply_serial);
1827 dbus_free (pending);
1831 bus_pending_reply_send_no_reply (BusConnections *connections,
1832 BusTransaction *transaction,
1833 BusPendingReply *pending)
1835 DBusMessage *message;
1836 DBusMessageIter iter;
1842 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1843 if (message == NULL)
1846 dbus_message_set_no_reply (message, TRUE);
1848 if (!dbus_message_set_reply_serial (message,
1849 pending->reply_serial))
1852 if (!dbus_message_set_error_name (message,
1853 DBUS_ERROR_NO_REPLY))
1856 /* If you change these messages, adjust test/dbus-daemon.c to match */
1857 if (pending->will_send_reply == NULL)
1858 errmsg = "Message recipient disconnected from message bus without replying";
1860 errmsg = "Message did not receive a reply (timeout by message bus)";
1862 dbus_message_iter_init_append (message, &iter);
1863 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1866 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1873 dbus_message_unref (message);
1878 bus_pending_reply_expired (BusExpireList *list,
1882 BusPendingReply *pending = link->data;
1883 BusConnections *connections = data;
1884 BusTransaction *transaction;
1886 /* No reply is forthcoming. So nuke it if we can. If not,
1887 * leave it in the list to try expiring again later when we
1891 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1893 pending->will_send_reply,
1894 pending->will_get_reply,
1895 pending->reply_serial);
1897 transaction = bus_transaction_new (connections->context);
1898 if (transaction == NULL)
1901 if (!bus_pending_reply_send_no_reply (connections,
1905 bus_transaction_cancel_and_free (transaction);
1909 bus_expire_list_remove_link (connections->pending_replies, link);
1911 bus_pending_reply_free (pending);
1912 bus_transaction_execute_and_free (transaction);
1918 bus_connection_drop_pending_replies (BusConnections *connections,
1919 DBusConnection *connection)
1921 /* The DBusConnection is almost 100% finalized here, so you can't
1922 * do anything with it except check for pointer equality
1926 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1929 link = bus_expire_list_get_first_link (connections->pending_replies);
1930 while (link != NULL)
1933 BusPendingReply *pending;
1935 next = bus_expire_list_get_next_link (connections->pending_replies,
1937 pending = link->data;
1939 if (pending->will_get_reply == connection)
1941 /* We don't need to track this pending reply anymore */
1943 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1945 pending->will_send_reply,
1946 pending->will_get_reply,
1947 pending->reply_serial);
1949 bus_expire_list_remove_link (connections->pending_replies,
1951 bus_pending_reply_free (pending);
1953 else if (pending->will_send_reply == connection)
1955 /* The reply isn't going to be sent, so set things
1956 * up so it will be expired right away
1958 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1960 pending->will_send_reply,
1961 pending->will_get_reply,
1962 pending->reply_serial);
1964 pending->will_send_reply = NULL;
1965 pending->expire_item.added_tv_sec = 0;
1966 pending->expire_item.added_tv_usec = 0;
1968 bus_expire_list_recheck_immediately (connections->pending_replies);
1978 BusPendingReply *pending;
1979 BusConnections *connections;
1980 } CancelPendingReplyData;
1983 cancel_pending_reply (void *data)
1985 CancelPendingReplyData *d = data;
1987 _dbus_verbose ("d = %p\n", d);
1989 if (!bus_expire_list_remove (d->connections->pending_replies,
1990 &d->pending->expire_item))
1991 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1993 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1997 cancel_pending_reply_data_free (void *data)
1999 CancelPendingReplyData *d = data;
2001 _dbus_verbose ("d = %p\n", d);
2003 /* d->pending should be either freed or still
2004 * in the list of pending replies (owned by someone
2012 * Record that a reply is allowed; return TRUE on success.
2015 bus_connections_expect_reply (BusConnections *connections,
2016 BusTransaction *transaction,
2017 DBusConnection *will_get_reply,
2018 DBusConnection *will_send_reply,
2019 DBusMessage *reply_to_this,
2022 BusPendingReply *pending;
2023 dbus_uint32_t reply_serial;
2025 CancelPendingReplyData *cprd;
2029 _dbus_assert (will_get_reply != NULL);
2030 _dbus_assert (will_send_reply != NULL);
2031 _dbus_assert (reply_to_this != NULL);
2033 if (dbus_message_get_no_reply (reply_to_this))
2034 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
2036 reply_serial = dbus_message_get_serial (reply_to_this);
2038 link = bus_expire_list_get_first_link (connections->pending_replies);
2040 while (link != NULL)
2042 pending = link->data;
2044 if (pending->reply_serial == reply_serial &&
2045 pending->will_get_reply == will_get_reply &&
2046 pending->will_send_reply == will_send_reply)
2048 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
2049 "Message has the same reply serial as a currently-outstanding existing method call");
2053 link = bus_expire_list_get_next_link (connections->pending_replies,
2055 if (pending->will_get_reply == will_get_reply)
2059 limit = bus_context_get_max_replies_per_connection (connections->context);
2063 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
2064 "The maximum number of pending replies for "
2065 "\"%s\" (%s) has been reached "
2066 "(max_replies_per_connection=%d)",
2067 bus_connection_get_name (will_get_reply),
2068 bus_connection_get_loginfo (will_get_reply),
2071 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
2072 "The maximum number of pending replies per connection has been reached");
2076 #ifdef DBUS_ENABLE_STATS
2078 BusConnectionData *d;
2080 d = BUS_CONNECTION_DATA (will_get_reply);
2081 _dbus_assert (d != NULL);
2083 update_peak(&d->peak_pending_replies, count);
2087 pending = dbus_new0 (BusPendingReply, 1);
2088 if (pending == NULL)
2090 BUS_SET_OOM (error);
2094 #ifdef DBUS_ENABLE_VERBOSE_MODE
2095 /* so we can see a not-yet-added pending reply */
2096 pending->expire_item.added_tv_sec = 1;
2097 pending->expire_item.added_tv_usec = 1;
2100 pending->will_get_reply = will_get_reply;
2101 pending->will_send_reply = will_send_reply;
2102 pending->reply_serial = reply_serial;
2104 cprd = dbus_new0 (CancelPendingReplyData, 1);
2107 BUS_SET_OOM (error);
2108 bus_pending_reply_free (pending);
2112 if (!bus_expire_list_add (connections->pending_replies,
2113 &pending->expire_item))
2115 BUS_SET_OOM (error);
2117 bus_pending_reply_free (pending);
2121 if (!bus_transaction_add_cancel_hook (transaction,
2122 cancel_pending_reply,
2124 cancel_pending_reply_data_free))
2126 BUS_SET_OOM (error);
2127 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
2129 bus_pending_reply_free (pending);
2133 cprd->pending = pending;
2134 cprd->connections = connections;
2136 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
2137 &pending->expire_item.added_tv_usec);
2139 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2141 pending->will_send_reply,
2142 pending->will_get_reply,
2143 pending->reply_serial);
2151 BusConnections *connections;
2152 } CheckPendingReplyData;
2155 cancel_check_pending_reply (void *data)
2157 CheckPendingReplyData *d = data;
2159 _dbus_verbose ("d = %p\n",d);
2161 bus_expire_list_add_link (d->connections->pending_replies,
2167 check_pending_reply_data_free (void *data)
2169 CheckPendingReplyData *d = data;
2171 _dbus_verbose ("d = %p\n",d);
2173 if (d->link != NULL)
2175 BusPendingReply *pending = d->link->data;
2177 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2178 &pending->expire_item));
2180 bus_pending_reply_free (pending);
2181 _dbus_list_free_link (d->link);
2188 * Check whether a reply is allowed, remove BusPendingReply
2189 * if so, return TRUE if so.
2192 bus_connections_check_reply (BusConnections *connections,
2193 BusTransaction *transaction,
2194 DBusConnection *sending_reply,
2195 DBusConnection *receiving_reply,
2199 CheckPendingReplyData *cprd;
2201 dbus_uint32_t reply_serial;
2203 _dbus_assert (sending_reply != NULL);
2204 _dbus_assert (receiving_reply != NULL);
2206 reply_serial = dbus_message_get_reply_serial (reply);
2208 link = bus_expire_list_get_first_link (connections->pending_replies);
2209 while (link != NULL)
2211 BusPendingReply *pending = link->data;
2213 if (pending->reply_serial == reply_serial &&
2214 pending->will_get_reply == receiving_reply &&
2215 pending->will_send_reply == sending_reply)
2217 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2221 link = bus_expire_list_get_next_link (connections->pending_replies,
2227 _dbus_verbose ("No pending reply expected\n");
2232 cprd = dbus_new0 (CheckPendingReplyData, 1);
2235 BUS_SET_OOM (error);
2239 if (!bus_transaction_add_cancel_hook (transaction,
2240 cancel_check_pending_reply,
2242 check_pending_reply_data_free))
2244 BUS_SET_OOM (error);
2250 cprd->connections = connections;
2252 bus_expire_list_unlink (connections->pending_replies,
2255 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2263 * Note that this is fairly fragile; in particular, don't try to use
2264 * one transaction across any main loop iterations.
2269 BusTransaction *transaction;
2270 DBusMessage *message;
2271 DBusPreallocatedSend *preallocated;
2276 BusTransactionCancelFunction cancel_function;
2277 DBusFreeFunction free_data_function;
2281 struct BusTransaction
2283 DBusList *connections;
2284 BusContext *context;
2285 DBusList *cancel_hooks;
2289 message_to_send_free (DBusConnection *connection,
2290 MessageToSend *to_send)
2292 if (to_send->message)
2293 dbus_message_unref (to_send->message);
2295 if (to_send->preallocated)
2296 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2298 dbus_free (to_send);
2302 cancel_hook_cancel (void *element,
2305 CancelHook *ch = element;
2307 _dbus_verbose ("Running transaction cancel hook\n");
2309 if (ch->cancel_function)
2310 (* ch->cancel_function) (ch->data);
2314 cancel_hook_free (void *element,
2317 CancelHook *ch = element;
2319 if (ch->free_data_function)
2320 (* ch->free_data_function) (ch->data);
2326 free_cancel_hooks (BusTransaction *transaction)
2328 _dbus_list_foreach (&transaction->cancel_hooks,
2329 cancel_hook_free, NULL);
2331 _dbus_list_clear (&transaction->cancel_hooks);
2335 bus_transaction_new (BusContext *context)
2337 BusTransaction *transaction;
2339 transaction = dbus_new0 (BusTransaction, 1);
2340 if (transaction == NULL)
2343 transaction->context = context;
2349 bus_transaction_get_context (BusTransaction *transaction)
2351 return transaction->context;
2355 * Reserve enough memory to capture the given message if the
2356 * transaction goes through.
2359 bus_transaction_capture (BusTransaction *transaction,
2360 DBusConnection *sender,
2361 DBusConnection *addressed_recipient,
2362 DBusMessage *message)
2364 BusConnections *connections;
2367 DBusList *recipients = NULL;
2368 dbus_bool_t ret = FALSE;
2370 connections = bus_context_get_connections (transaction->context);
2372 /* shortcut: don't compose the message unless someone wants it */
2373 if (connections->monitors == NULL)
2376 mm = connections->monitor_matchmaker;
2377 /* This is non-null if there has ever been a monitor - we don't GC it.
2378 * There's little point, since there is up to 1 per process. */
2379 _dbus_assert (mm != NULL);
2381 if (!bus_matchmaker_get_recipients (mm, connections, sender,
2382 addressed_recipient, message, &recipients))
2385 for (link = _dbus_list_get_first_link (&recipients);
2387 link = _dbus_list_get_next_link (&recipients, link))
2389 DBusConnection *recipient = link->data;
2391 if (!bus_transaction_send (transaction, recipient, message, FALSE))
2398 _dbus_list_clear (&recipients);
2403 bus_transaction_capture_error_reply (BusTransaction *transaction,
2404 DBusConnection *addressed_recipient,
2405 const DBusError *error,
2406 DBusMessage *in_reply_to)
2408 BusConnections *connections;
2410 dbus_bool_t ret = FALSE;
2412 _dbus_assert (error != NULL);
2413 _DBUS_ASSERT_ERROR_IS_SET (error);
2415 connections = bus_context_get_connections (transaction->context);
2417 /* shortcut: don't compose the message unless someone wants it */
2418 if (connections->monitors == NULL)
2421 reply = dbus_message_new_error (in_reply_to,
2428 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2431 ret = bus_transaction_capture (transaction, NULL, addressed_recipient, reply);
2434 dbus_message_unref (reply);
2439 bus_transaction_send_from_driver (BusTransaction *transaction,
2440 DBusConnection *connection,
2441 DBusMessage *message)
2443 DBusError error = DBUS_ERROR_INIT;
2444 BusDeferredMessage *deferred_message = NULL;
2446 /* We have to set the sender to the driver, and have
2447 * to check security policy since it was not done in
2450 _dbus_verbose ("Sending %s %s %s from driver\n",
2451 dbus_message_get_interface (message) ?
2452 dbus_message_get_interface (message) : "(no interface)",
2453 dbus_message_get_member (message) ?
2454 dbus_message_get_member (message) : "(no member)",
2455 dbus_message_get_error_name (message) ?
2456 dbus_message_get_error_name (message) : "(no error name)");
2458 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2461 /* Make sure the message has a non-zero serial number, otherwise
2462 * bus_transaction_capture_error_reply() will not be able to mock up
2463 * a corresponding reply for it. Normally this would be delayed until
2464 * the first time we actually send the message out from a
2465 * connection, when the transaction is committed, but that's too late
2468 if (dbus_message_get_serial (message) == 0)
2470 dbus_uint32_t next_serial;
2472 next_serial = _dbus_connection_get_next_client_serial (connection);
2473 dbus_message_set_serial (message, next_serial);
2476 if (bus_connection_is_active (connection))
2478 if (!dbus_message_set_destination (message,
2479 bus_connection_get_name (connection)))
2483 /* bus driver never wants a reply */
2484 dbus_message_set_no_reply (message, TRUE);
2486 /* Capture it for monitors, even if the real recipient's receive policy
2487 * does not allow it to receive this message from us (which would be odd).
2489 if (!bus_transaction_capture (transaction, NULL, connection, message))
2492 /* If security policy doesn't allow the message, we would silently
2493 * eat it; the driver doesn't care about getting a reply. However,
2494 * if we're actively capturing messages, it's nice to log that we
2495 * tried to send it and did not allow ourselves to do so.
2497 switch (bus_context_check_security_policy (bus_transaction_get_context (transaction),
2499 NULL, connection, connection,
2500 message, NULL, &error,
2503 case BUS_RESULT_TRUE:
2505 case BUS_RESULT_FALSE:
2506 if (!bus_transaction_capture_error_reply (transaction, connection,
2509 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2510 "message from dbus-daemon rejected but not enough "
2511 "memory to capture it");
2514 /* This is not fatal to the transaction so silently eat the disallowed
2515 * message (see reasoning above) */
2516 dbus_error_free (&error);
2519 case BUS_RESULT_LATER:
2520 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
2522 return TRUE; /* pretend to have sent it */
2525 return bus_transaction_send (transaction, connection, message, FALSE);
2529 bus_transaction_send (BusTransaction *transaction,
2530 DBusConnection *connection,
2531 DBusMessage *message,
2532 dbus_bool_t deferred_dispatch)
2534 MessageToSend *to_send;
2535 BusConnectionData *d;
2538 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2539 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2540 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2542 dbus_message_get_interface (message) ?
2543 dbus_message_get_interface (message) : "(unset)",
2544 dbus_message_get_member (message) ?
2545 dbus_message_get_member (message) : "(unset)",
2546 dbus_message_get_error_name (message) ?
2547 dbus_message_get_error_name (message) : "(unset)",
2548 dbus_connection_get_is_connected (connection) ?
2549 "" : " (disconnected)");
2551 _dbus_assert (dbus_message_get_sender (message) != NULL);
2553 if (!dbus_connection_get_is_connected (connection))
2554 return TRUE; /* silently ignore disconnected connections */
2556 d = BUS_CONNECTION_DATA (connection);
2557 _dbus_assert (d != NULL);
2559 if (!deferred_dispatch && d->deferred_messages != NULL)
2561 BusDeferredMessage *deferred_message;
2562 dbus_bool_t success;
2563 /* sender and addressed recipient are not required at this point as we only need to send message
2564 * to a single recipient without performing policy check. */
2565 deferred_message = bus_deferred_message_new (message,
2570 if (deferred_message == NULL)
2573 success = bus_deferred_message_queue_at_recipient(deferred_message, transaction,
2575 bus_deferred_message_unref(deferred_message);
2580 to_send = dbus_new (MessageToSend, 1);
2581 if (to_send == NULL)
2586 to_send->preallocated = dbus_connection_preallocate_send (connection);
2587 if (to_send->preallocated == NULL)
2589 dbus_free (to_send);
2593 dbus_message_ref (message);
2594 to_send->message = message;
2595 to_send->transaction = transaction;
2597 _dbus_verbose ("about to prepend message\n");
2599 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2601 message_to_send_free (connection, to_send);
2605 _dbus_verbose ("prepended message\n");
2607 /* See if we already had this connection in the list
2608 * for this transaction. If we have a pending message,
2609 * then we should already be in transaction->connections
2611 link = _dbus_list_get_first_link (&d->transaction_messages);
2612 _dbus_assert (link->data == to_send);
2613 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2614 while (link != NULL)
2616 MessageToSend *m = link->data;
2617 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2619 if (m->transaction == transaction)
2627 if (!_dbus_list_prepend (&transaction->connections, connection))
2629 _dbus_list_remove (&d->transaction_messages, to_send);
2630 message_to_send_free (connection, to_send);
2639 transaction_free (BusTransaction *transaction)
2641 _dbus_assert (transaction->connections == NULL);
2643 free_cancel_hooks (transaction);
2645 dbus_free (transaction);
2649 connection_cancel_transaction (DBusConnection *connection,
2650 BusTransaction *transaction)
2653 BusConnectionData *d;
2655 d = BUS_CONNECTION_DATA (connection);
2656 _dbus_assert (d != NULL);
2658 link = _dbus_list_get_first_link (&d->transaction_messages);
2659 while (link != NULL)
2661 MessageToSend *m = link->data;
2662 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2664 if (m->transaction == transaction)
2666 _dbus_list_remove_link (&d->transaction_messages,
2669 message_to_send_free (connection, m);
2677 bus_transaction_cancel_and_free (BusTransaction *transaction)
2679 DBusConnection *connection;
2681 _dbus_verbose ("TRANSACTION: cancelled\n");
2683 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2684 connection_cancel_transaction (connection, transaction);
2686 _dbus_list_foreach (&transaction->cancel_hooks,
2687 cancel_hook_cancel, NULL);
2689 transaction_free (transaction);
2693 connection_execute_transaction (DBusConnection *connection,
2694 BusTransaction *transaction)
2697 BusConnectionData *d;
2699 d = BUS_CONNECTION_DATA (connection);
2700 _dbus_assert (d != NULL);
2702 /* Send the queue in order (FIFO) */
2703 link = _dbus_list_get_last_link (&d->transaction_messages);
2704 while (link != NULL)
2706 MessageToSend *m = link->data;
2707 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2709 if (m->transaction == transaction)
2711 _dbus_list_remove_link (&d->transaction_messages,
2714 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2716 dbus_connection_send_preallocated (connection,
2721 m->preallocated = NULL; /* so we don't double-free it */
2723 message_to_send_free (connection, m);
2731 bus_transaction_execute_and_free (BusTransaction *transaction)
2733 /* For each connection in transaction->connections
2736 DBusConnection *connection;
2738 _dbus_verbose ("TRANSACTION: executing\n");
2740 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2741 connection_execute_transaction (connection, transaction);
2743 transaction_free (transaction);
2747 bus_transaction_copy_connections (BusTransaction *transaction)
2749 DBusList *result = NULL;
2750 _dbus_list_copy (&transaction->connections, &result);
2755 bus_connection_remove_transactions (DBusConnection *connection)
2757 MessageToSend *to_send;
2758 BusConnectionData *d;
2760 d = BUS_CONNECTION_DATA (connection);
2761 _dbus_assert (d != NULL);
2763 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2765 /* only has an effect for the first MessageToSend listing this transaction */
2766 _dbus_list_remove (&to_send->transaction->connections,
2769 _dbus_list_remove (&d->transaction_messages, to_send);
2770 message_to_send_free (connection, to_send);
2775 * Converts the DBusError to a message reply
2778 bus_transaction_send_error_reply (BusTransaction *transaction,
2779 DBusConnection *connection,
2780 const DBusError *error,
2781 DBusMessage *in_reply_to)
2785 _dbus_assert (error != NULL);
2786 _DBUS_ASSERT_ERROR_IS_SET (error);
2788 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2789 error->name, error->message);
2791 reply = dbus_message_new_error (in_reply_to,
2797 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2799 dbus_message_unref (reply);
2803 dbus_message_unref (reply);
2809 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2810 BusTransactionCancelFunction cancel_function,
2812 DBusFreeFunction free_data_function)
2816 ch = dbus_new (CancelHook, 1);
2820 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2821 cancel_function, data);
2823 ch->cancel_function = cancel_function;
2825 ch->free_data_function = free_data_function;
2827 /* It's important that the hooks get run in reverse order that they
2830 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2840 bus_connection_dispatch_deferred (DBusConnection *connection)
2842 BusDeferredMessage *message;
2844 _dbus_return_if_fail (connection != NULL);
2846 while ((message = bus_connection_pop_deferred_message(connection)) != NULL)
2848 bus_deferred_message_dispatch(message);
2849 bus_deferred_message_unref(message);
2854 bus_connection_has_deferred_messages (DBusConnection *connection)
2856 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2858 _dbus_assert (d != NULL);
2860 return d->deferred_messages != NULL ? TRUE : FALSE;
2864 bus_connection_queue_deferred_message (DBusConnection *connection,
2865 BusDeferredMessage *message,
2866 dbus_bool_t prepend)
2868 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2869 dbus_bool_t success;
2871 _dbus_assert (d != NULL);
2874 success = _dbus_list_prepend(&d->deferred_messages, message);
2876 success = _dbus_list_append(&d->deferred_messages, message);
2880 bus_deferred_message_ref(message);
2887 BusDeferredMessage *
2888 bus_connection_pop_deferred_message (DBusConnection *connection)
2891 BusDeferredMessage *message;
2892 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2894 _dbus_assert (d != NULL);
2896 link =_dbus_list_get_first_link(&d->deferred_messages);
2899 message = link->data;
2900 if (!bus_deferred_message_waits_for_check(message))
2902 _dbus_list_remove_link(&d->deferred_messages, link);
2911 bus_connection_putback_deferred_message (DBusConnection *connection, BusDeferredMessage *message)
2913 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2915 _dbus_assert (d != NULL);
2917 if (_dbus_list_prepend(&d->deferred_messages, message))
2925 bus_connection_clear_deferred_messages (DBusConnection *connection)
2927 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2930 BusDeferredMessage *message;
2932 _dbus_assert (d != NULL);
2934 link =_dbus_list_get_first_link(&d->deferred_messages);
2935 while (link != NULL)
2937 next = _dbus_list_get_next_link (&d->deferred_messages, link);
2938 message = link->data;
2940 bus_deferred_message_unref(message);
2941 _dbus_list_remove_link(&d->deferred_messages, link);
2948 bus_connection_remove_deferred_message (DBusConnection *connection,
2949 BusDeferredMessage *message)
2951 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2953 _dbus_assert (d != NULL);
2955 if (_dbus_list_remove(&d->deferred_messages, message))
2956 bus_deferred_message_unref(message);
2960 bus_connections_get_n_active (BusConnections *connections)
2962 return connections->n_completed;
2966 bus_connections_get_n_incomplete (BusConnections *connections)
2968 return connections->n_incomplete;
2971 #ifdef DBUS_ENABLE_STATS
2973 bus_connections_get_total_match_rules (BusConnections *connections)
2975 return connections->total_match_rules;
2979 bus_connections_get_peak_match_rules (BusConnections *connections)
2981 return connections->peak_match_rules;
2985 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2987 return connections->peak_match_rules_per_conn;
2991 bus_connections_get_total_bus_names (BusConnections *connections)
2993 return connections->total_bus_names;
2997 bus_connections_get_peak_bus_names (BusConnections *connections)
2999 return connections->peak_bus_names;
3003 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
3005 return connections->peak_bus_names_per_conn;
3009 bus_connection_get_peak_match_rules (DBusConnection *connection)
3011 BusConnectionData *d;
3013 d = BUS_CONNECTION_DATA (connection);
3014 _dbus_assert(d != NULL);
3016 return d->peak_match_rules;
3020 bus_connection_get_peak_bus_names (DBusConnection *connection)
3022 BusConnectionData *d;
3024 d = BUS_CONNECTION_DATA (connection);
3025 _dbus_assert(d != NULL);
3027 return d->peak_bus_names;
3030 int bus_connection_get_n_pending_replies (DBusConnection *connection)
3032 BusConnectionData *d;
3034 BusPendingReply *pending;
3037 d = BUS_CONNECTION_DATA (connection);
3038 _dbus_assert(d != NULL);
3040 link = bus_expire_list_get_first_link(d->connections->pending_replies);
3041 while (link != NULL)
3043 pending = link->data;
3044 link = bus_expire_list_get_next_link(d->connections->pending_replies,
3046 if (pending->will_get_reply == connection)
3054 bus_connection_get_peak_pending_replies (DBusConnection *connection)
3056 BusConnectionData *d;
3058 d = BUS_CONNECTION_DATA (connection);
3059 _dbus_assert (d != NULL);
3061 return d->peak_pending_replies;
3063 #endif /* DBUS_ENABLE_STATS */
3066 bus_connection_is_monitor (DBusConnection *connection)
3068 BusConnectionData *d;
3070 d = BUS_CONNECTION_DATA (connection);
3071 _dbus_assert(d != NULL);
3073 return d->link_in_monitors != NULL;
3077 bcd_add_monitor_rules (BusConnectionData *d,
3078 DBusConnection *connection,
3081 BusMatchmaker *mm = d->connections->monitor_matchmaker;
3086 mm = bus_matchmaker_new ();
3091 d->connections->monitor_matchmaker = mm;
3094 for (iter = _dbus_list_get_first_link (rules);
3096 iter = _dbus_list_get_next_link (rules, iter))
3098 if (!bus_matchmaker_add_rule (mm, iter->data))
3100 bus_matchmaker_disconnected (mm, connection);
3109 bcd_drop_monitor_rules (BusConnectionData *d,
3110 DBusConnection *connection)
3112 BusMatchmaker *mm = d->connections->monitor_matchmaker;
3115 bus_matchmaker_disconnected (mm, connection);
3119 bus_connection_be_monitor (DBusConnection *connection,
3120 BusTransaction *transaction,
3124 BusConnectionData *d;
3129 d = BUS_CONNECTION_DATA (connection);
3130 _dbus_assert (d != NULL);
3132 link = _dbus_list_alloc_link (connection);
3136 BUS_SET_OOM (error);
3140 if (!bcd_add_monitor_rules (d, connection, rules))
3142 _dbus_list_free_link (link);
3143 BUS_SET_OOM (error);
3147 /* release all its names */
3148 if (!_dbus_list_copy (&d->services_owned, &tmp))
3150 bcd_drop_monitor_rules (d, connection);
3151 _dbus_list_free_link (link);
3152 BUS_SET_OOM (error);
3156 for (iter = _dbus_list_get_first_link (&tmp);
3158 iter = _dbus_list_get_next_link (&tmp, iter))
3160 BusService *service = iter->data;
3162 /* This call is transactional: if there isn't enough memory to
3163 * do everything, then the service gets all its names back when
3164 * the transaction is cancelled due to OOM. */
3165 if (!bus_service_remove_owner (service, connection, transaction, error))
3167 bcd_drop_monitor_rules (d, connection);
3168 _dbus_list_free_link (link);
3169 _dbus_list_clear (&tmp);
3174 /* We have now done everything that can fail, so there is no problem
3175 * with doing the irrevocable stuff. */
3177 _dbus_list_clear (&tmp);
3179 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
3180 "Connection %s (%s) became a monitor.", d->name,
3181 d->cached_loginfo_string);
3183 if (d->n_match_rules > 0)
3187 mm = bus_context_get_matchmaker (d->connections->context);
3188 bus_matchmaker_disconnected (mm, connection);
3191 /* flag it as a monitor */
3192 d->link_in_monitors = link;
3193 _dbus_list_append_link (&d->connections->monitors, link);
3195 /* it isn't allowed to reply, and it is no longer relevant whether it
3196 * receives replies */
3197 bus_connection_drop_pending_replies (d->connections, connection);