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,
295 dbus_connection_disconnect (connection);
299 if (!dbus_connection_set_timeout_functions (connection,
300 (DBusAddTimeoutFunction) add_connection_timeout,
301 (DBusRemoveTimeoutFunction) remove_connection_timeout,
305 dbus_connection_disconnect (connection);
310 /* Setup the connection with the dispatcher */
311 if (!bus_dispatch_add_connection (connection))
313 dbus_connection_disconnect (connection);
317 dbus_connection_ref (connection);
324 * Calls function on each connection; if the function returns
325 * #FALSE, stops iterating.
327 * @param connections the connections object
328 * @param function the function
329 * @param data data to pass to it as a second arg
332 bus_connections_foreach (BusConnections *connections,
333 BusConnectionForeachFunction function,
338 link = _dbus_list_get_first_link (&connections->list);
341 DBusConnection *connection = link->data;
342 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
344 if (!(* function) (connection, data))
352 bus_connections_get_context (BusConnections *connections)
354 return connections->context;
358 bus_connection_get_context (DBusConnection *connection)
360 BusConnectionData *d;
362 d = BUS_CONNECTION_DATA (connection);
364 _dbus_assert (d != NULL);
366 return d->connections->context;
370 bus_connection_get_connections (DBusConnection *connection)
372 BusConnectionData *d;
374 d = BUS_CONNECTION_DATA (connection);
376 _dbus_assert (d != NULL);
378 return d->connections;
382 bus_connection_get_registry (DBusConnection *connection)
384 BusConnectionData *d;
386 d = BUS_CONNECTION_DATA (connection);
388 _dbus_assert (d != NULL);
390 return bus_context_get_registry (d->connections->context);
394 bus_connection_get_activation (DBusConnection *connection)
396 BusConnectionData *d;
398 d = BUS_CONNECTION_DATA (connection);
400 _dbus_assert (d != NULL);
402 return bus_context_get_activation (d->connections->context);
406 * Checks whether the connection is registered with the message bus.
408 * @param connection the connection
409 * @returns #TRUE if we're an active message bus participant
412 bus_connection_is_active (DBusConnection *connection)
414 BusConnectionData *d;
416 d = BUS_CONNECTION_DATA (connection);
418 return d != NULL && d->name != NULL;
422 bus_connection_preallocate_oom_error (DBusConnection *connection)
424 DBusMessage *message;
425 DBusPreallocatedSend *preallocated;
426 BusConnectionData *d;
428 d = BUS_CONNECTION_DATA (connection);
430 _dbus_assert (d != NULL);
432 if (d->oom_preallocated != NULL)
435 preallocated = dbus_connection_preallocate_send (connection);
436 if (preallocated == NULL)
439 message = dbus_message_new (DBUS_SERVICE_DBUS,
440 DBUS_ERROR_NO_MEMORY);
443 dbus_connection_free_preallocated_send (connection, preallocated);
447 dbus_message_set_is_error (message, TRUE);
449 /* set reply serial to placeholder value just so space is already allocated
452 if (!dbus_message_set_reply_serial (message, 14))
454 dbus_connection_free_preallocated_send (connection, preallocated);
455 dbus_message_unref (message);
459 d->oom_message = message;
460 d->oom_preallocated = preallocated;
466 bus_connection_send_oom_error (DBusConnection *connection,
467 DBusMessage *in_reply_to)
469 BusConnectionData *d;
471 d = BUS_CONNECTION_DATA (connection);
473 _dbus_assert (d != NULL);
474 _dbus_assert (d->oom_message != NULL);
476 /* should always succeed since we set it to a placeholder earlier */
477 if (!dbus_message_set_reply_serial (d->oom_message,
478 dbus_message_get_serial (in_reply_to)))
479 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
481 dbus_connection_send_preallocated (connection, d->oom_preallocated,
482 d->oom_message, NULL);
484 dbus_message_unref (d->oom_message);
485 d->oom_message = NULL;
486 d->oom_preallocated = NULL;
490 bus_connection_add_owned_service (DBusConnection *connection,
493 BusConnectionData *d;
495 d = BUS_CONNECTION_DATA (connection);
496 _dbus_assert (d != NULL);
498 if (!_dbus_list_append (&d->services_owned,
506 bus_connection_remove_owned_service (DBusConnection *connection,
509 BusConnectionData *d;
511 d = BUS_CONNECTION_DATA (connection);
512 _dbus_assert (d != NULL);
514 _dbus_list_remove_last (&d->services_owned, service);
518 bus_connection_set_name (DBusConnection *connection,
519 const DBusString *name)
522 BusConnectionData *d;
524 d = BUS_CONNECTION_DATA (connection);
525 _dbus_assert (d != NULL);
526 _dbus_assert (d->name == NULL);
528 _dbus_string_get_const_data (name, &c_name);
530 d->name = _dbus_strdup (c_name);
539 bus_connection_get_name (DBusConnection *connection)
541 BusConnectionData *d;
543 d = BUS_CONNECTION_DATA (connection);
544 _dbus_assert (d != NULL);
551 BusTransaction *transaction;
552 DBusMessage *message;
553 DBusPreallocatedSend *preallocated;
556 struct BusTransaction
558 DBusList *connections;
563 message_to_send_free (DBusConnection *connection,
564 MessageToSend *to_send)
566 if (to_send->message)
567 dbus_message_unref (to_send->message);
569 if (to_send->preallocated)
570 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
576 bus_transaction_new (BusContext *context)
578 BusTransaction *transaction;
580 transaction = dbus_new0 (BusTransaction, 1);
581 if (transaction == NULL)
584 transaction->context = context;
590 bus_transaction_get_context (BusTransaction *transaction)
592 return transaction->context;
596 bus_transaction_get_connections (BusTransaction *transaction)
598 return bus_context_get_connections (transaction->context);
602 bus_transaction_send_message (BusTransaction *transaction,
603 DBusConnection *connection,
604 DBusMessage *message)
606 MessageToSend *to_send;
607 BusConnectionData *d;
610 _dbus_verbose (" trying to add message %s to transaction\n",
611 dbus_message_get_name (message));
613 if (!dbus_connection_get_is_connected (connection))
614 return TRUE; /* silently ignore disconnected connections */
616 d = BUS_CONNECTION_DATA (connection);
617 _dbus_assert (d != NULL);
619 to_send = dbus_new (MessageToSend, 1);
625 to_send->preallocated = dbus_connection_preallocate_send (connection);
626 if (to_send->preallocated == NULL)
632 dbus_message_ref (message);
633 to_send->message = message;
634 to_send->transaction = transaction;
636 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
638 message_to_send_free (connection, to_send);
642 /* See if we already had this connection in the list
643 * for this transaction. If we have a pending message,
644 * then we should already be in transaction->connections
646 link = _dbus_list_get_first_link (&d->transaction_messages);
647 _dbus_assert (link->data == to_send);
648 link = _dbus_list_get_next_link (&d->transaction_messages, link);
651 MessageToSend *m = link->data;
652 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
654 if (m->transaction == transaction)
662 if (!_dbus_list_prepend (&transaction->connections, connection))
664 _dbus_list_remove (&d->transaction_messages, to_send);
665 message_to_send_free (connection, to_send);
674 connection_cancel_transaction (DBusConnection *connection,
675 BusTransaction *transaction)
678 BusConnectionData *d;
680 d = BUS_CONNECTION_DATA (connection);
681 _dbus_assert (d != NULL);
683 link = _dbus_list_get_first_link (&d->transaction_messages);
686 MessageToSend *m = link->data;
687 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
689 if (m->transaction == transaction)
691 _dbus_list_remove_link (&d->transaction_messages,
694 message_to_send_free (connection, m);
702 bus_transaction_cancel_and_free (BusTransaction *transaction)
704 DBusConnection *connection;
706 while ((connection = _dbus_list_pop_first (&transaction->connections)))
707 connection_cancel_transaction (connection, transaction);
709 _dbus_assert (transaction->connections == NULL);
711 dbus_free (transaction);
715 connection_execute_transaction (DBusConnection *connection,
716 BusTransaction *transaction)
719 BusConnectionData *d;
721 d = BUS_CONNECTION_DATA (connection);
722 _dbus_assert (d != NULL);
724 /* Send the queue in order (FIFO) */
725 link = _dbus_list_get_last_link (&d->transaction_messages);
728 MessageToSend *m = link->data;
729 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
731 if (m->transaction == transaction)
733 _dbus_list_remove_link (&d->transaction_messages,
736 dbus_connection_send_preallocated (connection,
741 m->preallocated = NULL; /* so we don't double-free it */
743 message_to_send_free (connection, m);
751 bus_transaction_execute_and_free (BusTransaction *transaction)
753 /* For each connection in transaction->connections
756 DBusConnection *connection;
758 while ((connection = _dbus_list_pop_first (&transaction->connections)))
759 connection_execute_transaction (connection, transaction);
761 _dbus_assert (transaction->connections == NULL);
763 dbus_free (transaction);
767 bus_connection_remove_transactions (DBusConnection *connection)
769 MessageToSend *to_send;
770 BusConnectionData *d;
772 d = BUS_CONNECTION_DATA (connection);
773 _dbus_assert (d != NULL);
775 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
777 /* only has an effect for the first MessageToSend listing this transaction */
778 _dbus_list_remove (&to_send->transaction->connections,
781 _dbus_list_remove (&d->transaction_messages, to_send);
782 message_to_send_free (connection, to_send);
787 * Converts the DBusError to a message reply
790 bus_transaction_send_error_reply (BusTransaction *transaction,
791 DBusConnection *connection,
792 const DBusError *error,
793 DBusMessage *in_reply_to)
797 _dbus_assert (error != NULL);
798 _DBUS_ASSERT_ERROR_IS_SET (error);
800 _dbus_verbose (" trying to add error %s to transaction\n",
803 reply = dbus_message_new_error_reply (in_reply_to,
809 if (!bus_transaction_send_message (transaction, connection, reply))
811 dbus_message_unref (reply);