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 * Depending on the exit code of the helper, set the error accordingly
1125 handle_activation_exit_error (int exit_code,
1130 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1131 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1132 "Launcher could not run (out of memory)");
1134 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1135 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1136 "Failed to setup environment correctly");
1138 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1139 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1140 "Bus name is not valid or missing");
1142 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1143 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1144 "Bus name not found in system service directory");
1146 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1147 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1148 "The permission of the setuid helper is not correct");
1150 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1151 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1152 "The service file is incorrect or does not have all required attributes");
1154 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1155 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1156 "Cannot launch daemon, file not found or permissions invalid");
1158 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1159 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1160 "Invalid arguments to command line");
1162 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1163 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1164 "Launched child was signaled, it probably crashed");
1167 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1168 "Launch helper exited with unknown return code %i", exit_code);
1174 babysitter_watch_callback (DBusWatch *watch,
1175 unsigned int condition,
1178 BusPendingActivation *pending_activation = data;
1180 DBusBabysitter *babysitter;
1182 babysitter = pending_activation->babysitter;
1184 _dbus_babysitter_ref (babysitter);
1186 retval = dbus_watch_handle (watch, condition);
1188 /* FIXME this is broken in the same way that
1189 * connection watches used to be; there should be
1190 * a separate callback for status change, instead
1191 * of doing "if we handled a watch status might
1194 * Fixing this lets us move dbus_watch_handle
1195 * calls into dbus-mainloop.c
1198 if (_dbus_babysitter_get_child_exited (babysitter))
1203 dbus_error_init (&error);
1204 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1206 /* refine the error code if we got an exit code */
1207 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED))
1210 if (_dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1212 dbus_error_free (&error);
1213 handle_activation_exit_error (exit_code, &error);
1217 /* Destroy all pending activations with the same exec */
1218 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1220 while (_dbus_hash_iter_next (&iter))
1222 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1224 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1225 pending_activation_failed (p, &error);
1228 /* Destroys the pending activation */
1229 pending_activation_failed (pending_activation, &error);
1231 dbus_error_free (&error);
1234 _dbus_babysitter_unref (babysitter);
1240 add_babysitter_watch (DBusWatch *watch,
1243 BusPendingActivation *pending_activation = data;
1245 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1246 watch, babysitter_watch_callback, pending_activation,
1251 remove_babysitter_watch (DBusWatch *watch,
1254 BusPendingActivation *pending_activation = data;
1256 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1257 watch, babysitter_watch_callback, pending_activation);
1261 pending_activation_timed_out (void *data)
1263 BusPendingActivation *pending_activation = data;
1266 /* Kill the spawned process, since it sucks
1267 * (not sure this is what we want to do, but
1268 * may as well try it for now)
1270 if (pending_activation->babysitter)
1271 _dbus_babysitter_kill_child (pending_activation->babysitter);
1273 dbus_error_init (&error);
1275 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1276 "Activation of %s timed out",
1277 pending_activation->service_name);
1279 pending_activation_failed (pending_activation, &error);
1281 dbus_error_free (&error);
1287 cancel_pending (void *data)
1289 BusPendingActivation *pending_activation = data;
1291 _dbus_verbose ("Canceling pending activation of %s\n",
1292 pending_activation->service_name);
1294 if (pending_activation->babysitter)
1295 _dbus_babysitter_kill_child (pending_activation->babysitter);
1297 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1298 pending_activation->service_name);
1302 free_pending_cancel_data (void *data)
1304 BusPendingActivation *pending_activation = data;
1306 bus_pending_activation_unref (pending_activation);
1310 add_cancel_pending_to_transaction (BusTransaction *transaction,
1311 BusPendingActivation *pending_activation)
1313 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1315 free_pending_cancel_data))
1318 bus_pending_activation_ref (pending_activation);
1320 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1326 update_service_cache (BusActivation *activation, DBusError *error)
1330 _dbus_hash_iter_init (activation->directories, &iter);
1331 while (_dbus_hash_iter_next (&iter))
1333 DBusError tmp_error;
1334 BusServiceDirectory *s_dir;
1336 s_dir = _dbus_hash_iter_get_value (&iter);
1338 dbus_error_init (&tmp_error);
1339 if (!update_directory (activation, s_dir, &tmp_error))
1341 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1343 dbus_move_error (&tmp_error, error);
1347 dbus_error_free (&tmp_error);
1355 static BusActivationEntry *
1356 activation_find_entry (BusActivation *activation,
1357 const char *service_name,
1360 BusActivationEntry *entry;
1362 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1365 if (!update_service_cache (activation, error))
1368 entry = _dbus_hash_table_lookup_string (activation->entries,
1373 BusActivationEntry *updated_entry;
1375 if (!check_service_file (activation, entry, &updated_entry, error))
1378 entry = updated_entry;
1383 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1384 "The name %s was not provided by any .service files",
1393 bus_activation_activate_service (BusActivation *activation,
1394 DBusConnection *connection,
1395 BusTransaction *transaction,
1396 dbus_bool_t auto_activation,
1397 DBusMessage *activation_message,
1398 const char *service_name,
1401 BusActivationEntry *entry;
1402 BusPendingActivation *pending_activation;
1403 BusPendingActivationEntry *pending_activation_entry;
1404 DBusMessage *message;
1405 DBusString service_str;
1406 const char *servicehelper;
1412 dbus_bool_t activated;
1417 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1419 if (activation->n_pending_activations >=
1420 bus_context_get_max_pending_activations (activation->context))
1422 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1423 "The maximum number of pending activations has been reached, activation of %s failed",
1428 entry = activation_find_entry (activation, service_name, error);
1432 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1433 * call us if the service is already active.
1435 if (!auto_activation)
1437 /* Check if the service is active */
1438 _dbus_string_init_const (&service_str, service_name);
1439 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1441 dbus_uint32_t result;
1443 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1445 message = dbus_message_new_method_return (activation_message);
1449 _dbus_verbose ("No memory to create reply to activate message\n");
1450 BUS_SET_OOM (error);
1454 result = DBUS_START_REPLY_ALREADY_RUNNING;
1456 if (!dbus_message_append_args (message,
1457 DBUS_TYPE_UINT32, &result,
1460 _dbus_verbose ("No memory to set args of reply to activate message\n");
1461 BUS_SET_OOM (error);
1462 dbus_message_unref (message);
1466 retval = bus_transaction_send_from_driver (transaction, connection, message);
1467 dbus_message_unref (message);
1470 _dbus_verbose ("Failed to send reply\n");
1471 BUS_SET_OOM (error);
1478 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1479 if (!pending_activation_entry)
1481 _dbus_verbose ("Failed to create pending activation entry\n");
1482 BUS_SET_OOM (error);
1486 pending_activation_entry->auto_activation = auto_activation;
1488 pending_activation_entry->activation_message = activation_message;
1489 dbus_message_ref (activation_message);
1490 pending_activation_entry->connection = connection;
1491 dbus_connection_ref (connection);
1493 /* Check if the service is being activated */
1494 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1495 if (pending_activation)
1497 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1499 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1501 BUS_SET_OOM (error);
1502 bus_pending_activation_entry_free (pending_activation_entry);
1506 pending_activation->n_entries += 1;
1507 pending_activation->activation->n_pending_activations += 1;
1511 pending_activation = dbus_new0 (BusPendingActivation, 1);
1512 if (!pending_activation)
1514 _dbus_verbose ("Failed to create pending activation\n");
1516 BUS_SET_OOM (error);
1517 bus_pending_activation_entry_free (pending_activation_entry);
1521 pending_activation->activation = activation;
1522 pending_activation->refcount = 1;
1524 pending_activation->service_name = _dbus_strdup (service_name);
1525 if (!pending_activation->service_name)
1527 _dbus_verbose ("Failed to copy service name for pending activation\n");
1529 BUS_SET_OOM (error);
1530 bus_pending_activation_unref (pending_activation);
1531 bus_pending_activation_entry_free (pending_activation_entry);
1535 pending_activation->exec = _dbus_strdup (entry->exec);
1536 if (!pending_activation->exec)
1538 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1539 BUS_SET_OOM (error);
1540 bus_pending_activation_unref (pending_activation);
1541 bus_pending_activation_entry_free (pending_activation_entry);
1545 pending_activation->timeout =
1546 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1547 pending_activation_timed_out,
1550 if (!pending_activation->timeout)
1552 _dbus_verbose ("Failed to create timeout for pending activation\n");
1554 BUS_SET_OOM (error);
1555 bus_pending_activation_unref (pending_activation);
1556 bus_pending_activation_entry_free (pending_activation_entry);
1560 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1561 pending_activation->timeout,
1562 handle_timeout_callback,
1566 _dbus_verbose ("Failed to add timeout for pending activation\n");
1568 BUS_SET_OOM (error);
1569 bus_pending_activation_unref (pending_activation);
1570 bus_pending_activation_entry_free (pending_activation_entry);
1574 pending_activation->timeout_added = TRUE;
1576 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1578 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1580 BUS_SET_OOM (error);
1581 bus_pending_activation_unref (pending_activation);
1582 bus_pending_activation_entry_free (pending_activation_entry);
1586 pending_activation->n_entries += 1;
1587 pending_activation->activation->n_pending_activations += 1;
1590 _dbus_hash_iter_init (activation->pending_activations, &iter);
1591 while (_dbus_hash_iter_next (&iter))
1593 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1595 if (strcmp (p->exec, entry->exec) == 0)
1602 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1603 pending_activation->service_name,
1604 pending_activation))
1606 _dbus_verbose ("Failed to put pending activation in hash table\n");
1608 BUS_SET_OOM (error);
1609 bus_pending_activation_unref (pending_activation);
1614 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1616 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1617 BUS_SET_OOM (error);
1618 _dbus_hash_table_remove_string (activation->pending_activations,
1619 pending_activation->service_name);
1627 /* use command as system and session different */
1628 if (!_dbus_string_init (&command))
1630 BUS_SET_OOM (error);
1634 /* does the bus use a helper? */
1635 servicehelper = bus_context_get_servicehelper (activation->context);
1636 if (servicehelper != NULL)
1638 if (entry->user == NULL)
1640 _dbus_string_free (&command);
1641 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
1642 "Cannot do system-bus activation with no user\n");
1646 /* join the helper path and the service name */
1647 if (!_dbus_string_append (&command, servicehelper))
1649 _dbus_string_free (&command);
1650 BUS_SET_OOM (error);
1653 if (!_dbus_string_append (&command, " "))
1655 _dbus_string_free (&command);
1656 BUS_SET_OOM (error);
1659 if (!_dbus_string_append (&command, service_name))
1661 _dbus_string_free (&command);
1662 BUS_SET_OOM (error);
1668 /* the bus does not use a helper, so we can append arguments with the exec line */
1669 if (!_dbus_string_append (&command, entry->exec))
1671 _dbus_string_free (&command);
1672 BUS_SET_OOM (error);
1677 /* convert command into arguments */
1678 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
1680 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
1681 _DBUS_ASSERT_ERROR_IS_SET (error);
1683 _dbus_hash_table_remove_string (activation->pending_activations,
1684 pending_activation->service_name);
1686 _dbus_string_free (&command);
1689 _dbus_string_free (&command);
1691 _dbus_verbose ("Spawning %s ...\n", argv[0]);
1692 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1694 child_setup, activation,
1697 _dbus_verbose ("Failed to spawn child\n");
1698 _DBUS_ASSERT_ERROR_IS_SET (error);
1699 dbus_free_string_array (argv);
1704 dbus_free_string_array (argv);
1706 _dbus_assert (pending_activation->babysitter != NULL);
1708 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1709 add_babysitter_watch,
1710 remove_babysitter_watch,
1715 BUS_SET_OOM (error);
1716 _dbus_verbose ("Failed to set babysitter watch functions\n");
1724 bus_activation_list_services (BusActivation *activation,
1732 len = _dbus_hash_table_get_n_entries (activation->entries);
1733 retval = dbus_new (char *, len + 1);
1738 _dbus_hash_iter_init (activation->entries, &iter);
1740 while (_dbus_hash_iter_next (&iter))
1742 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
1744 retval[i] = _dbus_strdup (entry->name);
1745 if (retval[i] == NULL)
1760 for (j = 0; j < i; j++)
1761 dbus_free (retval[i]);
1768 #ifdef DBUS_BUILD_TESTS
1772 #define SERVICE_NAME_1 "MyService1"
1773 #define SERVICE_NAME_2 "MyService2"
1774 #define SERVICE_NAME_3 "MyService3"
1776 #define SERVICE_FILE_1 "service-1.service"
1777 #define SERVICE_FILE_2 "service-2.service"
1778 #define SERVICE_FILE_3 "service-3.service"
1781 test_create_service_file (DBusString *dir,
1782 const char *filename,
1786 DBusString file_name, full_path;
1788 dbus_bool_t ret_val;
1791 _dbus_string_init_const (&file_name, filename);
1793 if (!_dbus_string_init (&full_path))
1796 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1797 !_dbus_concat_dir_and_file (&full_path, &file_name))
1803 file = fopen (_dbus_string_get_const_data (&full_path), "w");
1810 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
1814 _dbus_string_free (&full_path);
1819 test_remove_service_file (DBusString *dir, const char *filename)
1821 DBusString file_name, full_path;
1822 dbus_bool_t ret_val;
1826 _dbus_string_init_const (&file_name, filename);
1828 if (!_dbus_string_init (&full_path))
1831 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1832 !_dbus_concat_dir_and_file (&full_path, &file_name))
1838 if (!_dbus_delete_file (&full_path, NULL))
1845 _dbus_string_free (&full_path);
1850 test_remove_directory (DBusString *dir)
1853 DBusString filename, full_path;
1854 dbus_bool_t ret_val;
1858 if (!_dbus_string_init (&filename))
1861 if (!_dbus_string_init (&full_path))
1863 _dbus_string_free (&filename);
1867 iter = _dbus_directory_open (dir, NULL);
1874 while (_dbus_directory_get_next_file (iter, &filename, NULL))
1876 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
1882 _dbus_directory_close (iter);
1884 if (!_dbus_delete_directory (dir, NULL))
1891 _dbus_string_free (&filename);
1892 _dbus_string_free (&full_path);
1898 init_service_reload_test (DBusString *dir)
1902 if (!_dbus_stat (dir, &stat_buf, NULL))
1904 if (!_dbus_create_directory (dir, NULL))
1909 if (!test_remove_directory (dir))
1912 if (!_dbus_create_directory (dir, NULL))
1916 /* Create one initial file */
1917 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
1924 cleanup_service_reload_test (DBusString *dir)
1926 if (!test_remove_directory (dir))
1934 BusActivation *activation;
1935 const char *service_name;
1936 dbus_bool_t expecting_find;
1940 check_func (void *data)
1943 BusActivationEntry *entry;
1945 dbus_bool_t ret_val;
1950 dbus_error_init (&error);
1952 entry = activation_find_entry (d->activation, d->service_name, &error);
1955 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1961 if (d->expecting_find)
1965 dbus_error_free (&error);
1969 if (!d->expecting_find)
1977 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
1982 err = !_dbus_test_oom_handling (description, check_func, data);
1984 err = !check_func (data);
1987 _dbus_assert_not_reached ("Test failed");
1993 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
1995 BusActivation *activation;
1997 DBusList *directories;
2001 _dbus_string_init_const (&address, "");
2003 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2006 activation = bus_activation_new (NULL, &address, &directories, NULL);
2010 d.activation = activation;
2012 /* Check for existing service file */
2013 d.expecting_find = TRUE;
2014 d.service_name = SERVICE_NAME_1;
2016 if (!do_test ("Existing service file", oom_test, &d))
2019 /* Check for non-existing service file */
2020 d.expecting_find = FALSE;
2021 d.service_name = SERVICE_NAME_3;
2023 if (!do_test ("Nonexisting service file", oom_test, &d))
2026 /* Check for added service file */
2027 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2030 d.expecting_find = TRUE;
2031 d.service_name = SERVICE_NAME_2;
2033 if (!do_test ("Added service file", oom_test, &d))
2036 /* Check for removed service file */
2037 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2040 d.expecting_find = FALSE;
2041 d.service_name = SERVICE_FILE_2;
2043 if (!do_test ("Removed service file", oom_test, &d))
2046 /* Check for updated service file */
2048 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2050 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2053 d.expecting_find = TRUE;
2054 d.service_name = SERVICE_NAME_3;
2056 if (!do_test ("Updated service file, part 1", oom_test, &d))
2059 d.expecting_find = FALSE;
2060 d.service_name = SERVICE_NAME_1;
2062 if (!do_test ("Updated service file, part 2", oom_test, &d))
2065 bus_activation_unref (activation);
2066 _dbus_list_clear (&directories);
2072 bus_activation_service_reload_test (const DBusString *test_data_dir)
2074 DBusString directory;
2076 if (!_dbus_string_init (&directory))
2079 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2082 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2083 !_dbus_generate_random_ascii (&directory, 6))
2088 /* Do normal tests */
2089 if (!init_service_reload_test (&directory))
2090 _dbus_assert_not_reached ("could not initiate service reload test");
2092 if (!do_service_reload_test (&directory, FALSE))
2096 if (!init_service_reload_test (&directory))
2097 _dbus_assert_not_reached ("could not initiate service reload test");
2099 if (!do_service_reload_test (&directory, TRUE))
2102 /* Cleanup test directory */
2103 if (!cleanup_service_reload_test (&directory))
2106 _dbus_string_free (&directory);
2111 #endif /* DBUS_BUILD_TESTS */