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 <dbus/dbus-list.h>
30 #include <dbus/dbus-hash.h>
31 #include <dbus/dbus-timeout.h>
33 static void bus_connection_remove_transactions (DBusConnection *connection);
38 DBusList *completed; /**< List of all completed connections */
39 int n_completed; /**< Length of completed list */
40 DBusList *incomplete; /**< List of all not-yet-active connections */
41 int n_incomplete; /**< Length of incomplete list */
43 DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
44 DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
45 int stamp; /**< Incrementing number */
48 static dbus_int32_t connection_data_slot = -1;
52 BusConnections *connections;
53 DBusList *link_in_connection_list;
54 DBusConnection *connection;
55 DBusList *services_owned;
57 DBusList *match_rules;
60 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
61 DBusMessage *oom_message;
62 DBusPreallocatedSend *oom_preallocated;
63 BusClientPolicy *policy;
65 long connection_tv_sec; /**< Time when we connected (seconds component) */
66 long connection_tv_usec; /**< Time when we connected (microsec component) */
67 int stamp; /**< connections->stamp last time we were traversed */
70 static dbus_bool_t expire_incomplete_timeout (void *data);
72 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
75 connection_get_loop (DBusConnection *connection)
79 d = BUS_CONNECTION_DATA (connection);
81 return bus_context_get_loop (d->connections->context);
86 get_connections_for_uid (BusConnections *connections,
92 /* val is NULL is 0 when it isn't in the hash yet */
94 val = _dbus_hash_table_lookup_ulong (connections->completed_by_user,
97 current_count = _DBUS_POINTER_TO_INT (val);
103 adjust_connections_for_uid (BusConnections *connections,
109 current_count = get_connections_for_uid (connections, uid);
111 _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
112 ": was %d adjustment %d making %d\n",
113 uid, current_count, adjustment, current_count + adjustment);
115 _dbus_assert (current_count >= 0);
117 current_count += adjustment;
119 _dbus_assert (current_count >= 0);
121 if (current_count == 0)
123 _dbus_hash_table_remove_ulong (connections->completed_by_user, uid);
130 retval = _dbus_hash_table_insert_ulong (connections->completed_by_user,
131 uid, _DBUS_INT_TO_POINTER (current_count));
133 /* only positive adjustment can fail as otherwise
134 * a hash entry should already exist
136 _dbus_assert (adjustment > 0 ||
137 (adjustment <= 0 && retval));
144 bus_connection_disconnected (DBusConnection *connection)
146 BusConnectionData *d;
148 BusMatchmaker *matchmaker;
150 d = BUS_CONNECTION_DATA (connection);
151 _dbus_assert (d != NULL);
153 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
154 d->name ? d->name : "(inactive)");
156 /* Delete our match rules */
157 if (d->n_match_rules > 0)
159 matchmaker = bus_context_get_matchmaker (d->connections->context);
160 bus_matchmaker_disconnected (matchmaker, connection);
163 /* Drop any service ownership. FIXME Unfortunately, this requires
164 * memory allocation and there doesn't seem to be a good way to
165 * handle it other than sleeping; we can't "fail" the operation of
166 * disconnecting a client, and preallocating a broadcast "service is
167 * now gone" message for every client-service pair seems kind of
168 * involved. Probably we need to do that though.
170 while ((service = _dbus_list_get_last (&d->services_owned)))
172 BusTransaction *transaction;
177 dbus_error_init (&error);
180 while (transaction == NULL)
182 transaction = bus_transaction_new (d->connections->context);
183 _dbus_wait_for_memory ();
186 if (!bus_service_remove_owner (service, connection,
187 transaction, &error))
189 _DBUS_ASSERT_ERROR_IS_SET (&error);
191 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
193 dbus_error_free (&error);
194 bus_transaction_cancel_and_free (transaction);
195 _dbus_wait_for_memory ();
200 _dbus_verbose ("Failed to remove service owner: %s %s\n",
201 error.name, error.message);
202 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
206 bus_transaction_execute_and_free (transaction);
209 bus_dispatch_remove_connection (connection);
211 /* no more watching */
212 if (!dbus_connection_set_watch_functions (connection,
216 _dbus_assert_not_reached ("setting watch functions to NULL failed");
218 if (!dbus_connection_set_timeout_functions (connection,
222 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
224 dbus_connection_set_unix_user_function (connection,
227 dbus_connection_set_dispatch_status_function (connection,
230 bus_connection_remove_transactions (connection);
232 if (d->link_in_connection_list != NULL)
238 _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
239 d->link_in_connection_list = NULL;
240 d->connections->n_completed -= 1;
242 if (dbus_connection_get_unix_user (connection, &uid))
244 if (!adjust_connections_for_uid (d->connections,
246 _dbus_assert_not_reached ("adjusting downward should never fail");
251 _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
252 d->link_in_connection_list = NULL;
253 d->connections->n_incomplete -= 1;
256 _dbus_assert (d->connections->n_incomplete >= 0);
257 _dbus_assert (d->connections->n_completed >= 0);
260 /* frees "d" as side effect */
261 dbus_connection_set_data (connection,
262 connection_data_slot,
265 dbus_connection_unref (connection);
269 connection_watch_callback (DBusWatch *watch,
270 unsigned int condition,
273 /* FIXME this can be done in dbus-mainloop.c
274 * if the code in activation.c for the babysitter
275 * watch handler is fixed.
279 _dbus_verbose ("Calling handle_watch\n");
281 return dbus_watch_handle (watch, condition);
285 add_connection_watch (DBusWatch *watch,
288 DBusConnection *connection = data;
290 return _dbus_loop_add_watch (connection_get_loop (connection),
291 watch, connection_watch_callback, connection,
296 remove_connection_watch (DBusWatch *watch,
299 DBusConnection *connection = data;
301 _dbus_loop_remove_watch (connection_get_loop (connection),
302 watch, connection_watch_callback, connection);
306 connection_timeout_callback (DBusTimeout *timeout,
309 /* DBusConnection *connection = data; */
311 /* can return FALSE on OOM but we just let it fire again later */
312 dbus_timeout_handle (timeout);
316 add_connection_timeout (DBusTimeout *timeout,
319 DBusConnection *connection = data;
321 return _dbus_loop_add_timeout (connection_get_loop (connection),
322 timeout, connection_timeout_callback, connection, NULL);
326 remove_connection_timeout (DBusTimeout *timeout,
329 DBusConnection *connection = data;
331 _dbus_loop_remove_timeout (connection_get_loop (connection),
332 timeout, connection_timeout_callback, connection);
336 dispatch_status_function (DBusConnection *connection,
337 DBusDispatchStatus new_status,
340 DBusLoop *loop = data;
342 if (new_status != DBUS_DISPATCH_COMPLETE)
344 while (!_dbus_loop_queue_dispatch (loop, connection))
345 _dbus_wait_for_memory ();
350 allow_user_function (DBusConnection *connection,
354 BusConnectionData *d;
356 d = BUS_CONNECTION_DATA (connection);
358 _dbus_assert (d != NULL);
360 return bus_context_allow_user (d->connections->context, uid);
364 free_connection_data (void *data)
366 BusConnectionData *d = data;
368 /* services_owned should be NULL since we should be disconnected */
369 _dbus_assert (d->services_owned == NULL);
370 _dbus_assert (d->n_services_owned == 0);
372 _dbus_assert (d->transaction_messages == NULL);
374 if (d->oom_preallocated)
375 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
378 dbus_message_unref (d->oom_message);
381 bus_client_policy_unref (d->policy);
389 call_timeout_callback (DBusTimeout *timeout,
392 /* can return FALSE on OOM but we just let it fire again later */
393 dbus_timeout_handle (timeout);
397 bus_connections_new (BusContext *context)
399 BusConnections *connections;
401 if (!dbus_connection_allocate_data_slot (&connection_data_slot))
404 connections = dbus_new0 (BusConnections, 1);
405 if (connections == NULL)
408 connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_ULONG,
410 if (connections->completed_by_user == NULL)
413 connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
414 expire_incomplete_timeout,
416 if (connections->expire_timeout == NULL)
419 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
421 if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
422 connections->expire_timeout,
423 call_timeout_callback, NULL, NULL))
426 connections->refcount = 1;
427 connections->context = context;
432 _dbus_timeout_unref (connections->expire_timeout);
434 _dbus_hash_table_unref (connections->completed_by_user);
436 dbus_free (connections);
438 dbus_connection_free_data_slot (&connection_data_slot);
444 bus_connections_ref (BusConnections *connections)
446 _dbus_assert (connections->refcount > 0);
447 connections->refcount += 1;
451 bus_connections_unref (BusConnections *connections)
453 _dbus_assert (connections->refcount > 0);
454 connections->refcount -= 1;
455 if (connections->refcount == 0)
457 /* drop all incomplete */
458 while (connections->incomplete != NULL)
460 DBusConnection *connection;
462 connection = connections->incomplete->data;
464 dbus_connection_ref (connection);
465 dbus_connection_disconnect (connection);
466 bus_connection_disconnected (connection);
467 dbus_connection_unref (connection);
470 _dbus_assert (connections->n_incomplete == 0);
472 /* drop all real connections */
473 while (connections->completed != NULL)
475 DBusConnection *connection;
477 connection = connections->completed->data;
479 dbus_connection_ref (connection);
480 dbus_connection_disconnect (connection);
481 bus_connection_disconnected (connection);
482 dbus_connection_unref (connection);
485 _dbus_assert (connections->n_completed == 0);
487 _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
488 connections->expire_timeout,
489 call_timeout_callback, NULL);
491 _dbus_timeout_unref (connections->expire_timeout);
493 _dbus_hash_table_unref (connections->completed_by_user);
495 dbus_free (connections);
497 dbus_connection_free_data_slot (&connection_data_slot);
502 bus_connections_setup_connection (BusConnections *connections,
503 DBusConnection *connection)
505 BusConnectionData *d;
508 d = dbus_new0 (BusConnectionData, 1);
513 d->connections = connections;
514 d->connection = connection;
516 _dbus_get_current_time (&d->connection_tv_sec,
517 &d->connection_tv_usec);
519 _dbus_assert (connection_data_slot >= 0);
521 if (!dbus_connection_set_data (connection,
522 connection_data_slot,
523 d, free_connection_data))
531 if (!dbus_connection_set_watch_functions (connection,
532 add_connection_watch,
533 remove_connection_watch,
539 if (!dbus_connection_set_timeout_functions (connection,
540 add_connection_timeout,
541 remove_connection_timeout,
546 dbus_connection_set_unix_user_function (connection,
550 dbus_connection_set_dispatch_status_function (connection,
551 dispatch_status_function,
552 bus_context_get_loop (connections->context),
555 d->link_in_connection_list = _dbus_list_alloc_link (connection);
556 if (d->link_in_connection_list == NULL)
559 /* Setup the connection with the dispatcher */
560 if (!bus_dispatch_add_connection (connection))
563 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
565 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
567 bus_dispatch_remove_connection (connection);
572 _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
573 connections->n_incomplete += 1;
575 dbus_connection_ref (connection);
577 /* Note that we might disconnect ourselves here, but it only takes
578 * effect on return to the main loop. We call this to free up
579 * expired connections if possible, and to queue the timeout for our
582 bus_connections_expire_incomplete (connections);
584 /* And we might also disconnect ourselves here, but again it
585 * only takes effect on return to main loop.
587 if (connections->n_incomplete >
588 bus_context_get_max_incomplete_connections (connections->context))
590 _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
592 _dbus_assert (connections->incomplete != NULL);
593 /* Disconnect the oldest unauthenticated connection. FIXME
594 * would it be more secure to drop a *random* connection? This
595 * algorithm seems to mean that if someone can create new
596 * connections quickly enough, they can keep anyone else from
597 * completing authentication. But random may or may not really
598 * help with that, a more elaborate solution might be required.
600 dbus_connection_disconnect (connections->incomplete->data);
608 if (!dbus_connection_set_watch_functions (connection,
612 _dbus_assert_not_reached ("setting watch functions to NULL failed");
614 if (!dbus_connection_set_timeout_functions (connection,
618 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
620 dbus_connection_set_unix_user_function (connection,
623 dbus_connection_set_dispatch_status_function (connection,
626 if (d->link_in_connection_list != NULL)
628 _dbus_assert (d->link_in_connection_list->next == NULL);
629 _dbus_assert (d->link_in_connection_list->prev == NULL);
630 _dbus_list_free_link (d->link_in_connection_list);
631 d->link_in_connection_list = NULL;
634 if (!dbus_connection_set_data (connection,
635 connection_data_slot,
637 _dbus_assert_not_reached ("failed to set connection data to null");
639 /* "d" has now been freed */
646 bus_connections_expire_incomplete (BusConnections *connections)
652 if (connections->incomplete != NULL)
654 long tv_sec, tv_usec;
658 _dbus_get_current_time (&tv_sec, &tv_usec);
659 auth_timeout = bus_context_get_auth_timeout (connections->context);
661 link = _dbus_list_get_first_link (&connections->incomplete);
664 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
665 DBusConnection *connection;
666 BusConnectionData *d;
669 connection = link->data;
671 d = BUS_CONNECTION_DATA (connection);
673 _dbus_assert (d != NULL);
675 elapsed = ((double) tv_sec - (double) d->connection_tv_sec) * 1000.0 +
676 ((double) tv_usec - (double) d->connection_tv_usec) / 1000.0;
678 if (elapsed >= (double) auth_timeout)
680 _dbus_verbose ("Timing out authentication for connection %p\n", connection);
681 dbus_connection_disconnect (connection);
685 /* We can end the loop, since the connections are in oldest-first order */
686 next_interval = ((double)auth_timeout) - elapsed;
687 _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
688 connection, next_interval);
697 if (next_interval >= 0)
699 _dbus_timeout_set_interval (connections->expire_timeout,
701 _dbus_timeout_set_enabled (connections->expire_timeout, TRUE);
703 _dbus_verbose ("Enabled incomplete connections timeout with interval %d, %d incomplete connections\n",
704 next_interval, connections->n_incomplete);
706 else if (dbus_timeout_get_enabled (connections->expire_timeout))
708 _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
710 _dbus_verbose ("Disabled incomplete connections timeout, %d incomplete connections\n",
711 connections->n_incomplete);
714 _dbus_verbose ("No need to disable incomplete connections timeout\n");
718 expire_incomplete_timeout (void *data)
720 BusConnections *connections = data;
722 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
724 /* note that this may remove the timeout */
725 bus_connections_expire_incomplete (connections);
731 bus_connection_get_groups (DBusConnection *connection,
732 unsigned long **groups,
736 BusConnectionData *d;
738 DBusUserDatabase *user_database;
740 d = BUS_CONNECTION_DATA (connection);
742 _dbus_assert (d != NULL);
744 user_database = bus_context_get_user_database (d->connections->context);
749 if (dbus_connection_get_unix_user (connection, &uid))
751 if (!_dbus_user_database_get_groups (user_database,
752 uid, groups, n_groups,
755 _DBUS_ASSERT_ERROR_IS_SET (error);
756 _dbus_verbose ("Did not get any groups for UID %lu\n",
762 _dbus_verbose ("Got %d groups for UID %lu\n",
768 return TRUE; /* successfully got 0 groups */
772 bus_connection_is_in_group (DBusConnection *connection,
776 unsigned long *group_ids;
779 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids,
784 while (i < n_group_ids)
786 if (group_ids[i] == gid)
788 dbus_free (group_ids);
794 dbus_free (group_ids);
799 bus_connection_get_policy (DBusConnection *connection)
801 BusConnectionData *d;
803 d = BUS_CONNECTION_DATA (connection);
805 _dbus_assert (d != NULL);
806 _dbus_assert (d->policy != NULL);
812 foreach_active (BusConnections *connections,
813 BusConnectionForeachFunction function,
818 link = _dbus_list_get_first_link (&connections->completed);
821 DBusConnection *connection = link->data;
822 DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
824 if (!(* function) (connection, data))
834 foreach_inactive (BusConnections *connections,
835 BusConnectionForeachFunction function,
840 link = _dbus_list_get_first_link (&connections->incomplete);
843 DBusConnection *connection = link->data;
844 DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
846 if (!(* function) (connection, data))
856 * Calls function on each active connection; if the function returns
857 * #FALSE, stops iterating. Active connections are authenticated
858 * and have sent a Hello message.
860 * @param connections the connections object
861 * @param function the function
862 * @param data data to pass to it as a second arg
865 bus_connections_foreach_active (BusConnections *connections,
866 BusConnectionForeachFunction function,
869 foreach_active (connections, function, data);
873 * Calls function on each connection; if the function returns
874 * #FALSE, stops iterating.
876 * @param connections the connections object
877 * @param function the function
878 * @param data data to pass to it as a second arg
881 bus_connections_foreach (BusConnections *connections,
882 BusConnectionForeachFunction function,
885 if (!foreach_active (connections, function, data))
888 foreach_inactive (connections, function, data);
892 bus_connections_get_context (BusConnections *connections)
894 return connections->context;
898 * This is used to avoid covering the same connection twice when
899 * traversing connections. Note that it assumes we will
900 * bus_connection_mark_stamp() each connection at least once per
901 * INT_MAX increments of the global stamp, or wraparound would break
905 bus_connections_increment_stamp (BusConnections *connections)
907 connections->stamp += 1;
910 /* Mark connection with current stamp, return TRUE if it
911 * didn't already have that stamp
914 bus_connection_mark_stamp (DBusConnection *connection)
916 BusConnectionData *d;
918 d = BUS_CONNECTION_DATA (connection);
920 _dbus_assert (d != NULL);
922 if (d->stamp == d->connections->stamp)
926 d->stamp = d->connections->stamp;
932 bus_connection_get_context (DBusConnection *connection)
934 BusConnectionData *d;
936 d = BUS_CONNECTION_DATA (connection);
938 _dbus_assert (d != NULL);
940 return d->connections->context;
944 bus_connection_get_connections (DBusConnection *connection)
946 BusConnectionData *d;
948 d = BUS_CONNECTION_DATA (connection);
950 _dbus_assert (d != NULL);
952 return d->connections;
956 bus_connection_get_registry (DBusConnection *connection)
958 BusConnectionData *d;
960 d = BUS_CONNECTION_DATA (connection);
962 _dbus_assert (d != NULL);
964 return bus_context_get_registry (d->connections->context);
968 bus_connection_get_activation (DBusConnection *connection)
970 BusConnectionData *d;
972 d = BUS_CONNECTION_DATA (connection);
974 _dbus_assert (d != NULL);
976 return bus_context_get_activation (d->connections->context);
980 bus_connection_get_matchmaker (DBusConnection *connection)
982 BusConnectionData *d;
984 d = BUS_CONNECTION_DATA (connection);
986 _dbus_assert (d != NULL);
988 return bus_context_get_matchmaker (d->connections->context);
992 * Checks whether the connection is registered with the message bus.
994 * @param connection the connection
995 * @returns #TRUE if we're an active message bus participant
998 bus_connection_is_active (DBusConnection *connection)
1000 BusConnectionData *d;
1002 d = BUS_CONNECTION_DATA (connection);
1004 return d != NULL && d->name != NULL;
1008 bus_connection_preallocate_oom_error (DBusConnection *connection)
1010 DBusMessage *message;
1011 DBusPreallocatedSend *preallocated;
1012 BusConnectionData *d;
1014 d = BUS_CONNECTION_DATA (connection);
1016 _dbus_assert (d != NULL);
1018 if (d->oom_preallocated != NULL)
1021 preallocated = dbus_connection_preallocate_send (connection);
1022 if (preallocated == NULL)
1025 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1027 if (message == NULL)
1029 dbus_connection_free_preallocated_send (connection, preallocated);
1033 /* d->name may be NULL, but that is OK */
1034 if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1035 !dbus_message_set_destination (message, d->name) ||
1036 !dbus_message_set_sender (message,
1037 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1039 dbus_connection_free_preallocated_send (connection, preallocated);
1040 dbus_message_unref (message);
1044 /* set reply serial to placeholder value just so space is already allocated
1047 if (!dbus_message_set_reply_serial (message, 14))
1049 dbus_connection_free_preallocated_send (connection, preallocated);
1050 dbus_message_unref (message);
1054 d->oom_message = message;
1055 d->oom_preallocated = preallocated;
1061 bus_connection_send_oom_error (DBusConnection *connection,
1062 DBusMessage *in_reply_to)
1064 BusConnectionData *d;
1066 d = BUS_CONNECTION_DATA (connection);
1068 _dbus_assert (d != NULL);
1069 _dbus_assert (d->oom_message != NULL);
1071 /* should always succeed since we set it to a placeholder earlier */
1072 if (!dbus_message_set_reply_serial (d->oom_message,
1073 dbus_message_get_serial (in_reply_to)))
1074 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1076 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1078 dbus_connection_send_preallocated (connection, d->oom_preallocated,
1079 d->oom_message, NULL);
1081 dbus_message_unref (d->oom_message);
1082 d->oom_message = NULL;
1083 d->oom_preallocated = NULL;
1087 bus_connection_add_match_rule_link (DBusConnection *connection,
1090 BusConnectionData *d;
1092 d = BUS_CONNECTION_DATA (connection);
1093 _dbus_assert (d != NULL);
1095 _dbus_list_append_link (&d->match_rules, link);
1097 d->n_match_rules += 1;
1101 bus_connection_add_match_rule (DBusConnection *connection,
1106 link = _dbus_list_alloc_link (rule);
1111 bus_connection_add_match_rule_link (connection, link);
1117 bus_connection_remove_match_rule (DBusConnection *connection,
1120 BusConnectionData *d;
1122 d = BUS_CONNECTION_DATA (connection);
1123 _dbus_assert (d != NULL);
1125 _dbus_list_remove_last (&d->match_rules, rule);
1127 d->n_match_rules -= 1;
1128 _dbus_assert (d->n_match_rules >= 0);
1132 bus_connection_get_n_match_rules (DBusConnection *connection)
1134 BusConnectionData *d;
1136 d = BUS_CONNECTION_DATA (connection);
1137 _dbus_assert (d != NULL);
1139 return d->n_match_rules;
1143 bus_connection_add_owned_service_link (DBusConnection *connection,
1146 BusConnectionData *d;
1148 d = BUS_CONNECTION_DATA (connection);
1149 _dbus_assert (d != NULL);
1151 _dbus_list_append_link (&d->services_owned, link);
1153 d->n_services_owned += 1;
1157 bus_connection_add_owned_service (DBusConnection *connection,
1158 BusService *service)
1162 link = _dbus_list_alloc_link (service);
1167 bus_connection_add_owned_service_link (connection, link);
1173 bus_connection_remove_owned_service (DBusConnection *connection,
1174 BusService *service)
1176 BusConnectionData *d;
1178 d = BUS_CONNECTION_DATA (connection);
1179 _dbus_assert (d != NULL);
1181 _dbus_list_remove_last (&d->services_owned, service);
1183 d->n_services_owned -= 1;
1184 _dbus_assert (d->n_services_owned >= 0);
1188 bus_connection_get_n_services_owned (DBusConnection *connection)
1190 BusConnectionData *d;
1192 d = BUS_CONNECTION_DATA (connection);
1193 _dbus_assert (d != NULL);
1195 return d->n_services_owned;
1199 bus_connection_complete (DBusConnection *connection,
1200 const DBusString *name,
1203 BusConnectionData *d;
1206 d = BUS_CONNECTION_DATA (connection);
1207 _dbus_assert (d != NULL);
1208 _dbus_assert (d->name == NULL);
1209 _dbus_assert (d->policy == NULL);
1211 _dbus_assert (!bus_connection_is_active (connection));
1213 if (!_dbus_string_copy_data (name, &d->name))
1215 BUS_SET_OOM (error);
1219 _dbus_assert (d->name != NULL);
1221 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1223 d->policy = bus_context_create_client_policy (d->connections->context,
1227 /* we may have a NULL policy on OOM or error getting list of
1228 * groups for a user. In the latter case we don't handle it so
1229 * well currently, as it will just keep failing over and over.
1232 if (d->policy == NULL)
1234 _dbus_verbose ("Failed to create security policy for connection %p\n",
1236 _DBUS_ASSERT_ERROR_IS_SET (error);
1237 dbus_free (d->name);
1242 if (dbus_connection_get_unix_user (connection, &uid))
1244 if (!adjust_connections_for_uid (d->connections,
1247 BUS_SET_OOM (error);
1248 dbus_free (d->name);
1254 /* Now the connection is active, move it between lists */
1255 _dbus_list_unlink (&d->connections->incomplete,
1256 d->link_in_connection_list);
1257 d->connections->n_incomplete -= 1;
1258 _dbus_list_append_link (&d->connections->completed,
1259 d->link_in_connection_list);
1260 d->connections->n_completed += 1;
1262 _dbus_assert (d->connections->n_incomplete >= 0);
1263 _dbus_assert (d->connections->n_completed > 0);
1265 /* See if we can remove the timeout */
1266 bus_connections_expire_incomplete (d->connections);
1268 _dbus_assert (bus_connection_is_active (connection));
1274 bus_connection_get_name (DBusConnection *connection)
1276 BusConnectionData *d;
1278 d = BUS_CONNECTION_DATA (connection);
1279 _dbus_assert (d != NULL);
1285 * Check whether completing the passed-in connection would
1286 * exceed limits, and if so set error and return #FALSE
1289 bus_connections_check_limits (BusConnections *connections,
1290 DBusConnection *requesting_completion,
1293 BusConnectionData *d;
1296 d = BUS_CONNECTION_DATA (requesting_completion);
1297 _dbus_assert (d != NULL);
1299 _dbus_assert (d->name == NULL);
1301 if (connections->n_completed >=
1302 bus_context_get_max_completed_connections (connections->context))
1304 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1305 "The maximum number of active connections has been reached");
1309 if (dbus_connection_get_unix_user (requesting_completion, &uid))
1311 if (get_connections_for_uid (connections, uid) >=
1312 bus_context_get_max_connections_per_user (connections->context))
1314 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1315 "The maximum number of active connections for UID %lu has been reached",
1328 * Note that this is fairly fragile; in particular, don't try to use
1329 * one transaction across any main loop iterations.
1334 BusTransaction *transaction;
1335 DBusMessage *message;
1336 DBusPreallocatedSend *preallocated;
1341 BusTransactionCancelFunction cancel_function;
1342 DBusFreeFunction free_data_function;
1346 struct BusTransaction
1348 DBusList *connections;
1349 BusContext *context;
1350 DBusList *cancel_hooks;
1354 message_to_send_free (DBusConnection *connection,
1355 MessageToSend *to_send)
1357 if (to_send->message)
1358 dbus_message_unref (to_send->message);
1360 if (to_send->preallocated)
1361 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1363 dbus_free (to_send);
1367 cancel_hook_cancel (void *element,
1370 CancelHook *ch = element;
1372 _dbus_verbose ("Running transaction cancel hook\n");
1374 if (ch->cancel_function)
1375 (* ch->cancel_function) (ch->data);
1379 cancel_hook_free (void *element,
1382 CancelHook *ch = element;
1384 if (ch->free_data_function)
1385 (* ch->free_data_function) (ch->data);
1391 free_cancel_hooks (BusTransaction *transaction)
1393 _dbus_list_foreach (&transaction->cancel_hooks,
1394 cancel_hook_free, NULL);
1396 _dbus_list_clear (&transaction->cancel_hooks);
1400 bus_transaction_new (BusContext *context)
1402 BusTransaction *transaction;
1404 transaction = dbus_new0 (BusTransaction, 1);
1405 if (transaction == NULL)
1408 transaction->context = context;
1414 bus_transaction_get_context (BusTransaction *transaction)
1416 return transaction->context;
1420 bus_transaction_get_connections (BusTransaction *transaction)
1422 return bus_context_get_connections (transaction->context);
1426 bus_transaction_send_from_driver (BusTransaction *transaction,
1427 DBusConnection *connection,
1428 DBusMessage *message)
1430 /* We have to set the sender to the driver, and have
1431 * to check security policy since it was not done in
1434 _dbus_verbose ("Sending %s %s %s from driver\n",
1435 dbus_message_get_interface (message) ?
1436 dbus_message_get_interface (message) : "(no interface)",
1437 dbus_message_get_member (message) ?
1438 dbus_message_get_member (message) : "(no member)",
1439 dbus_message_get_error_name (message) ?
1440 dbus_message_get_error_name (message) : "(no error name)");
1442 if (!dbus_message_set_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1445 /* If security policy doesn't allow the message, we silently
1446 * eat it; the driver doesn't care about getting a reply.
1448 if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
1449 NULL, connection, connection, message, NULL))
1452 return bus_transaction_send (transaction, connection, message);
1456 bus_transaction_send (BusTransaction *transaction,
1457 DBusConnection *connection,
1458 DBusMessage *message)
1460 MessageToSend *to_send;
1461 BusConnectionData *d;
1464 _dbus_verbose (" trying to add %s interface=%s member=%s error=%s to transaction%s\n",
1465 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
1466 dbus_message_get_reply_serial (message) != 0 ? "reply" :
1468 dbus_message_get_interface (message) ?
1469 dbus_message_get_interface (message) : "(unset)",
1470 dbus_message_get_member (message) ?
1471 dbus_message_get_member (message) : "(unset)",
1472 dbus_message_get_error_name (message) ?
1473 dbus_message_get_error_name (message) : "(unset)",
1474 dbus_connection_get_is_connected (connection) ?
1475 "" : " (disconnected)");
1477 _dbus_assert (dbus_message_get_sender (message) != NULL);
1479 if (!dbus_connection_get_is_connected (connection))
1480 return TRUE; /* silently ignore disconnected connections */
1482 d = BUS_CONNECTION_DATA (connection);
1483 _dbus_assert (d != NULL);
1485 to_send = dbus_new (MessageToSend, 1);
1486 if (to_send == NULL)
1491 to_send->preallocated = dbus_connection_preallocate_send (connection);
1492 if (to_send->preallocated == NULL)
1494 dbus_free (to_send);
1498 dbus_message_ref (message);
1499 to_send->message = message;
1500 to_send->transaction = transaction;
1502 _dbus_verbose ("about to prepend message\n");
1504 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
1506 message_to_send_free (connection, to_send);
1510 _dbus_verbose ("prepended message\n");
1512 /* See if we already had this connection in the list
1513 * for this transaction. If we have a pending message,
1514 * then we should already be in transaction->connections
1516 link = _dbus_list_get_first_link (&d->transaction_messages);
1517 _dbus_assert (link->data == to_send);
1518 link = _dbus_list_get_next_link (&d->transaction_messages, link);
1519 while (link != NULL)
1521 MessageToSend *m = link->data;
1522 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1524 if (m->transaction == transaction)
1532 if (!_dbus_list_prepend (&transaction->connections, connection))
1534 _dbus_list_remove (&d->transaction_messages, to_send);
1535 message_to_send_free (connection, to_send);
1544 connection_cancel_transaction (DBusConnection *connection,
1545 BusTransaction *transaction)
1548 BusConnectionData *d;
1550 d = BUS_CONNECTION_DATA (connection);
1551 _dbus_assert (d != NULL);
1553 link = _dbus_list_get_first_link (&d->transaction_messages);
1554 while (link != NULL)
1556 MessageToSend *m = link->data;
1557 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
1559 if (m->transaction == transaction)
1561 _dbus_list_remove_link (&d->transaction_messages,
1564 message_to_send_free (connection, m);
1572 bus_transaction_cancel_and_free (BusTransaction *transaction)
1574 DBusConnection *connection;
1576 _dbus_verbose ("TRANSACTION: cancelled\n");
1578 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1579 connection_cancel_transaction (connection, transaction);
1581 _dbus_assert (transaction->connections == NULL);
1583 _dbus_list_foreach (&transaction->cancel_hooks,
1584 cancel_hook_cancel, NULL);
1586 free_cancel_hooks (transaction);
1588 dbus_free (transaction);
1592 connection_execute_transaction (DBusConnection *connection,
1593 BusTransaction *transaction)
1596 BusConnectionData *d;
1598 d = BUS_CONNECTION_DATA (connection);
1599 _dbus_assert (d != NULL);
1601 /* Send the queue in order (FIFO) */
1602 link = _dbus_list_get_last_link (&d->transaction_messages);
1603 while (link != NULL)
1605 MessageToSend *m = link->data;
1606 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
1608 if (m->transaction == transaction)
1610 _dbus_list_remove_link (&d->transaction_messages,
1613 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
1615 dbus_connection_send_preallocated (connection,
1620 m->preallocated = NULL; /* so we don't double-free it */
1622 message_to_send_free (connection, m);
1630 bus_transaction_execute_and_free (BusTransaction *transaction)
1632 /* For each connection in transaction->connections
1635 DBusConnection *connection;
1637 _dbus_verbose ("TRANSACTION: executing\n");
1639 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1640 connection_execute_transaction (connection, transaction);
1642 _dbus_assert (transaction->connections == NULL);
1644 free_cancel_hooks (transaction);
1646 dbus_free (transaction);
1650 bus_connection_remove_transactions (DBusConnection *connection)
1652 MessageToSend *to_send;
1653 BusConnectionData *d;
1655 d = BUS_CONNECTION_DATA (connection);
1656 _dbus_assert (d != NULL);
1658 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
1660 /* only has an effect for the first MessageToSend listing this transaction */
1661 _dbus_list_remove (&to_send->transaction->connections,
1664 _dbus_list_remove (&d->transaction_messages, to_send);
1665 message_to_send_free (connection, to_send);
1670 * Converts the DBusError to a message reply
1673 bus_transaction_send_error_reply (BusTransaction *transaction,
1674 DBusConnection *connection,
1675 const DBusError *error,
1676 DBusMessage *in_reply_to)
1680 _dbus_assert (error != NULL);
1681 _DBUS_ASSERT_ERROR_IS_SET (error);
1683 _dbus_verbose ("Sending error reply %s \"%s\"\n",
1684 error->name, error->message);
1686 reply = dbus_message_new_error (in_reply_to,
1692 if (!bus_transaction_send_from_driver (transaction, connection, reply))
1694 dbus_message_unref (reply);
1698 dbus_message_unref (reply);
1704 bus_transaction_add_cancel_hook (BusTransaction *transaction,
1705 BusTransactionCancelFunction cancel_function,
1707 DBusFreeFunction free_data_function)
1711 ch = dbus_new (CancelHook, 1);
1715 ch->cancel_function = cancel_function;
1717 ch->free_data_function = free_data_function;
1719 /* It's important that the hooks get run in reverse order that they
1722 if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))