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 = NULL;
731 d = dbus_new0 (BusConnectionData, 1);
736 d->connections = connections;
737 d->connection = connection;
739 _dbus_get_monotonic_time (&d->connection_tv_sec,
740 &d->connection_tv_usec);
742 _dbus_assert (connection_data_slot >= 0);
744 if (!dbus_connection_set_data (connection,
745 connection_data_slot,
746 d, free_connection_data))
748 /* We have to free d explicitly, because this is the only code
749 * path where it's non-NULL but dbus_connection_set_data() hasn't
750 * taken responsibility for freeing it. */
756 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 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
764 "Unable to set up new connection: %s", error.message);
765 dbus_error_free (&error);
769 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
771 if (dbus_error_is_set (&error))
773 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
774 "Unable to set up new connection: %s", error.message);
775 dbus_error_free (&error);
779 if (!dbus_connection_set_watch_functions (connection,
780 add_connection_watch,
781 remove_connection_watch,
782 toggle_connection_watch,
787 if (!dbus_connection_set_timeout_functions (connection,
788 add_connection_timeout,
789 remove_connection_timeout,
794 /* For now we don't need to set a Windows user function because
795 * there are no policies in the config file controlling what
796 * Windows users can connect. The default 'same user that owns the
797 * bus can connect' behavior of DBusConnection is fine on Windows.
799 dbus_connection_set_unix_user_function (connection,
800 allow_unix_user_function,
803 dbus_connection_set_dispatch_status_function (connection,
804 dispatch_status_function,
805 bus_context_get_loop (connections->context),
808 d->link_in_connection_list = _dbus_list_alloc_link (connection);
809 if (d->link_in_connection_list == NULL)
812 /* Setup the connection with the dispatcher */
813 if (!bus_dispatch_add_connection (connection))
816 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
818 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
820 bus_dispatch_remove_connection (connection);
825 /* Setup pending fds timeout (see #80559) */
826 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
827 pending_unix_fds_timeout_cb,
829 if (d->pending_unix_fds_timeout == NULL)
832 _dbus_timeout_disable (d->pending_unix_fds_timeout);
833 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
834 d->pending_unix_fds_timeout))
837 _dbus_connection_set_pending_fds_function (connection,
838 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
841 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
842 connections->n_incomplete += 1;
844 dbus_connection_ref (connection);
846 bus_connections_expire_incomplete (connections);
848 /* The listening socket is removed from the main loop,
849 * i.e. does not accept(), while n_incomplete is at its
850 * maximum value; so we shouldn't get here in that case */
851 _dbus_assert (connections->n_incomplete <=
852 bus_context_get_max_incomplete_connections (connections->context));
854 /* If we have the maximum number of incomplete connections,
855 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
856 bus_context_check_all_watches (d->connections->context);
861 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
862 "No memory to set up new connection");
867 d->selinux_id = NULL;
869 if (d->apparmor_confinement)
870 bus_apparmor_confinement_unref (d->apparmor_confinement);
871 d->apparmor_confinement = NULL;
873 if (!dbus_connection_set_watch_functions (connection,
877 _dbus_assert_not_reached ("setting watch functions to NULL failed");
879 if (!dbus_connection_set_timeout_functions (connection,
883 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
885 dbus_connection_set_unix_user_function (connection,
888 dbus_connection_set_windows_user_function (connection,
891 dbus_connection_set_dispatch_status_function (connection,
894 if (d->pending_unix_fds_timeout)
895 _dbus_timeout_unref (d->pending_unix_fds_timeout);
897 d->pending_unix_fds_timeout = NULL;
899 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
901 if (d->link_in_connection_list != NULL)
903 _dbus_assert (d->link_in_connection_list->next == NULL);
904 _dbus_assert (d->link_in_connection_list->prev == NULL);
905 _dbus_list_free_link (d->link_in_connection_list);
906 d->link_in_connection_list = NULL;
909 if (!dbus_connection_set_data (connection,
910 connection_data_slot,
912 _dbus_assert_not_reached ("failed to set connection data to null");
914 /* "d" has now been freed */
921 bus_connections_expire_incomplete (BusConnections *connections)
927 if (connections->incomplete != NULL)
929 long tv_sec, tv_usec;
933 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
934 auth_timeout = bus_context_get_auth_timeout (connections->context);
936 link = _dbus_list_get_first_link (&connections->incomplete);
939 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
940 DBusConnection *connection;
941 BusConnectionData *d;
944 connection = link->data;
946 d = BUS_CONNECTION_DATA (connection);
948 _dbus_assert (d != NULL);
950 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
951 d->connection_tv_usec,
954 if (elapsed >= (double) auth_timeout)
956 /* Unfortunately, we can't identify the connection: it doesn't
957 * have a unique name yet, we don't know its uid/pid yet,
959 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
960 "Connection has not authenticated soon enough, closing it "
961 "(auth_timeout=%dms, elapsed: %.0fms)",
962 auth_timeout, elapsed);
964 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
965 dbus_connection_close (connection);
969 /* We can end the loop, since the connections are in oldest-first order */
970 next_interval = ((double)auth_timeout) - elapsed;
971 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
972 connection, next_interval);
981 bus_expire_timeout_set_interval (connections->expire_timeout,
986 expire_incomplete_timeout (void *data)
988 BusConnections *connections = data;
990 _dbus_verbose ("Running\n");
992 /* note that this may remove the timeout */
993 bus_connections_expire_incomplete (connections);
999 bus_connection_get_unix_groups (DBusConnection *connection,
1000 unsigned long **groups,
1009 if (dbus_connection_get_unix_user (connection, &uid))
1011 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
1013 _dbus_verbose ("Did not get any groups for UID %lu\n",
1019 _dbus_verbose ("Got %d groups for UID %lu\n",
1025 return TRUE; /* successfully got 0 groups */
1029 bus_connection_is_in_unix_group (DBusConnection *connection,
1033 unsigned long *group_ids;
1036 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1041 while (i < n_group_ids)
1043 if (group_ids[i] == gid)
1045 dbus_free (group_ids);
1051 dbus_free (group_ids);
1056 bus_connection_get_loginfo (DBusConnection *connection)
1058 BusConnectionData *d;
1060 d = BUS_CONNECTION_DATA (connection);
1061 _dbus_assert(d != NULL);
1063 if (!bus_connection_is_active (connection))
1065 return d->cached_loginfo_string;
1069 bus_connection_get_policy (DBusConnection *connection)
1071 BusConnectionData *d;
1073 d = BUS_CONNECTION_DATA (connection);
1075 _dbus_assert (d != NULL);
1076 _dbus_assert (d->policy != NULL);
1082 foreach_active (BusConnections *connections,
1083 BusConnectionForeachFunction function,
1088 link = _dbus_list_get_first_link (&connections->completed);
1089 while (link != NULL)
1091 DBusConnection *connection = link->data;
1092 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1094 if (!(* function) (connection, data))
1104 foreach_inactive (BusConnections *connections,
1105 BusConnectionForeachFunction function,
1110 link = _dbus_list_get_first_link (&connections->incomplete);
1111 while (link != NULL)
1113 DBusConnection *connection = link->data;
1114 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1116 if (!(* function) (connection, data))
1126 * Calls function on each active connection; if the function returns
1127 * #FALSE, stops iterating. Active connections are authenticated
1128 * and have sent a Hello message.
1130 * @param connections the connections object
1131 * @param function the function
1132 * @param data data to pass to it as a second arg
1135 bus_connections_foreach_active (BusConnections *connections,
1136 BusConnectionForeachFunction function,
1139 foreach_active (connections, function, data);
1143 * Calls function on each connection; if the function returns
1144 * #FALSE, stops iterating.
1146 * @param connections the connections object
1147 * @param function the function
1148 * @param data data to pass to it as a second arg
1151 bus_connections_foreach (BusConnections *connections,
1152 BusConnectionForeachFunction function,
1155 if (!foreach_active (connections, function, data))
1158 foreach_inactive (connections, function, data);
1162 bus_connections_get_context (BusConnections *connections)
1164 return connections->context;
1168 * This is used to avoid covering the same connection twice when
1169 * traversing connections. Note that it assumes we will
1170 * bus_connection_mark_stamp() each connection at least once per
1171 * INT_MAX increments of the global stamp, or wraparound would break
1175 bus_connections_increment_stamp (BusConnections *connections)
1177 connections->stamp += 1;
1180 /* Mark connection with current stamp, return TRUE if it
1181 * didn't already have that stamp
1184 bus_connection_mark_stamp (DBusConnection *connection)
1186 BusConnectionData *d;
1188 d = BUS_CONNECTION_DATA (connection);
1190 _dbus_assert (d != NULL);
1192 if (d->stamp == d->connections->stamp)
1196 d->stamp = d->connections->stamp;
1202 bus_connection_get_context (DBusConnection *connection)
1204 BusConnectionData *d;
1206 d = BUS_CONNECTION_DATA (connection);
1208 _dbus_assert (d != NULL);
1210 return d->connections->context;
1214 bus_connection_get_connections (DBusConnection *connection)
1216 BusConnectionData *d;
1218 d = BUS_CONNECTION_DATA (connection);
1220 _dbus_assert (d != NULL);
1222 return d->connections;
1226 bus_connection_get_registry (DBusConnection *connection)
1228 BusConnectionData *d;
1230 d = BUS_CONNECTION_DATA (connection);
1232 _dbus_assert (d != NULL);
1234 return bus_context_get_registry (d->connections->context);
1238 bus_connection_get_activation (DBusConnection *connection)
1240 BusConnectionData *d;
1242 d = BUS_CONNECTION_DATA (connection);
1244 _dbus_assert (d != NULL);
1246 return bus_context_get_activation (d->connections->context);
1250 bus_connection_get_matchmaker (DBusConnection *connection)
1252 BusConnectionData *d;
1254 d = BUS_CONNECTION_DATA (connection);
1256 _dbus_assert (d != NULL);
1258 return bus_context_get_matchmaker (d->connections->context);
1262 bus_connection_get_selinux_id (DBusConnection *connection)
1264 BusConnectionData *d;
1266 d = BUS_CONNECTION_DATA (connection);
1268 _dbus_assert (d != NULL);
1270 return d->selinux_id;
1273 BusAppArmorConfinement*
1274 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1276 BusConnectionData *d;
1278 d = BUS_CONNECTION_DATA (connection);
1280 _dbus_assert (d != NULL);
1282 bus_apparmor_confinement_ref (d->apparmor_confinement);
1283 return d->apparmor_confinement;
1287 * Checks whether the connection is registered with the message bus.
1289 * @param connection the connection
1290 * @returns #TRUE if we're an active message bus participant
1293 bus_connection_is_active (DBusConnection *connection)
1295 BusConnectionData *d;
1297 d = BUS_CONNECTION_DATA (connection);
1298 _dbus_assert(d != NULL);
1300 return d->name != NULL;
1304 bus_connection_preallocate_oom_error (DBusConnection *connection)
1306 DBusMessage *message;
1307 DBusPreallocatedSend *preallocated;
1308 BusConnectionData *d;
1310 d = BUS_CONNECTION_DATA (connection);
1312 _dbus_assert (d != NULL);
1314 if (d->oom_preallocated != NULL)
1317 preallocated = dbus_connection_preallocate_send (connection);
1318 if (preallocated == NULL)
1321 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1323 if (message == NULL)
1325 dbus_connection_free_preallocated_send (connection, preallocated);
1329 /* d->name may be NULL, but that is OK */
1330 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1331 !dbus_message_set_destination (message, d->name) ||
1332 !dbus_message_set_sender (message,
1335 dbus_connection_free_preallocated_send (connection, preallocated);
1336 dbus_message_unref (message);
1340 /* set reply serial to placeholder value just so space is already allocated
1343 if (!dbus_message_set_reply_serial (message, 14))
1345 dbus_connection_free_preallocated_send (connection, preallocated);
1346 dbus_message_unref (message);
1350 d->oom_message = message;
1351 d->oom_preallocated = preallocated;
1357 bus_connection_send_oom_error (DBusConnection *connection,
1358 DBusMessage *in_reply_to)
1360 BusConnectionData *d;
1362 d = BUS_CONNECTION_DATA (connection);
1364 _dbus_assert (d != NULL);
1365 _dbus_assert (d->oom_message != NULL);
1367 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1368 "dbus-daemon transaction failed (OOM), sending error to "
1369 "sender %s", bus_connection_get_loginfo (connection));
1371 /* should always succeed since we set it to a placeholder earlier */
1372 if (!dbus_message_set_reply_serial (d->oom_message,
1373 dbus_message_get_serial (in_reply_to)))
1374 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1376 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1378 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1379 d->oom_message, NULL);
1381 dbus_message_unref (d->oom_message);
1382 d->oom_message = NULL;
1383 d->oom_preallocated = NULL;
1386 #ifdef DBUS_ENABLE_STATS
1388 update_peak (int *peak,
1397 bus_connection_add_match_rule_link (DBusConnection *connection,
1400 BusConnectionData *d;
1402 d = BUS_CONNECTION_DATA (connection);
1403 _dbus_assert (d != NULL);
1405 _dbus_list_append_link (&d->match_rules, link);
1407 d->n_match_rules += 1;
1409 #ifdef DBUS_ENABLE_STATS
1410 update_peak (&d->peak_match_rules, d->n_match_rules);
1411 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1413 d->connections->total_match_rules += 1;
1414 update_peak (&d->connections->peak_match_rules,
1415 d->connections->total_match_rules);
1420 bus_connection_add_match_rule (DBusConnection *connection,
1425 link = _dbus_list_alloc_link (rule);
1430 bus_connection_add_match_rule_link (connection, link);
1436 bus_connection_remove_match_rule (DBusConnection *connection,
1439 BusConnectionData *d;
1441 d = BUS_CONNECTION_DATA (connection);
1442 _dbus_assert (d != NULL);
1444 _dbus_list_remove_last (&d->match_rules, rule);
1446 d->n_match_rules -= 1;
1447 _dbus_assert (d->n_match_rules >= 0);
1449 #ifdef DBUS_ENABLE_STATS
1450 d->connections->total_match_rules -= 1;
1455 bus_connection_get_n_match_rules (DBusConnection *connection)
1457 BusConnectionData *d;
1459 d = BUS_CONNECTION_DATA (connection);
1460 _dbus_assert (d != NULL);
1462 return d->n_match_rules;
1466 bus_connection_add_owned_service_link (DBusConnection *connection,
1469 BusConnectionData *d;
1471 d = BUS_CONNECTION_DATA (connection);
1472 _dbus_assert (d != NULL);
1474 _dbus_list_append_link (&d->services_owned, link);
1476 d->n_services_owned += 1;
1478 #ifdef DBUS_ENABLE_STATS
1479 update_peak (&d->peak_bus_names, d->n_services_owned);
1480 update_peak (&d->connections->peak_bus_names_per_conn,
1481 d->n_services_owned);
1483 d->connections->total_bus_names += 1;
1484 update_peak (&d->connections->peak_bus_names,
1485 d->connections->total_bus_names);
1490 bus_connection_add_owned_service (DBusConnection *connection,
1491 BusService *service)
1495 link = _dbus_list_alloc_link (service);
1500 bus_connection_add_owned_service_link (connection, link);
1506 bus_connection_remove_owned_service (DBusConnection *connection,
1507 BusService *service)
1509 BusConnectionData *d;
1511 d = BUS_CONNECTION_DATA (connection);
1512 _dbus_assert (d != NULL);
1514 _dbus_list_remove_last (&d->services_owned, service);
1516 d->n_services_owned -= 1;
1517 _dbus_assert (d->n_services_owned >= 0);
1519 #ifdef DBUS_ENABLE_STATS
1520 d->connections->total_bus_names -= 1;
1525 bus_connection_get_n_services_owned (DBusConnection *connection)
1527 BusConnectionData *d;
1529 d = BUS_CONNECTION_DATA (connection);
1530 _dbus_assert (d != NULL);
1532 return d->n_services_owned;
1536 bus_connection_complete (DBusConnection *connection,
1537 const DBusString *name,
1540 BusConnectionData *d;
1543 d = BUS_CONNECTION_DATA (connection);
1544 _dbus_assert (d != NULL);
1545 _dbus_assert (d->name == NULL);
1546 _dbus_assert (d->policy == NULL);
1548 _dbus_assert (!bus_connection_is_active (connection));
1550 if (!_dbus_string_copy_data (name, &d->name))
1552 BUS_SET_OOM (error);
1556 _dbus_assert (d->name != NULL);
1558 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1560 d->policy = bus_context_create_client_policy (d->connections->context,
1564 /* we may have a NULL policy on OOM or error getting list of
1565 * groups for a user. In the latter case we don't handle it so
1566 * well currently, as it will just keep failing over and over.
1569 if (d->policy == NULL)
1571 _dbus_verbose ("Failed to create security policy for connection %p\n",
1573 _DBUS_ASSERT_ERROR_IS_SET (error);
1574 dbus_free (d->name);
1579 if (dbus_connection_get_unix_user (connection, &uid))
1581 if (!adjust_connections_for_uid (d->connections,
1586 /* Create and cache a string which holds information about the
1587 * peer process; used for logging purposes.
1589 if (!cache_peer_loginfo_string (d, connection))
1592 /* Now the connection is active, move it between lists */
1593 _dbus_list_unlink (&d->connections->incomplete,
1594 d->link_in_connection_list);
1595 d->connections->n_incomplete -= 1;
1596 _dbus_list_append_link (&d->connections->completed,
1597 d->link_in_connection_list);
1598 d->connections->n_completed += 1;
1600 _dbus_assert (d->connections->n_incomplete >= 0);
1601 _dbus_assert (d->connections->n_completed > 0);
1603 /* If we have dropped below the max. number of incomplete
1604 * connections, start accept()ing again */
1605 bus_context_check_all_watches (d->connections->context);
1607 /* See if we can remove the timeout */
1608 bus_connections_expire_incomplete (d->connections);
1610 _dbus_assert (bus_connection_is_active (connection));
1614 BUS_SET_OOM (error);
1615 dbus_free (d->name);
1618 bus_client_policy_unref (d->policy);
1624 bus_connections_reload_policy (BusConnections *connections,
1627 BusConnectionData *d;
1628 DBusConnection *connection;
1631 _dbus_assert (connections != NULL);
1632 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1634 for (link = _dbus_list_get_first_link (&(connections->completed));
1636 link = _dbus_list_get_next_link (&(connections->completed), link))
1638 connection = link->data;
1639 d = BUS_CONNECTION_DATA (connection);
1640 _dbus_assert (d != NULL);
1641 _dbus_assert (d->policy != NULL);
1643 bus_client_policy_unref (d->policy);
1644 d->policy = bus_context_create_client_policy (connections->context,
1647 if (d->policy == NULL)
1649 _dbus_verbose ("Failed to create security policy for connection %p\n",
1651 _DBUS_ASSERT_ERROR_IS_SET (error);
1660 bus_connection_get_name (DBusConnection *connection)
1662 BusConnectionData *d;
1664 d = BUS_CONNECTION_DATA (connection);
1665 _dbus_assert (d != NULL);
1671 * Check whether completing the passed-in connection would
1672 * exceed limits, and if so set error and return #FALSE
1675 bus_connections_check_limits (BusConnections *connections,
1676 DBusConnection *requesting_completion,
1677 const char **limit_name_out,
1684 limit = bus_context_get_max_completed_connections (connections->context);
1686 if (connections->n_completed >= limit)
1688 if (limit_name_out != NULL)
1689 *limit_name_out = "max_completed_connections";
1691 if (limit_out != NULL)
1694 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1695 "The maximum number of active connections has been reached");
1699 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1701 limit = bus_context_get_max_connections_per_user (connections->context);
1703 if (get_connections_for_uid (connections, uid) >= limit)
1705 if (limit_name_out != NULL)
1706 *limit_name_out = "max_connections_per_user";
1708 if (limit_out != NULL)
1711 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1712 "The maximum number of active connections for UID %lu has been reached",
1722 bus_pending_reply_free (BusPendingReply *pending)
1724 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1726 pending->will_send_reply,
1727 pending->will_get_reply,
1728 pending->reply_serial);
1730 dbus_free (pending);
1734 bus_pending_reply_send_no_reply (BusConnections *connections,
1735 BusTransaction *transaction,
1736 BusPendingReply *pending)
1738 DBusMessage *message;
1739 DBusMessageIter iter;
1745 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1746 if (message == NULL)
1749 dbus_message_set_no_reply (message, TRUE);
1751 if (!dbus_message_set_reply_serial (message,
1752 pending->reply_serial))
1755 if (!dbus_message_set_error_name (message,
1756 DBUS_ERROR_NO_REPLY))
1759 /* If you change these messages, adjust test/dbus-daemon.c to match */
1760 if (pending->will_send_reply == NULL)
1761 errmsg = "Message recipient disconnected from message bus without replying";
1763 errmsg = "Message did not receive a reply (timeout by message bus)";
1765 dbus_message_iter_init_append (message, &iter);
1766 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1769 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1776 dbus_message_unref (message);
1781 bus_pending_reply_expired (BusExpireList *list,
1785 BusPendingReply *pending = link->data;
1786 BusConnections *connections = data;
1787 BusTransaction *transaction;
1789 /* No reply is forthcoming. So nuke it if we can. If not,
1790 * leave it in the list to try expiring again later when we
1794 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1796 pending->will_send_reply,
1797 pending->will_get_reply,
1798 pending->reply_serial);
1800 transaction = bus_transaction_new (connections->context);
1801 if (transaction == NULL)
1804 if (!bus_pending_reply_send_no_reply (connections,
1808 bus_transaction_cancel_and_free (transaction);
1812 bus_expire_list_remove_link (connections->pending_replies, link);
1814 bus_pending_reply_free (pending);
1815 bus_transaction_execute_and_free (transaction);
1821 bus_connection_drop_pending_replies (BusConnections *connections,
1822 DBusConnection *connection)
1824 /* The DBusConnection is almost 100% finalized here, so you can't
1825 * do anything with it except check for pointer equality
1829 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1832 link = bus_expire_list_get_first_link (connections->pending_replies);
1833 while (link != NULL)
1836 BusPendingReply *pending;
1838 next = bus_expire_list_get_next_link (connections->pending_replies,
1840 pending = link->data;
1842 if (pending->will_get_reply == connection)
1844 /* We don't need to track this pending reply anymore */
1846 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1848 pending->will_send_reply,
1849 pending->will_get_reply,
1850 pending->reply_serial);
1852 bus_expire_list_remove_link (connections->pending_replies,
1854 bus_pending_reply_free (pending);
1856 else if (pending->will_send_reply == connection)
1858 /* The reply isn't going to be sent, so set things
1859 * up so it will be expired right away
1861 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1863 pending->will_send_reply,
1864 pending->will_get_reply,
1865 pending->reply_serial);
1867 pending->will_send_reply = NULL;
1868 pending->expire_item.added_tv_sec = 0;
1869 pending->expire_item.added_tv_usec = 0;
1871 bus_expire_list_recheck_immediately (connections->pending_replies);
1881 BusPendingReply *pending;
1882 BusConnections *connections;
1883 } CancelPendingReplyData;
1886 cancel_pending_reply (void *data)
1888 CancelPendingReplyData *d = data;
1890 _dbus_verbose ("d = %p\n", d);
1892 if (!bus_expire_list_remove (d->connections->pending_replies,
1893 &d->pending->expire_item))
1894 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1896 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1900 cancel_pending_reply_data_free (void *data)
1902 CancelPendingReplyData *d = data;
1904 _dbus_verbose ("d = %p\n", d);
1906 /* d->pending should be either freed or still
1907 * in the list of pending replies (owned by someone
1915 * Record that a reply is allowed; return TRUE on success.
1918 bus_connections_expect_reply (BusConnections *connections,
1919 BusTransaction *transaction,
1920 DBusConnection *will_get_reply,
1921 DBusConnection *will_send_reply,
1922 DBusMessage *reply_to_this,
1925 BusPendingReply *pending;
1926 dbus_uint32_t reply_serial;
1928 CancelPendingReplyData *cprd;
1932 _dbus_assert (will_get_reply != NULL);
1933 _dbus_assert (will_send_reply != NULL);
1934 _dbus_assert (reply_to_this != NULL);
1936 if (dbus_message_get_no_reply (reply_to_this))
1937 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1939 reply_serial = dbus_message_get_serial (reply_to_this);
1941 link = bus_expire_list_get_first_link (connections->pending_replies);
1943 while (link != NULL)
1945 pending = link->data;
1947 if (pending->reply_serial == reply_serial &&
1948 pending->will_get_reply == will_get_reply &&
1949 pending->will_send_reply == will_send_reply)
1951 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1952 "Message has the same reply serial as a currently-outstanding existing method call");
1956 link = bus_expire_list_get_next_link (connections->pending_replies,
1958 if (pending->will_get_reply == will_get_reply)
1962 limit = bus_context_get_max_replies_per_connection (connections->context);
1966 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
1967 "The maximum number of pending replies for "
1968 "\"%s\" (%s) has been reached "
1969 "(max_replies_per_connection=%d)",
1970 bus_connection_get_name (will_get_reply),
1971 bus_connection_get_loginfo (will_get_reply),
1974 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1975 "The maximum number of pending replies per connection has been reached");
1979 pending = dbus_new0 (BusPendingReply, 1);
1980 if (pending == NULL)
1982 BUS_SET_OOM (error);
1986 #ifdef DBUS_ENABLE_VERBOSE_MODE
1987 /* so we can see a not-yet-added pending reply */
1988 pending->expire_item.added_tv_sec = 1;
1989 pending->expire_item.added_tv_usec = 1;
1992 pending->will_get_reply = will_get_reply;
1993 pending->will_send_reply = will_send_reply;
1994 pending->reply_serial = reply_serial;
1996 cprd = dbus_new0 (CancelPendingReplyData, 1);
1999 BUS_SET_OOM (error);
2000 bus_pending_reply_free (pending);
2004 if (!bus_expire_list_add (connections->pending_replies,
2005 &pending->expire_item))
2007 BUS_SET_OOM (error);
2009 bus_pending_reply_free (pending);
2013 if (!bus_transaction_add_cancel_hook (transaction,
2014 cancel_pending_reply,
2016 cancel_pending_reply_data_free))
2018 BUS_SET_OOM (error);
2019 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
2021 bus_pending_reply_free (pending);
2025 cprd->pending = pending;
2026 cprd->connections = connections;
2028 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
2029 &pending->expire_item.added_tv_usec);
2031 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2033 pending->will_send_reply,
2034 pending->will_get_reply,
2035 pending->reply_serial);
2043 BusConnections *connections;
2044 } CheckPendingReplyData;
2047 cancel_check_pending_reply (void *data)
2049 CheckPendingReplyData *d = data;
2051 _dbus_verbose ("d = %p\n",d);
2053 bus_expire_list_add_link (d->connections->pending_replies,
2059 check_pending_reply_data_free (void *data)
2061 CheckPendingReplyData *d = data;
2063 _dbus_verbose ("d = %p\n",d);
2065 if (d->link != NULL)
2067 BusPendingReply *pending = d->link->data;
2069 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2070 &pending->expire_item));
2072 bus_pending_reply_free (pending);
2073 _dbus_list_free_link (d->link);
2080 * Check whether a reply is allowed, remove BusPendingReply
2081 * if so, return TRUE if so.
2084 bus_connections_check_reply (BusConnections *connections,
2085 BusTransaction *transaction,
2086 DBusConnection *sending_reply,
2087 DBusConnection *receiving_reply,
2091 CheckPendingReplyData *cprd;
2093 dbus_uint32_t reply_serial;
2095 _dbus_assert (sending_reply != NULL);
2096 _dbus_assert (receiving_reply != NULL);
2098 reply_serial = dbus_message_get_reply_serial (reply);
2100 link = bus_expire_list_get_first_link (connections->pending_replies);
2101 while (link != NULL)
2103 BusPendingReply *pending = link->data;
2105 if (pending->reply_serial == reply_serial &&
2106 pending->will_get_reply == receiving_reply &&
2107 pending->will_send_reply == sending_reply)
2109 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2113 link = bus_expire_list_get_next_link (connections->pending_replies,
2119 _dbus_verbose ("No pending reply expected\n");
2124 cprd = dbus_new0 (CheckPendingReplyData, 1);
2127 BUS_SET_OOM (error);
2131 if (!bus_transaction_add_cancel_hook (transaction,
2132 cancel_check_pending_reply,
2134 check_pending_reply_data_free))
2136 BUS_SET_OOM (error);
2142 cprd->connections = connections;
2144 bus_expire_list_unlink (connections->pending_replies,
2147 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2155 * Note that this is fairly fragile; in particular, don't try to use
2156 * one transaction across any main loop iterations.
2161 BusTransaction *transaction;
2162 DBusMessage *message;
2163 DBusPreallocatedSend *preallocated;
2168 BusTransactionCancelFunction cancel_function;
2169 DBusFreeFunction free_data_function;
2173 struct BusTransaction
2175 DBusList *connections;
2176 BusContext *context;
2177 DBusList *cancel_hooks;
2181 message_to_send_free (DBusConnection *connection,
2182 MessageToSend *to_send)
2184 if (to_send->message)
2185 dbus_message_unref (to_send->message);
2187 if (to_send->preallocated)
2188 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2190 dbus_free (to_send);
2194 cancel_hook_cancel (void *element,
2197 CancelHook *ch = element;
2199 _dbus_verbose ("Running transaction cancel hook\n");
2201 if (ch->cancel_function)
2202 (* ch->cancel_function) (ch->data);
2206 cancel_hook_free (void *element,
2209 CancelHook *ch = element;
2211 if (ch->free_data_function)
2212 (* ch->free_data_function) (ch->data);
2218 free_cancel_hooks (BusTransaction *transaction)
2220 _dbus_list_foreach (&transaction->cancel_hooks,
2221 cancel_hook_free, NULL);
2223 _dbus_list_clear (&transaction->cancel_hooks);
2227 bus_transaction_new (BusContext *context)
2229 BusTransaction *transaction;
2231 transaction = dbus_new0 (BusTransaction, 1);
2232 if (transaction == NULL)
2235 transaction->context = context;
2241 bus_transaction_get_context (BusTransaction *transaction)
2243 return transaction->context;
2247 * Reserve enough memory to capture the given message if the
2248 * transaction goes through.
2251 bus_transaction_capture (BusTransaction *transaction,
2252 DBusConnection *sender,
2253 DBusConnection *addressed_recipient,
2254 DBusMessage *message)
2256 BusConnections *connections;
2259 DBusList *recipients = NULL;
2260 dbus_bool_t ret = FALSE;
2262 connections = bus_context_get_connections (transaction->context);
2264 /* shortcut: don't compose the message unless someone wants it */
2265 if (connections->monitors == NULL)
2268 mm = connections->monitor_matchmaker;
2269 /* This is non-null if there has ever been a monitor - we don't GC it.
2270 * There's little point, since there is up to 1 per process. */
2271 _dbus_assert (mm != NULL);
2273 if (!bus_matchmaker_get_recipients (mm, connections, sender,
2274 addressed_recipient, message, &recipients))
2277 for (link = _dbus_list_get_first_link (&recipients);
2279 link = _dbus_list_get_next_link (&recipients, link))
2281 DBusConnection *recipient = link->data;
2283 if (!bus_transaction_send (transaction, recipient, message))
2290 _dbus_list_clear (&recipients);
2295 bus_transaction_capture_error_reply (BusTransaction *transaction,
2296 DBusConnection *addressed_recipient,
2297 const DBusError *error,
2298 DBusMessage *in_reply_to)
2300 BusConnections *connections;
2302 dbus_bool_t ret = FALSE;
2304 _dbus_assert (error != NULL);
2305 _DBUS_ASSERT_ERROR_IS_SET (error);
2307 connections = bus_context_get_connections (transaction->context);
2309 /* shortcut: don't compose the message unless someone wants it */
2310 if (connections->monitors == NULL)
2313 reply = dbus_message_new_error (in_reply_to,
2320 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2323 ret = bus_transaction_capture (transaction, NULL, addressed_recipient, reply);
2326 dbus_message_unref (reply);
2331 bus_transaction_send_from_driver (BusTransaction *transaction,
2332 DBusConnection *connection,
2333 DBusMessage *message)
2335 DBusError error = DBUS_ERROR_INIT;
2337 /* We have to set the sender to the driver, and have
2338 * to check security policy since it was not done in
2341 _dbus_verbose ("Sending %s %s %s from driver\n",
2342 dbus_message_get_interface (message) ?
2343 dbus_message_get_interface (message) : "(no interface)",
2344 dbus_message_get_member (message) ?
2345 dbus_message_get_member (message) : "(no member)",
2346 dbus_message_get_error_name (message) ?
2347 dbus_message_get_error_name (message) : "(no error name)");
2349 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2352 if (bus_connection_is_active (connection))
2354 if (!dbus_message_set_destination (message,
2355 bus_connection_get_name (connection)))
2359 /* bus driver never wants a reply */
2360 dbus_message_set_no_reply (message, TRUE);
2362 /* Capture it for monitors, even if the real recipient's receive policy
2363 * does not allow it to receive this message from us (which would be odd).
2365 if (!bus_transaction_capture (transaction, NULL, connection, message))
2368 /* If security policy doesn't allow the message, we would silently
2369 * eat it; the driver doesn't care about getting a reply. However,
2370 * if we're actively capturing messages, it's nice to log that we
2371 * tried to send it and did not allow ourselves to do so.
2373 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2375 NULL, connection, connection,
2376 message, NULL, &error))
2378 if (!bus_transaction_capture_error_reply (transaction, connection,
2381 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2382 "message from dbus-daemon rejected but not enough "
2383 "memory to capture it");
2386 /* This is not fatal to the transaction so silently eat the disallowed
2387 * message (see reasoning above) */
2388 dbus_error_free (&error);
2392 return bus_transaction_send (transaction, connection, message);
2396 bus_transaction_send (BusTransaction *transaction,
2397 DBusConnection *connection,
2398 DBusMessage *message)
2400 MessageToSend *to_send;
2401 BusConnectionData *d;
2404 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2405 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2406 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2408 dbus_message_get_interface (message) ?
2409 dbus_message_get_interface (message) : "(unset)",
2410 dbus_message_get_member (message) ?
2411 dbus_message_get_member (message) : "(unset)",
2412 dbus_message_get_error_name (message) ?
2413 dbus_message_get_error_name (message) : "(unset)",
2414 dbus_connection_get_is_connected (connection) ?
2415 "" : " (disconnected)");
2417 _dbus_assert (dbus_message_get_sender (message) != NULL);
2419 if (!dbus_connection_get_is_connected (connection))
2420 return TRUE; /* silently ignore disconnected connections */
2422 d = BUS_CONNECTION_DATA (connection);
2423 _dbus_assert (d != NULL);
2425 to_send = dbus_new (MessageToSend, 1);
2426 if (to_send == NULL)
2431 to_send->preallocated = dbus_connection_preallocate_send (connection);
2432 if (to_send->preallocated == NULL)
2434 dbus_free (to_send);
2438 dbus_message_ref (message);
2439 to_send->message = message;
2440 to_send->transaction = transaction;
2442 _dbus_verbose ("about to prepend message\n");
2444 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2446 message_to_send_free (connection, to_send);
2450 _dbus_verbose ("prepended message\n");
2452 /* See if we already had this connection in the list
2453 * for this transaction. If we have a pending message,
2454 * then we should already be in transaction->connections
2456 link = _dbus_list_get_first_link (&d->transaction_messages);
2457 _dbus_assert (link->data == to_send);
2458 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2459 while (link != NULL)
2461 MessageToSend *m = link->data;
2462 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2464 if (m->transaction == transaction)
2472 if (!_dbus_list_prepend (&transaction->connections, connection))
2474 _dbus_list_remove (&d->transaction_messages, to_send);
2475 message_to_send_free (connection, to_send);
2484 transaction_free (BusTransaction *transaction)
2486 _dbus_assert (transaction->connections == NULL);
2488 free_cancel_hooks (transaction);
2490 dbus_free (transaction);
2494 connection_cancel_transaction (DBusConnection *connection,
2495 BusTransaction *transaction)
2498 BusConnectionData *d;
2500 d = BUS_CONNECTION_DATA (connection);
2501 _dbus_assert (d != NULL);
2503 link = _dbus_list_get_first_link (&d->transaction_messages);
2504 while (link != NULL)
2506 MessageToSend *m = link->data;
2507 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2509 if (m->transaction == transaction)
2511 _dbus_list_remove_link (&d->transaction_messages,
2514 message_to_send_free (connection, m);
2522 bus_transaction_cancel_and_free (BusTransaction *transaction)
2524 DBusConnection *connection;
2526 _dbus_verbose ("TRANSACTION: cancelled\n");
2528 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2529 connection_cancel_transaction (connection, transaction);
2531 _dbus_list_foreach (&transaction->cancel_hooks,
2532 cancel_hook_cancel, NULL);
2534 transaction_free (transaction);
2538 connection_execute_transaction (DBusConnection *connection,
2539 BusTransaction *transaction)
2542 BusConnectionData *d;
2544 d = BUS_CONNECTION_DATA (connection);
2545 _dbus_assert (d != NULL);
2547 /* Send the queue in order (FIFO) */
2548 link = _dbus_list_get_last_link (&d->transaction_messages);
2549 while (link != NULL)
2551 MessageToSend *m = link->data;
2552 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2554 if (m->transaction == transaction)
2556 _dbus_list_remove_link (&d->transaction_messages,
2559 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2561 dbus_connection_send_preallocated (connection,
2566 m->preallocated = NULL; /* so we don't double-free it */
2568 message_to_send_free (connection, m);
2576 bus_transaction_execute_and_free (BusTransaction *transaction)
2578 /* For each connection in transaction->connections
2581 DBusConnection *connection;
2583 _dbus_verbose ("TRANSACTION: executing\n");
2585 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2586 connection_execute_transaction (connection, transaction);
2588 transaction_free (transaction);
2592 bus_connection_remove_transactions (DBusConnection *connection)
2594 MessageToSend *to_send;
2595 BusConnectionData *d;
2597 d = BUS_CONNECTION_DATA (connection);
2598 _dbus_assert (d != NULL);
2600 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2602 /* only has an effect for the first MessageToSend listing this transaction */
2603 _dbus_list_remove (&to_send->transaction->connections,
2606 _dbus_list_remove (&d->transaction_messages, to_send);
2607 message_to_send_free (connection, to_send);
2612 * Converts the DBusError to a message reply
2615 bus_transaction_send_error_reply (BusTransaction *transaction,
2616 DBusConnection *connection,
2617 const DBusError *error,
2618 DBusMessage *in_reply_to)
2622 _dbus_assert (error != NULL);
2623 _DBUS_ASSERT_ERROR_IS_SET (error);
2625 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2626 error->name, error->message);
2628 reply = dbus_message_new_error (in_reply_to,
2634 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2636 dbus_message_unref (reply);
2640 dbus_message_unref (reply);
2646 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2647 BusTransactionCancelFunction cancel_function,
2649 DBusFreeFunction free_data_function)
2653 ch = dbus_new (CancelHook, 1);
2657 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2658 cancel_function, data);
2660 ch->cancel_function = cancel_function;
2662 ch->free_data_function = free_data_function;
2664 /* It's important that the hooks get run in reverse order that they
2667 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2677 bus_connections_get_n_active (BusConnections *connections)
2679 return connections->n_completed;
2683 bus_connections_get_n_incomplete (BusConnections *connections)
2685 return connections->n_incomplete;
2688 #ifdef DBUS_ENABLE_STATS
2690 bus_connections_get_total_match_rules (BusConnections *connections)
2692 return connections->total_match_rules;
2696 bus_connections_get_peak_match_rules (BusConnections *connections)
2698 return connections->peak_match_rules;
2702 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2704 return connections->peak_match_rules_per_conn;
2708 bus_connections_get_total_bus_names (BusConnections *connections)
2710 return connections->total_bus_names;
2714 bus_connections_get_peak_bus_names (BusConnections *connections)
2716 return connections->peak_bus_names;
2720 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2722 return connections->peak_bus_names_per_conn;
2726 bus_connection_get_peak_match_rules (DBusConnection *connection)
2728 BusConnectionData *d;
2730 d = BUS_CONNECTION_DATA (connection);
2731 _dbus_assert(d != NULL);
2733 return d->peak_match_rules;
2737 bus_connection_get_peak_bus_names (DBusConnection *connection)
2739 BusConnectionData *d;
2741 d = BUS_CONNECTION_DATA (connection);
2742 _dbus_assert(d != NULL);
2744 return d->peak_bus_names;
2746 #endif /* DBUS_ENABLE_STATS */
2749 bus_connection_is_monitor (DBusConnection *connection)
2751 BusConnectionData *d;
2753 d = BUS_CONNECTION_DATA (connection);
2754 _dbus_assert(d != NULL);
2756 return d->link_in_monitors != NULL;
2760 bcd_add_monitor_rules (BusConnectionData *d,
2761 DBusConnection *connection,
2764 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2769 mm = bus_matchmaker_new ();
2774 d->connections->monitor_matchmaker = mm;
2777 for (iter = _dbus_list_get_first_link (rules);
2779 iter = _dbus_list_get_next_link (rules, iter))
2781 if (!bus_matchmaker_add_rule (mm, iter->data))
2783 bus_matchmaker_disconnected (mm, connection);
2792 bcd_drop_monitor_rules (BusConnectionData *d,
2793 DBusConnection *connection)
2795 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2798 bus_matchmaker_disconnected (mm, connection);
2802 bus_connection_be_monitor (DBusConnection *connection,
2803 BusTransaction *transaction,
2807 BusConnectionData *d;
2812 d = BUS_CONNECTION_DATA (connection);
2813 _dbus_assert (d != NULL);
2815 link = _dbus_list_alloc_link (connection);
2819 BUS_SET_OOM (error);
2823 if (!bcd_add_monitor_rules (d, connection, rules))
2825 _dbus_list_free_link (link);
2826 BUS_SET_OOM (error);
2830 /* release all its names */
2831 if (!_dbus_list_copy (&d->services_owned, &tmp))
2833 bcd_drop_monitor_rules (d, connection);
2834 _dbus_list_free_link (link);
2835 BUS_SET_OOM (error);
2839 for (iter = _dbus_list_get_first_link (&tmp);
2841 iter = _dbus_list_get_next_link (&tmp, iter))
2843 BusService *service = iter->data;
2845 /* This call is transactional: if there isn't enough memory to
2846 * do everything, then the service gets all its names back when
2847 * the transaction is cancelled due to OOM. */
2848 if (!bus_service_remove_owner (service, connection, transaction, error))
2850 bcd_drop_monitor_rules (d, connection);
2851 _dbus_list_free_link (link);
2852 _dbus_list_clear (&tmp);
2857 /* We have now done everything that can fail, so there is no problem
2858 * with doing the irrevocable stuff. */
2860 _dbus_list_clear (&tmp);
2862 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2863 "Connection %s (%s) became a monitor.", d->name,
2864 d->cached_loginfo_string);
2866 if (d->n_match_rules > 0)
2870 mm = bus_context_get_matchmaker (d->connections->context);
2871 bus_matchmaker_disconnected (mm, connection);
2874 /* flag it as a monitor */
2875 d->link_in_monitors = link;
2876 _dbus_list_append_link (&d->connections->monitors, link);
2878 /* it isn't allowed to reply, and it is no longer relevant whether it
2879 * receives replies */
2880 bus_connection_drop_pending_replies (d->connections, connection);