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;
53 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
56 connection_data_slot_ref (void)
58 if (connection_data_slot < 0)
60 connection_data_slot = dbus_connection_allocate_data_slot ();
62 if (connection_data_slot < 0)
65 _dbus_assert (connection_data_slot_refcount == 0);
68 connection_data_slot_refcount += 1;
75 connection_data_slot_unref (void)
77 _dbus_assert (connection_data_slot_refcount > 0);
79 connection_data_slot_refcount -= 1;
81 if (connection_data_slot_refcount == 0)
83 dbus_connection_free_data_slot (connection_data_slot);
84 connection_data_slot = -1;
89 bus_connection_disconnected (DBusConnection *connection)
94 d = BUS_CONNECTION_DATA (connection);
95 _dbus_assert (d != NULL);
97 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
98 d->name ? d->name : "(inactive)");
100 /* Drop any service ownership. FIXME Unfortunately, this requires
101 * memory allocation and there doesn't seem to be a good way to
102 * handle it other than sleeping; we can't "fail" the operation of
103 * disconnecting a client, and preallocating a broadcast "service is
104 * now gone" message for every client-service pair seems kind of
105 * involved. Probably we need to do that though, and also
106 * extend BusTransaction to be able to revert generic
107 * stuff, not just sending a message (so we can e.g. revert
108 * removal of service owners).
110 while ((service = _dbus_list_get_last (&d->services_owned)))
112 BusTransaction *transaction;
117 dbus_error_init (&error);
120 while (transaction == NULL)
122 transaction = bus_transaction_new (d->connections->context);
123 bus_wait_for_memory ();
126 if (!bus_service_remove_owner (service, connection,
127 transaction, &error))
129 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
131 dbus_error_free (&error);
132 bus_transaction_cancel_and_free (transaction);
133 bus_wait_for_memory ();
137 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
140 bus_transaction_execute_and_free (transaction);
143 bus_dispatch_remove_connection (connection);
145 /* no more watching */
146 if (!dbus_connection_set_watch_functions (connection,
150 _dbus_assert_not_reached ("setting watch functions to NULL failed");
152 if (!dbus_connection_set_timeout_functions (connection,
156 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
158 bus_connection_remove_transactions (connection);
160 _dbus_list_remove (&d->connections->list, connection);
162 /* frees "d" as side effect */
163 dbus_connection_set_data (connection,
164 connection_data_slot,
167 dbus_connection_unref (connection);
171 connection_watch_callback (DBusWatch *watch,
172 unsigned int condition,
175 DBusConnection *connection = data;
178 dbus_connection_ref (connection);
180 retval = dbus_connection_handle_watch (connection, watch, condition);
182 bus_connection_dispatch_all_messages (connection);
184 dbus_connection_unref (connection);
190 add_connection_watch (DBusWatch *watch,
191 DBusConnection *connection)
193 return bus_loop_add_watch (watch, connection_watch_callback, connection,
198 remove_connection_watch (DBusWatch *watch,
199 DBusConnection *connection)
201 bus_loop_remove_watch (watch, connection_watch_callback, connection);
205 connection_timeout_callback (DBusTimeout *timeout,
208 DBusConnection *connection = data;
210 dbus_connection_ref (connection);
212 /* can return FALSE on OOM but we just let it fire again later */
213 dbus_timeout_handle (timeout);
215 bus_connection_dispatch_all_messages (connection);
217 dbus_connection_unref (connection);
221 add_connection_timeout (DBusTimeout *timeout,
222 DBusConnection *connection)
224 return bus_loop_add_timeout (timeout, connection_timeout_callback, connection, NULL);
228 remove_connection_timeout (DBusTimeout *timeout,
229 DBusConnection *connection)
231 bus_loop_remove_timeout (timeout, connection_timeout_callback, connection);
235 free_connection_data (void *data)
237 BusConnectionData *d = data;
239 /* services_owned should be NULL since we should be disconnected */
240 _dbus_assert (d->services_owned == NULL);
242 _dbus_assert (d->transaction_messages == NULL);
244 if (d->oom_preallocated)
245 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
248 dbus_message_unref (d->oom_message);
256 bus_connections_new (BusContext *context)
258 BusConnections *connections;
260 if (!connection_data_slot_ref ())
263 connections = dbus_new0 (BusConnections, 1);
264 if (connections == NULL)
266 connection_data_slot_unref ();
270 connections->refcount = 1;
271 connections->context = context;
277 bus_connections_ref (BusConnections *connections)
279 _dbus_assert (connections->refcount > 0);
280 connections->refcount += 1;
284 bus_connections_unref (BusConnections *connections)
286 _dbus_assert (connections->refcount > 0);
287 connections->refcount -= 1;
288 if (connections->refcount == 0)
290 while (connections->list != NULL)
292 DBusConnection *connection;
294 connection = connections->list->data;
296 dbus_connection_ref (connection);
297 dbus_connection_disconnect (connection);
298 bus_connection_disconnected (connection);
299 dbus_connection_unref (connection);
302 _dbus_list_clear (&connections->list);
304 dbus_free (connections);
306 connection_data_slot_unref ();
311 bus_connections_setup_connection (BusConnections *connections,
312 DBusConnection *connection)
314 BusConnectionData *d;
317 d = dbus_new0 (BusConnectionData, 1);
322 d->connections = connections;
323 d->connection = connection;
325 _dbus_assert (connection_data_slot >= 0);
327 if (!dbus_connection_set_data (connection,
328 connection_data_slot,
329 d, free_connection_data))
337 if (!dbus_connection_set_watch_functions (connection,
338 (DBusAddWatchFunction) add_connection_watch,
339 (DBusRemoveWatchFunction) remove_connection_watch,
345 if (!dbus_connection_set_timeout_functions (connection,
346 (DBusAddTimeoutFunction) add_connection_timeout,
347 (DBusRemoveTimeoutFunction) remove_connection_timeout,
353 /* Setup the connection with the dispatcher */
354 if (!bus_dispatch_add_connection (connection))
357 if (!_dbus_list_append (&connections->list, connection))
359 bus_dispatch_remove_connection (connection);
363 dbus_connection_ref (connection);
369 if (!dbus_connection_set_data (connection,
370 connection_data_slot,
372 _dbus_assert_not_reached ("failed to set connection data to null");
374 if (!dbus_connection_set_watch_functions (connection,
378 _dbus_assert_not_reached ("setting watch functions to NULL failed");
380 if (!dbus_connection_set_timeout_functions (connection,
384 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
392 * Calls function on each connection; if the function returns
393 * #FALSE, stops iterating.
395 * @param connections the connections object
396 * @param function the function
397 * @param data data to pass to it as a second arg
400 bus_connections_foreach (BusConnections *connections,
401 BusConnectionForeachFunction function,
406 link = _dbus_list_get_first_link (&connections->list);
409 DBusConnection *connection = link->data;
410 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
412 if (!(* function) (connection, data))
420 bus_connections_get_context (BusConnections *connections)
422 return connections->context;
426 bus_connection_get_context (DBusConnection *connection)
428 BusConnectionData *d;
430 d = BUS_CONNECTION_DATA (connection);
432 _dbus_assert (d != NULL);
434 return d->connections->context;
438 bus_connection_get_connections (DBusConnection *connection)
440 BusConnectionData *d;
442 d = BUS_CONNECTION_DATA (connection);
444 _dbus_assert (d != NULL);
446 return d->connections;
450 bus_connection_get_registry (DBusConnection *connection)
452 BusConnectionData *d;
454 d = BUS_CONNECTION_DATA (connection);
456 _dbus_assert (d != NULL);
458 return bus_context_get_registry (d->connections->context);
462 bus_connection_get_activation (DBusConnection *connection)
464 BusConnectionData *d;
466 d = BUS_CONNECTION_DATA (connection);
468 _dbus_assert (d != NULL);
470 return bus_context_get_activation (d->connections->context);
474 * Checks whether the connection is registered with the message bus.
476 * @param connection the connection
477 * @returns #TRUE if we're an active message bus participant
480 bus_connection_is_active (DBusConnection *connection)
482 BusConnectionData *d;
484 d = BUS_CONNECTION_DATA (connection);
486 return d != NULL && d->name != NULL;
490 bus_connection_preallocate_oom_error (DBusConnection *connection)
492 DBusMessage *message;
493 DBusPreallocatedSend *preallocated;
494 BusConnectionData *d;
496 d = BUS_CONNECTION_DATA (connection);
498 _dbus_assert (d != NULL);
500 if (d->oom_preallocated != NULL)
503 preallocated = dbus_connection_preallocate_send (connection);
504 if (preallocated == NULL)
507 /* d->name may be NULL, but that should be OK */
508 message = dbus_message_new (d->name,
509 DBUS_ERROR_NO_MEMORY);
512 dbus_connection_free_preallocated_send (connection, preallocated);
516 dbus_message_set_is_error (message, TRUE);
518 if (!dbus_message_set_sender (message,
521 dbus_connection_free_preallocated_send (connection, preallocated);
522 dbus_message_unref (message);
526 /* set reply serial to placeholder value just so space is already allocated
529 if (!dbus_message_set_reply_serial (message, 14))
531 dbus_connection_free_preallocated_send (connection, preallocated);
532 dbus_message_unref (message);
536 d->oom_message = message;
537 d->oom_preallocated = preallocated;
543 bus_connection_send_oom_error (DBusConnection *connection,
544 DBusMessage *in_reply_to)
546 BusConnectionData *d;
548 d = BUS_CONNECTION_DATA (connection);
550 _dbus_assert (d != NULL);
551 _dbus_assert (d->oom_message != NULL);
553 /* should always succeed since we set it to a placeholder earlier */
554 if (!dbus_message_set_reply_serial (d->oom_message,
555 dbus_message_get_serial (in_reply_to)))
556 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
558 _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
560 dbus_connection_send_preallocated (connection, d->oom_preallocated,
561 d->oom_message, NULL);
563 dbus_message_unref (d->oom_message);
564 d->oom_message = NULL;
565 d->oom_preallocated = NULL;
569 bus_connection_add_owned_service (DBusConnection *connection,
572 BusConnectionData *d;
574 d = BUS_CONNECTION_DATA (connection);
575 _dbus_assert (d != NULL);
577 if (!_dbus_list_append (&d->services_owned,
585 bus_connection_remove_owned_service (DBusConnection *connection,
588 BusConnectionData *d;
590 d = BUS_CONNECTION_DATA (connection);
591 _dbus_assert (d != NULL);
593 _dbus_list_remove_last (&d->services_owned, service);
597 bus_connection_set_name (DBusConnection *connection,
598 const DBusString *name)
601 BusConnectionData *d;
603 d = BUS_CONNECTION_DATA (connection);
604 _dbus_assert (d != NULL);
605 _dbus_assert (d->name == NULL);
607 _dbus_string_get_const_data (name, &c_name);
609 d->name = _dbus_strdup (c_name);
614 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
620 bus_connection_get_name (DBusConnection *connection)
622 BusConnectionData *d;
624 d = BUS_CONNECTION_DATA (connection);
625 _dbus_assert (d != NULL);
632 BusTransaction *transaction;
633 DBusMessage *message;
634 DBusPreallocatedSend *preallocated;
637 struct BusTransaction
639 DBusList *connections;
644 message_to_send_free (DBusConnection *connection,
645 MessageToSend *to_send)
647 if (to_send->message)
648 dbus_message_unref (to_send->message);
650 if (to_send->preallocated)
651 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
657 bus_transaction_new (BusContext *context)
659 BusTransaction *transaction;
661 transaction = dbus_new0 (BusTransaction, 1);
662 if (transaction == NULL)
665 transaction->context = context;
671 bus_transaction_get_context (BusTransaction *transaction)
673 return transaction->context;
677 bus_transaction_get_connections (BusTransaction *transaction)
679 return bus_context_get_connections (transaction->context);
683 bus_transaction_send_message (BusTransaction *transaction,
684 DBusConnection *connection,
685 DBusMessage *message)
687 MessageToSend *to_send;
688 BusConnectionData *d;
691 _dbus_verbose (" trying to add message %s to transaction%s\n",
692 dbus_message_get_name (message),
693 dbus_connection_get_is_connected (connection) ?
694 "" : " (disconnected)");
696 _dbus_assert (dbus_message_get_sender (message) != NULL);
698 if (!dbus_connection_get_is_connected (connection))
699 return TRUE; /* silently ignore disconnected connections */
701 d = BUS_CONNECTION_DATA (connection);
702 _dbus_assert (d != NULL);
704 to_send = dbus_new (MessageToSend, 1);
710 to_send->preallocated = dbus_connection_preallocate_send (connection);
711 if (to_send->preallocated == NULL)
717 dbus_message_ref (message);
718 to_send->message = message;
719 to_send->transaction = transaction;
721 _dbus_verbose ("about to prepend message\n");
723 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
725 message_to_send_free (connection, to_send);
729 _dbus_verbose ("prepended message\n");
731 /* See if we already had this connection in the list
732 * for this transaction. If we have a pending message,
733 * then we should already be in transaction->connections
735 link = _dbus_list_get_first_link (&d->transaction_messages);
736 _dbus_assert (link->data == to_send);
737 link = _dbus_list_get_next_link (&d->transaction_messages, link);
740 MessageToSend *m = link->data;
741 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
743 if (m->transaction == transaction)
751 if (!_dbus_list_prepend (&transaction->connections, connection))
753 _dbus_list_remove (&d->transaction_messages, to_send);
754 message_to_send_free (connection, to_send);
763 connection_cancel_transaction (DBusConnection *connection,
764 BusTransaction *transaction)
767 BusConnectionData *d;
769 d = BUS_CONNECTION_DATA (connection);
770 _dbus_assert (d != NULL);
772 link = _dbus_list_get_first_link (&d->transaction_messages);
775 MessageToSend *m = link->data;
776 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
778 if (m->transaction == transaction)
780 _dbus_list_remove_link (&d->transaction_messages,
783 message_to_send_free (connection, m);
791 bus_transaction_cancel_and_free (BusTransaction *transaction)
793 DBusConnection *connection;
795 _dbus_verbose ("TRANSACTION: cancelled\n");
797 while ((connection = _dbus_list_pop_first (&transaction->connections)))
798 connection_cancel_transaction (connection, transaction);
800 _dbus_assert (transaction->connections == NULL);
802 dbus_free (transaction);
806 connection_execute_transaction (DBusConnection *connection,
807 BusTransaction *transaction)
810 BusConnectionData *d;
812 d = BUS_CONNECTION_DATA (connection);
813 _dbus_assert (d != NULL);
815 /* Send the queue in order (FIFO) */
816 link = _dbus_list_get_last_link (&d->transaction_messages);
819 MessageToSend *m = link->data;
820 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
822 if (m->transaction == transaction)
824 _dbus_list_remove_link (&d->transaction_messages,
827 _dbus_assert (dbus_message_get_sender (m->message) != NULL);
829 dbus_connection_send_preallocated (connection,
834 m->preallocated = NULL; /* so we don't double-free it */
836 message_to_send_free (connection, m);
844 bus_transaction_execute_and_free (BusTransaction *transaction)
846 /* For each connection in transaction->connections
849 DBusConnection *connection;
851 _dbus_verbose ("TRANSACTION: executing\n");
853 while ((connection = _dbus_list_pop_first (&transaction->connections)))
854 connection_execute_transaction (connection, transaction);
856 _dbus_assert (transaction->connections == NULL);
858 dbus_free (transaction);
862 bus_connection_remove_transactions (DBusConnection *connection)
864 MessageToSend *to_send;
865 BusConnectionData *d;
867 d = BUS_CONNECTION_DATA (connection);
868 _dbus_assert (d != NULL);
870 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
872 /* only has an effect for the first MessageToSend listing this transaction */
873 _dbus_list_remove (&to_send->transaction->connections,
876 _dbus_list_remove (&d->transaction_messages, to_send);
877 message_to_send_free (connection, to_send);
882 * Converts the DBusError to a message reply
885 bus_transaction_send_error_reply (BusTransaction *transaction,
886 DBusConnection *connection,
887 const DBusError *error,
888 DBusMessage *in_reply_to)
892 _dbus_assert (error != NULL);
893 _DBUS_ASSERT_ERROR_IS_SET (error);
895 reply = dbus_message_new_error_reply (in_reply_to,
901 if (!bus_transaction_send_message (transaction, connection, reply))
903 dbus_message_unref (reply);