1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "connection.h"
29 #include "expirelist.h"
30 #include <dbus/dbus-list.h>
31 #include <dbus/dbus-hash.h>
32 #include <dbus/dbus-timeout.h>
34 static void bus_connection_remove_transactions (DBusConnection *connection);
38 BusExpireItem expire_item;
40 DBusConnection *will_get_reply;
41 DBusConnection *will_send_reply;
43 dbus_uint32_t reply_serial;
50 DBusList *completed; /**< List of all completed connections */
51 int n_completed; /**< Length of completed list */
52 DBusList *incomplete; /**< List of all not-yet-active connections */
53 int n_incomplete; /**< Length of incomplete list */
55 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
56 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
57 int stamp; /**< Incrementing number */
60 static dbus_int32_t connection_data_slot = -1;
64 BusConnections *connections;
65 DBusList *link_in_connection_list;
66 DBusConnection *connection;
67 DBusList *services_owned;
69 DBusList *match_rules;
72 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
73 DBusMessage *oom_message;
74 DBusPreallocatedSend *oom_preallocated;
75 BusClientPolicy *policy;
77 long connection_tv_sec; /**< Time when we connected (seconds component) */
78 long connection_tv_usec; /**< Time when we connected (microsec component) */
79 int stamp; /**< connections->stamp last time we were traversed */
82 static dbus_bool_t expire_incomplete_timeout (void *data);
84 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
87 connection_get_loop (DBusConnection *connection)
91 d = BUS_CONNECTION_DATA (connection);
93 return bus_context_get_loop (d->connections->context);
98 get_connections_for_uid (BusConnections *connections,
104 /* val is NULL is 0 when it isn't in the hash yet */
106 val = _dbus_hash_table_lookup_ulong (connections->completed_by_user,
109 current_count = _DBUS_POINTER_TO_INT (val);
111 return current_count;
115 adjust_connections_for_uid (BusConnections *connections,
121 current_count = get_connections_for_uid (connections, uid);
123 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
124 ": was %d adjustment %d making %d\n",
125 uid, current_count, adjustment, current_count + adjustment);
127 _dbus_assert (current_count >= 0);
129 current_count += adjustment;
131 _dbus_assert (current_count >= 0);
133 if (current_count == 0)
135 _dbus_hash_table_remove_ulong (connections->completed_by_user, uid);
142 retval = _dbus_hash_table_insert_ulong (connections->completed_by_user,
143 uid, _DBUS_INT_TO_POINTER (current_count));
145 /* only positive adjustment can fail as otherwise
146 * a hash entry should already exist
148 _dbus_assert (adjustment > 0 ||
149 (adjustment <= 0 && retval));
156 bus_connection_disconnected (DBusConnection *connection)
158 BusConnectionData *d;
160 BusMatchmaker *matchmaker;
162 d = BUS_CONNECTION_DATA (connection);
163 _dbus_assert (d != NULL);
165 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
166 d->name ? d->name : "(inactive)");
168 /* Delete our match rules */
169 if (d->n_match_rules > 0)
171 matchmaker = bus_context_get_matchmaker (d->connections->context);
172 bus_matchmaker_disconnected (matchmaker, connection);
175 /* Drop any service ownership. FIXME Unfortunately, this requires
176 * memory allocation and there doesn't seem to be a good way to
177 * handle it other than sleeping; we can't "fail" the operation of
178 * disconnecting a client, and preallocating a broadcast "service is
179 * now gone" message for every client-service pair seems kind of
180 * involved. Probably we need to do that though.
182 while ((service = _dbus_list_get_last (&d->services_owned)))
184 BusTransaction *transaction;
189 dbus_error_init (&error);
192 while (transaction == NULL)
194 transaction = bus_transaction_new (d->connections->context);
195 _dbus_wait_for_memory ();
198 if (!bus_service_remove_owner (service, connection,
199 transaction, &error))
201 _DBUS_ASSERT_ERROR_IS_SET (&error);
203 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
205 dbus_error_free (&error);
206 bus_transaction_cancel_and_free (transaction);
207 _dbus_wait_for_memory ();
212 _dbus_verbose ("Failed to remove service owner: %s %s\n",
213 error.name, error.message);
214 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
218 bus_transaction_execute_and_free (transaction);
221 bus_dispatch_remove_connection (connection);
223 /* no more watching */
224 if (!dbus_connection_set_watch_functions (connection,
228 _dbus_assert_not_reached ("setting watch functions to NULL failed");
230 if (!dbus_connection_set_timeout_functions (connection,
234 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
236 dbus_connection_set_unix_user_function (connection,
239 dbus_connection_set_dispatch_status_function (connection,
242 bus_connection_remove_transactions (connection);
244 if (d->link_in_connection_list != NULL)
250 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
251 d->link_in_connection_list = NULL;
252 d->connections->n_completed -= 1;
254 if (dbus_connection_get_unix_user (connection, &uid))
256 if (!adjust_connections_for_uid (d->connections,
258 _dbus_assert_not_reached ("adjusting downward should never fail");
263 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
264 d->link_in_connection_list = NULL;
265 d->connections->n_incomplete -= 1;
268 _dbus_assert (d->connections->n_incomplete >= 0);
269 _dbus_assert (d->connections->n_completed >= 0);
272 /* frees "d" as side effect */
273 dbus_connection_set_data (connection,
274 connection_data_slot,
277 dbus_connection_unref (connection);
281 connection_watch_callback (DBusWatch *watch,
282 unsigned int condition,
285 /* FIXME this can be done in dbus-mainloop.c
286 * if the code in activation.c for the babysitter
287 * watch handler is fixed.
291 _dbus_verbose ("Calling handle_watch\n");
293 return dbus_watch_handle (watch, condition);
297 add_connection_watch (DBusWatch *watch,
300 DBusConnection *connection = data;
302 return _dbus_loop_add_watch (connection_get_loop (connection),
303 watch, connection_watch_callback, connection,
308 remove_connection_watch (DBusWatch *watch,
311 DBusConnection *connection = data;
313 _dbus_loop_remove_watch (connection_get_loop (connection),
314 watch, connection_watch_callback, connection);
318 connection_timeout_callback (DBusTimeout *timeout,
321 /* DBusConnection *connection = data; */
323 /* can return FALSE on OOM but we just let it fire again later */
324 dbus_timeout_handle (timeout);
328 add_connection_timeout (DBusTimeout *timeout,
331 DBusConnection *connection = data;
333 return _dbus_loop_add_timeout (connection_get_loop (connection),
334 timeout, connection_timeout_callback, connection, NULL);
338 remove_connection_timeout (DBusTimeout *timeout,
341 DBusConnection *connection = data;
343 _dbus_loop_remove_timeout (connection_get_loop (connection),
344 timeout, connection_timeout_callback, connection);
348 dispatch_status_function (DBusConnection *connection,
349 DBusDispatchStatus new_status,
352 DBusLoop *loop = data;
354 if (new_status != DBUS_DISPATCH_COMPLETE)
356 while (!_dbus_loop_queue_dispatch (loop, connection))
357 _dbus_wait_for_memory ();
362 allow_user_function (DBusConnection *connection,
366 BusConnectionData *d;
368 d = BUS_CONNECTION_DATA (connection);
370 _dbus_assert (d != NULL);
372 return bus_context_allow_user (d->connections->context, uid);
376 free_connection_data (void *data)
378 BusConnectionData *d = data;
380 /* services_owned should be NULL since we should be disconnected */
381 _dbus_assert (d->services_owned == NULL);
382 _dbus_assert (d->n_services_owned == 0);
384 _dbus_assert (d->transaction_messages == NULL);
386 if (d->oom_preallocated)
387 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
390 dbus_message_unref (d->oom_message);
393 bus_client_policy_unref (d->policy);
401 call_timeout_callback (DBusTimeout *timeout,
404 /* can return FALSE on OOM but we just let it fire again later */
405 dbus_timeout_handle (timeout);
409 bus_connections_new (BusContext *context)
411 BusConnections *connections;
413 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
416 connections = dbus_new0 (BusConnections, 1);
417 if (connections == NULL)
420 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_ULONG,
422 if (connections->completed_by_user == NULL)
425 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
426 expire_incomplete_timeout,
428 if (connections->expire_timeout == NULL)
431 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
433 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
434 connections->expire_timeout,
435 call_timeout_callback, NULL, NULL))
438 connections->refcount = 1;
439 connections->context = context;
444 _dbus_timeout_unref (connections->expire_timeout);
446 _dbus_hash_table_unref (connections->completed_by_user);
448 dbus_free (connections);
450 dbus_connection_free_data_slot (&connection_data_slot);
456 bus_connections_ref (BusConnections *connections)
458 _dbus_assert (connections->refcount > 0);
459 connections->refcount += 1;
463 bus_connections_unref (BusConnections *connections)
465 _dbus_assert (connections->refcount > 0);
466 connections->refcount -= 1;
467 if (connections->refcount == 0)
469 /* drop all incomplete */
470 while (connections->incomplete != NULL)
472 DBusConnection *connection;
474 connection = connections->incomplete->data;
476 dbus_connection_ref (connection);
477 dbus_connection_disconnect (connection);
478 bus_connection_disconnected (connection);
479 dbus_connection_unref (connection);
482 _dbus_assert (connections->n_incomplete == 0);
484 /* drop all real connections */
485 while (connections->completed != NULL)
487 DBusConnection *connection;
489 connection = connections->completed->data;
491 dbus_connection_ref (connection);
492 dbus_connection_disconnect (connection);
493 bus_connection_disconnected (connection);
494 dbus_connection_unref (connection);
497 _dbus_assert (connections->n_completed == 0);
499 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
500 connections->expire_timeout,
501 call_timeout_callback, NULL);
503 _dbus_timeout_unref (connections->expire_timeout);
505 _dbus_hash_table_unref (connections->completed_by_user);
507 dbus_free (connections);
509 dbus_connection_free_data_slot (&connection_data_slot);
514 bus_connections_setup_connection (BusConnections *connections,
515 DBusConnection *connection)
517 BusConnectionData *d;
520 d = dbus_new0 (BusConnectionData, 1);
525 d->connections = connections;
526 d->connection = connection;
528 _dbus_get_current_time (&d->connection_tv_sec,
529 &d->connection_tv_usec);
531 _dbus_assert (connection_data_slot >= 0);
533 if (!dbus_connection_set_data (connection,
534 connection_data_slot,
535 d, free_connection_data))
543 if (!dbus_connection_set_watch_functions (connection,
544 add_connection_watch,
545 remove_connection_watch,
551 if (!dbus_connection_set_timeout_functions (connection,
552 add_connection_timeout,
553 remove_connection_timeout,
558 dbus_connection_set_unix_user_function (connection,
562 dbus_connection_set_dispatch_status_function (connection,
563 dispatch_status_function,
564 bus_context_get_loop (connections->context),
567 d->link_in_connection_list = _dbus_list_alloc_link (connection);
568 if (d->link_in_connection_list == NULL)
571 /* Setup the connection with the dispatcher */
572 if (!bus_dispatch_add_connection (connection))
575 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
577 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
579 bus_dispatch_remove_connection (connection);
584 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
585 connections->n_incomplete += 1;
587 dbus_connection_ref (connection);
589 /* Note that we might disconnect ourselves here, but it only takes
590 * effect on return to the main loop. We call this to free up
591 * expired connections if possible, and to queue the timeout for our
594 bus_connections_expire_incomplete (connections);
596 /* And we might also disconnect ourselves here, but again it
597 * only takes effect on return to main loop.
599 if (connections->n_incomplete >
600 bus_context_get_max_incomplete_connections (connections->context))
602 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
604 _dbus_assert (connections->incomplete != NULL);
605 /* Disconnect the oldest unauthenticated connection. FIXME
606 * would it be more secure to drop a *random* connection? This
607 * algorithm seems to mean that if someone can create new
608 * connections quickly enough, they can keep anyone else from
609 * completing authentication. But random may or may not really
610 * help with that, a more elaborate solution might be required.
612 dbus_connection_disconnect (connections->incomplete->data);
620 if (!dbus_connection_set_watch_functions (connection,
624 _dbus_assert_not_reached ("setting watch functions to NULL failed");
626 if (!dbus_connection_set_timeout_functions (connection,
630 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
632 dbus_connection_set_unix_user_function (connection,
635 dbus_connection_set_dispatch_status_function (connection,
638 if (d->link_in_connection_list != NULL)
640 _dbus_assert (d->link_in_connection_list->next == NULL);
641 _dbus_assert (d->link_in_connection_list->prev == NULL);
642 _dbus_list_free_link (d->link_in_connection_list);
643 d->link_in_connection_list = NULL;
646 if (!dbus_connection_set_data (connection,
647 connection_data_slot,
649 _dbus_assert_not_reached ("failed to set connection data to null");
651 /* "d" has now been freed */
658 bus_connections_expire_incomplete (BusConnections *connections)
664 if (connections->incomplete != NULL)
666 long tv_sec, tv_usec;
670 _dbus_get_current_time (&tv_sec, &tv_usec);
671 auth_timeout = bus_context_get_auth_timeout (connections->context);
673 link = _dbus_list_get_first_link (&connections->incomplete);
676 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
677 DBusConnection *connection;
678 BusConnectionData *d;
681 connection = link->data;
683 d = BUS_CONNECTION_DATA (connection);
685 _dbus_assert (d != NULL);
687 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
688 d->connection_tv_usec,
691 if (elapsed >= (double) auth_timeout)
693 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
694 dbus_connection_disconnect (connection);
698 /* We can end the loop, since the connections are in oldest-first order */
699 next_interval = ((double)auth_timeout) - elapsed;
700 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
701 connection, next_interval);
710 bus_expire_timeout_set_interval (connections->expire_timeout,
715 expire_incomplete_timeout (void *data)
717 BusConnections *connections = data;
719 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
721 /* note that this may remove the timeout */
722 bus_connections_expire_incomplete (connections);
728 bus_connection_get_groups (DBusConnection *connection,
729 unsigned long **groups,
733 BusConnectionData *d;
735 DBusUserDatabase *user_database;
737 d = BUS_CONNECTION_DATA (connection);
739 _dbus_assert (d != NULL);
741 user_database = bus_context_get_user_database (d->connections->context);
746 if (dbus_connection_get_unix_user (connection, &uid))
748 if (!_dbus_user_database_get_groups (user_database,
749 uid, groups, n_groups,
752 _DBUS_ASSERT_ERROR_IS_SET (error);
753 _dbus_verbose ("Did not get any groups for UID %lu\n",
759 _dbus_verbose ("Got %d groups for UID %lu\n",
765 return TRUE; /* successfully got 0 groups */
769 bus_connection_is_in_group (DBusConnection *connection,
773 unsigned long *group_ids;
776 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids,
781 while (i < n_group_ids)
783 if (group_ids[i] == gid)
785 dbus_free (group_ids);
791 dbus_free (group_ids);
796 bus_connection_get_policy (DBusConnection *connection)
798 BusConnectionData *d;
800 d = BUS_CONNECTION_DATA (connection);
802 _dbus_assert (d != NULL);
803 _dbus_assert (d->policy != NULL);
809 foreach_active (BusConnections *connections,
810 BusConnectionForeachFunction function,
815 link = _dbus_list_get_first_link (&connections->completed);
818 DBusConnection *connection = link->data;
819 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
821 if (!(* function) (connection, data))
831 foreach_inactive (BusConnections *connections,
832 BusConnectionForeachFunction function,
837 link = _dbus_list_get_first_link (&connections->incomplete);
840 DBusConnection *connection = link->data;
841 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
843 if (!(* function) (connection, data))
853 * Calls function on each active connection; if the function returns
854 * #FALSE, stops iterating. Active connections are authenticated
855 * and have sent a Hello message.
857 * @param connections the connections object
858 * @param function the function
859 * @param data data to pass to it as a second arg
862 bus_connections_foreach_active (BusConnections *connections,
863 BusConnectionForeachFunction function,
866 foreach_active (connections, function, data);
870 * Calls function on each connection; if the function returns
871 * #FALSE, stops iterating.
873 * @param connections the connections object
874 * @param function the function
875 * @param data data to pass to it as a second arg
878 bus_connections_foreach (BusConnections *connections,
879 BusConnectionForeachFunction function,
882 if (!foreach_active (connections, function, data))
885 foreach_inactive (connections, function, data);
889 bus_connections_get_context (BusConnections *connections)
891 return connections->context;
895 * This is used to avoid covering the same connection twice when
896 * traversing connections. Note that it assumes we will
897 * bus_connection_mark_stamp() each connection at least once per
898 * INT_MAX increments of the global stamp, or wraparound would break
902 bus_connections_increment_stamp (BusConnections *connections)
904 connections->stamp += 1;
907 /* Mark connection with current stamp, return TRUE if it
908 * didn't already have that stamp
911 bus_connection_mark_stamp (DBusConnection *connection)
913 BusConnectionData *d;
915 d = BUS_CONNECTION_DATA (connection);
917 _dbus_assert (d != NULL);
919 if (d->stamp == d->connections->stamp)
923 d->stamp = d->connections->stamp;
929 bus_connection_get_context (DBusConnection *connection)
931 BusConnectionData *d;
933 d = BUS_CONNECTION_DATA (connection);
935 _dbus_assert (d != NULL);
937 return d->connections->context;
941 bus_connection_get_connections (DBusConnection *connection)
943 BusConnectionData *d;
945 d = BUS_CONNECTION_DATA (connection);
947 _dbus_assert (d != NULL);
949 return d->connections;
953 bus_connection_get_registry (DBusConnection *connection)
955 BusConnectionData *d;
957 d = BUS_CONNECTION_DATA (connection);
959 _dbus_assert (d != NULL);
961 return bus_context_get_registry (d->connections->context);
965 bus_connection_get_activation (DBusConnection *connection)
967 BusConnectionData *d;
969 d = BUS_CONNECTION_DATA (connection);
971 _dbus_assert (d != NULL);
973 return bus_context_get_activation (d->connections->context);
977 bus_connection_get_matchmaker (DBusConnection *connection)
979 BusConnectionData *d;
981 d = BUS_CONNECTION_DATA (connection);
983 _dbus_assert (d != NULL);
985 return bus_context_get_matchmaker (d->connections->context);
989 * Checks whether the connection is registered with the message bus.
991 * @param connection the connection
992 * @returns #TRUE if we're an active message bus participant
995 bus_connection_is_active (DBusConnection *connection)
997 BusConnectionData *d;
999 d = BUS_CONNECTION_DATA (connection);
1001 return d != NULL && d->name != NULL;
1005 bus_connection_preallocate_oom_error (DBusConnection *connection)
1007 DBusMessage *message;
1008 DBusPreallocatedSend *preallocated;
1009 BusConnectionData *d;
1011 d = BUS_CONNECTION_DATA (connection);
1013 _dbus_assert (d != NULL);
1015 if (d->oom_preallocated != NULL)
1018 preallocated = dbus_connection_preallocate_send (connection);
1019 if (preallocated == NULL)
1022 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1024 if (message == NULL)
1026 dbus_connection_free_preallocated_send (connection, preallocated);
1030 /* d->name may be NULL, but that is OK */
1031 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1032 !dbus_message_set_destination (message, d->name) ||
1033 !dbus_message_set_sender (message,
1034 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1036 dbus_connection_free_preallocated_send (connection, preallocated);
1037 dbus_message_unref (message);
1041 /* set reply serial to placeholder value just so space is already allocated
1044 if (!dbus_message_set_reply_serial (message, 14))
1046 dbus_connection_free_preallocated_send (connection, preallocated);
1047 dbus_message_unref (message);
1051 d->oom_message = message;
1052 d->oom_preallocated = preallocated;
1058 bus_connection_send_oom_error (DBusConnection *connection,
1059 DBusMessage *in_reply_to)
1061 BusConnectionData *d;
1063 d = BUS_CONNECTION_DATA (connection);
1065 _dbus_assert (d != NULL);
1066 _dbus_assert (d->oom_message != NULL);
1068 /* should always succeed since we set it to a placeholder earlier */
1069 if (!dbus_message_set_reply_serial (d->oom_message,
1070 dbus_message_get_serial (in_reply_to)))
1071 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1073 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1075 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1076 d->oom_message, NULL);
1078 dbus_message_unref (d->oom_message);
1079 d->oom_message = NULL;
1080 d->oom_preallocated = NULL;
1084 bus_connection_add_match_rule_link (DBusConnection *connection,
1087 BusConnectionData *d;
1089 d = BUS_CONNECTION_DATA (connection);
1090 _dbus_assert (d != NULL);
1092 _dbus_list_append_link (&d->match_rules, link);
1094 d->n_match_rules += 1;
1098 bus_connection_add_match_rule (DBusConnection *connection,
1103 link = _dbus_list_alloc_link (rule);
1108 bus_connection_add_match_rule_link (connection, link);
1114 bus_connection_remove_match_rule (DBusConnection *connection,
1117 BusConnectionData *d;
1119 d = BUS_CONNECTION_DATA (connection);
1120 _dbus_assert (d != NULL);
1122 _dbus_list_remove_last (&d->match_rules, rule);
1124 d->n_match_rules -= 1;
1125 _dbus_assert (d->n_match_rules >= 0);
1129 bus_connection_get_n_match_rules (DBusConnection *connection)
1131 BusConnectionData *d;
1133 d = BUS_CONNECTION_DATA (connection);
1134 _dbus_assert (d != NULL);
1136 return d->n_match_rules;
1140 bus_connection_add_owned_service_link (DBusConnection *connection,
1143 BusConnectionData *d;
1145 d = BUS_CONNECTION_DATA (connection);
1146 _dbus_assert (d != NULL);
1148 _dbus_list_append_link (&d->services_owned, link);
1150 d->n_services_owned += 1;
1154 bus_connection_add_owned_service (DBusConnection *connection,
1155 BusService *service)
1159 link = _dbus_list_alloc_link (service);
1164 bus_connection_add_owned_service_link (connection, link);
1170 bus_connection_remove_owned_service (DBusConnection *connection,
1171 BusService *service)
1173 BusConnectionData *d;
1175 d = BUS_CONNECTION_DATA (connection);
1176 _dbus_assert (d != NULL);
1178 _dbus_list_remove_last (&d->services_owned, service);
1180 d->n_services_owned -= 1;
1181 _dbus_assert (d->n_services_owned >= 0);
1185 bus_connection_get_n_services_owned (DBusConnection *connection)
1187 BusConnectionData *d;
1189 d = BUS_CONNECTION_DATA (connection);
1190 _dbus_assert (d != NULL);
1192 return d->n_services_owned;
1196 bus_connection_complete (DBusConnection *connection,
1197 const DBusString *name,
1200 BusConnectionData *d;
1203 d = BUS_CONNECTION_DATA (connection);
1204 _dbus_assert (d != NULL);
1205 _dbus_assert (d->name == NULL);
1206 _dbus_assert (d->policy == NULL);
1208 _dbus_assert (!bus_connection_is_active (connection));
1210 if (!_dbus_string_copy_data (name, &d->name))
1212 BUS_SET_OOM (error);
1216 _dbus_assert (d->name != NULL);
1218 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1220 d->policy = bus_context_create_client_policy (d->connections->context,
1224 /* we may have a NULL policy on OOM or error getting list of
1225 * groups for a user. In the latter case we don't handle it so
1226 * well currently, as it will just keep failing over and over.
1229 if (d->policy == NULL)
1231 _dbus_verbose ("Failed to create security policy for connection %p\n",
1233 _DBUS_ASSERT_ERROR_IS_SET (error);
1234 dbus_free (d->name);
1239 if (dbus_connection_get_unix_user (connection, &uid))
1241 if (!adjust_connections_for_uid (d->connections,
1244 BUS_SET_OOM (error);
1245 dbus_free (d->name);
1251 /* Now the connection is active, move it between lists */
1252 _dbus_list_unlink (&d->connections->incomplete,
1253 d->link_in_connection_list);
1254 d->connections->n_incomplete -= 1;
1255 _dbus_list_append_link (&d->connections->completed,
1256 d->link_in_connection_list);
1257 d->connections->n_completed += 1;
1259 _dbus_assert (d->connections->n_incomplete >= 0);
1260 _dbus_assert (d->connections->n_completed > 0);
1262 /* See if we can remove the timeout */
1263 bus_connections_expire_incomplete (d->connections);
1265 _dbus_assert (bus_connection_is_active (connection));
1271 bus_connection_get_name (DBusConnection *connection)
1273 BusConnectionData *d;
1275 d = BUS_CONNECTION_DATA (connection);
1276 _dbus_assert (d != NULL);
1282 * Check whether completing the passed-in connection would
1283 * exceed limits, and if so set error and return #FALSE
1286 bus_connections_check_limits (BusConnections *connections,
1287 DBusConnection *requesting_completion,
1290 BusConnectionData *d;
1293 d = BUS_CONNECTION_DATA (requesting_completion);
1294 _dbus_assert (d != NULL);
1296 _dbus_assert (d->name == NULL);
1298 if (connections->n_completed >=
1299 bus_context_get_max_completed_connections (connections->context))
1301 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1302 "The maximum number of active connections has been reached");
1306 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1308 if (get_connections_for_uid (connections, uid) >=
1309 bus_context_get_max_connections_per_user (connections->context))
1311 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1312 "The maximum number of active connections for UID %lu has been reached",
1325 * Note that this is fairly fragile; in particular, don't try to use
1326 * one transaction across any main loop iterations.
1331 BusTransaction *transaction;
1332 DBusMessage *message;
1333 DBusPreallocatedSend *preallocated;
1338 BusTransactionCancelFunction cancel_function;
1339 DBusFreeFunction free_data_function;
1343 struct BusTransaction
1345 DBusList *connections;
1346 BusContext *context;
1347 DBusList *cancel_hooks;
1351 message_to_send_free (DBusConnection *connection,
1352 MessageToSend *to_send)
1354 if (to_send->message)
1355 dbus_message_unref (to_send->message);
1357 if (to_send->preallocated)
1358 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1360 dbus_free (to_send);
1364 cancel_hook_cancel (void *element,
1367 CancelHook *ch = element;
1369 _dbus_verbose ("Running transaction cancel hook\n");
1371 if (ch->cancel_function)
1372 (* ch->cancel_function) (ch->data);
1376 cancel_hook_free (void *element,
1379 CancelHook *ch = element;
1381 if (ch->free_data_function)
1382 (* ch->free_data_function) (ch->data);
1388 free_cancel_hooks (BusTransaction *transaction)
1390 _dbus_list_foreach (&transaction->cancel_hooks,
1391 cancel_hook_free, NULL);
1393 _dbus_list_clear (&transaction->cancel_hooks);
1397 bus_transaction_new (BusContext *context)
1399 BusTransaction *transaction;
1401 transaction = dbus_new0 (BusTransaction, 1);
1402 if (transaction == NULL)
1405 transaction->context = context;
1411 bus_transaction_get_context (BusTransaction *transaction)
1413 return transaction->context;
1417 bus_transaction_get_connections (BusTransaction *transaction)
1419 return bus_context_get_connections (transaction->context);
1423 bus_transaction_send_from_driver (BusTransaction *transaction,
1424 DBusConnection *connection,
1425 DBusMessage *message)
1427 /* We have to set the sender to the driver, and have
1428 * to check security policy since it was not done in
1431 _dbus_verbose ("Sending %s %s %s from driver\n",
1432 dbus_message_get_interface (message) ?
1433 dbus_message_get_interface (message) : "(no interface)",
1434 dbus_message_get_member (message) ?
1435 dbus_message_get_member (message) : "(no member)",
1436 dbus_message_get_error_name (message) ?
1437 dbus_message_get_error_name (message) : "(no error name)");
1439 if (!dbus_message_set_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1442 /* If security policy doesn't allow the message, we silently
1443 * eat it; the driver doesn't care about getting a reply.
1445 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
1446 NULL, connection, connection, message, NULL))
1449 return bus_transaction_send (transaction, connection, message);
1453 bus_transaction_send (BusTransaction *transaction,
1454 DBusConnection *connection,
1455 DBusMessage *message)
1457 MessageToSend *to_send;
1458 BusConnectionData *d;
1461 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
1462 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
1463 dbus_message_get_reply_serial (message) != 0 ? "reply" :
1465 dbus_message_get_interface (message) ?
1466 dbus_message_get_interface (message) : "(unset)",
1467 dbus_message_get_member (message) ?
1468 dbus_message_get_member (message) : "(unset)",
1469 dbus_message_get_error_name (message) ?
1470 dbus_message_get_error_name (message) : "(unset)",
1471 dbus_connection_get_is_connected (connection) ?
1472 "" : " (disconnected)");
1474 _dbus_assert (dbus_message_get_sender (message) != NULL);
1476 if (!dbus_connection_get_is_connected (connection))
1477 return TRUE; /* silently ignore disconnected connections */
1479 d = BUS_CONNECTION_DATA (connection);
1480 _dbus_assert (d != NULL);
1482 to_send = dbus_new (MessageToSend, 1);
1483 if (to_send == NULL)
1488 to_send->preallocated = dbus_connection_preallocate_send (connection);
1489 if (to_send->preallocated == NULL)
1491 dbus_free (to_send);
1495 dbus_message_ref (message);
1496 to_send->message = message;
1497 to_send->transaction = transaction;
1499 _dbus_verbose ("about to prepend message\n");
1501 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
1503 message_to_send_free (connection, to_send);
1507 _dbus_verbose ("prepended message\n");
1509 /* See if we already had this connection in the list
1510 * for this transaction. If we have a pending message,
1511 * then we should already be in transaction->connections
1513 link = _dbus_list_get_first_link (&d->transaction_messages);
1514 _dbus_assert (link->data == to_send);
1515 link = _dbus_list_get_next_link (&d->transaction_messages, link);
1516 while (link != NULL)
1518 MessageToSend *m = link->data;
1519 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1521 if (m->transaction == transaction)
1529 if (!_dbus_list_prepend (&transaction->connections, connection))
1531 _dbus_list_remove (&d->transaction_messages, to_send);
1532 message_to_send_free (connection, to_send);
1541 connection_cancel_transaction (DBusConnection *connection,
1542 BusTransaction *transaction)
1545 BusConnectionData *d;
1547 d = BUS_CONNECTION_DATA (connection);
1548 _dbus_assert (d != NULL);
1550 link = _dbus_list_get_first_link (&d->transaction_messages);
1551 while (link != NULL)
1553 MessageToSend *m = link->data;
1554 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1556 if (m->transaction == transaction)
1558 _dbus_list_remove_link (&d->transaction_messages,
1561 message_to_send_free (connection, m);
1569 bus_transaction_cancel_and_free (BusTransaction *transaction)
1571 DBusConnection *connection;
1573 _dbus_verbose ("TRANSACTION: cancelled\n");
1575 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1576 connection_cancel_transaction (connection, transaction);
1578 _dbus_assert (transaction->connections == NULL);
1580 _dbus_list_foreach (&transaction->cancel_hooks,
1581 cancel_hook_cancel, NULL);
1583 free_cancel_hooks (transaction);
1585 dbus_free (transaction);
1589 connection_execute_transaction (DBusConnection *connection,
1590 BusTransaction *transaction)
1593 BusConnectionData *d;
1595 d = BUS_CONNECTION_DATA (connection);
1596 _dbus_assert (d != NULL);
1598 /* Send the queue in order (FIFO) */
1599 link = _dbus_list_get_last_link (&d->transaction_messages);
1600 while (link != NULL)
1602 MessageToSend *m = link->data;
1603 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
1605 if (m->transaction == transaction)
1607 _dbus_list_remove_link (&d->transaction_messages,
1610 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
1612 dbus_connection_send_preallocated (connection,
1617 m->preallocated = NULL; /* so we don't double-free it */
1619 message_to_send_free (connection, m);
1627 bus_transaction_execute_and_free (BusTransaction *transaction)
1629 /* For each connection in transaction->connections
1632 DBusConnection *connection;
1634 _dbus_verbose ("TRANSACTION: executing\n");
1636 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1637 connection_execute_transaction (connection, transaction);
1639 _dbus_assert (transaction->connections == NULL);
1641 free_cancel_hooks (transaction);
1643 dbus_free (transaction);
1647 bus_connection_remove_transactions (DBusConnection *connection)
1649 MessageToSend *to_send;
1650 BusConnectionData *d;
1652 d = BUS_CONNECTION_DATA (connection);
1653 _dbus_assert (d != NULL);
1655 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
1657 /* only has an effect for the first MessageToSend listing this transaction */
1658 _dbus_list_remove (&to_send->transaction->connections,
1661 _dbus_list_remove (&d->transaction_messages, to_send);
1662 message_to_send_free (connection, to_send);
1667 * Converts the DBusError to a message reply
1670 bus_transaction_send_error_reply (BusTransaction *transaction,
1671 DBusConnection *connection,
1672 const DBusError *error,
1673 DBusMessage *in_reply_to)
1677 _dbus_assert (error != NULL);
1678 _DBUS_ASSERT_ERROR_IS_SET (error);
1680 _dbus_verbose ("Sending error reply %s \"%s\"\n",
1681 error->name, error->message);
1683 reply = dbus_message_new_error (in_reply_to,
1689 if (!bus_transaction_send_from_driver (transaction, connection, reply))
1691 dbus_message_unref (reply);
1695 dbus_message_unref (reply);
1701 bus_transaction_add_cancel_hook (BusTransaction *transaction,
1702 BusTransactionCancelFunction cancel_function,
1704 DBusFreeFunction free_data_function)
1708 ch = dbus_new (CancelHook, 1);
1712 ch->cancel_function = cancel_function;
1714 ch->free_data_function = free_data_function;
1716 /* It's important that the hooks get run in reverse order that they
1719 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))