1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
5 * Copyright (C) 2013 Samsung Electronics
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "connection.h"
32 #include "expirelist.h"
34 #include <dbus/dbus-list.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-timeout.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 #ifdef DBUS_ENABLE_STATS
68 int total_match_rules;
70 int peak_match_rules_per_conn;
74 int peak_bus_names_per_conn;
78 static dbus_int32_t connection_data_slot = -1;
82 BusConnections *connections;
83 DBusList *link_in_connection_list;
84 DBusConnection *connection;
85 DBusList *services_owned;
87 DBusList *match_rules;
90 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
91 DBusMessage *oom_message;
92 DBusPreallocatedSend *oom_preallocated;
93 BusClientPolicy *policy;
95 char *cached_loginfo_string;
96 BusSELinuxID *selinux_id;
98 long connection_tv_sec; /**< Time when we connected (seconds component) */
99 long connection_tv_usec; /**< Time when we connected (microsec component) */
100 int stamp; /**< connections->stamp last time we were traversed */
102 #ifdef DBUS_ENABLE_STATS
103 int peak_match_rules;
108 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
112 static void bus_connection_drop_pending_replies (BusConnections *connections,
113 DBusConnection *connection);
115 static dbus_bool_t expire_incomplete_timeout (void *data);
117 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
120 connection_get_loop (DBusConnection *connection)
122 BusConnectionData *d;
124 d = BUS_CONNECTION_DATA (connection);
126 return bus_context_get_loop (d->connections->context);
131 get_connections_for_uid (BusConnections *connections,
137 /* val is NULL is 0 when it isn't in the hash yet */
139 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
142 current_count = _DBUS_POINTER_TO_INT (val);
144 return current_count;
148 adjust_connections_for_uid (BusConnections *connections,
154 current_count = get_connections_for_uid (connections, uid);
156 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
157 ": was %d adjustment %d making %d\n",
158 uid, current_count, adjustment, current_count + adjustment);
160 _dbus_assert (current_count >= 0);
162 current_count += adjustment;
164 _dbus_assert (current_count >= 0);
166 if (current_count == 0)
168 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
175 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
176 uid, _DBUS_INT_TO_POINTER (current_count));
178 /* only positive adjustment can fail as otherwise
179 * a hash entry should already exist
181 _dbus_assert (adjustment > 0 ||
182 (adjustment <= 0 && retval));
189 bus_connection_disconnected (DBusConnection *connection)
191 BusConnectionData *d;
193 BusMatchmaker *matchmaker;
194 dbus_bool_t is_phantom = FALSE;
196 d = BUS_CONNECTION_DATA (connection);
197 _dbus_assert (d != NULL);
199 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
200 d->name ? d->name : "(inactive)");
202 if(bus_context_is_kdbus(d->connections->context) && (strcmp(bus_connection_get_name(connection), ":1.1")))
205 /* Delete our match rules */
206 if (d->n_match_rules > 0)
208 matchmaker = bus_context_get_matchmaker (d->connections->context);
209 bus_matchmaker_disconnected (matchmaker, connection);
212 /* Drop any service ownership. Unfortunately, this requires
213 * memory allocation and there doesn't seem to be a good way to
214 * handle it other than sleeping; we can't "fail" the operation of
215 * disconnecting a client, and preallocating a broadcast "service is
216 * now gone" message for every client-service pair seems kind of
219 while ((service = _dbus_list_get_last (&d->services_owned)))
221 BusTransaction *transaction;
226 dbus_error_init (&error);
228 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
229 _dbus_wait_for_memory ();
231 if (!bus_service_remove_owner (service, connection,
232 transaction, &error))
234 _DBUS_ASSERT_ERROR_IS_SET (&error);
236 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
238 dbus_error_free (&error);
239 bus_transaction_cancel_and_free (transaction);
240 _dbus_wait_for_memory ();
245 _dbus_verbose ("Failed to remove service owner: %s %s\n",
246 error.name, error.message);
247 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
251 bus_transaction_execute_and_free (transaction);
254 bus_dispatch_remove_connection (connection);
256 /* no more watching */
257 if (!dbus_connection_set_watch_functions (connection,
261 _dbus_assert_not_reached ("setting watch functions to NULL failed");
263 if (!dbus_connection_set_timeout_functions (connection,
267 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
269 dbus_connection_set_unix_user_function (connection,
271 dbus_connection_set_windows_user_function (connection,
274 dbus_connection_set_dispatch_status_function (connection,
277 bus_connection_remove_transactions (connection);
279 if (d->link_in_connection_list != NULL)
285 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
286 d->link_in_connection_list = NULL;
287 d->connections->n_completed -= 1;
289 if (dbus_connection_get_unix_user (connection, &uid))
291 if (!adjust_connections_for_uid (d->connections,
293 _dbus_assert_not_reached ("adjusting downward should never fail");
298 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
299 d->link_in_connection_list = NULL;
300 d->connections->n_incomplete -= 1;
303 _dbus_assert (d->connections->n_incomplete >= 0);
304 _dbus_assert (d->connections->n_completed >= 0);
307 bus_connection_drop_pending_replies (d->connections, connection);
309 /* frees "d" as side effect */
310 dbus_connection_set_data (connection,
311 connection_data_slot,
314 dbus_connection_unref_phantom(connection);
316 dbus_connection_unref (connection);
320 add_connection_watch (DBusWatch *watch,
323 DBusConnection *connection = data;
325 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
329 remove_connection_watch (DBusWatch *watch,
332 DBusConnection *connection = data;
334 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
338 toggle_connection_watch (DBusWatch *watch,
341 DBusConnection *connection = data;
343 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
347 add_connection_timeout (DBusTimeout *timeout,
350 DBusConnection *connection = data;
352 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
356 remove_connection_timeout (DBusTimeout *timeout,
359 DBusConnection *connection = data;
361 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
365 dispatch_status_function (DBusConnection *connection,
366 DBusDispatchStatus new_status,
369 DBusLoop *loop = data;
371 if (new_status != DBUS_DISPATCH_COMPLETE)
373 while (!_dbus_loop_queue_dispatch (loop, connection))
374 _dbus_wait_for_memory ();
379 allow_unix_user_function (DBusConnection *connection,
383 BusConnectionData *d;
385 d = BUS_CONNECTION_DATA (connection);
387 _dbus_assert (d != NULL);
389 return bus_context_allow_unix_user (d->connections->context, uid);
393 free_connection_data (void *data)
395 BusConnectionData *d = data;
397 /* services_owned should be NULL since we should be disconnected */
398 _dbus_assert (d->services_owned == NULL);
399 _dbus_assert (d->n_services_owned == 0);
401 _dbus_assert (d->transaction_messages == NULL);
403 if (d->oom_preallocated)
404 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
407 dbus_message_unref (d->oom_message);
410 bus_client_policy_unref (d->policy);
413 bus_selinux_id_unref (d->selinux_id);
415 dbus_free (d->cached_loginfo_string);
423 bus_connections_new (BusContext *context)
425 BusConnections *connections;
427 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
430 connections = dbus_new0 (BusConnections, 1);
431 if (connections == NULL)
434 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
436 if (connections->completed_by_user == NULL)
439 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
440 expire_incomplete_timeout,
442 if (connections->expire_timeout == NULL)
445 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
447 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
448 bus_context_get_reply_timeout (context),
449 bus_pending_reply_expired,
451 if (connections->pending_replies == NULL)
454 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
455 connections->expire_timeout))
458 connections->refcount = 1;
459 connections->context = context;
464 bus_expire_list_free (connections->pending_replies);
466 _dbus_timeout_unref (connections->expire_timeout);
468 _dbus_hash_table_unref (connections->completed_by_user);
470 dbus_free (connections);
472 dbus_connection_free_data_slot (&connection_data_slot);
478 bus_connections_ref (BusConnections *connections)
480 _dbus_assert (connections->refcount > 0);
481 connections->refcount += 1;
487 bus_connections_unref (BusConnections *connections)
489 _dbus_assert (connections->refcount > 0);
490 connections->refcount -= 1;
491 if (connections->refcount == 0)
493 /* drop all incomplete */
494 while (connections->incomplete != NULL)
496 DBusConnection *connection;
498 connection = connections->incomplete->data;
500 dbus_connection_ref (connection);
501 dbus_connection_close (connection);
502 bus_connection_disconnected (connection);
503 dbus_connection_unref (connection);
506 _dbus_assert (connections->n_incomplete == 0);
508 /* drop all real connections */
509 while (connections->completed != NULL)
511 DBusConnection *connection;
513 connection = connections->completed->data;
515 dbus_connection_ref (connection);
516 dbus_connection_close (connection);
517 bus_connection_disconnected (connection);
518 dbus_connection_unref (connection);
521 _dbus_assert (connections->n_completed == 0);
523 bus_expire_list_free (connections->pending_replies);
525 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
526 connections->expire_timeout);
528 _dbus_timeout_unref (connections->expire_timeout);
530 _dbus_hash_table_unref (connections->completed_by_user);
532 dbus_free (connections);
534 dbus_connection_free_data_slot (&connection_data_slot);
538 /* Used for logging */
540 cache_peer_loginfo_string (BusConnectionData *d,
541 DBusConnection *connection)
543 DBusString loginfo_buf;
547 dbus_bool_t prev_added;
549 if (!_dbus_string_init (&loginfo_buf))
553 if (dbus_connection_get_unix_user (connection, &uid))
555 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
561 if (dbus_connection_get_unix_process_id (connection, &pid))
565 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
568 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
570 /* Ignore errors here; we may not have permissions to read the
572 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
573 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
577 if (dbus_connection_get_windows_user (connection, &windows_sid))
579 dbus_bool_t did_append;
580 did_append = _dbus_string_append_printf (&loginfo_buf,
581 "sid=\"%s\" ", windows_sid);
582 dbus_free (windows_sid);
587 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
590 _dbus_string_free (&loginfo_buf);
594 _dbus_string_free (&loginfo_buf);
599 bus_connections_setup_connection (BusConnections *connections,
600 DBusConnection *connection)
603 BusConnectionData *d;
608 d = dbus_new0 (BusConnectionData, 1);
613 d->connections = connections;
614 d->connection = connection;
616 _dbus_get_monotonic_time (&d->connection_tv_sec,
617 &d->connection_tv_usec);
619 _dbus_assert (connection_data_slot >= 0);
621 if (!dbus_connection_set_data (connection,
622 connection_data_slot,
623 d, free_connection_data))
629 dbus_connection_set_route_peer_messages (connection, TRUE);
633 dbus_error_init (&error);
634 d->selinux_id = bus_selinux_init_connection_id (connection,
636 if (dbus_error_is_set (&error))
638 /* This is a bit bogus because we pretend all errors
639 * are OOM; this is done because we know that in bus.c
640 * an OOM error disconnects the connection, which is
641 * the same thing we want on any other error.
643 dbus_error_free (&error);
647 if (!dbus_connection_set_watch_functions (connection,
648 add_connection_watch,
649 remove_connection_watch,
650 toggle_connection_watch,
655 if (!dbus_connection_set_timeout_functions (connection,
656 add_connection_timeout,
657 remove_connection_timeout,
662 /* For now we don't need to set a Windows user function because
663 * there are no policies in the config file controlling what
664 * Windows users can connect. The default 'same user that owns the
665 * bus can connect' behavior of DBusConnection is fine on Windows.
667 dbus_connection_set_unix_user_function (connection,
668 allow_unix_user_function,
671 dbus_connection_set_dispatch_status_function (connection,
672 dispatch_status_function,
673 bus_context_get_loop (connections->context),
676 d->link_in_connection_list = _dbus_list_alloc_link (connection);
677 if (d->link_in_connection_list == NULL)
680 /* Setup the connection with the dispatcher */
681 if (!bus_dispatch_add_connection (connection))
684 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
686 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
688 bus_dispatch_remove_connection (connection);
693 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
694 connections->n_incomplete += 1;
696 dbus_connection_ref (connection);
698 /* Note that we might disconnect ourselves here, but it only takes
699 * effect on return to the main loop. We call this to free up
700 * expired connections if possible, and to queue the timeout for our
703 bus_connections_expire_incomplete (connections);
705 /* And we might also disconnect ourselves here, but again it
706 * only takes effect on return to main loop.
708 if (connections->n_incomplete >
709 bus_context_get_max_incomplete_connections (connections->context))
711 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
713 _dbus_assert (connections->incomplete != NULL);
714 /* Disconnect the oldest unauthenticated connection. FIXME
715 * would it be more secure to drop a *random* connection? This
716 * algorithm seems to mean that if someone can create new
717 * connections quickly enough, they can keep anyone else from
718 * completing authentication. But random may or may not really
719 * help with that, a more elaborate solution might be required.
721 dbus_connection_close (connections->incomplete->data);
730 bus_selinux_id_unref (d->selinux_id);
731 d->selinux_id = NULL;
733 if (!dbus_connection_set_watch_functions (connection,
737 _dbus_assert_not_reached ("setting watch functions to NULL failed");
739 if (!dbus_connection_set_timeout_functions (connection,
743 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
745 dbus_connection_set_unix_user_function (connection,
748 dbus_connection_set_windows_user_function (connection,
751 dbus_connection_set_dispatch_status_function (connection,
754 if (d->link_in_connection_list != NULL)
756 _dbus_assert (d->link_in_connection_list->next == NULL);
757 _dbus_assert (d->link_in_connection_list->prev == NULL);
758 _dbus_list_free_link (d->link_in_connection_list);
759 d->link_in_connection_list = NULL;
762 if (!dbus_connection_set_data (connection,
763 connection_data_slot,
765 _dbus_assert_not_reached ("failed to set connection data to null");
767 /* "d" has now been freed */
774 bus_connections_expire_incomplete (BusConnections *connections)
780 if (connections->incomplete != NULL)
782 long tv_sec, tv_usec;
786 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
787 auth_timeout = bus_context_get_auth_timeout (connections->context);
789 link = _dbus_list_get_first_link (&connections->incomplete);
792 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
793 DBusConnection *connection;
794 BusConnectionData *d;
797 connection = link->data;
799 d = BUS_CONNECTION_DATA (connection);
801 _dbus_assert (d != NULL);
803 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
804 d->connection_tv_usec,
807 if (elapsed >= (double) auth_timeout)
809 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
810 dbus_connection_close (connection);
814 /* We can end the loop, since the connections are in oldest-first order */
815 next_interval = ((double)auth_timeout) - elapsed;
816 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
817 connection, next_interval);
826 bus_expire_timeout_set_interval (connections->expire_timeout,
831 expire_incomplete_timeout (void *data)
833 BusConnections *connections = data;
835 _dbus_verbose ("Running\n");
837 /* note that this may remove the timeout */
838 bus_connections_expire_incomplete (connections);
844 bus_connection_get_unix_groups (DBusConnection *connection,
845 unsigned long **groups,
854 if (dbus_connection_get_unix_user (connection, &uid))
856 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
858 _dbus_verbose ("Did not get any groups for UID %lu\n",
864 _dbus_verbose ("Got %d groups for UID %lu\n",
870 return TRUE; /* successfully got 0 groups */
874 bus_connection_is_in_unix_group (DBusConnection *connection,
878 unsigned long *group_ids;
881 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
886 while (i < n_group_ids)
888 if (group_ids[i] == gid)
890 dbus_free (group_ids);
896 dbus_free (group_ids);
901 bus_connection_get_loginfo (DBusConnection *connection)
903 BusConnectionData *d;
905 d = BUS_CONNECTION_DATA (connection);
907 if (!bus_connection_is_active (connection))
909 return d->cached_loginfo_string;
913 bus_connection_get_policy (DBusConnection *connection)
915 BusConnectionData *d;
917 d = BUS_CONNECTION_DATA (connection);
919 _dbus_assert (d != NULL);
920 _dbus_assert (d->policy != NULL);
926 foreach_active (BusConnections *connections,
927 BusConnectionForeachFunction function,
932 link = _dbus_list_get_first_link (&connections->completed);
935 DBusConnection *connection = link->data;
936 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
938 if (!(* function) (connection, data))
948 foreach_inactive (BusConnections *connections,
949 BusConnectionForeachFunction function,
954 link = _dbus_list_get_first_link (&connections->incomplete);
957 DBusConnection *connection = link->data;
958 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
960 if (!(* function) (connection, data))
970 * Calls function on each active connection; if the function returns
971 * #FALSE, stops iterating. Active connections are authenticated
972 * and have sent a Hello message.
974 * @param connections the connections object
975 * @param function the function
976 * @param data data to pass to it as a second arg
979 bus_connections_foreach_active (BusConnections *connections,
980 BusConnectionForeachFunction function,
983 foreach_active (connections, function, data);
987 * Calls function on each connection; if the function returns
988 * #FALSE, stops iterating.
990 * @param connections the connections object
991 * @param function the function
992 * @param data data to pass to it as a second arg
995 bus_connections_foreach (BusConnections *connections,
996 BusConnectionForeachFunction function,
999 if (!foreach_active (connections, function, data))
1002 foreach_inactive (connections, function, data);
1006 bus_connections_find_conn_by_name(BusConnections *connections, const char* name)
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 (!strcmp(bus_connection_get_name(connection), name))
1026 bus_connections_get_context (BusConnections *connections)
1028 return connections->context;
1032 * This is used to avoid covering the same connection twice when
1033 * traversing connections. Note that it assumes we will
1034 * bus_connection_mark_stamp() each connection at least once per
1035 * INT_MAX increments of the global stamp, or wraparound would break
1039 bus_connections_increment_stamp (BusConnections *connections)
1041 connections->stamp += 1;
1044 /* Mark connection with current stamp, return TRUE if it
1045 * didn't already have that stamp
1048 bus_connection_mark_stamp (DBusConnection *connection)
1050 BusConnectionData *d;
1052 d = BUS_CONNECTION_DATA (connection);
1054 _dbus_assert (d != NULL);
1056 if (d->stamp == d->connections->stamp)
1060 d->stamp = d->connections->stamp;
1066 bus_connection_get_context (DBusConnection *connection)
1068 BusConnectionData *d;
1070 d = BUS_CONNECTION_DATA (connection);
1072 _dbus_assert (d != NULL);
1074 return d->connections->context;
1078 bus_connection_get_connections (DBusConnection *connection)
1080 BusConnectionData *d;
1082 d = BUS_CONNECTION_DATA (connection);
1084 _dbus_assert (d != NULL);
1086 return d->connections;
1090 bus_connection_get_registry (DBusConnection *connection)
1092 BusConnectionData *d;
1094 d = BUS_CONNECTION_DATA (connection);
1096 _dbus_assert (d != NULL);
1098 return bus_context_get_registry (d->connections->context);
1102 bus_connection_get_activation (DBusConnection *connection)
1104 BusConnectionData *d;
1106 d = BUS_CONNECTION_DATA (connection);
1108 _dbus_assert (d != NULL);
1110 return bus_context_get_activation (d->connections->context);
1114 bus_connection_get_matchmaker (DBusConnection *connection)
1116 BusConnectionData *d;
1118 d = BUS_CONNECTION_DATA (connection);
1120 _dbus_assert (d != NULL);
1122 return bus_context_get_matchmaker (d->connections->context);
1126 bus_connection_get_selinux_id (DBusConnection *connection)
1128 BusConnectionData *d;
1130 d = BUS_CONNECTION_DATA (connection);
1132 _dbus_assert (d != NULL);
1134 return d->selinux_id;
1138 * Checks whether the connection is registered with the message bus.
1140 * @param connection the connection
1141 * @returns #TRUE if we're an active message bus participant
1144 bus_connection_is_active (DBusConnection *connection)
1146 BusConnectionData *d;
1148 d = BUS_CONNECTION_DATA (connection);
1150 return d != NULL && d->name != NULL;
1154 bus_connection_preallocate_oom_error (DBusConnection *connection)
1156 DBusMessage *message;
1157 DBusPreallocatedSend *preallocated;
1158 BusConnectionData *d;
1160 d = BUS_CONNECTION_DATA (connection);
1162 _dbus_assert (d != NULL);
1164 if (d->oom_preallocated != NULL)
1167 preallocated = dbus_connection_preallocate_send (connection);
1168 if (preallocated == NULL)
1171 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1173 if (message == NULL)
1175 dbus_connection_free_preallocated_send (connection, preallocated);
1179 /* d->name may be NULL, but that is OK */
1180 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1181 !dbus_message_set_destination (message, d->name) ||
1182 !dbus_message_set_sender (message,
1185 dbus_connection_free_preallocated_send (connection, preallocated);
1186 dbus_message_unref (message);
1190 /* set reply serial to placeholder value just so space is already allocated
1193 if (!dbus_message_set_reply_serial (message, 14))
1195 dbus_connection_free_preallocated_send (connection, preallocated);
1196 dbus_message_unref (message);
1200 d->oom_message = message;
1201 d->oom_preallocated = preallocated;
1207 bus_connection_send_oom_error (DBusConnection *connection,
1208 DBusMessage *in_reply_to)
1210 BusConnectionData *d;
1212 d = BUS_CONNECTION_DATA (connection);
1214 _dbus_assert (d != NULL);
1215 _dbus_assert (d->oom_message != NULL);
1217 /* should always succeed since we set it to a placeholder earlier */
1218 if (!dbus_message_set_reply_serial (d->oom_message,
1219 dbus_message_get_serial (in_reply_to)))
1220 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1222 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1224 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1225 d->oom_message, NULL);
1227 dbus_message_unref (d->oom_message);
1228 d->oom_message = NULL;
1229 d->oom_preallocated = NULL;
1232 #ifdef DBUS_ENABLE_STATS
1234 update_peak (int *peak,
1243 bus_connection_add_match_rule_link (DBusConnection *connection,
1246 BusConnectionData *d;
1248 d = BUS_CONNECTION_DATA (connection);
1249 _dbus_assert (d != NULL);
1251 _dbus_list_append_link (&d->match_rules, link);
1253 d->n_match_rules += 1;
1255 #ifdef DBUS_ENABLE_STATS
1256 update_peak (&d->peak_match_rules, d->n_match_rules);
1257 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1259 d->connections->total_match_rules += 1;
1260 update_peak (&d->connections->peak_match_rules,
1261 d->connections->total_match_rules);
1266 bus_connection_add_match_rule (DBusConnection *connection,
1271 link = _dbus_list_alloc_link (rule);
1276 bus_connection_add_match_rule_link (connection, link);
1282 bus_connection_remove_match_rule (DBusConnection *connection,
1285 BusConnectionData *d;
1287 d = BUS_CONNECTION_DATA (connection);
1288 _dbus_assert (d != NULL);
1290 _dbus_list_remove_last (&d->match_rules, rule);
1292 d->n_match_rules -= 1;
1293 _dbus_assert (d->n_match_rules >= 0);
1295 #ifdef DBUS_ENABLE_STATS
1296 d->connections->total_match_rules -= 1;
1301 bus_connection_get_n_match_rules (DBusConnection *connection)
1303 BusConnectionData *d;
1305 d = BUS_CONNECTION_DATA (connection);
1306 _dbus_assert (d != NULL);
1308 return d->n_match_rules;
1312 bus_connection_add_owned_service_link (DBusConnection *connection,
1315 BusConnectionData *d;
1317 d = BUS_CONNECTION_DATA (connection);
1318 _dbus_assert (d != NULL);
1320 _dbus_list_append_link (&d->services_owned, link);
1322 d->n_services_owned += 1;
1324 #ifdef DBUS_ENABLE_STATS
1325 update_peak (&d->peak_bus_names, d->n_services_owned);
1326 update_peak (&d->connections->peak_bus_names_per_conn,
1327 d->n_services_owned);
1329 d->connections->total_bus_names += 1;
1330 update_peak (&d->connections->peak_bus_names,
1331 d->connections->total_bus_names);
1336 bus_connection_add_owned_service (DBusConnection *connection,
1337 BusService *service)
1341 link = _dbus_list_alloc_link (service);
1346 bus_connection_add_owned_service_link (connection, link);
1352 bus_connection_remove_owned_service (DBusConnection *connection,
1353 BusService *service)
1355 BusConnectionData *d;
1357 d = BUS_CONNECTION_DATA (connection);
1358 _dbus_assert (d != NULL);
1360 _dbus_list_remove_last (&d->services_owned, service);
1362 d->n_services_owned -= 1;
1363 _dbus_assert (d->n_services_owned >= 0);
1365 #ifdef DBUS_ENABLE_STATS
1366 d->connections->total_bus_names -= 1;
1371 bus_connection_get_n_services_owned (DBusConnection *connection)
1373 BusConnectionData *d;
1375 d = BUS_CONNECTION_DATA (connection);
1376 _dbus_assert (d != NULL);
1378 return d->n_services_owned;
1382 bus_connection_get_services_owned (DBusConnection *connection)
1384 BusConnectionData *d;
1385 d = BUS_CONNECTION_DATA (connection);
1386 _dbus_assert (d != NULL);
1388 return &d->services_owned;
1392 bus_connection_complete (DBusConnection *connection,
1393 const DBusString *name,
1396 BusConnectionData *d;
1399 d = BUS_CONNECTION_DATA (connection);
1400 _dbus_assert (d != NULL);
1401 _dbus_assert (d->name == NULL);
1402 _dbus_assert (d->policy == NULL);
1404 _dbus_assert (!bus_connection_is_active (connection));
1406 if (!_dbus_string_copy_data (name, &d->name))
1408 BUS_SET_OOM (error);
1412 _dbus_assert (d->name != NULL);
1414 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1416 d->policy = bus_context_create_client_policy (d->connections->context,
1420 /* we may have a NULL policy on OOM or error getting list of
1421 * groups for a user. In the latter case we don't handle it so
1422 * well currently, as it will just keep failing over and over.
1425 if (d->policy == NULL)
1427 _dbus_verbose ("Failed to create security policy for connection %p\n",
1429 _DBUS_ASSERT_ERROR_IS_SET (error);
1430 dbus_free (d->name);
1435 if (dbus_connection_get_unix_user (connection, &uid))
1437 if (!adjust_connections_for_uid (d->connections,
1442 /* Create and cache a string which holds information about the
1443 * peer process; used for logging purposes.
1445 if (!cache_peer_loginfo_string (d, connection))
1448 /* Now the connection is active, move it between lists */
1449 _dbus_list_unlink (&d->connections->incomplete,
1450 d->link_in_connection_list);
1451 d->connections->n_incomplete -= 1;
1452 _dbus_list_append_link (&d->connections->completed,
1453 d->link_in_connection_list);
1454 d->connections->n_completed += 1;
1456 _dbus_assert (d->connections->n_incomplete >= 0);
1457 _dbus_assert (d->connections->n_completed > 0);
1459 /* See if we can remove the timeout */
1460 bus_connections_expire_incomplete (d->connections);
1462 _dbus_assert (bus_connection_is_active (connection));
1466 BUS_SET_OOM (error);
1467 dbus_free (d->name);
1470 bus_client_policy_unref (d->policy);
1476 bus_connection_get_name (DBusConnection *connection)
1478 BusConnectionData *d;
1480 d = BUS_CONNECTION_DATA (connection);
1481 _dbus_assert (d != NULL);
1487 * Check whether completing the passed-in connection would
1488 * exceed limits, and if so set error and return #FALSE
1491 bus_connections_check_limits (BusConnections *connections,
1492 DBusConnection *requesting_completion,
1497 if (connections->n_completed >=
1498 bus_context_get_max_completed_connections (connections->context))
1500 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1501 "The maximum number of active connections has been reached");
1505 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1507 if (get_connections_for_uid (connections, uid) >=
1508 bus_context_get_max_connections_per_user (connections->context))
1510 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1511 "The maximum number of active connections for UID %lu has been reached",
1521 bus_pending_reply_free (BusPendingReply *pending)
1523 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1525 pending->will_send_reply,
1526 pending->will_get_reply,
1527 pending->reply_serial);
1529 dbus_free (pending);
1533 bus_pending_reply_send_no_reply (BusConnections *connections,
1534 BusTransaction *transaction,
1535 BusPendingReply *pending)
1537 DBusMessage *message;
1538 DBusMessageIter iter;
1544 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1545 if (message == NULL)
1548 dbus_message_set_no_reply (message, TRUE);
1550 if (!dbus_message_set_reply_serial (message,
1551 pending->reply_serial))
1554 if (!dbus_message_set_error_name (message,
1555 DBUS_ERROR_NO_REPLY))
1558 errmsg = "Message did not receive a reply (timeout by message bus)";
1559 dbus_message_iter_init_append (message, &iter);
1560 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1563 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1570 dbus_message_unref (message);
1575 bus_pending_reply_expired (BusExpireList *list,
1579 BusPendingReply *pending = link->data;
1580 BusConnections *connections = data;
1581 BusTransaction *transaction;
1583 /* No reply is forthcoming. So nuke it if we can. If not,
1584 * leave it in the list to try expiring again later when we
1588 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1590 pending->will_send_reply,
1591 pending->will_get_reply,
1592 pending->reply_serial);
1594 transaction = bus_transaction_new (connections->context);
1595 if (transaction == NULL)
1598 if (!bus_pending_reply_send_no_reply (connections,
1602 bus_transaction_cancel_and_free (transaction);
1606 bus_expire_list_remove_link (connections->pending_replies, link);
1608 bus_pending_reply_free (pending);
1609 bus_transaction_execute_and_free (transaction);
1615 bus_connection_drop_pending_replies (BusConnections *connections,
1616 DBusConnection *connection)
1618 /* The DBusConnection is almost 100% finalized here, so you can't
1619 * do anything with it except check for pointer equality
1623 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1626 link = bus_expire_list_get_first_link (connections->pending_replies);
1627 while (link != NULL)
1630 BusPendingReply *pending;
1632 next = bus_expire_list_get_next_link (connections->pending_replies,
1634 pending = link->data;
1636 if (pending->will_get_reply == connection)
1638 /* We don't need to track this pending reply anymore */
1640 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1642 pending->will_send_reply,
1643 pending->will_get_reply,
1644 pending->reply_serial);
1646 bus_expire_list_remove_link (connections->pending_replies,
1648 bus_pending_reply_free (pending);
1650 else if (pending->will_send_reply == connection)
1652 /* The reply isn't going to be sent, so set things
1653 * up so it will be expired right away
1655 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1657 pending->will_send_reply,
1658 pending->will_get_reply,
1659 pending->reply_serial);
1661 pending->will_send_reply = NULL;
1662 pending->expire_item.added_tv_sec = 0;
1663 pending->expire_item.added_tv_usec = 0;
1665 bus_expire_list_recheck_immediately (connections->pending_replies);
1675 BusPendingReply *pending;
1676 BusConnections *connections;
1677 } CancelPendingReplyData;
1680 cancel_pending_reply (void *data)
1682 CancelPendingReplyData *d = data;
1684 _dbus_verbose ("d = %p\n", d);
1686 if (!bus_expire_list_remove (d->connections->pending_replies,
1687 &d->pending->expire_item))
1688 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1690 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1694 cancel_pending_reply_data_free (void *data)
1696 CancelPendingReplyData *d = data;
1698 _dbus_verbose ("d = %p\n", d);
1700 /* d->pending should be either freed or still
1701 * in the list of pending replies (owned by someone
1709 * Record that a reply is allowed; return TRUE on success.
1712 bus_connections_expect_reply (BusConnections *connections,
1713 BusTransaction *transaction,
1714 DBusConnection *will_get_reply,
1715 DBusConnection *will_send_reply,
1716 DBusMessage *reply_to_this,
1719 BusPendingReply *pending;
1720 dbus_uint32_t reply_serial;
1722 CancelPendingReplyData *cprd;
1725 _dbus_assert (will_get_reply != NULL);
1726 _dbus_assert (will_send_reply != NULL);
1727 _dbus_assert (reply_to_this != NULL);
1729 if (dbus_message_get_no_reply (reply_to_this))
1730 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1732 reply_serial = dbus_message_get_serial (reply_to_this);
1734 link = bus_expire_list_get_first_link (connections->pending_replies);
1736 while (link != NULL)
1738 pending = link->data;
1740 if (pending->reply_serial == reply_serial &&
1741 pending->will_get_reply == will_get_reply &&
1742 pending->will_send_reply == will_send_reply)
1744 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1745 "Message has the same reply serial as a currently-outstanding existing method call");
1749 link = bus_expire_list_get_next_link (connections->pending_replies,
1751 if (pending->will_get_reply == will_get_reply)
1756 bus_context_get_max_replies_per_connection (connections->context))
1758 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1759 "The maximum number of pending replies per connection has been reached");
1763 pending = dbus_new0 (BusPendingReply, 1);
1764 if (pending == NULL)
1766 BUS_SET_OOM (error);
1770 #ifdef DBUS_ENABLE_VERBOSE_MODE
1771 /* so we can see a not-yet-added pending reply */
1772 pending->expire_item.added_tv_sec = 1;
1773 pending->expire_item.added_tv_usec = 1;
1776 pending->will_get_reply = will_get_reply;
1777 pending->will_send_reply = will_send_reply;
1778 pending->reply_serial = reply_serial;
1780 cprd = dbus_new0 (CancelPendingReplyData, 1);
1783 BUS_SET_OOM (error);
1784 bus_pending_reply_free (pending);
1788 if (!bus_expire_list_add (connections->pending_replies,
1789 &pending->expire_item))
1791 BUS_SET_OOM (error);
1793 bus_pending_reply_free (pending);
1797 if (!bus_transaction_add_cancel_hook (transaction,
1798 cancel_pending_reply,
1800 cancel_pending_reply_data_free))
1802 BUS_SET_OOM (error);
1803 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1805 bus_pending_reply_free (pending);
1809 cprd->pending = pending;
1810 cprd->connections = connections;
1812 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1813 &pending->expire_item.added_tv_usec);
1815 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1817 pending->will_send_reply,
1818 pending->will_get_reply,
1819 pending->reply_serial);
1827 BusConnections *connections;
1828 } CheckPendingReplyData;
1831 cancel_check_pending_reply (void *data)
1833 CheckPendingReplyData *d = data;
1835 _dbus_verbose ("d = %p\n",d);
1837 bus_expire_list_add_link (d->connections->pending_replies,
1843 check_pending_reply_data_free (void *data)
1845 CheckPendingReplyData *d = data;
1847 _dbus_verbose ("d = %p\n",d);
1849 if (d->link != NULL)
1851 BusPendingReply *pending = d->link->data;
1853 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1854 &pending->expire_item));
1856 bus_pending_reply_free (pending);
1857 _dbus_list_free_link (d->link);
1864 * Check whether a reply is allowed, remove BusPendingReply
1865 * if so, return TRUE if so.
1868 bus_connections_check_reply (BusConnections *connections,
1869 BusTransaction *transaction,
1870 DBusConnection *sending_reply,
1871 DBusConnection *receiving_reply,
1875 CheckPendingReplyData *cprd;
1877 dbus_uint32_t reply_serial;
1879 _dbus_assert (sending_reply != NULL);
1880 _dbus_assert (receiving_reply != NULL);
1882 reply_serial = dbus_message_get_reply_serial (reply);
1884 link = bus_expire_list_get_first_link (connections->pending_replies);
1885 while (link != NULL)
1887 BusPendingReply *pending = link->data;
1889 if (pending->reply_serial == reply_serial &&
1890 pending->will_get_reply == receiving_reply &&
1891 pending->will_send_reply == sending_reply)
1893 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1897 link = bus_expire_list_get_next_link (connections->pending_replies,
1903 _dbus_verbose ("No pending reply expected\n");
1908 cprd = dbus_new0 (CheckPendingReplyData, 1);
1911 BUS_SET_OOM (error);
1915 if (!bus_transaction_add_cancel_hook (transaction,
1916 cancel_check_pending_reply,
1918 check_pending_reply_data_free))
1920 BUS_SET_OOM (error);
1926 cprd->connections = connections;
1928 bus_expire_list_unlink (connections->pending_replies,
1931 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1939 * Note that this is fairly fragile; in particular, don't try to use
1940 * one transaction across any main loop iterations.
1945 BusTransaction *transaction;
1946 DBusMessage *message;
1947 DBusPreallocatedSend *preallocated;
1952 BusTransactionCancelFunction cancel_function;
1953 DBusFreeFunction free_data_function;
1957 struct BusTransaction
1959 DBusList *connections;
1960 BusContext *context;
1961 DBusList *cancel_hooks;
1965 message_to_send_free (DBusConnection *connection,
1966 MessageToSend *to_send)
1968 if (to_send->message)
1969 dbus_message_unref (to_send->message);
1971 if (to_send->preallocated)
1972 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1974 dbus_free (to_send);
1978 cancel_hook_cancel (void *element,
1981 CancelHook *ch = element;
1983 _dbus_verbose ("Running transaction cancel hook\n");
1985 if (ch->cancel_function)
1986 (* ch->cancel_function) (ch->data);
1990 cancel_hook_free (void *element,
1993 CancelHook *ch = element;
1995 if (ch->free_data_function)
1996 (* ch->free_data_function) (ch->data);
2002 free_cancel_hooks (BusTransaction *transaction)
2004 _dbus_list_foreach (&transaction->cancel_hooks,
2005 cancel_hook_free, NULL);
2007 _dbus_list_clear (&transaction->cancel_hooks);
2011 bus_transaction_new (BusContext *context)
2013 BusTransaction *transaction;
2015 transaction = dbus_new0 (BusTransaction, 1);
2016 if (transaction == NULL)
2019 transaction->context = context;
2025 bus_transaction_get_context (BusTransaction *transaction)
2027 return transaction->context;
2031 bus_transaction_get_connections (BusTransaction *transaction)
2033 return bus_context_get_connections (transaction->context);
2037 bus_transaction_send_from_driver (BusTransaction *transaction,
2038 DBusConnection *connection,
2039 DBusMessage *message)
2041 /* We have to set the sender to the driver, and have
2042 * to check security policy since it was not done in
2045 _dbus_verbose ("Sending %s %s %s from driver\n",
2046 dbus_message_get_interface (message) ?
2047 dbus_message_get_interface (message) : "(no interface)",
2048 dbus_message_get_member (message) ?
2049 dbus_message_get_member (message) : "(no member)",
2050 dbus_message_get_error_name (message) ?
2051 dbus_message_get_error_name (message) : "(no error name)");
2053 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2056 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) /* we can't set destination on the basis of connection when on kdbus*/
2057 if (bus_connection_is_active (connection))
2059 if (!dbus_message_set_destination (message,
2060 bus_connection_get_name (connection)))
2064 /* bus driver never wants a reply */
2065 dbus_message_set_no_reply (message, TRUE);
2067 /* If security policy doesn't allow the message, we silently
2068 * eat it; the driver doesn't care about getting a reply.
2070 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2072 NULL, connection, connection, message, NULL))
2075 return bus_transaction_send (transaction, connection, message);
2079 bus_transaction_send (BusTransaction *transaction,
2080 DBusConnection *connection,
2081 DBusMessage *message)
2083 MessageToSend *to_send;
2084 BusConnectionData *d;
2087 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2088 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2089 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2091 dbus_message_get_interface (message) ?
2092 dbus_message_get_interface (message) : "(unset)",
2093 dbus_message_get_member (message) ?
2094 dbus_message_get_member (message) : "(unset)",
2095 dbus_message_get_error_name (message) ?
2096 dbus_message_get_error_name (message) : "(unset)",
2097 dbus_connection_get_is_connected (connection) ?
2098 "" : " (disconnected)");
2100 _dbus_assert (dbus_message_get_sender (message) != NULL);
2102 if (!dbus_connection_get_is_connected (connection))
2103 return TRUE; /* silently ignore disconnected connections */
2105 d = BUS_CONNECTION_DATA (connection);
2106 _dbus_assert (d != NULL);
2108 to_send = dbus_new (MessageToSend, 1);
2109 if (to_send == NULL)
2114 to_send->preallocated = dbus_connection_preallocate_send (connection);
2115 if (to_send->preallocated == NULL)
2117 dbus_free (to_send);
2121 dbus_message_ref (message);
2122 to_send->message = message;
2123 to_send->transaction = transaction;
2125 _dbus_verbose ("about to prepend message\n");
2127 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2129 message_to_send_free (connection, to_send);
2133 _dbus_verbose ("prepended message\n");
2135 /* See if we already had this connection in the list
2136 * for this transaction. If we have a pending message,
2137 * then we should already be in transaction->connections
2139 link = _dbus_list_get_first_link (&d->transaction_messages);
2140 _dbus_assert (link->data == to_send);
2141 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2142 while (link != NULL)
2144 MessageToSend *m = link->data;
2145 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2147 if (m->transaction == transaction)
2155 if (!_dbus_list_prepend (&transaction->connections, connection))
2157 _dbus_list_remove (&d->transaction_messages, to_send);
2158 message_to_send_free (connection, to_send);
2167 connection_cancel_transaction (DBusConnection *connection,
2168 BusTransaction *transaction)
2171 BusConnectionData *d;
2173 d = BUS_CONNECTION_DATA (connection);
2174 _dbus_assert (d != NULL);
2176 link = _dbus_list_get_first_link (&d->transaction_messages);
2177 while (link != NULL)
2179 MessageToSend *m = link->data;
2180 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2182 if (m->transaction == transaction)
2184 _dbus_list_remove_link (&d->transaction_messages,
2187 message_to_send_free (connection, m);
2195 bus_transaction_cancel_and_free (BusTransaction *transaction)
2197 DBusConnection *connection;
2199 _dbus_verbose ("TRANSACTION: cancelled\n");
2201 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2202 connection_cancel_transaction (connection, transaction);
2204 _dbus_assert (transaction->connections == NULL);
2206 _dbus_list_foreach (&transaction->cancel_hooks,
2207 cancel_hook_cancel, NULL);
2209 free_cancel_hooks (transaction);
2211 dbus_free (transaction);
2215 connection_execute_transaction (DBusConnection *connection,
2216 BusTransaction *transaction)
2219 BusConnectionData *d;
2221 d = BUS_CONNECTION_DATA (connection);
2222 _dbus_assert (d != NULL);
2224 /* Send the queue in order (FIFO) */
2225 link = _dbus_list_get_last_link (&d->transaction_messages);
2226 while (link != NULL)
2228 MessageToSend *m = link->data;
2229 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2231 if (m->transaction == transaction)
2233 _dbus_list_remove_link (&d->transaction_messages,
2236 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2238 dbus_connection_send_preallocated (connection,
2243 m->preallocated = NULL; /* so we don't double-free it */
2245 message_to_send_free (connection, m);
2253 bus_transaction_execute_and_free (BusTransaction *transaction)
2255 /* For each connection in transaction->connections
2258 DBusConnection *connection;
2260 _dbus_verbose ("TRANSACTION: executing\n");
2262 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2263 connection_execute_transaction (connection, transaction);
2265 _dbus_assert (transaction->connections == NULL);
2267 free_cancel_hooks (transaction);
2269 dbus_free (transaction);
2273 bus_transaction_free (BusTransaction *transaction)
2275 free_cancel_hooks (transaction);
2276 dbus_free (transaction);
2280 bus_connection_remove_transactions (DBusConnection *connection)
2282 MessageToSend *to_send;
2283 BusConnectionData *d;
2285 d = BUS_CONNECTION_DATA (connection);
2286 _dbus_assert (d != NULL);
2288 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2290 /* only has an effect for the first MessageToSend listing this transaction */
2291 _dbus_list_remove (&to_send->transaction->connections,
2294 _dbus_list_remove (&d->transaction_messages, to_send);
2295 message_to_send_free (connection, to_send);
2300 * Converts the DBusError to a message reply
2303 bus_transaction_send_error_reply (BusTransaction *transaction,
2304 DBusConnection *connection,
2305 const DBusError *error,
2306 DBusMessage *in_reply_to)
2310 _dbus_assert (error != NULL);
2311 _DBUS_ASSERT_ERROR_IS_SET (error);
2313 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2314 error->name, error->message);
2316 reply = dbus_message_new_error (in_reply_to,
2322 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2324 dbus_message_unref (reply);
2328 dbus_message_unref (reply);
2334 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2335 BusTransactionCancelFunction cancel_function,
2337 DBusFreeFunction free_data_function)
2341 ch = dbus_new (CancelHook, 1);
2345 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2346 cancel_function, data);
2348 ch->cancel_function = cancel_function;
2350 ch->free_data_function = free_data_function;
2352 /* It's important that the hooks get run in reverse order that they
2355 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2364 #ifdef DBUS_ENABLE_STATS
2366 bus_connections_get_n_active (BusConnections *connections)
2368 return connections->n_completed;
2372 bus_connections_get_n_incomplete (BusConnections *connections)
2374 return connections->n_incomplete;
2378 bus_connections_get_total_match_rules (BusConnections *connections)
2380 return connections->total_match_rules;
2384 bus_connections_get_peak_match_rules (BusConnections *connections)
2386 return connections->peak_match_rules;
2390 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2392 return connections->peak_match_rules_per_conn;
2396 bus_connections_get_total_bus_names (BusConnections *connections)
2398 return connections->total_bus_names;
2402 bus_connections_get_peak_bus_names (BusConnections *connections)
2404 return connections->peak_bus_names;
2408 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2410 return connections->peak_bus_names_per_conn;
2414 bus_connection_get_peak_match_rules (DBusConnection *connection)
2416 BusConnectionData *d;
2418 d = BUS_CONNECTION_DATA (connection);
2419 return d->peak_match_rules;
2423 bus_connection_get_peak_bus_names (DBusConnection *connection)
2425 BusConnectionData *d;
2427 d = BUS_CONNECTION_DATA (connection);
2428 return d->peak_bus_names;
2430 #endif /* DBUS_ENABLE_STATS */