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;
194 d = BUS_CONNECTION_DATA (connection);
195 _dbus_assert (d != NULL);
197 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
198 d->name ? d->name : "(inactive)");
200 /* Delete our match rules */
201 if (d->n_match_rules > 0)
203 matchmaker = bus_context_get_matchmaker (d->connections->context);
204 bus_matchmaker_disconnected (matchmaker, connection);
207 /* Drop any service ownership. Unfortunately, this requires
208 * memory allocation and there doesn't seem to be a good way to
209 * handle it other than sleeping; we can't "fail" the operation of
210 * disconnecting a client, and preallocating a broadcast "service is
211 * now gone" message for every client-service pair seems kind of
214 while ((service = _dbus_list_get_last (&d->services_owned)))
216 BusTransaction *transaction;
221 dbus_error_init (&error);
223 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
224 _dbus_wait_for_memory ();
226 if (!bus_service_remove_owner (service, connection,
227 transaction, &error))
229 _DBUS_ASSERT_ERROR_IS_SET (&error);
231 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
233 dbus_error_free (&error);
234 bus_transaction_cancel_and_free (transaction);
235 _dbus_wait_for_memory ();
240 _dbus_verbose ("Failed to remove service owner: %s %s\n",
241 error.name, error.message);
242 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
246 bus_transaction_execute_and_free (transaction);
249 bus_dispatch_remove_connection (connection);
251 /* no more watching */
252 if (!dbus_connection_set_watch_functions (connection,
256 _dbus_assert_not_reached ("setting watch functions to NULL failed");
258 if (!dbus_connection_set_timeout_functions (connection,
262 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
264 dbus_connection_set_unix_user_function (connection,
266 dbus_connection_set_windows_user_function (connection,
269 dbus_connection_set_dispatch_status_function (connection,
272 bus_connection_remove_transactions (connection);
274 if (d->link_in_connection_list != NULL)
280 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
281 d->link_in_connection_list = NULL;
282 d->connections->n_completed -= 1;
284 if (dbus_connection_get_unix_user (connection, &uid))
286 if (!adjust_connections_for_uid (d->connections,
288 _dbus_assert_not_reached ("adjusting downward should never fail");
293 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
294 d->link_in_connection_list = NULL;
295 d->connections->n_incomplete -= 1;
298 _dbus_assert (d->connections->n_incomplete >= 0);
299 _dbus_assert (d->connections->n_completed >= 0);
302 bus_connection_drop_pending_replies (d->connections, connection);
304 /* frees "d" as side effect */
305 dbus_connection_set_data (connection,
306 connection_data_slot,
309 dbus_connection_unref (connection);
313 add_connection_watch (DBusWatch *watch,
316 DBusConnection *connection = data;
318 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
322 remove_connection_watch (DBusWatch *watch,
325 DBusConnection *connection = data;
327 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
331 toggle_connection_watch (DBusWatch *watch,
334 DBusConnection *connection = data;
336 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
340 add_connection_timeout (DBusTimeout *timeout,
343 DBusConnection *connection = data;
345 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
349 remove_connection_timeout (DBusTimeout *timeout,
352 DBusConnection *connection = data;
354 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
358 dispatch_status_function (DBusConnection *connection,
359 DBusDispatchStatus new_status,
362 DBusLoop *loop = data;
364 if (new_status != DBUS_DISPATCH_COMPLETE)
366 while (!_dbus_loop_queue_dispatch (loop, connection))
367 _dbus_wait_for_memory ();
372 allow_unix_user_function (DBusConnection *connection,
376 BusConnectionData *d;
378 d = BUS_CONNECTION_DATA (connection);
380 _dbus_assert (d != NULL);
382 return bus_context_allow_unix_user (d->connections->context, uid);
386 free_connection_data (void *data)
388 BusConnectionData *d = data;
390 /* services_owned should be NULL since we should be disconnected */
391 _dbus_assert (d->services_owned == NULL);
392 _dbus_assert (d->n_services_owned == 0);
394 _dbus_assert (d->transaction_messages == NULL);
396 if (d->oom_preallocated)
397 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
400 dbus_message_unref (d->oom_message);
403 bus_client_policy_unref (d->policy);
406 bus_selinux_id_unref (d->selinux_id);
408 dbus_free (d->cached_loginfo_string);
416 bus_connections_new (BusContext *context)
418 BusConnections *connections;
420 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
423 connections = dbus_new0 (BusConnections, 1);
424 if (connections == NULL)
427 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
429 if (connections->completed_by_user == NULL)
432 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
433 expire_incomplete_timeout,
435 if (connections->expire_timeout == NULL)
438 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
440 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
441 bus_context_get_reply_timeout (context),
442 bus_pending_reply_expired,
444 if (connections->pending_replies == NULL)
447 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
448 connections->expire_timeout))
451 connections->refcount = 1;
452 connections->context = context;
457 bus_expire_list_free (connections->pending_replies);
459 _dbus_timeout_unref (connections->expire_timeout);
461 _dbus_hash_table_unref (connections->completed_by_user);
463 dbus_free (connections);
465 dbus_connection_free_data_slot (&connection_data_slot);
471 bus_connections_ref (BusConnections *connections)
473 _dbus_assert (connections->refcount > 0);
474 connections->refcount += 1;
480 bus_connections_unref (BusConnections *connections)
482 _dbus_assert (connections->refcount > 0);
483 connections->refcount -= 1;
484 if (connections->refcount == 0)
486 /* drop all incomplete */
487 while (connections->incomplete != NULL)
489 DBusConnection *connection;
491 connection = connections->incomplete->data;
493 dbus_connection_ref (connection);
494 dbus_connection_close (connection);
495 bus_connection_disconnected (connection);
496 dbus_connection_unref (connection);
499 _dbus_assert (connections->n_incomplete == 0);
501 /* drop all real connections */
502 while (connections->completed != NULL)
504 DBusConnection *connection;
506 connection = connections->completed->data;
508 dbus_connection_ref (connection);
509 dbus_connection_close (connection);
510 bus_connection_disconnected (connection);
511 dbus_connection_unref (connection);
514 _dbus_assert (connections->n_completed == 0);
516 bus_expire_list_free (connections->pending_replies);
518 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
519 connections->expire_timeout);
521 _dbus_timeout_unref (connections->expire_timeout);
523 _dbus_hash_table_unref (connections->completed_by_user);
525 dbus_free (connections);
527 dbus_connection_free_data_slot (&connection_data_slot);
531 /* Used for logging */
533 cache_peer_loginfo_string (BusConnectionData *d,
534 DBusConnection *connection)
536 DBusString loginfo_buf;
540 dbus_bool_t prev_added;
542 if (!_dbus_string_init (&loginfo_buf))
546 if (dbus_connection_get_unix_user (connection, &uid))
548 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
554 if (dbus_connection_get_unix_process_id (connection, &pid))
558 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
561 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
563 /* Ignore errors here; we may not have permissions to read the
565 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
566 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
570 if (dbus_connection_get_windows_user (connection, &windows_sid))
572 dbus_bool_t did_append;
573 did_append = _dbus_string_append_printf (&loginfo_buf,
574 "sid=\"%s\" ", windows_sid);
575 dbus_free (windows_sid);
580 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
583 _dbus_string_free (&loginfo_buf);
587 _dbus_string_free (&loginfo_buf);
592 bus_connections_setup_connection (BusConnections *connections,
593 DBusConnection *connection)
596 BusConnectionData *d;
601 d = dbus_new0 (BusConnectionData, 1);
606 d->connections = connections;
607 d->connection = connection;
609 _dbus_get_monotonic_time (&d->connection_tv_sec,
610 &d->connection_tv_usec);
612 _dbus_assert (connection_data_slot >= 0);
614 if (!dbus_connection_set_data (connection,
615 connection_data_slot,
616 d, free_connection_data))
622 dbus_connection_set_route_peer_messages (connection, TRUE);
626 dbus_error_init (&error);
627 d->selinux_id = bus_selinux_init_connection_id (connection,
629 if (dbus_error_is_set (&error))
631 /* This is a bit bogus because we pretend all errors
632 * are OOM; this is done because we know that in bus.c
633 * an OOM error disconnects the connection, which is
634 * the same thing we want on any other error.
636 dbus_error_free (&error);
640 if (!dbus_connection_set_watch_functions (connection,
641 add_connection_watch,
642 remove_connection_watch,
643 toggle_connection_watch,
648 if (!dbus_connection_set_timeout_functions (connection,
649 add_connection_timeout,
650 remove_connection_timeout,
655 /* For now we don't need to set a Windows user function because
656 * there are no policies in the config file controlling what
657 * Windows users can connect. The default 'same user that owns the
658 * bus can connect' behavior of DBusConnection is fine on Windows.
660 dbus_connection_set_unix_user_function (connection,
661 allow_unix_user_function,
664 dbus_connection_set_dispatch_status_function (connection,
665 dispatch_status_function,
666 bus_context_get_loop (connections->context),
669 d->link_in_connection_list = _dbus_list_alloc_link (connection);
670 if (d->link_in_connection_list == NULL)
673 /* Setup the connection with the dispatcher */
674 if (!bus_dispatch_add_connection (connection))
677 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
679 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
681 bus_dispatch_remove_connection (connection);
686 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
687 connections->n_incomplete += 1;
689 dbus_connection_ref (connection);
691 /* Note that we might disconnect ourselves here, but it only takes
692 * effect on return to the main loop. We call this to free up
693 * expired connections if possible, and to queue the timeout for our
696 bus_connections_expire_incomplete (connections);
698 /* And we might also disconnect ourselves here, but again it
699 * only takes effect on return to main loop.
701 if (connections->n_incomplete >
702 bus_context_get_max_incomplete_connections (connections->context))
704 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
706 _dbus_assert (connections->incomplete != NULL);
707 /* Disconnect the oldest unauthenticated connection. FIXME
708 * would it be more secure to drop a *random* connection? This
709 * algorithm seems to mean that if someone can create new
710 * connections quickly enough, they can keep anyone else from
711 * completing authentication. But random may or may not really
712 * help with that, a more elaborate solution might be required.
714 dbus_connection_close (connections->incomplete->data);
723 bus_selinux_id_unref (d->selinux_id);
724 d->selinux_id = NULL;
726 if (!dbus_connection_set_watch_functions (connection,
730 _dbus_assert_not_reached ("setting watch functions to NULL failed");
732 if (!dbus_connection_set_timeout_functions (connection,
736 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
738 dbus_connection_set_unix_user_function (connection,
741 dbus_connection_set_windows_user_function (connection,
744 dbus_connection_set_dispatch_status_function (connection,
747 if (d->link_in_connection_list != NULL)
749 _dbus_assert (d->link_in_connection_list->next == NULL);
750 _dbus_assert (d->link_in_connection_list->prev == NULL);
751 _dbus_list_free_link (d->link_in_connection_list);
752 d->link_in_connection_list = NULL;
755 if (!dbus_connection_set_data (connection,
756 connection_data_slot,
758 _dbus_assert_not_reached ("failed to set connection data to null");
760 /* "d" has now been freed */
767 bus_connections_expire_incomplete (BusConnections *connections)
773 if (connections->incomplete != NULL)
775 long tv_sec, tv_usec;
779 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
780 auth_timeout = bus_context_get_auth_timeout (connections->context);
782 link = _dbus_list_get_first_link (&connections->incomplete);
785 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
786 DBusConnection *connection;
787 BusConnectionData *d;
790 connection = link->data;
792 d = BUS_CONNECTION_DATA (connection);
794 _dbus_assert (d != NULL);
796 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
797 d->connection_tv_usec,
800 if (elapsed >= (double) auth_timeout)
802 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
803 dbus_connection_close (connection);
807 /* We can end the loop, since the connections are in oldest-first order */
808 next_interval = ((double)auth_timeout) - elapsed;
809 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
810 connection, next_interval);
819 bus_expire_timeout_set_interval (connections->expire_timeout,
824 expire_incomplete_timeout (void *data)
826 BusConnections *connections = data;
828 _dbus_verbose ("Running\n");
830 /* note that this may remove the timeout */
831 bus_connections_expire_incomplete (connections);
837 bus_connection_get_unix_groups (DBusConnection *connection,
838 unsigned long **groups,
847 if (dbus_connection_get_unix_user (connection, &uid))
849 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
851 _dbus_verbose ("Did not get any groups for UID %lu\n",
857 _dbus_verbose ("Got %d groups for UID %lu\n",
863 return TRUE; /* successfully got 0 groups */
867 bus_connection_is_in_unix_group (DBusConnection *connection,
871 unsigned long *group_ids;
874 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
879 while (i < n_group_ids)
881 if (group_ids[i] == gid)
883 dbus_free (group_ids);
889 dbus_free (group_ids);
894 bus_connection_get_loginfo (DBusConnection *connection)
896 BusConnectionData *d;
898 d = BUS_CONNECTION_DATA (connection);
900 if (!bus_connection_is_active (connection))
902 return d->cached_loginfo_string;
906 bus_connection_get_policy (DBusConnection *connection)
908 BusConnectionData *d;
910 d = BUS_CONNECTION_DATA (connection);
912 _dbus_assert (d != NULL);
913 _dbus_assert (d->policy != NULL);
919 foreach_active (BusConnections *connections,
920 BusConnectionForeachFunction function,
925 link = _dbus_list_get_first_link (&connections->completed);
928 DBusConnection *connection = link->data;
929 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
931 if (!(* function) (connection, data))
941 foreach_inactive (BusConnections *connections,
942 BusConnectionForeachFunction function,
947 link = _dbus_list_get_first_link (&connections->incomplete);
950 DBusConnection *connection = link->data;
951 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
953 if (!(* function) (connection, data))
963 * Calls function on each active connection; if the function returns
964 * #FALSE, stops iterating. Active connections are authenticated
965 * and have sent a Hello message.
967 * @param connections the connections object
968 * @param function the function
969 * @param data data to pass to it as a second arg
972 bus_connections_foreach_active (BusConnections *connections,
973 BusConnectionForeachFunction function,
976 foreach_active (connections, function, data);
980 * Calls function on each connection; if the function returns
981 * #FALSE, stops iterating.
983 * @param connections the connections object
984 * @param function the function
985 * @param data data to pass to it as a second arg
988 bus_connections_foreach (BusConnections *connections,
989 BusConnectionForeachFunction function,
992 if (!foreach_active (connections, function, data))
995 foreach_inactive (connections, function, data);
999 bus_connections_get_context (BusConnections *connections)
1001 return connections->context;
1005 * This is used to avoid covering the same connection twice when
1006 * traversing connections. Note that it assumes we will
1007 * bus_connection_mark_stamp() each connection at least once per
1008 * INT_MAX increments of the global stamp, or wraparound would break
1012 bus_connections_increment_stamp (BusConnections *connections)
1014 connections->stamp += 1;
1017 /* Mark connection with current stamp, return TRUE if it
1018 * didn't already have that stamp
1021 bus_connection_mark_stamp (DBusConnection *connection)
1023 BusConnectionData *d;
1025 d = BUS_CONNECTION_DATA (connection);
1027 _dbus_assert (d != NULL);
1029 if (d->stamp == d->connections->stamp)
1033 d->stamp = d->connections->stamp;
1039 bus_connection_get_context (DBusConnection *connection)
1041 BusConnectionData *d;
1043 d = BUS_CONNECTION_DATA (connection);
1045 _dbus_assert (d != NULL);
1047 return d->connections->context;
1051 bus_connection_get_connections (DBusConnection *connection)
1053 BusConnectionData *d;
1055 d = BUS_CONNECTION_DATA (connection);
1057 _dbus_assert (d != NULL);
1059 return d->connections;
1063 bus_connection_get_registry (DBusConnection *connection)
1065 BusConnectionData *d;
1067 d = BUS_CONNECTION_DATA (connection);
1069 _dbus_assert (d != NULL);
1071 return bus_context_get_registry (d->connections->context);
1075 bus_connection_get_activation (DBusConnection *connection)
1077 BusConnectionData *d;
1079 d = BUS_CONNECTION_DATA (connection);
1081 _dbus_assert (d != NULL);
1083 return bus_context_get_activation (d->connections->context);
1087 bus_connection_get_matchmaker (DBusConnection *connection)
1089 BusConnectionData *d;
1091 d = BUS_CONNECTION_DATA (connection);
1093 _dbus_assert (d != NULL);
1095 return bus_context_get_matchmaker (d->connections->context);
1099 bus_connection_get_selinux_id (DBusConnection *connection)
1101 BusConnectionData *d;
1103 d = BUS_CONNECTION_DATA (connection);
1105 _dbus_assert (d != NULL);
1107 return d->selinux_id;
1111 * Checks whether the connection is registered with the message bus.
1113 * @param connection the connection
1114 * @returns #TRUE if we're an active message bus participant
1117 bus_connection_is_active (DBusConnection *connection)
1119 BusConnectionData *d;
1121 d = BUS_CONNECTION_DATA (connection);
1123 return d != NULL && d->name != NULL;
1127 bus_connection_preallocate_oom_error (DBusConnection *connection)
1129 DBusMessage *message;
1130 DBusPreallocatedSend *preallocated;
1131 BusConnectionData *d;
1133 d = BUS_CONNECTION_DATA (connection);
1135 _dbus_assert (d != NULL);
1137 if (d->oom_preallocated != NULL)
1140 preallocated = dbus_connection_preallocate_send (connection);
1141 if (preallocated == NULL)
1144 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1146 if (message == NULL)
1148 dbus_connection_free_preallocated_send (connection, preallocated);
1152 /* d->name may be NULL, but that is OK */
1153 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1154 !dbus_message_set_destination (message, d->name) ||
1155 !dbus_message_set_sender (message,
1158 dbus_connection_free_preallocated_send (connection, preallocated);
1159 dbus_message_unref (message);
1163 /* set reply serial to placeholder value just so space is already allocated
1166 if (!dbus_message_set_reply_serial (message, 14))
1168 dbus_connection_free_preallocated_send (connection, preallocated);
1169 dbus_message_unref (message);
1173 d->oom_message = message;
1174 d->oom_preallocated = preallocated;
1180 bus_connection_send_oom_error (DBusConnection *connection,
1181 DBusMessage *in_reply_to)
1183 BusConnectionData *d;
1185 d = BUS_CONNECTION_DATA (connection);
1187 _dbus_assert (d != NULL);
1188 _dbus_assert (d->oom_message != NULL);
1190 /* should always succeed since we set it to a placeholder earlier */
1191 if (!dbus_message_set_reply_serial (d->oom_message,
1192 dbus_message_get_serial (in_reply_to)))
1193 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1195 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1197 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1198 d->oom_message, NULL);
1200 dbus_message_unref (d->oom_message);
1201 d->oom_message = NULL;
1202 d->oom_preallocated = NULL;
1205 #ifdef DBUS_ENABLE_STATS
1207 update_peak (int *peak,
1216 bus_connection_add_match_rule_link (DBusConnection *connection,
1219 BusConnectionData *d;
1221 d = BUS_CONNECTION_DATA (connection);
1222 _dbus_assert (d != NULL);
1224 _dbus_list_append_link (&d->match_rules, link);
1226 d->n_match_rules += 1;
1228 #ifdef DBUS_ENABLE_STATS
1229 update_peak (&d->peak_match_rules, d->n_match_rules);
1230 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1232 d->connections->total_match_rules += 1;
1233 update_peak (&d->connections->peak_match_rules,
1234 d->connections->total_match_rules);
1239 bus_connection_add_match_rule (DBusConnection *connection,
1244 link = _dbus_list_alloc_link (rule);
1249 bus_connection_add_match_rule_link (connection, link);
1255 bus_connection_remove_match_rule (DBusConnection *connection,
1258 BusConnectionData *d;
1260 d = BUS_CONNECTION_DATA (connection);
1261 _dbus_assert (d != NULL);
1263 _dbus_list_remove_last (&d->match_rules, rule);
1265 d->n_match_rules -= 1;
1266 _dbus_assert (d->n_match_rules >= 0);
1268 #ifdef DBUS_ENABLE_STATS
1269 d->connections->total_match_rules -= 1;
1274 bus_connection_get_n_match_rules (DBusConnection *connection)
1276 BusConnectionData *d;
1278 d = BUS_CONNECTION_DATA (connection);
1279 _dbus_assert (d != NULL);
1281 return d->n_match_rules;
1285 bus_connection_add_owned_service_link (DBusConnection *connection,
1288 BusConnectionData *d;
1290 d = BUS_CONNECTION_DATA (connection);
1291 _dbus_assert (d != NULL);
1293 _dbus_list_append_link (&d->services_owned, link);
1295 d->n_services_owned += 1;
1297 #ifdef DBUS_ENABLE_STATS
1298 update_peak (&d->peak_bus_names, d->n_services_owned);
1299 update_peak (&d->connections->peak_bus_names_per_conn,
1300 d->n_services_owned);
1302 d->connections->total_bus_names += 1;
1303 update_peak (&d->connections->peak_bus_names,
1304 d->connections->total_bus_names);
1309 bus_connection_add_owned_service (DBusConnection *connection,
1310 BusService *service)
1314 link = _dbus_list_alloc_link (service);
1319 bus_connection_add_owned_service_link (connection, link);
1325 bus_connection_remove_owned_service (DBusConnection *connection,
1326 BusService *service)
1328 BusConnectionData *d;
1330 d = BUS_CONNECTION_DATA (connection);
1331 _dbus_assert (d != NULL);
1333 _dbus_list_remove_last (&d->services_owned, service);
1335 d->n_services_owned -= 1;
1336 _dbus_assert (d->n_services_owned >= 0);
1338 #ifdef DBUS_ENABLE_STATS
1339 d->connections->total_bus_names -= 1;
1344 bus_connection_get_n_services_owned (DBusConnection *connection)
1346 BusConnectionData *d;
1348 d = BUS_CONNECTION_DATA (connection);
1349 _dbus_assert (d != NULL);
1351 return d->n_services_owned;
1355 bus_connection_complete (DBusConnection *connection,
1356 const DBusString *name,
1359 BusConnectionData *d;
1362 d = BUS_CONNECTION_DATA (connection);
1363 _dbus_assert (d != NULL);
1364 _dbus_assert (d->name == NULL);
1365 _dbus_assert (d->policy == NULL);
1367 _dbus_assert (!bus_connection_is_active (connection));
1369 if (!_dbus_string_copy_data (name, &d->name))
1371 BUS_SET_OOM (error);
1375 _dbus_assert (d->name != NULL);
1377 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1379 d->policy = bus_context_create_client_policy (d->connections->context,
1383 /* we may have a NULL policy on OOM or error getting list of
1384 * groups for a user. In the latter case we don't handle it so
1385 * well currently, as it will just keep failing over and over.
1388 if (d->policy == NULL)
1390 _dbus_verbose ("Failed to create security policy for connection %p\n",
1392 _DBUS_ASSERT_ERROR_IS_SET (error);
1393 dbus_free (d->name);
1398 if (dbus_connection_get_unix_user (connection, &uid))
1400 if (!adjust_connections_for_uid (d->connections,
1405 /* Create and cache a string which holds information about the
1406 * peer process; used for logging purposes.
1408 if (!cache_peer_loginfo_string (d, connection))
1411 /* Now the connection is active, move it between lists */
1412 _dbus_list_unlink (&d->connections->incomplete,
1413 d->link_in_connection_list);
1414 d->connections->n_incomplete -= 1;
1415 _dbus_list_append_link (&d->connections->completed,
1416 d->link_in_connection_list);
1417 d->connections->n_completed += 1;
1419 _dbus_assert (d->connections->n_incomplete >= 0);
1420 _dbus_assert (d->connections->n_completed > 0);
1422 /* See if we can remove the timeout */
1423 bus_connections_expire_incomplete (d->connections);
1425 _dbus_assert (bus_connection_is_active (connection));
1429 BUS_SET_OOM (error);
1430 dbus_free (d->name);
1433 bus_client_policy_unref (d->policy);
1439 bus_connection_get_name (DBusConnection *connection)
1441 BusConnectionData *d;
1443 d = BUS_CONNECTION_DATA (connection);
1444 _dbus_assert (d != NULL);
1450 * Check whether completing the passed-in connection would
1451 * exceed limits, and if so set error and return #FALSE
1454 bus_connections_check_limits (BusConnections *connections,
1455 DBusConnection *requesting_completion,
1460 if (connections->n_completed >=
1461 bus_context_get_max_completed_connections (connections->context))
1463 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1464 "The maximum number of active connections has been reached");
1468 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1470 if (get_connections_for_uid (connections, uid) >=
1471 bus_context_get_max_connections_per_user (connections->context))
1473 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1474 "The maximum number of active connections for UID %lu has been reached",
1484 bus_pending_reply_free (BusPendingReply *pending)
1486 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1488 pending->will_send_reply,
1489 pending->will_get_reply,
1490 pending->reply_serial);
1492 dbus_free (pending);
1496 bus_pending_reply_send_no_reply (BusConnections *connections,
1497 BusTransaction *transaction,
1498 BusPendingReply *pending)
1500 DBusMessage *message;
1501 DBusMessageIter iter;
1507 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1508 if (message == NULL)
1511 dbus_message_set_no_reply (message, TRUE);
1513 if (!dbus_message_set_reply_serial (message,
1514 pending->reply_serial))
1517 if (!dbus_message_set_error_name (message,
1518 DBUS_ERROR_NO_REPLY))
1521 errmsg = "Message did not receive a reply (timeout by message bus)";
1522 dbus_message_iter_init_append (message, &iter);
1523 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1526 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1533 dbus_message_unref (message);
1538 bus_pending_reply_expired (BusExpireList *list,
1542 BusPendingReply *pending = link->data;
1543 BusConnections *connections = data;
1544 BusTransaction *transaction;
1546 /* No reply is forthcoming. So nuke it if we can. If not,
1547 * leave it in the list to try expiring again later when we
1551 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1553 pending->will_send_reply,
1554 pending->will_get_reply,
1555 pending->reply_serial);
1557 transaction = bus_transaction_new (connections->context);
1558 if (transaction == NULL)
1561 if (!bus_pending_reply_send_no_reply (connections,
1565 bus_transaction_cancel_and_free (transaction);
1569 bus_expire_list_remove_link (connections->pending_replies, link);
1571 bus_pending_reply_free (pending);
1572 bus_transaction_execute_and_free (transaction);
1578 bus_connection_drop_pending_replies (BusConnections *connections,
1579 DBusConnection *connection)
1581 /* The DBusConnection is almost 100% finalized here, so you can't
1582 * do anything with it except check for pointer equality
1586 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1589 link = bus_expire_list_get_first_link (connections->pending_replies);
1590 while (link != NULL)
1593 BusPendingReply *pending;
1595 next = bus_expire_list_get_next_link (connections->pending_replies,
1597 pending = link->data;
1599 if (pending->will_get_reply == connection)
1601 /* We don't need to track this pending reply anymore */
1603 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1605 pending->will_send_reply,
1606 pending->will_get_reply,
1607 pending->reply_serial);
1609 bus_expire_list_remove_link (connections->pending_replies,
1611 bus_pending_reply_free (pending);
1613 else if (pending->will_send_reply == connection)
1615 /* The reply isn't going to be sent, so set things
1616 * up so it will be expired right away
1618 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1620 pending->will_send_reply,
1621 pending->will_get_reply,
1622 pending->reply_serial);
1624 pending->will_send_reply = NULL;
1625 pending->expire_item.added_tv_sec = 0;
1626 pending->expire_item.added_tv_usec = 0;
1628 bus_expire_list_recheck_immediately (connections->pending_replies);
1638 BusPendingReply *pending;
1639 BusConnections *connections;
1640 } CancelPendingReplyData;
1643 cancel_pending_reply (void *data)
1645 CancelPendingReplyData *d = data;
1647 _dbus_verbose ("d = %p\n", d);
1649 if (!bus_expire_list_remove (d->connections->pending_replies,
1650 &d->pending->expire_item))
1651 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1653 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1657 cancel_pending_reply_data_free (void *data)
1659 CancelPendingReplyData *d = data;
1661 _dbus_verbose ("d = %p\n", d);
1663 /* d->pending should be either freed or still
1664 * in the list of pending replies (owned by someone
1672 * Record that a reply is allowed; return TRUE on success.
1675 bus_connections_expect_reply (BusConnections *connections,
1676 BusTransaction *transaction,
1677 DBusConnection *will_get_reply,
1678 DBusConnection *will_send_reply,
1679 DBusMessage *reply_to_this,
1682 BusPendingReply *pending;
1683 dbus_uint32_t reply_serial;
1685 CancelPendingReplyData *cprd;
1688 _dbus_assert (will_get_reply != NULL);
1689 _dbus_assert (will_send_reply != NULL);
1690 _dbus_assert (reply_to_this != NULL);
1692 if (dbus_message_get_no_reply (reply_to_this))
1693 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1695 reply_serial = dbus_message_get_serial (reply_to_this);
1697 link = bus_expire_list_get_first_link (connections->pending_replies);
1699 while (link != NULL)
1701 pending = link->data;
1703 if (pending->reply_serial == reply_serial &&
1704 pending->will_get_reply == will_get_reply &&
1705 pending->will_send_reply == will_send_reply)
1707 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1708 "Message has the same reply serial as a currently-outstanding existing method call");
1712 link = bus_expire_list_get_next_link (connections->pending_replies,
1714 if (pending->will_get_reply == will_get_reply)
1719 bus_context_get_max_replies_per_connection (connections->context))
1721 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1722 "The maximum number of pending replies per connection has been reached");
1726 pending = dbus_new0 (BusPendingReply, 1);
1727 if (pending == NULL)
1729 BUS_SET_OOM (error);
1733 #ifdef DBUS_ENABLE_VERBOSE_MODE
1734 /* so we can see a not-yet-added pending reply */
1735 pending->expire_item.added_tv_sec = 1;
1736 pending->expire_item.added_tv_usec = 1;
1739 pending->will_get_reply = will_get_reply;
1740 pending->will_send_reply = will_send_reply;
1741 pending->reply_serial = reply_serial;
1743 cprd = dbus_new0 (CancelPendingReplyData, 1);
1746 BUS_SET_OOM (error);
1747 bus_pending_reply_free (pending);
1751 if (!bus_expire_list_add (connections->pending_replies,
1752 &pending->expire_item))
1754 BUS_SET_OOM (error);
1756 bus_pending_reply_free (pending);
1760 if (!bus_transaction_add_cancel_hook (transaction,
1761 cancel_pending_reply,
1763 cancel_pending_reply_data_free))
1765 BUS_SET_OOM (error);
1766 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1768 bus_pending_reply_free (pending);
1772 cprd->pending = pending;
1773 cprd->connections = connections;
1775 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1776 &pending->expire_item.added_tv_usec);
1778 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1780 pending->will_send_reply,
1781 pending->will_get_reply,
1782 pending->reply_serial);
1790 BusConnections *connections;
1791 } CheckPendingReplyData;
1794 cancel_check_pending_reply (void *data)
1796 CheckPendingReplyData *d = data;
1798 _dbus_verbose ("d = %p\n",d);
1800 bus_expire_list_add_link (d->connections->pending_replies,
1806 check_pending_reply_data_free (void *data)
1808 CheckPendingReplyData *d = data;
1810 _dbus_verbose ("d = %p\n",d);
1812 if (d->link != NULL)
1814 BusPendingReply *pending = d->link->data;
1816 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1817 &pending->expire_item));
1819 bus_pending_reply_free (pending);
1820 _dbus_list_free_link (d->link);
1827 * Check whether a reply is allowed, remove BusPendingReply
1828 * if so, return TRUE if so.
1831 bus_connections_check_reply (BusConnections *connections,
1832 BusTransaction *transaction,
1833 DBusConnection *sending_reply,
1834 DBusConnection *receiving_reply,
1838 CheckPendingReplyData *cprd;
1840 dbus_uint32_t reply_serial;
1842 _dbus_assert (sending_reply != NULL);
1843 _dbus_assert (receiving_reply != NULL);
1845 reply_serial = dbus_message_get_reply_serial (reply);
1847 link = bus_expire_list_get_first_link (connections->pending_replies);
1848 while (link != NULL)
1850 BusPendingReply *pending = link->data;
1852 if (pending->reply_serial == reply_serial &&
1853 pending->will_get_reply == receiving_reply &&
1854 pending->will_send_reply == sending_reply)
1856 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1860 link = bus_expire_list_get_next_link (connections->pending_replies,
1866 _dbus_verbose ("No pending reply expected\n");
1871 cprd = dbus_new0 (CheckPendingReplyData, 1);
1874 BUS_SET_OOM (error);
1878 if (!bus_transaction_add_cancel_hook (transaction,
1879 cancel_check_pending_reply,
1881 check_pending_reply_data_free))
1883 BUS_SET_OOM (error);
1889 cprd->connections = connections;
1891 bus_expire_list_unlink (connections->pending_replies,
1894 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1902 * Note that this is fairly fragile; in particular, don't try to use
1903 * one transaction across any main loop iterations.
1908 BusTransaction *transaction;
1909 DBusMessage *message;
1910 DBusPreallocatedSend *preallocated;
1915 BusTransactionCancelFunction cancel_function;
1916 DBusFreeFunction free_data_function;
1920 struct BusTransaction
1922 DBusList *connections;
1923 BusContext *context;
1924 DBusList *cancel_hooks;
1928 message_to_send_free (DBusConnection *connection,
1929 MessageToSend *to_send)
1931 if (to_send->message)
1932 dbus_message_unref (to_send->message);
1934 if (to_send->preallocated)
1935 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1937 dbus_free (to_send);
1941 cancel_hook_cancel (void *element,
1944 CancelHook *ch = element;
1946 _dbus_verbose ("Running transaction cancel hook\n");
1948 if (ch->cancel_function)
1949 (* ch->cancel_function) (ch->data);
1953 cancel_hook_free (void *element,
1956 CancelHook *ch = element;
1958 if (ch->free_data_function)
1959 (* ch->free_data_function) (ch->data);
1965 free_cancel_hooks (BusTransaction *transaction)
1967 _dbus_list_foreach (&transaction->cancel_hooks,
1968 cancel_hook_free, NULL);
1970 _dbus_list_clear (&transaction->cancel_hooks);
1974 bus_transaction_new (BusContext *context)
1976 BusTransaction *transaction;
1978 transaction = dbus_new0 (BusTransaction, 1);
1979 if (transaction == NULL)
1982 transaction->context = context;
1988 bus_transaction_get_context (BusTransaction *transaction)
1990 return transaction->context;
1994 bus_transaction_get_connections (BusTransaction *transaction)
1996 return bus_context_get_connections (transaction->context);
2000 bus_transaction_send_from_driver (BusTransaction *transaction,
2001 DBusConnection *connection,
2002 DBusMessage *message)
2004 /* We have to set the sender to the driver, and have
2005 * to check security policy since it was not done in
2008 _dbus_verbose ("Sending %s %s %s from driver\n",
2009 dbus_message_get_interface (message) ?
2010 dbus_message_get_interface (message) : "(no interface)",
2011 dbus_message_get_member (message) ?
2012 dbus_message_get_member (message) : "(no member)",
2013 dbus_message_get_error_name (message) ?
2014 dbus_message_get_error_name (message) : "(no error name)");
2016 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2019 if (bus_connection_is_active (connection))
2021 if (!dbus_message_set_destination (message,
2022 bus_connection_get_name (connection)))
2026 /* bus driver never wants a reply */
2027 dbus_message_set_no_reply (message, TRUE);
2029 /* If security policy doesn't allow the message, we silently
2030 * eat it; the driver doesn't care about getting a reply.
2032 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2034 NULL, connection, connection, message, NULL))
2037 return bus_transaction_send (transaction, connection, message);
2041 bus_transaction_send (BusTransaction *transaction,
2042 DBusConnection *connection,
2043 DBusMessage *message)
2045 MessageToSend *to_send;
2046 BusConnectionData *d;
2049 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2050 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2051 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2053 dbus_message_get_interface (message) ?
2054 dbus_message_get_interface (message) : "(unset)",
2055 dbus_message_get_member (message) ?
2056 dbus_message_get_member (message) : "(unset)",
2057 dbus_message_get_error_name (message) ?
2058 dbus_message_get_error_name (message) : "(unset)",
2059 dbus_connection_get_is_connected (connection) ?
2060 "" : " (disconnected)");
2062 _dbus_assert (dbus_message_get_sender (message) != NULL);
2064 if (!dbus_connection_get_is_connected (connection))
2065 return TRUE; /* silently ignore disconnected connections */
2067 d = BUS_CONNECTION_DATA (connection);
2068 _dbus_assert (d != NULL);
2070 to_send = dbus_new (MessageToSend, 1);
2071 if (to_send == NULL)
2076 to_send->preallocated = dbus_connection_preallocate_send (connection);
2077 if (to_send->preallocated == NULL)
2079 dbus_free (to_send);
2083 dbus_message_ref (message);
2084 to_send->message = message;
2085 to_send->transaction = transaction;
2087 _dbus_verbose ("about to prepend message\n");
2089 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2091 message_to_send_free (connection, to_send);
2095 _dbus_verbose ("prepended message\n");
2097 /* See if we already had this connection in the list
2098 * for this transaction. If we have a pending message,
2099 * then we should already be in transaction->connections
2101 link = _dbus_list_get_first_link (&d->transaction_messages);
2102 _dbus_assert (link->data == to_send);
2103 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2104 while (link != NULL)
2106 MessageToSend *m = link->data;
2107 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2109 if (m->transaction == transaction)
2117 if (!_dbus_list_prepend (&transaction->connections, connection))
2119 _dbus_list_remove (&d->transaction_messages, to_send);
2120 message_to_send_free (connection, to_send);
2129 connection_cancel_transaction (DBusConnection *connection,
2130 BusTransaction *transaction)
2133 BusConnectionData *d;
2135 d = BUS_CONNECTION_DATA (connection);
2136 _dbus_assert (d != NULL);
2138 link = _dbus_list_get_first_link (&d->transaction_messages);
2139 while (link != NULL)
2141 MessageToSend *m = link->data;
2142 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2144 if (m->transaction == transaction)
2146 _dbus_list_remove_link (&d->transaction_messages,
2149 message_to_send_free (connection, m);
2157 bus_transaction_cancel_and_free (BusTransaction *transaction)
2159 DBusConnection *connection;
2161 _dbus_verbose ("TRANSACTION: cancelled\n");
2163 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2164 connection_cancel_transaction (connection, transaction);
2166 _dbus_assert (transaction->connections == NULL);
2168 _dbus_list_foreach (&transaction->cancel_hooks,
2169 cancel_hook_cancel, NULL);
2171 free_cancel_hooks (transaction);
2173 dbus_free (transaction);
2177 connection_execute_transaction (DBusConnection *connection,
2178 BusTransaction *transaction)
2181 BusConnectionData *d;
2183 d = BUS_CONNECTION_DATA (connection);
2184 _dbus_assert (d != NULL);
2186 /* Send the queue in order (FIFO) */
2187 link = _dbus_list_get_last_link (&d->transaction_messages);
2188 while (link != NULL)
2190 MessageToSend *m = link->data;
2191 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2193 if (m->transaction == transaction)
2195 _dbus_list_remove_link (&d->transaction_messages,
2198 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2200 dbus_connection_send_preallocated (connection,
2205 m->preallocated = NULL; /* so we don't double-free it */
2207 message_to_send_free (connection, m);
2215 bus_transaction_execute_and_free (BusTransaction *transaction)
2217 /* For each connection in transaction->connections
2220 DBusConnection *connection;
2222 _dbus_verbose ("TRANSACTION: executing\n");
2224 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2225 connection_execute_transaction (connection, transaction);
2227 _dbus_assert (transaction->connections == NULL);
2229 free_cancel_hooks (transaction);
2231 dbus_free (transaction);
2235 bus_connection_remove_transactions (DBusConnection *connection)
2237 MessageToSend *to_send;
2238 BusConnectionData *d;
2240 d = BUS_CONNECTION_DATA (connection);
2241 _dbus_assert (d != NULL);
2243 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2245 /* only has an effect for the first MessageToSend listing this transaction */
2246 _dbus_list_remove (&to_send->transaction->connections,
2249 _dbus_list_remove (&d->transaction_messages, to_send);
2250 message_to_send_free (connection, to_send);
2255 * Converts the DBusError to a message reply
2258 bus_transaction_send_error_reply (BusTransaction *transaction,
2259 DBusConnection *connection,
2260 const DBusError *error,
2261 DBusMessage *in_reply_to)
2265 _dbus_assert (error != NULL);
2266 _DBUS_ASSERT_ERROR_IS_SET (error);
2268 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2269 error->name, error->message);
2271 reply = dbus_message_new_error (in_reply_to,
2277 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2279 dbus_message_unref (reply);
2283 dbus_message_unref (reply);
2289 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2290 BusTransactionCancelFunction cancel_function,
2292 DBusFreeFunction free_data_function)
2296 ch = dbus_new (CancelHook, 1);
2300 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2301 cancel_function, data);
2303 ch->cancel_function = cancel_function;
2305 ch->free_data_function = free_data_function;
2307 /* It's important that the hooks get run in reverse order that they
2310 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2319 #ifdef DBUS_ENABLE_STATS
2321 bus_connections_get_n_active (BusConnections *connections)
2323 return connections->n_completed;
2327 bus_connections_get_n_incomplete (BusConnections *connections)
2329 return connections->n_incomplete;
2333 bus_connections_get_total_match_rules (BusConnections *connections)
2335 return connections->total_match_rules;
2339 bus_connections_get_peak_match_rules (BusConnections *connections)
2341 return connections->peak_match_rules;
2345 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2347 return connections->peak_match_rules_per_conn;
2351 bus_connections_get_total_bus_names (BusConnections *connections)
2353 return connections->total_bus_names;
2357 bus_connections_get_peak_bus_names (BusConnections *connections)
2359 return connections->peak_bus_names;
2363 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2365 return connections->peak_bus_names_per_conn;
2369 bus_connection_get_peak_match_rules (DBusConnection *connection)
2371 BusConnectionData *d;
2373 d = BUS_CONNECTION_DATA (connection);
2374 return d->peak_match_rules;
2378 bus_connection_get_peak_bus_names (DBusConnection *connection)
2380 BusConnectionData *d;
2382 d = BUS_CONNECTION_DATA (connection);
2383 return d->peak_bus_names;
2385 #endif /* DBUS_ENABLE_STATS */