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 _dbus_warn ("Disconnected\n");
62 d = BUS_CONNECTION_DATA (connection);
63 _dbus_assert (d != NULL);
65 /* Drop any service ownership. FIXME Unfortunately, this requires
66 * memory allocation and there doesn't seem to be a good way to
67 * handle it other than sleeping; we can't "fail" the operation of
68 * disconnecting a client, and preallocating a broadcast "service is
69 * now gone" message for every client-service pair seems kind of
70 * involved. Probably we need to do that though, and also
71 * extend BusTransaction to be able to revert generic
72 * stuff, not just sending a message (so we can e.g. revert
73 * removal of service owners).
76 BusTransaction *transaction;
79 dbus_error_init (&error);
82 while (transaction == NULL)
84 transaction = bus_transaction_new (d->connections->context);
85 bus_wait_for_memory ();
88 while ((service = _dbus_list_get_last (&d->services_owned)))
91 if (!bus_service_remove_owner (service, connection,
94 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
96 dbus_error_free (&error);
97 bus_wait_for_memory ();
101 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
105 bus_transaction_execute_and_free (transaction);
108 bus_dispatch_remove_connection (connection);
110 /* no more watching */
111 if (!dbus_connection_set_watch_functions (connection,
115 _dbus_assert_not_reached ("setting watch functions to NULL failed");
117 if (!dbus_connection_set_timeout_functions (connection,
121 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
123 bus_connection_remove_transactions (connection);
125 _dbus_list_remove (&d->connections->list, connection);
127 /* frees "d" as side effect */
128 dbus_connection_set_data (connection,
129 connection_data_slot,
132 dbus_connection_unref (connection);
136 connection_watch_callback (DBusWatch *watch,
137 unsigned int condition,
140 DBusConnection *connection = data;
142 dbus_connection_ref (connection);
144 dbus_connection_handle_watch (connection, watch, condition);
146 while (dbus_connection_dispatch_message (connection))
148 dbus_connection_unref (connection);
152 add_connection_watch (DBusWatch *watch,
153 DBusConnection *connection)
155 return bus_loop_add_watch (watch, connection_watch_callback, connection,
160 remove_connection_watch (DBusWatch *watch,
161 DBusConnection *connection)
163 bus_loop_remove_watch (watch, connection_watch_callback, connection);
167 connection_timeout_callback (DBusTimeout *timeout,
170 DBusConnection *connection = data;
172 dbus_connection_ref (connection);
174 dbus_timeout_handle (timeout);
176 while (dbus_connection_dispatch_message (connection))
178 dbus_connection_unref (connection);
182 add_connection_timeout (DBusTimeout *timeout,
183 DBusConnection *connection)
185 return bus_loop_add_timeout (timeout, connection_timeout_callback, connection, NULL);
189 remove_connection_timeout (DBusTimeout *timeout,
190 DBusConnection *connection)
192 bus_loop_remove_timeout (timeout, connection_timeout_callback, connection);
196 free_connection_data (void *data)
198 BusConnectionData *d = data;
200 /* services_owned should be NULL since we should be disconnected */
201 _dbus_assert (d->services_owned == NULL);
203 _dbus_assert (d->transaction_messages == NULL);
205 if (d->oom_preallocated)
206 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
208 dbus_message_unref (d->oom_message);
216 bus_connections_new (BusContext *context)
218 BusConnections *connections;
220 if (connection_data_slot < 0)
222 connection_data_slot = dbus_connection_allocate_data_slot ();
224 if (connection_data_slot < 0)
228 connections = dbus_new0 (BusConnections, 1);
229 if (connections == NULL)
232 connections->refcount = 1;
233 connections->context = context;
239 bus_connections_ref (BusConnections *connections)
241 _dbus_assert (connections->refcount > 0);
242 connections->refcount += 1;
246 bus_connections_unref (BusConnections *connections)
248 _dbus_assert (connections->refcount > 0);
249 connections->refcount -= 1;
250 if (connections->refcount == 0)
252 /* FIXME free each connection... */
253 _dbus_assert_not_reached ("shutting down connections not implemented");
255 _dbus_list_clear (&connections->list);
257 dbus_free (connections);
262 bus_connections_setup_connection (BusConnections *connections,
263 DBusConnection *connection)
265 BusConnectionData *d;
267 d = dbus_new0 (BusConnectionData, 1);
272 d->connections = connections;
273 d->connection = connection;
275 if (!dbus_connection_set_data (connection,
276 connection_data_slot,
277 d, free_connection_data))
283 if (!_dbus_list_append (&connections->list, connection))
285 /* this will free our data when connection gets finalized */
286 dbus_connection_disconnect (connection);
290 if (!dbus_connection_set_watch_functions (connection,
291 (DBusAddWatchFunction) add_connection_watch,
292 (DBusRemoveWatchFunction) remove_connection_watch,
296 dbus_connection_disconnect (connection);
300 if (!dbus_connection_set_timeout_functions (connection,
301 (DBusAddTimeoutFunction) add_connection_timeout,
302 (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 /* set reply serial to placeholder value just so space is already allocated
450 if (!dbus_message_set_reply_serial (message, 14))
452 dbus_connection_free_preallocated_send (connection, preallocated);
453 dbus_message_unref (message);
457 d->oom_message = message;
458 d->oom_preallocated = preallocated;
464 bus_connection_send_oom_error (DBusConnection *connection,
465 DBusMessage *in_reply_to)
467 BusConnectionData *d;
469 d = BUS_CONNECTION_DATA (connection);
471 _dbus_assert (d != NULL);
472 _dbus_assert (d->oom_message != NULL);
474 /* should always succeed since we set it to a placeholder earlier */
475 if (!dbus_message_set_reply_serial (d->oom_message,
476 dbus_message_get_serial (in_reply_to)))
477 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
479 dbus_connection_send_preallocated (connection, d->oom_preallocated,
480 d->oom_message, NULL);
482 dbus_message_unref (d->oom_message);
483 d->oom_message = NULL;
484 d->oom_preallocated = NULL;
488 bus_connection_add_owned_service (DBusConnection *connection,
491 BusConnectionData *d;
493 d = BUS_CONNECTION_DATA (connection);
494 _dbus_assert (d != NULL);
496 if (!_dbus_list_append (&d->services_owned,
504 bus_connection_remove_owned_service (DBusConnection *connection,
507 BusConnectionData *d;
509 d = BUS_CONNECTION_DATA (connection);
510 _dbus_assert (d != NULL);
512 _dbus_list_remove_last (&d->services_owned, service);
516 bus_connection_set_name (DBusConnection *connection,
517 const DBusString *name)
520 BusConnectionData *d;
522 d = BUS_CONNECTION_DATA (connection);
523 _dbus_assert (d != NULL);
524 _dbus_assert (d->name == NULL);
526 _dbus_string_get_const_data (name, &c_name);
528 d->name = _dbus_strdup (c_name);
537 bus_connection_get_name (DBusConnection *connection)
539 BusConnectionData *d;
541 d = BUS_CONNECTION_DATA (connection);
542 _dbus_assert (d != NULL);
549 BusTransaction *transaction;
550 DBusMessage *message;
551 DBusPreallocatedSend *preallocated;
554 struct BusTransaction
556 DBusList *connections;
561 message_to_send_free (DBusConnection *connection,
562 MessageToSend *to_send)
564 if (to_send->message)
565 dbus_message_unref (to_send->message);
567 if (to_send->preallocated)
568 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
574 bus_transaction_new (BusContext *context)
576 BusTransaction *transaction;
578 transaction = dbus_new0 (BusTransaction, 1);
579 if (transaction == NULL)
582 transaction->context = context;
588 bus_transaction_get_context (BusTransaction *transaction)
590 return transaction->context;
594 bus_transaction_get_connections (BusTransaction *transaction)
596 return bus_context_get_connections (transaction->context);
600 bus_transaction_send_message (BusTransaction *transaction,
601 DBusConnection *connection,
602 DBusMessage *message)
604 MessageToSend *to_send;
605 BusConnectionData *d;
608 if (!dbus_connection_get_is_connected (connection))
609 return TRUE; /* silently ignore disconnected connections */
611 d = BUS_CONNECTION_DATA (connection);
612 _dbus_assert (d != NULL);
614 to_send = dbus_new (MessageToSend, 1);
620 to_send->preallocated = dbus_connection_preallocate_send (connection);
621 if (to_send->preallocated == NULL)
627 dbus_message_ref (message);
628 to_send->message = message;
629 to_send->transaction = transaction;
631 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
633 message_to_send_free (connection, to_send);
637 /* See if we already had this connection in the list
638 * for this transaction. If we have a pending message,
639 * then we should already be in transaction->connections
641 link = _dbus_list_get_first_link (&d->transaction_messages);
642 _dbus_assert (link->data == to_send);
643 link = _dbus_list_get_next_link (&d->transaction_messages, link);
646 MessageToSend *m = link->data;
647 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
649 if (m->transaction == transaction)
657 if (!_dbus_list_prepend (&transaction->connections, connection))
659 _dbus_list_remove (&d->transaction_messages, to_send);
660 message_to_send_free (connection, to_send);
669 connection_cancel_transaction (DBusConnection *connection,
670 BusTransaction *transaction)
673 BusConnectionData *d;
675 d = BUS_CONNECTION_DATA (connection);
676 _dbus_assert (d != NULL);
678 link = _dbus_list_get_first_link (&d->transaction_messages);
681 MessageToSend *m = link->data;
682 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
684 if (m->transaction == transaction)
686 _dbus_list_remove_link (&d->transaction_messages,
689 message_to_send_free (connection, m);
697 bus_transaction_cancel_and_free (BusTransaction *transaction)
699 DBusConnection *connection;
701 while ((connection = _dbus_list_pop_first (&transaction->connections)))
702 connection_cancel_transaction (connection, transaction);
704 _dbus_assert (transaction->connections == NULL);
706 dbus_free (transaction);
710 connection_execute_transaction (DBusConnection *connection,
711 BusTransaction *transaction)
714 BusConnectionData *d;
716 d = BUS_CONNECTION_DATA (connection);
717 _dbus_assert (d != NULL);
719 /* Send the queue in order (FIFO) */
720 link = _dbus_list_get_last_link (&d->transaction_messages);
723 MessageToSend *m = link->data;
724 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
726 if (m->transaction == transaction)
728 _dbus_list_remove_link (&d->transaction_messages,
731 dbus_connection_send_preallocated (connection,
736 m->preallocated = NULL; /* so we don't double-free it */
738 message_to_send_free (connection, m);
746 bus_transaction_execute_and_free (BusTransaction *transaction)
748 /* For each connection in transaction->connections
751 DBusConnection *connection;
753 while ((connection = _dbus_list_pop_first (&transaction->connections)))
754 connection_execute_transaction (connection, transaction);
756 _dbus_assert (transaction->connections == NULL);
758 dbus_free (transaction);
762 bus_connection_remove_transactions (DBusConnection *connection)
764 MessageToSend *to_send;
765 BusConnectionData *d;
767 d = BUS_CONNECTION_DATA (connection);
768 _dbus_assert (d != NULL);
770 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
772 /* only has an effect for the first MessageToSend listing this transaction */
773 _dbus_list_remove (&to_send->transaction->connections,
776 _dbus_list_remove (&d->transaction_messages, to_send);
777 message_to_send_free (connection, to_send);
782 * Converts the DBusError to a message reply
785 bus_transaction_send_error_reply (BusTransaction *transaction,
786 DBusConnection *connection,
787 const DBusError *error,
788 DBusMessage *in_reply_to)
792 _dbus_assert (error != NULL);
793 _DBUS_ASSERT_ERROR_IS_SET (error);
795 reply = dbus_message_new_error_reply (in_reply_to,
801 if (!bus_transaction_send_message (transaction, connection, reply))
803 dbus_message_unref (reply);