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))
492 if (strcmp (type, "session") == 0)
494 if (!_dbus_setenv ("DBUS_SESSION_BUS_ADDRESS",
495 activation->server_address))
498 else if (strcmp (type, "system") == 0)
500 if (!_dbus_setenv ("DBUS_SYSTEM_BUS_ADDRESS",
501 activation->server_address))
509 BusPendingActivation *pending_activation;
510 DBusPreallocatedHash *hash_entry;
511 } RestorePendingData;
514 restore_pending (void *data)
516 RestorePendingData *d = data;
518 _dbus_assert (d->pending_activation != NULL);
519 _dbus_assert (d->hash_entry != NULL);
521 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
522 d->pending_activation->service_name,
523 d->pending_activation->timeout_added);
525 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
527 d->pending_activation->service_name, d->pending_activation);
529 bus_pending_activation_ref (d->pending_activation);
531 d->hash_entry = NULL;
535 free_pending_restore_data (void *data)
537 RestorePendingData *d = data;
540 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
543 bus_pending_activation_unref (d->pending_activation);
549 add_restore_pending_to_transaction (BusTransaction *transaction,
550 BusPendingActivation *pending_activation)
552 RestorePendingData *d;
554 d = dbus_new (RestorePendingData, 1);
558 d->pending_activation = pending_activation;
559 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
561 bus_pending_activation_ref (d->pending_activation);
563 if (d->hash_entry == NULL ||
564 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
565 free_pending_restore_data))
567 free_pending_restore_data (d);
571 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
577 bus_activation_service_created (BusActivation *activation,
578 const char *service_name,
579 BusTransaction *transaction,
582 BusPendingActivation *pending_activation;
583 DBusMessage *message;
586 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
588 /* Check if it's a pending activation */
589 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
591 if (!pending_activation)
594 link = _dbus_list_get_first_link (&pending_activation->entries);
597 BusPendingActivationEntry *entry = link->data;
598 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
600 if (dbus_connection_get_is_connected (entry->connection))
602 message = dbus_message_new_method_return (entry->activation_message);
609 if (!dbus_message_append_args (message,
610 DBUS_TYPE_UINT32, DBUS_ACTIVATION_REPLY_ACTIVATED,
613 dbus_message_unref (message);
618 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
620 dbus_message_unref (message);
625 dbus_message_unref (message);
631 if (!add_restore_pending_to_transaction (transaction, pending_activation))
633 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
638 _dbus_hash_table_remove_string (activation->pending_activations, service_name);
647 * FIXME @todo the error messages here would ideally be preallocated
648 * so we don't need to allocate memory to send them.
649 * Using the usual tactic, prealloc an OOM message, then
650 * if we can't alloc the real error send the OOM error instead.
653 try_send_activation_failure (BusPendingActivation *pending_activation,
654 const DBusError *how)
656 BusActivation *activation;
658 BusTransaction *transaction;
660 activation = pending_activation->activation;
662 transaction = bus_transaction_new (activation->context);
663 if (transaction == NULL)
666 link = _dbus_list_get_first_link (&pending_activation->entries);
669 BusPendingActivationEntry *entry = link->data;
670 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
672 if (dbus_connection_get_is_connected (entry->connection))
674 if (!bus_transaction_send_error_reply (transaction,
677 entry->activation_message))
684 bus_transaction_execute_and_free (transaction);
690 bus_transaction_cancel_and_free (transaction);
695 * Free the pending activation and send an error message to all the
696 * connections that were waiting for it.
699 pending_activation_failed (BusPendingActivation *pending_activation,
700 const DBusError *how)
702 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
703 while (!try_send_activation_failure (pending_activation, how))
704 _dbus_wait_for_memory ();
706 /* Destroy this pending activation */
707 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
708 pending_activation->service_name);
712 babysitter_watch_callback (DBusWatch *watch,
713 unsigned int condition,
716 BusPendingActivation *pending_activation = data;
718 DBusBabysitter *babysitter;
720 babysitter = pending_activation->babysitter;
722 _dbus_babysitter_ref (babysitter);
724 retval = dbus_watch_handle (watch, condition);
726 /* FIXME this is broken in the same way that
727 * connection watches used to be; there should be
728 * a separate callback for status change, instead
729 * of doing "if we handled a watch status might
732 * Fixing this lets us move dbus_watch_handle
733 * calls into dbus-mainloop.c
736 if (_dbus_babysitter_get_child_exited (babysitter))
740 dbus_error_init (&error);
741 _dbus_babysitter_set_child_exit_error (babysitter, &error);
743 /* Destroys the pending activation */
744 pending_activation_failed (pending_activation, &error);
746 dbus_error_free (&error);
749 _dbus_babysitter_unref (babysitter);
755 add_babysitter_watch (DBusWatch *watch,
758 BusPendingActivation *pending_activation = data;
760 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
761 watch, babysitter_watch_callback, pending_activation,
766 remove_babysitter_watch (DBusWatch *watch,
769 BusPendingActivation *pending_activation = data;
771 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
772 watch, babysitter_watch_callback, pending_activation);
776 pending_activation_timed_out (void *data)
778 BusPendingActivation *pending_activation = data;
781 /* Kill the spawned process, since it sucks
782 * (not sure this is what we want to do, but
783 * may as well try it for now)
785 _dbus_babysitter_kill_child (pending_activation->babysitter);
787 dbus_error_init (&error);
789 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
790 "Activation of %s timed out",
791 pending_activation->service_name);
793 pending_activation_failed (pending_activation, &error);
795 dbus_error_free (&error);
801 cancel_pending (void *data)
803 BusPendingActivation *pending_activation = data;
805 _dbus_verbose ("Canceling pending activation of %s\n",
806 pending_activation->service_name);
808 if (pending_activation->babysitter)
809 _dbus_babysitter_kill_child (pending_activation->babysitter);
811 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
812 pending_activation->service_name);
816 free_pending_cancel_data (void *data)
818 BusPendingActivation *pending_activation = data;
820 bus_pending_activation_unref (pending_activation);
824 add_cancel_pending_to_transaction (BusTransaction *transaction,
825 BusPendingActivation *pending_activation)
827 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
829 free_pending_cancel_data))
832 bus_pending_activation_ref (pending_activation);
834 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
840 bus_activation_activate_service (BusActivation *activation,
841 DBusConnection *connection,
842 BusTransaction *transaction,
843 DBusMessage *activation_message,
844 const char *service_name,
847 BusActivationEntry *entry;
848 BusPendingActivation *pending_activation;
849 BusPendingActivationEntry *pending_activation_entry;
850 DBusMessage *message;
851 DBusString service_str;
855 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
857 if (activation->n_pending_activations >=
858 bus_context_get_max_pending_activations (activation->context))
860 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
861 "The maximum number of pending activations has been reached, activation of %s failed",
866 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
870 dbus_set_error (error, DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND,
871 "The service %s was not found in the activation entry list",
876 /* Check if the service is active */
877 _dbus_string_init_const (&service_str, service_name);
878 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
880 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
882 message = dbus_message_new_method_return (activation_message);
886 _dbus_verbose ("No memory to create reply to activate message\n");
891 if (!dbus_message_append_args (message,
892 DBUS_TYPE_UINT32, DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE,
895 _dbus_verbose ("No memory to set args of reply to activate message\n");
897 dbus_message_unref (message);
901 retval = bus_transaction_send_from_driver (transaction, connection, message);
902 dbus_message_unref (message);
905 _dbus_verbose ("Failed to send reply\n");
912 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
913 if (!pending_activation_entry)
915 _dbus_verbose ("Failed to create pending activation entry\n");
920 pending_activation_entry->activation_message = activation_message;
921 dbus_message_ref (activation_message);
922 pending_activation_entry->connection = connection;
923 dbus_connection_ref (connection);
925 /* Check if the service is being activated */
926 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
927 if (pending_activation)
929 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
931 _dbus_verbose ("Failed to append a new entry to pending activation\n");
934 bus_pending_activation_entry_free (pending_activation_entry);
938 pending_activation->n_entries += 1;
939 pending_activation->activation->n_pending_activations += 1;
943 pending_activation = dbus_new0 (BusPendingActivation, 1);
944 if (!pending_activation)
946 _dbus_verbose ("Failed to create pending activation\n");
949 bus_pending_activation_entry_free (pending_activation_entry);
953 pending_activation->activation = activation;
954 pending_activation->refcount = 1;
956 pending_activation->service_name = _dbus_strdup (service_name);
957 if (!pending_activation->service_name)
959 _dbus_verbose ("Failed to copy service name for pending activation\n");
962 bus_pending_activation_unref (pending_activation);
963 bus_pending_activation_entry_free (pending_activation_entry);
967 pending_activation->timeout =
968 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
969 pending_activation_timed_out,
972 if (!pending_activation->timeout)
974 _dbus_verbose ("Failed to create timeout for pending activation\n");
977 bus_pending_activation_unref (pending_activation);
978 bus_pending_activation_entry_free (pending_activation_entry);
982 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
983 pending_activation->timeout,
984 handle_timeout_callback,
988 _dbus_verbose ("Failed to add timeout for pending activation\n");
991 bus_pending_activation_unref (pending_activation);
992 bus_pending_activation_entry_free (pending_activation_entry);
996 pending_activation->timeout_added = TRUE;
998 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1000 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1002 BUS_SET_OOM (error);
1003 bus_pending_activation_unref (pending_activation);
1004 bus_pending_activation_entry_free (pending_activation_entry);
1008 pending_activation->n_entries += 1;
1009 pending_activation->activation->n_pending_activations += 1;
1011 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1012 pending_activation->service_name,
1013 pending_activation))
1015 _dbus_verbose ("Failed to put pending activation in hash table\n");
1017 BUS_SET_OOM (error);
1018 bus_pending_activation_unref (pending_activation);
1023 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1025 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1026 BUS_SET_OOM (error);
1027 _dbus_hash_table_remove_string (activation->pending_activations,
1028 pending_activation->service_name);
1032 /* FIXME we need to support a full command line, not just a single
1036 /* Now try to spawn the process */
1037 argv[0] = entry->exec;
1040 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1041 child_setup, activation,
1044 _dbus_verbose ("Failed to spawn child\n");
1045 _DBUS_ASSERT_ERROR_IS_SET (error);
1049 _dbus_assert (pending_activation->babysitter != NULL);
1051 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1052 add_babysitter_watch,
1053 remove_babysitter_watch,
1058 BUS_SET_OOM (error);
1059 _dbus_verbose ("Failed to set babysitter watch functions\n");