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>
39 #ifdef DBUS_ENABLE_CYNARA
41 #include <cynara-session.h>
44 /* Trim executed commands to this length; we want to keep logs readable */
45 #define MAX_LOG_COMMAND_LEN 50
47 static void bus_connection_remove_transactions (DBusConnection *connection);
51 BusExpireItem expire_item;
53 DBusConnection *will_get_reply;
54 DBusConnection *will_send_reply;
56 dbus_uint32_t reply_serial;
63 DBusList *completed; /**< List of all completed connections */
64 int n_completed; /**< Length of completed list */
65 DBusList *incomplete; /**< List of all not-yet-active connections */
66 int n_incomplete; /**< Length of incomplete list */
68 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
69 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
70 int stamp; /**< Incrementing number */
71 BusExpireList *pending_replies; /**< List of pending replies */
73 /** List of all monitoring connections, a subset of completed.
74 * Each member is a #DBusConnection. */
76 BusMatchmaker *monitor_matchmaker;
78 #ifdef DBUS_ENABLE_STATS
79 int total_match_rules;
81 int peak_match_rules_per_conn;
85 int peak_bus_names_per_conn;
89 static dbus_int32_t connection_data_slot = -1;
93 BusConnections *connections;
94 DBusList *link_in_connection_list;
95 DBusConnection *connection;
96 DBusList *services_owned;
98 DBusList *match_rules;
101 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
102 DBusMessage *oom_message;
103 DBusPreallocatedSend *oom_preallocated;
104 BusClientPolicy *policy;
106 char *cached_loginfo_string;
107 BusSELinuxID *selinux_id;
108 BusAppArmorConfinement *apparmor_confinement;
110 long connection_tv_sec; /**< Time when we connected (seconds component) */
111 long connection_tv_usec; /**< Time when we connected (microsec component) */
112 int stamp; /**< connections->stamp last time we were traversed */
114 #ifdef DBUS_ENABLE_STATS
115 int peak_match_rules;
118 int n_pending_unix_fds;
119 DBusTimeout *pending_unix_fds_timeout;
121 /** non-NULL if and only if this is a monitor */
122 DBusList *link_in_monitors;
123 #ifdef DBUS_ENABLE_CYNARA
124 char *cynara_session_id;
128 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
132 static void bus_connection_drop_pending_replies (BusConnections *connections,
133 DBusConnection *connection);
135 static dbus_bool_t expire_incomplete_timeout (void *data);
137 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
140 bus_connection_get_loop (DBusConnection *connection)
142 BusConnectionData *d;
144 d = BUS_CONNECTION_DATA (connection);
145 _dbus_assert(d != NULL);
147 return bus_context_get_loop (d->connections->context);
152 get_connections_for_uid (BusConnections *connections,
158 /* val is NULL is 0 when it isn't in the hash yet */
160 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
163 current_count = _DBUS_POINTER_TO_INT (val);
165 return current_count;
169 adjust_connections_for_uid (BusConnections *connections,
175 current_count = get_connections_for_uid (connections, uid);
177 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
178 ": was %d adjustment %d making %d\n",
179 uid, current_count, adjustment, current_count + adjustment);
181 _dbus_assert (current_count >= 0);
183 current_count += adjustment;
185 _dbus_assert (current_count >= 0);
187 if (current_count == 0)
189 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
196 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
197 uid, _DBUS_INT_TO_POINTER (current_count));
199 /* only positive adjustment can fail as otherwise
200 * a hash entry should already exist
202 _dbus_assert (adjustment > 0 ||
203 (adjustment <= 0 && retval));
210 bus_connection_disconnected (DBusConnection *connection)
212 BusConnectionData *d;
214 BusMatchmaker *matchmaker;
216 d = BUS_CONNECTION_DATA (connection);
217 _dbus_assert (d != NULL);
219 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
220 d->name ? d->name : "(inactive)");
222 /* Delete our match rules */
223 if (d->n_match_rules > 0)
225 matchmaker = bus_context_get_matchmaker (d->connections->context);
226 bus_matchmaker_disconnected (matchmaker, connection);
229 /* Drop any service ownership. Unfortunately, this requires
230 * memory allocation and there doesn't seem to be a good way to
231 * handle it other than sleeping; we can't "fail" the operation of
232 * disconnecting a client, and preallocating a broadcast "service is
233 * now gone" message for every client-service pair seems kind of
236 while ((service = _dbus_list_get_last (&d->services_owned)))
238 BusTransaction *transaction;
243 dbus_error_init (&error);
245 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
246 _dbus_wait_for_memory ();
248 if (!bus_service_remove_owner (service, connection,
249 transaction, &error))
251 _DBUS_ASSERT_ERROR_IS_SET (&error);
253 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
255 dbus_error_free (&error);
256 bus_transaction_cancel_and_free (transaction);
257 _dbus_wait_for_memory ();
262 _dbus_verbose ("Failed to remove service owner: %s %s\n",
263 error.name, error.message);
264 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
268 bus_transaction_execute_and_free (transaction);
271 bus_dispatch_remove_connection (connection);
273 /* no more watching */
274 if (!dbus_connection_set_watch_functions (connection,
278 _dbus_assert_not_reached ("setting watch functions to NULL failed");
280 if (!dbus_connection_set_timeout_functions (connection,
284 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
286 dbus_connection_set_unix_user_function (connection,
288 dbus_connection_set_windows_user_function (connection,
291 dbus_connection_set_dispatch_status_function (connection,
294 if (d->pending_unix_fds_timeout)
296 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
297 d->pending_unix_fds_timeout);
298 _dbus_timeout_unref (d->pending_unix_fds_timeout);
300 d->pending_unix_fds_timeout = NULL;
301 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
303 bus_connection_remove_transactions (connection);
305 if (d->link_in_monitors != NULL)
307 BusMatchmaker *mm = d->connections->monitor_matchmaker;
310 bus_matchmaker_disconnected (mm, connection);
312 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
313 d->link_in_monitors = NULL;
316 if (d->link_in_connection_list != NULL)
322 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
323 d->link_in_connection_list = NULL;
324 d->connections->n_completed -= 1;
326 if (dbus_connection_get_unix_user (connection, &uid))
328 if (!adjust_connections_for_uid (d->connections,
330 _dbus_assert_not_reached ("adjusting downward should never fail");
335 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
336 d->link_in_connection_list = NULL;
337 d->connections->n_incomplete -= 1;
339 /* If we have dropped below the max. number of incomplete
340 * connections, start accept()ing again */
341 bus_context_check_all_watches (d->connections->context);
344 _dbus_assert (d->connections->n_incomplete >= 0);
345 _dbus_assert (d->connections->n_completed >= 0);
348 bus_connection_drop_pending_replies (d->connections, connection);
350 /* frees "d" as side effect */
351 dbus_connection_set_data (connection,
352 connection_data_slot,
355 dbus_connection_unref (connection);
359 add_connection_watch (DBusWatch *watch,
362 DBusConnection *connection = data;
364 return _dbus_loop_add_watch (bus_connection_get_loop (connection), watch);
368 remove_connection_watch (DBusWatch *watch,
371 DBusConnection *connection = data;
373 _dbus_loop_remove_watch (bus_connection_get_loop (connection), watch);
377 toggle_connection_watch (DBusWatch *watch,
380 DBusConnection *connection = data;
382 _dbus_loop_toggle_watch (bus_connection_get_loop (connection), watch);
386 add_connection_timeout (DBusTimeout *timeout,
389 DBusConnection *connection = data;
391 return _dbus_loop_add_timeout (bus_connection_get_loop (connection), timeout);
395 remove_connection_timeout (DBusTimeout *timeout,
398 DBusConnection *connection = data;
400 _dbus_loop_remove_timeout (bus_connection_get_loop (connection), timeout);
404 dispatch_status_function (DBusConnection *connection,
405 DBusDispatchStatus new_status,
408 DBusLoop *loop = data;
410 if (new_status != DBUS_DISPATCH_COMPLETE)
412 while (!_dbus_loop_queue_dispatch (loop, connection))
413 _dbus_wait_for_memory ();
418 allow_unix_user_function (DBusConnection *connection,
422 BusConnectionData *d;
424 d = BUS_CONNECTION_DATA (connection);
426 _dbus_assert (d != NULL);
428 return bus_context_allow_unix_user (d->connections->context, uid);
432 free_connection_data (void *data)
434 BusConnectionData *d = data;
436 /* services_owned should be NULL since we should be disconnected */
437 _dbus_assert (d->services_owned == NULL);
438 _dbus_assert (d->n_services_owned == 0);
440 _dbus_assert (d->transaction_messages == NULL);
442 if (d->oom_preallocated)
443 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
446 dbus_message_unref (d->oom_message);
449 bus_client_policy_unref (d->policy);
452 bus_selinux_id_unref (d->selinux_id);
454 if (d->apparmor_confinement)
455 bus_apparmor_confinement_unref (d->apparmor_confinement);
457 dbus_free (d->cached_loginfo_string);
461 #ifdef DBUS_ENABLE_CYNARA
462 free (d->cynara_session_id);
469 bus_connections_new (BusContext *context)
471 BusConnections *connections;
473 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
476 connections = dbus_new0 (BusConnections, 1);
477 if (connections == NULL)
480 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
482 if (connections->completed_by_user == NULL)
485 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
486 expire_incomplete_timeout,
488 if (connections->expire_timeout == NULL)
491 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
493 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
494 bus_context_get_reply_timeout (context),
495 bus_pending_reply_expired,
497 if (connections->pending_replies == NULL)
500 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
501 connections->expire_timeout))
504 connections->refcount = 1;
505 connections->context = context;
510 bus_expire_list_free (connections->pending_replies);
512 _dbus_timeout_unref (connections->expire_timeout);
514 _dbus_hash_table_unref (connections->completed_by_user);
516 dbus_free (connections);
518 dbus_connection_free_data_slot (&connection_data_slot);
524 bus_connections_ref (BusConnections *connections)
526 _dbus_assert (connections->refcount > 0);
527 connections->refcount += 1;
533 bus_connections_unref (BusConnections *connections)
535 _dbus_assert (connections->refcount > 0);
536 connections->refcount -= 1;
537 if (connections->refcount == 0)
539 /* drop all incomplete */
540 while (connections->incomplete != NULL)
542 DBusConnection *connection;
544 connection = connections->incomplete->data;
546 dbus_connection_ref (connection);
547 dbus_connection_close (connection);
548 bus_connection_disconnected (connection);
549 dbus_connection_unref (connection);
552 _dbus_assert (connections->n_incomplete == 0);
554 /* drop all monitors */
555 _dbus_list_clear (&connections->monitors);
557 /* drop all real connections */
558 while (connections->completed != NULL)
560 DBusConnection *connection;
562 connection = connections->completed->data;
564 dbus_connection_ref (connection);
565 dbus_connection_close (connection);
566 bus_connection_disconnected (connection);
567 dbus_connection_unref (connection);
570 _dbus_assert (connections->n_completed == 0);
572 bus_expire_list_free (connections->pending_replies);
574 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
575 connections->expire_timeout);
577 _dbus_timeout_unref (connections->expire_timeout);
579 _dbus_hash_table_unref (connections->completed_by_user);
581 if (connections->monitor_matchmaker != NULL)
582 bus_matchmaker_unref (connections->monitor_matchmaker);
584 dbus_free (connections);
586 dbus_connection_free_data_slot (&connection_data_slot);
590 /* Used for logging */
592 cache_peer_loginfo_string (BusConnectionData *d,
593 DBusConnection *connection)
595 DBusString loginfo_buf;
599 dbus_bool_t prev_added;
601 if (!_dbus_string_init (&loginfo_buf))
605 if (dbus_connection_get_unix_user (connection, &uid))
607 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
613 if (dbus_connection_get_unix_process_id (connection, &pid))
617 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
620 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
622 /* Ignore errors here; we may not have permissions to read the
624 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
625 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
629 if (dbus_connection_get_windows_user (connection, &windows_sid))
631 dbus_bool_t did_append;
632 did_append = _dbus_string_append_printf (&loginfo_buf,
633 "sid=\"%s\" ", windows_sid);
634 dbus_free (windows_sid);
639 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
642 _dbus_string_free (&loginfo_buf);
646 _dbus_string_free (&loginfo_buf);
651 check_pending_fds_cb (DBusConnection *connection)
653 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
654 int n_pending_unix_fds_old;
655 int n_pending_unix_fds_new;
657 _dbus_assert(d != NULL);
659 n_pending_unix_fds_old = d->n_pending_unix_fds;
660 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
662 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
663 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
665 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
667 _dbus_timeout_set_interval (d->pending_unix_fds_timeout,
668 bus_context_get_pending_fd_timeout (d->connections->context));
669 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, TRUE);
672 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
674 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
678 d->n_pending_unix_fds = n_pending_unix_fds_new;
682 pending_unix_fds_timeout_cb (void *data)
684 DBusConnection *connection = data;
685 dbus_connection_close (connection);
690 bus_connections_setup_connection (BusConnections *connections,
691 DBusConnection *connection)
694 BusConnectionData *d;
699 d = dbus_new0 (BusConnectionData, 1);
704 d->connections = connections;
705 d->connection = connection;
707 _dbus_get_monotonic_time (&d->connection_tv_sec,
708 &d->connection_tv_usec);
710 _dbus_assert (connection_data_slot >= 0);
712 if (!dbus_connection_set_data (connection,
713 connection_data_slot,
714 d, free_connection_data))
720 dbus_connection_set_route_peer_messages (connection, TRUE);
724 dbus_error_init (&error);
725 d->selinux_id = bus_selinux_init_connection_id (connection,
727 if (dbus_error_is_set (&error))
729 /* This is a bit bogus because we pretend all errors
730 * are OOM; this is done because we know that in bus.c
731 * an OOM error disconnects the connection, which is
732 * the same thing we want on any other error.
734 dbus_error_free (&error);
738 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
740 if (dbus_error_is_set (&error))
742 /* This is a bit bogus because we pretend all errors
743 * are OOM; this is done because we know that in bus.c
744 * an OOM error disconnects the connection, which is
745 * the same thing we want on any other error.
747 dbus_error_free (&error);
751 if (!dbus_connection_set_watch_functions (connection,
752 add_connection_watch,
753 remove_connection_watch,
754 toggle_connection_watch,
759 if (!dbus_connection_set_timeout_functions (connection,
760 add_connection_timeout,
761 remove_connection_timeout,
766 /* For now we don't need to set a Windows user function because
767 * there are no policies in the config file controlling what
768 * Windows users can connect. The default 'same user that owns the
769 * bus can connect' behavior of DBusConnection is fine on Windows.
771 dbus_connection_set_unix_user_function (connection,
772 allow_unix_user_function,
775 dbus_connection_set_dispatch_status_function (connection,
776 dispatch_status_function,
777 bus_context_get_loop (connections->context),
780 d->link_in_connection_list = _dbus_list_alloc_link (connection);
781 if (d->link_in_connection_list == NULL)
784 /* Setup the connection with the dispatcher */
785 if (!bus_dispatch_add_connection (connection))
788 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
790 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
792 bus_dispatch_remove_connection (connection);
797 /* Setup pending fds timeout (see #80559) */
798 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
799 pending_unix_fds_timeout_cb,
801 if (d->pending_unix_fds_timeout == NULL)
804 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
805 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
806 d->pending_unix_fds_timeout))
809 _dbus_connection_set_pending_fds_function (connection,
810 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
813 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
814 connections->n_incomplete += 1;
816 dbus_connection_ref (connection);
818 bus_connections_expire_incomplete (connections);
820 /* The listening socket is removed from the main loop,
821 * i.e. does not accept(), while n_incomplete is at its
822 * maximum value; so we shouldn't get here in that case */
823 _dbus_assert (connections->n_incomplete <=
824 bus_context_get_max_incomplete_connections (connections->context));
826 /* If we have the maximum number of incomplete connections,
827 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
828 bus_context_check_all_watches (d->connections->context);
836 bus_selinux_id_unref (d->selinux_id);
837 d->selinux_id = NULL;
839 if (d->apparmor_confinement)
840 bus_apparmor_confinement_unref (d->apparmor_confinement);
841 d->apparmor_confinement = NULL;
843 if (!dbus_connection_set_watch_functions (connection,
847 _dbus_assert_not_reached ("setting watch functions to NULL failed");
849 if (!dbus_connection_set_timeout_functions (connection,
853 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
855 dbus_connection_set_unix_user_function (connection,
858 dbus_connection_set_windows_user_function (connection,
861 dbus_connection_set_dispatch_status_function (connection,
864 if (d->pending_unix_fds_timeout)
865 _dbus_timeout_unref (d->pending_unix_fds_timeout);
867 d->pending_unix_fds_timeout = NULL;
869 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
871 if (d->link_in_connection_list != NULL)
873 _dbus_assert (d->link_in_connection_list->next == NULL);
874 _dbus_assert (d->link_in_connection_list->prev == NULL);
875 _dbus_list_free_link (d->link_in_connection_list);
876 d->link_in_connection_list = NULL;
879 if (!dbus_connection_set_data (connection,
880 connection_data_slot,
882 _dbus_assert_not_reached ("failed to set connection data to null");
884 /* "d" has now been freed */
891 bus_connections_expire_incomplete (BusConnections *connections)
897 if (connections->incomplete != NULL)
899 long tv_sec, tv_usec;
903 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
904 auth_timeout = bus_context_get_auth_timeout (connections->context);
906 link = _dbus_list_get_first_link (&connections->incomplete);
909 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
910 DBusConnection *connection;
911 BusConnectionData *d;
914 connection = link->data;
916 d = BUS_CONNECTION_DATA (connection);
918 _dbus_assert (d != NULL);
920 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
921 d->connection_tv_usec,
924 if (elapsed >= (double) auth_timeout)
926 /* Unfortunately, we can't identify the connection: it doesn't
927 * have a unique name yet, we don't know its uid/pid yet,
929 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
930 "Connection has not authenticated soon enough, closing it "
931 "(auth_timeout=%dms, elapsed: %.0fms)",
932 auth_timeout, elapsed);
934 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
935 dbus_connection_close (connection);
939 /* We can end the loop, since the connections are in oldest-first order */
940 next_interval = ((double)auth_timeout) - elapsed;
941 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
942 connection, next_interval);
951 bus_expire_timeout_set_interval (connections->expire_timeout,
956 expire_incomplete_timeout (void *data)
958 BusConnections *connections = data;
960 _dbus_verbose ("Running\n");
962 /* note that this may remove the timeout */
963 bus_connections_expire_incomplete (connections);
969 bus_connection_get_unix_groups (DBusConnection *connection,
970 unsigned long **groups,
979 if (dbus_connection_get_unix_user (connection, &uid))
981 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
983 _dbus_verbose ("Did not get any groups for UID %lu\n",
989 _dbus_verbose ("Got %d groups for UID %lu\n",
995 return TRUE; /* successfully got 0 groups */
999 bus_connection_is_in_unix_group (DBusConnection *connection,
1003 unsigned long *group_ids;
1006 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1011 while (i < n_group_ids)
1013 if (group_ids[i] == gid)
1015 dbus_free (group_ids);
1021 dbus_free (group_ids);
1026 bus_connection_get_loginfo (DBusConnection *connection)
1028 BusConnectionData *d;
1030 d = BUS_CONNECTION_DATA (connection);
1031 _dbus_assert(d != NULL);
1033 if (!bus_connection_is_active (connection))
1035 return d->cached_loginfo_string;
1039 bus_connection_get_policy (DBusConnection *connection)
1041 BusConnectionData *d;
1043 d = BUS_CONNECTION_DATA (connection);
1045 _dbus_assert (d != NULL);
1046 _dbus_assert (d->policy != NULL);
1051 #ifdef DBUS_ENABLE_CYNARA
1052 const char *bus_connection_get_cynara_session_id (DBusConnection *connection)
1054 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
1055 _dbus_assert (d != NULL);
1057 if (d->cynara_session_id == NULL)
1060 if (dbus_connection_get_unix_process_id(connection, &pid))
1061 d->cynara_session_id = cynara_session_from_pid(pid);
1063 return d->cynara_session_id;
1068 foreach_active (BusConnections *connections,
1069 BusConnectionForeachFunction function,
1074 link = _dbus_list_get_first_link (&connections->completed);
1075 while (link != NULL)
1077 DBusConnection *connection = link->data;
1078 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1080 if (!(* function) (connection, data))
1090 foreach_inactive (BusConnections *connections,
1091 BusConnectionForeachFunction function,
1096 link = _dbus_list_get_first_link (&connections->incomplete);
1097 while (link != NULL)
1099 DBusConnection *connection = link->data;
1100 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1102 if (!(* function) (connection, data))
1112 * Calls function on each active connection; if the function returns
1113 * #FALSE, stops iterating. Active connections are authenticated
1114 * and have sent a Hello message.
1116 * @param connections the connections object
1117 * @param function the function
1118 * @param data data to pass to it as a second arg
1121 bus_connections_foreach_active (BusConnections *connections,
1122 BusConnectionForeachFunction function,
1125 foreach_active (connections, function, data);
1129 * Calls function on each connection; if the function returns
1130 * #FALSE, stops iterating.
1132 * @param connections the connections object
1133 * @param function the function
1134 * @param data data to pass to it as a second arg
1137 bus_connections_foreach (BusConnections *connections,
1138 BusConnectionForeachFunction function,
1141 if (!foreach_active (connections, function, data))
1144 foreach_inactive (connections, function, data);
1148 bus_connections_get_context (BusConnections *connections)
1150 return connections->context;
1154 * This is used to avoid covering the same connection twice when
1155 * traversing connections. Note that it assumes we will
1156 * bus_connection_mark_stamp() each connection at least once per
1157 * INT_MAX increments of the global stamp, or wraparound would break
1161 bus_connections_increment_stamp (BusConnections *connections)
1163 connections->stamp += 1;
1166 /* Mark connection with current stamp, return TRUE if it
1167 * didn't already have that stamp
1170 bus_connection_mark_stamp (DBusConnection *connection)
1172 BusConnectionData *d;
1174 d = BUS_CONNECTION_DATA (connection);
1176 _dbus_assert (d != NULL);
1178 if (d->stamp == d->connections->stamp)
1182 d->stamp = d->connections->stamp;
1188 bus_connection_get_context (DBusConnection *connection)
1190 BusConnectionData *d;
1192 d = BUS_CONNECTION_DATA (connection);
1194 _dbus_assert (d != NULL);
1196 return d->connections->context;
1200 bus_connection_get_connections (DBusConnection *connection)
1202 BusConnectionData *d;
1204 d = BUS_CONNECTION_DATA (connection);
1206 _dbus_assert (d != NULL);
1208 return d->connections;
1212 bus_connection_get_registry (DBusConnection *connection)
1214 BusConnectionData *d;
1216 d = BUS_CONNECTION_DATA (connection);
1218 _dbus_assert (d != NULL);
1220 return bus_context_get_registry (d->connections->context);
1224 bus_connection_get_activation (DBusConnection *connection)
1226 BusConnectionData *d;
1228 d = BUS_CONNECTION_DATA (connection);
1230 _dbus_assert (d != NULL);
1232 return bus_context_get_activation (d->connections->context);
1236 bus_connection_get_matchmaker (DBusConnection *connection)
1238 BusConnectionData *d;
1240 d = BUS_CONNECTION_DATA (connection);
1242 _dbus_assert (d != NULL);
1244 return bus_context_get_matchmaker (d->connections->context);
1248 bus_connection_get_selinux_id (DBusConnection *connection)
1250 BusConnectionData *d;
1252 d = BUS_CONNECTION_DATA (connection);
1254 _dbus_assert (d != NULL);
1256 return d->selinux_id;
1259 BusAppArmorConfinement*
1260 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1262 BusConnectionData *d;
1264 d = BUS_CONNECTION_DATA (connection);
1266 _dbus_assert (d != NULL);
1268 bus_apparmor_confinement_ref (d->apparmor_confinement);
1269 return d->apparmor_confinement;
1273 * Checks whether the connection is registered with the message bus.
1275 * @param connection the connection
1276 * @returns #TRUE if we're an active message bus participant
1279 bus_connection_is_active (DBusConnection *connection)
1281 BusConnectionData *d;
1283 d = BUS_CONNECTION_DATA (connection);
1284 _dbus_assert(d != NULL);
1286 return d->name != NULL;
1290 bus_connection_preallocate_oom_error (DBusConnection *connection)
1292 DBusMessage *message;
1293 DBusPreallocatedSend *preallocated;
1294 BusConnectionData *d;
1296 d = BUS_CONNECTION_DATA (connection);
1298 _dbus_assert (d != NULL);
1300 if (d->oom_preallocated != NULL)
1303 preallocated = dbus_connection_preallocate_send (connection);
1304 if (preallocated == NULL)
1307 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1309 if (message == NULL)
1311 dbus_connection_free_preallocated_send (connection, preallocated);
1315 /* d->name may be NULL, but that is OK */
1316 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1317 !dbus_message_set_destination (message, d->name) ||
1318 !dbus_message_set_sender (message,
1321 dbus_connection_free_preallocated_send (connection, preallocated);
1322 dbus_message_unref (message);
1326 /* set reply serial to placeholder value just so space is already allocated
1329 if (!dbus_message_set_reply_serial (message, 14))
1331 dbus_connection_free_preallocated_send (connection, preallocated);
1332 dbus_message_unref (message);
1336 d->oom_message = message;
1337 d->oom_preallocated = preallocated;
1343 bus_connection_send_oom_error (DBusConnection *connection,
1344 DBusMessage *in_reply_to)
1346 BusConnectionData *d;
1348 d = BUS_CONNECTION_DATA (connection);
1350 _dbus_assert (d != NULL);
1351 _dbus_assert (d->oom_message != NULL);
1353 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1354 "dbus-daemon transaction failed (OOM), sending error to "
1355 "sender %s", bus_connection_get_loginfo (connection));
1357 /* should always succeed since we set it to a placeholder earlier */
1358 if (!dbus_message_set_reply_serial (d->oom_message,
1359 dbus_message_get_serial (in_reply_to)))
1360 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1362 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1364 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1365 d->oom_message, NULL);
1367 dbus_message_unref (d->oom_message);
1368 d->oom_message = NULL;
1369 d->oom_preallocated = NULL;
1372 #ifdef DBUS_ENABLE_STATS
1374 update_peak (int *peak,
1383 bus_connection_add_match_rule_link (DBusConnection *connection,
1386 BusConnectionData *d;
1388 d = BUS_CONNECTION_DATA (connection);
1389 _dbus_assert (d != NULL);
1391 _dbus_list_append_link (&d->match_rules, link);
1393 d->n_match_rules += 1;
1395 #ifdef DBUS_ENABLE_STATS
1396 update_peak (&d->peak_match_rules, d->n_match_rules);
1397 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1399 d->connections->total_match_rules += 1;
1400 update_peak (&d->connections->peak_match_rules,
1401 d->connections->total_match_rules);
1406 bus_connection_add_match_rule (DBusConnection *connection,
1411 link = _dbus_list_alloc_link (rule);
1416 bus_connection_add_match_rule_link (connection, link);
1422 bus_connection_remove_match_rule (DBusConnection *connection,
1425 BusConnectionData *d;
1427 d = BUS_CONNECTION_DATA (connection);
1428 _dbus_assert (d != NULL);
1430 _dbus_list_remove_last (&d->match_rules, rule);
1432 d->n_match_rules -= 1;
1433 _dbus_assert (d->n_match_rules >= 0);
1435 #ifdef DBUS_ENABLE_STATS
1436 d->connections->total_match_rules -= 1;
1441 bus_connection_get_n_match_rules (DBusConnection *connection)
1443 BusConnectionData *d;
1445 d = BUS_CONNECTION_DATA (connection);
1446 _dbus_assert (d != NULL);
1448 return d->n_match_rules;
1452 bus_connection_add_owned_service_link (DBusConnection *connection,
1455 BusConnectionData *d;
1457 d = BUS_CONNECTION_DATA (connection);
1458 _dbus_assert (d != NULL);
1460 _dbus_list_append_link (&d->services_owned, link);
1462 d->n_services_owned += 1;
1464 #ifdef DBUS_ENABLE_STATS
1465 update_peak (&d->peak_bus_names, d->n_services_owned);
1466 update_peak (&d->connections->peak_bus_names_per_conn,
1467 d->n_services_owned);
1469 d->connections->total_bus_names += 1;
1470 update_peak (&d->connections->peak_bus_names,
1471 d->connections->total_bus_names);
1476 bus_connection_add_owned_service (DBusConnection *connection,
1477 BusService *service)
1481 link = _dbus_list_alloc_link (service);
1486 bus_connection_add_owned_service_link (connection, link);
1492 bus_connection_remove_owned_service (DBusConnection *connection,
1493 BusService *service)
1495 BusConnectionData *d;
1497 d = BUS_CONNECTION_DATA (connection);
1498 _dbus_assert (d != NULL);
1500 _dbus_list_remove_last (&d->services_owned, service);
1502 d->n_services_owned -= 1;
1503 _dbus_assert (d->n_services_owned >= 0);
1505 #ifdef DBUS_ENABLE_STATS
1506 d->connections->total_bus_names -= 1;
1511 bus_connection_get_n_services_owned (DBusConnection *connection)
1513 BusConnectionData *d;
1515 d = BUS_CONNECTION_DATA (connection);
1516 _dbus_assert (d != NULL);
1518 return d->n_services_owned;
1522 bus_connection_complete (DBusConnection *connection,
1523 const DBusString *name,
1526 BusConnectionData *d;
1529 d = BUS_CONNECTION_DATA (connection);
1530 _dbus_assert (d != NULL);
1531 _dbus_assert (d->name == NULL);
1532 _dbus_assert (d->policy == NULL);
1534 _dbus_assert (!bus_connection_is_active (connection));
1536 if (!_dbus_string_copy_data (name, &d->name))
1538 BUS_SET_OOM (error);
1542 _dbus_assert (d->name != NULL);
1544 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1546 d->policy = bus_context_create_client_policy (d->connections->context,
1550 /* we may have a NULL policy on OOM or error getting list of
1551 * groups for a user. In the latter case we don't handle it so
1552 * well currently, as it will just keep failing over and over.
1555 if (d->policy == NULL)
1557 _dbus_verbose ("Failed to create security policy for connection %p\n",
1559 _DBUS_ASSERT_ERROR_IS_SET (error);
1560 dbus_free (d->name);
1565 if (dbus_connection_get_unix_user (connection, &uid))
1567 if (!adjust_connections_for_uid (d->connections,
1572 /* Create and cache a string which holds information about the
1573 * peer process; used for logging purposes.
1575 if (!cache_peer_loginfo_string (d, connection))
1578 /* Now the connection is active, move it between lists */
1579 _dbus_list_unlink (&d->connections->incomplete,
1580 d->link_in_connection_list);
1581 d->connections->n_incomplete -= 1;
1582 _dbus_list_append_link (&d->connections->completed,
1583 d->link_in_connection_list);
1584 d->connections->n_completed += 1;
1586 _dbus_assert (d->connections->n_incomplete >= 0);
1587 _dbus_assert (d->connections->n_completed > 0);
1589 /* If we have dropped below the max. number of incomplete
1590 * connections, start accept()ing again */
1591 bus_context_check_all_watches (d->connections->context);
1593 /* See if we can remove the timeout */
1594 bus_connections_expire_incomplete (d->connections);
1596 _dbus_assert (bus_connection_is_active (connection));
1600 BUS_SET_OOM (error);
1601 dbus_free (d->name);
1604 bus_client_policy_unref (d->policy);
1610 bus_connections_reload_policy (BusConnections *connections,
1613 BusConnectionData *d;
1614 DBusConnection *connection;
1617 _dbus_assert (connections != NULL);
1618 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1620 for (link = _dbus_list_get_first_link (&(connections->completed));
1622 link = _dbus_list_get_next_link (&(connections->completed), link))
1624 connection = link->data;
1625 d = BUS_CONNECTION_DATA (connection);
1626 _dbus_assert (d != NULL);
1627 _dbus_assert (d->policy != NULL);
1629 bus_client_policy_unref (d->policy);
1630 d->policy = bus_context_create_client_policy (connections->context,
1633 if (d->policy == NULL)
1635 _dbus_verbose ("Failed to create security policy for connection %p\n",
1637 _DBUS_ASSERT_ERROR_IS_SET (error);
1646 bus_connection_get_name (DBusConnection *connection)
1648 BusConnectionData *d;
1650 d = BUS_CONNECTION_DATA (connection);
1651 _dbus_assert (d != NULL);
1657 * Check whether completing the passed-in connection would
1658 * exceed limits, and if so set error and return #FALSE
1661 bus_connections_check_limits (BusConnections *connections,
1662 DBusConnection *requesting_completion,
1667 if (connections->n_completed >=
1668 bus_context_get_max_completed_connections (connections->context))
1670 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1671 "The maximum number of active connections has been reached");
1675 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1677 if (get_connections_for_uid (connections, uid) >=
1678 bus_context_get_max_connections_per_user (connections->context))
1680 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1681 "The maximum number of active connections for UID %lu has been reached",
1691 bus_pending_reply_free (BusPendingReply *pending)
1693 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1695 pending->will_send_reply,
1696 pending->will_get_reply,
1697 pending->reply_serial);
1699 dbus_free (pending);
1703 bus_pending_reply_send_no_reply (BusConnections *connections,
1704 BusTransaction *transaction,
1705 BusPendingReply *pending)
1707 DBusMessage *message;
1708 DBusMessageIter iter;
1714 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1715 if (message == NULL)
1718 dbus_message_set_no_reply (message, TRUE);
1720 if (!dbus_message_set_reply_serial (message,
1721 pending->reply_serial))
1724 if (!dbus_message_set_error_name (message,
1725 DBUS_ERROR_NO_REPLY))
1728 /* If you change these messages, adjust test/dbus-daemon.c to match */
1729 if (pending->will_send_reply == NULL)
1730 errmsg = "Message recipient disconnected from message bus without replying";
1732 errmsg = "Message did not receive a reply (timeout by message bus)";
1734 dbus_message_iter_init_append (message, &iter);
1735 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1738 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1745 dbus_message_unref (message);
1750 bus_pending_reply_expired (BusExpireList *list,
1754 BusPendingReply *pending = link->data;
1755 BusConnections *connections = data;
1756 BusTransaction *transaction;
1758 /* No reply is forthcoming. So nuke it if we can. If not,
1759 * leave it in the list to try expiring again later when we
1763 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1765 pending->will_send_reply,
1766 pending->will_get_reply,
1767 pending->reply_serial);
1769 transaction = bus_transaction_new (connections->context);
1770 if (transaction == NULL)
1773 if (!bus_pending_reply_send_no_reply (connections,
1777 bus_transaction_cancel_and_free (transaction);
1781 bus_expire_list_remove_link (connections->pending_replies, link);
1783 bus_pending_reply_free (pending);
1784 bus_transaction_execute_and_free (transaction);
1790 bus_connection_drop_pending_replies (BusConnections *connections,
1791 DBusConnection *connection)
1793 /* The DBusConnection is almost 100% finalized here, so you can't
1794 * do anything with it except check for pointer equality
1798 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1801 link = bus_expire_list_get_first_link (connections->pending_replies);
1802 while (link != NULL)
1805 BusPendingReply *pending;
1807 next = bus_expire_list_get_next_link (connections->pending_replies,
1809 pending = link->data;
1811 if (pending->will_get_reply == connection)
1813 /* We don't need to track this pending reply anymore */
1815 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1817 pending->will_send_reply,
1818 pending->will_get_reply,
1819 pending->reply_serial);
1821 bus_expire_list_remove_link (connections->pending_replies,
1823 bus_pending_reply_free (pending);
1825 else if (pending->will_send_reply == connection)
1827 /* The reply isn't going to be sent, so set things
1828 * up so it will be expired right away
1830 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1832 pending->will_send_reply,
1833 pending->will_get_reply,
1834 pending->reply_serial);
1836 pending->will_send_reply = NULL;
1837 pending->expire_item.added_tv_sec = 0;
1838 pending->expire_item.added_tv_usec = 0;
1840 bus_expire_list_recheck_immediately (connections->pending_replies);
1850 BusPendingReply *pending;
1851 BusConnections *connections;
1852 } CancelPendingReplyData;
1855 cancel_pending_reply (void *data)
1857 CancelPendingReplyData *d = data;
1859 _dbus_verbose ("d = %p\n", d);
1861 if (!bus_expire_list_remove (d->connections->pending_replies,
1862 &d->pending->expire_item))
1863 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1865 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1869 cancel_pending_reply_data_free (void *data)
1871 CancelPendingReplyData *d = data;
1873 _dbus_verbose ("d = %p\n", d);
1875 /* d->pending should be either freed or still
1876 * in the list of pending replies (owned by someone
1884 * Record that a reply is allowed; return TRUE on success.
1887 bus_connections_expect_reply (BusConnections *connections,
1888 BusTransaction *transaction,
1889 DBusConnection *will_get_reply,
1890 DBusConnection *will_send_reply,
1891 DBusMessage *reply_to_this,
1894 BusPendingReply *pending;
1895 dbus_uint32_t reply_serial;
1897 CancelPendingReplyData *cprd;
1900 _dbus_assert (will_get_reply != NULL);
1901 _dbus_assert (will_send_reply != NULL);
1902 _dbus_assert (reply_to_this != NULL);
1904 if (dbus_message_get_no_reply (reply_to_this))
1905 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1907 reply_serial = dbus_message_get_serial (reply_to_this);
1909 link = bus_expire_list_get_first_link (connections->pending_replies);
1911 while (link != NULL)
1913 pending = link->data;
1915 if (pending->reply_serial == reply_serial &&
1916 pending->will_get_reply == will_get_reply &&
1917 pending->will_send_reply == will_send_reply)
1919 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1920 "Message has the same reply serial as a currently-outstanding existing method call");
1924 link = bus_expire_list_get_next_link (connections->pending_replies,
1926 if (pending->will_get_reply == will_get_reply)
1931 bus_context_get_max_replies_per_connection (connections->context))
1933 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1934 "The maximum number of pending replies per connection has been reached");
1938 pending = dbus_new0 (BusPendingReply, 1);
1939 if (pending == NULL)
1941 BUS_SET_OOM (error);
1945 #ifdef DBUS_ENABLE_VERBOSE_MODE
1946 /* so we can see a not-yet-added pending reply */
1947 pending->expire_item.added_tv_sec = 1;
1948 pending->expire_item.added_tv_usec = 1;
1951 pending->will_get_reply = will_get_reply;
1952 pending->will_send_reply = will_send_reply;
1953 pending->reply_serial = reply_serial;
1955 cprd = dbus_new0 (CancelPendingReplyData, 1);
1958 BUS_SET_OOM (error);
1959 bus_pending_reply_free (pending);
1963 if (!bus_expire_list_add (connections->pending_replies,
1964 &pending->expire_item))
1966 BUS_SET_OOM (error);
1968 bus_pending_reply_free (pending);
1972 if (!bus_transaction_add_cancel_hook (transaction,
1973 cancel_pending_reply,
1975 cancel_pending_reply_data_free))
1977 BUS_SET_OOM (error);
1978 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1980 bus_pending_reply_free (pending);
1984 cprd->pending = pending;
1985 cprd->connections = connections;
1987 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1988 &pending->expire_item.added_tv_usec);
1990 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1992 pending->will_send_reply,
1993 pending->will_get_reply,
1994 pending->reply_serial);
2002 BusConnections *connections;
2003 } CheckPendingReplyData;
2006 cancel_check_pending_reply (void *data)
2008 CheckPendingReplyData *d = data;
2010 _dbus_verbose ("d = %p\n",d);
2012 bus_expire_list_add_link (d->connections->pending_replies,
2018 check_pending_reply_data_free (void *data)
2020 CheckPendingReplyData *d = data;
2022 _dbus_verbose ("d = %p\n",d);
2024 if (d->link != NULL)
2026 BusPendingReply *pending = d->link->data;
2028 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2029 &pending->expire_item));
2031 bus_pending_reply_free (pending);
2032 _dbus_list_free_link (d->link);
2039 * Check whether a reply is allowed, remove BusPendingReply
2040 * if so, return TRUE if so.
2043 bus_connections_check_reply (BusConnections *connections,
2044 BusTransaction *transaction,
2045 DBusConnection *sending_reply,
2046 DBusConnection *receiving_reply,
2050 CheckPendingReplyData *cprd;
2052 dbus_uint32_t reply_serial;
2054 _dbus_assert (sending_reply != NULL);
2055 _dbus_assert (receiving_reply != NULL);
2057 reply_serial = dbus_message_get_reply_serial (reply);
2059 link = bus_expire_list_get_first_link (connections->pending_replies);
2060 while (link != NULL)
2062 BusPendingReply *pending = link->data;
2064 if (pending->reply_serial == reply_serial &&
2065 pending->will_get_reply == receiving_reply &&
2066 pending->will_send_reply == sending_reply)
2068 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2072 link = bus_expire_list_get_next_link (connections->pending_replies,
2078 _dbus_verbose ("No pending reply expected\n");
2083 cprd = dbus_new0 (CheckPendingReplyData, 1);
2086 BUS_SET_OOM (error);
2090 if (!bus_transaction_add_cancel_hook (transaction,
2091 cancel_check_pending_reply,
2093 check_pending_reply_data_free))
2095 BUS_SET_OOM (error);
2101 cprd->connections = connections;
2103 bus_expire_list_unlink (connections->pending_replies,
2106 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2114 * Note that this is fairly fragile; in particular, don't try to use
2115 * one transaction across any main loop iterations.
2120 BusTransaction *transaction;
2121 DBusMessage *message;
2122 DBusPreallocatedSend *preallocated;
2127 BusTransactionCancelFunction cancel_function;
2128 DBusFreeFunction free_data_function;
2132 struct BusTransaction
2134 DBusList *connections;
2135 BusContext *context;
2136 DBusList *cancel_hooks;
2140 message_to_send_free (DBusConnection *connection,
2141 MessageToSend *to_send)
2143 if (to_send->message)
2144 dbus_message_unref (to_send->message);
2146 if (to_send->preallocated)
2147 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2149 dbus_free (to_send);
2153 cancel_hook_cancel (void *element,
2156 CancelHook *ch = element;
2158 _dbus_verbose ("Running transaction cancel hook\n");
2160 if (ch->cancel_function)
2161 (* ch->cancel_function) (ch->data);
2165 cancel_hook_free (void *element,
2168 CancelHook *ch = element;
2170 if (ch->free_data_function)
2171 (* ch->free_data_function) (ch->data);
2177 free_cancel_hooks (BusTransaction *transaction)
2179 _dbus_list_foreach (&transaction->cancel_hooks,
2180 cancel_hook_free, NULL);
2182 _dbus_list_clear (&transaction->cancel_hooks);
2186 bus_transaction_new (BusContext *context)
2188 BusTransaction *transaction;
2190 transaction = dbus_new0 (BusTransaction, 1);
2191 if (transaction == NULL)
2194 transaction->context = context;
2200 bus_transaction_get_context (BusTransaction *transaction)
2202 return transaction->context;
2206 * Reserve enough memory to capture the given message if the
2207 * transaction goes through.
2210 bus_transaction_capture (BusTransaction *transaction,
2211 DBusConnection *sender,
2212 DBusMessage *message)
2214 BusConnections *connections;
2217 DBusList *recipients = NULL;
2218 dbus_bool_t ret = FALSE;
2220 connections = bus_context_get_connections (transaction->context);
2222 /* shortcut: don't compose the message unless someone wants it */
2223 if (connections->monitors == NULL)
2226 mm = connections->monitor_matchmaker;
2227 /* This is non-null if there has ever been a monitor - we don't GC it.
2228 * There's little point, since there is up to 1 per process. */
2229 _dbus_assert (mm != NULL);
2231 if (!bus_matchmaker_get_recipients (mm, connections, sender, NULL, message,
2235 for (link = _dbus_list_get_first_link (&recipients);
2237 link = _dbus_list_get_next_link (&recipients, link))
2239 DBusConnection *recipient = link->data;
2241 if (!bus_transaction_send (transaction, recipient, message))
2248 _dbus_list_clear (&recipients);
2253 bus_transaction_capture_error_reply (BusTransaction *transaction,
2254 const DBusError *error,
2255 DBusMessage *in_reply_to)
2257 BusConnections *connections;
2259 dbus_bool_t ret = FALSE;
2261 _dbus_assert (error != NULL);
2262 _DBUS_ASSERT_ERROR_IS_SET (error);
2264 connections = bus_context_get_connections (transaction->context);
2266 /* shortcut: don't compose the message unless someone wants it */
2267 if (connections->monitors == NULL)
2270 reply = dbus_message_new_error (in_reply_to,
2277 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2280 ret = bus_transaction_capture (transaction, NULL, reply);
2283 dbus_message_unref (reply);
2288 bus_transaction_send_from_driver (BusTransaction *transaction,
2289 DBusConnection *connection,
2290 DBusMessage *message)
2292 DBusError error = DBUS_ERROR_INIT;
2294 /* We have to set the sender to the driver, and have
2295 * to check security policy since it was not done in
2298 _dbus_verbose ("Sending %s %s %s from driver\n",
2299 dbus_message_get_interface (message) ?
2300 dbus_message_get_interface (message) : "(no interface)",
2301 dbus_message_get_member (message) ?
2302 dbus_message_get_member (message) : "(no member)",
2303 dbus_message_get_error_name (message) ?
2304 dbus_message_get_error_name (message) : "(no error name)");
2306 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2309 if (bus_connection_is_active (connection))
2311 if (!dbus_message_set_destination (message,
2312 bus_connection_get_name (connection)))
2316 /* bus driver never wants a reply */
2317 dbus_message_set_no_reply (message, TRUE);
2319 /* Capture it for monitors, even if the real recipient's receive policy
2320 * does not allow it to receive this message from us (which would be odd).
2322 if (!bus_transaction_capture (transaction, NULL, message))
2325 /* If security policy doesn't allow the message, we would silently
2326 * eat it; the driver doesn't care about getting a reply. However,
2327 * if we're actively capturing messages, it's nice to log that we
2328 * tried to send it and did not allow ourselves to do so.
2330 switch (bus_context_check_security_policy (bus_transaction_get_context (transaction),
2332 NULL, connection, connection, message, &error,
2335 case BUS_RESULT_TRUE:
2337 case BUS_RESULT_FALSE:
2338 if (!bus_transaction_capture_error_reply (transaction, &error, message))
2340 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2341 "message from dbus-daemon rejected but not enough "
2342 "memory to capture it");
2345 /* This is not fatal to the transaction so silently eat the disallowed
2346 * message (see reasoning above) */
2347 dbus_error_free (&error);
2350 case BUS_RESULT_LATER:
2351 _dbus_verbose ("Cannot delay sending message from bus driver, dropping it\n");
2356 return bus_transaction_send (transaction, connection, message);
2360 bus_transaction_send (BusTransaction *transaction,
2361 DBusConnection *connection,
2362 DBusMessage *message)
2364 MessageToSend *to_send;
2365 BusConnectionData *d;
2368 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2369 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2370 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2372 dbus_message_get_interface (message) ?
2373 dbus_message_get_interface (message) : "(unset)",
2374 dbus_message_get_member (message) ?
2375 dbus_message_get_member (message) : "(unset)",
2376 dbus_message_get_error_name (message) ?
2377 dbus_message_get_error_name (message) : "(unset)",
2378 dbus_connection_get_is_connected (connection) ?
2379 "" : " (disconnected)");
2381 _dbus_assert (dbus_message_get_sender (message) != NULL);
2383 if (!dbus_connection_get_is_connected (connection))
2384 return TRUE; /* silently ignore disconnected connections */
2386 d = BUS_CONNECTION_DATA (connection);
2387 _dbus_assert (d != NULL);
2389 to_send = dbus_new (MessageToSend, 1);
2390 if (to_send == NULL)
2395 to_send->preallocated = dbus_connection_preallocate_send (connection);
2396 if (to_send->preallocated == NULL)
2398 dbus_free (to_send);
2402 dbus_message_ref (message);
2403 to_send->message = message;
2404 to_send->transaction = transaction;
2406 _dbus_verbose ("about to prepend message\n");
2408 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2410 message_to_send_free (connection, to_send);
2414 _dbus_verbose ("prepended message\n");
2416 /* See if we already had this connection in the list
2417 * for this transaction. If we have a pending message,
2418 * then we should already be in transaction->connections
2420 link = _dbus_list_get_first_link (&d->transaction_messages);
2421 _dbus_assert (link->data == to_send);
2422 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2423 while (link != NULL)
2425 MessageToSend *m = link->data;
2426 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2428 if (m->transaction == transaction)
2436 if (!_dbus_list_prepend (&transaction->connections, connection))
2438 _dbus_list_remove (&d->transaction_messages, to_send);
2439 message_to_send_free (connection, to_send);
2448 transaction_free (BusTransaction *transaction)
2450 _dbus_assert (transaction->connections == NULL);
2452 free_cancel_hooks (transaction);
2454 dbus_free (transaction);
2458 connection_cancel_transaction (DBusConnection *connection,
2459 BusTransaction *transaction)
2462 BusConnectionData *d;
2464 d = BUS_CONNECTION_DATA (connection);
2465 _dbus_assert (d != NULL);
2467 link = _dbus_list_get_first_link (&d->transaction_messages);
2468 while (link != NULL)
2470 MessageToSend *m = link->data;
2471 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2473 if (m->transaction == transaction)
2475 _dbus_list_remove_link (&d->transaction_messages,
2478 message_to_send_free (connection, m);
2486 bus_transaction_cancel_and_free (BusTransaction *transaction)
2488 DBusConnection *connection;
2490 _dbus_verbose ("TRANSACTION: cancelled\n");
2492 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2493 connection_cancel_transaction (connection, transaction);
2495 _dbus_list_foreach (&transaction->cancel_hooks,
2496 cancel_hook_cancel, NULL);
2498 transaction_free (transaction);
2502 connection_execute_transaction (DBusConnection *connection,
2503 BusTransaction *transaction)
2506 BusConnectionData *d;
2508 d = BUS_CONNECTION_DATA (connection);
2509 _dbus_assert (d != NULL);
2511 /* Send the queue in order (FIFO) */
2512 link = _dbus_list_get_last_link (&d->transaction_messages);
2513 while (link != NULL)
2515 MessageToSend *m = link->data;
2516 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2518 if (m->transaction == transaction)
2520 _dbus_list_remove_link (&d->transaction_messages,
2523 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2525 dbus_connection_send_preallocated (connection,
2530 m->preallocated = NULL; /* so we don't double-free it */
2532 message_to_send_free (connection, m);
2540 bus_transaction_execute_and_free (BusTransaction *transaction)
2542 /* For each connection in transaction->connections
2545 DBusConnection *connection;
2547 _dbus_verbose ("TRANSACTION: executing\n");
2549 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2550 connection_execute_transaction (connection, transaction);
2552 transaction_free (transaction);
2556 bus_connection_remove_transactions (DBusConnection *connection)
2558 MessageToSend *to_send;
2559 BusConnectionData *d;
2561 d = BUS_CONNECTION_DATA (connection);
2562 _dbus_assert (d != NULL);
2564 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2566 /* only has an effect for the first MessageToSend listing this transaction */
2567 _dbus_list_remove (&to_send->transaction->connections,
2570 _dbus_list_remove (&d->transaction_messages, to_send);
2571 message_to_send_free (connection, to_send);
2576 * Converts the DBusError to a message reply
2579 bus_transaction_send_error_reply (BusTransaction *transaction,
2580 DBusConnection *connection,
2581 const DBusError *error,
2582 DBusMessage *in_reply_to)
2586 _dbus_assert (error != NULL);
2587 _DBUS_ASSERT_ERROR_IS_SET (error);
2589 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2590 error->name, error->message);
2592 reply = dbus_message_new_error (in_reply_to,
2598 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2600 dbus_message_unref (reply);
2604 dbus_message_unref (reply);
2610 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2611 BusTransactionCancelFunction cancel_function,
2613 DBusFreeFunction free_data_function)
2617 ch = dbus_new (CancelHook, 1);
2621 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2622 cancel_function, data);
2624 ch->cancel_function = cancel_function;
2626 ch->free_data_function = free_data_function;
2628 /* It's important that the hooks get run in reverse order that they
2631 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2641 bus_connections_get_n_active (BusConnections *connections)
2643 return connections->n_completed;
2647 bus_connections_get_n_incomplete (BusConnections *connections)
2649 return connections->n_incomplete;
2652 #ifdef DBUS_ENABLE_STATS
2654 bus_connections_get_total_match_rules (BusConnections *connections)
2656 return connections->total_match_rules;
2660 bus_connections_get_peak_match_rules (BusConnections *connections)
2662 return connections->peak_match_rules;
2666 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2668 return connections->peak_match_rules_per_conn;
2672 bus_connections_get_total_bus_names (BusConnections *connections)
2674 return connections->total_bus_names;
2678 bus_connections_get_peak_bus_names (BusConnections *connections)
2680 return connections->peak_bus_names;
2684 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2686 return connections->peak_bus_names_per_conn;
2690 bus_connection_get_peak_match_rules (DBusConnection *connection)
2692 BusConnectionData *d;
2694 d = BUS_CONNECTION_DATA (connection);
2695 _dbus_assert(d != NULL);
2697 return d->peak_match_rules;
2701 bus_connection_get_peak_bus_names (DBusConnection *connection)
2703 BusConnectionData *d;
2705 d = BUS_CONNECTION_DATA (connection);
2706 _dbus_assert(d != NULL);
2708 return d->peak_bus_names;
2710 #endif /* DBUS_ENABLE_STATS */
2713 bus_connection_is_monitor (DBusConnection *connection)
2715 BusConnectionData *d;
2717 d = BUS_CONNECTION_DATA (connection);
2718 _dbus_assert(d != NULL);
2720 return d->link_in_monitors != NULL;
2724 bcd_add_monitor_rules (BusConnectionData *d,
2725 DBusConnection *connection,
2728 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2733 mm = bus_matchmaker_new ();
2738 d->connections->monitor_matchmaker = mm;
2741 for (iter = _dbus_list_get_first_link (rules);
2743 iter = _dbus_list_get_next_link (rules, iter))
2745 if (!bus_matchmaker_add_rule (mm, iter->data))
2747 bus_matchmaker_disconnected (mm, connection);
2756 bcd_drop_monitor_rules (BusConnectionData *d,
2757 DBusConnection *connection)
2759 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2762 bus_matchmaker_disconnected (mm, connection);
2766 bus_connection_be_monitor (DBusConnection *connection,
2767 BusTransaction *transaction,
2771 BusConnectionData *d;
2776 d = BUS_CONNECTION_DATA (connection);
2777 _dbus_assert (d != NULL);
2779 link = _dbus_list_alloc_link (connection);
2783 BUS_SET_OOM (error);
2787 if (!bcd_add_monitor_rules (d, connection, rules))
2789 _dbus_list_free_link (link);
2790 BUS_SET_OOM (error);
2794 /* release all its names */
2795 if (!_dbus_list_copy (&d->services_owned, &tmp))
2797 bcd_drop_monitor_rules (d, connection);
2798 _dbus_list_free_link (link);
2799 BUS_SET_OOM (error);
2803 for (iter = _dbus_list_get_first_link (&tmp);
2805 iter = _dbus_list_get_next_link (&tmp, iter))
2807 BusService *service = iter->data;
2809 /* This call is transactional: if there isn't enough memory to
2810 * do everything, then the service gets all its names back when
2811 * the transaction is cancelled due to OOM. */
2812 if (!bus_service_remove_owner (service, connection, transaction, error))
2814 bcd_drop_monitor_rules (d, connection);
2815 _dbus_list_free_link (link);
2816 _dbus_list_clear (&tmp);
2821 /* We have now done everything that can fail, so there is no problem
2822 * with doing the irrevocable stuff. */
2824 _dbus_list_clear (&tmp);
2826 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2827 "Connection %s (%s) became a monitor.", d->name,
2828 d->cached_loginfo_string);
2830 if (d->n_match_rules > 0)
2834 mm = bus_context_get_matchmaker (d->connections->context);
2835 bus_matchmaker_disconnected (mm, connection);
2838 /* flag it as a monitor */
2839 d->link_in_monitors = link;
2840 _dbus_list_append_link (&d->connections->monitors, link);
2842 /* it isn't allowed to reply, and it is no longer relevant whether it
2843 * receives replies */
2844 bus_connection_drop_pending_replies (d->connections, connection);