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 /* Normally a method call, but if connection is NULL, this is a signal
86 DBusMessage *activation_message;
87 /* NULL if this activation entry is for the dbus-daemon itself,
88 * waiting for systemd to start. In this case, auto_activation is always
91 DBusConnection *connection;
93 dbus_bool_t auto_activation;
99 BusActivation *activation;
102 char *systemd_service;
105 DBusBabysitter *babysitter;
106 DBusTimeout *timeout;
107 unsigned int timeout_added : 1;
108 } BusPendingActivation;
111 static BusServiceDirectory *
112 bus_service_directory_ref (BusServiceDirectory *dir)
114 _dbus_assert (dir->refcount);
123 bus_service_directory_unref (BusServiceDirectory *dir)
128 _dbus_assert (dir->refcount > 0);
131 if (dir->refcount > 0)
135 _dbus_hash_table_unref (dir->entries);
137 dbus_free (dir->dir_c);
142 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
144 if (entry->activation_message)
145 dbus_message_unref (entry->activation_message);
147 if (entry->connection)
148 dbus_connection_unref (entry->connection);
153 static BusPendingActivation *
154 bus_pending_activation_ref (BusPendingActivation *pending_activation)
156 _dbus_assert (pending_activation->refcount > 0);
157 pending_activation->refcount += 1;
159 return pending_activation;
163 bus_pending_activation_unref (BusPendingActivation *pending_activation)
167 if (pending_activation == NULL) /* hash table requires this */
170 _dbus_assert (pending_activation->refcount > 0);
171 pending_activation->refcount -= 1;
173 if (pending_activation->refcount > 0)
176 if (pending_activation->timeout_added)
178 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
179 pending_activation->timeout);
180 pending_activation->timeout_added = FALSE;
183 if (pending_activation->timeout)
184 _dbus_timeout_unref (pending_activation->timeout);
186 if (pending_activation->babysitter)
188 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
190 pending_activation->babysitter,
192 _dbus_assert_not_reached ("setting watch functions to NULL failed");
194 _dbus_babysitter_unref (pending_activation->babysitter);
197 dbus_free (pending_activation->service_name);
198 dbus_free (pending_activation->exec);
199 dbus_free (pending_activation->systemd_service);
201 link = _dbus_list_get_first_link (&pending_activation->entries);
205 BusPendingActivationEntry *entry = link->data;
207 bus_pending_activation_entry_free (entry);
209 link = _dbus_list_get_next_link (&pending_activation->entries, link);
211 _dbus_list_clear (&pending_activation->entries);
213 pending_activation->activation->n_pending_activations -=
214 pending_activation->n_entries;
216 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
218 dbus_free (pending_activation);
221 static BusActivationEntry *
222 bus_activation_entry_ref (BusActivationEntry *entry)
224 _dbus_assert (entry->refcount > 0);
231 bus_activation_entry_unref (BusActivationEntry *entry)
233 if (entry == NULL) /* hash table requires this */
236 _dbus_assert (entry->refcount > 0);
239 if (entry->refcount > 0)
242 dbus_free (entry->name);
243 dbus_free (entry->exec);
244 dbus_free (entry->user);
245 dbus_free (entry->filename);
246 dbus_free (entry->systemd_service);
252 update_desktop_file_entry (BusActivation *activation,
253 BusServiceDirectory *s_dir,
254 DBusString *filename,
255 BusDesktopFile *desktop_file,
258 char *name, *exec, *user, *exec_tmp, *systemd_service;
259 BusActivationEntry *entry;
261 DBusString file_path;
265 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
273 systemd_service = NULL;
275 dbus_error_init (&tmp_error);
277 if (!_dbus_string_init (&file_path))
283 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
284 !_dbus_concat_dir_and_file (&file_path, filename))
290 if (!_dbus_stat (&file_path, &stat_buf, NULL))
292 dbus_set_error (error, DBUS_ERROR_FAILED,
293 "Can't stat the service file\n");
297 if (!bus_desktop_file_get_string (desktop_file,
298 DBUS_SERVICE_SECTION,
304 if (!bus_desktop_file_get_string (desktop_file,
305 DBUS_SERVICE_SECTION,
311 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
312 dbus_free (exec_tmp);
315 /* user is not _required_ unless we are using system activation */
316 if (!bus_desktop_file_get_string (desktop_file,
317 DBUS_SERVICE_SECTION,
321 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
322 /* if we got OOM, then exit */
323 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
325 dbus_move_error (&tmp_error, error);
330 /* if we have error because we didn't find anything then continue */
331 dbus_error_free (&tmp_error);
336 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
338 /* systemd service is never required */
339 if (!bus_desktop_file_get_string (desktop_file,
340 DBUS_SERVICE_SECTION,
341 DBUS_SERVICE_SYSTEMD_SERVICE,
342 &systemd_service, &tmp_error))
344 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
345 /* if we got OOM, then exit */
346 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
348 dbus_move_error (&tmp_error, error);
353 /* if we have error because we didn't find anything then continue */
354 dbus_error_free (&tmp_error);
355 dbus_free (systemd_service);
356 systemd_service = NULL;
360 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
362 entry = _dbus_hash_table_lookup_string (s_dir->entries,
363 _dbus_string_get_const_data (filename));
365 if (entry == NULL) /* New file */
367 /* FIXME we need a better-defined algorithm for which service file to
368 * pick than "whichever one is first in the directory listing"
370 if (_dbus_hash_table_lookup_string (activation->entries, name))
372 dbus_set_error (error, DBUS_ERROR_FAILED,
373 "Service %s already exists in activation entry list\n", name);
377 entry = dbus_new0 (BusActivationEntry, 1);
387 entry->systemd_service = systemd_service;
390 /* ownership has been transferred to entry, do not free separately */
394 systemd_service = NULL;
396 entry->s_dir = s_dir;
397 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
398 if (!entry->filename)
404 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
410 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
412 /* Revert the insertion in the entries table */
413 _dbus_hash_table_remove_string (activation->entries, entry->name);
418 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
420 else /* Just update the entry */
422 bus_activation_entry_ref (entry);
423 _dbus_hash_table_remove_string (activation->entries, entry->name);
425 if (_dbus_hash_table_lookup_string (activation->entries, name))
427 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
428 name, _dbus_string_get_const_data (&file_path));
429 dbus_set_error (error, DBUS_ERROR_FAILED,
430 "The new service name \"%s\" of service file \"%s\" is 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;
895 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
897 activation = dbus_new0 (BusActivation, 1);
898 if (activation == NULL)
904 activation->refcount = 1;
905 activation->context = context;
906 activation->n_pending_activations = 0;
908 if (!bus_activation_reload (activation, address, directories, error))
911 /* Initialize this hash table once, we don't want to lose pending
912 * activations on reload. */
913 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
914 (DBusFreeFunction)bus_pending_activation_unref);
916 if (activation->pending_activations == NULL)
922 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
923 (DBusFreeFunction) dbus_free,
924 (DBusFreeFunction) dbus_free);
926 if (activation->environment == NULL)
932 if (!populate_environment (activation))
941 bus_activation_unref (activation);
946 bus_activation_ref (BusActivation *activation)
948 _dbus_assert (activation->refcount > 0);
950 activation->refcount += 1;
956 bus_activation_unref (BusActivation *activation)
958 _dbus_assert (activation->refcount > 0);
960 activation->refcount -= 1;
962 if (activation->refcount > 0)
965 dbus_free (activation->server_address);
966 if (activation->entries)
967 _dbus_hash_table_unref (activation->entries);
968 if (activation->pending_activations)
969 _dbus_hash_table_unref (activation->pending_activations);
970 if (activation->directories)
971 _dbus_hash_table_unref (activation->directories);
972 if (activation->environment)
973 _dbus_hash_table_unref (activation->environment);
975 dbus_free (activation);
979 add_bus_environment (BusActivation *activation,
984 if (!bus_activation_set_environment_variable (activation,
985 "DBUS_STARTER_ADDRESS",
986 activation->server_address,
990 type = bus_context_get_type (activation->context);
993 if (!bus_activation_set_environment_variable (activation,
994 "DBUS_STARTER_BUS_TYPE", type,
998 if (strcmp (type, "session") == 0)
1000 if (!bus_activation_set_environment_variable (activation,
1001 "DBUS_SESSION_BUS_ADDRESS",
1002 activation->server_address,
1006 else if (strcmp (type, "system") == 0)
1008 if (!bus_activation_set_environment_variable (activation,
1009 "DBUS_SYSTEM_BUS_ADDRESS",
1010 activation->server_address,
1021 BusPendingActivation *pending_activation;
1022 DBusPreallocatedHash *hash_entry;
1023 } RestorePendingData;
1026 restore_pending (void *data)
1028 RestorePendingData *d = data;
1030 _dbus_assert (d->pending_activation != NULL);
1031 _dbus_assert (d->hash_entry != NULL);
1033 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1034 d->pending_activation->service_name,
1035 d->pending_activation->timeout_added);
1037 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1039 d->pending_activation->service_name, d->pending_activation);
1041 bus_pending_activation_ref (d->pending_activation);
1043 d->hash_entry = NULL;
1047 free_pending_restore_data (void *data)
1049 RestorePendingData *d = data;
1052 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1055 bus_pending_activation_unref (d->pending_activation);
1061 add_restore_pending_to_transaction (BusTransaction *transaction,
1062 BusPendingActivation *pending_activation)
1064 RestorePendingData *d;
1066 d = dbus_new (RestorePendingData, 1);
1070 d->pending_activation = pending_activation;
1071 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1073 bus_pending_activation_ref (d->pending_activation);
1075 if (d->hash_entry == NULL ||
1076 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1077 free_pending_restore_data))
1079 free_pending_restore_data (d);
1083 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1089 bus_activation_service_created (BusActivation *activation,
1090 const char *service_name,
1091 BusTransaction *transaction,
1094 BusPendingActivation *pending_activation;
1095 DBusMessage *message;
1098 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1100 /* Check if it's a pending activation */
1101 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1103 if (!pending_activation)
1106 bus_context_log (activation->context,
1107 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1110 link = _dbus_list_get_first_link (&pending_activation->entries);
1111 while (link != NULL)
1113 BusPendingActivationEntry *entry = link->data;
1114 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1116 /* entry->connection is NULL for activating systemd */
1117 if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1119 /* Only send activation replies to regular activation requests. */
1120 if (!entry->auto_activation)
1122 dbus_uint32_t result;
1124 message = dbus_message_new_method_return (entry->activation_message);
1127 BUS_SET_OOM (error);
1131 result = DBUS_START_REPLY_SUCCESS;
1133 if (!dbus_message_append_args (message,
1134 DBUS_TYPE_UINT32, &result,
1137 dbus_message_unref (message);
1138 BUS_SET_OOM (error);
1142 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1144 dbus_message_unref (message);
1145 BUS_SET_OOM (error);
1149 dbus_message_unref (message);
1163 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1164 BusService *service,
1165 BusTransaction *transaction,
1168 BusPendingActivation *pending_activation;
1171 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1173 /* Check if it's a pending activation */
1174 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1175 bus_service_get_name (service));
1177 if (!pending_activation)
1180 link = _dbus_list_get_first_link (&pending_activation->entries);
1181 while (link != NULL)
1183 BusPendingActivationEntry *entry = link->data;
1184 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1186 if (entry->auto_activation && (entry->connection == NULL || dbus_connection_get_is_connected (entry->connection)))
1188 DBusConnection *addressed_recipient;
1190 /* kdbus change - we can not send anything using phantom connections
1191 * (DBusConnection structures for services other than daemon)
1192 * so we have to use daemon connection
1194 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1195 addressed_recipient = entry->connection;
1197 addressed_recipient = bus_service_get_primary_owners_connection (service);
1199 /* Resume dispatching where we left off in bus_dispatch() */
1200 if (!bus_dispatch_matches (transaction,
1202 addressed_recipient,
1203 entry->activation_message, error))
1210 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1212 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1213 BUS_SET_OOM (error);
1217 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1226 * FIXME @todo the error messages here would ideally be preallocated
1227 * so we don't need to allocate memory to send them.
1228 * Using the usual tactic, prealloc an OOM message, then
1229 * if we can't alloc the real error send the OOM error instead.
1232 try_send_activation_failure (BusPendingActivation *pending_activation,
1233 const DBusError *how)
1235 BusActivation *activation;
1237 BusTransaction *transaction;
1239 activation = pending_activation->activation;
1241 transaction = bus_transaction_new (activation->context);
1242 if (transaction == NULL)
1245 link = _dbus_list_get_first_link (&pending_activation->entries);
1246 while (link != NULL)
1248 BusPendingActivationEntry *entry = link->data;
1249 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1251 if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1253 if (!bus_transaction_send_error_reply (transaction,
1256 entry->activation_message))
1263 bus_transaction_execute_and_free (transaction);
1269 bus_transaction_cancel_and_free (transaction);
1274 * Free the pending activation and send an error message to all the
1275 * connections that were waiting for it.
1278 pending_activation_failed (BusPendingActivation *pending_activation,
1279 const DBusError *how)
1281 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1282 while (!try_send_activation_failure (pending_activation, how))
1283 _dbus_wait_for_memory ();
1285 /* Destroy this pending activation */
1286 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1287 pending_activation->service_name);
1291 * Depending on the exit code of the helper, set the error accordingly
1294 handle_servicehelper_exit_error (int exit_code,
1299 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1300 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1301 "Launcher could not run (out of memory)");
1303 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1304 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1305 "Failed to setup environment correctly");
1307 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1308 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1309 "Bus name is not valid or missing");
1311 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1312 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1313 "Bus name not found in system service directory");
1315 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1316 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1317 "The permission of the setuid helper is not correct");
1319 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1320 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1321 "The service file is incorrect or does not have all required attributes");
1323 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1324 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1325 "Cannot launch daemon, file not found or permissions invalid");
1327 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1328 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1329 "Invalid arguments to command line");
1331 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1332 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1333 "Launched child was signaled, it probably crashed");
1336 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1337 "Launch helper exited with unknown return code %i", exit_code);
1343 pending_activation_finished_cb (DBusBabysitter *babysitter,
1346 BusPendingActivation *pending_activation = data;
1347 dbus_bool_t uses_servicehelper;
1349 _dbus_assert (babysitter == pending_activation->babysitter);
1350 _dbus_babysitter_ref (babysitter);
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 /* strictly speaking this is redundant with the check in dbus-spawn now */
1363 if (_dbus_babysitter_get_child_exited (babysitter))
1367 dbus_bool_t activation_failed;
1370 dbus_error_init (&error);
1372 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1374 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1376 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1377 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1379 activation_failed = exit_code != 0;
1381 dbus_error_free(&error);
1383 if (activation_failed)
1385 if (uses_servicehelper)
1386 handle_servicehelper_exit_error (exit_code, &error);
1388 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1393 activation_failed = TRUE;
1396 if (activation_failed)
1398 bus_context_log (pending_activation->activation->context,
1399 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1400 pending_activation->service_name,
1403 /* Destroy all pending activations with the same exec */
1404 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1406 while (_dbus_hash_iter_next (&iter))
1408 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1410 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1411 pending_activation_failed (p, &error);
1414 /* Destroys the pending activation */
1415 pending_activation_failed (pending_activation, &error);
1417 dbus_error_free (&error);
1421 _dbus_babysitter_unref (babysitter);
1425 add_babysitter_watch (DBusWatch *watch,
1428 BusPendingActivation *pending_activation = data;
1430 return _dbus_loop_add_watch (
1431 bus_context_get_loop (pending_activation->activation->context),
1436 remove_babysitter_watch (DBusWatch *watch,
1439 BusPendingActivation *pending_activation = data;
1441 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1446 toggle_babysitter_watch (DBusWatch *watch,
1449 BusPendingActivation *pending_activation = data;
1451 _dbus_loop_toggle_watch (bus_context_get_loop (pending_activation->activation->context),
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;
1700 dbus_bool_t was_pending_activation;
1703 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1705 if (activation->n_pending_activations >=
1706 bus_context_get_max_pending_activations (activation->context))
1708 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1709 "The maximum number of pending activations has been reached, activation of %s failed",
1714 entry = activation_find_entry (activation, service_name, error);
1718 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1719 * call us if the service is already active.
1721 if (!auto_activation)
1723 /* Check if the service is active */
1724 _dbus_string_init_const (&service_str, service_name);
1725 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1727 dbus_uint32_t result;
1729 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1731 message = dbus_message_new_method_return (activation_message);
1735 _dbus_verbose ("No memory to create reply to activate message\n");
1736 BUS_SET_OOM (error);
1740 result = DBUS_START_REPLY_ALREADY_RUNNING;
1742 if (!dbus_message_append_args (message,
1743 DBUS_TYPE_UINT32, &result,
1746 _dbus_verbose ("No memory to set args of reply to activate message\n");
1747 BUS_SET_OOM (error);
1748 dbus_message_unref (message);
1752 retval = bus_transaction_send_from_driver (transaction, connection, message);
1753 dbus_message_unref (message);
1756 _dbus_verbose ("Failed to send reply\n");
1757 BUS_SET_OOM (error);
1764 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1765 if (!pending_activation_entry)
1767 _dbus_verbose ("Failed to create pending activation entry\n");
1768 BUS_SET_OOM (error);
1772 pending_activation_entry->auto_activation = auto_activation;
1774 pending_activation_entry->activation_message = activation_message;
1775 dbus_message_ref (activation_message);
1776 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))
1864 _dbus_verbose ("Failed to add timeout for pending activation\n");
1866 BUS_SET_OOM (error);
1867 bus_pending_activation_unref (pending_activation);
1868 bus_pending_activation_entry_free (pending_activation_entry);
1872 pending_activation->timeout_added = TRUE;
1874 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1876 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1878 BUS_SET_OOM (error);
1879 bus_pending_activation_unref (pending_activation);
1880 bus_pending_activation_entry_free (pending_activation_entry);
1884 pending_activation->n_entries += 1;
1885 pending_activation->activation->n_pending_activations += 1;
1887 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1888 pending_activation->service_name,
1889 pending_activation))
1891 _dbus_verbose ("Failed to put pending activation in hash table\n");
1893 BUS_SET_OOM (error);
1894 bus_pending_activation_unref (pending_activation);
1899 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1901 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1902 BUS_SET_OOM (error);
1903 _dbus_hash_table_remove_string (activation->pending_activations,
1904 pending_activation->service_name);
1909 if (was_pending_activation)
1912 if (bus_context_get_systemd_activation (activation->context))
1914 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1915 /* systemd itself is missing apparently. That can happen
1916 only during early startup. Let's just wait until systemd
1917 connects to us and do nothing. */
1920 if (entry->systemd_service)
1922 BusTransaction *activation_transaction;
1923 DBusString service_string;
1924 BusService *service;
1925 BusRegistry *registry;
1927 /* OK, we have a systemd service configured for this entry,
1928 hence let's enqueue an activation request message. This
1929 is implemented as a directed signal, not a method call,
1930 for three reasons: 1) we don't expect a response on
1931 success, where we just expect a name appearing on the
1932 bus; 2) at this time the systemd service might not yet
1933 have connected, so we wouldn't know the message serial at
1934 this point to set up a pending call; 3) it is ugly if the
1935 bus suddenly becomes the caller of a remote method. */
1937 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1938 "org.freedesktop.systemd1.Activator",
1939 "ActivationRequest");
1942 _dbus_verbose ("No memory to create activation message\n");
1943 BUS_SET_OOM (error);
1947 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1948 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1949 !dbus_message_append_args (message,
1950 DBUS_TYPE_STRING, &entry->systemd_service,
1953 _dbus_verbose ("No memory to set args of activation message\n");
1954 dbus_message_unref (message);
1955 BUS_SET_OOM (error);
1959 /* Create our transaction */
1960 activation_transaction = bus_transaction_new (activation->context);
1961 if (activation_transaction == NULL)
1963 _dbus_verbose ("No memory to create activation transaction\n");
1964 dbus_message_unref (message);
1965 BUS_SET_OOM (error);
1969 /* Check whether systemd is already connected */
1970 registry = bus_connection_get_registry (connection);
1971 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1972 service = bus_registry_lookup (registry, &service_string);
1974 if (service != NULL)
1976 bus_context_log (activation->context,
1977 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1979 entry->systemd_service);
1980 /* Wonderful, systemd is connected, let's just send the msg */
1981 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1986 bus_context_log (activation->context,
1987 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1989 entry->systemd_service);
1990 /* systemd is not around, let's "activate" it. */
1991 retval = bus_activation_activate_service (activation, NULL, activation_transaction, TRUE,
1992 message, "org.freedesktop.systemd1", error);
1995 dbus_message_unref (message);
1999 bus_context_log (activation->context,
2000 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
2002 entry->systemd_service);
2003 _DBUS_ASSERT_ERROR_IS_SET (error);
2004 _dbus_verbose ("failed to send activation message: %s\n", error->name);
2005 bus_transaction_cancel_and_free (activation_transaction);
2009 bus_transaction_execute_and_free (activation_transaction);
2013 /* OK, we have no configured systemd service, hence let's
2014 proceed with traditional activation. */
2017 /* use command as system and session different */
2018 if (!_dbus_string_init (&command))
2020 BUS_SET_OOM (error);
2024 /* does the bus use a helper? */
2025 servicehelper = bus_context_get_servicehelper (activation->context);
2026 if (servicehelper != NULL)
2028 if (entry->user == NULL)
2030 _dbus_string_free (&command);
2031 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2032 "Cannot do system-bus activation with no user\n");
2036 /* join the helper path and the service name */
2037 if (!_dbus_string_append (&command, servicehelper))
2039 _dbus_string_free (&command);
2040 BUS_SET_OOM (error);
2043 if (!_dbus_string_append (&command, " "))
2045 _dbus_string_free (&command);
2046 BUS_SET_OOM (error);
2049 if (!_dbus_string_append (&command, service_name))
2051 _dbus_string_free (&command);
2052 BUS_SET_OOM (error);
2058 /* the bus does not use a helper, so we can append arguments with the exec line */
2059 if (!_dbus_string_append (&command, entry->exec))
2061 _dbus_string_free (&command);
2062 BUS_SET_OOM (error);
2067 /* convert command into arguments */
2068 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2070 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2071 _DBUS_ASSERT_ERROR_IS_SET (error);
2073 _dbus_hash_table_remove_string (activation->pending_activations,
2074 pending_activation->service_name);
2076 _dbus_string_free (&command);
2079 _dbus_string_free (&command);
2081 if (!add_bus_environment (activation, error))
2083 _DBUS_ASSERT_ERROR_IS_SET (error);
2084 dbus_free_string_array (argv);
2088 envp = bus_activation_get_environment (activation);
2092 BUS_SET_OOM (error);
2093 dbus_free_string_array (argv);
2097 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2098 if (servicehelper != NULL)
2099 bus_context_log (activation->context,
2100 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2103 bus_context_log (activation->context,
2104 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2107 dbus_error_init (&tmp_error);
2109 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2114 _dbus_verbose ("Failed to spawn child\n");
2115 bus_context_log (activation->context,
2116 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2119 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2120 dbus_move_error (&tmp_error, error);
2121 dbus_free_string_array (argv);
2122 dbus_free_string_array (envp);
2127 dbus_free_string_array (argv);
2130 _dbus_assert (pending_activation->babysitter != NULL);
2132 _dbus_babysitter_set_result_function (pending_activation->babysitter,
2133 pending_activation_finished_cb,
2134 pending_activation);
2136 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2137 add_babysitter_watch,
2138 remove_babysitter_watch,
2139 toggle_babysitter_watch,
2143 BUS_SET_OOM (error);
2144 _dbus_verbose ("Failed to set babysitter watch functions\n");
2152 bus_activation_list_services (BusActivation *activation,
2160 len = _dbus_hash_table_get_n_entries (activation->entries);
2161 retval = dbus_new (char *, len + 1);
2166 _dbus_hash_iter_init (activation->entries, &iter);
2168 while (_dbus_hash_iter_next (&iter))
2170 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2172 retval[i] = _dbus_strdup (entry->name);
2173 if (retval[i] == NULL)
2188 for (j = 0; j < i; j++)
2189 dbus_free (retval[j]);
2196 dbus_activation_systemd_failure (BusActivation *activation,
2197 DBusMessage *message)
2200 const char *code, *str, *unit = NULL;
2202 dbus_error_init(&error);
2204 /* This is called whenever the systemd activator sent us a
2205 response. We'll invalidate all pending activations that match the
2208 if (dbus_message_get_args (message, &error,
2209 DBUS_TYPE_STRING, &unit,
2210 DBUS_TYPE_STRING, &code,
2211 DBUS_TYPE_STRING, &str,
2213 dbus_set_error(&error, code, str);
2220 bus_context_log (activation->context,
2221 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2225 _dbus_hash_iter_init (activation->pending_activations,
2228 while (_dbus_hash_iter_next (&iter))
2230 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2232 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2233 pending_activation_failed(p, &error);
2237 dbus_error_free(&error);
2242 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2246 #define SERVICE_NAME_1 "MyService1"
2247 #define SERVICE_NAME_2 "MyService2"
2248 #define SERVICE_NAME_3 "MyService3"
2250 #define SERVICE_FILE_1 "service-1.service"
2251 #define SERVICE_FILE_2 "service-2.service"
2252 #define SERVICE_FILE_3 "service-3.service"
2255 test_create_service_file (DBusString *dir,
2256 const char *filename,
2260 DBusString file_name, full_path;
2262 dbus_bool_t ret_val;
2265 _dbus_string_init_const (&file_name, filename);
2267 if (!_dbus_string_init (&full_path))
2270 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2271 !_dbus_concat_dir_and_file (&full_path, &file_name))
2277 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2284 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2288 _dbus_string_free (&full_path);
2293 test_remove_service_file (DBusString *dir, const char *filename)
2295 DBusString file_name, full_path;
2296 dbus_bool_t ret_val;
2300 _dbus_string_init_const (&file_name, filename);
2302 if (!_dbus_string_init (&full_path))
2305 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2306 !_dbus_concat_dir_and_file (&full_path, &file_name))
2312 if (!_dbus_delete_file (&full_path, NULL))
2319 _dbus_string_free (&full_path);
2324 test_remove_directory (DBusString *dir)
2327 DBusString filename, full_path;
2328 dbus_bool_t ret_val;
2332 if (!_dbus_string_init (&filename))
2335 if (!_dbus_string_init (&full_path))
2337 _dbus_string_free (&filename);
2341 iter = _dbus_directory_open (dir, NULL);
2348 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2350 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2356 _dbus_directory_close (iter);
2358 if (!_dbus_delete_directory (dir, NULL))
2365 _dbus_string_free (&filename);
2366 _dbus_string_free (&full_path);
2372 init_service_reload_test (DBusString *dir)
2376 if (!_dbus_stat (dir, &stat_buf, NULL))
2378 if (!_dbus_create_directory (dir, NULL))
2383 if (!test_remove_directory (dir))
2386 if (!_dbus_create_directory (dir, NULL))
2390 /* Create one initial file */
2391 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2398 cleanup_service_reload_test (DBusString *dir)
2400 if (!test_remove_directory (dir))
2408 BusActivation *activation;
2409 const char *service_name;
2410 dbus_bool_t expecting_find;
2414 check_func (void *data)
2417 BusActivationEntry *entry;
2419 dbus_bool_t ret_val;
2424 dbus_error_init (&error);
2426 entry = activation_find_entry (d->activation, d->service_name, &error);
2429 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2435 if (d->expecting_find)
2439 dbus_error_free (&error);
2443 if (!d->expecting_find)
2451 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2456 err = !_dbus_test_oom_handling (description, check_func, data);
2458 err = !check_func (data);
2461 _dbus_assert_not_reached ("Test failed");
2467 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2469 BusActivation *activation;
2471 DBusList *directories;
2475 _dbus_string_init_const (&address, "");
2477 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2480 activation = bus_activation_new (NULL, &address, &directories, NULL);
2484 d.activation = activation;
2486 /* Check for existing service file */
2487 d.expecting_find = TRUE;
2488 d.service_name = SERVICE_NAME_1;
2490 if (!do_test ("Existing service file", oom_test, &d))
2493 /* Check for non-existing service file */
2494 d.expecting_find = FALSE;
2495 d.service_name = SERVICE_NAME_3;
2497 if (!do_test ("Nonexisting service file", oom_test, &d))
2500 /* Check for added service file */
2501 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2504 d.expecting_find = TRUE;
2505 d.service_name = SERVICE_NAME_2;
2507 if (!do_test ("Added service file", oom_test, &d))
2510 /* Check for removed service file */
2511 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2514 d.expecting_find = FALSE;
2515 d.service_name = SERVICE_FILE_2;
2517 if (!do_test ("Removed service file", oom_test, &d))
2520 /* Check for updated service file */
2522 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2524 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2527 d.expecting_find = TRUE;
2528 d.service_name = SERVICE_NAME_3;
2530 if (!do_test ("Updated service file, part 1", oom_test, &d))
2533 d.expecting_find = FALSE;
2534 d.service_name = SERVICE_NAME_1;
2536 if (!do_test ("Updated service file, part 2", oom_test, &d))
2539 bus_activation_unref (activation);
2540 _dbus_list_clear (&directories);
2546 bus_activation_service_reload_test (const DBusString *test_data_dir)
2548 DBusString directory;
2551 if (!_dbus_string_init (&directory))
2554 tmp = _dbus_get_tmpdir ();
2559 if (!_dbus_string_append (&directory, tmp))
2562 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2563 !_dbus_generate_random_ascii (&directory, 6))
2568 /* Do normal tests */
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, FALSE))
2578 if (!init_service_reload_test (&directory))
2579 _dbus_assert_not_reached ("could not initiate service reload test");
2581 if (!do_service_reload_test (&directory, TRUE))
2586 /* Cleanup test directory */
2587 if (!cleanup_service_reload_test (&directory))
2590 _dbus_string_free (&directory);
2595 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */