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
380 typedef dbus_bool_t (* Check1Func) (BusContext *context);
381 typedef dbus_bool_t (* Check2Func) (BusContext *context,
382 DBusConnection *connection);
385 flush_bus (BusContext *context)
387 while (bus_loop_iterate (FALSE))
392 kill_client_connection (DBusConnection *connection)
394 /* kick in the disconnect handler that unrefs the connection */
395 dbus_connection_disconnect (connection);
396 while (dbus_connection_dispatch_message (connection))
400 /* returns TRUE if the correct thing happens,
401 * but the correct thing may include OOM errors.
404 check_hello_message (BusContext *context,
405 DBusConnection *connection)
407 DBusMessage *message;
412 dbus_error_init (&error);
414 message = dbus_message_new (DBUS_SERVICE_DBUS,
420 if (!dbus_connection_send (connection, message, &serial))
423 dbus_message_unref (message);
428 if (!dbus_connection_get_is_connected (connection))
430 _dbus_verbose ("connection was disconnected\n");
436 message = dbus_connection_pop_message (connection);
439 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
440 DBUS_MESSAGE_HELLO, serial, connection);
444 _dbus_verbose ("Received %s on %p\n",
445 dbus_message_get_name (message), connection);
447 if (dbus_message_get_is_error (message))
449 if (dbus_message_name_is (message,
450 DBUS_ERROR_NO_MEMORY))
452 ; /* good, this is a valid response */
456 _dbus_warn ("Did not expect error %s\n",
457 dbus_message_get_name (message));
465 if (dbus_message_name_is (message,
468 ; /* good, expected */
472 _dbus_warn ("Did not expect reply %s\n",
473 dbus_message_get_name (message));
477 if (!dbus_message_get_args (message, &error,
478 DBUS_TYPE_STRING, &str,
481 _dbus_warn ("Did not get the expected single string argument\n");
485 _dbus_verbose ("Got hello name: %s\n", str);
493 dbus_message_unref (message);
498 /* returns TRUE if the correct thing happens,
499 * but the correct thing may include OOM errors.
502 check_hello_connection (BusContext *context)
504 DBusConnection *connection;
505 DBusResultCode result;
507 result = DBUS_RESULT_SUCCESS;
508 connection = dbus_connection_open ("debug-pipe:name=test-server", &result);
509 if (connection == NULL)
511 _dbus_assert (result != DBUS_RESULT_SUCCESS);
515 if (!bus_setup_debug_client (connection))
517 dbus_connection_disconnect (connection);
518 dbus_connection_unref (connection);
522 if (!check_hello_message (context, connection))
525 kill_client_connection (connection);
531 check1_try_iterations (BusContext *context,
532 const char *description,
537 /* Run once to see about how many mallocs are involved */
539 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
541 if (! (*func) (context))
542 _dbus_assert_not_reached ("test failed");
544 approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
546 _dbus_verbose ("=================\n%s: about %d mallocs total\n=================\n",
547 description, approx_mallocs);
549 approx_mallocs += 10; /* fudge factor */
551 /* Now run failing each malloc */
553 while (approx_mallocs >= 0)
555 _dbus_set_fail_alloc_counter (approx_mallocs);
557 _dbus_verbose ("\n===\n %s: (will fail malloc %d)\n===\n",
558 description, approx_mallocs);
560 if (! (*func) (context))
561 _dbus_assert_not_reached ("test failed");
566 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
570 bus_dispatch_test (const DBusString *test_data_dir)
574 const char *activation_dirs[] = { NULL, NULL };
578 DBusResultCode result;
580 dbus_error_init (&error);
581 context = bus_context_new ("debug-pipe:name=test-server",
585 _dbus_assert_not_reached ("could not alloc context");
587 foo = dbus_connection_open ("debug-pipe:name=test-server", &result);
589 _dbus_assert_not_reached ("could not alloc connection");
591 bar = dbus_connection_open ("debug-pipe:name=test-server", &result);
593 _dbus_assert_not_reached ("could not alloc connection");
595 baz = dbus_connection_open ("debug-pipe:name=test-server", &result);
597 _dbus_assert_not_reached ("could not alloc connection");
599 if (!bus_setup_debug_client (foo) ||
600 !bus_setup_debug_client (bar) ||
601 !bus_setup_debug_client (baz))
602 _dbus_assert_not_reached ("could not set up connection");
604 if (!check_hello_message (context, foo))
605 _dbus_assert_not_reached ("hello message failed");
606 if (!check_hello_message (context, bar))
607 _dbus_assert_not_reached ("hello message failed");
608 if (!check_hello_message (context, baz))
609 _dbus_assert_not_reached ("hello message failed");
611 check1_try_iterations (context, "create_and_hello",
612 check_hello_connection);
614 dbus_connection_disconnect (foo);
615 dbus_connection_unref (foo);
616 dbus_connection_disconnect (bar);
617 dbus_connection_unref (bar);
618 dbus_connection_disconnect (baz);
619 dbus_connection_unref (baz);
623 #endif /* DBUS_BUILD_TESTS */