2003-03-12 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / bus / driver.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* driver.c  Bus client (driver)
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 "activation.h"
25 #include "connection.h"
26 #include "driver.h"
27 #include "dispatch.h"
28 #include "services.h"
29 #include "utils.h"
30 #include <dbus/dbus-string.h>
31 #include <dbus/dbus-internals.h>
32 #include <string.h>
33
34 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
35                                                     DBusMessage    *hello_message,
36                                                     BusTransaction *transaction,
37                                                     DBusError      *error);
38
39 dbus_bool_t
40 bus_driver_send_service_deleted (const char     *service_name,
41                                  BusTransaction *transaction,
42                                  DBusError      *error)
43 {
44   DBusMessage *message;
45   dbus_bool_t retval;
46   
47   _dbus_verbose ("sending service deleted: %s\n", service_name);
48   
49   message = dbus_message_new (DBUS_SERVICE_BROADCAST,
50                               DBUS_MESSAGE_SERVICE_DELETED);
51   if (message == NULL)
52     {
53       BUS_SET_OOM (error);
54       return FALSE;
55     }
56   
57   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
58       !dbus_message_append_args (message,
59                                  DBUS_TYPE_STRING, service_name,
60                                  0))
61     {
62       dbus_message_unref (message);
63       BUS_SET_OOM (error);
64       return FALSE;
65     }
66
67   retval = bus_dispatch_broadcast_message (transaction, message, error);
68   dbus_message_unref (message);
69
70   return retval;
71 }
72
73 dbus_bool_t
74 bus_driver_send_service_created (const char     *service_name,
75                                  BusTransaction *transaction,
76                                  DBusError      *error)
77 {
78   DBusMessage *message;
79   dbus_bool_t retval;
80   
81   message = dbus_message_new (DBUS_SERVICE_BROADCAST,
82                               DBUS_MESSAGE_SERVICE_CREATED);
83   if (message == NULL)
84     {
85       BUS_SET_OOM (error);
86       return FALSE;
87     }
88   
89   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
90     {
91       dbus_message_unref (message);
92       BUS_SET_OOM (error);
93       return FALSE;
94     }
95   
96   if (!dbus_message_append_args (message,
97                                  DBUS_TYPE_STRING, service_name,
98                                  0))
99     {
100       dbus_message_unref (message);
101       BUS_SET_OOM (error);
102       return FALSE;
103     }
104   
105   retval = bus_dispatch_broadcast_message (transaction, message, error);
106   dbus_message_unref (message);
107
108   return retval;
109 }
110
111 dbus_bool_t
112 bus_driver_send_service_lost (DBusConnection *connection,
113                               const char     *service_name,
114                               BusTransaction *transaction,
115                               DBusError      *error)
116 {
117   DBusMessage *message;
118
119   message = dbus_message_new (bus_connection_get_name (connection),
120                               DBUS_MESSAGE_SERVICE_LOST);
121   if (message == NULL)
122     {
123       BUS_SET_OOM (error);
124       return FALSE;
125     }
126   
127   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
128     {
129       dbus_message_unref (message);
130       BUS_SET_OOM (error);
131       return FALSE;
132     }
133   
134   if (!dbus_message_append_args (message,
135                                  DBUS_TYPE_STRING, service_name,
136                                  0))
137     {
138       dbus_message_unref (message);
139       BUS_SET_OOM (error);
140       return FALSE;
141     }
142
143   if (!bus_transaction_send_message (transaction, connection, message))
144     {
145       dbus_message_unref (message);
146       BUS_SET_OOM (error);
147       return FALSE;
148     }
149   else
150     {
151       dbus_message_unref (message);
152       return TRUE;
153     }
154 }
155
156 dbus_bool_t
157 bus_driver_send_service_acquired (DBusConnection *connection,
158                                   const char     *service_name,
159                                   BusTransaction *transaction,
160                                   DBusError      *error)
161 {
162   DBusMessage *message;
163
164   message = dbus_message_new (bus_connection_get_name (connection),
165                               DBUS_MESSAGE_SERVICE_ACQUIRED);
166   if (message == NULL)
167     {
168       BUS_SET_OOM (error);
169       return FALSE;
170     }
171   
172   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
173     {
174       dbus_message_unref (message);
175       BUS_SET_OOM (error);
176       return FALSE;
177     }
178   
179   if (!dbus_message_append_args (message,
180                                  DBUS_TYPE_STRING, service_name,
181                                  0))
182     {
183       dbus_message_unref (message);
184       BUS_SET_OOM (error);
185       return FALSE;
186     }
187
188   if (!bus_transaction_send_message (transaction, connection, message))
189     {
190       dbus_message_unref (message);
191       BUS_SET_OOM (error);
192       return FALSE;
193     }
194   else
195     {
196       dbus_message_unref (message);
197       return TRUE;
198     }
199 }
200
201 static dbus_bool_t
202 create_unique_client_name (BusRegistry *registry,
203                            DBusString  *str)
204 {
205   /* We never want to use the same unique client name twice, because
206    * we want to guarantee that if you send a message to a given unique
207    * name, you always get the same application. So we use two numbers
208    * for INT_MAX * INT_MAX combinations, should be pretty safe against
209    * wraparound.
210    */
211   static int next_major_number = 0;
212   static int next_minor_number = 0;
213   int len;
214
215   len = _dbus_string_get_length (str);
216   
217   while (TRUE)
218     {
219       /* start out with 1-0, go to 1-1, 1-2, 1-3,
220        * up to 1-MAXINT, then 2-0, 2-1, etc.
221        */
222       if (next_minor_number <= 0)
223         {
224           next_major_number += 1;
225           next_minor_number = 0;
226           if (next_major_number <= 0)
227             _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
228         }
229
230       _dbus_assert (next_major_number > 0);
231       _dbus_assert (next_minor_number >= 0);
232
233       /* appname:MAJOR-MINOR */
234       
235       if (!_dbus_string_append (str, ":"))
236         return FALSE;
237       
238       if (!_dbus_string_append_int (str, next_major_number))
239         return FALSE;
240
241       if (!_dbus_string_append (str, "-"))
242         return FALSE;
243       
244       if (!_dbus_string_append_int (str, next_minor_number))
245         return FALSE;
246
247       next_minor_number += 1;
248       
249       /* Check if a client with the name exists */
250       if (bus_registry_lookup (registry, str) == NULL)
251         break;
252
253       /* drop the number again, try the next one. */
254       _dbus_string_set_length (str, len);
255     }
256
257   return TRUE;
258 }
259
260 static dbus_bool_t
261 bus_driver_handle_hello (DBusConnection *connection,
262                          BusTransaction *transaction,
263                          DBusMessage    *message,
264                          DBusError      *error)
265 {
266   DBusString unique_name;
267   BusService *service;
268   dbus_bool_t retval;
269   BusRegistry *registry;
270   
271   if (!_dbus_string_init (&unique_name, _DBUS_INT_MAX))
272     {
273       BUS_SET_OOM (error);
274       return FALSE;
275     }
276
277   retval = FALSE;
278
279   registry = bus_connection_get_registry (connection);
280   
281   if (!create_unique_client_name (registry, &unique_name))
282     {
283       BUS_SET_OOM (error);
284       goto out_0;
285     }
286
287   if (!bus_connection_set_name (connection, &unique_name))
288     {
289       BUS_SET_OOM (error);
290       goto out_0;
291     }
292   
293   if (!dbus_message_set_sender (message,
294                                 bus_connection_get_name (connection)))
295     {
296       BUS_SET_OOM (error);
297       goto out_0;
298     }
299   
300   if (!bus_driver_send_welcome_message (connection, message, transaction, error))
301     goto out_0;
302
303   /* Create the service */
304   service = bus_registry_ensure (registry,
305                                  &unique_name, connection, transaction, error);
306   if (service == NULL)
307     goto out_0;
308   
309   bus_service_set_prohibit_replacement (service, TRUE);
310
311   retval = TRUE;
312   
313  out_0:
314   _dbus_string_free (&unique_name);
315   return retval;
316 }
317
318 static dbus_bool_t
319 bus_driver_send_welcome_message (DBusConnection *connection,
320                                  DBusMessage    *hello_message,
321                                  BusTransaction *transaction,
322                                  DBusError      *error)
323 {
324   DBusMessage *welcome;
325   const char *name;
326   
327   name = bus_connection_get_name (connection);
328   _dbus_assert (name != NULL);
329   
330   welcome = dbus_message_new_reply (hello_message);
331   if (welcome == NULL)
332     {
333       BUS_SET_OOM (error);
334       return FALSE;
335     }
336   
337   if (!dbus_message_set_sender (welcome, DBUS_SERVICE_DBUS))
338     {
339       dbus_message_unref (welcome);
340       BUS_SET_OOM (error);
341       return FALSE;
342     }
343   
344   if (!dbus_message_append_args (welcome,
345                                  DBUS_TYPE_STRING, name,
346                                  NULL))
347     {
348       dbus_message_unref (welcome);
349       BUS_SET_OOM (error);
350       return FALSE;
351     }
352
353   if (!bus_transaction_send_message (transaction, connection, welcome))
354     {
355       dbus_message_unref (welcome);
356       BUS_SET_OOM (error);
357       return FALSE;
358     }
359   else
360     {
361       dbus_message_unref (welcome);
362       return TRUE;
363     }
364 }
365
366 static dbus_bool_t
367 bus_driver_handle_list_services (DBusConnection *connection,
368                                  BusTransaction *transaction,
369                                  DBusMessage    *message,
370                                  DBusError      *error)
371 {
372   DBusMessage *reply;
373   int len;
374   char **services;
375   BusRegistry *registry;
376   
377   registry = bus_connection_get_registry (connection);
378   
379   reply = dbus_message_new_reply (message);
380   if (reply == NULL)
381     {
382       BUS_SET_OOM (error);
383       return FALSE;
384     }
385
386   if (!bus_registry_list_services (registry, &services, &len))
387     {
388       dbus_message_unref (reply);
389       BUS_SET_OOM (error);
390       return FALSE;
391     }
392   
393   if (!dbus_message_append_args (reply,
394                                  DBUS_TYPE_STRING_ARRAY, services, len,
395                                  0))
396     {
397       dbus_free_string_array (services);
398       dbus_message_unref (reply);
399       BUS_SET_OOM (error);
400       return FALSE;
401     }
402
403   dbus_free_string_array (services);
404   
405   if (!bus_transaction_send_message (transaction, connection, reply))
406     {
407       dbus_message_unref (reply);
408       BUS_SET_OOM (error);
409       return FALSE;
410     }
411   else
412     {
413       dbus_message_unref (reply);
414       return TRUE;
415     }
416 }
417
418 static dbus_bool_t
419 bus_driver_handle_acquire_service (DBusConnection *connection,
420                                    BusTransaction *transaction,
421                                    DBusMessage    *message,
422                                    DBusError      *error)
423 {
424   DBusMessage *reply;
425   DBusString service_name;
426   BusService *service;  
427   char *name;
428   int service_reply;
429   int flags;
430   dbus_bool_t retval;
431   DBusConnection *old_owner;
432   DBusConnection *current_owner;
433   BusRegistry *registry;
434   
435   registry = bus_connection_get_registry (connection);
436   
437   if (!dbus_message_get_args (message,
438                               error,
439                               DBUS_TYPE_STRING, &name,
440                               DBUS_TYPE_UINT32, &flags,
441                               0))
442     return FALSE;
443   
444   _dbus_verbose ("Trying to own service %s with flags 0x%x\n", name, flags);
445
446   retval = FALSE;
447   reply = NULL;
448
449   _dbus_string_init_const (&service_name, name);
450   
451   service = bus_registry_lookup (registry, &service_name);
452
453   if (service != NULL)
454     old_owner = bus_service_get_primary_owner (service);
455   else
456     old_owner = NULL;  
457   
458   reply = dbus_message_new_reply (message);
459   if (reply == NULL)
460     {
461       BUS_SET_OOM (error);
462       goto out;
463     }
464
465   if (service == NULL)
466     {
467       service = bus_registry_ensure (registry,
468                                      &service_name, connection, transaction, error);
469       if (service == NULL)
470         goto out;
471     }
472
473   current_owner = bus_service_get_primary_owner (service);
474
475   if (old_owner == NULL)
476     {
477       _dbus_assert (current_owner == connection);
478
479       bus_service_set_prohibit_replacement (service,
480                                             (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT));      
481                         
482       service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER;      
483     }
484   else if (old_owner == connection)
485     service_reply = DBUS_SERVICE_REPLY_ALREADY_OWNER;
486   else if (!((flags & DBUS_SERVICE_FLAG_REPLACE_EXISTING)))
487     service_reply = DBUS_SERVICE_REPLY_SERVICE_EXISTS;
488   else if (bus_service_get_prohibit_replacement (service))
489     {
490       /* Queue the connection */
491       if (!bus_service_add_owner (service, connection,
492                                   transaction, error))
493         goto out;
494       
495       service_reply = DBUS_SERVICE_REPLY_IN_QUEUE;
496     }
497   else
498     {
499       /* Replace the current owner */
500
501       /* We enqueue the new owner and remove the first one because
502        * that will cause ServiceAcquired and ServiceLost messages to
503        * be sent.
504        */
505       
506       /* FIXME this is broken, if the remove_owner fails
507        * we don't undo the add_owner
508        * (easiest fix is probably to move all this to
509        * services.c and have a single routine for it)
510        */
511       
512       if (!bus_service_add_owner (service, connection,
513                                   transaction, error))
514         goto out;
515       
516       if (!bus_service_remove_owner (service, old_owner,
517                                      transaction, error))
518         goto out;
519       
520       _dbus_assert (connection == bus_service_get_primary_owner (service));
521       service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
522     }
523
524   if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, service_reply, 0))
525     {
526       BUS_SET_OOM (error);
527       goto out;
528     }
529
530   if (!bus_transaction_send_message (transaction, connection, reply))
531     {
532       BUS_SET_OOM (error);
533       goto out;
534     }
535
536   retval = TRUE;
537   
538  out:
539   dbus_free (name);
540   if (reply)
541     dbus_message_unref (reply);
542   return retval;
543
544
545 static dbus_bool_t
546 bus_driver_handle_service_exists (DBusConnection *connection,
547                                   BusTransaction *transaction,
548                                   DBusMessage    *message,
549                                   DBusError      *error)
550 {
551   DBusMessage *reply;
552   DBusString service_name;
553   BusService *service;
554   char *name;
555   dbus_bool_t retval;
556   BusRegistry *registry;
557   
558   registry = bus_connection_get_registry (connection);
559   
560   if (!dbus_message_get_args (message, error,
561                               DBUS_TYPE_STRING, &name,
562                               0))
563     return FALSE;
564
565   retval = FALSE;
566   
567   _dbus_string_init_const (&service_name, name);
568   service = bus_registry_lookup (registry, &service_name);
569  
570   reply = dbus_message_new_reply (message);
571   if (reply == NULL)
572     {
573       BUS_SET_OOM (error);
574       goto out;
575     }
576   
577   if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
578     {
579       BUS_SET_OOM (error);
580       goto out;
581     }
582
583   if (!dbus_message_append_args (reply,
584                                  DBUS_TYPE_UINT32, service != NULL,
585                                  0))
586     {
587       BUS_SET_OOM (error);
588       goto out;
589     }
590
591   if (!bus_transaction_send_message (transaction, connection, reply))
592     {
593       BUS_SET_OOM (error);
594       goto out;
595     }
596
597   retval = TRUE;
598   
599  out:
600   if (reply)
601     dbus_message_unref (reply);
602   dbus_free (name);
603
604   return retval;
605 }
606
607 static dbus_bool_t
608 bus_driver_handle_activate_service (DBusConnection *connection,
609                                     BusTransaction *transaction,
610                                     DBusMessage    *message,
611                                     DBusError      *error)
612 {
613   dbus_uint32_t flags;
614   char *name;
615   dbus_bool_t retval;
616   BusActivation *activation;
617
618   activation = bus_connection_get_activation (connection);
619   
620   if (!dbus_message_get_args (message, error,
621                               DBUS_TYPE_STRING, &name,
622                               DBUS_TYPE_UINT32, &flags,
623                               0))
624     return FALSE;
625
626   retval = FALSE;
627
628   if (!bus_activation_activate_service (activation, name, error))
629     goto out;
630
631   retval = TRUE;
632   
633  out:
634   dbus_free (name);
635   return retval;
636 }
637
638 /* For speed it might be useful to sort this in order of
639  * frequency of use (but doesn't matter with only a few items
640  * anyhow)
641  */
642 struct
643 {
644   const char *name;
645   dbus_bool_t (* handler) (DBusConnection *connection,
646                            BusTransaction *transaction,
647                            DBusMessage    *message,
648                            DBusError      *error);
649 } message_handlers[] = {
650   { DBUS_MESSAGE_ACQUIRE_SERVICE, bus_driver_handle_acquire_service },
651   { DBUS_MESSAGE_ACTIVATE_SERVICE, bus_driver_handle_activate_service },
652   { DBUS_MESSAGE_HELLO, bus_driver_handle_hello },
653   { DBUS_MESSAGE_SERVICE_EXISTS, bus_driver_handle_service_exists },
654   { DBUS_MESSAGE_LIST_SERVICES, bus_driver_handle_list_services }
655 };
656
657 dbus_bool_t
658 bus_driver_handle_message (DBusConnection *connection,
659                            BusTransaction *transaction,
660                            DBusMessage    *message,
661                            DBusError      *error)
662 {
663   const char *name, *sender;
664   int i;
665   
666   _dbus_verbose ("Driver got a message: %s\n",
667                  dbus_message_get_name (message));
668   
669   name = dbus_message_get_name (message);
670   sender = dbus_message_get_sender (message);
671
672   if (sender == NULL && (strcmp (name, DBUS_MESSAGE_HELLO) != 0))
673     {
674       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
675                       "Client tried to send a message other than %s without being registered",
676                       DBUS_MESSAGE_HELLO);
677
678       dbus_connection_disconnect (connection);
679       return FALSE;
680     }
681
682   i = 0;
683   while (i < _DBUS_N_ELEMENTS (message_handlers))
684     {
685       if (strcmp (message_handlers[i].name, name) == 0)
686         {
687           if ((* message_handlers[i].handler) (connection, transaction, message, error))
688             return TRUE;
689           else
690             return FALSE;
691         }
692       
693       ++i;
694     }
695
696   dbus_set_error (error, DBUS_ERROR_UNKNOWN_MESSAGE,
697                   "%s does not understand message %s",
698                   DBUS_SERVICE_DBUS, name);
699   
700   return FALSE;
701 }
702
703 void
704 bus_driver_remove_connection (DBusConnection *connection)
705 {
706   /* FIXME Does nothing for now, should unregister the connection
707    * with the bus driver.
708    */
709 }