1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* activation.c Activation of services
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "activation.h"
28 #include "activation-exit-codes.h"
29 #include "desktop-file.h"
34 #include <dbus/dbus-internals.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-list.h>
37 #include <dbus/dbus-shell.h>
38 #include <dbus/dbus-spawn.h>
39 #include <dbus/dbus-timeout.h>
40 #include <dbus/dbus-sysdeps.h>
48 DBusHashTable *entries;
49 DBusHashTable *pending_activations;
52 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
53 * i.e. number of pending activation requests, not pending
56 DBusHashTable *directories;
57 DBusHashTable *environment;
64 DBusHashTable *entries;
65 } BusServiceDirectory;
73 char *systemd_service;
75 BusServiceDirectory *s_dir;
79 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
81 struct BusPendingActivationEntry
83 DBusMessage *activation_message;
84 DBusConnection *connection;
86 dbus_bool_t auto_activation;
92 BusActivation *activation;
95 char *systemd_service;
98 DBusBabysitter *babysitter;
100 unsigned int timeout_added : 1;
101 } BusPendingActivation;
104 static BusServiceDirectory *
105 bus_service_directory_ref (BusServiceDirectory *dir)
107 _dbus_assert (dir->refcount);
116 bus_service_directory_unref (BusServiceDirectory *dir)
121 _dbus_assert (dir->refcount > 0);
124 if (dir->refcount > 0)
128 _dbus_hash_table_unref (dir->entries);
130 dbus_free (dir->dir_c);
135 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
137 if (entry->activation_message)
138 dbus_message_unref (entry->activation_message);
140 if (entry->connection)
141 dbus_connection_unref (entry->connection);
147 handle_timeout_callback (DBusTimeout *timeout,
150 BusPendingActivation *pending_activation = data;
152 while (!dbus_timeout_handle (pending_activation->timeout))
153 _dbus_wait_for_memory ();
156 static BusPendingActivation *
157 bus_pending_activation_ref (BusPendingActivation *pending_activation)
159 _dbus_assert (pending_activation->refcount > 0);
160 pending_activation->refcount += 1;
162 return pending_activation;
166 bus_pending_activation_unref (BusPendingActivation *pending_activation)
170 if (pending_activation == NULL) /* hash table requires this */
173 _dbus_assert (pending_activation->refcount > 0);
174 pending_activation->refcount -= 1;
176 if (pending_activation->refcount > 0)
179 if (pending_activation->timeout_added)
181 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
182 pending_activation->timeout,
183 handle_timeout_callback, pending_activation);
184 pending_activation->timeout_added = FALSE;
187 if (pending_activation->timeout)
188 _dbus_timeout_unref (pending_activation->timeout);
190 if (pending_activation->babysitter)
192 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
194 pending_activation->babysitter,
196 _dbus_assert_not_reached ("setting watch functions to NULL failed");
198 _dbus_babysitter_unref (pending_activation->babysitter);
201 dbus_free (pending_activation->service_name);
202 dbus_free (pending_activation->exec);
203 dbus_free (pending_activation->systemd_service);
205 link = _dbus_list_get_first_link (&pending_activation->entries);
209 BusPendingActivationEntry *entry = link->data;
211 bus_pending_activation_entry_free (entry);
213 link = _dbus_list_get_next_link (&pending_activation->entries, link);
215 _dbus_list_clear (&pending_activation->entries);
217 pending_activation->activation->n_pending_activations -=
218 pending_activation->n_entries;
220 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
222 dbus_free (pending_activation);
225 static BusActivationEntry *
226 bus_activation_entry_ref (BusActivationEntry *entry)
228 _dbus_assert (entry->refcount > 0);
235 bus_activation_entry_unref (BusActivationEntry *entry)
237 if (entry == NULL) /* hash table requires this */
240 _dbus_assert (entry->refcount > 0);
243 if (entry->refcount > 0)
246 dbus_free (entry->name);
247 dbus_free (entry->exec);
248 dbus_free (entry->user);
249 dbus_free (entry->filename);
250 dbus_free (entry->systemd_service);
256 update_desktop_file_entry (BusActivation *activation,
257 BusServiceDirectory *s_dir,
258 DBusString *filename,
259 BusDesktopFile *desktop_file,
262 char *name, *exec, *user, *exec_tmp, *systemd_service;
263 BusActivationEntry *entry;
265 DBusString file_path;
269 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
276 systemd_service = NULL;
278 dbus_error_init (&tmp_error);
280 if (!_dbus_string_init (&file_path))
286 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
287 !_dbus_concat_dir_and_file (&file_path, filename))
293 if (!_dbus_stat (&file_path, &stat_buf, NULL))
295 dbus_set_error (error, DBUS_ERROR_FAILED,
296 "Can't stat the service file\n");
300 if (!bus_desktop_file_get_string (desktop_file,
301 DBUS_SERVICE_SECTION,
307 if (!bus_desktop_file_get_string (desktop_file,
308 DBUS_SERVICE_SECTION,
314 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
315 dbus_free (exec_tmp);
318 /* user is not _required_ unless we are using system activation */
319 if (!bus_desktop_file_get_string (desktop_file,
320 DBUS_SERVICE_SECTION,
324 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
325 /* if we got OOM, then exit */
326 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
328 dbus_move_error (&tmp_error, error);
333 /* if we have error because we didn't find anything then continue */
334 dbus_error_free (&tmp_error);
339 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
341 /* systemd service is never required */
342 if (!bus_desktop_file_get_string (desktop_file,
343 DBUS_SERVICE_SECTION,
344 DBUS_SERVICE_SYSTEMD_SERVICE,
345 &systemd_service, &tmp_error))
347 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
348 /* if we got OOM, then exit */
349 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
351 dbus_move_error (&tmp_error, error);
356 /* if we have error because we didn't find anything then continue */
357 dbus_error_free (&tmp_error);
358 dbus_free (systemd_service);
359 systemd_service = NULL;
363 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
365 entry = _dbus_hash_table_lookup_string (s_dir->entries,
366 _dbus_string_get_const_data (filename));
368 if (entry == NULL) /* New file */
370 /* FIXME we need a better-defined algorithm for which service file to
371 * pick than "whichever one is first in the directory listing"
373 if (_dbus_hash_table_lookup_string (activation->entries, name))
375 dbus_set_error (error, DBUS_ERROR_FAILED,
376 "Service %s already exists in activation entry list\n", name);
380 entry = dbus_new0 (BusActivationEntry, 1);
390 entry->systemd_service = systemd_service;
393 /* ownership has been transferred to entry, do not free separately */
397 systemd_service = NULL;
399 entry->s_dir = s_dir;
400 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
401 if (!entry->filename)
407 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
413 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
415 /* Revert the insertion in the entries table */
416 _dbus_hash_table_remove_string (activation->entries, entry->name);
421 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
423 else /* Just update the entry */
425 bus_activation_entry_ref (entry);
426 _dbus_hash_table_remove_string (activation->entries, entry->name);
428 if (_dbus_hash_table_lookup_string (activation->entries, name))
430 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
431 name, _dbus_string_get_const_data (&file_path));
435 /* ownership has been transferred to entry, do not free separately */
436 dbus_free (entry->name);
440 dbus_free (entry->exec);
444 dbus_free (entry->user);
448 dbus_free (entry->systemd_service);
449 entry->systemd_service = systemd_service;
450 systemd_service = NULL;
452 if (!_dbus_hash_table_insert_string (activation->entries,
453 entry->name, bus_activation_entry_ref(entry)))
456 /* Also remove path to entries hash since we want this in sync with
457 * the entries hash table */
458 _dbus_hash_table_remove_string (entry->s_dir->entries,
460 bus_activation_entry_unref (entry);
465 entry->mtime = stat_buf.mtime;
469 /* if these have been transferred into entry, the variables will be NULL */
473 dbus_free (systemd_service);
474 _dbus_string_free (&file_path);
477 bus_activation_entry_unref (entry);
483 check_service_file (BusActivation *activation,
484 BusActivationEntry *entry,
485 BusActivationEntry **updated_entry,
490 BusActivationEntry *tmp_entry;
491 DBusString file_path;
497 _dbus_string_init_const (&filename, entry->filename);
499 if (!_dbus_string_init (&file_path))
505 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
506 !_dbus_concat_dir_and_file (&file_path, &filename))
513 if (!_dbus_stat (&file_path, &stat_buf, NULL))
515 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
516 _dbus_string_get_const_data (&file_path));
518 _dbus_hash_table_remove_string (activation->entries, entry->name);
519 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
527 if (stat_buf.mtime > entry->mtime)
529 BusDesktopFile *desktop_file;
532 dbus_error_init (&tmp_error);
534 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
535 if (desktop_file == NULL)
537 _dbus_verbose ("Could not load %s: %s\n",
538 _dbus_string_get_const_data (&file_path),
540 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
542 dbus_move_error (&tmp_error, error);
546 dbus_error_free (&tmp_error);
551 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
552 * Handle these both better
554 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
556 bus_desktop_file_free (desktop_file);
557 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
559 dbus_move_error (&tmp_error, error);
563 dbus_error_free (&tmp_error);
568 bus_desktop_file_free (desktop_file);
574 _dbus_string_free (&file_path);
576 if (updated_entry != NULL)
577 *updated_entry = tmp_entry;
582 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
583 * hash entries it already added.
586 update_directory (BusActivation *activation,
587 BusServiceDirectory *s_dir,
591 DBusString dir, filename;
592 BusDesktopFile *desktop_file;
595 BusActivationEntry *entry;
596 DBusString full_path;
598 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
603 _dbus_string_init_const (&dir, s_dir->dir_c);
605 if (!_dbus_string_init (&filename))
611 if (!_dbus_string_init (&full_path))
614 _dbus_string_free (&filename);
620 /* from this point it's safe to "goto out" */
622 iter = _dbus_directory_open (&dir, error);
625 _dbus_verbose ("Failed to open directory %s: %s\n",
627 error ? error->message : "unknown");
631 /* Now read the files */
632 dbus_error_init (&tmp_error);
633 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
635 _dbus_assert (!dbus_error_is_set (&tmp_error));
637 _dbus_string_set_length (&full_path, 0);
639 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
641 _dbus_verbose ("Skipping non-.service file %s\n",
642 _dbus_string_get_const_data (&filename));
646 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
647 if (entry) /* Already has this service file in the cache */
649 if (!check_service_file (activation, entry, NULL, error))
655 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
656 !_dbus_concat_dir_and_file (&full_path, &filename))
663 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
664 if (desktop_file == NULL)
666 _dbus_verbose ("Could not load %s: %s\n",
667 _dbus_string_get_const_data (&full_path),
670 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
672 dbus_move_error (&tmp_error, error);
676 dbus_error_free (&tmp_error);
680 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
681 * Handle these both better
683 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
685 bus_desktop_file_free (desktop_file);
688 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
689 _dbus_string_get_const_data (&full_path), tmp_error.message);
691 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
693 dbus_move_error (&tmp_error, error);
697 dbus_error_free (&tmp_error);
702 bus_desktop_file_free (desktop_file);
708 if (dbus_error_is_set (&tmp_error))
710 dbus_move_error (&tmp_error, error);
718 _DBUS_ASSERT_ERROR_IS_SET (error);
720 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
723 _dbus_directory_close (iter);
724 _dbus_string_free (&filename);
725 _dbus_string_free (&full_path);
731 populate_environment (BusActivation *activation)
737 dbus_bool_t retval = FALSE;
739 environment = _dbus_get_environment ();
741 if (environment == NULL)
744 if (!_dbus_string_init (&key))
746 dbus_free_string_array (environment);
750 if (!_dbus_string_init (&value))
752 _dbus_string_free (&key);
753 dbus_free_string_array (environment);
757 for (i = 0; environment[i] != NULL; i++)
759 if (!_dbus_string_append (&key, environment[i]))
762 if (_dbus_string_split_on_byte (&key, '=', &value))
764 char *hash_key, *hash_value;
766 if (!_dbus_string_steal_data (&key, &hash_key))
769 if (!_dbus_string_steal_data (&value, &hash_value))
772 if (!_dbus_hash_table_insert_string (activation->environment,
773 hash_key, hash_value))
776 _dbus_string_set_length (&key, 0);
777 _dbus_string_set_length (&value, 0);
780 if (environment[i] != NULL)
786 _dbus_string_free (&key);
787 _dbus_string_free (&value);
788 dbus_free_string_array (environment);
794 bus_activation_reload (BusActivation *activation,
795 const DBusString *address,
796 DBusList **directories,
802 if (activation->server_address != NULL)
803 dbus_free (activation->server_address);
804 if (!_dbus_string_copy_data (address, &activation->server_address))
810 if (activation->entries != NULL)
811 _dbus_hash_table_unref (activation->entries);
812 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
813 (DBusFreeFunction)bus_activation_entry_unref);
814 if (activation->entries == NULL)
820 if (activation->directories != NULL)
821 _dbus_hash_table_unref (activation->directories);
822 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
823 (DBusFreeFunction)bus_service_directory_unref);
825 if (activation->directories == NULL)
831 link = _dbus_list_get_first_link (directories);
834 BusServiceDirectory *s_dir;
836 dir = _dbus_strdup ((const char *) link->data);
843 s_dir = dbus_new0 (BusServiceDirectory, 1);
854 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
855 (DBusFreeFunction)bus_activation_entry_unref);
859 bus_service_directory_unref (s_dir);
864 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
866 bus_service_directory_unref (s_dir);
871 /* only fail on OOM, it is ok if we can't read the directory */
872 if (!update_directory (activation, s_dir, error))
874 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
877 dbus_error_free (error);
880 link = _dbus_list_get_next_link (directories, link);
889 bus_activation_new (BusContext *context,
890 const DBusString *address,
891 DBusList **directories,
894 BusActivation *activation;
898 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
900 activation = dbus_new0 (BusActivation, 1);
901 if (activation == NULL)
907 activation->refcount = 1;
908 activation->context = context;
909 activation->n_pending_activations = 0;
911 if (!bus_activation_reload (activation, address, directories, error))
914 /* Initialize this hash table once, we don't want to lose pending
915 * activations on reload. */
916 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
917 (DBusFreeFunction)bus_pending_activation_unref);
919 if (activation->pending_activations == NULL)
925 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
926 (DBusFreeFunction) dbus_free,
927 (DBusFreeFunction) dbus_free);
929 if (activation->environment == NULL)
935 if (!populate_environment (activation))
944 bus_activation_unref (activation);
949 bus_activation_ref (BusActivation *activation)
951 _dbus_assert (activation->refcount > 0);
953 activation->refcount += 1;
959 bus_activation_unref (BusActivation *activation)
961 _dbus_assert (activation->refcount > 0);
963 activation->refcount -= 1;
965 if (activation->refcount > 0)
968 dbus_free (activation->server_address);
969 if (activation->entries)
970 _dbus_hash_table_unref (activation->entries);
971 if (activation->pending_activations)
972 _dbus_hash_table_unref (activation->pending_activations);
973 if (activation->directories)
974 _dbus_hash_table_unref (activation->directories);
975 if (activation->environment)
976 _dbus_hash_table_unref (activation->environment);
978 dbus_free (activation);
982 add_bus_environment (BusActivation *activation,
987 if (!bus_activation_set_environment_variable (activation,
988 "DBUS_STARTER_ADDRESS",
989 activation->server_address,
993 type = bus_context_get_type (activation->context);
996 if (!bus_activation_set_environment_variable (activation,
997 "DBUS_STARTER_BUS_TYPE", type,
1001 if (strcmp (type, "session") == 0)
1003 if (!bus_activation_set_environment_variable (activation,
1004 "DBUS_SESSION_BUS_ADDRESS",
1005 activation->server_address,
1009 else if (strcmp (type, "system") == 0)
1011 if (!bus_activation_set_environment_variable (activation,
1012 "DBUS_SYSTEM_BUS_ADDRESS",
1013 activation->server_address,
1024 BusPendingActivation *pending_activation;
1025 DBusPreallocatedHash *hash_entry;
1026 } RestorePendingData;
1029 restore_pending (void *data)
1031 RestorePendingData *d = data;
1033 _dbus_assert (d->pending_activation != NULL);
1034 _dbus_assert (d->hash_entry != NULL);
1036 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1037 d->pending_activation->service_name,
1038 d->pending_activation->timeout_added);
1040 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1042 d->pending_activation->service_name, d->pending_activation);
1044 bus_pending_activation_ref (d->pending_activation);
1046 d->hash_entry = NULL;
1050 free_pending_restore_data (void *data)
1052 RestorePendingData *d = data;
1055 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1058 bus_pending_activation_unref (d->pending_activation);
1064 add_restore_pending_to_transaction (BusTransaction *transaction,
1065 BusPendingActivation *pending_activation)
1067 RestorePendingData *d;
1069 d = dbus_new (RestorePendingData, 1);
1073 d->pending_activation = pending_activation;
1074 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1076 bus_pending_activation_ref (d->pending_activation);
1078 if (d->hash_entry == NULL ||
1079 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1080 free_pending_restore_data))
1082 free_pending_restore_data (d);
1086 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1092 bus_activation_service_created (BusActivation *activation,
1093 const char *service_name,
1094 BusTransaction *transaction,
1097 BusPendingActivation *pending_activation;
1098 DBusMessage *message;
1101 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1103 /* Check if it's a pending activation */
1104 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1106 if (!pending_activation)
1109 bus_context_log (activation->context,
1110 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1113 link = _dbus_list_get_first_link (&pending_activation->entries);
1114 while (link != NULL)
1116 BusPendingActivationEntry *entry = link->data;
1117 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1119 if (dbus_connection_get_is_connected (entry->connection))
1121 /* Only send activation replies to regular activation requests. */
1122 if (!entry->auto_activation)
1124 dbus_uint32_t result;
1126 message = dbus_message_new_method_return (entry->activation_message);
1129 BUS_SET_OOM (error);
1133 result = DBUS_START_REPLY_SUCCESS;
1135 if (!dbus_message_append_args (message,
1136 DBUS_TYPE_UINT32, &result,
1139 dbus_message_unref (message);
1140 BUS_SET_OOM (error);
1144 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1146 dbus_message_unref (message);
1147 BUS_SET_OOM (error);
1151 dbus_message_unref (message);
1165 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1166 BusService *service,
1167 BusTransaction *transaction,
1170 BusPendingActivation *pending_activation;
1173 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1175 /* Check if it's a pending activation */
1176 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1177 bus_service_get_name (service));
1179 if (!pending_activation)
1182 link = _dbus_list_get_first_link (&pending_activation->entries);
1183 while (link != NULL)
1185 BusPendingActivationEntry *entry = link->data;
1186 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1188 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1190 DBusConnection *addressed_recipient;
1192 addressed_recipient = bus_service_get_primary_owners_connection (service);
1194 /* Resume dispatching where we left off in bus_dispatch() */
1195 if (!bus_dispatch_matches (transaction,
1197 addressed_recipient,
1198 entry->activation_message, error))
1205 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1207 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1208 BUS_SET_OOM (error);
1212 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1221 * FIXME @todo the error messages here would ideally be preallocated
1222 * so we don't need to allocate memory to send them.
1223 * Using the usual tactic, prealloc an OOM message, then
1224 * if we can't alloc the real error send the OOM error instead.
1227 try_send_activation_failure (BusPendingActivation *pending_activation,
1228 const DBusError *how)
1230 BusActivation *activation;
1232 BusTransaction *transaction;
1234 activation = pending_activation->activation;
1236 transaction = bus_transaction_new (activation->context);
1237 if (transaction == NULL)
1240 link = _dbus_list_get_first_link (&pending_activation->entries);
1241 while (link != NULL)
1243 BusPendingActivationEntry *entry = link->data;
1244 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1246 if (dbus_connection_get_is_connected (entry->connection))
1248 if (!bus_transaction_send_error_reply (transaction,
1251 entry->activation_message))
1258 bus_transaction_execute_and_free (transaction);
1264 bus_transaction_cancel_and_free (transaction);
1269 * Free the pending activation and send an error message to all the
1270 * connections that were waiting for it.
1273 pending_activation_failed (BusPendingActivation *pending_activation,
1274 const DBusError *how)
1276 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1277 while (!try_send_activation_failure (pending_activation, how))
1278 _dbus_wait_for_memory ();
1280 /* Destroy this pending activation */
1281 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1282 pending_activation->service_name);
1286 * Depending on the exit code of the helper, set the error accordingly
1289 handle_servicehelper_exit_error (int exit_code,
1294 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1295 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1296 "Launcher could not run (out of memory)");
1298 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1299 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1300 "Failed to setup environment correctly");
1302 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1303 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1304 "Bus name is not valid or missing");
1306 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1307 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1308 "Bus name not found in system service directory");
1310 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1311 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1312 "The permission of the setuid helper is not correct");
1314 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1315 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1316 "The service file is incorrect or does not have all required attributes");
1318 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1319 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1320 "Cannot launch daemon, file not found or permissions invalid");
1322 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1323 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1324 "Invalid arguments to command line");
1326 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1327 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1328 "Launched child was signaled, it probably crashed");
1331 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1332 "Launch helper exited with unknown return code %i", exit_code);
1338 babysitter_watch_callback (DBusWatch *watch,
1339 unsigned int condition,
1342 BusPendingActivation *pending_activation = data;
1344 DBusBabysitter *babysitter;
1345 dbus_bool_t uses_servicehelper;
1347 babysitter = pending_activation->babysitter;
1349 _dbus_babysitter_ref (babysitter);
1351 retval = dbus_watch_handle (watch, condition);
1353 /* There are two major cases here; are we the system bus or the session? Here this
1354 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1355 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1357 * In both cases though, just ignore when a process exits with status 0; it's possible for
1358 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1359 * condition between this code and the child process claiming the bus name.
1361 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1363 /* FIXME this is broken in the same way that
1364 * connection watches used to be; there should be
1365 * a separate callback for status change, instead
1366 * of doing "if we handled a watch status might
1369 * Fixing this lets us move dbus_watch_handle
1370 * calls into dbus-mainloop.c
1372 if (_dbus_babysitter_get_child_exited (babysitter))
1376 dbus_bool_t activation_failed;
1379 dbus_error_init (&error);
1381 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1383 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1385 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1386 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1388 activation_failed = exit_code != 0;
1390 dbus_error_free(&error);
1392 if (activation_failed)
1394 if (uses_servicehelper)
1395 handle_servicehelper_exit_error (exit_code, &error);
1397 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1402 activation_failed = TRUE;
1405 if (activation_failed)
1407 bus_context_log (pending_activation->activation->context,
1408 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1409 pending_activation->service_name,
1412 /* Destroy all pending activations with the same exec */
1413 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1415 while (_dbus_hash_iter_next (&iter))
1417 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1419 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1420 pending_activation_failed (p, &error);
1423 /* Destroys the pending activation */
1424 pending_activation_failed (pending_activation, &error);
1426 dbus_error_free (&error);
1430 _dbus_babysitter_unref (babysitter);
1436 add_babysitter_watch (DBusWatch *watch,
1439 BusPendingActivation *pending_activation = data;
1441 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1442 watch, babysitter_watch_callback, pending_activation,
1447 remove_babysitter_watch (DBusWatch *watch,
1450 BusPendingActivation *pending_activation = data;
1452 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1453 watch, babysitter_watch_callback, pending_activation);
1457 pending_activation_timed_out (void *data)
1459 BusPendingActivation *pending_activation = data;
1462 /* Kill the spawned process, since it sucks
1463 * (not sure this is what we want to do, but
1464 * may as well try it for now)
1466 if (pending_activation->babysitter)
1467 _dbus_babysitter_kill_child (pending_activation->babysitter);
1469 dbus_error_init (&error);
1471 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1472 "Activation of %s timed out",
1473 pending_activation->service_name);
1474 bus_context_log (pending_activation->activation->context,
1475 DBUS_SYSTEM_LOG_INFO,
1476 "Failed to activate service '%s': timed out",
1477 pending_activation->service_name);
1479 pending_activation_failed (pending_activation, &error);
1481 dbus_error_free (&error);
1487 cancel_pending (void *data)
1489 BusPendingActivation *pending_activation = data;
1491 _dbus_verbose ("Canceling pending activation of %s\n",
1492 pending_activation->service_name);
1494 if (pending_activation->babysitter)
1495 _dbus_babysitter_kill_child (pending_activation->babysitter);
1497 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1498 pending_activation->service_name);
1502 free_pending_cancel_data (void *data)
1504 BusPendingActivation *pending_activation = data;
1506 bus_pending_activation_unref (pending_activation);
1510 add_cancel_pending_to_transaction (BusTransaction *transaction,
1511 BusPendingActivation *pending_activation)
1513 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1515 free_pending_cancel_data))
1518 bus_pending_activation_ref (pending_activation);
1520 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1526 update_service_cache (BusActivation *activation, DBusError *error)
1530 _dbus_hash_iter_init (activation->directories, &iter);
1531 while (_dbus_hash_iter_next (&iter))
1533 DBusError tmp_error;
1534 BusServiceDirectory *s_dir;
1536 s_dir = _dbus_hash_iter_get_value (&iter);
1538 dbus_error_init (&tmp_error);
1539 if (!update_directory (activation, s_dir, &tmp_error))
1541 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1543 dbus_move_error (&tmp_error, error);
1547 dbus_error_free (&tmp_error);
1555 static BusActivationEntry *
1556 activation_find_entry (BusActivation *activation,
1557 const char *service_name,
1560 BusActivationEntry *entry;
1562 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1565 if (!update_service_cache (activation, error))
1568 entry = _dbus_hash_table_lookup_string (activation->entries,
1573 BusActivationEntry *updated_entry;
1575 if (!check_service_file (activation, entry, &updated_entry, error))
1578 entry = updated_entry;
1583 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1584 "The name %s was not provided by any .service files",
1593 bus_activation_get_environment (BusActivation *activation)
1600 length = _dbus_hash_table_get_n_entries (activation->environment);
1602 environment = dbus_new0 (char *, length + 1);
1604 if (environment == NULL)
1608 _dbus_hash_iter_init (activation->environment, &iter);
1610 if (!_dbus_string_init (&entry))
1612 dbus_free_string_array (environment);
1616 while (_dbus_hash_iter_next (&iter))
1618 const char *key, *value;
1620 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1621 value = (const char *) _dbus_hash_iter_get_value (&iter);
1623 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1626 if (!_dbus_string_steal_data (&entry, environment + i))
1631 _dbus_string_free (&entry);
1635 dbus_free_string_array (environment);
1643 bus_activation_set_environment_variable (BusActivation *activation,
1655 hash_key = _dbus_strdup (key);
1657 if (hash_key == NULL)
1660 hash_value = _dbus_strdup (value);
1662 if (hash_value == NULL)
1665 if (!_dbus_hash_table_insert_string (activation->environment,
1666 hash_key, hash_value))
1671 if (retval == FALSE)
1673 dbus_free (hash_key);
1674 dbus_free (hash_value);
1675 BUS_SET_OOM (error);
1682 bus_activation_activate_service (BusActivation *activation,
1683 DBusConnection *connection,
1684 BusTransaction *transaction,
1685 dbus_bool_t auto_activation,
1686 DBusMessage *activation_message,
1687 const char *service_name,
1690 DBusError tmp_error;
1691 BusActivationEntry *entry;
1692 BusPendingActivation *pending_activation;
1693 BusPendingActivationEntry *pending_activation_entry;
1694 DBusMessage *message;
1695 DBusString service_str;
1696 const char *servicehelper;
1702 dbus_bool_t was_pending_activation;
1705 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1707 if (activation->n_pending_activations >=
1708 bus_context_get_max_pending_activations (activation->context))
1710 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1711 "The maximum number of pending activations has been reached, activation of %s failed",
1716 entry = activation_find_entry (activation, service_name, error);
1720 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1721 * call us if the service is already active.
1723 if (!auto_activation)
1725 /* Check if the service is active */
1726 _dbus_string_init_const (&service_str, service_name);
1727 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1729 dbus_uint32_t result;
1731 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1733 message = dbus_message_new_method_return (activation_message);
1737 _dbus_verbose ("No memory to create reply to activate message\n");
1738 BUS_SET_OOM (error);
1742 result = DBUS_START_REPLY_ALREADY_RUNNING;
1744 if (!dbus_message_append_args (message,
1745 DBUS_TYPE_UINT32, &result,
1748 _dbus_verbose ("No memory to set args of reply to activate message\n");
1749 BUS_SET_OOM (error);
1750 dbus_message_unref (message);
1754 retval = bus_transaction_send_from_driver (transaction, connection, message);
1755 dbus_message_unref (message);
1758 _dbus_verbose ("Failed to send reply\n");
1759 BUS_SET_OOM (error);
1766 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1767 if (!pending_activation_entry)
1769 _dbus_verbose ("Failed to create pending activation entry\n");
1770 BUS_SET_OOM (error);
1774 pending_activation_entry->auto_activation = auto_activation;
1776 pending_activation_entry->activation_message = activation_message;
1777 dbus_message_ref (activation_message);
1778 pending_activation_entry->connection = connection;
1779 dbus_connection_ref (connection);
1781 /* Check if the service is being activated */
1782 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1783 was_pending_activation = (pending_activation != NULL);
1784 if (was_pending_activation)
1786 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1788 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1790 BUS_SET_OOM (error);
1791 bus_pending_activation_entry_free (pending_activation_entry);
1795 pending_activation->n_entries += 1;
1796 pending_activation->activation->n_pending_activations += 1;
1800 pending_activation = dbus_new0 (BusPendingActivation, 1);
1801 if (!pending_activation)
1803 _dbus_verbose ("Failed to create pending activation\n");
1805 BUS_SET_OOM (error);
1806 bus_pending_activation_entry_free (pending_activation_entry);
1810 pending_activation->activation = activation;
1811 pending_activation->refcount = 1;
1813 pending_activation->service_name = _dbus_strdup (service_name);
1814 if (!pending_activation->service_name)
1816 _dbus_verbose ("Failed to copy service name for pending activation\n");
1818 BUS_SET_OOM (error);
1819 bus_pending_activation_unref (pending_activation);
1820 bus_pending_activation_entry_free (pending_activation_entry);
1824 pending_activation->exec = _dbus_strdup (entry->exec);
1825 if (!pending_activation->exec)
1827 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1828 BUS_SET_OOM (error);
1829 bus_pending_activation_unref (pending_activation);
1830 bus_pending_activation_entry_free (pending_activation_entry);
1834 if (entry->systemd_service)
1836 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1837 if (!pending_activation->systemd_service)
1839 _dbus_verbose ("Failed to copy systemd service 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);
1847 pending_activation->timeout =
1848 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1849 pending_activation_timed_out,
1852 if (!pending_activation->timeout)
1854 _dbus_verbose ("Failed to create timeout for pending activation\n");
1856 BUS_SET_OOM (error);
1857 bus_pending_activation_unref (pending_activation);
1858 bus_pending_activation_entry_free (pending_activation_entry);
1862 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1863 pending_activation->timeout,
1864 handle_timeout_callback,
1868 _dbus_verbose ("Failed to add timeout for pending activation\n");
1870 BUS_SET_OOM (error);
1871 bus_pending_activation_unref (pending_activation);
1872 bus_pending_activation_entry_free (pending_activation_entry);
1876 pending_activation->timeout_added = TRUE;
1878 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1880 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1882 BUS_SET_OOM (error);
1883 bus_pending_activation_unref (pending_activation);
1884 bus_pending_activation_entry_free (pending_activation_entry);
1888 pending_activation->n_entries += 1;
1889 pending_activation->activation->n_pending_activations += 1;
1891 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1892 pending_activation->service_name,
1893 pending_activation))
1895 _dbus_verbose ("Failed to put pending activation in hash table\n");
1897 BUS_SET_OOM (error);
1898 bus_pending_activation_unref (pending_activation);
1903 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1905 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1906 BUS_SET_OOM (error);
1907 _dbus_hash_table_remove_string (activation->pending_activations,
1908 pending_activation->service_name);
1913 if (was_pending_activation)
1916 if (bus_context_get_systemd_activation (activation->context))
1918 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1919 /* systemd itself is missing apparently. That can happen
1920 only during early startup. Let's just wait until systemd
1921 connects to us and do nothing. */
1924 if (entry->systemd_service)
1926 BusTransaction *activation_transaction;
1927 DBusString service_string;
1928 BusService *service;
1929 BusRegistry *registry;
1931 /* OK, we have a systemd service configured for this entry,
1932 hence let's enqueue an activation request message. This
1933 is implemented as a directed signal, not a method call,
1934 for three reasons: 1) we don't expect a response on
1935 success, where we just expect a name appearing on the
1936 bus; 2) at this time the systemd service might not yet
1937 have connected, so we wouldn't know the message serial at
1938 this point to set up a pending call; 3) it is ugly if the
1939 bus suddenly becomes the caller of a remote method. */
1941 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1942 "org.freedesktop.systemd1.Activator",
1943 "ActivationRequest");
1946 _dbus_verbose ("No memory to create activation message\n");
1947 BUS_SET_OOM (error);
1951 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1952 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1953 !dbus_message_append_args (message,
1954 DBUS_TYPE_STRING, &entry->systemd_service,
1957 _dbus_verbose ("No memory to set args of activation message\n");
1958 dbus_message_unref (message);
1959 BUS_SET_OOM (error);
1963 /* Create our transaction */
1964 activation_transaction = bus_transaction_new (activation->context);
1965 if (activation_transaction == NULL)
1967 _dbus_verbose ("No memory to create activation transaction\n");
1968 dbus_message_unref (message);
1969 BUS_SET_OOM (error);
1973 /* Check whether systemd is already connected */
1974 registry = bus_connection_get_registry (connection);
1975 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1976 service = bus_registry_lookup (registry, &service_string);
1978 if (service != NULL)
1980 bus_context_log (activation->context,
1981 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1983 entry->systemd_service);
1984 /* Wonderful, systemd is connected, let's just send the msg */
1985 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1990 bus_context_log (activation->context,
1991 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1993 entry->systemd_service);
1994 /* systemd is not around, let's "activate" it. */
1995 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1996 message, "org.freedesktop.systemd1", error);
1999 dbus_message_unref (message);
2003 bus_context_log (activation->context,
2004 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
2006 entry->systemd_service);
2007 _DBUS_ASSERT_ERROR_IS_SET (error);
2008 _dbus_verbose ("failed to send activation message: %s\n", error->name);
2009 bus_transaction_cancel_and_free (activation_transaction);
2013 bus_transaction_execute_and_free (activation_transaction);
2017 /* OK, we have no configured systemd service, hence let's
2018 proceed with traditional activation. */
2021 /* use command as system and session different */
2022 if (!_dbus_string_init (&command))
2024 BUS_SET_OOM (error);
2028 /* does the bus use a helper? */
2029 servicehelper = bus_context_get_servicehelper (activation->context);
2030 if (servicehelper != NULL)
2032 if (entry->user == NULL)
2034 _dbus_string_free (&command);
2035 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2036 "Cannot do system-bus activation with no user\n");
2040 /* join the helper path and the service name */
2041 if (!_dbus_string_append (&command, servicehelper))
2043 _dbus_string_free (&command);
2044 BUS_SET_OOM (error);
2047 if (!_dbus_string_append (&command, " "))
2049 _dbus_string_free (&command);
2050 BUS_SET_OOM (error);
2053 if (!_dbus_string_append (&command, service_name))
2055 _dbus_string_free (&command);
2056 BUS_SET_OOM (error);
2062 /* the bus does not use a helper, so we can append arguments with the exec line */
2063 if (!_dbus_string_append (&command, entry->exec))
2065 _dbus_string_free (&command);
2066 BUS_SET_OOM (error);
2071 /* convert command into arguments */
2072 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2074 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2075 _DBUS_ASSERT_ERROR_IS_SET (error);
2077 _dbus_hash_table_remove_string (activation->pending_activations,
2078 pending_activation->service_name);
2080 _dbus_string_free (&command);
2083 _dbus_string_free (&command);
2085 if (!add_bus_environment (activation, error))
2087 _DBUS_ASSERT_ERROR_IS_SET (error);
2088 dbus_free_string_array (argv);
2092 envp = bus_activation_get_environment (activation);
2096 BUS_SET_OOM (error);
2097 dbus_free_string_array (argv);
2101 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2102 if (servicehelper != NULL)
2103 bus_context_log (activation->context,
2104 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2107 bus_context_log (activation->context,
2108 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2111 dbus_error_init (&tmp_error);
2113 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2118 _dbus_verbose ("Failed to spawn child\n");
2119 bus_context_log (activation->context,
2120 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2123 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2124 dbus_move_error (&tmp_error, error);
2125 dbus_free_string_array (argv);
2126 dbus_free_string_array (envp);
2131 dbus_free_string_array (argv);
2134 _dbus_assert (pending_activation->babysitter != NULL);
2136 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2137 add_babysitter_watch,
2138 remove_babysitter_watch,
2143 BUS_SET_OOM (error);
2144 _dbus_verbose ("Failed to set babysitter watch functions\n");
2152 bus_activation_list_services (BusActivation *activation,
2160 len = _dbus_hash_table_get_n_entries (activation->entries);
2161 retval = dbus_new (char *, len + 1);
2166 _dbus_hash_iter_init (activation->entries, &iter);
2168 while (_dbus_hash_iter_next (&iter))
2170 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2172 retval[i] = _dbus_strdup (entry->name);
2173 if (retval[i] == NULL)
2188 for (j = 0; j < i; j++)
2189 dbus_free (retval[i]);
2196 dbus_activation_systemd_failure (BusActivation *activation,
2197 DBusMessage *message)
2200 const char *code, *str, *unit = NULL;
2202 dbus_error_init(&error);
2204 /* This is called whenever the systemd activator sent us a
2205 response. We'll invalidate all pending activations that match the
2208 if (dbus_message_get_args (message, &error,
2209 DBUS_TYPE_STRING, &unit,
2210 DBUS_TYPE_STRING, &code,
2211 DBUS_TYPE_STRING, &str,
2213 dbus_set_error(&error, code, str);
2220 bus_context_log (activation->context,
2221 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2225 _dbus_hash_iter_init (activation->pending_activations,
2228 while (_dbus_hash_iter_next (&iter))
2230 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2232 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2233 pending_activation_failed(p, &error);
2237 dbus_error_free(&error);
2242 #ifdef DBUS_BUILD_TESTS
2246 #define SERVICE_NAME_1 "MyService1"
2247 #define SERVICE_NAME_2 "MyService2"
2248 #define SERVICE_NAME_3 "MyService3"
2250 #define SERVICE_FILE_1 "service-1.service"
2251 #define SERVICE_FILE_2 "service-2.service"
2252 #define SERVICE_FILE_3 "service-3.service"
2255 test_create_service_file (DBusString *dir,
2256 const char *filename,
2260 DBusString file_name, full_path;
2262 dbus_bool_t ret_val;
2265 _dbus_string_init_const (&file_name, filename);
2267 if (!_dbus_string_init (&full_path))
2270 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2271 !_dbus_concat_dir_and_file (&full_path, &file_name))
2277 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2284 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2288 _dbus_string_free (&full_path);
2293 test_remove_service_file (DBusString *dir, const char *filename)
2295 DBusString file_name, full_path;
2296 dbus_bool_t ret_val;
2300 _dbus_string_init_const (&file_name, filename);
2302 if (!_dbus_string_init (&full_path))
2305 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2306 !_dbus_concat_dir_and_file (&full_path, &file_name))
2312 if (!_dbus_delete_file (&full_path, NULL))
2319 _dbus_string_free (&full_path);
2324 test_remove_directory (DBusString *dir)
2327 DBusString filename, full_path;
2328 dbus_bool_t ret_val;
2332 if (!_dbus_string_init (&filename))
2335 if (!_dbus_string_init (&full_path))
2337 _dbus_string_free (&filename);
2341 iter = _dbus_directory_open (dir, NULL);
2348 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2350 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2356 _dbus_directory_close (iter);
2358 if (!_dbus_delete_directory (dir, NULL))
2365 _dbus_string_free (&filename);
2366 _dbus_string_free (&full_path);
2372 init_service_reload_test (DBusString *dir)
2376 if (!_dbus_stat (dir, &stat_buf, NULL))
2378 if (!_dbus_create_directory (dir, NULL))
2383 if (!test_remove_directory (dir))
2386 if (!_dbus_create_directory (dir, NULL))
2390 /* Create one initial file */
2391 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2398 cleanup_service_reload_test (DBusString *dir)
2400 if (!test_remove_directory (dir))
2408 BusActivation *activation;
2409 const char *service_name;
2410 dbus_bool_t expecting_find;
2414 check_func (void *data)
2417 BusActivationEntry *entry;
2419 dbus_bool_t ret_val;
2424 dbus_error_init (&error);
2426 entry = activation_find_entry (d->activation, d->service_name, &error);
2429 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2435 if (d->expecting_find)
2439 dbus_error_free (&error);
2443 if (!d->expecting_find)
2451 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2456 err = !_dbus_test_oom_handling (description, check_func, data);
2458 err = !check_func (data);
2461 _dbus_assert_not_reached ("Test failed");
2467 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2469 BusActivation *activation;
2471 DBusList *directories;
2475 _dbus_string_init_const (&address, "");
2477 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2480 activation = bus_activation_new (NULL, &address, &directories, NULL);
2484 d.activation = activation;
2486 /* Check for existing service file */
2487 d.expecting_find = TRUE;
2488 d.service_name = SERVICE_NAME_1;
2490 if (!do_test ("Existing service file", oom_test, &d))
2493 /* Check for non-existing service file */
2494 d.expecting_find = FALSE;
2495 d.service_name = SERVICE_NAME_3;
2497 if (!do_test ("Nonexisting service file", oom_test, &d))
2500 /* Check for added service file */
2501 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2504 d.expecting_find = TRUE;
2505 d.service_name = SERVICE_NAME_2;
2507 if (!do_test ("Added service file", oom_test, &d))
2510 /* Check for removed service file */
2511 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2514 d.expecting_find = FALSE;
2515 d.service_name = SERVICE_FILE_2;
2517 if (!do_test ("Removed service file", oom_test, &d))
2520 /* Check for updated service file */
2522 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2524 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2527 d.expecting_find = TRUE;
2528 d.service_name = SERVICE_NAME_3;
2530 if (!do_test ("Updated service file, part 1", oom_test, &d))
2533 d.expecting_find = FALSE;
2534 d.service_name = SERVICE_NAME_1;
2536 if (!do_test ("Updated service file, part 2", oom_test, &d))
2539 bus_activation_unref (activation);
2540 _dbus_list_clear (&directories);
2546 bus_activation_service_reload_test (const DBusString *test_data_dir)
2548 DBusString directory;
2550 if (!_dbus_string_init (&directory))
2553 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2556 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2557 !_dbus_generate_random_ascii (&directory, 6))
2562 /* Do normal tests */
2563 if (!init_service_reload_test (&directory))
2564 _dbus_assert_not_reached ("could not initiate service reload test");
2566 if (!do_service_reload_test (&directory, FALSE))
2570 if (!init_service_reload_test (&directory))
2571 _dbus_assert_not_reached ("could not initiate service reload test");
2573 if (!do_service_reload_test (&directory, TRUE))
2576 /* Cleanup test directory */
2577 if (!cleanup_service_reload_test (&directory))
2580 _dbus_string_free (&directory);
2585 #endif /* DBUS_BUILD_TESTS */