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;
43 BusConnections *connections;
44 DBusConnection *connection;
45 DBusList *services_owned;
47 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
48 DBusMessage *oom_message;
49 DBusPreallocatedSend *oom_preallocated;
52 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
55 bus_connection_disconnected (DBusConnection *connection)
60 d = BUS_CONNECTION_DATA (connection);
61 _dbus_assert (d != NULL);
63 _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
64 d->name ? d->name : "(inactive)");
66 /* Drop any service ownership. FIXME Unfortunately, this requires
67 * memory allocation and there doesn't seem to be a good way to
68 * handle it other than sleeping; we can't "fail" the operation of
69 * disconnecting a client, and preallocating a broadcast "service is
70 * now gone" message for every client-service pair seems kind of
71 * involved. Probably we need to do that though, and also
72 * extend BusTransaction to be able to revert generic
73 * stuff, not just sending a message (so we can e.g. revert
74 * removal of service owners).
76 while ((service = _dbus_list_get_last (&d->services_owned)))
78 BusTransaction *transaction;
83 dbus_error_init (&error);
86 while (transaction == NULL)
88 transaction = bus_transaction_new (d->connections->context);
89 bus_wait_for_memory ();
92 if (!bus_service_remove_owner (service, connection,
95 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
97 dbus_error_free (&error);
98 bus_transaction_cancel_and_free (transaction);
99 bus_wait_for_memory ();
103 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
106 bus_transaction_execute_and_free (transaction);
109 bus_dispatch_remove_connection (connection);
111 /* no more watching */
112 if (!dbus_connection_set_watch_functions (connection,
116 _dbus_assert_not_reached ("setting watch functions to NULL failed");
118 if (!dbus_connection_set_timeout_functions (connection,
122 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
124 bus_connection_remove_transactions (connection);
126 _dbus_list_remove (&d->connections->list, connection);
128 /* frees "d" as side effect */
129 dbus_connection_set_data (connection,
130 connection_data_slot,
133 dbus_connection_unref (connection);
137 connection_watch_callback (DBusWatch *watch,
138 unsigned int condition,
141 DBusConnection *connection = data;
143 dbus_connection_ref (connection);
145 dbus_connection_handle_watch (connection, watch, condition);
147 bus_connection_dispatch_all_messages (connection);
149 dbus_connection_unref (connection);
153 add_connection_watch (DBusWatch *watch,
154 DBusConnection *connection)
156 return bus_loop_add_watch (watch, connection_watch_callback, connection,
161 remove_connection_watch (DBusWatch *watch,
162 DBusConnection *connection)
164 bus_loop_remove_watch (watch, connection_watch_callback, connection);
168 connection_timeout_callback (DBusTimeout *timeout,
171 DBusConnection *connection = data;
173 dbus_connection_ref (connection);
175 dbus_timeout_handle (timeout);
177 bus_connection_dispatch_all_messages (connection);
179 dbus_connection_unref (connection);
183 add_connection_timeout (DBusTimeout *timeout,
184 DBusConnection *connection)
186 return bus_loop_add_timeout (timeout, connection_timeout_callback, connection, NULL);
190 remove_connection_timeout (DBusTimeout *timeout,
191 DBusConnection *connection)
193 bus_loop_remove_timeout (timeout, connection_timeout_callback, connection);
197 free_connection_data (void *data)
199 BusConnectionData *d = data;
201 /* services_owned should be NULL since we should be disconnected */
202 _dbus_assert (d->services_owned == NULL);
204 _dbus_assert (d->transaction_messages == NULL);
206 if (d->oom_preallocated)
207 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
209 dbus_message_unref (d->oom_message);
217 bus_connections_new (BusContext *context)
219 BusConnections *connections;
221 if (connection_data_slot < 0)
223 connection_data_slot = dbus_connection_allocate_data_slot ();
225 if (connection_data_slot < 0)
229 connections = dbus_new0 (BusConnections, 1);
230 if (connections == NULL)
233 connections->refcount = 1;
234 connections->context = context;
240 bus_connections_ref (BusConnections *connections)
242 _dbus_assert (connections->refcount > 0);
243 connections->refcount += 1;
247 bus_connections_unref (BusConnections *connections)
249 _dbus_assert (connections->refcount > 0);
250 connections->refcount -= 1;
251 if (connections->refcount == 0)
253 while (connections->list != NULL)
255 DBusConnection *connection;
257 connection = connections->list->data;
259 dbus_connection_ref (connection);
260 dbus_connection_disconnect (connection);
261 bus_connection_disconnected (connection);
262 dbus_connection_unref (connection);
265 _dbus_list_clear (&connections->list);
267 dbus_free (connections);
272 bus_connections_setup_connection (BusConnections *connections,
273 DBusConnection *connection)
275 BusConnectionData *d;
278 d = dbus_new0 (BusConnectionData, 1);
283 d->connections = connections;
284 d->connection = connection;
286 if (!dbus_connection_set_data (connection,
287 connection_data_slot,
288 d, free_connection_data))
296 if (!dbus_connection_set_watch_functions (connection,
297 (DBusAddWatchFunction) add_connection_watch,
298 (DBusRemoveWatchFunction) remove_connection_watch,
304 if (!dbus_connection_set_timeout_functions (connection,
305 (DBusAddTimeoutFunction) add_connection_timeout,
306 (DBusRemoveTimeoutFunction) remove_connection_timeout,
312 /* Setup the connection with the dispatcher */
313 if (!bus_dispatch_add_connection (connection))
316 if (!_dbus_list_append (&connections->list, connection))
318 bus_dispatch_remove_connection (connection);
322 dbus_connection_ref (connection);
328 if (!dbus_connection_set_watch_functions (connection,
332 _dbus_assert_not_reached ("setting watch functions to NULL failed");
334 if (!dbus_connection_set_timeout_functions (connection,
338 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
346 * Calls function on each connection; if the function returns
347 * #FALSE, stops iterating.
349 * @param connections the connections object
350 * @param function the function
351 * @param data data to pass to it as a second arg
354 bus_connections_foreach (BusConnections *connections,
355 BusConnectionForeachFunction function,
360 link = _dbus_list_get_first_link (&connections->list);
363 DBusConnection *connection = link->data;
364 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
366 if (!(* function) (connection, data))
374 bus_connections_get_context (BusConnections *connections)
376 return connections->context;
380 bus_connection_get_context (DBusConnection *connection)
382 BusConnectionData *d;
384 d = BUS_CONNECTION_DATA (connection);
386 _dbus_assert (d != NULL);
388 return d->connections->context;
392 bus_connection_get_connections (DBusConnection *connection)
394 BusConnectionData *d;
396 d = BUS_CONNECTION_DATA (connection);
398 _dbus_assert (d != NULL);
400 return d->connections;
404 bus_connection_get_registry (DBusConnection *connection)
406 BusConnectionData *d;
408 d = BUS_CONNECTION_DATA (connection);
410 _dbus_assert (d != NULL);
412 return bus_context_get_registry (d->connections->context);
416 bus_connection_get_activation (DBusConnection *connection)
418 BusConnectionData *d;
420 d = BUS_CONNECTION_DATA (connection);
422 _dbus_assert (d != NULL);
424 return bus_context_get_activation (d->connections->context);
428 * Checks whether the connection is registered with the message bus.
430 * @param connection the connection
431 * @returns #TRUE if we're an active message bus participant
434 bus_connection_is_active (DBusConnection *connection)
436 BusConnectionData *d;
438 d = BUS_CONNECTION_DATA (connection);
440 return d != NULL && d->name != NULL;
444 bus_connection_preallocate_oom_error (DBusConnection *connection)
446 DBusMessage *message;
447 DBusPreallocatedSend *preallocated;
448 BusConnectionData *d;
450 d = BUS_CONNECTION_DATA (connection);
452 _dbus_assert (d != NULL);
454 if (d->oom_preallocated != NULL)
457 preallocated = dbus_connection_preallocate_send (connection);
458 if (preallocated == NULL)
461 message = dbus_message_new (DBUS_SERVICE_DBUS,
462 DBUS_ERROR_NO_MEMORY);
465 dbus_connection_free_preallocated_send (connection, preallocated);
469 dbus_message_set_is_error (message, TRUE);
471 /* set reply serial to placeholder value just so space is already allocated
474 if (!dbus_message_set_reply_serial (message, 14))
476 dbus_connection_free_preallocated_send (connection, preallocated);
477 dbus_message_unref (message);
481 d->oom_message = message;
482 d->oom_preallocated = preallocated;
488 bus_connection_send_oom_error (DBusConnection *connection,
489 DBusMessage *in_reply_to)
491 BusConnectionData *d;
493 d = BUS_CONNECTION_DATA (connection);
495 _dbus_assert (d != NULL);
496 _dbus_assert (d->oom_message != NULL);
498 /* should always succeed since we set it to a placeholder earlier */
499 if (!dbus_message_set_reply_serial (d->oom_message,
500 dbus_message_get_serial (in_reply_to)))
501 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
503 dbus_connection_send_preallocated (connection, d->oom_preallocated,
504 d->oom_message, NULL);
506 dbus_message_unref (d->oom_message);
507 d->oom_message = NULL;
508 d->oom_preallocated = NULL;
512 bus_connection_add_owned_service (DBusConnection *connection,
515 BusConnectionData *d;
517 d = BUS_CONNECTION_DATA (connection);
518 _dbus_assert (d != NULL);
520 if (!_dbus_list_append (&d->services_owned,
528 bus_connection_remove_owned_service (DBusConnection *connection,
531 BusConnectionData *d;
533 d = BUS_CONNECTION_DATA (connection);
534 _dbus_assert (d != NULL);
536 _dbus_list_remove_last (&d->services_owned, service);
540 bus_connection_set_name (DBusConnection *connection,
541 const DBusString *name)
544 BusConnectionData *d;
546 d = BUS_CONNECTION_DATA (connection);
547 _dbus_assert (d != NULL);
548 _dbus_assert (d->name == NULL);
550 _dbus_string_get_const_data (name, &c_name);
552 d->name = _dbus_strdup (c_name);
557 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
563 bus_connection_get_name (DBusConnection *connection)
565 BusConnectionData *d;
567 d = BUS_CONNECTION_DATA (connection);
568 _dbus_assert (d != NULL);
575 BusTransaction *transaction;
576 DBusMessage *message;
577 DBusPreallocatedSend *preallocated;
580 struct BusTransaction
582 DBusList *connections;
587 message_to_send_free (DBusConnection *connection,
588 MessageToSend *to_send)
590 if (to_send->message)
591 dbus_message_unref (to_send->message);
593 if (to_send->preallocated)
594 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
600 bus_transaction_new (BusContext *context)
602 BusTransaction *transaction;
604 transaction = dbus_new0 (BusTransaction, 1);
605 if (transaction == NULL)
608 transaction->context = context;
614 bus_transaction_get_context (BusTransaction *transaction)
616 return transaction->context;
620 bus_transaction_get_connections (BusTransaction *transaction)
622 return bus_context_get_connections (transaction->context);
626 bus_transaction_send_message (BusTransaction *transaction,
627 DBusConnection *connection,
628 DBusMessage *message)
630 MessageToSend *to_send;
631 BusConnectionData *d;
634 _dbus_verbose (" trying to add message %s to transaction%s\n",
635 dbus_message_get_name (message),
636 dbus_connection_get_is_connected (connection) ?
637 "" : " (disconnected)");
639 if (!dbus_connection_get_is_connected (connection))
640 return TRUE; /* silently ignore disconnected connections */
642 d = BUS_CONNECTION_DATA (connection);
643 _dbus_assert (d != NULL);
645 to_send = dbus_new (MessageToSend, 1);
651 to_send->preallocated = dbus_connection_preallocate_send (connection);
652 if (to_send->preallocated == NULL)
658 dbus_message_ref (message);
659 to_send->message = message;
660 to_send->transaction = transaction;
662 _dbus_verbose ("about to prepend message\n");
664 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
666 message_to_send_free (connection, to_send);
670 _dbus_verbose ("prepended message\n");
672 /* See if we already had this connection in the list
673 * for this transaction. If we have a pending message,
674 * then we should already be in transaction->connections
676 link = _dbus_list_get_first_link (&d->transaction_messages);
677 _dbus_assert (link->data == to_send);
678 link = _dbus_list_get_next_link (&d->transaction_messages, link);
681 MessageToSend *m = link->data;
682 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
684 if (m->transaction == transaction)
692 if (!_dbus_list_prepend (&transaction->connections, connection))
694 _dbus_list_remove (&d->transaction_messages, to_send);
695 message_to_send_free (connection, to_send);
704 connection_cancel_transaction (DBusConnection *connection,
705 BusTransaction *transaction)
708 BusConnectionData *d;
710 d = BUS_CONNECTION_DATA (connection);
711 _dbus_assert (d != NULL);
713 link = _dbus_list_get_first_link (&d->transaction_messages);
716 MessageToSend *m = link->data;
717 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
719 if (m->transaction == transaction)
721 _dbus_list_remove_link (&d->transaction_messages,
724 message_to_send_free (connection, m);
732 bus_transaction_cancel_and_free (BusTransaction *transaction)
734 DBusConnection *connection;
736 _dbus_verbose ("TRANSACTION: cancelled\n");
738 while ((connection = _dbus_list_pop_first (&transaction->connections)))
739 connection_cancel_transaction (connection, transaction);
741 _dbus_assert (transaction->connections == NULL);
743 dbus_free (transaction);
747 connection_execute_transaction (DBusConnection *connection,
748 BusTransaction *transaction)
751 BusConnectionData *d;
753 d = BUS_CONNECTION_DATA (connection);
754 _dbus_assert (d != NULL);
756 /* Send the queue in order (FIFO) */
757 link = _dbus_list_get_last_link (&d->transaction_messages);
760 MessageToSend *m = link->data;
761 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
763 if (m->transaction == transaction)
765 _dbus_list_remove_link (&d->transaction_messages,
768 dbus_connection_send_preallocated (connection,
773 m->preallocated = NULL; /* so we don't double-free it */
775 message_to_send_free (connection, m);
783 bus_transaction_execute_and_free (BusTransaction *transaction)
785 /* For each connection in transaction->connections
788 DBusConnection *connection;
790 _dbus_verbose ("TRANSACTION: executing\n");
792 while ((connection = _dbus_list_pop_first (&transaction->connections)))
793 connection_execute_transaction (connection, transaction);
795 _dbus_assert (transaction->connections == NULL);
797 dbus_free (transaction);
801 bus_connection_remove_transactions (DBusConnection *connection)
803 MessageToSend *to_send;
804 BusConnectionData *d;
806 d = BUS_CONNECTION_DATA (connection);
807 _dbus_assert (d != NULL);
809 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
811 /* only has an effect for the first MessageToSend listing this transaction */
812 _dbus_list_remove (&to_send->transaction->connections,
815 _dbus_list_remove (&d->transaction_messages, to_send);
816 message_to_send_free (connection, to_send);
821 * Converts the DBusError to a message reply
824 bus_transaction_send_error_reply (BusTransaction *transaction,
825 DBusConnection *connection,
826 const DBusError *error,
827 DBusMessage *in_reply_to)
831 _dbus_assert (error != NULL);
832 _DBUS_ASSERT_ERROR_IS_SET (error);
834 reply = dbus_message_new_error_reply (in_reply_to,
840 if (!bus_transaction_send_message (transaction, connection, reply))
842 dbus_message_unref (reply);