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);
446 const char *expected_service_name;
448 } CheckServiceDeletedData;
451 check_service_deleted_foreach (DBusConnection *connection,
454 CheckServiceDeletedData *d = data;
455 DBusMessage *message;
459 dbus_error_init (&error);
463 message = dbus_connection_pop_message (connection);
466 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
467 connection, DBUS_MESSAGE_SERVICE_DELETED);
470 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_DELETED))
472 _dbus_warn ("Received message %s on %p, expecting %s\n",
473 dbus_message_get_name (message),
474 connection, DBUS_MESSAGE_SERVICE_DELETED);
479 if (!dbus_message_get_args (message, &error,
480 DBUS_TYPE_STRING, &service_name,
483 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
485 _dbus_verbose ("no memory to get service name arg\n");
489 _dbus_assert (dbus_error_is_set (&error));
490 _dbus_warn ("Did not get the expected single string argument\n");
494 else if (strcmp (service_name, d->expected_service_name) != 0)
496 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
497 d->expected_service_name,
506 dbus_free (service_name);
507 dbus_error_free (&error);
510 dbus_message_unref (message);
516 kill_client_connection (BusContext *context,
517 DBusConnection *connection)
521 CheckServiceDeletedData csdd;
523 _dbus_verbose ("killing connection %p\n", connection);
525 s = dbus_bus_get_base_service (connection);
526 _dbus_assert (s != NULL);
528 while ((base_service = _dbus_strdup (s)) == NULL)
529 bus_wait_for_memory ();
531 dbus_connection_ref (connection);
533 /* kick in the disconnect handler that unrefs the connection */
534 dbus_connection_disconnect (connection);
536 bus_test_flush_bus (context);
538 _dbus_assert (bus_test_client_listed (connection));
540 /* Run disconnect handler in test.c */
541 if (bus_connection_dispatch_one_message (connection))
542 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
544 _dbus_assert (!dbus_connection_get_is_connected (connection));
545 dbus_connection_unref (connection);
547 _dbus_assert (!bus_test_client_listed (connection));
549 csdd.expected_service_name = base_service;
552 bus_test_clients_foreach (check_service_deleted_foreach,
555 dbus_free (base_service);
558 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
560 if (!check_no_leftovers (context))
561 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
565 kill_client_connection_unchecked (DBusConnection *connection)
567 /* This kills the connection without expecting it to affect
568 * the rest of the bus.
570 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
572 dbus_connection_ref (connection);
573 dbus_connection_disconnect (connection);
574 /* dispatching disconnect handler will unref once */
575 if (bus_connection_dispatch_one_message (connection))
576 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
577 dbus_connection_unref (connection);
578 _dbus_assert (!bus_test_client_listed (connection));
584 } CheckNoMessagesData;
587 check_no_messages_foreach (DBusConnection *connection,
590 CheckNoMessagesData *d = data;
591 DBusMessage *message;
593 message = dbus_connection_pop_message (connection);
596 _dbus_warn ("Received message %s on %p, expecting no messages\n",
597 dbus_message_get_name (message), connection);
602 dbus_message_unref (message);
608 DBusConnection *skip_connection;
609 const char *expected_service_name;
611 } CheckServiceCreatedData;
614 check_service_created_foreach (DBusConnection *connection,
617 CheckServiceCreatedData *d = data;
618 DBusMessage *message;
622 if (connection == d->skip_connection)
625 dbus_error_init (&error);
629 message = dbus_connection_pop_message (connection);
632 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
633 connection, DBUS_MESSAGE_SERVICE_CREATED);
636 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
638 _dbus_warn ("Received message %s on %p, expecting %s\n",
639 dbus_message_get_name (message),
640 connection, DBUS_MESSAGE_SERVICE_CREATED);
645 if (!dbus_message_get_args (message, &error,
646 DBUS_TYPE_STRING, &service_name,
649 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
651 _dbus_verbose ("no memory to get service name arg\n");
655 _dbus_assert (dbus_error_is_set (&error));
656 _dbus_warn ("Did not get the expected single string argument\n");
660 else if (strcmp (service_name, d->expected_service_name) != 0)
662 _dbus_warn ("expected creation of service %s, got creation of %s\n",
663 d->expected_service_name,
672 dbus_free (service_name);
673 dbus_error_free (&error);
676 dbus_message_unref (message);
682 check_no_leftovers (BusContext *context)
684 CheckNoMessagesData nmd;
687 bus_test_clients_foreach (check_no_messages_foreach,
696 /* returns TRUE if the correct thing happens,
697 * but the correct thing may include OOM errors.
700 check_hello_message (BusContext *context,
701 DBusConnection *connection)
703 DBusMessage *message;
710 dbus_error_init (&error);
714 message = dbus_message_new (DBUS_SERVICE_DBUS,
720 if (!dbus_connection_send (connection, message, &serial))
722 dbus_message_unref (message);
726 dbus_message_unref (message);
729 bus_test_flush_bus (context);
731 if (!dbus_connection_get_is_connected (connection))
733 _dbus_verbose ("connection was disconnected\n");
739 message = dbus_connection_pop_message (connection);
742 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
743 DBUS_MESSAGE_HELLO, serial, connection);
747 _dbus_verbose ("Received %s on %p\n",
748 dbus_message_get_name (message), connection);
750 if (dbus_message_get_is_error (message))
752 if (dbus_message_name_is (message,
753 DBUS_ERROR_NO_MEMORY))
755 ; /* good, this is a valid response */
759 _dbus_warn ("Did not expect error %s\n",
760 dbus_message_get_name (message));
766 CheckServiceCreatedData scd;
768 if (dbus_message_name_is (message,
771 ; /* good, expected */
775 _dbus_warn ("Did not expect reply %s\n",
776 dbus_message_get_name (message));
780 retry_get_hello_name:
781 if (!dbus_message_get_args (message, &error,
782 DBUS_TYPE_STRING, &name,
785 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
787 _dbus_verbose ("no memory to get service name arg from hello\n");
788 dbus_error_free (&error);
789 bus_wait_for_memory ();
790 goto retry_get_hello_name;
794 _dbus_assert (dbus_error_is_set (&error));
795 _dbus_warn ("Did not get the expected single string argument to hello\n");
800 _dbus_verbose ("Got hello name: %s\n", name);
802 while (!dbus_bus_set_base_service (connection, name))
803 bus_wait_for_memory ();
805 scd.skip_connection = NULL;
807 scd.expected_service_name = name;
808 bus_test_clients_foreach (check_service_created_foreach,
814 /* Client should also have gotten ServiceAcquired */
815 dbus_message_unref (message);
816 message = dbus_connection_pop_message (connection);
819 _dbus_warn ("Expecting %s, got nothing\n",
820 DBUS_MESSAGE_SERVICE_ACQUIRED);
824 retry_get_acquired_name:
825 if (!dbus_message_get_args (message, &error,
826 DBUS_TYPE_STRING, &acquired,
829 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
831 _dbus_verbose ("no memory to get service name arg from acquired\n");
832 dbus_error_free (&error);
833 bus_wait_for_memory ();
834 goto retry_get_acquired_name;
838 _dbus_assert (dbus_error_is_set (&error));
839 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
844 _dbus_verbose ("Got acquired name: %s\n", acquired);
846 if (strcmp (acquired, name) != 0)
848 _dbus_warn ("Acquired name is %s but expected %s\n",
854 if (!check_no_leftovers (context))
860 dbus_error_free (&error);
863 dbus_free (acquired);
866 dbus_message_unref (message);
871 /* returns TRUE if the correct thing happens,
872 * but the correct thing may include OOM errors.
875 check_hello_connection (BusContext *context)
877 DBusConnection *connection;
880 dbus_error_init (&error);
882 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
883 if (connection == NULL)
885 _DBUS_ASSERT_ERROR_IS_SET (&error);
886 dbus_error_free (&error);
890 if (!bus_setup_debug_client (connection))
892 dbus_connection_disconnect (connection);
893 dbus_connection_unref (connection);
897 if (!check_hello_message (context, connection))
900 if (dbus_bus_get_base_service (connection) == NULL)
902 /* We didn't successfully register, so we can't
903 * do the usual kill_client_connection() checks
905 kill_client_connection_unchecked (connection);
909 kill_client_connection (context, connection);
916 check1_try_iterations (BusContext *context,
917 const char *description,
922 /* Run once to see about how many mallocs are involved */
924 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
926 if (! (*func) (context))
927 _dbus_assert_not_reached ("test failed");
929 approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
931 _dbus_verbose ("=================\n%s: about %d mallocs total\n=================\n",
932 description, approx_mallocs);
934 approx_mallocs += 10; /* fudge factor */
936 /* Now run failing each malloc */
938 while (approx_mallocs >= 0)
940 _dbus_set_fail_alloc_counter (approx_mallocs);
942 _dbus_verbose ("\n===\n %s: (will fail malloc %d)\n===\n",
943 description, approx_mallocs);
945 if (! (*func) (context))
946 _dbus_assert_not_reached ("test failed");
948 if (!check_no_leftovers (context))
949 _dbus_assert_not_reached ("Messages were left over, should be covered by test suite");
954 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
956 _dbus_verbose ("=================\n%s: all iterations passed\n=================\n",
961 bus_dispatch_test (const DBusString *test_data_dir)
965 const char *activation_dirs[] = { NULL, NULL };
970 dbus_error_init (&error);
971 context = bus_context_new ("debug-pipe:name=test-server",
975 _dbus_assert_not_reached ("could not alloc context");
977 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
979 _dbus_assert_not_reached ("could not alloc connection");
981 if (!bus_setup_debug_client (foo))
982 _dbus_assert_not_reached ("could not set up connection");
984 if (!check_hello_message (context, foo))
985 _dbus_assert_not_reached ("hello message failed");
987 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
989 _dbus_assert_not_reached ("could not alloc connection");
991 if (!bus_setup_debug_client (bar))
992 _dbus_assert_not_reached ("could not set up connection");
994 if (!check_hello_message (context, bar))
995 _dbus_assert_not_reached ("hello message failed");
997 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
999 _dbus_assert_not_reached ("could not alloc connection");
1001 if (!bus_setup_debug_client (baz))
1002 _dbus_assert_not_reached ("could not set up connection");
1004 if (!check_hello_message (context, baz))
1005 _dbus_assert_not_reached ("hello message failed");
1007 check1_try_iterations (context, "create_and_hello",
1008 check_hello_connection);
1010 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1012 kill_client_connection_unchecked (foo);
1013 kill_client_connection_unchecked (bar);
1014 kill_client_connection_unchecked (baz);
1016 bus_context_unref (context);
1020 #endif /* DBUS_BUILD_TESTS */