2003-02-13 Anders Carlsson <andersca@codefactory.se>
[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 "connection.h"
25 #include "driver.h"
26 #include "dispatch.h"
27 #include "services.h"
28 #include <dbus/dbus-string.h>
29 #include <dbus/dbus-internals.h>
30 #include <string.h>
31
32 static void bus_driver_send_welcome_message (DBusConnection *connection,
33                                              DBusMessage    *hello_message);
34
35 void
36 bus_driver_send_service_deleted (const char *service_name)
37 {
38   DBusMessage *message;
39
40   _dbus_verbose ("sending service deleted: %s\n", service_name);
41   
42   _DBUS_HANDLE_OOM (message = dbus_message_new (DBUS_SERVICE_BROADCAST,
43                                                 DBUS_MESSAGE_SERVICE_DELETED));
44   
45   _DBUS_HANDLE_OOM (dbus_message_set_sender (message, DBUS_SERVICE_DBUS));
46
47   _DBUS_HANDLE_OOM (dbus_message_append_fields (message,
48                                                 DBUS_TYPE_STRING, service_name,
49                                                 0));
50   bus_dispatch_broadcast_message (message);
51   dbus_message_unref (message);  
52 }
53
54 static void
55 bus_driver_send_service_created (const char *service_name)
56 {
57   DBusMessage *message;
58
59   _DBUS_HANDLE_OOM (message = dbus_message_new (DBUS_SERVICE_BROADCAST,
60                                                 DBUS_MESSAGE_SERVICE_CREATED));
61   
62   _DBUS_HANDLE_OOM (dbus_message_set_sender (message, DBUS_SERVICE_DBUS));
63
64   _DBUS_HANDLE_OOM (dbus_message_append_fields (message,
65                                                 DBUS_TYPE_STRING, service_name,
66                                                 0));
67   bus_dispatch_broadcast_message (message);
68   dbus_message_unref (message);
69 }
70
71 void
72 bus_driver_send_service_lost (DBusConnection *connection,
73                               const char *service_name)
74 {
75   DBusMessage *message;
76
77   _DBUS_HANDLE_OOM (message = dbus_message_new (DBUS_SERVICE_BROADCAST,
78                                                 DBUS_MESSAGE_SERVICE_LOST));
79   
80   _DBUS_HANDLE_OOM (dbus_message_set_sender (message, DBUS_SERVICE_DBUS));
81   _DBUS_HANDLE_OOM (dbus_message_append_fields (message,
82                                                 DBUS_TYPE_STRING, service_name,
83                                                 0));
84   _DBUS_HANDLE_OOM (dbus_connection_send_message (connection, message, NULL, NULL));
85   
86   dbus_message_unref (message);
87 }
88
89 void
90 bus_driver_send_service_acquired (DBusConnection *connection,
91                                   const char *service_name)
92 {
93   DBusMessage *message;
94
95   _DBUS_HANDLE_OOM (message = dbus_message_new (DBUS_SERVICE_BROADCAST,
96                                                 DBUS_MESSAGE_SERVICE_ACQUIRED));
97   
98   _DBUS_HANDLE_OOM (dbus_message_set_sender (message, DBUS_SERVICE_DBUS));
99   _DBUS_HANDLE_OOM (dbus_message_append_fields (message,
100                                                 DBUS_TYPE_STRING, service_name,
101                                                 0));
102   _DBUS_HANDLE_OOM (dbus_connection_send_message (connection, message, NULL, NULL));
103   
104   dbus_message_unref (message);
105 }
106
107 static dbus_bool_t
108 create_unique_client_name (DBusString *str)
109 {
110   /* We never want to use the same unique client name twice, because
111    * we want to guarantee that if you send a message to a given unique
112    * name, you always get the same application. So we use two numbers
113    * for INT_MAX * INT_MAX combinations, should be pretty safe against
114    * wraparound.
115    */
116   static int next_major_number = 0;
117   static int next_minor_number = 0;
118   int len;
119
120   len = _dbus_string_get_length (str);
121   
122   while (TRUE)
123     {
124       /* start out with 1-0, go to 1-1, 1-2, 1-3,
125        * up to 1-MAXINT, then 2-0, 2-1, etc.
126        */
127       if (next_minor_number <= 0)
128         {
129           next_major_number += 1;
130           next_minor_number = 0;
131           if (next_major_number <= 0)
132             _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
133         }
134
135       _dbus_assert (next_major_number > 0);
136       _dbus_assert (next_minor_number >= 0);
137
138       /* appname:MAJOR-MINOR */
139       
140       if (!_dbus_string_append (str, ":"))
141         return FALSE;
142       
143       if (!_dbus_string_append_int (str, next_major_number))
144         return FALSE;
145
146       if (!_dbus_string_append (str, "-"))
147         return FALSE;
148       
149       if (!_dbus_string_append_int (str, next_minor_number))
150         return FALSE;
151
152       next_minor_number += 1;
153       
154       /* Check if a client with the name exists */
155       if (bus_service_lookup (str, FALSE) == NULL)
156         break;
157
158       /* drop the number again, try the next one. */
159       _dbus_string_set_length (str, len);
160     }
161
162   return TRUE;
163 }
164
165 static void
166 bus_driver_handle_hello (DBusConnection *connection,
167                          DBusMessage    *message)
168 {
169   DBusString unique_name;
170   BusService *service;
171   
172   _DBUS_HANDLE_OOM (_dbus_string_init (&unique_name, _DBUS_INT_MAX));
173   _DBUS_HANDLE_OOM (create_unique_client_name (&unique_name));
174   
175   /* Create the service */
176   _DBUS_HANDLE_OOM (service = bus_service_lookup (&unique_name, TRUE));
177   bus_service_set_prohibit_replacement (service, TRUE);
178   
179   /* Add the connection as the owner */
180   _DBUS_HANDLE_OOM (bus_service_add_owner (service, connection));
181   _DBUS_HANDLE_OOM (bus_connection_set_name (connection, &unique_name));
182
183   _DBUS_HANDLE_OOM (dbus_message_set_sender (message,
184                                              bus_connection_get_name (connection)));
185   
186   _dbus_string_free (&unique_name);
187
188   _DBUS_HANDLE_OOM (bus_driver_send_welcome_message (connection, message));
189
190   /* Broadcast a service created message */
191   bus_driver_send_service_created (bus_service_get_name (service));
192 }
193
194 static void
195 bus_driver_send_welcome_message (DBusConnection *connection,
196                                  DBusMessage    *hello_message)
197 {
198   DBusMessage *welcome;
199   const char *name;
200   
201   name = bus_connection_get_name (connection);
202   _dbus_assert (name != NULL);
203   
204   _DBUS_HANDLE_OOM (welcome = dbus_message_new_reply (hello_message));
205   
206   _DBUS_HANDLE_OOM (dbus_message_set_sender (welcome, DBUS_SERVICE_DBUS));
207   
208   _DBUS_HANDLE_OOM (dbus_message_append_fields (welcome,
209                                                 DBUS_TYPE_STRING, name,
210                                                 NULL));
211   
212   _DBUS_HANDLE_OOM (dbus_connection_send_message (connection, welcome, NULL, NULL));
213
214   dbus_message_unref (welcome);
215 }
216
217 static void
218 bus_driver_handle_list_services (DBusConnection *connection,
219                                  DBusMessage    *message)
220 {
221   DBusMessage *reply;
222   int len, i;
223   char **services;
224
225   _DBUS_HANDLE_OOM (reply = dbus_message_new_reply (message));
226
227   _DBUS_HANDLE_OOM (services = bus_services_list (&len));
228
229   _DBUS_HANDLE_OOM (dbus_message_append_fields (reply,
230                                                 DBUS_TYPE_STRING_ARRAY, services, len,
231                                                 0));
232
233   _DBUS_HANDLE_OOM (dbus_connection_send_message (connection, reply, NULL, NULL));
234
235   dbus_message_unref (reply);
236
237   if (services != NULL)
238     {
239       for (i = 0; i < len; i++)
240         dbus_free (services[i]);
241       dbus_free (services);
242     }
243 }
244
245 static void
246 bus_driver_handle_acquire_service (DBusConnection *connection,
247                                    DBusMessage    *message)
248 {
249   DBusMessage *reply;
250   DBusResultCode result;
251   DBusString service_name;
252   BusService *service;  
253   char *name;
254   int service_reply;
255   int flags;
256   
257   _DBUS_HANDLE_OOM ((result = dbus_message_get_fields (message,
258                                                        DBUS_TYPE_STRING, &name,
259                                                        DBUS_TYPE_UINT32, &flags,
260                                                        0)) != DBUS_RESULT_NO_MEMORY);
261   
262   if (result != DBUS_RESULT_SUCCESS)
263     {
264       dbus_free (name);
265       dbus_connection_disconnect (connection);
266       return;
267     }
268
269   _dbus_verbose ("Trying to own service %s with flags %d\n", name, flags);
270
271   _dbus_string_init_const (&service_name, name);
272   service = bus_service_lookup (&service_name, TRUE);
273
274   _DBUS_HANDLE_OOM ((reply = dbus_message_new_reply (message)));
275   
276   /*
277    * Check if the service already has an owner
278    */
279   if (bus_service_get_primary_owner (service) != NULL)
280     {
281       if (bus_service_has_owner (service, connection))
282         service_reply = DBUS_SERVICE_REPLY_ALREADY_OWNER;
283       else if (!(flags & DBUS_SERVICE_FLAG_REPLACE_EXISTING))
284         service_reply = DBUS_SERVICE_REPLY_SERVICE_EXISTS;
285       else
286         {
287           if (bus_service_get_prohibit_replacement (service))
288             {
289               
290               /* Queue the connection */
291               _DBUS_HANDLE_OOM (bus_service_add_owner (service, connection));
292               
293               service_reply = DBUS_SERVICE_REPLY_IN_QUEUE;
294             }
295           else
296             {
297               DBusConnection *owner;
298               
299               /* We can replace the primary owner */
300               owner = bus_service_get_primary_owner (service);
301
302               /* We enqueue the new owner and remove the first one because
303                * that will cause ServiceAcquired and ServiceLost messages to
304                * be sent.
305                */
306               _DBUS_HANDLE_OOM (bus_service_add_owner (service, connection));
307               bus_service_remove_owner (service, owner);
308               _dbus_assert (connection == bus_service_get_primary_owner (service));
309               service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
310             }
311         }
312     }
313   else
314     {
315       bus_service_set_prohibit_replacement (service,
316                                             (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT));
317       
318       /* Broadcast service created message */
319       bus_driver_send_service_created (bus_service_get_name (service));
320       
321       _DBUS_HANDLE_OOM (bus_service_add_owner (service, connection));
322                         
323       service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
324     }
325
326   _DBUS_HANDLE_OOM (dbus_message_append_fields (reply, DBUS_TYPE_UINT32, service_reply, 0));
327
328   /* Send service reply */
329   _DBUS_HANDLE_OOM (dbus_connection_send_message (connection, reply, NULL, NULL));
330   dbus_free (name);
331   dbus_message_unref (reply);
332 }
333
334 static void
335 bus_driver_handle_service_exists (DBusConnection *connection,
336                                   DBusMessage    *message)
337 {
338   DBusMessage *reply;
339   DBusResultCode result;
340   DBusString service_name;
341   BusService *service;
342   char *name;
343   
344   _DBUS_HANDLE_OOM ((result = dbus_message_get_fields (message,
345                                                        DBUS_TYPE_STRING, &name,
346                                                        0)) != DBUS_RESULT_NO_MEMORY);
347  if (result != DBUS_RESULT_SUCCESS)
348     {
349       dbus_free (name);
350       dbus_connection_disconnect (connection);
351       return;
352     }
353
354  _dbus_string_init_const (&service_name, name);
355  service = bus_service_lookup (&service_name, FALSE);
356  
357  _DBUS_HANDLE_OOM ((reply = dbus_message_new_reply (message)));
358  _DBUS_HANDLE_OOM (dbus_message_set_sender (reply, DBUS_SERVICE_DBUS));
359
360  _DBUS_HANDLE_OOM (dbus_message_append_fields (reply,
361                                                DBUS_TYPE_UINT32, service != NULL,
362                                                0));
363  _DBUS_HANDLE_OOM (dbus_connection_send_message (connection, reply, NULL, NULL));
364  dbus_message_unref (reply);
365  dbus_free (name);
366 }
367
368 void
369 bus_driver_handle_message (DBusConnection *connection,
370                            DBusMessage    *message)
371 {
372   const char *name, *sender;
373
374   _dbus_verbose ("Driver got a message: %s\n",
375                  dbus_message_get_name (message));
376   
377   name = dbus_message_get_name (message);
378   sender = dbus_message_get_sender (message);
379
380   if (sender == NULL && (strcmp (name, DBUS_MESSAGE_HELLO) != 0))
381     {
382       _dbus_verbose ("Trying to send a message without being registered. Disconnecting.\n");
383       dbus_connection_disconnect (connection);
384       return;
385     }
386
387   /* Now check names. */
388   if (strcmp (name, DBUS_MESSAGE_HELLO) == 0)
389     bus_driver_handle_hello (connection, message);
390   else if (strcmp (name, DBUS_MESSAGE_LIST_SERVICES) == 0)
391     bus_driver_handle_list_services (connection, message);
392   else if (strcmp (name, DBUS_MESSAGE_ACQUIRE_SERVICE) == 0)
393     bus_driver_handle_acquire_service (connection, message);
394   else if (strcmp (name, DBUS_MESSAGE_SERVICE_EXISTS) == 0)
395     bus_driver_handle_service_exists (connection, message);
396   
397 }
398
399 void
400 bus_driver_remove_connection (DBusConnection *connection)
401 {
402   /* Does nothing for now */
403 }