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);
32 static int connection_data_slot;
33 static DBusList *connections = NULL;
37 DBusConnection *connection;
38 DBusList *services_owned;
40 DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
41 DBusMessage *oom_message;
42 DBusPreallocatedSend *oom_preallocated;
45 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
48 bus_connection_disconnected (DBusConnection *connection)
53 _dbus_warn ("Disconnected\n");
55 d = BUS_CONNECTION_DATA (connection);
56 _dbus_assert (d != NULL);
58 /* Drop any service ownership. FIXME Unfortunately, this requires
59 * memory allocation and there doesn't seem to be a good way to
60 * handle it other than sleeping; we can't "fail" the operation of
61 * disconnecting a client, and preallocating a broadcast "service is
62 * now gone" message for every client-service pair seems kind of
63 * involved. Probably we need to do that though, and also
64 * extend BusTransaction to be able to revert generic
65 * stuff, not just sending a message (so we can e.g. revert
66 * removal of service owners).
69 BusTransaction *transaction;
72 dbus_error_init (&error);
75 while (transaction == NULL)
77 transaction = bus_transaction_new ();
78 bus_wait_for_memory ();
81 while ((service = _dbus_list_get_last (&d->services_owned)))
84 if (!bus_service_remove_owner (service, connection,
87 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
89 dbus_error_free (&error);
90 bus_wait_for_memory ();
94 _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
98 bus_transaction_execute_and_free (transaction);
101 bus_dispatch_remove_connection (connection);
103 /* no more watching */
104 dbus_connection_set_watch_functions (connection,
109 bus_connection_remove_transactions (connection);
111 dbus_connection_set_data (connection,
112 connection_data_slot,
115 _dbus_list_remove (&connections, connection);
116 dbus_connection_unref (connection);
120 connection_watch_callback (DBusWatch *watch,
121 unsigned int condition,
124 DBusConnection *connection = data;
126 dbus_connection_ref (connection);
128 dbus_connection_handle_watch (connection, watch, condition);
130 while (dbus_connection_dispatch_message (connection));
131 dbus_connection_unref (connection);
135 add_connection_watch (DBusWatch *watch,
136 DBusConnection *connection)
138 bus_loop_add_watch (watch, connection_watch_callback, connection,
143 remove_connection_watch (DBusWatch *watch,
144 DBusConnection *connection)
146 bus_loop_remove_watch (watch, connection_watch_callback, connection);
150 free_connection_data (void *data)
152 BusConnectionData *d = data;
154 /* services_owned should be NULL since we should be disconnected */
155 _dbus_assert (d->services_owned == NULL);
157 _dbus_assert (d->transaction_messages == NULL);
159 if (d->oom_preallocated)
160 dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
162 dbus_message_unref (d->oom_message);
170 bus_connection_init (void)
172 connection_data_slot = dbus_connection_allocate_data_slot ();
174 if (connection_data_slot < 0)
181 bus_connection_setup (DBusConnection *connection)
183 BusConnectionData *d;
185 d = dbus_new0 (BusConnectionData, 1);
190 d->connection = connection;
192 if (!dbus_connection_set_data (connection,
193 connection_data_slot,
194 d, free_connection_data))
200 if (!_dbus_list_append (&connections, connection))
202 /* this will free our data when connection gets finalized */
203 dbus_connection_disconnect (connection);
207 dbus_connection_ref (connection);
209 dbus_connection_set_watch_functions (connection,
210 (DBusAddWatchFunction) add_connection_watch,
211 (DBusRemoveWatchFunction) remove_connection_watch,
215 /* Setup the connection with the dispatcher */
216 if (!bus_dispatch_add_connection (connection))
223 * Checks whether the connection is registered with the message bus.
225 * @param connection the connection
226 * @returns #TRUE if we're an active message bus participant
229 bus_connection_is_active (DBusConnection *connection)
231 BusConnectionData *d;
233 d = BUS_CONNECTION_DATA (connection);
235 return d != NULL && d->name != NULL;
239 bus_connection_preallocate_oom_error (DBusConnection *connection)
241 DBusMessage *message;
242 DBusPreallocatedSend *preallocated;
243 BusConnectionData *d;
245 d = BUS_CONNECTION_DATA (connection);
247 _dbus_assert (d != NULL);
249 if (d->oom_preallocated != NULL)
252 preallocated = dbus_connection_preallocate_send (connection);
253 if (preallocated == NULL)
256 message = dbus_message_new (DBUS_SERVICE_DBUS,
257 DBUS_ERROR_NO_MEMORY);
260 dbus_connection_free_preallocated_send (connection, preallocated);
264 /* set reply serial to placeholder value just so space is already allocated
267 if (!dbus_message_set_reply_serial (message, 14))
269 dbus_connection_free_preallocated_send (connection, preallocated);
270 dbus_message_unref (message);
274 d->oom_message = message;
275 d->oom_preallocated = preallocated;
281 bus_connection_send_oom_error (DBusConnection *connection,
282 DBusMessage *in_reply_to)
284 BusConnectionData *d;
286 d = BUS_CONNECTION_DATA (connection);
288 _dbus_assert (d != NULL);
289 _dbus_assert (d->oom_message != NULL);
291 /* should always succeed since we set it to a placeholder earlier */
292 if (!dbus_message_set_reply_serial (d->oom_message,
293 dbus_message_get_serial (in_reply_to)))
294 _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
296 dbus_connection_send_preallocated (connection, d->oom_preallocated,
297 d->oom_message, NULL);
299 dbus_message_unref (d->oom_message);
300 d->oom_message = NULL;
301 d->oom_preallocated = NULL;
305 bus_connection_add_owned_service (DBusConnection *connection,
308 BusConnectionData *d;
310 d = BUS_CONNECTION_DATA (connection);
311 _dbus_assert (d != NULL);
313 if (!_dbus_list_append (&d->services_owned,
321 bus_connection_remove_owned_service (DBusConnection *connection,
324 BusConnectionData *d;
326 d = BUS_CONNECTION_DATA (connection);
327 _dbus_assert (d != NULL);
329 _dbus_list_remove_last (&d->services_owned, service);
333 bus_connection_set_name (DBusConnection *connection,
334 const DBusString *name)
337 BusConnectionData *d;
339 d = BUS_CONNECTION_DATA (connection);
340 _dbus_assert (d != NULL);
341 _dbus_assert (d->name == NULL);
343 _dbus_string_get_const_data (name, &c_name);
345 d->name = _dbus_strdup (c_name);
354 bus_connection_get_name (DBusConnection *connection)
356 BusConnectionData *d;
358 d = BUS_CONNECTION_DATA (connection);
359 _dbus_assert (d != NULL);
365 * Calls function on each connection; if the function returns
366 * #FALSE, stops iterating.
368 * @param function the function
369 * @param data data to pass to it as a second arg
372 bus_connection_foreach (BusConnectionForeachFunction function,
377 link = _dbus_list_get_first_link (&connections);
380 DBusConnection *connection = link->data;
381 DBusList *next = _dbus_list_get_next_link (&connections, link);
383 if (!(* function) (connection, data))
392 BusTransaction *transaction;
393 DBusMessage *message;
394 DBusPreallocatedSend *preallocated;
397 struct BusTransaction
399 DBusList *connections;
404 message_to_send_free (DBusConnection *connection,
405 MessageToSend *to_send)
407 if (to_send->message)
408 dbus_message_unref (to_send->message);
410 if (to_send->preallocated)
411 dbus_connection_free_preallocated_send (connection, to_send->preallocated);
417 bus_transaction_new (void)
419 BusTransaction *transaction;
421 transaction = dbus_new0 (BusTransaction, 1);
422 if (transaction == NULL)
429 bus_transaction_send_message (BusTransaction *transaction,
430 DBusConnection *connection,
431 DBusMessage *message)
433 MessageToSend *to_send;
434 BusConnectionData *d;
437 if (!dbus_connection_get_is_connected (connection))
438 return TRUE; /* silently ignore disconnected connections */
440 d = BUS_CONNECTION_DATA (connection);
441 _dbus_assert (d != NULL);
443 to_send = dbus_new (MessageToSend, 1);
449 to_send->preallocated = dbus_connection_preallocate_send (connection);
450 if (to_send->preallocated == NULL)
456 dbus_message_ref (message);
457 to_send->message = message;
458 to_send->transaction = transaction;
460 if (!_dbus_list_prepend (&d->transaction_messages, to_send))
462 message_to_send_free (connection, to_send);
466 /* See if we already had this connection in the list
467 * for this transaction. If we have a pending message,
468 * then we should already be in transaction->connections
470 link = _dbus_list_get_first_link (&d->transaction_messages);
471 _dbus_assert (link->data == to_send);
472 link = _dbus_list_get_next_link (&d->transaction_messages, link);
475 MessageToSend *m = link->data;
476 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
478 if (m->transaction == transaction)
486 if (!_dbus_list_prepend (&transaction->connections, connection))
488 _dbus_list_remove (&d->transaction_messages, to_send);
489 message_to_send_free (connection, to_send);
498 connection_cancel_transaction (DBusConnection *connection,
499 BusTransaction *transaction)
502 BusConnectionData *d;
504 d = BUS_CONNECTION_DATA (connection);
505 _dbus_assert (d != NULL);
507 link = _dbus_list_get_first_link (&d->transaction_messages);
510 MessageToSend *m = link->data;
511 DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
513 if (m->transaction == transaction)
515 _dbus_list_remove_link (&d->transaction_messages,
518 message_to_send_free (connection, m);
526 bus_transaction_cancel_and_free (BusTransaction *transaction)
528 DBusConnection *connection;
530 while ((connection = _dbus_list_pop_first (&transaction->connections)))
531 connection_cancel_transaction (connection, transaction);
533 _dbus_assert (transaction->connections == NULL);
535 dbus_free (transaction);
539 connection_execute_transaction (DBusConnection *connection,
540 BusTransaction *transaction)
543 BusConnectionData *d;
545 d = BUS_CONNECTION_DATA (connection);
546 _dbus_assert (d != NULL);
548 /* Send the queue in order (FIFO) */
549 link = _dbus_list_get_last_link (&d->transaction_messages);
552 MessageToSend *m = link->data;
553 DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
555 if (m->transaction == transaction)
557 _dbus_list_remove_link (&d->transaction_messages,
560 dbus_connection_send_preallocated (connection,
565 m->preallocated = NULL; /* so we don't double-free it */
567 message_to_send_free (connection, m);
575 bus_transaction_execute_and_free (BusTransaction *transaction)
577 /* For each connection in transaction->connections
580 DBusConnection *connection;
582 while ((connection = _dbus_list_pop_first (&transaction->connections)))
583 connection_execute_transaction (connection, transaction);
585 _dbus_assert (transaction->connections == NULL);
587 dbus_free (transaction);
591 bus_connection_remove_transactions (DBusConnection *connection)
593 MessageToSend *to_send;
594 BusConnectionData *d;
596 d = BUS_CONNECTION_DATA (connection);
597 _dbus_assert (d != NULL);
599 while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
601 /* only has an effect for the first MessageToSend listing this transaction */
602 _dbus_list_remove (&to_send->transaction->connections,
605 _dbus_list_remove (&d->transaction_messages, to_send);
606 message_to_send_free (connection, to_send);
611 * Converts the DBusError to a message reply
614 bus_transaction_send_error_reply (BusTransaction *transaction,
615 DBusConnection *connection,
616 const DBusError *error,
617 DBusMessage *in_reply_to)
621 _dbus_assert (error != NULL);
622 _DBUS_ASSERT_ERROR_IS_SET (error);
624 reply = dbus_message_new_error_reply (in_reply_to,
630 if (!bus_transaction_send_message (transaction, connection, reply))
632 dbus_message_unref (reply);