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);
277 systemd_service = NULL;
279 dbus_error_init (&tmp_error);
281 if (!_dbus_string_init (&file_path))
287 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
288 !_dbus_concat_dir_and_file (&file_path, filename))
294 if (!_dbus_stat (&file_path, &stat_buf, NULL))
296 dbus_set_error (error, DBUS_ERROR_FAILED,
297 "Can't stat the service file\n");
301 if (!bus_desktop_file_get_string (desktop_file,
302 DBUS_SERVICE_SECTION,
308 if (!bus_desktop_file_get_string (desktop_file,
309 DBUS_SERVICE_SECTION,
315 exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
316 dbus_free (exec_tmp);
319 /* user is not _required_ unless we are using system activation */
320 if (!bus_desktop_file_get_string (desktop_file,
321 DBUS_SERVICE_SECTION,
325 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
326 /* if we got OOM, then exit */
327 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
329 dbus_move_error (&tmp_error, error);
334 /* if we have error because we didn't find anything then continue */
335 dbus_error_free (&tmp_error);
340 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
342 /* systemd service is never required */
343 if (!bus_desktop_file_get_string (desktop_file,
344 DBUS_SERVICE_SECTION,
345 DBUS_SERVICE_SYSTEMD_SERVICE,
346 &systemd_service, &tmp_error))
348 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
349 /* if we got OOM, then exit */
350 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
352 dbus_move_error (&tmp_error, error);
357 /* if we have error because we didn't find anything then continue */
358 dbus_error_free (&tmp_error);
359 dbus_free (systemd_service);
360 systemd_service = NULL;
364 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
366 entry = _dbus_hash_table_lookup_string (s_dir->entries,
367 _dbus_string_get_const_data (filename));
369 if (entry == NULL) /* New file */
371 /* FIXME we need a better-defined algorithm for which service file to
372 * pick than "whichever one is first in the directory listing"
374 if (_dbus_hash_table_lookup_string (activation->entries, name))
376 dbus_set_error (error, DBUS_ERROR_FAILED,
377 "Service %s already exists in activation entry list\n", name);
381 entry = dbus_new0 (BusActivationEntry, 1);
391 entry->systemd_service = systemd_service;
394 /* ownership has been transferred to entry, do not free separately */
398 systemd_service = NULL;
400 entry->s_dir = s_dir;
401 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
402 if (!entry->filename)
408 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
414 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
416 /* Revert the insertion in the entries table */
417 _dbus_hash_table_remove_string (activation->entries, entry->name);
422 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
424 else /* Just update the entry */
426 bus_activation_entry_ref (entry);
427 _dbus_hash_table_remove_string (activation->entries, entry->name);
429 if (_dbus_hash_table_lookup_string (activation->entries, name))
431 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
432 name, _dbus_string_get_const_data (&file_path));
433 dbus_set_error (error, DBUS_ERROR_FAILED,
434 "The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
435 name, _dbus_string_get_const_data (&file_path));
439 /* ownership has been transferred to entry, do not free separately */
440 dbus_free (entry->name);
444 dbus_free (entry->exec);
448 dbus_free (entry->user);
452 dbus_free (entry->systemd_service);
453 entry->systemd_service = systemd_service;
454 systemd_service = NULL;
456 if (!_dbus_hash_table_insert_string (activation->entries,
457 entry->name, bus_activation_entry_ref(entry)))
460 /* Also remove path to entries hash since we want this in sync with
461 * the entries hash table */
462 _dbus_hash_table_remove_string (entry->s_dir->entries,
468 entry->mtime = stat_buf.mtime;
472 /* if these have been transferred into entry, the variables will be NULL */
476 dbus_free (systemd_service);
477 _dbus_string_free (&file_path);
480 bus_activation_entry_unref (entry);
486 check_service_file (BusActivation *activation,
487 BusActivationEntry *entry,
488 BusActivationEntry **updated_entry,
493 BusActivationEntry *tmp_entry;
494 DBusString file_path;
500 _dbus_string_init_const (&filename, entry->filename);
502 if (!_dbus_string_init (&file_path))
508 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
509 !_dbus_concat_dir_and_file (&file_path, &filename))
516 if (!_dbus_stat (&file_path, &stat_buf, NULL))
518 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
519 _dbus_string_get_const_data (&file_path));
521 _dbus_hash_table_remove_string (activation->entries, entry->name);
522 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
530 if (stat_buf.mtime > entry->mtime)
532 BusDesktopFile *desktop_file;
535 dbus_error_init (&tmp_error);
537 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
538 if (desktop_file == NULL)
540 _dbus_verbose ("Could not load %s: %s\n",
541 _dbus_string_get_const_data (&file_path),
543 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
545 dbus_move_error (&tmp_error, error);
549 dbus_error_free (&tmp_error);
554 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
555 * Handle these both better
557 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
559 bus_desktop_file_free (desktop_file);
560 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
562 dbus_move_error (&tmp_error, error);
566 dbus_error_free (&tmp_error);
571 bus_desktop_file_free (desktop_file);
577 _dbus_string_free (&file_path);
579 if (updated_entry != NULL)
580 *updated_entry = tmp_entry;
585 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
586 * hash entries it already added.
589 update_directory (BusActivation *activation,
590 BusServiceDirectory *s_dir,
594 DBusString dir, filename;
595 BusDesktopFile *desktop_file;
598 BusActivationEntry *entry;
599 DBusString full_path;
601 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
606 _dbus_string_init_const (&dir, s_dir->dir_c);
608 if (!_dbus_string_init (&filename))
614 if (!_dbus_string_init (&full_path))
617 _dbus_string_free (&filename);
623 /* from this point it's safe to "goto out" */
625 iter = _dbus_directory_open (&dir, error);
628 _dbus_verbose ("Failed to open directory %s: %s\n",
630 error ? error->message : "unknown");
634 /* Now read the files */
635 dbus_error_init (&tmp_error);
636 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
638 _dbus_assert (!dbus_error_is_set (&tmp_error));
640 _dbus_string_set_length (&full_path, 0);
642 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
644 _dbus_verbose ("Skipping non-.service file %s\n",
645 _dbus_string_get_const_data (&filename));
649 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
650 if (entry) /* Already has this service file in the cache */
652 if (!check_service_file (activation, entry, NULL, error))
658 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
659 !_dbus_concat_dir_and_file (&full_path, &filename))
666 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
667 if (desktop_file == NULL)
669 _dbus_verbose ("Could not load %s: %s\n",
670 _dbus_string_get_const_data (&full_path),
673 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
675 dbus_move_error (&tmp_error, error);
679 dbus_error_free (&tmp_error);
683 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
684 * Handle these both better
686 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
688 bus_desktop_file_free (desktop_file);
691 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
692 _dbus_string_get_const_data (&full_path), tmp_error.message);
694 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
696 dbus_move_error (&tmp_error, error);
700 dbus_error_free (&tmp_error);
705 bus_desktop_file_free (desktop_file);
711 if (dbus_error_is_set (&tmp_error))
713 dbus_move_error (&tmp_error, error);
721 _DBUS_ASSERT_ERROR_IS_SET (error);
723 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
726 _dbus_directory_close (iter);
727 _dbus_string_free (&filename);
728 _dbus_string_free (&full_path);
734 populate_environment (BusActivation *activation)
740 dbus_bool_t retval = FALSE;
742 environment = _dbus_get_environment ();
744 if (environment == NULL)
747 if (!_dbus_string_init (&key))
749 dbus_free_string_array (environment);
753 if (!_dbus_string_init (&value))
755 _dbus_string_free (&key);
756 dbus_free_string_array (environment);
760 for (i = 0; environment[i] != NULL; i++)
762 if (!_dbus_string_append (&key, environment[i]))
765 if (_dbus_string_split_on_byte (&key, '=', &value))
767 char *hash_key, *hash_value;
769 if (!_dbus_string_steal_data (&key, &hash_key))
772 if (!_dbus_string_steal_data (&value, &hash_value))
775 if (!_dbus_hash_table_insert_string (activation->environment,
776 hash_key, hash_value))
779 _dbus_string_set_length (&key, 0);
780 _dbus_string_set_length (&value, 0);
783 if (environment[i] != NULL)
789 _dbus_string_free (&key);
790 _dbus_string_free (&value);
791 dbus_free_string_array (environment);
797 bus_activation_reload (BusActivation *activation,
798 const DBusString *address,
799 DBusList **directories,
805 if (activation->server_address != NULL)
806 dbus_free (activation->server_address);
807 if (!_dbus_string_copy_data (address, &activation->server_address))
813 if (activation->entries != NULL)
814 _dbus_hash_table_unref (activation->entries);
815 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
816 (DBusFreeFunction)bus_activation_entry_unref);
817 if (activation->entries == NULL)
823 if (activation->directories != NULL)
824 _dbus_hash_table_unref (activation->directories);
825 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
826 (DBusFreeFunction)bus_service_directory_unref);
828 if (activation->directories == NULL)
834 link = _dbus_list_get_first_link (directories);
837 BusServiceDirectory *s_dir;
839 dir = _dbus_strdup ((const char *) link->data);
846 s_dir = dbus_new0 (BusServiceDirectory, 1);
857 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
858 (DBusFreeFunction)bus_activation_entry_unref);
862 bus_service_directory_unref (s_dir);
867 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
869 bus_service_directory_unref (s_dir);
874 /* only fail on OOM, it is ok if we can't read the directory */
875 if (!update_directory (activation, s_dir, error))
877 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
880 dbus_error_free (error);
883 link = _dbus_list_get_next_link (directories, link);
892 bus_activation_new (BusContext *context,
893 const DBusString *address,
894 DBusList **directories,
897 BusActivation *activation;
901 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
903 activation = dbus_new0 (BusActivation, 1);
904 if (activation == NULL)
910 activation->refcount = 1;
911 activation->context = context;
912 activation->n_pending_activations = 0;
914 if (!bus_activation_reload (activation, address, directories, error))
917 /* Initialize this hash table once, we don't want to lose pending
918 * activations on reload. */
919 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
920 (DBusFreeFunction)bus_pending_activation_unref);
922 if (activation->pending_activations == NULL)
928 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
929 (DBusFreeFunction) dbus_free,
930 (DBusFreeFunction) dbus_free);
932 if (activation->environment == NULL)
938 if (!populate_environment (activation))
947 bus_activation_unref (activation);
952 bus_activation_ref (BusActivation *activation)
954 _dbus_assert (activation->refcount > 0);
956 activation->refcount += 1;
962 bus_activation_unref (BusActivation *activation)
964 _dbus_assert (activation->refcount > 0);
966 activation->refcount -= 1;
968 if (activation->refcount > 0)
971 dbus_free (activation->server_address);
972 if (activation->entries)
973 _dbus_hash_table_unref (activation->entries);
974 if (activation->pending_activations)
975 _dbus_hash_table_unref (activation->pending_activations);
976 if (activation->directories)
977 _dbus_hash_table_unref (activation->directories);
978 if (activation->environment)
979 _dbus_hash_table_unref (activation->environment);
981 dbus_free (activation);
985 add_bus_environment (BusActivation *activation,
990 if (!bus_activation_set_environment_variable (activation,
991 "DBUS_STARTER_ADDRESS",
992 activation->server_address,
996 type = bus_context_get_type (activation->context);
999 if (!bus_activation_set_environment_variable (activation,
1000 "DBUS_STARTER_BUS_TYPE", type,
1004 if (strcmp (type, "session") == 0)
1006 if (!bus_activation_set_environment_variable (activation,
1007 "DBUS_SESSION_BUS_ADDRESS",
1008 activation->server_address,
1012 else if (strcmp (type, "system") == 0)
1014 if (!bus_activation_set_environment_variable (activation,
1015 "DBUS_SYSTEM_BUS_ADDRESS",
1016 activation->server_address,
1027 BusPendingActivation *pending_activation;
1028 DBusPreallocatedHash *hash_entry;
1029 } RestorePendingData;
1032 restore_pending (void *data)
1034 RestorePendingData *d = data;
1036 _dbus_assert (d->pending_activation != NULL);
1037 _dbus_assert (d->hash_entry != NULL);
1039 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1040 d->pending_activation->service_name,
1041 d->pending_activation->timeout_added);
1043 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1045 d->pending_activation->service_name, d->pending_activation);
1047 bus_pending_activation_ref (d->pending_activation);
1049 d->hash_entry = NULL;
1053 free_pending_restore_data (void *data)
1055 RestorePendingData *d = data;
1058 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1061 bus_pending_activation_unref (d->pending_activation);
1067 add_restore_pending_to_transaction (BusTransaction *transaction,
1068 BusPendingActivation *pending_activation)
1070 RestorePendingData *d;
1072 d = dbus_new (RestorePendingData, 1);
1076 d->pending_activation = pending_activation;
1077 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1079 bus_pending_activation_ref (d->pending_activation);
1081 if (d->hash_entry == NULL ||
1082 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1083 free_pending_restore_data))
1085 free_pending_restore_data (d);
1089 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1095 bus_activation_service_created (BusActivation *activation,
1096 const char *service_name,
1097 BusTransaction *transaction,
1100 BusPendingActivation *pending_activation;
1101 DBusMessage *message;
1104 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1106 /* Check if it's a pending activation */
1107 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1109 if (!pending_activation)
1112 bus_context_log (activation->context,
1113 DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
1116 link = _dbus_list_get_first_link (&pending_activation->entries);
1117 while (link != NULL)
1119 BusPendingActivationEntry *entry = link->data;
1120 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1122 if (dbus_connection_get_is_connected (entry->connection))
1124 /* Only send activation replies to regular activation requests. */
1125 if (!entry->auto_activation)
1127 dbus_uint32_t result;
1129 message = dbus_message_new_method_return (entry->activation_message);
1132 BUS_SET_OOM (error);
1136 result = DBUS_START_REPLY_SUCCESS;
1138 if (!dbus_message_append_args (message,
1139 DBUS_TYPE_UINT32, &result,
1142 dbus_message_unref (message);
1143 BUS_SET_OOM (error);
1147 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1149 dbus_message_unref (message);
1150 BUS_SET_OOM (error);
1154 dbus_message_unref (message);
1168 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1169 BusService *service,
1170 BusTransaction *transaction,
1173 BusPendingActivation *pending_activation;
1176 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1178 /* Check if it's a pending activation */
1179 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1180 bus_service_get_name (service));
1182 if (!pending_activation)
1185 link = _dbus_list_get_first_link (&pending_activation->entries);
1186 while (link != NULL)
1188 BusPendingActivationEntry *entry = link->data;
1189 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1191 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1193 DBusConnection *addressed_recipient;
1195 addressed_recipient = bus_service_get_primary_owners_connection (service);
1197 /* Resume dispatching where we left off in bus_dispatch() */
1198 if (!bus_dispatch_matches (transaction,
1200 addressed_recipient,
1201 entry->activation_message, error))
1208 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1210 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1211 BUS_SET_OOM (error);
1215 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1224 * FIXME @todo the error messages here would ideally be preallocated
1225 * so we don't need to allocate memory to send them.
1226 * Using the usual tactic, prealloc an OOM message, then
1227 * if we can't alloc the real error send the OOM error instead.
1230 try_send_activation_failure (BusPendingActivation *pending_activation,
1231 const DBusError *how)
1233 BusActivation *activation;
1235 BusTransaction *transaction;
1237 activation = pending_activation->activation;
1239 transaction = bus_transaction_new (activation->context);
1240 if (transaction == NULL)
1243 link = _dbus_list_get_first_link (&pending_activation->entries);
1244 while (link != NULL)
1246 BusPendingActivationEntry *entry = link->data;
1247 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1249 if (dbus_connection_get_is_connected (entry->connection))
1251 if (!bus_transaction_send_error_reply (transaction,
1254 entry->activation_message))
1261 bus_transaction_execute_and_free (transaction);
1267 bus_transaction_cancel_and_free (transaction);
1272 * Free the pending activation and send an error message to all the
1273 * connections that were waiting for it.
1276 pending_activation_failed (BusPendingActivation *pending_activation,
1277 const DBusError *how)
1279 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1280 while (!try_send_activation_failure (pending_activation, how))
1281 _dbus_wait_for_memory ();
1283 /* Destroy this pending activation */
1284 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1285 pending_activation->service_name);
1289 * Depending on the exit code of the helper, set the error accordingly
1292 handle_servicehelper_exit_error (int exit_code,
1297 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1298 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1299 "Launcher could not run (out of memory)");
1301 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1302 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1303 "Failed to setup environment correctly");
1305 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1306 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1307 "Bus name is not valid or missing");
1309 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1310 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1311 "Bus name not found in system service directory");
1313 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1314 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1315 "The permission of the setuid helper is not correct");
1317 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1318 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1319 "The service file is incorrect or does not have all required attributes");
1321 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1322 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1323 "Cannot launch daemon, file not found or permissions invalid");
1325 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1326 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1327 "Invalid arguments to command line");
1329 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1330 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1331 "Launched child was signaled, it probably crashed");
1334 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1335 "Launch helper exited with unknown return code %i", exit_code);
1341 babysitter_watch_callback (DBusWatch *watch,
1342 unsigned int condition,
1345 BusPendingActivation *pending_activation = data;
1347 DBusBabysitter *babysitter;
1348 dbus_bool_t uses_servicehelper;
1350 babysitter = pending_activation->babysitter;
1352 _dbus_babysitter_ref (babysitter);
1354 retval = dbus_watch_handle (watch, condition);
1356 /* There are two major cases here; are we the system bus or the session? Here this
1357 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1358 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1360 * In both cases though, just ignore when a process exits with status 0; it's possible for
1361 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1362 * condition between this code and the child process claiming the bus name.
1364 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1366 /* FIXME this is broken in the same way that
1367 * connection watches used to be; there should be
1368 * a separate callback for status change, instead
1369 * of doing "if we handled a watch status might
1372 * Fixing this lets us move dbus_watch_handle
1373 * calls into dbus-mainloop.c
1375 if (_dbus_babysitter_get_child_exited (babysitter))
1379 dbus_bool_t activation_failed;
1382 dbus_error_init (&error);
1384 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1386 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1388 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1389 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1391 activation_failed = exit_code != 0;
1393 dbus_error_free(&error);
1395 if (activation_failed)
1397 if (uses_servicehelper)
1398 handle_servicehelper_exit_error (exit_code, &error);
1400 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1405 activation_failed = TRUE;
1408 if (activation_failed)
1410 bus_context_log (pending_activation->activation->context,
1411 DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
1412 pending_activation->service_name,
1415 /* Destroy all pending activations with the same exec */
1416 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1418 while (_dbus_hash_iter_next (&iter))
1420 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1422 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1423 pending_activation_failed (p, &error);
1426 /* Destroys the pending activation */
1427 pending_activation_failed (pending_activation, &error);
1429 dbus_error_free (&error);
1433 _dbus_babysitter_unref (babysitter);
1439 add_babysitter_watch (DBusWatch *watch,
1442 BusPendingActivation *pending_activation = data;
1444 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1445 watch, babysitter_watch_callback, pending_activation,
1450 remove_babysitter_watch (DBusWatch *watch,
1453 BusPendingActivation *pending_activation = data;
1455 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1456 watch, babysitter_watch_callback, pending_activation);
1460 pending_activation_timed_out (void *data)
1462 BusPendingActivation *pending_activation = data;
1465 /* Kill the spawned process, since it sucks
1466 * (not sure this is what we want to do, but
1467 * may as well try it for now)
1469 if (pending_activation->babysitter)
1470 _dbus_babysitter_kill_child (pending_activation->babysitter);
1472 dbus_error_init (&error);
1474 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1475 "Activation of %s timed out",
1476 pending_activation->service_name);
1477 bus_context_log (pending_activation->activation->context,
1478 DBUS_SYSTEM_LOG_INFO,
1479 "Failed to activate service '%s': timed out",
1480 pending_activation->service_name);
1482 pending_activation_failed (pending_activation, &error);
1484 dbus_error_free (&error);
1490 cancel_pending (void *data)
1492 BusPendingActivation *pending_activation = data;
1494 _dbus_verbose ("Canceling pending activation of %s\n",
1495 pending_activation->service_name);
1497 if (pending_activation->babysitter)
1498 _dbus_babysitter_kill_child (pending_activation->babysitter);
1500 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1501 pending_activation->service_name);
1505 free_pending_cancel_data (void *data)
1507 BusPendingActivation *pending_activation = data;
1509 bus_pending_activation_unref (pending_activation);
1513 add_cancel_pending_to_transaction (BusTransaction *transaction,
1514 BusPendingActivation *pending_activation)
1516 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1518 free_pending_cancel_data))
1521 bus_pending_activation_ref (pending_activation);
1523 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1529 update_service_cache (BusActivation *activation, DBusError *error)
1533 _dbus_hash_iter_init (activation->directories, &iter);
1534 while (_dbus_hash_iter_next (&iter))
1536 DBusError tmp_error;
1537 BusServiceDirectory *s_dir;
1539 s_dir = _dbus_hash_iter_get_value (&iter);
1541 dbus_error_init (&tmp_error);
1542 if (!update_directory (activation, s_dir, &tmp_error))
1544 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1546 dbus_move_error (&tmp_error, error);
1550 dbus_error_free (&tmp_error);
1558 static BusActivationEntry *
1559 activation_find_entry (BusActivation *activation,
1560 const char *service_name,
1563 BusActivationEntry *entry;
1565 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1568 if (!update_service_cache (activation, error))
1571 entry = _dbus_hash_table_lookup_string (activation->entries,
1576 BusActivationEntry *updated_entry;
1578 if (!check_service_file (activation, entry, &updated_entry, error))
1581 entry = updated_entry;
1586 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1587 "The name %s was not provided by any .service files",
1596 bus_activation_get_environment (BusActivation *activation)
1603 length = _dbus_hash_table_get_n_entries (activation->environment);
1605 environment = dbus_new0 (char *, length + 1);
1607 if (environment == NULL)
1611 _dbus_hash_iter_init (activation->environment, &iter);
1613 if (!_dbus_string_init (&entry))
1615 dbus_free_string_array (environment);
1619 while (_dbus_hash_iter_next (&iter))
1621 const char *key, *value;
1623 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1624 value = (const char *) _dbus_hash_iter_get_value (&iter);
1626 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1629 if (!_dbus_string_steal_data (&entry, environment + i))
1634 _dbus_string_free (&entry);
1638 dbus_free_string_array (environment);
1646 bus_activation_set_environment_variable (BusActivation *activation,
1658 hash_key = _dbus_strdup (key);
1660 if (hash_key == NULL)
1663 hash_value = _dbus_strdup (value);
1665 if (hash_value == NULL)
1668 if (!_dbus_hash_table_insert_string (activation->environment,
1669 hash_key, hash_value))
1674 if (retval == FALSE)
1676 dbus_free (hash_key);
1677 dbus_free (hash_value);
1678 BUS_SET_OOM (error);
1685 bus_activation_activate_service (BusActivation *activation,
1686 DBusConnection *connection,
1687 BusTransaction *transaction,
1688 dbus_bool_t auto_activation,
1689 DBusMessage *activation_message,
1690 const char *service_name,
1693 DBusError tmp_error;
1694 BusActivationEntry *entry;
1695 BusPendingActivation *pending_activation;
1696 BusPendingActivationEntry *pending_activation_entry;
1697 DBusMessage *message;
1698 DBusString service_str;
1699 const char *servicehelper;
1705 dbus_bool_t was_pending_activation;
1708 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1710 if (activation->n_pending_activations >=
1711 bus_context_get_max_pending_activations (activation->context))
1713 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1714 "The maximum number of pending activations has been reached, activation of %s failed",
1719 entry = activation_find_entry (activation, service_name, error);
1723 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1724 * call us if the service is already active.
1726 if (!auto_activation)
1728 /* Check if the service is active */
1729 _dbus_string_init_const (&service_str, service_name);
1730 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1732 dbus_uint32_t result;
1734 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1736 message = dbus_message_new_method_return (activation_message);
1740 _dbus_verbose ("No memory to create reply to activate message\n");
1741 BUS_SET_OOM (error);
1745 result = DBUS_START_REPLY_ALREADY_RUNNING;
1747 if (!dbus_message_append_args (message,
1748 DBUS_TYPE_UINT32, &result,
1751 _dbus_verbose ("No memory to set args of reply to activate message\n");
1752 BUS_SET_OOM (error);
1753 dbus_message_unref (message);
1757 retval = bus_transaction_send_from_driver (transaction, connection, message);
1758 dbus_message_unref (message);
1761 _dbus_verbose ("Failed to send reply\n");
1762 BUS_SET_OOM (error);
1769 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1770 if (!pending_activation_entry)
1772 _dbus_verbose ("Failed to create pending activation entry\n");
1773 BUS_SET_OOM (error);
1777 pending_activation_entry->auto_activation = auto_activation;
1779 pending_activation_entry->activation_message = activation_message;
1780 dbus_message_ref (activation_message);
1781 pending_activation_entry->connection = connection;
1782 dbus_connection_ref (connection);
1784 /* Check if the service is being activated */
1785 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1786 was_pending_activation = (pending_activation != NULL);
1787 if (was_pending_activation)
1789 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1791 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1793 BUS_SET_OOM (error);
1794 bus_pending_activation_entry_free (pending_activation_entry);
1798 pending_activation->n_entries += 1;
1799 pending_activation->activation->n_pending_activations += 1;
1803 pending_activation = dbus_new0 (BusPendingActivation, 1);
1804 if (!pending_activation)
1806 _dbus_verbose ("Failed to create pending activation\n");
1808 BUS_SET_OOM (error);
1809 bus_pending_activation_entry_free (pending_activation_entry);
1813 pending_activation->activation = activation;
1814 pending_activation->refcount = 1;
1816 pending_activation->service_name = _dbus_strdup (service_name);
1817 if (!pending_activation->service_name)
1819 _dbus_verbose ("Failed to copy service name for pending activation\n");
1821 BUS_SET_OOM (error);
1822 bus_pending_activation_unref (pending_activation);
1823 bus_pending_activation_entry_free (pending_activation_entry);
1827 pending_activation->exec = _dbus_strdup (entry->exec);
1828 if (!pending_activation->exec)
1830 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1831 BUS_SET_OOM (error);
1832 bus_pending_activation_unref (pending_activation);
1833 bus_pending_activation_entry_free (pending_activation_entry);
1837 if (entry->systemd_service)
1839 pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1840 if (!pending_activation->systemd_service)
1842 _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1843 BUS_SET_OOM (error);
1844 bus_pending_activation_unref (pending_activation);
1845 bus_pending_activation_entry_free (pending_activation_entry);
1850 pending_activation->timeout =
1851 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1852 pending_activation_timed_out,
1855 if (!pending_activation->timeout)
1857 _dbus_verbose ("Failed to create timeout for pending activation\n");
1859 BUS_SET_OOM (error);
1860 bus_pending_activation_unref (pending_activation);
1861 bus_pending_activation_entry_free (pending_activation_entry);
1865 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1866 pending_activation->timeout,
1867 handle_timeout_callback,
1871 _dbus_verbose ("Failed to add timeout for pending activation\n");
1873 BUS_SET_OOM (error);
1874 bus_pending_activation_unref (pending_activation);
1875 bus_pending_activation_entry_free (pending_activation_entry);
1879 pending_activation->timeout_added = TRUE;
1881 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1883 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1885 BUS_SET_OOM (error);
1886 bus_pending_activation_unref (pending_activation);
1887 bus_pending_activation_entry_free (pending_activation_entry);
1891 pending_activation->n_entries += 1;
1892 pending_activation->activation->n_pending_activations += 1;
1894 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1895 pending_activation->service_name,
1896 pending_activation))
1898 _dbus_verbose ("Failed to put pending activation in hash table\n");
1900 BUS_SET_OOM (error);
1901 bus_pending_activation_unref (pending_activation);
1906 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1908 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1909 BUS_SET_OOM (error);
1910 _dbus_hash_table_remove_string (activation->pending_activations,
1911 pending_activation->service_name);
1916 if (was_pending_activation)
1919 if (bus_context_get_systemd_activation (activation->context))
1921 if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1922 /* systemd itself is missing apparently. That can happen
1923 only during early startup. Let's just wait until systemd
1924 connects to us and do nothing. */
1927 if (entry->systemd_service)
1929 BusTransaction *activation_transaction;
1930 DBusString service_string;
1931 BusService *service;
1932 BusRegistry *registry;
1934 /* OK, we have a systemd service configured for this entry,
1935 hence let's enqueue an activation request message. This
1936 is implemented as a directed signal, not a method call,
1937 for three reasons: 1) we don't expect a response on
1938 success, where we just expect a name appearing on the
1939 bus; 2) at this time the systemd service might not yet
1940 have connected, so we wouldn't know the message serial at
1941 this point to set up a pending call; 3) it is ugly if the
1942 bus suddenly becomes the caller of a remote method. */
1944 message = dbus_message_new_signal (DBUS_PATH_DBUS,
1945 "org.freedesktop.systemd1.Activator",
1946 "ActivationRequest");
1949 _dbus_verbose ("No memory to create activation message\n");
1950 BUS_SET_OOM (error);
1954 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1955 !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1956 !dbus_message_append_args (message,
1957 DBUS_TYPE_STRING, &entry->systemd_service,
1960 _dbus_verbose ("No memory to set args of activation message\n");
1961 dbus_message_unref (message);
1962 BUS_SET_OOM (error);
1966 /* Create our transaction */
1967 activation_transaction = bus_transaction_new (activation->context);
1968 if (activation_transaction == NULL)
1970 _dbus_verbose ("No memory to create activation transaction\n");
1971 dbus_message_unref (message);
1972 BUS_SET_OOM (error);
1976 /* Check whether systemd is already connected */
1977 registry = bus_connection_get_registry (connection);
1978 _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1979 service = bus_registry_lookup (registry, &service_string);
1981 if (service != NULL)
1983 bus_context_log (activation->context,
1984 DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
1986 entry->systemd_service);
1987 /* Wonderful, systemd is connected, let's just send the msg */
1988 retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1993 bus_context_log (activation->context,
1994 DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
1996 entry->systemd_service);
1997 /* systemd is not around, let's "activate" it. */
1998 retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1999 message, "org.freedesktop.systemd1", error);
2002 dbus_message_unref (message);
2006 bus_context_log (activation->context,
2007 DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
2009 entry->systemd_service);
2010 _DBUS_ASSERT_ERROR_IS_SET (error);
2011 _dbus_verbose ("failed to send activation message: %s\n", error->name);
2012 bus_transaction_cancel_and_free (activation_transaction);
2016 bus_transaction_execute_and_free (activation_transaction);
2020 /* OK, we have no configured systemd service, hence let's
2021 proceed with traditional activation. */
2024 /* use command as system and session different */
2025 if (!_dbus_string_init (&command))
2027 BUS_SET_OOM (error);
2031 /* does the bus use a helper? */
2032 servicehelper = bus_context_get_servicehelper (activation->context);
2033 if (servicehelper != NULL)
2035 if (entry->user == NULL)
2037 _dbus_string_free (&command);
2038 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2039 "Cannot do system-bus activation with no user\n");
2043 /* join the helper path and the service name */
2044 if (!_dbus_string_append (&command, servicehelper))
2046 _dbus_string_free (&command);
2047 BUS_SET_OOM (error);
2050 if (!_dbus_string_append (&command, " "))
2052 _dbus_string_free (&command);
2053 BUS_SET_OOM (error);
2056 if (!_dbus_string_append (&command, service_name))
2058 _dbus_string_free (&command);
2059 BUS_SET_OOM (error);
2065 /* the bus does not use a helper, so we can append arguments with the exec line */
2066 if (!_dbus_string_append (&command, entry->exec))
2068 _dbus_string_free (&command);
2069 BUS_SET_OOM (error);
2074 /* convert command into arguments */
2075 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2077 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2078 _DBUS_ASSERT_ERROR_IS_SET (error);
2080 _dbus_hash_table_remove_string (activation->pending_activations,
2081 pending_activation->service_name);
2083 _dbus_string_free (&command);
2086 _dbus_string_free (&command);
2088 if (!add_bus_environment (activation, error))
2090 _DBUS_ASSERT_ERROR_IS_SET (error);
2091 dbus_free_string_array (argv);
2095 envp = bus_activation_get_environment (activation);
2099 BUS_SET_OOM (error);
2100 dbus_free_string_array (argv);
2104 _dbus_verbose ("Spawning %s ...\n", argv[0]);
2105 if (servicehelper != NULL)
2106 bus_context_log (activation->context,
2107 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
2110 bus_context_log (activation->context,
2111 DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
2114 dbus_error_init (&tmp_error);
2116 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2121 _dbus_verbose ("Failed to spawn child\n");
2122 bus_context_log (activation->context,
2123 DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
2126 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2127 dbus_move_error (&tmp_error, error);
2128 dbus_free_string_array (argv);
2129 dbus_free_string_array (envp);
2134 dbus_free_string_array (argv);
2137 _dbus_assert (pending_activation->babysitter != NULL);
2139 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2140 add_babysitter_watch,
2141 remove_babysitter_watch,
2146 BUS_SET_OOM (error);
2147 _dbus_verbose ("Failed to set babysitter watch functions\n");
2155 bus_activation_list_services (BusActivation *activation,
2163 len = _dbus_hash_table_get_n_entries (activation->entries);
2164 retval = dbus_new (char *, len + 1);
2169 _dbus_hash_iter_init (activation->entries, &iter);
2171 while (_dbus_hash_iter_next (&iter))
2173 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2175 retval[i] = _dbus_strdup (entry->name);
2176 if (retval[i] == NULL)
2191 for (j = 0; j < i; j++)
2192 dbus_free (retval[i]);
2199 dbus_activation_systemd_failure (BusActivation *activation,
2200 DBusMessage *message)
2203 const char *code, *str, *unit = NULL;
2205 dbus_error_init(&error);
2207 /* This is called whenever the systemd activator sent us a
2208 response. We'll invalidate all pending activations that match the
2211 if (dbus_message_get_args (message, &error,
2212 DBUS_TYPE_STRING, &unit,
2213 DBUS_TYPE_STRING, &code,
2214 DBUS_TYPE_STRING, &str,
2216 dbus_set_error(&error, code, str);
2223 bus_context_log (activation->context,
2224 DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
2228 _dbus_hash_iter_init (activation->pending_activations,
2231 while (_dbus_hash_iter_next (&iter))
2233 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2235 if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2236 pending_activation_failed(p, &error);
2240 dbus_error_free(&error);
2245 #ifdef DBUS_BUILD_TESTS
2249 #define SERVICE_NAME_1 "MyService1"
2250 #define SERVICE_NAME_2 "MyService2"
2251 #define SERVICE_NAME_3 "MyService3"
2253 #define SERVICE_FILE_1 "service-1.service"
2254 #define SERVICE_FILE_2 "service-2.service"
2255 #define SERVICE_FILE_3 "service-3.service"
2258 test_create_service_file (DBusString *dir,
2259 const char *filename,
2263 DBusString file_name, full_path;
2265 dbus_bool_t ret_val;
2268 _dbus_string_init_const (&file_name, filename);
2270 if (!_dbus_string_init (&full_path))
2273 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2274 !_dbus_concat_dir_and_file (&full_path, &file_name))
2280 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2287 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2291 _dbus_string_free (&full_path);
2296 test_remove_service_file (DBusString *dir, const char *filename)
2298 DBusString file_name, full_path;
2299 dbus_bool_t ret_val;
2303 _dbus_string_init_const (&file_name, filename);
2305 if (!_dbus_string_init (&full_path))
2308 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2309 !_dbus_concat_dir_and_file (&full_path, &file_name))
2315 if (!_dbus_delete_file (&full_path, NULL))
2322 _dbus_string_free (&full_path);
2327 test_remove_directory (DBusString *dir)
2330 DBusString filename, full_path;
2331 dbus_bool_t ret_val;
2335 if (!_dbus_string_init (&filename))
2338 if (!_dbus_string_init (&full_path))
2340 _dbus_string_free (&filename);
2344 iter = _dbus_directory_open (dir, NULL);
2351 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2353 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2359 _dbus_directory_close (iter);
2361 if (!_dbus_delete_directory (dir, NULL))
2368 _dbus_string_free (&filename);
2369 _dbus_string_free (&full_path);
2375 init_service_reload_test (DBusString *dir)
2379 if (!_dbus_stat (dir, &stat_buf, NULL))
2381 if (!_dbus_create_directory (dir, NULL))
2386 if (!test_remove_directory (dir))
2389 if (!_dbus_create_directory (dir, NULL))
2393 /* Create one initial file */
2394 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2401 cleanup_service_reload_test (DBusString *dir)
2403 if (!test_remove_directory (dir))
2411 BusActivation *activation;
2412 const char *service_name;
2413 dbus_bool_t expecting_find;
2417 check_func (void *data)
2420 BusActivationEntry *entry;
2422 dbus_bool_t ret_val;
2427 dbus_error_init (&error);
2429 entry = activation_find_entry (d->activation, d->service_name, &error);
2432 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2438 if (d->expecting_find)
2442 dbus_error_free (&error);
2446 if (!d->expecting_find)
2454 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2459 err = !_dbus_test_oom_handling (description, check_func, data);
2461 err = !check_func (data);
2464 _dbus_assert_not_reached ("Test failed");
2470 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2472 BusActivation *activation;
2474 DBusList *directories;
2478 _dbus_string_init_const (&address, "");
2480 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2483 activation = bus_activation_new (NULL, &address, &directories, NULL);
2487 d.activation = activation;
2489 /* Check for existing service file */
2490 d.expecting_find = TRUE;
2491 d.service_name = SERVICE_NAME_1;
2493 if (!do_test ("Existing service file", oom_test, &d))
2496 /* Check for non-existing service file */
2497 d.expecting_find = FALSE;
2498 d.service_name = SERVICE_NAME_3;
2500 if (!do_test ("Nonexisting service file", oom_test, &d))
2503 /* Check for added service file */
2504 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2507 d.expecting_find = TRUE;
2508 d.service_name = SERVICE_NAME_2;
2510 if (!do_test ("Added service file", oom_test, &d))
2513 /* Check for removed service file */
2514 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2517 d.expecting_find = FALSE;
2518 d.service_name = SERVICE_FILE_2;
2520 if (!do_test ("Removed service file", oom_test, &d))
2523 /* Check for updated service file */
2525 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2527 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2530 d.expecting_find = TRUE;
2531 d.service_name = SERVICE_NAME_3;
2533 if (!do_test ("Updated service file, part 1", oom_test, &d))
2536 d.expecting_find = FALSE;
2537 d.service_name = SERVICE_NAME_1;
2539 if (!do_test ("Updated service file, part 2", oom_test, &d))
2542 bus_activation_unref (activation);
2543 _dbus_list_clear (&directories);
2549 bus_activation_service_reload_test (const DBusString *test_data_dir)
2551 DBusString directory;
2553 if (!_dbus_string_init (&directory))
2556 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2559 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2560 !_dbus_generate_random_ascii (&directory, 6))
2565 /* Do normal tests */
2566 if (!init_service_reload_test (&directory))
2567 _dbus_assert_not_reached ("could not initiate service reload test");
2569 if (!do_service_reload_test (&directory, FALSE))
2573 if (!init_service_reload_test (&directory))
2574 _dbus_assert_not_reached ("could not initiate service reload test");
2576 if (!do_service_reload_test (&directory, TRUE))
2579 /* Cleanup test directory */
2580 if (!cleanup_service_reload_test (&directory))
2583 _dbus_string_free (&directory);
2588 #endif /* DBUS_BUILD_TESTS */