1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
5 * Copyright (C) 2013 Samsung Electronics
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "connection.h"
32 #include "expirelist.h"
34 #include <dbus/dbus-list.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-timeout.h>
38 /* Trim executed commands to this length; we want to keep logs readable */
39 #define MAX_LOG_COMMAND_LEN 50
41 static void bus_connection_remove_transactions (DBusConnection *connection);
45 BusExpireItem expire_item;
47 DBusConnection *will_get_reply;
48 DBusConnection *will_send_reply;
50 dbus_uint32_t reply_serial;
57 DBusList *completed; /**< List of all completed connections */
58 int n_completed; /**< Length of completed list */
59 DBusList *incomplete; /**< List of all not-yet-active connections */
60 int n_incomplete; /**< Length of incomplete list */
62 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
63 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
64 int stamp; /**< Incrementing number */
65 BusExpireList *pending_replies; /**< List of pending replies */
67 #ifdef DBUS_ENABLE_STATS
68 int total_match_rules;
70 int peak_match_rules_per_conn;
74 int peak_bus_names_per_conn;
78 static dbus_int32_t connection_data_slot = -1;
82 BusConnections *connections;
83 DBusList *link_in_connection_list;
84 DBusConnection *connection;
85 DBusList *services_owned;
87 DBusList *match_rules;
90 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
91 DBusMessage *oom_message;
92 DBusPreallocatedSend *oom_preallocated;
93 BusClientPolicy *policy;
95 char *cached_loginfo_string;
96 BusSELinuxID *selinux_id;
98 long connection_tv_sec; /**< Time when we connected (seconds component) */
99 long connection_tv_usec; /**< Time when we connected (microsec component) */
100 int stamp; /**< connections->stamp last time we were traversed */
102 #ifdef DBUS_ENABLE_STATS
103 int peak_match_rules;
108 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
112 static void bus_connection_drop_pending_replies (BusConnections *connections,
113 DBusConnection *connection);
115 static dbus_bool_t expire_incomplete_timeout (void *data);
117 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
120 connection_get_loop (DBusConnection *connection)
122 BusConnectionData *d;
124 d = BUS_CONNECTION_DATA (connection);
126 return bus_context_get_loop (d->connections->context);
131 get_connections_for_uid (BusConnections *connections,
137 /* val is NULL is 0 when it isn't in the hash yet */
139 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
142 current_count = _DBUS_POINTER_TO_INT (val);
144 return current_count;
148 adjust_connections_for_uid (BusConnections *connections,
154 current_count = get_connections_for_uid (connections, uid);
156 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
157 ": was %d adjustment %d making %d\n",
158 uid, current_count, adjustment, current_count + adjustment);
160 _dbus_assert (current_count >= 0);
162 current_count += adjustment;
164 _dbus_assert (current_count >= 0);
166 if (current_count == 0)
168 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
175 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
176 uid, _DBUS_INT_TO_POINTER (current_count));
178 /* only positive adjustment can fail as otherwise
179 * a hash entry should already exist
181 _dbus_assert (adjustment > 0 ||
182 (adjustment <= 0 && retval));
189 bus_connection_disconnected (DBusConnection *connection)
191 BusConnectionData *d;
193 BusMatchmaker *matchmaker;
194 #ifdef ENABLE_KDBUS_TRANSPORT
195 dbus_bool_t is_phantom = FALSE;
198 d = BUS_CONNECTION_DATA (connection);
199 _dbus_assert (d != NULL);
201 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
202 d->name ? d->name : "(inactive)");
204 #ifdef ENABLE_KDBUS_TRANSPORT
205 if(bus_context_is_kdbus(d->connections->context) && (strcmp(bus_connection_get_name(connection), ":1.1")))
209 /* Delete our match rules */
210 if (d->n_match_rules > 0)
212 matchmaker = bus_context_get_matchmaker (d->connections->context);
213 bus_matchmaker_disconnected (matchmaker, connection);
216 /* Drop any service ownership. Unfortunately, this requires
217 * memory allocation and there doesn't seem to be a good way to
218 * handle it other than sleeping; we can't "fail" the operation of
219 * disconnecting a client, and preallocating a broadcast "service is
220 * now gone" message for every client-service pair seems kind of
223 while ((service = _dbus_list_get_last (&d->services_owned)))
225 BusTransaction *transaction;
230 dbus_error_init (&error);
232 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
233 _dbus_wait_for_memory ();
235 if (!bus_service_remove_owner (service, connection,
236 transaction, &error))
238 _DBUS_ASSERT_ERROR_IS_SET (&error);
240 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
242 dbus_error_free (&error);
243 bus_transaction_cancel_and_free (transaction);
244 _dbus_wait_for_memory ();
249 _dbus_verbose ("Failed to remove service owner: %s %s\n",
250 error.name, error.message);
251 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
255 bus_transaction_execute_and_free (transaction);
258 bus_dispatch_remove_connection (connection);
260 /* no more watching */
261 if (!dbus_connection_set_watch_functions (connection,
265 _dbus_assert_not_reached ("setting watch functions to NULL failed");
267 if (!dbus_connection_set_timeout_functions (connection,
271 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
273 dbus_connection_set_unix_user_function (connection,
275 dbus_connection_set_windows_user_function (connection,
278 dbus_connection_set_dispatch_status_function (connection,
281 bus_connection_remove_transactions (connection);
283 if (d->link_in_connection_list != NULL)
289 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
290 d->link_in_connection_list = NULL;
291 d->connections->n_completed -= 1;
293 if (dbus_connection_get_unix_user (connection, &uid))
295 if (!adjust_connections_for_uid (d->connections,
297 _dbus_assert_not_reached ("adjusting downward should never fail");
302 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
303 d->link_in_connection_list = NULL;
304 d->connections->n_incomplete -= 1;
307 _dbus_assert (d->connections->n_incomplete >= 0);
308 _dbus_assert (d->connections->n_completed >= 0);
311 bus_connection_drop_pending_replies (d->connections, connection);
313 /* frees "d" as side effect */
314 dbus_connection_set_data (connection,
315 connection_data_slot,
318 #ifdef ENABLE_KDBUS_TRANSPORT
320 dbus_connection_unref_phantom(connection);
323 dbus_connection_unref (connection);
327 add_connection_watch (DBusWatch *watch,
330 DBusConnection *connection = data;
332 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
336 remove_connection_watch (DBusWatch *watch,
339 DBusConnection *connection = data;
341 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
345 toggle_connection_watch (DBusWatch *watch,
348 DBusConnection *connection = data;
350 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
354 add_connection_timeout (DBusTimeout *timeout,
357 DBusConnection *connection = data;
359 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
363 remove_connection_timeout (DBusTimeout *timeout,
366 DBusConnection *connection = data;
368 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
372 dispatch_status_function (DBusConnection *connection,
373 DBusDispatchStatus new_status,
376 DBusLoop *loop = data;
378 if (new_status != DBUS_DISPATCH_COMPLETE)
380 while (!_dbus_loop_queue_dispatch (loop, connection))
381 _dbus_wait_for_memory ();
386 allow_unix_user_function (DBusConnection *connection,
390 BusConnectionData *d;
392 d = BUS_CONNECTION_DATA (connection);
394 _dbus_assert (d != NULL);
396 return bus_context_allow_unix_user (d->connections->context, uid);
400 free_connection_data (void *data)
402 BusConnectionData *d = data;
404 /* services_owned should be NULL since we should be disconnected */
405 _dbus_assert (d->services_owned == NULL);
406 _dbus_assert (d->n_services_owned == 0);
408 _dbus_assert (d->transaction_messages == NULL);
410 if (d->oom_preallocated)
411 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
414 dbus_message_unref (d->oom_message);
417 bus_client_policy_unref (d->policy);
420 bus_selinux_id_unref (d->selinux_id);
422 dbus_free (d->cached_loginfo_string);
430 bus_connections_new (BusContext *context)
432 BusConnections *connections;
434 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
437 connections = dbus_new0 (BusConnections, 1);
438 if (connections == NULL)
441 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
443 if (connections->completed_by_user == NULL)
446 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
447 expire_incomplete_timeout,
449 if (connections->expire_timeout == NULL)
452 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
454 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
455 bus_context_get_reply_timeout (context),
456 bus_pending_reply_expired,
458 if (connections->pending_replies == NULL)
461 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
462 connections->expire_timeout))
465 connections->refcount = 1;
466 connections->context = context;
471 bus_expire_list_free (connections->pending_replies);
473 _dbus_timeout_unref (connections->expire_timeout);
475 _dbus_hash_table_unref (connections->completed_by_user);
477 dbus_free (connections);
479 dbus_connection_free_data_slot (&connection_data_slot);
485 bus_connections_ref (BusConnections *connections)
487 _dbus_assert (connections->refcount > 0);
488 connections->refcount += 1;
494 bus_connections_unref (BusConnections *connections)
496 _dbus_assert (connections->refcount > 0);
497 connections->refcount -= 1;
498 if (connections->refcount == 0)
500 /* drop all incomplete */
501 while (connections->incomplete != NULL)
503 DBusConnection *connection;
505 connection = connections->incomplete->data;
507 dbus_connection_ref (connection);
508 dbus_connection_close (connection);
509 bus_connection_disconnected (connection);
510 dbus_connection_unref (connection);
513 _dbus_assert (connections->n_incomplete == 0);
515 /* drop all real connections */
516 while (connections->completed != NULL)
518 DBusConnection *connection;
520 connection = connections->completed->data;
522 dbus_connection_ref (connection);
523 dbus_connection_close (connection);
524 bus_connection_disconnected (connection);
525 dbus_connection_unref (connection);
528 _dbus_assert (connections->n_completed == 0);
530 bus_expire_list_free (connections->pending_replies);
532 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
533 connections->expire_timeout);
535 _dbus_timeout_unref (connections->expire_timeout);
537 _dbus_hash_table_unref (connections->completed_by_user);
539 dbus_free (connections);
541 dbus_connection_free_data_slot (&connection_data_slot);
545 /* Used for logging */
547 cache_peer_loginfo_string (BusConnectionData *d,
548 DBusConnection *connection)
550 DBusString loginfo_buf;
554 dbus_bool_t prev_added;
556 if (!_dbus_string_init (&loginfo_buf))
560 if (dbus_connection_get_unix_user (connection, &uid))
562 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
568 if (dbus_connection_get_unix_process_id (connection, &pid))
572 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
575 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
577 /* Ignore errors here; we may not have permissions to read the
579 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
580 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
584 if (dbus_connection_get_windows_user (connection, &windows_sid))
586 dbus_bool_t did_append;
587 did_append = _dbus_string_append_printf (&loginfo_buf,
588 "sid=\"%s\" ", windows_sid);
589 dbus_free (windows_sid);
594 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
597 _dbus_string_free (&loginfo_buf);
601 _dbus_string_free (&loginfo_buf);
606 bus_connections_setup_connection (BusConnections *connections,
607 DBusConnection *connection)
610 BusConnectionData *d;
615 d = dbus_new0 (BusConnectionData, 1);
620 d->connections = connections;
621 d->connection = connection;
623 _dbus_get_monotonic_time (&d->connection_tv_sec,
624 &d->connection_tv_usec);
626 _dbus_assert (connection_data_slot >= 0);
628 if (!dbus_connection_set_data (connection,
629 connection_data_slot,
630 d, free_connection_data))
636 dbus_connection_set_route_peer_messages (connection, TRUE);
640 dbus_error_init (&error);
641 d->selinux_id = bus_selinux_init_connection_id (connection,
643 if (dbus_error_is_set (&error))
645 /* This is a bit bogus because we pretend all errors
646 * are OOM; this is done because we know that in bus.c
647 * an OOM error disconnects the connection, which is
648 * the same thing we want on any other error.
650 dbus_error_free (&error);
654 if (!dbus_connection_set_watch_functions (connection,
655 add_connection_watch,
656 remove_connection_watch,
657 toggle_connection_watch,
662 if (!dbus_connection_set_timeout_functions (connection,
663 add_connection_timeout,
664 remove_connection_timeout,
669 /* For now we don't need to set a Windows user function because
670 * there are no policies in the config file controlling what
671 * Windows users can connect. The default 'same user that owns the
672 * bus can connect' behavior of DBusConnection is fine on Windows.
674 dbus_connection_set_unix_user_function (connection,
675 allow_unix_user_function,
678 dbus_connection_set_dispatch_status_function (connection,
679 dispatch_status_function,
680 bus_context_get_loop (connections->context),
683 d->link_in_connection_list = _dbus_list_alloc_link (connection);
684 if (d->link_in_connection_list == NULL)
687 /* Setup the connection with the dispatcher */
688 if (!bus_dispatch_add_connection (connection))
691 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
693 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
695 bus_dispatch_remove_connection (connection);
700 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
701 connections->n_incomplete += 1;
703 dbus_connection_ref (connection);
705 /* Note that we might disconnect ourselves here, but it only takes
706 * effect on return to the main loop. We call this to free up
707 * expired connections if possible, and to queue the timeout for our
710 bus_connections_expire_incomplete (connections);
712 /* And we might also disconnect ourselves here, but again it
713 * only takes effect on return to main loop.
715 if (connections->n_incomplete >
716 bus_context_get_max_incomplete_connections (connections->context))
718 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
720 _dbus_assert (connections->incomplete != NULL);
721 /* Disconnect the oldest unauthenticated connection. FIXME
722 * would it be more secure to drop a *random* connection? This
723 * algorithm seems to mean that if someone can create new
724 * connections quickly enough, they can keep anyone else from
725 * completing authentication. But random may or may not really
726 * help with that, a more elaborate solution might be required.
728 dbus_connection_close (connections->incomplete->data);
737 bus_selinux_id_unref (d->selinux_id);
738 d->selinux_id = NULL;
740 if (!dbus_connection_set_watch_functions (connection,
744 _dbus_assert_not_reached ("setting watch functions to NULL failed");
746 if (!dbus_connection_set_timeout_functions (connection,
750 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
752 dbus_connection_set_unix_user_function (connection,
755 dbus_connection_set_windows_user_function (connection,
758 dbus_connection_set_dispatch_status_function (connection,
761 if (d->link_in_connection_list != NULL)
763 _dbus_assert (d->link_in_connection_list->next == NULL);
764 _dbus_assert (d->link_in_connection_list->prev == NULL);
765 _dbus_list_free_link (d->link_in_connection_list);
766 d->link_in_connection_list = NULL;
769 if (!dbus_connection_set_data (connection,
770 connection_data_slot,
772 _dbus_assert_not_reached ("failed to set connection data to null");
774 /* "d" has now been freed */
781 bus_connections_expire_incomplete (BusConnections *connections)
787 if (connections->incomplete != NULL)
789 long tv_sec, tv_usec;
793 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
794 auth_timeout = bus_context_get_auth_timeout (connections->context);
796 link = _dbus_list_get_first_link (&connections->incomplete);
799 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
800 DBusConnection *connection;
801 BusConnectionData *d;
804 connection = link->data;
806 d = BUS_CONNECTION_DATA (connection);
808 _dbus_assert (d != NULL);
810 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
811 d->connection_tv_usec,
814 if (elapsed >= (double) auth_timeout)
816 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
817 dbus_connection_close (connection);
821 /* We can end the loop, since the connections are in oldest-first order */
822 next_interval = ((double)auth_timeout) - elapsed;
823 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
824 connection, next_interval);
833 bus_expire_timeout_set_interval (connections->expire_timeout,
838 expire_incomplete_timeout (void *data)
840 BusConnections *connections = data;
842 _dbus_verbose ("Running\n");
844 /* note that this may remove the timeout */
845 bus_connections_expire_incomplete (connections);
851 bus_connection_get_unix_groups (DBusConnection *connection,
852 unsigned long **groups,
861 if (dbus_connection_get_unix_user (connection, &uid))
863 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
865 _dbus_verbose ("Did not get any groups for UID %lu\n",
871 _dbus_verbose ("Got %d groups for UID %lu\n",
877 return TRUE; /* successfully got 0 groups */
881 bus_connection_is_in_unix_group (DBusConnection *connection,
885 unsigned long *group_ids;
888 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
893 while (i < n_group_ids)
895 if (group_ids[i] == gid)
897 dbus_free (group_ids);
903 dbus_free (group_ids);
908 bus_connection_get_loginfo (DBusConnection *connection)
910 BusConnectionData *d;
912 d = BUS_CONNECTION_DATA (connection);
914 if (!bus_connection_is_active (connection))
916 return d->cached_loginfo_string;
920 bus_connection_get_policy (DBusConnection *connection)
922 BusConnectionData *d;
924 d = BUS_CONNECTION_DATA (connection);
926 _dbus_assert (d != NULL);
927 _dbus_assert (d->policy != NULL);
933 foreach_active (BusConnections *connections,
934 BusConnectionForeachFunction function,
939 link = _dbus_list_get_first_link (&connections->completed);
942 DBusConnection *connection = link->data;
943 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
945 if (!(* function) (connection, data))
955 foreach_inactive (BusConnections *connections,
956 BusConnectionForeachFunction function,
961 link = _dbus_list_get_first_link (&connections->incomplete);
964 DBusConnection *connection = link->data;
965 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
967 if (!(* function) (connection, data))
977 * Calls function on each active connection; if the function returns
978 * #FALSE, stops iterating. Active connections are authenticated
979 * and have sent a Hello message.
981 * @param connections the connections object
982 * @param function the function
983 * @param data data to pass to it as a second arg
986 bus_connections_foreach_active (BusConnections *connections,
987 BusConnectionForeachFunction function,
990 foreach_active (connections, function, data);
994 * Calls function on each connection; if the function returns
995 * #FALSE, stops iterating.
997 * @param connections the connections object
998 * @param function the function
999 * @param data data to pass to it as a second arg
1002 bus_connections_foreach (BusConnections *connections,
1003 BusConnectionForeachFunction function,
1006 if (!foreach_active (connections, function, data))
1009 foreach_inactive (connections, function, data);
1012 #ifdef ENABLE_KDBUS_TRANSPORT
1014 bus_connections_find_conn_by_name(BusConnections *connections, const char* name)
1018 link = _dbus_list_get_first_link (&connections->completed);
1019 while (link != NULL)
1021 DBusConnection *connection = link->data;
1022 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1024 if (!strcmp(bus_connection_get_name(connection), name))
1035 bus_connections_get_context (BusConnections *connections)
1037 return connections->context;
1041 * This is used to avoid covering the same connection twice when
1042 * traversing connections. Note that it assumes we will
1043 * bus_connection_mark_stamp() each connection at least once per
1044 * INT_MAX increments of the global stamp, or wraparound would break
1048 bus_connections_increment_stamp (BusConnections *connections)
1050 connections->stamp += 1;
1053 /* Mark connection with current stamp, return TRUE if it
1054 * didn't already have that stamp
1057 bus_connection_mark_stamp (DBusConnection *connection)
1059 BusConnectionData *d;
1061 d = BUS_CONNECTION_DATA (connection);
1063 _dbus_assert (d != NULL);
1065 if (d->stamp == d->connections->stamp)
1069 d->stamp = d->connections->stamp;
1075 bus_connection_get_context (DBusConnection *connection)
1077 BusConnectionData *d;
1079 d = BUS_CONNECTION_DATA (connection);
1081 _dbus_assert (d != NULL);
1083 return d->connections->context;
1087 bus_connection_get_connections (DBusConnection *connection)
1089 BusConnectionData *d;
1091 d = BUS_CONNECTION_DATA (connection);
1093 _dbus_assert (d != NULL);
1095 return d->connections;
1099 bus_connection_get_registry (DBusConnection *connection)
1101 BusConnectionData *d;
1103 d = BUS_CONNECTION_DATA (connection);
1105 _dbus_assert (d != NULL);
1107 return bus_context_get_registry (d->connections->context);
1111 bus_connection_get_activation (DBusConnection *connection)
1113 BusConnectionData *d;
1115 d = BUS_CONNECTION_DATA (connection);
1117 _dbus_assert (d != NULL);
1119 return bus_context_get_activation (d->connections->context);
1123 bus_connection_get_matchmaker (DBusConnection *connection)
1125 BusConnectionData *d;
1127 d = BUS_CONNECTION_DATA (connection);
1129 _dbus_assert (d != NULL);
1131 return bus_context_get_matchmaker (d->connections->context);
1135 bus_connection_get_selinux_id (DBusConnection *connection)
1137 BusConnectionData *d;
1139 d = BUS_CONNECTION_DATA (connection);
1141 _dbus_assert (d != NULL);
1143 return d->selinux_id;
1147 * Checks whether the connection is registered with the message bus.
1149 * @param connection the connection
1150 * @returns #TRUE if we're an active message bus participant
1153 bus_connection_is_active (DBusConnection *connection)
1155 BusConnectionData *d;
1157 d = BUS_CONNECTION_DATA (connection);
1159 return d != NULL && d->name != NULL;
1163 bus_connection_preallocate_oom_error (DBusConnection *connection)
1165 DBusMessage *message;
1166 DBusPreallocatedSend *preallocated;
1167 BusConnectionData *d;
1169 d = BUS_CONNECTION_DATA (connection);
1171 _dbus_assert (d != NULL);
1173 if (d->oom_preallocated != NULL)
1176 preallocated = dbus_connection_preallocate_send (connection);
1177 if (preallocated == NULL)
1180 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1182 if (message == NULL)
1184 dbus_connection_free_preallocated_send (connection, preallocated);
1188 /* d->name may be NULL, but that is OK */
1189 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1190 !dbus_message_set_destination (message, d->name) ||
1191 !dbus_message_set_sender (message,
1194 dbus_connection_free_preallocated_send (connection, preallocated);
1195 dbus_message_unref (message);
1199 /* set reply serial to placeholder value just so space is already allocated
1202 if (!dbus_message_set_reply_serial (message, 14))
1204 dbus_connection_free_preallocated_send (connection, preallocated);
1205 dbus_message_unref (message);
1209 d->oom_message = message;
1210 d->oom_preallocated = preallocated;
1216 bus_connection_send_oom_error (DBusConnection *connection,
1217 DBusMessage *in_reply_to)
1219 BusConnectionData *d;
1221 d = BUS_CONNECTION_DATA (connection);
1223 _dbus_assert (d != NULL);
1224 _dbus_assert (d->oom_message != NULL);
1226 /* should always succeed since we set it to a placeholder earlier */
1227 if (!dbus_message_set_reply_serial (d->oom_message,
1228 dbus_message_get_serial (in_reply_to)))
1229 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1231 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1233 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1234 d->oom_message, NULL);
1236 dbus_message_unref (d->oom_message);
1237 d->oom_message = NULL;
1238 d->oom_preallocated = NULL;
1241 #ifdef DBUS_ENABLE_STATS
1243 update_peak (int *peak,
1252 bus_connection_add_match_rule_link (DBusConnection *connection,
1255 BusConnectionData *d;
1257 d = BUS_CONNECTION_DATA (connection);
1258 _dbus_assert (d != NULL);
1260 _dbus_list_append_link (&d->match_rules, link);
1262 d->n_match_rules += 1;
1264 #ifdef DBUS_ENABLE_STATS
1265 update_peak (&d->peak_match_rules, d->n_match_rules);
1266 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1268 d->connections->total_match_rules += 1;
1269 update_peak (&d->connections->peak_match_rules,
1270 d->connections->total_match_rules);
1275 bus_connection_add_match_rule (DBusConnection *connection,
1280 link = _dbus_list_alloc_link (rule);
1285 bus_connection_add_match_rule_link (connection, link);
1291 bus_connection_remove_match_rule (DBusConnection *connection,
1294 BusConnectionData *d;
1296 d = BUS_CONNECTION_DATA (connection);
1297 _dbus_assert (d != NULL);
1299 _dbus_list_remove_last (&d->match_rules, rule);
1301 d->n_match_rules -= 1;
1302 _dbus_assert (d->n_match_rules >= 0);
1304 #ifdef DBUS_ENABLE_STATS
1305 d->connections->total_match_rules -= 1;
1310 bus_connection_get_n_match_rules (DBusConnection *connection)
1312 BusConnectionData *d;
1314 d = BUS_CONNECTION_DATA (connection);
1315 _dbus_assert (d != NULL);
1317 return d->n_match_rules;
1321 bus_connection_add_owned_service_link (DBusConnection *connection,
1324 BusConnectionData *d;
1326 d = BUS_CONNECTION_DATA (connection);
1327 _dbus_assert (d != NULL);
1329 _dbus_list_append_link (&d->services_owned, link);
1331 d->n_services_owned += 1;
1333 #ifdef DBUS_ENABLE_STATS
1334 update_peak (&d->peak_bus_names, d->n_services_owned);
1335 update_peak (&d->connections->peak_bus_names_per_conn,
1336 d->n_services_owned);
1338 d->connections->total_bus_names += 1;
1339 update_peak (&d->connections->peak_bus_names,
1340 d->connections->total_bus_names);
1345 bus_connection_add_owned_service (DBusConnection *connection,
1346 BusService *service)
1350 link = _dbus_list_alloc_link (service);
1355 bus_connection_add_owned_service_link (connection, link);
1361 bus_connection_remove_owned_service (DBusConnection *connection,
1362 BusService *service)
1364 BusConnectionData *d;
1366 d = BUS_CONNECTION_DATA (connection);
1367 _dbus_assert (d != NULL);
1369 _dbus_list_remove_last (&d->services_owned, service);
1371 d->n_services_owned -= 1;
1372 _dbus_assert (d->n_services_owned >= 0);
1374 #ifdef DBUS_ENABLE_STATS
1375 d->connections->total_bus_names -= 1;
1380 bus_connection_get_n_services_owned (DBusConnection *connection)
1382 BusConnectionData *d;
1384 d = BUS_CONNECTION_DATA (connection);
1385 _dbus_assert (d != NULL);
1387 return d->n_services_owned;
1390 #ifdef ENABLE_KDBUS_TRANSPORT
1392 bus_connection_get_services_owned (DBusConnection *connection)
1394 BusConnectionData *d;
1395 d = BUS_CONNECTION_DATA (connection);
1396 _dbus_assert (d != NULL);
1398 return &d->services_owned;
1403 bus_connection_complete (DBusConnection *connection,
1404 const DBusString *name,
1407 BusConnectionData *d;
1410 d = BUS_CONNECTION_DATA (connection);
1411 _dbus_assert (d != NULL);
1412 _dbus_assert (d->name == NULL);
1413 _dbus_assert (d->policy == NULL);
1415 _dbus_assert (!bus_connection_is_active (connection));
1417 if (!_dbus_string_copy_data (name, &d->name))
1419 BUS_SET_OOM (error);
1423 _dbus_assert (d->name != NULL);
1425 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1427 d->policy = bus_context_create_client_policy (d->connections->context,
1431 /* we may have a NULL policy on OOM or error getting list of
1432 * groups for a user. In the latter case we don't handle it so
1433 * well currently, as it will just keep failing over and over.
1436 if (d->policy == NULL)
1438 _dbus_verbose ("Failed to create security policy for connection %p\n",
1440 _DBUS_ASSERT_ERROR_IS_SET (error);
1441 dbus_free (d->name);
1446 if (dbus_connection_get_unix_user (connection, &uid))
1448 if (!adjust_connections_for_uid (d->connections,
1453 /* Create and cache a string which holds information about the
1454 * peer process; used for logging purposes.
1456 if (!cache_peer_loginfo_string (d, connection))
1459 /* Now the connection is active, move it between lists */
1460 _dbus_list_unlink (&d->connections->incomplete,
1461 d->link_in_connection_list);
1462 d->connections->n_incomplete -= 1;
1463 _dbus_list_append_link (&d->connections->completed,
1464 d->link_in_connection_list);
1465 d->connections->n_completed += 1;
1467 _dbus_assert (d->connections->n_incomplete >= 0);
1468 _dbus_assert (d->connections->n_completed > 0);
1470 /* See if we can remove the timeout */
1471 bus_connections_expire_incomplete (d->connections);
1473 _dbus_assert (bus_connection_is_active (connection));
1477 BUS_SET_OOM (error);
1478 dbus_free (d->name);
1481 bus_client_policy_unref (d->policy);
1487 bus_connection_get_name (DBusConnection *connection)
1489 BusConnectionData *d;
1491 d = BUS_CONNECTION_DATA (connection);
1492 _dbus_assert (d != NULL);
1498 * Check whether completing the passed-in connection would
1499 * exceed limits, and if so set error and return #FALSE
1502 bus_connections_check_limits (BusConnections *connections,
1503 DBusConnection *requesting_completion,
1508 if (connections->n_completed >=
1509 bus_context_get_max_completed_connections (connections->context))
1511 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1512 "The maximum number of active connections has been reached");
1516 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1518 if (get_connections_for_uid (connections, uid) >=
1519 bus_context_get_max_connections_per_user (connections->context))
1521 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1522 "The maximum number of active connections for UID %lu has been reached",
1532 bus_pending_reply_free (BusPendingReply *pending)
1534 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1536 pending->will_send_reply,
1537 pending->will_get_reply,
1538 pending->reply_serial);
1540 dbus_free (pending);
1544 bus_pending_reply_send_no_reply (BusConnections *connections,
1545 BusTransaction *transaction,
1546 BusPendingReply *pending)
1548 DBusMessage *message;
1549 DBusMessageIter iter;
1555 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1556 if (message == NULL)
1559 dbus_message_set_no_reply (message, TRUE);
1561 if (!dbus_message_set_reply_serial (message,
1562 pending->reply_serial))
1565 if (!dbus_message_set_error_name (message,
1566 DBUS_ERROR_NO_REPLY))
1569 errmsg = "Message did not receive a reply (timeout by message bus)";
1570 dbus_message_iter_init_append (message, &iter);
1571 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1574 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1581 dbus_message_unref (message);
1586 bus_pending_reply_expired (BusExpireList *list,
1590 BusPendingReply *pending = link->data;
1591 BusConnections *connections = data;
1592 BusTransaction *transaction;
1594 /* No reply is forthcoming. So nuke it if we can. If not,
1595 * leave it in the list to try expiring again later when we
1599 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1601 pending->will_send_reply,
1602 pending->will_get_reply,
1603 pending->reply_serial);
1605 transaction = bus_transaction_new (connections->context);
1606 if (transaction == NULL)
1609 if (!bus_pending_reply_send_no_reply (connections,
1613 bus_transaction_cancel_and_free (transaction);
1617 bus_expire_list_remove_link (connections->pending_replies, link);
1619 bus_pending_reply_free (pending);
1620 bus_transaction_execute_and_free (transaction);
1626 bus_connection_drop_pending_replies (BusConnections *connections,
1627 DBusConnection *connection)
1629 /* The DBusConnection is almost 100% finalized here, so you can't
1630 * do anything with it except check for pointer equality
1634 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1637 link = bus_expire_list_get_first_link (connections->pending_replies);
1638 while (link != NULL)
1641 BusPendingReply *pending;
1643 next = bus_expire_list_get_next_link (connections->pending_replies,
1645 pending = link->data;
1647 if (pending->will_get_reply == connection)
1649 /* We don't need to track this pending reply anymore */
1651 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1653 pending->will_send_reply,
1654 pending->will_get_reply,
1655 pending->reply_serial);
1657 bus_expire_list_remove_link (connections->pending_replies,
1659 bus_pending_reply_free (pending);
1661 else if (pending->will_send_reply == connection)
1663 /* The reply isn't going to be sent, so set things
1664 * up so it will be expired right away
1666 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1668 pending->will_send_reply,
1669 pending->will_get_reply,
1670 pending->reply_serial);
1672 pending->will_send_reply = NULL;
1673 pending->expire_item.added_tv_sec = 0;
1674 pending->expire_item.added_tv_usec = 0;
1676 bus_expire_list_recheck_immediately (connections->pending_replies);
1686 BusPendingReply *pending;
1687 BusConnections *connections;
1688 } CancelPendingReplyData;
1691 cancel_pending_reply (void *data)
1693 CancelPendingReplyData *d = data;
1695 _dbus_verbose ("d = %p\n", d);
1697 if (!bus_expire_list_remove (d->connections->pending_replies,
1698 &d->pending->expire_item))
1699 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1701 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1705 cancel_pending_reply_data_free (void *data)
1707 CancelPendingReplyData *d = data;
1709 _dbus_verbose ("d = %p\n", d);
1711 /* d->pending should be either freed or still
1712 * in the list of pending replies (owned by someone
1720 * Record that a reply is allowed; return TRUE on success.
1723 bus_connections_expect_reply (BusConnections *connections,
1724 BusTransaction *transaction,
1725 DBusConnection *will_get_reply,
1726 DBusConnection *will_send_reply,
1727 DBusMessage *reply_to_this,
1730 BusPendingReply *pending;
1731 dbus_uint32_t reply_serial;
1733 CancelPendingReplyData *cprd;
1736 _dbus_assert (will_get_reply != NULL);
1737 _dbus_assert (will_send_reply != NULL);
1738 _dbus_assert (reply_to_this != NULL);
1740 if (dbus_message_get_no_reply (reply_to_this))
1741 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1743 reply_serial = dbus_message_get_serial (reply_to_this);
1745 link = bus_expire_list_get_first_link (connections->pending_replies);
1747 while (link != NULL)
1749 pending = link->data;
1751 if (pending->reply_serial == reply_serial &&
1752 pending->will_get_reply == will_get_reply &&
1753 pending->will_send_reply == will_send_reply)
1755 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1756 "Message has the same reply serial as a currently-outstanding existing method call");
1760 link = bus_expire_list_get_next_link (connections->pending_replies,
1762 if (pending->will_get_reply == will_get_reply)
1767 bus_context_get_max_replies_per_connection (connections->context))
1769 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1770 "The maximum number of pending replies per connection has been reached");
1774 pending = dbus_new0 (BusPendingReply, 1);
1775 if (pending == NULL)
1777 BUS_SET_OOM (error);
1781 #ifdef DBUS_ENABLE_VERBOSE_MODE
1782 /* so we can see a not-yet-added pending reply */
1783 pending->expire_item.added_tv_sec = 1;
1784 pending->expire_item.added_tv_usec = 1;
1787 pending->will_get_reply = will_get_reply;
1788 pending->will_send_reply = will_send_reply;
1789 pending->reply_serial = reply_serial;
1791 cprd = dbus_new0 (CancelPendingReplyData, 1);
1794 BUS_SET_OOM (error);
1795 bus_pending_reply_free (pending);
1799 if (!bus_expire_list_add (connections->pending_replies,
1800 &pending->expire_item))
1802 BUS_SET_OOM (error);
1804 bus_pending_reply_free (pending);
1808 if (!bus_transaction_add_cancel_hook (transaction,
1809 cancel_pending_reply,
1811 cancel_pending_reply_data_free))
1813 BUS_SET_OOM (error);
1814 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1816 bus_pending_reply_free (pending);
1820 cprd->pending = pending;
1821 cprd->connections = connections;
1823 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1824 &pending->expire_item.added_tv_usec);
1826 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1828 pending->will_send_reply,
1829 pending->will_get_reply,
1830 pending->reply_serial);
1838 BusConnections *connections;
1839 } CheckPendingReplyData;
1842 cancel_check_pending_reply (void *data)
1844 CheckPendingReplyData *d = data;
1846 _dbus_verbose ("d = %p\n",d);
1848 bus_expire_list_add_link (d->connections->pending_replies,
1854 check_pending_reply_data_free (void *data)
1856 CheckPendingReplyData *d = data;
1858 _dbus_verbose ("d = %p\n",d);
1860 if (d->link != NULL)
1862 BusPendingReply *pending = d->link->data;
1864 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1865 &pending->expire_item));
1867 bus_pending_reply_free (pending);
1868 _dbus_list_free_link (d->link);
1875 * Check whether a reply is allowed, remove BusPendingReply
1876 * if so, return TRUE if so.
1879 bus_connections_check_reply (BusConnections *connections,
1880 BusTransaction *transaction,
1881 DBusConnection *sending_reply,
1882 DBusConnection *receiving_reply,
1886 CheckPendingReplyData *cprd;
1888 dbus_uint32_t reply_serial;
1890 _dbus_assert (sending_reply != NULL);
1891 _dbus_assert (receiving_reply != NULL);
1893 reply_serial = dbus_message_get_reply_serial (reply);
1895 link = bus_expire_list_get_first_link (connections->pending_replies);
1896 while (link != NULL)
1898 BusPendingReply *pending = link->data;
1900 if (pending->reply_serial == reply_serial &&
1901 pending->will_get_reply == receiving_reply &&
1902 pending->will_send_reply == sending_reply)
1904 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1908 link = bus_expire_list_get_next_link (connections->pending_replies,
1914 _dbus_verbose ("No pending reply expected\n");
1919 cprd = dbus_new0 (CheckPendingReplyData, 1);
1922 BUS_SET_OOM (error);
1926 if (!bus_transaction_add_cancel_hook (transaction,
1927 cancel_check_pending_reply,
1929 check_pending_reply_data_free))
1931 BUS_SET_OOM (error);
1937 cprd->connections = connections;
1939 bus_expire_list_unlink (connections->pending_replies,
1942 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1950 * Note that this is fairly fragile; in particular, don't try to use
1951 * one transaction across any main loop iterations.
1956 BusTransaction *transaction;
1957 DBusMessage *message;
1958 DBusPreallocatedSend *preallocated;
1963 BusTransactionCancelFunction cancel_function;
1964 DBusFreeFunction free_data_function;
1968 struct BusTransaction
1970 DBusList *connections;
1971 BusContext *context;
1972 DBusList *cancel_hooks;
1976 message_to_send_free (DBusConnection *connection,
1977 MessageToSend *to_send)
1979 if (to_send->message)
1980 dbus_message_unref (to_send->message);
1982 if (to_send->preallocated)
1983 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1985 dbus_free (to_send);
1989 cancel_hook_cancel (void *element,
1992 CancelHook *ch = element;
1994 _dbus_verbose ("Running transaction cancel hook\n");
1996 if (ch->cancel_function)
1997 (* ch->cancel_function) (ch->data);
2001 cancel_hook_free (void *element,
2004 CancelHook *ch = element;
2006 if (ch->free_data_function)
2007 (* ch->free_data_function) (ch->data);
2013 free_cancel_hooks (BusTransaction *transaction)
2015 _dbus_list_foreach (&transaction->cancel_hooks,
2016 cancel_hook_free, NULL);
2018 _dbus_list_clear (&transaction->cancel_hooks);
2022 bus_transaction_new (BusContext *context)
2024 BusTransaction *transaction;
2026 transaction = dbus_new0 (BusTransaction, 1);
2027 if (transaction == NULL)
2030 transaction->context = context;
2036 bus_transaction_get_context (BusTransaction *transaction)
2038 return transaction->context;
2042 bus_transaction_get_connections (BusTransaction *transaction)
2044 return bus_context_get_connections (transaction->context);
2048 bus_transaction_send_from_driver (BusTransaction *transaction,
2049 DBusConnection *connection,
2050 DBusMessage *message)
2052 /* We have to set the sender to the driver, and have
2053 * to check security policy since it was not done in
2056 _dbus_verbose ("Sending %s %s %s from driver\n",
2057 dbus_message_get_interface (message) ?
2058 dbus_message_get_interface (message) : "(no interface)",
2059 dbus_message_get_member (message) ?
2060 dbus_message_get_member (message) : "(no member)",
2061 dbus_message_get_error_name (message) ?
2062 dbus_message_get_error_name (message) : "(no error name)");
2064 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2067 #ifdef ENABLE_KDBUS_TRANSPORT
2068 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) /* we can't set destination on the basis of connection when on kdbus*/
2070 if (bus_connection_is_active (connection))
2072 if (!dbus_message_set_destination (message,
2073 bus_connection_get_name (connection)))
2077 /* bus driver never wants a reply */
2078 dbus_message_set_no_reply (message, TRUE);
2080 /* If security policy doesn't allow the message, we silently
2081 * eat it; the driver doesn't care about getting a reply.
2083 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2085 NULL, connection, connection, message, NULL))
2088 return bus_transaction_send (transaction, connection, message);
2092 bus_transaction_send (BusTransaction *transaction,
2093 DBusConnection *connection,
2094 DBusMessage *message)
2096 MessageToSend *to_send;
2097 BusConnectionData *d;
2100 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2101 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2102 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2104 dbus_message_get_interface (message) ?
2105 dbus_message_get_interface (message) : "(unset)",
2106 dbus_message_get_member (message) ?
2107 dbus_message_get_member (message) : "(unset)",
2108 dbus_message_get_error_name (message) ?
2109 dbus_message_get_error_name (message) : "(unset)",
2110 dbus_connection_get_is_connected (connection) ?
2111 "" : " (disconnected)");
2113 _dbus_assert (dbus_message_get_sender (message) != NULL);
2115 if (!dbus_connection_get_is_connected (connection))
2116 return TRUE; /* silently ignore disconnected connections */
2118 d = BUS_CONNECTION_DATA (connection);
2119 _dbus_assert (d != NULL);
2121 to_send = dbus_new (MessageToSend, 1);
2122 if (to_send == NULL)
2127 to_send->preallocated = dbus_connection_preallocate_send (connection);
2128 if (to_send->preallocated == NULL)
2130 dbus_free (to_send);
2134 dbus_message_ref (message);
2135 to_send->message = message;
2136 to_send->transaction = transaction;
2138 _dbus_verbose ("about to prepend message\n");
2140 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2142 message_to_send_free (connection, to_send);
2146 _dbus_verbose ("prepended message\n");
2148 /* See if we already had this connection in the list
2149 * for this transaction. If we have a pending message,
2150 * then we should already be in transaction->connections
2152 link = _dbus_list_get_first_link (&d->transaction_messages);
2153 _dbus_assert (link->data == to_send);
2154 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2155 while (link != NULL)
2157 MessageToSend *m = link->data;
2158 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2160 if (m->transaction == transaction)
2168 if (!_dbus_list_prepend (&transaction->connections, connection))
2170 _dbus_list_remove (&d->transaction_messages, to_send);
2171 message_to_send_free (connection, to_send);
2180 connection_cancel_transaction (DBusConnection *connection,
2181 BusTransaction *transaction)
2184 BusConnectionData *d;
2186 d = BUS_CONNECTION_DATA (connection);
2187 _dbus_assert (d != NULL);
2189 link = _dbus_list_get_first_link (&d->transaction_messages);
2190 while (link != NULL)
2192 MessageToSend *m = link->data;
2193 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2195 if (m->transaction == transaction)
2197 _dbus_list_remove_link (&d->transaction_messages,
2200 message_to_send_free (connection, m);
2208 bus_transaction_cancel_and_free (BusTransaction *transaction)
2210 DBusConnection *connection;
2212 _dbus_verbose ("TRANSACTION: cancelled\n");
2214 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2215 connection_cancel_transaction (connection, transaction);
2217 _dbus_assert (transaction->connections == NULL);
2219 _dbus_list_foreach (&transaction->cancel_hooks,
2220 cancel_hook_cancel, NULL);
2222 free_cancel_hooks (transaction);
2224 dbus_free (transaction);
2228 connection_execute_transaction (DBusConnection *connection,
2229 BusTransaction *transaction)
2232 BusConnectionData *d;
2234 d = BUS_CONNECTION_DATA (connection);
2235 _dbus_assert (d != NULL);
2237 /* Send the queue in order (FIFO) */
2238 link = _dbus_list_get_last_link (&d->transaction_messages);
2239 while (link != NULL)
2241 MessageToSend *m = link->data;
2242 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2244 if (m->transaction == transaction)
2246 _dbus_list_remove_link (&d->transaction_messages,
2249 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2251 dbus_connection_send_preallocated (connection,
2256 m->preallocated = NULL; /* so we don't double-free it */
2258 message_to_send_free (connection, m);
2266 bus_transaction_execute_and_free (BusTransaction *transaction)
2268 /* For each connection in transaction->connections
2271 DBusConnection *connection;
2273 _dbus_verbose ("TRANSACTION: executing\n");
2275 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2276 connection_execute_transaction (connection, transaction);
2278 _dbus_assert (transaction->connections == NULL);
2280 free_cancel_hooks (transaction);
2282 dbus_free (transaction);
2286 bus_connection_remove_transactions (DBusConnection *connection)
2288 MessageToSend *to_send;
2289 BusConnectionData *d;
2291 d = BUS_CONNECTION_DATA (connection);
2292 _dbus_assert (d != NULL);
2294 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2296 /* only has an effect for the first MessageToSend listing this transaction */
2297 _dbus_list_remove (&to_send->transaction->connections,
2300 _dbus_list_remove (&d->transaction_messages, to_send);
2301 message_to_send_free (connection, to_send);
2306 * Converts the DBusError to a message reply
2309 bus_transaction_send_error_reply (BusTransaction *transaction,
2310 DBusConnection *connection,
2311 const DBusError *error,
2312 DBusMessage *in_reply_to)
2316 _dbus_assert (error != NULL);
2317 _DBUS_ASSERT_ERROR_IS_SET (error);
2319 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2320 error->name, error->message);
2322 reply = dbus_message_new_error (in_reply_to,
2328 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2330 dbus_message_unref (reply);
2334 dbus_message_unref (reply);
2340 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2341 BusTransactionCancelFunction cancel_function,
2343 DBusFreeFunction free_data_function)
2347 ch = dbus_new (CancelHook, 1);
2351 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2352 cancel_function, data);
2354 ch->cancel_function = cancel_function;
2356 ch->free_data_function = free_data_function;
2358 /* It's important that the hooks get run in reverse order that they
2361 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2370 #ifdef DBUS_ENABLE_STATS
2372 bus_connections_get_n_active (BusConnections *connections)
2374 return connections->n_completed;
2378 bus_connections_get_n_incomplete (BusConnections *connections)
2380 return connections->n_incomplete;
2384 bus_connections_get_total_match_rules (BusConnections *connections)
2386 return connections->total_match_rules;
2390 bus_connections_get_peak_match_rules (BusConnections *connections)
2392 return connections->peak_match_rules;
2396 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2398 return connections->peak_match_rules_per_conn;
2402 bus_connections_get_total_bus_names (BusConnections *connections)
2404 return connections->total_bus_names;
2408 bus_connections_get_peak_bus_names (BusConnections *connections)
2410 return connections->peak_bus_names;
2414 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2416 return connections->peak_bus_names_per_conn;
2420 bus_connection_get_peak_match_rules (DBusConnection *connection)
2422 BusConnectionData *d;
2424 d = BUS_CONNECTION_DATA (connection);
2425 return d->peak_match_rules;
2429 bus_connection_get_peak_bus_names (DBusConnection *connection)
2431 BusConnectionData *d;
2433 d = BUS_CONNECTION_DATA (connection);
2434 return d->peak_bus_names;
2436 #endif /* DBUS_ENABLE_STATS */