1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* activation.c Activation of services
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "activation.h"
28 #include "activation-exit-codes.h"
29 #include "desktop-file.h"
34 #include <dbus/dbus-internals.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-list.h>
37 #include <dbus/dbus-shell.h>
38 #include <dbus/dbus-spawn.h>
39 #include <dbus/dbus-timeout.h>
40 #include <dbus/dbus-sysdeps.h>
48 DBusHashTable *entries;
49 DBusHashTable *pending_activations;
52 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
53 * i.e. number of pending activation requests, not pending
56 DBusHashTable *directories;
57 DBusHashTable *environment;
64 DBusHashTable *entries;
65 } BusServiceDirectory;
73 char *systemd_service;
75 BusServiceDirectory *s_dir;
79 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
81 struct BusPendingActivationEntry
83 DBusMessage *activation_message;
84 DBusConnection *connection;
86 dbus_bool_t auto_activation;
92 BusActivation *activation;
95 char *systemd_service;
98 DBusBabysitter *babysitter;
100 unsigned int timeout_added : 1;
101 } BusPendingActivation;
104 static BusServiceDirectory *
105 bus_service_directory_ref (BusServiceDirectory *dir)
107 _dbus_assert (dir->refcount);
116 bus_service_directory_unref (BusServiceDirectory *dir)
121 _dbus_assert (dir->refcount > 0);
124 if (dir->refcount > 0)
128 _dbus_hash_table_unref (dir->entries);
130 dbus_free (dir->dir_c);
135 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
137 if (entry->activation_message)
138 dbus_message_unref (entry->activation_message);
140 if (entry->connection)
141 dbus_connection_unref (entry->connection);
146 static BusPendingActivation *
147 bus_pending_activation_ref (BusPendingActivation *pending_activation)
149 _dbus_assert (pending_activation->refcount > 0);
150 pending_activation->refcount += 1;
152 return pending_activation;
156 bus_pending_activation_unref (BusPendingActivation *pending_activation)
160 if (pending_activation == NULL) /* hash table requires this */
163 _dbus_assert (pending_activation->refcount > 0);
164 pending_activation->refcount -= 1;
166 if (pending_activation->refcount > 0)
169 if (pending_activation->timeout_added)
171 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
172 pending_activation->timeout);
173 pending_activation->timeout_added = FALSE;
176 if (pending_activation->timeout)
177 _dbus_timeout_unref (pending_activation->timeout);
179 if (pending_activation->babysitter)
181 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
183 pending_activation->babysitter,
185 _dbus_assert_not_reached ("setting watch functions to NULL failed");
187 _dbus_babysitter_unref (pending_activation->babysitter);
190 dbus_free (pending_activation->service_name);
191 dbus_free (pending_activation->exec);
192 dbus_free (pending_activation->systemd_service);
194 link = _dbus_list_get_first_link (&pending_activation->entries);
198 BusPendingActivationEntry *entry = link->data;
200 bus_pending_activation_entry_free (entry);
202 link = _dbus_list_get_next_link (&pending_activation->entries, link);
204 _dbus_list_clear (&pending_activation->entries);
206 pending_activation->activation->n_pending_activations -=
207 pending_activation->n_entries;
209 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
211 dbus_free (pending_activation);
214 static BusActivationEntry *
215 bus_activation_entry_ref (BusActivationEntry *entry)
217 _dbus_assert (entry->refcount > 0);
224 bus_activation_entry_unref (BusActivationEntry *entry)
226 if (entry == NULL) /* hash table requires this */
229 _dbus_assert (entry->refcount > 0);
232 if (entry->refcount > 0)
235 dbus_free (entry->name);
236 dbus_free (entry->exec);
237 dbus_free (entry->user);
238 dbus_free (entry->filename);
239 dbus_free (entry->systemd_service);
245 update_desktop_file_entry (BusActivation *activation,
246 BusServiceDirectory *s_dir,
247 DBusString *filename,
248 BusDesktopFile *desktop_file,
251 char *name, *exec, *user, *exec_tmp, *systemd_service;
252 BusActivationEntry *entry;
254 DBusString file_path;
258 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
266 systemd_service = NULL;
268 dbus_error_init (&tmp_error);
270 if (!_dbus_string_init (&file_path))
276 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
277 !_dbus_concat_dir_and_file (&file_path, filename))
283 if (!_dbus_stat (&file_path, &stat_buf, NULL))
285 dbus_set_error (error, DBUS_ERROR_FAILED,
286 "Can't stat the service file\n");
290 if (!bus_desktop_file_get_string (desktop_file,
291 DBUS_SERVICE_SECTION,
297 if (!bus_desktop_file_get_string (desktop_file,
298 DBUS_SERVICE_SECTION,
304 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
305 dbus_free (exec_tmp);
308 /* user is not _required_ unless we are using system activation */
309 if (!bus_desktop_file_get_string (desktop_file,
310 DBUS_SERVICE_SECTION,
314 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
315 /* if we got OOM, then exit */
316 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
318 dbus_move_error (&tmp_error, error);
323 /* if we have error because we didn't find anything then continue */
324 dbus_error_free (&tmp_error);
329 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
331 /* systemd service is never required */
332 if (!bus_desktop_file_get_string (desktop_file,
333 DBUS_SERVICE_SECTION,
334 DBUS_SERVICE_SYSTEMD_SERVICE,
335 &systemd_service, &tmp_error))
337 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
338 /* if we got OOM, then exit */
339 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
341 dbus_move_error (&tmp_error, error);
346 /* if we have error because we didn't find anything then continue */
347 dbus_error_free (&tmp_error);
348 dbus_free (systemd_service);
349 systemd_service = NULL;
353 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
355 entry = _dbus_hash_table_lookup_string (s_dir->entries,
356 _dbus_string_get_const_data (filename));
358 if (entry == NULL) /* New file */
360 /* FIXME we need a better-defined algorithm for which service file to
361 * pick than "whichever one is first in the directory listing"
363 if (_dbus_hash_table_lookup_string (activation->entries, name))
365 dbus_set_error (error, DBUS_ERROR_FAILED,
366 "Service %s already exists in activation entry list\n", name);
370 entry = dbus_new0 (BusActivationEntry, 1);
380 entry->systemd_service = systemd_service;
383 /* ownership has been transferred to entry, do not free separately */
387 systemd_service = NULL;
389 entry->s_dir = s_dir;
390 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
391 if (!entry->filename)
397 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
403 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
405 /* Revert the insertion in the entries table */
406 _dbus_hash_table_remove_string (activation->entries, entry->name);
411 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
413 else /* Just update the entry */
415 bus_activation_entry_ref (entry);
416 _dbus_hash_table_remove_string (activation->entries, entry->name);
418 if (_dbus_hash_table_lookup_string (activation->entries, name))
420 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
421 name, _dbus_string_get_const_data (&file_path));
422 dbus_set_error (error, DBUS_ERROR_FAILED,
423 "The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
424 name, _dbus_string_get_const_data (&file_path));
428 /* ownership has been transferred to entry, do not free separately */
429 dbus_free (entry->name);
433 dbus_free (entry->exec);
437 dbus_free (entry->user);
441 dbus_free (entry->systemd_service);
442 entry->systemd_service = systemd_service;
443 systemd_service = NULL;
445 if (!_dbus_hash_table_insert_string (activation->entries,
446 entry->name, bus_activation_entry_ref(entry)))
449 /* Also remove path to entries hash since we want this in sync with
450 * the entries hash table */
451 _dbus_hash_table_remove_string (entry->s_dir->entries,
457 entry->mtime = stat_buf.mtime;
461 /* if these have been transferred into entry, the variables will be NULL */
465 dbus_free (systemd_service);
466 _dbus_string_free (&file_path);
469 bus_activation_entry_unref (entry);
475 check_service_file (BusActivation *activation,
476 BusActivationEntry *entry,
477 BusActivationEntry **updated_entry,
482 BusActivationEntry *tmp_entry;
483 DBusString file_path;
489 _dbus_string_init_const (&filename, entry->filename);
491 if (!_dbus_string_init (&file_path))
497 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
498 !_dbus_concat_dir_and_file (&file_path, &filename))
505 if (!_dbus_stat (&file_path, &stat_buf, NULL))
507 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
508 _dbus_string_get_const_data (&file_path));
510 _dbus_hash_table_remove_string (activation->entries, entry->name);
511 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
519 if (stat_buf.mtime > entry->mtime)
521 BusDesktopFile *desktop_file;
524 dbus_error_init (&tmp_error);
526 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
527 if (desktop_file == NULL)
529 _dbus_verbose ("Could not load %s: %s\n",
530 _dbus_string_get_const_data (&file_path),
532 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
534 dbus_move_error (&tmp_error, error);
538 dbus_error_free (&tmp_error);
543 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
544 * Handle these both better
546 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
548 bus_desktop_file_free (desktop_file);
549 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
551 dbus_move_error (&tmp_error, error);
555 dbus_error_free (&tmp_error);
560 bus_desktop_file_free (desktop_file);
566 _dbus_string_free (&file_path);
568 if (updated_entry != NULL)
569 *updated_entry = tmp_entry;
574 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
575 * hash entries it already added.
578 update_directory (BusActivation *activation,
579 BusServiceDirectory *s_dir,
583 DBusString dir, filename;
584 BusDesktopFile *desktop_file;
587 BusActivationEntry *entry;
588 DBusString full_path;
590 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
595 _dbus_string_init_const (&dir, s_dir->dir_c);
597 if (!_dbus_string_init (&filename))
603 if (!_dbus_string_init (&full_path))
606 _dbus_string_free (&filename);
612 /* from this point it's safe to "goto out" */
614 iter = _dbus_directory_open (&dir, error);
617 _dbus_verbose ("Failed to open directory %s: %s\n",
619 error ? error->message : "unknown");
623 /* Now read the files */
624 dbus_error_init (&tmp_error);
625 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
627 _dbus_assert (!dbus_error_is_set (&tmp_error));
629 _dbus_string_set_length (&full_path, 0);
631 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
633 _dbus_verbose ("Skipping non-.service file %s\n",
634 _dbus_string_get_const_data (&filename));
638 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
639 if (entry) /* Already has this service file in the cache */
641 if (!check_service_file (activation, entry, NULL, error))
647 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
648 !_dbus_concat_dir_and_file (&full_path, &filename))
655 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
656 if (desktop_file == NULL)
658 _dbus_verbose ("Could not load %s: %s\n",
659 _dbus_string_get_const_data (&full_path),
662 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
664 dbus_move_error (&tmp_error, error);
668 dbus_error_free (&tmp_error);
672 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
673 * Handle these both better
675 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
677 bus_desktop_file_free (desktop_file);
680 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
681 _dbus_string_get_const_data (&full_path), tmp_error.message);
683 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
685 dbus_move_error (&tmp_error, error);
689 dbus_error_free (&tmp_error);
694 bus_desktop_file_free (desktop_file);
700 if (dbus_error_is_set (&tmp_error))
702 dbus_move_error (&tmp_error, error);
710 _DBUS_ASSERT_ERROR_IS_SET (error);
712 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
715 _dbus_directory_close (iter);
716 _dbus_string_free (&filename);
717 _dbus_string_free (&full_path);
723 populate_environment (BusActivation *activation)
729 dbus_bool_t retval = FALSE;
731 environment = _dbus_get_environment ();
733 if (environment == NULL)
736 if (!_dbus_string_init (&key))
738 dbus_free_string_array (environment);
742 if (!_dbus_string_init (&value))
744 _dbus_string_free (&key);
745 dbus_free_string_array (environment);
749 for (i = 0; environment[i] != NULL; i++)
751 if (!_dbus_string_append (&key, environment[i]))
754 if (_dbus_string_split_on_byte (&key, '=', &value))
756 char *hash_key, *hash_value;
758 if (!_dbus_string_steal_data (&key, &hash_key))
761 if (!_dbus_string_steal_data (&value, &hash_value))
764 if (!_dbus_hash_table_insert_string (activation->environment,
765 hash_key, hash_value))
768 _dbus_string_set_length (&key, 0);
769 _dbus_string_set_length (&value, 0);
772 if (environment[i] != NULL)
778 _dbus_string_free (&key);
779 _dbus_string_free (&value);
780 dbus_free_string_array (environment);
786 bus_activation_reload (BusActivation *activation,
787 const DBusString *address,
788 DBusList **directories,
794 if (activation->server_address != NULL)
795 dbus_free (activation->server_address);
796 if (!_dbus_string_copy_data (address, &activation->server_address))
802 if (activation->entries != NULL)
803 _dbus_hash_table_unref (activation->entries);
804 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
805 (DBusFreeFunction)bus_activation_entry_unref);
806 if (activation->entries == NULL)
812 if (activation->directories != NULL)
813 _dbus_hash_table_unref (activation->directories);
814 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
815 (DBusFreeFunction)bus_service_directory_unref);
817 if (activation->directories == NULL)
823 link = _dbus_list_get_first_link (directories);
826 BusServiceDirectory *s_dir;
828 dir = _dbus_strdup ((const char *) link->data);
835 s_dir = dbus_new0 (BusServiceDirectory, 1);
846 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
847 (DBusFreeFunction)bus_activation_entry_unref);
851 bus_service_directory_unref (s_dir);
856 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
858 bus_service_directory_unref (s_dir);
863 /* only fail on OOM, it is ok if we can't read the directory */
864 if (!update_directory (activation, s_dir, error))
866 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
869 dbus_error_free (error);
872 link = _dbus_list_get_next_link (directories, link);
881 bus_activation_new (BusContext *context,
882 const DBusString *address,
883 DBusList **directories,
886 BusActivation *activation;
888 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
890 activation = dbus_new0 (BusActivation, 1);
891 if (activation == NULL)
897 activation->refcount = 1;
898 activation->context = context;
899 activation->n_pending_activations = 0;
901 if (!bus_activation_reload (activation, address, directories, error))
904 /* Initialize this hash table once, we don't want to lose pending
905 * activations on reload. */
906 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
907 (DBusFreeFunction)bus_pending_activation_unref);
909 if (activation->pending_activations == NULL)
915 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
916 (DBusFreeFunction) dbus_free,
917 (DBusFreeFunction) dbus_free);
919 if (activation->environment == NULL)
925 if (!populate_environment (activation))
934 bus_activation_unref (activation);
939 bus_activation_ref (BusActivation *activation)
941 _dbus_assert (activation->refcount > 0);
943 activation->refcount += 1;
949 bus_activation_unref (BusActivation *activation)
951 _dbus_assert (activation->refcount > 0);
953 activation->refcount -= 1;
955 if (activation->refcount > 0)
958 dbus_free (activation->server_address);
959 if (activation->entries)
960 _dbus_hash_table_unref (activation->entries);
961 if (activation->pending_activations)
962 _dbus_hash_table_unref (activation->pending_activations);
963 if (activation->directories)
964 _dbus_hash_table_unref (activation->directories);
965 if (activation->environment)
966 _dbus_hash_table_unref (activation->environment);
968 dbus_free (activation);
972 add_bus_environment (BusActivation *activation,
977 if (!bus_activation_set_environment_variable (activation,
978 "DBUS_STARTER_ADDRESS",
979 activation->server_address,
983 type = bus_context_get_type (activation->context);
986 if (!bus_activation_set_environment_variable (activation,
987 "DBUS_STARTER_BUS_TYPE", type,
991 if (strcmp (type, "session") == 0)
993 if (!bus_activation_set_environment_variable (activation,
994 "DBUS_SESSION_BUS_ADDRESS",
995 activation->server_address,
999 else if (strcmp (type, "system") == 0)
1001 if (!bus_activation_set_environment_variable (activation,
1002 "DBUS_SYSTEM_BUS_ADDRESS",
1003 activation->server_address,
1014 BusPendingActivation *pending_activation;
1015 DBusPreallocatedHash *hash_entry;
1016 } RestorePendingData;
1019 restore_pending (void *data)
1021 RestorePendingData *d = data;
1023 _dbus_assert (d->pending_activation != NULL);
1024 _dbus_assert (d->hash_entry != NULL);
1026 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1027 d->pending_activation->service_name,
1028 d->pending_activation->timeout_added);
1030 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1032 d->pending_activation->service_name, d->pending_activation);
1034 bus_pending_activation_ref (d->pending_activation);
1036 d->hash_entry = NULL;
1040 free_pending_restore_data (void *data)
1042 RestorePendingData *d = data;
1045 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1048 bus_pending_activation_unref (d->pending_activation);
1054 add_restore_pending_to_transaction (BusTransaction *transaction,
1055 BusPendingActivation *pending_activation)
1057 RestorePendingData *d;
1059 d = dbus_new (RestorePendingData, 1);
1063 d->pending_activation = pending_activation;
1064 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1066 bus_pending_activation_ref (d->pending_activation);
1068 if (d->hash_entry == NULL ||
1069 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1070 free_pending_restore_data))
1072 free_pending_restore_data (d);
1076 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1082 bus_activation_service_created (BusActivation *activation,
1083 const char *service_name,
1084 BusTransaction *transaction,
1087 BusPendingActivation *pending_activation;
1088 DBusMessage *message;
1091 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1093 /* Check if it's a pending activation */
1094 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1096 if (!pending_activation)
1099 bus_context_log (activation->context,
1100 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1103 link = _dbus_list_get_first_link (&pending_activation->entries);
1104 while (link != NULL)
1106 BusPendingActivationEntry *entry = link->data;
1107 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1109 if (dbus_connection_get_is_connected (entry->connection))
1111 /* Only send activation replies to regular activation requests. */
1112 if (!entry->auto_activation)
1114 dbus_uint32_t result;
1116 message = dbus_message_new_method_return (entry->activation_message);
1119 BUS_SET_OOM (error);
1123 result = DBUS_START_REPLY_SUCCESS;
1125 if (!dbus_message_append_args (message,
1126 DBUS_TYPE_UINT32, &result,
1129 dbus_message_unref (message);
1130 BUS_SET_OOM (error);
1134 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1136 dbus_message_unref (message);
1137 BUS_SET_OOM (error);
1141 dbus_message_unref (message);
1155 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1156 BusService *service,
1157 BusTransaction *transaction,
1160 BusPendingActivation *pending_activation;
1163 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1165 /* Check if it's a pending activation */
1166 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1167 bus_service_get_name (service));
1169 if (!pending_activation)
1172 link = _dbus_list_get_first_link (&pending_activation->entries);
1173 while (link != NULL)
1175 BusPendingActivationEntry *entry = link->data;
1176 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1178 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1180 DBusConnection *addressed_recipient;
1182 addressed_recipient = bus_service_get_primary_owners_connection (service);
1184 /* Resume dispatching where we left off in bus_dispatch() */
1185 if (!bus_dispatch_matches (transaction,
1187 addressed_recipient,
1188 entry->activation_message, error))
1195 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1197 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1198 BUS_SET_OOM (error);
1202 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1211 * FIXME @todo the error messages here would ideally be preallocated
1212 * so we don't need to allocate memory to send them.
1213 * Using the usual tactic, prealloc an OOM message, then
1214 * if we can't alloc the real error send the OOM error instead.
1217 try_send_activation_failure (BusPendingActivation *pending_activation,
1218 const DBusError *how)
1220 BusActivation *activation;
1222 BusTransaction *transaction;
1224 activation = pending_activation->activation;
1226 transaction = bus_transaction_new (activation->context);
1227 if (transaction == NULL)
1230 link = _dbus_list_get_first_link (&pending_activation->entries);
1231 while (link != NULL)
1233 BusPendingActivationEntry *entry = link->data;
1234 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1236 if (dbus_connection_get_is_connected (entry->connection))
1238 if (!bus_transaction_send_error_reply (transaction,
1241 entry->activation_message))
1248 bus_transaction_execute_and_free (transaction);
1254 bus_transaction_cancel_and_free (transaction);
1259 * Free the pending activation and send an error message to all the
1260 * connections that were waiting for it.
1263 pending_activation_failed (BusPendingActivation *pending_activation,
1264 const DBusError *how)
1266 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1267 while (!try_send_activation_failure (pending_activation, how))
1268 _dbus_wait_for_memory ();
1270 /* Destroy this pending activation */
1271 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1272 pending_activation->service_name);
1276 * Depending on the exit code of the helper, set the error accordingly
1279 handle_servicehelper_exit_error (int exit_code,
1284 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1285 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1286 "Launcher could not run (out of memory)");
1288 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1289 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1290 "Failed to setup environment correctly");
1292 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1293 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1294 "Bus name is not valid or missing");
1296 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1297 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1298 "Bus name not found in system service directory");
1300 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1301 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1302 "The permission of the setuid helper is not correct");
1304 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1305 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1306 "The service file is incorrect or does not have all required attributes");
1308 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1309 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1310 "Cannot launch daemon, file not found or permissions invalid");
1312 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1313 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1314 "Invalid arguments to command line");
1316 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1317 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1318 "Launched child was signaled, it probably crashed");
1321 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1322 "Launch helper exited with unknown return code %i", exit_code);
1328 pending_activation_finished_cb (DBusBabysitter *babysitter,
1331 BusPendingActivation *pending_activation = data;
1332 dbus_bool_t uses_servicehelper;
1334 _dbus_assert (babysitter == pending_activation->babysitter);
1335 _dbus_babysitter_ref (babysitter);
1337 /* There are two major cases here; are we the system bus or the session? Here this
1338 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1339 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1341 * In both cases though, just ignore when a process exits with status 0; it's possible for
1342 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1343 * condition between this code and the child process claiming the bus name.
1345 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1347 /* strictly speaking this is redundant with the check in dbus-spawn now */
1348 if (_dbus_babysitter_get_child_exited (babysitter))
1352 dbus_bool_t activation_failed;
1355 dbus_error_init (&error);
1357 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1359 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1361 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1362 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1364 activation_failed = exit_code != 0;
1366 dbus_error_free(&error);
1368 if (activation_failed)
1370 if (uses_servicehelper)
1371 handle_servicehelper_exit_error (exit_code, &error);
1373 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1378 activation_failed = TRUE;
1381 if (activation_failed)
1383 bus_context_log (pending_activation->activation->context,
1384 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1385 pending_activation->service_name,
1388 /* Destroy all pending activations with the same exec */
1389 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1391 while (_dbus_hash_iter_next (&iter))
1393 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1395 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1396 pending_activation_failed (p, &error);
1399 /* Destroys the pending activation */
1400 pending_activation_failed (pending_activation, &error);
1402 dbus_error_free (&error);
1406 _dbus_babysitter_unref (babysitter);
1410 add_babysitter_watch (DBusWatch *watch,
1413 BusPendingActivation *pending_activation = data;
1415 return _dbus_loop_add_watch (
1416 bus_context_get_loop (pending_activation->activation->context),
1421 remove_babysitter_watch (DBusWatch *watch,
1424 BusPendingActivation *pending_activation = data;
1426 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1431 toggle_babysitter_watch (DBusWatch *watch,
1434 BusPendingActivation *pending_activation = data;
1436 _dbus_loop_toggle_watch (bus_context_get_loop (pending_activation->activation->context),
1441 pending_activation_timed_out (void *data)
1443 BusPendingActivation *pending_activation = data;
1446 /* Kill the spawned process, since it sucks
1447 * (not sure this is what we want to do, but
1448 * may as well try it for now)
1450 if (pending_activation->babysitter)
1451 _dbus_babysitter_kill_child (pending_activation->babysitter);
1453 dbus_error_init (&error);
1455 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1456 "Activation of %s timed out",
1457 pending_activation->service_name);
1458 bus_context_log (pending_activation->activation->context,
1459 DBUS_SYSTEM_LOG_INFO,
1460 "Failed to activate service '%s': timed out",
1461 pending_activation->service_name);
1463 pending_activation_failed (pending_activation, &error);
1465 dbus_error_free (&error);
1471 cancel_pending (void *data)
1473 BusPendingActivation *pending_activation = data;
1475 _dbus_verbose ("Canceling pending activation of %s\n",
1476 pending_activation->service_name);
1478 if (pending_activation->babysitter)
1479 _dbus_babysitter_kill_child (pending_activation->babysitter);
1481 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1482 pending_activation->service_name);
1486 free_pending_cancel_data (void *data)
1488 BusPendingActivation *pending_activation = data;
1490 bus_pending_activation_unref (pending_activation);
1494 add_cancel_pending_to_transaction (BusTransaction *transaction,
1495 BusPendingActivation *pending_activation)
1497 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1499 free_pending_cancel_data))
1502 bus_pending_activation_ref (pending_activation);
1504 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1510 update_service_cache (BusActivation *activation, DBusError *error)
1514 _dbus_hash_iter_init (activation->directories, &iter);
1515 while (_dbus_hash_iter_next (&iter))
1517 DBusError tmp_error;
1518 BusServiceDirectory *s_dir;
1520 s_dir = _dbus_hash_iter_get_value (&iter);
1522 dbus_error_init (&tmp_error);
1523 if (!update_directory (activation, s_dir, &tmp_error))
1525 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1527 dbus_move_error (&tmp_error, error);
1531 dbus_error_free (&tmp_error);
1539 static BusActivationEntry *
1540 activation_find_entry (BusActivation *activation,
1541 const char *service_name,
1544 BusActivationEntry *entry;
1546 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1549 if (!update_service_cache (activation, error))
1552 entry = _dbus_hash_table_lookup_string (activation->entries,
1557 BusActivationEntry *updated_entry;
1559 if (!check_service_file (activation, entry, &updated_entry, error))
1562 entry = updated_entry;
1567 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1568 "The name %s was not provided by any .service files",
1577 bus_activation_get_environment (BusActivation *activation)
1584 length = _dbus_hash_table_get_n_entries (activation->environment);
1586 environment = dbus_new0 (char *, length + 1);
1588 if (environment == NULL)
1592 _dbus_hash_iter_init (activation->environment, &iter);
1594 if (!_dbus_string_init (&entry))
1596 dbus_free_string_array (environment);
1600 while (_dbus_hash_iter_next (&iter))
1602 const char *key, *value;
1604 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1605 value = (const char *) _dbus_hash_iter_get_value (&iter);
1607 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1610 if (!_dbus_string_steal_data (&entry, environment + i))
1615 _dbus_string_free (&entry);
1619 dbus_free_string_array (environment);
1627 bus_activation_set_environment_variable (BusActivation *activation,
1639 hash_key = _dbus_strdup (key);
1641 if (hash_key == NULL)
1644 hash_value = _dbus_strdup (value);
1646 if (hash_value == NULL)
1649 if (!_dbus_hash_table_insert_string (activation->environment,
1650 hash_key, hash_value))
1655 if (retval == FALSE)
1657 dbus_free (hash_key);
1658 dbus_free (hash_value);
1659 BUS_SET_OOM (error);
1666 bus_activation_activate_service (BusActivation *activation,
1667 DBusConnection *connection,
1668 BusTransaction *transaction,
1669 dbus_bool_t auto_activation,
1670 DBusMessage *activation_message,
1671 const char *service_name,
1674 DBusError tmp_error;
1675 BusActivationEntry *entry;
1676 BusPendingActivation *pending_activation;
1677 BusPendingActivationEntry *pending_activation_entry;
1678 DBusMessage *message;
1679 DBusString service_str;
1680 const char *servicehelper;
1685 dbus_bool_t was_pending_activation;
1688 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1690 if (activation->n_pending_activations >=
1691 bus_context_get_max_pending_activations (activation->context))
1693 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1694 "The maximum number of pending activations has been reached, activation of %s failed",
1699 entry = activation_find_entry (activation, service_name, error);
1703 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1704 * call us if the service is already active.
1706 if (!auto_activation)
1708 /* Check if the service is active */
1709 _dbus_string_init_const (&service_str, service_name);
1710 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1712 dbus_uint32_t result;
1714 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1716 message = dbus_message_new_method_return (activation_message);
1720 _dbus_verbose ("No memory to create reply to activate message\n");
1721 BUS_SET_OOM (error);
1725 result = DBUS_START_REPLY_ALREADY_RUNNING;
1727 if (!dbus_message_append_args (message,
1728 DBUS_TYPE_UINT32, &result,
1731 _dbus_verbose ("No memory to set args of reply to activate message\n");
1732 BUS_SET_OOM (error);
1733 dbus_message_unref (message);
1737 retval = bus_transaction_send_from_driver (transaction, connection, message);
1738 dbus_message_unref (message);
1741 _dbus_verbose ("Failed to send reply\n");
1742 BUS_SET_OOM (error);
1749 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1750 if (!pending_activation_entry)
1752 _dbus_verbose ("Failed to create pending activation entry\n");
1753 BUS_SET_OOM (error);
1757 pending_activation_entry->auto_activation = auto_activation;
1759 pending_activation_entry->activation_message = activation_message;
1760 dbus_message_ref (activation_message);
1761 pending_activation_entry->connection = connection;
1762 dbus_connection_ref (connection);
1764 /* Check if the service is being activated */
1765 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1766 was_pending_activation = (pending_activation != NULL);
1767 if (was_pending_activation)
1769 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1771 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1773 BUS_SET_OOM (error);
1774 bus_pending_activation_entry_free (pending_activation_entry);
1778 pending_activation->n_entries += 1;
1779 pending_activation->activation->n_pending_activations += 1;
1783 pending_activation = dbus_new0 (BusPendingActivation, 1);
1784 if (!pending_activation)
1786 _dbus_verbose ("Failed to create pending activation\n");
1788 BUS_SET_OOM (error);
1789 bus_pending_activation_entry_free (pending_activation_entry);
1793 pending_activation->activation = activation;
1794 pending_activation->refcount = 1;
1796 pending_activation->service_name = _dbus_strdup (service_name);
1797 if (!pending_activation->service_name)
1799 _dbus_verbose ("Failed to copy service name for pending activation\n");
1801 BUS_SET_OOM (error);
1802 bus_pending_activation_unref (pending_activation);
1803 bus_pending_activation_entry_free (pending_activation_entry);
1807 pending_activation->exec = _dbus_strdup (entry->exec);
1808 if (!pending_activation->exec)
1810 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1811 BUS_SET_OOM (error);
1812 bus_pending_activation_unref (pending_activation);
1813 bus_pending_activation_entry_free (pending_activation_entry);
1817 if (entry->systemd_service)
1819 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1820 if (!pending_activation->systemd_service)
1822 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1823 BUS_SET_OOM (error);
1824 bus_pending_activation_unref (pending_activation);
1825 bus_pending_activation_entry_free (pending_activation_entry);
1830 pending_activation->timeout =
1831 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1832 pending_activation_timed_out,
1835 if (!pending_activation->timeout)
1837 _dbus_verbose ("Failed to create timeout 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);
1845 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1846 pending_activation->timeout))
1848 _dbus_verbose ("Failed to add timeout for pending activation\n");
1850 BUS_SET_OOM (error);
1851 bus_pending_activation_unref (pending_activation);
1852 bus_pending_activation_entry_free (pending_activation_entry);
1856 pending_activation->timeout_added = TRUE;
1858 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1860 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1862 BUS_SET_OOM (error);
1863 bus_pending_activation_unref (pending_activation);
1864 bus_pending_activation_entry_free (pending_activation_entry);
1868 pending_activation->n_entries += 1;
1869 pending_activation->activation->n_pending_activations += 1;
1871 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1872 pending_activation->service_name,
1873 pending_activation))
1875 _dbus_verbose ("Failed to put pending activation in hash table\n");
1877 BUS_SET_OOM (error);
1878 bus_pending_activation_unref (pending_activation);
1883 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1885 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1886 BUS_SET_OOM (error);
1887 _dbus_hash_table_remove_string (activation->pending_activations,
1888 pending_activation->service_name);
1893 if (was_pending_activation)
1896 if (bus_context_get_systemd_activation (activation->context))
1898 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1899 /* systemd itself is missing apparently. That can happen
1900 only during early startup. Let's just wait until systemd
1901 connects to us and do nothing. */
1904 if (entry->systemd_service)
1906 BusTransaction *activation_transaction;
1907 DBusString service_string;
1908 BusService *service;
1909 BusRegistry *registry;
1911 /* OK, we have a systemd service configured for this entry,
1912 hence let's enqueue an activation request message. This
1913 is implemented as a directed signal, not a method call,
1914 for three reasons: 1) we don't expect a response on
1915 success, where we just expect a name appearing on the
1916 bus; 2) at this time the systemd service might not yet
1917 have connected, so we wouldn't know the message serial at
1918 this point to set up a pending call; 3) it is ugly if the
1919 bus suddenly becomes the caller of a remote method. */
1921 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1922 "org.freedesktop.systemd1.Activator",
1923 "ActivationRequest");
1926 _dbus_verbose ("No memory to create activation message\n");
1927 BUS_SET_OOM (error);
1931 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1932 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1933 !dbus_message_append_args (message,
1934 DBUS_TYPE_STRING, &entry->systemd_service,
1937 _dbus_verbose ("No memory to set args of activation message\n");
1938 dbus_message_unref (message);
1939 BUS_SET_OOM (error);
1943 /* Create our transaction */
1944 activation_transaction = bus_transaction_new (activation->context);
1945 if (activation_transaction == NULL)
1947 _dbus_verbose ("No memory to create activation transaction\n");
1948 dbus_message_unref (message);
1949 BUS_SET_OOM (error);
1953 /* Check whether systemd is already connected */
1954 registry = bus_connection_get_registry (connection);
1955 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1956 service = bus_registry_lookup (registry, &service_string);
1958 if (service != NULL)
1960 bus_context_log (activation->context,
1961 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1963 entry->systemd_service);
1964 /* Wonderful, systemd is connected, let's just send the msg */
1965 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1970 bus_context_log (activation->context,
1971 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1973 entry->systemd_service);
1974 /* systemd is not around, let's "activate" it. */
1975 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1976 message, "org.freedesktop.systemd1", error);
1979 dbus_message_unref (message);
1983 bus_context_log (activation->context,
1984 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
1986 entry->systemd_service);
1987 _DBUS_ASSERT_ERROR_IS_SET (error);
1988 _dbus_verbose ("failed to send activation message: %s\n", error->name);
1989 bus_transaction_cancel_and_free (activation_transaction);
1993 bus_transaction_execute_and_free (activation_transaction);
1997 /* OK, we have no configured systemd service, hence let's
1998 proceed with traditional activation. */
2001 /* use command as system and session different */
2002 if (!_dbus_string_init (&command))
2004 BUS_SET_OOM (error);
2008 /* does the bus use a helper? */
2009 servicehelper = bus_context_get_servicehelper (activation->context);
2010 if (servicehelper != NULL)
2012 if (entry->user == NULL)
2014 _dbus_string_free (&command);
2015 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2016 "Cannot do system-bus activation with no user\n");
2020 /* join the helper path and the service name */
2021 if (!_dbus_string_append (&command, servicehelper))
2023 _dbus_string_free (&command);
2024 BUS_SET_OOM (error);
2027 if (!_dbus_string_append (&command, " "))
2029 _dbus_string_free (&command);
2030 BUS_SET_OOM (error);
2033 if (!_dbus_string_append (&command, service_name))
2035 _dbus_string_free (&command);
2036 BUS_SET_OOM (error);
2042 /* the bus does not use a helper, so we can append arguments with the exec line */
2043 if (!_dbus_string_append (&command, entry->exec))
2045 _dbus_string_free (&command);
2046 BUS_SET_OOM (error);
2051 /* convert command into arguments */
2052 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2054 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2055 _DBUS_ASSERT_ERROR_IS_SET (error);
2057 _dbus_hash_table_remove_string (activation->pending_activations,
2058 pending_activation->service_name);
2060 _dbus_string_free (&command);
2063 _dbus_string_free (&command);
2065 if (!add_bus_environment (activation, error))
2067 _DBUS_ASSERT_ERROR_IS_SET (error);
2068 dbus_free_string_array (argv);
2072 envp = bus_activation_get_environment (activation);
2076 BUS_SET_OOM (error);
2077 dbus_free_string_array (argv);
2081 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2082 if (servicehelper != NULL)
2083 bus_context_log (activation->context,
2084 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2087 bus_context_log (activation->context,
2088 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2091 dbus_error_init (&tmp_error);
2093 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2098 _dbus_verbose ("Failed to spawn child\n");
2099 bus_context_log (activation->context,
2100 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2103 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2104 dbus_move_error (&tmp_error, error);
2105 dbus_free_string_array (argv);
2106 dbus_free_string_array (envp);
2111 dbus_free_string_array (argv);
2114 _dbus_assert (pending_activation->babysitter != NULL);
2116 _dbus_babysitter_set_result_function (pending_activation->babysitter,
2117 pending_activation_finished_cb,
2118 pending_activation);
2120 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2121 add_babysitter_watch,
2122 remove_babysitter_watch,
2123 toggle_babysitter_watch,
2127 BUS_SET_OOM (error);
2128 _dbus_verbose ("Failed to set babysitter watch functions\n");
2136 bus_activation_list_services (BusActivation *activation,
2144 len = _dbus_hash_table_get_n_entries (activation->entries);
2145 retval = dbus_new (char *, len + 1);
2150 _dbus_hash_iter_init (activation->entries, &iter);
2152 while (_dbus_hash_iter_next (&iter))
2154 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2156 retval[i] = _dbus_strdup (entry->name);
2157 if (retval[i] == NULL)
2172 for (j = 0; j < i; j++)
2173 dbus_free (retval[i]);
2180 dbus_activation_systemd_failure (BusActivation *activation,
2181 DBusMessage *message)
2184 const char *code, *str, *unit = NULL;
2186 dbus_error_init(&error);
2188 /* This is called whenever the systemd activator sent us a
2189 response. We'll invalidate all pending activations that match the
2192 if (dbus_message_get_args (message, &error,
2193 DBUS_TYPE_STRING, &unit,
2194 DBUS_TYPE_STRING, &code,
2195 DBUS_TYPE_STRING, &str,
2197 dbus_set_error(&error, code, str);
2204 bus_context_log (activation->context,
2205 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2209 _dbus_hash_iter_init (activation->pending_activations,
2212 while (_dbus_hash_iter_next (&iter))
2214 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2216 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2217 pending_activation_failed(p, &error);
2221 dbus_error_free(&error);
2226 #ifdef DBUS_BUILD_TESTS
2230 #define SERVICE_NAME_1 "MyService1"
2231 #define SERVICE_NAME_2 "MyService2"
2232 #define SERVICE_NAME_3 "MyService3"
2234 #define SERVICE_FILE_1 "service-1.service"
2235 #define SERVICE_FILE_2 "service-2.service"
2236 #define SERVICE_FILE_3 "service-3.service"
2239 test_create_service_file (DBusString *dir,
2240 const char *filename,
2244 DBusString file_name, full_path;
2246 dbus_bool_t ret_val;
2249 _dbus_string_init_const (&file_name, filename);
2251 if (!_dbus_string_init (&full_path))
2254 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2255 !_dbus_concat_dir_and_file (&full_path, &file_name))
2261 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2268 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2272 _dbus_string_free (&full_path);
2277 test_remove_service_file (DBusString *dir, const char *filename)
2279 DBusString file_name, full_path;
2280 dbus_bool_t ret_val;
2284 _dbus_string_init_const (&file_name, filename);
2286 if (!_dbus_string_init (&full_path))
2289 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2290 !_dbus_concat_dir_and_file (&full_path, &file_name))
2296 if (!_dbus_delete_file (&full_path, NULL))
2303 _dbus_string_free (&full_path);
2308 test_remove_directory (DBusString *dir)
2311 DBusString filename, full_path;
2312 dbus_bool_t ret_val;
2316 if (!_dbus_string_init (&filename))
2319 if (!_dbus_string_init (&full_path))
2321 _dbus_string_free (&filename);
2325 iter = _dbus_directory_open (dir, NULL);
2332 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2334 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2340 _dbus_directory_close (iter);
2342 if (!_dbus_delete_directory (dir, NULL))
2349 _dbus_string_free (&filename);
2350 _dbus_string_free (&full_path);
2356 init_service_reload_test (DBusString *dir)
2360 if (!_dbus_stat (dir, &stat_buf, NULL))
2362 if (!_dbus_create_directory (dir, NULL))
2367 if (!test_remove_directory (dir))
2370 if (!_dbus_create_directory (dir, NULL))
2374 /* Create one initial file */
2375 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2382 cleanup_service_reload_test (DBusString *dir)
2384 if (!test_remove_directory (dir))
2392 BusActivation *activation;
2393 const char *service_name;
2394 dbus_bool_t expecting_find;
2398 check_func (void *data)
2401 BusActivationEntry *entry;
2403 dbus_bool_t ret_val;
2408 dbus_error_init (&error);
2410 entry = activation_find_entry (d->activation, d->service_name, &error);
2413 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2419 if (d->expecting_find)
2423 dbus_error_free (&error);
2427 if (!d->expecting_find)
2435 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2440 err = !_dbus_test_oom_handling (description, check_func, data);
2442 err = !check_func (data);
2445 _dbus_assert_not_reached ("Test failed");
2451 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2453 BusActivation *activation;
2455 DBusList *directories;
2459 _dbus_string_init_const (&address, "");
2461 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2464 activation = bus_activation_new (NULL, &address, &directories, NULL);
2468 d.activation = activation;
2470 /* Check for existing service file */
2471 d.expecting_find = TRUE;
2472 d.service_name = SERVICE_NAME_1;
2474 if (!do_test ("Existing service file", oom_test, &d))
2477 /* Check for non-existing service file */
2478 d.expecting_find = FALSE;
2479 d.service_name = SERVICE_NAME_3;
2481 if (!do_test ("Nonexisting service file", oom_test, &d))
2484 /* Check for added service file */
2485 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2488 d.expecting_find = TRUE;
2489 d.service_name = SERVICE_NAME_2;
2491 if (!do_test ("Added service file", oom_test, &d))
2494 /* Check for removed service file */
2495 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2498 d.expecting_find = FALSE;
2499 d.service_name = SERVICE_FILE_2;
2501 if (!do_test ("Removed service file", oom_test, &d))
2504 /* Check for updated service file */
2506 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2508 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2511 d.expecting_find = TRUE;
2512 d.service_name = SERVICE_NAME_3;
2514 if (!do_test ("Updated service file, part 1", oom_test, &d))
2517 d.expecting_find = FALSE;
2518 d.service_name = SERVICE_NAME_1;
2520 if (!do_test ("Updated service file, part 2", oom_test, &d))
2523 bus_activation_unref (activation);
2524 _dbus_list_clear (&directories);
2530 bus_activation_service_reload_test (const DBusString *test_data_dir)
2532 DBusString directory;
2534 if (!_dbus_string_init (&directory))
2537 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2540 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2541 !_dbus_generate_random_ascii (&directory, 6))
2546 /* Do normal tests */
2547 if (!init_service_reload_test (&directory))
2548 _dbus_assert_not_reached ("could not initiate service reload test");
2550 if (!do_service_reload_test (&directory, FALSE))
2556 if (!init_service_reload_test (&directory))
2557 _dbus_assert_not_reached ("could not initiate service reload test");
2559 if (!do_service_reload_test (&directory, TRUE))
2564 /* Cleanup test directory */
2565 if (!cleanup_service_reload_test (&directory))
2568 _dbus_string_free (&directory);
2573 #endif /* DBUS_BUILD_TESTS */