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-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;
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,
288 dbus_set_error (error, DBUS_ERROR_FAILED,
289 "No \""DBUS_SERVICE_NAME"\" key in .service file\n");
293 if (!bus_desktop_file_get_string (desktop_file,
294 DBUS_SERVICE_SECTION,
298 dbus_set_error (error, DBUS_ERROR_FAILED,
299 "No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
303 entry = _dbus_hash_table_lookup_string (s_dir->entries,
304 _dbus_string_get_const_data (filename));
305 if (entry == NULL) /* New file */
307 /* FIXME we need a better-defined algorithm for which service file to
308 * pick than "whichever one is first in the directory listing"
310 if (_dbus_hash_table_lookup_string (activation->entries, name))
312 dbus_set_error (error, DBUS_ERROR_FAILED,
313 "Service %s already exists in activation entry list\n", name);
317 entry = dbus_new0 (BusActivationEntry, 1);
328 entry->s_dir = s_dir;
329 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
330 if (!entry->filename)
336 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
342 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
344 /* Revert the insertion in the entries table */
345 _dbus_hash_table_remove_string (activation->entries, entry->name);
350 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
352 else /* Just update the entry */
354 bus_activation_entry_ref (entry);
355 _dbus_hash_table_remove_string (activation->entries, entry->name);
357 if (_dbus_hash_table_lookup_string (activation->entries, name))
359 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
360 name, _dbus_string_get_const_data (&file_path));
364 dbus_free (entry->name);
365 dbus_free (entry->exec);
368 if (!_dbus_hash_table_insert_string (activation->entries,
369 entry->name, bus_activation_entry_ref(entry)))
372 /* Also remove path to entries hash since we want this in sync with
373 * the entries hash table */
374 _dbus_hash_table_remove_string (entry->s_dir->entries,
376 bus_activation_entry_unref (entry);
381 entry->mtime = stat_buf.mtime;
383 _dbus_string_free (&file_path);
384 bus_activation_entry_unref (entry);
391 _dbus_string_free (&file_path);
394 bus_activation_entry_unref (entry);
400 check_service_file (BusActivation *activation,
401 BusActivationEntry *entry,
402 BusActivationEntry **updated_entry,
407 BusActivationEntry *tmp_entry;
408 DBusString file_path;
414 _dbus_string_init_const (&filename, entry->filename);
416 if (!_dbus_string_init (&file_path))
422 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
423 !_dbus_concat_dir_and_file (&file_path, &filename))
430 if (!_dbus_stat (&file_path, &stat_buf, NULL))
432 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
433 _dbus_string_get_const_data (&file_path));
435 _dbus_hash_table_remove_string (activation->entries, entry->name);
436 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
444 if (stat_buf.mtime > entry->mtime)
446 BusDesktopFile *desktop_file;
449 dbus_error_init (&tmp_error);
451 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
452 if (desktop_file == NULL)
454 _dbus_verbose ("Could not load %s: %s\n",
455 _dbus_string_get_const_data (&file_path),
457 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
459 dbus_move_error (&tmp_error, error);
463 dbus_error_free (&tmp_error);
468 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
470 bus_desktop_file_free (desktop_file);
471 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
473 dbus_move_error (&tmp_error, error);
477 dbus_error_free (&tmp_error);
482 bus_desktop_file_free (desktop_file);
488 _dbus_string_free (&file_path);
490 if (updated_entry != NULL)
491 *updated_entry = tmp_entry;
496 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
497 * hash entries it already added.
500 update_directory (BusActivation *activation,
501 BusServiceDirectory *s_dir,
505 DBusString dir, filename;
506 BusDesktopFile *desktop_file;
509 BusActivationEntry *entry;
510 DBusString full_path;
512 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
517 _dbus_string_init_const (&dir, s_dir->dir_c);
519 if (!_dbus_string_init (&filename))
525 if (!_dbus_string_init (&full_path))
528 _dbus_string_free (&filename);
534 /* from this point it's safe to "goto out" */
536 iter = _dbus_directory_open (&dir, error);
539 _dbus_verbose ("Failed to open directory %s: %s\n",
541 error ? error->message : "unknown");
545 /* Now read the files */
546 dbus_error_init (&tmp_error);
547 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
549 _dbus_assert (!dbus_error_is_set (&tmp_error));
551 _dbus_string_set_length (&full_path, 0);
553 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
555 _dbus_verbose ("Skipping non-.service file %s\n",
556 _dbus_string_get_const_data (&filename));
560 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
561 if (entry) /* Already has this service file in the cache */
563 if (!check_service_file (activation, entry, NULL, error))
569 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
570 !_dbus_concat_dir_and_file (&full_path, &filename))
577 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
578 if (desktop_file == NULL)
580 _dbus_verbose ("Could not load %s: %s\n",
581 _dbus_string_get_const_data (&full_path),
584 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
586 dbus_move_error (&tmp_error, error);
590 dbus_error_free (&tmp_error);
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);
636 bus_desktop_file_free (desktop_file);
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_ACTIVATION_ADDRESS", activation->server_address))
795 type = bus_context_get_type (activation->context);
798 if (!_dbus_setenv ("DBUS_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_ACTIVATION_REPLY_ACTIVATED;
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_owner (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 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_ACTIVATE_SERVICE_NOT_FOUND,
1284 "The service %s was not found in the activation entry list",
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;
1309 dbus_bool_t activated;
1313 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1315 if (activation->n_pending_activations >=
1316 bus_context_get_max_pending_activations (activation->context))
1318 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1319 "The maximum number of pending activations has been reached, activation of %s failed",
1324 entry = activation_find_entry (activation, service_name, error);
1328 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1329 * call us if the service is already active.
1331 if (!auto_activation)
1333 /* Check if the service is active */
1334 _dbus_string_init_const (&service_str, service_name);
1335 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1337 dbus_uint32_t result;
1339 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1341 message = dbus_message_new_method_return (activation_message);
1345 _dbus_verbose ("No memory to create reply to activate message\n");
1346 BUS_SET_OOM (error);
1350 result = DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE;
1352 if (!dbus_message_append_args (message,
1353 DBUS_TYPE_UINT32, &result,
1356 _dbus_verbose ("No memory to set args of reply to activate message\n");
1357 BUS_SET_OOM (error);
1358 dbus_message_unref (message);
1362 retval = bus_transaction_send_from_driver (transaction, connection, message);
1363 dbus_message_unref (message);
1366 _dbus_verbose ("Failed to send reply\n");
1367 BUS_SET_OOM (error);
1374 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1375 if (!pending_activation_entry)
1377 _dbus_verbose ("Failed to create pending activation entry\n");
1378 BUS_SET_OOM (error);
1382 pending_activation_entry->auto_activation = auto_activation;
1384 pending_activation_entry->activation_message = activation_message;
1385 dbus_message_ref (activation_message);
1386 pending_activation_entry->connection = connection;
1387 dbus_connection_ref (connection);
1389 /* Check if the service is being activated */
1390 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1391 if (pending_activation)
1393 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1395 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1397 BUS_SET_OOM (error);
1398 bus_pending_activation_entry_free (pending_activation_entry);
1402 pending_activation->n_entries += 1;
1403 pending_activation->activation->n_pending_activations += 1;
1407 pending_activation = dbus_new0 (BusPendingActivation, 1);
1408 if (!pending_activation)
1410 _dbus_verbose ("Failed to create pending activation\n");
1412 BUS_SET_OOM (error);
1413 bus_pending_activation_entry_free (pending_activation_entry);
1417 pending_activation->activation = activation;
1418 pending_activation->refcount = 1;
1420 pending_activation->service_name = _dbus_strdup (service_name);
1421 if (!pending_activation->service_name)
1423 _dbus_verbose ("Failed to copy service name for pending activation\n");
1425 BUS_SET_OOM (error);
1426 bus_pending_activation_unref (pending_activation);
1427 bus_pending_activation_entry_free (pending_activation_entry);
1431 pending_activation->exec = _dbus_strdup (entry->exec);
1432 if (!pending_activation->exec)
1434 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1435 BUS_SET_OOM (error);
1436 bus_pending_activation_unref (pending_activation);
1437 bus_pending_activation_entry_free (pending_activation_entry);
1441 pending_activation->timeout =
1442 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1443 pending_activation_timed_out,
1446 if (!pending_activation->timeout)
1448 _dbus_verbose ("Failed to create timeout for pending activation\n");
1450 BUS_SET_OOM (error);
1451 bus_pending_activation_unref (pending_activation);
1452 bus_pending_activation_entry_free (pending_activation_entry);
1456 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1457 pending_activation->timeout,
1458 handle_timeout_callback,
1462 _dbus_verbose ("Failed to add timeout for pending activation\n");
1464 BUS_SET_OOM (error);
1465 bus_pending_activation_unref (pending_activation);
1466 bus_pending_activation_entry_free (pending_activation_entry);
1470 pending_activation->timeout_added = TRUE;
1472 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1474 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1476 BUS_SET_OOM (error);
1477 bus_pending_activation_unref (pending_activation);
1478 bus_pending_activation_entry_free (pending_activation_entry);
1482 pending_activation->n_entries += 1;
1483 pending_activation->activation->n_pending_activations += 1;
1486 _dbus_hash_iter_init (activation->pending_activations, &iter);
1487 while (_dbus_hash_iter_next (&iter))
1489 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1491 if (strcmp (p->exec, entry->exec) == 0)
1498 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1499 pending_activation->service_name,
1500 pending_activation))
1502 _dbus_verbose ("Failed to put pending activation in hash table\n");
1504 BUS_SET_OOM (error);
1505 bus_pending_activation_unref (pending_activation);
1510 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1512 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1513 BUS_SET_OOM (error);
1514 _dbus_hash_table_remove_string (activation->pending_activations,
1515 pending_activation->service_name);
1519 /* FIXME we need to support a full command line, not just a single
1526 /* Now try to spawn the process */
1527 argv[0] = entry->exec;
1530 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1531 child_setup, activation,
1534 _dbus_verbose ("Failed to spawn child\n");
1535 _DBUS_ASSERT_ERROR_IS_SET (error);
1539 _dbus_assert (pending_activation->babysitter != NULL);
1541 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1542 add_babysitter_watch,
1543 remove_babysitter_watch,
1548 BUS_SET_OOM (error);
1549 _dbus_verbose ("Failed to set babysitter watch functions\n");
1556 #ifdef DBUS_BUILD_TESTS
1560 #define SERVICE_NAME_1 "MyService1"
1561 #define SERVICE_NAME_2 "MyService2"
1562 #define SERVICE_NAME_3 "MyService3"
1564 #define SERVICE_FILE_1 "service-1.service"
1565 #define SERVICE_FILE_2 "service-2.service"
1566 #define SERVICE_FILE_3 "service-3.service"
1569 test_create_service_file (DBusString *dir,
1570 const char *filename,
1574 DBusString file_name, full_path;
1576 dbus_bool_t ret_val;
1579 _dbus_string_init_const (&file_name, filename);
1581 if (!_dbus_string_init (&full_path))
1584 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1585 !_dbus_concat_dir_and_file (&full_path, &file_name))
1591 file = fopen (_dbus_string_get_const_data (&full_path), "w");
1598 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
1602 _dbus_string_free (&full_path);
1607 test_remove_service_file (DBusString *dir, const char *filename)
1609 DBusString file_name, full_path;
1610 dbus_bool_t ret_val;
1614 _dbus_string_init_const (&file_name, filename);
1616 if (!_dbus_string_init (&full_path))
1619 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1620 !_dbus_concat_dir_and_file (&full_path, &file_name))
1626 if (!_dbus_delete_file (&full_path, NULL))
1633 _dbus_string_free (&full_path);
1638 test_remove_directory (DBusString *dir)
1641 DBusString filename, full_path;
1642 dbus_bool_t ret_val;
1646 if (!_dbus_string_init (&filename))
1649 if (!_dbus_string_init (&full_path))
1651 _dbus_string_free (&filename);
1655 iter = _dbus_directory_open (dir, NULL);
1662 while (_dbus_directory_get_next_file (iter, &filename, NULL))
1664 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
1670 _dbus_directory_close (iter);
1672 if (!_dbus_delete_directory (dir, NULL))
1679 _dbus_string_free (&filename);
1680 _dbus_string_free (&full_path);
1686 init_service_reload_test (DBusString *dir)
1690 if (!_dbus_stat (dir, &stat_buf, NULL))
1692 if (!_dbus_create_directory (dir, NULL))
1697 if (!test_remove_directory (dir))
1700 if (!_dbus_create_directory (dir, NULL))
1704 /* Create one initial file */
1705 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
1712 cleanup_service_reload_test (DBusString *dir)
1714 if (!test_remove_directory (dir))
1722 BusActivation *activation;
1723 const char *service_name;
1724 dbus_bool_t expecting_find;
1728 check_func (void *data)
1731 BusActivationEntry *entry;
1733 dbus_bool_t ret_val;
1738 dbus_error_init (&error);
1740 entry = activation_find_entry (d->activation, d->service_name, &error);
1743 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1749 if (d->expecting_find)
1753 dbus_error_free (&error);
1757 if (!d->expecting_find)
1765 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
1770 err = !_dbus_test_oom_handling (description, check_func, data);
1772 err = !check_func (data);
1775 _dbus_assert_not_reached ("Test failed");
1781 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
1783 BusActivation *activation;
1785 DBusList *directories;
1789 _dbus_string_init_const (&address, "");
1791 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
1794 activation = bus_activation_new (NULL, &address, &directories, NULL);
1798 d.activation = activation;
1800 /* Check for existing service file */
1801 d.expecting_find = TRUE;
1802 d.service_name = SERVICE_NAME_1;
1804 if (!do_test ("Existing service file", oom_test, &d))
1807 /* Check for non-existing service file */
1808 d.expecting_find = FALSE;
1809 d.service_name = SERVICE_NAME_3;
1811 if (!do_test ("Nonexisting service file", oom_test, &d))
1814 /* Check for added service file */
1815 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
1818 d.expecting_find = TRUE;
1819 d.service_name = SERVICE_NAME_2;
1821 if (!do_test ("Added service file", oom_test, &d))
1824 /* Check for removed service file */
1825 if (!test_remove_service_file (dir, SERVICE_FILE_2))
1828 d.expecting_find = FALSE;
1829 d.service_name = SERVICE_FILE_2;
1831 if (!do_test ("Removed service file", oom_test, &d))
1834 /* Check for updated service file */
1836 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
1838 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
1841 d.expecting_find = TRUE;
1842 d.service_name = SERVICE_NAME_3;
1844 if (!do_test ("Updated service file, part 1", oom_test, &d))
1847 d.expecting_find = FALSE;
1848 d.service_name = SERVICE_NAME_1;
1850 if (!do_test ("Updated service file, part 2", oom_test, &d))
1853 bus_activation_unref (activation);
1854 _dbus_list_clear (&directories);
1860 bus_activation_service_reload_test (const DBusString *test_data_dir)
1862 DBusString directory;
1864 if (!_dbus_string_init (&directory))
1867 if (!_dbus_string_append (&directory, "/tmp/dbus-reload-test-") ||
1868 !_dbus_generate_random_ascii (&directory, 6))
1873 /* Do normal tests */
1874 if (!init_service_reload_test (&directory))
1875 _dbus_assert_not_reached ("could not initiate service reload test");
1877 if (!do_service_reload_test (&directory, FALSE));
1880 if (!init_service_reload_test (&directory))
1881 _dbus_assert_not_reached ("could not initiate service reload test");
1883 if (!do_service_reload_test (&directory, TRUE));
1885 /* Cleanup test directory */
1886 if (!cleanup_service_reload_test (&directory))
1889 _dbus_string_free (&directory);
1894 #endif /* DBUS_BUILD_TESTS */