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"
32 #include <dbus/dbus-internals.h>
35 static dbus_int32_t message_handler_slot = -1;
40 DBusConnection *sender;
42 BusTransaction *transaction;
47 send_one_message (DBusConnection *connection, void *data)
49 SendMessageData *d = data;
51 if (!bus_context_check_security_policy (d->context,
56 return TRUE; /* silently don't send it */
58 if (!bus_transaction_send (d->transaction,
62 BUS_SET_OOM (d->error);
70 bus_dispatch_broadcast_message (BusTransaction *transaction,
71 DBusConnection *sender,
77 BusConnections *connections;
79 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
81 _dbus_assert (dbus_message_get_sender (message) != NULL);
83 connections = bus_transaction_get_connections (transaction);
85 dbus_error_init (&tmp_error);
87 d.context = bus_transaction_get_context (transaction);
89 d.transaction = transaction;
92 bus_connections_foreach_active (connections, send_one_message, &d);
94 if (dbus_error_is_set (&tmp_error))
96 dbus_move_error (&tmp_error, error);
103 static DBusHandlerResult
104 bus_dispatch (DBusConnection *connection,
105 DBusMessage *message)
107 const char *sender, *service_name;
109 BusTransaction *transaction;
111 DBusHandlerResult result;
113 result = DBUS_HANDLER_RESULT_HANDLED;
116 dbus_error_init (&error);
118 context = bus_connection_get_context (connection);
119 _dbus_assert (context != NULL);
121 /* If we can't even allocate an OOM error, we just go to sleep
124 while (!bus_connection_preallocate_oom_error (connection))
125 _dbus_wait_for_memory ();
127 /* Ref connection in case we disconnect it at some point in here */
128 dbus_connection_ref (connection);
130 service_name = dbus_message_get_destination (message);
132 #ifdef DBUS_ENABLE_VERBOSE_MODE
134 const char *interface_name, *member_name, *error_name;
136 interface_name = dbus_message_get_interface (message);
137 member_name = dbus_message_get_member (message);
138 error_name = dbus_message_get_error_name (message);
140 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
141 interface_name ? interface_name : "(no interface)",
142 member_name ? member_name : "(no member)",
143 error_name ? error_name : "(no error name)",
144 service_name ? service_name : "peer");
146 #endif /* DBUS_ENABLE_VERBOSE_MODE */
148 /* If service_name is NULL, this is a message to the bus daemon, not
149 * intended to actually go "on the bus"; e.g. a peer-to-peer
150 * ping. Handle these immediately, especially disconnection
151 * messages. There are no security policy checks on these.
153 if (service_name == NULL)
155 if (dbus_message_is_signal (message,
156 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
158 bus_connection_disconnected (connection);
160 /* DBusConnection also handles some of these automatically, we leave
163 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
167 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
169 /* Create our transaction */
170 transaction = bus_transaction_new (context);
171 if (transaction == NULL)
173 BUS_SET_OOM (&error);
177 /* Assign a sender to the message */
178 if (bus_connection_is_active (connection))
180 sender = bus_connection_get_name (connection);
181 _dbus_assert (sender != NULL);
183 if (!dbus_message_set_sender (message, sender))
185 BUS_SET_OOM (&error);
189 /* We need to refetch the service name here, because
190 * dbus_message_set_sender can cause the header to be
191 * reallocated, and thus the service_name pointer will become
194 service_name = dbus_message_get_destination (message);
197 if (strcmp (service_name, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS) == 0) /* to bus driver */
199 if (!bus_context_check_security_policy (context,
200 connection, NULL, message, &error))
202 _dbus_verbose ("Security policy rejected message\n");
206 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
207 if (!bus_driver_handle_message (connection, transaction, message, &error))
210 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
212 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
213 dbus_connection_disconnect (connection);
215 /* FIXME what if we un-special-case this service and just have a flag
216 * on services that all service owners will get messages to it, not just
219 else if (strcmp (service_name, DBUS_SERVICE_ORG_FREEDESKTOP_BROADCAST) == 0) /* spam! */
221 if (!bus_dispatch_broadcast_message (transaction, connection, message, &error))
224 else /* route to named service */
226 DBusString service_string;
228 BusRegistry *registry;
230 registry = bus_connection_get_registry (connection);
232 _dbus_string_init_const (&service_string, service_name);
233 service = bus_registry_lookup (registry, &service_string);
237 dbus_set_error (&error,
238 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
239 "Service \"%s\" does not exist",
245 DBusConnection *recipient;
247 recipient = bus_service_get_primary_owner (service);
248 _dbus_assert (recipient != NULL);
250 if (!bus_context_check_security_policy (context,
251 connection, recipient, message, &error))
254 /* Dispatch the message */
255 if (!bus_transaction_send (transaction, recipient, message))
257 BUS_SET_OOM (&error);
264 if (dbus_error_is_set (&error))
266 if (!dbus_connection_get_is_connected (connection))
268 /* If we disconnected it, we won't bother to send it any error
271 _dbus_verbose ("Not sending error to connection we disconnected\n");
273 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
275 bus_connection_send_oom_error (connection, message);
277 /* cancel transaction due to OOM */
278 if (transaction != NULL)
280 bus_transaction_cancel_and_free (transaction);
286 /* Try to send the real error, if no mem to do that, send
289 _dbus_assert (transaction != NULL);
291 if (!bus_transaction_send_error_reply (transaction, connection,
294 bus_connection_send_oom_error (connection, message);
296 /* cancel transaction due to OOM */
297 if (transaction != NULL)
299 bus_transaction_cancel_and_free (transaction);
305 dbus_error_free (&error);
308 if (transaction != NULL)
310 bus_transaction_execute_and_free (transaction);
313 dbus_connection_unref (connection);
318 static DBusHandlerResult
319 bus_dispatch_message_handler (DBusMessageHandler *handler,
320 DBusConnection *connection,
321 DBusMessage *message,
324 return bus_dispatch (connection, message);
328 free_message_handler (void *data)
330 DBusMessageHandler *handler = data;
332 _dbus_assert (message_handler_slot >= 0);
334 dbus_message_handler_unref (handler);
335 dbus_connection_free_data_slot (&message_handler_slot);
339 bus_dispatch_add_connection (DBusConnection *connection)
341 DBusMessageHandler *handler;
343 if (!dbus_connection_allocate_data_slot (&message_handler_slot))
346 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
349 dbus_connection_free_data_slot (&message_handler_slot);
353 if (!dbus_connection_add_filter (connection, handler))
355 dbus_message_handler_unref (handler);
356 dbus_connection_free_data_slot (&message_handler_slot);
361 _dbus_assert (message_handler_slot >= 0);
363 if (!dbus_connection_set_data (connection,
364 message_handler_slot,
366 free_message_handler))
368 dbus_message_handler_unref (handler);
369 dbus_connection_free_data_slot (&message_handler_slot);
378 bus_dispatch_remove_connection (DBusConnection *connection)
380 /* Here we tell the bus driver that we want to get off. */
381 bus_driver_remove_connection (connection);
383 dbus_connection_set_data (connection,
384 message_handler_slot,
388 #ifdef DBUS_BUILD_TESTS
390 typedef dbus_bool_t (* Check1Func) (BusContext *context);
391 typedef dbus_bool_t (* Check2Func) (BusContext *context,
392 DBusConnection *connection);
394 static dbus_bool_t check_no_leftovers (BusContext *context);
397 block_connection_until_message_from_bus (BusContext *context,
398 DBusConnection *connection)
400 while (dbus_connection_get_dispatch_status (connection) ==
401 DBUS_DISPATCH_COMPLETE &&
402 dbus_connection_get_is_connected (connection))
404 bus_test_run_bus_loop (context, TRUE);
405 bus_test_run_clients_loop (FALSE);
409 /* compensate for fact that pop_message() can return #NULL due to OOM */
411 pop_message_waiting_for_memory (DBusConnection *connection)
413 while (dbus_connection_get_dispatch_status (connection) ==
414 DBUS_DISPATCH_NEED_MEMORY)
415 _dbus_wait_for_memory ();
417 return dbus_connection_pop_message (connection);
421 warn_unexpected_real (DBusConnection *connection,
422 DBusMessage *message,
423 const char *expected,
424 const char *function,
427 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
429 dbus_message_get_interface (message) ?
430 dbus_message_get_interface (message) : "(unset)",
431 dbus_message_get_member (message) ?
432 dbus_message_get_member (message) : "(unset)",
433 dbus_message_get_error_name (message) ?
434 dbus_message_get_error_name (message) : "(unset)",
439 #define warn_unexpected(connection, message, expected) \
440 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
443 verbose_message_received (DBusConnection *connection,
444 DBusMessage *message)
446 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
447 dbus_message_get_interface (message) ?
448 dbus_message_get_interface (message) : "(unset)",
449 dbus_message_get_member (message) ?
450 dbus_message_get_member (message) : "(unset)",
451 dbus_message_get_error_name (message) ?
452 dbus_message_get_error_name (message) : "(unset)",
458 const char *expected_service_name;
460 } CheckServiceDeletedData;
463 check_service_deleted_foreach (DBusConnection *connection,
466 CheckServiceDeletedData *d = data;
467 DBusMessage *message;
471 dbus_error_init (&error);
475 message = pop_message_waiting_for_memory (connection);
478 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
479 connection, "ServiceDeleted");
482 else if (!dbus_message_is_signal (message,
483 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
486 warn_unexpected (connection, message, "ServiceDeleted");
492 if (!dbus_message_get_args (message, &error,
493 DBUS_TYPE_STRING, &service_name,
496 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
498 _dbus_verbose ("no memory to get service name arg\n");
502 _dbus_assert (dbus_error_is_set (&error));
503 _dbus_warn ("Did not get the expected single string argument\n");
507 else if (strcmp (service_name, d->expected_service_name) != 0)
509 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
510 d->expected_service_name,
519 dbus_free (service_name);
520 dbus_error_free (&error);
523 dbus_message_unref (message);
529 kill_client_connection (BusContext *context,
530 DBusConnection *connection)
534 CheckServiceDeletedData csdd;
536 _dbus_verbose ("killing connection %p\n", connection);
538 s = dbus_bus_get_base_service (connection);
539 _dbus_assert (s != NULL);
541 while ((base_service = _dbus_strdup (s)) == NULL)
542 _dbus_wait_for_memory ();
544 dbus_connection_ref (connection);
546 /* kick in the disconnect handler that unrefs the connection */
547 dbus_connection_disconnect (connection);
549 bus_test_run_everything (context);
551 _dbus_assert (bus_test_client_listed (connection));
553 /* Run disconnect handler in test.c */
554 if (bus_connection_dispatch_one_message (connection))
555 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
557 _dbus_assert (!dbus_connection_get_is_connected (connection));
558 dbus_connection_unref (connection);
560 _dbus_assert (!bus_test_client_listed (connection));
562 csdd.expected_service_name = base_service;
565 bus_test_clients_foreach (check_service_deleted_foreach,
568 dbus_free (base_service);
571 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
573 if (!check_no_leftovers (context))
574 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
578 kill_client_connection_unchecked (DBusConnection *connection)
580 /* This kills the connection without expecting it to affect
581 * the rest of the bus.
583 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
585 dbus_connection_ref (connection);
586 dbus_connection_disconnect (connection);
587 /* dispatching disconnect handler will unref once */
588 if (bus_connection_dispatch_one_message (connection))
589 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
591 _dbus_assert (!bus_test_client_listed (connection));
592 dbus_connection_unref (connection);
598 } CheckNoMessagesData;
601 check_no_messages_foreach (DBusConnection *connection,
604 CheckNoMessagesData *d = data;
605 DBusMessage *message;
607 message = pop_message_waiting_for_memory (connection);
610 warn_unexpected (connection, message, "no messages");
616 dbus_message_unref (message);
622 DBusConnection *skip_connection;
623 const char *expected_service_name;
625 } CheckServiceCreatedData;
628 check_service_created_foreach (DBusConnection *connection,
631 CheckServiceCreatedData *d = data;
632 DBusMessage *message;
636 if (connection == d->skip_connection)
639 dbus_error_init (&error);
643 message = pop_message_waiting_for_memory (connection);
646 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
647 connection, "ServiceCreated");
650 else if (!dbus_message_is_signal (message,
651 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
654 warn_unexpected (connection, message, "ServiceCreated");
659 if (!dbus_message_get_args (message, &error,
660 DBUS_TYPE_STRING, &service_name,
663 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
665 _dbus_verbose ("no memory to get service name arg\n");
669 _dbus_assert (dbus_error_is_set (&error));
670 _dbus_warn ("Did not get the expected single string argument\n");
674 else if (strcmp (service_name, d->expected_service_name) != 0)
676 _dbus_warn ("expected creation of service %s, got creation of %s\n",
677 d->expected_service_name,
686 dbus_free (service_name);
687 dbus_error_free (&error);
690 dbus_message_unref (message);
696 check_no_leftovers (BusContext *context)
698 CheckNoMessagesData nmd;
701 bus_test_clients_foreach (check_no_messages_foreach,
710 /* returns TRUE if the correct thing happens,
711 * but the correct thing may include OOM errors.
714 check_hello_message (BusContext *context,
715 DBusConnection *connection)
717 DBusMessage *message;
725 dbus_error_init (&error);
730 message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
732 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
737 if (!dbus_connection_send (connection, message, &serial))
739 dbus_message_unref (message);
743 dbus_message_unref (message);
746 /* send our message */
747 bus_test_run_clients_loop (TRUE);
749 dbus_connection_ref (connection); /* because we may get disconnected */
750 block_connection_until_message_from_bus (context, connection);
752 if (!dbus_connection_get_is_connected (connection))
754 _dbus_verbose ("connection was disconnected\n");
756 dbus_connection_unref (connection);
761 dbus_connection_unref (connection);
763 message = pop_message_waiting_for_memory (connection);
766 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
767 "Hello", serial, connection);
771 verbose_message_received (connection, message);
773 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
775 _dbus_warn ("Message has wrong sender %s\n",
776 dbus_message_get_sender (message) ?
777 dbus_message_get_sender (message) : "(none)");
781 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
783 if (dbus_message_is_error (message,
784 DBUS_ERROR_NO_MEMORY))
786 ; /* good, this is a valid response */
790 warn_unexpected (connection, message, "not this error");
797 CheckServiceCreatedData scd;
799 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
801 ; /* good, expected */
805 warn_unexpected (connection, message, "method return for Hello");
810 retry_get_hello_name:
811 if (!dbus_message_get_args (message, &error,
812 DBUS_TYPE_STRING, &name,
815 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
817 _dbus_verbose ("no memory to get service name arg from hello\n");
818 dbus_error_free (&error);
819 _dbus_wait_for_memory ();
820 goto retry_get_hello_name;
824 _dbus_assert (dbus_error_is_set (&error));
825 _dbus_warn ("Did not get the expected single string argument to hello\n");
830 _dbus_verbose ("Got hello name: %s\n", name);
832 while (!dbus_bus_set_base_service (connection, name))
833 _dbus_wait_for_memory ();
835 scd.skip_connection = NULL;
837 scd.expected_service_name = name;
838 bus_test_clients_foreach (check_service_created_foreach,
844 /* Client should also have gotten ServiceAcquired */
845 dbus_message_unref (message);
846 message = pop_message_waiting_for_memory (connection);
849 _dbus_warn ("Expecting %s, got nothing\n",
854 retry_get_acquired_name:
855 if (!dbus_message_get_args (message, &error,
856 DBUS_TYPE_STRING, &acquired,
859 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
861 _dbus_verbose ("no memory to get service name arg from acquired\n");
862 dbus_error_free (&error);
863 _dbus_wait_for_memory ();
864 goto retry_get_acquired_name;
868 _dbus_assert (dbus_error_is_set (&error));
869 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
874 _dbus_verbose ("Got acquired name: %s\n", acquired);
876 if (strcmp (acquired, name) != 0)
878 _dbus_warn ("Acquired name is %s but expected %s\n",
884 if (!check_no_leftovers (context))
890 dbus_error_free (&error);
893 dbus_free (acquired);
896 dbus_message_unref (message);
901 /* returns TRUE if the correct thing happens,
902 * but the correct thing may include OOM errors.
905 check_hello_connection (BusContext *context)
907 DBusConnection *connection;
910 dbus_error_init (&error);
912 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
913 if (connection == NULL)
915 _DBUS_ASSERT_ERROR_IS_SET (&error);
916 dbus_error_free (&error);
920 if (!bus_setup_debug_client (connection))
922 dbus_connection_disconnect (connection);
923 dbus_connection_unref (connection);
927 if (!check_hello_message (context, connection))
930 if (dbus_bus_get_base_service (connection) == NULL)
932 /* We didn't successfully register, so we can't
933 * do the usual kill_client_connection() checks
935 kill_client_connection_unchecked (connection);
939 kill_client_connection (context, connection);
945 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
947 /* returns TRUE if the correct thing happens,
948 * but the correct thing may include OOM errors.
951 check_nonexistent_service_activation (BusContext *context,
952 DBusConnection *connection)
954 DBusMessage *message;
959 dbus_error_init (&error);
961 message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
963 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
968 if (!dbus_message_append_args (message,
969 DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
973 dbus_message_unref (message);
977 if (!dbus_connection_send (connection, message, &serial))
979 dbus_message_unref (message);
983 dbus_message_unref (message);
986 bus_test_run_everything (context);
987 block_connection_until_message_from_bus (context, connection);
988 bus_test_run_everything (context);
990 if (!dbus_connection_get_is_connected (connection))
992 _dbus_verbose ("connection was disconnected\n");
998 message = pop_message_waiting_for_memory (connection);
1001 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1002 "ActivateService", serial, connection);
1006 verbose_message_received (connection, message);
1008 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1010 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1012 _dbus_warn ("Message has wrong sender %s\n",
1013 dbus_message_get_sender (message) ?
1014 dbus_message_get_sender (message) : "(none)");
1018 if (dbus_message_is_error (message,
1019 DBUS_ERROR_NO_MEMORY))
1021 ; /* good, this is a valid response */
1023 else if (dbus_message_is_error (message,
1024 DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1026 ; /* good, this is expected also */
1030 warn_unexpected (connection, message, "not this error");
1036 _dbus_warn ("Did not expect to successfully activate %s\n",
1037 NONEXISTENT_SERVICE_NAME);
1045 dbus_message_unref (message);
1051 check_base_service_activated (BusContext *context,
1052 DBusConnection *connection,
1053 DBusMessage *initial_message,
1054 char **base_service_p)
1056 DBusMessage *message;
1061 base_service = NULL;
1064 dbus_error_init (&error);
1066 message = initial_message;
1067 dbus_message_ref (message);
1069 if (dbus_message_is_signal (message,
1070 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1074 CheckServiceCreatedData scd;
1076 reget_service_name_arg:
1077 if (!dbus_message_get_args (message, &error,
1078 DBUS_TYPE_STRING, &service_name,
1081 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1083 dbus_error_free (&error);
1084 _dbus_wait_for_memory ();
1085 goto reget_service_name_arg;
1089 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1092 dbus_error_free (&error);
1097 if (*service_name != ':')
1099 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1104 base_service = service_name;
1105 service_name = NULL;
1107 scd.skip_connection = connection;
1109 scd.expected_service_name = base_service;
1110 bus_test_clients_foreach (check_service_created_foreach,
1118 warn_unexpected (connection, message, "ServiceCreated for base service");
1127 *base_service_p = base_service;
1128 base_service = NULL;
1133 dbus_message_unref (message);
1136 dbus_free (base_service);
1142 check_service_activated (BusContext *context,
1143 DBusConnection *connection,
1144 const char *activated_name,
1145 const char *base_service_name,
1146 DBusMessage *initial_message)
1148 DBusMessage *message;
1151 dbus_uint32_t activation_result;
1155 dbus_error_init (&error);
1157 message = initial_message;
1158 dbus_message_ref (message);
1160 if (dbus_message_is_signal (message,
1161 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1165 CheckServiceCreatedData scd;
1167 reget_service_name_arg:
1168 if (!dbus_message_get_args (message, &error,
1169 DBUS_TYPE_STRING, &service_name,
1172 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1174 dbus_error_free (&error);
1175 _dbus_wait_for_memory ();
1176 goto reget_service_name_arg;
1180 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1183 dbus_error_free (&error);
1188 if (strcmp (service_name, activated_name) != 0)
1190 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1191 activated_name, service_name);
1192 dbus_free (service_name);
1196 scd.skip_connection = connection;
1198 scd.expected_service_name = service_name;
1199 bus_test_clients_foreach (check_service_created_foreach,
1202 dbus_free (service_name);
1207 dbus_message_unref (message);
1208 message = pop_message_waiting_for_memory (connection);
1209 if (message == NULL)
1211 _dbus_warn ("Expected a reply to %s, got nothing\n",
1218 warn_unexpected (connection, message, "ServiceCreated for the activated name");
1223 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1225 warn_unexpected (connection, message, "reply to ActivateService");
1230 activation_result = 0;
1231 if (!dbus_message_get_args (message, &error,
1232 DBUS_TYPE_UINT32, &activation_result,
1235 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1237 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1238 "ActivateService", error.message);
1239 dbus_error_free (&error);
1243 dbus_error_free (&error);
1247 if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1249 else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1253 _dbus_warn ("Activation result was 0x%x, no good.\n",
1259 dbus_message_unref (message);
1262 if (!check_no_leftovers (context))
1264 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1272 dbus_message_unref (message);
1278 check_service_deactivated (BusContext *context,
1279 DBusConnection *connection,
1280 const char *activated_name,
1281 const char *base_service)
1283 DBusMessage *message;
1286 CheckServiceDeletedData csdd;
1291 dbus_error_init (&error);
1293 /* Now we are expecting ServiceDeleted messages for the base
1294 * service and the activated_name. The base service
1295 * notification is required to come last.
1297 csdd.expected_service_name = activated_name;
1298 csdd.failed = FALSE;
1299 bus_test_clients_foreach (check_service_deleted_foreach,
1305 csdd.expected_service_name = base_service;
1306 csdd.failed = FALSE;
1307 bus_test_clients_foreach (check_service_deleted_foreach,
1313 if (!check_no_leftovers (context))
1315 _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1323 dbus_message_unref (message);
1329 check_send_exit_to_service (BusContext *context,
1330 DBusConnection *connection,
1331 const char *service_name,
1332 const char *base_service)
1334 dbus_bool_t got_error;
1335 DBusMessage *message;
1336 dbus_int32_t serial;
1339 _dbus_verbose ("Sending exit message to the test service\n");
1343 /* Kill off the test service by sending it a quit message */
1344 message = dbus_message_new_method_call ("org.freedesktop.TestSuite",
1348 if (message == NULL)
1350 /* Do this again; we still need the service to exit... */
1351 if (!check_send_exit_to_service (context, connection,
1352 service_name, base_service))
1358 if (!dbus_connection_send (connection, message, &serial))
1360 dbus_message_unref (message);
1362 /* Do this again; we still need the service to exit... */
1363 if (!check_send_exit_to_service (context, connection,
1364 service_name, base_service))
1370 dbus_message_unref (message);
1374 bus_test_run_clients_loop (TRUE);
1376 /* read it in and write it out to test service */
1377 bus_test_run_bus_loop (context, FALSE);
1379 /* see if we got an error during message bus dispatching */
1380 bus_test_run_clients_loop (FALSE);
1381 message = dbus_connection_borrow_message (connection);
1382 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1385 dbus_connection_return_message (connection, message);
1391 /* If no error, wait for the test service to exit */
1392 block_connection_until_message_from_bus (context, connection);
1394 bus_test_run_everything (context);
1399 message = pop_message_waiting_for_memory (connection);
1400 _dbus_assert (message != NULL);
1402 if (!dbus_message_is_error (message,
1403 DBUS_ERROR_NO_MEMORY))
1405 warn_unexpected (connection, message,
1406 "a no memory error from asking test service to exit");
1410 _dbus_verbose ("Got error %s when asking test service to exit\n",
1411 dbus_message_get_error_name (message));
1413 /* Do this again; we still need the service to exit... */
1414 if (!check_send_exit_to_service (context, connection,
1415 service_name, base_service))
1420 if (!check_service_deactivated (context, connection,
1421 service_name, base_service))
1429 dbus_message_unref (message);
1435 check_got_error (BusContext *context,
1436 DBusConnection *connection,
1437 const char *first_error_name,
1440 DBusMessage *message;
1443 dbus_bool_t error_found;
1444 const char *error_name;
1448 message = pop_message_waiting_for_memory (connection);
1449 if (message == NULL)
1451 _dbus_warn ("Did not get an expected error\n");
1455 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1457 warn_unexpected (connection, message, "an error");
1462 error_found = FALSE;
1464 va_start (ap, first_error_name);
1465 error_name = first_error_name;
1466 while (error_name != NULL)
1468 if (dbus_message_is_error (message, error_name))
1473 error_name = va_arg (ap, char*);
1479 _dbus_warn ("Expected error %s or other, got %s instead\n",
1481 dbus_message_get_error_name (message));
1489 dbus_message_unref (message);
1494 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1496 /* returns TRUE if the correct thing happens,
1497 * but the correct thing may include OOM errors.
1500 check_existent_service_activation (BusContext *context,
1501 DBusConnection *connection)
1503 DBusMessage *message;
1504 dbus_int32_t serial;
1509 base_service = NULL;
1511 dbus_error_init (&error);
1513 message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1515 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
1517 if (message == NULL)
1520 if (!dbus_message_append_args (message,
1521 DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1522 DBUS_TYPE_UINT32, 0,
1525 dbus_message_unref (message);
1529 if (!dbus_connection_send (connection, message, &serial))
1531 dbus_message_unref (message);
1535 dbus_message_unref (message);
1538 bus_test_run_everything (context);
1540 /* now wait for the message bus to hear back from the activated
1543 block_connection_until_message_from_bus (context, connection);
1545 bus_test_run_everything (context);
1547 if (!dbus_connection_get_is_connected (connection))
1549 _dbus_verbose ("connection was disconnected\n");
1555 message = pop_message_waiting_for_memory (connection);
1556 if (message == NULL)
1558 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1559 "ActivateService", serial, connection);
1563 verbose_message_received (connection, message);
1564 _dbus_verbose (" (after sending %s)\n", "ActivateService");
1566 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1568 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1570 _dbus_warn ("Message has wrong sender %s\n",
1571 dbus_message_get_sender (message) ?
1572 dbus_message_get_sender (message) : "(none)");
1576 if (dbus_message_is_error (message,
1577 DBUS_ERROR_NO_MEMORY))
1579 ; /* good, this is a valid response */
1581 else if (dbus_message_is_error (message,
1582 DBUS_ERROR_SPAWN_CHILD_EXITED))
1584 ; /* good, this is expected also */
1588 _dbus_warn ("Did not expect error %s\n",
1589 dbus_message_get_error_name (message));
1595 dbus_bool_t got_service_deleted;
1596 dbus_bool_t got_error;
1598 if (!check_base_service_activated (context, connection,
1599 message, &base_service))
1602 dbus_message_unref (message);
1605 /* We may need to block here for the test service to exit or finish up */
1606 block_connection_until_message_from_bus (context, connection);
1608 message = dbus_connection_borrow_message (connection);
1609 if (message == NULL)
1611 _dbus_warn ("Did not receive any messages after base service creation notification\n");
1615 got_service_deleted = dbus_message_is_signal (message,
1616 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1618 got_error = dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1620 dbus_connection_return_message (connection, message);
1625 if (!check_got_error (context, connection,
1626 DBUS_ERROR_SPAWN_CHILD_EXITED,
1627 DBUS_ERROR_NO_MEMORY,
1631 /* A service deleted should be coming along now after this error.
1632 * We can also get the error *after* the service deleted.
1634 got_service_deleted = TRUE;
1637 if (got_service_deleted)
1639 /* The service started up and got a base address, but then
1640 * failed to register under EXISTENT_SERVICE_NAME
1642 CheckServiceDeletedData csdd;
1644 csdd.expected_service_name = base_service;
1645 csdd.failed = FALSE;
1646 bus_test_clients_foreach (check_service_deleted_foreach,
1652 /* Now we should get an error about the service exiting
1653 * if we didn't get it before.
1657 block_connection_until_message_from_bus (context, connection);
1659 /* and process everything again */
1660 bus_test_run_everything (context);
1662 if (!check_got_error (context, connection,
1663 DBUS_ERROR_SPAWN_CHILD_EXITED,
1670 message = pop_message_waiting_for_memory (connection);
1671 if (message == NULL)
1673 _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1677 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1678 base_service, message))
1681 dbus_message_unref (message);
1685 if (!check_no_leftovers (context))
1687 _dbus_warn ("Messages were left over after successful activation\n");
1691 if (!check_send_exit_to_service (context, connection,
1692 EXISTENT_SERVICE_NAME, base_service))
1701 dbus_message_unref (message);
1704 dbus_free (base_service);
1709 /* returns TRUE if the correct thing happens,
1710 * but the correct thing may include OOM errors.
1713 check_segfault_service_activation (BusContext *context,
1714 DBusConnection *connection)
1716 DBusMessage *message;
1717 dbus_int32_t serial;
1721 dbus_error_init (&error);
1723 message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1725 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
1727 if (message == NULL)
1730 if (!dbus_message_append_args (message,
1732 "org.freedesktop.DBus.TestSuiteSegfaultService",
1733 DBUS_TYPE_UINT32, 0,
1736 dbus_message_unref (message);
1740 if (!dbus_connection_send (connection, message, &serial))
1742 dbus_message_unref (message);
1746 dbus_message_unref (message);
1749 bus_test_run_everything (context);
1750 block_connection_until_message_from_bus (context, connection);
1751 bus_test_run_everything (context);
1753 if (!dbus_connection_get_is_connected (connection))
1755 _dbus_verbose ("connection was disconnected\n");
1761 message = pop_message_waiting_for_memory (connection);
1762 if (message == NULL)
1764 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1765 "ActivateService", serial, connection);
1769 verbose_message_received (connection, message);
1771 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1773 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1775 _dbus_warn ("Message has wrong sender %s\n",
1776 dbus_message_get_sender (message) ?
1777 dbus_message_get_sender (message) : "(none)");
1781 if (dbus_message_is_error (message,
1782 DBUS_ERROR_NO_MEMORY))
1784 ; /* good, this is a valid response */
1786 else if (dbus_message_is_error (message,
1787 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1789 ; /* good, this is expected also */
1793 warn_unexpected (connection, message, "not this error");
1800 _dbus_warn ("Did not expect to successfully activate segfault service\n");
1808 dbus_message_unref (message);
1816 BusContext *context;
1820 check_oom_check1_func (void *data)
1822 Check1Data *d = data;
1824 if (! (* d->func) (d->context))
1827 if (!check_no_leftovers (d->context))
1829 _dbus_warn ("Messages were left over, should be covered by test suite\n");
1837 check1_try_iterations (BusContext *context,
1838 const char *description,
1844 d.context = context;
1846 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1848 _dbus_assert_not_reached ("test failed");
1854 BusContext *context;
1855 DBusConnection *connection;
1859 check_oom_check2_func (void *data)
1861 Check2Data *d = data;
1863 if (! (* d->func) (d->context, d->connection))
1866 if (!check_no_leftovers (d->context))
1868 _dbus_warn ("Messages were left over, should be covered by test suite");
1876 check2_try_iterations (BusContext *context,
1877 DBusConnection *connection,
1878 const char *description,
1884 d.context = context;
1885 d.connection = connection;
1887 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
1889 _dbus_assert_not_reached ("test failed");
1893 bus_dispatch_test (const DBusString *test_data_dir)
1895 BusContext *context;
1896 DBusConnection *foo;
1897 DBusConnection *bar;
1898 DBusConnection *baz;
1901 dbus_error_init (&error);
1903 context = bus_context_new_test (test_data_dir,
1904 "valid-config-files/debug-allow-all.conf");
1905 if (context == NULL)
1908 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1910 _dbus_assert_not_reached ("could not alloc connection");
1912 if (!bus_setup_debug_client (foo))
1913 _dbus_assert_not_reached ("could not set up connection");
1915 if (!check_hello_message (context, foo))
1916 _dbus_assert_not_reached ("hello message failed");
1918 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
1920 _dbus_assert_not_reached ("could not alloc connection");
1922 if (!bus_setup_debug_client (bar))
1923 _dbus_assert_not_reached ("could not set up connection");
1925 if (!check_hello_message (context, bar))
1926 _dbus_assert_not_reached ("hello message failed");
1928 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
1930 _dbus_assert_not_reached ("could not alloc connection");
1932 if (!bus_setup_debug_client (baz))
1933 _dbus_assert_not_reached ("could not set up connection");
1935 if (!check_hello_message (context, baz))
1936 _dbus_assert_not_reached ("hello message failed");
1938 if (!check_no_leftovers (context))
1940 _dbus_warn ("Messages were left over after setting up initial connections");
1941 _dbus_assert_not_reached ("initial connection setup failed");
1944 check1_try_iterations (context, "create_and_hello",
1945 check_hello_connection);
1947 check2_try_iterations (context, foo, "nonexistent_service_activation",
1948 check_nonexistent_service_activation);
1950 check2_try_iterations (context, foo, "segfault_service_activation",
1951 check_segfault_service_activation);
1953 check2_try_iterations (context, foo, "existent_service_activation",
1954 check_existent_service_activation);
1956 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1958 kill_client_connection_unchecked (foo);
1959 kill_client_connection_unchecked (bar);
1960 kill_client_connection_unchecked (baz);
1962 bus_context_unref (context);
1968 bus_dispatch_sha1_test (const DBusString *test_data_dir)
1970 BusContext *context;
1971 DBusConnection *foo;
1974 dbus_error_init (&error);
1976 /* Test SHA1 authentication */
1977 _dbus_verbose ("Testing SHA1 context\n");
1979 context = bus_context_new_test (test_data_dir,
1980 "valid-config-files/debug-allow-all-sha1.conf");
1981 if (context == NULL)
1984 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1986 _dbus_assert_not_reached ("could not alloc connection");
1988 if (!bus_setup_debug_client (foo))
1989 _dbus_assert_not_reached ("could not set up connection");
1991 if (!check_hello_message (context, foo))
1992 _dbus_assert_not_reached ("hello message failed");
1994 if (!check_no_leftovers (context))
1996 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
1997 _dbus_assert_not_reached ("initial connection setup failed");
2000 check1_try_iterations (context, "create_and_hello_sha1",
2001 check_hello_connection);
2003 kill_client_connection_unchecked (foo);
2005 bus_context_unref (context);
2010 #endif /* DBUS_BUILD_TESTS */