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.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 "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-shell.h>
34 #include <dbus/dbus-spawn.h>
35 #include <dbus/dbus-timeout.h>
36 #include <dbus/dbus-sysdeps.h>
40 #define DBUS_SERVICE_SECTION "D-BUS Service"
41 #define DBUS_SERVICE_NAME "Name"
42 #define DBUS_SERVICE_EXEC "Exec"
47 DBusHashTable *entries;
48 DBusHashTable *pending_activations;
51 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
52 * i.e. number of pending activation requests, not pending
55 DBusHashTable *directories;
62 DBusHashTable *entries;
63 } 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->filename);
248 update_desktop_file_entry (BusActivation *activation,
249 BusServiceDirectory *s_dir,
250 DBusString *filename,
251 BusDesktopFile *desktop_file,
255 BusActivationEntry *entry;
257 DBusString file_path;
259 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
265 if (!_dbus_string_init (&file_path))
271 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
272 !_dbus_concat_dir_and_file (&file_path, filename))
278 if (!_dbus_stat (&file_path, &stat_buf, NULL))
280 dbus_set_error (error, DBUS_ERROR_FAILED,
281 "Can't stat the service file\n");
285 if (!bus_desktop_file_get_string (desktop_file,
286 DBUS_SERVICE_SECTION,
290 dbus_set_error (error, DBUS_ERROR_FAILED,
291 "No \""DBUS_SERVICE_NAME"\" key in .service file\n");
295 if (!bus_desktop_file_get_string (desktop_file,
296 DBUS_SERVICE_SECTION,
300 dbus_set_error (error, DBUS_ERROR_FAILED,
301 "No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
305 entry = _dbus_hash_table_lookup_string (s_dir->entries,
306 _dbus_string_get_const_data (filename));
307 if (entry == NULL) /* New file */
309 /* FIXME we need a better-defined algorithm for which service file to
310 * pick than "whichever one is first in the directory listing"
312 if (_dbus_hash_table_lookup_string (activation->entries, name))
314 dbus_set_error (error, DBUS_ERROR_FAILED,
315 "Service %s already exists in activation entry list\n", name);
319 entry = dbus_new0 (BusActivationEntry, 1);
330 entry->s_dir = s_dir;
331 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
332 if (!entry->filename)
338 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
344 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
346 /* Revert the insertion in the entries table */
347 _dbus_hash_table_remove_string (activation->entries, entry->name);
352 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
354 else /* Just update the entry */
356 bus_activation_entry_ref (entry);
357 _dbus_hash_table_remove_string (activation->entries, entry->name);
359 if (_dbus_hash_table_lookup_string (activation->entries, name))
361 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
362 name, _dbus_string_get_const_data (&file_path));
366 dbus_free (entry->name);
367 dbus_free (entry->exec);
370 if (!_dbus_hash_table_insert_string (activation->entries,
371 entry->name, bus_activation_entry_ref(entry)))
374 /* Also remove path to entries hash since we want this in sync with
375 * the entries hash table */
376 _dbus_hash_table_remove_string (entry->s_dir->entries,
378 bus_activation_entry_unref (entry);
383 entry->mtime = stat_buf.mtime;
385 _dbus_string_free (&file_path);
386 bus_activation_entry_unref (entry);
393 _dbus_string_free (&file_path);
396 bus_activation_entry_unref (entry);
402 check_service_file (BusActivation *activation,
403 BusActivationEntry *entry,
404 BusActivationEntry **updated_entry,
409 BusActivationEntry *tmp_entry;
410 DBusString file_path;
416 _dbus_string_init_const (&filename, entry->filename);
418 if (!_dbus_string_init (&file_path))
424 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
425 !_dbus_concat_dir_and_file (&file_path, &filename))
432 if (!_dbus_stat (&file_path, &stat_buf, NULL))
434 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
435 _dbus_string_get_const_data (&file_path));
437 _dbus_hash_table_remove_string (activation->entries, entry->name);
438 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
446 if (stat_buf.mtime > entry->mtime)
448 BusDesktopFile *desktop_file;
451 dbus_error_init (&tmp_error);
453 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
454 if (desktop_file == NULL)
456 _dbus_verbose ("Could not load %s: %s\n",
457 _dbus_string_get_const_data (&file_path),
459 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
461 dbus_move_error (&tmp_error, error);
465 dbus_error_free (&tmp_error);
470 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
472 bus_desktop_file_free (desktop_file);
473 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
475 dbus_move_error (&tmp_error, error);
479 dbus_error_free (&tmp_error);
484 bus_desktop_file_free (desktop_file);
490 _dbus_string_free (&file_path);
492 if (updated_entry != NULL)
493 *updated_entry = tmp_entry;
498 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
499 * hash entries it already added.
502 update_directory (BusActivation *activation,
503 BusServiceDirectory *s_dir,
507 DBusString dir, filename;
508 BusDesktopFile *desktop_file;
511 BusActivationEntry *entry;
512 DBusString full_path;
514 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
519 _dbus_string_init_const (&dir, s_dir->dir_c);
521 if (!_dbus_string_init (&filename))
527 if (!_dbus_string_init (&full_path))
530 _dbus_string_free (&filename);
536 /* from this point it's safe to "goto out" */
538 iter = _dbus_directory_open (&dir, error);
541 _dbus_verbose ("Failed to open directory %s: %s\n",
543 error ? error->message : "unknown");
547 /* Now read the files */
548 dbus_error_init (&tmp_error);
549 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
551 _dbus_assert (!dbus_error_is_set (&tmp_error));
553 _dbus_string_set_length (&full_path, 0);
555 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
557 _dbus_verbose ("Skipping non-.service file %s\n",
558 _dbus_string_get_const_data (&filename));
562 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
563 if (entry) /* Already has this service file in the cache */
565 if (!check_service_file (activation, entry, NULL, error))
571 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
572 !_dbus_concat_dir_and_file (&full_path, &filename))
579 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
580 if (desktop_file == NULL)
582 _dbus_verbose ("Could not load %s: %s\n",
583 _dbus_string_get_const_data (&full_path),
586 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
588 dbus_move_error (&tmp_error, error);
592 dbus_error_free (&tmp_error);
596 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
598 bus_desktop_file_free (desktop_file);
601 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
602 _dbus_string_get_const_data (&full_path), tmp_error.message);
604 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
606 dbus_move_error (&tmp_error, error);
610 dbus_error_free (&tmp_error);
615 bus_desktop_file_free (desktop_file);
621 if (dbus_error_is_set (&tmp_error))
623 dbus_move_error (&tmp_error, error);
631 _DBUS_ASSERT_ERROR_IS_SET (error);
633 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
636 _dbus_directory_close (iter);
637 _dbus_string_free (&filename);
638 _dbus_string_free (&full_path);
644 bus_activation_new (BusContext *context,
645 const DBusString *address,
646 DBusList **directories,
649 BusActivation *activation;
653 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
655 activation = dbus_new0 (BusActivation, 1);
656 if (activation == NULL)
662 activation->refcount = 1;
663 activation->context = context;
664 activation->n_pending_activations = 0;
666 if (!_dbus_string_copy_data (address, &activation->server_address))
672 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
673 (DBusFreeFunction)bus_activation_entry_unref);
674 if (activation->entries == NULL)
680 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
681 (DBusFreeFunction)bus_pending_activation_unref);
683 if (activation->pending_activations == NULL)
689 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
690 (DBusFreeFunction)bus_service_directory_unref);
692 if (activation->directories == NULL)
698 /* Load service files */
699 link = _dbus_list_get_first_link (directories);
702 BusServiceDirectory *s_dir;
704 dir = _dbus_strdup ((const char *) link->data);
711 s_dir = dbus_new0 (BusServiceDirectory, 1);
722 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
723 (DBusFreeFunction)bus_activation_entry_unref);
727 bus_service_directory_unref (s_dir);
732 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
734 bus_service_directory_unref (s_dir);
739 if (!update_directory (activation, s_dir, error))
742 link = _dbus_list_get_next_link (directories, link);
748 bus_activation_unref (activation);
753 bus_activation_ref (BusActivation *activation)
755 _dbus_assert (activation->refcount > 0);
757 activation->refcount += 1;
763 bus_activation_unref (BusActivation *activation)
765 _dbus_assert (activation->refcount > 0);
767 activation->refcount -= 1;
769 if (activation->refcount > 0)
772 dbus_free (activation->server_address);
773 if (activation->entries)
774 _dbus_hash_table_unref (activation->entries);
775 if (activation->pending_activations)
776 _dbus_hash_table_unref (activation->pending_activations);
777 if (activation->directories)
778 _dbus_hash_table_unref (activation->directories);
780 dbus_free (activation);
784 child_setup (void *data)
786 BusActivation *activation = data;
789 /* If no memory, we simply have the child exit, so it won't try
790 * to connect to the wrong thing.
792 if (!_dbus_setenv ("DBUS_STARTER_ADDRESS", activation->server_address))
795 type = bus_context_get_type (activation->context);
798 if (!_dbus_setenv ("DBUS_STARTER_BUS_TYPE", type))
801 if (strcmp (type, "session") == 0)
803 if (!_dbus_setenv ("DBUS_SESSION_BUS_ADDRESS",
804 activation->server_address))
807 else if (strcmp (type, "system") == 0)
809 if (!_dbus_setenv ("DBUS_SYSTEM_BUS_ADDRESS",
810 activation->server_address))
818 BusPendingActivation *pending_activation;
819 DBusPreallocatedHash *hash_entry;
820 } RestorePendingData;
823 restore_pending (void *data)
825 RestorePendingData *d = data;
827 _dbus_assert (d->pending_activation != NULL);
828 _dbus_assert (d->hash_entry != NULL);
830 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
831 d->pending_activation->service_name,
832 d->pending_activation->timeout_added);
834 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
836 d->pending_activation->service_name, d->pending_activation);
838 bus_pending_activation_ref (d->pending_activation);
840 d->hash_entry = NULL;
844 free_pending_restore_data (void *data)
846 RestorePendingData *d = data;
849 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
852 bus_pending_activation_unref (d->pending_activation);
858 add_restore_pending_to_transaction (BusTransaction *transaction,
859 BusPendingActivation *pending_activation)
861 RestorePendingData *d;
863 d = dbus_new (RestorePendingData, 1);
867 d->pending_activation = pending_activation;
868 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
870 bus_pending_activation_ref (d->pending_activation);
872 if (d->hash_entry == NULL ||
873 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
874 free_pending_restore_data))
876 free_pending_restore_data (d);
880 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
886 bus_activation_service_created (BusActivation *activation,
887 const char *service_name,
888 BusTransaction *transaction,
891 BusPendingActivation *pending_activation;
892 DBusMessage *message;
895 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
897 /* Check if it's a pending activation */
898 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
900 if (!pending_activation)
903 link = _dbus_list_get_first_link (&pending_activation->entries);
906 BusPendingActivationEntry *entry = link->data;
907 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
909 if (dbus_connection_get_is_connected (entry->connection))
911 /* Only send activation replies to regular activation requests. */
912 if (!entry->auto_activation)
914 dbus_uint32_t result;
916 message = dbus_message_new_method_return (entry->activation_message);
923 result = DBUS_START_REPLY_SUCCESS;
925 if (!dbus_message_append_args (message,
926 DBUS_TYPE_UINT32, &result,
929 dbus_message_unref (message);
934 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
936 dbus_message_unref (message);
941 dbus_message_unref (message);
955 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
957 BusTransaction *transaction,
960 BusPendingActivation *pending_activation;
963 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
965 /* Check if it's a pending activation */
966 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
967 bus_service_get_name (service));
969 if (!pending_activation)
972 link = _dbus_list_get_first_link (&pending_activation->entries);
975 BusPendingActivationEntry *entry = link->data;
976 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
978 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
980 DBusConnection *addressed_recipient;
982 addressed_recipient = bus_service_get_primary_owners_connection (service);
984 /* Check the security policy, which has the side-effect of adding an
985 * expected pending reply.
987 if (!bus_context_check_security_policy (activation->context, transaction,
991 entry->activation_message, error))
994 if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
1004 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1006 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1007 BUS_SET_OOM (error);
1011 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1020 * FIXME @todo 1.0? the error messages here would ideally be preallocated
1021 * so we don't need to allocate memory to send them.
1022 * Using the usual tactic, prealloc an OOM message, then
1023 * if we can't alloc the real error send the OOM error instead.
1026 try_send_activation_failure (BusPendingActivation *pending_activation,
1027 const DBusError *how)
1029 BusActivation *activation;
1031 BusTransaction *transaction;
1033 activation = pending_activation->activation;
1035 transaction = bus_transaction_new (activation->context);
1036 if (transaction == NULL)
1039 link = _dbus_list_get_first_link (&pending_activation->entries);
1040 while (link != NULL)
1042 BusPendingActivationEntry *entry = link->data;
1043 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1045 if (dbus_connection_get_is_connected (entry->connection))
1047 if (!bus_transaction_send_error_reply (transaction,
1050 entry->activation_message))
1057 bus_transaction_execute_and_free (transaction);
1063 bus_transaction_cancel_and_free (transaction);
1068 * Free the pending activation and send an error message to all the
1069 * connections that were waiting for it.
1072 pending_activation_failed (BusPendingActivation *pending_activation,
1073 const DBusError *how)
1075 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1076 while (!try_send_activation_failure (pending_activation, how))
1077 _dbus_wait_for_memory ();
1079 /* Destroy this pending activation */
1080 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1081 pending_activation->service_name);
1085 babysitter_watch_callback (DBusWatch *watch,
1086 unsigned int condition,
1089 BusPendingActivation *pending_activation = data;
1091 DBusBabysitter *babysitter;
1093 babysitter = pending_activation->babysitter;
1095 _dbus_babysitter_ref (babysitter);
1097 retval = dbus_watch_handle (watch, condition);
1099 /* FIXME this is broken in the same way that
1100 * connection watches used to be; there should be
1101 * a separate callback for status change, instead
1102 * of doing "if we handled a watch status might
1105 * Fixing this lets us move dbus_watch_handle
1106 * calls into dbus-mainloop.c
1109 if (_dbus_babysitter_get_child_exited (babysitter))
1114 dbus_error_init (&error);
1115 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1117 /* Destroy all pending activations with the same exec */
1118 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1120 while (_dbus_hash_iter_next (&iter))
1122 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1124 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1125 pending_activation_failed (p, &error);
1128 /* Destroys the pending activation */
1129 pending_activation_failed (pending_activation, &error);
1131 dbus_error_free (&error);
1134 _dbus_babysitter_unref (babysitter);
1140 add_babysitter_watch (DBusWatch *watch,
1143 BusPendingActivation *pending_activation = data;
1145 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1146 watch, babysitter_watch_callback, pending_activation,
1151 remove_babysitter_watch (DBusWatch *watch,
1154 BusPendingActivation *pending_activation = data;
1156 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1157 watch, babysitter_watch_callback, pending_activation);
1161 pending_activation_timed_out (void *data)
1163 BusPendingActivation *pending_activation = data;
1166 /* Kill the spawned process, since it sucks
1167 * (not sure this is what we want to do, but
1168 * may as well try it for now)
1170 if (pending_activation->babysitter)
1171 _dbus_babysitter_kill_child (pending_activation->babysitter);
1173 dbus_error_init (&error);
1175 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1176 "Activation of %s timed out",
1177 pending_activation->service_name);
1179 pending_activation_failed (pending_activation, &error);
1181 dbus_error_free (&error);
1187 cancel_pending (void *data)
1189 BusPendingActivation *pending_activation = data;
1191 _dbus_verbose ("Canceling pending activation of %s\n",
1192 pending_activation->service_name);
1194 if (pending_activation->babysitter)
1195 _dbus_babysitter_kill_child (pending_activation->babysitter);
1197 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1198 pending_activation->service_name);
1202 free_pending_cancel_data (void *data)
1204 BusPendingActivation *pending_activation = data;
1206 bus_pending_activation_unref (pending_activation);
1210 add_cancel_pending_to_transaction (BusTransaction *transaction,
1211 BusPendingActivation *pending_activation)
1213 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1215 free_pending_cancel_data))
1218 bus_pending_activation_ref (pending_activation);
1220 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1226 update_service_cache (BusActivation *activation, DBusError *error)
1230 _dbus_hash_iter_init (activation->directories, &iter);
1231 while (_dbus_hash_iter_next (&iter))
1233 DBusError tmp_error;
1234 BusServiceDirectory *s_dir;
1236 s_dir = _dbus_hash_iter_get_value (&iter);
1238 dbus_error_init (&tmp_error);
1239 if (!update_directory (activation, s_dir, &tmp_error))
1241 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1243 dbus_move_error (&tmp_error, error);
1247 dbus_error_free (&tmp_error);
1255 static BusActivationEntry *
1256 activation_find_entry (BusActivation *activation,
1257 const char *service_name,
1260 BusActivationEntry *entry;
1262 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1265 if (!update_service_cache (activation, error))
1268 entry = _dbus_hash_table_lookup_string (activation->entries,
1273 BusActivationEntry *updated_entry;
1275 if (!check_service_file (activation, entry, &updated_entry, error))
1278 entry = updated_entry;
1283 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1284 "The name %s was not provided by any .service files",
1293 bus_activation_activate_service (BusActivation *activation,
1294 DBusConnection *connection,
1295 BusTransaction *transaction,
1296 dbus_bool_t auto_activation,
1297 DBusMessage *activation_message,
1298 const char *service_name,
1301 BusActivationEntry *entry;
1302 BusPendingActivation *pending_activation;
1303 BusPendingActivationEntry *pending_activation_entry;
1304 DBusMessage *message;
1305 DBusString service_str;
1310 dbus_bool_t activated;
1314 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1316 if (activation->n_pending_activations >=
1317 bus_context_get_max_pending_activations (activation->context))
1319 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1320 "The maximum number of pending activations has been reached, activation of %s failed",
1325 entry = activation_find_entry (activation, service_name, error);
1329 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1330 * call us if the service is already active.
1332 if (!auto_activation)
1334 /* Check if the service is active */
1335 _dbus_string_init_const (&service_str, service_name);
1336 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1338 dbus_uint32_t result;
1340 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1342 message = dbus_message_new_method_return (activation_message);
1346 _dbus_verbose ("No memory to create reply to activate message\n");
1347 BUS_SET_OOM (error);
1351 result = DBUS_START_REPLY_ALREADY_RUNNING;
1353 if (!dbus_message_append_args (message,
1354 DBUS_TYPE_UINT32, &result,
1357 _dbus_verbose ("No memory to set args of reply to activate message\n");
1358 BUS_SET_OOM (error);
1359 dbus_message_unref (message);
1363 retval = bus_transaction_send_from_driver (transaction, connection, message);
1364 dbus_message_unref (message);
1367 _dbus_verbose ("Failed to send reply\n");
1368 BUS_SET_OOM (error);
1375 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1376 if (!pending_activation_entry)
1378 _dbus_verbose ("Failed to create pending activation entry\n");
1379 BUS_SET_OOM (error);
1383 pending_activation_entry->auto_activation = auto_activation;
1385 pending_activation_entry->activation_message = activation_message;
1386 dbus_message_ref (activation_message);
1387 pending_activation_entry->connection = connection;
1388 dbus_connection_ref (connection);
1390 /* Check if the service is being activated */
1391 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1392 if (pending_activation)
1394 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1396 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1398 BUS_SET_OOM (error);
1399 bus_pending_activation_entry_free (pending_activation_entry);
1403 pending_activation->n_entries += 1;
1404 pending_activation->activation->n_pending_activations += 1;
1408 pending_activation = dbus_new0 (BusPendingActivation, 1);
1409 if (!pending_activation)
1411 _dbus_verbose ("Failed to create pending activation\n");
1413 BUS_SET_OOM (error);
1414 bus_pending_activation_entry_free (pending_activation_entry);
1418 pending_activation->activation = activation;
1419 pending_activation->refcount = 1;
1421 pending_activation->service_name = _dbus_strdup (service_name);
1422 if (!pending_activation->service_name)
1424 _dbus_verbose ("Failed to copy service name for pending activation\n");
1426 BUS_SET_OOM (error);
1427 bus_pending_activation_unref (pending_activation);
1428 bus_pending_activation_entry_free (pending_activation_entry);
1432 pending_activation->exec = _dbus_strdup (entry->exec);
1433 if (!pending_activation->exec)
1435 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1436 BUS_SET_OOM (error);
1437 bus_pending_activation_unref (pending_activation);
1438 bus_pending_activation_entry_free (pending_activation_entry);
1442 pending_activation->timeout =
1443 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1444 pending_activation_timed_out,
1447 if (!pending_activation->timeout)
1449 _dbus_verbose ("Failed to create timeout for pending activation\n");
1451 BUS_SET_OOM (error);
1452 bus_pending_activation_unref (pending_activation);
1453 bus_pending_activation_entry_free (pending_activation_entry);
1457 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1458 pending_activation->timeout,
1459 handle_timeout_callback,
1463 _dbus_verbose ("Failed to add timeout for pending activation\n");
1465 BUS_SET_OOM (error);
1466 bus_pending_activation_unref (pending_activation);
1467 bus_pending_activation_entry_free (pending_activation_entry);
1471 pending_activation->timeout_added = TRUE;
1473 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1475 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1477 BUS_SET_OOM (error);
1478 bus_pending_activation_unref (pending_activation);
1479 bus_pending_activation_entry_free (pending_activation_entry);
1483 pending_activation->n_entries += 1;
1484 pending_activation->activation->n_pending_activations += 1;
1487 _dbus_hash_iter_init (activation->pending_activations, &iter);
1488 while (_dbus_hash_iter_next (&iter))
1490 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1492 if (strcmp (p->exec, entry->exec) == 0)
1499 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1500 pending_activation->service_name,
1501 pending_activation))
1503 _dbus_verbose ("Failed to put pending activation in hash table\n");
1505 BUS_SET_OOM (error);
1506 bus_pending_activation_unref (pending_activation);
1511 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1513 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1514 BUS_SET_OOM (error);
1515 _dbus_hash_table_remove_string (activation->pending_activations,
1516 pending_activation->service_name);
1524 /* Now try to spawn the process */
1525 if (!_dbus_shell_parse_argv (entry->exec, &argc, &argv, error))
1527 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
1528 _DBUS_ASSERT_ERROR_IS_SET (error);
1530 _dbus_hash_table_remove_string (activation->pending_activations,
1531 pending_activation->service_name);
1536 _dbus_verbose ("Spawning %s ...\n", argv[0]);
1537 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1538 child_setup, activation,
1541 _dbus_verbose ("Failed to spawn child\n");
1542 _DBUS_ASSERT_ERROR_IS_SET (error);
1543 dbus_free_string_array (argv);
1548 dbus_free_string_array (argv);
1550 _dbus_assert (pending_activation->babysitter != NULL);
1552 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1553 add_babysitter_watch,
1554 remove_babysitter_watch,
1559 BUS_SET_OOM (error);
1560 _dbus_verbose ("Failed to set babysitter watch functions\n");
1568 bus_activation_list_services (BusActivation *activation,
1576 len = _dbus_hash_table_get_n_entries (activation->entries);
1577 retval = dbus_new (char *, len + 1);
1582 _dbus_hash_iter_init (activation->entries, &iter);
1584 while (_dbus_hash_iter_next (&iter))
1586 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
1588 retval[i] = _dbus_strdup (entry->name);
1589 if (retval[i] == NULL)
1604 for (j = 0; j < i; j++)
1605 dbus_free (retval[i]);
1612 #ifdef DBUS_BUILD_TESTS
1616 #define SERVICE_NAME_1 "MyService1"
1617 #define SERVICE_NAME_2 "MyService2"
1618 #define SERVICE_NAME_3 "MyService3"
1620 #define SERVICE_FILE_1 "service-1.service"
1621 #define SERVICE_FILE_2 "service-2.service"
1622 #define SERVICE_FILE_3 "service-3.service"
1625 test_create_service_file (DBusString *dir,
1626 const char *filename,
1630 DBusString file_name, full_path;
1632 dbus_bool_t ret_val;
1635 _dbus_string_init_const (&file_name, filename);
1637 if (!_dbus_string_init (&full_path))
1640 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1641 !_dbus_concat_dir_and_file (&full_path, &file_name))
1647 file = fopen (_dbus_string_get_const_data (&full_path), "w");
1654 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
1658 _dbus_string_free (&full_path);
1663 test_remove_service_file (DBusString *dir, const char *filename)
1665 DBusString file_name, full_path;
1666 dbus_bool_t ret_val;
1670 _dbus_string_init_const (&file_name, filename);
1672 if (!_dbus_string_init (&full_path))
1675 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1676 !_dbus_concat_dir_and_file (&full_path, &file_name))
1682 if (!_dbus_delete_file (&full_path, NULL))
1689 _dbus_string_free (&full_path);
1694 test_remove_directory (DBusString *dir)
1697 DBusString filename, full_path;
1698 dbus_bool_t ret_val;
1702 if (!_dbus_string_init (&filename))
1705 if (!_dbus_string_init (&full_path))
1707 _dbus_string_free (&filename);
1711 iter = _dbus_directory_open (dir, NULL);
1718 while (_dbus_directory_get_next_file (iter, &filename, NULL))
1720 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
1726 _dbus_directory_close (iter);
1728 if (!_dbus_delete_directory (dir, NULL))
1735 _dbus_string_free (&filename);
1736 _dbus_string_free (&full_path);
1742 init_service_reload_test (DBusString *dir)
1746 if (!_dbus_stat (dir, &stat_buf, NULL))
1748 if (!_dbus_create_directory (dir, NULL))
1753 if (!test_remove_directory (dir))
1756 if (!_dbus_create_directory (dir, NULL))
1760 /* Create one initial file */
1761 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
1768 cleanup_service_reload_test (DBusString *dir)
1770 if (!test_remove_directory (dir))
1778 BusActivation *activation;
1779 const char *service_name;
1780 dbus_bool_t expecting_find;
1784 check_func (void *data)
1787 BusActivationEntry *entry;
1789 dbus_bool_t ret_val;
1794 dbus_error_init (&error);
1796 entry = activation_find_entry (d->activation, d->service_name, &error);
1799 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1805 if (d->expecting_find)
1809 dbus_error_free (&error);
1813 if (!d->expecting_find)
1821 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
1826 err = !_dbus_test_oom_handling (description, check_func, data);
1828 err = !check_func (data);
1831 _dbus_assert_not_reached ("Test failed");
1837 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
1839 BusActivation *activation;
1841 DBusList *directories;
1845 _dbus_string_init_const (&address, "");
1847 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
1850 activation = bus_activation_new (NULL, &address, &directories, NULL);
1854 d.activation = activation;
1856 /* Check for existing service file */
1857 d.expecting_find = TRUE;
1858 d.service_name = SERVICE_NAME_1;
1860 if (!do_test ("Existing service file", oom_test, &d))
1863 /* Check for non-existing service file */
1864 d.expecting_find = FALSE;
1865 d.service_name = SERVICE_NAME_3;
1867 if (!do_test ("Nonexisting service file", oom_test, &d))
1870 /* Check for added service file */
1871 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
1874 d.expecting_find = TRUE;
1875 d.service_name = SERVICE_NAME_2;
1877 if (!do_test ("Added service file", oom_test, &d))
1880 /* Check for removed service file */
1881 if (!test_remove_service_file (dir, SERVICE_FILE_2))
1884 d.expecting_find = FALSE;
1885 d.service_name = SERVICE_FILE_2;
1887 if (!do_test ("Removed service file", oom_test, &d))
1890 /* Check for updated service file */
1892 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
1894 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
1897 d.expecting_find = TRUE;
1898 d.service_name = SERVICE_NAME_3;
1900 if (!do_test ("Updated service file, part 1", oom_test, &d))
1903 d.expecting_find = FALSE;
1904 d.service_name = SERVICE_NAME_1;
1906 if (!do_test ("Updated service file, part 2", oom_test, &d))
1909 bus_activation_unref (activation);
1910 _dbus_list_clear (&directories);
1916 bus_activation_service_reload_test (const DBusString *test_data_dir)
1918 DBusString directory;
1920 if (!_dbus_string_init (&directory))
1923 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
1926 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
1927 !_dbus_generate_random_ascii (&directory, 6))
1932 /* Do normal tests */
1933 if (!init_service_reload_test (&directory))
1934 _dbus_assert_not_reached ("could not initiate service reload test");
1936 if (!do_service_reload_test (&directory, FALSE))
1940 if (!init_service_reload_test (&directory))
1941 _dbus_assert_not_reached ("could not initiate service reload test");
1943 if (!do_service_reload_test (&directory, TRUE))
1946 /* Cleanup test directory */
1947 if (!cleanup_service_reload_test (&directory))
1950 _dbus_string_free (&directory);
1955 #endif /* DBUS_BUILD_TESTS */