1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* activation.c Activation of services
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
24 #include "activation.h"
25 #include "desktop-file.h"
28 #include <dbus/dbus-internals.h>
29 #include <dbus/dbus-hash.h>
30 #include <dbus/dbus-list.h>
31 #include <dbus/dbus-spawn.h>
32 #include <dbus/dbus-timeout.h>
33 #include <sys/types.h>
37 #define DBUS_SERVICE_SECTION "D-BUS Service"
38 #define DBUS_SERVICE_NAME "Name"
39 #define DBUS_SERVICE_EXEC "Exec"
44 DBusHashTable *entries;
45 DBusHashTable *pending_activations;
48 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
49 * i.e. number of pending activation requests, not pending
60 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
62 struct BusPendingActivationEntry
64 DBusMessage *activation_message;
65 DBusConnection *connection;
71 BusActivation *activation;
75 DBusBabysitter *babysitter;
77 unsigned int timeout_added : 1;
78 } BusPendingActivation;
81 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
83 if (entry->activation_message)
84 dbus_message_unref (entry->activation_message);
86 if (entry->connection)
87 dbus_connection_unref (entry->connection);
93 handle_timeout_callback (DBusTimeout *timeout,
96 BusPendingActivation *pending_activation = data;
98 while (!dbus_timeout_handle (pending_activation->timeout))
99 _dbus_wait_for_memory ();
103 bus_pending_activation_ref (BusPendingActivation *pending_activation)
105 _dbus_assert (pending_activation->refcount > 0);
106 pending_activation->refcount += 1;
110 bus_pending_activation_unref (BusPendingActivation *pending_activation)
114 if (pending_activation == NULL) /* hash table requires this */
117 _dbus_assert (pending_activation->refcount > 0);
118 pending_activation->refcount -= 1;
120 if (pending_activation->refcount > 0)
123 if (pending_activation->timeout_added)
125 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
126 pending_activation->timeout,
127 handle_timeout_callback, pending_activation);
128 pending_activation->timeout_added = FALSE;
131 if (pending_activation->timeout)
132 _dbus_timeout_unref (pending_activation->timeout);
134 if (pending_activation->babysitter)
136 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
138 pending_activation->babysitter,
140 _dbus_assert_not_reached ("setting watch functions to NULL failed");
142 _dbus_babysitter_unref (pending_activation->babysitter);
145 dbus_free (pending_activation->service_name);
147 link = _dbus_list_get_first_link (&pending_activation->entries);
151 BusPendingActivationEntry *entry = link->data;
153 bus_pending_activation_entry_free (entry);
155 link = _dbus_list_get_next_link (&pending_activation->entries, link);
157 _dbus_list_clear (&pending_activation->entries);
159 pending_activation->activation->n_pending_activations -=
160 pending_activation->n_entries;
162 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
164 dbus_free (pending_activation);
168 bus_activation_entry_free (BusActivationEntry *entry)
173 dbus_free (entry->name);
174 dbus_free (entry->exec);
180 add_desktop_file_entry (BusActivation *activation,
181 BusDesktopFile *desktop_file,
185 BusActivationEntry *entry;
187 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
193 if (!bus_desktop_file_get_string (desktop_file,
194 DBUS_SERVICE_SECTION,
198 dbus_set_error (error, DBUS_ERROR_FAILED,
199 "No \""DBUS_SERVICE_NAME"\" key in .service file\n");
203 if (!bus_desktop_file_get_string (desktop_file,
204 DBUS_SERVICE_SECTION,
208 dbus_set_error (error, DBUS_ERROR_FAILED,
209 "No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
213 /* FIXME we need a better-defined algorithm for which service file to
214 * pick than "whichever one is first in the directory listing"
216 if (_dbus_hash_table_lookup_string (activation->entries, name))
218 dbus_set_error (error, DBUS_ERROR_FAILED,
219 "Service %s already exists in activation entry list\n", name);
223 entry = dbus_new0 (BusActivationEntry, 1);
233 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, entry))
239 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
251 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
252 * hash entries it already added.
255 load_directory (BusActivation *activation,
256 const char *directory,
260 DBusString dir, filename;
261 DBusString full_path;
262 BusDesktopFile *desktop_file;
266 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
268 _dbus_string_init_const (&dir, directory);
273 if (!_dbus_string_init (&filename))
279 if (!_dbus_string_init (&full_path))
282 _dbus_string_free (&filename);
288 /* from this point it's safe to "goto out" */
290 iter = _dbus_directory_open (&dir, error);
293 _dbus_verbose ("Failed to open directory %s: %s\n",
294 directory, error ? error->message : "unknown");
298 /* Now read the files */
299 dbus_error_init (&tmp_error);
300 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
302 _dbus_assert (!dbus_error_is_set (&tmp_error));
304 _dbus_string_set_length (&full_path, 0);
306 if (!_dbus_string_append (&full_path, directory) ||
307 !_dbus_concat_dir_and_file (&full_path, &filename))
313 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
315 _dbus_verbose ("Skipping non-.service file %s\n",
316 _dbus_string_get_const_data (&filename));
320 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
322 if (desktop_file == NULL)
324 _dbus_verbose ("Could not load %s: %s\n",
325 _dbus_string_get_const_data (&full_path),
328 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
330 dbus_move_error (&tmp_error, error);
334 dbus_error_free (&tmp_error);
338 if (!add_desktop_file_entry (activation, desktop_file, &tmp_error))
340 bus_desktop_file_free (desktop_file);
343 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
344 _dbus_string_get_const_data (&full_path), tmp_error.message);
346 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
348 dbus_move_error (&tmp_error, error);
352 dbus_error_free (&tmp_error);
357 bus_desktop_file_free (desktop_file);
363 if (dbus_error_is_set (&tmp_error))
365 dbus_move_error (&tmp_error, error);
373 _DBUS_ASSERT_ERROR_IS_SET (error);
375 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
378 _dbus_directory_close (iter);
380 bus_desktop_file_free (desktop_file);
381 _dbus_string_free (&filename);
382 _dbus_string_free (&full_path);
388 bus_activation_new (BusContext *context,
389 const DBusString *address,
390 DBusList **directories,
393 BusActivation *activation;
396 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
398 activation = dbus_new0 (BusActivation, 1);
399 if (activation == NULL)
405 activation->refcount = 1;
406 activation->context = context;
407 activation->n_pending_activations = 0;
409 if (!_dbus_string_copy_data (address, &activation->server_address))
415 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
416 (DBusFreeFunction)bus_activation_entry_free);
417 if (activation->entries == NULL)
423 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
424 (DBusFreeFunction)bus_pending_activation_unref);
426 if (activation->pending_activations == NULL)
432 /* Load service files */
433 link = _dbus_list_get_first_link (directories);
436 if (!load_directory (activation, link->data, error))
438 link = _dbus_list_get_next_link (directories, link);
444 bus_activation_unref (activation);
449 bus_activation_ref (BusActivation *activation)
451 _dbus_assert (activation->refcount > 0);
453 activation->refcount += 1;
457 bus_activation_unref (BusActivation *activation)
459 _dbus_assert (activation->refcount > 0);
461 activation->refcount -= 1;
463 if (activation->refcount == 0)
465 dbus_free (activation->server_address);
466 if (activation->entries)
467 _dbus_hash_table_unref (activation->entries);
468 if (activation->pending_activations)
469 _dbus_hash_table_unref (activation->pending_activations);
470 dbus_free (activation);
475 child_setup (void *data)
477 BusActivation *activation = data;
480 /* If no memory, we simply have the child exit, so it won't try
481 * to connect to the wrong thing.
483 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", activation->server_address))
486 type = bus_context_get_type (activation->context);
489 if (!_dbus_setenv ("DBUS_BUS_TYPE", type))
496 BusPendingActivation *pending_activation;
497 DBusPreallocatedHash *hash_entry;
498 } RestorePendingData;
501 restore_pending (void *data)
503 RestorePendingData *d = data;
505 _dbus_assert (d->pending_activation != NULL);
506 _dbus_assert (d->hash_entry != NULL);
508 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
509 d->pending_activation->service_name,
510 d->pending_activation->timeout_added);
512 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
514 d->pending_activation->service_name, d->pending_activation);
516 bus_pending_activation_ref (d->pending_activation);
518 d->hash_entry = NULL;
522 free_pending_restore_data (void *data)
524 RestorePendingData *d = data;
527 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
530 bus_pending_activation_unref (d->pending_activation);
536 add_restore_pending_to_transaction (BusTransaction *transaction,
537 BusPendingActivation *pending_activation)
539 RestorePendingData *d;
541 d = dbus_new (RestorePendingData, 1);
545 d->pending_activation = pending_activation;
546 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
548 bus_pending_activation_ref (d->pending_activation);
550 if (d->hash_entry == NULL ||
551 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
552 free_pending_restore_data))
554 free_pending_restore_data (d);
558 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
564 bus_activation_service_created (BusActivation *activation,
565 const char *service_name,
566 BusTransaction *transaction,
569 BusPendingActivation *pending_activation;
570 DBusMessage *message;
573 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
575 /* Check if it's a pending activation */
576 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
578 if (!pending_activation)
581 link = _dbus_list_get_first_link (&pending_activation->entries);
584 BusPendingActivationEntry *entry = link->data;
585 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
587 if (dbus_connection_get_is_connected (entry->connection))
589 message = dbus_message_new_reply (entry->activation_message);
596 if (!dbus_message_append_args (message,
597 DBUS_TYPE_UINT32, DBUS_ACTIVATION_REPLY_ACTIVATED,
600 dbus_message_unref (message);
605 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
607 dbus_message_unref (message);
612 dbus_message_unref (message);
618 if (!add_restore_pending_to_transaction (transaction, pending_activation))
620 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
625 _dbus_hash_table_remove_string (activation->pending_activations, service_name);
634 * FIXME @todo the error messages here would ideally be preallocated
635 * so we don't need to allocate memory to send them.
636 * Using the usual tactic, prealloc an OOM message, then
637 * if we can't alloc the real error send the OOM error instead.
640 try_send_activation_failure (BusPendingActivation *pending_activation,
641 const DBusError *how)
643 BusActivation *activation;
645 BusTransaction *transaction;
647 activation = pending_activation->activation;
649 transaction = bus_transaction_new (activation->context);
650 if (transaction == NULL)
653 link = _dbus_list_get_first_link (&pending_activation->entries);
656 BusPendingActivationEntry *entry = link->data;
657 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
659 if (dbus_connection_get_is_connected (entry->connection))
661 if (!bus_transaction_send_error_reply (transaction,
664 entry->activation_message))
671 bus_transaction_execute_and_free (transaction);
677 bus_transaction_cancel_and_free (transaction);
682 * Free the pending activation and send an error message to all the
683 * connections that were waiting for it.
686 pending_activation_failed (BusPendingActivation *pending_activation,
687 const DBusError *how)
689 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
690 while (!try_send_activation_failure (pending_activation, how))
691 _dbus_wait_for_memory ();
693 /* Destroy this pending activation */
694 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
695 pending_activation->service_name);
699 babysitter_watch_callback (DBusWatch *watch,
700 unsigned int condition,
703 BusPendingActivation *pending_activation = data;
705 DBusBabysitter *babysitter;
707 babysitter = pending_activation->babysitter;
709 _dbus_babysitter_ref (babysitter);
711 retval = dbus_watch_handle (watch, condition);
713 /* FIXME this is broken in the same way that
714 * connection watches used to be; there should be
715 * a separate callback for status change, instead
716 * of doing "if we handled a watch status might
719 * Fixing this lets us move dbus_watch_handle
720 * calls into dbus-mainloop.c
723 if (_dbus_babysitter_get_child_exited (babysitter))
727 dbus_error_init (&error);
728 _dbus_babysitter_set_child_exit_error (babysitter, &error);
730 /* Destroys the pending activation */
731 pending_activation_failed (pending_activation, &error);
733 dbus_error_free (&error);
736 _dbus_babysitter_unref (babysitter);
742 add_babysitter_watch (DBusWatch *watch,
745 BusPendingActivation *pending_activation = data;
747 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
748 watch, babysitter_watch_callback, pending_activation,
753 remove_babysitter_watch (DBusWatch *watch,
756 BusPendingActivation *pending_activation = data;
758 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
759 watch, babysitter_watch_callback, pending_activation);
763 pending_activation_timed_out (void *data)
765 BusPendingActivation *pending_activation = data;
768 /* Kill the spawned process, since it sucks
769 * (not sure this is what we want to do, but
770 * may as well try it for now)
772 _dbus_babysitter_kill_child (pending_activation->babysitter);
774 dbus_error_init (&error);
776 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
777 "Activation of %s timed out",
778 pending_activation->service_name);
780 pending_activation_failed (pending_activation, &error);
782 dbus_error_free (&error);
788 cancel_pending (void *data)
790 BusPendingActivation *pending_activation = data;
792 _dbus_verbose ("Canceling pending activation of %s\n",
793 pending_activation->service_name);
795 if (pending_activation->babysitter)
796 _dbus_babysitter_kill_child (pending_activation->babysitter);
798 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
799 pending_activation->service_name);
803 free_pending_cancel_data (void *data)
805 BusPendingActivation *pending_activation = data;
807 bus_pending_activation_unref (pending_activation);
811 add_cancel_pending_to_transaction (BusTransaction *transaction,
812 BusPendingActivation *pending_activation)
814 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
816 free_pending_cancel_data))
819 bus_pending_activation_ref (pending_activation);
821 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
827 bus_activation_activate_service (BusActivation *activation,
828 DBusConnection *connection,
829 BusTransaction *transaction,
830 DBusMessage *activation_message,
831 const char *service_name,
834 BusActivationEntry *entry;
835 BusPendingActivation *pending_activation;
836 BusPendingActivationEntry *pending_activation_entry;
837 DBusMessage *message;
838 DBusString service_str;
842 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
844 if (activation->n_pending_activations >=
845 bus_context_get_max_pending_activations (activation->context))
847 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
848 "The maximum number of pending activations has been reached, activation of %s failed",
853 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
857 dbus_set_error (error, DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND,
858 "The service %s was not found in the activation entry list",
863 /* Check if the service is active */
864 _dbus_string_init_const (&service_str, service_name);
865 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
867 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
869 message = dbus_message_new_reply (activation_message);
873 _dbus_verbose ("No memory to create reply to activate message\n");
878 if (!dbus_message_append_args (message,
879 DBUS_TYPE_UINT32, DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE,
882 _dbus_verbose ("No memory to set args of reply to activate message\n");
884 dbus_message_unref (message);
888 retval = bus_transaction_send_from_driver (transaction, connection, message);
889 dbus_message_unref (message);
892 _dbus_verbose ("Failed to send reply\n");
899 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
900 if (!pending_activation_entry)
902 _dbus_verbose ("Failed to create pending activation entry\n");
907 pending_activation_entry->activation_message = activation_message;
908 dbus_message_ref (activation_message);
909 pending_activation_entry->connection = connection;
910 dbus_connection_ref (connection);
912 /* Check if the service is being activated */
913 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
914 if (pending_activation)
916 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
918 _dbus_verbose ("Failed to append a new entry to pending activation\n");
921 bus_pending_activation_entry_free (pending_activation_entry);
925 pending_activation->n_entries += 1;
926 pending_activation->activation->n_pending_activations += 1;
930 pending_activation = dbus_new0 (BusPendingActivation, 1);
931 if (!pending_activation)
933 _dbus_verbose ("Failed to create pending activation\n");
936 bus_pending_activation_entry_free (pending_activation_entry);
940 pending_activation->activation = activation;
941 pending_activation->refcount = 1;
943 pending_activation->service_name = _dbus_strdup (service_name);
944 if (!pending_activation->service_name)
946 _dbus_verbose ("Failed to copy service name for pending activation\n");
949 bus_pending_activation_unref (pending_activation);
950 bus_pending_activation_entry_free (pending_activation_entry);
954 pending_activation->timeout =
955 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
956 pending_activation_timed_out,
959 if (!pending_activation->timeout)
961 _dbus_verbose ("Failed to create timeout for pending activation\n");
964 bus_pending_activation_unref (pending_activation);
965 bus_pending_activation_entry_free (pending_activation_entry);
969 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
970 pending_activation->timeout,
971 handle_timeout_callback,
975 _dbus_verbose ("Failed to add timeout for pending activation\n");
978 bus_pending_activation_unref (pending_activation);
979 bus_pending_activation_entry_free (pending_activation_entry);
983 pending_activation->timeout_added = TRUE;
985 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
987 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
990 bus_pending_activation_unref (pending_activation);
991 bus_pending_activation_entry_free (pending_activation_entry);
995 pending_activation->n_entries += 1;
996 pending_activation->activation->n_pending_activations += 1;
998 if (!_dbus_hash_table_insert_string (activation->pending_activations,
999 pending_activation->service_name,
1000 pending_activation))
1002 _dbus_verbose ("Failed to put pending activation in hash table\n");
1004 BUS_SET_OOM (error);
1005 bus_pending_activation_unref (pending_activation);
1010 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1012 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1013 BUS_SET_OOM (error);
1014 _dbus_hash_table_remove_string (activation->pending_activations,
1015 pending_activation->service_name);
1019 /* FIXME we need to support a full command line, not just a single
1023 /* Now try to spawn the process */
1024 argv[0] = entry->exec;
1027 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1028 child_setup, activation,
1031 _dbus_verbose ("Failed to spawn child\n");
1032 _DBUS_ASSERT_ERROR_IS_SET (error);
1036 _dbus_assert (pending_activation->babysitter != NULL);
1038 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1039 add_babysitter_watch,
1040 remove_babysitter_watch,
1045 BUS_SET_OOM (error);
1046 _dbus_verbose ("Failed to set babysitter watch functions\n");