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>
37 send_one_message (DBusConnection *connection,
39 DBusConnection *sender,
40 DBusConnection *addressed_recipient,
42 BusTransaction *transaction,
45 if (!bus_context_check_security_policy (context, transaction,
51 return TRUE; /* silently don't send it */
53 if (!bus_transaction_send (transaction,
65 bus_dispatch_matches (BusTransaction *transaction,
66 DBusConnection *sender,
67 DBusConnection *addressed_recipient,
72 BusConnections *connections;
74 BusMatchmaker *matchmaker;
78 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
80 /* sender and recipient can both be NULL for the bus driver,
81 * or for signals with no particular recipient
84 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
85 _dbus_assert (dbus_message_get_sender (message) != NULL);
87 connections = bus_transaction_get_connections (transaction);
89 dbus_error_init (&tmp_error);
90 context = bus_transaction_get_context (transaction);
91 matchmaker = bus_context_get_matchmaker (context);
94 if (!bus_matchmaker_get_recipients (matchmaker,
95 bus_context_get_connections (context),
96 sender, addressed_recipient, message,
103 link = _dbus_list_get_first_link (&recipients);
106 DBusConnection *dest;
110 if (!send_one_message (dest, context, sender, addressed_recipient,
111 message, transaction, &tmp_error))
114 link = _dbus_list_get_next_link (&recipients, link);
117 _dbus_list_clear (&recipients);
119 if (dbus_error_is_set (&tmp_error))
121 dbus_move_error (&tmp_error, error);
128 static DBusHandlerResult
129 bus_dispatch (DBusConnection *connection,
130 DBusMessage *message)
132 const char *sender, *service_name;
134 BusTransaction *transaction;
136 DBusHandlerResult result;
137 DBusConnection *addressed_recipient;
139 result = DBUS_HANDLER_RESULT_HANDLED;
142 addressed_recipient = NULL;
143 dbus_error_init (&error);
145 context = bus_connection_get_context (connection);
146 _dbus_assert (context != NULL);
148 /* If we can't even allocate an OOM error, we just go to sleep
151 while (!bus_connection_preallocate_oom_error (connection))
152 _dbus_wait_for_memory ();
154 /* Ref connection in case we disconnect it at some point in here */
155 dbus_connection_ref (connection);
157 service_name = dbus_message_get_destination (message);
159 #ifdef DBUS_ENABLE_VERBOSE_MODE
161 const char *interface_name, *member_name, *error_name;
163 interface_name = dbus_message_get_interface (message);
164 member_name = dbus_message_get_member (message);
165 error_name = dbus_message_get_error_name (message);
167 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
168 interface_name ? interface_name : "(no interface)",
169 member_name ? member_name : "(no member)",
170 error_name ? error_name : "(no error name)",
171 service_name ? service_name : "peer");
173 #endif /* DBUS_ENABLE_VERBOSE_MODE */
175 /* If service_name is NULL, if it's a signal we send it to all
176 * connections with a match rule. If it's not a signal, there
177 * are some special cases here but mostly we just bail out.
179 if (service_name == NULL)
181 if (dbus_message_is_signal (message,
182 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
185 bus_connection_disconnected (connection);
189 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
191 /* DBusConnection also handles some of these automatically, we leave
194 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
199 /* Create our transaction */
200 transaction = bus_transaction_new (context);
201 if (transaction == NULL)
203 BUS_SET_OOM (&error);
207 /* Assign a sender to the message */
208 if (bus_connection_is_active (connection))
210 sender = bus_connection_get_name (connection);
211 _dbus_assert (sender != NULL);
213 if (!dbus_message_set_sender (message, sender))
215 BUS_SET_OOM (&error);
219 /* We need to refetch the service name here, because
220 * dbus_message_set_sender can cause the header to be
221 * reallocated, and thus the service_name pointer will become
224 service_name = dbus_message_get_destination (message);
228 strcmp (service_name, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS) == 0) /* to bus driver */
230 if (!bus_context_check_security_policy (context, transaction,
231 connection, NULL, NULL, message, &error))
233 _dbus_verbose ("Security policy rejected message\n");
237 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
238 if (!bus_driver_handle_message (connection, transaction, message, &error))
241 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
243 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
244 dbus_connection_disconnect (connection);
247 else if (service_name != NULL) /* route to named service */
249 DBusString service_string;
251 BusRegistry *registry;
253 _dbus_assert (service_name != NULL);
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 dbus_set_error (&error,
263 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
264 "Service \"%s\" does not exist",
270 addressed_recipient = bus_service_get_primary_owner (service);
271 _dbus_assert (addressed_recipient != NULL);
273 if (!bus_context_check_security_policy (context, transaction,
274 connection, addressed_recipient,
279 /* Dispatch the message */
280 if (!bus_transaction_send (transaction, addressed_recipient, message))
282 BUS_SET_OOM (&error);
288 /* Now match the messages against any match rules, which will send
289 * out signals and such. addressed_recipient may == NULL.
291 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
295 if (dbus_error_is_set (&error))
297 if (!dbus_connection_get_is_connected (connection))
299 /* If we disconnected it, we won't bother to send it any error
302 _dbus_verbose ("Not sending error to connection we disconnected\n");
304 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
306 bus_connection_send_oom_error (connection, message);
308 /* cancel transaction due to OOM */
309 if (transaction != NULL)
311 bus_transaction_cancel_and_free (transaction);
317 /* Try to send the real error, if no mem to do that, send
320 _dbus_assert (transaction != NULL);
322 if (!bus_transaction_send_error_reply (transaction, connection,
325 bus_connection_send_oom_error (connection, message);
327 /* cancel transaction due to OOM */
328 if (transaction != NULL)
330 bus_transaction_cancel_and_free (transaction);
336 dbus_error_free (&error);
339 if (transaction != NULL)
341 bus_transaction_execute_and_free (transaction);
344 dbus_connection_unref (connection);
349 static DBusHandlerResult
350 bus_dispatch_message_filter (DBusConnection *connection,
351 DBusMessage *message,
354 return bus_dispatch (connection, message);
358 bus_dispatch_add_connection (DBusConnection *connection)
360 if (!dbus_connection_add_filter (connection,
361 bus_dispatch_message_filter,
369 bus_dispatch_remove_connection (DBusConnection *connection)
371 /* Here we tell the bus driver that we want to get off. */
372 bus_driver_remove_connection (connection);
374 dbus_connection_remove_filter (connection,
375 bus_dispatch_message_filter,
379 #ifdef DBUS_BUILD_TESTS
381 typedef dbus_bool_t (* Check1Func) (BusContext *context);
382 typedef dbus_bool_t (* Check2Func) (BusContext *context,
383 DBusConnection *connection);
385 static dbus_bool_t check_no_leftovers (BusContext *context);
388 block_connection_until_message_from_bus (BusContext *context,
389 DBusConnection *connection)
391 while (dbus_connection_get_dispatch_status (connection) ==
392 DBUS_DISPATCH_COMPLETE &&
393 dbus_connection_get_is_connected (connection))
395 bus_test_run_bus_loop (context, TRUE);
396 bus_test_run_clients_loop (FALSE);
400 /* compensate for fact that pop_message() can return #NULL due to OOM */
402 pop_message_waiting_for_memory (DBusConnection *connection)
404 while (dbus_connection_get_dispatch_status (connection) ==
405 DBUS_DISPATCH_NEED_MEMORY)
406 _dbus_wait_for_memory ();
408 return dbus_connection_pop_message (connection);
412 warn_unexpected_real (DBusConnection *connection,
413 DBusMessage *message,
414 const char *expected,
415 const char *function,
418 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
420 dbus_message_get_interface (message) ?
421 dbus_message_get_interface (message) : "(unset)",
422 dbus_message_get_member (message) ?
423 dbus_message_get_member (message) : "(unset)",
424 dbus_message_get_error_name (message) ?
425 dbus_message_get_error_name (message) : "(unset)",
430 #define warn_unexpected(connection, message, expected) \
431 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
434 verbose_message_received (DBusConnection *connection,
435 DBusMessage *message)
437 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
438 dbus_message_get_interface (message) ?
439 dbus_message_get_interface (message) : "(unset)",
440 dbus_message_get_member (message) ?
441 dbus_message_get_member (message) : "(unset)",
442 dbus_message_get_error_name (message) ?
443 dbus_message_get_error_name (message) : "(unset)",
449 const char *expected_service_name;
451 } CheckServiceDeletedData;
454 check_service_deleted_foreach (DBusConnection *connection,
457 CheckServiceDeletedData *d = data;
458 DBusMessage *message;
462 dbus_error_init (&error);
466 message = pop_message_waiting_for_memory (connection);
469 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
470 connection, "ServiceDeleted");
473 else if (!dbus_message_is_signal (message,
474 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
477 warn_unexpected (connection, message, "ServiceDeleted");
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 _dbus_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_run_everything (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");
582 _dbus_assert (!bus_test_client_listed (connection));
583 dbus_connection_unref (connection);
589 } CheckNoMessagesData;
592 check_no_messages_foreach (DBusConnection *connection,
595 CheckNoMessagesData *d = data;
596 DBusMessage *message;
598 message = pop_message_waiting_for_memory (connection);
601 warn_unexpected (connection, message, "no messages");
607 dbus_message_unref (message);
613 DBusConnection *skip_connection;
614 const char *expected_service_name;
616 } CheckServiceCreatedData;
619 check_service_created_foreach (DBusConnection *connection,
622 CheckServiceCreatedData *d = data;
623 DBusMessage *message;
627 if (connection == d->skip_connection)
630 dbus_error_init (&error);
634 message = pop_message_waiting_for_memory (connection);
637 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
638 connection, "ServiceCreated");
641 else if (!dbus_message_is_signal (message,
642 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
645 warn_unexpected (connection, message, "ServiceCreated");
650 if (!dbus_message_get_args (message, &error,
651 DBUS_TYPE_STRING, &service_name,
654 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
656 _dbus_verbose ("no memory to get service name arg\n");
660 _dbus_assert (dbus_error_is_set (&error));
661 _dbus_warn ("Did not get the expected single string argument\n");
665 else if (strcmp (service_name, d->expected_service_name) != 0)
667 _dbus_warn ("expected creation of service %s, got creation of %s\n",
668 d->expected_service_name,
677 dbus_free (service_name);
678 dbus_error_free (&error);
681 dbus_message_unref (message);
687 check_no_leftovers (BusContext *context)
689 CheckNoMessagesData nmd;
692 bus_test_clients_foreach (check_no_messages_foreach,
701 /* returns TRUE if the correct thing happens,
702 * but the correct thing may include OOM errors.
705 check_hello_message (BusContext *context,
706 DBusConnection *connection)
708 DBusMessage *message;
709 dbus_uint32_t serial;
716 dbus_error_init (&error);
721 _dbus_verbose ("check_hello_message for %p\n", connection);
723 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
724 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
725 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
731 if (!dbus_connection_send (connection, message, &serial))
733 dbus_message_unref (message);
737 dbus_message_unref (message);
740 /* send our message */
741 bus_test_run_clients_loop (TRUE);
743 dbus_connection_ref (connection); /* because we may get disconnected */
744 block_connection_until_message_from_bus (context, connection);
746 if (!dbus_connection_get_is_connected (connection))
748 _dbus_verbose ("connection was disconnected\n");
750 dbus_connection_unref (connection);
755 dbus_connection_unref (connection);
757 message = pop_message_waiting_for_memory (connection);
760 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
761 "Hello", serial, connection);
765 verbose_message_received (connection, message);
767 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
769 _dbus_warn ("Message has wrong sender %s\n",
770 dbus_message_get_sender (message) ?
771 dbus_message_get_sender (message) : "(none)");
775 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
777 if (dbus_message_is_error (message,
778 DBUS_ERROR_NO_MEMORY))
780 ; /* good, this is a valid response */
784 warn_unexpected (connection, message, "not this error");
791 CheckServiceCreatedData scd;
793 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
795 ; /* good, expected */
799 warn_unexpected (connection, message, "method return for Hello");
804 retry_get_hello_name:
805 if (!dbus_message_get_args (message, &error,
806 DBUS_TYPE_STRING, &name,
809 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
811 _dbus_verbose ("no memory to get service name arg from hello\n");
812 dbus_error_free (&error);
813 _dbus_wait_for_memory ();
814 goto retry_get_hello_name;
818 _dbus_assert (dbus_error_is_set (&error));
819 _dbus_warn ("Did not get the expected single string argument to hello\n");
824 _dbus_verbose ("Got hello name: %s\n", name);
826 while (!dbus_bus_set_base_service (connection, name))
827 _dbus_wait_for_memory ();
829 scd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
831 scd.expected_service_name = name;
832 bus_test_clients_foreach (check_service_created_foreach,
838 /* Client should also have gotten ServiceAcquired */
839 dbus_message_unref (message);
840 message = pop_message_waiting_for_memory (connection);
843 _dbus_warn ("Expecting %s, got nothing\n",
848 retry_get_acquired_name:
849 if (!dbus_message_get_args (message, &error,
850 DBUS_TYPE_STRING, &acquired,
853 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
855 _dbus_verbose ("no memory to get service name arg from acquired\n");
856 dbus_error_free (&error);
857 _dbus_wait_for_memory ();
858 goto retry_get_acquired_name;
862 _dbus_assert (dbus_error_is_set (&error));
863 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
868 _dbus_verbose ("Got acquired name: %s\n", acquired);
870 if (strcmp (acquired, name) != 0)
872 _dbus_warn ("Acquired name is %s but expected %s\n",
878 if (!check_no_leftovers (context))
884 dbus_error_free (&error);
887 dbus_free (acquired);
890 dbus_message_unref (message);
895 /* returns TRUE if the correct thing happens,
896 * but the correct thing may include OOM errors.
899 check_add_match_all (BusContext *context,
900 DBusConnection *connection)
902 DBusMessage *message;
904 dbus_uint32_t serial;
908 dbus_error_init (&error);
911 _dbus_verbose ("check_add_match_all for %p\n", connection);
913 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
914 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
915 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
921 /* empty string match rule matches everything */
922 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, "",
925 dbus_message_unref (message);
929 if (!dbus_connection_send (connection, message, &serial))
931 dbus_message_unref (message);
935 dbus_message_unref (message);
938 /* send our message */
939 bus_test_run_clients_loop (TRUE);
941 dbus_connection_ref (connection); /* because we may get disconnected */
942 block_connection_until_message_from_bus (context, connection);
944 if (!dbus_connection_get_is_connected (connection))
946 _dbus_verbose ("connection was disconnected\n");
948 dbus_connection_unref (connection);
953 dbus_connection_unref (connection);
955 message = pop_message_waiting_for_memory (connection);
958 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
959 "AddMatch", serial, connection);
963 verbose_message_received (connection, message);
965 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
967 _dbus_warn ("Message has wrong sender %s\n",
968 dbus_message_get_sender (message) ?
969 dbus_message_get_sender (message) : "(none)");
973 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
975 if (dbus_message_is_error (message,
976 DBUS_ERROR_NO_MEMORY))
978 ; /* good, this is a valid response */
982 warn_unexpected (connection, message, "not this error");
989 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
991 ; /* good, expected */
992 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
996 warn_unexpected (connection, message, "method return for AddMatch");
1002 if (!check_no_leftovers (context))
1008 dbus_error_free (&error);
1011 dbus_message_unref (message);
1016 /* returns TRUE if the correct thing happens,
1017 * but the correct thing may include OOM errors.
1020 check_hello_connection (BusContext *context)
1022 DBusConnection *connection;
1025 dbus_error_init (&error);
1027 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
1028 if (connection == NULL)
1030 _DBUS_ASSERT_ERROR_IS_SET (&error);
1031 dbus_error_free (&error);
1035 if (!bus_setup_debug_client (connection))
1037 dbus_connection_disconnect (connection);
1038 dbus_connection_unref (connection);
1042 if (!check_hello_message (context, connection))
1045 if (dbus_bus_get_base_service (connection) == NULL)
1047 /* We didn't successfully register, so we can't
1048 * do the usual kill_client_connection() checks
1050 kill_client_connection_unchecked (connection);
1054 if (!check_add_match_all (context, connection))
1057 kill_client_connection (context, connection);
1063 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1065 /* returns TRUE if the correct thing happens,
1066 * but the correct thing may include OOM errors.
1069 check_nonexistent_service_activation (BusContext *context,
1070 DBusConnection *connection)
1072 DBusMessage *message;
1073 dbus_uint32_t serial;
1077 dbus_error_init (&error);
1079 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1080 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1081 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1084 if (message == NULL)
1087 if (!dbus_message_append_args (message,
1088 DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
1089 DBUS_TYPE_UINT32, 0,
1092 dbus_message_unref (message);
1096 if (!dbus_connection_send (connection, message, &serial))
1098 dbus_message_unref (message);
1102 dbus_message_unref (message);
1105 bus_test_run_everything (context);
1106 block_connection_until_message_from_bus (context, connection);
1107 bus_test_run_everything (context);
1109 if (!dbus_connection_get_is_connected (connection))
1111 _dbus_verbose ("connection was disconnected\n");
1117 message = pop_message_waiting_for_memory (connection);
1118 if (message == NULL)
1120 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1121 "ActivateService", serial, connection);
1125 verbose_message_received (connection, message);
1127 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1129 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1131 _dbus_warn ("Message has wrong sender %s\n",
1132 dbus_message_get_sender (message) ?
1133 dbus_message_get_sender (message) : "(none)");
1137 if (dbus_message_is_error (message,
1138 DBUS_ERROR_NO_MEMORY))
1140 ; /* good, this is a valid response */
1142 else if (dbus_message_is_error (message,
1143 DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1145 ; /* good, this is expected also */
1149 warn_unexpected (connection, message, "not this error");
1155 _dbus_warn ("Did not expect to successfully activate %s\n",
1156 NONEXISTENT_SERVICE_NAME);
1164 dbus_message_unref (message);
1170 check_base_service_activated (BusContext *context,
1171 DBusConnection *connection,
1172 DBusMessage *initial_message,
1173 char **base_service_p)
1175 DBusMessage *message;
1180 base_service = NULL;
1183 dbus_error_init (&error);
1185 message = initial_message;
1186 dbus_message_ref (message);
1188 if (dbus_message_is_signal (message,
1189 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1193 CheckServiceCreatedData scd;
1195 reget_service_name_arg:
1196 if (!dbus_message_get_args (message, &error,
1197 DBUS_TYPE_STRING, &service_name,
1200 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1202 dbus_error_free (&error);
1203 _dbus_wait_for_memory ();
1204 goto reget_service_name_arg;
1208 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1211 dbus_error_free (&error);
1216 if (*service_name != ':')
1218 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1223 base_service = service_name;
1224 service_name = NULL;
1226 scd.skip_connection = connection;
1228 scd.expected_service_name = base_service;
1229 bus_test_clients_foreach (check_service_created_foreach,
1237 warn_unexpected (connection, message, "ServiceCreated for base service");
1246 *base_service_p = base_service;
1247 base_service = NULL;
1252 dbus_message_unref (message);
1255 dbus_free (base_service);
1261 check_service_activated (BusContext *context,
1262 DBusConnection *connection,
1263 const char *activated_name,
1264 const char *base_service_name,
1265 DBusMessage *initial_message)
1267 DBusMessage *message;
1270 dbus_uint32_t activation_result;
1274 dbus_error_init (&error);
1276 message = initial_message;
1277 dbus_message_ref (message);
1279 if (dbus_message_is_signal (message,
1280 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1284 CheckServiceCreatedData scd;
1286 reget_service_name_arg:
1287 if (!dbus_message_get_args (message, &error,
1288 DBUS_TYPE_STRING, &service_name,
1291 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1293 dbus_error_free (&error);
1294 _dbus_wait_for_memory ();
1295 goto reget_service_name_arg;
1299 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1302 dbus_error_free (&error);
1307 if (strcmp (service_name, activated_name) != 0)
1309 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1310 activated_name, service_name);
1311 dbus_free (service_name);
1315 scd.skip_connection = connection;
1317 scd.expected_service_name = service_name;
1318 bus_test_clients_foreach (check_service_created_foreach,
1321 dbus_free (service_name);
1326 dbus_message_unref (message);
1327 message = pop_message_waiting_for_memory (connection);
1328 if (message == NULL)
1330 _dbus_warn ("Expected a reply to %s, got nothing\n",
1337 warn_unexpected (connection, message, "ServiceCreated for the activated name");
1342 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1344 warn_unexpected (connection, message, "reply to ActivateService");
1349 activation_result = 0;
1350 if (!dbus_message_get_args (message, &error,
1351 DBUS_TYPE_UINT32, &activation_result,
1354 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1356 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1357 "ActivateService", error.message);
1358 dbus_error_free (&error);
1362 dbus_error_free (&error);
1366 if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1368 else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1372 _dbus_warn ("Activation result was 0x%x, no good.\n",
1378 dbus_message_unref (message);
1381 if (!check_no_leftovers (context))
1383 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1391 dbus_message_unref (message);
1397 check_service_deactivated (BusContext *context,
1398 DBusConnection *connection,
1399 const char *activated_name,
1400 const char *base_service)
1402 DBusMessage *message;
1405 CheckServiceDeletedData csdd;
1410 dbus_error_init (&error);
1412 /* Now we are expecting ServiceDeleted messages for the base
1413 * service and the activated_name. The base service
1414 * notification is required to come last.
1416 csdd.expected_service_name = activated_name;
1417 csdd.failed = FALSE;
1418 bus_test_clients_foreach (check_service_deleted_foreach,
1424 csdd.expected_service_name = base_service;
1425 csdd.failed = FALSE;
1426 bus_test_clients_foreach (check_service_deleted_foreach,
1432 if (!check_no_leftovers (context))
1434 _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1442 dbus_message_unref (message);
1448 check_send_exit_to_service (BusContext *context,
1449 DBusConnection *connection,
1450 const char *service_name,
1451 const char *base_service)
1453 dbus_bool_t got_error;
1454 DBusMessage *message;
1455 dbus_uint32_t serial;
1458 _dbus_verbose ("Sending exit message to the test service\n");
1462 /* Kill off the test service by sending it a quit message */
1463 message = dbus_message_new_method_call (service_name,
1464 "/org/freedesktop/TestSuite",
1465 "org.freedesktop.TestSuite",
1468 if (message == NULL)
1470 /* Do this again; we still need the service to exit... */
1471 if (!check_send_exit_to_service (context, connection,
1472 service_name, base_service))
1478 if (!dbus_connection_send (connection, message, &serial))
1480 dbus_message_unref (message);
1482 /* Do this again; we still need the service to exit... */
1483 if (!check_send_exit_to_service (context, connection,
1484 service_name, base_service))
1490 dbus_message_unref (message);
1494 bus_test_run_clients_loop (TRUE);
1496 /* read it in and write it out to test service */
1497 bus_test_run_bus_loop (context, FALSE);
1499 /* see if we got an error during message bus dispatching */
1500 bus_test_run_clients_loop (FALSE);
1501 message = dbus_connection_borrow_message (connection);
1502 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1505 dbus_connection_return_message (connection, message);
1511 /* If no error, wait for the test service to exit */
1512 block_connection_until_message_from_bus (context, connection);
1514 bus_test_run_everything (context);
1519 message = pop_message_waiting_for_memory (connection);
1520 _dbus_assert (message != NULL);
1522 if (!dbus_message_is_error (message,
1523 DBUS_ERROR_NO_MEMORY))
1525 warn_unexpected (connection, message,
1526 "a no memory error from asking test service to exit");
1530 _dbus_verbose ("Got error %s when asking test service to exit\n",
1531 dbus_message_get_error_name (message));
1533 /* Do this again; we still need the service to exit... */
1534 if (!check_send_exit_to_service (context, connection,
1535 service_name, base_service))
1540 if (!check_service_deactivated (context, connection,
1541 service_name, base_service))
1549 dbus_message_unref (message);
1555 check_got_error (BusContext *context,
1556 DBusConnection *connection,
1557 const char *first_error_name,
1560 DBusMessage *message;
1563 dbus_bool_t error_found;
1564 const char *error_name;
1568 message = pop_message_waiting_for_memory (connection);
1569 if (message == NULL)
1571 _dbus_warn ("Did not get an expected error\n");
1575 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1577 warn_unexpected (connection, message, "an error");
1582 error_found = FALSE;
1584 va_start (ap, first_error_name);
1585 error_name = first_error_name;
1586 while (error_name != NULL)
1588 if (dbus_message_is_error (message, error_name))
1593 error_name = va_arg (ap, char*);
1599 _dbus_warn ("Expected error %s or other, got %s instead\n",
1601 dbus_message_get_error_name (message));
1609 dbus_message_unref (message);
1614 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1616 /* returns TRUE if the correct thing happens,
1617 * but the correct thing may include OOM errors.
1620 check_existent_service_activation (BusContext *context,
1621 DBusConnection *connection)
1623 DBusMessage *message;
1624 dbus_uint32_t serial;
1629 base_service = NULL;
1631 dbus_error_init (&error);
1633 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1634 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1635 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1638 if (message == NULL)
1641 if (!dbus_message_append_args (message,
1642 DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1643 DBUS_TYPE_UINT32, 0,
1646 dbus_message_unref (message);
1650 if (!dbus_connection_send (connection, message, &serial))
1652 dbus_message_unref (message);
1656 dbus_message_unref (message);
1659 bus_test_run_everything (context);
1661 /* now wait for the message bus to hear back from the activated
1664 block_connection_until_message_from_bus (context, connection);
1666 bus_test_run_everything (context);
1668 if (!dbus_connection_get_is_connected (connection))
1670 _dbus_verbose ("connection was disconnected\n");
1676 message = pop_message_waiting_for_memory (connection);
1677 if (message == NULL)
1679 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1680 "ActivateService", serial, connection);
1684 verbose_message_received (connection, message);
1685 _dbus_verbose (" (after sending %s)\n", "ActivateService");
1687 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1689 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1691 _dbus_warn ("Message has wrong sender %s\n",
1692 dbus_message_get_sender (message) ?
1693 dbus_message_get_sender (message) : "(none)");
1697 if (dbus_message_is_error (message,
1698 DBUS_ERROR_NO_MEMORY))
1700 ; /* good, this is a valid response */
1702 else if (dbus_message_is_error (message,
1703 DBUS_ERROR_SPAWN_CHILD_EXITED))
1705 ; /* good, this is expected also */
1709 _dbus_warn ("Did not expect error %s\n",
1710 dbus_message_get_error_name (message));
1716 dbus_bool_t got_service_deleted;
1717 dbus_bool_t got_error;
1719 if (!check_base_service_activated (context, connection,
1720 message, &base_service))
1723 dbus_message_unref (message);
1726 /* We may need to block here for the test service to exit or finish up */
1727 block_connection_until_message_from_bus (context, connection);
1729 message = dbus_connection_borrow_message (connection);
1730 if (message == NULL)
1732 _dbus_warn ("Did not receive any messages after base service creation notification\n");
1736 got_service_deleted = dbus_message_is_signal (message,
1737 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1739 got_error = dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1741 dbus_connection_return_message (connection, message);
1746 if (!check_got_error (context, connection,
1747 DBUS_ERROR_SPAWN_CHILD_EXITED,
1748 DBUS_ERROR_NO_MEMORY,
1752 /* A service deleted should be coming along now after this error.
1753 * We can also get the error *after* the service deleted.
1755 got_service_deleted = TRUE;
1758 if (got_service_deleted)
1760 /* The service started up and got a base address, but then
1761 * failed to register under EXISTENT_SERVICE_NAME
1763 CheckServiceDeletedData csdd;
1765 csdd.expected_service_name = base_service;
1766 csdd.failed = FALSE;
1767 bus_test_clients_foreach (check_service_deleted_foreach,
1773 /* Now we should get an error about the service exiting
1774 * if we didn't get it before.
1778 block_connection_until_message_from_bus (context, connection);
1780 /* and process everything again */
1781 bus_test_run_everything (context);
1783 if (!check_got_error (context, connection,
1784 DBUS_ERROR_SPAWN_CHILD_EXITED,
1791 message = pop_message_waiting_for_memory (connection);
1792 if (message == NULL)
1794 _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1798 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1799 base_service, message))
1802 dbus_message_unref (message);
1806 if (!check_no_leftovers (context))
1808 _dbus_warn ("Messages were left over after successful activation\n");
1812 if (!check_send_exit_to_service (context, connection,
1813 EXISTENT_SERVICE_NAME, base_service))
1822 dbus_message_unref (message);
1825 dbus_free (base_service);
1830 /* returns TRUE if the correct thing happens,
1831 * but the correct thing may include OOM errors.
1834 check_segfault_service_activation (BusContext *context,
1835 DBusConnection *connection)
1837 DBusMessage *message;
1838 dbus_uint32_t serial;
1842 dbus_error_init (&error);
1844 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1845 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1846 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1849 if (message == NULL)
1852 if (!dbus_message_append_args (message,
1854 "org.freedesktop.DBus.TestSuiteSegfaultService",
1855 DBUS_TYPE_UINT32, 0,
1858 dbus_message_unref (message);
1862 if (!dbus_connection_send (connection, message, &serial))
1864 dbus_message_unref (message);
1868 dbus_message_unref (message);
1871 bus_test_run_everything (context);
1872 block_connection_until_message_from_bus (context, connection);
1873 bus_test_run_everything (context);
1875 if (!dbus_connection_get_is_connected (connection))
1877 _dbus_verbose ("connection was disconnected\n");
1883 message = pop_message_waiting_for_memory (connection);
1884 if (message == NULL)
1886 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1887 "ActivateService", serial, connection);
1891 verbose_message_received (connection, message);
1893 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1895 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1897 _dbus_warn ("Message has wrong sender %s\n",
1898 dbus_message_get_sender (message) ?
1899 dbus_message_get_sender (message) : "(none)");
1903 if (dbus_message_is_error (message,
1904 DBUS_ERROR_NO_MEMORY))
1906 ; /* good, this is a valid response */
1908 else if (dbus_message_is_error (message,
1909 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1911 ; /* good, this is expected also */
1915 warn_unexpected (connection, message, "not this error");
1922 _dbus_warn ("Did not expect to successfully activate segfault service\n");
1930 dbus_message_unref (message);
1938 BusContext *context;
1942 check_oom_check1_func (void *data)
1944 Check1Data *d = data;
1946 if (! (* d->func) (d->context))
1949 if (!check_no_leftovers (d->context))
1951 _dbus_warn ("Messages were left over, should be covered by test suite\n");
1959 check1_try_iterations (BusContext *context,
1960 const char *description,
1966 d.context = context;
1968 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1970 _dbus_assert_not_reached ("test failed");
1976 BusContext *context;
1977 DBusConnection *connection;
1981 check_oom_check2_func (void *data)
1983 Check2Data *d = data;
1985 if (! (* d->func) (d->context, d->connection))
1988 if (!check_no_leftovers (d->context))
1990 _dbus_warn ("Messages were left over, should be covered by test suite");
1998 check2_try_iterations (BusContext *context,
1999 DBusConnection *connection,
2000 const char *description,
2006 d.context = context;
2007 d.connection = connection;
2009 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
2011 _dbus_assert_not_reached ("test failed");
2015 bus_dispatch_test (const DBusString *test_data_dir)
2017 BusContext *context;
2018 DBusConnection *foo;
2019 DBusConnection *bar;
2020 DBusConnection *baz;
2023 dbus_error_init (&error);
2025 context = bus_context_new_test (test_data_dir,
2026 "valid-config-files/debug-allow-all.conf");
2027 if (context == NULL)
2030 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
2032 _dbus_assert_not_reached ("could not alloc connection");
2034 if (!bus_setup_debug_client (foo))
2035 _dbus_assert_not_reached ("could not set up connection");
2037 if (!check_hello_message (context, foo))
2038 _dbus_assert_not_reached ("hello message failed");
2040 if (!check_add_match_all (context, foo))
2041 _dbus_assert_not_reached ("AddMatch message failed");
2043 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
2045 _dbus_assert_not_reached ("could not alloc connection");
2047 if (!bus_setup_debug_client (bar))
2048 _dbus_assert_not_reached ("could not set up connection");
2050 if (!check_hello_message (context, bar))
2051 _dbus_assert_not_reached ("hello message failed");
2053 if (!check_add_match_all (context, bar))
2054 _dbus_assert_not_reached ("AddMatch message failed");
2056 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
2058 _dbus_assert_not_reached ("could not alloc connection");
2060 if (!bus_setup_debug_client (baz))
2061 _dbus_assert_not_reached ("could not set up connection");
2063 if (!check_hello_message (context, baz))
2064 _dbus_assert_not_reached ("hello message failed");
2066 if (!check_add_match_all (context, baz))
2067 _dbus_assert_not_reached ("AddMatch message failed");
2069 if (!check_no_leftovers (context))
2071 _dbus_warn ("Messages were left over after setting up initial connections");
2072 _dbus_assert_not_reached ("initial connection setup failed");
2075 check1_try_iterations (context, "create_and_hello",
2076 check_hello_connection);
2078 check2_try_iterations (context, foo, "nonexistent_service_activation",
2079 check_nonexistent_service_activation);
2081 check2_try_iterations (context, foo, "segfault_service_activation",
2082 check_segfault_service_activation);
2084 check2_try_iterations (context, foo, "existent_service_activation",
2085 check_existent_service_activation);
2087 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
2089 kill_client_connection_unchecked (foo);
2090 kill_client_connection_unchecked (bar);
2091 kill_client_connection_unchecked (baz);
2093 bus_context_unref (context);
2099 bus_dispatch_sha1_test (const DBusString *test_data_dir)
2101 BusContext *context;
2102 DBusConnection *foo;
2105 dbus_error_init (&error);
2107 /* Test SHA1 authentication */
2108 _dbus_verbose ("Testing SHA1 context\n");
2110 context = bus_context_new_test (test_data_dir,
2111 "valid-config-files/debug-allow-all-sha1.conf");
2112 if (context == NULL)
2115 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
2117 _dbus_assert_not_reached ("could not alloc connection");
2119 if (!bus_setup_debug_client (foo))
2120 _dbus_assert_not_reached ("could not set up connection");
2122 if (!check_hello_message (context, foo))
2123 _dbus_assert_not_reached ("hello message failed");
2125 if (!check_add_match_all (context, foo))
2126 _dbus_assert_not_reached ("addmatch message failed");
2128 if (!check_no_leftovers (context))
2130 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
2131 _dbus_assert_not_reached ("initial connection setup failed");
2134 check1_try_iterations (context, "create_and_hello_sha1",
2135 check_hello_connection);
2137 kill_client_connection_unchecked (foo);
2139 bus_context_unref (context);
2144 #endif /* DBUS_BUILD_TESTS */