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 int message_handler_slot = -1;
36 static int message_handler_slot_refcount;
41 DBusConnection *sender;
43 BusTransaction *transaction;
48 send_one_message (DBusConnection *connection, void *data)
50 SendMessageData *d = data;
52 if (!bus_connection_is_active (connection))
55 if (!bus_context_check_security_policy (d->context,
60 return TRUE; /* silently don't send it */
62 if (!bus_transaction_send (d->transaction,
66 BUS_SET_OOM (d->error);
74 bus_dispatch_broadcast_message (BusTransaction *transaction,
75 DBusConnection *sender,
81 BusConnections *connections;
83 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
85 _dbus_assert (dbus_message_get_sender (message) != NULL);
87 connections = bus_transaction_get_connections (transaction);
89 dbus_error_init (&tmp_error);
91 d.context = bus_transaction_get_context (transaction);
93 d.transaction = transaction;
96 bus_connections_foreach (connections, send_one_message, &d);
98 if (dbus_error_is_set (&tmp_error))
100 dbus_move_error (&tmp_error, error);
108 bus_dispatch (DBusConnection *connection,
109 DBusMessage *message)
111 const char *sender, *service_name, *message_name;
113 BusTransaction *transaction;
117 dbus_error_init (&error);
119 context = bus_connection_get_context (connection);
120 _dbus_assert (context != NULL);
122 /* If we can't even allocate an OOM error, we just go to sleep
125 while (!bus_connection_preallocate_oom_error (connection))
126 _dbus_wait_for_memory ();
128 /* Ref connection in case we disconnect it at some point in here */
129 dbus_connection_ref (connection);
131 service_name = dbus_message_get_service (message);
132 message_name = dbus_message_get_name (message);
134 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
136 _dbus_verbose ("DISPATCH: %s to %s\n",
137 message_name, service_name ? service_name : "peer");
139 /* If service_name is NULL, this is a message to the bus daemon, not
140 * intended to actually go "on the bus"; e.g. a peer-to-peer
141 * ping. Handle these immediately, especially disconnection
142 * messages. There are no security policy checks on these.
144 if (service_name == NULL)
146 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
147 bus_connection_disconnected (connection);
149 /* DBusConnection also handles some of these automatically, we leave
155 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
157 /* Create our transaction */
158 transaction = bus_transaction_new (context);
159 if (transaction == NULL)
161 BUS_SET_OOM (&error);
165 /* Assign a sender to the message */
166 if (bus_connection_is_active (connection))
168 sender = bus_connection_get_name (connection);
169 _dbus_assert (sender != NULL);
171 if (!dbus_message_set_sender (message, sender))
173 BUS_SET_OOM (&error);
177 /* We need to refetch the service name here, because
178 * dbus_message_set_sender can cause the header to be
179 * reallocated, and thus the service_name pointer will become
182 service_name = dbus_message_get_service (message);
185 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
187 if (!bus_context_check_security_policy (context,
188 connection, NULL, message, &error))
191 if (!bus_driver_handle_message (connection, transaction, message, &error))
194 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
196 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
197 dbus_connection_disconnect (connection);
199 /* FIXME what if we un-special-case this service and just have a flag
200 * on services that all service owners will get messages to it, not just
203 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
205 if (!bus_dispatch_broadcast_message (transaction, connection, message, &error))
208 else /* route to named service */
210 DBusString service_string;
212 BusRegistry *registry;
214 registry = bus_connection_get_registry (connection);
216 _dbus_string_init_const (&service_string, service_name);
217 service = bus_registry_lookup (registry, &service_string);
221 dbus_set_error (&error,
222 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
223 "Service \"%s\" does not exist",
229 DBusConnection *recipient;
231 recipient = bus_service_get_primary_owner (service);
232 _dbus_assert (recipient != NULL);
234 if (!bus_context_check_security_policy (context,
235 connection, recipient, message, &error))
238 /* Dispatch the message */
239 if (!bus_transaction_send (transaction, recipient, message))
241 BUS_SET_OOM (&error);
248 if (dbus_error_is_set (&error))
250 if (!dbus_connection_get_is_connected (connection))
252 /* If we disconnected it, we won't bother to send it any error
256 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
258 bus_connection_send_oom_error (connection, message);
260 /* cancel transaction due to OOM */
261 if (transaction != NULL)
263 bus_transaction_cancel_and_free (transaction);
269 /* Try to send the real error, if no mem to do that, send
272 _dbus_assert (transaction != NULL);
274 if (!bus_transaction_send_error_reply (transaction, connection,
277 bus_connection_send_oom_error (connection, message);
279 /* cancel transaction due to OOM */
280 if (transaction != NULL)
282 bus_transaction_cancel_and_free (transaction);
288 dbus_error_free (&error);
291 if (transaction != NULL)
293 bus_transaction_execute_and_free (transaction);
296 dbus_connection_unref (connection);
299 static DBusHandlerResult
300 bus_dispatch_message_handler (DBusMessageHandler *handler,
301 DBusConnection *connection,
302 DBusMessage *message,
305 bus_dispatch (connection, message);
307 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
311 message_handler_slot_ref (void)
313 if (message_handler_slot < 0)
315 message_handler_slot = dbus_connection_allocate_data_slot ();
317 if (message_handler_slot < 0)
320 _dbus_assert (message_handler_slot_refcount == 0);
323 message_handler_slot_refcount += 1;
329 message_handler_slot_unref (void)
331 _dbus_assert (message_handler_slot_refcount > 0);
333 message_handler_slot_refcount -= 1;
335 if (message_handler_slot_refcount == 0)
337 dbus_connection_free_data_slot (message_handler_slot);
338 message_handler_slot = -1;
343 free_message_handler (void *data)
345 DBusMessageHandler *handler = data;
347 _dbus_assert (message_handler_slot >= 0);
348 _dbus_assert (message_handler_slot_refcount > 0);
350 dbus_message_handler_unref (handler);
351 message_handler_slot_unref ();
355 bus_dispatch_add_connection (DBusConnection *connection)
357 DBusMessageHandler *handler;
359 if (!message_handler_slot_ref ())
362 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
365 message_handler_slot_unref ();
369 if (!dbus_connection_add_filter (connection, handler))
371 dbus_message_handler_unref (handler);
372 message_handler_slot_unref ();
377 _dbus_assert (message_handler_slot >= 0);
378 _dbus_assert (message_handler_slot_refcount > 0);
380 if (!dbus_connection_set_data (connection,
381 message_handler_slot,
383 free_message_handler))
385 dbus_message_handler_unref (handler);
386 message_handler_slot_unref ();
395 bus_dispatch_remove_connection (DBusConnection *connection)
397 /* Here we tell the bus driver that we want to get off. */
398 bus_driver_remove_connection (connection);
400 dbus_connection_set_data (connection,
401 message_handler_slot,
405 #ifdef DBUS_BUILD_TESTS
407 typedef dbus_bool_t (* Check1Func) (BusContext *context);
408 typedef dbus_bool_t (* Check2Func) (BusContext *context,
409 DBusConnection *connection);
411 static dbus_bool_t check_no_leftovers (BusContext *context);
414 block_connection_until_message_from_bus (BusContext *context,
415 DBusConnection *connection)
417 while (dbus_connection_get_dispatch_status (connection) ==
418 DBUS_DISPATCH_COMPLETE &&
419 dbus_connection_get_is_connected (connection))
421 bus_test_run_bus_loop (context, TRUE);
422 bus_test_run_clients_loop (FALSE);
426 /* compensate for fact that pop_message() can return #NULL due to OOM */
428 pop_message_waiting_for_memory (DBusConnection *connection)
430 while (dbus_connection_get_dispatch_status (connection) ==
431 DBUS_DISPATCH_NEED_MEMORY)
432 _dbus_wait_for_memory ();
434 return dbus_connection_pop_message (connection);
439 const char *expected_service_name;
441 } CheckServiceDeletedData;
444 check_service_deleted_foreach (DBusConnection *connection,
447 CheckServiceDeletedData *d = data;
448 DBusMessage *message;
452 dbus_error_init (&error);
456 message = pop_message_waiting_for_memory (connection);
459 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
460 connection, DBUS_MESSAGE_SERVICE_DELETED);
463 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_DELETED))
465 _dbus_warn ("Received message %s on %p, expecting %s\n",
466 dbus_message_get_name (message),
467 connection, DBUS_MESSAGE_SERVICE_DELETED);
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 _dbus_warn ("Received message %s on %p, expecting no messages\n",
591 dbus_message_get_name (message), connection);
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, DBUS_MESSAGE_SERVICE_CREATED);
630 else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
632 _dbus_warn ("Received message %s on %p, expecting %s\n",
633 dbus_message_get_name (message),
634 connection, DBUS_MESSAGE_SERVICE_CREATED);
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 (DBUS_SERVICE_DBUS,
716 if (!dbus_connection_send (connection, message, &serial))
718 dbus_message_unref (message);
722 dbus_message_unref (message);
725 /* send our message */
726 bus_test_run_clients_loop (TRUE);
728 dbus_connection_ref (connection); /* because we may get disconnected */
729 block_connection_until_message_from_bus (context, connection);
731 if (!dbus_connection_get_is_connected (connection))
733 _dbus_verbose ("connection was disconnected\n");
735 dbus_connection_unref (connection);
740 dbus_connection_unref (connection);
742 message = pop_message_waiting_for_memory (connection);
745 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
746 DBUS_MESSAGE_HELLO, serial, connection);
750 _dbus_verbose ("Received %s on %p\n",
751 dbus_message_get_name (message), connection);
753 if (!dbus_message_sender_is (message, DBUS_SERVICE_DBUS))
755 _dbus_warn ("Message has wrong sender %s\n",
756 dbus_message_get_sender (message) ?
757 dbus_message_get_sender (message) : "(none)");
761 if (dbus_message_get_is_error (message))
763 if (dbus_message_name_is (message,
764 DBUS_ERROR_NO_MEMORY))
766 ; /* good, this is a valid response */
770 _dbus_warn ("Did not expect error %s\n",
771 dbus_message_get_name (message));
777 CheckServiceCreatedData scd;
779 if (dbus_message_name_is (message,
782 ; /* good, expected */
786 _dbus_warn ("Did not expect reply %s\n",
787 dbus_message_get_name (message));
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",
831 DBUS_MESSAGE_SERVICE_ACQUIRED);
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 (DBUS_SERVICE_DBUS,
943 DBUS_MESSAGE_ACTIVATE_SERVICE);
948 if (!dbus_message_append_args (message,
949 DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
953 dbus_message_unref (message);
957 if (!dbus_connection_send (connection, message, &serial))
959 dbus_message_unref (message);
963 dbus_message_unref (message);
966 bus_test_run_everything (context);
967 block_connection_until_message_from_bus (context, connection);
968 bus_test_run_everything (context);
970 if (!dbus_connection_get_is_connected (connection))
972 _dbus_verbose ("connection was disconnected\n");
978 message = pop_message_waiting_for_memory (connection);
981 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
982 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
986 _dbus_verbose ("Received %s on %p\n",
987 dbus_message_get_name (message), connection);
989 if (dbus_message_get_is_error (message))
991 if (!dbus_message_sender_is (message, DBUS_SERVICE_DBUS))
993 _dbus_warn ("Message has wrong sender %s\n",
994 dbus_message_get_sender (message) ?
995 dbus_message_get_sender (message) : "(none)");
999 if (dbus_message_name_is (message,
1000 DBUS_ERROR_NO_MEMORY))
1002 ; /* good, this is a valid response */
1004 else if (dbus_message_name_is (message,
1005 DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1007 ; /* good, this is expected also */
1011 _dbus_warn ("Did not expect error %s\n",
1012 dbus_message_get_name (message));
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_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
1054 CheckServiceCreatedData scd;
1056 reget_service_name_arg:
1057 if (!dbus_message_get_args (message, &error,
1058 DBUS_TYPE_STRING, &service_name,
1061 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1063 dbus_error_free (&error);
1064 _dbus_wait_for_memory ();
1065 goto reget_service_name_arg;
1069 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1070 dbus_message_get_name (message),
1072 dbus_error_free (&error);
1077 if (*service_name != ':')
1079 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1084 base_service = service_name;
1085 service_name = NULL;
1087 scd.skip_connection = connection;
1089 scd.expected_service_name = base_service;
1090 bus_test_clients_foreach (check_service_created_foreach,
1098 _dbus_warn ("Expected to get base service ServiceCreated, instead got %s\n",
1099 dbus_message_get_name (message));
1107 *base_service_p = base_service;
1108 base_service = NULL;
1113 dbus_message_unref (message);
1116 dbus_free (base_service);
1122 check_service_activated (BusContext *context,
1123 DBusConnection *connection,
1124 const char *activated_name,
1125 const char *base_service_name,
1126 DBusMessage *initial_message)
1128 DBusMessage *message;
1131 dbus_uint32_t activation_result;
1135 dbus_error_init (&error);
1137 message = initial_message;
1138 dbus_message_ref (message);
1140 if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
1143 CheckServiceCreatedData scd;
1145 reget_service_name_arg:
1146 if (!dbus_message_get_args (message, &error,
1147 DBUS_TYPE_STRING, &service_name,
1150 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1152 dbus_error_free (&error);
1153 _dbus_wait_for_memory ();
1154 goto reget_service_name_arg;
1158 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1159 dbus_message_get_name (message),
1161 dbus_error_free (&error);
1166 if (strcmp (service_name, activated_name) != 0)
1168 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1169 activated_name, service_name);
1170 dbus_free (service_name);
1174 scd.skip_connection = connection;
1176 scd.expected_service_name = service_name;
1177 bus_test_clients_foreach (check_service_created_foreach,
1180 dbus_free (service_name);
1185 dbus_message_unref (message);
1186 message = pop_message_waiting_for_memory (connection);
1187 if (message == NULL)
1189 _dbus_warn ("Expected a reply to %s, got nothing\n",
1190 DBUS_MESSAGE_ACTIVATE_SERVICE);
1196 _dbus_warn ("Expected to get service %s ServiceCreated, instead got %s\n",
1197 activated_name, dbus_message_get_name (message));
1201 if (!dbus_message_name_is (message, DBUS_MESSAGE_ACTIVATE_SERVICE))
1203 _dbus_warn ("Expected reply to %s, got message %s instead\n",
1204 DBUS_MESSAGE_ACTIVATE_SERVICE,
1205 dbus_message_get_name (message));
1209 activation_result = 0;
1210 if (!dbus_message_get_args (message, &error,
1211 DBUS_TYPE_UINT32, &activation_result,
1214 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1216 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1217 DBUS_MESSAGE_ACTIVATE_SERVICE, error.message);
1218 dbus_error_free (&error);
1222 dbus_error_free (&error);
1226 if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1228 else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1232 _dbus_warn ("Activation result was 0x%x, no good.\n",
1238 dbus_message_unref (message);
1241 if (!check_no_leftovers (context))
1243 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1251 dbus_message_unref (message);
1257 check_service_deactivated (BusContext *context,
1258 DBusConnection *connection,
1259 const char *activated_name,
1260 const char *base_service)
1262 DBusMessage *message;
1265 CheckServiceDeletedData csdd;
1270 dbus_error_init (&error);
1272 /* Now we are expecting ServiceDeleted messages for the base
1273 * service and the activated_name. The base service
1274 * notification is required to come last.
1276 csdd.expected_service_name = activated_name;
1277 csdd.failed = FALSE;
1278 bus_test_clients_foreach (check_service_deleted_foreach,
1284 csdd.expected_service_name = base_service;
1285 csdd.failed = FALSE;
1286 bus_test_clients_foreach (check_service_deleted_foreach,
1292 if (!check_no_leftovers (context))
1294 _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1302 dbus_message_unref (message);
1308 check_send_exit_to_service (BusContext *context,
1309 DBusConnection *connection,
1310 const char *service_name,
1311 const char *base_service)
1313 dbus_bool_t got_error;
1314 DBusMessage *message;
1315 dbus_int32_t serial;
1318 _dbus_verbose ("Sending exit message to the test service\n");
1322 /* Kill off the test service by sending it a quit message */
1323 message = dbus_message_new (service_name,
1324 "org.freedesktop.DBus.TestSuiteExit");
1326 if (message == NULL)
1328 /* Do this again; we still need the service to exit... */
1329 if (!check_send_exit_to_service (context, connection,
1330 service_name, base_service))
1336 if (!dbus_connection_send (connection, message, &serial))
1338 dbus_message_unref (message);
1340 /* Do this again; we still need the service to exit... */
1341 if (!check_send_exit_to_service (context, connection,
1342 service_name, base_service))
1348 dbus_message_unref (message);
1352 bus_test_run_clients_loop (TRUE);
1354 /* read it in and write it out to test service */
1355 bus_test_run_bus_loop (context, FALSE);
1357 /* see if we got an error during message bus dispatching */
1358 bus_test_run_clients_loop (FALSE);
1359 message = dbus_connection_borrow_message (connection);
1360 got_error = message != NULL && dbus_message_get_is_error (message);
1363 dbus_connection_return_message (connection, message);
1369 /* If no error, wait for the test service to exit */
1370 block_connection_until_message_from_bus (context, connection);
1372 bus_test_run_everything (context);
1377 message = pop_message_waiting_for_memory (connection);
1378 _dbus_assert (message != NULL);
1380 if (!dbus_message_get_is_error (message))
1382 _dbus_warn ("expecting an error reply to asking test service to exit, got %s\n",
1383 dbus_message_get_name (message));
1386 else if (!dbus_message_name_is (message, DBUS_ERROR_NO_MEMORY))
1388 _dbus_warn ("not expecting error %s when asking test service to exit\n",
1389 dbus_message_get_name (message));
1393 _dbus_verbose ("Got error %s when asking test service to exit\n",
1394 dbus_message_get_name (message));
1396 /* Do this again; we still need the service to exit... */
1397 if (!check_send_exit_to_service (context, connection,
1398 service_name, base_service))
1403 if (!check_service_deactivated (context, connection,
1404 service_name, base_service))
1412 dbus_message_unref (message);
1418 check_got_error (BusContext *context,
1419 DBusConnection *connection,
1420 const char *first_error_name,
1423 DBusMessage *message;
1426 dbus_bool_t error_found;
1427 const char *error_name;
1431 message = pop_message_waiting_for_memory (connection);
1432 if (message == NULL)
1434 _dbus_warn ("Did not get an expected error\n");
1438 if (!dbus_message_get_is_error (message))
1440 _dbus_warn ("Expected an error, got %s\n",
1441 dbus_message_get_name (message));
1445 error_found = FALSE;
1447 va_start (ap, first_error_name);
1448 error_name = first_error_name;
1449 while (error_name != NULL)
1451 if (dbus_message_name_is (message, error_name))
1456 error_name = va_arg (ap, char*);
1462 _dbus_warn ("Expected error %s or other, got %s instead\n",
1464 dbus_message_get_name (message));
1472 dbus_message_unref (message);
1477 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1479 /* returns TRUE if the correct thing happens,
1480 * but the correct thing may include OOM errors.
1483 check_existent_service_activation (BusContext *context,
1484 DBusConnection *connection)
1486 DBusMessage *message;
1487 dbus_int32_t serial;
1492 base_service = NULL;
1494 dbus_error_init (&error);
1496 message = dbus_message_new (DBUS_SERVICE_DBUS,
1497 DBUS_MESSAGE_ACTIVATE_SERVICE);
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 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
1545 _dbus_verbose ("Received %s on %p after sending %s\n",
1546 dbus_message_get_name (message), connection,
1547 DBUS_MESSAGE_ACTIVATE_SERVICE);
1549 if (dbus_message_get_is_error (message))
1551 if (!dbus_message_sender_is (message, DBUS_SERVICE_DBUS))
1553 _dbus_warn ("Message has wrong sender %s\n",
1554 dbus_message_get_sender (message) ?
1555 dbus_message_get_sender (message) : "(none)");
1559 if (dbus_message_name_is (message,
1560 DBUS_ERROR_NO_MEMORY))
1562 ; /* good, this is a valid response */
1564 else if (dbus_message_name_is (message,
1565 DBUS_ERROR_SPAWN_CHILD_EXITED))
1567 ; /* good, this is expected also */
1571 _dbus_warn ("Did not expect error %s\n",
1572 dbus_message_get_name (message));
1578 dbus_bool_t got_service_deleted;
1579 dbus_bool_t got_error;
1581 if (!check_base_service_activated (context, connection,
1582 message, &base_service))
1585 dbus_message_unref (message);
1588 /* We may need to block here for the test service to exit or finish up */
1589 block_connection_until_message_from_bus (context, connection);
1591 message = dbus_connection_borrow_message (connection);
1592 if (message == NULL)
1594 _dbus_warn ("Did not receive any messages after base service creation notification\n");
1598 got_service_deleted = dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_DELETED);
1599 got_error = dbus_message_get_is_error (message);
1601 dbus_connection_return_message (connection, message);
1606 if (!check_got_error (context, connection,
1607 DBUS_ERROR_SPAWN_CHILD_EXITED,
1608 DBUS_ERROR_NO_MEMORY,
1612 /* A service deleted should be coming along now after this error.
1613 * We can also get the error *after* the service deleted.
1615 got_service_deleted = TRUE;
1618 if (got_service_deleted)
1620 /* The service started up and got a base address, but then
1621 * failed to register under EXISTENT_SERVICE_NAME
1623 CheckServiceDeletedData csdd;
1625 csdd.expected_service_name = base_service;
1626 csdd.failed = FALSE;
1627 bus_test_clients_foreach (check_service_deleted_foreach,
1633 /* Now we should get an error about the service exiting
1634 * if we didn't get it before.
1638 block_connection_until_message_from_bus (context, connection);
1640 /* and process everything again */
1641 bus_test_run_everything (context);
1643 if (!check_got_error (context, connection,
1644 DBUS_ERROR_SPAWN_CHILD_EXITED,
1651 message = pop_message_waiting_for_memory (connection);
1652 if (message == NULL)
1654 _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1658 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1659 base_service, message))
1662 dbus_message_unref (message);
1666 if (!check_no_leftovers (context))
1668 _dbus_warn ("Messages were left over after successful activation\n");
1672 if (!check_send_exit_to_service (context, connection,
1673 EXISTENT_SERVICE_NAME, base_service))
1682 dbus_message_unref (message);
1685 dbus_free (base_service);
1690 /* returns TRUE if the correct thing happens,
1691 * but the correct thing may include OOM errors.
1694 check_segfault_service_activation (BusContext *context,
1695 DBusConnection *connection)
1697 DBusMessage *message;
1698 dbus_int32_t serial;
1702 dbus_error_init (&error);
1704 message = dbus_message_new (DBUS_SERVICE_DBUS,
1705 DBUS_MESSAGE_ACTIVATE_SERVICE);
1707 if (message == NULL)
1710 if (!dbus_message_append_args (message,
1712 "org.freedesktop.DBus.TestSuiteSegfaultService",
1713 DBUS_TYPE_UINT32, 0,
1716 dbus_message_unref (message);
1720 if (!dbus_connection_send (connection, message, &serial))
1722 dbus_message_unref (message);
1726 dbus_message_unref (message);
1729 bus_test_run_everything (context);
1730 block_connection_until_message_from_bus (context, connection);
1731 bus_test_run_everything (context);
1733 if (!dbus_connection_get_is_connected (connection))
1735 _dbus_verbose ("connection was disconnected\n");
1741 message = pop_message_waiting_for_memory (connection);
1742 if (message == NULL)
1744 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1745 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
1749 _dbus_verbose ("Received %s on %p\n",
1750 dbus_message_get_name (message), connection);
1752 if (dbus_message_get_is_error (message))
1754 if (!dbus_message_sender_is (message, DBUS_SERVICE_DBUS))
1756 _dbus_warn ("Message has wrong sender %s\n",
1757 dbus_message_get_sender (message) ?
1758 dbus_message_get_sender (message) : "(none)");
1762 if (dbus_message_name_is (message,
1763 DBUS_ERROR_NO_MEMORY))
1765 ; /* good, this is a valid response */
1767 else if (dbus_message_name_is (message,
1768 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1770 ; /* good, this is expected also */
1774 _dbus_warn ("Did not expect error %s\n",
1775 dbus_message_get_name (message));
1781 _dbus_warn ("Did not expect to successfully activate segfault service\n");
1789 dbus_message_unref (message);
1797 BusContext *context;
1801 check_oom_check1_func (void *data)
1803 Check1Data *d = data;
1805 if (! (* d->func) (d->context))
1808 if (!check_no_leftovers (d->context))
1810 _dbus_warn ("Messages were left over, should be covered by test suite\n");
1818 check1_try_iterations (BusContext *context,
1819 const char *description,
1825 d.context = context;
1827 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1829 _dbus_assert_not_reached ("test failed");
1835 BusContext *context;
1836 DBusConnection *connection;
1840 check_oom_check2_func (void *data)
1842 Check2Data *d = data;
1844 if (! (* d->func) (d->context, d->connection))
1847 if (!check_no_leftovers (d->context))
1849 _dbus_warn ("Messages were left over, should be covered by test suite");
1857 check2_try_iterations (BusContext *context,
1858 DBusConnection *connection,
1859 const char *description,
1865 d.context = context;
1866 d.connection = connection;
1868 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
1870 _dbus_assert_not_reached ("test failed");
1874 bus_dispatch_test (const DBusString *test_data_dir)
1876 BusContext *context;
1877 DBusConnection *foo;
1878 DBusConnection *bar;
1879 DBusConnection *baz;
1882 dbus_error_init (&error);
1884 context = bus_context_new_test (test_data_dir,
1885 "valid-config-files/debug-allow-all.conf");
1886 if (context == NULL)
1889 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1891 _dbus_assert_not_reached ("could not alloc connection");
1893 if (!bus_setup_debug_client (foo))
1894 _dbus_assert_not_reached ("could not set up connection");
1896 if (!check_hello_message (context, foo))
1897 _dbus_assert_not_reached ("hello message failed");
1899 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
1901 _dbus_assert_not_reached ("could not alloc connection");
1903 if (!bus_setup_debug_client (bar))
1904 _dbus_assert_not_reached ("could not set up connection");
1906 if (!check_hello_message (context, bar))
1907 _dbus_assert_not_reached ("hello message failed");
1909 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
1911 _dbus_assert_not_reached ("could not alloc connection");
1913 if (!bus_setup_debug_client (baz))
1914 _dbus_assert_not_reached ("could not set up connection");
1916 if (!check_hello_message (context, baz))
1917 _dbus_assert_not_reached ("hello message failed");
1919 if (!check_no_leftovers (context))
1921 _dbus_warn ("Messages were left over after setting up initial connections");
1922 _dbus_assert_not_reached ("initial connection setup failed");
1925 check1_try_iterations (context, "create_and_hello",
1926 check_hello_connection);
1928 check2_try_iterations (context, foo, "nonexistent_service_activation",
1929 check_nonexistent_service_activation);
1931 check2_try_iterations (context, foo, "segfault_service_activation",
1932 check_segfault_service_activation);
1934 check2_try_iterations (context, foo, "existent_service_activation",
1935 check_existent_service_activation);
1937 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1939 kill_client_connection_unchecked (foo);
1940 kill_client_connection_unchecked (bar);
1941 kill_client_connection_unchecked (baz);
1943 bus_context_unref (context);
1949 bus_dispatch_sha1_test (const DBusString *test_data_dir)
1951 BusContext *context;
1952 DBusConnection *foo;
1955 dbus_error_init (&error);
1957 /* Test SHA1 authentication */
1958 _dbus_verbose ("Testing SHA1 context\n");
1960 context = bus_context_new_test (test_data_dir,
1961 "valid-config-files/debug-allow-all-sha1.conf");
1962 if (context == NULL)
1965 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1967 _dbus_assert_not_reached ("could not alloc connection");
1969 if (!bus_setup_debug_client (foo))
1970 _dbus_assert_not_reached ("could not set up connection");
1972 if (!check_hello_message (context, foo))
1973 _dbus_assert_not_reached ("hello message failed");
1975 if (!check_no_leftovers (context))
1977 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection");
1978 _dbus_assert_not_reached ("initial connection setup failed");
1981 check1_try_iterations (context, "create_and_hello_sha1",
1982 check_hello_connection);
1984 kill_client_connection_unchecked (foo);
1986 bus_context_unref (context);
1991 #endif /* DBUS_BUILD_TESTS */