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"
33 #include <dbus/dbus-list.h>
34 #include <dbus/dbus-hash.h>
35 #include <dbus/dbus-timeout.h>
36 #include <dbus/dbus-connection-internal.h>
38 /* Trim executed commands to this length; we want to keep logs readable */
39 #define MAX_LOG_COMMAND_LEN 50
41 static void bus_connection_remove_transactions (DBusConnection *connection);
45 BusExpireItem expire_item;
47 DBusConnection *will_get_reply;
48 DBusConnection *will_send_reply;
50 dbus_uint32_t reply_serial;
57 DBusList *completed; /**< List of all completed connections */
58 int n_completed; /**< Length of completed list */
59 DBusList *incomplete; /**< List of all not-yet-active connections */
60 int n_incomplete; /**< Length of incomplete list */
62 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
63 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
64 int stamp; /**< Incrementing number */
65 BusExpireList *pending_replies; /**< List of pending replies */
67 /** List of all monitoring connections, a subset of completed.
68 * Each member is a #DBusConnection. */
70 BusMatchmaker *monitor_matchmaker;
72 #ifdef DBUS_ENABLE_STATS
73 int total_match_rules;
75 int peak_match_rules_per_conn;
79 int peak_bus_names_per_conn;
83 static dbus_int32_t connection_data_slot = -1;
87 BusConnections *connections;
88 DBusList *link_in_connection_list;
89 DBusConnection *connection;
90 DBusList *services_owned;
92 DBusList *match_rules;
95 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
96 DBusMessage *oom_message;
97 DBusPreallocatedSend *oom_preallocated;
98 BusClientPolicy *policy;
100 char *cached_loginfo_string;
101 BusSELinuxID *selinux_id;
103 long connection_tv_sec; /**< Time when we connected (seconds component) */
104 long connection_tv_usec; /**< Time when we connected (microsec component) */
105 int stamp; /**< connections->stamp last time we were traversed */
107 #ifdef DBUS_ENABLE_STATS
108 int peak_match_rules;
111 int n_pending_unix_fds;
112 DBusTimeout *pending_unix_fds_timeout;
114 /** non-NULL if and only if this is a monitor */
115 DBusList *link_in_monitors;
118 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
122 static void bus_connection_drop_pending_replies (BusConnections *connections,
123 DBusConnection *connection);
125 static dbus_bool_t expire_incomplete_timeout (void *data);
127 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
130 connection_get_loop (DBusConnection *connection)
132 BusConnectionData *d;
134 d = BUS_CONNECTION_DATA (connection);
136 return bus_context_get_loop (d->connections->context);
141 get_connections_for_uid (BusConnections *connections,
147 /* val is NULL is 0 when it isn't in the hash yet */
149 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
152 current_count = _DBUS_POINTER_TO_INT (val);
154 return current_count;
158 adjust_connections_for_uid (BusConnections *connections,
164 current_count = get_connections_for_uid (connections, uid);
166 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
167 ": was %d adjustment %d making %d\n",
168 uid, current_count, adjustment, current_count + adjustment);
170 _dbus_assert (current_count >= 0);
172 current_count += adjustment;
174 _dbus_assert (current_count >= 0);
176 if (current_count == 0)
178 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
185 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
186 uid, _DBUS_INT_TO_POINTER (current_count));
188 /* only positive adjustment can fail as otherwise
189 * a hash entry should already exist
191 _dbus_assert (adjustment > 0 ||
192 (adjustment <= 0 && retval));
199 bus_connection_disconnected (DBusConnection *connection)
201 BusConnectionData *d;
203 BusMatchmaker *matchmaker;
205 d = BUS_CONNECTION_DATA (connection);
206 _dbus_assert (d != NULL);
208 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
209 d->name ? d->name : "(inactive)");
211 /* Delete our match rules */
212 if (d->n_match_rules > 0)
214 matchmaker = bus_context_get_matchmaker (d->connections->context);
215 bus_matchmaker_disconnected (matchmaker, connection);
218 /* Drop any service ownership. Unfortunately, this requires
219 * memory allocation and there doesn't seem to be a good way to
220 * handle it other than sleeping; we can't "fail" the operation of
221 * disconnecting a client, and preallocating a broadcast "service is
222 * now gone" message for every client-service pair seems kind of
225 while ((service = _dbus_list_get_last (&d->services_owned)))
227 BusTransaction *transaction;
232 dbus_error_init (&error);
234 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
235 _dbus_wait_for_memory ();
237 if (!bus_service_remove_owner (service, connection,
238 transaction, &error))
240 _DBUS_ASSERT_ERROR_IS_SET (&error);
242 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
244 dbus_error_free (&error);
245 bus_transaction_cancel_and_free (transaction);
246 _dbus_wait_for_memory ();
251 _dbus_verbose ("Failed to remove service owner: %s %s\n",
252 error.name, error.message);
253 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
257 bus_transaction_execute_and_free (transaction);
260 bus_dispatch_remove_connection (connection);
262 /* no more watching */
263 if (!dbus_connection_set_watch_functions (connection,
267 _dbus_assert_not_reached ("setting watch functions to NULL failed");
269 if (!dbus_connection_set_timeout_functions (connection,
273 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
275 dbus_connection_set_unix_user_function (connection,
277 dbus_connection_set_windows_user_function (connection,
280 dbus_connection_set_dispatch_status_function (connection,
283 if (d->pending_unix_fds_timeout)
285 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
286 d->pending_unix_fds_timeout);
287 _dbus_timeout_unref (d->pending_unix_fds_timeout);
289 d->pending_unix_fds_timeout = NULL;
290 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
292 bus_connection_remove_transactions (connection);
294 if (d->link_in_monitors != NULL)
296 BusMatchmaker *mm = d->connections->monitor_matchmaker;
299 bus_matchmaker_disconnected (mm, connection);
301 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
302 d->link_in_monitors = NULL;
305 if (d->link_in_connection_list != NULL)
311 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
312 d->link_in_connection_list = NULL;
313 d->connections->n_completed -= 1;
315 if (dbus_connection_get_unix_user (connection, &uid))
317 if (!adjust_connections_for_uid (d->connections,
319 _dbus_assert_not_reached ("adjusting downward should never fail");
324 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
325 d->link_in_connection_list = NULL;
326 d->connections->n_incomplete -= 1;
328 /* If we have dropped below the max. number of incomplete
329 * connections, start accept()ing again */
330 bus_context_check_all_watches (d->connections->context);
333 _dbus_assert (d->connections->n_incomplete >= 0);
334 _dbus_assert (d->connections->n_completed >= 0);
337 bus_connection_drop_pending_replies (d->connections, connection);
339 /* frees "d" as side effect */
340 dbus_connection_set_data (connection,
341 connection_data_slot,
344 dbus_connection_unref (connection);
348 add_connection_watch (DBusWatch *watch,
351 DBusConnection *connection = data;
353 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
357 remove_connection_watch (DBusWatch *watch,
360 DBusConnection *connection = data;
362 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
366 toggle_connection_watch (DBusWatch *watch,
369 DBusConnection *connection = data;
371 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
375 add_connection_timeout (DBusTimeout *timeout,
378 DBusConnection *connection = data;
380 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
384 remove_connection_timeout (DBusTimeout *timeout,
387 DBusConnection *connection = data;
389 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
393 dispatch_status_function (DBusConnection *connection,
394 DBusDispatchStatus new_status,
397 DBusLoop *loop = data;
399 if (new_status != DBUS_DISPATCH_COMPLETE)
401 while (!_dbus_loop_queue_dispatch (loop, connection))
402 _dbus_wait_for_memory ();
407 allow_unix_user_function (DBusConnection *connection,
411 BusConnectionData *d;
413 d = BUS_CONNECTION_DATA (connection);
415 _dbus_assert (d != NULL);
417 return bus_context_allow_unix_user (d->connections->context, uid);
421 free_connection_data (void *data)
423 BusConnectionData *d = data;
425 /* services_owned should be NULL since we should be disconnected */
426 _dbus_assert (d->services_owned == NULL);
427 _dbus_assert (d->n_services_owned == 0);
429 _dbus_assert (d->transaction_messages == NULL);
431 if (d->oom_preallocated)
432 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
435 dbus_message_unref (d->oom_message);
438 bus_client_policy_unref (d->policy);
441 bus_selinux_id_unref (d->selinux_id);
443 dbus_free (d->cached_loginfo_string);
451 bus_connections_new (BusContext *context)
453 BusConnections *connections;
455 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
458 connections = dbus_new0 (BusConnections, 1);
459 if (connections == NULL)
462 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
464 if (connections->completed_by_user == NULL)
467 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
468 expire_incomplete_timeout,
470 if (connections->expire_timeout == NULL)
473 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
475 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
476 bus_context_get_reply_timeout (context),
477 bus_pending_reply_expired,
479 if (connections->pending_replies == NULL)
482 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
483 connections->expire_timeout))
486 connections->refcount = 1;
487 connections->context = context;
492 bus_expire_list_free (connections->pending_replies);
494 _dbus_timeout_unref (connections->expire_timeout);
496 _dbus_hash_table_unref (connections->completed_by_user);
498 dbus_free (connections);
500 dbus_connection_free_data_slot (&connection_data_slot);
506 bus_connections_ref (BusConnections *connections)
508 _dbus_assert (connections->refcount > 0);
509 connections->refcount += 1;
515 bus_connections_unref (BusConnections *connections)
517 _dbus_assert (connections->refcount > 0);
518 connections->refcount -= 1;
519 if (connections->refcount == 0)
521 /* drop all incomplete */
522 while (connections->incomplete != NULL)
524 DBusConnection *connection;
526 connection = connections->incomplete->data;
528 dbus_connection_ref (connection);
529 dbus_connection_close (connection);
530 bus_connection_disconnected (connection);
531 dbus_connection_unref (connection);
534 _dbus_assert (connections->n_incomplete == 0);
536 /* drop all monitors */
537 _dbus_list_clear (&connections->monitors);
539 /* drop all real connections */
540 while (connections->completed != NULL)
542 DBusConnection *connection;
544 connection = connections->completed->data;
546 dbus_connection_ref (connection);
547 dbus_connection_close (connection);
548 bus_connection_disconnected (connection);
549 dbus_connection_unref (connection);
552 _dbus_assert (connections->n_completed == 0);
554 bus_expire_list_free (connections->pending_replies);
556 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
557 connections->expire_timeout);
559 _dbus_timeout_unref (connections->expire_timeout);
561 _dbus_hash_table_unref (connections->completed_by_user);
563 if (connections->monitor_matchmaker != NULL)
564 bus_matchmaker_unref (connections->monitor_matchmaker);
566 dbus_free (connections);
568 dbus_connection_free_data_slot (&connection_data_slot);
572 /* Used for logging */
574 cache_peer_loginfo_string (BusConnectionData *d,
575 DBusConnection *connection)
577 DBusString loginfo_buf;
581 dbus_bool_t prev_added;
583 if (!_dbus_string_init (&loginfo_buf))
587 if (dbus_connection_get_unix_user (connection, &uid))
589 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
595 if (dbus_connection_get_unix_process_id (connection, &pid))
599 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
602 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
604 /* Ignore errors here; we may not have permissions to read the
606 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
607 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
611 if (dbus_connection_get_windows_user (connection, &windows_sid))
613 dbus_bool_t did_append;
614 did_append = _dbus_string_append_printf (&loginfo_buf,
615 "sid=\"%s\" ", windows_sid);
616 dbus_free (windows_sid);
621 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
624 _dbus_string_free (&loginfo_buf);
628 _dbus_string_free (&loginfo_buf);
633 check_pending_fds_cb (DBusConnection *connection)
635 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
636 int n_pending_unix_fds_old = d->n_pending_unix_fds;
637 int n_pending_unix_fds_new;
639 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
641 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
642 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
644 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
646 _dbus_timeout_set_interval (d->pending_unix_fds_timeout,
647 bus_context_get_pending_fd_timeout (d->connections->context));
648 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, TRUE);
651 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
653 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
657 d->n_pending_unix_fds = n_pending_unix_fds_new;
661 pending_unix_fds_timeout_cb (void *data)
663 DBusConnection *connection = data;
664 dbus_connection_close (connection);
669 bus_connections_setup_connection (BusConnections *connections,
670 DBusConnection *connection)
673 BusConnectionData *d;
678 d = dbus_new0 (BusConnectionData, 1);
683 d->connections = connections;
684 d->connection = connection;
686 _dbus_get_monotonic_time (&d->connection_tv_sec,
687 &d->connection_tv_usec);
689 _dbus_assert (connection_data_slot >= 0);
691 if (!dbus_connection_set_data (connection,
692 connection_data_slot,
693 d, free_connection_data))
699 dbus_connection_set_route_peer_messages (connection, TRUE);
703 dbus_error_init (&error);
704 d->selinux_id = bus_selinux_init_connection_id (connection,
706 if (dbus_error_is_set (&error))
708 /* This is a bit bogus because we pretend all errors
709 * are OOM; this is done because we know that in bus.c
710 * an OOM error disconnects the connection, which is
711 * the same thing we want on any other error.
713 dbus_error_free (&error);
717 if (!dbus_connection_set_watch_functions (connection,
718 add_connection_watch,
719 remove_connection_watch,
720 toggle_connection_watch,
725 if (!dbus_connection_set_timeout_functions (connection,
726 add_connection_timeout,
727 remove_connection_timeout,
732 /* For now we don't need to set a Windows user function because
733 * there are no policies in the config file controlling what
734 * Windows users can connect. The default 'same user that owns the
735 * bus can connect' behavior of DBusConnection is fine on Windows.
737 dbus_connection_set_unix_user_function (connection,
738 allow_unix_user_function,
741 dbus_connection_set_dispatch_status_function (connection,
742 dispatch_status_function,
743 bus_context_get_loop (connections->context),
746 d->link_in_connection_list = _dbus_list_alloc_link (connection);
747 if (d->link_in_connection_list == NULL)
750 /* Setup the connection with the dispatcher */
751 if (!bus_dispatch_add_connection (connection))
754 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
756 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
758 bus_dispatch_remove_connection (connection);
763 /* Setup pending fds timeout (see #80559) */
764 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
765 pending_unix_fds_timeout_cb,
767 if (d->pending_unix_fds_timeout == NULL)
770 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
771 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
772 d->pending_unix_fds_timeout))
775 _dbus_connection_set_pending_fds_function (connection,
776 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
779 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
780 connections->n_incomplete += 1;
782 dbus_connection_ref (connection);
784 bus_connections_expire_incomplete (connections);
786 /* The listening socket is removed from the main loop,
787 * i.e. does not accept(), while n_incomplete is at its
788 * maximum value; so we shouldn't get here in that case */
789 _dbus_assert (connections->n_incomplete <=
790 bus_context_get_max_incomplete_connections (connections->context));
792 /* If we have the maximum number of incomplete connections,
793 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
794 bus_context_check_all_watches (d->connections->context);
802 bus_selinux_id_unref (d->selinux_id);
803 d->selinux_id = NULL;
805 if (!dbus_connection_set_watch_functions (connection,
809 _dbus_assert_not_reached ("setting watch functions to NULL failed");
811 if (!dbus_connection_set_timeout_functions (connection,
815 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
817 dbus_connection_set_unix_user_function (connection,
820 dbus_connection_set_windows_user_function (connection,
823 dbus_connection_set_dispatch_status_function (connection,
826 if (d->pending_unix_fds_timeout)
827 _dbus_timeout_unref (d->pending_unix_fds_timeout);
829 d->pending_unix_fds_timeout = NULL;
831 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
833 if (d->link_in_connection_list != NULL)
835 _dbus_assert (d->link_in_connection_list->next == NULL);
836 _dbus_assert (d->link_in_connection_list->prev == NULL);
837 _dbus_list_free_link (d->link_in_connection_list);
838 d->link_in_connection_list = NULL;
841 if (!dbus_connection_set_data (connection,
842 connection_data_slot,
844 _dbus_assert_not_reached ("failed to set connection data to null");
846 /* "d" has now been freed */
853 bus_connections_expire_incomplete (BusConnections *connections)
859 if (connections->incomplete != NULL)
861 long tv_sec, tv_usec;
865 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
866 auth_timeout = bus_context_get_auth_timeout (connections->context);
868 link = _dbus_list_get_first_link (&connections->incomplete);
871 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
872 DBusConnection *connection;
873 BusConnectionData *d;
876 connection = link->data;
878 d = BUS_CONNECTION_DATA (connection);
880 _dbus_assert (d != NULL);
882 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
883 d->connection_tv_usec,
886 if (elapsed >= (double) auth_timeout)
888 /* Unfortunately, we can't identify the connection: it doesn't
889 * have a unique name yet, we don't know its uid/pid yet,
891 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
892 "Connection has not authenticated soon enough, closing it "
893 "(auth_timeout=%dms, elapsed: %.0fms)",
894 auth_timeout, elapsed);
896 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
897 dbus_connection_close (connection);
901 /* We can end the loop, since the connections are in oldest-first order */
902 next_interval = ((double)auth_timeout) - elapsed;
903 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
904 connection, next_interval);
913 bus_expire_timeout_set_interval (connections->expire_timeout,
918 expire_incomplete_timeout (void *data)
920 BusConnections *connections = data;
922 _dbus_verbose ("Running\n");
924 /* note that this may remove the timeout */
925 bus_connections_expire_incomplete (connections);
931 bus_connection_get_unix_groups (DBusConnection *connection,
932 unsigned long **groups,
941 if (dbus_connection_get_unix_user (connection, &uid))
943 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
945 _dbus_verbose ("Did not get any groups for UID %lu\n",
951 _dbus_verbose ("Got %d groups for UID %lu\n",
957 return TRUE; /* successfully got 0 groups */
961 bus_connection_is_in_unix_group (DBusConnection *connection,
965 unsigned long *group_ids;
968 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
973 while (i < n_group_ids)
975 if (group_ids[i] == gid)
977 dbus_free (group_ids);
983 dbus_free (group_ids);
988 bus_connection_get_loginfo (DBusConnection *connection)
990 BusConnectionData *d;
992 d = BUS_CONNECTION_DATA (connection);
994 if (!bus_connection_is_active (connection))
996 return d->cached_loginfo_string;
1000 bus_connection_get_policy (DBusConnection *connection)
1002 BusConnectionData *d;
1004 d = BUS_CONNECTION_DATA (connection);
1006 _dbus_assert (d != NULL);
1007 _dbus_assert (d->policy != NULL);
1013 foreach_active (BusConnections *connections,
1014 BusConnectionForeachFunction function,
1019 link = _dbus_list_get_first_link (&connections->completed);
1020 while (link != NULL)
1022 DBusConnection *connection = link->data;
1023 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1025 if (!(* function) (connection, data))
1035 foreach_inactive (BusConnections *connections,
1036 BusConnectionForeachFunction function,
1041 link = _dbus_list_get_first_link (&connections->incomplete);
1042 while (link != NULL)
1044 DBusConnection *connection = link->data;
1045 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1047 if (!(* function) (connection, data))
1057 * Calls function on each active connection; if the function returns
1058 * #FALSE, stops iterating. Active connections are authenticated
1059 * and have sent a Hello message.
1061 * @param connections the connections object
1062 * @param function the function
1063 * @param data data to pass to it as a second arg
1066 bus_connections_foreach_active (BusConnections *connections,
1067 BusConnectionForeachFunction function,
1070 foreach_active (connections, function, data);
1074 * Calls function on each connection; if the function returns
1075 * #FALSE, stops iterating.
1077 * @param connections the connections object
1078 * @param function the function
1079 * @param data data to pass to it as a second arg
1082 bus_connections_foreach (BusConnections *connections,
1083 BusConnectionForeachFunction function,
1086 if (!foreach_active (connections, function, data))
1089 foreach_inactive (connections, function, data);
1093 bus_connections_get_context (BusConnections *connections)
1095 return connections->context;
1099 * This is used to avoid covering the same connection twice when
1100 * traversing connections. Note that it assumes we will
1101 * bus_connection_mark_stamp() each connection at least once per
1102 * INT_MAX increments of the global stamp, or wraparound would break
1106 bus_connections_increment_stamp (BusConnections *connections)
1108 connections->stamp += 1;
1111 /* Mark connection with current stamp, return TRUE if it
1112 * didn't already have that stamp
1115 bus_connection_mark_stamp (DBusConnection *connection)
1117 BusConnectionData *d;
1119 d = BUS_CONNECTION_DATA (connection);
1121 _dbus_assert (d != NULL);
1123 if (d->stamp == d->connections->stamp)
1127 d->stamp = d->connections->stamp;
1133 bus_connection_get_context (DBusConnection *connection)
1135 BusConnectionData *d;
1137 d = BUS_CONNECTION_DATA (connection);
1139 _dbus_assert (d != NULL);
1141 return d->connections->context;
1145 bus_connection_get_connections (DBusConnection *connection)
1147 BusConnectionData *d;
1149 d = BUS_CONNECTION_DATA (connection);
1151 _dbus_assert (d != NULL);
1153 return d->connections;
1157 bus_connection_get_registry (DBusConnection *connection)
1159 BusConnectionData *d;
1161 d = BUS_CONNECTION_DATA (connection);
1163 _dbus_assert (d != NULL);
1165 return bus_context_get_registry (d->connections->context);
1169 bus_connection_get_activation (DBusConnection *connection)
1171 BusConnectionData *d;
1173 d = BUS_CONNECTION_DATA (connection);
1175 _dbus_assert (d != NULL);
1177 return bus_context_get_activation (d->connections->context);
1181 bus_connection_get_matchmaker (DBusConnection *connection)
1183 BusConnectionData *d;
1185 d = BUS_CONNECTION_DATA (connection);
1187 _dbus_assert (d != NULL);
1189 return bus_context_get_matchmaker (d->connections->context);
1193 bus_connection_get_selinux_id (DBusConnection *connection)
1195 BusConnectionData *d;
1197 d = BUS_CONNECTION_DATA (connection);
1199 _dbus_assert (d != NULL);
1201 return d->selinux_id;
1205 * Checks whether the connection is registered with the message bus.
1207 * @param connection the connection
1208 * @returns #TRUE if we're an active message bus participant
1211 bus_connection_is_active (DBusConnection *connection)
1213 BusConnectionData *d;
1215 d = BUS_CONNECTION_DATA (connection);
1217 return d != NULL && d->name != NULL;
1221 bus_connection_preallocate_oom_error (DBusConnection *connection)
1223 DBusMessage *message;
1224 DBusPreallocatedSend *preallocated;
1225 BusConnectionData *d;
1227 d = BUS_CONNECTION_DATA (connection);
1229 _dbus_assert (d != NULL);
1231 if (d->oom_preallocated != NULL)
1234 preallocated = dbus_connection_preallocate_send (connection);
1235 if (preallocated == NULL)
1238 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1240 if (message == NULL)
1242 dbus_connection_free_preallocated_send (connection, preallocated);
1246 /* d->name may be NULL, but that is OK */
1247 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1248 !dbus_message_set_destination (message, d->name) ||
1249 !dbus_message_set_sender (message,
1252 dbus_connection_free_preallocated_send (connection, preallocated);
1253 dbus_message_unref (message);
1257 /* set reply serial to placeholder value just so space is already allocated
1260 if (!dbus_message_set_reply_serial (message, 14))
1262 dbus_connection_free_preallocated_send (connection, preallocated);
1263 dbus_message_unref (message);
1267 d->oom_message = message;
1268 d->oom_preallocated = preallocated;
1274 bus_connection_send_oom_error (DBusConnection *connection,
1275 DBusMessage *in_reply_to)
1277 BusConnectionData *d;
1279 d = BUS_CONNECTION_DATA (connection);
1281 _dbus_assert (d != NULL);
1282 _dbus_assert (d->oom_message != NULL);
1284 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1285 "dbus-daemon transaction failed (OOM), sending error to "
1286 "sender %s", bus_connection_get_loginfo (connection));
1288 /* should always succeed since we set it to a placeholder earlier */
1289 if (!dbus_message_set_reply_serial (d->oom_message,
1290 dbus_message_get_serial (in_reply_to)))
1291 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1293 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1295 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1296 d->oom_message, NULL);
1298 dbus_message_unref (d->oom_message);
1299 d->oom_message = NULL;
1300 d->oom_preallocated = NULL;
1303 #ifdef DBUS_ENABLE_STATS
1305 update_peak (int *peak,
1314 bus_connection_add_match_rule_link (DBusConnection *connection,
1317 BusConnectionData *d;
1319 d = BUS_CONNECTION_DATA (connection);
1320 _dbus_assert (d != NULL);
1322 _dbus_list_append_link (&d->match_rules, link);
1324 d->n_match_rules += 1;
1326 #ifdef DBUS_ENABLE_STATS
1327 update_peak (&d->peak_match_rules, d->n_match_rules);
1328 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1330 d->connections->total_match_rules += 1;
1331 update_peak (&d->connections->peak_match_rules,
1332 d->connections->total_match_rules);
1337 bus_connection_add_match_rule (DBusConnection *connection,
1342 link = _dbus_list_alloc_link (rule);
1347 bus_connection_add_match_rule_link (connection, link);
1353 bus_connection_remove_match_rule (DBusConnection *connection,
1356 BusConnectionData *d;
1358 d = BUS_CONNECTION_DATA (connection);
1359 _dbus_assert (d != NULL);
1361 _dbus_list_remove_last (&d->match_rules, rule);
1363 d->n_match_rules -= 1;
1364 _dbus_assert (d->n_match_rules >= 0);
1366 #ifdef DBUS_ENABLE_STATS
1367 d->connections->total_match_rules -= 1;
1372 bus_connection_get_n_match_rules (DBusConnection *connection)
1374 BusConnectionData *d;
1376 d = BUS_CONNECTION_DATA (connection);
1377 _dbus_assert (d != NULL);
1379 return d->n_match_rules;
1383 bus_connection_add_owned_service_link (DBusConnection *connection,
1386 BusConnectionData *d;
1388 d = BUS_CONNECTION_DATA (connection);
1389 _dbus_assert (d != NULL);
1391 _dbus_list_append_link (&d->services_owned, link);
1393 d->n_services_owned += 1;
1395 #ifdef DBUS_ENABLE_STATS
1396 update_peak (&d->peak_bus_names, d->n_services_owned);
1397 update_peak (&d->connections->peak_bus_names_per_conn,
1398 d->n_services_owned);
1400 d->connections->total_bus_names += 1;
1401 update_peak (&d->connections->peak_bus_names,
1402 d->connections->total_bus_names);
1407 bus_connection_add_owned_service (DBusConnection *connection,
1408 BusService *service)
1412 link = _dbus_list_alloc_link (service);
1417 bus_connection_add_owned_service_link (connection, link);
1423 bus_connection_remove_owned_service (DBusConnection *connection,
1424 BusService *service)
1426 BusConnectionData *d;
1428 d = BUS_CONNECTION_DATA (connection);
1429 _dbus_assert (d != NULL);
1431 _dbus_list_remove_last (&d->services_owned, service);
1433 d->n_services_owned -= 1;
1434 _dbus_assert (d->n_services_owned >= 0);
1436 #ifdef DBUS_ENABLE_STATS
1437 d->connections->total_bus_names -= 1;
1442 bus_connection_get_n_services_owned (DBusConnection *connection)
1444 BusConnectionData *d;
1446 d = BUS_CONNECTION_DATA (connection);
1447 _dbus_assert (d != NULL);
1449 return d->n_services_owned;
1453 bus_connection_complete (DBusConnection *connection,
1454 const DBusString *name,
1457 BusConnectionData *d;
1460 d = BUS_CONNECTION_DATA (connection);
1461 _dbus_assert (d != NULL);
1462 _dbus_assert (d->name == NULL);
1463 _dbus_assert (d->policy == NULL);
1465 _dbus_assert (!bus_connection_is_active (connection));
1467 if (!_dbus_string_copy_data (name, &d->name))
1469 BUS_SET_OOM (error);
1473 _dbus_assert (d->name != NULL);
1475 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1477 d->policy = bus_context_create_client_policy (d->connections->context,
1481 /* we may have a NULL policy on OOM or error getting list of
1482 * groups for a user. In the latter case we don't handle it so
1483 * well currently, as it will just keep failing over and over.
1486 if (d->policy == NULL)
1488 _dbus_verbose ("Failed to create security policy for connection %p\n",
1490 _DBUS_ASSERT_ERROR_IS_SET (error);
1491 dbus_free (d->name);
1496 if (dbus_connection_get_unix_user (connection, &uid))
1498 if (!adjust_connections_for_uid (d->connections,
1503 /* Create and cache a string which holds information about the
1504 * peer process; used for logging purposes.
1506 if (!cache_peer_loginfo_string (d, connection))
1509 /* Now the connection is active, move it between lists */
1510 _dbus_list_unlink (&d->connections->incomplete,
1511 d->link_in_connection_list);
1512 d->connections->n_incomplete -= 1;
1513 _dbus_list_append_link (&d->connections->completed,
1514 d->link_in_connection_list);
1515 d->connections->n_completed += 1;
1517 _dbus_assert (d->connections->n_incomplete >= 0);
1518 _dbus_assert (d->connections->n_completed > 0);
1520 /* If we have dropped below the max. number of incomplete
1521 * connections, start accept()ing again */
1522 bus_context_check_all_watches (d->connections->context);
1524 /* See if we can remove the timeout */
1525 bus_connections_expire_incomplete (d->connections);
1527 _dbus_assert (bus_connection_is_active (connection));
1531 BUS_SET_OOM (error);
1532 dbus_free (d->name);
1535 bus_client_policy_unref (d->policy);
1541 bus_connections_reload_policy (BusConnections *connections,
1544 BusConnectionData *d;
1545 DBusConnection *connection;
1548 _dbus_assert (connections != NULL);
1549 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1551 for (link = _dbus_list_get_first_link (&(connections->completed));
1553 link = _dbus_list_get_next_link (&(connections->completed), link))
1555 connection = link->data;
1556 d = BUS_CONNECTION_DATA (connection);
1557 _dbus_assert (d != NULL);
1558 _dbus_assert (d->policy != NULL);
1560 bus_client_policy_unref (d->policy);
1561 d->policy = bus_context_create_client_policy (connections->context,
1564 if (d->policy == NULL)
1566 _dbus_verbose ("Failed to create security policy for connection %p\n",
1568 _DBUS_ASSERT_ERROR_IS_SET (error);
1577 bus_connection_get_name (DBusConnection *connection)
1579 BusConnectionData *d;
1581 d = BUS_CONNECTION_DATA (connection);
1582 _dbus_assert (d != NULL);
1588 * Check whether completing the passed-in connection would
1589 * exceed limits, and if so set error and return #FALSE
1592 bus_connections_check_limits (BusConnections *connections,
1593 DBusConnection *requesting_completion,
1598 if (connections->n_completed >=
1599 bus_context_get_max_completed_connections (connections->context))
1601 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1602 "The maximum number of active connections has been reached");
1606 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1608 if (get_connections_for_uid (connections, uid) >=
1609 bus_context_get_max_connections_per_user (connections->context))
1611 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1612 "The maximum number of active connections for UID %lu has been reached",
1622 bus_pending_reply_free (BusPendingReply *pending)
1624 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1626 pending->will_send_reply,
1627 pending->will_get_reply,
1628 pending->reply_serial);
1630 dbus_free (pending);
1634 bus_pending_reply_send_no_reply (BusConnections *connections,
1635 BusTransaction *transaction,
1636 BusPendingReply *pending)
1638 DBusMessage *message;
1639 DBusMessageIter iter;
1645 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1646 if (message == NULL)
1649 dbus_message_set_no_reply (message, TRUE);
1651 if (!dbus_message_set_reply_serial (message,
1652 pending->reply_serial))
1655 if (!dbus_message_set_error_name (message,
1656 DBUS_ERROR_NO_REPLY))
1659 /* If you change these messages, adjust test/dbus-daemon.c to match */
1660 if (pending->will_send_reply == NULL)
1661 errmsg = "Message recipient disconnected from message bus without replying";
1663 errmsg = "Message did not receive a reply (timeout by message bus)";
1665 dbus_message_iter_init_append (message, &iter);
1666 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1669 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1676 dbus_message_unref (message);
1681 bus_pending_reply_expired (BusExpireList *list,
1685 BusPendingReply *pending = link->data;
1686 BusConnections *connections = data;
1687 BusTransaction *transaction;
1689 /* No reply is forthcoming. So nuke it if we can. If not,
1690 * leave it in the list to try expiring again later when we
1694 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1696 pending->will_send_reply,
1697 pending->will_get_reply,
1698 pending->reply_serial);
1700 transaction = bus_transaction_new (connections->context);
1701 if (transaction == NULL)
1704 if (!bus_pending_reply_send_no_reply (connections,
1708 bus_transaction_cancel_and_free (transaction);
1712 bus_expire_list_remove_link (connections->pending_replies, link);
1714 bus_pending_reply_free (pending);
1715 bus_transaction_execute_and_free (transaction);
1721 bus_connection_drop_pending_replies (BusConnections *connections,
1722 DBusConnection *connection)
1724 /* The DBusConnection is almost 100% finalized here, so you can't
1725 * do anything with it except check for pointer equality
1729 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1732 link = bus_expire_list_get_first_link (connections->pending_replies);
1733 while (link != NULL)
1736 BusPendingReply *pending;
1738 next = bus_expire_list_get_next_link (connections->pending_replies,
1740 pending = link->data;
1742 if (pending->will_get_reply == connection)
1744 /* We don't need to track this pending reply anymore */
1746 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1748 pending->will_send_reply,
1749 pending->will_get_reply,
1750 pending->reply_serial);
1752 bus_expire_list_remove_link (connections->pending_replies,
1754 bus_pending_reply_free (pending);
1756 else if (pending->will_send_reply == connection)
1758 /* The reply isn't going to be sent, so set things
1759 * up so it will be expired right away
1761 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1763 pending->will_send_reply,
1764 pending->will_get_reply,
1765 pending->reply_serial);
1767 pending->will_send_reply = NULL;
1768 pending->expire_item.added_tv_sec = 0;
1769 pending->expire_item.added_tv_usec = 0;
1771 bus_expire_list_recheck_immediately (connections->pending_replies);
1781 BusPendingReply *pending;
1782 BusConnections *connections;
1783 } CancelPendingReplyData;
1786 cancel_pending_reply (void *data)
1788 CancelPendingReplyData *d = data;
1790 _dbus_verbose ("d = %p\n", d);
1792 if (!bus_expire_list_remove (d->connections->pending_replies,
1793 &d->pending->expire_item))
1794 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1796 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1800 cancel_pending_reply_data_free (void *data)
1802 CancelPendingReplyData *d = data;
1804 _dbus_verbose ("d = %p\n", d);
1806 /* d->pending should be either freed or still
1807 * in the list of pending replies (owned by someone
1815 * Record that a reply is allowed; return TRUE on success.
1818 bus_connections_expect_reply (BusConnections *connections,
1819 BusTransaction *transaction,
1820 DBusConnection *will_get_reply,
1821 DBusConnection *will_send_reply,
1822 DBusMessage *reply_to_this,
1825 BusPendingReply *pending;
1826 dbus_uint32_t reply_serial;
1828 CancelPendingReplyData *cprd;
1831 _dbus_assert (will_get_reply != NULL);
1832 _dbus_assert (will_send_reply != NULL);
1833 _dbus_assert (reply_to_this != NULL);
1835 if (dbus_message_get_no_reply (reply_to_this))
1836 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1838 reply_serial = dbus_message_get_serial (reply_to_this);
1840 link = bus_expire_list_get_first_link (connections->pending_replies);
1842 while (link != NULL)
1844 pending = link->data;
1846 if (pending->reply_serial == reply_serial &&
1847 pending->will_get_reply == will_get_reply &&
1848 pending->will_send_reply == will_send_reply)
1850 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1851 "Message has the same reply serial as a currently-outstanding existing method call");
1855 link = bus_expire_list_get_next_link (connections->pending_replies,
1857 if (pending->will_get_reply == will_get_reply)
1862 bus_context_get_max_replies_per_connection (connections->context))
1864 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1865 "The maximum number of pending replies per connection has been reached");
1869 pending = dbus_new0 (BusPendingReply, 1);
1870 if (pending == NULL)
1872 BUS_SET_OOM (error);
1876 #ifdef DBUS_ENABLE_VERBOSE_MODE
1877 /* so we can see a not-yet-added pending reply */
1878 pending->expire_item.added_tv_sec = 1;
1879 pending->expire_item.added_tv_usec = 1;
1882 pending->will_get_reply = will_get_reply;
1883 pending->will_send_reply = will_send_reply;
1884 pending->reply_serial = reply_serial;
1886 cprd = dbus_new0 (CancelPendingReplyData, 1);
1889 BUS_SET_OOM (error);
1890 bus_pending_reply_free (pending);
1894 if (!bus_expire_list_add (connections->pending_replies,
1895 &pending->expire_item))
1897 BUS_SET_OOM (error);
1899 bus_pending_reply_free (pending);
1903 if (!bus_transaction_add_cancel_hook (transaction,
1904 cancel_pending_reply,
1906 cancel_pending_reply_data_free))
1908 BUS_SET_OOM (error);
1909 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1911 bus_pending_reply_free (pending);
1915 cprd->pending = pending;
1916 cprd->connections = connections;
1918 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1919 &pending->expire_item.added_tv_usec);
1921 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1923 pending->will_send_reply,
1924 pending->will_get_reply,
1925 pending->reply_serial);
1933 BusConnections *connections;
1934 } CheckPendingReplyData;
1937 cancel_check_pending_reply (void *data)
1939 CheckPendingReplyData *d = data;
1941 _dbus_verbose ("d = %p\n",d);
1943 bus_expire_list_add_link (d->connections->pending_replies,
1949 check_pending_reply_data_free (void *data)
1951 CheckPendingReplyData *d = data;
1953 _dbus_verbose ("d = %p\n",d);
1955 if (d->link != NULL)
1957 BusPendingReply *pending = d->link->data;
1959 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1960 &pending->expire_item));
1962 bus_pending_reply_free (pending);
1963 _dbus_list_free_link (d->link);
1970 * Check whether a reply is allowed, remove BusPendingReply
1971 * if so, return TRUE if so.
1974 bus_connections_check_reply (BusConnections *connections,
1975 BusTransaction *transaction,
1976 DBusConnection *sending_reply,
1977 DBusConnection *receiving_reply,
1981 CheckPendingReplyData *cprd;
1983 dbus_uint32_t reply_serial;
1985 _dbus_assert (sending_reply != NULL);
1986 _dbus_assert (receiving_reply != NULL);
1988 reply_serial = dbus_message_get_reply_serial (reply);
1990 link = bus_expire_list_get_first_link (connections->pending_replies);
1991 while (link != NULL)
1993 BusPendingReply *pending = link->data;
1995 if (pending->reply_serial == reply_serial &&
1996 pending->will_get_reply == receiving_reply &&
1997 pending->will_send_reply == sending_reply)
1999 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2003 link = bus_expire_list_get_next_link (connections->pending_replies,
2009 _dbus_verbose ("No pending reply expected\n");
2014 cprd = dbus_new0 (CheckPendingReplyData, 1);
2017 BUS_SET_OOM (error);
2021 if (!bus_transaction_add_cancel_hook (transaction,
2022 cancel_check_pending_reply,
2024 check_pending_reply_data_free))
2026 BUS_SET_OOM (error);
2032 cprd->connections = connections;
2034 bus_expire_list_unlink (connections->pending_replies,
2037 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2045 * Note that this is fairly fragile; in particular, don't try to use
2046 * one transaction across any main loop iterations.
2051 BusTransaction *transaction;
2052 DBusMessage *message;
2053 DBusPreallocatedSend *preallocated;
2058 BusTransactionCancelFunction cancel_function;
2059 DBusFreeFunction free_data_function;
2063 struct BusTransaction
2065 DBusList *connections;
2066 BusContext *context;
2067 DBusList *cancel_hooks;
2071 message_to_send_free (DBusConnection *connection,
2072 MessageToSend *to_send)
2074 if (to_send->message)
2075 dbus_message_unref (to_send->message);
2077 if (to_send->preallocated)
2078 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2080 dbus_free (to_send);
2084 cancel_hook_cancel (void *element,
2087 CancelHook *ch = element;
2089 _dbus_verbose ("Running transaction cancel hook\n");
2091 if (ch->cancel_function)
2092 (* ch->cancel_function) (ch->data);
2096 cancel_hook_free (void *element,
2099 CancelHook *ch = element;
2101 if (ch->free_data_function)
2102 (* ch->free_data_function) (ch->data);
2108 free_cancel_hooks (BusTransaction *transaction)
2110 _dbus_list_foreach (&transaction->cancel_hooks,
2111 cancel_hook_free, NULL);
2113 _dbus_list_clear (&transaction->cancel_hooks);
2117 bus_transaction_new (BusContext *context)
2119 BusTransaction *transaction;
2121 transaction = dbus_new0 (BusTransaction, 1);
2122 if (transaction == NULL)
2125 transaction->context = context;
2131 bus_transaction_get_context (BusTransaction *transaction)
2133 return transaction->context;
2137 * Reserve enough memory to capture the given message if the
2138 * transaction goes through.
2141 bus_transaction_capture (BusTransaction *transaction,
2142 DBusConnection *sender,
2143 DBusMessage *message)
2145 BusConnections *connections;
2148 DBusList *recipients = NULL;
2149 dbus_bool_t ret = FALSE;
2151 connections = bus_context_get_connections (transaction->context);
2153 /* shortcut: don't compose the message unless someone wants it */
2154 if (connections->monitors == NULL)
2157 mm = connections->monitor_matchmaker;
2158 /* This is non-null if there has ever been a monitor - we don't GC it.
2159 * There's little point, since there is up to 1 per process. */
2160 _dbus_assert (mm != NULL);
2162 if (!bus_matchmaker_get_recipients (mm, connections, sender, NULL, message,
2166 for (link = _dbus_list_get_first_link (&recipients);
2168 link = _dbus_list_get_next_link (&recipients, link))
2170 DBusConnection *recipient = link->data;
2172 if (!bus_transaction_send (transaction, recipient, message))
2179 _dbus_list_clear (&recipients);
2184 bus_transaction_capture_error_reply (BusTransaction *transaction,
2185 const DBusError *error,
2186 DBusMessage *in_reply_to)
2188 BusConnections *connections;
2190 dbus_bool_t ret = FALSE;
2192 _dbus_assert (error != NULL);
2193 _DBUS_ASSERT_ERROR_IS_SET (error);
2195 connections = bus_context_get_connections (transaction->context);
2197 /* shortcut: don't compose the message unless someone wants it */
2198 if (connections->monitors == NULL)
2201 reply = dbus_message_new_error (in_reply_to,
2208 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2211 ret = bus_transaction_capture (transaction, NULL, reply);
2214 dbus_message_unref (reply);
2219 bus_transaction_send_from_driver (BusTransaction *transaction,
2220 DBusConnection *connection,
2221 DBusMessage *message)
2223 DBusError error = DBUS_ERROR_INIT;
2225 /* We have to set the sender to the driver, and have
2226 * to check security policy since it was not done in
2229 _dbus_verbose ("Sending %s %s %s from driver\n",
2230 dbus_message_get_interface (message) ?
2231 dbus_message_get_interface (message) : "(no interface)",
2232 dbus_message_get_member (message) ?
2233 dbus_message_get_member (message) : "(no member)",
2234 dbus_message_get_error_name (message) ?
2235 dbus_message_get_error_name (message) : "(no error name)");
2237 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2240 if (bus_connection_is_active (connection))
2242 if (!dbus_message_set_destination (message,
2243 bus_connection_get_name (connection)))
2247 /* bus driver never wants a reply */
2248 dbus_message_set_no_reply (message, TRUE);
2250 /* Capture it for monitors, even if the real recipient's receive policy
2251 * does not allow it to receive this message from us (which would be odd).
2253 if (!bus_transaction_capture (transaction, NULL, message))
2256 /* If security policy doesn't allow the message, we would silently
2257 * eat it; the driver doesn't care about getting a reply. However,
2258 * if we're actively capturing messages, it's nice to log that we
2259 * tried to send it and did not allow ourselves to do so.
2261 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2263 NULL, connection, connection, message, &error))
2265 if (!bus_transaction_capture_error_reply (transaction, &error, message))
2267 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2268 "message from dbus-daemon rejected but not enough "
2269 "memory to capture it");
2272 /* This is not fatal to the transaction so silently eat the disallowed
2273 * message (see reasoning above) */
2274 dbus_error_free (&error);
2278 return bus_transaction_send (transaction, connection, message);
2282 bus_transaction_send (BusTransaction *transaction,
2283 DBusConnection *connection,
2284 DBusMessage *message)
2286 MessageToSend *to_send;
2287 BusConnectionData *d;
2290 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2291 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2292 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2294 dbus_message_get_interface (message) ?
2295 dbus_message_get_interface (message) : "(unset)",
2296 dbus_message_get_member (message) ?
2297 dbus_message_get_member (message) : "(unset)",
2298 dbus_message_get_error_name (message) ?
2299 dbus_message_get_error_name (message) : "(unset)",
2300 dbus_connection_get_is_connected (connection) ?
2301 "" : " (disconnected)");
2303 _dbus_assert (dbus_message_get_sender (message) != NULL);
2305 if (!dbus_connection_get_is_connected (connection))
2306 return TRUE; /* silently ignore disconnected connections */
2308 d = BUS_CONNECTION_DATA (connection);
2309 _dbus_assert (d != NULL);
2311 to_send = dbus_new (MessageToSend, 1);
2312 if (to_send == NULL)
2317 to_send->preallocated = dbus_connection_preallocate_send (connection);
2318 if (to_send->preallocated == NULL)
2320 dbus_free (to_send);
2324 dbus_message_ref (message);
2325 to_send->message = message;
2326 to_send->transaction = transaction;
2328 _dbus_verbose ("about to prepend message\n");
2330 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2332 message_to_send_free (connection, to_send);
2336 _dbus_verbose ("prepended message\n");
2338 /* See if we already had this connection in the list
2339 * for this transaction. If we have a pending message,
2340 * then we should already be in transaction->connections
2342 link = _dbus_list_get_first_link (&d->transaction_messages);
2343 _dbus_assert (link->data == to_send);
2344 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2345 while (link != NULL)
2347 MessageToSend *m = link->data;
2348 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2350 if (m->transaction == transaction)
2358 if (!_dbus_list_prepend (&transaction->connections, connection))
2360 _dbus_list_remove (&d->transaction_messages, to_send);
2361 message_to_send_free (connection, to_send);
2370 transaction_free (BusTransaction *transaction)
2372 _dbus_assert (transaction->connections == NULL);
2374 free_cancel_hooks (transaction);
2376 dbus_free (transaction);
2380 connection_cancel_transaction (DBusConnection *connection,
2381 BusTransaction *transaction)
2384 BusConnectionData *d;
2386 d = BUS_CONNECTION_DATA (connection);
2387 _dbus_assert (d != NULL);
2389 link = _dbus_list_get_first_link (&d->transaction_messages);
2390 while (link != NULL)
2392 MessageToSend *m = link->data;
2393 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2395 if (m->transaction == transaction)
2397 _dbus_list_remove_link (&d->transaction_messages,
2400 message_to_send_free (connection, m);
2408 bus_transaction_cancel_and_free (BusTransaction *transaction)
2410 DBusConnection *connection;
2412 _dbus_verbose ("TRANSACTION: cancelled\n");
2414 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2415 connection_cancel_transaction (connection, transaction);
2417 _dbus_list_foreach (&transaction->cancel_hooks,
2418 cancel_hook_cancel, NULL);
2420 transaction_free (transaction);
2424 connection_execute_transaction (DBusConnection *connection,
2425 BusTransaction *transaction)
2428 BusConnectionData *d;
2430 d = BUS_CONNECTION_DATA (connection);
2431 _dbus_assert (d != NULL);
2433 /* Send the queue in order (FIFO) */
2434 link = _dbus_list_get_last_link (&d->transaction_messages);
2435 while (link != NULL)
2437 MessageToSend *m = link->data;
2438 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2440 if (m->transaction == transaction)
2442 _dbus_list_remove_link (&d->transaction_messages,
2445 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2447 dbus_connection_send_preallocated (connection,
2452 m->preallocated = NULL; /* so we don't double-free it */
2454 message_to_send_free (connection, m);
2462 bus_transaction_execute_and_free (BusTransaction *transaction)
2464 /* For each connection in transaction->connections
2467 DBusConnection *connection;
2469 _dbus_verbose ("TRANSACTION: executing\n");
2471 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2472 connection_execute_transaction (connection, transaction);
2474 transaction_free (transaction);
2478 bus_connection_remove_transactions (DBusConnection *connection)
2480 MessageToSend *to_send;
2481 BusConnectionData *d;
2483 d = BUS_CONNECTION_DATA (connection);
2484 _dbus_assert (d != NULL);
2486 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2488 /* only has an effect for the first MessageToSend listing this transaction */
2489 _dbus_list_remove (&to_send->transaction->connections,
2492 _dbus_list_remove (&d->transaction_messages, to_send);
2493 message_to_send_free (connection, to_send);
2498 * Converts the DBusError to a message reply
2501 bus_transaction_send_error_reply (BusTransaction *transaction,
2502 DBusConnection *connection,
2503 const DBusError *error,
2504 DBusMessage *in_reply_to)
2508 _dbus_assert (error != NULL);
2509 _DBUS_ASSERT_ERROR_IS_SET (error);
2511 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2512 error->name, error->message);
2514 reply = dbus_message_new_error (in_reply_to,
2520 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2522 dbus_message_unref (reply);
2526 dbus_message_unref (reply);
2532 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2533 BusTransactionCancelFunction cancel_function,
2535 DBusFreeFunction free_data_function)
2539 ch = dbus_new (CancelHook, 1);
2543 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2544 cancel_function, data);
2546 ch->cancel_function = cancel_function;
2548 ch->free_data_function = free_data_function;
2550 /* It's important that the hooks get run in reverse order that they
2553 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2563 bus_connections_get_n_active (BusConnections *connections)
2565 return connections->n_completed;
2569 bus_connections_get_n_incomplete (BusConnections *connections)
2571 return connections->n_incomplete;
2574 #ifdef DBUS_ENABLE_STATS
2576 bus_connections_get_total_match_rules (BusConnections *connections)
2578 return connections->total_match_rules;
2582 bus_connections_get_peak_match_rules (BusConnections *connections)
2584 return connections->peak_match_rules;
2588 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2590 return connections->peak_match_rules_per_conn;
2594 bus_connections_get_total_bus_names (BusConnections *connections)
2596 return connections->total_bus_names;
2600 bus_connections_get_peak_bus_names (BusConnections *connections)
2602 return connections->peak_bus_names;
2606 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2608 return connections->peak_bus_names_per_conn;
2612 bus_connection_get_peak_match_rules (DBusConnection *connection)
2614 BusConnectionData *d;
2616 d = BUS_CONNECTION_DATA (connection);
2617 return d->peak_match_rules;
2621 bus_connection_get_peak_bus_names (DBusConnection *connection)
2623 BusConnectionData *d;
2625 d = BUS_CONNECTION_DATA (connection);
2626 return d->peak_bus_names;
2628 #endif /* DBUS_ENABLE_STATS */
2631 bus_connection_is_monitor (DBusConnection *connection)
2633 BusConnectionData *d;
2635 d = BUS_CONNECTION_DATA (connection);
2637 return d != NULL && d->link_in_monitors != NULL;
2641 bcd_add_monitor_rules (BusConnectionData *d,
2642 DBusConnection *connection,
2645 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2650 mm = bus_matchmaker_new ();
2655 d->connections->monitor_matchmaker = mm;
2658 for (iter = _dbus_list_get_first_link (rules);
2660 iter = _dbus_list_get_next_link (rules, iter))
2662 if (!bus_matchmaker_add_rule (mm, iter->data))
2664 bus_matchmaker_disconnected (mm, connection);
2673 bcd_drop_monitor_rules (BusConnectionData *d,
2674 DBusConnection *connection)
2676 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2679 bus_matchmaker_disconnected (mm, connection);
2683 bus_connection_be_monitor (DBusConnection *connection,
2684 BusTransaction *transaction,
2688 BusConnectionData *d;
2693 d = BUS_CONNECTION_DATA (connection);
2694 _dbus_assert (d != NULL);
2696 link = _dbus_list_alloc_link (connection);
2700 BUS_SET_OOM (error);
2704 if (!bcd_add_monitor_rules (d, connection, rules))
2706 _dbus_list_free_link (link);
2707 BUS_SET_OOM (error);
2711 /* release all its names */
2712 if (!_dbus_list_copy (&d->services_owned, &tmp))
2714 bcd_drop_monitor_rules (d, connection);
2715 _dbus_list_free_link (link);
2716 BUS_SET_OOM (error);
2720 for (iter = _dbus_list_get_first_link (&tmp);
2722 iter = _dbus_list_get_next_link (&tmp, iter))
2724 BusService *service = iter->data;
2726 /* This call is transactional: if there isn't enough memory to
2727 * do everything, then the service gets all its names back when
2728 * the transaction is cancelled due to OOM. */
2729 if (!bus_service_remove_owner (service, connection, transaction, error))
2731 bcd_drop_monitor_rules (d, connection);
2732 _dbus_list_free_link (link);
2733 _dbus_list_clear (&tmp);
2738 /* We have now done everything that can fail, so there is no problem
2739 * with doing the irrevocable stuff. */
2741 _dbus_list_clear (&tmp);
2743 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2744 "Connection %s (%s) became a monitor.", d->name,
2745 d->cached_loginfo_string);
2747 if (d->n_match_rules > 0)
2751 mm = bus_context_get_matchmaker (d->connections->context);
2752 bus_matchmaker_disconnected (mm, connection);
2755 /* flag it as a monitor */
2756 d->link_in_monitors = link;
2757 _dbus_list_append_link (&d->connections->monitors, link);
2759 /* it isn't allowed to reply, and it is no longer relevant whether it
2760 * receives replies */
2761 bus_connection_drop_pending_replies (d->connections, connection);