1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "connection.h"
33 #include <dbus/dbus-internals.h>
36 static int message_handler_slot = -1;
37 static int message_handler_slot_refcount;
42 BusTransaction *transaction;
47 send_one_message (DBusConnection *connection, void *data)
49 SendMessageData *d = data;
51 if (!bus_connection_is_active (connection))
54 if (!bus_transaction_send_message (d->transaction,
58 BUS_SET_OOM (d->error);
66 bus_dispatch_broadcast_message (BusTransaction *transaction,
72 BusConnections *connections;
74 _dbus_assert (dbus_message_get_sender (message) != NULL);
76 connections = bus_transaction_get_connections (transaction);
78 dbus_error_init (&tmp_error);
80 d.transaction = transaction;
83 bus_connections_foreach (connections, send_one_message, &d);
85 if (dbus_error_is_set (&tmp_error))
87 dbus_move_error (&tmp_error, error);
95 send_service_nonexistent_error (BusTransaction *transaction,
96 DBusConnection *connection,
97 const char *service_name,
98 DBusMessage *in_reply_to,
101 DBusMessage *error_reply;
102 DBusString error_message;
103 const char *error_str;
105 /* Trying to send a message to a non-existant service,
106 * bounce back an error message.
109 if (!_dbus_string_init (&error_message, _DBUS_INT_MAX))
115 if (!_dbus_string_append (&error_message, "Service \"") ||
116 !_dbus_string_append (&error_message, service_name) ||
117 !_dbus_string_append (&error_message, "\" does not exist"))
119 _dbus_string_free (&error_message);
124 _dbus_string_get_const_data (&error_message, &error_str);
125 error_reply = dbus_message_new_error_reply (in_reply_to,
126 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
129 _dbus_string_free (&error_message);
131 if (error_reply == NULL)
137 if (!bus_transaction_send_message (transaction, connection, error_reply))
139 dbus_message_unref (error_reply);
144 dbus_message_unref (error_reply);
150 bus_dispatch (DBusConnection *connection,
151 DBusMessage *message)
153 const char *sender, *service_name, *message_name;
155 BusTransaction *transaction;
159 dbus_error_init (&error);
161 context = bus_connection_get_context (connection);
162 _dbus_assert (context != NULL);
164 /* If we can't even allocate an OOM error, we just go to sleep
167 while (!bus_connection_preallocate_oom_error (connection))
168 bus_wait_for_memory ();
170 /* Ref connection in case we disconnect it at some point in here */
171 dbus_connection_ref (connection);
173 service_name = dbus_message_get_service (message);
174 message_name = dbus_message_get_name (message);
176 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
178 _dbus_verbose ("DISPATCH: %s to %s\n",
179 message_name, service_name ? service_name : "peer");
181 /* If service_name is NULL, this is a message to the bus daemon, not intended
182 * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these
183 * immediately, especially disconnection messages.
185 if (service_name == NULL)
187 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
188 bus_connection_disconnected (connection);
190 /* DBusConnection also handles some of these automatically, we leave
196 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
198 /* Create our transaction */
199 transaction = bus_transaction_new (context);
200 if (transaction == NULL)
202 BUS_SET_OOM (&error);
206 /* Assign a sender to the message */
207 if (bus_connection_is_active (connection))
209 sender = bus_connection_get_name (connection);
210 _dbus_assert (sender != NULL);
212 if (!dbus_message_set_sender (message, sender))
214 BUS_SET_OOM (&error);
218 /* We need to refetch the service name here, because
219 * dbus_message_set_sender can cause the header to be
220 * reallocated, and thus the service_name pointer will become
223 service_name = dbus_message_get_service (message);
226 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
228 if (!bus_driver_handle_message (connection, transaction, message, &error))
231 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
233 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
234 dbus_connection_disconnect (connection);
236 /* FIXME what if we un-special-case this service and just have a flag
237 * on services that all service owners will get messages to it, not just
240 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
242 if (!bus_dispatch_broadcast_message (transaction, message, &error))
245 else /* route to named service */
247 DBusString service_string;
249 BusRegistry *registry;
251 registry = bus_connection_get_registry (connection);
253 _dbus_string_init_const (&service_string, service_name);
254 service = bus_registry_lookup (registry, &service_string);
258 if (!send_service_nonexistent_error (transaction, connection,
265 _dbus_assert (bus_service_get_primary_owner (service) != NULL);
267 /* Dispatch the message */
268 if (!bus_transaction_send_message (transaction,
269 bus_service_get_primary_owner (service),
272 BUS_SET_OOM (&error);
279 if (dbus_error_is_set (&error))
281 if (!dbus_connection_get_is_connected (connection))
283 /* If we disconnected it, we won't bother to send it any error
287 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
289 bus_connection_send_oom_error (connection, message);
291 /* cancel transaction due to OOM */
292 if (transaction != NULL)
294 bus_transaction_cancel_and_free (transaction);
300 /* Try to send the real error, if no mem to do that, send
303 _dbus_assert (transaction != NULL);
305 if (!bus_transaction_send_error_reply (transaction, connection,
308 bus_connection_send_oom_error (connection, message);
310 /* cancel transaction due to OOM */
311 if (transaction != NULL)
313 bus_transaction_cancel_and_free (transaction);
319 dbus_error_free (&error);
322 if (transaction != NULL)
324 bus_transaction_execute_and_free (transaction);
327 dbus_connection_unref (connection);
330 static DBusHandlerResult
331 bus_dispatch_message_handler (DBusMessageHandler *handler,
332 DBusConnection *connection,
333 DBusMessage *message,
336 bus_dispatch (connection, message);
338 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
342 message_handler_slot_ref (void)
344 if (message_handler_slot < 0)
346 message_handler_slot = dbus_connection_allocate_data_slot ();
348 if (message_handler_slot < 0)
351 _dbus_assert (message_handler_slot_refcount == 0);
354 message_handler_slot_refcount += 1;
360 message_handler_slot_unref (void)
362 _dbus_assert (message_handler_slot_refcount > 0);
364 message_handler_slot_refcount -= 1;
366 if (message_handler_slot_refcount == 0)
368 dbus_connection_free_data_slot (message_handler_slot);
369 message_handler_slot = -1;
374 free_message_handler (void *data)
376 DBusMessageHandler *handler = data;
378 _dbus_assert (message_handler_slot >= 0);
379 _dbus_assert (message_handler_slot_refcount > 0);
381 dbus_message_handler_unref (handler);
382 message_handler_slot_unref ();
386 bus_dispatch_add_connection (DBusConnection *connection)
388 DBusMessageHandler *handler;
390 if (!message_handler_slot_ref ())
393 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
396 message_handler_slot_unref ();
400 if (!dbus_connection_add_filter (connection, handler))
402 dbus_message_handler_unref (handler);
403 message_handler_slot_unref ();
408 _dbus_assert (message_handler_slot >= 0);
409 _dbus_assert (message_handler_slot_refcount > 0);
411 if (!dbus_connection_set_data (connection,
412 message_handler_slot,
414 free_message_handler))
416 dbus_message_handler_unref (handler);
417 message_handler_slot_unref ();
426 bus_dispatch_remove_connection (DBusConnection *connection)
428 /* Here we tell the bus driver that we want to get off. */
429 bus_driver_remove_connection (connection);
431 dbus_connection_set_data (connection,
432 message_handler_slot,
436 #ifdef DBUS_BUILD_TESTS
438 typedef dbus_bool_t (* Check1Func) (BusContext *context);
439 typedef dbus_bool_t (* Check2Func) (BusContext *context,
440 DBusConnection *connection);
442 static dbus_bool_t check_no_leftovers (BusContext *context);
445 flush_bus (BusContext *context)
447 /* This is race condition city, obviously. since we're all in one
448 * process we can't block, we just have to wait for data we put in
449 * one end of the debug pipe to come out the other end...
450 * a more robust setup would be good.
453 while (bus_loop_iterate (FALSE))
455 _dbus_sleep_milliseconds (15);
456 while (bus_loop_iterate (FALSE))
462 const char *expected_service_name;
464 } CheckServiceDeletedData;
467 check_service_deleted_foreach (DBusConnection *connection,
470 CheckServiceDeletedData *d = data;
471 DBusMessage *message;
475 dbus_error_init (&error);
479 message = dbus_connection_pop_message (connection);
482 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
483 connection, DBUS_MESSAGE_SERVICE_DELETED);
486 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_DELETED))
488 _dbus_warn ("Received message %s on %p, expecting %s\n",
489 dbus_message_get_name (message),
490 connection, DBUS_MESSAGE_SERVICE_DELETED);
495 if (!dbus_message_get_args (message, &error,
496 DBUS_TYPE_STRING, &service_name,
499 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
501 _dbus_verbose ("no memory to get service name arg\n");
505 _dbus_assert (dbus_error_is_set (&error));
506 _dbus_warn ("Did not get the expected single string argument\n");
510 else if (strcmp (service_name, d->expected_service_name) != 0)
512 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
513 d->expected_service_name,
522 dbus_free (service_name);
523 dbus_error_free (&error);
526 dbus_message_unref (message);
532 kill_client_connection (BusContext *context,
533 DBusConnection *connection)
537 CheckServiceDeletedData csdd;
539 _dbus_verbose ("killing connection %p\n", connection);
541 s = dbus_bus_get_base_service (connection);
542 _dbus_assert (s != NULL);
544 while ((base_service = _dbus_strdup (s)) == NULL)
545 bus_wait_for_memory ();
547 dbus_connection_ref (connection);
549 /* kick in the disconnect handler that unrefs the connection */
550 dbus_connection_disconnect (connection);
554 _dbus_assert (bus_test_client_listed (connection));
556 /* Run disconnect handler in test.c */
557 if (bus_connection_dispatch_one_message (connection))
558 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
560 _dbus_assert (!dbus_connection_get_is_connected (connection));
561 dbus_connection_unref (connection);
563 _dbus_assert (!bus_test_client_listed (connection));
565 csdd.expected_service_name = base_service;
568 bus_test_clients_foreach (check_service_deleted_foreach,
571 dbus_free (base_service);
574 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
576 if (!check_no_leftovers (context))
577 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
581 kill_client_connection_unchecked (DBusConnection *connection)
583 /* This kills the connection without expecting it to affect
584 * the rest of the bus.
586 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
588 dbus_connection_ref (connection);
589 dbus_connection_disconnect (connection);
590 /* dispatching disconnect handler will unref once */
591 if (bus_connection_dispatch_one_message (connection))
592 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
593 dbus_connection_unref (connection);
594 _dbus_assert (!bus_test_client_listed (connection));
600 } CheckNoMessagesData;
603 check_no_messages_foreach (DBusConnection *connection,
606 CheckNoMessagesData *d = data;
607 DBusMessage *message;
609 message = dbus_connection_pop_message (connection);
612 _dbus_warn ("Received message %s on %p, expecting no messages\n",
613 dbus_message_get_name (message), connection);
618 dbus_message_unref (message);
624 DBusConnection *skip_connection;
625 const char *expected_service_name;
627 } CheckServiceCreatedData;
630 check_service_created_foreach (DBusConnection *connection,
633 CheckServiceCreatedData *d = data;
634 DBusMessage *message;
638 if (connection == d->skip_connection)
641 dbus_error_init (&error);
645 message = dbus_connection_pop_message (connection);
648 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
649 connection, DBUS_MESSAGE_SERVICE_CREATED);
652 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
654 _dbus_warn ("Received message %s on %p, expecting %s\n",
655 dbus_message_get_name (message),
656 connection, DBUS_MESSAGE_SERVICE_CREATED);
661 if (!dbus_message_get_args (message, &error,
662 DBUS_TYPE_STRING, &service_name,
665 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
667 _dbus_verbose ("no memory to get service name arg\n");
671 _dbus_assert (dbus_error_is_set (&error));
672 _dbus_warn ("Did not get the expected single string argument\n");
676 else if (strcmp (service_name, d->expected_service_name) != 0)
678 _dbus_warn ("expected creation of service %s, got creation of %s\n",
679 d->expected_service_name,
688 dbus_free (service_name);
689 dbus_error_free (&error);
692 dbus_message_unref (message);
698 check_no_leftovers (BusContext *context)
700 CheckNoMessagesData nmd;
703 bus_test_clients_foreach (check_no_messages_foreach,
712 /* returns TRUE if the correct thing happens,
713 * but the correct thing may include OOM errors.
716 check_hello_message (BusContext *context,
717 DBusConnection *connection)
719 DBusMessage *message;
726 dbus_error_init (&error);
730 message = dbus_message_new (DBUS_SERVICE_DBUS,
736 if (!dbus_connection_send (connection, message, &serial))
738 dbus_message_unref (message);
742 dbus_message_unref (message);
747 if (!dbus_connection_get_is_connected (connection))
749 _dbus_verbose ("connection was disconnected\n");
755 message = dbus_connection_pop_message (connection);
758 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
759 DBUS_MESSAGE_HELLO, serial, connection);
763 _dbus_verbose ("Received %s on %p\n",
764 dbus_message_get_name (message), connection);
766 if (dbus_message_get_is_error (message))
768 if (dbus_message_name_is (message,
769 DBUS_ERROR_NO_MEMORY))
771 ; /* good, this is a valid response */
775 _dbus_warn ("Did not expect error %s\n",
776 dbus_message_get_name (message));
782 CheckServiceCreatedData scd;
784 if (dbus_message_name_is (message,
787 ; /* good, expected */
791 _dbus_warn ("Did not expect reply %s\n",
792 dbus_message_get_name (message));
796 retry_get_hello_name:
797 if (!dbus_message_get_args (message, &error,
798 DBUS_TYPE_STRING, &name,
801 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
803 _dbus_verbose ("no memory to get service name arg from hello\n");
804 dbus_error_free (&error);
805 bus_wait_for_memory ();
806 goto retry_get_hello_name;
810 _dbus_assert (dbus_error_is_set (&error));
811 _dbus_warn ("Did not get the expected single string argument to hello\n");
816 _dbus_verbose ("Got hello name: %s\n", name);
818 while (!dbus_bus_set_base_service (connection, name))
819 bus_wait_for_memory ();
821 scd.skip_connection = NULL;
823 scd.expected_service_name = name;
824 bus_test_clients_foreach (check_service_created_foreach,
830 /* Client should also have gotten ServiceAcquired */
831 dbus_message_unref (message);
832 message = dbus_connection_pop_message (connection);
835 _dbus_warn ("Expecting %s, got nothing\n",
836 DBUS_MESSAGE_SERVICE_ACQUIRED);
840 retry_get_acquired_name:
841 if (!dbus_message_get_args (message, &error,
842 DBUS_TYPE_STRING, &acquired,
845 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
847 _dbus_verbose ("no memory to get service name arg from acquired\n");
848 dbus_error_free (&error);
849 bus_wait_for_memory ();
850 goto retry_get_acquired_name;
854 _dbus_assert (dbus_error_is_set (&error));
855 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
860 _dbus_verbose ("Got acquired name: %s\n", acquired);
862 if (strcmp (acquired, name) != 0)
864 _dbus_warn ("Acquired name is %s but expected %s\n",
870 if (!check_no_leftovers (context))
876 dbus_error_free (&error);
879 dbus_free (acquired);
882 dbus_message_unref (message);
887 /* returns TRUE if the correct thing happens,
888 * but the correct thing may include OOM errors.
891 check_hello_connection (BusContext *context)
893 DBusConnection *connection;
894 DBusResultCode result;
896 result = DBUS_RESULT_SUCCESS;
897 connection = dbus_connection_open ("debug-pipe:name=test-server", &result);
898 if (connection == NULL)
900 _dbus_assert (result != DBUS_RESULT_SUCCESS);
904 if (!bus_setup_debug_client (connection))
906 dbus_connection_disconnect (connection);
907 dbus_connection_unref (connection);
911 if (!check_hello_message (context, connection))
914 if (dbus_bus_get_base_service (connection) == NULL)
916 /* We didn't successfully register, so we can't
917 * do the usual kill_client_connection() checks
919 kill_client_connection_unchecked (connection);
923 kill_client_connection (context, connection);
930 check1_try_iterations (BusContext *context,
931 const char *description,
936 /* Run once to see about how many mallocs are involved */
938 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
940 if (! (*func) (context))
941 _dbus_assert_not_reached ("test failed");
943 approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
945 _dbus_verbose ("=================\n%s: about %d mallocs total\n=================\n",
946 description, approx_mallocs);
948 approx_mallocs += 10; /* fudge factor */
950 /* Now run failing each malloc */
952 while (approx_mallocs >= 0)
954 _dbus_set_fail_alloc_counter (approx_mallocs);
956 _dbus_verbose ("\n===\n %s: (will fail malloc %d)\n===\n",
957 description, approx_mallocs);
959 if (! (*func) (context))
960 _dbus_assert_not_reached ("test failed");
962 if (!check_no_leftovers (context))
963 _dbus_assert_not_reached ("Messages were left over, should be covered by test suite");
968 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
970 _dbus_verbose ("=================\n%s: all iterations passed\n=================\n",
975 bus_dispatch_test (const DBusString *test_data_dir)
979 const char *activation_dirs[] = { NULL, NULL };
983 DBusResultCode result;
985 dbus_error_init (&error);
986 context = bus_context_new ("debug-pipe:name=test-server",
990 _dbus_assert_not_reached ("could not alloc context");
992 foo = dbus_connection_open ("debug-pipe:name=test-server", &result);
994 _dbus_assert_not_reached ("could not alloc connection");
996 if (!bus_setup_debug_client (foo))
997 _dbus_assert_not_reached ("could not set up connection");
999 if (!check_hello_message (context, foo))
1000 _dbus_assert_not_reached ("hello message failed");
1002 bar = dbus_connection_open ("debug-pipe:name=test-server", &result);
1004 _dbus_assert_not_reached ("could not alloc connection");
1006 if (!bus_setup_debug_client (bar))
1007 _dbus_assert_not_reached ("could not set up connection");
1009 if (!check_hello_message (context, bar))
1010 _dbus_assert_not_reached ("hello message failed");
1012 baz = dbus_connection_open ("debug-pipe:name=test-server", &result);
1014 _dbus_assert_not_reached ("could not alloc connection");
1016 if (!bus_setup_debug_client (baz))
1017 _dbus_assert_not_reached ("could not set up connection");
1019 if (!check_hello_message (context, baz))
1020 _dbus_assert_not_reached ("hello message failed");
1022 check1_try_iterations (context, "create_and_hello",
1023 check_hello_connection);
1025 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1027 kill_client_connection_unchecked (foo);
1028 kill_client_connection_unchecked (bar);
1029 kill_client_connection_unchecked (baz);
1031 bus_context_unref (context);
1035 #endif /* DBUS_BUILD_TESTS */