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>
36 #include <dbus/dbus-connection-internal.h>
38 /* Trim executed commands to this length; we want to keep logs readable */
39 #define MAX_LOG_COMMAND_LEN 50
41 static void bus_connection_remove_transactions (DBusConnection *connection);
45 BusExpireItem expire_item;
47 DBusConnection *will_get_reply;
48 DBusConnection *will_send_reply;
50 dbus_uint32_t reply_serial;
57 DBusList *completed; /**< List of all completed connections */
58 int n_completed; /**< Length of completed list */
59 DBusList *incomplete; /**< List of all not-yet-active connections */
60 int n_incomplete; /**< Length of incomplete list */
62 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
63 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
64 int stamp; /**< Incrementing number */
65 BusExpireList *pending_replies; /**< List of pending replies */
67 #ifdef DBUS_ENABLE_STATS
68 int total_match_rules;
70 int peak_match_rules_per_conn;
74 int peak_bus_names_per_conn;
78 static dbus_int32_t connection_data_slot = -1;
82 BusConnections *connections;
83 DBusList *link_in_connection_list;
84 DBusConnection *connection;
85 DBusList *services_owned;
87 DBusList *match_rules;
90 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
91 DBusMessage *oom_message;
92 DBusPreallocatedSend *oom_preallocated;
93 BusClientPolicy *policy;
95 char *cached_loginfo_string;
96 BusSELinuxID *selinux_id;
98 long connection_tv_sec; /**< Time when we connected (seconds component) */
99 long connection_tv_usec; /**< Time when we connected (microsec component) */
100 int stamp; /**< connections->stamp last time we were traversed */
102 #ifdef DBUS_ENABLE_STATS
103 int peak_match_rules;
106 int n_pending_unix_fds;
107 DBusTimeout *pending_unix_fds_timeout;
110 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
114 static void bus_connection_drop_pending_replies (BusConnections *connections,
115 DBusConnection *connection);
117 static dbus_bool_t expire_incomplete_timeout (void *data);
119 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
122 connection_get_loop (DBusConnection *connection)
124 BusConnectionData *d;
126 d = BUS_CONNECTION_DATA (connection);
128 return bus_context_get_loop (d->connections->context);
133 get_connections_for_uid (BusConnections *connections,
139 /* val is NULL is 0 when it isn't in the hash yet */
141 val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
144 current_count = _DBUS_POINTER_TO_INT (val);
146 return current_count;
150 adjust_connections_for_uid (BusConnections *connections,
156 current_count = get_connections_for_uid (connections, uid);
158 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
159 ": was %d adjustment %d making %d\n",
160 uid, current_count, adjustment, current_count + adjustment);
162 _dbus_assert (current_count >= 0);
164 current_count += adjustment;
166 _dbus_assert (current_count >= 0);
168 if (current_count == 0)
170 _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
177 retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
178 uid, _DBUS_INT_TO_POINTER (current_count));
180 /* only positive adjustment can fail as otherwise
181 * a hash entry should already exist
183 _dbus_assert (adjustment > 0 ||
184 (adjustment <= 0 && retval));
191 bus_connection_disconnected (DBusConnection *connection)
193 BusConnectionData *d;
195 BusMatchmaker *matchmaker;
197 d = BUS_CONNECTION_DATA (connection);
198 _dbus_assert (d != NULL);
200 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
201 d->name ? d->name : "(inactive)");
203 /* Delete our match rules */
204 if (d->n_match_rules > 0)
206 matchmaker = bus_context_get_matchmaker (d->connections->context);
207 bus_matchmaker_disconnected (matchmaker, connection);
210 /* Drop any service ownership. Unfortunately, this requires
211 * memory allocation and there doesn't seem to be a good way to
212 * handle it other than sleeping; we can't "fail" the operation of
213 * disconnecting a client, and preallocating a broadcast "service is
214 * now gone" message for every client-service pair seems kind of
217 while ((service = _dbus_list_get_last (&d->services_owned)))
219 BusTransaction *transaction;
224 dbus_error_init (&error);
226 while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
227 _dbus_wait_for_memory ();
229 if (!bus_service_remove_owner (service, connection,
230 transaction, &error))
232 _DBUS_ASSERT_ERROR_IS_SET (&error);
234 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
236 dbus_error_free (&error);
237 bus_transaction_cancel_and_free (transaction);
238 _dbus_wait_for_memory ();
243 _dbus_verbose ("Failed to remove service owner: %s %s\n",
244 error.name, error.message);
245 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
249 bus_transaction_execute_and_free (transaction);
252 bus_dispatch_remove_connection (connection);
254 /* no more watching */
255 if (!dbus_connection_set_watch_functions (connection,
259 _dbus_assert_not_reached ("setting watch functions to NULL failed");
261 if (!dbus_connection_set_timeout_functions (connection,
265 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
267 dbus_connection_set_unix_user_function (connection,
269 dbus_connection_set_windows_user_function (connection,
272 dbus_connection_set_dispatch_status_function (connection,
275 if (d->pending_unix_fds_timeout)
277 _dbus_loop_remove_timeout (bus_context_get_loop (d->connections->context),
278 d->pending_unix_fds_timeout);
279 _dbus_timeout_unref (d->pending_unix_fds_timeout);
281 d->pending_unix_fds_timeout = NULL;
282 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
284 bus_connection_remove_transactions (connection);
286 if (d->link_in_connection_list != NULL)
292 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
293 d->link_in_connection_list = NULL;
294 d->connections->n_completed -= 1;
296 if (dbus_connection_get_unix_user (connection, &uid))
298 if (!adjust_connections_for_uid (d->connections,
300 _dbus_assert_not_reached ("adjusting downward should never fail");
305 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
306 d->link_in_connection_list = NULL;
307 d->connections->n_incomplete -= 1;
309 /* If we have dropped below the max. number of incomplete
310 * connections, start accept()ing again */
311 bus_context_check_all_watches (d->connections->context);
314 _dbus_assert (d->connections->n_incomplete >= 0);
315 _dbus_assert (d->connections->n_completed >= 0);
318 bus_connection_drop_pending_replies (d->connections, connection);
320 /* frees "d" as side effect */
321 dbus_connection_set_data (connection,
322 connection_data_slot,
325 dbus_connection_unref (connection);
329 add_connection_watch (DBusWatch *watch,
332 DBusConnection *connection = data;
334 return _dbus_loop_add_watch (connection_get_loop (connection), watch);
338 remove_connection_watch (DBusWatch *watch,
341 DBusConnection *connection = data;
343 _dbus_loop_remove_watch (connection_get_loop (connection), watch);
347 toggle_connection_watch (DBusWatch *watch,
350 DBusConnection *connection = data;
352 _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
356 add_connection_timeout (DBusTimeout *timeout,
359 DBusConnection *connection = data;
361 return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
365 remove_connection_timeout (DBusTimeout *timeout,
368 DBusConnection *connection = data;
370 _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
374 dispatch_status_function (DBusConnection *connection,
375 DBusDispatchStatus new_status,
378 DBusLoop *loop = data;
380 if (new_status != DBUS_DISPATCH_COMPLETE)
382 while (!_dbus_loop_queue_dispatch (loop, connection))
383 _dbus_wait_for_memory ();
388 allow_unix_user_function (DBusConnection *connection,
392 BusConnectionData *d;
394 d = BUS_CONNECTION_DATA (connection);
396 _dbus_assert (d != NULL);
398 return bus_context_allow_unix_user (d->connections->context, uid);
402 free_connection_data (void *data)
404 BusConnectionData *d = data;
406 /* services_owned should be NULL since we should be disconnected */
407 _dbus_assert (d->services_owned == NULL);
408 _dbus_assert (d->n_services_owned == 0);
410 _dbus_assert (d->transaction_messages == NULL);
412 if (d->oom_preallocated)
413 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
416 dbus_message_unref (d->oom_message);
419 bus_client_policy_unref (d->policy);
422 bus_selinux_id_unref (d->selinux_id);
424 dbus_free (d->cached_loginfo_string);
432 bus_connections_new (BusContext *context)
434 BusConnections *connections;
436 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
439 connections = dbus_new0 (BusConnections, 1);
440 if (connections == NULL)
443 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
445 if (connections->completed_by_user == NULL)
448 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
449 expire_incomplete_timeout,
451 if (connections->expire_timeout == NULL)
454 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
456 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
457 bus_context_get_reply_timeout (context),
458 bus_pending_reply_expired,
460 if (connections->pending_replies == NULL)
463 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
464 connections->expire_timeout))
467 connections->refcount = 1;
468 connections->context = context;
473 bus_expire_list_free (connections->pending_replies);
475 _dbus_timeout_unref (connections->expire_timeout);
477 _dbus_hash_table_unref (connections->completed_by_user);
479 dbus_free (connections);
481 dbus_connection_free_data_slot (&connection_data_slot);
487 bus_connections_ref (BusConnections *connections)
489 _dbus_assert (connections->refcount > 0);
490 connections->refcount += 1;
496 bus_connections_unref (BusConnections *connections)
498 _dbus_assert (connections->refcount > 0);
499 connections->refcount -= 1;
500 if (connections->refcount == 0)
502 /* drop all incomplete */
503 while (connections->incomplete != NULL)
505 DBusConnection *connection;
507 connection = connections->incomplete->data;
509 dbus_connection_ref (connection);
510 dbus_connection_close (connection);
511 bus_connection_disconnected (connection);
512 dbus_connection_unref (connection);
515 _dbus_assert (connections->n_incomplete == 0);
517 /* drop all real connections */
518 while (connections->completed != NULL)
520 DBusConnection *connection;
522 connection = connections->completed->data;
524 dbus_connection_ref (connection);
525 dbus_connection_close (connection);
526 bus_connection_disconnected (connection);
527 dbus_connection_unref (connection);
530 _dbus_assert (connections->n_completed == 0);
532 bus_expire_list_free (connections->pending_replies);
534 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
535 connections->expire_timeout);
537 _dbus_timeout_unref (connections->expire_timeout);
539 _dbus_hash_table_unref (connections->completed_by_user);
541 dbus_free (connections);
543 dbus_connection_free_data_slot (&connection_data_slot);
547 /* Used for logging */
549 cache_peer_loginfo_string (BusConnectionData *d,
550 DBusConnection *connection)
552 DBusString loginfo_buf;
556 dbus_bool_t prev_added;
558 if (!_dbus_string_init (&loginfo_buf))
562 if (dbus_connection_get_unix_user (connection, &uid))
564 if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
570 if (dbus_connection_get_unix_process_id (connection, &pid))
574 if (!_dbus_string_append_byte (&loginfo_buf, ' '))
577 if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
579 /* Ignore errors here; we may not have permissions to read the
581 _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
582 if (!_dbus_string_append_byte (&loginfo_buf, '"'))
586 if (dbus_connection_get_windows_user (connection, &windows_sid))
588 dbus_bool_t did_append;
589 did_append = _dbus_string_append_printf (&loginfo_buf,
590 "sid=\"%s\" ", windows_sid);
591 dbus_free (windows_sid);
596 if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
599 _dbus_string_free (&loginfo_buf);
603 _dbus_string_free (&loginfo_buf);
608 check_pending_fds_cb (DBusConnection *connection)
610 BusConnectionData *d = BUS_CONNECTION_DATA (connection);
611 int n_pending_unix_fds_old = d->n_pending_unix_fds;
612 int n_pending_unix_fds_new;
614 n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection);
616 _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n",
617 connection, n_pending_unix_fds_old, n_pending_unix_fds_new);
619 if (n_pending_unix_fds_old == 0 && n_pending_unix_fds_new > 0)
621 _dbus_timeout_set_interval (d->pending_unix_fds_timeout,
622 bus_context_get_pending_fd_timeout (d->connections->context));
623 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, TRUE);
626 if (n_pending_unix_fds_old > 0 && n_pending_unix_fds_new == 0)
628 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
632 d->n_pending_unix_fds = n_pending_unix_fds_new;
636 pending_unix_fds_timeout_cb (void *data)
638 DBusConnection *connection = data;
639 dbus_connection_close (connection);
644 bus_connections_setup_connection (BusConnections *connections,
645 DBusConnection *connection)
648 BusConnectionData *d;
653 d = dbus_new0 (BusConnectionData, 1);
658 d->connections = connections;
659 d->connection = connection;
661 _dbus_get_monotonic_time (&d->connection_tv_sec,
662 &d->connection_tv_usec);
664 _dbus_assert (connection_data_slot >= 0);
666 if (!dbus_connection_set_data (connection,
667 connection_data_slot,
668 d, free_connection_data))
674 dbus_connection_set_route_peer_messages (connection, TRUE);
678 dbus_error_init (&error);
679 d->selinux_id = bus_selinux_init_connection_id (connection,
681 if (dbus_error_is_set (&error))
683 /* This is a bit bogus because we pretend all errors
684 * are OOM; this is done because we know that in bus.c
685 * an OOM error disconnects the connection, which is
686 * the same thing we want on any other error.
688 dbus_error_free (&error);
692 if (!dbus_connection_set_watch_functions (connection,
693 add_connection_watch,
694 remove_connection_watch,
695 toggle_connection_watch,
700 if (!dbus_connection_set_timeout_functions (connection,
701 add_connection_timeout,
702 remove_connection_timeout,
707 /* For now we don't need to set a Windows user function because
708 * there are no policies in the config file controlling what
709 * Windows users can connect. The default 'same user that owns the
710 * bus can connect' behavior of DBusConnection is fine on Windows.
712 dbus_connection_set_unix_user_function (connection,
713 allow_unix_user_function,
716 dbus_connection_set_dispatch_status_function (connection,
717 dispatch_status_function,
718 bus_context_get_loop (connections->context),
721 d->link_in_connection_list = _dbus_list_alloc_link (connection);
722 if (d->link_in_connection_list == NULL)
725 /* Setup the connection with the dispatcher */
726 if (!bus_dispatch_add_connection (connection))
729 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
731 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
733 bus_dispatch_remove_connection (connection);
738 /* Setup pending fds timeout (see #80559) */
739 d->pending_unix_fds_timeout = _dbus_timeout_new (100, /* irrelevant */
740 pending_unix_fds_timeout_cb,
742 if (d->pending_unix_fds_timeout == NULL)
745 _dbus_timeout_set_enabled (d->pending_unix_fds_timeout, FALSE);
746 if (!_dbus_loop_add_timeout (bus_context_get_loop (connections->context),
747 d->pending_unix_fds_timeout))
750 _dbus_connection_set_pending_fds_function (connection,
751 (DBusPendingFdsChangeFunction) check_pending_fds_cb,
754 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
755 connections->n_incomplete += 1;
757 dbus_connection_ref (connection);
759 bus_connections_expire_incomplete (connections);
761 /* The listening socket is removed from the main loop,
762 * i.e. does not accept(), while n_incomplete is at its
763 * maximum value; so we shouldn't get here in that case */
764 _dbus_assert (connections->n_incomplete <=
765 bus_context_get_max_incomplete_connections (connections->context));
767 /* If we have the maximum number of incomplete connections,
768 * stop accept()ing any more, to avert a DoS. See fd.o #80919 */
769 bus_context_check_all_watches (d->connections->context);
777 bus_selinux_id_unref (d->selinux_id);
778 d->selinux_id = NULL;
780 if (!dbus_connection_set_watch_functions (connection,
784 _dbus_assert_not_reached ("setting watch functions to NULL failed");
786 if (!dbus_connection_set_timeout_functions (connection,
790 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
792 dbus_connection_set_unix_user_function (connection,
795 dbus_connection_set_windows_user_function (connection,
798 dbus_connection_set_dispatch_status_function (connection,
801 if (d->pending_unix_fds_timeout)
802 _dbus_timeout_unref (d->pending_unix_fds_timeout);
804 d->pending_unix_fds_timeout = NULL;
806 _dbus_connection_set_pending_fds_function (connection, NULL, NULL);
808 if (d->link_in_connection_list != NULL)
810 _dbus_assert (d->link_in_connection_list->next == NULL);
811 _dbus_assert (d->link_in_connection_list->prev == NULL);
812 _dbus_list_free_link (d->link_in_connection_list);
813 d->link_in_connection_list = NULL;
816 if (!dbus_connection_set_data (connection,
817 connection_data_slot,
819 _dbus_assert_not_reached ("failed to set connection data to null");
821 /* "d" has now been freed */
828 bus_connections_expire_incomplete (BusConnections *connections)
834 if (connections->incomplete != NULL)
836 long tv_sec, tv_usec;
840 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
841 auth_timeout = bus_context_get_auth_timeout (connections->context);
843 link = _dbus_list_get_first_link (&connections->incomplete);
846 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
847 DBusConnection *connection;
848 BusConnectionData *d;
851 connection = link->data;
853 d = BUS_CONNECTION_DATA (connection);
855 _dbus_assert (d != NULL);
857 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
858 d->connection_tv_usec,
861 if (elapsed >= (double) auth_timeout)
863 /* Unfortunately, we can't identify the connection: it doesn't
864 * have a unique name yet, we don't know its uid/pid yet,
866 bus_context_log (connections->context, DBUS_SYSTEM_LOG_WARNING,
867 "Connection has not authenticated soon enough, closing it "
868 "(auth_timeout=%dms, elapsed: %.0fms)",
869 auth_timeout, elapsed);
871 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
872 dbus_connection_close (connection);
876 /* We can end the loop, since the connections are in oldest-first order */
877 next_interval = ((double)auth_timeout) - elapsed;
878 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
879 connection, next_interval);
888 bus_expire_timeout_set_interval (connections->expire_timeout,
893 expire_incomplete_timeout (void *data)
895 BusConnections *connections = data;
897 _dbus_verbose ("Running\n");
899 /* note that this may remove the timeout */
900 bus_connections_expire_incomplete (connections);
906 bus_connection_get_unix_groups (DBusConnection *connection,
907 unsigned long **groups,
916 if (dbus_connection_get_unix_user (connection, &uid))
918 if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
920 _dbus_verbose ("Did not get any groups for UID %lu\n",
926 _dbus_verbose ("Got %d groups for UID %lu\n",
932 return TRUE; /* successfully got 0 groups */
936 bus_connection_is_in_unix_group (DBusConnection *connection,
940 unsigned long *group_ids;
943 if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
948 while (i < n_group_ids)
950 if (group_ids[i] == gid)
952 dbus_free (group_ids);
958 dbus_free (group_ids);
963 bus_connection_get_loginfo (DBusConnection *connection)
965 BusConnectionData *d;
967 d = BUS_CONNECTION_DATA (connection);
969 if (!bus_connection_is_active (connection))
971 return d->cached_loginfo_string;
975 bus_connection_get_policy (DBusConnection *connection)
977 BusConnectionData *d;
979 d = BUS_CONNECTION_DATA (connection);
981 _dbus_assert (d != NULL);
982 _dbus_assert (d->policy != NULL);
988 foreach_active (BusConnections *connections,
989 BusConnectionForeachFunction function,
994 link = _dbus_list_get_first_link (&connections->completed);
997 DBusConnection *connection = link->data;
998 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1000 if (!(* function) (connection, data))
1010 foreach_inactive (BusConnections *connections,
1011 BusConnectionForeachFunction function,
1016 link = _dbus_list_get_first_link (&connections->incomplete);
1017 while (link != NULL)
1019 DBusConnection *connection = link->data;
1020 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
1022 if (!(* function) (connection, data))
1032 * Calls function on each active connection; if the function returns
1033 * #FALSE, stops iterating. Active connections are authenticated
1034 * and have sent a Hello message.
1036 * @param connections the connections object
1037 * @param function the function
1038 * @param data data to pass to it as a second arg
1041 bus_connections_foreach_active (BusConnections *connections,
1042 BusConnectionForeachFunction function,
1045 foreach_active (connections, function, data);
1049 * Calls function on each connection; if the function returns
1050 * #FALSE, stops iterating.
1052 * @param connections the connections object
1053 * @param function the function
1054 * @param data data to pass to it as a second arg
1057 bus_connections_foreach (BusConnections *connections,
1058 BusConnectionForeachFunction function,
1061 if (!foreach_active (connections, function, data))
1064 foreach_inactive (connections, function, data);
1068 bus_connections_get_context (BusConnections *connections)
1070 return connections->context;
1074 * This is used to avoid covering the same connection twice when
1075 * traversing connections. Note that it assumes we will
1076 * bus_connection_mark_stamp() each connection at least once per
1077 * INT_MAX increments of the global stamp, or wraparound would break
1081 bus_connections_increment_stamp (BusConnections *connections)
1083 connections->stamp += 1;
1086 /* Mark connection with current stamp, return TRUE if it
1087 * didn't already have that stamp
1090 bus_connection_mark_stamp (DBusConnection *connection)
1092 BusConnectionData *d;
1094 d = BUS_CONNECTION_DATA (connection);
1096 _dbus_assert (d != NULL);
1098 if (d->stamp == d->connections->stamp)
1102 d->stamp = d->connections->stamp;
1108 bus_connection_get_context (DBusConnection *connection)
1110 BusConnectionData *d;
1112 d = BUS_CONNECTION_DATA (connection);
1114 _dbus_assert (d != NULL);
1116 return d->connections->context;
1120 bus_connection_get_connections (DBusConnection *connection)
1122 BusConnectionData *d;
1124 d = BUS_CONNECTION_DATA (connection);
1126 _dbus_assert (d != NULL);
1128 return d->connections;
1132 bus_connection_get_registry (DBusConnection *connection)
1134 BusConnectionData *d;
1136 d = BUS_CONNECTION_DATA (connection);
1138 _dbus_assert (d != NULL);
1140 return bus_context_get_registry (d->connections->context);
1144 bus_connection_get_activation (DBusConnection *connection)
1146 BusConnectionData *d;
1148 d = BUS_CONNECTION_DATA (connection);
1150 _dbus_assert (d != NULL);
1152 return bus_context_get_activation (d->connections->context);
1156 bus_connection_get_matchmaker (DBusConnection *connection)
1158 BusConnectionData *d;
1160 d = BUS_CONNECTION_DATA (connection);
1162 _dbus_assert (d != NULL);
1164 return bus_context_get_matchmaker (d->connections->context);
1168 bus_connection_get_selinux_id (DBusConnection *connection)
1170 BusConnectionData *d;
1172 d = BUS_CONNECTION_DATA (connection);
1174 _dbus_assert (d != NULL);
1176 return d->selinux_id;
1180 * Checks whether the connection is registered with the message bus.
1182 * @param connection the connection
1183 * @returns #TRUE if we're an active message bus participant
1186 bus_connection_is_active (DBusConnection *connection)
1188 BusConnectionData *d;
1190 d = BUS_CONNECTION_DATA (connection);
1192 return d != NULL && d->name != NULL;
1196 bus_connection_preallocate_oom_error (DBusConnection *connection)
1198 DBusMessage *message;
1199 DBusPreallocatedSend *preallocated;
1200 BusConnectionData *d;
1202 d = BUS_CONNECTION_DATA (connection);
1204 _dbus_assert (d != NULL);
1206 if (d->oom_preallocated != NULL)
1209 preallocated = dbus_connection_preallocate_send (connection);
1210 if (preallocated == NULL)
1213 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1215 if (message == NULL)
1217 dbus_connection_free_preallocated_send (connection, preallocated);
1221 /* d->name may be NULL, but that is OK */
1222 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1223 !dbus_message_set_destination (message, d->name) ||
1224 !dbus_message_set_sender (message,
1227 dbus_connection_free_preallocated_send (connection, preallocated);
1228 dbus_message_unref (message);
1232 /* set reply serial to placeholder value just so space is already allocated
1235 if (!dbus_message_set_reply_serial (message, 14))
1237 dbus_connection_free_preallocated_send (connection, preallocated);
1238 dbus_message_unref (message);
1242 d->oom_message = message;
1243 d->oom_preallocated = preallocated;
1249 bus_connection_send_oom_error (DBusConnection *connection,
1250 DBusMessage *in_reply_to)
1252 BusConnectionData *d;
1254 d = BUS_CONNECTION_DATA (connection);
1256 _dbus_assert (d != NULL);
1257 _dbus_assert (d->oom_message != NULL);
1259 /* should always succeed since we set it to a placeholder earlier */
1260 if (!dbus_message_set_reply_serial (d->oom_message,
1261 dbus_message_get_serial (in_reply_to)))
1262 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1264 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1266 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1267 d->oom_message, NULL);
1269 dbus_message_unref (d->oom_message);
1270 d->oom_message = NULL;
1271 d->oom_preallocated = NULL;
1274 #ifdef DBUS_ENABLE_STATS
1276 update_peak (int *peak,
1285 bus_connection_add_match_rule_link (DBusConnection *connection,
1288 BusConnectionData *d;
1290 d = BUS_CONNECTION_DATA (connection);
1291 _dbus_assert (d != NULL);
1293 _dbus_list_append_link (&d->match_rules, link);
1295 d->n_match_rules += 1;
1297 #ifdef DBUS_ENABLE_STATS
1298 update_peak (&d->peak_match_rules, d->n_match_rules);
1299 update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1301 d->connections->total_match_rules += 1;
1302 update_peak (&d->connections->peak_match_rules,
1303 d->connections->total_match_rules);
1308 bus_connection_add_match_rule (DBusConnection *connection,
1313 link = _dbus_list_alloc_link (rule);
1318 bus_connection_add_match_rule_link (connection, link);
1324 bus_connection_remove_match_rule (DBusConnection *connection,
1327 BusConnectionData *d;
1329 d = BUS_CONNECTION_DATA (connection);
1330 _dbus_assert (d != NULL);
1332 _dbus_list_remove_last (&d->match_rules, rule);
1334 d->n_match_rules -= 1;
1335 _dbus_assert (d->n_match_rules >= 0);
1337 #ifdef DBUS_ENABLE_STATS
1338 d->connections->total_match_rules -= 1;
1343 bus_connection_get_n_match_rules (DBusConnection *connection)
1345 BusConnectionData *d;
1347 d = BUS_CONNECTION_DATA (connection);
1348 _dbus_assert (d != NULL);
1350 return d->n_match_rules;
1354 bus_connection_add_owned_service_link (DBusConnection *connection,
1357 BusConnectionData *d;
1359 d = BUS_CONNECTION_DATA (connection);
1360 _dbus_assert (d != NULL);
1362 _dbus_list_append_link (&d->services_owned, link);
1364 d->n_services_owned += 1;
1366 #ifdef DBUS_ENABLE_STATS
1367 update_peak (&d->peak_bus_names, d->n_services_owned);
1368 update_peak (&d->connections->peak_bus_names_per_conn,
1369 d->n_services_owned);
1371 d->connections->total_bus_names += 1;
1372 update_peak (&d->connections->peak_bus_names,
1373 d->connections->total_bus_names);
1378 bus_connection_add_owned_service (DBusConnection *connection,
1379 BusService *service)
1383 link = _dbus_list_alloc_link (service);
1388 bus_connection_add_owned_service_link (connection, link);
1394 bus_connection_remove_owned_service (DBusConnection *connection,
1395 BusService *service)
1397 BusConnectionData *d;
1399 d = BUS_CONNECTION_DATA (connection);
1400 _dbus_assert (d != NULL);
1402 _dbus_list_remove_last (&d->services_owned, service);
1404 d->n_services_owned -= 1;
1405 _dbus_assert (d->n_services_owned >= 0);
1407 #ifdef DBUS_ENABLE_STATS
1408 d->connections->total_bus_names -= 1;
1413 bus_connection_get_n_services_owned (DBusConnection *connection)
1415 BusConnectionData *d;
1417 d = BUS_CONNECTION_DATA (connection);
1418 _dbus_assert (d != NULL);
1420 return d->n_services_owned;
1424 bus_connection_complete (DBusConnection *connection,
1425 const DBusString *name,
1428 BusConnectionData *d;
1431 d = BUS_CONNECTION_DATA (connection);
1432 _dbus_assert (d != NULL);
1433 _dbus_assert (d->name == NULL);
1434 _dbus_assert (d->policy == NULL);
1436 _dbus_assert (!bus_connection_is_active (connection));
1438 if (!_dbus_string_copy_data (name, &d->name))
1440 BUS_SET_OOM (error);
1444 _dbus_assert (d->name != NULL);
1446 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1448 d->policy = bus_context_create_client_policy (d->connections->context,
1452 /* we may have a NULL policy on OOM or error getting list of
1453 * groups for a user. In the latter case we don't handle it so
1454 * well currently, as it will just keep failing over and over.
1457 if (d->policy == NULL)
1459 _dbus_verbose ("Failed to create security policy for connection %p\n",
1461 _DBUS_ASSERT_ERROR_IS_SET (error);
1462 dbus_free (d->name);
1467 if (dbus_connection_get_unix_user (connection, &uid))
1469 if (!adjust_connections_for_uid (d->connections,
1474 /* Create and cache a string which holds information about the
1475 * peer process; used for logging purposes.
1477 if (!cache_peer_loginfo_string (d, connection))
1480 /* Now the connection is active, move it between lists */
1481 _dbus_list_unlink (&d->connections->incomplete,
1482 d->link_in_connection_list);
1483 d->connections->n_incomplete -= 1;
1484 _dbus_list_append_link (&d->connections->completed,
1485 d->link_in_connection_list);
1486 d->connections->n_completed += 1;
1488 _dbus_assert (d->connections->n_incomplete >= 0);
1489 _dbus_assert (d->connections->n_completed > 0);
1491 /* If we have dropped below the max. number of incomplete
1492 * connections, start accept()ing again */
1493 bus_context_check_all_watches (d->connections->context);
1495 /* See if we can remove the timeout */
1496 bus_connections_expire_incomplete (d->connections);
1498 _dbus_assert (bus_connection_is_active (connection));
1502 BUS_SET_OOM (error);
1503 dbus_free (d->name);
1506 bus_client_policy_unref (d->policy);
1512 bus_connections_reload_policy (BusConnections *connections,
1515 BusConnectionData *d;
1516 DBusConnection *connection;
1519 _dbus_assert (connections != NULL);
1520 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1522 for (link = _dbus_list_get_first_link (&(connections->completed));
1524 link = _dbus_list_get_next_link (&(connections->completed), link))
1526 connection = link->data;
1527 d = BUS_CONNECTION_DATA (connection);
1528 _dbus_assert (d != NULL);
1529 _dbus_assert (d->policy != NULL);
1531 bus_client_policy_unref (d->policy);
1532 d->policy = bus_context_create_client_policy (connections->context,
1535 if (d->policy == NULL)
1537 _dbus_verbose ("Failed to create security policy for connection %p\n",
1539 _DBUS_ASSERT_ERROR_IS_SET (error);
1548 bus_connection_get_name (DBusConnection *connection)
1550 BusConnectionData *d;
1552 d = BUS_CONNECTION_DATA (connection);
1553 _dbus_assert (d != NULL);
1559 * Check whether completing the passed-in connection would
1560 * exceed limits, and if so set error and return #FALSE
1563 bus_connections_check_limits (BusConnections *connections,
1564 DBusConnection *requesting_completion,
1569 if (connections->n_completed >=
1570 bus_context_get_max_completed_connections (connections->context))
1572 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1573 "The maximum number of active connections has been reached");
1577 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1579 if (get_connections_for_uid (connections, uid) >=
1580 bus_context_get_max_connections_per_user (connections->context))
1582 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1583 "The maximum number of active connections for UID %lu has been reached",
1593 bus_pending_reply_free (BusPendingReply *pending)
1595 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1597 pending->will_send_reply,
1598 pending->will_get_reply,
1599 pending->reply_serial);
1601 dbus_free (pending);
1605 bus_pending_reply_send_no_reply (BusConnections *connections,
1606 BusTransaction *transaction,
1607 BusPendingReply *pending)
1609 DBusMessage *message;
1610 DBusMessageIter iter;
1616 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1617 if (message == NULL)
1620 dbus_message_set_no_reply (message, TRUE);
1622 if (!dbus_message_set_reply_serial (message,
1623 pending->reply_serial))
1626 if (!dbus_message_set_error_name (message,
1627 DBUS_ERROR_NO_REPLY))
1630 /* If you change these messages, adjust test/dbus-daemon.c to match */
1631 if (pending->will_send_reply == NULL)
1632 errmsg = "Message recipient disconnected from message bus without replying";
1634 errmsg = "Message did not receive a reply (timeout by message bus)";
1636 dbus_message_iter_init_append (message, &iter);
1637 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1640 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1647 dbus_message_unref (message);
1652 bus_pending_reply_expired (BusExpireList *list,
1656 BusPendingReply *pending = link->data;
1657 BusConnections *connections = data;
1658 BusTransaction *transaction;
1660 /* No reply is forthcoming. So nuke it if we can. If not,
1661 * leave it in the list to try expiring again later when we
1665 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1667 pending->will_send_reply,
1668 pending->will_get_reply,
1669 pending->reply_serial);
1671 transaction = bus_transaction_new (connections->context);
1672 if (transaction == NULL)
1675 if (!bus_pending_reply_send_no_reply (connections,
1679 bus_transaction_cancel_and_free (transaction);
1683 bus_expire_list_remove_link (connections->pending_replies, link);
1685 bus_pending_reply_free (pending);
1686 bus_transaction_execute_and_free (transaction);
1692 bus_connection_drop_pending_replies (BusConnections *connections,
1693 DBusConnection *connection)
1695 /* The DBusConnection is almost 100% finalized here, so you can't
1696 * do anything with it except check for pointer equality
1700 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1703 link = bus_expire_list_get_first_link (connections->pending_replies);
1704 while (link != NULL)
1707 BusPendingReply *pending;
1709 next = bus_expire_list_get_next_link (connections->pending_replies,
1711 pending = link->data;
1713 if (pending->will_get_reply == connection)
1715 /* We don't need to track this pending reply anymore */
1717 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1719 pending->will_send_reply,
1720 pending->will_get_reply,
1721 pending->reply_serial);
1723 bus_expire_list_remove_link (connections->pending_replies,
1725 bus_pending_reply_free (pending);
1727 else if (pending->will_send_reply == connection)
1729 /* The reply isn't going to be sent, so set things
1730 * up so it will be expired right away
1732 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1734 pending->will_send_reply,
1735 pending->will_get_reply,
1736 pending->reply_serial);
1738 pending->will_send_reply = NULL;
1739 pending->expire_item.added_tv_sec = 0;
1740 pending->expire_item.added_tv_usec = 0;
1742 bus_expire_list_recheck_immediately (connections->pending_replies);
1752 BusPendingReply *pending;
1753 BusConnections *connections;
1754 } CancelPendingReplyData;
1757 cancel_pending_reply (void *data)
1759 CancelPendingReplyData *d = data;
1761 _dbus_verbose ("d = %p\n", d);
1763 if (!bus_expire_list_remove (d->connections->pending_replies,
1764 &d->pending->expire_item))
1765 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1767 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1771 cancel_pending_reply_data_free (void *data)
1773 CancelPendingReplyData *d = data;
1775 _dbus_verbose ("d = %p\n", d);
1777 /* d->pending should be either freed or still
1778 * in the list of pending replies (owned by someone
1786 * Record that a reply is allowed; return TRUE on success.
1789 bus_connections_expect_reply (BusConnections *connections,
1790 BusTransaction *transaction,
1791 DBusConnection *will_get_reply,
1792 DBusConnection *will_send_reply,
1793 DBusMessage *reply_to_this,
1796 BusPendingReply *pending;
1797 dbus_uint32_t reply_serial;
1799 CancelPendingReplyData *cprd;
1802 _dbus_assert (will_get_reply != NULL);
1803 _dbus_assert (will_send_reply != NULL);
1804 _dbus_assert (reply_to_this != NULL);
1806 if (dbus_message_get_no_reply (reply_to_this))
1807 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1809 reply_serial = dbus_message_get_serial (reply_to_this);
1811 link = bus_expire_list_get_first_link (connections->pending_replies);
1813 while (link != NULL)
1815 pending = link->data;
1817 if (pending->reply_serial == reply_serial &&
1818 pending->will_get_reply == will_get_reply &&
1819 pending->will_send_reply == will_send_reply)
1821 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1822 "Message has the same reply serial as a currently-outstanding existing method call");
1826 link = bus_expire_list_get_next_link (connections->pending_replies,
1828 if (pending->will_get_reply == will_get_reply)
1833 bus_context_get_max_replies_per_connection (connections->context))
1835 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1836 "The maximum number of pending replies per connection has been reached");
1840 pending = dbus_new0 (BusPendingReply, 1);
1841 if (pending == NULL)
1843 BUS_SET_OOM (error);
1847 #ifdef DBUS_ENABLE_VERBOSE_MODE
1848 /* so we can see a not-yet-added pending reply */
1849 pending->expire_item.added_tv_sec = 1;
1850 pending->expire_item.added_tv_usec = 1;
1853 pending->will_get_reply = will_get_reply;
1854 pending->will_send_reply = will_send_reply;
1855 pending->reply_serial = reply_serial;
1857 cprd = dbus_new0 (CancelPendingReplyData, 1);
1860 BUS_SET_OOM (error);
1861 bus_pending_reply_free (pending);
1865 if (!bus_expire_list_add (connections->pending_replies,
1866 &pending->expire_item))
1868 BUS_SET_OOM (error);
1870 bus_pending_reply_free (pending);
1874 if (!bus_transaction_add_cancel_hook (transaction,
1875 cancel_pending_reply,
1877 cancel_pending_reply_data_free))
1879 BUS_SET_OOM (error);
1880 bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1882 bus_pending_reply_free (pending);
1886 cprd->pending = pending;
1887 cprd->connections = connections;
1889 _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1890 &pending->expire_item.added_tv_usec);
1892 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1894 pending->will_send_reply,
1895 pending->will_get_reply,
1896 pending->reply_serial);
1904 BusConnections *connections;
1905 } CheckPendingReplyData;
1908 cancel_check_pending_reply (void *data)
1910 CheckPendingReplyData *d = data;
1912 _dbus_verbose ("d = %p\n",d);
1914 bus_expire_list_add_link (d->connections->pending_replies,
1920 check_pending_reply_data_free (void *data)
1922 CheckPendingReplyData *d = data;
1924 _dbus_verbose ("d = %p\n",d);
1926 if (d->link != NULL)
1928 BusPendingReply *pending = d->link->data;
1930 _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1931 &pending->expire_item));
1933 bus_pending_reply_free (pending);
1934 _dbus_list_free_link (d->link);
1941 * Check whether a reply is allowed, remove BusPendingReply
1942 * if so, return TRUE if so.
1945 bus_connections_check_reply (BusConnections *connections,
1946 BusTransaction *transaction,
1947 DBusConnection *sending_reply,
1948 DBusConnection *receiving_reply,
1952 CheckPendingReplyData *cprd;
1954 dbus_uint32_t reply_serial;
1956 _dbus_assert (sending_reply != NULL);
1957 _dbus_assert (receiving_reply != NULL);
1959 reply_serial = dbus_message_get_reply_serial (reply);
1961 link = bus_expire_list_get_first_link (connections->pending_replies);
1962 while (link != NULL)
1964 BusPendingReply *pending = link->data;
1966 if (pending->reply_serial == reply_serial &&
1967 pending->will_get_reply == receiving_reply &&
1968 pending->will_send_reply == sending_reply)
1970 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1974 link = bus_expire_list_get_next_link (connections->pending_replies,
1980 _dbus_verbose ("No pending reply expected\n");
1985 cprd = dbus_new0 (CheckPendingReplyData, 1);
1988 BUS_SET_OOM (error);
1992 if (!bus_transaction_add_cancel_hook (transaction,
1993 cancel_check_pending_reply,
1995 check_pending_reply_data_free))
1997 BUS_SET_OOM (error);
2003 cprd->connections = connections;
2005 bus_expire_list_unlink (connections->pending_replies,
2008 _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
2016 * Note that this is fairly fragile; in particular, don't try to use
2017 * one transaction across any main loop iterations.
2022 BusTransaction *transaction;
2023 DBusMessage *message;
2024 DBusPreallocatedSend *preallocated;
2029 BusTransactionCancelFunction cancel_function;
2030 DBusFreeFunction free_data_function;
2034 struct BusTransaction
2036 DBusList *connections;
2037 BusContext *context;
2038 DBusList *cancel_hooks;
2042 message_to_send_free (DBusConnection *connection,
2043 MessageToSend *to_send)
2045 if (to_send->message)
2046 dbus_message_unref (to_send->message);
2048 if (to_send->preallocated)
2049 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
2051 dbus_free (to_send);
2055 cancel_hook_cancel (void *element,
2058 CancelHook *ch = element;
2060 _dbus_verbose ("Running transaction cancel hook\n");
2062 if (ch->cancel_function)
2063 (* ch->cancel_function) (ch->data);
2067 cancel_hook_free (void *element,
2070 CancelHook *ch = element;
2072 if (ch->free_data_function)
2073 (* ch->free_data_function) (ch->data);
2079 free_cancel_hooks (BusTransaction *transaction)
2081 _dbus_list_foreach (&transaction->cancel_hooks,
2082 cancel_hook_free, NULL);
2084 _dbus_list_clear (&transaction->cancel_hooks);
2088 bus_transaction_new (BusContext *context)
2090 BusTransaction *transaction;
2092 transaction = dbus_new0 (BusTransaction, 1);
2093 if (transaction == NULL)
2096 transaction->context = context;
2102 bus_transaction_get_context (BusTransaction *transaction)
2104 return transaction->context;
2108 bus_transaction_send_from_driver (BusTransaction *transaction,
2109 DBusConnection *connection,
2110 DBusMessage *message)
2112 /* We have to set the sender to the driver, and have
2113 * to check security policy since it was not done in
2116 _dbus_verbose ("Sending %s %s %s from driver\n",
2117 dbus_message_get_interface (message) ?
2118 dbus_message_get_interface (message) : "(no interface)",
2119 dbus_message_get_member (message) ?
2120 dbus_message_get_member (message) : "(no member)",
2121 dbus_message_get_error_name (message) ?
2122 dbus_message_get_error_name (message) : "(no error name)");
2124 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2127 if (bus_connection_is_active (connection))
2129 if (!dbus_message_set_destination (message,
2130 bus_connection_get_name (connection)))
2134 /* bus driver never wants a reply */
2135 dbus_message_set_no_reply (message, TRUE);
2137 /* If security policy doesn't allow the message, we silently
2138 * eat it; the driver doesn't care about getting a reply.
2140 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2142 NULL, connection, connection, message, NULL))
2145 return bus_transaction_send (transaction, connection, message);
2149 bus_transaction_send (BusTransaction *transaction,
2150 DBusConnection *connection,
2151 DBusMessage *message)
2153 MessageToSend *to_send;
2154 BusConnectionData *d;
2157 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2158 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2159 dbus_message_get_reply_serial (message) != 0 ? "reply" :
2161 dbus_message_get_interface (message) ?
2162 dbus_message_get_interface (message) : "(unset)",
2163 dbus_message_get_member (message) ?
2164 dbus_message_get_member (message) : "(unset)",
2165 dbus_message_get_error_name (message) ?
2166 dbus_message_get_error_name (message) : "(unset)",
2167 dbus_connection_get_is_connected (connection) ?
2168 "" : " (disconnected)");
2170 _dbus_assert (dbus_message_get_sender (message) != NULL);
2172 if (!dbus_connection_get_is_connected (connection))
2173 return TRUE; /* silently ignore disconnected connections */
2175 d = BUS_CONNECTION_DATA (connection);
2176 _dbus_assert (d != NULL);
2178 to_send = dbus_new (MessageToSend, 1);
2179 if (to_send == NULL)
2184 to_send->preallocated = dbus_connection_preallocate_send (connection);
2185 if (to_send->preallocated == NULL)
2187 dbus_free (to_send);
2191 dbus_message_ref (message);
2192 to_send->message = message;
2193 to_send->transaction = transaction;
2195 _dbus_verbose ("about to prepend message\n");
2197 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2199 message_to_send_free (connection, to_send);
2203 _dbus_verbose ("prepended message\n");
2205 /* See if we already had this connection in the list
2206 * for this transaction. If we have a pending message,
2207 * then we should already be in transaction->connections
2209 link = _dbus_list_get_first_link (&d->transaction_messages);
2210 _dbus_assert (link->data == to_send);
2211 link = _dbus_list_get_next_link (&d->transaction_messages, link);
2212 while (link != NULL)
2214 MessageToSend *m = link->data;
2215 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2217 if (m->transaction == transaction)
2225 if (!_dbus_list_prepend (&transaction->connections, connection))
2227 _dbus_list_remove (&d->transaction_messages, to_send);
2228 message_to_send_free (connection, to_send);
2237 transaction_free (BusTransaction *transaction)
2239 _dbus_assert (transaction->connections == NULL);
2241 free_cancel_hooks (transaction);
2243 dbus_free (transaction);
2247 connection_cancel_transaction (DBusConnection *connection,
2248 BusTransaction *transaction)
2251 BusConnectionData *d;
2253 d = BUS_CONNECTION_DATA (connection);
2254 _dbus_assert (d != NULL);
2256 link = _dbus_list_get_first_link (&d->transaction_messages);
2257 while (link != NULL)
2259 MessageToSend *m = link->data;
2260 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2262 if (m->transaction == transaction)
2264 _dbus_list_remove_link (&d->transaction_messages,
2267 message_to_send_free (connection, m);
2275 bus_transaction_cancel_and_free (BusTransaction *transaction)
2277 DBusConnection *connection;
2279 _dbus_verbose ("TRANSACTION: cancelled\n");
2281 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2282 connection_cancel_transaction (connection, transaction);
2284 _dbus_list_foreach (&transaction->cancel_hooks,
2285 cancel_hook_cancel, NULL);
2287 transaction_free (transaction);
2291 connection_execute_transaction (DBusConnection *connection,
2292 BusTransaction *transaction)
2295 BusConnectionData *d;
2297 d = BUS_CONNECTION_DATA (connection);
2298 _dbus_assert (d != NULL);
2300 /* Send the queue in order (FIFO) */
2301 link = _dbus_list_get_last_link (&d->transaction_messages);
2302 while (link != NULL)
2304 MessageToSend *m = link->data;
2305 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2307 if (m->transaction == transaction)
2309 _dbus_list_remove_link (&d->transaction_messages,
2312 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2314 dbus_connection_send_preallocated (connection,
2319 m->preallocated = NULL; /* so we don't double-free it */
2321 message_to_send_free (connection, m);
2329 bus_transaction_execute_and_free (BusTransaction *transaction)
2331 /* For each connection in transaction->connections
2334 DBusConnection *connection;
2336 _dbus_verbose ("TRANSACTION: executing\n");
2338 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2339 connection_execute_transaction (connection, transaction);
2341 transaction_free (transaction);
2345 bus_connection_remove_transactions (DBusConnection *connection)
2347 MessageToSend *to_send;
2348 BusConnectionData *d;
2350 d = BUS_CONNECTION_DATA (connection);
2351 _dbus_assert (d != NULL);
2353 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2355 /* only has an effect for the first MessageToSend listing this transaction */
2356 _dbus_list_remove (&to_send->transaction->connections,
2359 _dbus_list_remove (&d->transaction_messages, to_send);
2360 message_to_send_free (connection, to_send);
2365 * Converts the DBusError to a message reply
2368 bus_transaction_send_error_reply (BusTransaction *transaction,
2369 DBusConnection *connection,
2370 const DBusError *error,
2371 DBusMessage *in_reply_to)
2375 _dbus_assert (error != NULL);
2376 _DBUS_ASSERT_ERROR_IS_SET (error);
2378 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2379 error->name, error->message);
2381 reply = dbus_message_new_error (in_reply_to,
2387 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2389 dbus_message_unref (reply);
2393 dbus_message_unref (reply);
2399 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2400 BusTransactionCancelFunction cancel_function,
2402 DBusFreeFunction free_data_function)
2406 ch = dbus_new (CancelHook, 1);
2410 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2411 cancel_function, data);
2413 ch->cancel_function = cancel_function;
2415 ch->free_data_function = free_data_function;
2417 /* It's important that the hooks get run in reverse order that they
2420 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2430 bus_connections_get_n_active (BusConnections *connections)
2432 return connections->n_completed;
2436 bus_connections_get_n_incomplete (BusConnections *connections)
2438 return connections->n_incomplete;
2441 #ifdef DBUS_ENABLE_STATS
2443 bus_connections_get_total_match_rules (BusConnections *connections)
2445 return connections->total_match_rules;
2449 bus_connections_get_peak_match_rules (BusConnections *connections)
2451 return connections->peak_match_rules;
2455 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2457 return connections->peak_match_rules_per_conn;
2461 bus_connections_get_total_bus_names (BusConnections *connections)
2463 return connections->total_bus_names;
2467 bus_connections_get_peak_bus_names (BusConnections *connections)
2469 return connections->peak_bus_names;
2473 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2475 return connections->peak_bus_names_per_conn;
2479 bus_connection_get_peak_match_rules (DBusConnection *connection)
2481 BusConnectionData *d;
2483 d = BUS_CONNECTION_DATA (connection);
2484 return d->peak_match_rules;
2488 bus_connection_get_peak_bus_names (DBusConnection *connection)
2490 BusConnectionData *d;
2492 d = BUS_CONNECTION_DATA (connection);
2493 return d->peak_bus_names;
2495 #endif /* DBUS_ENABLE_STATS */