1 /* -*- mode: C; c-file-style: "gnu" -*- */
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.0
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 "desktop-file.h"
30 #include <dbus/dbus-internals.h>
31 #include <dbus/dbus-hash.h>
32 #include <dbus/dbus-list.h>
33 #include <dbus/dbus-spawn.h>
34 #include <dbus/dbus-timeout.h>
38 #define DBUS_SERVICE_SECTION "D-BUS Service"
39 #define DBUS_SERVICE_NAME "Name"
40 #define DBUS_SERVICE_EXEC "Exec"
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;
60 DBusHashTable *entries;
61 } BusServiceDirectory;
69 BusServiceDirectory *s_dir;
73 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
75 struct BusPendingActivationEntry
77 DBusMessage *activation_message;
78 DBusConnection *connection;
80 dbus_bool_t auto_activation;
86 BusActivation *activation;
91 DBusBabysitter *babysitter;
93 unsigned int timeout_added : 1;
94 } BusPendingActivation;
96 static BusServiceDirectory *
97 bus_service_directory_ref (BusServiceDirectory *dir)
99 _dbus_assert (dir->refcount);
107 bus_service_directory_unref (BusServiceDirectory *dir)
112 _dbus_assert (dir->refcount > 0);
115 if (dir->refcount > 0)
119 _dbus_hash_table_unref (dir->entries);
121 dbus_free (dir->dir_c);
126 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
128 if (entry->activation_message)
129 dbus_message_unref (entry->activation_message);
131 if (entry->connection)
132 dbus_connection_unref (entry->connection);
138 handle_timeout_callback (DBusTimeout *timeout,
141 BusPendingActivation *pending_activation = data;
143 while (!dbus_timeout_handle (pending_activation->timeout))
144 _dbus_wait_for_memory ();
147 static BusPendingActivation *
148 bus_pending_activation_ref (BusPendingActivation *pending_activation)
150 _dbus_assert (pending_activation->refcount > 0);
151 pending_activation->refcount += 1;
153 return pending_activation;
157 bus_pending_activation_unref (BusPendingActivation *pending_activation)
161 if (pending_activation == NULL) /* hash table requires this */
164 _dbus_assert (pending_activation->refcount > 0);
165 pending_activation->refcount -= 1;
167 if (pending_activation->refcount > 0)
170 if (pending_activation->timeout_added)
172 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
173 pending_activation->timeout,
174 handle_timeout_callback, pending_activation);
175 pending_activation->timeout_added = FALSE;
178 if (pending_activation->timeout)
179 _dbus_timeout_unref (pending_activation->timeout);
181 if (pending_activation->babysitter)
183 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
185 pending_activation->babysitter,
187 _dbus_assert_not_reached ("setting watch functions to NULL failed");
189 _dbus_babysitter_unref (pending_activation->babysitter);
192 dbus_free (pending_activation->service_name);
193 dbus_free (pending_activation->exec);
195 link = _dbus_list_get_first_link (&pending_activation->entries);
199 BusPendingActivationEntry *entry = link->data;
201 bus_pending_activation_entry_free (entry);
203 link = _dbus_list_get_next_link (&pending_activation->entries, link);
205 _dbus_list_clear (&pending_activation->entries);
207 pending_activation->activation->n_pending_activations -=
208 pending_activation->n_entries;
210 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
212 dbus_free (pending_activation);
215 static BusActivationEntry *
216 bus_activation_entry_ref (BusActivationEntry *entry)
218 _dbus_assert (entry->refcount > 0);
225 bus_activation_entry_unref (BusActivationEntry *entry)
227 if (entry == NULL) /* hash table requires this */
230 _dbus_assert (entry->refcount > 0);
233 if (entry->refcount > 0)
236 dbus_free (entry->name);
237 dbus_free (entry->exec);
238 dbus_free (entry->filename);
244 update_desktop_file_entry (BusActivation *activation,
245 BusServiceDirectory *s_dir,
246 DBusString *filename,
247 BusDesktopFile *desktop_file,
251 BusActivationEntry *entry;
253 DBusString file_path;
255 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
261 if (!_dbus_string_init (&file_path))
267 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
268 !_dbus_concat_dir_and_file (&file_path, filename))
274 if (!_dbus_stat (&file_path, &stat_buf, NULL))
276 dbus_set_error (error, DBUS_ERROR_FAILED,
277 "Can't stat the service file\n");
281 if (!bus_desktop_file_get_string (desktop_file,
282 DBUS_SERVICE_SECTION,
286 dbus_set_error (error, DBUS_ERROR_FAILED,
287 "No \""DBUS_SERVICE_NAME"\" key in .service file\n");
291 if (!bus_desktop_file_get_string (desktop_file,
292 DBUS_SERVICE_SECTION,
296 dbus_set_error (error, DBUS_ERROR_FAILED,
297 "No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
301 entry = _dbus_hash_table_lookup_string (s_dir->entries,
302 _dbus_string_get_const_data (filename));
303 if (entry == NULL) /* New file */
305 /* FIXME we need a better-defined algorithm for which service file to
306 * pick than "whichever one is first in the directory listing"
308 if (_dbus_hash_table_lookup_string (activation->entries, name))
310 dbus_set_error (error, DBUS_ERROR_FAILED,
311 "Service %s already exists in activation entry list\n", name);
315 entry = dbus_new0 (BusActivationEntry, 1);
326 entry->s_dir = s_dir;
327 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
328 if (!entry->filename)
334 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
340 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
342 /* Revert the insertion in the entries table */
343 _dbus_hash_table_remove_string (activation->entries, entry->name);
348 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
350 else /* Just update the entry */
352 bus_activation_entry_ref (entry);
353 _dbus_hash_table_remove_string (activation->entries, entry->name);
355 if (_dbus_hash_table_lookup_string (activation->entries, name))
357 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
358 name, _dbus_string_get_const_data (&file_path));
362 dbus_free (entry->name);
363 dbus_free (entry->exec);
366 if (!_dbus_hash_table_insert_string (activation->entries,
367 entry->name, bus_activation_entry_ref(entry)))
370 /* Also remove path to entries hash since we want this in sync with
371 * the entries hash table */
372 _dbus_hash_table_remove_string (entry->s_dir->entries,
374 bus_activation_entry_unref (entry);
379 entry->mtime = stat_buf.mtime;
381 _dbus_string_free (&file_path);
382 bus_activation_entry_unref (entry);
389 _dbus_string_free (&file_path);
392 bus_activation_entry_unref (entry);
398 check_service_file (BusActivation *activation,
399 BusActivationEntry *entry,
400 BusActivationEntry **updated_entry,
405 BusActivationEntry *tmp_entry;
406 DBusString file_path;
412 _dbus_string_init_const (&filename, entry->filename);
414 if (!_dbus_string_init (&file_path))
420 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
421 !_dbus_concat_dir_and_file (&file_path, &filename))
428 if (!_dbus_stat (&file_path, &stat_buf, NULL))
430 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
431 _dbus_string_get_const_data (&file_path));
433 _dbus_hash_table_remove_string (activation->entries, entry->name);
434 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
442 if (stat_buf.mtime > entry->mtime)
444 BusDesktopFile *desktop_file;
447 dbus_error_init (&tmp_error);
449 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
450 if (desktop_file == NULL)
452 _dbus_verbose ("Could not load %s: %s\n",
453 _dbus_string_get_const_data (&file_path),
455 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
457 dbus_move_error (&tmp_error, error);
461 dbus_error_free (&tmp_error);
466 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
468 bus_desktop_file_free (desktop_file);
469 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
471 dbus_move_error (&tmp_error, error);
475 dbus_error_free (&tmp_error);
480 bus_desktop_file_free (desktop_file);
486 _dbus_string_free (&file_path);
488 if (updated_entry != NULL)
489 *updated_entry = tmp_entry;
494 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
495 * hash entries it already added.
498 update_directory (BusActivation *activation,
499 BusServiceDirectory *s_dir,
503 DBusString dir, filename;
504 BusDesktopFile *desktop_file;
507 BusActivationEntry *entry;
508 DBusString full_path;
510 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
515 _dbus_string_init_const (&dir, s_dir->dir_c);
517 if (!_dbus_string_init (&filename))
523 if (!_dbus_string_init (&full_path))
526 _dbus_string_free (&filename);
532 /* from this point it's safe to "goto out" */
534 iter = _dbus_directory_open (&dir, error);
537 _dbus_verbose ("Failed to open directory %s: %s\n",
539 error ? error->message : "unknown");
543 /* Now read the files */
544 dbus_error_init (&tmp_error);
545 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
547 _dbus_assert (!dbus_error_is_set (&tmp_error));
549 _dbus_string_set_length (&full_path, 0);
551 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
553 _dbus_verbose ("Skipping non-.service file %s\n",
554 _dbus_string_get_const_data (&filename));
558 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
559 if (entry) /* Already has this service file in the cache */
561 if (!check_service_file (activation, entry, NULL, error))
567 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
568 !_dbus_concat_dir_and_file (&full_path, &filename))
575 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
576 if (desktop_file == NULL)
578 _dbus_verbose ("Could not load %s: %s\n",
579 _dbus_string_get_const_data (&full_path),
582 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
584 dbus_move_error (&tmp_error, error);
588 dbus_error_free (&tmp_error);
592 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
594 bus_desktop_file_free (desktop_file);
597 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
598 _dbus_string_get_const_data (&full_path), tmp_error.message);
600 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
602 dbus_move_error (&tmp_error, error);
606 dbus_error_free (&tmp_error);
611 bus_desktop_file_free (desktop_file);
617 if (dbus_error_is_set (&tmp_error))
619 dbus_move_error (&tmp_error, error);
627 _DBUS_ASSERT_ERROR_IS_SET (error);
629 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
632 _dbus_directory_close (iter);
634 bus_desktop_file_free (desktop_file);
635 _dbus_string_free (&filename);
636 _dbus_string_free (&full_path);
642 bus_activation_new (BusContext *context,
643 const DBusString *address,
644 DBusList **directories,
647 BusActivation *activation;
651 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
653 activation = dbus_new0 (BusActivation, 1);
654 if (activation == NULL)
660 activation->refcount = 1;
661 activation->context = context;
662 activation->n_pending_activations = 0;
664 if (!_dbus_string_copy_data (address, &activation->server_address))
670 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
671 (DBusFreeFunction)bus_activation_entry_unref);
672 if (activation->entries == NULL)
678 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
679 (DBusFreeFunction)bus_pending_activation_unref);
681 if (activation->pending_activations == NULL)
687 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
688 (DBusFreeFunction)bus_service_directory_unref);
690 if (activation->directories == NULL)
696 /* Load service files */
697 link = _dbus_list_get_first_link (directories);
700 BusServiceDirectory *s_dir;
702 dir = _dbus_strdup ((const char *) link->data);
709 s_dir = dbus_new0 (BusServiceDirectory, 1);
720 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
721 (DBusFreeFunction)bus_activation_entry_unref);
725 bus_service_directory_unref (s_dir);
730 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
732 bus_service_directory_unref (s_dir);
737 if (!update_directory (activation, s_dir, error))
740 link = _dbus_list_get_next_link (directories, link);
746 bus_activation_unref (activation);
751 bus_activation_ref (BusActivation *activation)
753 _dbus_assert (activation->refcount > 0);
755 activation->refcount += 1;
761 bus_activation_unref (BusActivation *activation)
763 _dbus_assert (activation->refcount > 0);
765 activation->refcount -= 1;
767 if (activation->refcount > 0)
770 dbus_free (activation->server_address);
771 if (activation->entries)
772 _dbus_hash_table_unref (activation->entries);
773 if (activation->pending_activations)
774 _dbus_hash_table_unref (activation->pending_activations);
775 if (activation->directories)
776 _dbus_hash_table_unref (activation->directories);
778 dbus_free (activation);
782 child_setup (void *data)
784 BusActivation *activation = data;
787 /* If no memory, we simply have the child exit, so it won't try
788 * to connect to the wrong thing.
790 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", activation->server_address))
793 type = bus_context_get_type (activation->context);
796 if (!_dbus_setenv ("DBUS_BUS_TYPE", type))
799 if (strcmp (type, "session") == 0)
801 if (!_dbus_setenv ("DBUS_SESSION_BUS_ADDRESS",
802 activation->server_address))
805 else if (strcmp (type, "system") == 0)
807 if (!_dbus_setenv ("DBUS_SYSTEM_BUS_ADDRESS",
808 activation->server_address))
816 BusPendingActivation *pending_activation;
817 DBusPreallocatedHash *hash_entry;
818 } RestorePendingData;
821 restore_pending (void *data)
823 RestorePendingData *d = data;
825 _dbus_assert (d->pending_activation != NULL);
826 _dbus_assert (d->hash_entry != NULL);
828 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
829 d->pending_activation->service_name,
830 d->pending_activation->timeout_added);
832 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
834 d->pending_activation->service_name, d->pending_activation);
836 bus_pending_activation_ref (d->pending_activation);
838 d->hash_entry = NULL;
842 free_pending_restore_data (void *data)
844 RestorePendingData *d = data;
847 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
850 bus_pending_activation_unref (d->pending_activation);
856 add_restore_pending_to_transaction (BusTransaction *transaction,
857 BusPendingActivation *pending_activation)
859 RestorePendingData *d;
861 d = dbus_new (RestorePendingData, 1);
865 d->pending_activation = pending_activation;
866 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
868 bus_pending_activation_ref (d->pending_activation);
870 if (d->hash_entry == NULL ||
871 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
872 free_pending_restore_data))
874 free_pending_restore_data (d);
878 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
884 bus_activation_service_created (BusActivation *activation,
885 const char *service_name,
886 BusTransaction *transaction,
889 BusPendingActivation *pending_activation;
890 DBusMessage *message;
893 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
895 /* Check if it's a pending activation */
896 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
898 if (!pending_activation)
901 link = _dbus_list_get_first_link (&pending_activation->entries);
904 BusPendingActivationEntry *entry = link->data;
905 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
907 if (dbus_connection_get_is_connected (entry->connection))
909 /* Only send activation replies to regular activation requests. */
910 if (!entry->auto_activation)
912 message = dbus_message_new_method_return (entry->activation_message);
919 if (!dbus_message_append_args (message,
920 DBUS_TYPE_UINT32, DBUS_ACTIVATION_REPLY_ACTIVATED,
923 dbus_message_unref (message);
928 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
930 dbus_message_unref (message);
935 dbus_message_unref (message);
949 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
951 BusTransaction *transaction,
954 BusPendingActivation *pending_activation;
957 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
959 /* Check if it's a pending activation */
960 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
961 bus_service_get_name (service));
963 if (!pending_activation)
966 link = _dbus_list_get_first_link (&pending_activation->entries);
969 BusPendingActivationEntry *entry = link->data;
970 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
972 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
974 DBusConnection *addressed_recipient;
976 addressed_recipient = bus_service_get_primary_owner (service);
978 /* Check the security policy, which has the side-effect of adding an
979 * expected pending reply.
981 if (!bus_context_check_security_policy (activation->context, transaction,
985 entry->activation_message, error))
988 if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
998 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1000 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1001 BUS_SET_OOM (error);
1005 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1014 * FIXME @todo the error messages here would ideally be preallocated
1015 * so we don't need to allocate memory to send them.
1016 * Using the usual tactic, prealloc an OOM message, then
1017 * if we can't alloc the real error send the OOM error instead.
1020 try_send_activation_failure (BusPendingActivation *pending_activation,
1021 const DBusError *how)
1023 BusActivation *activation;
1025 BusTransaction *transaction;
1027 activation = pending_activation->activation;
1029 transaction = bus_transaction_new (activation->context);
1030 if (transaction == NULL)
1033 link = _dbus_list_get_first_link (&pending_activation->entries);
1034 while (link != NULL)
1036 BusPendingActivationEntry *entry = link->data;
1037 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1039 if (dbus_connection_get_is_connected (entry->connection))
1041 if (!bus_transaction_send_error_reply (transaction,
1044 entry->activation_message))
1051 bus_transaction_execute_and_free (transaction);
1057 bus_transaction_cancel_and_free (transaction);
1062 * Free the pending activation and send an error message to all the
1063 * connections that were waiting for it.
1066 pending_activation_failed (BusPendingActivation *pending_activation,
1067 const DBusError *how)
1069 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1070 while (!try_send_activation_failure (pending_activation, how))
1071 _dbus_wait_for_memory ();
1073 /* Destroy this pending activation */
1074 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1075 pending_activation->service_name);
1079 babysitter_watch_callback (DBusWatch *watch,
1080 unsigned int condition,
1083 BusPendingActivation *pending_activation = data;
1085 DBusBabysitter *babysitter;
1087 babysitter = pending_activation->babysitter;
1089 _dbus_babysitter_ref (babysitter);
1091 retval = dbus_watch_handle (watch, condition);
1093 /* FIXME this is broken in the same way that
1094 * connection watches used to be; there should be
1095 * a separate callback for status change, instead
1096 * of doing "if we handled a watch status might
1099 * Fixing this lets us move dbus_watch_handle
1100 * calls into dbus-mainloop.c
1103 if (_dbus_babysitter_get_child_exited (babysitter))
1108 dbus_error_init (&error);
1109 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1111 /* Destroy all pending activations with the same exec */
1112 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1114 while (_dbus_hash_iter_next (&iter))
1116 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1118 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1119 pending_activation_failed (p, &error);
1122 /* Destroys the pending activation */
1123 pending_activation_failed (pending_activation, &error);
1125 dbus_error_free (&error);
1128 _dbus_babysitter_unref (babysitter);
1134 add_babysitter_watch (DBusWatch *watch,
1137 BusPendingActivation *pending_activation = data;
1139 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1140 watch, babysitter_watch_callback, pending_activation,
1145 remove_babysitter_watch (DBusWatch *watch,
1148 BusPendingActivation *pending_activation = data;
1150 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1151 watch, babysitter_watch_callback, pending_activation);
1155 pending_activation_timed_out (void *data)
1157 BusPendingActivation *pending_activation = data;
1160 /* Kill the spawned process, since it sucks
1161 * (not sure this is what we want to do, but
1162 * may as well try it for now)
1164 if (pending_activation->babysitter)
1165 _dbus_babysitter_kill_child (pending_activation->babysitter);
1167 dbus_error_init (&error);
1169 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1170 "Activation of %s timed out",
1171 pending_activation->service_name);
1173 pending_activation_failed (pending_activation, &error);
1175 dbus_error_free (&error);
1181 cancel_pending (void *data)
1183 BusPendingActivation *pending_activation = data;
1185 _dbus_verbose ("Canceling pending activation of %s\n",
1186 pending_activation->service_name);
1188 if (pending_activation->babysitter)
1189 _dbus_babysitter_kill_child (pending_activation->babysitter);
1191 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1192 pending_activation->service_name);
1196 free_pending_cancel_data (void *data)
1198 BusPendingActivation *pending_activation = data;
1200 bus_pending_activation_unref (pending_activation);
1204 add_cancel_pending_to_transaction (BusTransaction *transaction,
1205 BusPendingActivation *pending_activation)
1207 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1209 free_pending_cancel_data))
1212 bus_pending_activation_ref (pending_activation);
1214 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1220 update_service_cache (BusActivation *activation, DBusError *error)
1224 _dbus_hash_iter_init (activation->directories, &iter);
1225 while (_dbus_hash_iter_next (&iter))
1227 DBusError tmp_error;
1228 BusServiceDirectory *s_dir;
1230 s_dir = _dbus_hash_iter_get_value (&iter);
1232 dbus_error_init (&tmp_error);
1233 if (!update_directory (activation, s_dir, &tmp_error))
1235 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1237 dbus_move_error (&tmp_error, error);
1241 dbus_error_free (&tmp_error);
1249 static BusActivationEntry *
1250 activation_find_entry (BusActivation *activation,
1251 const char *service_name,
1254 BusActivationEntry *entry;
1256 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1259 if (!update_service_cache (activation, error))
1262 entry = _dbus_hash_table_lookup_string (activation->entries,
1267 BusActivationEntry *updated_entry;
1269 if (!check_service_file (activation, entry, &updated_entry, error))
1272 entry = updated_entry;
1277 dbus_set_error (error, DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND,
1278 "The service %s was not found in the activation entry list",
1287 bus_activation_activate_service (BusActivation *activation,
1288 DBusConnection *connection,
1289 BusTransaction *transaction,
1290 dbus_bool_t auto_activation,
1291 DBusMessage *activation_message,
1292 const char *service_name,
1295 BusActivationEntry *entry;
1296 BusPendingActivation *pending_activation;
1297 BusPendingActivationEntry *pending_activation_entry;
1298 DBusMessage *message;
1299 DBusString service_str;
1303 dbus_bool_t activated;
1307 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1309 if (activation->n_pending_activations >=
1310 bus_context_get_max_pending_activations (activation->context))
1312 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1313 "The maximum number of pending activations has been reached, activation of %s failed",
1318 entry = activation_find_entry (activation, service_name, error);
1322 /* Check if the service is active */
1323 _dbus_string_init_const (&service_str, service_name);
1324 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1326 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1328 message = dbus_message_new_method_return (activation_message);
1332 _dbus_verbose ("No memory to create reply to activate message\n");
1333 BUS_SET_OOM (error);
1337 if (!dbus_message_append_args (message,
1338 DBUS_TYPE_UINT32, DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE,
1341 _dbus_verbose ("No memory to set args of reply to activate message\n");
1342 BUS_SET_OOM (error);
1343 dbus_message_unref (message);
1347 retval = bus_transaction_send_from_driver (transaction, connection, message);
1348 dbus_message_unref (message);
1351 _dbus_verbose ("Failed to send reply\n");
1352 BUS_SET_OOM (error);
1358 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1359 if (!pending_activation_entry)
1361 _dbus_verbose ("Failed to create pending activation entry\n");
1362 BUS_SET_OOM (error);
1366 pending_activation_entry->auto_activation = auto_activation;
1368 pending_activation_entry->activation_message = activation_message;
1369 dbus_message_ref (activation_message);
1370 pending_activation_entry->connection = connection;
1371 dbus_connection_ref (connection);
1373 /* Check if the service is being activated */
1374 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1375 if (pending_activation)
1377 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1379 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1381 BUS_SET_OOM (error);
1382 bus_pending_activation_entry_free (pending_activation_entry);
1386 pending_activation->n_entries += 1;
1387 pending_activation->activation->n_pending_activations += 1;
1391 pending_activation = dbus_new0 (BusPendingActivation, 1);
1392 if (!pending_activation)
1394 _dbus_verbose ("Failed to create pending activation\n");
1396 BUS_SET_OOM (error);
1397 bus_pending_activation_entry_free (pending_activation_entry);
1401 pending_activation->activation = activation;
1402 pending_activation->refcount = 1;
1404 pending_activation->service_name = _dbus_strdup (service_name);
1405 if (!pending_activation->service_name)
1407 _dbus_verbose ("Failed to copy service name for pending activation\n");
1409 BUS_SET_OOM (error);
1410 bus_pending_activation_unref (pending_activation);
1411 bus_pending_activation_entry_free (pending_activation_entry);
1415 pending_activation->exec = _dbus_strdup (entry->exec);
1416 if (!pending_activation->exec)
1418 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1419 BUS_SET_OOM (error);
1420 bus_pending_activation_unref (pending_activation);
1421 bus_pending_activation_entry_free (pending_activation_entry);
1425 pending_activation->timeout =
1426 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1427 pending_activation_timed_out,
1430 if (!pending_activation->timeout)
1432 _dbus_verbose ("Failed to create timeout for pending activation\n");
1434 BUS_SET_OOM (error);
1435 bus_pending_activation_unref (pending_activation);
1436 bus_pending_activation_entry_free (pending_activation_entry);
1440 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1441 pending_activation->timeout,
1442 handle_timeout_callback,
1446 _dbus_verbose ("Failed to add timeout for pending activation\n");
1448 BUS_SET_OOM (error);
1449 bus_pending_activation_unref (pending_activation);
1450 bus_pending_activation_entry_free (pending_activation_entry);
1454 pending_activation->timeout_added = TRUE;
1456 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1458 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1460 BUS_SET_OOM (error);
1461 bus_pending_activation_unref (pending_activation);
1462 bus_pending_activation_entry_free (pending_activation_entry);
1466 pending_activation->n_entries += 1;
1467 pending_activation->activation->n_pending_activations += 1;
1470 _dbus_hash_iter_init (activation->pending_activations, &iter);
1471 while (_dbus_hash_iter_next (&iter))
1473 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1475 if (strcmp (p->exec, entry->exec) == 0)
1482 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1483 pending_activation->service_name,
1484 pending_activation))
1486 _dbus_verbose ("Failed to put pending activation in hash table\n");
1488 BUS_SET_OOM (error);
1489 bus_pending_activation_unref (pending_activation);
1494 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1496 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1497 BUS_SET_OOM (error);
1498 _dbus_hash_table_remove_string (activation->pending_activations,
1499 pending_activation->service_name);
1503 /* FIXME we need to support a full command line, not just a single
1507 if (activated == TRUE)
1509 pending_activation->babysitter = NULL;
1513 /* Now try to spawn the process */
1514 argv[0] = entry->exec;
1517 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1518 child_setup, activation,
1521 _dbus_verbose ("Failed to spawn child\n");
1522 _DBUS_ASSERT_ERROR_IS_SET (error);
1526 _dbus_assert (pending_activation->babysitter != NULL);
1528 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1529 add_babysitter_watch,
1530 remove_babysitter_watch,
1535 BUS_SET_OOM (error);
1536 _dbus_verbose ("Failed to set babysitter watch functions\n");
1543 #ifdef DBUS_BUILD_TESTS
1547 #define SERVICE_NAME_1 "MyService1"
1548 #define SERVICE_NAME_2 "MyService2"
1549 #define SERVICE_NAME_3 "MyService3"
1551 #define SERVICE_FILE_1 "service-1.service"
1552 #define SERVICE_FILE_2 "service-2.service"
1553 #define SERVICE_FILE_3 "service-3.service"
1556 test_create_service_file (DBusString *dir,
1557 const char *filename,
1561 DBusString file_name, full_path;
1563 dbus_bool_t ret_val;
1566 _dbus_string_init_const (&file_name, filename);
1568 if (!_dbus_string_init (&full_path))
1571 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1572 !_dbus_concat_dir_and_file (&full_path, &file_name))
1578 file = fopen (_dbus_string_get_const_data (&full_path), "w");
1585 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
1589 _dbus_string_free (&full_path);
1594 test_remove_service_file (DBusString *dir, const char *filename)
1596 DBusString file_name, full_path;
1597 dbus_bool_t ret_val;
1601 _dbus_string_init_const (&file_name, filename);
1603 if (!_dbus_string_init (&full_path))
1606 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1607 !_dbus_concat_dir_and_file (&full_path, &file_name))
1613 if (!_dbus_delete_file (&full_path, NULL))
1620 _dbus_string_free (&full_path);
1625 test_remove_directory (DBusString *dir)
1628 DBusString filename, full_path;
1629 dbus_bool_t ret_val;
1633 if (!_dbus_string_init (&filename))
1636 if (!_dbus_string_init (&full_path))
1638 _dbus_string_free (&filename);
1642 iter = _dbus_directory_open (dir, NULL);
1649 while (_dbus_directory_get_next_file (iter, &filename, NULL))
1651 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
1657 _dbus_directory_close (iter);
1659 if (!_dbus_delete_directory (dir, NULL))
1666 _dbus_string_free (&filename);
1667 _dbus_string_free (&full_path);
1673 init_service_reload_test (DBusString *dir)
1677 if (!_dbus_stat (dir, &stat_buf, NULL))
1679 if (!_dbus_create_directory (dir, NULL))
1684 if (!test_remove_directory (dir))
1687 if (!_dbus_create_directory (dir, NULL))
1691 /* Create one initial file */
1692 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
1699 cleanup_service_reload_test (DBusString *dir)
1701 if (!test_remove_directory (dir))
1709 BusActivation *activation;
1710 const char *service_name;
1711 dbus_bool_t expecting_find;
1715 check_func (void *data)
1718 BusActivationEntry *entry;
1720 dbus_bool_t ret_val;
1725 dbus_error_init (&error);
1727 entry = activation_find_entry (d->activation, d->service_name, &error);
1730 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1736 if (d->expecting_find)
1740 dbus_error_free (&error);
1744 if (!d->expecting_find)
1752 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
1757 err = !_dbus_test_oom_handling (description, check_func, data);
1759 err = !check_func (data);
1762 _dbus_assert_not_reached ("Test failed");
1768 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
1770 BusActivation *activation;
1772 DBusList *directories;
1776 _dbus_string_init_const (&address, "");
1778 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
1781 activation = bus_activation_new (NULL, &address, &directories, NULL);
1785 d.activation = activation;
1787 /* Check for existing service file */
1788 d.expecting_find = TRUE;
1789 d.service_name = SERVICE_NAME_1;
1791 if (!do_test ("Existing service file", oom_test, &d))
1794 /* Check for non-existing service file */
1795 d.expecting_find = FALSE;
1796 d.service_name = SERVICE_NAME_3;
1798 if (!do_test ("Nonexisting service file", oom_test, &d))
1801 /* Check for added service file */
1802 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
1805 d.expecting_find = TRUE;
1806 d.service_name = SERVICE_NAME_2;
1808 if (!do_test ("Added service file", oom_test, &d))
1811 /* Check for removed service file */
1812 if (!test_remove_service_file (dir, SERVICE_FILE_2))
1815 d.expecting_find = FALSE;
1816 d.service_name = SERVICE_FILE_2;
1818 if (!do_test ("Removed service file", oom_test, &d))
1821 /* Check for updated service file */
1823 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
1825 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
1828 d.expecting_find = TRUE;
1829 d.service_name = SERVICE_NAME_3;
1831 if (!do_test ("Updated service file, part 1", oom_test, &d))
1834 d.expecting_find = FALSE;
1835 d.service_name = SERVICE_NAME_1;
1837 if (!do_test ("Updated service file, part 2", oom_test, &d))
1840 bus_activation_unref (activation);
1841 _dbus_list_clear (&directories);
1847 bus_activation_service_reload_test (const DBusString *test_data_dir)
1849 DBusString directory;
1851 if (!_dbus_string_init (&directory))
1854 if (!_dbus_string_append (&directory, "/tmp/dbus-reload-test-") ||
1855 !_dbus_generate_random_ascii (&directory, 6))
1860 /* Do normal tests */
1861 if (!init_service_reload_test (&directory))
1862 _dbus_assert_not_reached ("could not initiate service reload test");
1864 if (!do_service_reload_test (&directory, FALSE));
1867 if (!init_service_reload_test (&directory))
1868 _dbus_assert_not_reached ("could not initiate service reload test");
1870 if (!do_service_reload_test (&directory, TRUE));
1872 /* Cleanup test directory */
1873 if (!cleanup_service_reload_test (&directory))
1876 _dbus_string_free (&directory);
1881 #endif /* DBUS_BUILD_TESTS */