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"
34 #include <dbus/dbus-list.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-timeout.h>
37 #include <dbus/dbus-connection-internal.h>
38 #include <dbus/dbus-internals.h>
40 /* Trim executed commands to this length; we want to keep logs readable */
41 #define MAX_LOG_COMMAND_LEN 50
43 static void bus_connection_remove_transactions (DBusConnection *connection);
47 BusExpireItem expire_item;
49 DBusConnection *will_get_reply;
50 DBusConnection *will_send_reply;
52 dbus_uint32_t reply_serial;
59 DBusList *completed; /**< List of all completed connections */
60 int n_completed; /**< Length of completed list */
61 DBusList *incomplete; /**< List of all not-yet-active connections */
62 int n_incomplete; /**< Length of incomplete list */
64 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
65 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
66 int stamp; /**< Incrementing number */
67 BusExpireList *pending_replies; /**< List of pending replies */
69 /** List of all monitoring connections, a subset of completed.
70 * Each member is a #DBusConnection. */
72 BusMatchmaker *monitor_matchmaker;
74 #ifdef DBUS_ENABLE_STATS
75 int total_match_rules;
77 int peak_match_rules_per_conn;
81 int peak_bus_names_per_conn;
85 static dbus_int32_t connection_data_slot = -1;
89 BusConnections *connections;
90 DBusList *link_in_connection_list;
91 DBusConnection *connection;
92 DBusList *services_owned;
94 DBusList *match_rules;
97 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
98 DBusMessage *oom_message;
99 DBusPreallocatedSend *oom_preallocated;
100 BusClientPolicy *policy;
102 char *cached_loginfo_string;
103 BusSELinuxID *selinux_id;
104 BusAppArmorConfinement *apparmor_confinement;
106 long connection_tv_sec; /**< Time when we connected (seconds component) */
107 long connection_tv_usec; /**< Time when we connected (microsec component) */
108 int stamp; /**< connections->stamp last time we were traversed */
110 #ifdef DBUS_ENABLE_STATS
111 int peak_match_rules;
114 int n_pending_unix_fds;
115 DBusTimeout *pending_unix_fds_timeout;
117 /** non-NULL if and only if this is a monitor */
118 DBusList *link_in_monitors;
121 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
125 static void bus_connection_drop_pending_replies (BusConnections *connections,
126 DBusConnection *connection);
128 static dbus_bool_t expire_incomplete_timeout (void *data);
130 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
133 connection_get_loop (DBusConnection *connection)
135 BusConnectionData *d;
137 d = BUS_CONNECTION_DATA (connection);
138 _dbus_assert(d != NULL);
140 return bus_context_get_loop (d->connections->context);
145 get_connections_for_uid (BusConnections *connections,
151 /* val is NULL is 0 when it isn't in the hash yet */
153 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
156 current_count = _DBUS_POINTER_TO_INT (val);
158 return current_count;
162 adjust_connections_for_uid (BusConnections *connections,
168 current_count = get_connections_for_uid (connections, uid);
170 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
171 ": was %d adjustment %d making %d\n",
172 uid, current_count, adjustment, current_count + adjustment);
174 _dbus_assert (current_count >= 0);
176 current_count += adjustment;
178 _dbus_assert (current_count >= 0);
180 if (current_count == 0)
182 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
189 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
190 uid, _DBUS_INT_TO_POINTER (current_count));
192 /* only positive adjustment can fail as otherwise
193 * a hash entry should already exist
195 _dbus_assert (adjustment > 0 ||
196 (adjustment <= 0 && retval));
203 bus_connection_disconnected (DBusConnection *connection)
205 BusConnectionData *d;
207 BusMatchmaker *matchmaker;
209 d = BUS_CONNECTION_DATA (connection);
210 _dbus_assert (d != NULL);
212 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
213 d->name ? d->name : "(inactive)");
215 /* Delete our match rules */
216 if (d->n_match_rules > 0)
218 matchmaker = bus_context_get_matchmaker (d->connections->context);
219 bus_matchmaker_disconnected (matchmaker, connection);
222 /* Drop any service ownership. Unfortunately, this requires
223 * memory allocation and there doesn't seem to be a good way to
224 * handle it other than sleeping; we can't "fail" the operation of
225 * disconnecting a client, and preallocating a broadcast "service is
226 * now gone" message for every client-service pair seems kind of
229 while ((service = _dbus_list_get_last (&d->services_owned)))
231 BusTransaction *transaction;
236 dbus_error_init (&error);
238 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
239 _dbus_wait_for_memory ();
241 if (!bus_service_remove_owner (service, connection,
242 transaction, &error))
244 _DBUS_ASSERT_ERROR_IS_SET (&error);
246 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
248 dbus_error_free (&error);
249 bus_transaction_cancel_and_free (transaction);
250 _dbus_wait_for_memory ();
255 _dbus_verbose ("Failed to remove service owner: %s %s\n",
256 error.name, error.message);
257 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
261 bus_transaction_execute_and_free (transaction);
264 bus_dispatch_remove_connection (connection);
266 /* no more watching */
267 if (!dbus_connection_set_watch_functions (connection,
271 _dbus_assert_not_reached ("setting watch functions to NULL failed");
273 if (!dbus_connection_set_timeout_functions (connection,
277 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
279 dbus_connection_set_unix_user_function (connection,
281 dbus_connection_set_windows_user_function (connection,
284 dbus_connection_set_dispatch_status_function (connection,
287 if (d->pending_unix_fds_timeout)
289 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
290 d->pending_unix_fds_timeout);
291 _dbus_timeout_unref (d->pending_unix_fds_timeout);
293 d->pending_unix_fds_timeout = NULL;
294 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
296 bus_connection_remove_transactions (connection);
298 if (d->link_in_monitors != NULL)
300 BusMatchmaker *mm = d->connections->monitor_matchmaker;
303 bus_matchmaker_disconnected (mm, connection);
305 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
306 d->link_in_monitors = NULL;
309 if (d->link_in_connection_list != NULL)
315 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
316 d->link_in_connection_list = NULL;
317 d->connections->n_completed -= 1;
319 if (dbus_connection_get_unix_user (connection, &uid))
321 if (!adjust_connections_for_uid (d->connections,
323 _dbus_assert_not_reached ("adjusting downward should never fail");
328 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
329 d->link_in_connection_list = NULL;
330 d->connections->n_incomplete -= 1;
332 /* If we have dropped below the max. number of incomplete
333 * connections, start accept()ing again */
334 bus_context_check_all_watches (d->connections->context);
337 _dbus_assert (d->connections->n_incomplete >= 0);
338 _dbus_assert (d->connections->n_completed >= 0);
341 bus_connection_drop_pending_replies (d->connections, connection);
343 /* frees "d" as side effect */
344 dbus_connection_set_data (connection,
345 connection_data_slot,
348 dbus_connection_unref (connection);
352 add_connection_watch (DBusWatch *watch,
355 DBusConnection *connection = data;
357 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
361 remove_connection_watch (DBusWatch *watch,
364 DBusConnection *connection = data;
366 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
370 toggle_connection_watch (DBusWatch *watch,
373 DBusConnection *connection = data;
375 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
379 add_connection_timeout (DBusTimeout *timeout,
382 DBusConnection *connection = data;
384 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
388 remove_connection_timeout (DBusTimeout *timeout,
391 DBusConnection *connection = data;
393 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
397 dispatch_status_function (DBusConnection *connection,
398 DBusDispatchStatus new_status,
401 DBusLoop *loop = data;
403 if (new_status != DBUS_DISPATCH_COMPLETE)
405 while (!_dbus_loop_queue_dispatch (loop, connection))
406 _dbus_wait_for_memory ();
411 allow_unix_user_function (DBusConnection *connection,
415 BusConnectionData *d;
417 d = BUS_CONNECTION_DATA (connection);
419 _dbus_assert (d != NULL);
421 return bus_context_allow_unix_user (d->connections->context, uid);
425 free_connection_data (void *data)
427 BusConnectionData *d = data;
429 /* services_owned should be NULL since we should be disconnected */
430 _dbus_assert (d->services_owned == NULL);
431 _dbus_assert (d->n_services_owned == 0);
433 _dbus_assert (d->transaction_messages == NULL);
435 if (d->oom_preallocated)
436 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
439 dbus_message_unref (d->oom_message);
442 bus_client_policy_unref (d->policy);
445 bus_selinux_id_unref (d->selinux_id);
447 if (d->apparmor_confinement)
448 bus_apparmor_confinement_unref (d->apparmor_confinement);
450 dbus_free (d->cached_loginfo_string);
458 bus_connections_new (BusContext *context)
460 BusConnections *connections;
462 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
465 connections = dbus_new0 (BusConnections, 1);
466 if (connections == NULL)
469 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
471 if (connections->completed_by_user == NULL)
474 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
475 expire_incomplete_timeout,
477 if (connections->expire_timeout == NULL)
480 _dbus_timeout_disable (connections->expire_timeout);
482 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
483 bus_context_get_reply_timeout (context),
484 bus_pending_reply_expired,
486 if (connections->pending_replies == NULL)
489 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
490 connections->expire_timeout))
493 connections->refcount = 1;
494 connections->context = context;
499 bus_expire_list_free (connections->pending_replies);
501 _dbus_timeout_unref (connections->expire_timeout);
503 _dbus_hash_table_unref (connections->completed_by_user);
505 dbus_free (connections);
507 dbus_connection_free_data_slot (&connection_data_slot);
513 bus_connections_ref (BusConnections *connections)
515 _dbus_assert (connections->refcount > 0);
516 connections->refcount += 1;
522 bus_connections_unref (BusConnections *connections)
524 _dbus_assert (connections->refcount > 0);
525 connections->refcount -= 1;
526 if (connections->refcount == 0)
528 /* drop all incomplete */
529 while (connections->incomplete != NULL)
531 DBusConnection *connection;
533 connection = connections->incomplete->data;
535 dbus_connection_ref (connection);
536 dbus_connection_close (connection);
537 bus_connection_disconnected (connection);
538 dbus_connection_unref (connection);
541 _dbus_assert (connections->n_incomplete == 0);
543 /* drop all monitors */
544 _dbus_list_clear (&connections->monitors);
546 /* drop all real connections */
547 while (connections->completed != NULL)
549 DBusConnection *connection;
551 connection = connections->completed->data;
553 dbus_connection_ref (connection);
554 dbus_connection_close (connection);
555 bus_connection_disconnected (connection);
556 dbus_connection_unref (connection);
559 _dbus_assert (connections->n_completed == 0);
561 bus_expire_list_free (connections->pending_replies);
563 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
564 connections->expire_timeout);
566 _dbus_timeout_unref (connections->expire_timeout);
568 _dbus_hash_table_unref (connections->completed_by_user);
570 if (connections->monitor_matchmaker != NULL)
571 bus_matchmaker_unref (connections->monitor_matchmaker);
573 dbus_free (connections);
575 dbus_connection_free_data_slot (&connection_data_slot);
579 /* Used for logging */
581 cache_peer_loginfo_string (BusConnectionData *d,
582 DBusConnection *connection)
584 DBusString loginfo_buf;
587 char *windows_sid, *security_label;
588 dbus_bool_t prev_added;
590 if (!_dbus_string_init (&loginfo_buf))
594 if (dbus_connection_get_unix_user (connection, &uid))
596 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
602 if (dbus_connection_get_unix_process_id (connection, &pid))
606 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
609 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
611 /* Ignore errors here; we may not have permissions to read the
613 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
614 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
620 if (dbus_connection_get_windows_user (connection, &windows_sid))
622 dbus_bool_t did_append;
626 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
630 did_append = _dbus_string_append_printf (&loginfo_buf,
631 "sid=\"%s\"", windows_sid);
632 dbus_free (windows_sid);
639 if (_dbus_connection_get_linux_security_label (connection, &security_label))
641 dbus_bool_t did_append;
645 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
649 did_append = _dbus_string_append_printf (&loginfo_buf,
650 "label=\"%s\"", security_label);
651 dbus_free (security_label);
658 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
661 _dbus_string_free (&loginfo_buf);
665 _dbus_string_free (&loginfo_buf);
670 check_pending_fds_cb (DBusConnection *connection)
672 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
673 int n_pending_unix_fds_old;
674 int n_pending_unix_fds_new;
676 _dbus_assert(d != NULL);
678 n_pending_unix_fds_old = d->n_pending_unix_fds;
679 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
681 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
682 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
684 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
686 _dbus_timeout_restart (d->pending_unix_fds_timeout,
687 bus_context_get_pending_fd_timeout (d->connections->context));
690 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
692 _dbus_timeout_disable (d->pending_unix_fds_timeout);
696 d->n_pending_unix_fds = n_pending_unix_fds_new;
700 pending_unix_fds_timeout_cb (void *data)
702 DBusConnection *connection = data;
703 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
707 _dbus_assert (d != NULL);
708 limit = bus_context_get_pending_fd_timeout (d->connections->context);
710 if (dbus_connection_get_unix_user (connection, &uid) && uid == 0)
712 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
713 "Connection \"%s\" (%s) has had Unix fds pending for "
714 "too long (pending_fd_timeout=%dms); tolerating it, "
715 "because it has uid 0",
716 d->name != NULL ? d->name : "(null)",
717 bus_connection_get_loginfo (connection),
722 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
723 "Connection \"%s\" (%s) has had Unix fds pending for too long, "
724 "closing it (pending_fd_timeout=%d ms)",
725 d->name != NULL ? d->name : "(null)",
726 bus_connection_get_loginfo (connection),
729 dbus_connection_close (connection);
734 bus_connections_setup_connection (BusConnections *connections,
735 DBusConnection *connection)
738 BusConnectionData *d;
743 d = dbus_new0 (BusConnectionData, 1);
748 d->connections = connections;
749 d->connection = connection;
751 _dbus_get_monotonic_time (&d->connection_tv_sec,
752 &d->connection_tv_usec);
754 _dbus_assert (connection_data_slot >= 0);
756 if (!dbus_connection_set_data (connection,
757 connection_data_slot,
758 d, free_connection_data))
764 dbus_connection_set_route_peer_messages (connection, TRUE);
768 dbus_error_init (&error);
769 d->selinux_id = bus_selinux_init_connection_id (connection,
771 if (dbus_error_is_set (&error))
773 /* This is a bit bogus because we pretend all errors
774 * are OOM; this is done because we know that in bus.c
775 * an OOM error disconnects the connection, which is
776 * the same thing we want on any other error.
778 dbus_error_free (&error);
782 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
784 if (dbus_error_is_set (&error))
786 /* This is a bit bogus because we pretend all errors
787 * are OOM; this is done because we know that in bus.c
788 * an OOM error disconnects the connection, which is
789 * the same thing we want on any other error.
791 dbus_error_free (&error);
795 if (!dbus_connection_set_watch_functions (connection,
796 add_connection_watch,
797 remove_connection_watch,
798 toggle_connection_watch,
803 if (!dbus_connection_set_timeout_functions (connection,
804 add_connection_timeout,
805 remove_connection_timeout,
810 /* For now we don't need to set a Windows user function because
811 * there are no policies in the config file controlling what
812 * Windows users can connect. The default 'same user that owns the
813 * bus can connect' behavior of DBusConnection is fine on Windows.
815 dbus_connection_set_unix_user_function (connection,
816 allow_unix_user_function,
819 dbus_connection_set_dispatch_status_function (connection,
820 dispatch_status_function,
821 bus_context_get_loop (connections->context),
824 d->link_in_connection_list = _dbus_list_alloc_link (connection);
825 if (d->link_in_connection_list == NULL)
828 /* Setup the connection with the dispatcher */
829 if (!bus_dispatch_add_connection (connection))
832 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
834 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
836 bus_dispatch_remove_connection (connection);
841 /* Setup pending fds timeout (see #80559) */
842 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
843 pending_unix_fds_timeout_cb,
845 if (d->pending_unix_fds_timeout == NULL)
848 _dbus_timeout_disable (d->pending_unix_fds_timeout);
849 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
850 d->pending_unix_fds_timeout))
853 _dbus_connection_set_pending_fds_function (connection,
854 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
857 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
858 connections->n_incomplete += 1;
860 dbus_connection_ref (connection);
862 bus_connections_expire_incomplete (connections);
864 /* The listening socket is removed from the main loop,
865 * i.e. does not accept(), while n_incomplete is at its
866 * maximum value; so we shouldn't get here in that case */
867 _dbus_assert (connections->n_incomplete <=
868 bus_context_get_max_incomplete_connections (connections->context));
870 /* If we have the maximum number of incomplete connections,
871 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
872 bus_context_check_all_watches (d->connections->context);
880 bus_selinux_id_unref (d->selinux_id);
881 d->selinux_id = NULL;
883 if (d->apparmor_confinement)
884 bus_apparmor_confinement_unref (d->apparmor_confinement);
885 d->apparmor_confinement = NULL;
887 if (!dbus_connection_set_watch_functions (connection,
891 _dbus_assert_not_reached ("setting watch functions to NULL failed");
893 if (!dbus_connection_set_timeout_functions (connection,
897 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
899 dbus_connection_set_unix_user_function (connection,
902 dbus_connection_set_windows_user_function (connection,
905 dbus_connection_set_dispatch_status_function (connection,
908 if (d->pending_unix_fds_timeout)
909 _dbus_timeout_unref (d->pending_unix_fds_timeout);
911 d->pending_unix_fds_timeout = NULL;
913 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
915 if (d->link_in_connection_list != NULL)
917 _dbus_assert (d->link_in_connection_list->next == NULL);
918 _dbus_assert (d->link_in_connection_list->prev == NULL);
919 _dbus_list_free_link (d->link_in_connection_list);
920 d->link_in_connection_list = NULL;
923 if (!dbus_connection_set_data (connection,
924 connection_data_slot,
926 _dbus_assert_not_reached ("failed to set connection data to null");
928 /* "d" has now been freed */
935 bus_connections_expire_incomplete (BusConnections *connections)
941 if (connections->incomplete != NULL)
943 long tv_sec, tv_usec;
947 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
948 auth_timeout = bus_context_get_auth_timeout (connections->context);
950 link = _dbus_list_get_first_link (&connections->incomplete);
953 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
954 DBusConnection *connection;
955 BusConnectionData *d;
958 connection = link->data;
960 d = BUS_CONNECTION_DATA (connection);
962 _dbus_assert (d != NULL);
964 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
965 d->connection_tv_usec,
968 if (elapsed >= (double) auth_timeout)
970 /* Unfortunately, we can't identify the connection: it doesn't
971 * have a unique name yet, we don't know its uid/pid yet,
973 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
974 "Connection has not authenticated soon enough, closing it "
975 "(auth_timeout=%dms, elapsed: %.0fms)",
976 auth_timeout, elapsed);
978 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
979 dbus_connection_close (connection);
983 /* We can end the loop, since the connections are in oldest-first order */
984 next_interval = ((double)auth_timeout) - elapsed;
985 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
986 connection, next_interval);
995 bus_expire_timeout_set_interval (connections->expire_timeout,
1000 expire_incomplete_timeout (void *data)
1002 BusConnections *connections = data;
1004 _dbus_verbose ("Running\n");
1006 /* note that this may remove the timeout */
1007 bus_connections_expire_incomplete (connections);
1013 bus_connection_get_unix_groups (DBusConnection *connection,
1014 unsigned long **groups,
1023 if (dbus_connection_get_unix_user (connection, &uid))
1025 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
1027 _dbus_verbose ("Did not get any groups for UID %lu\n",
1033 _dbus_verbose ("Got %d groups for UID %lu\n",
1039 return TRUE; /* successfully got 0 groups */
1043 bus_connection_is_in_unix_group (DBusConnection *connection,
1047 unsigned long *group_ids;
1050 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1055 while (i < n_group_ids)
1057 if (group_ids[i] == gid)
1059 dbus_free (group_ids);
1065 dbus_free (group_ids);
1070 bus_connection_get_loginfo (DBusConnection *connection)
1072 BusConnectionData *d;
1074 d = BUS_CONNECTION_DATA (connection);
1075 _dbus_assert(d != NULL);
1077 if (!bus_connection_is_active (connection))
1079 return d->cached_loginfo_string;
1083 bus_connection_get_policy (DBusConnection *connection)
1085 BusConnectionData *d;
1087 d = BUS_CONNECTION_DATA (connection);
1089 _dbus_assert (d != NULL);
1090 _dbus_assert (d->policy != NULL);
1096 foreach_active (BusConnections *connections,
1097 BusConnectionForeachFunction function,
1102 link = _dbus_list_get_first_link (&connections->completed);
1103 while (link != NULL)
1105 DBusConnection *connection = link->data;
1106 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1108 if (!(* function) (connection, data))
1118 foreach_inactive (BusConnections *connections,
1119 BusConnectionForeachFunction function,
1124 link = _dbus_list_get_first_link (&connections->incomplete);
1125 while (link != NULL)
1127 DBusConnection *connection = link->data;
1128 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1130 if (!(* function) (connection, data))
1140 * Calls function on each active connection; if the function returns
1141 * #FALSE, stops iterating. Active connections are authenticated
1142 * and have sent a Hello message.
1144 * @param connections the connections object
1145 * @param function the function
1146 * @param data data to pass to it as a second arg
1149 bus_connections_foreach_active (BusConnections *connections,
1150 BusConnectionForeachFunction function,
1153 foreach_active (connections, function, data);
1157 * Calls function on each connection; if the function returns
1158 * #FALSE, stops iterating.
1160 * @param connections the connections object
1161 * @param function the function
1162 * @param data data to pass to it as a second arg
1165 bus_connections_foreach (BusConnections *connections,
1166 BusConnectionForeachFunction function,
1169 if (!foreach_active (connections, function, data))
1172 foreach_inactive (connections, function, data);
1176 bus_connections_get_context (BusConnections *connections)
1178 return connections->context;
1182 * This is used to avoid covering the same connection twice when
1183 * traversing connections. Note that it assumes we will
1184 * bus_connection_mark_stamp() each connection at least once per
1185 * INT_MAX increments of the global stamp, or wraparound would break
1189 bus_connections_increment_stamp (BusConnections *connections)
1191 connections->stamp += 1;
1194 /* Mark connection with current stamp, return TRUE if it
1195 * didn't already have that stamp
1198 bus_connection_mark_stamp (DBusConnection *connection)
1200 BusConnectionData *d;
1202 d = BUS_CONNECTION_DATA (connection);
1204 _dbus_assert (d != NULL);
1206 if (d->stamp == d->connections->stamp)
1210 d->stamp = d->connections->stamp;
1216 bus_connection_get_context (DBusConnection *connection)
1218 BusConnectionData *d;
1220 d = BUS_CONNECTION_DATA (connection);
1222 _dbus_assert (d != NULL);
1224 return d->connections->context;
1228 bus_connection_get_connections (DBusConnection *connection)
1230 BusConnectionData *d;
1232 d = BUS_CONNECTION_DATA (connection);
1234 _dbus_assert (d != NULL);
1236 return d->connections;
1240 bus_connection_get_registry (DBusConnection *connection)
1242 BusConnectionData *d;
1244 d = BUS_CONNECTION_DATA (connection);
1246 _dbus_assert (d != NULL);
1248 return bus_context_get_registry (d->connections->context);
1252 bus_connection_get_activation (DBusConnection *connection)
1254 BusConnectionData *d;
1256 d = BUS_CONNECTION_DATA (connection);
1258 _dbus_assert (d != NULL);
1260 return bus_context_get_activation (d->connections->context);
1264 bus_connection_get_matchmaker (DBusConnection *connection)
1266 BusConnectionData *d;
1268 d = BUS_CONNECTION_DATA (connection);
1270 _dbus_assert (d != NULL);
1272 return bus_context_get_matchmaker (d->connections->context);
1276 bus_connection_get_selinux_id (DBusConnection *connection)
1278 BusConnectionData *d;
1280 d = BUS_CONNECTION_DATA (connection);
1282 _dbus_assert (d != NULL);
1284 return d->selinux_id;
1287 BusAppArmorConfinement*
1288 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1290 BusConnectionData *d;
1292 d = BUS_CONNECTION_DATA (connection);
1294 _dbus_assert (d != NULL);
1296 bus_apparmor_confinement_ref (d->apparmor_confinement);
1297 return d->apparmor_confinement;
1301 * Checks whether the connection is registered with the message bus.
1303 * @param connection the connection
1304 * @returns #TRUE if we're an active message bus participant
1307 bus_connection_is_active (DBusConnection *connection)
1309 BusConnectionData *d;
1311 d = BUS_CONNECTION_DATA (connection);
1312 _dbus_assert(d != NULL);
1314 return d->name != NULL;
1318 bus_connection_preallocate_oom_error (DBusConnection *connection)
1320 DBusMessage *message;
1321 DBusPreallocatedSend *preallocated;
1322 BusConnectionData *d;
1324 d = BUS_CONNECTION_DATA (connection);
1326 _dbus_assert (d != NULL);
1328 if (d->oom_preallocated != NULL)
1331 preallocated = dbus_connection_preallocate_send (connection);
1332 if (preallocated == NULL)
1335 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1337 if (message == NULL)
1339 dbus_connection_free_preallocated_send (connection, preallocated);
1343 /* d->name may be NULL, but that is OK */
1344 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1345 !dbus_message_set_destination (message, d->name) ||
1346 !dbus_message_set_sender (message,
1349 dbus_connection_free_preallocated_send (connection, preallocated);
1350 dbus_message_unref (message);
1354 /* set reply serial to placeholder value just so space is already allocated
1357 if (!dbus_message_set_reply_serial (message, 14))
1359 dbus_connection_free_preallocated_send (connection, preallocated);
1360 dbus_message_unref (message);
1364 d->oom_message = message;
1365 d->oom_preallocated = preallocated;
1371 bus_connection_send_oom_error (DBusConnection *connection,
1372 DBusMessage *in_reply_to)
1374 BusConnectionData *d;
1376 d = BUS_CONNECTION_DATA (connection);
1378 _dbus_assert (d != NULL);
1379 _dbus_assert (d->oom_message != NULL);
1381 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1382 "dbus-daemon transaction failed (OOM), sending error to "
1383 "sender %s", bus_connection_get_loginfo (connection));
1385 /* should always succeed since we set it to a placeholder earlier */
1386 if (!dbus_message_set_reply_serial (d->oom_message,
1387 dbus_message_get_serial (in_reply_to)))
1388 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1390 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1392 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1393 d->oom_message, NULL);
1395 dbus_message_unref (d->oom_message);
1396 d->oom_message = NULL;
1397 d->oom_preallocated = NULL;
1400 #ifdef DBUS_ENABLE_STATS
1402 update_peak (int *peak,
1411 bus_connection_add_match_rule_link (DBusConnection *connection,
1414 BusConnectionData *d;
1416 d = BUS_CONNECTION_DATA (connection);
1417 _dbus_assert (d != NULL);
1419 _dbus_list_append_link (&d->match_rules, link);
1421 d->n_match_rules += 1;
1423 #ifdef DBUS_ENABLE_STATS
1424 update_peak (&d->peak_match_rules, d->n_match_rules);
1425 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1427 d->connections->total_match_rules += 1;
1428 update_peak (&d->connections->peak_match_rules,
1429 d->connections->total_match_rules);
1434 bus_connection_add_match_rule (DBusConnection *connection,
1439 link = _dbus_list_alloc_link (rule);
1444 bus_connection_add_match_rule_link (connection, link);
1450 bus_connection_remove_match_rule (DBusConnection *connection,
1453 BusConnectionData *d;
1455 d = BUS_CONNECTION_DATA (connection);
1456 _dbus_assert (d != NULL);
1458 _dbus_list_remove_last (&d->match_rules, rule);
1460 d->n_match_rules -= 1;
1461 _dbus_assert (d->n_match_rules >= 0);
1463 #ifdef DBUS_ENABLE_STATS
1464 d->connections->total_match_rules -= 1;
1469 bus_connection_get_n_match_rules (DBusConnection *connection)
1471 BusConnectionData *d;
1473 d = BUS_CONNECTION_DATA (connection);
1474 _dbus_assert (d != NULL);
1476 return d->n_match_rules;
1480 bus_connection_add_owned_service_link (DBusConnection *connection,
1483 BusConnectionData *d;
1485 d = BUS_CONNECTION_DATA (connection);
1486 _dbus_assert (d != NULL);
1488 _dbus_list_append_link (&d->services_owned, link);
1490 d->n_services_owned += 1;
1492 #ifdef DBUS_ENABLE_STATS
1493 update_peak (&d->peak_bus_names, d->n_services_owned);
1494 update_peak (&d->connections->peak_bus_names_per_conn,
1495 d->n_services_owned);
1497 d->connections->total_bus_names += 1;
1498 update_peak (&d->connections->peak_bus_names,
1499 d->connections->total_bus_names);
1504 bus_connection_add_owned_service (DBusConnection *connection,
1505 BusService *service)
1509 link = _dbus_list_alloc_link (service);
1514 bus_connection_add_owned_service_link (connection, link);
1520 bus_connection_remove_owned_service (DBusConnection *connection,
1521 BusService *service)
1523 BusConnectionData *d;
1525 d = BUS_CONNECTION_DATA (connection);
1526 _dbus_assert (d != NULL);
1528 _dbus_list_remove_last (&d->services_owned, service);
1530 d->n_services_owned -= 1;
1531 _dbus_assert (d->n_services_owned >= 0);
1533 #ifdef DBUS_ENABLE_STATS
1534 d->connections->total_bus_names -= 1;
1539 bus_connection_get_n_services_owned (DBusConnection *connection)
1541 BusConnectionData *d;
1543 d = BUS_CONNECTION_DATA (connection);
1544 _dbus_assert (d != NULL);
1546 return d->n_services_owned;
1550 bus_connection_complete (DBusConnection *connection,
1551 const DBusString *name,
1554 BusConnectionData *d;
1557 d = BUS_CONNECTION_DATA (connection);
1558 _dbus_assert (d != NULL);
1559 _dbus_assert (d->name == NULL);
1560 _dbus_assert (d->policy == NULL);
1562 _dbus_assert (!bus_connection_is_active (connection));
1564 if (!_dbus_string_copy_data (name, &d->name))
1566 BUS_SET_OOM (error);
1570 _dbus_assert (d->name != NULL);
1572 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1574 d->policy = bus_context_create_client_policy (d->connections->context,
1578 /* we may have a NULL policy on OOM or error getting list of
1579 * groups for a user. In the latter case we don't handle it so
1580 * well currently, as it will just keep failing over and over.
1583 if (d->policy == NULL)
1585 _dbus_verbose ("Failed to create security policy for connection %p\n",
1587 _DBUS_ASSERT_ERROR_IS_SET (error);
1588 dbus_free (d->name);
1593 if (dbus_connection_get_unix_user (connection, &uid))
1595 if (!adjust_connections_for_uid (d->connections,
1600 /* Create and cache a string which holds information about the
1601 * peer process; used for logging purposes.
1603 if (!cache_peer_loginfo_string (d, connection))
1606 /* Now the connection is active, move it between lists */
1607 _dbus_list_unlink (&d->connections->incomplete,
1608 d->link_in_connection_list);
1609 d->connections->n_incomplete -= 1;
1610 _dbus_list_append_link (&d->connections->completed,
1611 d->link_in_connection_list);
1612 d->connections->n_completed += 1;
1614 _dbus_assert (d->connections->n_incomplete >= 0);
1615 _dbus_assert (d->connections->n_completed > 0);
1617 /* If we have dropped below the max. number of incomplete
1618 * connections, start accept()ing again */
1619 bus_context_check_all_watches (d->connections->context);
1621 /* See if we can remove the timeout */
1622 bus_connections_expire_incomplete (d->connections);
1624 _dbus_assert (bus_connection_is_active (connection));
1628 BUS_SET_OOM (error);
1629 dbus_free (d->name);
1632 bus_client_policy_unref (d->policy);
1638 bus_connections_reload_policy (BusConnections *connections,
1641 BusConnectionData *d;
1642 DBusConnection *connection;
1645 _dbus_assert (connections != NULL);
1646 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1648 for (link = _dbus_list_get_first_link (&(connections->completed));
1650 link = _dbus_list_get_next_link (&(connections->completed), link))
1652 connection = link->data;
1653 d = BUS_CONNECTION_DATA (connection);
1654 _dbus_assert (d != NULL);
1655 _dbus_assert (d->policy != NULL);
1657 bus_client_policy_unref (d->policy);
1658 d->policy = bus_context_create_client_policy (connections->context,
1661 if (d->policy == NULL)
1663 _dbus_verbose ("Failed to create security policy for connection %p\n",
1665 _DBUS_ASSERT_ERROR_IS_SET (error);
1674 bus_connection_get_name (DBusConnection *connection)
1676 BusConnectionData *d;
1678 d = BUS_CONNECTION_DATA (connection);
1679 _dbus_assert (d != NULL);
1685 * Check whether completing the passed-in connection would
1686 * exceed limits, and if so set error and return #FALSE
1689 bus_connections_check_limits (BusConnections *connections,
1690 DBusConnection *requesting_completion,
1691 const char **limit_name_out,
1698 limit = bus_context_get_max_completed_connections (connections->context);
1700 if (connections->n_completed >= limit)
1702 if (limit_name_out != NULL)
1703 *limit_name_out = "max_completed_connections";
1705 if (limit_out != NULL)
1708 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1709 "The maximum number of active connections has been reached");
1713 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1715 limit = bus_context_get_max_connections_per_user (connections->context);
1717 if (get_connections_for_uid (connections, uid) >= limit)
1719 if (limit_name_out != NULL)
1720 *limit_name_out = "max_connections_per_user";
1722 if (limit_out != NULL)
1725 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1726 "The maximum number of active connections for UID %lu has been reached",
1736 bus_pending_reply_free (BusPendingReply *pending)
1738 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1740 pending->will_send_reply,
1741 pending->will_get_reply,
1742 pending->reply_serial);
1744 dbus_free (pending);
1748 bus_pending_reply_send_no_reply (BusConnections *connections,
1749 BusTransaction *transaction,
1750 BusPendingReply *pending)
1752 DBusMessage *message;
1753 DBusMessageIter iter;
1759 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1760 if (message == NULL)
1763 dbus_message_set_no_reply (message, TRUE);
1765 if (!dbus_message_set_reply_serial (message,
1766 pending->reply_serial))
1769 if (!dbus_message_set_error_name (message,
1770 DBUS_ERROR_NO_REPLY))
1773 /* If you change these messages, adjust test/dbus-daemon.c to match */
1774 if (pending->will_send_reply == NULL)
1775 errmsg = "Message recipient disconnected from message bus without replying";
1777 errmsg = "Message did not receive a reply (timeout by message bus)";
1779 dbus_message_iter_init_append (message, &iter);
1780 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1783 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1790 dbus_message_unref (message);
1795 bus_pending_reply_expired (BusExpireList *list,
1799 BusPendingReply *pending = link->data;
1800 BusConnections *connections = data;
1801 BusTransaction *transaction;
1803 /* No reply is forthcoming. So nuke it if we can. If not,
1804 * leave it in the list to try expiring again later when we
1808 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1810 pending->will_send_reply,
1811 pending->will_get_reply,
1812 pending->reply_serial);
1814 transaction = bus_transaction_new (connections->context);
1815 if (transaction == NULL)
1818 if (!bus_pending_reply_send_no_reply (connections,
1822 bus_transaction_cancel_and_free (transaction);
1826 bus_expire_list_remove_link (connections->pending_replies, link);
1828 bus_pending_reply_free (pending);
1829 bus_transaction_execute_and_free (transaction);
1835 bus_connection_drop_pending_replies (BusConnections *connections,
1836 DBusConnection *connection)
1838 /* The DBusConnection is almost 100% finalized here, so you can't
1839 * do anything with it except check for pointer equality
1843 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1846 link = bus_expire_list_get_first_link (connections->pending_replies);
1847 while (link != NULL)
1850 BusPendingReply *pending;
1852 next = bus_expire_list_get_next_link (connections->pending_replies,
1854 pending = link->data;
1856 if (pending->will_get_reply == connection)
1858 /* We don't need to track this pending reply anymore */
1860 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1862 pending->will_send_reply,
1863 pending->will_get_reply,
1864 pending->reply_serial);
1866 bus_expire_list_remove_link (connections->pending_replies,
1868 bus_pending_reply_free (pending);
1870 else if (pending->will_send_reply == connection)
1872 /* The reply isn't going to be sent, so set things
1873 * up so it will be expired right away
1875 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1877 pending->will_send_reply,
1878 pending->will_get_reply,
1879 pending->reply_serial);
1881 pending->will_send_reply = NULL;
1882 pending->expire_item.added_tv_sec = 0;
1883 pending->expire_item.added_tv_usec = 0;
1885 bus_expire_list_recheck_immediately (connections->pending_replies);
1895 BusPendingReply *pending;
1896 BusConnections *connections;
1897 } CancelPendingReplyData;
1900 cancel_pending_reply (void *data)
1902 CancelPendingReplyData *d = data;
1904 _dbus_verbose ("d = %p\n", d);
1906 if (!bus_expire_list_remove (d->connections->pending_replies,
1907 &d->pending->expire_item))
1908 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1910 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1914 cancel_pending_reply_data_free (void *data)
1916 CancelPendingReplyData *d = data;
1918 _dbus_verbose ("d = %p\n", d);
1920 /* d->pending should be either freed or still
1921 * in the list of pending replies (owned by someone
1929 * Record that a reply is allowed; return TRUE on success.
1932 bus_connections_expect_reply (BusConnections *connections,
1933 BusTransaction *transaction,
1934 DBusConnection *will_get_reply,
1935 DBusConnection *will_send_reply,
1936 DBusMessage *reply_to_this,
1939 BusPendingReply *pending;
1940 dbus_uint32_t reply_serial;
1942 CancelPendingReplyData *cprd;
1946 _dbus_assert (will_get_reply != NULL);
1947 _dbus_assert (will_send_reply != NULL);
1948 _dbus_assert (reply_to_this != NULL);
1950 if (dbus_message_get_no_reply (reply_to_this))
1951 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1953 reply_serial = dbus_message_get_serial (reply_to_this);
1955 link = bus_expire_list_get_first_link (connections->pending_replies);
1957 while (link != NULL)
1959 pending = link->data;
1961 if (pending->reply_serial == reply_serial &&
1962 pending->will_get_reply == will_get_reply &&
1963 pending->will_send_reply == will_send_reply)
1965 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1966 "Message has the same reply serial as a currently-outstanding existing method call");
1970 link = bus_expire_list_get_next_link (connections->pending_replies,
1972 if (pending->will_get_reply == will_get_reply)
1976 limit = bus_context_get_max_replies_per_connection (connections->context);
1980 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
1981 "The maximum number of pending replies for "
1982 "\"%s\" (%s) has been reached "
1983 "(max_replies_per_connection=%d)",
1984 bus_connection_get_name (will_get_reply),
1985 bus_connection_get_loginfo (will_get_reply),
1988 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1989 "The maximum number of pending replies per connection has been reached");
1993 pending = dbus_new0 (BusPendingReply, 1);
1994 if (pending == NULL)
1996 BUS_SET_OOM (error);
2000 #ifdef DBUS_ENABLE_VERBOSE_MODE
2001 /* so we can see a not-yet-added pending reply */
2002 pending->expire_item.added_tv_sec = 1;
2003 pending->expire_item.added_tv_usec = 1;
2006 pending->will_get_reply = will_get_reply;
2007 pending->will_send_reply = will_send_reply;
2008 pending->reply_serial = reply_serial;
2010 cprd = dbus_new0 (CancelPendingReplyData, 1);
2013 BUS_SET_OOM (error);
2014 bus_pending_reply_free (pending);
2018 if (!bus_expire_list_add (connections->pending_replies,
2019 &pending->expire_item))
2021 BUS_SET_OOM (error);
2023 bus_pending_reply_free (pending);
2027 if (!bus_transaction_add_cancel_hook (transaction,
2028 cancel_pending_reply,
2030 cancel_pending_reply_data_free))
2032 BUS_SET_OOM (error);
2033 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
2035 bus_pending_reply_free (pending);
2039 cprd->pending = pending;
2040 cprd->connections = connections;
2042 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
2043 &pending->expire_item.added_tv_usec);
2045 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2047 pending->will_send_reply,
2048 pending->will_get_reply,
2049 pending->reply_serial);
2057 BusConnections *connections;
2058 } CheckPendingReplyData;
2061 cancel_check_pending_reply (void *data)
2063 CheckPendingReplyData *d = data;
2065 _dbus_verbose ("d = %p\n",d);
2067 bus_expire_list_add_link (d->connections->pending_replies,
2073 check_pending_reply_data_free (void *data)
2075 CheckPendingReplyData *d = data;
2077 _dbus_verbose ("d = %p\n",d);
2079 if (d->link != NULL)
2081 BusPendingReply *pending = d->link->data;
2083 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2084 &pending->expire_item));
2086 bus_pending_reply_free (pending);
2087 _dbus_list_free_link (d->link);
2094 * Check whether a reply is allowed, remove BusPendingReply
2095 * if so, return TRUE if so.
2098 bus_connections_check_reply (BusConnections *connections,
2099 BusTransaction *transaction,
2100 DBusConnection *sending_reply,
2101 DBusConnection *receiving_reply,
2105 CheckPendingReplyData *cprd;
2107 dbus_uint32_t reply_serial;
2109 _dbus_assert (sending_reply != NULL);
2110 _dbus_assert (receiving_reply != NULL);
2112 reply_serial = dbus_message_get_reply_serial (reply);
2114 link = bus_expire_list_get_first_link (connections->pending_replies);
2115 while (link != NULL)
2117 BusPendingReply *pending = link->data;
2119 if (pending->reply_serial == reply_serial &&
2120 pending->will_get_reply == receiving_reply &&
2121 pending->will_send_reply == sending_reply)
2123 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2127 link = bus_expire_list_get_next_link (connections->pending_replies,
2133 _dbus_verbose ("No pending reply expected\n");
2138 cprd = dbus_new0 (CheckPendingReplyData, 1);
2141 BUS_SET_OOM (error);
2145 if (!bus_transaction_add_cancel_hook (transaction,
2146 cancel_check_pending_reply,
2148 check_pending_reply_data_free))
2150 BUS_SET_OOM (error);
2156 cprd->connections = connections;
2158 bus_expire_list_unlink (connections->pending_replies,
2161 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2169 * Note that this is fairly fragile; in particular, don't try to use
2170 * one transaction across any main loop iterations.
2175 BusTransaction *transaction;
2176 DBusMessage *message;
2177 DBusPreallocatedSend *preallocated;
2182 BusTransactionCancelFunction cancel_function;
2183 DBusFreeFunction free_data_function;
2187 struct BusTransaction
2189 DBusList *connections;
2190 BusContext *context;
2191 DBusList *cancel_hooks;
2195 message_to_send_free (DBusConnection *connection,
2196 MessageToSend *to_send)
2198 if (to_send->message)
2199 dbus_message_unref (to_send->message);
2201 if (to_send->preallocated)
2202 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2204 dbus_free (to_send);
2208 cancel_hook_cancel (void *element,
2211 CancelHook *ch = element;
2213 _dbus_verbose ("Running transaction cancel hook\n");
2215 if (ch->cancel_function)
2216 (* ch->cancel_function) (ch->data);
2220 cancel_hook_free (void *element,
2223 CancelHook *ch = element;
2225 if (ch->free_data_function)
2226 (* ch->free_data_function) (ch->data);
2232 free_cancel_hooks (BusTransaction *transaction)
2234 _dbus_list_foreach (&transaction->cancel_hooks,
2235 cancel_hook_free, NULL);
2237 _dbus_list_clear (&transaction->cancel_hooks);
2241 bus_transaction_new (BusContext *context)
2243 BusTransaction *transaction;
2245 transaction = dbus_new0 (BusTransaction, 1);
2246 if (transaction == NULL)
2249 transaction->context = context;
2255 bus_transaction_get_context (BusTransaction *transaction)
2257 return transaction->context;
2261 * Reserve enough memory to capture the given message if the
2262 * transaction goes through.
2265 bus_transaction_capture (BusTransaction *transaction,
2266 DBusConnection *sender,
2267 DBusConnection *addressed_recipient,
2268 DBusMessage *message)
2270 BusConnections *connections;
2273 DBusList *recipients = NULL;
2274 dbus_bool_t ret = FALSE;
2276 connections = bus_context_get_connections (transaction->context);
2278 /* shortcut: don't compose the message unless someone wants it */
2279 if (connections->monitors == NULL)
2282 mm = connections->monitor_matchmaker;
2283 /* This is non-null if there has ever been a monitor - we don't GC it.
2284 * There's little point, since there is up to 1 per process. */
2285 _dbus_assert (mm != NULL);
2287 if (!bus_matchmaker_get_recipients (mm, connections, sender,
2288 addressed_recipient, message, &recipients))
2291 for (link = _dbus_list_get_first_link (&recipients);
2293 link = _dbus_list_get_next_link (&recipients, link))
2295 DBusConnection *recipient = link->data;
2297 if (!bus_transaction_send (transaction, recipient, message))
2304 _dbus_list_clear (&recipients);
2309 bus_transaction_capture_error_reply (BusTransaction *transaction,
2310 DBusConnection *addressed_recipient,
2311 const DBusError *error,
2312 DBusMessage *in_reply_to)
2314 BusConnections *connections;
2316 dbus_bool_t ret = FALSE;
2318 _dbus_assert (error != NULL);
2319 _DBUS_ASSERT_ERROR_IS_SET (error);
2321 connections = bus_context_get_connections (transaction->context);
2323 /* shortcut: don't compose the message unless someone wants it */
2324 if (connections->monitors == NULL)
2327 reply = dbus_message_new_error (in_reply_to,
2334 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2337 ret = bus_transaction_capture (transaction, NULL, addressed_recipient, reply);
2340 dbus_message_unref (reply);
2345 bus_transaction_send_from_driver (BusTransaction *transaction,
2346 DBusConnection *connection,
2347 DBusMessage *message)
2349 DBusError error = DBUS_ERROR_INIT;
2351 /* We have to set the sender to the driver, and have
2352 * to check security policy since it was not done in
2355 _dbus_verbose ("Sending %s %s %s from driver\n",
2356 dbus_message_get_interface (message) ?
2357 dbus_message_get_interface (message) : "(no interface)",
2358 dbus_message_get_member (message) ?
2359 dbus_message_get_member (message) : "(no member)",
2360 dbus_message_get_error_name (message) ?
2361 dbus_message_get_error_name (message) : "(no error name)");
2363 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2366 if (bus_connection_is_active (connection))
2368 if (!dbus_message_set_destination (message,
2369 bus_connection_get_name (connection)))
2373 /* bus driver never wants a reply */
2374 dbus_message_set_no_reply (message, TRUE);
2376 /* Capture it for monitors, even if the real recipient's receive policy
2377 * does not allow it to receive this message from us (which would be odd).
2379 if (!bus_transaction_capture (transaction, NULL, connection, message))
2382 /* If security policy doesn't allow the message, we would silently
2383 * eat it; the driver doesn't care about getting a reply. However,
2384 * if we're actively capturing messages, it's nice to log that we
2385 * tried to send it and did not allow ourselves to do so.
2387 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2389 NULL, connection, connection,
2390 message, NULL, &error))
2392 if (!bus_transaction_capture_error_reply (transaction, connection,
2395 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2396 "message from dbus-daemon rejected but not enough "
2397 "memory to capture it");
2400 /* This is not fatal to the transaction so silently eat the disallowed
2401 * message (see reasoning above) */
2402 dbus_error_free (&error);
2406 return bus_transaction_send (transaction, connection, message);
2410 bus_transaction_send (BusTransaction *transaction,
2411 DBusConnection *connection,
2412 DBusMessage *message)
2414 MessageToSend *to_send;
2415 BusConnectionData *d;
2418 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2419 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2420 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2422 dbus_message_get_interface (message) ?
2423 dbus_message_get_interface (message) : "(unset)",
2424 dbus_message_get_member (message) ?
2425 dbus_message_get_member (message) : "(unset)",
2426 dbus_message_get_error_name (message) ?
2427 dbus_message_get_error_name (message) : "(unset)",
2428 dbus_connection_get_is_connected (connection) ?
2429 "" : " (disconnected)");
2431 _dbus_assert (dbus_message_get_sender (message) != NULL);
2433 if (!dbus_connection_get_is_connected (connection))
2434 return TRUE; /* silently ignore disconnected connections */
2436 d = BUS_CONNECTION_DATA (connection);
2437 _dbus_assert (d != NULL);
2439 to_send = dbus_new (MessageToSend, 1);
2440 if (to_send == NULL)
2445 to_send->preallocated = dbus_connection_preallocate_send (connection);
2446 if (to_send->preallocated == NULL)
2448 dbus_free (to_send);
2452 dbus_message_ref (message);
2453 to_send->message = message;
2454 to_send->transaction = transaction;
2456 _dbus_verbose ("about to prepend message\n");
2458 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2460 message_to_send_free (connection, to_send);
2464 _dbus_verbose ("prepended message\n");
2466 /* See if we already had this connection in the list
2467 * for this transaction. If we have a pending message,
2468 * then we should already be in transaction->connections
2470 link = _dbus_list_get_first_link (&d->transaction_messages);
2471 _dbus_assert (link->data == to_send);
2472 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2473 while (link != NULL)
2475 MessageToSend *m = link->data;
2476 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2478 if (m->transaction == transaction)
2486 if (!_dbus_list_prepend (&transaction->connections, connection))
2488 _dbus_list_remove (&d->transaction_messages, to_send);
2489 message_to_send_free (connection, to_send);
2498 transaction_free (BusTransaction *transaction)
2500 _dbus_assert (transaction->connections == NULL);
2502 free_cancel_hooks (transaction);
2504 dbus_free (transaction);
2508 connection_cancel_transaction (DBusConnection *connection,
2509 BusTransaction *transaction)
2512 BusConnectionData *d;
2514 d = BUS_CONNECTION_DATA (connection);
2515 _dbus_assert (d != NULL);
2517 link = _dbus_list_get_first_link (&d->transaction_messages);
2518 while (link != NULL)
2520 MessageToSend *m = link->data;
2521 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2523 if (m->transaction == transaction)
2525 _dbus_list_remove_link (&d->transaction_messages,
2528 message_to_send_free (connection, m);
2536 bus_transaction_cancel_and_free (BusTransaction *transaction)
2538 DBusConnection *connection;
2540 _dbus_verbose ("TRANSACTION: cancelled\n");
2542 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2543 connection_cancel_transaction (connection, transaction);
2545 _dbus_list_foreach (&transaction->cancel_hooks,
2546 cancel_hook_cancel, NULL);
2548 transaction_free (transaction);
2552 connection_execute_transaction (DBusConnection *connection,
2553 BusTransaction *transaction)
2556 BusConnectionData *d;
2558 d = BUS_CONNECTION_DATA (connection);
2559 _dbus_assert (d != NULL);
2561 /* Send the queue in order (FIFO) */
2562 link = _dbus_list_get_last_link (&d->transaction_messages);
2563 while (link != NULL)
2565 MessageToSend *m = link->data;
2566 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2568 if (m->transaction == transaction)
2570 _dbus_list_remove_link (&d->transaction_messages,
2573 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2575 dbus_connection_send_preallocated (connection,
2580 m->preallocated = NULL; /* so we don't double-free it */
2582 message_to_send_free (connection, m);
2590 bus_transaction_execute_and_free (BusTransaction *transaction)
2592 /* For each connection in transaction->connections
2595 DBusConnection *connection;
2597 _dbus_verbose ("TRANSACTION: executing\n");
2599 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2600 connection_execute_transaction (connection, transaction);
2602 transaction_free (transaction);
2606 bus_connection_remove_transactions (DBusConnection *connection)
2608 MessageToSend *to_send;
2609 BusConnectionData *d;
2611 d = BUS_CONNECTION_DATA (connection);
2612 _dbus_assert (d != NULL);
2614 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2616 /* only has an effect for the first MessageToSend listing this transaction */
2617 _dbus_list_remove (&to_send->transaction->connections,
2620 _dbus_list_remove (&d->transaction_messages, to_send);
2621 message_to_send_free (connection, to_send);
2626 * Converts the DBusError to a message reply
2629 bus_transaction_send_error_reply (BusTransaction *transaction,
2630 DBusConnection *connection,
2631 const DBusError *error,
2632 DBusMessage *in_reply_to)
2636 _dbus_assert (error != NULL);
2637 _DBUS_ASSERT_ERROR_IS_SET (error);
2639 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2640 error->name, error->message);
2642 reply = dbus_message_new_error (in_reply_to,
2648 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2650 dbus_message_unref (reply);
2654 dbus_message_unref (reply);
2660 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2661 BusTransactionCancelFunction cancel_function,
2663 DBusFreeFunction free_data_function)
2667 ch = dbus_new (CancelHook, 1);
2671 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2672 cancel_function, data);
2674 ch->cancel_function = cancel_function;
2676 ch->free_data_function = free_data_function;
2678 /* It's important that the hooks get run in reverse order that they
2681 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2691 bus_connections_get_n_active (BusConnections *connections)
2693 return connections->n_completed;
2697 bus_connections_get_n_incomplete (BusConnections *connections)
2699 return connections->n_incomplete;
2702 #ifdef DBUS_ENABLE_STATS
2704 bus_connections_get_total_match_rules (BusConnections *connections)
2706 return connections->total_match_rules;
2710 bus_connections_get_peak_match_rules (BusConnections *connections)
2712 return connections->peak_match_rules;
2716 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2718 return connections->peak_match_rules_per_conn;
2722 bus_connections_get_total_bus_names (BusConnections *connections)
2724 return connections->total_bus_names;
2728 bus_connections_get_peak_bus_names (BusConnections *connections)
2730 return connections->peak_bus_names;
2734 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2736 return connections->peak_bus_names_per_conn;
2740 bus_connection_get_peak_match_rules (DBusConnection *connection)
2742 BusConnectionData *d;
2744 d = BUS_CONNECTION_DATA (connection);
2745 _dbus_assert(d != NULL);
2747 return d->peak_match_rules;
2751 bus_connection_get_peak_bus_names (DBusConnection *connection)
2753 BusConnectionData *d;
2755 d = BUS_CONNECTION_DATA (connection);
2756 _dbus_assert(d != NULL);
2758 return d->peak_bus_names;
2760 #endif /* DBUS_ENABLE_STATS */
2763 bus_connection_is_monitor (DBusConnection *connection)
2765 BusConnectionData *d;
2767 d = BUS_CONNECTION_DATA (connection);
2768 _dbus_assert(d != NULL);
2770 return d->link_in_monitors != NULL;
2774 bcd_add_monitor_rules (BusConnectionData *d,
2775 DBusConnection *connection,
2778 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2783 mm = bus_matchmaker_new ();
2788 d->connections->monitor_matchmaker = mm;
2791 for (iter = _dbus_list_get_first_link (rules);
2793 iter = _dbus_list_get_next_link (rules, iter))
2795 if (!bus_matchmaker_add_rule (mm, iter->data))
2797 bus_matchmaker_disconnected (mm, connection);
2806 bcd_drop_monitor_rules (BusConnectionData *d,
2807 DBusConnection *connection)
2809 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2812 bus_matchmaker_disconnected (mm, connection);
2816 bus_connection_be_monitor (DBusConnection *connection,
2817 BusTransaction *transaction,
2821 BusConnectionData *d;
2826 d = BUS_CONNECTION_DATA (connection);
2827 _dbus_assert (d != NULL);
2829 link = _dbus_list_alloc_link (connection);
2833 BUS_SET_OOM (error);
2837 if (!bcd_add_monitor_rules (d, connection, rules))
2839 _dbus_list_free_link (link);
2840 BUS_SET_OOM (error);
2844 /* release all its names */
2845 if (!_dbus_list_copy (&d->services_owned, &tmp))
2847 bcd_drop_monitor_rules (d, connection);
2848 _dbus_list_free_link (link);
2849 BUS_SET_OOM (error);
2853 for (iter = _dbus_list_get_first_link (&tmp);
2855 iter = _dbus_list_get_next_link (&tmp, iter))
2857 BusService *service = iter->data;
2859 /* This call is transactional: if there isn't enough memory to
2860 * do everything, then the service gets all its names back when
2861 * the transaction is cancelled due to OOM. */
2862 if (!bus_service_remove_owner (service, connection, transaction, error))
2864 bcd_drop_monitor_rules (d, connection);
2865 _dbus_list_free_link (link);
2866 _dbus_list_clear (&tmp);
2871 /* We have now done everything that can fail, so there is no problem
2872 * with doing the irrevocable stuff. */
2874 _dbus_list_clear (&tmp);
2876 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2877 "Connection %s (%s) became a monitor.", d->name,
2878 d->cached_loginfo_string);
2880 if (d->n_match_rules > 0)
2884 mm = bus_context_get_matchmaker (d->connections->context);
2885 bus_matchmaker_disconnected (mm, connection);
2888 /* flag it as a monitor */
2889 d->link_in_monitors = link;
2890 _dbus_list_append_link (&d->connections->monitors, link);
2892 /* it isn't allowed to reply, and it is no longer relevant whether it
2893 * receives replies */
2894 bus_connection_drop_pending_replies (d->connections, connection);