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
25 #include "activation.h"
26 #include "activation-exit-codes.h"
27 #include "desktop-file.h"
31 #include <dbus/dbus-internals.h>
32 #include <dbus/dbus-hash.h>
33 #include <dbus/dbus-list.h>
34 #include <dbus/dbus-shell.h>
35 #include <dbus/dbus-spawn.h>
36 #include <dbus/dbus-timeout.h>
37 #include <dbus/dbus-sysdeps.h>
45 DBusHashTable *entries;
46 DBusHashTable *pending_activations;
49 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
50 * i.e. number of pending activation requests, not pending
53 DBusHashTable *directories;
54 DBusHashTable *environment;
61 DBusHashTable *entries;
62 } BusServiceDirectory;
71 BusServiceDirectory *s_dir;
75 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
77 struct BusPendingActivationEntry
79 DBusMessage *activation_message;
80 DBusConnection *connection;
82 dbus_bool_t auto_activation;
88 BusActivation *activation;
93 DBusBabysitter *babysitter;
95 unsigned int timeout_added : 1;
96 } BusPendingActivation;
99 static BusServiceDirectory *
100 bus_service_directory_ref (BusServiceDirectory *dir)
102 _dbus_assert (dir->refcount);
111 bus_service_directory_unref (BusServiceDirectory *dir)
116 _dbus_assert (dir->refcount > 0);
119 if (dir->refcount > 0)
123 _dbus_hash_table_unref (dir->entries);
125 dbus_free (dir->dir_c);
130 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
132 if (entry->activation_message)
133 dbus_message_unref (entry->activation_message);
135 if (entry->connection)
136 dbus_connection_unref (entry->connection);
142 handle_timeout_callback (DBusTimeout *timeout,
145 BusPendingActivation *pending_activation = data;
147 while (!dbus_timeout_handle (pending_activation->timeout))
148 _dbus_wait_for_memory ();
151 static BusPendingActivation *
152 bus_pending_activation_ref (BusPendingActivation *pending_activation)
154 _dbus_assert (pending_activation->refcount > 0);
155 pending_activation->refcount += 1;
157 return pending_activation;
161 bus_pending_activation_unref (BusPendingActivation *pending_activation)
165 if (pending_activation == NULL) /* hash table requires this */
168 _dbus_assert (pending_activation->refcount > 0);
169 pending_activation->refcount -= 1;
171 if (pending_activation->refcount > 0)
174 if (pending_activation->timeout_added)
176 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
177 pending_activation->timeout,
178 handle_timeout_callback, pending_activation);
179 pending_activation->timeout_added = FALSE;
182 if (pending_activation->timeout)
183 _dbus_timeout_unref (pending_activation->timeout);
185 if (pending_activation->babysitter)
187 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
189 pending_activation->babysitter,
191 _dbus_assert_not_reached ("setting watch functions to NULL failed");
193 _dbus_babysitter_unref (pending_activation->babysitter);
196 dbus_free (pending_activation->service_name);
197 dbus_free (pending_activation->exec);
199 link = _dbus_list_get_first_link (&pending_activation->entries);
203 BusPendingActivationEntry *entry = link->data;
205 bus_pending_activation_entry_free (entry);
207 link = _dbus_list_get_next_link (&pending_activation->entries, link);
209 _dbus_list_clear (&pending_activation->entries);
211 pending_activation->activation->n_pending_activations -=
212 pending_activation->n_entries;
214 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
216 dbus_free (pending_activation);
219 static BusActivationEntry *
220 bus_activation_entry_ref (BusActivationEntry *entry)
222 _dbus_assert (entry->refcount > 0);
229 bus_activation_entry_unref (BusActivationEntry *entry)
231 if (entry == NULL) /* hash table requires this */
234 _dbus_assert (entry->refcount > 0);
237 if (entry->refcount > 0)
240 dbus_free (entry->name);
241 dbus_free (entry->exec);
242 dbus_free (entry->user);
243 dbus_free (entry->filename);
249 update_desktop_file_entry (BusActivation *activation,
250 BusServiceDirectory *s_dir,
251 DBusString *filename,
252 BusDesktopFile *desktop_file,
255 char *name, *exec, *user;
256 BusActivationEntry *entry;
258 DBusString file_path;
261 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
268 dbus_error_init (&tmp_error);
270 if (!_dbus_string_init (&file_path))
276 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
277 !_dbus_concat_dir_and_file (&file_path, filename))
283 if (!_dbus_stat (&file_path, &stat_buf, NULL))
285 dbus_set_error (error, DBUS_ERROR_FAILED,
286 "Can't stat the service file\n");
290 if (!bus_desktop_file_get_string (desktop_file,
291 DBUS_SERVICE_SECTION,
297 if (!bus_desktop_file_get_string (desktop_file,
298 DBUS_SERVICE_SECTION,
304 /* user is not _required_ unless we are using system activation */
305 if (!bus_desktop_file_get_string (desktop_file,
306 DBUS_SERVICE_SECTION,
310 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
311 /* if we got OOM, then exit */
312 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
314 dbus_move_error (&tmp_error, error);
319 /* if we have error because we didn't find anything then continue */
320 dbus_error_free (&tmp_error);
325 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
327 entry = _dbus_hash_table_lookup_string (s_dir->entries,
328 _dbus_string_get_const_data (filename));
329 if (entry == NULL) /* New file */
331 /* FIXME we need a better-defined algorithm for which service file to
332 * pick than "whichever one is first in the directory listing"
334 if (_dbus_hash_table_lookup_string (activation->entries, name))
336 dbus_set_error (error, DBUS_ERROR_FAILED,
337 "Service %s already exists in activation entry list\n", name);
341 entry = dbus_new0 (BusActivationEntry, 1);
353 entry->s_dir = s_dir;
354 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
355 if (!entry->filename)
361 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
367 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
369 /* Revert the insertion in the entries table */
370 _dbus_hash_table_remove_string (activation->entries, entry->name);
375 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
377 else /* Just update the entry */
379 bus_activation_entry_ref (entry);
380 _dbus_hash_table_remove_string (activation->entries, entry->name);
382 if (_dbus_hash_table_lookup_string (activation->entries, name))
384 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
385 name, _dbus_string_get_const_data (&file_path));
389 dbus_free (entry->name);
390 dbus_free (entry->exec);
391 dbus_free (entry->user);
395 if (!_dbus_hash_table_insert_string (activation->entries,
396 entry->name, bus_activation_entry_ref(entry)))
399 /* Also remove path to entries hash since we want this in sync with
400 * the entries hash table */
401 _dbus_hash_table_remove_string (entry->s_dir->entries,
403 bus_activation_entry_unref (entry);
408 entry->mtime = stat_buf.mtime;
410 _dbus_string_free (&file_path);
411 bus_activation_entry_unref (entry);
419 _dbus_string_free (&file_path);
422 bus_activation_entry_unref (entry);
428 check_service_file (BusActivation *activation,
429 BusActivationEntry *entry,
430 BusActivationEntry **updated_entry,
435 BusActivationEntry *tmp_entry;
436 DBusString file_path;
442 _dbus_string_init_const (&filename, entry->filename);
444 if (!_dbus_string_init (&file_path))
450 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
451 !_dbus_concat_dir_and_file (&file_path, &filename))
458 if (!_dbus_stat (&file_path, &stat_buf, NULL))
460 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
461 _dbus_string_get_const_data (&file_path));
463 _dbus_hash_table_remove_string (activation->entries, entry->name);
464 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
472 if (stat_buf.mtime > entry->mtime)
474 BusDesktopFile *desktop_file;
477 dbus_error_init (&tmp_error);
479 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
480 if (desktop_file == NULL)
482 _dbus_verbose ("Could not load %s: %s\n",
483 _dbus_string_get_const_data (&file_path),
485 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
487 dbus_move_error (&tmp_error, error);
491 dbus_error_free (&tmp_error);
496 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
497 * Handle these both better
499 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
501 bus_desktop_file_free (desktop_file);
502 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
504 dbus_move_error (&tmp_error, error);
508 dbus_error_free (&tmp_error);
513 bus_desktop_file_free (desktop_file);
519 _dbus_string_free (&file_path);
521 if (updated_entry != NULL)
522 *updated_entry = tmp_entry;
527 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
528 * hash entries it already added.
531 update_directory (BusActivation *activation,
532 BusServiceDirectory *s_dir,
536 DBusString dir, filename;
537 BusDesktopFile *desktop_file;
540 BusActivationEntry *entry;
541 DBusString full_path;
543 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
548 _dbus_string_init_const (&dir, s_dir->dir_c);
550 if (!_dbus_string_init (&filename))
556 if (!_dbus_string_init (&full_path))
559 _dbus_string_free (&filename);
565 /* from this point it's safe to "goto out" */
567 iter = _dbus_directory_open (&dir, error);
570 _dbus_verbose ("Failed to open directory %s: %s\n",
572 error ? error->message : "unknown");
576 /* Now read the files */
577 dbus_error_init (&tmp_error);
578 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
580 _dbus_assert (!dbus_error_is_set (&tmp_error));
582 _dbus_string_set_length (&full_path, 0);
584 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
586 _dbus_verbose ("Skipping non-.service file %s\n",
587 _dbus_string_get_const_data (&filename));
591 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
592 if (entry) /* Already has this service file in the cache */
594 if (!check_service_file (activation, entry, NULL, error))
600 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
601 !_dbus_concat_dir_and_file (&full_path, &filename))
608 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
609 if (desktop_file == NULL)
611 _dbus_verbose ("Could not load %s: %s\n",
612 _dbus_string_get_const_data (&full_path),
615 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
617 dbus_move_error (&tmp_error, error);
621 dbus_error_free (&tmp_error);
625 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
626 * Handle these both better
628 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
630 bus_desktop_file_free (desktop_file);
633 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
634 _dbus_string_get_const_data (&full_path), tmp_error.message);
636 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
638 dbus_move_error (&tmp_error, error);
642 dbus_error_free (&tmp_error);
647 bus_desktop_file_free (desktop_file);
653 if (dbus_error_is_set (&tmp_error))
655 dbus_move_error (&tmp_error, error);
663 _DBUS_ASSERT_ERROR_IS_SET (error);
665 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
668 _dbus_directory_close (iter);
669 _dbus_string_free (&filename);
670 _dbus_string_free (&full_path);
676 populate_environment (BusActivation *activation)
682 dbus_bool_t retval = FALSE;
684 environment = _dbus_get_environment ();
686 if (environment == NULL)
689 if (!_dbus_string_init (&key))
691 dbus_free_string_array (environment);
695 if (!_dbus_string_init (&value))
697 _dbus_string_free (&key);
698 dbus_free_string_array (environment);
702 for (i = 0; environment[i] != NULL; i++)
704 if (!_dbus_string_append (&key, environment[i]))
707 if (_dbus_string_split_on_byte (&key, '=', &value))
709 char *hash_key, *hash_value;
711 if (!_dbus_string_steal_data (&key, &hash_key))
714 if (!_dbus_string_steal_data (&value, &hash_value))
717 if (!_dbus_hash_table_insert_string (activation->environment,
718 hash_key, hash_value))
721 _dbus_string_set_length (&key, 0);
722 _dbus_string_set_length (&value, 0);
725 if (environment[i] != NULL)
731 _dbus_string_free (&key);
732 _dbus_string_free (&value);
733 dbus_free_string_array (environment);
739 bus_activation_reload (BusActivation *activation,
740 const DBusString *address,
741 DBusList **directories,
747 if (activation->server_address != NULL)
748 dbus_free (activation->server_address);
749 if (!_dbus_string_copy_data (address, &activation->server_address))
755 if (activation->entries != NULL)
756 _dbus_hash_table_unref (activation->entries);
757 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
758 (DBusFreeFunction)bus_activation_entry_unref);
759 if (activation->entries == NULL)
765 if (activation->directories != NULL)
766 _dbus_hash_table_unref (activation->directories);
767 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
768 (DBusFreeFunction)bus_service_directory_unref);
770 if (activation->directories == NULL)
776 link = _dbus_list_get_first_link (directories);
779 BusServiceDirectory *s_dir;
781 dir = _dbus_strdup ((const char *) link->data);
788 s_dir = dbus_new0 (BusServiceDirectory, 1);
799 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
800 (DBusFreeFunction)bus_activation_entry_unref);
804 bus_service_directory_unref (s_dir);
809 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
811 bus_service_directory_unref (s_dir);
816 /* only fail on OOM, it is ok if we can't read the directory */
817 if (!update_directory (activation, s_dir, error))
819 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
822 dbus_error_free (error);
825 link = _dbus_list_get_next_link (directories, link);
834 bus_activation_new (BusContext *context,
835 const DBusString *address,
836 DBusList **directories,
839 BusActivation *activation;
843 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
845 activation = dbus_new0 (BusActivation, 1);
846 if (activation == NULL)
852 activation->refcount = 1;
853 activation->context = context;
854 activation->n_pending_activations = 0;
856 if (!bus_activation_reload (activation, address, directories, error))
859 /* Initialize this hash table once, we don't want to lose pending
860 * activations on reload. */
861 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
862 (DBusFreeFunction)bus_pending_activation_unref);
864 if (activation->pending_activations == NULL)
870 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
871 (DBusFreeFunction) dbus_free,
872 (DBusFreeFunction) dbus_free);
874 if (activation->environment == NULL)
880 if (!populate_environment (activation))
889 bus_activation_unref (activation);
894 bus_activation_ref (BusActivation *activation)
896 _dbus_assert (activation->refcount > 0);
898 activation->refcount += 1;
904 bus_activation_unref (BusActivation *activation)
906 _dbus_assert (activation->refcount > 0);
908 activation->refcount -= 1;
910 if (activation->refcount > 0)
913 dbus_free (activation->server_address);
914 if (activation->entries)
915 _dbus_hash_table_unref (activation->entries);
916 if (activation->pending_activations)
917 _dbus_hash_table_unref (activation->pending_activations);
918 if (activation->directories)
919 _dbus_hash_table_unref (activation->directories);
920 if (activation->environment)
921 _dbus_hash_table_unref (activation->environment);
923 dbus_free (activation);
927 add_bus_environment (BusActivation *activation,
932 if (!bus_activation_set_environment_variable (activation,
933 "DBUS_STARTER_ADDRESS",
934 activation->server_address,
938 type = bus_context_get_type (activation->context);
941 if (!bus_activation_set_environment_variable (activation,
942 "DBUS_STARTER_BUS_TYPE", type,
946 if (strcmp (type, "session") == 0)
948 if (!bus_activation_set_environment_variable (activation,
949 "DBUS_SESSION_BUS_ADDRESS",
950 activation->server_address,
954 else if (strcmp (type, "system") == 0)
956 if (!bus_activation_set_environment_variable (activation,
957 "DBUS_SYSTEM_BUS_ADDRESS",
958 activation->server_address,
969 BusPendingActivation *pending_activation;
970 DBusPreallocatedHash *hash_entry;
971 } RestorePendingData;
974 restore_pending (void *data)
976 RestorePendingData *d = data;
978 _dbus_assert (d->pending_activation != NULL);
979 _dbus_assert (d->hash_entry != NULL);
981 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
982 d->pending_activation->service_name,
983 d->pending_activation->timeout_added);
985 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
987 d->pending_activation->service_name, d->pending_activation);
989 bus_pending_activation_ref (d->pending_activation);
991 d->hash_entry = NULL;
995 free_pending_restore_data (void *data)
997 RestorePendingData *d = data;
1000 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1003 bus_pending_activation_unref (d->pending_activation);
1009 add_restore_pending_to_transaction (BusTransaction *transaction,
1010 BusPendingActivation *pending_activation)
1012 RestorePendingData *d;
1014 d = dbus_new (RestorePendingData, 1);
1018 d->pending_activation = pending_activation;
1019 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1021 bus_pending_activation_ref (d->pending_activation);
1023 if (d->hash_entry == NULL ||
1024 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1025 free_pending_restore_data))
1027 free_pending_restore_data (d);
1031 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1037 bus_activation_service_created (BusActivation *activation,
1038 const char *service_name,
1039 BusTransaction *transaction,
1042 BusPendingActivation *pending_activation;
1043 DBusMessage *message;
1046 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1048 /* Check if it's a pending activation */
1049 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1051 if (!pending_activation)
1054 link = _dbus_list_get_first_link (&pending_activation->entries);
1055 while (link != NULL)
1057 BusPendingActivationEntry *entry = link->data;
1058 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1060 if (dbus_connection_get_is_connected (entry->connection))
1062 /* Only send activation replies to regular activation requests. */
1063 if (!entry->auto_activation)
1065 dbus_uint32_t result;
1067 message = dbus_message_new_method_return (entry->activation_message);
1070 BUS_SET_OOM (error);
1074 result = DBUS_START_REPLY_SUCCESS;
1076 if (!dbus_message_append_args (message,
1077 DBUS_TYPE_UINT32, &result,
1080 dbus_message_unref (message);
1081 BUS_SET_OOM (error);
1085 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1087 dbus_message_unref (message);
1088 BUS_SET_OOM (error);
1092 dbus_message_unref (message);
1106 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1107 BusService *service,
1108 BusTransaction *transaction,
1111 BusPendingActivation *pending_activation;
1114 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1116 /* Check if it's a pending activation */
1117 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1118 bus_service_get_name (service));
1120 if (!pending_activation)
1123 link = _dbus_list_get_first_link (&pending_activation->entries);
1124 while (link != NULL)
1126 BusPendingActivationEntry *entry = link->data;
1127 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1129 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1131 DBusConnection *addressed_recipient;
1133 addressed_recipient = bus_service_get_primary_owners_connection (service);
1135 /* Check the security policy, which has the side-effect of adding an
1136 * expected pending reply.
1138 if (!bus_context_check_security_policy (activation->context, transaction,
1140 addressed_recipient,
1141 addressed_recipient,
1142 entry->activation_message, error))
1145 if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
1147 BUS_SET_OOM (error);
1155 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1157 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1158 BUS_SET_OOM (error);
1162 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1171 * FIXME @todo the error messages here would ideally be preallocated
1172 * so we don't need to allocate memory to send them.
1173 * Using the usual tactic, prealloc an OOM message, then
1174 * if we can't alloc the real error send the OOM error instead.
1177 try_send_activation_failure (BusPendingActivation *pending_activation,
1178 const DBusError *how)
1180 BusActivation *activation;
1182 BusTransaction *transaction;
1184 activation = pending_activation->activation;
1186 transaction = bus_transaction_new (activation->context);
1187 if (transaction == NULL)
1190 link = _dbus_list_get_first_link (&pending_activation->entries);
1191 while (link != NULL)
1193 BusPendingActivationEntry *entry = link->data;
1194 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1196 if (dbus_connection_get_is_connected (entry->connection))
1198 if (!bus_transaction_send_error_reply (transaction,
1201 entry->activation_message))
1208 bus_transaction_execute_and_free (transaction);
1214 bus_transaction_cancel_and_free (transaction);
1219 * Free the pending activation and send an error message to all the
1220 * connections that were waiting for it.
1223 pending_activation_failed (BusPendingActivation *pending_activation,
1224 const DBusError *how)
1226 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1227 while (!try_send_activation_failure (pending_activation, how))
1228 _dbus_wait_for_memory ();
1230 /* Destroy this pending activation */
1231 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1232 pending_activation->service_name);
1236 * Depending on the exit code of the helper, set the error accordingly
1239 handle_servicehelper_exit_error (int exit_code,
1244 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1245 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1246 "Launcher could not run (out of memory)");
1248 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1249 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1250 "Failed to setup environment correctly");
1252 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1253 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1254 "Bus name is not valid or missing");
1256 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1257 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1258 "Bus name not found in system service directory");
1260 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1261 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1262 "The permission of the setuid helper is not correct");
1264 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1265 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1266 "The service file is incorrect or does not have all required attributes");
1268 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1269 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1270 "Cannot launch daemon, file not found or permissions invalid");
1272 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1273 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1274 "Invalid arguments to command line");
1276 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1277 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1278 "Launched child was signaled, it probably crashed");
1281 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1282 "Launch helper exited with unknown return code %i", exit_code);
1288 babysitter_watch_callback (DBusWatch *watch,
1289 unsigned int condition,
1292 BusPendingActivation *pending_activation = data;
1294 DBusBabysitter *babysitter;
1295 dbus_bool_t uses_servicehelper;
1297 babysitter = pending_activation->babysitter;
1299 _dbus_babysitter_ref (babysitter);
1301 retval = dbus_watch_handle (watch, condition);
1303 /* There are two major cases here; are we the system bus or the session? Here this
1304 * is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
1305 * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1307 * In both cases though, just ignore when a process exits with status 0; it's possible for
1308 * a program to (misguidedly) "daemonize", and that appears to us as an exit. This closes a race
1309 * condition between this code and the child process claiming the bus name.
1311 uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1313 /* FIXME this is broken in the same way that
1314 * connection watches used to be; there should be
1315 * a separate callback for status change, instead
1316 * of doing "if we handled a watch status might
1319 * Fixing this lets us move dbus_watch_handle
1320 * calls into dbus-mainloop.c
1322 if (_dbus_babysitter_get_child_exited (babysitter))
1326 dbus_bool_t activation_failed;
1329 dbus_error_init (&error);
1331 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1333 /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1335 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1336 && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1338 activation_failed = exit_code != 0;
1340 dbus_error_free(&error);
1342 if (activation_failed)
1344 if (uses_servicehelper)
1345 handle_servicehelper_exit_error (exit_code, &error);
1347 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1352 activation_failed = TRUE;
1355 if (activation_failed)
1357 /* Destroy all pending activations with the same exec */
1358 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1360 while (_dbus_hash_iter_next (&iter))
1362 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1364 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1365 pending_activation_failed (p, &error);
1368 /* Destroys the pending activation */
1369 pending_activation_failed (pending_activation, &error);
1371 dbus_error_free (&error);
1375 _dbus_babysitter_unref (babysitter);
1381 add_babysitter_watch (DBusWatch *watch,
1384 BusPendingActivation *pending_activation = data;
1386 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1387 watch, babysitter_watch_callback, pending_activation,
1392 remove_babysitter_watch (DBusWatch *watch,
1395 BusPendingActivation *pending_activation = data;
1397 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1398 watch, babysitter_watch_callback, pending_activation);
1402 pending_activation_timed_out (void *data)
1404 BusPendingActivation *pending_activation = data;
1407 /* Kill the spawned process, since it sucks
1408 * (not sure this is what we want to do, but
1409 * may as well try it for now)
1411 if (pending_activation->babysitter)
1412 _dbus_babysitter_kill_child (pending_activation->babysitter);
1414 dbus_error_init (&error);
1416 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1417 "Activation of %s timed out",
1418 pending_activation->service_name);
1420 pending_activation_failed (pending_activation, &error);
1422 dbus_error_free (&error);
1428 cancel_pending (void *data)
1430 BusPendingActivation *pending_activation = data;
1432 _dbus_verbose ("Canceling pending activation of %s\n",
1433 pending_activation->service_name);
1435 if (pending_activation->babysitter)
1436 _dbus_babysitter_kill_child (pending_activation->babysitter);
1438 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1439 pending_activation->service_name);
1443 free_pending_cancel_data (void *data)
1445 BusPendingActivation *pending_activation = data;
1447 bus_pending_activation_unref (pending_activation);
1451 add_cancel_pending_to_transaction (BusTransaction *transaction,
1452 BusPendingActivation *pending_activation)
1454 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1456 free_pending_cancel_data))
1459 bus_pending_activation_ref (pending_activation);
1461 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1467 update_service_cache (BusActivation *activation, DBusError *error)
1471 _dbus_hash_iter_init (activation->directories, &iter);
1472 while (_dbus_hash_iter_next (&iter))
1474 DBusError tmp_error;
1475 BusServiceDirectory *s_dir;
1477 s_dir = _dbus_hash_iter_get_value (&iter);
1479 dbus_error_init (&tmp_error);
1480 if (!update_directory (activation, s_dir, &tmp_error))
1482 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1484 dbus_move_error (&tmp_error, error);
1488 dbus_error_free (&tmp_error);
1496 static BusActivationEntry *
1497 activation_find_entry (BusActivation *activation,
1498 const char *service_name,
1501 BusActivationEntry *entry;
1503 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1506 if (!update_service_cache (activation, error))
1509 entry = _dbus_hash_table_lookup_string (activation->entries,
1514 BusActivationEntry *updated_entry;
1516 if (!check_service_file (activation, entry, &updated_entry, error))
1519 entry = updated_entry;
1524 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1525 "The name %s was not provided by any .service files",
1534 bus_activation_get_environment (BusActivation *activation)
1541 length = _dbus_hash_table_get_n_entries (activation->environment);
1543 environment = dbus_new0 (char *, length + 1);
1545 if (environment == NULL)
1549 _dbus_hash_iter_init (activation->environment, &iter);
1551 if (!_dbus_string_init (&entry))
1553 dbus_free_string_array (environment);
1557 while (_dbus_hash_iter_next (&iter))
1559 const char *key, *value;
1561 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1562 value = (const char *) _dbus_hash_iter_get_value (&iter);
1564 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1567 if (!_dbus_string_steal_data (&entry, environment + i))
1572 _dbus_string_free (&entry);
1576 dbus_free_string_array (environment);
1584 bus_activation_set_environment_variable (BusActivation *activation,
1596 hash_key = _dbus_strdup (key);
1598 if (hash_key == NULL)
1601 hash_value = _dbus_strdup (value);
1603 if (hash_value == NULL)
1606 if (!_dbus_hash_table_insert_string (activation->environment,
1607 hash_key, hash_value))
1612 if (retval == FALSE)
1614 dbus_free (hash_key);
1615 dbus_free (hash_value);
1616 BUS_SET_OOM (error);
1623 bus_activation_activate_service (BusActivation *activation,
1624 DBusConnection *connection,
1625 BusTransaction *transaction,
1626 dbus_bool_t auto_activation,
1627 DBusMessage *activation_message,
1628 const char *service_name,
1631 BusActivationEntry *entry;
1632 BusPendingActivation *pending_activation;
1633 BusPendingActivationEntry *pending_activation_entry;
1634 DBusMessage *message;
1635 DBusString service_str;
1636 const char *servicehelper;
1642 dbus_bool_t activated;
1647 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1649 if (activation->n_pending_activations >=
1650 bus_context_get_max_pending_activations (activation->context))
1652 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1653 "The maximum number of pending activations has been reached, activation of %s failed",
1658 entry = activation_find_entry (activation, service_name, error);
1662 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1663 * call us if the service is already active.
1665 if (!auto_activation)
1667 /* Check if the service is active */
1668 _dbus_string_init_const (&service_str, service_name);
1669 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1671 dbus_uint32_t result;
1673 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1675 message = dbus_message_new_method_return (activation_message);
1679 _dbus_verbose ("No memory to create reply to activate message\n");
1680 BUS_SET_OOM (error);
1684 result = DBUS_START_REPLY_ALREADY_RUNNING;
1686 if (!dbus_message_append_args (message,
1687 DBUS_TYPE_UINT32, &result,
1690 _dbus_verbose ("No memory to set args of reply to activate message\n");
1691 BUS_SET_OOM (error);
1692 dbus_message_unref (message);
1696 retval = bus_transaction_send_from_driver (transaction, connection, message);
1697 dbus_message_unref (message);
1700 _dbus_verbose ("Failed to send reply\n");
1701 BUS_SET_OOM (error);
1708 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1709 if (!pending_activation_entry)
1711 _dbus_verbose ("Failed to create pending activation entry\n");
1712 BUS_SET_OOM (error);
1716 pending_activation_entry->auto_activation = auto_activation;
1718 pending_activation_entry->activation_message = activation_message;
1719 dbus_message_ref (activation_message);
1720 pending_activation_entry->connection = connection;
1721 dbus_connection_ref (connection);
1723 /* Check if the service is being activated */
1724 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1725 if (pending_activation)
1727 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1729 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1731 BUS_SET_OOM (error);
1732 bus_pending_activation_entry_free (pending_activation_entry);
1736 pending_activation->n_entries += 1;
1737 pending_activation->activation->n_pending_activations += 1;
1741 pending_activation = dbus_new0 (BusPendingActivation, 1);
1742 if (!pending_activation)
1744 _dbus_verbose ("Failed to create pending activation\n");
1746 BUS_SET_OOM (error);
1747 bus_pending_activation_entry_free (pending_activation_entry);
1751 pending_activation->activation = activation;
1752 pending_activation->refcount = 1;
1754 pending_activation->service_name = _dbus_strdup (service_name);
1755 if (!pending_activation->service_name)
1757 _dbus_verbose ("Failed to copy service name for pending activation\n");
1759 BUS_SET_OOM (error);
1760 bus_pending_activation_unref (pending_activation);
1761 bus_pending_activation_entry_free (pending_activation_entry);
1765 pending_activation->exec = _dbus_strdup (entry->exec);
1766 if (!pending_activation->exec)
1768 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1769 BUS_SET_OOM (error);
1770 bus_pending_activation_unref (pending_activation);
1771 bus_pending_activation_entry_free (pending_activation_entry);
1775 pending_activation->timeout =
1776 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1777 pending_activation_timed_out,
1780 if (!pending_activation->timeout)
1782 _dbus_verbose ("Failed to create timeout for pending activation\n");
1784 BUS_SET_OOM (error);
1785 bus_pending_activation_unref (pending_activation);
1786 bus_pending_activation_entry_free (pending_activation_entry);
1790 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1791 pending_activation->timeout,
1792 handle_timeout_callback,
1796 _dbus_verbose ("Failed to add timeout for pending activation\n");
1798 BUS_SET_OOM (error);
1799 bus_pending_activation_unref (pending_activation);
1800 bus_pending_activation_entry_free (pending_activation_entry);
1804 pending_activation->timeout_added = TRUE;
1806 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1808 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1810 BUS_SET_OOM (error);
1811 bus_pending_activation_unref (pending_activation);
1812 bus_pending_activation_entry_free (pending_activation_entry);
1816 pending_activation->n_entries += 1;
1817 pending_activation->activation->n_pending_activations += 1;
1820 _dbus_hash_iter_init (activation->pending_activations, &iter);
1821 while (_dbus_hash_iter_next (&iter))
1823 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1825 if (strcmp (p->exec, entry->exec) == 0)
1832 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1833 pending_activation->service_name,
1834 pending_activation))
1836 _dbus_verbose ("Failed to put pending activation in hash table\n");
1838 BUS_SET_OOM (error);
1839 bus_pending_activation_unref (pending_activation);
1844 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1846 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1847 BUS_SET_OOM (error);
1848 _dbus_hash_table_remove_string (activation->pending_activations,
1849 pending_activation->service_name);
1857 /* use command as system and session different */
1858 if (!_dbus_string_init (&command))
1860 BUS_SET_OOM (error);
1864 /* does the bus use a helper? */
1865 servicehelper = bus_context_get_servicehelper (activation->context);
1866 if (servicehelper != NULL)
1868 if (entry->user == NULL)
1870 _dbus_string_free (&command);
1871 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
1872 "Cannot do system-bus activation with no user\n");
1876 /* join the helper path and the service name */
1877 if (!_dbus_string_append (&command, servicehelper))
1879 _dbus_string_free (&command);
1880 BUS_SET_OOM (error);
1883 if (!_dbus_string_append (&command, " "))
1885 _dbus_string_free (&command);
1886 BUS_SET_OOM (error);
1889 if (!_dbus_string_append (&command, service_name))
1891 _dbus_string_free (&command);
1892 BUS_SET_OOM (error);
1898 /* the bus does not use a helper, so we can append arguments with the exec line */
1899 if (!_dbus_string_append (&command, entry->exec))
1901 _dbus_string_free (&command);
1902 BUS_SET_OOM (error);
1907 /* convert command into arguments */
1908 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
1910 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
1911 _DBUS_ASSERT_ERROR_IS_SET (error);
1913 _dbus_hash_table_remove_string (activation->pending_activations,
1914 pending_activation->service_name);
1916 _dbus_string_free (&command);
1919 _dbus_string_free (&command);
1921 if (!add_bus_environment (activation, error))
1923 _DBUS_ASSERT_ERROR_IS_SET (error);
1924 dbus_free_string_array (argv);
1928 envp = bus_activation_get_environment (activation);
1932 BUS_SET_OOM (error);
1933 dbus_free_string_array (argv);
1937 _dbus_verbose ("Spawning %s ...\n", argv[0]);
1938 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1943 _dbus_verbose ("Failed to spawn child\n");
1944 _DBUS_ASSERT_ERROR_IS_SET (error);
1945 dbus_free_string_array (argv);
1946 dbus_free_string_array (envp);
1951 dbus_free_string_array (argv);
1954 _dbus_assert (pending_activation->babysitter != NULL);
1956 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1957 add_babysitter_watch,
1958 remove_babysitter_watch,
1963 BUS_SET_OOM (error);
1964 _dbus_verbose ("Failed to set babysitter watch functions\n");
1972 bus_activation_list_services (BusActivation *activation,
1980 len = _dbus_hash_table_get_n_entries (activation->entries);
1981 retval = dbus_new (char *, len + 1);
1986 _dbus_hash_iter_init (activation->entries, &iter);
1988 while (_dbus_hash_iter_next (&iter))
1990 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
1992 retval[i] = _dbus_strdup (entry->name);
1993 if (retval[i] == NULL)
2008 for (j = 0; j < i; j++)
2009 dbus_free (retval[i]);
2016 #ifdef DBUS_BUILD_TESTS
2020 #define SERVICE_NAME_1 "MyService1"
2021 #define SERVICE_NAME_2 "MyService2"
2022 #define SERVICE_NAME_3 "MyService3"
2024 #define SERVICE_FILE_1 "service-1.service"
2025 #define SERVICE_FILE_2 "service-2.service"
2026 #define SERVICE_FILE_3 "service-3.service"
2029 test_create_service_file (DBusString *dir,
2030 const char *filename,
2034 DBusString file_name, full_path;
2036 dbus_bool_t ret_val;
2039 _dbus_string_init_const (&file_name, filename);
2041 if (!_dbus_string_init (&full_path))
2044 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2045 !_dbus_concat_dir_and_file (&full_path, &file_name))
2051 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2058 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2062 _dbus_string_free (&full_path);
2067 test_remove_service_file (DBusString *dir, const char *filename)
2069 DBusString file_name, full_path;
2070 dbus_bool_t ret_val;
2074 _dbus_string_init_const (&file_name, filename);
2076 if (!_dbus_string_init (&full_path))
2079 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2080 !_dbus_concat_dir_and_file (&full_path, &file_name))
2086 if (!_dbus_delete_file (&full_path, NULL))
2093 _dbus_string_free (&full_path);
2098 test_remove_directory (DBusString *dir)
2101 DBusString filename, full_path;
2102 dbus_bool_t ret_val;
2106 if (!_dbus_string_init (&filename))
2109 if (!_dbus_string_init (&full_path))
2111 _dbus_string_free (&filename);
2115 iter = _dbus_directory_open (dir, NULL);
2122 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2124 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2130 _dbus_directory_close (iter);
2132 if (!_dbus_delete_directory (dir, NULL))
2139 _dbus_string_free (&filename);
2140 _dbus_string_free (&full_path);
2146 init_service_reload_test (DBusString *dir)
2150 if (!_dbus_stat (dir, &stat_buf, NULL))
2152 if (!_dbus_create_directory (dir, NULL))
2157 if (!test_remove_directory (dir))
2160 if (!_dbus_create_directory (dir, NULL))
2164 /* Create one initial file */
2165 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2172 cleanup_service_reload_test (DBusString *dir)
2174 if (!test_remove_directory (dir))
2182 BusActivation *activation;
2183 const char *service_name;
2184 dbus_bool_t expecting_find;
2188 check_func (void *data)
2191 BusActivationEntry *entry;
2193 dbus_bool_t ret_val;
2198 dbus_error_init (&error);
2200 entry = activation_find_entry (d->activation, d->service_name, &error);
2203 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2209 if (d->expecting_find)
2213 dbus_error_free (&error);
2217 if (!d->expecting_find)
2225 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2230 err = !_dbus_test_oom_handling (description, check_func, data);
2232 err = !check_func (data);
2235 _dbus_assert_not_reached ("Test failed");
2241 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2243 BusActivation *activation;
2245 DBusList *directories;
2249 _dbus_string_init_const (&address, "");
2251 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2254 activation = bus_activation_new (NULL, &address, &directories, NULL);
2258 d.activation = activation;
2260 /* Check for existing service file */
2261 d.expecting_find = TRUE;
2262 d.service_name = SERVICE_NAME_1;
2264 if (!do_test ("Existing service file", oom_test, &d))
2267 /* Check for non-existing service file */
2268 d.expecting_find = FALSE;
2269 d.service_name = SERVICE_NAME_3;
2271 if (!do_test ("Nonexisting service file", oom_test, &d))
2274 /* Check for added service file */
2275 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2278 d.expecting_find = TRUE;
2279 d.service_name = SERVICE_NAME_2;
2281 if (!do_test ("Added service file", oom_test, &d))
2284 /* Check for removed service file */
2285 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2288 d.expecting_find = FALSE;
2289 d.service_name = SERVICE_FILE_2;
2291 if (!do_test ("Removed service file", oom_test, &d))
2294 /* Check for updated service file */
2296 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2298 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2301 d.expecting_find = TRUE;
2302 d.service_name = SERVICE_NAME_3;
2304 if (!do_test ("Updated service file, part 1", oom_test, &d))
2307 d.expecting_find = FALSE;
2308 d.service_name = SERVICE_NAME_1;
2310 if (!do_test ("Updated service file, part 2", oom_test, &d))
2313 bus_activation_unref (activation);
2314 _dbus_list_clear (&directories);
2320 bus_activation_service_reload_test (const DBusString *test_data_dir)
2322 DBusString directory;
2324 if (!_dbus_string_init (&directory))
2327 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2330 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2331 !_dbus_generate_random_ascii (&directory, 6))
2336 /* Do normal tests */
2337 if (!init_service_reload_test (&directory))
2338 _dbus_assert_not_reached ("could not initiate service reload test");
2340 if (!do_service_reload_test (&directory, FALSE))
2344 if (!init_service_reload_test (&directory))
2345 _dbus_assert_not_reached ("could not initiate service reload test");
2347 if (!do_service_reload_test (&directory, TRUE))
2350 /* Cleanup test directory */
2351 if (!cleanup_service_reload_test (&directory))
2354 _dbus_string_free (&directory);
2359 #endif /* DBUS_BUILD_TESTS */