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 (DBusConnection *connection,
422 DBusMessage *message,
423 const char *expected)
425 _dbus_warn ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
426 dbus_message_get_interface (message) ?
427 dbus_message_get_interface (message) : "(unset)",
428 dbus_message_get_member (message) ?
429 dbus_message_get_member (message) : "(unset)",
430 dbus_message_get_error_name (message) ?
431 dbus_message_get_error_name (message) : "(unset)",
438 const char *expected_service_name;
440 } CheckServiceDeletedData;
443 check_service_deleted_foreach (DBusConnection *connection,
446 CheckServiceDeletedData *d = data;
447 DBusMessage *message;
451 dbus_error_init (&error);
455 message = pop_message_waiting_for_memory (connection);
458 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
459 connection, "ServiceDeleted");
462 else if (!dbus_message_is_signal (message,
463 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
466 warn_unexpected (connection, message, "ServiceDeleted");
472 if (!dbus_message_get_args (message, &error,
473 DBUS_TYPE_STRING, &service_name,
476 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
478 _dbus_verbose ("no memory to get service name arg\n");
482 _dbus_assert (dbus_error_is_set (&error));
483 _dbus_warn ("Did not get the expected single string argument\n");
487 else if (strcmp (service_name, d->expected_service_name) != 0)
489 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
490 d->expected_service_name,
499 dbus_free (service_name);
500 dbus_error_free (&error);
503 dbus_message_unref (message);
509 kill_client_connection (BusContext *context,
510 DBusConnection *connection)
514 CheckServiceDeletedData csdd;
516 _dbus_verbose ("killing connection %p\n", connection);
518 s = dbus_bus_get_base_service (connection);
519 _dbus_assert (s != NULL);
521 while ((base_service = _dbus_strdup (s)) == NULL)
522 _dbus_wait_for_memory ();
524 dbus_connection_ref (connection);
526 /* kick in the disconnect handler that unrefs the connection */
527 dbus_connection_disconnect (connection);
529 bus_test_run_everything (context);
531 _dbus_assert (bus_test_client_listed (connection));
533 /* Run disconnect handler in test.c */
534 if (bus_connection_dispatch_one_message (connection))
535 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
537 _dbus_assert (!dbus_connection_get_is_connected (connection));
538 dbus_connection_unref (connection);
540 _dbus_assert (!bus_test_client_listed (connection));
542 csdd.expected_service_name = base_service;
545 bus_test_clients_foreach (check_service_deleted_foreach,
548 dbus_free (base_service);
551 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
553 if (!check_no_leftovers (context))
554 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
558 kill_client_connection_unchecked (DBusConnection *connection)
560 /* This kills the connection without expecting it to affect
561 * the rest of the bus.
563 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
565 dbus_connection_ref (connection);
566 dbus_connection_disconnect (connection);
567 /* dispatching disconnect handler will unref once */
568 if (bus_connection_dispatch_one_message (connection))
569 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
571 _dbus_assert (!bus_test_client_listed (connection));
572 dbus_connection_unref (connection);
578 } CheckNoMessagesData;
581 check_no_messages_foreach (DBusConnection *connection,
584 CheckNoMessagesData *d = data;
585 DBusMessage *message;
587 message = pop_message_waiting_for_memory (connection);
590 warn_unexpected (connection, message, "no messages");
596 dbus_message_unref (message);
602 DBusConnection *skip_connection;
603 const char *expected_service_name;
605 } CheckServiceCreatedData;
608 check_service_created_foreach (DBusConnection *connection,
611 CheckServiceCreatedData *d = data;
612 DBusMessage *message;
616 if (connection == d->skip_connection)
619 dbus_error_init (&error);
623 message = pop_message_waiting_for_memory (connection);
626 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
627 connection, "ServiceCreated");
630 else if (!dbus_message_is_signal (message,
631 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
634 warn_unexpected (connection, message, "ServiceCreated");
639 if (!dbus_message_get_args (message, &error,
640 DBUS_TYPE_STRING, &service_name,
643 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
645 _dbus_verbose ("no memory to get service name arg\n");
649 _dbus_assert (dbus_error_is_set (&error));
650 _dbus_warn ("Did not get the expected single string argument\n");
654 else if (strcmp (service_name, d->expected_service_name) != 0)
656 _dbus_warn ("expected creation of service %s, got creation of %s\n",
657 d->expected_service_name,
666 dbus_free (service_name);
667 dbus_error_free (&error);
670 dbus_message_unref (message);
676 check_no_leftovers (BusContext *context)
678 CheckNoMessagesData nmd;
681 bus_test_clients_foreach (check_no_messages_foreach,
690 /* returns TRUE if the correct thing happens,
691 * but the correct thing may include OOM errors.
694 check_hello_message (BusContext *context,
695 DBusConnection *connection)
697 DBusMessage *message;
705 dbus_error_init (&error);
710 message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
712 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
717 if (!dbus_connection_send (connection, message, &serial))
719 dbus_message_unref (message);
723 dbus_message_unref (message);
726 /* send our message */
727 bus_test_run_clients_loop (TRUE);
729 dbus_connection_ref (connection); /* because we may get disconnected */
730 block_connection_until_message_from_bus (context, connection);
732 if (!dbus_connection_get_is_connected (connection))
734 _dbus_verbose ("connection was disconnected\n");
736 dbus_connection_unref (connection);
741 dbus_connection_unref (connection);
743 message = pop_message_waiting_for_memory (connection);
746 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
747 "Hello", serial, connection);
751 _dbus_verbose ("Received message %p on %p\n",
752 message, connection);
754 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
756 _dbus_warn ("Message has wrong sender %s\n",
757 dbus_message_get_sender (message) ?
758 dbus_message_get_sender (message) : "(none)");
762 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
764 if (dbus_message_is_error (message,
765 DBUS_ERROR_NO_MEMORY))
767 ; /* good, this is a valid response */
771 warn_unexpected (connection, message, "not this error");
778 CheckServiceCreatedData scd;
780 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
782 ; /* good, expected */
786 warn_unexpected (connection, message, "method return for Hello");
791 retry_get_hello_name:
792 if (!dbus_message_get_args (message, &error,
793 DBUS_TYPE_STRING, &name,
796 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
798 _dbus_verbose ("no memory to get service name arg from hello\n");
799 dbus_error_free (&error);
800 _dbus_wait_for_memory ();
801 goto retry_get_hello_name;
805 _dbus_assert (dbus_error_is_set (&error));
806 _dbus_warn ("Did not get the expected single string argument to hello\n");
811 _dbus_verbose ("Got hello name: %s\n", name);
813 while (!dbus_bus_set_base_service (connection, name))
814 _dbus_wait_for_memory ();
816 scd.skip_connection = NULL;
818 scd.expected_service_name = name;
819 bus_test_clients_foreach (check_service_created_foreach,
825 /* Client should also have gotten ServiceAcquired */
826 dbus_message_unref (message);
827 message = pop_message_waiting_for_memory (connection);
830 _dbus_warn ("Expecting %s, got nothing\n",
835 retry_get_acquired_name:
836 if (!dbus_message_get_args (message, &error,
837 DBUS_TYPE_STRING, &acquired,
840 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
842 _dbus_verbose ("no memory to get service name arg from acquired\n");
843 dbus_error_free (&error);
844 _dbus_wait_for_memory ();
845 goto retry_get_acquired_name;
849 _dbus_assert (dbus_error_is_set (&error));
850 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
855 _dbus_verbose ("Got acquired name: %s\n", acquired);
857 if (strcmp (acquired, name) != 0)
859 _dbus_warn ("Acquired name is %s but expected %s\n",
865 if (!check_no_leftovers (context))
871 dbus_error_free (&error);
874 dbus_free (acquired);
877 dbus_message_unref (message);
882 /* returns TRUE if the correct thing happens,
883 * but the correct thing may include OOM errors.
886 check_hello_connection (BusContext *context)
888 DBusConnection *connection;
891 dbus_error_init (&error);
893 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
894 if (connection == NULL)
896 _DBUS_ASSERT_ERROR_IS_SET (&error);
897 dbus_error_free (&error);
901 if (!bus_setup_debug_client (connection))
903 dbus_connection_disconnect (connection);
904 dbus_connection_unref (connection);
908 if (!check_hello_message (context, connection))
911 if (dbus_bus_get_base_service (connection) == NULL)
913 /* We didn't successfully register, so we can't
914 * do the usual kill_client_connection() checks
916 kill_client_connection_unchecked (connection);
920 kill_client_connection (context, connection);
926 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
928 /* returns TRUE if the correct thing happens,
929 * but the correct thing may include OOM errors.
932 check_nonexistent_service_activation (BusContext *context,
933 DBusConnection *connection)
935 DBusMessage *message;
940 dbus_error_init (&error);
942 message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
944 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
949 if (!dbus_message_append_args (message,
950 DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
954 dbus_message_unref (message);
958 if (!dbus_connection_send (connection, message, &serial))
960 dbus_message_unref (message);
964 dbus_message_unref (message);
967 bus_test_run_everything (context);
968 block_connection_until_message_from_bus (context, connection);
969 bus_test_run_everything (context);
971 if (!dbus_connection_get_is_connected (connection))
973 _dbus_verbose ("connection was disconnected\n");
979 message = pop_message_waiting_for_memory (connection);
982 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
983 "ActivateService", serial, connection);
987 _dbus_verbose ("Received message %p on %p\n",
988 message, connection);
990 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
992 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
994 _dbus_warn ("Message has wrong sender %s\n",
995 dbus_message_get_sender (message) ?
996 dbus_message_get_sender (message) : "(none)");
1000 if (dbus_message_is_error (message,
1001 DBUS_ERROR_NO_MEMORY))
1003 ; /* good, this is a valid response */
1005 else if (dbus_message_is_error (message,
1006 DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1008 ; /* good, this is expected also */
1012 warn_unexpected (connection, message, "not this error");
1018 _dbus_warn ("Did not expect to successfully activate %s\n",
1019 NONEXISTENT_SERVICE_NAME);
1027 dbus_message_unref (message);
1033 check_base_service_activated (BusContext *context,
1034 DBusConnection *connection,
1035 DBusMessage *initial_message,
1036 char **base_service_p)
1038 DBusMessage *message;
1043 base_service = NULL;
1046 dbus_error_init (&error);
1048 message = initial_message;
1049 dbus_message_ref (message);
1051 if (dbus_message_is_signal (message,
1052 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1056 CheckServiceCreatedData scd;
1058 reget_service_name_arg:
1059 if (!dbus_message_get_args (message, &error,
1060 DBUS_TYPE_STRING, &service_name,
1063 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1065 dbus_error_free (&error);
1066 _dbus_wait_for_memory ();
1067 goto reget_service_name_arg;
1071 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1074 dbus_error_free (&error);
1079 if (*service_name != ':')
1081 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1086 base_service = service_name;
1087 service_name = NULL;
1089 scd.skip_connection = connection;
1091 scd.expected_service_name = base_service;
1092 bus_test_clients_foreach (check_service_created_foreach,
1100 warn_unexpected (connection, message, "ServiceCreated for base service");
1109 *base_service_p = base_service;
1110 base_service = NULL;
1115 dbus_message_unref (message);
1118 dbus_free (base_service);
1124 check_service_activated (BusContext *context,
1125 DBusConnection *connection,
1126 const char *activated_name,
1127 const char *base_service_name,
1128 DBusMessage *initial_message)
1130 DBusMessage *message;
1133 dbus_uint32_t activation_result;
1137 dbus_error_init (&error);
1139 message = initial_message;
1140 dbus_message_ref (message);
1142 if (dbus_message_is_signal (message,
1143 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1147 CheckServiceCreatedData scd;
1149 reget_service_name_arg:
1150 if (!dbus_message_get_args (message, &error,
1151 DBUS_TYPE_STRING, &service_name,
1154 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1156 dbus_error_free (&error);
1157 _dbus_wait_for_memory ();
1158 goto reget_service_name_arg;
1162 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1165 dbus_error_free (&error);
1170 if (strcmp (service_name, activated_name) != 0)
1172 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1173 activated_name, service_name);
1174 dbus_free (service_name);
1178 scd.skip_connection = connection;
1180 scd.expected_service_name = service_name;
1181 bus_test_clients_foreach (check_service_created_foreach,
1184 dbus_free (service_name);
1189 dbus_message_unref (message);
1190 message = pop_message_waiting_for_memory (connection);
1191 if (message == NULL)
1193 _dbus_warn ("Expected a reply to %s, got nothing\n",
1200 warn_unexpected (connection, message, "ServiceCreated for the activated name");
1205 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1207 warn_unexpected (connection, message, "reply to ActivateService");
1212 activation_result = 0;
1213 if (!dbus_message_get_args (message, &error,
1214 DBUS_TYPE_UINT32, &activation_result,
1217 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1219 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1220 "ActivateService", error.message);
1221 dbus_error_free (&error);
1225 dbus_error_free (&error);
1229 if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1231 else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1235 _dbus_warn ("Activation result was 0x%x, no good.\n",
1241 dbus_message_unref (message);
1244 if (!check_no_leftovers (context))
1246 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1254 dbus_message_unref (message);
1260 check_service_deactivated (BusContext *context,
1261 DBusConnection *connection,
1262 const char *activated_name,
1263 const char *base_service)
1265 DBusMessage *message;
1268 CheckServiceDeletedData csdd;
1273 dbus_error_init (&error);
1275 /* Now we are expecting ServiceDeleted messages for the base
1276 * service and the activated_name. The base service
1277 * notification is required to come last.
1279 csdd.expected_service_name = activated_name;
1280 csdd.failed = FALSE;
1281 bus_test_clients_foreach (check_service_deleted_foreach,
1287 csdd.expected_service_name = base_service;
1288 csdd.failed = FALSE;
1289 bus_test_clients_foreach (check_service_deleted_foreach,
1295 if (!check_no_leftovers (context))
1297 _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1305 dbus_message_unref (message);
1311 check_send_exit_to_service (BusContext *context,
1312 DBusConnection *connection,
1313 const char *service_name,
1314 const char *base_service)
1316 dbus_bool_t got_error;
1317 DBusMessage *message;
1318 dbus_int32_t serial;
1321 _dbus_verbose ("Sending exit message to the test service\n");
1325 /* Kill off the test service by sending it a quit message */
1326 message = dbus_message_new_method_call ("org.freedesktop.TestSuite",
1330 if (message == NULL)
1332 /* Do this again; we still need the service to exit... */
1333 if (!check_send_exit_to_service (context, connection,
1334 service_name, base_service))
1340 if (!dbus_connection_send (connection, message, &serial))
1342 dbus_message_unref (message);
1344 /* Do this again; we still need the service to exit... */
1345 if (!check_send_exit_to_service (context, connection,
1346 service_name, base_service))
1352 dbus_message_unref (message);
1356 bus_test_run_clients_loop (TRUE);
1358 /* read it in and write it out to test service */
1359 bus_test_run_bus_loop (context, FALSE);
1361 /* see if we got an error during message bus dispatching */
1362 bus_test_run_clients_loop (FALSE);
1363 message = dbus_connection_borrow_message (connection);
1364 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1367 dbus_connection_return_message (connection, message);
1373 /* If no error, wait for the test service to exit */
1374 block_connection_until_message_from_bus (context, connection);
1376 bus_test_run_everything (context);
1381 message = pop_message_waiting_for_memory (connection);
1382 _dbus_assert (message != NULL);
1384 if (!dbus_message_is_error (message,
1385 DBUS_ERROR_NO_MEMORY))
1387 warn_unexpected (connection, message,
1388 "a no memory error from asking test service to exit");
1392 _dbus_verbose ("Got error %s when asking test service to exit\n",
1393 dbus_message_get_error_name (message));
1395 /* Do this again; we still need the service to exit... */
1396 if (!check_send_exit_to_service (context, connection,
1397 service_name, base_service))
1402 if (!check_service_deactivated (context, connection,
1403 service_name, base_service))
1411 dbus_message_unref (message);
1417 check_got_error (BusContext *context,
1418 DBusConnection *connection,
1419 const char *first_error_name,
1422 DBusMessage *message;
1425 dbus_bool_t error_found;
1426 const char *error_name;
1430 message = pop_message_waiting_for_memory (connection);
1431 if (message == NULL)
1433 _dbus_warn ("Did not get an expected error\n");
1437 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1439 warn_unexpected (connection, message, "an error");
1444 error_found = FALSE;
1446 va_start (ap, first_error_name);
1447 error_name = first_error_name;
1448 while (error_name != NULL)
1450 if (dbus_message_is_error (message, error_name))
1455 error_name = va_arg (ap, char*);
1461 _dbus_warn ("Expected error %s or other, got %s instead\n",
1463 dbus_message_get_error_name (message));
1471 dbus_message_unref (message);
1476 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1478 /* returns TRUE if the correct thing happens,
1479 * but the correct thing may include OOM errors.
1482 check_existent_service_activation (BusContext *context,
1483 DBusConnection *connection)
1485 DBusMessage *message;
1486 dbus_int32_t serial;
1491 base_service = NULL;
1493 dbus_error_init (&error);
1495 message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1497 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
1499 if (message == NULL)
1502 if (!dbus_message_append_args (message,
1503 DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1504 DBUS_TYPE_UINT32, 0,
1507 dbus_message_unref (message);
1511 if (!dbus_connection_send (connection, message, &serial))
1513 dbus_message_unref (message);
1517 dbus_message_unref (message);
1520 bus_test_run_everything (context);
1522 /* now wait for the message bus to hear back from the activated
1525 block_connection_until_message_from_bus (context, connection);
1527 bus_test_run_everything (context);
1529 if (!dbus_connection_get_is_connected (connection))
1531 _dbus_verbose ("connection was disconnected\n");
1537 message = pop_message_waiting_for_memory (connection);
1538 if (message == NULL)
1540 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1541 "ActivateService", serial, connection);
1545 _dbus_verbose ("Received message %p on %p after sending %s\n",
1546 message, connection, "ActivateService");
1548 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1550 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1552 _dbus_warn ("Message has wrong sender %s\n",
1553 dbus_message_get_sender (message) ?
1554 dbus_message_get_sender (message) : "(none)");
1558 if (dbus_message_is_error (message,
1559 DBUS_ERROR_NO_MEMORY))
1561 ; /* good, this is a valid response */
1563 else if (dbus_message_is_error (message,
1564 DBUS_ERROR_SPAWN_CHILD_EXITED))
1566 ; /* good, this is expected also */
1570 _dbus_warn ("Did not expect error %s\n",
1571 dbus_message_get_error_name (message));
1577 dbus_bool_t got_service_deleted;
1578 dbus_bool_t got_error;
1580 if (!check_base_service_activated (context, connection,
1581 message, &base_service))
1584 dbus_message_unref (message);
1587 /* We may need to block here for the test service to exit or finish up */
1588 block_connection_until_message_from_bus (context, connection);
1590 message = dbus_connection_borrow_message (connection);
1591 if (message == NULL)
1593 _dbus_warn ("Did not receive any messages after base service creation notification\n");
1597 got_service_deleted = dbus_message_is_signal (message,
1598 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1600 got_error = dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1602 dbus_connection_return_message (connection, message);
1607 if (!check_got_error (context, connection,
1608 DBUS_ERROR_SPAWN_CHILD_EXITED,
1609 DBUS_ERROR_NO_MEMORY,
1613 /* A service deleted should be coming along now after this error.
1614 * We can also get the error *after* the service deleted.
1616 got_service_deleted = TRUE;
1619 if (got_service_deleted)
1621 /* The service started up and got a base address, but then
1622 * failed to register under EXISTENT_SERVICE_NAME
1624 CheckServiceDeletedData csdd;
1626 csdd.expected_service_name = base_service;
1627 csdd.failed = FALSE;
1628 bus_test_clients_foreach (check_service_deleted_foreach,
1634 /* Now we should get an error about the service exiting
1635 * if we didn't get it before.
1639 block_connection_until_message_from_bus (context, connection);
1641 /* and process everything again */
1642 bus_test_run_everything (context);
1644 if (!check_got_error (context, connection,
1645 DBUS_ERROR_SPAWN_CHILD_EXITED,
1652 message = pop_message_waiting_for_memory (connection);
1653 if (message == NULL)
1655 _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1659 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1660 base_service, message))
1663 dbus_message_unref (message);
1667 if (!check_no_leftovers (context))
1669 _dbus_warn ("Messages were left over after successful activation\n");
1673 if (!check_send_exit_to_service (context, connection,
1674 EXISTENT_SERVICE_NAME, base_service))
1683 dbus_message_unref (message);
1686 dbus_free (base_service);
1691 /* returns TRUE if the correct thing happens,
1692 * but the correct thing may include OOM errors.
1695 check_segfault_service_activation (BusContext *context,
1696 DBusConnection *connection)
1698 DBusMessage *message;
1699 dbus_int32_t serial;
1703 dbus_error_init (&error);
1705 message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1707 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
1709 if (message == NULL)
1712 if (!dbus_message_append_args (message,
1714 "org.freedesktop.DBus.TestSuiteSegfaultService",
1715 DBUS_TYPE_UINT32, 0,
1718 dbus_message_unref (message);
1722 if (!dbus_connection_send (connection, message, &serial))
1724 dbus_message_unref (message);
1728 dbus_message_unref (message);
1731 bus_test_run_everything (context);
1732 block_connection_until_message_from_bus (context, connection);
1733 bus_test_run_everything (context);
1735 if (!dbus_connection_get_is_connected (connection))
1737 _dbus_verbose ("connection was disconnected\n");
1743 message = pop_message_waiting_for_memory (connection);
1744 if (message == NULL)
1746 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1747 "ActivateService", serial, connection);
1751 _dbus_verbose ("Received message %p on %p\n",
1752 message, connection);
1754 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1756 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1758 _dbus_warn ("Message has wrong sender %s\n",
1759 dbus_message_get_sender (message) ?
1760 dbus_message_get_sender (message) : "(none)");
1764 if (dbus_message_is_error (message,
1765 DBUS_ERROR_NO_MEMORY))
1767 ; /* good, this is a valid response */
1769 else if (dbus_message_is_error (message,
1770 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1772 ; /* good, this is expected also */
1776 warn_unexpected (connection, message, "not this error");
1783 _dbus_warn ("Did not expect to successfully activate segfault service\n");
1791 dbus_message_unref (message);
1799 BusContext *context;
1803 check_oom_check1_func (void *data)
1805 Check1Data *d = data;
1807 if (! (* d->func) (d->context))
1810 if (!check_no_leftovers (d->context))
1812 _dbus_warn ("Messages were left over, should be covered by test suite\n");
1820 check1_try_iterations (BusContext *context,
1821 const char *description,
1827 d.context = context;
1829 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1831 _dbus_assert_not_reached ("test failed");
1837 BusContext *context;
1838 DBusConnection *connection;
1842 check_oom_check2_func (void *data)
1844 Check2Data *d = data;
1846 if (! (* d->func) (d->context, d->connection))
1849 if (!check_no_leftovers (d->context))
1851 _dbus_warn ("Messages were left over, should be covered by test suite");
1859 check2_try_iterations (BusContext *context,
1860 DBusConnection *connection,
1861 const char *description,
1867 d.context = context;
1868 d.connection = connection;
1870 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
1872 _dbus_assert_not_reached ("test failed");
1876 bus_dispatch_test (const DBusString *test_data_dir)
1878 BusContext *context;
1879 DBusConnection *foo;
1880 DBusConnection *bar;
1881 DBusConnection *baz;
1884 dbus_error_init (&error);
1886 context = bus_context_new_test (test_data_dir,
1887 "valid-config-files/debug-allow-all.conf");
1888 if (context == NULL)
1891 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1893 _dbus_assert_not_reached ("could not alloc connection");
1895 if (!bus_setup_debug_client (foo))
1896 _dbus_assert_not_reached ("could not set up connection");
1898 if (!check_hello_message (context, foo))
1899 _dbus_assert_not_reached ("hello message failed");
1901 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
1903 _dbus_assert_not_reached ("could not alloc connection");
1905 if (!bus_setup_debug_client (bar))
1906 _dbus_assert_not_reached ("could not set up connection");
1908 if (!check_hello_message (context, bar))
1909 _dbus_assert_not_reached ("hello message failed");
1911 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
1913 _dbus_assert_not_reached ("could not alloc connection");
1915 if (!bus_setup_debug_client (baz))
1916 _dbus_assert_not_reached ("could not set up connection");
1918 if (!check_hello_message (context, baz))
1919 _dbus_assert_not_reached ("hello message failed");
1921 if (!check_no_leftovers (context))
1923 _dbus_warn ("Messages were left over after setting up initial connections");
1924 _dbus_assert_not_reached ("initial connection setup failed");
1927 check1_try_iterations (context, "create_and_hello",
1928 check_hello_connection);
1930 check2_try_iterations (context, foo, "nonexistent_service_activation",
1931 check_nonexistent_service_activation);
1933 check2_try_iterations (context, foo, "segfault_service_activation",
1934 check_segfault_service_activation);
1936 check2_try_iterations (context, foo, "existent_service_activation",
1937 check_existent_service_activation);
1939 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1941 kill_client_connection_unchecked (foo);
1942 kill_client_connection_unchecked (bar);
1943 kill_client_connection_unchecked (baz);
1945 bus_context_unref (context);
1951 bus_dispatch_sha1_test (const DBusString *test_data_dir)
1953 BusContext *context;
1954 DBusConnection *foo;
1957 dbus_error_init (&error);
1959 /* Test SHA1 authentication */
1960 _dbus_verbose ("Testing SHA1 context\n");
1962 context = bus_context_new_test (test_data_dir,
1963 "valid-config-files/debug-allow-all-sha1.conf");
1964 if (context == NULL)
1967 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1969 _dbus_assert_not_reached ("could not alloc connection");
1971 if (!bus_setup_debug_client (foo))
1972 _dbus_assert_not_reached ("could not set up connection");
1974 if (!check_hello_message (context, foo))
1975 _dbus_assert_not_reached ("hello message failed");
1977 if (!check_no_leftovers (context))
1979 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
1980 _dbus_assert_not_reached ("initial connection setup failed");
1983 check1_try_iterations (context, "create_and_hello_sha1",
1984 check_hello_connection);
1986 kill_client_connection_unchecked (foo);
1988 bus_context_unref (context);
1993 #endif /* DBUS_BUILD_TESTS */