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_disable (connections->expire_timeout);
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_restart (d->pending_unix_fds_timeout,
673 bus_context_get_pending_fd_timeout (d->connections->context));
676 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
678 _dbus_timeout_disable (d->pending_unix_fds_timeout);
682 d->n_pending_unix_fds = n_pending_unix_fds_new;
686 pending_unix_fds_timeout_cb (void *data)
688 DBusConnection *connection = data;
689 dbus_connection_close (connection);
694 bus_connections_setup_connection (BusConnections *connections,
695 DBusConnection *connection)
698 BusConnectionData *d;
703 d = dbus_new0 (BusConnectionData, 1);
708 d->connections = connections;
709 d->connection = connection;
711 _dbus_get_monotonic_time (&d->connection_tv_sec,
712 &d->connection_tv_usec);
714 _dbus_assert (connection_data_slot >= 0);
716 if (!dbus_connection_set_data (connection,
717 connection_data_slot,
718 d, free_connection_data))
724 dbus_connection_set_route_peer_messages (connection, TRUE);
728 dbus_error_init (&error);
729 d->selinux_id = bus_selinux_init_connection_id (connection,
731 if (dbus_error_is_set (&error))
733 /* This is a bit bogus because we pretend all errors
734 * are OOM; this is done because we know that in bus.c
735 * an OOM error disconnects the connection, which is
736 * the same thing we want on any other error.
738 dbus_error_free (&error);
742 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
744 if (dbus_error_is_set (&error))
746 /* This is a bit bogus because we pretend all errors
747 * are OOM; this is done because we know that in bus.c
748 * an OOM error disconnects the connection, which is
749 * the same thing we want on any other error.
751 dbus_error_free (&error);
755 if (!dbus_connection_set_watch_functions (connection,
756 add_connection_watch,
757 remove_connection_watch,
758 toggle_connection_watch,
763 if (!dbus_connection_set_timeout_functions (connection,
764 add_connection_timeout,
765 remove_connection_timeout,
770 /* For now we don't need to set a Windows user function because
771 * there are no policies in the config file controlling what
772 * Windows users can connect. The default 'same user that owns the
773 * bus can connect' behavior of DBusConnection is fine on Windows.
775 dbus_connection_set_unix_user_function (connection,
776 allow_unix_user_function,
779 dbus_connection_set_dispatch_status_function (connection,
780 dispatch_status_function,
781 bus_context_get_loop (connections->context),
784 d->link_in_connection_list = _dbus_list_alloc_link (connection);
785 if (d->link_in_connection_list == NULL)
788 /* Setup the connection with the dispatcher */
789 if (!bus_dispatch_add_connection (connection))
792 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
794 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
796 bus_dispatch_remove_connection (connection);
801 /* Setup pending fds timeout (see #80559) */
802 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
803 pending_unix_fds_timeout_cb,
805 if (d->pending_unix_fds_timeout == NULL)
808 _dbus_timeout_disable (d->pending_unix_fds_timeout);
809 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
810 d->pending_unix_fds_timeout))
813 _dbus_connection_set_pending_fds_function (connection,
814 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
817 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
818 connections->n_incomplete += 1;
820 dbus_connection_ref (connection);
822 bus_connections_expire_incomplete (connections);
824 /* The listening socket is removed from the main loop,
825 * i.e. does not accept(), while n_incomplete is at its
826 * maximum value; so we shouldn't get here in that case */
827 _dbus_assert (connections->n_incomplete <=
828 bus_context_get_max_incomplete_connections (connections->context));
830 /* If we have the maximum number of incomplete connections,
831 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
832 bus_context_check_all_watches (d->connections->context);
840 bus_selinux_id_unref (d->selinux_id);
841 d->selinux_id = NULL;
843 if (d->apparmor_confinement)
844 bus_apparmor_confinement_unref (d->apparmor_confinement);
845 d->apparmor_confinement = NULL;
847 if (!dbus_connection_set_watch_functions (connection,
851 _dbus_assert_not_reached ("setting watch functions to NULL failed");
853 if (!dbus_connection_set_timeout_functions (connection,
857 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
859 dbus_connection_set_unix_user_function (connection,
862 dbus_connection_set_windows_user_function (connection,
865 dbus_connection_set_dispatch_status_function (connection,
868 if (d->pending_unix_fds_timeout)
869 _dbus_timeout_unref (d->pending_unix_fds_timeout);
871 d->pending_unix_fds_timeout = NULL;
873 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
875 if (d->link_in_connection_list != NULL)
877 _dbus_assert (d->link_in_connection_list->next == NULL);
878 _dbus_assert (d->link_in_connection_list->prev == NULL);
879 _dbus_list_free_link (d->link_in_connection_list);
880 d->link_in_connection_list = NULL;
883 if (!dbus_connection_set_data (connection,
884 connection_data_slot,
886 _dbus_assert_not_reached ("failed to set connection data to null");
888 /* "d" has now been freed */
895 bus_connections_expire_incomplete (BusConnections *connections)
901 if (connections->incomplete != NULL)
903 long tv_sec, tv_usec;
907 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
908 auth_timeout = bus_context_get_auth_timeout (connections->context);
910 link = _dbus_list_get_first_link (&connections->incomplete);
913 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
914 DBusConnection *connection;
915 BusConnectionData *d;
918 connection = link->data;
920 d = BUS_CONNECTION_DATA (connection);
922 _dbus_assert (d != NULL);
924 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
925 d->connection_tv_usec,
928 if (elapsed >= (double) auth_timeout)
930 /* Unfortunately, we can't identify the connection: it doesn't
931 * have a unique name yet, we don't know its uid/pid yet,
933 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
934 "Connection has not authenticated soon enough, closing it "
935 "(auth_timeout=%dms, elapsed: %.0fms)",
936 auth_timeout, elapsed);
938 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
939 dbus_connection_close (connection);
943 /* We can end the loop, since the connections are in oldest-first order */
944 next_interval = ((double)auth_timeout) - elapsed;
945 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
946 connection, next_interval);
955 bus_expire_timeout_set_interval (connections->expire_timeout,
960 expire_incomplete_timeout (void *data)
962 BusConnections *connections = data;
964 _dbus_verbose ("Running\n");
966 /* note that this may remove the timeout */
967 bus_connections_expire_incomplete (connections);
973 bus_connection_get_unix_groups (DBusConnection *connection,
974 unsigned long **groups,
983 if (dbus_connection_get_unix_user (connection, &uid))
985 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
987 _dbus_verbose ("Did not get any groups for UID %lu\n",
993 _dbus_verbose ("Got %d groups for UID %lu\n",
999 return TRUE; /* successfully got 0 groups */
1003 bus_connection_is_in_unix_group (DBusConnection *connection,
1007 unsigned long *group_ids;
1010 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1015 while (i < n_group_ids)
1017 if (group_ids[i] == gid)
1019 dbus_free (group_ids);
1025 dbus_free (group_ids);
1030 bus_connection_get_loginfo (DBusConnection *connection)
1032 BusConnectionData *d;
1034 d = BUS_CONNECTION_DATA (connection);
1035 _dbus_assert(d != NULL);
1037 if (!bus_connection_is_active (connection))
1039 return d->cached_loginfo_string;
1043 bus_connection_get_policy (DBusConnection *connection)
1045 BusConnectionData *d;
1047 d = BUS_CONNECTION_DATA (connection);
1049 _dbus_assert (d != NULL);
1050 _dbus_assert (d->policy != NULL);
1055 #ifdef DBUS_ENABLE_CYNARA
1056 const char *bus_connection_get_cynara_session_id (DBusConnection *connection)
1058 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
1059 _dbus_assert (d != NULL);
1061 if (d->cynara_session_id == NULL)
1064 if (dbus_connection_get_unix_process_id(connection, &pid))
1065 d->cynara_session_id = cynara_session_from_pid(pid);
1067 /* If client exits as soon as async call, cynara_session_from_pid() returns null.
1068 cynara_session_from_pid checks /proc/pid to verify process */
1069 if (d->cynara_session_id == NULL)
1070 asprintf (&d->cynara_session_id, "/proc/%ld", pid);
1072 return d->cynara_session_id;
1077 foreach_active (BusConnections *connections,
1078 BusConnectionForeachFunction function,
1083 link = _dbus_list_get_first_link (&connections->completed);
1084 while (link != NULL)
1086 DBusConnection *connection = link->data;
1087 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1089 if (!(* function) (connection, data))
1099 foreach_inactive (BusConnections *connections,
1100 BusConnectionForeachFunction function,
1105 link = _dbus_list_get_first_link (&connections->incomplete);
1106 while (link != NULL)
1108 DBusConnection *connection = link->data;
1109 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1111 if (!(* function) (connection, data))
1121 * Calls function on each active connection; if the function returns
1122 * #FALSE, stops iterating. Active connections are authenticated
1123 * and have sent a Hello message.
1125 * @param connections the connections object
1126 * @param function the function
1127 * @param data data to pass to it as a second arg
1130 bus_connections_foreach_active (BusConnections *connections,
1131 BusConnectionForeachFunction function,
1134 foreach_active (connections, function, data);
1138 * Calls function on each connection; if the function returns
1139 * #FALSE, stops iterating.
1141 * @param connections the connections object
1142 * @param function the function
1143 * @param data data to pass to it as a second arg
1146 bus_connections_foreach (BusConnections *connections,
1147 BusConnectionForeachFunction function,
1150 if (!foreach_active (connections, function, data))
1153 foreach_inactive (connections, function, data);
1157 bus_connections_get_context (BusConnections *connections)
1159 return connections->context;
1163 * This is used to avoid covering the same connection twice when
1164 * traversing connections. Note that it assumes we will
1165 * bus_connection_mark_stamp() each connection at least once per
1166 * INT_MAX increments of the global stamp, or wraparound would break
1170 bus_connections_increment_stamp (BusConnections *connections)
1172 connections->stamp += 1;
1175 /* Mark connection with current stamp, return TRUE if it
1176 * didn't already have that stamp
1179 bus_connection_mark_stamp (DBusConnection *connection)
1181 BusConnectionData *d;
1183 d = BUS_CONNECTION_DATA (connection);
1185 _dbus_assert (d != NULL);
1187 if (d->stamp == d->connections->stamp)
1191 d->stamp = d->connections->stamp;
1197 bus_connection_get_context (DBusConnection *connection)
1199 BusConnectionData *d;
1201 d = BUS_CONNECTION_DATA (connection);
1203 _dbus_assert (d != NULL);
1205 return d->connections->context;
1209 bus_connection_get_connections (DBusConnection *connection)
1211 BusConnectionData *d;
1213 d = BUS_CONNECTION_DATA (connection);
1215 _dbus_assert (d != NULL);
1217 return d->connections;
1221 bus_connection_get_registry (DBusConnection *connection)
1223 BusConnectionData *d;
1225 d = BUS_CONNECTION_DATA (connection);
1227 _dbus_assert (d != NULL);
1229 return bus_context_get_registry (d->connections->context);
1233 bus_connection_get_activation (DBusConnection *connection)
1235 BusConnectionData *d;
1237 d = BUS_CONNECTION_DATA (connection);
1239 _dbus_assert (d != NULL);
1241 return bus_context_get_activation (d->connections->context);
1245 bus_connection_get_matchmaker (DBusConnection *connection)
1247 BusConnectionData *d;
1249 d = BUS_CONNECTION_DATA (connection);
1251 _dbus_assert (d != NULL);
1253 return bus_context_get_matchmaker (d->connections->context);
1257 bus_connection_get_selinux_id (DBusConnection *connection)
1259 BusConnectionData *d;
1261 d = BUS_CONNECTION_DATA (connection);
1263 _dbus_assert (d != NULL);
1265 return d->selinux_id;
1268 BusAppArmorConfinement*
1269 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1271 BusConnectionData *d;
1273 d = BUS_CONNECTION_DATA (connection);
1275 _dbus_assert (d != NULL);
1277 bus_apparmor_confinement_ref (d->apparmor_confinement);
1278 return d->apparmor_confinement;
1282 * Checks whether the connection is registered with the message bus.
1284 * @param connection the connection
1285 * @returns #TRUE if we're an active message bus participant
1288 bus_connection_is_active (DBusConnection *connection)
1290 BusConnectionData *d;
1292 d = BUS_CONNECTION_DATA (connection);
1293 _dbus_assert(d != NULL);
1295 return d->name != NULL;
1299 bus_connection_preallocate_oom_error (DBusConnection *connection)
1301 DBusMessage *message;
1302 DBusPreallocatedSend *preallocated;
1303 BusConnectionData *d;
1305 d = BUS_CONNECTION_DATA (connection);
1307 _dbus_assert (d != NULL);
1309 if (d->oom_preallocated != NULL)
1312 preallocated = dbus_connection_preallocate_send (connection);
1313 if (preallocated == NULL)
1316 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1318 if (message == NULL)
1320 dbus_connection_free_preallocated_send (connection, preallocated);
1324 /* d->name may be NULL, but that is OK */
1325 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1326 !dbus_message_set_destination (message, d->name) ||
1327 !dbus_message_set_sender (message,
1330 dbus_connection_free_preallocated_send (connection, preallocated);
1331 dbus_message_unref (message);
1335 /* set reply serial to placeholder value just so space is already allocated
1338 if (!dbus_message_set_reply_serial (message, 14))
1340 dbus_connection_free_preallocated_send (connection, preallocated);
1341 dbus_message_unref (message);
1345 d->oom_message = message;
1346 d->oom_preallocated = preallocated;
1352 bus_connection_send_oom_error (DBusConnection *connection,
1353 DBusMessage *in_reply_to)
1355 BusConnectionData *d;
1357 d = BUS_CONNECTION_DATA (connection);
1359 _dbus_assert (d != NULL);
1360 _dbus_assert (d->oom_message != NULL);
1362 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1363 "dbus-daemon transaction failed (OOM), sending error to "
1364 "sender %s", bus_connection_get_loginfo (connection));
1366 /* should always succeed since we set it to a placeholder earlier */
1367 if (!dbus_message_set_reply_serial (d->oom_message,
1368 dbus_message_get_serial (in_reply_to)))
1369 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1371 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1373 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1374 d->oom_message, NULL);
1376 dbus_message_unref (d->oom_message);
1377 d->oom_message = NULL;
1378 d->oom_preallocated = NULL;
1381 #ifdef DBUS_ENABLE_STATS
1383 update_peak (int *peak,
1392 bus_connection_add_match_rule_link (DBusConnection *connection,
1395 BusConnectionData *d;
1397 d = BUS_CONNECTION_DATA (connection);
1398 _dbus_assert (d != NULL);
1400 _dbus_list_append_link (&d->match_rules, link);
1402 d->n_match_rules += 1;
1404 #ifdef DBUS_ENABLE_STATS
1405 update_peak (&d->peak_match_rules, d->n_match_rules);
1406 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1408 d->connections->total_match_rules += 1;
1409 update_peak (&d->connections->peak_match_rules,
1410 d->connections->total_match_rules);
1415 bus_connection_add_match_rule (DBusConnection *connection,
1420 link = _dbus_list_alloc_link (rule);
1425 bus_connection_add_match_rule_link (connection, link);
1431 bus_connection_remove_match_rule (DBusConnection *connection,
1434 BusConnectionData *d;
1436 d = BUS_CONNECTION_DATA (connection);
1437 _dbus_assert (d != NULL);
1439 _dbus_list_remove_last (&d->match_rules, rule);
1441 d->n_match_rules -= 1;
1442 _dbus_assert (d->n_match_rules >= 0);
1444 #ifdef DBUS_ENABLE_STATS
1445 d->connections->total_match_rules -= 1;
1450 bus_connection_get_n_match_rules (DBusConnection *connection)
1452 BusConnectionData *d;
1454 d = BUS_CONNECTION_DATA (connection);
1455 _dbus_assert (d != NULL);
1457 return d->n_match_rules;
1461 bus_connection_add_owned_service_link (DBusConnection *connection,
1464 BusConnectionData *d;
1466 d = BUS_CONNECTION_DATA (connection);
1467 _dbus_assert (d != NULL);
1469 _dbus_list_append_link (&d->services_owned, link);
1471 d->n_services_owned += 1;
1473 #ifdef DBUS_ENABLE_STATS
1474 update_peak (&d->peak_bus_names, d->n_services_owned);
1475 update_peak (&d->connections->peak_bus_names_per_conn,
1476 d->n_services_owned);
1478 d->connections->total_bus_names += 1;
1479 update_peak (&d->connections->peak_bus_names,
1480 d->connections->total_bus_names);
1485 bus_connection_add_owned_service (DBusConnection *connection,
1486 BusService *service)
1490 link = _dbus_list_alloc_link (service);
1495 bus_connection_add_owned_service_link (connection, link);
1501 bus_connection_remove_owned_service (DBusConnection *connection,
1502 BusService *service)
1504 BusConnectionData *d;
1506 d = BUS_CONNECTION_DATA (connection);
1507 _dbus_assert (d != NULL);
1509 _dbus_list_remove_last (&d->services_owned, service);
1511 d->n_services_owned -= 1;
1512 _dbus_assert (d->n_services_owned >= 0);
1514 #ifdef DBUS_ENABLE_STATS
1515 d->connections->total_bus_names -= 1;
1520 bus_connection_get_n_services_owned (DBusConnection *connection)
1522 BusConnectionData *d;
1524 d = BUS_CONNECTION_DATA (connection);
1525 _dbus_assert (d != NULL);
1527 return d->n_services_owned;
1531 bus_connection_complete (DBusConnection *connection,
1532 const DBusString *name,
1535 BusConnectionData *d;
1538 d = BUS_CONNECTION_DATA (connection);
1539 _dbus_assert (d != NULL);
1540 _dbus_assert (d->name == NULL);
1541 _dbus_assert (d->policy == NULL);
1543 _dbus_assert (!bus_connection_is_active (connection));
1545 if (!_dbus_string_copy_data (name, &d->name))
1547 BUS_SET_OOM (error);
1551 _dbus_assert (d->name != NULL);
1553 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1555 d->policy = bus_context_create_client_policy (d->connections->context,
1559 /* we may have a NULL policy on OOM or error getting list of
1560 * groups for a user. In the latter case we don't handle it so
1561 * well currently, as it will just keep failing over and over.
1564 if (d->policy == NULL)
1566 _dbus_verbose ("Failed to create security policy for connection %p\n",
1568 _DBUS_ASSERT_ERROR_IS_SET (error);
1569 dbus_free (d->name);
1574 if (dbus_connection_get_unix_user (connection, &uid))
1576 if (!adjust_connections_for_uid (d->connections,
1581 /* Create and cache a string which holds information about the
1582 * peer process; used for logging purposes.
1584 if (!cache_peer_loginfo_string (d, connection))
1587 /* Now the connection is active, move it between lists */
1588 _dbus_list_unlink (&d->connections->incomplete,
1589 d->link_in_connection_list);
1590 d->connections->n_incomplete -= 1;
1591 _dbus_list_append_link (&d->connections->completed,
1592 d->link_in_connection_list);
1593 d->connections->n_completed += 1;
1595 _dbus_assert (d->connections->n_incomplete >= 0);
1596 _dbus_assert (d->connections->n_completed > 0);
1598 /* If we have dropped below the max. number of incomplete
1599 * connections, start accept()ing again */
1600 bus_context_check_all_watches (d->connections->context);
1602 /* See if we can remove the timeout */
1603 bus_connections_expire_incomplete (d->connections);
1605 _dbus_assert (bus_connection_is_active (connection));
1609 BUS_SET_OOM (error);
1610 dbus_free (d->name);
1613 bus_client_policy_unref (d->policy);
1619 bus_connections_reload_policy (BusConnections *connections,
1622 BusConnectionData *d;
1623 DBusConnection *connection;
1626 _dbus_assert (connections != NULL);
1627 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1629 for (link = _dbus_list_get_first_link (&(connections->completed));
1631 link = _dbus_list_get_next_link (&(connections->completed), link))
1633 connection = link->data;
1634 d = BUS_CONNECTION_DATA (connection);
1635 _dbus_assert (d != NULL);
1636 _dbus_assert (d->policy != NULL);
1638 bus_client_policy_unref (d->policy);
1639 d->policy = bus_context_create_client_policy (connections->context,
1642 if (d->policy == NULL)
1644 _dbus_verbose ("Failed to create security policy for connection %p\n",
1646 _DBUS_ASSERT_ERROR_IS_SET (error);
1655 bus_connection_get_name (DBusConnection *connection)
1657 BusConnectionData *d;
1659 d = BUS_CONNECTION_DATA (connection);
1660 _dbus_assert (d != NULL);
1666 * Check whether completing the passed-in connection would
1667 * exceed limits, and if so set error and return #FALSE
1670 bus_connections_check_limits (BusConnections *connections,
1671 DBusConnection *requesting_completion,
1676 if (connections->n_completed >=
1677 bus_context_get_max_completed_connections (connections->context))
1679 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1680 "The maximum number of active connections has been reached");
1684 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1686 if (get_connections_for_uid (connections, uid) >=
1687 bus_context_get_max_connections_per_user (connections->context))
1689 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1690 "The maximum number of active connections for UID %lu has been reached",
1700 bus_pending_reply_free (BusPendingReply *pending)
1702 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1704 pending->will_send_reply,
1705 pending->will_get_reply,
1706 pending->reply_serial);
1708 dbus_free (pending);
1712 bus_pending_reply_send_no_reply (BusConnections *connections,
1713 BusTransaction *transaction,
1714 BusPendingReply *pending)
1716 DBusMessage *message;
1717 DBusMessageIter iter;
1723 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1724 if (message == NULL)
1727 dbus_message_set_no_reply (message, TRUE);
1729 if (!dbus_message_set_reply_serial (message,
1730 pending->reply_serial))
1733 if (!dbus_message_set_error_name (message,
1734 DBUS_ERROR_NO_REPLY))
1737 /* If you change these messages, adjust test/dbus-daemon.c to match */
1738 if (pending->will_send_reply == NULL)
1739 errmsg = "Message recipient disconnected from message bus without replying";
1741 errmsg = "Message did not receive a reply (timeout by message bus)";
1743 dbus_message_iter_init_append (message, &iter);
1744 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1747 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1754 dbus_message_unref (message);
1759 bus_pending_reply_expired (BusExpireList *list,
1763 BusPendingReply *pending = link->data;
1764 BusConnections *connections = data;
1765 BusTransaction *transaction;
1767 /* No reply is forthcoming. So nuke it if we can. If not,
1768 * leave it in the list to try expiring again later when we
1772 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1774 pending->will_send_reply,
1775 pending->will_get_reply,
1776 pending->reply_serial);
1778 transaction = bus_transaction_new (connections->context);
1779 if (transaction == NULL)
1782 if (!bus_pending_reply_send_no_reply (connections,
1786 bus_transaction_cancel_and_free (transaction);
1790 bus_expire_list_remove_link (connections->pending_replies, link);
1792 bus_pending_reply_free (pending);
1793 bus_transaction_execute_and_free (transaction);
1799 bus_connection_drop_pending_replies (BusConnections *connections,
1800 DBusConnection *connection)
1802 /* The DBusConnection is almost 100% finalized here, so you can't
1803 * do anything with it except check for pointer equality
1807 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1810 link = bus_expire_list_get_first_link (connections->pending_replies);
1811 while (link != NULL)
1814 BusPendingReply *pending;
1816 next = bus_expire_list_get_next_link (connections->pending_replies,
1818 pending = link->data;
1820 if (pending->will_get_reply == connection)
1822 /* We don't need to track this pending reply anymore */
1824 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1826 pending->will_send_reply,
1827 pending->will_get_reply,
1828 pending->reply_serial);
1830 bus_expire_list_remove_link (connections->pending_replies,
1832 bus_pending_reply_free (pending);
1834 else if (pending->will_send_reply == connection)
1836 /* The reply isn't going to be sent, so set things
1837 * up so it will be expired right away
1839 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1841 pending->will_send_reply,
1842 pending->will_get_reply,
1843 pending->reply_serial);
1845 pending->will_send_reply = NULL;
1846 pending->expire_item.added_tv_sec = 0;
1847 pending->expire_item.added_tv_usec = 0;
1849 bus_expire_list_recheck_immediately (connections->pending_replies);
1859 BusPendingReply *pending;
1860 BusConnections *connections;
1861 } CancelPendingReplyData;
1864 cancel_pending_reply (void *data)
1866 CancelPendingReplyData *d = data;
1868 _dbus_verbose ("d = %p\n", d);
1870 if (!bus_expire_list_remove (d->connections->pending_replies,
1871 &d->pending->expire_item))
1872 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1874 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1878 cancel_pending_reply_data_free (void *data)
1880 CancelPendingReplyData *d = data;
1882 _dbus_verbose ("d = %p\n", d);
1884 /* d->pending should be either freed or still
1885 * in the list of pending replies (owned by someone
1893 * Record that a reply is allowed; return TRUE on success.
1896 bus_connections_expect_reply (BusConnections *connections,
1897 BusTransaction *transaction,
1898 DBusConnection *will_get_reply,
1899 DBusConnection *will_send_reply,
1900 DBusMessage *reply_to_this,
1903 BusPendingReply *pending;
1904 dbus_uint32_t reply_serial;
1906 CancelPendingReplyData *cprd;
1909 _dbus_assert (will_get_reply != NULL);
1910 _dbus_assert (will_send_reply != NULL);
1911 _dbus_assert (reply_to_this != NULL);
1913 if (dbus_message_get_no_reply (reply_to_this))
1914 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1916 reply_serial = dbus_message_get_serial (reply_to_this);
1918 link = bus_expire_list_get_first_link (connections->pending_replies);
1920 while (link != NULL)
1922 pending = link->data;
1924 if (pending->reply_serial == reply_serial &&
1925 pending->will_get_reply == will_get_reply &&
1926 pending->will_send_reply == will_send_reply)
1928 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1929 "Message has the same reply serial as a currently-outstanding existing method call");
1933 link = bus_expire_list_get_next_link (connections->pending_replies,
1935 if (pending->will_get_reply == will_get_reply)
1940 bus_context_get_max_replies_per_connection (connections->context))
1942 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1943 "The maximum number of pending replies per connection has been reached");
1947 pending = dbus_new0 (BusPendingReply, 1);
1948 if (pending == NULL)
1950 BUS_SET_OOM (error);
1954 #ifdef DBUS_ENABLE_VERBOSE_MODE
1955 /* so we can see a not-yet-added pending reply */
1956 pending->expire_item.added_tv_sec = 1;
1957 pending->expire_item.added_tv_usec = 1;
1960 pending->will_get_reply = will_get_reply;
1961 pending->will_send_reply = will_send_reply;
1962 pending->reply_serial = reply_serial;
1964 cprd = dbus_new0 (CancelPendingReplyData, 1);
1967 BUS_SET_OOM (error);
1968 bus_pending_reply_free (pending);
1972 if (!bus_expire_list_add (connections->pending_replies,
1973 &pending->expire_item))
1975 BUS_SET_OOM (error);
1977 bus_pending_reply_free (pending);
1981 if (!bus_transaction_add_cancel_hook (transaction,
1982 cancel_pending_reply,
1984 cancel_pending_reply_data_free))
1986 BUS_SET_OOM (error);
1987 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1989 bus_pending_reply_free (pending);
1993 cprd->pending = pending;
1994 cprd->connections = connections;
1996 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1997 &pending->expire_item.added_tv_usec);
1999 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2001 pending->will_send_reply,
2002 pending->will_get_reply,
2003 pending->reply_serial);
2011 BusConnections *connections;
2012 } CheckPendingReplyData;
2015 cancel_check_pending_reply (void *data)
2017 CheckPendingReplyData *d = data;
2019 _dbus_verbose ("d = %p\n",d);
2021 bus_expire_list_add_link (d->connections->pending_replies,
2027 check_pending_reply_data_free (void *data)
2029 CheckPendingReplyData *d = data;
2031 _dbus_verbose ("d = %p\n",d);
2033 if (d->link != NULL)
2035 BusPendingReply *pending = d->link->data;
2037 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2038 &pending->expire_item));
2040 bus_pending_reply_free (pending);
2041 _dbus_list_free_link (d->link);
2048 * Check whether a reply is allowed, remove BusPendingReply
2049 * if so, return TRUE if so.
2052 bus_connections_check_reply (BusConnections *connections,
2053 BusTransaction *transaction,
2054 DBusConnection *sending_reply,
2055 DBusConnection *receiving_reply,
2059 CheckPendingReplyData *cprd;
2061 dbus_uint32_t reply_serial;
2063 _dbus_assert (sending_reply != NULL);
2064 _dbus_assert (receiving_reply != NULL);
2066 reply_serial = dbus_message_get_reply_serial (reply);
2068 link = bus_expire_list_get_first_link (connections->pending_replies);
2069 while (link != NULL)
2071 BusPendingReply *pending = link->data;
2073 if (pending->reply_serial == reply_serial &&
2074 pending->will_get_reply == receiving_reply &&
2075 pending->will_send_reply == sending_reply)
2077 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2081 link = bus_expire_list_get_next_link (connections->pending_replies,
2087 _dbus_verbose ("No pending reply expected\n");
2092 cprd = dbus_new0 (CheckPendingReplyData, 1);
2095 BUS_SET_OOM (error);
2099 if (!bus_transaction_add_cancel_hook (transaction,
2100 cancel_check_pending_reply,
2102 check_pending_reply_data_free))
2104 BUS_SET_OOM (error);
2110 cprd->connections = connections;
2112 bus_expire_list_unlink (connections->pending_replies,
2115 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2123 * Note that this is fairly fragile; in particular, don't try to use
2124 * one transaction across any main loop iterations.
2129 BusTransaction *transaction;
2130 DBusMessage *message;
2131 DBusPreallocatedSend *preallocated;
2136 BusTransactionCancelFunction cancel_function;
2137 DBusFreeFunction free_data_function;
2141 struct BusTransaction
2143 DBusList *connections;
2144 BusContext *context;
2145 DBusList *cancel_hooks;
2149 message_to_send_free (DBusConnection *connection,
2150 MessageToSend *to_send)
2152 if (to_send->message)
2153 dbus_message_unref (to_send->message);
2155 if (to_send->preallocated)
2156 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2158 dbus_free (to_send);
2162 cancel_hook_cancel (void *element,
2165 CancelHook *ch = element;
2167 _dbus_verbose ("Running transaction cancel hook\n");
2169 if (ch->cancel_function)
2170 (* ch->cancel_function) (ch->data);
2174 cancel_hook_free (void *element,
2177 CancelHook *ch = element;
2179 if (ch->free_data_function)
2180 (* ch->free_data_function) (ch->data);
2186 free_cancel_hooks (BusTransaction *transaction)
2188 _dbus_list_foreach (&transaction->cancel_hooks,
2189 cancel_hook_free, NULL);
2191 _dbus_list_clear (&transaction->cancel_hooks);
2195 bus_transaction_new (BusContext *context)
2197 BusTransaction *transaction;
2199 transaction = dbus_new0 (BusTransaction, 1);
2200 if (transaction == NULL)
2203 transaction->context = context;
2209 bus_transaction_get_context (BusTransaction *transaction)
2211 return transaction->context;
2215 * Reserve enough memory to capture the given message if the
2216 * transaction goes through.
2219 bus_transaction_capture (BusTransaction *transaction,
2220 DBusConnection *sender,
2221 DBusMessage *message)
2223 BusConnections *connections;
2226 DBusList *recipients = NULL;
2227 dbus_bool_t ret = FALSE;
2229 connections = bus_context_get_connections (transaction->context);
2231 /* shortcut: don't compose the message unless someone wants it */
2232 if (connections->monitors == NULL)
2235 mm = connections->monitor_matchmaker;
2236 /* This is non-null if there has ever been a monitor - we don't GC it.
2237 * There's little point, since there is up to 1 per process. */
2238 _dbus_assert (mm != NULL);
2240 if (!bus_matchmaker_get_recipients (mm, connections, sender, NULL, message,
2244 for (link = _dbus_list_get_first_link (&recipients);
2246 link = _dbus_list_get_next_link (&recipients, link))
2248 DBusConnection *recipient = link->data;
2250 if (!bus_transaction_send (transaction, recipient, message, FALSE))
2257 _dbus_list_clear (&recipients);
2262 bus_transaction_capture_error_reply (BusTransaction *transaction,
2263 const DBusError *error,
2264 DBusMessage *in_reply_to)
2266 BusConnections *connections;
2268 dbus_bool_t ret = FALSE;
2270 _dbus_assert (error != NULL);
2271 _DBUS_ASSERT_ERROR_IS_SET (error);
2273 connections = bus_context_get_connections (transaction->context);
2275 /* shortcut: don't compose the message unless someone wants it */
2276 if (connections->monitors == NULL)
2279 reply = dbus_message_new_error (in_reply_to,
2286 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2289 ret = bus_transaction_capture (transaction, NULL, reply);
2292 dbus_message_unref (reply);
2297 bus_transaction_send_from_driver (BusTransaction *transaction,
2298 DBusConnection *connection,
2299 DBusMessage *message)
2301 DBusError error = DBUS_ERROR_INIT;
2302 BusDeferredMessage *deferred_message;
2304 /* We have to set the sender to the driver, and have
2305 * to check security policy since it was not done in
2308 _dbus_verbose ("Sending %s %s %s from driver\n",
2309 dbus_message_get_interface (message) ?
2310 dbus_message_get_interface (message) : "(no interface)",
2311 dbus_message_get_member (message) ?
2312 dbus_message_get_member (message) : "(no member)",
2313 dbus_message_get_error_name (message) ?
2314 dbus_message_get_error_name (message) : "(no error name)");
2316 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2319 if (bus_connection_is_active (connection))
2321 if (!dbus_message_set_destination (message,
2322 bus_connection_get_name (connection)))
2326 /* bus driver never wants a reply */
2327 dbus_message_set_no_reply (message, TRUE);
2329 /* Capture it for monitors, even if the real recipient's receive policy
2330 * does not allow it to receive this message from us (which would be odd).
2332 if (!bus_transaction_capture (transaction, NULL, message))
2335 /* If security policy doesn't allow the message, we would silently
2336 * eat it; the driver doesn't care about getting a reply. However,
2337 * if we're actively capturing messages, it's nice to log that we
2338 * tried to send it and did not allow ourselves to do so.
2340 switch (bus_context_check_security_policy (bus_transaction_get_context (transaction),
2342 NULL, connection, connection, message, &error,
2345 case BUS_RESULT_TRUE:
2347 case BUS_RESULT_FALSE:
2348 if (!bus_transaction_capture_error_reply (transaction, &error, message))
2350 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2351 "message from dbus-daemon rejected but not enough "
2352 "memory to capture it");
2355 /* This is not fatal to the transaction so silently eat the disallowed
2356 * message (see reasoning above) */
2357 dbus_error_free (&error);
2360 case BUS_RESULT_LATER:
2361 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
2363 return TRUE; /* pretend to have sent it */
2366 return bus_transaction_send (transaction, connection, message, FALSE);
2370 bus_transaction_send (BusTransaction *transaction,
2371 DBusConnection *connection,
2372 DBusMessage *message,
2373 dbus_bool_t deferred_dispatch)
2375 MessageToSend *to_send;
2376 BusConnectionData *d;
2379 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2380 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2381 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2383 dbus_message_get_interface (message) ?
2384 dbus_message_get_interface (message) : "(unset)",
2385 dbus_message_get_member (message) ?
2386 dbus_message_get_member (message) : "(unset)",
2387 dbus_message_get_error_name (message) ?
2388 dbus_message_get_error_name (message) : "(unset)",
2389 dbus_connection_get_is_connected (connection) ?
2390 "" : " (disconnected)");
2392 _dbus_assert (dbus_message_get_sender (message) != NULL);
2394 if (!dbus_connection_get_is_connected (connection))
2395 return TRUE; /* silently ignore disconnected connections */
2397 d = BUS_CONNECTION_DATA (connection);
2398 _dbus_assert (d != NULL);
2400 if (!deferred_dispatch && d->deferred_messages != NULL)
2402 BusDeferredMessage *deferred_message;
2403 dbus_bool_t success;
2404 /* sender and addressed recipient are not required at this point as we only need to send message
2405 * to a single recipient without performing policy check. */
2406 deferred_message = bus_deferred_message_new (message,
2411 if (deferred_message == NULL)
2414 success = bus_deferred_message_queue_at_recipient(deferred_message, transaction,
2416 bus_deferred_message_unref(deferred_message);
2421 to_send = dbus_new (MessageToSend, 1);
2422 if (to_send == NULL)
2427 to_send->preallocated = dbus_connection_preallocate_send (connection);
2428 if (to_send->preallocated == NULL)
2430 dbus_free (to_send);
2434 dbus_message_ref (message);
2435 to_send->message = message;
2436 to_send->transaction = transaction;
2438 _dbus_verbose ("about to prepend message\n");
2440 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2442 message_to_send_free (connection, to_send);
2446 _dbus_verbose ("prepended message\n");
2448 /* See if we already had this connection in the list
2449 * for this transaction. If we have a pending message,
2450 * then we should already be in transaction->connections
2452 link = _dbus_list_get_first_link (&d->transaction_messages);
2453 _dbus_assert (link->data == to_send);
2454 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2455 while (link != NULL)
2457 MessageToSend *m = link->data;
2458 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2460 if (m->transaction == transaction)
2468 if (!_dbus_list_prepend (&transaction->connections, connection))
2470 _dbus_list_remove (&d->transaction_messages, to_send);
2471 message_to_send_free (connection, to_send);
2480 transaction_free (BusTransaction *transaction)
2482 _dbus_assert (transaction->connections == NULL);
2484 free_cancel_hooks (transaction);
2486 dbus_free (transaction);
2490 connection_cancel_transaction (DBusConnection *connection,
2491 BusTransaction *transaction)
2494 BusConnectionData *d;
2496 d = BUS_CONNECTION_DATA (connection);
2497 _dbus_assert (d != NULL);
2499 link = _dbus_list_get_first_link (&d->transaction_messages);
2500 while (link != NULL)
2502 MessageToSend *m = link->data;
2503 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2505 if (m->transaction == transaction)
2507 _dbus_list_remove_link (&d->transaction_messages,
2510 message_to_send_free (connection, m);
2518 bus_transaction_cancel_and_free (BusTransaction *transaction)
2520 DBusConnection *connection;
2522 _dbus_verbose ("TRANSACTION: cancelled\n");
2524 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2525 connection_cancel_transaction (connection, transaction);
2527 _dbus_list_foreach (&transaction->cancel_hooks,
2528 cancel_hook_cancel, NULL);
2530 transaction_free (transaction);
2534 connection_execute_transaction (DBusConnection *connection,
2535 BusTransaction *transaction)
2538 BusConnectionData *d;
2540 d = BUS_CONNECTION_DATA (connection);
2541 _dbus_assert (d != NULL);
2543 /* Send the queue in order (FIFO) */
2544 link = _dbus_list_get_last_link (&d->transaction_messages);
2545 while (link != NULL)
2547 MessageToSend *m = link->data;
2548 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2550 if (m->transaction == transaction)
2552 _dbus_list_remove_link (&d->transaction_messages,
2555 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2557 dbus_connection_send_preallocated (connection,
2562 m->preallocated = NULL; /* so we don't double-free it */
2564 message_to_send_free (connection, m);
2572 bus_transaction_execute_and_free (BusTransaction *transaction)
2574 /* For each connection in transaction->connections
2577 DBusConnection *connection;
2579 _dbus_verbose ("TRANSACTION: executing\n");
2581 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2582 connection_execute_transaction (connection, transaction);
2584 transaction_free (transaction);
2588 bus_connection_remove_transactions (DBusConnection *connection)
2590 MessageToSend *to_send;
2591 BusConnectionData *d;
2593 d = BUS_CONNECTION_DATA (connection);
2594 _dbus_assert (d != NULL);
2596 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2598 /* only has an effect for the first MessageToSend listing this transaction */
2599 _dbus_list_remove (&to_send->transaction->connections,
2602 _dbus_list_remove (&d->transaction_messages, to_send);
2603 message_to_send_free (connection, to_send);
2608 * Converts the DBusError to a message reply
2611 bus_transaction_send_error_reply (BusTransaction *transaction,
2612 DBusConnection *connection,
2613 const DBusError *error,
2614 DBusMessage *in_reply_to)
2618 _dbus_assert (error != NULL);
2619 _DBUS_ASSERT_ERROR_IS_SET (error);
2621 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2622 error->name, error->message);
2624 reply = dbus_message_new_error (in_reply_to,
2630 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2632 dbus_message_unref (reply);
2636 dbus_message_unref (reply);
2642 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2643 BusTransactionCancelFunction cancel_function,
2645 DBusFreeFunction free_data_function)
2649 ch = dbus_new (CancelHook, 1);
2653 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2654 cancel_function, data);
2656 ch->cancel_function = cancel_function;
2658 ch->free_data_function = free_data_function;
2660 /* It's important that the hooks get run in reverse order that they
2663 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2673 bus_connection_dispatch_deferred (DBusConnection *connection)
2675 BusDeferredMessage *message;
2677 _dbus_return_if_fail (connection != NULL);
2679 while ((message = bus_connection_pop_deferred_message(connection)) != NULL)
2681 bus_deferred_message_dispatch(message);
2682 bus_deferred_message_unref(message);
2687 bus_connection_has_deferred_messages (DBusConnection *connection)
2689 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2690 return d->deferred_messages != NULL ? TRUE : FALSE;
2694 bus_connection_queue_deferred_message (DBusConnection *connection,
2695 BusDeferredMessage *message,
2696 dbus_bool_t prepend)
2698 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2699 dbus_bool_t success;
2701 success = _dbus_list_prepend(&d->deferred_messages, message);
2703 success = _dbus_list_append(&d->deferred_messages, message);
2707 bus_deferred_message_ref(message);
2715 bus_connection_replace_deferred_message (DBusConnection *connection,
2716 BusDeferredMessage *oldMessage,
2717 BusDeferredMessage *newMessage)
2720 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2722 link = _dbus_list_find_first(&d->deferred_messages, oldMessage);
2726 if (!_dbus_list_insert_after(&d->deferred_messages, link, newMessage))
2729 bus_deferred_message_ref(newMessage);
2730 _dbus_list_remove_link(&d->deferred_messages, link);
2731 bus_deferred_message_unref(oldMessage);
2735 BusDeferredMessage *
2736 bus_connection_pop_deferred_message (DBusConnection *connection)
2739 BusDeferredMessage *message;
2740 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2742 link =_dbus_list_get_first_link(&d->deferred_messages);
2745 message = link->data;
2746 if (!bus_deferred_message_waits_for_check(message))
2748 _dbus_list_remove_link(&d->deferred_messages, link);
2757 bus_connection_putback_deferred_message (DBusConnection *connection, BusDeferredMessage *message)
2759 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2760 if (_dbus_list_prepend(&d->deferred_messages, message))
2768 bus_connection_clear_deferred_messages (DBusConnection *connection)
2770 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2773 BusDeferredMessage *message;
2775 link =_dbus_list_get_first_link(&d->deferred_messages);
2776 while (link != NULL)
2778 next = _dbus_list_get_next_link (&d->deferred_messages, link);
2779 message = link->data;
2781 bus_deferred_message_unref(message);
2782 _dbus_list_remove_link(&d->deferred_messages, link);
2789 bus_connection_remove_deferred_message (DBusConnection *connection,
2790 BusDeferredMessage *message)
2792 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2793 if (_dbus_list_remove(&d->deferred_messages, message))
2794 bus_deferred_message_unref(message);
2798 bus_connections_get_n_active (BusConnections *connections)
2800 return connections->n_completed;
2804 bus_connections_get_n_incomplete (BusConnections *connections)
2806 return connections->n_incomplete;
2809 #ifdef DBUS_ENABLE_STATS
2811 bus_connections_get_total_match_rules (BusConnections *connections)
2813 return connections->total_match_rules;
2817 bus_connections_get_peak_match_rules (BusConnections *connections)
2819 return connections->peak_match_rules;
2823 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2825 return connections->peak_match_rules_per_conn;
2829 bus_connections_get_total_bus_names (BusConnections *connections)
2831 return connections->total_bus_names;
2835 bus_connections_get_peak_bus_names (BusConnections *connections)
2837 return connections->peak_bus_names;
2841 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2843 return connections->peak_bus_names_per_conn;
2847 bus_connection_get_peak_match_rules (DBusConnection *connection)
2849 BusConnectionData *d;
2851 d = BUS_CONNECTION_DATA (connection);
2852 _dbus_assert(d != NULL);
2854 return d->peak_match_rules;
2858 bus_connection_get_peak_bus_names (DBusConnection *connection)
2860 BusConnectionData *d;
2862 d = BUS_CONNECTION_DATA (connection);
2863 _dbus_assert(d != NULL);
2865 return d->peak_bus_names;
2867 #endif /* DBUS_ENABLE_STATS */
2870 bus_connection_is_monitor (DBusConnection *connection)
2872 BusConnectionData *d;
2874 d = BUS_CONNECTION_DATA (connection);
2875 _dbus_assert(d != NULL);
2877 return d->link_in_monitors != NULL;
2881 bcd_add_monitor_rules (BusConnectionData *d,
2882 DBusConnection *connection,
2885 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2890 mm = bus_matchmaker_new ();
2895 d->connections->monitor_matchmaker = mm;
2898 for (iter = _dbus_list_get_first_link (rules);
2900 iter = _dbus_list_get_next_link (rules, iter))
2902 if (!bus_matchmaker_add_rule (mm, iter->data))
2904 bus_matchmaker_disconnected (mm, connection);
2913 bcd_drop_monitor_rules (BusConnectionData *d,
2914 DBusConnection *connection)
2916 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2919 bus_matchmaker_disconnected (mm, connection);
2923 bus_connection_be_monitor (DBusConnection *connection,
2924 BusTransaction *transaction,
2928 BusConnectionData *d;
2933 d = BUS_CONNECTION_DATA (connection);
2934 _dbus_assert (d != NULL);
2936 link = _dbus_list_alloc_link (connection);
2940 BUS_SET_OOM (error);
2944 if (!bcd_add_monitor_rules (d, connection, rules))
2946 _dbus_list_free_link (link);
2947 BUS_SET_OOM (error);
2951 /* release all its names */
2952 if (!_dbus_list_copy (&d->services_owned, &tmp))
2954 bcd_drop_monitor_rules (d, connection);
2955 _dbus_list_free_link (link);
2956 BUS_SET_OOM (error);
2960 for (iter = _dbus_list_get_first_link (&tmp);
2962 iter = _dbus_list_get_next_link (&tmp, iter))
2964 BusService *service = iter->data;
2966 /* This call is transactional: if there isn't enough memory to
2967 * do everything, then the service gets all its names back when
2968 * the transaction is cancelled due to OOM. */
2969 if (!bus_service_remove_owner (service, connection, transaction, error))
2971 bcd_drop_monitor_rules (d, connection);
2972 _dbus_list_free_link (link);
2973 _dbus_list_clear (&tmp);
2978 /* We have now done everything that can fail, so there is no problem
2979 * with doing the irrevocable stuff. */
2981 _dbus_list_clear (&tmp);
2983 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2984 "Connection %s (%s) became a monitor.", d->name,
2985 d->cached_loginfo_string);
2987 if (d->n_match_rules > 0)
2991 mm = bus_context_get_matchmaker (d->connections->context);
2992 bus_matchmaker_disconnected (mm, connection);
2995 /* flag it as a monitor */
2996 d->link_in_monitors = link;
2997 _dbus_list_append_link (&d->connections->monitors, link);
2999 /* it isn't allowed to reply, and it is no longer relevant whether it
3000 * receives replies */
3001 bus_connection_drop_pending_replies (d->connections, connection);