1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "connection.h"
31 #include "expirelist.h"
35 #include <dbus/dbus-list.h>
36 #include <dbus/dbus-hash.h>
37 #include <dbus/dbus-timeout.h>
38 #include <dbus/dbus-connection-internal.h>
39 #include <dbus/dbus-internals.h>
40 #include <dbus/dbus-message-internal.h>
41 #ifdef DBUS_ENABLE_CYNARA
43 #include <cynara-session.h>
47 /* Trim executed commands to this length; we want to keep logs readable */
48 #define MAX_LOG_COMMAND_LEN 50
50 static void bus_connection_remove_transactions (DBusConnection *connection);
54 BusExpireItem expire_item;
56 DBusConnection *will_get_reply;
57 DBusConnection *will_send_reply;
59 dbus_uint32_t reply_serial;
66 DBusList *completed; /**< List of all completed connections */
67 int n_completed; /**< Length of completed list */
68 DBusList *incomplete; /**< List of all not-yet-active connections */
69 int n_incomplete; /**< Length of incomplete list */
71 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
72 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
73 int stamp; /**< Incrementing number */
74 BusExpireList *pending_replies; /**< List of pending replies */
76 /** List of all monitoring connections, a subset of completed.
77 * Each member is a #DBusConnection. */
79 BusMatchmaker *monitor_matchmaker;
81 #ifdef DBUS_ENABLE_STATS
82 int total_match_rules;
84 int peak_match_rules_per_conn;
88 int peak_bus_names_per_conn;
92 static dbus_int32_t connection_data_slot = -1;
96 BusConnections *connections;
97 DBusList *link_in_connection_list;
98 DBusConnection *connection;
99 DBusList *services_owned;
100 int n_services_owned;
101 DBusList *match_rules;
104 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
105 DBusMessage *oom_message;
106 DBusPreallocatedSend *oom_preallocated;
107 BusClientPolicy *policy;
108 DBusList *deferred_messages; /**< Queue of messages deferred due to pending policy check */
110 char *cached_loginfo_string;
111 BusSELinuxID *selinux_id;
112 BusAppArmorConfinement *apparmor_confinement;
114 long connection_tv_sec; /**< Time when we connected (seconds component) */
115 long connection_tv_usec; /**< Time when we connected (microsec component) */
116 int stamp; /**< connections->stamp last time we were traversed */
118 #ifdef DBUS_ENABLE_STATS
119 int peak_match_rules;
122 int n_pending_unix_fds;
123 DBusTimeout *pending_unix_fds_timeout;
125 /** non-NULL if and only if this is a monitor */
126 DBusList *link_in_monitors;
127 #ifdef DBUS_ENABLE_CYNARA
128 char *cynara_session_id;
132 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
136 static void bus_connection_drop_pending_replies (BusConnections *connections,
137 DBusConnection *connection);
139 static dbus_bool_t expire_incomplete_timeout (void *data);
141 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
144 bus_connection_get_loop (DBusConnection *connection)
146 BusConnectionData *d;
148 d = BUS_CONNECTION_DATA (connection);
149 _dbus_assert(d != NULL);
151 return bus_context_get_loop (d->connections->context);
156 get_connections_for_uid (BusConnections *connections,
162 /* val is NULL is 0 when it isn't in the hash yet */
164 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
167 current_count = _DBUS_POINTER_TO_INT (val);
169 return current_count;
173 adjust_connections_for_uid (BusConnections *connections,
179 current_count = get_connections_for_uid (connections, uid);
181 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
182 ": was %d adjustment %d making %d\n",
183 uid, current_count, adjustment, current_count + adjustment);
185 _dbus_assert (current_count >= 0);
187 current_count += adjustment;
189 _dbus_assert (current_count >= 0);
191 if (current_count == 0)
193 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
200 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
201 uid, _DBUS_INT_TO_POINTER (current_count));
203 /* only positive adjustment can fail as otherwise
204 * a hash entry should already exist
206 _dbus_assert (adjustment > 0 ||
207 (adjustment <= 0 && retval));
214 bus_connection_disconnected (DBusConnection *connection)
216 BusConnectionData *d;
218 BusMatchmaker *matchmaker;
220 d = BUS_CONNECTION_DATA (connection);
221 _dbus_assert (d != NULL);
223 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
224 d->name ? d->name : "(inactive)");
226 /* Delete our match rules */
227 if (d->n_match_rules > 0)
229 matchmaker = bus_context_get_matchmaker (d->connections->context);
230 bus_matchmaker_disconnected (matchmaker, connection);
233 /* Drop any service ownership. Unfortunately, this requires
234 * memory allocation and there doesn't seem to be a good way to
235 * handle it other than sleeping; we can't "fail" the operation of
236 * disconnecting a client, and preallocating a broadcast "service is
237 * now gone" message for every client-service pair seems kind of
240 while ((service = _dbus_list_get_last (&d->services_owned)))
242 BusTransaction *transaction;
247 dbus_error_init (&error);
249 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
250 _dbus_wait_for_memory ();
252 if (!bus_service_remove_owner (service, connection,
253 transaction, &error))
255 _DBUS_ASSERT_ERROR_IS_SET (&error);
257 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
259 dbus_error_free (&error);
260 bus_transaction_cancel_and_free (transaction);
261 _dbus_wait_for_memory ();
266 _dbus_verbose ("Failed to remove service owner: %s %s\n",
267 error.name, error.message);
268 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
272 bus_transaction_execute_and_free (transaction);
275 bus_connection_clear_deferred_messages(connection);
277 bus_dispatch_remove_connection (connection);
279 /* no more watching */
280 if (!dbus_connection_set_watch_functions (connection,
284 _dbus_assert_not_reached ("setting watch functions to NULL failed");
286 if (!dbus_connection_set_timeout_functions (connection,
290 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
292 dbus_connection_set_unix_user_function (connection,
294 dbus_connection_set_windows_user_function (connection,
297 dbus_connection_set_dispatch_status_function (connection,
300 if (d->pending_unix_fds_timeout)
302 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
303 d->pending_unix_fds_timeout);
304 _dbus_timeout_unref (d->pending_unix_fds_timeout);
306 d->pending_unix_fds_timeout = NULL;
307 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
309 bus_connection_remove_transactions (connection);
311 if (d->link_in_monitors != NULL)
313 BusMatchmaker *mm = d->connections->monitor_matchmaker;
316 bus_matchmaker_disconnected (mm, connection);
318 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
319 d->link_in_monitors = NULL;
322 if (d->link_in_connection_list != NULL)
328 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
329 d->link_in_connection_list = NULL;
330 d->connections->n_completed -= 1;
332 if (dbus_connection_get_unix_user (connection, &uid))
334 if (!adjust_connections_for_uid (d->connections,
336 _dbus_assert_not_reached ("adjusting downward should never fail");
341 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
342 d->link_in_connection_list = NULL;
343 d->connections->n_incomplete -= 1;
345 /* If we have dropped below the max. number of incomplete
346 * connections, start accept()ing again */
347 bus_context_check_all_watches (d->connections->context);
350 _dbus_assert (d->connections->n_incomplete >= 0);
351 _dbus_assert (d->connections->n_completed >= 0);
354 bus_connection_drop_pending_replies (d->connections, connection);
356 /* frees "d" as side effect */
357 dbus_connection_set_data (connection,
358 connection_data_slot,
361 dbus_connection_unref (connection);
365 add_connection_watch (DBusWatch *watch,
368 DBusConnection *connection = data;
370 return _dbus_loop_add_watch (bus_connection_get_loop (connection), watch);
374 remove_connection_watch (DBusWatch *watch,
377 DBusConnection *connection = data;
379 _dbus_loop_remove_watch (bus_connection_get_loop (connection), watch);
383 toggle_connection_watch (DBusWatch *watch,
386 DBusConnection *connection = data;
388 _dbus_loop_toggle_watch (bus_connection_get_loop (connection), watch);
392 add_connection_timeout (DBusTimeout *timeout,
395 DBusConnection *connection = data;
397 return _dbus_loop_add_timeout (bus_connection_get_loop (connection), timeout);
401 remove_connection_timeout (DBusTimeout *timeout,
404 DBusConnection *connection = data;
406 _dbus_loop_remove_timeout (bus_connection_get_loop (connection), timeout);
410 dispatch_status_function (DBusConnection *connection,
411 DBusDispatchStatus new_status,
414 DBusLoop *loop = data;
416 if (new_status != DBUS_DISPATCH_COMPLETE)
418 while (!_dbus_loop_queue_dispatch (loop, connection))
419 _dbus_wait_for_memory ();
424 allow_unix_user_function (DBusConnection *connection,
428 BusConnectionData *d;
430 d = BUS_CONNECTION_DATA (connection);
432 _dbus_assert (d != NULL);
434 return bus_context_allow_unix_user (d->connections->context, uid);
438 free_connection_data (void *data)
440 BusConnectionData *d = data;
442 /* services_owned should be NULL since we should be disconnected */
443 _dbus_assert (d->services_owned == NULL);
444 _dbus_assert (d->n_services_owned == 0);
446 _dbus_assert (d->transaction_messages == NULL);
448 if (d->oom_preallocated)
449 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
452 dbus_message_unref (d->oom_message);
455 bus_client_policy_unref (d->policy);
457 if (d->apparmor_confinement)
458 bus_apparmor_confinement_unref (d->apparmor_confinement);
460 dbus_free (d->cached_loginfo_string);
464 #ifdef DBUS_ENABLE_CYNARA
465 free (d->cynara_session_id);
472 bus_connections_new (BusContext *context)
474 BusConnections *connections;
476 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
479 connections = dbus_new0 (BusConnections, 1);
480 if (connections == NULL)
483 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
485 if (connections->completed_by_user == NULL)
488 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
489 expire_incomplete_timeout,
491 if (connections->expire_timeout == NULL)
494 _dbus_timeout_disable (connections->expire_timeout);
496 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
497 bus_context_get_reply_timeout (context),
498 bus_pending_reply_expired,
500 if (connections->pending_replies == NULL)
503 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
504 connections->expire_timeout))
507 connections->refcount = 1;
508 connections->context = context;
513 bus_expire_list_free (connections->pending_replies);
515 _dbus_timeout_unref (connections->expire_timeout);
517 _dbus_hash_table_unref (connections->completed_by_user);
519 dbus_free (connections);
521 dbus_connection_free_data_slot (&connection_data_slot);
527 bus_connections_ref (BusConnections *connections)
529 _dbus_assert (connections->refcount > 0);
530 connections->refcount += 1;
536 bus_connections_unref (BusConnections *connections)
538 _dbus_assert (connections->refcount > 0);
539 connections->refcount -= 1;
540 if (connections->refcount == 0)
542 /* drop all incomplete */
543 while (connections->incomplete != NULL)
545 DBusConnection *connection;
547 connection = connections->incomplete->data;
549 dbus_connection_ref (connection);
550 dbus_connection_close (connection);
551 bus_connection_disconnected (connection);
552 dbus_connection_unref (connection);
555 _dbus_assert (connections->n_incomplete == 0);
557 /* drop all monitors */
558 _dbus_list_clear (&connections->monitors);
560 /* drop all real connections */
561 while (connections->completed != NULL)
563 DBusConnection *connection;
565 connection = connections->completed->data;
567 dbus_connection_ref (connection);
568 dbus_connection_close (connection);
569 bus_connection_disconnected (connection);
570 dbus_connection_unref (connection);
573 _dbus_assert (connections->n_completed == 0);
575 bus_expire_list_free (connections->pending_replies);
577 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
578 connections->expire_timeout);
580 _dbus_timeout_unref (connections->expire_timeout);
582 _dbus_hash_table_unref (connections->completed_by_user);
584 if (connections->monitor_matchmaker != NULL)
585 bus_matchmaker_unref (connections->monitor_matchmaker);
587 dbus_free (connections);
589 dbus_connection_free_data_slot (&connection_data_slot);
593 /* Used for logging */
595 cache_peer_loginfo_string (BusConnectionData *d,
596 DBusConnection *connection)
598 DBusString loginfo_buf;
601 char *windows_sid = NULL, *security_label = NULL;
602 dbus_bool_t prev_added;
604 if (!_dbus_string_init (&loginfo_buf))
608 if (dbus_connection_get_unix_user (connection, &uid))
610 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
616 if (dbus_connection_get_unix_process_id (connection, &pid))
620 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
623 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
625 /* Ignore errors here; we may not have permissions to read the
627 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
628 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
634 if (dbus_connection_get_windows_user (connection, &windows_sid))
636 dbus_bool_t did_append;
640 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
644 did_append = _dbus_string_append_printf (&loginfo_buf,
645 "sid=\"%s\"", windows_sid);
646 dbus_free (windows_sid);
654 if (_dbus_connection_get_linux_security_label (connection, &security_label))
656 dbus_bool_t did_append;
660 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
664 did_append = _dbus_string_append_printf (&loginfo_buf,
665 "label=\"%s\"", security_label);
666 dbus_free (security_label);
667 security_label = NULL;
674 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
677 _dbus_string_free (&loginfo_buf);
681 _dbus_string_free (&loginfo_buf);
682 if (security_label != NULL)
683 dbus_free (security_label);
684 if (windows_sid != NULL)
685 dbus_free (windows_sid);
691 check_pending_fds_cb (DBusConnection *connection)
693 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
694 int n_pending_unix_fds_old;
695 int n_pending_unix_fds_new;
697 _dbus_assert(d != NULL);
699 n_pending_unix_fds_old = d->n_pending_unix_fds;
700 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
702 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
703 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
705 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
707 _dbus_timeout_restart (d->pending_unix_fds_timeout,
708 bus_context_get_pending_fd_timeout (d->connections->context));
711 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
713 _dbus_timeout_disable (d->pending_unix_fds_timeout);
717 d->n_pending_unix_fds = n_pending_unix_fds_new;
721 pending_unix_fds_timeout_cb (void *data)
723 DBusConnection *connection = data;
724 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
727 _dbus_assert (d != NULL);
728 limit = bus_context_get_pending_fd_timeout (d->connections->context);
729 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
730 "Connection \"%s\" (%s) has had Unix fds pending for too long, "
731 "closing it (pending_fd_timeout=%d ms)",
732 d->name != NULL ? d->name : "(null)",
733 bus_connection_get_loginfo (connection),
736 dbus_connection_close (connection);
741 bus_connections_setup_connection (BusConnections *connections,
742 DBusConnection *connection)
745 BusConnectionData *d = NULL;
748 d = dbus_new0 (BusConnectionData, 1);
753 d->connections = connections;
754 d->connection = connection;
756 _dbus_get_monotonic_time (&d->connection_tv_sec,
757 &d->connection_tv_usec);
759 _dbus_assert (connection_data_slot >= 0);
761 if (!dbus_connection_set_data (connection,
762 connection_data_slot,
763 d, free_connection_data))
765 /* We have to free d explicitly, because this is the only code
766 * path where it's non-NULL but dbus_connection_set_data() hasn't
767 * taken responsibility for freeing it. */
773 dbus_connection_set_route_peer_messages (connection, TRUE);
775 dbus_error_init (&error);
776 d->selinux_id = bus_selinux_init_connection_id (connection,
778 if (dbus_error_is_set (&error))
780 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
781 "Unable to set up new connection: %s", error.message);
782 dbus_error_free (&error);
786 d->apparmor_confinement = bus_apparmor_init_connection_confinement (connection,
788 if (dbus_error_is_set (&error))
790 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
791 "Unable to set up new connection: %s", error.message);
792 dbus_error_free (&error);
796 if (!dbus_connection_set_watch_functions (connection,
797 add_connection_watch,
798 remove_connection_watch,
799 toggle_connection_watch,
804 if (!dbus_connection_set_timeout_functions (connection,
805 add_connection_timeout,
806 remove_connection_timeout,
811 /* For now we don't need to set a Windows user function because
812 * there are no policies in the config file controlling what
813 * Windows users can connect. The default 'same user that owns the
814 * bus can connect' behavior of DBusConnection is fine on Windows.
816 dbus_connection_set_unix_user_function (connection,
817 allow_unix_user_function,
820 dbus_connection_set_dispatch_status_function (connection,
821 dispatch_status_function,
822 bus_context_get_loop (connections->context),
825 d->link_in_connection_list = _dbus_list_alloc_link (connection);
826 if (d->link_in_connection_list == NULL)
829 /* Setup the connection with the dispatcher */
830 if (!bus_dispatch_add_connection (connection))
833 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
835 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
837 bus_dispatch_remove_connection (connection);
842 /* Setup pending fds timeout (see #80559) */
843 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
844 pending_unix_fds_timeout_cb,
846 if (d->pending_unix_fds_timeout == NULL)
849 _dbus_timeout_disable (d->pending_unix_fds_timeout);
850 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
851 d->pending_unix_fds_timeout))
854 _dbus_connection_set_pending_fds_function (connection,
855 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
858 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
859 connections->n_incomplete += 1;
861 dbus_connection_ref (connection);
863 bus_connections_expire_incomplete (connections);
865 /* The listening socket is removed from the main loop,
866 * i.e. does not accept(), while n_incomplete is at its
867 * maximum value; so we shouldn't get here in that case */
868 _dbus_assert (connections->n_incomplete <=
869 bus_context_get_max_incomplete_connections (connections->context));
871 /* If we have the maximum number of incomplete connections,
872 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
873 bus_context_check_all_watches (d->connections->context);
878 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
879 "No memory to set up new connection");
884 d->selinux_id = NULL;
886 if (d->apparmor_confinement)
887 bus_apparmor_confinement_unref (d->apparmor_confinement);
888 d->apparmor_confinement = NULL;
890 if (!dbus_connection_set_watch_functions (connection,
894 _dbus_assert_not_reached ("setting watch functions to NULL failed");
896 if (!dbus_connection_set_timeout_functions (connection,
900 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
902 dbus_connection_set_unix_user_function (connection,
905 dbus_connection_set_windows_user_function (connection,
908 dbus_connection_set_dispatch_status_function (connection,
911 if (d->pending_unix_fds_timeout)
912 _dbus_timeout_unref (d->pending_unix_fds_timeout);
914 d->pending_unix_fds_timeout = NULL;
916 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
918 if (d->link_in_connection_list != NULL)
920 _dbus_assert (d->link_in_connection_list->next == NULL);
921 _dbus_assert (d->link_in_connection_list->prev == NULL);
922 _dbus_list_free_link (d->link_in_connection_list);
923 d->link_in_connection_list = NULL;
926 if (!dbus_connection_set_data (connection,
927 connection_data_slot,
929 _dbus_assert_not_reached ("failed to set connection data to null");
931 /* "d" has now been freed */
938 bus_connections_expire_incomplete (BusConnections *connections)
944 if (connections->incomplete != NULL)
946 long tv_sec, tv_usec;
950 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
951 auth_timeout = bus_context_get_auth_timeout (connections->context);
953 link = _dbus_list_get_first_link (&connections->incomplete);
956 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
957 DBusConnection *connection;
958 BusConnectionData *d;
961 connection = link->data;
963 d = BUS_CONNECTION_DATA (connection);
965 _dbus_assert (d != NULL);
967 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
968 d->connection_tv_usec,
971 if (elapsed >= (double) auth_timeout)
973 /* Unfortunately, we can't identify the connection: it doesn't
974 * have a unique name yet, we don't know its uid/pid yet,
976 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
977 "Connection has not authenticated soon enough, closing it "
978 "(auth_timeout=%dms, elapsed: %.0fms)",
979 auth_timeout, elapsed);
981 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
982 dbus_connection_close (connection);
986 /* We can end the loop, since the connections are in oldest-first order */
987 next_interval = ((double)auth_timeout) - elapsed;
988 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
989 connection, next_interval);
998 bus_expire_timeout_set_interval (connections->expire_timeout,
1003 expire_incomplete_timeout (void *data)
1005 BusConnections *connections = data;
1007 _dbus_verbose ("Running\n");
1009 /* note that this may remove the timeout */
1010 bus_connections_expire_incomplete (connections);
1016 bus_connection_get_unix_groups (DBusConnection *connection,
1017 unsigned long **groups,
1026 if (dbus_connection_get_unix_user (connection, &uid))
1028 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
1030 _dbus_verbose ("Did not get any groups for UID %lu\n",
1036 _dbus_verbose ("Got %d groups for UID %lu\n",
1042 return TRUE; /* successfully got 0 groups */
1046 bus_connection_is_in_unix_group (DBusConnection *connection,
1050 unsigned long *group_ids;
1053 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
1058 while (i < n_group_ids)
1060 if (group_ids[i] == gid)
1062 dbus_free (group_ids);
1068 dbus_free (group_ids);
1073 bus_connection_get_loginfo (DBusConnection *connection)
1075 BusConnectionData *d;
1077 d = BUS_CONNECTION_DATA (connection);
1078 _dbus_assert(d != NULL);
1080 if (!bus_connection_is_active (connection))
1082 return d->cached_loginfo_string;
1086 bus_connection_get_policy (DBusConnection *connection)
1088 BusConnectionData *d;
1090 d = BUS_CONNECTION_DATA (connection);
1092 _dbus_assert (d != NULL);
1093 _dbus_assert (d->policy != NULL);
1098 #ifdef DBUS_ENABLE_CYNARA
1099 const char *bus_connection_get_cynara_session_id (DBusConnection *connection)
1101 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
1102 _dbus_assert (d != NULL);
1104 if (d->cynara_session_id == NULL)
1107 if (dbus_connection_get_unix_process_id(connection, &pid))
1108 d->cynara_session_id = cynara_session_from_pid(pid);
1110 /* If client exits as soon as async call, cynara_session_from_pid() returns null.
1111 cynara_session_from_pid checks /proc/pid to verify process */
1112 if (d->cynara_session_id == NULL)
1113 asprintf (&d->cynara_session_id, "/proc/%ld", pid);
1115 return d->cynara_session_id;
1120 foreach_active (BusConnections *connections,
1121 BusConnectionForeachFunction function,
1126 link = _dbus_list_get_first_link (&connections->completed);
1127 while (link != NULL)
1129 DBusConnection *connection = link->data;
1130 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1132 if (!(* function) (connection, data))
1142 foreach_inactive (BusConnections *connections,
1143 BusConnectionForeachFunction function,
1148 link = _dbus_list_get_first_link (&connections->incomplete);
1149 while (link != NULL)
1151 DBusConnection *connection = link->data;
1152 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1154 if (!(* function) (connection, data))
1164 * Calls function on each active connection; if the function returns
1165 * #FALSE, stops iterating. Active connections are authenticated
1166 * and have sent a Hello message.
1168 * @param connections the connections object
1169 * @param function the function
1170 * @param data data to pass to it as a second arg
1173 bus_connections_foreach_active (BusConnections *connections,
1174 BusConnectionForeachFunction function,
1177 foreach_active (connections, function, data);
1181 * Calls function on each connection; if the function returns
1182 * #FALSE, stops iterating.
1184 * @param connections the connections object
1185 * @param function the function
1186 * @param data data to pass to it as a second arg
1189 bus_connections_foreach (BusConnections *connections,
1190 BusConnectionForeachFunction function,
1193 if (!foreach_active (connections, function, data))
1196 foreach_inactive (connections, function, data);
1200 bus_connections_get_context (BusConnections *connections)
1202 return connections->context;
1206 * This is used to avoid covering the same connection twice when
1207 * traversing connections. Note that it assumes we will
1208 * bus_connection_mark_stamp() each connection at least once per
1209 * INT_MAX increments of the global stamp, or wraparound would break
1213 bus_connections_increment_stamp (BusConnections *connections)
1215 connections->stamp += 1;
1218 /* Mark connection with current stamp, return TRUE if it
1219 * didn't already have that stamp
1222 bus_connection_mark_stamp (DBusConnection *connection)
1224 BusConnectionData *d;
1226 d = BUS_CONNECTION_DATA (connection);
1228 _dbus_assert (d != NULL);
1230 if (d->stamp == d->connections->stamp)
1234 d->stamp = d->connections->stamp;
1240 bus_connection_get_context (DBusConnection *connection)
1242 BusConnectionData *d;
1244 d = BUS_CONNECTION_DATA (connection);
1246 _dbus_assert (d != NULL);
1248 return d->connections->context;
1252 bus_connection_get_connections (DBusConnection *connection)
1254 BusConnectionData *d;
1256 d = BUS_CONNECTION_DATA (connection);
1258 _dbus_assert (d != NULL);
1260 return d->connections;
1264 bus_connection_get_registry (DBusConnection *connection)
1266 BusConnectionData *d;
1268 d = BUS_CONNECTION_DATA (connection);
1270 _dbus_assert (d != NULL);
1272 return bus_context_get_registry (d->connections->context);
1276 bus_connection_get_activation (DBusConnection *connection)
1278 BusConnectionData *d;
1280 d = BUS_CONNECTION_DATA (connection);
1282 _dbus_assert (d != NULL);
1284 return bus_context_get_activation (d->connections->context);
1288 bus_connection_get_matchmaker (DBusConnection *connection)
1290 BusConnectionData *d;
1292 d = BUS_CONNECTION_DATA (connection);
1294 _dbus_assert (d != NULL);
1296 return bus_context_get_matchmaker (d->connections->context);
1300 bus_connection_get_selinux_id (DBusConnection *connection)
1302 BusConnectionData *d;
1304 d = BUS_CONNECTION_DATA (connection);
1306 _dbus_assert (d != NULL);
1308 return d->selinux_id;
1311 BusAppArmorConfinement*
1312 bus_connection_dup_apparmor_confinement (DBusConnection *connection)
1314 BusConnectionData *d;
1316 d = BUS_CONNECTION_DATA (connection);
1318 _dbus_assert (d != NULL);
1320 bus_apparmor_confinement_ref (d->apparmor_confinement);
1321 return d->apparmor_confinement;
1325 * Checks whether the connection is registered with the message bus.
1327 * @param connection the connection
1328 * @returns #TRUE if we're an active message bus participant
1331 bus_connection_is_active (DBusConnection *connection)
1333 BusConnectionData *d;
1335 d = BUS_CONNECTION_DATA (connection);
1336 _dbus_assert(d != NULL);
1338 return d->name != NULL;
1342 bus_connection_preallocate_oom_error (DBusConnection *connection)
1344 DBusMessage *message;
1345 DBusPreallocatedSend *preallocated;
1346 BusConnectionData *d;
1348 d = BUS_CONNECTION_DATA (connection);
1350 _dbus_assert (d != NULL);
1352 if (d->oom_preallocated != NULL)
1355 preallocated = dbus_connection_preallocate_send (connection);
1356 if (preallocated == NULL)
1359 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1361 if (message == NULL)
1363 dbus_connection_free_preallocated_send (connection, preallocated);
1367 /* d->name may be NULL, but that is OK */
1368 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1369 !dbus_message_set_destination (message, d->name) ||
1370 !dbus_message_set_sender (message,
1373 dbus_connection_free_preallocated_send (connection, preallocated);
1374 dbus_message_unref (message);
1378 /* set reply serial to placeholder value just so space is already allocated
1381 if (!dbus_message_set_reply_serial (message, 14))
1383 dbus_connection_free_preallocated_send (connection, preallocated);
1384 dbus_message_unref (message);
1388 d->oom_message = message;
1389 d->oom_preallocated = preallocated;
1395 bus_connection_send_oom_error (DBusConnection *connection,
1396 DBusMessage *in_reply_to)
1398 BusConnectionData *d;
1400 d = BUS_CONNECTION_DATA (connection);
1402 _dbus_assert (d != NULL);
1403 _dbus_assert (d->oom_message != NULL);
1405 bus_context_log (d->connections->context, DBUS_SYSTEM_LOG_WARNING,
1406 "dbus-daemon transaction failed (OOM), sending error to "
1407 "sender %s", bus_connection_get_loginfo (connection));
1409 /* should always succeed since we set it to a placeholder earlier */
1410 if (!dbus_message_set_reply_serial (d->oom_message,
1411 dbus_message_get_serial (in_reply_to)))
1412 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1414 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1416 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1417 d->oom_message, NULL);
1419 dbus_message_unref (d->oom_message);
1420 d->oom_message = NULL;
1421 d->oom_preallocated = NULL;
1424 #ifdef DBUS_ENABLE_STATS
1426 update_peak (int *peak,
1435 bus_connection_add_match_rule_link (DBusConnection *connection,
1438 BusConnectionData *d;
1440 d = BUS_CONNECTION_DATA (connection);
1441 _dbus_assert (d != NULL);
1443 _dbus_list_append_link (&d->match_rules, link);
1445 d->n_match_rules += 1;
1447 #ifdef DBUS_ENABLE_STATS
1448 update_peak (&d->peak_match_rules, d->n_match_rules);
1449 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1451 d->connections->total_match_rules += 1;
1452 update_peak (&d->connections->peak_match_rules,
1453 d->connections->total_match_rules);
1458 bus_connection_add_match_rule (DBusConnection *connection,
1463 link = _dbus_list_alloc_link (rule);
1468 bus_connection_add_match_rule_link (connection, link);
1474 bus_connection_remove_match_rule (DBusConnection *connection,
1477 BusConnectionData *d;
1479 d = BUS_CONNECTION_DATA (connection);
1480 _dbus_assert (d != NULL);
1482 _dbus_list_remove_last (&d->match_rules, rule);
1484 d->n_match_rules -= 1;
1485 _dbus_assert (d->n_match_rules >= 0);
1487 #ifdef DBUS_ENABLE_STATS
1488 d->connections->total_match_rules -= 1;
1493 bus_connection_get_n_match_rules (DBusConnection *connection)
1495 BusConnectionData *d;
1497 d = BUS_CONNECTION_DATA (connection);
1498 _dbus_assert (d != NULL);
1500 return d->n_match_rules;
1504 bus_connection_is_service_owner_by_prefix (DBusConnection *connection,
1505 const char *name_prefix)
1507 BusConnectionData *d;
1510 d = BUS_CONNECTION_DATA (connection);
1511 _dbus_assert (d != NULL);
1513 link = _dbus_list_get_first_link (&d->services_owned);
1514 while (link != NULL)
1516 BusService *service = link->data;
1519 _dbus_string_init_const (&str, bus_service_get_name (service));
1521 if (_dbus_string_starts_with_words_c_str (&str, name_prefix, '.'))
1524 link = _dbus_list_get_next_link (&d->services_owned, link);
1531 bus_connection_add_owned_service_link (DBusConnection *connection,
1534 BusConnectionData *d;
1536 d = BUS_CONNECTION_DATA (connection);
1537 _dbus_assert (d != NULL);
1539 _dbus_list_append_link (&d->services_owned, link);
1541 d->n_services_owned += 1;
1543 #ifdef DBUS_ENABLE_STATS
1544 update_peak (&d->peak_bus_names, d->n_services_owned);
1545 update_peak (&d->connections->peak_bus_names_per_conn,
1546 d->n_services_owned);
1548 d->connections->total_bus_names += 1;
1549 update_peak (&d->connections->peak_bus_names,
1550 d->connections->total_bus_names);
1555 bus_connection_add_owned_service (DBusConnection *connection,
1556 BusService *service)
1560 link = _dbus_list_alloc_link (service);
1565 bus_connection_add_owned_service_link (connection, link);
1571 bus_connection_remove_owned_service (DBusConnection *connection,
1572 BusService *service)
1574 BusConnectionData *d;
1576 d = BUS_CONNECTION_DATA (connection);
1577 _dbus_assert (d != NULL);
1579 _dbus_list_remove_last (&d->services_owned, service);
1581 d->n_services_owned -= 1;
1582 _dbus_assert (d->n_services_owned >= 0);
1584 #ifdef DBUS_ENABLE_STATS
1585 d->connections->total_bus_names -= 1;
1590 bus_connection_get_n_services_owned (DBusConnection *connection)
1592 BusConnectionData *d;
1594 d = BUS_CONNECTION_DATA (connection);
1595 _dbus_assert (d != NULL);
1597 return d->n_services_owned;
1601 bus_connection_complete (DBusConnection *connection,
1602 const DBusString *name,
1605 BusConnectionData *d;
1608 d = BUS_CONNECTION_DATA (connection);
1609 _dbus_assert (d != NULL);
1610 _dbus_assert (d->name == NULL);
1611 _dbus_assert (d->policy == NULL);
1613 _dbus_assert (!bus_connection_is_active (connection));
1615 if (!_dbus_string_copy_data (name, &d->name))
1617 BUS_SET_OOM (error);
1621 _dbus_assert (d->name != NULL);
1623 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1625 d->policy = bus_context_create_client_policy (d->connections->context,
1629 /* we may have a NULL policy on OOM or error getting list of
1630 * groups for a user. In the latter case we don't handle it so
1631 * well currently, as it will just keep failing over and over.
1634 if (d->policy == NULL)
1636 _dbus_verbose ("Failed to create security policy for connection %p\n",
1638 _DBUS_ASSERT_ERROR_IS_SET (error);
1639 dbus_free (d->name);
1644 if (dbus_connection_get_unix_user (connection, &uid))
1646 if (!adjust_connections_for_uid (d->connections,
1651 /* Create and cache a string which holds information about the
1652 * peer process; used for logging purposes.
1654 if (!cache_peer_loginfo_string (d, connection))
1657 /* Now the connection is active, move it between lists */
1658 _dbus_list_unlink (&d->connections->incomplete,
1659 d->link_in_connection_list);
1660 d->connections->n_incomplete -= 1;
1661 _dbus_list_append_link (&d->connections->completed,
1662 d->link_in_connection_list);
1663 d->connections->n_completed += 1;
1665 _dbus_assert (d->connections->n_incomplete >= 0);
1666 _dbus_assert (d->connections->n_completed > 0);
1668 /* If we have dropped below the max. number of incomplete
1669 * connections, start accept()ing again */
1670 bus_context_check_all_watches (d->connections->context);
1672 /* See if we can remove the timeout */
1673 bus_connections_expire_incomplete (d->connections);
1675 _dbus_assert (bus_connection_is_active (connection));
1679 BUS_SET_OOM (error);
1680 dbus_free (d->name);
1683 bus_client_policy_unref (d->policy);
1689 bus_connections_reload_policy (BusConnections *connections,
1692 BusConnectionData *d;
1693 DBusConnection *connection;
1696 _dbus_assert (connections != NULL);
1697 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1699 for (link = _dbus_list_get_first_link (&(connections->completed));
1701 link = _dbus_list_get_next_link (&(connections->completed), link))
1703 connection = link->data;
1704 d = BUS_CONNECTION_DATA (connection);
1705 _dbus_assert (d != NULL);
1706 _dbus_assert (d->policy != NULL);
1708 bus_client_policy_unref (d->policy);
1709 d->policy = bus_context_create_client_policy (connections->context,
1712 if (d->policy == NULL)
1714 _dbus_verbose ("Failed to create security policy for connection %p\n",
1716 _DBUS_ASSERT_ERROR_IS_SET (error);
1725 bus_connection_get_name (DBusConnection *connection)
1727 BusConnectionData *d;
1729 d = BUS_CONNECTION_DATA (connection);
1730 _dbus_assert (d != NULL);
1736 * Check whether completing the passed-in connection would
1737 * exceed limits, and if so set error and return #FALSE
1740 bus_connections_check_limits (BusConnections *connections,
1741 DBusConnection *requesting_completion,
1742 const char **limit_name_out,
1749 limit = bus_context_get_max_completed_connections (connections->context);
1751 if (connections->n_completed >= limit)
1753 if (limit_name_out != NULL)
1754 *limit_name_out = "max_completed_connections";
1756 if (limit_out != NULL)
1759 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1760 "The maximum number of active connections has been reached");
1764 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1766 limit = bus_context_get_max_connections_per_user (connections->context);
1768 if (get_connections_for_uid (connections, uid) >= limit)
1770 if (limit_name_out != NULL)
1771 *limit_name_out = "max_connections_per_user";
1773 if (limit_out != NULL)
1776 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1777 "The maximum number of active connections for UID %lu has been reached",
1787 bus_pending_reply_free (BusPendingReply *pending)
1789 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1791 pending->will_send_reply,
1792 pending->will_get_reply,
1793 pending->reply_serial);
1795 dbus_free (pending);
1799 bus_pending_reply_send_no_reply (BusConnections *connections,
1800 BusTransaction *transaction,
1801 BusPendingReply *pending)
1803 DBusMessage *message;
1804 DBusMessageIter iter;
1810 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1811 if (message == NULL)
1814 dbus_message_set_no_reply (message, TRUE);
1816 if (!dbus_message_set_reply_serial (message,
1817 pending->reply_serial))
1820 if (!dbus_message_set_error_name (message,
1821 DBUS_ERROR_NO_REPLY))
1824 /* If you change these messages, adjust test/dbus-daemon.c to match */
1825 if (pending->will_send_reply == NULL)
1826 errmsg = "Message recipient disconnected from message bus without replying";
1828 errmsg = "Message did not receive a reply (timeout by message bus)";
1830 dbus_message_iter_init_append (message, &iter);
1831 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1834 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1841 dbus_message_unref (message);
1846 bus_pending_reply_expired (BusExpireList *list,
1850 BusPendingReply *pending = link->data;
1851 BusConnections *connections = data;
1852 BusTransaction *transaction;
1854 /* No reply is forthcoming. So nuke it if we can. If not,
1855 * leave it in the list to try expiring again later when we
1859 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1861 pending->will_send_reply,
1862 pending->will_get_reply,
1863 pending->reply_serial);
1865 transaction = bus_transaction_new (connections->context);
1866 if (transaction == NULL)
1869 if (!bus_pending_reply_send_no_reply (connections,
1873 bus_transaction_cancel_and_free (transaction);
1877 bus_expire_list_remove_link (connections->pending_replies, link);
1879 bus_pending_reply_free (pending);
1880 bus_transaction_execute_and_free (transaction);
1886 bus_connection_drop_pending_replies (BusConnections *connections,
1887 DBusConnection *connection)
1889 /* The DBusConnection is almost 100% finalized here, so you can't
1890 * do anything with it except check for pointer equality
1894 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1897 link = bus_expire_list_get_first_link (connections->pending_replies);
1898 while (link != NULL)
1901 BusPendingReply *pending;
1903 next = bus_expire_list_get_next_link (connections->pending_replies,
1905 pending = link->data;
1907 if (pending->will_get_reply == connection)
1909 /* We don't need to track this pending reply anymore */
1911 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1913 pending->will_send_reply,
1914 pending->will_get_reply,
1915 pending->reply_serial);
1917 bus_expire_list_remove_link (connections->pending_replies,
1919 bus_pending_reply_free (pending);
1921 else if (pending->will_send_reply == connection)
1923 /* The reply isn't going to be sent, so set things
1924 * up so it will be expired right away
1926 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1928 pending->will_send_reply,
1929 pending->will_get_reply,
1930 pending->reply_serial);
1932 pending->will_send_reply = NULL;
1933 pending->expire_item.added_tv_sec = 0;
1934 pending->expire_item.added_tv_usec = 0;
1936 bus_expire_list_recheck_immediately (connections->pending_replies);
1946 BusPendingReply *pending;
1947 BusConnections *connections;
1948 } CancelPendingReplyData;
1951 cancel_pending_reply (void *data)
1953 CancelPendingReplyData *d = data;
1955 _dbus_verbose ("d = %p\n", d);
1957 if (!bus_expire_list_remove (d->connections->pending_replies,
1958 &d->pending->expire_item))
1959 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1961 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1965 cancel_pending_reply_data_free (void *data)
1967 CancelPendingReplyData *d = data;
1969 _dbus_verbose ("d = %p\n", d);
1971 /* d->pending should be either freed or still
1972 * in the list of pending replies (owned by someone
1980 * Record that a reply is allowed; return TRUE on success.
1983 bus_connections_expect_reply (BusConnections *connections,
1984 BusTransaction *transaction,
1985 DBusConnection *will_get_reply,
1986 DBusConnection *will_send_reply,
1987 DBusMessage *reply_to_this,
1990 BusPendingReply *pending;
1991 dbus_uint32_t reply_serial;
1993 CancelPendingReplyData *cprd;
1997 _dbus_assert (will_get_reply != NULL);
1998 _dbus_assert (will_send_reply != NULL);
1999 _dbus_assert (reply_to_this != NULL);
2001 if (dbus_message_get_no_reply (reply_to_this))
2002 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
2004 reply_serial = dbus_message_get_serial (reply_to_this);
2006 link = bus_expire_list_get_first_link (connections->pending_replies);
2008 while (link != NULL)
2010 pending = link->data;
2012 if (pending->reply_serial == reply_serial &&
2013 pending->will_get_reply == will_get_reply &&
2014 pending->will_send_reply == will_send_reply)
2016 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
2017 "Message has the same reply serial as a currently-outstanding existing method call");
2021 link = bus_expire_list_get_next_link (connections->pending_replies,
2023 if (pending->will_get_reply == will_get_reply)
2027 limit = bus_context_get_max_replies_per_connection (connections->context);
2031 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
2032 "The maximum number of pending replies for "
2033 "\"%s\" (%s) has been reached "
2034 "(max_replies_per_connection=%d)",
2035 bus_connection_get_name (will_get_reply),
2036 bus_connection_get_loginfo (will_get_reply),
2039 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
2040 "The maximum number of pending replies per connection has been reached");
2044 pending = dbus_new0 (BusPendingReply, 1);
2045 if (pending == NULL)
2047 BUS_SET_OOM (error);
2051 #ifdef DBUS_ENABLE_VERBOSE_MODE
2052 /* so we can see a not-yet-added pending reply */
2053 pending->expire_item.added_tv_sec = 1;
2054 pending->expire_item.added_tv_usec = 1;
2057 pending->will_get_reply = will_get_reply;
2058 pending->will_send_reply = will_send_reply;
2059 pending->reply_serial = reply_serial;
2061 cprd = dbus_new0 (CancelPendingReplyData, 1);
2064 BUS_SET_OOM (error);
2065 bus_pending_reply_free (pending);
2069 if (!bus_expire_list_add (connections->pending_replies,
2070 &pending->expire_item))
2072 BUS_SET_OOM (error);
2074 bus_pending_reply_free (pending);
2078 if (!bus_transaction_add_cancel_hook (transaction,
2079 cancel_pending_reply,
2081 cancel_pending_reply_data_free))
2083 BUS_SET_OOM (error);
2084 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
2086 bus_pending_reply_free (pending);
2090 cprd->pending = pending;
2091 cprd->connections = connections;
2093 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
2094 &pending->expire_item.added_tv_usec);
2096 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
2098 pending->will_send_reply,
2099 pending->will_get_reply,
2100 pending->reply_serial);
2108 BusConnections *connections;
2109 } CheckPendingReplyData;
2112 cancel_check_pending_reply (void *data)
2114 CheckPendingReplyData *d = data;
2116 _dbus_verbose ("d = %p\n",d);
2118 bus_expire_list_add_link (d->connections->pending_replies,
2124 check_pending_reply_data_free (void *data)
2126 CheckPendingReplyData *d = data;
2128 _dbus_verbose ("d = %p\n",d);
2130 if (d->link != NULL)
2132 BusPendingReply *pending = d->link->data;
2134 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
2135 &pending->expire_item));
2137 bus_pending_reply_free (pending);
2138 _dbus_list_free_link (d->link);
2145 * Check whether a reply is allowed, remove BusPendingReply
2146 * if so, return TRUE if so.
2149 bus_connections_check_reply (BusConnections *connections,
2150 BusTransaction *transaction,
2151 DBusConnection *sending_reply,
2152 DBusConnection *receiving_reply,
2156 CheckPendingReplyData *cprd;
2158 dbus_uint32_t reply_serial;
2160 _dbus_assert (sending_reply != NULL);
2161 _dbus_assert (receiving_reply != NULL);
2163 reply_serial = dbus_message_get_reply_serial (reply);
2165 link = bus_expire_list_get_first_link (connections->pending_replies);
2166 while (link != NULL)
2168 BusPendingReply *pending = link->data;
2170 if (pending->reply_serial == reply_serial &&
2171 pending->will_get_reply == receiving_reply &&
2172 pending->will_send_reply == sending_reply)
2174 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
2178 link = bus_expire_list_get_next_link (connections->pending_replies,
2184 _dbus_verbose ("No pending reply expected\n");
2189 cprd = dbus_new0 (CheckPendingReplyData, 1);
2192 BUS_SET_OOM (error);
2196 if (!bus_transaction_add_cancel_hook (transaction,
2197 cancel_check_pending_reply,
2199 check_pending_reply_data_free))
2201 BUS_SET_OOM (error);
2207 cprd->connections = connections;
2209 bus_expire_list_unlink (connections->pending_replies,
2212 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2220 * Note that this is fairly fragile; in particular, don't try to use
2221 * one transaction across any main loop iterations.
2226 BusTransaction *transaction;
2227 DBusMessage *message;
2228 DBusPreallocatedSend *preallocated;
2233 BusTransactionCancelFunction cancel_function;
2234 DBusFreeFunction free_data_function;
2238 struct BusTransaction
2240 DBusList *connections;
2241 BusContext *context;
2242 DBusList *cancel_hooks;
2246 message_to_send_free (DBusConnection *connection,
2247 MessageToSend *to_send)
2249 if (to_send->message)
2250 dbus_message_unref (to_send->message);
2252 if (to_send->preallocated)
2253 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2255 dbus_free (to_send);
2259 cancel_hook_cancel (void *element,
2262 CancelHook *ch = element;
2264 _dbus_verbose ("Running transaction cancel hook\n");
2266 if (ch->cancel_function)
2267 (* ch->cancel_function) (ch->data);
2271 cancel_hook_free (void *element,
2274 CancelHook *ch = element;
2276 if (ch->free_data_function)
2277 (* ch->free_data_function) (ch->data);
2283 free_cancel_hooks (BusTransaction *transaction)
2285 _dbus_list_foreach (&transaction->cancel_hooks,
2286 cancel_hook_free, NULL);
2288 _dbus_list_clear (&transaction->cancel_hooks);
2292 bus_transaction_new (BusContext *context)
2294 BusTransaction *transaction;
2296 transaction = dbus_new0 (BusTransaction, 1);
2297 if (transaction == NULL)
2300 transaction->context = context;
2306 bus_transaction_get_context (BusTransaction *transaction)
2308 return transaction->context;
2312 * Reserve enough memory to capture the given message if the
2313 * transaction goes through.
2316 bus_transaction_capture (BusTransaction *transaction,
2317 DBusConnection *sender,
2318 DBusConnection *addressed_recipient,
2319 DBusMessage *message)
2321 BusConnections *connections;
2324 DBusList *recipients = NULL;
2325 dbus_bool_t ret = FALSE;
2327 connections = bus_context_get_connections (transaction->context);
2329 /* shortcut: don't compose the message unless someone wants it */
2330 if (connections->monitors == NULL)
2333 mm = connections->monitor_matchmaker;
2334 /* This is non-null if there has ever been a monitor - we don't GC it.
2335 * There's little point, since there is up to 1 per process. */
2336 _dbus_assert (mm != NULL);
2338 if (!bus_matchmaker_get_recipients (mm, connections, sender,
2339 addressed_recipient, message, &recipients))
2342 for (link = _dbus_list_get_first_link (&recipients);
2344 link = _dbus_list_get_next_link (&recipients, link))
2346 DBusConnection *recipient = link->data;
2348 if (!bus_transaction_send (transaction, recipient, message, FALSE))
2355 _dbus_list_clear (&recipients);
2360 bus_transaction_capture_error_reply (BusTransaction *transaction,
2361 DBusConnection *addressed_recipient,
2362 const DBusError *error,
2363 DBusMessage *in_reply_to)
2365 BusConnections *connections;
2367 dbus_bool_t ret = FALSE;
2369 _dbus_assert (error != NULL);
2370 _DBUS_ASSERT_ERROR_IS_SET (error);
2372 connections = bus_context_get_connections (transaction->context);
2374 /* shortcut: don't compose the message unless someone wants it */
2375 if (connections->monitors == NULL)
2378 reply = dbus_message_new_error (in_reply_to,
2385 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
2388 ret = bus_transaction_capture (transaction, NULL, addressed_recipient, reply);
2391 dbus_message_unref (reply);
2396 bus_transaction_send_from_driver (BusTransaction *transaction,
2397 DBusConnection *connection,
2398 DBusMessage *message)
2400 DBusError error = DBUS_ERROR_INIT;
2401 BusDeferredMessage *deferred_message;
2403 /* We have to set the sender to the driver, and have
2404 * to check security policy since it was not done in
2407 _dbus_verbose ("Sending %s %s %s from driver\n",
2408 dbus_message_get_interface (message) ?
2409 dbus_message_get_interface (message) : "(no interface)",
2410 dbus_message_get_member (message) ?
2411 dbus_message_get_member (message) : "(no member)",
2412 dbus_message_get_error_name (message) ?
2413 dbus_message_get_error_name (message) : "(no error name)");
2415 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2418 if (bus_connection_is_active (connection))
2420 if (!dbus_message_set_destination (message,
2421 bus_connection_get_name (connection)))
2425 /* bus driver never wants a reply */
2426 dbus_message_set_no_reply (message, TRUE);
2428 /* Capture it for monitors, even if the real recipient's receive policy
2429 * does not allow it to receive this message from us (which would be odd).
2431 if (!bus_transaction_capture (transaction, NULL, connection, message))
2434 /* If security policy doesn't allow the message, we would silently
2435 * eat it; the driver doesn't care about getting a reply. However,
2436 * if we're actively capturing messages, it's nice to log that we
2437 * tried to send it and did not allow ourselves to do so.
2439 switch (bus_context_check_security_policy (bus_transaction_get_context (transaction),
2441 NULL, connection, connection,
2442 message, NULL, &error,
2445 case BUS_RESULT_TRUE:
2447 case BUS_RESULT_FALSE:
2448 if (!bus_transaction_capture_error_reply (transaction, connection,
2451 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_WARNING,
2452 "message from dbus-daemon rejected but not enough "
2453 "memory to capture it");
2456 /* This is not fatal to the transaction so silently eat the disallowed
2457 * message (see reasoning above) */
2458 dbus_error_free (&error);
2461 case BUS_RESULT_LATER:
2462 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
2464 return TRUE; /* pretend to have sent it */
2467 return bus_transaction_send (transaction, connection, message, FALSE);
2471 bus_transaction_send (BusTransaction *transaction,
2472 DBusConnection *connection,
2473 DBusMessage *message,
2474 dbus_bool_t deferred_dispatch)
2476 MessageToSend *to_send;
2477 BusConnectionData *d;
2480 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2481 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2482 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2484 dbus_message_get_interface (message) ?
2485 dbus_message_get_interface (message) : "(unset)",
2486 dbus_message_get_member (message) ?
2487 dbus_message_get_member (message) : "(unset)",
2488 dbus_message_get_error_name (message) ?
2489 dbus_message_get_error_name (message) : "(unset)",
2490 dbus_connection_get_is_connected (connection) ?
2491 "" : " (disconnected)");
2493 _dbus_assert (dbus_message_get_sender (message) != NULL);
2495 if (!dbus_connection_get_is_connected (connection))
2496 return TRUE; /* silently ignore disconnected connections */
2498 d = BUS_CONNECTION_DATA (connection);
2499 _dbus_assert (d != NULL);
2501 if (!deferred_dispatch && d->deferred_messages != NULL)
2503 BusDeferredMessage *deferred_message;
2504 dbus_bool_t success;
2505 /* sender and addressed recipient are not required at this point as we only need to send message
2506 * to a single recipient without performing policy check. */
2507 deferred_message = bus_deferred_message_new (message,
2512 if (deferred_message == NULL)
2515 success = bus_deferred_message_queue_at_recipient(deferred_message, transaction,
2517 bus_deferred_message_unref(deferred_message);
2522 to_send = dbus_new (MessageToSend, 1);
2523 if (to_send == NULL)
2528 to_send->preallocated = dbus_connection_preallocate_send (connection);
2529 if (to_send->preallocated == NULL)
2531 dbus_free (to_send);
2535 dbus_message_ref (message);
2536 to_send->message = message;
2537 to_send->transaction = transaction;
2539 _dbus_verbose ("about to prepend message\n");
2541 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2543 message_to_send_free (connection, to_send);
2547 _dbus_verbose ("prepended message\n");
2549 /* See if we already had this connection in the list
2550 * for this transaction. If we have a pending message,
2551 * then we should already be in transaction->connections
2553 link = _dbus_list_get_first_link (&d->transaction_messages);
2554 _dbus_assert (link->data == to_send);
2555 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2556 while (link != NULL)
2558 MessageToSend *m = link->data;
2559 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2561 if (m->transaction == transaction)
2569 if (!_dbus_list_prepend (&transaction->connections, connection))
2571 _dbus_list_remove (&d->transaction_messages, to_send);
2572 message_to_send_free (connection, to_send);
2581 transaction_free (BusTransaction *transaction)
2583 _dbus_assert (transaction->connections == NULL);
2585 free_cancel_hooks (transaction);
2587 dbus_free (transaction);
2591 connection_cancel_transaction (DBusConnection *connection,
2592 BusTransaction *transaction)
2595 BusConnectionData *d;
2597 d = BUS_CONNECTION_DATA (connection);
2598 _dbus_assert (d != NULL);
2600 link = _dbus_list_get_first_link (&d->transaction_messages);
2601 while (link != NULL)
2603 MessageToSend *m = link->data;
2604 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2606 if (m->transaction == transaction)
2608 _dbus_list_remove_link (&d->transaction_messages,
2611 message_to_send_free (connection, m);
2619 bus_transaction_cancel_and_free (BusTransaction *transaction)
2621 DBusConnection *connection;
2623 _dbus_verbose ("TRANSACTION: cancelled\n");
2625 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2626 connection_cancel_transaction (connection, transaction);
2628 _dbus_list_foreach (&transaction->cancel_hooks,
2629 cancel_hook_cancel, NULL);
2631 transaction_free (transaction);
2635 connection_execute_transaction (DBusConnection *connection,
2636 BusTransaction *transaction)
2639 BusConnectionData *d;
2641 d = BUS_CONNECTION_DATA (connection);
2642 _dbus_assert (d != NULL);
2644 /* Send the queue in order (FIFO) */
2645 link = _dbus_list_get_last_link (&d->transaction_messages);
2646 while (link != NULL)
2648 MessageToSend *m = link->data;
2649 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2651 if (m->transaction == transaction)
2653 _dbus_list_remove_link (&d->transaction_messages,
2656 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2658 dbus_connection_send_preallocated (connection,
2663 m->preallocated = NULL; /* so we don't double-free it */
2665 message_to_send_free (connection, m);
2673 bus_transaction_execute_and_free (BusTransaction *transaction)
2675 /* For each connection in transaction->connections
2678 DBusConnection *connection;
2680 _dbus_verbose ("TRANSACTION: executing\n");
2682 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2683 connection_execute_transaction (connection, transaction);
2685 transaction_free (transaction);
2689 bus_connection_remove_transactions (DBusConnection *connection)
2691 MessageToSend *to_send;
2692 BusConnectionData *d;
2694 d = BUS_CONNECTION_DATA (connection);
2695 _dbus_assert (d != NULL);
2697 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2699 /* only has an effect for the first MessageToSend listing this transaction */
2700 _dbus_list_remove (&to_send->transaction->connections,
2703 _dbus_list_remove (&d->transaction_messages, to_send);
2704 message_to_send_free (connection, to_send);
2709 * Converts the DBusError to a message reply
2712 bus_transaction_send_error_reply (BusTransaction *transaction,
2713 DBusConnection *connection,
2714 const DBusError *error,
2715 DBusMessage *in_reply_to)
2719 _dbus_assert (error != NULL);
2720 _DBUS_ASSERT_ERROR_IS_SET (error);
2722 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2723 error->name, error->message);
2725 reply = dbus_message_new_error (in_reply_to,
2731 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2733 dbus_message_unref (reply);
2737 dbus_message_unref (reply);
2743 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2744 BusTransactionCancelFunction cancel_function,
2746 DBusFreeFunction free_data_function)
2750 ch = dbus_new (CancelHook, 1);
2754 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2755 cancel_function, data);
2757 ch->cancel_function = cancel_function;
2759 ch->free_data_function = free_data_function;
2761 /* It's important that the hooks get run in reverse order that they
2764 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2774 bus_connection_dispatch_deferred (DBusConnection *connection)
2776 BusDeferredMessage *message;
2778 _dbus_return_if_fail (connection != NULL);
2780 while ((message = bus_connection_pop_deferred_message(connection)) != NULL)
2782 bus_deferred_message_dispatch(message);
2783 bus_deferred_message_unref(message);
2788 bus_connection_has_deferred_messages (DBusConnection *connection)
2790 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2791 return d->deferred_messages != NULL ? TRUE : FALSE;
2795 bus_connection_queue_deferred_message (DBusConnection *connection,
2796 BusDeferredMessage *message,
2797 dbus_bool_t prepend)
2799 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2800 dbus_bool_t success;
2802 success = _dbus_list_prepend(&d->deferred_messages, message);
2804 success = _dbus_list_append(&d->deferred_messages, message);
2808 bus_deferred_message_ref(message);
2816 bus_connection_replace_deferred_message (DBusConnection *connection,
2817 BusDeferredMessage *oldMessage,
2818 BusDeferredMessage *newMessage)
2821 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2823 link = _dbus_list_find_first(&d->deferred_messages, oldMessage);
2827 if (!_dbus_list_insert_after(&d->deferred_messages, link, newMessage))
2830 bus_deferred_message_ref(newMessage);
2831 _dbus_list_remove_link(&d->deferred_messages, link);
2832 bus_deferred_message_unref(oldMessage);
2836 BusDeferredMessage *
2837 bus_connection_pop_deferred_message (DBusConnection *connection)
2840 BusDeferredMessage *message;
2841 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2843 link =_dbus_list_get_first_link(&d->deferred_messages);
2846 message = link->data;
2847 if (!bus_deferred_message_waits_for_check(message))
2849 _dbus_list_remove_link(&d->deferred_messages, link);
2858 bus_connection_putback_deferred_message (DBusConnection *connection, BusDeferredMessage *message)
2860 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2861 if (_dbus_list_prepend(&d->deferred_messages, message))
2869 bus_connection_clear_deferred_messages (DBusConnection *connection)
2871 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2874 BusDeferredMessage *message;
2876 link =_dbus_list_get_first_link(&d->deferred_messages);
2877 while (link != NULL)
2879 next = _dbus_list_get_next_link (&d->deferred_messages, link);
2880 message = link->data;
2882 bus_deferred_message_unref(message);
2883 _dbus_list_remove_link(&d->deferred_messages, link);
2890 bus_connection_remove_deferred_message (DBusConnection *connection,
2891 BusDeferredMessage *message)
2893 BusConnectionData *d = BUS_CONNECTION_DATA(connection);
2894 if (_dbus_list_remove(&d->deferred_messages, message))
2895 bus_deferred_message_unref(message);
2899 bus_connections_get_n_active (BusConnections *connections)
2901 return connections->n_completed;
2905 bus_connections_get_n_incomplete (BusConnections *connections)
2907 return connections->n_incomplete;
2910 #ifdef DBUS_ENABLE_STATS
2912 bus_connections_get_total_match_rules (BusConnections *connections)
2914 return connections->total_match_rules;
2918 bus_connections_get_peak_match_rules (BusConnections *connections)
2920 return connections->peak_match_rules;
2924 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2926 return connections->peak_match_rules_per_conn;
2930 bus_connections_get_total_bus_names (BusConnections *connections)
2932 return connections->total_bus_names;
2936 bus_connections_get_peak_bus_names (BusConnections *connections)
2938 return connections->peak_bus_names;
2942 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2944 return connections->peak_bus_names_per_conn;
2948 bus_connection_get_peak_match_rules (DBusConnection *connection)
2950 BusConnectionData *d;
2952 d = BUS_CONNECTION_DATA (connection);
2953 _dbus_assert(d != NULL);
2955 return d->peak_match_rules;
2959 bus_connection_get_peak_bus_names (DBusConnection *connection)
2961 BusConnectionData *d;
2963 d = BUS_CONNECTION_DATA (connection);
2964 _dbus_assert(d != NULL);
2966 return d->peak_bus_names;
2968 #endif /* DBUS_ENABLE_STATS */
2971 bus_connection_is_monitor (DBusConnection *connection)
2973 BusConnectionData *d;
2975 d = BUS_CONNECTION_DATA (connection);
2976 _dbus_assert(d != NULL);
2978 return d->link_in_monitors != NULL;
2982 bcd_add_monitor_rules (BusConnectionData *d,
2983 DBusConnection *connection,
2986 BusMatchmaker *mm = d->connections->monitor_matchmaker;
2991 mm = bus_matchmaker_new ();
2996 d->connections->monitor_matchmaker = mm;
2999 for (iter = _dbus_list_get_first_link (rules);
3001 iter = _dbus_list_get_next_link (rules, iter))
3003 if (!bus_matchmaker_add_rule (mm, iter->data))
3005 bus_matchmaker_disconnected (mm, connection);
3014 bcd_drop_monitor_rules (BusConnectionData *d,
3015 DBusConnection *connection)
3017 BusMatchmaker *mm = d->connections->monitor_matchmaker;
3020 bus_matchmaker_disconnected (mm, connection);
3024 bus_connection_be_monitor (DBusConnection *connection,
3025 BusTransaction *transaction,
3029 BusConnectionData *d;
3034 d = BUS_CONNECTION_DATA (connection);
3035 _dbus_assert (d != NULL);
3037 link = _dbus_list_alloc_link (connection);
3041 BUS_SET_OOM (error);
3045 if (!bcd_add_monitor_rules (d, connection, rules))
3047 _dbus_list_free_link (link);
3048 BUS_SET_OOM (error);
3052 /* release all its names */
3053 if (!_dbus_list_copy (&d->services_owned, &tmp))
3055 bcd_drop_monitor_rules (d, connection);
3056 _dbus_list_free_link (link);
3057 BUS_SET_OOM (error);
3061 for (iter = _dbus_list_get_first_link (&tmp);
3063 iter = _dbus_list_get_next_link (&tmp, iter))
3065 BusService *service = iter->data;
3067 /* This call is transactional: if there isn't enough memory to
3068 * do everything, then the service gets all its names back when
3069 * the transaction is cancelled due to OOM. */
3070 if (!bus_service_remove_owner (service, connection, transaction, error))
3072 bcd_drop_monitor_rules (d, connection);
3073 _dbus_list_free_link (link);
3074 _dbus_list_clear (&tmp);
3079 /* We have now done everything that can fail, so there is no problem
3080 * with doing the irrevocable stuff. */
3082 _dbus_list_clear (&tmp);
3084 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
3085 "Connection %s (%s) became a monitor.", d->name,
3086 d->cached_loginfo_string);
3088 if (d->n_match_rules > 0)
3092 mm = bus_context_get_matchmaker (d->connections->context);
3093 bus_matchmaker_disconnected (mm, connection);
3096 /* flag it as a monitor */
3097 d->link_in_monitors = link;
3098 _dbus_list_append_link (&d->connections->monitors, link);
3100 /* it isn't allowed to reply, and it is no longer relevant whether it
3101 * receives replies */
3102 bus_connection_drop_pending_replies (d->connections, connection);