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 connection_watch_callback (DBusWatch *watch,
299 unsigned int condition,
302 /* FIXME this can be done in dbus-mainloop.c
303 * if the code in activation.c for the babysitter
304 * watch handler is fixed.
308 _dbus_verbose ("Calling handle_watch\n");
310 return dbus_watch_handle (watch, condition);
314 add_connection_watch (DBusWatch *watch,
317 DBusConnection *connection = data;
319 return _dbus_loop_add_watch (connection_get_loop (connection),
320 watch, connection_watch_callback, connection,
325 remove_connection_watch (DBusWatch *watch,
328 DBusConnection *connection = data;
330 _dbus_loop_remove_watch (connection_get_loop (connection),
331 watch, connection_watch_callback, connection);
335 add_connection_timeout (DBusTimeout *timeout,
338 DBusConnection *connection = data;
340 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
344 remove_connection_timeout (DBusTimeout *timeout,
347 DBusConnection *connection = data;
349 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
353 dispatch_status_function (DBusConnection *connection,
354 DBusDispatchStatus new_status,
357 DBusLoop *loop = data;
359 if (new_status != DBUS_DISPATCH_COMPLETE)
361 while (!_dbus_loop_queue_dispatch (loop, connection))
362 _dbus_wait_for_memory ();
367 allow_unix_user_function (DBusConnection *connection,
371 BusConnectionData *d;
373 d = BUS_CONNECTION_DATA (connection);
375 _dbus_assert (d != NULL);
377 return bus_context_allow_unix_user (d->connections->context, uid);
381 free_connection_data (void *data)
383 BusConnectionData *d = data;
385 /* services_owned should be NULL since we should be disconnected */
386 _dbus_assert (d->services_owned == NULL);
387 _dbus_assert (d->n_services_owned == 0);
389 _dbus_assert (d->transaction_messages == NULL);
391 if (d->oom_preallocated)
392 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
395 dbus_message_unref (d->oom_message);
398 bus_client_policy_unref (d->policy);
401 bus_selinux_id_unref (d->selinux_id);
403 dbus_free (d->cached_loginfo_string);
411 call_timeout_callback (DBusTimeout *timeout,
414 /* can return FALSE on OOM but we just let it fire again later */
415 dbus_timeout_handle (timeout);
419 bus_connections_new (BusContext *context)
421 BusConnections *connections;
423 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
426 connections = dbus_new0 (BusConnections, 1);
427 if (connections == NULL)
430 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
432 if (connections->completed_by_user == NULL)
435 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
436 expire_incomplete_timeout,
438 if (connections->expire_timeout == NULL)
441 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
443 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
444 bus_context_get_reply_timeout (context),
445 bus_pending_reply_expired,
447 if (connections->pending_replies == NULL)
450 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
451 connections->expire_timeout))
454 connections->refcount = 1;
455 connections->context = context;
460 bus_expire_list_free (connections->pending_replies);
462 _dbus_timeout_unref (connections->expire_timeout);
464 _dbus_hash_table_unref (connections->completed_by_user);
466 dbus_free (connections);
468 dbus_connection_free_data_slot (&connection_data_slot);
474 bus_connections_ref (BusConnections *connections)
476 _dbus_assert (connections->refcount > 0);
477 connections->refcount += 1;
483 bus_connections_unref (BusConnections *connections)
485 _dbus_assert (connections->refcount > 0);
486 connections->refcount -= 1;
487 if (connections->refcount == 0)
489 /* drop all incomplete */
490 while (connections->incomplete != NULL)
492 DBusConnection *connection;
494 connection = connections->incomplete->data;
496 dbus_connection_ref (connection);
497 dbus_connection_close (connection);
498 bus_connection_disconnected (connection);
499 dbus_connection_unref (connection);
502 _dbus_assert (connections->n_incomplete == 0);
504 /* drop all real connections */
505 while (connections->completed != NULL)
507 DBusConnection *connection;
509 connection = connections->completed->data;
511 dbus_connection_ref (connection);
512 dbus_connection_close (connection);
513 bus_connection_disconnected (connection);
514 dbus_connection_unref (connection);
517 _dbus_assert (connections->n_completed == 0);
519 bus_expire_list_free (connections->pending_replies);
521 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
522 connections->expire_timeout);
524 _dbus_timeout_unref (connections->expire_timeout);
526 _dbus_hash_table_unref (connections->completed_by_user);
528 dbus_free (connections);
530 dbus_connection_free_data_slot (&connection_data_slot);
534 /* Used for logging */
536 cache_peer_loginfo_string (BusConnectionData *d,
537 DBusConnection *connection)
539 DBusString loginfo_buf;
543 dbus_bool_t prev_added;
545 if (!_dbus_string_init (&loginfo_buf))
549 if (dbus_connection_get_unix_user (connection, &uid))
551 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
557 if (dbus_connection_get_unix_process_id (connection, &pid))
561 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
564 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
566 /* Ignore errors here; we may not have permissions to read the
568 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
569 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
573 if (dbus_connection_get_windows_user (connection, &windows_sid))
575 dbus_bool_t did_append;
576 did_append = _dbus_string_append_printf (&loginfo_buf,
577 "sid=\"%s\" ", windows_sid);
578 dbus_free (windows_sid);
583 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
586 _dbus_string_free (&loginfo_buf);
590 _dbus_string_free (&loginfo_buf);
595 bus_connections_setup_connection (BusConnections *connections,
596 DBusConnection *connection)
599 BusConnectionData *d;
604 d = dbus_new0 (BusConnectionData, 1);
609 d->connections = connections;
610 d->connection = connection;
612 _dbus_get_current_time (&d->connection_tv_sec,
613 &d->connection_tv_usec);
615 _dbus_assert (connection_data_slot >= 0);
617 if (!dbus_connection_set_data (connection,
618 connection_data_slot,
619 d, free_connection_data))
625 dbus_connection_set_route_peer_messages (connection, TRUE);
629 dbus_error_init (&error);
630 d->selinux_id = bus_selinux_init_connection_id (connection,
632 if (dbus_error_is_set (&error))
634 /* This is a bit bogus because we pretend all errors
635 * are OOM; this is done because we know that in bus.c
636 * an OOM error disconnects the connection, which is
637 * the same thing we want on any other error.
639 dbus_error_free (&error);
643 if (!dbus_connection_set_watch_functions (connection,
644 add_connection_watch,
645 remove_connection_watch,
651 if (!dbus_connection_set_timeout_functions (connection,
652 add_connection_timeout,
653 remove_connection_timeout,
658 /* For now we don't need to set a Windows user function because
659 * there are no policies in the config file controlling what
660 * Windows users can connect. The default 'same user that owns the
661 * bus can connect' behavior of DBusConnection is fine on Windows.
663 dbus_connection_set_unix_user_function (connection,
664 allow_unix_user_function,
667 dbus_connection_set_dispatch_status_function (connection,
668 dispatch_status_function,
669 bus_context_get_loop (connections->context),
672 d->link_in_connection_list = _dbus_list_alloc_link (connection);
673 if (d->link_in_connection_list == NULL)
676 /* Setup the connection with the dispatcher */
677 if (!bus_dispatch_add_connection (connection))
680 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
682 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
684 bus_dispatch_remove_connection (connection);
689 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
690 connections->n_incomplete += 1;
692 dbus_connection_ref (connection);
694 /* Note that we might disconnect ourselves here, but it only takes
695 * effect on return to the main loop. We call this to free up
696 * expired connections if possible, and to queue the timeout for our
699 bus_connections_expire_incomplete (connections);
701 /* And we might also disconnect ourselves here, but again it
702 * only takes effect on return to main loop.
704 if (connections->n_incomplete >
705 bus_context_get_max_incomplete_connections (connections->context))
707 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
709 _dbus_assert (connections->incomplete != NULL);
710 /* Disconnect the oldest unauthenticated connection. FIXME
711 * would it be more secure to drop a *random* connection? This
712 * algorithm seems to mean that if someone can create new
713 * connections quickly enough, they can keep anyone else from
714 * completing authentication. But random may or may not really
715 * help with that, a more elaborate solution might be required.
717 dbus_connection_close (connections->incomplete->data);
726 bus_selinux_id_unref (d->selinux_id);
727 d->selinux_id = NULL;
729 if (!dbus_connection_set_watch_functions (connection,
733 _dbus_assert_not_reached ("setting watch functions to NULL failed");
735 if (!dbus_connection_set_timeout_functions (connection,
739 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
741 dbus_connection_set_unix_user_function (connection,
744 dbus_connection_set_windows_user_function (connection,
747 dbus_connection_set_dispatch_status_function (connection,
750 if (d->link_in_connection_list != NULL)
752 _dbus_assert (d->link_in_connection_list->next == NULL);
753 _dbus_assert (d->link_in_connection_list->prev == NULL);
754 _dbus_list_free_link (d->link_in_connection_list);
755 d->link_in_connection_list = NULL;
758 if (!dbus_connection_set_data (connection,
759 connection_data_slot,
761 _dbus_assert_not_reached ("failed to set connection data to null");
763 /* "d" has now been freed */
770 bus_connections_expire_incomplete (BusConnections *connections)
776 if (connections->incomplete != NULL)
778 long tv_sec, tv_usec;
782 _dbus_get_current_time (&tv_sec, &tv_usec);
783 auth_timeout = bus_context_get_auth_timeout (connections->context);
785 link = _dbus_list_get_first_link (&connections->incomplete);
788 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
789 DBusConnection *connection;
790 BusConnectionData *d;
793 connection = link->data;
795 d = BUS_CONNECTION_DATA (connection);
797 _dbus_assert (d != NULL);
799 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
800 d->connection_tv_usec,
803 if (elapsed >= (double) auth_timeout)
805 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
806 dbus_connection_close (connection);
810 /* We can end the loop, since the connections are in oldest-first order */
811 next_interval = ((double)auth_timeout) - elapsed;
812 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
813 connection, next_interval);
822 bus_expire_timeout_set_interval (connections->expire_timeout,
827 expire_incomplete_timeout (void *data)
829 BusConnections *connections = data;
831 _dbus_verbose ("Running\n");
833 /* note that this may remove the timeout */
834 bus_connections_expire_incomplete (connections);
840 bus_connection_get_unix_groups (DBusConnection *connection,
841 unsigned long **groups,
845 BusConnectionData *d;
848 d = BUS_CONNECTION_DATA (connection);
850 _dbus_assert (d != NULL);
855 if (dbus_connection_get_unix_user (connection, &uid))
857 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
859 _dbus_verbose ("Did not get any groups for UID %lu\n",
865 _dbus_verbose ("Got %d groups for UID %lu\n",
871 return TRUE; /* successfully got 0 groups */
875 bus_connection_is_in_unix_group (DBusConnection *connection,
879 unsigned long *group_ids;
882 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
887 while (i < n_group_ids)
889 if (group_ids[i] == gid)
891 dbus_free (group_ids);
897 dbus_free (group_ids);
902 bus_connection_get_loginfo (DBusConnection *connection)
904 BusConnectionData *d;
906 d = BUS_CONNECTION_DATA (connection);
908 if (!bus_connection_is_active (connection))
910 return d->cached_loginfo_string;
914 bus_connection_get_policy (DBusConnection *connection)
916 BusConnectionData *d;
918 d = BUS_CONNECTION_DATA (connection);
920 _dbus_assert (d != NULL);
921 _dbus_assert (d->policy != NULL);
927 foreach_active (BusConnections *connections,
928 BusConnectionForeachFunction function,
933 link = _dbus_list_get_first_link (&connections->completed);
936 DBusConnection *connection = link->data;
937 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
939 if (!(* function) (connection, data))
949 foreach_inactive (BusConnections *connections,
950 BusConnectionForeachFunction function,
955 link = _dbus_list_get_first_link (&connections->incomplete);
958 DBusConnection *connection = link->data;
959 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
961 if (!(* function) (connection, data))
971 * Calls function on each active connection; if the function returns
972 * #FALSE, stops iterating. Active connections are authenticated
973 * and have sent a Hello message.
975 * @param connections the connections object
976 * @param function the function
977 * @param data data to pass to it as a second arg
980 bus_connections_foreach_active (BusConnections *connections,
981 BusConnectionForeachFunction function,
984 foreach_active (connections, function, data);
988 * Calls function on each connection; if the function returns
989 * #FALSE, stops iterating.
991 * @param connections the connections object
992 * @param function the function
993 * @param data data to pass to it as a second arg
996 bus_connections_foreach (BusConnections *connections,
997 BusConnectionForeachFunction function,
1000 if (!foreach_active (connections, function, data))
1003 foreach_inactive (connections, function, data);
1007 bus_connections_get_context (BusConnections *connections)
1009 return connections->context;
1013 * This is used to avoid covering the same connection twice when
1014 * traversing connections. Note that it assumes we will
1015 * bus_connection_mark_stamp() each connection at least once per
1016 * INT_MAX increments of the global stamp, or wraparound would break
1020 bus_connections_increment_stamp (BusConnections *connections)
1022 connections->stamp += 1;
1025 /* Mark connection with current stamp, return TRUE if it
1026 * didn't already have that stamp
1029 bus_connection_mark_stamp (DBusConnection *connection)
1031 BusConnectionData *d;
1033 d = BUS_CONNECTION_DATA (connection);
1035 _dbus_assert (d != NULL);
1037 if (d->stamp == d->connections->stamp)
1041 d->stamp = d->connections->stamp;
1047 bus_connection_get_context (DBusConnection *connection)
1049 BusConnectionData *d;
1051 d = BUS_CONNECTION_DATA (connection);
1053 _dbus_assert (d != NULL);
1055 return d->connections->context;
1059 bus_connection_get_connections (DBusConnection *connection)
1061 BusConnectionData *d;
1063 d = BUS_CONNECTION_DATA (connection);
1065 _dbus_assert (d != NULL);
1067 return d->connections;
1071 bus_connection_get_registry (DBusConnection *connection)
1073 BusConnectionData *d;
1075 d = BUS_CONNECTION_DATA (connection);
1077 _dbus_assert (d != NULL);
1079 return bus_context_get_registry (d->connections->context);
1083 bus_connection_get_activation (DBusConnection *connection)
1085 BusConnectionData *d;
1087 d = BUS_CONNECTION_DATA (connection);
1089 _dbus_assert (d != NULL);
1091 return bus_context_get_activation (d->connections->context);
1095 bus_connection_get_matchmaker (DBusConnection *connection)
1097 BusConnectionData *d;
1099 d = BUS_CONNECTION_DATA (connection);
1101 _dbus_assert (d != NULL);
1103 return bus_context_get_matchmaker (d->connections->context);
1107 bus_connection_get_selinux_id (DBusConnection *connection)
1109 BusConnectionData *d;
1111 d = BUS_CONNECTION_DATA (connection);
1113 _dbus_assert (d != NULL);
1115 return d->selinux_id;
1119 * Checks whether the connection is registered with the message bus.
1121 * @param connection the connection
1122 * @returns #TRUE if we're an active message bus participant
1125 bus_connection_is_active (DBusConnection *connection)
1127 BusConnectionData *d;
1129 d = BUS_CONNECTION_DATA (connection);
1131 return d != NULL && d->name != NULL;
1135 bus_connection_preallocate_oom_error (DBusConnection *connection)
1137 DBusMessage *message;
1138 DBusPreallocatedSend *preallocated;
1139 BusConnectionData *d;
1141 d = BUS_CONNECTION_DATA (connection);
1143 _dbus_assert (d != NULL);
1145 if (d->oom_preallocated != NULL)
1148 preallocated = dbus_connection_preallocate_send (connection);
1149 if (preallocated == NULL)
1152 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1154 if (message == NULL)
1156 dbus_connection_free_preallocated_send (connection, preallocated);
1160 /* d->name may be NULL, but that is OK */
1161 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1162 !dbus_message_set_destination (message, d->name) ||
1163 !dbus_message_set_sender (message,
1166 dbus_connection_free_preallocated_send (connection, preallocated);
1167 dbus_message_unref (message);
1171 /* set reply serial to placeholder value just so space is already allocated
1174 if (!dbus_message_set_reply_serial (message, 14))
1176 dbus_connection_free_preallocated_send (connection, preallocated);
1177 dbus_message_unref (message);
1181 d->oom_message = message;
1182 d->oom_preallocated = preallocated;
1188 bus_connection_send_oom_error (DBusConnection *connection,
1189 DBusMessage *in_reply_to)
1191 BusConnectionData *d;
1193 d = BUS_CONNECTION_DATA (connection);
1195 _dbus_assert (d != NULL);
1196 _dbus_assert (d->oom_message != NULL);
1198 /* should always succeed since we set it to a placeholder earlier */
1199 if (!dbus_message_set_reply_serial (d->oom_message,
1200 dbus_message_get_serial (in_reply_to)))
1201 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1203 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1205 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1206 d->oom_message, NULL);
1208 dbus_message_unref (d->oom_message);
1209 d->oom_message = NULL;
1210 d->oom_preallocated = NULL;
1214 bus_connection_add_match_rule_link (DBusConnection *connection,
1217 BusConnectionData *d;
1219 d = BUS_CONNECTION_DATA (connection);
1220 _dbus_assert (d != NULL);
1222 _dbus_list_append_link (&d->match_rules, link);
1224 d->n_match_rules += 1;
1228 bus_connection_add_match_rule (DBusConnection *connection,
1233 link = _dbus_list_alloc_link (rule);
1238 bus_connection_add_match_rule_link (connection, link);
1244 bus_connection_remove_match_rule (DBusConnection *connection,
1247 BusConnectionData *d;
1249 d = BUS_CONNECTION_DATA (connection);
1250 _dbus_assert (d != NULL);
1252 _dbus_list_remove_last (&d->match_rules, rule);
1254 d->n_match_rules -= 1;
1255 _dbus_assert (d->n_match_rules >= 0);
1259 bus_connection_get_n_match_rules (DBusConnection *connection)
1261 BusConnectionData *d;
1263 d = BUS_CONNECTION_DATA (connection);
1264 _dbus_assert (d != NULL);
1266 return d->n_match_rules;
1270 bus_connection_add_owned_service_link (DBusConnection *connection,
1273 BusConnectionData *d;
1275 d = BUS_CONNECTION_DATA (connection);
1276 _dbus_assert (d != NULL);
1278 _dbus_list_append_link (&d->services_owned, link);
1280 d->n_services_owned += 1;
1284 bus_connection_add_owned_service (DBusConnection *connection,
1285 BusService *service)
1289 link = _dbus_list_alloc_link (service);
1294 bus_connection_add_owned_service_link (connection, link);
1300 bus_connection_remove_owned_service (DBusConnection *connection,
1301 BusService *service)
1303 BusConnectionData *d;
1305 d = BUS_CONNECTION_DATA (connection);
1306 _dbus_assert (d != NULL);
1308 _dbus_list_remove_last (&d->services_owned, service);
1310 d->n_services_owned -= 1;
1311 _dbus_assert (d->n_services_owned >= 0);
1315 bus_connection_get_n_services_owned (DBusConnection *connection)
1317 BusConnectionData *d;
1319 d = BUS_CONNECTION_DATA (connection);
1320 _dbus_assert (d != NULL);
1322 return d->n_services_owned;
1326 bus_connection_complete (DBusConnection *connection,
1327 const DBusString *name,
1330 BusConnectionData *d;
1333 d = BUS_CONNECTION_DATA (connection);
1334 _dbus_assert (d != NULL);
1335 _dbus_assert (d->name == NULL);
1336 _dbus_assert (d->policy == NULL);
1338 _dbus_assert (!bus_connection_is_active (connection));
1340 if (!_dbus_string_copy_data (name, &d->name))
1342 BUS_SET_OOM (error);
1346 _dbus_assert (d->name != NULL);
1348 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1350 d->policy = bus_context_create_client_policy (d->connections->context,
1354 /* we may have a NULL policy on OOM or error getting list of
1355 * groups for a user. In the latter case we don't handle it so
1356 * well currently, as it will just keep failing over and over.
1359 if (d->policy == NULL)
1361 _dbus_verbose ("Failed to create security policy for connection %p\n",
1363 _DBUS_ASSERT_ERROR_IS_SET (error);
1364 dbus_free (d->name);
1369 if (dbus_connection_get_unix_user (connection, &uid))
1371 if (!adjust_connections_for_uid (d->connections,
1376 /* Create and cache a string which holds information about the
1377 * peer process; used for logging purposes.
1379 if (!cache_peer_loginfo_string (d, connection))
1382 /* Now the connection is active, move it between lists */
1383 _dbus_list_unlink (&d->connections->incomplete,
1384 d->link_in_connection_list);
1385 d->connections->n_incomplete -= 1;
1386 _dbus_list_append_link (&d->connections->completed,
1387 d->link_in_connection_list);
1388 d->connections->n_completed += 1;
1390 _dbus_assert (d->connections->n_incomplete >= 0);
1391 _dbus_assert (d->connections->n_completed > 0);
1393 /* See if we can remove the timeout */
1394 bus_connections_expire_incomplete (d->connections);
1396 _dbus_assert (bus_connection_is_active (connection));
1400 BUS_SET_OOM (error);
1401 dbus_free (d->name);
1404 bus_client_policy_unref (d->policy);
1410 bus_connection_get_name (DBusConnection *connection)
1412 BusConnectionData *d;
1414 d = BUS_CONNECTION_DATA (connection);
1415 _dbus_assert (d != NULL);
1421 * Check whether completing the passed-in connection would
1422 * exceed limits, and if so set error and return #FALSE
1425 bus_connections_check_limits (BusConnections *connections,
1426 DBusConnection *requesting_completion,
1429 BusConnectionData *d;
1432 d = BUS_CONNECTION_DATA (requesting_completion);
1433 _dbus_assert (d != NULL);
1435 _dbus_assert (d->name == NULL);
1437 if (connections->n_completed >=
1438 bus_context_get_max_completed_connections (connections->context))
1440 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1441 "The maximum number of active connections has been reached");
1445 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1447 if (get_connections_for_uid (connections, uid) >=
1448 bus_context_get_max_connections_per_user (connections->context))
1450 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1451 "The maximum number of active connections for UID %lu has been reached",
1461 bus_pending_reply_free (BusPendingReply *pending)
1463 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1465 pending->will_send_reply,
1466 pending->will_get_reply,
1467 pending->reply_serial);
1469 dbus_free (pending);
1473 bus_pending_reply_send_no_reply (BusConnections *connections,
1474 BusTransaction *transaction,
1475 BusPendingReply *pending)
1477 DBusMessage *message;
1478 DBusMessageIter iter;
1484 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1485 if (message == NULL)
1488 dbus_message_set_no_reply (message, TRUE);
1490 if (!dbus_message_set_reply_serial (message,
1491 pending->reply_serial))
1494 if (!dbus_message_set_error_name (message,
1495 DBUS_ERROR_NO_REPLY))
1498 errmsg = "Message did not receive a reply (timeout by message bus)";
1499 dbus_message_iter_init_append (message, &iter);
1500 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1503 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1510 dbus_message_unref (message);
1515 bus_pending_reply_expired (BusExpireList *list,
1519 BusPendingReply *pending = link->data;
1520 BusConnections *connections = data;
1521 BusTransaction *transaction;
1523 /* No reply is forthcoming. So nuke it if we can. If not,
1524 * leave it in the list to try expiring again later when we
1528 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1530 pending->will_send_reply,
1531 pending->will_get_reply,
1532 pending->reply_serial);
1534 transaction = bus_transaction_new (connections->context);
1535 if (transaction == NULL)
1538 if (!bus_pending_reply_send_no_reply (connections,
1542 bus_transaction_cancel_and_free (transaction);
1546 bus_expire_list_remove_link (connections->pending_replies, link);
1548 bus_pending_reply_free (pending);
1549 bus_transaction_execute_and_free (transaction);
1555 bus_connection_drop_pending_replies (BusConnections *connections,
1556 DBusConnection *connection)
1558 /* The DBusConnection is almost 100% finalized here, so you can't
1559 * do anything with it except check for pointer equality
1563 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1566 link = bus_expire_list_get_first_link (connections->pending_replies);
1567 while (link != NULL)
1570 BusPendingReply *pending;
1572 next = bus_expire_list_get_next_link (connections->pending_replies,
1574 pending = link->data;
1576 if (pending->will_get_reply == connection)
1578 /* We don't need to track this pending reply anymore */
1580 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1582 pending->will_send_reply,
1583 pending->will_get_reply,
1584 pending->reply_serial);
1586 bus_expire_list_remove_link (connections->pending_replies,
1588 bus_pending_reply_free (pending);
1590 else if (pending->will_send_reply == connection)
1592 /* The reply isn't going to be sent, so set things
1593 * up so it will be expired right away
1595 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1597 pending->will_send_reply,
1598 pending->will_get_reply,
1599 pending->reply_serial);
1601 pending->will_send_reply = NULL;
1602 pending->expire_item.added_tv_sec = 0;
1603 pending->expire_item.added_tv_usec = 0;
1605 bus_expire_list_recheck_immediately (connections->pending_replies);
1615 BusPendingReply *pending;
1616 BusConnections *connections;
1617 } CancelPendingReplyData;
1620 cancel_pending_reply (void *data)
1622 CancelPendingReplyData *d = data;
1624 _dbus_verbose ("d = %p\n", d);
1626 if (!bus_expire_list_remove (d->connections->pending_replies,
1627 &d->pending->expire_item))
1628 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1630 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1634 cancel_pending_reply_data_free (void *data)
1636 CancelPendingReplyData *d = data;
1638 _dbus_verbose ("d = %p\n", d);
1640 /* d->pending should be either freed or still
1641 * in the list of pending replies (owned by someone
1649 * Record that a reply is allowed; return TRUE on success.
1652 bus_connections_expect_reply (BusConnections *connections,
1653 BusTransaction *transaction,
1654 DBusConnection *will_get_reply,
1655 DBusConnection *will_send_reply,
1656 DBusMessage *reply_to_this,
1659 BusPendingReply *pending;
1660 dbus_uint32_t reply_serial;
1662 CancelPendingReplyData *cprd;
1665 _dbus_assert (will_get_reply != NULL);
1666 _dbus_assert (will_send_reply != NULL);
1667 _dbus_assert (reply_to_this != NULL);
1669 if (dbus_message_get_no_reply (reply_to_this))
1670 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1672 reply_serial = dbus_message_get_serial (reply_to_this);
1674 link = bus_expire_list_get_first_link (connections->pending_replies);
1676 while (link != NULL)
1678 pending = link->data;
1680 if (pending->reply_serial == reply_serial &&
1681 pending->will_get_reply == will_get_reply &&
1682 pending->will_send_reply == will_send_reply)
1684 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1685 "Message has the same reply serial as a currently-outstanding existing method call");
1689 link = bus_expire_list_get_next_link (connections->pending_replies,
1691 if (pending->will_get_reply == will_get_reply)
1696 bus_context_get_max_replies_per_connection (connections->context))
1698 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1699 "The maximum number of pending replies per connection has been reached");
1703 pending = dbus_new0 (BusPendingReply, 1);
1704 if (pending == NULL)
1706 BUS_SET_OOM (error);
1710 #ifdef DBUS_ENABLE_VERBOSE_MODE
1711 /* so we can see a not-yet-added pending reply */
1712 pending->expire_item.added_tv_sec = 1;
1713 pending->expire_item.added_tv_usec = 1;
1716 pending->will_get_reply = will_get_reply;
1717 pending->will_send_reply = will_send_reply;
1718 pending->reply_serial = reply_serial;
1720 cprd = dbus_new0 (CancelPendingReplyData, 1);
1723 BUS_SET_OOM (error);
1724 bus_pending_reply_free (pending);
1728 if (!bus_expire_list_add (connections->pending_replies,
1729 &pending->expire_item))
1731 BUS_SET_OOM (error);
1733 bus_pending_reply_free (pending);
1737 if (!bus_transaction_add_cancel_hook (transaction,
1738 cancel_pending_reply,
1740 cancel_pending_reply_data_free))
1742 BUS_SET_OOM (error);
1743 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1745 bus_pending_reply_free (pending);
1749 cprd->pending = pending;
1750 cprd->connections = connections;
1752 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1753 &pending->expire_item.added_tv_usec);
1755 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1757 pending->will_send_reply,
1758 pending->will_get_reply,
1759 pending->reply_serial);
1767 BusConnections *connections;
1768 } CheckPendingReplyData;
1771 cancel_check_pending_reply (void *data)
1773 CheckPendingReplyData *d = data;
1775 _dbus_verbose ("d = %p\n",d);
1777 bus_expire_list_add_link (d->connections->pending_replies,
1783 check_pending_reply_data_free (void *data)
1785 CheckPendingReplyData *d = data;
1787 _dbus_verbose ("d = %p\n",d);
1789 if (d->link != NULL)
1791 BusPendingReply *pending = d->link->data;
1793 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1794 &pending->expire_item));
1796 bus_pending_reply_free (pending);
1797 _dbus_list_free_link (d->link);
1804 * Check whether a reply is allowed, remove BusPendingReply
1805 * if so, return TRUE if so.
1808 bus_connections_check_reply (BusConnections *connections,
1809 BusTransaction *transaction,
1810 DBusConnection *sending_reply,
1811 DBusConnection *receiving_reply,
1815 CheckPendingReplyData *cprd;
1817 dbus_uint32_t reply_serial;
1819 _dbus_assert (sending_reply != NULL);
1820 _dbus_assert (receiving_reply != NULL);
1822 reply_serial = dbus_message_get_reply_serial (reply);
1824 link = bus_expire_list_get_first_link (connections->pending_replies);
1825 while (link != NULL)
1827 BusPendingReply *pending = link->data;
1829 if (pending->reply_serial == reply_serial &&
1830 pending->will_get_reply == receiving_reply &&
1831 pending->will_send_reply == sending_reply)
1833 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1837 link = bus_expire_list_get_next_link (connections->pending_replies,
1843 _dbus_verbose ("No pending reply expected\n");
1848 cprd = dbus_new0 (CheckPendingReplyData, 1);
1851 BUS_SET_OOM (error);
1855 if (!bus_transaction_add_cancel_hook (transaction,
1856 cancel_check_pending_reply,
1858 check_pending_reply_data_free))
1860 BUS_SET_OOM (error);
1866 cprd->connections = connections;
1868 bus_expire_list_unlink (connections->pending_replies,
1871 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1879 * Note that this is fairly fragile; in particular, don't try to use
1880 * one transaction across any main loop iterations.
1885 BusTransaction *transaction;
1886 DBusMessage *message;
1887 DBusPreallocatedSend *preallocated;
1892 BusTransactionCancelFunction cancel_function;
1893 DBusFreeFunction free_data_function;
1897 struct BusTransaction
1899 DBusList *connections;
1900 BusContext *context;
1901 DBusList *cancel_hooks;
1905 message_to_send_free (DBusConnection *connection,
1906 MessageToSend *to_send)
1908 if (to_send->message)
1909 dbus_message_unref (to_send->message);
1911 if (to_send->preallocated)
1912 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1914 dbus_free (to_send);
1918 cancel_hook_cancel (void *element,
1921 CancelHook *ch = element;
1923 _dbus_verbose ("Running transaction cancel hook\n");
1925 if (ch->cancel_function)
1926 (* ch->cancel_function) (ch->data);
1930 cancel_hook_free (void *element,
1933 CancelHook *ch = element;
1935 if (ch->free_data_function)
1936 (* ch->free_data_function) (ch->data);
1942 free_cancel_hooks (BusTransaction *transaction)
1944 _dbus_list_foreach (&transaction->cancel_hooks,
1945 cancel_hook_free, NULL);
1947 _dbus_list_clear (&transaction->cancel_hooks);
1951 bus_transaction_new (BusContext *context)
1953 BusTransaction *transaction;
1955 transaction = dbus_new0 (BusTransaction, 1);
1956 if (transaction == NULL)
1959 transaction->context = context;
1965 bus_transaction_get_context (BusTransaction *transaction)
1967 return transaction->context;
1971 bus_transaction_get_connections (BusTransaction *transaction)
1973 return bus_context_get_connections (transaction->context);
1977 bus_transaction_send_from_driver (BusTransaction *transaction,
1978 DBusConnection *connection,
1979 DBusMessage *message)
1981 /* We have to set the sender to the driver, and have
1982 * to check security policy since it was not done in
1985 _dbus_verbose ("Sending %s %s %s from driver\n",
1986 dbus_message_get_interface (message) ?
1987 dbus_message_get_interface (message) : "(no interface)",
1988 dbus_message_get_member (message) ?
1989 dbus_message_get_member (message) : "(no member)",
1990 dbus_message_get_error_name (message) ?
1991 dbus_message_get_error_name (message) : "(no error name)");
1993 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
1996 if (bus_connection_is_active (connection))
1998 if (!dbus_message_set_destination (message,
1999 bus_connection_get_name (connection)))
2003 /* bus driver never wants a reply */
2004 dbus_message_set_no_reply (message, TRUE);
2006 /* If security policy doesn't allow the message, we silently
2007 * eat it; the driver doesn't care about getting a reply.
2009 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2011 NULL, connection, connection, message, NULL))
2014 return bus_transaction_send (transaction, connection, message);
2018 bus_transaction_send (BusTransaction *transaction,
2019 DBusConnection *connection,
2020 DBusMessage *message)
2022 MessageToSend *to_send;
2023 BusConnectionData *d;
2026 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2027 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2028 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2030 dbus_message_get_interface (message) ?
2031 dbus_message_get_interface (message) : "(unset)",
2032 dbus_message_get_member (message) ?
2033 dbus_message_get_member (message) : "(unset)",
2034 dbus_message_get_error_name (message) ?
2035 dbus_message_get_error_name (message) : "(unset)",
2036 dbus_connection_get_is_connected (connection) ?
2037 "" : " (disconnected)");
2039 _dbus_assert (dbus_message_get_sender (message) != NULL);
2041 if (!dbus_connection_get_is_connected (connection))
2042 return TRUE; /* silently ignore disconnected connections */
2044 d = BUS_CONNECTION_DATA (connection);
2045 _dbus_assert (d != NULL);
2047 to_send = dbus_new (MessageToSend, 1);
2048 if (to_send == NULL)
2053 to_send->preallocated = dbus_connection_preallocate_send (connection);
2054 if (to_send->preallocated == NULL)
2056 dbus_free (to_send);
2060 dbus_message_ref (message);
2061 to_send->message = message;
2062 to_send->transaction = transaction;
2064 _dbus_verbose ("about to prepend message\n");
2066 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2068 message_to_send_free (connection, to_send);
2072 _dbus_verbose ("prepended message\n");
2074 /* See if we already had this connection in the list
2075 * for this transaction. If we have a pending message,
2076 * then we should already be in transaction->connections
2078 link = _dbus_list_get_first_link (&d->transaction_messages);
2079 _dbus_assert (link->data == to_send);
2080 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2081 while (link != NULL)
2083 MessageToSend *m = link->data;
2084 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2086 if (m->transaction == transaction)
2094 if (!_dbus_list_prepend (&transaction->connections, connection))
2096 _dbus_list_remove (&d->transaction_messages, to_send);
2097 message_to_send_free (connection, to_send);
2106 connection_cancel_transaction (DBusConnection *connection,
2107 BusTransaction *transaction)
2110 BusConnectionData *d;
2112 d = BUS_CONNECTION_DATA (connection);
2113 _dbus_assert (d != NULL);
2115 link = _dbus_list_get_first_link (&d->transaction_messages);
2116 while (link != NULL)
2118 MessageToSend *m = link->data;
2119 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2121 if (m->transaction == transaction)
2123 _dbus_list_remove_link (&d->transaction_messages,
2126 message_to_send_free (connection, m);
2134 bus_transaction_cancel_and_free (BusTransaction *transaction)
2136 DBusConnection *connection;
2138 _dbus_verbose ("TRANSACTION: cancelled\n");
2140 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2141 connection_cancel_transaction (connection, transaction);
2143 _dbus_assert (transaction->connections == NULL);
2145 _dbus_list_foreach (&transaction->cancel_hooks,
2146 cancel_hook_cancel, NULL);
2148 free_cancel_hooks (transaction);
2150 dbus_free (transaction);
2154 connection_execute_transaction (DBusConnection *connection,
2155 BusTransaction *transaction)
2158 BusConnectionData *d;
2160 d = BUS_CONNECTION_DATA (connection);
2161 _dbus_assert (d != NULL);
2163 /* Send the queue in order (FIFO) */
2164 link = _dbus_list_get_last_link (&d->transaction_messages);
2165 while (link != NULL)
2167 MessageToSend *m = link->data;
2168 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2170 if (m->transaction == transaction)
2172 _dbus_list_remove_link (&d->transaction_messages,
2175 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2177 dbus_connection_send_preallocated (connection,
2182 m->preallocated = NULL; /* so we don't double-free it */
2184 message_to_send_free (connection, m);
2192 bus_transaction_execute_and_free (BusTransaction *transaction)
2194 /* For each connection in transaction->connections
2197 DBusConnection *connection;
2199 _dbus_verbose ("TRANSACTION: executing\n");
2201 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2202 connection_execute_transaction (connection, transaction);
2204 _dbus_assert (transaction->connections == NULL);
2206 free_cancel_hooks (transaction);
2208 dbus_free (transaction);
2212 bus_connection_remove_transactions (DBusConnection *connection)
2214 MessageToSend *to_send;
2215 BusConnectionData *d;
2217 d = BUS_CONNECTION_DATA (connection);
2218 _dbus_assert (d != NULL);
2220 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2222 /* only has an effect for the first MessageToSend listing this transaction */
2223 _dbus_list_remove (&to_send->transaction->connections,
2226 _dbus_list_remove (&d->transaction_messages, to_send);
2227 message_to_send_free (connection, to_send);
2232 * Converts the DBusError to a message reply
2235 bus_transaction_send_error_reply (BusTransaction *transaction,
2236 DBusConnection *connection,
2237 const DBusError *error,
2238 DBusMessage *in_reply_to)
2242 _dbus_assert (error != NULL);
2243 _DBUS_ASSERT_ERROR_IS_SET (error);
2245 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2246 error->name, error->message);
2248 reply = dbus_message_new_error (in_reply_to,
2254 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2256 dbus_message_unref (reply);
2260 dbus_message_unref (reply);
2266 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2267 BusTransactionCancelFunction cancel_function,
2269 DBusFreeFunction free_data_function)
2273 ch = dbus_new (CancelHook, 1);
2277 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2278 cancel_function, data);
2280 ch->cancel_function = cancel_function;
2282 ch->free_data_function = free_data_function;
2284 /* It's important that the hooks get run in reverse order that they
2287 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))