1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* driver.c Bus client (driver)
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
24 #include "activation.h"
25 #include "connection.h"
30 #include <dbus/dbus-string.h>
31 #include <dbus/dbus-internals.h>
34 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
35 DBusMessage *hello_message,
36 BusTransaction *transaction,
40 bus_driver_send_service_deleted (const char *service_name,
41 BusTransaction *transaction,
47 _dbus_verbose ("sending service deleted: %s\n", service_name);
49 message = dbus_message_new (DBUS_SERVICE_BROADCAST,
50 DBUS_MESSAGE_SERVICE_DELETED);
57 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
58 !dbus_message_append_args (message,
59 DBUS_TYPE_STRING, service_name,
62 dbus_message_unref (message);
67 retval = bus_dispatch_broadcast_message (transaction, message, error);
68 dbus_message_unref (message);
74 bus_driver_send_service_created (const char *service_name,
75 BusTransaction *transaction,
81 message = dbus_message_new (DBUS_SERVICE_BROADCAST,
82 DBUS_MESSAGE_SERVICE_CREATED);
89 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
91 dbus_message_unref (message);
96 if (!dbus_message_append_args (message,
97 DBUS_TYPE_STRING, service_name,
100 dbus_message_unref (message);
105 retval = bus_dispatch_broadcast_message (transaction, message, error);
106 dbus_message_unref (message);
112 bus_driver_send_service_lost (DBusConnection *connection,
113 const char *service_name,
114 BusTransaction *transaction,
117 DBusMessage *message;
119 message = dbus_message_new (bus_connection_get_name (connection),
120 DBUS_MESSAGE_SERVICE_LOST);
127 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
129 dbus_message_unref (message);
134 if (!dbus_message_append_args (message,
135 DBUS_TYPE_STRING, service_name,
138 dbus_message_unref (message);
143 if (!bus_transaction_send_message (transaction, connection, message))
145 dbus_message_unref (message);
151 dbus_message_unref (message);
157 bus_driver_send_service_acquired (DBusConnection *connection,
158 const char *service_name,
159 BusTransaction *transaction,
162 DBusMessage *message;
164 message = dbus_message_new (bus_connection_get_name (connection),
165 DBUS_MESSAGE_SERVICE_ACQUIRED);
172 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
174 dbus_message_unref (message);
179 if (!dbus_message_append_args (message,
180 DBUS_TYPE_STRING, service_name,
183 dbus_message_unref (message);
188 if (!bus_transaction_send_message (transaction, connection, message))
190 dbus_message_unref (message);
196 dbus_message_unref (message);
202 create_unique_client_name (BusRegistry *registry,
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
211 static int next_major_number = 0;
212 static int next_minor_number = 0;
215 len = _dbus_string_get_length (str);
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.
222 if (next_minor_number <= 0)
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");
230 _dbus_assert (next_major_number > 0);
231 _dbus_assert (next_minor_number >= 0);
233 /* appname:MAJOR-MINOR */
235 if (!_dbus_string_append (str, ":"))
238 if (!_dbus_string_append_int (str, next_major_number))
241 if (!_dbus_string_append (str, "-"))
244 if (!_dbus_string_append_int (str, next_minor_number))
247 next_minor_number += 1;
249 /* Check if a client with the name exists */
250 if (bus_registry_lookup (registry, str) == NULL)
253 /* drop the number again, try the next one. */
254 _dbus_string_set_length (str, len);
261 bus_driver_handle_hello (DBusConnection *connection,
262 BusTransaction *transaction,
263 DBusMessage *message,
266 DBusString unique_name;
269 BusRegistry *registry;
271 if (!_dbus_string_init (&unique_name, _DBUS_INT_MAX))
279 registry = bus_connection_get_registry (connection);
281 if (!create_unique_client_name (registry, &unique_name))
287 if (!bus_connection_set_name (connection, &unique_name))
293 if (!dbus_message_set_sender (message,
294 bus_connection_get_name (connection)))
300 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
303 /* Create the service */
304 service = bus_registry_ensure (registry,
305 &unique_name, connection, transaction, error);
309 bus_service_set_prohibit_replacement (service, TRUE);
314 _dbus_string_free (&unique_name);
319 bus_driver_send_welcome_message (DBusConnection *connection,
320 DBusMessage *hello_message,
321 BusTransaction *transaction,
324 DBusMessage *welcome;
327 name = bus_connection_get_name (connection);
328 _dbus_assert (name != NULL);
330 welcome = dbus_message_new_reply (hello_message);
337 if (!dbus_message_set_sender (welcome, DBUS_SERVICE_DBUS))
339 dbus_message_unref (welcome);
344 if (!dbus_message_append_args (welcome,
345 DBUS_TYPE_STRING, name,
348 dbus_message_unref (welcome);
353 if (!bus_transaction_send_message (transaction, connection, welcome))
355 dbus_message_unref (welcome);
361 dbus_message_unref (welcome);
367 bus_driver_handle_list_services (DBusConnection *connection,
368 BusTransaction *transaction,
369 DBusMessage *message,
375 BusRegistry *registry;
377 registry = bus_connection_get_registry (connection);
379 reply = dbus_message_new_reply (message);
386 if (!bus_registry_list_services (registry, &services, &len))
388 dbus_message_unref (reply);
393 if (!dbus_message_append_args (reply,
394 DBUS_TYPE_STRING_ARRAY, services, len,
397 dbus_free_string_array (services);
398 dbus_message_unref (reply);
403 dbus_free_string_array (services);
405 if (!bus_transaction_send_message (transaction, connection, reply))
407 dbus_message_unref (reply);
413 dbus_message_unref (reply);
419 bus_driver_handle_acquire_service (DBusConnection *connection,
420 BusTransaction *transaction,
421 DBusMessage *message,
425 DBusString service_name;
431 DBusConnection *old_owner;
432 DBusConnection *current_owner;
433 BusRegistry *registry;
435 registry = bus_connection_get_registry (connection);
437 if (!dbus_message_get_args (message,
439 DBUS_TYPE_STRING, &name,
440 DBUS_TYPE_UINT32, &flags,
444 _dbus_verbose ("Trying to own service %s with flags 0x%x\n", name, flags);
449 _dbus_string_init_const (&service_name, name);
451 service = bus_registry_lookup (registry, &service_name);
454 old_owner = bus_service_get_primary_owner (service);
458 reply = dbus_message_new_reply (message);
467 service = bus_registry_ensure (registry,
468 &service_name, connection, transaction, error);
473 current_owner = bus_service_get_primary_owner (service);
475 if (old_owner == NULL)
477 _dbus_assert (current_owner == connection);
479 bus_service_set_prohibit_replacement (service,
480 (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT));
482 service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
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))
490 /* Queue the connection */
491 if (!bus_service_add_owner (service, connection,
495 service_reply = DBUS_SERVICE_REPLY_IN_QUEUE;
499 /* Replace the current owner */
501 /* We enqueue the new owner and remove the first one because
502 * that will cause ServiceAcquired and ServiceLost messages to
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)
512 if (!bus_service_add_owner (service, connection,
516 if (!bus_service_remove_owner (service, old_owner,
520 _dbus_assert (connection == bus_service_get_primary_owner (service));
521 service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
524 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, service_reply, 0))
530 if (!bus_transaction_send_message (transaction, connection, reply))
541 dbus_message_unref (reply);
546 bus_driver_handle_service_exists (DBusConnection *connection,
547 BusTransaction *transaction,
548 DBusMessage *message,
552 DBusString service_name;
556 BusRegistry *registry;
558 registry = bus_connection_get_registry (connection);
560 if (!dbus_message_get_args (message, error,
561 DBUS_TYPE_STRING, &name,
567 _dbus_string_init_const (&service_name, name);
568 service = bus_registry_lookup (registry, &service_name);
570 reply = dbus_message_new_reply (message);
577 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
583 if (!dbus_message_append_args (reply,
584 DBUS_TYPE_UINT32, service != NULL,
591 if (!bus_transaction_send_message (transaction, connection, reply))
601 dbus_message_unref (reply);
608 bus_driver_handle_activate_service (DBusConnection *connection,
609 BusTransaction *transaction,
610 DBusMessage *message,
616 BusActivation *activation;
618 activation = bus_connection_get_activation (connection);
620 if (!dbus_message_get_args (message, error,
621 DBUS_TYPE_STRING, &name,
622 DBUS_TYPE_UINT32, &flags,
628 if (!bus_activation_activate_service (activation, connection, message, name, error))
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
645 dbus_bool_t (* handler) (DBusConnection *connection,
646 BusTransaction *transaction,
647 DBusMessage *message,
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 }
658 bus_driver_handle_message (DBusConnection *connection,
659 BusTransaction *transaction,
660 DBusMessage *message,
663 const char *name, *sender;
666 _dbus_verbose ("Driver got a message: %s\n",
667 dbus_message_get_name (message));
669 name = dbus_message_get_name (message);
670 sender = dbus_message_get_sender (message);
672 if (sender == NULL && (strcmp (name, DBUS_MESSAGE_HELLO) != 0))
674 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
675 "Client tried to send a message other than %s without being registered",
678 dbus_connection_disconnect (connection);
683 while (i < _DBUS_N_ELEMENTS (message_handlers))
685 if (strcmp (message_handlers[i].name, name) == 0)
687 if ((* message_handlers[i].handler) (connection, transaction, message, error))
696 dbus_set_error (error, DBUS_ERROR_UNKNOWN_MESSAGE,
697 "%s does not understand message %s",
698 DBUS_SERVICE_DBUS, name);
704 bus_driver_remove_connection (DBusConnection *connection)
706 /* FIXME Does nothing for now, should unregister the connection
707 * with the bus driver.