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 dbus_free (entry->name);
436 dbus_free (entry->exec);
437 dbus_free (entry->user);
438 dbus_free (entry->systemd_service);
439 entry->systemd_service = systemd_service;
443 /* ownership has been transferred to entry, do not free separately */
447 systemd_service = NULL;
449 if (!_dbus_hash_table_insert_string (activation->entries,
450 entry->name, bus_activation_entry_ref(entry)))
453 /* Also remove path to entries hash since we want this in sync with
454 * the entries hash table */
455 _dbus_hash_table_remove_string (entry->s_dir->entries,
457 bus_activation_entry_unref (entry);
462 entry->mtime = stat_buf.mtime;
466 /* if these have been transferred into entry, the variables will be NULL */
470 dbus_free (systemd_service);
471 _dbus_string_free (&file_path);
474 bus_activation_entry_unref (entry);
480 check_service_file (BusActivation *activation,
481 BusActivationEntry *entry,
482 BusActivationEntry **updated_entry,
487 BusActivationEntry *tmp_entry;
488 DBusString file_path;
494 _dbus_string_init_const (&filename, entry->filename);
496 if (!_dbus_string_init (&file_path))
502 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
503 !_dbus_concat_dir_and_file (&file_path, &filename))
510 if (!_dbus_stat (&file_path, &stat_buf, NULL))
512 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
513 _dbus_string_get_const_data (&file_path));
515 _dbus_hash_table_remove_string (activation->entries, entry->name);
516 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
524 if (stat_buf.mtime > entry->mtime)
526 BusDesktopFile *desktop_file;
529 dbus_error_init (&tmp_error);
531 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
532 if (desktop_file == NULL)
534 _dbus_verbose ("Could not load %s: %s\n",
535 _dbus_string_get_const_data (&file_path),
537 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
539 dbus_move_error (&tmp_error, error);
543 dbus_error_free (&tmp_error);
548 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
549 * Handle these both better
551 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
553 bus_desktop_file_free (desktop_file);
554 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
556 dbus_move_error (&tmp_error, error);
560 dbus_error_free (&tmp_error);
565 bus_desktop_file_free (desktop_file);
571 _dbus_string_free (&file_path);
573 if (updated_entry != NULL)
574 *updated_entry = tmp_entry;
579 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
580 * hash entries it already added.
583 update_directory (BusActivation *activation,
584 BusServiceDirectory *s_dir,
588 DBusString dir, filename;
589 BusDesktopFile *desktop_file;
592 BusActivationEntry *entry;
593 DBusString full_path;
595 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
600 _dbus_string_init_const (&dir, s_dir->dir_c);
602 if (!_dbus_string_init (&filename))
608 if (!_dbus_string_init (&full_path))
611 _dbus_string_free (&filename);
617 /* from this point it's safe to "goto out" */
619 iter = _dbus_directory_open (&dir, error);
622 _dbus_verbose ("Failed to open directory %s: %s\n",
624 error ? error->message : "unknown");
628 /* Now read the files */
629 dbus_error_init (&tmp_error);
630 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
632 _dbus_assert (!dbus_error_is_set (&tmp_error));
634 _dbus_string_set_length (&full_path, 0);
636 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
638 _dbus_verbose ("Skipping non-.service file %s\n",
639 _dbus_string_get_const_data (&filename));
643 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
644 if (entry) /* Already has this service file in the cache */
646 if (!check_service_file (activation, entry, NULL, error))
652 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
653 !_dbus_concat_dir_and_file (&full_path, &filename))
660 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
661 if (desktop_file == NULL)
663 _dbus_verbose ("Could not load %s: %s\n",
664 _dbus_string_get_const_data (&full_path),
667 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
669 dbus_move_error (&tmp_error, error);
673 dbus_error_free (&tmp_error);
677 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
678 * Handle these both better
680 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
682 bus_desktop_file_free (desktop_file);
685 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
686 _dbus_string_get_const_data (&full_path), tmp_error.message);
688 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
690 dbus_move_error (&tmp_error, error);
694 dbus_error_free (&tmp_error);
699 bus_desktop_file_free (desktop_file);
705 if (dbus_error_is_set (&tmp_error))
707 dbus_move_error (&tmp_error, error);
715 _DBUS_ASSERT_ERROR_IS_SET (error);
717 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
720 _dbus_directory_close (iter);
721 _dbus_string_free (&filename);
722 _dbus_string_free (&full_path);
728 populate_environment (BusActivation *activation)
734 dbus_bool_t retval = FALSE;
736 environment = _dbus_get_environment ();
738 if (environment == NULL)
741 if (!_dbus_string_init (&key))
743 dbus_free_string_array (environment);
747 if (!_dbus_string_init (&value))
749 _dbus_string_free (&key);
750 dbus_free_string_array (environment);
754 for (i = 0; environment[i] != NULL; i++)
756 if (!_dbus_string_append (&key, environment[i]))
759 if (_dbus_string_split_on_byte (&key, '=', &value))
761 char *hash_key, *hash_value;
763 if (!_dbus_string_steal_data (&key, &hash_key))
766 if (!_dbus_string_steal_data (&value, &hash_value))
769 if (!_dbus_hash_table_insert_string (activation->environment,
770 hash_key, hash_value))
773 _dbus_string_set_length (&key, 0);
774 _dbus_string_set_length (&value, 0);
777 if (environment[i] != NULL)
783 _dbus_string_free (&key);
784 _dbus_string_free (&value);
785 dbus_free_string_array (environment);
791 bus_activation_reload (BusActivation *activation,
792 const DBusString *address,
793 DBusList **directories,
799 if (activation->server_address != NULL)
800 dbus_free (activation->server_address);
801 if (!_dbus_string_copy_data (address, &activation->server_address))
807 if (activation->entries != NULL)
808 _dbus_hash_table_unref (activation->entries);
809 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
810 (DBusFreeFunction)bus_activation_entry_unref);
811 if (activation->entries == NULL)
817 if (activation->directories != NULL)
818 _dbus_hash_table_unref (activation->directories);
819 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
820 (DBusFreeFunction)bus_service_directory_unref);
822 if (activation->directories == NULL)
828 link = _dbus_list_get_first_link (directories);
831 BusServiceDirectory *s_dir;
833 dir = _dbus_strdup ((const char *) link->data);
840 s_dir = dbus_new0 (BusServiceDirectory, 1);
851 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
852 (DBusFreeFunction)bus_activation_entry_unref);
856 bus_service_directory_unref (s_dir);
861 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
863 bus_service_directory_unref (s_dir);
868 /* only fail on OOM, it is ok if we can't read the directory */
869 if (!update_directory (activation, s_dir, error))
871 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
874 dbus_error_free (error);
877 link = _dbus_list_get_next_link (directories, link);
886 bus_activation_new (BusContext *context,
887 const DBusString *address,
888 DBusList **directories,
891 BusActivation *activation;
895 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
897 activation = dbus_new0 (BusActivation, 1);
898 if (activation == NULL)
904 activation->refcount = 1;
905 activation->context = context;
906 activation->n_pending_activations = 0;
908 if (!bus_activation_reload (activation, address, directories, error))
911 /* Initialize this hash table once, we don't want to lose pending
912 * activations on reload. */
913 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
914 (DBusFreeFunction)bus_pending_activation_unref);
916 if (activation->pending_activations == NULL)
922 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
923 (DBusFreeFunction) dbus_free,
924 (DBusFreeFunction) dbus_free);
926 if (activation->environment == NULL)
932 if (!populate_environment (activation))
941 bus_activation_unref (activation);
946 bus_activation_ref (BusActivation *activation)
948 _dbus_assert (activation->refcount > 0);
950 activation->refcount += 1;
956 bus_activation_unref (BusActivation *activation)
958 _dbus_assert (activation->refcount > 0);
960 activation->refcount -= 1;
962 if (activation->refcount > 0)
965 dbus_free (activation->server_address);
966 if (activation->entries)
967 _dbus_hash_table_unref (activation->entries);
968 if (activation->pending_activations)
969 _dbus_hash_table_unref (activation->pending_activations);
970 if (activation->directories)
971 _dbus_hash_table_unref (activation->directories);
972 if (activation->environment)
973 _dbus_hash_table_unref (activation->environment);
975 dbus_free (activation);
979 add_bus_environment (BusActivation *activation,
984 if (!bus_activation_set_environment_variable (activation,
985 "DBUS_STARTER_ADDRESS",
986 activation->server_address,
990 type = bus_context_get_type (activation->context);
993 if (!bus_activation_set_environment_variable (activation,
994 "DBUS_STARTER_BUS_TYPE", type,
998 if (strcmp (type, "session") == 0)
1000 if (!bus_activation_set_environment_variable (activation,
1001 "DBUS_SESSION_BUS_ADDRESS",
1002 activation->server_address,
1006 else if (strcmp (type, "system") == 0)
1008 if (!bus_activation_set_environment_variable (activation,
1009 "DBUS_SYSTEM_BUS_ADDRESS",
1010 activation->server_address,
1021 BusPendingActivation *pending_activation;
1022 DBusPreallocatedHash *hash_entry;
1023 } RestorePendingData;
1026 restore_pending (void *data)
1028 RestorePendingData *d = data;
1030 _dbus_assert (d->pending_activation != NULL);
1031 _dbus_assert (d->hash_entry != NULL);
1033 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1034 d->pending_activation->service_name,
1035 d->pending_activation->timeout_added);
1037 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1039 d->pending_activation->service_name, d->pending_activation);
1041 bus_pending_activation_ref (d->pending_activation);
1043 d->hash_entry = NULL;
1047 free_pending_restore_data (void *data)
1049 RestorePendingData *d = data;
1052 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1055 bus_pending_activation_unref (d->pending_activation);
1061 add_restore_pending_to_transaction (BusTransaction *transaction,
1062 BusPendingActivation *pending_activation)
1064 RestorePendingData *d;
1066 d = dbus_new (RestorePendingData, 1);
1070 d->pending_activation = pending_activation;
1071 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1073 bus_pending_activation_ref (d->pending_activation);
1075 if (d->hash_entry == NULL ||
1076 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1077 free_pending_restore_data))
1079 free_pending_restore_data (d);
1083 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1089 bus_activation_service_created (BusActivation *activation,
1090 const char *service_name,
1091 BusTransaction *transaction,
1094 BusPendingActivation *pending_activation;
1095 DBusMessage *message;
1098 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1100 /* Check if it's a pending activation */
1101 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1103 if (!pending_activation)
1106 link = _dbus_list_get_first_link (&pending_activation->entries);
1107 while (link != NULL)
1109 BusPendingActivationEntry *entry = link->data;
1110 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1112 if (dbus_connection_get_is_connected (entry->connection))
1114 /* Only send activation replies to regular activation requests. */
1115 if (!entry->auto_activation)
1117 dbus_uint32_t result;
1119 message = dbus_message_new_method_return (entry->activation_message);
1122 BUS_SET_OOM (error);
1126 result = DBUS_START_REPLY_SUCCESS;
1128 if (!dbus_message_append_args (message,
1129 DBUS_TYPE_UINT32, &result,
1132 dbus_message_unref (message);
1133 BUS_SET_OOM (error);
1137 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1139 dbus_message_unref (message);
1140 BUS_SET_OOM (error);
1144 dbus_message_unref (message);
1158 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1159 BusService *service,
1160 BusTransaction *transaction,
1163 BusPendingActivation *pending_activation;
1166 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1168 /* Check if it's a pending activation */
1169 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1170 bus_service_get_name (service));
1172 if (!pending_activation)
1175 link = _dbus_list_get_first_link (&pending_activation->entries);
1176 while (link != NULL)
1178 BusPendingActivationEntry *entry = link->data;
1179 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1181 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1183 DBusConnection *addressed_recipient;
1185 addressed_recipient = bus_service_get_primary_owners_connection (service);
1187 /* Resume dispatching where we left off in bus_dispatch() */
1188 if (!bus_dispatch_matches (transaction,
1190 addressed_recipient,
1191 entry->activation_message, error))
1198 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1200 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1201 BUS_SET_OOM (error);
1205 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1214 * FIXME @todo the error messages here would ideally be preallocated
1215 * so we don't need to allocate memory to send them.
1216 * Using the usual tactic, prealloc an OOM message, then
1217 * if we can't alloc the real error send the OOM error instead.
1220 try_send_activation_failure (BusPendingActivation *pending_activation,
1221 const DBusError *how)
1223 BusActivation *activation;
1225 BusTransaction *transaction;
1227 activation = pending_activation->activation;
1229 transaction = bus_transaction_new (activation->context);
1230 if (transaction == NULL)
1233 link = _dbus_list_get_first_link (&pending_activation->entries);
1234 while (link != NULL)
1236 BusPendingActivationEntry *entry = link->data;
1237 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1239 if (dbus_connection_get_is_connected (entry->connection))
1241 if (!bus_transaction_send_error_reply (transaction,
1244 entry->activation_message))
1251 bus_transaction_execute_and_free (transaction);
1257 bus_transaction_cancel_and_free (transaction);
1262 * Free the pending activation and send an error message to all the
1263 * connections that were waiting for it.
1266 pending_activation_failed (BusPendingActivation *pending_activation,
1267 const DBusError *how)
1269 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1270 while (!try_send_activation_failure (pending_activation, how))
1271 _dbus_wait_for_memory ();
1273 /* Destroy this pending activation */
1274 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1275 pending_activation->service_name);
1279 * Depending on the exit code of the helper, set the error accordingly
1282 handle_servicehelper_exit_error (int exit_code,
1287 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1288 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1289 "Launcher could not run (out of memory)");
1291 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1292 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1293 "Failed to setup environment correctly");
1295 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1296 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1297 "Bus name is not valid or missing");
1299 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1300 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1301 "Bus name not found in system service directory");
1303 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1304 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1305 "The permission of the setuid helper is not correct");
1307 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1308 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1309 "The service file is incorrect or does not have all required attributes");
1311 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1312 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1313 "Cannot launch daemon, file not found or permissions invalid");
1315 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1316 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1317 "Invalid arguments to command line");
1319 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1320 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1321 "Launched child was signaled, it probably crashed");
1324 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1325 "Launch helper exited with unknown return code %i", exit_code);
1331 babysitter_watch_callback (DBusWatch *watch,
1332 unsigned int condition,
1335 BusPendingActivation *pending_activation = data;
1337 DBusBabysitter *babysitter;
1338 dbus_bool_t uses_servicehelper;
1340 babysitter = pending_activation->babysitter;
1342 _dbus_babysitter_ref (babysitter);
1344 retval = dbus_watch_handle (watch, condition);
1346 /* There are two major cases here; are we the system bus or the session? Here this
1347 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1348 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1350 * In both cases though, just ignore when a process exits with status 0; it's possible for
1351 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1352 * condition between this code and the child process claiming the bus name.
1354 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1356 /* FIXME this is broken in the same way that
1357 * connection watches used to be; there should be
1358 * a separate callback for status change, instead
1359 * of doing "if we handled a watch status might
1362 * Fixing this lets us move dbus_watch_handle
1363 * calls into dbus-mainloop.c
1365 if (_dbus_babysitter_get_child_exited (babysitter))
1369 dbus_bool_t activation_failed;
1372 dbus_error_init (&error);
1374 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1376 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1378 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1379 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1381 activation_failed = exit_code != 0;
1383 dbus_error_free(&error);
1385 if (activation_failed)
1387 if (uses_servicehelper)
1388 handle_servicehelper_exit_error (exit_code, &error);
1390 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1395 activation_failed = TRUE;
1398 if (activation_failed)
1400 /* Destroy all pending activations with the same exec */
1401 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1403 while (_dbus_hash_iter_next (&iter))
1405 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1407 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1408 pending_activation_failed (p, &error);
1411 /* Destroys the pending activation */
1412 pending_activation_failed (pending_activation, &error);
1414 dbus_error_free (&error);
1418 _dbus_babysitter_unref (babysitter);
1424 add_babysitter_watch (DBusWatch *watch,
1427 BusPendingActivation *pending_activation = data;
1429 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1430 watch, babysitter_watch_callback, pending_activation,
1435 remove_babysitter_watch (DBusWatch *watch,
1438 BusPendingActivation *pending_activation = data;
1440 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1441 watch, babysitter_watch_callback, pending_activation);
1445 pending_activation_timed_out (void *data)
1447 BusPendingActivation *pending_activation = data;
1450 /* Kill the spawned process, since it sucks
1451 * (not sure this is what we want to do, but
1452 * may as well try it for now)
1454 if (pending_activation->babysitter)
1455 _dbus_babysitter_kill_child (pending_activation->babysitter);
1457 dbus_error_init (&error);
1459 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1460 "Activation of %s timed out",
1461 pending_activation->service_name);
1463 pending_activation_failed (pending_activation, &error);
1465 dbus_error_free (&error);
1471 cancel_pending (void *data)
1473 BusPendingActivation *pending_activation = data;
1475 _dbus_verbose ("Canceling pending activation of %s\n",
1476 pending_activation->service_name);
1478 if (pending_activation->babysitter)
1479 _dbus_babysitter_kill_child (pending_activation->babysitter);
1481 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1482 pending_activation->service_name);
1486 free_pending_cancel_data (void *data)
1488 BusPendingActivation *pending_activation = data;
1490 bus_pending_activation_unref (pending_activation);
1494 add_cancel_pending_to_transaction (BusTransaction *transaction,
1495 BusPendingActivation *pending_activation)
1497 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1499 free_pending_cancel_data))
1502 bus_pending_activation_ref (pending_activation);
1504 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1510 update_service_cache (BusActivation *activation, DBusError *error)
1514 _dbus_hash_iter_init (activation->directories, &iter);
1515 while (_dbus_hash_iter_next (&iter))
1517 DBusError tmp_error;
1518 BusServiceDirectory *s_dir;
1520 s_dir = _dbus_hash_iter_get_value (&iter);
1522 dbus_error_init (&tmp_error);
1523 if (!update_directory (activation, s_dir, &tmp_error))
1525 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1527 dbus_move_error (&tmp_error, error);
1531 dbus_error_free (&tmp_error);
1539 static BusActivationEntry *
1540 activation_find_entry (BusActivation *activation,
1541 const char *service_name,
1544 BusActivationEntry *entry;
1546 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1549 if (!update_service_cache (activation, error))
1552 entry = _dbus_hash_table_lookup_string (activation->entries,
1557 BusActivationEntry *updated_entry;
1559 if (!check_service_file (activation, entry, &updated_entry, error))
1562 entry = updated_entry;
1567 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1568 "The name %s was not provided by any .service files",
1577 bus_activation_get_environment (BusActivation *activation)
1584 length = _dbus_hash_table_get_n_entries (activation->environment);
1586 environment = dbus_new0 (char *, length + 1);
1588 if (environment == NULL)
1592 _dbus_hash_iter_init (activation->environment, &iter);
1594 if (!_dbus_string_init (&entry))
1596 dbus_free_string_array (environment);
1600 while (_dbus_hash_iter_next (&iter))
1602 const char *key, *value;
1604 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1605 value = (const char *) _dbus_hash_iter_get_value (&iter);
1607 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1610 if (!_dbus_string_steal_data (&entry, environment + i))
1615 _dbus_string_free (&entry);
1619 dbus_free_string_array (environment);
1627 bus_activation_set_environment_variable (BusActivation *activation,
1639 hash_key = _dbus_strdup (key);
1641 if (hash_key == NULL)
1644 hash_value = _dbus_strdup (value);
1646 if (hash_value == NULL)
1649 if (!_dbus_hash_table_insert_string (activation->environment,
1650 hash_key, hash_value))
1655 if (retval == FALSE)
1657 dbus_free (hash_key);
1658 dbus_free (hash_value);
1659 BUS_SET_OOM (error);
1666 bus_activation_activate_service (BusActivation *activation,
1667 DBusConnection *connection,
1668 BusTransaction *transaction,
1669 dbus_bool_t auto_activation,
1670 DBusMessage *activation_message,
1671 const char *service_name,
1674 BusActivationEntry *entry;
1675 BusPendingActivation *pending_activation;
1676 BusPendingActivationEntry *pending_activation_entry;
1677 DBusMessage *message;
1678 DBusString service_str;
1679 const char *servicehelper;
1685 dbus_bool_t activated;
1690 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1692 if (activation->n_pending_activations >=
1693 bus_context_get_max_pending_activations (activation->context))
1695 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1696 "The maximum number of pending activations has been reached, activation of %s failed",
1701 entry = activation_find_entry (activation, service_name, error);
1705 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1706 * call us if the service is already active.
1708 if (!auto_activation)
1710 /* Check if the service is active */
1711 _dbus_string_init_const (&service_str, service_name);
1712 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1714 dbus_uint32_t result;
1716 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1718 message = dbus_message_new_method_return (activation_message);
1722 _dbus_verbose ("No memory to create reply to activate message\n");
1723 BUS_SET_OOM (error);
1727 result = DBUS_START_REPLY_ALREADY_RUNNING;
1729 if (!dbus_message_append_args (message,
1730 DBUS_TYPE_UINT32, &result,
1733 _dbus_verbose ("No memory to set args of reply to activate message\n");
1734 BUS_SET_OOM (error);
1735 dbus_message_unref (message);
1739 retval = bus_transaction_send_from_driver (transaction, connection, message);
1740 dbus_message_unref (message);
1743 _dbus_verbose ("Failed to send reply\n");
1744 BUS_SET_OOM (error);
1751 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1752 if (!pending_activation_entry)
1754 _dbus_verbose ("Failed to create pending activation entry\n");
1755 BUS_SET_OOM (error);
1759 pending_activation_entry->auto_activation = auto_activation;
1761 pending_activation_entry->activation_message = activation_message;
1762 dbus_message_ref (activation_message);
1763 pending_activation_entry->connection = connection;
1764 dbus_connection_ref (connection);
1766 /* Check if the service is being activated */
1767 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1768 if (pending_activation)
1770 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1772 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1774 BUS_SET_OOM (error);
1775 bus_pending_activation_entry_free (pending_activation_entry);
1779 pending_activation->n_entries += 1;
1780 pending_activation->activation->n_pending_activations += 1;
1784 pending_activation = dbus_new0 (BusPendingActivation, 1);
1785 if (!pending_activation)
1787 _dbus_verbose ("Failed to create pending activation\n");
1789 BUS_SET_OOM (error);
1790 bus_pending_activation_entry_free (pending_activation_entry);
1794 pending_activation->activation = activation;
1795 pending_activation->refcount = 1;
1797 pending_activation->service_name = _dbus_strdup (service_name);
1798 if (!pending_activation->service_name)
1800 _dbus_verbose ("Failed to copy service name for pending activation\n");
1802 BUS_SET_OOM (error);
1803 bus_pending_activation_unref (pending_activation);
1804 bus_pending_activation_entry_free (pending_activation_entry);
1808 pending_activation->exec = _dbus_strdup (entry->exec);
1809 if (!pending_activation->exec)
1811 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1812 BUS_SET_OOM (error);
1813 bus_pending_activation_unref (pending_activation);
1814 bus_pending_activation_entry_free (pending_activation_entry);
1818 if (entry->systemd_service)
1820 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1821 if (!pending_activation->systemd_service)
1823 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1824 BUS_SET_OOM (error);
1825 bus_pending_activation_unref (pending_activation);
1826 bus_pending_activation_entry_free (pending_activation_entry);
1831 pending_activation->timeout =
1832 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1833 pending_activation_timed_out,
1836 if (!pending_activation->timeout)
1838 _dbus_verbose ("Failed to create timeout for pending activation\n");
1840 BUS_SET_OOM (error);
1841 bus_pending_activation_unref (pending_activation);
1842 bus_pending_activation_entry_free (pending_activation_entry);
1846 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1847 pending_activation->timeout,
1848 handle_timeout_callback,
1852 _dbus_verbose ("Failed to add timeout for pending activation\n");
1854 BUS_SET_OOM (error);
1855 bus_pending_activation_unref (pending_activation);
1856 bus_pending_activation_entry_free (pending_activation_entry);
1860 pending_activation->timeout_added = TRUE;
1862 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1864 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1866 BUS_SET_OOM (error);
1867 bus_pending_activation_unref (pending_activation);
1868 bus_pending_activation_entry_free (pending_activation_entry);
1872 pending_activation->n_entries += 1;
1873 pending_activation->activation->n_pending_activations += 1;
1876 _dbus_hash_iter_init (activation->pending_activations, &iter);
1877 while (_dbus_hash_iter_next (&iter))
1879 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1881 if (strcmp (p->exec, entry->exec) == 0)
1888 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1889 pending_activation->service_name,
1890 pending_activation))
1892 _dbus_verbose ("Failed to put pending activation in hash table\n");
1894 BUS_SET_OOM (error);
1895 bus_pending_activation_unref (pending_activation);
1900 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1902 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1903 BUS_SET_OOM (error);
1904 _dbus_hash_table_remove_string (activation->pending_activations,
1905 pending_activation->service_name);
1913 if (bus_context_get_systemd_activation (activation->context))
1915 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1916 /* systemd itself is missing apparently. That can happen
1917 only during early startup. Let's just wait until systemd
1918 connects to us and do nothing. */
1921 if (entry->systemd_service)
1923 BusTransaction *activation_transaction;
1924 DBusString service_string;
1925 BusService *service;
1926 BusRegistry *registry;
1928 /* OK, we have a systemd service configured for this entry,
1929 hence let's enqueue an activation request message. This
1930 is implemented as a directed signal, not a method call,
1931 for three reasons: 1) we don't expect a response on
1932 success, where we just expect a name appearing on the
1933 bus; 2) at this time the systemd service might not yet
1934 have connected, so we wouldn't know the message serial at
1935 this point to set up a pending call; 3) it is ugly if the
1936 bus suddenly becomes the caller of a remote method. */
1938 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1939 "org.freedesktop.systemd1.Activator",
1940 "ActivationRequest");
1943 _dbus_verbose ("No memory to create activation message\n");
1944 BUS_SET_OOM (error);
1948 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1949 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1950 !dbus_message_append_args (message,
1951 DBUS_TYPE_STRING, &entry->systemd_service,
1954 _dbus_verbose ("No memory to set args of activation message\n");
1955 dbus_message_unref (message);
1956 BUS_SET_OOM (error);
1960 /* Create our transaction */
1961 activation_transaction = bus_transaction_new (activation->context);
1962 if (activation_transaction == NULL)
1964 _dbus_verbose ("No memory to create activation transaction\n");
1965 dbus_message_unref (message);
1966 BUS_SET_OOM (error);
1970 /* Check whether systemd is already connected */
1971 registry = bus_connection_get_registry (connection);
1972 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1973 service = bus_registry_lookup (registry, &service_string);
1975 if (service != NULL)
1976 /* Wonderful, systemd is connected, let's just send the msg */
1977 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1980 /* systemd is not around, let's "activate" it. */
1981 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1982 message, "org.freedesktop.systemd1", error);
1984 dbus_message_unref (message);
1988 _DBUS_ASSERT_ERROR_IS_SET (error);
1989 _dbus_verbose ("failed to send activation message: %s\n", error->name);
1990 bus_transaction_cancel_and_free (activation_transaction);
1994 bus_transaction_execute_and_free (activation_transaction);
1998 /* OK, we have no configured systemd service, hence let's
1999 proceed with traditional activation. */
2002 /* use command as system and session different */
2003 if (!_dbus_string_init (&command))
2005 BUS_SET_OOM (error);
2009 /* does the bus use a helper? */
2010 servicehelper = bus_context_get_servicehelper (activation->context);
2011 if (servicehelper != NULL)
2013 if (entry->user == NULL)
2015 _dbus_string_free (&command);
2016 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2017 "Cannot do system-bus activation with no user\n");
2021 /* join the helper path and the service name */
2022 if (!_dbus_string_append (&command, servicehelper))
2024 _dbus_string_free (&command);
2025 BUS_SET_OOM (error);
2028 if (!_dbus_string_append (&command, " "))
2030 _dbus_string_free (&command);
2031 BUS_SET_OOM (error);
2034 if (!_dbus_string_append (&command, service_name))
2036 _dbus_string_free (&command);
2037 BUS_SET_OOM (error);
2043 /* the bus does not use a helper, so we can append arguments with the exec line */
2044 if (!_dbus_string_append (&command, entry->exec))
2046 _dbus_string_free (&command);
2047 BUS_SET_OOM (error);
2052 /* convert command into arguments */
2053 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2055 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2056 _DBUS_ASSERT_ERROR_IS_SET (error);
2058 _dbus_hash_table_remove_string (activation->pending_activations,
2059 pending_activation->service_name);
2061 _dbus_string_free (&command);
2064 _dbus_string_free (&command);
2066 if (!add_bus_environment (activation, error))
2068 _DBUS_ASSERT_ERROR_IS_SET (error);
2069 dbus_free_string_array (argv);
2073 envp = bus_activation_get_environment (activation);
2077 BUS_SET_OOM (error);
2078 dbus_free_string_array (argv);
2082 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2083 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2088 _dbus_verbose ("Failed to spawn child\n");
2089 _DBUS_ASSERT_ERROR_IS_SET (error);
2090 dbus_free_string_array (argv);
2091 dbus_free_string_array (envp);
2096 dbus_free_string_array (argv);
2099 _dbus_assert (pending_activation->babysitter != NULL);
2101 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2102 add_babysitter_watch,
2103 remove_babysitter_watch,
2108 BUS_SET_OOM (error);
2109 _dbus_verbose ("Failed to set babysitter watch functions\n");
2117 bus_activation_list_services (BusActivation *activation,
2125 len = _dbus_hash_table_get_n_entries (activation->entries);
2126 retval = dbus_new (char *, len + 1);
2131 _dbus_hash_iter_init (activation->entries, &iter);
2133 while (_dbus_hash_iter_next (&iter))
2135 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2137 retval[i] = _dbus_strdup (entry->name);
2138 if (retval[i] == NULL)
2153 for (j = 0; j < i; j++)
2154 dbus_free (retval[i]);
2161 dbus_activation_systemd_failure (BusActivation *activation,
2162 DBusMessage *message)
2165 const char *code, *str, *unit = NULL;
2167 dbus_error_init(&error);
2169 /* This is called whenever the systemd activator sent us a
2170 response. We'll invalidate all pending activations that match the
2173 if (dbus_message_get_args (message, &error,
2174 DBUS_TYPE_STRING, &unit,
2175 DBUS_TYPE_STRING, &code,
2176 DBUS_TYPE_STRING, &str,
2178 dbus_set_error(&error, code, str);
2184 _dbus_hash_iter_init (activation->pending_activations,
2187 while (_dbus_hash_iter_next (&iter))
2189 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2191 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2192 pending_activation_failed(p, &error);
2196 dbus_error_free(&error);
2201 #ifdef DBUS_BUILD_TESTS
2205 #define SERVICE_NAME_1 "MyService1"
2206 #define SERVICE_NAME_2 "MyService2"
2207 #define SERVICE_NAME_3 "MyService3"
2209 #define SERVICE_FILE_1 "service-1.service"
2210 #define SERVICE_FILE_2 "service-2.service"
2211 #define SERVICE_FILE_3 "service-3.service"
2214 test_create_service_file (DBusString *dir,
2215 const char *filename,
2219 DBusString file_name, full_path;
2221 dbus_bool_t ret_val;
2224 _dbus_string_init_const (&file_name, filename);
2226 if (!_dbus_string_init (&full_path))
2229 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2230 !_dbus_concat_dir_and_file (&full_path, &file_name))
2236 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2243 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2247 _dbus_string_free (&full_path);
2252 test_remove_service_file (DBusString *dir, const char *filename)
2254 DBusString file_name, full_path;
2255 dbus_bool_t ret_val;
2259 _dbus_string_init_const (&file_name, filename);
2261 if (!_dbus_string_init (&full_path))
2264 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2265 !_dbus_concat_dir_and_file (&full_path, &file_name))
2271 if (!_dbus_delete_file (&full_path, NULL))
2278 _dbus_string_free (&full_path);
2283 test_remove_directory (DBusString *dir)
2286 DBusString filename, full_path;
2287 dbus_bool_t ret_val;
2291 if (!_dbus_string_init (&filename))
2294 if (!_dbus_string_init (&full_path))
2296 _dbus_string_free (&filename);
2300 iter = _dbus_directory_open (dir, NULL);
2307 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2309 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2315 _dbus_directory_close (iter);
2317 if (!_dbus_delete_directory (dir, NULL))
2324 _dbus_string_free (&filename);
2325 _dbus_string_free (&full_path);
2331 init_service_reload_test (DBusString *dir)
2335 if (!_dbus_stat (dir, &stat_buf, NULL))
2337 if (!_dbus_create_directory (dir, NULL))
2342 if (!test_remove_directory (dir))
2345 if (!_dbus_create_directory (dir, NULL))
2349 /* Create one initial file */
2350 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2357 cleanup_service_reload_test (DBusString *dir)
2359 if (!test_remove_directory (dir))
2367 BusActivation *activation;
2368 const char *service_name;
2369 dbus_bool_t expecting_find;
2373 check_func (void *data)
2376 BusActivationEntry *entry;
2378 dbus_bool_t ret_val;
2383 dbus_error_init (&error);
2385 entry = activation_find_entry (d->activation, d->service_name, &error);
2388 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2394 if (d->expecting_find)
2398 dbus_error_free (&error);
2402 if (!d->expecting_find)
2410 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2415 err = !_dbus_test_oom_handling (description, check_func, data);
2417 err = !check_func (data);
2420 _dbus_assert_not_reached ("Test failed");
2426 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2428 BusActivation *activation;
2430 DBusList *directories;
2434 _dbus_string_init_const (&address, "");
2436 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2439 activation = bus_activation_new (NULL, &address, &directories, NULL);
2443 d.activation = activation;
2445 /* Check for existing service file */
2446 d.expecting_find = TRUE;
2447 d.service_name = SERVICE_NAME_1;
2449 if (!do_test ("Existing service file", oom_test, &d))
2452 /* Check for non-existing service file */
2453 d.expecting_find = FALSE;
2454 d.service_name = SERVICE_NAME_3;
2456 if (!do_test ("Nonexisting service file", oom_test, &d))
2459 /* Check for added service file */
2460 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2463 d.expecting_find = TRUE;
2464 d.service_name = SERVICE_NAME_2;
2466 if (!do_test ("Added service file", oom_test, &d))
2469 /* Check for removed service file */
2470 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2473 d.expecting_find = FALSE;
2474 d.service_name = SERVICE_FILE_2;
2476 if (!do_test ("Removed service file", oom_test, &d))
2479 /* Check for updated service file */
2481 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2483 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2486 d.expecting_find = TRUE;
2487 d.service_name = SERVICE_NAME_3;
2489 if (!do_test ("Updated service file, part 1", oom_test, &d))
2492 d.expecting_find = FALSE;
2493 d.service_name = SERVICE_NAME_1;
2495 if (!do_test ("Updated service file, part 2", oom_test, &d))
2498 bus_activation_unref (activation);
2499 _dbus_list_clear (&directories);
2505 bus_activation_service_reload_test (const DBusString *test_data_dir)
2507 DBusString directory;
2509 if (!_dbus_string_init (&directory))
2512 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2515 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2516 !_dbus_generate_random_ascii (&directory, 6))
2521 /* Do normal tests */
2522 if (!init_service_reload_test (&directory))
2523 _dbus_assert_not_reached ("could not initiate service reload test");
2525 if (!do_service_reload_test (&directory, FALSE))
2529 if (!init_service_reload_test (&directory))
2530 _dbus_assert_not_reached ("could not initiate service reload test");
2532 if (!do_service_reload_test (&directory, TRUE))
2535 /* Cleanup test directory */
2536 if (!cleanup_service_reload_test (&directory))
2539 _dbus_string_free (&directory);
2544 #endif /* DBUS_BUILD_TESTS */