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 */
58 BusExpireList *pending_replies; /**< List of pending replies */
61 static dbus_int32_t connection_data_slot = -1;
65 BusConnections *connections;
66 DBusList *link_in_connection_list;
67 DBusConnection *connection;
68 DBusList *services_owned;
70 DBusList *match_rules;
73 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
74 DBusMessage *oom_message;
75 DBusPreallocatedSend *oom_preallocated;
76 BusClientPolicy *policy;
78 long connection_tv_sec; /**< Time when we connected (seconds component) */
79 long connection_tv_usec; /**< Time when we connected (microsec component) */
80 int stamp; /**< connections->stamp last time we were traversed */
83 static void bus_pending_reply_expired (BusExpireList *list,
87 static void bus_connection_drop_pending_replies (BusConnections *connections,
88 DBusConnection *connection);
90 static dbus_bool_t expire_incomplete_timeout (void *data);
92 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
95 connection_get_loop (DBusConnection *connection)
99 d = BUS_CONNECTION_DATA (connection);
101 return bus_context_get_loop (d->connections->context);
106 get_connections_for_uid (BusConnections *connections,
112 /* val is NULL is 0 when it isn't in the hash yet */
114 val = _dbus_hash_table_lookup_ulong (connections->completed_by_user,
117 current_count = _DBUS_POINTER_TO_INT (val);
119 return current_count;
123 adjust_connections_for_uid (BusConnections *connections,
129 current_count = get_connections_for_uid (connections, uid);
131 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
132 ": was %d adjustment %d making %d\n",
133 uid, current_count, adjustment, current_count + adjustment);
135 _dbus_assert (current_count >= 0);
137 current_count += adjustment;
139 _dbus_assert (current_count >= 0);
141 if (current_count == 0)
143 _dbus_hash_table_remove_ulong (connections->completed_by_user, uid);
150 retval = _dbus_hash_table_insert_ulong (connections->completed_by_user,
151 uid, _DBUS_INT_TO_POINTER (current_count));
153 /* only positive adjustment can fail as otherwise
154 * a hash entry should already exist
156 _dbus_assert (adjustment > 0 ||
157 (adjustment <= 0 && retval));
164 bus_connection_disconnected (DBusConnection *connection)
166 BusConnectionData *d;
168 BusMatchmaker *matchmaker;
170 d = BUS_CONNECTION_DATA (connection);
171 _dbus_assert (d != NULL);
173 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
174 d->name ? d->name : "(inactive)");
176 /* Delete our match rules */
177 if (d->n_match_rules > 0)
179 matchmaker = bus_context_get_matchmaker (d->connections->context);
180 bus_matchmaker_disconnected (matchmaker, connection);
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 bus_connection_drop_pending_replies (d->connections, connection);
282 /* frees "d" as side effect */
283 dbus_connection_set_data (connection,
284 connection_data_slot,
287 dbus_connection_unref (connection);
291 connection_watch_callback (DBusWatch *watch,
292 unsigned int condition,
295 /* FIXME this can be done in dbus-mainloop.c
296 * if the code in activation.c for the babysitter
297 * watch handler is fixed.
301 _dbus_verbose ("Calling handle_watch\n");
303 return dbus_watch_handle (watch, condition);
307 add_connection_watch (DBusWatch *watch,
310 DBusConnection *connection = data;
312 return _dbus_loop_add_watch (connection_get_loop (connection),
313 watch, connection_watch_callback, connection,
318 remove_connection_watch (DBusWatch *watch,
321 DBusConnection *connection = data;
323 _dbus_loop_remove_watch (connection_get_loop (connection),
324 watch, connection_watch_callback, connection);
328 connection_timeout_callback (DBusTimeout *timeout,
331 /* DBusConnection *connection = data; */
333 /* can return FALSE on OOM but we just let it fire again later */
334 dbus_timeout_handle (timeout);
338 add_connection_timeout (DBusTimeout *timeout,
341 DBusConnection *connection = data;
343 return _dbus_loop_add_timeout (connection_get_loop (connection),
344 timeout, connection_timeout_callback, connection, NULL);
348 remove_connection_timeout (DBusTimeout *timeout,
351 DBusConnection *connection = data;
353 _dbus_loop_remove_timeout (connection_get_loop (connection),
354 timeout, connection_timeout_callback, connection);
358 dispatch_status_function (DBusConnection *connection,
359 DBusDispatchStatus new_status,
362 DBusLoop *loop = data;
364 if (new_status != DBUS_DISPATCH_COMPLETE)
366 while (!_dbus_loop_queue_dispatch (loop, connection))
367 _dbus_wait_for_memory ();
372 allow_user_function (DBusConnection *connection,
376 BusConnectionData *d;
378 d = BUS_CONNECTION_DATA (connection);
380 _dbus_assert (d != NULL);
382 return bus_context_allow_user (d->connections->context, uid);
386 free_connection_data (void *data)
388 BusConnectionData *d = data;
390 /* services_owned should be NULL since we should be disconnected */
391 _dbus_assert (d->services_owned == NULL);
392 _dbus_assert (d->n_services_owned == 0);
394 _dbus_assert (d->transaction_messages == NULL);
396 if (d->oom_preallocated)
397 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
400 dbus_message_unref (d->oom_message);
403 bus_client_policy_unref (d->policy);
411 call_timeout_callback (DBusTimeout *timeout,
414 /* can return FALSE on OOM but we just let it fire again later */
415 dbus_timeout_handle (timeout);
419 bus_connections_new (BusContext *context)
421 BusConnections *connections;
423 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
426 connections = dbus_new0 (BusConnections, 1);
427 if (connections == NULL)
430 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_ULONG,
432 if (connections->completed_by_user == NULL)
435 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
436 expire_incomplete_timeout,
438 if (connections->expire_timeout == NULL)
441 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
443 connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
444 bus_context_get_reply_timeout (context),
445 bus_pending_reply_expired,
447 if (connections->pending_replies == NULL)
450 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
451 connections->expire_timeout,
452 call_timeout_callback, NULL, NULL))
455 connections->refcount = 1;
456 connections->context = context;
461 bus_expire_list_free (connections->pending_replies);
463 _dbus_timeout_unref (connections->expire_timeout);
465 _dbus_hash_table_unref (connections->completed_by_user);
467 dbus_free (connections);
469 dbus_connection_free_data_slot (&connection_data_slot);
475 bus_connections_ref (BusConnections *connections)
477 _dbus_assert (connections->refcount > 0);
478 connections->refcount += 1;
482 bus_connections_unref (BusConnections *connections)
484 _dbus_assert (connections->refcount > 0);
485 connections->refcount -= 1;
486 if (connections->refcount == 0)
488 /* drop all incomplete */
489 while (connections->incomplete != NULL)
491 DBusConnection *connection;
493 connection = connections->incomplete->data;
495 dbus_connection_ref (connection);
496 dbus_connection_disconnect (connection);
497 bus_connection_disconnected (connection);
498 dbus_connection_unref (connection);
501 _dbus_assert (connections->n_incomplete == 0);
503 /* drop all real connections */
504 while (connections->completed != NULL)
506 DBusConnection *connection;
508 connection = connections->completed->data;
510 dbus_connection_ref (connection);
511 dbus_connection_disconnect (connection);
512 bus_connection_disconnected (connection);
513 dbus_connection_unref (connection);
516 _dbus_assert (connections->n_completed == 0);
518 _dbus_assert (connections->pending_replies->n_items == 0);
519 bus_expire_list_free (connections->pending_replies);
521 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
522 connections->expire_timeout,
523 call_timeout_callback, NULL);
525 _dbus_timeout_unref (connections->expire_timeout);
527 _dbus_hash_table_unref (connections->completed_by_user);
529 dbus_free (connections);
531 dbus_connection_free_data_slot (&connection_data_slot);
536 bus_connections_setup_connection (BusConnections *connections,
537 DBusConnection *connection)
539 BusConnectionData *d;
542 d = dbus_new0 (BusConnectionData, 1);
547 d->connections = connections;
548 d->connection = connection;
550 _dbus_get_current_time (&d->connection_tv_sec,
551 &d->connection_tv_usec);
553 _dbus_assert (connection_data_slot >= 0);
555 if (!dbus_connection_set_data (connection,
556 connection_data_slot,
557 d, free_connection_data))
565 if (!dbus_connection_set_watch_functions (connection,
566 add_connection_watch,
567 remove_connection_watch,
573 if (!dbus_connection_set_timeout_functions (connection,
574 add_connection_timeout,
575 remove_connection_timeout,
580 dbus_connection_set_unix_user_function (connection,
584 dbus_connection_set_dispatch_status_function (connection,
585 dispatch_status_function,
586 bus_context_get_loop (connections->context),
589 d->link_in_connection_list = _dbus_list_alloc_link (connection);
590 if (d->link_in_connection_list == NULL)
593 /* Setup the connection with the dispatcher */
594 if (!bus_dispatch_add_connection (connection))
597 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
599 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
601 bus_dispatch_remove_connection (connection);
606 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
607 connections->n_incomplete += 1;
609 dbus_connection_ref (connection);
611 /* Note that we might disconnect ourselves here, but it only takes
612 * effect on return to the main loop. We call this to free up
613 * expired connections if possible, and to queue the timeout for our
616 bus_connections_expire_incomplete (connections);
618 /* And we might also disconnect ourselves here, but again it
619 * only takes effect on return to main loop.
621 if (connections->n_incomplete >
622 bus_context_get_max_incomplete_connections (connections->context))
624 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
626 _dbus_assert (connections->incomplete != NULL);
627 /* Disconnect the oldest unauthenticated connection. FIXME
628 * would it be more secure to drop a *random* connection? This
629 * algorithm seems to mean that if someone can create new
630 * connections quickly enough, they can keep anyone else from
631 * completing authentication. But random may or may not really
632 * help with that, a more elaborate solution might be required.
634 dbus_connection_disconnect (connections->incomplete->data);
642 if (!dbus_connection_set_watch_functions (connection,
646 _dbus_assert_not_reached ("setting watch functions to NULL failed");
648 if (!dbus_connection_set_timeout_functions (connection,
652 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
654 dbus_connection_set_unix_user_function (connection,
657 dbus_connection_set_dispatch_status_function (connection,
660 if (d->link_in_connection_list != NULL)
662 _dbus_assert (d->link_in_connection_list->next == NULL);
663 _dbus_assert (d->link_in_connection_list->prev == NULL);
664 _dbus_list_free_link (d->link_in_connection_list);
665 d->link_in_connection_list = NULL;
668 if (!dbus_connection_set_data (connection,
669 connection_data_slot,
671 _dbus_assert_not_reached ("failed to set connection data to null");
673 /* "d" has now been freed */
680 bus_connections_expire_incomplete (BusConnections *connections)
686 if (connections->incomplete != NULL)
688 long tv_sec, tv_usec;
692 _dbus_get_current_time (&tv_sec, &tv_usec);
693 auth_timeout = bus_context_get_auth_timeout (connections->context);
695 link = _dbus_list_get_first_link (&connections->incomplete);
698 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
699 DBusConnection *connection;
700 BusConnectionData *d;
703 connection = link->data;
705 d = BUS_CONNECTION_DATA (connection);
707 _dbus_assert (d != NULL);
709 elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
710 d->connection_tv_usec,
713 if (elapsed >= (double) auth_timeout)
715 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
716 dbus_connection_disconnect (connection);
720 /* We can end the loop, since the connections are in oldest-first order */
721 next_interval = ((double)auth_timeout) - elapsed;
722 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
723 connection, next_interval);
732 bus_expire_timeout_set_interval (connections->expire_timeout,
737 expire_incomplete_timeout (void *data)
739 BusConnections *connections = data;
741 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
743 /* note that this may remove the timeout */
744 bus_connections_expire_incomplete (connections);
750 bus_connection_get_groups (DBusConnection *connection,
751 unsigned long **groups,
755 BusConnectionData *d;
757 DBusUserDatabase *user_database;
759 d = BUS_CONNECTION_DATA (connection);
761 _dbus_assert (d != NULL);
763 user_database = bus_context_get_user_database (d->connections->context);
768 if (dbus_connection_get_unix_user (connection, &uid))
770 if (!_dbus_user_database_get_groups (user_database,
771 uid, groups, n_groups,
774 _DBUS_ASSERT_ERROR_IS_SET (error);
775 _dbus_verbose ("Did not get any groups for UID %lu\n",
781 _dbus_verbose ("Got %d groups for UID %lu\n",
787 return TRUE; /* successfully got 0 groups */
791 bus_connection_is_in_group (DBusConnection *connection,
795 unsigned long *group_ids;
798 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids,
803 while (i < n_group_ids)
805 if (group_ids[i] == gid)
807 dbus_free (group_ids);
813 dbus_free (group_ids);
818 bus_connection_get_policy (DBusConnection *connection)
820 BusConnectionData *d;
822 d = BUS_CONNECTION_DATA (connection);
824 _dbus_assert (d != NULL);
825 _dbus_assert (d->policy != NULL);
831 foreach_active (BusConnections *connections,
832 BusConnectionForeachFunction function,
837 link = _dbus_list_get_first_link (&connections->completed);
840 DBusConnection *connection = link->data;
841 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
843 if (!(* function) (connection, data))
853 foreach_inactive (BusConnections *connections,
854 BusConnectionForeachFunction function,
859 link = _dbus_list_get_first_link (&connections->incomplete);
862 DBusConnection *connection = link->data;
863 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
865 if (!(* function) (connection, data))
875 * Calls function on each active connection; if the function returns
876 * #FALSE, stops iterating. Active connections are authenticated
877 * and have sent a Hello message.
879 * @param connections the connections object
880 * @param function the function
881 * @param data data to pass to it as a second arg
884 bus_connections_foreach_active (BusConnections *connections,
885 BusConnectionForeachFunction function,
888 foreach_active (connections, function, data);
892 * Calls function on each connection; if the function returns
893 * #FALSE, stops iterating.
895 * @param connections the connections object
896 * @param function the function
897 * @param data data to pass to it as a second arg
900 bus_connections_foreach (BusConnections *connections,
901 BusConnectionForeachFunction function,
904 if (!foreach_active (connections, function, data))
907 foreach_inactive (connections, function, data);
911 bus_connections_get_context (BusConnections *connections)
913 return connections->context;
917 * This is used to avoid covering the same connection twice when
918 * traversing connections. Note that it assumes we will
919 * bus_connection_mark_stamp() each connection at least once per
920 * INT_MAX increments of the global stamp, or wraparound would break
924 bus_connections_increment_stamp (BusConnections *connections)
926 connections->stamp += 1;
929 /* Mark connection with current stamp, return TRUE if it
930 * didn't already have that stamp
933 bus_connection_mark_stamp (DBusConnection *connection)
935 BusConnectionData *d;
937 d = BUS_CONNECTION_DATA (connection);
939 _dbus_assert (d != NULL);
941 if (d->stamp == d->connections->stamp)
945 d->stamp = d->connections->stamp;
951 bus_connection_get_context (DBusConnection *connection)
953 BusConnectionData *d;
955 d = BUS_CONNECTION_DATA (connection);
957 _dbus_assert (d != NULL);
959 return d->connections->context;
963 bus_connection_get_connections (DBusConnection *connection)
965 BusConnectionData *d;
967 d = BUS_CONNECTION_DATA (connection);
969 _dbus_assert (d != NULL);
971 return d->connections;
975 bus_connection_get_registry (DBusConnection *connection)
977 BusConnectionData *d;
979 d = BUS_CONNECTION_DATA (connection);
981 _dbus_assert (d != NULL);
983 return bus_context_get_registry (d->connections->context);
987 bus_connection_get_activation (DBusConnection *connection)
989 BusConnectionData *d;
991 d = BUS_CONNECTION_DATA (connection);
993 _dbus_assert (d != NULL);
995 return bus_context_get_activation (d->connections->context);
999 bus_connection_get_matchmaker (DBusConnection *connection)
1001 BusConnectionData *d;
1003 d = BUS_CONNECTION_DATA (connection);
1005 _dbus_assert (d != NULL);
1007 return bus_context_get_matchmaker (d->connections->context);
1011 * Checks whether the connection is registered with the message bus.
1013 * @param connection the connection
1014 * @returns #TRUE if we're an active message bus participant
1017 bus_connection_is_active (DBusConnection *connection)
1019 BusConnectionData *d;
1021 d = BUS_CONNECTION_DATA (connection);
1023 return d != NULL && d->name != NULL;
1027 bus_connection_preallocate_oom_error (DBusConnection *connection)
1029 DBusMessage *message;
1030 DBusPreallocatedSend *preallocated;
1031 BusConnectionData *d;
1033 d = BUS_CONNECTION_DATA (connection);
1035 _dbus_assert (d != NULL);
1037 if (d->oom_preallocated != NULL)
1040 preallocated = dbus_connection_preallocate_send (connection);
1041 if (preallocated == NULL)
1044 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1046 if (message == NULL)
1048 dbus_connection_free_preallocated_send (connection, preallocated);
1052 /* d->name may be NULL, but that is OK */
1053 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1054 !dbus_message_set_destination (message, d->name) ||
1055 !dbus_message_set_sender (message,
1056 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1058 dbus_connection_free_preallocated_send (connection, preallocated);
1059 dbus_message_unref (message);
1063 /* set reply serial to placeholder value just so space is already allocated
1066 if (!dbus_message_set_reply_serial (message, 14))
1068 dbus_connection_free_preallocated_send (connection, preallocated);
1069 dbus_message_unref (message);
1073 d->oom_message = message;
1074 d->oom_preallocated = preallocated;
1080 bus_connection_send_oom_error (DBusConnection *connection,
1081 DBusMessage *in_reply_to)
1083 BusConnectionData *d;
1085 d = BUS_CONNECTION_DATA (connection);
1087 _dbus_assert (d != NULL);
1088 _dbus_assert (d->oom_message != NULL);
1090 /* should always succeed since we set it to a placeholder earlier */
1091 if (!dbus_message_set_reply_serial (d->oom_message,
1092 dbus_message_get_serial (in_reply_to)))
1093 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1095 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1097 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1098 d->oom_message, NULL);
1100 dbus_message_unref (d->oom_message);
1101 d->oom_message = NULL;
1102 d->oom_preallocated = NULL;
1106 bus_connection_add_match_rule_link (DBusConnection *connection,
1109 BusConnectionData *d;
1111 d = BUS_CONNECTION_DATA (connection);
1112 _dbus_assert (d != NULL);
1114 _dbus_list_append_link (&d->match_rules, link);
1116 d->n_match_rules += 1;
1120 bus_connection_add_match_rule (DBusConnection *connection,
1125 link = _dbus_list_alloc_link (rule);
1130 bus_connection_add_match_rule_link (connection, link);
1136 bus_connection_remove_match_rule (DBusConnection *connection,
1139 BusConnectionData *d;
1141 d = BUS_CONNECTION_DATA (connection);
1142 _dbus_assert (d != NULL);
1144 _dbus_list_remove_last (&d->match_rules, rule);
1146 d->n_match_rules -= 1;
1147 _dbus_assert (d->n_match_rules >= 0);
1151 bus_connection_get_n_match_rules (DBusConnection *connection)
1153 BusConnectionData *d;
1155 d = BUS_CONNECTION_DATA (connection);
1156 _dbus_assert (d != NULL);
1158 return d->n_match_rules;
1162 bus_connection_add_owned_service_link (DBusConnection *connection,
1165 BusConnectionData *d;
1167 d = BUS_CONNECTION_DATA (connection);
1168 _dbus_assert (d != NULL);
1170 _dbus_list_append_link (&d->services_owned, link);
1172 d->n_services_owned += 1;
1176 bus_connection_add_owned_service (DBusConnection *connection,
1177 BusService *service)
1181 link = _dbus_list_alloc_link (service);
1186 bus_connection_add_owned_service_link (connection, link);
1192 bus_connection_remove_owned_service (DBusConnection *connection,
1193 BusService *service)
1195 BusConnectionData *d;
1197 d = BUS_CONNECTION_DATA (connection);
1198 _dbus_assert (d != NULL);
1200 _dbus_list_remove_last (&d->services_owned, service);
1202 d->n_services_owned -= 1;
1203 _dbus_assert (d->n_services_owned >= 0);
1207 bus_connection_get_n_services_owned (DBusConnection *connection)
1209 BusConnectionData *d;
1211 d = BUS_CONNECTION_DATA (connection);
1212 _dbus_assert (d != NULL);
1214 return d->n_services_owned;
1218 bus_connection_complete (DBusConnection *connection,
1219 const DBusString *name,
1222 BusConnectionData *d;
1225 d = BUS_CONNECTION_DATA (connection);
1226 _dbus_assert (d != NULL);
1227 _dbus_assert (d->name == NULL);
1228 _dbus_assert (d->policy == NULL);
1230 _dbus_assert (!bus_connection_is_active (connection));
1232 if (!_dbus_string_copy_data (name, &d->name))
1234 BUS_SET_OOM (error);
1238 _dbus_assert (d->name != NULL);
1240 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1242 d->policy = bus_context_create_client_policy (d->connections->context,
1246 /* we may have a NULL policy on OOM or error getting list of
1247 * groups for a user. In the latter case we don't handle it so
1248 * well currently, as it will just keep failing over and over.
1251 if (d->policy == NULL)
1253 _dbus_verbose ("Failed to create security policy for connection %p\n",
1255 _DBUS_ASSERT_ERROR_IS_SET (error);
1256 dbus_free (d->name);
1261 if (dbus_connection_get_unix_user (connection, &uid))
1263 if (!adjust_connections_for_uid (d->connections,
1266 BUS_SET_OOM (error);
1267 dbus_free (d->name);
1273 /* Now the connection is active, move it between lists */
1274 _dbus_list_unlink (&d->connections->incomplete,
1275 d->link_in_connection_list);
1276 d->connections->n_incomplete -= 1;
1277 _dbus_list_append_link (&d->connections->completed,
1278 d->link_in_connection_list);
1279 d->connections->n_completed += 1;
1281 _dbus_assert (d->connections->n_incomplete >= 0);
1282 _dbus_assert (d->connections->n_completed > 0);
1284 /* See if we can remove the timeout */
1285 bus_connections_expire_incomplete (d->connections);
1287 _dbus_assert (bus_connection_is_active (connection));
1293 bus_connection_get_name (DBusConnection *connection)
1295 BusConnectionData *d;
1297 d = BUS_CONNECTION_DATA (connection);
1298 _dbus_assert (d != NULL);
1304 * Check whether completing the passed-in connection would
1305 * exceed limits, and if so set error and return #FALSE
1308 bus_connections_check_limits (BusConnections *connections,
1309 DBusConnection *requesting_completion,
1312 BusConnectionData *d;
1315 d = BUS_CONNECTION_DATA (requesting_completion);
1316 _dbus_assert (d != NULL);
1318 _dbus_assert (d->name == NULL);
1320 if (connections->n_completed >=
1321 bus_context_get_max_completed_connections (connections->context))
1323 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1324 "The maximum number of active connections has been reached");
1328 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1330 if (get_connections_for_uid (connections, uid) >=
1331 bus_context_get_max_connections_per_user (connections->context))
1333 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1334 "The maximum number of active connections for UID %lu has been reached",
1344 bus_pending_reply_send_no_reply (BusConnections *connections,
1345 BusTransaction *transaction,
1346 BusPendingReply *pending)
1348 DBusMessage *message;
1349 DBusMessageIter iter;
1354 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1355 if (message == NULL)
1358 dbus_message_set_no_reply (message, TRUE);
1360 if (!dbus_message_set_reply_serial (message,
1361 pending->reply_serial))
1364 if (!dbus_message_set_error_name (message,
1365 DBUS_ERROR_NO_REPLY))
1368 dbus_message_append_iter_init (message, &iter);
1369 if (!dbus_message_iter_append_string (&iter, "Message did not receive a reply (timeout by message bus)"))
1372 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1379 dbus_message_unref (message);
1384 bus_pending_reply_expired (BusExpireList *list,
1388 BusPendingReply *pending = link->data;
1389 BusConnections *connections = data;
1390 BusTransaction *transaction;
1392 /* No reply is forthcoming. So nuke it if we can. If not,
1393 * leave it in the list to try expiring again later when we
1396 transaction = bus_transaction_new (connections->context);
1397 if (transaction == NULL)
1400 if (bus_pending_reply_send_no_reply (connections,
1404 _dbus_list_remove_link (&connections->pending_replies->items,
1406 dbus_free (pending);
1407 bus_transaction_cancel_and_free (transaction);
1410 bus_transaction_execute_and_free (transaction);
1414 bus_connection_drop_pending_replies (BusConnections *connections,
1415 DBusConnection *connection)
1417 /* The DBusConnection is almost 100% finalized here, so you can't
1418 * do anything with it except check for pointer equality
1422 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1423 while (link != NULL)
1426 BusPendingReply *pending;
1428 next = _dbus_list_get_next_link (&connections->pending_replies->items,
1430 pending = link->data;
1432 if (pending->will_get_reply == connection)
1434 /* We don't need to track this pending reply anymore */
1436 _dbus_list_remove_link (&connections->pending_replies->items,
1438 dbus_free (pending);
1440 else if (pending->will_send_reply == connection)
1442 /* The reply isn't going to be sent, so set things
1443 * up so it will be expired right away
1446 pending->will_send_reply = NULL;
1447 pending->expire_item.added_tv_sec = 0;
1448 pending->expire_item.added_tv_usec = 0;
1450 bus_expire_timeout_set_interval (connections->pending_replies->timeout,
1461 BusPendingReply *pending;
1462 BusConnections *connections;
1463 } CancelPendingReplyData;
1466 cancel_pending_reply (void *data)
1468 CancelPendingReplyData *d = data;
1470 if (!_dbus_list_remove (&d->connections->pending_replies->items,
1472 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1474 dbus_free (d->pending); /* since it's been cancelled */
1478 cancel_pending_reply_data_free (void *data)
1480 CancelPendingReplyData *d = data;
1482 /* d->pending should be either freed or still
1483 * in the list of pending replies (owned by someone
1491 * Record that a reply is allowed; return TRUE on success.
1494 bus_connections_expect_reply (BusConnections *connections,
1495 BusTransaction *transaction,
1496 DBusConnection *will_get_reply,
1497 DBusConnection *will_send_reply,
1498 DBusMessage *reply_to_this,
1501 BusPendingReply *pending;
1502 dbus_uint32_t reply_serial;
1504 CancelPendingReplyData *cprd;
1506 _dbus_assert (will_get_reply != NULL);
1507 _dbus_assert (will_send_reply != NULL);
1508 _dbus_assert (reply_to_this != NULL);
1510 if (dbus_message_get_no_reply (reply_to_this))
1511 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1513 reply_serial = dbus_message_get_serial (reply_to_this);
1515 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1516 while (link != NULL)
1518 pending = link->data;
1520 if (pending->reply_serial == reply_serial &&
1521 pending->will_get_reply == will_get_reply &&
1522 pending->will_send_reply == will_send_reply)
1524 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1525 "Message has the same reply serial as a currently-outstanding existing method call");
1529 link = _dbus_list_get_next_link (&connections->pending_replies->items,
1533 pending = dbus_new0 (BusPendingReply, 1);
1534 if (pending == NULL)
1536 BUS_SET_OOM (error);
1540 cprd = dbus_new0 (CancelPendingReplyData, 1);
1543 BUS_SET_OOM (error);
1544 dbus_free (pending);
1548 if (!_dbus_list_prepend (&connections->pending_replies->items,
1551 BUS_SET_OOM (error);
1553 dbus_free (pending);
1557 if (!bus_transaction_add_cancel_hook (transaction,
1558 cancel_pending_reply,
1560 cancel_pending_reply_data_free))
1562 BUS_SET_OOM (error);
1563 _dbus_list_remove (&connections->pending_replies->items, pending);
1565 dbus_free (pending);
1569 cprd->pending = pending;
1570 cprd->connections = connections;
1572 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1573 &pending->expire_item.added_tv_usec);
1575 pending->will_get_reply = will_get_reply;
1576 pending->will_send_reply = will_send_reply;
1577 pending->reply_serial = reply_serial;
1585 BusConnections *connections;
1586 } CheckPendingReplyData;
1589 cancel_check_pending_reply (void *data)
1591 CheckPendingReplyData *d = data;
1593 _dbus_list_prepend_link (&d->connections->pending_replies->items,
1599 check_pending_reply_data_free (void *data)
1601 CheckPendingReplyData *d = data;
1603 if (d->link != NULL)
1605 BusPendingReply *pending = d->link->data;
1607 dbus_free (pending);
1608 _dbus_list_free_link (d->link);
1615 * Check whether a reply is allowed, remove BusPendingReply
1616 * if so, return TRUE if so.
1619 bus_connections_check_reply (BusConnections *connections,
1620 BusTransaction *transaction,
1621 DBusConnection *sending_reply,
1622 DBusConnection *receiving_reply,
1626 CheckPendingReplyData *cprd;
1628 dbus_uint32_t reply_serial;
1630 _dbus_assert (sending_reply != NULL);
1631 _dbus_assert (receiving_reply != NULL);
1633 reply_serial = dbus_message_get_reply_serial (reply);
1635 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1636 while (link != NULL)
1638 BusPendingReply *pending = link->data;
1640 if (pending->reply_serial == reply_serial &&
1641 pending->will_get_reply == receiving_reply &&
1642 pending->will_send_reply == sending_reply)
1644 _dbus_verbose ("Found pending reply\n");
1648 link = _dbus_list_get_next_link (&connections->pending_replies->items,
1654 _dbus_verbose ("No pending reply expected\n");
1659 cprd = dbus_new0 (CheckPendingReplyData, 1);
1662 BUS_SET_OOM (error);
1666 if (!bus_transaction_add_cancel_hook (transaction,
1667 cancel_check_pending_reply,
1669 check_pending_reply_data_free))
1671 BUS_SET_OOM (error);
1677 cprd->connections = connections;
1679 _dbus_list_remove_link (&connections->pending_replies->items,
1688 * Note that this is fairly fragile; in particular, don't try to use
1689 * one transaction across any main loop iterations.
1694 BusTransaction *transaction;
1695 DBusMessage *message;
1696 DBusPreallocatedSend *preallocated;
1701 BusTransactionCancelFunction cancel_function;
1702 DBusFreeFunction free_data_function;
1706 struct BusTransaction
1708 DBusList *connections;
1709 BusContext *context;
1710 DBusList *cancel_hooks;
1714 message_to_send_free (DBusConnection *connection,
1715 MessageToSend *to_send)
1717 if (to_send->message)
1718 dbus_message_unref (to_send->message);
1720 if (to_send->preallocated)
1721 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1723 dbus_free (to_send);
1727 cancel_hook_cancel (void *element,
1730 CancelHook *ch = element;
1732 _dbus_verbose ("Running transaction cancel hook\n");
1734 if (ch->cancel_function)
1735 (* ch->cancel_function) (ch->data);
1739 cancel_hook_free (void *element,
1742 CancelHook *ch = element;
1744 if (ch->free_data_function)
1745 (* ch->free_data_function) (ch->data);
1751 free_cancel_hooks (BusTransaction *transaction)
1753 _dbus_list_foreach (&transaction->cancel_hooks,
1754 cancel_hook_free, NULL);
1756 _dbus_list_clear (&transaction->cancel_hooks);
1760 bus_transaction_new (BusContext *context)
1762 BusTransaction *transaction;
1764 transaction = dbus_new0 (BusTransaction, 1);
1765 if (transaction == NULL)
1768 transaction->context = context;
1774 bus_transaction_get_context (BusTransaction *transaction)
1776 return transaction->context;
1780 bus_transaction_get_connections (BusTransaction *transaction)
1782 return bus_context_get_connections (transaction->context);
1786 bus_transaction_send_from_driver (BusTransaction *transaction,
1787 DBusConnection *connection,
1788 DBusMessage *message)
1790 /* We have to set the sender to the driver, and have
1791 * to check security policy since it was not done in
1794 _dbus_verbose ("Sending %s %s %s from driver\n",
1795 dbus_message_get_interface (message) ?
1796 dbus_message_get_interface (message) : "(no interface)",
1797 dbus_message_get_member (message) ?
1798 dbus_message_get_member (message) : "(no member)",
1799 dbus_message_get_error_name (message) ?
1800 dbus_message_get_error_name (message) : "(no error name)");
1802 if (!dbus_message_set_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1805 /* bus driver never wants a reply */
1806 dbus_message_set_no_reply (message, TRUE);
1808 /* If security policy doesn't allow the message, we silently
1809 * eat it; the driver doesn't care about getting a reply.
1811 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
1813 NULL, connection, connection, message, NULL))
1816 return bus_transaction_send (transaction, connection, message);
1820 bus_transaction_send (BusTransaction *transaction,
1821 DBusConnection *connection,
1822 DBusMessage *message)
1824 MessageToSend *to_send;
1825 BusConnectionData *d;
1828 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
1829 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
1830 dbus_message_get_reply_serial (message) != 0 ? "reply" :
1832 dbus_message_get_interface (message) ?
1833 dbus_message_get_interface (message) : "(unset)",
1834 dbus_message_get_member (message) ?
1835 dbus_message_get_member (message) : "(unset)",
1836 dbus_message_get_error_name (message) ?
1837 dbus_message_get_error_name (message) : "(unset)",
1838 dbus_connection_get_is_connected (connection) ?
1839 "" : " (disconnected)");
1841 _dbus_assert (dbus_message_get_sender (message) != NULL);
1843 if (!dbus_connection_get_is_connected (connection))
1844 return TRUE; /* silently ignore disconnected connections */
1846 d = BUS_CONNECTION_DATA (connection);
1847 _dbus_assert (d != NULL);
1849 to_send = dbus_new (MessageToSend, 1);
1850 if (to_send == NULL)
1855 to_send->preallocated = dbus_connection_preallocate_send (connection);
1856 if (to_send->preallocated == NULL)
1858 dbus_free (to_send);
1862 dbus_message_ref (message);
1863 to_send->message = message;
1864 to_send->transaction = transaction;
1866 _dbus_verbose ("about to prepend message\n");
1868 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
1870 message_to_send_free (connection, to_send);
1874 _dbus_verbose ("prepended message\n");
1876 /* See if we already had this connection in the list
1877 * for this transaction. If we have a pending message,
1878 * then we should already be in transaction->connections
1880 link = _dbus_list_get_first_link (&d->transaction_messages);
1881 _dbus_assert (link->data == to_send);
1882 link = _dbus_list_get_next_link (&d->transaction_messages, link);
1883 while (link != NULL)
1885 MessageToSend *m = link->data;
1886 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1888 if (m->transaction == transaction)
1896 if (!_dbus_list_prepend (&transaction->connections, connection))
1898 _dbus_list_remove (&d->transaction_messages, to_send);
1899 message_to_send_free (connection, to_send);
1908 connection_cancel_transaction (DBusConnection *connection,
1909 BusTransaction *transaction)
1912 BusConnectionData *d;
1914 d = BUS_CONNECTION_DATA (connection);
1915 _dbus_assert (d != NULL);
1917 link = _dbus_list_get_first_link (&d->transaction_messages);
1918 while (link != NULL)
1920 MessageToSend *m = link->data;
1921 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1923 if (m->transaction == transaction)
1925 _dbus_list_remove_link (&d->transaction_messages,
1928 message_to_send_free (connection, m);
1936 bus_transaction_cancel_and_free (BusTransaction *transaction)
1938 DBusConnection *connection;
1940 _dbus_verbose ("TRANSACTION: cancelled\n");
1942 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1943 connection_cancel_transaction (connection, transaction);
1945 _dbus_assert (transaction->connections == NULL);
1947 _dbus_list_foreach (&transaction->cancel_hooks,
1948 cancel_hook_cancel, NULL);
1950 free_cancel_hooks (transaction);
1952 dbus_free (transaction);
1956 connection_execute_transaction (DBusConnection *connection,
1957 BusTransaction *transaction)
1960 BusConnectionData *d;
1962 d = BUS_CONNECTION_DATA (connection);
1963 _dbus_assert (d != NULL);
1965 /* Send the queue in order (FIFO) */
1966 link = _dbus_list_get_last_link (&d->transaction_messages);
1967 while (link != NULL)
1969 MessageToSend *m = link->data;
1970 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
1972 if (m->transaction == transaction)
1974 _dbus_list_remove_link (&d->transaction_messages,
1977 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
1979 dbus_connection_send_preallocated (connection,
1984 m->preallocated = NULL; /* so we don't double-free it */
1986 message_to_send_free (connection, m);
1994 bus_transaction_execute_and_free (BusTransaction *transaction)
1996 /* For each connection in transaction->connections
1999 DBusConnection *connection;
2001 _dbus_verbose ("TRANSACTION: executing\n");
2003 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2004 connection_execute_transaction (connection, transaction);
2006 _dbus_assert (transaction->connections == NULL);
2008 free_cancel_hooks (transaction);
2010 dbus_free (transaction);
2014 bus_connection_remove_transactions (DBusConnection *connection)
2016 MessageToSend *to_send;
2017 BusConnectionData *d;
2019 d = BUS_CONNECTION_DATA (connection);
2020 _dbus_assert (d != NULL);
2022 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2024 /* only has an effect for the first MessageToSend listing this transaction */
2025 _dbus_list_remove (&to_send->transaction->connections,
2028 _dbus_list_remove (&d->transaction_messages, to_send);
2029 message_to_send_free (connection, to_send);
2034 * Converts the DBusError to a message reply
2037 bus_transaction_send_error_reply (BusTransaction *transaction,
2038 DBusConnection *connection,
2039 const DBusError *error,
2040 DBusMessage *in_reply_to)
2044 _dbus_assert (error != NULL);
2045 _DBUS_ASSERT_ERROR_IS_SET (error);
2047 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2048 error->name, error->message);
2050 reply = dbus_message_new_error (in_reply_to,
2056 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2058 dbus_message_unref (reply);
2062 dbus_message_unref (reply);
2068 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2069 BusTransactionCancelFunction cancel_function,
2071 DBusFreeFunction free_data_function)
2075 ch = dbus_new (CancelHook, 1);
2079 ch->cancel_function = cancel_function;
2081 ch->free_data_function = free_data_function;
2083 /* It's important that the hooks get run in reverse order that they
2086 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))