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_add_owned_service_link (DBusConnection *connection,
1465 BusConnectionData *d;
1467 d = BUS_CONNECTION_DATA (connection);
1468 _dbus_assert (d != NULL);
1470 _dbus_list_append_link (&d->services_owned, link);
1472 d->n_services_owned += 1;
1474 #ifdef DBUS_ENABLE_STATS
1475 update_peak (&d->peak_bus_names, d->n_services_owned);
1476 update_peak (&d->connections->peak_bus_names_per_conn,
1477 d->n_services_owned);
1479 d->connections->total_bus_names += 1;
1480 update_peak (&d->connections->peak_bus_names,
1481 d->connections->total_bus_names);
1486 bus_connection_add_owned_service (DBusConnection *connection,
1487 BusService *service)
1491 link = _dbus_list_alloc_link (service);
1496 bus_connection_add_owned_service_link (connection, link);
1502 bus_connection_remove_owned_service (DBusConnection *connection,
1503 BusService *service)
1505 BusConnectionData *d;
1507 d = BUS_CONNECTION_DATA (connection);
1508 _dbus_assert (d != NULL);
1510 _dbus_list_remove_last (&d->services_owned, service);
1512 d->n_services_owned -= 1;
1513 _dbus_assert (d->n_services_owned >= 0);
1515 #ifdef DBUS_ENABLE_STATS
1516 d->connections->total_bus_names -= 1;
1521 bus_connection_get_n_services_owned (DBusConnection *connection)
1523 BusConnectionData *d;
1525 d = BUS_CONNECTION_DATA (connection);
1526 _dbus_assert (d != NULL);
1528 return d->n_services_owned;
1532 bus_connection_complete (DBusConnection *connection,
1533 const DBusString *name,
1536 BusConnectionData *d;
1539 d = BUS_CONNECTION_DATA (connection);
1540 _dbus_assert (d != NULL);
1541 _dbus_assert (d->name == NULL);
1542 _dbus_assert (d->policy == NULL);
1544 _dbus_assert (!bus_connection_is_active (connection));
1546 if (!_dbus_string_copy_data (name, &d->name))
1548 BUS_SET_OOM (error);
1552 _dbus_assert (d->name != NULL);
1554 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1556 d->policy = bus_context_create_client_policy (d->connections->context,
1560 /* we may have a NULL policy on OOM or error getting list of
1561 * groups for a user. In the latter case we don't handle it so
1562 * well currently, as it will just keep failing over and over.
1565 if (d->policy == NULL)
1567 _dbus_verbose ("Failed to create security policy for connection %p\n",
1569 _DBUS_ASSERT_ERROR_IS_SET (error);
1570 dbus_free (d->name);
1575 if (dbus_connection_get_unix_user (connection, &uid))
1577 if (!adjust_connections_for_uid (d->connections,
1582 /* Create and cache a string which holds information about the
1583 * peer process; used for logging purposes.
1585 if (!cache_peer_loginfo_string (d, connection))
1588 /* Now the connection is active, move it between lists */
1589 _dbus_list_unlink (&d->connections->incomplete,
1590 d->link_in_connection_list);
1591 d->connections->n_incomplete -= 1;
1592 _dbus_list_append_link (&d->connections->completed,
1593 d->link_in_connection_list);
1594 d->connections->n_completed += 1;
1596 _dbus_assert (d->connections->n_incomplete >= 0);
1597 _dbus_assert (d->connections->n_completed > 0);
1599 /* If we have dropped below the max. number of incomplete
1600 * connections, start accept()ing again */
1601 bus_context_check_all_watches (d->connections->context);
1603 /* See if we can remove the timeout */
1604 bus_connections_expire_incomplete (d->connections);
1606 _dbus_assert (bus_connection_is_active (connection));
1610 BUS_SET_OOM (error);
1611 dbus_free (d->name);
1614 bus_client_policy_unref (d->policy);
1620 bus_connections_reload_policy (BusConnections *connections,
1623 BusConnectionData *d;
1624 DBusConnection *connection;
1627 _dbus_assert (connections != NULL);
1628 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1630 for (link = _dbus_list_get_first_link (&(connections->completed));
1632 link = _dbus_list_get_next_link (&(connections->completed), link))
1634 connection = link->data;
1635 d = BUS_CONNECTION_DATA (connection);
1636 _dbus_assert (d != NULL);
1637 _dbus_assert (d->policy != NULL);
1639 bus_client_policy_unref (d->policy);
1640 d->policy = bus_context_create_client_policy (connections->context,
1643 if (d->policy == NULL)
1645 _dbus_verbose ("Failed to create security policy for connection %p\n",
1647 _DBUS_ASSERT_ERROR_IS_SET (error);
1656 bus_connection_get_name (DBusConnection *connection)
1658 BusConnectionData *d;
1660 d = BUS_CONNECTION_DATA (connection);
1661 _dbus_assert (d != NULL);
1667 * Check whether completing the passed-in connection would
1668 * exceed limits, and if so set error and return #FALSE
1671 bus_connections_check_limits (BusConnections *connections,
1672 DBusConnection *requesting_completion,
1677 if (connections->n_completed >=
1678 bus_context_get_max_completed_connections (connections->context))
1680 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1681 "The maximum number of active connections has been reached");
1685 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1687 if (get_connections_for_uid (connections, uid) >=
1688 bus_context_get_max_connections_per_user (connections->context))
1690 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1691 "The maximum number of active connections for UID %lu has been reached",
1701 bus_pending_reply_free (BusPendingReply *pending)
1703 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1705 pending->will_send_reply,
1706 pending->will_get_reply,
1707 pending->reply_serial);
1709 dbus_free (pending);
1713 bus_pending_reply_send_no_reply (BusConnections *connections,
1714 BusTransaction *transaction,
1715 BusPendingReply *pending)
1717 DBusMessage *message;
1718 DBusMessageIter iter;
1724 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1725 if (message == NULL)
1728 dbus_message_set_no_reply (message, TRUE);
1730 if (!dbus_message_set_reply_serial (message,
1731 pending->reply_serial))
1734 if (!dbus_message_set_error_name (message,
1735 DBUS_ERROR_NO_REPLY))
1738 /* If you change these messages, adjust test/dbus-daemon.c to match */
1739 if (pending->will_send_reply == NULL)
1740 errmsg = "Message recipient disconnected from message bus without replying";
1742 errmsg = "Message did not receive a reply (timeout by message bus)";
1744 dbus_message_iter_init_append (message, &iter);
1745 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1748 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1755 dbus_message_unref (message);
1760 bus_pending_reply_expired (BusExpireList *list,
1764 BusPendingReply *pending = link->data;
1765 BusConnections *connections = data;
1766 BusTransaction *transaction;
1768 /* No reply is forthcoming. So nuke it if we can. If not,
1769 * leave it in the list to try expiring again later when we
1773 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1775 pending->will_send_reply,
1776 pending->will_get_reply,
1777 pending->reply_serial);
1779 transaction = bus_transaction_new (connections->context);
1780 if (transaction == NULL)
1783 if (!bus_pending_reply_send_no_reply (connections,
1787 bus_transaction_cancel_and_free (transaction);
1791 bus_expire_list_remove_link (connections->pending_replies, link);
1793 bus_pending_reply_free (pending);
1794 bus_transaction_execute_and_free (transaction);
1800 bus_connection_drop_pending_replies (BusConnections *connections,
1801 DBusConnection *connection)
1803 /* The DBusConnection is almost 100% finalized here, so you can't
1804 * do anything with it except check for pointer equality
1808 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1811 link = bus_expire_list_get_first_link (connections->pending_replies);
1812 while (link != NULL)
1815 BusPendingReply *pending;
1817 next = bus_expire_list_get_next_link (connections->pending_replies,
1819 pending = link->data;
1821 if (pending->will_get_reply == connection)
1823 /* We don't need to track this pending reply anymore */
1825 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1827 pending->will_send_reply,
1828 pending->will_get_reply,
1829 pending->reply_serial);
1831 bus_expire_list_remove_link (connections->pending_replies,
1833 bus_pending_reply_free (pending);
1835 else if (pending->will_send_reply == connection)
1837 /* The reply isn't going to be sent, so set things
1838 * up so it will be expired right away
1840 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1842 pending->will_send_reply,
1843 pending->will_get_reply,
1844 pending->reply_serial);
1846 pending->will_send_reply = NULL;
1847 pending->expire_item.added_tv_sec = 0;
1848 pending->expire_item.added_tv_usec = 0;
1850 bus_expire_list_recheck_immediately (connections->pending_replies);
1860 BusPendingReply *pending;
1861 BusConnections *connections;
1862 } CancelPendingReplyData;
1865 cancel_pending_reply (void *data)
1867 CancelPendingReplyData *d = data;
1869 _dbus_verbose ("d = %p\n", d);
1871 if (!bus_expire_list_remove (d->connections->pending_replies,
1872 &d->pending->expire_item))
1873 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1875 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1879 cancel_pending_reply_data_free (void *data)
1881 CancelPendingReplyData *d = data;
1883 _dbus_verbose ("d = %p\n", d);
1885 /* d->pending should be either freed or still
1886 * in the list of pending replies (owned by someone
1894 * Record that a reply is allowed; return TRUE on success.
1897 bus_connections_expect_reply (BusConnections *connections,
1898 BusTransaction *transaction,
1899 DBusConnection *will_get_reply,
1900 DBusConnection *will_send_reply,
1901 DBusMessage *reply_to_this,
1904 BusPendingReply *pending;
1905 dbus_uint32_t reply_serial;
1907 CancelPendingReplyData *cprd;
1910 _dbus_assert (will_get_reply != NULL);
1911 _dbus_assert (will_send_reply != NULL);
1912 _dbus_assert (reply_to_this != NULL);
1914 if (dbus_message_get_no_reply (reply_to_this))
1915 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1917 reply_serial = dbus_message_get_serial (reply_to_this);
1919 link = bus_expire_list_get_first_link (connections->pending_replies);
1921 while (link != NULL)
1923 pending = link->data;
1925 if (pending->reply_serial == reply_serial &&
1926 pending->will_get_reply == will_get_reply &&
1927 pending->will_send_reply == will_send_reply)
1929 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1930 "Message has the same reply serial as a currently-outstanding existing method call");
1934 link = bus_expire_list_get_next_link (connections->pending_replies,
1936 if (pending->will_get_reply == will_get_reply)
1941 bus_context_get_max_replies_per_connection (connections->context))
1943 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1944 "The maximum number of pending replies per connection has been reached");
1948 pending = dbus_new0 (BusPendingReply, 1);
1949 if (pending == NULL)
1951 BUS_SET_OOM (error);
1955 #ifdef DBUS_ENABLE_VERBOSE_MODE
1956 /* so we can see a not-yet-added pending reply */
1957 pending->expire_item.added_tv_sec = 1;
1958 pending->expire_item.added_tv_usec = 1;
1961 pending->will_get_reply = will_get_reply;
1962 pending->will_send_reply = will_send_reply;
1963 pending->reply_serial = reply_serial;
1965 cprd = dbus_new0 (CancelPendingReplyData, 1);
1968 BUS_SET_OOM (error);
1969 bus_pending_reply_free (pending);
1973 if (!bus_expire_list_add (connections->pending_replies,
1974 &pending->expire_item))
1976 BUS_SET_OOM (error);
1978 bus_pending_reply_free (pending);
1982 if (!bus_transaction_add_cancel_hook (transaction,
1983 cancel_pending_reply,
1985 cancel_pending_reply_data_free))
1987 BUS_SET_OOM (error);
1988 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1990 bus_pending_reply_free (pending);
1994 cprd->pending = pending;
1995 cprd->connections = connections;
1997 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1998 &pending->expire_item.added_tv_usec);
2000 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2002 pending->will_send_reply,
2003 pending->will_get_reply,
2004 pending->reply_serial);
2012 BusConnections *connections;
2013 } CheckPendingReplyData;
2016 cancel_check_pending_reply (void *data)
2018 CheckPendingReplyData *d = data;
2020 _dbus_verbose ("d = %p\n",d);
2022 bus_expire_list_add_link (d->connections->pending_replies,
2028 check_pending_reply_data_free (void *data)
2030 CheckPendingReplyData *d = data;
2032 _dbus_verbose ("d = %p\n",d);
2034 if (d->link != NULL)
2036 BusPendingReply *pending = d->link->data;
2038 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2039 &pending->expire_item));
2041 bus_pending_reply_free (pending);
2042 _dbus_list_free_link (d->link);
2049 * Check whether a reply is allowed, remove BusPendingReply
2050 * if so, return TRUE if so.
2053 bus_connections_check_reply (BusConnections *connections,
2054 BusTransaction *transaction,
2055 DBusConnection *sending_reply,
2056 DBusConnection *receiving_reply,
2060 CheckPendingReplyData *cprd;
2062 dbus_uint32_t reply_serial;
2064 _dbus_assert (sending_reply != NULL);
2065 _dbus_assert (receiving_reply != NULL);
2067 reply_serial = dbus_message_get_reply_serial (reply);
2069 link = bus_expire_list_get_first_link (connections->pending_replies);
2070 while (link != NULL)
2072 BusPendingReply *pending = link->data;
2074 if (pending->reply_serial == reply_serial &&
2075 pending->will_get_reply == receiving_reply &&
2076 pending->will_send_reply == sending_reply)
2078 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2082 link = bus_expire_list_get_next_link (connections->pending_replies,
2088 _dbus_verbose ("No pending reply expected\n");
2093 cprd = dbus_new0 (CheckPendingReplyData, 1);
2096 BUS_SET_OOM (error);
2100 if (!bus_transaction_add_cancel_hook (transaction,
2101 cancel_check_pending_reply,
2103 check_pending_reply_data_free))
2105 BUS_SET_OOM (error);
2111 cprd->connections = connections;
2113 bus_expire_list_unlink (connections->pending_replies,
2116 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2124 * Note that this is fairly fragile; in particular, don't try to use
2125 * one transaction across any main loop iterations.
2130 BusTransaction *transaction;
2131 DBusMessage *message;
2132 DBusPreallocatedSend *preallocated;
2137 BusTransactionCancelFunction cancel_function;
2138 DBusFreeFunction free_data_function;
2142 struct BusTransaction
2144 DBusList *connections;
2145 BusContext *context;
2146 DBusList *cancel_hooks;
2150 message_to_send_free (DBusConnection *connection,
2151 MessageToSend *to_send)
2153 if (to_send->message)
2154 dbus_message_unref (to_send->message);
2156 if (to_send->preallocated)
2157 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2159 dbus_free (to_send);
2163 cancel_hook_cancel (void *element,
2166 CancelHook *ch = element;
2168 _dbus_verbose ("Running transaction cancel hook\n");
2170 if (ch->cancel_function)
2171 (* ch->cancel_function) (ch->data);
2175 cancel_hook_free (void *element,
2178 CancelHook *ch = element;
2180 if (ch->free_data_function)
2181 (* ch->free_data_function) (ch->data);
2187 free_cancel_hooks (BusTransaction *transaction)
2189 _dbus_list_foreach (&transaction->cancel_hooks,
2190 cancel_hook_free, NULL);
2192 _dbus_list_clear (&transaction->cancel_hooks);
2196 bus_transaction_new (BusContext *context)
2198 BusTransaction *transaction;
2200 transaction = dbus_new0 (BusTransaction, 1);
2201 if (transaction == NULL)
2204 transaction->context = context;
2210 bus_transaction_get_context (BusTransaction *transaction)
2212 return transaction->context;
2216 * Reserve enough memory to capture the given message if the
2217 * transaction goes through.
2220 bus_transaction_capture (BusTransaction *transaction,
2221 DBusConnection *sender,
2222 DBusMessage *message)
2224 BusConnections *connections;
2227 DBusList *recipients = NULL;
2228 dbus_bool_t ret = FALSE;
2230 connections = bus_context_get_connections (transaction->context);
2232 /* shortcut: don't compose the message unless someone wants it */
2233 if (connections->monitors == NULL)
2236 mm = connections->monitor_matchmaker;
2237 /* This is non-null if there has ever been a monitor - we don't GC it.
2238 * There's little point, since there is up to 1 per process. */
2239 _dbus_assert (mm != NULL);
2241 if (!bus_matchmaker_get_recipients (mm, connections, sender, NULL, message,
2245 for (link = _dbus_list_get_first_link (&recipients);
2247 link = _dbus_list_get_next_link (&recipients, link))
2249 DBusConnection *recipient = link->data;
2251 if (!bus_transaction_send (transaction, recipient, message, FALSE))
2258 _dbus_list_clear (&recipients);
2263 bus_transaction_capture_error_reply (BusTransaction *transaction,
2264 const DBusError *error,
2265 DBusMessage *in_reply_to)
2267 BusConnections *connections;
2269 dbus_bool_t ret = FALSE;
2271 _dbus_assert (error != NULL);
2272 _DBUS_ASSERT_ERROR_IS_SET (error);
2274 connections = bus_context_get_connections (transaction->context);
2276 /* shortcut: don't compose the message unless someone wants it */
2277 if (connections->monitors == NULL)
2280 reply = dbus_message_new_error (in_reply_to,
2287 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2290 ret = bus_transaction_capture (transaction, NULL, reply);
2293 dbus_message_unref (reply);
2298 bus_transaction_send_from_driver (BusTransaction *transaction,
2299 DBusConnection *connection,
2300 DBusMessage *message)
2302 DBusError error = DBUS_ERROR_INIT;
2303 BusDeferredMessage *deferred_message;
2305 /* We have to set the sender to the driver, and have
2306 * to check security policy since it was not done in
2309 _dbus_verbose ("Sending %s %s %s from driver\n",
2310 dbus_message_get_interface (message) ?
2311 dbus_message_get_interface (message) : "(no interface)",
2312 dbus_message_get_member (message) ?
2313 dbus_message_get_member (message) : "(no member)",
2314 dbus_message_get_error_name (message) ?
2315 dbus_message_get_error_name (message) : "(no error name)");
2317 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2320 if (bus_connection_is_active (connection))
2322 if (!dbus_message_set_destination (message,
2323 bus_connection_get_name (connection)))
2327 /* bus driver never wants a reply */
2328 dbus_message_set_no_reply (message, TRUE);
2330 /* Capture it for monitors, even if the real recipient's receive policy
2331 * does not allow it to receive this message from us (which would be odd).
2333 if (!bus_transaction_capture (transaction, NULL, message))
2336 /* If security policy doesn't allow the message, we would silently
2337 * eat it; the driver doesn't care about getting a reply. However,
2338 * if we're actively capturing messages, it's nice to log that we
2339 * tried to send it and did not allow ourselves to do so.
2341 switch (bus_context_check_security_policy (bus_transaction_get_context (transaction),
2343 NULL, connection, connection,
2344 message, NULL, &error,
2347 case BUS_RESULT_TRUE:
2349 case BUS_RESULT_FALSE:
2350 if (!bus_transaction_capture_error_reply (transaction, &error, message))
2352 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2353 "message from dbus-daemon rejected but not enough "
2354 "memory to capture it");
2357 /* This is not fatal to the transaction so silently eat the disallowed
2358 * message (see reasoning above) */
2359 dbus_error_free (&error);
2362 case BUS_RESULT_LATER:
2363 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
2365 return TRUE; /* pretend to have sent it */
2368 return bus_transaction_send (transaction, connection, message, FALSE);
2372 bus_transaction_send (BusTransaction *transaction,
2373 DBusConnection *connection,
2374 DBusMessage *message,
2375 dbus_bool_t deferred_dispatch)
2377 MessageToSend *to_send;
2378 BusConnectionData *d;
2381 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2382 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2383 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2385 dbus_message_get_interface (message) ?
2386 dbus_message_get_interface (message) : "(unset)",
2387 dbus_message_get_member (message) ?
2388 dbus_message_get_member (message) : "(unset)",
2389 dbus_message_get_error_name (message) ?
2390 dbus_message_get_error_name (message) : "(unset)",
2391 dbus_connection_get_is_connected (connection) ?
2392 "" : " (disconnected)");
2394 _dbus_assert (dbus_message_get_sender (message) != NULL);
2396 if (!dbus_connection_get_is_connected (connection))
2397 return TRUE; /* silently ignore disconnected connections */
2399 d = BUS_CONNECTION_DATA (connection);
2400 _dbus_assert (d != NULL);
2402 if (!deferred_dispatch && d->deferred_messages != NULL)
2404 BusDeferredMessage *deferred_message;
2405 dbus_bool_t success;
2406 /* sender and addressed recipient are not required at this point as we only need to send message
2407 * to a single recipient without performing policy check. */
2408 deferred_message = bus_deferred_message_new (message,
2413 if (deferred_message == NULL)
2416 success = bus_deferred_message_queue_at_recipient(deferred_message, transaction,
2418 bus_deferred_message_unref(deferred_message);
2423 to_send = dbus_new (MessageToSend, 1);
2424 if (to_send == NULL)
2429 to_send->preallocated = dbus_connection_preallocate_send (connection);
2430 if (to_send->preallocated == NULL)
2432 dbus_free (to_send);
2436 dbus_message_ref (message);
2437 to_send->message = message;
2438 to_send->transaction = transaction;
2440 _dbus_verbose ("about to prepend message\n");
2442 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2444 message_to_send_free (connection, to_send);
2448 _dbus_verbose ("prepended message\n");
2450 /* See if we already had this connection in the list
2451 * for this transaction. If we have a pending message,
2452 * then we should already be in transaction->connections
2454 link = _dbus_list_get_first_link (&d->transaction_messages);
2455 _dbus_assert (link->data == to_send);
2456 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2457 while (link != NULL)
2459 MessageToSend *m = link->data;
2460 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2462 if (m->transaction == transaction)
2470 if (!_dbus_list_prepend (&transaction->connections, connection))
2472 _dbus_list_remove (&d->transaction_messages, to_send);
2473 message_to_send_free (connection, to_send);
2482 transaction_free (BusTransaction *transaction)
2484 _dbus_assert (transaction->connections == NULL);
2486 free_cancel_hooks (transaction);
2488 dbus_free (transaction);
2492 connection_cancel_transaction (DBusConnection *connection,
2493 BusTransaction *transaction)
2496 BusConnectionData *d;
2498 d = BUS_CONNECTION_DATA (connection);
2499 _dbus_assert (d != NULL);
2501 link = _dbus_list_get_first_link (&d->transaction_messages);
2502 while (link != NULL)
2504 MessageToSend *m = link->data;
2505 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2507 if (m->transaction == transaction)
2509 _dbus_list_remove_link (&d->transaction_messages,
2512 message_to_send_free (connection, m);
2520 bus_transaction_cancel_and_free (BusTransaction *transaction)
2522 DBusConnection *connection;
2524 _dbus_verbose ("TRANSACTION: cancelled\n");
2526 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2527 connection_cancel_transaction (connection, transaction);
2529 _dbus_list_foreach (&transaction->cancel_hooks,
2530 cancel_hook_cancel, NULL);
2532 transaction_free (transaction);
2536 connection_execute_transaction (DBusConnection *connection,
2537 BusTransaction *transaction)
2540 BusConnectionData *d;
2542 d = BUS_CONNECTION_DATA (connection);
2543 _dbus_assert (d != NULL);
2545 /* Send the queue in order (FIFO) */
2546 link = _dbus_list_get_last_link (&d->transaction_messages);
2547 while (link != NULL)
2549 MessageToSend *m = link->data;
2550 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2552 if (m->transaction == transaction)
2554 _dbus_list_remove_link (&d->transaction_messages,
2557 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2559 dbus_connection_send_preallocated (connection,
2564 m->preallocated = NULL; /* so we don't double-free it */
2566 message_to_send_free (connection, m);
2574 bus_transaction_execute_and_free (BusTransaction *transaction)
2576 /* For each connection in transaction->connections
2579 DBusConnection *connection;
2581 _dbus_verbose ("TRANSACTION: executing\n");
2583 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2584 connection_execute_transaction (connection, transaction);
2586 transaction_free (transaction);
2590 bus_connection_remove_transactions (DBusConnection *connection)
2592 MessageToSend *to_send;
2593 BusConnectionData *d;
2595 d = BUS_CONNECTION_DATA (connection);
2596 _dbus_assert (d != NULL);
2598 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2600 /* only has an effect for the first MessageToSend listing this transaction */
2601 _dbus_list_remove (&to_send->transaction->connections,
2604 _dbus_list_remove (&d->transaction_messages, to_send);
2605 message_to_send_free (connection, to_send);
2610 * Converts the DBusError to a message reply
2613 bus_transaction_send_error_reply (BusTransaction *transaction,
2614 DBusConnection *connection,
2615 const DBusError *error,
2616 DBusMessage *in_reply_to)
2620 _dbus_assert (error != NULL);
2621 _DBUS_ASSERT_ERROR_IS_SET (error);
2623 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2624 error->name, error->message);
2626 reply = dbus_message_new_error (in_reply_to,
2632 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2634 dbus_message_unref (reply);
2638 dbus_message_unref (reply);
2644 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2645 BusTransactionCancelFunction cancel_function,
2647 DBusFreeFunction free_data_function)
2651 ch = dbus_new (CancelHook, 1);
2655 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2656 cancel_function, data);
2658 ch->cancel_function = cancel_function;
2660 ch->free_data_function = free_data_function;
2662 /* It's important that the hooks get run in reverse order that they
2665 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2675 bus_connection_dispatch_deferred (DBusConnection *connection)
2677 BusDeferredMessage *message;
2679 _dbus_return_if_fail (connection != NULL);
2681 while ((message = bus_connection_pop_deferred_message(connection)) != NULL)
2683 bus_deferred_message_dispatch(message);
2684 bus_deferred_message_unref(message);
2689 bus_connection_has_deferred_messages (DBusConnection *connection)
2691 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2692 return d->deferred_messages != NULL ? TRUE : FALSE;
2696 bus_connection_queue_deferred_message (DBusConnection *connection,
2697 BusDeferredMessage *message,
2698 dbus_bool_t prepend)
2700 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2701 dbus_bool_t success;
2703 success = _dbus_list_prepend(&d->deferred_messages, message);
2705 success = _dbus_list_append(&d->deferred_messages, message);
2709 bus_deferred_message_ref(message);
2717 bus_connection_replace_deferred_message (DBusConnection *connection,
2718 BusDeferredMessage *oldMessage,
2719 BusDeferredMessage *newMessage)
2722 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2724 link = _dbus_list_find_first(&d->deferred_messages, oldMessage);
2728 if (!_dbus_list_insert_after(&d->deferred_messages, link, newMessage))
2731 bus_deferred_message_ref(newMessage);
2732 _dbus_list_remove_link(&d->deferred_messages, link);
2733 bus_deferred_message_unref(oldMessage);
2737 BusDeferredMessage *
2738 bus_connection_pop_deferred_message (DBusConnection *connection)
2741 BusDeferredMessage *message;
2742 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2744 link =_dbus_list_get_first_link(&d->deferred_messages);
2747 message = link->data;
2748 if (!bus_deferred_message_waits_for_check(message))
2750 _dbus_list_remove_link(&d->deferred_messages, link);
2759 bus_connection_putback_deferred_message (DBusConnection *connection, BusDeferredMessage *message)
2761 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2762 if (_dbus_list_prepend(&d->deferred_messages, message))
2770 bus_connection_clear_deferred_messages (DBusConnection *connection)
2772 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2775 BusDeferredMessage *message;
2777 link =_dbus_list_get_first_link(&d->deferred_messages);
2778 while (link != NULL)
2780 next = _dbus_list_get_next_link (&d->deferred_messages, link);
2781 message = link->data;
2783 bus_deferred_message_unref(message);
2784 _dbus_list_remove_link(&d->deferred_messages, link);
2791 bus_connection_remove_deferred_message (DBusConnection *connection,
2792 BusDeferredMessage *message)
2794 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2795 if (_dbus_list_remove(&d->deferred_messages, message))
2796 bus_deferred_message_unref(message);
2800 bus_connections_get_n_active (BusConnections *connections)
2802 return connections->n_completed;
2806 bus_connections_get_n_incomplete (BusConnections *connections)
2808 return connections->n_incomplete;
2811 #ifdef DBUS_ENABLE_STATS
2813 bus_connections_get_total_match_rules (BusConnections *connections)
2815 return connections->total_match_rules;
2819 bus_connections_get_peak_match_rules (BusConnections *connections)
2821 return connections->peak_match_rules;
2825 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2827 return connections->peak_match_rules_per_conn;
2831 bus_connections_get_total_bus_names (BusConnections *connections)
2833 return connections->total_bus_names;
2837 bus_connections_get_peak_bus_names (BusConnections *connections)
2839 return connections->peak_bus_names;
2843 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2845 return connections->peak_bus_names_per_conn;
2849 bus_connection_get_peak_match_rules (DBusConnection *connection)
2851 BusConnectionData *d;
2853 d = BUS_CONNECTION_DATA (connection);
2854 _dbus_assert(d != NULL);
2856 return d->peak_match_rules;
2860 bus_connection_get_peak_bus_names (DBusConnection *connection)
2862 BusConnectionData *d;
2864 d = BUS_CONNECTION_DATA (connection);
2865 _dbus_assert(d != NULL);
2867 return d->peak_bus_names;
2869 #endif /* DBUS_ENABLE_STATS */
2872 bus_connection_is_monitor (DBusConnection *connection)
2874 BusConnectionData *d;
2876 d = BUS_CONNECTION_DATA (connection);
2877 _dbus_assert(d != NULL);
2879 return d->link_in_monitors != NULL;
2883 bcd_add_monitor_rules (BusConnectionData *d,
2884 DBusConnection *connection,
2887 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2892 mm = bus_matchmaker_new ();
2897 d->connections->monitor_matchmaker = mm;
2900 for (iter = _dbus_list_get_first_link (rules);
2902 iter = _dbus_list_get_next_link (rules, iter))
2904 if (!bus_matchmaker_add_rule (mm, iter->data))
2906 bus_matchmaker_disconnected (mm, connection);
2915 bcd_drop_monitor_rules (BusConnectionData *d,
2916 DBusConnection *connection)
2918 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2921 bus_matchmaker_disconnected (mm, connection);
2925 bus_connection_be_monitor (DBusConnection *connection,
2926 BusTransaction *transaction,
2930 BusConnectionData *d;
2935 d = BUS_CONNECTION_DATA (connection);
2936 _dbus_assert (d != NULL);
2938 link = _dbus_list_alloc_link (connection);
2942 BUS_SET_OOM (error);
2946 if (!bcd_add_monitor_rules (d, connection, rules))
2948 _dbus_list_free_link (link);
2949 BUS_SET_OOM (error);
2953 /* release all its names */
2954 if (!_dbus_list_copy (&d->services_owned, &tmp))
2956 bcd_drop_monitor_rules (d, connection);
2957 _dbus_list_free_link (link);
2958 BUS_SET_OOM (error);
2962 for (iter = _dbus_list_get_first_link (&tmp);
2964 iter = _dbus_list_get_next_link (&tmp, iter))
2966 BusService *service = iter->data;
2968 /* This call is transactional: if there isn't enough memory to
2969 * do everything, then the service gets all its names back when
2970 * the transaction is cancelled due to OOM. */
2971 if (!bus_service_remove_owner (service, connection, transaction, error))
2973 bcd_drop_monitor_rules (d, connection);
2974 _dbus_list_free_link (link);
2975 _dbus_list_clear (&tmp);
2980 /* We have now done everything that can fail, so there is no problem
2981 * with doing the irrevocable stuff. */
2983 _dbus_list_clear (&tmp);
2985 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2986 "Connection %s (%s) became a monitor.", d->name,
2987 d->cached_loginfo_string);
2989 if (d->n_match_rules > 0)
2993 mm = bus_context_get_matchmaker (d->connections->context);
2994 bus_matchmaker_disconnected (mm, connection);
2997 /* flag it as a monitor */
2998 d->link_in_monitors = link;
2999 _dbus_list_append_link (&d->connections->monitors, link);
3001 /* it isn't allowed to reply, and it is no longer relevant whether it
3002 * receives replies */
3003 bus_connection_drop_pending_replies (d->connections, connection);