1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "connection.h"
31 #include "expirelist.h"
33 #include <dbus/dbus-list.h>
34 #include <dbus/dbus-hash.h>
35 #include <dbus/dbus-timeout.h>
37 /* Trim executed commands to this length; we want to keep logs readable */
38 #define MAX_LOG_COMMAND_LEN 50
40 static void bus_connection_remove_transactions (DBusConnection *connection);
44 BusExpireItem expire_item;
46 DBusConnection *will_get_reply;
47 DBusConnection *will_send_reply;
49 dbus_uint32_t reply_serial;
56 DBusList *completed; /**< List of all completed connections */
57 int n_completed; /**< Length of completed list */
58 DBusList *incomplete; /**< List of all not-yet-active connections */
59 int n_incomplete; /**< Length of incomplete list */
61 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
62 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
63 int stamp; /**< Incrementing number */
64 BusExpireList *pending_replies; /**< List of pending replies */
67 static dbus_int32_t connection_data_slot = -1;
71 BusConnections *connections;
72 DBusList *link_in_connection_list;
73 DBusConnection *connection;
74 DBusList *services_owned;
76 DBusList *match_rules;
79 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
80 DBusMessage *oom_message;
81 DBusPreallocatedSend *oom_preallocated;
82 BusClientPolicy *policy;
84 char *cached_loginfo_string;
85 BusSELinuxID *selinux_id;
87 long connection_tv_sec; /**< Time when we connected (seconds component) */
88 long connection_tv_usec; /**< Time when we connected (microsec component) */
89 int stamp; /**< connections->stamp last time we were traversed */
92 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
96 static void bus_connection_drop_pending_replies (BusConnections *connections,
97 DBusConnection *connection);
99 static dbus_bool_t expire_incomplete_timeout (void *data);
101 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
104 connection_get_loop (DBusConnection *connection)
106 BusConnectionData *d;
108 d = BUS_CONNECTION_DATA (connection);
110 return bus_context_get_loop (d->connections->context);
115 get_connections_for_uid (BusConnections *connections,
121 /* val is NULL is 0 when it isn't in the hash yet */
123 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
126 current_count = _DBUS_POINTER_TO_INT (val);
128 return current_count;
132 adjust_connections_for_uid (BusConnections *connections,
138 current_count = get_connections_for_uid (connections, uid);
140 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
141 ": was %d adjustment %d making %d\n",
142 uid, current_count, adjustment, current_count + adjustment);
144 _dbus_assert (current_count >= 0);
146 current_count += adjustment;
148 _dbus_assert (current_count >= 0);
150 if (current_count == 0)
152 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
159 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
160 uid, _DBUS_INT_TO_POINTER (current_count));
162 /* only positive adjustment can fail as otherwise
163 * a hash entry should already exist
165 _dbus_assert (adjustment > 0 ||
166 (adjustment <= 0 && retval));
173 bus_connection_disconnected (DBusConnection *connection)
175 BusConnectionData *d;
177 BusMatchmaker *matchmaker;
179 d = BUS_CONNECTION_DATA (connection);
180 _dbus_assert (d != NULL);
182 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
183 d->name ? d->name : "(inactive)");
185 /* Delete our match rules */
186 if (d->n_match_rules > 0)
188 matchmaker = bus_context_get_matchmaker (d->connections->context);
189 bus_matchmaker_disconnected (matchmaker, connection);
192 /* Drop any service ownership. Unfortunately, this requires
193 * memory allocation and there doesn't seem to be a good way to
194 * handle it other than sleeping; we can't "fail" the operation of
195 * disconnecting a client, and preallocating a broadcast "service is
196 * now gone" message for every client-service pair seems kind of
199 while ((service = _dbus_list_get_last (&d->services_owned)))
201 BusTransaction *transaction;
206 dbus_error_init (&error);
208 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
209 _dbus_wait_for_memory ();
211 if (!bus_service_remove_owner (service, connection,
212 transaction, &error))
214 _DBUS_ASSERT_ERROR_IS_SET (&error);
216 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
218 dbus_error_free (&error);
219 bus_transaction_cancel_and_free (transaction);
220 _dbus_wait_for_memory ();
225 _dbus_verbose ("Failed to remove service owner: %s %s\n",
226 error.name, error.message);
227 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
231 bus_transaction_execute_and_free (transaction);
234 bus_dispatch_remove_connection (connection);
236 /* no more watching */
237 if (!dbus_connection_set_watch_functions (connection,
241 _dbus_assert_not_reached ("setting watch functions to NULL failed");
243 if (!dbus_connection_set_timeout_functions (connection,
247 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
249 dbus_connection_set_unix_user_function (connection,
251 dbus_connection_set_windows_user_function (connection,
254 dbus_connection_set_dispatch_status_function (connection,
257 bus_connection_remove_transactions (connection);
259 if (d->link_in_connection_list != NULL)
265 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
266 d->link_in_connection_list = NULL;
267 d->connections->n_completed -= 1;
269 if (dbus_connection_get_unix_user (connection, &uid))
271 if (!adjust_connections_for_uid (d->connections,
273 _dbus_assert_not_reached ("adjusting downward should never fail");
278 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
279 d->link_in_connection_list = NULL;
280 d->connections->n_incomplete -= 1;
283 _dbus_assert (d->connections->n_incomplete >= 0);
284 _dbus_assert (d->connections->n_completed >= 0);
287 bus_connection_drop_pending_replies (d->connections, connection);
289 /* frees "d" as side effect */
290 dbus_connection_set_data (connection,
291 connection_data_slot,
294 dbus_connection_unref (connection);
298 add_connection_watch (DBusWatch *watch,
301 DBusConnection *connection = data;
303 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
307 remove_connection_watch (DBusWatch *watch,
310 DBusConnection *connection = data;
312 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
316 add_connection_timeout (DBusTimeout *timeout,
319 DBusConnection *connection = data;
321 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
325 remove_connection_timeout (DBusTimeout *timeout,
328 DBusConnection *connection = data;
330 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
334 dispatch_status_function (DBusConnection *connection,
335 DBusDispatchStatus new_status,
338 DBusLoop *loop = data;
340 if (new_status != DBUS_DISPATCH_COMPLETE)
342 while (!_dbus_loop_queue_dispatch (loop, connection))
343 _dbus_wait_for_memory ();
348 allow_unix_user_function (DBusConnection *connection,
352 BusConnectionData *d;
354 d = BUS_CONNECTION_DATA (connection);
356 _dbus_assert (d != NULL);
358 return bus_context_allow_unix_user (d->connections->context, uid);
362 free_connection_data (void *data)
364 BusConnectionData *d = data;
366 /* services_owned should be NULL since we should be disconnected */
367 _dbus_assert (d->services_owned == NULL);
368 _dbus_assert (d->n_services_owned == 0);
370 _dbus_assert (d->transaction_messages == NULL);
372 if (d->oom_preallocated)
373 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
376 dbus_message_unref (d->oom_message);
379 bus_client_policy_unref (d->policy);
382 bus_selinux_id_unref (d->selinux_id);
384 dbus_free (d->cached_loginfo_string);
392 call_timeout_callback (DBusTimeout *timeout,
395 /* can return FALSE on OOM but we just let it fire again later */
396 dbus_timeout_handle (timeout);
400 bus_connections_new (BusContext *context)
402 BusConnections *connections;
404 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
407 connections = dbus_new0 (BusConnections, 1);
408 if (connections == NULL)
411 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
413 if (connections->completed_by_user == NULL)
416 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
417 expire_incomplete_timeout,
419 if (connections->expire_timeout == NULL)
422 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
424 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
425 bus_context_get_reply_timeout (context),
426 bus_pending_reply_expired,
428 if (connections->pending_replies == NULL)
431 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
432 connections->expire_timeout))
435 connections->refcount = 1;
436 connections->context = context;
441 bus_expire_list_free (connections->pending_replies);
443 _dbus_timeout_unref (connections->expire_timeout);
445 _dbus_hash_table_unref (connections->completed_by_user);
447 dbus_free (connections);
449 dbus_connection_free_data_slot (&connection_data_slot);
455 bus_connections_ref (BusConnections *connections)
457 _dbus_assert (connections->refcount > 0);
458 connections->refcount += 1;
464 bus_connections_unref (BusConnections *connections)
466 _dbus_assert (connections->refcount > 0);
467 connections->refcount -= 1;
468 if (connections->refcount == 0)
470 /* drop all incomplete */
471 while (connections->incomplete != NULL)
473 DBusConnection *connection;
475 connection = connections->incomplete->data;
477 dbus_connection_ref (connection);
478 dbus_connection_close (connection);
479 bus_connection_disconnected (connection);
480 dbus_connection_unref (connection);
483 _dbus_assert (connections->n_incomplete == 0);
485 /* drop all real connections */
486 while (connections->completed != NULL)
488 DBusConnection *connection;
490 connection = connections->completed->data;
492 dbus_connection_ref (connection);
493 dbus_connection_close (connection);
494 bus_connection_disconnected (connection);
495 dbus_connection_unref (connection);
498 _dbus_assert (connections->n_completed == 0);
500 bus_expire_list_free (connections->pending_replies);
502 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
503 connections->expire_timeout);
505 _dbus_timeout_unref (connections->expire_timeout);
507 _dbus_hash_table_unref (connections->completed_by_user);
509 dbus_free (connections);
511 dbus_connection_free_data_slot (&connection_data_slot);
515 /* Used for logging */
517 cache_peer_loginfo_string (BusConnectionData *d,
518 DBusConnection *connection)
520 DBusString loginfo_buf;
524 dbus_bool_t prev_added;
526 if (!_dbus_string_init (&loginfo_buf))
530 if (dbus_connection_get_unix_user (connection, &uid))
532 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
538 if (dbus_connection_get_unix_process_id (connection, &pid))
542 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
545 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
547 /* Ignore errors here; we may not have permissions to read the
549 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
550 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
554 if (dbus_connection_get_windows_user (connection, &windows_sid))
556 dbus_bool_t did_append;
557 did_append = _dbus_string_append_printf (&loginfo_buf,
558 "sid=\"%s\" ", windows_sid);
559 dbus_free (windows_sid);
564 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
567 _dbus_string_free (&loginfo_buf);
571 _dbus_string_free (&loginfo_buf);
576 bus_connections_setup_connection (BusConnections *connections,
577 DBusConnection *connection)
580 BusConnectionData *d;
585 d = dbus_new0 (BusConnectionData, 1);
590 d->connections = connections;
591 d->connection = connection;
593 _dbus_get_current_time (&d->connection_tv_sec,
594 &d->connection_tv_usec);
596 _dbus_assert (connection_data_slot >= 0);
598 if (!dbus_connection_set_data (connection,
599 connection_data_slot,
600 d, free_connection_data))
606 dbus_connection_set_route_peer_messages (connection, TRUE);
610 dbus_error_init (&error);
611 d->selinux_id = bus_selinux_init_connection_id (connection,
613 if (dbus_error_is_set (&error))
615 /* This is a bit bogus because we pretend all errors
616 * are OOM; this is done because we know that in bus.c
617 * an OOM error disconnects the connection, which is
618 * the same thing we want on any other error.
620 dbus_error_free (&error);
624 if (!dbus_connection_set_watch_functions (connection,
625 add_connection_watch,
626 remove_connection_watch,
632 if (!dbus_connection_set_timeout_functions (connection,
633 add_connection_timeout,
634 remove_connection_timeout,
639 /* For now we don't need to set a Windows user function because
640 * there are no policies in the config file controlling what
641 * Windows users can connect. The default 'same user that owns the
642 * bus can connect' behavior of DBusConnection is fine on Windows.
644 dbus_connection_set_unix_user_function (connection,
645 allow_unix_user_function,
648 dbus_connection_set_dispatch_status_function (connection,
649 dispatch_status_function,
650 bus_context_get_loop (connections->context),
653 d->link_in_connection_list = _dbus_list_alloc_link (connection);
654 if (d->link_in_connection_list == NULL)
657 /* Setup the connection with the dispatcher */
658 if (!bus_dispatch_add_connection (connection))
661 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
663 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
665 bus_dispatch_remove_connection (connection);
670 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
671 connections->n_incomplete += 1;
673 dbus_connection_ref (connection);
675 /* Note that we might disconnect ourselves here, but it only takes
676 * effect on return to the main loop. We call this to free up
677 * expired connections if possible, and to queue the timeout for our
680 bus_connections_expire_incomplete (connections);
682 /* And we might also disconnect ourselves here, but again it
683 * only takes effect on return to main loop.
685 if (connections->n_incomplete >
686 bus_context_get_max_incomplete_connections (connections->context))
688 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
690 _dbus_assert (connections->incomplete != NULL);
691 /* Disconnect the oldest unauthenticated connection. FIXME
692 * would it be more secure to drop a *random* connection? This
693 * algorithm seems to mean that if someone can create new
694 * connections quickly enough, they can keep anyone else from
695 * completing authentication. But random may or may not really
696 * help with that, a more elaborate solution might be required.
698 dbus_connection_close (connections->incomplete->data);
707 bus_selinux_id_unref (d->selinux_id);
708 d->selinux_id = NULL;
710 if (!dbus_connection_set_watch_functions (connection,
714 _dbus_assert_not_reached ("setting watch functions to NULL failed");
716 if (!dbus_connection_set_timeout_functions (connection,
720 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
722 dbus_connection_set_unix_user_function (connection,
725 dbus_connection_set_windows_user_function (connection,
728 dbus_connection_set_dispatch_status_function (connection,
731 if (d->link_in_connection_list != NULL)
733 _dbus_assert (d->link_in_connection_list->next == NULL);
734 _dbus_assert (d->link_in_connection_list->prev == NULL);
735 _dbus_list_free_link (d->link_in_connection_list);
736 d->link_in_connection_list = NULL;
739 if (!dbus_connection_set_data (connection,
740 connection_data_slot,
742 _dbus_assert_not_reached ("failed to set connection data to null");
744 /* "d" has now been freed */
751 bus_connections_expire_incomplete (BusConnections *connections)
757 if (connections->incomplete != NULL)
759 long tv_sec, tv_usec;
763 _dbus_get_current_time (&tv_sec, &tv_usec);
764 auth_timeout = bus_context_get_auth_timeout (connections->context);
766 link = _dbus_list_get_first_link (&connections->incomplete);
769 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
770 DBusConnection *connection;
771 BusConnectionData *d;
774 connection = link->data;
776 d = BUS_CONNECTION_DATA (connection);
778 _dbus_assert (d != NULL);
780 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
781 d->connection_tv_usec,
784 if (elapsed >= (double) auth_timeout)
786 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
787 dbus_connection_close (connection);
791 /* We can end the loop, since the connections are in oldest-first order */
792 next_interval = ((double)auth_timeout) - elapsed;
793 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
794 connection, next_interval);
803 bus_expire_timeout_set_interval (connections->expire_timeout,
808 expire_incomplete_timeout (void *data)
810 BusConnections *connections = data;
812 _dbus_verbose ("Running\n");
814 /* note that this may remove the timeout */
815 bus_connections_expire_incomplete (connections);
821 bus_connection_get_unix_groups (DBusConnection *connection,
822 unsigned long **groups,
826 BusConnectionData *d;
829 d = BUS_CONNECTION_DATA (connection);
831 _dbus_assert (d != NULL);
836 if (dbus_connection_get_unix_user (connection, &uid))
838 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
840 _dbus_verbose ("Did not get any groups for UID %lu\n",
846 _dbus_verbose ("Got %d groups for UID %lu\n",
852 return TRUE; /* successfully got 0 groups */
856 bus_connection_is_in_unix_group (DBusConnection *connection,
860 unsigned long *group_ids;
863 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
868 while (i < n_group_ids)
870 if (group_ids[i] == gid)
872 dbus_free (group_ids);
878 dbus_free (group_ids);
883 bus_connection_get_loginfo (DBusConnection *connection)
885 BusConnectionData *d;
887 d = BUS_CONNECTION_DATA (connection);
889 if (!bus_connection_is_active (connection))
891 return d->cached_loginfo_string;
895 bus_connection_get_policy (DBusConnection *connection)
897 BusConnectionData *d;
899 d = BUS_CONNECTION_DATA (connection);
901 _dbus_assert (d != NULL);
902 _dbus_assert (d->policy != NULL);
908 foreach_active (BusConnections *connections,
909 BusConnectionForeachFunction function,
914 link = _dbus_list_get_first_link (&connections->completed);
917 DBusConnection *connection = link->data;
918 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
920 if (!(* function) (connection, data))
930 foreach_inactive (BusConnections *connections,
931 BusConnectionForeachFunction function,
936 link = _dbus_list_get_first_link (&connections->incomplete);
939 DBusConnection *connection = link->data;
940 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
942 if (!(* function) (connection, data))
952 * Calls function on each active connection; if the function returns
953 * #FALSE, stops iterating. Active connections are authenticated
954 * and have sent a Hello message.
956 * @param connections the connections object
957 * @param function the function
958 * @param data data to pass to it as a second arg
961 bus_connections_foreach_active (BusConnections *connections,
962 BusConnectionForeachFunction function,
965 foreach_active (connections, function, data);
969 * Calls function on each connection; if the function returns
970 * #FALSE, stops iterating.
972 * @param connections the connections object
973 * @param function the function
974 * @param data data to pass to it as a second arg
977 bus_connections_foreach (BusConnections *connections,
978 BusConnectionForeachFunction function,
981 if (!foreach_active (connections, function, data))
984 foreach_inactive (connections, function, data);
988 bus_connections_get_context (BusConnections *connections)
990 return connections->context;
994 * This is used to avoid covering the same connection twice when
995 * traversing connections. Note that it assumes we will
996 * bus_connection_mark_stamp() each connection at least once per
997 * INT_MAX increments of the global stamp, or wraparound would break
1001 bus_connections_increment_stamp (BusConnections *connections)
1003 connections->stamp += 1;
1006 /* Mark connection with current stamp, return TRUE if it
1007 * didn't already have that stamp
1010 bus_connection_mark_stamp (DBusConnection *connection)
1012 BusConnectionData *d;
1014 d = BUS_CONNECTION_DATA (connection);
1016 _dbus_assert (d != NULL);
1018 if (d->stamp == d->connections->stamp)
1022 d->stamp = d->connections->stamp;
1028 bus_connection_get_context (DBusConnection *connection)
1030 BusConnectionData *d;
1032 d = BUS_CONNECTION_DATA (connection);
1034 _dbus_assert (d != NULL);
1036 return d->connections->context;
1040 bus_connection_get_connections (DBusConnection *connection)
1042 BusConnectionData *d;
1044 d = BUS_CONNECTION_DATA (connection);
1046 _dbus_assert (d != NULL);
1048 return d->connections;
1052 bus_connection_get_registry (DBusConnection *connection)
1054 BusConnectionData *d;
1056 d = BUS_CONNECTION_DATA (connection);
1058 _dbus_assert (d != NULL);
1060 return bus_context_get_registry (d->connections->context);
1064 bus_connection_get_activation (DBusConnection *connection)
1066 BusConnectionData *d;
1068 d = BUS_CONNECTION_DATA (connection);
1070 _dbus_assert (d != NULL);
1072 return bus_context_get_activation (d->connections->context);
1076 bus_connection_get_matchmaker (DBusConnection *connection)
1078 BusConnectionData *d;
1080 d = BUS_CONNECTION_DATA (connection);
1082 _dbus_assert (d != NULL);
1084 return bus_context_get_matchmaker (d->connections->context);
1088 bus_connection_get_selinux_id (DBusConnection *connection)
1090 BusConnectionData *d;
1092 d = BUS_CONNECTION_DATA (connection);
1094 _dbus_assert (d != NULL);
1096 return d->selinux_id;
1100 * Checks whether the connection is registered with the message bus.
1102 * @param connection the connection
1103 * @returns #TRUE if we're an active message bus participant
1106 bus_connection_is_active (DBusConnection *connection)
1108 BusConnectionData *d;
1110 d = BUS_CONNECTION_DATA (connection);
1112 return d != NULL && d->name != NULL;
1116 bus_connection_preallocate_oom_error (DBusConnection *connection)
1118 DBusMessage *message;
1119 DBusPreallocatedSend *preallocated;
1120 BusConnectionData *d;
1122 d = BUS_CONNECTION_DATA (connection);
1124 _dbus_assert (d != NULL);
1126 if (d->oom_preallocated != NULL)
1129 preallocated = dbus_connection_preallocate_send (connection);
1130 if (preallocated == NULL)
1133 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1135 if (message == NULL)
1137 dbus_connection_free_preallocated_send (connection, preallocated);
1141 /* d->name may be NULL, but that is OK */
1142 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1143 !dbus_message_set_destination (message, d->name) ||
1144 !dbus_message_set_sender (message,
1147 dbus_connection_free_preallocated_send (connection, preallocated);
1148 dbus_message_unref (message);
1152 /* set reply serial to placeholder value just so space is already allocated
1155 if (!dbus_message_set_reply_serial (message, 14))
1157 dbus_connection_free_preallocated_send (connection, preallocated);
1158 dbus_message_unref (message);
1162 d->oom_message = message;
1163 d->oom_preallocated = preallocated;
1169 bus_connection_send_oom_error (DBusConnection *connection,
1170 DBusMessage *in_reply_to)
1172 BusConnectionData *d;
1174 d = BUS_CONNECTION_DATA (connection);
1176 _dbus_assert (d != NULL);
1177 _dbus_assert (d->oom_message != NULL);
1179 /* should always succeed since we set it to a placeholder earlier */
1180 if (!dbus_message_set_reply_serial (d->oom_message,
1181 dbus_message_get_serial (in_reply_to)))
1182 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1184 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1186 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1187 d->oom_message, NULL);
1189 dbus_message_unref (d->oom_message);
1190 d->oom_message = NULL;
1191 d->oom_preallocated = NULL;
1195 bus_connection_add_match_rule_link (DBusConnection *connection,
1198 BusConnectionData *d;
1200 d = BUS_CONNECTION_DATA (connection);
1201 _dbus_assert (d != NULL);
1203 _dbus_list_append_link (&d->match_rules, link);
1205 d->n_match_rules += 1;
1209 bus_connection_add_match_rule (DBusConnection *connection,
1214 link = _dbus_list_alloc_link (rule);
1219 bus_connection_add_match_rule_link (connection, link);
1225 bus_connection_remove_match_rule (DBusConnection *connection,
1228 BusConnectionData *d;
1230 d = BUS_CONNECTION_DATA (connection);
1231 _dbus_assert (d != NULL);
1233 _dbus_list_remove_last (&d->match_rules, rule);
1235 d->n_match_rules -= 1;
1236 _dbus_assert (d->n_match_rules >= 0);
1240 bus_connection_get_n_match_rules (DBusConnection *connection)
1242 BusConnectionData *d;
1244 d = BUS_CONNECTION_DATA (connection);
1245 _dbus_assert (d != NULL);
1247 return d->n_match_rules;
1251 bus_connection_add_owned_service_link (DBusConnection *connection,
1254 BusConnectionData *d;
1256 d = BUS_CONNECTION_DATA (connection);
1257 _dbus_assert (d != NULL);
1259 _dbus_list_append_link (&d->services_owned, link);
1261 d->n_services_owned += 1;
1265 bus_connection_add_owned_service (DBusConnection *connection,
1266 BusService *service)
1270 link = _dbus_list_alloc_link (service);
1275 bus_connection_add_owned_service_link (connection, link);
1281 bus_connection_remove_owned_service (DBusConnection *connection,
1282 BusService *service)
1284 BusConnectionData *d;
1286 d = BUS_CONNECTION_DATA (connection);
1287 _dbus_assert (d != NULL);
1289 _dbus_list_remove_last (&d->services_owned, service);
1291 d->n_services_owned -= 1;
1292 _dbus_assert (d->n_services_owned >= 0);
1296 bus_connection_get_n_services_owned (DBusConnection *connection)
1298 BusConnectionData *d;
1300 d = BUS_CONNECTION_DATA (connection);
1301 _dbus_assert (d != NULL);
1303 return d->n_services_owned;
1307 bus_connection_complete (DBusConnection *connection,
1308 const DBusString *name,
1311 BusConnectionData *d;
1314 d = BUS_CONNECTION_DATA (connection);
1315 _dbus_assert (d != NULL);
1316 _dbus_assert (d->name == NULL);
1317 _dbus_assert (d->policy == NULL);
1319 _dbus_assert (!bus_connection_is_active (connection));
1321 if (!_dbus_string_copy_data (name, &d->name))
1323 BUS_SET_OOM (error);
1327 _dbus_assert (d->name != NULL);
1329 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1331 d->policy = bus_context_create_client_policy (d->connections->context,
1335 /* we may have a NULL policy on OOM or error getting list of
1336 * groups for a user. In the latter case we don't handle it so
1337 * well currently, as it will just keep failing over and over.
1340 if (d->policy == NULL)
1342 _dbus_verbose ("Failed to create security policy for connection %p\n",
1344 _DBUS_ASSERT_ERROR_IS_SET (error);
1345 dbus_free (d->name);
1350 if (dbus_connection_get_unix_user (connection, &uid))
1352 if (!adjust_connections_for_uid (d->connections,
1357 /* Create and cache a string which holds information about the
1358 * peer process; used for logging purposes.
1360 if (!cache_peer_loginfo_string (d, connection))
1363 /* Now the connection is active, move it between lists */
1364 _dbus_list_unlink (&d->connections->incomplete,
1365 d->link_in_connection_list);
1366 d->connections->n_incomplete -= 1;
1367 _dbus_list_append_link (&d->connections->completed,
1368 d->link_in_connection_list);
1369 d->connections->n_completed += 1;
1371 _dbus_assert (d->connections->n_incomplete >= 0);
1372 _dbus_assert (d->connections->n_completed > 0);
1374 /* See if we can remove the timeout */
1375 bus_connections_expire_incomplete (d->connections);
1377 _dbus_assert (bus_connection_is_active (connection));
1381 BUS_SET_OOM (error);
1382 dbus_free (d->name);
1385 bus_client_policy_unref (d->policy);
1391 bus_connection_get_name (DBusConnection *connection)
1393 BusConnectionData *d;
1395 d = BUS_CONNECTION_DATA (connection);
1396 _dbus_assert (d != NULL);
1402 * Check whether completing the passed-in connection would
1403 * exceed limits, and if so set error and return #FALSE
1406 bus_connections_check_limits (BusConnections *connections,
1407 DBusConnection *requesting_completion,
1410 BusConnectionData *d;
1413 d = BUS_CONNECTION_DATA (requesting_completion);
1414 _dbus_assert (d != NULL);
1416 _dbus_assert (d->name == NULL);
1418 if (connections->n_completed >=
1419 bus_context_get_max_completed_connections (connections->context))
1421 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1422 "The maximum number of active connections has been reached");
1426 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1428 if (get_connections_for_uid (connections, uid) >=
1429 bus_context_get_max_connections_per_user (connections->context))
1431 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1432 "The maximum number of active connections for UID %lu has been reached",
1442 bus_pending_reply_free (BusPendingReply *pending)
1444 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1446 pending->will_send_reply,
1447 pending->will_get_reply,
1448 pending->reply_serial);
1450 dbus_free (pending);
1454 bus_pending_reply_send_no_reply (BusConnections *connections,
1455 BusTransaction *transaction,
1456 BusPendingReply *pending)
1458 DBusMessage *message;
1459 DBusMessageIter iter;
1465 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1466 if (message == NULL)
1469 dbus_message_set_no_reply (message, TRUE);
1471 if (!dbus_message_set_reply_serial (message,
1472 pending->reply_serial))
1475 if (!dbus_message_set_error_name (message,
1476 DBUS_ERROR_NO_REPLY))
1479 errmsg = "Message did not receive a reply (timeout by message bus)";
1480 dbus_message_iter_init_append (message, &iter);
1481 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1484 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1491 dbus_message_unref (message);
1496 bus_pending_reply_expired (BusExpireList *list,
1500 BusPendingReply *pending = link->data;
1501 BusConnections *connections = data;
1502 BusTransaction *transaction;
1504 /* No reply is forthcoming. So nuke it if we can. If not,
1505 * leave it in the list to try expiring again later when we
1509 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1511 pending->will_send_reply,
1512 pending->will_get_reply,
1513 pending->reply_serial);
1515 transaction = bus_transaction_new (connections->context);
1516 if (transaction == NULL)
1519 if (!bus_pending_reply_send_no_reply (connections,
1523 bus_transaction_cancel_and_free (transaction);
1527 bus_expire_list_remove_link (connections->pending_replies, link);
1529 bus_pending_reply_free (pending);
1530 bus_transaction_execute_and_free (transaction);
1536 bus_connection_drop_pending_replies (BusConnections *connections,
1537 DBusConnection *connection)
1539 /* The DBusConnection is almost 100% finalized here, so you can't
1540 * do anything with it except check for pointer equality
1544 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1547 link = bus_expire_list_get_first_link (connections->pending_replies);
1548 while (link != NULL)
1551 BusPendingReply *pending;
1553 next = bus_expire_list_get_next_link (connections->pending_replies,
1555 pending = link->data;
1557 if (pending->will_get_reply == connection)
1559 /* We don't need to track this pending reply anymore */
1561 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1563 pending->will_send_reply,
1564 pending->will_get_reply,
1565 pending->reply_serial);
1567 bus_expire_list_remove_link (connections->pending_replies,
1569 bus_pending_reply_free (pending);
1571 else if (pending->will_send_reply == connection)
1573 /* The reply isn't going to be sent, so set things
1574 * up so it will be expired right away
1576 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1578 pending->will_send_reply,
1579 pending->will_get_reply,
1580 pending->reply_serial);
1582 pending->will_send_reply = NULL;
1583 pending->expire_item.added_tv_sec = 0;
1584 pending->expire_item.added_tv_usec = 0;
1586 bus_expire_list_recheck_immediately (connections->pending_replies);
1596 BusPendingReply *pending;
1597 BusConnections *connections;
1598 } CancelPendingReplyData;
1601 cancel_pending_reply (void *data)
1603 CancelPendingReplyData *d = data;
1605 _dbus_verbose ("d = %p\n", d);
1607 if (!bus_expire_list_remove (d->connections->pending_replies,
1608 &d->pending->expire_item))
1609 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1611 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1615 cancel_pending_reply_data_free (void *data)
1617 CancelPendingReplyData *d = data;
1619 _dbus_verbose ("d = %p\n", d);
1621 /* d->pending should be either freed or still
1622 * in the list of pending replies (owned by someone
1630 * Record that a reply is allowed; return TRUE on success.
1633 bus_connections_expect_reply (BusConnections *connections,
1634 BusTransaction *transaction,
1635 DBusConnection *will_get_reply,
1636 DBusConnection *will_send_reply,
1637 DBusMessage *reply_to_this,
1640 BusPendingReply *pending;
1641 dbus_uint32_t reply_serial;
1643 CancelPendingReplyData *cprd;
1646 _dbus_assert (will_get_reply != NULL);
1647 _dbus_assert (will_send_reply != NULL);
1648 _dbus_assert (reply_to_this != NULL);
1650 if (dbus_message_get_no_reply (reply_to_this))
1651 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1653 reply_serial = dbus_message_get_serial (reply_to_this);
1655 link = bus_expire_list_get_first_link (connections->pending_replies);
1657 while (link != NULL)
1659 pending = link->data;
1661 if (pending->reply_serial == reply_serial &&
1662 pending->will_get_reply == will_get_reply &&
1663 pending->will_send_reply == will_send_reply)
1665 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1666 "Message has the same reply serial as a currently-outstanding existing method call");
1670 link = bus_expire_list_get_next_link (connections->pending_replies,
1672 if (pending->will_get_reply == will_get_reply)
1677 bus_context_get_max_replies_per_connection (connections->context))
1679 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1680 "The maximum number of pending replies per connection has been reached");
1684 pending = dbus_new0 (BusPendingReply, 1);
1685 if (pending == NULL)
1687 BUS_SET_OOM (error);
1691 #ifdef DBUS_ENABLE_VERBOSE_MODE
1692 /* so we can see a not-yet-added pending reply */
1693 pending->expire_item.added_tv_sec = 1;
1694 pending->expire_item.added_tv_usec = 1;
1697 pending->will_get_reply = will_get_reply;
1698 pending->will_send_reply = will_send_reply;
1699 pending->reply_serial = reply_serial;
1701 cprd = dbus_new0 (CancelPendingReplyData, 1);
1704 BUS_SET_OOM (error);
1705 bus_pending_reply_free (pending);
1709 if (!bus_expire_list_add (connections->pending_replies,
1710 &pending->expire_item))
1712 BUS_SET_OOM (error);
1714 bus_pending_reply_free (pending);
1718 if (!bus_transaction_add_cancel_hook (transaction,
1719 cancel_pending_reply,
1721 cancel_pending_reply_data_free))
1723 BUS_SET_OOM (error);
1724 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1726 bus_pending_reply_free (pending);
1730 cprd->pending = pending;
1731 cprd->connections = connections;
1733 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1734 &pending->expire_item.added_tv_usec);
1736 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1738 pending->will_send_reply,
1739 pending->will_get_reply,
1740 pending->reply_serial);
1748 BusConnections *connections;
1749 } CheckPendingReplyData;
1752 cancel_check_pending_reply (void *data)
1754 CheckPendingReplyData *d = data;
1756 _dbus_verbose ("d = %p\n",d);
1758 bus_expire_list_add_link (d->connections->pending_replies,
1764 check_pending_reply_data_free (void *data)
1766 CheckPendingReplyData *d = data;
1768 _dbus_verbose ("d = %p\n",d);
1770 if (d->link != NULL)
1772 BusPendingReply *pending = d->link->data;
1774 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1775 &pending->expire_item));
1777 bus_pending_reply_free (pending);
1778 _dbus_list_free_link (d->link);
1785 * Check whether a reply is allowed, remove BusPendingReply
1786 * if so, return TRUE if so.
1789 bus_connections_check_reply (BusConnections *connections,
1790 BusTransaction *transaction,
1791 DBusConnection *sending_reply,
1792 DBusConnection *receiving_reply,
1796 CheckPendingReplyData *cprd;
1798 dbus_uint32_t reply_serial;
1800 _dbus_assert (sending_reply != NULL);
1801 _dbus_assert (receiving_reply != NULL);
1803 reply_serial = dbus_message_get_reply_serial (reply);
1805 link = bus_expire_list_get_first_link (connections->pending_replies);
1806 while (link != NULL)
1808 BusPendingReply *pending = link->data;
1810 if (pending->reply_serial == reply_serial &&
1811 pending->will_get_reply == receiving_reply &&
1812 pending->will_send_reply == sending_reply)
1814 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1818 link = bus_expire_list_get_next_link (connections->pending_replies,
1824 _dbus_verbose ("No pending reply expected\n");
1829 cprd = dbus_new0 (CheckPendingReplyData, 1);
1832 BUS_SET_OOM (error);
1836 if (!bus_transaction_add_cancel_hook (transaction,
1837 cancel_check_pending_reply,
1839 check_pending_reply_data_free))
1841 BUS_SET_OOM (error);
1847 cprd->connections = connections;
1849 bus_expire_list_unlink (connections->pending_replies,
1852 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1860 * Note that this is fairly fragile; in particular, don't try to use
1861 * one transaction across any main loop iterations.
1866 BusTransaction *transaction;
1867 DBusMessage *message;
1868 DBusPreallocatedSend *preallocated;
1873 BusTransactionCancelFunction cancel_function;
1874 DBusFreeFunction free_data_function;
1878 struct BusTransaction
1880 DBusList *connections;
1881 BusContext *context;
1882 DBusList *cancel_hooks;
1886 message_to_send_free (DBusConnection *connection,
1887 MessageToSend *to_send)
1889 if (to_send->message)
1890 dbus_message_unref (to_send->message);
1892 if (to_send->preallocated)
1893 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1895 dbus_free (to_send);
1899 cancel_hook_cancel (void *element,
1902 CancelHook *ch = element;
1904 _dbus_verbose ("Running transaction cancel hook\n");
1906 if (ch->cancel_function)
1907 (* ch->cancel_function) (ch->data);
1911 cancel_hook_free (void *element,
1914 CancelHook *ch = element;
1916 if (ch->free_data_function)
1917 (* ch->free_data_function) (ch->data);
1923 free_cancel_hooks (BusTransaction *transaction)
1925 _dbus_list_foreach (&transaction->cancel_hooks,
1926 cancel_hook_free, NULL);
1928 _dbus_list_clear (&transaction->cancel_hooks);
1932 bus_transaction_new (BusContext *context)
1934 BusTransaction *transaction;
1936 transaction = dbus_new0 (BusTransaction, 1);
1937 if (transaction == NULL)
1940 transaction->context = context;
1946 bus_transaction_get_context (BusTransaction *transaction)
1948 return transaction->context;
1952 bus_transaction_get_connections (BusTransaction *transaction)
1954 return bus_context_get_connections (transaction->context);
1958 bus_transaction_send_from_driver (BusTransaction *transaction,
1959 DBusConnection *connection,
1960 DBusMessage *message)
1962 /* We have to set the sender to the driver, and have
1963 * to check security policy since it was not done in
1966 _dbus_verbose ("Sending %s %s %s from driver\n",
1967 dbus_message_get_interface (message) ?
1968 dbus_message_get_interface (message) : "(no interface)",
1969 dbus_message_get_member (message) ?
1970 dbus_message_get_member (message) : "(no member)",
1971 dbus_message_get_error_name (message) ?
1972 dbus_message_get_error_name (message) : "(no error name)");
1974 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
1977 if (bus_connection_is_active (connection))
1979 if (!dbus_message_set_destination (message,
1980 bus_connection_get_name (connection)))
1984 /* bus driver never wants a reply */
1985 dbus_message_set_no_reply (message, TRUE);
1987 /* If security policy doesn't allow the message, we silently
1988 * eat it; the driver doesn't care about getting a reply.
1990 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
1992 NULL, connection, connection, message, NULL))
1995 return bus_transaction_send (transaction, connection, message);
1999 bus_transaction_send (BusTransaction *transaction,
2000 DBusConnection *connection,
2001 DBusMessage *message)
2003 MessageToSend *to_send;
2004 BusConnectionData *d;
2007 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2008 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2009 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2011 dbus_message_get_interface (message) ?
2012 dbus_message_get_interface (message) : "(unset)",
2013 dbus_message_get_member (message) ?
2014 dbus_message_get_member (message) : "(unset)",
2015 dbus_message_get_error_name (message) ?
2016 dbus_message_get_error_name (message) : "(unset)",
2017 dbus_connection_get_is_connected (connection) ?
2018 "" : " (disconnected)");
2020 _dbus_assert (dbus_message_get_sender (message) != NULL);
2022 if (!dbus_connection_get_is_connected (connection))
2023 return TRUE; /* silently ignore disconnected connections */
2025 d = BUS_CONNECTION_DATA (connection);
2026 _dbus_assert (d != NULL);
2028 to_send = dbus_new (MessageToSend, 1);
2029 if (to_send == NULL)
2034 to_send->preallocated = dbus_connection_preallocate_send (connection);
2035 if (to_send->preallocated == NULL)
2037 dbus_free (to_send);
2041 dbus_message_ref (message);
2042 to_send->message = message;
2043 to_send->transaction = transaction;
2045 _dbus_verbose ("about to prepend message\n");
2047 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2049 message_to_send_free (connection, to_send);
2053 _dbus_verbose ("prepended message\n");
2055 /* See if we already had this connection in the list
2056 * for this transaction. If we have a pending message,
2057 * then we should already be in transaction->connections
2059 link = _dbus_list_get_first_link (&d->transaction_messages);
2060 _dbus_assert (link->data == to_send);
2061 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2062 while (link != NULL)
2064 MessageToSend *m = link->data;
2065 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2067 if (m->transaction == transaction)
2075 if (!_dbus_list_prepend (&transaction->connections, connection))
2077 _dbus_list_remove (&d->transaction_messages, to_send);
2078 message_to_send_free (connection, to_send);
2087 connection_cancel_transaction (DBusConnection *connection,
2088 BusTransaction *transaction)
2091 BusConnectionData *d;
2093 d = BUS_CONNECTION_DATA (connection);
2094 _dbus_assert (d != NULL);
2096 link = _dbus_list_get_first_link (&d->transaction_messages);
2097 while (link != NULL)
2099 MessageToSend *m = link->data;
2100 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2102 if (m->transaction == transaction)
2104 _dbus_list_remove_link (&d->transaction_messages,
2107 message_to_send_free (connection, m);
2115 bus_transaction_cancel_and_free (BusTransaction *transaction)
2117 DBusConnection *connection;
2119 _dbus_verbose ("TRANSACTION: cancelled\n");
2121 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2122 connection_cancel_transaction (connection, transaction);
2124 _dbus_assert (transaction->connections == NULL);
2126 _dbus_list_foreach (&transaction->cancel_hooks,
2127 cancel_hook_cancel, NULL);
2129 free_cancel_hooks (transaction);
2131 dbus_free (transaction);
2135 connection_execute_transaction (DBusConnection *connection,
2136 BusTransaction *transaction)
2139 BusConnectionData *d;
2141 d = BUS_CONNECTION_DATA (connection);
2142 _dbus_assert (d != NULL);
2144 /* Send the queue in order (FIFO) */
2145 link = _dbus_list_get_last_link (&d->transaction_messages);
2146 while (link != NULL)
2148 MessageToSend *m = link->data;
2149 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2151 if (m->transaction == transaction)
2153 _dbus_list_remove_link (&d->transaction_messages,
2156 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2158 dbus_connection_send_preallocated (connection,
2163 m->preallocated = NULL; /* so we don't double-free it */
2165 message_to_send_free (connection, m);
2173 bus_transaction_execute_and_free (BusTransaction *transaction)
2175 /* For each connection in transaction->connections
2178 DBusConnection *connection;
2180 _dbus_verbose ("TRANSACTION: executing\n");
2182 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2183 connection_execute_transaction (connection, transaction);
2185 _dbus_assert (transaction->connections == NULL);
2187 free_cancel_hooks (transaction);
2189 dbus_free (transaction);
2193 bus_connection_remove_transactions (DBusConnection *connection)
2195 MessageToSend *to_send;
2196 BusConnectionData *d;
2198 d = BUS_CONNECTION_DATA (connection);
2199 _dbus_assert (d != NULL);
2201 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2203 /* only has an effect for the first MessageToSend listing this transaction */
2204 _dbus_list_remove (&to_send->transaction->connections,
2207 _dbus_list_remove (&d->transaction_messages, to_send);
2208 message_to_send_free (connection, to_send);
2213 * Converts the DBusError to a message reply
2216 bus_transaction_send_error_reply (BusTransaction *transaction,
2217 DBusConnection *connection,
2218 const DBusError *error,
2219 DBusMessage *in_reply_to)
2223 _dbus_assert (error != NULL);
2224 _DBUS_ASSERT_ERROR_IS_SET (error);
2226 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2227 error->name, error->message);
2229 reply = dbus_message_new_error (in_reply_to,
2235 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2237 dbus_message_unref (reply);
2241 dbus_message_unref (reply);
2247 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2248 BusTransactionCancelFunction cancel_function,
2250 DBusFreeFunction free_data_function)
2254 ch = dbus_new (CancelHook, 1);
2258 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2259 cancel_function, data);
2261 ch->cancel_function = cancel_function;
2263 ch->free_data_function = free_data_function;
2265 /* It's important that the hooks get run in reverse order that they
2268 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))