1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
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
25 #include "connection.h"
32 #include <dbus/dbus-internals.h>
35 static int message_handler_slot;
40 BusTransaction *transaction;
45 send_one_message (DBusConnection *connection, void *data)
47 SendMessageData *d = data;
49 if (!bus_connection_is_active (connection))
52 if (!bus_transaction_send_message (d->transaction,
56 BUS_SET_OOM (d->error);
64 bus_dispatch_broadcast_message (BusTransaction *transaction,
70 BusConnections *connections;
72 _dbus_assert (dbus_message_get_sender (message) != NULL);
74 connections = bus_transaction_get_connections (transaction);
76 dbus_error_init (&tmp_error);
78 d.transaction = transaction;
81 bus_connections_foreach (connections, send_one_message, &d);
83 if (dbus_error_is_set (&tmp_error))
85 dbus_move_error (&tmp_error, error);
93 send_service_nonexistent_error (BusTransaction *transaction,
94 DBusConnection *connection,
95 const char *service_name,
96 DBusMessage *in_reply_to,
99 DBusMessage *error_reply;
100 DBusString error_message;
101 const char *error_str;
103 /* Trying to send a message to a non-existant service,
104 * bounce back an error message.
107 if (!_dbus_string_init (&error_message, _DBUS_INT_MAX))
113 if (!_dbus_string_append (&error_message, "Service \"") ||
114 !_dbus_string_append (&error_message, service_name) ||
115 !_dbus_string_append (&error_message, "\" does not exist"))
117 _dbus_string_free (&error_message);
122 _dbus_string_get_const_data (&error_message, &error_str);
123 error_reply = dbus_message_new_error_reply (in_reply_to,
124 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
127 _dbus_string_free (&error_message);
129 if (error_reply == NULL)
135 if (!bus_transaction_send_message (transaction, connection, error_reply))
137 dbus_message_unref (error_reply);
142 dbus_message_unref (error_reply);
148 bus_dispatch (DBusConnection *connection,
149 DBusMessage *message)
151 const char *sender, *service_name, *message_name;
153 BusTransaction *transaction;
157 dbus_error_init (&error);
159 context = bus_connection_get_context (connection);
160 _dbus_assert (context != NULL);
162 /* If we can't even allocate an OOM error, we just go to sleep
165 while (!bus_connection_preallocate_oom_error (connection))
166 bus_wait_for_memory ();
168 /* Ref connection in case we disconnect it at some point in here */
169 dbus_connection_ref (connection);
171 service_name = dbus_message_get_service (message);
172 message_name = dbus_message_get_name (message);
174 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
176 _dbus_verbose ("DISPATCH: %s to %s\n",
177 message_name, service_name ? service_name : "peer");
179 /* If service_name is NULL, this is a message to the bus daemon, not intended
180 * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these
181 * immediately, especially disconnection messages.
183 if (service_name == NULL)
185 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
186 bus_connection_disconnected (connection);
188 /* DBusConnection also handles some of these automatically, we leave
194 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
196 /* Create our transaction */
197 transaction = bus_transaction_new (context);
198 if (transaction == NULL)
200 BUS_SET_OOM (&error);
204 /* Assign a sender to the message */
205 if (bus_connection_is_active (connection))
207 sender = bus_connection_get_name (connection);
208 _dbus_assert (sender != NULL);
210 if (!dbus_message_set_sender (message, sender))
212 BUS_SET_OOM (&error);
217 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
219 if (!bus_driver_handle_message (connection, transaction, message, &error))
222 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
224 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
225 dbus_connection_disconnect (connection);
227 /* FIXME what if we un-special-case this service and just have a flag
228 * on services that all service owners will get messages to it, not just
231 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
233 if (!bus_dispatch_broadcast_message (transaction, message, &error))
236 else /* route to named service */
238 DBusString service_string;
240 BusRegistry *registry;
242 registry = bus_connection_get_registry (connection);
244 _dbus_string_init_const (&service_string, service_name);
245 service = bus_registry_lookup (registry, &service_string);
249 if (!send_service_nonexistent_error (transaction, connection,
256 _dbus_assert (bus_service_get_primary_owner (service) != NULL);
258 /* Dispatch the message */
259 if (!bus_transaction_send_message (transaction,
260 bus_service_get_primary_owner (service),
263 BUS_SET_OOM (&error);
270 if (dbus_error_is_set (&error))
272 if (!dbus_connection_get_is_connected (connection))
274 /* If we disconnected it, we won't bother to send it any error
278 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
280 bus_connection_send_oom_error (connection, message);
282 /* cancel transaction due to OOM */
283 if (transaction != NULL)
285 bus_transaction_cancel_and_free (transaction);
291 /* Try to send the real error, if no mem to do that, send
294 _dbus_assert (transaction != NULL);
296 if (!bus_transaction_send_error_reply (transaction, connection,
299 bus_connection_send_oom_error (connection, message);
301 /* cancel transaction due to OOM */
302 if (transaction != NULL)
304 bus_transaction_cancel_and_free (transaction);
310 dbus_error_free (&error);
313 if (transaction != NULL)
315 bus_transaction_execute_and_free (transaction);
318 dbus_connection_unref (connection);
321 static DBusHandlerResult
322 bus_dispatch_message_handler (DBusMessageHandler *handler,
323 DBusConnection *connection,
324 DBusMessage *message,
327 bus_dispatch (connection, message);
329 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
333 bus_dispatch_add_connection (DBusConnection *connection)
335 DBusMessageHandler *handler;
337 message_handler_slot = dbus_connection_allocate_data_slot ();
339 if (message_handler_slot < 0)
342 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
344 if (!dbus_connection_add_filter (connection, handler))
346 dbus_message_handler_unref (handler);
351 if (!dbus_connection_set_data (connection,
352 message_handler_slot,
354 (DBusFreeFunction)dbus_message_handler_unref))
356 dbus_connection_remove_filter (connection, handler);
357 dbus_message_handler_unref (handler);
366 bus_dispatch_remove_connection (DBusConnection *connection)
368 /* Here we tell the bus driver that we want to get off. */
369 bus_driver_remove_connection (connection);
371 dbus_connection_set_data (connection,
372 message_handler_slot,
378 #ifdef DBUS_BUILD_TESTS
381 flush_bus (BusContext *context)
383 while (bus_loop_iterate (FALSE))
387 /* returns TRUE if the correct thing happens,
388 * but the correct thing may include OOM errors.
391 check_hello_message (BusContext *context,
392 DBusConnection *connection)
394 DBusMessage *message;
397 message = dbus_message_new (DBUS_SERVICE_DBUS,
403 if (!dbus_connection_send (connection, message, &serial))
406 dbus_message_unref (message);
410 message = dbus_connection_pop_message (connection);
413 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
414 DBUS_MESSAGE_HELLO, serial, connection);
418 _dbus_verbose ("Received %s on %p\n",
419 dbus_message_get_name (message), connection);
421 dbus_message_unref (message);
427 bus_dispatch_test (const DBusString *test_data_dir)
431 const char *activation_dirs[] = { NULL, NULL };
435 DBusResultCode result;
437 dbus_error_init (&error);
438 context = bus_context_new ("debug-pipe:name=test-server",
442 _dbus_assert_not_reached ("could not alloc context");
444 foo = dbus_connection_open ("debug-pipe:name=test-server", &result);
446 _dbus_assert_not_reached ("could not alloc connection");
448 bar = dbus_connection_open ("debug-pipe:name=test-server", &result);
450 _dbus_assert_not_reached ("could not alloc connection");
452 baz = dbus_connection_open ("debug-pipe:name=test-server", &result);
454 _dbus_assert_not_reached ("could not alloc connection");
456 if (!bus_setup_debug_client (foo) ||
457 !bus_setup_debug_client (bar) ||
458 !bus_setup_debug_client (baz))
459 _dbus_assert_not_reached ("could not set up connection");
461 if (!check_hello_message (context, foo))
462 _dbus_assert_not_reached ("hello message failed");
463 if (!check_hello_message (context, bar))
464 _dbus_assert_not_reached ("hello message failed");
465 if (!check_hello_message (context, baz))
466 _dbus_assert_not_reached ("hello message failed");
470 #endif /* DBUS_BUILD_TESTS */