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_ERROR_IS_CLEAR (error);
76 _dbus_assert (dbus_message_get_sender (message) != NULL);
78 connections = bus_transaction_get_connections (transaction);
80 dbus_error_init (&tmp_error);
82 d.transaction = transaction;
85 bus_connections_foreach (connections, send_one_message, &d);
87 if (dbus_error_is_set (&tmp_error))
89 dbus_move_error (&tmp_error, error);
97 send_service_nonexistent_error (BusTransaction *transaction,
98 DBusConnection *connection,
99 const char *service_name,
100 DBusMessage *in_reply_to,
103 DBusMessage *error_reply;
104 DBusString error_message;
105 const char *error_str;
107 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
109 /* Trying to send a message to a non-existant service,
110 * bounce back an error message.
113 if (!_dbus_string_init (&error_message, _DBUS_INT_MAX))
119 if (!_dbus_string_append (&error_message, "Service \"") ||
120 !_dbus_string_append (&error_message, service_name) ||
121 !_dbus_string_append (&error_message, "\" does not exist"))
123 _dbus_string_free (&error_message);
128 _dbus_string_get_const_data (&error_message, &error_str);
129 error_reply = dbus_message_new_error_reply (in_reply_to,
130 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
133 _dbus_string_free (&error_message);
135 if (error_reply == NULL)
141 if (!bus_transaction_send_message (transaction, connection, error_reply))
143 dbus_message_unref (error_reply);
148 dbus_message_unref (error_reply);
154 bus_dispatch (DBusConnection *connection,
155 DBusMessage *message)
157 const char *sender, *service_name, *message_name;
159 BusTransaction *transaction;
163 dbus_error_init (&error);
165 context = bus_connection_get_context (connection);
166 _dbus_assert (context != NULL);
168 /* If we can't even allocate an OOM error, we just go to sleep
171 while (!bus_connection_preallocate_oom_error (connection))
172 bus_wait_for_memory ();
174 /* Ref connection in case we disconnect it at some point in here */
175 dbus_connection_ref (connection);
177 service_name = dbus_message_get_service (message);
178 message_name = dbus_message_get_name (message);
180 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
182 _dbus_verbose ("DISPATCH: %s to %s\n",
183 message_name, service_name ? service_name : "peer");
185 /* If service_name is NULL, this is a message to the bus daemon, not intended
186 * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these
187 * immediately, especially disconnection messages.
189 if (service_name == NULL)
191 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
192 bus_connection_disconnected (connection);
194 /* DBusConnection also handles some of these automatically, we leave
200 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
202 /* Create our transaction */
203 transaction = bus_transaction_new (context);
204 if (transaction == NULL)
206 BUS_SET_OOM (&error);
210 /* Assign a sender to the message */
211 if (bus_connection_is_active (connection))
213 sender = bus_connection_get_name (connection);
214 _dbus_assert (sender != NULL);
216 if (!dbus_message_set_sender (message, sender))
218 BUS_SET_OOM (&error);
222 /* We need to refetch the service name here, because
223 * dbus_message_set_sender can cause the header to be
224 * reallocated, and thus the service_name pointer will become
227 service_name = dbus_message_get_service (message);
230 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
232 if (!bus_driver_handle_message (connection, transaction, message, &error))
235 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
237 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
238 dbus_connection_disconnect (connection);
240 /* FIXME what if we un-special-case this service and just have a flag
241 * on services that all service owners will get messages to it, not just
244 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
246 if (!bus_dispatch_broadcast_message (transaction, message, &error))
249 else /* route to named service */
251 DBusString service_string;
253 BusRegistry *registry;
255 registry = bus_connection_get_registry (connection);
257 _dbus_string_init_const (&service_string, service_name);
258 service = bus_registry_lookup (registry, &service_string);
262 if (!send_service_nonexistent_error (transaction, connection,
269 _dbus_assert (bus_service_get_primary_owner (service) != NULL);
271 /* Dispatch the message */
272 if (!bus_transaction_send_message (transaction,
273 bus_service_get_primary_owner (service),
276 BUS_SET_OOM (&error);
283 if (dbus_error_is_set (&error))
285 if (!dbus_connection_get_is_connected (connection))
287 /* If we disconnected it, we won't bother to send it any error
291 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
293 bus_connection_send_oom_error (connection, message);
295 /* cancel transaction due to OOM */
296 if (transaction != NULL)
298 bus_transaction_cancel_and_free (transaction);
304 /* Try to send the real error, if no mem to do that, send
307 _dbus_assert (transaction != NULL);
309 if (!bus_transaction_send_error_reply (transaction, connection,
312 bus_connection_send_oom_error (connection, message);
314 /* cancel transaction due to OOM */
315 if (transaction != NULL)
317 bus_transaction_cancel_and_free (transaction);
323 dbus_error_free (&error);
326 if (transaction != NULL)
328 bus_transaction_execute_and_free (transaction);
331 dbus_connection_unref (connection);
334 static DBusHandlerResult
335 bus_dispatch_message_handler (DBusMessageHandler *handler,
336 DBusConnection *connection,
337 DBusMessage *message,
340 bus_dispatch (connection, message);
342 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
346 message_handler_slot_ref (void)
348 if (message_handler_slot < 0)
350 message_handler_slot = dbus_connection_allocate_data_slot ();
352 if (message_handler_slot < 0)
355 _dbus_assert (message_handler_slot_refcount == 0);
358 message_handler_slot_refcount += 1;
364 message_handler_slot_unref (void)
366 _dbus_assert (message_handler_slot_refcount > 0);
368 message_handler_slot_refcount -= 1;
370 if (message_handler_slot_refcount == 0)
372 dbus_connection_free_data_slot (message_handler_slot);
373 message_handler_slot = -1;
378 free_message_handler (void *data)
380 DBusMessageHandler *handler = data;
382 _dbus_assert (message_handler_slot >= 0);
383 _dbus_assert (message_handler_slot_refcount > 0);
385 dbus_message_handler_unref (handler);
386 message_handler_slot_unref ();
390 bus_dispatch_add_connection (DBusConnection *connection)
392 DBusMessageHandler *handler;
394 if (!message_handler_slot_ref ())
397 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
400 message_handler_slot_unref ();
404 if (!dbus_connection_add_filter (connection, handler))
406 dbus_message_handler_unref (handler);
407 message_handler_slot_unref ();
412 _dbus_assert (message_handler_slot >= 0);
413 _dbus_assert (message_handler_slot_refcount > 0);
415 if (!dbus_connection_set_data (connection,
416 message_handler_slot,
418 free_message_handler))
420 dbus_message_handler_unref (handler);
421 message_handler_slot_unref ();
430 bus_dispatch_remove_connection (DBusConnection *connection)
432 /* Here we tell the bus driver that we want to get off. */
433 bus_driver_remove_connection (connection);
435 dbus_connection_set_data (connection,
436 message_handler_slot,
440 #ifdef DBUS_BUILD_TESTS
442 typedef dbus_bool_t (* Check1Func) (BusContext *context);
443 typedef dbus_bool_t (* Check2Func) (BusContext *context,
444 DBusConnection *connection);
446 static dbus_bool_t check_no_leftovers (BusContext *context);
450 const char *expected_service_name;
452 } CheckServiceDeletedData;
455 check_service_deleted_foreach (DBusConnection *connection,
458 CheckServiceDeletedData *d = data;
459 DBusMessage *message;
463 dbus_error_init (&error);
467 message = dbus_connection_pop_message (connection);
470 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
471 connection, DBUS_MESSAGE_SERVICE_DELETED);
474 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_DELETED))
476 _dbus_warn ("Received message %s on %p, expecting %s\n",
477 dbus_message_get_name (message),
478 connection, DBUS_MESSAGE_SERVICE_DELETED);
483 if (!dbus_message_get_args (message, &error,
484 DBUS_TYPE_STRING, &service_name,
487 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
489 _dbus_verbose ("no memory to get service name arg\n");
493 _dbus_assert (dbus_error_is_set (&error));
494 _dbus_warn ("Did not get the expected single string argument\n");
498 else if (strcmp (service_name, d->expected_service_name) != 0)
500 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
501 d->expected_service_name,
510 dbus_free (service_name);
511 dbus_error_free (&error);
514 dbus_message_unref (message);
520 kill_client_connection (BusContext *context,
521 DBusConnection *connection)
525 CheckServiceDeletedData csdd;
527 _dbus_verbose ("killing connection %p\n", connection);
529 s = dbus_bus_get_base_service (connection);
530 _dbus_assert (s != NULL);
532 while ((base_service = _dbus_strdup (s)) == NULL)
533 bus_wait_for_memory ();
535 dbus_connection_ref (connection);
537 /* kick in the disconnect handler that unrefs the connection */
538 dbus_connection_disconnect (connection);
540 bus_test_flush_bus (context);
542 _dbus_assert (bus_test_client_listed (connection));
544 /* Run disconnect handler in test.c */
545 if (bus_connection_dispatch_one_message (connection))
546 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
548 _dbus_assert (!dbus_connection_get_is_connected (connection));
549 dbus_connection_unref (connection);
551 _dbus_assert (!bus_test_client_listed (connection));
553 csdd.expected_service_name = base_service;
556 bus_test_clients_foreach (check_service_deleted_foreach,
559 dbus_free (base_service);
562 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
564 if (!check_no_leftovers (context))
565 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
569 kill_client_connection_unchecked (DBusConnection *connection)
571 /* This kills the connection without expecting it to affect
572 * the rest of the bus.
574 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
576 dbus_connection_ref (connection);
577 dbus_connection_disconnect (connection);
578 /* dispatching disconnect handler will unref once */
579 if (bus_connection_dispatch_one_message (connection))
580 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
581 dbus_connection_unref (connection);
582 _dbus_assert (!bus_test_client_listed (connection));
588 } CheckNoMessagesData;
591 check_no_messages_foreach (DBusConnection *connection,
594 CheckNoMessagesData *d = data;
595 DBusMessage *message;
597 message = dbus_connection_pop_message (connection);
600 _dbus_warn ("Received message %s on %p, expecting no messages\n",
601 dbus_message_get_name (message), connection);
606 dbus_message_unref (message);
612 DBusConnection *skip_connection;
613 const char *expected_service_name;
615 } CheckServiceCreatedData;
618 check_service_created_foreach (DBusConnection *connection,
621 CheckServiceCreatedData *d = data;
622 DBusMessage *message;
626 if (connection == d->skip_connection)
629 dbus_error_init (&error);
633 message = dbus_connection_pop_message (connection);
636 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
637 connection, DBUS_MESSAGE_SERVICE_CREATED);
640 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
642 _dbus_warn ("Received message %s on %p, expecting %s\n",
643 dbus_message_get_name (message),
644 connection, DBUS_MESSAGE_SERVICE_CREATED);
649 if (!dbus_message_get_args (message, &error,
650 DBUS_TYPE_STRING, &service_name,
653 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
655 _dbus_verbose ("no memory to get service name arg\n");
659 _dbus_assert (dbus_error_is_set (&error));
660 _dbus_warn ("Did not get the expected single string argument\n");
664 else if (strcmp (service_name, d->expected_service_name) != 0)
666 _dbus_warn ("expected creation of service %s, got creation of %s\n",
667 d->expected_service_name,
676 dbus_free (service_name);
677 dbus_error_free (&error);
680 dbus_message_unref (message);
686 check_no_leftovers (BusContext *context)
688 CheckNoMessagesData nmd;
691 bus_test_clients_foreach (check_no_messages_foreach,
700 /* returns TRUE if the correct thing happens,
701 * but the correct thing may include OOM errors.
704 check_hello_message (BusContext *context,
705 DBusConnection *connection)
707 DBusMessage *message;
714 dbus_error_init (&error);
718 message = dbus_message_new (DBUS_SERVICE_DBUS,
724 if (!dbus_connection_send (connection, message, &serial))
726 dbus_message_unref (message);
730 dbus_message_unref (message);
733 bus_test_flush_bus (context);
735 if (!dbus_connection_get_is_connected (connection))
737 _dbus_verbose ("connection was disconnected\n");
743 message = dbus_connection_pop_message (connection);
746 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
747 DBUS_MESSAGE_HELLO, serial, connection);
751 _dbus_verbose ("Received %s on %p\n",
752 dbus_message_get_name (message), connection);
754 if (dbus_message_get_is_error (message))
756 if (dbus_message_name_is (message,
757 DBUS_ERROR_NO_MEMORY))
759 ; /* good, this is a valid response */
763 _dbus_warn ("Did not expect error %s\n",
764 dbus_message_get_name (message));
770 CheckServiceCreatedData scd;
772 if (dbus_message_name_is (message,
775 ; /* good, expected */
779 _dbus_warn ("Did not expect reply %s\n",
780 dbus_message_get_name (message));
784 retry_get_hello_name:
785 if (!dbus_message_get_args (message, &error,
786 DBUS_TYPE_STRING, &name,
789 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
791 _dbus_verbose ("no memory to get service name arg from hello\n");
792 dbus_error_free (&error);
793 bus_wait_for_memory ();
794 goto retry_get_hello_name;
798 _dbus_assert (dbus_error_is_set (&error));
799 _dbus_warn ("Did not get the expected single string argument to hello\n");
804 _dbus_verbose ("Got hello name: %s\n", name);
806 while (!dbus_bus_set_base_service (connection, name))
807 bus_wait_for_memory ();
809 scd.skip_connection = NULL;
811 scd.expected_service_name = name;
812 bus_test_clients_foreach (check_service_created_foreach,
818 /* Client should also have gotten ServiceAcquired */
819 dbus_message_unref (message);
820 message = dbus_connection_pop_message (connection);
823 _dbus_warn ("Expecting %s, got nothing\n",
824 DBUS_MESSAGE_SERVICE_ACQUIRED);
828 retry_get_acquired_name:
829 if (!dbus_message_get_args (message, &error,
830 DBUS_TYPE_STRING, &acquired,
833 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
835 _dbus_verbose ("no memory to get service name arg from acquired\n");
836 dbus_error_free (&error);
837 bus_wait_for_memory ();
838 goto retry_get_acquired_name;
842 _dbus_assert (dbus_error_is_set (&error));
843 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
848 _dbus_verbose ("Got acquired name: %s\n", acquired);
850 if (strcmp (acquired, name) != 0)
852 _dbus_warn ("Acquired name is %s but expected %s\n",
858 if (!check_no_leftovers (context))
864 dbus_error_free (&error);
867 dbus_free (acquired);
870 dbus_message_unref (message);
875 /* returns TRUE if the correct thing happens,
876 * but the correct thing may include OOM errors.
879 check_hello_connection (BusContext *context)
881 DBusConnection *connection;
884 dbus_error_init (&error);
886 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
887 if (connection == NULL)
889 _DBUS_ASSERT_ERROR_IS_SET (&error);
890 dbus_error_free (&error);
894 if (!bus_setup_debug_client (connection))
896 dbus_connection_disconnect (connection);
897 dbus_connection_unref (connection);
901 if (!check_hello_message (context, connection))
904 if (dbus_bus_get_base_service (connection) == NULL)
906 /* We didn't successfully register, so we can't
907 * do the usual kill_client_connection() checks
909 kill_client_connection_unchecked (connection);
913 kill_client_connection (context, connection);
926 check_oom_check1_func (void *data)
928 Check1Data *d = data;
930 if (! (* d->func) (d->context))
933 if (!check_no_leftovers (d->context))
935 _dbus_warn ("Messages were left over, should be covered by test suite");
943 check1_try_iterations (BusContext *context,
944 const char *description,
952 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
954 _dbus_assert_not_reached ("test failed");
958 bus_dispatch_test (const DBusString *test_data_dir)
966 context = bus_context_new_test (test_data_dir,
967 "valid-config-files/debug-allow-all.conf");
971 dbus_error_init (&error);
973 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
975 _dbus_assert_not_reached ("could not alloc connection");
977 if (!bus_setup_debug_client (foo))
978 _dbus_assert_not_reached ("could not set up connection");
980 if (!check_hello_message (context, foo))
981 _dbus_assert_not_reached ("hello message failed");
983 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
985 _dbus_assert_not_reached ("could not alloc connection");
987 if (!bus_setup_debug_client (bar))
988 _dbus_assert_not_reached ("could not set up connection");
990 if (!check_hello_message (context, bar))
991 _dbus_assert_not_reached ("hello message failed");
993 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
995 _dbus_assert_not_reached ("could not alloc connection");
997 if (!bus_setup_debug_client (baz))
998 _dbus_assert_not_reached ("could not set up connection");
1000 if (!check_hello_message (context, baz))
1001 _dbus_assert_not_reached ("hello message failed");
1003 check1_try_iterations (context, "create_and_hello",
1004 check_hello_connection);
1006 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1008 kill_client_connection_unchecked (foo);
1009 kill_client_connection_unchecked (bar);
1010 kill_client_connection_unchecked (baz);
1012 bus_context_unref (context);
1016 #endif /* DBUS_BUILD_TESTS */