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;
144 dbus_connection_ref (connection);
146 retval = dbus_connection_handle_watch (connection, watch, condition);
148 bus_connection_dispatch_all_messages (connection);
150 dbus_connection_unref (connection);
156 add_connection_watch (DBusWatch *watch,
157 DBusConnection *connection)
159 return bus_loop_add_watch (watch, connection_watch_callback, connection,
164 remove_connection_watch (DBusWatch *watch,
165 DBusConnection *connection)
167 bus_loop_remove_watch (watch, connection_watch_callback, connection);
171 connection_timeout_callback (DBusTimeout *timeout,
174 DBusConnection *connection = data;
176 dbus_connection_ref (connection);
178 /* can return FALSE on OOM but we just let it fire again later */
179 dbus_timeout_handle (timeout);
181 bus_connection_dispatch_all_messages (connection);
183 dbus_connection_unref (connection);
187 add_connection_timeout (DBusTimeout *timeout,
188 DBusConnection *connection)
190 return bus_loop_add_timeout (timeout, connection_timeout_callback, connection, NULL);
194 remove_connection_timeout (DBusTimeout *timeout,
195 DBusConnection *connection)
197 bus_loop_remove_timeout (timeout, connection_timeout_callback, connection);
201 free_connection_data (void *data)
203 BusConnectionData *d = data;
205 /* services_owned should be NULL since we should be disconnected */
206 _dbus_assert (d->services_owned == NULL);
208 _dbus_assert (d->transaction_messages == NULL);
210 if (d->oom_preallocated)
211 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
213 dbus_message_unref (d->oom_message);
221 bus_connections_new (BusContext *context)
223 BusConnections *connections;
225 if (connection_data_slot < 0)
227 connection_data_slot = dbus_connection_allocate_data_slot ();
229 if (connection_data_slot < 0)
233 connections = dbus_new0 (BusConnections, 1);
234 if (connections == NULL)
237 connections->refcount = 1;
238 connections->context = context;
244 bus_connections_ref (BusConnections *connections)
246 _dbus_assert (connections->refcount > 0);
247 connections->refcount += 1;
251 bus_connections_unref (BusConnections *connections)
253 _dbus_assert (connections->refcount > 0);
254 connections->refcount -= 1;
255 if (connections->refcount == 0)
257 while (connections->list != NULL)
259 DBusConnection *connection;
261 connection = connections->list->data;
263 dbus_connection_ref (connection);
264 dbus_connection_disconnect (connection);
265 bus_connection_disconnected (connection);
266 dbus_connection_unref (connection);
269 _dbus_list_clear (&connections->list);
271 dbus_free (connections);
276 bus_connections_setup_connection (BusConnections *connections,
277 DBusConnection *connection)
279 BusConnectionData *d;
282 d = dbus_new0 (BusConnectionData, 1);
287 d->connections = connections;
288 d->connection = connection;
290 if (!dbus_connection_set_data (connection,
291 connection_data_slot,
292 d, free_connection_data))
300 if (!dbus_connection_set_watch_functions (connection,
301 (DBusAddWatchFunction) add_connection_watch,
302 (DBusRemoveWatchFunction) remove_connection_watch,
308 if (!dbus_connection_set_timeout_functions (connection,
309 (DBusAddTimeoutFunction) add_connection_timeout,
310 (DBusRemoveTimeoutFunction) remove_connection_timeout,
316 /* Setup the connection with the dispatcher */
317 if (!bus_dispatch_add_connection (connection))
320 if (!_dbus_list_append (&connections->list, connection))
322 bus_dispatch_remove_connection (connection);
326 dbus_connection_ref (connection);
332 if (!dbus_connection_set_watch_functions (connection,
336 _dbus_assert_not_reached ("setting watch functions to NULL failed");
338 if (!dbus_connection_set_timeout_functions (connection,
342 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
350 * Calls function on each connection; if the function returns
351 * #FALSE, stops iterating.
353 * @param connections the connections object
354 * @param function the function
355 * @param data data to pass to it as a second arg
358 bus_connections_foreach (BusConnections *connections,
359 BusConnectionForeachFunction function,
364 link = _dbus_list_get_first_link (&connections->list);
367 DBusConnection *connection = link->data;
368 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
370 if (!(* function) (connection, data))
378 bus_connections_get_context (BusConnections *connections)
380 return connections->context;
384 bus_connection_get_context (DBusConnection *connection)
386 BusConnectionData *d;
388 d = BUS_CONNECTION_DATA (connection);
390 _dbus_assert (d != NULL);
392 return d->connections->context;
396 bus_connection_get_connections (DBusConnection *connection)
398 BusConnectionData *d;
400 d = BUS_CONNECTION_DATA (connection);
402 _dbus_assert (d != NULL);
404 return d->connections;
408 bus_connection_get_registry (DBusConnection *connection)
410 BusConnectionData *d;
412 d = BUS_CONNECTION_DATA (connection);
414 _dbus_assert (d != NULL);
416 return bus_context_get_registry (d->connections->context);
420 bus_connection_get_activation (DBusConnection *connection)
422 BusConnectionData *d;
424 d = BUS_CONNECTION_DATA (connection);
426 _dbus_assert (d != NULL);
428 return bus_context_get_activation (d->connections->context);
432 * Checks whether the connection is registered with the message bus.
434 * @param connection the connection
435 * @returns #TRUE if we're an active message bus participant
438 bus_connection_is_active (DBusConnection *connection)
440 BusConnectionData *d;
442 d = BUS_CONNECTION_DATA (connection);
444 return d != NULL && d->name != NULL;
448 bus_connection_preallocate_oom_error (DBusConnection *connection)
450 DBusMessage *message;
451 DBusPreallocatedSend *preallocated;
452 BusConnectionData *d;
454 d = BUS_CONNECTION_DATA (connection);
456 _dbus_assert (d != NULL);
458 if (d->oom_preallocated != NULL)
461 preallocated = dbus_connection_preallocate_send (connection);
462 if (preallocated == NULL)
465 message = dbus_message_new (DBUS_SERVICE_DBUS,
466 DBUS_ERROR_NO_MEMORY);
469 dbus_connection_free_preallocated_send (connection, preallocated);
473 dbus_message_set_is_error (message, TRUE);
475 /* set reply serial to placeholder value just so space is already allocated
478 if (!dbus_message_set_reply_serial (message, 14))
480 dbus_connection_free_preallocated_send (connection, preallocated);
481 dbus_message_unref (message);
485 d->oom_message = message;
486 d->oom_preallocated = preallocated;
492 bus_connection_send_oom_error (DBusConnection *connection,
493 DBusMessage *in_reply_to)
495 BusConnectionData *d;
497 d = BUS_CONNECTION_DATA (connection);
499 _dbus_assert (d != NULL);
500 _dbus_assert (d->oom_message != NULL);
502 /* should always succeed since we set it to a placeholder earlier */
503 if (!dbus_message_set_reply_serial (d->oom_message,
504 dbus_message_get_serial (in_reply_to)))
505 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
507 dbus_connection_send_preallocated (connection, d->oom_preallocated,
508 d->oom_message, NULL);
510 dbus_message_unref (d->oom_message);
511 d->oom_message = NULL;
512 d->oom_preallocated = NULL;
516 bus_connection_add_owned_service (DBusConnection *connection,
519 BusConnectionData *d;
521 d = BUS_CONNECTION_DATA (connection);
522 _dbus_assert (d != NULL);
524 if (!_dbus_list_append (&d->services_owned,
532 bus_connection_remove_owned_service (DBusConnection *connection,
535 BusConnectionData *d;
537 d = BUS_CONNECTION_DATA (connection);
538 _dbus_assert (d != NULL);
540 _dbus_list_remove_last (&d->services_owned, service);
544 bus_connection_set_name (DBusConnection *connection,
545 const DBusString *name)
548 BusConnectionData *d;
550 d = BUS_CONNECTION_DATA (connection);
551 _dbus_assert (d != NULL);
552 _dbus_assert (d->name == NULL);
554 _dbus_string_get_const_data (name, &c_name);
556 d->name = _dbus_strdup (c_name);
561 _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
567 bus_connection_get_name (DBusConnection *connection)
569 BusConnectionData *d;
571 d = BUS_CONNECTION_DATA (connection);
572 _dbus_assert (d != NULL);
579 BusTransaction *transaction;
580 DBusMessage *message;
581 DBusPreallocatedSend *preallocated;
584 struct BusTransaction
586 DBusList *connections;
591 message_to_send_free (DBusConnection *connection,
592 MessageToSend *to_send)
594 if (to_send->message)
595 dbus_message_unref (to_send->message);
597 if (to_send->preallocated)
598 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
604 bus_transaction_new (BusContext *context)
606 BusTransaction *transaction;
608 transaction = dbus_new0 (BusTransaction, 1);
609 if (transaction == NULL)
612 transaction->context = context;
618 bus_transaction_get_context (BusTransaction *transaction)
620 return transaction->context;
624 bus_transaction_get_connections (BusTransaction *transaction)
626 return bus_context_get_connections (transaction->context);
630 bus_transaction_send_message (BusTransaction *transaction,
631 DBusConnection *connection,
632 DBusMessage *message)
634 MessageToSend *to_send;
635 BusConnectionData *d;
638 _dbus_verbose (" trying to add message %s to transaction%s\n",
639 dbus_message_get_name (message),
640 dbus_connection_get_is_connected (connection) ?
641 "" : " (disconnected)");
643 if (!dbus_connection_get_is_connected (connection))
644 return TRUE; /* silently ignore disconnected connections */
646 d = BUS_CONNECTION_DATA (connection);
647 _dbus_assert (d != NULL);
649 to_send = dbus_new (MessageToSend, 1);
655 to_send->preallocated = dbus_connection_preallocate_send (connection);
656 if (to_send->preallocated == NULL)
662 dbus_message_ref (message);
663 to_send->message = message;
664 to_send->transaction = transaction;
666 _dbus_verbose ("about to prepend message\n");
668 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
670 message_to_send_free (connection, to_send);
674 _dbus_verbose ("prepended message\n");
676 /* See if we already had this connection in the list
677 * for this transaction. If we have a pending message,
678 * then we should already be in transaction->connections
680 link = _dbus_list_get_first_link (&d->transaction_messages);
681 _dbus_assert (link->data == to_send);
682 link = _dbus_list_get_next_link (&d->transaction_messages, link);
685 MessageToSend *m = link->data;
686 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
688 if (m->transaction == transaction)
696 if (!_dbus_list_prepend (&transaction->connections, connection))
698 _dbus_list_remove (&d->transaction_messages, to_send);
699 message_to_send_free (connection, to_send);
708 connection_cancel_transaction (DBusConnection *connection,
709 BusTransaction *transaction)
712 BusConnectionData *d;
714 d = BUS_CONNECTION_DATA (connection);
715 _dbus_assert (d != NULL);
717 link = _dbus_list_get_first_link (&d->transaction_messages);
720 MessageToSend *m = link->data;
721 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
723 if (m->transaction == transaction)
725 _dbus_list_remove_link (&d->transaction_messages,
728 message_to_send_free (connection, m);
736 bus_transaction_cancel_and_free (BusTransaction *transaction)
738 DBusConnection *connection;
740 _dbus_verbose ("TRANSACTION: cancelled\n");
742 while ((connection = _dbus_list_pop_first (&transaction->connections)))
743 connection_cancel_transaction (connection, transaction);
745 _dbus_assert (transaction->connections == NULL);
747 dbus_free (transaction);
751 connection_execute_transaction (DBusConnection *connection,
752 BusTransaction *transaction)
755 BusConnectionData *d;
757 d = BUS_CONNECTION_DATA (connection);
758 _dbus_assert (d != NULL);
760 /* Send the queue in order (FIFO) */
761 link = _dbus_list_get_last_link (&d->transaction_messages);
764 MessageToSend *m = link->data;
765 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
767 if (m->transaction == transaction)
769 _dbus_list_remove_link (&d->transaction_messages,
772 dbus_connection_send_preallocated (connection,
777 m->preallocated = NULL; /* so we don't double-free it */
779 message_to_send_free (connection, m);
787 bus_transaction_execute_and_free (BusTransaction *transaction)
789 /* For each connection in transaction->connections
792 DBusConnection *connection;
794 _dbus_verbose ("TRANSACTION: executing\n");
796 while ((connection = _dbus_list_pop_first (&transaction->connections)))
797 connection_execute_transaction (connection, transaction);
799 _dbus_assert (transaction->connections == NULL);
801 dbus_free (transaction);
805 bus_connection_remove_transactions (DBusConnection *connection)
807 MessageToSend *to_send;
808 BusConnectionData *d;
810 d = BUS_CONNECTION_DATA (connection);
811 _dbus_assert (d != NULL);
813 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
815 /* only has an effect for the first MessageToSend listing this transaction */
816 _dbus_list_remove (&to_send->transaction->connections,
819 _dbus_list_remove (&d->transaction_messages, to_send);
820 message_to_send_free (connection, to_send);
825 * Converts the DBusError to a message reply
828 bus_transaction_send_error_reply (BusTransaction *transaction,
829 DBusConnection *connection,
830 const DBusError *error,
831 DBusMessage *in_reply_to)
835 _dbus_assert (error != NULL);
836 _DBUS_ASSERT_ERROR_IS_SET (error);
838 reply = dbus_message_new_error_reply (in_reply_to,
844 if (!bus_transaction_send_message (transaction, connection, reply))
846 dbus_message_unref (reply);