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;
1227 * Checks whether the connection is registered with the message bus.
1229 * @param connection the connection
1230 * @returns #TRUE if we're an active message bus participant
1233 bus_connection_is_active (DBusConnection *connection)
1235 BusConnectionData *d;
1237 d = BUS_CONNECTION_DATA (connection);
1239 return d != NULL && d->name != NULL;
1243 bus_connection_preallocate_oom_error (DBusConnection *connection)
1245 DBusMessage *message;
1246 DBusPreallocatedSend *preallocated;
1247 BusConnectionData *d;
1249 d = BUS_CONNECTION_DATA (connection);
1251 _dbus_assert (d != NULL);
1253 if (d->oom_preallocated != NULL)
1256 preallocated = dbus_connection_preallocate_send (connection);
1257 if (preallocated == NULL)
1260 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1262 if (message == NULL)
1264 dbus_connection_free_preallocated_send (connection, preallocated);
1268 /* d->name may be NULL, but that is OK */
1269 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1270 !dbus_message_set_destination (message, d->name) ||
1271 !dbus_message_set_sender (message,
1274 dbus_connection_free_preallocated_send (connection, preallocated);
1275 dbus_message_unref (message);
1279 /* set reply serial to placeholder value just so space is already allocated
1282 if (!dbus_message_set_reply_serial (message, 14))
1284 dbus_connection_free_preallocated_send (connection, preallocated);
1285 dbus_message_unref (message);
1289 d->oom_message = message;
1290 d->oom_preallocated = preallocated;
1296 bus_connection_send_oom_error (DBusConnection *connection,
1297 DBusMessage *in_reply_to)
1299 BusConnectionData *d;
1301 d = BUS_CONNECTION_DATA (connection);
1303 _dbus_assert (d != NULL);
1304 _dbus_assert (d->oom_message != NULL);
1306 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1307 "dbus-daemon transaction failed (OOM), sending error to "
1308 "sender %s", bus_connection_get_loginfo (connection));
1310 /* should always succeed since we set it to a placeholder earlier */
1311 if (!dbus_message_set_reply_serial (d->oom_message,
1312 dbus_message_get_serial (in_reply_to)))
1313 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1315 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1317 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1318 d->oom_message, NULL);
1320 dbus_message_unref (d->oom_message);
1321 d->oom_message = NULL;
1322 d->oom_preallocated = NULL;
1325 #ifdef DBUS_ENABLE_STATS
1327 update_peak (int *peak,
1336 bus_connection_add_match_rule_link (DBusConnection *connection,
1339 BusConnectionData *d;
1341 d = BUS_CONNECTION_DATA (connection);
1342 _dbus_assert (d != NULL);
1344 _dbus_list_append_link (&d->match_rules, link);
1346 d->n_match_rules += 1;
1348 #ifdef DBUS_ENABLE_STATS
1349 update_peak (&d->peak_match_rules, d->n_match_rules);
1350 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1352 d->connections->total_match_rules += 1;
1353 update_peak (&d->connections->peak_match_rules,
1354 d->connections->total_match_rules);
1359 bus_connection_add_match_rule (DBusConnection *connection,
1364 link = _dbus_list_alloc_link (rule);
1369 bus_connection_add_match_rule_link (connection, link);
1375 bus_connection_remove_match_rule (DBusConnection *connection,
1378 BusConnectionData *d;
1380 d = BUS_CONNECTION_DATA (connection);
1381 _dbus_assert (d != NULL);
1383 _dbus_list_remove_last (&d->match_rules, rule);
1385 d->n_match_rules -= 1;
1386 _dbus_assert (d->n_match_rules >= 0);
1388 #ifdef DBUS_ENABLE_STATS
1389 d->connections->total_match_rules -= 1;
1394 bus_connection_get_n_match_rules (DBusConnection *connection)
1396 BusConnectionData *d;
1398 d = BUS_CONNECTION_DATA (connection);
1399 _dbus_assert (d != NULL);
1401 return d->n_match_rules;
1405 bus_connection_add_owned_service_link (DBusConnection *connection,
1408 BusConnectionData *d;
1410 d = BUS_CONNECTION_DATA (connection);
1411 _dbus_assert (d != NULL);
1413 _dbus_list_append_link (&d->services_owned, link);
1415 d->n_services_owned += 1;
1417 #ifdef DBUS_ENABLE_STATS
1418 update_peak (&d->peak_bus_names, d->n_services_owned);
1419 update_peak (&d->connections->peak_bus_names_per_conn,
1420 d->n_services_owned);
1422 d->connections->total_bus_names += 1;
1423 update_peak (&d->connections->peak_bus_names,
1424 d->connections->total_bus_names);
1429 bus_connection_add_owned_service (DBusConnection *connection,
1430 BusService *service)
1434 link = _dbus_list_alloc_link (service);
1439 bus_connection_add_owned_service_link (connection, link);
1445 bus_connection_remove_owned_service (DBusConnection *connection,
1446 BusService *service)
1448 BusConnectionData *d;
1450 d = BUS_CONNECTION_DATA (connection);
1451 _dbus_assert (d != NULL);
1453 _dbus_list_remove_last (&d->services_owned, service);
1455 d->n_services_owned -= 1;
1456 _dbus_assert (d->n_services_owned >= 0);
1458 #ifdef DBUS_ENABLE_STATS
1459 d->connections->total_bus_names -= 1;
1464 bus_connection_get_n_services_owned (DBusConnection *connection)
1466 BusConnectionData *d;
1468 d = BUS_CONNECTION_DATA (connection);
1469 _dbus_assert (d != NULL);
1471 return d->n_services_owned;
1475 bus_connection_complete (DBusConnection *connection,
1476 const DBusString *name,
1479 BusConnectionData *d;
1482 d = BUS_CONNECTION_DATA (connection);
1483 _dbus_assert (d != NULL);
1484 _dbus_assert (d->name == NULL);
1485 _dbus_assert (d->policy == NULL);
1487 _dbus_assert (!bus_connection_is_active (connection));
1489 if (!_dbus_string_copy_data (name, &d->name))
1491 BUS_SET_OOM (error);
1495 _dbus_assert (d->name != NULL);
1497 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1499 d->policy = bus_context_create_client_policy (d->connections->context,
1503 /* we may have a NULL policy on OOM or error getting list of
1504 * groups for a user. In the latter case we don't handle it so
1505 * well currently, as it will just keep failing over and over.
1508 if (d->policy == NULL)
1510 _dbus_verbose ("Failed to create security policy for connection %p\n",
1512 _DBUS_ASSERT_ERROR_IS_SET (error);
1513 dbus_free (d->name);
1518 if (dbus_connection_get_unix_user (connection, &uid))
1520 if (!adjust_connections_for_uid (d->connections,
1525 /* Create and cache a string which holds information about the
1526 * peer process; used for logging purposes.
1528 if (!cache_peer_loginfo_string (d, connection))
1531 /* Now the connection is active, move it between lists */
1532 _dbus_list_unlink (&d->connections->incomplete,
1533 d->link_in_connection_list);
1534 d->connections->n_incomplete -= 1;
1535 _dbus_list_append_link (&d->connections->completed,
1536 d->link_in_connection_list);
1537 d->connections->n_completed += 1;
1539 _dbus_assert (d->connections->n_incomplete >= 0);
1540 _dbus_assert (d->connections->n_completed > 0);
1542 /* If we have dropped below the max. number of incomplete
1543 * connections, start accept()ing again */
1544 bus_context_check_all_watches (d->connections->context);
1546 /* See if we can remove the timeout */
1547 bus_connections_expire_incomplete (d->connections);
1549 _dbus_assert (bus_connection_is_active (connection));
1553 BUS_SET_OOM (error);
1554 dbus_free (d->name);
1557 bus_client_policy_unref (d->policy);
1563 bus_connections_reload_policy (BusConnections *connections,
1566 BusConnectionData *d;
1567 DBusConnection *connection;
1570 _dbus_assert (connections != NULL);
1571 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1573 for (link = _dbus_list_get_first_link (&(connections->completed));
1575 link = _dbus_list_get_next_link (&(connections->completed), link))
1577 connection = link->data;
1578 d = BUS_CONNECTION_DATA (connection);
1579 _dbus_assert (d != NULL);
1580 _dbus_assert (d->policy != NULL);
1582 bus_client_policy_unref (d->policy);
1583 d->policy = bus_context_create_client_policy (connections->context,
1586 if (d->policy == NULL)
1588 _dbus_verbose ("Failed to create security policy for connection %p\n",
1590 _DBUS_ASSERT_ERROR_IS_SET (error);
1599 bus_connection_get_name (DBusConnection *connection)
1601 BusConnectionData *d;
1603 d = BUS_CONNECTION_DATA (connection);
1604 _dbus_assert (d != NULL);
1610 * Check whether completing the passed-in connection would
1611 * exceed limits, and if so set error and return #FALSE
1614 bus_connections_check_limits (BusConnections *connections,
1615 DBusConnection *requesting_completion,
1620 if (connections->n_completed >=
1621 bus_context_get_max_completed_connections (connections->context))
1623 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1624 "The maximum number of active connections has been reached");
1628 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1630 if (get_connections_for_uid (connections, uid) >=
1631 bus_context_get_max_connections_per_user (connections->context))
1633 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1634 "The maximum number of active connections for UID %lu has been reached",
1644 bus_pending_reply_free (BusPendingReply *pending)
1646 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1648 pending->will_send_reply,
1649 pending->will_get_reply,
1650 pending->reply_serial);
1652 dbus_free (pending);
1656 bus_pending_reply_send_no_reply (BusConnections *connections,
1657 BusTransaction *transaction,
1658 BusPendingReply *pending)
1660 DBusMessage *message;
1661 DBusMessageIter iter;
1667 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1668 if (message == NULL)
1671 dbus_message_set_no_reply (message, TRUE);
1673 if (!dbus_message_set_reply_serial (message,
1674 pending->reply_serial))
1677 if (!dbus_message_set_error_name (message,
1678 DBUS_ERROR_NO_REPLY))
1681 /* If you change these messages, adjust test/dbus-daemon.c to match */
1682 if (pending->will_send_reply == NULL)
1683 errmsg = "Message recipient disconnected from message bus without replying";
1685 errmsg = "Message did not receive a reply (timeout by message bus)";
1687 dbus_message_iter_init_append (message, &iter);
1688 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1691 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1698 dbus_message_unref (message);
1703 bus_pending_reply_expired (BusExpireList *list,
1707 BusPendingReply *pending = link->data;
1708 BusConnections *connections = data;
1709 BusTransaction *transaction;
1711 /* No reply is forthcoming. So nuke it if we can. If not,
1712 * leave it in the list to try expiring again later when we
1716 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1718 pending->will_send_reply,
1719 pending->will_get_reply,
1720 pending->reply_serial);
1722 transaction = bus_transaction_new (connections->context);
1723 if (transaction == NULL)
1726 if (!bus_pending_reply_send_no_reply (connections,
1730 bus_transaction_cancel_and_free (transaction);
1734 bus_expire_list_remove_link (connections->pending_replies, link);
1736 bus_pending_reply_free (pending);
1737 bus_transaction_execute_and_free (transaction);
1743 bus_connection_drop_pending_replies (BusConnections *connections,
1744 DBusConnection *connection)
1746 /* The DBusConnection is almost 100% finalized here, so you can't
1747 * do anything with it except check for pointer equality
1751 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1754 link = bus_expire_list_get_first_link (connections->pending_replies);
1755 while (link != NULL)
1758 BusPendingReply *pending;
1760 next = bus_expire_list_get_next_link (connections->pending_replies,
1762 pending = link->data;
1764 if (pending->will_get_reply == connection)
1766 /* We don't need to track this pending reply anymore */
1768 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1770 pending->will_send_reply,
1771 pending->will_get_reply,
1772 pending->reply_serial);
1774 bus_expire_list_remove_link (connections->pending_replies,
1776 bus_pending_reply_free (pending);
1778 else if (pending->will_send_reply == connection)
1780 /* The reply isn't going to be sent, so set things
1781 * up so it will be expired right away
1783 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1785 pending->will_send_reply,
1786 pending->will_get_reply,
1787 pending->reply_serial);
1789 pending->will_send_reply = NULL;
1790 pending->expire_item.added_tv_sec = 0;
1791 pending->expire_item.added_tv_usec = 0;
1793 bus_expire_list_recheck_immediately (connections->pending_replies);
1803 BusPendingReply *pending;
1804 BusConnections *connections;
1805 } CancelPendingReplyData;
1808 cancel_pending_reply (void *data)
1810 CancelPendingReplyData *d = data;
1812 _dbus_verbose ("d = %p\n", d);
1814 if (!bus_expire_list_remove (d->connections->pending_replies,
1815 &d->pending->expire_item))
1816 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1818 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1822 cancel_pending_reply_data_free (void *data)
1824 CancelPendingReplyData *d = data;
1826 _dbus_verbose ("d = %p\n", d);
1828 /* d->pending should be either freed or still
1829 * in the list of pending replies (owned by someone
1837 * Record that a reply is allowed; return TRUE on success.
1840 bus_connections_expect_reply (BusConnections *connections,
1841 BusTransaction *transaction,
1842 DBusConnection *will_get_reply,
1843 DBusConnection *will_send_reply,
1844 DBusMessage *reply_to_this,
1847 BusPendingReply *pending;
1848 dbus_uint32_t reply_serial;
1850 CancelPendingReplyData *cprd;
1853 _dbus_assert (will_get_reply != NULL);
1854 _dbus_assert (will_send_reply != NULL);
1855 _dbus_assert (reply_to_this != NULL);
1857 if (dbus_message_get_no_reply (reply_to_this))
1858 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1860 reply_serial = dbus_message_get_serial (reply_to_this);
1862 link = bus_expire_list_get_first_link (connections->pending_replies);
1864 while (link != NULL)
1866 pending = link->data;
1868 if (pending->reply_serial == reply_serial &&
1869 pending->will_get_reply == will_get_reply &&
1870 pending->will_send_reply == will_send_reply)
1872 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1873 "Message has the same reply serial as a currently-outstanding existing method call");
1877 link = bus_expire_list_get_next_link (connections->pending_replies,
1879 if (pending->will_get_reply == will_get_reply)
1884 bus_context_get_max_replies_per_connection (connections->context))
1886 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1887 "The maximum number of pending replies per connection has been reached");
1891 pending = dbus_new0 (BusPendingReply, 1);
1892 if (pending == NULL)
1894 BUS_SET_OOM (error);
1898 #ifdef DBUS_ENABLE_VERBOSE_MODE
1899 /* so we can see a not-yet-added pending reply */
1900 pending->expire_item.added_tv_sec = 1;
1901 pending->expire_item.added_tv_usec = 1;
1904 pending->will_get_reply = will_get_reply;
1905 pending->will_send_reply = will_send_reply;
1906 pending->reply_serial = reply_serial;
1908 cprd = dbus_new0 (CancelPendingReplyData, 1);
1911 BUS_SET_OOM (error);
1912 bus_pending_reply_free (pending);
1916 if (!bus_expire_list_add (connections->pending_replies,
1917 &pending->expire_item))
1919 BUS_SET_OOM (error);
1921 bus_pending_reply_free (pending);
1925 if (!bus_transaction_add_cancel_hook (transaction,
1926 cancel_pending_reply,
1928 cancel_pending_reply_data_free))
1930 BUS_SET_OOM (error);
1931 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1933 bus_pending_reply_free (pending);
1937 cprd->pending = pending;
1938 cprd->connections = connections;
1940 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1941 &pending->expire_item.added_tv_usec);
1943 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1945 pending->will_send_reply,
1946 pending->will_get_reply,
1947 pending->reply_serial);
1955 BusConnections *connections;
1956 } CheckPendingReplyData;
1959 cancel_check_pending_reply (void *data)
1961 CheckPendingReplyData *d = data;
1963 _dbus_verbose ("d = %p\n",d);
1965 bus_expire_list_add_link (d->connections->pending_replies,
1971 check_pending_reply_data_free (void *data)
1973 CheckPendingReplyData *d = data;
1975 _dbus_verbose ("d = %p\n",d);
1977 if (d->link != NULL)
1979 BusPendingReply *pending = d->link->data;
1981 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1982 &pending->expire_item));
1984 bus_pending_reply_free (pending);
1985 _dbus_list_free_link (d->link);
1992 * Check whether a reply is allowed, remove BusPendingReply
1993 * if so, return TRUE if so.
1996 bus_connections_check_reply (BusConnections *connections,
1997 BusTransaction *transaction,
1998 DBusConnection *sending_reply,
1999 DBusConnection *receiving_reply,
2003 CheckPendingReplyData *cprd;
2005 dbus_uint32_t reply_serial;
2007 _dbus_assert (sending_reply != NULL);
2008 _dbus_assert (receiving_reply != NULL);
2010 reply_serial = dbus_message_get_reply_serial (reply);
2012 link = bus_expire_list_get_first_link (connections->pending_replies);
2013 while (link != NULL)
2015 BusPendingReply *pending = link->data;
2017 if (pending->reply_serial == reply_serial &&
2018 pending->will_get_reply == receiving_reply &&
2019 pending->will_send_reply == sending_reply)
2021 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2025 link = bus_expire_list_get_next_link (connections->pending_replies,
2031 _dbus_verbose ("No pending reply expected\n");
2036 cprd = dbus_new0 (CheckPendingReplyData, 1);
2039 BUS_SET_OOM (error);
2043 if (!bus_transaction_add_cancel_hook (transaction,
2044 cancel_check_pending_reply,
2046 check_pending_reply_data_free))
2048 BUS_SET_OOM (error);
2054 cprd->connections = connections;
2056 bus_expire_list_unlink (connections->pending_replies,
2059 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2067 * Note that this is fairly fragile; in particular, don't try to use
2068 * one transaction across any main loop iterations.
2073 BusTransaction *transaction;
2074 DBusMessage *message;
2075 DBusPreallocatedSend *preallocated;
2080 BusTransactionCancelFunction cancel_function;
2081 DBusFreeFunction free_data_function;
2085 struct BusTransaction
2087 DBusList *connections;
2088 BusContext *context;
2089 DBusList *cancel_hooks;
2093 message_to_send_free (DBusConnection *connection,
2094 MessageToSend *to_send)
2096 if (to_send->message)
2097 dbus_message_unref (to_send->message);
2099 if (to_send->preallocated)
2100 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2102 dbus_free (to_send);
2106 cancel_hook_cancel (void *element,
2109 CancelHook *ch = element;
2111 _dbus_verbose ("Running transaction cancel hook\n");
2113 if (ch->cancel_function)
2114 (* ch->cancel_function) (ch->data);
2118 cancel_hook_free (void *element,
2121 CancelHook *ch = element;
2123 if (ch->free_data_function)
2124 (* ch->free_data_function) (ch->data);
2130 free_cancel_hooks (BusTransaction *transaction)
2132 _dbus_list_foreach (&transaction->cancel_hooks,
2133 cancel_hook_free, NULL);
2135 _dbus_list_clear (&transaction->cancel_hooks);
2139 bus_transaction_new (BusContext *context)
2141 BusTransaction *transaction;
2143 transaction = dbus_new0 (BusTransaction, 1);
2144 if (transaction == NULL)
2147 transaction->context = context;
2153 bus_transaction_get_context (BusTransaction *transaction)
2155 return transaction->context;
2159 * Reserve enough memory to capture the given message if the
2160 * transaction goes through.
2163 bus_transaction_capture (BusTransaction *transaction,
2164 DBusConnection *sender,
2165 DBusMessage *message)
2167 BusConnections *connections;
2170 DBusList *recipients = NULL;
2171 dbus_bool_t ret = FALSE;
2173 connections = bus_context_get_connections (transaction->context);
2175 /* shortcut: don't compose the message unless someone wants it */
2176 if (connections->monitors == NULL)
2179 mm = connections->monitor_matchmaker;
2180 /* This is non-null if there has ever been a monitor - we don't GC it.
2181 * There's little point, since there is up to 1 per process. */
2182 _dbus_assert (mm != NULL);
2184 if (!bus_matchmaker_get_recipients (mm, connections, sender, NULL, message,
2188 for (link = _dbus_list_get_first_link (&recipients);
2190 link = _dbus_list_get_next_link (&recipients, link))
2192 DBusConnection *recipient = link->data;
2194 if (!bus_transaction_send (transaction, recipient, message))
2201 _dbus_list_clear (&recipients);
2206 bus_transaction_capture_error_reply (BusTransaction *transaction,
2207 const DBusError *error,
2208 DBusMessage *in_reply_to)
2210 BusConnections *connections;
2212 dbus_bool_t ret = FALSE;
2214 _dbus_assert (error != NULL);
2215 _DBUS_ASSERT_ERROR_IS_SET (error);
2217 connections = bus_context_get_connections (transaction->context);
2219 /* shortcut: don't compose the message unless someone wants it */
2220 if (connections->monitors == NULL)
2223 reply = dbus_message_new_error (in_reply_to,
2230 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2233 ret = bus_transaction_capture (transaction, NULL, reply);
2236 dbus_message_unref (reply);
2241 bus_transaction_send_from_driver (BusTransaction *transaction,
2242 DBusConnection *connection,
2243 DBusMessage *message)
2245 DBusError error = DBUS_ERROR_INIT;
2247 /* We have to set the sender to the driver, and have
2248 * to check security policy since it was not done in
2251 _dbus_verbose ("Sending %s %s %s from driver\n",
2252 dbus_message_get_interface (message) ?
2253 dbus_message_get_interface (message) : "(no interface)",
2254 dbus_message_get_member (message) ?
2255 dbus_message_get_member (message) : "(no member)",
2256 dbus_message_get_error_name (message) ?
2257 dbus_message_get_error_name (message) : "(no error name)");
2259 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2262 if (bus_connection_is_active (connection))
2264 if (!dbus_message_set_destination (message,
2265 bus_connection_get_name (connection)))
2269 /* bus driver never wants a reply */
2270 dbus_message_set_no_reply (message, TRUE);
2272 /* Capture it for monitors, even if the real recipient's receive policy
2273 * does not allow it to receive this message from us (which would be odd).
2275 if (!bus_transaction_capture (transaction, NULL, message))
2278 /* If security policy doesn't allow the message, we would silently
2279 * eat it; the driver doesn't care about getting a reply. However,
2280 * if we're actively capturing messages, it's nice to log that we
2281 * tried to send it and did not allow ourselves to do so.
2283 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2285 NULL, connection, connection, message, &error))
2287 if (!bus_transaction_capture_error_reply (transaction, &error, message))
2289 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2290 "message from dbus-daemon rejected but not enough "
2291 "memory to capture it");
2294 /* This is not fatal to the transaction so silently eat the disallowed
2295 * message (see reasoning above) */
2296 dbus_error_free (&error);
2300 return bus_transaction_send (transaction, connection, message);
2304 bus_transaction_send (BusTransaction *transaction,
2305 DBusConnection *connection,
2306 DBusMessage *message)
2308 MessageToSend *to_send;
2309 BusConnectionData *d;
2312 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2313 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2314 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2316 dbus_message_get_interface (message) ?
2317 dbus_message_get_interface (message) : "(unset)",
2318 dbus_message_get_member (message) ?
2319 dbus_message_get_member (message) : "(unset)",
2320 dbus_message_get_error_name (message) ?
2321 dbus_message_get_error_name (message) : "(unset)",
2322 dbus_connection_get_is_connected (connection) ?
2323 "" : " (disconnected)");
2325 _dbus_assert (dbus_message_get_sender (message) != NULL);
2327 if (!dbus_connection_get_is_connected (connection))
2328 return TRUE; /* silently ignore disconnected connections */
2330 d = BUS_CONNECTION_DATA (connection);
2331 _dbus_assert (d != NULL);
2333 to_send = dbus_new (MessageToSend, 1);
2334 if (to_send == NULL)
2339 to_send->preallocated = dbus_connection_preallocate_send (connection);
2340 if (to_send->preallocated == NULL)
2342 dbus_free (to_send);
2346 dbus_message_ref (message);
2347 to_send->message = message;
2348 to_send->transaction = transaction;
2350 _dbus_verbose ("about to prepend message\n");
2352 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2354 message_to_send_free (connection, to_send);
2358 _dbus_verbose ("prepended message\n");
2360 /* See if we already had this connection in the list
2361 * for this transaction. If we have a pending message,
2362 * then we should already be in transaction->connections
2364 link = _dbus_list_get_first_link (&d->transaction_messages);
2365 _dbus_assert (link->data == to_send);
2366 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2367 while (link != NULL)
2369 MessageToSend *m = link->data;
2370 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2372 if (m->transaction == transaction)
2380 if (!_dbus_list_prepend (&transaction->connections, connection))
2382 _dbus_list_remove (&d->transaction_messages, to_send);
2383 message_to_send_free (connection, to_send);
2392 transaction_free (BusTransaction *transaction)
2394 _dbus_assert (transaction->connections == NULL);
2396 free_cancel_hooks (transaction);
2398 dbus_free (transaction);
2402 connection_cancel_transaction (DBusConnection *connection,
2403 BusTransaction *transaction)
2406 BusConnectionData *d;
2408 d = BUS_CONNECTION_DATA (connection);
2409 _dbus_assert (d != NULL);
2411 link = _dbus_list_get_first_link (&d->transaction_messages);
2412 while (link != NULL)
2414 MessageToSend *m = link->data;
2415 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2417 if (m->transaction == transaction)
2419 _dbus_list_remove_link (&d->transaction_messages,
2422 message_to_send_free (connection, m);
2430 bus_transaction_cancel_and_free (BusTransaction *transaction)
2432 DBusConnection *connection;
2434 _dbus_verbose ("TRANSACTION: cancelled\n");
2436 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2437 connection_cancel_transaction (connection, transaction);
2439 _dbus_list_foreach (&transaction->cancel_hooks,
2440 cancel_hook_cancel, NULL);
2442 transaction_free (transaction);
2446 connection_execute_transaction (DBusConnection *connection,
2447 BusTransaction *transaction)
2450 BusConnectionData *d;
2452 d = BUS_CONNECTION_DATA (connection);
2453 _dbus_assert (d != NULL);
2455 /* Send the queue in order (FIFO) */
2456 link = _dbus_list_get_last_link (&d->transaction_messages);
2457 while (link != NULL)
2459 MessageToSend *m = link->data;
2460 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2462 if (m->transaction == transaction)
2464 _dbus_list_remove_link (&d->transaction_messages,
2467 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2469 dbus_connection_send_preallocated (connection,
2474 m->preallocated = NULL; /* so we don't double-free it */
2476 message_to_send_free (connection, m);
2484 bus_transaction_execute_and_free (BusTransaction *transaction)
2486 /* For each connection in transaction->connections
2489 DBusConnection *connection;
2491 _dbus_verbose ("TRANSACTION: executing\n");
2493 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2494 connection_execute_transaction (connection, transaction);
2496 transaction_free (transaction);
2500 bus_connection_remove_transactions (DBusConnection *connection)
2502 MessageToSend *to_send;
2503 BusConnectionData *d;
2505 d = BUS_CONNECTION_DATA (connection);
2506 _dbus_assert (d != NULL);
2508 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2510 /* only has an effect for the first MessageToSend listing this transaction */
2511 _dbus_list_remove (&to_send->transaction->connections,
2514 _dbus_list_remove (&d->transaction_messages, to_send);
2515 message_to_send_free (connection, to_send);
2520 * Converts the DBusError to a message reply
2523 bus_transaction_send_error_reply (BusTransaction *transaction,
2524 DBusConnection *connection,
2525 const DBusError *error,
2526 DBusMessage *in_reply_to)
2530 _dbus_assert (error != NULL);
2531 _DBUS_ASSERT_ERROR_IS_SET (error);
2533 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2534 error->name, error->message);
2536 reply = dbus_message_new_error (in_reply_to,
2542 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2544 dbus_message_unref (reply);
2548 dbus_message_unref (reply);
2554 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2555 BusTransactionCancelFunction cancel_function,
2557 DBusFreeFunction free_data_function)
2561 ch = dbus_new (CancelHook, 1);
2565 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2566 cancel_function, data);
2568 ch->cancel_function = cancel_function;
2570 ch->free_data_function = free_data_function;
2572 /* It's important that the hooks get run in reverse order that they
2575 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2585 bus_connections_get_n_active (BusConnections *connections)
2587 return connections->n_completed;
2591 bus_connections_get_n_incomplete (BusConnections *connections)
2593 return connections->n_incomplete;
2596 #ifdef DBUS_ENABLE_STATS
2598 bus_connections_get_total_match_rules (BusConnections *connections)
2600 return connections->total_match_rules;
2604 bus_connections_get_peak_match_rules (BusConnections *connections)
2606 return connections->peak_match_rules;
2610 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2612 return connections->peak_match_rules_per_conn;
2616 bus_connections_get_total_bus_names (BusConnections *connections)
2618 return connections->total_bus_names;
2622 bus_connections_get_peak_bus_names (BusConnections *connections)
2624 return connections->peak_bus_names;
2628 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2630 return connections->peak_bus_names_per_conn;
2634 bus_connection_get_peak_match_rules (DBusConnection *connection)
2636 BusConnectionData *d;
2638 d = BUS_CONNECTION_DATA (connection);
2639 return d->peak_match_rules;
2643 bus_connection_get_peak_bus_names (DBusConnection *connection)
2645 BusConnectionData *d;
2647 d = BUS_CONNECTION_DATA (connection);
2648 return d->peak_bus_names;
2650 #endif /* DBUS_ENABLE_STATS */
2653 bus_connection_is_monitor (DBusConnection *connection)
2655 BusConnectionData *d;
2657 d = BUS_CONNECTION_DATA (connection);
2659 return d != NULL && d->link_in_monitors != NULL;
2663 bcd_add_monitor_rules (BusConnectionData *d,
2664 DBusConnection *connection,
2667 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2672 mm = bus_matchmaker_new ();
2677 d->connections->monitor_matchmaker = mm;
2680 for (iter = _dbus_list_get_first_link (rules);
2682 iter = _dbus_list_get_next_link (rules, iter))
2684 if (!bus_matchmaker_add_rule (mm, iter->data))
2686 bus_matchmaker_disconnected (mm, connection);
2695 bcd_drop_monitor_rules (BusConnectionData *d,
2696 DBusConnection *connection)
2698 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2701 bus_matchmaker_disconnected (mm, connection);
2705 bus_connection_be_monitor (DBusConnection *connection,
2706 BusTransaction *transaction,
2710 BusConnectionData *d;
2715 d = BUS_CONNECTION_DATA (connection);
2716 _dbus_assert (d != NULL);
2718 link = _dbus_list_alloc_link (connection);
2722 BUS_SET_OOM (error);
2726 if (!bcd_add_monitor_rules (d, connection, rules))
2728 _dbus_list_free_link (link);
2729 BUS_SET_OOM (error);
2733 /* release all its names */
2734 if (!_dbus_list_copy (&d->services_owned, &tmp))
2736 bcd_drop_monitor_rules (d, connection);
2737 _dbus_list_free_link (link);
2738 BUS_SET_OOM (error);
2742 for (iter = _dbus_list_get_first_link (&tmp);
2744 iter = _dbus_list_get_next_link (&tmp, iter))
2746 BusService *service = iter->data;
2748 /* This call is transactional: if there isn't enough memory to
2749 * do everything, then the service gets all its names back when
2750 * the transaction is cancelled due to OOM. */
2751 if (!bus_service_remove_owner (service, connection, transaction, error))
2753 bcd_drop_monitor_rules (d, connection);
2754 _dbus_list_free_link (link);
2755 _dbus_list_clear (&tmp);
2760 /* We have now done everything that can fail, so there is no problem
2761 * with doing the irrevocable stuff. */
2763 _dbus_list_clear (&tmp);
2765 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2766 "Connection %s (%s) became a monitor.", d->name,
2767 d->cached_loginfo_string);
2769 if (d->n_match_rules > 0)
2773 mm = bus_context_get_matchmaker (d->connections->context);
2774 bus_matchmaker_disconnected (mm, connection);
2777 /* flag it as a monitor */
2778 d->link_in_monitors = link;
2779 _dbus_list_append_link (&d->connections->monitors, link);
2781 /* it isn't allowed to reply, and it is no longer relevant whether it
2782 * receives replies */
2783 bus_connection_drop_pending_replies (d->connections, connection);