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_ulong (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_ulong (connections->completed_by_user, uid);
159 retval = _dbus_hash_table_insert_ulong (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 connection_timeout_callback (DBusTimeout *timeout,
338 /* DBusConnection *connection = data; */
340 /* can return FALSE on OOM but we just let it fire again later */
341 dbus_timeout_handle (timeout);
345 add_connection_timeout (DBusTimeout *timeout,
348 DBusConnection *connection = data;
350 return _dbus_loop_add_timeout (connection_get_loop (connection),
351 timeout, connection_timeout_callback, connection, NULL);
355 remove_connection_timeout (DBusTimeout *timeout,
358 DBusConnection *connection = data;
360 _dbus_loop_remove_timeout (connection_get_loop (connection),
361 timeout, connection_timeout_callback, connection);
365 dispatch_status_function (DBusConnection *connection,
366 DBusDispatchStatus new_status,
369 DBusLoop *loop = data;
371 if (new_status != DBUS_DISPATCH_COMPLETE)
373 while (!_dbus_loop_queue_dispatch (loop, connection))
374 _dbus_wait_for_memory ();
379 allow_unix_user_function (DBusConnection *connection,
383 BusConnectionData *d;
385 d = BUS_CONNECTION_DATA (connection);
387 _dbus_assert (d != NULL);
389 return bus_context_allow_unix_user (d->connections->context, uid);
393 free_connection_data (void *data)
395 BusConnectionData *d = data;
397 /* services_owned should be NULL since we should be disconnected */
398 _dbus_assert (d->services_owned == NULL);
399 _dbus_assert (d->n_services_owned == 0);
401 _dbus_assert (d->transaction_messages == NULL);
403 if (d->oom_preallocated)
404 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
407 dbus_message_unref (d->oom_message);
410 bus_client_policy_unref (d->policy);
413 bus_selinux_id_unref (d->selinux_id);
415 dbus_free (d->cached_loginfo_string);
423 call_timeout_callback (DBusTimeout *timeout,
426 /* can return FALSE on OOM but we just let it fire again later */
427 dbus_timeout_handle (timeout);
431 bus_connections_new (BusContext *context)
433 BusConnections *connections;
435 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
438 connections = dbus_new0 (BusConnections, 1);
439 if (connections == NULL)
442 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_ULONG,
444 if (connections->completed_by_user == NULL)
447 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
448 expire_incomplete_timeout,
450 if (connections->expire_timeout == NULL)
453 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
455 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
456 bus_context_get_reply_timeout (context),
457 bus_pending_reply_expired,
459 if (connections->pending_replies == NULL)
462 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
463 connections->expire_timeout,
464 call_timeout_callback, NULL, NULL))
467 connections->refcount = 1;
468 connections->context = context;
473 bus_expire_list_free (connections->pending_replies);
475 _dbus_timeout_unref (connections->expire_timeout);
477 _dbus_hash_table_unref (connections->completed_by_user);
479 dbus_free (connections);
481 dbus_connection_free_data_slot (&connection_data_slot);
487 bus_connections_ref (BusConnections *connections)
489 _dbus_assert (connections->refcount > 0);
490 connections->refcount += 1;
496 bus_connections_unref (BusConnections *connections)
498 _dbus_assert (connections->refcount > 0);
499 connections->refcount -= 1;
500 if (connections->refcount == 0)
502 /* drop all incomplete */
503 while (connections->incomplete != NULL)
505 DBusConnection *connection;
507 connection = connections->incomplete->data;
509 dbus_connection_ref (connection);
510 dbus_connection_close (connection);
511 bus_connection_disconnected (connection);
512 dbus_connection_unref (connection);
515 _dbus_assert (connections->n_incomplete == 0);
517 /* drop all real connections */
518 while (connections->completed != NULL)
520 DBusConnection *connection;
522 connection = connections->completed->data;
524 dbus_connection_ref (connection);
525 dbus_connection_close (connection);
526 bus_connection_disconnected (connection);
527 dbus_connection_unref (connection);
530 _dbus_assert (connections->n_completed == 0);
532 bus_expire_list_free (connections->pending_replies);
534 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
535 connections->expire_timeout,
536 call_timeout_callback, NULL);
538 _dbus_timeout_unref (connections->expire_timeout);
540 _dbus_hash_table_unref (connections->completed_by_user);
542 dbus_free (connections);
544 dbus_connection_free_data_slot (&connection_data_slot);
548 /* Used for logging */
550 cache_peer_loginfo_string (BusConnectionData *d,
551 DBusConnection *connection)
553 DBusString loginfo_buf;
557 dbus_bool_t prev_added;
559 if (!_dbus_string_init (&loginfo_buf))
563 if (dbus_connection_get_unix_user (connection, &uid))
565 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
571 if (dbus_connection_get_unix_process_id (connection, &pid))
575 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
578 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
580 /* Ignore errors here; we may not have permissions to read the
582 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
583 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
587 if (dbus_connection_get_windows_user (connection, &windows_sid))
589 if (!_dbus_string_append_printf (&loginfo_buf, "sid=\"%s\" ", windows_sid))
591 dbus_free (windows_sid);
594 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
597 _dbus_string_free (&loginfo_buf);
601 _dbus_string_free (&loginfo_buf);
606 bus_connections_setup_connection (BusConnections *connections,
607 DBusConnection *connection)
610 BusConnectionData *d;
615 d = dbus_new0 (BusConnectionData, 1);
620 d->connections = connections;
621 d->connection = connection;
623 _dbus_get_current_time (&d->connection_tv_sec,
624 &d->connection_tv_usec);
626 _dbus_assert (connection_data_slot >= 0);
628 if (!dbus_connection_set_data (connection,
629 connection_data_slot,
630 d, free_connection_data))
636 dbus_connection_set_route_peer_messages (connection, TRUE);
640 dbus_error_init (&error);
641 d->selinux_id = bus_selinux_init_connection_id (connection,
643 if (dbus_error_is_set (&error))
645 /* This is a bit bogus because we pretend all errors
646 * are OOM; this is done because we know that in bus.c
647 * an OOM error disconnects the connection, which is
648 * the same thing we want on any other error.
650 dbus_error_free (&error);
654 if (!dbus_connection_set_watch_functions (connection,
655 add_connection_watch,
656 remove_connection_watch,
662 if (!dbus_connection_set_timeout_functions (connection,
663 add_connection_timeout,
664 remove_connection_timeout,
669 /* For now we don't need to set a Windows user function because
670 * there are no policies in the config file controlling what
671 * Windows users can connect. The default 'same user that owns the
672 * bus can connect' behavior of DBusConnection is fine on Windows.
674 dbus_connection_set_unix_user_function (connection,
675 allow_unix_user_function,
678 dbus_connection_set_dispatch_status_function (connection,
679 dispatch_status_function,
680 bus_context_get_loop (connections->context),
683 d->link_in_connection_list = _dbus_list_alloc_link (connection);
684 if (d->link_in_connection_list == NULL)
687 /* Setup the connection with the dispatcher */
688 if (!bus_dispatch_add_connection (connection))
691 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
693 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
695 bus_dispatch_remove_connection (connection);
700 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
701 connections->n_incomplete += 1;
703 dbus_connection_ref (connection);
705 /* Note that we might disconnect ourselves here, but it only takes
706 * effect on return to the main loop. We call this to free up
707 * expired connections if possible, and to queue the timeout for our
710 bus_connections_expire_incomplete (connections);
712 /* And we might also disconnect ourselves here, but again it
713 * only takes effect on return to main loop.
715 if (connections->n_incomplete >
716 bus_context_get_max_incomplete_connections (connections->context))
718 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
720 _dbus_assert (connections->incomplete != NULL);
721 /* Disconnect the oldest unauthenticated connection. FIXME
722 * would it be more secure to drop a *random* connection? This
723 * algorithm seems to mean that if someone can create new
724 * connections quickly enough, they can keep anyone else from
725 * completing authentication. But random may or may not really
726 * help with that, a more elaborate solution might be required.
728 dbus_connection_close (connections->incomplete->data);
737 bus_selinux_id_unref (d->selinux_id);
738 d->selinux_id = NULL;
740 if (!dbus_connection_set_watch_functions (connection,
744 _dbus_assert_not_reached ("setting watch functions to NULL failed");
746 if (!dbus_connection_set_timeout_functions (connection,
750 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
752 dbus_connection_set_unix_user_function (connection,
755 dbus_connection_set_windows_user_function (connection,
758 dbus_connection_set_dispatch_status_function (connection,
761 if (d->link_in_connection_list != NULL)
763 _dbus_assert (d->link_in_connection_list->next == NULL);
764 _dbus_assert (d->link_in_connection_list->prev == NULL);
765 _dbus_list_free_link (d->link_in_connection_list);
766 d->link_in_connection_list = NULL;
769 if (!dbus_connection_set_data (connection,
770 connection_data_slot,
772 _dbus_assert_not_reached ("failed to set connection data to null");
774 /* "d" has now been freed */
781 bus_connections_expire_incomplete (BusConnections *connections)
787 if (connections->incomplete != NULL)
789 long tv_sec, tv_usec;
793 _dbus_get_current_time (&tv_sec, &tv_usec);
794 auth_timeout = bus_context_get_auth_timeout (connections->context);
796 link = _dbus_list_get_first_link (&connections->incomplete);
799 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
800 DBusConnection *connection;
801 BusConnectionData *d;
804 connection = link->data;
806 d = BUS_CONNECTION_DATA (connection);
808 _dbus_assert (d != NULL);
810 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
811 d->connection_tv_usec,
814 if (elapsed >= (double) auth_timeout)
816 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
817 dbus_connection_close (connection);
821 /* We can end the loop, since the connections are in oldest-first order */
822 next_interval = ((double)auth_timeout) - elapsed;
823 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
824 connection, next_interval);
833 bus_expire_timeout_set_interval (connections->expire_timeout,
838 expire_incomplete_timeout (void *data)
840 BusConnections *connections = data;
842 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
844 /* note that this may remove the timeout */
845 bus_connections_expire_incomplete (connections);
851 bus_connection_get_unix_groups (DBusConnection *connection,
852 unsigned long **groups,
856 BusConnectionData *d;
859 d = BUS_CONNECTION_DATA (connection);
861 _dbus_assert (d != NULL);
866 if (dbus_connection_get_unix_user (connection, &uid))
868 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
870 _dbus_verbose ("Did not get any groups for UID %lu\n",
876 _dbus_verbose ("Got %d groups for UID %lu\n",
882 return TRUE; /* successfully got 0 groups */
886 bus_connection_is_in_unix_group (DBusConnection *connection,
890 unsigned long *group_ids;
893 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
898 while (i < n_group_ids)
900 if (group_ids[i] == gid)
902 dbus_free (group_ids);
908 dbus_free (group_ids);
913 bus_connection_get_loginfo (DBusConnection *connection)
915 BusConnectionData *d;
917 d = BUS_CONNECTION_DATA (connection);
919 if (!bus_connection_is_active (connection))
921 return d->cached_loginfo_string;
925 bus_connection_get_policy (DBusConnection *connection)
927 BusConnectionData *d;
929 d = BUS_CONNECTION_DATA (connection);
931 _dbus_assert (d != NULL);
932 _dbus_assert (d->policy != NULL);
938 foreach_active (BusConnections *connections,
939 BusConnectionForeachFunction function,
944 link = _dbus_list_get_first_link (&connections->completed);
947 DBusConnection *connection = link->data;
948 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
950 if (!(* function) (connection, data))
960 foreach_inactive (BusConnections *connections,
961 BusConnectionForeachFunction function,
966 link = _dbus_list_get_first_link (&connections->incomplete);
969 DBusConnection *connection = link->data;
970 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
972 if (!(* function) (connection, data))
982 * Calls function on each active connection; if the function returns
983 * #FALSE, stops iterating. Active connections are authenticated
984 * and have sent a Hello message.
986 * @param connections the connections object
987 * @param function the function
988 * @param data data to pass to it as a second arg
991 bus_connections_foreach_active (BusConnections *connections,
992 BusConnectionForeachFunction function,
995 foreach_active (connections, function, data);
999 * Calls function on each connection; if the function returns
1000 * #FALSE, stops iterating.
1002 * @param connections the connections object
1003 * @param function the function
1004 * @param data data to pass to it as a second arg
1007 bus_connections_foreach (BusConnections *connections,
1008 BusConnectionForeachFunction function,
1011 if (!foreach_active (connections, function, data))
1014 foreach_inactive (connections, function, data);
1018 bus_connections_get_context (BusConnections *connections)
1020 return connections->context;
1024 * This is used to avoid covering the same connection twice when
1025 * traversing connections. Note that it assumes we will
1026 * bus_connection_mark_stamp() each connection at least once per
1027 * INT_MAX increments of the global stamp, or wraparound would break
1031 bus_connections_increment_stamp (BusConnections *connections)
1033 connections->stamp += 1;
1036 /* Mark connection with current stamp, return TRUE if it
1037 * didn't already have that stamp
1040 bus_connection_mark_stamp (DBusConnection *connection)
1042 BusConnectionData *d;
1044 d = BUS_CONNECTION_DATA (connection);
1046 _dbus_assert (d != NULL);
1048 if (d->stamp == d->connections->stamp)
1052 d->stamp = d->connections->stamp;
1058 bus_connection_get_context (DBusConnection *connection)
1060 BusConnectionData *d;
1062 d = BUS_CONNECTION_DATA (connection);
1064 _dbus_assert (d != NULL);
1066 return d->connections->context;
1070 bus_connection_get_connections (DBusConnection *connection)
1072 BusConnectionData *d;
1074 d = BUS_CONNECTION_DATA (connection);
1076 _dbus_assert (d != NULL);
1078 return d->connections;
1082 bus_connection_get_registry (DBusConnection *connection)
1084 BusConnectionData *d;
1086 d = BUS_CONNECTION_DATA (connection);
1088 _dbus_assert (d != NULL);
1090 return bus_context_get_registry (d->connections->context);
1094 bus_connection_get_activation (DBusConnection *connection)
1096 BusConnectionData *d;
1098 d = BUS_CONNECTION_DATA (connection);
1100 _dbus_assert (d != NULL);
1102 return bus_context_get_activation (d->connections->context);
1106 bus_connection_get_matchmaker (DBusConnection *connection)
1108 BusConnectionData *d;
1110 d = BUS_CONNECTION_DATA (connection);
1112 _dbus_assert (d != NULL);
1114 return bus_context_get_matchmaker (d->connections->context);
1118 bus_connection_get_selinux_id (DBusConnection *connection)
1120 BusConnectionData *d;
1122 d = BUS_CONNECTION_DATA (connection);
1124 _dbus_assert (d != NULL);
1126 return d->selinux_id;
1130 * Checks whether the connection is registered with the message bus.
1132 * @param connection the connection
1133 * @returns #TRUE if we're an active message bus participant
1136 bus_connection_is_active (DBusConnection *connection)
1138 BusConnectionData *d;
1140 d = BUS_CONNECTION_DATA (connection);
1142 return d != NULL && d->name != NULL;
1146 bus_connection_preallocate_oom_error (DBusConnection *connection)
1148 DBusMessage *message;
1149 DBusPreallocatedSend *preallocated;
1150 BusConnectionData *d;
1152 d = BUS_CONNECTION_DATA (connection);
1154 _dbus_assert (d != NULL);
1156 if (d->oom_preallocated != NULL)
1159 preallocated = dbus_connection_preallocate_send (connection);
1160 if (preallocated == NULL)
1163 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1165 if (message == NULL)
1167 dbus_connection_free_preallocated_send (connection, preallocated);
1171 /* d->name may be NULL, but that is OK */
1172 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1173 !dbus_message_set_destination (message, d->name) ||
1174 !dbus_message_set_sender (message,
1177 dbus_connection_free_preallocated_send (connection, preallocated);
1178 dbus_message_unref (message);
1182 /* set reply serial to placeholder value just so space is already allocated
1185 if (!dbus_message_set_reply_serial (message, 14))
1187 dbus_connection_free_preallocated_send (connection, preallocated);
1188 dbus_message_unref (message);
1192 d->oom_message = message;
1193 d->oom_preallocated = preallocated;
1199 bus_connection_send_oom_error (DBusConnection *connection,
1200 DBusMessage *in_reply_to)
1202 BusConnectionData *d;
1204 d = BUS_CONNECTION_DATA (connection);
1206 _dbus_assert (d != NULL);
1207 _dbus_assert (d->oom_message != NULL);
1209 /* should always succeed since we set it to a placeholder earlier */
1210 if (!dbus_message_set_reply_serial (d->oom_message,
1211 dbus_message_get_serial (in_reply_to)))
1212 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1214 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1216 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1217 d->oom_message, NULL);
1219 dbus_message_unref (d->oom_message);
1220 d->oom_message = NULL;
1221 d->oom_preallocated = NULL;
1225 bus_connection_add_match_rule_link (DBusConnection *connection,
1228 BusConnectionData *d;
1230 d = BUS_CONNECTION_DATA (connection);
1231 _dbus_assert (d != NULL);
1233 _dbus_list_append_link (&d->match_rules, link);
1235 d->n_match_rules += 1;
1239 bus_connection_add_match_rule (DBusConnection *connection,
1244 link = _dbus_list_alloc_link (rule);
1249 bus_connection_add_match_rule_link (connection, link);
1255 bus_connection_remove_match_rule (DBusConnection *connection,
1258 BusConnectionData *d;
1260 d = BUS_CONNECTION_DATA (connection);
1261 _dbus_assert (d != NULL);
1263 _dbus_list_remove_last (&d->match_rules, rule);
1265 d->n_match_rules -= 1;
1266 _dbus_assert (d->n_match_rules >= 0);
1270 bus_connection_get_n_match_rules (DBusConnection *connection)
1272 BusConnectionData *d;
1274 d = BUS_CONNECTION_DATA (connection);
1275 _dbus_assert (d != NULL);
1277 return d->n_match_rules;
1281 bus_connection_add_owned_service_link (DBusConnection *connection,
1284 BusConnectionData *d;
1286 d = BUS_CONNECTION_DATA (connection);
1287 _dbus_assert (d != NULL);
1289 _dbus_list_append_link (&d->services_owned, link);
1291 d->n_services_owned += 1;
1295 bus_connection_add_owned_service (DBusConnection *connection,
1296 BusService *service)
1300 link = _dbus_list_alloc_link (service);
1305 bus_connection_add_owned_service_link (connection, link);
1311 bus_connection_remove_owned_service (DBusConnection *connection,
1312 BusService *service)
1314 BusConnectionData *d;
1316 d = BUS_CONNECTION_DATA (connection);
1317 _dbus_assert (d != NULL);
1319 _dbus_list_remove_last (&d->services_owned, service);
1321 d->n_services_owned -= 1;
1322 _dbus_assert (d->n_services_owned >= 0);
1326 bus_connection_get_n_services_owned (DBusConnection *connection)
1328 BusConnectionData *d;
1330 d = BUS_CONNECTION_DATA (connection);
1331 _dbus_assert (d != NULL);
1333 return d->n_services_owned;
1337 bus_connection_complete (DBusConnection *connection,
1338 const DBusString *name,
1341 BusConnectionData *d;
1344 d = BUS_CONNECTION_DATA (connection);
1345 _dbus_assert (d != NULL);
1346 _dbus_assert (d->name == NULL);
1347 _dbus_assert (d->policy == NULL);
1349 _dbus_assert (!bus_connection_is_active (connection));
1351 if (!_dbus_string_copy_data (name, &d->name))
1353 BUS_SET_OOM (error);
1357 _dbus_assert (d->name != NULL);
1359 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1361 d->policy = bus_context_create_client_policy (d->connections->context,
1365 /* we may have a NULL policy on OOM or error getting list of
1366 * groups for a user. In the latter case we don't handle it so
1367 * well currently, as it will just keep failing over and over.
1370 if (d->policy == NULL)
1372 _dbus_verbose ("Failed to create security policy for connection %p\n",
1374 _DBUS_ASSERT_ERROR_IS_SET (error);
1375 dbus_free (d->name);
1380 if (dbus_connection_get_unix_user (connection, &uid))
1382 if (!adjust_connections_for_uid (d->connections,
1387 /* Create and cache a string which holds information about the
1388 * peer process; used for logging purposes.
1390 if (!cache_peer_loginfo_string (d, connection))
1393 /* Now the connection is active, move it between lists */
1394 _dbus_list_unlink (&d->connections->incomplete,
1395 d->link_in_connection_list);
1396 d->connections->n_incomplete -= 1;
1397 _dbus_list_append_link (&d->connections->completed,
1398 d->link_in_connection_list);
1399 d->connections->n_completed += 1;
1401 _dbus_assert (d->connections->n_incomplete >= 0);
1402 _dbus_assert (d->connections->n_completed > 0);
1404 /* See if we can remove the timeout */
1405 bus_connections_expire_incomplete (d->connections);
1407 _dbus_assert (bus_connection_is_active (connection));
1411 BUS_SET_OOM (error);
1412 dbus_free (d->name);
1415 bus_client_policy_unref (d->policy);
1421 bus_connection_get_name (DBusConnection *connection)
1423 BusConnectionData *d;
1425 d = BUS_CONNECTION_DATA (connection);
1426 _dbus_assert (d != NULL);
1432 * Check whether completing the passed-in connection would
1433 * exceed limits, and if so set error and return #FALSE
1436 bus_connections_check_limits (BusConnections *connections,
1437 DBusConnection *requesting_completion,
1440 BusConnectionData *d;
1443 d = BUS_CONNECTION_DATA (requesting_completion);
1444 _dbus_assert (d != NULL);
1446 _dbus_assert (d->name == NULL);
1448 if (connections->n_completed >=
1449 bus_context_get_max_completed_connections (connections->context))
1451 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1452 "The maximum number of active connections has been reached");
1456 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1458 if (get_connections_for_uid (connections, uid) >=
1459 bus_context_get_max_connections_per_user (connections->context))
1461 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1462 "The maximum number of active connections for UID %lu has been reached",
1472 bus_pending_reply_free (BusPendingReply *pending)
1474 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1476 pending->will_send_reply,
1477 pending->will_get_reply,
1478 pending->reply_serial);
1480 dbus_free (pending);
1484 bus_pending_reply_send_no_reply (BusConnections *connections,
1485 BusTransaction *transaction,
1486 BusPendingReply *pending)
1488 DBusMessage *message;
1489 DBusMessageIter iter;
1495 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1496 if (message == NULL)
1499 dbus_message_set_no_reply (message, TRUE);
1501 if (!dbus_message_set_reply_serial (message,
1502 pending->reply_serial))
1505 if (!dbus_message_set_error_name (message,
1506 DBUS_ERROR_NO_REPLY))
1509 errmsg = "Message did not receive a reply (timeout by message bus)";
1510 dbus_message_iter_init_append (message, &iter);
1511 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1514 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1521 dbus_message_unref (message);
1526 bus_pending_reply_expired (BusExpireList *list,
1530 BusPendingReply *pending = link->data;
1531 BusConnections *connections = data;
1532 BusTransaction *transaction;
1534 /* No reply is forthcoming. So nuke it if we can. If not,
1535 * leave it in the list to try expiring again later when we
1539 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1541 pending->will_send_reply,
1542 pending->will_get_reply,
1543 pending->reply_serial);
1545 transaction = bus_transaction_new (connections->context);
1546 if (transaction == NULL)
1549 if (!bus_pending_reply_send_no_reply (connections,
1553 bus_transaction_cancel_and_free (transaction);
1557 bus_expire_list_remove_link (connections->pending_replies, link);
1559 bus_pending_reply_free (pending);
1560 bus_transaction_execute_and_free (transaction);
1566 bus_connection_drop_pending_replies (BusConnections *connections,
1567 DBusConnection *connection)
1569 /* The DBusConnection is almost 100% finalized here, so you can't
1570 * do anything with it except check for pointer equality
1574 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1577 link = bus_expire_list_get_first_link (connections->pending_replies);
1578 while (link != NULL)
1581 BusPendingReply *pending;
1583 next = bus_expire_list_get_next_link (connections->pending_replies,
1585 pending = link->data;
1587 if (pending->will_get_reply == connection)
1589 /* We don't need to track this pending reply anymore */
1591 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1593 pending->will_send_reply,
1594 pending->will_get_reply,
1595 pending->reply_serial);
1597 bus_expire_list_remove_link (connections->pending_replies,
1599 bus_pending_reply_free (pending);
1601 else if (pending->will_send_reply == connection)
1603 /* The reply isn't going to be sent, so set things
1604 * up so it will be expired right away
1606 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1608 pending->will_send_reply,
1609 pending->will_get_reply,
1610 pending->reply_serial);
1612 pending->will_send_reply = NULL;
1613 pending->expire_item.added_tv_sec = 0;
1614 pending->expire_item.added_tv_usec = 0;
1616 bus_expire_list_recheck_immediately (connections->pending_replies);
1626 BusPendingReply *pending;
1627 BusConnections *connections;
1628 } CancelPendingReplyData;
1631 cancel_pending_reply (void *data)
1633 CancelPendingReplyData *d = data;
1635 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1637 if (!bus_expire_list_remove (d->connections->pending_replies,
1638 &d->pending->expire_item))
1639 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1641 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1645 cancel_pending_reply_data_free (void *data)
1647 CancelPendingReplyData *d = data;
1649 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1651 /* d->pending should be either freed or still
1652 * in the list of pending replies (owned by someone
1660 * Record that a reply is allowed; return TRUE on success.
1663 bus_connections_expect_reply (BusConnections *connections,
1664 BusTransaction *transaction,
1665 DBusConnection *will_get_reply,
1666 DBusConnection *will_send_reply,
1667 DBusMessage *reply_to_this,
1670 BusPendingReply *pending;
1671 dbus_uint32_t reply_serial;
1673 CancelPendingReplyData *cprd;
1676 _dbus_assert (will_get_reply != NULL);
1677 _dbus_assert (will_send_reply != NULL);
1678 _dbus_assert (reply_to_this != NULL);
1680 if (dbus_message_get_no_reply (reply_to_this))
1681 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1683 reply_serial = dbus_message_get_serial (reply_to_this);
1685 link = bus_expire_list_get_first_link (connections->pending_replies);
1687 while (link != NULL)
1689 pending = link->data;
1691 if (pending->reply_serial == reply_serial &&
1692 pending->will_get_reply == will_get_reply &&
1693 pending->will_send_reply == will_send_reply)
1695 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1696 "Message has the same reply serial as a currently-outstanding existing method call");
1700 link = bus_expire_list_get_next_link (connections->pending_replies,
1702 if (pending->will_get_reply == will_get_reply)
1707 bus_context_get_max_replies_per_connection (connections->context))
1709 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1710 "The maximum number of pending replies per connection has been reached");
1714 pending = dbus_new0 (BusPendingReply, 1);
1715 if (pending == NULL)
1717 BUS_SET_OOM (error);
1721 #ifdef DBUS_ENABLE_VERBOSE_MODE
1722 /* so we can see a not-yet-added pending reply */
1723 pending->expire_item.added_tv_sec = 1;
1724 pending->expire_item.added_tv_usec = 1;
1727 pending->will_get_reply = will_get_reply;
1728 pending->will_send_reply = will_send_reply;
1729 pending->reply_serial = reply_serial;
1731 cprd = dbus_new0 (CancelPendingReplyData, 1);
1734 BUS_SET_OOM (error);
1735 bus_pending_reply_free (pending);
1739 if (!bus_expire_list_add (connections->pending_replies,
1740 &pending->expire_item))
1742 BUS_SET_OOM (error);
1744 bus_pending_reply_free (pending);
1748 if (!bus_transaction_add_cancel_hook (transaction,
1749 cancel_pending_reply,
1751 cancel_pending_reply_data_free))
1753 BUS_SET_OOM (error);
1754 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1756 bus_pending_reply_free (pending);
1760 cprd->pending = pending;
1761 cprd->connections = connections;
1763 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1764 &pending->expire_item.added_tv_usec);
1766 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1768 pending->will_send_reply,
1769 pending->will_get_reply,
1770 pending->reply_serial);
1778 BusConnections *connections;
1779 } CheckPendingReplyData;
1782 cancel_check_pending_reply (void *data)
1784 CheckPendingReplyData *d = data;
1786 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1788 bus_expire_list_add_link (d->connections->pending_replies,
1794 check_pending_reply_data_free (void *data)
1796 CheckPendingReplyData *d = data;
1798 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1800 if (d->link != NULL)
1802 BusPendingReply *pending = d->link->data;
1804 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1805 &pending->expire_item));
1807 bus_pending_reply_free (pending);
1808 _dbus_list_free_link (d->link);
1815 * Check whether a reply is allowed, remove BusPendingReply
1816 * if so, return TRUE if so.
1819 bus_connections_check_reply (BusConnections *connections,
1820 BusTransaction *transaction,
1821 DBusConnection *sending_reply,
1822 DBusConnection *receiving_reply,
1826 CheckPendingReplyData *cprd;
1828 dbus_uint32_t reply_serial;
1830 _dbus_assert (sending_reply != NULL);
1831 _dbus_assert (receiving_reply != NULL);
1833 reply_serial = dbus_message_get_reply_serial (reply);
1835 link = bus_expire_list_get_first_link (connections->pending_replies);
1836 while (link != NULL)
1838 BusPendingReply *pending = link->data;
1840 if (pending->reply_serial == reply_serial &&
1841 pending->will_get_reply == receiving_reply &&
1842 pending->will_send_reply == sending_reply)
1844 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1848 link = bus_expire_list_get_next_link (connections->pending_replies,
1854 _dbus_verbose ("No pending reply expected\n");
1859 cprd = dbus_new0 (CheckPendingReplyData, 1);
1862 BUS_SET_OOM (error);
1866 if (!bus_transaction_add_cancel_hook (transaction,
1867 cancel_check_pending_reply,
1869 check_pending_reply_data_free))
1871 BUS_SET_OOM (error);
1877 cprd->connections = connections;
1879 bus_expire_list_unlink (connections->pending_replies,
1882 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1890 * Note that this is fairly fragile; in particular, don't try to use
1891 * one transaction across any main loop iterations.
1896 BusTransaction *transaction;
1897 DBusMessage *message;
1898 DBusPreallocatedSend *preallocated;
1903 BusTransactionCancelFunction cancel_function;
1904 DBusFreeFunction free_data_function;
1908 struct BusTransaction
1910 DBusList *connections;
1911 BusContext *context;
1912 DBusList *cancel_hooks;
1916 message_to_send_free (DBusConnection *connection,
1917 MessageToSend *to_send)
1919 if (to_send->message)
1920 dbus_message_unref (to_send->message);
1922 if (to_send->preallocated)
1923 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1925 dbus_free (to_send);
1929 cancel_hook_cancel (void *element,
1932 CancelHook *ch = element;
1934 _dbus_verbose ("Running transaction cancel hook\n");
1936 if (ch->cancel_function)
1937 (* ch->cancel_function) (ch->data);
1941 cancel_hook_free (void *element,
1944 CancelHook *ch = element;
1946 if (ch->free_data_function)
1947 (* ch->free_data_function) (ch->data);
1953 free_cancel_hooks (BusTransaction *transaction)
1955 _dbus_list_foreach (&transaction->cancel_hooks,
1956 cancel_hook_free, NULL);
1958 _dbus_list_clear (&transaction->cancel_hooks);
1962 bus_transaction_new (BusContext *context)
1964 BusTransaction *transaction;
1966 transaction = dbus_new0 (BusTransaction, 1);
1967 if (transaction == NULL)
1970 transaction->context = context;
1976 bus_transaction_get_context (BusTransaction *transaction)
1978 return transaction->context;
1982 bus_transaction_get_connections (BusTransaction *transaction)
1984 return bus_context_get_connections (transaction->context);
1988 bus_transaction_send_from_driver (BusTransaction *transaction,
1989 DBusConnection *connection,
1990 DBusMessage *message)
1992 /* We have to set the sender to the driver, and have
1993 * to check security policy since it was not done in
1996 _dbus_verbose ("Sending %s %s %s from driver\n",
1997 dbus_message_get_interface (message) ?
1998 dbus_message_get_interface (message) : "(no interface)",
1999 dbus_message_get_member (message) ?
2000 dbus_message_get_member (message) : "(no member)",
2001 dbus_message_get_error_name (message) ?
2002 dbus_message_get_error_name (message) : "(no error name)");
2004 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2007 if (bus_connection_is_active (connection))
2009 if (!dbus_message_set_destination (message,
2010 bus_connection_get_name (connection)))
2014 /* bus driver never wants a reply */
2015 dbus_message_set_no_reply (message, TRUE);
2017 /* If security policy doesn't allow the message, we silently
2018 * eat it; the driver doesn't care about getting a reply.
2020 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2022 NULL, connection, connection, message, NULL))
2025 return bus_transaction_send (transaction, connection, message);
2029 bus_transaction_send (BusTransaction *transaction,
2030 DBusConnection *connection,
2031 DBusMessage *message)
2033 MessageToSend *to_send;
2034 BusConnectionData *d;
2037 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2038 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2039 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2041 dbus_message_get_interface (message) ?
2042 dbus_message_get_interface (message) : "(unset)",
2043 dbus_message_get_member (message) ?
2044 dbus_message_get_member (message) : "(unset)",
2045 dbus_message_get_error_name (message) ?
2046 dbus_message_get_error_name (message) : "(unset)",
2047 dbus_connection_get_is_connected (connection) ?
2048 "" : " (disconnected)");
2050 _dbus_assert (dbus_message_get_sender (message) != NULL);
2052 if (!dbus_connection_get_is_connected (connection))
2053 return TRUE; /* silently ignore disconnected connections */
2055 d = BUS_CONNECTION_DATA (connection);
2056 _dbus_assert (d != NULL);
2058 to_send = dbus_new (MessageToSend, 1);
2059 if (to_send == NULL)
2064 to_send->preallocated = dbus_connection_preallocate_send (connection);
2065 if (to_send->preallocated == NULL)
2067 dbus_free (to_send);
2071 dbus_message_ref (message);
2072 to_send->message = message;
2073 to_send->transaction = transaction;
2075 _dbus_verbose ("about to prepend message\n");
2077 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2079 message_to_send_free (connection, to_send);
2083 _dbus_verbose ("prepended message\n");
2085 /* See if we already had this connection in the list
2086 * for this transaction. If we have a pending message,
2087 * then we should already be in transaction->connections
2089 link = _dbus_list_get_first_link (&d->transaction_messages);
2090 _dbus_assert (link->data == to_send);
2091 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2092 while (link != NULL)
2094 MessageToSend *m = link->data;
2095 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2097 if (m->transaction == transaction)
2105 if (!_dbus_list_prepend (&transaction->connections, connection))
2107 _dbus_list_remove (&d->transaction_messages, to_send);
2108 message_to_send_free (connection, to_send);
2117 connection_cancel_transaction (DBusConnection *connection,
2118 BusTransaction *transaction)
2121 BusConnectionData *d;
2123 d = BUS_CONNECTION_DATA (connection);
2124 _dbus_assert (d != NULL);
2126 link = _dbus_list_get_first_link (&d->transaction_messages);
2127 while (link != NULL)
2129 MessageToSend *m = link->data;
2130 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2132 if (m->transaction == transaction)
2134 _dbus_list_remove_link (&d->transaction_messages,
2137 message_to_send_free (connection, m);
2145 bus_transaction_cancel_and_free (BusTransaction *transaction)
2147 DBusConnection *connection;
2149 _dbus_verbose ("TRANSACTION: cancelled\n");
2151 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2152 connection_cancel_transaction (connection, transaction);
2154 _dbus_assert (transaction->connections == NULL);
2156 _dbus_list_foreach (&transaction->cancel_hooks,
2157 cancel_hook_cancel, NULL);
2159 free_cancel_hooks (transaction);
2161 dbus_free (transaction);
2165 connection_execute_transaction (DBusConnection *connection,
2166 BusTransaction *transaction)
2169 BusConnectionData *d;
2171 d = BUS_CONNECTION_DATA (connection);
2172 _dbus_assert (d != NULL);
2174 /* Send the queue in order (FIFO) */
2175 link = _dbus_list_get_last_link (&d->transaction_messages);
2176 while (link != NULL)
2178 MessageToSend *m = link->data;
2179 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2181 if (m->transaction == transaction)
2183 _dbus_list_remove_link (&d->transaction_messages,
2186 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2188 dbus_connection_send_preallocated (connection,
2193 m->preallocated = NULL; /* so we don't double-free it */
2195 message_to_send_free (connection, m);
2203 bus_transaction_execute_and_free (BusTransaction *transaction)
2205 /* For each connection in transaction->connections
2208 DBusConnection *connection;
2210 _dbus_verbose ("TRANSACTION: executing\n");
2212 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2213 connection_execute_transaction (connection, transaction);
2215 _dbus_assert (transaction->connections == NULL);
2217 free_cancel_hooks (transaction);
2219 dbus_free (transaction);
2223 bus_connection_remove_transactions (DBusConnection *connection)
2225 MessageToSend *to_send;
2226 BusConnectionData *d;
2228 d = BUS_CONNECTION_DATA (connection);
2229 _dbus_assert (d != NULL);
2231 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2233 /* only has an effect for the first MessageToSend listing this transaction */
2234 _dbus_list_remove (&to_send->transaction->connections,
2237 _dbus_list_remove (&d->transaction_messages, to_send);
2238 message_to_send_free (connection, to_send);
2243 * Converts the DBusError to a message reply
2246 bus_transaction_send_error_reply (BusTransaction *transaction,
2247 DBusConnection *connection,
2248 const DBusError *error,
2249 DBusMessage *in_reply_to)
2253 _dbus_assert (error != NULL);
2254 _DBUS_ASSERT_ERROR_IS_SET (error);
2256 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2257 error->name, error->message);
2259 reply = dbus_message_new_error (in_reply_to,
2265 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2267 dbus_message_unref (reply);
2271 dbus_message_unref (reply);
2277 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2278 BusTransactionCancelFunction cancel_function,
2280 DBusFreeFunction free_data_function)
2284 ch = dbus_new (CancelHook, 1);
2288 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2289 cancel_function, data);
2291 ch->cancel_function = cancel_function;
2293 ch->free_data_function = free_data_function;
2295 /* It's important that the hooks get run in reverse order that they
2298 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))