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_free (BusPendingReply *pending)
1346 _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1348 pending->will_send_reply,
1349 pending->will_get_reply,
1350 pending->reply_serial);
1352 dbus_free (pending);
1356 bus_pending_reply_send_no_reply (BusConnections *connections,
1357 BusTransaction *transaction,
1358 BusPendingReply *pending)
1360 DBusMessage *message;
1361 DBusMessageIter iter;
1366 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1367 if (message == NULL)
1370 dbus_message_set_no_reply (message, TRUE);
1372 if (!dbus_message_set_reply_serial (message,
1373 pending->reply_serial))
1376 if (!dbus_message_set_error_name (message,
1377 DBUS_ERROR_NO_REPLY))
1380 dbus_message_append_iter_init (message, &iter);
1381 if (!dbus_message_iter_append_string (&iter, "Message did not receive a reply (timeout by message bus)"))
1384 if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1391 dbus_message_unref (message);
1396 bus_pending_reply_expired (BusExpireList *list,
1400 BusPendingReply *pending = link->data;
1401 BusConnections *connections = data;
1402 BusTransaction *transaction;
1404 /* No reply is forthcoming. So nuke it if we can. If not,
1405 * leave it in the list to try expiring again later when we
1409 _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1411 pending->will_send_reply,
1412 pending->will_get_reply,
1413 pending->reply_serial);
1415 transaction = bus_transaction_new (connections->context);
1416 if (transaction == NULL)
1419 if (!bus_pending_reply_send_no_reply (connections,
1423 bus_transaction_cancel_and_free (transaction);
1427 _dbus_list_remove_link (&connections->pending_replies->items,
1429 bus_pending_reply_free (pending);
1430 bus_transaction_execute_and_free (transaction);
1434 bus_connection_drop_pending_replies (BusConnections *connections,
1435 DBusConnection *connection)
1437 /* The DBusConnection is almost 100% finalized here, so you can't
1438 * do anything with it except check for pointer equality
1442 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1445 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1446 while (link != NULL)
1449 BusPendingReply *pending;
1451 next = _dbus_list_get_next_link (&connections->pending_replies->items,
1453 pending = link->data;
1455 if (pending->will_get_reply == connection)
1457 /* We don't need to track this pending reply anymore */
1459 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1461 pending->will_send_reply,
1462 pending->will_get_reply,
1463 pending->reply_serial);
1465 _dbus_list_remove_link (&connections->pending_replies->items,
1467 bus_pending_reply_free (pending);
1469 else if (pending->will_send_reply == connection)
1471 /* The reply isn't going to be sent, so set things
1472 * up so it will be expired right away
1474 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1476 pending->will_send_reply,
1477 pending->will_get_reply,
1478 pending->reply_serial);
1480 pending->will_send_reply = NULL;
1481 pending->expire_item.added_tv_sec = 0;
1482 pending->expire_item.added_tv_usec = 0;
1484 bus_expire_timeout_set_interval (connections->pending_replies->timeout,
1495 BusPendingReply *pending;
1496 BusConnections *connections;
1497 } CancelPendingReplyData;
1500 cancel_pending_reply (void *data)
1502 CancelPendingReplyData *d = data;
1504 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1506 if (!_dbus_list_remove (&d->connections->pending_replies->items,
1508 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1510 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1514 cancel_pending_reply_data_free (void *data)
1516 CancelPendingReplyData *d = data;
1518 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1520 /* d->pending should be either freed or still
1521 * in the list of pending replies (owned by someone
1529 * Record that a reply is allowed; return TRUE on success.
1532 bus_connections_expect_reply (BusConnections *connections,
1533 BusTransaction *transaction,
1534 DBusConnection *will_get_reply,
1535 DBusConnection *will_send_reply,
1536 DBusMessage *reply_to_this,
1539 BusPendingReply *pending;
1540 dbus_uint32_t reply_serial;
1542 CancelPendingReplyData *cprd;
1544 _dbus_assert (will_get_reply != NULL);
1545 _dbus_assert (will_send_reply != NULL);
1546 _dbus_assert (reply_to_this != NULL);
1548 if (dbus_message_get_no_reply (reply_to_this))
1549 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1551 reply_serial = dbus_message_get_serial (reply_to_this);
1553 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1554 while (link != NULL)
1556 pending = link->data;
1558 if (pending->reply_serial == reply_serial &&
1559 pending->will_get_reply == will_get_reply &&
1560 pending->will_send_reply == will_send_reply)
1562 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1563 "Message has the same reply serial as a currently-outstanding existing method call");
1567 link = _dbus_list_get_next_link (&connections->pending_replies->items,
1571 pending = dbus_new0 (BusPendingReply, 1);
1572 if (pending == NULL)
1574 BUS_SET_OOM (error);
1578 #ifdef DBUS_ENABLE_VERBOSE_MODE
1579 /* so we can see a not-yet-added pending reply */
1580 pending->expire_item.added_tv_sec = 1;
1581 pending->expire_item.added_tv_usec = 1;
1584 pending->will_get_reply = will_get_reply;
1585 pending->will_send_reply = will_send_reply;
1586 pending->reply_serial = reply_serial;
1588 cprd = dbus_new0 (CancelPendingReplyData, 1);
1591 BUS_SET_OOM (error);
1592 bus_pending_reply_free (pending);
1596 if (!_dbus_list_prepend (&connections->pending_replies->items,
1599 BUS_SET_OOM (error);
1601 bus_pending_reply_free (pending);
1605 if (!bus_transaction_add_cancel_hook (transaction,
1606 cancel_pending_reply,
1608 cancel_pending_reply_data_free))
1610 BUS_SET_OOM (error);
1611 _dbus_list_remove (&connections->pending_replies->items, pending);
1613 bus_pending_reply_free (pending);
1617 cprd->pending = pending;
1618 cprd->connections = connections;
1620 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1621 &pending->expire_item.added_tv_usec);
1623 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1625 pending->will_send_reply,
1626 pending->will_get_reply,
1627 pending->reply_serial);
1635 BusConnections *connections;
1636 } CheckPendingReplyData;
1639 cancel_check_pending_reply (void *data)
1641 CheckPendingReplyData *d = data;
1643 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1645 _dbus_list_prepend_link (&d->connections->pending_replies->items,
1651 check_pending_reply_data_free (void *data)
1653 CheckPendingReplyData *d = data;
1655 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1657 if (d->link != NULL)
1659 BusPendingReply *pending = d->link->data;
1661 _dbus_assert (_dbus_list_find_last (&d->connections->pending_replies->items,
1664 bus_pending_reply_free (pending);
1665 _dbus_list_free_link (d->link);
1672 * Check whether a reply is allowed, remove BusPendingReply
1673 * if so, return TRUE if so.
1676 bus_connections_check_reply (BusConnections *connections,
1677 BusTransaction *transaction,
1678 DBusConnection *sending_reply,
1679 DBusConnection *receiving_reply,
1683 CheckPendingReplyData *cprd;
1685 dbus_uint32_t reply_serial;
1687 _dbus_assert (sending_reply != NULL);
1688 _dbus_assert (receiving_reply != NULL);
1690 reply_serial = dbus_message_get_reply_serial (reply);
1692 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1693 while (link != NULL)
1695 BusPendingReply *pending = link->data;
1697 if (pending->reply_serial == reply_serial &&
1698 pending->will_get_reply == receiving_reply &&
1699 pending->will_send_reply == sending_reply)
1701 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1705 link = _dbus_list_get_next_link (&connections->pending_replies->items,
1711 _dbus_verbose ("No pending reply expected\n");
1716 cprd = dbus_new0 (CheckPendingReplyData, 1);
1719 BUS_SET_OOM (error);
1723 if (!bus_transaction_add_cancel_hook (transaction,
1724 cancel_check_pending_reply,
1726 check_pending_reply_data_free))
1728 BUS_SET_OOM (error);
1734 cprd->connections = connections;
1736 _dbus_list_unlink (&connections->pending_replies->items,
1739 _dbus_assert (_dbus_list_find_last (&connections->pending_replies->items,
1740 link->data) == NULL);
1748 * Note that this is fairly fragile; in particular, don't try to use
1749 * one transaction across any main loop iterations.
1754 BusTransaction *transaction;
1755 DBusMessage *message;
1756 DBusPreallocatedSend *preallocated;
1761 BusTransactionCancelFunction cancel_function;
1762 DBusFreeFunction free_data_function;
1766 struct BusTransaction
1768 DBusList *connections;
1769 BusContext *context;
1770 DBusList *cancel_hooks;
1774 message_to_send_free (DBusConnection *connection,
1775 MessageToSend *to_send)
1777 if (to_send->message)
1778 dbus_message_unref (to_send->message);
1780 if (to_send->preallocated)
1781 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1783 dbus_free (to_send);
1787 cancel_hook_cancel (void *element,
1790 CancelHook *ch = element;
1792 _dbus_verbose ("Running transaction cancel hook\n");
1794 if (ch->cancel_function)
1795 (* ch->cancel_function) (ch->data);
1799 cancel_hook_free (void *element,
1802 CancelHook *ch = element;
1804 if (ch->free_data_function)
1805 (* ch->free_data_function) (ch->data);
1811 free_cancel_hooks (BusTransaction *transaction)
1813 _dbus_list_foreach (&transaction->cancel_hooks,
1814 cancel_hook_free, NULL);
1816 _dbus_list_clear (&transaction->cancel_hooks);
1820 bus_transaction_new (BusContext *context)
1822 BusTransaction *transaction;
1824 transaction = dbus_new0 (BusTransaction, 1);
1825 if (transaction == NULL)
1828 transaction->context = context;
1834 bus_transaction_get_context (BusTransaction *transaction)
1836 return transaction->context;
1840 bus_transaction_get_connections (BusTransaction *transaction)
1842 return bus_context_get_connections (transaction->context);
1846 bus_transaction_send_from_driver (BusTransaction *transaction,
1847 DBusConnection *connection,
1848 DBusMessage *message)
1850 /* We have to set the sender to the driver, and have
1851 * to check security policy since it was not done in
1854 _dbus_verbose ("Sending %s %s %s from driver\n",
1855 dbus_message_get_interface (message) ?
1856 dbus_message_get_interface (message) : "(no interface)",
1857 dbus_message_get_member (message) ?
1858 dbus_message_get_member (message) : "(no member)",
1859 dbus_message_get_error_name (message) ?
1860 dbus_message_get_error_name (message) : "(no error name)");
1862 if (!dbus_message_set_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1865 if (bus_connection_is_active (connection))
1867 if (!dbus_message_set_destination (message,
1868 bus_connection_get_name (connection)))
1872 /* bus driver never wants a reply */
1873 dbus_message_set_no_reply (message, TRUE);
1875 /* If security policy doesn't allow the message, we silently
1876 * eat it; the driver doesn't care about getting a reply.
1878 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
1880 NULL, connection, connection, message, NULL))
1883 return bus_transaction_send (transaction, connection, message);
1887 bus_transaction_send (BusTransaction *transaction,
1888 DBusConnection *connection,
1889 DBusMessage *message)
1891 MessageToSend *to_send;
1892 BusConnectionData *d;
1895 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
1896 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
1897 dbus_message_get_reply_serial (message) != 0 ? "reply" :
1899 dbus_message_get_interface (message) ?
1900 dbus_message_get_interface (message) : "(unset)",
1901 dbus_message_get_member (message) ?
1902 dbus_message_get_member (message) : "(unset)",
1903 dbus_message_get_error_name (message) ?
1904 dbus_message_get_error_name (message) : "(unset)",
1905 dbus_connection_get_is_connected (connection) ?
1906 "" : " (disconnected)");
1908 _dbus_assert (dbus_message_get_sender (message) != NULL);
1910 if (!dbus_connection_get_is_connected (connection))
1911 return TRUE; /* silently ignore disconnected connections */
1913 d = BUS_CONNECTION_DATA (connection);
1914 _dbus_assert (d != NULL);
1916 to_send = dbus_new (MessageToSend, 1);
1917 if (to_send == NULL)
1922 to_send->preallocated = dbus_connection_preallocate_send (connection);
1923 if (to_send->preallocated == NULL)
1925 dbus_free (to_send);
1929 dbus_message_ref (message);
1930 to_send->message = message;
1931 to_send->transaction = transaction;
1933 _dbus_verbose ("about to prepend message\n");
1935 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
1937 message_to_send_free (connection, to_send);
1941 _dbus_verbose ("prepended message\n");
1943 /* See if we already had this connection in the list
1944 * for this transaction. If we have a pending message,
1945 * then we should already be in transaction->connections
1947 link = _dbus_list_get_first_link (&d->transaction_messages);
1948 _dbus_assert (link->data == to_send);
1949 link = _dbus_list_get_next_link (&d->transaction_messages, link);
1950 while (link != NULL)
1952 MessageToSend *m = link->data;
1953 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1955 if (m->transaction == transaction)
1963 if (!_dbus_list_prepend (&transaction->connections, connection))
1965 _dbus_list_remove (&d->transaction_messages, to_send);
1966 message_to_send_free (connection, to_send);
1975 connection_cancel_transaction (DBusConnection *connection,
1976 BusTransaction *transaction)
1979 BusConnectionData *d;
1981 d = BUS_CONNECTION_DATA (connection);
1982 _dbus_assert (d != NULL);
1984 link = _dbus_list_get_first_link (&d->transaction_messages);
1985 while (link != NULL)
1987 MessageToSend *m = link->data;
1988 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1990 if (m->transaction == transaction)
1992 _dbus_list_remove_link (&d->transaction_messages,
1995 message_to_send_free (connection, m);
2003 bus_transaction_cancel_and_free (BusTransaction *transaction)
2005 DBusConnection *connection;
2007 _dbus_verbose ("TRANSACTION: cancelled\n");
2009 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2010 connection_cancel_transaction (connection, transaction);
2012 _dbus_assert (transaction->connections == NULL);
2014 _dbus_list_foreach (&transaction->cancel_hooks,
2015 cancel_hook_cancel, NULL);
2017 free_cancel_hooks (transaction);
2019 dbus_free (transaction);
2023 connection_execute_transaction (DBusConnection *connection,
2024 BusTransaction *transaction)
2027 BusConnectionData *d;
2029 d = BUS_CONNECTION_DATA (connection);
2030 _dbus_assert (d != NULL);
2032 /* Send the queue in order (FIFO) */
2033 link = _dbus_list_get_last_link (&d->transaction_messages);
2034 while (link != NULL)
2036 MessageToSend *m = link->data;
2037 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2039 if (m->transaction == transaction)
2041 _dbus_list_remove_link (&d->transaction_messages,
2044 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2046 dbus_connection_send_preallocated (connection,
2051 m->preallocated = NULL; /* so we don't double-free it */
2053 message_to_send_free (connection, m);
2061 bus_transaction_execute_and_free (BusTransaction *transaction)
2063 /* For each connection in transaction->connections
2066 DBusConnection *connection;
2068 _dbus_verbose ("TRANSACTION: executing\n");
2070 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2071 connection_execute_transaction (connection, transaction);
2073 _dbus_assert (transaction->connections == NULL);
2075 free_cancel_hooks (transaction);
2077 dbus_free (transaction);
2081 bus_connection_remove_transactions (DBusConnection *connection)
2083 MessageToSend *to_send;
2084 BusConnectionData *d;
2086 d = BUS_CONNECTION_DATA (connection);
2087 _dbus_assert (d != NULL);
2089 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2091 /* only has an effect for the first MessageToSend listing this transaction */
2092 _dbus_list_remove (&to_send->transaction->connections,
2095 _dbus_list_remove (&d->transaction_messages, to_send);
2096 message_to_send_free (connection, to_send);
2101 * Converts the DBusError to a message reply
2104 bus_transaction_send_error_reply (BusTransaction *transaction,
2105 DBusConnection *connection,
2106 const DBusError *error,
2107 DBusMessage *in_reply_to)
2111 _dbus_assert (error != NULL);
2112 _DBUS_ASSERT_ERROR_IS_SET (error);
2114 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2115 error->name, error->message);
2117 reply = dbus_message_new_error (in_reply_to,
2123 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2125 dbus_message_unref (reply);
2129 dbus_message_unref (reply);
2135 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2136 BusTransactionCancelFunction cancel_function,
2138 DBusFreeFunction free_data_function)
2142 ch = dbus_new (CancelHook, 1);
2146 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2147 cancel_function, data);
2149 ch->cancel_function = cancel_function;
2151 ch->free_data_function = free_data_function;
2153 /* It's important that the hooks get run in reverse order that they
2156 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))