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;
56 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
57 DBusMessage *oom_message;
58 DBusPreallocatedSend *oom_preallocated;
59 BusClientPolicy *policy;
61 long connection_tv_sec; /**< Time when we connected (seconds component) */
62 long connection_tv_usec; /**< Time when we connected (microsec component) */
65 static dbus_bool_t expire_incomplete_timeout (void *data);
67 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
70 connection_data_slot_ref (void)
72 if (connection_data_slot < 0)
74 connection_data_slot = dbus_connection_allocate_data_slot ();
76 if (connection_data_slot < 0)
79 _dbus_assert (connection_data_slot_refcount == 0);
82 connection_data_slot_refcount += 1;
89 connection_data_slot_unref (void)
91 _dbus_assert (connection_data_slot_refcount > 0);
93 connection_data_slot_refcount -= 1;
95 if (connection_data_slot_refcount == 0)
97 dbus_connection_free_data_slot (connection_data_slot);
98 connection_data_slot = -1;
103 connection_get_loop (DBusConnection *connection)
105 BusConnectionData *d;
107 d = BUS_CONNECTION_DATA (connection);
109 return bus_context_get_loop (d->connections->context);
114 get_connections_for_uid (BusConnections *connections,
120 /* val is NULL is 0 when it isn't in the hash yet */
122 val = _dbus_hash_table_lookup_ulong (connections->completed_by_user,
125 current_count = _DBUS_POINTER_TO_INT (val);
127 return current_count;
131 adjust_connections_for_uid (BusConnections *connections,
137 current_count = get_connections_for_uid (connections, uid);
139 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
140 ": was %d adjustment %d making %d\n",
141 uid, current_count, adjustment, current_count + adjustment);
143 _dbus_assert (current_count >= 0);
145 current_count += adjustment;
147 _dbus_assert (current_count >= 0);
149 if (current_count == 0)
151 _dbus_hash_table_remove_ulong (connections->completed_by_user, uid);
158 retval = _dbus_hash_table_insert_ulong (connections->completed_by_user,
159 uid, _DBUS_INT_TO_POINTER (current_count));
161 /* only positive adjustment can fail as otherwise
162 * a hash entry should already exist
164 _dbus_assert (adjustment > 0 ||
165 (adjustment <= 0 && retval));
172 bus_connection_disconnected (DBusConnection *connection)
174 BusConnectionData *d;
177 d = BUS_CONNECTION_DATA (connection);
178 _dbus_assert (d != NULL);
180 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
181 d->name ? d->name : "(inactive)");
183 /* Drop any service ownership. FIXME Unfortunately, this requires
184 * memory allocation and there doesn't seem to be a good way to
185 * handle it other than sleeping; we can't "fail" the operation of
186 * disconnecting a client, and preallocating a broadcast "service is
187 * now gone" message for every client-service pair seems kind of
188 * involved. Probably we need to do that though.
190 while ((service = _dbus_list_get_last (&d->services_owned)))
192 BusTransaction *transaction;
197 dbus_error_init (&error);
200 while (transaction == NULL)
202 transaction = bus_transaction_new (d->connections->context);
203 _dbus_wait_for_memory ();
206 if (!bus_service_remove_owner (service, connection,
207 transaction, &error))
209 _DBUS_ASSERT_ERROR_IS_SET (&error);
211 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
213 dbus_error_free (&error);
214 bus_transaction_cancel_and_free (transaction);
215 _dbus_wait_for_memory ();
220 _dbus_verbose ("Failed to remove service owner: %s %s\n",
221 error.name, error.message);
222 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
226 bus_transaction_execute_and_free (transaction);
229 bus_dispatch_remove_connection (connection);
231 /* no more watching */
232 if (!dbus_connection_set_watch_functions (connection,
236 _dbus_assert_not_reached ("setting watch functions to NULL failed");
238 if (!dbus_connection_set_timeout_functions (connection,
242 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
244 dbus_connection_set_unix_user_function (connection,
247 dbus_connection_set_dispatch_status_function (connection,
250 bus_connection_remove_transactions (connection);
252 if (d->link_in_connection_list != NULL)
258 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
259 d->link_in_connection_list = NULL;
260 d->connections->n_completed -= 1;
262 if (dbus_connection_get_unix_user (connection, &uid))
264 if (!adjust_connections_for_uid (d->connections,
266 _dbus_assert_not_reached ("adjusting downward should never fail");
271 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
272 d->link_in_connection_list = NULL;
273 d->connections->n_incomplete -= 1;
276 _dbus_assert (d->connections->n_incomplete >= 0);
277 _dbus_assert (d->connections->n_completed >= 0);
280 /* frees "d" as side effect */
281 dbus_connection_set_data (connection,
282 connection_data_slot,
285 dbus_connection_unref (connection);
289 connection_watch_callback (DBusWatch *watch,
290 unsigned int condition,
293 /* FIXME this can be done in dbus-mainloop.c
294 * if the code in activation.c for the babysitter
295 * watch handler is fixed.
299 _dbus_verbose ("Calling handle_watch\n");
301 return dbus_watch_handle (watch, condition);
305 add_connection_watch (DBusWatch *watch,
308 DBusConnection *connection = data;
310 return _dbus_loop_add_watch (connection_get_loop (connection),
311 watch, connection_watch_callback, connection,
316 remove_connection_watch (DBusWatch *watch,
319 DBusConnection *connection = data;
321 _dbus_loop_remove_watch (connection_get_loop (connection),
322 watch, connection_watch_callback, connection);
326 connection_timeout_callback (DBusTimeout *timeout,
329 /* DBusConnection *connection = data; */
331 /* can return FALSE on OOM but we just let it fire again later */
332 dbus_timeout_handle (timeout);
336 add_connection_timeout (DBusTimeout *timeout,
339 DBusConnection *connection = data;
341 return _dbus_loop_add_timeout (connection_get_loop (connection),
342 timeout, connection_timeout_callback, connection, NULL);
346 remove_connection_timeout (DBusTimeout *timeout,
349 DBusConnection *connection = data;
351 _dbus_loop_remove_timeout (connection_get_loop (connection),
352 timeout, connection_timeout_callback, connection);
356 dispatch_status_function (DBusConnection *connection,
357 DBusDispatchStatus new_status,
360 DBusLoop *loop = data;
362 if (new_status != DBUS_DISPATCH_COMPLETE)
364 while (!_dbus_loop_queue_dispatch (loop, connection))
365 _dbus_wait_for_memory ();
370 allow_user_function (DBusConnection *connection,
374 BusConnectionData *d;
376 d = BUS_CONNECTION_DATA (connection);
378 _dbus_assert (d != NULL);
380 return bus_context_allow_user (d->connections->context, uid);
384 free_connection_data (void *data)
386 BusConnectionData *d = data;
388 /* services_owned should be NULL since we should be disconnected */
389 _dbus_assert (d->services_owned == NULL);
391 _dbus_assert (d->transaction_messages == NULL);
393 if (d->oom_preallocated)
394 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
397 dbus_message_unref (d->oom_message);
400 bus_client_policy_unref (d->policy);
408 call_timeout_callback (DBusTimeout *timeout,
411 /* can return FALSE on OOM but we just let it fire again later */
412 dbus_timeout_handle (timeout);
416 bus_connections_new (BusContext *context)
418 BusConnections *connections;
420 if (!connection_data_slot_ref ())
423 connections = dbus_new0 (BusConnections, 1);
424 if (connections == NULL)
427 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_ULONG,
429 if (connections->completed_by_user == NULL)
432 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
433 expire_incomplete_timeout,
435 if (connections->expire_timeout == NULL)
438 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
440 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
441 connections->expire_timeout,
442 call_timeout_callback, NULL, NULL))
445 connections->refcount = 1;
446 connections->context = context;
451 _dbus_timeout_unref (connections->expire_timeout);
453 _dbus_hash_table_unref (connections->completed_by_user);
455 dbus_free (connections);
457 connection_data_slot_unref ();
463 bus_connections_ref (BusConnections *connections)
465 _dbus_assert (connections->refcount > 0);
466 connections->refcount += 1;
470 bus_connections_unref (BusConnections *connections)
472 _dbus_assert (connections->refcount > 0);
473 connections->refcount -= 1;
474 if (connections->refcount == 0)
476 /* drop all incomplete */
477 while (connections->incomplete != NULL)
479 DBusConnection *connection;
481 connection = connections->incomplete->data;
483 dbus_connection_ref (connection);
484 dbus_connection_disconnect (connection);
485 bus_connection_disconnected (connection);
486 dbus_connection_unref (connection);
489 _dbus_assert (connections->n_incomplete == 0);
491 /* drop all real connections */
492 while (connections->completed != NULL)
494 DBusConnection *connection;
496 connection = connections->completed->data;
498 dbus_connection_ref (connection);
499 dbus_connection_disconnect (connection);
500 bus_connection_disconnected (connection);
501 dbus_connection_unref (connection);
504 _dbus_assert (connections->n_completed == 0);
506 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
507 connections->expire_timeout,
508 call_timeout_callback, NULL);
510 _dbus_timeout_unref (connections->expire_timeout);
512 _dbus_hash_table_unref (connections->completed_by_user);
514 dbus_free (connections);
516 connection_data_slot_unref ();
521 bus_connections_setup_connection (BusConnections *connections,
522 DBusConnection *connection)
524 BusConnectionData *d;
527 d = dbus_new0 (BusConnectionData, 1);
532 d->connections = connections;
533 d->connection = connection;
535 _dbus_get_current_time (&d->connection_tv_sec,
536 &d->connection_tv_usec);
538 _dbus_assert (connection_data_slot >= 0);
540 if (!dbus_connection_set_data (connection,
541 connection_data_slot,
542 d, free_connection_data))
550 if (!dbus_connection_set_watch_functions (connection,
551 add_connection_watch,
552 remove_connection_watch,
558 if (!dbus_connection_set_timeout_functions (connection,
559 add_connection_timeout,
560 remove_connection_timeout,
565 dbus_connection_set_unix_user_function (connection,
569 dbus_connection_set_dispatch_status_function (connection,
570 dispatch_status_function,
571 bus_context_get_loop (connections->context),
574 d->link_in_connection_list = _dbus_list_alloc_link (connection);
575 if (d->link_in_connection_list == NULL)
578 /* Setup the connection with the dispatcher */
579 if (!bus_dispatch_add_connection (connection))
582 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
584 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
586 bus_dispatch_remove_connection (connection);
591 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
592 connections->n_incomplete += 1;
594 dbus_connection_ref (connection);
596 /* Note that we might disconnect ourselves here, but it only takes
597 * effect on return to the main loop. We call this to free up
598 * expired connections if possible, and to queue the timeout for our
601 bus_connections_expire_incomplete (connections);
603 /* And we might also disconnect ourselves here, but again it
604 * only takes effect on return to main loop.
606 if (connections->n_incomplete >
607 bus_context_get_max_incomplete_connections (connections->context))
609 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
611 _dbus_assert (connections->incomplete != NULL);
612 /* Disconnect the oldest unauthenticated connection. FIXME
613 * would it be more secure to drop a *random* connection? This
614 * algorithm seems to mean that if someone can create new
615 * connections quickly enough, they can keep anyone else from
616 * completing authentication. But random may or may not really
617 * help with that, a more elaborate solution might be required.
619 dbus_connection_disconnect (connections->incomplete->data);
627 if (!dbus_connection_set_watch_functions (connection,
631 _dbus_assert_not_reached ("setting watch functions to NULL failed");
633 if (!dbus_connection_set_timeout_functions (connection,
637 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
639 dbus_connection_set_unix_user_function (connection,
642 dbus_connection_set_dispatch_status_function (connection,
645 if (!dbus_connection_set_data (connection,
646 connection_data_slot,
648 _dbus_assert_not_reached ("failed to set connection data to null");
650 if (d->link_in_connection_list != NULL)
652 _dbus_assert (d->link_in_connection_list->next == NULL);
653 _dbus_assert (d->link_in_connection_list->prev == NULL);
654 _dbus_list_free_link (d->link_in_connection_list);
662 bus_connections_expire_incomplete (BusConnections *connections)
665 long tv_sec, tv_usec;
669 if (connections->incomplete == NULL)
672 _dbus_get_current_time (&tv_sec, &tv_usec);
673 auth_timeout = bus_context_get_auth_timeout (connections->context);
676 link = _dbus_list_get_first_link (&connections->incomplete);
679 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
680 DBusConnection *connection;
681 BusConnectionData *d;
684 connection = link->data;
686 d = BUS_CONNECTION_DATA (connection);
688 _dbus_assert (d != NULL);
690 elapsed = ((double) tv_sec - (double) d->connection_tv_sec) * 1000.0 +
691 ((double) tv_usec - (double) d->connection_tv_usec) / 1000.0;
693 if (elapsed > (double) auth_timeout)
695 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
696 dbus_connection_disconnect (connection);
700 /* We can end the loop, since the connections are in oldest-first order */
701 next_interval = auth_timeout - (int) elapsed;
702 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
703 connection, next_interval);
711 if (next_interval >= 0)
713 _dbus_timeout_set_interval (connections->expire_timeout,
715 _dbus_timeout_set_enabled (connections->expire_timeout, TRUE);
717 _dbus_verbose ("Enabled incomplete connections timeout with interval %d, %d incomplete connections\n",
718 next_interval, connections->n_incomplete);
722 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
724 _dbus_verbose ("Disabled incomplete connections timeout, %d incomplete connections\n",
725 connections->n_incomplete);
730 expire_incomplete_timeout (void *data)
732 BusConnections *connections = data;
734 /* note that this may remove the timeout */
735 bus_connections_expire_incomplete (connections);
741 bus_connection_get_groups (DBusConnection *connection,
742 unsigned long **groups,
745 BusConnectionData *d;
747 DBusUserDatabase *user_database;
749 d = BUS_CONNECTION_DATA (connection);
751 _dbus_assert (d != NULL);
753 user_database = bus_context_get_user_database (d->connections->context);
758 if (dbus_connection_get_unix_user (connection, &uid))
760 if (!_dbus_user_database_get_groups (user_database,
761 uid, groups, n_groups,
764 _dbus_verbose ("Did not get any groups for UID %lu\n",
770 _dbus_verbose ("Got %d groups for UID %lu\n",
776 return TRUE; /* successfully got 0 groups */
780 bus_connection_is_in_group (DBusConnection *connection,
784 unsigned long *group_ids;
787 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
791 while (i < n_group_ids)
793 if (group_ids[i] == gid)
795 dbus_free (group_ids);
801 dbus_free (group_ids);
806 bus_connection_get_policy (DBusConnection *connection)
808 BusConnectionData *d;
810 d = BUS_CONNECTION_DATA (connection);
812 _dbus_assert (d != NULL);
814 if (!dbus_connection_get_is_authenticated (connection))
816 _dbus_verbose ("Tried to get policy for unauthenticated connection!\n");
820 /* We do lazy creation of the policy because
821 * it can only be done post-authentication.
823 if (d->policy == NULL)
826 bus_context_create_client_policy (d->connections->context,
829 /* we may have a NULL policy on OOM or error getting list of
830 * groups for a user. In the latter case we don't handle it so
831 * well currently, just keep pretending we're out of memory,
832 * which is kind of bizarre.
840 foreach_active (BusConnections *connections,
841 BusConnectionForeachFunction function,
846 link = _dbus_list_get_first_link (&connections->completed);
849 DBusConnection *connection = link->data;
850 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
852 if (!(* function) (connection, data))
862 foreach_inactive (BusConnections *connections,
863 BusConnectionForeachFunction function,
868 link = _dbus_list_get_first_link (&connections->incomplete);
871 DBusConnection *connection = link->data;
872 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
874 if (!(* function) (connection, data))
884 * Calls function on each active connection; if the function returns
885 * #FALSE, stops iterating. Active connections are authenticated
886 * and have sent a Hello message.
888 * @param connections the connections object
889 * @param function the function
890 * @param data data to pass to it as a second arg
893 bus_connections_foreach_active (BusConnections *connections,
894 BusConnectionForeachFunction function,
897 foreach_active (connections, function, data);
901 * Calls function on each connection; if the function returns
902 * #FALSE, stops iterating.
904 * @param connections the connections object
905 * @param function the function
906 * @param data data to pass to it as a second arg
909 bus_connections_foreach (BusConnections *connections,
910 BusConnectionForeachFunction function,
913 if (!foreach_active (connections, function, data))
916 foreach_inactive (connections, function, data);
920 bus_connections_get_context (BusConnections *connections)
922 return connections->context;
926 bus_connection_get_context (DBusConnection *connection)
928 BusConnectionData *d;
930 d = BUS_CONNECTION_DATA (connection);
932 _dbus_assert (d != NULL);
934 return d->connections->context;
938 bus_connection_get_connections (DBusConnection *connection)
940 BusConnectionData *d;
942 d = BUS_CONNECTION_DATA (connection);
944 _dbus_assert (d != NULL);
946 return d->connections;
950 bus_connection_get_registry (DBusConnection *connection)
952 BusConnectionData *d;
954 d = BUS_CONNECTION_DATA (connection);
956 _dbus_assert (d != NULL);
958 return bus_context_get_registry (d->connections->context);
962 bus_connection_get_activation (DBusConnection *connection)
964 BusConnectionData *d;
966 d = BUS_CONNECTION_DATA (connection);
968 _dbus_assert (d != NULL);
970 return bus_context_get_activation (d->connections->context);
974 * Checks whether the connection is registered with the message bus.
976 * @param connection the connection
977 * @returns #TRUE if we're an active message bus participant
980 bus_connection_is_active (DBusConnection *connection)
982 BusConnectionData *d;
984 d = BUS_CONNECTION_DATA (connection);
986 return d != NULL && d->name != NULL;
990 bus_connection_preallocate_oom_error (DBusConnection *connection)
992 DBusMessage *message;
993 DBusPreallocatedSend *preallocated;
994 BusConnectionData *d;
996 d = BUS_CONNECTION_DATA (connection);
998 _dbus_assert (d != NULL);
1000 if (d->oom_preallocated != NULL)
1003 preallocated = dbus_connection_preallocate_send (connection);
1004 if (preallocated == NULL)
1007 /* d->name may be NULL, but that is OK */
1008 message = dbus_message_new (DBUS_ERROR_NO_MEMORY,
1010 if (message == NULL)
1012 dbus_connection_free_preallocated_send (connection, preallocated);
1016 dbus_message_set_is_error (message, TRUE);
1018 if (!dbus_message_set_sender (message,
1021 dbus_connection_free_preallocated_send (connection, preallocated);
1022 dbus_message_unref (message);
1026 /* set reply serial to placeholder value just so space is already allocated
1029 if (!dbus_message_set_reply_serial (message, 14))
1031 dbus_connection_free_preallocated_send (connection, preallocated);
1032 dbus_message_unref (message);
1036 d->oom_message = message;
1037 d->oom_preallocated = preallocated;
1043 bus_connection_send_oom_error (DBusConnection *connection,
1044 DBusMessage *in_reply_to)
1046 BusConnectionData *d;
1048 d = BUS_CONNECTION_DATA (connection);
1050 _dbus_assert (d != NULL);
1051 _dbus_assert (d->oom_message != NULL);
1053 /* should always succeed since we set it to a placeholder earlier */
1054 if (!dbus_message_set_reply_serial (d->oom_message,
1055 dbus_message_get_serial (in_reply_to)))
1056 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1058 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1060 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1061 d->oom_message, NULL);
1063 dbus_message_unref (d->oom_message);
1064 d->oom_message = NULL;
1065 d->oom_preallocated = NULL;
1069 bus_connection_add_owned_service_link (DBusConnection *connection,
1072 BusConnectionData *d;
1074 d = BUS_CONNECTION_DATA (connection);
1075 _dbus_assert (d != NULL);
1077 _dbus_list_append_link (&d->services_owned, link);
1081 bus_connection_add_owned_service (DBusConnection *connection,
1082 BusService *service)
1086 link = _dbus_list_alloc_link (service);
1091 bus_connection_add_owned_service_link (connection, link);
1097 bus_connection_remove_owned_service (DBusConnection *connection,
1098 BusService *service)
1100 BusConnectionData *d;
1102 d = BUS_CONNECTION_DATA (connection);
1103 _dbus_assert (d != NULL);
1105 _dbus_list_remove_last (&d->services_owned, service);
1109 bus_connection_set_name (DBusConnection *connection,
1110 const DBusString *name)
1112 BusConnectionData *d;
1115 d = BUS_CONNECTION_DATA (connection);
1116 _dbus_assert (d != NULL);
1117 _dbus_assert (d->name == NULL);
1119 if (!_dbus_string_copy_data (name, &d->name))
1122 _dbus_assert (d->name != NULL);
1124 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1126 if (dbus_connection_get_unix_user (connection, &uid))
1128 if (!adjust_connections_for_uid (d->connections,
1131 dbus_free (d->name);
1137 /* Now the connection is active, move it between lists */
1138 _dbus_list_unlink (&d->connections->incomplete,
1139 d->link_in_connection_list);
1140 d->connections->n_incomplete -= 1;
1141 _dbus_list_append_link (&d->connections->completed,
1142 d->link_in_connection_list);
1143 d->connections->n_completed += 1;
1145 _dbus_assert (d->connections->n_incomplete >= 0);
1146 _dbus_assert (d->connections->n_completed > 0);
1152 bus_connection_get_name (DBusConnection *connection)
1154 BusConnectionData *d;
1156 d = BUS_CONNECTION_DATA (connection);
1157 _dbus_assert (d != NULL);
1163 * Check whether completing the passed-in connection would
1164 * exceed limits, and if so set error and return #FALSE
1167 bus_connections_check_limits (BusConnections *connections,
1168 DBusConnection *requesting_completion,
1171 BusConnectionData *d;
1174 d = BUS_CONNECTION_DATA (requesting_completion);
1175 _dbus_assert (d != NULL);
1177 _dbus_assert (d->name == NULL);
1179 if (connections->n_completed >=
1180 bus_context_get_max_completed_connections (connections->context))
1182 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1183 "The maximum number of active connections has been reached");
1187 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1189 if (get_connections_for_uid (connections, uid) >=
1190 bus_context_get_max_connections_per_user (connections->context))
1192 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1193 "The maximum number of active connections for UID %lu has been reached",
1206 * Note that this is fairly fragile; in particular, don't try to use
1207 * one transaction across any main loop iterations.
1212 BusTransaction *transaction;
1213 DBusMessage *message;
1214 DBusPreallocatedSend *preallocated;
1219 BusTransactionCancelFunction cancel_function;
1220 DBusFreeFunction free_data_function;
1224 struct BusTransaction
1226 DBusList *connections;
1227 BusContext *context;
1228 DBusList *cancel_hooks;
1232 message_to_send_free (DBusConnection *connection,
1233 MessageToSend *to_send)
1235 if (to_send->message)
1236 dbus_message_unref (to_send->message);
1238 if (to_send->preallocated)
1239 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1241 dbus_free (to_send);
1245 cancel_hook_cancel (void *element,
1248 CancelHook *ch = element;
1250 _dbus_verbose ("Running transaction cancel hook\n");
1252 if (ch->cancel_function)
1253 (* ch->cancel_function) (ch->data);
1257 cancel_hook_free (void *element,
1260 CancelHook *ch = element;
1262 if (ch->free_data_function)
1263 (* ch->free_data_function) (ch->data);
1269 free_cancel_hooks (BusTransaction *transaction)
1271 _dbus_list_foreach (&transaction->cancel_hooks,
1272 cancel_hook_free, NULL);
1274 _dbus_list_clear (&transaction->cancel_hooks);
1278 bus_transaction_new (BusContext *context)
1280 BusTransaction *transaction;
1282 transaction = dbus_new0 (BusTransaction, 1);
1283 if (transaction == NULL)
1286 transaction->context = context;
1292 bus_transaction_get_context (BusTransaction *transaction)
1294 return transaction->context;
1298 bus_transaction_get_connections (BusTransaction *transaction)
1300 return bus_context_get_connections (transaction->context);
1304 bus_transaction_send_from_driver (BusTransaction *transaction,
1305 DBusConnection *connection,
1306 DBusMessage *message)
1308 /* We have to set the sender to the driver, and have
1309 * to check security policy since it was not done in
1312 _dbus_verbose ("Sending %s from driver\n",
1313 dbus_message_get_name (message));
1315 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
1318 /* If security policy doesn't allow the message, we silently
1319 * eat it; the driver doesn't care about getting a reply.
1321 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
1322 NULL, connection, message, NULL))
1325 return bus_transaction_send (transaction, connection, message);
1329 bus_transaction_send (BusTransaction *transaction,
1330 DBusConnection *connection,
1331 DBusMessage *message)
1333 MessageToSend *to_send;
1334 BusConnectionData *d;
1337 _dbus_verbose (" trying to add %s %s to transaction%s\n",
1338 dbus_message_get_is_error (message) ? "error" :
1339 dbus_message_get_reply_serial (message) != 0 ? "reply" :
1341 dbus_message_get_name (message),
1342 dbus_connection_get_is_connected (connection) ?
1343 "" : " (disconnected)");
1345 _dbus_assert (dbus_message_get_sender (message) != NULL);
1347 if (!dbus_connection_get_is_connected (connection))
1348 return TRUE; /* silently ignore disconnected connections */
1350 d = BUS_CONNECTION_DATA (connection);
1351 _dbus_assert (d != NULL);
1353 to_send = dbus_new (MessageToSend, 1);
1354 if (to_send == NULL)
1359 to_send->preallocated = dbus_connection_preallocate_send (connection);
1360 if (to_send->preallocated == NULL)
1362 dbus_free (to_send);
1366 dbus_message_ref (message);
1367 to_send->message = message;
1368 to_send->transaction = transaction;
1370 _dbus_verbose ("about to prepend message\n");
1372 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
1374 message_to_send_free (connection, to_send);
1378 _dbus_verbose ("prepended message\n");
1380 /* See if we already had this connection in the list
1381 * for this transaction. If we have a pending message,
1382 * then we should already be in transaction->connections
1384 link = _dbus_list_get_first_link (&d->transaction_messages);
1385 _dbus_assert (link->data == to_send);
1386 link = _dbus_list_get_next_link (&d->transaction_messages, link);
1387 while (link != NULL)
1389 MessageToSend *m = link->data;
1390 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1392 if (m->transaction == transaction)
1400 if (!_dbus_list_prepend (&transaction->connections, connection))
1402 _dbus_list_remove (&d->transaction_messages, to_send);
1403 message_to_send_free (connection, to_send);
1412 connection_cancel_transaction (DBusConnection *connection,
1413 BusTransaction *transaction)
1416 BusConnectionData *d;
1418 d = BUS_CONNECTION_DATA (connection);
1419 _dbus_assert (d != NULL);
1421 link = _dbus_list_get_first_link (&d->transaction_messages);
1422 while (link != NULL)
1424 MessageToSend *m = link->data;
1425 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1427 if (m->transaction == transaction)
1429 _dbus_list_remove_link (&d->transaction_messages,
1432 message_to_send_free (connection, m);
1440 bus_transaction_cancel_and_free (BusTransaction *transaction)
1442 DBusConnection *connection;
1444 _dbus_verbose ("TRANSACTION: cancelled\n");
1446 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1447 connection_cancel_transaction (connection, transaction);
1449 _dbus_assert (transaction->connections == NULL);
1451 _dbus_list_foreach (&transaction->cancel_hooks,
1452 cancel_hook_cancel, NULL);
1454 free_cancel_hooks (transaction);
1456 dbus_free (transaction);
1460 connection_execute_transaction (DBusConnection *connection,
1461 BusTransaction *transaction)
1464 BusConnectionData *d;
1466 d = BUS_CONNECTION_DATA (connection);
1467 _dbus_assert (d != NULL);
1469 /* Send the queue in order (FIFO) */
1470 link = _dbus_list_get_last_link (&d->transaction_messages);
1471 while (link != NULL)
1473 MessageToSend *m = link->data;
1474 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
1476 if (m->transaction == transaction)
1478 _dbus_list_remove_link (&d->transaction_messages,
1481 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
1483 dbus_connection_send_preallocated (connection,
1488 m->preallocated = NULL; /* so we don't double-free it */
1490 message_to_send_free (connection, m);
1498 bus_transaction_execute_and_free (BusTransaction *transaction)
1500 /* For each connection in transaction->connections
1503 DBusConnection *connection;
1505 _dbus_verbose ("TRANSACTION: executing\n");
1507 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1508 connection_execute_transaction (connection, transaction);
1510 _dbus_assert (transaction->connections == NULL);
1512 free_cancel_hooks (transaction);
1514 dbus_free (transaction);
1518 bus_connection_remove_transactions (DBusConnection *connection)
1520 MessageToSend *to_send;
1521 BusConnectionData *d;
1523 d = BUS_CONNECTION_DATA (connection);
1524 _dbus_assert (d != NULL);
1526 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
1528 /* only has an effect for the first MessageToSend listing this transaction */
1529 _dbus_list_remove (&to_send->transaction->connections,
1532 _dbus_list_remove (&d->transaction_messages, to_send);
1533 message_to_send_free (connection, to_send);
1538 * Converts the DBusError to a message reply
1541 bus_transaction_send_error_reply (BusTransaction *transaction,
1542 DBusConnection *connection,
1543 const DBusError *error,
1544 DBusMessage *in_reply_to)
1548 _dbus_assert (error != NULL);
1549 _DBUS_ASSERT_ERROR_IS_SET (error);
1551 reply = dbus_message_new_error_reply (in_reply_to,
1557 if (!bus_transaction_send_from_driver (transaction, connection, reply))
1559 dbus_message_unref (reply);
1563 dbus_message_unref (reply);
1569 bus_transaction_add_cancel_hook (BusTransaction *transaction,
1570 BusTransactionCancelFunction cancel_function,
1572 DBusFreeFunction free_data_function)
1576 ch = dbus_new (CancelHook, 1);
1580 ch->cancel_function = cancel_function;
1582 ch->free_data_function = free_data_function;
1584 /* It's important that the hooks get run in reverse order that they
1587 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))