2003-03-13 Havoc Pennington <hp@redhat.com>
[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 "services.h"
28 #include "utils.h"
29 #include "bus.h"
30 #include "test.h"
31 #include <dbus/dbus-internals.h>
32 #include <string.h>
33
34 static int message_handler_slot;
35
36 typedef struct
37 {
38   DBusMessage    *message;
39   BusTransaction *transaction;
40   DBusError      *error;
41 } SendMessageData;
42
43 static dbus_bool_t
44 send_one_message (DBusConnection *connection, void *data)
45 {
46   SendMessageData *d = data;
47   
48   if (!bus_connection_is_active (connection))
49     return TRUE;
50
51   if (!bus_transaction_send_message (d->transaction,
52                                      connection,
53                                      d->message))
54     {
55       BUS_SET_OOM (d->error);
56       return FALSE;
57     }
58
59   return TRUE;
60 }
61
62 dbus_bool_t
63 bus_dispatch_broadcast_message (BusTransaction *transaction,
64                                 DBusMessage    *message,
65                                 DBusError      *error)
66 {
67   DBusError tmp_error;
68   SendMessageData d;
69   BusConnections *connections;
70   
71   _dbus_assert (dbus_message_get_sender (message) != NULL);
72
73   connections = bus_transaction_get_connections (transaction);
74   
75   dbus_error_init (&tmp_error);
76   d.message = message;
77   d.transaction = transaction;
78   d.error = &tmp_error;
79   
80   bus_connections_foreach (connections, send_one_message, &d);
81
82   if (dbus_error_is_set (&tmp_error))
83     {
84       dbus_move_error (&tmp_error, error);
85       return FALSE;
86     }
87   else
88     return TRUE;
89 }
90
91 static dbus_bool_t
92 send_service_nonexistent_error (BusTransaction *transaction,
93                                 DBusConnection *connection,
94                                 const char     *service_name,
95                                 DBusMessage    *in_reply_to,
96                                 DBusError      *error)
97 {
98   DBusMessage *error_reply;
99   DBusString error_message;
100   const char *error_str;
101           
102   /* Trying to send a message to a non-existant service,
103    * bounce back an error message.
104    */
105           
106   if (!_dbus_string_init (&error_message, _DBUS_INT_MAX))
107     {
108       BUS_SET_OOM (error);
109       return FALSE;
110     }
111
112   if (!_dbus_string_append (&error_message, "Service \"") ||
113       !_dbus_string_append (&error_message, service_name) ||
114       !_dbus_string_append (&error_message, "\" does not exist"))
115     {
116       _dbus_string_free (&error_message);
117       BUS_SET_OOM (error);
118       return FALSE;
119     }
120               
121   _dbus_string_get_const_data (&error_message, &error_str);
122   error_reply = dbus_message_new_error_reply (in_reply_to,
123                                               DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
124                                               error_str);
125
126   _dbus_string_free (&error_message);
127               
128   if (error_reply == NULL)
129     {
130       BUS_SET_OOM (error);
131       return FALSE;
132     }
133               
134   if (!bus_transaction_send_message (transaction, connection, error_reply))
135     {
136       dbus_message_unref (error_reply);
137       BUS_SET_OOM (error);
138       return FALSE;
139     }
140               
141   dbus_message_unref (error_reply);
142
143   return TRUE;
144 }
145
146 static void
147 bus_dispatch (DBusConnection *connection,
148               DBusMessage    *message)
149 {
150   const char *sender, *service_name, *message_name;
151   DBusError error;
152   BusTransaction *transaction;
153   BusContext *context;
154   
155   transaction = NULL;
156   dbus_error_init (&error);
157
158   context = bus_connection_get_context (connection);
159   _dbus_assert (context != NULL);
160   
161   /* If we can't even allocate an OOM error, we just go to sleep
162    * until we can.
163    */
164   while (!bus_connection_preallocate_oom_error (connection))
165     bus_wait_for_memory ();
166   
167   /* Ref connection in case we disconnect it at some point in here */
168   dbus_connection_ref (connection);
169
170   service_name = dbus_message_get_service (message);
171   message_name = dbus_message_get_name (message);
172
173   _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
174
175   /* If service_name is NULL, this is a message to the bus daemon, not intended
176    * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these
177    * immediately, especially disconnection messages.
178    */
179   if (service_name == NULL)
180     {
181       if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
182         bus_connection_disconnected (connection);
183
184       /* DBusConnection also handles some of these automatically, we leave
185        * it to do so.
186        */
187       goto out;
188     }
189
190   _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
191   
192   /* Create our transaction */
193   transaction = bus_transaction_new (context);
194   if (transaction == NULL)
195     {
196       BUS_SET_OOM (&error);
197       goto out;
198     }
199   
200   /* Assign a sender to the message */
201   if (bus_connection_is_active (connection))
202     {
203       sender = bus_connection_get_name (connection);
204       _dbus_assert (sender != NULL);
205       
206       if (!dbus_message_set_sender (message, sender))
207         {
208           BUS_SET_OOM (&error);
209           goto out;
210         }
211     }
212
213   if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
214     {
215       if (!bus_driver_handle_message (connection, transaction, message, &error))
216         goto out;
217     }
218   else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
219     {
220       _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
221       dbus_connection_disconnect (connection);
222     }
223   /* FIXME what if we un-special-case this service and just have a flag
224    * on services that all service owners will get messages to it, not just
225    * the primary owner.
226    */
227   else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
228     {
229       if (!bus_dispatch_broadcast_message (transaction, message, &error))
230         goto out;
231     }
232   else  /* route to named service */
233     {
234       DBusString service_string;
235       BusService *service;
236       BusRegistry *registry;
237
238       registry = bus_connection_get_registry (connection);
239       
240       _dbus_string_init_const (&service_string, service_name);
241       service = bus_registry_lookup (registry, &service_string);
242
243       if (service == NULL)
244         {
245           if (!send_service_nonexistent_error (transaction, connection,
246                                                service_name,
247                                                message, &error))
248             goto out;
249         }
250       else
251         {
252           _dbus_assert (bus_service_get_primary_owner (service) != NULL);
253       
254           /* Dispatch the message */
255           if (!bus_transaction_send_message (transaction,
256                                              bus_service_get_primary_owner (service),
257                                              message))
258             {
259               BUS_SET_OOM (&error);
260               goto out;
261             }
262         }
263     }
264   
265  out:
266   if (dbus_error_is_set (&error))
267     {
268       if (!dbus_connection_get_is_connected (connection))
269         {
270           /* If we disconnected it, we won't bother to send it any error
271            * messages.
272            */
273         }
274       else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
275         {
276           bus_connection_send_oom_error (connection, message);
277
278           /* cancel transaction due to OOM */
279           if (transaction != NULL)
280             {
281               bus_transaction_cancel_and_free (transaction);
282               transaction = NULL;
283             }
284         }
285       else
286         {
287           /* Try to send the real error, if no mem to do that, send
288            * the OOM error
289            */
290           _dbus_assert (transaction != NULL);
291           
292           if (!bus_transaction_send_error_reply (transaction, connection,
293                                                  &error, message))
294             {
295               bus_connection_send_oom_error (connection, message);
296
297               /* cancel transaction due to OOM */
298               if (transaction != NULL)
299                 {
300                   bus_transaction_cancel_and_free (transaction);
301                   transaction = NULL;
302                 }
303             }
304         }
305       
306       dbus_error_free (&error);
307     }
308
309   if (transaction != NULL)
310     {
311       bus_transaction_execute_and_free (transaction);
312     }
313
314   dbus_connection_unref (connection);
315 }
316
317 static DBusHandlerResult
318 bus_dispatch_message_handler (DBusMessageHandler *handler,
319                               DBusConnection     *connection,
320                               DBusMessage        *message,
321                               void               *user_data)
322 {
323   bus_dispatch (connection, message);
324   
325   return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
326 }
327
328 dbus_bool_t
329 bus_dispatch_add_connection (DBusConnection *connection)
330 {
331   DBusMessageHandler *handler;
332   
333   message_handler_slot = dbus_connection_allocate_data_slot ();
334
335   if (message_handler_slot < 0)
336     return FALSE;
337
338   handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);  
339
340   if (!dbus_connection_add_filter (connection, handler))
341     {
342       dbus_message_handler_unref (handler);
343
344       return FALSE;
345     }
346
347   if (!dbus_connection_set_data (connection,
348                                  message_handler_slot,
349                                  handler,
350                                  (DBusFreeFunction)dbus_message_handler_unref))
351     {
352       dbus_connection_remove_filter (connection, handler);
353       dbus_message_handler_unref (handler);
354
355       return FALSE;
356     }
357
358   return TRUE;
359 }
360
361 void
362 bus_dispatch_remove_connection (DBusConnection *connection)
363 {
364   /* Here we tell the bus driver that we want to get off. */
365   bus_driver_remove_connection (connection);
366
367   dbus_connection_set_data (connection,
368                             message_handler_slot,
369                             NULL, NULL);
370 }
371
372
373
374 #ifdef DBUS_BUILD_TESTS
375
376 static void
377 run_test_bus (BusContext *context)
378 {
379   
380   
381 }
382
383 static dbus_bool_t
384 check_hello_message (BusContext     *context,
385                      DBusConnection *connection)
386 {
387   DBusMessage *message;
388   dbus_int32_t serial;
389   
390   message = dbus_message_new (DBUS_SERVICE_DBUS,
391                               DBUS_MESSAGE_HELLO);
392
393   if (message == NULL)
394     _dbus_assert_not_reached ("no memory");
395
396   if (!dbus_connection_send (connection, message, &serial))
397     _dbus_assert_not_reached ("no memory");
398
399   return TRUE;
400 }
401
402 dbus_bool_t
403 bus_dispatch_test (const DBusString *test_data_dir)
404 {
405   BusContext *context;
406   DBusError error;
407   const char *activation_dirs[] = { NULL, NULL };
408   DBusConnection *foo;
409   DBusConnection *bar;
410   DBusConnection *baz;
411   DBusResultCode result;
412
413   return TRUE; /* FIXME */
414   
415   dbus_error_init (&error);
416   context = bus_context_new ("debug:name=test-server",
417                              activation_dirs,
418                              &error);
419   if (context == NULL)
420     _dbus_assert_not_reached ("could not alloc context");
421   
422   foo = dbus_connection_open ("debug:name=test-server", &result);
423   if (foo == NULL)
424     _dbus_assert_not_reached ("could not alloc connection");
425
426   bar = dbus_connection_open ("debug:name=test-server", &result);
427   if (bar == NULL)
428     _dbus_assert_not_reached ("could not alloc connection");
429
430   baz = dbus_connection_open ("debug:name=test-server", &result);
431   if (baz == NULL)
432     _dbus_assert_not_reached ("could not alloc connection");
433   
434   
435   
436   return TRUE;
437 }
438 #endif /* DBUS_BUILD_TESTS */