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
7 * Copyright (C) 2013 Samsung Electronics
9 * Licensed under the Academic Free License version 2.1
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "activation.h"
29 #include "activation-exit-codes.h"
30 #include "desktop-file.h"
35 #include <dbus/dbus-internals.h>
36 #include <dbus/dbus-hash.h>
37 #include <dbus/dbus-list.h>
38 #include <dbus/dbus-shell.h>
39 #include <dbus/dbus-spawn.h>
40 #include <dbus/dbus-timeout.h>
41 #include <dbus/dbus-sysdeps.h>
49 DBusHashTable *entries;
50 DBusHashTable *pending_activations;
53 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
54 * i.e. number of pending activation requests, not pending
57 DBusHashTable *directories;
58 DBusHashTable *environment;
65 DBusHashTable *entries;
66 } BusServiceDirectory;
74 char *systemd_service;
76 BusServiceDirectory *s_dir;
80 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
82 struct BusPendingActivationEntry
84 /* Normally a method call, but if connection is NULL, this is a signal
87 DBusMessage *activation_message;
88 /* NULL if this activation entry is for the dbus-daemon itself,
89 * waiting for systemd to start. In this case, auto_activation is always
92 DBusConnection *connection;
94 dbus_bool_t auto_activation;
100 BusActivation *activation;
103 char *systemd_service;
106 DBusBabysitter *babysitter;
107 DBusTimeout *timeout;
108 unsigned int timeout_added : 1;
109 } BusPendingActivation;
112 static BusServiceDirectory *
113 bus_service_directory_ref (BusServiceDirectory *dir)
115 _dbus_assert (dir->refcount);
124 bus_service_directory_unref (BusServiceDirectory *dir)
129 _dbus_assert (dir->refcount > 0);
132 if (dir->refcount > 0)
136 _dbus_hash_table_unref (dir->entries);
138 dbus_free (dir->dir_c);
143 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
145 if (entry->activation_message)
146 dbus_message_unref (entry->activation_message);
148 if (entry->connection)
149 dbus_connection_unref (entry->connection);
154 static BusPendingActivation *
155 bus_pending_activation_ref (BusPendingActivation *pending_activation)
157 _dbus_assert (pending_activation->refcount > 0);
158 pending_activation->refcount += 1;
160 return pending_activation;
164 bus_pending_activation_unref (BusPendingActivation *pending_activation)
168 if (pending_activation == NULL) /* hash table requires this */
171 _dbus_assert (pending_activation->refcount > 0);
172 pending_activation->refcount -= 1;
174 if (pending_activation->refcount > 0)
177 if (pending_activation->timeout_added)
179 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
180 pending_activation->timeout);
181 pending_activation->timeout_added = FALSE;
184 if (pending_activation->timeout)
185 _dbus_timeout_unref (pending_activation->timeout);
187 if (pending_activation->babysitter)
189 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
191 pending_activation->babysitter,
193 _dbus_assert_not_reached ("setting watch functions to NULL failed");
195 _dbus_babysitter_unref (pending_activation->babysitter);
198 dbus_free (pending_activation->service_name);
199 dbus_free (pending_activation->exec);
200 dbus_free (pending_activation->systemd_service);
202 link = _dbus_list_get_first_link (&pending_activation->entries);
206 BusPendingActivationEntry *entry = link->data;
208 bus_pending_activation_entry_free (entry);
210 link = _dbus_list_get_next_link (&pending_activation->entries, link);
212 _dbus_list_clear (&pending_activation->entries);
214 pending_activation->activation->n_pending_activations -=
215 pending_activation->n_entries;
217 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
219 dbus_free (pending_activation);
222 static BusActivationEntry *
223 bus_activation_entry_ref (BusActivationEntry *entry)
225 _dbus_assert (entry->refcount > 0);
232 bus_activation_entry_unref (BusActivationEntry *entry)
234 if (entry == NULL) /* hash table requires this */
237 _dbus_assert (entry->refcount > 0);
240 if (entry->refcount > 0)
243 dbus_free (entry->name);
244 dbus_free (entry->exec);
245 dbus_free (entry->user);
246 dbus_free (entry->filename);
247 dbus_free (entry->systemd_service);
253 update_desktop_file_entry (BusActivation *activation,
254 BusServiceDirectory *s_dir,
255 DBusString *filename,
256 BusDesktopFile *desktop_file,
259 char *name, *exec, *user, *exec_tmp, *systemd_service;
260 BusActivationEntry *entry;
262 DBusString file_path;
266 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
274 systemd_service = NULL;
276 dbus_error_init (&tmp_error);
278 if (!_dbus_string_init (&file_path))
284 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
285 !_dbus_concat_dir_and_file (&file_path, filename))
291 if (!_dbus_stat (&file_path, &stat_buf, NULL))
293 dbus_set_error (error, DBUS_ERROR_FAILED,
294 "Can't stat the service file\n");
298 if (!bus_desktop_file_get_string (desktop_file,
299 DBUS_SERVICE_SECTION,
305 if (!bus_desktop_file_get_string (desktop_file,
306 DBUS_SERVICE_SECTION,
312 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
313 dbus_free (exec_tmp);
316 /* user is not _required_ unless we are using system activation */
317 if (!bus_desktop_file_get_string (desktop_file,
318 DBUS_SERVICE_SECTION,
322 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
323 /* if we got OOM, then exit */
324 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
326 dbus_move_error (&tmp_error, error);
331 /* if we have error because we didn't find anything then continue */
332 dbus_error_free (&tmp_error);
337 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
339 /* systemd service is never required */
340 if (!bus_desktop_file_get_string (desktop_file,
341 DBUS_SERVICE_SECTION,
342 DBUS_SERVICE_SYSTEMD_SERVICE,
343 &systemd_service, &tmp_error))
345 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
346 /* if we got OOM, then exit */
347 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
349 dbus_move_error (&tmp_error, error);
354 /* if we have error because we didn't find anything then continue */
355 dbus_error_free (&tmp_error);
356 dbus_free (systemd_service);
357 systemd_service = NULL;
361 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
363 entry = _dbus_hash_table_lookup_string (s_dir->entries,
364 _dbus_string_get_const_data (filename));
366 if (entry == NULL) /* New file */
368 /* FIXME we need a better-defined algorithm for which service file to
369 * pick than "whichever one is first in the directory listing"
371 if (_dbus_hash_table_lookup_string (activation->entries, name))
373 dbus_set_error (error, DBUS_ERROR_FAILED,
374 "Service %s already exists in activation entry list\n", name);
378 entry = dbus_new0 (BusActivationEntry, 1);
388 entry->systemd_service = systemd_service;
391 /* ownership has been transferred to entry, do not free separately */
395 systemd_service = NULL;
397 entry->s_dir = s_dir;
398 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
399 if (!entry->filename)
405 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
411 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
413 /* Revert the insertion in the entries table */
414 _dbus_hash_table_remove_string (activation->entries, entry->name);
419 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
421 else /* Just update the entry */
423 bus_activation_entry_ref (entry);
424 _dbus_hash_table_remove_string (activation->entries, entry->name);
426 if (_dbus_hash_table_lookup_string (activation->entries, name))
428 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
429 name, _dbus_string_get_const_data (&file_path));
430 dbus_set_error (error, DBUS_ERROR_FAILED,
431 "The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
432 name, _dbus_string_get_const_data (&file_path));
436 /* ownership has been transferred to entry, do not free separately */
437 dbus_free (entry->name);
441 dbus_free (entry->exec);
445 dbus_free (entry->user);
449 dbus_free (entry->systemd_service);
450 entry->systemd_service = systemd_service;
451 systemd_service = NULL;
453 if (!_dbus_hash_table_insert_string (activation->entries,
454 entry->name, bus_activation_entry_ref(entry)))
457 /* Also remove path to entries hash since we want this in sync with
458 * the entries hash table */
459 _dbus_hash_table_remove_string (entry->s_dir->entries,
465 entry->mtime = stat_buf.mtime;
469 /* if these have been transferred into entry, the variables will be NULL */
473 dbus_free (systemd_service);
474 _dbus_string_free (&file_path);
477 bus_activation_entry_unref (entry);
483 check_service_file (BusActivation *activation,
484 BusActivationEntry *entry,
485 BusActivationEntry **updated_entry,
490 BusActivationEntry *tmp_entry;
491 DBusString file_path;
497 _dbus_string_init_const (&filename, entry->filename);
499 if (!_dbus_string_init (&file_path))
505 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
506 !_dbus_concat_dir_and_file (&file_path, &filename))
513 if (!_dbus_stat (&file_path, &stat_buf, NULL))
515 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
516 _dbus_string_get_const_data (&file_path));
518 _dbus_hash_table_remove_string (activation->entries, entry->name);
519 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
527 if (stat_buf.mtime > entry->mtime)
529 BusDesktopFile *desktop_file;
532 dbus_error_init (&tmp_error);
534 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
535 if (desktop_file == NULL)
537 _dbus_verbose ("Could not load %s: %s\n",
538 _dbus_string_get_const_data (&file_path),
540 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
542 dbus_move_error (&tmp_error, error);
546 dbus_error_free (&tmp_error);
551 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
552 * Handle these both better
554 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
556 bus_desktop_file_free (desktop_file);
557 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
559 dbus_move_error (&tmp_error, error);
563 dbus_error_free (&tmp_error);
568 bus_desktop_file_free (desktop_file);
574 _dbus_string_free (&file_path);
576 if (updated_entry != NULL)
577 *updated_entry = tmp_entry;
582 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
583 * hash entries it already added.
586 update_directory (BusActivation *activation,
587 BusServiceDirectory *s_dir,
591 DBusString dir, filename;
592 BusDesktopFile *desktop_file;
595 BusActivationEntry *entry;
596 DBusString full_path;
598 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
603 _dbus_string_init_const (&dir, s_dir->dir_c);
605 if (!_dbus_string_init (&filename))
611 if (!_dbus_string_init (&full_path))
614 _dbus_string_free (&filename);
620 /* from this point it's safe to "goto out" */
622 iter = _dbus_directory_open (&dir, error);
625 _dbus_verbose ("Failed to open directory %s: %s\n",
627 error ? error->message : "unknown");
631 /* Now read the files */
632 dbus_error_init (&tmp_error);
633 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
635 _dbus_assert (!dbus_error_is_set (&tmp_error));
637 _dbus_string_set_length (&full_path, 0);
639 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
641 _dbus_verbose ("Skipping non-.service file %s\n",
642 _dbus_string_get_const_data (&filename));
646 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
647 if (entry) /* Already has this service file in the cache */
649 if (!check_service_file (activation, entry, NULL, error))
655 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
656 !_dbus_concat_dir_and_file (&full_path, &filename))
663 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
664 if (desktop_file == NULL)
666 _dbus_verbose ("Could not load %s: %s\n",
667 _dbus_string_get_const_data (&full_path),
670 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
672 dbus_move_error (&tmp_error, error);
676 dbus_error_free (&tmp_error);
680 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
681 * Handle these both better
683 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
685 bus_desktop_file_free (desktop_file);
688 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
689 _dbus_string_get_const_data (&full_path), tmp_error.message);
691 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
693 dbus_move_error (&tmp_error, error);
697 dbus_error_free (&tmp_error);
702 bus_desktop_file_free (desktop_file);
708 if (dbus_error_is_set (&tmp_error))
710 dbus_move_error (&tmp_error, error);
718 _DBUS_ASSERT_ERROR_IS_SET (error);
720 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
723 _dbus_directory_close (iter);
724 _dbus_string_free (&filename);
725 _dbus_string_free (&full_path);
731 populate_environment (BusActivation *activation)
737 dbus_bool_t retval = FALSE;
739 environment = _dbus_get_environment ();
741 if (environment == NULL)
744 if (!_dbus_string_init (&key))
746 dbus_free_string_array (environment);
750 if (!_dbus_string_init (&value))
752 _dbus_string_free (&key);
753 dbus_free_string_array (environment);
757 for (i = 0; environment[i] != NULL; i++)
759 if (!_dbus_string_append (&key, environment[i]))
762 if (_dbus_string_split_on_byte (&key, '=', &value))
764 char *hash_key, *hash_value;
766 if (!_dbus_string_steal_data (&key, &hash_key))
769 if (!_dbus_string_steal_data (&value, &hash_value))
772 if (!_dbus_hash_table_insert_string (activation->environment,
773 hash_key, hash_value))
776 _dbus_string_set_length (&key, 0);
777 _dbus_string_set_length (&value, 0);
780 if (environment[i] != NULL)
786 _dbus_string_free (&key);
787 _dbus_string_free (&value);
788 dbus_free_string_array (environment);
794 bus_activation_reload (BusActivation *activation,
795 const DBusString *address,
796 DBusList **directories,
802 if (activation->server_address != NULL)
803 dbus_free (activation->server_address);
804 if (!_dbus_string_copy_data (address, &activation->server_address))
810 if (activation->entries != NULL)
811 _dbus_hash_table_unref (activation->entries);
812 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
813 (DBusFreeFunction)bus_activation_entry_unref);
814 if (activation->entries == NULL)
820 if (activation->directories != NULL)
821 _dbus_hash_table_unref (activation->directories);
822 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
823 (DBusFreeFunction)bus_service_directory_unref);
825 if (activation->directories == NULL)
831 link = _dbus_list_get_first_link (directories);
834 BusServiceDirectory *s_dir;
836 dir = _dbus_strdup ((const char *) link->data);
843 s_dir = dbus_new0 (BusServiceDirectory, 1);
854 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
855 (DBusFreeFunction)bus_activation_entry_unref);
859 bus_service_directory_unref (s_dir);
864 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
866 bus_service_directory_unref (s_dir);
871 /* only fail on OOM, it is ok if we can't read the directory */
872 if (!update_directory (activation, s_dir, error))
874 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
877 dbus_error_free (error);
880 link = _dbus_list_get_next_link (directories, link);
889 bus_activation_new (BusContext *context,
890 const DBusString *address,
891 DBusList **directories,
894 BusActivation *activation;
896 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
898 activation = dbus_new0 (BusActivation, 1);
899 if (activation == NULL)
905 activation->refcount = 1;
906 activation->context = context;
907 activation->n_pending_activations = 0;
909 if (!bus_activation_reload (activation, address, directories, error))
912 /* Initialize this hash table once, we don't want to lose pending
913 * activations on reload. */
914 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
915 (DBusFreeFunction)bus_pending_activation_unref);
917 if (activation->pending_activations == NULL)
923 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
924 (DBusFreeFunction) dbus_free,
925 (DBusFreeFunction) dbus_free);
927 if (activation->environment == NULL)
933 if (!populate_environment (activation))
942 bus_activation_unref (activation);
947 bus_activation_ref (BusActivation *activation)
949 _dbus_assert (activation->refcount > 0);
951 activation->refcount += 1;
957 bus_activation_unref (BusActivation *activation)
959 _dbus_assert (activation->refcount > 0);
961 activation->refcount -= 1;
963 if (activation->refcount > 0)
966 dbus_free (activation->server_address);
967 if (activation->entries)
968 _dbus_hash_table_unref (activation->entries);
969 if (activation->pending_activations)
970 _dbus_hash_table_unref (activation->pending_activations);
971 if (activation->directories)
972 _dbus_hash_table_unref (activation->directories);
973 if (activation->environment)
974 _dbus_hash_table_unref (activation->environment);
976 dbus_free (activation);
980 add_bus_environment (BusActivation *activation,
985 if (!bus_activation_set_environment_variable (activation,
986 "DBUS_STARTER_ADDRESS",
987 activation->server_address,
991 type = bus_context_get_type (activation->context);
994 if (!bus_activation_set_environment_variable (activation,
995 "DBUS_STARTER_BUS_TYPE", type,
999 if (strcmp (type, "session") == 0)
1001 if (!bus_activation_set_environment_variable (activation,
1002 "DBUS_SESSION_BUS_ADDRESS",
1003 activation->server_address,
1007 else if (strcmp (type, "system") == 0)
1009 if (!bus_activation_set_environment_variable (activation,
1010 "DBUS_SYSTEM_BUS_ADDRESS",
1011 activation->server_address,
1022 BusPendingActivation *pending_activation;
1023 DBusPreallocatedHash *hash_entry;
1024 } RestorePendingData;
1027 restore_pending (void *data)
1029 RestorePendingData *d = data;
1031 _dbus_assert (d->pending_activation != NULL);
1032 _dbus_assert (d->hash_entry != NULL);
1034 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1035 d->pending_activation->service_name,
1036 d->pending_activation->timeout_added);
1038 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1040 d->pending_activation->service_name, d->pending_activation);
1042 bus_pending_activation_ref (d->pending_activation);
1044 d->hash_entry = NULL;
1048 free_pending_restore_data (void *data)
1050 RestorePendingData *d = data;
1053 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1056 bus_pending_activation_unref (d->pending_activation);
1062 add_restore_pending_to_transaction (BusTransaction *transaction,
1063 BusPendingActivation *pending_activation)
1065 RestorePendingData *d;
1067 d = dbus_new (RestorePendingData, 1);
1071 d->pending_activation = pending_activation;
1072 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1074 bus_pending_activation_ref (d->pending_activation);
1076 if (d->hash_entry == NULL ||
1077 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1078 free_pending_restore_data))
1080 free_pending_restore_data (d);
1084 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1090 bus_activation_service_created (BusActivation *activation,
1091 const char *service_name,
1092 BusTransaction *transaction,
1095 BusPendingActivation *pending_activation;
1096 DBusMessage *message;
1099 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1101 /* Check if it's a pending activation */
1102 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1104 if (!pending_activation)
1107 bus_context_log (activation->context,
1108 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1111 link = _dbus_list_get_first_link (&pending_activation->entries);
1112 while (link != NULL)
1114 BusPendingActivationEntry *entry = link->data;
1115 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1117 /* entry->connection is NULL for activating systemd */
1118 if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1120 /* Only send activation replies to regular activation requests. */
1121 if (!entry->auto_activation)
1123 dbus_uint32_t result;
1125 message = dbus_message_new_method_return (entry->activation_message);
1128 BUS_SET_OOM (error);
1132 result = DBUS_START_REPLY_SUCCESS;
1134 if (!dbus_message_append_args (message,
1135 DBUS_TYPE_UINT32, &result,
1138 dbus_message_unref (message);
1139 BUS_SET_OOM (error);
1143 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1145 dbus_message_unref (message);
1146 BUS_SET_OOM (error);
1150 dbus_message_unref (message);
1164 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1165 BusService *service,
1166 BusTransaction *transaction,
1169 BusPendingActivation *pending_activation;
1172 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1174 /* Check if it's a pending activation */
1175 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1176 bus_service_get_name (service));
1178 if (!pending_activation)
1181 link = _dbus_list_get_first_link (&pending_activation->entries);
1182 while (link != NULL)
1184 BusPendingActivationEntry *entry = link->data;
1185 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1187 if (entry->auto_activation && (entry->connection == NULL || dbus_connection_get_is_connected (entry->connection)))
1189 DBusConnection *addressed_recipient;
1191 #ifdef ENABLE_KDBUS_TRANSPORT
1192 /* kdbus change - we can not send anything using phantom connections
1193 * (DBusConnection structures for services other than daemon)
1194 * so we have to use daemon connection
1196 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1197 addressed_recipient = entry->connection;
1200 addressed_recipient = bus_service_get_primary_owners_connection (service);
1202 /* Resume dispatching where we left off in bus_dispatch() */
1203 if (!bus_dispatch_matches (transaction,
1205 addressed_recipient,
1206 entry->activation_message, error))
1213 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1215 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1216 BUS_SET_OOM (error);
1220 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1229 * FIXME @todo the error messages here would ideally be preallocated
1230 * so we don't need to allocate memory to send them.
1231 * Using the usual tactic, prealloc an OOM message, then
1232 * if we can't alloc the real error send the OOM error instead.
1235 try_send_activation_failure (BusPendingActivation *pending_activation,
1236 const DBusError *how)
1238 BusActivation *activation;
1240 BusTransaction *transaction;
1242 activation = pending_activation->activation;
1244 transaction = bus_transaction_new (activation->context);
1245 if (transaction == NULL)
1248 link = _dbus_list_get_first_link (&pending_activation->entries);
1249 while (link != NULL)
1251 BusPendingActivationEntry *entry = link->data;
1252 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1254 if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1256 if (!bus_transaction_send_error_reply (transaction,
1259 entry->activation_message))
1266 bus_transaction_execute_and_free (transaction);
1272 bus_transaction_cancel_and_free (transaction);
1277 * Free the pending activation and send an error message to all the
1278 * connections that were waiting for it.
1281 pending_activation_failed (BusPendingActivation *pending_activation,
1282 const DBusError *how)
1284 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1285 while (!try_send_activation_failure (pending_activation, how))
1286 _dbus_wait_for_memory ();
1288 /* Destroy this pending activation */
1289 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1290 pending_activation->service_name);
1294 * Depending on the exit code of the helper, set the error accordingly
1297 handle_servicehelper_exit_error (int exit_code,
1302 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1303 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1304 "Launcher could not run (out of memory)");
1306 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1307 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1308 "Failed to setup environment correctly");
1310 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1311 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1312 "Bus name is not valid or missing");
1314 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1315 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1316 "Bus name not found in system service directory");
1318 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1319 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1320 "The permission of the setuid helper is not correct");
1322 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1323 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1324 "The service file is incorrect or does not have all required attributes");
1326 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1327 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1328 "Cannot launch daemon, file not found or permissions invalid");
1330 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1331 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1332 "Invalid arguments to command line");
1334 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1335 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1336 "Launched child was signaled, it probably crashed");
1339 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1340 "Launch helper exited with unknown return code %i", exit_code);
1346 pending_activation_finished_cb (DBusBabysitter *babysitter,
1349 BusPendingActivation *pending_activation = data;
1350 dbus_bool_t uses_servicehelper;
1352 _dbus_assert (babysitter == pending_activation->babysitter);
1353 _dbus_babysitter_ref (babysitter);
1355 /* There are two major cases here; are we the system bus or the session? Here this
1356 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1357 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1359 * In both cases though, just ignore when a process exits with status 0; it's possible for
1360 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1361 * condition between this code and the child process claiming the bus name.
1363 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1365 /* strictly speaking this is redundant with the check in dbus-spawn now */
1366 if (_dbus_babysitter_get_child_exited (babysitter))
1370 dbus_bool_t activation_failed;
1373 dbus_error_init (&error);
1375 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1377 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1379 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1380 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1382 activation_failed = exit_code != 0;
1384 dbus_error_free(&error);
1386 if (activation_failed)
1388 if (uses_servicehelper)
1389 handle_servicehelper_exit_error (exit_code, &error);
1391 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1396 activation_failed = TRUE;
1399 if (activation_failed)
1401 bus_context_log (pending_activation->activation->context,
1402 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1403 pending_activation->service_name,
1406 /* Destroy all pending activations with the same exec */
1407 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1409 while (_dbus_hash_iter_next (&iter))
1411 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1413 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1414 pending_activation_failed (p, &error);
1417 /* Destroys the pending activation */
1418 pending_activation_failed (pending_activation, &error);
1420 dbus_error_free (&error);
1424 _dbus_babysitter_unref (babysitter);
1428 add_babysitter_watch (DBusWatch *watch,
1431 BusPendingActivation *pending_activation = data;
1433 return _dbus_loop_add_watch (
1434 bus_context_get_loop (pending_activation->activation->context),
1439 remove_babysitter_watch (DBusWatch *watch,
1442 BusPendingActivation *pending_activation = data;
1444 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1449 toggle_babysitter_watch (DBusWatch *watch,
1452 BusPendingActivation *pending_activation = data;
1454 _dbus_loop_toggle_watch (bus_context_get_loop (pending_activation->activation->context),
1459 pending_activation_timed_out (void *data)
1461 BusPendingActivation *pending_activation = data;
1464 /* Kill the spawned process, since it sucks
1465 * (not sure this is what we want to do, but
1466 * may as well try it for now)
1468 if (pending_activation->babysitter)
1469 _dbus_babysitter_kill_child (pending_activation->babysitter);
1471 dbus_error_init (&error);
1473 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1474 "Activation of %s timed out",
1475 pending_activation->service_name);
1476 bus_context_log (pending_activation->activation->context,
1477 DBUS_SYSTEM_LOG_INFO,
1478 "Failed to activate service '%s': timed out",
1479 pending_activation->service_name);
1481 pending_activation_failed (pending_activation, &error);
1483 dbus_error_free (&error);
1489 cancel_pending (void *data)
1491 BusPendingActivation *pending_activation = data;
1493 _dbus_verbose ("Canceling pending activation of %s\n",
1494 pending_activation->service_name);
1496 if (pending_activation->babysitter)
1497 _dbus_babysitter_kill_child (pending_activation->babysitter);
1499 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1500 pending_activation->service_name);
1504 free_pending_cancel_data (void *data)
1506 BusPendingActivation *pending_activation = data;
1508 bus_pending_activation_unref (pending_activation);
1512 add_cancel_pending_to_transaction (BusTransaction *transaction,
1513 BusPendingActivation *pending_activation)
1515 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1517 free_pending_cancel_data))
1520 bus_pending_activation_ref (pending_activation);
1522 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1528 update_service_cache (BusActivation *activation, DBusError *error)
1532 _dbus_hash_iter_init (activation->directories, &iter);
1533 while (_dbus_hash_iter_next (&iter))
1535 DBusError tmp_error;
1536 BusServiceDirectory *s_dir;
1538 s_dir = _dbus_hash_iter_get_value (&iter);
1540 dbus_error_init (&tmp_error);
1541 if (!update_directory (activation, s_dir, &tmp_error))
1543 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1545 dbus_move_error (&tmp_error, error);
1549 dbus_error_free (&tmp_error);
1557 static BusActivationEntry *
1558 activation_find_entry (BusActivation *activation,
1559 const char *service_name,
1562 BusActivationEntry *entry;
1564 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1567 if (!update_service_cache (activation, error))
1570 entry = _dbus_hash_table_lookup_string (activation->entries,
1575 BusActivationEntry *updated_entry;
1577 if (!check_service_file (activation, entry, &updated_entry, error))
1580 entry = updated_entry;
1585 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1586 "The name %s was not provided by any .service files",
1595 bus_activation_get_environment (BusActivation *activation)
1602 length = _dbus_hash_table_get_n_entries (activation->environment);
1604 environment = dbus_new0 (char *, length + 1);
1606 if (environment == NULL)
1610 _dbus_hash_iter_init (activation->environment, &iter);
1612 if (!_dbus_string_init (&entry))
1614 dbus_free_string_array (environment);
1618 while (_dbus_hash_iter_next (&iter))
1620 const char *key, *value;
1622 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1623 value = (const char *) _dbus_hash_iter_get_value (&iter);
1625 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1628 if (!_dbus_string_steal_data (&entry, environment + i))
1633 _dbus_string_free (&entry);
1637 dbus_free_string_array (environment);
1645 bus_activation_set_environment_variable (BusActivation *activation,
1657 hash_key = _dbus_strdup (key);
1659 if (hash_key == NULL)
1662 hash_value = _dbus_strdup (value);
1664 if (hash_value == NULL)
1667 if (!_dbus_hash_table_insert_string (activation->environment,
1668 hash_key, hash_value))
1673 if (retval == FALSE)
1675 dbus_free (hash_key);
1676 dbus_free (hash_value);
1677 BUS_SET_OOM (error);
1684 bus_activation_activate_service (BusActivation *activation,
1685 DBusConnection *connection,
1686 BusTransaction *transaction,
1687 dbus_bool_t auto_activation,
1688 DBusMessage *activation_message,
1689 const char *service_name,
1692 DBusError tmp_error;
1693 BusActivationEntry *entry;
1694 BusPendingActivation *pending_activation;
1695 BusPendingActivationEntry *pending_activation_entry;
1696 DBusMessage *message;
1697 DBusString service_str;
1698 const char *servicehelper;
1703 dbus_bool_t was_pending_activation;
1706 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1708 if (activation->n_pending_activations >=
1709 bus_context_get_max_pending_activations (activation->context))
1711 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1712 "The maximum number of pending activations has been reached, activation of %s failed",
1717 entry = activation_find_entry (activation, service_name, error);
1721 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1722 * call us if the service is already active.
1724 if (!auto_activation)
1726 /* Check if the service is active */
1727 _dbus_string_init_const (&service_str, service_name);
1728 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1730 dbus_uint32_t result;
1732 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1734 message = dbus_message_new_method_return (activation_message);
1738 _dbus_verbose ("No memory to create reply to activate message\n");
1739 BUS_SET_OOM (error);
1743 result = DBUS_START_REPLY_ALREADY_RUNNING;
1745 if (!dbus_message_append_args (message,
1746 DBUS_TYPE_UINT32, &result,
1749 _dbus_verbose ("No memory to set args of reply to activate message\n");
1750 BUS_SET_OOM (error);
1751 dbus_message_unref (message);
1755 retval = bus_transaction_send_from_driver (transaction, connection, message);
1756 dbus_message_unref (message);
1759 _dbus_verbose ("Failed to send reply\n");
1760 BUS_SET_OOM (error);
1767 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1768 if (!pending_activation_entry)
1770 _dbus_verbose ("Failed to create pending activation entry\n");
1771 BUS_SET_OOM (error);
1775 pending_activation_entry->auto_activation = auto_activation;
1777 pending_activation_entry->activation_message = activation_message;
1778 dbus_message_ref (activation_message);
1779 pending_activation_entry->connection = connection;
1781 dbus_connection_ref (connection);
1783 /* Check if the service is being activated */
1784 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1785 was_pending_activation = (pending_activation != NULL);
1786 if (was_pending_activation)
1788 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1790 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1792 BUS_SET_OOM (error);
1793 bus_pending_activation_entry_free (pending_activation_entry);
1797 pending_activation->n_entries += 1;
1798 pending_activation->activation->n_pending_activations += 1;
1802 pending_activation = dbus_new0 (BusPendingActivation, 1);
1803 if (!pending_activation)
1805 _dbus_verbose ("Failed to create pending activation\n");
1807 BUS_SET_OOM (error);
1808 bus_pending_activation_entry_free (pending_activation_entry);
1812 pending_activation->activation = activation;
1813 pending_activation->refcount = 1;
1815 pending_activation->service_name = _dbus_strdup (service_name);
1816 if (!pending_activation->service_name)
1818 _dbus_verbose ("Failed to copy service name for pending activation\n");
1820 BUS_SET_OOM (error);
1821 bus_pending_activation_unref (pending_activation);
1822 bus_pending_activation_entry_free (pending_activation_entry);
1826 pending_activation->exec = _dbus_strdup (entry->exec);
1827 if (!pending_activation->exec)
1829 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1830 BUS_SET_OOM (error);
1831 bus_pending_activation_unref (pending_activation);
1832 bus_pending_activation_entry_free (pending_activation_entry);
1836 if (entry->systemd_service)
1838 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1839 if (!pending_activation->systemd_service)
1841 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1842 BUS_SET_OOM (error);
1843 bus_pending_activation_unref (pending_activation);
1844 bus_pending_activation_entry_free (pending_activation_entry);
1849 pending_activation->timeout =
1850 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1851 pending_activation_timed_out,
1854 if (!pending_activation->timeout)
1856 _dbus_verbose ("Failed to create timeout for pending activation\n");
1858 BUS_SET_OOM (error);
1859 bus_pending_activation_unref (pending_activation);
1860 bus_pending_activation_entry_free (pending_activation_entry);
1864 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1865 pending_activation->timeout))
1867 _dbus_verbose ("Failed to add timeout for pending activation\n");
1869 BUS_SET_OOM (error);
1870 bus_pending_activation_unref (pending_activation);
1871 bus_pending_activation_entry_free (pending_activation_entry);
1875 pending_activation->timeout_added = TRUE;
1877 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1879 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1881 BUS_SET_OOM (error);
1882 bus_pending_activation_unref (pending_activation);
1883 bus_pending_activation_entry_free (pending_activation_entry);
1887 pending_activation->n_entries += 1;
1888 pending_activation->activation->n_pending_activations += 1;
1890 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1891 pending_activation->service_name,
1892 pending_activation))
1894 _dbus_verbose ("Failed to put pending activation in hash table\n");
1896 BUS_SET_OOM (error);
1897 bus_pending_activation_unref (pending_activation);
1902 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1904 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1905 BUS_SET_OOM (error);
1906 _dbus_hash_table_remove_string (activation->pending_activations,
1907 pending_activation->service_name);
1912 if (was_pending_activation)
1915 if (bus_context_get_systemd_activation (activation->context))
1917 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1918 /* systemd itself is missing apparently. That can happen
1919 only during early startup. Let's just wait until systemd
1920 connects to us and do nothing. */
1923 if (entry->systemd_service)
1925 BusTransaction *activation_transaction;
1926 DBusString service_string;
1927 BusService *service;
1928 BusRegistry *registry;
1930 /* OK, we have a systemd service configured for this entry,
1931 hence let's enqueue an activation request message. This
1932 is implemented as a directed signal, not a method call,
1933 for three reasons: 1) we don't expect a response on
1934 success, where we just expect a name appearing on the
1935 bus; 2) at this time the systemd service might not yet
1936 have connected, so we wouldn't know the message serial at
1937 this point to set up a pending call; 3) it is ugly if the
1938 bus suddenly becomes the caller of a remote method. */
1940 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1941 "org.freedesktop.systemd1.Activator",
1942 "ActivationRequest");
1945 _dbus_verbose ("No memory to create activation message\n");
1946 BUS_SET_OOM (error);
1950 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1951 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1952 !dbus_message_append_args (message,
1953 DBUS_TYPE_STRING, &entry->systemd_service,
1956 _dbus_verbose ("No memory to set args of activation message\n");
1957 dbus_message_unref (message);
1958 BUS_SET_OOM (error);
1962 /* Create our transaction */
1963 activation_transaction = bus_transaction_new (activation->context);
1964 if (activation_transaction == NULL)
1966 _dbus_verbose ("No memory to create activation transaction\n");
1967 dbus_message_unref (message);
1968 BUS_SET_OOM (error);
1972 /* Check whether systemd is already connected */
1973 registry = bus_connection_get_registry (connection);
1974 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1975 service = bus_registry_lookup (registry, &service_string);
1977 if (service != NULL)
1979 bus_context_log (activation->context,
1980 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1982 entry->systemd_service);
1983 /* Wonderful, systemd is connected, let's just send the msg */
1984 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1989 bus_context_log (activation->context,
1990 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1992 entry->systemd_service);
1993 /* systemd is not around, let's "activate" it. */
1994 retval = bus_activation_activate_service (activation, NULL, activation_transaction, TRUE,
1995 message, "org.freedesktop.systemd1", error);
1998 dbus_message_unref (message);
2002 bus_context_log (activation->context,
2003 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
2005 entry->systemd_service);
2006 _DBUS_ASSERT_ERROR_IS_SET (error);
2007 _dbus_verbose ("failed to send activation message: %s\n", error->name);
2008 bus_transaction_cancel_and_free (activation_transaction);
2012 bus_transaction_execute_and_free (activation_transaction);
2016 /* OK, we have no configured systemd service, hence let's
2017 proceed with traditional activation. */
2020 /* use command as system and session different */
2021 if (!_dbus_string_init (&command))
2023 BUS_SET_OOM (error);
2027 /* does the bus use a helper? */
2028 servicehelper = bus_context_get_servicehelper (activation->context);
2029 if (servicehelper != NULL)
2031 if (entry->user == NULL)
2033 _dbus_string_free (&command);
2034 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2035 "Cannot do system-bus activation with no user\n");
2039 /* join the helper path and the service name */
2040 if (!_dbus_string_append (&command, servicehelper))
2042 _dbus_string_free (&command);
2043 BUS_SET_OOM (error);
2046 if (!_dbus_string_append (&command, " "))
2048 _dbus_string_free (&command);
2049 BUS_SET_OOM (error);
2052 if (!_dbus_string_append (&command, service_name))
2054 _dbus_string_free (&command);
2055 BUS_SET_OOM (error);
2061 /* the bus does not use a helper, so we can append arguments with the exec line */
2062 if (!_dbus_string_append (&command, entry->exec))
2064 _dbus_string_free (&command);
2065 BUS_SET_OOM (error);
2070 /* convert command into arguments */
2071 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2073 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2074 _DBUS_ASSERT_ERROR_IS_SET (error);
2076 _dbus_hash_table_remove_string (activation->pending_activations,
2077 pending_activation->service_name);
2079 _dbus_string_free (&command);
2082 _dbus_string_free (&command);
2084 if (!add_bus_environment (activation, error))
2086 _DBUS_ASSERT_ERROR_IS_SET (error);
2087 dbus_free_string_array (argv);
2091 envp = bus_activation_get_environment (activation);
2095 BUS_SET_OOM (error);
2096 dbus_free_string_array (argv);
2100 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2101 if (servicehelper != NULL)
2102 bus_context_log (activation->context,
2103 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2106 bus_context_log (activation->context,
2107 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2110 dbus_error_init (&tmp_error);
2112 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2117 _dbus_verbose ("Failed to spawn child\n");
2118 bus_context_log (activation->context,
2119 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2122 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2123 dbus_move_error (&tmp_error, error);
2124 dbus_free_string_array (argv);
2125 dbus_free_string_array (envp);
2130 dbus_free_string_array (argv);
2133 _dbus_assert (pending_activation->babysitter != NULL);
2135 _dbus_babysitter_set_result_function (pending_activation->babysitter,
2136 pending_activation_finished_cb,
2137 pending_activation);
2139 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2140 add_babysitter_watch,
2141 remove_babysitter_watch,
2142 toggle_babysitter_watch,
2146 BUS_SET_OOM (error);
2147 _dbus_verbose ("Failed to set babysitter watch functions\n");
2155 bus_activation_list_services (BusActivation *activation,
2163 len = _dbus_hash_table_get_n_entries (activation->entries);
2164 retval = dbus_new (char *, len + 1);
2169 _dbus_hash_iter_init (activation->entries, &iter);
2171 while (_dbus_hash_iter_next (&iter))
2173 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2175 retval[i] = _dbus_strdup (entry->name);
2176 if (retval[i] == NULL)
2191 for (j = 0; j < i; j++)
2192 dbus_free (retval[j]);
2199 dbus_activation_systemd_failure (BusActivation *activation,
2200 DBusMessage *message)
2203 const char *code, *str, *unit = NULL;
2205 dbus_error_init(&error);
2207 /* This is called whenever the systemd activator sent us a
2208 response. We'll invalidate all pending activations that match the
2211 if (dbus_message_get_args (message, &error,
2212 DBUS_TYPE_STRING, &unit,
2213 DBUS_TYPE_STRING, &code,
2214 DBUS_TYPE_STRING, &str,
2216 dbus_set_error(&error, code, str);
2223 bus_context_log (activation->context,
2224 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2228 _dbus_hash_iter_init (activation->pending_activations,
2231 while (_dbus_hash_iter_next (&iter))
2233 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2235 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2236 pending_activation_failed(p, &error);
2240 dbus_error_free(&error);
2245 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2249 #define SERVICE_NAME_1 "MyService1"
2250 #define SERVICE_NAME_2 "MyService2"
2251 #define SERVICE_NAME_3 "MyService3"
2253 #define SERVICE_FILE_1 "service-1.service"
2254 #define SERVICE_FILE_2 "service-2.service"
2255 #define SERVICE_FILE_3 "service-3.service"
2258 test_create_service_file (DBusString *dir,
2259 const char *filename,
2263 DBusString file_name, full_path;
2265 dbus_bool_t ret_val;
2268 _dbus_string_init_const (&file_name, filename);
2270 if (!_dbus_string_init (&full_path))
2273 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2274 !_dbus_concat_dir_and_file (&full_path, &file_name))
2280 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2287 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2291 _dbus_string_free (&full_path);
2296 test_remove_service_file (DBusString *dir, const char *filename)
2298 DBusString file_name, full_path;
2299 dbus_bool_t ret_val;
2303 _dbus_string_init_const (&file_name, filename);
2305 if (!_dbus_string_init (&full_path))
2308 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2309 !_dbus_concat_dir_and_file (&full_path, &file_name))
2315 if (!_dbus_delete_file (&full_path, NULL))
2322 _dbus_string_free (&full_path);
2327 test_remove_directory (DBusString *dir)
2330 DBusString filename, full_path;
2331 dbus_bool_t ret_val;
2335 if (!_dbus_string_init (&filename))
2338 if (!_dbus_string_init (&full_path))
2340 _dbus_string_free (&filename);
2344 iter = _dbus_directory_open (dir, NULL);
2351 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2353 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2359 _dbus_directory_close (iter);
2361 if (!_dbus_delete_directory (dir, NULL))
2368 _dbus_string_free (&filename);
2369 _dbus_string_free (&full_path);
2375 init_service_reload_test (DBusString *dir)
2379 if (!_dbus_stat (dir, &stat_buf, NULL))
2381 if (!_dbus_create_directory (dir, NULL))
2386 if (!test_remove_directory (dir))
2389 if (!_dbus_create_directory (dir, NULL))
2393 /* Create one initial file */
2394 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2401 cleanup_service_reload_test (DBusString *dir)
2403 if (!test_remove_directory (dir))
2411 BusActivation *activation;
2412 const char *service_name;
2413 dbus_bool_t expecting_find;
2417 check_func (void *data)
2420 BusActivationEntry *entry;
2422 dbus_bool_t ret_val;
2427 dbus_error_init (&error);
2429 entry = activation_find_entry (d->activation, d->service_name, &error);
2432 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2438 if (d->expecting_find)
2442 dbus_error_free (&error);
2446 if (!d->expecting_find)
2454 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2459 err = !_dbus_test_oom_handling (description, check_func, data);
2461 err = !check_func (data);
2464 _dbus_assert_not_reached ("Test failed");
2470 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2472 BusActivation *activation;
2474 DBusList *directories;
2478 _dbus_string_init_const (&address, "");
2480 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2483 activation = bus_activation_new (NULL, &address, &directories, NULL);
2487 d.activation = activation;
2489 /* Check for existing service file */
2490 d.expecting_find = TRUE;
2491 d.service_name = SERVICE_NAME_1;
2493 if (!do_test ("Existing service file", oom_test, &d))
2496 /* Check for non-existing service file */
2497 d.expecting_find = FALSE;
2498 d.service_name = SERVICE_NAME_3;
2500 if (!do_test ("Nonexisting service file", oom_test, &d))
2503 /* Check for added service file */
2504 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2507 d.expecting_find = TRUE;
2508 d.service_name = SERVICE_NAME_2;
2510 if (!do_test ("Added service file", oom_test, &d))
2513 /* Check for removed service file */
2514 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2517 d.expecting_find = FALSE;
2518 d.service_name = SERVICE_FILE_2;
2520 if (!do_test ("Removed service file", oom_test, &d))
2523 /* Check for updated service file */
2525 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2527 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2530 d.expecting_find = TRUE;
2531 d.service_name = SERVICE_NAME_3;
2533 if (!do_test ("Updated service file, part 1", oom_test, &d))
2536 d.expecting_find = FALSE;
2537 d.service_name = SERVICE_NAME_1;
2539 if (!do_test ("Updated service file, part 2", oom_test, &d))
2542 bus_activation_unref (activation);
2543 _dbus_list_clear (&directories);
2549 bus_activation_service_reload_test (const DBusString *test_data_dir)
2551 DBusString directory;
2554 if (!_dbus_string_init (&directory))
2557 tmp = _dbus_get_tmpdir ();
2562 if (!_dbus_string_append (&directory, tmp))
2565 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2566 !_dbus_generate_random_ascii (&directory, 6))
2571 /* Do normal tests */
2572 if (!init_service_reload_test (&directory))
2573 _dbus_assert_not_reached ("could not initiate service reload test");
2575 if (!do_service_reload_test (&directory, FALSE))
2581 if (!init_service_reload_test (&directory))
2582 _dbus_assert_not_reached ("could not initiate service reload test");
2584 if (!do_service_reload_test (&directory, TRUE))
2589 /* Cleanup test directory */
2590 if (!cleanup_service_reload_test (&directory))
2593 _dbus_string_free (&directory);
2598 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */