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 /* Drop any service ownership. FIXME Unfortunately, this requires
64 * memory allocation and there doesn't seem to be a good way to
65 * handle it other than sleeping; we can't "fail" the operation of
66 * disconnecting a client, and preallocating a broadcast "service is
67 * now gone" message for every client-service pair seems kind of
68 * involved. Probably we need to do that though, and also
69 * extend BusTransaction to be able to revert generic
70 * stuff, not just sending a message (so we can e.g. revert
71 * removal of service owners).
74 BusTransaction *transaction;
77 dbus_error_init (&error);
80 while (transaction == NULL)
82 transaction = bus_transaction_new (d->connections->context);
83 bus_wait_for_memory ();
86 while ((service = _dbus_list_get_last (&d->services_owned)))
89 if (!bus_service_remove_owner (service, connection,
92 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
94 dbus_error_free (&error);
95 bus_wait_for_memory ();
99 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
103 bus_transaction_execute_and_free (transaction);
106 bus_dispatch_remove_connection (connection);
108 /* no more watching */
109 if (!dbus_connection_set_watch_functions (connection,
113 _dbus_assert_not_reached ("setting watch functions to NULL failed");
115 if (!dbus_connection_set_timeout_functions (connection,
119 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
121 bus_connection_remove_transactions (connection);
123 _dbus_list_remove (&d->connections->list, connection);
125 /* frees "d" as side effect */
126 dbus_connection_set_data (connection,
127 connection_data_slot,
130 dbus_connection_unref (connection);
134 connection_watch_callback (DBusWatch *watch,
135 unsigned int condition,
138 DBusConnection *connection = data;
140 dbus_connection_ref (connection);
142 dbus_connection_handle_watch (connection, watch, condition);
144 while (dbus_connection_dispatch_message (connection))
146 dbus_connection_unref (connection);
150 add_connection_watch (DBusWatch *watch,
151 DBusConnection *connection)
153 return bus_loop_add_watch (watch, connection_watch_callback, connection,
158 remove_connection_watch (DBusWatch *watch,
159 DBusConnection *connection)
161 bus_loop_remove_watch (watch, connection_watch_callback, connection);
165 connection_timeout_callback (DBusTimeout *timeout,
168 DBusConnection *connection = data;
170 dbus_connection_ref (connection);
172 dbus_timeout_handle (timeout);
174 while (dbus_connection_dispatch_message (connection))
176 dbus_connection_unref (connection);
180 add_connection_timeout (DBusTimeout *timeout,
181 DBusConnection *connection)
183 return bus_loop_add_timeout (timeout, connection_timeout_callback, connection, NULL);
187 remove_connection_timeout (DBusTimeout *timeout,
188 DBusConnection *connection)
190 bus_loop_remove_timeout (timeout, connection_timeout_callback, connection);
194 free_connection_data (void *data)
196 BusConnectionData *d = data;
198 /* services_owned should be NULL since we should be disconnected */
199 _dbus_assert (d->services_owned == NULL);
201 _dbus_assert (d->transaction_messages == NULL);
203 if (d->oom_preallocated)
204 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
206 dbus_message_unref (d->oom_message);
214 bus_connections_new (BusContext *context)
216 BusConnections *connections;
218 if (connection_data_slot < 0)
220 connection_data_slot = dbus_connection_allocate_data_slot ();
222 if (connection_data_slot < 0)
226 connections = dbus_new0 (BusConnections, 1);
227 if (connections == NULL)
230 connections->refcount = 1;
231 connections->context = context;
237 bus_connections_ref (BusConnections *connections)
239 _dbus_assert (connections->refcount > 0);
240 connections->refcount += 1;
244 bus_connections_unref (BusConnections *connections)
246 _dbus_assert (connections->refcount > 0);
247 connections->refcount -= 1;
248 if (connections->refcount == 0)
250 /* FIXME free each connection... */
251 _dbus_assert_not_reached ("shutting down connections not implemented");
253 _dbus_list_clear (&connections->list);
255 dbus_free (connections);
260 bus_connections_setup_connection (BusConnections *connections,
261 DBusConnection *connection)
263 BusConnectionData *d;
265 d = dbus_new0 (BusConnectionData, 1);
270 d->connections = connections;
271 d->connection = connection;
273 if (!dbus_connection_set_data (connection,
274 connection_data_slot,
275 d, free_connection_data))
281 if (!_dbus_list_append (&connections->list, connection))
283 /* this will free our data when connection gets finalized */
284 dbus_connection_disconnect (connection);
288 if (!dbus_connection_set_watch_functions (connection,
289 (DBusAddWatchFunction) add_connection_watch,
290 (DBusRemoveWatchFunction) remove_connection_watch,
294 dbus_connection_disconnect (connection);
298 if (!dbus_connection_set_timeout_functions (connection,
299 (DBusAddTimeoutFunction) add_connection_timeout,
300 (DBusRemoveTimeoutFunction) remove_connection_timeout,
303 dbus_connection_disconnect (connection);
308 /* Setup the connection with the dispatcher */
309 if (!bus_dispatch_add_connection (connection))
311 dbus_connection_disconnect (connection);
315 dbus_connection_ref (connection);
322 * Calls function on each connection; if the function returns
323 * #FALSE, stops iterating.
325 * @param connections the connections object
326 * @param function the function
327 * @param data data to pass to it as a second arg
330 bus_connections_foreach (BusConnections *connections,
331 BusConnectionForeachFunction function,
336 link = _dbus_list_get_first_link (&connections->list);
339 DBusConnection *connection = link->data;
340 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
342 if (!(* function) (connection, data))
350 bus_connections_get_context (BusConnections *connections)
352 return connections->context;
356 bus_connection_get_context (DBusConnection *connection)
358 BusConnectionData *d;
360 d = BUS_CONNECTION_DATA (connection);
362 _dbus_assert (d != NULL);
364 return d->connections->context;
368 bus_connection_get_connections (DBusConnection *connection)
370 BusConnectionData *d;
372 d = BUS_CONNECTION_DATA (connection);
374 _dbus_assert (d != NULL);
376 return d->connections;
380 bus_connection_get_registry (DBusConnection *connection)
382 BusConnectionData *d;
384 d = BUS_CONNECTION_DATA (connection);
386 _dbus_assert (d != NULL);
388 return bus_context_get_registry (d->connections->context);
392 bus_connection_get_activation (DBusConnection *connection)
394 BusConnectionData *d;
396 d = BUS_CONNECTION_DATA (connection);
398 _dbus_assert (d != NULL);
400 return bus_context_get_activation (d->connections->context);
404 * Checks whether the connection is registered with the message bus.
406 * @param connection the connection
407 * @returns #TRUE if we're an active message bus participant
410 bus_connection_is_active (DBusConnection *connection)
412 BusConnectionData *d;
414 d = BUS_CONNECTION_DATA (connection);
416 return d != NULL && d->name != NULL;
420 bus_connection_preallocate_oom_error (DBusConnection *connection)
422 DBusMessage *message;
423 DBusPreallocatedSend *preallocated;
424 BusConnectionData *d;
426 d = BUS_CONNECTION_DATA (connection);
428 _dbus_assert (d != NULL);
430 if (d->oom_preallocated != NULL)
433 preallocated = dbus_connection_preallocate_send (connection);
434 if (preallocated == NULL)
437 message = dbus_message_new (DBUS_SERVICE_DBUS,
438 DBUS_ERROR_NO_MEMORY);
441 dbus_connection_free_preallocated_send (connection, preallocated);
445 /* set reply serial to placeholder value just so space is already allocated
448 if (!dbus_message_set_reply_serial (message, 14))
450 dbus_connection_free_preallocated_send (connection, preallocated);
451 dbus_message_unref (message);
455 d->oom_message = message;
456 d->oom_preallocated = preallocated;
462 bus_connection_send_oom_error (DBusConnection *connection,
463 DBusMessage *in_reply_to)
465 BusConnectionData *d;
467 d = BUS_CONNECTION_DATA (connection);
469 _dbus_assert (d != NULL);
470 _dbus_assert (d->oom_message != NULL);
472 /* should always succeed since we set it to a placeholder earlier */
473 if (!dbus_message_set_reply_serial (d->oom_message,
474 dbus_message_get_serial (in_reply_to)))
475 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
477 dbus_connection_send_preallocated (connection, d->oom_preallocated,
478 d->oom_message, NULL);
480 dbus_message_unref (d->oom_message);
481 d->oom_message = NULL;
482 d->oom_preallocated = NULL;
486 bus_connection_add_owned_service (DBusConnection *connection,
489 BusConnectionData *d;
491 d = BUS_CONNECTION_DATA (connection);
492 _dbus_assert (d != NULL);
494 if (!_dbus_list_append (&d->services_owned,
502 bus_connection_remove_owned_service (DBusConnection *connection,
505 BusConnectionData *d;
507 d = BUS_CONNECTION_DATA (connection);
508 _dbus_assert (d != NULL);
510 _dbus_list_remove_last (&d->services_owned, service);
514 bus_connection_set_name (DBusConnection *connection,
515 const DBusString *name)
518 BusConnectionData *d;
520 d = BUS_CONNECTION_DATA (connection);
521 _dbus_assert (d != NULL);
522 _dbus_assert (d->name == NULL);
524 _dbus_string_get_const_data (name, &c_name);
526 d->name = _dbus_strdup (c_name);
535 bus_connection_get_name (DBusConnection *connection)
537 BusConnectionData *d;
539 d = BUS_CONNECTION_DATA (connection);
540 _dbus_assert (d != NULL);
547 BusTransaction *transaction;
548 DBusMessage *message;
549 DBusPreallocatedSend *preallocated;
552 struct BusTransaction
554 DBusList *connections;
559 message_to_send_free (DBusConnection *connection,
560 MessageToSend *to_send)
562 if (to_send->message)
563 dbus_message_unref (to_send->message);
565 if (to_send->preallocated)
566 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
572 bus_transaction_new (BusContext *context)
574 BusTransaction *transaction;
576 transaction = dbus_new0 (BusTransaction, 1);
577 if (transaction == NULL)
580 transaction->context = context;
586 bus_transaction_get_context (BusTransaction *transaction)
588 return transaction->context;
592 bus_transaction_get_connections (BusTransaction *transaction)
594 return bus_context_get_connections (transaction->context);
598 bus_transaction_send_message (BusTransaction *transaction,
599 DBusConnection *connection,
600 DBusMessage *message)
602 MessageToSend *to_send;
603 BusConnectionData *d;
606 if (!dbus_connection_get_is_connected (connection))
607 return TRUE; /* silently ignore disconnected connections */
609 d = BUS_CONNECTION_DATA (connection);
610 _dbus_assert (d != NULL);
612 to_send = dbus_new (MessageToSend, 1);
618 to_send->preallocated = dbus_connection_preallocate_send (connection);
619 if (to_send->preallocated == NULL)
625 dbus_message_ref (message);
626 to_send->message = message;
627 to_send->transaction = transaction;
629 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
631 message_to_send_free (connection, to_send);
635 /* See if we already had this connection in the list
636 * for this transaction. If we have a pending message,
637 * then we should already be in transaction->connections
639 link = _dbus_list_get_first_link (&d->transaction_messages);
640 _dbus_assert (link->data == to_send);
641 link = _dbus_list_get_next_link (&d->transaction_messages, link);
644 MessageToSend *m = link->data;
645 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
647 if (m->transaction == transaction)
655 if (!_dbus_list_prepend (&transaction->connections, connection))
657 _dbus_list_remove (&d->transaction_messages, to_send);
658 message_to_send_free (connection, to_send);
667 connection_cancel_transaction (DBusConnection *connection,
668 BusTransaction *transaction)
671 BusConnectionData *d;
673 d = BUS_CONNECTION_DATA (connection);
674 _dbus_assert (d != NULL);
676 link = _dbus_list_get_first_link (&d->transaction_messages);
679 MessageToSend *m = link->data;
680 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
682 if (m->transaction == transaction)
684 _dbus_list_remove_link (&d->transaction_messages,
687 message_to_send_free (connection, m);
695 bus_transaction_cancel_and_free (BusTransaction *transaction)
697 DBusConnection *connection;
699 while ((connection = _dbus_list_pop_first (&transaction->connections)))
700 connection_cancel_transaction (connection, transaction);
702 _dbus_assert (transaction->connections == NULL);
704 dbus_free (transaction);
708 connection_execute_transaction (DBusConnection *connection,
709 BusTransaction *transaction)
712 BusConnectionData *d;
714 d = BUS_CONNECTION_DATA (connection);
715 _dbus_assert (d != NULL);
717 /* Send the queue in order (FIFO) */
718 link = _dbus_list_get_last_link (&d->transaction_messages);
721 MessageToSend *m = link->data;
722 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
724 if (m->transaction == transaction)
726 _dbus_list_remove_link (&d->transaction_messages,
729 dbus_connection_send_preallocated (connection,
734 m->preallocated = NULL; /* so we don't double-free it */
736 message_to_send_free (connection, m);
744 bus_transaction_execute_and_free (BusTransaction *transaction)
746 /* For each connection in transaction->connections
749 DBusConnection *connection;
751 while ((connection = _dbus_list_pop_first (&transaction->connections)))
752 connection_execute_transaction (connection, transaction);
754 _dbus_assert (transaction->connections == NULL);
756 dbus_free (transaction);
760 bus_connection_remove_transactions (DBusConnection *connection)
762 MessageToSend *to_send;
763 BusConnectionData *d;
765 d = BUS_CONNECTION_DATA (connection);
766 _dbus_assert (d != NULL);
768 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
770 /* only has an effect for the first MessageToSend listing this transaction */
771 _dbus_list_remove (&to_send->transaction->connections,
774 _dbus_list_remove (&d->transaction_messages, to_send);
775 message_to_send_free (connection, to_send);
780 * Converts the DBusError to a message reply
783 bus_transaction_send_error_reply (BusTransaction *transaction,
784 DBusConnection *connection,
785 const DBusError *error,
786 DBusMessage *in_reply_to)
790 _dbus_assert (error != NULL);
791 _DBUS_ASSERT_ERROR_IS_SET (error);
793 reply = dbus_message_new_error_reply (in_reply_to,
799 if (!bus_transaction_send_message (transaction, connection, reply))
801 dbus_message_unref (reply);