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 add_connection_timeout (DBusTimeout *timeout,
334 DBusConnection *connection = data;
336 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
340 remove_connection_timeout (DBusTimeout *timeout,
343 DBusConnection *connection = data;
345 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
349 dispatch_status_function (DBusConnection *connection,
350 DBusDispatchStatus new_status,
353 DBusLoop *loop = data;
355 if (new_status != DBUS_DISPATCH_COMPLETE)
357 while (!_dbus_loop_queue_dispatch (loop, connection))
358 _dbus_wait_for_memory ();
363 allow_unix_user_function (DBusConnection *connection,
367 BusConnectionData *d;
369 d = BUS_CONNECTION_DATA (connection);
371 _dbus_assert (d != NULL);
373 return bus_context_allow_unix_user (d->connections->context, uid);
377 free_connection_data (void *data)
379 BusConnectionData *d = data;
381 /* services_owned should be NULL since we should be disconnected */
382 _dbus_assert (d->services_owned == NULL);
383 _dbus_assert (d->n_services_owned == 0);
385 _dbus_assert (d->transaction_messages == NULL);
387 if (d->oom_preallocated)
388 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
391 dbus_message_unref (d->oom_message);
394 bus_client_policy_unref (d->policy);
397 bus_selinux_id_unref (d->selinux_id);
399 dbus_free (d->cached_loginfo_string);
407 bus_connections_new (BusContext *context)
409 BusConnections *connections;
411 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
414 connections = dbus_new0 (BusConnections, 1);
415 if (connections == NULL)
418 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
420 if (connections->completed_by_user == NULL)
423 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
424 expire_incomplete_timeout,
426 if (connections->expire_timeout == NULL)
429 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
431 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
432 bus_context_get_reply_timeout (context),
433 bus_pending_reply_expired,
435 if (connections->pending_replies == NULL)
438 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
439 connections->expire_timeout))
442 connections->refcount = 1;
443 connections->context = context;
448 bus_expire_list_free (connections->pending_replies);
450 _dbus_timeout_unref (connections->expire_timeout);
452 _dbus_hash_table_unref (connections->completed_by_user);
454 dbus_free (connections);
456 dbus_connection_free_data_slot (&connection_data_slot);
462 bus_connections_ref (BusConnections *connections)
464 _dbus_assert (connections->refcount > 0);
465 connections->refcount += 1;
471 bus_connections_unref (BusConnections *connections)
473 _dbus_assert (connections->refcount > 0);
474 connections->refcount -= 1;
475 if (connections->refcount == 0)
477 /* drop all incomplete */
478 while (connections->incomplete != NULL)
480 DBusConnection *connection;
482 connection = connections->incomplete->data;
484 dbus_connection_ref (connection);
485 dbus_connection_close (connection);
486 bus_connection_disconnected (connection);
487 dbus_connection_unref (connection);
490 _dbus_assert (connections->n_incomplete == 0);
492 /* drop all real connections */
493 while (connections->completed != NULL)
495 DBusConnection *connection;
497 connection = connections->completed->data;
499 dbus_connection_ref (connection);
500 dbus_connection_close (connection);
501 bus_connection_disconnected (connection);
502 dbus_connection_unref (connection);
505 _dbus_assert (connections->n_completed == 0);
507 bus_expire_list_free (connections->pending_replies);
509 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
510 connections->expire_timeout);
512 _dbus_timeout_unref (connections->expire_timeout);
514 _dbus_hash_table_unref (connections->completed_by_user);
516 dbus_free (connections);
518 dbus_connection_free_data_slot (&connection_data_slot);
522 /* Used for logging */
524 cache_peer_loginfo_string (BusConnectionData *d,
525 DBusConnection *connection)
527 DBusString loginfo_buf;
531 dbus_bool_t prev_added;
533 if (!_dbus_string_init (&loginfo_buf))
537 if (dbus_connection_get_unix_user (connection, &uid))
539 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
545 if (dbus_connection_get_unix_process_id (connection, &pid))
549 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
552 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
554 /* Ignore errors here; we may not have permissions to read the
556 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
557 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
561 if (dbus_connection_get_windows_user (connection, &windows_sid))
563 dbus_bool_t did_append;
564 did_append = _dbus_string_append_printf (&loginfo_buf,
565 "sid=\"%s\" ", windows_sid);
566 dbus_free (windows_sid);
571 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
574 _dbus_string_free (&loginfo_buf);
578 _dbus_string_free (&loginfo_buf);
583 bus_connections_setup_connection (BusConnections *connections,
584 DBusConnection *connection)
587 BusConnectionData *d;
592 d = dbus_new0 (BusConnectionData, 1);
597 d->connections = connections;
598 d->connection = connection;
600 _dbus_get_current_time (&d->connection_tv_sec,
601 &d->connection_tv_usec);
603 _dbus_assert (connection_data_slot >= 0);
605 if (!dbus_connection_set_data (connection,
606 connection_data_slot,
607 d, free_connection_data))
613 dbus_connection_set_route_peer_messages (connection, TRUE);
617 dbus_error_init (&error);
618 d->selinux_id = bus_selinux_init_connection_id (connection,
620 if (dbus_error_is_set (&error))
622 /* This is a bit bogus because we pretend all errors
623 * are OOM; this is done because we know that in bus.c
624 * an OOM error disconnects the connection, which is
625 * the same thing we want on any other error.
627 dbus_error_free (&error);
631 if (!dbus_connection_set_watch_functions (connection,
632 add_connection_watch,
633 remove_connection_watch,
639 if (!dbus_connection_set_timeout_functions (connection,
640 add_connection_timeout,
641 remove_connection_timeout,
646 /* For now we don't need to set a Windows user function because
647 * there are no policies in the config file controlling what
648 * Windows users can connect. The default 'same user that owns the
649 * bus can connect' behavior of DBusConnection is fine on Windows.
651 dbus_connection_set_unix_user_function (connection,
652 allow_unix_user_function,
655 dbus_connection_set_dispatch_status_function (connection,
656 dispatch_status_function,
657 bus_context_get_loop (connections->context),
660 d->link_in_connection_list = _dbus_list_alloc_link (connection);
661 if (d->link_in_connection_list == NULL)
664 /* Setup the connection with the dispatcher */
665 if (!bus_dispatch_add_connection (connection))
668 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
670 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
672 bus_dispatch_remove_connection (connection);
677 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
678 connections->n_incomplete += 1;
680 dbus_connection_ref (connection);
682 /* Note that we might disconnect ourselves here, but it only takes
683 * effect on return to the main loop. We call this to free up
684 * expired connections if possible, and to queue the timeout for our
687 bus_connections_expire_incomplete (connections);
689 /* And we might also disconnect ourselves here, but again it
690 * only takes effect on return to main loop.
692 if (connections->n_incomplete >
693 bus_context_get_max_incomplete_connections (connections->context))
695 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
697 _dbus_assert (connections->incomplete != NULL);
698 /* Disconnect the oldest unauthenticated connection. FIXME
699 * would it be more secure to drop a *random* connection? This
700 * algorithm seems to mean that if someone can create new
701 * connections quickly enough, they can keep anyone else from
702 * completing authentication. But random may or may not really
703 * help with that, a more elaborate solution might be required.
705 dbus_connection_close (connections->incomplete->data);
714 bus_selinux_id_unref (d->selinux_id);
715 d->selinux_id = NULL;
717 if (!dbus_connection_set_watch_functions (connection,
721 _dbus_assert_not_reached ("setting watch functions to NULL failed");
723 if (!dbus_connection_set_timeout_functions (connection,
727 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
729 dbus_connection_set_unix_user_function (connection,
732 dbus_connection_set_windows_user_function (connection,
735 dbus_connection_set_dispatch_status_function (connection,
738 if (d->link_in_connection_list != NULL)
740 _dbus_assert (d->link_in_connection_list->next == NULL);
741 _dbus_assert (d->link_in_connection_list->prev == NULL);
742 _dbus_list_free_link (d->link_in_connection_list);
743 d->link_in_connection_list = NULL;
746 if (!dbus_connection_set_data (connection,
747 connection_data_slot,
749 _dbus_assert_not_reached ("failed to set connection data to null");
751 /* "d" has now been freed */
758 bus_connections_expire_incomplete (BusConnections *connections)
764 if (connections->incomplete != NULL)
766 long tv_sec, tv_usec;
770 _dbus_get_current_time (&tv_sec, &tv_usec);
771 auth_timeout = bus_context_get_auth_timeout (connections->context);
773 link = _dbus_list_get_first_link (&connections->incomplete);
776 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
777 DBusConnection *connection;
778 BusConnectionData *d;
781 connection = link->data;
783 d = BUS_CONNECTION_DATA (connection);
785 _dbus_assert (d != NULL);
787 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
788 d->connection_tv_usec,
791 if (elapsed >= (double) auth_timeout)
793 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
794 dbus_connection_close (connection);
798 /* We can end the loop, since the connections are in oldest-first order */
799 next_interval = ((double)auth_timeout) - elapsed;
800 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
801 connection, next_interval);
810 bus_expire_timeout_set_interval (connections->expire_timeout,
815 expire_incomplete_timeout (void *data)
817 BusConnections *connections = data;
819 _dbus_verbose ("Running\n");
821 /* note that this may remove the timeout */
822 bus_connections_expire_incomplete (connections);
828 bus_connection_get_unix_groups (DBusConnection *connection,
829 unsigned long **groups,
838 if (dbus_connection_get_unix_user (connection, &uid))
840 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
842 _dbus_verbose ("Did not get any groups for UID %lu\n",
848 _dbus_verbose ("Got %d groups for UID %lu\n",
854 return TRUE; /* successfully got 0 groups */
858 bus_connection_is_in_unix_group (DBusConnection *connection,
862 unsigned long *group_ids;
865 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
870 while (i < n_group_ids)
872 if (group_ids[i] == gid)
874 dbus_free (group_ids);
880 dbus_free (group_ids);
885 bus_connection_get_loginfo (DBusConnection *connection)
887 BusConnectionData *d;
889 d = BUS_CONNECTION_DATA (connection);
891 if (!bus_connection_is_active (connection))
893 return d->cached_loginfo_string;
897 bus_connection_get_policy (DBusConnection *connection)
899 BusConnectionData *d;
901 d = BUS_CONNECTION_DATA (connection);
903 _dbus_assert (d != NULL);
904 _dbus_assert (d->policy != NULL);
910 foreach_active (BusConnections *connections,
911 BusConnectionForeachFunction function,
916 link = _dbus_list_get_first_link (&connections->completed);
919 DBusConnection *connection = link->data;
920 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
922 if (!(* function) (connection, data))
932 foreach_inactive (BusConnections *connections,
933 BusConnectionForeachFunction function,
938 link = _dbus_list_get_first_link (&connections->incomplete);
941 DBusConnection *connection = link->data;
942 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
944 if (!(* function) (connection, data))
954 * Calls function on each active connection; if the function returns
955 * #FALSE, stops iterating. Active connections are authenticated
956 * and have sent a Hello message.
958 * @param connections the connections object
959 * @param function the function
960 * @param data data to pass to it as a second arg
963 bus_connections_foreach_active (BusConnections *connections,
964 BusConnectionForeachFunction function,
967 foreach_active (connections, function, data);
971 * Calls function on each connection; if the function returns
972 * #FALSE, stops iterating.
974 * @param connections the connections object
975 * @param function the function
976 * @param data data to pass to it as a second arg
979 bus_connections_foreach (BusConnections *connections,
980 BusConnectionForeachFunction function,
983 if (!foreach_active (connections, function, data))
986 foreach_inactive (connections, function, data);
990 bus_connections_get_context (BusConnections *connections)
992 return connections->context;
996 * This is used to avoid covering the same connection twice when
997 * traversing connections. Note that it assumes we will
998 * bus_connection_mark_stamp() each connection at least once per
999 * INT_MAX increments of the global stamp, or wraparound would break
1003 bus_connections_increment_stamp (BusConnections *connections)
1005 connections->stamp += 1;
1008 /* Mark connection with current stamp, return TRUE if it
1009 * didn't already have that stamp
1012 bus_connection_mark_stamp (DBusConnection *connection)
1014 BusConnectionData *d;
1016 d = BUS_CONNECTION_DATA (connection);
1018 _dbus_assert (d != NULL);
1020 if (d->stamp == d->connections->stamp)
1024 d->stamp = d->connections->stamp;
1030 bus_connection_get_context (DBusConnection *connection)
1032 BusConnectionData *d;
1034 d = BUS_CONNECTION_DATA (connection);
1036 _dbus_assert (d != NULL);
1038 return d->connections->context;
1042 bus_connection_get_connections (DBusConnection *connection)
1044 BusConnectionData *d;
1046 d = BUS_CONNECTION_DATA (connection);
1048 _dbus_assert (d != NULL);
1050 return d->connections;
1054 bus_connection_get_registry (DBusConnection *connection)
1056 BusConnectionData *d;
1058 d = BUS_CONNECTION_DATA (connection);
1060 _dbus_assert (d != NULL);
1062 return bus_context_get_registry (d->connections->context);
1066 bus_connection_get_activation (DBusConnection *connection)
1068 BusConnectionData *d;
1070 d = BUS_CONNECTION_DATA (connection);
1072 _dbus_assert (d != NULL);
1074 return bus_context_get_activation (d->connections->context);
1078 bus_connection_get_matchmaker (DBusConnection *connection)
1080 BusConnectionData *d;
1082 d = BUS_CONNECTION_DATA (connection);
1084 _dbus_assert (d != NULL);
1086 return bus_context_get_matchmaker (d->connections->context);
1090 bus_connection_get_selinux_id (DBusConnection *connection)
1092 BusConnectionData *d;
1094 d = BUS_CONNECTION_DATA (connection);
1096 _dbus_assert (d != NULL);
1098 return d->selinux_id;
1102 * Checks whether the connection is registered with the message bus.
1104 * @param connection the connection
1105 * @returns #TRUE if we're an active message bus participant
1108 bus_connection_is_active (DBusConnection *connection)
1110 BusConnectionData *d;
1112 d = BUS_CONNECTION_DATA (connection);
1114 return d != NULL && d->name != NULL;
1118 bus_connection_preallocate_oom_error (DBusConnection *connection)
1120 DBusMessage *message;
1121 DBusPreallocatedSend *preallocated;
1122 BusConnectionData *d;
1124 d = BUS_CONNECTION_DATA (connection);
1126 _dbus_assert (d != NULL);
1128 if (d->oom_preallocated != NULL)
1131 preallocated = dbus_connection_preallocate_send (connection);
1132 if (preallocated == NULL)
1135 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1137 if (message == NULL)
1139 dbus_connection_free_preallocated_send (connection, preallocated);
1143 /* d->name may be NULL, but that is OK */
1144 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1145 !dbus_message_set_destination (message, d->name) ||
1146 !dbus_message_set_sender (message,
1149 dbus_connection_free_preallocated_send (connection, preallocated);
1150 dbus_message_unref (message);
1154 /* set reply serial to placeholder value just so space is already allocated
1157 if (!dbus_message_set_reply_serial (message, 14))
1159 dbus_connection_free_preallocated_send (connection, preallocated);
1160 dbus_message_unref (message);
1164 d->oom_message = message;
1165 d->oom_preallocated = preallocated;
1171 bus_connection_send_oom_error (DBusConnection *connection,
1172 DBusMessage *in_reply_to)
1174 BusConnectionData *d;
1176 d = BUS_CONNECTION_DATA (connection);
1178 _dbus_assert (d != NULL);
1179 _dbus_assert (d->oom_message != NULL);
1181 /* should always succeed since we set it to a placeholder earlier */
1182 if (!dbus_message_set_reply_serial (d->oom_message,
1183 dbus_message_get_serial (in_reply_to)))
1184 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1186 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1188 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1189 d->oom_message, NULL);
1191 dbus_message_unref (d->oom_message);
1192 d->oom_message = NULL;
1193 d->oom_preallocated = NULL;
1196 #ifdef DBUS_ENABLE_STATS
1198 update_peak (int *peak,
1207 bus_connection_add_match_rule_link (DBusConnection *connection,
1210 BusConnectionData *d;
1212 d = BUS_CONNECTION_DATA (connection);
1213 _dbus_assert (d != NULL);
1215 _dbus_list_append_link (&d->match_rules, link);
1217 d->n_match_rules += 1;
1219 #ifdef DBUS_ENABLE_STATS
1220 update_peak (&d->peak_match_rules, d->n_match_rules);
1221 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1223 d->connections->total_match_rules += 1;
1224 update_peak (&d->connections->peak_match_rules,
1225 d->connections->total_match_rules);
1230 bus_connection_add_match_rule (DBusConnection *connection,
1235 link = _dbus_list_alloc_link (rule);
1240 bus_connection_add_match_rule_link (connection, link);
1246 bus_connection_remove_match_rule (DBusConnection *connection,
1249 BusConnectionData *d;
1251 d = BUS_CONNECTION_DATA (connection);
1252 _dbus_assert (d != NULL);
1254 _dbus_list_remove_last (&d->match_rules, rule);
1256 d->n_match_rules -= 1;
1257 _dbus_assert (d->n_match_rules >= 0);
1259 #ifdef DBUS_ENABLE_STATS
1260 d->connections->total_match_rules -= 1;
1265 bus_connection_get_n_match_rules (DBusConnection *connection)
1267 BusConnectionData *d;
1269 d = BUS_CONNECTION_DATA (connection);
1270 _dbus_assert (d != NULL);
1272 return d->n_match_rules;
1276 bus_connection_add_owned_service_link (DBusConnection *connection,
1279 BusConnectionData *d;
1281 d = BUS_CONNECTION_DATA (connection);
1282 _dbus_assert (d != NULL);
1284 _dbus_list_append_link (&d->services_owned, link);
1286 d->n_services_owned += 1;
1288 #ifdef DBUS_ENABLE_STATS
1289 update_peak (&d->peak_bus_names, d->n_services_owned);
1290 update_peak (&d->connections->peak_bus_names_per_conn,
1291 d->n_services_owned);
1293 d->connections->total_bus_names += 1;
1294 update_peak (&d->connections->peak_bus_names,
1295 d->connections->total_bus_names);
1300 bus_connection_add_owned_service (DBusConnection *connection,
1301 BusService *service)
1305 link = _dbus_list_alloc_link (service);
1310 bus_connection_add_owned_service_link (connection, link);
1316 bus_connection_remove_owned_service (DBusConnection *connection,
1317 BusService *service)
1319 BusConnectionData *d;
1321 d = BUS_CONNECTION_DATA (connection);
1322 _dbus_assert (d != NULL);
1324 _dbus_list_remove_last (&d->services_owned, service);
1326 d->n_services_owned -= 1;
1327 _dbus_assert (d->n_services_owned >= 0);
1329 #ifdef DBUS_ENABLE_STATS
1330 d->connections->total_bus_names -= 1;
1335 bus_connection_get_n_services_owned (DBusConnection *connection)
1337 BusConnectionData *d;
1339 d = BUS_CONNECTION_DATA (connection);
1340 _dbus_assert (d != NULL);
1342 return d->n_services_owned;
1346 bus_connection_complete (DBusConnection *connection,
1347 const DBusString *name,
1350 BusConnectionData *d;
1353 d = BUS_CONNECTION_DATA (connection);
1354 _dbus_assert (d != NULL);
1355 _dbus_assert (d->name == NULL);
1356 _dbus_assert (d->policy == NULL);
1358 _dbus_assert (!bus_connection_is_active (connection));
1360 if (!_dbus_string_copy_data (name, &d->name))
1362 BUS_SET_OOM (error);
1366 _dbus_assert (d->name != NULL);
1368 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1370 d->policy = bus_context_create_client_policy (d->connections->context,
1374 /* we may have a NULL policy on OOM or error getting list of
1375 * groups for a user. In the latter case we don't handle it so
1376 * well currently, as it will just keep failing over and over.
1379 if (d->policy == NULL)
1381 _dbus_verbose ("Failed to create security policy for connection %p\n",
1383 _DBUS_ASSERT_ERROR_IS_SET (error);
1384 dbus_free (d->name);
1389 if (dbus_connection_get_unix_user (connection, &uid))
1391 if (!adjust_connections_for_uid (d->connections,
1396 /* Create and cache a string which holds information about the
1397 * peer process; used for logging purposes.
1399 if (!cache_peer_loginfo_string (d, connection))
1402 /* Now the connection is active, move it between lists */
1403 _dbus_list_unlink (&d->connections->incomplete,
1404 d->link_in_connection_list);
1405 d->connections->n_incomplete -= 1;
1406 _dbus_list_append_link (&d->connections->completed,
1407 d->link_in_connection_list);
1408 d->connections->n_completed += 1;
1410 _dbus_assert (d->connections->n_incomplete >= 0);
1411 _dbus_assert (d->connections->n_completed > 0);
1413 /* See if we can remove the timeout */
1414 bus_connections_expire_incomplete (d->connections);
1416 _dbus_assert (bus_connection_is_active (connection));
1420 BUS_SET_OOM (error);
1421 dbus_free (d->name);
1424 bus_client_policy_unref (d->policy);
1430 bus_connection_get_name (DBusConnection *connection)
1432 BusConnectionData *d;
1434 d = BUS_CONNECTION_DATA (connection);
1435 _dbus_assert (d != NULL);
1441 * Check whether completing the passed-in connection would
1442 * exceed limits, and if so set error and return #FALSE
1445 bus_connections_check_limits (BusConnections *connections,
1446 DBusConnection *requesting_completion,
1451 if (connections->n_completed >=
1452 bus_context_get_max_completed_connections (connections->context))
1454 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1455 "The maximum number of active connections has been reached");
1459 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1461 if (get_connections_for_uid (connections, uid) >=
1462 bus_context_get_max_connections_per_user (connections->context))
1464 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1465 "The maximum number of active connections for UID %lu has been reached",
1475 bus_pending_reply_free (BusPendingReply *pending)
1477 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1479 pending->will_send_reply,
1480 pending->will_get_reply,
1481 pending->reply_serial);
1483 dbus_free (pending);
1487 bus_pending_reply_send_no_reply (BusConnections *connections,
1488 BusTransaction *transaction,
1489 BusPendingReply *pending)
1491 DBusMessage *message;
1492 DBusMessageIter iter;
1498 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1499 if (message == NULL)
1502 dbus_message_set_no_reply (message, TRUE);
1504 if (!dbus_message_set_reply_serial (message,
1505 pending->reply_serial))
1508 if (!dbus_message_set_error_name (message,
1509 DBUS_ERROR_NO_REPLY))
1512 errmsg = "Message did not receive a reply (timeout by message bus)";
1513 dbus_message_iter_init_append (message, &iter);
1514 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1517 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1524 dbus_message_unref (message);
1529 bus_pending_reply_expired (BusExpireList *list,
1533 BusPendingReply *pending = link->data;
1534 BusConnections *connections = data;
1535 BusTransaction *transaction;
1537 /* No reply is forthcoming. So nuke it if we can. If not,
1538 * leave it in the list to try expiring again later when we
1542 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1544 pending->will_send_reply,
1545 pending->will_get_reply,
1546 pending->reply_serial);
1548 transaction = bus_transaction_new (connections->context);
1549 if (transaction == NULL)
1552 if (!bus_pending_reply_send_no_reply (connections,
1556 bus_transaction_cancel_and_free (transaction);
1560 bus_expire_list_remove_link (connections->pending_replies, link);
1562 bus_pending_reply_free (pending);
1563 bus_transaction_execute_and_free (transaction);
1569 bus_connection_drop_pending_replies (BusConnections *connections,
1570 DBusConnection *connection)
1572 /* The DBusConnection is almost 100% finalized here, so you can't
1573 * do anything with it except check for pointer equality
1577 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1580 link = bus_expire_list_get_first_link (connections->pending_replies);
1581 while (link != NULL)
1584 BusPendingReply *pending;
1586 next = bus_expire_list_get_next_link (connections->pending_replies,
1588 pending = link->data;
1590 if (pending->will_get_reply == connection)
1592 /* We don't need to track this pending reply anymore */
1594 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1596 pending->will_send_reply,
1597 pending->will_get_reply,
1598 pending->reply_serial);
1600 bus_expire_list_remove_link (connections->pending_replies,
1602 bus_pending_reply_free (pending);
1604 else if (pending->will_send_reply == connection)
1606 /* The reply isn't going to be sent, so set things
1607 * up so it will be expired right away
1609 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1611 pending->will_send_reply,
1612 pending->will_get_reply,
1613 pending->reply_serial);
1615 pending->will_send_reply = NULL;
1616 pending->expire_item.added_tv_sec = 0;
1617 pending->expire_item.added_tv_usec = 0;
1619 bus_expire_list_recheck_immediately (connections->pending_replies);
1629 BusPendingReply *pending;
1630 BusConnections *connections;
1631 } CancelPendingReplyData;
1634 cancel_pending_reply (void *data)
1636 CancelPendingReplyData *d = data;
1638 _dbus_verbose ("d = %p\n", d);
1640 if (!bus_expire_list_remove (d->connections->pending_replies,
1641 &d->pending->expire_item))
1642 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1644 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1648 cancel_pending_reply_data_free (void *data)
1650 CancelPendingReplyData *d = data;
1652 _dbus_verbose ("d = %p\n", d);
1654 /* d->pending should be either freed or still
1655 * in the list of pending replies (owned by someone
1663 * Record that a reply is allowed; return TRUE on success.
1666 bus_connections_expect_reply (BusConnections *connections,
1667 BusTransaction *transaction,
1668 DBusConnection *will_get_reply,
1669 DBusConnection *will_send_reply,
1670 DBusMessage *reply_to_this,
1673 BusPendingReply *pending;
1674 dbus_uint32_t reply_serial;
1676 CancelPendingReplyData *cprd;
1679 _dbus_assert (will_get_reply != NULL);
1680 _dbus_assert (will_send_reply != NULL);
1681 _dbus_assert (reply_to_this != NULL);
1683 if (dbus_message_get_no_reply (reply_to_this))
1684 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1686 reply_serial = dbus_message_get_serial (reply_to_this);
1688 link = bus_expire_list_get_first_link (connections->pending_replies);
1690 while (link != NULL)
1692 pending = link->data;
1694 if (pending->reply_serial == reply_serial &&
1695 pending->will_get_reply == will_get_reply &&
1696 pending->will_send_reply == will_send_reply)
1698 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1699 "Message has the same reply serial as a currently-outstanding existing method call");
1703 link = bus_expire_list_get_next_link (connections->pending_replies,
1705 if (pending->will_get_reply == will_get_reply)
1710 bus_context_get_max_replies_per_connection (connections->context))
1712 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1713 "The maximum number of pending replies per connection has been reached");
1717 pending = dbus_new0 (BusPendingReply, 1);
1718 if (pending == NULL)
1720 BUS_SET_OOM (error);
1724 #ifdef DBUS_ENABLE_VERBOSE_MODE
1725 /* so we can see a not-yet-added pending reply */
1726 pending->expire_item.added_tv_sec = 1;
1727 pending->expire_item.added_tv_usec = 1;
1730 pending->will_get_reply = will_get_reply;
1731 pending->will_send_reply = will_send_reply;
1732 pending->reply_serial = reply_serial;
1734 cprd = dbus_new0 (CancelPendingReplyData, 1);
1737 BUS_SET_OOM (error);
1738 bus_pending_reply_free (pending);
1742 if (!bus_expire_list_add (connections->pending_replies,
1743 &pending->expire_item))
1745 BUS_SET_OOM (error);
1747 bus_pending_reply_free (pending);
1751 if (!bus_transaction_add_cancel_hook (transaction,
1752 cancel_pending_reply,
1754 cancel_pending_reply_data_free))
1756 BUS_SET_OOM (error);
1757 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1759 bus_pending_reply_free (pending);
1763 cprd->pending = pending;
1764 cprd->connections = connections;
1766 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1767 &pending->expire_item.added_tv_usec);
1769 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1771 pending->will_send_reply,
1772 pending->will_get_reply,
1773 pending->reply_serial);
1781 BusConnections *connections;
1782 } CheckPendingReplyData;
1785 cancel_check_pending_reply (void *data)
1787 CheckPendingReplyData *d = data;
1789 _dbus_verbose ("d = %p\n",d);
1791 bus_expire_list_add_link (d->connections->pending_replies,
1797 check_pending_reply_data_free (void *data)
1799 CheckPendingReplyData *d = data;
1801 _dbus_verbose ("d = %p\n",d);
1803 if (d->link != NULL)
1805 BusPendingReply *pending = d->link->data;
1807 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1808 &pending->expire_item));
1810 bus_pending_reply_free (pending);
1811 _dbus_list_free_link (d->link);
1818 * Check whether a reply is allowed, remove BusPendingReply
1819 * if so, return TRUE if so.
1822 bus_connections_check_reply (BusConnections *connections,
1823 BusTransaction *transaction,
1824 DBusConnection *sending_reply,
1825 DBusConnection *receiving_reply,
1829 CheckPendingReplyData *cprd;
1831 dbus_uint32_t reply_serial;
1833 _dbus_assert (sending_reply != NULL);
1834 _dbus_assert (receiving_reply != NULL);
1836 reply_serial = dbus_message_get_reply_serial (reply);
1838 link = bus_expire_list_get_first_link (connections->pending_replies);
1839 while (link != NULL)
1841 BusPendingReply *pending = link->data;
1843 if (pending->reply_serial == reply_serial &&
1844 pending->will_get_reply == receiving_reply &&
1845 pending->will_send_reply == sending_reply)
1847 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1851 link = bus_expire_list_get_next_link (connections->pending_replies,
1857 _dbus_verbose ("No pending reply expected\n");
1862 cprd = dbus_new0 (CheckPendingReplyData, 1);
1865 BUS_SET_OOM (error);
1869 if (!bus_transaction_add_cancel_hook (transaction,
1870 cancel_check_pending_reply,
1872 check_pending_reply_data_free))
1874 BUS_SET_OOM (error);
1880 cprd->connections = connections;
1882 bus_expire_list_unlink (connections->pending_replies,
1885 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1893 * Note that this is fairly fragile; in particular, don't try to use
1894 * one transaction across any main loop iterations.
1899 BusTransaction *transaction;
1900 DBusMessage *message;
1901 DBusPreallocatedSend *preallocated;
1906 BusTransactionCancelFunction cancel_function;
1907 DBusFreeFunction free_data_function;
1911 struct BusTransaction
1913 DBusList *connections;
1914 BusContext *context;
1915 DBusList *cancel_hooks;
1919 message_to_send_free (DBusConnection *connection,
1920 MessageToSend *to_send)
1922 if (to_send->message)
1923 dbus_message_unref (to_send->message);
1925 if (to_send->preallocated)
1926 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1928 dbus_free (to_send);
1932 cancel_hook_cancel (void *element,
1935 CancelHook *ch = element;
1937 _dbus_verbose ("Running transaction cancel hook\n");
1939 if (ch->cancel_function)
1940 (* ch->cancel_function) (ch->data);
1944 cancel_hook_free (void *element,
1947 CancelHook *ch = element;
1949 if (ch->free_data_function)
1950 (* ch->free_data_function) (ch->data);
1956 free_cancel_hooks (BusTransaction *transaction)
1958 _dbus_list_foreach (&transaction->cancel_hooks,
1959 cancel_hook_free, NULL);
1961 _dbus_list_clear (&transaction->cancel_hooks);
1965 bus_transaction_new (BusContext *context)
1967 BusTransaction *transaction;
1969 transaction = dbus_new0 (BusTransaction, 1);
1970 if (transaction == NULL)
1973 transaction->context = context;
1979 bus_transaction_get_context (BusTransaction *transaction)
1981 return transaction->context;
1985 bus_transaction_get_connections (BusTransaction *transaction)
1987 return bus_context_get_connections (transaction->context);
1991 bus_transaction_send_from_driver (BusTransaction *transaction,
1992 DBusConnection *connection,
1993 DBusMessage *message)
1995 /* We have to set the sender to the driver, and have
1996 * to check security policy since it was not done in
1999 _dbus_verbose ("Sending %s %s %s from driver\n",
2000 dbus_message_get_interface (message) ?
2001 dbus_message_get_interface (message) : "(no interface)",
2002 dbus_message_get_member (message) ?
2003 dbus_message_get_member (message) : "(no member)",
2004 dbus_message_get_error_name (message) ?
2005 dbus_message_get_error_name (message) : "(no error name)");
2007 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2010 if (bus_connection_is_active (connection))
2012 if (!dbus_message_set_destination (message,
2013 bus_connection_get_name (connection)))
2017 /* bus driver never wants a reply */
2018 dbus_message_set_no_reply (message, TRUE);
2020 /* If security policy doesn't allow the message, we silently
2021 * eat it; the driver doesn't care about getting a reply.
2023 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2025 NULL, connection, connection, message, NULL))
2028 return bus_transaction_send (transaction, connection, message);
2032 bus_transaction_send (BusTransaction *transaction,
2033 DBusConnection *connection,
2034 DBusMessage *message)
2036 MessageToSend *to_send;
2037 BusConnectionData *d;
2040 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2041 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2042 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2044 dbus_message_get_interface (message) ?
2045 dbus_message_get_interface (message) : "(unset)",
2046 dbus_message_get_member (message) ?
2047 dbus_message_get_member (message) : "(unset)",
2048 dbus_message_get_error_name (message) ?
2049 dbus_message_get_error_name (message) : "(unset)",
2050 dbus_connection_get_is_connected (connection) ?
2051 "" : " (disconnected)");
2053 _dbus_assert (dbus_message_get_sender (message) != NULL);
2055 if (!dbus_connection_get_is_connected (connection))
2056 return TRUE; /* silently ignore disconnected connections */
2058 d = BUS_CONNECTION_DATA (connection);
2059 _dbus_assert (d != NULL);
2061 to_send = dbus_new (MessageToSend, 1);
2062 if (to_send == NULL)
2067 to_send->preallocated = dbus_connection_preallocate_send (connection);
2068 if (to_send->preallocated == NULL)
2070 dbus_free (to_send);
2074 dbus_message_ref (message);
2075 to_send->message = message;
2076 to_send->transaction = transaction;
2078 _dbus_verbose ("about to prepend message\n");
2080 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2082 message_to_send_free (connection, to_send);
2086 _dbus_verbose ("prepended message\n");
2088 /* See if we already had this connection in the list
2089 * for this transaction. If we have a pending message,
2090 * then we should already be in transaction->connections
2092 link = _dbus_list_get_first_link (&d->transaction_messages);
2093 _dbus_assert (link->data == to_send);
2094 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2095 while (link != NULL)
2097 MessageToSend *m = link->data;
2098 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2100 if (m->transaction == transaction)
2108 if (!_dbus_list_prepend (&transaction->connections, connection))
2110 _dbus_list_remove (&d->transaction_messages, to_send);
2111 message_to_send_free (connection, to_send);
2120 connection_cancel_transaction (DBusConnection *connection,
2121 BusTransaction *transaction)
2124 BusConnectionData *d;
2126 d = BUS_CONNECTION_DATA (connection);
2127 _dbus_assert (d != NULL);
2129 link = _dbus_list_get_first_link (&d->transaction_messages);
2130 while (link != NULL)
2132 MessageToSend *m = link->data;
2133 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2135 if (m->transaction == transaction)
2137 _dbus_list_remove_link (&d->transaction_messages,
2140 message_to_send_free (connection, m);
2148 bus_transaction_cancel_and_free (BusTransaction *transaction)
2150 DBusConnection *connection;
2152 _dbus_verbose ("TRANSACTION: cancelled\n");
2154 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2155 connection_cancel_transaction (connection, transaction);
2157 _dbus_assert (transaction->connections == NULL);
2159 _dbus_list_foreach (&transaction->cancel_hooks,
2160 cancel_hook_cancel, NULL);
2162 free_cancel_hooks (transaction);
2164 dbus_free (transaction);
2168 connection_execute_transaction (DBusConnection *connection,
2169 BusTransaction *transaction)
2172 BusConnectionData *d;
2174 d = BUS_CONNECTION_DATA (connection);
2175 _dbus_assert (d != NULL);
2177 /* Send the queue in order (FIFO) */
2178 link = _dbus_list_get_last_link (&d->transaction_messages);
2179 while (link != NULL)
2181 MessageToSend *m = link->data;
2182 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2184 if (m->transaction == transaction)
2186 _dbus_list_remove_link (&d->transaction_messages,
2189 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2191 dbus_connection_send_preallocated (connection,
2196 m->preallocated = NULL; /* so we don't double-free it */
2198 message_to_send_free (connection, m);
2206 bus_transaction_execute_and_free (BusTransaction *transaction)
2208 /* For each connection in transaction->connections
2211 DBusConnection *connection;
2213 _dbus_verbose ("TRANSACTION: executing\n");
2215 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2216 connection_execute_transaction (connection, transaction);
2218 _dbus_assert (transaction->connections == NULL);
2220 free_cancel_hooks (transaction);
2222 dbus_free (transaction);
2226 bus_connection_remove_transactions (DBusConnection *connection)
2228 MessageToSend *to_send;
2229 BusConnectionData *d;
2231 d = BUS_CONNECTION_DATA (connection);
2232 _dbus_assert (d != NULL);
2234 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2236 /* only has an effect for the first MessageToSend listing this transaction */
2237 _dbus_list_remove (&to_send->transaction->connections,
2240 _dbus_list_remove (&d->transaction_messages, to_send);
2241 message_to_send_free (connection, to_send);
2246 * Converts the DBusError to a message reply
2249 bus_transaction_send_error_reply (BusTransaction *transaction,
2250 DBusConnection *connection,
2251 const DBusError *error,
2252 DBusMessage *in_reply_to)
2256 _dbus_assert (error != NULL);
2257 _DBUS_ASSERT_ERROR_IS_SET (error);
2259 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2260 error->name, error->message);
2262 reply = dbus_message_new_error (in_reply_to,
2268 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2270 dbus_message_unref (reply);
2274 dbus_message_unref (reply);
2280 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2281 BusTransactionCancelFunction cancel_function,
2283 DBusFreeFunction free_data_function)
2287 ch = dbus_new (CancelHook, 1);
2291 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2292 cancel_function, data);
2294 ch->cancel_function = cancel_function;
2296 ch->free_data_function = free_data_function;
2298 /* It's important that the hooks get run in reverse order that they
2301 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2310 #ifdef DBUS_ENABLE_STATS
2312 bus_connections_get_n_active (BusConnections *connections)
2314 return connections->n_completed;
2318 bus_connections_get_n_incomplete (BusConnections *connections)
2320 return connections->n_incomplete;
2324 bus_connections_get_total_match_rules (BusConnections *connections)
2326 return connections->total_match_rules;
2330 bus_connections_get_peak_match_rules (BusConnections *connections)
2332 return connections->peak_match_rules;
2336 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2338 return connections->peak_match_rules_per_conn;
2342 bus_connections_get_total_bus_names (BusConnections *connections)
2344 return connections->total_bus_names;
2348 bus_connections_get_peak_bus_names (BusConnections *connections)
2350 return connections->peak_bus_names;
2354 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2356 return connections->peak_bus_names_per_conn;
2360 bus_connection_get_peak_match_rules (DBusConnection *connection)
2362 BusConnectionData *d;
2364 d = BUS_CONNECTION_DATA (connection);
2365 return d->peak_match_rules;
2369 bus_connection_get_peak_bus_names (DBusConnection *connection)
2371 BusConnectionData *d;
2373 d = BUS_CONNECTION_DATA (connection);
2374 return d->peak_bus_names;
2376 #endif /* DBUS_ENABLE_STATS */