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"
35 #include <dbus/dbus-list.h>
36 #include <dbus/dbus-hash.h>
37 #include <dbus/dbus-timeout.h>
38 #include <dbus/dbus-connection-internal.h>
39 #include <dbus/dbus-internals.h>
40 #include <dbus/dbus-message-internal.h>
41 #ifdef DBUS_ENABLE_CYNARA
43 #include <cynara-session.h>
47 /* Trim executed commands to this length; we want to keep logs readable */
48 #define MAX_LOG_COMMAND_LEN 50
50 static void bus_connection_remove_transactions (DBusConnection *connection);
54 BusExpireItem expire_item;
56 DBusConnection *will_get_reply;
57 DBusConnection *will_send_reply;
59 dbus_uint32_t reply_serial;
66 DBusList *completed; /**< List of all completed connections */
67 int n_completed; /**< Length of completed list */
68 DBusList *incomplete; /**< List of all not-yet-active connections */
69 int n_incomplete; /**< Length of incomplete list */
71 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
72 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
73 int stamp; /**< Incrementing number */
74 BusExpireList *pending_replies; /**< List of pending replies */
76 /** List of all monitoring connections, a subset of completed.
77 * Each member is a #DBusConnection. */
79 BusMatchmaker *monitor_matchmaker;
81 #ifdef DBUS_ENABLE_STATS
82 int total_match_rules;
84 int peak_match_rules_per_conn;
88 int peak_bus_names_per_conn;
92 static dbus_int32_t connection_data_slot = -1;
96 BusConnections *connections;
97 DBusList *link_in_connection_list;
98 DBusConnection *connection;
99 DBusList *services_owned;
100 int n_services_owned;
101 DBusList *match_rules;
104 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
105 DBusMessage *oom_message;
106 DBusPreallocatedSend *oom_preallocated;
107 BusClientPolicy *policy;
108 DBusList *deferred_messages; /**< Queue of messages deferred due to pending policy check */
110 char *cached_loginfo_string;
111 BusSELinuxID *selinux_id;
112 BusAppArmorConfinement *apparmor_confinement;
114 long connection_tv_sec; /**< Time when we connected (seconds component) */
115 long connection_tv_usec; /**< Time when we connected (microsec component) */
116 int stamp; /**< connections->stamp last time we were traversed */
118 #ifdef DBUS_ENABLE_STATS
119 int peak_match_rules;
122 int n_pending_unix_fds;
123 DBusTimeout *pending_unix_fds_timeout;
125 /** non-NULL if and only if this is a monitor */
126 DBusList *link_in_monitors;
127 #ifdef DBUS_ENABLE_CYNARA
128 char *cynara_session_id;
132 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
136 static void bus_connection_drop_pending_replies (BusConnections *connections,
137 DBusConnection *connection);
139 static dbus_bool_t expire_incomplete_timeout (void *data);
141 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
144 bus_connection_get_loop (DBusConnection *connection)
146 BusConnectionData *d;
148 d = BUS_CONNECTION_DATA (connection);
149 _dbus_assert(d != NULL);
151 return bus_context_get_loop (d->connections->context);
156 get_connections_for_uid (BusConnections *connections,
162 /* val is NULL is 0 when it isn't in the hash yet */
164 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
167 current_count = _DBUS_POINTER_TO_INT (val);
169 return current_count;
173 adjust_connections_for_uid (BusConnections *connections,
179 current_count = get_connections_for_uid (connections, uid);
181 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
182 ": was %d adjustment %d making %d\n",
183 uid, current_count, adjustment, current_count + adjustment);
185 _dbus_assert (current_count >= 0);
187 current_count += adjustment;
189 _dbus_assert (current_count >= 0);
191 if (current_count == 0)
193 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
200 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
201 uid, _DBUS_INT_TO_POINTER (current_count));
203 /* only positive adjustment can fail as otherwise
204 * a hash entry should already exist
206 _dbus_assert (adjustment > 0 ||
207 (adjustment <= 0 && retval));
214 bus_connection_disconnected (DBusConnection *connection)
216 BusConnectionData *d;
218 BusMatchmaker *matchmaker;
220 d = BUS_CONNECTION_DATA (connection);
221 _dbus_assert (d != NULL);
223 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
224 d->name ? d->name : "(inactive)");
226 /* Delete our match rules */
227 if (d->n_match_rules > 0)
229 matchmaker = bus_context_get_matchmaker (d->connections->context);
230 bus_matchmaker_disconnected (matchmaker, connection);
233 /* Drop any service ownership. Unfortunately, this requires
234 * memory allocation and there doesn't seem to be a good way to
235 * handle it other than sleeping; we can't "fail" the operation of
236 * disconnecting a client, and preallocating a broadcast "service is
237 * now gone" message for every client-service pair seems kind of
240 while ((service = _dbus_list_get_last (&d->services_owned)))
242 BusTransaction *transaction;
247 dbus_error_init (&error);
249 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
250 _dbus_wait_for_memory ();
252 if (!bus_service_remove_owner (service, connection,
253 transaction, &error))
255 _DBUS_ASSERT_ERROR_IS_SET (&error);
257 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
259 dbus_error_free (&error);
260 bus_transaction_cancel_and_free (transaction);
261 _dbus_wait_for_memory ();
266 _dbus_verbose ("Failed to remove service owner: %s %s\n",
267 error.name, error.message);
268 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
272 bus_transaction_execute_and_free (transaction);
275 bus_connection_clear_deferred_messages(connection);
277 bus_dispatch_remove_connection (connection);
279 /* no more watching */
280 if (!dbus_connection_set_watch_functions (connection,
284 _dbus_assert_not_reached ("setting watch functions to NULL failed");
286 if (!dbus_connection_set_timeout_functions (connection,
290 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
292 dbus_connection_set_unix_user_function (connection,
294 dbus_connection_set_windows_user_function (connection,
297 dbus_connection_set_dispatch_status_function (connection,
300 if (d->pending_unix_fds_timeout)
302 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
303 d->pending_unix_fds_timeout);
304 _dbus_timeout_unref (d->pending_unix_fds_timeout);
306 d->pending_unix_fds_timeout = NULL;
307 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
309 bus_connection_remove_transactions (connection);
311 if (d->link_in_monitors != NULL)
313 BusMatchmaker *mm = d->connections->monitor_matchmaker;
316 bus_matchmaker_disconnected (mm, connection);
318 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
319 d->link_in_monitors = NULL;
322 if (d->link_in_connection_list != NULL)
328 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
329 d->link_in_connection_list = NULL;
330 d->connections->n_completed -= 1;
332 if (dbus_connection_get_unix_user (connection, &uid))
334 if (!adjust_connections_for_uid (d->connections,
336 _dbus_assert_not_reached ("adjusting downward should never fail");
341 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
342 d->link_in_connection_list = NULL;
343 d->connections->n_incomplete -= 1;
345 /* If we have dropped below the max. number of incomplete
346 * connections, start accept()ing again */
347 bus_context_check_all_watches (d->connections->context);
350 _dbus_assert (d->connections->n_incomplete >= 0);
351 _dbus_assert (d->connections->n_completed >= 0);
354 bus_connection_drop_pending_replies (d->connections, connection);
356 /* frees "d" as side effect */
357 dbus_connection_set_data (connection,
358 connection_data_slot,
361 dbus_connection_unref (connection);
365 add_connection_watch (DBusWatch *watch,
368 DBusConnection *connection = data;
370 return _dbus_loop_add_watch (bus_connection_get_loop (connection), watch);
374 remove_connection_watch (DBusWatch *watch,
377 DBusConnection *connection = data;
379 _dbus_loop_remove_watch (bus_connection_get_loop (connection), watch);
383 toggle_connection_watch (DBusWatch *watch,
386 DBusConnection *connection = data;
388 _dbus_loop_toggle_watch (bus_connection_get_loop (connection), watch);
392 add_connection_timeout (DBusTimeout *timeout,
395 DBusConnection *connection = data;
397 return _dbus_loop_add_timeout (bus_connection_get_loop (connection), timeout);
401 remove_connection_timeout (DBusTimeout *timeout,
404 DBusConnection *connection = data;
406 _dbus_loop_remove_timeout (bus_connection_get_loop (connection), timeout);
410 dispatch_status_function (DBusConnection *connection,
411 DBusDispatchStatus new_status,
414 DBusLoop *loop = data;
416 if (new_status != DBUS_DISPATCH_COMPLETE)
418 while (!_dbus_loop_queue_dispatch (loop, connection))
419 _dbus_wait_for_memory ();
424 allow_unix_user_function (DBusConnection *connection,
428 BusConnectionData *d;
430 d = BUS_CONNECTION_DATA (connection);
432 _dbus_assert (d != NULL);
434 return bus_context_allow_unix_user (d->connections->context, uid);
438 free_connection_data (void *data)
440 BusConnectionData *d = data;
442 /* services_owned should be NULL since we should be disconnected */
443 _dbus_assert (d->services_owned == NULL);
444 _dbus_assert (d->n_services_owned == 0);
446 _dbus_assert (d->transaction_messages == NULL);
448 if (d->oom_preallocated)
449 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
452 dbus_message_unref (d->oom_message);
455 bus_client_policy_unref (d->policy);
458 bus_selinux_id_unref (d->selinux_id);
460 if (d->apparmor_confinement)
461 bus_apparmor_confinement_unref (d->apparmor_confinement);
463 dbus_free (d->cached_loginfo_string);
467 #ifdef DBUS_ENABLE_CYNARA
468 free (d->cynara_session_id);
475 bus_connections_new (BusContext *context)
477 BusConnections *connections;
479 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
482 connections = dbus_new0 (BusConnections, 1);
483 if (connections == NULL)
486 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
488 if (connections->completed_by_user == NULL)
491 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
492 expire_incomplete_timeout,
494 if (connections->expire_timeout == NULL)
497 _dbus_timeout_disable (connections->expire_timeout);
499 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
500 bus_context_get_reply_timeout (context),
501 bus_pending_reply_expired,
503 if (connections->pending_replies == NULL)
506 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
507 connections->expire_timeout))
510 connections->refcount = 1;
511 connections->context = context;
516 bus_expire_list_free (connections->pending_replies);
518 _dbus_timeout_unref (connections->expire_timeout);
520 _dbus_hash_table_unref (connections->completed_by_user);
522 dbus_free (connections);
524 dbus_connection_free_data_slot (&connection_data_slot);
530 bus_connections_ref (BusConnections *connections)
532 _dbus_assert (connections->refcount > 0);
533 connections->refcount += 1;
539 bus_connections_unref (BusConnections *connections)
541 _dbus_assert (connections->refcount > 0);
542 connections->refcount -= 1;
543 if (connections->refcount == 0)
545 /* drop all incomplete */
546 while (connections->incomplete != NULL)
548 DBusConnection *connection;
550 connection = connections->incomplete->data;
552 dbus_connection_ref (connection);
553 dbus_connection_close (connection);
554 bus_connection_disconnected (connection);
555 dbus_connection_unref (connection);
558 _dbus_assert (connections->n_incomplete == 0);
560 /* drop all monitors */
561 _dbus_list_clear (&connections->monitors);
563 /* drop all real connections */
564 while (connections->completed != NULL)
566 DBusConnection *connection;
568 connection = connections->completed->data;
570 dbus_connection_ref (connection);
571 dbus_connection_close (connection);
572 bus_connection_disconnected (connection);
573 dbus_connection_unref (connection);
576 _dbus_assert (connections->n_completed == 0);
578 bus_expire_list_free (connections->pending_replies);
580 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
581 connections->expire_timeout);
583 _dbus_timeout_unref (connections->expire_timeout);
585 _dbus_hash_table_unref (connections->completed_by_user);
587 if (connections->monitor_matchmaker != NULL)
588 bus_matchmaker_unref (connections->monitor_matchmaker);
590 dbus_free (connections);
592 dbus_connection_free_data_slot (&connection_data_slot);
596 /* Used for logging */
598 cache_peer_loginfo_string (BusConnectionData *d,
599 DBusConnection *connection)
601 DBusString loginfo_buf;
605 dbus_bool_t prev_added;
607 if (!_dbus_string_init (&loginfo_buf))
611 if (dbus_connection_get_unix_user (connection, &uid))
613 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
619 if (dbus_connection_get_unix_process_id (connection, &pid))
623 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
626 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
628 /* Ignore errors here; we may not have permissions to read the
630 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
631 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
635 if (dbus_connection_get_windows_user (connection, &windows_sid))
637 dbus_bool_t did_append;
638 did_append = _dbus_string_append_printf (&loginfo_buf,
639 "sid=\"%s\" ", windows_sid);
640 dbus_free (windows_sid);
645 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
648 _dbus_string_free (&loginfo_buf);
652 _dbus_string_free (&loginfo_buf);
657 check_pending_fds_cb (DBusConnection *connection)
659 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
660 int n_pending_unix_fds_old;
661 int n_pending_unix_fds_new;
663 _dbus_assert(d != NULL);
665 n_pending_unix_fds_old = d->n_pending_unix_fds;
666 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
668 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
669 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
671 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
673 _dbus_timeout_restart (d->pending_unix_fds_timeout,
674 bus_context_get_pending_fd_timeout (d->connections->context));
677 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
679 _dbus_timeout_disable (d->pending_unix_fds_timeout);
683 d->n_pending_unix_fds = n_pending_unix_fds_new;
687 pending_unix_fds_timeout_cb (void *data)
689 DBusConnection *connection = data;
690 dbus_connection_close (connection);
695 bus_connections_setup_connection (BusConnections *connections,
696 DBusConnection *connection)
699 BusConnectionData *d;
704 d = dbus_new0 (BusConnectionData, 1);
709 d->connections = connections;
710 d->connection = connection;
712 _dbus_get_monotonic_time (&d->connection_tv_sec,
713 &d->connection_tv_usec);
715 _dbus_assert (connection_data_slot >= 0);
717 if (!dbus_connection_set_data (connection,
718 connection_data_slot,
719 d, free_connection_data))
725 dbus_connection_set_route_peer_messages (connection, TRUE);
729 dbus_error_init (&error);
730 d->selinux_id = bus_selinux_init_connection_id (connection,
732 if (dbus_error_is_set (&error))
734 /* This is a bit bogus because we pretend all errors
735 * are OOM; this is done because we know that in bus.c
736 * an OOM error disconnects the connection, which is
737 * the same thing we want on any other error.
739 dbus_error_free (&error);
743 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
745 if (dbus_error_is_set (&error))
747 /* This is a bit bogus because we pretend all errors
748 * are OOM; this is done because we know that in bus.c
749 * an OOM error disconnects the connection, which is
750 * the same thing we want on any other error.
752 dbus_error_free (&error);
756 if (!dbus_connection_set_watch_functions (connection,
757 add_connection_watch,
758 remove_connection_watch,
759 toggle_connection_watch,
764 if (!dbus_connection_set_timeout_functions (connection,
765 add_connection_timeout,
766 remove_connection_timeout,
771 /* For now we don't need to set a Windows user function because
772 * there are no policies in the config file controlling what
773 * Windows users can connect. The default 'same user that owns the
774 * bus can connect' behavior of DBusConnection is fine on Windows.
776 dbus_connection_set_unix_user_function (connection,
777 allow_unix_user_function,
780 dbus_connection_set_dispatch_status_function (connection,
781 dispatch_status_function,
782 bus_context_get_loop (connections->context),
785 d->link_in_connection_list = _dbus_list_alloc_link (connection);
786 if (d->link_in_connection_list == NULL)
789 /* Setup the connection with the dispatcher */
790 if (!bus_dispatch_add_connection (connection))
793 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
795 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
797 bus_dispatch_remove_connection (connection);
802 /* Setup pending fds timeout (see #80559) */
803 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
804 pending_unix_fds_timeout_cb,
806 if (d->pending_unix_fds_timeout == NULL)
809 _dbus_timeout_disable (d->pending_unix_fds_timeout);
810 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
811 d->pending_unix_fds_timeout))
814 _dbus_connection_set_pending_fds_function (connection,
815 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
818 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
819 connections->n_incomplete += 1;
821 dbus_connection_ref (connection);
823 bus_connections_expire_incomplete (connections);
825 /* The listening socket is removed from the main loop,
826 * i.e. does not accept(), while n_incomplete is at its
827 * maximum value; so we shouldn't get here in that case */
828 _dbus_assert (connections->n_incomplete <=
829 bus_context_get_max_incomplete_connections (connections->context));
831 /* If we have the maximum number of incomplete connections,
832 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
833 bus_context_check_all_watches (d->connections->context);
841 bus_selinux_id_unref (d->selinux_id);
842 d->selinux_id = NULL;
844 if (d->apparmor_confinement)
845 bus_apparmor_confinement_unref (d->apparmor_confinement);
846 d->apparmor_confinement = NULL;
848 if (!dbus_connection_set_watch_functions (connection,
852 _dbus_assert_not_reached ("setting watch functions to NULL failed");
854 if (!dbus_connection_set_timeout_functions (connection,
858 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
860 dbus_connection_set_unix_user_function (connection,
863 dbus_connection_set_windows_user_function (connection,
866 dbus_connection_set_dispatch_status_function (connection,
869 if (d->pending_unix_fds_timeout)
870 _dbus_timeout_unref (d->pending_unix_fds_timeout);
872 d->pending_unix_fds_timeout = NULL;
874 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
876 if (d->link_in_connection_list != NULL)
878 _dbus_assert (d->link_in_connection_list->next == NULL);
879 _dbus_assert (d->link_in_connection_list->prev == NULL);
880 _dbus_list_free_link (d->link_in_connection_list);
881 d->link_in_connection_list = NULL;
884 if (!dbus_connection_set_data (connection,
885 connection_data_slot,
887 _dbus_assert_not_reached ("failed to set connection data to null");
889 /* "d" has now been freed */
896 bus_connections_expire_incomplete (BusConnections *connections)
902 if (connections->incomplete != NULL)
904 long tv_sec, tv_usec;
908 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
909 auth_timeout = bus_context_get_auth_timeout (connections->context);
911 link = _dbus_list_get_first_link (&connections->incomplete);
914 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
915 DBusConnection *connection;
916 BusConnectionData *d;
919 connection = link->data;
921 d = BUS_CONNECTION_DATA (connection);
923 _dbus_assert (d != NULL);
925 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
926 d->connection_tv_usec,
929 if (elapsed >= (double) auth_timeout)
931 /* Unfortunately, we can't identify the connection: it doesn't
932 * have a unique name yet, we don't know its uid/pid yet,
934 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
935 "Connection has not authenticated soon enough, closing it "
936 "(auth_timeout=%dms, elapsed: %.0fms)",
937 auth_timeout, elapsed);
939 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
940 dbus_connection_close (connection);
944 /* We can end the loop, since the connections are in oldest-first order */
945 next_interval = ((double)auth_timeout) - elapsed;
946 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
947 connection, next_interval);
956 bus_expire_timeout_set_interval (connections->expire_timeout,
961 expire_incomplete_timeout (void *data)
963 BusConnections *connections = data;
965 _dbus_verbose ("Running\n");
967 /* note that this may remove the timeout */
968 bus_connections_expire_incomplete (connections);
974 bus_connection_get_unix_groups (DBusConnection *connection,
975 unsigned long **groups,
984 if (dbus_connection_get_unix_user (connection, &uid))
986 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
988 _dbus_verbose ("Did not get any groups for UID %lu\n",
994 _dbus_verbose ("Got %d groups for UID %lu\n",
1000 return TRUE; /* successfully got 0 groups */
1004 bus_connection_is_in_unix_group (DBusConnection *connection,
1008 unsigned long *group_ids;
1011 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1016 while (i < n_group_ids)
1018 if (group_ids[i] == gid)
1020 dbus_free (group_ids);
1026 dbus_free (group_ids);
1031 bus_connection_get_loginfo (DBusConnection *connection)
1033 BusConnectionData *d;
1035 d = BUS_CONNECTION_DATA (connection);
1036 _dbus_assert(d != NULL);
1038 if (!bus_connection_is_active (connection))
1040 return d->cached_loginfo_string;
1044 bus_connection_get_policy (DBusConnection *connection)
1046 BusConnectionData *d;
1048 d = BUS_CONNECTION_DATA (connection);
1050 _dbus_assert (d != NULL);
1051 _dbus_assert (d->policy != NULL);
1056 #ifdef DBUS_ENABLE_CYNARA
1057 const char *bus_connection_get_cynara_session_id (DBusConnection *connection)
1059 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
1060 _dbus_assert (d != NULL);
1062 if (d->cynara_session_id == NULL)
1065 if (dbus_connection_get_unix_process_id(connection, &pid))
1066 d->cynara_session_id = cynara_session_from_pid(pid);
1068 /* If client exits as soon as async call, cynara_session_from_pid() returns null.
1069 cynara_session_from_pid checks /proc/pid to verify process */
1070 if (d->cynara_session_id == NULL)
1071 asprintf (&d->cynara_session_id, "/proc/%ld", pid);
1073 return d->cynara_session_id;
1078 foreach_active (BusConnections *connections,
1079 BusConnectionForeachFunction function,
1084 link = _dbus_list_get_first_link (&connections->completed);
1085 while (link != NULL)
1087 DBusConnection *connection = link->data;
1088 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1090 if (!(* function) (connection, data))
1100 foreach_inactive (BusConnections *connections,
1101 BusConnectionForeachFunction function,
1106 link = _dbus_list_get_first_link (&connections->incomplete);
1107 while (link != NULL)
1109 DBusConnection *connection = link->data;
1110 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1112 if (!(* function) (connection, data))
1122 * Calls function on each active connection; if the function returns
1123 * #FALSE, stops iterating. Active connections are authenticated
1124 * and have sent a Hello message.
1126 * @param connections the connections object
1127 * @param function the function
1128 * @param data data to pass to it as a second arg
1131 bus_connections_foreach_active (BusConnections *connections,
1132 BusConnectionForeachFunction function,
1135 foreach_active (connections, function, data);
1139 * Calls function on each connection; if the function returns
1140 * #FALSE, stops iterating.
1142 * @param connections the connections object
1143 * @param function the function
1144 * @param data data to pass to it as a second arg
1147 bus_connections_foreach (BusConnections *connections,
1148 BusConnectionForeachFunction function,
1151 if (!foreach_active (connections, function, data))
1154 foreach_inactive (connections, function, data);
1158 bus_connections_get_context (BusConnections *connections)
1160 return connections->context;
1164 * This is used to avoid covering the same connection twice when
1165 * traversing connections. Note that it assumes we will
1166 * bus_connection_mark_stamp() each connection at least once per
1167 * INT_MAX increments of the global stamp, or wraparound would break
1171 bus_connections_increment_stamp (BusConnections *connections)
1173 connections->stamp += 1;
1176 /* Mark connection with current stamp, return TRUE if it
1177 * didn't already have that stamp
1180 bus_connection_mark_stamp (DBusConnection *connection)
1182 BusConnectionData *d;
1184 d = BUS_CONNECTION_DATA (connection);
1186 _dbus_assert (d != NULL);
1188 if (d->stamp == d->connections->stamp)
1192 d->stamp = d->connections->stamp;
1198 bus_connection_get_context (DBusConnection *connection)
1200 BusConnectionData *d;
1202 d = BUS_CONNECTION_DATA (connection);
1204 _dbus_assert (d != NULL);
1206 return d->connections->context;
1210 bus_connection_get_connections (DBusConnection *connection)
1212 BusConnectionData *d;
1214 d = BUS_CONNECTION_DATA (connection);
1216 _dbus_assert (d != NULL);
1218 return d->connections;
1222 bus_connection_get_registry (DBusConnection *connection)
1224 BusConnectionData *d;
1226 d = BUS_CONNECTION_DATA (connection);
1228 _dbus_assert (d != NULL);
1230 return bus_context_get_registry (d->connections->context);
1234 bus_connection_get_activation (DBusConnection *connection)
1236 BusConnectionData *d;
1238 d = BUS_CONNECTION_DATA (connection);
1240 _dbus_assert (d != NULL);
1242 return bus_context_get_activation (d->connections->context);
1246 bus_connection_get_matchmaker (DBusConnection *connection)
1248 BusConnectionData *d;
1250 d = BUS_CONNECTION_DATA (connection);
1252 _dbus_assert (d != NULL);
1254 return bus_context_get_matchmaker (d->connections->context);
1258 bus_connection_get_selinux_id (DBusConnection *connection)
1260 BusConnectionData *d;
1262 d = BUS_CONNECTION_DATA (connection);
1264 _dbus_assert (d != NULL);
1266 return d->selinux_id;
1269 BusAppArmorConfinement*
1270 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1272 BusConnectionData *d;
1274 d = BUS_CONNECTION_DATA (connection);
1276 _dbus_assert (d != NULL);
1278 bus_apparmor_confinement_ref (d->apparmor_confinement);
1279 return d->apparmor_confinement;
1283 * Checks whether the connection is registered with the message bus.
1285 * @param connection the connection
1286 * @returns #TRUE if we're an active message bus participant
1289 bus_connection_is_active (DBusConnection *connection)
1291 BusConnectionData *d;
1293 d = BUS_CONNECTION_DATA (connection);
1294 _dbus_assert(d != NULL);
1296 return d->name != NULL;
1300 bus_connection_preallocate_oom_error (DBusConnection *connection)
1302 DBusMessage *message;
1303 DBusPreallocatedSend *preallocated;
1304 BusConnectionData *d;
1306 d = BUS_CONNECTION_DATA (connection);
1308 _dbus_assert (d != NULL);
1310 if (d->oom_preallocated != NULL)
1313 preallocated = dbus_connection_preallocate_send (connection);
1314 if (preallocated == NULL)
1317 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1319 if (message == NULL)
1321 dbus_connection_free_preallocated_send (connection, preallocated);
1325 /* d->name may be NULL, but that is OK */
1326 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1327 !dbus_message_set_destination (message, d->name) ||
1328 !dbus_message_set_sender (message,
1331 dbus_connection_free_preallocated_send (connection, preallocated);
1332 dbus_message_unref (message);
1336 /* set reply serial to placeholder value just so space is already allocated
1339 if (!dbus_message_set_reply_serial (message, 14))
1341 dbus_connection_free_preallocated_send (connection, preallocated);
1342 dbus_message_unref (message);
1346 d->oom_message = message;
1347 d->oom_preallocated = preallocated;
1353 bus_connection_send_oom_error (DBusConnection *connection,
1354 DBusMessage *in_reply_to)
1356 BusConnectionData *d;
1358 d = BUS_CONNECTION_DATA (connection);
1360 _dbus_assert (d != NULL);
1361 _dbus_assert (d->oom_message != NULL);
1363 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1364 "dbus-daemon transaction failed (OOM), sending error to "
1365 "sender %s", bus_connection_get_loginfo (connection));
1367 /* should always succeed since we set it to a placeholder earlier */
1368 if (!dbus_message_set_reply_serial (d->oom_message,
1369 dbus_message_get_serial (in_reply_to)))
1370 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1372 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1374 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1375 d->oom_message, NULL);
1377 dbus_message_unref (d->oom_message);
1378 d->oom_message = NULL;
1379 d->oom_preallocated = NULL;
1382 #ifdef DBUS_ENABLE_STATS
1384 update_peak (int *peak,
1393 bus_connection_add_match_rule_link (DBusConnection *connection,
1396 BusConnectionData *d;
1398 d = BUS_CONNECTION_DATA (connection);
1399 _dbus_assert (d != NULL);
1401 _dbus_list_append_link (&d->match_rules, link);
1403 d->n_match_rules += 1;
1405 #ifdef DBUS_ENABLE_STATS
1406 update_peak (&d->peak_match_rules, d->n_match_rules);
1407 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1409 d->connections->total_match_rules += 1;
1410 update_peak (&d->connections->peak_match_rules,
1411 d->connections->total_match_rules);
1416 bus_connection_add_match_rule (DBusConnection *connection,
1421 link = _dbus_list_alloc_link (rule);
1426 bus_connection_add_match_rule_link (connection, link);
1432 bus_connection_remove_match_rule (DBusConnection *connection,
1435 BusConnectionData *d;
1437 d = BUS_CONNECTION_DATA (connection);
1438 _dbus_assert (d != NULL);
1440 _dbus_list_remove_last (&d->match_rules, rule);
1442 d->n_match_rules -= 1;
1443 _dbus_assert (d->n_match_rules >= 0);
1445 #ifdef DBUS_ENABLE_STATS
1446 d->connections->total_match_rules -= 1;
1451 bus_connection_get_n_match_rules (DBusConnection *connection)
1453 BusConnectionData *d;
1455 d = BUS_CONNECTION_DATA (connection);
1456 _dbus_assert (d != NULL);
1458 return d->n_match_rules;
1462 bus_connection_is_service_owner_by_prefix (DBusConnection *connection,
1463 const char *name_prefix)
1465 BusConnectionData *d;
1468 d = BUS_CONNECTION_DATA (connection);
1469 _dbus_assert (d != NULL);
1471 link = _dbus_list_get_first_link (&d->services_owned);
1472 while (link != NULL)
1474 BusService *service = link->data;
1477 _dbus_string_init_const (&str, bus_service_get_name (service));
1479 if (_dbus_string_starts_with_words_c_str (&str, name_prefix, '.'))
1482 link = _dbus_list_get_next_link (&d->services_owned, link);
1489 bus_connection_add_owned_service_link (DBusConnection *connection,
1492 BusConnectionData *d;
1494 d = BUS_CONNECTION_DATA (connection);
1495 _dbus_assert (d != NULL);
1497 _dbus_list_append_link (&d->services_owned, link);
1499 d->n_services_owned += 1;
1501 #ifdef DBUS_ENABLE_STATS
1502 update_peak (&d->peak_bus_names, d->n_services_owned);
1503 update_peak (&d->connections->peak_bus_names_per_conn,
1504 d->n_services_owned);
1506 d->connections->total_bus_names += 1;
1507 update_peak (&d->connections->peak_bus_names,
1508 d->connections->total_bus_names);
1513 bus_connection_add_owned_service (DBusConnection *connection,
1514 BusService *service)
1518 link = _dbus_list_alloc_link (service);
1523 bus_connection_add_owned_service_link (connection, link);
1529 bus_connection_remove_owned_service (DBusConnection *connection,
1530 BusService *service)
1532 BusConnectionData *d;
1534 d = BUS_CONNECTION_DATA (connection);
1535 _dbus_assert (d != NULL);
1537 _dbus_list_remove_last (&d->services_owned, service);
1539 d->n_services_owned -= 1;
1540 _dbus_assert (d->n_services_owned >= 0);
1542 #ifdef DBUS_ENABLE_STATS
1543 d->connections->total_bus_names -= 1;
1548 bus_connection_get_n_services_owned (DBusConnection *connection)
1550 BusConnectionData *d;
1552 d = BUS_CONNECTION_DATA (connection);
1553 _dbus_assert (d != NULL);
1555 return d->n_services_owned;
1559 bus_connection_complete (DBusConnection *connection,
1560 const DBusString *name,
1563 BusConnectionData *d;
1566 d = BUS_CONNECTION_DATA (connection);
1567 _dbus_assert (d != NULL);
1568 _dbus_assert (d->name == NULL);
1569 _dbus_assert (d->policy == NULL);
1571 _dbus_assert (!bus_connection_is_active (connection));
1573 if (!_dbus_string_copy_data (name, &d->name))
1575 BUS_SET_OOM (error);
1579 _dbus_assert (d->name != NULL);
1581 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1583 d->policy = bus_context_create_client_policy (d->connections->context,
1587 /* we may have a NULL policy on OOM or error getting list of
1588 * groups for a user. In the latter case we don't handle it so
1589 * well currently, as it will just keep failing over and over.
1592 if (d->policy == NULL)
1594 _dbus_verbose ("Failed to create security policy for connection %p\n",
1596 _DBUS_ASSERT_ERROR_IS_SET (error);
1597 dbus_free (d->name);
1602 if (dbus_connection_get_unix_user (connection, &uid))
1604 if (!adjust_connections_for_uid (d->connections,
1609 /* Create and cache a string which holds information about the
1610 * peer process; used for logging purposes.
1612 if (!cache_peer_loginfo_string (d, connection))
1615 /* Now the connection is active, move it between lists */
1616 _dbus_list_unlink (&d->connections->incomplete,
1617 d->link_in_connection_list);
1618 d->connections->n_incomplete -= 1;
1619 _dbus_list_append_link (&d->connections->completed,
1620 d->link_in_connection_list);
1621 d->connections->n_completed += 1;
1623 _dbus_assert (d->connections->n_incomplete >= 0);
1624 _dbus_assert (d->connections->n_completed > 0);
1626 /* If we have dropped below the max. number of incomplete
1627 * connections, start accept()ing again */
1628 bus_context_check_all_watches (d->connections->context);
1630 /* See if we can remove the timeout */
1631 bus_connections_expire_incomplete (d->connections);
1633 _dbus_assert (bus_connection_is_active (connection));
1637 BUS_SET_OOM (error);
1638 dbus_free (d->name);
1641 bus_client_policy_unref (d->policy);
1647 bus_connections_reload_policy (BusConnections *connections,
1650 BusConnectionData *d;
1651 DBusConnection *connection;
1654 _dbus_assert (connections != NULL);
1655 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1657 for (link = _dbus_list_get_first_link (&(connections->completed));
1659 link = _dbus_list_get_next_link (&(connections->completed), link))
1661 connection = link->data;
1662 d = BUS_CONNECTION_DATA (connection);
1663 _dbus_assert (d != NULL);
1664 _dbus_assert (d->policy != NULL);
1666 bus_client_policy_unref (d->policy);
1667 d->policy = bus_context_create_client_policy (connections->context,
1670 if (d->policy == NULL)
1672 _dbus_verbose ("Failed to create security policy for connection %p\n",
1674 _DBUS_ASSERT_ERROR_IS_SET (error);
1683 bus_connection_get_name (DBusConnection *connection)
1685 BusConnectionData *d;
1687 d = BUS_CONNECTION_DATA (connection);
1688 _dbus_assert (d != NULL);
1694 * Check whether completing the passed-in connection would
1695 * exceed limits, and if so set error and return #FALSE
1698 bus_connections_check_limits (BusConnections *connections,
1699 DBusConnection *requesting_completion,
1704 if (connections->n_completed >=
1705 bus_context_get_max_completed_connections (connections->context))
1707 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1708 "The maximum number of active connections has been reached");
1712 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1714 if (get_connections_for_uid (connections, uid) >=
1715 bus_context_get_max_connections_per_user (connections->context))
1717 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1718 "The maximum number of active connections for UID %lu has been reached",
1728 bus_pending_reply_free (BusPendingReply *pending)
1730 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1732 pending->will_send_reply,
1733 pending->will_get_reply,
1734 pending->reply_serial);
1736 dbus_free (pending);
1740 bus_pending_reply_send_no_reply (BusConnections *connections,
1741 BusTransaction *transaction,
1742 BusPendingReply *pending)
1744 DBusMessage *message;
1745 DBusMessageIter iter;
1751 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1752 if (message == NULL)
1755 dbus_message_set_no_reply (message, TRUE);
1757 if (!dbus_message_set_reply_serial (message,
1758 pending->reply_serial))
1761 if (!dbus_message_set_error_name (message,
1762 DBUS_ERROR_NO_REPLY))
1765 /* If you change these messages, adjust test/dbus-daemon.c to match */
1766 if (pending->will_send_reply == NULL)
1767 errmsg = "Message recipient disconnected from message bus without replying";
1769 errmsg = "Message did not receive a reply (timeout by message bus)";
1771 dbus_message_iter_init_append (message, &iter);
1772 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1775 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1782 dbus_message_unref (message);
1787 bus_pending_reply_expired (BusExpireList *list,
1791 BusPendingReply *pending = link->data;
1792 BusConnections *connections = data;
1793 BusTransaction *transaction;
1795 /* No reply is forthcoming. So nuke it if we can. If not,
1796 * leave it in the list to try expiring again later when we
1800 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1802 pending->will_send_reply,
1803 pending->will_get_reply,
1804 pending->reply_serial);
1806 transaction = bus_transaction_new (connections->context);
1807 if (transaction == NULL)
1810 if (!bus_pending_reply_send_no_reply (connections,
1814 bus_transaction_cancel_and_free (transaction);
1818 bus_expire_list_remove_link (connections->pending_replies, link);
1820 bus_pending_reply_free (pending);
1821 bus_transaction_execute_and_free (transaction);
1827 bus_connection_drop_pending_replies (BusConnections *connections,
1828 DBusConnection *connection)
1830 /* The DBusConnection is almost 100% finalized here, so you can't
1831 * do anything with it except check for pointer equality
1835 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1838 link = bus_expire_list_get_first_link (connections->pending_replies);
1839 while (link != NULL)
1842 BusPendingReply *pending;
1844 next = bus_expire_list_get_next_link (connections->pending_replies,
1846 pending = link->data;
1848 if (pending->will_get_reply == connection)
1850 /* We don't need to track this pending reply anymore */
1852 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1854 pending->will_send_reply,
1855 pending->will_get_reply,
1856 pending->reply_serial);
1858 bus_expire_list_remove_link (connections->pending_replies,
1860 bus_pending_reply_free (pending);
1862 else if (pending->will_send_reply == connection)
1864 /* The reply isn't going to be sent, so set things
1865 * up so it will be expired right away
1867 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1869 pending->will_send_reply,
1870 pending->will_get_reply,
1871 pending->reply_serial);
1873 pending->will_send_reply = NULL;
1874 pending->expire_item.added_tv_sec = 0;
1875 pending->expire_item.added_tv_usec = 0;
1877 bus_expire_list_recheck_immediately (connections->pending_replies);
1887 BusPendingReply *pending;
1888 BusConnections *connections;
1889 } CancelPendingReplyData;
1892 cancel_pending_reply (void *data)
1894 CancelPendingReplyData *d = data;
1896 _dbus_verbose ("d = %p\n", d);
1898 if (!bus_expire_list_remove (d->connections->pending_replies,
1899 &d->pending->expire_item))
1900 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1902 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1906 cancel_pending_reply_data_free (void *data)
1908 CancelPendingReplyData *d = data;
1910 _dbus_verbose ("d = %p\n", d);
1912 /* d->pending should be either freed or still
1913 * in the list of pending replies (owned by someone
1921 * Record that a reply is allowed; return TRUE on success.
1924 bus_connections_expect_reply (BusConnections *connections,
1925 BusTransaction *transaction,
1926 DBusConnection *will_get_reply,
1927 DBusConnection *will_send_reply,
1928 DBusMessage *reply_to_this,
1931 BusPendingReply *pending;
1932 dbus_uint32_t reply_serial;
1934 CancelPendingReplyData *cprd;
1937 _dbus_assert (will_get_reply != NULL);
1938 _dbus_assert (will_send_reply != NULL);
1939 _dbus_assert (reply_to_this != NULL);
1941 if (dbus_message_get_no_reply (reply_to_this))
1942 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1944 reply_serial = dbus_message_get_serial (reply_to_this);
1946 link = bus_expire_list_get_first_link (connections->pending_replies);
1948 while (link != NULL)
1950 pending = link->data;
1952 if (pending->reply_serial == reply_serial &&
1953 pending->will_get_reply == will_get_reply &&
1954 pending->will_send_reply == will_send_reply)
1956 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1957 "Message has the same reply serial as a currently-outstanding existing method call");
1961 link = bus_expire_list_get_next_link (connections->pending_replies,
1963 if (pending->will_get_reply == will_get_reply)
1968 bus_context_get_max_replies_per_connection (connections->context))
1970 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1971 "The maximum number of pending replies per connection has been reached");
1975 pending = dbus_new0 (BusPendingReply, 1);
1976 if (pending == NULL)
1978 BUS_SET_OOM (error);
1982 #ifdef DBUS_ENABLE_VERBOSE_MODE
1983 /* so we can see a not-yet-added pending reply */
1984 pending->expire_item.added_tv_sec = 1;
1985 pending->expire_item.added_tv_usec = 1;
1988 pending->will_get_reply = will_get_reply;
1989 pending->will_send_reply = will_send_reply;
1990 pending->reply_serial = reply_serial;
1992 cprd = dbus_new0 (CancelPendingReplyData, 1);
1995 BUS_SET_OOM (error);
1996 bus_pending_reply_free (pending);
2000 if (!bus_expire_list_add (connections->pending_replies,
2001 &pending->expire_item))
2003 BUS_SET_OOM (error);
2005 bus_pending_reply_free (pending);
2009 if (!bus_transaction_add_cancel_hook (transaction,
2010 cancel_pending_reply,
2012 cancel_pending_reply_data_free))
2014 BUS_SET_OOM (error);
2015 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
2017 bus_pending_reply_free (pending);
2021 cprd->pending = pending;
2022 cprd->connections = connections;
2024 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
2025 &pending->expire_item.added_tv_usec);
2027 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2029 pending->will_send_reply,
2030 pending->will_get_reply,
2031 pending->reply_serial);
2039 BusConnections *connections;
2040 } CheckPendingReplyData;
2043 cancel_check_pending_reply (void *data)
2045 CheckPendingReplyData *d = data;
2047 _dbus_verbose ("d = %p\n",d);
2049 bus_expire_list_add_link (d->connections->pending_replies,
2055 check_pending_reply_data_free (void *data)
2057 CheckPendingReplyData *d = data;
2059 _dbus_verbose ("d = %p\n",d);
2061 if (d->link != NULL)
2063 BusPendingReply *pending = d->link->data;
2065 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2066 &pending->expire_item));
2068 bus_pending_reply_free (pending);
2069 _dbus_list_free_link (d->link);
2076 * Check whether a reply is allowed, remove BusPendingReply
2077 * if so, return TRUE if so.
2080 bus_connections_check_reply (BusConnections *connections,
2081 BusTransaction *transaction,
2082 DBusConnection *sending_reply,
2083 DBusConnection *receiving_reply,
2087 CheckPendingReplyData *cprd;
2089 dbus_uint32_t reply_serial;
2091 _dbus_assert (sending_reply != NULL);
2092 _dbus_assert (receiving_reply != NULL);
2094 reply_serial = dbus_message_get_reply_serial (reply);
2096 link = bus_expire_list_get_first_link (connections->pending_replies);
2097 while (link != NULL)
2099 BusPendingReply *pending = link->data;
2101 if (pending->reply_serial == reply_serial &&
2102 pending->will_get_reply == receiving_reply &&
2103 pending->will_send_reply == sending_reply)
2105 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2109 link = bus_expire_list_get_next_link (connections->pending_replies,
2115 _dbus_verbose ("No pending reply expected\n");
2120 cprd = dbus_new0 (CheckPendingReplyData, 1);
2123 BUS_SET_OOM (error);
2127 if (!bus_transaction_add_cancel_hook (transaction,
2128 cancel_check_pending_reply,
2130 check_pending_reply_data_free))
2132 BUS_SET_OOM (error);
2138 cprd->connections = connections;
2140 bus_expire_list_unlink (connections->pending_replies,
2143 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2151 * Note that this is fairly fragile; in particular, don't try to use
2152 * one transaction across any main loop iterations.
2157 BusTransaction *transaction;
2158 DBusMessage *message;
2159 DBusPreallocatedSend *preallocated;
2164 BusTransactionCancelFunction cancel_function;
2165 DBusFreeFunction free_data_function;
2169 struct BusTransaction
2171 DBusList *connections;
2172 BusContext *context;
2173 DBusList *cancel_hooks;
2177 message_to_send_free (DBusConnection *connection,
2178 MessageToSend *to_send)
2180 if (to_send->message)
2181 dbus_message_unref (to_send->message);
2183 if (to_send->preallocated)
2184 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2186 dbus_free (to_send);
2190 cancel_hook_cancel (void *element,
2193 CancelHook *ch = element;
2195 _dbus_verbose ("Running transaction cancel hook\n");
2197 if (ch->cancel_function)
2198 (* ch->cancel_function) (ch->data);
2202 cancel_hook_free (void *element,
2205 CancelHook *ch = element;
2207 if (ch->free_data_function)
2208 (* ch->free_data_function) (ch->data);
2214 free_cancel_hooks (BusTransaction *transaction)
2216 _dbus_list_foreach (&transaction->cancel_hooks,
2217 cancel_hook_free, NULL);
2219 _dbus_list_clear (&transaction->cancel_hooks);
2223 bus_transaction_new (BusContext *context)
2225 BusTransaction *transaction;
2227 transaction = dbus_new0 (BusTransaction, 1);
2228 if (transaction == NULL)
2231 transaction->context = context;
2237 bus_transaction_get_context (BusTransaction *transaction)
2239 return transaction->context;
2243 * Reserve enough memory to capture the given message if the
2244 * transaction goes through.
2247 bus_transaction_capture (BusTransaction *transaction,
2248 DBusConnection *sender,
2249 DBusMessage *message)
2251 BusConnections *connections;
2254 DBusList *recipients = NULL;
2255 dbus_bool_t ret = FALSE;
2257 connections = bus_context_get_connections (transaction->context);
2259 /* shortcut: don't compose the message unless someone wants it */
2260 if (connections->monitors == NULL)
2263 mm = connections->monitor_matchmaker;
2264 /* This is non-null if there has ever been a monitor - we don't GC it.
2265 * There's little point, since there is up to 1 per process. */
2266 _dbus_assert (mm != NULL);
2268 if (!bus_matchmaker_get_recipients (mm, connections, sender, NULL, message,
2272 for (link = _dbus_list_get_first_link (&recipients);
2274 link = _dbus_list_get_next_link (&recipients, link))
2276 DBusConnection *recipient = link->data;
2278 if (!bus_transaction_send (transaction, recipient, message, FALSE))
2285 _dbus_list_clear (&recipients);
2290 bus_transaction_capture_error_reply (BusTransaction *transaction,
2291 const DBusError *error,
2292 DBusMessage *in_reply_to)
2294 BusConnections *connections;
2296 dbus_bool_t ret = FALSE;
2298 _dbus_assert (error != NULL);
2299 _DBUS_ASSERT_ERROR_IS_SET (error);
2301 connections = bus_context_get_connections (transaction->context);
2303 /* shortcut: don't compose the message unless someone wants it */
2304 if (connections->monitors == NULL)
2307 reply = dbus_message_new_error (in_reply_to,
2314 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2317 ret = bus_transaction_capture (transaction, NULL, reply);
2320 dbus_message_unref (reply);
2325 bus_transaction_send_from_driver (BusTransaction *transaction,
2326 DBusConnection *connection,
2327 DBusMessage *message)
2329 DBusError error = DBUS_ERROR_INIT;
2330 BusDeferredMessage *deferred_message;
2332 /* We have to set the sender to the driver, and have
2333 * to check security policy since it was not done in
2336 _dbus_verbose ("Sending %s %s %s from driver\n",
2337 dbus_message_get_interface (message) ?
2338 dbus_message_get_interface (message) : "(no interface)",
2339 dbus_message_get_member (message) ?
2340 dbus_message_get_member (message) : "(no member)",
2341 dbus_message_get_error_name (message) ?
2342 dbus_message_get_error_name (message) : "(no error name)");
2344 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2347 if (bus_connection_is_active (connection))
2349 if (!dbus_message_set_destination (message,
2350 bus_connection_get_name (connection)))
2354 /* bus driver never wants a reply */
2355 dbus_message_set_no_reply (message, TRUE);
2357 /* Capture it for monitors, even if the real recipient's receive policy
2358 * does not allow it to receive this message from us (which would be odd).
2360 if (!bus_transaction_capture (transaction, NULL, message))
2363 /* If security policy doesn't allow the message, we would silently
2364 * eat it; the driver doesn't care about getting a reply. However,
2365 * if we're actively capturing messages, it's nice to log that we
2366 * tried to send it and did not allow ourselves to do so.
2368 switch (bus_context_check_security_policy (bus_transaction_get_context (transaction),
2370 NULL, connection, connection,
2371 message, NULL, &error,
2374 case BUS_RESULT_TRUE:
2376 case BUS_RESULT_FALSE:
2377 if (!bus_transaction_capture_error_reply (transaction, &error, message))
2379 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2380 "message from dbus-daemon rejected but not enough "
2381 "memory to capture it");
2384 /* This is not fatal to the transaction so silently eat the disallowed
2385 * message (see reasoning above) */
2386 dbus_error_free (&error);
2389 case BUS_RESULT_LATER:
2390 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
2392 return TRUE; /* pretend to have sent it */
2395 return bus_transaction_send (transaction, connection, message, FALSE);
2399 bus_transaction_send (BusTransaction *transaction,
2400 DBusConnection *connection,
2401 DBusMessage *message,
2402 dbus_bool_t deferred_dispatch)
2404 MessageToSend *to_send;
2405 BusConnectionData *d;
2408 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2409 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2410 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2412 dbus_message_get_interface (message) ?
2413 dbus_message_get_interface (message) : "(unset)",
2414 dbus_message_get_member (message) ?
2415 dbus_message_get_member (message) : "(unset)",
2416 dbus_message_get_error_name (message) ?
2417 dbus_message_get_error_name (message) : "(unset)",
2418 dbus_connection_get_is_connected (connection) ?
2419 "" : " (disconnected)");
2421 _dbus_assert (dbus_message_get_sender (message) != NULL);
2423 if (!dbus_connection_get_is_connected (connection))
2424 return TRUE; /* silently ignore disconnected connections */
2426 d = BUS_CONNECTION_DATA (connection);
2427 _dbus_assert (d != NULL);
2429 if (!deferred_dispatch && d->deferred_messages != NULL)
2431 BusDeferredMessage *deferred_message;
2432 dbus_bool_t success;
2433 /* sender and addressed recipient are not required at this point as we only need to send message
2434 * to a single recipient without performing policy check. */
2435 deferred_message = bus_deferred_message_new (message,
2440 if (deferred_message == NULL)
2443 success = bus_deferred_message_queue_at_recipient(deferred_message, transaction,
2445 bus_deferred_message_unref(deferred_message);
2450 to_send = dbus_new (MessageToSend, 1);
2451 if (to_send == NULL)
2456 to_send->preallocated = dbus_connection_preallocate_send (connection);
2457 if (to_send->preallocated == NULL)
2459 dbus_free (to_send);
2463 dbus_message_ref (message);
2464 to_send->message = message;
2465 to_send->transaction = transaction;
2467 _dbus_verbose ("about to prepend message\n");
2469 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2471 message_to_send_free (connection, to_send);
2475 _dbus_verbose ("prepended message\n");
2477 /* See if we already had this connection in the list
2478 * for this transaction. If we have a pending message,
2479 * then we should already be in transaction->connections
2481 link = _dbus_list_get_first_link (&d->transaction_messages);
2482 _dbus_assert (link->data == to_send);
2483 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2484 while (link != NULL)
2486 MessageToSend *m = link->data;
2487 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2489 if (m->transaction == transaction)
2497 if (!_dbus_list_prepend (&transaction->connections, connection))
2499 _dbus_list_remove (&d->transaction_messages, to_send);
2500 message_to_send_free (connection, to_send);
2509 transaction_free (BusTransaction *transaction)
2511 _dbus_assert (transaction->connections == NULL);
2513 free_cancel_hooks (transaction);
2515 dbus_free (transaction);
2519 connection_cancel_transaction (DBusConnection *connection,
2520 BusTransaction *transaction)
2523 BusConnectionData *d;
2525 d = BUS_CONNECTION_DATA (connection);
2526 _dbus_assert (d != NULL);
2528 link = _dbus_list_get_first_link (&d->transaction_messages);
2529 while (link != NULL)
2531 MessageToSend *m = link->data;
2532 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2534 if (m->transaction == transaction)
2536 _dbus_list_remove_link (&d->transaction_messages,
2539 message_to_send_free (connection, m);
2547 bus_transaction_cancel_and_free (BusTransaction *transaction)
2549 DBusConnection *connection;
2551 _dbus_verbose ("TRANSACTION: cancelled\n");
2553 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2554 connection_cancel_transaction (connection, transaction);
2556 _dbus_list_foreach (&transaction->cancel_hooks,
2557 cancel_hook_cancel, NULL);
2559 transaction_free (transaction);
2563 connection_execute_transaction (DBusConnection *connection,
2564 BusTransaction *transaction)
2567 BusConnectionData *d;
2569 d = BUS_CONNECTION_DATA (connection);
2570 _dbus_assert (d != NULL);
2572 /* Send the queue in order (FIFO) */
2573 link = _dbus_list_get_last_link (&d->transaction_messages);
2574 while (link != NULL)
2576 MessageToSend *m = link->data;
2577 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2579 if (m->transaction == transaction)
2581 _dbus_list_remove_link (&d->transaction_messages,
2584 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2586 dbus_connection_send_preallocated (connection,
2591 m->preallocated = NULL; /* so we don't double-free it */
2593 message_to_send_free (connection, m);
2601 bus_transaction_execute_and_free (BusTransaction *transaction)
2603 /* For each connection in transaction->connections
2606 DBusConnection *connection;
2608 _dbus_verbose ("TRANSACTION: executing\n");
2610 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2611 connection_execute_transaction (connection, transaction);
2613 transaction_free (transaction);
2617 bus_connection_remove_transactions (DBusConnection *connection)
2619 MessageToSend *to_send;
2620 BusConnectionData *d;
2622 d = BUS_CONNECTION_DATA (connection);
2623 _dbus_assert (d != NULL);
2625 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2627 /* only has an effect for the first MessageToSend listing this transaction */
2628 _dbus_list_remove (&to_send->transaction->connections,
2631 _dbus_list_remove (&d->transaction_messages, to_send);
2632 message_to_send_free (connection, to_send);
2637 * Converts the DBusError to a message reply
2640 bus_transaction_send_error_reply (BusTransaction *transaction,
2641 DBusConnection *connection,
2642 const DBusError *error,
2643 DBusMessage *in_reply_to)
2647 _dbus_assert (error != NULL);
2648 _DBUS_ASSERT_ERROR_IS_SET (error);
2650 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2651 error->name, error->message);
2653 reply = dbus_message_new_error (in_reply_to,
2659 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2661 dbus_message_unref (reply);
2665 dbus_message_unref (reply);
2671 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2672 BusTransactionCancelFunction cancel_function,
2674 DBusFreeFunction free_data_function)
2678 ch = dbus_new (CancelHook, 1);
2682 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2683 cancel_function, data);
2685 ch->cancel_function = cancel_function;
2687 ch->free_data_function = free_data_function;
2689 /* It's important that the hooks get run in reverse order that they
2692 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2702 bus_connection_dispatch_deferred (DBusConnection *connection)
2704 BusDeferredMessage *message;
2706 _dbus_return_if_fail (connection != NULL);
2708 while ((message = bus_connection_pop_deferred_message(connection)) != NULL)
2710 bus_deferred_message_dispatch(message);
2711 bus_deferred_message_unref(message);
2716 bus_connection_has_deferred_messages (DBusConnection *connection)
2718 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2719 return d->deferred_messages != NULL ? TRUE : FALSE;
2723 bus_connection_queue_deferred_message (DBusConnection *connection,
2724 BusDeferredMessage *message,
2725 dbus_bool_t prepend)
2727 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2728 dbus_bool_t success;
2730 success = _dbus_list_prepend(&d->deferred_messages, message);
2732 success = _dbus_list_append(&d->deferred_messages, message);
2736 bus_deferred_message_ref(message);
2744 bus_connection_replace_deferred_message (DBusConnection *connection,
2745 BusDeferredMessage *oldMessage,
2746 BusDeferredMessage *newMessage)
2749 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2751 link = _dbus_list_find_first(&d->deferred_messages, oldMessage);
2755 if (!_dbus_list_insert_after(&d->deferred_messages, link, newMessage))
2758 bus_deferred_message_ref(newMessage);
2759 _dbus_list_remove_link(&d->deferred_messages, link);
2760 bus_deferred_message_unref(oldMessage);
2764 BusDeferredMessage *
2765 bus_connection_pop_deferred_message (DBusConnection *connection)
2768 BusDeferredMessage *message;
2769 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2771 link =_dbus_list_get_first_link(&d->deferred_messages);
2774 message = link->data;
2775 if (!bus_deferred_message_waits_for_check(message))
2777 _dbus_list_remove_link(&d->deferred_messages, link);
2786 bus_connection_putback_deferred_message (DBusConnection *connection, BusDeferredMessage *message)
2788 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2789 if (_dbus_list_prepend(&d->deferred_messages, message))
2797 bus_connection_clear_deferred_messages (DBusConnection *connection)
2799 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2802 BusDeferredMessage *message;
2804 link =_dbus_list_get_first_link(&d->deferred_messages);
2805 while (link != NULL)
2807 next = _dbus_list_get_next_link (&d->deferred_messages, link);
2808 message = link->data;
2810 bus_deferred_message_unref(message);
2811 _dbus_list_remove_link(&d->deferred_messages, link);
2818 bus_connection_remove_deferred_message (DBusConnection *connection,
2819 BusDeferredMessage *message)
2821 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2822 if (_dbus_list_remove(&d->deferred_messages, message))
2823 bus_deferred_message_unref(message);
2827 bus_connections_get_n_active (BusConnections *connections)
2829 return connections->n_completed;
2833 bus_connections_get_n_incomplete (BusConnections *connections)
2835 return connections->n_incomplete;
2838 #ifdef DBUS_ENABLE_STATS
2840 bus_connections_get_total_match_rules (BusConnections *connections)
2842 return connections->total_match_rules;
2846 bus_connections_get_peak_match_rules (BusConnections *connections)
2848 return connections->peak_match_rules;
2852 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2854 return connections->peak_match_rules_per_conn;
2858 bus_connections_get_total_bus_names (BusConnections *connections)
2860 return connections->total_bus_names;
2864 bus_connections_get_peak_bus_names (BusConnections *connections)
2866 return connections->peak_bus_names;
2870 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2872 return connections->peak_bus_names_per_conn;
2876 bus_connection_get_peak_match_rules (DBusConnection *connection)
2878 BusConnectionData *d;
2880 d = BUS_CONNECTION_DATA (connection);
2881 _dbus_assert(d != NULL);
2883 return d->peak_match_rules;
2887 bus_connection_get_peak_bus_names (DBusConnection *connection)
2889 BusConnectionData *d;
2891 d = BUS_CONNECTION_DATA (connection);
2892 _dbus_assert(d != NULL);
2894 return d->peak_bus_names;
2896 #endif /* DBUS_ENABLE_STATS */
2899 bus_connection_is_monitor (DBusConnection *connection)
2901 BusConnectionData *d;
2903 d = BUS_CONNECTION_DATA (connection);
2904 _dbus_assert(d != NULL);
2906 return d->link_in_monitors != NULL;
2910 bcd_add_monitor_rules (BusConnectionData *d,
2911 DBusConnection *connection,
2914 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2919 mm = bus_matchmaker_new ();
2924 d->connections->monitor_matchmaker = mm;
2927 for (iter = _dbus_list_get_first_link (rules);
2929 iter = _dbus_list_get_next_link (rules, iter))
2931 if (!bus_matchmaker_add_rule (mm, iter->data))
2933 bus_matchmaker_disconnected (mm, connection);
2942 bcd_drop_monitor_rules (BusConnectionData *d,
2943 DBusConnection *connection)
2945 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2948 bus_matchmaker_disconnected (mm, connection);
2952 bus_connection_be_monitor (DBusConnection *connection,
2953 BusTransaction *transaction,
2957 BusConnectionData *d;
2962 d = BUS_CONNECTION_DATA (connection);
2963 _dbus_assert (d != NULL);
2965 link = _dbus_list_alloc_link (connection);
2969 BUS_SET_OOM (error);
2973 if (!bcd_add_monitor_rules (d, connection, rules))
2975 _dbus_list_free_link (link);
2976 BUS_SET_OOM (error);
2980 /* release all its names */
2981 if (!_dbus_list_copy (&d->services_owned, &tmp))
2983 bcd_drop_monitor_rules (d, connection);
2984 _dbus_list_free_link (link);
2985 BUS_SET_OOM (error);
2989 for (iter = _dbus_list_get_first_link (&tmp);
2991 iter = _dbus_list_get_next_link (&tmp, iter))
2993 BusService *service = iter->data;
2995 /* This call is transactional: if there isn't enough memory to
2996 * do everything, then the service gets all its names back when
2997 * the transaction is cancelled due to OOM. */
2998 if (!bus_service_remove_owner (service, connection, transaction, error))
3000 bcd_drop_monitor_rules (d, connection);
3001 _dbus_list_free_link (link);
3002 _dbus_list_clear (&tmp);
3007 /* We have now done everything that can fail, so there is no problem
3008 * with doing the irrevocable stuff. */
3010 _dbus_list_clear (&tmp);
3012 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
3013 "Connection %s (%s) became a monitor.", d->name,
3014 d->cached_loginfo_string);
3016 if (d->n_match_rules > 0)
3020 mm = bus_context_get_matchmaker (d->connections->context);
3021 bus_matchmaker_disconnected (mm, connection);
3024 /* flag it as a monitor */
3025 d->link_in_monitors = link;
3026 _dbus_list_append_link (&d->connections->monitors, link);
3028 /* it isn't allowed to reply, and it is no longer relevant whether it
3029 * receives replies */
3030 bus_connection_drop_pending_replies (d->connections, connection);