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_ASSERT_ERROR_IS_CLEAR (error);
49 _dbus_verbose ("sending service deleted: %s\n", service_name);
51 message = dbus_message_new (DBUS_SERVICE_BROADCAST,
52 DBUS_MESSAGE_SERVICE_DELETED);
59 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
60 !dbus_message_append_args (message,
61 DBUS_TYPE_STRING, service_name,
64 dbus_message_unref (message);
69 retval = bus_dispatch_broadcast_message (transaction, message, error);
70 dbus_message_unref (message);
76 bus_driver_send_service_created (const char *service_name,
77 BusTransaction *transaction,
83 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
85 message = dbus_message_new (DBUS_SERVICE_BROADCAST,
86 DBUS_MESSAGE_SERVICE_CREATED);
93 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
95 dbus_message_unref (message);
100 if (!dbus_message_append_args (message,
101 DBUS_TYPE_STRING, service_name,
104 dbus_message_unref (message);
109 retval = bus_dispatch_broadcast_message (transaction, message, error);
110 dbus_message_unref (message);
116 bus_driver_send_service_lost (DBusConnection *connection,
117 const char *service_name,
118 BusTransaction *transaction,
121 DBusMessage *message;
123 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
125 message = dbus_message_new (bus_connection_get_name (connection),
126 DBUS_MESSAGE_SERVICE_LOST);
133 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
135 dbus_message_unref (message);
140 if (!dbus_message_append_args (message,
141 DBUS_TYPE_STRING, service_name,
144 dbus_message_unref (message);
149 if (!bus_transaction_send_message (transaction, connection, message))
151 dbus_message_unref (message);
157 dbus_message_unref (message);
163 bus_driver_send_service_acquired (DBusConnection *connection,
164 const char *service_name,
165 BusTransaction *transaction,
168 DBusMessage *message;
170 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
172 message = dbus_message_new (bus_connection_get_name (connection),
173 DBUS_MESSAGE_SERVICE_ACQUIRED);
180 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
182 dbus_message_unref (message);
187 if (!dbus_message_append_args (message,
188 DBUS_TYPE_STRING, service_name,
191 dbus_message_unref (message);
196 if (!bus_transaction_send_message (transaction, connection, message))
198 dbus_message_unref (message);
204 dbus_message_unref (message);
210 create_unique_client_name (BusRegistry *registry,
213 /* We never want to use the same unique client name twice, because
214 * we want to guarantee that if you send a message to a given unique
215 * name, you always get the same application. So we use two numbers
216 * for INT_MAX * INT_MAX combinations, should be pretty safe against
219 static int next_major_number = 0;
220 static int next_minor_number = 0;
223 len = _dbus_string_get_length (str);
227 /* start out with 1-0, go to 1-1, 1-2, 1-3,
228 * up to 1-MAXINT, then 2-0, 2-1, etc.
230 if (next_minor_number <= 0)
232 next_major_number += 1;
233 next_minor_number = 0;
234 if (next_major_number <= 0)
235 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
238 _dbus_assert (next_major_number > 0);
239 _dbus_assert (next_minor_number >= 0);
241 /* appname:MAJOR-MINOR */
243 if (!_dbus_string_append (str, ":"))
246 if (!_dbus_string_append_int (str, next_major_number))
249 if (!_dbus_string_append (str, "-"))
252 if (!_dbus_string_append_int (str, next_minor_number))
255 next_minor_number += 1;
257 /* Check if a client with the name exists */
258 if (bus_registry_lookup (registry, str) == NULL)
261 /* drop the number again, try the next one. */
262 _dbus_string_set_length (str, len);
269 bus_driver_handle_hello (DBusConnection *connection,
270 BusTransaction *transaction,
271 DBusMessage *message,
274 DBusString unique_name;
277 BusRegistry *registry;
279 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
281 if (!_dbus_string_init (&unique_name))
289 registry = bus_connection_get_registry (connection);
291 if (!create_unique_client_name (registry, &unique_name))
297 if (!bus_connection_set_name (connection, &unique_name))
303 if (!dbus_message_set_sender (message,
304 bus_connection_get_name (connection)))
310 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
313 /* Create the service */
314 service = bus_registry_ensure (registry,
315 &unique_name, connection, transaction, error);
319 bus_service_set_prohibit_replacement (service, TRUE);
324 _dbus_string_free (&unique_name);
329 bus_driver_send_welcome_message (DBusConnection *connection,
330 DBusMessage *hello_message,
331 BusTransaction *transaction,
334 DBusMessage *welcome;
337 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
339 name = bus_connection_get_name (connection);
340 _dbus_assert (name != NULL);
342 welcome = dbus_message_new_reply (hello_message);
349 if (!dbus_message_set_sender (welcome, DBUS_SERVICE_DBUS))
351 dbus_message_unref (welcome);
356 if (!dbus_message_append_args (welcome,
357 DBUS_TYPE_STRING, name,
360 dbus_message_unref (welcome);
365 if (!bus_transaction_send_message (transaction, connection, welcome))
367 dbus_message_unref (welcome);
373 dbus_message_unref (welcome);
379 bus_driver_handle_list_services (DBusConnection *connection,
380 BusTransaction *transaction,
381 DBusMessage *message,
387 BusRegistry *registry;
389 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
391 registry = bus_connection_get_registry (connection);
393 reply = dbus_message_new_reply (message);
400 if (!bus_registry_list_services (registry, &services, &len))
402 dbus_message_unref (reply);
407 if (!dbus_message_append_args (reply,
408 DBUS_TYPE_STRING_ARRAY, services, len,
411 dbus_free_string_array (services);
412 dbus_message_unref (reply);
417 dbus_free_string_array (services);
419 if (!bus_transaction_send_message (transaction, connection, reply))
421 dbus_message_unref (reply);
427 dbus_message_unref (reply);
433 bus_driver_handle_acquire_service (DBusConnection *connection,
434 BusTransaction *transaction,
435 DBusMessage *message,
439 DBusString service_name;
445 DBusConnection *old_owner;
446 DBusConnection *current_owner;
447 BusRegistry *registry;
449 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
451 registry = bus_connection_get_registry (connection);
453 if (!dbus_message_get_args (message,
455 DBUS_TYPE_STRING, &name,
456 DBUS_TYPE_UINT32, &flags,
460 _dbus_verbose ("Trying to own service %s with flags 0x%x\n", name, flags);
465 _dbus_string_init_const (&service_name, name);
467 service = bus_registry_lookup (registry, &service_name);
470 old_owner = bus_service_get_primary_owner (service);
474 reply = dbus_message_new_reply (message);
481 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
489 service = bus_registry_ensure (registry,
490 &service_name, connection, transaction, error);
495 current_owner = bus_service_get_primary_owner (service);
497 if (old_owner == NULL)
499 _dbus_assert (current_owner == connection);
501 bus_service_set_prohibit_replacement (service,
502 (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT));
504 service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
506 else if (old_owner == connection)
507 service_reply = DBUS_SERVICE_REPLY_ALREADY_OWNER;
508 else if (!((flags & DBUS_SERVICE_FLAG_REPLACE_EXISTING)))
509 service_reply = DBUS_SERVICE_REPLY_SERVICE_EXISTS;
510 else if (bus_service_get_prohibit_replacement (service))
512 /* Queue the connection */
513 if (!bus_service_add_owner (service, connection,
517 service_reply = DBUS_SERVICE_REPLY_IN_QUEUE;
521 /* Replace the current owner */
523 /* We enqueue the new owner and remove the first one because
524 * that will cause ServiceAcquired and ServiceLost messages to
528 /* FIXME this is broken, if the remove_owner fails
529 * we don't undo the add_owner
530 * (easiest fix is probably to move all this to
531 * services.c and have a single routine for it)
534 if (!bus_service_add_owner (service, connection,
538 if (!bus_service_remove_owner (service, old_owner,
542 _dbus_assert (connection == bus_service_get_primary_owner (service));
543 service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
546 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, service_reply, 0))
552 if (!bus_transaction_send_message (transaction, connection, reply))
563 dbus_message_unref (reply);
568 bus_driver_handle_service_exists (DBusConnection *connection,
569 BusTransaction *transaction,
570 DBusMessage *message,
574 DBusString service_name;
578 BusRegistry *registry;
580 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
582 registry = bus_connection_get_registry (connection);
584 if (!dbus_message_get_args (message, error,
585 DBUS_TYPE_STRING, &name,
591 _dbus_string_init_const (&service_name, name);
592 service = bus_registry_lookup (registry, &service_name);
594 reply = dbus_message_new_reply (message);
601 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
607 if (!dbus_message_append_args (reply,
608 DBUS_TYPE_UINT32, service != NULL,
615 if (!bus_transaction_send_message (transaction, connection, reply))
625 dbus_message_unref (reply);
632 bus_driver_handle_activate_service (DBusConnection *connection,
633 BusTransaction *transaction,
634 DBusMessage *message,
640 BusActivation *activation;
642 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
644 activation = bus_connection_get_activation (connection);
646 if (!dbus_message_get_args (message, error,
647 DBUS_TYPE_STRING, &name,
648 DBUS_TYPE_UINT32, &flags,
654 if (!bus_activation_activate_service (activation, connection, transaction,
655 message, name, error))
665 /* For speed it might be useful to sort this in order of
666 * frequency of use (but doesn't matter with only a few items
672 dbus_bool_t (* handler) (DBusConnection *connection,
673 BusTransaction *transaction,
674 DBusMessage *message,
676 } message_handlers[] = {
677 { DBUS_MESSAGE_ACQUIRE_SERVICE, bus_driver_handle_acquire_service },
678 { DBUS_MESSAGE_ACTIVATE_SERVICE, bus_driver_handle_activate_service },
679 { DBUS_MESSAGE_HELLO, bus_driver_handle_hello },
680 { DBUS_MESSAGE_SERVICE_EXISTS, bus_driver_handle_service_exists },
681 { DBUS_MESSAGE_LIST_SERVICES, bus_driver_handle_list_services }
685 bus_driver_handle_message (DBusConnection *connection,
686 BusTransaction *transaction,
687 DBusMessage *message,
690 const char *name, *sender;
693 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
695 _dbus_verbose ("Driver got a message: %s\n",
696 dbus_message_get_name (message));
698 name = dbus_message_get_name (message);
699 sender = dbus_message_get_sender (message);
701 if (sender == NULL && (strcmp (name, DBUS_MESSAGE_HELLO) != 0))
703 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
704 "Client tried to send a message other than %s without being registered",
707 dbus_connection_disconnect (connection);
712 while (i < _DBUS_N_ELEMENTS (message_handlers))
714 if (strcmp (message_handlers[i].name, name) == 0)
716 if ((* message_handlers[i].handler) (connection, transaction, message, error))
725 dbus_set_error (error, DBUS_ERROR_UNKNOWN_MESSAGE,
726 "%s does not understand message %s",
727 DBUS_SERVICE_DBUS, name);
733 bus_driver_remove_connection (DBusConnection *connection)
735 /* FIXME Does nothing for now, should unregister the connection
736 * with the bus driver.