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;
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);
219 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
221 if (!bus_driver_handle_message (connection, transaction, message, &error))
224 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
226 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
227 dbus_connection_disconnect (connection);
229 /* FIXME what if we un-special-case this service and just have a flag
230 * on services that all service owners will get messages to it, not just
233 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
235 if (!bus_dispatch_broadcast_message (transaction, message, &error))
238 else /* route to named service */
240 DBusString service_string;
242 BusRegistry *registry;
244 registry = bus_connection_get_registry (connection);
246 _dbus_string_init_const (&service_string, service_name);
247 service = bus_registry_lookup (registry, &service_string);
251 if (!send_service_nonexistent_error (transaction, connection,
258 _dbus_assert (bus_service_get_primary_owner (service) != NULL);
260 /* Dispatch the message */
261 if (!bus_transaction_send_message (transaction,
262 bus_service_get_primary_owner (service),
265 BUS_SET_OOM (&error);
272 if (dbus_error_is_set (&error))
274 if (!dbus_connection_get_is_connected (connection))
276 /* If we disconnected it, we won't bother to send it any error
280 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
282 bus_connection_send_oom_error (connection, message);
284 /* cancel transaction due to OOM */
285 if (transaction != NULL)
287 bus_transaction_cancel_and_free (transaction);
293 /* Try to send the real error, if no mem to do that, send
296 _dbus_assert (transaction != NULL);
298 if (!bus_transaction_send_error_reply (transaction, connection,
301 bus_connection_send_oom_error (connection, message);
303 /* cancel transaction due to OOM */
304 if (transaction != NULL)
306 bus_transaction_cancel_and_free (transaction);
312 dbus_error_free (&error);
315 if (transaction != NULL)
317 bus_transaction_execute_and_free (transaction);
320 dbus_connection_unref (connection);
323 static DBusHandlerResult
324 bus_dispatch_message_handler (DBusMessageHandler *handler,
325 DBusConnection *connection,
326 DBusMessage *message,
329 bus_dispatch (connection, message);
331 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
335 message_handler_slot_ref (void)
337 message_handler_slot = dbus_connection_allocate_data_slot ();
339 if (message_handler_slot < 0)
342 message_handler_slot_refcount += 1;
348 message_handler_slot_unref (void)
350 _dbus_assert (message_handler_slot_refcount > 0);
351 message_handler_slot_refcount -= 1;
352 if (message_handler_slot_refcount == 0)
354 dbus_connection_free_data_slot (message_handler_slot);
355 message_handler_slot = -1;
360 bus_dispatch_add_connection (DBusConnection *connection)
362 DBusMessageHandler *handler;
364 if (!message_handler_slot_ref ())
367 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
369 if (!dbus_connection_add_filter (connection, handler))
371 dbus_message_handler_unref (handler);
372 message_handler_slot_unref ();
377 if (!dbus_connection_set_data (connection,
378 message_handler_slot,
380 (DBusFreeFunction)dbus_message_handler_unref))
382 dbus_connection_remove_filter (connection, handler);
383 dbus_message_handler_unref (handler);
384 message_handler_slot_unref ();
393 bus_dispatch_remove_connection (DBusConnection *connection)
395 /* Here we tell the bus driver that we want to get off. */
396 bus_driver_remove_connection (connection);
398 dbus_connection_set_data (connection,
399 message_handler_slot,
402 message_handler_slot_unref ();
405 #ifdef DBUS_BUILD_TESTS
407 typedef dbus_bool_t (* Check1Func) (BusContext *context);
408 typedef dbus_bool_t (* Check2Func) (BusContext *context,
409 DBusConnection *connection);
411 static dbus_bool_t check_no_leftovers (BusContext *context);
414 flush_bus (BusContext *context)
416 while (bus_loop_iterate (FALSE))
422 const char *expected_service_name;
424 } CheckServiceDeletedData;
427 check_service_deleted_foreach (DBusConnection *connection,
430 CheckServiceDeletedData *d = data;
431 DBusMessage *message;
435 dbus_error_init (&error);
439 message = dbus_connection_pop_message (connection);
442 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
443 connection, DBUS_MESSAGE_SERVICE_DELETED);
446 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_DELETED))
448 _dbus_warn ("Received message %s on %p, expecting %s\n",
449 dbus_message_get_name (message),
450 connection, DBUS_MESSAGE_SERVICE_DELETED);
455 if (!dbus_message_get_args (message, &error,
456 DBUS_TYPE_STRING, &service_name,
459 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
461 _dbus_verbose ("no memory to get service name arg\n");
465 _dbus_assert (dbus_error_is_set (&error));
466 _dbus_warn ("Did not get the expected single string argument\n");
470 else if (strcmp (service_name, d->expected_service_name) != 0)
472 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
473 d->expected_service_name,
482 dbus_free (service_name);
483 dbus_error_free (&error);
486 dbus_message_unref (message);
492 kill_client_connection (BusContext *context,
493 DBusConnection *connection)
497 CheckServiceDeletedData csdd;
499 _dbus_verbose ("killing connection %p\n", connection);
501 s = dbus_bus_get_base_service (connection);
502 _dbus_assert (s != NULL);
504 while ((base_service = _dbus_strdup (s)) == NULL)
505 bus_wait_for_memory ();
507 dbus_connection_ref (connection);
509 /* kick in the disconnect handler that unrefs the connection */
510 dbus_connection_disconnect (connection);
514 _dbus_assert (bus_test_client_listed (connection));
516 /* Run disconnect handler in test.c */
517 if (bus_connection_dispatch_one_message (connection))
518 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
520 _dbus_assert (!dbus_connection_get_is_connected (connection));
521 dbus_connection_unref (connection);
523 _dbus_assert (!bus_test_client_listed (connection));
525 csdd.expected_service_name = base_service;
528 bus_test_clients_foreach (check_service_deleted_foreach,
531 dbus_free (base_service);
534 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
536 if (!check_no_leftovers (context))
537 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
543 } CheckNoMessagesData;
546 check_no_messages_foreach (DBusConnection *connection,
549 CheckNoMessagesData *d = data;
550 DBusMessage *message;
552 message = dbus_connection_pop_message (connection);
555 _dbus_warn ("Received message %s on %p, expecting no messages\n",
556 dbus_message_get_name (message), connection);
561 dbus_message_unref (message);
567 DBusConnection *skip_connection;
568 const char *expected_service_name;
570 } CheckServiceCreatedData;
573 check_service_created_foreach (DBusConnection *connection,
576 CheckServiceCreatedData *d = data;
577 DBusMessage *message;
581 if (connection == d->skip_connection)
584 dbus_error_init (&error);
588 message = dbus_connection_pop_message (connection);
591 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
592 connection, DBUS_MESSAGE_SERVICE_CREATED);
595 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
597 _dbus_warn ("Received message %s on %p, expecting %s\n",
598 dbus_message_get_name (message),
599 connection, DBUS_MESSAGE_SERVICE_CREATED);
604 if (!dbus_message_get_args (message, &error,
605 DBUS_TYPE_STRING, &service_name,
608 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
610 _dbus_verbose ("no memory to get service name arg\n");
614 _dbus_assert (dbus_error_is_set (&error));
615 _dbus_warn ("Did not get the expected single string argument\n");
619 else if (strcmp (service_name, d->expected_service_name) != 0)
621 _dbus_warn ("expected creation of service %s, got creation of %s\n",
622 d->expected_service_name,
631 dbus_free (service_name);
632 dbus_error_free (&error);
635 dbus_message_unref (message);
641 check_no_leftovers (BusContext *context)
643 CheckNoMessagesData nmd;
646 bus_test_clients_foreach (check_no_messages_foreach,
655 /* returns TRUE if the correct thing happens,
656 * but the correct thing may include OOM errors.
659 check_hello_message (BusContext *context,
660 DBusConnection *connection)
662 DBusMessage *message;
669 dbus_error_init (&error);
673 message = dbus_message_new (DBUS_SERVICE_DBUS,
679 if (!dbus_connection_send (connection, message, &serial))
682 dbus_message_unref (message);
687 if (!dbus_connection_get_is_connected (connection))
689 _dbus_verbose ("connection was disconnected\n");
695 message = dbus_connection_pop_message (connection);
698 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
699 DBUS_MESSAGE_HELLO, serial, connection);
703 _dbus_verbose ("Received %s on %p\n",
704 dbus_message_get_name (message), connection);
706 if (dbus_message_get_is_error (message))
708 if (dbus_message_name_is (message,
709 DBUS_ERROR_NO_MEMORY))
711 ; /* good, this is a valid response */
715 _dbus_warn ("Did not expect error %s\n",
716 dbus_message_get_name (message));
722 CheckServiceCreatedData scd;
724 if (dbus_message_name_is (message,
727 ; /* good, expected */
731 _dbus_warn ("Did not expect reply %s\n",
732 dbus_message_get_name (message));
736 retry_get_hello_name:
737 if (!dbus_message_get_args (message, &error,
738 DBUS_TYPE_STRING, &name,
741 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
743 _dbus_verbose ("no memory to get service name arg from hello\n");
744 dbus_error_free (&error);
745 bus_wait_for_memory ();
746 goto retry_get_hello_name;
750 _dbus_assert (dbus_error_is_set (&error));
751 _dbus_warn ("Did not get the expected single string argument to hello\n");
756 _dbus_verbose ("Got hello name: %s\n", name);
758 while (!dbus_bus_set_base_service (connection, name))
759 bus_wait_for_memory ();
761 scd.skip_connection = NULL;
763 scd.expected_service_name = name;
764 bus_test_clients_foreach (check_service_created_foreach,
770 /* Client should also have gotten ServiceAcquired */
771 dbus_message_unref (message);
772 message = dbus_connection_pop_message (connection);
775 _dbus_warn ("Expecting %s, got nothing\n",
776 DBUS_MESSAGE_SERVICE_ACQUIRED);
780 retry_get_acquired_name:
781 if (!dbus_message_get_args (message, &error,
782 DBUS_TYPE_STRING, &acquired,
785 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
787 _dbus_verbose ("no memory to get service name arg from acquired\n");
788 dbus_error_free (&error);
789 bus_wait_for_memory ();
790 goto retry_get_acquired_name;
794 _dbus_assert (dbus_error_is_set (&error));
795 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
800 _dbus_verbose ("Got acquired name: %s\n", acquired);
802 if (strcmp (acquired, name) != 0)
804 _dbus_warn ("Acquired name is %s but expected %s\n",
810 if (!check_no_leftovers (context))
816 dbus_error_free (&error);
819 dbus_free (acquired);
822 dbus_message_unref (message);
827 /* returns TRUE if the correct thing happens,
828 * but the correct thing may include OOM errors.
831 check_hello_connection (BusContext *context)
833 DBusConnection *connection;
834 DBusResultCode result;
836 result = DBUS_RESULT_SUCCESS;
837 connection = dbus_connection_open ("debug-pipe:name=test-server", &result);
838 if (connection == NULL)
840 _dbus_assert (result != DBUS_RESULT_SUCCESS);
844 if (!bus_setup_debug_client (connection))
846 dbus_connection_disconnect (connection);
847 dbus_connection_unref (connection);
851 if (!check_hello_message (context, connection))
854 if (dbus_bus_get_base_service (connection) == NULL)
856 /* We didn't successfully register, so we can't
857 * do the usual kill_client_connection() checks
859 dbus_connection_ref (connection);
860 dbus_connection_disconnect (connection);
861 /* dispatching disconnect handler will unref once */
862 if (bus_connection_dispatch_one_message (connection))
863 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
864 dbus_connection_unref (connection);
865 _dbus_assert (!bus_test_client_listed (connection));
870 kill_client_connection (context, connection);
877 check1_try_iterations (BusContext *context,
878 const char *description,
883 /* Run once to see about how many mallocs are involved */
885 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
887 if (! (*func) (context))
888 _dbus_assert_not_reached ("test failed");
890 approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
892 _dbus_verbose ("=================\n%s: about %d mallocs total\n=================\n",
893 description, approx_mallocs);
895 approx_mallocs += 10; /* fudge factor */
897 /* Now run failing each malloc */
899 while (approx_mallocs >= 0)
901 _dbus_set_fail_alloc_counter (approx_mallocs);
903 _dbus_verbose ("\n===\n %s: (will fail malloc %d)\n===\n",
904 description, approx_mallocs);
906 if (! (*func) (context))
907 _dbus_assert_not_reached ("test failed");
909 if (!check_no_leftovers (context))
910 _dbus_assert_not_reached ("Messages were left over, should be covered by test suite");
915 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
919 bus_dispatch_test (const DBusString *test_data_dir)
923 const char *activation_dirs[] = { NULL, NULL };
927 DBusResultCode result;
929 dbus_error_init (&error);
930 context = bus_context_new ("debug-pipe:name=test-server",
934 _dbus_assert_not_reached ("could not alloc context");
936 foo = dbus_connection_open ("debug-pipe:name=test-server", &result);
938 _dbus_assert_not_reached ("could not alloc connection");
940 if (!bus_setup_debug_client (foo))
941 _dbus_assert_not_reached ("could not set up connection");
943 if (!check_hello_message (context, foo))
944 _dbus_assert_not_reached ("hello message failed");
946 bar = dbus_connection_open ("debug-pipe:name=test-server", &result);
948 _dbus_assert_not_reached ("could not alloc connection");
950 if (!bus_setup_debug_client (bar))
951 _dbus_assert_not_reached ("could not set up connection");
953 if (!check_hello_message (context, bar))
954 _dbus_assert_not_reached ("hello message failed");
956 baz = dbus_connection_open ("debug-pipe:name=test-server", &result);
958 _dbus_assert_not_reached ("could not alloc connection");
960 if (!bus_setup_debug_client (baz))
961 _dbus_assert_not_reached ("could not set up connection");
963 if (!check_hello_message (context, baz))
964 _dbus_assert_not_reached ("hello message failed");
966 check1_try_iterations (context, "create_and_hello",
967 check_hello_connection);
969 dbus_connection_disconnect (foo);
970 if (bus_connection_dispatch_one_message (foo))
971 _dbus_assert_not_reached ("extra message in queue");
972 dbus_connection_unref (foo);
973 _dbus_assert (!bus_test_client_listed (foo));
975 dbus_connection_disconnect (bar);
976 if (bus_connection_dispatch_one_message (bar))
977 _dbus_assert_not_reached ("extra message in queue");
978 dbus_connection_unref (bar);
979 _dbus_assert (!bus_test_client_listed (bar));
981 dbus_connection_disconnect (baz);
982 if (bus_connection_dispatch_one_message (baz))
983 _dbus_assert_not_reached ("extra message in queue");
984 dbus_connection_unref (baz);
985 _dbus_assert (!bus_test_client_listed (baz));
987 bus_context_unref (context);
991 #endif /* DBUS_BUILD_TESTS */