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;
269 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
276 systemd_service = NULL;
278 dbus_error_init (&tmp_error);
280 if (!_dbus_string_init (&file_path))
286 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
287 !_dbus_concat_dir_and_file (&file_path, filename))
293 if (!_dbus_stat (&file_path, &stat_buf, NULL))
295 dbus_set_error (error, DBUS_ERROR_FAILED,
296 "Can't stat the service file\n");
300 if (!bus_desktop_file_get_string (desktop_file,
301 DBUS_SERVICE_SECTION,
307 if (!bus_desktop_file_get_string (desktop_file,
308 DBUS_SERVICE_SECTION,
314 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
315 dbus_free (exec_tmp);
318 /* user is not _required_ unless we are using system activation */
319 if (!bus_desktop_file_get_string (desktop_file,
320 DBUS_SERVICE_SECTION,
324 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
325 /* if we got OOM, then exit */
326 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
328 dbus_move_error (&tmp_error, error);
333 /* if we have error because we didn't find anything then continue */
334 dbus_error_free (&tmp_error);
339 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
341 /* systemd service is never required */
342 if (!bus_desktop_file_get_string (desktop_file,
343 DBUS_SERVICE_SECTION,
344 DBUS_SERVICE_SYSTEMD_SERVICE,
345 &systemd_service, &tmp_error))
347 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
348 /* if we got OOM, then exit */
349 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
351 dbus_move_error (&tmp_error, error);
356 /* if we have error because we didn't find anything then continue */
357 dbus_error_free (&tmp_error);
358 dbus_free (systemd_service);
359 systemd_service = NULL;
363 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
365 entry = _dbus_hash_table_lookup_string (s_dir->entries,
366 _dbus_string_get_const_data (filename));
368 if (entry == NULL) /* New file */
370 /* FIXME we need a better-defined algorithm for which service file to
371 * pick than "whichever one is first in the directory listing"
373 if (_dbus_hash_table_lookup_string (activation->entries, name))
375 dbus_set_error (error, DBUS_ERROR_FAILED,
376 "Service %s already exists in activation entry list\n", name);
380 entry = dbus_new0 (BusActivationEntry, 1);
390 entry->systemd_service = systemd_service;
393 /* ownership has been transferred to entry, do not free separately */
397 systemd_service = NULL;
399 entry->s_dir = s_dir;
400 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
401 if (!entry->filename)
407 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
413 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
415 /* Revert the insertion in the entries table */
416 _dbus_hash_table_remove_string (activation->entries, entry->name);
421 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
423 else /* Just update the entry */
425 bus_activation_entry_ref (entry);
426 _dbus_hash_table_remove_string (activation->entries, entry->name);
428 if (_dbus_hash_table_lookup_string (activation->entries, name))
430 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
431 name, _dbus_string_get_const_data (&file_path));
435 /* ownership has been transferred to entry, do not free separately */
436 dbus_free (entry->name);
440 dbus_free (entry->exec);
444 dbus_free (entry->user);
448 dbus_free (entry->systemd_service);
449 entry->systemd_service = systemd_service;
450 systemd_service = NULL;
452 if (!_dbus_hash_table_insert_string (activation->entries,
453 entry->name, bus_activation_entry_ref(entry)))
456 /* Also remove path to entries hash since we want this in sync with
457 * the entries hash table */
458 _dbus_hash_table_remove_string (entry->s_dir->entries,
460 bus_activation_entry_unref (entry);
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;
898 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
900 activation = dbus_new0 (BusActivation, 1);
901 if (activation == NULL)
907 activation->refcount = 1;
908 activation->context = context;
909 activation->n_pending_activations = 0;
911 if (!bus_activation_reload (activation, address, directories, error))
914 /* Initialize this hash table once, we don't want to lose pending
915 * activations on reload. */
916 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
917 (DBusFreeFunction)bus_pending_activation_unref);
919 if (activation->pending_activations == NULL)
925 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
926 (DBusFreeFunction) dbus_free,
927 (DBusFreeFunction) dbus_free);
929 if (activation->environment == NULL)
935 if (!populate_environment (activation))
944 bus_activation_unref (activation);
949 bus_activation_ref (BusActivation *activation)
951 _dbus_assert (activation->refcount > 0);
953 activation->refcount += 1;
959 bus_activation_unref (BusActivation *activation)
961 _dbus_assert (activation->refcount > 0);
963 activation->refcount -= 1;
965 if (activation->refcount > 0)
968 dbus_free (activation->server_address);
969 if (activation->entries)
970 _dbus_hash_table_unref (activation->entries);
971 if (activation->pending_activations)
972 _dbus_hash_table_unref (activation->pending_activations);
973 if (activation->directories)
974 _dbus_hash_table_unref (activation->directories);
975 if (activation->environment)
976 _dbus_hash_table_unref (activation->environment);
978 dbus_free (activation);
982 add_bus_environment (BusActivation *activation,
987 if (!bus_activation_set_environment_variable (activation,
988 "DBUS_STARTER_ADDRESS",
989 activation->server_address,
993 type = bus_context_get_type (activation->context);
996 if (!bus_activation_set_environment_variable (activation,
997 "DBUS_STARTER_BUS_TYPE", type,
1001 if (strcmp (type, "session") == 0)
1003 if (!bus_activation_set_environment_variable (activation,
1004 "DBUS_SESSION_BUS_ADDRESS",
1005 activation->server_address,
1009 else if (strcmp (type, "system") == 0)
1011 if (!bus_activation_set_environment_variable (activation,
1012 "DBUS_SYSTEM_BUS_ADDRESS",
1013 activation->server_address,
1024 BusPendingActivation *pending_activation;
1025 DBusPreallocatedHash *hash_entry;
1026 } RestorePendingData;
1029 restore_pending (void *data)
1031 RestorePendingData *d = data;
1033 _dbus_assert (d->pending_activation != NULL);
1034 _dbus_assert (d->hash_entry != NULL);
1036 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1037 d->pending_activation->service_name,
1038 d->pending_activation->timeout_added);
1040 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1042 d->pending_activation->service_name, d->pending_activation);
1044 bus_pending_activation_ref (d->pending_activation);
1046 d->hash_entry = NULL;
1050 free_pending_restore_data (void *data)
1052 RestorePendingData *d = data;
1055 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1058 bus_pending_activation_unref (d->pending_activation);
1064 add_restore_pending_to_transaction (BusTransaction *transaction,
1065 BusPendingActivation *pending_activation)
1067 RestorePendingData *d;
1069 d = dbus_new (RestorePendingData, 1);
1073 d->pending_activation = pending_activation;
1074 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1076 bus_pending_activation_ref (d->pending_activation);
1078 if (d->hash_entry == NULL ||
1079 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1080 free_pending_restore_data))
1082 free_pending_restore_data (d);
1086 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1092 bus_activation_service_created (BusActivation *activation,
1093 const char *service_name,
1094 BusTransaction *transaction,
1097 BusPendingActivation *pending_activation;
1098 DBusMessage *message;
1101 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1103 /* Check if it's a pending activation */
1104 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1106 if (!pending_activation)
1109 link = _dbus_list_get_first_link (&pending_activation->entries);
1110 while (link != NULL)
1112 BusPendingActivationEntry *entry = link->data;
1113 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1115 if (dbus_connection_get_is_connected (entry->connection))
1117 /* Only send activation replies to regular activation requests. */
1118 if (!entry->auto_activation)
1120 dbus_uint32_t result;
1122 message = dbus_message_new_method_return (entry->activation_message);
1125 BUS_SET_OOM (error);
1129 result = DBUS_START_REPLY_SUCCESS;
1131 if (!dbus_message_append_args (message,
1132 DBUS_TYPE_UINT32, &result,
1135 dbus_message_unref (message);
1136 BUS_SET_OOM (error);
1140 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1142 dbus_message_unref (message);
1143 BUS_SET_OOM (error);
1147 dbus_message_unref (message);
1161 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1162 BusService *service,
1163 BusTransaction *transaction,
1166 BusPendingActivation *pending_activation;
1169 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1171 /* Check if it's a pending activation */
1172 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1173 bus_service_get_name (service));
1175 if (!pending_activation)
1178 link = _dbus_list_get_first_link (&pending_activation->entries);
1179 while (link != NULL)
1181 BusPendingActivationEntry *entry = link->data;
1182 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1184 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1186 DBusConnection *addressed_recipient;
1188 addressed_recipient = bus_service_get_primary_owners_connection (service);
1190 /* Resume dispatching where we left off in bus_dispatch() */
1191 if (!bus_dispatch_matches (transaction,
1193 addressed_recipient,
1194 entry->activation_message, error))
1201 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1203 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1204 BUS_SET_OOM (error);
1208 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1217 * FIXME @todo the error messages here would ideally be preallocated
1218 * so we don't need to allocate memory to send them.
1219 * Using the usual tactic, prealloc an OOM message, then
1220 * if we can't alloc the real error send the OOM error instead.
1223 try_send_activation_failure (BusPendingActivation *pending_activation,
1224 const DBusError *how)
1226 BusActivation *activation;
1228 BusTransaction *transaction;
1230 activation = pending_activation->activation;
1232 transaction = bus_transaction_new (activation->context);
1233 if (transaction == NULL)
1236 link = _dbus_list_get_first_link (&pending_activation->entries);
1237 while (link != NULL)
1239 BusPendingActivationEntry *entry = link->data;
1240 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1242 if (dbus_connection_get_is_connected (entry->connection))
1244 if (!bus_transaction_send_error_reply (transaction,
1247 entry->activation_message))
1254 bus_transaction_execute_and_free (transaction);
1260 bus_transaction_cancel_and_free (transaction);
1265 * Free the pending activation and send an error message to all the
1266 * connections that were waiting for it.
1269 pending_activation_failed (BusPendingActivation *pending_activation,
1270 const DBusError *how)
1272 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1273 while (!try_send_activation_failure (pending_activation, how))
1274 _dbus_wait_for_memory ();
1276 /* Destroy this pending activation */
1277 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1278 pending_activation->service_name);
1282 * Depending on the exit code of the helper, set the error accordingly
1285 handle_servicehelper_exit_error (int exit_code,
1290 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1291 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1292 "Launcher could not run (out of memory)");
1294 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1295 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1296 "Failed to setup environment correctly");
1298 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1299 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1300 "Bus name is not valid or missing");
1302 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1303 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1304 "Bus name not found in system service directory");
1306 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1307 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1308 "The permission of the setuid helper is not correct");
1310 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1311 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1312 "The service file is incorrect or does not have all required attributes");
1314 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1315 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1316 "Cannot launch daemon, file not found or permissions invalid");
1318 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1319 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1320 "Invalid arguments to command line");
1322 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1323 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1324 "Launched child was signaled, it probably crashed");
1327 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1328 "Launch helper exited with unknown return code %i", exit_code);
1334 babysitter_watch_callback (DBusWatch *watch,
1335 unsigned int condition,
1338 BusPendingActivation *pending_activation = data;
1340 DBusBabysitter *babysitter;
1341 dbus_bool_t uses_servicehelper;
1343 babysitter = pending_activation->babysitter;
1345 _dbus_babysitter_ref (babysitter);
1347 retval = dbus_watch_handle (watch, condition);
1349 /* There are two major cases here; are we the system bus or the session? Here this
1350 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1351 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1353 * In both cases though, just ignore when a process exits with status 0; it's possible for
1354 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1355 * condition between this code and the child process claiming the bus name.
1357 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1359 /* FIXME this is broken in the same way that
1360 * connection watches used to be; there should be
1361 * a separate callback for status change, instead
1362 * of doing "if we handled a watch status might
1365 * Fixing this lets us move dbus_watch_handle
1366 * calls into dbus-mainloop.c
1368 if (_dbus_babysitter_get_child_exited (babysitter))
1372 dbus_bool_t activation_failed;
1375 dbus_error_init (&error);
1377 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1379 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1381 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1382 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1384 activation_failed = exit_code != 0;
1386 dbus_error_free(&error);
1388 if (activation_failed)
1390 if (uses_servicehelper)
1391 handle_servicehelper_exit_error (exit_code, &error);
1393 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1398 activation_failed = TRUE;
1401 if (activation_failed)
1403 /* Destroy all pending activations with the same exec */
1404 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1406 while (_dbus_hash_iter_next (&iter))
1408 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1410 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1411 pending_activation_failed (p, &error);
1414 /* Destroys the pending activation */
1415 pending_activation_failed (pending_activation, &error);
1417 dbus_error_free (&error);
1421 _dbus_babysitter_unref (babysitter);
1427 add_babysitter_watch (DBusWatch *watch,
1430 BusPendingActivation *pending_activation = data;
1432 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1433 watch, babysitter_watch_callback, pending_activation,
1438 remove_babysitter_watch (DBusWatch *watch,
1441 BusPendingActivation *pending_activation = data;
1443 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1444 watch, babysitter_watch_callback, pending_activation);
1448 pending_activation_timed_out (void *data)
1450 BusPendingActivation *pending_activation = data;
1453 /* Kill the spawned process, since it sucks
1454 * (not sure this is what we want to do, but
1455 * may as well try it for now)
1457 if (pending_activation->babysitter)
1458 _dbus_babysitter_kill_child (pending_activation->babysitter);
1460 dbus_error_init (&error);
1462 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1463 "Activation of %s timed out",
1464 pending_activation->service_name);
1466 pending_activation_failed (pending_activation, &error);
1468 dbus_error_free (&error);
1474 cancel_pending (void *data)
1476 BusPendingActivation *pending_activation = data;
1478 _dbus_verbose ("Canceling pending activation of %s\n",
1479 pending_activation->service_name);
1481 if (pending_activation->babysitter)
1482 _dbus_babysitter_kill_child (pending_activation->babysitter);
1484 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1485 pending_activation->service_name);
1489 free_pending_cancel_data (void *data)
1491 BusPendingActivation *pending_activation = data;
1493 bus_pending_activation_unref (pending_activation);
1497 add_cancel_pending_to_transaction (BusTransaction *transaction,
1498 BusPendingActivation *pending_activation)
1500 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1502 free_pending_cancel_data))
1505 bus_pending_activation_ref (pending_activation);
1507 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1513 update_service_cache (BusActivation *activation, DBusError *error)
1517 _dbus_hash_iter_init (activation->directories, &iter);
1518 while (_dbus_hash_iter_next (&iter))
1520 DBusError tmp_error;
1521 BusServiceDirectory *s_dir;
1523 s_dir = _dbus_hash_iter_get_value (&iter);
1525 dbus_error_init (&tmp_error);
1526 if (!update_directory (activation, s_dir, &tmp_error))
1528 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1530 dbus_move_error (&tmp_error, error);
1534 dbus_error_free (&tmp_error);
1542 static BusActivationEntry *
1543 activation_find_entry (BusActivation *activation,
1544 const char *service_name,
1547 BusActivationEntry *entry;
1549 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1552 if (!update_service_cache (activation, error))
1555 entry = _dbus_hash_table_lookup_string (activation->entries,
1560 BusActivationEntry *updated_entry;
1562 if (!check_service_file (activation, entry, &updated_entry, error))
1565 entry = updated_entry;
1570 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1571 "The name %s was not provided by any .service files",
1580 bus_activation_get_environment (BusActivation *activation)
1587 length = _dbus_hash_table_get_n_entries (activation->environment);
1589 environment = dbus_new0 (char *, length + 1);
1591 if (environment == NULL)
1595 _dbus_hash_iter_init (activation->environment, &iter);
1597 if (!_dbus_string_init (&entry))
1599 dbus_free_string_array (environment);
1603 while (_dbus_hash_iter_next (&iter))
1605 const char *key, *value;
1607 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1608 value = (const char *) _dbus_hash_iter_get_value (&iter);
1610 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1613 if (!_dbus_string_steal_data (&entry, environment + i))
1618 _dbus_string_free (&entry);
1622 dbus_free_string_array (environment);
1630 bus_activation_set_environment_variable (BusActivation *activation,
1642 hash_key = _dbus_strdup (key);
1644 if (hash_key == NULL)
1647 hash_value = _dbus_strdup (value);
1649 if (hash_value == NULL)
1652 if (!_dbus_hash_table_insert_string (activation->environment,
1653 hash_key, hash_value))
1658 if (retval == FALSE)
1660 dbus_free (hash_key);
1661 dbus_free (hash_value);
1662 BUS_SET_OOM (error);
1669 bus_activation_activate_service (BusActivation *activation,
1670 DBusConnection *connection,
1671 BusTransaction *transaction,
1672 dbus_bool_t auto_activation,
1673 DBusMessage *activation_message,
1674 const char *service_name,
1677 BusActivationEntry *entry;
1678 BusPendingActivation *pending_activation;
1679 BusPendingActivationEntry *pending_activation_entry;
1680 DBusMessage *message;
1681 DBusString service_str;
1682 const char *servicehelper;
1688 dbus_bool_t activated;
1693 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1695 if (activation->n_pending_activations >=
1696 bus_context_get_max_pending_activations (activation->context))
1698 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1699 "The maximum number of pending activations has been reached, activation of %s failed",
1704 entry = activation_find_entry (activation, service_name, error);
1708 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1709 * call us if the service is already active.
1711 if (!auto_activation)
1713 /* Check if the service is active */
1714 _dbus_string_init_const (&service_str, service_name);
1715 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1717 dbus_uint32_t result;
1719 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1721 message = dbus_message_new_method_return (activation_message);
1725 _dbus_verbose ("No memory to create reply to activate message\n");
1726 BUS_SET_OOM (error);
1730 result = DBUS_START_REPLY_ALREADY_RUNNING;
1732 if (!dbus_message_append_args (message,
1733 DBUS_TYPE_UINT32, &result,
1736 _dbus_verbose ("No memory to set args of reply to activate message\n");
1737 BUS_SET_OOM (error);
1738 dbus_message_unref (message);
1742 retval = bus_transaction_send_from_driver (transaction, connection, message);
1743 dbus_message_unref (message);
1746 _dbus_verbose ("Failed to send reply\n");
1747 BUS_SET_OOM (error);
1754 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1755 if (!pending_activation_entry)
1757 _dbus_verbose ("Failed to create pending activation entry\n");
1758 BUS_SET_OOM (error);
1762 pending_activation_entry->auto_activation = auto_activation;
1764 pending_activation_entry->activation_message = activation_message;
1765 dbus_message_ref (activation_message);
1766 pending_activation_entry->connection = connection;
1767 dbus_connection_ref (connection);
1769 /* Check if the service is being activated */
1770 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1771 if (pending_activation)
1773 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1775 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1777 BUS_SET_OOM (error);
1778 bus_pending_activation_entry_free (pending_activation_entry);
1782 pending_activation->n_entries += 1;
1783 pending_activation->activation->n_pending_activations += 1;
1787 pending_activation = dbus_new0 (BusPendingActivation, 1);
1788 if (!pending_activation)
1790 _dbus_verbose ("Failed to create pending activation\n");
1792 BUS_SET_OOM (error);
1793 bus_pending_activation_entry_free (pending_activation_entry);
1797 pending_activation->activation = activation;
1798 pending_activation->refcount = 1;
1800 pending_activation->service_name = _dbus_strdup (service_name);
1801 if (!pending_activation->service_name)
1803 _dbus_verbose ("Failed to copy service name for pending activation\n");
1805 BUS_SET_OOM (error);
1806 bus_pending_activation_unref (pending_activation);
1807 bus_pending_activation_entry_free (pending_activation_entry);
1811 pending_activation->exec = _dbus_strdup (entry->exec);
1812 if (!pending_activation->exec)
1814 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1815 BUS_SET_OOM (error);
1816 bus_pending_activation_unref (pending_activation);
1817 bus_pending_activation_entry_free (pending_activation_entry);
1821 if (entry->systemd_service)
1823 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1824 if (!pending_activation->systemd_service)
1826 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1827 BUS_SET_OOM (error);
1828 bus_pending_activation_unref (pending_activation);
1829 bus_pending_activation_entry_free (pending_activation_entry);
1834 pending_activation->timeout =
1835 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1836 pending_activation_timed_out,
1839 if (!pending_activation->timeout)
1841 _dbus_verbose ("Failed to create timeout for pending activation\n");
1843 BUS_SET_OOM (error);
1844 bus_pending_activation_unref (pending_activation);
1845 bus_pending_activation_entry_free (pending_activation_entry);
1849 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1850 pending_activation->timeout,
1851 handle_timeout_callback,
1855 _dbus_verbose ("Failed to add timeout for pending activation\n");
1857 BUS_SET_OOM (error);
1858 bus_pending_activation_unref (pending_activation);
1859 bus_pending_activation_entry_free (pending_activation_entry);
1863 pending_activation->timeout_added = TRUE;
1865 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1867 _dbus_verbose ("Failed to add entry to just-created 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->n_entries += 1;
1876 pending_activation->activation->n_pending_activations += 1;
1879 _dbus_hash_iter_init (activation->pending_activations, &iter);
1880 while (_dbus_hash_iter_next (&iter))
1882 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1884 if (strcmp (p->exec, entry->exec) == 0)
1891 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1892 pending_activation->service_name,
1893 pending_activation))
1895 _dbus_verbose ("Failed to put pending activation in hash table\n");
1897 BUS_SET_OOM (error);
1898 bus_pending_activation_unref (pending_activation);
1903 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1905 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1906 BUS_SET_OOM (error);
1907 _dbus_hash_table_remove_string (activation->pending_activations,
1908 pending_activation->service_name);
1916 if (bus_context_get_systemd_activation (activation->context))
1918 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1919 /* systemd itself is missing apparently. That can happen
1920 only during early startup. Let's just wait until systemd
1921 connects to us and do nothing. */
1924 if (entry->systemd_service)
1926 BusTransaction *activation_transaction;
1927 DBusString service_string;
1928 BusService *service;
1929 BusRegistry *registry;
1931 /* OK, we have a systemd service configured for this entry,
1932 hence let's enqueue an activation request message. This
1933 is implemented as a directed signal, not a method call,
1934 for three reasons: 1) we don't expect a response on
1935 success, where we just expect a name appearing on the
1936 bus; 2) at this time the systemd service might not yet
1937 have connected, so we wouldn't know the message serial at
1938 this point to set up a pending call; 3) it is ugly if the
1939 bus suddenly becomes the caller of a remote method. */
1941 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1942 "org.freedesktop.systemd1.Activator",
1943 "ActivationRequest");
1946 _dbus_verbose ("No memory to create activation message\n");
1947 BUS_SET_OOM (error);
1951 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1952 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1953 !dbus_message_append_args (message,
1954 DBUS_TYPE_STRING, &entry->systemd_service,
1957 _dbus_verbose ("No memory to set args of activation message\n");
1958 dbus_message_unref (message);
1959 BUS_SET_OOM (error);
1963 /* Create our transaction */
1964 activation_transaction = bus_transaction_new (activation->context);
1965 if (activation_transaction == NULL)
1967 _dbus_verbose ("No memory to create activation transaction\n");
1968 dbus_message_unref (message);
1969 BUS_SET_OOM (error);
1973 /* Check whether systemd is already connected */
1974 registry = bus_connection_get_registry (connection);
1975 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1976 service = bus_registry_lookup (registry, &service_string);
1978 if (service != NULL)
1979 /* Wonderful, systemd is connected, let's just send the msg */
1980 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1983 /* systemd is not around, let's "activate" it. */
1984 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1985 message, "org.freedesktop.systemd1", error);
1987 dbus_message_unref (message);
1991 _DBUS_ASSERT_ERROR_IS_SET (error);
1992 _dbus_verbose ("failed to send activation message: %s\n", error->name);
1993 bus_transaction_cancel_and_free (activation_transaction);
1997 bus_transaction_execute_and_free (activation_transaction);
2001 /* OK, we have no configured systemd service, hence let's
2002 proceed with traditional activation. */
2005 /* use command as system and session different */
2006 if (!_dbus_string_init (&command))
2008 BUS_SET_OOM (error);
2012 /* does the bus use a helper? */
2013 servicehelper = bus_context_get_servicehelper (activation->context);
2014 if (servicehelper != NULL)
2016 if (entry->user == NULL)
2018 _dbus_string_free (&command);
2019 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2020 "Cannot do system-bus activation with no user\n");
2024 /* join the helper path and the service name */
2025 if (!_dbus_string_append (&command, servicehelper))
2027 _dbus_string_free (&command);
2028 BUS_SET_OOM (error);
2031 if (!_dbus_string_append (&command, " "))
2033 _dbus_string_free (&command);
2034 BUS_SET_OOM (error);
2037 if (!_dbus_string_append (&command, service_name))
2039 _dbus_string_free (&command);
2040 BUS_SET_OOM (error);
2046 /* the bus does not use a helper, so we can append arguments with the exec line */
2047 if (!_dbus_string_append (&command, entry->exec))
2049 _dbus_string_free (&command);
2050 BUS_SET_OOM (error);
2055 /* convert command into arguments */
2056 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2058 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2059 _DBUS_ASSERT_ERROR_IS_SET (error);
2061 _dbus_hash_table_remove_string (activation->pending_activations,
2062 pending_activation->service_name);
2064 _dbus_string_free (&command);
2067 _dbus_string_free (&command);
2069 if (!add_bus_environment (activation, error))
2071 _DBUS_ASSERT_ERROR_IS_SET (error);
2072 dbus_free_string_array (argv);
2076 envp = bus_activation_get_environment (activation);
2080 BUS_SET_OOM (error);
2081 dbus_free_string_array (argv);
2085 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2086 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2091 _dbus_verbose ("Failed to spawn child\n");
2092 _DBUS_ASSERT_ERROR_IS_SET (error);
2093 dbus_free_string_array (argv);
2094 dbus_free_string_array (envp);
2099 dbus_free_string_array (argv);
2102 _dbus_assert (pending_activation->babysitter != NULL);
2104 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2105 add_babysitter_watch,
2106 remove_babysitter_watch,
2111 BUS_SET_OOM (error);
2112 _dbus_verbose ("Failed to set babysitter watch functions\n");
2120 bus_activation_list_services (BusActivation *activation,
2128 len = _dbus_hash_table_get_n_entries (activation->entries);
2129 retval = dbus_new (char *, len + 1);
2134 _dbus_hash_iter_init (activation->entries, &iter);
2136 while (_dbus_hash_iter_next (&iter))
2138 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2140 retval[i] = _dbus_strdup (entry->name);
2141 if (retval[i] == NULL)
2156 for (j = 0; j < i; j++)
2157 dbus_free (retval[i]);
2164 dbus_activation_systemd_failure (BusActivation *activation,
2165 DBusMessage *message)
2168 const char *code, *str, *unit = NULL;
2170 dbus_error_init(&error);
2172 /* This is called whenever the systemd activator sent us a
2173 response. We'll invalidate all pending activations that match the
2176 if (dbus_message_get_args (message, &error,
2177 DBUS_TYPE_STRING, &unit,
2178 DBUS_TYPE_STRING, &code,
2179 DBUS_TYPE_STRING, &str,
2181 dbus_set_error(&error, code, str);
2187 _dbus_hash_iter_init (activation->pending_activations,
2190 while (_dbus_hash_iter_next (&iter))
2192 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2194 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2195 pending_activation_failed(p, &error);
2199 dbus_error_free(&error);
2204 #ifdef DBUS_BUILD_TESTS
2208 #define SERVICE_NAME_1 "MyService1"
2209 #define SERVICE_NAME_2 "MyService2"
2210 #define SERVICE_NAME_3 "MyService3"
2212 #define SERVICE_FILE_1 "service-1.service"
2213 #define SERVICE_FILE_2 "service-2.service"
2214 #define SERVICE_FILE_3 "service-3.service"
2217 test_create_service_file (DBusString *dir,
2218 const char *filename,
2222 DBusString file_name, full_path;
2224 dbus_bool_t ret_val;
2227 _dbus_string_init_const (&file_name, filename);
2229 if (!_dbus_string_init (&full_path))
2232 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2233 !_dbus_concat_dir_and_file (&full_path, &file_name))
2239 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2246 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2250 _dbus_string_free (&full_path);
2255 test_remove_service_file (DBusString *dir, const char *filename)
2257 DBusString file_name, full_path;
2258 dbus_bool_t ret_val;
2262 _dbus_string_init_const (&file_name, filename);
2264 if (!_dbus_string_init (&full_path))
2267 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2268 !_dbus_concat_dir_and_file (&full_path, &file_name))
2274 if (!_dbus_delete_file (&full_path, NULL))
2281 _dbus_string_free (&full_path);
2286 test_remove_directory (DBusString *dir)
2289 DBusString filename, full_path;
2290 dbus_bool_t ret_val;
2294 if (!_dbus_string_init (&filename))
2297 if (!_dbus_string_init (&full_path))
2299 _dbus_string_free (&filename);
2303 iter = _dbus_directory_open (dir, NULL);
2310 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2312 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2318 _dbus_directory_close (iter);
2320 if (!_dbus_delete_directory (dir, NULL))
2327 _dbus_string_free (&filename);
2328 _dbus_string_free (&full_path);
2334 init_service_reload_test (DBusString *dir)
2338 if (!_dbus_stat (dir, &stat_buf, NULL))
2340 if (!_dbus_create_directory (dir, NULL))
2345 if (!test_remove_directory (dir))
2348 if (!_dbus_create_directory (dir, NULL))
2352 /* Create one initial file */
2353 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2360 cleanup_service_reload_test (DBusString *dir)
2362 if (!test_remove_directory (dir))
2370 BusActivation *activation;
2371 const char *service_name;
2372 dbus_bool_t expecting_find;
2376 check_func (void *data)
2379 BusActivationEntry *entry;
2381 dbus_bool_t ret_val;
2386 dbus_error_init (&error);
2388 entry = activation_find_entry (d->activation, d->service_name, &error);
2391 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2397 if (d->expecting_find)
2401 dbus_error_free (&error);
2405 if (!d->expecting_find)
2413 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2418 err = !_dbus_test_oom_handling (description, check_func, data);
2420 err = !check_func (data);
2423 _dbus_assert_not_reached ("Test failed");
2429 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2431 BusActivation *activation;
2433 DBusList *directories;
2437 _dbus_string_init_const (&address, "");
2439 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2442 activation = bus_activation_new (NULL, &address, &directories, NULL);
2446 d.activation = activation;
2448 /* Check for existing service file */
2449 d.expecting_find = TRUE;
2450 d.service_name = SERVICE_NAME_1;
2452 if (!do_test ("Existing service file", oom_test, &d))
2455 /* Check for non-existing service file */
2456 d.expecting_find = FALSE;
2457 d.service_name = SERVICE_NAME_3;
2459 if (!do_test ("Nonexisting service file", oom_test, &d))
2462 /* Check for added service file */
2463 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2466 d.expecting_find = TRUE;
2467 d.service_name = SERVICE_NAME_2;
2469 if (!do_test ("Added service file", oom_test, &d))
2472 /* Check for removed service file */
2473 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2476 d.expecting_find = FALSE;
2477 d.service_name = SERVICE_FILE_2;
2479 if (!do_test ("Removed service file", oom_test, &d))
2482 /* Check for updated service file */
2484 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2486 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2489 d.expecting_find = TRUE;
2490 d.service_name = SERVICE_NAME_3;
2492 if (!do_test ("Updated service file, part 1", oom_test, &d))
2495 d.expecting_find = FALSE;
2496 d.service_name = SERVICE_NAME_1;
2498 if (!do_test ("Updated service file, part 2", oom_test, &d))
2501 bus_activation_unref (activation);
2502 _dbus_list_clear (&directories);
2508 bus_activation_service_reload_test (const DBusString *test_data_dir)
2510 DBusString directory;
2512 if (!_dbus_string_init (&directory))
2515 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2518 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2519 !_dbus_generate_random_ascii (&directory, 6))
2524 /* Do normal tests */
2525 if (!init_service_reload_test (&directory))
2526 _dbus_assert_not_reached ("could not initiate service reload test");
2528 if (!do_service_reload_test (&directory, FALSE))
2532 if (!init_service_reload_test (&directory))
2533 _dbus_assert_not_reached ("could not initiate service reload test");
2535 if (!do_service_reload_test (&directory, TRUE))
2538 /* Cleanup test directory */
2539 if (!cleanup_service_reload_test (&directory))
2542 _dbus_string_free (&directory);
2547 #endif /* DBUS_BUILD_TESTS */