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 bus_connection_remove_transactions (connection);
176 _dbus_list_remove (&d->connections->list, connection);
178 /* frees "d" as side effect */
179 dbus_connection_set_data (connection,
180 connection_data_slot,
183 dbus_connection_unref (connection);
187 connection_watch_callback (DBusWatch *watch,
188 unsigned int condition,
191 DBusConnection *connection = data;
194 dbus_connection_ref (connection);
196 retval = dbus_connection_handle_watch (connection, watch, condition);
198 bus_connection_dispatch_all_messages (connection);
200 dbus_connection_unref (connection);
206 add_connection_watch (DBusWatch *watch,
209 DBusConnection *connection = data;
211 return _dbus_loop_add_watch (connection_get_loop (connection),
212 watch, connection_watch_callback, connection,
217 remove_connection_watch (DBusWatch *watch,
220 DBusConnection *connection = data;
222 _dbus_loop_remove_watch (connection_get_loop (connection),
223 watch, connection_watch_callback, connection);
227 connection_timeout_callback (DBusTimeout *timeout,
230 DBusConnection *connection = data;
232 dbus_connection_ref (connection);
234 /* can return FALSE on OOM but we just let it fire again later */
235 dbus_timeout_handle (timeout);
237 bus_connection_dispatch_all_messages (connection);
239 dbus_connection_unref (connection);
243 add_connection_timeout (DBusTimeout *timeout,
246 DBusConnection *connection = data;
248 return _dbus_loop_add_timeout (connection_get_loop (connection),
249 timeout, connection_timeout_callback, connection, NULL);
253 remove_connection_timeout (DBusTimeout *timeout,
256 DBusConnection *connection = data;
258 _dbus_loop_remove_timeout (connection_get_loop (connection),
259 timeout, connection_timeout_callback, connection);
263 allow_user_function (DBusConnection *connection,
267 BusConnectionData *d;
269 d = BUS_CONNECTION_DATA (connection);
271 _dbus_assert (d != NULL);
273 return TRUE; /* FIXME - this is just until we can parse a config file */
275 return bus_context_allow_user (d->connections->context, uid);
279 free_connection_data (void *data)
281 BusConnectionData *d = data;
283 /* services_owned should be NULL since we should be disconnected */
284 _dbus_assert (d->services_owned == NULL);
286 _dbus_assert (d->transaction_messages == NULL);
288 if (d->oom_preallocated)
289 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
292 dbus_message_unref (d->oom_message);
295 bus_policy_unref (d->policy);
297 dbus_free (d->group_ids);
305 bus_connections_new (BusContext *context)
307 BusConnections *connections;
309 if (!connection_data_slot_ref ())
312 connections = dbus_new0 (BusConnections, 1);
313 if (connections == NULL)
315 connection_data_slot_unref ();
319 connections->refcount = 1;
320 connections->context = context;
326 bus_connections_ref (BusConnections *connections)
328 _dbus_assert (connections->refcount > 0);
329 connections->refcount += 1;
333 bus_connections_unref (BusConnections *connections)
335 _dbus_assert (connections->refcount > 0);
336 connections->refcount -= 1;
337 if (connections->refcount == 0)
339 while (connections->list != NULL)
341 DBusConnection *connection;
343 connection = connections->list->data;
345 dbus_connection_ref (connection);
346 dbus_connection_disconnect (connection);
347 bus_connection_disconnected (connection);
348 dbus_connection_unref (connection);
351 _dbus_list_clear (&connections->list);
353 dbus_free (connections);
355 connection_data_slot_unref ();
360 bus_connections_setup_connection (BusConnections *connections,
361 DBusConnection *connection)
363 BusConnectionData *d;
366 d = dbus_new0 (BusConnectionData, 1);
371 d->connections = connections;
372 d->connection = connection;
374 _dbus_assert (connection_data_slot >= 0);
376 if (!dbus_connection_set_data (connection,
377 connection_data_slot,
378 d, free_connection_data))
389 if (!dbus_connection_set_watch_functions (connection,
390 add_connection_watch,
391 remove_connection_watch,
397 if (!dbus_connection_set_timeout_functions (connection,
398 add_connection_timeout,
399 remove_connection_timeout,
405 dbus_connection_set_unix_user_function (connection,
409 /* Setup the connection with the dispatcher */
410 if (!bus_dispatch_add_connection (connection))
413 if (!_dbus_list_append (&connections->list, connection))
415 bus_dispatch_remove_connection (connection);
419 dbus_connection_ref (connection);
425 if (!dbus_connection_set_watch_functions (connection,
429 _dbus_assert_not_reached ("setting watch functions to NULL failed");
431 if (!dbus_connection_set_timeout_functions (connection,
435 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
437 dbus_connection_set_unix_user_function (connection,
440 if (!dbus_connection_set_data (connection,
441 connection_data_slot,
443 _dbus_assert_not_reached ("failed to set connection data to null");
450 bus_connection_get_groups (DBusConnection *connection,
451 const unsigned long **groups,
454 BusConnectionData *d;
456 d = BUS_CONNECTION_DATA (connection);
458 _dbus_assert (d != NULL);
463 /* we do a lazy lookup on groups a user is in for two reasons:
464 * 1) we can't do it on connection setup since the user
465 * hasn't authenticated and 2) it might be expensive
466 * and we don't need to do it if there are no group-based
467 * rules in the config file
470 if (d->n_group_ids == 0)
474 if (dbus_connection_get_unix_user (connection, &uid))
476 if (!_dbus_get_groups (uid, &d->group_ids, &d->n_group_ids))
478 _dbus_verbose ("Did not get any groups for UID %lu\n",
485 *groups = d->group_ids;
486 *n_groups = d->n_group_ids;
492 bus_connection_is_in_group (DBusConnection *connection,
496 const unsigned long *group_ids;
499 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
503 while (i < n_group_ids)
505 if (group_ids[i] == gid)
514 bus_connection_get_policy (DBusConnection *connection)
516 BusConnectionData *d;
518 d = BUS_CONNECTION_DATA (connection);
520 _dbus_assert (d != NULL);
522 if (!dbus_connection_get_is_authenticated (connection))
524 _dbus_verbose ("Tried to get policy for unauthenticated connection!\n");
528 /* We do lazy creation of the policy because
529 * it can only be done post-authentication.
531 if (d->policy == NULL)
534 bus_context_create_connection_policy (d->connections->context,
537 /* we may have a NULL policy on OOM or error getting list of
538 * groups for a user. In the latter case we don't handle it so
539 * well currently, just keep pretending we're out of memory,
540 * which is kind of bizarre.
548 * Calls function on each connection; if the function returns
549 * #FALSE, stops iterating.
551 * @param connections the connections object
552 * @param function the function
553 * @param data data to pass to it as a second arg
556 bus_connections_foreach (BusConnections *connections,
557 BusConnectionForeachFunction function,
562 link = _dbus_list_get_first_link (&connections->list);
565 DBusConnection *connection = link->data;
566 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
568 if (!(* function) (connection, data))
576 bus_connections_get_context (BusConnections *connections)
578 return connections->context;
582 bus_connection_get_context (DBusConnection *connection)
584 BusConnectionData *d;
586 d = BUS_CONNECTION_DATA (connection);
588 _dbus_assert (d != NULL);
590 return d->connections->context;
594 bus_connection_get_connections (DBusConnection *connection)
596 BusConnectionData *d;
598 d = BUS_CONNECTION_DATA (connection);
600 _dbus_assert (d != NULL);
602 return d->connections;
606 bus_connection_get_registry (DBusConnection *connection)
608 BusConnectionData *d;
610 d = BUS_CONNECTION_DATA (connection);
612 _dbus_assert (d != NULL);
614 return bus_context_get_registry (d->connections->context);
618 bus_connection_get_activation (DBusConnection *connection)
620 BusConnectionData *d;
622 d = BUS_CONNECTION_DATA (connection);
624 _dbus_assert (d != NULL);
626 return bus_context_get_activation (d->connections->context);
630 * Checks whether the connection is registered with the message bus.
632 * @param connection the connection
633 * @returns #TRUE if we're an active message bus participant
636 bus_connection_is_active (DBusConnection *connection)
638 BusConnectionData *d;
640 d = BUS_CONNECTION_DATA (connection);
642 return d != NULL && d->name != NULL;
646 bus_connection_preallocate_oom_error (DBusConnection *connection)
648 DBusMessage *message;
649 DBusPreallocatedSend *preallocated;
650 BusConnectionData *d;
652 d = BUS_CONNECTION_DATA (connection);
654 _dbus_assert (d != NULL);
656 if (d->oom_preallocated != NULL)
659 preallocated = dbus_connection_preallocate_send (connection);
660 if (preallocated == NULL)
663 /* d->name may be NULL, but that should be OK */
664 message = dbus_message_new (d->name,
665 DBUS_ERROR_NO_MEMORY);
668 dbus_connection_free_preallocated_send (connection, preallocated);
672 dbus_message_set_is_error (message, TRUE);
674 if (!dbus_message_set_sender (message,
677 dbus_connection_free_preallocated_send (connection, preallocated);
678 dbus_message_unref (message);
682 /* set reply serial to placeholder value just so space is already allocated
685 if (!dbus_message_set_reply_serial (message, 14))
687 dbus_connection_free_preallocated_send (connection, preallocated);
688 dbus_message_unref (message);
692 d->oom_message = message;
693 d->oom_preallocated = preallocated;
699 bus_connection_send_oom_error (DBusConnection *connection,
700 DBusMessage *in_reply_to)
702 BusConnectionData *d;
704 d = BUS_CONNECTION_DATA (connection);
706 _dbus_assert (d != NULL);
707 _dbus_assert (d->oom_message != NULL);
709 /* should always succeed since we set it to a placeholder earlier */
710 if (!dbus_message_set_reply_serial (d->oom_message,
711 dbus_message_get_serial (in_reply_to)))
712 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
714 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
716 dbus_connection_send_preallocated (connection, d->oom_preallocated,
717 d->oom_message, NULL);
719 dbus_message_unref (d->oom_message);
720 d->oom_message = NULL;
721 d->oom_preallocated = NULL;
725 bus_connection_add_owned_service (DBusConnection *connection,
728 BusConnectionData *d;
730 d = BUS_CONNECTION_DATA (connection);
731 _dbus_assert (d != NULL);
733 if (!_dbus_list_append (&d->services_owned,
741 bus_connection_remove_owned_service (DBusConnection *connection,
744 BusConnectionData *d;
746 d = BUS_CONNECTION_DATA (connection);
747 _dbus_assert (d != NULL);
749 _dbus_list_remove_last (&d->services_owned, service);
753 bus_connection_set_name (DBusConnection *connection,
754 const DBusString *name)
756 BusConnectionData *d;
758 d = BUS_CONNECTION_DATA (connection);
759 _dbus_assert (d != NULL);
760 _dbus_assert (d->name == NULL);
762 if (!_dbus_string_copy_data (name, &d->name))
765 _dbus_assert (d->name != NULL);
767 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
773 bus_connection_get_name (DBusConnection *connection)
775 BusConnectionData *d;
777 d = BUS_CONNECTION_DATA (connection);
778 _dbus_assert (d != NULL);
785 BusTransaction *transaction;
786 DBusMessage *message;
787 DBusPreallocatedSend *preallocated;
790 struct BusTransaction
792 DBusList *connections;
797 message_to_send_free (DBusConnection *connection,
798 MessageToSend *to_send)
800 if (to_send->message)
801 dbus_message_unref (to_send->message);
803 if (to_send->preallocated)
804 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
810 bus_transaction_new (BusContext *context)
812 BusTransaction *transaction;
814 transaction = dbus_new0 (BusTransaction, 1);
815 if (transaction == NULL)
818 transaction->context = context;
824 bus_transaction_get_context (BusTransaction *transaction)
826 return transaction->context;
830 bus_transaction_get_connections (BusTransaction *transaction)
832 return bus_context_get_connections (transaction->context);
836 bus_transaction_send_message (BusTransaction *transaction,
837 DBusConnection *connection,
838 DBusMessage *message)
840 MessageToSend *to_send;
841 BusConnectionData *d;
844 _dbus_verbose (" trying to add message %s to transaction%s\n",
845 dbus_message_get_name (message),
846 dbus_connection_get_is_connected (connection) ?
847 "" : " (disconnected)");
849 _dbus_assert (dbus_message_get_sender (message) != NULL);
851 if (!dbus_connection_get_is_connected (connection))
852 return TRUE; /* silently ignore disconnected connections */
854 d = BUS_CONNECTION_DATA (connection);
855 _dbus_assert (d != NULL);
857 to_send = dbus_new (MessageToSend, 1);
863 to_send->preallocated = dbus_connection_preallocate_send (connection);
864 if (to_send->preallocated == NULL)
870 dbus_message_ref (message);
871 to_send->message = message;
872 to_send->transaction = transaction;
874 _dbus_verbose ("about to prepend message\n");
876 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
878 message_to_send_free (connection, to_send);
882 _dbus_verbose ("prepended message\n");
884 /* See if we already had this connection in the list
885 * for this transaction. If we have a pending message,
886 * then we should already be in transaction->connections
888 link = _dbus_list_get_first_link (&d->transaction_messages);
889 _dbus_assert (link->data == to_send);
890 link = _dbus_list_get_next_link (&d->transaction_messages, link);
893 MessageToSend *m = link->data;
894 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
896 if (m->transaction == transaction)
904 if (!_dbus_list_prepend (&transaction->connections, connection))
906 _dbus_list_remove (&d->transaction_messages, to_send);
907 message_to_send_free (connection, to_send);
916 connection_cancel_transaction (DBusConnection *connection,
917 BusTransaction *transaction)
920 BusConnectionData *d;
922 d = BUS_CONNECTION_DATA (connection);
923 _dbus_assert (d != NULL);
925 link = _dbus_list_get_first_link (&d->transaction_messages);
928 MessageToSend *m = link->data;
929 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
931 if (m->transaction == transaction)
933 _dbus_list_remove_link (&d->transaction_messages,
936 message_to_send_free (connection, m);
944 bus_transaction_cancel_and_free (BusTransaction *transaction)
946 DBusConnection *connection;
948 _dbus_verbose ("TRANSACTION: cancelled\n");
950 while ((connection = _dbus_list_pop_first (&transaction->connections)))
951 connection_cancel_transaction (connection, transaction);
953 _dbus_assert (transaction->connections == NULL);
955 dbus_free (transaction);
959 connection_execute_transaction (DBusConnection *connection,
960 BusTransaction *transaction)
963 BusConnectionData *d;
965 d = BUS_CONNECTION_DATA (connection);
966 _dbus_assert (d != NULL);
968 /* Send the queue in order (FIFO) */
969 link = _dbus_list_get_last_link (&d->transaction_messages);
972 MessageToSend *m = link->data;
973 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
975 if (m->transaction == transaction)
977 _dbus_list_remove_link (&d->transaction_messages,
980 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
982 dbus_connection_send_preallocated (connection,
987 m->preallocated = NULL; /* so we don't double-free it */
989 message_to_send_free (connection, m);
997 bus_transaction_execute_and_free (BusTransaction *transaction)
999 /* For each connection in transaction->connections
1002 DBusConnection *connection;
1004 _dbus_verbose ("TRANSACTION: executing\n");
1006 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1007 connection_execute_transaction (connection, transaction);
1009 _dbus_assert (transaction->connections == NULL);
1011 dbus_free (transaction);
1015 bus_connection_remove_transactions (DBusConnection *connection)
1017 MessageToSend *to_send;
1018 BusConnectionData *d;
1020 d = BUS_CONNECTION_DATA (connection);
1021 _dbus_assert (d != NULL);
1023 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
1025 /* only has an effect for the first MessageToSend listing this transaction */
1026 _dbus_list_remove (&to_send->transaction->connections,
1029 _dbus_list_remove (&d->transaction_messages, to_send);
1030 message_to_send_free (connection, to_send);
1035 * Converts the DBusError to a message reply
1038 bus_transaction_send_error_reply (BusTransaction *transaction,
1039 DBusConnection *connection,
1040 const DBusError *error,
1041 DBusMessage *in_reply_to)
1045 _dbus_assert (error != NULL);
1046 _DBUS_ASSERT_ERROR_IS_SET (error);
1048 reply = dbus_message_new_error_reply (in_reply_to,
1054 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS) ||
1055 !bus_transaction_send_message (transaction, connection, reply))
1057 dbus_message_unref (reply);
1061 dbus_message_unref (reply);