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>
31 static void bus_connection_remove_transactions (DBusConnection *connection);
36 DBusList *list; /**< List of all the connections */
40 static int connection_data_slot = -1;
41 static int connection_data_slot_refcount = 0;
45 BusConnections *connections;
46 DBusConnection *connection;
47 DBusList *services_owned;
49 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
50 DBusMessage *oom_message;
51 DBusPreallocatedSend *oom_preallocated;
52 unsigned long *group_ids;
57 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
60 connection_data_slot_ref (void)
62 if (connection_data_slot < 0)
64 connection_data_slot = dbus_connection_allocate_data_slot ();
66 if (connection_data_slot < 0)
69 _dbus_assert (connection_data_slot_refcount == 0);
72 connection_data_slot_refcount += 1;
79 connection_data_slot_unref (void)
81 _dbus_assert (connection_data_slot_refcount > 0);
83 connection_data_slot_refcount -= 1;
85 if (connection_data_slot_refcount == 0)
87 dbus_connection_free_data_slot (connection_data_slot);
88 connection_data_slot = -1;
93 bus_connection_disconnected (DBusConnection *connection)
98 d = BUS_CONNECTION_DATA (connection);
99 _dbus_assert (d != NULL);
101 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
102 d->name ? d->name : "(inactive)");
104 /* Drop any service ownership. FIXME Unfortunately, this requires
105 * memory allocation and there doesn't seem to be a good way to
106 * handle it other than sleeping; we can't "fail" the operation of
107 * disconnecting a client, and preallocating a broadcast "service is
108 * now gone" message for every client-service pair seems kind of
109 * involved. Probably we need to do that though, and also
110 * extend BusTransaction to be able to revert generic
111 * stuff, not just sending a message (so we can e.g. revert
112 * removal of service owners).
114 while ((service = _dbus_list_get_last (&d->services_owned)))
116 BusTransaction *transaction;
121 dbus_error_init (&error);
124 while (transaction == NULL)
126 transaction = bus_transaction_new (d->connections->context);
127 bus_wait_for_memory ();
130 if (!bus_service_remove_owner (service, connection,
131 transaction, &error))
133 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
135 dbus_error_free (&error);
136 bus_transaction_cancel_and_free (transaction);
137 bus_wait_for_memory ();
141 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
144 bus_transaction_execute_and_free (transaction);
147 bus_dispatch_remove_connection (connection);
149 /* no more watching */
150 if (!dbus_connection_set_watch_functions (connection,
154 _dbus_assert_not_reached ("setting watch functions to NULL failed");
156 if (!dbus_connection_set_timeout_functions (connection,
160 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
162 dbus_connection_set_unix_user_function (connection,
165 bus_connection_remove_transactions (connection);
167 _dbus_list_remove (&d->connections->list, connection);
169 /* frees "d" as side effect */
170 dbus_connection_set_data (connection,
171 connection_data_slot,
174 dbus_connection_unref (connection);
178 connection_watch_callback (DBusWatch *watch,
179 unsigned int condition,
182 DBusConnection *connection = data;
185 dbus_connection_ref (connection);
187 retval = dbus_connection_handle_watch (connection, watch, condition);
189 bus_connection_dispatch_all_messages (connection);
191 dbus_connection_unref (connection);
197 add_connection_watch (DBusWatch *watch,
198 DBusConnection *connection)
200 return bus_loop_add_watch (watch, connection_watch_callback, connection,
205 remove_connection_watch (DBusWatch *watch,
206 DBusConnection *connection)
208 bus_loop_remove_watch (watch, connection_watch_callback, connection);
212 connection_timeout_callback (DBusTimeout *timeout,
215 DBusConnection *connection = data;
217 dbus_connection_ref (connection);
219 /* can return FALSE on OOM but we just let it fire again later */
220 dbus_timeout_handle (timeout);
222 bus_connection_dispatch_all_messages (connection);
224 dbus_connection_unref (connection);
228 add_connection_timeout (DBusTimeout *timeout,
229 DBusConnection *connection)
231 return bus_loop_add_timeout (timeout, connection_timeout_callback, connection, NULL);
235 remove_connection_timeout (DBusTimeout *timeout,
236 DBusConnection *connection)
238 bus_loop_remove_timeout (timeout, connection_timeout_callback, connection);
242 allow_user_function (DBusConnection *connection,
246 BusConnectionData *d;
248 d = BUS_CONNECTION_DATA (connection);
250 _dbus_assert (d != NULL);
252 return TRUE; /* FIXME - this is just until we can parse a config file */
254 return bus_context_allow_user (d->connections->context, uid);
258 free_connection_data (void *data)
260 BusConnectionData *d = data;
262 /* services_owned should be NULL since we should be disconnected */
263 _dbus_assert (d->services_owned == NULL);
265 _dbus_assert (d->transaction_messages == NULL);
267 if (d->oom_preallocated)
268 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
271 dbus_message_unref (d->oom_message);
274 bus_policy_unref (d->policy);
276 dbus_free (d->group_ids);
284 bus_connections_new (BusContext *context)
286 BusConnections *connections;
288 if (!connection_data_slot_ref ())
291 connections = dbus_new0 (BusConnections, 1);
292 if (connections == NULL)
294 connection_data_slot_unref ();
298 connections->refcount = 1;
299 connections->context = context;
305 bus_connections_ref (BusConnections *connections)
307 _dbus_assert (connections->refcount > 0);
308 connections->refcount += 1;
312 bus_connections_unref (BusConnections *connections)
314 _dbus_assert (connections->refcount > 0);
315 connections->refcount -= 1;
316 if (connections->refcount == 0)
318 while (connections->list != NULL)
320 DBusConnection *connection;
322 connection = connections->list->data;
324 dbus_connection_ref (connection);
325 dbus_connection_disconnect (connection);
326 bus_connection_disconnected (connection);
327 dbus_connection_unref (connection);
330 _dbus_list_clear (&connections->list);
332 dbus_free (connections);
334 connection_data_slot_unref ();
339 bus_connections_setup_connection (BusConnections *connections,
340 DBusConnection *connection)
342 BusConnectionData *d;
345 d = dbus_new0 (BusConnectionData, 1);
350 d->connections = connections;
351 d->connection = connection;
353 _dbus_assert (connection_data_slot >= 0);
355 if (!dbus_connection_set_data (connection,
356 connection_data_slot,
357 d, free_connection_data))
368 if (!dbus_connection_set_watch_functions (connection,
369 (DBusAddWatchFunction) add_connection_watch,
370 (DBusRemoveWatchFunction) remove_connection_watch,
376 if (!dbus_connection_set_timeout_functions (connection,
377 (DBusAddTimeoutFunction) add_connection_timeout,
378 (DBusRemoveTimeoutFunction) remove_connection_timeout,
384 dbus_connection_set_unix_user_function (connection,
388 /* Setup the connection with the dispatcher */
389 if (!bus_dispatch_add_connection (connection))
392 if (!_dbus_list_append (&connections->list, connection))
394 bus_dispatch_remove_connection (connection);
398 dbus_connection_ref (connection);
404 if (!dbus_connection_set_data (connection,
405 connection_data_slot,
407 _dbus_assert_not_reached ("failed to set connection data to null");
409 if (!dbus_connection_set_watch_functions (connection,
413 _dbus_assert_not_reached ("setting watch functions to NULL failed");
415 if (!dbus_connection_set_timeout_functions (connection,
419 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
421 dbus_connection_set_unix_user_function (connection,
429 bus_connection_get_groups (DBusConnection *connection,
430 const unsigned long **groups,
433 BusConnectionData *d;
435 d = BUS_CONNECTION_DATA (connection);
437 _dbus_assert (d != NULL);
442 /* we do a lazy lookup on groups a user is in for two reasons:
443 * 1) we can't do it on connection setup since the user
444 * hasn't authenticated and 2) it might be expensive
445 * and we don't need to do it if there are no group-based
446 * rules in the config file
449 if (d->n_group_ids == 0)
453 if (dbus_connection_get_unix_user (connection, &uid))
455 if (!_dbus_get_groups (uid, &d->group_ids, &d->n_group_ids))
457 _dbus_verbose ("Did not get any groups for UID %lu\n",
464 *groups = d->group_ids;
465 *n_groups = d->n_group_ids;
471 bus_connection_is_in_group (DBusConnection *connection,
475 const unsigned long *group_ids;
478 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
482 while (i < n_group_ids)
484 if (group_ids[i] == gid)
493 bus_connection_get_policy (DBusConnection *connection)
495 BusConnectionData *d;
497 d = BUS_CONNECTION_DATA (connection);
499 _dbus_assert (d != NULL);
501 if (!dbus_connection_get_is_authenticated (connection))
503 _dbus_verbose ("Tried to get policy for unauthenticated connection!\n");
507 /* We do lazy creation of the policy because
508 * it can only be done post-authentication.
510 if (d->policy == NULL)
513 bus_context_create_connection_policy (d->connections->context,
516 /* we may have a NULL policy on OOM or error getting list of
517 * groups for a user. In the latter case we don't handle it so
518 * well currently, just keep pretending we're out of memory,
519 * which is kind of bizarre.
527 * Calls function on each connection; if the function returns
528 * #FALSE, stops iterating.
530 * @param connections the connections object
531 * @param function the function
532 * @param data data to pass to it as a second arg
535 bus_connections_foreach (BusConnections *connections,
536 BusConnectionForeachFunction function,
541 link = _dbus_list_get_first_link (&connections->list);
544 DBusConnection *connection = link->data;
545 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
547 if (!(* function) (connection, data))
555 bus_connections_get_context (BusConnections *connections)
557 return connections->context;
561 bus_connection_get_context (DBusConnection *connection)
563 BusConnectionData *d;
565 d = BUS_CONNECTION_DATA (connection);
567 _dbus_assert (d != NULL);
569 return d->connections->context;
573 bus_connection_get_connections (DBusConnection *connection)
575 BusConnectionData *d;
577 d = BUS_CONNECTION_DATA (connection);
579 _dbus_assert (d != NULL);
581 return d->connections;
585 bus_connection_get_registry (DBusConnection *connection)
587 BusConnectionData *d;
589 d = BUS_CONNECTION_DATA (connection);
591 _dbus_assert (d != NULL);
593 return bus_context_get_registry (d->connections->context);
597 bus_connection_get_activation (DBusConnection *connection)
599 BusConnectionData *d;
601 d = BUS_CONNECTION_DATA (connection);
603 _dbus_assert (d != NULL);
605 return bus_context_get_activation (d->connections->context);
609 * Checks whether the connection is registered with the message bus.
611 * @param connection the connection
612 * @returns #TRUE if we're an active message bus participant
615 bus_connection_is_active (DBusConnection *connection)
617 BusConnectionData *d;
619 d = BUS_CONNECTION_DATA (connection);
621 return d != NULL && d->name != NULL;
625 bus_connection_preallocate_oom_error (DBusConnection *connection)
627 DBusMessage *message;
628 DBusPreallocatedSend *preallocated;
629 BusConnectionData *d;
631 d = BUS_CONNECTION_DATA (connection);
633 _dbus_assert (d != NULL);
635 if (d->oom_preallocated != NULL)
638 preallocated = dbus_connection_preallocate_send (connection);
639 if (preallocated == NULL)
642 /* d->name may be NULL, but that should be OK */
643 message = dbus_message_new (d->name,
644 DBUS_ERROR_NO_MEMORY);
647 dbus_connection_free_preallocated_send (connection, preallocated);
651 dbus_message_set_is_error (message, TRUE);
653 if (!dbus_message_set_sender (message,
656 dbus_connection_free_preallocated_send (connection, preallocated);
657 dbus_message_unref (message);
661 /* set reply serial to placeholder value just so space is already allocated
664 if (!dbus_message_set_reply_serial (message, 14))
666 dbus_connection_free_preallocated_send (connection, preallocated);
667 dbus_message_unref (message);
671 d->oom_message = message;
672 d->oom_preallocated = preallocated;
678 bus_connection_send_oom_error (DBusConnection *connection,
679 DBusMessage *in_reply_to)
681 BusConnectionData *d;
683 d = BUS_CONNECTION_DATA (connection);
685 _dbus_assert (d != NULL);
686 _dbus_assert (d->oom_message != NULL);
688 /* should always succeed since we set it to a placeholder earlier */
689 if (!dbus_message_set_reply_serial (d->oom_message,
690 dbus_message_get_serial (in_reply_to)))
691 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
693 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
695 dbus_connection_send_preallocated (connection, d->oom_preallocated,
696 d->oom_message, NULL);
698 dbus_message_unref (d->oom_message);
699 d->oom_message = NULL;
700 d->oom_preallocated = NULL;
704 bus_connection_add_owned_service (DBusConnection *connection,
707 BusConnectionData *d;
709 d = BUS_CONNECTION_DATA (connection);
710 _dbus_assert (d != NULL);
712 if (!_dbus_list_append (&d->services_owned,
720 bus_connection_remove_owned_service (DBusConnection *connection,
723 BusConnectionData *d;
725 d = BUS_CONNECTION_DATA (connection);
726 _dbus_assert (d != NULL);
728 _dbus_list_remove_last (&d->services_owned, service);
732 bus_connection_set_name (DBusConnection *connection,
733 const DBusString *name)
735 BusConnectionData *d;
737 d = BUS_CONNECTION_DATA (connection);
738 _dbus_assert (d != NULL);
739 _dbus_assert (d->name == NULL);
741 if (!_dbus_string_copy_data (name, &d->name))
744 _dbus_assert (d->name != NULL);
746 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
752 bus_connection_get_name (DBusConnection *connection)
754 BusConnectionData *d;
756 d = BUS_CONNECTION_DATA (connection);
757 _dbus_assert (d != NULL);
764 BusTransaction *transaction;
765 DBusMessage *message;
766 DBusPreallocatedSend *preallocated;
769 struct BusTransaction
771 DBusList *connections;
776 message_to_send_free (DBusConnection *connection,
777 MessageToSend *to_send)
779 if (to_send->message)
780 dbus_message_unref (to_send->message);
782 if (to_send->preallocated)
783 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
789 bus_transaction_new (BusContext *context)
791 BusTransaction *transaction;
793 transaction = dbus_new0 (BusTransaction, 1);
794 if (transaction == NULL)
797 transaction->context = context;
803 bus_transaction_get_context (BusTransaction *transaction)
805 return transaction->context;
809 bus_transaction_get_connections (BusTransaction *transaction)
811 return bus_context_get_connections (transaction->context);
815 bus_transaction_send_message (BusTransaction *transaction,
816 DBusConnection *connection,
817 DBusMessage *message)
819 MessageToSend *to_send;
820 BusConnectionData *d;
823 _dbus_verbose (" trying to add message %s to transaction%s\n",
824 dbus_message_get_name (message),
825 dbus_connection_get_is_connected (connection) ?
826 "" : " (disconnected)");
828 _dbus_assert (dbus_message_get_sender (message) != NULL);
830 if (!dbus_connection_get_is_connected (connection))
831 return TRUE; /* silently ignore disconnected connections */
833 d = BUS_CONNECTION_DATA (connection);
834 _dbus_assert (d != NULL);
836 to_send = dbus_new (MessageToSend, 1);
842 to_send->preallocated = dbus_connection_preallocate_send (connection);
843 if (to_send->preallocated == NULL)
849 dbus_message_ref (message);
850 to_send->message = message;
851 to_send->transaction = transaction;
853 _dbus_verbose ("about to prepend message\n");
855 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
857 message_to_send_free (connection, to_send);
861 _dbus_verbose ("prepended message\n");
863 /* See if we already had this connection in the list
864 * for this transaction. If we have a pending message,
865 * then we should already be in transaction->connections
867 link = _dbus_list_get_first_link (&d->transaction_messages);
868 _dbus_assert (link->data == to_send);
869 link = _dbus_list_get_next_link (&d->transaction_messages, link);
872 MessageToSend *m = link->data;
873 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
875 if (m->transaction == transaction)
883 if (!_dbus_list_prepend (&transaction->connections, connection))
885 _dbus_list_remove (&d->transaction_messages, to_send);
886 message_to_send_free (connection, to_send);
895 connection_cancel_transaction (DBusConnection *connection,
896 BusTransaction *transaction)
899 BusConnectionData *d;
901 d = BUS_CONNECTION_DATA (connection);
902 _dbus_assert (d != NULL);
904 link = _dbus_list_get_first_link (&d->transaction_messages);
907 MessageToSend *m = link->data;
908 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
910 if (m->transaction == transaction)
912 _dbus_list_remove_link (&d->transaction_messages,
915 message_to_send_free (connection, m);
923 bus_transaction_cancel_and_free (BusTransaction *transaction)
925 DBusConnection *connection;
927 _dbus_verbose ("TRANSACTION: cancelled\n");
929 while ((connection = _dbus_list_pop_first (&transaction->connections)))
930 connection_cancel_transaction (connection, transaction);
932 _dbus_assert (transaction->connections == NULL);
934 dbus_free (transaction);
938 connection_execute_transaction (DBusConnection *connection,
939 BusTransaction *transaction)
942 BusConnectionData *d;
944 d = BUS_CONNECTION_DATA (connection);
945 _dbus_assert (d != NULL);
947 /* Send the queue in order (FIFO) */
948 link = _dbus_list_get_last_link (&d->transaction_messages);
951 MessageToSend *m = link->data;
952 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
954 if (m->transaction == transaction)
956 _dbus_list_remove_link (&d->transaction_messages,
959 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
961 dbus_connection_send_preallocated (connection,
966 m->preallocated = NULL; /* so we don't double-free it */
968 message_to_send_free (connection, m);
976 bus_transaction_execute_and_free (BusTransaction *transaction)
978 /* For each connection in transaction->connections
981 DBusConnection *connection;
983 _dbus_verbose ("TRANSACTION: executing\n");
985 while ((connection = _dbus_list_pop_first (&transaction->connections)))
986 connection_execute_transaction (connection, transaction);
988 _dbus_assert (transaction->connections == NULL);
990 dbus_free (transaction);
994 bus_connection_remove_transactions (DBusConnection *connection)
996 MessageToSend *to_send;
997 BusConnectionData *d;
999 d = BUS_CONNECTION_DATA (connection);
1000 _dbus_assert (d != NULL);
1002 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
1004 /* only has an effect for the first MessageToSend listing this transaction */
1005 _dbus_list_remove (&to_send->transaction->connections,
1008 _dbus_list_remove (&d->transaction_messages, to_send);
1009 message_to_send_free (connection, to_send);
1014 * Converts the DBusError to a message reply
1017 bus_transaction_send_error_reply (BusTransaction *transaction,
1018 DBusConnection *connection,
1019 const DBusError *error,
1020 DBusMessage *in_reply_to)
1024 _dbus_assert (error != NULL);
1025 _DBUS_ASSERT_ERROR_IS_SET (error);
1027 reply = dbus_message_new_error_reply (in_reply_to,
1033 if (!bus_transaction_send_message (transaction, connection, reply))
1035 dbus_message_unref (reply);