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_MESSAGE_SERVICE_DELETED,
53 DBUS_SERVICE_BROADCAST);
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, NULL, 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_MESSAGE_SERVICE_CREATED,
87 DBUS_SERVICE_BROADCAST);
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, NULL, 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 (DBUS_MESSAGE_SERVICE_LOST,
127 bus_connection_get_name (connection));
134 if (!dbus_message_append_args (message,
135 DBUS_TYPE_STRING, service_name,
138 dbus_message_unref (message);
143 if (!bus_transaction_send_from_driver (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 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
166 message = dbus_message_new (DBUS_MESSAGE_SERVICE_ACQUIRED,
167 bus_connection_get_name (connection));
175 if (!dbus_message_append_args (message,
176 DBUS_TYPE_STRING, service_name,
179 dbus_message_unref (message);
184 if (!bus_transaction_send_from_driver (transaction, connection, message))
186 dbus_message_unref (message);
192 dbus_message_unref (message);
198 create_unique_client_name (BusRegistry *registry,
201 /* We never want to use the same unique client name twice, because
202 * we want to guarantee that if you send a message to a given unique
203 * name, you always get the same application. So we use two numbers
204 * for INT_MAX * INT_MAX combinations, should be pretty safe against
207 /* FIXME these should be in BusRegistry rather than static vars */
208 static int next_major_number = 0;
209 static int next_minor_number = 0;
212 len = _dbus_string_get_length (str);
216 /* start out with 1-0, go to 1-1, 1-2, 1-3,
217 * up to 1-MAXINT, then 2-0, 2-1, etc.
219 if (next_minor_number <= 0)
221 next_major_number += 1;
222 next_minor_number = 0;
223 if (next_major_number <= 0)
224 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
227 _dbus_assert (next_major_number > 0);
228 _dbus_assert (next_minor_number >= 0);
230 /* appname:MAJOR-MINOR */
232 if (!_dbus_string_append (str, ":"))
235 if (!_dbus_string_append_int (str, next_major_number))
238 if (!_dbus_string_append (str, "-"))
241 if (!_dbus_string_append_int (str, next_minor_number))
244 next_minor_number += 1;
246 /* Check if a client with the name exists */
247 if (bus_registry_lookup (registry, str) == NULL)
250 /* drop the number again, try the next one. */
251 _dbus_string_set_length (str, len);
258 bus_driver_handle_hello (DBusConnection *connection,
259 BusTransaction *transaction,
260 DBusMessage *message,
263 DBusString unique_name;
266 BusRegistry *registry;
267 BusConnections *connections;
269 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
271 /* Note that when these limits are exceeded we don't disconnect the
272 * connection; we just sort of leave it hanging there until it times
273 * out or disconnects itself or is dropped due to the max number of
274 * incomplete connections. It's even OK if the connection wants to
275 * retry the hello message, we support that.
277 connections = bus_connection_get_connections (connection);
278 if (!bus_connections_check_limits (connections, connection,
281 _DBUS_ASSERT_ERROR_IS_SET (error);
285 if (!_dbus_string_init (&unique_name))
293 registry = bus_connection_get_registry (connection);
295 if (!create_unique_client_name (registry, &unique_name))
301 if (!bus_connection_complete (connection, &unique_name, error))
303 _DBUS_ASSERT_ERROR_IS_SET (error);
307 if (!dbus_message_set_sender (message,
308 bus_connection_get_name (connection)))
314 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
317 /* Create the service */
318 service = bus_registry_ensure (registry,
319 &unique_name, connection, transaction, error);
323 bus_service_set_prohibit_replacement (service, TRUE);
328 _dbus_string_free (&unique_name);
333 bus_driver_send_welcome_message (DBusConnection *connection,
334 DBusMessage *hello_message,
335 BusTransaction *transaction,
338 DBusMessage *welcome;
341 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
343 name = bus_connection_get_name (connection);
344 _dbus_assert (name != NULL);
346 welcome = dbus_message_new_reply (hello_message);
353 if (!dbus_message_append_args (welcome,
354 DBUS_TYPE_STRING, name,
357 dbus_message_unref (welcome);
362 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
364 dbus_message_unref (welcome);
370 dbus_message_unref (welcome);
376 bus_driver_handle_list_services (DBusConnection *connection,
377 BusTransaction *transaction,
378 DBusMessage *message,
384 BusRegistry *registry;
386 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
388 registry = bus_connection_get_registry (connection);
390 reply = dbus_message_new_reply (message);
397 if (!bus_registry_list_services (registry, &services, &len))
399 dbus_message_unref (reply);
404 if (!dbus_message_append_args (reply,
405 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, services, len,
408 dbus_free_string_array (services);
409 dbus_message_unref (reply);
414 dbus_free_string_array (services);
416 if (!bus_transaction_send_from_driver (transaction, connection, reply))
418 dbus_message_unref (reply);
424 dbus_message_unref (reply);
430 bus_driver_handle_acquire_service (DBusConnection *connection,
431 BusTransaction *transaction,
432 DBusMessage *message,
436 DBusString service_name;
441 BusRegistry *registry;
443 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
445 registry = bus_connection_get_registry (connection);
447 if (!dbus_message_get_args (message, error,
448 DBUS_TYPE_STRING, &name,
449 DBUS_TYPE_UINT32, &flags,
453 _dbus_verbose ("Trying to own service %s with flags 0x%x\n", name, flags);
458 _dbus_string_init_const (&service_name, name);
460 if (!bus_registry_acquire_service (registry, connection,
461 &service_name, flags,
462 &service_reply, transaction,
466 reply = dbus_message_new_reply (message);
473 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, service_reply, DBUS_TYPE_INVALID))
479 if (!bus_transaction_send_from_driver (transaction, connection, reply))
490 dbus_message_unref (reply);
495 bus_driver_handle_service_exists (DBusConnection *connection,
496 BusTransaction *transaction,
497 DBusMessage *message,
501 DBusString service_name;
505 BusRegistry *registry;
507 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
509 registry = bus_connection_get_registry (connection);
511 if (!dbus_message_get_args (message, error,
512 DBUS_TYPE_STRING, &name,
518 _dbus_string_init_const (&service_name, name);
519 service = bus_registry_lookup (registry, &service_name);
521 reply = dbus_message_new_reply (message);
528 if (!dbus_message_append_args (reply,
529 DBUS_TYPE_UINT32, service != NULL,
536 if (!bus_transaction_send_from_driver (transaction, connection, reply))
546 dbus_message_unref (reply);
553 bus_driver_handle_activate_service (DBusConnection *connection,
554 BusTransaction *transaction,
555 DBusMessage *message,
561 BusActivation *activation;
563 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
565 activation = bus_connection_get_activation (connection);
567 if (!dbus_message_get_args (message, error,
568 DBUS_TYPE_STRING, &name,
569 DBUS_TYPE_UINT32, &flags,
572 _DBUS_ASSERT_ERROR_IS_SET (error);
573 _dbus_verbose ("No memory to get arguments to ActivateService\n");
579 if (!bus_activation_activate_service (activation, connection, transaction,
580 message, name, error))
582 _DBUS_ASSERT_ERROR_IS_SET (error);
583 _dbus_verbose ("bus_activation_activate_service() failed\n");
594 /* For speed it might be useful to sort this in order of
595 * frequency of use (but doesn't matter with only a few items
601 dbus_bool_t (* handler) (DBusConnection *connection,
602 BusTransaction *transaction,
603 DBusMessage *message,
605 } message_handlers[] = {
606 { DBUS_MESSAGE_ACQUIRE_SERVICE, bus_driver_handle_acquire_service },
607 { DBUS_MESSAGE_ACTIVATE_SERVICE, bus_driver_handle_activate_service },
608 { DBUS_MESSAGE_HELLO, bus_driver_handle_hello },
609 { DBUS_MESSAGE_SERVICE_EXISTS, bus_driver_handle_service_exists },
610 { DBUS_MESSAGE_LIST_SERVICES, bus_driver_handle_list_services }
614 bus_driver_handle_message (DBusConnection *connection,
615 BusTransaction *transaction,
616 DBusMessage *message,
619 const char *name, *sender;
622 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
624 _dbus_verbose ("Driver got a message: %s\n",
625 dbus_message_get_name (message));
627 name = dbus_message_get_name (message);
628 sender = dbus_message_get_sender (message);
630 /* security checks should have kept this from getting here */
631 _dbus_assert (sender != NULL || strcmp (name, DBUS_MESSAGE_HELLO) == 0);
633 if (dbus_message_get_reply_serial (message) == 0)
635 _dbus_verbose ("Client sent a reply to the bus driver, ignoring it\n");
640 while (i < _DBUS_N_ELEMENTS (message_handlers))
642 if (strcmp (message_handlers[i].name, name) == 0)
644 _dbus_verbose ("Running driver handler for %s\n", name);
645 if ((* message_handlers[i].handler) (connection, transaction, message, error))
647 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
648 _dbus_verbose ("Driver handler succeeded\n");
653 _DBUS_ASSERT_ERROR_IS_SET (error);
654 _dbus_verbose ("Driver handler returned failure\n");
662 _dbus_verbose ("No driver handler for %s\n", name);
664 dbus_set_error (error, DBUS_ERROR_UNKNOWN_MESSAGE,
665 "%s does not understand message %s",
666 DBUS_SERVICE_DBUS, name);
672 bus_driver_remove_connection (DBusConnection *connection)
674 /* FIXME Does nothing for now, should unregister the connection
675 * with the bus driver.