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);
147 handle_timeout_callback (DBusTimeout *timeout,
150 BusPendingActivation *pending_activation = data;
152 while (!dbus_timeout_handle (pending_activation->timeout))
153 _dbus_wait_for_memory ();
156 static BusPendingActivation *
157 bus_pending_activation_ref (BusPendingActivation *pending_activation)
159 _dbus_assert (pending_activation->refcount > 0);
160 pending_activation->refcount += 1;
162 return pending_activation;
166 bus_pending_activation_unref (BusPendingActivation *pending_activation)
170 if (pending_activation == NULL) /* hash table requires this */
173 _dbus_assert (pending_activation->refcount > 0);
174 pending_activation->refcount -= 1;
176 if (pending_activation->refcount > 0)
179 if (pending_activation->timeout_added)
181 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
182 pending_activation->timeout,
183 handle_timeout_callback, pending_activation);
184 pending_activation->timeout_added = FALSE;
187 if (pending_activation->timeout)
188 _dbus_timeout_unref (pending_activation->timeout);
190 if (pending_activation->babysitter)
192 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
194 pending_activation->babysitter,
196 _dbus_assert_not_reached ("setting watch functions to NULL failed");
198 _dbus_babysitter_unref (pending_activation->babysitter);
201 dbus_free (pending_activation->service_name);
202 dbus_free (pending_activation->exec);
203 dbus_free (pending_activation->systemd_service);
205 link = _dbus_list_get_first_link (&pending_activation->entries);
209 BusPendingActivationEntry *entry = link->data;
211 bus_pending_activation_entry_free (entry);
213 link = _dbus_list_get_next_link (&pending_activation->entries, link);
215 _dbus_list_clear (&pending_activation->entries);
217 pending_activation->activation->n_pending_activations -=
218 pending_activation->n_entries;
220 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
222 dbus_free (pending_activation);
225 static BusActivationEntry *
226 bus_activation_entry_ref (BusActivationEntry *entry)
228 _dbus_assert (entry->refcount > 0);
235 bus_activation_entry_unref (BusActivationEntry *entry)
237 if (entry == NULL) /* hash table requires this */
240 _dbus_assert (entry->refcount > 0);
243 if (entry->refcount > 0)
246 dbus_free (entry->name);
247 dbus_free (entry->exec);
248 dbus_free (entry->user);
249 dbus_free (entry->filename);
250 dbus_free (entry->systemd_service);
256 update_desktop_file_entry (BusActivation *activation,
257 BusServiceDirectory *s_dir,
258 DBusString *filename,
259 BusDesktopFile *desktop_file,
262 char *name, *exec, *user, *exec_tmp, *systemd_service;
263 BusActivationEntry *entry;
265 DBusString file_path;
269 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
276 systemd_service = NULL;
278 dbus_error_init (&tmp_error);
280 if (!_dbus_string_init (&file_path))
286 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
287 !_dbus_concat_dir_and_file (&file_path, filename))
293 if (!_dbus_stat (&file_path, &stat_buf, NULL))
295 dbus_set_error (error, DBUS_ERROR_FAILED,
296 "Can't stat the service file\n");
300 if (!bus_desktop_file_get_string (desktop_file,
301 DBUS_SERVICE_SECTION,
307 if (!bus_desktop_file_get_string (desktop_file,
308 DBUS_SERVICE_SECTION,
314 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
315 dbus_free (exec_tmp);
318 /* user is not _required_ unless we are using system activation */
319 if (!bus_desktop_file_get_string (desktop_file,
320 DBUS_SERVICE_SECTION,
324 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
325 /* if we got OOM, then exit */
326 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
328 dbus_move_error (&tmp_error, error);
333 /* if we have error because we didn't find anything then continue */
334 dbus_error_free (&tmp_error);
339 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
341 /* systemd service is never required */
342 if (!bus_desktop_file_get_string (desktop_file,
343 DBUS_SERVICE_SECTION,
344 DBUS_SERVICE_SYSTEMD_SERVICE,
345 &systemd_service, &tmp_error))
347 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
348 /* if we got OOM, then exit */
349 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
351 dbus_move_error (&tmp_error, error);
356 /* if we have error because we didn't find anything then continue */
357 dbus_error_free (&tmp_error);
358 dbus_free (systemd_service);
359 systemd_service = NULL;
363 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
365 entry = _dbus_hash_table_lookup_string (s_dir->entries,
366 _dbus_string_get_const_data (filename));
368 if (entry == NULL) /* New file */
370 /* FIXME we need a better-defined algorithm for which service file to
371 * pick than "whichever one is first in the directory listing"
373 if (_dbus_hash_table_lookup_string (activation->entries, name))
375 dbus_set_error (error, DBUS_ERROR_FAILED,
376 "Service %s already exists in activation entry list\n", name);
380 entry = dbus_new0 (BusActivationEntry, 1);
390 entry->systemd_service = systemd_service;
393 /* ownership has been transferred to entry, do not free separately */
397 systemd_service = NULL;
399 entry->s_dir = s_dir;
400 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
401 if (!entry->filename)
407 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
413 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
415 /* Revert the insertion in the entries table */
416 _dbus_hash_table_remove_string (activation->entries, entry->name);
421 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
423 else /* Just update the entry */
425 bus_activation_entry_ref (entry);
426 _dbus_hash_table_remove_string (activation->entries, entry->name);
428 if (_dbus_hash_table_lookup_string (activation->entries, name))
430 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
431 name, _dbus_string_get_const_data (&file_path));
435 /* ownership has been transferred to entry, do not free separately */
436 dbus_free (entry->name);
440 dbus_free (entry->exec);
444 dbus_free (entry->user);
448 dbus_free (entry->systemd_service);
449 entry->systemd_service = systemd_service;
450 systemd_service = NULL;
452 if (!_dbus_hash_table_insert_string (activation->entries,
453 entry->name, bus_activation_entry_ref(entry)))
456 /* Also remove path to entries hash since we want this in sync with
457 * the entries hash table */
458 _dbus_hash_table_remove_string (entry->s_dir->entries,
464 entry->mtime = stat_buf.mtime;
468 /* if these have been transferred into entry, the variables will be NULL */
472 dbus_free (systemd_service);
473 _dbus_string_free (&file_path);
476 bus_activation_entry_unref (entry);
482 check_service_file (BusActivation *activation,
483 BusActivationEntry *entry,
484 BusActivationEntry **updated_entry,
489 BusActivationEntry *tmp_entry;
490 DBusString file_path;
496 _dbus_string_init_const (&filename, entry->filename);
498 if (!_dbus_string_init (&file_path))
504 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
505 !_dbus_concat_dir_and_file (&file_path, &filename))
512 if (!_dbus_stat (&file_path, &stat_buf, NULL))
514 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
515 _dbus_string_get_const_data (&file_path));
517 _dbus_hash_table_remove_string (activation->entries, entry->name);
518 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
526 if (stat_buf.mtime > entry->mtime)
528 BusDesktopFile *desktop_file;
531 dbus_error_init (&tmp_error);
533 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
534 if (desktop_file == NULL)
536 _dbus_verbose ("Could not load %s: %s\n",
537 _dbus_string_get_const_data (&file_path),
539 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
541 dbus_move_error (&tmp_error, error);
545 dbus_error_free (&tmp_error);
550 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
551 * Handle these both better
553 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
555 bus_desktop_file_free (desktop_file);
556 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
558 dbus_move_error (&tmp_error, error);
562 dbus_error_free (&tmp_error);
567 bus_desktop_file_free (desktop_file);
573 _dbus_string_free (&file_path);
575 if (updated_entry != NULL)
576 *updated_entry = tmp_entry;
581 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
582 * hash entries it already added.
585 update_directory (BusActivation *activation,
586 BusServiceDirectory *s_dir,
590 DBusString dir, filename;
591 BusDesktopFile *desktop_file;
594 BusActivationEntry *entry;
595 DBusString full_path;
597 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
602 _dbus_string_init_const (&dir, s_dir->dir_c);
604 if (!_dbus_string_init (&filename))
610 if (!_dbus_string_init (&full_path))
613 _dbus_string_free (&filename);
619 /* from this point it's safe to "goto out" */
621 iter = _dbus_directory_open (&dir, error);
624 _dbus_verbose ("Failed to open directory %s: %s\n",
626 error ? error->message : "unknown");
630 /* Now read the files */
631 dbus_error_init (&tmp_error);
632 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
634 _dbus_assert (!dbus_error_is_set (&tmp_error));
636 _dbus_string_set_length (&full_path, 0);
638 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
640 _dbus_verbose ("Skipping non-.service file %s\n",
641 _dbus_string_get_const_data (&filename));
645 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
646 if (entry) /* Already has this service file in the cache */
648 if (!check_service_file (activation, entry, NULL, error))
654 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
655 !_dbus_concat_dir_and_file (&full_path, &filename))
662 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
663 if (desktop_file == NULL)
665 _dbus_verbose ("Could not load %s: %s\n",
666 _dbus_string_get_const_data (&full_path),
669 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
671 dbus_move_error (&tmp_error, error);
675 dbus_error_free (&tmp_error);
679 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
680 * Handle these both better
682 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
684 bus_desktop_file_free (desktop_file);
687 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
688 _dbus_string_get_const_data (&full_path), tmp_error.message);
690 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
692 dbus_move_error (&tmp_error, error);
696 dbus_error_free (&tmp_error);
701 bus_desktop_file_free (desktop_file);
707 if (dbus_error_is_set (&tmp_error))
709 dbus_move_error (&tmp_error, error);
717 _DBUS_ASSERT_ERROR_IS_SET (error);
719 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
722 _dbus_directory_close (iter);
723 _dbus_string_free (&filename);
724 _dbus_string_free (&full_path);
730 populate_environment (BusActivation *activation)
736 dbus_bool_t retval = FALSE;
738 environment = _dbus_get_environment ();
740 if (environment == NULL)
743 if (!_dbus_string_init (&key))
745 dbus_free_string_array (environment);
749 if (!_dbus_string_init (&value))
751 _dbus_string_free (&key);
752 dbus_free_string_array (environment);
756 for (i = 0; environment[i] != NULL; i++)
758 if (!_dbus_string_append (&key, environment[i]))
761 if (_dbus_string_split_on_byte (&key, '=', &value))
763 char *hash_key, *hash_value;
765 if (!_dbus_string_steal_data (&key, &hash_key))
768 if (!_dbus_string_steal_data (&value, &hash_value))
771 if (!_dbus_hash_table_insert_string (activation->environment,
772 hash_key, hash_value))
775 _dbus_string_set_length (&key, 0);
776 _dbus_string_set_length (&value, 0);
779 if (environment[i] != NULL)
785 _dbus_string_free (&key);
786 _dbus_string_free (&value);
787 dbus_free_string_array (environment);
793 bus_activation_reload (BusActivation *activation,
794 const DBusString *address,
795 DBusList **directories,
801 if (activation->server_address != NULL)
802 dbus_free (activation->server_address);
803 if (!_dbus_string_copy_data (address, &activation->server_address))
809 if (activation->entries != NULL)
810 _dbus_hash_table_unref (activation->entries);
811 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
812 (DBusFreeFunction)bus_activation_entry_unref);
813 if (activation->entries == NULL)
819 if (activation->directories != NULL)
820 _dbus_hash_table_unref (activation->directories);
821 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
822 (DBusFreeFunction)bus_service_directory_unref);
824 if (activation->directories == NULL)
830 link = _dbus_list_get_first_link (directories);
833 BusServiceDirectory *s_dir;
835 dir = _dbus_strdup ((const char *) link->data);
842 s_dir = dbus_new0 (BusServiceDirectory, 1);
853 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
854 (DBusFreeFunction)bus_activation_entry_unref);
858 bus_service_directory_unref (s_dir);
863 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
865 bus_service_directory_unref (s_dir);
870 /* only fail on OOM, it is ok if we can't read the directory */
871 if (!update_directory (activation, s_dir, error))
873 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
876 dbus_error_free (error);
879 link = _dbus_list_get_next_link (directories, link);
888 bus_activation_new (BusContext *context,
889 const DBusString *address,
890 DBusList **directories,
893 BusActivation *activation;
897 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
899 activation = dbus_new0 (BusActivation, 1);
900 if (activation == NULL)
906 activation->refcount = 1;
907 activation->context = context;
908 activation->n_pending_activations = 0;
910 if (!bus_activation_reload (activation, address, directories, error))
913 /* Initialize this hash table once, we don't want to lose pending
914 * activations on reload. */
915 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
916 (DBusFreeFunction)bus_pending_activation_unref);
918 if (activation->pending_activations == NULL)
924 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
925 (DBusFreeFunction) dbus_free,
926 (DBusFreeFunction) dbus_free);
928 if (activation->environment == NULL)
934 if (!populate_environment (activation))
943 bus_activation_unref (activation);
948 bus_activation_ref (BusActivation *activation)
950 _dbus_assert (activation->refcount > 0);
952 activation->refcount += 1;
958 bus_activation_unref (BusActivation *activation)
960 _dbus_assert (activation->refcount > 0);
962 activation->refcount -= 1;
964 if (activation->refcount > 0)
967 dbus_free (activation->server_address);
968 if (activation->entries)
969 _dbus_hash_table_unref (activation->entries);
970 if (activation->pending_activations)
971 _dbus_hash_table_unref (activation->pending_activations);
972 if (activation->directories)
973 _dbus_hash_table_unref (activation->directories);
974 if (activation->environment)
975 _dbus_hash_table_unref (activation->environment);
977 dbus_free (activation);
981 add_bus_environment (BusActivation *activation,
986 if (!bus_activation_set_environment_variable (activation,
987 "DBUS_STARTER_ADDRESS",
988 activation->server_address,
992 type = bus_context_get_type (activation->context);
995 if (!bus_activation_set_environment_variable (activation,
996 "DBUS_STARTER_BUS_TYPE", type,
1000 if (strcmp (type, "session") == 0)
1002 if (!bus_activation_set_environment_variable (activation,
1003 "DBUS_SESSION_BUS_ADDRESS",
1004 activation->server_address,
1008 else if (strcmp (type, "system") == 0)
1010 if (!bus_activation_set_environment_variable (activation,
1011 "DBUS_SYSTEM_BUS_ADDRESS",
1012 activation->server_address,
1023 BusPendingActivation *pending_activation;
1024 DBusPreallocatedHash *hash_entry;
1025 } RestorePendingData;
1028 restore_pending (void *data)
1030 RestorePendingData *d = data;
1032 _dbus_assert (d->pending_activation != NULL);
1033 _dbus_assert (d->hash_entry != NULL);
1035 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1036 d->pending_activation->service_name,
1037 d->pending_activation->timeout_added);
1039 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1041 d->pending_activation->service_name, d->pending_activation);
1043 bus_pending_activation_ref (d->pending_activation);
1045 d->hash_entry = NULL;
1049 free_pending_restore_data (void *data)
1051 RestorePendingData *d = data;
1054 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1057 bus_pending_activation_unref (d->pending_activation);
1063 add_restore_pending_to_transaction (BusTransaction *transaction,
1064 BusPendingActivation *pending_activation)
1066 RestorePendingData *d;
1068 d = dbus_new (RestorePendingData, 1);
1072 d->pending_activation = pending_activation;
1073 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1075 bus_pending_activation_ref (d->pending_activation);
1077 if (d->hash_entry == NULL ||
1078 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1079 free_pending_restore_data))
1081 free_pending_restore_data (d);
1085 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1091 bus_activation_service_created (BusActivation *activation,
1092 const char *service_name,
1093 BusTransaction *transaction,
1096 BusPendingActivation *pending_activation;
1097 DBusMessage *message;
1100 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1102 /* Check if it's a pending activation */
1103 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1105 if (!pending_activation)
1108 bus_context_log (activation->context,
1109 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1112 link = _dbus_list_get_first_link (&pending_activation->entries);
1113 while (link != NULL)
1115 BusPendingActivationEntry *entry = link->data;
1116 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1118 if (dbus_connection_get_is_connected (entry->connection))
1120 /* Only send activation replies to regular activation requests. */
1121 if (!entry->auto_activation)
1123 dbus_uint32_t result;
1125 message = dbus_message_new_method_return (entry->activation_message);
1128 BUS_SET_OOM (error);
1132 result = DBUS_START_REPLY_SUCCESS;
1134 if (!dbus_message_append_args (message,
1135 DBUS_TYPE_UINT32, &result,
1138 dbus_message_unref (message);
1139 BUS_SET_OOM (error);
1143 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1145 dbus_message_unref (message);
1146 BUS_SET_OOM (error);
1150 dbus_message_unref (message);
1164 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1165 BusService *service,
1166 BusTransaction *transaction,
1169 BusPendingActivation *pending_activation;
1172 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1174 /* Check if it's a pending activation */
1175 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1176 bus_service_get_name (service));
1178 if (!pending_activation)
1181 link = _dbus_list_get_first_link (&pending_activation->entries);
1182 while (link != NULL)
1184 BusPendingActivationEntry *entry = link->data;
1185 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1187 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1189 DBusConnection *addressed_recipient;
1191 addressed_recipient = bus_service_get_primary_owners_connection (service);
1193 /* Resume dispatching where we left off in bus_dispatch() */
1194 if (!bus_dispatch_matches (transaction,
1196 addressed_recipient,
1197 entry->activation_message, error))
1204 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1206 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1207 BUS_SET_OOM (error);
1211 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1220 * FIXME @todo the error messages here would ideally be preallocated
1221 * so we don't need to allocate memory to send them.
1222 * Using the usual tactic, prealloc an OOM message, then
1223 * if we can't alloc the real error send the OOM error instead.
1226 try_send_activation_failure (BusPendingActivation *pending_activation,
1227 const DBusError *how)
1229 BusActivation *activation;
1231 BusTransaction *transaction;
1233 activation = pending_activation->activation;
1235 transaction = bus_transaction_new (activation->context);
1236 if (transaction == NULL)
1239 link = _dbus_list_get_first_link (&pending_activation->entries);
1240 while (link != NULL)
1242 BusPendingActivationEntry *entry = link->data;
1243 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1245 if (dbus_connection_get_is_connected (entry->connection))
1247 if (!bus_transaction_send_error_reply (transaction,
1250 entry->activation_message))
1257 bus_transaction_execute_and_free (transaction);
1263 bus_transaction_cancel_and_free (transaction);
1268 * Free the pending activation and send an error message to all the
1269 * connections that were waiting for it.
1272 pending_activation_failed (BusPendingActivation *pending_activation,
1273 const DBusError *how)
1275 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1276 while (!try_send_activation_failure (pending_activation, how))
1277 _dbus_wait_for_memory ();
1279 /* Destroy this pending activation */
1280 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1281 pending_activation->service_name);
1285 * Depending on the exit code of the helper, set the error accordingly
1288 handle_servicehelper_exit_error (int exit_code,
1293 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1294 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1295 "Launcher could not run (out of memory)");
1297 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1298 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1299 "Failed to setup environment correctly");
1301 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1302 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1303 "Bus name is not valid or missing");
1305 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1306 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1307 "Bus name not found in system service directory");
1309 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1310 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1311 "The permission of the setuid helper is not correct");
1313 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1314 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1315 "The service file is incorrect or does not have all required attributes");
1317 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1318 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1319 "Cannot launch daemon, file not found or permissions invalid");
1321 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1322 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1323 "Invalid arguments to command line");
1325 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1326 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1327 "Launched child was signaled, it probably crashed");
1330 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1331 "Launch helper exited with unknown return code %i", exit_code);
1337 babysitter_watch_callback (DBusWatch *watch,
1338 unsigned int condition,
1341 BusPendingActivation *pending_activation = data;
1343 DBusBabysitter *babysitter;
1344 dbus_bool_t uses_servicehelper;
1346 babysitter = pending_activation->babysitter;
1348 _dbus_babysitter_ref (babysitter);
1350 retval = dbus_watch_handle (watch, condition);
1352 /* There are two major cases here; are we the system bus or the session? Here this
1353 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1354 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1356 * In both cases though, just ignore when a process exits with status 0; it's possible for
1357 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1358 * condition between this code and the child process claiming the bus name.
1360 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1362 /* FIXME this is broken in the same way that
1363 * connection watches used to be; there should be
1364 * a separate callback for status change, instead
1365 * of doing "if we handled a watch status might
1368 * Fixing this lets us move dbus_watch_handle
1369 * calls into dbus-mainloop.c
1371 if (_dbus_babysitter_get_child_exited (babysitter))
1375 dbus_bool_t activation_failed;
1378 dbus_error_init (&error);
1380 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1382 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1384 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1385 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1387 activation_failed = exit_code != 0;
1389 dbus_error_free(&error);
1391 if (activation_failed)
1393 if (uses_servicehelper)
1394 handle_servicehelper_exit_error (exit_code, &error);
1396 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1401 activation_failed = TRUE;
1404 if (activation_failed)
1406 bus_context_log (pending_activation->activation->context,
1407 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1408 pending_activation->service_name,
1411 /* Destroy all pending activations with the same exec */
1412 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1414 while (_dbus_hash_iter_next (&iter))
1416 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1418 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1419 pending_activation_failed (p, &error);
1422 /* Destroys the pending activation */
1423 pending_activation_failed (pending_activation, &error);
1425 dbus_error_free (&error);
1429 _dbus_babysitter_unref (babysitter);
1435 add_babysitter_watch (DBusWatch *watch,
1438 BusPendingActivation *pending_activation = data;
1440 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1441 watch, babysitter_watch_callback, pending_activation,
1446 remove_babysitter_watch (DBusWatch *watch,
1449 BusPendingActivation *pending_activation = data;
1451 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1452 watch, babysitter_watch_callback, pending_activation);
1456 pending_activation_timed_out (void *data)
1458 BusPendingActivation *pending_activation = data;
1461 /* Kill the spawned process, since it sucks
1462 * (not sure this is what we want to do, but
1463 * may as well try it for now)
1465 if (pending_activation->babysitter)
1466 _dbus_babysitter_kill_child (pending_activation->babysitter);
1468 dbus_error_init (&error);
1470 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1471 "Activation of %s timed out",
1472 pending_activation->service_name);
1473 bus_context_log (pending_activation->activation->context,
1474 DBUS_SYSTEM_LOG_INFO,
1475 "Failed to activate service '%s': timed out",
1476 pending_activation->service_name);
1478 pending_activation_failed (pending_activation, &error);
1480 dbus_error_free (&error);
1486 cancel_pending (void *data)
1488 BusPendingActivation *pending_activation = data;
1490 _dbus_verbose ("Canceling pending activation of %s\n",
1491 pending_activation->service_name);
1493 if (pending_activation->babysitter)
1494 _dbus_babysitter_kill_child (pending_activation->babysitter);
1496 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1497 pending_activation->service_name);
1501 free_pending_cancel_data (void *data)
1503 BusPendingActivation *pending_activation = data;
1505 bus_pending_activation_unref (pending_activation);
1509 add_cancel_pending_to_transaction (BusTransaction *transaction,
1510 BusPendingActivation *pending_activation)
1512 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1514 free_pending_cancel_data))
1517 bus_pending_activation_ref (pending_activation);
1519 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1525 update_service_cache (BusActivation *activation, DBusError *error)
1529 _dbus_hash_iter_init (activation->directories, &iter);
1530 while (_dbus_hash_iter_next (&iter))
1532 DBusError tmp_error;
1533 BusServiceDirectory *s_dir;
1535 s_dir = _dbus_hash_iter_get_value (&iter);
1537 dbus_error_init (&tmp_error);
1538 if (!update_directory (activation, s_dir, &tmp_error))
1540 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1542 dbus_move_error (&tmp_error, error);
1546 dbus_error_free (&tmp_error);
1554 static BusActivationEntry *
1555 activation_find_entry (BusActivation *activation,
1556 const char *service_name,
1559 BusActivationEntry *entry;
1561 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1564 if (!update_service_cache (activation, error))
1567 entry = _dbus_hash_table_lookup_string (activation->entries,
1572 BusActivationEntry *updated_entry;
1574 if (!check_service_file (activation, entry, &updated_entry, error))
1577 entry = updated_entry;
1582 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1583 "The name %s was not provided by any .service files",
1592 bus_activation_get_environment (BusActivation *activation)
1599 length = _dbus_hash_table_get_n_entries (activation->environment);
1601 environment = dbus_new0 (char *, length + 1);
1603 if (environment == NULL)
1607 _dbus_hash_iter_init (activation->environment, &iter);
1609 if (!_dbus_string_init (&entry))
1611 dbus_free_string_array (environment);
1615 while (_dbus_hash_iter_next (&iter))
1617 const char *key, *value;
1619 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1620 value = (const char *) _dbus_hash_iter_get_value (&iter);
1622 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1625 if (!_dbus_string_steal_data (&entry, environment + i))
1630 _dbus_string_free (&entry);
1634 dbus_free_string_array (environment);
1642 bus_activation_set_environment_variable (BusActivation *activation,
1654 hash_key = _dbus_strdup (key);
1656 if (hash_key == NULL)
1659 hash_value = _dbus_strdup (value);
1661 if (hash_value == NULL)
1664 if (!_dbus_hash_table_insert_string (activation->environment,
1665 hash_key, hash_value))
1670 if (retval == FALSE)
1672 dbus_free (hash_key);
1673 dbus_free (hash_value);
1674 BUS_SET_OOM (error);
1681 bus_activation_activate_service (BusActivation *activation,
1682 DBusConnection *connection,
1683 BusTransaction *transaction,
1684 dbus_bool_t auto_activation,
1685 DBusMessage *activation_message,
1686 const char *service_name,
1689 DBusError tmp_error;
1690 BusActivationEntry *entry;
1691 BusPendingActivation *pending_activation;
1692 BusPendingActivationEntry *pending_activation_entry;
1693 DBusMessage *message;
1694 DBusString service_str;
1695 const char *servicehelper;
1701 dbus_bool_t was_pending_activation;
1704 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1706 if (activation->n_pending_activations >=
1707 bus_context_get_max_pending_activations (activation->context))
1709 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1710 "The maximum number of pending activations has been reached, activation of %s failed",
1715 entry = activation_find_entry (activation, service_name, error);
1719 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1720 * call us if the service is already active.
1722 if (!auto_activation)
1724 /* Check if the service is active */
1725 _dbus_string_init_const (&service_str, service_name);
1726 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1728 dbus_uint32_t result;
1730 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1732 message = dbus_message_new_method_return (activation_message);
1736 _dbus_verbose ("No memory to create reply to activate message\n");
1737 BUS_SET_OOM (error);
1741 result = DBUS_START_REPLY_ALREADY_RUNNING;
1743 if (!dbus_message_append_args (message,
1744 DBUS_TYPE_UINT32, &result,
1747 _dbus_verbose ("No memory to set args of reply to activate message\n");
1748 BUS_SET_OOM (error);
1749 dbus_message_unref (message);
1753 retval = bus_transaction_send_from_driver (transaction, connection, message);
1754 dbus_message_unref (message);
1757 _dbus_verbose ("Failed to send reply\n");
1758 BUS_SET_OOM (error);
1765 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1766 if (!pending_activation_entry)
1768 _dbus_verbose ("Failed to create pending activation entry\n");
1769 BUS_SET_OOM (error);
1773 pending_activation_entry->auto_activation = auto_activation;
1775 pending_activation_entry->activation_message = activation_message;
1776 dbus_message_ref (activation_message);
1777 pending_activation_entry->connection = connection;
1778 dbus_connection_ref (connection);
1780 /* Check if the service is being activated */
1781 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1782 was_pending_activation = (pending_activation != NULL);
1783 if (was_pending_activation)
1785 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1787 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1789 BUS_SET_OOM (error);
1790 bus_pending_activation_entry_free (pending_activation_entry);
1794 pending_activation->n_entries += 1;
1795 pending_activation->activation->n_pending_activations += 1;
1799 pending_activation = dbus_new0 (BusPendingActivation, 1);
1800 if (!pending_activation)
1802 _dbus_verbose ("Failed to create pending activation\n");
1804 BUS_SET_OOM (error);
1805 bus_pending_activation_entry_free (pending_activation_entry);
1809 pending_activation->activation = activation;
1810 pending_activation->refcount = 1;
1812 pending_activation->service_name = _dbus_strdup (service_name);
1813 if (!pending_activation->service_name)
1815 _dbus_verbose ("Failed to copy service name 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 pending_activation->exec = _dbus_strdup (entry->exec);
1824 if (!pending_activation->exec)
1826 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1827 BUS_SET_OOM (error);
1828 bus_pending_activation_unref (pending_activation);
1829 bus_pending_activation_entry_free (pending_activation_entry);
1833 if (entry->systemd_service)
1835 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1836 if (!pending_activation->systemd_service)
1838 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1839 BUS_SET_OOM (error);
1840 bus_pending_activation_unref (pending_activation);
1841 bus_pending_activation_entry_free (pending_activation_entry);
1846 pending_activation->timeout =
1847 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1848 pending_activation_timed_out,
1851 if (!pending_activation->timeout)
1853 _dbus_verbose ("Failed to create timeout for pending activation\n");
1855 BUS_SET_OOM (error);
1856 bus_pending_activation_unref (pending_activation);
1857 bus_pending_activation_entry_free (pending_activation_entry);
1861 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1862 pending_activation->timeout,
1863 handle_timeout_callback,
1867 _dbus_verbose ("Failed to add timeout for pending activation\n");
1869 BUS_SET_OOM (error);
1870 bus_pending_activation_unref (pending_activation);
1871 bus_pending_activation_entry_free (pending_activation_entry);
1875 pending_activation->timeout_added = TRUE;
1877 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1879 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1881 BUS_SET_OOM (error);
1882 bus_pending_activation_unref (pending_activation);
1883 bus_pending_activation_entry_free (pending_activation_entry);
1887 pending_activation->n_entries += 1;
1888 pending_activation->activation->n_pending_activations += 1;
1890 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1891 pending_activation->service_name,
1892 pending_activation))
1894 _dbus_verbose ("Failed to put pending activation in hash table\n");
1896 BUS_SET_OOM (error);
1897 bus_pending_activation_unref (pending_activation);
1902 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1904 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1905 BUS_SET_OOM (error);
1906 _dbus_hash_table_remove_string (activation->pending_activations,
1907 pending_activation->service_name);
1912 if (was_pending_activation)
1915 if (bus_context_get_systemd_activation (activation->context))
1917 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1918 /* systemd itself is missing apparently. That can happen
1919 only during early startup. Let's just wait until systemd
1920 connects to us and do nothing. */
1923 if (entry->systemd_service)
1925 BusTransaction *activation_transaction;
1926 DBusString service_string;
1927 BusService *service;
1928 BusRegistry *registry;
1930 /* OK, we have a systemd service configured for this entry,
1931 hence let's enqueue an activation request message. This
1932 is implemented as a directed signal, not a method call,
1933 for three reasons: 1) we don't expect a response on
1934 success, where we just expect a name appearing on the
1935 bus; 2) at this time the systemd service might not yet
1936 have connected, so we wouldn't know the message serial at
1937 this point to set up a pending call; 3) it is ugly if the
1938 bus suddenly becomes the caller of a remote method. */
1940 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1941 "org.freedesktop.systemd1.Activator",
1942 "ActivationRequest");
1945 _dbus_verbose ("No memory to create activation message\n");
1946 BUS_SET_OOM (error);
1950 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1951 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1952 !dbus_message_append_args (message,
1953 DBUS_TYPE_STRING, &entry->systemd_service,
1956 _dbus_verbose ("No memory to set args of activation message\n");
1957 dbus_message_unref (message);
1958 BUS_SET_OOM (error);
1962 /* Create our transaction */
1963 activation_transaction = bus_transaction_new (activation->context);
1964 if (activation_transaction == NULL)
1966 _dbus_verbose ("No memory to create activation transaction\n");
1967 dbus_message_unref (message);
1968 BUS_SET_OOM (error);
1972 /* Check whether systemd is already connected */
1973 registry = bus_connection_get_registry (connection);
1974 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1975 service = bus_registry_lookup (registry, &service_string);
1977 if (service != NULL)
1979 bus_context_log (activation->context,
1980 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1982 entry->systemd_service);
1983 /* Wonderful, systemd is connected, let's just send the msg */
1984 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1989 bus_context_log (activation->context,
1990 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1992 entry->systemd_service);
1993 /* systemd is not around, let's "activate" it. */
1994 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1995 message, "org.freedesktop.systemd1", error);
1998 dbus_message_unref (message);
2002 bus_context_log (activation->context,
2003 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
2005 entry->systemd_service);
2006 _DBUS_ASSERT_ERROR_IS_SET (error);
2007 _dbus_verbose ("failed to send activation message: %s\n", error->name);
2008 bus_transaction_cancel_and_free (activation_transaction);
2012 bus_transaction_execute_and_free (activation_transaction);
2016 /* OK, we have no configured systemd service, hence let's
2017 proceed with traditional activation. */
2020 /* use command as system and session different */
2021 if (!_dbus_string_init (&command))
2023 BUS_SET_OOM (error);
2027 /* does the bus use a helper? */
2028 servicehelper = bus_context_get_servicehelper (activation->context);
2029 if (servicehelper != NULL)
2031 if (entry->user == NULL)
2033 _dbus_string_free (&command);
2034 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2035 "Cannot do system-bus activation with no user\n");
2039 /* join the helper path and the service name */
2040 if (!_dbus_string_append (&command, servicehelper))
2042 _dbus_string_free (&command);
2043 BUS_SET_OOM (error);
2046 if (!_dbus_string_append (&command, " "))
2048 _dbus_string_free (&command);
2049 BUS_SET_OOM (error);
2052 if (!_dbus_string_append (&command, service_name))
2054 _dbus_string_free (&command);
2055 BUS_SET_OOM (error);
2061 /* the bus does not use a helper, so we can append arguments with the exec line */
2062 if (!_dbus_string_append (&command, entry->exec))
2064 _dbus_string_free (&command);
2065 BUS_SET_OOM (error);
2070 /* convert command into arguments */
2071 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2073 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2074 _DBUS_ASSERT_ERROR_IS_SET (error);
2076 _dbus_hash_table_remove_string (activation->pending_activations,
2077 pending_activation->service_name);
2079 _dbus_string_free (&command);
2082 _dbus_string_free (&command);
2084 if (!add_bus_environment (activation, error))
2086 _DBUS_ASSERT_ERROR_IS_SET (error);
2087 dbus_free_string_array (argv);
2091 envp = bus_activation_get_environment (activation);
2095 BUS_SET_OOM (error);
2096 dbus_free_string_array (argv);
2100 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2101 if (servicehelper != NULL)
2102 bus_context_log (activation->context,
2103 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2106 bus_context_log (activation->context,
2107 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2110 dbus_error_init (&tmp_error);
2112 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2117 _dbus_verbose ("Failed to spawn child\n");
2118 bus_context_log (activation->context,
2119 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2122 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2123 dbus_move_error (&tmp_error, error);
2124 dbus_free_string_array (argv);
2125 dbus_free_string_array (envp);
2130 dbus_free_string_array (argv);
2133 _dbus_assert (pending_activation->babysitter != NULL);
2135 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2136 add_babysitter_watch,
2137 remove_babysitter_watch,
2142 BUS_SET_OOM (error);
2143 _dbus_verbose ("Failed to set babysitter watch functions\n");
2151 bus_activation_list_services (BusActivation *activation,
2159 len = _dbus_hash_table_get_n_entries (activation->entries);
2160 retval = dbus_new (char *, len + 1);
2165 _dbus_hash_iter_init (activation->entries, &iter);
2167 while (_dbus_hash_iter_next (&iter))
2169 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2171 retval[i] = _dbus_strdup (entry->name);
2172 if (retval[i] == NULL)
2187 for (j = 0; j < i; j++)
2188 dbus_free (retval[i]);
2195 dbus_activation_systemd_failure (BusActivation *activation,
2196 DBusMessage *message)
2199 const char *code, *str, *unit = NULL;
2201 dbus_error_init(&error);
2203 /* This is called whenever the systemd activator sent us a
2204 response. We'll invalidate all pending activations that match the
2207 if (dbus_message_get_args (message, &error,
2208 DBUS_TYPE_STRING, &unit,
2209 DBUS_TYPE_STRING, &code,
2210 DBUS_TYPE_STRING, &str,
2212 dbus_set_error(&error, code, str);
2219 bus_context_log (activation->context,
2220 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2224 _dbus_hash_iter_init (activation->pending_activations,
2227 while (_dbus_hash_iter_next (&iter))
2229 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2231 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2232 pending_activation_failed(p, &error);
2236 dbus_error_free(&error);
2241 #ifdef DBUS_BUILD_TESTS
2245 #define SERVICE_NAME_1 "MyService1"
2246 #define SERVICE_NAME_2 "MyService2"
2247 #define SERVICE_NAME_3 "MyService3"
2249 #define SERVICE_FILE_1 "service-1.service"
2250 #define SERVICE_FILE_2 "service-2.service"
2251 #define SERVICE_FILE_3 "service-3.service"
2254 test_create_service_file (DBusString *dir,
2255 const char *filename,
2259 DBusString file_name, full_path;
2261 dbus_bool_t ret_val;
2264 _dbus_string_init_const (&file_name, filename);
2266 if (!_dbus_string_init (&full_path))
2269 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2270 !_dbus_concat_dir_and_file (&full_path, &file_name))
2276 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2283 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2287 _dbus_string_free (&full_path);
2292 test_remove_service_file (DBusString *dir, const char *filename)
2294 DBusString file_name, full_path;
2295 dbus_bool_t ret_val;
2299 _dbus_string_init_const (&file_name, filename);
2301 if (!_dbus_string_init (&full_path))
2304 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2305 !_dbus_concat_dir_and_file (&full_path, &file_name))
2311 if (!_dbus_delete_file (&full_path, NULL))
2318 _dbus_string_free (&full_path);
2323 test_remove_directory (DBusString *dir)
2326 DBusString filename, full_path;
2327 dbus_bool_t ret_val;
2331 if (!_dbus_string_init (&filename))
2334 if (!_dbus_string_init (&full_path))
2336 _dbus_string_free (&filename);
2340 iter = _dbus_directory_open (dir, NULL);
2347 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2349 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2355 _dbus_directory_close (iter);
2357 if (!_dbus_delete_directory (dir, NULL))
2364 _dbus_string_free (&filename);
2365 _dbus_string_free (&full_path);
2371 init_service_reload_test (DBusString *dir)
2375 if (!_dbus_stat (dir, &stat_buf, NULL))
2377 if (!_dbus_create_directory (dir, NULL))
2382 if (!test_remove_directory (dir))
2385 if (!_dbus_create_directory (dir, NULL))
2389 /* Create one initial file */
2390 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2397 cleanup_service_reload_test (DBusString *dir)
2399 if (!test_remove_directory (dir))
2407 BusActivation *activation;
2408 const char *service_name;
2409 dbus_bool_t expecting_find;
2413 check_func (void *data)
2416 BusActivationEntry *entry;
2418 dbus_bool_t ret_val;
2423 dbus_error_init (&error);
2425 entry = activation_find_entry (d->activation, d->service_name, &error);
2428 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2434 if (d->expecting_find)
2438 dbus_error_free (&error);
2442 if (!d->expecting_find)
2450 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2455 err = !_dbus_test_oom_handling (description, check_func, data);
2457 err = !check_func (data);
2460 _dbus_assert_not_reached ("Test failed");
2466 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2468 BusActivation *activation;
2470 DBusList *directories;
2474 _dbus_string_init_const (&address, "");
2476 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2479 activation = bus_activation_new (NULL, &address, &directories, NULL);
2483 d.activation = activation;
2485 /* Check for existing service file */
2486 d.expecting_find = TRUE;
2487 d.service_name = SERVICE_NAME_1;
2489 if (!do_test ("Existing service file", oom_test, &d))
2492 /* Check for non-existing service file */
2493 d.expecting_find = FALSE;
2494 d.service_name = SERVICE_NAME_3;
2496 if (!do_test ("Nonexisting service file", oom_test, &d))
2499 /* Check for added service file */
2500 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2503 d.expecting_find = TRUE;
2504 d.service_name = SERVICE_NAME_2;
2506 if (!do_test ("Added service file", oom_test, &d))
2509 /* Check for removed service file */
2510 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2513 d.expecting_find = FALSE;
2514 d.service_name = SERVICE_FILE_2;
2516 if (!do_test ("Removed service file", oom_test, &d))
2519 /* Check for updated service file */
2521 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2523 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2526 d.expecting_find = TRUE;
2527 d.service_name = SERVICE_NAME_3;
2529 if (!do_test ("Updated service file, part 1", oom_test, &d))
2532 d.expecting_find = FALSE;
2533 d.service_name = SERVICE_NAME_1;
2535 if (!do_test ("Updated service file, part 2", oom_test, &d))
2538 bus_activation_unref (activation);
2539 _dbus_list_clear (&directories);
2545 bus_activation_service_reload_test (const DBusString *test_data_dir)
2547 DBusString directory;
2549 if (!_dbus_string_init (&directory))
2552 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2555 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2556 !_dbus_generate_random_ascii (&directory, 6))
2561 /* Do normal tests */
2562 if (!init_service_reload_test (&directory))
2563 _dbus_assert_not_reached ("could not initiate service reload test");
2565 if (!do_service_reload_test (&directory, FALSE))
2569 if (!init_service_reload_test (&directory))
2570 _dbus_assert_not_reached ("could not initiate service reload test");
2572 if (!do_service_reload_test (&directory, TRUE))
2575 /* Cleanup test directory */
2576 if (!cleanup_service_reload_test (&directory))
2579 _dbus_string_free (&directory);
2584 #endif /* DBUS_BUILD_TESTS */