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_set_enabled (connections->expire_timeout, FALSE);
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;
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, '"'))
618 if (dbus_connection_get_windows_user (connection, &windows_sid))
620 dbus_bool_t did_append;
621 did_append = _dbus_string_append_printf (&loginfo_buf,
622 "sid=\"%s\" ", windows_sid);
623 dbus_free (windows_sid);
628 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
631 _dbus_string_free (&loginfo_buf);
635 _dbus_string_free (&loginfo_buf);
640 check_pending_fds_cb (DBusConnection *connection)
642 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
643 int n_pending_unix_fds_old;
644 int n_pending_unix_fds_new;
646 _dbus_assert(d != NULL);
648 n_pending_unix_fds_old = d->n_pending_unix_fds;
649 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
651 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
652 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
654 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
656 _dbus_timeout_set_interval (d->pending_unix_fds_timeout,
657 bus_context_get_pending_fd_timeout (d->connections->context));
658 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, TRUE);
661 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
663 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
667 d->n_pending_unix_fds = n_pending_unix_fds_new;
671 pending_unix_fds_timeout_cb (void *data)
673 DBusConnection *connection = data;
674 dbus_connection_close (connection);
679 bus_connections_setup_connection (BusConnections *connections,
680 DBusConnection *connection)
683 BusConnectionData *d;
688 d = dbus_new0 (BusConnectionData, 1);
693 d->connections = connections;
694 d->connection = connection;
696 _dbus_get_monotonic_time (&d->connection_tv_sec,
697 &d->connection_tv_usec);
699 _dbus_assert (connection_data_slot >= 0);
701 if (!dbus_connection_set_data (connection,
702 connection_data_slot,
703 d, free_connection_data))
709 dbus_connection_set_route_peer_messages (connection, TRUE);
713 dbus_error_init (&error);
714 d->selinux_id = bus_selinux_init_connection_id (connection,
716 if (dbus_error_is_set (&error))
718 /* This is a bit bogus because we pretend all errors
719 * are OOM; this is done because we know that in bus.c
720 * an OOM error disconnects the connection, which is
721 * the same thing we want on any other error.
723 dbus_error_free (&error);
727 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
729 if (dbus_error_is_set (&error))
731 /* This is a bit bogus because we pretend all errors
732 * are OOM; this is done because we know that in bus.c
733 * an OOM error disconnects the connection, which is
734 * the same thing we want on any other error.
736 dbus_error_free (&error);
740 if (!dbus_connection_set_watch_functions (connection,
741 add_connection_watch,
742 remove_connection_watch,
743 toggle_connection_watch,
748 if (!dbus_connection_set_timeout_functions (connection,
749 add_connection_timeout,
750 remove_connection_timeout,
755 /* For now we don't need to set a Windows user function because
756 * there are no policies in the config file controlling what
757 * Windows users can connect. The default 'same user that owns the
758 * bus can connect' behavior of DBusConnection is fine on Windows.
760 dbus_connection_set_unix_user_function (connection,
761 allow_unix_user_function,
764 dbus_connection_set_dispatch_status_function (connection,
765 dispatch_status_function,
766 bus_context_get_loop (connections->context),
769 d->link_in_connection_list = _dbus_list_alloc_link (connection);
770 if (d->link_in_connection_list == NULL)
773 /* Setup the connection with the dispatcher */
774 if (!bus_dispatch_add_connection (connection))
777 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
779 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
781 bus_dispatch_remove_connection (connection);
786 /* Setup pending fds timeout (see #80559) */
787 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
788 pending_unix_fds_timeout_cb,
790 if (d->pending_unix_fds_timeout == NULL)
793 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
794 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
795 d->pending_unix_fds_timeout))
798 _dbus_connection_set_pending_fds_function (connection,
799 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
802 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
803 connections->n_incomplete += 1;
805 dbus_connection_ref (connection);
807 bus_connections_expire_incomplete (connections);
809 /* The listening socket is removed from the main loop,
810 * i.e. does not accept(), while n_incomplete is at its
811 * maximum value; so we shouldn't get here in that case */
812 _dbus_assert (connections->n_incomplete <=
813 bus_context_get_max_incomplete_connections (connections->context));
815 /* If we have the maximum number of incomplete connections,
816 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
817 bus_context_check_all_watches (d->connections->context);
825 bus_selinux_id_unref (d->selinux_id);
826 d->selinux_id = NULL;
828 if (d->apparmor_confinement)
829 bus_apparmor_confinement_unref (d->apparmor_confinement);
830 d->apparmor_confinement = NULL;
832 if (!dbus_connection_set_watch_functions (connection,
836 _dbus_assert_not_reached ("setting watch functions to NULL failed");
838 if (!dbus_connection_set_timeout_functions (connection,
842 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
844 dbus_connection_set_unix_user_function (connection,
847 dbus_connection_set_windows_user_function (connection,
850 dbus_connection_set_dispatch_status_function (connection,
853 if (d->pending_unix_fds_timeout)
854 _dbus_timeout_unref (d->pending_unix_fds_timeout);
856 d->pending_unix_fds_timeout = NULL;
858 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
860 if (d->link_in_connection_list != NULL)
862 _dbus_assert (d->link_in_connection_list->next == NULL);
863 _dbus_assert (d->link_in_connection_list->prev == NULL);
864 _dbus_list_free_link (d->link_in_connection_list);
865 d->link_in_connection_list = NULL;
868 if (!dbus_connection_set_data (connection,
869 connection_data_slot,
871 _dbus_assert_not_reached ("failed to set connection data to null");
873 /* "d" has now been freed */
880 bus_connections_expire_incomplete (BusConnections *connections)
886 if (connections->incomplete != NULL)
888 long tv_sec, tv_usec;
892 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
893 auth_timeout = bus_context_get_auth_timeout (connections->context);
895 link = _dbus_list_get_first_link (&connections->incomplete);
898 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
899 DBusConnection *connection;
900 BusConnectionData *d;
903 connection = link->data;
905 d = BUS_CONNECTION_DATA (connection);
907 _dbus_assert (d != NULL);
909 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
910 d->connection_tv_usec,
913 if (elapsed >= (double) auth_timeout)
915 /* Unfortunately, we can't identify the connection: it doesn't
916 * have a unique name yet, we don't know its uid/pid yet,
918 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
919 "Connection has not authenticated soon enough, closing it "
920 "(auth_timeout=%dms, elapsed: %.0fms)",
921 auth_timeout, elapsed);
923 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
924 dbus_connection_close (connection);
928 /* We can end the loop, since the connections are in oldest-first order */
929 next_interval = ((double)auth_timeout) - elapsed;
930 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
931 connection, next_interval);
940 bus_expire_timeout_set_interval (connections->expire_timeout,
945 expire_incomplete_timeout (void *data)
947 BusConnections *connections = data;
949 _dbus_verbose ("Running\n");
951 /* note that this may remove the timeout */
952 bus_connections_expire_incomplete (connections);
958 bus_connection_get_unix_groups (DBusConnection *connection,
959 unsigned long **groups,
968 if (dbus_connection_get_unix_user (connection, &uid))
970 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
972 _dbus_verbose ("Did not get any groups for UID %lu\n",
978 _dbus_verbose ("Got %d groups for UID %lu\n",
984 return TRUE; /* successfully got 0 groups */
988 bus_connection_is_in_unix_group (DBusConnection *connection,
992 unsigned long *group_ids;
995 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1000 while (i < n_group_ids)
1002 if (group_ids[i] == gid)
1004 dbus_free (group_ids);
1010 dbus_free (group_ids);
1015 bus_connection_get_loginfo (DBusConnection *connection)
1017 BusConnectionData *d;
1019 d = BUS_CONNECTION_DATA (connection);
1020 _dbus_assert(d != NULL);
1022 if (!bus_connection_is_active (connection))
1024 return d->cached_loginfo_string;
1028 bus_connection_get_policy (DBusConnection *connection)
1030 BusConnectionData *d;
1032 d = BUS_CONNECTION_DATA (connection);
1034 _dbus_assert (d != NULL);
1035 _dbus_assert (d->policy != NULL);
1041 foreach_active (BusConnections *connections,
1042 BusConnectionForeachFunction function,
1047 link = _dbus_list_get_first_link (&connections->completed);
1048 while (link != NULL)
1050 DBusConnection *connection = link->data;
1051 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1053 if (!(* function) (connection, data))
1063 foreach_inactive (BusConnections *connections,
1064 BusConnectionForeachFunction function,
1069 link = _dbus_list_get_first_link (&connections->incomplete);
1070 while (link != NULL)
1072 DBusConnection *connection = link->data;
1073 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1075 if (!(* function) (connection, data))
1085 * Calls function on each active connection; if the function returns
1086 * #FALSE, stops iterating. Active connections are authenticated
1087 * and have sent a Hello message.
1089 * @param connections the connections object
1090 * @param function the function
1091 * @param data data to pass to it as a second arg
1094 bus_connections_foreach_active (BusConnections *connections,
1095 BusConnectionForeachFunction function,
1098 foreach_active (connections, function, data);
1102 * Calls function on each connection; if the function returns
1103 * #FALSE, stops iterating.
1105 * @param connections the connections object
1106 * @param function the function
1107 * @param data data to pass to it as a second arg
1110 bus_connections_foreach (BusConnections *connections,
1111 BusConnectionForeachFunction function,
1114 if (!foreach_active (connections, function, data))
1117 foreach_inactive (connections, function, data);
1121 bus_connections_get_context (BusConnections *connections)
1123 return connections->context;
1127 * This is used to avoid covering the same connection twice when
1128 * traversing connections. Note that it assumes we will
1129 * bus_connection_mark_stamp() each connection at least once per
1130 * INT_MAX increments of the global stamp, or wraparound would break
1134 bus_connections_increment_stamp (BusConnections *connections)
1136 connections->stamp += 1;
1139 /* Mark connection with current stamp, return TRUE if it
1140 * didn't already have that stamp
1143 bus_connection_mark_stamp (DBusConnection *connection)
1145 BusConnectionData *d;
1147 d = BUS_CONNECTION_DATA (connection);
1149 _dbus_assert (d != NULL);
1151 if (d->stamp == d->connections->stamp)
1155 d->stamp = d->connections->stamp;
1161 bus_connection_get_context (DBusConnection *connection)
1163 BusConnectionData *d;
1165 d = BUS_CONNECTION_DATA (connection);
1167 _dbus_assert (d != NULL);
1169 return d->connections->context;
1173 bus_connection_get_connections (DBusConnection *connection)
1175 BusConnectionData *d;
1177 d = BUS_CONNECTION_DATA (connection);
1179 _dbus_assert (d != NULL);
1181 return d->connections;
1185 bus_connection_get_registry (DBusConnection *connection)
1187 BusConnectionData *d;
1189 d = BUS_CONNECTION_DATA (connection);
1191 _dbus_assert (d != NULL);
1193 return bus_context_get_registry (d->connections->context);
1197 bus_connection_get_activation (DBusConnection *connection)
1199 BusConnectionData *d;
1201 d = BUS_CONNECTION_DATA (connection);
1203 _dbus_assert (d != NULL);
1205 return bus_context_get_activation (d->connections->context);
1209 bus_connection_get_matchmaker (DBusConnection *connection)
1211 BusConnectionData *d;
1213 d = BUS_CONNECTION_DATA (connection);
1215 _dbus_assert (d != NULL);
1217 return bus_context_get_matchmaker (d->connections->context);
1221 bus_connection_get_selinux_id (DBusConnection *connection)
1223 BusConnectionData *d;
1225 d = BUS_CONNECTION_DATA (connection);
1227 _dbus_assert (d != NULL);
1229 return d->selinux_id;
1232 BusAppArmorConfinement*
1233 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1235 BusConnectionData *d;
1237 d = BUS_CONNECTION_DATA (connection);
1239 _dbus_assert (d != NULL);
1241 bus_apparmor_confinement_ref (d->apparmor_confinement);
1242 return d->apparmor_confinement;
1246 * Checks whether the connection is registered with the message bus.
1248 * @param connection the connection
1249 * @returns #TRUE if we're an active message bus participant
1252 bus_connection_is_active (DBusConnection *connection)
1254 BusConnectionData *d;
1256 d = BUS_CONNECTION_DATA (connection);
1257 _dbus_assert(d != NULL);
1259 return d->name != NULL;
1263 bus_connection_preallocate_oom_error (DBusConnection *connection)
1265 DBusMessage *message;
1266 DBusPreallocatedSend *preallocated;
1267 BusConnectionData *d;
1269 d = BUS_CONNECTION_DATA (connection);
1271 _dbus_assert (d != NULL);
1273 if (d->oom_preallocated != NULL)
1276 preallocated = dbus_connection_preallocate_send (connection);
1277 if (preallocated == NULL)
1280 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1282 if (message == NULL)
1284 dbus_connection_free_preallocated_send (connection, preallocated);
1288 /* d->name may be NULL, but that is OK */
1289 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1290 !dbus_message_set_destination (message, d->name) ||
1291 !dbus_message_set_sender (message,
1294 dbus_connection_free_preallocated_send (connection, preallocated);
1295 dbus_message_unref (message);
1299 /* set reply serial to placeholder value just so space is already allocated
1302 if (!dbus_message_set_reply_serial (message, 14))
1304 dbus_connection_free_preallocated_send (connection, preallocated);
1305 dbus_message_unref (message);
1309 d->oom_message = message;
1310 d->oom_preallocated = preallocated;
1316 bus_connection_send_oom_error (DBusConnection *connection,
1317 DBusMessage *in_reply_to)
1319 BusConnectionData *d;
1321 d = BUS_CONNECTION_DATA (connection);
1323 _dbus_assert (d != NULL);
1324 _dbus_assert (d->oom_message != NULL);
1326 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1327 "dbus-daemon transaction failed (OOM), sending error to "
1328 "sender %s", bus_connection_get_loginfo (connection));
1330 /* should always succeed since we set it to a placeholder earlier */
1331 if (!dbus_message_set_reply_serial (d->oom_message,
1332 dbus_message_get_serial (in_reply_to)))
1333 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1335 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1337 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1338 d->oom_message, NULL);
1340 dbus_message_unref (d->oom_message);
1341 d->oom_message = NULL;
1342 d->oom_preallocated = NULL;
1345 #ifdef DBUS_ENABLE_STATS
1347 update_peak (int *peak,
1356 bus_connection_add_match_rule_link (DBusConnection *connection,
1359 BusConnectionData *d;
1361 d = BUS_CONNECTION_DATA (connection);
1362 _dbus_assert (d != NULL);
1364 _dbus_list_append_link (&d->match_rules, link);
1366 d->n_match_rules += 1;
1368 #ifdef DBUS_ENABLE_STATS
1369 update_peak (&d->peak_match_rules, d->n_match_rules);
1370 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1372 d->connections->total_match_rules += 1;
1373 update_peak (&d->connections->peak_match_rules,
1374 d->connections->total_match_rules);
1379 bus_connection_add_match_rule (DBusConnection *connection,
1384 link = _dbus_list_alloc_link (rule);
1389 bus_connection_add_match_rule_link (connection, link);
1395 bus_connection_remove_match_rule (DBusConnection *connection,
1398 BusConnectionData *d;
1400 d = BUS_CONNECTION_DATA (connection);
1401 _dbus_assert (d != NULL);
1403 _dbus_list_remove_last (&d->match_rules, rule);
1405 d->n_match_rules -= 1;
1406 _dbus_assert (d->n_match_rules >= 0);
1408 #ifdef DBUS_ENABLE_STATS
1409 d->connections->total_match_rules -= 1;
1414 bus_connection_get_n_match_rules (DBusConnection *connection)
1416 BusConnectionData *d;
1418 d = BUS_CONNECTION_DATA (connection);
1419 _dbus_assert (d != NULL);
1421 return d->n_match_rules;
1425 bus_connection_add_owned_service_link (DBusConnection *connection,
1428 BusConnectionData *d;
1430 d = BUS_CONNECTION_DATA (connection);
1431 _dbus_assert (d != NULL);
1433 _dbus_list_append_link (&d->services_owned, link);
1435 d->n_services_owned += 1;
1437 #ifdef DBUS_ENABLE_STATS
1438 update_peak (&d->peak_bus_names, d->n_services_owned);
1439 update_peak (&d->connections->peak_bus_names_per_conn,
1440 d->n_services_owned);
1442 d->connections->total_bus_names += 1;
1443 update_peak (&d->connections->peak_bus_names,
1444 d->connections->total_bus_names);
1449 bus_connection_add_owned_service (DBusConnection *connection,
1450 BusService *service)
1454 link = _dbus_list_alloc_link (service);
1459 bus_connection_add_owned_service_link (connection, link);
1465 bus_connection_remove_owned_service (DBusConnection *connection,
1466 BusService *service)
1468 BusConnectionData *d;
1470 d = BUS_CONNECTION_DATA (connection);
1471 _dbus_assert (d != NULL);
1473 _dbus_list_remove_last (&d->services_owned, service);
1475 d->n_services_owned -= 1;
1476 _dbus_assert (d->n_services_owned >= 0);
1478 #ifdef DBUS_ENABLE_STATS
1479 d->connections->total_bus_names -= 1;
1484 bus_connection_get_n_services_owned (DBusConnection *connection)
1486 BusConnectionData *d;
1488 d = BUS_CONNECTION_DATA (connection);
1489 _dbus_assert (d != NULL);
1491 return d->n_services_owned;
1495 bus_connection_complete (DBusConnection *connection,
1496 const DBusString *name,
1499 BusConnectionData *d;
1502 d = BUS_CONNECTION_DATA (connection);
1503 _dbus_assert (d != NULL);
1504 _dbus_assert (d->name == NULL);
1505 _dbus_assert (d->policy == NULL);
1507 _dbus_assert (!bus_connection_is_active (connection));
1509 if (!_dbus_string_copy_data (name, &d->name))
1511 BUS_SET_OOM (error);
1515 _dbus_assert (d->name != NULL);
1517 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1519 d->policy = bus_context_create_client_policy (d->connections->context,
1523 /* we may have a NULL policy on OOM or error getting list of
1524 * groups for a user. In the latter case we don't handle it so
1525 * well currently, as it will just keep failing over and over.
1528 if (d->policy == NULL)
1530 _dbus_verbose ("Failed to create security policy for connection %p\n",
1532 _DBUS_ASSERT_ERROR_IS_SET (error);
1533 dbus_free (d->name);
1538 if (dbus_connection_get_unix_user (connection, &uid))
1540 if (!adjust_connections_for_uid (d->connections,
1545 /* Create and cache a string which holds information about the
1546 * peer process; used for logging purposes.
1548 if (!cache_peer_loginfo_string (d, connection))
1551 /* Now the connection is active, move it between lists */
1552 _dbus_list_unlink (&d->connections->incomplete,
1553 d->link_in_connection_list);
1554 d->connections->n_incomplete -= 1;
1555 _dbus_list_append_link (&d->connections->completed,
1556 d->link_in_connection_list);
1557 d->connections->n_completed += 1;
1559 _dbus_assert (d->connections->n_incomplete >= 0);
1560 _dbus_assert (d->connections->n_completed > 0);
1562 /* If we have dropped below the max. number of incomplete
1563 * connections, start accept()ing again */
1564 bus_context_check_all_watches (d->connections->context);
1566 /* See if we can remove the timeout */
1567 bus_connections_expire_incomplete (d->connections);
1569 _dbus_assert (bus_connection_is_active (connection));
1573 BUS_SET_OOM (error);
1574 dbus_free (d->name);
1577 bus_client_policy_unref (d->policy);
1583 bus_connections_reload_policy (BusConnections *connections,
1586 BusConnectionData *d;
1587 DBusConnection *connection;
1590 _dbus_assert (connections != NULL);
1591 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1593 for (link = _dbus_list_get_first_link (&(connections->completed));
1595 link = _dbus_list_get_next_link (&(connections->completed), link))
1597 connection = link->data;
1598 d = BUS_CONNECTION_DATA (connection);
1599 _dbus_assert (d != NULL);
1600 _dbus_assert (d->policy != NULL);
1602 bus_client_policy_unref (d->policy);
1603 d->policy = bus_context_create_client_policy (connections->context,
1606 if (d->policy == NULL)
1608 _dbus_verbose ("Failed to create security policy for connection %p\n",
1610 _DBUS_ASSERT_ERROR_IS_SET (error);
1619 bus_connection_get_name (DBusConnection *connection)
1621 BusConnectionData *d;
1623 d = BUS_CONNECTION_DATA (connection);
1624 _dbus_assert (d != NULL);
1630 * Check whether completing the passed-in connection would
1631 * exceed limits, and if so set error and return #FALSE
1634 bus_connections_check_limits (BusConnections *connections,
1635 DBusConnection *requesting_completion,
1636 const char **limit_name_out,
1643 limit = bus_context_get_max_completed_connections (connections->context);
1645 if (connections->n_completed >= limit)
1647 if (limit_name_out != NULL)
1648 *limit_name_out = "max_completed_connections";
1650 if (limit_out != NULL)
1653 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1654 "The maximum number of active connections has been reached");
1658 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1660 limit = bus_context_get_max_connections_per_user (connections->context);
1662 if (get_connections_for_uid (connections, uid) >= limit)
1664 if (limit_name_out != NULL)
1665 *limit_name_out = "max_connections_per_user";
1667 if (limit_out != NULL)
1670 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1671 "The maximum number of active connections for UID %lu has been reached",
1681 bus_pending_reply_free (BusPendingReply *pending)
1683 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1685 pending->will_send_reply,
1686 pending->will_get_reply,
1687 pending->reply_serial);
1689 dbus_free (pending);
1693 bus_pending_reply_send_no_reply (BusConnections *connections,
1694 BusTransaction *transaction,
1695 BusPendingReply *pending)
1697 DBusMessage *message;
1698 DBusMessageIter iter;
1704 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1705 if (message == NULL)
1708 dbus_message_set_no_reply (message, TRUE);
1710 if (!dbus_message_set_reply_serial (message,
1711 pending->reply_serial))
1714 if (!dbus_message_set_error_name (message,
1715 DBUS_ERROR_NO_REPLY))
1718 /* If you change these messages, adjust test/dbus-daemon.c to match */
1719 if (pending->will_send_reply == NULL)
1720 errmsg = "Message recipient disconnected from message bus without replying";
1722 errmsg = "Message did not receive a reply (timeout by message bus)";
1724 dbus_message_iter_init_append (message, &iter);
1725 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1728 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1735 dbus_message_unref (message);
1740 bus_pending_reply_expired (BusExpireList *list,
1744 BusPendingReply *pending = link->data;
1745 BusConnections *connections = data;
1746 BusTransaction *transaction;
1748 /* No reply is forthcoming. So nuke it if we can. If not,
1749 * leave it in the list to try expiring again later when we
1753 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1755 pending->will_send_reply,
1756 pending->will_get_reply,
1757 pending->reply_serial);
1759 transaction = bus_transaction_new (connections->context);
1760 if (transaction == NULL)
1763 if (!bus_pending_reply_send_no_reply (connections,
1767 bus_transaction_cancel_and_free (transaction);
1771 bus_expire_list_remove_link (connections->pending_replies, link);
1773 bus_pending_reply_free (pending);
1774 bus_transaction_execute_and_free (transaction);
1780 bus_connection_drop_pending_replies (BusConnections *connections,
1781 DBusConnection *connection)
1783 /* The DBusConnection is almost 100% finalized here, so you can't
1784 * do anything with it except check for pointer equality
1788 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1791 link = bus_expire_list_get_first_link (connections->pending_replies);
1792 while (link != NULL)
1795 BusPendingReply *pending;
1797 next = bus_expire_list_get_next_link (connections->pending_replies,
1799 pending = link->data;
1801 if (pending->will_get_reply == connection)
1803 /* We don't need to track this pending reply anymore */
1805 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1807 pending->will_send_reply,
1808 pending->will_get_reply,
1809 pending->reply_serial);
1811 bus_expire_list_remove_link (connections->pending_replies,
1813 bus_pending_reply_free (pending);
1815 else if (pending->will_send_reply == connection)
1817 /* The reply isn't going to be sent, so set things
1818 * up so it will be expired right away
1820 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1822 pending->will_send_reply,
1823 pending->will_get_reply,
1824 pending->reply_serial);
1826 pending->will_send_reply = NULL;
1827 pending->expire_item.added_tv_sec = 0;
1828 pending->expire_item.added_tv_usec = 0;
1830 bus_expire_list_recheck_immediately (connections->pending_replies);
1840 BusPendingReply *pending;
1841 BusConnections *connections;
1842 } CancelPendingReplyData;
1845 cancel_pending_reply (void *data)
1847 CancelPendingReplyData *d = data;
1849 _dbus_verbose ("d = %p\n", d);
1851 if (!bus_expire_list_remove (d->connections->pending_replies,
1852 &d->pending->expire_item))
1853 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1855 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1859 cancel_pending_reply_data_free (void *data)
1861 CancelPendingReplyData *d = data;
1863 _dbus_verbose ("d = %p\n", d);
1865 /* d->pending should be either freed or still
1866 * in the list of pending replies (owned by someone
1874 * Record that a reply is allowed; return TRUE on success.
1877 bus_connections_expect_reply (BusConnections *connections,
1878 BusTransaction *transaction,
1879 DBusConnection *will_get_reply,
1880 DBusConnection *will_send_reply,
1881 DBusMessage *reply_to_this,
1884 BusPendingReply *pending;
1885 dbus_uint32_t reply_serial;
1887 CancelPendingReplyData *cprd;
1890 _dbus_assert (will_get_reply != NULL);
1891 _dbus_assert (will_send_reply != NULL);
1892 _dbus_assert (reply_to_this != NULL);
1894 if (dbus_message_get_no_reply (reply_to_this))
1895 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1897 reply_serial = dbus_message_get_serial (reply_to_this);
1899 link = bus_expire_list_get_first_link (connections->pending_replies);
1901 while (link != NULL)
1903 pending = link->data;
1905 if (pending->reply_serial == reply_serial &&
1906 pending->will_get_reply == will_get_reply &&
1907 pending->will_send_reply == will_send_reply)
1909 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1910 "Message has the same reply serial as a currently-outstanding existing method call");
1914 link = bus_expire_list_get_next_link (connections->pending_replies,
1916 if (pending->will_get_reply == will_get_reply)
1921 bus_context_get_max_replies_per_connection (connections->context))
1923 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1924 "The maximum number of pending replies per connection has been reached");
1928 pending = dbus_new0 (BusPendingReply, 1);
1929 if (pending == NULL)
1931 BUS_SET_OOM (error);
1935 #ifdef DBUS_ENABLE_VERBOSE_MODE
1936 /* so we can see a not-yet-added pending reply */
1937 pending->expire_item.added_tv_sec = 1;
1938 pending->expire_item.added_tv_usec = 1;
1941 pending->will_get_reply = will_get_reply;
1942 pending->will_send_reply = will_send_reply;
1943 pending->reply_serial = reply_serial;
1945 cprd = dbus_new0 (CancelPendingReplyData, 1);
1948 BUS_SET_OOM (error);
1949 bus_pending_reply_free (pending);
1953 if (!bus_expire_list_add (connections->pending_replies,
1954 &pending->expire_item))
1956 BUS_SET_OOM (error);
1958 bus_pending_reply_free (pending);
1962 if (!bus_transaction_add_cancel_hook (transaction,
1963 cancel_pending_reply,
1965 cancel_pending_reply_data_free))
1967 BUS_SET_OOM (error);
1968 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1970 bus_pending_reply_free (pending);
1974 cprd->pending = pending;
1975 cprd->connections = connections;
1977 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1978 &pending->expire_item.added_tv_usec);
1980 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1982 pending->will_send_reply,
1983 pending->will_get_reply,
1984 pending->reply_serial);
1992 BusConnections *connections;
1993 } CheckPendingReplyData;
1996 cancel_check_pending_reply (void *data)
1998 CheckPendingReplyData *d = data;
2000 _dbus_verbose ("d = %p\n",d);
2002 bus_expire_list_add_link (d->connections->pending_replies,
2008 check_pending_reply_data_free (void *data)
2010 CheckPendingReplyData *d = data;
2012 _dbus_verbose ("d = %p\n",d);
2014 if (d->link != NULL)
2016 BusPendingReply *pending = d->link->data;
2018 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2019 &pending->expire_item));
2021 bus_pending_reply_free (pending);
2022 _dbus_list_free_link (d->link);
2029 * Check whether a reply is allowed, remove BusPendingReply
2030 * if so, return TRUE if so.
2033 bus_connections_check_reply (BusConnections *connections,
2034 BusTransaction *transaction,
2035 DBusConnection *sending_reply,
2036 DBusConnection *receiving_reply,
2040 CheckPendingReplyData *cprd;
2042 dbus_uint32_t reply_serial;
2044 _dbus_assert (sending_reply != NULL);
2045 _dbus_assert (receiving_reply != NULL);
2047 reply_serial = dbus_message_get_reply_serial (reply);
2049 link = bus_expire_list_get_first_link (connections->pending_replies);
2050 while (link != NULL)
2052 BusPendingReply *pending = link->data;
2054 if (pending->reply_serial == reply_serial &&
2055 pending->will_get_reply == receiving_reply &&
2056 pending->will_send_reply == sending_reply)
2058 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2062 link = bus_expire_list_get_next_link (connections->pending_replies,
2068 _dbus_verbose ("No pending reply expected\n");
2073 cprd = dbus_new0 (CheckPendingReplyData, 1);
2076 BUS_SET_OOM (error);
2080 if (!bus_transaction_add_cancel_hook (transaction,
2081 cancel_check_pending_reply,
2083 check_pending_reply_data_free))
2085 BUS_SET_OOM (error);
2091 cprd->connections = connections;
2093 bus_expire_list_unlink (connections->pending_replies,
2096 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2104 * Note that this is fairly fragile; in particular, don't try to use
2105 * one transaction across any main loop iterations.
2110 BusTransaction *transaction;
2111 DBusMessage *message;
2112 DBusPreallocatedSend *preallocated;
2117 BusTransactionCancelFunction cancel_function;
2118 DBusFreeFunction free_data_function;
2122 struct BusTransaction
2124 DBusList *connections;
2125 BusContext *context;
2126 DBusList *cancel_hooks;
2130 message_to_send_free (DBusConnection *connection,
2131 MessageToSend *to_send)
2133 if (to_send->message)
2134 dbus_message_unref (to_send->message);
2136 if (to_send->preallocated)
2137 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2139 dbus_free (to_send);
2143 cancel_hook_cancel (void *element,
2146 CancelHook *ch = element;
2148 _dbus_verbose ("Running transaction cancel hook\n");
2150 if (ch->cancel_function)
2151 (* ch->cancel_function) (ch->data);
2155 cancel_hook_free (void *element,
2158 CancelHook *ch = element;
2160 if (ch->free_data_function)
2161 (* ch->free_data_function) (ch->data);
2167 free_cancel_hooks (BusTransaction *transaction)
2169 _dbus_list_foreach (&transaction->cancel_hooks,
2170 cancel_hook_free, NULL);
2172 _dbus_list_clear (&transaction->cancel_hooks);
2176 bus_transaction_new (BusContext *context)
2178 BusTransaction *transaction;
2180 transaction = dbus_new0 (BusTransaction, 1);
2181 if (transaction == NULL)
2184 transaction->context = context;
2190 bus_transaction_get_context (BusTransaction *transaction)
2192 return transaction->context;
2196 * Reserve enough memory to capture the given message if the
2197 * transaction goes through.
2200 bus_transaction_capture (BusTransaction *transaction,
2201 DBusConnection *sender,
2202 DBusConnection *addressed_recipient,
2203 DBusMessage *message)
2205 BusConnections *connections;
2208 DBusList *recipients = NULL;
2209 dbus_bool_t ret = FALSE;
2211 connections = bus_context_get_connections (transaction->context);
2213 /* shortcut: don't compose the message unless someone wants it */
2214 if (connections->monitors == NULL)
2217 mm = connections->monitor_matchmaker;
2218 /* This is non-null if there has ever been a monitor - we don't GC it.
2219 * There's little point, since there is up to 1 per process. */
2220 _dbus_assert (mm != NULL);
2222 if (!bus_matchmaker_get_recipients (mm, connections, sender,
2223 addressed_recipient, message, &recipients))
2226 for (link = _dbus_list_get_first_link (&recipients);
2228 link = _dbus_list_get_next_link (&recipients, link))
2230 DBusConnection *recipient = link->data;
2232 if (!bus_transaction_send (transaction, recipient, message))
2239 _dbus_list_clear (&recipients);
2244 bus_transaction_capture_error_reply (BusTransaction *transaction,
2245 DBusConnection *addressed_recipient,
2246 const DBusError *error,
2247 DBusMessage *in_reply_to)
2249 BusConnections *connections;
2251 dbus_bool_t ret = FALSE;
2253 _dbus_assert (error != NULL);
2254 _DBUS_ASSERT_ERROR_IS_SET (error);
2256 connections = bus_context_get_connections (transaction->context);
2258 /* shortcut: don't compose the message unless someone wants it */
2259 if (connections->monitors == NULL)
2262 reply = dbus_message_new_error (in_reply_to,
2269 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2272 ret = bus_transaction_capture (transaction, NULL, addressed_recipient, reply);
2275 dbus_message_unref (reply);
2280 bus_transaction_send_from_driver (BusTransaction *transaction,
2281 DBusConnection *connection,
2282 DBusMessage *message)
2284 DBusError error = DBUS_ERROR_INIT;
2286 /* We have to set the sender to the driver, and have
2287 * to check security policy since it was not done in
2290 _dbus_verbose ("Sending %s %s %s from driver\n",
2291 dbus_message_get_interface (message) ?
2292 dbus_message_get_interface (message) : "(no interface)",
2293 dbus_message_get_member (message) ?
2294 dbus_message_get_member (message) : "(no member)",
2295 dbus_message_get_error_name (message) ?
2296 dbus_message_get_error_name (message) : "(no error name)");
2298 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2301 if (bus_connection_is_active (connection))
2303 if (!dbus_message_set_destination (message,
2304 bus_connection_get_name (connection)))
2308 /* bus driver never wants a reply */
2309 dbus_message_set_no_reply (message, TRUE);
2311 /* Capture it for monitors, even if the real recipient's receive policy
2312 * does not allow it to receive this message from us (which would be odd).
2314 if (!bus_transaction_capture (transaction, NULL, connection, message))
2317 /* If security policy doesn't allow the message, we would silently
2318 * eat it; the driver doesn't care about getting a reply. However,
2319 * if we're actively capturing messages, it's nice to log that we
2320 * tried to send it and did not allow ourselves to do so.
2322 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2324 NULL, connection, connection, message, &error))
2326 if (!bus_transaction_capture_error_reply (transaction, connection,
2329 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2330 "message from dbus-daemon rejected but not enough "
2331 "memory to capture it");
2334 /* This is not fatal to the transaction so silently eat the disallowed
2335 * message (see reasoning above) */
2336 dbus_error_free (&error);
2340 return bus_transaction_send (transaction, connection, message);
2344 bus_transaction_send (BusTransaction *transaction,
2345 DBusConnection *connection,
2346 DBusMessage *message)
2348 MessageToSend *to_send;
2349 BusConnectionData *d;
2352 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2353 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2354 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2356 dbus_message_get_interface (message) ?
2357 dbus_message_get_interface (message) : "(unset)",
2358 dbus_message_get_member (message) ?
2359 dbus_message_get_member (message) : "(unset)",
2360 dbus_message_get_error_name (message) ?
2361 dbus_message_get_error_name (message) : "(unset)",
2362 dbus_connection_get_is_connected (connection) ?
2363 "" : " (disconnected)");
2365 _dbus_assert (dbus_message_get_sender (message) != NULL);
2367 if (!dbus_connection_get_is_connected (connection))
2368 return TRUE; /* silently ignore disconnected connections */
2370 d = BUS_CONNECTION_DATA (connection);
2371 _dbus_assert (d != NULL);
2373 to_send = dbus_new (MessageToSend, 1);
2374 if (to_send == NULL)
2379 to_send->preallocated = dbus_connection_preallocate_send (connection);
2380 if (to_send->preallocated == NULL)
2382 dbus_free (to_send);
2386 dbus_message_ref (message);
2387 to_send->message = message;
2388 to_send->transaction = transaction;
2390 _dbus_verbose ("about to prepend message\n");
2392 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2394 message_to_send_free (connection, to_send);
2398 _dbus_verbose ("prepended message\n");
2400 /* See if we already had this connection in the list
2401 * for this transaction. If we have a pending message,
2402 * then we should already be in transaction->connections
2404 link = _dbus_list_get_first_link (&d->transaction_messages);
2405 _dbus_assert (link->data == to_send);
2406 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2407 while (link != NULL)
2409 MessageToSend *m = link->data;
2410 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2412 if (m->transaction == transaction)
2420 if (!_dbus_list_prepend (&transaction->connections, connection))
2422 _dbus_list_remove (&d->transaction_messages, to_send);
2423 message_to_send_free (connection, to_send);
2432 transaction_free (BusTransaction *transaction)
2434 _dbus_assert (transaction->connections == NULL);
2436 free_cancel_hooks (transaction);
2438 dbus_free (transaction);
2442 connection_cancel_transaction (DBusConnection *connection,
2443 BusTransaction *transaction)
2446 BusConnectionData *d;
2448 d = BUS_CONNECTION_DATA (connection);
2449 _dbus_assert (d != NULL);
2451 link = _dbus_list_get_first_link (&d->transaction_messages);
2452 while (link != NULL)
2454 MessageToSend *m = link->data;
2455 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2457 if (m->transaction == transaction)
2459 _dbus_list_remove_link (&d->transaction_messages,
2462 message_to_send_free (connection, m);
2470 bus_transaction_cancel_and_free (BusTransaction *transaction)
2472 DBusConnection *connection;
2474 _dbus_verbose ("TRANSACTION: cancelled\n");
2476 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2477 connection_cancel_transaction (connection, transaction);
2479 _dbus_list_foreach (&transaction->cancel_hooks,
2480 cancel_hook_cancel, NULL);
2482 transaction_free (transaction);
2486 connection_execute_transaction (DBusConnection *connection,
2487 BusTransaction *transaction)
2490 BusConnectionData *d;
2492 d = BUS_CONNECTION_DATA (connection);
2493 _dbus_assert (d != NULL);
2495 /* Send the queue in order (FIFO) */
2496 link = _dbus_list_get_last_link (&d->transaction_messages);
2497 while (link != NULL)
2499 MessageToSend *m = link->data;
2500 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2502 if (m->transaction == transaction)
2504 _dbus_list_remove_link (&d->transaction_messages,
2507 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2509 dbus_connection_send_preallocated (connection,
2514 m->preallocated = NULL; /* so we don't double-free it */
2516 message_to_send_free (connection, m);
2524 bus_transaction_execute_and_free (BusTransaction *transaction)
2526 /* For each connection in transaction->connections
2529 DBusConnection *connection;
2531 _dbus_verbose ("TRANSACTION: executing\n");
2533 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2534 connection_execute_transaction (connection, transaction);
2536 transaction_free (transaction);
2540 bus_connection_remove_transactions (DBusConnection *connection)
2542 MessageToSend *to_send;
2543 BusConnectionData *d;
2545 d = BUS_CONNECTION_DATA (connection);
2546 _dbus_assert (d != NULL);
2548 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2550 /* only has an effect for the first MessageToSend listing this transaction */
2551 _dbus_list_remove (&to_send->transaction->connections,
2554 _dbus_list_remove (&d->transaction_messages, to_send);
2555 message_to_send_free (connection, to_send);
2560 * Converts the DBusError to a message reply
2563 bus_transaction_send_error_reply (BusTransaction *transaction,
2564 DBusConnection *connection,
2565 const DBusError *error,
2566 DBusMessage *in_reply_to)
2570 _dbus_assert (error != NULL);
2571 _DBUS_ASSERT_ERROR_IS_SET (error);
2573 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2574 error->name, error->message);
2576 reply = dbus_message_new_error (in_reply_to,
2582 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2584 dbus_message_unref (reply);
2588 dbus_message_unref (reply);
2594 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2595 BusTransactionCancelFunction cancel_function,
2597 DBusFreeFunction free_data_function)
2601 ch = dbus_new (CancelHook, 1);
2605 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2606 cancel_function, data);
2608 ch->cancel_function = cancel_function;
2610 ch->free_data_function = free_data_function;
2612 /* It's important that the hooks get run in reverse order that they
2615 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2625 bus_connections_get_n_active (BusConnections *connections)
2627 return connections->n_completed;
2631 bus_connections_get_n_incomplete (BusConnections *connections)
2633 return connections->n_incomplete;
2636 #ifdef DBUS_ENABLE_STATS
2638 bus_connections_get_total_match_rules (BusConnections *connections)
2640 return connections->total_match_rules;
2644 bus_connections_get_peak_match_rules (BusConnections *connections)
2646 return connections->peak_match_rules;
2650 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2652 return connections->peak_match_rules_per_conn;
2656 bus_connections_get_total_bus_names (BusConnections *connections)
2658 return connections->total_bus_names;
2662 bus_connections_get_peak_bus_names (BusConnections *connections)
2664 return connections->peak_bus_names;
2668 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2670 return connections->peak_bus_names_per_conn;
2674 bus_connection_get_peak_match_rules (DBusConnection *connection)
2676 BusConnectionData *d;
2678 d = BUS_CONNECTION_DATA (connection);
2679 _dbus_assert(d != NULL);
2681 return d->peak_match_rules;
2685 bus_connection_get_peak_bus_names (DBusConnection *connection)
2687 BusConnectionData *d;
2689 d = BUS_CONNECTION_DATA (connection);
2690 _dbus_assert(d != NULL);
2692 return d->peak_bus_names;
2694 #endif /* DBUS_ENABLE_STATS */
2697 bus_connection_is_monitor (DBusConnection *connection)
2699 BusConnectionData *d;
2701 d = BUS_CONNECTION_DATA (connection);
2702 _dbus_assert(d != NULL);
2704 return d->link_in_monitors != NULL;
2708 bcd_add_monitor_rules (BusConnectionData *d,
2709 DBusConnection *connection,
2712 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2717 mm = bus_matchmaker_new ();
2722 d->connections->monitor_matchmaker = mm;
2725 for (iter = _dbus_list_get_first_link (rules);
2727 iter = _dbus_list_get_next_link (rules, iter))
2729 if (!bus_matchmaker_add_rule (mm, iter->data))
2731 bus_matchmaker_disconnected (mm, connection);
2740 bcd_drop_monitor_rules (BusConnectionData *d,
2741 DBusConnection *connection)
2743 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2746 bus_matchmaker_disconnected (mm, connection);
2750 bus_connection_be_monitor (DBusConnection *connection,
2751 BusTransaction *transaction,
2755 BusConnectionData *d;
2760 d = BUS_CONNECTION_DATA (connection);
2761 _dbus_assert (d != NULL);
2763 link = _dbus_list_alloc_link (connection);
2767 BUS_SET_OOM (error);
2771 if (!bcd_add_monitor_rules (d, connection, rules))
2773 _dbus_list_free_link (link);
2774 BUS_SET_OOM (error);
2778 /* release all its names */
2779 if (!_dbus_list_copy (&d->services_owned, &tmp))
2781 bcd_drop_monitor_rules (d, connection);
2782 _dbus_list_free_link (link);
2783 BUS_SET_OOM (error);
2787 for (iter = _dbus_list_get_first_link (&tmp);
2789 iter = _dbus_list_get_next_link (&tmp, iter))
2791 BusService *service = iter->data;
2793 /* This call is transactional: if there isn't enough memory to
2794 * do everything, then the service gets all its names back when
2795 * the transaction is cancelled due to OOM. */
2796 if (!bus_service_remove_owner (service, connection, transaction, error))
2798 bcd_drop_monitor_rules (d, connection);
2799 _dbus_list_free_link (link);
2800 _dbus_list_clear (&tmp);
2805 /* We have now done everything that can fail, so there is no problem
2806 * with doing the irrevocable stuff. */
2808 _dbus_list_clear (&tmp);
2810 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2811 "Connection %s (%s) became a monitor.", d->name,
2812 d->cached_loginfo_string);
2814 if (d->n_match_rules > 0)
2818 mm = bus_context_get_matchmaker (d->connections->context);
2819 bus_matchmaker_disconnected (mm, connection);
2822 /* flag it as a monitor */
2823 d->link_in_monitors = link;
2824 _dbus_list_append_link (&d->connections->monitors, link);
2826 /* it isn't allowed to reply, and it is no longer relevant whether it
2827 * receives replies */
2828 bus_connection_drop_pending_replies (d->connections, connection);