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"
33 #include <dbus/dbus-list.h>
34 #include <dbus/dbus-hash.h>
35 #include <dbus/dbus-timeout.h>
36 #include <dbus/dbus-connection-internal.h>
38 /* Trim executed commands to this length; we want to keep logs readable */
39 #define MAX_LOG_COMMAND_LEN 50
41 static void bus_connection_remove_transactions (DBusConnection *connection);
45 BusExpireItem expire_item;
47 DBusConnection *will_get_reply;
48 DBusConnection *will_send_reply;
50 dbus_uint32_t reply_serial;
57 DBusList *completed; /**< List of all completed connections */
58 int n_completed; /**< Length of completed list */
59 DBusList *incomplete; /**< List of all not-yet-active connections */
60 int n_incomplete; /**< Length of incomplete list */
62 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
63 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
64 int stamp; /**< Incrementing number */
65 BusExpireList *pending_replies; /**< List of pending replies */
67 /** List of all monitoring connections, a subset of completed.
68 * Each member is a #DBusConnection. */
71 #ifdef DBUS_ENABLE_STATS
72 int total_match_rules;
74 int peak_match_rules_per_conn;
78 int peak_bus_names_per_conn;
82 static dbus_int32_t connection_data_slot = -1;
86 BusConnections *connections;
87 DBusList *link_in_connection_list;
88 DBusConnection *connection;
89 DBusList *services_owned;
91 DBusList *match_rules;
94 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
95 DBusMessage *oom_message;
96 DBusPreallocatedSend *oom_preallocated;
97 BusClientPolicy *policy;
99 char *cached_loginfo_string;
100 BusSELinuxID *selinux_id;
102 long connection_tv_sec; /**< Time when we connected (seconds component) */
103 long connection_tv_usec; /**< Time when we connected (microsec component) */
104 int stamp; /**< connections->stamp last time we were traversed */
106 #ifdef DBUS_ENABLE_STATS
107 int peak_match_rules;
110 int n_pending_unix_fds;
111 DBusTimeout *pending_unix_fds_timeout;
113 /** non-NULL if and only if this is a monitor */
114 DBusList *link_in_monitors;
117 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
121 static void bus_connection_drop_pending_replies (BusConnections *connections,
122 DBusConnection *connection);
124 static dbus_bool_t expire_incomplete_timeout (void *data);
126 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
129 connection_get_loop (DBusConnection *connection)
131 BusConnectionData *d;
133 d = BUS_CONNECTION_DATA (connection);
135 return bus_context_get_loop (d->connections->context);
140 get_connections_for_uid (BusConnections *connections,
146 /* val is NULL is 0 when it isn't in the hash yet */
148 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
151 current_count = _DBUS_POINTER_TO_INT (val);
153 return current_count;
157 adjust_connections_for_uid (BusConnections *connections,
163 current_count = get_connections_for_uid (connections, uid);
165 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
166 ": was %d adjustment %d making %d\n",
167 uid, current_count, adjustment, current_count + adjustment);
169 _dbus_assert (current_count >= 0);
171 current_count += adjustment;
173 _dbus_assert (current_count >= 0);
175 if (current_count == 0)
177 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
184 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
185 uid, _DBUS_INT_TO_POINTER (current_count));
187 /* only positive adjustment can fail as otherwise
188 * a hash entry should already exist
190 _dbus_assert (adjustment > 0 ||
191 (adjustment <= 0 && retval));
198 bus_connection_disconnected (DBusConnection *connection)
200 BusConnectionData *d;
202 BusMatchmaker *matchmaker;
204 d = BUS_CONNECTION_DATA (connection);
205 _dbus_assert (d != NULL);
207 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
208 d->name ? d->name : "(inactive)");
210 /* Delete our match rules */
211 if (d->n_match_rules > 0)
213 matchmaker = bus_context_get_matchmaker (d->connections->context);
214 bus_matchmaker_disconnected (matchmaker, connection);
217 /* Drop any service ownership. Unfortunately, this requires
218 * memory allocation and there doesn't seem to be a good way to
219 * handle it other than sleeping; we can't "fail" the operation of
220 * disconnecting a client, and preallocating a broadcast "service is
221 * now gone" message for every client-service pair seems kind of
224 while ((service = _dbus_list_get_last (&d->services_owned)))
226 BusTransaction *transaction;
231 dbus_error_init (&error);
233 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
234 _dbus_wait_for_memory ();
236 if (!bus_service_remove_owner (service, connection,
237 transaction, &error))
239 _DBUS_ASSERT_ERROR_IS_SET (&error);
241 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
243 dbus_error_free (&error);
244 bus_transaction_cancel_and_free (transaction);
245 _dbus_wait_for_memory ();
250 _dbus_verbose ("Failed to remove service owner: %s %s\n",
251 error.name, error.message);
252 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
256 bus_transaction_execute_and_free (transaction);
259 bus_dispatch_remove_connection (connection);
261 /* no more watching */
262 if (!dbus_connection_set_watch_functions (connection,
266 _dbus_assert_not_reached ("setting watch functions to NULL failed");
268 if (!dbus_connection_set_timeout_functions (connection,
272 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
274 dbus_connection_set_unix_user_function (connection,
276 dbus_connection_set_windows_user_function (connection,
279 dbus_connection_set_dispatch_status_function (connection,
282 if (d->pending_unix_fds_timeout)
284 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
285 d->pending_unix_fds_timeout);
286 _dbus_timeout_unref (d->pending_unix_fds_timeout);
288 d->pending_unix_fds_timeout = NULL;
289 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
291 bus_connection_remove_transactions (connection);
293 if (d->link_in_monitors != NULL)
295 _dbus_list_remove_link (&d->connections->monitors, d->link_in_monitors);
296 d->link_in_monitors = NULL;
299 if (d->link_in_connection_list != NULL)
305 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
306 d->link_in_connection_list = NULL;
307 d->connections->n_completed -= 1;
309 if (dbus_connection_get_unix_user (connection, &uid))
311 if (!adjust_connections_for_uid (d->connections,
313 _dbus_assert_not_reached ("adjusting downward should never fail");
318 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
319 d->link_in_connection_list = NULL;
320 d->connections->n_incomplete -= 1;
322 /* If we have dropped below the max. number of incomplete
323 * connections, start accept()ing again */
324 bus_context_check_all_watches (d->connections->context);
327 _dbus_assert (d->connections->n_incomplete >= 0);
328 _dbus_assert (d->connections->n_completed >= 0);
331 bus_connection_drop_pending_replies (d->connections, connection);
333 /* frees "d" as side effect */
334 dbus_connection_set_data (connection,
335 connection_data_slot,
338 dbus_connection_unref (connection);
342 add_connection_watch (DBusWatch *watch,
345 DBusConnection *connection = data;
347 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
351 remove_connection_watch (DBusWatch *watch,
354 DBusConnection *connection = data;
356 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
360 toggle_connection_watch (DBusWatch *watch,
363 DBusConnection *connection = data;
365 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
369 add_connection_timeout (DBusTimeout *timeout,
372 DBusConnection *connection = data;
374 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
378 remove_connection_timeout (DBusTimeout *timeout,
381 DBusConnection *connection = data;
383 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
387 dispatch_status_function (DBusConnection *connection,
388 DBusDispatchStatus new_status,
391 DBusLoop *loop = data;
393 if (new_status != DBUS_DISPATCH_COMPLETE)
395 while (!_dbus_loop_queue_dispatch (loop, connection))
396 _dbus_wait_for_memory ();
401 allow_unix_user_function (DBusConnection *connection,
405 BusConnectionData *d;
407 d = BUS_CONNECTION_DATA (connection);
409 _dbus_assert (d != NULL);
411 return bus_context_allow_unix_user (d->connections->context, uid);
415 free_connection_data (void *data)
417 BusConnectionData *d = data;
419 /* services_owned should be NULL since we should be disconnected */
420 _dbus_assert (d->services_owned == NULL);
421 _dbus_assert (d->n_services_owned == 0);
423 _dbus_assert (d->transaction_messages == NULL);
425 if (d->oom_preallocated)
426 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
429 dbus_message_unref (d->oom_message);
432 bus_client_policy_unref (d->policy);
435 bus_selinux_id_unref (d->selinux_id);
437 dbus_free (d->cached_loginfo_string);
445 bus_connections_new (BusContext *context)
447 BusConnections *connections;
449 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
452 connections = dbus_new0 (BusConnections, 1);
453 if (connections == NULL)
456 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
458 if (connections->completed_by_user == NULL)
461 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
462 expire_incomplete_timeout,
464 if (connections->expire_timeout == NULL)
467 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
469 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
470 bus_context_get_reply_timeout (context),
471 bus_pending_reply_expired,
473 if (connections->pending_replies == NULL)
476 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
477 connections->expire_timeout))
480 connections->refcount = 1;
481 connections->context = context;
486 bus_expire_list_free (connections->pending_replies);
488 _dbus_timeout_unref (connections->expire_timeout);
490 _dbus_hash_table_unref (connections->completed_by_user);
492 dbus_free (connections);
494 dbus_connection_free_data_slot (&connection_data_slot);
500 bus_connections_ref (BusConnections *connections)
502 _dbus_assert (connections->refcount > 0);
503 connections->refcount += 1;
509 bus_connections_unref (BusConnections *connections)
511 _dbus_assert (connections->refcount > 0);
512 connections->refcount -= 1;
513 if (connections->refcount == 0)
515 /* drop all incomplete */
516 while (connections->incomplete != NULL)
518 DBusConnection *connection;
520 connection = connections->incomplete->data;
522 dbus_connection_ref (connection);
523 dbus_connection_close (connection);
524 bus_connection_disconnected (connection);
525 dbus_connection_unref (connection);
528 _dbus_assert (connections->n_incomplete == 0);
530 /* drop all monitors */
531 _dbus_list_clear (&connections->monitors);
533 /* drop all real connections */
534 while (connections->completed != NULL)
536 DBusConnection *connection;
538 connection = connections->completed->data;
540 dbus_connection_ref (connection);
541 dbus_connection_close (connection);
542 bus_connection_disconnected (connection);
543 dbus_connection_unref (connection);
546 _dbus_assert (connections->n_completed == 0);
548 bus_expire_list_free (connections->pending_replies);
550 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
551 connections->expire_timeout);
553 _dbus_timeout_unref (connections->expire_timeout);
555 _dbus_hash_table_unref (connections->completed_by_user);
557 dbus_free (connections);
559 dbus_connection_free_data_slot (&connection_data_slot);
563 /* Used for logging */
565 cache_peer_loginfo_string (BusConnectionData *d,
566 DBusConnection *connection)
568 DBusString loginfo_buf;
572 dbus_bool_t prev_added;
574 if (!_dbus_string_init (&loginfo_buf))
578 if (dbus_connection_get_unix_user (connection, &uid))
580 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
586 if (dbus_connection_get_unix_process_id (connection, &pid))
590 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
593 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
595 /* Ignore errors here; we may not have permissions to read the
597 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
598 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
602 if (dbus_connection_get_windows_user (connection, &windows_sid))
604 dbus_bool_t did_append;
605 did_append = _dbus_string_append_printf (&loginfo_buf,
606 "sid=\"%s\" ", windows_sid);
607 dbus_free (windows_sid);
612 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
615 _dbus_string_free (&loginfo_buf);
619 _dbus_string_free (&loginfo_buf);
624 check_pending_fds_cb (DBusConnection *connection)
626 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
627 int n_pending_unix_fds_old = d->n_pending_unix_fds;
628 int n_pending_unix_fds_new;
630 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
632 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
633 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
635 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
637 _dbus_timeout_set_interval (d->pending_unix_fds_timeout,
638 bus_context_get_pending_fd_timeout (d->connections->context));
639 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, TRUE);
642 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
644 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
648 d->n_pending_unix_fds = n_pending_unix_fds_new;
652 pending_unix_fds_timeout_cb (void *data)
654 DBusConnection *connection = data;
655 dbus_connection_close (connection);
660 bus_connections_setup_connection (BusConnections *connections,
661 DBusConnection *connection)
664 BusConnectionData *d;
669 d = dbus_new0 (BusConnectionData, 1);
674 d->connections = connections;
675 d->connection = connection;
677 _dbus_get_monotonic_time (&d->connection_tv_sec,
678 &d->connection_tv_usec);
680 _dbus_assert (connection_data_slot >= 0);
682 if (!dbus_connection_set_data (connection,
683 connection_data_slot,
684 d, free_connection_data))
690 dbus_connection_set_route_peer_messages (connection, TRUE);
694 dbus_error_init (&error);
695 d->selinux_id = bus_selinux_init_connection_id (connection,
697 if (dbus_error_is_set (&error))
699 /* This is a bit bogus because we pretend all errors
700 * are OOM; this is done because we know that in bus.c
701 * an OOM error disconnects the connection, which is
702 * the same thing we want on any other error.
704 dbus_error_free (&error);
708 if (!dbus_connection_set_watch_functions (connection,
709 add_connection_watch,
710 remove_connection_watch,
711 toggle_connection_watch,
716 if (!dbus_connection_set_timeout_functions (connection,
717 add_connection_timeout,
718 remove_connection_timeout,
723 /* For now we don't need to set a Windows user function because
724 * there are no policies in the config file controlling what
725 * Windows users can connect. The default 'same user that owns the
726 * bus can connect' behavior of DBusConnection is fine on Windows.
728 dbus_connection_set_unix_user_function (connection,
729 allow_unix_user_function,
732 dbus_connection_set_dispatch_status_function (connection,
733 dispatch_status_function,
734 bus_context_get_loop (connections->context),
737 d->link_in_connection_list = _dbus_list_alloc_link (connection);
738 if (d->link_in_connection_list == NULL)
741 /* Setup the connection with the dispatcher */
742 if (!bus_dispatch_add_connection (connection))
745 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
747 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
749 bus_dispatch_remove_connection (connection);
754 /* Setup pending fds timeout (see #80559) */
755 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
756 pending_unix_fds_timeout_cb,
758 if (d->pending_unix_fds_timeout == NULL)
761 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
762 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
763 d->pending_unix_fds_timeout))
766 _dbus_connection_set_pending_fds_function (connection,
767 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
770 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
771 connections->n_incomplete += 1;
773 dbus_connection_ref (connection);
775 bus_connections_expire_incomplete (connections);
777 /* The listening socket is removed from the main loop,
778 * i.e. does not accept(), while n_incomplete is at its
779 * maximum value; so we shouldn't get here in that case */
780 _dbus_assert (connections->n_incomplete <=
781 bus_context_get_max_incomplete_connections (connections->context));
783 /* If we have the maximum number of incomplete connections,
784 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
785 bus_context_check_all_watches (d->connections->context);
793 bus_selinux_id_unref (d->selinux_id);
794 d->selinux_id = NULL;
796 if (!dbus_connection_set_watch_functions (connection,
800 _dbus_assert_not_reached ("setting watch functions to NULL failed");
802 if (!dbus_connection_set_timeout_functions (connection,
806 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
808 dbus_connection_set_unix_user_function (connection,
811 dbus_connection_set_windows_user_function (connection,
814 dbus_connection_set_dispatch_status_function (connection,
817 if (d->pending_unix_fds_timeout)
818 _dbus_timeout_unref (d->pending_unix_fds_timeout);
820 d->pending_unix_fds_timeout = NULL;
822 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
824 if (d->link_in_connection_list != NULL)
826 _dbus_assert (d->link_in_connection_list->next == NULL);
827 _dbus_assert (d->link_in_connection_list->prev == NULL);
828 _dbus_list_free_link (d->link_in_connection_list);
829 d->link_in_connection_list = NULL;
832 if (!dbus_connection_set_data (connection,
833 connection_data_slot,
835 _dbus_assert_not_reached ("failed to set connection data to null");
837 /* "d" has now been freed */
844 bus_connections_expire_incomplete (BusConnections *connections)
850 if (connections->incomplete != NULL)
852 long tv_sec, tv_usec;
856 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
857 auth_timeout = bus_context_get_auth_timeout (connections->context);
859 link = _dbus_list_get_first_link (&connections->incomplete);
862 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
863 DBusConnection *connection;
864 BusConnectionData *d;
867 connection = link->data;
869 d = BUS_CONNECTION_DATA (connection);
871 _dbus_assert (d != NULL);
873 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
874 d->connection_tv_usec,
877 if (elapsed >= (double) auth_timeout)
879 /* Unfortunately, we can't identify the connection: it doesn't
880 * have a unique name yet, we don't know its uid/pid yet,
882 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
883 "Connection has not authenticated soon enough, closing it "
884 "(auth_timeout=%dms, elapsed: %.0fms)",
885 auth_timeout, elapsed);
887 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
888 dbus_connection_close (connection);
892 /* We can end the loop, since the connections are in oldest-first order */
893 next_interval = ((double)auth_timeout) - elapsed;
894 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
895 connection, next_interval);
904 bus_expire_timeout_set_interval (connections->expire_timeout,
909 expire_incomplete_timeout (void *data)
911 BusConnections *connections = data;
913 _dbus_verbose ("Running\n");
915 /* note that this may remove the timeout */
916 bus_connections_expire_incomplete (connections);
922 bus_connection_get_unix_groups (DBusConnection *connection,
923 unsigned long **groups,
932 if (dbus_connection_get_unix_user (connection, &uid))
934 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
936 _dbus_verbose ("Did not get any groups for UID %lu\n",
942 _dbus_verbose ("Got %d groups for UID %lu\n",
948 return TRUE; /* successfully got 0 groups */
952 bus_connection_is_in_unix_group (DBusConnection *connection,
956 unsigned long *group_ids;
959 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
964 while (i < n_group_ids)
966 if (group_ids[i] == gid)
968 dbus_free (group_ids);
974 dbus_free (group_ids);
979 bus_connection_get_loginfo (DBusConnection *connection)
981 BusConnectionData *d;
983 d = BUS_CONNECTION_DATA (connection);
985 if (!bus_connection_is_active (connection))
987 return d->cached_loginfo_string;
991 bus_connection_get_policy (DBusConnection *connection)
993 BusConnectionData *d;
995 d = BUS_CONNECTION_DATA (connection);
997 _dbus_assert (d != NULL);
998 _dbus_assert (d->policy != NULL);
1004 foreach_active (BusConnections *connections,
1005 BusConnectionForeachFunction function,
1010 link = _dbus_list_get_first_link (&connections->completed);
1011 while (link != NULL)
1013 DBusConnection *connection = link->data;
1014 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1016 if (!(* function) (connection, data))
1026 foreach_inactive (BusConnections *connections,
1027 BusConnectionForeachFunction function,
1032 link = _dbus_list_get_first_link (&connections->incomplete);
1033 while (link != NULL)
1035 DBusConnection *connection = link->data;
1036 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1038 if (!(* function) (connection, data))
1048 * Calls function on each active connection; if the function returns
1049 * #FALSE, stops iterating. Active connections are authenticated
1050 * and have sent a Hello message.
1052 * @param connections the connections object
1053 * @param function the function
1054 * @param data data to pass to it as a second arg
1057 bus_connections_foreach_active (BusConnections *connections,
1058 BusConnectionForeachFunction function,
1061 foreach_active (connections, function, data);
1065 * Calls function on each connection; if the function returns
1066 * #FALSE, stops iterating.
1068 * @param connections the connections object
1069 * @param function the function
1070 * @param data data to pass to it as a second arg
1073 bus_connections_foreach (BusConnections *connections,
1074 BusConnectionForeachFunction function,
1077 if (!foreach_active (connections, function, data))
1080 foreach_inactive (connections, function, data);
1084 bus_connections_get_context (BusConnections *connections)
1086 return connections->context;
1090 * This is used to avoid covering the same connection twice when
1091 * traversing connections. Note that it assumes we will
1092 * bus_connection_mark_stamp() each connection at least once per
1093 * INT_MAX increments of the global stamp, or wraparound would break
1097 bus_connections_increment_stamp (BusConnections *connections)
1099 connections->stamp += 1;
1102 /* Mark connection with current stamp, return TRUE if it
1103 * didn't already have that stamp
1106 bus_connection_mark_stamp (DBusConnection *connection)
1108 BusConnectionData *d;
1110 d = BUS_CONNECTION_DATA (connection);
1112 _dbus_assert (d != NULL);
1114 if (d->stamp == d->connections->stamp)
1118 d->stamp = d->connections->stamp;
1124 bus_connection_get_context (DBusConnection *connection)
1126 BusConnectionData *d;
1128 d = BUS_CONNECTION_DATA (connection);
1130 _dbus_assert (d != NULL);
1132 return d->connections->context;
1136 bus_connection_get_connections (DBusConnection *connection)
1138 BusConnectionData *d;
1140 d = BUS_CONNECTION_DATA (connection);
1142 _dbus_assert (d != NULL);
1144 return d->connections;
1148 bus_connection_get_registry (DBusConnection *connection)
1150 BusConnectionData *d;
1152 d = BUS_CONNECTION_DATA (connection);
1154 _dbus_assert (d != NULL);
1156 return bus_context_get_registry (d->connections->context);
1160 bus_connection_get_activation (DBusConnection *connection)
1162 BusConnectionData *d;
1164 d = BUS_CONNECTION_DATA (connection);
1166 _dbus_assert (d != NULL);
1168 return bus_context_get_activation (d->connections->context);
1172 bus_connection_get_matchmaker (DBusConnection *connection)
1174 BusConnectionData *d;
1176 d = BUS_CONNECTION_DATA (connection);
1178 _dbus_assert (d != NULL);
1180 return bus_context_get_matchmaker (d->connections->context);
1184 bus_connection_get_selinux_id (DBusConnection *connection)
1186 BusConnectionData *d;
1188 d = BUS_CONNECTION_DATA (connection);
1190 _dbus_assert (d != NULL);
1192 return d->selinux_id;
1196 * Checks whether the connection is registered with the message bus.
1198 * @param connection the connection
1199 * @returns #TRUE if we're an active message bus participant
1202 bus_connection_is_active (DBusConnection *connection)
1204 BusConnectionData *d;
1206 d = BUS_CONNECTION_DATA (connection);
1208 return d != NULL && d->name != NULL;
1212 bus_connection_preallocate_oom_error (DBusConnection *connection)
1214 DBusMessage *message;
1215 DBusPreallocatedSend *preallocated;
1216 BusConnectionData *d;
1218 d = BUS_CONNECTION_DATA (connection);
1220 _dbus_assert (d != NULL);
1222 if (d->oom_preallocated != NULL)
1225 preallocated = dbus_connection_preallocate_send (connection);
1226 if (preallocated == NULL)
1229 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1231 if (message == NULL)
1233 dbus_connection_free_preallocated_send (connection, preallocated);
1237 /* d->name may be NULL, but that is OK */
1238 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1239 !dbus_message_set_destination (message, d->name) ||
1240 !dbus_message_set_sender (message,
1243 dbus_connection_free_preallocated_send (connection, preallocated);
1244 dbus_message_unref (message);
1248 /* set reply serial to placeholder value just so space is already allocated
1251 if (!dbus_message_set_reply_serial (message, 14))
1253 dbus_connection_free_preallocated_send (connection, preallocated);
1254 dbus_message_unref (message);
1258 d->oom_message = message;
1259 d->oom_preallocated = preallocated;
1265 bus_connection_send_oom_error (DBusConnection *connection,
1266 DBusMessage *in_reply_to)
1268 BusConnectionData *d;
1270 d = BUS_CONNECTION_DATA (connection);
1272 _dbus_assert (d != NULL);
1273 _dbus_assert (d->oom_message != NULL);
1275 /* should always succeed since we set it to a placeholder earlier */
1276 if (!dbus_message_set_reply_serial (d->oom_message,
1277 dbus_message_get_serial (in_reply_to)))
1278 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1280 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1282 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1283 d->oom_message, NULL);
1285 dbus_message_unref (d->oom_message);
1286 d->oom_message = NULL;
1287 d->oom_preallocated = NULL;
1290 #ifdef DBUS_ENABLE_STATS
1292 update_peak (int *peak,
1301 bus_connection_add_match_rule_link (DBusConnection *connection,
1304 BusConnectionData *d;
1306 d = BUS_CONNECTION_DATA (connection);
1307 _dbus_assert (d != NULL);
1309 _dbus_list_append_link (&d->match_rules, link);
1311 d->n_match_rules += 1;
1313 #ifdef DBUS_ENABLE_STATS
1314 update_peak (&d->peak_match_rules, d->n_match_rules);
1315 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1317 d->connections->total_match_rules += 1;
1318 update_peak (&d->connections->peak_match_rules,
1319 d->connections->total_match_rules);
1324 bus_connection_add_match_rule (DBusConnection *connection,
1329 link = _dbus_list_alloc_link (rule);
1334 bus_connection_add_match_rule_link (connection, link);
1340 bus_connection_remove_match_rule (DBusConnection *connection,
1343 BusConnectionData *d;
1345 d = BUS_CONNECTION_DATA (connection);
1346 _dbus_assert (d != NULL);
1348 _dbus_list_remove_last (&d->match_rules, rule);
1350 d->n_match_rules -= 1;
1351 _dbus_assert (d->n_match_rules >= 0);
1353 #ifdef DBUS_ENABLE_STATS
1354 d->connections->total_match_rules -= 1;
1359 bus_connection_get_n_match_rules (DBusConnection *connection)
1361 BusConnectionData *d;
1363 d = BUS_CONNECTION_DATA (connection);
1364 _dbus_assert (d != NULL);
1366 return d->n_match_rules;
1370 bus_connection_add_owned_service_link (DBusConnection *connection,
1373 BusConnectionData *d;
1375 d = BUS_CONNECTION_DATA (connection);
1376 _dbus_assert (d != NULL);
1378 _dbus_list_append_link (&d->services_owned, link);
1380 d->n_services_owned += 1;
1382 #ifdef DBUS_ENABLE_STATS
1383 update_peak (&d->peak_bus_names, d->n_services_owned);
1384 update_peak (&d->connections->peak_bus_names_per_conn,
1385 d->n_services_owned);
1387 d->connections->total_bus_names += 1;
1388 update_peak (&d->connections->peak_bus_names,
1389 d->connections->total_bus_names);
1394 bus_connection_add_owned_service (DBusConnection *connection,
1395 BusService *service)
1399 link = _dbus_list_alloc_link (service);
1404 bus_connection_add_owned_service_link (connection, link);
1410 bus_connection_remove_owned_service (DBusConnection *connection,
1411 BusService *service)
1413 BusConnectionData *d;
1415 d = BUS_CONNECTION_DATA (connection);
1416 _dbus_assert (d != NULL);
1418 _dbus_list_remove_last (&d->services_owned, service);
1420 d->n_services_owned -= 1;
1421 _dbus_assert (d->n_services_owned >= 0);
1423 #ifdef DBUS_ENABLE_STATS
1424 d->connections->total_bus_names -= 1;
1429 bus_connection_get_n_services_owned (DBusConnection *connection)
1431 BusConnectionData *d;
1433 d = BUS_CONNECTION_DATA (connection);
1434 _dbus_assert (d != NULL);
1436 return d->n_services_owned;
1440 bus_connection_complete (DBusConnection *connection,
1441 const DBusString *name,
1444 BusConnectionData *d;
1447 d = BUS_CONNECTION_DATA (connection);
1448 _dbus_assert (d != NULL);
1449 _dbus_assert (d->name == NULL);
1450 _dbus_assert (d->policy == NULL);
1452 _dbus_assert (!bus_connection_is_active (connection));
1454 if (!_dbus_string_copy_data (name, &d->name))
1456 BUS_SET_OOM (error);
1460 _dbus_assert (d->name != NULL);
1462 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1464 d->policy = bus_context_create_client_policy (d->connections->context,
1468 /* we may have a NULL policy on OOM or error getting list of
1469 * groups for a user. In the latter case we don't handle it so
1470 * well currently, as it will just keep failing over and over.
1473 if (d->policy == NULL)
1475 _dbus_verbose ("Failed to create security policy for connection %p\n",
1477 _DBUS_ASSERT_ERROR_IS_SET (error);
1478 dbus_free (d->name);
1483 if (dbus_connection_get_unix_user (connection, &uid))
1485 if (!adjust_connections_for_uid (d->connections,
1490 /* Create and cache a string which holds information about the
1491 * peer process; used for logging purposes.
1493 if (!cache_peer_loginfo_string (d, connection))
1496 /* Now the connection is active, move it between lists */
1497 _dbus_list_unlink (&d->connections->incomplete,
1498 d->link_in_connection_list);
1499 d->connections->n_incomplete -= 1;
1500 _dbus_list_append_link (&d->connections->completed,
1501 d->link_in_connection_list);
1502 d->connections->n_completed += 1;
1504 _dbus_assert (d->connections->n_incomplete >= 0);
1505 _dbus_assert (d->connections->n_completed > 0);
1507 /* If we have dropped below the max. number of incomplete
1508 * connections, start accept()ing again */
1509 bus_context_check_all_watches (d->connections->context);
1511 /* See if we can remove the timeout */
1512 bus_connections_expire_incomplete (d->connections);
1514 _dbus_assert (bus_connection_is_active (connection));
1518 BUS_SET_OOM (error);
1519 dbus_free (d->name);
1522 bus_client_policy_unref (d->policy);
1528 bus_connections_reload_policy (BusConnections *connections,
1531 BusConnectionData *d;
1532 DBusConnection *connection;
1535 _dbus_assert (connections != NULL);
1536 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1538 for (link = _dbus_list_get_first_link (&(connections->completed));
1540 link = _dbus_list_get_next_link (&(connections->completed), link))
1542 connection = link->data;
1543 d = BUS_CONNECTION_DATA (connection);
1544 _dbus_assert (d != NULL);
1545 _dbus_assert (d->policy != NULL);
1547 bus_client_policy_unref (d->policy);
1548 d->policy = bus_context_create_client_policy (connections->context,
1551 if (d->policy == NULL)
1553 _dbus_verbose ("Failed to create security policy for connection %p\n",
1555 _DBUS_ASSERT_ERROR_IS_SET (error);
1564 bus_connection_get_name (DBusConnection *connection)
1566 BusConnectionData *d;
1568 d = BUS_CONNECTION_DATA (connection);
1569 _dbus_assert (d != NULL);
1575 * Check whether completing the passed-in connection would
1576 * exceed limits, and if so set error and return #FALSE
1579 bus_connections_check_limits (BusConnections *connections,
1580 DBusConnection *requesting_completion,
1585 if (connections->n_completed >=
1586 bus_context_get_max_completed_connections (connections->context))
1588 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1589 "The maximum number of active connections has been reached");
1593 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1595 if (get_connections_for_uid (connections, uid) >=
1596 bus_context_get_max_connections_per_user (connections->context))
1598 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1599 "The maximum number of active connections for UID %lu has been reached",
1609 bus_pending_reply_free (BusPendingReply *pending)
1611 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1613 pending->will_send_reply,
1614 pending->will_get_reply,
1615 pending->reply_serial);
1617 dbus_free (pending);
1621 bus_pending_reply_send_no_reply (BusConnections *connections,
1622 BusTransaction *transaction,
1623 BusPendingReply *pending)
1625 DBusMessage *message;
1626 DBusMessageIter iter;
1632 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1633 if (message == NULL)
1636 dbus_message_set_no_reply (message, TRUE);
1638 if (!dbus_message_set_reply_serial (message,
1639 pending->reply_serial))
1642 if (!dbus_message_set_error_name (message,
1643 DBUS_ERROR_NO_REPLY))
1646 /* If you change these messages, adjust test/dbus-daemon.c to match */
1647 if (pending->will_send_reply == NULL)
1648 errmsg = "Message recipient disconnected from message bus without replying";
1650 errmsg = "Message did not receive a reply (timeout by message bus)";
1652 dbus_message_iter_init_append (message, &iter);
1653 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1656 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1663 dbus_message_unref (message);
1668 bus_pending_reply_expired (BusExpireList *list,
1672 BusPendingReply *pending = link->data;
1673 BusConnections *connections = data;
1674 BusTransaction *transaction;
1676 /* No reply is forthcoming. So nuke it if we can. If not,
1677 * leave it in the list to try expiring again later when we
1681 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1683 pending->will_send_reply,
1684 pending->will_get_reply,
1685 pending->reply_serial);
1687 transaction = bus_transaction_new (connections->context);
1688 if (transaction == NULL)
1691 if (!bus_pending_reply_send_no_reply (connections,
1695 bus_transaction_cancel_and_free (transaction);
1699 bus_expire_list_remove_link (connections->pending_replies, link);
1701 bus_pending_reply_free (pending);
1702 bus_transaction_execute_and_free (transaction);
1708 bus_connection_drop_pending_replies (BusConnections *connections,
1709 DBusConnection *connection)
1711 /* The DBusConnection is almost 100% finalized here, so you can't
1712 * do anything with it except check for pointer equality
1716 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1719 link = bus_expire_list_get_first_link (connections->pending_replies);
1720 while (link != NULL)
1723 BusPendingReply *pending;
1725 next = bus_expire_list_get_next_link (connections->pending_replies,
1727 pending = link->data;
1729 if (pending->will_get_reply == connection)
1731 /* We don't need to track this pending reply anymore */
1733 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1735 pending->will_send_reply,
1736 pending->will_get_reply,
1737 pending->reply_serial);
1739 bus_expire_list_remove_link (connections->pending_replies,
1741 bus_pending_reply_free (pending);
1743 else if (pending->will_send_reply == connection)
1745 /* The reply isn't going to be sent, so set things
1746 * up so it will be expired right away
1748 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1750 pending->will_send_reply,
1751 pending->will_get_reply,
1752 pending->reply_serial);
1754 pending->will_send_reply = NULL;
1755 pending->expire_item.added_tv_sec = 0;
1756 pending->expire_item.added_tv_usec = 0;
1758 bus_expire_list_recheck_immediately (connections->pending_replies);
1768 BusPendingReply *pending;
1769 BusConnections *connections;
1770 } CancelPendingReplyData;
1773 cancel_pending_reply (void *data)
1775 CancelPendingReplyData *d = data;
1777 _dbus_verbose ("d = %p\n", d);
1779 if (!bus_expire_list_remove (d->connections->pending_replies,
1780 &d->pending->expire_item))
1781 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1783 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1787 cancel_pending_reply_data_free (void *data)
1789 CancelPendingReplyData *d = data;
1791 _dbus_verbose ("d = %p\n", d);
1793 /* d->pending should be either freed or still
1794 * in the list of pending replies (owned by someone
1802 * Record that a reply is allowed; return TRUE on success.
1805 bus_connections_expect_reply (BusConnections *connections,
1806 BusTransaction *transaction,
1807 DBusConnection *will_get_reply,
1808 DBusConnection *will_send_reply,
1809 DBusMessage *reply_to_this,
1812 BusPendingReply *pending;
1813 dbus_uint32_t reply_serial;
1815 CancelPendingReplyData *cprd;
1818 _dbus_assert (will_get_reply != NULL);
1819 _dbus_assert (will_send_reply != NULL);
1820 _dbus_assert (reply_to_this != NULL);
1822 if (dbus_message_get_no_reply (reply_to_this))
1823 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1825 reply_serial = dbus_message_get_serial (reply_to_this);
1827 link = bus_expire_list_get_first_link (connections->pending_replies);
1829 while (link != NULL)
1831 pending = link->data;
1833 if (pending->reply_serial == reply_serial &&
1834 pending->will_get_reply == will_get_reply &&
1835 pending->will_send_reply == will_send_reply)
1837 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1838 "Message has the same reply serial as a currently-outstanding existing method call");
1842 link = bus_expire_list_get_next_link (connections->pending_replies,
1844 if (pending->will_get_reply == will_get_reply)
1849 bus_context_get_max_replies_per_connection (connections->context))
1851 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1852 "The maximum number of pending replies per connection has been reached");
1856 pending = dbus_new0 (BusPendingReply, 1);
1857 if (pending == NULL)
1859 BUS_SET_OOM (error);
1863 #ifdef DBUS_ENABLE_VERBOSE_MODE
1864 /* so we can see a not-yet-added pending reply */
1865 pending->expire_item.added_tv_sec = 1;
1866 pending->expire_item.added_tv_usec = 1;
1869 pending->will_get_reply = will_get_reply;
1870 pending->will_send_reply = will_send_reply;
1871 pending->reply_serial = reply_serial;
1873 cprd = dbus_new0 (CancelPendingReplyData, 1);
1876 BUS_SET_OOM (error);
1877 bus_pending_reply_free (pending);
1881 if (!bus_expire_list_add (connections->pending_replies,
1882 &pending->expire_item))
1884 BUS_SET_OOM (error);
1886 bus_pending_reply_free (pending);
1890 if (!bus_transaction_add_cancel_hook (transaction,
1891 cancel_pending_reply,
1893 cancel_pending_reply_data_free))
1895 BUS_SET_OOM (error);
1896 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1898 bus_pending_reply_free (pending);
1902 cprd->pending = pending;
1903 cprd->connections = connections;
1905 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1906 &pending->expire_item.added_tv_usec);
1908 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1910 pending->will_send_reply,
1911 pending->will_get_reply,
1912 pending->reply_serial);
1920 BusConnections *connections;
1921 } CheckPendingReplyData;
1924 cancel_check_pending_reply (void *data)
1926 CheckPendingReplyData *d = data;
1928 _dbus_verbose ("d = %p\n",d);
1930 bus_expire_list_add_link (d->connections->pending_replies,
1936 check_pending_reply_data_free (void *data)
1938 CheckPendingReplyData *d = data;
1940 _dbus_verbose ("d = %p\n",d);
1942 if (d->link != NULL)
1944 BusPendingReply *pending = d->link->data;
1946 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1947 &pending->expire_item));
1949 bus_pending_reply_free (pending);
1950 _dbus_list_free_link (d->link);
1957 * Check whether a reply is allowed, remove BusPendingReply
1958 * if so, return TRUE if so.
1961 bus_connections_check_reply (BusConnections *connections,
1962 BusTransaction *transaction,
1963 DBusConnection *sending_reply,
1964 DBusConnection *receiving_reply,
1968 CheckPendingReplyData *cprd;
1970 dbus_uint32_t reply_serial;
1972 _dbus_assert (sending_reply != NULL);
1973 _dbus_assert (receiving_reply != NULL);
1975 reply_serial = dbus_message_get_reply_serial (reply);
1977 link = bus_expire_list_get_first_link (connections->pending_replies);
1978 while (link != NULL)
1980 BusPendingReply *pending = link->data;
1982 if (pending->reply_serial == reply_serial &&
1983 pending->will_get_reply == receiving_reply &&
1984 pending->will_send_reply == sending_reply)
1986 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1990 link = bus_expire_list_get_next_link (connections->pending_replies,
1996 _dbus_verbose ("No pending reply expected\n");
2001 cprd = dbus_new0 (CheckPendingReplyData, 1);
2004 BUS_SET_OOM (error);
2008 if (!bus_transaction_add_cancel_hook (transaction,
2009 cancel_check_pending_reply,
2011 check_pending_reply_data_free))
2013 BUS_SET_OOM (error);
2019 cprd->connections = connections;
2021 bus_expire_list_unlink (connections->pending_replies,
2024 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2032 * Note that this is fairly fragile; in particular, don't try to use
2033 * one transaction across any main loop iterations.
2038 BusTransaction *transaction;
2039 DBusMessage *message;
2040 DBusPreallocatedSend *preallocated;
2045 BusTransactionCancelFunction cancel_function;
2046 DBusFreeFunction free_data_function;
2050 struct BusTransaction
2052 DBusList *connections;
2053 BusContext *context;
2054 DBusList *cancel_hooks;
2058 message_to_send_free (DBusConnection *connection,
2059 MessageToSend *to_send)
2061 if (to_send->message)
2062 dbus_message_unref (to_send->message);
2064 if (to_send->preallocated)
2065 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2067 dbus_free (to_send);
2071 cancel_hook_cancel (void *element,
2074 CancelHook *ch = element;
2076 _dbus_verbose ("Running transaction cancel hook\n");
2078 if (ch->cancel_function)
2079 (* ch->cancel_function) (ch->data);
2083 cancel_hook_free (void *element,
2086 CancelHook *ch = element;
2088 if (ch->free_data_function)
2089 (* ch->free_data_function) (ch->data);
2095 free_cancel_hooks (BusTransaction *transaction)
2097 _dbus_list_foreach (&transaction->cancel_hooks,
2098 cancel_hook_free, NULL);
2100 _dbus_list_clear (&transaction->cancel_hooks);
2104 bus_transaction_new (BusContext *context)
2106 BusTransaction *transaction;
2108 transaction = dbus_new0 (BusTransaction, 1);
2109 if (transaction == NULL)
2112 transaction->context = context;
2118 bus_transaction_get_context (BusTransaction *transaction)
2120 return transaction->context;
2124 bus_transaction_send_from_driver (BusTransaction *transaction,
2125 DBusConnection *connection,
2126 DBusMessage *message)
2128 /* We have to set the sender to the driver, and have
2129 * to check security policy since it was not done in
2132 _dbus_verbose ("Sending %s %s %s from driver\n",
2133 dbus_message_get_interface (message) ?
2134 dbus_message_get_interface (message) : "(no interface)",
2135 dbus_message_get_member (message) ?
2136 dbus_message_get_member (message) : "(no member)",
2137 dbus_message_get_error_name (message) ?
2138 dbus_message_get_error_name (message) : "(no error name)");
2140 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2143 if (bus_connection_is_active (connection))
2145 if (!dbus_message_set_destination (message,
2146 bus_connection_get_name (connection)))
2150 /* bus driver never wants a reply */
2151 dbus_message_set_no_reply (message, TRUE);
2153 /* If security policy doesn't allow the message, we silently
2154 * eat it; the driver doesn't care about getting a reply.
2156 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2158 NULL, connection, connection, message, NULL))
2161 return bus_transaction_send (transaction, connection, message);
2165 bus_transaction_send (BusTransaction *transaction,
2166 DBusConnection *connection,
2167 DBusMessage *message)
2169 MessageToSend *to_send;
2170 BusConnectionData *d;
2173 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2174 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2175 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2177 dbus_message_get_interface (message) ?
2178 dbus_message_get_interface (message) : "(unset)",
2179 dbus_message_get_member (message) ?
2180 dbus_message_get_member (message) : "(unset)",
2181 dbus_message_get_error_name (message) ?
2182 dbus_message_get_error_name (message) : "(unset)",
2183 dbus_connection_get_is_connected (connection) ?
2184 "" : " (disconnected)");
2186 _dbus_assert (dbus_message_get_sender (message) != NULL);
2188 if (!dbus_connection_get_is_connected (connection))
2189 return TRUE; /* silently ignore disconnected connections */
2191 d = BUS_CONNECTION_DATA (connection);
2192 _dbus_assert (d != NULL);
2194 to_send = dbus_new (MessageToSend, 1);
2195 if (to_send == NULL)
2200 to_send->preallocated = dbus_connection_preallocate_send (connection);
2201 if (to_send->preallocated == NULL)
2203 dbus_free (to_send);
2207 dbus_message_ref (message);
2208 to_send->message = message;
2209 to_send->transaction = transaction;
2211 _dbus_verbose ("about to prepend message\n");
2213 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2215 message_to_send_free (connection, to_send);
2219 _dbus_verbose ("prepended message\n");
2221 /* See if we already had this connection in the list
2222 * for this transaction. If we have a pending message,
2223 * then we should already be in transaction->connections
2225 link = _dbus_list_get_first_link (&d->transaction_messages);
2226 _dbus_assert (link->data == to_send);
2227 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2228 while (link != NULL)
2230 MessageToSend *m = link->data;
2231 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2233 if (m->transaction == transaction)
2241 if (!_dbus_list_prepend (&transaction->connections, connection))
2243 _dbus_list_remove (&d->transaction_messages, to_send);
2244 message_to_send_free (connection, to_send);
2253 transaction_free (BusTransaction *transaction)
2255 _dbus_assert (transaction->connections == NULL);
2257 free_cancel_hooks (transaction);
2259 dbus_free (transaction);
2263 connection_cancel_transaction (DBusConnection *connection,
2264 BusTransaction *transaction)
2267 BusConnectionData *d;
2269 d = BUS_CONNECTION_DATA (connection);
2270 _dbus_assert (d != NULL);
2272 link = _dbus_list_get_first_link (&d->transaction_messages);
2273 while (link != NULL)
2275 MessageToSend *m = link->data;
2276 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2278 if (m->transaction == transaction)
2280 _dbus_list_remove_link (&d->transaction_messages,
2283 message_to_send_free (connection, m);
2291 bus_transaction_cancel_and_free (BusTransaction *transaction)
2293 DBusConnection *connection;
2295 _dbus_verbose ("TRANSACTION: cancelled\n");
2297 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2298 connection_cancel_transaction (connection, transaction);
2300 _dbus_list_foreach (&transaction->cancel_hooks,
2301 cancel_hook_cancel, NULL);
2303 transaction_free (transaction);
2307 connection_execute_transaction (DBusConnection *connection,
2308 BusTransaction *transaction)
2311 BusConnectionData *d;
2313 d = BUS_CONNECTION_DATA (connection);
2314 _dbus_assert (d != NULL);
2316 /* Send the queue in order (FIFO) */
2317 link = _dbus_list_get_last_link (&d->transaction_messages);
2318 while (link != NULL)
2320 MessageToSend *m = link->data;
2321 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2323 if (m->transaction == transaction)
2325 _dbus_list_remove_link (&d->transaction_messages,
2328 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2330 dbus_connection_send_preallocated (connection,
2335 m->preallocated = NULL; /* so we don't double-free it */
2337 message_to_send_free (connection, m);
2345 bus_transaction_execute_and_free (BusTransaction *transaction)
2347 /* For each connection in transaction->connections
2350 DBusConnection *connection;
2352 _dbus_verbose ("TRANSACTION: executing\n");
2354 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2355 connection_execute_transaction (connection, transaction);
2357 transaction_free (transaction);
2361 bus_connection_remove_transactions (DBusConnection *connection)
2363 MessageToSend *to_send;
2364 BusConnectionData *d;
2366 d = BUS_CONNECTION_DATA (connection);
2367 _dbus_assert (d != NULL);
2369 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2371 /* only has an effect for the first MessageToSend listing this transaction */
2372 _dbus_list_remove (&to_send->transaction->connections,
2375 _dbus_list_remove (&d->transaction_messages, to_send);
2376 message_to_send_free (connection, to_send);
2381 * Converts the DBusError to a message reply
2384 bus_transaction_send_error_reply (BusTransaction *transaction,
2385 DBusConnection *connection,
2386 const DBusError *error,
2387 DBusMessage *in_reply_to)
2391 _dbus_assert (error != NULL);
2392 _DBUS_ASSERT_ERROR_IS_SET (error);
2394 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2395 error->name, error->message);
2397 reply = dbus_message_new_error (in_reply_to,
2403 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2405 dbus_message_unref (reply);
2409 dbus_message_unref (reply);
2415 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2416 BusTransactionCancelFunction cancel_function,
2418 DBusFreeFunction free_data_function)
2422 ch = dbus_new (CancelHook, 1);
2426 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2427 cancel_function, data);
2429 ch->cancel_function = cancel_function;
2431 ch->free_data_function = free_data_function;
2433 /* It's important that the hooks get run in reverse order that they
2436 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2446 bus_connections_get_n_active (BusConnections *connections)
2448 return connections->n_completed;
2452 bus_connections_get_n_incomplete (BusConnections *connections)
2454 return connections->n_incomplete;
2457 #ifdef DBUS_ENABLE_STATS
2459 bus_connections_get_total_match_rules (BusConnections *connections)
2461 return connections->total_match_rules;
2465 bus_connections_get_peak_match_rules (BusConnections *connections)
2467 return connections->peak_match_rules;
2471 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2473 return connections->peak_match_rules_per_conn;
2477 bus_connections_get_total_bus_names (BusConnections *connections)
2479 return connections->total_bus_names;
2483 bus_connections_get_peak_bus_names (BusConnections *connections)
2485 return connections->peak_bus_names;
2489 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2491 return connections->peak_bus_names_per_conn;
2495 bus_connection_get_peak_match_rules (DBusConnection *connection)
2497 BusConnectionData *d;
2499 d = BUS_CONNECTION_DATA (connection);
2500 return d->peak_match_rules;
2504 bus_connection_get_peak_bus_names (DBusConnection *connection)
2506 BusConnectionData *d;
2508 d = BUS_CONNECTION_DATA (connection);
2509 return d->peak_bus_names;
2511 #endif /* DBUS_ENABLE_STATS */
2514 bus_connection_is_monitor (DBusConnection *connection)
2516 BusConnectionData *d;
2518 d = BUS_CONNECTION_DATA (connection);
2520 return d != NULL && d->link_in_monitors != NULL;
2524 bus_connection_be_monitor (DBusConnection *connection,
2525 BusTransaction *transaction,
2528 BusConnectionData *d;
2533 d = BUS_CONNECTION_DATA (connection);
2534 _dbus_assert (d != NULL);
2536 link = _dbus_list_alloc_link (connection);
2540 BUS_SET_OOM (error);
2544 /* release all its names */
2545 if (!_dbus_list_copy (&d->services_owned, &tmp))
2547 _dbus_list_free_link (link);
2548 BUS_SET_OOM (error);
2552 for (iter = _dbus_list_get_first_link (&tmp);
2554 iter = _dbus_list_get_next_link (&tmp, iter))
2556 BusService *service = iter->data;
2558 /* This call is transactional: if there isn't enough memory to
2559 * do everything, then the service gets all its names back when
2560 * the transaction is cancelled due to OOM. */
2561 if (!bus_service_remove_owner (service, connection, transaction, error))
2563 _dbus_list_free_link (link);
2564 _dbus_list_clear (&tmp);
2569 /* We have now done everything that can fail, so there is no problem
2570 * with doing the irrevocable stuff. */
2572 _dbus_list_clear (&tmp);
2574 bus_context_log (transaction->context, DBUS_SYSTEM_LOG_INFO,
2575 "Connection %s (%s) became a monitor.", d->name,
2576 d->cached_loginfo_string);
2578 if (d->n_match_rules > 0)
2582 mm = bus_context_get_matchmaker (d->connections->context);
2583 bus_matchmaker_disconnected (mm, connection);
2586 /* flag it as a monitor */
2587 d->link_in_monitors = link;
2588 _dbus_list_append_link (&d->connections->monitors, link);
2590 /* it isn't allowed to reply, and it is no longer relevant whether it
2591 * receives replies */
2592 bus_connection_drop_pending_replies (d->connections, connection);