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 */
67 static dbus_int32_t connection_data_slot = -1;
71 BusConnections *connections;
72 DBusList *link_in_connection_list;
73 DBusConnection *connection;
74 DBusList *services_owned;
76 DBusList *match_rules;
79 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
80 DBusMessage *oom_message;
81 DBusPreallocatedSend *oom_preallocated;
82 BusClientPolicy *policy;
84 char *cached_loginfo_string;
85 BusSELinuxID *selinux_id;
87 long connection_tv_sec; /**< Time when we connected (seconds component) */
88 long connection_tv_usec; /**< Time when we connected (microsec component) */
89 int stamp; /**< connections->stamp last time we were traversed */
92 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
96 static void bus_connection_drop_pending_replies (BusConnections *connections,
97 DBusConnection *connection);
99 static dbus_bool_t expire_incomplete_timeout (void *data);
101 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
104 connection_get_loop (DBusConnection *connection)
106 BusConnectionData *d;
108 d = BUS_CONNECTION_DATA (connection);
110 return bus_context_get_loop (d->connections->context);
115 get_connections_for_uid (BusConnections *connections,
121 /* val is NULL is 0 when it isn't in the hash yet */
123 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
126 current_count = _DBUS_POINTER_TO_INT (val);
128 return current_count;
132 adjust_connections_for_uid (BusConnections *connections,
138 current_count = get_connections_for_uid (connections, uid);
140 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
141 ": was %d adjustment %d making %d\n",
142 uid, current_count, adjustment, current_count + adjustment);
144 _dbus_assert (current_count >= 0);
146 current_count += adjustment;
148 _dbus_assert (current_count >= 0);
150 if (current_count == 0)
152 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
159 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
160 uid, _DBUS_INT_TO_POINTER (current_count));
162 /* only positive adjustment can fail as otherwise
163 * a hash entry should already exist
165 _dbus_assert (adjustment > 0 ||
166 (adjustment <= 0 && retval));
173 bus_connection_disconnected (DBusConnection *connection)
175 BusConnectionData *d;
177 BusMatchmaker *matchmaker;
179 d = BUS_CONNECTION_DATA (connection);
180 _dbus_assert (d != NULL);
182 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
183 d->name ? d->name : "(inactive)");
185 /* Delete our match rules */
186 if (d->n_match_rules > 0)
188 matchmaker = bus_context_get_matchmaker (d->connections->context);
189 bus_matchmaker_disconnected (matchmaker, connection);
192 /* Drop any service ownership. Unfortunately, this requires
193 * memory allocation and there doesn't seem to be a good way to
194 * handle it other than sleeping; we can't "fail" the operation of
195 * disconnecting a client, and preallocating a broadcast "service is
196 * now gone" message for every client-service pair seems kind of
199 while ((service = _dbus_list_get_last (&d->services_owned)))
201 BusTransaction *transaction;
206 dbus_error_init (&error);
208 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
209 _dbus_wait_for_memory ();
211 if (!bus_service_remove_owner (service, connection,
212 transaction, &error))
214 _DBUS_ASSERT_ERROR_IS_SET (&error);
216 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
218 dbus_error_free (&error);
219 bus_transaction_cancel_and_free (transaction);
220 _dbus_wait_for_memory ();
225 _dbus_verbose ("Failed to remove service owner: %s %s\n",
226 error.name, error.message);
227 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
231 bus_transaction_execute_and_free (transaction);
234 bus_dispatch_remove_connection (connection);
236 /* no more watching */
237 if (!dbus_connection_set_watch_functions (connection,
241 _dbus_assert_not_reached ("setting watch functions to NULL failed");
243 if (!dbus_connection_set_timeout_functions (connection,
247 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
249 dbus_connection_set_unix_user_function (connection,
251 dbus_connection_set_windows_user_function (connection,
254 dbus_connection_set_dispatch_status_function (connection,
257 bus_connection_remove_transactions (connection);
259 if (d->link_in_connection_list != NULL)
265 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
266 d->link_in_connection_list = NULL;
267 d->connections->n_completed -= 1;
269 if (dbus_connection_get_unix_user (connection, &uid))
271 if (!adjust_connections_for_uid (d->connections,
273 _dbus_assert_not_reached ("adjusting downward should never fail");
278 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
279 d->link_in_connection_list = NULL;
280 d->connections->n_incomplete -= 1;
283 _dbus_assert (d->connections->n_incomplete >= 0);
284 _dbus_assert (d->connections->n_completed >= 0);
287 bus_connection_drop_pending_replies (d->connections, connection);
289 /* frees "d" as side effect */
290 dbus_connection_set_data (connection,
291 connection_data_slot,
294 dbus_connection_unref (connection);
298 add_connection_watch (DBusWatch *watch,
301 DBusConnection *connection = data;
303 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
307 remove_connection_watch (DBusWatch *watch,
310 DBusConnection *connection = data;
312 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
316 toggle_connection_watch (DBusWatch *watch,
319 DBusConnection *connection = data;
321 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
325 add_connection_timeout (DBusTimeout *timeout,
328 DBusConnection *connection = data;
330 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
334 remove_connection_timeout (DBusTimeout *timeout,
337 DBusConnection *connection = data;
339 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
343 dispatch_status_function (DBusConnection *connection,
344 DBusDispatchStatus new_status,
347 DBusLoop *loop = data;
349 if (new_status != DBUS_DISPATCH_COMPLETE)
351 while (!_dbus_loop_queue_dispatch (loop, connection))
352 _dbus_wait_for_memory ();
357 allow_unix_user_function (DBusConnection *connection,
361 BusConnectionData *d;
363 d = BUS_CONNECTION_DATA (connection);
365 _dbus_assert (d != NULL);
367 return bus_context_allow_unix_user (d->connections->context, uid);
371 free_connection_data (void *data)
373 BusConnectionData *d = data;
375 /* services_owned should be NULL since we should be disconnected */
376 _dbus_assert (d->services_owned == NULL);
377 _dbus_assert (d->n_services_owned == 0);
379 _dbus_assert (d->transaction_messages == NULL);
381 if (d->oom_preallocated)
382 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
385 dbus_message_unref (d->oom_message);
388 bus_client_policy_unref (d->policy);
391 bus_selinux_id_unref (d->selinux_id);
393 dbus_free (d->cached_loginfo_string);
401 call_timeout_callback (DBusTimeout *timeout,
404 /* can return FALSE on OOM but we just let it fire again later */
405 dbus_timeout_handle (timeout);
409 bus_connections_new (BusContext *context)
411 BusConnections *connections;
413 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
416 connections = dbus_new0 (BusConnections, 1);
417 if (connections == NULL)
420 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
422 if (connections->completed_by_user == NULL)
425 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
426 expire_incomplete_timeout,
428 if (connections->expire_timeout == NULL)
431 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
433 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
434 bus_context_get_reply_timeout (context),
435 bus_pending_reply_expired,
437 if (connections->pending_replies == NULL)
440 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
441 connections->expire_timeout))
444 connections->refcount = 1;
445 connections->context = context;
450 bus_expire_list_free (connections->pending_replies);
452 _dbus_timeout_unref (connections->expire_timeout);
454 _dbus_hash_table_unref (connections->completed_by_user);
456 dbus_free (connections);
458 dbus_connection_free_data_slot (&connection_data_slot);
464 bus_connections_ref (BusConnections *connections)
466 _dbus_assert (connections->refcount > 0);
467 connections->refcount += 1;
473 bus_connections_unref (BusConnections *connections)
475 _dbus_assert (connections->refcount > 0);
476 connections->refcount -= 1;
477 if (connections->refcount == 0)
479 /* drop all incomplete */
480 while (connections->incomplete != NULL)
482 DBusConnection *connection;
484 connection = connections->incomplete->data;
486 dbus_connection_ref (connection);
487 dbus_connection_close (connection);
488 bus_connection_disconnected (connection);
489 dbus_connection_unref (connection);
492 _dbus_assert (connections->n_incomplete == 0);
494 /* drop all real connections */
495 while (connections->completed != NULL)
497 DBusConnection *connection;
499 connection = connections->completed->data;
501 dbus_connection_ref (connection);
502 dbus_connection_close (connection);
503 bus_connection_disconnected (connection);
504 dbus_connection_unref (connection);
507 _dbus_assert (connections->n_completed == 0);
509 bus_expire_list_free (connections->pending_replies);
511 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
512 connections->expire_timeout);
514 _dbus_timeout_unref (connections->expire_timeout);
516 _dbus_hash_table_unref (connections->completed_by_user);
518 dbus_free (connections);
520 dbus_connection_free_data_slot (&connection_data_slot);
524 /* Used for logging */
526 cache_peer_loginfo_string (BusConnectionData *d,
527 DBusConnection *connection)
529 DBusString loginfo_buf;
533 dbus_bool_t prev_added;
535 if (!_dbus_string_init (&loginfo_buf))
539 if (dbus_connection_get_unix_user (connection, &uid))
541 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
547 if (dbus_connection_get_unix_process_id (connection, &pid))
551 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
554 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
556 /* Ignore errors here; we may not have permissions to read the
558 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
559 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
563 if (dbus_connection_get_windows_user (connection, &windows_sid))
565 dbus_bool_t did_append;
566 did_append = _dbus_string_append_printf (&loginfo_buf,
567 "sid=\"%s\" ", windows_sid);
568 dbus_free (windows_sid);
573 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
576 _dbus_string_free (&loginfo_buf);
580 _dbus_string_free (&loginfo_buf);
585 bus_connections_setup_connection (BusConnections *connections,
586 DBusConnection *connection)
589 BusConnectionData *d;
594 d = dbus_new0 (BusConnectionData, 1);
599 d->connections = connections;
600 d->connection = connection;
602 _dbus_get_current_time (&d->connection_tv_sec,
603 &d->connection_tv_usec);
605 _dbus_assert (connection_data_slot >= 0);
607 if (!dbus_connection_set_data (connection,
608 connection_data_slot,
609 d, free_connection_data))
615 dbus_connection_set_route_peer_messages (connection, TRUE);
619 dbus_error_init (&error);
620 d->selinux_id = bus_selinux_init_connection_id (connection,
622 if (dbus_error_is_set (&error))
624 /* This is a bit bogus because we pretend all errors
625 * are OOM; this is done because we know that in bus.c
626 * an OOM error disconnects the connection, which is
627 * the same thing we want on any other error.
629 dbus_error_free (&error);
633 if (!dbus_connection_set_watch_functions (connection,
634 add_connection_watch,
635 remove_connection_watch,
636 toggle_connection_watch,
641 if (!dbus_connection_set_timeout_functions (connection,
642 add_connection_timeout,
643 remove_connection_timeout,
648 /* For now we don't need to set a Windows user function because
649 * there are no policies in the config file controlling what
650 * Windows users can connect. The default 'same user that owns the
651 * bus can connect' behavior of DBusConnection is fine on Windows.
653 dbus_connection_set_unix_user_function (connection,
654 allow_unix_user_function,
657 dbus_connection_set_dispatch_status_function (connection,
658 dispatch_status_function,
659 bus_context_get_loop (connections->context),
662 d->link_in_connection_list = _dbus_list_alloc_link (connection);
663 if (d->link_in_connection_list == NULL)
666 /* Setup the connection with the dispatcher */
667 if (!bus_dispatch_add_connection (connection))
670 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
672 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
674 bus_dispatch_remove_connection (connection);
679 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
680 connections->n_incomplete += 1;
682 dbus_connection_ref (connection);
684 /* Note that we might disconnect ourselves here, but it only takes
685 * effect on return to the main loop. We call this to free up
686 * expired connections if possible, and to queue the timeout for our
689 bus_connections_expire_incomplete (connections);
691 /* And we might also disconnect ourselves here, but again it
692 * only takes effect on return to main loop.
694 if (connections->n_incomplete >
695 bus_context_get_max_incomplete_connections (connections->context))
697 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
699 _dbus_assert (connections->incomplete != NULL);
700 /* Disconnect the oldest unauthenticated connection. FIXME
701 * would it be more secure to drop a *random* connection? This
702 * algorithm seems to mean that if someone can create new
703 * connections quickly enough, they can keep anyone else from
704 * completing authentication. But random may or may not really
705 * help with that, a more elaborate solution might be required.
707 dbus_connection_close (connections->incomplete->data);
716 bus_selinux_id_unref (d->selinux_id);
717 d->selinux_id = NULL;
719 if (!dbus_connection_set_watch_functions (connection,
723 _dbus_assert_not_reached ("setting watch functions to NULL failed");
725 if (!dbus_connection_set_timeout_functions (connection,
729 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
731 dbus_connection_set_unix_user_function (connection,
734 dbus_connection_set_windows_user_function (connection,
737 dbus_connection_set_dispatch_status_function (connection,
740 if (d->link_in_connection_list != NULL)
742 _dbus_assert (d->link_in_connection_list->next == NULL);
743 _dbus_assert (d->link_in_connection_list->prev == NULL);
744 _dbus_list_free_link (d->link_in_connection_list);
745 d->link_in_connection_list = NULL;
748 if (!dbus_connection_set_data (connection,
749 connection_data_slot,
751 _dbus_assert_not_reached ("failed to set connection data to null");
753 /* "d" has now been freed */
760 bus_connections_expire_incomplete (BusConnections *connections)
766 if (connections->incomplete != NULL)
768 long tv_sec, tv_usec;
772 _dbus_get_current_time (&tv_sec, &tv_usec);
773 auth_timeout = bus_context_get_auth_timeout (connections->context);
775 link = _dbus_list_get_first_link (&connections->incomplete);
778 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
779 DBusConnection *connection;
780 BusConnectionData *d;
783 connection = link->data;
785 d = BUS_CONNECTION_DATA (connection);
787 _dbus_assert (d != NULL);
789 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
790 d->connection_tv_usec,
793 if (elapsed >= (double) auth_timeout)
795 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
796 dbus_connection_close (connection);
800 /* We can end the loop, since the connections are in oldest-first order */
801 next_interval = ((double)auth_timeout) - elapsed;
802 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
803 connection, next_interval);
812 bus_expire_timeout_set_interval (connections->expire_timeout,
817 expire_incomplete_timeout (void *data)
819 BusConnections *connections = data;
821 _dbus_verbose ("Running\n");
823 /* note that this may remove the timeout */
824 bus_connections_expire_incomplete (connections);
830 bus_connection_get_unix_groups (DBusConnection *connection,
831 unsigned long **groups,
835 BusConnectionData *d;
838 d = BUS_CONNECTION_DATA (connection);
840 _dbus_assert (d != NULL);
845 if (dbus_connection_get_unix_user (connection, &uid))
847 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
849 _dbus_verbose ("Did not get any groups for UID %lu\n",
855 _dbus_verbose ("Got %d groups for UID %lu\n",
861 return TRUE; /* successfully got 0 groups */
865 bus_connection_is_in_unix_group (DBusConnection *connection,
869 unsigned long *group_ids;
872 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
877 while (i < n_group_ids)
879 if (group_ids[i] == gid)
881 dbus_free (group_ids);
887 dbus_free (group_ids);
892 bus_connection_get_loginfo (DBusConnection *connection)
894 BusConnectionData *d;
896 d = BUS_CONNECTION_DATA (connection);
898 if (!bus_connection_is_active (connection))
900 return d->cached_loginfo_string;
904 bus_connection_get_policy (DBusConnection *connection)
906 BusConnectionData *d;
908 d = BUS_CONNECTION_DATA (connection);
910 _dbus_assert (d != NULL);
911 _dbus_assert (d->policy != NULL);
917 foreach_active (BusConnections *connections,
918 BusConnectionForeachFunction function,
923 link = _dbus_list_get_first_link (&connections->completed);
926 DBusConnection *connection = link->data;
927 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
929 if (!(* function) (connection, data))
939 foreach_inactive (BusConnections *connections,
940 BusConnectionForeachFunction function,
945 link = _dbus_list_get_first_link (&connections->incomplete);
948 DBusConnection *connection = link->data;
949 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
951 if (!(* function) (connection, data))
961 * Calls function on each active connection; if the function returns
962 * #FALSE, stops iterating. Active connections are authenticated
963 * and have sent a Hello message.
965 * @param connections the connections object
966 * @param function the function
967 * @param data data to pass to it as a second arg
970 bus_connections_foreach_active (BusConnections *connections,
971 BusConnectionForeachFunction function,
974 foreach_active (connections, function, data);
978 * Calls function on each connection; if the function returns
979 * #FALSE, stops iterating.
981 * @param connections the connections object
982 * @param function the function
983 * @param data data to pass to it as a second arg
986 bus_connections_foreach (BusConnections *connections,
987 BusConnectionForeachFunction function,
990 if (!foreach_active (connections, function, data))
993 foreach_inactive (connections, function, data);
997 bus_connections_get_context (BusConnections *connections)
999 return connections->context;
1003 * This is used to avoid covering the same connection twice when
1004 * traversing connections. Note that it assumes we will
1005 * bus_connection_mark_stamp() each connection at least once per
1006 * INT_MAX increments of the global stamp, or wraparound would break
1010 bus_connections_increment_stamp (BusConnections *connections)
1012 connections->stamp += 1;
1015 /* Mark connection with current stamp, return TRUE if it
1016 * didn't already have that stamp
1019 bus_connection_mark_stamp (DBusConnection *connection)
1021 BusConnectionData *d;
1023 d = BUS_CONNECTION_DATA (connection);
1025 _dbus_assert (d != NULL);
1027 if (d->stamp == d->connections->stamp)
1031 d->stamp = d->connections->stamp;
1037 bus_connection_get_context (DBusConnection *connection)
1039 BusConnectionData *d;
1041 d = BUS_CONNECTION_DATA (connection);
1043 _dbus_assert (d != NULL);
1045 return d->connections->context;
1049 bus_connection_get_connections (DBusConnection *connection)
1051 BusConnectionData *d;
1053 d = BUS_CONNECTION_DATA (connection);
1055 _dbus_assert (d != NULL);
1057 return d->connections;
1061 bus_connection_get_registry (DBusConnection *connection)
1063 BusConnectionData *d;
1065 d = BUS_CONNECTION_DATA (connection);
1067 _dbus_assert (d != NULL);
1069 return bus_context_get_registry (d->connections->context);
1073 bus_connection_get_activation (DBusConnection *connection)
1075 BusConnectionData *d;
1077 d = BUS_CONNECTION_DATA (connection);
1079 _dbus_assert (d != NULL);
1081 return bus_context_get_activation (d->connections->context);
1085 bus_connection_get_matchmaker (DBusConnection *connection)
1087 BusConnectionData *d;
1089 d = BUS_CONNECTION_DATA (connection);
1091 _dbus_assert (d != NULL);
1093 return bus_context_get_matchmaker (d->connections->context);
1097 bus_connection_get_selinux_id (DBusConnection *connection)
1099 BusConnectionData *d;
1101 d = BUS_CONNECTION_DATA (connection);
1103 _dbus_assert (d != NULL);
1105 return d->selinux_id;
1109 * Checks whether the connection is registered with the message bus.
1111 * @param connection the connection
1112 * @returns #TRUE if we're an active message bus participant
1115 bus_connection_is_active (DBusConnection *connection)
1117 BusConnectionData *d;
1119 d = BUS_CONNECTION_DATA (connection);
1121 return d != NULL && d->name != NULL;
1125 bus_connection_preallocate_oom_error (DBusConnection *connection)
1127 DBusMessage *message;
1128 DBusPreallocatedSend *preallocated;
1129 BusConnectionData *d;
1131 d = BUS_CONNECTION_DATA (connection);
1133 _dbus_assert (d != NULL);
1135 if (d->oom_preallocated != NULL)
1138 preallocated = dbus_connection_preallocate_send (connection);
1139 if (preallocated == NULL)
1142 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1144 if (message == NULL)
1146 dbus_connection_free_preallocated_send (connection, preallocated);
1150 /* d->name may be NULL, but that is OK */
1151 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1152 !dbus_message_set_destination (message, d->name) ||
1153 !dbus_message_set_sender (message,
1156 dbus_connection_free_preallocated_send (connection, preallocated);
1157 dbus_message_unref (message);
1161 /* set reply serial to placeholder value just so space is already allocated
1164 if (!dbus_message_set_reply_serial (message, 14))
1166 dbus_connection_free_preallocated_send (connection, preallocated);
1167 dbus_message_unref (message);
1171 d->oom_message = message;
1172 d->oom_preallocated = preallocated;
1178 bus_connection_send_oom_error (DBusConnection *connection,
1179 DBusMessage *in_reply_to)
1181 BusConnectionData *d;
1183 d = BUS_CONNECTION_DATA (connection);
1185 _dbus_assert (d != NULL);
1186 _dbus_assert (d->oom_message != NULL);
1188 /* should always succeed since we set it to a placeholder earlier */
1189 if (!dbus_message_set_reply_serial (d->oom_message,
1190 dbus_message_get_serial (in_reply_to)))
1191 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1193 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1195 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1196 d->oom_message, NULL);
1198 dbus_message_unref (d->oom_message);
1199 d->oom_message = NULL;
1200 d->oom_preallocated = NULL;
1204 bus_connection_add_match_rule_link (DBusConnection *connection,
1207 BusConnectionData *d;
1209 d = BUS_CONNECTION_DATA (connection);
1210 _dbus_assert (d != NULL);
1212 _dbus_list_append_link (&d->match_rules, link);
1214 d->n_match_rules += 1;
1218 bus_connection_add_match_rule (DBusConnection *connection,
1223 link = _dbus_list_alloc_link (rule);
1228 bus_connection_add_match_rule_link (connection, link);
1234 bus_connection_remove_match_rule (DBusConnection *connection,
1237 BusConnectionData *d;
1239 d = BUS_CONNECTION_DATA (connection);
1240 _dbus_assert (d != NULL);
1242 _dbus_list_remove_last (&d->match_rules, rule);
1244 d->n_match_rules -= 1;
1245 _dbus_assert (d->n_match_rules >= 0);
1249 bus_connection_get_n_match_rules (DBusConnection *connection)
1251 BusConnectionData *d;
1253 d = BUS_CONNECTION_DATA (connection);
1254 _dbus_assert (d != NULL);
1256 return d->n_match_rules;
1260 bus_connection_add_owned_service_link (DBusConnection *connection,
1263 BusConnectionData *d;
1265 d = BUS_CONNECTION_DATA (connection);
1266 _dbus_assert (d != NULL);
1268 _dbus_list_append_link (&d->services_owned, link);
1270 d->n_services_owned += 1;
1274 bus_connection_add_owned_service (DBusConnection *connection,
1275 BusService *service)
1279 link = _dbus_list_alloc_link (service);
1284 bus_connection_add_owned_service_link (connection, link);
1290 bus_connection_remove_owned_service (DBusConnection *connection,
1291 BusService *service)
1293 BusConnectionData *d;
1295 d = BUS_CONNECTION_DATA (connection);
1296 _dbus_assert (d != NULL);
1298 _dbus_list_remove_last (&d->services_owned, service);
1300 d->n_services_owned -= 1;
1301 _dbus_assert (d->n_services_owned >= 0);
1305 bus_connection_get_n_services_owned (DBusConnection *connection)
1307 BusConnectionData *d;
1309 d = BUS_CONNECTION_DATA (connection);
1310 _dbus_assert (d != NULL);
1312 return d->n_services_owned;
1316 bus_connection_complete (DBusConnection *connection,
1317 const DBusString *name,
1320 BusConnectionData *d;
1323 d = BUS_CONNECTION_DATA (connection);
1324 _dbus_assert (d != NULL);
1325 _dbus_assert (d->name == NULL);
1326 _dbus_assert (d->policy == NULL);
1328 _dbus_assert (!bus_connection_is_active (connection));
1330 if (!_dbus_string_copy_data (name, &d->name))
1332 BUS_SET_OOM (error);
1336 _dbus_assert (d->name != NULL);
1338 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1340 d->policy = bus_context_create_client_policy (d->connections->context,
1344 /* we may have a NULL policy on OOM or error getting list of
1345 * groups for a user. In the latter case we don't handle it so
1346 * well currently, as it will just keep failing over and over.
1349 if (d->policy == NULL)
1351 _dbus_verbose ("Failed to create security policy for connection %p\n",
1353 _DBUS_ASSERT_ERROR_IS_SET (error);
1354 dbus_free (d->name);
1359 if (dbus_connection_get_unix_user (connection, &uid))
1361 if (!adjust_connections_for_uid (d->connections,
1366 /* Create and cache a string which holds information about the
1367 * peer process; used for logging purposes.
1369 if (!cache_peer_loginfo_string (d, connection))
1372 /* Now the connection is active, move it between lists */
1373 _dbus_list_unlink (&d->connections->incomplete,
1374 d->link_in_connection_list);
1375 d->connections->n_incomplete -= 1;
1376 _dbus_list_append_link (&d->connections->completed,
1377 d->link_in_connection_list);
1378 d->connections->n_completed += 1;
1380 _dbus_assert (d->connections->n_incomplete >= 0);
1381 _dbus_assert (d->connections->n_completed > 0);
1383 /* See if we can remove the timeout */
1384 bus_connections_expire_incomplete (d->connections);
1386 _dbus_assert (bus_connection_is_active (connection));
1390 BUS_SET_OOM (error);
1391 dbus_free (d->name);
1394 bus_client_policy_unref (d->policy);
1400 bus_connection_get_name (DBusConnection *connection)
1402 BusConnectionData *d;
1404 d = BUS_CONNECTION_DATA (connection);
1405 _dbus_assert (d != NULL);
1411 * Check whether completing the passed-in connection would
1412 * exceed limits, and if so set error and return #FALSE
1415 bus_connections_check_limits (BusConnections *connections,
1416 DBusConnection *requesting_completion,
1419 BusConnectionData *d;
1422 d = BUS_CONNECTION_DATA (requesting_completion);
1423 _dbus_assert (d != NULL);
1425 _dbus_assert (d->name == NULL);
1427 if (connections->n_completed >=
1428 bus_context_get_max_completed_connections (connections->context))
1430 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1431 "The maximum number of active connections has been reached");
1435 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1437 if (get_connections_for_uid (connections, uid) >=
1438 bus_context_get_max_connections_per_user (connections->context))
1440 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1441 "The maximum number of active connections for UID %lu has been reached",
1451 bus_pending_reply_free (BusPendingReply *pending)
1453 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1455 pending->will_send_reply,
1456 pending->will_get_reply,
1457 pending->reply_serial);
1459 dbus_free (pending);
1463 bus_pending_reply_send_no_reply (BusConnections *connections,
1464 BusTransaction *transaction,
1465 BusPendingReply *pending)
1467 DBusMessage *message;
1468 DBusMessageIter iter;
1474 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1475 if (message == NULL)
1478 dbus_message_set_no_reply (message, TRUE);
1480 if (!dbus_message_set_reply_serial (message,
1481 pending->reply_serial))
1484 if (!dbus_message_set_error_name (message,
1485 DBUS_ERROR_NO_REPLY))
1488 errmsg = "Message did not receive a reply (timeout by message bus)";
1489 dbus_message_iter_init_append (message, &iter);
1490 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1493 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1500 dbus_message_unref (message);
1505 bus_pending_reply_expired (BusExpireList *list,
1509 BusPendingReply *pending = link->data;
1510 BusConnections *connections = data;
1511 BusTransaction *transaction;
1513 /* No reply is forthcoming. So nuke it if we can. If not,
1514 * leave it in the list to try expiring again later when we
1518 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1520 pending->will_send_reply,
1521 pending->will_get_reply,
1522 pending->reply_serial);
1524 transaction = bus_transaction_new (connections->context);
1525 if (transaction == NULL)
1528 if (!bus_pending_reply_send_no_reply (connections,
1532 bus_transaction_cancel_and_free (transaction);
1536 bus_expire_list_remove_link (connections->pending_replies, link);
1538 bus_pending_reply_free (pending);
1539 bus_transaction_execute_and_free (transaction);
1545 bus_connection_drop_pending_replies (BusConnections *connections,
1546 DBusConnection *connection)
1548 /* The DBusConnection is almost 100% finalized here, so you can't
1549 * do anything with it except check for pointer equality
1553 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1556 link = bus_expire_list_get_first_link (connections->pending_replies);
1557 while (link != NULL)
1560 BusPendingReply *pending;
1562 next = bus_expire_list_get_next_link (connections->pending_replies,
1564 pending = link->data;
1566 if (pending->will_get_reply == connection)
1568 /* We don't need to track this pending reply anymore */
1570 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1572 pending->will_send_reply,
1573 pending->will_get_reply,
1574 pending->reply_serial);
1576 bus_expire_list_remove_link (connections->pending_replies,
1578 bus_pending_reply_free (pending);
1580 else if (pending->will_send_reply == connection)
1582 /* The reply isn't going to be sent, so set things
1583 * up so it will be expired right away
1585 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1587 pending->will_send_reply,
1588 pending->will_get_reply,
1589 pending->reply_serial);
1591 pending->will_send_reply = NULL;
1592 pending->expire_item.added_tv_sec = 0;
1593 pending->expire_item.added_tv_usec = 0;
1595 bus_expire_list_recheck_immediately (connections->pending_replies);
1605 BusPendingReply *pending;
1606 BusConnections *connections;
1607 } CancelPendingReplyData;
1610 cancel_pending_reply (void *data)
1612 CancelPendingReplyData *d = data;
1614 _dbus_verbose ("d = %p\n", d);
1616 if (!bus_expire_list_remove (d->connections->pending_replies,
1617 &d->pending->expire_item))
1618 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1620 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1624 cancel_pending_reply_data_free (void *data)
1626 CancelPendingReplyData *d = data;
1628 _dbus_verbose ("d = %p\n", d);
1630 /* d->pending should be either freed or still
1631 * in the list of pending replies (owned by someone
1639 * Record that a reply is allowed; return TRUE on success.
1642 bus_connections_expect_reply (BusConnections *connections,
1643 BusTransaction *transaction,
1644 DBusConnection *will_get_reply,
1645 DBusConnection *will_send_reply,
1646 DBusMessage *reply_to_this,
1649 BusPendingReply *pending;
1650 dbus_uint32_t reply_serial;
1652 CancelPendingReplyData *cprd;
1655 _dbus_assert (will_get_reply != NULL);
1656 _dbus_assert (will_send_reply != NULL);
1657 _dbus_assert (reply_to_this != NULL);
1659 if (dbus_message_get_no_reply (reply_to_this))
1660 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1662 reply_serial = dbus_message_get_serial (reply_to_this);
1664 link = bus_expire_list_get_first_link (connections->pending_replies);
1666 while (link != NULL)
1668 pending = link->data;
1670 if (pending->reply_serial == reply_serial &&
1671 pending->will_get_reply == will_get_reply &&
1672 pending->will_send_reply == will_send_reply)
1674 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1675 "Message has the same reply serial as a currently-outstanding existing method call");
1679 link = bus_expire_list_get_next_link (connections->pending_replies,
1681 if (pending->will_get_reply == will_get_reply)
1686 bus_context_get_max_replies_per_connection (connections->context))
1688 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1689 "The maximum number of pending replies per connection has been reached");
1693 pending = dbus_new0 (BusPendingReply, 1);
1694 if (pending == NULL)
1696 BUS_SET_OOM (error);
1700 #ifdef DBUS_ENABLE_VERBOSE_MODE
1701 /* so we can see a not-yet-added pending reply */
1702 pending->expire_item.added_tv_sec = 1;
1703 pending->expire_item.added_tv_usec = 1;
1706 pending->will_get_reply = will_get_reply;
1707 pending->will_send_reply = will_send_reply;
1708 pending->reply_serial = reply_serial;
1710 cprd = dbus_new0 (CancelPendingReplyData, 1);
1713 BUS_SET_OOM (error);
1714 bus_pending_reply_free (pending);
1718 if (!bus_expire_list_add (connections->pending_replies,
1719 &pending->expire_item))
1721 BUS_SET_OOM (error);
1723 bus_pending_reply_free (pending);
1727 if (!bus_transaction_add_cancel_hook (transaction,
1728 cancel_pending_reply,
1730 cancel_pending_reply_data_free))
1732 BUS_SET_OOM (error);
1733 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1735 bus_pending_reply_free (pending);
1739 cprd->pending = pending;
1740 cprd->connections = connections;
1742 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1743 &pending->expire_item.added_tv_usec);
1745 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1747 pending->will_send_reply,
1748 pending->will_get_reply,
1749 pending->reply_serial);
1757 BusConnections *connections;
1758 } CheckPendingReplyData;
1761 cancel_check_pending_reply (void *data)
1763 CheckPendingReplyData *d = data;
1765 _dbus_verbose ("d = %p\n",d);
1767 bus_expire_list_add_link (d->connections->pending_replies,
1773 check_pending_reply_data_free (void *data)
1775 CheckPendingReplyData *d = data;
1777 _dbus_verbose ("d = %p\n",d);
1779 if (d->link != NULL)
1781 BusPendingReply *pending = d->link->data;
1783 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1784 &pending->expire_item));
1786 bus_pending_reply_free (pending);
1787 _dbus_list_free_link (d->link);
1794 * Check whether a reply is allowed, remove BusPendingReply
1795 * if so, return TRUE if so.
1798 bus_connections_check_reply (BusConnections *connections,
1799 BusTransaction *transaction,
1800 DBusConnection *sending_reply,
1801 DBusConnection *receiving_reply,
1805 CheckPendingReplyData *cprd;
1807 dbus_uint32_t reply_serial;
1809 _dbus_assert (sending_reply != NULL);
1810 _dbus_assert (receiving_reply != NULL);
1812 reply_serial = dbus_message_get_reply_serial (reply);
1814 link = bus_expire_list_get_first_link (connections->pending_replies);
1815 while (link != NULL)
1817 BusPendingReply *pending = link->data;
1819 if (pending->reply_serial == reply_serial &&
1820 pending->will_get_reply == receiving_reply &&
1821 pending->will_send_reply == sending_reply)
1823 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1827 link = bus_expire_list_get_next_link (connections->pending_replies,
1833 _dbus_verbose ("No pending reply expected\n");
1838 cprd = dbus_new0 (CheckPendingReplyData, 1);
1841 BUS_SET_OOM (error);
1845 if (!bus_transaction_add_cancel_hook (transaction,
1846 cancel_check_pending_reply,
1848 check_pending_reply_data_free))
1850 BUS_SET_OOM (error);
1856 cprd->connections = connections;
1858 bus_expire_list_unlink (connections->pending_replies,
1861 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1869 * Note that this is fairly fragile; in particular, don't try to use
1870 * one transaction across any main loop iterations.
1875 BusTransaction *transaction;
1876 DBusMessage *message;
1877 DBusPreallocatedSend *preallocated;
1882 BusTransactionCancelFunction cancel_function;
1883 DBusFreeFunction free_data_function;
1887 struct BusTransaction
1889 DBusList *connections;
1890 BusContext *context;
1891 DBusList *cancel_hooks;
1895 message_to_send_free (DBusConnection *connection,
1896 MessageToSend *to_send)
1898 if (to_send->message)
1899 dbus_message_unref (to_send->message);
1901 if (to_send->preallocated)
1902 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1904 dbus_free (to_send);
1908 cancel_hook_cancel (void *element,
1911 CancelHook *ch = element;
1913 _dbus_verbose ("Running transaction cancel hook\n");
1915 if (ch->cancel_function)
1916 (* ch->cancel_function) (ch->data);
1920 cancel_hook_free (void *element,
1923 CancelHook *ch = element;
1925 if (ch->free_data_function)
1926 (* ch->free_data_function) (ch->data);
1932 free_cancel_hooks (BusTransaction *transaction)
1934 _dbus_list_foreach (&transaction->cancel_hooks,
1935 cancel_hook_free, NULL);
1937 _dbus_list_clear (&transaction->cancel_hooks);
1941 bus_transaction_new (BusContext *context)
1943 BusTransaction *transaction;
1945 transaction = dbus_new0 (BusTransaction, 1);
1946 if (transaction == NULL)
1949 transaction->context = context;
1955 bus_transaction_get_context (BusTransaction *transaction)
1957 return transaction->context;
1961 bus_transaction_get_connections (BusTransaction *transaction)
1963 return bus_context_get_connections (transaction->context);
1967 bus_transaction_send_from_driver (BusTransaction *transaction,
1968 DBusConnection *connection,
1969 DBusMessage *message)
1971 /* We have to set the sender to the driver, and have
1972 * to check security policy since it was not done in
1975 _dbus_verbose ("Sending %s %s %s from driver\n",
1976 dbus_message_get_interface (message) ?
1977 dbus_message_get_interface (message) : "(no interface)",
1978 dbus_message_get_member (message) ?
1979 dbus_message_get_member (message) : "(no member)",
1980 dbus_message_get_error_name (message) ?
1981 dbus_message_get_error_name (message) : "(no error name)");
1983 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
1986 if (bus_connection_is_active (connection))
1988 if (!dbus_message_set_destination (message,
1989 bus_connection_get_name (connection)))
1993 /* bus driver never wants a reply */
1994 dbus_message_set_no_reply (message, TRUE);
1996 /* If security policy doesn't allow the message, we silently
1997 * eat it; the driver doesn't care about getting a reply.
1999 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2001 NULL, connection, connection, message, NULL))
2004 return bus_transaction_send (transaction, connection, message);
2008 bus_transaction_send (BusTransaction *transaction,
2009 DBusConnection *connection,
2010 DBusMessage *message)
2012 MessageToSend *to_send;
2013 BusConnectionData *d;
2016 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2017 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2018 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2020 dbus_message_get_interface (message) ?
2021 dbus_message_get_interface (message) : "(unset)",
2022 dbus_message_get_member (message) ?
2023 dbus_message_get_member (message) : "(unset)",
2024 dbus_message_get_error_name (message) ?
2025 dbus_message_get_error_name (message) : "(unset)",
2026 dbus_connection_get_is_connected (connection) ?
2027 "" : " (disconnected)");
2029 _dbus_assert (dbus_message_get_sender (message) != NULL);
2031 if (!dbus_connection_get_is_connected (connection))
2032 return TRUE; /* silently ignore disconnected connections */
2034 d = BUS_CONNECTION_DATA (connection);
2035 _dbus_assert (d != NULL);
2037 to_send = dbus_new (MessageToSend, 1);
2038 if (to_send == NULL)
2043 to_send->preallocated = dbus_connection_preallocate_send (connection);
2044 if (to_send->preallocated == NULL)
2046 dbus_free (to_send);
2050 dbus_message_ref (message);
2051 to_send->message = message;
2052 to_send->transaction = transaction;
2054 _dbus_verbose ("about to prepend message\n");
2056 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2058 message_to_send_free (connection, to_send);
2062 _dbus_verbose ("prepended message\n");
2064 /* See if we already had this connection in the list
2065 * for this transaction. If we have a pending message,
2066 * then we should already be in transaction->connections
2068 link = _dbus_list_get_first_link (&d->transaction_messages);
2069 _dbus_assert (link->data == to_send);
2070 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2071 while (link != NULL)
2073 MessageToSend *m = link->data;
2074 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2076 if (m->transaction == transaction)
2084 if (!_dbus_list_prepend (&transaction->connections, connection))
2086 _dbus_list_remove (&d->transaction_messages, to_send);
2087 message_to_send_free (connection, to_send);
2096 connection_cancel_transaction (DBusConnection *connection,
2097 BusTransaction *transaction)
2100 BusConnectionData *d;
2102 d = BUS_CONNECTION_DATA (connection);
2103 _dbus_assert (d != NULL);
2105 link = _dbus_list_get_first_link (&d->transaction_messages);
2106 while (link != NULL)
2108 MessageToSend *m = link->data;
2109 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2111 if (m->transaction == transaction)
2113 _dbus_list_remove_link (&d->transaction_messages,
2116 message_to_send_free (connection, m);
2124 bus_transaction_cancel_and_free (BusTransaction *transaction)
2126 DBusConnection *connection;
2128 _dbus_verbose ("TRANSACTION: cancelled\n");
2130 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2131 connection_cancel_transaction (connection, transaction);
2133 _dbus_assert (transaction->connections == NULL);
2135 _dbus_list_foreach (&transaction->cancel_hooks,
2136 cancel_hook_cancel, NULL);
2138 free_cancel_hooks (transaction);
2140 dbus_free (transaction);
2144 connection_execute_transaction (DBusConnection *connection,
2145 BusTransaction *transaction)
2148 BusConnectionData *d;
2150 d = BUS_CONNECTION_DATA (connection);
2151 _dbus_assert (d != NULL);
2153 /* Send the queue in order (FIFO) */
2154 link = _dbus_list_get_last_link (&d->transaction_messages);
2155 while (link != NULL)
2157 MessageToSend *m = link->data;
2158 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2160 if (m->transaction == transaction)
2162 _dbus_list_remove_link (&d->transaction_messages,
2165 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2167 dbus_connection_send_preallocated (connection,
2172 m->preallocated = NULL; /* so we don't double-free it */
2174 message_to_send_free (connection, m);
2182 bus_transaction_execute_and_free (BusTransaction *transaction)
2184 /* For each connection in transaction->connections
2187 DBusConnection *connection;
2189 _dbus_verbose ("TRANSACTION: executing\n");
2191 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2192 connection_execute_transaction (connection, transaction);
2194 _dbus_assert (transaction->connections == NULL);
2196 free_cancel_hooks (transaction);
2198 dbus_free (transaction);
2202 bus_connection_remove_transactions (DBusConnection *connection)
2204 MessageToSend *to_send;
2205 BusConnectionData *d;
2207 d = BUS_CONNECTION_DATA (connection);
2208 _dbus_assert (d != NULL);
2210 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2212 /* only has an effect for the first MessageToSend listing this transaction */
2213 _dbus_list_remove (&to_send->transaction->connections,
2216 _dbus_list_remove (&d->transaction_messages, to_send);
2217 message_to_send_free (connection, to_send);
2222 * Converts the DBusError to a message reply
2225 bus_transaction_send_error_reply (BusTransaction *transaction,
2226 DBusConnection *connection,
2227 const DBusError *error,
2228 DBusMessage *in_reply_to)
2232 _dbus_assert (error != NULL);
2233 _DBUS_ASSERT_ERROR_IS_SET (error);
2235 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2236 error->name, error->message);
2238 reply = dbus_message_new_error (in_reply_to,
2244 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2246 dbus_message_unref (reply);
2250 dbus_message_unref (reply);
2256 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2257 BusTransactionCancelFunction cancel_function,
2259 DBusFreeFunction free_data_function)
2263 ch = dbus_new (CancelHook, 1);
2267 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2268 cancel_function, data);
2270 ch->cancel_function = cancel_function;
2272 ch->free_data_function = free_data_function;
2274 /* It's important that the hooks get run in reverse order that they
2277 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))