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>
39 /* Trim executed commands to this length; we want to keep logs readable */
40 #define MAX_LOG_COMMAND_LEN 50
42 static void bus_connection_remove_transactions (DBusConnection *connection);
46 BusExpireItem expire_item;
48 DBusConnection *will_get_reply;
49 DBusConnection *will_send_reply;
51 dbus_uint32_t reply_serial;
58 DBusList *completed; /**< List of all completed connections */
59 int n_completed; /**< Length of completed list */
60 DBusList *incomplete; /**< List of all not-yet-active connections */
61 int n_incomplete; /**< Length of incomplete list */
63 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
64 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
65 int stamp; /**< Incrementing number */
66 BusExpireList *pending_replies; /**< List of pending replies */
68 /** List of all monitoring connections, a subset of completed.
69 * Each member is a #DBusConnection. */
71 BusMatchmaker *monitor_matchmaker;
73 #ifdef DBUS_ENABLE_STATS
74 int total_match_rules;
76 int peak_match_rules_per_conn;
80 int peak_bus_names_per_conn;
84 static dbus_int32_t connection_data_slot = -1;
88 BusConnections *connections;
89 DBusList *link_in_connection_list;
90 DBusConnection *connection;
91 DBusList *services_owned;
93 DBusList *match_rules;
96 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
97 DBusMessage *oom_message;
98 DBusPreallocatedSend *oom_preallocated;
99 BusClientPolicy *policy;
101 char *cached_loginfo_string;
102 BusSELinuxID *selinux_id;
103 BusAppArmorConfinement *apparmor_confinement;
105 long connection_tv_sec; /**< Time when we connected (seconds component) */
106 long connection_tv_usec; /**< Time when we connected (microsec component) */
107 int stamp; /**< connections->stamp last time we were traversed */
109 #ifdef DBUS_ENABLE_STATS
110 int peak_match_rules;
113 int n_pending_unix_fds;
114 DBusTimeout *pending_unix_fds_timeout;
116 /** non-NULL if and only if this is a monitor */
117 DBusList *link_in_monitors;
120 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
124 static void bus_connection_drop_pending_replies (BusConnections *connections,
125 DBusConnection *connection);
127 static dbus_bool_t expire_incomplete_timeout (void *data);
129 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
132 connection_get_loop (DBusConnection *connection)
134 BusConnectionData *d;
136 d = BUS_CONNECTION_DATA (connection);
138 return bus_context_get_loop (d->connections->context);
143 get_connections_for_uid (BusConnections *connections,
149 /* val is NULL is 0 when it isn't in the hash yet */
151 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
154 current_count = _DBUS_POINTER_TO_INT (val);
156 return current_count;
160 adjust_connections_for_uid (BusConnections *connections,
166 current_count = get_connections_for_uid (connections, uid);
168 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
169 ": was %d adjustment %d making %d\n",
170 uid, current_count, adjustment, current_count + adjustment);
172 _dbus_assert (current_count >= 0);
174 current_count += adjustment;
176 _dbus_assert (current_count >= 0);
178 if (current_count == 0)
180 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
187 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
188 uid, _DBUS_INT_TO_POINTER (current_count));
190 /* only positive adjustment can fail as otherwise
191 * a hash entry should already exist
193 _dbus_assert (adjustment > 0 ||
194 (adjustment <= 0 && retval));
201 bus_connection_disconnected (DBusConnection *connection)
203 BusConnectionData *d;
205 BusMatchmaker *matchmaker;
207 d = BUS_CONNECTION_DATA (connection);
208 _dbus_assert (d != NULL);
210 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
211 d->name ? d->name : "(inactive)");
213 /* Delete our match rules */
214 if (d->n_match_rules > 0)
216 matchmaker = bus_context_get_matchmaker (d->connections->context);
217 bus_matchmaker_disconnected (matchmaker, connection);
220 /* Drop any service ownership. Unfortunately, this requires
221 * memory allocation and there doesn't seem to be a good way to
222 * handle it other than sleeping; we can't "fail" the operation of
223 * disconnecting a client, and preallocating a broadcast "service is
224 * now gone" message for every client-service pair seems kind of
227 while ((service = _dbus_list_get_last (&d->services_owned)))
229 BusTransaction *transaction;
234 dbus_error_init (&error);
236 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
237 _dbus_wait_for_memory ();
239 if (!bus_service_remove_owner (service, connection,
240 transaction, &error))
242 _DBUS_ASSERT_ERROR_IS_SET (&error);
244 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
246 dbus_error_free (&error);
247 bus_transaction_cancel_and_free (transaction);
248 _dbus_wait_for_memory ();
253 _dbus_verbose ("Failed to remove service owner: %s %s\n",
254 error.name, error.message);
255 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
259 bus_transaction_execute_and_free (transaction);
262 bus_dispatch_remove_connection (connection);
264 /* no more watching */
265 if (!dbus_connection_set_watch_functions (connection,
269 _dbus_assert_not_reached ("setting watch functions to NULL failed");
271 if (!dbus_connection_set_timeout_functions (connection,
275 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
277 dbus_connection_set_unix_user_function (connection,
279 dbus_connection_set_windows_user_function (connection,
282 dbus_connection_set_dispatch_status_function (connection,
285 if (d->pending_unix_fds_timeout)
287 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
288 d->pending_unix_fds_timeout);
289 _dbus_timeout_unref (d->pending_unix_fds_timeout);
291 d->pending_unix_fds_timeout = NULL;
292 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
294 bus_connection_remove_transactions (connection);
296 if (d->link_in_monitors != NULL)
298 BusMatchmaker *mm = d->connections->monitor_matchmaker;
301 bus_matchmaker_disconnected (mm, connection);
303 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
304 d->link_in_monitors = NULL;
307 if (d->link_in_connection_list != NULL)
313 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
314 d->link_in_connection_list = NULL;
315 d->connections->n_completed -= 1;
317 if (dbus_connection_get_unix_user (connection, &uid))
319 if (!adjust_connections_for_uid (d->connections,
321 _dbus_assert_not_reached ("adjusting downward should never fail");
326 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
327 d->link_in_connection_list = NULL;
328 d->connections->n_incomplete -= 1;
330 /* If we have dropped below the max. number of incomplete
331 * connections, start accept()ing again */
332 bus_context_check_all_watches (d->connections->context);
335 _dbus_assert (d->connections->n_incomplete >= 0);
336 _dbus_assert (d->connections->n_completed >= 0);
339 bus_connection_drop_pending_replies (d->connections, connection);
341 /* frees "d" as side effect */
342 dbus_connection_set_data (connection,
343 connection_data_slot,
346 dbus_connection_unref (connection);
350 add_connection_watch (DBusWatch *watch,
353 DBusConnection *connection = data;
355 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
359 remove_connection_watch (DBusWatch *watch,
362 DBusConnection *connection = data;
364 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
368 toggle_connection_watch (DBusWatch *watch,
371 DBusConnection *connection = data;
373 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
377 add_connection_timeout (DBusTimeout *timeout,
380 DBusConnection *connection = data;
382 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
386 remove_connection_timeout (DBusTimeout *timeout,
389 DBusConnection *connection = data;
391 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
395 dispatch_status_function (DBusConnection *connection,
396 DBusDispatchStatus new_status,
399 DBusLoop *loop = data;
401 if (new_status != DBUS_DISPATCH_COMPLETE)
403 while (!_dbus_loop_queue_dispatch (loop, connection))
404 _dbus_wait_for_memory ();
409 allow_unix_user_function (DBusConnection *connection,
413 BusConnectionData *d;
415 d = BUS_CONNECTION_DATA (connection);
417 _dbus_assert (d != NULL);
419 return bus_context_allow_unix_user (d->connections->context, uid);
423 free_connection_data (void *data)
425 BusConnectionData *d = data;
427 /* services_owned should be NULL since we should be disconnected */
428 _dbus_assert (d->services_owned == NULL);
429 _dbus_assert (d->n_services_owned == 0);
431 _dbus_assert (d->transaction_messages == NULL);
433 if (d->oom_preallocated)
434 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
437 dbus_message_unref (d->oom_message);
440 bus_client_policy_unref (d->policy);
443 bus_selinux_id_unref (d->selinux_id);
445 if (d->apparmor_confinement)
446 bus_apparmor_confinement_unref (d->apparmor_confinement);
448 dbus_free (d->cached_loginfo_string);
456 bus_connections_new (BusContext *context)
458 BusConnections *connections;
460 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
463 connections = dbus_new0 (BusConnections, 1);
464 if (connections == NULL)
467 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
469 if (connections->completed_by_user == NULL)
472 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
473 expire_incomplete_timeout,
475 if (connections->expire_timeout == NULL)
478 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
480 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
481 bus_context_get_reply_timeout (context),
482 bus_pending_reply_expired,
484 if (connections->pending_replies == NULL)
487 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
488 connections->expire_timeout))
491 connections->refcount = 1;
492 connections->context = context;
497 bus_expire_list_free (connections->pending_replies);
499 _dbus_timeout_unref (connections->expire_timeout);
501 _dbus_hash_table_unref (connections->completed_by_user);
503 dbus_free (connections);
505 dbus_connection_free_data_slot (&connection_data_slot);
511 bus_connections_ref (BusConnections *connections)
513 _dbus_assert (connections->refcount > 0);
514 connections->refcount += 1;
520 bus_connections_unref (BusConnections *connections)
522 _dbus_assert (connections->refcount > 0);
523 connections->refcount -= 1;
524 if (connections->refcount == 0)
526 /* drop all incomplete */
527 while (connections->incomplete != NULL)
529 DBusConnection *connection;
531 connection = connections->incomplete->data;
533 dbus_connection_ref (connection);
534 dbus_connection_close (connection);
535 bus_connection_disconnected (connection);
536 dbus_connection_unref (connection);
539 _dbus_assert (connections->n_incomplete == 0);
541 /* drop all monitors */
542 _dbus_list_clear (&connections->monitors);
544 /* drop all real connections */
545 while (connections->completed != NULL)
547 DBusConnection *connection;
549 connection = connections->completed->data;
551 dbus_connection_ref (connection);
552 dbus_connection_close (connection);
553 bus_connection_disconnected (connection);
554 dbus_connection_unref (connection);
557 _dbus_assert (connections->n_completed == 0);
559 bus_expire_list_free (connections->pending_replies);
561 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
562 connections->expire_timeout);
564 _dbus_timeout_unref (connections->expire_timeout);
566 _dbus_hash_table_unref (connections->completed_by_user);
568 if (connections->monitor_matchmaker != NULL)
569 bus_matchmaker_unref (connections->monitor_matchmaker);
571 dbus_free (connections);
573 dbus_connection_free_data_slot (&connection_data_slot);
577 /* Used for logging */
579 cache_peer_loginfo_string (BusConnectionData *d,
580 DBusConnection *connection)
582 DBusString loginfo_buf;
586 dbus_bool_t prev_added;
588 if (!_dbus_string_init (&loginfo_buf))
592 if (dbus_connection_get_unix_user (connection, &uid))
594 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
600 if (dbus_connection_get_unix_process_id (connection, &pid))
604 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
607 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
609 /* Ignore errors here; we may not have permissions to read the
611 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
612 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
616 if (dbus_connection_get_windows_user (connection, &windows_sid))
618 dbus_bool_t did_append;
619 did_append = _dbus_string_append_printf (&loginfo_buf,
620 "sid=\"%s\" ", windows_sid);
621 dbus_free (windows_sid);
626 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
629 _dbus_string_free (&loginfo_buf);
633 _dbus_string_free (&loginfo_buf);
638 check_pending_fds_cb (DBusConnection *connection)
640 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
641 int n_pending_unix_fds_old = d->n_pending_unix_fds;
642 int n_pending_unix_fds_new;
644 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
646 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
647 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
649 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
651 _dbus_timeout_set_interval (d->pending_unix_fds_timeout,
652 bus_context_get_pending_fd_timeout (d->connections->context));
653 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, TRUE);
656 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
658 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
662 d->n_pending_unix_fds = n_pending_unix_fds_new;
666 pending_unix_fds_timeout_cb (void *data)
668 DBusConnection *connection = data;
669 dbus_connection_close (connection);
674 bus_connections_setup_connection (BusConnections *connections,
675 DBusConnection *connection)
678 BusConnectionData *d;
683 d = dbus_new0 (BusConnectionData, 1);
688 d->connections = connections;
689 d->connection = connection;
691 _dbus_get_monotonic_time (&d->connection_tv_sec,
692 &d->connection_tv_usec);
694 _dbus_assert (connection_data_slot >= 0);
696 if (!dbus_connection_set_data (connection,
697 connection_data_slot,
698 d, free_connection_data))
704 dbus_connection_set_route_peer_messages (connection, TRUE);
708 dbus_error_init (&error);
709 d->selinux_id = bus_selinux_init_connection_id (connection,
711 if (dbus_error_is_set (&error))
713 /* This is a bit bogus because we pretend all errors
714 * are OOM; this is done because we know that in bus.c
715 * an OOM error disconnects the connection, which is
716 * the same thing we want on any other error.
718 dbus_error_free (&error);
722 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
724 if (dbus_error_is_set (&error))
726 /* This is a bit bogus because we pretend all errors
727 * are OOM; this is done because we know that in bus.c
728 * an OOM error disconnects the connection, which is
729 * the same thing we want on any other error.
731 dbus_error_free (&error);
735 if (!dbus_connection_set_watch_functions (connection,
736 add_connection_watch,
737 remove_connection_watch,
738 toggle_connection_watch,
743 if (!dbus_connection_set_timeout_functions (connection,
744 add_connection_timeout,
745 remove_connection_timeout,
750 /* For now we don't need to set a Windows user function because
751 * there are no policies in the config file controlling what
752 * Windows users can connect. The default 'same user that owns the
753 * bus can connect' behavior of DBusConnection is fine on Windows.
755 dbus_connection_set_unix_user_function (connection,
756 allow_unix_user_function,
759 dbus_connection_set_dispatch_status_function (connection,
760 dispatch_status_function,
761 bus_context_get_loop (connections->context),
764 d->link_in_connection_list = _dbus_list_alloc_link (connection);
765 if (d->link_in_connection_list == NULL)
768 /* Setup the connection with the dispatcher */
769 if (!bus_dispatch_add_connection (connection))
772 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
774 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
776 bus_dispatch_remove_connection (connection);
781 /* Setup pending fds timeout (see #80559) */
782 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
783 pending_unix_fds_timeout_cb,
785 if (d->pending_unix_fds_timeout == NULL)
788 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
789 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
790 d->pending_unix_fds_timeout))
793 _dbus_connection_set_pending_fds_function (connection,
794 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
797 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
798 connections->n_incomplete += 1;
800 dbus_connection_ref (connection);
802 bus_connections_expire_incomplete (connections);
804 /* The listening socket is removed from the main loop,
805 * i.e. does not accept(), while n_incomplete is at its
806 * maximum value; so we shouldn't get here in that case */
807 _dbus_assert (connections->n_incomplete <=
808 bus_context_get_max_incomplete_connections (connections->context));
810 /* If we have the maximum number of incomplete connections,
811 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
812 bus_context_check_all_watches (d->connections->context);
820 bus_selinux_id_unref (d->selinux_id);
821 d->selinux_id = NULL;
823 if (d->apparmor_confinement)
824 bus_apparmor_confinement_unref (d->apparmor_confinement);
825 d->apparmor_confinement = NULL;
827 if (!dbus_connection_set_watch_functions (connection,
831 _dbus_assert_not_reached ("setting watch functions to NULL failed");
833 if (!dbus_connection_set_timeout_functions (connection,
837 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
839 dbus_connection_set_unix_user_function (connection,
842 dbus_connection_set_windows_user_function (connection,
845 dbus_connection_set_dispatch_status_function (connection,
848 if (d->pending_unix_fds_timeout)
849 _dbus_timeout_unref (d->pending_unix_fds_timeout);
851 d->pending_unix_fds_timeout = NULL;
853 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
855 if (d->link_in_connection_list != NULL)
857 _dbus_assert (d->link_in_connection_list->next == NULL);
858 _dbus_assert (d->link_in_connection_list->prev == NULL);
859 _dbus_list_free_link (d->link_in_connection_list);
860 d->link_in_connection_list = NULL;
863 if (!dbus_connection_set_data (connection,
864 connection_data_slot,
866 _dbus_assert_not_reached ("failed to set connection data to null");
868 /* "d" has now been freed */
875 bus_connections_expire_incomplete (BusConnections *connections)
881 if (connections->incomplete != NULL)
883 long tv_sec, tv_usec;
887 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
888 auth_timeout = bus_context_get_auth_timeout (connections->context);
890 link = _dbus_list_get_first_link (&connections->incomplete);
893 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
894 DBusConnection *connection;
895 BusConnectionData *d;
898 connection = link->data;
900 d = BUS_CONNECTION_DATA (connection);
902 _dbus_assert (d != NULL);
904 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
905 d->connection_tv_usec,
908 if (elapsed >= (double) auth_timeout)
910 /* Unfortunately, we can't identify the connection: it doesn't
911 * have a unique name yet, we don't know its uid/pid yet,
913 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
914 "Connection has not authenticated soon enough, closing it "
915 "(auth_timeout=%dms, elapsed: %.0fms)",
916 auth_timeout, elapsed);
918 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
919 dbus_connection_close (connection);
923 /* We can end the loop, since the connections are in oldest-first order */
924 next_interval = ((double)auth_timeout) - elapsed;
925 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
926 connection, next_interval);
935 bus_expire_timeout_set_interval (connections->expire_timeout,
940 expire_incomplete_timeout (void *data)
942 BusConnections *connections = data;
944 _dbus_verbose ("Running\n");
946 /* note that this may remove the timeout */
947 bus_connections_expire_incomplete (connections);
953 bus_connection_get_unix_groups (DBusConnection *connection,
954 unsigned long **groups,
963 if (dbus_connection_get_unix_user (connection, &uid))
965 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
967 _dbus_verbose ("Did not get any groups for UID %lu\n",
973 _dbus_verbose ("Got %d groups for UID %lu\n",
979 return TRUE; /* successfully got 0 groups */
983 bus_connection_is_in_unix_group (DBusConnection *connection,
987 unsigned long *group_ids;
990 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
995 while (i < n_group_ids)
997 if (group_ids[i] == gid)
999 dbus_free (group_ids);
1005 dbus_free (group_ids);
1010 bus_connection_get_loginfo (DBusConnection *connection)
1012 BusConnectionData *d;
1014 d = BUS_CONNECTION_DATA (connection);
1016 if (!bus_connection_is_active (connection))
1018 return d->cached_loginfo_string;
1022 bus_connection_get_policy (DBusConnection *connection)
1024 BusConnectionData *d;
1026 d = BUS_CONNECTION_DATA (connection);
1028 _dbus_assert (d != NULL);
1029 _dbus_assert (d->policy != NULL);
1035 foreach_active (BusConnections *connections,
1036 BusConnectionForeachFunction function,
1041 link = _dbus_list_get_first_link (&connections->completed);
1042 while (link != NULL)
1044 DBusConnection *connection = link->data;
1045 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1047 if (!(* function) (connection, data))
1057 foreach_inactive (BusConnections *connections,
1058 BusConnectionForeachFunction function,
1063 link = _dbus_list_get_first_link (&connections->incomplete);
1064 while (link != NULL)
1066 DBusConnection *connection = link->data;
1067 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1069 if (!(* function) (connection, data))
1079 * Calls function on each active connection; if the function returns
1080 * #FALSE, stops iterating. Active connections are authenticated
1081 * and have sent a Hello message.
1083 * @param connections the connections object
1084 * @param function the function
1085 * @param data data to pass to it as a second arg
1088 bus_connections_foreach_active (BusConnections *connections,
1089 BusConnectionForeachFunction function,
1092 foreach_active (connections, function, data);
1096 * Calls function on each connection; if the function returns
1097 * #FALSE, stops iterating.
1099 * @param connections the connections object
1100 * @param function the function
1101 * @param data data to pass to it as a second arg
1104 bus_connections_foreach (BusConnections *connections,
1105 BusConnectionForeachFunction function,
1108 if (!foreach_active (connections, function, data))
1111 foreach_inactive (connections, function, data);
1115 bus_connections_get_context (BusConnections *connections)
1117 return connections->context;
1121 * This is used to avoid covering the same connection twice when
1122 * traversing connections. Note that it assumes we will
1123 * bus_connection_mark_stamp() each connection at least once per
1124 * INT_MAX increments of the global stamp, or wraparound would break
1128 bus_connections_increment_stamp (BusConnections *connections)
1130 connections->stamp += 1;
1133 /* Mark connection with current stamp, return TRUE if it
1134 * didn't already have that stamp
1137 bus_connection_mark_stamp (DBusConnection *connection)
1139 BusConnectionData *d;
1141 d = BUS_CONNECTION_DATA (connection);
1143 _dbus_assert (d != NULL);
1145 if (d->stamp == d->connections->stamp)
1149 d->stamp = d->connections->stamp;
1155 bus_connection_get_context (DBusConnection *connection)
1157 BusConnectionData *d;
1159 d = BUS_CONNECTION_DATA (connection);
1161 _dbus_assert (d != NULL);
1163 return d->connections->context;
1167 bus_connection_get_connections (DBusConnection *connection)
1169 BusConnectionData *d;
1171 d = BUS_CONNECTION_DATA (connection);
1173 _dbus_assert (d != NULL);
1175 return d->connections;
1179 bus_connection_get_registry (DBusConnection *connection)
1181 BusConnectionData *d;
1183 d = BUS_CONNECTION_DATA (connection);
1185 _dbus_assert (d != NULL);
1187 return bus_context_get_registry (d->connections->context);
1191 bus_connection_get_activation (DBusConnection *connection)
1193 BusConnectionData *d;
1195 d = BUS_CONNECTION_DATA (connection);
1197 _dbus_assert (d != NULL);
1199 return bus_context_get_activation (d->connections->context);
1203 bus_connection_get_matchmaker (DBusConnection *connection)
1205 BusConnectionData *d;
1207 d = BUS_CONNECTION_DATA (connection);
1209 _dbus_assert (d != NULL);
1211 return bus_context_get_matchmaker (d->connections->context);
1215 bus_connection_get_selinux_id (DBusConnection *connection)
1217 BusConnectionData *d;
1219 d = BUS_CONNECTION_DATA (connection);
1221 _dbus_assert (d != NULL);
1223 return d->selinux_id;
1226 BusAppArmorConfinement*
1227 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1229 BusConnectionData *d;
1231 d = BUS_CONNECTION_DATA (connection);
1233 _dbus_assert (d != NULL);
1235 bus_apparmor_confinement_ref (d->apparmor_confinement);
1236 return d->apparmor_confinement;
1240 * Checks whether the connection is registered with the message bus.
1242 * @param connection the connection
1243 * @returns #TRUE if we're an active message bus participant
1246 bus_connection_is_active (DBusConnection *connection)
1248 BusConnectionData *d;
1250 d = BUS_CONNECTION_DATA (connection);
1252 return d != NULL && d->name != NULL;
1256 bus_connection_preallocate_oom_error (DBusConnection *connection)
1258 DBusMessage *message;
1259 DBusPreallocatedSend *preallocated;
1260 BusConnectionData *d;
1262 d = BUS_CONNECTION_DATA (connection);
1264 _dbus_assert (d != NULL);
1266 if (d->oom_preallocated != NULL)
1269 preallocated = dbus_connection_preallocate_send (connection);
1270 if (preallocated == NULL)
1273 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1275 if (message == NULL)
1277 dbus_connection_free_preallocated_send (connection, preallocated);
1281 /* d->name may be NULL, but that is OK */
1282 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1283 !dbus_message_set_destination (message, d->name) ||
1284 !dbus_message_set_sender (message,
1287 dbus_connection_free_preallocated_send (connection, preallocated);
1288 dbus_message_unref (message);
1292 /* set reply serial to placeholder value just so space is already allocated
1295 if (!dbus_message_set_reply_serial (message, 14))
1297 dbus_connection_free_preallocated_send (connection, preallocated);
1298 dbus_message_unref (message);
1302 d->oom_message = message;
1303 d->oom_preallocated = preallocated;
1309 bus_connection_send_oom_error (DBusConnection *connection,
1310 DBusMessage *in_reply_to)
1312 BusConnectionData *d;
1314 d = BUS_CONNECTION_DATA (connection);
1316 _dbus_assert (d != NULL);
1317 _dbus_assert (d->oom_message != NULL);
1319 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1320 "dbus-daemon transaction failed (OOM), sending error to "
1321 "sender %s", bus_connection_get_loginfo (connection));
1323 /* should always succeed since we set it to a placeholder earlier */
1324 if (!dbus_message_set_reply_serial (d->oom_message,
1325 dbus_message_get_serial (in_reply_to)))
1326 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1328 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1330 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1331 d->oom_message, NULL);
1333 dbus_message_unref (d->oom_message);
1334 d->oom_message = NULL;
1335 d->oom_preallocated = NULL;
1338 #ifdef DBUS_ENABLE_STATS
1340 update_peak (int *peak,
1349 bus_connection_add_match_rule_link (DBusConnection *connection,
1352 BusConnectionData *d;
1354 d = BUS_CONNECTION_DATA (connection);
1355 _dbus_assert (d != NULL);
1357 _dbus_list_append_link (&d->match_rules, link);
1359 d->n_match_rules += 1;
1361 #ifdef DBUS_ENABLE_STATS
1362 update_peak (&d->peak_match_rules, d->n_match_rules);
1363 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1365 d->connections->total_match_rules += 1;
1366 update_peak (&d->connections->peak_match_rules,
1367 d->connections->total_match_rules);
1372 bus_connection_add_match_rule (DBusConnection *connection,
1377 link = _dbus_list_alloc_link (rule);
1382 bus_connection_add_match_rule_link (connection, link);
1388 bus_connection_remove_match_rule (DBusConnection *connection,
1391 BusConnectionData *d;
1393 d = BUS_CONNECTION_DATA (connection);
1394 _dbus_assert (d != NULL);
1396 _dbus_list_remove_last (&d->match_rules, rule);
1398 d->n_match_rules -= 1;
1399 _dbus_assert (d->n_match_rules >= 0);
1401 #ifdef DBUS_ENABLE_STATS
1402 d->connections->total_match_rules -= 1;
1407 bus_connection_get_n_match_rules (DBusConnection *connection)
1409 BusConnectionData *d;
1411 d = BUS_CONNECTION_DATA (connection);
1412 _dbus_assert (d != NULL);
1414 return d->n_match_rules;
1418 bus_connection_add_owned_service_link (DBusConnection *connection,
1421 BusConnectionData *d;
1423 d = BUS_CONNECTION_DATA (connection);
1424 _dbus_assert (d != NULL);
1426 _dbus_list_append_link (&d->services_owned, link);
1428 d->n_services_owned += 1;
1430 #ifdef DBUS_ENABLE_STATS
1431 update_peak (&d->peak_bus_names, d->n_services_owned);
1432 update_peak (&d->connections->peak_bus_names_per_conn,
1433 d->n_services_owned);
1435 d->connections->total_bus_names += 1;
1436 update_peak (&d->connections->peak_bus_names,
1437 d->connections->total_bus_names);
1442 bus_connection_add_owned_service (DBusConnection *connection,
1443 BusService *service)
1447 link = _dbus_list_alloc_link (service);
1452 bus_connection_add_owned_service_link (connection, link);
1458 bus_connection_remove_owned_service (DBusConnection *connection,
1459 BusService *service)
1461 BusConnectionData *d;
1463 d = BUS_CONNECTION_DATA (connection);
1464 _dbus_assert (d != NULL);
1466 _dbus_list_remove_last (&d->services_owned, service);
1468 d->n_services_owned -= 1;
1469 _dbus_assert (d->n_services_owned >= 0);
1471 #ifdef DBUS_ENABLE_STATS
1472 d->connections->total_bus_names -= 1;
1477 bus_connection_get_n_services_owned (DBusConnection *connection)
1479 BusConnectionData *d;
1481 d = BUS_CONNECTION_DATA (connection);
1482 _dbus_assert (d != NULL);
1484 return d->n_services_owned;
1488 bus_connection_complete (DBusConnection *connection,
1489 const DBusString *name,
1492 BusConnectionData *d;
1495 d = BUS_CONNECTION_DATA (connection);
1496 _dbus_assert (d != NULL);
1497 _dbus_assert (d->name == NULL);
1498 _dbus_assert (d->policy == NULL);
1500 _dbus_assert (!bus_connection_is_active (connection));
1502 if (!_dbus_string_copy_data (name, &d->name))
1504 BUS_SET_OOM (error);
1508 _dbus_assert (d->name != NULL);
1510 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1512 d->policy = bus_context_create_client_policy (d->connections->context,
1516 /* we may have a NULL policy on OOM or error getting list of
1517 * groups for a user. In the latter case we don't handle it so
1518 * well currently, as it will just keep failing over and over.
1521 if (d->policy == NULL)
1523 _dbus_verbose ("Failed to create security policy for connection %p\n",
1525 _DBUS_ASSERT_ERROR_IS_SET (error);
1526 dbus_free (d->name);
1531 if (dbus_connection_get_unix_user (connection, &uid))
1533 if (!adjust_connections_for_uid (d->connections,
1538 /* Create and cache a string which holds information about the
1539 * peer process; used for logging purposes.
1541 if (!cache_peer_loginfo_string (d, connection))
1544 /* Now the connection is active, move it between lists */
1545 _dbus_list_unlink (&d->connections->incomplete,
1546 d->link_in_connection_list);
1547 d->connections->n_incomplete -= 1;
1548 _dbus_list_append_link (&d->connections->completed,
1549 d->link_in_connection_list);
1550 d->connections->n_completed += 1;
1552 _dbus_assert (d->connections->n_incomplete >= 0);
1553 _dbus_assert (d->connections->n_completed > 0);
1555 /* If we have dropped below the max. number of incomplete
1556 * connections, start accept()ing again */
1557 bus_context_check_all_watches (d->connections->context);
1559 /* See if we can remove the timeout */
1560 bus_connections_expire_incomplete (d->connections);
1562 _dbus_assert (bus_connection_is_active (connection));
1566 BUS_SET_OOM (error);
1567 dbus_free (d->name);
1570 bus_client_policy_unref (d->policy);
1576 bus_connections_reload_policy (BusConnections *connections,
1579 BusConnectionData *d;
1580 DBusConnection *connection;
1583 _dbus_assert (connections != NULL);
1584 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1586 for (link = _dbus_list_get_first_link (&(connections->completed));
1588 link = _dbus_list_get_next_link (&(connections->completed), link))
1590 connection = link->data;
1591 d = BUS_CONNECTION_DATA (connection);
1592 _dbus_assert (d != NULL);
1593 _dbus_assert (d->policy != NULL);
1595 bus_client_policy_unref (d->policy);
1596 d->policy = bus_context_create_client_policy (connections->context,
1599 if (d->policy == NULL)
1601 _dbus_verbose ("Failed to create security policy for connection %p\n",
1603 _DBUS_ASSERT_ERROR_IS_SET (error);
1612 bus_connection_get_name (DBusConnection *connection)
1614 BusConnectionData *d;
1616 d = BUS_CONNECTION_DATA (connection);
1617 _dbus_assert (d != NULL);
1623 * Check whether completing the passed-in connection would
1624 * exceed limits, and if so set error and return #FALSE
1627 bus_connections_check_limits (BusConnections *connections,
1628 DBusConnection *requesting_completion,
1633 if (connections->n_completed >=
1634 bus_context_get_max_completed_connections (connections->context))
1636 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1637 "The maximum number of active connections has been reached");
1641 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1643 if (get_connections_for_uid (connections, uid) >=
1644 bus_context_get_max_connections_per_user (connections->context))
1646 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1647 "The maximum number of active connections for UID %lu has been reached",
1657 bus_pending_reply_free (BusPendingReply *pending)
1659 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1661 pending->will_send_reply,
1662 pending->will_get_reply,
1663 pending->reply_serial);
1665 dbus_free (pending);
1669 bus_pending_reply_send_no_reply (BusConnections *connections,
1670 BusTransaction *transaction,
1671 BusPendingReply *pending)
1673 DBusMessage *message;
1674 DBusMessageIter iter;
1680 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1681 if (message == NULL)
1684 dbus_message_set_no_reply (message, TRUE);
1686 if (!dbus_message_set_reply_serial (message,
1687 pending->reply_serial))
1690 if (!dbus_message_set_error_name (message,
1691 DBUS_ERROR_NO_REPLY))
1694 /* If you change these messages, adjust test/dbus-daemon.c to match */
1695 if (pending->will_send_reply == NULL)
1696 errmsg = "Message recipient disconnected from message bus without replying";
1698 errmsg = "Message did not receive a reply (timeout by message bus)";
1700 dbus_message_iter_init_append (message, &iter);
1701 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1704 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1711 dbus_message_unref (message);
1716 bus_pending_reply_expired (BusExpireList *list,
1720 BusPendingReply *pending = link->data;
1721 BusConnections *connections = data;
1722 BusTransaction *transaction;
1724 /* No reply is forthcoming. So nuke it if we can. If not,
1725 * leave it in the list to try expiring again later when we
1729 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1731 pending->will_send_reply,
1732 pending->will_get_reply,
1733 pending->reply_serial);
1735 transaction = bus_transaction_new (connections->context);
1736 if (transaction == NULL)
1739 if (!bus_pending_reply_send_no_reply (connections,
1743 bus_transaction_cancel_and_free (transaction);
1747 bus_expire_list_remove_link (connections->pending_replies, link);
1749 bus_pending_reply_free (pending);
1750 bus_transaction_execute_and_free (transaction);
1756 bus_connection_drop_pending_replies (BusConnections *connections,
1757 DBusConnection *connection)
1759 /* The DBusConnection is almost 100% finalized here, so you can't
1760 * do anything with it except check for pointer equality
1764 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1767 link = bus_expire_list_get_first_link (connections->pending_replies);
1768 while (link != NULL)
1771 BusPendingReply *pending;
1773 next = bus_expire_list_get_next_link (connections->pending_replies,
1775 pending = link->data;
1777 if (pending->will_get_reply == connection)
1779 /* We don't need to track this pending reply anymore */
1781 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1783 pending->will_send_reply,
1784 pending->will_get_reply,
1785 pending->reply_serial);
1787 bus_expire_list_remove_link (connections->pending_replies,
1789 bus_pending_reply_free (pending);
1791 else if (pending->will_send_reply == connection)
1793 /* The reply isn't going to be sent, so set things
1794 * up so it will be expired right away
1796 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1798 pending->will_send_reply,
1799 pending->will_get_reply,
1800 pending->reply_serial);
1802 pending->will_send_reply = NULL;
1803 pending->expire_item.added_tv_sec = 0;
1804 pending->expire_item.added_tv_usec = 0;
1806 bus_expire_list_recheck_immediately (connections->pending_replies);
1816 BusPendingReply *pending;
1817 BusConnections *connections;
1818 } CancelPendingReplyData;
1821 cancel_pending_reply (void *data)
1823 CancelPendingReplyData *d = data;
1825 _dbus_verbose ("d = %p\n", d);
1827 if (!bus_expire_list_remove (d->connections->pending_replies,
1828 &d->pending->expire_item))
1829 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1831 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1835 cancel_pending_reply_data_free (void *data)
1837 CancelPendingReplyData *d = data;
1839 _dbus_verbose ("d = %p\n", d);
1841 /* d->pending should be either freed or still
1842 * in the list of pending replies (owned by someone
1850 * Record that a reply is allowed; return TRUE on success.
1853 bus_connections_expect_reply (BusConnections *connections,
1854 BusTransaction *transaction,
1855 DBusConnection *will_get_reply,
1856 DBusConnection *will_send_reply,
1857 DBusMessage *reply_to_this,
1860 BusPendingReply *pending;
1861 dbus_uint32_t reply_serial;
1863 CancelPendingReplyData *cprd;
1866 _dbus_assert (will_get_reply != NULL);
1867 _dbus_assert (will_send_reply != NULL);
1868 _dbus_assert (reply_to_this != NULL);
1870 if (dbus_message_get_no_reply (reply_to_this))
1871 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1873 reply_serial = dbus_message_get_serial (reply_to_this);
1875 link = bus_expire_list_get_first_link (connections->pending_replies);
1877 while (link != NULL)
1879 pending = link->data;
1881 if (pending->reply_serial == reply_serial &&
1882 pending->will_get_reply == will_get_reply &&
1883 pending->will_send_reply == will_send_reply)
1885 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1886 "Message has the same reply serial as a currently-outstanding existing method call");
1890 link = bus_expire_list_get_next_link (connections->pending_replies,
1892 if (pending->will_get_reply == will_get_reply)
1897 bus_context_get_max_replies_per_connection (connections->context))
1899 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1900 "The maximum number of pending replies per connection has been reached");
1904 pending = dbus_new0 (BusPendingReply, 1);
1905 if (pending == NULL)
1907 BUS_SET_OOM (error);
1911 #ifdef DBUS_ENABLE_VERBOSE_MODE
1912 /* so we can see a not-yet-added pending reply */
1913 pending->expire_item.added_tv_sec = 1;
1914 pending->expire_item.added_tv_usec = 1;
1917 pending->will_get_reply = will_get_reply;
1918 pending->will_send_reply = will_send_reply;
1919 pending->reply_serial = reply_serial;
1921 cprd = dbus_new0 (CancelPendingReplyData, 1);
1924 BUS_SET_OOM (error);
1925 bus_pending_reply_free (pending);
1929 if (!bus_expire_list_add (connections->pending_replies,
1930 &pending->expire_item))
1932 BUS_SET_OOM (error);
1934 bus_pending_reply_free (pending);
1938 if (!bus_transaction_add_cancel_hook (transaction,
1939 cancel_pending_reply,
1941 cancel_pending_reply_data_free))
1943 BUS_SET_OOM (error);
1944 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1946 bus_pending_reply_free (pending);
1950 cprd->pending = pending;
1951 cprd->connections = connections;
1953 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1954 &pending->expire_item.added_tv_usec);
1956 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1958 pending->will_send_reply,
1959 pending->will_get_reply,
1960 pending->reply_serial);
1968 BusConnections *connections;
1969 } CheckPendingReplyData;
1972 cancel_check_pending_reply (void *data)
1974 CheckPendingReplyData *d = data;
1976 _dbus_verbose ("d = %p\n",d);
1978 bus_expire_list_add_link (d->connections->pending_replies,
1984 check_pending_reply_data_free (void *data)
1986 CheckPendingReplyData *d = data;
1988 _dbus_verbose ("d = %p\n",d);
1990 if (d->link != NULL)
1992 BusPendingReply *pending = d->link->data;
1994 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1995 &pending->expire_item));
1997 bus_pending_reply_free (pending);
1998 _dbus_list_free_link (d->link);
2005 * Check whether a reply is allowed, remove BusPendingReply
2006 * if so, return TRUE if so.
2009 bus_connections_check_reply (BusConnections *connections,
2010 BusTransaction *transaction,
2011 DBusConnection *sending_reply,
2012 DBusConnection *receiving_reply,
2016 CheckPendingReplyData *cprd;
2018 dbus_uint32_t reply_serial;
2020 _dbus_assert (sending_reply != NULL);
2021 _dbus_assert (receiving_reply != NULL);
2023 reply_serial = dbus_message_get_reply_serial (reply);
2025 link = bus_expire_list_get_first_link (connections->pending_replies);
2026 while (link != NULL)
2028 BusPendingReply *pending = link->data;
2030 if (pending->reply_serial == reply_serial &&
2031 pending->will_get_reply == receiving_reply &&
2032 pending->will_send_reply == sending_reply)
2034 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2038 link = bus_expire_list_get_next_link (connections->pending_replies,
2044 _dbus_verbose ("No pending reply expected\n");
2049 cprd = dbus_new0 (CheckPendingReplyData, 1);
2052 BUS_SET_OOM (error);
2056 if (!bus_transaction_add_cancel_hook (transaction,
2057 cancel_check_pending_reply,
2059 check_pending_reply_data_free))
2061 BUS_SET_OOM (error);
2067 cprd->connections = connections;
2069 bus_expire_list_unlink (connections->pending_replies,
2072 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2080 * Note that this is fairly fragile; in particular, don't try to use
2081 * one transaction across any main loop iterations.
2086 BusTransaction *transaction;
2087 DBusMessage *message;
2088 DBusPreallocatedSend *preallocated;
2093 BusTransactionCancelFunction cancel_function;
2094 DBusFreeFunction free_data_function;
2098 struct BusTransaction
2100 DBusList *connections;
2101 BusContext *context;
2102 DBusList *cancel_hooks;
2106 message_to_send_free (DBusConnection *connection,
2107 MessageToSend *to_send)
2109 if (to_send->message)
2110 dbus_message_unref (to_send->message);
2112 if (to_send->preallocated)
2113 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2115 dbus_free (to_send);
2119 cancel_hook_cancel (void *element,
2122 CancelHook *ch = element;
2124 _dbus_verbose ("Running transaction cancel hook\n");
2126 if (ch->cancel_function)
2127 (* ch->cancel_function) (ch->data);
2131 cancel_hook_free (void *element,
2134 CancelHook *ch = element;
2136 if (ch->free_data_function)
2137 (* ch->free_data_function) (ch->data);
2143 free_cancel_hooks (BusTransaction *transaction)
2145 _dbus_list_foreach (&transaction->cancel_hooks,
2146 cancel_hook_free, NULL);
2148 _dbus_list_clear (&transaction->cancel_hooks);
2152 bus_transaction_new (BusContext *context)
2154 BusTransaction *transaction;
2156 transaction = dbus_new0 (BusTransaction, 1);
2157 if (transaction == NULL)
2160 transaction->context = context;
2166 bus_transaction_get_context (BusTransaction *transaction)
2168 return transaction->context;
2172 * Reserve enough memory to capture the given message if the
2173 * transaction goes through.
2176 bus_transaction_capture (BusTransaction *transaction,
2177 DBusConnection *sender,
2178 DBusMessage *message)
2180 BusConnections *connections;
2183 DBusList *recipients = NULL;
2184 dbus_bool_t ret = FALSE;
2186 connections = bus_context_get_connections (transaction->context);
2188 /* shortcut: don't compose the message unless someone wants it */
2189 if (connections->monitors == NULL)
2192 mm = connections->monitor_matchmaker;
2193 /* This is non-null if there has ever been a monitor - we don't GC it.
2194 * There's little point, since there is up to 1 per process. */
2195 _dbus_assert (mm != NULL);
2197 if (!bus_matchmaker_get_recipients (mm, connections, sender, NULL, message,
2201 for (link = _dbus_list_get_first_link (&recipients);
2203 link = _dbus_list_get_next_link (&recipients, link))
2205 DBusConnection *recipient = link->data;
2207 if (!bus_transaction_send (transaction, recipient, message))
2214 _dbus_list_clear (&recipients);
2219 bus_transaction_capture_error_reply (BusTransaction *transaction,
2220 const DBusError *error,
2221 DBusMessage *in_reply_to)
2223 BusConnections *connections;
2225 dbus_bool_t ret = FALSE;
2227 _dbus_assert (error != NULL);
2228 _DBUS_ASSERT_ERROR_IS_SET (error);
2230 connections = bus_context_get_connections (transaction->context);
2232 /* shortcut: don't compose the message unless someone wants it */
2233 if (connections->monitors == NULL)
2236 reply = dbus_message_new_error (in_reply_to,
2243 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2246 ret = bus_transaction_capture (transaction, NULL, reply);
2249 dbus_message_unref (reply);
2254 bus_transaction_send_from_driver (BusTransaction *transaction,
2255 DBusConnection *connection,
2256 DBusMessage *message)
2258 DBusError error = DBUS_ERROR_INIT;
2260 /* We have to set the sender to the driver, and have
2261 * to check security policy since it was not done in
2264 _dbus_verbose ("Sending %s %s %s from driver\n",
2265 dbus_message_get_interface (message) ?
2266 dbus_message_get_interface (message) : "(no interface)",
2267 dbus_message_get_member (message) ?
2268 dbus_message_get_member (message) : "(no member)",
2269 dbus_message_get_error_name (message) ?
2270 dbus_message_get_error_name (message) : "(no error name)");
2272 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2275 if (bus_connection_is_active (connection))
2277 if (!dbus_message_set_destination (message,
2278 bus_connection_get_name (connection)))
2282 /* bus driver never wants a reply */
2283 dbus_message_set_no_reply (message, TRUE);
2285 /* Capture it for monitors, even if the real recipient's receive policy
2286 * does not allow it to receive this message from us (which would be odd).
2288 if (!bus_transaction_capture (transaction, NULL, message))
2291 /* If security policy doesn't allow the message, we would silently
2292 * eat it; the driver doesn't care about getting a reply. However,
2293 * if we're actively capturing messages, it's nice to log that we
2294 * tried to send it and did not allow ourselves to do so.
2296 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2298 NULL, connection, connection, message, &error))
2300 if (!bus_transaction_capture_error_reply (transaction, &error, message))
2302 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2303 "message from dbus-daemon rejected but not enough "
2304 "memory to capture it");
2307 /* This is not fatal to the transaction so silently eat the disallowed
2308 * message (see reasoning above) */
2309 dbus_error_free (&error);
2313 return bus_transaction_send (transaction, connection, message);
2317 bus_transaction_send (BusTransaction *transaction,
2318 DBusConnection *connection,
2319 DBusMessage *message)
2321 MessageToSend *to_send;
2322 BusConnectionData *d;
2325 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2326 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2327 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2329 dbus_message_get_interface (message) ?
2330 dbus_message_get_interface (message) : "(unset)",
2331 dbus_message_get_member (message) ?
2332 dbus_message_get_member (message) : "(unset)",
2333 dbus_message_get_error_name (message) ?
2334 dbus_message_get_error_name (message) : "(unset)",
2335 dbus_connection_get_is_connected (connection) ?
2336 "" : " (disconnected)");
2338 _dbus_assert (dbus_message_get_sender (message) != NULL);
2340 if (!dbus_connection_get_is_connected (connection))
2341 return TRUE; /* silently ignore disconnected connections */
2343 d = BUS_CONNECTION_DATA (connection);
2344 _dbus_assert (d != NULL);
2346 to_send = dbus_new (MessageToSend, 1);
2347 if (to_send == NULL)
2352 to_send->preallocated = dbus_connection_preallocate_send (connection);
2353 if (to_send->preallocated == NULL)
2355 dbus_free (to_send);
2359 dbus_message_ref (message);
2360 to_send->message = message;
2361 to_send->transaction = transaction;
2363 _dbus_verbose ("about to prepend message\n");
2365 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2367 message_to_send_free (connection, to_send);
2371 _dbus_verbose ("prepended message\n");
2373 /* See if we already had this connection in the list
2374 * for this transaction. If we have a pending message,
2375 * then we should already be in transaction->connections
2377 link = _dbus_list_get_first_link (&d->transaction_messages);
2378 _dbus_assert (link->data == to_send);
2379 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2380 while (link != NULL)
2382 MessageToSend *m = link->data;
2383 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2385 if (m->transaction == transaction)
2393 if (!_dbus_list_prepend (&transaction->connections, connection))
2395 _dbus_list_remove (&d->transaction_messages, to_send);
2396 message_to_send_free (connection, to_send);
2405 transaction_free (BusTransaction *transaction)
2407 _dbus_assert (transaction->connections == NULL);
2409 free_cancel_hooks (transaction);
2411 dbus_free (transaction);
2415 connection_cancel_transaction (DBusConnection *connection,
2416 BusTransaction *transaction)
2419 BusConnectionData *d;
2421 d = BUS_CONNECTION_DATA (connection);
2422 _dbus_assert (d != NULL);
2424 link = _dbus_list_get_first_link (&d->transaction_messages);
2425 while (link != NULL)
2427 MessageToSend *m = link->data;
2428 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2430 if (m->transaction == transaction)
2432 _dbus_list_remove_link (&d->transaction_messages,
2435 message_to_send_free (connection, m);
2443 bus_transaction_cancel_and_free (BusTransaction *transaction)
2445 DBusConnection *connection;
2447 _dbus_verbose ("TRANSACTION: cancelled\n");
2449 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2450 connection_cancel_transaction (connection, transaction);
2452 _dbus_list_foreach (&transaction->cancel_hooks,
2453 cancel_hook_cancel, NULL);
2455 transaction_free (transaction);
2459 connection_execute_transaction (DBusConnection *connection,
2460 BusTransaction *transaction)
2463 BusConnectionData *d;
2465 d = BUS_CONNECTION_DATA (connection);
2466 _dbus_assert (d != NULL);
2468 /* Send the queue in order (FIFO) */
2469 link = _dbus_list_get_last_link (&d->transaction_messages);
2470 while (link != NULL)
2472 MessageToSend *m = link->data;
2473 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2475 if (m->transaction == transaction)
2477 _dbus_list_remove_link (&d->transaction_messages,
2480 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2482 dbus_connection_send_preallocated (connection,
2487 m->preallocated = NULL; /* so we don't double-free it */
2489 message_to_send_free (connection, m);
2497 bus_transaction_execute_and_free (BusTransaction *transaction)
2499 /* For each connection in transaction->connections
2502 DBusConnection *connection;
2504 _dbus_verbose ("TRANSACTION: executing\n");
2506 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2507 connection_execute_transaction (connection, transaction);
2509 transaction_free (transaction);
2513 bus_connection_remove_transactions (DBusConnection *connection)
2515 MessageToSend *to_send;
2516 BusConnectionData *d;
2518 d = BUS_CONNECTION_DATA (connection);
2519 _dbus_assert (d != NULL);
2521 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2523 /* only has an effect for the first MessageToSend listing this transaction */
2524 _dbus_list_remove (&to_send->transaction->connections,
2527 _dbus_list_remove (&d->transaction_messages, to_send);
2528 message_to_send_free (connection, to_send);
2533 * Converts the DBusError to a message reply
2536 bus_transaction_send_error_reply (BusTransaction *transaction,
2537 DBusConnection *connection,
2538 const DBusError *error,
2539 DBusMessage *in_reply_to)
2543 _dbus_assert (error != NULL);
2544 _DBUS_ASSERT_ERROR_IS_SET (error);
2546 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2547 error->name, error->message);
2549 reply = dbus_message_new_error (in_reply_to,
2555 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2557 dbus_message_unref (reply);
2561 dbus_message_unref (reply);
2567 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2568 BusTransactionCancelFunction cancel_function,
2570 DBusFreeFunction free_data_function)
2574 ch = dbus_new (CancelHook, 1);
2578 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2579 cancel_function, data);
2581 ch->cancel_function = cancel_function;
2583 ch->free_data_function = free_data_function;
2585 /* It's important that the hooks get run in reverse order that they
2588 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2598 bus_connections_get_n_active (BusConnections *connections)
2600 return connections->n_completed;
2604 bus_connections_get_n_incomplete (BusConnections *connections)
2606 return connections->n_incomplete;
2609 #ifdef DBUS_ENABLE_STATS
2611 bus_connections_get_total_match_rules (BusConnections *connections)
2613 return connections->total_match_rules;
2617 bus_connections_get_peak_match_rules (BusConnections *connections)
2619 return connections->peak_match_rules;
2623 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2625 return connections->peak_match_rules_per_conn;
2629 bus_connections_get_total_bus_names (BusConnections *connections)
2631 return connections->total_bus_names;
2635 bus_connections_get_peak_bus_names (BusConnections *connections)
2637 return connections->peak_bus_names;
2641 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2643 return connections->peak_bus_names_per_conn;
2647 bus_connection_get_peak_match_rules (DBusConnection *connection)
2649 BusConnectionData *d;
2651 d = BUS_CONNECTION_DATA (connection);
2652 return d->peak_match_rules;
2656 bus_connection_get_peak_bus_names (DBusConnection *connection)
2658 BusConnectionData *d;
2660 d = BUS_CONNECTION_DATA (connection);
2661 return d->peak_bus_names;
2663 #endif /* DBUS_ENABLE_STATS */
2666 bus_connection_is_monitor (DBusConnection *connection)
2668 BusConnectionData *d;
2670 d = BUS_CONNECTION_DATA (connection);
2672 return d != NULL && d->link_in_monitors != NULL;
2676 bcd_add_monitor_rules (BusConnectionData *d,
2677 DBusConnection *connection,
2680 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2685 mm = bus_matchmaker_new ();
2690 d->connections->monitor_matchmaker = mm;
2693 for (iter = _dbus_list_get_first_link (rules);
2695 iter = _dbus_list_get_next_link (rules, iter))
2697 if (!bus_matchmaker_add_rule (mm, iter->data))
2699 bus_matchmaker_disconnected (mm, connection);
2708 bcd_drop_monitor_rules (BusConnectionData *d,
2709 DBusConnection *connection)
2711 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2714 bus_matchmaker_disconnected (mm, connection);
2718 bus_connection_be_monitor (DBusConnection *connection,
2719 BusTransaction *transaction,
2723 BusConnectionData *d;
2728 d = BUS_CONNECTION_DATA (connection);
2729 _dbus_assert (d != NULL);
2731 link = _dbus_list_alloc_link (connection);
2735 BUS_SET_OOM (error);
2739 if (!bcd_add_monitor_rules (d, connection, rules))
2741 _dbus_list_free_link (link);
2742 BUS_SET_OOM (error);
2746 /* release all its names */
2747 if (!_dbus_list_copy (&d->services_owned, &tmp))
2749 bcd_drop_monitor_rules (d, connection);
2750 _dbus_list_free_link (link);
2751 BUS_SET_OOM (error);
2755 for (iter = _dbus_list_get_first_link (&tmp);
2757 iter = _dbus_list_get_next_link (&tmp, iter))
2759 BusService *service = iter->data;
2761 /* This call is transactional: if there isn't enough memory to
2762 * do everything, then the service gets all its names back when
2763 * the transaction is cancelled due to OOM. */
2764 if (!bus_service_remove_owner (service, connection, transaction, error))
2766 bcd_drop_monitor_rules (d, connection);
2767 _dbus_list_free_link (link);
2768 _dbus_list_clear (&tmp);
2773 /* We have now done everything that can fail, so there is no problem
2774 * with doing the irrevocable stuff. */
2776 _dbus_list_clear (&tmp);
2778 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2779 "Connection %s (%s) became a monitor.", d->name,
2780 d->cached_loginfo_string);
2782 if (d->n_match_rules > 0)
2786 mm = bus_context_get_matchmaker (d->connections->context);
2787 bus_matchmaker_disconnected (mm, connection);
2790 /* flag it as a monitor */
2791 d->link_in_monitors = link;
2792 _dbus_list_append_link (&d->connections->monitors, link);
2794 /* it isn't allowed to reply, and it is no longer relevant whether it
2795 * receives replies */
2796 bus_connection_drop_pending_replies (d->connections, connection);