1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* driver.c Bus client (driver)
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "activation.h"
26 #include "connection.h"
31 #include <dbus/dbus-string.h>
32 #include <dbus/dbus-internals.h>
35 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
36 DBusMessage *hello_message,
37 BusTransaction *transaction,
41 bus_driver_send_service_deleted (const char *service_name,
42 BusTransaction *transaction,
48 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
50 _dbus_verbose ("sending service deleted: %s\n", service_name);
52 message = dbus_message_new (DBUS_SERVICE_BROADCAST,
53 DBUS_MESSAGE_SERVICE_DELETED);
60 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
61 !dbus_message_append_args (message,
62 DBUS_TYPE_STRING, service_name,
65 dbus_message_unref (message);
70 retval = bus_dispatch_broadcast_message (transaction, message, error);
71 dbus_message_unref (message);
77 bus_driver_send_service_created (const char *service_name,
78 BusTransaction *transaction,
84 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
86 message = dbus_message_new (DBUS_SERVICE_BROADCAST,
87 DBUS_MESSAGE_SERVICE_CREATED);
94 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
96 dbus_message_unref (message);
101 if (!dbus_message_append_args (message,
102 DBUS_TYPE_STRING, service_name,
105 dbus_message_unref (message);
110 retval = bus_dispatch_broadcast_message (transaction, message, error);
111 dbus_message_unref (message);
117 bus_driver_send_service_lost (DBusConnection *connection,
118 const char *service_name,
119 BusTransaction *transaction,
122 DBusMessage *message;
124 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
126 message = dbus_message_new (bus_connection_get_name (connection),
127 DBUS_MESSAGE_SERVICE_LOST);
134 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
136 dbus_message_unref (message);
141 if (!dbus_message_append_args (message,
142 DBUS_TYPE_STRING, service_name,
145 dbus_message_unref (message);
150 if (!bus_transaction_send_message (transaction, connection, message))
152 dbus_message_unref (message);
158 dbus_message_unref (message);
164 bus_driver_send_service_acquired (DBusConnection *connection,
165 const char *service_name,
166 BusTransaction *transaction,
169 DBusMessage *message;
171 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
173 message = dbus_message_new (bus_connection_get_name (connection),
174 DBUS_MESSAGE_SERVICE_ACQUIRED);
181 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
183 dbus_message_unref (message);
188 if (!dbus_message_append_args (message,
189 DBUS_TYPE_STRING, service_name,
192 dbus_message_unref (message);
197 if (!bus_transaction_send_message (transaction, connection, message))
199 dbus_message_unref (message);
205 dbus_message_unref (message);
211 create_unique_client_name (BusRegistry *registry,
214 /* We never want to use the same unique client name twice, because
215 * we want to guarantee that if you send a message to a given unique
216 * name, you always get the same application. So we use two numbers
217 * for INT_MAX * INT_MAX combinations, should be pretty safe against
220 static int next_major_number = 0;
221 static int next_minor_number = 0;
224 len = _dbus_string_get_length (str);
228 /* start out with 1-0, go to 1-1, 1-2, 1-3,
229 * up to 1-MAXINT, then 2-0, 2-1, etc.
231 if (next_minor_number <= 0)
233 next_major_number += 1;
234 next_minor_number = 0;
235 if (next_major_number <= 0)
236 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
239 _dbus_assert (next_major_number > 0);
240 _dbus_assert (next_minor_number >= 0);
242 /* appname:MAJOR-MINOR */
244 if (!_dbus_string_append (str, ":"))
247 if (!_dbus_string_append_int (str, next_major_number))
250 if (!_dbus_string_append (str, "-"))
253 if (!_dbus_string_append_int (str, next_minor_number))
256 next_minor_number += 1;
258 /* Check if a client with the name exists */
259 if (bus_registry_lookup (registry, str) == NULL)
262 /* drop the number again, try the next one. */
263 _dbus_string_set_length (str, len);
270 bus_driver_handle_hello (DBusConnection *connection,
271 BusTransaction *transaction,
272 DBusMessage *message,
275 DBusString unique_name;
278 BusRegistry *registry;
280 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
282 if (!_dbus_string_init (&unique_name))
290 registry = bus_connection_get_registry (connection);
292 if (!create_unique_client_name (registry, &unique_name))
298 if (!bus_connection_set_name (connection, &unique_name))
304 if (!dbus_message_set_sender (message,
305 bus_connection_get_name (connection)))
311 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
314 /* Create the service */
315 service = bus_registry_ensure (registry,
316 &unique_name, connection, transaction, error);
320 bus_service_set_prohibit_replacement (service, TRUE);
325 _dbus_string_free (&unique_name);
330 bus_driver_send_welcome_message (DBusConnection *connection,
331 DBusMessage *hello_message,
332 BusTransaction *transaction,
335 DBusMessage *welcome;
338 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
340 name = bus_connection_get_name (connection);
341 _dbus_assert (name != NULL);
343 welcome = dbus_message_new_reply (hello_message);
350 if (!dbus_message_set_sender (welcome, DBUS_SERVICE_DBUS))
352 dbus_message_unref (welcome);
357 if (!dbus_message_append_args (welcome,
358 DBUS_TYPE_STRING, name,
361 dbus_message_unref (welcome);
366 if (!bus_transaction_send_message (transaction, connection, welcome))
368 dbus_message_unref (welcome);
374 dbus_message_unref (welcome);
380 bus_driver_handle_list_services (DBusConnection *connection,
381 BusTransaction *transaction,
382 DBusMessage *message,
388 BusRegistry *registry;
390 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
392 registry = bus_connection_get_registry (connection);
394 reply = dbus_message_new_reply (message);
401 if (!bus_registry_list_services (registry, &services, &len))
403 dbus_message_unref (reply);
408 if (!dbus_message_append_args (reply,
409 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, services, len,
412 dbus_free_string_array (services);
413 dbus_message_unref (reply);
418 dbus_free_string_array (services);
420 if (!bus_transaction_send_message (transaction, connection, reply))
422 dbus_message_unref (reply);
428 dbus_message_unref (reply);
434 bus_driver_handle_acquire_service (DBusConnection *connection,
435 BusTransaction *transaction,
436 DBusMessage *message,
440 DBusString service_name;
445 BusRegistry *registry;
447 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
449 registry = bus_connection_get_registry (connection);
451 if (!dbus_message_get_args (message, error,
452 DBUS_TYPE_STRING, &name,
453 DBUS_TYPE_UINT32, &flags,
457 _dbus_verbose ("Trying to own service %s with flags 0x%x\n", name, flags);
462 _dbus_string_init_const (&service_name, name);
464 if (!bus_registry_acquire_service (registry, connection,
465 &service_name, flags,
466 &service_reply, transaction,
470 reply = dbus_message_new_reply (message);
477 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
483 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, service_reply, DBUS_TYPE_INVALID))
489 if (!bus_transaction_send_message (transaction, connection, reply))
500 dbus_message_unref (reply);
505 bus_driver_handle_service_exists (DBusConnection *connection,
506 BusTransaction *transaction,
507 DBusMessage *message,
511 DBusString service_name;
515 BusRegistry *registry;
517 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
519 registry = bus_connection_get_registry (connection);
521 if (!dbus_message_get_args (message, error,
522 DBUS_TYPE_STRING, &name,
528 _dbus_string_init_const (&service_name, name);
529 service = bus_registry_lookup (registry, &service_name);
531 reply = dbus_message_new_reply (message);
538 if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS))
544 if (!dbus_message_append_args (reply,
545 DBUS_TYPE_UINT32, service != NULL,
552 if (!bus_transaction_send_message (transaction, connection, reply))
562 dbus_message_unref (reply);
569 bus_driver_handle_activate_service (DBusConnection *connection,
570 BusTransaction *transaction,
571 DBusMessage *message,
577 BusActivation *activation;
579 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
581 activation = bus_connection_get_activation (connection);
583 if (!dbus_message_get_args (message, error,
584 DBUS_TYPE_STRING, &name,
585 DBUS_TYPE_UINT32, &flags,
591 if (!bus_activation_activate_service (activation, connection, transaction,
592 message, name, error))
602 /* For speed it might be useful to sort this in order of
603 * frequency of use (but doesn't matter with only a few items
609 dbus_bool_t (* handler) (DBusConnection *connection,
610 BusTransaction *transaction,
611 DBusMessage *message,
613 } message_handlers[] = {
614 { DBUS_MESSAGE_ACQUIRE_SERVICE, bus_driver_handle_acquire_service },
615 { DBUS_MESSAGE_ACTIVATE_SERVICE, bus_driver_handle_activate_service },
616 { DBUS_MESSAGE_HELLO, bus_driver_handle_hello },
617 { DBUS_MESSAGE_SERVICE_EXISTS, bus_driver_handle_service_exists },
618 { DBUS_MESSAGE_LIST_SERVICES, bus_driver_handle_list_services }
622 bus_driver_handle_message (DBusConnection *connection,
623 BusTransaction *transaction,
624 DBusMessage *message,
627 const char *name, *sender;
630 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
632 _dbus_verbose ("Driver got a message: %s\n",
633 dbus_message_get_name (message));
635 name = dbus_message_get_name (message);
636 sender = dbus_message_get_sender (message);
638 if (sender == NULL && (strcmp (name, DBUS_MESSAGE_HELLO) != 0))
640 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
641 "Client tried to send a message other than %s without being registered",
644 dbus_connection_disconnect (connection);
649 while (i < _DBUS_N_ELEMENTS (message_handlers))
651 if (strcmp (message_handlers[i].name, name) == 0)
653 if ((* message_handlers[i].handler) (connection, transaction, message, error))
662 dbus_set_error (error, DBUS_ERROR_UNKNOWN_MESSAGE,
663 "%s does not understand message %s",
664 DBUS_SERVICE_DBUS, name);
670 bus_driver_remove_connection (DBusConnection *connection)
672 /* FIXME Does nothing for now, should unregister the connection
673 * with the bus driver.