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 "config-parser.h"
30 #include "desktop-file.h"
35 #include <dbus/dbus-connection-internal.h>
36 #include <dbus/dbus-internals.h>
37 #include <dbus/dbus-hash.h>
38 #include <dbus/dbus-list.h>
39 #include <dbus/dbus-shell.h>
40 #include <dbus/dbus-spawn.h>
41 #include <dbus/dbus-timeout.h>
42 #include <dbus/dbus-sysdeps.h>
50 DBusHashTable *entries;
51 DBusHashTable *pending_activations;
54 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
55 * i.e. number of pending activation requests, not pending
58 DBusList *directories;
59 DBusHashTable *environment;
66 BusServiceDirFlags flags;
67 DBusHashTable *entries;
68 } BusServiceDirectory;
70 struct BusActivationEntry
76 char *systemd_service;
77 char *assumed_apparmor_label;
79 BusServiceDirectory *s_dir;
83 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
85 struct BusPendingActivationEntry
87 /* Normally a method call, but if connection is NULL, this is a signal
90 DBusMessage *activation_message;
91 /* NULL if this activation entry is for the dbus-daemon itself,
92 * waiting for systemd to start. In this case, auto_activation is always
95 DBusConnection *connection;
97 dbus_bool_t auto_activation;
99 dbus_bool_t is_put_back;
105 BusActivation *activation;
108 char *systemd_service;
111 DBusBabysitter *babysitter;
112 DBusTimeout *timeout;
113 unsigned int timeout_added : 1;
114 } BusPendingActivation;
117 static BusServiceDirectory *
118 bus_service_directory_ref (BusServiceDirectory *dir)
120 _dbus_assert (dir->refcount);
129 bus_service_directory_unref (BusServiceDirectory *dir)
134 _dbus_assert (dir->refcount > 0);
137 if (dir->refcount > 0)
141 _dbus_hash_table_unref (dir->entries);
143 dbus_free (dir->dir_c);
148 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
150 if (entry->activation_message)
151 dbus_message_unref (entry->activation_message);
153 if (entry->connection)
154 dbus_connection_unref (entry->connection);
159 static BusPendingActivation *
160 bus_pending_activation_ref (BusPendingActivation *pending_activation)
162 _dbus_assert (pending_activation->refcount > 0);
163 pending_activation->refcount += 1;
165 return pending_activation;
169 bus_pending_activation_unref (BusPendingActivation *pending_activation)
173 if (pending_activation == NULL) /* hash table requires this */
176 _dbus_assert (pending_activation->refcount > 0);
177 pending_activation->refcount -= 1;
179 if (pending_activation->refcount > 0)
182 if (pending_activation->timeout_added)
184 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
185 pending_activation->timeout);
186 pending_activation->timeout_added = FALSE;
189 if (pending_activation->timeout)
190 _dbus_timeout_unref (pending_activation->timeout);
192 if (pending_activation->babysitter)
194 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
196 pending_activation->babysitter,
198 _dbus_assert_not_reached ("setting watch functions to NULL failed");
200 _dbus_babysitter_unref (pending_activation->babysitter);
203 dbus_free (pending_activation->service_name);
204 dbus_free (pending_activation->exec);
205 dbus_free (pending_activation->systemd_service);
207 link = _dbus_list_get_first_link (&pending_activation->entries);
211 BusPendingActivationEntry *entry = link->data;
213 bus_pending_activation_entry_free (entry);
215 link = _dbus_list_get_next_link (&pending_activation->entries, link);
217 _dbus_list_clear (&pending_activation->entries);
219 pending_activation->activation->n_pending_activations -=
220 pending_activation->n_entries;
222 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
224 dbus_free (pending_activation);
227 static BusActivationEntry *
228 bus_activation_entry_ref (BusActivationEntry *entry)
230 _dbus_assert (entry->refcount > 0);
237 bus_activation_entry_unref (BusActivationEntry *entry)
239 if (entry == NULL) /* hash table requires this */
242 _dbus_assert (entry->refcount > 0);
245 if (entry->refcount > 0)
248 dbus_free (entry->name);
249 dbus_free (entry->exec);
250 dbus_free (entry->user);
251 dbus_free (entry->filename);
252 dbus_free (entry->systemd_service);
253 dbus_free (entry->assumed_apparmor_label);
259 update_desktop_file_entry (BusActivation *activation,
260 BusServiceDirectory *s_dir,
261 DBusString *filename,
262 BusDesktopFile *desktop_file,
265 char *name, *exec, *user, *exec_tmp, *systemd_service;
266 char *assumed_apparmor_label;
267 BusActivationEntry *entry;
269 DBusString file_path;
274 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
282 systemd_service = NULL;
283 assumed_apparmor_label = NULL;
285 dbus_error_init (&tmp_error);
287 if (!_dbus_string_init (&file_path))
293 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
294 !_dbus_concat_dir_and_file (&file_path, filename))
300 if (!_dbus_stat (&file_path, &stat_buf, NULL))
302 dbus_set_error (error, DBUS_ERROR_FAILED,
303 "Can't stat the service file\n");
307 if (!bus_desktop_file_get_string (desktop_file,
308 DBUS_SERVICE_SECTION,
314 if (!bus_desktop_file_get_string (desktop_file,
315 DBUS_SERVICE_SECTION,
321 if (!_dbus_string_init (&str))
324 if (!_dbus_string_append (&str, exec_tmp) ||
325 !_dbus_replace_install_prefix (&str) ||
326 !_dbus_string_steal_data (&str, &exec))
328 _dbus_string_free (&str);
332 _dbus_string_free (&str);
334 /* user is not _required_ unless we are using system activation */
335 if (!bus_desktop_file_get_string (desktop_file,
336 DBUS_SERVICE_SECTION,
340 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
341 /* if we got OOM, then exit */
342 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
344 dbus_move_error (&tmp_error, error);
349 /* if we have error because we didn't find anything then continue */
350 dbus_error_free (&tmp_error);
355 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
357 /* systemd service is never required */
358 if (!bus_desktop_file_get_string (desktop_file,
359 DBUS_SERVICE_SECTION,
360 DBUS_SERVICE_SYSTEMD_SERVICE,
361 &systemd_service, &tmp_error))
363 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
364 /* if we got OOM, then exit */
365 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
367 dbus_move_error (&tmp_error, error);
372 /* if we have error because we didn't find anything then continue */
373 dbus_error_free (&tmp_error);
374 dbus_free (systemd_service);
375 systemd_service = NULL;
379 /* assumed AppArmor label is never required */
380 if (!bus_desktop_file_get_string (desktop_file,
381 DBUS_SERVICE_SECTION,
382 DBUS_SERVICE_ASSUMED_APPARMOR_LABEL,
383 &assumed_apparmor_label, &tmp_error))
385 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
386 /* if we got OOM, then exit */
387 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
389 dbus_move_error (&tmp_error, error);
394 /* if we have error because we didn't find anything then continue */
395 dbus_error_free (&tmp_error);
396 dbus_free (assumed_apparmor_label);
397 assumed_apparmor_label = NULL;
401 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
403 entry = _dbus_hash_table_lookup_string (s_dir->entries,
404 _dbus_string_get_const_data (filename));
406 if (entry == NULL) /* New file */
408 DBusString expected_name;
410 if (!_dbus_string_init (&expected_name))
416 if (!_dbus_string_append (&expected_name, name) ||
417 !_dbus_string_append (&expected_name, ".service"))
419 _dbus_string_free (&expected_name);
424 if (_dbus_string_equal (&expected_name, filename))
426 _dbus_verbose ("Name of \"%s\" is as expected\n",
427 _dbus_string_get_const_data (&file_path));
429 else if (s_dir->flags & BUS_SERVICE_DIR_FLAGS_STRICT_NAMING)
431 bus_context_log_and_set_error (activation->context,
432 DBUS_SYSTEM_LOG_WARNING, error,
434 "Service file \"%s\" should have "
435 "been named \"%s\": not loading it",
436 _dbus_string_get_const_data (&file_path),
437 _dbus_string_get_const_data (&expected_name));
438 _dbus_string_free (&expected_name);
441 else if (bus_context_get_servicehelper (activation->context) != NULL)
443 bus_context_log (activation->context, DBUS_SYSTEM_LOG_WARNING,
444 "Service file \"%s\" should have been named \"%s\" "
445 "and will not work with system bus activation",
446 _dbus_string_get_const_data (&file_path),
447 _dbus_string_get_const_data (&expected_name));
448 /* We don't actually error out here, because *technically* it could
449 * still work on systemd systems, where we tell systemd to start the
450 * SystemdService instead of launching dbus-daemon-launch-helper
451 * ourselves. But maybe we should:
452 * https://bugs.freedesktop.org/show_bug.cgi?id=99874 */
456 /* We could maybe log mismatched names for session services in
457 * a user-visible way too, but not until
458 * https://lintian.debian.org/tags/dbus-session-service-wrong-name.html
460 * https://bugs.freedesktop.org/show_bug.cgi?id=99873 */
461 _dbus_verbose ("Name of \"%s\" should canonically be \"%s\"\n",
462 _dbus_string_get_const_data (&file_path),
463 _dbus_string_get_const_data (&expected_name));
466 _dbus_string_free (&expected_name);
468 /* FIXME we need a better-defined algorithm for which service file to
469 * pick than "whichever one is first in the directory listing"
470 * See also https://bugs.freedesktop.org/show_bug.cgi?id=99874
472 if (_dbus_hash_table_lookup_string (activation->entries, name))
474 dbus_set_error (error, DBUS_ERROR_FAILED,
475 "Service %s already exists in activation entry list\n", name);
479 entry = dbus_new0 (BusActivationEntry, 1);
489 entry->systemd_service = systemd_service;
490 entry->assumed_apparmor_label = assumed_apparmor_label;
493 /* ownership has been transferred to entry, do not free separately */
497 systemd_service = NULL;
498 assumed_apparmor_label = NULL;
500 entry->s_dir = s_dir;
501 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
502 if (!entry->filename)
508 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
514 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
516 /* Revert the insertion in the entries table */
517 _dbus_hash_table_remove_string (activation->entries, entry->name);
522 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
524 else /* Just update the entry */
526 bus_activation_entry_ref (entry);
527 _dbus_hash_table_remove_string (activation->entries, entry->name);
529 if (_dbus_hash_table_lookup_string (activation->entries, name))
531 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
532 name, _dbus_string_get_const_data (&file_path));
533 dbus_set_error (error, DBUS_ERROR_FAILED,
534 "The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
535 name, _dbus_string_get_const_data (&file_path));
539 /* ownership has been transferred to entry, do not free separately */
540 dbus_free (entry->name);
544 dbus_free (entry->exec);
548 dbus_free (entry->user);
552 dbus_free (entry->systemd_service);
553 entry->systemd_service = systemd_service;
554 systemd_service = NULL;
556 dbus_free (entry->assumed_apparmor_label);
557 entry->assumed_apparmor_label = assumed_apparmor_label;
558 assumed_apparmor_label = NULL;
560 if (!_dbus_hash_table_insert_string (activation->entries,
561 entry->name, bus_activation_entry_ref(entry)))
564 /* Also remove path to entries hash since we want this in sync with
565 * the entries hash table */
566 _dbus_hash_table_remove_string (entry->s_dir->entries,
572 entry->mtime = stat_buf.mtime;
576 /* if these have been transferred into entry, the variables will be NULL */
577 dbus_free (exec_tmp);
581 dbus_free (systemd_service);
582 dbus_free (assumed_apparmor_label);
583 _dbus_string_free (&file_path);
586 bus_activation_entry_unref (entry);
592 check_service_file (BusActivation *activation,
593 BusActivationEntry *entry,
594 BusActivationEntry **updated_entry,
599 BusActivationEntry *tmp_entry;
600 DBusString file_path;
606 _dbus_string_init_const (&filename, entry->filename);
608 if (!_dbus_string_init (&file_path))
614 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
615 !_dbus_concat_dir_and_file (&file_path, &filename))
622 if (!_dbus_stat (&file_path, &stat_buf, NULL))
624 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
625 _dbus_string_get_const_data (&file_path));
627 _dbus_hash_table_remove_string (activation->entries, entry->name);
628 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
636 if (stat_buf.mtime > entry->mtime)
638 BusDesktopFile *desktop_file;
641 dbus_error_init (&tmp_error);
643 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
644 if (desktop_file == NULL)
646 _dbus_verbose ("Could not load %s: %s\n",
647 _dbus_string_get_const_data (&file_path),
649 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
651 dbus_move_error (&tmp_error, error);
655 dbus_error_free (&tmp_error);
660 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
661 * Handle these both better
663 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
665 bus_desktop_file_free (desktop_file);
666 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
668 dbus_move_error (&tmp_error, error);
672 dbus_error_free (&tmp_error);
677 bus_desktop_file_free (desktop_file);
683 _dbus_string_free (&file_path);
685 if (updated_entry != NULL)
686 *updated_entry = tmp_entry;
691 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
692 * hash entries it already added.
695 update_directory (BusActivation *activation,
696 BusServiceDirectory *s_dir,
700 DBusString dir, filename;
701 BusDesktopFile *desktop_file;
704 BusActivationEntry *entry;
705 DBusString full_path;
707 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
712 _dbus_string_init_const (&dir, s_dir->dir_c);
714 if (!_dbus_string_init (&filename))
720 if (!_dbus_string_init (&full_path))
723 _dbus_string_free (&filename);
729 /* from this point it's safe to "goto out" */
731 iter = _dbus_directory_open (&dir, error);
734 _dbus_verbose ("Failed to open directory %s: %s\n",
736 error ? error->message : "unknown");
740 /* Now read the files */
741 dbus_error_init (&tmp_error);
742 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
744 _dbus_assert (!dbus_error_is_set (&tmp_error));
746 _dbus_string_set_length (&full_path, 0);
748 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
750 _dbus_verbose ("Skipping non-.service file '%s'\n",
751 _dbus_string_get_const_data (&filename));
755 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
756 if (entry) /* Already has this service file in the cache */
758 if (!check_service_file (activation, entry, NULL, error))
764 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
765 !_dbus_concat_dir_and_file (&full_path, &filename))
772 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
773 if (desktop_file == NULL)
775 _dbus_verbose ("Could not load %s: %s\n",
776 _dbus_string_get_const_data (&full_path),
779 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
781 dbus_move_error (&tmp_error, error);
785 dbus_error_free (&tmp_error);
789 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
790 * Handle these both better
792 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
794 bus_desktop_file_free (desktop_file);
797 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
798 _dbus_string_get_const_data (&full_path), tmp_error.message);
800 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
802 dbus_move_error (&tmp_error, error);
806 dbus_error_free (&tmp_error);
811 bus_desktop_file_free (desktop_file);
817 if (dbus_error_is_set (&tmp_error))
819 dbus_move_error (&tmp_error, error);
827 _DBUS_ASSERT_ERROR_IS_SET (error);
829 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
832 _dbus_directory_close (iter);
833 _dbus_string_free (&filename);
834 _dbus_string_free (&full_path);
840 populate_environment (BusActivation *activation)
843 dbus_bool_t retval = FALSE;
845 environment = _dbus_get_environment ();
847 if (environment == NULL)
850 retval = _dbus_hash_table_from_array (activation->environment, environment, '=');
851 dbus_free_string_array (environment);
857 bus_activation_reload (BusActivation *activation,
858 const DBusString *address,
859 DBusList **directories,
865 if (activation->server_address != NULL)
866 dbus_free (activation->server_address);
867 if (!_dbus_string_copy_data (address, &activation->server_address))
873 if (activation->entries != NULL)
874 _dbus_hash_table_unref (activation->entries);
875 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
876 (DBusFreeFunction)bus_activation_entry_unref);
877 if (activation->entries == NULL)
883 _dbus_list_foreach (&activation->directories,
884 (DBusForeachFunction) bus_service_directory_unref, NULL);
885 _dbus_list_clear (&activation->directories);
887 link = _dbus_list_get_first_link (directories);
890 BusConfigServiceDir *config = link->data;
891 BusServiceDirectory *s_dir;
893 _dbus_assert (config->path != NULL);
895 dir = _dbus_strdup (config->path);
902 s_dir = dbus_new0 (BusServiceDirectory, 1);
912 s_dir->flags = config->flags;
914 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
915 (DBusFreeFunction)bus_activation_entry_unref);
919 bus_service_directory_unref (s_dir);
924 if (!_dbus_list_append (&activation->directories, s_dir))
926 bus_service_directory_unref (s_dir);
931 /* only fail on OOM, it is ok if we can't read the directory */
932 if (!update_directory (activation, s_dir, error))
934 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
937 dbus_error_free (error);
940 link = _dbus_list_get_next_link (directories, link);
949 bus_activation_new (BusContext *context,
950 const DBusString *address,
951 DBusList **directories,
954 BusActivation *activation;
956 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
958 activation = dbus_new0 (BusActivation, 1);
959 if (activation == NULL)
965 activation->refcount = 1;
966 activation->context = context;
967 activation->n_pending_activations = 0;
969 if (!bus_activation_reload (activation, address, directories, error))
972 /* Initialize this hash table once, we don't want to lose pending
973 * activations on reload. */
974 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
975 (DBusFreeFunction)bus_pending_activation_unref);
977 if (activation->pending_activations == NULL)
983 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
984 (DBusFreeFunction) dbus_free,
985 (DBusFreeFunction) dbus_free);
987 if (activation->environment == NULL)
993 if (!populate_environment (activation))
1002 bus_activation_unref (activation);
1007 bus_activation_ref (BusActivation *activation)
1009 _dbus_assert (activation->refcount > 0);
1011 activation->refcount += 1;
1017 bus_activation_unref (BusActivation *activation)
1019 _dbus_assert (activation->refcount > 0);
1021 activation->refcount -= 1;
1023 if (activation->refcount > 0)
1026 dbus_free (activation->server_address);
1027 if (activation->entries)
1028 _dbus_hash_table_unref (activation->entries);
1029 if (activation->pending_activations)
1030 _dbus_hash_table_unref (activation->pending_activations);
1032 _dbus_list_foreach (&activation->directories,
1033 (DBusForeachFunction) bus_service_directory_unref, NULL);
1034 _dbus_list_clear (&activation->directories);
1036 if (activation->environment)
1037 _dbus_hash_table_unref (activation->environment);
1039 dbus_free (activation);
1043 add_bus_environment (BusActivation *activation,
1048 if (!bus_activation_set_environment_variable (activation,
1049 "DBUS_STARTER_ADDRESS",
1050 activation->server_address,
1054 type = bus_context_get_type (activation->context);
1057 if (!bus_activation_set_environment_variable (activation,
1058 "DBUS_STARTER_BUS_TYPE", type,
1062 if (strcmp (type, "session") == 0)
1064 if (!bus_activation_set_environment_variable (activation,
1065 "DBUS_SESSION_BUS_ADDRESS",
1066 activation->server_address,
1070 else if (strcmp (type, "system") == 0)
1072 if (!bus_activation_set_environment_variable (activation,
1073 "DBUS_SYSTEM_BUS_ADDRESS",
1074 activation->server_address,
1085 BusPendingActivation *pending_activation;
1086 DBusPreallocatedHash *hash_entry;
1087 } RestorePendingData;
1090 restore_pending (void *data)
1092 RestorePendingData *d = data;
1094 _dbus_assert (d->pending_activation != NULL);
1095 _dbus_assert (d->hash_entry != NULL);
1097 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1098 d->pending_activation->service_name,
1099 d->pending_activation->timeout_added);
1101 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1103 d->pending_activation->service_name, d->pending_activation);
1105 bus_pending_activation_ref (d->pending_activation);
1107 d->hash_entry = NULL;
1111 free_restore_pending_data (void *data)
1113 RestorePendingData *d = data;
1116 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1119 bus_pending_activation_unref (d->pending_activation);
1125 add_restore_pending_to_transaction (BusTransaction *transaction,
1126 BusPendingActivation *pending_activation)
1128 RestorePendingData *d;
1130 d = dbus_new (RestorePendingData, 1);
1134 d->pending_activation = pending_activation;
1135 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1137 bus_pending_activation_ref (d->pending_activation);
1139 if (d->hash_entry == NULL ||
1140 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1141 free_restore_pending_data))
1143 free_restore_pending_data (d);
1147 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1153 bus_activation_service_created (BusActivation *activation,
1154 const char *service_name,
1155 BusTransaction *transaction,
1158 BusPendingActivation *pending_activation;
1159 DBusMessage *message;
1162 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1164 /* Check if it's a pending activation */
1165 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1167 if (!pending_activation)
1170 bus_context_log (activation->context,
1171 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1174 link = _dbus_list_get_first_link (&pending_activation->entries);
1175 while (link != NULL)
1177 BusPendingActivationEntry *entry = link->data;
1178 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1180 /* entry->connection is NULL for activating systemd */
1181 if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1183 /* Only send activation replies to regular activation requests. */
1184 if (!entry->auto_activation)
1186 dbus_uint32_t result;
1188 message = dbus_message_new_method_return (entry->activation_message);
1191 BUS_SET_OOM (error);
1195 result = DBUS_START_REPLY_SUCCESS;
1197 if (!dbus_message_append_args (message,
1198 DBUS_TYPE_UINT32, &result,
1201 dbus_message_unref (message);
1202 BUS_SET_OOM (error);
1206 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1208 dbus_message_unref (message);
1209 BUS_SET_OOM (error);
1213 dbus_message_unref (message);
1227 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1228 BusService *service,
1229 BusTransaction *transaction)
1231 BusPendingActivation *pending_activation;
1234 /* Check if it's a pending activation */
1235 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1236 bus_service_get_name (service));
1238 if (!pending_activation)
1241 link = _dbus_list_get_first_link (&pending_activation->entries);
1242 while (link != NULL)
1244 BusPendingActivationEntry *entry = link->data;
1245 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1247 if (entry->auto_activation && !entry->is_put_back &&
1248 (entry->connection == NULL || dbus_connection_get_is_connected (entry->connection)))
1250 DBusConnection *addressed_recipient;
1253 dbus_error_init (&error);
1255 addressed_recipient = bus_service_get_primary_owners_connection (service);
1257 /* Resume dispatching where we left off in bus_dispatch() */
1258 switch (bus_dispatch_matches (transaction,
1260 addressed_recipient,
1261 entry->activation_message, NULL, &error))
1263 case BUS_RESULT_TRUE:
1265 case BUS_RESULT_FALSE:
1266 /* If permission is denied, we just want to return the error
1267 * to the original method invoker; in particular, we don't
1268 * want to make the RequestName call fail with that error
1269 * (see fd.o #78979, CVE-2014-3477). */
1270 if (!bus_transaction_send_error_reply (transaction, entry->connection,
1271 &error, entry->activation_message))
1273 bus_connection_send_oom_error (entry->connection,
1274 entry->activation_message);
1277 dbus_error_free (&error);
1280 case BUS_RESULT_LATER:
1282 DBusList *putback_message_link = link;
1283 DBusMessage *last_inserted_message = NULL;
1285 /* NULL entry->connection implies sending pending ActivationRequest message to systemd */
1286 if (entry->connection == NULL)
1288 _dbus_assert_not_reached ("bus_dispatch_matches returned BUS_RESULT_LATER unexpectedly when sender is NULL");
1294 * Getting here means that policy check result is not yet available and dispatching
1295 * messages from entry->connection has been disabled.
1296 * Let's put back all messages for the given connection in the incoming queue and mark
1297 * this entry as put back so they are not handled twice.
1299 while (putback_message_link != NULL)
1301 BusPendingActivationEntry *putback_message = putback_message_link->data;
1302 if (putback_message->connection == entry->connection)
1304 if (!_dbus_connection_putback_message (putback_message->connection, last_inserted_message,
1305 putback_message->activation_message, &error))
1307 last_inserted_message = putback_message->activation_message;
1308 putback_message->is_put_back = TRUE;
1311 putback_message_link = _dbus_list_get_next_link(&pending_activation->entries, putback_message_link);
1321 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1323 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1327 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1332 /* remove all messages that have been put to connections' incoming queues */
1333 link = _dbus_list_get_first_link (&pending_activation->entries);
1334 while (link != NULL)
1336 BusPendingActivationEntry *entry = link->data;
1337 if (entry->is_put_back)
1339 _dbus_connection_remove_message(entry->connection, entry->activation_message);
1340 entry->is_put_back = FALSE;
1342 link = _dbus_list_get_next_link(&pending_activation->entries, link);
1349 * FIXME @todo the error messages here would ideally be preallocated
1350 * so we don't need to allocate memory to send them.
1351 * Using the usual tactic, prealloc an OOM message, then
1352 * if we can't alloc the real error send the OOM error instead.
1355 try_send_activation_failure (BusPendingActivation *pending_activation,
1356 const DBusError *how)
1358 BusActivation *activation;
1360 BusTransaction *transaction;
1362 activation = pending_activation->activation;
1364 transaction = bus_transaction_new (activation->context);
1365 if (transaction == NULL)
1368 link = _dbus_list_get_first_link (&pending_activation->entries);
1369 while (link != NULL)
1371 BusPendingActivationEntry *entry = link->data;
1372 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1374 if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1376 if (!bus_transaction_send_error_reply (transaction,
1379 entry->activation_message))
1386 bus_transaction_execute_and_free (transaction);
1392 bus_transaction_cancel_and_free (transaction);
1397 * Free the pending activation and send an error message to all the
1398 * connections that were waiting for it.
1401 pending_activation_failed (BusPendingActivation *pending_activation,
1402 const DBusError *how)
1404 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1405 while (!try_send_activation_failure (pending_activation, how))
1406 _dbus_wait_for_memory ();
1408 /* Destroy this pending activation */
1409 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1410 pending_activation->service_name);
1414 * Depending on the exit code of the helper, set the error accordingly
1417 handle_servicehelper_exit_error (int exit_code,
1422 case BUS_SPAWN_EXIT_CODE_CONFIG_INVALID:
1423 dbus_set_error (error, DBUS_ERROR_SPAWN_CONFIG_INVALID,
1424 "Invalid configuration (missing or empty <user>?)");
1426 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1427 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1428 "Launcher could not run (out of memory)");
1430 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1431 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1432 "Failed to setup environment correctly");
1434 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1435 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1436 "Bus name is not valid or missing");
1438 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1439 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1440 "Bus name not found in system service directory");
1442 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1443 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1444 "The permission of the setuid helper is not correct");
1446 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1447 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1448 "The service file is incorrect or does not have all required attributes");
1450 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1451 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1452 "Cannot launch daemon, file not found or permissions invalid");
1454 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1455 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1456 "Invalid arguments to command line");
1458 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1459 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1460 "Launched child was signaled, it probably crashed");
1462 case BUS_SPAWN_EXIT_CODE_GENERIC_FAILURE:
1464 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1465 "Launch helper exited with unknown return code %i", exit_code);
1471 pending_activation_finished_cb (DBusBabysitter *babysitter,
1474 BusPendingActivation *pending_activation = data;
1475 dbus_bool_t uses_servicehelper;
1477 _dbus_assert (babysitter == pending_activation->babysitter);
1478 _dbus_babysitter_ref (babysitter);
1480 /* There are two major cases here; are we the system bus or the session? Here this
1481 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1482 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1484 * In both cases though, just ignore when a process exits with status 0; it's possible for
1485 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1486 * condition between this code and the child process claiming the bus name.
1488 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1490 /* strictly speaking this is redundant with the check in dbus-spawn now */
1491 if (_dbus_babysitter_get_child_exited (babysitter))
1495 dbus_bool_t activation_failed;
1498 dbus_error_init (&error);
1500 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1502 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1504 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1505 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1507 activation_failed = exit_code != 0;
1509 dbus_error_free(&error);
1511 if (activation_failed)
1513 if (uses_servicehelper)
1514 handle_servicehelper_exit_error (exit_code, &error);
1516 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1521 activation_failed = TRUE;
1524 if (activation_failed)
1526 bus_context_log (pending_activation->activation->context,
1527 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1528 pending_activation->service_name,
1531 /* Destroy all pending activations with the same exec */
1532 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1534 while (_dbus_hash_iter_next (&iter))
1536 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1538 if (p != pending_activation && p->exec != NULL &&
1539 strcmp (p->exec, pending_activation->exec) == 0)
1540 pending_activation_failed (p, &error);
1543 /* Destroys the pending activation */
1544 pending_activation_failed (pending_activation, &error);
1546 dbus_error_free (&error);
1550 _dbus_babysitter_unref (babysitter);
1554 add_babysitter_watch (DBusWatch *watch,
1557 BusPendingActivation *pending_activation = data;
1559 return _dbus_loop_add_watch (
1560 bus_context_get_loop (pending_activation->activation->context),
1565 remove_babysitter_watch (DBusWatch *watch,
1568 BusPendingActivation *pending_activation = data;
1570 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1575 toggle_babysitter_watch (DBusWatch *watch,
1578 BusPendingActivation *pending_activation = data;
1580 _dbus_loop_toggle_watch (bus_context_get_loop (pending_activation->activation->context),
1585 pending_activation_timed_out (void *data)
1587 BusPendingActivation *pending_activation = data;
1588 BusContext *context;
1592 context = pending_activation->activation->context;
1593 timeout = bus_context_get_activation_timeout (context);
1595 /* Kill the spawned process, since it sucks
1596 * (not sure this is what we want to do, but
1597 * may as well try it for now)
1599 if (pending_activation->babysitter)
1600 _dbus_babysitter_kill_child (pending_activation->babysitter);
1602 dbus_error_init (&error);
1604 bus_context_log_and_set_error (context, DBUS_SYSTEM_LOG_WARNING, &error,
1605 DBUS_ERROR_TIMED_OUT,
1606 "Failed to activate service '%s': timed out "
1607 "(service_start_timeout=%dms)",
1608 pending_activation->service_name, timeout);
1610 pending_activation_failed (pending_activation, &error);
1612 dbus_error_free (&error);
1618 cancel_pending (void *data)
1620 BusPendingActivation *pending_activation = data;
1622 _dbus_verbose ("Canceling pending activation of %s\n",
1623 pending_activation->service_name);
1625 if (pending_activation->babysitter)
1626 _dbus_babysitter_kill_child (pending_activation->babysitter);
1628 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1629 pending_activation->service_name);
1633 free_pending_cancel_data (void *data)
1635 BusPendingActivation *pending_activation = data;
1637 bus_pending_activation_unref (pending_activation);
1641 add_cancel_pending_to_transaction (BusTransaction *transaction,
1642 BusPendingActivation *pending_activation)
1644 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1646 free_pending_cancel_data))
1649 bus_pending_activation_ref (pending_activation);
1651 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1657 update_service_cache (BusActivation *activation, DBusError *error)
1661 for (iter = _dbus_list_get_first_link (&activation->directories);
1663 iter = _dbus_list_get_next_link (&activation->directories, iter))
1665 DBusError tmp_error;
1666 BusServiceDirectory *s_dir = iter->data;
1668 dbus_error_init (&tmp_error);
1669 if (!update_directory (activation, s_dir, &tmp_error))
1671 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1673 dbus_move_error (&tmp_error, error);
1677 dbus_error_free (&tmp_error);
1685 static BusActivationEntry *
1686 activation_find_entry (BusActivation *activation,
1687 const char *service_name,
1690 BusActivationEntry *entry;
1692 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1695 if (!update_service_cache (activation, error))
1698 entry = _dbus_hash_table_lookup_string (activation->entries,
1703 BusActivationEntry *updated_entry;
1705 if (!check_service_file (activation, entry, &updated_entry, error))
1708 entry = updated_entry;
1713 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1714 "The name %s was not provided by any .service files",
1723 bus_activation_get_environment (BusActivation *activation)
1725 return _dbus_hash_table_to_array (activation->environment, '=');
1729 bus_activation_set_environment_variable (BusActivation *activation,
1741 hash_key = _dbus_strdup (key);
1743 if (hash_key == NULL)
1746 hash_value = _dbus_strdup (value);
1748 if (hash_value == NULL)
1751 if (!_dbus_hash_table_insert_string (activation->environment,
1752 hash_key, hash_value))
1757 if (retval == FALSE)
1759 dbus_free (hash_key);
1760 dbus_free (hash_value);
1761 BUS_SET_OOM (error);
1768 child_setup (void *user_data)
1771 BusActivation *activation = user_data;
1772 DBusRLimit *initial_fd_limit;
1775 dbus_error_init (&error);
1776 initial_fd_limit = bus_context_get_initial_fd_limit (activation->context);
1778 if (initial_fd_limit != NULL &&
1779 !_dbus_rlimit_restore_fd_limit (initial_fd_limit, &error))
1781 /* unfortunately we don't actually know the service name here */
1782 bus_context_log (activation->context,
1783 DBUS_SYSTEM_LOG_WARNING,
1784 "Failed to reset fd limit before activating "
1786 error.name, error.message);
1793 bus_activation_activate_service (BusActivation *activation,
1794 DBusConnection *connection,
1795 BusTransaction *transaction,
1796 dbus_bool_t auto_activation,
1797 DBusMessage *activation_message,
1798 const char *service_name,
1800 BusDeferredMessage **deferred_message)
1802 DBusError tmp_error;
1803 BusActivationEntry *entry;
1804 BusPendingActivation *pending_activation;
1805 BusPendingActivationEntry *pending_activation_entry;
1806 DBusMessage *message;
1807 DBusString service_str;
1808 const char *servicehelper;
1812 BusResult retval = BUS_RESULT_FALSE;
1813 dbus_bool_t was_pending_activation;
1816 DBusSpawnFlags flags = DBUS_SPAWN_NONE;
1818 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1820 limit = bus_context_get_max_pending_activations (activation->context);
1822 if (activation->n_pending_activations >= limit)
1824 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1825 "The maximum number of pending activations has been "
1826 "reached, activation of %s failed "
1827 "(max_pending_service_starts=%d)",
1828 service_name, limit);
1829 return BUS_RESULT_FALSE;
1832 if (bus_context_get_systemd_activation (activation->context) &&
1833 strcmp (service_name, "org.freedesktop.systemd1") == 0)
1835 /* if we're doing systemd activation, we assume systemd will connect
1836 * eventually, and it does not need a .service file */
1841 entry = activation_find_entry (activation, service_name, error);
1843 return BUS_RESULT_FALSE;
1846 if (auto_activation && entry != NULL)
1848 switch (bus_context_check_security_policy (activation->context,
1850 connection, /* sender */
1851 NULL, /* addressed recipient */
1852 NULL, /* proposed recipient */
1858 case BUS_RESULT_TRUE:
1860 case BUS_RESULT_FALSE:
1861 _DBUS_ASSERT_ERROR_IS_SET (error);
1862 _dbus_verbose ("activation not authorized: %s: %s\n",
1863 error != NULL ? error->name : "(error ignored)",
1864 error != NULL ? error->message : "(error ignored)");
1865 return BUS_RESULT_FALSE;
1866 case BUS_RESULT_LATER:
1867 return BUS_RESULT_LATER;
1871 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1872 * call us if the service is already active.
1874 if (!auto_activation)
1876 /* Check if the service is active */
1877 _dbus_string_init_const (&service_str, service_name);
1878 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1880 dbus_uint32_t result;
1881 dbus_bool_t send_retval;
1883 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1885 message = dbus_message_new_method_return (activation_message);
1889 _dbus_verbose ("No memory to create reply to activate message\n");
1890 BUS_SET_OOM (error);
1891 return BUS_RESULT_FALSE;
1894 result = DBUS_START_REPLY_ALREADY_RUNNING;
1896 if (!dbus_message_append_args (message,
1897 DBUS_TYPE_UINT32, &result,
1900 _dbus_verbose ("No memory to set args of reply to activate message\n");
1901 BUS_SET_OOM (error);
1902 dbus_message_unref (message);
1903 return BUS_RESULT_FALSE;
1906 send_retval = bus_transaction_send_from_driver (transaction, connection, message);
1907 dbus_message_unref (message);
1910 _dbus_verbose ("Failed to send reply\n");
1911 BUS_SET_OOM (error);
1912 return BUS_RESULT_FALSE;
1915 return BUS_RESULT_TRUE;
1919 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1920 if (!pending_activation_entry)
1922 _dbus_verbose ("Failed to create pending activation entry\n");
1923 BUS_SET_OOM (error);
1924 return BUS_RESULT_FALSE;
1927 pending_activation_entry->auto_activation = auto_activation;
1929 pending_activation_entry->activation_message = activation_message;
1930 dbus_message_ref (activation_message);
1931 pending_activation_entry->connection = connection;
1933 dbus_connection_ref (connection);
1935 /* Check if the service is being activated */
1936 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1937 was_pending_activation = (pending_activation != NULL);
1938 if (was_pending_activation)
1940 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1942 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1944 BUS_SET_OOM (error);
1945 bus_pending_activation_entry_free (pending_activation_entry);
1946 return BUS_RESULT_FALSE;
1949 pending_activation->n_entries += 1;
1950 pending_activation->activation->n_pending_activations += 1;
1954 pending_activation = dbus_new0 (BusPendingActivation, 1);
1955 if (!pending_activation)
1957 _dbus_verbose ("Failed to create pending activation\n");
1959 BUS_SET_OOM (error);
1960 bus_pending_activation_entry_free (pending_activation_entry);
1961 return BUS_RESULT_FALSE;
1964 pending_activation->activation = activation;
1965 pending_activation->refcount = 1;
1967 pending_activation->service_name = _dbus_strdup (service_name);
1968 if (!pending_activation->service_name)
1970 _dbus_verbose ("Failed to copy service name for pending activation\n");
1972 BUS_SET_OOM (error);
1973 bus_pending_activation_unref (pending_activation);
1974 bus_pending_activation_entry_free (pending_activation_entry);
1975 return BUS_RESULT_FALSE;
1980 pending_activation->exec = _dbus_strdup (entry->exec);
1981 if (!pending_activation->exec)
1983 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1984 BUS_SET_OOM (error);
1985 bus_pending_activation_unref (pending_activation);
1986 bus_pending_activation_entry_free (pending_activation_entry);
1987 return BUS_RESULT_FALSE;
1991 if (entry != NULL && entry->systemd_service != NULL)
1993 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1994 if (!pending_activation->systemd_service)
1996 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1997 BUS_SET_OOM (error);
1998 bus_pending_activation_unref (pending_activation);
1999 bus_pending_activation_entry_free (pending_activation_entry);
2000 return BUS_RESULT_FALSE;
2004 pending_activation->timeout =
2005 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
2006 pending_activation_timed_out,
2009 if (!pending_activation->timeout)
2011 _dbus_verbose ("Failed to create timeout for pending activation\n");
2013 BUS_SET_OOM (error);
2014 bus_pending_activation_unref (pending_activation);
2015 bus_pending_activation_entry_free (pending_activation_entry);
2016 return BUS_RESULT_FALSE;
2019 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
2020 pending_activation->timeout))
2022 _dbus_verbose ("Failed to add timeout for pending activation\n");
2024 BUS_SET_OOM (error);
2025 bus_pending_activation_unref (pending_activation);
2026 bus_pending_activation_entry_free (pending_activation_entry);
2027 return BUS_RESULT_FALSE;
2030 pending_activation->timeout_added = TRUE;
2032 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
2034 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
2036 BUS_SET_OOM (error);
2037 bus_pending_activation_unref (pending_activation);
2038 bus_pending_activation_entry_free (pending_activation_entry);
2039 return BUS_RESULT_FALSE;
2042 pending_activation->n_entries += 1;
2043 pending_activation->activation->n_pending_activations += 1;
2045 if (!_dbus_hash_table_insert_string (activation->pending_activations,
2046 pending_activation->service_name,
2047 pending_activation))
2049 _dbus_verbose ("Failed to put pending activation in hash table\n");
2051 BUS_SET_OOM (error);
2052 bus_pending_activation_unref (pending_activation);
2053 return BUS_RESULT_FALSE;
2057 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
2059 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
2060 BUS_SET_OOM (error);
2061 goto cancel_pending_activation;
2064 if (was_pending_activation)
2065 return BUS_RESULT_TRUE;
2067 if (bus_context_get_systemd_activation (activation->context))
2069 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
2070 /* systemd itself is missing apparently. That can happen
2071 only during early startup. Let's just wait until systemd
2072 connects to us and do nothing. */
2073 return BUS_RESULT_TRUE;
2075 if (entry->systemd_service)
2077 BusTransaction *activation_transaction;
2078 DBusString service_string;
2079 BusService *service;
2080 BusRegistry *registry;
2081 DBusConnection *systemd = NULL;
2083 /* OK, we have a systemd service configured for this entry,
2084 hence let's enqueue an activation request message. This
2085 is implemented as a directed signal, not a method call,
2086 for three reasons: 1) we don't expect a response on
2087 success, where we just expect a name appearing on the
2088 bus; 2) at this time the systemd service might not yet
2089 have connected, so we wouldn't know the message serial at
2090 this point to set up a pending call; 3) it is ugly if the
2091 bus suddenly becomes the caller of a remote method. */
2093 message = dbus_message_new_signal (DBUS_PATH_DBUS,
2094 "org.freedesktop.systemd1.Activator",
2095 "ActivationRequest");
2098 _dbus_verbose ("No memory to create activation message\n");
2099 BUS_SET_OOM (error);
2100 goto cancel_pending_activation;
2103 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
2104 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
2105 !dbus_message_append_args (message,
2106 DBUS_TYPE_STRING, &entry->systemd_service,
2109 _dbus_verbose ("No memory to set args of activation message\n");
2110 dbus_message_unref (message);
2111 BUS_SET_OOM (error);
2112 goto cancel_pending_activation;
2115 /* Create our transaction */
2116 activation_transaction = bus_transaction_new (activation->context);
2117 if (activation_transaction == NULL)
2119 _dbus_verbose ("No memory to create activation transaction\n");
2120 dbus_message_unref (message);
2121 BUS_SET_OOM (error);
2122 goto cancel_pending_activation;
2125 /* Check whether systemd is already connected */
2126 registry = bus_connection_get_registry (connection);
2127 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
2128 service = bus_registry_lookup (registry, &service_string);
2131 systemd = bus_service_get_primary_owners_connection (service);
2133 /* Following the general principle of "log early and often",
2134 * we capture that we *want* to send the activation message, even if
2135 * systemd is not actually there to receive it yet */
2136 if (!bus_transaction_capture (activation_transaction,
2137 NULL, systemd, message))
2139 dbus_message_unref (message);
2140 BUS_SET_OOM (error);
2141 goto cancel_pending_activation;
2144 if (service != NULL)
2146 bus_context_log (activation->context,
2147 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s' requested by '%s' (%s)",
2149 entry->systemd_service,
2150 bus_connection_get_name (connection),
2151 bus_connection_get_loginfo (connection));
2152 /* Wonderful, systemd is connected, let's just send the msg */
2153 retval = bus_dispatch_matches (activation_transaction, NULL,
2154 systemd, message, NULL, error);
2155 if (BUS_RESULT_LATER == retval)
2156 _dbus_verbose("Unexpectedly need time to check message from bus driver to systemd - dropping the message.\n");
2160 bus_context_log (activation->context,
2161 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s' requested by '%s' (%s)",
2163 entry->systemd_service,
2164 bus_connection_get_name (connection),
2165 bus_connection_get_loginfo (connection));
2166 /* systemd is not around, let's "activate" it. */
2167 retval = bus_activation_activate_service (activation, NULL, activation_transaction, TRUE,
2168 message, "org.freedesktop.systemd1", error, deferred_message);
2171 dbus_message_unref (message);
2173 if (retval != BUS_RESULT_TRUE)
2175 bus_context_log (activation->context,
2176 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
2178 entry->systemd_service);
2179 _DBUS_ASSERT_ERROR_IS_SET (error);
2180 _dbus_verbose ("failed to send activation message: %s\n", error->name);
2181 bus_transaction_cancel_and_free (activation_transaction);
2182 goto cancel_pending_activation;
2185 bus_transaction_execute_and_free (activation_transaction);
2186 return BUS_RESULT_TRUE;
2189 /* OK, we have no configured systemd service, hence let's
2190 proceed with traditional activation. */
2193 /* If entry was NULL, it would be because we were doing systemd activation
2194 * and activating systemd itself; but we already handled that case with
2195 * an early-return */
2196 _dbus_assert (entry != NULL);
2198 /* use command as system and session different */
2199 if (!_dbus_string_init (&command))
2201 BUS_SET_OOM (error);
2202 goto cancel_pending_activation;
2205 /* does the bus use a helper? */
2206 servicehelper = bus_context_get_servicehelper (activation->context);
2207 if (servicehelper != NULL)
2209 if (entry->user == NULL)
2211 _dbus_string_free (&command);
2212 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2213 "Cannot do system-bus activation with no user\n");
2214 goto cancel_pending_activation;
2217 /* join the helper path and the service name */
2218 if (!_dbus_string_append (&command, servicehelper))
2220 _dbus_string_free (&command);
2221 BUS_SET_OOM (error);
2222 goto cancel_pending_activation;
2224 if (!_dbus_string_append (&command, " "))
2226 _dbus_string_free (&command);
2227 BUS_SET_OOM (error);
2228 goto cancel_pending_activation;
2230 if (!_dbus_string_append (&command, service_name))
2232 _dbus_string_free (&command);
2233 BUS_SET_OOM (error);
2234 goto cancel_pending_activation;
2239 /* the bus does not use a helper, so we can append arguments with the exec line */
2240 if (!_dbus_string_append (&command, entry->exec))
2242 _dbus_string_free (&command);
2243 BUS_SET_OOM (error);
2244 goto cancel_pending_activation;
2248 /* convert command into arguments */
2249 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2251 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2252 _DBUS_ASSERT_ERROR_IS_SET (error);
2253 _dbus_string_free (&command);
2254 goto cancel_pending_activation;
2256 _dbus_string_free (&command);
2258 if (!add_bus_environment (activation, error))
2260 _DBUS_ASSERT_ERROR_IS_SET (error);
2261 dbus_free_string_array (argv);
2262 goto cancel_pending_activation;
2265 envp = bus_activation_get_environment (activation);
2269 BUS_SET_OOM (error);
2270 dbus_free_string_array (argv);
2271 goto cancel_pending_activation;
2274 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2275 if (servicehelper != NULL)
2276 bus_context_log (activation->context,
2277 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' requested by '%s' (%s) (using servicehelper)",
2279 bus_connection_get_name (connection),
2280 bus_connection_get_loginfo (connection));
2282 bus_context_log (activation->context,
2283 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' requested by '%s' (%s)",
2285 bus_connection_get_name (connection),
2286 bus_connection_get_loginfo (connection));
2288 dbus_error_init (&tmp_error);
2290 if (bus_context_get_using_syslog (activation->context))
2291 flags |= DBUS_SPAWN_REDIRECT_OUTPUT;
2293 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter,
2302 _dbus_verbose ("Failed to spawn child\n");
2303 bus_context_log (activation->context,
2304 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2307 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2308 dbus_move_error (&tmp_error, error);
2309 dbus_free_string_array (argv);
2310 dbus_free_string_array (envp);
2311 goto cancel_pending_activation;
2314 dbus_free_string_array (argv);
2317 _dbus_assert (pending_activation->babysitter != NULL);
2319 _dbus_babysitter_set_result_function (pending_activation->babysitter,
2320 pending_activation_finished_cb,
2321 pending_activation);
2323 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2324 add_babysitter_watch,
2325 remove_babysitter_watch,
2326 toggle_babysitter_watch,
2330 BUS_SET_OOM (error);
2331 _dbus_verbose ("Failed to set babysitter watch functions\n");
2332 goto cancel_pending_activation;
2335 return BUS_RESULT_TRUE;
2337 cancel_pending_activation:
2338 _DBUS_ASSERT_ERROR_IS_SET (error);
2339 _dbus_hash_table_remove_string (activation->pending_activations,
2340 pending_activation->service_name);
2341 return BUS_RESULT_FALSE;
2345 bus_activation_list_services (BusActivation *activation,
2353 len = _dbus_hash_table_get_n_entries (activation->entries);
2354 retval = dbus_new (char *, len + 1);
2359 _dbus_hash_iter_init (activation->entries, &iter);
2361 while (_dbus_hash_iter_next (&iter))
2363 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2365 retval[i] = _dbus_strdup (entry->name);
2366 if (retval[i] == NULL)
2381 for (j = 0; j < i; j++)
2382 dbus_free (retval[j]);
2389 dbus_activation_systemd_failure (BusActivation *activation,
2390 DBusMessage *message)
2393 const char *code, *str, *unit = NULL;
2395 dbus_error_init(&error);
2397 /* This is called whenever the systemd activator sent us a
2398 response. We'll invalidate all pending activations that match the
2401 if (dbus_message_get_args (message, &error,
2402 DBUS_TYPE_STRING, &unit,
2403 DBUS_TYPE_STRING, &code,
2404 DBUS_TYPE_STRING, &str,
2406 dbus_set_error (&error, code, "%s", str);
2413 bus_context_log (activation->context,
2414 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2418 _dbus_hash_iter_init (activation->pending_activations,
2421 while (_dbus_hash_iter_next (&iter))
2423 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2425 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2426 pending_activation_failed(p, &error);
2430 dbus_error_free(&error);
2436 bus_activation_entry_get_assumed_apparmor_label (BusActivationEntry *entry)
2438 return entry->assumed_apparmor_label;
2441 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2445 #define SERVICE_NAME_1 "com.example.MyService1"
2446 #define SERVICE_NAME_2 "org.example.MyService2"
2447 #define SERVICE_NAME_3 "net.example.MyService3"
2449 #define SERVICE_FILE_1 "service-1.service"
2450 #define SERVICE_FILE_2 "service-2.service"
2451 #define SERVICE_FILE_3 "service-3.service"
2454 test_create_service_file (DBusString *dir,
2455 const char *filename,
2459 DBusString file_name, full_path;
2461 dbus_bool_t ret_val;
2464 _dbus_string_init_const (&file_name, filename);
2466 if (!_dbus_string_init (&full_path))
2469 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2470 !_dbus_concat_dir_and_file (&full_path, &file_name))
2476 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2483 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2487 _dbus_string_free (&full_path);
2492 test_remove_service_file (DBusString *dir, const char *filename)
2494 DBusString file_name, full_path;
2495 dbus_bool_t ret_val;
2499 _dbus_string_init_const (&file_name, filename);
2501 if (!_dbus_string_init (&full_path))
2504 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2505 !_dbus_concat_dir_and_file (&full_path, &file_name))
2511 if (!_dbus_delete_file (&full_path, NULL))
2518 _dbus_string_free (&full_path);
2523 test_remove_directory (DBusString *dir)
2526 DBusString filename, full_path;
2527 dbus_bool_t ret_val;
2531 if (!_dbus_string_init (&filename))
2534 if (!_dbus_string_init (&full_path))
2536 _dbus_string_free (&filename);
2540 iter = _dbus_directory_open (dir, NULL);
2547 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2549 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2552 _dbus_directory_close (iter);
2556 _dbus_directory_close (iter);
2558 if (!_dbus_delete_directory (dir, NULL))
2565 _dbus_string_free (&filename);
2566 _dbus_string_free (&full_path);
2572 init_service_reload_test (DBusString *dir)
2574 if (!_dbus_create_directory (dir, NULL))
2577 /* Create one initial file */
2578 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2585 cleanup_service_reload_test (DBusString *dir)
2587 if (!test_remove_directory (dir))
2595 BusActivation *activation;
2596 const char *service_name;
2597 dbus_bool_t expecting_find;
2601 check_func (void *data)
2604 BusActivationEntry *entry;
2606 dbus_bool_t ret_val;
2611 dbus_error_init (&error);
2613 entry = activation_find_entry (d->activation, d->service_name, &error);
2616 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2622 if (d->expecting_find)
2626 dbus_error_free (&error);
2630 if (!d->expecting_find)
2638 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2643 err = !_dbus_test_oom_handling (description, check_func, data);
2645 err = !check_func (data);
2648 _dbus_assert_not_reached ("Test failed");
2654 do_service_reload_test (const DBusString *test_data_dir,
2656 dbus_bool_t oom_test)
2658 BusActivation *activation;
2659 BusConfigServiceDir config;
2660 BusContext *context;
2662 DBusList *directories;
2666 _dbus_string_init_const (&address, "");
2668 config.path = _dbus_string_get_data (dir);
2669 config.flags = BUS_SERVICE_DIR_FLAGS_NONE;
2671 if (!_dbus_list_append (&directories, &config))
2674 context = bus_context_new_test (test_data_dir,
2675 "valid-config-files/debug-allow-all.conf");
2676 if (context == NULL)
2679 activation = bus_activation_new (context, &address, &directories, NULL);
2683 d.activation = activation;
2685 /* Check for existing service file */
2686 d.expecting_find = TRUE;
2687 d.service_name = SERVICE_NAME_1;
2689 if (!do_test ("Existing service file", oom_test, &d))
2692 /* Check for non-existing service file */
2693 d.expecting_find = FALSE;
2694 d.service_name = SERVICE_NAME_3;
2696 if (!do_test ("Nonexisting service file", oom_test, &d))
2699 /* Check for added service file */
2700 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2703 d.expecting_find = TRUE;
2704 d.service_name = SERVICE_NAME_2;
2706 if (!do_test ("Added service file", oom_test, &d))
2709 /* Check for removed service file */
2710 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2713 d.expecting_find = FALSE;
2714 d.service_name = SERVICE_FILE_2;
2716 if (!do_test ("Removed service file", oom_test, &d))
2719 /* Check for updated service file */
2721 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2723 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2726 d.expecting_find = TRUE;
2727 d.service_name = SERVICE_NAME_3;
2729 if (!do_test ("Updated service file, part 1", oom_test, &d))
2732 d.expecting_find = FALSE;
2733 d.service_name = SERVICE_NAME_1;
2735 if (!do_test ("Updated service file, part 2", oom_test, &d))
2738 bus_activation_unref (activation);
2739 _dbus_list_clear (&directories);
2740 bus_context_unref (context);
2746 bus_activation_service_reload_test (const DBusString *test_data_dir)
2748 DBusString directory;
2750 dbus_bool_t ret = FALSE;
2752 if (!_dbus_string_init (&directory))
2755 tmp = _dbus_get_tmpdir ();
2760 if (!_dbus_string_append (&directory, tmp))
2763 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2764 !_dbus_generate_random_ascii (&directory, 6, NULL))
2767 /* Do normal tests */
2768 if (!init_service_reload_test (&directory))
2769 _dbus_assert_not_reached ("could not initiate service reload test");
2771 if (!do_service_reload_test (test_data_dir, &directory, FALSE))
2776 if (!cleanup_service_reload_test (&directory))
2780 if (!init_service_reload_test (&directory))
2781 _dbus_assert_not_reached ("could not initiate service reload test");
2783 if (!do_service_reload_test (test_data_dir, &directory, TRUE))
2788 /* Cleanup test directory */
2789 if (!cleanup_service_reload_test (&directory))
2795 _dbus_string_free (&directory);
2799 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */