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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_new (BusContext *context,
740 const DBusString *address,
741 DBusList **directories,
744 BusActivation *activation;
748 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
750 activation = dbus_new0 (BusActivation, 1);
751 if (activation == NULL)
757 activation->refcount = 1;
758 activation->context = context;
759 activation->n_pending_activations = 0;
761 if (!_dbus_string_copy_data (address, &activation->server_address))
767 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
768 (DBusFreeFunction)bus_activation_entry_unref);
769 if (activation->entries == NULL)
775 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
776 (DBusFreeFunction)bus_pending_activation_unref);
778 if (activation->pending_activations == NULL)
784 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
785 (DBusFreeFunction)bus_service_directory_unref);
787 if (activation->directories == NULL)
793 /* Load service files */
794 link = _dbus_list_get_first_link (directories);
797 BusServiceDirectory *s_dir;
799 dir = _dbus_strdup ((const char *) link->data);
806 s_dir = dbus_new0 (BusServiceDirectory, 1);
817 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
818 (DBusFreeFunction)bus_activation_entry_unref);
822 bus_service_directory_unref (s_dir);
827 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
829 bus_service_directory_unref (s_dir);
834 /* only fail on OOM, it is ok if we can't read the directory */
835 if (!update_directory (activation, s_dir, error))
837 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
840 dbus_error_free (error);
843 link = _dbus_list_get_next_link (directories, link);
846 activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
847 (DBusFreeFunction) dbus_free,
848 (DBusFreeFunction) dbus_free);
850 if (activation->environment == NULL)
856 if (!populate_environment (activation))
865 bus_activation_unref (activation);
870 bus_activation_ref (BusActivation *activation)
872 _dbus_assert (activation->refcount > 0);
874 activation->refcount += 1;
880 bus_activation_unref (BusActivation *activation)
882 _dbus_assert (activation->refcount > 0);
884 activation->refcount -= 1;
886 if (activation->refcount > 0)
889 dbus_free (activation->server_address);
890 if (activation->entries)
891 _dbus_hash_table_unref (activation->entries);
892 if (activation->pending_activations)
893 _dbus_hash_table_unref (activation->pending_activations);
894 if (activation->directories)
895 _dbus_hash_table_unref (activation->directories);
896 if (activation->environment)
897 _dbus_hash_table_unref (activation->environment);
899 dbus_free (activation);
903 add_bus_environment (BusActivation *activation,
908 if (!bus_activation_set_environment_variable (activation,
909 "DBUS_STARTER_ADDRESS",
910 activation->server_address,
914 type = bus_context_get_type (activation->context);
917 if (!bus_activation_set_environment_variable (activation,
918 "DBUS_STARTER_BUS_TYPE", type,
922 if (strcmp (type, "session") == 0)
924 if (!bus_activation_set_environment_variable (activation,
925 "DBUS_SESSION_BUS_ADDRESS",
926 activation->server_address,
930 else if (strcmp (type, "system") == 0)
932 if (!bus_activation_set_environment_variable (activation,
933 "DBUS_SYSTEM_BUS_ADDRESS",
934 activation->server_address,
945 BusPendingActivation *pending_activation;
946 DBusPreallocatedHash *hash_entry;
947 } RestorePendingData;
950 restore_pending (void *data)
952 RestorePendingData *d = data;
954 _dbus_assert (d->pending_activation != NULL);
955 _dbus_assert (d->hash_entry != NULL);
957 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
958 d->pending_activation->service_name,
959 d->pending_activation->timeout_added);
961 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
963 d->pending_activation->service_name, d->pending_activation);
965 bus_pending_activation_ref (d->pending_activation);
967 d->hash_entry = NULL;
971 free_pending_restore_data (void *data)
973 RestorePendingData *d = data;
976 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
979 bus_pending_activation_unref (d->pending_activation);
985 add_restore_pending_to_transaction (BusTransaction *transaction,
986 BusPendingActivation *pending_activation)
988 RestorePendingData *d;
990 d = dbus_new (RestorePendingData, 1);
994 d->pending_activation = pending_activation;
995 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
997 bus_pending_activation_ref (d->pending_activation);
999 if (d->hash_entry == NULL ||
1000 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1001 free_pending_restore_data))
1003 free_pending_restore_data (d);
1007 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1013 bus_activation_service_created (BusActivation *activation,
1014 const char *service_name,
1015 BusTransaction *transaction,
1018 BusPendingActivation *pending_activation;
1019 DBusMessage *message;
1022 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1024 /* Check if it's a pending activation */
1025 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1027 if (!pending_activation)
1030 link = _dbus_list_get_first_link (&pending_activation->entries);
1031 while (link != NULL)
1033 BusPendingActivationEntry *entry = link->data;
1034 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1036 if (dbus_connection_get_is_connected (entry->connection))
1038 /* Only send activation replies to regular activation requests. */
1039 if (!entry->auto_activation)
1041 dbus_uint32_t result;
1043 message = dbus_message_new_method_return (entry->activation_message);
1046 BUS_SET_OOM (error);
1050 result = DBUS_START_REPLY_SUCCESS;
1052 if (!dbus_message_append_args (message,
1053 DBUS_TYPE_UINT32, &result,
1056 dbus_message_unref (message);
1057 BUS_SET_OOM (error);
1061 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1063 dbus_message_unref (message);
1064 BUS_SET_OOM (error);
1068 dbus_message_unref (message);
1082 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
1083 BusService *service,
1084 BusTransaction *transaction,
1087 BusPendingActivation *pending_activation;
1090 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1092 /* Check if it's a pending activation */
1093 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1094 bus_service_get_name (service));
1096 if (!pending_activation)
1099 link = _dbus_list_get_first_link (&pending_activation->entries);
1100 while (link != NULL)
1102 BusPendingActivationEntry *entry = link->data;
1103 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1105 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1107 DBusConnection *addressed_recipient;
1109 addressed_recipient = bus_service_get_primary_owners_connection (service);
1111 /* Check the security policy, which has the side-effect of adding an
1112 * expected pending reply.
1114 if (!bus_context_check_security_policy (activation->context, transaction,
1116 addressed_recipient,
1117 addressed_recipient,
1118 entry->activation_message, error))
1121 if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
1123 BUS_SET_OOM (error);
1131 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1133 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1134 BUS_SET_OOM (error);
1138 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1147 * FIXME @todo the error messages here would ideally be preallocated
1148 * so we don't need to allocate memory to send them.
1149 * Using the usual tactic, prealloc an OOM message, then
1150 * if we can't alloc the real error send the OOM error instead.
1153 try_send_activation_failure (BusPendingActivation *pending_activation,
1154 const DBusError *how)
1156 BusActivation *activation;
1158 BusTransaction *transaction;
1160 activation = pending_activation->activation;
1162 transaction = bus_transaction_new (activation->context);
1163 if (transaction == NULL)
1166 link = _dbus_list_get_first_link (&pending_activation->entries);
1167 while (link != NULL)
1169 BusPendingActivationEntry *entry = link->data;
1170 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1172 if (dbus_connection_get_is_connected (entry->connection))
1174 if (!bus_transaction_send_error_reply (transaction,
1177 entry->activation_message))
1184 bus_transaction_execute_and_free (transaction);
1190 bus_transaction_cancel_and_free (transaction);
1195 * Free the pending activation and send an error message to all the
1196 * connections that were waiting for it.
1199 pending_activation_failed (BusPendingActivation *pending_activation,
1200 const DBusError *how)
1202 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1203 while (!try_send_activation_failure (pending_activation, how))
1204 _dbus_wait_for_memory ();
1206 /* Destroy this pending activation */
1207 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1208 pending_activation->service_name);
1212 * Depending on the exit code of the helper, set the error accordingly
1215 handle_activation_exit_error (int exit_code,
1220 case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1221 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1222 "Launcher could not run (out of memory)");
1224 case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1225 dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1226 "Failed to setup environment correctly");
1228 case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1229 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1230 "Bus name is not valid or missing");
1232 case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1233 dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1234 "Bus name not found in system service directory");
1236 case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1237 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1238 "The permission of the setuid helper is not correct");
1240 case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1241 dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1242 "The service file is incorrect or does not have all required attributes");
1244 case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1245 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1246 "Cannot launch daemon, file not found or permissions invalid");
1248 case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1249 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1250 "Invalid arguments to command line");
1252 case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1253 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1254 "Launched child was signaled, it probably crashed");
1257 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1258 "Launch helper exited with unknown return code %i", exit_code);
1264 babysitter_watch_callback (DBusWatch *watch,
1265 unsigned int condition,
1268 BusPendingActivation *pending_activation = data;
1270 DBusBabysitter *babysitter;
1272 babysitter = pending_activation->babysitter;
1274 _dbus_babysitter_ref (babysitter);
1276 retval = dbus_watch_handle (watch, condition);
1278 /* FIXME this is broken in the same way that
1279 * connection watches used to be; there should be
1280 * a separate callback for status change, instead
1281 * of doing "if we handled a watch status might
1284 * Fixing this lets us move dbus_watch_handle
1285 * calls into dbus-mainloop.c
1288 if (_dbus_babysitter_get_child_exited (babysitter))
1293 dbus_error_init (&error);
1294 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1296 /* refine the error code if we got an exit code */
1297 if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED))
1300 if (_dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1302 dbus_error_free (&error);
1303 handle_activation_exit_error (exit_code, &error);
1307 /* Destroy all pending activations with the same exec */
1308 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1310 while (_dbus_hash_iter_next (&iter))
1312 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1314 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1315 pending_activation_failed (p, &error);
1318 /* Destroys the pending activation */
1319 pending_activation_failed (pending_activation, &error);
1321 dbus_error_free (&error);
1324 _dbus_babysitter_unref (babysitter);
1330 add_babysitter_watch (DBusWatch *watch,
1333 BusPendingActivation *pending_activation = data;
1335 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1336 watch, babysitter_watch_callback, pending_activation,
1341 remove_babysitter_watch (DBusWatch *watch,
1344 BusPendingActivation *pending_activation = data;
1346 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1347 watch, babysitter_watch_callback, pending_activation);
1351 pending_activation_timed_out (void *data)
1353 BusPendingActivation *pending_activation = data;
1356 /* Kill the spawned process, since it sucks
1357 * (not sure this is what we want to do, but
1358 * may as well try it for now)
1360 if (pending_activation->babysitter)
1361 _dbus_babysitter_kill_child (pending_activation->babysitter);
1363 dbus_error_init (&error);
1365 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1366 "Activation of %s timed out",
1367 pending_activation->service_name);
1369 pending_activation_failed (pending_activation, &error);
1371 dbus_error_free (&error);
1377 cancel_pending (void *data)
1379 BusPendingActivation *pending_activation = data;
1381 _dbus_verbose ("Canceling pending activation of %s\n",
1382 pending_activation->service_name);
1384 if (pending_activation->babysitter)
1385 _dbus_babysitter_kill_child (pending_activation->babysitter);
1387 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1388 pending_activation->service_name);
1392 free_pending_cancel_data (void *data)
1394 BusPendingActivation *pending_activation = data;
1396 bus_pending_activation_unref (pending_activation);
1400 add_cancel_pending_to_transaction (BusTransaction *transaction,
1401 BusPendingActivation *pending_activation)
1403 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1405 free_pending_cancel_data))
1408 bus_pending_activation_ref (pending_activation);
1410 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1416 update_service_cache (BusActivation *activation, DBusError *error)
1420 _dbus_hash_iter_init (activation->directories, &iter);
1421 while (_dbus_hash_iter_next (&iter))
1423 DBusError tmp_error;
1424 BusServiceDirectory *s_dir;
1426 s_dir = _dbus_hash_iter_get_value (&iter);
1428 dbus_error_init (&tmp_error);
1429 if (!update_directory (activation, s_dir, &tmp_error))
1431 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1433 dbus_move_error (&tmp_error, error);
1437 dbus_error_free (&tmp_error);
1445 static BusActivationEntry *
1446 activation_find_entry (BusActivation *activation,
1447 const char *service_name,
1450 BusActivationEntry *entry;
1452 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1455 if (!update_service_cache (activation, error))
1458 entry = _dbus_hash_table_lookup_string (activation->entries,
1463 BusActivationEntry *updated_entry;
1465 if (!check_service_file (activation, entry, &updated_entry, error))
1468 entry = updated_entry;
1473 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1474 "The name %s was not provided by any .service files",
1483 bus_activation_get_environment (BusActivation *activation)
1490 length = _dbus_hash_table_get_n_entries (activation->environment);
1492 environment = dbus_new0 (char *, length + 1);
1494 if (environment == NULL)
1498 _dbus_hash_iter_init (activation->environment, &iter);
1500 if (!_dbus_string_init (&entry))
1502 dbus_free_string_array (environment);
1506 while (_dbus_hash_iter_next (&iter))
1508 const char *key, *value;
1510 key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1511 value = (const char *) _dbus_hash_iter_get_value (&iter);
1513 if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1516 if (!_dbus_string_steal_data (&entry, environment + i))
1521 _dbus_string_free (&entry);
1525 dbus_free_string_array (environment);
1533 bus_activation_set_environment_variable (BusActivation *activation,
1545 hash_key = _dbus_strdup (key);
1547 if (hash_key == NULL)
1550 hash_value = _dbus_strdup (value);
1552 if (hash_value == NULL)
1555 if (!_dbus_hash_table_insert_string (activation->environment,
1556 hash_key, hash_value))
1561 if (retval == FALSE)
1563 dbus_free (hash_key);
1564 dbus_free (hash_value);
1565 BUS_SET_OOM (error);
1572 bus_activation_activate_service (BusActivation *activation,
1573 DBusConnection *connection,
1574 BusTransaction *transaction,
1575 dbus_bool_t auto_activation,
1576 DBusMessage *activation_message,
1577 const char *service_name,
1580 BusActivationEntry *entry;
1581 BusPendingActivation *pending_activation;
1582 BusPendingActivationEntry *pending_activation_entry;
1583 DBusMessage *message;
1584 DBusString service_str;
1585 const char *servicehelper;
1591 dbus_bool_t activated;
1596 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1598 if (activation->n_pending_activations >=
1599 bus_context_get_max_pending_activations (activation->context))
1601 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1602 "The maximum number of pending activations has been reached, activation of %s failed",
1607 entry = activation_find_entry (activation, service_name, error);
1611 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1612 * call us if the service is already active.
1614 if (!auto_activation)
1616 /* Check if the service is active */
1617 _dbus_string_init_const (&service_str, service_name);
1618 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1620 dbus_uint32_t result;
1622 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1624 message = dbus_message_new_method_return (activation_message);
1628 _dbus_verbose ("No memory to create reply to activate message\n");
1629 BUS_SET_OOM (error);
1633 result = DBUS_START_REPLY_ALREADY_RUNNING;
1635 if (!dbus_message_append_args (message,
1636 DBUS_TYPE_UINT32, &result,
1639 _dbus_verbose ("No memory to set args of reply to activate message\n");
1640 BUS_SET_OOM (error);
1641 dbus_message_unref (message);
1645 retval = bus_transaction_send_from_driver (transaction, connection, message);
1646 dbus_message_unref (message);
1649 _dbus_verbose ("Failed to send reply\n");
1650 BUS_SET_OOM (error);
1657 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1658 if (!pending_activation_entry)
1660 _dbus_verbose ("Failed to create pending activation entry\n");
1661 BUS_SET_OOM (error);
1665 pending_activation_entry->auto_activation = auto_activation;
1667 pending_activation_entry->activation_message = activation_message;
1668 dbus_message_ref (activation_message);
1669 pending_activation_entry->connection = connection;
1670 dbus_connection_ref (connection);
1672 /* Check if the service is being activated */
1673 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1674 if (pending_activation)
1676 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1678 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1680 BUS_SET_OOM (error);
1681 bus_pending_activation_entry_free (pending_activation_entry);
1685 pending_activation->n_entries += 1;
1686 pending_activation->activation->n_pending_activations += 1;
1690 pending_activation = dbus_new0 (BusPendingActivation, 1);
1691 if (!pending_activation)
1693 _dbus_verbose ("Failed to create pending activation\n");
1695 BUS_SET_OOM (error);
1696 bus_pending_activation_entry_free (pending_activation_entry);
1700 pending_activation->activation = activation;
1701 pending_activation->refcount = 1;
1703 pending_activation->service_name = _dbus_strdup (service_name);
1704 if (!pending_activation->service_name)
1706 _dbus_verbose ("Failed to copy service name for pending activation\n");
1708 BUS_SET_OOM (error);
1709 bus_pending_activation_unref (pending_activation);
1710 bus_pending_activation_entry_free (pending_activation_entry);
1714 pending_activation->exec = _dbus_strdup (entry->exec);
1715 if (!pending_activation->exec)
1717 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1718 BUS_SET_OOM (error);
1719 bus_pending_activation_unref (pending_activation);
1720 bus_pending_activation_entry_free (pending_activation_entry);
1724 pending_activation->timeout =
1725 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1726 pending_activation_timed_out,
1729 if (!pending_activation->timeout)
1731 _dbus_verbose ("Failed to create timeout for pending activation\n");
1733 BUS_SET_OOM (error);
1734 bus_pending_activation_unref (pending_activation);
1735 bus_pending_activation_entry_free (pending_activation_entry);
1739 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1740 pending_activation->timeout,
1741 handle_timeout_callback,
1745 _dbus_verbose ("Failed to add timeout for pending activation\n");
1747 BUS_SET_OOM (error);
1748 bus_pending_activation_unref (pending_activation);
1749 bus_pending_activation_entry_free (pending_activation_entry);
1753 pending_activation->timeout_added = TRUE;
1755 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1757 _dbus_verbose ("Failed to add entry to just-created 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->n_entries += 1;
1766 pending_activation->activation->n_pending_activations += 1;
1769 _dbus_hash_iter_init (activation->pending_activations, &iter);
1770 while (_dbus_hash_iter_next (&iter))
1772 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1774 if (strcmp (p->exec, entry->exec) == 0)
1781 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1782 pending_activation->service_name,
1783 pending_activation))
1785 _dbus_verbose ("Failed to put pending activation in hash table\n");
1787 BUS_SET_OOM (error);
1788 bus_pending_activation_unref (pending_activation);
1793 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1795 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1796 BUS_SET_OOM (error);
1797 _dbus_hash_table_remove_string (activation->pending_activations,
1798 pending_activation->service_name);
1806 /* use command as system and session different */
1807 if (!_dbus_string_init (&command))
1809 BUS_SET_OOM (error);
1813 /* does the bus use a helper? */
1814 servicehelper = bus_context_get_servicehelper (activation->context);
1815 if (servicehelper != NULL)
1817 if (entry->user == NULL)
1819 _dbus_string_free (&command);
1820 dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
1821 "Cannot do system-bus activation with no user\n");
1825 /* join the helper path and the service name */
1826 if (!_dbus_string_append (&command, servicehelper))
1828 _dbus_string_free (&command);
1829 BUS_SET_OOM (error);
1832 if (!_dbus_string_append (&command, " "))
1834 _dbus_string_free (&command);
1835 BUS_SET_OOM (error);
1838 if (!_dbus_string_append (&command, service_name))
1840 _dbus_string_free (&command);
1841 BUS_SET_OOM (error);
1847 /* the bus does not use a helper, so we can append arguments with the exec line */
1848 if (!_dbus_string_append (&command, entry->exec))
1850 _dbus_string_free (&command);
1851 BUS_SET_OOM (error);
1856 /* convert command into arguments */
1857 if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
1859 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
1860 _DBUS_ASSERT_ERROR_IS_SET (error);
1862 _dbus_hash_table_remove_string (activation->pending_activations,
1863 pending_activation->service_name);
1865 _dbus_string_free (&command);
1868 _dbus_string_free (&command);
1870 if (!add_bus_environment (activation, error))
1872 _DBUS_ASSERT_ERROR_IS_SET (error);
1873 dbus_free_string_array (argv);
1877 envp = bus_activation_get_environment (activation);
1881 BUS_SET_OOM (error);
1882 dbus_free_string_array (argv);
1886 _dbus_verbose ("Spawning %s ...\n", argv[0]);
1887 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1892 _dbus_verbose ("Failed to spawn child\n");
1893 _DBUS_ASSERT_ERROR_IS_SET (error);
1894 dbus_free_string_array (argv);
1895 dbus_free_string_array (envp);
1900 dbus_free_string_array (argv);
1903 _dbus_assert (pending_activation->babysitter != NULL);
1905 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1906 add_babysitter_watch,
1907 remove_babysitter_watch,
1912 BUS_SET_OOM (error);
1913 _dbus_verbose ("Failed to set babysitter watch functions\n");
1921 bus_activation_list_services (BusActivation *activation,
1929 len = _dbus_hash_table_get_n_entries (activation->entries);
1930 retval = dbus_new (char *, len + 1);
1935 _dbus_hash_iter_init (activation->entries, &iter);
1937 while (_dbus_hash_iter_next (&iter))
1939 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
1941 retval[i] = _dbus_strdup (entry->name);
1942 if (retval[i] == NULL)
1957 for (j = 0; j < i; j++)
1958 dbus_free (retval[i]);
1965 #ifdef DBUS_BUILD_TESTS
1969 #define SERVICE_NAME_1 "MyService1"
1970 #define SERVICE_NAME_2 "MyService2"
1971 #define SERVICE_NAME_3 "MyService3"
1973 #define SERVICE_FILE_1 "service-1.service"
1974 #define SERVICE_FILE_2 "service-2.service"
1975 #define SERVICE_FILE_3 "service-3.service"
1978 test_create_service_file (DBusString *dir,
1979 const char *filename,
1983 DBusString file_name, full_path;
1985 dbus_bool_t ret_val;
1988 _dbus_string_init_const (&file_name, filename);
1990 if (!_dbus_string_init (&full_path))
1993 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1994 !_dbus_concat_dir_and_file (&full_path, &file_name))
2000 file = fopen (_dbus_string_get_const_data (&full_path), "w");
2007 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2011 _dbus_string_free (&full_path);
2016 test_remove_service_file (DBusString *dir, const char *filename)
2018 DBusString file_name, full_path;
2019 dbus_bool_t ret_val;
2023 _dbus_string_init_const (&file_name, filename);
2025 if (!_dbus_string_init (&full_path))
2028 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2029 !_dbus_concat_dir_and_file (&full_path, &file_name))
2035 if (!_dbus_delete_file (&full_path, NULL))
2042 _dbus_string_free (&full_path);
2047 test_remove_directory (DBusString *dir)
2050 DBusString filename, full_path;
2051 dbus_bool_t ret_val;
2055 if (!_dbus_string_init (&filename))
2058 if (!_dbus_string_init (&full_path))
2060 _dbus_string_free (&filename);
2064 iter = _dbus_directory_open (dir, NULL);
2071 while (_dbus_directory_get_next_file (iter, &filename, NULL))
2073 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2079 _dbus_directory_close (iter);
2081 if (!_dbus_delete_directory (dir, NULL))
2088 _dbus_string_free (&filename);
2089 _dbus_string_free (&full_path);
2095 init_service_reload_test (DBusString *dir)
2099 if (!_dbus_stat (dir, &stat_buf, NULL))
2101 if (!_dbus_create_directory (dir, NULL))
2106 if (!test_remove_directory (dir))
2109 if (!_dbus_create_directory (dir, NULL))
2113 /* Create one initial file */
2114 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2121 cleanup_service_reload_test (DBusString *dir)
2123 if (!test_remove_directory (dir))
2131 BusActivation *activation;
2132 const char *service_name;
2133 dbus_bool_t expecting_find;
2137 check_func (void *data)
2140 BusActivationEntry *entry;
2142 dbus_bool_t ret_val;
2147 dbus_error_init (&error);
2149 entry = activation_find_entry (d->activation, d->service_name, &error);
2152 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2158 if (d->expecting_find)
2162 dbus_error_free (&error);
2166 if (!d->expecting_find)
2174 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2179 err = !_dbus_test_oom_handling (description, check_func, data);
2181 err = !check_func (data);
2184 _dbus_assert_not_reached ("Test failed");
2190 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2192 BusActivation *activation;
2194 DBusList *directories;
2198 _dbus_string_init_const (&address, "");
2200 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2203 activation = bus_activation_new (NULL, &address, &directories, NULL);
2207 d.activation = activation;
2209 /* Check for existing service file */
2210 d.expecting_find = TRUE;
2211 d.service_name = SERVICE_NAME_1;
2213 if (!do_test ("Existing service file", oom_test, &d))
2216 /* Check for non-existing service file */
2217 d.expecting_find = FALSE;
2218 d.service_name = SERVICE_NAME_3;
2220 if (!do_test ("Nonexisting service file", oom_test, &d))
2223 /* Check for added service file */
2224 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2227 d.expecting_find = TRUE;
2228 d.service_name = SERVICE_NAME_2;
2230 if (!do_test ("Added service file", oom_test, &d))
2233 /* Check for removed service file */
2234 if (!test_remove_service_file (dir, SERVICE_FILE_2))
2237 d.expecting_find = FALSE;
2238 d.service_name = SERVICE_FILE_2;
2240 if (!do_test ("Removed service file", oom_test, &d))
2243 /* Check for updated service file */
2245 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2247 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2250 d.expecting_find = TRUE;
2251 d.service_name = SERVICE_NAME_3;
2253 if (!do_test ("Updated service file, part 1", oom_test, &d))
2256 d.expecting_find = FALSE;
2257 d.service_name = SERVICE_NAME_1;
2259 if (!do_test ("Updated service file, part 2", oom_test, &d))
2262 bus_activation_unref (activation);
2263 _dbus_list_clear (&directories);
2269 bus_activation_service_reload_test (const DBusString *test_data_dir)
2271 DBusString directory;
2273 if (!_dbus_string_init (&directory))
2276 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2279 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2280 !_dbus_generate_random_ascii (&directory, 6))
2285 /* Do normal tests */
2286 if (!init_service_reload_test (&directory))
2287 _dbus_assert_not_reached ("could not initiate service reload test");
2289 if (!do_service_reload_test (&directory, FALSE))
2293 if (!init_service_reload_test (&directory))
2294 _dbus_assert_not_reached ("could not initiate service reload test");
2296 if (!do_service_reload_test (&directory, TRUE))
2299 /* Cleanup test directory */
2300 if (!cleanup_service_reload_test (&directory))
2303 _dbus_string_free (&directory);
2308 #endif /* DBUS_BUILD_TESTS */