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;
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;
97 static BusServiceDirectory *
98 bus_service_directory_ref (BusServiceDirectory *dir)
100 _dbus_assert (dir->refcount);
109 bus_service_directory_unref (BusServiceDirectory *dir)
114 _dbus_assert (dir->refcount > 0);
117 if (dir->refcount > 0)
121 _dbus_hash_table_unref (dir->entries);
123 dbus_free (dir->dir_c);
128 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
130 if (entry->activation_message)
131 dbus_message_unref (entry->activation_message);
133 if (entry->connection)
134 dbus_connection_unref (entry->connection);
140 handle_timeout_callback (DBusTimeout *timeout,
143 BusPendingActivation *pending_activation = data;
145 while (!dbus_timeout_handle (pending_activation->timeout))
146 _dbus_wait_for_memory ();
149 static BusPendingActivation *
150 bus_pending_activation_ref (BusPendingActivation *pending_activation)
152 _dbus_assert (pending_activation->refcount > 0);
153 pending_activation->refcount += 1;
155 return pending_activation;
159 bus_pending_activation_unref (BusPendingActivation *pending_activation)
163 if (pending_activation == NULL) /* hash table requires this */
166 _dbus_assert (pending_activation->refcount > 0);
167 pending_activation->refcount -= 1;
169 if (pending_activation->refcount > 0)
172 if (pending_activation->timeout_added)
174 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
175 pending_activation->timeout,
176 handle_timeout_callback, pending_activation);
177 pending_activation->timeout_added = FALSE;
180 if (pending_activation->timeout)
181 _dbus_timeout_unref (pending_activation->timeout);
183 if (pending_activation->babysitter)
185 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
187 pending_activation->babysitter,
189 _dbus_assert_not_reached ("setting watch functions to NULL failed");
191 _dbus_babysitter_unref (pending_activation->babysitter);
194 dbus_free (pending_activation->service_name);
195 dbus_free (pending_activation->exec);
197 link = _dbus_list_get_first_link (&pending_activation->entries);
201 BusPendingActivationEntry *entry = link->data;
203 bus_pending_activation_entry_free (entry);
205 link = _dbus_list_get_next_link (&pending_activation->entries, link);
207 _dbus_list_clear (&pending_activation->entries);
209 pending_activation->activation->n_pending_activations -=
210 pending_activation->n_entries;
212 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
214 dbus_free (pending_activation);
217 static BusActivationEntry *
218 bus_activation_entry_ref (BusActivationEntry *entry)
220 _dbus_assert (entry->refcount > 0);
227 bus_activation_entry_unref (BusActivationEntry *entry)
229 if (entry == NULL) /* hash table requires this */
232 _dbus_assert (entry->refcount > 0);
235 if (entry->refcount > 0)
238 dbus_free (entry->name);
239 dbus_free (entry->exec);
240 dbus_free (entry->filename);
246 update_desktop_file_entry (BusActivation *activation,
247 BusServiceDirectory *s_dir,
248 DBusString *filename,
249 BusDesktopFile *desktop_file,
253 BusActivationEntry *entry;
255 DBusString file_path;
257 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
263 if (!_dbus_string_init (&file_path))
269 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
270 !_dbus_concat_dir_and_file (&file_path, filename))
276 if (!_dbus_stat (&file_path, &stat_buf, NULL))
278 dbus_set_error (error, DBUS_ERROR_FAILED,
279 "Can't stat the service file\n");
283 if (!bus_desktop_file_get_string (desktop_file,
284 DBUS_SERVICE_SECTION,
290 if (!bus_desktop_file_get_string (desktop_file,
291 DBUS_SERVICE_SECTION,
297 entry = _dbus_hash_table_lookup_string (s_dir->entries,
298 _dbus_string_get_const_data (filename));
299 if (entry == NULL) /* New file */
301 /* FIXME we need a better-defined algorithm for which service file to
302 * pick than "whichever one is first in the directory listing"
304 if (_dbus_hash_table_lookup_string (activation->entries, name))
306 dbus_set_error (error, DBUS_ERROR_FAILED,
307 "Service %s already exists in activation entry list\n", name);
311 entry = dbus_new0 (BusActivationEntry, 1);
322 entry->s_dir = s_dir;
323 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
324 if (!entry->filename)
330 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
336 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
338 /* Revert the insertion in the entries table */
339 _dbus_hash_table_remove_string (activation->entries, entry->name);
344 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
346 else /* Just update the entry */
348 bus_activation_entry_ref (entry);
349 _dbus_hash_table_remove_string (activation->entries, entry->name);
351 if (_dbus_hash_table_lookup_string (activation->entries, name))
353 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
354 name, _dbus_string_get_const_data (&file_path));
358 dbus_free (entry->name);
359 dbus_free (entry->exec);
362 if (!_dbus_hash_table_insert_string (activation->entries,
363 entry->name, bus_activation_entry_ref(entry)))
366 /* Also remove path to entries hash since we want this in sync with
367 * the entries hash table */
368 _dbus_hash_table_remove_string (entry->s_dir->entries,
370 bus_activation_entry_unref (entry);
375 entry->mtime = stat_buf.mtime;
377 _dbus_string_free (&file_path);
378 bus_activation_entry_unref (entry);
385 _dbus_string_free (&file_path);
388 bus_activation_entry_unref (entry);
394 check_service_file (BusActivation *activation,
395 BusActivationEntry *entry,
396 BusActivationEntry **updated_entry,
401 BusActivationEntry *tmp_entry;
402 DBusString file_path;
408 _dbus_string_init_const (&filename, entry->filename);
410 if (!_dbus_string_init (&file_path))
416 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
417 !_dbus_concat_dir_and_file (&file_path, &filename))
424 if (!_dbus_stat (&file_path, &stat_buf, NULL))
426 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
427 _dbus_string_get_const_data (&file_path));
429 _dbus_hash_table_remove_string (activation->entries, entry->name);
430 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
438 if (stat_buf.mtime > entry->mtime)
440 BusDesktopFile *desktop_file;
443 dbus_error_init (&tmp_error);
445 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
446 if (desktop_file == NULL)
448 _dbus_verbose ("Could not load %s: %s\n",
449 _dbus_string_get_const_data (&file_path),
451 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
453 dbus_move_error (&tmp_error, error);
457 dbus_error_free (&tmp_error);
462 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
463 * Handle these both better
465 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
467 bus_desktop_file_free (desktop_file);
468 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
470 dbus_move_error (&tmp_error, error);
474 dbus_error_free (&tmp_error);
479 bus_desktop_file_free (desktop_file);
485 _dbus_string_free (&file_path);
487 if (updated_entry != NULL)
488 *updated_entry = tmp_entry;
493 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
494 * hash entries it already added.
497 update_directory (BusActivation *activation,
498 BusServiceDirectory *s_dir,
502 DBusString dir, filename;
503 BusDesktopFile *desktop_file;
506 BusActivationEntry *entry;
507 DBusString full_path;
509 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
514 _dbus_string_init_const (&dir, s_dir->dir_c);
516 if (!_dbus_string_init (&filename))
522 if (!_dbus_string_init (&full_path))
525 _dbus_string_free (&filename);
531 /* from this point it's safe to "goto out" */
533 iter = _dbus_directory_open (&dir, error);
536 _dbus_verbose ("Failed to open directory %s: %s\n",
538 error ? error->message : "unknown");
542 /* Now read the files */
543 dbus_error_init (&tmp_error);
544 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
546 _dbus_assert (!dbus_error_is_set (&tmp_error));
548 _dbus_string_set_length (&full_path, 0);
550 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
552 _dbus_verbose ("Skipping non-.service file %s\n",
553 _dbus_string_get_const_data (&filename));
557 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
558 if (entry) /* Already has this service file in the cache */
560 if (!check_service_file (activation, entry, NULL, error))
566 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
567 !_dbus_concat_dir_and_file (&full_path, &filename))
574 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
575 if (desktop_file == NULL)
577 _dbus_verbose ("Could not load %s: %s\n",
578 _dbus_string_get_const_data (&full_path),
581 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
583 dbus_move_error (&tmp_error, error);
587 dbus_error_free (&tmp_error);
591 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
592 * Handle these both better
594 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
596 bus_desktop_file_free (desktop_file);
599 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
600 _dbus_string_get_const_data (&full_path), tmp_error.message);
602 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
604 dbus_move_error (&tmp_error, error);
608 dbus_error_free (&tmp_error);
613 bus_desktop_file_free (desktop_file);
619 if (dbus_error_is_set (&tmp_error))
621 dbus_move_error (&tmp_error, error);
629 _DBUS_ASSERT_ERROR_IS_SET (error);
631 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
634 _dbus_directory_close (iter);
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 /* only fail on OOM, it is ok if we can't read the directory */
738 if (!update_directory (activation, s_dir, error))
740 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
743 dbus_error_free (error);
746 link = _dbus_list_get_next_link (directories, link);
752 bus_activation_unref (activation);
757 bus_activation_ref (BusActivation *activation)
759 _dbus_assert (activation->refcount > 0);
761 activation->refcount += 1;
767 bus_activation_unref (BusActivation *activation)
769 _dbus_assert (activation->refcount > 0);
771 activation->refcount -= 1;
773 if (activation->refcount > 0)
776 dbus_free (activation->server_address);
777 if (activation->entries)
778 _dbus_hash_table_unref (activation->entries);
779 if (activation->pending_activations)
780 _dbus_hash_table_unref (activation->pending_activations);
781 if (activation->directories)
782 _dbus_hash_table_unref (activation->directories);
784 dbus_free (activation);
788 child_setup (void *data)
790 BusActivation *activation = data;
793 /* If no memory, we simply have the child exit, so it won't try
794 * to connect to the wrong thing.
796 if (!_dbus_setenv ("DBUS_STARTER_ADDRESS", activation->server_address))
799 type = bus_context_get_type (activation->context);
802 if (!_dbus_setenv ("DBUS_STARTER_BUS_TYPE", type))
805 if (strcmp (type, "session") == 0)
807 if (!_dbus_setenv ("DBUS_SESSION_BUS_ADDRESS",
808 activation->server_address))
811 else if (strcmp (type, "system") == 0)
813 if (!_dbus_setenv ("DBUS_SYSTEM_BUS_ADDRESS",
814 activation->server_address))
822 BusPendingActivation *pending_activation;
823 DBusPreallocatedHash *hash_entry;
824 } RestorePendingData;
827 restore_pending (void *data)
829 RestorePendingData *d = data;
831 _dbus_assert (d->pending_activation != NULL);
832 _dbus_assert (d->hash_entry != NULL);
834 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
835 d->pending_activation->service_name,
836 d->pending_activation->timeout_added);
838 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
840 d->pending_activation->service_name, d->pending_activation);
842 bus_pending_activation_ref (d->pending_activation);
844 d->hash_entry = NULL;
848 free_pending_restore_data (void *data)
850 RestorePendingData *d = data;
853 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
856 bus_pending_activation_unref (d->pending_activation);
862 add_restore_pending_to_transaction (BusTransaction *transaction,
863 BusPendingActivation *pending_activation)
865 RestorePendingData *d;
867 d = dbus_new (RestorePendingData, 1);
871 d->pending_activation = pending_activation;
872 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
874 bus_pending_activation_ref (d->pending_activation);
876 if (d->hash_entry == NULL ||
877 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
878 free_pending_restore_data))
880 free_pending_restore_data (d);
884 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
890 bus_activation_service_created (BusActivation *activation,
891 const char *service_name,
892 BusTransaction *transaction,
895 BusPendingActivation *pending_activation;
896 DBusMessage *message;
899 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
901 /* Check if it's a pending activation */
902 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
904 if (!pending_activation)
907 link = _dbus_list_get_first_link (&pending_activation->entries);
910 BusPendingActivationEntry *entry = link->data;
911 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
913 if (dbus_connection_get_is_connected (entry->connection))
915 /* Only send activation replies to regular activation requests. */
916 if (!entry->auto_activation)
918 dbus_uint32_t result;
920 message = dbus_message_new_method_return (entry->activation_message);
927 result = DBUS_START_REPLY_SUCCESS;
929 if (!dbus_message_append_args (message,
930 DBUS_TYPE_UINT32, &result,
933 dbus_message_unref (message);
938 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
940 dbus_message_unref (message);
945 dbus_message_unref (message);
959 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
961 BusTransaction *transaction,
964 BusPendingActivation *pending_activation;
967 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
969 /* Check if it's a pending activation */
970 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
971 bus_service_get_name (service));
973 if (!pending_activation)
976 link = _dbus_list_get_first_link (&pending_activation->entries);
979 BusPendingActivationEntry *entry = link->data;
980 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
982 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
984 DBusConnection *addressed_recipient;
986 addressed_recipient = bus_service_get_primary_owners_connection (service);
988 /* Check the security policy, which has the side-effect of adding an
989 * expected pending reply.
991 if (!bus_context_check_security_policy (activation->context, transaction,
995 entry->activation_message, error))
998 if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
1000 BUS_SET_OOM (error);
1008 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1010 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1011 BUS_SET_OOM (error);
1015 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1024 * FIXME @todo the error messages here would ideally be preallocated
1025 * so we don't need to allocate memory to send them.
1026 * Using the usual tactic, prealloc an OOM message, then
1027 * if we can't alloc the real error send the OOM error instead.
1030 try_send_activation_failure (BusPendingActivation *pending_activation,
1031 const DBusError *how)
1033 BusActivation *activation;
1035 BusTransaction *transaction;
1037 activation = pending_activation->activation;
1039 transaction = bus_transaction_new (activation->context);
1040 if (transaction == NULL)
1043 link = _dbus_list_get_first_link (&pending_activation->entries);
1044 while (link != NULL)
1046 BusPendingActivationEntry *entry = link->data;
1047 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1049 if (dbus_connection_get_is_connected (entry->connection))
1051 if (!bus_transaction_send_error_reply (transaction,
1054 entry->activation_message))
1061 bus_transaction_execute_and_free (transaction);
1067 bus_transaction_cancel_and_free (transaction);
1072 * Free the pending activation and send an error message to all the
1073 * connections that were waiting for it.
1076 pending_activation_failed (BusPendingActivation *pending_activation,
1077 const DBusError *how)
1079 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1080 while (!try_send_activation_failure (pending_activation, how))
1081 _dbus_wait_for_memory ();
1083 /* Destroy this pending activation */
1084 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1085 pending_activation->service_name);
1089 babysitter_watch_callback (DBusWatch *watch,
1090 unsigned int condition,
1093 BusPendingActivation *pending_activation = data;
1095 DBusBabysitter *babysitter;
1097 babysitter = pending_activation->babysitter;
1099 _dbus_babysitter_ref (babysitter);
1101 retval = dbus_watch_handle (watch, condition);
1103 /* FIXME this is broken in the same way that
1104 * connection watches used to be; there should be
1105 * a separate callback for status change, instead
1106 * of doing "if we handled a watch status might
1109 * Fixing this lets us move dbus_watch_handle
1110 * calls into dbus-mainloop.c
1113 if (_dbus_babysitter_get_child_exited (babysitter))
1118 dbus_error_init (&error);
1119 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1121 /* Destroy all pending activations with the same exec */
1122 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1124 while (_dbus_hash_iter_next (&iter))
1126 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1128 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1129 pending_activation_failed (p, &error);
1132 /* Destroys the pending activation */
1133 pending_activation_failed (pending_activation, &error);
1135 dbus_error_free (&error);
1138 _dbus_babysitter_unref (babysitter);
1144 add_babysitter_watch (DBusWatch *watch,
1147 BusPendingActivation *pending_activation = data;
1149 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1150 watch, babysitter_watch_callback, pending_activation,
1155 remove_babysitter_watch (DBusWatch *watch,
1158 BusPendingActivation *pending_activation = data;
1160 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1161 watch, babysitter_watch_callback, pending_activation);
1165 pending_activation_timed_out (void *data)
1167 BusPendingActivation *pending_activation = data;
1170 /* Kill the spawned process, since it sucks
1171 * (not sure this is what we want to do, but
1172 * may as well try it for now)
1174 if (pending_activation->babysitter)
1175 _dbus_babysitter_kill_child (pending_activation->babysitter);
1177 dbus_error_init (&error);
1179 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1180 "Activation of %s timed out",
1181 pending_activation->service_name);
1183 pending_activation_failed (pending_activation, &error);
1185 dbus_error_free (&error);
1191 cancel_pending (void *data)
1193 BusPendingActivation *pending_activation = data;
1195 _dbus_verbose ("Canceling pending activation of %s\n",
1196 pending_activation->service_name);
1198 if (pending_activation->babysitter)
1199 _dbus_babysitter_kill_child (pending_activation->babysitter);
1201 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1202 pending_activation->service_name);
1206 free_pending_cancel_data (void *data)
1208 BusPendingActivation *pending_activation = data;
1210 bus_pending_activation_unref (pending_activation);
1214 add_cancel_pending_to_transaction (BusTransaction *transaction,
1215 BusPendingActivation *pending_activation)
1217 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1219 free_pending_cancel_data))
1222 bus_pending_activation_ref (pending_activation);
1224 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1230 update_service_cache (BusActivation *activation, DBusError *error)
1234 _dbus_hash_iter_init (activation->directories, &iter);
1235 while (_dbus_hash_iter_next (&iter))
1237 DBusError tmp_error;
1238 BusServiceDirectory *s_dir;
1240 s_dir = _dbus_hash_iter_get_value (&iter);
1242 dbus_error_init (&tmp_error);
1243 if (!update_directory (activation, s_dir, &tmp_error))
1245 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1247 dbus_move_error (&tmp_error, error);
1251 dbus_error_free (&tmp_error);
1259 static BusActivationEntry *
1260 activation_find_entry (BusActivation *activation,
1261 const char *service_name,
1264 BusActivationEntry *entry;
1266 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1269 if (!update_service_cache (activation, error))
1272 entry = _dbus_hash_table_lookup_string (activation->entries,
1277 BusActivationEntry *updated_entry;
1279 if (!check_service_file (activation, entry, &updated_entry, error))
1282 entry = updated_entry;
1287 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1288 "The name %s was not provided by any .service files",
1297 bus_activation_activate_service (BusActivation *activation,
1298 DBusConnection *connection,
1299 BusTransaction *transaction,
1300 dbus_bool_t auto_activation,
1301 DBusMessage *activation_message,
1302 const char *service_name,
1305 BusActivationEntry *entry;
1306 BusPendingActivation *pending_activation;
1307 BusPendingActivationEntry *pending_activation_entry;
1308 DBusMessage *message;
1309 DBusString service_str;
1315 dbus_bool_t activated;
1319 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1321 if (activation->n_pending_activations >=
1322 bus_context_get_max_pending_activations (activation->context))
1324 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1325 "The maximum number of pending activations has been reached, activation of %s failed",
1330 entry = activation_find_entry (activation, service_name, error);
1334 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1335 * call us if the service is already active.
1337 if (!auto_activation)
1339 /* Check if the service is active */
1340 _dbus_string_init_const (&service_str, service_name);
1341 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1343 dbus_uint32_t result;
1345 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1347 message = dbus_message_new_method_return (activation_message);
1351 _dbus_verbose ("No memory to create reply to activate message\n");
1352 BUS_SET_OOM (error);
1356 result = DBUS_START_REPLY_ALREADY_RUNNING;
1358 if (!dbus_message_append_args (message,
1359 DBUS_TYPE_UINT32, &result,
1362 _dbus_verbose ("No memory to set args of reply to activate message\n");
1363 BUS_SET_OOM (error);
1364 dbus_message_unref (message);
1368 retval = bus_transaction_send_from_driver (transaction, connection, message);
1369 dbus_message_unref (message);
1372 _dbus_verbose ("Failed to send reply\n");
1373 BUS_SET_OOM (error);
1380 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1381 if (!pending_activation_entry)
1383 _dbus_verbose ("Failed to create pending activation entry\n");
1384 BUS_SET_OOM (error);
1388 pending_activation_entry->auto_activation = auto_activation;
1390 pending_activation_entry->activation_message = activation_message;
1391 dbus_message_ref (activation_message);
1392 pending_activation_entry->connection = connection;
1393 dbus_connection_ref (connection);
1395 /* Check if the service is being activated */
1396 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1397 if (pending_activation)
1399 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1401 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1403 BUS_SET_OOM (error);
1404 bus_pending_activation_entry_free (pending_activation_entry);
1408 pending_activation->n_entries += 1;
1409 pending_activation->activation->n_pending_activations += 1;
1413 pending_activation = dbus_new0 (BusPendingActivation, 1);
1414 if (!pending_activation)
1416 _dbus_verbose ("Failed to create pending activation\n");
1418 BUS_SET_OOM (error);
1419 bus_pending_activation_entry_free (pending_activation_entry);
1423 pending_activation->activation = activation;
1424 pending_activation->refcount = 1;
1426 pending_activation->service_name = _dbus_strdup (service_name);
1427 if (!pending_activation->service_name)
1429 _dbus_verbose ("Failed to copy service name for pending activation\n");
1431 BUS_SET_OOM (error);
1432 bus_pending_activation_unref (pending_activation);
1433 bus_pending_activation_entry_free (pending_activation_entry);
1437 pending_activation->exec = _dbus_strdup (entry->exec);
1438 if (!pending_activation->exec)
1440 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1441 BUS_SET_OOM (error);
1442 bus_pending_activation_unref (pending_activation);
1443 bus_pending_activation_entry_free (pending_activation_entry);
1447 pending_activation->timeout =
1448 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1449 pending_activation_timed_out,
1452 if (!pending_activation->timeout)
1454 _dbus_verbose ("Failed to create timeout for pending activation\n");
1456 BUS_SET_OOM (error);
1457 bus_pending_activation_unref (pending_activation);
1458 bus_pending_activation_entry_free (pending_activation_entry);
1462 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1463 pending_activation->timeout,
1464 handle_timeout_callback,
1468 _dbus_verbose ("Failed to add timeout for pending activation\n");
1470 BUS_SET_OOM (error);
1471 bus_pending_activation_unref (pending_activation);
1472 bus_pending_activation_entry_free (pending_activation_entry);
1476 pending_activation->timeout_added = TRUE;
1478 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1480 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1482 BUS_SET_OOM (error);
1483 bus_pending_activation_unref (pending_activation);
1484 bus_pending_activation_entry_free (pending_activation_entry);
1488 pending_activation->n_entries += 1;
1489 pending_activation->activation->n_pending_activations += 1;
1492 _dbus_hash_iter_init (activation->pending_activations, &iter);
1493 while (_dbus_hash_iter_next (&iter))
1495 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1497 if (strcmp (p->exec, entry->exec) == 0)
1504 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1505 pending_activation->service_name,
1506 pending_activation))
1508 _dbus_verbose ("Failed to put pending activation in hash table\n");
1510 BUS_SET_OOM (error);
1511 bus_pending_activation_unref (pending_activation);
1516 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1518 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1519 BUS_SET_OOM (error);
1520 _dbus_hash_table_remove_string (activation->pending_activations,
1521 pending_activation->service_name);
1529 /* Now try to spawn the process */
1530 if (!_dbus_shell_parse_argv (entry->exec, &argc, &argv, error))
1532 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
1533 _DBUS_ASSERT_ERROR_IS_SET (error);
1535 _dbus_hash_table_remove_string (activation->pending_activations,
1536 pending_activation->service_name);
1541 _dbus_verbose ("Spawning %s ...\n", argv[0]);
1542 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1544 child_setup, activation,
1547 _dbus_verbose ("Failed to spawn child\n");
1548 _DBUS_ASSERT_ERROR_IS_SET (error);
1549 dbus_free_string_array (argv);
1554 dbus_free_string_array (argv);
1556 _dbus_assert (pending_activation->babysitter != NULL);
1558 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1559 add_babysitter_watch,
1560 remove_babysitter_watch,
1565 BUS_SET_OOM (error);
1566 _dbus_verbose ("Failed to set babysitter watch functions\n");
1574 bus_activation_list_services (BusActivation *activation,
1582 len = _dbus_hash_table_get_n_entries (activation->entries);
1583 retval = dbus_new (char *, len + 1);
1588 _dbus_hash_iter_init (activation->entries, &iter);
1590 while (_dbus_hash_iter_next (&iter))
1592 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
1594 retval[i] = _dbus_strdup (entry->name);
1595 if (retval[i] == NULL)
1610 for (j = 0; j < i; j++)
1611 dbus_free (retval[i]);
1618 #ifdef DBUS_BUILD_TESTS
1622 #define SERVICE_NAME_1 "MyService1"
1623 #define SERVICE_NAME_2 "MyService2"
1624 #define SERVICE_NAME_3 "MyService3"
1626 #define SERVICE_FILE_1 "service-1.service"
1627 #define SERVICE_FILE_2 "service-2.service"
1628 #define SERVICE_FILE_3 "service-3.service"
1631 test_create_service_file (DBusString *dir,
1632 const char *filename,
1636 DBusString file_name, full_path;
1638 dbus_bool_t ret_val;
1641 _dbus_string_init_const (&file_name, filename);
1643 if (!_dbus_string_init (&full_path))
1646 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1647 !_dbus_concat_dir_and_file (&full_path, &file_name))
1653 file = fopen (_dbus_string_get_const_data (&full_path), "w");
1660 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
1664 _dbus_string_free (&full_path);
1669 test_remove_service_file (DBusString *dir, const char *filename)
1671 DBusString file_name, full_path;
1672 dbus_bool_t ret_val;
1676 _dbus_string_init_const (&file_name, filename);
1678 if (!_dbus_string_init (&full_path))
1681 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1682 !_dbus_concat_dir_and_file (&full_path, &file_name))
1688 if (!_dbus_delete_file (&full_path, NULL))
1695 _dbus_string_free (&full_path);
1700 test_remove_directory (DBusString *dir)
1703 DBusString filename, full_path;
1704 dbus_bool_t ret_val;
1708 if (!_dbus_string_init (&filename))
1711 if (!_dbus_string_init (&full_path))
1713 _dbus_string_free (&filename);
1717 iter = _dbus_directory_open (dir, NULL);
1724 while (_dbus_directory_get_next_file (iter, &filename, NULL))
1726 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
1732 _dbus_directory_close (iter);
1734 if (!_dbus_delete_directory (dir, NULL))
1741 _dbus_string_free (&filename);
1742 _dbus_string_free (&full_path);
1748 init_service_reload_test (DBusString *dir)
1752 if (!_dbus_stat (dir, &stat_buf, NULL))
1754 if (!_dbus_create_directory (dir, NULL))
1759 if (!test_remove_directory (dir))
1762 if (!_dbus_create_directory (dir, NULL))
1766 /* Create one initial file */
1767 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
1774 cleanup_service_reload_test (DBusString *dir)
1776 if (!test_remove_directory (dir))
1784 BusActivation *activation;
1785 const char *service_name;
1786 dbus_bool_t expecting_find;
1790 check_func (void *data)
1793 BusActivationEntry *entry;
1795 dbus_bool_t ret_val;
1800 dbus_error_init (&error);
1802 entry = activation_find_entry (d->activation, d->service_name, &error);
1805 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1811 if (d->expecting_find)
1815 dbus_error_free (&error);
1819 if (!d->expecting_find)
1827 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
1832 err = !_dbus_test_oom_handling (description, check_func, data);
1834 err = !check_func (data);
1837 _dbus_assert_not_reached ("Test failed");
1843 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
1845 BusActivation *activation;
1847 DBusList *directories;
1851 _dbus_string_init_const (&address, "");
1853 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
1856 activation = bus_activation_new (NULL, &address, &directories, NULL);
1860 d.activation = activation;
1862 /* Check for existing service file */
1863 d.expecting_find = TRUE;
1864 d.service_name = SERVICE_NAME_1;
1866 if (!do_test ("Existing service file", oom_test, &d))
1869 /* Check for non-existing service file */
1870 d.expecting_find = FALSE;
1871 d.service_name = SERVICE_NAME_3;
1873 if (!do_test ("Nonexisting service file", oom_test, &d))
1876 /* Check for added service file */
1877 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
1880 d.expecting_find = TRUE;
1881 d.service_name = SERVICE_NAME_2;
1883 if (!do_test ("Added service file", oom_test, &d))
1886 /* Check for removed service file */
1887 if (!test_remove_service_file (dir, SERVICE_FILE_2))
1890 d.expecting_find = FALSE;
1891 d.service_name = SERVICE_FILE_2;
1893 if (!do_test ("Removed service file", oom_test, &d))
1896 /* Check for updated service file */
1898 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
1900 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
1903 d.expecting_find = TRUE;
1904 d.service_name = SERVICE_NAME_3;
1906 if (!do_test ("Updated service file, part 1", oom_test, &d))
1909 d.expecting_find = FALSE;
1910 d.service_name = SERVICE_NAME_1;
1912 if (!do_test ("Updated service file, part 2", oom_test, &d))
1915 bus_activation_unref (activation);
1916 _dbus_list_clear (&directories);
1922 bus_activation_service_reload_test (const DBusString *test_data_dir)
1924 DBusString directory;
1926 if (!_dbus_string_init (&directory))
1929 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
1932 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
1933 !_dbus_generate_random_ascii (&directory, 6))
1938 /* Do normal tests */
1939 if (!init_service_reload_test (&directory))
1940 _dbus_assert_not_reached ("could not initiate service reload test");
1942 if (!do_service_reload_test (&directory, FALSE))
1946 if (!init_service_reload_test (&directory))
1947 _dbus_assert_not_reached ("could not initiate service reload test");
1949 if (!do_service_reload_test (&directory, TRUE))
1952 /* Cleanup test directory */
1953 if (!cleanup_service_reload_test (&directory))
1956 _dbus_string_free (&directory);
1961 #endif /* DBUS_BUILD_TESTS */