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\" is already in cache, ignoring\n",
431 name, _dbus_string_get_const_data (&file_path));
432 dbus_set_error (error, DBUS_ERROR_FAILED,
433 "The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
434 name, _dbus_string_get_const_data (&file_path));
438 /* ownership has been transferred to entry, do not free separately */
439 dbus_free (entry->name);
443 dbus_free (entry->exec);
447 dbus_free (entry->user);
451 dbus_free (entry->systemd_service);
452 entry->systemd_service = systemd_service;
453 systemd_service = NULL;
455 if (!_dbus_hash_table_insert_string (activation->entries,
456 entry->name, bus_activation_entry_ref(entry)))
459 /* Also remove path to entries hash since we want this in sync with
460 * the entries hash table */
461 _dbus_hash_table_remove_string (entry->s_dir->entries,
467 entry->mtime = stat_buf.mtime;
471 /* if these have been transferred into entry, the variables will be NULL */
475 dbus_free (systemd_service);
476 _dbus_string_free (&file_path);
479 bus_activation_entry_unref (entry);
485 check_service_file (BusActivation *activation,
486 BusActivationEntry *entry,
487 BusActivationEntry **updated_entry,
492 BusActivationEntry *tmp_entry;
493 DBusString file_path;
499 _dbus_string_init_const (&filename, entry->filename);
501 if (!_dbus_string_init (&file_path))
507 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
508 !_dbus_concat_dir_and_file (&file_path, &filename))
515 if (!_dbus_stat (&file_path, &stat_buf, NULL))
517 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
518 _dbus_string_get_const_data (&file_path));
520 _dbus_hash_table_remove_string (activation->entries, entry->name);
521 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
529 if (stat_buf.mtime > entry->mtime)
531 BusDesktopFile *desktop_file;
534 dbus_error_init (&tmp_error);
536 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
537 if (desktop_file == NULL)
539 _dbus_verbose ("Could not load %s: %s\n",
540 _dbus_string_get_const_data (&file_path),
542 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
544 dbus_move_error (&tmp_error, error);
548 dbus_error_free (&tmp_error);
553 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
554 * Handle these both better
556 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
558 bus_desktop_file_free (desktop_file);
559 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
561 dbus_move_error (&tmp_error, error);
565 dbus_error_free (&tmp_error);
570 bus_desktop_file_free (desktop_file);
576 _dbus_string_free (&file_path);
578 if (updated_entry != NULL)
579 *updated_entry = tmp_entry;
584 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
585 * hash entries it already added.
588 update_directory (BusActivation *activation,
589 BusServiceDirectory *s_dir,
593 DBusString dir, filename;
594 BusDesktopFile *desktop_file;
597 BusActivationEntry *entry;
598 DBusString full_path;
600 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
605 _dbus_string_init_const (&dir, s_dir->dir_c);
607 if (!_dbus_string_init (&filename))
613 if (!_dbus_string_init (&full_path))
616 _dbus_string_free (&filename);
622 /* from this point it's safe to "goto out" */
624 iter = _dbus_directory_open (&dir, error);
627 _dbus_verbose ("Failed to open directory %s: %s\n",
629 error ? error->message : "unknown");
633 /* Now read the files */
634 dbus_error_init (&tmp_error);
635 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
637 _dbus_assert (!dbus_error_is_set (&tmp_error));
639 _dbus_string_set_length (&full_path, 0);
641 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
643 _dbus_verbose ("Skipping non-.service file %s\n",
644 _dbus_string_get_const_data (&filename));
648 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
649 if (entry) /* Already has this service file in the cache */
651 if (!check_service_file (activation, entry, NULL, error))
657 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
658 !_dbus_concat_dir_and_file (&full_path, &filename))
665 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
666 if (desktop_file == NULL)
668 _dbus_verbose ("Could not load %s: %s\n",
669 _dbus_string_get_const_data (&full_path),
672 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
674 dbus_move_error (&tmp_error, error);
678 dbus_error_free (&tmp_error);
682 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
683 * Handle these both better
685 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
687 bus_desktop_file_free (desktop_file);
690 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
691 _dbus_string_get_const_data (&full_path), tmp_error.message);
693 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
695 dbus_move_error (&tmp_error, error);
699 dbus_error_free (&tmp_error);
704 bus_desktop_file_free (desktop_file);
710 if (dbus_error_is_set (&tmp_error))
712 dbus_move_error (&tmp_error, error);
720 _DBUS_ASSERT_ERROR_IS_SET (error);
722 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
725 _dbus_directory_close (iter);
726 _dbus_string_free (&filename);
727 _dbus_string_free (&full_path);
733 populate_environment (BusActivation *activation)
739 dbus_bool_t retval = FALSE;
741 environment = _dbus_get_environment ();
743 if (environment == NULL)
746 if (!_dbus_string_init (&key))
748 dbus_free_string_array (environment);
752 if (!_dbus_string_init (&value))
754 _dbus_string_free (&key);
755 dbus_free_string_array (environment);
759 for (i = 0; environment[i] != NULL; i++)
761 if (!_dbus_string_append (&key, environment[i]))
764 if (_dbus_string_split_on_byte (&key, '=', &value))
766 char *hash_key, *hash_value;
768 if (!_dbus_string_steal_data (&key, &hash_key))
771 if (!_dbus_string_steal_data (&value, &hash_value))
774 if (!_dbus_hash_table_insert_string (activation->environment,
775 hash_key, hash_value))
778 _dbus_string_set_length (&key, 0);
779 _dbus_string_set_length (&value, 0);
782 if (environment[i] != NULL)
788 _dbus_string_free (&key);
789 _dbus_string_free (&value);
790 dbus_free_string_array (environment);
796 bus_activation_reload (BusActivation *activation,
797 const DBusString *address,
798 DBusList **directories,
804 if (activation->server_address != NULL)
805 dbus_free (activation->server_address);
806 if (!_dbus_string_copy_data (address, &activation->server_address))
812 if (activation->entries != NULL)
813 _dbus_hash_table_unref (activation->entries);
814 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
815 (DBusFreeFunction)bus_activation_entry_unref);
816 if (activation->entries == NULL)
822 if (activation->directories != NULL)
823 _dbus_hash_table_unref (activation->directories);
824 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
825 (DBusFreeFunction)bus_service_directory_unref);
827 if (activation->directories == NULL)
833 link = _dbus_list_get_first_link (directories);
836 BusServiceDirectory *s_dir;
838 dir = _dbus_strdup ((const char *) link->data);
845 s_dir = dbus_new0 (BusServiceDirectory, 1);
856 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
857 (DBusFreeFunction)bus_activation_entry_unref);
861 bus_service_directory_unref (s_dir);
866 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
868 bus_service_directory_unref (s_dir);
873 /* only fail on OOM, it is ok if we can't read the directory */
874 if (!update_directory (activation, s_dir, error))
876 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
879 dbus_error_free (error);
882 link = _dbus_list_get_next_link (directories, link);
891 bus_activation_new (BusContext *context,
892 const DBusString *address,
893 DBusList **directories,
896 BusActivation *activation;
900 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
902 activation = dbus_new0 (BusActivation, 1);
903 if (activation == NULL)
909 activation->refcount = 1;
910 activation->context = context;
911 activation->n_pending_activations = 0;
913 if (!bus_activation_reload (activation, address, directories, error))
916 /* Initialize this hash table once, we don't want to lose pending
917 * activations on reload. */
918 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
919 (DBusFreeFunction)bus_pending_activation_unref);
921 if (activation->pending_activations == NULL)
927 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
928 (DBusFreeFunction) dbus_free,
929 (DBusFreeFunction) dbus_free);
931 if (activation->environment == NULL)
937 if (!populate_environment (activation))
946 bus_activation_unref (activation);
951 bus_activation_ref (BusActivation *activation)
953 _dbus_assert (activation->refcount > 0);
955 activation->refcount += 1;
961 bus_activation_unref (BusActivation *activation)
963 _dbus_assert (activation->refcount > 0);
965 activation->refcount -= 1;
967 if (activation->refcount > 0)
970 dbus_free (activation->server_address);
971 if (activation->entries)
972 _dbus_hash_table_unref (activation->entries);
973 if (activation->pending_activations)
974 _dbus_hash_table_unref (activation->pending_activations);
975 if (activation->directories)
976 _dbus_hash_table_unref (activation->directories);
977 if (activation->environment)
978 _dbus_hash_table_unref (activation->environment);
980 dbus_free (activation);
984 add_bus_environment (BusActivation *activation,
989 if (!bus_activation_set_environment_variable (activation,
990 "DBUS_STARTER_ADDRESS",
991 activation->server_address,
995 type = bus_context_get_type (activation->context);
998 if (!bus_activation_set_environment_variable (activation,
999 "DBUS_STARTER_BUS_TYPE", type,
1003 if (strcmp (type, "session") == 0)
1005 if (!bus_activation_set_environment_variable (activation,
1006 "DBUS_SESSION_BUS_ADDRESS",
1007 activation->server_address,
1011 else if (strcmp (type, "system") == 0)
1013 if (!bus_activation_set_environment_variable (activation,
1014 "DBUS_SYSTEM_BUS_ADDRESS",
1015 activation->server_address,
1026 BusPendingActivation *pending_activation;
1027 DBusPreallocatedHash *hash_entry;
1028 } RestorePendingData;
1031 restore_pending (void *data)
1033 RestorePendingData *d = data;
1035 _dbus_assert (d->pending_activation != NULL);
1036 _dbus_assert (d->hash_entry != NULL);
1038 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1039 d->pending_activation->service_name,
1040 d->pending_activation->timeout_added);
1042 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1044 d->pending_activation->service_name, d->pending_activation);
1046 bus_pending_activation_ref (d->pending_activation);
1048 d->hash_entry = NULL;
1052 free_pending_restore_data (void *data)
1054 RestorePendingData *d = data;
1057 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1060 bus_pending_activation_unref (d->pending_activation);
1066 add_restore_pending_to_transaction (BusTransaction *transaction,
1067 BusPendingActivation *pending_activation)
1069 RestorePendingData *d;
1071 d = dbus_new (RestorePendingData, 1);
1075 d->pending_activation = pending_activation;
1076 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1078 bus_pending_activation_ref (d->pending_activation);
1080 if (d->hash_entry == NULL ||
1081 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1082 free_pending_restore_data))
1084 free_pending_restore_data (d);
1088 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1094 bus_activation_service_created (BusActivation *activation,
1095 const char *service_name,
1096 BusTransaction *transaction,
1099 BusPendingActivation *pending_activation;
1100 DBusMessage *message;
1103 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1105 /* Check if it's a pending activation */
1106 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1108 if (!pending_activation)
1111 bus_context_log (activation->context,
1112 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1115 link = _dbus_list_get_first_link (&pending_activation->entries);
1116 while (link != NULL)
1118 BusPendingActivationEntry *entry = link->data;
1119 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1121 if (dbus_connection_get_is_connected (entry->connection))
1123 /* Only send activation replies to regular activation requests. */
1124 if (!entry->auto_activation)
1126 dbus_uint32_t result;
1128 message = dbus_message_new_method_return (entry->activation_message);
1131 BUS_SET_OOM (error);
1135 result = DBUS_START_REPLY_SUCCESS;
1137 if (!dbus_message_append_args (message,
1138 DBUS_TYPE_UINT32, &result,
1141 dbus_message_unref (message);
1142 BUS_SET_OOM (error);
1146 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1148 dbus_message_unref (message);
1149 BUS_SET_OOM (error);
1153 dbus_message_unref (message);
1167 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1168 BusService *service,
1169 BusTransaction *transaction,
1172 BusPendingActivation *pending_activation;
1175 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1177 /* Check if it's a pending activation */
1178 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1179 bus_service_get_name (service));
1181 if (!pending_activation)
1184 link = _dbus_list_get_first_link (&pending_activation->entries);
1185 while (link != NULL)
1187 BusPendingActivationEntry *entry = link->data;
1188 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1190 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1192 DBusConnection *addressed_recipient;
1194 addressed_recipient = bus_service_get_primary_owners_connection (service);
1196 /* Resume dispatching where we left off in bus_dispatch() */
1197 if (!bus_dispatch_matches (transaction,
1199 addressed_recipient,
1200 entry->activation_message, error))
1207 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1209 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1210 BUS_SET_OOM (error);
1214 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1223 * FIXME @todo the error messages here would ideally be preallocated
1224 * so we don't need to allocate memory to send them.
1225 * Using the usual tactic, prealloc an OOM message, then
1226 * if we can't alloc the real error send the OOM error instead.
1229 try_send_activation_failure (BusPendingActivation *pending_activation,
1230 const DBusError *how)
1232 BusActivation *activation;
1234 BusTransaction *transaction;
1236 activation = pending_activation->activation;
1238 transaction = bus_transaction_new (activation->context);
1239 if (transaction == NULL)
1242 link = _dbus_list_get_first_link (&pending_activation->entries);
1243 while (link != NULL)
1245 BusPendingActivationEntry *entry = link->data;
1246 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1248 if (dbus_connection_get_is_connected (entry->connection))
1250 if (!bus_transaction_send_error_reply (transaction,
1253 entry->activation_message))
1260 bus_transaction_execute_and_free (transaction);
1266 bus_transaction_cancel_and_free (transaction);
1271 * Free the pending activation and send an error message to all the
1272 * connections that were waiting for it.
1275 pending_activation_failed (BusPendingActivation *pending_activation,
1276 const DBusError *how)
1278 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1279 while (!try_send_activation_failure (pending_activation, how))
1280 _dbus_wait_for_memory ();
1282 /* Destroy this pending activation */
1283 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1284 pending_activation->service_name);
1288 * Depending on the exit code of the helper, set the error accordingly
1291 handle_servicehelper_exit_error (int exit_code,
1296 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1297 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1298 "Launcher could not run (out of memory)");
1300 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1301 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1302 "Failed to setup environment correctly");
1304 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1305 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1306 "Bus name is not valid or missing");
1308 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1309 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1310 "Bus name not found in system service directory");
1312 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1313 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1314 "The permission of the setuid helper is not correct");
1316 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1317 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1318 "The service file is incorrect or does not have all required attributes");
1320 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1321 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1322 "Cannot launch daemon, file not found or permissions invalid");
1324 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1325 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1326 "Invalid arguments to command line");
1328 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1329 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1330 "Launched child was signaled, it probably crashed");
1333 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1334 "Launch helper exited with unknown return code %i", exit_code);
1340 babysitter_watch_callback (DBusWatch *watch,
1341 unsigned int condition,
1344 BusPendingActivation *pending_activation = data;
1346 DBusBabysitter *babysitter;
1347 dbus_bool_t uses_servicehelper;
1349 babysitter = pending_activation->babysitter;
1351 _dbus_babysitter_ref (babysitter);
1353 retval = dbus_watch_handle (watch, condition);
1355 /* There are two major cases here; are we the system bus or the session? Here this
1356 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1357 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1359 * In both cases though, just ignore when a process exits with status 0; it's possible for
1360 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1361 * condition between this code and the child process claiming the bus name.
1363 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1365 /* FIXME this is broken in the same way that
1366 * connection watches used to be; there should be
1367 * a separate callback for status change, instead
1368 * of doing "if we handled a watch status might
1371 * Fixing this lets us move dbus_watch_handle
1372 * calls into dbus-mainloop.c
1374 if (_dbus_babysitter_get_child_exited (babysitter))
1378 dbus_bool_t activation_failed;
1381 dbus_error_init (&error);
1383 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1385 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1387 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1388 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1390 activation_failed = exit_code != 0;
1392 dbus_error_free(&error);
1394 if (activation_failed)
1396 if (uses_servicehelper)
1397 handle_servicehelper_exit_error (exit_code, &error);
1399 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1404 activation_failed = TRUE;
1407 if (activation_failed)
1409 bus_context_log (pending_activation->activation->context,
1410 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1411 pending_activation->service_name,
1414 /* Destroy all pending activations with the same exec */
1415 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1417 while (_dbus_hash_iter_next (&iter))
1419 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1421 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1422 pending_activation_failed (p, &error);
1425 /* Destroys the pending activation */
1426 pending_activation_failed (pending_activation, &error);
1428 dbus_error_free (&error);
1432 _dbus_babysitter_unref (babysitter);
1438 add_babysitter_watch (DBusWatch *watch,
1441 BusPendingActivation *pending_activation = data;
1443 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1444 watch, babysitter_watch_callback, pending_activation,
1449 remove_babysitter_watch (DBusWatch *watch,
1452 BusPendingActivation *pending_activation = data;
1454 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1455 watch, babysitter_watch_callback, pending_activation);
1459 pending_activation_timed_out (void *data)
1461 BusPendingActivation *pending_activation = data;
1464 /* Kill the spawned process, since it sucks
1465 * (not sure this is what we want to do, but
1466 * may as well try it for now)
1468 if (pending_activation->babysitter)
1469 _dbus_babysitter_kill_child (pending_activation->babysitter);
1471 dbus_error_init (&error);
1473 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1474 "Activation of %s timed out",
1475 pending_activation->service_name);
1476 bus_context_log (pending_activation->activation->context,
1477 DBUS_SYSTEM_LOG_INFO,
1478 "Failed to activate service '%s': timed out",
1479 pending_activation->service_name);
1481 pending_activation_failed (pending_activation, &error);
1483 dbus_error_free (&error);
1489 cancel_pending (void *data)
1491 BusPendingActivation *pending_activation = data;
1493 _dbus_verbose ("Canceling pending activation of %s\n",
1494 pending_activation->service_name);
1496 if (pending_activation->babysitter)
1497 _dbus_babysitter_kill_child (pending_activation->babysitter);
1499 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1500 pending_activation->service_name);
1504 free_pending_cancel_data (void *data)
1506 BusPendingActivation *pending_activation = data;
1508 bus_pending_activation_unref (pending_activation);
1512 add_cancel_pending_to_transaction (BusTransaction *transaction,
1513 BusPendingActivation *pending_activation)
1515 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1517 free_pending_cancel_data))
1520 bus_pending_activation_ref (pending_activation);
1522 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1528 update_service_cache (BusActivation *activation, DBusError *error)
1532 _dbus_hash_iter_init (activation->directories, &iter);
1533 while (_dbus_hash_iter_next (&iter))
1535 DBusError tmp_error;
1536 BusServiceDirectory *s_dir;
1538 s_dir = _dbus_hash_iter_get_value (&iter);
1540 dbus_error_init (&tmp_error);
1541 if (!update_directory (activation, s_dir, &tmp_error))
1543 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1545 dbus_move_error (&tmp_error, error);
1549 dbus_error_free (&tmp_error);
1557 static BusActivationEntry *
1558 activation_find_entry (BusActivation *activation,
1559 const char *service_name,
1562 BusActivationEntry *entry;
1564 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1567 if (!update_service_cache (activation, error))
1570 entry = _dbus_hash_table_lookup_string (activation->entries,
1575 BusActivationEntry *updated_entry;
1577 if (!check_service_file (activation, entry, &updated_entry, error))
1580 entry = updated_entry;
1585 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1586 "The name %s was not provided by any .service files",
1595 bus_activation_get_environment (BusActivation *activation)
1602 length = _dbus_hash_table_get_n_entries (activation->environment);
1604 environment = dbus_new0 (char *, length + 1);
1606 if (environment == NULL)
1610 _dbus_hash_iter_init (activation->environment, &iter);
1612 if (!_dbus_string_init (&entry))
1614 dbus_free_string_array (environment);
1618 while (_dbus_hash_iter_next (&iter))
1620 const char *key, *value;
1622 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1623 value = (const char *) _dbus_hash_iter_get_value (&iter);
1625 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1628 if (!_dbus_string_steal_data (&entry, environment + i))
1633 _dbus_string_free (&entry);
1637 dbus_free_string_array (environment);
1645 bus_activation_set_environment_variable (BusActivation *activation,
1657 hash_key = _dbus_strdup (key);
1659 if (hash_key == NULL)
1662 hash_value = _dbus_strdup (value);
1664 if (hash_value == NULL)
1667 if (!_dbus_hash_table_insert_string (activation->environment,
1668 hash_key, hash_value))
1673 if (retval == FALSE)
1675 dbus_free (hash_key);
1676 dbus_free (hash_value);
1677 BUS_SET_OOM (error);
1684 bus_activation_activate_service (BusActivation *activation,
1685 DBusConnection *connection,
1686 BusTransaction *transaction,
1687 dbus_bool_t auto_activation,
1688 DBusMessage *activation_message,
1689 const char *service_name,
1692 DBusError tmp_error;
1693 BusActivationEntry *entry;
1694 BusPendingActivation *pending_activation;
1695 BusPendingActivationEntry *pending_activation_entry;
1696 DBusMessage *message;
1697 DBusString service_str;
1698 const char *servicehelper;
1704 dbus_bool_t was_pending_activation;
1707 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1709 if (activation->n_pending_activations >=
1710 bus_context_get_max_pending_activations (activation->context))
1712 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1713 "The maximum number of pending activations has been reached, activation of %s failed",
1718 entry = activation_find_entry (activation, service_name, error);
1722 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1723 * call us if the service is already active.
1725 if (!auto_activation)
1727 /* Check if the service is active */
1728 _dbus_string_init_const (&service_str, service_name);
1729 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1731 dbus_uint32_t result;
1733 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1735 message = dbus_message_new_method_return (activation_message);
1739 _dbus_verbose ("No memory to create reply to activate message\n");
1740 BUS_SET_OOM (error);
1744 result = DBUS_START_REPLY_ALREADY_RUNNING;
1746 if (!dbus_message_append_args (message,
1747 DBUS_TYPE_UINT32, &result,
1750 _dbus_verbose ("No memory to set args of reply to activate message\n");
1751 BUS_SET_OOM (error);
1752 dbus_message_unref (message);
1756 retval = bus_transaction_send_from_driver (transaction, connection, message);
1757 dbus_message_unref (message);
1760 _dbus_verbose ("Failed to send reply\n");
1761 BUS_SET_OOM (error);
1768 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1769 if (!pending_activation_entry)
1771 _dbus_verbose ("Failed to create pending activation entry\n");
1772 BUS_SET_OOM (error);
1776 pending_activation_entry->auto_activation = auto_activation;
1778 pending_activation_entry->activation_message = activation_message;
1779 dbus_message_ref (activation_message);
1780 pending_activation_entry->connection = connection;
1781 dbus_connection_ref (connection);
1783 /* Check if the service is being activated */
1784 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1785 was_pending_activation = (pending_activation != NULL);
1786 if (was_pending_activation)
1788 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1790 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1792 BUS_SET_OOM (error);
1793 bus_pending_activation_entry_free (pending_activation_entry);
1797 pending_activation->n_entries += 1;
1798 pending_activation->activation->n_pending_activations += 1;
1802 pending_activation = dbus_new0 (BusPendingActivation, 1);
1803 if (!pending_activation)
1805 _dbus_verbose ("Failed to create pending activation\n");
1807 BUS_SET_OOM (error);
1808 bus_pending_activation_entry_free (pending_activation_entry);
1812 pending_activation->activation = activation;
1813 pending_activation->refcount = 1;
1815 pending_activation->service_name = _dbus_strdup (service_name);
1816 if (!pending_activation->service_name)
1818 _dbus_verbose ("Failed to copy service name for pending activation\n");
1820 BUS_SET_OOM (error);
1821 bus_pending_activation_unref (pending_activation);
1822 bus_pending_activation_entry_free (pending_activation_entry);
1826 pending_activation->exec = _dbus_strdup (entry->exec);
1827 if (!pending_activation->exec)
1829 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1830 BUS_SET_OOM (error);
1831 bus_pending_activation_unref (pending_activation);
1832 bus_pending_activation_entry_free (pending_activation_entry);
1836 if (entry->systemd_service)
1838 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1839 if (!pending_activation->systemd_service)
1841 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1842 BUS_SET_OOM (error);
1843 bus_pending_activation_unref (pending_activation);
1844 bus_pending_activation_entry_free (pending_activation_entry);
1849 pending_activation->timeout =
1850 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1851 pending_activation_timed_out,
1854 if (!pending_activation->timeout)
1856 _dbus_verbose ("Failed to create timeout for pending activation\n");
1858 BUS_SET_OOM (error);
1859 bus_pending_activation_unref (pending_activation);
1860 bus_pending_activation_entry_free (pending_activation_entry);
1864 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1865 pending_activation->timeout,
1866 handle_timeout_callback,
1870 _dbus_verbose ("Failed to add timeout for pending activation\n");
1872 BUS_SET_OOM (error);
1873 bus_pending_activation_unref (pending_activation);
1874 bus_pending_activation_entry_free (pending_activation_entry);
1878 pending_activation->timeout_added = TRUE;
1880 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1882 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1884 BUS_SET_OOM (error);
1885 bus_pending_activation_unref (pending_activation);
1886 bus_pending_activation_entry_free (pending_activation_entry);
1890 pending_activation->n_entries += 1;
1891 pending_activation->activation->n_pending_activations += 1;
1893 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1894 pending_activation->service_name,
1895 pending_activation))
1897 _dbus_verbose ("Failed to put pending activation in hash table\n");
1899 BUS_SET_OOM (error);
1900 bus_pending_activation_unref (pending_activation);
1905 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1907 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1908 BUS_SET_OOM (error);
1909 _dbus_hash_table_remove_string (activation->pending_activations,
1910 pending_activation->service_name);
1915 if (was_pending_activation)
1918 if (bus_context_get_systemd_activation (activation->context))
1920 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1921 /* systemd itself is missing apparently. That can happen
1922 only during early startup. Let's just wait until systemd
1923 connects to us and do nothing. */
1926 if (entry->systemd_service)
1928 BusTransaction *activation_transaction;
1929 DBusString service_string;
1930 BusService *service;
1931 BusRegistry *registry;
1933 /* OK, we have a systemd service configured for this entry,
1934 hence let's enqueue an activation request message. This
1935 is implemented as a directed signal, not a method call,
1936 for three reasons: 1) we don't expect a response on
1937 success, where we just expect a name appearing on the
1938 bus; 2) at this time the systemd service might not yet
1939 have connected, so we wouldn't know the message serial at
1940 this point to set up a pending call; 3) it is ugly if the
1941 bus suddenly becomes the caller of a remote method. */
1943 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1944 "org.freedesktop.systemd1.Activator",
1945 "ActivationRequest");
1948 _dbus_verbose ("No memory to create activation message\n");
1949 BUS_SET_OOM (error);
1953 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1954 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1955 !dbus_message_append_args (message,
1956 DBUS_TYPE_STRING, &entry->systemd_service,
1959 _dbus_verbose ("No memory to set args of activation message\n");
1960 dbus_message_unref (message);
1961 BUS_SET_OOM (error);
1965 /* Create our transaction */
1966 activation_transaction = bus_transaction_new (activation->context);
1967 if (activation_transaction == NULL)
1969 _dbus_verbose ("No memory to create activation transaction\n");
1970 dbus_message_unref (message);
1971 BUS_SET_OOM (error);
1975 /* Check whether systemd is already connected */
1976 registry = bus_connection_get_registry (connection);
1977 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1978 service = bus_registry_lookup (registry, &service_string);
1980 if (service != NULL)
1982 bus_context_log (activation->context,
1983 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1985 entry->systemd_service);
1986 /* Wonderful, systemd is connected, let's just send the msg */
1987 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1992 bus_context_log (activation->context,
1993 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1995 entry->systemd_service);
1996 /* systemd is not around, let's "activate" it. */
1997 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1998 message, "org.freedesktop.systemd1", error);
2001 dbus_message_unref (message);
2005 bus_context_log (activation->context,
2006 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
2008 entry->systemd_service);
2009 _DBUS_ASSERT_ERROR_IS_SET (error);
2010 _dbus_verbose ("failed to send activation message: %s\n", error->name);
2011 bus_transaction_cancel_and_free (activation_transaction);
2015 bus_transaction_execute_and_free (activation_transaction);
2019 /* OK, we have no configured systemd service, hence let's
2020 proceed with traditional activation. */
2023 /* use command as system and session different */
2024 if (!_dbus_string_init (&command))
2026 BUS_SET_OOM (error);
2030 /* does the bus use a helper? */
2031 servicehelper = bus_context_get_servicehelper (activation->context);
2032 if (servicehelper != NULL)
2034 if (entry->user == NULL)
2036 _dbus_string_free (&command);
2037 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2038 "Cannot do system-bus activation with no user\n");
2042 /* join the helper path and the service name */
2043 if (!_dbus_string_append (&command, servicehelper))
2045 _dbus_string_free (&command);
2046 BUS_SET_OOM (error);
2049 if (!_dbus_string_append (&command, " "))
2051 _dbus_string_free (&command);
2052 BUS_SET_OOM (error);
2055 if (!_dbus_string_append (&command, service_name))
2057 _dbus_string_free (&command);
2058 BUS_SET_OOM (error);
2064 /* the bus does not use a helper, so we can append arguments with the exec line */
2065 if (!_dbus_string_append (&command, entry->exec))
2067 _dbus_string_free (&command);
2068 BUS_SET_OOM (error);
2073 /* convert command into arguments */
2074 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2076 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2077 _DBUS_ASSERT_ERROR_IS_SET (error);
2079 _dbus_hash_table_remove_string (activation->pending_activations,
2080 pending_activation->service_name);
2082 _dbus_string_free (&command);
2085 _dbus_string_free (&command);
2087 if (!add_bus_environment (activation, error))
2089 _DBUS_ASSERT_ERROR_IS_SET (error);
2090 dbus_free_string_array (argv);
2094 envp = bus_activation_get_environment (activation);
2098 BUS_SET_OOM (error);
2099 dbus_free_string_array (argv);
2103 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2104 if (servicehelper != NULL)
2105 bus_context_log (activation->context,
2106 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2109 bus_context_log (activation->context,
2110 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2113 dbus_error_init (&tmp_error);
2115 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2120 _dbus_verbose ("Failed to spawn child\n");
2121 bus_context_log (activation->context,
2122 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2125 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2126 dbus_move_error (&tmp_error, error);
2127 dbus_free_string_array (argv);
2128 dbus_free_string_array (envp);
2133 dbus_free_string_array (argv);
2136 _dbus_assert (pending_activation->babysitter != NULL);
2138 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2139 add_babysitter_watch,
2140 remove_babysitter_watch,
2145 BUS_SET_OOM (error);
2146 _dbus_verbose ("Failed to set babysitter watch functions\n");
2154 bus_activation_list_services (BusActivation *activation,
2162 len = _dbus_hash_table_get_n_entries (activation->entries);
2163 retval = dbus_new (char *, len + 1);
2168 _dbus_hash_iter_init (activation->entries, &iter);
2170 while (_dbus_hash_iter_next (&iter))
2172 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2174 retval[i] = _dbus_strdup (entry->name);
2175 if (retval[i] == NULL)
2190 for (j = 0; j < i; j++)
2191 dbus_free (retval[i]);
2198 dbus_activation_systemd_failure (BusActivation *activation,
2199 DBusMessage *message)
2202 const char *code, *str, *unit = NULL;
2204 dbus_error_init(&error);
2206 /* This is called whenever the systemd activator sent us a
2207 response. We'll invalidate all pending activations that match the
2210 if (dbus_message_get_args (message, &error,
2211 DBUS_TYPE_STRING, &unit,
2212 DBUS_TYPE_STRING, &code,
2213 DBUS_TYPE_STRING, &str,
2215 dbus_set_error(&error, code, str);
2222 bus_context_log (activation->context,
2223 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2227 _dbus_hash_iter_init (activation->pending_activations,
2230 while (_dbus_hash_iter_next (&iter))
2232 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2234 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2235 pending_activation_failed(p, &error);
2239 dbus_error_free(&error);
2244 #ifdef DBUS_BUILD_TESTS
2248 #define SERVICE_NAME_1 "MyService1"
2249 #define SERVICE_NAME_2 "MyService2"
2250 #define SERVICE_NAME_3 "MyService3"
2252 #define SERVICE_FILE_1 "service-1.service"
2253 #define SERVICE_FILE_2 "service-2.service"
2254 #define SERVICE_FILE_3 "service-3.service"
2257 test_create_service_file (DBusString *dir,
2258 const char *filename,
2262 DBusString file_name, full_path;
2264 dbus_bool_t ret_val;
2267 _dbus_string_init_const (&file_name, filename);
2269 if (!_dbus_string_init (&full_path))
2272 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2273 !_dbus_concat_dir_and_file (&full_path, &file_name))
2279 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2286 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2290 _dbus_string_free (&full_path);
2295 test_remove_service_file (DBusString *dir, const char *filename)
2297 DBusString file_name, full_path;
2298 dbus_bool_t ret_val;
2302 _dbus_string_init_const (&file_name, filename);
2304 if (!_dbus_string_init (&full_path))
2307 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2308 !_dbus_concat_dir_and_file (&full_path, &file_name))
2314 if (!_dbus_delete_file (&full_path, NULL))
2321 _dbus_string_free (&full_path);
2326 test_remove_directory (DBusString *dir)
2329 DBusString filename, full_path;
2330 dbus_bool_t ret_val;
2334 if (!_dbus_string_init (&filename))
2337 if (!_dbus_string_init (&full_path))
2339 _dbus_string_free (&filename);
2343 iter = _dbus_directory_open (dir, NULL);
2350 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2352 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2358 _dbus_directory_close (iter);
2360 if (!_dbus_delete_directory (dir, NULL))
2367 _dbus_string_free (&filename);
2368 _dbus_string_free (&full_path);
2374 init_service_reload_test (DBusString *dir)
2378 if (!_dbus_stat (dir, &stat_buf, NULL))
2380 if (!_dbus_create_directory (dir, NULL))
2385 if (!test_remove_directory (dir))
2388 if (!_dbus_create_directory (dir, NULL))
2392 /* Create one initial file */
2393 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2400 cleanup_service_reload_test (DBusString *dir)
2402 if (!test_remove_directory (dir))
2410 BusActivation *activation;
2411 const char *service_name;
2412 dbus_bool_t expecting_find;
2416 check_func (void *data)
2419 BusActivationEntry *entry;
2421 dbus_bool_t ret_val;
2426 dbus_error_init (&error);
2428 entry = activation_find_entry (d->activation, d->service_name, &error);
2431 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2437 if (d->expecting_find)
2441 dbus_error_free (&error);
2445 if (!d->expecting_find)
2453 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2458 err = !_dbus_test_oom_handling (description, check_func, data);
2460 err = !check_func (data);
2463 _dbus_assert_not_reached ("Test failed");
2469 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2471 BusActivation *activation;
2473 DBusList *directories;
2477 _dbus_string_init_const (&address, "");
2479 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2482 activation = bus_activation_new (NULL, &address, &directories, NULL);
2486 d.activation = activation;
2488 /* Check for existing service file */
2489 d.expecting_find = TRUE;
2490 d.service_name = SERVICE_NAME_1;
2492 if (!do_test ("Existing service file", oom_test, &d))
2495 /* Check for non-existing service file */
2496 d.expecting_find = FALSE;
2497 d.service_name = SERVICE_NAME_3;
2499 if (!do_test ("Nonexisting service file", oom_test, &d))
2502 /* Check for added service file */
2503 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2506 d.expecting_find = TRUE;
2507 d.service_name = SERVICE_NAME_2;
2509 if (!do_test ("Added service file", oom_test, &d))
2512 /* Check for removed service file */
2513 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2516 d.expecting_find = FALSE;
2517 d.service_name = SERVICE_FILE_2;
2519 if (!do_test ("Removed service file", oom_test, &d))
2522 /* Check for updated service file */
2524 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2526 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2529 d.expecting_find = TRUE;
2530 d.service_name = SERVICE_NAME_3;
2532 if (!do_test ("Updated service file, part 1", oom_test, &d))
2535 d.expecting_find = FALSE;
2536 d.service_name = SERVICE_NAME_1;
2538 if (!do_test ("Updated service file, part 2", oom_test, &d))
2541 bus_activation_unref (activation);
2542 _dbus_list_clear (&directories);
2548 bus_activation_service_reload_test (const DBusString *test_data_dir)
2550 DBusString directory;
2552 if (!_dbus_string_init (&directory))
2555 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2558 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2559 !_dbus_generate_random_ascii (&directory, 6))
2564 /* Do normal tests */
2565 if (!init_service_reload_test (&directory))
2566 _dbus_assert_not_reached ("could not initiate service reload test");
2568 if (!do_service_reload_test (&directory, FALSE))
2572 if (!init_service_reload_test (&directory))
2573 _dbus_assert_not_reached ("could not initiate service reload test");
2575 if (!do_service_reload_test (&directory, TRUE))
2578 /* Cleanup test directory */
2579 if (!cleanup_service_reload_test (&directory))
2582 _dbus_string_free (&directory);
2587 #endif /* DBUS_BUILD_TESTS */