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);
104 bus_dispatch (DBusConnection *connection,
105 DBusMessage *message)
107 const char *sender, *service_name, *message_name;
109 BusTransaction *transaction;
113 dbus_error_init (&error);
115 context = bus_connection_get_context (connection);
116 _dbus_assert (context != NULL);
118 /* If we can't even allocate an OOM error, we just go to sleep
121 while (!bus_connection_preallocate_oom_error (connection))
122 _dbus_wait_for_memory ();
124 /* Ref connection in case we disconnect it at some point in here */
125 dbus_connection_ref (connection);
127 service_name = dbus_message_get_destination (message);
128 message_name = dbus_message_get_name (message);
130 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
132 _dbus_verbose ("DISPATCH: %s to %s\n",
133 message_name, service_name ? service_name : "peer");
135 /* If service_name is NULL, this is a message to the bus daemon, not
136 * intended to actually go "on the bus"; e.g. a peer-to-peer
137 * ping. Handle these immediately, especially disconnection
138 * messages. There are no security policy checks on these.
140 if (service_name == NULL)
142 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
143 bus_connection_disconnected (connection);
145 /* DBusConnection also handles some of these automatically, we leave
151 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
153 /* Create our transaction */
154 transaction = bus_transaction_new (context);
155 if (transaction == NULL)
157 BUS_SET_OOM (&error);
161 /* Assign a sender to the message */
162 if (bus_connection_is_active (connection))
164 sender = bus_connection_get_name (connection);
165 _dbus_assert (sender != NULL);
167 if (!dbus_message_set_sender (message, sender))
169 BUS_SET_OOM (&error);
173 /* We need to refetch the service name here, because
174 * dbus_message_set_sender can cause the header to be
175 * reallocated, and thus the service_name pointer will become
178 service_name = dbus_message_get_destination (message);
181 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
183 if (!bus_context_check_security_policy (context,
184 connection, NULL, message, &error))
186 _dbus_verbose ("Security policy rejected message\n");
190 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
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
255 _dbus_verbose ("Not sending error to connection we disconnected\n");
257 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
259 bus_connection_send_oom_error (connection, message);
261 /* cancel transaction due to OOM */
262 if (transaction != NULL)
264 bus_transaction_cancel_and_free (transaction);
270 /* Try to send the real error, if no mem to do that, send
273 _dbus_assert (transaction != NULL);
275 if (!bus_transaction_send_error_reply (transaction, connection,
278 bus_connection_send_oom_error (connection, message);
280 /* cancel transaction due to OOM */
281 if (transaction != NULL)
283 bus_transaction_cancel_and_free (transaction);
289 dbus_error_free (&error);
292 if (transaction != NULL)
294 bus_transaction_execute_and_free (transaction);
297 dbus_connection_unref (connection);
300 static DBusHandlerResult
301 bus_dispatch_message_handler (DBusMessageHandler *handler,
302 DBusConnection *connection,
303 DBusMessage *message,
306 bus_dispatch (connection, message);
308 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
312 free_message_handler (void *data)
314 DBusMessageHandler *handler = data;
316 _dbus_assert (message_handler_slot >= 0);
318 dbus_message_handler_unref (handler);
319 dbus_connection_free_data_slot (&message_handler_slot);
323 bus_dispatch_add_connection (DBusConnection *connection)
325 DBusMessageHandler *handler;
327 if (!dbus_connection_allocate_data_slot (&message_handler_slot))
330 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
333 dbus_connection_free_data_slot (&message_handler_slot);
337 if (!dbus_connection_add_filter (connection, handler))
339 dbus_message_handler_unref (handler);
340 dbus_connection_free_data_slot (&message_handler_slot);
345 _dbus_assert (message_handler_slot >= 0);
347 if (!dbus_connection_set_data (connection,
348 message_handler_slot,
350 free_message_handler))
352 dbus_message_handler_unref (handler);
353 dbus_connection_free_data_slot (&message_handler_slot);
362 bus_dispatch_remove_connection (DBusConnection *connection)
364 /* Here we tell the bus driver that we want to get off. */
365 bus_driver_remove_connection (connection);
367 dbus_connection_set_data (connection,
368 message_handler_slot,
372 #ifdef DBUS_BUILD_TESTS
374 typedef dbus_bool_t (* Check1Func) (BusContext *context);
375 typedef dbus_bool_t (* Check2Func) (BusContext *context,
376 DBusConnection *connection);
378 static dbus_bool_t check_no_leftovers (BusContext *context);
381 block_connection_until_message_from_bus (BusContext *context,
382 DBusConnection *connection)
384 while (dbus_connection_get_dispatch_status (connection) ==
385 DBUS_DISPATCH_COMPLETE &&
386 dbus_connection_get_is_connected (connection))
388 bus_test_run_bus_loop (context, TRUE);
389 bus_test_run_clients_loop (FALSE);
393 /* compensate for fact that pop_message() can return #NULL due to OOM */
395 pop_message_waiting_for_memory (DBusConnection *connection)
397 while (dbus_connection_get_dispatch_status (connection) ==
398 DBUS_DISPATCH_NEED_MEMORY)
399 _dbus_wait_for_memory ();
401 return dbus_connection_pop_message (connection);
406 const char *expected_service_name;
408 } CheckServiceDeletedData;
411 check_service_deleted_foreach (DBusConnection *connection,
414 CheckServiceDeletedData *d = data;
415 DBusMessage *message;
419 dbus_error_init (&error);
423 message = pop_message_waiting_for_memory (connection);
426 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
427 connection, DBUS_MESSAGE_SERVICE_DELETED);
430 else if (!dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_DELETED))
432 _dbus_warn ("Received message %s on %p, expecting %s\n",
433 dbus_message_get_name (message),
434 connection, DBUS_MESSAGE_SERVICE_DELETED);
439 if (!dbus_message_get_args (message, &error,
440 DBUS_TYPE_STRING, &service_name,
443 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
445 _dbus_verbose ("no memory to get service name arg\n");
449 _dbus_assert (dbus_error_is_set (&error));
450 _dbus_warn ("Did not get the expected single string argument\n");
454 else if (strcmp (service_name, d->expected_service_name) != 0)
456 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
457 d->expected_service_name,
466 dbus_free (service_name);
467 dbus_error_free (&error);
470 dbus_message_unref (message);
476 kill_client_connection (BusContext *context,
477 DBusConnection *connection)
481 CheckServiceDeletedData csdd;
483 _dbus_verbose ("killing connection %p\n", connection);
485 s = dbus_bus_get_base_service (connection);
486 _dbus_assert (s != NULL);
488 while ((base_service = _dbus_strdup (s)) == NULL)
489 _dbus_wait_for_memory ();
491 dbus_connection_ref (connection);
493 /* kick in the disconnect handler that unrefs the connection */
494 dbus_connection_disconnect (connection);
496 bus_test_run_everything (context);
498 _dbus_assert (bus_test_client_listed (connection));
500 /* Run disconnect handler in test.c */
501 if (bus_connection_dispatch_one_message (connection))
502 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
504 _dbus_assert (!dbus_connection_get_is_connected (connection));
505 dbus_connection_unref (connection);
507 _dbus_assert (!bus_test_client_listed (connection));
509 csdd.expected_service_name = base_service;
512 bus_test_clients_foreach (check_service_deleted_foreach,
515 dbus_free (base_service);
518 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
520 if (!check_no_leftovers (context))
521 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
525 kill_client_connection_unchecked (DBusConnection *connection)
527 /* This kills the connection without expecting it to affect
528 * the rest of the bus.
530 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
532 dbus_connection_ref (connection);
533 dbus_connection_disconnect (connection);
534 /* dispatching disconnect handler will unref once */
535 if (bus_connection_dispatch_one_message (connection))
536 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
538 _dbus_assert (!bus_test_client_listed (connection));
539 dbus_connection_unref (connection);
545 } CheckNoMessagesData;
548 check_no_messages_foreach (DBusConnection *connection,
551 CheckNoMessagesData *d = data;
552 DBusMessage *message;
554 message = pop_message_waiting_for_memory (connection);
557 _dbus_warn ("Received message %s on %p, expecting no messages\n",
558 dbus_message_get_name (message), connection);
563 dbus_message_unref (message);
569 DBusConnection *skip_connection;
570 const char *expected_service_name;
572 } CheckServiceCreatedData;
575 check_service_created_foreach (DBusConnection *connection,
578 CheckServiceCreatedData *d = data;
579 DBusMessage *message;
583 if (connection == d->skip_connection)
586 dbus_error_init (&error);
590 message = pop_message_waiting_for_memory (connection);
593 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
594 connection, DBUS_MESSAGE_SERVICE_CREATED);
597 else if (!dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_CREATED))
599 _dbus_warn ("Received message %s on %p, expecting %s\n",
600 dbus_message_get_name (message),
601 connection, DBUS_MESSAGE_SERVICE_CREATED);
606 if (!dbus_message_get_args (message, &error,
607 DBUS_TYPE_STRING, &service_name,
610 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
612 _dbus_verbose ("no memory to get service name arg\n");
616 _dbus_assert (dbus_error_is_set (&error));
617 _dbus_warn ("Did not get the expected single string argument\n");
621 else if (strcmp (service_name, d->expected_service_name) != 0)
623 _dbus_warn ("expected creation of service %s, got creation of %s\n",
624 d->expected_service_name,
633 dbus_free (service_name);
634 dbus_error_free (&error);
637 dbus_message_unref (message);
643 check_no_leftovers (BusContext *context)
645 CheckNoMessagesData nmd;
648 bus_test_clients_foreach (check_no_messages_foreach,
657 /* returns TRUE if the correct thing happens,
658 * but the correct thing may include OOM errors.
661 check_hello_message (BusContext *context,
662 DBusConnection *connection)
664 DBusMessage *message;
672 dbus_error_init (&error);
677 message = dbus_message_new_method_call (DBUS_MESSAGE_HELLO,
683 if (!dbus_connection_send (connection, message, &serial))
685 dbus_message_unref (message);
689 dbus_message_unref (message);
692 /* send our message */
693 bus_test_run_clients_loop (TRUE);
695 dbus_connection_ref (connection); /* because we may get disconnected */
696 block_connection_until_message_from_bus (context, connection);
698 if (!dbus_connection_get_is_connected (connection))
700 _dbus_verbose ("connection was disconnected\n");
702 dbus_connection_unref (connection);
707 dbus_connection_unref (connection);
709 message = pop_message_waiting_for_memory (connection);
712 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
713 DBUS_MESSAGE_HELLO, serial, connection);
717 _dbus_verbose ("Received %s on %p\n",
718 dbus_message_get_name (message), connection);
720 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
722 _dbus_warn ("Message has wrong sender %s\n",
723 dbus_message_get_sender (message) ?
724 dbus_message_get_sender (message) : "(none)");
728 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
730 if (dbus_message_has_name (message,
731 DBUS_ERROR_NO_MEMORY))
733 ; /* good, this is a valid response */
737 _dbus_warn ("Did not expect error %s\n",
738 dbus_message_get_name (message));
744 CheckServiceCreatedData scd;
746 if (dbus_message_has_name (message,
749 ; /* good, expected */
753 _dbus_warn ("Did not expect reply %s\n",
754 dbus_message_get_name (message));
758 retry_get_hello_name:
759 if (!dbus_message_get_args (message, &error,
760 DBUS_TYPE_STRING, &name,
763 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
765 _dbus_verbose ("no memory to get service name arg from hello\n");
766 dbus_error_free (&error);
767 _dbus_wait_for_memory ();
768 goto retry_get_hello_name;
772 _dbus_assert (dbus_error_is_set (&error));
773 _dbus_warn ("Did not get the expected single string argument to hello\n");
778 _dbus_verbose ("Got hello name: %s\n", name);
780 while (!dbus_bus_set_base_service (connection, name))
781 _dbus_wait_for_memory ();
783 scd.skip_connection = NULL;
785 scd.expected_service_name = name;
786 bus_test_clients_foreach (check_service_created_foreach,
792 /* Client should also have gotten ServiceAcquired */
793 dbus_message_unref (message);
794 message = pop_message_waiting_for_memory (connection);
797 _dbus_warn ("Expecting %s, got nothing\n",
798 DBUS_MESSAGE_SERVICE_ACQUIRED);
802 retry_get_acquired_name:
803 if (!dbus_message_get_args (message, &error,
804 DBUS_TYPE_STRING, &acquired,
807 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
809 _dbus_verbose ("no memory to get service name arg from acquired\n");
810 dbus_error_free (&error);
811 _dbus_wait_for_memory ();
812 goto retry_get_acquired_name;
816 _dbus_assert (dbus_error_is_set (&error));
817 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
822 _dbus_verbose ("Got acquired name: %s\n", acquired);
824 if (strcmp (acquired, name) != 0)
826 _dbus_warn ("Acquired name is %s but expected %s\n",
832 if (!check_no_leftovers (context))
838 dbus_error_free (&error);
841 dbus_free (acquired);
844 dbus_message_unref (message);
849 /* returns TRUE if the correct thing happens,
850 * but the correct thing may include OOM errors.
853 check_hello_connection (BusContext *context)
855 DBusConnection *connection;
858 dbus_error_init (&error);
860 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
861 if (connection == NULL)
863 _DBUS_ASSERT_ERROR_IS_SET (&error);
864 dbus_error_free (&error);
868 if (!bus_setup_debug_client (connection))
870 dbus_connection_disconnect (connection);
871 dbus_connection_unref (connection);
875 if (!check_hello_message (context, connection))
878 if (dbus_bus_get_base_service (connection) == NULL)
880 /* We didn't successfully register, so we can't
881 * do the usual kill_client_connection() checks
883 kill_client_connection_unchecked (connection);
887 kill_client_connection (context, connection);
893 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
895 /* returns TRUE if the correct thing happens,
896 * but the correct thing may include OOM errors.
899 check_nonexistent_service_activation (BusContext *context,
900 DBusConnection *connection)
902 DBusMessage *message;
907 dbus_error_init (&error);
909 message = dbus_message_new_method_call (DBUS_MESSAGE_ACTIVATE_SERVICE,
915 if (!dbus_message_append_args (message,
916 DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
920 dbus_message_unref (message);
924 if (!dbus_connection_send (connection, message, &serial))
926 dbus_message_unref (message);
930 dbus_message_unref (message);
933 bus_test_run_everything (context);
934 block_connection_until_message_from_bus (context, connection);
935 bus_test_run_everything (context);
937 if (!dbus_connection_get_is_connected (connection))
939 _dbus_verbose ("connection was disconnected\n");
945 message = pop_message_waiting_for_memory (connection);
948 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
949 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
953 _dbus_verbose ("Received %s on %p\n",
954 dbus_message_get_name (message), connection);
956 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
958 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
960 _dbus_warn ("Message has wrong sender %s\n",
961 dbus_message_get_sender (message) ?
962 dbus_message_get_sender (message) : "(none)");
966 if (dbus_message_has_name (message,
967 DBUS_ERROR_NO_MEMORY))
969 ; /* good, this is a valid response */
971 else if (dbus_message_has_name (message,
972 DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
974 ; /* good, this is expected also */
978 _dbus_warn ("Did not expect error %s\n",
979 dbus_message_get_name (message));
985 _dbus_warn ("Did not expect to successfully activate %s\n",
986 NONEXISTENT_SERVICE_NAME);
994 dbus_message_unref (message);
1000 check_base_service_activated (BusContext *context,
1001 DBusConnection *connection,
1002 DBusMessage *initial_message,
1003 char **base_service_p)
1005 DBusMessage *message;
1010 base_service = NULL;
1013 dbus_error_init (&error);
1015 message = initial_message;
1016 dbus_message_ref (message);
1018 if (dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_CREATED))
1021 CheckServiceCreatedData scd;
1023 reget_service_name_arg:
1024 if (!dbus_message_get_args (message, &error,
1025 DBUS_TYPE_STRING, &service_name,
1028 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1030 dbus_error_free (&error);
1031 _dbus_wait_for_memory ();
1032 goto reget_service_name_arg;
1036 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1037 dbus_message_get_name (message),
1039 dbus_error_free (&error);
1044 if (*service_name != ':')
1046 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1051 base_service = service_name;
1052 service_name = NULL;
1054 scd.skip_connection = connection;
1056 scd.expected_service_name = base_service;
1057 bus_test_clients_foreach (check_service_created_foreach,
1065 _dbus_warn ("Expected to get base service ServiceCreated, instead got %s\n",
1066 dbus_message_get_name (message));
1074 *base_service_p = base_service;
1075 base_service = NULL;
1080 dbus_message_unref (message);
1083 dbus_free (base_service);
1089 check_service_activated (BusContext *context,
1090 DBusConnection *connection,
1091 const char *activated_name,
1092 const char *base_service_name,
1093 DBusMessage *initial_message)
1095 DBusMessage *message;
1098 dbus_uint32_t activation_result;
1102 dbus_error_init (&error);
1104 message = initial_message;
1105 dbus_message_ref (message);
1107 if (dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_CREATED))
1110 CheckServiceCreatedData scd;
1112 reget_service_name_arg:
1113 if (!dbus_message_get_args (message, &error,
1114 DBUS_TYPE_STRING, &service_name,
1117 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1119 dbus_error_free (&error);
1120 _dbus_wait_for_memory ();
1121 goto reget_service_name_arg;
1125 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1126 dbus_message_get_name (message),
1128 dbus_error_free (&error);
1133 if (strcmp (service_name, activated_name) != 0)
1135 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1136 activated_name, service_name);
1137 dbus_free (service_name);
1141 scd.skip_connection = connection;
1143 scd.expected_service_name = service_name;
1144 bus_test_clients_foreach (check_service_created_foreach,
1147 dbus_free (service_name);
1152 dbus_message_unref (message);
1153 message = pop_message_waiting_for_memory (connection);
1154 if (message == NULL)
1156 _dbus_warn ("Expected a reply to %s, got nothing\n",
1157 DBUS_MESSAGE_ACTIVATE_SERVICE);
1163 _dbus_warn ("Expected to get service %s ServiceCreated, instead got %s\n",
1164 activated_name, dbus_message_get_name (message));
1168 if (!dbus_message_has_name (message, DBUS_MESSAGE_ACTIVATE_SERVICE))
1170 _dbus_warn ("Expected reply to %s, got message %s instead\n",
1171 DBUS_MESSAGE_ACTIVATE_SERVICE,
1172 dbus_message_get_name (message));
1176 activation_result = 0;
1177 if (!dbus_message_get_args (message, &error,
1178 DBUS_TYPE_UINT32, &activation_result,
1181 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1183 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1184 DBUS_MESSAGE_ACTIVATE_SERVICE, error.message);
1185 dbus_error_free (&error);
1189 dbus_error_free (&error);
1193 if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1195 else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1199 _dbus_warn ("Activation result was 0x%x, no good.\n",
1205 dbus_message_unref (message);
1208 if (!check_no_leftovers (context))
1210 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1218 dbus_message_unref (message);
1224 check_service_deactivated (BusContext *context,
1225 DBusConnection *connection,
1226 const char *activated_name,
1227 const char *base_service)
1229 DBusMessage *message;
1232 CheckServiceDeletedData csdd;
1237 dbus_error_init (&error);
1239 /* Now we are expecting ServiceDeleted messages for the base
1240 * service and the activated_name. The base service
1241 * notification is required to come last.
1243 csdd.expected_service_name = activated_name;
1244 csdd.failed = FALSE;
1245 bus_test_clients_foreach (check_service_deleted_foreach,
1251 csdd.expected_service_name = base_service;
1252 csdd.failed = FALSE;
1253 bus_test_clients_foreach (check_service_deleted_foreach,
1259 if (!check_no_leftovers (context))
1261 _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1269 dbus_message_unref (message);
1275 check_send_exit_to_service (BusContext *context,
1276 DBusConnection *connection,
1277 const char *service_name,
1278 const char *base_service)
1280 dbus_bool_t got_error;
1281 DBusMessage *message;
1282 dbus_int32_t serial;
1285 _dbus_verbose ("Sending exit message to the test service\n");
1289 /* Kill off the test service by sending it a quit message */
1290 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteExit",
1293 if (message == NULL)
1295 /* Do this again; we still need the service to exit... */
1296 if (!check_send_exit_to_service (context, connection,
1297 service_name, base_service))
1303 if (!dbus_connection_send (connection, message, &serial))
1305 dbus_message_unref (message);
1307 /* Do this again; we still need the service to exit... */
1308 if (!check_send_exit_to_service (context, connection,
1309 service_name, base_service))
1315 dbus_message_unref (message);
1319 bus_test_run_clients_loop (TRUE);
1321 /* read it in and write it out to test service */
1322 bus_test_run_bus_loop (context, FALSE);
1324 /* see if we got an error during message bus dispatching */
1325 bus_test_run_clients_loop (FALSE);
1326 message = dbus_connection_borrow_message (connection);
1327 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1330 dbus_connection_return_message (connection, message);
1336 /* If no error, wait for the test service to exit */
1337 block_connection_until_message_from_bus (context, connection);
1339 bus_test_run_everything (context);
1344 message = pop_message_waiting_for_memory (connection);
1345 _dbus_assert (message != NULL);
1347 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1349 _dbus_warn ("expecting an error reply to asking test service to exit, got %s\n",
1350 dbus_message_get_name (message));
1353 else if (!dbus_message_has_name (message, DBUS_ERROR_NO_MEMORY))
1355 _dbus_warn ("not expecting error %s when asking test service to exit\n",
1356 dbus_message_get_name (message));
1360 _dbus_verbose ("Got error %s when asking test service to exit\n",
1361 dbus_message_get_name (message));
1363 /* Do this again; we still need the service to exit... */
1364 if (!check_send_exit_to_service (context, connection,
1365 service_name, base_service))
1370 if (!check_service_deactivated (context, connection,
1371 service_name, base_service))
1379 dbus_message_unref (message);
1385 check_got_error (BusContext *context,
1386 DBusConnection *connection,
1387 const char *first_error_name,
1390 DBusMessage *message;
1393 dbus_bool_t error_found;
1394 const char *error_name;
1398 message = pop_message_waiting_for_memory (connection);
1399 if (message == NULL)
1401 _dbus_warn ("Did not get an expected error\n");
1405 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1407 _dbus_warn ("Expected an error, got %s\n",
1408 dbus_message_get_name (message));
1412 error_found = FALSE;
1414 va_start (ap, first_error_name);
1415 error_name = first_error_name;
1416 while (error_name != NULL)
1418 if (dbus_message_has_name (message, error_name))
1423 error_name = va_arg (ap, char*);
1429 _dbus_warn ("Expected error %s or other, got %s instead\n",
1431 dbus_message_get_name (message));
1439 dbus_message_unref (message);
1444 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1446 /* returns TRUE if the correct thing happens,
1447 * but the correct thing may include OOM errors.
1450 check_existent_service_activation (BusContext *context,
1451 DBusConnection *connection)
1453 DBusMessage *message;
1454 dbus_int32_t serial;
1459 base_service = NULL;
1461 dbus_error_init (&error);
1463 message = dbus_message_new_method_call (DBUS_MESSAGE_ACTIVATE_SERVICE,
1466 if (message == NULL)
1469 if (!dbus_message_append_args (message,
1470 DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1471 DBUS_TYPE_UINT32, 0,
1474 dbus_message_unref (message);
1478 if (!dbus_connection_send (connection, message, &serial))
1480 dbus_message_unref (message);
1484 dbus_message_unref (message);
1487 bus_test_run_everything (context);
1489 /* now wait for the message bus to hear back from the activated
1492 block_connection_until_message_from_bus (context, connection);
1494 bus_test_run_everything (context);
1496 if (!dbus_connection_get_is_connected (connection))
1498 _dbus_verbose ("connection was disconnected\n");
1504 message = pop_message_waiting_for_memory (connection);
1505 if (message == NULL)
1507 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1508 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
1512 _dbus_verbose ("Received %s on %p after sending %s\n",
1513 dbus_message_get_name (message), connection,
1514 DBUS_MESSAGE_ACTIVATE_SERVICE);
1516 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1518 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1520 _dbus_warn ("Message has wrong sender %s\n",
1521 dbus_message_get_sender (message) ?
1522 dbus_message_get_sender (message) : "(none)");
1526 if (dbus_message_has_name (message,
1527 DBUS_ERROR_NO_MEMORY))
1529 ; /* good, this is a valid response */
1531 else if (dbus_message_has_name (message,
1532 DBUS_ERROR_SPAWN_CHILD_EXITED))
1534 ; /* good, this is expected also */
1538 _dbus_warn ("Did not expect error %s\n",
1539 dbus_message_get_name (message));
1545 dbus_bool_t got_service_deleted;
1546 dbus_bool_t got_error;
1548 if (!check_base_service_activated (context, connection,
1549 message, &base_service))
1552 dbus_message_unref (message);
1555 /* We may need to block here for the test service to exit or finish up */
1556 block_connection_until_message_from_bus (context, connection);
1558 message = dbus_connection_borrow_message (connection);
1559 if (message == NULL)
1561 _dbus_warn ("Did not receive any messages after base service creation notification\n");
1565 got_service_deleted = dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_DELETED);
1566 got_error = dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1568 dbus_connection_return_message (connection, message);
1573 if (!check_got_error (context, connection,
1574 DBUS_ERROR_SPAWN_CHILD_EXITED,
1575 DBUS_ERROR_NO_MEMORY,
1579 /* A service deleted should be coming along now after this error.
1580 * We can also get the error *after* the service deleted.
1582 got_service_deleted = TRUE;
1585 if (got_service_deleted)
1587 /* The service started up and got a base address, but then
1588 * failed to register under EXISTENT_SERVICE_NAME
1590 CheckServiceDeletedData csdd;
1592 csdd.expected_service_name = base_service;
1593 csdd.failed = FALSE;
1594 bus_test_clients_foreach (check_service_deleted_foreach,
1600 /* Now we should get an error about the service exiting
1601 * if we didn't get it before.
1605 block_connection_until_message_from_bus (context, connection);
1607 /* and process everything again */
1608 bus_test_run_everything (context);
1610 if (!check_got_error (context, connection,
1611 DBUS_ERROR_SPAWN_CHILD_EXITED,
1618 message = pop_message_waiting_for_memory (connection);
1619 if (message == NULL)
1621 _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1625 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1626 base_service, message))
1629 dbus_message_unref (message);
1633 if (!check_no_leftovers (context))
1635 _dbus_warn ("Messages were left over after successful activation\n");
1639 if (!check_send_exit_to_service (context, connection,
1640 EXISTENT_SERVICE_NAME, base_service))
1649 dbus_message_unref (message);
1652 dbus_free (base_service);
1657 /* returns TRUE if the correct thing happens,
1658 * but the correct thing may include OOM errors.
1661 check_segfault_service_activation (BusContext *context,
1662 DBusConnection *connection)
1664 DBusMessage *message;
1665 dbus_int32_t serial;
1669 dbus_error_init (&error);
1671 message = dbus_message_new_method_call (DBUS_MESSAGE_ACTIVATE_SERVICE,
1674 if (message == NULL)
1677 if (!dbus_message_append_args (message,
1679 "org.freedesktop.DBus.TestSuiteSegfaultService",
1680 DBUS_TYPE_UINT32, 0,
1683 dbus_message_unref (message);
1687 if (!dbus_connection_send (connection, message, &serial))
1689 dbus_message_unref (message);
1693 dbus_message_unref (message);
1696 bus_test_run_everything (context);
1697 block_connection_until_message_from_bus (context, connection);
1698 bus_test_run_everything (context);
1700 if (!dbus_connection_get_is_connected (connection))
1702 _dbus_verbose ("connection was disconnected\n");
1708 message = pop_message_waiting_for_memory (connection);
1709 if (message == NULL)
1711 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1712 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
1716 _dbus_verbose ("Received %s on %p\n",
1717 dbus_message_get_name (message), connection);
1719 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1721 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1723 _dbus_warn ("Message has wrong sender %s\n",
1724 dbus_message_get_sender (message) ?
1725 dbus_message_get_sender (message) : "(none)");
1729 if (dbus_message_has_name (message,
1730 DBUS_ERROR_NO_MEMORY))
1732 ; /* good, this is a valid response */
1734 else if (dbus_message_has_name (message,
1735 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1737 ; /* good, this is expected also */
1741 _dbus_warn ("Did not expect error %s\n",
1742 dbus_message_get_name (message));
1748 _dbus_warn ("Did not expect to successfully activate segfault service\n");
1756 dbus_message_unref (message);
1764 BusContext *context;
1768 check_oom_check1_func (void *data)
1770 Check1Data *d = data;
1772 if (! (* d->func) (d->context))
1775 if (!check_no_leftovers (d->context))
1777 _dbus_warn ("Messages were left over, should be covered by test suite\n");
1785 check1_try_iterations (BusContext *context,
1786 const char *description,
1792 d.context = context;
1794 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1796 _dbus_assert_not_reached ("test failed");
1802 BusContext *context;
1803 DBusConnection *connection;
1807 check_oom_check2_func (void *data)
1809 Check2Data *d = data;
1811 if (! (* d->func) (d->context, d->connection))
1814 if (!check_no_leftovers (d->context))
1816 _dbus_warn ("Messages were left over, should be covered by test suite");
1824 check2_try_iterations (BusContext *context,
1825 DBusConnection *connection,
1826 const char *description,
1832 d.context = context;
1833 d.connection = connection;
1835 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
1837 _dbus_assert_not_reached ("test failed");
1841 bus_dispatch_test (const DBusString *test_data_dir)
1843 BusContext *context;
1844 DBusConnection *foo;
1845 DBusConnection *bar;
1846 DBusConnection *baz;
1849 dbus_error_init (&error);
1851 context = bus_context_new_test (test_data_dir,
1852 "valid-config-files/debug-allow-all.conf");
1853 if (context == NULL)
1856 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1858 _dbus_assert_not_reached ("could not alloc connection");
1860 if (!bus_setup_debug_client (foo))
1861 _dbus_assert_not_reached ("could not set up connection");
1863 if (!check_hello_message (context, foo))
1864 _dbus_assert_not_reached ("hello message failed");
1866 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
1868 _dbus_assert_not_reached ("could not alloc connection");
1870 if (!bus_setup_debug_client (bar))
1871 _dbus_assert_not_reached ("could not set up connection");
1873 if (!check_hello_message (context, bar))
1874 _dbus_assert_not_reached ("hello message failed");
1876 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
1878 _dbus_assert_not_reached ("could not alloc connection");
1880 if (!bus_setup_debug_client (baz))
1881 _dbus_assert_not_reached ("could not set up connection");
1883 if (!check_hello_message (context, baz))
1884 _dbus_assert_not_reached ("hello message failed");
1886 if (!check_no_leftovers (context))
1888 _dbus_warn ("Messages were left over after setting up initial connections");
1889 _dbus_assert_not_reached ("initial connection setup failed");
1892 check1_try_iterations (context, "create_and_hello",
1893 check_hello_connection);
1895 check2_try_iterations (context, foo, "nonexistent_service_activation",
1896 check_nonexistent_service_activation);
1898 check2_try_iterations (context, foo, "segfault_service_activation",
1899 check_segfault_service_activation);
1901 check2_try_iterations (context, foo, "existent_service_activation",
1902 check_existent_service_activation);
1904 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1906 kill_client_connection_unchecked (foo);
1907 kill_client_connection_unchecked (bar);
1908 kill_client_connection_unchecked (baz);
1910 bus_context_unref (context);
1916 bus_dispatch_sha1_test (const DBusString *test_data_dir)
1918 BusContext *context;
1919 DBusConnection *foo;
1922 dbus_error_init (&error);
1924 /* Test SHA1 authentication */
1925 _dbus_verbose ("Testing SHA1 context\n");
1927 context = bus_context_new_test (test_data_dir,
1928 "valid-config-files/debug-allow-all-sha1.conf");
1929 if (context == NULL)
1932 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1934 _dbus_assert_not_reached ("could not alloc connection");
1936 if (!bus_setup_debug_client (foo))
1937 _dbus_assert_not_reached ("could not set up connection");
1939 if (!check_hello_message (context, foo))
1940 _dbus_assert_not_reached ("hello message failed");
1942 if (!check_no_leftovers (context))
1944 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
1945 _dbus_assert_not_reached ("initial connection setup failed");
1948 check1_try_iterations (context, "create_and_hello_sha1",
1949 check_hello_connection);
1951 kill_client_connection_unchecked (foo);
1953 bus_context_unref (context);
1958 #endif /* DBUS_BUILD_TESTS */