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 message = dbus_message_new (DBUS_SERVICE_DBUS,
508 DBUS_ERROR_NO_MEMORY);
511 dbus_connection_free_preallocated_send (connection, preallocated);
515 dbus_message_set_is_error (message, TRUE);
517 /* set reply serial to placeholder value just so space is already allocated
520 if (!dbus_message_set_reply_serial (message, 14))
522 dbus_connection_free_preallocated_send (connection, preallocated);
523 dbus_message_unref (message);
527 d->oom_message = message;
528 d->oom_preallocated = preallocated;
534 bus_connection_send_oom_error (DBusConnection *connection,
535 DBusMessage *in_reply_to)
537 BusConnectionData *d;
539 d = BUS_CONNECTION_DATA (connection);
541 _dbus_assert (d != NULL);
542 _dbus_assert (d->oom_message != NULL);
544 /* should always succeed since we set it to a placeholder earlier */
545 if (!dbus_message_set_reply_serial (d->oom_message,
546 dbus_message_get_serial (in_reply_to)))
547 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
549 dbus_connection_send_preallocated (connection, d->oom_preallocated,
550 d->oom_message, NULL);
552 dbus_message_unref (d->oom_message);
553 d->oom_message = NULL;
554 d->oom_preallocated = NULL;
558 bus_connection_add_owned_service (DBusConnection *connection,
561 BusConnectionData *d;
563 d = BUS_CONNECTION_DATA (connection);
564 _dbus_assert (d != NULL);
566 if (!_dbus_list_append (&d->services_owned,
574 bus_connection_remove_owned_service (DBusConnection *connection,
577 BusConnectionData *d;
579 d = BUS_CONNECTION_DATA (connection);
580 _dbus_assert (d != NULL);
582 _dbus_list_remove_last (&d->services_owned, service);
586 bus_connection_set_name (DBusConnection *connection,
587 const DBusString *name)
590 BusConnectionData *d;
592 d = BUS_CONNECTION_DATA (connection);
593 _dbus_assert (d != NULL);
594 _dbus_assert (d->name == NULL);
596 _dbus_string_get_const_data (name, &c_name);
598 d->name = _dbus_strdup (c_name);
603 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
609 bus_connection_get_name (DBusConnection *connection)
611 BusConnectionData *d;
613 d = BUS_CONNECTION_DATA (connection);
614 _dbus_assert (d != NULL);
621 BusTransaction *transaction;
622 DBusMessage *message;
623 DBusPreallocatedSend *preallocated;
626 struct BusTransaction
628 DBusList *connections;
633 message_to_send_free (DBusConnection *connection,
634 MessageToSend *to_send)
636 if (to_send->message)
637 dbus_message_unref (to_send->message);
639 if (to_send->preallocated)
640 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
646 bus_transaction_new (BusContext *context)
648 BusTransaction *transaction;
650 transaction = dbus_new0 (BusTransaction, 1);
651 if (transaction == NULL)
654 transaction->context = context;
660 bus_transaction_get_context (BusTransaction *transaction)
662 return transaction->context;
666 bus_transaction_get_connections (BusTransaction *transaction)
668 return bus_context_get_connections (transaction->context);
672 bus_transaction_send_message (BusTransaction *transaction,
673 DBusConnection *connection,
674 DBusMessage *message)
676 MessageToSend *to_send;
677 BusConnectionData *d;
680 _dbus_verbose (" trying to add message %s to transaction%s\n",
681 dbus_message_get_name (message),
682 dbus_connection_get_is_connected (connection) ?
683 "" : " (disconnected)");
685 if (!dbus_connection_get_is_connected (connection))
686 return TRUE; /* silently ignore disconnected connections */
688 d = BUS_CONNECTION_DATA (connection);
689 _dbus_assert (d != NULL);
691 to_send = dbus_new (MessageToSend, 1);
697 to_send->preallocated = dbus_connection_preallocate_send (connection);
698 if (to_send->preallocated == NULL)
704 dbus_message_ref (message);
705 to_send->message = message;
706 to_send->transaction = transaction;
708 _dbus_verbose ("about to prepend message\n");
710 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
712 message_to_send_free (connection, to_send);
716 _dbus_verbose ("prepended message\n");
718 /* See if we already had this connection in the list
719 * for this transaction. If we have a pending message,
720 * then we should already be in transaction->connections
722 link = _dbus_list_get_first_link (&d->transaction_messages);
723 _dbus_assert (link->data == to_send);
724 link = _dbus_list_get_next_link (&d->transaction_messages, link);
727 MessageToSend *m = link->data;
728 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
730 if (m->transaction == transaction)
738 if (!_dbus_list_prepend (&transaction->connections, connection))
740 _dbus_list_remove (&d->transaction_messages, to_send);
741 message_to_send_free (connection, to_send);
750 connection_cancel_transaction (DBusConnection *connection,
751 BusTransaction *transaction)
754 BusConnectionData *d;
756 d = BUS_CONNECTION_DATA (connection);
757 _dbus_assert (d != NULL);
759 link = _dbus_list_get_first_link (&d->transaction_messages);
762 MessageToSend *m = link->data;
763 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
765 if (m->transaction == transaction)
767 _dbus_list_remove_link (&d->transaction_messages,
770 message_to_send_free (connection, m);
778 bus_transaction_cancel_and_free (BusTransaction *transaction)
780 DBusConnection *connection;
782 _dbus_verbose ("TRANSACTION: cancelled\n");
784 while ((connection = _dbus_list_pop_first (&transaction->connections)))
785 connection_cancel_transaction (connection, transaction);
787 _dbus_assert (transaction->connections == NULL);
789 dbus_free (transaction);
793 connection_execute_transaction (DBusConnection *connection,
794 BusTransaction *transaction)
797 BusConnectionData *d;
799 d = BUS_CONNECTION_DATA (connection);
800 _dbus_assert (d != NULL);
802 /* Send the queue in order (FIFO) */
803 link = _dbus_list_get_last_link (&d->transaction_messages);
806 MessageToSend *m = link->data;
807 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
809 if (m->transaction == transaction)
811 _dbus_list_remove_link (&d->transaction_messages,
814 dbus_connection_send_preallocated (connection,
819 m->preallocated = NULL; /* so we don't double-free it */
821 message_to_send_free (connection, m);
829 bus_transaction_execute_and_free (BusTransaction *transaction)
831 /* For each connection in transaction->connections
834 DBusConnection *connection;
836 _dbus_verbose ("TRANSACTION: executing\n");
838 while ((connection = _dbus_list_pop_first (&transaction->connections)))
839 connection_execute_transaction (connection, transaction);
841 _dbus_assert (transaction->connections == NULL);
843 dbus_free (transaction);
847 bus_connection_remove_transactions (DBusConnection *connection)
849 MessageToSend *to_send;
850 BusConnectionData *d;
852 d = BUS_CONNECTION_DATA (connection);
853 _dbus_assert (d != NULL);
855 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
857 /* only has an effect for the first MessageToSend listing this transaction */
858 _dbus_list_remove (&to_send->transaction->connections,
861 _dbus_list_remove (&d->transaction_messages, to_send);
862 message_to_send_free (connection, to_send);
867 * Converts the DBusError to a message reply
870 bus_transaction_send_error_reply (BusTransaction *transaction,
871 DBusConnection *connection,
872 const DBusError *error,
873 DBusMessage *in_reply_to)
877 _dbus_assert (error != NULL);
878 _DBUS_ASSERT_ERROR_IS_SET (error);
880 reply = dbus_message_new_error_reply (in_reply_to,
886 if (!bus_transaction_send_message (transaction, connection, reply))
888 dbus_message_unref (reply);