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_SERVICE_ORG_FREEDESKTOP_DBUS,
731 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
732 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
738 if (!dbus_connection_send (connection, message, &serial))
740 dbus_message_unref (message);
744 dbus_message_unref (message);
747 /* send our message */
748 bus_test_run_clients_loop (TRUE);
750 dbus_connection_ref (connection); /* because we may get disconnected */
751 block_connection_until_message_from_bus (context, connection);
753 if (!dbus_connection_get_is_connected (connection))
755 _dbus_verbose ("connection was disconnected\n");
757 dbus_connection_unref (connection);
762 dbus_connection_unref (connection);
764 message = pop_message_waiting_for_memory (connection);
767 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
768 "Hello", serial, connection);
772 verbose_message_received (connection, message);
774 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
776 _dbus_warn ("Message has wrong sender %s\n",
777 dbus_message_get_sender (message) ?
778 dbus_message_get_sender (message) : "(none)");
782 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
784 if (dbus_message_is_error (message,
785 DBUS_ERROR_NO_MEMORY))
787 ; /* good, this is a valid response */
791 warn_unexpected (connection, message, "not this error");
798 CheckServiceCreatedData scd;
800 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
802 ; /* good, expected */
806 warn_unexpected (connection, message, "method return for Hello");
811 retry_get_hello_name:
812 if (!dbus_message_get_args (message, &error,
813 DBUS_TYPE_STRING, &name,
816 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
818 _dbus_verbose ("no memory to get service name arg from hello\n");
819 dbus_error_free (&error);
820 _dbus_wait_for_memory ();
821 goto retry_get_hello_name;
825 _dbus_assert (dbus_error_is_set (&error));
826 _dbus_warn ("Did not get the expected single string argument to hello\n");
831 _dbus_verbose ("Got hello name: %s\n", name);
833 while (!dbus_bus_set_base_service (connection, name))
834 _dbus_wait_for_memory ();
836 scd.skip_connection = NULL;
838 scd.expected_service_name = name;
839 bus_test_clients_foreach (check_service_created_foreach,
845 /* Client should also have gotten ServiceAcquired */
846 dbus_message_unref (message);
847 message = pop_message_waiting_for_memory (connection);
850 _dbus_warn ("Expecting %s, got nothing\n",
855 retry_get_acquired_name:
856 if (!dbus_message_get_args (message, &error,
857 DBUS_TYPE_STRING, &acquired,
860 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
862 _dbus_verbose ("no memory to get service name arg from acquired\n");
863 dbus_error_free (&error);
864 _dbus_wait_for_memory ();
865 goto retry_get_acquired_name;
869 _dbus_assert (dbus_error_is_set (&error));
870 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
875 _dbus_verbose ("Got acquired name: %s\n", acquired);
877 if (strcmp (acquired, name) != 0)
879 _dbus_warn ("Acquired name is %s but expected %s\n",
885 if (!check_no_leftovers (context))
891 dbus_error_free (&error);
894 dbus_free (acquired);
897 dbus_message_unref (message);
902 /* returns TRUE if the correct thing happens,
903 * but the correct thing may include OOM errors.
906 check_hello_connection (BusContext *context)
908 DBusConnection *connection;
911 dbus_error_init (&error);
913 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
914 if (connection == NULL)
916 _DBUS_ASSERT_ERROR_IS_SET (&error);
917 dbus_error_free (&error);
921 if (!bus_setup_debug_client (connection))
923 dbus_connection_disconnect (connection);
924 dbus_connection_unref (connection);
928 if (!check_hello_message (context, connection))
931 if (dbus_bus_get_base_service (connection) == NULL)
933 /* We didn't successfully register, so we can't
934 * do the usual kill_client_connection() checks
936 kill_client_connection_unchecked (connection);
940 kill_client_connection (context, connection);
946 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
948 /* returns TRUE if the correct thing happens,
949 * but the correct thing may include OOM errors.
952 check_nonexistent_service_activation (BusContext *context,
953 DBusConnection *connection)
955 DBusMessage *message;
960 dbus_error_init (&error);
962 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
963 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
964 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
970 if (!dbus_message_append_args (message,
971 DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
975 dbus_message_unref (message);
979 if (!dbus_connection_send (connection, message, &serial))
981 dbus_message_unref (message);
985 dbus_message_unref (message);
988 bus_test_run_everything (context);
989 block_connection_until_message_from_bus (context, connection);
990 bus_test_run_everything (context);
992 if (!dbus_connection_get_is_connected (connection))
994 _dbus_verbose ("connection was disconnected\n");
1000 message = pop_message_waiting_for_memory (connection);
1001 if (message == NULL)
1003 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1004 "ActivateService", serial, connection);
1008 verbose_message_received (connection, message);
1010 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1012 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1014 _dbus_warn ("Message has wrong sender %s\n",
1015 dbus_message_get_sender (message) ?
1016 dbus_message_get_sender (message) : "(none)");
1020 if (dbus_message_is_error (message,
1021 DBUS_ERROR_NO_MEMORY))
1023 ; /* good, this is a valid response */
1025 else if (dbus_message_is_error (message,
1026 DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1028 ; /* good, this is expected also */
1032 warn_unexpected (connection, message, "not this error");
1038 _dbus_warn ("Did not expect to successfully activate %s\n",
1039 NONEXISTENT_SERVICE_NAME);
1047 dbus_message_unref (message);
1053 check_base_service_activated (BusContext *context,
1054 DBusConnection *connection,
1055 DBusMessage *initial_message,
1056 char **base_service_p)
1058 DBusMessage *message;
1063 base_service = NULL;
1066 dbus_error_init (&error);
1068 message = initial_message;
1069 dbus_message_ref (message);
1071 if (dbus_message_is_signal (message,
1072 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1076 CheckServiceCreatedData scd;
1078 reget_service_name_arg:
1079 if (!dbus_message_get_args (message, &error,
1080 DBUS_TYPE_STRING, &service_name,
1083 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1085 dbus_error_free (&error);
1086 _dbus_wait_for_memory ();
1087 goto reget_service_name_arg;
1091 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1094 dbus_error_free (&error);
1099 if (*service_name != ':')
1101 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1106 base_service = service_name;
1107 service_name = NULL;
1109 scd.skip_connection = connection;
1111 scd.expected_service_name = base_service;
1112 bus_test_clients_foreach (check_service_created_foreach,
1120 warn_unexpected (connection, message, "ServiceCreated for base service");
1129 *base_service_p = base_service;
1130 base_service = NULL;
1135 dbus_message_unref (message);
1138 dbus_free (base_service);
1144 check_service_activated (BusContext *context,
1145 DBusConnection *connection,
1146 const char *activated_name,
1147 const char *base_service_name,
1148 DBusMessage *initial_message)
1150 DBusMessage *message;
1153 dbus_uint32_t activation_result;
1157 dbus_error_init (&error);
1159 message = initial_message;
1160 dbus_message_ref (message);
1162 if (dbus_message_is_signal (message,
1163 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1167 CheckServiceCreatedData scd;
1169 reget_service_name_arg:
1170 if (!dbus_message_get_args (message, &error,
1171 DBUS_TYPE_STRING, &service_name,
1174 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1176 dbus_error_free (&error);
1177 _dbus_wait_for_memory ();
1178 goto reget_service_name_arg;
1182 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1185 dbus_error_free (&error);
1190 if (strcmp (service_name, activated_name) != 0)
1192 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1193 activated_name, service_name);
1194 dbus_free (service_name);
1198 scd.skip_connection = connection;
1200 scd.expected_service_name = service_name;
1201 bus_test_clients_foreach (check_service_created_foreach,
1204 dbus_free (service_name);
1209 dbus_message_unref (message);
1210 message = pop_message_waiting_for_memory (connection);
1211 if (message == NULL)
1213 _dbus_warn ("Expected a reply to %s, got nothing\n",
1220 warn_unexpected (connection, message, "ServiceCreated for the activated name");
1225 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1227 warn_unexpected (connection, message, "reply to ActivateService");
1232 activation_result = 0;
1233 if (!dbus_message_get_args (message, &error,
1234 DBUS_TYPE_UINT32, &activation_result,
1237 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1239 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1240 "ActivateService", error.message);
1241 dbus_error_free (&error);
1245 dbus_error_free (&error);
1249 if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1251 else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1255 _dbus_warn ("Activation result was 0x%x, no good.\n",
1261 dbus_message_unref (message);
1264 if (!check_no_leftovers (context))
1266 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1274 dbus_message_unref (message);
1280 check_service_deactivated (BusContext *context,
1281 DBusConnection *connection,
1282 const char *activated_name,
1283 const char *base_service)
1285 DBusMessage *message;
1288 CheckServiceDeletedData csdd;
1293 dbus_error_init (&error);
1295 /* Now we are expecting ServiceDeleted messages for the base
1296 * service and the activated_name. The base service
1297 * notification is required to come last.
1299 csdd.expected_service_name = activated_name;
1300 csdd.failed = FALSE;
1301 bus_test_clients_foreach (check_service_deleted_foreach,
1307 csdd.expected_service_name = base_service;
1308 csdd.failed = FALSE;
1309 bus_test_clients_foreach (check_service_deleted_foreach,
1315 if (!check_no_leftovers (context))
1317 _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1325 dbus_message_unref (message);
1331 check_send_exit_to_service (BusContext *context,
1332 DBusConnection *connection,
1333 const char *service_name,
1334 const char *base_service)
1336 dbus_bool_t got_error;
1337 DBusMessage *message;
1338 dbus_int32_t serial;
1341 _dbus_verbose ("Sending exit message to the test service\n");
1345 /* Kill off the test service by sending it a quit message */
1346 message = dbus_message_new_method_call (service_name,
1347 "/org/freedesktop/TestSuite",
1348 "org.freedesktop.TestSuite",
1351 if (message == NULL)
1353 /* Do this again; we still need the service to exit... */
1354 if (!check_send_exit_to_service (context, connection,
1355 service_name, base_service))
1361 if (!dbus_connection_send (connection, message, &serial))
1363 dbus_message_unref (message);
1365 /* Do this again; we still need the service to exit... */
1366 if (!check_send_exit_to_service (context, connection,
1367 service_name, base_service))
1373 dbus_message_unref (message);
1377 bus_test_run_clients_loop (TRUE);
1379 /* read it in and write it out to test service */
1380 bus_test_run_bus_loop (context, FALSE);
1382 /* see if we got an error during message bus dispatching */
1383 bus_test_run_clients_loop (FALSE);
1384 message = dbus_connection_borrow_message (connection);
1385 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1388 dbus_connection_return_message (connection, message);
1394 /* If no error, wait for the test service to exit */
1395 block_connection_until_message_from_bus (context, connection);
1397 bus_test_run_everything (context);
1402 message = pop_message_waiting_for_memory (connection);
1403 _dbus_assert (message != NULL);
1405 if (!dbus_message_is_error (message,
1406 DBUS_ERROR_NO_MEMORY))
1408 warn_unexpected (connection, message,
1409 "a no memory error from asking test service to exit");
1413 _dbus_verbose ("Got error %s when asking test service to exit\n",
1414 dbus_message_get_error_name (message));
1416 /* Do this again; we still need the service to exit... */
1417 if (!check_send_exit_to_service (context, connection,
1418 service_name, base_service))
1423 if (!check_service_deactivated (context, connection,
1424 service_name, base_service))
1432 dbus_message_unref (message);
1438 check_got_error (BusContext *context,
1439 DBusConnection *connection,
1440 const char *first_error_name,
1443 DBusMessage *message;
1446 dbus_bool_t error_found;
1447 const char *error_name;
1451 message = pop_message_waiting_for_memory (connection);
1452 if (message == NULL)
1454 _dbus_warn ("Did not get an expected error\n");
1458 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1460 warn_unexpected (connection, message, "an error");
1465 error_found = FALSE;
1467 va_start (ap, first_error_name);
1468 error_name = first_error_name;
1469 while (error_name != NULL)
1471 if (dbus_message_is_error (message, error_name))
1476 error_name = va_arg (ap, char*);
1482 _dbus_warn ("Expected error %s or other, got %s instead\n",
1484 dbus_message_get_error_name (message));
1492 dbus_message_unref (message);
1497 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1499 /* returns TRUE if the correct thing happens,
1500 * but the correct thing may include OOM errors.
1503 check_existent_service_activation (BusContext *context,
1504 DBusConnection *connection)
1506 DBusMessage *message;
1507 dbus_int32_t serial;
1512 base_service = NULL;
1514 dbus_error_init (&error);
1516 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1517 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1518 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1521 if (message == NULL)
1524 if (!dbus_message_append_args (message,
1525 DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1526 DBUS_TYPE_UINT32, 0,
1529 dbus_message_unref (message);
1533 if (!dbus_connection_send (connection, message, &serial))
1535 dbus_message_unref (message);
1539 dbus_message_unref (message);
1542 bus_test_run_everything (context);
1544 /* now wait for the message bus to hear back from the activated
1547 block_connection_until_message_from_bus (context, connection);
1549 bus_test_run_everything (context);
1551 if (!dbus_connection_get_is_connected (connection))
1553 _dbus_verbose ("connection was disconnected\n");
1559 message = pop_message_waiting_for_memory (connection);
1560 if (message == NULL)
1562 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1563 "ActivateService", serial, connection);
1567 verbose_message_received (connection, message);
1568 _dbus_verbose (" (after sending %s)\n", "ActivateService");
1570 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1572 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1574 _dbus_warn ("Message has wrong sender %s\n",
1575 dbus_message_get_sender (message) ?
1576 dbus_message_get_sender (message) : "(none)");
1580 if (dbus_message_is_error (message,
1581 DBUS_ERROR_NO_MEMORY))
1583 ; /* good, this is a valid response */
1585 else if (dbus_message_is_error (message,
1586 DBUS_ERROR_SPAWN_CHILD_EXITED))
1588 ; /* good, this is expected also */
1592 _dbus_warn ("Did not expect error %s\n",
1593 dbus_message_get_error_name (message));
1599 dbus_bool_t got_service_deleted;
1600 dbus_bool_t got_error;
1602 if (!check_base_service_activated (context, connection,
1603 message, &base_service))
1606 dbus_message_unref (message);
1609 /* We may need to block here for the test service to exit or finish up */
1610 block_connection_until_message_from_bus (context, connection);
1612 message = dbus_connection_borrow_message (connection);
1613 if (message == NULL)
1615 _dbus_warn ("Did not receive any messages after base service creation notification\n");
1619 got_service_deleted = dbus_message_is_signal (message,
1620 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1622 got_error = dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1624 dbus_connection_return_message (connection, message);
1629 if (!check_got_error (context, connection,
1630 DBUS_ERROR_SPAWN_CHILD_EXITED,
1631 DBUS_ERROR_NO_MEMORY,
1635 /* A service deleted should be coming along now after this error.
1636 * We can also get the error *after* the service deleted.
1638 got_service_deleted = TRUE;
1641 if (got_service_deleted)
1643 /* The service started up and got a base address, but then
1644 * failed to register under EXISTENT_SERVICE_NAME
1646 CheckServiceDeletedData csdd;
1648 csdd.expected_service_name = base_service;
1649 csdd.failed = FALSE;
1650 bus_test_clients_foreach (check_service_deleted_foreach,
1656 /* Now we should get an error about the service exiting
1657 * if we didn't get it before.
1661 block_connection_until_message_from_bus (context, connection);
1663 /* and process everything again */
1664 bus_test_run_everything (context);
1666 if (!check_got_error (context, connection,
1667 DBUS_ERROR_SPAWN_CHILD_EXITED,
1674 message = pop_message_waiting_for_memory (connection);
1675 if (message == NULL)
1677 _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1681 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1682 base_service, message))
1685 dbus_message_unref (message);
1689 if (!check_no_leftovers (context))
1691 _dbus_warn ("Messages were left over after successful activation\n");
1695 if (!check_send_exit_to_service (context, connection,
1696 EXISTENT_SERVICE_NAME, base_service))
1705 dbus_message_unref (message);
1708 dbus_free (base_service);
1713 /* returns TRUE if the correct thing happens,
1714 * but the correct thing may include OOM errors.
1717 check_segfault_service_activation (BusContext *context,
1718 DBusConnection *connection)
1720 DBusMessage *message;
1721 dbus_int32_t serial;
1725 dbus_error_init (&error);
1727 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1728 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1729 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1732 if (message == NULL)
1735 if (!dbus_message_append_args (message,
1737 "org.freedesktop.DBus.TestSuiteSegfaultService",
1738 DBUS_TYPE_UINT32, 0,
1741 dbus_message_unref (message);
1745 if (!dbus_connection_send (connection, message, &serial))
1747 dbus_message_unref (message);
1751 dbus_message_unref (message);
1754 bus_test_run_everything (context);
1755 block_connection_until_message_from_bus (context, connection);
1756 bus_test_run_everything (context);
1758 if (!dbus_connection_get_is_connected (connection))
1760 _dbus_verbose ("connection was disconnected\n");
1766 message = pop_message_waiting_for_memory (connection);
1767 if (message == NULL)
1769 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1770 "ActivateService", serial, connection);
1774 verbose_message_received (connection, message);
1776 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1778 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1780 _dbus_warn ("Message has wrong sender %s\n",
1781 dbus_message_get_sender (message) ?
1782 dbus_message_get_sender (message) : "(none)");
1786 if (dbus_message_is_error (message,
1787 DBUS_ERROR_NO_MEMORY))
1789 ; /* good, this is a valid response */
1791 else if (dbus_message_is_error (message,
1792 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1794 ; /* good, this is expected also */
1798 warn_unexpected (connection, message, "not this error");
1805 _dbus_warn ("Did not expect to successfully activate segfault service\n");
1813 dbus_message_unref (message);
1821 BusContext *context;
1825 check_oom_check1_func (void *data)
1827 Check1Data *d = data;
1829 if (! (* d->func) (d->context))
1832 if (!check_no_leftovers (d->context))
1834 _dbus_warn ("Messages were left over, should be covered by test suite\n");
1842 check1_try_iterations (BusContext *context,
1843 const char *description,
1849 d.context = context;
1851 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1853 _dbus_assert_not_reached ("test failed");
1859 BusContext *context;
1860 DBusConnection *connection;
1864 check_oom_check2_func (void *data)
1866 Check2Data *d = data;
1868 if (! (* d->func) (d->context, d->connection))
1871 if (!check_no_leftovers (d->context))
1873 _dbus_warn ("Messages were left over, should be covered by test suite");
1881 check2_try_iterations (BusContext *context,
1882 DBusConnection *connection,
1883 const char *description,
1889 d.context = context;
1890 d.connection = connection;
1892 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
1894 _dbus_assert_not_reached ("test failed");
1898 bus_dispatch_test (const DBusString *test_data_dir)
1900 BusContext *context;
1901 DBusConnection *foo;
1902 DBusConnection *bar;
1903 DBusConnection *baz;
1906 dbus_error_init (&error);
1908 context = bus_context_new_test (test_data_dir,
1909 "valid-config-files/debug-allow-all.conf");
1910 if (context == NULL)
1913 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1915 _dbus_assert_not_reached ("could not alloc connection");
1917 if (!bus_setup_debug_client (foo))
1918 _dbus_assert_not_reached ("could not set up connection");
1920 if (!check_hello_message (context, foo))
1921 _dbus_assert_not_reached ("hello message failed");
1923 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
1925 _dbus_assert_not_reached ("could not alloc connection");
1927 if (!bus_setup_debug_client (bar))
1928 _dbus_assert_not_reached ("could not set up connection");
1930 if (!check_hello_message (context, bar))
1931 _dbus_assert_not_reached ("hello message failed");
1933 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
1935 _dbus_assert_not_reached ("could not alloc connection");
1937 if (!bus_setup_debug_client (baz))
1938 _dbus_assert_not_reached ("could not set up connection");
1940 if (!check_hello_message (context, baz))
1941 _dbus_assert_not_reached ("hello message failed");
1943 if (!check_no_leftovers (context))
1945 _dbus_warn ("Messages were left over after setting up initial connections");
1946 _dbus_assert_not_reached ("initial connection setup failed");
1949 check1_try_iterations (context, "create_and_hello",
1950 check_hello_connection);
1952 check2_try_iterations (context, foo, "nonexistent_service_activation",
1953 check_nonexistent_service_activation);
1955 check2_try_iterations (context, foo, "segfault_service_activation",
1956 check_segfault_service_activation);
1958 check2_try_iterations (context, foo, "existent_service_activation",
1959 check_existent_service_activation);
1961 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1963 kill_client_connection_unchecked (foo);
1964 kill_client_connection_unchecked (bar);
1965 kill_client_connection_unchecked (baz);
1967 bus_context_unref (context);
1973 bus_dispatch_sha1_test (const DBusString *test_data_dir)
1975 BusContext *context;
1976 DBusConnection *foo;
1979 dbus_error_init (&error);
1981 /* Test SHA1 authentication */
1982 _dbus_verbose ("Testing SHA1 context\n");
1984 context = bus_context_new_test (test_data_dir,
1985 "valid-config-files/debug-allow-all-sha1.conf");
1986 if (context == NULL)
1989 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1991 _dbus_assert_not_reached ("could not alloc connection");
1993 if (!bus_setup_debug_client (foo))
1994 _dbus_assert_not_reached ("could not set up connection");
1996 if (!check_hello_message (context, foo))
1997 _dbus_assert_not_reached ("hello message failed");
1999 if (!check_no_leftovers (context))
2001 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
2002 _dbus_assert_not_reached ("initial connection setup failed");
2005 check1_try_iterations (context, "create_and_hello_sha1",
2006 check_hello_connection);
2008 kill_client_connection_unchecked (foo);
2010 bus_context_unref (context);
2015 #endif /* DBUS_BUILD_TESTS */