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 connection_get_loop (DBusConnection *connection)
97 d = BUS_CONNECTION_DATA (connection);
99 return bus_context_get_loop (d->connections->context);
103 bus_connection_disconnected (DBusConnection *connection)
105 BusConnectionData *d;
108 d = BUS_CONNECTION_DATA (connection);
109 _dbus_assert (d != NULL);
111 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
112 d->name ? d->name : "(inactive)");
114 /* Drop any service ownership. FIXME Unfortunately, this requires
115 * memory allocation and there doesn't seem to be a good way to
116 * handle it other than sleeping; we can't "fail" the operation of
117 * disconnecting a client, and preallocating a broadcast "service is
118 * now gone" message for every client-service pair seems kind of
119 * involved. Probably we need to do that though, and also
120 * extend BusTransaction to be able to revert generic
121 * stuff, not just sending a message (so we can e.g. revert
122 * removal of service owners).
124 while ((service = _dbus_list_get_last (&d->services_owned)))
126 BusTransaction *transaction;
131 dbus_error_init (&error);
134 while (transaction == NULL)
136 transaction = bus_transaction_new (d->connections->context);
137 bus_wait_for_memory ();
140 if (!bus_service_remove_owner (service, connection,
141 transaction, &error))
143 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
145 dbus_error_free (&error);
146 bus_transaction_cancel_and_free (transaction);
147 bus_wait_for_memory ();
151 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
154 bus_transaction_execute_and_free (transaction);
157 bus_dispatch_remove_connection (connection);
159 /* no more watching */
160 if (!dbus_connection_set_watch_functions (connection,
164 _dbus_assert_not_reached ("setting watch functions to NULL failed");
166 if (!dbus_connection_set_timeout_functions (connection,
170 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
172 dbus_connection_set_unix_user_function (connection,
175 bus_connection_remove_transactions (connection);
177 _dbus_list_remove (&d->connections->list, connection);
179 /* frees "d" as side effect */
180 dbus_connection_set_data (connection,
181 connection_data_slot,
184 dbus_connection_unref (connection);
188 connection_watch_callback (DBusWatch *watch,
189 unsigned int condition,
192 DBusConnection *connection = data;
195 dbus_connection_ref (connection);
197 retval = dbus_connection_handle_watch (connection, watch, condition);
199 bus_connection_dispatch_all_messages (connection);
201 dbus_connection_unref (connection);
207 add_connection_watch (DBusWatch *watch,
210 DBusConnection *connection = data;
212 return bus_loop_add_watch (connection_get_loop (connection),
213 watch, connection_watch_callback, connection,
218 remove_connection_watch (DBusWatch *watch,
221 DBusConnection *connection = data;
223 bus_loop_remove_watch (connection_get_loop (connection),
224 watch, connection_watch_callback, connection);
228 connection_timeout_callback (DBusTimeout *timeout,
231 DBusConnection *connection = data;
233 dbus_connection_ref (connection);
235 /* can return FALSE on OOM but we just let it fire again later */
236 dbus_timeout_handle (timeout);
238 bus_connection_dispatch_all_messages (connection);
240 dbus_connection_unref (connection);
244 add_connection_timeout (DBusTimeout *timeout,
247 DBusConnection *connection = data;
249 return bus_loop_add_timeout (connection_get_loop (connection),
250 timeout, connection_timeout_callback, connection, NULL);
254 remove_connection_timeout (DBusTimeout *timeout,
257 DBusConnection *connection = data;
259 bus_loop_remove_timeout (connection_get_loop (connection),
260 timeout, connection_timeout_callback, connection);
264 allow_user_function (DBusConnection *connection,
268 BusConnectionData *d;
270 d = BUS_CONNECTION_DATA (connection);
272 _dbus_assert (d != NULL);
274 return TRUE; /* FIXME - this is just until we can parse a config file */
276 return bus_context_allow_user (d->connections->context, uid);
280 free_connection_data (void *data)
282 BusConnectionData *d = data;
284 /* services_owned should be NULL since we should be disconnected */
285 _dbus_assert (d->services_owned == NULL);
287 _dbus_assert (d->transaction_messages == NULL);
289 if (d->oom_preallocated)
290 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
293 dbus_message_unref (d->oom_message);
296 bus_policy_unref (d->policy);
298 dbus_free (d->group_ids);
306 bus_connections_new (BusContext *context)
308 BusConnections *connections;
310 if (!connection_data_slot_ref ())
313 connections = dbus_new0 (BusConnections, 1);
314 if (connections == NULL)
316 connection_data_slot_unref ();
320 connections->refcount = 1;
321 connections->context = context;
327 bus_connections_ref (BusConnections *connections)
329 _dbus_assert (connections->refcount > 0);
330 connections->refcount += 1;
334 bus_connections_unref (BusConnections *connections)
336 _dbus_assert (connections->refcount > 0);
337 connections->refcount -= 1;
338 if (connections->refcount == 0)
340 while (connections->list != NULL)
342 DBusConnection *connection;
344 connection = connections->list->data;
346 dbus_connection_ref (connection);
347 dbus_connection_disconnect (connection);
348 bus_connection_disconnected (connection);
349 dbus_connection_unref (connection);
352 _dbus_list_clear (&connections->list);
354 dbus_free (connections);
356 connection_data_slot_unref ();
361 bus_connections_setup_connection (BusConnections *connections,
362 DBusConnection *connection)
364 BusConnectionData *d;
367 d = dbus_new0 (BusConnectionData, 1);
372 d->connections = connections;
373 d->connection = connection;
375 _dbus_assert (connection_data_slot >= 0);
377 if (!dbus_connection_set_data (connection,
378 connection_data_slot,
379 d, free_connection_data))
390 if (!dbus_connection_set_watch_functions (connection,
391 add_connection_watch,
392 remove_connection_watch,
398 if (!dbus_connection_set_timeout_functions (connection,
399 add_connection_timeout,
400 remove_connection_timeout,
406 dbus_connection_set_unix_user_function (connection,
410 /* Setup the connection with the dispatcher */
411 if (!bus_dispatch_add_connection (connection))
414 if (!_dbus_list_append (&connections->list, connection))
416 bus_dispatch_remove_connection (connection);
420 dbus_connection_ref (connection);
426 if (!dbus_connection_set_watch_functions (connection,
430 _dbus_assert_not_reached ("setting watch functions to NULL failed");
432 if (!dbus_connection_set_timeout_functions (connection,
436 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
438 dbus_connection_set_unix_user_function (connection,
441 if (!dbus_connection_set_data (connection,
442 connection_data_slot,
444 _dbus_assert_not_reached ("failed to set connection data to null");
451 bus_connection_get_groups (DBusConnection *connection,
452 const unsigned long **groups,
455 BusConnectionData *d;
457 d = BUS_CONNECTION_DATA (connection);
459 _dbus_assert (d != NULL);
464 /* we do a lazy lookup on groups a user is in for two reasons:
465 * 1) we can't do it on connection setup since the user
466 * hasn't authenticated and 2) it might be expensive
467 * and we don't need to do it if there are no group-based
468 * rules in the config file
471 if (d->n_group_ids == 0)
475 if (dbus_connection_get_unix_user (connection, &uid))
477 if (!_dbus_get_groups (uid, &d->group_ids, &d->n_group_ids))
479 _dbus_verbose ("Did not get any groups for UID %lu\n",
486 *groups = d->group_ids;
487 *n_groups = d->n_group_ids;
493 bus_connection_is_in_group (DBusConnection *connection,
497 const unsigned long *group_ids;
500 if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
504 while (i < n_group_ids)
506 if (group_ids[i] == gid)
515 bus_connection_get_policy (DBusConnection *connection)
517 BusConnectionData *d;
519 d = BUS_CONNECTION_DATA (connection);
521 _dbus_assert (d != NULL);
523 if (!dbus_connection_get_is_authenticated (connection))
525 _dbus_verbose ("Tried to get policy for unauthenticated connection!\n");
529 /* We do lazy creation of the policy because
530 * it can only be done post-authentication.
532 if (d->policy == NULL)
535 bus_context_create_connection_policy (d->connections->context,
538 /* we may have a NULL policy on OOM or error getting list of
539 * groups for a user. In the latter case we don't handle it so
540 * well currently, just keep pretending we're out of memory,
541 * which is kind of bizarre.
549 * Calls function on each connection; if the function returns
550 * #FALSE, stops iterating.
552 * @param connections the connections object
553 * @param function the function
554 * @param data data to pass to it as a second arg
557 bus_connections_foreach (BusConnections *connections,
558 BusConnectionForeachFunction function,
563 link = _dbus_list_get_first_link (&connections->list);
566 DBusConnection *connection = link->data;
567 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
569 if (!(* function) (connection, data))
577 bus_connections_get_context (BusConnections *connections)
579 return connections->context;
583 bus_connection_get_context (DBusConnection *connection)
585 BusConnectionData *d;
587 d = BUS_CONNECTION_DATA (connection);
589 _dbus_assert (d != NULL);
591 return d->connections->context;
595 bus_connection_get_connections (DBusConnection *connection)
597 BusConnectionData *d;
599 d = BUS_CONNECTION_DATA (connection);
601 _dbus_assert (d != NULL);
603 return d->connections;
607 bus_connection_get_registry (DBusConnection *connection)
609 BusConnectionData *d;
611 d = BUS_CONNECTION_DATA (connection);
613 _dbus_assert (d != NULL);
615 return bus_context_get_registry (d->connections->context);
619 bus_connection_get_activation (DBusConnection *connection)
621 BusConnectionData *d;
623 d = BUS_CONNECTION_DATA (connection);
625 _dbus_assert (d != NULL);
627 return bus_context_get_activation (d->connections->context);
631 * Checks whether the connection is registered with the message bus.
633 * @param connection the connection
634 * @returns #TRUE if we're an active message bus participant
637 bus_connection_is_active (DBusConnection *connection)
639 BusConnectionData *d;
641 d = BUS_CONNECTION_DATA (connection);
643 return d != NULL && d->name != NULL;
647 bus_connection_preallocate_oom_error (DBusConnection *connection)
649 DBusMessage *message;
650 DBusPreallocatedSend *preallocated;
651 BusConnectionData *d;
653 d = BUS_CONNECTION_DATA (connection);
655 _dbus_assert (d != NULL);
657 if (d->oom_preallocated != NULL)
660 preallocated = dbus_connection_preallocate_send (connection);
661 if (preallocated == NULL)
664 /* d->name may be NULL, but that should be OK */
665 message = dbus_message_new (d->name,
666 DBUS_ERROR_NO_MEMORY);
669 dbus_connection_free_preallocated_send (connection, preallocated);
673 dbus_message_set_is_error (message, TRUE);
675 if (!dbus_message_set_sender (message,
678 dbus_connection_free_preallocated_send (connection, preallocated);
679 dbus_message_unref (message);
683 /* set reply serial to placeholder value just so space is already allocated
686 if (!dbus_message_set_reply_serial (message, 14))
688 dbus_connection_free_preallocated_send (connection, preallocated);
689 dbus_message_unref (message);
693 d->oom_message = message;
694 d->oom_preallocated = preallocated;
700 bus_connection_send_oom_error (DBusConnection *connection,
701 DBusMessage *in_reply_to)
703 BusConnectionData *d;
705 d = BUS_CONNECTION_DATA (connection);
707 _dbus_assert (d != NULL);
708 _dbus_assert (d->oom_message != NULL);
710 /* should always succeed since we set it to a placeholder earlier */
711 if (!dbus_message_set_reply_serial (d->oom_message,
712 dbus_message_get_serial (in_reply_to)))
713 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
715 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
717 dbus_connection_send_preallocated (connection, d->oom_preallocated,
718 d->oom_message, NULL);
720 dbus_message_unref (d->oom_message);
721 d->oom_message = NULL;
722 d->oom_preallocated = NULL;
726 bus_connection_add_owned_service (DBusConnection *connection,
729 BusConnectionData *d;
731 d = BUS_CONNECTION_DATA (connection);
732 _dbus_assert (d != NULL);
734 if (!_dbus_list_append (&d->services_owned,
742 bus_connection_remove_owned_service (DBusConnection *connection,
745 BusConnectionData *d;
747 d = BUS_CONNECTION_DATA (connection);
748 _dbus_assert (d != NULL);
750 _dbus_list_remove_last (&d->services_owned, service);
754 bus_connection_set_name (DBusConnection *connection,
755 const DBusString *name)
757 BusConnectionData *d;
759 d = BUS_CONNECTION_DATA (connection);
760 _dbus_assert (d != NULL);
761 _dbus_assert (d->name == NULL);
763 if (!_dbus_string_copy_data (name, &d->name))
766 _dbus_assert (d->name != NULL);
768 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
774 bus_connection_get_name (DBusConnection *connection)
776 BusConnectionData *d;
778 d = BUS_CONNECTION_DATA (connection);
779 _dbus_assert (d != NULL);
786 BusTransaction *transaction;
787 DBusMessage *message;
788 DBusPreallocatedSend *preallocated;
791 struct BusTransaction
793 DBusList *connections;
798 message_to_send_free (DBusConnection *connection,
799 MessageToSend *to_send)
801 if (to_send->message)
802 dbus_message_unref (to_send->message);
804 if (to_send->preallocated)
805 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
811 bus_transaction_new (BusContext *context)
813 BusTransaction *transaction;
815 transaction = dbus_new0 (BusTransaction, 1);
816 if (transaction == NULL)
819 transaction->context = context;
825 bus_transaction_get_context (BusTransaction *transaction)
827 return transaction->context;
831 bus_transaction_get_connections (BusTransaction *transaction)
833 return bus_context_get_connections (transaction->context);
837 bus_transaction_send_message (BusTransaction *transaction,
838 DBusConnection *connection,
839 DBusMessage *message)
841 MessageToSend *to_send;
842 BusConnectionData *d;
845 _dbus_verbose (" trying to add message %s to transaction%s\n",
846 dbus_message_get_name (message),
847 dbus_connection_get_is_connected (connection) ?
848 "" : " (disconnected)");
850 _dbus_assert (dbus_message_get_sender (message) != NULL);
852 if (!dbus_connection_get_is_connected (connection))
853 return TRUE; /* silently ignore disconnected connections */
855 d = BUS_CONNECTION_DATA (connection);
856 _dbus_assert (d != NULL);
858 to_send = dbus_new (MessageToSend, 1);
864 to_send->preallocated = dbus_connection_preallocate_send (connection);
865 if (to_send->preallocated == NULL)
871 dbus_message_ref (message);
872 to_send->message = message;
873 to_send->transaction = transaction;
875 _dbus_verbose ("about to prepend message\n");
877 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
879 message_to_send_free (connection, to_send);
883 _dbus_verbose ("prepended message\n");
885 /* See if we already had this connection in the list
886 * for this transaction. If we have a pending message,
887 * then we should already be in transaction->connections
889 link = _dbus_list_get_first_link (&d->transaction_messages);
890 _dbus_assert (link->data == to_send);
891 link = _dbus_list_get_next_link (&d->transaction_messages, link);
894 MessageToSend *m = link->data;
895 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
897 if (m->transaction == transaction)
905 if (!_dbus_list_prepend (&transaction->connections, connection))
907 _dbus_list_remove (&d->transaction_messages, to_send);
908 message_to_send_free (connection, to_send);
917 connection_cancel_transaction (DBusConnection *connection,
918 BusTransaction *transaction)
921 BusConnectionData *d;
923 d = BUS_CONNECTION_DATA (connection);
924 _dbus_assert (d != NULL);
926 link = _dbus_list_get_first_link (&d->transaction_messages);
929 MessageToSend *m = link->data;
930 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
932 if (m->transaction == transaction)
934 _dbus_list_remove_link (&d->transaction_messages,
937 message_to_send_free (connection, m);
945 bus_transaction_cancel_and_free (BusTransaction *transaction)
947 DBusConnection *connection;
949 _dbus_verbose ("TRANSACTION: cancelled\n");
951 while ((connection = _dbus_list_pop_first (&transaction->connections)))
952 connection_cancel_transaction (connection, transaction);
954 _dbus_assert (transaction->connections == NULL);
956 dbus_free (transaction);
960 connection_execute_transaction (DBusConnection *connection,
961 BusTransaction *transaction)
964 BusConnectionData *d;
966 d = BUS_CONNECTION_DATA (connection);
967 _dbus_assert (d != NULL);
969 /* Send the queue in order (FIFO) */
970 link = _dbus_list_get_last_link (&d->transaction_messages);
973 MessageToSend *m = link->data;
974 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
976 if (m->transaction == transaction)
978 _dbus_list_remove_link (&d->transaction_messages,
981 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
983 dbus_connection_send_preallocated (connection,
988 m->preallocated = NULL; /* so we don't double-free it */
990 message_to_send_free (connection, m);
998 bus_transaction_execute_and_free (BusTransaction *transaction)
1000 /* For each connection in transaction->connections
1003 DBusConnection *connection;
1005 _dbus_verbose ("TRANSACTION: executing\n");
1007 while ((connection = _dbus_list_pop_first (&transaction->connections)))
1008 connection_execute_transaction (connection, transaction);
1010 _dbus_assert (transaction->connections == NULL);
1012 dbus_free (transaction);
1016 bus_connection_remove_transactions (DBusConnection *connection)
1018 MessageToSend *to_send;
1019 BusConnectionData *d;
1021 d = BUS_CONNECTION_DATA (connection);
1022 _dbus_assert (d != NULL);
1024 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
1026 /* only has an effect for the first MessageToSend listing this transaction */
1027 _dbus_list_remove (&to_send->transaction->connections,
1030 _dbus_list_remove (&d->transaction_messages, to_send);
1031 message_to_send_free (connection, to_send);
1036 * Converts the DBusError to a message reply
1039 bus_transaction_send_error_reply (BusTransaction *transaction,
1040 DBusConnection *connection,
1041 const DBusError *error,
1042 DBusMessage *in_reply_to)
1046 _dbus_assert (error != NULL);
1047 _DBUS_ASSERT_ERROR_IS_SET (error);
1049 reply = dbus_message_new_error_reply (in_reply_to,
1055 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS) ||
1056 !bus_transaction_send_message (transaction, connection, reply))
1058 dbus_message_unref (reply);
1062 dbus_message_unref (reply);