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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "activation.h"
28 #include "activation-exit-codes.h"
29 #include "desktop-file.h"
34 #include <dbus/dbus-internals.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-list.h>
37 #include <dbus/dbus-shell.h>
38 #include <dbus/dbus-spawn.h>
39 #include <dbus/dbus-timeout.h>
40 #include <dbus/dbus-sysdeps.h>
48 DBusHashTable *entries;
49 DBusHashTable *pending_activations;
52 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
53 * i.e. number of pending activation requests, not pending
56 DBusHashTable *directories;
57 DBusHashTable *environment;
64 DBusHashTable *entries;
65 } BusServiceDirectory;
73 char *systemd_service;
75 BusServiceDirectory *s_dir;
79 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
81 struct BusPendingActivationEntry
83 DBusMessage *activation_message;
84 DBusConnection *connection;
86 dbus_bool_t auto_activation;
92 BusActivation *activation;
95 char *systemd_service;
98 DBusBabysitter *babysitter;
100 unsigned int timeout_added : 1;
101 } BusPendingActivation;
104 static BusServiceDirectory *
105 bus_service_directory_ref (BusServiceDirectory *dir)
107 _dbus_assert (dir->refcount);
116 bus_service_directory_unref (BusServiceDirectory *dir)
121 _dbus_assert (dir->refcount > 0);
124 if (dir->refcount > 0)
128 _dbus_hash_table_unref (dir->entries);
130 dbus_free (dir->dir_c);
135 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
137 if (entry->activation_message)
138 dbus_message_unref (entry->activation_message);
140 if (entry->connection)
141 dbus_connection_unref (entry->connection);
146 static BusPendingActivation *
147 bus_pending_activation_ref (BusPendingActivation *pending_activation)
149 _dbus_assert (pending_activation->refcount > 0);
150 pending_activation->refcount += 1;
152 return pending_activation;
156 bus_pending_activation_unref (BusPendingActivation *pending_activation)
160 if (pending_activation == NULL) /* hash table requires this */
163 _dbus_assert (pending_activation->refcount > 0);
164 pending_activation->refcount -= 1;
166 if (pending_activation->refcount > 0)
169 if (pending_activation->timeout_added)
171 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
172 pending_activation->timeout);
173 pending_activation->timeout_added = FALSE;
176 if (pending_activation->timeout)
177 _dbus_timeout_unref (pending_activation->timeout);
179 if (pending_activation->babysitter)
181 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
183 pending_activation->babysitter,
185 _dbus_assert_not_reached ("setting watch functions to NULL failed");
187 _dbus_babysitter_unref (pending_activation->babysitter);
190 dbus_free (pending_activation->service_name);
191 dbus_free (pending_activation->exec);
192 dbus_free (pending_activation->systemd_service);
194 link = _dbus_list_get_first_link (&pending_activation->entries);
198 BusPendingActivationEntry *entry = link->data;
200 bus_pending_activation_entry_free (entry);
202 link = _dbus_list_get_next_link (&pending_activation->entries, link);
204 _dbus_list_clear (&pending_activation->entries);
206 pending_activation->activation->n_pending_activations -=
207 pending_activation->n_entries;
209 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
211 dbus_free (pending_activation);
214 static BusActivationEntry *
215 bus_activation_entry_ref (BusActivationEntry *entry)
217 _dbus_assert (entry->refcount > 0);
224 bus_activation_entry_unref (BusActivationEntry *entry)
226 if (entry == NULL) /* hash table requires this */
229 _dbus_assert (entry->refcount > 0);
232 if (entry->refcount > 0)
235 dbus_free (entry->name);
236 dbus_free (entry->exec);
237 dbus_free (entry->user);
238 dbus_free (entry->filename);
239 dbus_free (entry->systemd_service);
245 update_desktop_file_entry (BusActivation *activation,
246 BusServiceDirectory *s_dir,
247 DBusString *filename,
248 BusDesktopFile *desktop_file,
251 char *name, *exec, *user, *exec_tmp, *systemd_service;
252 BusActivationEntry *entry;
254 DBusString file_path;
258 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
265 systemd_service = NULL;
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 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
304 dbus_free (exec_tmp);
307 /* user is not _required_ unless we are using system activation */
308 if (!bus_desktop_file_get_string (desktop_file,
309 DBUS_SERVICE_SECTION,
313 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
314 /* if we got OOM, then exit */
315 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
317 dbus_move_error (&tmp_error, error);
322 /* if we have error because we didn't find anything then continue */
323 dbus_error_free (&tmp_error);
328 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
330 /* systemd service is never required */
331 if (!bus_desktop_file_get_string (desktop_file,
332 DBUS_SERVICE_SECTION,
333 DBUS_SERVICE_SYSTEMD_SERVICE,
334 &systemd_service, &tmp_error))
336 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
337 /* if we got OOM, then exit */
338 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
340 dbus_move_error (&tmp_error, error);
345 /* if we have error because we didn't find anything then continue */
346 dbus_error_free (&tmp_error);
347 dbus_free (systemd_service);
348 systemd_service = NULL;
352 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
354 entry = _dbus_hash_table_lookup_string (s_dir->entries,
355 _dbus_string_get_const_data (filename));
357 if (entry == NULL) /* New file */
359 /* FIXME we need a better-defined algorithm for which service file to
360 * pick than "whichever one is first in the directory listing"
362 if (_dbus_hash_table_lookup_string (activation->entries, name))
364 dbus_set_error (error, DBUS_ERROR_FAILED,
365 "Service %s already exists in activation entry list\n", name);
369 entry = dbus_new0 (BusActivationEntry, 1);
379 entry->systemd_service = systemd_service;
382 /* ownership has been transferred to entry, do not free separately */
386 systemd_service = NULL;
388 entry->s_dir = s_dir;
389 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
390 if (!entry->filename)
396 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
402 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
404 /* Revert the insertion in the entries table */
405 _dbus_hash_table_remove_string (activation->entries, entry->name);
410 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
412 else /* Just update the entry */
414 bus_activation_entry_ref (entry);
415 _dbus_hash_table_remove_string (activation->entries, entry->name);
417 if (_dbus_hash_table_lookup_string (activation->entries, name))
419 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
420 name, _dbus_string_get_const_data (&file_path));
424 /* ownership has been transferred to entry, do not free separately */
425 dbus_free (entry->name);
429 dbus_free (entry->exec);
433 dbus_free (entry->user);
437 dbus_free (entry->systemd_service);
438 entry->systemd_service = systemd_service;
439 systemd_service = NULL;
441 if (!_dbus_hash_table_insert_string (activation->entries,
442 entry->name, bus_activation_entry_ref(entry)))
445 /* Also remove path to entries hash since we want this in sync with
446 * the entries hash table */
447 _dbus_hash_table_remove_string (entry->s_dir->entries,
449 bus_activation_entry_unref (entry);
454 entry->mtime = stat_buf.mtime;
458 /* if these have been transferred into entry, the variables will be NULL */
462 dbus_free (systemd_service);
463 _dbus_string_free (&file_path);
466 bus_activation_entry_unref (entry);
472 check_service_file (BusActivation *activation,
473 BusActivationEntry *entry,
474 BusActivationEntry **updated_entry,
479 BusActivationEntry *tmp_entry;
480 DBusString file_path;
486 _dbus_string_init_const (&filename, entry->filename);
488 if (!_dbus_string_init (&file_path))
494 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
495 !_dbus_concat_dir_and_file (&file_path, &filename))
502 if (!_dbus_stat (&file_path, &stat_buf, NULL))
504 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
505 _dbus_string_get_const_data (&file_path));
507 _dbus_hash_table_remove_string (activation->entries, entry->name);
508 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
516 if (stat_buf.mtime > entry->mtime)
518 BusDesktopFile *desktop_file;
521 dbus_error_init (&tmp_error);
523 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
524 if (desktop_file == NULL)
526 _dbus_verbose ("Could not load %s: %s\n",
527 _dbus_string_get_const_data (&file_path),
529 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
531 dbus_move_error (&tmp_error, error);
535 dbus_error_free (&tmp_error);
540 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
541 * Handle these both better
543 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
545 bus_desktop_file_free (desktop_file);
546 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
548 dbus_move_error (&tmp_error, error);
552 dbus_error_free (&tmp_error);
557 bus_desktop_file_free (desktop_file);
563 _dbus_string_free (&file_path);
565 if (updated_entry != NULL)
566 *updated_entry = tmp_entry;
571 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
572 * hash entries it already added.
575 update_directory (BusActivation *activation,
576 BusServiceDirectory *s_dir,
580 DBusString dir, filename;
581 BusDesktopFile *desktop_file;
584 BusActivationEntry *entry;
585 DBusString full_path;
587 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
592 _dbus_string_init_const (&dir, s_dir->dir_c);
594 if (!_dbus_string_init (&filename))
600 if (!_dbus_string_init (&full_path))
603 _dbus_string_free (&filename);
609 /* from this point it's safe to "goto out" */
611 iter = _dbus_directory_open (&dir, error);
614 _dbus_verbose ("Failed to open directory %s: %s\n",
616 error ? error->message : "unknown");
620 /* Now read the files */
621 dbus_error_init (&tmp_error);
622 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
624 _dbus_assert (!dbus_error_is_set (&tmp_error));
626 _dbus_string_set_length (&full_path, 0);
628 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
630 _dbus_verbose ("Skipping non-.service file %s\n",
631 _dbus_string_get_const_data (&filename));
635 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
636 if (entry) /* Already has this service file in the cache */
638 if (!check_service_file (activation, entry, NULL, error))
644 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
645 !_dbus_concat_dir_and_file (&full_path, &filename))
652 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
653 if (desktop_file == NULL)
655 _dbus_verbose ("Could not load %s: %s\n",
656 _dbus_string_get_const_data (&full_path),
659 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
661 dbus_move_error (&tmp_error, error);
665 dbus_error_free (&tmp_error);
669 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
670 * Handle these both better
672 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
674 bus_desktop_file_free (desktop_file);
677 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
678 _dbus_string_get_const_data (&full_path), tmp_error.message);
680 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
682 dbus_move_error (&tmp_error, error);
686 dbus_error_free (&tmp_error);
691 bus_desktop_file_free (desktop_file);
697 if (dbus_error_is_set (&tmp_error))
699 dbus_move_error (&tmp_error, error);
707 _DBUS_ASSERT_ERROR_IS_SET (error);
709 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
712 _dbus_directory_close (iter);
713 _dbus_string_free (&filename);
714 _dbus_string_free (&full_path);
720 populate_environment (BusActivation *activation)
726 dbus_bool_t retval = FALSE;
728 environment = _dbus_get_environment ();
730 if (environment == NULL)
733 if (!_dbus_string_init (&key))
735 dbus_free_string_array (environment);
739 if (!_dbus_string_init (&value))
741 _dbus_string_free (&key);
742 dbus_free_string_array (environment);
746 for (i = 0; environment[i] != NULL; i++)
748 if (!_dbus_string_append (&key, environment[i]))
751 if (_dbus_string_split_on_byte (&key, '=', &value))
753 char *hash_key, *hash_value;
755 if (!_dbus_string_steal_data (&key, &hash_key))
758 if (!_dbus_string_steal_data (&value, &hash_value))
761 if (!_dbus_hash_table_insert_string (activation->environment,
762 hash_key, hash_value))
765 _dbus_string_set_length (&key, 0);
766 _dbus_string_set_length (&value, 0);
769 if (environment[i] != NULL)
775 _dbus_string_free (&key);
776 _dbus_string_free (&value);
777 dbus_free_string_array (environment);
783 bus_activation_reload (BusActivation *activation,
784 const DBusString *address,
785 DBusList **directories,
791 if (activation->server_address != NULL)
792 dbus_free (activation->server_address);
793 if (!_dbus_string_copy_data (address, &activation->server_address))
799 if (activation->entries != NULL)
800 _dbus_hash_table_unref (activation->entries);
801 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
802 (DBusFreeFunction)bus_activation_entry_unref);
803 if (activation->entries == NULL)
809 if (activation->directories != NULL)
810 _dbus_hash_table_unref (activation->directories);
811 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
812 (DBusFreeFunction)bus_service_directory_unref);
814 if (activation->directories == NULL)
820 link = _dbus_list_get_first_link (directories);
823 BusServiceDirectory *s_dir;
825 dir = _dbus_strdup ((const char *) link->data);
832 s_dir = dbus_new0 (BusServiceDirectory, 1);
843 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
844 (DBusFreeFunction)bus_activation_entry_unref);
848 bus_service_directory_unref (s_dir);
853 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
855 bus_service_directory_unref (s_dir);
860 /* only fail on OOM, it is ok if we can't read the directory */
861 if (!update_directory (activation, s_dir, error))
863 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
866 dbus_error_free (error);
869 link = _dbus_list_get_next_link (directories, link);
878 bus_activation_new (BusContext *context,
879 const DBusString *address,
880 DBusList **directories,
883 BusActivation *activation;
887 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
889 activation = dbus_new0 (BusActivation, 1);
890 if (activation == NULL)
896 activation->refcount = 1;
897 activation->context = context;
898 activation->n_pending_activations = 0;
900 if (!bus_activation_reload (activation, address, directories, error))
903 /* Initialize this hash table once, we don't want to lose pending
904 * activations on reload. */
905 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
906 (DBusFreeFunction)bus_pending_activation_unref);
908 if (activation->pending_activations == NULL)
914 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
915 (DBusFreeFunction) dbus_free,
916 (DBusFreeFunction) dbus_free);
918 if (activation->environment == NULL)
924 if (!populate_environment (activation))
933 bus_activation_unref (activation);
938 bus_activation_ref (BusActivation *activation)
940 _dbus_assert (activation->refcount > 0);
942 activation->refcount += 1;
948 bus_activation_unref (BusActivation *activation)
950 _dbus_assert (activation->refcount > 0);
952 activation->refcount -= 1;
954 if (activation->refcount > 0)
957 dbus_free (activation->server_address);
958 if (activation->entries)
959 _dbus_hash_table_unref (activation->entries);
960 if (activation->pending_activations)
961 _dbus_hash_table_unref (activation->pending_activations);
962 if (activation->directories)
963 _dbus_hash_table_unref (activation->directories);
964 if (activation->environment)
965 _dbus_hash_table_unref (activation->environment);
967 dbus_free (activation);
971 add_bus_environment (BusActivation *activation,
976 if (!bus_activation_set_environment_variable (activation,
977 "DBUS_STARTER_ADDRESS",
978 activation->server_address,
982 type = bus_context_get_type (activation->context);
985 if (!bus_activation_set_environment_variable (activation,
986 "DBUS_STARTER_BUS_TYPE", type,
990 if (strcmp (type, "session") == 0)
992 if (!bus_activation_set_environment_variable (activation,
993 "DBUS_SESSION_BUS_ADDRESS",
994 activation->server_address,
998 else if (strcmp (type, "system") == 0)
1000 if (!bus_activation_set_environment_variable (activation,
1001 "DBUS_SYSTEM_BUS_ADDRESS",
1002 activation->server_address,
1013 BusPendingActivation *pending_activation;
1014 DBusPreallocatedHash *hash_entry;
1015 } RestorePendingData;
1018 restore_pending (void *data)
1020 RestorePendingData *d = data;
1022 _dbus_assert (d->pending_activation != NULL);
1023 _dbus_assert (d->hash_entry != NULL);
1025 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1026 d->pending_activation->service_name,
1027 d->pending_activation->timeout_added);
1029 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1031 d->pending_activation->service_name, d->pending_activation);
1033 bus_pending_activation_ref (d->pending_activation);
1035 d->hash_entry = NULL;
1039 free_pending_restore_data (void *data)
1041 RestorePendingData *d = data;
1044 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1047 bus_pending_activation_unref (d->pending_activation);
1053 add_restore_pending_to_transaction (BusTransaction *transaction,
1054 BusPendingActivation *pending_activation)
1056 RestorePendingData *d;
1058 d = dbus_new (RestorePendingData, 1);
1062 d->pending_activation = pending_activation;
1063 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1065 bus_pending_activation_ref (d->pending_activation);
1067 if (d->hash_entry == NULL ||
1068 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1069 free_pending_restore_data))
1071 free_pending_restore_data (d);
1075 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1081 bus_activation_service_created (BusActivation *activation,
1082 const char *service_name,
1083 BusTransaction *transaction,
1086 BusPendingActivation *pending_activation;
1087 DBusMessage *message;
1090 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1092 /* Check if it's a pending activation */
1093 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1095 if (!pending_activation)
1098 bus_context_log (activation->context,
1099 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1102 link = _dbus_list_get_first_link (&pending_activation->entries);
1103 while (link != NULL)
1105 BusPendingActivationEntry *entry = link->data;
1106 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1108 if (dbus_connection_get_is_connected (entry->connection))
1110 /* Only send activation replies to regular activation requests. */
1111 if (!entry->auto_activation)
1113 dbus_uint32_t result;
1115 message = dbus_message_new_method_return (entry->activation_message);
1118 BUS_SET_OOM (error);
1122 result = DBUS_START_REPLY_SUCCESS;
1124 if (!dbus_message_append_args (message,
1125 DBUS_TYPE_UINT32, &result,
1128 dbus_message_unref (message);
1129 BUS_SET_OOM (error);
1133 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1135 dbus_message_unref (message);
1136 BUS_SET_OOM (error);
1140 dbus_message_unref (message);
1154 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1155 BusService *service,
1156 BusTransaction *transaction,
1159 BusPendingActivation *pending_activation;
1162 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1164 /* Check if it's a pending activation */
1165 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1166 bus_service_get_name (service));
1168 if (!pending_activation)
1171 link = _dbus_list_get_first_link (&pending_activation->entries);
1172 while (link != NULL)
1174 BusPendingActivationEntry *entry = link->data;
1175 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1177 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1179 DBusConnection *addressed_recipient;
1181 addressed_recipient = bus_service_get_primary_owners_connection (service);
1183 /* Resume dispatching where we left off in bus_dispatch() */
1184 if (!bus_dispatch_matches (transaction,
1186 addressed_recipient,
1187 entry->activation_message, error))
1194 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1196 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1197 BUS_SET_OOM (error);
1201 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1210 * FIXME @todo the error messages here would ideally be preallocated
1211 * so we don't need to allocate memory to send them.
1212 * Using the usual tactic, prealloc an OOM message, then
1213 * if we can't alloc the real error send the OOM error instead.
1216 try_send_activation_failure (BusPendingActivation *pending_activation,
1217 const DBusError *how)
1219 BusActivation *activation;
1221 BusTransaction *transaction;
1223 activation = pending_activation->activation;
1225 transaction = bus_transaction_new (activation->context);
1226 if (transaction == NULL)
1229 link = _dbus_list_get_first_link (&pending_activation->entries);
1230 while (link != NULL)
1232 BusPendingActivationEntry *entry = link->data;
1233 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1235 if (dbus_connection_get_is_connected (entry->connection))
1237 if (!bus_transaction_send_error_reply (transaction,
1240 entry->activation_message))
1247 bus_transaction_execute_and_free (transaction);
1253 bus_transaction_cancel_and_free (transaction);
1258 * Free the pending activation and send an error message to all the
1259 * connections that were waiting for it.
1262 pending_activation_failed (BusPendingActivation *pending_activation,
1263 const DBusError *how)
1265 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1266 while (!try_send_activation_failure (pending_activation, how))
1267 _dbus_wait_for_memory ();
1269 /* Destroy this pending activation */
1270 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1271 pending_activation->service_name);
1275 * Depending on the exit code of the helper, set the error accordingly
1278 handle_servicehelper_exit_error (int exit_code,
1283 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1284 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1285 "Launcher could not run (out of memory)");
1287 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1288 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1289 "Failed to setup environment correctly");
1291 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1292 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1293 "Bus name is not valid or missing");
1295 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1296 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1297 "Bus name not found in system service directory");
1299 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1300 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1301 "The permission of the setuid helper is not correct");
1303 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1304 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1305 "The service file is incorrect or does not have all required attributes");
1307 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1308 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1309 "Cannot launch daemon, file not found or permissions invalid");
1311 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1312 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1313 "Invalid arguments to command line");
1315 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1316 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1317 "Launched child was signaled, it probably crashed");
1320 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1321 "Launch helper exited with unknown return code %i", exit_code);
1327 babysitter_watch_callback (DBusWatch *watch,
1328 unsigned int condition,
1331 BusPendingActivation *pending_activation = data;
1333 DBusBabysitter *babysitter;
1334 dbus_bool_t uses_servicehelper;
1336 babysitter = pending_activation->babysitter;
1338 _dbus_babysitter_ref (babysitter);
1340 retval = dbus_watch_handle (watch, condition);
1342 /* There are two major cases here; are we the system bus or the session? Here this
1343 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1344 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1346 * In both cases though, just ignore when a process exits with status 0; it's possible for
1347 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1348 * condition between this code and the child process claiming the bus name.
1350 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1352 /* FIXME this is broken in the same way that
1353 * connection watches used to be; there should be
1354 * a separate callback for status change, instead
1355 * of doing "if we handled a watch status might
1358 * Fixing this lets us move dbus_watch_handle
1359 * calls into dbus-mainloop.c
1361 if (_dbus_babysitter_get_child_exited (babysitter))
1365 dbus_bool_t activation_failed;
1368 dbus_error_init (&error);
1370 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1372 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1374 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1375 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1377 activation_failed = exit_code != 0;
1379 dbus_error_free(&error);
1381 if (activation_failed)
1383 if (uses_servicehelper)
1384 handle_servicehelper_exit_error (exit_code, &error);
1386 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1391 activation_failed = TRUE;
1394 if (activation_failed)
1396 bus_context_log (pending_activation->activation->context,
1397 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1398 pending_activation->service_name,
1401 /* Destroy all pending activations with the same exec */
1402 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1404 while (_dbus_hash_iter_next (&iter))
1406 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1408 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1409 pending_activation_failed (p, &error);
1412 /* Destroys the pending activation */
1413 pending_activation_failed (pending_activation, &error);
1415 dbus_error_free (&error);
1419 _dbus_babysitter_unref (babysitter);
1425 add_babysitter_watch (DBusWatch *watch,
1428 BusPendingActivation *pending_activation = data;
1430 return _dbus_loop_add_watch_full (
1431 bus_context_get_loop (pending_activation->activation->context),
1432 watch, babysitter_watch_callback, pending_activation, NULL);
1436 remove_babysitter_watch (DBusWatch *watch,
1439 BusPendingActivation *pending_activation = data;
1441 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1446 pending_activation_timed_out (void *data)
1448 BusPendingActivation *pending_activation = data;
1451 /* Kill the spawned process, since it sucks
1452 * (not sure this is what we want to do, but
1453 * may as well try it for now)
1455 if (pending_activation->babysitter)
1456 _dbus_babysitter_kill_child (pending_activation->babysitter);
1458 dbus_error_init (&error);
1460 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1461 "Activation of %s timed out",
1462 pending_activation->service_name);
1463 bus_context_log (pending_activation->activation->context,
1464 DBUS_SYSTEM_LOG_INFO,
1465 "Failed to activate service '%s': timed out",
1466 pending_activation->service_name);
1468 pending_activation_failed (pending_activation, &error);
1470 dbus_error_free (&error);
1476 cancel_pending (void *data)
1478 BusPendingActivation *pending_activation = data;
1480 _dbus_verbose ("Canceling pending activation of %s\n",
1481 pending_activation->service_name);
1483 if (pending_activation->babysitter)
1484 _dbus_babysitter_kill_child (pending_activation->babysitter);
1486 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1487 pending_activation->service_name);
1491 free_pending_cancel_data (void *data)
1493 BusPendingActivation *pending_activation = data;
1495 bus_pending_activation_unref (pending_activation);
1499 add_cancel_pending_to_transaction (BusTransaction *transaction,
1500 BusPendingActivation *pending_activation)
1502 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1504 free_pending_cancel_data))
1507 bus_pending_activation_ref (pending_activation);
1509 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1515 update_service_cache (BusActivation *activation, DBusError *error)
1519 _dbus_hash_iter_init (activation->directories, &iter);
1520 while (_dbus_hash_iter_next (&iter))
1522 DBusError tmp_error;
1523 BusServiceDirectory *s_dir;
1525 s_dir = _dbus_hash_iter_get_value (&iter);
1527 dbus_error_init (&tmp_error);
1528 if (!update_directory (activation, s_dir, &tmp_error))
1530 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1532 dbus_move_error (&tmp_error, error);
1536 dbus_error_free (&tmp_error);
1544 static BusActivationEntry *
1545 activation_find_entry (BusActivation *activation,
1546 const char *service_name,
1549 BusActivationEntry *entry;
1551 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1554 if (!update_service_cache (activation, error))
1557 entry = _dbus_hash_table_lookup_string (activation->entries,
1562 BusActivationEntry *updated_entry;
1564 if (!check_service_file (activation, entry, &updated_entry, error))
1567 entry = updated_entry;
1572 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1573 "The name %s was not provided by any .service files",
1582 bus_activation_get_environment (BusActivation *activation)
1589 length = _dbus_hash_table_get_n_entries (activation->environment);
1591 environment = dbus_new0 (char *, length + 1);
1593 if (environment == NULL)
1597 _dbus_hash_iter_init (activation->environment, &iter);
1599 if (!_dbus_string_init (&entry))
1601 dbus_free_string_array (environment);
1605 while (_dbus_hash_iter_next (&iter))
1607 const char *key, *value;
1609 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1610 value = (const char *) _dbus_hash_iter_get_value (&iter);
1612 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1615 if (!_dbus_string_steal_data (&entry, environment + i))
1620 _dbus_string_free (&entry);
1624 dbus_free_string_array (environment);
1632 bus_activation_set_environment_variable (BusActivation *activation,
1644 hash_key = _dbus_strdup (key);
1646 if (hash_key == NULL)
1649 hash_value = _dbus_strdup (value);
1651 if (hash_value == NULL)
1654 if (!_dbus_hash_table_insert_string (activation->environment,
1655 hash_key, hash_value))
1660 if (retval == FALSE)
1662 dbus_free (hash_key);
1663 dbus_free (hash_value);
1664 BUS_SET_OOM (error);
1671 bus_activation_activate_service (BusActivation *activation,
1672 DBusConnection *connection,
1673 BusTransaction *transaction,
1674 dbus_bool_t auto_activation,
1675 DBusMessage *activation_message,
1676 const char *service_name,
1679 DBusError tmp_error;
1680 BusActivationEntry *entry;
1681 BusPendingActivation *pending_activation;
1682 BusPendingActivationEntry *pending_activation_entry;
1683 DBusMessage *message;
1684 DBusString service_str;
1685 const char *servicehelper;
1691 dbus_bool_t was_pending_activation;
1694 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1696 if (activation->n_pending_activations >=
1697 bus_context_get_max_pending_activations (activation->context))
1699 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1700 "The maximum number of pending activations has been reached, activation of %s failed",
1705 entry = activation_find_entry (activation, service_name, error);
1709 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1710 * call us if the service is already active.
1712 if (!auto_activation)
1714 /* Check if the service is active */
1715 _dbus_string_init_const (&service_str, service_name);
1716 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1718 dbus_uint32_t result;
1720 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1722 message = dbus_message_new_method_return (activation_message);
1726 _dbus_verbose ("No memory to create reply to activate message\n");
1727 BUS_SET_OOM (error);
1731 result = DBUS_START_REPLY_ALREADY_RUNNING;
1733 if (!dbus_message_append_args (message,
1734 DBUS_TYPE_UINT32, &result,
1737 _dbus_verbose ("No memory to set args of reply to activate message\n");
1738 BUS_SET_OOM (error);
1739 dbus_message_unref (message);
1743 retval = bus_transaction_send_from_driver (transaction, connection, message);
1744 dbus_message_unref (message);
1747 _dbus_verbose ("Failed to send reply\n");
1748 BUS_SET_OOM (error);
1755 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1756 if (!pending_activation_entry)
1758 _dbus_verbose ("Failed to create pending activation entry\n");
1759 BUS_SET_OOM (error);
1763 pending_activation_entry->auto_activation = auto_activation;
1765 pending_activation_entry->activation_message = activation_message;
1766 dbus_message_ref (activation_message);
1767 pending_activation_entry->connection = connection;
1768 dbus_connection_ref (connection);
1770 /* Check if the service is being activated */
1771 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1772 was_pending_activation = (pending_activation != NULL);
1773 if (was_pending_activation)
1775 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1777 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1779 BUS_SET_OOM (error);
1780 bus_pending_activation_entry_free (pending_activation_entry);
1784 pending_activation->n_entries += 1;
1785 pending_activation->activation->n_pending_activations += 1;
1789 pending_activation = dbus_new0 (BusPendingActivation, 1);
1790 if (!pending_activation)
1792 _dbus_verbose ("Failed to create pending activation\n");
1794 BUS_SET_OOM (error);
1795 bus_pending_activation_entry_free (pending_activation_entry);
1799 pending_activation->activation = activation;
1800 pending_activation->refcount = 1;
1802 pending_activation->service_name = _dbus_strdup (service_name);
1803 if (!pending_activation->service_name)
1805 _dbus_verbose ("Failed to copy service name for pending activation\n");
1807 BUS_SET_OOM (error);
1808 bus_pending_activation_unref (pending_activation);
1809 bus_pending_activation_entry_free (pending_activation_entry);
1813 pending_activation->exec = _dbus_strdup (entry->exec);
1814 if (!pending_activation->exec)
1816 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1817 BUS_SET_OOM (error);
1818 bus_pending_activation_unref (pending_activation);
1819 bus_pending_activation_entry_free (pending_activation_entry);
1823 if (entry->systemd_service)
1825 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1826 if (!pending_activation->systemd_service)
1828 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1829 BUS_SET_OOM (error);
1830 bus_pending_activation_unref (pending_activation);
1831 bus_pending_activation_entry_free (pending_activation_entry);
1836 pending_activation->timeout =
1837 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1838 pending_activation_timed_out,
1841 if (!pending_activation->timeout)
1843 _dbus_verbose ("Failed to create timeout for pending activation\n");
1845 BUS_SET_OOM (error);
1846 bus_pending_activation_unref (pending_activation);
1847 bus_pending_activation_entry_free (pending_activation_entry);
1851 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1852 pending_activation->timeout))
1854 _dbus_verbose ("Failed to add timeout for pending activation\n");
1856 BUS_SET_OOM (error);
1857 bus_pending_activation_unref (pending_activation);
1858 bus_pending_activation_entry_free (pending_activation_entry);
1862 pending_activation->timeout_added = TRUE;
1864 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1866 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1868 BUS_SET_OOM (error);
1869 bus_pending_activation_unref (pending_activation);
1870 bus_pending_activation_entry_free (pending_activation_entry);
1874 pending_activation->n_entries += 1;
1875 pending_activation->activation->n_pending_activations += 1;
1877 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1878 pending_activation->service_name,
1879 pending_activation))
1881 _dbus_verbose ("Failed to put pending activation in hash table\n");
1883 BUS_SET_OOM (error);
1884 bus_pending_activation_unref (pending_activation);
1889 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1891 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1892 BUS_SET_OOM (error);
1893 _dbus_hash_table_remove_string (activation->pending_activations,
1894 pending_activation->service_name);
1899 if (was_pending_activation)
1902 if (bus_context_get_systemd_activation (activation->context))
1904 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1905 /* systemd itself is missing apparently. That can happen
1906 only during early startup. Let's just wait until systemd
1907 connects to us and do nothing. */
1910 if (entry->systemd_service)
1912 BusTransaction *activation_transaction;
1913 DBusString service_string;
1914 BusService *service;
1915 BusRegistry *registry;
1917 /* OK, we have a systemd service configured for this entry,
1918 hence let's enqueue an activation request message. This
1919 is implemented as a directed signal, not a method call,
1920 for three reasons: 1) we don't expect a response on
1921 success, where we just expect a name appearing on the
1922 bus; 2) at this time the systemd service might not yet
1923 have connected, so we wouldn't know the message serial at
1924 this point to set up a pending call; 3) it is ugly if the
1925 bus suddenly becomes the caller of a remote method. */
1927 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1928 "org.freedesktop.systemd1.Activator",
1929 "ActivationRequest");
1932 _dbus_verbose ("No memory to create activation message\n");
1933 BUS_SET_OOM (error);
1937 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1938 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1939 !dbus_message_append_args (message,
1940 DBUS_TYPE_STRING, &entry->systemd_service,
1943 _dbus_verbose ("No memory to set args of activation message\n");
1944 dbus_message_unref (message);
1945 BUS_SET_OOM (error);
1949 /* Create our transaction */
1950 activation_transaction = bus_transaction_new (activation->context);
1951 if (activation_transaction == NULL)
1953 _dbus_verbose ("No memory to create activation transaction\n");
1954 dbus_message_unref (message);
1955 BUS_SET_OOM (error);
1959 /* Check whether systemd is already connected */
1960 registry = bus_connection_get_registry (connection);
1961 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1962 service = bus_registry_lookup (registry, &service_string);
1964 if (service != NULL)
1966 bus_context_log (activation->context,
1967 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1969 entry->systemd_service);
1970 /* Wonderful, systemd is connected, let's just send the msg */
1971 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1976 bus_context_log (activation->context,
1977 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1979 entry->systemd_service);
1980 /* systemd is not around, let's "activate" it. */
1981 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1982 message, "org.freedesktop.systemd1", error);
1985 dbus_message_unref (message);
1989 bus_context_log (activation->context,
1990 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
1992 entry->systemd_service);
1993 _DBUS_ASSERT_ERROR_IS_SET (error);
1994 _dbus_verbose ("failed to send activation message: %s\n", error->name);
1995 bus_transaction_cancel_and_free (activation_transaction);
1999 bus_transaction_execute_and_free (activation_transaction);
2003 /* OK, we have no configured systemd service, hence let's
2004 proceed with traditional activation. */
2007 /* use command as system and session different */
2008 if (!_dbus_string_init (&command))
2010 BUS_SET_OOM (error);
2014 /* does the bus use a helper? */
2015 servicehelper = bus_context_get_servicehelper (activation->context);
2016 if (servicehelper != NULL)
2018 if (entry->user == NULL)
2020 _dbus_string_free (&command);
2021 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2022 "Cannot do system-bus activation with no user\n");
2026 /* join the helper path and the service name */
2027 if (!_dbus_string_append (&command, servicehelper))
2029 _dbus_string_free (&command);
2030 BUS_SET_OOM (error);
2033 if (!_dbus_string_append (&command, " "))
2035 _dbus_string_free (&command);
2036 BUS_SET_OOM (error);
2039 if (!_dbus_string_append (&command, service_name))
2041 _dbus_string_free (&command);
2042 BUS_SET_OOM (error);
2048 /* the bus does not use a helper, so we can append arguments with the exec line */
2049 if (!_dbus_string_append (&command, entry->exec))
2051 _dbus_string_free (&command);
2052 BUS_SET_OOM (error);
2057 /* convert command into arguments */
2058 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2060 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2061 _DBUS_ASSERT_ERROR_IS_SET (error);
2063 _dbus_hash_table_remove_string (activation->pending_activations,
2064 pending_activation->service_name);
2066 _dbus_string_free (&command);
2069 _dbus_string_free (&command);
2071 if (!add_bus_environment (activation, error))
2073 _DBUS_ASSERT_ERROR_IS_SET (error);
2074 dbus_free_string_array (argv);
2078 envp = bus_activation_get_environment (activation);
2082 BUS_SET_OOM (error);
2083 dbus_free_string_array (argv);
2087 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2088 if (servicehelper != NULL)
2089 bus_context_log (activation->context,
2090 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2093 bus_context_log (activation->context,
2094 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2097 dbus_error_init (&tmp_error);
2099 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2104 _dbus_verbose ("Failed to spawn child\n");
2105 bus_context_log (activation->context,
2106 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2109 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2110 dbus_move_error (&tmp_error, error);
2111 dbus_free_string_array (argv);
2112 dbus_free_string_array (envp);
2117 dbus_free_string_array (argv);
2120 _dbus_assert (pending_activation->babysitter != NULL);
2122 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2123 add_babysitter_watch,
2124 remove_babysitter_watch,
2129 BUS_SET_OOM (error);
2130 _dbus_verbose ("Failed to set babysitter watch functions\n");
2138 bus_activation_list_services (BusActivation *activation,
2146 len = _dbus_hash_table_get_n_entries (activation->entries);
2147 retval = dbus_new (char *, len + 1);
2152 _dbus_hash_iter_init (activation->entries, &iter);
2154 while (_dbus_hash_iter_next (&iter))
2156 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2158 retval[i] = _dbus_strdup (entry->name);
2159 if (retval[i] == NULL)
2174 for (j = 0; j < i; j++)
2175 dbus_free (retval[i]);
2182 dbus_activation_systemd_failure (BusActivation *activation,
2183 DBusMessage *message)
2186 const char *code, *str, *unit = NULL;
2188 dbus_error_init(&error);
2190 /* This is called whenever the systemd activator sent us a
2191 response. We'll invalidate all pending activations that match the
2194 if (dbus_message_get_args (message, &error,
2195 DBUS_TYPE_STRING, &unit,
2196 DBUS_TYPE_STRING, &code,
2197 DBUS_TYPE_STRING, &str,
2199 dbus_set_error(&error, code, str);
2206 bus_context_log (activation->context,
2207 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2211 _dbus_hash_iter_init (activation->pending_activations,
2214 while (_dbus_hash_iter_next (&iter))
2216 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2218 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2219 pending_activation_failed(p, &error);
2223 dbus_error_free(&error);
2228 #ifdef DBUS_BUILD_TESTS
2232 #define SERVICE_NAME_1 "MyService1"
2233 #define SERVICE_NAME_2 "MyService2"
2234 #define SERVICE_NAME_3 "MyService3"
2236 #define SERVICE_FILE_1 "service-1.service"
2237 #define SERVICE_FILE_2 "service-2.service"
2238 #define SERVICE_FILE_3 "service-3.service"
2241 test_create_service_file (DBusString *dir,
2242 const char *filename,
2246 DBusString file_name, full_path;
2248 dbus_bool_t ret_val;
2251 _dbus_string_init_const (&file_name, filename);
2253 if (!_dbus_string_init (&full_path))
2256 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2257 !_dbus_concat_dir_and_file (&full_path, &file_name))
2263 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2270 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2274 _dbus_string_free (&full_path);
2279 test_remove_service_file (DBusString *dir, const char *filename)
2281 DBusString file_name, full_path;
2282 dbus_bool_t ret_val;
2286 _dbus_string_init_const (&file_name, filename);
2288 if (!_dbus_string_init (&full_path))
2291 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2292 !_dbus_concat_dir_and_file (&full_path, &file_name))
2298 if (!_dbus_delete_file (&full_path, NULL))
2305 _dbus_string_free (&full_path);
2310 test_remove_directory (DBusString *dir)
2313 DBusString filename, full_path;
2314 dbus_bool_t ret_val;
2318 if (!_dbus_string_init (&filename))
2321 if (!_dbus_string_init (&full_path))
2323 _dbus_string_free (&filename);
2327 iter = _dbus_directory_open (dir, NULL);
2334 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2336 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2342 _dbus_directory_close (iter);
2344 if (!_dbus_delete_directory (dir, NULL))
2351 _dbus_string_free (&filename);
2352 _dbus_string_free (&full_path);
2358 init_service_reload_test (DBusString *dir)
2362 if (!_dbus_stat (dir, &stat_buf, NULL))
2364 if (!_dbus_create_directory (dir, NULL))
2369 if (!test_remove_directory (dir))
2372 if (!_dbus_create_directory (dir, NULL))
2376 /* Create one initial file */
2377 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2384 cleanup_service_reload_test (DBusString *dir)
2386 if (!test_remove_directory (dir))
2394 BusActivation *activation;
2395 const char *service_name;
2396 dbus_bool_t expecting_find;
2400 check_func (void *data)
2403 BusActivationEntry *entry;
2405 dbus_bool_t ret_val;
2410 dbus_error_init (&error);
2412 entry = activation_find_entry (d->activation, d->service_name, &error);
2415 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2421 if (d->expecting_find)
2425 dbus_error_free (&error);
2429 if (!d->expecting_find)
2437 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2442 err = !_dbus_test_oom_handling (description, check_func, data);
2444 err = !check_func (data);
2447 _dbus_assert_not_reached ("Test failed");
2453 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2455 BusActivation *activation;
2457 DBusList *directories;
2461 _dbus_string_init_const (&address, "");
2463 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2466 activation = bus_activation_new (NULL, &address, &directories, NULL);
2470 d.activation = activation;
2472 /* Check for existing service file */
2473 d.expecting_find = TRUE;
2474 d.service_name = SERVICE_NAME_1;
2476 if (!do_test ("Existing service file", oom_test, &d))
2479 /* Check for non-existing service file */
2480 d.expecting_find = FALSE;
2481 d.service_name = SERVICE_NAME_3;
2483 if (!do_test ("Nonexisting service file", oom_test, &d))
2486 /* Check for added service file */
2487 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2490 d.expecting_find = TRUE;
2491 d.service_name = SERVICE_NAME_2;
2493 if (!do_test ("Added service file", oom_test, &d))
2496 /* Check for removed service file */
2497 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2500 d.expecting_find = FALSE;
2501 d.service_name = SERVICE_FILE_2;
2503 if (!do_test ("Removed service file", oom_test, &d))
2506 /* Check for updated service file */
2508 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2510 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2513 d.expecting_find = TRUE;
2514 d.service_name = SERVICE_NAME_3;
2516 if (!do_test ("Updated service file, part 1", oom_test, &d))
2519 d.expecting_find = FALSE;
2520 d.service_name = SERVICE_NAME_1;
2522 if (!do_test ("Updated service file, part 2", oom_test, &d))
2525 bus_activation_unref (activation);
2526 _dbus_list_clear (&directories);
2532 bus_activation_service_reload_test (const DBusString *test_data_dir)
2534 DBusString directory;
2536 if (!_dbus_string_init (&directory))
2539 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2542 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2543 !_dbus_generate_random_ascii (&directory, 6))
2548 /* Do normal tests */
2549 if (!init_service_reload_test (&directory))
2550 _dbus_assert_not_reached ("could not initiate service reload test");
2552 if (!do_service_reload_test (&directory, FALSE))
2556 if (!init_service_reload_test (&directory))
2557 _dbus_assert_not_reached ("could not initiate service reload test");
2559 if (!do_service_reload_test (&directory, TRUE))
2562 /* Cleanup test directory */
2563 if (!cleanup_service_reload_test (&directory))
2566 _dbus_string_free (&directory);
2571 #endif /* DBUS_BUILD_TESTS */