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 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
314 dbus_free (exec_tmp);
317 /* user is not _required_ unless we are using system activation */
318 if (!bus_desktop_file_get_string (desktop_file,
319 DBUS_SERVICE_SECTION,
323 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
324 /* if we got OOM, then exit */
325 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
327 dbus_move_error (&tmp_error, error);
332 /* if we have error because we didn't find anything then continue */
333 dbus_error_free (&tmp_error);
338 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
340 /* systemd service is never required */
341 if (!bus_desktop_file_get_string (desktop_file,
342 DBUS_SERVICE_SECTION,
343 DBUS_SERVICE_SYSTEMD_SERVICE,
344 &systemd_service, &tmp_error))
346 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
347 /* if we got OOM, then exit */
348 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
350 dbus_move_error (&tmp_error, error);
355 /* if we have error because we didn't find anything then continue */
356 dbus_error_free (&tmp_error);
357 dbus_free (systemd_service);
358 systemd_service = NULL;
362 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
364 entry = _dbus_hash_table_lookup_string (s_dir->entries,
365 _dbus_string_get_const_data (filename));
367 if (entry == NULL) /* New file */
369 /* FIXME we need a better-defined algorithm for which service file to
370 * pick than "whichever one is first in the directory listing"
372 if (_dbus_hash_table_lookup_string (activation->entries, name))
374 dbus_set_error (error, DBUS_ERROR_FAILED,
375 "Service %s already exists in activation entry list\n", name);
379 entry = dbus_new0 (BusActivationEntry, 1);
389 entry->systemd_service = systemd_service;
392 /* ownership has been transferred to entry, do not free separately */
396 systemd_service = NULL;
397 entry->s_dir = s_dir;
398 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
399 if (!entry->filename)
405 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
411 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
413 /* Revert the insertion in the entries table */
414 _dbus_hash_table_remove_string (activation->entries, entry->name);
419 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
421 else /* Just update the entry */
423 bus_activation_entry_ref (entry);
424 _dbus_hash_table_remove_string (activation->entries, entry->name);
426 if (_dbus_hash_table_lookup_string (activation->entries, name))
428 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
429 name, _dbus_string_get_const_data (&file_path));
433 dbus_free (entry->name);
434 dbus_free (entry->exec);
435 dbus_free (entry->user);
436 dbus_free (entry->systemd_service);
437 entry->systemd_service = systemd_service;
441 /* ownership has been transferred to entry, do not free separately */
445 systemd_service = NULL;
447 if (!_dbus_hash_table_insert_string (activation->entries,
448 entry->name, bus_activation_entry_ref(entry)))
451 /* Also remove path to entries hash since we want this in sync with
452 * the entries hash table */
453 _dbus_hash_table_remove_string (entry->s_dir->entries,
455 bus_activation_entry_unref (entry);
460 entry->mtime = stat_buf.mtime;
462 _dbus_string_free (&file_path);
463 bus_activation_entry_unref (entry);
471 dbus_free (systemd_service);
472 _dbus_string_free (&file_path);
475 bus_activation_entry_unref (entry);
481 check_service_file (BusActivation *activation,
482 BusActivationEntry *entry,
483 BusActivationEntry **updated_entry,
488 BusActivationEntry *tmp_entry;
489 DBusString file_path;
495 _dbus_string_init_const (&filename, entry->filename);
497 if (!_dbus_string_init (&file_path))
503 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
504 !_dbus_concat_dir_and_file (&file_path, &filename))
511 if (!_dbus_stat (&file_path, &stat_buf, NULL))
513 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
514 _dbus_string_get_const_data (&file_path));
516 _dbus_hash_table_remove_string (activation->entries, entry->name);
517 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
525 if (stat_buf.mtime > entry->mtime)
527 BusDesktopFile *desktop_file;
530 dbus_error_init (&tmp_error);
532 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
533 if (desktop_file == NULL)
535 _dbus_verbose ("Could not load %s: %s\n",
536 _dbus_string_get_const_data (&file_path),
538 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
540 dbus_move_error (&tmp_error, error);
544 dbus_error_free (&tmp_error);
549 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
550 * Handle these both better
552 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
554 bus_desktop_file_free (desktop_file);
555 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
557 dbus_move_error (&tmp_error, error);
561 dbus_error_free (&tmp_error);
566 bus_desktop_file_free (desktop_file);
572 _dbus_string_free (&file_path);
574 if (updated_entry != NULL)
575 *updated_entry = tmp_entry;
580 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
581 * hash entries it already added.
584 update_directory (BusActivation *activation,
585 BusServiceDirectory *s_dir,
589 DBusString dir, filename;
590 BusDesktopFile *desktop_file;
593 BusActivationEntry *entry;
594 DBusString full_path;
596 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
601 _dbus_string_init_const (&dir, s_dir->dir_c);
603 if (!_dbus_string_init (&filename))
609 if (!_dbus_string_init (&full_path))
612 _dbus_string_free (&filename);
618 /* from this point it's safe to "goto out" */
620 iter = _dbus_directory_open (&dir, error);
623 _dbus_verbose ("Failed to open directory %s: %s\n",
625 error ? error->message : "unknown");
629 /* Now read the files */
630 dbus_error_init (&tmp_error);
631 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
633 _dbus_assert (!dbus_error_is_set (&tmp_error));
635 _dbus_string_set_length (&full_path, 0);
637 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
639 _dbus_verbose ("Skipping non-.service file %s\n",
640 _dbus_string_get_const_data (&filename));
644 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
645 if (entry) /* Already has this service file in the cache */
647 if (!check_service_file (activation, entry, NULL, error))
653 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
654 !_dbus_concat_dir_and_file (&full_path, &filename))
661 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
662 if (desktop_file == NULL)
664 _dbus_verbose ("Could not load %s: %s\n",
665 _dbus_string_get_const_data (&full_path),
668 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
670 dbus_move_error (&tmp_error, error);
674 dbus_error_free (&tmp_error);
678 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
679 * Handle these both better
681 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
683 bus_desktop_file_free (desktop_file);
686 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
687 _dbus_string_get_const_data (&full_path), tmp_error.message);
689 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
691 dbus_move_error (&tmp_error, error);
695 dbus_error_free (&tmp_error);
700 bus_desktop_file_free (desktop_file);
706 if (dbus_error_is_set (&tmp_error))
708 dbus_move_error (&tmp_error, error);
716 _DBUS_ASSERT_ERROR_IS_SET (error);
718 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
721 _dbus_directory_close (iter);
722 _dbus_string_free (&filename);
723 _dbus_string_free (&full_path);
729 populate_environment (BusActivation *activation)
735 dbus_bool_t retval = FALSE;
737 environment = _dbus_get_environment ();
739 if (environment == NULL)
742 if (!_dbus_string_init (&key))
744 dbus_free_string_array (environment);
748 if (!_dbus_string_init (&value))
750 _dbus_string_free (&key);
751 dbus_free_string_array (environment);
755 for (i = 0; environment[i] != NULL; i++)
757 if (!_dbus_string_append (&key, environment[i]))
760 if (_dbus_string_split_on_byte (&key, '=', &value))
762 char *hash_key, *hash_value;
764 if (!_dbus_string_steal_data (&key, &hash_key))
767 if (!_dbus_string_steal_data (&value, &hash_value))
770 if (!_dbus_hash_table_insert_string (activation->environment,
771 hash_key, hash_value))
774 _dbus_string_set_length (&key, 0);
775 _dbus_string_set_length (&value, 0);
778 if (environment[i] != NULL)
784 _dbus_string_free (&key);
785 _dbus_string_free (&value);
786 dbus_free_string_array (environment);
792 bus_activation_reload (BusActivation *activation,
793 const DBusString *address,
794 DBusList **directories,
800 if (activation->server_address != NULL)
801 dbus_free (activation->server_address);
802 if (!_dbus_string_copy_data (address, &activation->server_address))
808 if (activation->entries != NULL)
809 _dbus_hash_table_unref (activation->entries);
810 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
811 (DBusFreeFunction)bus_activation_entry_unref);
812 if (activation->entries == NULL)
818 if (activation->directories != NULL)
819 _dbus_hash_table_unref (activation->directories);
820 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
821 (DBusFreeFunction)bus_service_directory_unref);
823 if (activation->directories == NULL)
829 link = _dbus_list_get_first_link (directories);
832 BusServiceDirectory *s_dir;
834 dir = _dbus_strdup ((const char *) link->data);
841 s_dir = dbus_new0 (BusServiceDirectory, 1);
852 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
853 (DBusFreeFunction)bus_activation_entry_unref);
857 bus_service_directory_unref (s_dir);
862 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
864 bus_service_directory_unref (s_dir);
869 /* only fail on OOM, it is ok if we can't read the directory */
870 if (!update_directory (activation, s_dir, error))
872 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
875 dbus_error_free (error);
878 link = _dbus_list_get_next_link (directories, link);
887 bus_activation_new (BusContext *context,
888 const DBusString *address,
889 DBusList **directories,
892 BusActivation *activation;
896 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
898 activation = dbus_new0 (BusActivation, 1);
899 if (activation == NULL)
905 activation->refcount = 1;
906 activation->context = context;
907 activation->n_pending_activations = 0;
909 if (!bus_activation_reload (activation, address, directories, error))
912 /* Initialize this hash table once, we don't want to lose pending
913 * activations on reload. */
914 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
915 (DBusFreeFunction)bus_pending_activation_unref);
917 if (activation->pending_activations == NULL)
923 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
924 (DBusFreeFunction) dbus_free,
925 (DBusFreeFunction) dbus_free);
927 if (activation->environment == NULL)
933 if (!populate_environment (activation))
942 bus_activation_unref (activation);
947 bus_activation_ref (BusActivation *activation)
949 _dbus_assert (activation->refcount > 0);
951 activation->refcount += 1;
957 bus_activation_unref (BusActivation *activation)
959 _dbus_assert (activation->refcount > 0);
961 activation->refcount -= 1;
963 if (activation->refcount > 0)
966 dbus_free (activation->server_address);
967 if (activation->entries)
968 _dbus_hash_table_unref (activation->entries);
969 if (activation->pending_activations)
970 _dbus_hash_table_unref (activation->pending_activations);
971 if (activation->directories)
972 _dbus_hash_table_unref (activation->directories);
973 if (activation->environment)
974 _dbus_hash_table_unref (activation->environment);
976 dbus_free (activation);
980 add_bus_environment (BusActivation *activation,
985 if (!bus_activation_set_environment_variable (activation,
986 "DBUS_STARTER_ADDRESS",
987 activation->server_address,
991 type = bus_context_get_type (activation->context);
994 if (!bus_activation_set_environment_variable (activation,
995 "DBUS_STARTER_BUS_TYPE", type,
999 if (strcmp (type, "session") == 0)
1001 if (!bus_activation_set_environment_variable (activation,
1002 "DBUS_SESSION_BUS_ADDRESS",
1003 activation->server_address,
1007 else if (strcmp (type, "system") == 0)
1009 if (!bus_activation_set_environment_variable (activation,
1010 "DBUS_SYSTEM_BUS_ADDRESS",
1011 activation->server_address,
1022 BusPendingActivation *pending_activation;
1023 DBusPreallocatedHash *hash_entry;
1024 } RestorePendingData;
1027 restore_pending (void *data)
1029 RestorePendingData *d = data;
1031 _dbus_assert (d->pending_activation != NULL);
1032 _dbus_assert (d->hash_entry != NULL);
1034 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1035 d->pending_activation->service_name,
1036 d->pending_activation->timeout_added);
1038 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1040 d->pending_activation->service_name, d->pending_activation);
1042 bus_pending_activation_ref (d->pending_activation);
1044 d->hash_entry = NULL;
1048 free_pending_restore_data (void *data)
1050 RestorePendingData *d = data;
1053 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1056 bus_pending_activation_unref (d->pending_activation);
1062 add_restore_pending_to_transaction (BusTransaction *transaction,
1063 BusPendingActivation *pending_activation)
1065 RestorePendingData *d;
1067 d = dbus_new (RestorePendingData, 1);
1071 d->pending_activation = pending_activation;
1072 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1074 bus_pending_activation_ref (d->pending_activation);
1076 if (d->hash_entry == NULL ||
1077 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1078 free_pending_restore_data))
1080 free_pending_restore_data (d);
1084 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1090 bus_activation_service_created (BusActivation *activation,
1091 const char *service_name,
1092 BusTransaction *transaction,
1095 BusPendingActivation *pending_activation;
1096 DBusMessage *message;
1099 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1101 /* Check if it's a pending activation */
1102 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1104 if (!pending_activation)
1107 link = _dbus_list_get_first_link (&pending_activation->entries);
1108 while (link != NULL)
1110 BusPendingActivationEntry *entry = link->data;
1111 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1113 if (dbus_connection_get_is_connected (entry->connection))
1115 /* Only send activation replies to regular activation requests. */
1116 if (!entry->auto_activation)
1118 dbus_uint32_t result;
1120 message = dbus_message_new_method_return (entry->activation_message);
1123 BUS_SET_OOM (error);
1127 result = DBUS_START_REPLY_SUCCESS;
1129 if (!dbus_message_append_args (message,
1130 DBUS_TYPE_UINT32, &result,
1133 dbus_message_unref (message);
1134 BUS_SET_OOM (error);
1138 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1140 dbus_message_unref (message);
1141 BUS_SET_OOM (error);
1145 dbus_message_unref (message);
1159 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1160 BusService *service,
1161 BusTransaction *transaction,
1164 BusPendingActivation *pending_activation;
1167 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1169 /* Check if it's a pending activation */
1170 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1171 bus_service_get_name (service));
1173 if (!pending_activation)
1176 link = _dbus_list_get_first_link (&pending_activation->entries);
1177 while (link != NULL)
1179 BusPendingActivationEntry *entry = link->data;
1180 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1182 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1184 DBusConnection *addressed_recipient;
1186 addressed_recipient = bus_service_get_primary_owners_connection (service);
1188 /* Resume dispatching where we left off in bus_dispatch() */
1189 if (!bus_dispatch_matches (transaction,
1191 addressed_recipient,
1192 entry->activation_message, error))
1199 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1201 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1202 BUS_SET_OOM (error);
1206 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1215 * FIXME @todo the error messages here would ideally be preallocated
1216 * so we don't need to allocate memory to send them.
1217 * Using the usual tactic, prealloc an OOM message, then
1218 * if we can't alloc the real error send the OOM error instead.
1221 try_send_activation_failure (BusPendingActivation *pending_activation,
1222 const DBusError *how)
1224 BusActivation *activation;
1226 BusTransaction *transaction;
1228 activation = pending_activation->activation;
1230 transaction = bus_transaction_new (activation->context);
1231 if (transaction == NULL)
1234 link = _dbus_list_get_first_link (&pending_activation->entries);
1235 while (link != NULL)
1237 BusPendingActivationEntry *entry = link->data;
1238 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1240 if (dbus_connection_get_is_connected (entry->connection))
1242 if (!bus_transaction_send_error_reply (transaction,
1245 entry->activation_message))
1252 bus_transaction_execute_and_free (transaction);
1258 bus_transaction_cancel_and_free (transaction);
1263 * Free the pending activation and send an error message to all the
1264 * connections that were waiting for it.
1267 pending_activation_failed (BusPendingActivation *pending_activation,
1268 const DBusError *how)
1270 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1271 while (!try_send_activation_failure (pending_activation, how))
1272 _dbus_wait_for_memory ();
1274 /* Destroy this pending activation */
1275 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1276 pending_activation->service_name);
1280 * Depending on the exit code of the helper, set the error accordingly
1283 handle_servicehelper_exit_error (int exit_code,
1288 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1289 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1290 "Launcher could not run (out of memory)");
1292 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1293 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1294 "Failed to setup environment correctly");
1296 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1297 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1298 "Bus name is not valid or missing");
1300 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1301 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1302 "Bus name not found in system service directory");
1304 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1305 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1306 "The permission of the setuid helper is not correct");
1308 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1309 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1310 "The service file is incorrect or does not have all required attributes");
1312 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1313 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1314 "Cannot launch daemon, file not found or permissions invalid");
1316 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1317 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1318 "Invalid arguments to command line");
1320 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1321 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1322 "Launched child was signaled, it probably crashed");
1325 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1326 "Launch helper exited with unknown return code %i", exit_code);
1332 babysitter_watch_callback (DBusWatch *watch,
1333 unsigned int condition,
1336 BusPendingActivation *pending_activation = data;
1338 DBusBabysitter *babysitter;
1339 dbus_bool_t uses_servicehelper;
1341 babysitter = pending_activation->babysitter;
1343 _dbus_babysitter_ref (babysitter);
1345 retval = dbus_watch_handle (watch, condition);
1347 /* There are two major cases here; are we the system bus or the session? Here this
1348 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1349 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1351 * In both cases though, just ignore when a process exits with status 0; it's possible for
1352 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1353 * condition between this code and the child process claiming the bus name.
1355 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1357 /* FIXME this is broken in the same way that
1358 * connection watches used to be; there should be
1359 * a separate callback for status change, instead
1360 * of doing "if we handled a watch status might
1363 * Fixing this lets us move dbus_watch_handle
1364 * calls into dbus-mainloop.c
1366 if (_dbus_babysitter_get_child_exited (babysitter))
1370 dbus_bool_t activation_failed;
1373 dbus_error_init (&error);
1375 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1377 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1379 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1380 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1382 activation_failed = exit_code != 0;
1384 dbus_error_free(&error);
1386 if (activation_failed)
1388 if (uses_servicehelper)
1389 handle_servicehelper_exit_error (exit_code, &error);
1391 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1396 activation_failed = TRUE;
1399 if (activation_failed)
1401 /* Destroy all pending activations with the same exec */
1402 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1404 while (_dbus_hash_iter_next (&iter))
1406 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1408 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1409 pending_activation_failed (p, &error);
1412 /* Destroys the pending activation */
1413 pending_activation_failed (pending_activation, &error);
1415 dbus_error_free (&error);
1419 _dbus_babysitter_unref (babysitter);
1425 add_babysitter_watch (DBusWatch *watch,
1428 BusPendingActivation *pending_activation = data;
1430 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1431 watch, babysitter_watch_callback, pending_activation,
1436 remove_babysitter_watch (DBusWatch *watch,
1439 BusPendingActivation *pending_activation = data;
1441 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1442 watch, babysitter_watch_callback, pending_activation);
1446 pending_activation_timed_out (void *data)
1448 BusPendingActivation *pending_activation = data;
1451 /* Kill the spawned process, since it sucks
1452 * (not sure this is what we want to do, but
1453 * may as well try it for now)
1455 if (pending_activation->babysitter)
1456 _dbus_babysitter_kill_child (pending_activation->babysitter);
1458 dbus_error_init (&error);
1460 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1461 "Activation of %s timed out",
1462 pending_activation->service_name);
1464 pending_activation_failed (pending_activation, &error);
1466 dbus_error_free (&error);
1472 cancel_pending (void *data)
1474 BusPendingActivation *pending_activation = data;
1476 _dbus_verbose ("Canceling pending activation of %s\n",
1477 pending_activation->service_name);
1479 if (pending_activation->babysitter)
1480 _dbus_babysitter_kill_child (pending_activation->babysitter);
1482 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1483 pending_activation->service_name);
1487 free_pending_cancel_data (void *data)
1489 BusPendingActivation *pending_activation = data;
1491 bus_pending_activation_unref (pending_activation);
1495 add_cancel_pending_to_transaction (BusTransaction *transaction,
1496 BusPendingActivation *pending_activation)
1498 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1500 free_pending_cancel_data))
1503 bus_pending_activation_ref (pending_activation);
1505 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1511 update_service_cache (BusActivation *activation, DBusError *error)
1515 _dbus_hash_iter_init (activation->directories, &iter);
1516 while (_dbus_hash_iter_next (&iter))
1518 DBusError tmp_error;
1519 BusServiceDirectory *s_dir;
1521 s_dir = _dbus_hash_iter_get_value (&iter);
1523 dbus_error_init (&tmp_error);
1524 if (!update_directory (activation, s_dir, &tmp_error))
1526 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1528 dbus_move_error (&tmp_error, error);
1532 dbus_error_free (&tmp_error);
1540 static BusActivationEntry *
1541 activation_find_entry (BusActivation *activation,
1542 const char *service_name,
1545 BusActivationEntry *entry;
1547 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1550 if (!update_service_cache (activation, error))
1553 entry = _dbus_hash_table_lookup_string (activation->entries,
1558 BusActivationEntry *updated_entry;
1560 if (!check_service_file (activation, entry, &updated_entry, error))
1563 entry = updated_entry;
1568 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1569 "The name %s was not provided by any .service files",
1578 bus_activation_get_environment (BusActivation *activation)
1585 length = _dbus_hash_table_get_n_entries (activation->environment);
1587 environment = dbus_new0 (char *, length + 1);
1589 if (environment == NULL)
1593 _dbus_hash_iter_init (activation->environment, &iter);
1595 if (!_dbus_string_init (&entry))
1597 dbus_free_string_array (environment);
1601 while (_dbus_hash_iter_next (&iter))
1603 const char *key, *value;
1605 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1606 value = (const char *) _dbus_hash_iter_get_value (&iter);
1608 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1611 if (!_dbus_string_steal_data (&entry, environment + i))
1616 _dbus_string_free (&entry);
1620 dbus_free_string_array (environment);
1628 bus_activation_set_environment_variable (BusActivation *activation,
1640 hash_key = _dbus_strdup (key);
1642 if (hash_key == NULL)
1645 hash_value = _dbus_strdup (value);
1647 if (hash_value == NULL)
1650 if (!_dbus_hash_table_insert_string (activation->environment,
1651 hash_key, hash_value))
1656 if (retval == FALSE)
1658 dbus_free (hash_key);
1659 dbus_free (hash_value);
1660 BUS_SET_OOM (error);
1667 bus_activation_activate_service (BusActivation *activation,
1668 DBusConnection *connection,
1669 BusTransaction *transaction,
1670 dbus_bool_t auto_activation,
1671 DBusMessage *activation_message,
1672 const char *service_name,
1675 BusActivationEntry *entry;
1676 BusPendingActivation *pending_activation;
1677 BusPendingActivationEntry *pending_activation_entry;
1678 DBusMessage *message;
1679 DBusString service_str;
1680 const char *servicehelper;
1686 dbus_bool_t activated;
1691 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1693 if (activation->n_pending_activations >=
1694 bus_context_get_max_pending_activations (activation->context))
1696 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1697 "The maximum number of pending activations has been reached, activation of %s failed",
1702 entry = activation_find_entry (activation, service_name, error);
1706 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1707 * call us if the service is already active.
1709 if (!auto_activation)
1711 /* Check if the service is active */
1712 _dbus_string_init_const (&service_str, service_name);
1713 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1715 dbus_uint32_t result;
1717 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1719 message = dbus_message_new_method_return (activation_message);
1723 _dbus_verbose ("No memory to create reply to activate message\n");
1724 BUS_SET_OOM (error);
1728 result = DBUS_START_REPLY_ALREADY_RUNNING;
1730 if (!dbus_message_append_args (message,
1731 DBUS_TYPE_UINT32, &result,
1734 _dbus_verbose ("No memory to set args of reply to activate message\n");
1735 BUS_SET_OOM (error);
1736 dbus_message_unref (message);
1740 retval = bus_transaction_send_from_driver (transaction, connection, message);
1741 dbus_message_unref (message);
1744 _dbus_verbose ("Failed to send reply\n");
1745 BUS_SET_OOM (error);
1752 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1753 if (!pending_activation_entry)
1755 _dbus_verbose ("Failed to create pending activation entry\n");
1756 BUS_SET_OOM (error);
1760 pending_activation_entry->auto_activation = auto_activation;
1762 pending_activation_entry->activation_message = activation_message;
1763 dbus_message_ref (activation_message);
1764 pending_activation_entry->connection = connection;
1765 dbus_connection_ref (connection);
1767 /* Check if the service is being activated */
1768 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1769 if (pending_activation)
1771 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1773 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1775 BUS_SET_OOM (error);
1776 bus_pending_activation_entry_free (pending_activation_entry);
1780 pending_activation->n_entries += 1;
1781 pending_activation->activation->n_pending_activations += 1;
1785 pending_activation = dbus_new0 (BusPendingActivation, 1);
1786 if (!pending_activation)
1788 _dbus_verbose ("Failed to create pending activation\n");
1790 BUS_SET_OOM (error);
1791 bus_pending_activation_entry_free (pending_activation_entry);
1795 pending_activation->activation = activation;
1796 pending_activation->refcount = 1;
1798 pending_activation->service_name = _dbus_strdup (service_name);
1799 if (!pending_activation->service_name)
1801 _dbus_verbose ("Failed to copy service name for pending activation\n");
1803 BUS_SET_OOM (error);
1804 bus_pending_activation_unref (pending_activation);
1805 bus_pending_activation_entry_free (pending_activation_entry);
1809 pending_activation->exec = _dbus_strdup (entry->exec);
1810 if (!pending_activation->exec)
1812 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1813 BUS_SET_OOM (error);
1814 bus_pending_activation_unref (pending_activation);
1815 bus_pending_activation_entry_free (pending_activation_entry);
1819 if (entry->systemd_service)
1821 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1822 if (!pending_activation->systemd_service)
1824 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1825 BUS_SET_OOM (error);
1826 bus_pending_activation_unref (pending_activation);
1827 bus_pending_activation_entry_free (pending_activation_entry);
1832 pending_activation->timeout =
1833 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1834 pending_activation_timed_out,
1837 if (!pending_activation->timeout)
1839 _dbus_verbose ("Failed to create timeout for pending activation\n");
1841 BUS_SET_OOM (error);
1842 bus_pending_activation_unref (pending_activation);
1843 bus_pending_activation_entry_free (pending_activation_entry);
1847 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1848 pending_activation->timeout,
1849 handle_timeout_callback,
1853 _dbus_verbose ("Failed to add timeout for pending activation\n");
1855 BUS_SET_OOM (error);
1856 bus_pending_activation_unref (pending_activation);
1857 bus_pending_activation_entry_free (pending_activation_entry);
1861 pending_activation->timeout_added = TRUE;
1863 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1865 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1867 BUS_SET_OOM (error);
1868 bus_pending_activation_unref (pending_activation);
1869 bus_pending_activation_entry_free (pending_activation_entry);
1873 pending_activation->n_entries += 1;
1874 pending_activation->activation->n_pending_activations += 1;
1877 _dbus_hash_iter_init (activation->pending_activations, &iter);
1878 while (_dbus_hash_iter_next (&iter))
1880 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1882 if (strcmp (p->exec, entry->exec) == 0)
1889 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1890 pending_activation->service_name,
1891 pending_activation))
1893 _dbus_verbose ("Failed to put pending activation in hash table\n");
1895 BUS_SET_OOM (error);
1896 bus_pending_activation_unref (pending_activation);
1901 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1903 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1904 BUS_SET_OOM (error);
1905 _dbus_hash_table_remove_string (activation->pending_activations,
1906 pending_activation->service_name);
1914 if (bus_context_get_systemd_activation (activation->context))
1916 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1917 /* systemd itself is missing apparently. That can happen
1918 only during early startup. Let's just wait until systemd
1919 connects to us and do nothing. */
1922 if (entry->systemd_service)
1924 BusTransaction *activation_transaction;
1925 DBusString service_string;
1926 BusService *service;
1927 BusRegistry *registry;
1929 /* OK, we have a systemd service configured for this entry,
1930 hence let's enqueue an activation request message. This
1931 is implemented as a directed signal, not a method call,
1932 for three reasons: 1) we don't expect a response on
1933 success, where we just expect a name appearing on the
1934 bus; 2) at this time the systemd service might not yet
1935 have connected, so we wouldn't know the message serial at
1936 this point to set up a pending call; 3) it is ugly if the
1937 bus suddenly becomes the caller of a remote method. */
1939 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1940 "org.freedesktop.systemd1.Activator",
1941 "ActivationRequest");
1944 _dbus_verbose ("No memory to create activation message\n");
1945 BUS_SET_OOM (error);
1949 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1950 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1951 !dbus_message_append_args (message,
1952 DBUS_TYPE_STRING, &entry->systemd_service,
1955 _dbus_verbose ("No memory to set args of activation message\n");
1956 dbus_message_unref (message);
1957 BUS_SET_OOM (error);
1961 /* Create our transaction */
1962 activation_transaction = bus_transaction_new (activation->context);
1963 if (activation_transaction == NULL)
1965 _dbus_verbose ("No memory to create activation transaction\n");
1966 dbus_message_unref (message);
1967 BUS_SET_OOM (error);
1971 /* Check whether systemd is already connected */
1972 registry = bus_connection_get_registry (connection);
1973 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1974 service = bus_registry_lookup (registry, &service_string);
1976 if (service != NULL)
1977 /* Wonderful, systemd is connected, let's just send the msg */
1978 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1981 /* systemd is not around, let's "activate" it. */
1982 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1983 message, "org.freedesktop.systemd1", error);
1985 dbus_message_unref (message);
1989 _DBUS_ASSERT_ERROR_IS_SET (error);
1990 _dbus_verbose ("failed to send activation message: %s\n", error->name);
1991 bus_transaction_cancel_and_free (activation_transaction);
1995 bus_transaction_execute_and_free (activation_transaction);
1999 /* OK, we have no configured systemd service, hence let's
2000 proceed with traditional activation. */
2003 /* use command as system and session different */
2004 if (!_dbus_string_init (&command))
2006 BUS_SET_OOM (error);
2010 /* does the bus use a helper? */
2011 servicehelper = bus_context_get_servicehelper (activation->context);
2012 if (servicehelper != NULL)
2014 if (entry->user == NULL)
2016 _dbus_string_free (&command);
2017 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2018 "Cannot do system-bus activation with no user\n");
2022 /* join the helper path and the service name */
2023 if (!_dbus_string_append (&command, servicehelper))
2025 _dbus_string_free (&command);
2026 BUS_SET_OOM (error);
2029 if (!_dbus_string_append (&command, " "))
2031 _dbus_string_free (&command);
2032 BUS_SET_OOM (error);
2035 if (!_dbus_string_append (&command, service_name))
2037 _dbus_string_free (&command);
2038 BUS_SET_OOM (error);
2044 /* the bus does not use a helper, so we can append arguments with the exec line */
2045 if (!_dbus_string_append (&command, entry->exec))
2047 _dbus_string_free (&command);
2048 BUS_SET_OOM (error);
2053 /* convert command into arguments */
2054 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2056 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2057 _DBUS_ASSERT_ERROR_IS_SET (error);
2059 _dbus_hash_table_remove_string (activation->pending_activations,
2060 pending_activation->service_name);
2062 _dbus_string_free (&command);
2065 _dbus_string_free (&command);
2067 if (!add_bus_environment (activation, error))
2069 _DBUS_ASSERT_ERROR_IS_SET (error);
2070 dbus_free_string_array (argv);
2074 envp = bus_activation_get_environment (activation);
2078 BUS_SET_OOM (error);
2079 dbus_free_string_array (argv);
2083 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2084 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2089 _dbus_verbose ("Failed to spawn child\n");
2090 _DBUS_ASSERT_ERROR_IS_SET (error);
2091 dbus_free_string_array (argv);
2092 dbus_free_string_array (envp);
2097 dbus_free_string_array (argv);
2100 _dbus_assert (pending_activation->babysitter != NULL);
2102 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2103 add_babysitter_watch,
2104 remove_babysitter_watch,
2109 BUS_SET_OOM (error);
2110 _dbus_verbose ("Failed to set babysitter watch functions\n");
2118 bus_activation_list_services (BusActivation *activation,
2126 len = _dbus_hash_table_get_n_entries (activation->entries);
2127 retval = dbus_new (char *, len + 1);
2132 _dbus_hash_iter_init (activation->entries, &iter);
2134 while (_dbus_hash_iter_next (&iter))
2136 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2138 retval[i] = _dbus_strdup (entry->name);
2139 if (retval[i] == NULL)
2154 for (j = 0; j < i; j++)
2155 dbus_free (retval[i]);
2162 dbus_activation_systemd_failure (BusActivation *activation,
2163 DBusMessage *message)
2166 const char *code, *str, *unit = NULL;
2168 dbus_error_init(&error);
2170 /* This is called whenever the systemd activator sent us a
2171 response. We'll invalidate all pending activations that match the
2174 if (dbus_message_get_args (message, &error,
2175 DBUS_TYPE_STRING, &unit,
2176 DBUS_TYPE_STRING, &code,
2177 DBUS_TYPE_STRING, &str,
2179 dbus_set_error(&error, code, str);
2185 _dbus_hash_iter_init (activation->pending_activations,
2188 while (_dbus_hash_iter_next (&iter))
2190 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2192 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2193 pending_activation_failed(p, &error);
2197 dbus_error_free(&error);
2202 #ifdef DBUS_BUILD_TESTS
2206 #define SERVICE_NAME_1 "MyService1"
2207 #define SERVICE_NAME_2 "MyService2"
2208 #define SERVICE_NAME_3 "MyService3"
2210 #define SERVICE_FILE_1 "service-1.service"
2211 #define SERVICE_FILE_2 "service-2.service"
2212 #define SERVICE_FILE_3 "service-3.service"
2215 test_create_service_file (DBusString *dir,
2216 const char *filename,
2220 DBusString file_name, full_path;
2222 dbus_bool_t ret_val;
2225 _dbus_string_init_const (&file_name, filename);
2227 if (!_dbus_string_init (&full_path))
2230 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2231 !_dbus_concat_dir_and_file (&full_path, &file_name))
2237 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2244 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2248 _dbus_string_free (&full_path);
2253 test_remove_service_file (DBusString *dir, const char *filename)
2255 DBusString file_name, full_path;
2256 dbus_bool_t ret_val;
2260 _dbus_string_init_const (&file_name, filename);
2262 if (!_dbus_string_init (&full_path))
2265 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2266 !_dbus_concat_dir_and_file (&full_path, &file_name))
2272 if (!_dbus_delete_file (&full_path, NULL))
2279 _dbus_string_free (&full_path);
2284 test_remove_directory (DBusString *dir)
2287 DBusString filename, full_path;
2288 dbus_bool_t ret_val;
2292 if (!_dbus_string_init (&filename))
2295 if (!_dbus_string_init (&full_path))
2297 _dbus_string_free (&filename);
2301 iter = _dbus_directory_open (dir, NULL);
2308 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2310 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2316 _dbus_directory_close (iter);
2318 if (!_dbus_delete_directory (dir, NULL))
2325 _dbus_string_free (&filename);
2326 _dbus_string_free (&full_path);
2332 init_service_reload_test (DBusString *dir)
2336 if (!_dbus_stat (dir, &stat_buf, NULL))
2338 if (!_dbus_create_directory (dir, NULL))
2343 if (!test_remove_directory (dir))
2346 if (!_dbus_create_directory (dir, NULL))
2350 /* Create one initial file */
2351 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2358 cleanup_service_reload_test (DBusString *dir)
2360 if (!test_remove_directory (dir))
2368 BusActivation *activation;
2369 const char *service_name;
2370 dbus_bool_t expecting_find;
2374 check_func (void *data)
2377 BusActivationEntry *entry;
2379 dbus_bool_t ret_val;
2384 dbus_error_init (&error);
2386 entry = activation_find_entry (d->activation, d->service_name, &error);
2389 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2395 if (d->expecting_find)
2399 dbus_error_free (&error);
2403 if (!d->expecting_find)
2411 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2416 err = !_dbus_test_oom_handling (description, check_func, data);
2418 err = !check_func (data);
2421 _dbus_assert_not_reached ("Test failed");
2427 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2429 BusActivation *activation;
2431 DBusList *directories;
2435 _dbus_string_init_const (&address, "");
2437 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2440 activation = bus_activation_new (NULL, &address, &directories, NULL);
2444 d.activation = activation;
2446 /* Check for existing service file */
2447 d.expecting_find = TRUE;
2448 d.service_name = SERVICE_NAME_1;
2450 if (!do_test ("Existing service file", oom_test, &d))
2453 /* Check for non-existing service file */
2454 d.expecting_find = FALSE;
2455 d.service_name = SERVICE_NAME_3;
2457 if (!do_test ("Nonexisting service file", oom_test, &d))
2460 /* Check for added service file */
2461 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2464 d.expecting_find = TRUE;
2465 d.service_name = SERVICE_NAME_2;
2467 if (!do_test ("Added service file", oom_test, &d))
2470 /* Check for removed service file */
2471 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2474 d.expecting_find = FALSE;
2475 d.service_name = SERVICE_FILE_2;
2477 if (!do_test ("Removed service file", oom_test, &d))
2480 /* Check for updated service file */
2482 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2484 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2487 d.expecting_find = TRUE;
2488 d.service_name = SERVICE_NAME_3;
2490 if (!do_test ("Updated service file, part 1", oom_test, &d))
2493 d.expecting_find = FALSE;
2494 d.service_name = SERVICE_NAME_1;
2496 if (!do_test ("Updated service file, part 2", oom_test, &d))
2499 bus_activation_unref (activation);
2500 _dbus_list_clear (&directories);
2506 bus_activation_service_reload_test (const DBusString *test_data_dir)
2508 DBusString directory;
2510 if (!_dbus_string_init (&directory))
2513 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2516 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2517 !_dbus_generate_random_ascii (&directory, 6))
2522 /* Do normal tests */
2523 if (!init_service_reload_test (&directory))
2524 _dbus_assert_not_reached ("could not initiate service reload test");
2526 if (!do_service_reload_test (&directory, FALSE))
2530 if (!init_service_reload_test (&directory))
2531 _dbus_assert_not_reached ("could not initiate service reload test");
2533 if (!do_service_reload_test (&directory, TRUE))
2536 /* Cleanup test directory */
2537 if (!cleanup_service_reload_test (&directory))
2540 _dbus_string_free (&directory);
2545 #endif /* DBUS_BUILD_TESTS */