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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "connection.h"
29 #include "expirelist.h"
31 #include <dbus/dbus-list.h>
32 #include <dbus/dbus-hash.h>
33 #include <dbus/dbus-timeout.h>
35 /* Trim executed commands to this length; we want to keep logs readable */
36 #define MAX_LOG_COMMAND_LEN 50
38 static void bus_connection_remove_transactions (DBusConnection *connection);
42 BusExpireItem expire_item;
44 DBusConnection *will_get_reply;
45 DBusConnection *will_send_reply;
47 dbus_uint32_t reply_serial;
54 DBusList *completed; /**< List of all completed connections */
55 int n_completed; /**< Length of completed list */
56 DBusList *incomplete; /**< List of all not-yet-active connections */
57 int n_incomplete; /**< Length of incomplete list */
59 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
60 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
61 int stamp; /**< Incrementing number */
62 BusExpireList *pending_replies; /**< List of pending replies */
65 static dbus_int32_t connection_data_slot = -1;
69 BusConnections *connections;
70 DBusList *link_in_connection_list;
71 DBusConnection *connection;
72 DBusList *services_owned;
74 DBusList *match_rules;
77 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
78 DBusMessage *oom_message;
79 DBusPreallocatedSend *oom_preallocated;
80 BusClientPolicy *policy;
82 char *cached_loginfo_string;
83 BusSELinuxID *selinux_id;
85 long connection_tv_sec; /**< Time when we connected (seconds component) */
86 long connection_tv_usec; /**< Time when we connected (microsec component) */
87 int stamp; /**< connections->stamp last time we were traversed */
90 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
94 static void bus_connection_drop_pending_replies (BusConnections *connections,
95 DBusConnection *connection);
97 static dbus_bool_t expire_incomplete_timeout (void *data);
99 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
102 connection_get_loop (DBusConnection *connection)
104 BusConnectionData *d;
106 d = BUS_CONNECTION_DATA (connection);
108 return bus_context_get_loop (d->connections->context);
113 get_connections_for_uid (BusConnections *connections,
119 /* val is NULL is 0 when it isn't in the hash yet */
121 val = _dbus_hash_table_lookup_ulong (connections->completed_by_user,
124 current_count = _DBUS_POINTER_TO_INT (val);
126 return current_count;
130 adjust_connections_for_uid (BusConnections *connections,
136 current_count = get_connections_for_uid (connections, uid);
138 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
139 ": was %d adjustment %d making %d\n",
140 uid, current_count, adjustment, current_count + adjustment);
142 _dbus_assert (current_count >= 0);
144 current_count += adjustment;
146 _dbus_assert (current_count >= 0);
148 if (current_count == 0)
150 _dbus_hash_table_remove_ulong (connections->completed_by_user, uid);
157 retval = _dbus_hash_table_insert_ulong (connections->completed_by_user,
158 uid, _DBUS_INT_TO_POINTER (current_count));
160 /* only positive adjustment can fail as otherwise
161 * a hash entry should already exist
163 _dbus_assert (adjustment > 0 ||
164 (adjustment <= 0 && retval));
171 bus_connection_disconnected (DBusConnection *connection)
173 BusConnectionData *d;
175 BusMatchmaker *matchmaker;
177 d = BUS_CONNECTION_DATA (connection);
178 _dbus_assert (d != NULL);
180 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
181 d->name ? d->name : "(inactive)");
183 /* Delete our match rules */
184 if (d->n_match_rules > 0)
186 matchmaker = bus_context_get_matchmaker (d->connections->context);
187 bus_matchmaker_disconnected (matchmaker, connection);
190 /* Drop any service ownership. Unfortunately, this requires
191 * memory allocation and there doesn't seem to be a good way to
192 * handle it other than sleeping; we can't "fail" the operation of
193 * disconnecting a client, and preallocating a broadcast "service is
194 * now gone" message for every client-service pair seems kind of
197 while ((service = _dbus_list_get_last (&d->services_owned)))
199 BusTransaction *transaction;
204 dbus_error_init (&error);
206 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
207 _dbus_wait_for_memory ();
209 if (!bus_service_remove_owner (service, connection,
210 transaction, &error))
212 _DBUS_ASSERT_ERROR_IS_SET (&error);
214 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
216 dbus_error_free (&error);
217 bus_transaction_cancel_and_free (transaction);
218 _dbus_wait_for_memory ();
223 _dbus_verbose ("Failed to remove service owner: %s %s\n",
224 error.name, error.message);
225 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
229 bus_transaction_execute_and_free (transaction);
232 bus_dispatch_remove_connection (connection);
234 /* no more watching */
235 if (!dbus_connection_set_watch_functions (connection,
239 _dbus_assert_not_reached ("setting watch functions to NULL failed");
241 if (!dbus_connection_set_timeout_functions (connection,
245 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
247 dbus_connection_set_unix_user_function (connection,
249 dbus_connection_set_windows_user_function (connection,
252 dbus_connection_set_dispatch_status_function (connection,
255 bus_connection_remove_transactions (connection);
257 if (d->link_in_connection_list != NULL)
263 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
264 d->link_in_connection_list = NULL;
265 d->connections->n_completed -= 1;
267 if (dbus_connection_get_unix_user (connection, &uid))
269 if (!adjust_connections_for_uid (d->connections,
271 _dbus_assert_not_reached ("adjusting downward should never fail");
276 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
277 d->link_in_connection_list = NULL;
278 d->connections->n_incomplete -= 1;
281 _dbus_assert (d->connections->n_incomplete >= 0);
282 _dbus_assert (d->connections->n_completed >= 0);
285 bus_connection_drop_pending_replies (d->connections, connection);
287 /* frees "d" as side effect */
288 dbus_connection_set_data (connection,
289 connection_data_slot,
292 dbus_connection_unref (connection);
296 connection_watch_callback (DBusWatch *watch,
297 unsigned int condition,
300 /* FIXME this can be done in dbus-mainloop.c
301 * if the code in activation.c for the babysitter
302 * watch handler is fixed.
306 _dbus_verbose ("Calling handle_watch\n");
308 return dbus_watch_handle (watch, condition);
312 add_connection_watch (DBusWatch *watch,
315 DBusConnection *connection = data;
317 return _dbus_loop_add_watch (connection_get_loop (connection),
318 watch, connection_watch_callback, connection,
323 remove_connection_watch (DBusWatch *watch,
326 DBusConnection *connection = data;
328 _dbus_loop_remove_watch (connection_get_loop (connection),
329 watch, connection_watch_callback, connection);
333 connection_timeout_callback (DBusTimeout *timeout,
336 /* DBusConnection *connection = data; */
338 /* can return FALSE on OOM but we just let it fire again later */
339 dbus_timeout_handle (timeout);
343 add_connection_timeout (DBusTimeout *timeout,
346 DBusConnection *connection = data;
348 return _dbus_loop_add_timeout (connection_get_loop (connection),
349 timeout, connection_timeout_callback, connection, NULL);
353 remove_connection_timeout (DBusTimeout *timeout,
356 DBusConnection *connection = data;
358 _dbus_loop_remove_timeout (connection_get_loop (connection),
359 timeout, connection_timeout_callback, connection);
363 dispatch_status_function (DBusConnection *connection,
364 DBusDispatchStatus new_status,
367 DBusLoop *loop = data;
369 if (new_status != DBUS_DISPATCH_COMPLETE)
371 while (!_dbus_loop_queue_dispatch (loop, connection))
372 _dbus_wait_for_memory ();
377 allow_unix_user_function (DBusConnection *connection,
381 BusConnectionData *d;
383 d = BUS_CONNECTION_DATA (connection);
385 _dbus_assert (d != NULL);
387 return bus_context_allow_unix_user (d->connections->context, uid);
391 free_connection_data (void *data)
393 BusConnectionData *d = data;
395 /* services_owned should be NULL since we should be disconnected */
396 _dbus_assert (d->services_owned == NULL);
397 _dbus_assert (d->n_services_owned == 0);
399 _dbus_assert (d->transaction_messages == NULL);
401 if (d->oom_preallocated)
402 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
405 dbus_message_unref (d->oom_message);
408 bus_client_policy_unref (d->policy);
411 bus_selinux_id_unref (d->selinux_id);
413 dbus_free (d->cached_loginfo_string);
421 call_timeout_callback (DBusTimeout *timeout,
424 /* can return FALSE on OOM but we just let it fire again later */
425 dbus_timeout_handle (timeout);
429 bus_connections_new (BusContext *context)
431 BusConnections *connections;
433 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
436 connections = dbus_new0 (BusConnections, 1);
437 if (connections == NULL)
440 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_ULONG,
442 if (connections->completed_by_user == NULL)
445 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
446 expire_incomplete_timeout,
448 if (connections->expire_timeout == NULL)
451 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
453 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
454 bus_context_get_reply_timeout (context),
455 bus_pending_reply_expired,
457 if (connections->pending_replies == NULL)
460 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
461 connections->expire_timeout,
462 call_timeout_callback, NULL, NULL))
465 connections->refcount = 1;
466 connections->context = context;
471 bus_expire_list_free (connections->pending_replies);
473 _dbus_timeout_unref (connections->expire_timeout);
475 _dbus_hash_table_unref (connections->completed_by_user);
477 dbus_free (connections);
479 dbus_connection_free_data_slot (&connection_data_slot);
485 bus_connections_ref (BusConnections *connections)
487 _dbus_assert (connections->refcount > 0);
488 connections->refcount += 1;
494 bus_connections_unref (BusConnections *connections)
496 _dbus_assert (connections->refcount > 0);
497 connections->refcount -= 1;
498 if (connections->refcount == 0)
500 /* drop all incomplete */
501 while (connections->incomplete != NULL)
503 DBusConnection *connection;
505 connection = connections->incomplete->data;
507 dbus_connection_ref (connection);
508 dbus_connection_close (connection);
509 bus_connection_disconnected (connection);
510 dbus_connection_unref (connection);
513 _dbus_assert (connections->n_incomplete == 0);
515 /* drop all real connections */
516 while (connections->completed != NULL)
518 DBusConnection *connection;
520 connection = connections->completed->data;
522 dbus_connection_ref (connection);
523 dbus_connection_close (connection);
524 bus_connection_disconnected (connection);
525 dbus_connection_unref (connection);
528 _dbus_assert (connections->n_completed == 0);
530 bus_expire_list_free (connections->pending_replies);
532 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
533 connections->expire_timeout,
534 call_timeout_callback, NULL);
536 _dbus_timeout_unref (connections->expire_timeout);
538 _dbus_hash_table_unref (connections->completed_by_user);
540 dbus_free (connections);
542 dbus_connection_free_data_slot (&connection_data_slot);
546 /* Used for logging */
548 cache_peer_loginfo_string (BusConnectionData *d,
549 DBusConnection *connection)
551 DBusString loginfo_buf;
555 dbus_bool_t prev_added;
557 if (!_dbus_string_init (&loginfo_buf))
561 if (dbus_connection_get_unix_user (connection, &uid))
563 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
569 if (dbus_connection_get_unix_process_id (connection, &pid))
573 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
576 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
578 /* Ignore errors here */
579 if (_dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL))
581 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
586 if (dbus_connection_get_windows_user (connection, &windows_sid))
588 if (!_dbus_string_append_printf (&loginfo_buf, "sid=\"%s\" ", windows_sid))
590 dbus_free (windows_sid);
593 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
596 _dbus_string_free (&loginfo_buf);
600 _dbus_string_free (&loginfo_buf);
605 bus_connections_setup_connection (BusConnections *connections,
606 DBusConnection *connection)
609 BusConnectionData *d;
614 d = dbus_new0 (BusConnectionData, 1);
619 d->connections = connections;
620 d->connection = connection;
622 _dbus_get_current_time (&d->connection_tv_sec,
623 &d->connection_tv_usec);
625 _dbus_assert (connection_data_slot >= 0);
627 if (!dbus_connection_set_data (connection,
628 connection_data_slot,
629 d, free_connection_data))
635 dbus_connection_set_route_peer_messages (connection, TRUE);
639 dbus_error_init (&error);
640 d->selinux_id = bus_selinux_init_connection_id (connection,
642 if (dbus_error_is_set (&error))
644 /* This is a bit bogus because we pretend all errors
645 * are OOM; this is done because we know that in bus.c
646 * an OOM error disconnects the connection, which is
647 * the same thing we want on any other error.
649 dbus_error_free (&error);
653 if (!dbus_connection_set_watch_functions (connection,
654 add_connection_watch,
655 remove_connection_watch,
661 if (!dbus_connection_set_timeout_functions (connection,
662 add_connection_timeout,
663 remove_connection_timeout,
668 /* For now we don't need to set a Windows user function because
669 * there are no policies in the config file controlling what
670 * Windows users can connect. The default 'same user that owns the
671 * bus can connect' behavior of DBusConnection is fine on Windows.
673 dbus_connection_set_unix_user_function (connection,
674 allow_unix_user_function,
677 dbus_connection_set_dispatch_status_function (connection,
678 dispatch_status_function,
679 bus_context_get_loop (connections->context),
682 d->link_in_connection_list = _dbus_list_alloc_link (connection);
683 if (d->link_in_connection_list == NULL)
686 /* Setup the connection with the dispatcher */
687 if (!bus_dispatch_add_connection (connection))
690 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
692 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
694 bus_dispatch_remove_connection (connection);
699 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
700 connections->n_incomplete += 1;
702 dbus_connection_ref (connection);
704 /* Note that we might disconnect ourselves here, but it only takes
705 * effect on return to the main loop. We call this to free up
706 * expired connections if possible, and to queue the timeout for our
709 bus_connections_expire_incomplete (connections);
711 /* And we might also disconnect ourselves here, but again it
712 * only takes effect on return to main loop.
714 if (connections->n_incomplete >
715 bus_context_get_max_incomplete_connections (connections->context))
717 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
719 _dbus_assert (connections->incomplete != NULL);
720 /* Disconnect the oldest unauthenticated connection. FIXME
721 * would it be more secure to drop a *random* connection? This
722 * algorithm seems to mean that if someone can create new
723 * connections quickly enough, they can keep anyone else from
724 * completing authentication. But random may or may not really
725 * help with that, a more elaborate solution might be required.
727 dbus_connection_close (connections->incomplete->data);
736 bus_selinux_id_unref (d->selinux_id);
737 d->selinux_id = NULL;
739 if (!dbus_connection_set_watch_functions (connection,
743 _dbus_assert_not_reached ("setting watch functions to NULL failed");
745 if (!dbus_connection_set_timeout_functions (connection,
749 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
751 dbus_connection_set_unix_user_function (connection,
754 dbus_connection_set_windows_user_function (connection,
757 dbus_connection_set_dispatch_status_function (connection,
760 if (d->link_in_connection_list != NULL)
762 _dbus_assert (d->link_in_connection_list->next == NULL);
763 _dbus_assert (d->link_in_connection_list->prev == NULL);
764 _dbus_list_free_link (d->link_in_connection_list);
765 d->link_in_connection_list = NULL;
768 if (!dbus_connection_set_data (connection,
769 connection_data_slot,
771 _dbus_assert_not_reached ("failed to set connection data to null");
773 /* "d" has now been freed */
780 bus_connections_expire_incomplete (BusConnections *connections)
786 if (connections->incomplete != NULL)
788 long tv_sec, tv_usec;
792 _dbus_get_current_time (&tv_sec, &tv_usec);
793 auth_timeout = bus_context_get_auth_timeout (connections->context);
795 link = _dbus_list_get_first_link (&connections->incomplete);
798 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
799 DBusConnection *connection;
800 BusConnectionData *d;
803 connection = link->data;
805 d = BUS_CONNECTION_DATA (connection);
807 _dbus_assert (d != NULL);
809 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
810 d->connection_tv_usec,
813 if (elapsed >= (double) auth_timeout)
815 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
816 dbus_connection_close (connection);
820 /* We can end the loop, since the connections are in oldest-first order */
821 next_interval = ((double)auth_timeout) - elapsed;
822 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
823 connection, next_interval);
832 bus_expire_timeout_set_interval (connections->expire_timeout,
837 expire_incomplete_timeout (void *data)
839 BusConnections *connections = data;
841 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
843 /* note that this may remove the timeout */
844 bus_connections_expire_incomplete (connections);
850 bus_connection_get_unix_groups (DBusConnection *connection,
851 unsigned long **groups,
855 BusConnectionData *d;
858 d = BUS_CONNECTION_DATA (connection);
860 _dbus_assert (d != NULL);
865 if (dbus_connection_get_unix_user (connection, &uid))
867 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
869 _dbus_verbose ("Did not get any groups for UID %lu\n",
875 _dbus_verbose ("Got %d groups for UID %lu\n",
881 return TRUE; /* successfully got 0 groups */
885 bus_connection_is_in_unix_group (DBusConnection *connection,
889 unsigned long *group_ids;
892 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
897 while (i < n_group_ids)
899 if (group_ids[i] == gid)
901 dbus_free (group_ids);
907 dbus_free (group_ids);
912 bus_connection_get_loginfo (DBusConnection *connection)
914 BusConnectionData *d;
916 d = BUS_CONNECTION_DATA (connection);
918 if (!bus_connection_is_active (connection))
920 return d->cached_loginfo_string;
924 bus_connection_get_policy (DBusConnection *connection)
926 BusConnectionData *d;
928 d = BUS_CONNECTION_DATA (connection);
930 _dbus_assert (d != NULL);
931 _dbus_assert (d->policy != NULL);
937 foreach_active (BusConnections *connections,
938 BusConnectionForeachFunction function,
943 link = _dbus_list_get_first_link (&connections->completed);
946 DBusConnection *connection = link->data;
947 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
949 if (!(* function) (connection, data))
959 foreach_inactive (BusConnections *connections,
960 BusConnectionForeachFunction function,
965 link = _dbus_list_get_first_link (&connections->incomplete);
968 DBusConnection *connection = link->data;
969 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
971 if (!(* function) (connection, data))
981 * Calls function on each active connection; if the function returns
982 * #FALSE, stops iterating. Active connections are authenticated
983 * and have sent a Hello message.
985 * @param connections the connections object
986 * @param function the function
987 * @param data data to pass to it as a second arg
990 bus_connections_foreach_active (BusConnections *connections,
991 BusConnectionForeachFunction function,
994 foreach_active (connections, function, data);
998 * Calls function on each connection; if the function returns
999 * #FALSE, stops iterating.
1001 * @param connections the connections object
1002 * @param function the function
1003 * @param data data to pass to it as a second arg
1006 bus_connections_foreach (BusConnections *connections,
1007 BusConnectionForeachFunction function,
1010 if (!foreach_active (connections, function, data))
1013 foreach_inactive (connections, function, data);
1017 bus_connections_get_context (BusConnections *connections)
1019 return connections->context;
1023 * This is used to avoid covering the same connection twice when
1024 * traversing connections. Note that it assumes we will
1025 * bus_connection_mark_stamp() each connection at least once per
1026 * INT_MAX increments of the global stamp, or wraparound would break
1030 bus_connections_increment_stamp (BusConnections *connections)
1032 connections->stamp += 1;
1035 /* Mark connection with current stamp, return TRUE if it
1036 * didn't already have that stamp
1039 bus_connection_mark_stamp (DBusConnection *connection)
1041 BusConnectionData *d;
1043 d = BUS_CONNECTION_DATA (connection);
1045 _dbus_assert (d != NULL);
1047 if (d->stamp == d->connections->stamp)
1051 d->stamp = d->connections->stamp;
1057 bus_connection_get_context (DBusConnection *connection)
1059 BusConnectionData *d;
1061 d = BUS_CONNECTION_DATA (connection);
1063 _dbus_assert (d != NULL);
1065 return d->connections->context;
1069 bus_connection_get_connections (DBusConnection *connection)
1071 BusConnectionData *d;
1073 d = BUS_CONNECTION_DATA (connection);
1075 _dbus_assert (d != NULL);
1077 return d->connections;
1081 bus_connection_get_registry (DBusConnection *connection)
1083 BusConnectionData *d;
1085 d = BUS_CONNECTION_DATA (connection);
1087 _dbus_assert (d != NULL);
1089 return bus_context_get_registry (d->connections->context);
1093 bus_connection_get_activation (DBusConnection *connection)
1095 BusConnectionData *d;
1097 d = BUS_CONNECTION_DATA (connection);
1099 _dbus_assert (d != NULL);
1101 return bus_context_get_activation (d->connections->context);
1105 bus_connection_get_matchmaker (DBusConnection *connection)
1107 BusConnectionData *d;
1109 d = BUS_CONNECTION_DATA (connection);
1111 _dbus_assert (d != NULL);
1113 return bus_context_get_matchmaker (d->connections->context);
1117 bus_connection_get_selinux_id (DBusConnection *connection)
1119 BusConnectionData *d;
1121 d = BUS_CONNECTION_DATA (connection);
1123 _dbus_assert (d != NULL);
1125 return d->selinux_id;
1129 * Checks whether the connection is registered with the message bus.
1131 * @param connection the connection
1132 * @returns #TRUE if we're an active message bus participant
1135 bus_connection_is_active (DBusConnection *connection)
1137 BusConnectionData *d;
1139 d = BUS_CONNECTION_DATA (connection);
1141 return d != NULL && d->name != NULL;
1145 bus_connection_preallocate_oom_error (DBusConnection *connection)
1147 DBusMessage *message;
1148 DBusPreallocatedSend *preallocated;
1149 BusConnectionData *d;
1151 d = BUS_CONNECTION_DATA (connection);
1153 _dbus_assert (d != NULL);
1155 if (d->oom_preallocated != NULL)
1158 preallocated = dbus_connection_preallocate_send (connection);
1159 if (preallocated == NULL)
1162 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1164 if (message == NULL)
1166 dbus_connection_free_preallocated_send (connection, preallocated);
1170 /* d->name may be NULL, but that is OK */
1171 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1172 !dbus_message_set_destination (message, d->name) ||
1173 !dbus_message_set_sender (message,
1176 dbus_connection_free_preallocated_send (connection, preallocated);
1177 dbus_message_unref (message);
1181 /* set reply serial to placeholder value just so space is already allocated
1184 if (!dbus_message_set_reply_serial (message, 14))
1186 dbus_connection_free_preallocated_send (connection, preallocated);
1187 dbus_message_unref (message);
1191 d->oom_message = message;
1192 d->oom_preallocated = preallocated;
1198 bus_connection_send_oom_error (DBusConnection *connection,
1199 DBusMessage *in_reply_to)
1201 BusConnectionData *d;
1203 d = BUS_CONNECTION_DATA (connection);
1205 _dbus_assert (d != NULL);
1206 _dbus_assert (d->oom_message != NULL);
1208 /* should always succeed since we set it to a placeholder earlier */
1209 if (!dbus_message_set_reply_serial (d->oom_message,
1210 dbus_message_get_serial (in_reply_to)))
1211 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1213 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1215 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1216 d->oom_message, NULL);
1218 dbus_message_unref (d->oom_message);
1219 d->oom_message = NULL;
1220 d->oom_preallocated = NULL;
1224 bus_connection_add_match_rule_link (DBusConnection *connection,
1227 BusConnectionData *d;
1229 d = BUS_CONNECTION_DATA (connection);
1230 _dbus_assert (d != NULL);
1232 _dbus_list_append_link (&d->match_rules, link);
1234 d->n_match_rules += 1;
1238 bus_connection_add_match_rule (DBusConnection *connection,
1243 link = _dbus_list_alloc_link (rule);
1248 bus_connection_add_match_rule_link (connection, link);
1254 bus_connection_remove_match_rule (DBusConnection *connection,
1257 BusConnectionData *d;
1259 d = BUS_CONNECTION_DATA (connection);
1260 _dbus_assert (d != NULL);
1262 _dbus_list_remove_last (&d->match_rules, rule);
1264 d->n_match_rules -= 1;
1265 _dbus_assert (d->n_match_rules >= 0);
1269 bus_connection_get_n_match_rules (DBusConnection *connection)
1271 BusConnectionData *d;
1273 d = BUS_CONNECTION_DATA (connection);
1274 _dbus_assert (d != NULL);
1276 return d->n_match_rules;
1280 bus_connection_add_owned_service_link (DBusConnection *connection,
1283 BusConnectionData *d;
1285 d = BUS_CONNECTION_DATA (connection);
1286 _dbus_assert (d != NULL);
1288 _dbus_list_append_link (&d->services_owned, link);
1290 d->n_services_owned += 1;
1294 bus_connection_add_owned_service (DBusConnection *connection,
1295 BusService *service)
1299 link = _dbus_list_alloc_link (service);
1304 bus_connection_add_owned_service_link (connection, link);
1310 bus_connection_remove_owned_service (DBusConnection *connection,
1311 BusService *service)
1313 BusConnectionData *d;
1315 d = BUS_CONNECTION_DATA (connection);
1316 _dbus_assert (d != NULL);
1318 _dbus_list_remove_last (&d->services_owned, service);
1320 d->n_services_owned -= 1;
1321 _dbus_assert (d->n_services_owned >= 0);
1325 bus_connection_get_n_services_owned (DBusConnection *connection)
1327 BusConnectionData *d;
1329 d = BUS_CONNECTION_DATA (connection);
1330 _dbus_assert (d != NULL);
1332 return d->n_services_owned;
1336 bus_connection_complete (DBusConnection *connection,
1337 const DBusString *name,
1340 BusConnectionData *d;
1343 d = BUS_CONNECTION_DATA (connection);
1344 _dbus_assert (d != NULL);
1345 _dbus_assert (d->name == NULL);
1346 _dbus_assert (d->policy == NULL);
1348 _dbus_assert (!bus_connection_is_active (connection));
1350 if (!_dbus_string_copy_data (name, &d->name))
1352 BUS_SET_OOM (error);
1356 _dbus_assert (d->name != NULL);
1358 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1360 d->policy = bus_context_create_client_policy (d->connections->context,
1364 /* we may have a NULL policy on OOM or error getting list of
1365 * groups for a user. In the latter case we don't handle it so
1366 * well currently, as it will just keep failing over and over.
1369 if (d->policy == NULL)
1371 _dbus_verbose ("Failed to create security policy for connection %p\n",
1373 _DBUS_ASSERT_ERROR_IS_SET (error);
1374 dbus_free (d->name);
1379 if (dbus_connection_get_unix_user (connection, &uid))
1381 if (!adjust_connections_for_uid (d->connections,
1386 /* Create and cache a string which holds information about the
1387 * peer process; used for logging purposes.
1389 if (!cache_peer_loginfo_string (d, connection))
1392 /* Now the connection is active, move it between lists */
1393 _dbus_list_unlink (&d->connections->incomplete,
1394 d->link_in_connection_list);
1395 d->connections->n_incomplete -= 1;
1396 _dbus_list_append_link (&d->connections->completed,
1397 d->link_in_connection_list);
1398 d->connections->n_completed += 1;
1400 _dbus_assert (d->connections->n_incomplete >= 0);
1401 _dbus_assert (d->connections->n_completed > 0);
1403 /* See if we can remove the timeout */
1404 bus_connections_expire_incomplete (d->connections);
1406 _dbus_assert (bus_connection_is_active (connection));
1410 BUS_SET_OOM (error);
1411 dbus_free (d->name);
1414 bus_client_policy_unref (d->policy);
1420 bus_connection_get_name (DBusConnection *connection)
1422 BusConnectionData *d;
1424 d = BUS_CONNECTION_DATA (connection);
1425 _dbus_assert (d != NULL);
1431 * Check whether completing the passed-in connection would
1432 * exceed limits, and if so set error and return #FALSE
1435 bus_connections_check_limits (BusConnections *connections,
1436 DBusConnection *requesting_completion,
1439 BusConnectionData *d;
1442 d = BUS_CONNECTION_DATA (requesting_completion);
1443 _dbus_assert (d != NULL);
1445 _dbus_assert (d->name == NULL);
1447 if (connections->n_completed >=
1448 bus_context_get_max_completed_connections (connections->context))
1450 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1451 "The maximum number of active connections has been reached");
1455 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1457 if (get_connections_for_uid (connections, uid) >=
1458 bus_context_get_max_connections_per_user (connections->context))
1460 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1461 "The maximum number of active connections for UID %lu has been reached",
1471 bus_pending_reply_free (BusPendingReply *pending)
1473 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1475 pending->will_send_reply,
1476 pending->will_get_reply,
1477 pending->reply_serial);
1479 dbus_free (pending);
1483 bus_pending_reply_send_no_reply (BusConnections *connections,
1484 BusTransaction *transaction,
1485 BusPendingReply *pending)
1487 DBusMessage *message;
1488 DBusMessageIter iter;
1494 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1495 if (message == NULL)
1498 dbus_message_set_no_reply (message, TRUE);
1500 if (!dbus_message_set_reply_serial (message,
1501 pending->reply_serial))
1504 if (!dbus_message_set_error_name (message,
1505 DBUS_ERROR_NO_REPLY))
1508 errmsg = "Message did not receive a reply (timeout by message bus)";
1509 dbus_message_iter_init_append (message, &iter);
1510 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1513 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1520 dbus_message_unref (message);
1525 bus_pending_reply_expired (BusExpireList *list,
1529 BusPendingReply *pending = link->data;
1530 BusConnections *connections = data;
1531 BusTransaction *transaction;
1533 /* No reply is forthcoming. So nuke it if we can. If not,
1534 * leave it in the list to try expiring again later when we
1538 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1540 pending->will_send_reply,
1541 pending->will_get_reply,
1542 pending->reply_serial);
1544 transaction = bus_transaction_new (connections->context);
1545 if (transaction == NULL)
1548 if (!bus_pending_reply_send_no_reply (connections,
1552 bus_transaction_cancel_and_free (transaction);
1556 bus_expire_list_remove_link (connections->pending_replies, link);
1558 bus_pending_reply_free (pending);
1559 bus_transaction_execute_and_free (transaction);
1565 bus_connection_drop_pending_replies (BusConnections *connections,
1566 DBusConnection *connection)
1568 /* The DBusConnection is almost 100% finalized here, so you can't
1569 * do anything with it except check for pointer equality
1573 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1576 link = bus_expire_list_get_first_link (connections->pending_replies);
1577 while (link != NULL)
1580 BusPendingReply *pending;
1582 next = bus_expire_list_get_next_link (connections->pending_replies,
1584 pending = link->data;
1586 if (pending->will_get_reply == connection)
1588 /* We don't need to track this pending reply anymore */
1590 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1592 pending->will_send_reply,
1593 pending->will_get_reply,
1594 pending->reply_serial);
1596 bus_expire_list_remove_link (connections->pending_replies,
1598 bus_pending_reply_free (pending);
1600 else if (pending->will_send_reply == connection)
1602 /* The reply isn't going to be sent, so set things
1603 * up so it will be expired right away
1605 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1607 pending->will_send_reply,
1608 pending->will_get_reply,
1609 pending->reply_serial);
1611 pending->will_send_reply = NULL;
1612 pending->expire_item.added_tv_sec = 0;
1613 pending->expire_item.added_tv_usec = 0;
1615 bus_expire_list_recheck_immediately (connections->pending_replies);
1625 BusPendingReply *pending;
1626 BusConnections *connections;
1627 } CancelPendingReplyData;
1630 cancel_pending_reply (void *data)
1632 CancelPendingReplyData *d = data;
1634 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1636 if (!bus_expire_list_remove (d->connections->pending_replies,
1637 &d->pending->expire_item))
1638 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1640 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1644 cancel_pending_reply_data_free (void *data)
1646 CancelPendingReplyData *d = data;
1648 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1650 /* d->pending should be either freed or still
1651 * in the list of pending replies (owned by someone
1659 * Record that a reply is allowed; return TRUE on success.
1662 bus_connections_expect_reply (BusConnections *connections,
1663 BusTransaction *transaction,
1664 DBusConnection *will_get_reply,
1665 DBusConnection *will_send_reply,
1666 DBusMessage *reply_to_this,
1669 BusPendingReply *pending;
1670 dbus_uint32_t reply_serial;
1672 CancelPendingReplyData *cprd;
1675 _dbus_assert (will_get_reply != NULL);
1676 _dbus_assert (will_send_reply != NULL);
1677 _dbus_assert (reply_to_this != NULL);
1679 if (dbus_message_get_no_reply (reply_to_this))
1680 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1682 reply_serial = dbus_message_get_serial (reply_to_this);
1684 link = bus_expire_list_get_first_link (connections->pending_replies);
1686 while (link != NULL)
1688 pending = link->data;
1690 if (pending->reply_serial == reply_serial &&
1691 pending->will_get_reply == will_get_reply &&
1692 pending->will_send_reply == will_send_reply)
1694 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1695 "Message has the same reply serial as a currently-outstanding existing method call");
1699 link = bus_expire_list_get_next_link (connections->pending_replies,
1701 if (pending->will_get_reply == will_get_reply)
1706 bus_context_get_max_replies_per_connection (connections->context))
1708 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1709 "The maximum number of pending replies per connection has been reached");
1713 pending = dbus_new0 (BusPendingReply, 1);
1714 if (pending == NULL)
1716 BUS_SET_OOM (error);
1720 #ifdef DBUS_ENABLE_VERBOSE_MODE
1721 /* so we can see a not-yet-added pending reply */
1722 pending->expire_item.added_tv_sec = 1;
1723 pending->expire_item.added_tv_usec = 1;
1726 pending->will_get_reply = will_get_reply;
1727 pending->will_send_reply = will_send_reply;
1728 pending->reply_serial = reply_serial;
1730 cprd = dbus_new0 (CancelPendingReplyData, 1);
1733 BUS_SET_OOM (error);
1734 bus_pending_reply_free (pending);
1738 if (!bus_expire_list_add (connections->pending_replies,
1739 &pending->expire_item))
1741 BUS_SET_OOM (error);
1743 bus_pending_reply_free (pending);
1747 if (!bus_transaction_add_cancel_hook (transaction,
1748 cancel_pending_reply,
1750 cancel_pending_reply_data_free))
1752 BUS_SET_OOM (error);
1753 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1755 bus_pending_reply_free (pending);
1759 cprd->pending = pending;
1760 cprd->connections = connections;
1762 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1763 &pending->expire_item.added_tv_usec);
1765 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1767 pending->will_send_reply,
1768 pending->will_get_reply,
1769 pending->reply_serial);
1777 BusConnections *connections;
1778 } CheckPendingReplyData;
1781 cancel_check_pending_reply (void *data)
1783 CheckPendingReplyData *d = data;
1785 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1787 bus_expire_list_add_link (d->connections->pending_replies,
1793 check_pending_reply_data_free (void *data)
1795 CheckPendingReplyData *d = data;
1797 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1799 if (d->link != NULL)
1801 BusPendingReply *pending = d->link->data;
1803 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1804 &pending->expire_item));
1806 bus_pending_reply_free (pending);
1807 _dbus_list_free_link (d->link);
1814 * Check whether a reply is allowed, remove BusPendingReply
1815 * if so, return TRUE if so.
1818 bus_connections_check_reply (BusConnections *connections,
1819 BusTransaction *transaction,
1820 DBusConnection *sending_reply,
1821 DBusConnection *receiving_reply,
1825 CheckPendingReplyData *cprd;
1827 dbus_uint32_t reply_serial;
1829 _dbus_assert (sending_reply != NULL);
1830 _dbus_assert (receiving_reply != NULL);
1832 reply_serial = dbus_message_get_reply_serial (reply);
1834 link = bus_expire_list_get_first_link (connections->pending_replies);
1835 while (link != NULL)
1837 BusPendingReply *pending = link->data;
1839 if (pending->reply_serial == reply_serial &&
1840 pending->will_get_reply == receiving_reply &&
1841 pending->will_send_reply == sending_reply)
1843 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1847 link = bus_expire_list_get_next_link (connections->pending_replies,
1853 _dbus_verbose ("No pending reply expected\n");
1858 cprd = dbus_new0 (CheckPendingReplyData, 1);
1861 BUS_SET_OOM (error);
1865 if (!bus_transaction_add_cancel_hook (transaction,
1866 cancel_check_pending_reply,
1868 check_pending_reply_data_free))
1870 BUS_SET_OOM (error);
1876 cprd->connections = connections;
1878 bus_expire_list_unlink (connections->pending_replies,
1881 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1889 * Note that this is fairly fragile; in particular, don't try to use
1890 * one transaction across any main loop iterations.
1895 BusTransaction *transaction;
1896 DBusMessage *message;
1897 DBusPreallocatedSend *preallocated;
1902 BusTransactionCancelFunction cancel_function;
1903 DBusFreeFunction free_data_function;
1907 struct BusTransaction
1909 DBusList *connections;
1910 BusContext *context;
1911 DBusList *cancel_hooks;
1915 message_to_send_free (DBusConnection *connection,
1916 MessageToSend *to_send)
1918 if (to_send->message)
1919 dbus_message_unref (to_send->message);
1921 if (to_send->preallocated)
1922 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1924 dbus_free (to_send);
1928 cancel_hook_cancel (void *element,
1931 CancelHook *ch = element;
1933 _dbus_verbose ("Running transaction cancel hook\n");
1935 if (ch->cancel_function)
1936 (* ch->cancel_function) (ch->data);
1940 cancel_hook_free (void *element,
1943 CancelHook *ch = element;
1945 if (ch->free_data_function)
1946 (* ch->free_data_function) (ch->data);
1952 free_cancel_hooks (BusTransaction *transaction)
1954 _dbus_list_foreach (&transaction->cancel_hooks,
1955 cancel_hook_free, NULL);
1957 _dbus_list_clear (&transaction->cancel_hooks);
1961 bus_transaction_new (BusContext *context)
1963 BusTransaction *transaction;
1965 transaction = dbus_new0 (BusTransaction, 1);
1966 if (transaction == NULL)
1969 transaction->context = context;
1975 bus_transaction_get_context (BusTransaction *transaction)
1977 return transaction->context;
1981 bus_transaction_get_connections (BusTransaction *transaction)
1983 return bus_context_get_connections (transaction->context);
1987 bus_transaction_send_from_driver (BusTransaction *transaction,
1988 DBusConnection *connection,
1989 DBusMessage *message)
1991 /* We have to set the sender to the driver, and have
1992 * to check security policy since it was not done in
1995 _dbus_verbose ("Sending %s %s %s from driver\n",
1996 dbus_message_get_interface (message) ?
1997 dbus_message_get_interface (message) : "(no interface)",
1998 dbus_message_get_member (message) ?
1999 dbus_message_get_member (message) : "(no member)",
2000 dbus_message_get_error_name (message) ?
2001 dbus_message_get_error_name (message) : "(no error name)");
2003 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2006 if (bus_connection_is_active (connection))
2008 if (!dbus_message_set_destination (message,
2009 bus_connection_get_name (connection)))
2013 /* bus driver never wants a reply */
2014 dbus_message_set_no_reply (message, TRUE);
2016 /* If security policy doesn't allow the message, we silently
2017 * eat it; the driver doesn't care about getting a reply.
2019 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2021 NULL, connection, connection, message, NULL))
2024 return bus_transaction_send (transaction, connection, message);
2028 bus_transaction_send (BusTransaction *transaction,
2029 DBusConnection *connection,
2030 DBusMessage *message)
2032 MessageToSend *to_send;
2033 BusConnectionData *d;
2036 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2037 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2038 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2040 dbus_message_get_interface (message) ?
2041 dbus_message_get_interface (message) : "(unset)",
2042 dbus_message_get_member (message) ?
2043 dbus_message_get_member (message) : "(unset)",
2044 dbus_message_get_error_name (message) ?
2045 dbus_message_get_error_name (message) : "(unset)",
2046 dbus_connection_get_is_connected (connection) ?
2047 "" : " (disconnected)");
2049 _dbus_assert (dbus_message_get_sender (message) != NULL);
2051 if (!dbus_connection_get_is_connected (connection))
2052 return TRUE; /* silently ignore disconnected connections */
2054 d = BUS_CONNECTION_DATA (connection);
2055 _dbus_assert (d != NULL);
2057 to_send = dbus_new (MessageToSend, 1);
2058 if (to_send == NULL)
2063 to_send->preallocated = dbus_connection_preallocate_send (connection);
2064 if (to_send->preallocated == NULL)
2066 dbus_free (to_send);
2070 dbus_message_ref (message);
2071 to_send->message = message;
2072 to_send->transaction = transaction;
2074 _dbus_verbose ("about to prepend message\n");
2076 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2078 message_to_send_free (connection, to_send);
2082 _dbus_verbose ("prepended message\n");
2084 /* See if we already had this connection in the list
2085 * for this transaction. If we have a pending message,
2086 * then we should already be in transaction->connections
2088 link = _dbus_list_get_first_link (&d->transaction_messages);
2089 _dbus_assert (link->data == to_send);
2090 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2091 while (link != NULL)
2093 MessageToSend *m = link->data;
2094 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2096 if (m->transaction == transaction)
2104 if (!_dbus_list_prepend (&transaction->connections, connection))
2106 _dbus_list_remove (&d->transaction_messages, to_send);
2107 message_to_send_free (connection, to_send);
2116 connection_cancel_transaction (DBusConnection *connection,
2117 BusTransaction *transaction)
2120 BusConnectionData *d;
2122 d = BUS_CONNECTION_DATA (connection);
2123 _dbus_assert (d != NULL);
2125 link = _dbus_list_get_first_link (&d->transaction_messages);
2126 while (link != NULL)
2128 MessageToSend *m = link->data;
2129 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2131 if (m->transaction == transaction)
2133 _dbus_list_remove_link (&d->transaction_messages,
2136 message_to_send_free (connection, m);
2144 bus_transaction_cancel_and_free (BusTransaction *transaction)
2146 DBusConnection *connection;
2148 _dbus_verbose ("TRANSACTION: cancelled\n");
2150 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2151 connection_cancel_transaction (connection, transaction);
2153 _dbus_assert (transaction->connections == NULL);
2155 _dbus_list_foreach (&transaction->cancel_hooks,
2156 cancel_hook_cancel, NULL);
2158 free_cancel_hooks (transaction);
2160 dbus_free (transaction);
2164 connection_execute_transaction (DBusConnection *connection,
2165 BusTransaction *transaction)
2168 BusConnectionData *d;
2170 d = BUS_CONNECTION_DATA (connection);
2171 _dbus_assert (d != NULL);
2173 /* Send the queue in order (FIFO) */
2174 link = _dbus_list_get_last_link (&d->transaction_messages);
2175 while (link != NULL)
2177 MessageToSend *m = link->data;
2178 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2180 if (m->transaction == transaction)
2182 _dbus_list_remove_link (&d->transaction_messages,
2185 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2187 dbus_connection_send_preallocated (connection,
2192 m->preallocated = NULL; /* so we don't double-free it */
2194 message_to_send_free (connection, m);
2202 bus_transaction_execute_and_free (BusTransaction *transaction)
2204 /* For each connection in transaction->connections
2207 DBusConnection *connection;
2209 _dbus_verbose ("TRANSACTION: executing\n");
2211 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2212 connection_execute_transaction (connection, transaction);
2214 _dbus_assert (transaction->connections == NULL);
2216 free_cancel_hooks (transaction);
2218 dbus_free (transaction);
2222 bus_connection_remove_transactions (DBusConnection *connection)
2224 MessageToSend *to_send;
2225 BusConnectionData *d;
2227 d = BUS_CONNECTION_DATA (connection);
2228 _dbus_assert (d != NULL);
2230 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2232 /* only has an effect for the first MessageToSend listing this transaction */
2233 _dbus_list_remove (&to_send->transaction->connections,
2236 _dbus_list_remove (&d->transaction_messages, to_send);
2237 message_to_send_free (connection, to_send);
2242 * Converts the DBusError to a message reply
2245 bus_transaction_send_error_reply (BusTransaction *transaction,
2246 DBusConnection *connection,
2247 const DBusError *error,
2248 DBusMessage *in_reply_to)
2252 _dbus_assert (error != NULL);
2253 _DBUS_ASSERT_ERROR_IS_SET (error);
2255 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2256 error->name, error->message);
2258 reply = dbus_message_new_error (in_reply_to,
2264 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2266 dbus_message_unref (reply);
2270 dbus_message_unref (reply);
2276 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2277 BusTransactionCancelFunction cancel_function,
2279 DBusFreeFunction free_data_function)
2283 ch = dbus_new (CancelHook, 1);
2287 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2288 cancel_function, data);
2290 ch->cancel_function = cancel_function;
2292 ch->free_data_function = free_data_function;
2294 /* It's important that the hooks get run in reverse order that they
2297 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))