1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* activation.c Activation of services
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "activation.h"
26 #include "activation-exit-codes.h"
27 #include "desktop-file.h"
31 #include <dbus/dbus-internals.h>
32 #include <dbus/dbus-hash.h>
33 #include <dbus/dbus-list.h>
34 #include <dbus/dbus-shell.h>
35 #include <dbus/dbus-spawn.h>
36 #include <dbus/dbus-timeout.h>
37 #include <dbus/dbus-sysdeps.h>
45 DBusHashTable *entries;
46 DBusHashTable *pending_activations;
49 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
50 * i.e. number of pending activation requests, not pending
53 DBusHashTable *directories;
60 DBusHashTable *entries;
61 } BusServiceDirectory;
70 BusServiceDirectory *s_dir;
74 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
76 struct BusPendingActivationEntry
78 DBusMessage *activation_message;
79 DBusConnection *connection;
81 dbus_bool_t auto_activation;
87 BusActivation *activation;
92 DBusBabysitter *babysitter;
94 unsigned int timeout_added : 1;
95 } BusPendingActivation;
98 static BusServiceDirectory *
99 bus_service_directory_ref (BusServiceDirectory *dir)
101 _dbus_assert (dir->refcount);
110 bus_service_directory_unref (BusServiceDirectory *dir)
115 _dbus_assert (dir->refcount > 0);
118 if (dir->refcount > 0)
122 _dbus_hash_table_unref (dir->entries);
124 dbus_free (dir->dir_c);
129 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
131 if (entry->activation_message)
132 dbus_message_unref (entry->activation_message);
134 if (entry->connection)
135 dbus_connection_unref (entry->connection);
141 handle_timeout_callback (DBusTimeout *timeout,
144 BusPendingActivation *pending_activation = data;
146 while (!dbus_timeout_handle (pending_activation->timeout))
147 _dbus_wait_for_memory ();
150 static BusPendingActivation *
151 bus_pending_activation_ref (BusPendingActivation *pending_activation)
153 _dbus_assert (pending_activation->refcount > 0);
154 pending_activation->refcount += 1;
156 return pending_activation;
160 bus_pending_activation_unref (BusPendingActivation *pending_activation)
164 if (pending_activation == NULL) /* hash table requires this */
167 _dbus_assert (pending_activation->refcount > 0);
168 pending_activation->refcount -= 1;
170 if (pending_activation->refcount > 0)
173 if (pending_activation->timeout_added)
175 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
176 pending_activation->timeout,
177 handle_timeout_callback, pending_activation);
178 pending_activation->timeout_added = FALSE;
181 if (pending_activation->timeout)
182 _dbus_timeout_unref (pending_activation->timeout);
184 if (pending_activation->babysitter)
186 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
188 pending_activation->babysitter,
190 _dbus_assert_not_reached ("setting watch functions to NULL failed");
192 _dbus_babysitter_unref (pending_activation->babysitter);
195 dbus_free (pending_activation->service_name);
196 dbus_free (pending_activation->exec);
198 link = _dbus_list_get_first_link (&pending_activation->entries);
202 BusPendingActivationEntry *entry = link->data;
204 bus_pending_activation_entry_free (entry);
206 link = _dbus_list_get_next_link (&pending_activation->entries, link);
208 _dbus_list_clear (&pending_activation->entries);
210 pending_activation->activation->n_pending_activations -=
211 pending_activation->n_entries;
213 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
215 dbus_free (pending_activation);
218 static BusActivationEntry *
219 bus_activation_entry_ref (BusActivationEntry *entry)
221 _dbus_assert (entry->refcount > 0);
228 bus_activation_entry_unref (BusActivationEntry *entry)
230 if (entry == NULL) /* hash table requires this */
233 _dbus_assert (entry->refcount > 0);
236 if (entry->refcount > 0)
239 dbus_free (entry->name);
240 dbus_free (entry->exec);
241 dbus_free (entry->user);
242 dbus_free (entry->filename);
248 update_desktop_file_entry (BusActivation *activation,
249 BusServiceDirectory *s_dir,
250 DBusString *filename,
251 BusDesktopFile *desktop_file,
254 char *name, *exec, *user;
255 BusActivationEntry *entry;
257 DBusString file_path;
260 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
267 dbus_error_init (&tmp_error);
269 if (!_dbus_string_init (&file_path))
275 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
276 !_dbus_concat_dir_and_file (&file_path, filename))
282 if (!_dbus_stat (&file_path, &stat_buf, NULL))
284 dbus_set_error (error, DBUS_ERROR_FAILED,
285 "Can't stat the service file\n");
289 if (!bus_desktop_file_get_string (desktop_file,
290 DBUS_SERVICE_SECTION,
296 if (!bus_desktop_file_get_string (desktop_file,
297 DBUS_SERVICE_SECTION,
303 /* user is not _required_ unless we are using system activation */
304 if (!bus_desktop_file_get_string (desktop_file,
305 DBUS_SERVICE_SECTION,
309 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
310 /* if we got OOM, then exit */
311 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
313 dbus_move_error (&tmp_error, error);
318 /* if we have error because we didn't find anything then continue */
319 dbus_error_free (&tmp_error);
324 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
326 entry = _dbus_hash_table_lookup_string (s_dir->entries,
327 _dbus_string_get_const_data (filename));
328 if (entry == NULL) /* New file */
330 /* FIXME we need a better-defined algorithm for which service file to
331 * pick than "whichever one is first in the directory listing"
333 if (_dbus_hash_table_lookup_string (activation->entries, name))
335 dbus_set_error (error, DBUS_ERROR_FAILED,
336 "Service %s already exists in activation entry list\n", name);
340 entry = dbus_new0 (BusActivationEntry, 1);
352 entry->s_dir = s_dir;
353 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
354 if (!entry->filename)
360 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
366 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
368 /* Revert the insertion in the entries table */
369 _dbus_hash_table_remove_string (activation->entries, entry->name);
374 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
376 else /* Just update the entry */
378 bus_activation_entry_ref (entry);
379 _dbus_hash_table_remove_string (activation->entries, entry->name);
381 if (_dbus_hash_table_lookup_string (activation->entries, name))
383 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
384 name, _dbus_string_get_const_data (&file_path));
388 dbus_free (entry->name);
389 dbus_free (entry->exec);
390 dbus_free (entry->user);
394 if (!_dbus_hash_table_insert_string (activation->entries,
395 entry->name, bus_activation_entry_ref(entry)))
398 /* Also remove path to entries hash since we want this in sync with
399 * the entries hash table */
400 _dbus_hash_table_remove_string (entry->s_dir->entries,
402 bus_activation_entry_unref (entry);
407 entry->mtime = stat_buf.mtime;
409 _dbus_string_free (&file_path);
410 bus_activation_entry_unref (entry);
418 _dbus_string_free (&file_path);
421 bus_activation_entry_unref (entry);
427 check_service_file (BusActivation *activation,
428 BusActivationEntry *entry,
429 BusActivationEntry **updated_entry,
434 BusActivationEntry *tmp_entry;
435 DBusString file_path;
441 _dbus_string_init_const (&filename, entry->filename);
443 if (!_dbus_string_init (&file_path))
449 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
450 !_dbus_concat_dir_and_file (&file_path, &filename))
457 if (!_dbus_stat (&file_path, &stat_buf, NULL))
459 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
460 _dbus_string_get_const_data (&file_path));
462 _dbus_hash_table_remove_string (activation->entries, entry->name);
463 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
471 if (stat_buf.mtime > entry->mtime)
473 BusDesktopFile *desktop_file;
476 dbus_error_init (&tmp_error);
478 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
479 if (desktop_file == NULL)
481 _dbus_verbose ("Could not load %s: %s\n",
482 _dbus_string_get_const_data (&file_path),
484 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
486 dbus_move_error (&tmp_error, error);
490 dbus_error_free (&tmp_error);
495 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
496 * Handle these both better
498 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
500 bus_desktop_file_free (desktop_file);
501 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
503 dbus_move_error (&tmp_error, error);
507 dbus_error_free (&tmp_error);
512 bus_desktop_file_free (desktop_file);
518 _dbus_string_free (&file_path);
520 if (updated_entry != NULL)
521 *updated_entry = tmp_entry;
526 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
527 * hash entries it already added.
530 update_directory (BusActivation *activation,
531 BusServiceDirectory *s_dir,
535 DBusString dir, filename;
536 BusDesktopFile *desktop_file;
539 BusActivationEntry *entry;
540 DBusString full_path;
542 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
547 _dbus_string_init_const (&dir, s_dir->dir_c);
549 if (!_dbus_string_init (&filename))
555 if (!_dbus_string_init (&full_path))
558 _dbus_string_free (&filename);
564 /* from this point it's safe to "goto out" */
566 iter = _dbus_directory_open (&dir, error);
569 _dbus_verbose ("Failed to open directory %s: %s\n",
571 error ? error->message : "unknown");
575 /* Now read the files */
576 dbus_error_init (&tmp_error);
577 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
579 _dbus_assert (!dbus_error_is_set (&tmp_error));
581 _dbus_string_set_length (&full_path, 0);
583 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
585 _dbus_verbose ("Skipping non-.service file %s\n",
586 _dbus_string_get_const_data (&filename));
590 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
591 if (entry) /* Already has this service file in the cache */
593 if (!check_service_file (activation, entry, NULL, error))
599 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
600 !_dbus_concat_dir_and_file (&full_path, &filename))
607 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
608 if (desktop_file == NULL)
610 _dbus_verbose ("Could not load %s: %s\n",
611 _dbus_string_get_const_data (&full_path),
614 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
616 dbus_move_error (&tmp_error, error);
620 dbus_error_free (&tmp_error);
624 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
625 * Handle these both better
627 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
629 bus_desktop_file_free (desktop_file);
632 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
633 _dbus_string_get_const_data (&full_path), tmp_error.message);
635 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
637 dbus_move_error (&tmp_error, error);
641 dbus_error_free (&tmp_error);
646 bus_desktop_file_free (desktop_file);
652 if (dbus_error_is_set (&tmp_error))
654 dbus_move_error (&tmp_error, error);
662 _DBUS_ASSERT_ERROR_IS_SET (error);
664 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
667 _dbus_directory_close (iter);
668 _dbus_string_free (&filename);
669 _dbus_string_free (&full_path);
675 bus_activation_new (BusContext *context,
676 const DBusString *address,
677 DBusList **directories,
680 BusActivation *activation;
684 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
686 activation = dbus_new0 (BusActivation, 1);
687 if (activation == NULL)
693 activation->refcount = 1;
694 activation->context = context;
695 activation->n_pending_activations = 0;
697 if (!_dbus_string_copy_data (address, &activation->server_address))
703 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
704 (DBusFreeFunction)bus_activation_entry_unref);
705 if (activation->entries == NULL)
711 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
712 (DBusFreeFunction)bus_pending_activation_unref);
714 if (activation->pending_activations == NULL)
720 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
721 (DBusFreeFunction)bus_service_directory_unref);
723 if (activation->directories == NULL)
729 /* Load service files */
730 link = _dbus_list_get_first_link (directories);
733 BusServiceDirectory *s_dir;
735 dir = _dbus_strdup ((const char *) link->data);
742 s_dir = dbus_new0 (BusServiceDirectory, 1);
753 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
754 (DBusFreeFunction)bus_activation_entry_unref);
758 bus_service_directory_unref (s_dir);
763 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
765 bus_service_directory_unref (s_dir);
770 /* only fail on OOM, it is ok if we can't read the directory */
771 if (!update_directory (activation, s_dir, error))
773 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
776 dbus_error_free (error);
779 link = _dbus_list_get_next_link (directories, link);
785 bus_activation_unref (activation);
790 bus_activation_ref (BusActivation *activation)
792 _dbus_assert (activation->refcount > 0);
794 activation->refcount += 1;
800 bus_activation_unref (BusActivation *activation)
802 _dbus_assert (activation->refcount > 0);
804 activation->refcount -= 1;
806 if (activation->refcount > 0)
809 dbus_free (activation->server_address);
810 if (activation->entries)
811 _dbus_hash_table_unref (activation->entries);
812 if (activation->pending_activations)
813 _dbus_hash_table_unref (activation->pending_activations);
814 if (activation->directories)
815 _dbus_hash_table_unref (activation->directories);
817 dbus_free (activation);
821 child_setup (void *data)
823 BusActivation *activation = data;
826 /* If no memory, we simply have the child exit, so it won't try
827 * to connect to the wrong thing.
829 if (!_dbus_setenv ("DBUS_STARTER_ADDRESS", activation->server_address))
832 type = bus_context_get_type (activation->context);
835 if (!_dbus_setenv ("DBUS_STARTER_BUS_TYPE", type))
838 if (strcmp (type, "session") == 0)
840 if (!_dbus_setenv ("DBUS_SESSION_BUS_ADDRESS",
841 activation->server_address))
844 else if (strcmp (type, "system") == 0)
846 if (!_dbus_setenv ("DBUS_SYSTEM_BUS_ADDRESS",
847 activation->server_address))
855 BusPendingActivation *pending_activation;
856 DBusPreallocatedHash *hash_entry;
857 } RestorePendingData;
860 restore_pending (void *data)
862 RestorePendingData *d = data;
864 _dbus_assert (d->pending_activation != NULL);
865 _dbus_assert (d->hash_entry != NULL);
867 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
868 d->pending_activation->service_name,
869 d->pending_activation->timeout_added);
871 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
873 d->pending_activation->service_name, d->pending_activation);
875 bus_pending_activation_ref (d->pending_activation);
877 d->hash_entry = NULL;
881 free_pending_restore_data (void *data)
883 RestorePendingData *d = data;
886 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
889 bus_pending_activation_unref (d->pending_activation);
895 add_restore_pending_to_transaction (BusTransaction *transaction,
896 BusPendingActivation *pending_activation)
898 RestorePendingData *d;
900 d = dbus_new (RestorePendingData, 1);
904 d->pending_activation = pending_activation;
905 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
907 bus_pending_activation_ref (d->pending_activation);
909 if (d->hash_entry == NULL ||
910 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
911 free_pending_restore_data))
913 free_pending_restore_data (d);
917 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
923 bus_activation_service_created (BusActivation *activation,
924 const char *service_name,
925 BusTransaction *transaction,
928 BusPendingActivation *pending_activation;
929 DBusMessage *message;
932 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
934 /* Check if it's a pending activation */
935 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
937 if (!pending_activation)
940 link = _dbus_list_get_first_link (&pending_activation->entries);
943 BusPendingActivationEntry *entry = link->data;
944 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
946 if (dbus_connection_get_is_connected (entry->connection))
948 /* Only send activation replies to regular activation requests. */
949 if (!entry->auto_activation)
951 dbus_uint32_t result;
953 message = dbus_message_new_method_return (entry->activation_message);
960 result = DBUS_START_REPLY_SUCCESS;
962 if (!dbus_message_append_args (message,
963 DBUS_TYPE_UINT32, &result,
966 dbus_message_unref (message);
971 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
973 dbus_message_unref (message);
978 dbus_message_unref (message);
992 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
994 BusTransaction *transaction,
997 BusPendingActivation *pending_activation;
1000 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1002 /* Check if it's a pending activation */
1003 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1004 bus_service_get_name (service));
1006 if (!pending_activation)
1009 link = _dbus_list_get_first_link (&pending_activation->entries);
1010 while (link != NULL)
1012 BusPendingActivationEntry *entry = link->data;
1013 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1015 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1017 DBusConnection *addressed_recipient;
1019 addressed_recipient = bus_service_get_primary_owners_connection (service);
1021 /* Check the security policy, which has the side-effect of adding an
1022 * expected pending reply.
1024 if (!bus_context_check_security_policy (activation->context, transaction,
1026 addressed_recipient,
1027 addressed_recipient,
1028 entry->activation_message, error))
1031 if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
1033 BUS_SET_OOM (error);
1041 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1043 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1044 BUS_SET_OOM (error);
1048 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1057 * FIXME @todo the error messages here would ideally be preallocated
1058 * so we don't need to allocate memory to send them.
1059 * Using the usual tactic, prealloc an OOM message, then
1060 * if we can't alloc the real error send the OOM error instead.
1063 try_send_activation_failure (BusPendingActivation *pending_activation,
1064 const DBusError *how)
1066 BusActivation *activation;
1068 BusTransaction *transaction;
1070 activation = pending_activation->activation;
1072 transaction = bus_transaction_new (activation->context);
1073 if (transaction == NULL)
1076 link = _dbus_list_get_first_link (&pending_activation->entries);
1077 while (link != NULL)
1079 BusPendingActivationEntry *entry = link->data;
1080 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1082 if (dbus_connection_get_is_connected (entry->connection))
1084 if (!bus_transaction_send_error_reply (transaction,
1087 entry->activation_message))
1094 bus_transaction_execute_and_free (transaction);
1100 bus_transaction_cancel_and_free (transaction);
1105 * Free the pending activation and send an error message to all the
1106 * connections that were waiting for it.
1109 pending_activation_failed (BusPendingActivation *pending_activation,
1110 const DBusError *how)
1112 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1113 while (!try_send_activation_failure (pending_activation, how))
1114 _dbus_wait_for_memory ();
1116 /* Destroy this pending activation */
1117 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1118 pending_activation->service_name);
1122 babysitter_watch_callback (DBusWatch *watch,
1123 unsigned int condition,
1126 BusPendingActivation *pending_activation = data;
1128 DBusBabysitter *babysitter;
1130 babysitter = pending_activation->babysitter;
1132 _dbus_babysitter_ref (babysitter);
1134 retval = dbus_watch_handle (watch, condition);
1136 /* FIXME this is broken in the same way that
1137 * connection watches used to be; there should be
1138 * a separate callback for status change, instead
1139 * of doing "if we handled a watch status might
1142 * Fixing this lets us move dbus_watch_handle
1143 * calls into dbus-mainloop.c
1146 if (_dbus_babysitter_get_child_exited (babysitter))
1151 dbus_error_init (&error);
1152 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1154 /* Destroy all pending activations with the same exec */
1155 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1157 while (_dbus_hash_iter_next (&iter))
1159 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1161 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1162 pending_activation_failed (p, &error);
1165 /* Destroys the pending activation */
1166 pending_activation_failed (pending_activation, &error);
1168 dbus_error_free (&error);
1171 _dbus_babysitter_unref (babysitter);
1177 add_babysitter_watch (DBusWatch *watch,
1180 BusPendingActivation *pending_activation = data;
1182 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1183 watch, babysitter_watch_callback, pending_activation,
1188 remove_babysitter_watch (DBusWatch *watch,
1191 BusPendingActivation *pending_activation = data;
1193 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1194 watch, babysitter_watch_callback, pending_activation);
1198 pending_activation_timed_out (void *data)
1200 BusPendingActivation *pending_activation = data;
1203 /* Kill the spawned process, since it sucks
1204 * (not sure this is what we want to do, but
1205 * may as well try it for now)
1207 if (pending_activation->babysitter)
1208 _dbus_babysitter_kill_child (pending_activation->babysitter);
1210 dbus_error_init (&error);
1212 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1213 "Activation of %s timed out",
1214 pending_activation->service_name);
1216 pending_activation_failed (pending_activation, &error);
1218 dbus_error_free (&error);
1224 cancel_pending (void *data)
1226 BusPendingActivation *pending_activation = data;
1228 _dbus_verbose ("Canceling pending activation of %s\n",
1229 pending_activation->service_name);
1231 if (pending_activation->babysitter)
1232 _dbus_babysitter_kill_child (pending_activation->babysitter);
1234 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1235 pending_activation->service_name);
1239 free_pending_cancel_data (void *data)
1241 BusPendingActivation *pending_activation = data;
1243 bus_pending_activation_unref (pending_activation);
1247 add_cancel_pending_to_transaction (BusTransaction *transaction,
1248 BusPendingActivation *pending_activation)
1250 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1252 free_pending_cancel_data))
1255 bus_pending_activation_ref (pending_activation);
1257 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1263 update_service_cache (BusActivation *activation, DBusError *error)
1267 _dbus_hash_iter_init (activation->directories, &iter);
1268 while (_dbus_hash_iter_next (&iter))
1270 DBusError tmp_error;
1271 BusServiceDirectory *s_dir;
1273 s_dir = _dbus_hash_iter_get_value (&iter);
1275 dbus_error_init (&tmp_error);
1276 if (!update_directory (activation, s_dir, &tmp_error))
1278 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1280 dbus_move_error (&tmp_error, error);
1284 dbus_error_free (&tmp_error);
1292 static BusActivationEntry *
1293 activation_find_entry (BusActivation *activation,
1294 const char *service_name,
1297 BusActivationEntry *entry;
1299 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1302 if (!update_service_cache (activation, error))
1305 entry = _dbus_hash_table_lookup_string (activation->entries,
1310 BusActivationEntry *updated_entry;
1312 if (!check_service_file (activation, entry, &updated_entry, error))
1315 entry = updated_entry;
1320 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1321 "The name %s was not provided by any .service files",
1330 bus_activation_activate_service (BusActivation *activation,
1331 DBusConnection *connection,
1332 BusTransaction *transaction,
1333 dbus_bool_t auto_activation,
1334 DBusMessage *activation_message,
1335 const char *service_name,
1338 BusActivationEntry *entry;
1339 BusPendingActivation *pending_activation;
1340 BusPendingActivationEntry *pending_activation_entry;
1341 DBusMessage *message;
1342 DBusString service_str;
1348 dbus_bool_t activated;
1352 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1354 if (activation->n_pending_activations >=
1355 bus_context_get_max_pending_activations (activation->context))
1357 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1358 "The maximum number of pending activations has been reached, activation of %s failed",
1363 entry = activation_find_entry (activation, service_name, error);
1367 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1368 * call us if the service is already active.
1370 if (!auto_activation)
1372 /* Check if the service is active */
1373 _dbus_string_init_const (&service_str, service_name);
1374 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1376 dbus_uint32_t result;
1378 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1380 message = dbus_message_new_method_return (activation_message);
1384 _dbus_verbose ("No memory to create reply to activate message\n");
1385 BUS_SET_OOM (error);
1389 result = DBUS_START_REPLY_ALREADY_RUNNING;
1391 if (!dbus_message_append_args (message,
1392 DBUS_TYPE_UINT32, &result,
1395 _dbus_verbose ("No memory to set args of reply to activate message\n");
1396 BUS_SET_OOM (error);
1397 dbus_message_unref (message);
1401 retval = bus_transaction_send_from_driver (transaction, connection, message);
1402 dbus_message_unref (message);
1405 _dbus_verbose ("Failed to send reply\n");
1406 BUS_SET_OOM (error);
1413 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1414 if (!pending_activation_entry)
1416 _dbus_verbose ("Failed to create pending activation entry\n");
1417 BUS_SET_OOM (error);
1421 pending_activation_entry->auto_activation = auto_activation;
1423 pending_activation_entry->activation_message = activation_message;
1424 dbus_message_ref (activation_message);
1425 pending_activation_entry->connection = connection;
1426 dbus_connection_ref (connection);
1428 /* Check if the service is being activated */
1429 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1430 if (pending_activation)
1432 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1434 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1436 BUS_SET_OOM (error);
1437 bus_pending_activation_entry_free (pending_activation_entry);
1441 pending_activation->n_entries += 1;
1442 pending_activation->activation->n_pending_activations += 1;
1446 pending_activation = dbus_new0 (BusPendingActivation, 1);
1447 if (!pending_activation)
1449 _dbus_verbose ("Failed to create pending activation\n");
1451 BUS_SET_OOM (error);
1452 bus_pending_activation_entry_free (pending_activation_entry);
1456 pending_activation->activation = activation;
1457 pending_activation->refcount = 1;
1459 pending_activation->service_name = _dbus_strdup (service_name);
1460 if (!pending_activation->service_name)
1462 _dbus_verbose ("Failed to copy service name for pending activation\n");
1464 BUS_SET_OOM (error);
1465 bus_pending_activation_unref (pending_activation);
1466 bus_pending_activation_entry_free (pending_activation_entry);
1470 pending_activation->exec = _dbus_strdup (entry->exec);
1471 if (!pending_activation->exec)
1473 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1474 BUS_SET_OOM (error);
1475 bus_pending_activation_unref (pending_activation);
1476 bus_pending_activation_entry_free (pending_activation_entry);
1480 pending_activation->timeout =
1481 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1482 pending_activation_timed_out,
1485 if (!pending_activation->timeout)
1487 _dbus_verbose ("Failed to create timeout for pending activation\n");
1489 BUS_SET_OOM (error);
1490 bus_pending_activation_unref (pending_activation);
1491 bus_pending_activation_entry_free (pending_activation_entry);
1495 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1496 pending_activation->timeout,
1497 handle_timeout_callback,
1501 _dbus_verbose ("Failed to add timeout for pending activation\n");
1503 BUS_SET_OOM (error);
1504 bus_pending_activation_unref (pending_activation);
1505 bus_pending_activation_entry_free (pending_activation_entry);
1509 pending_activation->timeout_added = TRUE;
1511 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1513 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1515 BUS_SET_OOM (error);
1516 bus_pending_activation_unref (pending_activation);
1517 bus_pending_activation_entry_free (pending_activation_entry);
1521 pending_activation->n_entries += 1;
1522 pending_activation->activation->n_pending_activations += 1;
1525 _dbus_hash_iter_init (activation->pending_activations, &iter);
1526 while (_dbus_hash_iter_next (&iter))
1528 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1530 if (strcmp (p->exec, entry->exec) == 0)
1537 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1538 pending_activation->service_name,
1539 pending_activation))
1541 _dbus_verbose ("Failed to put pending activation in hash table\n");
1543 BUS_SET_OOM (error);
1544 bus_pending_activation_unref (pending_activation);
1549 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1551 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1552 BUS_SET_OOM (error);
1553 _dbus_hash_table_remove_string (activation->pending_activations,
1554 pending_activation->service_name);
1562 /* Now try to spawn the process */
1563 if (!_dbus_shell_parse_argv (entry->exec, &argc, &argv, error))
1565 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
1566 _DBUS_ASSERT_ERROR_IS_SET (error);
1568 _dbus_hash_table_remove_string (activation->pending_activations,
1569 pending_activation->service_name);
1574 _dbus_verbose ("Spawning %s ...\n", argv[0]);
1575 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1577 child_setup, activation,
1580 _dbus_verbose ("Failed to spawn child\n");
1581 _DBUS_ASSERT_ERROR_IS_SET (error);
1582 dbus_free_string_array (argv);
1587 dbus_free_string_array (argv);
1589 _dbus_assert (pending_activation->babysitter != NULL);
1591 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1592 add_babysitter_watch,
1593 remove_babysitter_watch,
1598 BUS_SET_OOM (error);
1599 _dbus_verbose ("Failed to set babysitter watch functions\n");
1607 bus_activation_list_services (BusActivation *activation,
1615 len = _dbus_hash_table_get_n_entries (activation->entries);
1616 retval = dbus_new (char *, len + 1);
1621 _dbus_hash_iter_init (activation->entries, &iter);
1623 while (_dbus_hash_iter_next (&iter))
1625 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
1627 retval[i] = _dbus_strdup (entry->name);
1628 if (retval[i] == NULL)
1643 for (j = 0; j < i; j++)
1644 dbus_free (retval[i]);
1651 #ifdef DBUS_BUILD_TESTS
1655 #define SERVICE_NAME_1 "MyService1"
1656 #define SERVICE_NAME_2 "MyService2"
1657 #define SERVICE_NAME_3 "MyService3"
1659 #define SERVICE_FILE_1 "service-1.service"
1660 #define SERVICE_FILE_2 "service-2.service"
1661 #define SERVICE_FILE_3 "service-3.service"
1664 test_create_service_file (DBusString *dir,
1665 const char *filename,
1669 DBusString file_name, full_path;
1671 dbus_bool_t ret_val;
1674 _dbus_string_init_const (&file_name, filename);
1676 if (!_dbus_string_init (&full_path))
1679 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1680 !_dbus_concat_dir_and_file (&full_path, &file_name))
1686 file = fopen (_dbus_string_get_const_data (&full_path), "w");
1693 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
1697 _dbus_string_free (&full_path);
1702 test_remove_service_file (DBusString *dir, const char *filename)
1704 DBusString file_name, full_path;
1705 dbus_bool_t ret_val;
1709 _dbus_string_init_const (&file_name, filename);
1711 if (!_dbus_string_init (&full_path))
1714 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1715 !_dbus_concat_dir_and_file (&full_path, &file_name))
1721 if (!_dbus_delete_file (&full_path, NULL))
1728 _dbus_string_free (&full_path);
1733 test_remove_directory (DBusString *dir)
1736 DBusString filename, full_path;
1737 dbus_bool_t ret_val;
1741 if (!_dbus_string_init (&filename))
1744 if (!_dbus_string_init (&full_path))
1746 _dbus_string_free (&filename);
1750 iter = _dbus_directory_open (dir, NULL);
1757 while (_dbus_directory_get_next_file (iter, &filename, NULL))
1759 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
1765 _dbus_directory_close (iter);
1767 if (!_dbus_delete_directory (dir, NULL))
1774 _dbus_string_free (&filename);
1775 _dbus_string_free (&full_path);
1781 init_service_reload_test (DBusString *dir)
1785 if (!_dbus_stat (dir, &stat_buf, NULL))
1787 if (!_dbus_create_directory (dir, NULL))
1792 if (!test_remove_directory (dir))
1795 if (!_dbus_create_directory (dir, NULL))
1799 /* Create one initial file */
1800 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
1807 cleanup_service_reload_test (DBusString *dir)
1809 if (!test_remove_directory (dir))
1817 BusActivation *activation;
1818 const char *service_name;
1819 dbus_bool_t expecting_find;
1823 check_func (void *data)
1826 BusActivationEntry *entry;
1828 dbus_bool_t ret_val;
1833 dbus_error_init (&error);
1835 entry = activation_find_entry (d->activation, d->service_name, &error);
1838 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1844 if (d->expecting_find)
1848 dbus_error_free (&error);
1852 if (!d->expecting_find)
1860 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
1865 err = !_dbus_test_oom_handling (description, check_func, data);
1867 err = !check_func (data);
1870 _dbus_assert_not_reached ("Test failed");
1876 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
1878 BusActivation *activation;
1880 DBusList *directories;
1884 _dbus_string_init_const (&address, "");
1886 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
1889 activation = bus_activation_new (NULL, &address, &directories, NULL);
1893 d.activation = activation;
1895 /* Check for existing service file */
1896 d.expecting_find = TRUE;
1897 d.service_name = SERVICE_NAME_1;
1899 if (!do_test ("Existing service file", oom_test, &d))
1902 /* Check for non-existing service file */
1903 d.expecting_find = FALSE;
1904 d.service_name = SERVICE_NAME_3;
1906 if (!do_test ("Nonexisting service file", oom_test, &d))
1909 /* Check for added service file */
1910 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
1913 d.expecting_find = TRUE;
1914 d.service_name = SERVICE_NAME_2;
1916 if (!do_test ("Added service file", oom_test, &d))
1919 /* Check for removed service file */
1920 if (!test_remove_service_file (dir, SERVICE_FILE_2))
1923 d.expecting_find = FALSE;
1924 d.service_name = SERVICE_FILE_2;
1926 if (!do_test ("Removed service file", oom_test, &d))
1929 /* Check for updated service file */
1931 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
1933 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
1936 d.expecting_find = TRUE;
1937 d.service_name = SERVICE_NAME_3;
1939 if (!do_test ("Updated service file, part 1", oom_test, &d))
1942 d.expecting_find = FALSE;
1943 d.service_name = SERVICE_NAME_1;
1945 if (!do_test ("Updated service file, part 2", oom_test, &d))
1948 bus_activation_unref (activation);
1949 _dbus_list_clear (&directories);
1955 bus_activation_service_reload_test (const DBusString *test_data_dir)
1957 DBusString directory;
1959 if (!_dbus_string_init (&directory))
1962 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
1965 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
1966 !_dbus_generate_random_ascii (&directory, 6))
1971 /* Do normal tests */
1972 if (!init_service_reload_test (&directory))
1973 _dbus_assert_not_reached ("could not initiate service reload test");
1975 if (!do_service_reload_test (&directory, FALSE))
1979 if (!init_service_reload_test (&directory))
1980 _dbus_assert_not_reached ("could not initiate service reload test");
1982 if (!do_service_reload_test (&directory, TRUE))
1985 /* Cleanup test directory */
1986 if (!cleanup_service_reload_test (&directory))
1989 _dbus_string_free (&directory);
1994 #endif /* DBUS_BUILD_TESTS */