1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* activation.c Activation of services
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "activation.h"
28 #include "activation-exit-codes.h"
29 #include "desktop-file.h"
34 #include <dbus/dbus-internals.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-list.h>
37 #include <dbus/dbus-shell.h>
38 #include <dbus/dbus-spawn.h>
39 #include <dbus/dbus-timeout.h>
40 #include <dbus/dbus-sysdeps.h>
48 DBusHashTable *entries;
49 DBusHashTable *pending_activations;
52 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
53 * i.e. number of pending activation requests, not pending
56 DBusHashTable *directories;
57 DBusHashTable *environment;
64 DBusHashTable *entries;
65 } BusServiceDirectory;
73 char *systemd_service;
75 BusServiceDirectory *s_dir;
79 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
81 struct BusPendingActivationEntry
83 DBusMessage *activation_message;
84 DBusConnection *connection;
86 dbus_bool_t auto_activation;
92 BusActivation *activation;
95 char *systemd_service;
98 DBusBabysitter *babysitter;
100 unsigned int timeout_added : 1;
101 } BusPendingActivation;
104 static BusServiceDirectory *
105 bus_service_directory_ref (BusServiceDirectory *dir)
107 _dbus_assert (dir->refcount);
116 bus_service_directory_unref (BusServiceDirectory *dir)
121 _dbus_assert (dir->refcount > 0);
124 if (dir->refcount > 0)
128 _dbus_hash_table_unref (dir->entries);
130 dbus_free (dir->dir_c);
135 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
137 if (entry->activation_message)
138 dbus_message_unref (entry->activation_message);
140 if (entry->connection)
141 dbus_connection_unref (entry->connection);
147 handle_timeout_callback (DBusTimeout *timeout,
150 BusPendingActivation *pending_activation = data;
152 while (!dbus_timeout_handle (pending_activation->timeout))
153 _dbus_wait_for_memory ();
156 static BusPendingActivation *
157 bus_pending_activation_ref (BusPendingActivation *pending_activation)
159 _dbus_assert (pending_activation->refcount > 0);
160 pending_activation->refcount += 1;
162 return pending_activation;
166 bus_pending_activation_unref (BusPendingActivation *pending_activation)
170 if (pending_activation == NULL) /* hash table requires this */
173 _dbus_assert (pending_activation->refcount > 0);
174 pending_activation->refcount -= 1;
176 if (pending_activation->refcount > 0)
179 if (pending_activation->timeout_added)
181 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
182 pending_activation->timeout,
183 handle_timeout_callback, pending_activation);
184 pending_activation->timeout_added = FALSE;
187 if (pending_activation->timeout)
188 _dbus_timeout_unref (pending_activation->timeout);
190 if (pending_activation->babysitter)
192 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
194 pending_activation->babysitter,
196 _dbus_assert_not_reached ("setting watch functions to NULL failed");
198 _dbus_babysitter_unref (pending_activation->babysitter);
201 dbus_free (pending_activation->service_name);
202 dbus_free (pending_activation->exec);
203 dbus_free (pending_activation->systemd_service);
205 link = _dbus_list_get_first_link (&pending_activation->entries);
209 BusPendingActivationEntry *entry = link->data;
211 bus_pending_activation_entry_free (entry);
213 link = _dbus_list_get_next_link (&pending_activation->entries, link);
215 _dbus_list_clear (&pending_activation->entries);
217 pending_activation->activation->n_pending_activations -=
218 pending_activation->n_entries;
220 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
222 dbus_free (pending_activation);
225 static BusActivationEntry *
226 bus_activation_entry_ref (BusActivationEntry *entry)
228 _dbus_assert (entry->refcount > 0);
235 bus_activation_entry_unref (BusActivationEntry *entry)
237 if (entry == NULL) /* hash table requires this */
240 _dbus_assert (entry->refcount > 0);
243 if (entry->refcount > 0)
246 dbus_free (entry->name);
247 dbus_free (entry->exec);
248 dbus_free (entry->user);
249 dbus_free (entry->filename);
250 dbus_free (entry->systemd_service);
256 update_desktop_file_entry (BusActivation *activation,
257 BusServiceDirectory *s_dir,
258 DBusString *filename,
259 BusDesktopFile *desktop_file,
262 char *name, *exec, *user, *exec_tmp, *systemd_service;
263 BusActivationEntry *entry;
265 DBusString file_path;
268 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
275 systemd_service = NULL;
277 dbus_error_init (&tmp_error);
279 if (!_dbus_string_init (&file_path))
285 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
286 !_dbus_concat_dir_and_file (&file_path, filename))
292 if (!_dbus_stat (&file_path, &stat_buf, NULL))
294 dbus_set_error (error, DBUS_ERROR_FAILED,
295 "Can't stat the service file\n");
299 if (!bus_desktop_file_get_string (desktop_file,
300 DBUS_SERVICE_SECTION,
306 if (!bus_desktop_file_get_string (desktop_file,
307 DBUS_SERVICE_SECTION,
313 /* user is not _required_ unless we are using system activation */
314 if (!bus_desktop_file_get_string (desktop_file,
315 DBUS_SERVICE_SECTION,
319 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
320 /* if we got OOM, then exit */
321 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
323 dbus_move_error (&tmp_error, error);
328 /* if we have error because we didn't find anything then continue */
329 dbus_error_free (&tmp_error);
334 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
336 /* systemd service is never required */
337 if (!bus_desktop_file_get_string (desktop_file,
338 DBUS_SERVICE_SECTION,
339 DBUS_SERVICE_SYSTEMD_SERVICE,
340 &systemd_service, &tmp_error))
342 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
343 /* if we got OOM, then exit */
344 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
346 dbus_move_error (&tmp_error, error);
351 /* if we have error because we didn't find anything then continue */
352 dbus_error_free (&tmp_error);
353 dbus_free (systemd_service);
354 systemd_service = NULL;
358 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
360 entry = _dbus_hash_table_lookup_string (s_dir->entries,
361 _dbus_string_get_const_data (filename));
363 exec = strdup (_dbus_replace_install_prefix (exec_tmp));
365 if (entry == NULL) /* New file */
367 /* FIXME we need a better-defined algorithm for which service file to
368 * pick than "whichever one is first in the directory listing"
370 if (_dbus_hash_table_lookup_string (activation->entries, name))
372 dbus_set_error (error, DBUS_ERROR_FAILED,
373 "Service %s already exists in activation entry list\n", name);
377 entry = dbus_new0 (BusActivationEntry, 1);
387 entry->systemd_service = systemd_service;
390 entry->s_dir = s_dir;
391 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
392 if (!entry->filename)
398 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
404 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
406 /* Revert the insertion in the entries table */
407 _dbus_hash_table_remove_string (activation->entries, entry->name);
412 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
414 else /* Just update the entry */
416 bus_activation_entry_ref (entry);
417 _dbus_hash_table_remove_string (activation->entries, entry->name);
419 if (_dbus_hash_table_lookup_string (activation->entries, name))
421 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
422 name, _dbus_string_get_const_data (&file_path));
426 dbus_free (entry->name);
427 dbus_free (entry->exec);
428 dbus_free (entry->user);
429 dbus_free (entry->systemd_service);
430 entry->systemd_service = systemd_service;
434 if (!_dbus_hash_table_insert_string (activation->entries,
435 entry->name, bus_activation_entry_ref(entry)))
438 /* Also remove path to entries hash since we want this in sync with
439 * the entries hash table */
440 _dbus_hash_table_remove_string (entry->s_dir->entries,
442 bus_activation_entry_unref (entry);
447 entry->mtime = stat_buf.mtime;
449 _dbus_string_free (&file_path);
450 bus_activation_entry_unref (entry);
456 dbus_free (exec_tmp);
458 dbus_free (systemd_service);
459 _dbus_string_free (&file_path);
462 bus_activation_entry_unref (entry);
468 check_service_file (BusActivation *activation,
469 BusActivationEntry *entry,
470 BusActivationEntry **updated_entry,
475 BusActivationEntry *tmp_entry;
476 DBusString file_path;
482 _dbus_string_init_const (&filename, entry->filename);
484 if (!_dbus_string_init (&file_path))
490 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
491 !_dbus_concat_dir_and_file (&file_path, &filename))
498 if (!_dbus_stat (&file_path, &stat_buf, NULL))
500 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
501 _dbus_string_get_const_data (&file_path));
503 _dbus_hash_table_remove_string (activation->entries, entry->name);
504 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
512 if (stat_buf.mtime > entry->mtime)
514 BusDesktopFile *desktop_file;
517 dbus_error_init (&tmp_error);
519 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
520 if (desktop_file == NULL)
522 _dbus_verbose ("Could not load %s: %s\n",
523 _dbus_string_get_const_data (&file_path),
525 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
527 dbus_move_error (&tmp_error, error);
531 dbus_error_free (&tmp_error);
536 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
537 * Handle these both better
539 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
541 bus_desktop_file_free (desktop_file);
542 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
544 dbus_move_error (&tmp_error, error);
548 dbus_error_free (&tmp_error);
553 bus_desktop_file_free (desktop_file);
559 _dbus_string_free (&file_path);
561 if (updated_entry != NULL)
562 *updated_entry = tmp_entry;
567 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
568 * hash entries it already added.
571 update_directory (BusActivation *activation,
572 BusServiceDirectory *s_dir,
576 DBusString dir, filename;
577 BusDesktopFile *desktop_file;
580 BusActivationEntry *entry;
581 DBusString full_path;
583 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
588 _dbus_string_init_const (&dir, s_dir->dir_c);
590 if (!_dbus_string_init (&filename))
596 if (!_dbus_string_init (&full_path))
599 _dbus_string_free (&filename);
605 /* from this point it's safe to "goto out" */
607 iter = _dbus_directory_open (&dir, error);
610 _dbus_verbose ("Failed to open directory %s: %s\n",
612 error ? error->message : "unknown");
616 /* Now read the files */
617 dbus_error_init (&tmp_error);
618 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
620 _dbus_assert (!dbus_error_is_set (&tmp_error));
622 _dbus_string_set_length (&full_path, 0);
624 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
626 _dbus_verbose ("Skipping non-.service file %s\n",
627 _dbus_string_get_const_data (&filename));
631 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
632 if (entry) /* Already has this service file in the cache */
634 if (!check_service_file (activation, entry, NULL, error))
640 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
641 !_dbus_concat_dir_and_file (&full_path, &filename))
648 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
649 if (desktop_file == NULL)
651 _dbus_verbose ("Could not load %s: %s\n",
652 _dbus_string_get_const_data (&full_path),
655 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
657 dbus_move_error (&tmp_error, error);
661 dbus_error_free (&tmp_error);
665 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
666 * Handle these both better
668 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
670 bus_desktop_file_free (desktop_file);
673 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
674 _dbus_string_get_const_data (&full_path), tmp_error.message);
676 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
678 dbus_move_error (&tmp_error, error);
682 dbus_error_free (&tmp_error);
687 bus_desktop_file_free (desktop_file);
693 if (dbus_error_is_set (&tmp_error))
695 dbus_move_error (&tmp_error, error);
703 _DBUS_ASSERT_ERROR_IS_SET (error);
705 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
708 _dbus_directory_close (iter);
709 _dbus_string_free (&filename);
710 _dbus_string_free (&full_path);
716 populate_environment (BusActivation *activation)
722 dbus_bool_t retval = FALSE;
724 environment = _dbus_get_environment ();
726 if (environment == NULL)
729 if (!_dbus_string_init (&key))
731 dbus_free_string_array (environment);
735 if (!_dbus_string_init (&value))
737 _dbus_string_free (&key);
738 dbus_free_string_array (environment);
742 for (i = 0; environment[i] != NULL; i++)
744 if (!_dbus_string_append (&key, environment[i]))
747 if (_dbus_string_split_on_byte (&key, '=', &value))
749 char *hash_key, *hash_value;
751 if (!_dbus_string_steal_data (&key, &hash_key))
754 if (!_dbus_string_steal_data (&value, &hash_value))
757 if (!_dbus_hash_table_insert_string (activation->environment,
758 hash_key, hash_value))
761 _dbus_string_set_length (&key, 0);
762 _dbus_string_set_length (&value, 0);
765 if (environment[i] != NULL)
771 _dbus_string_free (&key);
772 _dbus_string_free (&value);
773 dbus_free_string_array (environment);
779 bus_activation_reload (BusActivation *activation,
780 const DBusString *address,
781 DBusList **directories,
787 if (activation->server_address != NULL)
788 dbus_free (activation->server_address);
789 if (!_dbus_string_copy_data (address, &activation->server_address))
795 if (activation->entries != NULL)
796 _dbus_hash_table_unref (activation->entries);
797 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
798 (DBusFreeFunction)bus_activation_entry_unref);
799 if (activation->entries == NULL)
805 if (activation->directories != NULL)
806 _dbus_hash_table_unref (activation->directories);
807 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
808 (DBusFreeFunction)bus_service_directory_unref);
810 if (activation->directories == NULL)
816 link = _dbus_list_get_first_link (directories);
819 BusServiceDirectory *s_dir;
821 dir = _dbus_strdup ((const char *) link->data);
828 s_dir = dbus_new0 (BusServiceDirectory, 1);
839 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
840 (DBusFreeFunction)bus_activation_entry_unref);
844 bus_service_directory_unref (s_dir);
849 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
851 bus_service_directory_unref (s_dir);
856 /* only fail on OOM, it is ok if we can't read the directory */
857 if (!update_directory (activation, s_dir, error))
859 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
862 dbus_error_free (error);
865 link = _dbus_list_get_next_link (directories, link);
874 bus_activation_new (BusContext *context,
875 const DBusString *address,
876 DBusList **directories,
879 BusActivation *activation;
883 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
885 activation = dbus_new0 (BusActivation, 1);
886 if (activation == NULL)
892 activation->refcount = 1;
893 activation->context = context;
894 activation->n_pending_activations = 0;
896 if (!bus_activation_reload (activation, address, directories, error))
899 /* Initialize this hash table once, we don't want to lose pending
900 * activations on reload. */
901 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
902 (DBusFreeFunction)bus_pending_activation_unref);
904 if (activation->pending_activations == NULL)
910 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
911 (DBusFreeFunction) dbus_free,
912 (DBusFreeFunction) dbus_free);
914 if (activation->environment == NULL)
920 if (!populate_environment (activation))
929 bus_activation_unref (activation);
934 bus_activation_ref (BusActivation *activation)
936 _dbus_assert (activation->refcount > 0);
938 activation->refcount += 1;
944 bus_activation_unref (BusActivation *activation)
946 _dbus_assert (activation->refcount > 0);
948 activation->refcount -= 1;
950 if (activation->refcount > 0)
953 dbus_free (activation->server_address);
954 if (activation->entries)
955 _dbus_hash_table_unref (activation->entries);
956 if (activation->pending_activations)
957 _dbus_hash_table_unref (activation->pending_activations);
958 if (activation->directories)
959 _dbus_hash_table_unref (activation->directories);
960 if (activation->environment)
961 _dbus_hash_table_unref (activation->environment);
963 dbus_free (activation);
967 add_bus_environment (BusActivation *activation,
972 if (!bus_activation_set_environment_variable (activation,
973 "DBUS_STARTER_ADDRESS",
974 activation->server_address,
978 type = bus_context_get_type (activation->context);
981 if (!bus_activation_set_environment_variable (activation,
982 "DBUS_STARTER_BUS_TYPE", type,
986 if (strcmp (type, "session") == 0)
988 if (!bus_activation_set_environment_variable (activation,
989 "DBUS_SESSION_BUS_ADDRESS",
990 activation->server_address,
994 else if (strcmp (type, "system") == 0)
996 if (!bus_activation_set_environment_variable (activation,
997 "DBUS_SYSTEM_BUS_ADDRESS",
998 activation->server_address,
1009 BusPendingActivation *pending_activation;
1010 DBusPreallocatedHash *hash_entry;
1011 } RestorePendingData;
1014 restore_pending (void *data)
1016 RestorePendingData *d = data;
1018 _dbus_assert (d->pending_activation != NULL);
1019 _dbus_assert (d->hash_entry != NULL);
1021 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1022 d->pending_activation->service_name,
1023 d->pending_activation->timeout_added);
1025 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1027 d->pending_activation->service_name, d->pending_activation);
1029 bus_pending_activation_ref (d->pending_activation);
1031 d->hash_entry = NULL;
1035 free_pending_restore_data (void *data)
1037 RestorePendingData *d = data;
1040 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1043 bus_pending_activation_unref (d->pending_activation);
1049 add_restore_pending_to_transaction (BusTransaction *transaction,
1050 BusPendingActivation *pending_activation)
1052 RestorePendingData *d;
1054 d = dbus_new (RestorePendingData, 1);
1058 d->pending_activation = pending_activation;
1059 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1061 bus_pending_activation_ref (d->pending_activation);
1063 if (d->hash_entry == NULL ||
1064 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1065 free_pending_restore_data))
1067 free_pending_restore_data (d);
1071 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1077 bus_activation_service_created (BusActivation *activation,
1078 const char *service_name,
1079 BusTransaction *transaction,
1082 BusPendingActivation *pending_activation;
1083 DBusMessage *message;
1086 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1088 /* Check if it's a pending activation */
1089 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1091 if (!pending_activation)
1094 link = _dbus_list_get_first_link (&pending_activation->entries);
1095 while (link != NULL)
1097 BusPendingActivationEntry *entry = link->data;
1098 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1100 if (dbus_connection_get_is_connected (entry->connection))
1102 /* Only send activation replies to regular activation requests. */
1103 if (!entry->auto_activation)
1105 dbus_uint32_t result;
1107 message = dbus_message_new_method_return (entry->activation_message);
1110 BUS_SET_OOM (error);
1114 result = DBUS_START_REPLY_SUCCESS;
1116 if (!dbus_message_append_args (message,
1117 DBUS_TYPE_UINT32, &result,
1120 dbus_message_unref (message);
1121 BUS_SET_OOM (error);
1125 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1127 dbus_message_unref (message);
1128 BUS_SET_OOM (error);
1132 dbus_message_unref (message);
1146 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1147 BusService *service,
1148 BusTransaction *transaction,
1151 BusPendingActivation *pending_activation;
1154 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1156 /* Check if it's a pending activation */
1157 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1158 bus_service_get_name (service));
1160 if (!pending_activation)
1163 link = _dbus_list_get_first_link (&pending_activation->entries);
1164 while (link != NULL)
1166 BusPendingActivationEntry *entry = link->data;
1167 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1169 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1171 DBusConnection *addressed_recipient;
1173 addressed_recipient = bus_service_get_primary_owners_connection (service);
1175 /* Resume dispatching where we left off in bus_dispatch() */
1176 if (!bus_dispatch_matches (transaction,
1178 addressed_recipient,
1179 entry->activation_message, error))
1186 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1188 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1189 BUS_SET_OOM (error);
1193 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1202 * FIXME @todo the error messages here would ideally be preallocated
1203 * so we don't need to allocate memory to send them.
1204 * Using the usual tactic, prealloc an OOM message, then
1205 * if we can't alloc the real error send the OOM error instead.
1208 try_send_activation_failure (BusPendingActivation *pending_activation,
1209 const DBusError *how)
1211 BusActivation *activation;
1213 BusTransaction *transaction;
1215 activation = pending_activation->activation;
1217 transaction = bus_transaction_new (activation->context);
1218 if (transaction == NULL)
1221 link = _dbus_list_get_first_link (&pending_activation->entries);
1222 while (link != NULL)
1224 BusPendingActivationEntry *entry = link->data;
1225 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1227 if (dbus_connection_get_is_connected (entry->connection))
1229 if (!bus_transaction_send_error_reply (transaction,
1232 entry->activation_message))
1239 bus_transaction_execute_and_free (transaction);
1245 bus_transaction_cancel_and_free (transaction);
1250 * Free the pending activation and send an error message to all the
1251 * connections that were waiting for it.
1254 pending_activation_failed (BusPendingActivation *pending_activation,
1255 const DBusError *how)
1257 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1258 while (!try_send_activation_failure (pending_activation, how))
1259 _dbus_wait_for_memory ();
1261 /* Destroy this pending activation */
1262 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1263 pending_activation->service_name);
1267 * Depending on the exit code of the helper, set the error accordingly
1270 handle_servicehelper_exit_error (int exit_code,
1275 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1276 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1277 "Launcher could not run (out of memory)");
1279 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1280 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1281 "Failed to setup environment correctly");
1283 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1284 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1285 "Bus name is not valid or missing");
1287 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1288 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1289 "Bus name not found in system service directory");
1291 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1292 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1293 "The permission of the setuid helper is not correct");
1295 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1296 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1297 "The service file is incorrect or does not have all required attributes");
1299 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1300 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1301 "Cannot launch daemon, file not found or permissions invalid");
1303 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1304 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1305 "Invalid arguments to command line");
1307 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1308 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1309 "Launched child was signaled, it probably crashed");
1312 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1313 "Launch helper exited with unknown return code %i", exit_code);
1319 babysitter_watch_callback (DBusWatch *watch,
1320 unsigned int condition,
1323 BusPendingActivation *pending_activation = data;
1325 DBusBabysitter *babysitter;
1326 dbus_bool_t uses_servicehelper;
1328 babysitter = pending_activation->babysitter;
1330 _dbus_babysitter_ref (babysitter);
1332 retval = dbus_watch_handle (watch, condition);
1334 /* There are two major cases here; are we the system bus or the session? Here this
1335 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1336 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1338 * In both cases though, just ignore when a process exits with status 0; it's possible for
1339 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1340 * condition between this code and the child process claiming the bus name.
1342 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1344 /* FIXME this is broken in the same way that
1345 * connection watches used to be; there should be
1346 * a separate callback for status change, instead
1347 * of doing "if we handled a watch status might
1350 * Fixing this lets us move dbus_watch_handle
1351 * calls into dbus-mainloop.c
1353 if (_dbus_babysitter_get_child_exited (babysitter))
1357 dbus_bool_t activation_failed;
1360 dbus_error_init (&error);
1362 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1364 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1366 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1367 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1369 activation_failed = exit_code != 0;
1371 dbus_error_free(&error);
1373 if (activation_failed)
1375 if (uses_servicehelper)
1376 handle_servicehelper_exit_error (exit_code, &error);
1378 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1383 activation_failed = TRUE;
1386 if (activation_failed)
1388 /* Destroy all pending activations with the same exec */
1389 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1391 while (_dbus_hash_iter_next (&iter))
1393 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1395 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1396 pending_activation_failed (p, &error);
1399 /* Destroys the pending activation */
1400 pending_activation_failed (pending_activation, &error);
1402 dbus_error_free (&error);
1406 _dbus_babysitter_unref (babysitter);
1412 add_babysitter_watch (DBusWatch *watch,
1415 BusPendingActivation *pending_activation = data;
1417 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1418 watch, babysitter_watch_callback, pending_activation,
1423 remove_babysitter_watch (DBusWatch *watch,
1426 BusPendingActivation *pending_activation = data;
1428 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1429 watch, babysitter_watch_callback, pending_activation);
1433 pending_activation_timed_out (void *data)
1435 BusPendingActivation *pending_activation = data;
1438 /* Kill the spawned process, since it sucks
1439 * (not sure this is what we want to do, but
1440 * may as well try it for now)
1442 if (pending_activation->babysitter)
1443 _dbus_babysitter_kill_child (pending_activation->babysitter);
1445 dbus_error_init (&error);
1447 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1448 "Activation of %s timed out",
1449 pending_activation->service_name);
1451 pending_activation_failed (pending_activation, &error);
1453 dbus_error_free (&error);
1459 cancel_pending (void *data)
1461 BusPendingActivation *pending_activation = data;
1463 _dbus_verbose ("Canceling pending activation of %s\n",
1464 pending_activation->service_name);
1466 if (pending_activation->babysitter)
1467 _dbus_babysitter_kill_child (pending_activation->babysitter);
1469 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1470 pending_activation->service_name);
1474 free_pending_cancel_data (void *data)
1476 BusPendingActivation *pending_activation = data;
1478 bus_pending_activation_unref (pending_activation);
1482 add_cancel_pending_to_transaction (BusTransaction *transaction,
1483 BusPendingActivation *pending_activation)
1485 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1487 free_pending_cancel_data))
1490 bus_pending_activation_ref (pending_activation);
1492 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1498 update_service_cache (BusActivation *activation, DBusError *error)
1502 _dbus_hash_iter_init (activation->directories, &iter);
1503 while (_dbus_hash_iter_next (&iter))
1505 DBusError tmp_error;
1506 BusServiceDirectory *s_dir;
1508 s_dir = _dbus_hash_iter_get_value (&iter);
1510 dbus_error_init (&tmp_error);
1511 if (!update_directory (activation, s_dir, &tmp_error))
1513 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1515 dbus_move_error (&tmp_error, error);
1519 dbus_error_free (&tmp_error);
1527 static BusActivationEntry *
1528 activation_find_entry (BusActivation *activation,
1529 const char *service_name,
1532 BusActivationEntry *entry;
1534 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1537 if (!update_service_cache (activation, error))
1540 entry = _dbus_hash_table_lookup_string (activation->entries,
1545 BusActivationEntry *updated_entry;
1547 if (!check_service_file (activation, entry, &updated_entry, error))
1550 entry = updated_entry;
1555 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1556 "The name %s was not provided by any .service files",
1565 bus_activation_get_environment (BusActivation *activation)
1572 length = _dbus_hash_table_get_n_entries (activation->environment);
1574 environment = dbus_new0 (char *, length + 1);
1576 if (environment == NULL)
1580 _dbus_hash_iter_init (activation->environment, &iter);
1582 if (!_dbus_string_init (&entry))
1584 dbus_free_string_array (environment);
1588 while (_dbus_hash_iter_next (&iter))
1590 const char *key, *value;
1592 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1593 value = (const char *) _dbus_hash_iter_get_value (&iter);
1595 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1598 if (!_dbus_string_steal_data (&entry, environment + i))
1603 _dbus_string_free (&entry);
1607 dbus_free_string_array (environment);
1615 bus_activation_set_environment_variable (BusActivation *activation,
1627 hash_key = _dbus_strdup (key);
1629 if (hash_key == NULL)
1632 hash_value = _dbus_strdup (value);
1634 if (hash_value == NULL)
1637 if (!_dbus_hash_table_insert_string (activation->environment,
1638 hash_key, hash_value))
1643 if (retval == FALSE)
1645 dbus_free (hash_key);
1646 dbus_free (hash_value);
1647 BUS_SET_OOM (error);
1654 bus_activation_activate_service (BusActivation *activation,
1655 DBusConnection *connection,
1656 BusTransaction *transaction,
1657 dbus_bool_t auto_activation,
1658 DBusMessage *activation_message,
1659 const char *service_name,
1662 BusActivationEntry *entry;
1663 BusPendingActivation *pending_activation;
1664 BusPendingActivationEntry *pending_activation_entry;
1665 DBusMessage *message;
1666 DBusString service_str;
1667 const char *servicehelper;
1673 dbus_bool_t activated;
1678 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1680 if (activation->n_pending_activations >=
1681 bus_context_get_max_pending_activations (activation->context))
1683 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1684 "The maximum number of pending activations has been reached, activation of %s failed",
1689 entry = activation_find_entry (activation, service_name, error);
1693 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1694 * call us if the service is already active.
1696 if (!auto_activation)
1698 /* Check if the service is active */
1699 _dbus_string_init_const (&service_str, service_name);
1700 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1702 dbus_uint32_t result;
1704 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1706 message = dbus_message_new_method_return (activation_message);
1710 _dbus_verbose ("No memory to create reply to activate message\n");
1711 BUS_SET_OOM (error);
1715 result = DBUS_START_REPLY_ALREADY_RUNNING;
1717 if (!dbus_message_append_args (message,
1718 DBUS_TYPE_UINT32, &result,
1721 _dbus_verbose ("No memory to set args of reply to activate message\n");
1722 BUS_SET_OOM (error);
1723 dbus_message_unref (message);
1727 retval = bus_transaction_send_from_driver (transaction, connection, message);
1728 dbus_message_unref (message);
1731 _dbus_verbose ("Failed to send reply\n");
1732 BUS_SET_OOM (error);
1739 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1740 if (!pending_activation_entry)
1742 _dbus_verbose ("Failed to create pending activation entry\n");
1743 BUS_SET_OOM (error);
1747 pending_activation_entry->auto_activation = auto_activation;
1749 pending_activation_entry->activation_message = activation_message;
1750 dbus_message_ref (activation_message);
1751 pending_activation_entry->connection = connection;
1752 dbus_connection_ref (connection);
1754 /* Check if the service is being activated */
1755 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1756 if (pending_activation)
1758 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1760 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1762 BUS_SET_OOM (error);
1763 bus_pending_activation_entry_free (pending_activation_entry);
1767 pending_activation->n_entries += 1;
1768 pending_activation->activation->n_pending_activations += 1;
1772 pending_activation = dbus_new0 (BusPendingActivation, 1);
1773 if (!pending_activation)
1775 _dbus_verbose ("Failed to create pending activation\n");
1777 BUS_SET_OOM (error);
1778 bus_pending_activation_entry_free (pending_activation_entry);
1782 pending_activation->activation = activation;
1783 pending_activation->refcount = 1;
1785 pending_activation->service_name = _dbus_strdup (service_name);
1786 if (!pending_activation->service_name)
1788 _dbus_verbose ("Failed to copy service name for pending activation\n");
1790 BUS_SET_OOM (error);
1791 bus_pending_activation_unref (pending_activation);
1792 bus_pending_activation_entry_free (pending_activation_entry);
1796 pending_activation->exec = _dbus_strdup (entry->exec);
1797 if (!pending_activation->exec)
1799 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1800 BUS_SET_OOM (error);
1801 bus_pending_activation_unref (pending_activation);
1802 bus_pending_activation_entry_free (pending_activation_entry);
1806 if (entry->systemd_service)
1808 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1809 if (!pending_activation->systemd_service)
1811 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1812 BUS_SET_OOM (error);
1813 bus_pending_activation_unref (pending_activation);
1814 bus_pending_activation_entry_free (pending_activation_entry);
1819 pending_activation->timeout =
1820 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1821 pending_activation_timed_out,
1824 if (!pending_activation->timeout)
1826 _dbus_verbose ("Failed to create timeout for pending activation\n");
1828 BUS_SET_OOM (error);
1829 bus_pending_activation_unref (pending_activation);
1830 bus_pending_activation_entry_free (pending_activation_entry);
1834 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1835 pending_activation->timeout,
1836 handle_timeout_callback,
1840 _dbus_verbose ("Failed to add timeout 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);
1848 pending_activation->timeout_added = TRUE;
1850 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1852 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1854 BUS_SET_OOM (error);
1855 bus_pending_activation_unref (pending_activation);
1856 bus_pending_activation_entry_free (pending_activation_entry);
1860 pending_activation->n_entries += 1;
1861 pending_activation->activation->n_pending_activations += 1;
1864 _dbus_hash_iter_init (activation->pending_activations, &iter);
1865 while (_dbus_hash_iter_next (&iter))
1867 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1869 if (strcmp (p->exec, entry->exec) == 0)
1876 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1877 pending_activation->service_name,
1878 pending_activation))
1880 _dbus_verbose ("Failed to put pending activation in hash table\n");
1882 BUS_SET_OOM (error);
1883 bus_pending_activation_unref (pending_activation);
1888 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1890 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1891 BUS_SET_OOM (error);
1892 _dbus_hash_table_remove_string (activation->pending_activations,
1893 pending_activation->service_name);
1901 if (bus_context_get_systemd_activation (activation->context))
1903 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1904 /* systemd itself is missing apparently. That can happen
1905 only during early startup. Let's just wait until systemd
1906 connects to us and do nothing. */
1909 if (entry->systemd_service)
1911 BusTransaction *activation_transaction;
1912 DBusString service_string;
1913 BusService *service;
1914 BusRegistry *registry;
1916 /* OK, we have a systemd service configured for this entry,
1917 hence let's enqueue an activation request message. This
1918 is implemented as a directed signal, not a method call,
1919 for three reasons: 1) we don't expect a response on
1920 success, where we just expect a name appearing on the
1921 bus; 2) at this time the systemd service might not yet
1922 have connected, so we wouldn't know the message serial at
1923 this point to set up a pending call; 3) it is ugly if the
1924 bus suddenly becomes the caller of a remote method. */
1926 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1927 "org.freedesktop.systemd1.Activator",
1928 "ActivationRequest");
1931 _dbus_verbose ("No memory to create activation message\n");
1932 BUS_SET_OOM (error);
1936 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1937 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1938 !dbus_message_append_args (message,
1939 DBUS_TYPE_STRING, &entry->systemd_service,
1942 _dbus_verbose ("No memory to set args of activation message\n");
1943 dbus_message_unref (message);
1944 BUS_SET_OOM (error);
1948 /* Create our transaction */
1949 activation_transaction = bus_transaction_new (activation->context);
1950 if (activation_transaction == NULL)
1952 _dbus_verbose ("No memory to create activation transaction\n");
1953 dbus_message_unref (message);
1954 BUS_SET_OOM (error);
1958 /* Check whether systemd is already connected */
1959 registry = bus_connection_get_registry (connection);
1960 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1961 service = bus_registry_lookup (registry, &service_string);
1963 if (service != NULL)
1964 /* Wonderful, systemd is connected, let's just send the msg */
1965 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1968 /* systemd is not around, let's "activate" it. */
1969 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1970 message, "org.freedesktop.systemd1", error);
1972 dbus_message_unref (message);
1976 _DBUS_ASSERT_ERROR_IS_SET (error);
1977 _dbus_verbose ("failed to send activation message: %s\n", error->name);
1978 bus_transaction_cancel_and_free (activation_transaction);
1982 bus_transaction_execute_and_free (activation_transaction);
1986 /* OK, we have no configured systemd service, hence let's
1987 proceed with traditional activation. */
1990 /* use command as system and session different */
1991 if (!_dbus_string_init (&command))
1993 BUS_SET_OOM (error);
1997 /* does the bus use a helper? */
1998 servicehelper = bus_context_get_servicehelper (activation->context);
1999 if (servicehelper != NULL)
2001 if (entry->user == NULL)
2003 _dbus_string_free (&command);
2004 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2005 "Cannot do system-bus activation with no user\n");
2009 /* join the helper path and the service name */
2010 if (!_dbus_string_append (&command, servicehelper))
2012 _dbus_string_free (&command);
2013 BUS_SET_OOM (error);
2016 if (!_dbus_string_append (&command, " "))
2018 _dbus_string_free (&command);
2019 BUS_SET_OOM (error);
2022 if (!_dbus_string_append (&command, service_name))
2024 _dbus_string_free (&command);
2025 BUS_SET_OOM (error);
2031 /* the bus does not use a helper, so we can append arguments with the exec line */
2032 if (!_dbus_string_append (&command, entry->exec))
2034 _dbus_string_free (&command);
2035 BUS_SET_OOM (error);
2040 /* convert command into arguments */
2041 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2043 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2044 _DBUS_ASSERT_ERROR_IS_SET (error);
2046 _dbus_hash_table_remove_string (activation->pending_activations,
2047 pending_activation->service_name);
2049 _dbus_string_free (&command);
2052 _dbus_string_free (&command);
2054 if (!add_bus_environment (activation, error))
2056 _DBUS_ASSERT_ERROR_IS_SET (error);
2057 dbus_free_string_array (argv);
2061 envp = bus_activation_get_environment (activation);
2065 BUS_SET_OOM (error);
2066 dbus_free_string_array (argv);
2070 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2071 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2076 _dbus_verbose ("Failed to spawn child\n");
2077 _DBUS_ASSERT_ERROR_IS_SET (error);
2078 dbus_free_string_array (argv);
2079 dbus_free_string_array (envp);
2084 dbus_free_string_array (argv);
2087 _dbus_assert (pending_activation->babysitter != NULL);
2089 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2090 add_babysitter_watch,
2091 remove_babysitter_watch,
2096 BUS_SET_OOM (error);
2097 _dbus_verbose ("Failed to set babysitter watch functions\n");
2105 bus_activation_list_services (BusActivation *activation,
2113 len = _dbus_hash_table_get_n_entries (activation->entries);
2114 retval = dbus_new (char *, len + 1);
2119 _dbus_hash_iter_init (activation->entries, &iter);
2121 while (_dbus_hash_iter_next (&iter))
2123 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2125 retval[i] = _dbus_strdup (entry->name);
2126 if (retval[i] == NULL)
2141 for (j = 0; j < i; j++)
2142 dbus_free (retval[i]);
2149 dbus_activation_systemd_failure (BusActivation *activation,
2150 DBusMessage *message)
2153 const char *code, *str, *unit = NULL;
2155 dbus_error_init(&error);
2157 /* This is called whenever the systemd activator sent us a
2158 response. We'll invalidate all pending activations that match the
2161 if (dbus_message_get_args (message, &error,
2162 DBUS_TYPE_STRING, &unit,
2163 DBUS_TYPE_STRING, &code,
2164 DBUS_TYPE_STRING, &str,
2166 dbus_set_error(&error, code, str);
2172 _dbus_hash_iter_init (activation->pending_activations,
2175 while (_dbus_hash_iter_next (&iter))
2177 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2179 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2180 pending_activation_failed(p, &error);
2184 dbus_error_free(&error);
2189 #ifdef DBUS_BUILD_TESTS
2193 #define SERVICE_NAME_1 "MyService1"
2194 #define SERVICE_NAME_2 "MyService2"
2195 #define SERVICE_NAME_3 "MyService3"
2197 #define SERVICE_FILE_1 "service-1.service"
2198 #define SERVICE_FILE_2 "service-2.service"
2199 #define SERVICE_FILE_3 "service-3.service"
2202 test_create_service_file (DBusString *dir,
2203 const char *filename,
2207 DBusString file_name, full_path;
2209 dbus_bool_t ret_val;
2212 _dbus_string_init_const (&file_name, filename);
2214 if (!_dbus_string_init (&full_path))
2217 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2218 !_dbus_concat_dir_and_file (&full_path, &file_name))
2224 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2231 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2235 _dbus_string_free (&full_path);
2240 test_remove_service_file (DBusString *dir, const char *filename)
2242 DBusString file_name, full_path;
2243 dbus_bool_t ret_val;
2247 _dbus_string_init_const (&file_name, filename);
2249 if (!_dbus_string_init (&full_path))
2252 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2253 !_dbus_concat_dir_and_file (&full_path, &file_name))
2259 if (!_dbus_delete_file (&full_path, NULL))
2266 _dbus_string_free (&full_path);
2271 test_remove_directory (DBusString *dir)
2274 DBusString filename, full_path;
2275 dbus_bool_t ret_val;
2279 if (!_dbus_string_init (&filename))
2282 if (!_dbus_string_init (&full_path))
2284 _dbus_string_free (&filename);
2288 iter = _dbus_directory_open (dir, NULL);
2295 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2297 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2303 _dbus_directory_close (iter);
2305 if (!_dbus_delete_directory (dir, NULL))
2312 _dbus_string_free (&filename);
2313 _dbus_string_free (&full_path);
2319 init_service_reload_test (DBusString *dir)
2323 if (!_dbus_stat (dir, &stat_buf, NULL))
2325 if (!_dbus_create_directory (dir, NULL))
2330 if (!test_remove_directory (dir))
2333 if (!_dbus_create_directory (dir, NULL))
2337 /* Create one initial file */
2338 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2345 cleanup_service_reload_test (DBusString *dir)
2347 if (!test_remove_directory (dir))
2355 BusActivation *activation;
2356 const char *service_name;
2357 dbus_bool_t expecting_find;
2361 check_func (void *data)
2364 BusActivationEntry *entry;
2366 dbus_bool_t ret_val;
2371 dbus_error_init (&error);
2373 entry = activation_find_entry (d->activation, d->service_name, &error);
2376 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2382 if (d->expecting_find)
2386 dbus_error_free (&error);
2390 if (!d->expecting_find)
2398 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2403 err = !_dbus_test_oom_handling (description, check_func, data);
2405 err = !check_func (data);
2408 _dbus_assert_not_reached ("Test failed");
2414 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2416 BusActivation *activation;
2418 DBusList *directories;
2422 _dbus_string_init_const (&address, "");
2424 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2427 activation = bus_activation_new (NULL, &address, &directories, NULL);
2431 d.activation = activation;
2433 /* Check for existing service file */
2434 d.expecting_find = TRUE;
2435 d.service_name = SERVICE_NAME_1;
2437 if (!do_test ("Existing service file", oom_test, &d))
2440 /* Check for non-existing service file */
2441 d.expecting_find = FALSE;
2442 d.service_name = SERVICE_NAME_3;
2444 if (!do_test ("Nonexisting service file", oom_test, &d))
2447 /* Check for added service file */
2448 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2451 d.expecting_find = TRUE;
2452 d.service_name = SERVICE_NAME_2;
2454 if (!do_test ("Added service file", oom_test, &d))
2457 /* Check for removed service file */
2458 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2461 d.expecting_find = FALSE;
2462 d.service_name = SERVICE_FILE_2;
2464 if (!do_test ("Removed service file", oom_test, &d))
2467 /* Check for updated service file */
2469 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2471 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2474 d.expecting_find = TRUE;
2475 d.service_name = SERVICE_NAME_3;
2477 if (!do_test ("Updated service file, part 1", oom_test, &d))
2480 d.expecting_find = FALSE;
2481 d.service_name = SERVICE_NAME_1;
2483 if (!do_test ("Updated service file, part 2", oom_test, &d))
2486 bus_activation_unref (activation);
2487 _dbus_list_clear (&directories);
2493 bus_activation_service_reload_test (const DBusString *test_data_dir)
2495 DBusString directory;
2497 if (!_dbus_string_init (&directory))
2500 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2503 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2504 !_dbus_generate_random_ascii (&directory, 6))
2509 /* Do normal tests */
2510 if (!init_service_reload_test (&directory))
2511 _dbus_assert_not_reached ("could not initiate service reload test");
2513 if (!do_service_reload_test (&directory, FALSE))
2517 if (!init_service_reload_test (&directory))
2518 _dbus_assert_not_reached ("could not initiate service reload test");
2520 if (!do_service_reload_test (&directory, TRUE))
2523 /* Cleanup test directory */
2524 if (!cleanup_service_reload_test (&directory))
2527 _dbus_string_free (&directory);
2532 #endif /* DBUS_BUILD_TESTS */