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>
37 /* Trim executed commands to this length; we want to keep logs readable */
38 #define MAX_LOG_COMMAND_LEN 50
40 static void bus_connection_remove_transactions (DBusConnection *connection);
44 BusExpireItem expire_item;
46 DBusConnection *will_get_reply;
47 DBusConnection *will_send_reply;
49 dbus_uint32_t reply_serial;
56 DBusList *completed; /**< List of all completed connections */
57 int n_completed; /**< Length of completed list */
58 DBusList *incomplete; /**< List of all not-yet-active connections */
59 int n_incomplete; /**< Length of incomplete list */
61 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
62 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
63 int stamp; /**< Incrementing number */
64 BusExpireList *pending_replies; /**< List of pending replies */
66 #ifdef DBUS_ENABLE_STATS
67 int total_match_rules;
69 int peak_match_rules_per_conn;
73 int peak_bus_names_per_conn;
77 static dbus_int32_t connection_data_slot = -1;
81 BusConnections *connections;
82 DBusList *link_in_connection_list;
83 DBusConnection *connection;
84 DBusList *services_owned;
86 DBusList *match_rules;
89 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
90 DBusMessage *oom_message;
91 DBusPreallocatedSend *oom_preallocated;
92 BusClientPolicy *policy;
94 char *cached_loginfo_string;
95 BusSELinuxID *selinux_id;
97 long connection_tv_sec; /**< Time when we connected (seconds component) */
98 long connection_tv_usec; /**< Time when we connected (microsec component) */
99 int stamp; /**< connections->stamp last time we were traversed */
101 #ifdef DBUS_ENABLE_STATS
102 int peak_match_rules;
107 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
111 static void bus_connection_drop_pending_replies (BusConnections *connections,
112 DBusConnection *connection);
114 static dbus_bool_t expire_incomplete_timeout (void *data);
116 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
119 connection_get_loop (DBusConnection *connection)
121 BusConnectionData *d;
123 d = BUS_CONNECTION_DATA (connection);
125 return bus_context_get_loop (d->connections->context);
130 get_connections_for_uid (BusConnections *connections,
136 /* val is NULL is 0 when it isn't in the hash yet */
138 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
141 current_count = _DBUS_POINTER_TO_INT (val);
143 return current_count;
147 adjust_connections_for_uid (BusConnections *connections,
153 current_count = get_connections_for_uid (connections, uid);
155 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
156 ": was %d adjustment %d making %d\n",
157 uid, current_count, adjustment, current_count + adjustment);
159 _dbus_assert (current_count >= 0);
161 current_count += adjustment;
163 _dbus_assert (current_count >= 0);
165 if (current_count == 0)
167 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
174 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
175 uid, _DBUS_INT_TO_POINTER (current_count));
177 /* only positive adjustment can fail as otherwise
178 * a hash entry should already exist
180 _dbus_assert (adjustment > 0 ||
181 (adjustment <= 0 && retval));
188 bus_connection_disconnected (DBusConnection *connection)
190 BusConnectionData *d;
192 BusMatchmaker *matchmaker;
193 dbus_bool_t is_phantom = FALSE;
195 d = BUS_CONNECTION_DATA (connection);
196 _dbus_assert (d != NULL);
198 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
199 d->name ? d->name : "(inactive)");
201 if(bus_context_is_kdbus(d->connections->context) && (strcmp(bus_connection_get_name(connection), ":1.1")))
204 /* Delete our match rules */
205 if (d->n_match_rules > 0)
207 matchmaker = bus_context_get_matchmaker (d->connections->context);
208 bus_matchmaker_disconnected (matchmaker, connection);
211 /* Drop any service ownership. Unfortunately, this requires
212 * memory allocation and there doesn't seem to be a good way to
213 * handle it other than sleeping; we can't "fail" the operation of
214 * disconnecting a client, and preallocating a broadcast "service is
215 * now gone" message for every client-service pair seems kind of
218 while ((service = _dbus_list_get_last (&d->services_owned)))
220 BusTransaction *transaction;
225 dbus_error_init (&error);
227 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
228 _dbus_wait_for_memory ();
230 if (!bus_service_remove_owner (service, connection,
231 transaction, &error))
233 _DBUS_ASSERT_ERROR_IS_SET (&error);
235 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
237 dbus_error_free (&error);
238 bus_transaction_cancel_and_free (transaction);
239 _dbus_wait_for_memory ();
244 _dbus_verbose ("Failed to remove service owner: %s %s\n",
245 error.name, error.message);
246 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
250 bus_transaction_execute_and_free (transaction);
253 bus_dispatch_remove_connection (connection);
255 /* no more watching */
256 if (!dbus_connection_set_watch_functions (connection,
260 _dbus_assert_not_reached ("setting watch functions to NULL failed");
262 if (!dbus_connection_set_timeout_functions (connection,
266 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
268 dbus_connection_set_unix_user_function (connection,
270 dbus_connection_set_windows_user_function (connection,
273 dbus_connection_set_dispatch_status_function (connection,
276 bus_connection_remove_transactions (connection);
278 if (d->link_in_connection_list != NULL)
284 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
285 d->link_in_connection_list = NULL;
286 d->connections->n_completed -= 1;
288 if (dbus_connection_get_unix_user (connection, &uid))
290 if (!adjust_connections_for_uid (d->connections,
292 _dbus_assert_not_reached ("adjusting downward should never fail");
297 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
298 d->link_in_connection_list = NULL;
299 d->connections->n_incomplete -= 1;
302 _dbus_assert (d->connections->n_incomplete >= 0);
303 _dbus_assert (d->connections->n_completed >= 0);
306 bus_connection_drop_pending_replies (d->connections, connection);
308 /* frees "d" as side effect */
309 dbus_connection_set_data (connection,
310 connection_data_slot,
313 dbus_connection_unref_phantom(connection);
315 dbus_connection_unref (connection);
319 add_connection_watch (DBusWatch *watch,
322 DBusConnection *connection = data;
324 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
328 remove_connection_watch (DBusWatch *watch,
331 DBusConnection *connection = data;
333 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
337 toggle_connection_watch (DBusWatch *watch,
340 DBusConnection *connection = data;
342 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
346 add_connection_timeout (DBusTimeout *timeout,
349 DBusConnection *connection = data;
351 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
355 remove_connection_timeout (DBusTimeout *timeout,
358 DBusConnection *connection = data;
360 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
364 dispatch_status_function (DBusConnection *connection,
365 DBusDispatchStatus new_status,
368 DBusLoop *loop = data;
370 if (new_status != DBUS_DISPATCH_COMPLETE)
372 while (!_dbus_loop_queue_dispatch (loop, connection))
373 _dbus_wait_for_memory ();
378 allow_unix_user_function (DBusConnection *connection,
382 BusConnectionData *d;
384 d = BUS_CONNECTION_DATA (connection);
386 _dbus_assert (d != NULL);
388 return bus_context_allow_unix_user (d->connections->context, uid);
392 free_connection_data (void *data)
394 BusConnectionData *d = data;
396 /* services_owned should be NULL since we should be disconnected */
397 _dbus_assert (d->services_owned == NULL);
398 _dbus_assert (d->n_services_owned == 0);
400 _dbus_assert (d->transaction_messages == NULL);
402 if (d->oom_preallocated)
403 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
406 dbus_message_unref (d->oom_message);
409 bus_client_policy_unref (d->policy);
412 bus_selinux_id_unref (d->selinux_id);
414 dbus_free (d->cached_loginfo_string);
422 bus_connections_new (BusContext *context)
424 BusConnections *connections;
426 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
429 connections = dbus_new0 (BusConnections, 1);
430 if (connections == NULL)
433 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
435 if (connections->completed_by_user == NULL)
438 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
439 expire_incomplete_timeout,
441 if (connections->expire_timeout == NULL)
444 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
446 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
447 bus_context_get_reply_timeout (context),
448 bus_pending_reply_expired,
450 if (connections->pending_replies == NULL)
453 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
454 connections->expire_timeout))
457 connections->refcount = 1;
458 connections->context = context;
463 bus_expire_list_free (connections->pending_replies);
465 _dbus_timeout_unref (connections->expire_timeout);
467 _dbus_hash_table_unref (connections->completed_by_user);
469 dbus_free (connections);
471 dbus_connection_free_data_slot (&connection_data_slot);
477 bus_connections_ref (BusConnections *connections)
479 _dbus_assert (connections->refcount > 0);
480 connections->refcount += 1;
486 bus_connections_unref (BusConnections *connections)
488 _dbus_assert (connections->refcount > 0);
489 connections->refcount -= 1;
490 if (connections->refcount == 0)
492 /* drop all incomplete */
493 while (connections->incomplete != NULL)
495 DBusConnection *connection;
497 connection = connections->incomplete->data;
499 dbus_connection_ref (connection);
500 dbus_connection_close (connection);
501 bus_connection_disconnected (connection);
502 dbus_connection_unref (connection);
505 _dbus_assert (connections->n_incomplete == 0);
507 /* drop all real connections */
508 while (connections->completed != NULL)
510 DBusConnection *connection;
512 connection = connections->completed->data;
514 dbus_connection_ref (connection);
515 dbus_connection_close (connection);
516 bus_connection_disconnected (connection);
517 dbus_connection_unref (connection);
520 _dbus_assert (connections->n_completed == 0);
522 bus_expire_list_free (connections->pending_replies);
524 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
525 connections->expire_timeout);
527 _dbus_timeout_unref (connections->expire_timeout);
529 _dbus_hash_table_unref (connections->completed_by_user);
531 dbus_free (connections);
533 dbus_connection_free_data_slot (&connection_data_slot);
537 /* Used for logging */
539 cache_peer_loginfo_string (BusConnectionData *d,
540 DBusConnection *connection)
542 DBusString loginfo_buf;
546 dbus_bool_t prev_added;
548 if (!_dbus_string_init (&loginfo_buf))
552 if (dbus_connection_get_unix_user (connection, &uid))
554 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
560 if (dbus_connection_get_unix_process_id (connection, &pid))
564 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
567 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
569 /* Ignore errors here; we may not have permissions to read the
571 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
572 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
576 if (dbus_connection_get_windows_user (connection, &windows_sid))
578 dbus_bool_t did_append;
579 did_append = _dbus_string_append_printf (&loginfo_buf,
580 "sid=\"%s\" ", windows_sid);
581 dbus_free (windows_sid);
586 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
589 _dbus_string_free (&loginfo_buf);
593 _dbus_string_free (&loginfo_buf);
598 bus_connections_setup_connection (BusConnections *connections,
599 DBusConnection *connection)
602 BusConnectionData *d;
607 d = dbus_new0 (BusConnectionData, 1);
612 d->connections = connections;
613 d->connection = connection;
615 _dbus_get_monotonic_time (&d->connection_tv_sec,
616 &d->connection_tv_usec);
618 _dbus_assert (connection_data_slot >= 0);
620 if (!dbus_connection_set_data (connection,
621 connection_data_slot,
622 d, free_connection_data))
628 dbus_connection_set_route_peer_messages (connection, TRUE);
632 dbus_error_init (&error);
633 d->selinux_id = bus_selinux_init_connection_id (connection,
635 if (dbus_error_is_set (&error))
637 /* This is a bit bogus because we pretend all errors
638 * are OOM; this is done because we know that in bus.c
639 * an OOM error disconnects the connection, which is
640 * the same thing we want on any other error.
642 dbus_error_free (&error);
646 if (!dbus_connection_set_watch_functions (connection,
647 add_connection_watch,
648 remove_connection_watch,
649 toggle_connection_watch,
654 if (!dbus_connection_set_timeout_functions (connection,
655 add_connection_timeout,
656 remove_connection_timeout,
661 /* For now we don't need to set a Windows user function because
662 * there are no policies in the config file controlling what
663 * Windows users can connect. The default 'same user that owns the
664 * bus can connect' behavior of DBusConnection is fine on Windows.
666 dbus_connection_set_unix_user_function (connection,
667 allow_unix_user_function,
670 dbus_connection_set_dispatch_status_function (connection,
671 dispatch_status_function,
672 bus_context_get_loop (connections->context),
675 d->link_in_connection_list = _dbus_list_alloc_link (connection);
676 if (d->link_in_connection_list == NULL)
679 /* Setup the connection with the dispatcher */
680 if (!bus_dispatch_add_connection (connection))
683 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
685 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
687 bus_dispatch_remove_connection (connection);
692 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
693 connections->n_incomplete += 1;
695 dbus_connection_ref (connection);
697 /* Note that we might disconnect ourselves here, but it only takes
698 * effect on return to the main loop. We call this to free up
699 * expired connections if possible, and to queue the timeout for our
702 bus_connections_expire_incomplete (connections);
704 /* And we might also disconnect ourselves here, but again it
705 * only takes effect on return to main loop.
707 if (connections->n_incomplete >
708 bus_context_get_max_incomplete_connections (connections->context))
710 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
712 _dbus_assert (connections->incomplete != NULL);
713 /* Disconnect the oldest unauthenticated connection. FIXME
714 * would it be more secure to drop a *random* connection? This
715 * algorithm seems to mean that if someone can create new
716 * connections quickly enough, they can keep anyone else from
717 * completing authentication. But random may or may not really
718 * help with that, a more elaborate solution might be required.
720 dbus_connection_close (connections->incomplete->data);
729 bus_selinux_id_unref (d->selinux_id);
730 d->selinux_id = NULL;
732 if (!dbus_connection_set_watch_functions (connection,
736 _dbus_assert_not_reached ("setting watch functions to NULL failed");
738 if (!dbus_connection_set_timeout_functions (connection,
742 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
744 dbus_connection_set_unix_user_function (connection,
747 dbus_connection_set_windows_user_function (connection,
750 dbus_connection_set_dispatch_status_function (connection,
753 if (d->link_in_connection_list != NULL)
755 _dbus_assert (d->link_in_connection_list->next == NULL);
756 _dbus_assert (d->link_in_connection_list->prev == NULL);
757 _dbus_list_free_link (d->link_in_connection_list);
758 d->link_in_connection_list = NULL;
761 if (!dbus_connection_set_data (connection,
762 connection_data_slot,
764 _dbus_assert_not_reached ("failed to set connection data to null");
766 /* "d" has now been freed */
773 bus_connections_expire_incomplete (BusConnections *connections)
779 if (connections->incomplete != NULL)
781 long tv_sec, tv_usec;
785 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
786 auth_timeout = bus_context_get_auth_timeout (connections->context);
788 link = _dbus_list_get_first_link (&connections->incomplete);
791 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
792 DBusConnection *connection;
793 BusConnectionData *d;
796 connection = link->data;
798 d = BUS_CONNECTION_DATA (connection);
800 _dbus_assert (d != NULL);
802 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
803 d->connection_tv_usec,
806 if (elapsed >= (double) auth_timeout)
808 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
809 dbus_connection_close (connection);
813 /* We can end the loop, since the connections are in oldest-first order */
814 next_interval = ((double)auth_timeout) - elapsed;
815 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
816 connection, next_interval);
825 bus_expire_timeout_set_interval (connections->expire_timeout,
830 expire_incomplete_timeout (void *data)
832 BusConnections *connections = data;
834 _dbus_verbose ("Running\n");
836 /* note that this may remove the timeout */
837 bus_connections_expire_incomplete (connections);
843 bus_connection_get_unix_groups (DBusConnection *connection,
844 unsigned long **groups,
853 if (dbus_connection_get_unix_user (connection, &uid))
855 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
857 _dbus_verbose ("Did not get any groups for UID %lu\n",
863 _dbus_verbose ("Got %d groups for UID %lu\n",
869 return TRUE; /* successfully got 0 groups */
873 bus_connection_is_in_unix_group (DBusConnection *connection,
877 unsigned long *group_ids;
880 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
885 while (i < n_group_ids)
887 if (group_ids[i] == gid)
889 dbus_free (group_ids);
895 dbus_free (group_ids);
900 bus_connection_get_loginfo (DBusConnection *connection)
902 BusConnectionData *d;
904 d = BUS_CONNECTION_DATA (connection);
906 if (!bus_connection_is_active (connection))
908 return d->cached_loginfo_string;
912 bus_connection_get_policy (DBusConnection *connection)
914 BusConnectionData *d;
916 d = BUS_CONNECTION_DATA (connection);
918 _dbus_assert (d != NULL);
919 _dbus_assert (d->policy != NULL);
925 foreach_active (BusConnections *connections,
926 BusConnectionForeachFunction function,
931 link = _dbus_list_get_first_link (&connections->completed);
934 DBusConnection *connection = link->data;
935 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
937 if (!(* function) (connection, data))
947 foreach_inactive (BusConnections *connections,
948 BusConnectionForeachFunction function,
953 link = _dbus_list_get_first_link (&connections->incomplete);
956 DBusConnection *connection = link->data;
957 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
959 if (!(* function) (connection, data))
969 * Calls function on each active connection; if the function returns
970 * #FALSE, stops iterating. Active connections are authenticated
971 * and have sent a Hello message.
973 * @param connections the connections object
974 * @param function the function
975 * @param data data to pass to it as a second arg
978 bus_connections_foreach_active (BusConnections *connections,
979 BusConnectionForeachFunction function,
982 foreach_active (connections, function, data);
986 * Calls function on each connection; if the function returns
987 * #FALSE, stops iterating.
989 * @param connections the connections object
990 * @param function the function
991 * @param data data to pass to it as a second arg
994 bus_connections_foreach (BusConnections *connections,
995 BusConnectionForeachFunction function,
998 if (!foreach_active (connections, function, data))
1001 foreach_inactive (connections, function, data);
1005 bus_connections_find_conn_by_name(BusConnections *connections, const char* name)
1009 link = _dbus_list_get_first_link (&connections->completed);
1010 while (link != NULL)
1012 DBusConnection *connection = link->data;
1013 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1015 if (!strcmp(bus_connection_get_name(connection), name))
1025 bus_connections_get_context (BusConnections *connections)
1027 return connections->context;
1031 * This is used to avoid covering the same connection twice when
1032 * traversing connections. Note that it assumes we will
1033 * bus_connection_mark_stamp() each connection at least once per
1034 * INT_MAX increments of the global stamp, or wraparound would break
1038 bus_connections_increment_stamp (BusConnections *connections)
1040 connections->stamp += 1;
1043 /* Mark connection with current stamp, return TRUE if it
1044 * didn't already have that stamp
1047 bus_connection_mark_stamp (DBusConnection *connection)
1049 BusConnectionData *d;
1051 d = BUS_CONNECTION_DATA (connection);
1053 _dbus_assert (d != NULL);
1055 if (d->stamp == d->connections->stamp)
1059 d->stamp = d->connections->stamp;
1065 bus_connection_get_context (DBusConnection *connection)
1067 BusConnectionData *d;
1069 d = BUS_CONNECTION_DATA (connection);
1071 _dbus_assert (d != NULL);
1073 return d->connections->context;
1077 bus_connection_get_connections (DBusConnection *connection)
1079 BusConnectionData *d;
1081 d = BUS_CONNECTION_DATA (connection);
1083 _dbus_assert (d != NULL);
1085 return d->connections;
1089 bus_connection_get_registry (DBusConnection *connection)
1091 BusConnectionData *d;
1093 d = BUS_CONNECTION_DATA (connection);
1095 _dbus_assert (d != NULL);
1097 return bus_context_get_registry (d->connections->context);
1101 bus_connection_get_activation (DBusConnection *connection)
1103 BusConnectionData *d;
1105 d = BUS_CONNECTION_DATA (connection);
1107 _dbus_assert (d != NULL);
1109 return bus_context_get_activation (d->connections->context);
1113 bus_connection_get_matchmaker (DBusConnection *connection)
1115 BusConnectionData *d;
1117 d = BUS_CONNECTION_DATA (connection);
1119 _dbus_assert (d != NULL);
1121 return bus_context_get_matchmaker (d->connections->context);
1125 bus_connection_get_selinux_id (DBusConnection *connection)
1127 BusConnectionData *d;
1129 d = BUS_CONNECTION_DATA (connection);
1131 _dbus_assert (d != NULL);
1133 return d->selinux_id;
1137 * Checks whether the connection is registered with the message bus.
1139 * @param connection the connection
1140 * @returns #TRUE if we're an active message bus participant
1143 bus_connection_is_active (DBusConnection *connection)
1145 BusConnectionData *d;
1147 d = BUS_CONNECTION_DATA (connection);
1149 return d != NULL && d->name != NULL;
1153 bus_connection_preallocate_oom_error (DBusConnection *connection)
1155 DBusMessage *message;
1156 DBusPreallocatedSend *preallocated;
1157 BusConnectionData *d;
1159 d = BUS_CONNECTION_DATA (connection);
1161 _dbus_assert (d != NULL);
1163 if (d->oom_preallocated != NULL)
1166 preallocated = dbus_connection_preallocate_send (connection);
1167 if (preallocated == NULL)
1170 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1172 if (message == NULL)
1174 dbus_connection_free_preallocated_send (connection, preallocated);
1178 /* d->name may be NULL, but that is OK */
1179 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1180 !dbus_message_set_destination (message, d->name) ||
1181 !dbus_message_set_sender (message,
1184 dbus_connection_free_preallocated_send (connection, preallocated);
1185 dbus_message_unref (message);
1189 /* set reply serial to placeholder value just so space is already allocated
1192 if (!dbus_message_set_reply_serial (message, 14))
1194 dbus_connection_free_preallocated_send (connection, preallocated);
1195 dbus_message_unref (message);
1199 d->oom_message = message;
1200 d->oom_preallocated = preallocated;
1206 bus_connection_send_oom_error (DBusConnection *connection,
1207 DBusMessage *in_reply_to)
1209 BusConnectionData *d;
1211 d = BUS_CONNECTION_DATA (connection);
1213 _dbus_assert (d != NULL);
1214 _dbus_assert (d->oom_message != NULL);
1216 /* should always succeed since we set it to a placeholder earlier */
1217 if (!dbus_message_set_reply_serial (d->oom_message,
1218 dbus_message_get_serial (in_reply_to)))
1219 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1221 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1223 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1224 d->oom_message, NULL);
1226 dbus_message_unref (d->oom_message);
1227 d->oom_message = NULL;
1228 d->oom_preallocated = NULL;
1231 #ifdef DBUS_ENABLE_STATS
1233 update_peak (int *peak,
1242 bus_connection_add_match_rule_link (DBusConnection *connection,
1245 BusConnectionData *d;
1247 d = BUS_CONNECTION_DATA (connection);
1248 _dbus_assert (d != NULL);
1250 _dbus_list_append_link (&d->match_rules, link);
1252 d->n_match_rules += 1;
1254 #ifdef DBUS_ENABLE_STATS
1255 update_peak (&d->peak_match_rules, d->n_match_rules);
1256 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1258 d->connections->total_match_rules += 1;
1259 update_peak (&d->connections->peak_match_rules,
1260 d->connections->total_match_rules);
1265 bus_connection_add_match_rule (DBusConnection *connection,
1270 link = _dbus_list_alloc_link (rule);
1275 bus_connection_add_match_rule_link (connection, link);
1281 bus_connection_remove_match_rule (DBusConnection *connection,
1284 BusConnectionData *d;
1286 d = BUS_CONNECTION_DATA (connection);
1287 _dbus_assert (d != NULL);
1289 _dbus_list_remove_last (&d->match_rules, rule);
1291 d->n_match_rules -= 1;
1292 _dbus_assert (d->n_match_rules >= 0);
1294 #ifdef DBUS_ENABLE_STATS
1295 d->connections->total_match_rules -= 1;
1300 bus_connection_get_n_match_rules (DBusConnection *connection)
1302 BusConnectionData *d;
1304 d = BUS_CONNECTION_DATA (connection);
1305 _dbus_assert (d != NULL);
1307 return d->n_match_rules;
1311 bus_connection_add_owned_service_link (DBusConnection *connection,
1314 BusConnectionData *d;
1316 d = BUS_CONNECTION_DATA (connection);
1317 _dbus_assert (d != NULL);
1319 _dbus_list_append_link (&d->services_owned, link);
1321 d->n_services_owned += 1;
1323 #ifdef DBUS_ENABLE_STATS
1324 update_peak (&d->peak_bus_names, d->n_services_owned);
1325 update_peak (&d->connections->peak_bus_names_per_conn,
1326 d->n_services_owned);
1328 d->connections->total_bus_names += 1;
1329 update_peak (&d->connections->peak_bus_names,
1330 d->connections->total_bus_names);
1335 bus_connection_add_owned_service (DBusConnection *connection,
1336 BusService *service)
1340 link = _dbus_list_alloc_link (service);
1345 bus_connection_add_owned_service_link (connection, link);
1351 bus_connection_remove_owned_service (DBusConnection *connection,
1352 BusService *service)
1354 BusConnectionData *d;
1356 d = BUS_CONNECTION_DATA (connection);
1357 _dbus_assert (d != NULL);
1359 _dbus_list_remove_last (&d->services_owned, service);
1361 d->n_services_owned -= 1;
1362 _dbus_assert (d->n_services_owned >= 0);
1364 #ifdef DBUS_ENABLE_STATS
1365 d->connections->total_bus_names -= 1;
1370 bus_connection_get_n_services_owned (DBusConnection *connection)
1372 BusConnectionData *d;
1374 d = BUS_CONNECTION_DATA (connection);
1375 _dbus_assert (d != NULL);
1377 return d->n_services_owned;
1381 bus_connection_complete (DBusConnection *connection,
1382 const DBusString *name,
1385 BusConnectionData *d;
1388 d = BUS_CONNECTION_DATA (connection);
1389 _dbus_assert (d != NULL);
1390 _dbus_assert (d->name == NULL);
1391 _dbus_assert (d->policy == NULL);
1393 _dbus_assert (!bus_connection_is_active (connection));
1395 if (!_dbus_string_copy_data (name, &d->name))
1397 BUS_SET_OOM (error);
1401 _dbus_assert (d->name != NULL);
1403 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1405 d->policy = bus_context_create_client_policy (d->connections->context,
1409 /* we may have a NULL policy on OOM or error getting list of
1410 * groups for a user. In the latter case we don't handle it so
1411 * well currently, as it will just keep failing over and over.
1414 if (d->policy == NULL)
1416 _dbus_verbose ("Failed to create security policy for connection %p\n",
1418 _DBUS_ASSERT_ERROR_IS_SET (error);
1419 dbus_free (d->name);
1424 if (dbus_connection_get_unix_user (connection, &uid))
1426 if (!adjust_connections_for_uid (d->connections,
1431 /* Create and cache a string which holds information about the
1432 * peer process; used for logging purposes.
1434 if (!cache_peer_loginfo_string (d, connection))
1437 /* Now the connection is active, move it between lists */
1438 _dbus_list_unlink (&d->connections->incomplete,
1439 d->link_in_connection_list);
1440 d->connections->n_incomplete -= 1;
1441 _dbus_list_append_link (&d->connections->completed,
1442 d->link_in_connection_list);
1443 d->connections->n_completed += 1;
1445 _dbus_assert (d->connections->n_incomplete >= 0);
1446 _dbus_assert (d->connections->n_completed > 0);
1448 /* See if we can remove the timeout */
1449 bus_connections_expire_incomplete (d->connections);
1451 _dbus_assert (bus_connection_is_active (connection));
1455 BUS_SET_OOM (error);
1456 dbus_free (d->name);
1459 bus_client_policy_unref (d->policy);
1465 bus_connection_get_name (DBusConnection *connection)
1467 BusConnectionData *d;
1469 d = BUS_CONNECTION_DATA (connection);
1470 _dbus_assert (d != NULL);
1476 * Check whether completing the passed-in connection would
1477 * exceed limits, and if so set error and return #FALSE
1480 bus_connections_check_limits (BusConnections *connections,
1481 DBusConnection *requesting_completion,
1486 if (connections->n_completed >=
1487 bus_context_get_max_completed_connections (connections->context))
1489 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1490 "The maximum number of active connections has been reached");
1494 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1496 if (get_connections_for_uid (connections, uid) >=
1497 bus_context_get_max_connections_per_user (connections->context))
1499 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1500 "The maximum number of active connections for UID %lu has been reached",
1510 bus_pending_reply_free (BusPendingReply *pending)
1512 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1514 pending->will_send_reply,
1515 pending->will_get_reply,
1516 pending->reply_serial);
1518 dbus_free (pending);
1522 bus_pending_reply_send_no_reply (BusConnections *connections,
1523 BusTransaction *transaction,
1524 BusPendingReply *pending)
1526 DBusMessage *message;
1527 DBusMessageIter iter;
1533 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1534 if (message == NULL)
1537 dbus_message_set_no_reply (message, TRUE);
1539 if (!dbus_message_set_reply_serial (message,
1540 pending->reply_serial))
1543 if (!dbus_message_set_error_name (message,
1544 DBUS_ERROR_NO_REPLY))
1547 errmsg = "Message did not receive a reply (timeout by message bus)";
1548 dbus_message_iter_init_append (message, &iter);
1549 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1552 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1559 dbus_message_unref (message);
1564 bus_pending_reply_expired (BusExpireList *list,
1568 BusPendingReply *pending = link->data;
1569 BusConnections *connections = data;
1570 BusTransaction *transaction;
1572 /* No reply is forthcoming. So nuke it if we can. If not,
1573 * leave it in the list to try expiring again later when we
1577 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1579 pending->will_send_reply,
1580 pending->will_get_reply,
1581 pending->reply_serial);
1583 transaction = bus_transaction_new (connections->context);
1584 if (transaction == NULL)
1587 if (!bus_pending_reply_send_no_reply (connections,
1591 bus_transaction_cancel_and_free (transaction);
1595 bus_expire_list_remove_link (connections->pending_replies, link);
1597 bus_pending_reply_free (pending);
1598 bus_transaction_execute_and_free (transaction);
1604 bus_connection_drop_pending_replies (BusConnections *connections,
1605 DBusConnection *connection)
1607 /* The DBusConnection is almost 100% finalized here, so you can't
1608 * do anything with it except check for pointer equality
1612 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1615 link = bus_expire_list_get_first_link (connections->pending_replies);
1616 while (link != NULL)
1619 BusPendingReply *pending;
1621 next = bus_expire_list_get_next_link (connections->pending_replies,
1623 pending = link->data;
1625 if (pending->will_get_reply == connection)
1627 /* We don't need to track this pending reply anymore */
1629 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1631 pending->will_send_reply,
1632 pending->will_get_reply,
1633 pending->reply_serial);
1635 bus_expire_list_remove_link (connections->pending_replies,
1637 bus_pending_reply_free (pending);
1639 else if (pending->will_send_reply == connection)
1641 /* The reply isn't going to be sent, so set things
1642 * up so it will be expired right away
1644 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1646 pending->will_send_reply,
1647 pending->will_get_reply,
1648 pending->reply_serial);
1650 pending->will_send_reply = NULL;
1651 pending->expire_item.added_tv_sec = 0;
1652 pending->expire_item.added_tv_usec = 0;
1654 bus_expire_list_recheck_immediately (connections->pending_replies);
1664 BusPendingReply *pending;
1665 BusConnections *connections;
1666 } CancelPendingReplyData;
1669 cancel_pending_reply (void *data)
1671 CancelPendingReplyData *d = data;
1673 _dbus_verbose ("d = %p\n", d);
1675 if (!bus_expire_list_remove (d->connections->pending_replies,
1676 &d->pending->expire_item))
1677 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1679 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1683 cancel_pending_reply_data_free (void *data)
1685 CancelPendingReplyData *d = data;
1687 _dbus_verbose ("d = %p\n", d);
1689 /* d->pending should be either freed or still
1690 * in the list of pending replies (owned by someone
1698 * Record that a reply is allowed; return TRUE on success.
1701 bus_connections_expect_reply (BusConnections *connections,
1702 BusTransaction *transaction,
1703 DBusConnection *will_get_reply,
1704 DBusConnection *will_send_reply,
1705 DBusMessage *reply_to_this,
1708 BusPendingReply *pending;
1709 dbus_uint32_t reply_serial;
1711 CancelPendingReplyData *cprd;
1714 _dbus_assert (will_get_reply != NULL);
1715 _dbus_assert (will_send_reply != NULL);
1716 _dbus_assert (reply_to_this != NULL);
1718 if (dbus_message_get_no_reply (reply_to_this))
1719 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1721 reply_serial = dbus_message_get_serial (reply_to_this);
1723 link = bus_expire_list_get_first_link (connections->pending_replies);
1725 while (link != NULL)
1727 pending = link->data;
1729 if (pending->reply_serial == reply_serial &&
1730 pending->will_get_reply == will_get_reply &&
1731 pending->will_send_reply == will_send_reply)
1733 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1734 "Message has the same reply serial as a currently-outstanding existing method call");
1738 link = bus_expire_list_get_next_link (connections->pending_replies,
1740 if (pending->will_get_reply == will_get_reply)
1745 bus_context_get_max_replies_per_connection (connections->context))
1747 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1748 "The maximum number of pending replies per connection has been reached");
1752 pending = dbus_new0 (BusPendingReply, 1);
1753 if (pending == NULL)
1755 BUS_SET_OOM (error);
1759 #ifdef DBUS_ENABLE_VERBOSE_MODE
1760 /* so we can see a not-yet-added pending reply */
1761 pending->expire_item.added_tv_sec = 1;
1762 pending->expire_item.added_tv_usec = 1;
1765 pending->will_get_reply = will_get_reply;
1766 pending->will_send_reply = will_send_reply;
1767 pending->reply_serial = reply_serial;
1769 cprd = dbus_new0 (CancelPendingReplyData, 1);
1772 BUS_SET_OOM (error);
1773 bus_pending_reply_free (pending);
1777 if (!bus_expire_list_add (connections->pending_replies,
1778 &pending->expire_item))
1780 BUS_SET_OOM (error);
1782 bus_pending_reply_free (pending);
1786 if (!bus_transaction_add_cancel_hook (transaction,
1787 cancel_pending_reply,
1789 cancel_pending_reply_data_free))
1791 BUS_SET_OOM (error);
1792 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1794 bus_pending_reply_free (pending);
1798 cprd->pending = pending;
1799 cprd->connections = connections;
1801 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1802 &pending->expire_item.added_tv_usec);
1804 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1806 pending->will_send_reply,
1807 pending->will_get_reply,
1808 pending->reply_serial);
1816 BusConnections *connections;
1817 } CheckPendingReplyData;
1820 cancel_check_pending_reply (void *data)
1822 CheckPendingReplyData *d = data;
1824 _dbus_verbose ("d = %p\n",d);
1826 bus_expire_list_add_link (d->connections->pending_replies,
1832 check_pending_reply_data_free (void *data)
1834 CheckPendingReplyData *d = data;
1836 _dbus_verbose ("d = %p\n",d);
1838 if (d->link != NULL)
1840 BusPendingReply *pending = d->link->data;
1842 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1843 &pending->expire_item));
1845 bus_pending_reply_free (pending);
1846 _dbus_list_free_link (d->link);
1853 * Check whether a reply is allowed, remove BusPendingReply
1854 * if so, return TRUE if so.
1857 bus_connections_check_reply (BusConnections *connections,
1858 BusTransaction *transaction,
1859 DBusConnection *sending_reply,
1860 DBusConnection *receiving_reply,
1864 CheckPendingReplyData *cprd;
1866 dbus_uint32_t reply_serial;
1868 _dbus_assert (sending_reply != NULL);
1869 _dbus_assert (receiving_reply != NULL);
1871 reply_serial = dbus_message_get_reply_serial (reply);
1873 link = bus_expire_list_get_first_link (connections->pending_replies);
1874 while (link != NULL)
1876 BusPendingReply *pending = link->data;
1878 if (pending->reply_serial == reply_serial &&
1879 pending->will_get_reply == receiving_reply &&
1880 pending->will_send_reply == sending_reply)
1882 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1886 link = bus_expire_list_get_next_link (connections->pending_replies,
1892 _dbus_verbose ("No pending reply expected\n");
1897 cprd = dbus_new0 (CheckPendingReplyData, 1);
1900 BUS_SET_OOM (error);
1904 if (!bus_transaction_add_cancel_hook (transaction,
1905 cancel_check_pending_reply,
1907 check_pending_reply_data_free))
1909 BUS_SET_OOM (error);
1915 cprd->connections = connections;
1917 bus_expire_list_unlink (connections->pending_replies,
1920 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1928 * Note that this is fairly fragile; in particular, don't try to use
1929 * one transaction across any main loop iterations.
1934 BusTransaction *transaction;
1935 DBusMessage *message;
1936 DBusPreallocatedSend *preallocated;
1941 BusTransactionCancelFunction cancel_function;
1942 DBusFreeFunction free_data_function;
1946 struct BusTransaction
1948 DBusList *connections;
1949 BusContext *context;
1950 DBusList *cancel_hooks;
1954 message_to_send_free (DBusConnection *connection,
1955 MessageToSend *to_send)
1957 if (to_send->message)
1958 dbus_message_unref (to_send->message);
1960 if (to_send->preallocated)
1961 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1963 dbus_free (to_send);
1967 cancel_hook_cancel (void *element,
1970 CancelHook *ch = element;
1972 _dbus_verbose ("Running transaction cancel hook\n");
1974 if (ch->cancel_function)
1975 (* ch->cancel_function) (ch->data);
1979 cancel_hook_free (void *element,
1982 CancelHook *ch = element;
1984 if (ch->free_data_function)
1985 (* ch->free_data_function) (ch->data);
1991 free_cancel_hooks (BusTransaction *transaction)
1993 _dbus_list_foreach (&transaction->cancel_hooks,
1994 cancel_hook_free, NULL);
1996 _dbus_list_clear (&transaction->cancel_hooks);
2000 bus_transaction_new (BusContext *context)
2002 BusTransaction *transaction;
2004 transaction = dbus_new0 (BusTransaction, 1);
2005 if (transaction == NULL)
2008 transaction->context = context;
2014 bus_transaction_get_context (BusTransaction *transaction)
2016 return transaction->context;
2020 bus_transaction_get_connections (BusTransaction *transaction)
2022 return bus_context_get_connections (transaction->context);
2026 bus_transaction_send_from_driver (BusTransaction *transaction,
2027 DBusConnection *connection,
2028 DBusMessage *message)
2030 /* We have to set the sender to the driver, and have
2031 * to check security policy since it was not done in
2034 _dbus_verbose ("Sending %s %s %s from driver\n",
2035 dbus_message_get_interface (message) ?
2036 dbus_message_get_interface (message) : "(no interface)",
2037 dbus_message_get_member (message) ?
2038 dbus_message_get_member (message) : "(no member)",
2039 dbus_message_get_error_name (message) ?
2040 dbus_message_get_error_name (message) : "(no error name)");
2042 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2045 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) /* we can't set destination on the basis of connection when on kdbus*/
2046 if (bus_connection_is_active (connection))
2048 if (!dbus_message_set_destination (message,
2049 bus_connection_get_name (connection)))
2053 /* bus driver never wants a reply */
2054 dbus_message_set_no_reply (message, TRUE);
2056 /* If security policy doesn't allow the message, we silently
2057 * eat it; the driver doesn't care about getting a reply.
2059 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2061 NULL, connection, connection, message, NULL))
2064 return bus_transaction_send (transaction, connection, message);
2068 bus_transaction_send (BusTransaction *transaction,
2069 DBusConnection *connection,
2070 DBusMessage *message)
2072 MessageToSend *to_send;
2073 BusConnectionData *d;
2076 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2077 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2078 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2080 dbus_message_get_interface (message) ?
2081 dbus_message_get_interface (message) : "(unset)",
2082 dbus_message_get_member (message) ?
2083 dbus_message_get_member (message) : "(unset)",
2084 dbus_message_get_error_name (message) ?
2085 dbus_message_get_error_name (message) : "(unset)",
2086 dbus_connection_get_is_connected (connection) ?
2087 "" : " (disconnected)");
2089 _dbus_assert (dbus_message_get_sender (message) != NULL);
2091 if (!dbus_connection_get_is_connected (connection))
2092 return TRUE; /* silently ignore disconnected connections */
2094 d = BUS_CONNECTION_DATA (connection);
2095 _dbus_assert (d != NULL);
2097 to_send = dbus_new (MessageToSend, 1);
2098 if (to_send == NULL)
2103 to_send->preallocated = dbus_connection_preallocate_send (connection);
2104 if (to_send->preallocated == NULL)
2106 dbus_free (to_send);
2110 dbus_message_ref (message);
2111 to_send->message = message;
2112 to_send->transaction = transaction;
2114 _dbus_verbose ("about to prepend message\n");
2116 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2118 message_to_send_free (connection, to_send);
2122 _dbus_verbose ("prepended message\n");
2124 /* See if we already had this connection in the list
2125 * for this transaction. If we have a pending message,
2126 * then we should already be in transaction->connections
2128 link = _dbus_list_get_first_link (&d->transaction_messages);
2129 _dbus_assert (link->data == to_send);
2130 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2131 while (link != NULL)
2133 MessageToSend *m = link->data;
2134 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2136 if (m->transaction == transaction)
2144 if (!_dbus_list_prepend (&transaction->connections, connection))
2146 _dbus_list_remove (&d->transaction_messages, to_send);
2147 message_to_send_free (connection, to_send);
2156 connection_cancel_transaction (DBusConnection *connection,
2157 BusTransaction *transaction)
2160 BusConnectionData *d;
2162 d = BUS_CONNECTION_DATA (connection);
2163 _dbus_assert (d != NULL);
2165 link = _dbus_list_get_first_link (&d->transaction_messages);
2166 while (link != NULL)
2168 MessageToSend *m = link->data;
2169 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2171 if (m->transaction == transaction)
2173 _dbus_list_remove_link (&d->transaction_messages,
2176 message_to_send_free (connection, m);
2184 bus_transaction_cancel_and_free (BusTransaction *transaction)
2186 DBusConnection *connection;
2188 _dbus_verbose ("TRANSACTION: cancelled\n");
2190 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2191 connection_cancel_transaction (connection, transaction);
2193 _dbus_assert (transaction->connections == NULL);
2195 _dbus_list_foreach (&transaction->cancel_hooks,
2196 cancel_hook_cancel, NULL);
2198 free_cancel_hooks (transaction);
2200 dbus_free (transaction);
2204 connection_execute_transaction (DBusConnection *connection,
2205 BusTransaction *transaction)
2208 BusConnectionData *d;
2210 d = BUS_CONNECTION_DATA (connection);
2211 _dbus_assert (d != NULL);
2213 /* Send the queue in order (FIFO) */
2214 link = _dbus_list_get_last_link (&d->transaction_messages);
2215 while (link != NULL)
2217 MessageToSend *m = link->data;
2218 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2220 if (m->transaction == transaction)
2222 _dbus_list_remove_link (&d->transaction_messages,
2225 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2227 dbus_connection_send_preallocated (connection,
2232 m->preallocated = NULL; /* so we don't double-free it */
2234 message_to_send_free (connection, m);
2242 bus_transaction_execute_and_free (BusTransaction *transaction)
2244 /* For each connection in transaction->connections
2247 DBusConnection *connection;
2249 _dbus_verbose ("TRANSACTION: executing\n");
2251 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2252 connection_execute_transaction (connection, transaction);
2254 _dbus_assert (transaction->connections == NULL);
2256 free_cancel_hooks (transaction);
2258 dbus_free (transaction);
2262 bus_transaction_free (BusTransaction *transaction)
2264 free_cancel_hooks (transaction);
2265 dbus_free (transaction);
2269 bus_connection_remove_transactions (DBusConnection *connection)
2271 MessageToSend *to_send;
2272 BusConnectionData *d;
2274 d = BUS_CONNECTION_DATA (connection);
2275 _dbus_assert (d != NULL);
2277 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2279 /* only has an effect for the first MessageToSend listing this transaction */
2280 _dbus_list_remove (&to_send->transaction->connections,
2283 _dbus_list_remove (&d->transaction_messages, to_send);
2284 message_to_send_free (connection, to_send);
2289 * Converts the DBusError to a message reply
2292 bus_transaction_send_error_reply (BusTransaction *transaction,
2293 DBusConnection *connection,
2294 const DBusError *error,
2295 DBusMessage *in_reply_to)
2299 _dbus_assert (error != NULL);
2300 _DBUS_ASSERT_ERROR_IS_SET (error);
2302 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2303 error->name, error->message);
2305 reply = dbus_message_new_error (in_reply_to,
2311 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2313 dbus_message_unref (reply);
2317 dbus_message_unref (reply);
2323 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2324 BusTransactionCancelFunction cancel_function,
2326 DBusFreeFunction free_data_function)
2330 ch = dbus_new (CancelHook, 1);
2334 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2335 cancel_function, data);
2337 ch->cancel_function = cancel_function;
2339 ch->free_data_function = free_data_function;
2341 /* It's important that the hooks get run in reverse order that they
2344 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2353 #ifdef DBUS_ENABLE_STATS
2355 bus_connections_get_n_active (BusConnections *connections)
2357 return connections->n_completed;
2361 bus_connections_get_n_incomplete (BusConnections *connections)
2363 return connections->n_incomplete;
2367 bus_connections_get_total_match_rules (BusConnections *connections)
2369 return connections->total_match_rules;
2373 bus_connections_get_peak_match_rules (BusConnections *connections)
2375 return connections->peak_match_rules;
2379 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2381 return connections->peak_match_rules_per_conn;
2385 bus_connections_get_total_bus_names (BusConnections *connections)
2387 return connections->total_bus_names;
2391 bus_connections_get_peak_bus_names (BusConnections *connections)
2393 return connections->peak_bus_names;
2397 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2399 return connections->peak_bus_names_per_conn;
2403 bus_connection_get_peak_match_rules (DBusConnection *connection)
2405 BusConnectionData *d;
2407 d = BUS_CONNECTION_DATA (connection);
2408 return d->peak_match_rules;
2412 bus_connection_get_peak_bus_names (DBusConnection *connection)
2414 BusConnectionData *d;
2416 d = BUS_CONNECTION_DATA (connection);
2417 return d->peak_bus_names;
2419 #endif /* DBUS_ENABLE_STATS */