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>
46 /* Trim executed commands to this length; we want to keep logs readable */
47 #define MAX_LOG_COMMAND_LEN 50
49 static void bus_connection_remove_transactions (DBusConnection *connection);
53 BusExpireItem expire_item;
55 DBusConnection *will_get_reply;
56 DBusConnection *will_send_reply;
58 dbus_uint32_t reply_serial;
65 DBusList *completed; /**< List of all completed connections */
66 int n_completed; /**< Length of completed list */
67 DBusList *incomplete; /**< List of all not-yet-active connections */
68 int n_incomplete; /**< Length of incomplete list */
70 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
71 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
72 int stamp; /**< Incrementing number */
73 BusExpireList *pending_replies; /**< List of pending replies */
75 /** List of all monitoring connections, a subset of completed.
76 * Each member is a #DBusConnection. */
78 BusMatchmaker *monitor_matchmaker;
80 #ifdef DBUS_ENABLE_STATS
81 int total_match_rules;
83 int peak_match_rules_per_conn;
87 int peak_bus_names_per_conn;
91 static dbus_int32_t connection_data_slot = -1;
95 BusConnections *connections;
96 DBusList *link_in_connection_list;
97 DBusConnection *connection;
98 DBusList *services_owned;
100 DBusList *match_rules;
103 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
104 DBusMessage *oom_message;
105 DBusPreallocatedSend *oom_preallocated;
106 BusClientPolicy *policy;
107 DBusList *deferred_messages; /**< Queue of messages deferred due to pending policy check */
109 char *cached_loginfo_string;
110 BusSELinuxID *selinux_id;
111 BusAppArmorConfinement *apparmor_confinement;
113 long connection_tv_sec; /**< Time when we connected (seconds component) */
114 long connection_tv_usec; /**< Time when we connected (microsec component) */
115 int stamp; /**< connections->stamp last time we were traversed */
117 #ifdef DBUS_ENABLE_STATS
118 int peak_match_rules;
121 int n_pending_unix_fds;
122 DBusTimeout *pending_unix_fds_timeout;
124 /** non-NULL if and only if this is a monitor */
125 DBusList *link_in_monitors;
126 #ifdef DBUS_ENABLE_CYNARA
127 char *cynara_session_id;
131 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
135 static void bus_connection_drop_pending_replies (BusConnections *connections,
136 DBusConnection *connection);
138 static dbus_bool_t expire_incomplete_timeout (void *data);
140 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
143 bus_connection_get_loop (DBusConnection *connection)
145 BusConnectionData *d;
147 d = BUS_CONNECTION_DATA (connection);
148 _dbus_assert(d != NULL);
150 return bus_context_get_loop (d->connections->context);
155 get_connections_for_uid (BusConnections *connections,
161 /* val is NULL is 0 when it isn't in the hash yet */
163 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
166 current_count = _DBUS_POINTER_TO_INT (val);
168 return current_count;
172 adjust_connections_for_uid (BusConnections *connections,
178 current_count = get_connections_for_uid (connections, uid);
180 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
181 ": was %d adjustment %d making %d\n",
182 uid, current_count, adjustment, current_count + adjustment);
184 _dbus_assert (current_count >= 0);
186 current_count += adjustment;
188 _dbus_assert (current_count >= 0);
190 if (current_count == 0)
192 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
199 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
200 uid, _DBUS_INT_TO_POINTER (current_count));
202 /* only positive adjustment can fail as otherwise
203 * a hash entry should already exist
205 _dbus_assert (adjustment > 0 ||
206 (adjustment <= 0 && retval));
213 bus_connection_disconnected (DBusConnection *connection)
215 BusConnectionData *d;
217 BusMatchmaker *matchmaker;
219 d = BUS_CONNECTION_DATA (connection);
220 _dbus_assert (d != NULL);
222 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
223 d->name ? d->name : "(inactive)");
225 /* Delete our match rules */
226 if (d->n_match_rules > 0)
228 matchmaker = bus_context_get_matchmaker (d->connections->context);
229 bus_matchmaker_disconnected (matchmaker, connection);
232 /* Drop any service ownership. Unfortunately, this requires
233 * memory allocation and there doesn't seem to be a good way to
234 * handle it other than sleeping; we can't "fail" the operation of
235 * disconnecting a client, and preallocating a broadcast "service is
236 * now gone" message for every client-service pair seems kind of
239 while ((service = _dbus_list_get_last (&d->services_owned)))
241 BusTransaction *transaction;
246 dbus_error_init (&error);
248 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
249 _dbus_wait_for_memory ();
251 if (!bus_service_remove_owner (service, connection,
252 transaction, &error))
254 _DBUS_ASSERT_ERROR_IS_SET (&error);
256 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
258 dbus_error_free (&error);
259 bus_transaction_cancel_and_free (transaction);
260 _dbus_wait_for_memory ();
265 _dbus_verbose ("Failed to remove service owner: %s %s\n",
266 error.name, error.message);
267 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
271 bus_transaction_execute_and_free (transaction);
274 bus_connection_clear_deferred_messages(connection);
276 bus_dispatch_remove_connection (connection);
278 /* no more watching */
279 if (!dbus_connection_set_watch_functions (connection,
283 _dbus_assert_not_reached ("setting watch functions to NULL failed");
285 if (!dbus_connection_set_timeout_functions (connection,
289 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
291 dbus_connection_set_unix_user_function (connection,
293 dbus_connection_set_windows_user_function (connection,
296 dbus_connection_set_dispatch_status_function (connection,
299 if (d->pending_unix_fds_timeout)
301 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
302 d->pending_unix_fds_timeout);
303 _dbus_timeout_unref (d->pending_unix_fds_timeout);
305 d->pending_unix_fds_timeout = NULL;
306 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
308 bus_connection_remove_transactions (connection);
310 if (d->link_in_monitors != NULL)
312 BusMatchmaker *mm = d->connections->monitor_matchmaker;
315 bus_matchmaker_disconnected (mm, connection);
317 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
318 d->link_in_monitors = NULL;
321 if (d->link_in_connection_list != NULL)
327 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
328 d->link_in_connection_list = NULL;
329 d->connections->n_completed -= 1;
331 if (dbus_connection_get_unix_user (connection, &uid))
333 if (!adjust_connections_for_uid (d->connections,
335 _dbus_assert_not_reached ("adjusting downward should never fail");
340 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
341 d->link_in_connection_list = NULL;
342 d->connections->n_incomplete -= 1;
344 /* If we have dropped below the max. number of incomplete
345 * connections, start accept()ing again */
346 bus_context_check_all_watches (d->connections->context);
349 _dbus_assert (d->connections->n_incomplete >= 0);
350 _dbus_assert (d->connections->n_completed >= 0);
353 bus_connection_drop_pending_replies (d->connections, connection);
355 /* frees "d" as side effect */
356 dbus_connection_set_data (connection,
357 connection_data_slot,
360 dbus_connection_unref (connection);
364 add_connection_watch (DBusWatch *watch,
367 DBusConnection *connection = data;
369 return _dbus_loop_add_watch (bus_connection_get_loop (connection), watch);
373 remove_connection_watch (DBusWatch *watch,
376 DBusConnection *connection = data;
378 _dbus_loop_remove_watch (bus_connection_get_loop (connection), watch);
382 toggle_connection_watch (DBusWatch *watch,
385 DBusConnection *connection = data;
387 _dbus_loop_toggle_watch (bus_connection_get_loop (connection), watch);
391 add_connection_timeout (DBusTimeout *timeout,
394 DBusConnection *connection = data;
396 return _dbus_loop_add_timeout (bus_connection_get_loop (connection), timeout);
400 remove_connection_timeout (DBusTimeout *timeout,
403 DBusConnection *connection = data;
405 _dbus_loop_remove_timeout (bus_connection_get_loop (connection), timeout);
409 dispatch_status_function (DBusConnection *connection,
410 DBusDispatchStatus new_status,
413 DBusLoop *loop = data;
415 if (new_status != DBUS_DISPATCH_COMPLETE)
417 while (!_dbus_loop_queue_dispatch (loop, connection))
418 _dbus_wait_for_memory ();
423 allow_unix_user_function (DBusConnection *connection,
427 BusConnectionData *d;
429 d = BUS_CONNECTION_DATA (connection);
431 _dbus_assert (d != NULL);
433 return bus_context_allow_unix_user (d->connections->context, uid);
437 free_connection_data (void *data)
439 BusConnectionData *d = data;
441 /* services_owned should be NULL since we should be disconnected */
442 _dbus_assert (d->services_owned == NULL);
443 _dbus_assert (d->n_services_owned == 0);
445 _dbus_assert (d->transaction_messages == NULL);
447 if (d->oom_preallocated)
448 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
451 dbus_message_unref (d->oom_message);
454 bus_client_policy_unref (d->policy);
457 bus_selinux_id_unref (d->selinux_id);
459 if (d->apparmor_confinement)
460 bus_apparmor_confinement_unref (d->apparmor_confinement);
462 dbus_free (d->cached_loginfo_string);
466 #ifdef DBUS_ENABLE_CYNARA
467 free (d->cynara_session_id);
474 bus_connections_new (BusContext *context)
476 BusConnections *connections;
478 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
481 connections = dbus_new0 (BusConnections, 1);
482 if (connections == NULL)
485 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
487 if (connections->completed_by_user == NULL)
490 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
491 expire_incomplete_timeout,
493 if (connections->expire_timeout == NULL)
496 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
498 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
499 bus_context_get_reply_timeout (context),
500 bus_pending_reply_expired,
502 if (connections->pending_replies == NULL)
505 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
506 connections->expire_timeout))
509 connections->refcount = 1;
510 connections->context = context;
515 bus_expire_list_free (connections->pending_replies);
517 _dbus_timeout_unref (connections->expire_timeout);
519 _dbus_hash_table_unref (connections->completed_by_user);
521 dbus_free (connections);
523 dbus_connection_free_data_slot (&connection_data_slot);
529 bus_connections_ref (BusConnections *connections)
531 _dbus_assert (connections->refcount > 0);
532 connections->refcount += 1;
538 bus_connections_unref (BusConnections *connections)
540 _dbus_assert (connections->refcount > 0);
541 connections->refcount -= 1;
542 if (connections->refcount == 0)
544 /* drop all incomplete */
545 while (connections->incomplete != NULL)
547 DBusConnection *connection;
549 connection = connections->incomplete->data;
551 dbus_connection_ref (connection);
552 dbus_connection_close (connection);
553 bus_connection_disconnected (connection);
554 dbus_connection_unref (connection);
557 _dbus_assert (connections->n_incomplete == 0);
559 /* drop all monitors */
560 _dbus_list_clear (&connections->monitors);
562 /* drop all real connections */
563 while (connections->completed != NULL)
565 DBusConnection *connection;
567 connection = connections->completed->data;
569 dbus_connection_ref (connection);
570 dbus_connection_close (connection);
571 bus_connection_disconnected (connection);
572 dbus_connection_unref (connection);
575 _dbus_assert (connections->n_completed == 0);
577 bus_expire_list_free (connections->pending_replies);
579 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
580 connections->expire_timeout);
582 _dbus_timeout_unref (connections->expire_timeout);
584 _dbus_hash_table_unref (connections->completed_by_user);
586 if (connections->monitor_matchmaker != NULL)
587 bus_matchmaker_unref (connections->monitor_matchmaker);
589 dbus_free (connections);
591 dbus_connection_free_data_slot (&connection_data_slot);
595 /* Used for logging */
597 cache_peer_loginfo_string (BusConnectionData *d,
598 DBusConnection *connection)
600 DBusString loginfo_buf;
604 dbus_bool_t prev_added;
606 if (!_dbus_string_init (&loginfo_buf))
610 if (dbus_connection_get_unix_user (connection, &uid))
612 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
618 if (dbus_connection_get_unix_process_id (connection, &pid))
622 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
625 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
627 /* Ignore errors here; we may not have permissions to read the
629 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
630 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
634 if (dbus_connection_get_windows_user (connection, &windows_sid))
636 dbus_bool_t did_append;
637 did_append = _dbus_string_append_printf (&loginfo_buf,
638 "sid=\"%s\" ", windows_sid);
639 dbus_free (windows_sid);
644 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
647 _dbus_string_free (&loginfo_buf);
651 _dbus_string_free (&loginfo_buf);
656 check_pending_fds_cb (DBusConnection *connection)
658 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
659 int n_pending_unix_fds_old;
660 int n_pending_unix_fds_new;
662 _dbus_assert(d != NULL);
664 n_pending_unix_fds_old = d->n_pending_unix_fds;
665 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
667 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
668 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
670 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
672 _dbus_timeout_set_interval (d->pending_unix_fds_timeout,
673 bus_context_get_pending_fd_timeout (d->connections->context));
674 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, TRUE);
677 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
679 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
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_set_enabled (d->pending_unix_fds_timeout, FALSE);
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 return d->cynara_session_id;
1073 foreach_active (BusConnections *connections,
1074 BusConnectionForeachFunction function,
1079 link = _dbus_list_get_first_link (&connections->completed);
1080 while (link != NULL)
1082 DBusConnection *connection = link->data;
1083 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1085 if (!(* function) (connection, data))
1095 foreach_inactive (BusConnections *connections,
1096 BusConnectionForeachFunction function,
1101 link = _dbus_list_get_first_link (&connections->incomplete);
1102 while (link != NULL)
1104 DBusConnection *connection = link->data;
1105 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1107 if (!(* function) (connection, data))
1117 * Calls function on each active connection; if the function returns
1118 * #FALSE, stops iterating. Active connections are authenticated
1119 * and have sent a Hello message.
1121 * @param connections the connections object
1122 * @param function the function
1123 * @param data data to pass to it as a second arg
1126 bus_connections_foreach_active (BusConnections *connections,
1127 BusConnectionForeachFunction function,
1130 foreach_active (connections, function, data);
1134 * Calls function on each connection; if the function returns
1135 * #FALSE, stops iterating.
1137 * @param connections the connections object
1138 * @param function the function
1139 * @param data data to pass to it as a second arg
1142 bus_connections_foreach (BusConnections *connections,
1143 BusConnectionForeachFunction function,
1146 if (!foreach_active (connections, function, data))
1149 foreach_inactive (connections, function, data);
1153 bus_connections_get_context (BusConnections *connections)
1155 return connections->context;
1159 * This is used to avoid covering the same connection twice when
1160 * traversing connections. Note that it assumes we will
1161 * bus_connection_mark_stamp() each connection at least once per
1162 * INT_MAX increments of the global stamp, or wraparound would break
1166 bus_connections_increment_stamp (BusConnections *connections)
1168 connections->stamp += 1;
1171 /* Mark connection with current stamp, return TRUE if it
1172 * didn't already have that stamp
1175 bus_connection_mark_stamp (DBusConnection *connection)
1177 BusConnectionData *d;
1179 d = BUS_CONNECTION_DATA (connection);
1181 _dbus_assert (d != NULL);
1183 if (d->stamp == d->connections->stamp)
1187 d->stamp = d->connections->stamp;
1193 bus_connection_get_context (DBusConnection *connection)
1195 BusConnectionData *d;
1197 d = BUS_CONNECTION_DATA (connection);
1199 _dbus_assert (d != NULL);
1201 return d->connections->context;
1205 bus_connection_get_connections (DBusConnection *connection)
1207 BusConnectionData *d;
1209 d = BUS_CONNECTION_DATA (connection);
1211 _dbus_assert (d != NULL);
1213 return d->connections;
1217 bus_connection_get_registry (DBusConnection *connection)
1219 BusConnectionData *d;
1221 d = BUS_CONNECTION_DATA (connection);
1223 _dbus_assert (d != NULL);
1225 return bus_context_get_registry (d->connections->context);
1229 bus_connection_get_activation (DBusConnection *connection)
1231 BusConnectionData *d;
1233 d = BUS_CONNECTION_DATA (connection);
1235 _dbus_assert (d != NULL);
1237 return bus_context_get_activation (d->connections->context);
1241 bus_connection_get_matchmaker (DBusConnection *connection)
1243 BusConnectionData *d;
1245 d = BUS_CONNECTION_DATA (connection);
1247 _dbus_assert (d != NULL);
1249 return bus_context_get_matchmaker (d->connections->context);
1253 bus_connection_get_selinux_id (DBusConnection *connection)
1255 BusConnectionData *d;
1257 d = BUS_CONNECTION_DATA (connection);
1259 _dbus_assert (d != NULL);
1261 return d->selinux_id;
1264 BusAppArmorConfinement*
1265 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1267 BusConnectionData *d;
1269 d = BUS_CONNECTION_DATA (connection);
1271 _dbus_assert (d != NULL);
1273 bus_apparmor_confinement_ref (d->apparmor_confinement);
1274 return d->apparmor_confinement;
1278 * Checks whether the connection is registered with the message bus.
1280 * @param connection the connection
1281 * @returns #TRUE if we're an active message bus participant
1284 bus_connection_is_active (DBusConnection *connection)
1286 BusConnectionData *d;
1288 d = BUS_CONNECTION_DATA (connection);
1289 _dbus_assert(d != NULL);
1291 return d->name != NULL;
1295 bus_connection_preallocate_oom_error (DBusConnection *connection)
1297 DBusMessage *message;
1298 DBusPreallocatedSend *preallocated;
1299 BusConnectionData *d;
1301 d = BUS_CONNECTION_DATA (connection);
1303 _dbus_assert (d != NULL);
1305 if (d->oom_preallocated != NULL)
1308 preallocated = dbus_connection_preallocate_send (connection);
1309 if (preallocated == NULL)
1312 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1314 if (message == NULL)
1316 dbus_connection_free_preallocated_send (connection, preallocated);
1320 /* d->name may be NULL, but that is OK */
1321 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1322 !dbus_message_set_destination (message, d->name) ||
1323 !dbus_message_set_sender (message,
1326 dbus_connection_free_preallocated_send (connection, preallocated);
1327 dbus_message_unref (message);
1331 /* set reply serial to placeholder value just so space is already allocated
1334 if (!dbus_message_set_reply_serial (message, 14))
1336 dbus_connection_free_preallocated_send (connection, preallocated);
1337 dbus_message_unref (message);
1341 d->oom_message = message;
1342 d->oom_preallocated = preallocated;
1348 bus_connection_send_oom_error (DBusConnection *connection,
1349 DBusMessage *in_reply_to)
1351 BusConnectionData *d;
1353 d = BUS_CONNECTION_DATA (connection);
1355 _dbus_assert (d != NULL);
1356 _dbus_assert (d->oom_message != NULL);
1358 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1359 "dbus-daemon transaction failed (OOM), sending error to "
1360 "sender %s", bus_connection_get_loginfo (connection));
1362 /* should always succeed since we set it to a placeholder earlier */
1363 if (!dbus_message_set_reply_serial (d->oom_message,
1364 dbus_message_get_serial (in_reply_to)))
1365 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1367 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1369 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1370 d->oom_message, NULL);
1372 dbus_message_unref (d->oom_message);
1373 d->oom_message = NULL;
1374 d->oom_preallocated = NULL;
1377 #ifdef DBUS_ENABLE_STATS
1379 update_peak (int *peak,
1388 bus_connection_add_match_rule_link (DBusConnection *connection,
1391 BusConnectionData *d;
1393 d = BUS_CONNECTION_DATA (connection);
1394 _dbus_assert (d != NULL);
1396 _dbus_list_append_link (&d->match_rules, link);
1398 d->n_match_rules += 1;
1400 #ifdef DBUS_ENABLE_STATS
1401 update_peak (&d->peak_match_rules, d->n_match_rules);
1402 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1404 d->connections->total_match_rules += 1;
1405 update_peak (&d->connections->peak_match_rules,
1406 d->connections->total_match_rules);
1411 bus_connection_add_match_rule (DBusConnection *connection,
1416 link = _dbus_list_alloc_link (rule);
1421 bus_connection_add_match_rule_link (connection, link);
1427 bus_connection_remove_match_rule (DBusConnection *connection,
1430 BusConnectionData *d;
1432 d = BUS_CONNECTION_DATA (connection);
1433 _dbus_assert (d != NULL);
1435 _dbus_list_remove_last (&d->match_rules, rule);
1437 d->n_match_rules -= 1;
1438 _dbus_assert (d->n_match_rules >= 0);
1440 #ifdef DBUS_ENABLE_STATS
1441 d->connections->total_match_rules -= 1;
1446 bus_connection_get_n_match_rules (DBusConnection *connection)
1448 BusConnectionData *d;
1450 d = BUS_CONNECTION_DATA (connection);
1451 _dbus_assert (d != NULL);
1453 return d->n_match_rules;
1457 bus_connection_add_owned_service_link (DBusConnection *connection,
1460 BusConnectionData *d;
1462 d = BUS_CONNECTION_DATA (connection);
1463 _dbus_assert (d != NULL);
1465 _dbus_list_append_link (&d->services_owned, link);
1467 d->n_services_owned += 1;
1469 #ifdef DBUS_ENABLE_STATS
1470 update_peak (&d->peak_bus_names, d->n_services_owned);
1471 update_peak (&d->connections->peak_bus_names_per_conn,
1472 d->n_services_owned);
1474 d->connections->total_bus_names += 1;
1475 update_peak (&d->connections->peak_bus_names,
1476 d->connections->total_bus_names);
1481 bus_connection_add_owned_service (DBusConnection *connection,
1482 BusService *service)
1486 link = _dbus_list_alloc_link (service);
1491 bus_connection_add_owned_service_link (connection, link);
1497 bus_connection_remove_owned_service (DBusConnection *connection,
1498 BusService *service)
1500 BusConnectionData *d;
1502 d = BUS_CONNECTION_DATA (connection);
1503 _dbus_assert (d != NULL);
1505 _dbus_list_remove_last (&d->services_owned, service);
1507 d->n_services_owned -= 1;
1508 _dbus_assert (d->n_services_owned >= 0);
1510 #ifdef DBUS_ENABLE_STATS
1511 d->connections->total_bus_names -= 1;
1516 bus_connection_get_n_services_owned (DBusConnection *connection)
1518 BusConnectionData *d;
1520 d = BUS_CONNECTION_DATA (connection);
1521 _dbus_assert (d != NULL);
1523 return d->n_services_owned;
1527 bus_connection_complete (DBusConnection *connection,
1528 const DBusString *name,
1531 BusConnectionData *d;
1534 d = BUS_CONNECTION_DATA (connection);
1535 _dbus_assert (d != NULL);
1536 _dbus_assert (d->name == NULL);
1537 _dbus_assert (d->policy == NULL);
1539 _dbus_assert (!bus_connection_is_active (connection));
1541 if (!_dbus_string_copy_data (name, &d->name))
1543 BUS_SET_OOM (error);
1547 _dbus_assert (d->name != NULL);
1549 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1551 d->policy = bus_context_create_client_policy (d->connections->context,
1555 /* we may have a NULL policy on OOM or error getting list of
1556 * groups for a user. In the latter case we don't handle it so
1557 * well currently, as it will just keep failing over and over.
1560 if (d->policy == NULL)
1562 _dbus_verbose ("Failed to create security policy for connection %p\n",
1564 _DBUS_ASSERT_ERROR_IS_SET (error);
1565 dbus_free (d->name);
1570 if (dbus_connection_get_unix_user (connection, &uid))
1572 if (!adjust_connections_for_uid (d->connections,
1577 /* Create and cache a string which holds information about the
1578 * peer process; used for logging purposes.
1580 if (!cache_peer_loginfo_string (d, connection))
1583 /* Now the connection is active, move it between lists */
1584 _dbus_list_unlink (&d->connections->incomplete,
1585 d->link_in_connection_list);
1586 d->connections->n_incomplete -= 1;
1587 _dbus_list_append_link (&d->connections->completed,
1588 d->link_in_connection_list);
1589 d->connections->n_completed += 1;
1591 _dbus_assert (d->connections->n_incomplete >= 0);
1592 _dbus_assert (d->connections->n_completed > 0);
1594 /* If we have dropped below the max. number of incomplete
1595 * connections, start accept()ing again */
1596 bus_context_check_all_watches (d->connections->context);
1598 /* See if we can remove the timeout */
1599 bus_connections_expire_incomplete (d->connections);
1601 _dbus_assert (bus_connection_is_active (connection));
1605 BUS_SET_OOM (error);
1606 dbus_free (d->name);
1609 bus_client_policy_unref (d->policy);
1615 bus_connections_reload_policy (BusConnections *connections,
1618 BusConnectionData *d;
1619 DBusConnection *connection;
1622 _dbus_assert (connections != NULL);
1623 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1625 for (link = _dbus_list_get_first_link (&(connections->completed));
1627 link = _dbus_list_get_next_link (&(connections->completed), link))
1629 connection = link->data;
1630 d = BUS_CONNECTION_DATA (connection);
1631 _dbus_assert (d != NULL);
1632 _dbus_assert (d->policy != NULL);
1634 bus_client_policy_unref (d->policy);
1635 d->policy = bus_context_create_client_policy (connections->context,
1638 if (d->policy == NULL)
1640 _dbus_verbose ("Failed to create security policy for connection %p\n",
1642 _DBUS_ASSERT_ERROR_IS_SET (error);
1651 bus_connection_get_name (DBusConnection *connection)
1653 BusConnectionData *d;
1655 d = BUS_CONNECTION_DATA (connection);
1656 _dbus_assert (d != NULL);
1662 * Check whether completing the passed-in connection would
1663 * exceed limits, and if so set error and return #FALSE
1666 bus_connections_check_limits (BusConnections *connections,
1667 DBusConnection *requesting_completion,
1672 if (connections->n_completed >=
1673 bus_context_get_max_completed_connections (connections->context))
1675 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1676 "The maximum number of active connections has been reached");
1680 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1682 if (get_connections_for_uid (connections, uid) >=
1683 bus_context_get_max_connections_per_user (connections->context))
1685 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1686 "The maximum number of active connections for UID %lu has been reached",
1696 bus_pending_reply_free (BusPendingReply *pending)
1698 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1700 pending->will_send_reply,
1701 pending->will_get_reply,
1702 pending->reply_serial);
1704 dbus_free (pending);
1708 bus_pending_reply_send_no_reply (BusConnections *connections,
1709 BusTransaction *transaction,
1710 BusPendingReply *pending)
1712 DBusMessage *message;
1713 DBusMessageIter iter;
1719 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1720 if (message == NULL)
1723 dbus_message_set_no_reply (message, TRUE);
1725 if (!dbus_message_set_reply_serial (message,
1726 pending->reply_serial))
1729 if (!dbus_message_set_error_name (message,
1730 DBUS_ERROR_NO_REPLY))
1733 /* If you change these messages, adjust test/dbus-daemon.c to match */
1734 if (pending->will_send_reply == NULL)
1735 errmsg = "Message recipient disconnected from message bus without replying";
1737 errmsg = "Message did not receive a reply (timeout by message bus)";
1739 dbus_message_iter_init_append (message, &iter);
1740 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1743 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1750 dbus_message_unref (message);
1755 bus_pending_reply_expired (BusExpireList *list,
1759 BusPendingReply *pending = link->data;
1760 BusConnections *connections = data;
1761 BusTransaction *transaction;
1763 /* No reply is forthcoming. So nuke it if we can. If not,
1764 * leave it in the list to try expiring again later when we
1768 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1770 pending->will_send_reply,
1771 pending->will_get_reply,
1772 pending->reply_serial);
1774 transaction = bus_transaction_new (connections->context);
1775 if (transaction == NULL)
1778 if (!bus_pending_reply_send_no_reply (connections,
1782 bus_transaction_cancel_and_free (transaction);
1786 bus_expire_list_remove_link (connections->pending_replies, link);
1788 bus_pending_reply_free (pending);
1789 bus_transaction_execute_and_free (transaction);
1795 bus_connection_drop_pending_replies (BusConnections *connections,
1796 DBusConnection *connection)
1798 /* The DBusConnection is almost 100% finalized here, so you can't
1799 * do anything with it except check for pointer equality
1803 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1806 link = bus_expire_list_get_first_link (connections->pending_replies);
1807 while (link != NULL)
1810 BusPendingReply *pending;
1812 next = bus_expire_list_get_next_link (connections->pending_replies,
1814 pending = link->data;
1816 if (pending->will_get_reply == connection)
1818 /* We don't need to track this pending reply anymore */
1820 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1822 pending->will_send_reply,
1823 pending->will_get_reply,
1824 pending->reply_serial);
1826 bus_expire_list_remove_link (connections->pending_replies,
1828 bus_pending_reply_free (pending);
1830 else if (pending->will_send_reply == connection)
1832 /* The reply isn't going to be sent, so set things
1833 * up so it will be expired right away
1835 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1837 pending->will_send_reply,
1838 pending->will_get_reply,
1839 pending->reply_serial);
1841 pending->will_send_reply = NULL;
1842 pending->expire_item.added_tv_sec = 0;
1843 pending->expire_item.added_tv_usec = 0;
1845 bus_expire_list_recheck_immediately (connections->pending_replies);
1855 BusPendingReply *pending;
1856 BusConnections *connections;
1857 } CancelPendingReplyData;
1860 cancel_pending_reply (void *data)
1862 CancelPendingReplyData *d = data;
1864 _dbus_verbose ("d = %p\n", d);
1866 if (!bus_expire_list_remove (d->connections->pending_replies,
1867 &d->pending->expire_item))
1868 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1870 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1874 cancel_pending_reply_data_free (void *data)
1876 CancelPendingReplyData *d = data;
1878 _dbus_verbose ("d = %p\n", d);
1880 /* d->pending should be either freed or still
1881 * in the list of pending replies (owned by someone
1889 * Record that a reply is allowed; return TRUE on success.
1892 bus_connections_expect_reply (BusConnections *connections,
1893 BusTransaction *transaction,
1894 DBusConnection *will_get_reply,
1895 DBusConnection *will_send_reply,
1896 DBusMessage *reply_to_this,
1899 BusPendingReply *pending;
1900 dbus_uint32_t reply_serial;
1902 CancelPendingReplyData *cprd;
1905 _dbus_assert (will_get_reply != NULL);
1906 _dbus_assert (will_send_reply != NULL);
1907 _dbus_assert (reply_to_this != NULL);
1909 if (dbus_message_get_no_reply (reply_to_this))
1910 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1912 reply_serial = dbus_message_get_serial (reply_to_this);
1914 link = bus_expire_list_get_first_link (connections->pending_replies);
1916 while (link != NULL)
1918 pending = link->data;
1920 if (pending->reply_serial == reply_serial &&
1921 pending->will_get_reply == will_get_reply &&
1922 pending->will_send_reply == will_send_reply)
1924 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1925 "Message has the same reply serial as a currently-outstanding existing method call");
1929 link = bus_expire_list_get_next_link (connections->pending_replies,
1931 if (pending->will_get_reply == will_get_reply)
1936 bus_context_get_max_replies_per_connection (connections->context))
1938 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1939 "The maximum number of pending replies per connection has been reached");
1943 pending = dbus_new0 (BusPendingReply, 1);
1944 if (pending == NULL)
1946 BUS_SET_OOM (error);
1950 #ifdef DBUS_ENABLE_VERBOSE_MODE
1951 /* so we can see a not-yet-added pending reply */
1952 pending->expire_item.added_tv_sec = 1;
1953 pending->expire_item.added_tv_usec = 1;
1956 pending->will_get_reply = will_get_reply;
1957 pending->will_send_reply = will_send_reply;
1958 pending->reply_serial = reply_serial;
1960 cprd = dbus_new0 (CancelPendingReplyData, 1);
1963 BUS_SET_OOM (error);
1964 bus_pending_reply_free (pending);
1968 if (!bus_expire_list_add (connections->pending_replies,
1969 &pending->expire_item))
1971 BUS_SET_OOM (error);
1973 bus_pending_reply_free (pending);
1977 if (!bus_transaction_add_cancel_hook (transaction,
1978 cancel_pending_reply,
1980 cancel_pending_reply_data_free))
1982 BUS_SET_OOM (error);
1983 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1985 bus_pending_reply_free (pending);
1989 cprd->pending = pending;
1990 cprd->connections = connections;
1992 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1993 &pending->expire_item.added_tv_usec);
1995 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1997 pending->will_send_reply,
1998 pending->will_get_reply,
1999 pending->reply_serial);
2007 BusConnections *connections;
2008 } CheckPendingReplyData;
2011 cancel_check_pending_reply (void *data)
2013 CheckPendingReplyData *d = data;
2015 _dbus_verbose ("d = %p\n",d);
2017 bus_expire_list_add_link (d->connections->pending_replies,
2023 check_pending_reply_data_free (void *data)
2025 CheckPendingReplyData *d = data;
2027 _dbus_verbose ("d = %p\n",d);
2029 if (d->link != NULL)
2031 BusPendingReply *pending = d->link->data;
2033 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2034 &pending->expire_item));
2036 bus_pending_reply_free (pending);
2037 _dbus_list_free_link (d->link);
2044 * Check whether a reply is allowed, remove BusPendingReply
2045 * if so, return TRUE if so.
2048 bus_connections_check_reply (BusConnections *connections,
2049 BusTransaction *transaction,
2050 DBusConnection *sending_reply,
2051 DBusConnection *receiving_reply,
2055 CheckPendingReplyData *cprd;
2057 dbus_uint32_t reply_serial;
2059 _dbus_assert (sending_reply != NULL);
2060 _dbus_assert (receiving_reply != NULL);
2062 reply_serial = dbus_message_get_reply_serial (reply);
2064 link = bus_expire_list_get_first_link (connections->pending_replies);
2065 while (link != NULL)
2067 BusPendingReply *pending = link->data;
2069 if (pending->reply_serial == reply_serial &&
2070 pending->will_get_reply == receiving_reply &&
2071 pending->will_send_reply == sending_reply)
2073 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2077 link = bus_expire_list_get_next_link (connections->pending_replies,
2083 _dbus_verbose ("No pending reply expected\n");
2088 cprd = dbus_new0 (CheckPendingReplyData, 1);
2091 BUS_SET_OOM (error);
2095 if (!bus_transaction_add_cancel_hook (transaction,
2096 cancel_check_pending_reply,
2098 check_pending_reply_data_free))
2100 BUS_SET_OOM (error);
2106 cprd->connections = connections;
2108 bus_expire_list_unlink (connections->pending_replies,
2111 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2119 * Note that this is fairly fragile; in particular, don't try to use
2120 * one transaction across any main loop iterations.
2125 BusTransaction *transaction;
2126 DBusMessage *message;
2127 DBusPreallocatedSend *preallocated;
2132 BusTransactionCancelFunction cancel_function;
2133 DBusFreeFunction free_data_function;
2137 struct BusTransaction
2139 DBusList *connections;
2140 BusContext *context;
2141 DBusList *cancel_hooks;
2145 message_to_send_free (DBusConnection *connection,
2146 MessageToSend *to_send)
2148 if (to_send->message)
2149 dbus_message_unref (to_send->message);
2151 if (to_send->preallocated)
2152 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2154 dbus_free (to_send);
2158 cancel_hook_cancel (void *element,
2161 CancelHook *ch = element;
2163 _dbus_verbose ("Running transaction cancel hook\n");
2165 if (ch->cancel_function)
2166 (* ch->cancel_function) (ch->data);
2170 cancel_hook_free (void *element,
2173 CancelHook *ch = element;
2175 if (ch->free_data_function)
2176 (* ch->free_data_function) (ch->data);
2182 free_cancel_hooks (BusTransaction *transaction)
2184 _dbus_list_foreach (&transaction->cancel_hooks,
2185 cancel_hook_free, NULL);
2187 _dbus_list_clear (&transaction->cancel_hooks);
2191 bus_transaction_new (BusContext *context)
2193 BusTransaction *transaction;
2195 transaction = dbus_new0 (BusTransaction, 1);
2196 if (transaction == NULL)
2199 transaction->context = context;
2205 bus_transaction_get_context (BusTransaction *transaction)
2207 return transaction->context;
2211 * Reserve enough memory to capture the given message if the
2212 * transaction goes through.
2215 bus_transaction_capture (BusTransaction *transaction,
2216 DBusConnection *sender,
2217 DBusMessage *message)
2219 BusConnections *connections;
2222 DBusList *recipients = NULL;
2223 dbus_bool_t ret = FALSE;
2225 connections = bus_context_get_connections (transaction->context);
2227 /* shortcut: don't compose the message unless someone wants it */
2228 if (connections->monitors == NULL)
2231 mm = connections->monitor_matchmaker;
2232 /* This is non-null if there has ever been a monitor - we don't GC it.
2233 * There's little point, since there is up to 1 per process. */
2234 _dbus_assert (mm != NULL);
2236 if (!bus_matchmaker_get_recipients (mm, connections, sender, NULL, message,
2240 for (link = _dbus_list_get_first_link (&recipients);
2242 link = _dbus_list_get_next_link (&recipients, link))
2244 DBusConnection *recipient = link->data;
2246 if (!bus_transaction_send (transaction, recipient, message, FALSE))
2253 _dbus_list_clear (&recipients);
2258 bus_transaction_capture_error_reply (BusTransaction *transaction,
2259 const DBusError *error,
2260 DBusMessage *in_reply_to)
2262 BusConnections *connections;
2264 dbus_bool_t ret = FALSE;
2266 _dbus_assert (error != NULL);
2267 _DBUS_ASSERT_ERROR_IS_SET (error);
2269 connections = bus_context_get_connections (transaction->context);
2271 /* shortcut: don't compose the message unless someone wants it */
2272 if (connections->monitors == NULL)
2275 reply = dbus_message_new_error (in_reply_to,
2282 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2285 ret = bus_transaction_capture (transaction, NULL, reply);
2288 dbus_message_unref (reply);
2293 bus_transaction_send_from_driver (BusTransaction *transaction,
2294 DBusConnection *connection,
2295 DBusMessage *message)
2297 DBusError error = DBUS_ERROR_INIT;
2298 BusDeferredMessage *deferred_message;
2300 /* We have to set the sender to the driver, and have
2301 * to check security policy since it was not done in
2304 _dbus_verbose ("Sending %s %s %s from driver\n",
2305 dbus_message_get_interface (message) ?
2306 dbus_message_get_interface (message) : "(no interface)",
2307 dbus_message_get_member (message) ?
2308 dbus_message_get_member (message) : "(no member)",
2309 dbus_message_get_error_name (message) ?
2310 dbus_message_get_error_name (message) : "(no error name)");
2312 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2315 if (bus_connection_is_active (connection))
2317 if (!dbus_message_set_destination (message,
2318 bus_connection_get_name (connection)))
2322 /* bus driver never wants a reply */
2323 dbus_message_set_no_reply (message, TRUE);
2325 /* Capture it for monitors, even if the real recipient's receive policy
2326 * does not allow it to receive this message from us (which would be odd).
2328 if (!bus_transaction_capture (transaction, NULL, message))
2331 /* If security policy doesn't allow the message, we would silently
2332 * eat it; the driver doesn't care about getting a reply. However,
2333 * if we're actively capturing messages, it's nice to log that we
2334 * tried to send it and did not allow ourselves to do so.
2336 switch (bus_context_check_security_policy (bus_transaction_get_context (transaction),
2338 NULL, connection, connection, message, &error,
2341 case BUS_RESULT_TRUE:
2343 case BUS_RESULT_FALSE:
2344 if (!bus_transaction_capture_error_reply (transaction, &error, message))
2346 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2347 "message from dbus-daemon rejected but not enough "
2348 "memory to capture it");
2351 /* This is not fatal to the transaction so silently eat the disallowed
2352 * message (see reasoning above) */
2353 dbus_error_free (&error);
2356 case BUS_RESULT_LATER:
2357 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
2359 return TRUE; /* pretend to have sent it */
2362 return bus_transaction_send (transaction, connection, message, FALSE);
2366 bus_transaction_send (BusTransaction *transaction,
2367 DBusConnection *connection,
2368 DBusMessage *message,
2369 dbus_bool_t deferred_dispatch)
2371 MessageToSend *to_send;
2372 BusConnectionData *d;
2375 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2376 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2377 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2379 dbus_message_get_interface (message) ?
2380 dbus_message_get_interface (message) : "(unset)",
2381 dbus_message_get_member (message) ?
2382 dbus_message_get_member (message) : "(unset)",
2383 dbus_message_get_error_name (message) ?
2384 dbus_message_get_error_name (message) : "(unset)",
2385 dbus_connection_get_is_connected (connection) ?
2386 "" : " (disconnected)");
2388 _dbus_assert (dbus_message_get_sender (message) != NULL);
2390 if (!dbus_connection_get_is_connected (connection))
2391 return TRUE; /* silently ignore disconnected connections */
2393 d = BUS_CONNECTION_DATA (connection);
2394 _dbus_assert (d != NULL);
2396 if (!deferred_dispatch && d->deferred_messages != NULL)
2398 BusDeferredMessage *deferred_message;
2399 dbus_bool_t success;
2400 /* sender and addressed recipient are not required at this point as we only need to send message
2401 * to a single recipient without performing policy check. */
2402 deferred_message = bus_deferred_message_new (message,
2407 if (deferred_message == NULL)
2410 success = bus_deferred_message_queue_at_recipient(deferred_message, transaction,
2412 bus_deferred_message_unref(deferred_message);
2417 to_send = dbus_new (MessageToSend, 1);
2418 if (to_send == NULL)
2423 to_send->preallocated = dbus_connection_preallocate_send (connection);
2424 if (to_send->preallocated == NULL)
2426 dbus_free (to_send);
2430 dbus_message_ref (message);
2431 to_send->message = message;
2432 to_send->transaction = transaction;
2434 _dbus_verbose ("about to prepend message\n");
2436 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2438 message_to_send_free (connection, to_send);
2442 _dbus_verbose ("prepended message\n");
2444 /* See if we already had this connection in the list
2445 * for this transaction. If we have a pending message,
2446 * then we should already be in transaction->connections
2448 link = _dbus_list_get_first_link (&d->transaction_messages);
2449 _dbus_assert (link->data == to_send);
2450 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2451 while (link != NULL)
2453 MessageToSend *m = link->data;
2454 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2456 if (m->transaction == transaction)
2464 if (!_dbus_list_prepend (&transaction->connections, connection))
2466 _dbus_list_remove (&d->transaction_messages, to_send);
2467 message_to_send_free (connection, to_send);
2476 transaction_free (BusTransaction *transaction)
2478 _dbus_assert (transaction->connections == NULL);
2480 free_cancel_hooks (transaction);
2482 dbus_free (transaction);
2486 connection_cancel_transaction (DBusConnection *connection,
2487 BusTransaction *transaction)
2490 BusConnectionData *d;
2492 d = BUS_CONNECTION_DATA (connection);
2493 _dbus_assert (d != NULL);
2495 link = _dbus_list_get_first_link (&d->transaction_messages);
2496 while (link != NULL)
2498 MessageToSend *m = link->data;
2499 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2501 if (m->transaction == transaction)
2503 _dbus_list_remove_link (&d->transaction_messages,
2506 message_to_send_free (connection, m);
2514 bus_transaction_cancel_and_free (BusTransaction *transaction)
2516 DBusConnection *connection;
2518 _dbus_verbose ("TRANSACTION: cancelled\n");
2520 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2521 connection_cancel_transaction (connection, transaction);
2523 _dbus_list_foreach (&transaction->cancel_hooks,
2524 cancel_hook_cancel, NULL);
2526 transaction_free (transaction);
2530 connection_execute_transaction (DBusConnection *connection,
2531 BusTransaction *transaction)
2534 BusConnectionData *d;
2536 d = BUS_CONNECTION_DATA (connection);
2537 _dbus_assert (d != NULL);
2539 /* Send the queue in order (FIFO) */
2540 link = _dbus_list_get_last_link (&d->transaction_messages);
2541 while (link != NULL)
2543 MessageToSend *m = link->data;
2544 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2546 if (m->transaction == transaction)
2548 _dbus_list_remove_link (&d->transaction_messages,
2551 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2553 dbus_connection_send_preallocated (connection,
2558 m->preallocated = NULL; /* so we don't double-free it */
2560 message_to_send_free (connection, m);
2568 bus_transaction_execute_and_free (BusTransaction *transaction)
2570 /* For each connection in transaction->connections
2573 DBusConnection *connection;
2575 _dbus_verbose ("TRANSACTION: executing\n");
2577 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2578 connection_execute_transaction (connection, transaction);
2580 transaction_free (transaction);
2584 bus_connection_remove_transactions (DBusConnection *connection)
2586 MessageToSend *to_send;
2587 BusConnectionData *d;
2589 d = BUS_CONNECTION_DATA (connection);
2590 _dbus_assert (d != NULL);
2592 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2594 /* only has an effect for the first MessageToSend listing this transaction */
2595 _dbus_list_remove (&to_send->transaction->connections,
2598 _dbus_list_remove (&d->transaction_messages, to_send);
2599 message_to_send_free (connection, to_send);
2604 * Converts the DBusError to a message reply
2607 bus_transaction_send_error_reply (BusTransaction *transaction,
2608 DBusConnection *connection,
2609 const DBusError *error,
2610 DBusMessage *in_reply_to)
2614 _dbus_assert (error != NULL);
2615 _DBUS_ASSERT_ERROR_IS_SET (error);
2617 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2618 error->name, error->message);
2620 reply = dbus_message_new_error (in_reply_to,
2626 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2628 dbus_message_unref (reply);
2632 dbus_message_unref (reply);
2638 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2639 BusTransactionCancelFunction cancel_function,
2641 DBusFreeFunction free_data_function)
2645 ch = dbus_new (CancelHook, 1);
2649 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2650 cancel_function, data);
2652 ch->cancel_function = cancel_function;
2654 ch->free_data_function = free_data_function;
2656 /* It's important that the hooks get run in reverse order that they
2659 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2669 bus_connection_dispatch_deferred (DBusConnection *connection)
2671 BusDeferredMessage *message;
2673 _dbus_return_if_fail (connection != NULL);
2675 while ((message = bus_connection_pop_deferred_message(connection)) != NULL)
2677 bus_deferred_message_dispatch(message);
2678 bus_deferred_message_unref(message);
2683 bus_connection_has_deferred_messages (DBusConnection *connection)
2685 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2686 return d->deferred_messages != NULL ? TRUE : FALSE;
2690 bus_connection_queue_deferred_message (DBusConnection *connection,
2691 BusDeferredMessage *message,
2692 dbus_bool_t prepend)
2694 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2695 dbus_bool_t success;
2697 success = _dbus_list_prepend(&d->deferred_messages, message);
2699 success = _dbus_list_append(&d->deferred_messages, message);
2703 bus_deferred_message_ref(message);
2711 bus_connection_replace_deferred_message (DBusConnection *connection,
2712 BusDeferredMessage *oldMessage,
2713 BusDeferredMessage *newMessage)
2716 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2718 link = _dbus_list_find_first(&d->deferred_messages, oldMessage);
2722 if (!_dbus_list_insert_after(&d->deferred_messages, link, newMessage))
2725 bus_deferred_message_ref(newMessage);
2726 _dbus_list_remove_link(&d->deferred_messages, link);
2727 bus_deferred_message_unref(oldMessage);
2731 BusDeferredMessage *
2732 bus_connection_pop_deferred_message (DBusConnection *connection)
2735 BusDeferredMessage *message;
2736 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2738 link =_dbus_list_get_first_link(&d->deferred_messages);
2741 message = link->data;
2742 if (!bus_deferred_message_waits_for_check(message))
2744 _dbus_list_remove_link(&d->deferred_messages, link);
2753 bus_connection_putback_deferred_message (DBusConnection *connection, BusDeferredMessage *message)
2755 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2756 if (_dbus_list_prepend(&d->deferred_messages, message))
2764 bus_connection_clear_deferred_messages (DBusConnection *connection)
2766 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2769 BusDeferredMessage *message;
2771 link =_dbus_list_get_first_link(&d->deferred_messages);
2772 while (link != NULL)
2774 next = _dbus_list_get_next_link (&d->deferred_messages, link);
2775 message = link->data;
2777 bus_deferred_message_unref(message);
2778 _dbus_list_remove_link(&d->deferred_messages, link);
2785 bus_connection_remove_deferred_message (DBusConnection *connection,
2786 BusDeferredMessage *message)
2788 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2789 if (_dbus_list_remove(&d->deferred_messages, message))
2790 bus_deferred_message_unref(message);
2794 bus_connections_get_n_active (BusConnections *connections)
2796 return connections->n_completed;
2800 bus_connections_get_n_incomplete (BusConnections *connections)
2802 return connections->n_incomplete;
2805 #ifdef DBUS_ENABLE_STATS
2807 bus_connections_get_total_match_rules (BusConnections *connections)
2809 return connections->total_match_rules;
2813 bus_connections_get_peak_match_rules (BusConnections *connections)
2815 return connections->peak_match_rules;
2819 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2821 return connections->peak_match_rules_per_conn;
2825 bus_connections_get_total_bus_names (BusConnections *connections)
2827 return connections->total_bus_names;
2831 bus_connections_get_peak_bus_names (BusConnections *connections)
2833 return connections->peak_bus_names;
2837 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2839 return connections->peak_bus_names_per_conn;
2843 bus_connection_get_peak_match_rules (DBusConnection *connection)
2845 BusConnectionData *d;
2847 d = BUS_CONNECTION_DATA (connection);
2848 _dbus_assert(d != NULL);
2850 return d->peak_match_rules;
2854 bus_connection_get_peak_bus_names (DBusConnection *connection)
2856 BusConnectionData *d;
2858 d = BUS_CONNECTION_DATA (connection);
2859 _dbus_assert(d != NULL);
2861 return d->peak_bus_names;
2863 #endif /* DBUS_ENABLE_STATS */
2866 bus_connection_is_monitor (DBusConnection *connection)
2868 BusConnectionData *d;
2870 d = BUS_CONNECTION_DATA (connection);
2871 _dbus_assert(d != NULL);
2873 return d->link_in_monitors != NULL;
2877 bcd_add_monitor_rules (BusConnectionData *d,
2878 DBusConnection *connection,
2881 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2886 mm = bus_matchmaker_new ();
2891 d->connections->monitor_matchmaker = mm;
2894 for (iter = _dbus_list_get_first_link (rules);
2896 iter = _dbus_list_get_next_link (rules, iter))
2898 if (!bus_matchmaker_add_rule (mm, iter->data))
2900 bus_matchmaker_disconnected (mm, connection);
2909 bcd_drop_monitor_rules (BusConnectionData *d,
2910 DBusConnection *connection)
2912 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2915 bus_matchmaker_disconnected (mm, connection);
2919 bus_connection_be_monitor (DBusConnection *connection,
2920 BusTransaction *transaction,
2924 BusConnectionData *d;
2929 d = BUS_CONNECTION_DATA (connection);
2930 _dbus_assert (d != NULL);
2932 link = _dbus_list_alloc_link (connection);
2936 BUS_SET_OOM (error);
2940 if (!bcd_add_monitor_rules (d, connection, rules))
2942 _dbus_list_free_link (link);
2943 BUS_SET_OOM (error);
2947 /* release all its names */
2948 if (!_dbus_list_copy (&d->services_owned, &tmp))
2950 bcd_drop_monitor_rules (d, connection);
2951 _dbus_list_free_link (link);
2952 BUS_SET_OOM (error);
2956 for (iter = _dbus_list_get_first_link (&tmp);
2958 iter = _dbus_list_get_next_link (&tmp, iter))
2960 BusService *service = iter->data;
2962 /* This call is transactional: if there isn't enough memory to
2963 * do everything, then the service gets all its names back when
2964 * the transaction is cancelled due to OOM. */
2965 if (!bus_service_remove_owner (service, connection, transaction, error))
2967 bcd_drop_monitor_rules (d, connection);
2968 _dbus_list_free_link (link);
2969 _dbus_list_clear (&tmp);
2974 /* We have now done everything that can fail, so there is no problem
2975 * with doing the irrevocable stuff. */
2977 _dbus_list_clear (&tmp);
2979 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2980 "Connection %s (%s) became a monitor.", d->name,
2981 d->cached_loginfo_string);
2983 if (d->n_match_rules > 0)
2987 mm = bus_context_get_matchmaker (d->connections->context);
2988 bus_matchmaker_disconnected (mm, connection);
2991 /* flag it as a monitor */
2992 d->link_in_monitors = link;
2993 _dbus_list_append_link (&d->connections->monitors, link);
2995 /* it isn't allowed to reply, and it is no longer relevant whether it
2996 * receives replies */
2997 bus_connection_drop_pending_replies (d->connections, connection);