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_context_check_security_policy (d->context,
57 return TRUE; /* silently don't send it */
59 if (!bus_transaction_send (d->transaction,
63 BUS_SET_OOM (d->error);
71 bus_dispatch_broadcast_message (BusTransaction *transaction,
72 DBusConnection *sender,
78 BusConnections *connections;
80 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
82 _dbus_assert (dbus_message_get_sender (message) != NULL);
84 connections = bus_transaction_get_connections (transaction);
86 dbus_error_init (&tmp_error);
88 d.context = bus_transaction_get_context (transaction);
90 d.transaction = transaction;
93 bus_connections_foreach_active (connections, send_one_message, &d);
95 if (dbus_error_is_set (&tmp_error))
97 dbus_move_error (&tmp_error, error);
105 bus_dispatch (DBusConnection *connection,
106 DBusMessage *message)
108 const char *sender, *service_name, *message_name;
110 BusTransaction *transaction;
114 dbus_error_init (&error);
116 context = bus_connection_get_context (connection);
117 _dbus_assert (context != NULL);
119 /* If we can't even allocate an OOM error, we just go to sleep
122 while (!bus_connection_preallocate_oom_error (connection))
123 _dbus_wait_for_memory ();
125 /* Ref connection in case we disconnect it at some point in here */
126 dbus_connection_ref (connection);
128 service_name = dbus_message_get_destination (message);
129 message_name = dbus_message_get_name (message);
131 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
133 _dbus_verbose ("DISPATCH: %s to %s\n",
134 message_name, service_name ? service_name : "peer");
136 /* If service_name is NULL, this is a message to the bus daemon, not
137 * intended to actually go "on the bus"; e.g. a peer-to-peer
138 * ping. Handle these immediately, especially disconnection
139 * messages. There are no security policy checks on these.
141 if (service_name == NULL)
143 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
144 bus_connection_disconnected (connection);
146 /* DBusConnection also handles some of these automatically, we leave
152 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
154 /* Create our transaction */
155 transaction = bus_transaction_new (context);
156 if (transaction == NULL)
158 BUS_SET_OOM (&error);
162 /* Assign a sender to the message */
163 if (bus_connection_is_active (connection))
165 sender = bus_connection_get_name (connection);
166 _dbus_assert (sender != NULL);
168 if (!dbus_message_set_sender (message, sender))
170 BUS_SET_OOM (&error);
174 /* We need to refetch the service name here, because
175 * dbus_message_set_sender can cause the header to be
176 * reallocated, and thus the service_name pointer will become
179 service_name = dbus_message_get_destination (message);
182 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
184 if (!bus_context_check_security_policy (context,
185 connection, NULL, message, &error))
187 _dbus_verbose ("Security policy rejected message\n");
191 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
192 if (!bus_driver_handle_message (connection, transaction, message, &error))
195 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
197 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
198 dbus_connection_disconnect (connection);
200 /* FIXME what if we un-special-case this service and just have a flag
201 * on services that all service owners will get messages to it, not just
204 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
206 if (!bus_dispatch_broadcast_message (transaction, connection, message, &error))
209 else /* route to named service */
211 DBusString service_string;
213 BusRegistry *registry;
215 registry = bus_connection_get_registry (connection);
217 _dbus_string_init_const (&service_string, service_name);
218 service = bus_registry_lookup (registry, &service_string);
222 dbus_set_error (&error,
223 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
224 "Service \"%s\" does not exist",
230 DBusConnection *recipient;
232 recipient = bus_service_get_primary_owner (service);
233 _dbus_assert (recipient != NULL);
235 if (!bus_context_check_security_policy (context,
236 connection, recipient, message, &error))
239 /* Dispatch the message */
240 if (!bus_transaction_send (transaction, recipient, message))
242 BUS_SET_OOM (&error);
249 if (dbus_error_is_set (&error))
251 if (!dbus_connection_get_is_connected (connection))
253 /* If we disconnected it, we won't bother to send it any error
256 _dbus_verbose ("Not sending error to connection we disconnected\n");
258 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
260 bus_connection_send_oom_error (connection, message);
262 /* cancel transaction due to OOM */
263 if (transaction != NULL)
265 bus_transaction_cancel_and_free (transaction);
271 /* Try to send the real error, if no mem to do that, send
274 _dbus_assert (transaction != NULL);
276 if (!bus_transaction_send_error_reply (transaction, connection,
279 bus_connection_send_oom_error (connection, message);
281 /* cancel transaction due to OOM */
282 if (transaction != NULL)
284 bus_transaction_cancel_and_free (transaction);
290 dbus_error_free (&error);
293 if (transaction != NULL)
295 bus_transaction_execute_and_free (transaction);
298 dbus_connection_unref (connection);
301 static DBusHandlerResult
302 bus_dispatch_message_handler (DBusMessageHandler *handler,
303 DBusConnection *connection,
304 DBusMessage *message,
307 bus_dispatch (connection, message);
309 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
313 message_handler_slot_ref (void)
315 if (message_handler_slot < 0)
317 message_handler_slot = dbus_connection_allocate_data_slot ();
319 if (message_handler_slot < 0)
322 _dbus_assert (message_handler_slot_refcount == 0);
325 message_handler_slot_refcount += 1;
331 message_handler_slot_unref (void)
333 _dbus_assert (message_handler_slot_refcount > 0);
335 message_handler_slot_refcount -= 1;
337 if (message_handler_slot_refcount == 0)
339 dbus_connection_free_data_slot (message_handler_slot);
340 message_handler_slot = -1;
345 free_message_handler (void *data)
347 DBusMessageHandler *handler = data;
349 _dbus_assert (message_handler_slot >= 0);
350 _dbus_assert (message_handler_slot_refcount > 0);
352 dbus_message_handler_unref (handler);
353 message_handler_slot_unref ();
357 bus_dispatch_add_connection (DBusConnection *connection)
359 DBusMessageHandler *handler;
361 if (!message_handler_slot_ref ())
364 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
367 message_handler_slot_unref ();
371 if (!dbus_connection_add_filter (connection, handler))
373 dbus_message_handler_unref (handler);
374 message_handler_slot_unref ();
379 _dbus_assert (message_handler_slot >= 0);
380 _dbus_assert (message_handler_slot_refcount > 0);
382 if (!dbus_connection_set_data (connection,
383 message_handler_slot,
385 free_message_handler))
387 dbus_message_handler_unref (handler);
388 message_handler_slot_unref ();
397 bus_dispatch_remove_connection (DBusConnection *connection)
399 /* Here we tell the bus driver that we want to get off. */
400 bus_driver_remove_connection (connection);
402 dbus_connection_set_data (connection,
403 message_handler_slot,
407 #ifdef DBUS_BUILD_TESTS
409 typedef dbus_bool_t (* Check1Func) (BusContext *context);
410 typedef dbus_bool_t (* Check2Func) (BusContext *context,
411 DBusConnection *connection);
413 static dbus_bool_t check_no_leftovers (BusContext *context);
416 block_connection_until_message_from_bus (BusContext *context,
417 DBusConnection *connection)
419 while (dbus_connection_get_dispatch_status (connection) ==
420 DBUS_DISPATCH_COMPLETE &&
421 dbus_connection_get_is_connected (connection))
423 bus_test_run_bus_loop (context, TRUE);
424 bus_test_run_clients_loop (FALSE);
428 /* compensate for fact that pop_message() can return #NULL due to OOM */
430 pop_message_waiting_for_memory (DBusConnection *connection)
432 while (dbus_connection_get_dispatch_status (connection) ==
433 DBUS_DISPATCH_NEED_MEMORY)
434 _dbus_wait_for_memory ();
436 return dbus_connection_pop_message (connection);
441 const char *expected_service_name;
443 } CheckServiceDeletedData;
446 check_service_deleted_foreach (DBusConnection *connection,
449 CheckServiceDeletedData *d = data;
450 DBusMessage *message;
454 dbus_error_init (&error);
458 message = pop_message_waiting_for_memory (connection);
461 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
462 connection, DBUS_MESSAGE_SERVICE_DELETED);
465 else if (!dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_DELETED))
467 _dbus_warn ("Received message %s on %p, expecting %s\n",
468 dbus_message_get_name (message),
469 connection, DBUS_MESSAGE_SERVICE_DELETED);
474 if (!dbus_message_get_args (message, &error,
475 DBUS_TYPE_STRING, &service_name,
478 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
480 _dbus_verbose ("no memory to get service name arg\n");
484 _dbus_assert (dbus_error_is_set (&error));
485 _dbus_warn ("Did not get the expected single string argument\n");
489 else if (strcmp (service_name, d->expected_service_name) != 0)
491 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
492 d->expected_service_name,
501 dbus_free (service_name);
502 dbus_error_free (&error);
505 dbus_message_unref (message);
511 kill_client_connection (BusContext *context,
512 DBusConnection *connection)
516 CheckServiceDeletedData csdd;
518 _dbus_verbose ("killing connection %p\n", connection);
520 s = dbus_bus_get_base_service (connection);
521 _dbus_assert (s != NULL);
523 while ((base_service = _dbus_strdup (s)) == NULL)
524 _dbus_wait_for_memory ();
526 dbus_connection_ref (connection);
528 /* kick in the disconnect handler that unrefs the connection */
529 dbus_connection_disconnect (connection);
531 bus_test_run_everything (context);
533 _dbus_assert (bus_test_client_listed (connection));
535 /* Run disconnect handler in test.c */
536 if (bus_connection_dispatch_one_message (connection))
537 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
539 _dbus_assert (!dbus_connection_get_is_connected (connection));
540 dbus_connection_unref (connection);
542 _dbus_assert (!bus_test_client_listed (connection));
544 csdd.expected_service_name = base_service;
547 bus_test_clients_foreach (check_service_deleted_foreach,
550 dbus_free (base_service);
553 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
555 if (!check_no_leftovers (context))
556 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
560 kill_client_connection_unchecked (DBusConnection *connection)
562 /* This kills the connection without expecting it to affect
563 * the rest of the bus.
565 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
567 dbus_connection_ref (connection);
568 dbus_connection_disconnect (connection);
569 /* dispatching disconnect handler will unref once */
570 if (bus_connection_dispatch_one_message (connection))
571 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
573 _dbus_assert (!bus_test_client_listed (connection));
574 dbus_connection_unref (connection);
580 } CheckNoMessagesData;
583 check_no_messages_foreach (DBusConnection *connection,
586 CheckNoMessagesData *d = data;
587 DBusMessage *message;
589 message = pop_message_waiting_for_memory (connection);
592 _dbus_warn ("Received message %s on %p, expecting no messages\n",
593 dbus_message_get_name (message), connection);
598 dbus_message_unref (message);
604 DBusConnection *skip_connection;
605 const char *expected_service_name;
607 } CheckServiceCreatedData;
610 check_service_created_foreach (DBusConnection *connection,
613 CheckServiceCreatedData *d = data;
614 DBusMessage *message;
618 if (connection == d->skip_connection)
621 dbus_error_init (&error);
625 message = pop_message_waiting_for_memory (connection);
628 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
629 connection, DBUS_MESSAGE_SERVICE_CREATED);
632 else if (!dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_CREATED))
634 _dbus_warn ("Received message %s on %p, expecting %s\n",
635 dbus_message_get_name (message),
636 connection, DBUS_MESSAGE_SERVICE_CREATED);
641 if (!dbus_message_get_args (message, &error,
642 DBUS_TYPE_STRING, &service_name,
645 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
647 _dbus_verbose ("no memory to get service name arg\n");
651 _dbus_assert (dbus_error_is_set (&error));
652 _dbus_warn ("Did not get the expected single string argument\n");
656 else if (strcmp (service_name, d->expected_service_name) != 0)
658 _dbus_warn ("expected creation of service %s, got creation of %s\n",
659 d->expected_service_name,
668 dbus_free (service_name);
669 dbus_error_free (&error);
672 dbus_message_unref (message);
678 check_no_leftovers (BusContext *context)
680 CheckNoMessagesData nmd;
683 bus_test_clients_foreach (check_no_messages_foreach,
692 /* returns TRUE if the correct thing happens,
693 * but the correct thing may include OOM errors.
696 check_hello_message (BusContext *context,
697 DBusConnection *connection)
699 DBusMessage *message;
707 dbus_error_init (&error);
712 message = dbus_message_new (DBUS_MESSAGE_HELLO,
718 if (!dbus_connection_send (connection, message, &serial))
720 dbus_message_unref (message);
724 dbus_message_unref (message);
727 /* send our message */
728 bus_test_run_clients_loop (TRUE);
730 dbus_connection_ref (connection); /* because we may get disconnected */
731 block_connection_until_message_from_bus (context, connection);
733 if (!dbus_connection_get_is_connected (connection))
735 _dbus_verbose ("connection was disconnected\n");
737 dbus_connection_unref (connection);
742 dbus_connection_unref (connection);
744 message = pop_message_waiting_for_memory (connection);
747 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
748 DBUS_MESSAGE_HELLO, serial, connection);
752 _dbus_verbose ("Received %s on %p\n",
753 dbus_message_get_name (message), connection);
755 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
757 _dbus_warn ("Message has wrong sender %s\n",
758 dbus_message_get_sender (message) ?
759 dbus_message_get_sender (message) : "(none)");
763 if (dbus_message_get_is_error (message))
765 if (dbus_message_has_name (message,
766 DBUS_ERROR_NO_MEMORY))
768 ; /* good, this is a valid response */
772 _dbus_warn ("Did not expect error %s\n",
773 dbus_message_get_name (message));
779 CheckServiceCreatedData scd;
781 if (dbus_message_has_name (message,
784 ; /* good, expected */
788 _dbus_warn ("Did not expect reply %s\n",
789 dbus_message_get_name (message));
793 retry_get_hello_name:
794 if (!dbus_message_get_args (message, &error,
795 DBUS_TYPE_STRING, &name,
798 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
800 _dbus_verbose ("no memory to get service name arg from hello\n");
801 dbus_error_free (&error);
802 _dbus_wait_for_memory ();
803 goto retry_get_hello_name;
807 _dbus_assert (dbus_error_is_set (&error));
808 _dbus_warn ("Did not get the expected single string argument to hello\n");
813 _dbus_verbose ("Got hello name: %s\n", name);
815 while (!dbus_bus_set_base_service (connection, name))
816 _dbus_wait_for_memory ();
818 scd.skip_connection = NULL;
820 scd.expected_service_name = name;
821 bus_test_clients_foreach (check_service_created_foreach,
827 /* Client should also have gotten ServiceAcquired */
828 dbus_message_unref (message);
829 message = pop_message_waiting_for_memory (connection);
832 _dbus_warn ("Expecting %s, got nothing\n",
833 DBUS_MESSAGE_SERVICE_ACQUIRED);
837 retry_get_acquired_name:
838 if (!dbus_message_get_args (message, &error,
839 DBUS_TYPE_STRING, &acquired,
842 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
844 _dbus_verbose ("no memory to get service name arg from acquired\n");
845 dbus_error_free (&error);
846 _dbus_wait_for_memory ();
847 goto retry_get_acquired_name;
851 _dbus_assert (dbus_error_is_set (&error));
852 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
857 _dbus_verbose ("Got acquired name: %s\n", acquired);
859 if (strcmp (acquired, name) != 0)
861 _dbus_warn ("Acquired name is %s but expected %s\n",
867 if (!check_no_leftovers (context))
873 dbus_error_free (&error);
876 dbus_free (acquired);
879 dbus_message_unref (message);
884 /* returns TRUE if the correct thing happens,
885 * but the correct thing may include OOM errors.
888 check_hello_connection (BusContext *context)
890 DBusConnection *connection;
893 dbus_error_init (&error);
895 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
896 if (connection == NULL)
898 _DBUS_ASSERT_ERROR_IS_SET (&error);
899 dbus_error_free (&error);
903 if (!bus_setup_debug_client (connection))
905 dbus_connection_disconnect (connection);
906 dbus_connection_unref (connection);
910 if (!check_hello_message (context, connection))
913 if (dbus_bus_get_base_service (connection) == NULL)
915 /* We didn't successfully register, so we can't
916 * do the usual kill_client_connection() checks
918 kill_client_connection_unchecked (connection);
922 kill_client_connection (context, connection);
928 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
930 /* returns TRUE if the correct thing happens,
931 * but the correct thing may include OOM errors.
934 check_nonexistent_service_activation (BusContext *context,
935 DBusConnection *connection)
937 DBusMessage *message;
942 dbus_error_init (&error);
944 message = dbus_message_new (DBUS_MESSAGE_ACTIVATE_SERVICE,
950 if (!dbus_message_append_args (message,
951 DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
955 dbus_message_unref (message);
959 if (!dbus_connection_send (connection, message, &serial))
961 dbus_message_unref (message);
965 dbus_message_unref (message);
968 bus_test_run_everything (context);
969 block_connection_until_message_from_bus (context, connection);
970 bus_test_run_everything (context);
972 if (!dbus_connection_get_is_connected (connection))
974 _dbus_verbose ("connection was disconnected\n");
980 message = pop_message_waiting_for_memory (connection);
983 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
984 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
988 _dbus_verbose ("Received %s on %p\n",
989 dbus_message_get_name (message), connection);
991 if (dbus_message_get_is_error (message))
993 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
995 _dbus_warn ("Message has wrong sender %s\n",
996 dbus_message_get_sender (message) ?
997 dbus_message_get_sender (message) : "(none)");
1001 if (dbus_message_has_name (message,
1002 DBUS_ERROR_NO_MEMORY))
1004 ; /* good, this is a valid response */
1006 else if (dbus_message_has_name (message,
1007 DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1009 ; /* good, this is expected also */
1013 _dbus_warn ("Did not expect error %s\n",
1014 dbus_message_get_name (message));
1020 _dbus_warn ("Did not expect to successfully activate %s\n",
1021 NONEXISTENT_SERVICE_NAME);
1029 dbus_message_unref (message);
1035 check_base_service_activated (BusContext *context,
1036 DBusConnection *connection,
1037 DBusMessage *initial_message,
1038 char **base_service_p)
1040 DBusMessage *message;
1045 base_service = NULL;
1048 dbus_error_init (&error);
1050 message = initial_message;
1051 dbus_message_ref (message);
1053 if (dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_CREATED))
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",
1072 dbus_message_get_name (message),
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 _dbus_warn ("Expected to get base service ServiceCreated, instead got %s\n",
1101 dbus_message_get_name (message));
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_has_name (message, DBUS_MESSAGE_SERVICE_CREATED))
1145 CheckServiceCreatedData scd;
1147 reget_service_name_arg:
1148 if (!dbus_message_get_args (message, &error,
1149 DBUS_TYPE_STRING, &service_name,
1152 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1154 dbus_error_free (&error);
1155 _dbus_wait_for_memory ();
1156 goto reget_service_name_arg;
1160 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1161 dbus_message_get_name (message),
1163 dbus_error_free (&error);
1168 if (strcmp (service_name, activated_name) != 0)
1170 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1171 activated_name, service_name);
1172 dbus_free (service_name);
1176 scd.skip_connection = connection;
1178 scd.expected_service_name = service_name;
1179 bus_test_clients_foreach (check_service_created_foreach,
1182 dbus_free (service_name);
1187 dbus_message_unref (message);
1188 message = pop_message_waiting_for_memory (connection);
1189 if (message == NULL)
1191 _dbus_warn ("Expected a reply to %s, got nothing\n",
1192 DBUS_MESSAGE_ACTIVATE_SERVICE);
1198 _dbus_warn ("Expected to get service %s ServiceCreated, instead got %s\n",
1199 activated_name, dbus_message_get_name (message));
1203 if (!dbus_message_has_name (message, DBUS_MESSAGE_ACTIVATE_SERVICE))
1205 _dbus_warn ("Expected reply to %s, got message %s instead\n",
1206 DBUS_MESSAGE_ACTIVATE_SERVICE,
1207 dbus_message_get_name (message));
1211 activation_result = 0;
1212 if (!dbus_message_get_args (message, &error,
1213 DBUS_TYPE_UINT32, &activation_result,
1216 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1218 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1219 DBUS_MESSAGE_ACTIVATE_SERVICE, error.message);
1220 dbus_error_free (&error);
1224 dbus_error_free (&error);
1228 if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1230 else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1234 _dbus_warn ("Activation result was 0x%x, no good.\n",
1240 dbus_message_unref (message);
1243 if (!check_no_leftovers (context))
1245 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1253 dbus_message_unref (message);
1259 check_service_deactivated (BusContext *context,
1260 DBusConnection *connection,
1261 const char *activated_name,
1262 const char *base_service)
1264 DBusMessage *message;
1267 CheckServiceDeletedData csdd;
1272 dbus_error_init (&error);
1274 /* Now we are expecting ServiceDeleted messages for the base
1275 * service and the activated_name. The base service
1276 * notification is required to come last.
1278 csdd.expected_service_name = activated_name;
1279 csdd.failed = FALSE;
1280 bus_test_clients_foreach (check_service_deleted_foreach,
1286 csdd.expected_service_name = base_service;
1287 csdd.failed = FALSE;
1288 bus_test_clients_foreach (check_service_deleted_foreach,
1294 if (!check_no_leftovers (context))
1296 _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1304 dbus_message_unref (message);
1310 check_send_exit_to_service (BusContext *context,
1311 DBusConnection *connection,
1312 const char *service_name,
1313 const char *base_service)
1315 dbus_bool_t got_error;
1316 DBusMessage *message;
1317 dbus_int32_t serial;
1320 _dbus_verbose ("Sending exit message to the test service\n");
1324 /* Kill off the test service by sending it a quit message */
1325 message = dbus_message_new ("org.freedesktop.DBus.TestSuiteExit",
1328 if (message == NULL)
1330 /* Do this again; we still need the service to exit... */
1331 if (!check_send_exit_to_service (context, connection,
1332 service_name, base_service))
1338 if (!dbus_connection_send (connection, message, &serial))
1340 dbus_message_unref (message);
1342 /* Do this again; we still need the service to exit... */
1343 if (!check_send_exit_to_service (context, connection,
1344 service_name, base_service))
1350 dbus_message_unref (message);
1354 bus_test_run_clients_loop (TRUE);
1356 /* read it in and write it out to test service */
1357 bus_test_run_bus_loop (context, FALSE);
1359 /* see if we got an error during message bus dispatching */
1360 bus_test_run_clients_loop (FALSE);
1361 message = dbus_connection_borrow_message (connection);
1362 got_error = message != NULL && dbus_message_get_is_error (message);
1365 dbus_connection_return_message (connection, message);
1371 /* If no error, wait for the test service to exit */
1372 block_connection_until_message_from_bus (context, connection);
1374 bus_test_run_everything (context);
1379 message = pop_message_waiting_for_memory (connection);
1380 _dbus_assert (message != NULL);
1382 if (!dbus_message_get_is_error (message))
1384 _dbus_warn ("expecting an error reply to asking test service to exit, got %s\n",
1385 dbus_message_get_name (message));
1388 else if (!dbus_message_has_name (message, DBUS_ERROR_NO_MEMORY))
1390 _dbus_warn ("not expecting error %s when asking test service to exit\n",
1391 dbus_message_get_name (message));
1395 _dbus_verbose ("Got error %s when asking test service to exit\n",
1396 dbus_message_get_name (message));
1398 /* Do this again; we still need the service to exit... */
1399 if (!check_send_exit_to_service (context, connection,
1400 service_name, base_service))
1405 if (!check_service_deactivated (context, connection,
1406 service_name, base_service))
1414 dbus_message_unref (message);
1420 check_got_error (BusContext *context,
1421 DBusConnection *connection,
1422 const char *first_error_name,
1425 DBusMessage *message;
1428 dbus_bool_t error_found;
1429 const char *error_name;
1433 message = pop_message_waiting_for_memory (connection);
1434 if (message == NULL)
1436 _dbus_warn ("Did not get an expected error\n");
1440 if (!dbus_message_get_is_error (message))
1442 _dbus_warn ("Expected an error, got %s\n",
1443 dbus_message_get_name (message));
1447 error_found = FALSE;
1449 va_start (ap, first_error_name);
1450 error_name = first_error_name;
1451 while (error_name != NULL)
1453 if (dbus_message_has_name (message, error_name))
1458 error_name = va_arg (ap, char*);
1464 _dbus_warn ("Expected error %s or other, got %s instead\n",
1466 dbus_message_get_name (message));
1474 dbus_message_unref (message);
1479 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1481 /* returns TRUE if the correct thing happens,
1482 * but the correct thing may include OOM errors.
1485 check_existent_service_activation (BusContext *context,
1486 DBusConnection *connection)
1488 DBusMessage *message;
1489 dbus_int32_t serial;
1494 base_service = NULL;
1496 dbus_error_init (&error);
1498 message = dbus_message_new (DBUS_MESSAGE_ACTIVATE_SERVICE,
1501 if (message == NULL)
1504 if (!dbus_message_append_args (message,
1505 DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1506 DBUS_TYPE_UINT32, 0,
1509 dbus_message_unref (message);
1513 if (!dbus_connection_send (connection, message, &serial))
1515 dbus_message_unref (message);
1519 dbus_message_unref (message);
1522 bus_test_run_everything (context);
1524 /* now wait for the message bus to hear back from the activated
1527 block_connection_until_message_from_bus (context, connection);
1529 bus_test_run_everything (context);
1531 if (!dbus_connection_get_is_connected (connection))
1533 _dbus_verbose ("connection was disconnected\n");
1539 message = pop_message_waiting_for_memory (connection);
1540 if (message == NULL)
1542 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1543 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
1547 _dbus_verbose ("Received %s on %p after sending %s\n",
1548 dbus_message_get_name (message), connection,
1549 DBUS_MESSAGE_ACTIVATE_SERVICE);
1551 if (dbus_message_get_is_error (message))
1553 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1555 _dbus_warn ("Message has wrong sender %s\n",
1556 dbus_message_get_sender (message) ?
1557 dbus_message_get_sender (message) : "(none)");
1561 if (dbus_message_has_name (message,
1562 DBUS_ERROR_NO_MEMORY))
1564 ; /* good, this is a valid response */
1566 else if (dbus_message_has_name (message,
1567 DBUS_ERROR_SPAWN_CHILD_EXITED))
1569 ; /* good, this is expected also */
1573 _dbus_warn ("Did not expect error %s\n",
1574 dbus_message_get_name (message));
1580 dbus_bool_t got_service_deleted;
1581 dbus_bool_t got_error;
1583 if (!check_base_service_activated (context, connection,
1584 message, &base_service))
1587 dbus_message_unref (message);
1590 /* We may need to block here for the test service to exit or finish up */
1591 block_connection_until_message_from_bus (context, connection);
1593 message = dbus_connection_borrow_message (connection);
1594 if (message == NULL)
1596 _dbus_warn ("Did not receive any messages after base service creation notification\n");
1600 got_service_deleted = dbus_message_has_name (message, DBUS_MESSAGE_SERVICE_DELETED);
1601 got_error = dbus_message_get_is_error (message);
1603 dbus_connection_return_message (connection, message);
1608 if (!check_got_error (context, connection,
1609 DBUS_ERROR_SPAWN_CHILD_EXITED,
1610 DBUS_ERROR_NO_MEMORY,
1614 /* A service deleted should be coming along now after this error.
1615 * We can also get the error *after* the service deleted.
1617 got_service_deleted = TRUE;
1620 if (got_service_deleted)
1622 /* The service started up and got a base address, but then
1623 * failed to register under EXISTENT_SERVICE_NAME
1625 CheckServiceDeletedData csdd;
1627 csdd.expected_service_name = base_service;
1628 csdd.failed = FALSE;
1629 bus_test_clients_foreach (check_service_deleted_foreach,
1635 /* Now we should get an error about the service exiting
1636 * if we didn't get it before.
1640 block_connection_until_message_from_bus (context, connection);
1642 /* and process everything again */
1643 bus_test_run_everything (context);
1645 if (!check_got_error (context, connection,
1646 DBUS_ERROR_SPAWN_CHILD_EXITED,
1653 message = pop_message_waiting_for_memory (connection);
1654 if (message == NULL)
1656 _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1660 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1661 base_service, message))
1664 dbus_message_unref (message);
1668 if (!check_no_leftovers (context))
1670 _dbus_warn ("Messages were left over after successful activation\n");
1674 if (!check_send_exit_to_service (context, connection,
1675 EXISTENT_SERVICE_NAME, base_service))
1684 dbus_message_unref (message);
1687 dbus_free (base_service);
1692 /* returns TRUE if the correct thing happens,
1693 * but the correct thing may include OOM errors.
1696 check_segfault_service_activation (BusContext *context,
1697 DBusConnection *connection)
1699 DBusMessage *message;
1700 dbus_int32_t serial;
1704 dbus_error_init (&error);
1706 message = dbus_message_new (DBUS_MESSAGE_ACTIVATE_SERVICE,
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 DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
1751 _dbus_verbose ("Received %s on %p\n",
1752 dbus_message_get_name (message), connection);
1754 if (dbus_message_get_is_error (message))
1756 if (!dbus_message_has_sender (message, DBUS_SERVICE_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_has_name (message,
1765 DBUS_ERROR_NO_MEMORY))
1767 ; /* good, this is a valid response */
1769 else if (dbus_message_has_name (message,
1770 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1772 ; /* good, this is expected also */
1776 _dbus_warn ("Did not expect error %s\n",
1777 dbus_message_get_name (message));
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 */