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 dbus_bool_t 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);
1436 bus_connection_drop_pending_replies (BusConnections *connections,
1437 DBusConnection *connection)
1439 /* The DBusConnection is almost 100% finalized here, so you can't
1440 * do anything with it except check for pointer equality
1444 _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1447 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1448 while (link != NULL)
1451 BusPendingReply *pending;
1453 next = _dbus_list_get_next_link (&connections->pending_replies->items,
1455 pending = link->data;
1457 if (pending->will_get_reply == connection)
1459 /* We don't need to track this pending reply anymore */
1461 _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1463 pending->will_send_reply,
1464 pending->will_get_reply,
1465 pending->reply_serial);
1467 _dbus_list_remove_link (&connections->pending_replies->items,
1469 bus_pending_reply_free (pending);
1471 else if (pending->will_send_reply == connection)
1473 /* The reply isn't going to be sent, so set things
1474 * up so it will be expired right away
1476 _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1478 pending->will_send_reply,
1479 pending->will_get_reply,
1480 pending->reply_serial);
1482 pending->will_send_reply = NULL;
1483 pending->expire_item.added_tv_sec = 0;
1484 pending->expire_item.added_tv_usec = 0;
1486 bus_expire_timeout_set_interval (connections->pending_replies->timeout,
1497 BusPendingReply *pending;
1498 BusConnections *connections;
1499 } CancelPendingReplyData;
1502 cancel_pending_reply (void *data)
1504 CancelPendingReplyData *d = data;
1506 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1508 if (!_dbus_list_remove (&d->connections->pending_replies->items,
1510 _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1512 bus_pending_reply_free (d->pending); /* since it's been cancelled */
1516 cancel_pending_reply_data_free (void *data)
1518 CancelPendingReplyData *d = data;
1520 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1522 /* d->pending should be either freed or still
1523 * in the list of pending replies (owned by someone
1531 * Record that a reply is allowed; return TRUE on success.
1534 bus_connections_expect_reply (BusConnections *connections,
1535 BusTransaction *transaction,
1536 DBusConnection *will_get_reply,
1537 DBusConnection *will_send_reply,
1538 DBusMessage *reply_to_this,
1541 BusPendingReply *pending;
1542 dbus_uint32_t reply_serial;
1544 CancelPendingReplyData *cprd;
1546 _dbus_assert (will_get_reply != NULL);
1547 _dbus_assert (will_send_reply != NULL);
1548 _dbus_assert (reply_to_this != NULL);
1550 if (dbus_message_get_no_reply (reply_to_this))
1551 return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1553 reply_serial = dbus_message_get_serial (reply_to_this);
1555 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1556 while (link != NULL)
1558 pending = link->data;
1560 if (pending->reply_serial == reply_serial &&
1561 pending->will_get_reply == will_get_reply &&
1562 pending->will_send_reply == will_send_reply)
1564 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1565 "Message has the same reply serial as a currently-outstanding existing method call");
1569 link = _dbus_list_get_next_link (&connections->pending_replies->items,
1573 pending = dbus_new0 (BusPendingReply, 1);
1574 if (pending == NULL)
1576 BUS_SET_OOM (error);
1580 #ifdef DBUS_ENABLE_VERBOSE_MODE
1581 /* so we can see a not-yet-added pending reply */
1582 pending->expire_item.added_tv_sec = 1;
1583 pending->expire_item.added_tv_usec = 1;
1586 pending->will_get_reply = will_get_reply;
1587 pending->will_send_reply = will_send_reply;
1588 pending->reply_serial = reply_serial;
1590 cprd = dbus_new0 (CancelPendingReplyData, 1);
1593 BUS_SET_OOM (error);
1594 bus_pending_reply_free (pending);
1598 if (!_dbus_list_prepend (&connections->pending_replies->items,
1601 BUS_SET_OOM (error);
1603 bus_pending_reply_free (pending);
1607 if (!bus_transaction_add_cancel_hook (transaction,
1608 cancel_pending_reply,
1610 cancel_pending_reply_data_free))
1612 BUS_SET_OOM (error);
1613 _dbus_list_remove (&connections->pending_replies->items, pending);
1615 bus_pending_reply_free (pending);
1619 cprd->pending = pending;
1620 cprd->connections = connections;
1622 _dbus_get_current_time (&pending->expire_item.added_tv_sec,
1623 &pending->expire_item.added_tv_usec);
1625 _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1627 pending->will_send_reply,
1628 pending->will_get_reply,
1629 pending->reply_serial);
1637 BusConnections *connections;
1638 } CheckPendingReplyData;
1641 cancel_check_pending_reply (void *data)
1643 CheckPendingReplyData *d = data;
1645 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1647 _dbus_list_prepend_link (&d->connections->pending_replies->items,
1653 check_pending_reply_data_free (void *data)
1655 CheckPendingReplyData *d = data;
1657 _dbus_verbose ("%s: d = %p\n", _DBUS_FUNCTION_NAME, d);
1659 if (d->link != NULL)
1661 BusPendingReply *pending = d->link->data;
1663 _dbus_assert (_dbus_list_find_last (&d->connections->pending_replies->items,
1666 bus_pending_reply_free (pending);
1667 _dbus_list_free_link (d->link);
1674 * Check whether a reply is allowed, remove BusPendingReply
1675 * if so, return TRUE if so.
1678 bus_connections_check_reply (BusConnections *connections,
1679 BusTransaction *transaction,
1680 DBusConnection *sending_reply,
1681 DBusConnection *receiving_reply,
1685 CheckPendingReplyData *cprd;
1687 dbus_uint32_t reply_serial;
1689 _dbus_assert (sending_reply != NULL);
1690 _dbus_assert (receiving_reply != NULL);
1692 reply_serial = dbus_message_get_reply_serial (reply);
1694 link = _dbus_list_get_first_link (&connections->pending_replies->items);
1695 while (link != NULL)
1697 BusPendingReply *pending = link->data;
1699 if (pending->reply_serial == reply_serial &&
1700 pending->will_get_reply == receiving_reply &&
1701 pending->will_send_reply == sending_reply)
1703 _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1707 link = _dbus_list_get_next_link (&connections->pending_replies->items,
1713 _dbus_verbose ("No pending reply expected\n");
1718 cprd = dbus_new0 (CheckPendingReplyData, 1);
1721 BUS_SET_OOM (error);
1725 if (!bus_transaction_add_cancel_hook (transaction,
1726 cancel_check_pending_reply,
1728 check_pending_reply_data_free))
1730 BUS_SET_OOM (error);
1736 cprd->connections = connections;
1738 _dbus_list_unlink (&connections->pending_replies->items,
1741 _dbus_assert (_dbus_list_find_last (&connections->pending_replies->items,
1742 link->data) == NULL);
1750 * Note that this is fairly fragile; in particular, don't try to use
1751 * one transaction across any main loop iterations.
1756 BusTransaction *transaction;
1757 DBusMessage *message;
1758 DBusPreallocatedSend *preallocated;
1763 BusTransactionCancelFunction cancel_function;
1764 DBusFreeFunction free_data_function;
1768 struct BusTransaction
1770 DBusList *connections;
1771 BusContext *context;
1772 DBusList *cancel_hooks;
1776 message_to_send_free (DBusConnection *connection,
1777 MessageToSend *to_send)
1779 if (to_send->message)
1780 dbus_message_unref (to_send->message);
1782 if (to_send->preallocated)
1783 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1785 dbus_free (to_send);
1789 cancel_hook_cancel (void *element,
1792 CancelHook *ch = element;
1794 _dbus_verbose ("Running transaction cancel hook\n");
1796 if (ch->cancel_function)
1797 (* ch->cancel_function) (ch->data);
1801 cancel_hook_free (void *element,
1804 CancelHook *ch = element;
1806 if (ch->free_data_function)
1807 (* ch->free_data_function) (ch->data);
1813 free_cancel_hooks (BusTransaction *transaction)
1815 _dbus_list_foreach (&transaction->cancel_hooks,
1816 cancel_hook_free, NULL);
1818 _dbus_list_clear (&transaction->cancel_hooks);
1822 bus_transaction_new (BusContext *context)
1824 BusTransaction *transaction;
1826 transaction = dbus_new0 (BusTransaction, 1);
1827 if (transaction == NULL)
1830 transaction->context = context;
1836 bus_transaction_get_context (BusTransaction *transaction)
1838 return transaction->context;
1842 bus_transaction_get_connections (BusTransaction *transaction)
1844 return bus_context_get_connections (transaction->context);
1848 bus_transaction_send_from_driver (BusTransaction *transaction,
1849 DBusConnection *connection,
1850 DBusMessage *message)
1852 /* We have to set the sender to the driver, and have
1853 * to check security policy since it was not done in
1856 _dbus_verbose ("Sending %s %s %s from driver\n",
1857 dbus_message_get_interface (message) ?
1858 dbus_message_get_interface (message) : "(no interface)",
1859 dbus_message_get_member (message) ?
1860 dbus_message_get_member (message) : "(no member)",
1861 dbus_message_get_error_name (message) ?
1862 dbus_message_get_error_name (message) : "(no error name)");
1864 if (!dbus_message_set_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1867 if (bus_connection_is_active (connection))
1869 if (!dbus_message_set_destination (message,
1870 bus_connection_get_name (connection)))
1874 /* bus driver never wants a reply */
1875 dbus_message_set_no_reply (message, TRUE);
1877 /* If security policy doesn't allow the message, we silently
1878 * eat it; the driver doesn't care about getting a reply.
1880 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
1882 NULL, connection, connection, message, NULL))
1885 return bus_transaction_send (transaction, connection, message);
1889 bus_transaction_send (BusTransaction *transaction,
1890 DBusConnection *connection,
1891 DBusMessage *message)
1893 MessageToSend *to_send;
1894 BusConnectionData *d;
1897 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
1898 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
1899 dbus_message_get_reply_serial (message) != 0 ? "reply" :
1901 dbus_message_get_interface (message) ?
1902 dbus_message_get_interface (message) : "(unset)",
1903 dbus_message_get_member (message) ?
1904 dbus_message_get_member (message) : "(unset)",
1905 dbus_message_get_error_name (message) ?
1906 dbus_message_get_error_name (message) : "(unset)",
1907 dbus_connection_get_is_connected (connection) ?
1908 "" : " (disconnected)");
1910 _dbus_assert (dbus_message_get_sender (message) != NULL);
1912 if (!dbus_connection_get_is_connected (connection))
1913 return TRUE; /* silently ignore disconnected connections */
1915 d = BUS_CONNECTION_DATA (connection);
1916 _dbus_assert (d != NULL);
1918 to_send = dbus_new (MessageToSend, 1);
1919 if (to_send == NULL)
1924 to_send->preallocated = dbus_connection_preallocate_send (connection);
1925 if (to_send->preallocated == NULL)
1927 dbus_free (to_send);
1931 dbus_message_ref (message);
1932 to_send->message = message;
1933 to_send->transaction = transaction;
1935 _dbus_verbose ("about to prepend message\n");
1937 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
1939 message_to_send_free (connection, to_send);
1943 _dbus_verbose ("prepended message\n");
1945 /* See if we already had this connection in the list
1946 * for this transaction. If we have a pending message,
1947 * then we should already be in transaction->connections
1949 link = _dbus_list_get_first_link (&d->transaction_messages);
1950 _dbus_assert (link->data == to_send);
1951 link = _dbus_list_get_next_link (&d->transaction_messages, link);
1952 while (link != NULL)
1954 MessageToSend *m = link->data;
1955 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1957 if (m->transaction == transaction)
1965 if (!_dbus_list_prepend (&transaction->connections, connection))
1967 _dbus_list_remove (&d->transaction_messages, to_send);
1968 message_to_send_free (connection, to_send);
1977 connection_cancel_transaction (DBusConnection *connection,
1978 BusTransaction *transaction)
1981 BusConnectionData *d;
1983 d = BUS_CONNECTION_DATA (connection);
1984 _dbus_assert (d != NULL);
1986 link = _dbus_list_get_first_link (&d->transaction_messages);
1987 while (link != NULL)
1989 MessageToSend *m = link->data;
1990 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1992 if (m->transaction == transaction)
1994 _dbus_list_remove_link (&d->transaction_messages,
1997 message_to_send_free (connection, m);
2005 bus_transaction_cancel_and_free (BusTransaction *transaction)
2007 DBusConnection *connection;
2009 _dbus_verbose ("TRANSACTION: cancelled\n");
2011 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2012 connection_cancel_transaction (connection, transaction);
2014 _dbus_assert (transaction->connections == NULL);
2016 _dbus_list_foreach (&transaction->cancel_hooks,
2017 cancel_hook_cancel, NULL);
2019 free_cancel_hooks (transaction);
2021 dbus_free (transaction);
2025 connection_execute_transaction (DBusConnection *connection,
2026 BusTransaction *transaction)
2029 BusConnectionData *d;
2031 d = BUS_CONNECTION_DATA (connection);
2032 _dbus_assert (d != NULL);
2034 /* Send the queue in order (FIFO) */
2035 link = _dbus_list_get_last_link (&d->transaction_messages);
2036 while (link != NULL)
2038 MessageToSend *m = link->data;
2039 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2041 if (m->transaction == transaction)
2043 _dbus_list_remove_link (&d->transaction_messages,
2046 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2048 dbus_connection_send_preallocated (connection,
2053 m->preallocated = NULL; /* so we don't double-free it */
2055 message_to_send_free (connection, m);
2063 bus_transaction_execute_and_free (BusTransaction *transaction)
2065 /* For each connection in transaction->connections
2068 DBusConnection *connection;
2070 _dbus_verbose ("TRANSACTION: executing\n");
2072 while ((connection = _dbus_list_pop_first (&transaction->connections)))
2073 connection_execute_transaction (connection, transaction);
2075 _dbus_assert (transaction->connections == NULL);
2077 free_cancel_hooks (transaction);
2079 dbus_free (transaction);
2083 bus_connection_remove_transactions (DBusConnection *connection)
2085 MessageToSend *to_send;
2086 BusConnectionData *d;
2088 d = BUS_CONNECTION_DATA (connection);
2089 _dbus_assert (d != NULL);
2091 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2093 /* only has an effect for the first MessageToSend listing this transaction */
2094 _dbus_list_remove (&to_send->transaction->connections,
2097 _dbus_list_remove (&d->transaction_messages, to_send);
2098 message_to_send_free (connection, to_send);
2103 * Converts the DBusError to a message reply
2106 bus_transaction_send_error_reply (BusTransaction *transaction,
2107 DBusConnection *connection,
2108 const DBusError *error,
2109 DBusMessage *in_reply_to)
2113 _dbus_assert (error != NULL);
2114 _DBUS_ASSERT_ERROR_IS_SET (error);
2116 _dbus_verbose ("Sending error reply %s \"%s\"\n",
2117 error->name, error->message);
2119 reply = dbus_message_new_error (in_reply_to,
2125 if (!bus_transaction_send_from_driver (transaction, connection, reply))
2127 dbus_message_unref (reply);
2131 dbus_message_unref (reply);
2137 bus_transaction_add_cancel_hook (BusTransaction *transaction,
2138 BusTransactionCancelFunction cancel_function,
2140 DBusFreeFunction free_data_function)
2144 ch = dbus_new (CancelHook, 1);
2148 _dbus_verbose (" adding cancel hook function = %p data = %p\n",
2149 cancel_function, data);
2151 ch->cancel_function = cancel_function;
2153 ch->free_data_function = free_data_function;
2155 /* It's important that the hooks get run in reverse order that they
2158 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))