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"
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-hash.h>
30 #include <dbus/dbus-timeout.h>
32 static void bus_connection_remove_transactions (DBusConnection *connection);
37 DBusList *completed; /**< List of all completed connections */
38 int n_completed; /**< Length of completed list */
39 DBusList *incomplete; /**< List of all not-yet-active connections */
40 int n_incomplete; /**< Length of incomplete list */
42 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
43 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
46 static int connection_data_slot = -1;
47 static int connection_data_slot_refcount = 0;
51 BusConnections *connections;
52 DBusList *link_in_connection_list;
53 DBusConnection *connection;
54 DBusList *services_owned;
57 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
58 DBusMessage *oom_message;
59 DBusPreallocatedSend *oom_preallocated;
60 BusClientPolicy *policy;
62 long connection_tv_sec; /**< Time when we connected (seconds component) */
63 long connection_tv_usec; /**< Time when we connected (microsec component) */
66 static dbus_bool_t expire_incomplete_timeout (void *data);
68 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
71 connection_data_slot_ref (void)
73 if (connection_data_slot < 0)
75 connection_data_slot = dbus_connection_allocate_data_slot ();
77 if (connection_data_slot < 0)
80 _dbus_assert (connection_data_slot_refcount == 0);
83 connection_data_slot_refcount += 1;
90 connection_data_slot_unref (void)
92 _dbus_assert (connection_data_slot_refcount > 0);
94 connection_data_slot_refcount -= 1;
96 if (connection_data_slot_refcount == 0)
98 dbus_connection_free_data_slot (connection_data_slot);
99 connection_data_slot = -1;
104 connection_get_loop (DBusConnection *connection)
106 BusConnectionData *d;
108 d = BUS_CONNECTION_DATA (connection);
110 return bus_context_get_loop (d->connections->context);
115 get_connections_for_uid (BusConnections *connections,
121 /* val is NULL is 0 when it isn't in the hash yet */
123 val = _dbus_hash_table_lookup_ulong (connections->completed_by_user,
126 current_count = _DBUS_POINTER_TO_INT (val);
128 return current_count;
132 adjust_connections_for_uid (BusConnections *connections,
138 current_count = get_connections_for_uid (connections, uid);
140 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
141 ": was %d adjustment %d making %d\n",
142 uid, current_count, adjustment, current_count + adjustment);
144 _dbus_assert (current_count >= 0);
146 current_count += adjustment;
148 _dbus_assert (current_count >= 0);
150 if (current_count == 0)
152 _dbus_hash_table_remove_ulong (connections->completed_by_user, uid);
159 retval = _dbus_hash_table_insert_ulong (connections->completed_by_user,
160 uid, _DBUS_INT_TO_POINTER (current_count));
162 /* only positive adjustment can fail as otherwise
163 * a hash entry should already exist
165 _dbus_assert (adjustment > 0 ||
166 (adjustment <= 0 && retval));
173 bus_connection_disconnected (DBusConnection *connection)
175 BusConnectionData *d;
178 d = BUS_CONNECTION_DATA (connection);
179 _dbus_assert (d != NULL);
181 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
182 d->name ? d->name : "(inactive)");
184 /* Drop any service ownership. FIXME Unfortunately, this requires
185 * memory allocation and there doesn't seem to be a good way to
186 * handle it other than sleeping; we can't "fail" the operation of
187 * disconnecting a client, and preallocating a broadcast "service is
188 * now gone" message for every client-service pair seems kind of
189 * involved. Probably we need to do that though.
191 while ((service = _dbus_list_get_last (&d->services_owned)))
193 BusTransaction *transaction;
198 dbus_error_init (&error);
201 while (transaction == NULL)
203 transaction = bus_transaction_new (d->connections->context);
204 _dbus_wait_for_memory ();
207 if (!bus_service_remove_owner (service, connection,
208 transaction, &error))
210 _DBUS_ASSERT_ERROR_IS_SET (&error);
212 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
214 dbus_error_free (&error);
215 bus_transaction_cancel_and_free (transaction);
216 _dbus_wait_for_memory ();
221 _dbus_verbose ("Failed to remove service owner: %s %s\n",
222 error.name, error.message);
223 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
227 bus_transaction_execute_and_free (transaction);
230 bus_dispatch_remove_connection (connection);
232 /* no more watching */
233 if (!dbus_connection_set_watch_functions (connection,
237 _dbus_assert_not_reached ("setting watch functions to NULL failed");
239 if (!dbus_connection_set_timeout_functions (connection,
243 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
245 dbus_connection_set_unix_user_function (connection,
248 dbus_connection_set_dispatch_status_function (connection,
251 bus_connection_remove_transactions (connection);
253 if (d->link_in_connection_list != NULL)
259 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
260 d->link_in_connection_list = NULL;
261 d->connections->n_completed -= 1;
263 if (dbus_connection_get_unix_user (connection, &uid))
265 if (!adjust_connections_for_uid (d->connections,
267 _dbus_assert_not_reached ("adjusting downward should never fail");
272 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
273 d->link_in_connection_list = NULL;
274 d->connections->n_incomplete -= 1;
277 _dbus_assert (d->connections->n_incomplete >= 0);
278 _dbus_assert (d->connections->n_completed >= 0);
281 /* frees "d" as side effect */
282 dbus_connection_set_data (connection,
283 connection_data_slot,
286 dbus_connection_unref (connection);
290 connection_watch_callback (DBusWatch *watch,
291 unsigned int condition,
294 /* FIXME this can be done in dbus-mainloop.c
295 * if the code in activation.c for the babysitter
296 * watch handler is fixed.
300 _dbus_verbose ("Calling handle_watch\n");
302 return dbus_watch_handle (watch, condition);
306 add_connection_watch (DBusWatch *watch,
309 DBusConnection *connection = data;
311 return _dbus_loop_add_watch (connection_get_loop (connection),
312 watch, connection_watch_callback, connection,
317 remove_connection_watch (DBusWatch *watch,
320 DBusConnection *connection = data;
322 _dbus_loop_remove_watch (connection_get_loop (connection),
323 watch, connection_watch_callback, connection);
327 connection_timeout_callback (DBusTimeout *timeout,
330 /* DBusConnection *connection = data; */
332 /* can return FALSE on OOM but we just let it fire again later */
333 dbus_timeout_handle (timeout);
337 add_connection_timeout (DBusTimeout *timeout,
340 DBusConnection *connection = data;
342 return _dbus_loop_add_timeout (connection_get_loop (connection),
343 timeout, connection_timeout_callback, connection, NULL);
347 remove_connection_timeout (DBusTimeout *timeout,
350 DBusConnection *connection = data;
352 _dbus_loop_remove_timeout (connection_get_loop (connection),
353 timeout, connection_timeout_callback, connection);
357 dispatch_status_function (DBusConnection *connection,
358 DBusDispatchStatus new_status,
361 DBusLoop *loop = data;
363 if (new_status != DBUS_DISPATCH_COMPLETE)
365 while (!_dbus_loop_queue_dispatch (loop, connection))
366 _dbus_wait_for_memory ();
371 allow_user_function (DBusConnection *connection,
375 BusConnectionData *d;
377 d = BUS_CONNECTION_DATA (connection);
379 _dbus_assert (d != NULL);
381 return bus_context_allow_user (d->connections->context, uid);
385 free_connection_data (void *data)
387 BusConnectionData *d = data;
389 /* services_owned should be NULL since we should be disconnected */
390 _dbus_assert (d->services_owned == NULL);
391 _dbus_assert (d->n_services_owned == 0);
393 _dbus_assert (d->transaction_messages == NULL);
395 if (d->oom_preallocated)
396 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
399 dbus_message_unref (d->oom_message);
402 bus_client_policy_unref (d->policy);
410 call_timeout_callback (DBusTimeout *timeout,
413 /* can return FALSE on OOM but we just let it fire again later */
414 dbus_timeout_handle (timeout);
418 bus_connections_new (BusContext *context)
420 BusConnections *connections;
422 if (!connection_data_slot_ref ())
425 connections = dbus_new0 (BusConnections, 1);
426 if (connections == NULL)
429 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_ULONG,
431 if (connections->completed_by_user == NULL)
434 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
435 expire_incomplete_timeout,
437 if (connections->expire_timeout == NULL)
440 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
442 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
443 connections->expire_timeout,
444 call_timeout_callback, NULL, NULL))
447 connections->refcount = 1;
448 connections->context = context;
453 _dbus_timeout_unref (connections->expire_timeout);
455 _dbus_hash_table_unref (connections->completed_by_user);
457 dbus_free (connections);
459 connection_data_slot_unref ();
465 bus_connections_ref (BusConnections *connections)
467 _dbus_assert (connections->refcount > 0);
468 connections->refcount += 1;
472 bus_connections_unref (BusConnections *connections)
474 _dbus_assert (connections->refcount > 0);
475 connections->refcount -= 1;
476 if (connections->refcount == 0)
478 /* drop all incomplete */
479 while (connections->incomplete != NULL)
481 DBusConnection *connection;
483 connection = connections->incomplete->data;
485 dbus_connection_ref (connection);
486 dbus_connection_disconnect (connection);
487 bus_connection_disconnected (connection);
488 dbus_connection_unref (connection);
491 _dbus_assert (connections->n_incomplete == 0);
493 /* drop all real connections */
494 while (connections->completed != NULL)
496 DBusConnection *connection;
498 connection = connections->completed->data;
500 dbus_connection_ref (connection);
501 dbus_connection_disconnect (connection);
502 bus_connection_disconnected (connection);
503 dbus_connection_unref (connection);
506 _dbus_assert (connections->n_completed == 0);
508 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
509 connections->expire_timeout,
510 call_timeout_callback, NULL);
512 _dbus_timeout_unref (connections->expire_timeout);
514 _dbus_hash_table_unref (connections->completed_by_user);
516 dbus_free (connections);
518 connection_data_slot_unref ();
523 bus_connections_setup_connection (BusConnections *connections,
524 DBusConnection *connection)
526 BusConnectionData *d;
529 d = dbus_new0 (BusConnectionData, 1);
534 d->connections = connections;
535 d->connection = connection;
537 _dbus_get_current_time (&d->connection_tv_sec,
538 &d->connection_tv_usec);
540 _dbus_assert (connection_data_slot >= 0);
542 if (!dbus_connection_set_data (connection,
543 connection_data_slot,
544 d, free_connection_data))
552 if (!dbus_connection_set_watch_functions (connection,
553 add_connection_watch,
554 remove_connection_watch,
560 if (!dbus_connection_set_timeout_functions (connection,
561 add_connection_timeout,
562 remove_connection_timeout,
567 dbus_connection_set_unix_user_function (connection,
571 dbus_connection_set_dispatch_status_function (connection,
572 dispatch_status_function,
573 bus_context_get_loop (connections->context),
576 d->link_in_connection_list = _dbus_list_alloc_link (connection);
577 if (d->link_in_connection_list == NULL)
580 /* Setup the connection with the dispatcher */
581 if (!bus_dispatch_add_connection (connection))
584 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
586 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
588 bus_dispatch_remove_connection (connection);
593 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
594 connections->n_incomplete += 1;
596 dbus_connection_ref (connection);
598 /* Note that we might disconnect ourselves here, but it only takes
599 * effect on return to the main loop. We call this to free up
600 * expired connections if possible, and to queue the timeout for our
603 bus_connections_expire_incomplete (connections);
605 /* And we might also disconnect ourselves here, but again it
606 * only takes effect on return to main loop.
608 if (connections->n_incomplete >
609 bus_context_get_max_incomplete_connections (connections->context))
611 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
613 _dbus_assert (connections->incomplete != NULL);
614 /* Disconnect the oldest unauthenticated connection. FIXME
615 * would it be more secure to drop a *random* connection? This
616 * algorithm seems to mean that if someone can create new
617 * connections quickly enough, they can keep anyone else from
618 * completing authentication. But random may or may not really
619 * help with that, a more elaborate solution might be required.
621 dbus_connection_disconnect (connections->incomplete->data);
629 if (!dbus_connection_set_watch_functions (connection,
633 _dbus_assert_not_reached ("setting watch functions to NULL failed");
635 if (!dbus_connection_set_timeout_functions (connection,
639 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
641 dbus_connection_set_unix_user_function (connection,
644 dbus_connection_set_dispatch_status_function (connection,
647 if (!dbus_connection_set_data (connection,
648 connection_data_slot,
650 _dbus_assert_not_reached ("failed to set connection data to null");
652 if (d->link_in_connection_list != NULL)
654 _dbus_assert (d->link_in_connection_list->next == NULL);
655 _dbus_assert (d->link_in_connection_list->prev == NULL);
656 _dbus_list_free_link (d->link_in_connection_list);
664 bus_connections_expire_incomplete (BusConnections *connections)
670 if (connections->incomplete != NULL)
672 long tv_sec, tv_usec;
676 _dbus_get_current_time (&tv_sec, &tv_usec);
677 auth_timeout = bus_context_get_auth_timeout (connections->context);
679 link = _dbus_list_get_first_link (&connections->incomplete);
682 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
683 DBusConnection *connection;
684 BusConnectionData *d;
687 connection = link->data;
689 d = BUS_CONNECTION_DATA (connection);
691 _dbus_assert (d != NULL);
693 elapsed = ((double) tv_sec - (double) d->connection_tv_sec) * 1000.0 +
694 ((double) tv_usec - (double) d->connection_tv_usec) / 1000.0;
696 if (elapsed >= (double) auth_timeout)
698 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
699 dbus_connection_disconnect (connection);
703 /* We can end the loop, since the connections are in oldest-first order */
704 next_interval = ((double)auth_timeout) - elapsed;
705 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
706 connection, next_interval);
715 if (next_interval >= 0)
717 _dbus_timeout_set_interval (connections->expire_timeout,
719 _dbus_timeout_set_enabled (connections->expire_timeout, TRUE);
721 _dbus_verbose ("Enabled incomplete connections timeout with interval %d, %d incomplete connections\n",
722 next_interval, connections->n_incomplete);
724 else if (dbus_timeout_get_enabled (connections->expire_timeout))
726 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
728 _dbus_verbose ("Disabled incomplete connections timeout, %d incomplete connections\n",
729 connections->n_incomplete);
732 _dbus_verbose ("No need to disable incomplete connections timeout\n");
736 expire_incomplete_timeout (void *data)
738 BusConnections *connections = data;
740 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
742 /* note that this may remove the timeout */
743 bus_connections_expire_incomplete (connections);
749 bus_connection_get_groups (DBusConnection *connection,
750 unsigned long **groups,
753 BusConnectionData *d;
755 DBusUserDatabase *user_database;
757 d = BUS_CONNECTION_DATA (connection);
759 _dbus_assert (d != NULL);
761 user_database = bus_context_get_user_database (d->connections->context);
766 if (dbus_connection_get_unix_user (connection, &uid))
768 if (!_dbus_user_database_get_groups (user_database,
769 uid, groups, n_groups,
772 _dbus_verbose ("Did not get any groups for UID %lu\n",
778 _dbus_verbose ("Got %d groups for UID %lu\n",
784 return TRUE; /* successfully got 0 groups */
788 bus_connection_is_in_group (DBusConnection *connection,
792 unsigned long *group_ids;
795 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
799 while (i < n_group_ids)
801 if (group_ids[i] == gid)
803 dbus_free (group_ids);
809 dbus_free (group_ids);
814 bus_connection_get_policy (DBusConnection *connection,
817 BusConnectionData *d;
819 d = BUS_CONNECTION_DATA (connection);
821 _dbus_assert (d != NULL);
823 if (!dbus_connection_get_is_authenticated (connection))
825 _dbus_verbose ("Tried to get policy for unauthenticated connection!\n");
826 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
827 "Connection is not yet authenticated; the pre-authentication "
828 "implicit security policy is to deny everything");
832 /* We do lazy creation of the policy because
833 * it can only be done post-authentication.
835 if (d->policy == NULL)
838 bus_context_create_client_policy (d->connections->context,
841 /* we may have a NULL policy on OOM or error getting list of
842 * groups for a user. In the latter case we don't handle it so
843 * well currently, as it will just keep failing over and over.
847 if (d->policy == NULL)
849 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
850 "There was an error creating the security policy for connection \"%s\"; "
851 "all operations will fail for now.",
852 d->name ? d->name : "(inactive)");
860 foreach_active (BusConnections *connections,
861 BusConnectionForeachFunction function,
866 link = _dbus_list_get_first_link (&connections->completed);
869 DBusConnection *connection = link->data;
870 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
872 if (!(* function) (connection, data))
882 foreach_inactive (BusConnections *connections,
883 BusConnectionForeachFunction function,
888 link = _dbus_list_get_first_link (&connections->incomplete);
891 DBusConnection *connection = link->data;
892 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
894 if (!(* function) (connection, data))
904 * Calls function on each active connection; if the function returns
905 * #FALSE, stops iterating. Active connections are authenticated
906 * and have sent a Hello message.
908 * @param connections the connections object
909 * @param function the function
910 * @param data data to pass to it as a second arg
913 bus_connections_foreach_active (BusConnections *connections,
914 BusConnectionForeachFunction function,
917 foreach_active (connections, function, data);
921 * Calls function on each connection; if the function returns
922 * #FALSE, stops iterating.
924 * @param connections the connections object
925 * @param function the function
926 * @param data data to pass to it as a second arg
929 bus_connections_foreach (BusConnections *connections,
930 BusConnectionForeachFunction function,
933 if (!foreach_active (connections, function, data))
936 foreach_inactive (connections, function, data);
940 bus_connections_get_context (BusConnections *connections)
942 return connections->context;
946 bus_connection_get_context (DBusConnection *connection)
948 BusConnectionData *d;
950 d = BUS_CONNECTION_DATA (connection);
952 _dbus_assert (d != NULL);
954 return d->connections->context;
958 bus_connection_get_connections (DBusConnection *connection)
960 BusConnectionData *d;
962 d = BUS_CONNECTION_DATA (connection);
964 _dbus_assert (d != NULL);
966 return d->connections;
970 bus_connection_get_registry (DBusConnection *connection)
972 BusConnectionData *d;
974 d = BUS_CONNECTION_DATA (connection);
976 _dbus_assert (d != NULL);
978 return bus_context_get_registry (d->connections->context);
982 bus_connection_get_activation (DBusConnection *connection)
984 BusConnectionData *d;
986 d = BUS_CONNECTION_DATA (connection);
988 _dbus_assert (d != NULL);
990 return bus_context_get_activation (d->connections->context);
994 * Checks whether the connection is registered with the message bus.
996 * @param connection the connection
997 * @returns #TRUE if we're an active message bus participant
1000 bus_connection_is_active (DBusConnection *connection)
1002 BusConnectionData *d;
1004 d = BUS_CONNECTION_DATA (connection);
1006 return d != NULL && d->name != NULL;
1010 bus_connection_preallocate_oom_error (DBusConnection *connection)
1012 DBusMessage *message;
1013 DBusPreallocatedSend *preallocated;
1014 BusConnectionData *d;
1016 d = BUS_CONNECTION_DATA (connection);
1018 _dbus_assert (d != NULL);
1020 if (d->oom_preallocated != NULL)
1023 preallocated = dbus_connection_preallocate_send (connection);
1024 if (preallocated == NULL)
1027 /* d->name may be NULL, but that is OK */
1028 message = dbus_message_new (DBUS_ERROR_NO_MEMORY,
1030 if (message == NULL)
1032 dbus_connection_free_preallocated_send (connection, preallocated);
1036 dbus_message_set_is_error (message, TRUE);
1038 if (!dbus_message_set_sender (message,
1041 dbus_connection_free_preallocated_send (connection, preallocated);
1042 dbus_message_unref (message);
1046 /* set reply serial to placeholder value just so space is already allocated
1049 if (!dbus_message_set_reply_serial (message, 14))
1051 dbus_connection_free_preallocated_send (connection, preallocated);
1052 dbus_message_unref (message);
1056 d->oom_message = message;
1057 d->oom_preallocated = preallocated;
1063 bus_connection_send_oom_error (DBusConnection *connection,
1064 DBusMessage *in_reply_to)
1066 BusConnectionData *d;
1068 d = BUS_CONNECTION_DATA (connection);
1070 _dbus_assert (d != NULL);
1071 _dbus_assert (d->oom_message != NULL);
1073 /* should always succeed since we set it to a placeholder earlier */
1074 if (!dbus_message_set_reply_serial (d->oom_message,
1075 dbus_message_get_serial (in_reply_to)))
1076 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1078 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1080 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1081 d->oom_message, NULL);
1083 dbus_message_unref (d->oom_message);
1084 d->oom_message = NULL;
1085 d->oom_preallocated = NULL;
1089 bus_connection_add_owned_service_link (DBusConnection *connection,
1092 BusConnectionData *d;
1094 d = BUS_CONNECTION_DATA (connection);
1095 _dbus_assert (d != NULL);
1097 _dbus_list_append_link (&d->services_owned, link);
1099 d->n_services_owned += 1;
1103 bus_connection_add_owned_service (DBusConnection *connection,
1104 BusService *service)
1108 link = _dbus_list_alloc_link (service);
1113 bus_connection_add_owned_service_link (connection, link);
1119 bus_connection_remove_owned_service (DBusConnection *connection,
1120 BusService *service)
1122 BusConnectionData *d;
1124 d = BUS_CONNECTION_DATA (connection);
1125 _dbus_assert (d != NULL);
1127 _dbus_list_remove_last (&d->services_owned, service);
1129 d->n_services_owned -= 1;
1130 _dbus_assert (d->n_services_owned >= 0);
1134 bus_connection_get_n_services_owned (DBusConnection *connection)
1136 BusConnectionData *d;
1138 d = BUS_CONNECTION_DATA (connection);
1139 _dbus_assert (d != NULL);
1141 return d->n_services_owned;
1145 bus_connection_set_name (DBusConnection *connection,
1146 const DBusString *name)
1148 BusConnectionData *d;
1151 d = BUS_CONNECTION_DATA (connection);
1152 _dbus_assert (d != NULL);
1153 _dbus_assert (d->name == NULL);
1155 if (!_dbus_string_copy_data (name, &d->name))
1158 _dbus_assert (d->name != NULL);
1160 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1162 if (dbus_connection_get_unix_user (connection, &uid))
1164 if (!adjust_connections_for_uid (d->connections,
1167 dbus_free (d->name);
1173 /* Now the connection is active, move it between lists */
1174 _dbus_list_unlink (&d->connections->incomplete,
1175 d->link_in_connection_list);
1176 d->connections->n_incomplete -= 1;
1177 _dbus_list_append_link (&d->connections->completed,
1178 d->link_in_connection_list);
1179 d->connections->n_completed += 1;
1181 _dbus_assert (d->connections->n_incomplete >= 0);
1182 _dbus_assert (d->connections->n_completed > 0);
1184 /* See if we can remove the timeout */
1185 bus_connections_expire_incomplete (d->connections);
1191 bus_connection_get_name (DBusConnection *connection)
1193 BusConnectionData *d;
1195 d = BUS_CONNECTION_DATA (connection);
1196 _dbus_assert (d != NULL);
1202 * Check whether completing the passed-in connection would
1203 * exceed limits, and if so set error and return #FALSE
1206 bus_connections_check_limits (BusConnections *connections,
1207 DBusConnection *requesting_completion,
1210 BusConnectionData *d;
1213 d = BUS_CONNECTION_DATA (requesting_completion);
1214 _dbus_assert (d != NULL);
1216 _dbus_assert (d->name == NULL);
1218 if (connections->n_completed >=
1219 bus_context_get_max_completed_connections (connections->context))
1221 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1222 "The maximum number of active connections has been reached");
1226 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1228 if (get_connections_for_uid (connections, uid) >=
1229 bus_context_get_max_connections_per_user (connections->context))
1231 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1232 "The maximum number of active connections for UID %lu has been reached",
1245 * Note that this is fairly fragile; in particular, don't try to use
1246 * one transaction across any main loop iterations.
1251 BusTransaction *transaction;
1252 DBusMessage *message;
1253 DBusPreallocatedSend *preallocated;
1258 BusTransactionCancelFunction cancel_function;
1259 DBusFreeFunction free_data_function;
1263 struct BusTransaction
1265 DBusList *connections;
1266 BusContext *context;
1267 DBusList *cancel_hooks;
1271 message_to_send_free (DBusConnection *connection,
1272 MessageToSend *to_send)
1274 if (to_send->message)
1275 dbus_message_unref (to_send->message);
1277 if (to_send->preallocated)
1278 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1280 dbus_free (to_send);
1284 cancel_hook_cancel (void *element,
1287 CancelHook *ch = element;
1289 _dbus_verbose ("Running transaction cancel hook\n");
1291 if (ch->cancel_function)
1292 (* ch->cancel_function) (ch->data);
1296 cancel_hook_free (void *element,
1299 CancelHook *ch = element;
1301 if (ch->free_data_function)
1302 (* ch->free_data_function) (ch->data);
1308 free_cancel_hooks (BusTransaction *transaction)
1310 _dbus_list_foreach (&transaction->cancel_hooks,
1311 cancel_hook_free, NULL);
1313 _dbus_list_clear (&transaction->cancel_hooks);
1317 bus_transaction_new (BusContext *context)
1319 BusTransaction *transaction;
1321 transaction = dbus_new0 (BusTransaction, 1);
1322 if (transaction == NULL)
1325 transaction->context = context;
1331 bus_transaction_get_context (BusTransaction *transaction)
1333 return transaction->context;
1337 bus_transaction_get_connections (BusTransaction *transaction)
1339 return bus_context_get_connections (transaction->context);
1343 bus_transaction_send_from_driver (BusTransaction *transaction,
1344 DBusConnection *connection,
1345 DBusMessage *message)
1347 /* We have to set the sender to the driver, and have
1348 * to check security policy since it was not done in
1351 _dbus_verbose ("Sending %s from driver\n",
1352 dbus_message_get_name (message));
1354 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
1357 /* If security policy doesn't allow the message, we silently
1358 * eat it; the driver doesn't care about getting a reply.
1360 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
1361 NULL, connection, message, NULL))
1364 return bus_transaction_send (transaction, connection, message);
1368 bus_transaction_send (BusTransaction *transaction,
1369 DBusConnection *connection,
1370 DBusMessage *message)
1372 MessageToSend *to_send;
1373 BusConnectionData *d;
1376 _dbus_verbose (" trying to add %s %s to transaction%s\n",
1377 dbus_message_get_is_error (message) ? "error" :
1378 dbus_message_get_reply_serial (message) != 0 ? "reply" :
1380 dbus_message_get_name (message),
1381 dbus_connection_get_is_connected (connection) ?
1382 "" : " (disconnected)");
1384 _dbus_assert (dbus_message_get_sender (message) != NULL);
1386 if (!dbus_connection_get_is_connected (connection))
1387 return TRUE; /* silently ignore disconnected connections */
1389 d = BUS_CONNECTION_DATA (connection);
1390 _dbus_assert (d != NULL);
1392 to_send = dbus_new (MessageToSend, 1);
1393 if (to_send == NULL)
1398 to_send->preallocated = dbus_connection_preallocate_send (connection);
1399 if (to_send->preallocated == NULL)
1401 dbus_free (to_send);
1405 dbus_message_ref (message);
1406 to_send->message = message;
1407 to_send->transaction = transaction;
1409 _dbus_verbose ("about to prepend message\n");
1411 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
1413 message_to_send_free (connection, to_send);
1417 _dbus_verbose ("prepended message\n");
1419 /* See if we already had this connection in the list
1420 * for this transaction. If we have a pending message,
1421 * then we should already be in transaction->connections
1423 link = _dbus_list_get_first_link (&d->transaction_messages);
1424 _dbus_assert (link->data == to_send);
1425 link = _dbus_list_get_next_link (&d->transaction_messages, link);
1426 while (link != NULL)
1428 MessageToSend *m = link->data;
1429 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1431 if (m->transaction == transaction)
1439 if (!_dbus_list_prepend (&transaction->connections, connection))
1441 _dbus_list_remove (&d->transaction_messages, to_send);
1442 message_to_send_free (connection, to_send);
1451 connection_cancel_transaction (DBusConnection *connection,
1452 BusTransaction *transaction)
1455 BusConnectionData *d;
1457 d = BUS_CONNECTION_DATA (connection);
1458 _dbus_assert (d != NULL);
1460 link = _dbus_list_get_first_link (&d->transaction_messages);
1461 while (link != NULL)
1463 MessageToSend *m = link->data;
1464 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1466 if (m->transaction == transaction)
1468 _dbus_list_remove_link (&d->transaction_messages,
1471 message_to_send_free (connection, m);
1479 bus_transaction_cancel_and_free (BusTransaction *transaction)
1481 DBusConnection *connection;
1483 _dbus_verbose ("TRANSACTION: cancelled\n");
1485 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1486 connection_cancel_transaction (connection, transaction);
1488 _dbus_assert (transaction->connections == NULL);
1490 _dbus_list_foreach (&transaction->cancel_hooks,
1491 cancel_hook_cancel, NULL);
1493 free_cancel_hooks (transaction);
1495 dbus_free (transaction);
1499 connection_execute_transaction (DBusConnection *connection,
1500 BusTransaction *transaction)
1503 BusConnectionData *d;
1505 d = BUS_CONNECTION_DATA (connection);
1506 _dbus_assert (d != NULL);
1508 /* Send the queue in order (FIFO) */
1509 link = _dbus_list_get_last_link (&d->transaction_messages);
1510 while (link != NULL)
1512 MessageToSend *m = link->data;
1513 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
1515 if (m->transaction == transaction)
1517 _dbus_list_remove_link (&d->transaction_messages,
1520 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
1522 dbus_connection_send_preallocated (connection,
1527 m->preallocated = NULL; /* so we don't double-free it */
1529 message_to_send_free (connection, m);
1537 bus_transaction_execute_and_free (BusTransaction *transaction)
1539 /* For each connection in transaction->connections
1542 DBusConnection *connection;
1544 _dbus_verbose ("TRANSACTION: executing\n");
1546 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1547 connection_execute_transaction (connection, transaction);
1549 _dbus_assert (transaction->connections == NULL);
1551 free_cancel_hooks (transaction);
1553 dbus_free (transaction);
1557 bus_connection_remove_transactions (DBusConnection *connection)
1559 MessageToSend *to_send;
1560 BusConnectionData *d;
1562 d = BUS_CONNECTION_DATA (connection);
1563 _dbus_assert (d != NULL);
1565 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
1567 /* only has an effect for the first MessageToSend listing this transaction */
1568 _dbus_list_remove (&to_send->transaction->connections,
1571 _dbus_list_remove (&d->transaction_messages, to_send);
1572 message_to_send_free (connection, to_send);
1577 * Converts the DBusError to a message reply
1580 bus_transaction_send_error_reply (BusTransaction *transaction,
1581 DBusConnection *connection,
1582 const DBusError *error,
1583 DBusMessage *in_reply_to)
1587 _dbus_assert (error != NULL);
1588 _DBUS_ASSERT_ERROR_IS_SET (error);
1590 reply = dbus_message_new_error_reply (in_reply_to,
1596 if (!bus_transaction_send_from_driver (transaction, connection, reply))
1598 dbus_message_unref (reply);
1602 dbus_message_unref (reply);
1608 bus_transaction_add_cancel_hook (BusTransaction *transaction,
1609 BusTransactionCancelFunction cancel_function,
1611 DBusFreeFunction free_data_function)
1615 ch = dbus_new (CancelHook, 1);
1619 ch->cancel_function = cancel_function;
1621 ch->free_data_function = free_data_function;
1623 /* It's important that the hooks get run in reverse order that they
1626 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))