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"
28 #include <dbus/dbus-internals.h>
31 static int message_handler_slot;
36 BusTransaction *transaction;
41 send_one_message (DBusConnection *connection, void *data)
43 SendMessageData *d = data;
45 if (!bus_connection_is_active (connection))
48 if (!bus_transaction_send_message (d->transaction,
52 BUS_SET_OOM (d->error);
60 bus_dispatch_broadcast_message (BusTransaction *transaction,
67 _dbus_assert (dbus_message_get_sender (message) != NULL);
69 dbus_error_init (&tmp_error);
71 d.transaction = transaction;
73 bus_connection_foreach (send_one_message, &d);
75 if (dbus_error_is_set (&tmp_error))
77 dbus_move_error (&tmp_error, error);
85 send_service_nonexistent_error (BusTransaction *transaction,
86 DBusConnection *connection,
87 const char *service_name,
88 DBusMessage *in_reply_to,
91 DBusMessage *error_reply;
92 DBusString error_message;
93 const char *error_str;
95 /* Trying to send a message to a non-existant service,
96 * bounce back an error message.
99 if (!_dbus_string_init (&error_message, _DBUS_INT_MAX))
105 if (!_dbus_string_append (&error_message, "Service \"") ||
106 !_dbus_string_append (&error_message, service_name) ||
107 !_dbus_string_append (&error_message, "does not exist"))
109 _dbus_string_free (&error_message);
114 _dbus_string_get_const_data (&error_message, &error_str);
115 error_reply = dbus_message_new_error_reply (in_reply_to,
116 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
119 _dbus_string_free (&error_message);
121 if (error_reply == NULL)
127 if (!bus_transaction_send_message (transaction, connection, error_reply))
129 dbus_message_unref (error_reply);
134 dbus_message_unref (error_reply);
139 static DBusHandlerResult
140 bus_dispatch_message_handler (DBusMessageHandler *handler,
141 DBusConnection *connection,
142 DBusMessage *message,
145 const char *sender, *service_name, *message_name;
147 BusTransaction *transaction;
150 dbus_error_init (&error);
152 /* If we can't even allocate an OOM error, we just go to sleep
155 while (!bus_connection_preallocate_oom_error (connection))
156 bus_wait_for_memory ();
158 /* Ref connection in case we disconnect it at some point in here */
159 dbus_connection_ref (connection);
161 service_name = dbus_message_get_service (message);
162 message_name = dbus_message_get_name (message);
164 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
166 /* If service_name is NULL, this is a message to the bus daemon, not intended
167 * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these
168 * immediately, especially disconnection messages.
170 if (service_name == NULL)
172 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
173 bus_connection_disconnected (connection);
175 /* DBusConnection also handles some of these automatically, we leave
181 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
183 /* Create our transaction */
184 transaction = bus_transaction_new ();
185 if (transaction == NULL)
187 BUS_SET_OOM (&error);
191 /* Assign a sender to the message */
192 if (bus_connection_is_active (connection))
194 sender = bus_connection_get_name (connection);
195 _dbus_assert (sender != NULL);
197 if (!dbus_message_set_sender (message, sender))
199 BUS_SET_OOM (&error);
204 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
206 if (!bus_driver_handle_message (connection, transaction, message, &error))
209 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
211 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
212 dbus_connection_disconnect (connection);
214 /* FIXME what if we un-special-case this service and just have a flag
215 * on services that all service owners will get messages to it, not just
218 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
220 if (!bus_dispatch_broadcast_message (transaction, message, &error))
223 else /* route to named service */
225 DBusString service_string;
228 _dbus_string_init_const (&service_string, service_name);
229 service = bus_service_lookup (&service_string);
233 if (!send_service_nonexistent_error (transaction, connection,
240 _dbus_assert (bus_service_get_primary_owner (service) != NULL);
242 /* Dispatch the message */
243 if (!bus_transaction_send_message (transaction,
244 bus_service_get_primary_owner (service),
247 BUS_SET_OOM (&error);
254 if (dbus_error_is_set (&error))
256 if (!dbus_connection_get_is_connected (connection))
258 /* If we disconnected it, we won't bother to send it any error
262 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
264 bus_connection_send_oom_error (connection, message);
266 /* cancel transaction due to OOM */
267 if (transaction != NULL)
269 bus_transaction_cancel_and_free (transaction);
275 /* Try to send the real error, if no mem to do that, send
278 _dbus_assert (transaction != NULL);
280 if (!bus_transaction_send_error_reply (transaction, connection,
283 bus_connection_send_oom_error (connection, message);
285 /* cancel transaction due to OOM */
286 if (transaction != NULL)
288 bus_transaction_cancel_and_free (transaction);
294 dbus_error_free (&error);
297 if (transaction != NULL)
299 bus_transaction_execute_and_free (transaction);
302 dbus_connection_unref (connection);
304 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
308 bus_dispatch_add_connection (DBusConnection *connection)
310 DBusMessageHandler *handler;
312 message_handler_slot = dbus_connection_allocate_data_slot ();
314 if (message_handler_slot < 0)
317 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
319 if (!dbus_connection_add_filter (connection, handler))
321 dbus_message_handler_unref (handler);
326 if (!dbus_connection_set_data (connection,
327 message_handler_slot,
329 (DBusFreeFunction)dbus_message_handler_unref))
331 dbus_connection_remove_filter (connection, handler);
332 dbus_message_handler_unref (handler);
341 bus_dispatch_remove_connection (DBusConnection *connection)
343 /* Here we tell the bus driver that we want to get off. */
344 bus_driver_remove_connection (connection);
346 dbus_connection_set_data (connection,
347 message_handler_slot,