d9fe81acf3bfdf98b766872d53814f0ef8bfec6f
[platform/upstream/dbus.git] / bus / dispatch.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c  Message dispatcher
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include "dispatch.h"
25 #include "connection.h"
26 #include "driver.h"
27 #include "utils.h"
28 #include <dbus/dbus-internals.h>
29 #include <string.h>
30
31 static int message_handler_slot;
32
33 typedef struct
34 {
35   DBusMessage    *message;
36   BusTransaction *transaction;
37   DBusError      *error;
38 } SendMessageData;
39
40 static dbus_bool_t
41 send_one_message (DBusConnection *connection, void *data)
42 {
43   SendMessageData *d = data;
44   
45   if (!bus_connection_is_active (connection))
46     return TRUE;
47
48   if (!bus_transaction_send_message (d->transaction,
49                                      connection,
50                                      d->message))
51     {
52       BUS_SET_OOM (d->error);
53       return FALSE;
54     }
55
56   return TRUE;
57 }
58
59 dbus_bool_t
60 bus_dispatch_broadcast_message (BusTransaction *transaction,
61                                 DBusMessage    *message,
62                                 DBusError      *error)
63 {
64   DBusError tmp_error;
65   SendMessageData d;
66   
67   _dbus_assert (dbus_message_get_sender (message) != NULL);
68
69   dbus_error_init (&tmp_error);
70   d.message = message;
71   d.transaction = transaction;
72   d.error = &tmp_error;
73   bus_connection_foreach (send_one_message, &d);
74
75   if (dbus_error_is_set (&tmp_error))
76     {
77       dbus_move_error (&tmp_error, error);
78       return FALSE;
79     }
80   else
81     return TRUE;
82 }
83
84 static dbus_bool_t
85 send_service_nonexistent_error (BusTransaction *transaction,
86                                 DBusConnection *connection,
87                                 const char     *service_name,
88                                 DBusMessage    *in_reply_to,
89                                 DBusError      *error)
90 {
91   DBusMessage *error_reply;
92   DBusString error_message;
93   const char *error_str;
94           
95   /* Trying to send a message to a non-existant service,
96    * bounce back an error message.
97    */
98           
99   if (!_dbus_string_init (&error_message, _DBUS_INT_MAX))
100     {
101       BUS_SET_OOM (error);
102       return FALSE;
103     }
104
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"))
108     {
109       _dbus_string_free (&error_message);
110       BUS_SET_OOM (error);
111       return FALSE;
112     }
113               
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,
117                                               error_str);
118
119   _dbus_string_free (&error_message);
120               
121   if (error_reply == NULL)
122     {
123       BUS_SET_OOM (error);
124       return FALSE;
125     }
126               
127   if (!bus_transaction_send_message (transaction, connection, error_reply))
128     {
129       dbus_message_unref (error_reply);
130       BUS_SET_OOM (error);
131       return FALSE;
132     }
133               
134   dbus_message_unref (error_reply);
135
136   return TRUE;
137 }
138
139 static DBusHandlerResult
140 bus_dispatch_message_handler (DBusMessageHandler *handler,
141                               DBusConnection     *connection,
142                               DBusMessage        *message,
143                               void               *user_data)
144 {
145   const char *sender, *service_name, *message_name;
146   DBusError error;
147   BusTransaction *transaction;
148
149   transaction = NULL;
150   dbus_error_init (&error);
151   
152   /* If we can't even allocate an OOM error, we just go to sleep
153    * until we can.
154    */
155   while (!bus_connection_preallocate_oom_error (connection))
156     bus_wait_for_memory ();
157   
158   /* Ref connection in case we disconnect it at some point in here */
159   dbus_connection_ref (connection);
160
161   service_name = dbus_message_get_service (message);
162   message_name = dbus_message_get_name (message);
163
164   _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
165
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.
169    */
170   if (service_name == NULL)
171     {
172       if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
173         bus_connection_disconnected (connection);
174
175       /* DBusConnection also handles some of these automatically, we leave
176        * it to do so.
177        */
178       goto out;
179     }
180
181   _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
182   
183   /* Create our transaction */
184   transaction = bus_transaction_new ();
185   if (transaction == NULL)
186     {
187       BUS_SET_OOM (&error);
188       goto out;
189     }
190   
191   /* Assign a sender to the message */
192   if (bus_connection_is_active (connection))
193     {
194       sender = bus_connection_get_name (connection);
195       _dbus_assert (sender != NULL);
196       
197       if (!dbus_message_set_sender (message, sender))
198         {
199           BUS_SET_OOM (&error);
200           goto out;
201         }
202     }
203
204   if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
205     {
206       if (!bus_driver_handle_message (connection, transaction, message, &error))
207         goto out;
208     }
209   else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
210     {
211       _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
212       dbus_connection_disconnect (connection);
213     }
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
216    * the primary owner.
217    */
218   else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
219     {
220       if (!bus_dispatch_broadcast_message (transaction, message, &error))
221         goto out;
222     }
223   else  /* route to named service */
224     {
225       DBusString service_string;
226       BusService *service;
227
228       _dbus_string_init_const (&service_string, service_name);
229       service = bus_service_lookup (&service_string);
230
231       if (service == NULL)
232         {
233           if (!send_service_nonexistent_error (transaction, connection,
234                                                service_name,
235                                                message, &error))
236             goto out;
237         }
238       else
239         {
240           _dbus_assert (bus_service_get_primary_owner (service) != NULL);
241       
242           /* Dispatch the message */
243           if (!bus_transaction_send_message (transaction,
244                                              bus_service_get_primary_owner (service),
245                                              message))
246             {
247               BUS_SET_OOM (&error);
248               goto out;
249             }
250         }
251     }
252   
253  out:
254   if (dbus_error_is_set (&error))
255     {
256       if (!dbus_connection_get_is_connected (connection))
257         {
258           /* If we disconnected it, we won't bother to send it any error
259            * messages.
260            */
261         }
262       else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
263         {
264           bus_connection_send_oom_error (connection, message);
265
266           /* cancel transaction due to OOM */
267           if (transaction != NULL)
268             {
269               bus_transaction_cancel_and_free (transaction);
270               transaction = NULL;
271             }
272         }
273       else
274         {
275           /* Try to send the real error, if no mem to do that, send
276            * the OOM error
277            */
278           _dbus_assert (transaction != NULL);
279           
280           if (!bus_transaction_send_error_reply (transaction, connection,
281                                                  &error, message))
282             {
283               bus_connection_send_oom_error (connection, message);
284
285               /* cancel transaction due to OOM */
286               if (transaction != NULL)
287                 {
288                   bus_transaction_cancel_and_free (transaction);
289                   transaction = NULL;
290                 }
291             }
292         }
293       
294       dbus_error_free (&error);
295     }
296
297   if (transaction != NULL)
298     {
299       bus_transaction_execute_and_free (transaction);
300     }
301
302   dbus_connection_unref (connection);
303   
304   return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
305 }
306
307 dbus_bool_t
308 bus_dispatch_add_connection (DBusConnection *connection)
309 {
310   DBusMessageHandler *handler;
311   
312   message_handler_slot = dbus_connection_allocate_data_slot ();
313
314   if (message_handler_slot < 0)
315     return FALSE;
316
317   handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);  
318
319   if (!dbus_connection_add_filter (connection, handler))
320     {
321       dbus_message_handler_unref (handler);
322
323       return FALSE;
324     }
325
326   if (!dbus_connection_set_data (connection,
327                                  message_handler_slot,
328                                  handler,
329                                  (DBusFreeFunction)dbus_message_handler_unref))
330     {
331       dbus_connection_remove_filter (connection, handler);
332       dbus_message_handler_unref (handler);
333
334       return FALSE;
335     }
336
337   return TRUE;
338 }
339
340 void
341 bus_dispatch_remove_connection (DBusConnection *connection)
342 {
343   /* Here we tell the bus driver that we want to get off. */
344   bus_driver_remove_connection (connection);
345
346   dbus_connection_set_data (connection,
347                             message_handler_slot,
348                             NULL, NULL);
349 }
350