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_get_services_owned (DBusConnection *connection)
1383 BusConnectionData *d;
1384 d = BUS_CONNECTION_DATA (connection);
1385 _dbus_assert (d != NULL);
1387 return &d->services_owned;
1391 bus_connection_complete (DBusConnection *connection,
1392 const DBusString *name,
1395 BusConnectionData *d;
1398 d = BUS_CONNECTION_DATA (connection);
1399 _dbus_assert (d != NULL);
1400 _dbus_assert (d->name == NULL);
1401 _dbus_assert (d->policy == NULL);
1403 _dbus_assert (!bus_connection_is_active (connection));
1405 if (!_dbus_string_copy_data (name, &d->name))
1407 BUS_SET_OOM (error);
1411 _dbus_assert (d->name != NULL);
1413 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1415 d->policy = bus_context_create_client_policy (d->connections->context,
1419 /* we may have a NULL policy on OOM or error getting list of
1420 * groups for a user. In the latter case we don't handle it so
1421 * well currently, as it will just keep failing over and over.
1424 if (d->policy == NULL)
1426 _dbus_verbose ("Failed to create security policy for connection %p\n",
1428 _DBUS_ASSERT_ERROR_IS_SET (error);
1429 dbus_free (d->name);
1434 if (dbus_connection_get_unix_user (connection, &uid))
1436 if (!adjust_connections_for_uid (d->connections,
1441 /* Create and cache a string which holds information about the
1442 * peer process; used for logging purposes.
1444 if (!cache_peer_loginfo_string (d, connection))
1447 /* Now the connection is active, move it between lists */
1448 _dbus_list_unlink (&d->connections->incomplete,
1449 d->link_in_connection_list);
1450 d->connections->n_incomplete -= 1;
1451 _dbus_list_append_link (&d->connections->completed,
1452 d->link_in_connection_list);
1453 d->connections->n_completed += 1;
1455 _dbus_assert (d->connections->n_incomplete >= 0);
1456 _dbus_assert (d->connections->n_completed > 0);
1458 /* See if we can remove the timeout */
1459 bus_connections_expire_incomplete (d->connections);
1461 _dbus_assert (bus_connection_is_active (connection));
1465 BUS_SET_OOM (error);
1466 dbus_free (d->name);
1469 bus_client_policy_unref (d->policy);
1475 bus_connection_get_name (DBusConnection *connection)
1477 BusConnectionData *d;
1479 d = BUS_CONNECTION_DATA (connection);
1480 _dbus_assert (d != NULL);
1486 * Check whether completing the passed-in connection would
1487 * exceed limits, and if so set error and return #FALSE
1490 bus_connections_check_limits (BusConnections *connections,
1491 DBusConnection *requesting_completion,
1496 if (connections->n_completed >=
1497 bus_context_get_max_completed_connections (connections->context))
1499 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1500 "The maximum number of active connections has been reached");
1504 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1506 if (get_connections_for_uid (connections, uid) >=
1507 bus_context_get_max_connections_per_user (connections->context))
1509 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1510 "The maximum number of active connections for UID %lu has been reached",
1520 bus_pending_reply_free (BusPendingReply *pending)
1522 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1524 pending->will_send_reply,
1525 pending->will_get_reply,
1526 pending->reply_serial);
1528 dbus_free (pending);
1532 bus_pending_reply_send_no_reply (BusConnections *connections,
1533 BusTransaction *transaction,
1534 BusPendingReply *pending)
1536 DBusMessage *message;
1537 DBusMessageIter iter;
1543 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1544 if (message == NULL)
1547 dbus_message_set_no_reply (message, TRUE);
1549 if (!dbus_message_set_reply_serial (message,
1550 pending->reply_serial))
1553 if (!dbus_message_set_error_name (message,
1554 DBUS_ERROR_NO_REPLY))
1557 errmsg = "Message did not receive a reply (timeout by message bus)";
1558 dbus_message_iter_init_append (message, &iter);
1559 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1562 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1569 dbus_message_unref (message);
1574 bus_pending_reply_expired (BusExpireList *list,
1578 BusPendingReply *pending = link->data;
1579 BusConnections *connections = data;
1580 BusTransaction *transaction;
1582 /* No reply is forthcoming. So nuke it if we can. If not,
1583 * leave it in the list to try expiring again later when we
1587 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1589 pending->will_send_reply,
1590 pending->will_get_reply,
1591 pending->reply_serial);
1593 transaction = bus_transaction_new (connections->context);
1594 if (transaction == NULL)
1597 if (!bus_pending_reply_send_no_reply (connections,
1601 bus_transaction_cancel_and_free (transaction);
1605 bus_expire_list_remove_link (connections->pending_replies, link);
1607 bus_pending_reply_free (pending);
1608 bus_transaction_execute_and_free (transaction);
1614 bus_connection_drop_pending_replies (BusConnections *connections,
1615 DBusConnection *connection)
1617 /* The DBusConnection is almost 100% finalized here, so you can't
1618 * do anything with it except check for pointer equality
1622 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1625 link = bus_expire_list_get_first_link (connections->pending_replies);
1626 while (link != NULL)
1629 BusPendingReply *pending;
1631 next = bus_expire_list_get_next_link (connections->pending_replies,
1633 pending = link->data;
1635 if (pending->will_get_reply == connection)
1637 /* We don't need to track this pending reply anymore */
1639 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1641 pending->will_send_reply,
1642 pending->will_get_reply,
1643 pending->reply_serial);
1645 bus_expire_list_remove_link (connections->pending_replies,
1647 bus_pending_reply_free (pending);
1649 else if (pending->will_send_reply == connection)
1651 /* The reply isn't going to be sent, so set things
1652 * up so it will be expired right away
1654 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1656 pending->will_send_reply,
1657 pending->will_get_reply,
1658 pending->reply_serial);
1660 pending->will_send_reply = NULL;
1661 pending->expire_item.added_tv_sec = 0;
1662 pending->expire_item.added_tv_usec = 0;
1664 bus_expire_list_recheck_immediately (connections->pending_replies);
1674 BusPendingReply *pending;
1675 BusConnections *connections;
1676 } CancelPendingReplyData;
1679 cancel_pending_reply (void *data)
1681 CancelPendingReplyData *d = data;
1683 _dbus_verbose ("d = %p\n", d);
1685 if (!bus_expire_list_remove (d->connections->pending_replies,
1686 &d->pending->expire_item))
1687 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1689 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1693 cancel_pending_reply_data_free (void *data)
1695 CancelPendingReplyData *d = data;
1697 _dbus_verbose ("d = %p\n", d);
1699 /* d->pending should be either freed or still
1700 * in the list of pending replies (owned by someone
1708 * Record that a reply is allowed; return TRUE on success.
1711 bus_connections_expect_reply (BusConnections *connections,
1712 BusTransaction *transaction,
1713 DBusConnection *will_get_reply,
1714 DBusConnection *will_send_reply,
1715 DBusMessage *reply_to_this,
1718 BusPendingReply *pending;
1719 dbus_uint32_t reply_serial;
1721 CancelPendingReplyData *cprd;
1724 _dbus_assert (will_get_reply != NULL);
1725 _dbus_assert (will_send_reply != NULL);
1726 _dbus_assert (reply_to_this != NULL);
1728 if (dbus_message_get_no_reply (reply_to_this))
1729 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1731 reply_serial = dbus_message_get_serial (reply_to_this);
1733 link = bus_expire_list_get_first_link (connections->pending_replies);
1735 while (link != NULL)
1737 pending = link->data;
1739 if (pending->reply_serial == reply_serial &&
1740 pending->will_get_reply == will_get_reply &&
1741 pending->will_send_reply == will_send_reply)
1743 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1744 "Message has the same reply serial as a currently-outstanding existing method call");
1748 link = bus_expire_list_get_next_link (connections->pending_replies,
1750 if (pending->will_get_reply == will_get_reply)
1755 bus_context_get_max_replies_per_connection (connections->context))
1757 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1758 "The maximum number of pending replies per connection has been reached");
1762 pending = dbus_new0 (BusPendingReply, 1);
1763 if (pending == NULL)
1765 BUS_SET_OOM (error);
1769 #ifdef DBUS_ENABLE_VERBOSE_MODE
1770 /* so we can see a not-yet-added pending reply */
1771 pending->expire_item.added_tv_sec = 1;
1772 pending->expire_item.added_tv_usec = 1;
1775 pending->will_get_reply = will_get_reply;
1776 pending->will_send_reply = will_send_reply;
1777 pending->reply_serial = reply_serial;
1779 cprd = dbus_new0 (CancelPendingReplyData, 1);
1782 BUS_SET_OOM (error);
1783 bus_pending_reply_free (pending);
1787 if (!bus_expire_list_add (connections->pending_replies,
1788 &pending->expire_item))
1790 BUS_SET_OOM (error);
1792 bus_pending_reply_free (pending);
1796 if (!bus_transaction_add_cancel_hook (transaction,
1797 cancel_pending_reply,
1799 cancel_pending_reply_data_free))
1801 BUS_SET_OOM (error);
1802 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1804 bus_pending_reply_free (pending);
1808 cprd->pending = pending;
1809 cprd->connections = connections;
1811 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1812 &pending->expire_item.added_tv_usec);
1814 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1816 pending->will_send_reply,
1817 pending->will_get_reply,
1818 pending->reply_serial);
1826 BusConnections *connections;
1827 } CheckPendingReplyData;
1830 cancel_check_pending_reply (void *data)
1832 CheckPendingReplyData *d = data;
1834 _dbus_verbose ("d = %p\n",d);
1836 bus_expire_list_add_link (d->connections->pending_replies,
1842 check_pending_reply_data_free (void *data)
1844 CheckPendingReplyData *d = data;
1846 _dbus_verbose ("d = %p\n",d);
1848 if (d->link != NULL)
1850 BusPendingReply *pending = d->link->data;
1852 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1853 &pending->expire_item));
1855 bus_pending_reply_free (pending);
1856 _dbus_list_free_link (d->link);
1863 * Check whether a reply is allowed, remove BusPendingReply
1864 * if so, return TRUE if so.
1867 bus_connections_check_reply (BusConnections *connections,
1868 BusTransaction *transaction,
1869 DBusConnection *sending_reply,
1870 DBusConnection *receiving_reply,
1874 CheckPendingReplyData *cprd;
1876 dbus_uint32_t reply_serial;
1878 _dbus_assert (sending_reply != NULL);
1879 _dbus_assert (receiving_reply != NULL);
1881 reply_serial = dbus_message_get_reply_serial (reply);
1883 link = bus_expire_list_get_first_link (connections->pending_replies);
1884 while (link != NULL)
1886 BusPendingReply *pending = link->data;
1888 if (pending->reply_serial == reply_serial &&
1889 pending->will_get_reply == receiving_reply &&
1890 pending->will_send_reply == sending_reply)
1892 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1896 link = bus_expire_list_get_next_link (connections->pending_replies,
1902 _dbus_verbose ("No pending reply expected\n");
1907 cprd = dbus_new0 (CheckPendingReplyData, 1);
1910 BUS_SET_OOM (error);
1914 if (!bus_transaction_add_cancel_hook (transaction,
1915 cancel_check_pending_reply,
1917 check_pending_reply_data_free))
1919 BUS_SET_OOM (error);
1925 cprd->connections = connections;
1927 bus_expire_list_unlink (connections->pending_replies,
1930 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1938 * Note that this is fairly fragile; in particular, don't try to use
1939 * one transaction across any main loop iterations.
1944 BusTransaction *transaction;
1945 DBusMessage *message;
1946 DBusPreallocatedSend *preallocated;
1951 BusTransactionCancelFunction cancel_function;
1952 DBusFreeFunction free_data_function;
1956 struct BusTransaction
1958 DBusList *connections;
1959 BusContext *context;
1960 DBusList *cancel_hooks;
1964 message_to_send_free (DBusConnection *connection,
1965 MessageToSend *to_send)
1967 if (to_send->message)
1968 dbus_message_unref (to_send->message);
1970 if (to_send->preallocated)
1971 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1973 dbus_free (to_send);
1977 cancel_hook_cancel (void *element,
1980 CancelHook *ch = element;
1982 _dbus_verbose ("Running transaction cancel hook\n");
1984 if (ch->cancel_function)
1985 (* ch->cancel_function) (ch->data);
1989 cancel_hook_free (void *element,
1992 CancelHook *ch = element;
1994 if (ch->free_data_function)
1995 (* ch->free_data_function) (ch->data);
2001 free_cancel_hooks (BusTransaction *transaction)
2003 _dbus_list_foreach (&transaction->cancel_hooks,
2004 cancel_hook_free, NULL);
2006 _dbus_list_clear (&transaction->cancel_hooks);
2010 bus_transaction_new (BusContext *context)
2012 BusTransaction *transaction;
2014 transaction = dbus_new0 (BusTransaction, 1);
2015 if (transaction == NULL)
2018 transaction->context = context;
2024 bus_transaction_get_context (BusTransaction *transaction)
2026 return transaction->context;
2030 bus_transaction_get_connections (BusTransaction *transaction)
2032 return bus_context_get_connections (transaction->context);
2036 bus_transaction_send_from_driver (BusTransaction *transaction,
2037 DBusConnection *connection,
2038 DBusMessage *message)
2040 /* We have to set the sender to the driver, and have
2041 * to check security policy since it was not done in
2044 _dbus_verbose ("Sending %s %s %s from driver\n",
2045 dbus_message_get_interface (message) ?
2046 dbus_message_get_interface (message) : "(no interface)",
2047 dbus_message_get_member (message) ?
2048 dbus_message_get_member (message) : "(no member)",
2049 dbus_message_get_error_name (message) ?
2050 dbus_message_get_error_name (message) : "(no error name)");
2052 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2055 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) /* we can't set destination on the basis of connection when on kdbus*/
2056 if (bus_connection_is_active (connection))
2058 if (!dbus_message_set_destination (message,
2059 bus_connection_get_name (connection)))
2063 /* bus driver never wants a reply */
2064 dbus_message_set_no_reply (message, TRUE);
2066 /* If security policy doesn't allow the message, we silently
2067 * eat it; the driver doesn't care about getting a reply.
2069 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2071 NULL, connection, connection, message, NULL))
2074 return bus_transaction_send (transaction, connection, message);
2078 bus_transaction_send (BusTransaction *transaction,
2079 DBusConnection *connection,
2080 DBusMessage *message)
2082 MessageToSend *to_send;
2083 BusConnectionData *d;
2086 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2087 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2088 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2090 dbus_message_get_interface (message) ?
2091 dbus_message_get_interface (message) : "(unset)",
2092 dbus_message_get_member (message) ?
2093 dbus_message_get_member (message) : "(unset)",
2094 dbus_message_get_error_name (message) ?
2095 dbus_message_get_error_name (message) : "(unset)",
2096 dbus_connection_get_is_connected (connection) ?
2097 "" : " (disconnected)");
2099 _dbus_assert (dbus_message_get_sender (message) != NULL);
2101 if (!dbus_connection_get_is_connected (connection))
2102 return TRUE; /* silently ignore disconnected connections */
2104 d = BUS_CONNECTION_DATA (connection);
2105 _dbus_assert (d != NULL);
2107 to_send = dbus_new (MessageToSend, 1);
2108 if (to_send == NULL)
2113 to_send->preallocated = dbus_connection_preallocate_send (connection);
2114 if (to_send->preallocated == NULL)
2116 dbus_free (to_send);
2120 dbus_message_ref (message);
2121 to_send->message = message;
2122 to_send->transaction = transaction;
2124 _dbus_verbose ("about to prepend message\n");
2126 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2128 message_to_send_free (connection, to_send);
2132 _dbus_verbose ("prepended message\n");
2134 /* See if we already had this connection in the list
2135 * for this transaction. If we have a pending message,
2136 * then we should already be in transaction->connections
2138 link = _dbus_list_get_first_link (&d->transaction_messages);
2139 _dbus_assert (link->data == to_send);
2140 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2141 while (link != NULL)
2143 MessageToSend *m = link->data;
2144 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2146 if (m->transaction == transaction)
2154 if (!_dbus_list_prepend (&transaction->connections, connection))
2156 _dbus_list_remove (&d->transaction_messages, to_send);
2157 message_to_send_free (connection, to_send);
2166 connection_cancel_transaction (DBusConnection *connection,
2167 BusTransaction *transaction)
2170 BusConnectionData *d;
2172 d = BUS_CONNECTION_DATA (connection);
2173 _dbus_assert (d != NULL);
2175 link = _dbus_list_get_first_link (&d->transaction_messages);
2176 while (link != NULL)
2178 MessageToSend *m = link->data;
2179 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2181 if (m->transaction == transaction)
2183 _dbus_list_remove_link (&d->transaction_messages,
2186 message_to_send_free (connection, m);
2194 bus_transaction_cancel_and_free (BusTransaction *transaction)
2196 DBusConnection *connection;
2198 _dbus_verbose ("TRANSACTION: cancelled\n");
2200 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2201 connection_cancel_transaction (connection, transaction);
2203 _dbus_assert (transaction->connections == NULL);
2205 _dbus_list_foreach (&transaction->cancel_hooks,
2206 cancel_hook_cancel, NULL);
2208 free_cancel_hooks (transaction);
2210 dbus_free (transaction);
2214 connection_execute_transaction (DBusConnection *connection,
2215 BusTransaction *transaction)
2218 BusConnectionData *d;
2220 d = BUS_CONNECTION_DATA (connection);
2221 _dbus_assert (d != NULL);
2223 /* Send the queue in order (FIFO) */
2224 link = _dbus_list_get_last_link (&d->transaction_messages);
2225 while (link != NULL)
2227 MessageToSend *m = link->data;
2228 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2230 if (m->transaction == transaction)
2232 _dbus_list_remove_link (&d->transaction_messages,
2235 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2237 dbus_connection_send_preallocated (connection,
2242 m->preallocated = NULL; /* so we don't double-free it */
2244 message_to_send_free (connection, m);
2252 bus_transaction_execute_and_free (BusTransaction *transaction)
2254 /* For each connection in transaction->connections
2257 DBusConnection *connection;
2259 _dbus_verbose ("TRANSACTION: executing\n");
2261 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2262 connection_execute_transaction (connection, transaction);
2264 _dbus_assert (transaction->connections == NULL);
2266 free_cancel_hooks (transaction);
2268 dbus_free (transaction);
2272 bus_transaction_free (BusTransaction *transaction)
2274 free_cancel_hooks (transaction);
2275 dbus_free (transaction);
2279 bus_connection_remove_transactions (DBusConnection *connection)
2281 MessageToSend *to_send;
2282 BusConnectionData *d;
2284 d = BUS_CONNECTION_DATA (connection);
2285 _dbus_assert (d != NULL);
2287 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2289 /* only has an effect for the first MessageToSend listing this transaction */
2290 _dbus_list_remove (&to_send->transaction->connections,
2293 _dbus_list_remove (&d->transaction_messages, to_send);
2294 message_to_send_free (connection, to_send);
2299 * Converts the DBusError to a message reply
2302 bus_transaction_send_error_reply (BusTransaction *transaction,
2303 DBusConnection *connection,
2304 const DBusError *error,
2305 DBusMessage *in_reply_to)
2309 _dbus_assert (error != NULL);
2310 _DBUS_ASSERT_ERROR_IS_SET (error);
2312 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2313 error->name, error->message);
2315 reply = dbus_message_new_error (in_reply_to,
2321 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2323 dbus_message_unref (reply);
2327 dbus_message_unref (reply);
2333 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2334 BusTransactionCancelFunction cancel_function,
2336 DBusFreeFunction free_data_function)
2340 ch = dbus_new (CancelHook, 1);
2344 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2345 cancel_function, data);
2347 ch->cancel_function = cancel_function;
2349 ch->free_data_function = free_data_function;
2351 /* It's important that the hooks get run in reverse order that they
2354 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2363 #ifdef DBUS_ENABLE_STATS
2365 bus_connections_get_n_active (BusConnections *connections)
2367 return connections->n_completed;
2371 bus_connections_get_n_incomplete (BusConnections *connections)
2373 return connections->n_incomplete;
2377 bus_connections_get_total_match_rules (BusConnections *connections)
2379 return connections->total_match_rules;
2383 bus_connections_get_peak_match_rules (BusConnections *connections)
2385 return connections->peak_match_rules;
2389 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2391 return connections->peak_match_rules_per_conn;
2395 bus_connections_get_total_bus_names (BusConnections *connections)
2397 return connections->total_bus_names;
2401 bus_connections_get_peak_bus_names (BusConnections *connections)
2403 return connections->peak_bus_names;
2407 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2409 return connections->peak_bus_names_per_conn;
2413 bus_connection_get_peak_match_rules (DBusConnection *connection)
2415 BusConnectionData *d;
2417 d = BUS_CONNECTION_DATA (connection);
2418 return d->peak_match_rules;
2422 bus_connection_get_peak_bus_names (DBusConnection *connection)
2424 BusConnectionData *d;
2426 d = BUS_CONNECTION_DATA (connection);
2427 return d->peak_bus_names;
2429 #endif /* DBUS_ENABLE_STATS */