1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "connection.h"
30 #include <dbus/dbus-internals.h>
33 static int message_handler_slot;
38 BusTransaction *transaction;
43 send_one_message (DBusConnection *connection, void *data)
45 SendMessageData *d = data;
47 if (!bus_connection_is_active (connection))
50 if (!bus_transaction_send_message (d->transaction,
54 BUS_SET_OOM (d->error);
62 bus_dispatch_broadcast_message (BusTransaction *transaction,
68 BusConnections *connections;
70 _dbus_assert (dbus_message_get_sender (message) != NULL);
72 connections = bus_transaction_get_connections (transaction);
74 dbus_error_init (&tmp_error);
76 d.transaction = transaction;
79 bus_connections_foreach (connections, send_one_message, &d);
81 if (dbus_error_is_set (&tmp_error))
83 dbus_move_error (&tmp_error, error);
91 send_service_nonexistent_error (BusTransaction *transaction,
92 DBusConnection *connection,
93 const char *service_name,
94 DBusMessage *in_reply_to,
97 DBusMessage *error_reply;
98 DBusString error_message;
99 const char *error_str;
101 /* Trying to send a message to a non-existant service,
102 * bounce back an error message.
105 if (!_dbus_string_init (&error_message, _DBUS_INT_MAX))
111 if (!_dbus_string_append (&error_message, "Service \"") ||
112 !_dbus_string_append (&error_message, service_name) ||
113 !_dbus_string_append (&error_message, "does not exist"))
115 _dbus_string_free (&error_message);
120 _dbus_string_get_const_data (&error_message, &error_str);
121 error_reply = dbus_message_new_error_reply (in_reply_to,
122 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
125 _dbus_string_free (&error_message);
127 if (error_reply == NULL)
133 if (!bus_transaction_send_message (transaction, connection, error_reply))
135 dbus_message_unref (error_reply);
140 dbus_message_unref (error_reply);
145 static DBusHandlerResult
146 bus_dispatch_message_handler (DBusMessageHandler *handler,
147 DBusConnection *connection,
148 DBusMessage *message,
151 const char *sender, *service_name, *message_name;
153 BusTransaction *transaction;
157 dbus_error_init (&error);
159 context = bus_connection_get_context (connection);
160 _dbus_assert (context != NULL);
162 /* If we can't even allocate an OOM error, we just go to sleep
165 while (!bus_connection_preallocate_oom_error (connection))
166 bus_wait_for_memory ();
168 /* Ref connection in case we disconnect it at some point in here */
169 dbus_connection_ref (connection);
171 service_name = dbus_message_get_service (message);
172 message_name = dbus_message_get_name (message);
174 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
176 /* If service_name is NULL, this is a message to the bus daemon, not intended
177 * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these
178 * immediately, especially disconnection messages.
180 if (service_name == NULL)
182 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
183 bus_connection_disconnected (connection);
185 /* DBusConnection also handles some of these automatically, we leave
191 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
193 /* Create our transaction */
194 transaction = bus_transaction_new (context);
195 if (transaction == NULL)
197 BUS_SET_OOM (&error);
201 /* Assign a sender to the message */
202 if (bus_connection_is_active (connection))
204 sender = bus_connection_get_name (connection);
205 _dbus_assert (sender != NULL);
207 if (!dbus_message_set_sender (message, sender))
209 BUS_SET_OOM (&error);
214 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
216 if (!bus_driver_handle_message (connection, transaction, message, &error))
219 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
221 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
222 dbus_connection_disconnect (connection);
224 /* FIXME what if we un-special-case this service and just have a flag
225 * on services that all service owners will get messages to it, not just
228 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
230 if (!bus_dispatch_broadcast_message (transaction, message, &error))
233 else /* route to named service */
235 DBusString service_string;
237 BusRegistry *registry;
239 registry = bus_connection_get_registry (connection);
241 _dbus_string_init_const (&service_string, service_name);
242 service = bus_registry_lookup (registry, &service_string);
246 if (!send_service_nonexistent_error (transaction, connection,
253 _dbus_assert (bus_service_get_primary_owner (service) != NULL);
255 /* Dispatch the message */
256 if (!bus_transaction_send_message (transaction,
257 bus_service_get_primary_owner (service),
260 BUS_SET_OOM (&error);
267 if (dbus_error_is_set (&error))
269 if (!dbus_connection_get_is_connected (connection))
271 /* If we disconnected it, we won't bother to send it any error
275 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
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 /* Try to send the real error, if no mem to do that, send
291 _dbus_assert (transaction != NULL);
293 if (!bus_transaction_send_error_reply (transaction, connection,
296 bus_connection_send_oom_error (connection, message);
298 /* cancel transaction due to OOM */
299 if (transaction != NULL)
301 bus_transaction_cancel_and_free (transaction);
307 dbus_error_free (&error);
310 if (transaction != NULL)
312 bus_transaction_execute_and_free (transaction);
315 dbus_connection_unref (connection);
317 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
321 bus_dispatch_add_connection (DBusConnection *connection)
323 DBusMessageHandler *handler;
325 message_handler_slot = dbus_connection_allocate_data_slot ();
327 if (message_handler_slot < 0)
330 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
332 if (!dbus_connection_add_filter (connection, handler))
334 dbus_message_handler_unref (handler);
339 if (!dbus_connection_set_data (connection,
340 message_handler_slot,
342 (DBusFreeFunction)dbus_message_handler_unref))
344 dbus_connection_remove_filter (connection, handler);
345 dbus_message_handler_unref (handler);
354 bus_dispatch_remove_connection (DBusConnection *connection)
356 /* Here we tell the bus driver that we want to get off. */
357 bus_driver_remove_connection (connection);
359 dbus_connection_set_data (connection,
360 message_handler_slot,