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
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; we may not have permissions to read the
580 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
581 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
585 if (dbus_connection_get_windows_user (connection, &windows_sid))
587 if (!_dbus_string_append_printf (&loginfo_buf, "sid=\"%s\" ", windows_sid))
589 dbus_free (windows_sid);
592 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
595 _dbus_string_free (&loginfo_buf);
599 _dbus_string_free (&loginfo_buf);
604 bus_connections_setup_connection (BusConnections *connections,
605 DBusConnection *connection)
608 BusConnectionData *d;
613 d = dbus_new0 (BusConnectionData, 1);
618 d->connections = connections;
619 d->connection = connection;
621 _dbus_get_current_time (&d->connection_tv_sec,
622 &d->connection_tv_usec);
624 _dbus_assert (connection_data_slot >= 0);
626 if (!dbus_connection_set_data (connection,
627 connection_data_slot,
628 d, free_connection_data))
634 dbus_connection_set_route_peer_messages (connection, TRUE);
638 dbus_error_init (&error);
639 d->selinux_id = bus_selinux_init_connection_id (connection,
641 if (dbus_error_is_set (&error))
643 /* This is a bit bogus because we pretend all errors
644 * are OOM; this is done because we know that in bus.c
645 * an OOM error disconnects the connection, which is
646 * the same thing we want on any other error.
648 dbus_error_free (&error);
652 if (!dbus_connection_set_watch_functions (connection,
653 add_connection_watch,
654 remove_connection_watch,
660 if (!dbus_connection_set_timeout_functions (connection,
661 add_connection_timeout,
662 remove_connection_timeout,
667 /* For now we don't need to set a Windows user function because
668 * there are no policies in the config file controlling what
669 * Windows users can connect. The default 'same user that owns the
670 * bus can connect' behavior of DBusConnection is fine on Windows.
672 dbus_connection_set_unix_user_function (connection,
673 allow_unix_user_function,
676 dbus_connection_set_dispatch_status_function (connection,
677 dispatch_status_function,
678 bus_context_get_loop (connections->context),
681 d->link_in_connection_list = _dbus_list_alloc_link (connection);
682 if (d->link_in_connection_list == NULL)
685 /* Setup the connection with the dispatcher */
686 if (!bus_dispatch_add_connection (connection))
689 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
691 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
693 bus_dispatch_remove_connection (connection);
698 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
699 connections->n_incomplete += 1;
701 dbus_connection_ref (connection);
703 /* Note that we might disconnect ourselves here, but it only takes
704 * effect on return to the main loop. We call this to free up
705 * expired connections if possible, and to queue the timeout for our
708 bus_connections_expire_incomplete (connections);
710 /* And we might also disconnect ourselves here, but again it
711 * only takes effect on return to main loop.
713 if (connections->n_incomplete >
714 bus_context_get_max_incomplete_connections (connections->context))
716 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
718 _dbus_assert (connections->incomplete != NULL);
719 /* Disconnect the oldest unauthenticated connection. FIXME
720 * would it be more secure to drop a *random* connection? This
721 * algorithm seems to mean that if someone can create new
722 * connections quickly enough, they can keep anyone else from
723 * completing authentication. But random may or may not really
724 * help with that, a more elaborate solution might be required.
726 dbus_connection_close (connections->incomplete->data);
735 bus_selinux_id_unref (d->selinux_id);
736 d->selinux_id = NULL;
738 if (!dbus_connection_set_watch_functions (connection,
742 _dbus_assert_not_reached ("setting watch functions to NULL failed");
744 if (!dbus_connection_set_timeout_functions (connection,
748 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
750 dbus_connection_set_unix_user_function (connection,
753 dbus_connection_set_windows_user_function (connection,
756 dbus_connection_set_dispatch_status_function (connection,
759 if (d->link_in_connection_list != NULL)
761 _dbus_assert (d->link_in_connection_list->next == NULL);
762 _dbus_assert (d->link_in_connection_list->prev == NULL);
763 _dbus_list_free_link (d->link_in_connection_list);
764 d->link_in_connection_list = NULL;
767 if (!dbus_connection_set_data (connection,
768 connection_data_slot,
770 _dbus_assert_not_reached ("failed to set connection data to null");
772 /* "d" has now been freed */
779 bus_connections_expire_incomplete (BusConnections *connections)
785 if (connections->incomplete != NULL)
787 long tv_sec, tv_usec;
791 _dbus_get_current_time (&tv_sec, &tv_usec);
792 auth_timeout = bus_context_get_auth_timeout (connections->context);
794 link = _dbus_list_get_first_link (&connections->incomplete);
797 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
798 DBusConnection *connection;
799 BusConnectionData *d;
802 connection = link->data;
804 d = BUS_CONNECTION_DATA (connection);
806 _dbus_assert (d != NULL);
808 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
809 d->connection_tv_usec,
812 if (elapsed >= (double) auth_timeout)
814 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
815 dbus_connection_close (connection);
819 /* We can end the loop, since the connections are in oldest-first order */
820 next_interval = ((double)auth_timeout) - elapsed;
821 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
822 connection, next_interval);
831 bus_expire_timeout_set_interval (connections->expire_timeout,
836 expire_incomplete_timeout (void *data)
838 BusConnections *connections = data;
840 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
842 /* note that this may remove the timeout */
843 bus_connections_expire_incomplete (connections);
849 bus_connection_get_unix_groups (DBusConnection *connection,
850 unsigned long **groups,
854 BusConnectionData *d;
857 d = BUS_CONNECTION_DATA (connection);
859 _dbus_assert (d != NULL);
864 if (dbus_connection_get_unix_user (connection, &uid))
866 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
868 _dbus_verbose ("Did not get any groups for UID %lu\n",
874 _dbus_verbose ("Got %d groups for UID %lu\n",
880 return TRUE; /* successfully got 0 groups */
884 bus_connection_is_in_unix_group (DBusConnection *connection,
888 unsigned long *group_ids;
891 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
896 while (i < n_group_ids)
898 if (group_ids[i] == gid)
900 dbus_free (group_ids);
906 dbus_free (group_ids);
911 bus_connection_get_loginfo (DBusConnection *connection)
913 BusConnectionData *d;
915 d = BUS_CONNECTION_DATA (connection);
917 if (!bus_connection_is_active (connection))
919 return d->cached_loginfo_string;
923 bus_connection_get_policy (DBusConnection *connection)
925 BusConnectionData *d;
927 d = BUS_CONNECTION_DATA (connection);
929 _dbus_assert (d != NULL);
930 _dbus_assert (d->policy != NULL);
936 foreach_active (BusConnections *connections,
937 BusConnectionForeachFunction function,
942 link = _dbus_list_get_first_link (&connections->completed);
945 DBusConnection *connection = link->data;
946 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
948 if (!(* function) (connection, data))
958 foreach_inactive (BusConnections *connections,
959 BusConnectionForeachFunction function,
964 link = _dbus_list_get_first_link (&connections->incomplete);
967 DBusConnection *connection = link->data;
968 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
970 if (!(* function) (connection, data))
980 * Calls function on each active connection; if the function returns
981 * #FALSE, stops iterating. Active connections are authenticated
982 * and have sent a Hello message.
984 * @param connections the connections object
985 * @param function the function
986 * @param data data to pass to it as a second arg
989 bus_connections_foreach_active (BusConnections *connections,
990 BusConnectionForeachFunction function,
993 foreach_active (connections, function, data);
997 * Calls function on each connection; if the function returns
998 * #FALSE, stops iterating.
1000 * @param connections the connections object
1001 * @param function the function
1002 * @param data data to pass to it as a second arg
1005 bus_connections_foreach (BusConnections *connections,
1006 BusConnectionForeachFunction function,
1009 if (!foreach_active (connections, function, data))
1012 foreach_inactive (connections, function, data);
1016 bus_connections_get_context (BusConnections *connections)
1018 return connections->context;
1022 * This is used to avoid covering the same connection twice when
1023 * traversing connections. Note that it assumes we will
1024 * bus_connection_mark_stamp() each connection at least once per
1025 * INT_MAX increments of the global stamp, or wraparound would break
1029 bus_connections_increment_stamp (BusConnections *connections)
1031 connections->stamp += 1;
1034 /* Mark connection with current stamp, return TRUE if it
1035 * didn't already have that stamp
1038 bus_connection_mark_stamp (DBusConnection *connection)
1040 BusConnectionData *d;
1042 d = BUS_CONNECTION_DATA (connection);
1044 _dbus_assert (d != NULL);
1046 if (d->stamp == d->connections->stamp)
1050 d->stamp = d->connections->stamp;
1056 bus_connection_get_context (DBusConnection *connection)
1058 BusConnectionData *d;
1060 d = BUS_CONNECTION_DATA (connection);
1062 _dbus_assert (d != NULL);
1064 return d->connections->context;
1068 bus_connection_get_connections (DBusConnection *connection)
1070 BusConnectionData *d;
1072 d = BUS_CONNECTION_DATA (connection);
1074 _dbus_assert (d != NULL);
1076 return d->connections;
1080 bus_connection_get_registry (DBusConnection *connection)
1082 BusConnectionData *d;
1084 d = BUS_CONNECTION_DATA (connection);
1086 _dbus_assert (d != NULL);
1088 return bus_context_get_registry (d->connections->context);
1092 bus_connection_get_activation (DBusConnection *connection)
1094 BusConnectionData *d;
1096 d = BUS_CONNECTION_DATA (connection);
1098 _dbus_assert (d != NULL);
1100 return bus_context_get_activation (d->connections->context);
1104 bus_connection_get_matchmaker (DBusConnection *connection)
1106 BusConnectionData *d;
1108 d = BUS_CONNECTION_DATA (connection);
1110 _dbus_assert (d != NULL);
1112 return bus_context_get_matchmaker (d->connections->context);
1116 bus_connection_get_selinux_id (DBusConnection *connection)
1118 BusConnectionData *d;
1120 d = BUS_CONNECTION_DATA (connection);
1122 _dbus_assert (d != NULL);
1124 return d->selinux_id;
1128 * Checks whether the connection is registered with the message bus.
1130 * @param connection the connection
1131 * @returns #TRUE if we're an active message bus participant
1134 bus_connection_is_active (DBusConnection *connection)
1136 BusConnectionData *d;
1138 d = BUS_CONNECTION_DATA (connection);
1140 return d != NULL && d->name != NULL;
1144 bus_connection_preallocate_oom_error (DBusConnection *connection)
1146 DBusMessage *message;
1147 DBusPreallocatedSend *preallocated;
1148 BusConnectionData *d;
1150 d = BUS_CONNECTION_DATA (connection);
1152 _dbus_assert (d != NULL);
1154 if (d->oom_preallocated != NULL)
1157 preallocated = dbus_connection_preallocate_send (connection);
1158 if (preallocated == NULL)
1161 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1163 if (message == NULL)
1165 dbus_connection_free_preallocated_send (connection, preallocated);
1169 /* d->name may be NULL, but that is OK */
1170 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1171 !dbus_message_set_destination (message, d->name) ||
1172 !dbus_message_set_sender (message,
1175 dbus_connection_free_preallocated_send (connection, preallocated);
1176 dbus_message_unref (message);
1180 /* set reply serial to placeholder value just so space is already allocated
1183 if (!dbus_message_set_reply_serial (message, 14))
1185 dbus_connection_free_preallocated_send (connection, preallocated);
1186 dbus_message_unref (message);
1190 d->oom_message = message;
1191 d->oom_preallocated = preallocated;
1197 bus_connection_send_oom_error (DBusConnection *connection,
1198 DBusMessage *in_reply_to)
1200 BusConnectionData *d;
1202 d = BUS_CONNECTION_DATA (connection);
1204 _dbus_assert (d != NULL);
1205 _dbus_assert (d->oom_message != NULL);
1207 /* should always succeed since we set it to a placeholder earlier */
1208 if (!dbus_message_set_reply_serial (d->oom_message,
1209 dbus_message_get_serial (in_reply_to)))
1210 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1212 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1214 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1215 d->oom_message, NULL);
1217 dbus_message_unref (d->oom_message);
1218 d->oom_message = NULL;
1219 d->oom_preallocated = NULL;
1223 bus_connection_add_match_rule_link (DBusConnection *connection,
1226 BusConnectionData *d;
1228 d = BUS_CONNECTION_DATA (connection);
1229 _dbus_assert (d != NULL);
1231 _dbus_list_append_link (&d->match_rules, link);
1233 d->n_match_rules += 1;
1237 bus_connection_add_match_rule (DBusConnection *connection,
1242 link = _dbus_list_alloc_link (rule);
1247 bus_connection_add_match_rule_link (connection, link);
1253 bus_connection_remove_match_rule (DBusConnection *connection,
1256 BusConnectionData *d;
1258 d = BUS_CONNECTION_DATA (connection);
1259 _dbus_assert (d != NULL);
1261 _dbus_list_remove_last (&d->match_rules, rule);
1263 d->n_match_rules -= 1;
1264 _dbus_assert (d->n_match_rules >= 0);
1268 bus_connection_get_n_match_rules (DBusConnection *connection)
1270 BusConnectionData *d;
1272 d = BUS_CONNECTION_DATA (connection);
1273 _dbus_assert (d != NULL);
1275 return d->n_match_rules;
1279 bus_connection_add_owned_service_link (DBusConnection *connection,
1282 BusConnectionData *d;
1284 d = BUS_CONNECTION_DATA (connection);
1285 _dbus_assert (d != NULL);
1287 _dbus_list_append_link (&d->services_owned, link);
1289 d->n_services_owned += 1;
1293 bus_connection_add_owned_service (DBusConnection *connection,
1294 BusService *service)
1298 link = _dbus_list_alloc_link (service);
1303 bus_connection_add_owned_service_link (connection, link);
1309 bus_connection_remove_owned_service (DBusConnection *connection,
1310 BusService *service)
1312 BusConnectionData *d;
1314 d = BUS_CONNECTION_DATA (connection);
1315 _dbus_assert (d != NULL);
1317 _dbus_list_remove_last (&d->services_owned, service);
1319 d->n_services_owned -= 1;
1320 _dbus_assert (d->n_services_owned >= 0);
1324 bus_connection_get_n_services_owned (DBusConnection *connection)
1326 BusConnectionData *d;
1328 d = BUS_CONNECTION_DATA (connection);
1329 _dbus_assert (d != NULL);
1331 return d->n_services_owned;
1335 bus_connection_complete (DBusConnection *connection,
1336 const DBusString *name,
1339 BusConnectionData *d;
1342 d = BUS_CONNECTION_DATA (connection);
1343 _dbus_assert (d != NULL);
1344 _dbus_assert (d->name == NULL);
1345 _dbus_assert (d->policy == NULL);
1347 _dbus_assert (!bus_connection_is_active (connection));
1349 if (!_dbus_string_copy_data (name, &d->name))
1351 BUS_SET_OOM (error);
1355 _dbus_assert (d->name != NULL);
1357 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1359 d->policy = bus_context_create_client_policy (d->connections->context,
1363 /* we may have a NULL policy on OOM or error getting list of
1364 * groups for a user. In the latter case we don't handle it so
1365 * well currently, as it will just keep failing over and over.
1368 if (d->policy == NULL)
1370 _dbus_verbose ("Failed to create security policy for connection %p\n",
1372 _DBUS_ASSERT_ERROR_IS_SET (error);
1373 dbus_free (d->name);
1378 if (dbus_connection_get_unix_user (connection, &uid))
1380 if (!adjust_connections_for_uid (d->connections,
1385 /* Create and cache a string which holds information about the
1386 * peer process; used for logging purposes.
1388 if (!cache_peer_loginfo_string (d, connection))
1391 /* Now the connection is active, move it between lists */
1392 _dbus_list_unlink (&d->connections->incomplete,
1393 d->link_in_connection_list);
1394 d->connections->n_incomplete -= 1;
1395 _dbus_list_append_link (&d->connections->completed,
1396 d->link_in_connection_list);
1397 d->connections->n_completed += 1;
1399 _dbus_assert (d->connections->n_incomplete >= 0);
1400 _dbus_assert (d->connections->n_completed > 0);
1402 /* See if we can remove the timeout */
1403 bus_connections_expire_incomplete (d->connections);
1405 _dbus_assert (bus_connection_is_active (connection));
1409 BUS_SET_OOM (error);
1410 dbus_free (d->name);
1413 bus_client_policy_unref (d->policy);
1419 bus_connection_get_name (DBusConnection *connection)
1421 BusConnectionData *d;
1423 d = BUS_CONNECTION_DATA (connection);
1424 _dbus_assert (d != NULL);
1430 * Check whether completing the passed-in connection would
1431 * exceed limits, and if so set error and return #FALSE
1434 bus_connections_check_limits (BusConnections *connections,
1435 DBusConnection *requesting_completion,
1438 BusConnectionData *d;
1441 d = BUS_CONNECTION_DATA (requesting_completion);
1442 _dbus_assert (d != NULL);
1444 _dbus_assert (d->name == NULL);
1446 if (connections->n_completed >=
1447 bus_context_get_max_completed_connections (connections->context))
1449 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1450 "The maximum number of active connections has been reached");
1454 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1456 if (get_connections_for_uid (connections, uid) >=
1457 bus_context_get_max_connections_per_user (connections->context))
1459 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1460 "The maximum number of active connections for UID %lu has been reached",
1470 bus_pending_reply_free (BusPendingReply *pending)
1472 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1474 pending->will_send_reply,
1475 pending->will_get_reply,
1476 pending->reply_serial);
1478 dbus_free (pending);
1482 bus_pending_reply_send_no_reply (BusConnections *connections,
1483 BusTransaction *transaction,
1484 BusPendingReply *pending)
1486 DBusMessage *message;
1487 DBusMessageIter iter;
1493 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1494 if (message == NULL)
1497 dbus_message_set_no_reply (message, TRUE);
1499 if (!dbus_message_set_reply_serial (message,
1500 pending->reply_serial))
1503 if (!dbus_message_set_error_name (message,
1504 DBUS_ERROR_NO_REPLY))
1507 errmsg = "Message did not receive a reply (timeout by message bus)";
1508 dbus_message_iter_init_append (message, &iter);
1509 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1512 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1519 dbus_message_unref (message);
1524 bus_pending_reply_expired (BusExpireList *list,
1528 BusPendingReply *pending = link->data;
1529 BusConnections *connections = data;
1530 BusTransaction *transaction;
1532 /* No reply is forthcoming. So nuke it if we can. If not,
1533 * leave it in the list to try expiring again later when we
1537 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1539 pending->will_send_reply,
1540 pending->will_get_reply,
1541 pending->reply_serial);
1543 transaction = bus_transaction_new (connections->context);
1544 if (transaction == NULL)
1547 if (!bus_pending_reply_send_no_reply (connections,
1551 bus_transaction_cancel_and_free (transaction);
1555 bus_expire_list_remove_link (connections->pending_replies, link);
1557 bus_pending_reply_free (pending);
1558 bus_transaction_execute_and_free (transaction);
1564 bus_connection_drop_pending_replies (BusConnections *connections,
1565 DBusConnection *connection)
1567 /* The DBusConnection is almost 100% finalized here, so you can't
1568 * do anything with it except check for pointer equality
1572 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1575 link = bus_expire_list_get_first_link (connections->pending_replies);
1576 while (link != NULL)
1579 BusPendingReply *pending;
1581 next = bus_expire_list_get_next_link (connections->pending_replies,
1583 pending = link->data;
1585 if (pending->will_get_reply == connection)
1587 /* We don't need to track this pending reply anymore */
1589 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1591 pending->will_send_reply,
1592 pending->will_get_reply,
1593 pending->reply_serial);
1595 bus_expire_list_remove_link (connections->pending_replies,
1597 bus_pending_reply_free (pending);
1599 else if (pending->will_send_reply == connection)
1601 /* The reply isn't going to be sent, so set things
1602 * up so it will be expired right away
1604 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1606 pending->will_send_reply,
1607 pending->will_get_reply,
1608 pending->reply_serial);
1610 pending->will_send_reply = NULL;
1611 pending->expire_item.added_tv_sec = 0;
1612 pending->expire_item.added_tv_usec = 0;
1614 bus_expire_list_recheck_immediately (connections->pending_replies);
1624 BusPendingReply *pending;
1625 BusConnections *connections;
1626 } CancelPendingReplyData;
1629 cancel_pending_reply (void *data)
1631 CancelPendingReplyData *d = data;
1633 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1635 if (!bus_expire_list_remove (d->connections->pending_replies,
1636 &d->pending->expire_item))
1637 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1639 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1643 cancel_pending_reply_data_free (void *data)
1645 CancelPendingReplyData *d = data;
1647 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1649 /* d->pending should be either freed or still
1650 * in the list of pending replies (owned by someone
1658 * Record that a reply is allowed; return TRUE on success.
1661 bus_connections_expect_reply (BusConnections *connections,
1662 BusTransaction *transaction,
1663 DBusConnection *will_get_reply,
1664 DBusConnection *will_send_reply,
1665 DBusMessage *reply_to_this,
1668 BusPendingReply *pending;
1669 dbus_uint32_t reply_serial;
1671 CancelPendingReplyData *cprd;
1674 _dbus_assert (will_get_reply != NULL);
1675 _dbus_assert (will_send_reply != NULL);
1676 _dbus_assert (reply_to_this != NULL);
1678 if (dbus_message_get_no_reply (reply_to_this))
1679 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1681 reply_serial = dbus_message_get_serial (reply_to_this);
1683 link = bus_expire_list_get_first_link (connections->pending_replies);
1685 while (link != NULL)
1687 pending = link->data;
1689 if (pending->reply_serial == reply_serial &&
1690 pending->will_get_reply == will_get_reply &&
1691 pending->will_send_reply == will_send_reply)
1693 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1694 "Message has the same reply serial as a currently-outstanding existing method call");
1698 link = bus_expire_list_get_next_link (connections->pending_replies,
1700 if (pending->will_get_reply == will_get_reply)
1705 bus_context_get_max_replies_per_connection (connections->context))
1707 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1708 "The maximum number of pending replies per connection has been reached");
1712 pending = dbus_new0 (BusPendingReply, 1);
1713 if (pending == NULL)
1715 BUS_SET_OOM (error);
1719 #ifdef DBUS_ENABLE_VERBOSE_MODE
1720 /* so we can see a not-yet-added pending reply */
1721 pending->expire_item.added_tv_sec = 1;
1722 pending->expire_item.added_tv_usec = 1;
1725 pending->will_get_reply = will_get_reply;
1726 pending->will_send_reply = will_send_reply;
1727 pending->reply_serial = reply_serial;
1729 cprd = dbus_new0 (CancelPendingReplyData, 1);
1732 BUS_SET_OOM (error);
1733 bus_pending_reply_free (pending);
1737 if (!bus_expire_list_add (connections->pending_replies,
1738 &pending->expire_item))
1740 BUS_SET_OOM (error);
1742 bus_pending_reply_free (pending);
1746 if (!bus_transaction_add_cancel_hook (transaction,
1747 cancel_pending_reply,
1749 cancel_pending_reply_data_free))
1751 BUS_SET_OOM (error);
1752 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1754 bus_pending_reply_free (pending);
1758 cprd->pending = pending;
1759 cprd->connections = connections;
1761 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1762 &pending->expire_item.added_tv_usec);
1764 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1766 pending->will_send_reply,
1767 pending->will_get_reply,
1768 pending->reply_serial);
1776 BusConnections *connections;
1777 } CheckPendingReplyData;
1780 cancel_check_pending_reply (void *data)
1782 CheckPendingReplyData *d = data;
1784 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1786 bus_expire_list_add_link (d->connections->pending_replies,
1792 check_pending_reply_data_free (void *data)
1794 CheckPendingReplyData *d = data;
1796 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1798 if (d->link != NULL)
1800 BusPendingReply *pending = d->link->data;
1802 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1803 &pending->expire_item));
1805 bus_pending_reply_free (pending);
1806 _dbus_list_free_link (d->link);
1813 * Check whether a reply is allowed, remove BusPendingReply
1814 * if so, return TRUE if so.
1817 bus_connections_check_reply (BusConnections *connections,
1818 BusTransaction *transaction,
1819 DBusConnection *sending_reply,
1820 DBusConnection *receiving_reply,
1824 CheckPendingReplyData *cprd;
1826 dbus_uint32_t reply_serial;
1828 _dbus_assert (sending_reply != NULL);
1829 _dbus_assert (receiving_reply != NULL);
1831 reply_serial = dbus_message_get_reply_serial (reply);
1833 link = bus_expire_list_get_first_link (connections->pending_replies);
1834 while (link != NULL)
1836 BusPendingReply *pending = link->data;
1838 if (pending->reply_serial == reply_serial &&
1839 pending->will_get_reply == receiving_reply &&
1840 pending->will_send_reply == sending_reply)
1842 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1846 link = bus_expire_list_get_next_link (connections->pending_replies,
1852 _dbus_verbose ("No pending reply expected\n");
1857 cprd = dbus_new0 (CheckPendingReplyData, 1);
1860 BUS_SET_OOM (error);
1864 if (!bus_transaction_add_cancel_hook (transaction,
1865 cancel_check_pending_reply,
1867 check_pending_reply_data_free))
1869 BUS_SET_OOM (error);
1875 cprd->connections = connections;
1877 bus_expire_list_unlink (connections->pending_replies,
1880 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1888 * Note that this is fairly fragile; in particular, don't try to use
1889 * one transaction across any main loop iterations.
1894 BusTransaction *transaction;
1895 DBusMessage *message;
1896 DBusPreallocatedSend *preallocated;
1901 BusTransactionCancelFunction cancel_function;
1902 DBusFreeFunction free_data_function;
1906 struct BusTransaction
1908 DBusList *connections;
1909 BusContext *context;
1910 DBusList *cancel_hooks;
1914 message_to_send_free (DBusConnection *connection,
1915 MessageToSend *to_send)
1917 if (to_send->message)
1918 dbus_message_unref (to_send->message);
1920 if (to_send->preallocated)
1921 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1923 dbus_free (to_send);
1927 cancel_hook_cancel (void *element,
1930 CancelHook *ch = element;
1932 _dbus_verbose ("Running transaction cancel hook\n");
1934 if (ch->cancel_function)
1935 (* ch->cancel_function) (ch->data);
1939 cancel_hook_free (void *element,
1942 CancelHook *ch = element;
1944 if (ch->free_data_function)
1945 (* ch->free_data_function) (ch->data);
1951 free_cancel_hooks (BusTransaction *transaction)
1953 _dbus_list_foreach (&transaction->cancel_hooks,
1954 cancel_hook_free, NULL);
1956 _dbus_list_clear (&transaction->cancel_hooks);
1960 bus_transaction_new (BusContext *context)
1962 BusTransaction *transaction;
1964 transaction = dbus_new0 (BusTransaction, 1);
1965 if (transaction == NULL)
1968 transaction->context = context;
1974 bus_transaction_get_context (BusTransaction *transaction)
1976 return transaction->context;
1980 bus_transaction_get_connections (BusTransaction *transaction)
1982 return bus_context_get_connections (transaction->context);
1986 bus_transaction_send_from_driver (BusTransaction *transaction,
1987 DBusConnection *connection,
1988 DBusMessage *message)
1990 /* We have to set the sender to the driver, and have
1991 * to check security policy since it was not done in
1994 _dbus_verbose ("Sending %s %s %s from driver\n",
1995 dbus_message_get_interface (message) ?
1996 dbus_message_get_interface (message) : "(no interface)",
1997 dbus_message_get_member (message) ?
1998 dbus_message_get_member (message) : "(no member)",
1999 dbus_message_get_error_name (message) ?
2000 dbus_message_get_error_name (message) : "(no error name)");
2002 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2005 if (bus_connection_is_active (connection))
2007 if (!dbus_message_set_destination (message,
2008 bus_connection_get_name (connection)))
2012 /* bus driver never wants a reply */
2013 dbus_message_set_no_reply (message, TRUE);
2015 /* If security policy doesn't allow the message, we silently
2016 * eat it; the driver doesn't care about getting a reply.
2018 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2020 NULL, connection, connection, message, NULL))
2023 return bus_transaction_send (transaction, connection, message);
2027 bus_transaction_send (BusTransaction *transaction,
2028 DBusConnection *connection,
2029 DBusMessage *message)
2031 MessageToSend *to_send;
2032 BusConnectionData *d;
2035 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2036 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2037 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2039 dbus_message_get_interface (message) ?
2040 dbus_message_get_interface (message) : "(unset)",
2041 dbus_message_get_member (message) ?
2042 dbus_message_get_member (message) : "(unset)",
2043 dbus_message_get_error_name (message) ?
2044 dbus_message_get_error_name (message) : "(unset)",
2045 dbus_connection_get_is_connected (connection) ?
2046 "" : " (disconnected)");
2048 _dbus_assert (dbus_message_get_sender (message) != NULL);
2050 if (!dbus_connection_get_is_connected (connection))
2051 return TRUE; /* silently ignore disconnected connections */
2053 d = BUS_CONNECTION_DATA (connection);
2054 _dbus_assert (d != NULL);
2056 to_send = dbus_new (MessageToSend, 1);
2057 if (to_send == NULL)
2062 to_send->preallocated = dbus_connection_preallocate_send (connection);
2063 if (to_send->preallocated == NULL)
2065 dbus_free (to_send);
2069 dbus_message_ref (message);
2070 to_send->message = message;
2071 to_send->transaction = transaction;
2073 _dbus_verbose ("about to prepend message\n");
2075 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2077 message_to_send_free (connection, to_send);
2081 _dbus_verbose ("prepended message\n");
2083 /* See if we already had this connection in the list
2084 * for this transaction. If we have a pending message,
2085 * then we should already be in transaction->connections
2087 link = _dbus_list_get_first_link (&d->transaction_messages);
2088 _dbus_assert (link->data == to_send);
2089 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2090 while (link != NULL)
2092 MessageToSend *m = link->data;
2093 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2095 if (m->transaction == transaction)
2103 if (!_dbus_list_prepend (&transaction->connections, connection))
2105 _dbus_list_remove (&d->transaction_messages, to_send);
2106 message_to_send_free (connection, to_send);
2115 connection_cancel_transaction (DBusConnection *connection,
2116 BusTransaction *transaction)
2119 BusConnectionData *d;
2121 d = BUS_CONNECTION_DATA (connection);
2122 _dbus_assert (d != NULL);
2124 link = _dbus_list_get_first_link (&d->transaction_messages);
2125 while (link != NULL)
2127 MessageToSend *m = link->data;
2128 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2130 if (m->transaction == transaction)
2132 _dbus_list_remove_link (&d->transaction_messages,
2135 message_to_send_free (connection, m);
2143 bus_transaction_cancel_and_free (BusTransaction *transaction)
2145 DBusConnection *connection;
2147 _dbus_verbose ("TRANSACTION: cancelled\n");
2149 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2150 connection_cancel_transaction (connection, transaction);
2152 _dbus_assert (transaction->connections == NULL);
2154 _dbus_list_foreach (&transaction->cancel_hooks,
2155 cancel_hook_cancel, NULL);
2157 free_cancel_hooks (transaction);
2159 dbus_free (transaction);
2163 connection_execute_transaction (DBusConnection *connection,
2164 BusTransaction *transaction)
2167 BusConnectionData *d;
2169 d = BUS_CONNECTION_DATA (connection);
2170 _dbus_assert (d != NULL);
2172 /* Send the queue in order (FIFO) */
2173 link = _dbus_list_get_last_link (&d->transaction_messages);
2174 while (link != NULL)
2176 MessageToSend *m = link->data;
2177 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2179 if (m->transaction == transaction)
2181 _dbus_list_remove_link (&d->transaction_messages,
2184 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2186 dbus_connection_send_preallocated (connection,
2191 m->preallocated = NULL; /* so we don't double-free it */
2193 message_to_send_free (connection, m);
2201 bus_transaction_execute_and_free (BusTransaction *transaction)
2203 /* For each connection in transaction->connections
2206 DBusConnection *connection;
2208 _dbus_verbose ("TRANSACTION: executing\n");
2210 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2211 connection_execute_transaction (connection, transaction);
2213 _dbus_assert (transaction->connections == NULL);
2215 free_cancel_hooks (transaction);
2217 dbus_free (transaction);
2221 bus_connection_remove_transactions (DBusConnection *connection)
2223 MessageToSend *to_send;
2224 BusConnectionData *d;
2226 d = BUS_CONNECTION_DATA (connection);
2227 _dbus_assert (d != NULL);
2229 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2231 /* only has an effect for the first MessageToSend listing this transaction */
2232 _dbus_list_remove (&to_send->transaction->connections,
2235 _dbus_list_remove (&d->transaction_messages, to_send);
2236 message_to_send_free (connection, to_send);
2241 * Converts the DBusError to a message reply
2244 bus_transaction_send_error_reply (BusTransaction *transaction,
2245 DBusConnection *connection,
2246 const DBusError *error,
2247 DBusMessage *in_reply_to)
2251 _dbus_assert (error != NULL);
2252 _DBUS_ASSERT_ERROR_IS_SET (error);
2254 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2255 error->name, error->message);
2257 reply = dbus_message_new_error (in_reply_to,
2263 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2265 dbus_message_unref (reply);
2269 dbus_message_unref (reply);
2275 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2276 BusTransactionCancelFunction cancel_function,
2278 DBusFreeFunction free_data_function)
2282 ch = dbus_new (CancelHook, 1);
2286 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2287 cancel_function, data);
2289 ch->cancel_function = cancel_function;
2291 ch->free_data_function = free_data_function;
2293 /* It's important that the hooks get run in reverse order that they
2296 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))