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 dbus_connection_set_watch_functions (connection,
116 bus_connection_remove_transactions (connection);
118 _dbus_list_remove (&d->connections->list, connection);
120 /* frees "d" as side effect */
121 dbus_connection_set_data (connection,
122 connection_data_slot,
125 dbus_connection_unref (connection);
129 connection_watch_callback (DBusWatch *watch,
130 unsigned int condition,
133 DBusConnection *connection = data;
135 dbus_connection_ref (connection);
137 dbus_connection_handle_watch (connection, watch, condition);
139 while (dbus_connection_dispatch_message (connection));
140 dbus_connection_unref (connection);
144 add_connection_watch (DBusWatch *watch,
145 DBusConnection *connection)
147 bus_loop_add_watch (watch, connection_watch_callback, connection,
152 remove_connection_watch (DBusWatch *watch,
153 DBusConnection *connection)
155 bus_loop_remove_watch (watch, connection_watch_callback, connection);
159 free_connection_data (void *data)
161 BusConnectionData *d = data;
163 /* services_owned should be NULL since we should be disconnected */
164 _dbus_assert (d->services_owned == NULL);
166 _dbus_assert (d->transaction_messages == NULL);
168 if (d->oom_preallocated)
169 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
171 dbus_message_unref (d->oom_message);
179 bus_connections_new (BusContext *context)
181 BusConnections *connections;
183 if (connection_data_slot < 0)
185 connection_data_slot = dbus_connection_allocate_data_slot ();
187 if (connection_data_slot < 0)
191 connections = dbus_new0 (BusConnections, 1);
192 if (connections == NULL)
195 connections->refcount = 1;
196 connections->context = context;
202 bus_connections_ref (BusConnections *connections)
204 _dbus_assert (connections->refcount > 0);
205 connections->refcount += 1;
209 bus_connections_unref (BusConnections *connections)
211 _dbus_assert (connections->refcount > 0);
212 connections->refcount -= 1;
213 if (connections->refcount == 0)
215 /* FIXME free each connection... */
216 _dbus_assert_not_reached ("shutting down connections not implemented");
218 _dbus_list_clear (&connections->list);
220 dbus_free (connections);
225 bus_connections_setup_connection (BusConnections *connections,
226 DBusConnection *connection)
228 BusConnectionData *d;
230 d = dbus_new0 (BusConnectionData, 1);
235 d->connections = connections;
236 d->connection = connection;
238 if (!dbus_connection_set_data (connection,
239 connection_data_slot,
240 d, free_connection_data))
246 if (!_dbus_list_append (&connections->list, connection))
248 /* this will free our data when connection gets finalized */
249 dbus_connection_disconnect (connection);
253 dbus_connection_ref (connection);
255 dbus_connection_set_watch_functions (connection,
256 (DBusAddWatchFunction) add_connection_watch,
257 (DBusRemoveWatchFunction) remove_connection_watch,
261 /* Setup the connection with the dispatcher */
262 if (!bus_dispatch_add_connection (connection))
270 * Calls function on each connection; if the function returns
271 * #FALSE, stops iterating.
273 * @param connections the connections object
274 * @param function the function
275 * @param data data to pass to it as a second arg
278 bus_connections_foreach (BusConnections *connections,
279 BusConnectionForeachFunction function,
284 link = _dbus_list_get_first_link (&connections->list);
287 DBusConnection *connection = link->data;
288 DBusList *next = _dbus_list_get_next_link (&connections->list, link);
290 if (!(* function) (connection, data))
298 bus_connections_get_context (BusConnections *connections)
300 return connections->context;
304 bus_connection_get_context (DBusConnection *connection)
306 BusConnectionData *d;
308 d = BUS_CONNECTION_DATA (connection);
310 _dbus_assert (d != NULL);
312 return d->connections->context;
316 bus_connection_get_connections (DBusConnection *connection)
318 BusConnectionData *d;
320 d = BUS_CONNECTION_DATA (connection);
322 _dbus_assert (d != NULL);
324 return d->connections;
328 bus_connection_get_registry (DBusConnection *connection)
330 BusConnectionData *d;
332 d = BUS_CONNECTION_DATA (connection);
334 _dbus_assert (d != NULL);
336 return bus_context_get_registry (d->connections->context);
340 bus_connection_get_activation (DBusConnection *connection)
342 BusConnectionData *d;
344 d = BUS_CONNECTION_DATA (connection);
346 _dbus_assert (d != NULL);
348 return bus_context_get_activation (d->connections->context);
352 * Checks whether the connection is registered with the message bus.
354 * @param connection the connection
355 * @returns #TRUE if we're an active message bus participant
358 bus_connection_is_active (DBusConnection *connection)
360 BusConnectionData *d;
362 d = BUS_CONNECTION_DATA (connection);
364 return d != NULL && d->name != NULL;
368 bus_connection_preallocate_oom_error (DBusConnection *connection)
370 DBusMessage *message;
371 DBusPreallocatedSend *preallocated;
372 BusConnectionData *d;
374 d = BUS_CONNECTION_DATA (connection);
376 _dbus_assert (d != NULL);
378 if (d->oom_preallocated != NULL)
381 preallocated = dbus_connection_preallocate_send (connection);
382 if (preallocated == NULL)
385 message = dbus_message_new (DBUS_SERVICE_DBUS,
386 DBUS_ERROR_NO_MEMORY);
389 dbus_connection_free_preallocated_send (connection, preallocated);
393 /* set reply serial to placeholder value just so space is already allocated
396 if (!dbus_message_set_reply_serial (message, 14))
398 dbus_connection_free_preallocated_send (connection, preallocated);
399 dbus_message_unref (message);
403 d->oom_message = message;
404 d->oom_preallocated = preallocated;
410 bus_connection_send_oom_error (DBusConnection *connection,
411 DBusMessage *in_reply_to)
413 BusConnectionData *d;
415 d = BUS_CONNECTION_DATA (connection);
417 _dbus_assert (d != NULL);
418 _dbus_assert (d->oom_message != NULL);
420 /* should always succeed since we set it to a placeholder earlier */
421 if (!dbus_message_set_reply_serial (d->oom_message,
422 dbus_message_get_serial (in_reply_to)))
423 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
425 dbus_connection_send_preallocated (connection, d->oom_preallocated,
426 d->oom_message, NULL);
428 dbus_message_unref (d->oom_message);
429 d->oom_message = NULL;
430 d->oom_preallocated = NULL;
434 bus_connection_add_owned_service (DBusConnection *connection,
437 BusConnectionData *d;
439 d = BUS_CONNECTION_DATA (connection);
440 _dbus_assert (d != NULL);
442 if (!_dbus_list_append (&d->services_owned,
450 bus_connection_remove_owned_service (DBusConnection *connection,
453 BusConnectionData *d;
455 d = BUS_CONNECTION_DATA (connection);
456 _dbus_assert (d != NULL);
458 _dbus_list_remove_last (&d->services_owned, service);
462 bus_connection_set_name (DBusConnection *connection,
463 const DBusString *name)
466 BusConnectionData *d;
468 d = BUS_CONNECTION_DATA (connection);
469 _dbus_assert (d != NULL);
470 _dbus_assert (d->name == NULL);
472 _dbus_string_get_const_data (name, &c_name);
474 d->name = _dbus_strdup (c_name);
483 bus_connection_get_name (DBusConnection *connection)
485 BusConnectionData *d;
487 d = BUS_CONNECTION_DATA (connection);
488 _dbus_assert (d != NULL);
495 BusTransaction *transaction;
496 DBusMessage *message;
497 DBusPreallocatedSend *preallocated;
500 struct BusTransaction
502 DBusList *connections;
507 message_to_send_free (DBusConnection *connection,
508 MessageToSend *to_send)
510 if (to_send->message)
511 dbus_message_unref (to_send->message);
513 if (to_send->preallocated)
514 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
520 bus_transaction_new (BusContext *context)
522 BusTransaction *transaction;
524 transaction = dbus_new0 (BusTransaction, 1);
525 if (transaction == NULL)
528 transaction->context = context;
534 bus_transaction_get_context (BusTransaction *transaction)
536 return transaction->context;
540 bus_transaction_get_connections (BusTransaction *transaction)
542 return bus_context_get_connections (transaction->context);
546 bus_transaction_send_message (BusTransaction *transaction,
547 DBusConnection *connection,
548 DBusMessage *message)
550 MessageToSend *to_send;
551 BusConnectionData *d;
554 if (!dbus_connection_get_is_connected (connection))
555 return TRUE; /* silently ignore disconnected connections */
557 d = BUS_CONNECTION_DATA (connection);
558 _dbus_assert (d != NULL);
560 to_send = dbus_new (MessageToSend, 1);
566 to_send->preallocated = dbus_connection_preallocate_send (connection);
567 if (to_send->preallocated == NULL)
573 dbus_message_ref (message);
574 to_send->message = message;
575 to_send->transaction = transaction;
577 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
579 message_to_send_free (connection, to_send);
583 /* See if we already had this connection in the list
584 * for this transaction. If we have a pending message,
585 * then we should already be in transaction->connections
587 link = _dbus_list_get_first_link (&d->transaction_messages);
588 _dbus_assert (link->data == to_send);
589 link = _dbus_list_get_next_link (&d->transaction_messages, link);
592 MessageToSend *m = link->data;
593 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
595 if (m->transaction == transaction)
603 if (!_dbus_list_prepend (&transaction->connections, connection))
605 _dbus_list_remove (&d->transaction_messages, to_send);
606 message_to_send_free (connection, to_send);
615 connection_cancel_transaction (DBusConnection *connection,
616 BusTransaction *transaction)
619 BusConnectionData *d;
621 d = BUS_CONNECTION_DATA (connection);
622 _dbus_assert (d != NULL);
624 link = _dbus_list_get_first_link (&d->transaction_messages);
627 MessageToSend *m = link->data;
628 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
630 if (m->transaction == transaction)
632 _dbus_list_remove_link (&d->transaction_messages,
635 message_to_send_free (connection, m);
643 bus_transaction_cancel_and_free (BusTransaction *transaction)
645 DBusConnection *connection;
647 while ((connection = _dbus_list_pop_first (&transaction->connections)))
648 connection_cancel_transaction (connection, transaction);
650 _dbus_assert (transaction->connections == NULL);
652 dbus_free (transaction);
656 connection_execute_transaction (DBusConnection *connection,
657 BusTransaction *transaction)
660 BusConnectionData *d;
662 d = BUS_CONNECTION_DATA (connection);
663 _dbus_assert (d != NULL);
665 /* Send the queue in order (FIFO) */
666 link = _dbus_list_get_last_link (&d->transaction_messages);
669 MessageToSend *m = link->data;
670 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
672 if (m->transaction == transaction)
674 _dbus_list_remove_link (&d->transaction_messages,
677 dbus_connection_send_preallocated (connection,
682 m->preallocated = NULL; /* so we don't double-free it */
684 message_to_send_free (connection, m);
692 bus_transaction_execute_and_free (BusTransaction *transaction)
694 /* For each connection in transaction->connections
697 DBusConnection *connection;
699 while ((connection = _dbus_list_pop_first (&transaction->connections)))
700 connection_execute_transaction (connection, transaction);
702 _dbus_assert (transaction->connections == NULL);
704 dbus_free (transaction);
708 bus_connection_remove_transactions (DBusConnection *connection)
710 MessageToSend *to_send;
711 BusConnectionData *d;
713 d = BUS_CONNECTION_DATA (connection);
714 _dbus_assert (d != NULL);
716 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
718 /* only has an effect for the first MessageToSend listing this transaction */
719 _dbus_list_remove (&to_send->transaction->connections,
722 _dbus_list_remove (&d->transaction_messages, to_send);
723 message_to_send_free (connection, to_send);
728 * Converts the DBusError to a message reply
731 bus_transaction_send_error_reply (BusTransaction *transaction,
732 DBusConnection *connection,
733 const DBusError *error,
734 DBusMessage *in_reply_to)
738 _dbus_assert (error != NULL);
739 _DBUS_ASSERT_ERROR_IS_SET (error);
741 reply = dbus_message_new_error_reply (in_reply_to,
747 if (!bus_transaction_send_message (transaction, connection, reply))
749 dbus_message_unref (reply);