1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "connection.h"
31 #include "expirelist.h"
34 #include <dbus/dbus-list.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-timeout.h>
37 #include <dbus/dbus-connection-internal.h>
38 #include <dbus/dbus-internals.h>
40 /* Trim executed commands to this length; we want to keep logs readable */
41 #define MAX_LOG_COMMAND_LEN 50
43 static void bus_connection_remove_transactions (DBusConnection *connection);
47 BusExpireItem expire_item;
49 DBusConnection *will_get_reply;
50 DBusConnection *will_send_reply;
52 dbus_uint32_t reply_serial;
59 DBusList *completed; /**< List of all completed connections */
60 int n_completed; /**< Length of completed list */
61 DBusList *incomplete; /**< List of all not-yet-active connections */
62 int n_incomplete; /**< Length of incomplete list */
64 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
65 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
66 int stamp; /**< Incrementing number */
67 BusExpireList *pending_replies; /**< List of pending replies */
69 /** List of all monitoring connections, a subset of completed.
70 * Each member is a #DBusConnection. */
72 BusMatchmaker *monitor_matchmaker;
74 #ifdef DBUS_ENABLE_STATS
75 int total_match_rules;
77 int peak_match_rules_per_conn;
81 int peak_bus_names_per_conn;
85 static dbus_int32_t connection_data_slot = -1;
89 BusConnections *connections;
90 DBusList *link_in_connection_list;
91 DBusConnection *connection;
92 DBusList *services_owned;
94 DBusList *match_rules;
97 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
98 DBusMessage *oom_message;
99 DBusPreallocatedSend *oom_preallocated;
100 BusClientPolicy *policy;
102 char *cached_loginfo_string;
103 BusSELinuxID *selinux_id;
104 BusAppArmorConfinement *apparmor_confinement;
106 long connection_tv_sec; /**< Time when we connected (seconds component) */
107 long connection_tv_usec; /**< Time when we connected (microsec component) */
108 int stamp; /**< connections->stamp last time we were traversed */
110 #ifdef DBUS_ENABLE_STATS
111 int peak_match_rules;
114 int n_pending_unix_fds;
115 DBusTimeout *pending_unix_fds_timeout;
117 /** non-NULL if and only if this is a monitor */
118 DBusList *link_in_monitors;
121 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
125 static void bus_connection_drop_pending_replies (BusConnections *connections,
126 DBusConnection *connection);
128 static dbus_bool_t expire_incomplete_timeout (void *data);
130 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
133 connection_get_loop (DBusConnection *connection)
135 BusConnectionData *d;
137 d = BUS_CONNECTION_DATA (connection);
138 _dbus_assert(d != NULL);
140 return bus_context_get_loop (d->connections->context);
145 get_connections_for_uid (BusConnections *connections,
151 /* val is NULL is 0 when it isn't in the hash yet */
153 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
156 current_count = _DBUS_POINTER_TO_INT (val);
158 return current_count;
162 adjust_connections_for_uid (BusConnections *connections,
168 current_count = get_connections_for_uid (connections, uid);
170 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
171 ": was %d adjustment %d making %d\n",
172 uid, current_count, adjustment, current_count + adjustment);
174 _dbus_assert (current_count >= 0);
176 current_count += adjustment;
178 _dbus_assert (current_count >= 0);
180 if (current_count == 0)
182 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
189 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
190 uid, _DBUS_INT_TO_POINTER (current_count));
192 /* only positive adjustment can fail as otherwise
193 * a hash entry should already exist
195 _dbus_assert (adjustment > 0 ||
196 (adjustment <= 0 && retval));
203 bus_connection_disconnected (DBusConnection *connection)
205 BusConnectionData *d;
207 BusMatchmaker *matchmaker;
209 d = BUS_CONNECTION_DATA (connection);
210 _dbus_assert (d != NULL);
212 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
213 d->name ? d->name : "(inactive)");
215 /* Delete our match rules */
216 if (d->n_match_rules > 0)
218 matchmaker = bus_context_get_matchmaker (d->connections->context);
219 bus_matchmaker_disconnected (matchmaker, connection);
222 /* Drop any service ownership. Unfortunately, this requires
223 * memory allocation and there doesn't seem to be a good way to
224 * handle it other than sleeping; we can't "fail" the operation of
225 * disconnecting a client, and preallocating a broadcast "service is
226 * now gone" message for every client-service pair seems kind of
229 while ((service = _dbus_list_get_last (&d->services_owned)))
231 BusTransaction *transaction;
236 dbus_error_init (&error);
238 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
239 _dbus_wait_for_memory ();
241 if (!bus_service_remove_owner (service, connection,
242 transaction, &error))
244 _DBUS_ASSERT_ERROR_IS_SET (&error);
246 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
248 dbus_error_free (&error);
249 bus_transaction_cancel_and_free (transaction);
250 _dbus_wait_for_memory ();
255 _dbus_verbose ("Failed to remove service owner: %s %s\n",
256 error.name, error.message);
257 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
261 bus_transaction_execute_and_free (transaction);
264 bus_dispatch_remove_connection (connection);
266 /* no more watching */
267 if (!dbus_connection_set_watch_functions (connection,
271 _dbus_assert_not_reached ("setting watch functions to NULL failed");
273 if (!dbus_connection_set_timeout_functions (connection,
277 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
279 dbus_connection_set_unix_user_function (connection,
281 dbus_connection_set_windows_user_function (connection,
284 dbus_connection_set_dispatch_status_function (connection,
287 if (d->pending_unix_fds_timeout)
289 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
290 d->pending_unix_fds_timeout);
291 _dbus_timeout_unref (d->pending_unix_fds_timeout);
293 d->pending_unix_fds_timeout = NULL;
294 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
296 bus_connection_remove_transactions (connection);
298 if (d->link_in_monitors != NULL)
300 BusMatchmaker *mm = d->connections->monitor_matchmaker;
303 bus_matchmaker_disconnected (mm, connection);
305 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
306 d->link_in_monitors = NULL;
309 if (d->link_in_connection_list != NULL)
315 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
316 d->link_in_connection_list = NULL;
317 d->connections->n_completed -= 1;
319 if (dbus_connection_get_unix_user (connection, &uid))
321 if (!adjust_connections_for_uid (d->connections,
323 _dbus_assert_not_reached ("adjusting downward should never fail");
328 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
329 d->link_in_connection_list = NULL;
330 d->connections->n_incomplete -= 1;
332 /* If we have dropped below the max. number of incomplete
333 * connections, start accept()ing again */
334 bus_context_check_all_watches (d->connections->context);
337 _dbus_assert (d->connections->n_incomplete >= 0);
338 _dbus_assert (d->connections->n_completed >= 0);
341 bus_connection_drop_pending_replies (d->connections, connection);
343 /* frees "d" as side effect */
344 dbus_connection_set_data (connection,
345 connection_data_slot,
348 dbus_connection_unref (connection);
352 add_connection_watch (DBusWatch *watch,
355 DBusConnection *connection = data;
357 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
361 remove_connection_watch (DBusWatch *watch,
364 DBusConnection *connection = data;
366 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
370 toggle_connection_watch (DBusWatch *watch,
373 DBusConnection *connection = data;
375 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
379 add_connection_timeout (DBusTimeout *timeout,
382 DBusConnection *connection = data;
384 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
388 remove_connection_timeout (DBusTimeout *timeout,
391 DBusConnection *connection = data;
393 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
397 dispatch_status_function (DBusConnection *connection,
398 DBusDispatchStatus new_status,
401 DBusLoop *loop = data;
403 if (new_status != DBUS_DISPATCH_COMPLETE)
405 while (!_dbus_loop_queue_dispatch (loop, connection))
406 _dbus_wait_for_memory ();
411 allow_unix_user_function (DBusConnection *connection,
415 BusConnectionData *d;
417 d = BUS_CONNECTION_DATA (connection);
419 _dbus_assert (d != NULL);
421 return bus_context_allow_unix_user (d->connections->context, uid);
425 free_connection_data (void *data)
427 BusConnectionData *d = data;
429 /* services_owned should be NULL since we should be disconnected */
430 _dbus_assert (d->services_owned == NULL);
431 _dbus_assert (d->n_services_owned == 0);
433 _dbus_assert (d->transaction_messages == NULL);
435 if (d->oom_preallocated)
436 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
439 dbus_message_unref (d->oom_message);
442 bus_client_policy_unref (d->policy);
444 if (d->apparmor_confinement)
445 bus_apparmor_confinement_unref (d->apparmor_confinement);
447 dbus_free (d->cached_loginfo_string);
455 bus_connections_new (BusContext *context)
457 BusConnections *connections;
459 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
462 connections = dbus_new0 (BusConnections, 1);
463 if (connections == NULL)
466 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
468 if (connections->completed_by_user == NULL)
471 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
472 expire_incomplete_timeout,
474 if (connections->expire_timeout == NULL)
477 _dbus_timeout_disable (connections->expire_timeout);
479 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
480 bus_context_get_reply_timeout (context),
481 bus_pending_reply_expired,
483 if (connections->pending_replies == NULL)
486 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
487 connections->expire_timeout))
490 connections->refcount = 1;
491 connections->context = context;
496 bus_expire_list_free (connections->pending_replies);
498 _dbus_timeout_unref (connections->expire_timeout);
500 _dbus_hash_table_unref (connections->completed_by_user);
502 dbus_free (connections);
504 dbus_connection_free_data_slot (&connection_data_slot);
510 bus_connections_ref (BusConnections *connections)
512 _dbus_assert (connections->refcount > 0);
513 connections->refcount += 1;
519 bus_connections_unref (BusConnections *connections)
521 _dbus_assert (connections->refcount > 0);
522 connections->refcount -= 1;
523 if (connections->refcount == 0)
525 /* drop all incomplete */
526 while (connections->incomplete != NULL)
528 DBusConnection *connection;
530 connection = connections->incomplete->data;
532 dbus_connection_ref (connection);
533 dbus_connection_close (connection);
534 bus_connection_disconnected (connection);
535 dbus_connection_unref (connection);
538 _dbus_assert (connections->n_incomplete == 0);
540 /* drop all monitors */
541 _dbus_list_clear (&connections->monitors);
543 /* drop all real connections */
544 while (connections->completed != NULL)
546 DBusConnection *connection;
548 connection = connections->completed->data;
550 dbus_connection_ref (connection);
551 dbus_connection_close (connection);
552 bus_connection_disconnected (connection);
553 dbus_connection_unref (connection);
556 _dbus_assert (connections->n_completed == 0);
558 bus_expire_list_free (connections->pending_replies);
560 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
561 connections->expire_timeout);
563 _dbus_timeout_unref (connections->expire_timeout);
565 _dbus_hash_table_unref (connections->completed_by_user);
567 if (connections->monitor_matchmaker != NULL)
568 bus_matchmaker_unref (connections->monitor_matchmaker);
570 dbus_free (connections);
572 dbus_connection_free_data_slot (&connection_data_slot);
576 /* Used for logging */
578 cache_peer_loginfo_string (BusConnectionData *d,
579 DBusConnection *connection)
581 DBusString loginfo_buf;
584 char *windows_sid = NULL, *security_label = NULL;
585 dbus_bool_t prev_added;
587 if (!_dbus_string_init (&loginfo_buf))
591 if (dbus_connection_get_unix_user (connection, &uid))
593 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
599 if (dbus_connection_get_unix_process_id (connection, &pid))
603 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
606 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
608 /* Ignore errors here; we may not have permissions to read the
610 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
611 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
617 if (dbus_connection_get_windows_user (connection, &windows_sid))
619 dbus_bool_t did_append;
623 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
627 did_append = _dbus_string_append_printf (&loginfo_buf,
628 "sid=\"%s\"", windows_sid);
629 dbus_free (windows_sid);
637 if (_dbus_connection_get_linux_security_label (connection, &security_label))
639 dbus_bool_t did_append;
643 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
647 did_append = _dbus_string_append_printf (&loginfo_buf,
648 "label=\"%s\"", security_label);
649 dbus_free (security_label);
650 security_label = NULL;
657 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
660 _dbus_string_free (&loginfo_buf);
664 _dbus_string_free (&loginfo_buf);
665 if (security_label != NULL)
666 dbus_free (security_label);
667 if (windows_sid != NULL)
668 dbus_free (windows_sid);
674 check_pending_fds_cb (DBusConnection *connection)
676 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
677 int n_pending_unix_fds_old;
678 int n_pending_unix_fds_new;
680 _dbus_assert(d != NULL);
682 n_pending_unix_fds_old = d->n_pending_unix_fds;
683 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
685 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
686 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
688 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
690 _dbus_timeout_restart (d->pending_unix_fds_timeout,
691 bus_context_get_pending_fd_timeout (d->connections->context));
694 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
696 _dbus_timeout_disable (d->pending_unix_fds_timeout);
700 d->n_pending_unix_fds = n_pending_unix_fds_new;
704 pending_unix_fds_timeout_cb (void *data)
706 DBusConnection *connection = data;
707 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
710 _dbus_assert (d != NULL);
711 limit = bus_context_get_pending_fd_timeout (d->connections->context);
712 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
713 "Connection \"%s\" (%s) has had Unix fds pending for too long, "
714 "closing it (pending_fd_timeout=%d ms)",
715 d->name != NULL ? d->name : "(null)",
716 bus_connection_get_loginfo (connection),
719 dbus_connection_close (connection);
724 bus_connections_setup_connection (BusConnections *connections,
725 DBusConnection *connection)
728 BusConnectionData *d;
733 d = dbus_new0 (BusConnectionData, 1);
738 d->connections = connections;
739 d->connection = connection;
741 _dbus_get_monotonic_time (&d->connection_tv_sec,
742 &d->connection_tv_usec);
744 _dbus_assert (connection_data_slot >= 0);
746 if (!dbus_connection_set_data (connection,
747 connection_data_slot,
748 d, free_connection_data))
754 dbus_connection_set_route_peer_messages (connection, TRUE);
758 dbus_error_init (&error);
759 d->selinux_id = bus_selinux_init_connection_id (connection,
761 if (dbus_error_is_set (&error))
763 /* This is a bit bogus because we pretend all errors
764 * are OOM; this is done because we know that in bus.c
765 * an OOM error disconnects the connection, which is
766 * the same thing we want on any other error.
768 dbus_error_free (&error);
772 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
774 if (dbus_error_is_set (&error))
776 /* This is a bit bogus because we pretend all errors
777 * are OOM; this is done because we know that in bus.c
778 * an OOM error disconnects the connection, which is
779 * the same thing we want on any other error.
781 dbus_error_free (&error);
785 if (!dbus_connection_set_watch_functions (connection,
786 add_connection_watch,
787 remove_connection_watch,
788 toggle_connection_watch,
793 if (!dbus_connection_set_timeout_functions (connection,
794 add_connection_timeout,
795 remove_connection_timeout,
800 /* For now we don't need to set a Windows user function because
801 * there are no policies in the config file controlling what
802 * Windows users can connect. The default 'same user that owns the
803 * bus can connect' behavior of DBusConnection is fine on Windows.
805 dbus_connection_set_unix_user_function (connection,
806 allow_unix_user_function,
809 dbus_connection_set_dispatch_status_function (connection,
810 dispatch_status_function,
811 bus_context_get_loop (connections->context),
814 d->link_in_connection_list = _dbus_list_alloc_link (connection);
815 if (d->link_in_connection_list == NULL)
818 /* Setup the connection with the dispatcher */
819 if (!bus_dispatch_add_connection (connection))
822 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
824 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
826 bus_dispatch_remove_connection (connection);
831 /* Setup pending fds timeout (see #80559) */
832 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
833 pending_unix_fds_timeout_cb,
835 if (d->pending_unix_fds_timeout == NULL)
838 _dbus_timeout_disable (d->pending_unix_fds_timeout);
839 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
840 d->pending_unix_fds_timeout))
843 _dbus_connection_set_pending_fds_function (connection,
844 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
847 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
848 connections->n_incomplete += 1;
850 dbus_connection_ref (connection);
852 bus_connections_expire_incomplete (connections);
854 /* The listening socket is removed from the main loop,
855 * i.e. does not accept(), while n_incomplete is at its
856 * maximum value; so we shouldn't get here in that case */
857 _dbus_assert (connections->n_incomplete <=
858 bus_context_get_max_incomplete_connections (connections->context));
860 /* If we have the maximum number of incomplete connections,
861 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
862 bus_context_check_all_watches (d->connections->context);
869 d->selinux_id = NULL;
871 if (d->apparmor_confinement)
872 bus_apparmor_confinement_unref (d->apparmor_confinement);
873 d->apparmor_confinement = NULL;
875 if (!dbus_connection_set_watch_functions (connection,
879 _dbus_assert_not_reached ("setting watch functions to NULL failed");
881 if (!dbus_connection_set_timeout_functions (connection,
885 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
887 dbus_connection_set_unix_user_function (connection,
890 dbus_connection_set_windows_user_function (connection,
893 dbus_connection_set_dispatch_status_function (connection,
896 if (d->pending_unix_fds_timeout)
897 _dbus_timeout_unref (d->pending_unix_fds_timeout);
899 d->pending_unix_fds_timeout = NULL;
901 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
903 if (d->link_in_connection_list != NULL)
905 _dbus_assert (d->link_in_connection_list->next == NULL);
906 _dbus_assert (d->link_in_connection_list->prev == NULL);
907 _dbus_list_free_link (d->link_in_connection_list);
908 d->link_in_connection_list = NULL;
911 if (!dbus_connection_set_data (connection,
912 connection_data_slot,
914 _dbus_assert_not_reached ("failed to set connection data to null");
916 /* "d" has now been freed */
923 bus_connections_expire_incomplete (BusConnections *connections)
929 if (connections->incomplete != NULL)
931 long tv_sec, tv_usec;
935 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
936 auth_timeout = bus_context_get_auth_timeout (connections->context);
938 link = _dbus_list_get_first_link (&connections->incomplete);
941 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
942 DBusConnection *connection;
943 BusConnectionData *d;
946 connection = link->data;
948 d = BUS_CONNECTION_DATA (connection);
950 _dbus_assert (d != NULL);
952 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
953 d->connection_tv_usec,
956 if (elapsed >= (double) auth_timeout)
958 /* Unfortunately, we can't identify the connection: it doesn't
959 * have a unique name yet, we don't know its uid/pid yet,
961 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
962 "Connection has not authenticated soon enough, closing it "
963 "(auth_timeout=%dms, elapsed: %.0fms)",
964 auth_timeout, elapsed);
966 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
967 dbus_connection_close (connection);
971 /* We can end the loop, since the connections are in oldest-first order */
972 next_interval = ((double)auth_timeout) - elapsed;
973 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
974 connection, next_interval);
983 bus_expire_timeout_set_interval (connections->expire_timeout,
988 expire_incomplete_timeout (void *data)
990 BusConnections *connections = data;
992 _dbus_verbose ("Running\n");
994 /* note that this may remove the timeout */
995 bus_connections_expire_incomplete (connections);
1001 bus_connection_get_unix_groups (DBusConnection *connection,
1002 unsigned long **groups,
1011 if (dbus_connection_get_unix_user (connection, &uid))
1013 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
1015 _dbus_verbose ("Did not get any groups for UID %lu\n",
1021 _dbus_verbose ("Got %d groups for UID %lu\n",
1027 return TRUE; /* successfully got 0 groups */
1031 bus_connection_is_in_unix_group (DBusConnection *connection,
1035 unsigned long *group_ids;
1038 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1043 while (i < n_group_ids)
1045 if (group_ids[i] == gid)
1047 dbus_free (group_ids);
1053 dbus_free (group_ids);
1058 bus_connection_get_loginfo (DBusConnection *connection)
1060 BusConnectionData *d;
1062 d = BUS_CONNECTION_DATA (connection);
1063 _dbus_assert(d != NULL);
1065 if (!bus_connection_is_active (connection))
1067 return d->cached_loginfo_string;
1071 bus_connection_get_policy (DBusConnection *connection)
1073 BusConnectionData *d;
1075 d = BUS_CONNECTION_DATA (connection);
1077 _dbus_assert (d != NULL);
1078 _dbus_assert (d->policy != NULL);
1084 foreach_active (BusConnections *connections,
1085 BusConnectionForeachFunction function,
1090 link = _dbus_list_get_first_link (&connections->completed);
1091 while (link != NULL)
1093 DBusConnection *connection = link->data;
1094 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1096 if (!(* function) (connection, data))
1106 foreach_inactive (BusConnections *connections,
1107 BusConnectionForeachFunction function,
1112 link = _dbus_list_get_first_link (&connections->incomplete);
1113 while (link != NULL)
1115 DBusConnection *connection = link->data;
1116 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1118 if (!(* function) (connection, data))
1128 * Calls function on each active connection; if the function returns
1129 * #FALSE, stops iterating. Active connections are authenticated
1130 * and have sent a Hello message.
1132 * @param connections the connections object
1133 * @param function the function
1134 * @param data data to pass to it as a second arg
1137 bus_connections_foreach_active (BusConnections *connections,
1138 BusConnectionForeachFunction function,
1141 foreach_active (connections, function, data);
1145 * Calls function on each connection; if the function returns
1146 * #FALSE, stops iterating.
1148 * @param connections the connections object
1149 * @param function the function
1150 * @param data data to pass to it as a second arg
1153 bus_connections_foreach (BusConnections *connections,
1154 BusConnectionForeachFunction function,
1157 if (!foreach_active (connections, function, data))
1160 foreach_inactive (connections, function, data);
1164 bus_connections_get_context (BusConnections *connections)
1166 return connections->context;
1170 * This is used to avoid covering the same connection twice when
1171 * traversing connections. Note that it assumes we will
1172 * bus_connection_mark_stamp() each connection at least once per
1173 * INT_MAX increments of the global stamp, or wraparound would break
1177 bus_connections_increment_stamp (BusConnections *connections)
1179 connections->stamp += 1;
1182 /* Mark connection with current stamp, return TRUE if it
1183 * didn't already have that stamp
1186 bus_connection_mark_stamp (DBusConnection *connection)
1188 BusConnectionData *d;
1190 d = BUS_CONNECTION_DATA (connection);
1192 _dbus_assert (d != NULL);
1194 if (d->stamp == d->connections->stamp)
1198 d->stamp = d->connections->stamp;
1204 bus_connection_get_context (DBusConnection *connection)
1206 BusConnectionData *d;
1208 d = BUS_CONNECTION_DATA (connection);
1210 _dbus_assert (d != NULL);
1212 return d->connections->context;
1216 bus_connection_get_connections (DBusConnection *connection)
1218 BusConnectionData *d;
1220 d = BUS_CONNECTION_DATA (connection);
1222 _dbus_assert (d != NULL);
1224 return d->connections;
1228 bus_connection_get_registry (DBusConnection *connection)
1230 BusConnectionData *d;
1232 d = BUS_CONNECTION_DATA (connection);
1234 _dbus_assert (d != NULL);
1236 return bus_context_get_registry (d->connections->context);
1240 bus_connection_get_activation (DBusConnection *connection)
1242 BusConnectionData *d;
1244 d = BUS_CONNECTION_DATA (connection);
1246 _dbus_assert (d != NULL);
1248 return bus_context_get_activation (d->connections->context);
1252 bus_connection_get_matchmaker (DBusConnection *connection)
1254 BusConnectionData *d;
1256 d = BUS_CONNECTION_DATA (connection);
1258 _dbus_assert (d != NULL);
1260 return bus_context_get_matchmaker (d->connections->context);
1264 bus_connection_get_selinux_id (DBusConnection *connection)
1266 BusConnectionData *d;
1268 d = BUS_CONNECTION_DATA (connection);
1270 _dbus_assert (d != NULL);
1272 return d->selinux_id;
1275 BusAppArmorConfinement*
1276 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1278 BusConnectionData *d;
1280 d = BUS_CONNECTION_DATA (connection);
1282 _dbus_assert (d != NULL);
1284 bus_apparmor_confinement_ref (d->apparmor_confinement);
1285 return d->apparmor_confinement;
1289 * Checks whether the connection is registered with the message bus.
1291 * @param connection the connection
1292 * @returns #TRUE if we're an active message bus participant
1295 bus_connection_is_active (DBusConnection *connection)
1297 BusConnectionData *d;
1299 d = BUS_CONNECTION_DATA (connection);
1300 _dbus_assert(d != NULL);
1302 return d->name != NULL;
1306 bus_connection_preallocate_oom_error (DBusConnection *connection)
1308 DBusMessage *message;
1309 DBusPreallocatedSend *preallocated;
1310 BusConnectionData *d;
1312 d = BUS_CONNECTION_DATA (connection);
1314 _dbus_assert (d != NULL);
1316 if (d->oom_preallocated != NULL)
1319 preallocated = dbus_connection_preallocate_send (connection);
1320 if (preallocated == NULL)
1323 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1325 if (message == NULL)
1327 dbus_connection_free_preallocated_send (connection, preallocated);
1331 /* d->name may be NULL, but that is OK */
1332 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1333 !dbus_message_set_destination (message, d->name) ||
1334 !dbus_message_set_sender (message,
1337 dbus_connection_free_preallocated_send (connection, preallocated);
1338 dbus_message_unref (message);
1342 /* set reply serial to placeholder value just so space is already allocated
1345 if (!dbus_message_set_reply_serial (message, 14))
1347 dbus_connection_free_preallocated_send (connection, preallocated);
1348 dbus_message_unref (message);
1352 d->oom_message = message;
1353 d->oom_preallocated = preallocated;
1359 bus_connection_send_oom_error (DBusConnection *connection,
1360 DBusMessage *in_reply_to)
1362 BusConnectionData *d;
1364 d = BUS_CONNECTION_DATA (connection);
1366 _dbus_assert (d != NULL);
1367 _dbus_assert (d->oom_message != NULL);
1369 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1370 "dbus-daemon transaction failed (OOM), sending error to "
1371 "sender %s", bus_connection_get_loginfo (connection));
1373 /* should always succeed since we set it to a placeholder earlier */
1374 if (!dbus_message_set_reply_serial (d->oom_message,
1375 dbus_message_get_serial (in_reply_to)))
1376 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1378 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1380 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1381 d->oom_message, NULL);
1383 dbus_message_unref (d->oom_message);
1384 d->oom_message = NULL;
1385 d->oom_preallocated = NULL;
1388 #ifdef DBUS_ENABLE_STATS
1390 update_peak (int *peak,
1399 bus_connection_add_match_rule_link (DBusConnection *connection,
1402 BusConnectionData *d;
1404 d = BUS_CONNECTION_DATA (connection);
1405 _dbus_assert (d != NULL);
1407 _dbus_list_append_link (&d->match_rules, link);
1409 d->n_match_rules += 1;
1411 #ifdef DBUS_ENABLE_STATS
1412 update_peak (&d->peak_match_rules, d->n_match_rules);
1413 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1415 d->connections->total_match_rules += 1;
1416 update_peak (&d->connections->peak_match_rules,
1417 d->connections->total_match_rules);
1422 bus_connection_add_match_rule (DBusConnection *connection,
1427 link = _dbus_list_alloc_link (rule);
1432 bus_connection_add_match_rule_link (connection, link);
1438 bus_connection_remove_match_rule (DBusConnection *connection,
1441 BusConnectionData *d;
1443 d = BUS_CONNECTION_DATA (connection);
1444 _dbus_assert (d != NULL);
1446 _dbus_list_remove_last (&d->match_rules, rule);
1448 d->n_match_rules -= 1;
1449 _dbus_assert (d->n_match_rules >= 0);
1451 #ifdef DBUS_ENABLE_STATS
1452 d->connections->total_match_rules -= 1;
1457 bus_connection_get_n_match_rules (DBusConnection *connection)
1459 BusConnectionData *d;
1461 d = BUS_CONNECTION_DATA (connection);
1462 _dbus_assert (d != NULL);
1464 return d->n_match_rules;
1468 bus_connection_add_owned_service_link (DBusConnection *connection,
1471 BusConnectionData *d;
1473 d = BUS_CONNECTION_DATA (connection);
1474 _dbus_assert (d != NULL);
1476 _dbus_list_append_link (&d->services_owned, link);
1478 d->n_services_owned += 1;
1480 #ifdef DBUS_ENABLE_STATS
1481 update_peak (&d->peak_bus_names, d->n_services_owned);
1482 update_peak (&d->connections->peak_bus_names_per_conn,
1483 d->n_services_owned);
1485 d->connections->total_bus_names += 1;
1486 update_peak (&d->connections->peak_bus_names,
1487 d->connections->total_bus_names);
1492 bus_connection_add_owned_service (DBusConnection *connection,
1493 BusService *service)
1497 link = _dbus_list_alloc_link (service);
1502 bus_connection_add_owned_service_link (connection, link);
1508 bus_connection_remove_owned_service (DBusConnection *connection,
1509 BusService *service)
1511 BusConnectionData *d;
1513 d = BUS_CONNECTION_DATA (connection);
1514 _dbus_assert (d != NULL);
1516 _dbus_list_remove_last (&d->services_owned, service);
1518 d->n_services_owned -= 1;
1519 _dbus_assert (d->n_services_owned >= 0);
1521 #ifdef DBUS_ENABLE_STATS
1522 d->connections->total_bus_names -= 1;
1527 bus_connection_get_n_services_owned (DBusConnection *connection)
1529 BusConnectionData *d;
1531 d = BUS_CONNECTION_DATA (connection);
1532 _dbus_assert (d != NULL);
1534 return d->n_services_owned;
1538 bus_connection_complete (DBusConnection *connection,
1539 const DBusString *name,
1542 BusConnectionData *d;
1545 d = BUS_CONNECTION_DATA (connection);
1546 _dbus_assert (d != NULL);
1547 _dbus_assert (d->name == NULL);
1548 _dbus_assert (d->policy == NULL);
1550 _dbus_assert (!bus_connection_is_active (connection));
1552 if (!_dbus_string_copy_data (name, &d->name))
1554 BUS_SET_OOM (error);
1558 _dbus_assert (d->name != NULL);
1560 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1562 d->policy = bus_context_create_client_policy (d->connections->context,
1566 /* we may have a NULL policy on OOM or error getting list of
1567 * groups for a user. In the latter case we don't handle it so
1568 * well currently, as it will just keep failing over and over.
1571 if (d->policy == NULL)
1573 _dbus_verbose ("Failed to create security policy for connection %p\n",
1575 _DBUS_ASSERT_ERROR_IS_SET (error);
1576 dbus_free (d->name);
1581 if (dbus_connection_get_unix_user (connection, &uid))
1583 if (!adjust_connections_for_uid (d->connections,
1588 /* Create and cache a string which holds information about the
1589 * peer process; used for logging purposes.
1591 if (!cache_peer_loginfo_string (d, connection))
1594 /* Now the connection is active, move it between lists */
1595 _dbus_list_unlink (&d->connections->incomplete,
1596 d->link_in_connection_list);
1597 d->connections->n_incomplete -= 1;
1598 _dbus_list_append_link (&d->connections->completed,
1599 d->link_in_connection_list);
1600 d->connections->n_completed += 1;
1602 _dbus_assert (d->connections->n_incomplete >= 0);
1603 _dbus_assert (d->connections->n_completed > 0);
1605 /* If we have dropped below the max. number of incomplete
1606 * connections, start accept()ing again */
1607 bus_context_check_all_watches (d->connections->context);
1609 /* See if we can remove the timeout */
1610 bus_connections_expire_incomplete (d->connections);
1612 _dbus_assert (bus_connection_is_active (connection));
1616 BUS_SET_OOM (error);
1617 dbus_free (d->name);
1620 bus_client_policy_unref (d->policy);
1626 bus_connections_reload_policy (BusConnections *connections,
1629 BusConnectionData *d;
1630 DBusConnection *connection;
1633 _dbus_assert (connections != NULL);
1634 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1636 for (link = _dbus_list_get_first_link (&(connections->completed));
1638 link = _dbus_list_get_next_link (&(connections->completed), link))
1640 connection = link->data;
1641 d = BUS_CONNECTION_DATA (connection);
1642 _dbus_assert (d != NULL);
1643 _dbus_assert (d->policy != NULL);
1645 bus_client_policy_unref (d->policy);
1646 d->policy = bus_context_create_client_policy (connections->context,
1649 if (d->policy == NULL)
1651 _dbus_verbose ("Failed to create security policy for connection %p\n",
1653 _DBUS_ASSERT_ERROR_IS_SET (error);
1662 bus_connection_get_name (DBusConnection *connection)
1664 BusConnectionData *d;
1666 d = BUS_CONNECTION_DATA (connection);
1667 _dbus_assert (d != NULL);
1673 * Check whether completing the passed-in connection would
1674 * exceed limits, and if so set error and return #FALSE
1677 bus_connections_check_limits (BusConnections *connections,
1678 DBusConnection *requesting_completion,
1679 const char **limit_name_out,
1686 limit = bus_context_get_max_completed_connections (connections->context);
1688 if (connections->n_completed >= limit)
1690 if (limit_name_out != NULL)
1691 *limit_name_out = "max_completed_connections";
1693 if (limit_out != NULL)
1696 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1697 "The maximum number of active connections has been reached");
1701 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1703 limit = bus_context_get_max_connections_per_user (connections->context);
1705 if (get_connections_for_uid (connections, uid) >= limit)
1707 if (limit_name_out != NULL)
1708 *limit_name_out = "max_connections_per_user";
1710 if (limit_out != NULL)
1713 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1714 "The maximum number of active connections for UID %lu has been reached",
1724 bus_pending_reply_free (BusPendingReply *pending)
1726 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1728 pending->will_send_reply,
1729 pending->will_get_reply,
1730 pending->reply_serial);
1732 dbus_free (pending);
1736 bus_pending_reply_send_no_reply (BusConnections *connections,
1737 BusTransaction *transaction,
1738 BusPendingReply *pending)
1740 DBusMessage *message;
1741 DBusMessageIter iter;
1747 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1748 if (message == NULL)
1751 dbus_message_set_no_reply (message, TRUE);
1753 if (!dbus_message_set_reply_serial (message,
1754 pending->reply_serial))
1757 if (!dbus_message_set_error_name (message,
1758 DBUS_ERROR_NO_REPLY))
1761 /* If you change these messages, adjust test/dbus-daemon.c to match */
1762 if (pending->will_send_reply == NULL)
1763 errmsg = "Message recipient disconnected from message bus without replying";
1765 errmsg = "Message did not receive a reply (timeout by message bus)";
1767 dbus_message_iter_init_append (message, &iter);
1768 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1771 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1778 dbus_message_unref (message);
1783 bus_pending_reply_expired (BusExpireList *list,
1787 BusPendingReply *pending = link->data;
1788 BusConnections *connections = data;
1789 BusTransaction *transaction;
1791 /* No reply is forthcoming. So nuke it if we can. If not,
1792 * leave it in the list to try expiring again later when we
1796 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1798 pending->will_send_reply,
1799 pending->will_get_reply,
1800 pending->reply_serial);
1802 transaction = bus_transaction_new (connections->context);
1803 if (transaction == NULL)
1806 if (!bus_pending_reply_send_no_reply (connections,
1810 bus_transaction_cancel_and_free (transaction);
1814 bus_expire_list_remove_link (connections->pending_replies, link);
1816 bus_pending_reply_free (pending);
1817 bus_transaction_execute_and_free (transaction);
1823 bus_connection_drop_pending_replies (BusConnections *connections,
1824 DBusConnection *connection)
1826 /* The DBusConnection is almost 100% finalized here, so you can't
1827 * do anything with it except check for pointer equality
1831 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1834 link = bus_expire_list_get_first_link (connections->pending_replies);
1835 while (link != NULL)
1838 BusPendingReply *pending;
1840 next = bus_expire_list_get_next_link (connections->pending_replies,
1842 pending = link->data;
1844 if (pending->will_get_reply == connection)
1846 /* We don't need to track this pending reply anymore */
1848 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1850 pending->will_send_reply,
1851 pending->will_get_reply,
1852 pending->reply_serial);
1854 bus_expire_list_remove_link (connections->pending_replies,
1856 bus_pending_reply_free (pending);
1858 else if (pending->will_send_reply == connection)
1860 /* The reply isn't going to be sent, so set things
1861 * up so it will be expired right away
1863 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1865 pending->will_send_reply,
1866 pending->will_get_reply,
1867 pending->reply_serial);
1869 pending->will_send_reply = NULL;
1870 pending->expire_item.added_tv_sec = 0;
1871 pending->expire_item.added_tv_usec = 0;
1873 bus_expire_list_recheck_immediately (connections->pending_replies);
1883 BusPendingReply *pending;
1884 BusConnections *connections;
1885 } CancelPendingReplyData;
1888 cancel_pending_reply (void *data)
1890 CancelPendingReplyData *d = data;
1892 _dbus_verbose ("d = %p\n", d);
1894 if (!bus_expire_list_remove (d->connections->pending_replies,
1895 &d->pending->expire_item))
1896 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1898 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1902 cancel_pending_reply_data_free (void *data)
1904 CancelPendingReplyData *d = data;
1906 _dbus_verbose ("d = %p\n", d);
1908 /* d->pending should be either freed or still
1909 * in the list of pending replies (owned by someone
1917 * Record that a reply is allowed; return TRUE on success.
1920 bus_connections_expect_reply (BusConnections *connections,
1921 BusTransaction *transaction,
1922 DBusConnection *will_get_reply,
1923 DBusConnection *will_send_reply,
1924 DBusMessage *reply_to_this,
1927 BusPendingReply *pending;
1928 dbus_uint32_t reply_serial;
1930 CancelPendingReplyData *cprd;
1934 _dbus_assert (will_get_reply != NULL);
1935 _dbus_assert (will_send_reply != NULL);
1936 _dbus_assert (reply_to_this != NULL);
1938 if (dbus_message_get_no_reply (reply_to_this))
1939 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1941 reply_serial = dbus_message_get_serial (reply_to_this);
1943 link = bus_expire_list_get_first_link (connections->pending_replies);
1945 while (link != NULL)
1947 pending = link->data;
1949 if (pending->reply_serial == reply_serial &&
1950 pending->will_get_reply == will_get_reply &&
1951 pending->will_send_reply == will_send_reply)
1953 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1954 "Message has the same reply serial as a currently-outstanding existing method call");
1958 link = bus_expire_list_get_next_link (connections->pending_replies,
1960 if (pending->will_get_reply == will_get_reply)
1964 limit = bus_context_get_max_replies_per_connection (connections->context);
1968 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
1969 "The maximum number of pending replies for "
1970 "\"%s\" (%s) has been reached "
1971 "(max_replies_per_connection=%d)",
1972 bus_connection_get_name (will_get_reply),
1973 bus_connection_get_loginfo (will_get_reply),
1976 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1977 "The maximum number of pending replies per connection has been reached");
1981 pending = dbus_new0 (BusPendingReply, 1);
1982 if (pending == NULL)
1984 BUS_SET_OOM (error);
1988 #ifdef DBUS_ENABLE_VERBOSE_MODE
1989 /* so we can see a not-yet-added pending reply */
1990 pending->expire_item.added_tv_sec = 1;
1991 pending->expire_item.added_tv_usec = 1;
1994 pending->will_get_reply = will_get_reply;
1995 pending->will_send_reply = will_send_reply;
1996 pending->reply_serial = reply_serial;
1998 cprd = dbus_new0 (CancelPendingReplyData, 1);
2001 BUS_SET_OOM (error);
2002 bus_pending_reply_free (pending);
2006 if (!bus_expire_list_add (connections->pending_replies,
2007 &pending->expire_item))
2009 BUS_SET_OOM (error);
2011 bus_pending_reply_free (pending);
2015 if (!bus_transaction_add_cancel_hook (transaction,
2016 cancel_pending_reply,
2018 cancel_pending_reply_data_free))
2020 BUS_SET_OOM (error);
2021 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
2023 bus_pending_reply_free (pending);
2027 cprd->pending = pending;
2028 cprd->connections = connections;
2030 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
2031 &pending->expire_item.added_tv_usec);
2033 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2035 pending->will_send_reply,
2036 pending->will_get_reply,
2037 pending->reply_serial);
2045 BusConnections *connections;
2046 } CheckPendingReplyData;
2049 cancel_check_pending_reply (void *data)
2051 CheckPendingReplyData *d = data;
2053 _dbus_verbose ("d = %p\n",d);
2055 bus_expire_list_add_link (d->connections->pending_replies,
2061 check_pending_reply_data_free (void *data)
2063 CheckPendingReplyData *d = data;
2065 _dbus_verbose ("d = %p\n",d);
2067 if (d->link != NULL)
2069 BusPendingReply *pending = d->link->data;
2071 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2072 &pending->expire_item));
2074 bus_pending_reply_free (pending);
2075 _dbus_list_free_link (d->link);
2082 * Check whether a reply is allowed, remove BusPendingReply
2083 * if so, return TRUE if so.
2086 bus_connections_check_reply (BusConnections *connections,
2087 BusTransaction *transaction,
2088 DBusConnection *sending_reply,
2089 DBusConnection *receiving_reply,
2093 CheckPendingReplyData *cprd;
2095 dbus_uint32_t reply_serial;
2097 _dbus_assert (sending_reply != NULL);
2098 _dbus_assert (receiving_reply != NULL);
2100 reply_serial = dbus_message_get_reply_serial (reply);
2102 link = bus_expire_list_get_first_link (connections->pending_replies);
2103 while (link != NULL)
2105 BusPendingReply *pending = link->data;
2107 if (pending->reply_serial == reply_serial &&
2108 pending->will_get_reply == receiving_reply &&
2109 pending->will_send_reply == sending_reply)
2111 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2115 link = bus_expire_list_get_next_link (connections->pending_replies,
2121 _dbus_verbose ("No pending reply expected\n");
2126 cprd = dbus_new0 (CheckPendingReplyData, 1);
2129 BUS_SET_OOM (error);
2133 if (!bus_transaction_add_cancel_hook (transaction,
2134 cancel_check_pending_reply,
2136 check_pending_reply_data_free))
2138 BUS_SET_OOM (error);
2144 cprd->connections = connections;
2146 bus_expire_list_unlink (connections->pending_replies,
2149 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2157 * Note that this is fairly fragile; in particular, don't try to use
2158 * one transaction across any main loop iterations.
2163 BusTransaction *transaction;
2164 DBusMessage *message;
2165 DBusPreallocatedSend *preallocated;
2170 BusTransactionCancelFunction cancel_function;
2171 DBusFreeFunction free_data_function;
2175 struct BusTransaction
2177 DBusList *connections;
2178 BusContext *context;
2179 DBusList *cancel_hooks;
2183 message_to_send_free (DBusConnection *connection,
2184 MessageToSend *to_send)
2186 if (to_send->message)
2187 dbus_message_unref (to_send->message);
2189 if (to_send->preallocated)
2190 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2192 dbus_free (to_send);
2196 cancel_hook_cancel (void *element,
2199 CancelHook *ch = element;
2201 _dbus_verbose ("Running transaction cancel hook\n");
2203 if (ch->cancel_function)
2204 (* ch->cancel_function) (ch->data);
2208 cancel_hook_free (void *element,
2211 CancelHook *ch = element;
2213 if (ch->free_data_function)
2214 (* ch->free_data_function) (ch->data);
2220 free_cancel_hooks (BusTransaction *transaction)
2222 _dbus_list_foreach (&transaction->cancel_hooks,
2223 cancel_hook_free, NULL);
2225 _dbus_list_clear (&transaction->cancel_hooks);
2229 bus_transaction_new (BusContext *context)
2231 BusTransaction *transaction;
2233 transaction = dbus_new0 (BusTransaction, 1);
2234 if (transaction == NULL)
2237 transaction->context = context;
2243 bus_transaction_get_context (BusTransaction *transaction)
2245 return transaction->context;
2249 * Reserve enough memory to capture the given message if the
2250 * transaction goes through.
2253 bus_transaction_capture (BusTransaction *transaction,
2254 DBusConnection *sender,
2255 DBusConnection *addressed_recipient,
2256 DBusMessage *message)
2258 BusConnections *connections;
2261 DBusList *recipients = NULL;
2262 dbus_bool_t ret = FALSE;
2264 connections = bus_context_get_connections (transaction->context);
2266 /* shortcut: don't compose the message unless someone wants it */
2267 if (connections->monitors == NULL)
2270 mm = connections->monitor_matchmaker;
2271 /* This is non-null if there has ever been a monitor - we don't GC it.
2272 * There's little point, since there is up to 1 per process. */
2273 _dbus_assert (mm != NULL);
2275 if (!bus_matchmaker_get_recipients (mm, connections, sender,
2276 addressed_recipient, message, &recipients))
2279 for (link = _dbus_list_get_first_link (&recipients);
2281 link = _dbus_list_get_next_link (&recipients, link))
2283 DBusConnection *recipient = link->data;
2285 if (!bus_transaction_send (transaction, recipient, message))
2292 _dbus_list_clear (&recipients);
2297 bus_transaction_capture_error_reply (BusTransaction *transaction,
2298 DBusConnection *addressed_recipient,
2299 const DBusError *error,
2300 DBusMessage *in_reply_to)
2302 BusConnections *connections;
2304 dbus_bool_t ret = FALSE;
2306 _dbus_assert (error != NULL);
2307 _DBUS_ASSERT_ERROR_IS_SET (error);
2309 connections = bus_context_get_connections (transaction->context);
2311 /* shortcut: don't compose the message unless someone wants it */
2312 if (connections->monitors == NULL)
2315 reply = dbus_message_new_error (in_reply_to,
2322 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2325 ret = bus_transaction_capture (transaction, NULL, addressed_recipient, reply);
2328 dbus_message_unref (reply);
2333 bus_transaction_send_from_driver (BusTransaction *transaction,
2334 DBusConnection *connection,
2335 DBusMessage *message)
2337 DBusError error = DBUS_ERROR_INIT;
2339 /* We have to set the sender to the driver, and have
2340 * to check security policy since it was not done in
2343 _dbus_verbose ("Sending %s %s %s from driver\n",
2344 dbus_message_get_interface (message) ?
2345 dbus_message_get_interface (message) : "(no interface)",
2346 dbus_message_get_member (message) ?
2347 dbus_message_get_member (message) : "(no member)",
2348 dbus_message_get_error_name (message) ?
2349 dbus_message_get_error_name (message) : "(no error name)");
2351 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2354 if (bus_connection_is_active (connection))
2356 if (!dbus_message_set_destination (message,
2357 bus_connection_get_name (connection)))
2361 /* bus driver never wants a reply */
2362 dbus_message_set_no_reply (message, TRUE);
2364 /* Capture it for monitors, even if the real recipient's receive policy
2365 * does not allow it to receive this message from us (which would be odd).
2367 if (!bus_transaction_capture (transaction, NULL, connection, message))
2370 /* If security policy doesn't allow the message, we would silently
2371 * eat it; the driver doesn't care about getting a reply. However,
2372 * if we're actively capturing messages, it's nice to log that we
2373 * tried to send it and did not allow ourselves to do so.
2375 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2377 NULL, connection, connection,
2378 message, NULL, &error))
2380 if (!bus_transaction_capture_error_reply (transaction, connection,
2383 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2384 "message from dbus-daemon rejected but not enough "
2385 "memory to capture it");
2388 /* This is not fatal to the transaction so silently eat the disallowed
2389 * message (see reasoning above) */
2390 dbus_error_free (&error);
2394 return bus_transaction_send (transaction, connection, message);
2398 bus_transaction_send (BusTransaction *transaction,
2399 DBusConnection *connection,
2400 DBusMessage *message)
2402 MessageToSend *to_send;
2403 BusConnectionData *d;
2406 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2407 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2408 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2410 dbus_message_get_interface (message) ?
2411 dbus_message_get_interface (message) : "(unset)",
2412 dbus_message_get_member (message) ?
2413 dbus_message_get_member (message) : "(unset)",
2414 dbus_message_get_error_name (message) ?
2415 dbus_message_get_error_name (message) : "(unset)",
2416 dbus_connection_get_is_connected (connection) ?
2417 "" : " (disconnected)");
2419 _dbus_assert (dbus_message_get_sender (message) != NULL);
2421 if (!dbus_connection_get_is_connected (connection))
2422 return TRUE; /* silently ignore disconnected connections */
2424 d = BUS_CONNECTION_DATA (connection);
2425 _dbus_assert (d != NULL);
2427 to_send = dbus_new (MessageToSend, 1);
2428 if (to_send == NULL)
2433 to_send->preallocated = dbus_connection_preallocate_send (connection);
2434 if (to_send->preallocated == NULL)
2436 dbus_free (to_send);
2440 dbus_message_ref (message);
2441 to_send->message = message;
2442 to_send->transaction = transaction;
2444 _dbus_verbose ("about to prepend message\n");
2446 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2448 message_to_send_free (connection, to_send);
2452 _dbus_verbose ("prepended message\n");
2454 /* See if we already had this connection in the list
2455 * for this transaction. If we have a pending message,
2456 * then we should already be in transaction->connections
2458 link = _dbus_list_get_first_link (&d->transaction_messages);
2459 _dbus_assert (link->data == to_send);
2460 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2461 while (link != NULL)
2463 MessageToSend *m = link->data;
2464 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2466 if (m->transaction == transaction)
2474 if (!_dbus_list_prepend (&transaction->connections, connection))
2476 _dbus_list_remove (&d->transaction_messages, to_send);
2477 message_to_send_free (connection, to_send);
2486 transaction_free (BusTransaction *transaction)
2488 _dbus_assert (transaction->connections == NULL);
2490 free_cancel_hooks (transaction);
2492 dbus_free (transaction);
2496 connection_cancel_transaction (DBusConnection *connection,
2497 BusTransaction *transaction)
2500 BusConnectionData *d;
2502 d = BUS_CONNECTION_DATA (connection);
2503 _dbus_assert (d != NULL);
2505 link = _dbus_list_get_first_link (&d->transaction_messages);
2506 while (link != NULL)
2508 MessageToSend *m = link->data;
2509 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2511 if (m->transaction == transaction)
2513 _dbus_list_remove_link (&d->transaction_messages,
2516 message_to_send_free (connection, m);
2524 bus_transaction_cancel_and_free (BusTransaction *transaction)
2526 DBusConnection *connection;
2528 _dbus_verbose ("TRANSACTION: cancelled\n");
2530 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2531 connection_cancel_transaction (connection, transaction);
2533 _dbus_list_foreach (&transaction->cancel_hooks,
2534 cancel_hook_cancel, NULL);
2536 transaction_free (transaction);
2540 connection_execute_transaction (DBusConnection *connection,
2541 BusTransaction *transaction)
2544 BusConnectionData *d;
2546 d = BUS_CONNECTION_DATA (connection);
2547 _dbus_assert (d != NULL);
2549 /* Send the queue in order (FIFO) */
2550 link = _dbus_list_get_last_link (&d->transaction_messages);
2551 while (link != NULL)
2553 MessageToSend *m = link->data;
2554 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2556 if (m->transaction == transaction)
2558 _dbus_list_remove_link (&d->transaction_messages,
2561 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2563 dbus_connection_send_preallocated (connection,
2568 m->preallocated = NULL; /* so we don't double-free it */
2570 message_to_send_free (connection, m);
2578 bus_transaction_execute_and_free (BusTransaction *transaction)
2580 /* For each connection in transaction->connections
2583 DBusConnection *connection;
2585 _dbus_verbose ("TRANSACTION: executing\n");
2587 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2588 connection_execute_transaction (connection, transaction);
2590 transaction_free (transaction);
2594 bus_connection_remove_transactions (DBusConnection *connection)
2596 MessageToSend *to_send;
2597 BusConnectionData *d;
2599 d = BUS_CONNECTION_DATA (connection);
2600 _dbus_assert (d != NULL);
2602 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2604 /* only has an effect for the first MessageToSend listing this transaction */
2605 _dbus_list_remove (&to_send->transaction->connections,
2608 _dbus_list_remove (&d->transaction_messages, to_send);
2609 message_to_send_free (connection, to_send);
2614 * Converts the DBusError to a message reply
2617 bus_transaction_send_error_reply (BusTransaction *transaction,
2618 DBusConnection *connection,
2619 const DBusError *error,
2620 DBusMessage *in_reply_to)
2624 _dbus_assert (error != NULL);
2625 _DBUS_ASSERT_ERROR_IS_SET (error);
2627 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2628 error->name, error->message);
2630 reply = dbus_message_new_error (in_reply_to,
2636 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2638 dbus_message_unref (reply);
2642 dbus_message_unref (reply);
2648 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2649 BusTransactionCancelFunction cancel_function,
2651 DBusFreeFunction free_data_function)
2655 ch = dbus_new (CancelHook, 1);
2659 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2660 cancel_function, data);
2662 ch->cancel_function = cancel_function;
2664 ch->free_data_function = free_data_function;
2666 /* It's important that the hooks get run in reverse order that they
2669 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2679 bus_connections_get_n_active (BusConnections *connections)
2681 return connections->n_completed;
2685 bus_connections_get_n_incomplete (BusConnections *connections)
2687 return connections->n_incomplete;
2690 #ifdef DBUS_ENABLE_STATS
2692 bus_connections_get_total_match_rules (BusConnections *connections)
2694 return connections->total_match_rules;
2698 bus_connections_get_peak_match_rules (BusConnections *connections)
2700 return connections->peak_match_rules;
2704 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2706 return connections->peak_match_rules_per_conn;
2710 bus_connections_get_total_bus_names (BusConnections *connections)
2712 return connections->total_bus_names;
2716 bus_connections_get_peak_bus_names (BusConnections *connections)
2718 return connections->peak_bus_names;
2722 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2724 return connections->peak_bus_names_per_conn;
2728 bus_connection_get_peak_match_rules (DBusConnection *connection)
2730 BusConnectionData *d;
2732 d = BUS_CONNECTION_DATA (connection);
2733 _dbus_assert(d != NULL);
2735 return d->peak_match_rules;
2739 bus_connection_get_peak_bus_names (DBusConnection *connection)
2741 BusConnectionData *d;
2743 d = BUS_CONNECTION_DATA (connection);
2744 _dbus_assert(d != NULL);
2746 return d->peak_bus_names;
2748 #endif /* DBUS_ENABLE_STATS */
2751 bus_connection_is_monitor (DBusConnection *connection)
2753 BusConnectionData *d;
2755 d = BUS_CONNECTION_DATA (connection);
2756 _dbus_assert(d != NULL);
2758 return d->link_in_monitors != NULL;
2762 bcd_add_monitor_rules (BusConnectionData *d,
2763 DBusConnection *connection,
2766 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2771 mm = bus_matchmaker_new ();
2776 d->connections->monitor_matchmaker = mm;
2779 for (iter = _dbus_list_get_first_link (rules);
2781 iter = _dbus_list_get_next_link (rules, iter))
2783 if (!bus_matchmaker_add_rule (mm, iter->data))
2785 bus_matchmaker_disconnected (mm, connection);
2794 bcd_drop_monitor_rules (BusConnectionData *d,
2795 DBusConnection *connection)
2797 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2800 bus_matchmaker_disconnected (mm, connection);
2804 bus_connection_be_monitor (DBusConnection *connection,
2805 BusTransaction *transaction,
2809 BusConnectionData *d;
2814 d = BUS_CONNECTION_DATA (connection);
2815 _dbus_assert (d != NULL);
2817 link = _dbus_list_alloc_link (connection);
2821 BUS_SET_OOM (error);
2825 if (!bcd_add_monitor_rules (d, connection, rules))
2827 _dbus_list_free_link (link);
2828 BUS_SET_OOM (error);
2832 /* release all its names */
2833 if (!_dbus_list_copy (&d->services_owned, &tmp))
2835 bcd_drop_monitor_rules (d, connection);
2836 _dbus_list_free_link (link);
2837 BUS_SET_OOM (error);
2841 for (iter = _dbus_list_get_first_link (&tmp);
2843 iter = _dbus_list_get_next_link (&tmp, iter))
2845 BusService *service = iter->data;
2847 /* This call is transactional: if there isn't enough memory to
2848 * do everything, then the service gets all its names back when
2849 * the transaction is cancelled due to OOM. */
2850 if (!bus_service_remove_owner (service, connection, transaction, error))
2852 bcd_drop_monitor_rules (d, connection);
2853 _dbus_list_free_link (link);
2854 _dbus_list_clear (&tmp);
2859 /* We have now done everything that can fail, so there is no problem
2860 * with doing the irrevocable stuff. */
2862 _dbus_list_clear (&tmp);
2864 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2865 "Connection %s (%s) became a monitor.", d->name,
2866 d->cached_loginfo_string);
2868 if (d->n_match_rules > 0)
2872 mm = bus_context_get_matchmaker (d->connections->context);
2873 bus_matchmaker_disconnected (mm, connection);
2876 /* flag it as a monitor */
2877 d->link_in_monitors = link;
2878 _dbus_list_append_link (&d->connections->monitors, link);
2880 /* it isn't allowed to reply, and it is no longer relevant whether it
2881 * receives replies */
2882 bus_connection_drop_pending_replies (d->connections, connection);