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 addressed_recipient = bus_service_get_primary_owners_connection (service);
1192 /* Resume dispatching where we left off in bus_dispatch() */
1193 if (!bus_dispatch_matches (transaction,
1195 addressed_recipient,
1196 entry->activation_message, error))
1203 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1205 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1206 BUS_SET_OOM (error);
1210 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1219 * FIXME @todo the error messages here would ideally be preallocated
1220 * so we don't need to allocate memory to send them.
1221 * Using the usual tactic, prealloc an OOM message, then
1222 * if we can't alloc the real error send the OOM error instead.
1225 try_send_activation_failure (BusPendingActivation *pending_activation,
1226 const DBusError *how)
1228 BusActivation *activation;
1230 BusTransaction *transaction;
1232 activation = pending_activation->activation;
1234 transaction = bus_transaction_new (activation->context);
1235 if (transaction == NULL)
1238 link = _dbus_list_get_first_link (&pending_activation->entries);
1239 while (link != NULL)
1241 BusPendingActivationEntry *entry = link->data;
1242 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1244 if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1246 if (!bus_transaction_send_error_reply (transaction,
1249 entry->activation_message))
1256 bus_transaction_execute_and_free (transaction);
1262 bus_transaction_cancel_and_free (transaction);
1267 * Free the pending activation and send an error message to all the
1268 * connections that were waiting for it.
1271 pending_activation_failed (BusPendingActivation *pending_activation,
1272 const DBusError *how)
1274 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1275 while (!try_send_activation_failure (pending_activation, how))
1276 _dbus_wait_for_memory ();
1278 /* Destroy this pending activation */
1279 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1280 pending_activation->service_name);
1284 * Depending on the exit code of the helper, set the error accordingly
1287 handle_servicehelper_exit_error (int exit_code,
1292 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1293 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1294 "Launcher could not run (out of memory)");
1296 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1297 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1298 "Failed to setup environment correctly");
1300 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1301 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1302 "Bus name is not valid or missing");
1304 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1305 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1306 "Bus name not found in system service directory");
1308 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1309 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1310 "The permission of the setuid helper is not correct");
1312 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1313 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1314 "The service file is incorrect or does not have all required attributes");
1316 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1317 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1318 "Cannot launch daemon, file not found or permissions invalid");
1320 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1321 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1322 "Invalid arguments to command line");
1324 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1325 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1326 "Launched child was signaled, it probably crashed");
1329 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1330 "Launch helper exited with unknown return code %i", exit_code);
1336 pending_activation_finished_cb (DBusBabysitter *babysitter,
1339 BusPendingActivation *pending_activation = data;
1340 dbus_bool_t uses_servicehelper;
1342 _dbus_assert (babysitter == pending_activation->babysitter);
1343 _dbus_babysitter_ref (babysitter);
1345 /* There are two major cases here; are we the system bus or the session? Here this
1346 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1347 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1349 * In both cases though, just ignore when a process exits with status 0; it's possible for
1350 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1351 * condition between this code and the child process claiming the bus name.
1353 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1355 /* strictly speaking this is redundant with the check in dbus-spawn now */
1356 if (_dbus_babysitter_get_child_exited (babysitter))
1360 dbus_bool_t activation_failed;
1363 dbus_error_init (&error);
1365 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1367 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1369 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1370 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1372 activation_failed = exit_code != 0;
1374 dbus_error_free(&error);
1376 if (activation_failed)
1378 if (uses_servicehelper)
1379 handle_servicehelper_exit_error (exit_code, &error);
1381 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1386 activation_failed = TRUE;
1389 if (activation_failed)
1391 bus_context_log (pending_activation->activation->context,
1392 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1393 pending_activation->service_name,
1396 /* Destroy all pending activations with the same exec */
1397 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1399 while (_dbus_hash_iter_next (&iter))
1401 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1403 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1404 pending_activation_failed (p, &error);
1407 /* Destroys the pending activation */
1408 pending_activation_failed (pending_activation, &error);
1410 dbus_error_free (&error);
1414 _dbus_babysitter_unref (babysitter);
1418 add_babysitter_watch (DBusWatch *watch,
1421 BusPendingActivation *pending_activation = data;
1423 return _dbus_loop_add_watch (
1424 bus_context_get_loop (pending_activation->activation->context),
1429 remove_babysitter_watch (DBusWatch *watch,
1432 BusPendingActivation *pending_activation = data;
1434 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1439 toggle_babysitter_watch (DBusWatch *watch,
1442 BusPendingActivation *pending_activation = data;
1444 _dbus_loop_toggle_watch (bus_context_get_loop (pending_activation->activation->context),
1449 pending_activation_timed_out (void *data)
1451 BusPendingActivation *pending_activation = data;
1454 /* Kill the spawned process, since it sucks
1455 * (not sure this is what we want to do, but
1456 * may as well try it for now)
1458 if (pending_activation->babysitter)
1459 _dbus_babysitter_kill_child (pending_activation->babysitter);
1461 dbus_error_init (&error);
1463 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1464 "Activation of %s timed out",
1465 pending_activation->service_name);
1466 bus_context_log (pending_activation->activation->context,
1467 DBUS_SYSTEM_LOG_INFO,
1468 "Failed to activate service '%s': timed out",
1469 pending_activation->service_name);
1471 pending_activation_failed (pending_activation, &error);
1473 dbus_error_free (&error);
1479 cancel_pending (void *data)
1481 BusPendingActivation *pending_activation = data;
1483 _dbus_verbose ("Canceling pending activation of %s\n",
1484 pending_activation->service_name);
1486 if (pending_activation->babysitter)
1487 _dbus_babysitter_kill_child (pending_activation->babysitter);
1489 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1490 pending_activation->service_name);
1494 free_pending_cancel_data (void *data)
1496 BusPendingActivation *pending_activation = data;
1498 bus_pending_activation_unref (pending_activation);
1502 add_cancel_pending_to_transaction (BusTransaction *transaction,
1503 BusPendingActivation *pending_activation)
1505 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1507 free_pending_cancel_data))
1510 bus_pending_activation_ref (pending_activation);
1512 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1518 update_service_cache (BusActivation *activation, DBusError *error)
1522 _dbus_hash_iter_init (activation->directories, &iter);
1523 while (_dbus_hash_iter_next (&iter))
1525 DBusError tmp_error;
1526 BusServiceDirectory *s_dir;
1528 s_dir = _dbus_hash_iter_get_value (&iter);
1530 dbus_error_init (&tmp_error);
1531 if (!update_directory (activation, s_dir, &tmp_error))
1533 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1535 dbus_move_error (&tmp_error, error);
1539 dbus_error_free (&tmp_error);
1547 static BusActivationEntry *
1548 activation_find_entry (BusActivation *activation,
1549 const char *service_name,
1552 BusActivationEntry *entry;
1554 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1557 if (!update_service_cache (activation, error))
1560 entry = _dbus_hash_table_lookup_string (activation->entries,
1565 BusActivationEntry *updated_entry;
1567 if (!check_service_file (activation, entry, &updated_entry, error))
1570 entry = updated_entry;
1575 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1576 "The name %s was not provided by any .service files",
1585 bus_activation_get_environment (BusActivation *activation)
1592 length = _dbus_hash_table_get_n_entries (activation->environment);
1594 environment = dbus_new0 (char *, length + 1);
1596 if (environment == NULL)
1600 _dbus_hash_iter_init (activation->environment, &iter);
1602 if (!_dbus_string_init (&entry))
1604 dbus_free_string_array (environment);
1608 while (_dbus_hash_iter_next (&iter))
1610 const char *key, *value;
1612 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1613 value = (const char *) _dbus_hash_iter_get_value (&iter);
1615 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1618 if (!_dbus_string_steal_data (&entry, environment + i))
1623 _dbus_string_free (&entry);
1627 dbus_free_string_array (environment);
1635 bus_activation_set_environment_variable (BusActivation *activation,
1647 hash_key = _dbus_strdup (key);
1649 if (hash_key == NULL)
1652 hash_value = _dbus_strdup (value);
1654 if (hash_value == NULL)
1657 if (!_dbus_hash_table_insert_string (activation->environment,
1658 hash_key, hash_value))
1663 if (retval == FALSE)
1665 dbus_free (hash_key);
1666 dbus_free (hash_value);
1667 BUS_SET_OOM (error);
1674 bus_activation_activate_service (BusActivation *activation,
1675 DBusConnection *connection,
1676 BusTransaction *transaction,
1677 dbus_bool_t auto_activation,
1678 DBusMessage *activation_message,
1679 const char *service_name,
1682 DBusError tmp_error;
1683 BusActivationEntry *entry;
1684 BusPendingActivation *pending_activation;
1685 BusPendingActivationEntry *pending_activation_entry;
1686 DBusMessage *message;
1687 DBusString service_str;
1688 const char *servicehelper;
1693 dbus_bool_t was_pending_activation;
1696 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1698 if (activation->n_pending_activations >=
1699 bus_context_get_max_pending_activations (activation->context))
1701 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1702 "The maximum number of pending activations has been reached, activation of %s failed",
1707 entry = activation_find_entry (activation, service_name, error);
1711 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1712 * call us if the service is already active.
1714 if (!auto_activation)
1716 /* Check if the service is active */
1717 _dbus_string_init_const (&service_str, service_name);
1718 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1720 dbus_uint32_t result;
1722 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1724 message = dbus_message_new_method_return (activation_message);
1728 _dbus_verbose ("No memory to create reply to activate message\n");
1729 BUS_SET_OOM (error);
1733 result = DBUS_START_REPLY_ALREADY_RUNNING;
1735 if (!dbus_message_append_args (message,
1736 DBUS_TYPE_UINT32, &result,
1739 _dbus_verbose ("No memory to set args of reply to activate message\n");
1740 BUS_SET_OOM (error);
1741 dbus_message_unref (message);
1745 retval = bus_transaction_send_from_driver (transaction, connection, message);
1746 dbus_message_unref (message);
1749 _dbus_verbose ("Failed to send reply\n");
1750 BUS_SET_OOM (error);
1757 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1758 if (!pending_activation_entry)
1760 _dbus_verbose ("Failed to create pending activation entry\n");
1761 BUS_SET_OOM (error);
1765 pending_activation_entry->auto_activation = auto_activation;
1767 pending_activation_entry->activation_message = activation_message;
1768 dbus_message_ref (activation_message);
1769 pending_activation_entry->connection = connection;
1771 dbus_connection_ref (connection);
1773 /* Check if the service is being activated */
1774 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1775 was_pending_activation = (pending_activation != NULL);
1776 if (was_pending_activation)
1778 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1780 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1782 BUS_SET_OOM (error);
1783 bus_pending_activation_entry_free (pending_activation_entry);
1787 pending_activation->n_entries += 1;
1788 pending_activation->activation->n_pending_activations += 1;
1792 pending_activation = dbus_new0 (BusPendingActivation, 1);
1793 if (!pending_activation)
1795 _dbus_verbose ("Failed to create pending activation\n");
1797 BUS_SET_OOM (error);
1798 bus_pending_activation_entry_free (pending_activation_entry);
1802 pending_activation->activation = activation;
1803 pending_activation->refcount = 1;
1805 pending_activation->service_name = _dbus_strdup (service_name);
1806 if (!pending_activation->service_name)
1808 _dbus_verbose ("Failed to copy service name for pending activation\n");
1810 BUS_SET_OOM (error);
1811 bus_pending_activation_unref (pending_activation);
1812 bus_pending_activation_entry_free (pending_activation_entry);
1816 pending_activation->exec = _dbus_strdup (entry->exec);
1817 if (!pending_activation->exec)
1819 _dbus_verbose ("Failed to copy service exec 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 if (entry->systemd_service)
1828 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1829 if (!pending_activation->systemd_service)
1831 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1832 BUS_SET_OOM (error);
1833 bus_pending_activation_unref (pending_activation);
1834 bus_pending_activation_entry_free (pending_activation_entry);
1839 pending_activation->timeout =
1840 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1841 pending_activation_timed_out,
1844 if (!pending_activation->timeout)
1846 _dbus_verbose ("Failed to create timeout for pending activation\n");
1848 BUS_SET_OOM (error);
1849 bus_pending_activation_unref (pending_activation);
1850 bus_pending_activation_entry_free (pending_activation_entry);
1854 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1855 pending_activation->timeout))
1857 _dbus_verbose ("Failed to add timeout for pending activation\n");
1859 BUS_SET_OOM (error);
1860 bus_pending_activation_unref (pending_activation);
1861 bus_pending_activation_entry_free (pending_activation_entry);
1865 pending_activation->timeout_added = TRUE;
1867 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1869 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1871 BUS_SET_OOM (error);
1872 bus_pending_activation_unref (pending_activation);
1873 bus_pending_activation_entry_free (pending_activation_entry);
1877 pending_activation->n_entries += 1;
1878 pending_activation->activation->n_pending_activations += 1;
1880 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1881 pending_activation->service_name,
1882 pending_activation))
1884 _dbus_verbose ("Failed to put pending activation in hash table\n");
1886 BUS_SET_OOM (error);
1887 bus_pending_activation_unref (pending_activation);
1892 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1894 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1895 BUS_SET_OOM (error);
1896 _dbus_hash_table_remove_string (activation->pending_activations,
1897 pending_activation->service_name);
1902 if (was_pending_activation)
1905 if (bus_context_get_systemd_activation (activation->context))
1907 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1908 /* systemd itself is missing apparently. That can happen
1909 only during early startup. Let's just wait until systemd
1910 connects to us and do nothing. */
1913 if (entry->systemd_service)
1915 BusTransaction *activation_transaction;
1916 DBusString service_string;
1917 BusService *service;
1918 BusRegistry *registry;
1920 /* OK, we have a systemd service configured for this entry,
1921 hence let's enqueue an activation request message. This
1922 is implemented as a directed signal, not a method call,
1923 for three reasons: 1) we don't expect a response on
1924 success, where we just expect a name appearing on the
1925 bus; 2) at this time the systemd service might not yet
1926 have connected, so we wouldn't know the message serial at
1927 this point to set up a pending call; 3) it is ugly if the
1928 bus suddenly becomes the caller of a remote method. */
1930 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1931 "org.freedesktop.systemd1.Activator",
1932 "ActivationRequest");
1935 _dbus_verbose ("No memory to create activation message\n");
1936 BUS_SET_OOM (error);
1940 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1941 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1942 !dbus_message_append_args (message,
1943 DBUS_TYPE_STRING, &entry->systemd_service,
1946 _dbus_verbose ("No memory to set args of activation message\n");
1947 dbus_message_unref (message);
1948 BUS_SET_OOM (error);
1952 /* Create our transaction */
1953 activation_transaction = bus_transaction_new (activation->context);
1954 if (activation_transaction == NULL)
1956 _dbus_verbose ("No memory to create activation transaction\n");
1957 dbus_message_unref (message);
1958 BUS_SET_OOM (error);
1962 /* Check whether systemd is already connected */
1963 registry = bus_connection_get_registry (connection);
1964 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1965 service = bus_registry_lookup (registry, &service_string);
1967 if (service != NULL)
1969 bus_context_log (activation->context,
1970 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1972 entry->systemd_service);
1973 /* Wonderful, systemd is connected, let's just send the msg */
1974 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1979 bus_context_log (activation->context,
1980 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1982 entry->systemd_service);
1983 /* systemd is not around, let's "activate" it. */
1984 retval = bus_activation_activate_service (activation, NULL, activation_transaction, TRUE,
1985 message, "org.freedesktop.systemd1", error);
1988 dbus_message_unref (message);
1992 bus_context_log (activation->context,
1993 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
1995 entry->systemd_service);
1996 _DBUS_ASSERT_ERROR_IS_SET (error);
1997 _dbus_verbose ("failed to send activation message: %s\n", error->name);
1998 bus_transaction_cancel_and_free (activation_transaction);
2002 bus_transaction_execute_and_free (activation_transaction);
2006 /* OK, we have no configured systemd service, hence let's
2007 proceed with traditional activation. */
2010 /* use command as system and session different */
2011 if (!_dbus_string_init (&command))
2013 BUS_SET_OOM (error);
2017 /* does the bus use a helper? */
2018 servicehelper = bus_context_get_servicehelper (activation->context);
2019 if (servicehelper != NULL)
2021 if (entry->user == NULL)
2023 _dbus_string_free (&command);
2024 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2025 "Cannot do system-bus activation with no user\n");
2029 /* join the helper path and the service name */
2030 if (!_dbus_string_append (&command, servicehelper))
2032 _dbus_string_free (&command);
2033 BUS_SET_OOM (error);
2036 if (!_dbus_string_append (&command, " "))
2038 _dbus_string_free (&command);
2039 BUS_SET_OOM (error);
2042 if (!_dbus_string_append (&command, service_name))
2044 _dbus_string_free (&command);
2045 BUS_SET_OOM (error);
2051 /* the bus does not use a helper, so we can append arguments with the exec line */
2052 if (!_dbus_string_append (&command, entry->exec))
2054 _dbus_string_free (&command);
2055 BUS_SET_OOM (error);
2060 /* convert command into arguments */
2061 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2063 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2064 _DBUS_ASSERT_ERROR_IS_SET (error);
2066 _dbus_hash_table_remove_string (activation->pending_activations,
2067 pending_activation->service_name);
2069 _dbus_string_free (&command);
2072 _dbus_string_free (&command);
2074 if (!add_bus_environment (activation, error))
2076 _DBUS_ASSERT_ERROR_IS_SET (error);
2077 dbus_free_string_array (argv);
2081 envp = bus_activation_get_environment (activation);
2085 BUS_SET_OOM (error);
2086 dbus_free_string_array (argv);
2090 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2091 if (servicehelper != NULL)
2092 bus_context_log (activation->context,
2093 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2096 bus_context_log (activation->context,
2097 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2100 dbus_error_init (&tmp_error);
2102 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2107 _dbus_verbose ("Failed to spawn child\n");
2108 bus_context_log (activation->context,
2109 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2112 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2113 dbus_move_error (&tmp_error, error);
2114 dbus_free_string_array (argv);
2115 dbus_free_string_array (envp);
2120 dbus_free_string_array (argv);
2123 _dbus_assert (pending_activation->babysitter != NULL);
2125 _dbus_babysitter_set_result_function (pending_activation->babysitter,
2126 pending_activation_finished_cb,
2127 pending_activation);
2129 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2130 add_babysitter_watch,
2131 remove_babysitter_watch,
2132 toggle_babysitter_watch,
2136 BUS_SET_OOM (error);
2137 _dbus_verbose ("Failed to set babysitter watch functions\n");
2145 bus_activation_list_services (BusActivation *activation,
2153 len = _dbus_hash_table_get_n_entries (activation->entries);
2154 retval = dbus_new (char *, len + 1);
2159 _dbus_hash_iter_init (activation->entries, &iter);
2161 while (_dbus_hash_iter_next (&iter))
2163 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2165 retval[i] = _dbus_strdup (entry->name);
2166 if (retval[i] == NULL)
2181 for (j = 0; j < i; j++)
2182 dbus_free (retval[i]);
2189 dbus_activation_systemd_failure (BusActivation *activation,
2190 DBusMessage *message)
2193 const char *code, *str, *unit = NULL;
2195 dbus_error_init(&error);
2197 /* This is called whenever the systemd activator sent us a
2198 response. We'll invalidate all pending activations that match the
2201 if (dbus_message_get_args (message, &error,
2202 DBUS_TYPE_STRING, &unit,
2203 DBUS_TYPE_STRING, &code,
2204 DBUS_TYPE_STRING, &str,
2206 dbus_set_error(&error, code, str);
2213 bus_context_log (activation->context,
2214 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2218 _dbus_hash_iter_init (activation->pending_activations,
2221 while (_dbus_hash_iter_next (&iter))
2223 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2225 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2226 pending_activation_failed(p, &error);
2230 dbus_error_free(&error);
2235 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2239 #define SERVICE_NAME_1 "MyService1"
2240 #define SERVICE_NAME_2 "MyService2"
2241 #define SERVICE_NAME_3 "MyService3"
2243 #define SERVICE_FILE_1 "service-1.service"
2244 #define SERVICE_FILE_2 "service-2.service"
2245 #define SERVICE_FILE_3 "service-3.service"
2248 test_create_service_file (DBusString *dir,
2249 const char *filename,
2253 DBusString file_name, full_path;
2255 dbus_bool_t ret_val;
2258 _dbus_string_init_const (&file_name, filename);
2260 if (!_dbus_string_init (&full_path))
2263 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2264 !_dbus_concat_dir_and_file (&full_path, &file_name))
2270 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2277 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2281 _dbus_string_free (&full_path);
2286 test_remove_service_file (DBusString *dir, const char *filename)
2288 DBusString file_name, full_path;
2289 dbus_bool_t ret_val;
2293 _dbus_string_init_const (&file_name, filename);
2295 if (!_dbus_string_init (&full_path))
2298 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2299 !_dbus_concat_dir_and_file (&full_path, &file_name))
2305 if (!_dbus_delete_file (&full_path, NULL))
2312 _dbus_string_free (&full_path);
2317 test_remove_directory (DBusString *dir)
2320 DBusString filename, full_path;
2321 dbus_bool_t ret_val;
2325 if (!_dbus_string_init (&filename))
2328 if (!_dbus_string_init (&full_path))
2330 _dbus_string_free (&filename);
2334 iter = _dbus_directory_open (dir, NULL);
2341 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2343 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2349 _dbus_directory_close (iter);
2351 if (!_dbus_delete_directory (dir, NULL))
2358 _dbus_string_free (&filename);
2359 _dbus_string_free (&full_path);
2365 init_service_reload_test (DBusString *dir)
2369 if (!_dbus_stat (dir, &stat_buf, NULL))
2371 if (!_dbus_create_directory (dir, NULL))
2376 if (!test_remove_directory (dir))
2379 if (!_dbus_create_directory (dir, NULL))
2383 /* Create one initial file */
2384 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2391 cleanup_service_reload_test (DBusString *dir)
2393 if (!test_remove_directory (dir))
2401 BusActivation *activation;
2402 const char *service_name;
2403 dbus_bool_t expecting_find;
2407 check_func (void *data)
2410 BusActivationEntry *entry;
2412 dbus_bool_t ret_val;
2417 dbus_error_init (&error);
2419 entry = activation_find_entry (d->activation, d->service_name, &error);
2422 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2428 if (d->expecting_find)
2432 dbus_error_free (&error);
2436 if (!d->expecting_find)
2444 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2449 err = !_dbus_test_oom_handling (description, check_func, data);
2451 err = !check_func (data);
2454 _dbus_assert_not_reached ("Test failed");
2460 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2462 BusActivation *activation;
2464 DBusList *directories;
2468 _dbus_string_init_const (&address, "");
2470 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2473 activation = bus_activation_new (NULL, &address, &directories, NULL);
2477 d.activation = activation;
2479 /* Check for existing service file */
2480 d.expecting_find = TRUE;
2481 d.service_name = SERVICE_NAME_1;
2483 if (!do_test ("Existing service file", oom_test, &d))
2486 /* Check for non-existing service file */
2487 d.expecting_find = FALSE;
2488 d.service_name = SERVICE_NAME_3;
2490 if (!do_test ("Nonexisting service file", oom_test, &d))
2493 /* Check for added service file */
2494 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2497 d.expecting_find = TRUE;
2498 d.service_name = SERVICE_NAME_2;
2500 if (!do_test ("Added service file", oom_test, &d))
2503 /* Check for removed service file */
2504 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2507 d.expecting_find = FALSE;
2508 d.service_name = SERVICE_FILE_2;
2510 if (!do_test ("Removed service file", oom_test, &d))
2513 /* Check for updated service file */
2515 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2517 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2520 d.expecting_find = TRUE;
2521 d.service_name = SERVICE_NAME_3;
2523 if (!do_test ("Updated service file, part 1", oom_test, &d))
2526 d.expecting_find = FALSE;
2527 d.service_name = SERVICE_NAME_1;
2529 if (!do_test ("Updated service file, part 2", oom_test, &d))
2532 bus_activation_unref (activation);
2533 _dbus_list_clear (&directories);
2539 bus_activation_service_reload_test (const DBusString *test_data_dir)
2541 DBusString directory;
2544 if (!_dbus_string_init (&directory))
2547 tmp = _dbus_get_tmpdir ();
2552 if (!_dbus_string_append (&directory, tmp))
2555 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2556 !_dbus_generate_random_ascii (&directory, 6))
2561 /* Do normal tests */
2562 if (!init_service_reload_test (&directory))
2563 _dbus_assert_not_reached ("could not initiate service reload test");
2565 if (!do_service_reload_test (&directory, FALSE))
2571 if (!init_service_reload_test (&directory))
2572 _dbus_assert_not_reached ("could not initiate service reload test");
2574 if (!do_service_reload_test (&directory, TRUE))
2579 /* Cleanup test directory */
2580 if (!cleanup_service_reload_test (&directory))
2583 _dbus_string_free (&directory);
2588 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */