1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "connection.h"
28 #include <dbus/dbus-list.h>
30 static void bus_connection_remove_transactions (DBusConnection *connection);
35 DBusList *list; /**< List of all the connections */
39 static int connection_data_slot = -1;
40 static int connection_data_slot_refcount = 0;
44 BusConnections *connections;
45 DBusConnection *connection;
46 DBusList *services_owned;
48 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
49 DBusMessage *oom_message;
50 DBusPreallocatedSend *oom_preallocated;
51 unsigned long *group_ids;
56 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
59 connection_data_slot_ref (void)
61 if (connection_data_slot < 0)
63 connection_data_slot = dbus_connection_allocate_data_slot ();
65 if (connection_data_slot < 0)
68 _dbus_assert (connection_data_slot_refcount == 0);
71 connection_data_slot_refcount += 1;
78 connection_data_slot_unref (void)
80 _dbus_assert (connection_data_slot_refcount > 0);
82 connection_data_slot_refcount -= 1;
84 if (connection_data_slot_refcount == 0)
86 dbus_connection_free_data_slot (connection_data_slot);
87 connection_data_slot = -1;
92 connection_get_loop (DBusConnection *connection)
96 d = BUS_CONNECTION_DATA (connection);
98 return bus_context_get_loop (d->connections->context);
102 bus_connection_disconnected (DBusConnection *connection)
104 BusConnectionData *d;
107 d = BUS_CONNECTION_DATA (connection);
108 _dbus_assert (d != NULL);
110 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
111 d->name ? d->name : "(inactive)");
113 /* Drop any service ownership. FIXME Unfortunately, this requires
114 * memory allocation and there doesn't seem to be a good way to
115 * handle it other than sleeping; we can't "fail" the operation of
116 * disconnecting a client, and preallocating a broadcast "service is
117 * now gone" message for every client-service pair seems kind of
118 * involved. Probably we need to do that though, and also
119 * extend BusTransaction to be able to revert generic
120 * stuff, not just sending a message (so we can e.g. revert
121 * removal of service owners).
123 while ((service = _dbus_list_get_last (&d->services_owned)))
125 BusTransaction *transaction;
130 dbus_error_init (&error);
133 while (transaction == NULL)
135 transaction = bus_transaction_new (d->connections->context);
136 _dbus_wait_for_memory ();
139 if (!bus_service_remove_owner (service, connection,
140 transaction, &error))
142 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
144 dbus_error_free (&error);
145 bus_transaction_cancel_and_free (transaction);
146 _dbus_wait_for_memory ();
150 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
153 bus_transaction_execute_and_free (transaction);
156 bus_dispatch_remove_connection (connection);
158 /* no more watching */
159 if (!dbus_connection_set_watch_functions (connection,
163 _dbus_assert_not_reached ("setting watch functions to NULL failed");
165 if (!dbus_connection_set_timeout_functions (connection,
169 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
171 dbus_connection_set_unix_user_function (connection,
174 dbus_connection_set_dispatch_status_function (connection,
177 bus_connection_remove_transactions (connection);
179 _dbus_list_remove (&d->connections->list, connection);
181 /* frees "d" as side effect */
182 dbus_connection_set_data (connection,
183 connection_data_slot,
186 dbus_connection_unref (connection);
190 connection_watch_callback (DBusWatch *watch,
191 unsigned int condition,
194 DBusConnection *connection = data;
197 retval = dbus_connection_handle_watch (connection, watch, condition);
203 add_connection_watch (DBusWatch *watch,
206 DBusConnection *connection = data;
208 return _dbus_loop_add_watch (connection_get_loop (connection),
209 watch, connection_watch_callback, connection,
214 remove_connection_watch (DBusWatch *watch,
217 DBusConnection *connection = data;
219 _dbus_loop_remove_watch (connection_get_loop (connection),
220 watch, connection_watch_callback, connection);
224 connection_timeout_callback (DBusTimeout *timeout,
227 DBusConnection *connection = data;
229 /* can return FALSE on OOM but we just let it fire again later */
230 dbus_timeout_handle (timeout);
234 add_connection_timeout (DBusTimeout *timeout,
237 DBusConnection *connection = data;
239 return _dbus_loop_add_timeout (connection_get_loop (connection),
240 timeout, connection_timeout_callback, connection, NULL);
244 remove_connection_timeout (DBusTimeout *timeout,
247 DBusConnection *connection = data;
249 _dbus_loop_remove_timeout (connection_get_loop (connection),
250 timeout, connection_timeout_callback, connection);
254 dispatch_status_function (DBusConnection *connection,
255 DBusDispatchStatus new_status,
258 DBusLoop *loop = data;
260 if (new_status != DBUS_DISPATCH_COMPLETE)
262 while (!_dbus_loop_queue_dispatch (loop, connection))
263 _dbus_wait_for_memory ();
268 allow_user_function (DBusConnection *connection,
272 BusConnectionData *d;
274 d = BUS_CONNECTION_DATA (connection);
276 _dbus_assert (d != NULL);
278 return TRUE; /* FIXME - this is just until we can parse a config file */
280 return bus_context_allow_user (d->connections->context, uid);
284 free_connection_data (void *data)
286 BusConnectionData *d = data;
288 /* services_owned should be NULL since we should be disconnected */
289 _dbus_assert (d->services_owned == NULL);
291 _dbus_assert (d->transaction_messages == NULL);
293 if (d->oom_preallocated)
294 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
297 dbus_message_unref (d->oom_message);
300 bus_policy_unref (d->policy);
302 dbus_free (d->group_ids);
310 bus_connections_new (BusContext *context)
312 BusConnections *connections;
314 if (!connection_data_slot_ref ())
317 connections = dbus_new0 (BusConnections, 1);
318 if (connections == NULL)
320 connection_data_slot_unref ();
324 connections->refcount = 1;
325 connections->context = context;
331 bus_connections_ref (BusConnections *connections)
333 _dbus_assert (connections->refcount > 0);
334 connections->refcount += 1;
338 bus_connections_unref (BusConnections *connections)
340 _dbus_assert (connections->refcount > 0);
341 connections->refcount -= 1;
342 if (connections->refcount == 0)
344 while (connections->list != NULL)
346 DBusConnection *connection;
348 connection = connections->list->data;
350 dbus_connection_ref (connection);
351 dbus_connection_disconnect (connection);
352 bus_connection_disconnected (connection);
353 dbus_connection_unref (connection);
356 _dbus_list_clear (&connections->list);
358 dbus_free (connections);
360 connection_data_slot_unref ();
365 bus_connections_setup_connection (BusConnections *connections,
366 DBusConnection *connection)
368 BusConnectionData *d;
371 d = dbus_new0 (BusConnectionData, 1);
376 d->connections = connections;
377 d->connection = connection;
379 _dbus_assert (connection_data_slot >= 0);
381 if (!dbus_connection_set_data (connection,
382 connection_data_slot,
383 d, free_connection_data))
394 if (!dbus_connection_set_watch_functions (connection,
395 add_connection_watch,
396 remove_connection_watch,
402 if (!dbus_connection_set_timeout_functions (connection,
403 add_connection_timeout,
404 remove_connection_timeout,
410 dbus_connection_set_unix_user_function (connection,
414 dbus_connection_set_dispatch_status_function (connection,
415 dispatch_status_function,
416 bus_context_get_loop (connections->context),
419 /* Setup the connection with the dispatcher */
420 if (!bus_dispatch_add_connection (connection))
423 if (!_dbus_list_append (&connections->list, connection))
425 bus_dispatch_remove_connection (connection);
429 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
431 if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
433 bus_dispatch_remove_connection (connection);
438 dbus_connection_ref (connection);
444 if (!dbus_connection_set_watch_functions (connection,
448 _dbus_assert_not_reached ("setting watch functions to NULL failed");
450 if (!dbus_connection_set_timeout_functions (connection,
454 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
456 dbus_connection_set_unix_user_function (connection,
459 dbus_connection_set_dispatch_status_function (connection,
462 if (!dbus_connection_set_data (connection,
463 connection_data_slot,
465 _dbus_assert_not_reached ("failed to set connection data to null");
472 bus_connection_get_groups (DBusConnection *connection,
473 const unsigned long **groups,
476 BusConnectionData *d;
478 d = BUS_CONNECTION_DATA (connection);
480 _dbus_assert (d != NULL);
485 /* we do a lazy lookup on groups a user is in for two reasons:
486 * 1) we can't do it on connection setup since the user
487 * hasn't authenticated and 2) it might be expensive
488 * and we don't need to do it if there are no group-based
489 * rules in the config file
492 if (d->n_group_ids == 0)
496 if (dbus_connection_get_unix_user (connection, &uid))
498 if (!_dbus_get_groups (uid, &d->group_ids, &d->n_group_ids))
500 _dbus_verbose ("Did not get any groups for UID %lu\n",
507 *groups = d->group_ids;
508 *n_groups = d->n_group_ids;
514 bus_connection_is_in_group (DBusConnection *connection,
518 const unsigned long *group_ids;
521 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
525 while (i < n_group_ids)
527 if (group_ids[i] == gid)
536 bus_connection_get_policy (DBusConnection *connection)
538 BusConnectionData *d;
540 d = BUS_CONNECTION_DATA (connection);
542 _dbus_assert (d != NULL);
544 if (!dbus_connection_get_is_authenticated (connection))
546 _dbus_verbose ("Tried to get policy for unauthenticated connection!\n");
550 /* We do lazy creation of the policy because
551 * it can only be done post-authentication.
553 if (d->policy == NULL)
556 bus_context_create_connection_policy (d->connections->context,
559 /* we may have a NULL policy on OOM or error getting list of
560 * groups for a user. In the latter case we don't handle it so
561 * well currently, just keep pretending we're out of memory,
562 * which is kind of bizarre.
570 * Calls function on each connection; if the function returns
571 * #FALSE, stops iterating.
573 * @param connections the connections object
574 * @param function the function
575 * @param data data to pass to it as a second arg
578 bus_connections_foreach (BusConnections *connections,
579 BusConnectionForeachFunction function,
584 link = _dbus_list_get_first_link (&connections->list);
587 DBusConnection *connection = link->data;
588 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
590 if (!(* function) (connection, data))
598 bus_connections_get_context (BusConnections *connections)
600 return connections->context;
604 bus_connection_get_context (DBusConnection *connection)
606 BusConnectionData *d;
608 d = BUS_CONNECTION_DATA (connection);
610 _dbus_assert (d != NULL);
612 return d->connections->context;
616 bus_connection_get_connections (DBusConnection *connection)
618 BusConnectionData *d;
620 d = BUS_CONNECTION_DATA (connection);
622 _dbus_assert (d != NULL);
624 return d->connections;
628 bus_connection_get_registry (DBusConnection *connection)
630 BusConnectionData *d;
632 d = BUS_CONNECTION_DATA (connection);
634 _dbus_assert (d != NULL);
636 return bus_context_get_registry (d->connections->context);
640 bus_connection_get_activation (DBusConnection *connection)
642 BusConnectionData *d;
644 d = BUS_CONNECTION_DATA (connection);
646 _dbus_assert (d != NULL);
648 return bus_context_get_activation (d->connections->context);
652 * Checks whether the connection is registered with the message bus.
654 * @param connection the connection
655 * @returns #TRUE if we're an active message bus participant
658 bus_connection_is_active (DBusConnection *connection)
660 BusConnectionData *d;
662 d = BUS_CONNECTION_DATA (connection);
664 return d != NULL && d->name != NULL;
668 bus_connection_preallocate_oom_error (DBusConnection *connection)
670 DBusMessage *message;
671 DBusPreallocatedSend *preallocated;
672 BusConnectionData *d;
674 d = BUS_CONNECTION_DATA (connection);
676 _dbus_assert (d != NULL);
678 if (d->oom_preallocated != NULL)
681 preallocated = dbus_connection_preallocate_send (connection);
682 if (preallocated == NULL)
685 /* d->name may be NULL, but that should be OK */
686 message = dbus_message_new (d->name,
687 DBUS_ERROR_NO_MEMORY);
690 dbus_connection_free_preallocated_send (connection, preallocated);
694 dbus_message_set_is_error (message, TRUE);
696 if (!dbus_message_set_sender (message,
699 dbus_connection_free_preallocated_send (connection, preallocated);
700 dbus_message_unref (message);
704 /* set reply serial to placeholder value just so space is already allocated
707 if (!dbus_message_set_reply_serial (message, 14))
709 dbus_connection_free_preallocated_send (connection, preallocated);
710 dbus_message_unref (message);
714 d->oom_message = message;
715 d->oom_preallocated = preallocated;
721 bus_connection_send_oom_error (DBusConnection *connection,
722 DBusMessage *in_reply_to)
724 BusConnectionData *d;
726 d = BUS_CONNECTION_DATA (connection);
728 _dbus_assert (d != NULL);
729 _dbus_assert (d->oom_message != NULL);
731 /* should always succeed since we set it to a placeholder earlier */
732 if (!dbus_message_set_reply_serial (d->oom_message,
733 dbus_message_get_serial (in_reply_to)))
734 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
736 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
738 dbus_connection_send_preallocated (connection, d->oom_preallocated,
739 d->oom_message, NULL);
741 dbus_message_unref (d->oom_message);
742 d->oom_message = NULL;
743 d->oom_preallocated = NULL;
747 bus_connection_add_owned_service (DBusConnection *connection,
750 BusConnectionData *d;
752 d = BUS_CONNECTION_DATA (connection);
753 _dbus_assert (d != NULL);
755 if (!_dbus_list_append (&d->services_owned,
763 bus_connection_remove_owned_service (DBusConnection *connection,
766 BusConnectionData *d;
768 d = BUS_CONNECTION_DATA (connection);
769 _dbus_assert (d != NULL);
771 _dbus_list_remove_last (&d->services_owned, service);
775 bus_connection_set_name (DBusConnection *connection,
776 const DBusString *name)
778 BusConnectionData *d;
780 d = BUS_CONNECTION_DATA (connection);
781 _dbus_assert (d != NULL);
782 _dbus_assert (d->name == NULL);
784 if (!_dbus_string_copy_data (name, &d->name))
787 _dbus_assert (d->name != NULL);
789 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
795 bus_connection_get_name (DBusConnection *connection)
797 BusConnectionData *d;
799 d = BUS_CONNECTION_DATA (connection);
800 _dbus_assert (d != NULL);
807 BusTransaction *transaction;
808 DBusMessage *message;
809 DBusPreallocatedSend *preallocated;
812 struct BusTransaction
814 DBusList *connections;
819 message_to_send_free (DBusConnection *connection,
820 MessageToSend *to_send)
822 if (to_send->message)
823 dbus_message_unref (to_send->message);
825 if (to_send->preallocated)
826 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
832 bus_transaction_new (BusContext *context)
834 BusTransaction *transaction;
836 transaction = dbus_new0 (BusTransaction, 1);
837 if (transaction == NULL)
840 transaction->context = context;
846 bus_transaction_get_context (BusTransaction *transaction)
848 return transaction->context;
852 bus_transaction_get_connections (BusTransaction *transaction)
854 return bus_context_get_connections (transaction->context);
858 bus_transaction_send_message (BusTransaction *transaction,
859 DBusConnection *connection,
860 DBusMessage *message)
862 MessageToSend *to_send;
863 BusConnectionData *d;
866 _dbus_verbose (" trying to add message %s to transaction%s\n",
867 dbus_message_get_name (message),
868 dbus_connection_get_is_connected (connection) ?
869 "" : " (disconnected)");
871 _dbus_assert (dbus_message_get_sender (message) != NULL);
873 if (!dbus_connection_get_is_connected (connection))
874 return TRUE; /* silently ignore disconnected connections */
876 d = BUS_CONNECTION_DATA (connection);
877 _dbus_assert (d != NULL);
879 to_send = dbus_new (MessageToSend, 1);
885 to_send->preallocated = dbus_connection_preallocate_send (connection);
886 if (to_send->preallocated == NULL)
892 dbus_message_ref (message);
893 to_send->message = message;
894 to_send->transaction = transaction;
896 _dbus_verbose ("about to prepend message\n");
898 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
900 message_to_send_free (connection, to_send);
904 _dbus_verbose ("prepended message\n");
906 /* See if we already had this connection in the list
907 * for this transaction. If we have a pending message,
908 * then we should already be in transaction->connections
910 link = _dbus_list_get_first_link (&d->transaction_messages);
911 _dbus_assert (link->data == to_send);
912 link = _dbus_list_get_next_link (&d->transaction_messages, link);
915 MessageToSend *m = link->data;
916 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
918 if (m->transaction == transaction)
926 if (!_dbus_list_prepend (&transaction->connections, connection))
928 _dbus_list_remove (&d->transaction_messages, to_send);
929 message_to_send_free (connection, to_send);
938 connection_cancel_transaction (DBusConnection *connection,
939 BusTransaction *transaction)
942 BusConnectionData *d;
944 d = BUS_CONNECTION_DATA (connection);
945 _dbus_assert (d != NULL);
947 link = _dbus_list_get_first_link (&d->transaction_messages);
950 MessageToSend *m = link->data;
951 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
953 if (m->transaction == transaction)
955 _dbus_list_remove_link (&d->transaction_messages,
958 message_to_send_free (connection, m);
966 bus_transaction_cancel_and_free (BusTransaction *transaction)
968 DBusConnection *connection;
970 _dbus_verbose ("TRANSACTION: cancelled\n");
972 while ((connection = _dbus_list_pop_first (&transaction->connections)))
973 connection_cancel_transaction (connection, transaction);
975 _dbus_assert (transaction->connections == NULL);
977 dbus_free (transaction);
981 connection_execute_transaction (DBusConnection *connection,
982 BusTransaction *transaction)
985 BusConnectionData *d;
987 d = BUS_CONNECTION_DATA (connection);
988 _dbus_assert (d != NULL);
990 /* Send the queue in order (FIFO) */
991 link = _dbus_list_get_last_link (&d->transaction_messages);
994 MessageToSend *m = link->data;
995 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
997 if (m->transaction == transaction)
999 _dbus_list_remove_link (&d->transaction_messages,
1002 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
1004 dbus_connection_send_preallocated (connection,
1009 m->preallocated = NULL; /* so we don't double-free it */
1011 message_to_send_free (connection, m);
1019 bus_transaction_execute_and_free (BusTransaction *transaction)
1021 /* For each connection in transaction->connections
1024 DBusConnection *connection;
1026 _dbus_verbose ("TRANSACTION: executing\n");
1028 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1029 connection_execute_transaction (connection, transaction);
1031 _dbus_assert (transaction->connections == NULL);
1033 dbus_free (transaction);
1037 bus_connection_remove_transactions (DBusConnection *connection)
1039 MessageToSend *to_send;
1040 BusConnectionData *d;
1042 d = BUS_CONNECTION_DATA (connection);
1043 _dbus_assert (d != NULL);
1045 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
1047 /* only has an effect for the first MessageToSend listing this transaction */
1048 _dbus_list_remove (&to_send->transaction->connections,
1051 _dbus_list_remove (&d->transaction_messages, to_send);
1052 message_to_send_free (connection, to_send);
1057 * Converts the DBusError to a message reply
1060 bus_transaction_send_error_reply (BusTransaction *transaction,
1061 DBusConnection *connection,
1062 const DBusError *error,
1063 DBusMessage *in_reply_to)
1067 _dbus_assert (error != NULL);
1068 _DBUS_ASSERT_ERROR_IS_SET (error);
1070 reply = dbus_message_new_error_reply (in_reply_to,
1076 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS) ||
1077 !bus_transaction_send_message (transaction, connection, reply))
1079 dbus_message_unref (reply);
1083 dbus_message_unref (reply);