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 "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>
41 #define DBUS_SERVICE_SECTION "D-BUS Service"
42 #define DBUS_SERVICE_NAME "Name"
43 #define DBUS_SERVICE_EXEC "Exec"
48 DBusHashTable *entries;
49 DBusHashTable *pending_activations;
52 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
53 * i.e. number of pending activation requests, not pending
56 DBusHashTable *directories;
63 DBusHashTable *entries;
64 } BusServiceDirectory;
72 BusServiceDirectory *s_dir;
76 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
78 struct BusPendingActivationEntry
80 DBusMessage *activation_message;
81 DBusConnection *connection;
83 dbus_bool_t auto_activation;
89 BusActivation *activation;
94 DBusBabysitter *babysitter;
96 unsigned int timeout_added : 1;
97 } BusPendingActivation;
100 static BusServiceDirectory *
101 bus_service_directory_ref (BusServiceDirectory *dir)
103 _dbus_assert (dir->refcount);
112 bus_service_directory_unref (BusServiceDirectory *dir)
117 _dbus_assert (dir->refcount > 0);
120 if (dir->refcount > 0)
124 _dbus_hash_table_unref (dir->entries);
126 dbus_free (dir->dir_c);
131 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
133 if (entry->activation_message)
134 dbus_message_unref (entry->activation_message);
136 if (entry->connection)
137 dbus_connection_unref (entry->connection);
143 handle_timeout_callback (DBusTimeout *timeout,
146 BusPendingActivation *pending_activation = data;
148 while (!dbus_timeout_handle (pending_activation->timeout))
149 _dbus_wait_for_memory ();
152 static BusPendingActivation *
153 bus_pending_activation_ref (BusPendingActivation *pending_activation)
155 _dbus_assert (pending_activation->refcount > 0);
156 pending_activation->refcount += 1;
158 return pending_activation;
162 bus_pending_activation_unref (BusPendingActivation *pending_activation)
166 if (pending_activation == NULL) /* hash table requires this */
169 _dbus_assert (pending_activation->refcount > 0);
170 pending_activation->refcount -= 1;
172 if (pending_activation->refcount > 0)
175 if (pending_activation->timeout_added)
177 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
178 pending_activation->timeout,
179 handle_timeout_callback, pending_activation);
180 pending_activation->timeout_added = FALSE;
183 if (pending_activation->timeout)
184 _dbus_timeout_unref (pending_activation->timeout);
186 if (pending_activation->babysitter)
188 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
190 pending_activation->babysitter,
192 _dbus_assert_not_reached ("setting watch functions to NULL failed");
194 _dbus_babysitter_unref (pending_activation->babysitter);
197 dbus_free (pending_activation->service_name);
198 dbus_free (pending_activation->exec);
200 link = _dbus_list_get_first_link (&pending_activation->entries);
204 BusPendingActivationEntry *entry = link->data;
206 bus_pending_activation_entry_free (entry);
208 link = _dbus_list_get_next_link (&pending_activation->entries, link);
210 _dbus_list_clear (&pending_activation->entries);
212 pending_activation->activation->n_pending_activations -=
213 pending_activation->n_entries;
215 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
217 dbus_free (pending_activation);
220 static BusActivationEntry *
221 bus_activation_entry_ref (BusActivationEntry *entry)
223 _dbus_assert (entry->refcount > 0);
230 bus_activation_entry_unref (BusActivationEntry *entry)
232 if (entry == NULL) /* hash table requires this */
235 _dbus_assert (entry->refcount > 0);
238 if (entry->refcount > 0)
241 dbus_free (entry->name);
242 dbus_free (entry->exec);
243 dbus_free (entry->filename);
249 update_desktop_file_entry (BusActivation *activation,
250 BusServiceDirectory *s_dir,
251 DBusString *filename,
252 BusDesktopFile *desktop_file,
256 BusActivationEntry *entry;
258 DBusString file_path;
260 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
266 if (!_dbus_string_init (&file_path))
272 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
273 !_dbus_concat_dir_and_file (&file_path, filename))
279 if (!_dbus_stat (&file_path, &stat_buf, NULL))
281 dbus_set_error (error, DBUS_ERROR_FAILED,
282 "Can't stat the service file\n");
286 if (!bus_desktop_file_get_string (desktop_file,
287 DBUS_SERVICE_SECTION,
293 if (!bus_desktop_file_get_string (desktop_file,
294 DBUS_SERVICE_SECTION,
300 entry = _dbus_hash_table_lookup_string (s_dir->entries,
301 _dbus_string_get_const_data (filename));
302 if (entry == NULL) /* New file */
304 /* FIXME we need a better-defined algorithm for which service file to
305 * pick than "whichever one is first in the directory listing"
307 if (_dbus_hash_table_lookup_string (activation->entries, name))
309 dbus_set_error (error, DBUS_ERROR_FAILED,
310 "Service %s already exists in activation entry list\n", name);
314 entry = dbus_new0 (BusActivationEntry, 1);
325 entry->s_dir = s_dir;
326 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
327 if (!entry->filename)
333 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
339 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
341 /* Revert the insertion in the entries table */
342 _dbus_hash_table_remove_string (activation->entries, entry->name);
347 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
349 else /* Just update the entry */
351 bus_activation_entry_ref (entry);
352 _dbus_hash_table_remove_string (activation->entries, entry->name);
354 if (_dbus_hash_table_lookup_string (activation->entries, name))
356 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
357 name, _dbus_string_get_const_data (&file_path));
361 dbus_free (entry->name);
362 dbus_free (entry->exec);
365 if (!_dbus_hash_table_insert_string (activation->entries,
366 entry->name, bus_activation_entry_ref(entry)))
369 /* Also remove path to entries hash since we want this in sync with
370 * the entries hash table */
371 _dbus_hash_table_remove_string (entry->s_dir->entries,
373 bus_activation_entry_unref (entry);
378 entry->mtime = stat_buf.mtime;
380 _dbus_string_free (&file_path);
381 bus_activation_entry_unref (entry);
388 _dbus_string_free (&file_path);
391 bus_activation_entry_unref (entry);
397 check_service_file (BusActivation *activation,
398 BusActivationEntry *entry,
399 BusActivationEntry **updated_entry,
404 BusActivationEntry *tmp_entry;
405 DBusString file_path;
411 _dbus_string_init_const (&filename, entry->filename);
413 if (!_dbus_string_init (&file_path))
419 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
420 !_dbus_concat_dir_and_file (&file_path, &filename))
427 if (!_dbus_stat (&file_path, &stat_buf, NULL))
429 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
430 _dbus_string_get_const_data (&file_path));
432 _dbus_hash_table_remove_string (activation->entries, entry->name);
433 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
441 if (stat_buf.mtime > entry->mtime)
443 BusDesktopFile *desktop_file;
446 dbus_error_init (&tmp_error);
448 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
449 if (desktop_file == NULL)
451 _dbus_verbose ("Could not load %s: %s\n",
452 _dbus_string_get_const_data (&file_path),
454 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
456 dbus_move_error (&tmp_error, error);
460 dbus_error_free (&tmp_error);
465 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
466 * Handle these both better
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 /* @todo We can return OOM or a DBUS_ERROR_FAILED error
595 * Handle these both better
597 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
599 bus_desktop_file_free (desktop_file);
602 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
603 _dbus_string_get_const_data (&full_path), tmp_error.message);
605 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
607 dbus_move_error (&tmp_error, error);
611 dbus_error_free (&tmp_error);
616 bus_desktop_file_free (desktop_file);
622 if (dbus_error_is_set (&tmp_error))
624 dbus_move_error (&tmp_error, error);
632 _DBUS_ASSERT_ERROR_IS_SET (error);
634 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
637 _dbus_directory_close (iter);
638 _dbus_string_free (&filename);
639 _dbus_string_free (&full_path);
645 bus_activation_new (BusContext *context,
646 const DBusString *address,
647 DBusList **directories,
650 BusActivation *activation;
654 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
656 activation = dbus_new0 (BusActivation, 1);
657 if (activation == NULL)
663 activation->refcount = 1;
664 activation->context = context;
665 activation->n_pending_activations = 0;
667 if (!_dbus_string_copy_data (address, &activation->server_address))
673 activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
674 (DBusFreeFunction)bus_activation_entry_unref);
675 if (activation->entries == NULL)
681 activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
682 (DBusFreeFunction)bus_pending_activation_unref);
684 if (activation->pending_activations == NULL)
690 activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
691 (DBusFreeFunction)bus_service_directory_unref);
693 if (activation->directories == NULL)
699 /* Load service files */
700 link = _dbus_list_get_first_link (directories);
703 BusServiceDirectory *s_dir;
705 dir = _dbus_strdup ((const char *) link->data);
712 s_dir = dbus_new0 (BusServiceDirectory, 1);
723 s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
724 (DBusFreeFunction)bus_activation_entry_unref);
728 bus_service_directory_unref (s_dir);
733 if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
735 bus_service_directory_unref (s_dir);
740 /* only fail on OOM, it is ok if we can't read the directory */
741 if (!update_directory (activation, s_dir, error))
743 if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
746 dbus_error_free (error);
749 link = _dbus_list_get_next_link (directories, link);
755 bus_activation_unref (activation);
760 bus_activation_ref (BusActivation *activation)
762 _dbus_assert (activation->refcount > 0);
764 activation->refcount += 1;
770 bus_activation_unref (BusActivation *activation)
772 _dbus_assert (activation->refcount > 0);
774 activation->refcount -= 1;
776 if (activation->refcount > 0)
779 dbus_free (activation->server_address);
780 if (activation->entries)
781 _dbus_hash_table_unref (activation->entries);
782 if (activation->pending_activations)
783 _dbus_hash_table_unref (activation->pending_activations);
784 if (activation->directories)
785 _dbus_hash_table_unref (activation->directories);
787 dbus_free (activation);
791 child_setup (void *data)
793 BusActivation *activation = data;
796 /* If no memory, we simply have the child exit, so it won't try
797 * to connect to the wrong thing.
799 if (!_dbus_setenv ("DBUS_STARTER_ADDRESS", activation->server_address))
802 type = bus_context_get_type (activation->context);
805 if (!_dbus_setenv ("DBUS_STARTER_BUS_TYPE", type))
808 if (strcmp (type, "session") == 0)
810 if (!_dbus_setenv ("DBUS_SESSION_BUS_ADDRESS",
811 activation->server_address))
814 else if (strcmp (type, "system") == 0)
816 if (!_dbus_setenv ("DBUS_SYSTEM_BUS_ADDRESS",
817 activation->server_address))
825 BusPendingActivation *pending_activation;
826 DBusPreallocatedHash *hash_entry;
827 } RestorePendingData;
830 restore_pending (void *data)
832 RestorePendingData *d = data;
834 _dbus_assert (d->pending_activation != NULL);
835 _dbus_assert (d->hash_entry != NULL);
837 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
838 d->pending_activation->service_name,
839 d->pending_activation->timeout_added);
841 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
843 d->pending_activation->service_name, d->pending_activation);
845 bus_pending_activation_ref (d->pending_activation);
847 d->hash_entry = NULL;
851 free_pending_restore_data (void *data)
853 RestorePendingData *d = data;
856 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
859 bus_pending_activation_unref (d->pending_activation);
865 add_restore_pending_to_transaction (BusTransaction *transaction,
866 BusPendingActivation *pending_activation)
868 RestorePendingData *d;
870 d = dbus_new (RestorePendingData, 1);
874 d->pending_activation = pending_activation;
875 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
877 bus_pending_activation_ref (d->pending_activation);
879 if (d->hash_entry == NULL ||
880 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
881 free_pending_restore_data))
883 free_pending_restore_data (d);
887 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
893 bus_activation_service_created (BusActivation *activation,
894 const char *service_name,
895 BusTransaction *transaction,
898 BusPendingActivation *pending_activation;
899 DBusMessage *message;
902 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
904 /* Check if it's a pending activation */
905 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
907 if (!pending_activation)
910 link = _dbus_list_get_first_link (&pending_activation->entries);
913 BusPendingActivationEntry *entry = link->data;
914 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
916 if (dbus_connection_get_is_connected (entry->connection))
918 /* Only send activation replies to regular activation requests. */
919 if (!entry->auto_activation)
921 dbus_uint32_t result;
923 message = dbus_message_new_method_return (entry->activation_message);
930 result = DBUS_START_REPLY_SUCCESS;
932 if (!dbus_message_append_args (message,
933 DBUS_TYPE_UINT32, &result,
936 dbus_message_unref (message);
941 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
943 dbus_message_unref (message);
948 dbus_message_unref (message);
962 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
964 BusTransaction *transaction,
967 BusPendingActivation *pending_activation;
970 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
972 /* Check if it's a pending activation */
973 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
974 bus_service_get_name (service));
976 if (!pending_activation)
979 link = _dbus_list_get_first_link (&pending_activation->entries);
982 BusPendingActivationEntry *entry = link->data;
983 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
985 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
987 DBusConnection *addressed_recipient;
989 addressed_recipient = bus_service_get_primary_owners_connection (service);
991 /* Check the security policy, which has the side-effect of adding an
992 * expected pending reply.
994 if (!bus_context_check_security_policy (activation->context, transaction,
998 entry->activation_message, error))
1001 if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
1003 BUS_SET_OOM (error);
1011 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1013 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1014 BUS_SET_OOM (error);
1018 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1027 * FIXME @todo the error messages here would ideally be preallocated
1028 * so we don't need to allocate memory to send them.
1029 * Using the usual tactic, prealloc an OOM message, then
1030 * if we can't alloc the real error send the OOM error instead.
1033 try_send_activation_failure (BusPendingActivation *pending_activation,
1034 const DBusError *how)
1036 BusActivation *activation;
1038 BusTransaction *transaction;
1040 activation = pending_activation->activation;
1042 transaction = bus_transaction_new (activation->context);
1043 if (transaction == NULL)
1046 link = _dbus_list_get_first_link (&pending_activation->entries);
1047 while (link != NULL)
1049 BusPendingActivationEntry *entry = link->data;
1050 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1052 if (dbus_connection_get_is_connected (entry->connection))
1054 if (!bus_transaction_send_error_reply (transaction,
1057 entry->activation_message))
1064 bus_transaction_execute_and_free (transaction);
1070 bus_transaction_cancel_and_free (transaction);
1075 * Free the pending activation and send an error message to all the
1076 * connections that were waiting for it.
1079 pending_activation_failed (BusPendingActivation *pending_activation,
1080 const DBusError *how)
1082 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1083 while (!try_send_activation_failure (pending_activation, how))
1084 _dbus_wait_for_memory ();
1086 /* Destroy this pending activation */
1087 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1088 pending_activation->service_name);
1092 babysitter_watch_callback (DBusWatch *watch,
1093 unsigned int condition,
1096 BusPendingActivation *pending_activation = data;
1098 DBusBabysitter *babysitter;
1100 babysitter = pending_activation->babysitter;
1102 _dbus_babysitter_ref (babysitter);
1104 retval = dbus_watch_handle (watch, condition);
1106 /* FIXME this is broken in the same way that
1107 * connection watches used to be; there should be
1108 * a separate callback for status change, instead
1109 * of doing "if we handled a watch status might
1112 * Fixing this lets us move dbus_watch_handle
1113 * calls into dbus-mainloop.c
1116 if (_dbus_babysitter_get_child_exited (babysitter))
1121 dbus_error_init (&error);
1122 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1124 /* Destroy all pending activations with the same exec */
1125 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1127 while (_dbus_hash_iter_next (&iter))
1129 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1131 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1132 pending_activation_failed (p, &error);
1135 /* Destroys the pending activation */
1136 pending_activation_failed (pending_activation, &error);
1138 dbus_error_free (&error);
1141 _dbus_babysitter_unref (babysitter);
1147 add_babysitter_watch (DBusWatch *watch,
1150 BusPendingActivation *pending_activation = data;
1152 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1153 watch, babysitter_watch_callback, pending_activation,
1158 remove_babysitter_watch (DBusWatch *watch,
1161 BusPendingActivation *pending_activation = data;
1163 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1164 watch, babysitter_watch_callback, pending_activation);
1168 pending_activation_timed_out (void *data)
1170 BusPendingActivation *pending_activation = data;
1173 /* Kill the spawned process, since it sucks
1174 * (not sure this is what we want to do, but
1175 * may as well try it for now)
1177 if (pending_activation->babysitter)
1178 _dbus_babysitter_kill_child (pending_activation->babysitter);
1180 dbus_error_init (&error);
1182 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1183 "Activation of %s timed out",
1184 pending_activation->service_name);
1186 pending_activation_failed (pending_activation, &error);
1188 dbus_error_free (&error);
1194 cancel_pending (void *data)
1196 BusPendingActivation *pending_activation = data;
1198 _dbus_verbose ("Canceling pending activation of %s\n",
1199 pending_activation->service_name);
1201 if (pending_activation->babysitter)
1202 _dbus_babysitter_kill_child (pending_activation->babysitter);
1204 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1205 pending_activation->service_name);
1209 free_pending_cancel_data (void *data)
1211 BusPendingActivation *pending_activation = data;
1213 bus_pending_activation_unref (pending_activation);
1217 add_cancel_pending_to_transaction (BusTransaction *transaction,
1218 BusPendingActivation *pending_activation)
1220 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1222 free_pending_cancel_data))
1225 bus_pending_activation_ref (pending_activation);
1227 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1233 update_service_cache (BusActivation *activation, DBusError *error)
1237 _dbus_hash_iter_init (activation->directories, &iter);
1238 while (_dbus_hash_iter_next (&iter))
1240 DBusError tmp_error;
1241 BusServiceDirectory *s_dir;
1243 s_dir = _dbus_hash_iter_get_value (&iter);
1245 dbus_error_init (&tmp_error);
1246 if (!update_directory (activation, s_dir, &tmp_error))
1248 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1250 dbus_move_error (&tmp_error, error);
1254 dbus_error_free (&tmp_error);
1262 static BusActivationEntry *
1263 activation_find_entry (BusActivation *activation,
1264 const char *service_name,
1267 BusActivationEntry *entry;
1269 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1272 if (!update_service_cache (activation, error))
1275 entry = _dbus_hash_table_lookup_string (activation->entries,
1280 BusActivationEntry *updated_entry;
1282 if (!check_service_file (activation, entry, &updated_entry, error))
1285 entry = updated_entry;
1290 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1291 "The name %s was not provided by any .service files",
1300 bus_activation_activate_service (BusActivation *activation,
1301 DBusConnection *connection,
1302 BusTransaction *transaction,
1303 dbus_bool_t auto_activation,
1304 DBusMessage *activation_message,
1305 const char *service_name,
1308 BusActivationEntry *entry;
1309 BusPendingActivation *pending_activation;
1310 BusPendingActivationEntry *pending_activation_entry;
1311 DBusMessage *message;
1312 DBusString service_str;
1318 dbus_bool_t activated;
1322 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1324 if (activation->n_pending_activations >=
1325 bus_context_get_max_pending_activations (activation->context))
1327 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1328 "The maximum number of pending activations has been reached, activation of %s failed",
1333 entry = activation_find_entry (activation, service_name, error);
1337 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1338 * call us if the service is already active.
1340 if (!auto_activation)
1342 /* Check if the service is active */
1343 _dbus_string_init_const (&service_str, service_name);
1344 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1346 dbus_uint32_t result;
1348 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1350 message = dbus_message_new_method_return (activation_message);
1354 _dbus_verbose ("No memory to create reply to activate message\n");
1355 BUS_SET_OOM (error);
1359 result = DBUS_START_REPLY_ALREADY_RUNNING;
1361 if (!dbus_message_append_args (message,
1362 DBUS_TYPE_UINT32, &result,
1365 _dbus_verbose ("No memory to set args of reply to activate message\n");
1366 BUS_SET_OOM (error);
1367 dbus_message_unref (message);
1371 retval = bus_transaction_send_from_driver (transaction, connection, message);
1372 dbus_message_unref (message);
1375 _dbus_verbose ("Failed to send reply\n");
1376 BUS_SET_OOM (error);
1383 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1384 if (!pending_activation_entry)
1386 _dbus_verbose ("Failed to create pending activation entry\n");
1387 BUS_SET_OOM (error);
1391 pending_activation_entry->auto_activation = auto_activation;
1393 pending_activation_entry->activation_message = activation_message;
1394 dbus_message_ref (activation_message);
1395 pending_activation_entry->connection = connection;
1396 dbus_connection_ref (connection);
1398 /* Check if the service is being activated */
1399 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1400 if (pending_activation)
1402 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1404 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1406 BUS_SET_OOM (error);
1407 bus_pending_activation_entry_free (pending_activation_entry);
1411 pending_activation->n_entries += 1;
1412 pending_activation->activation->n_pending_activations += 1;
1416 pending_activation = dbus_new0 (BusPendingActivation, 1);
1417 if (!pending_activation)
1419 _dbus_verbose ("Failed to create pending activation\n");
1421 BUS_SET_OOM (error);
1422 bus_pending_activation_entry_free (pending_activation_entry);
1426 pending_activation->activation = activation;
1427 pending_activation->refcount = 1;
1429 pending_activation->service_name = _dbus_strdup (service_name);
1430 if (!pending_activation->service_name)
1432 _dbus_verbose ("Failed to copy service name for pending activation\n");
1434 BUS_SET_OOM (error);
1435 bus_pending_activation_unref (pending_activation);
1436 bus_pending_activation_entry_free (pending_activation_entry);
1440 pending_activation->exec = _dbus_strdup (entry->exec);
1441 if (!pending_activation->exec)
1443 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1444 BUS_SET_OOM (error);
1445 bus_pending_activation_unref (pending_activation);
1446 bus_pending_activation_entry_free (pending_activation_entry);
1450 pending_activation->timeout =
1451 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1452 pending_activation_timed_out,
1455 if (!pending_activation->timeout)
1457 _dbus_verbose ("Failed to create timeout for pending activation\n");
1459 BUS_SET_OOM (error);
1460 bus_pending_activation_unref (pending_activation);
1461 bus_pending_activation_entry_free (pending_activation_entry);
1465 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1466 pending_activation->timeout,
1467 handle_timeout_callback,
1471 _dbus_verbose ("Failed to add timeout for pending activation\n");
1473 BUS_SET_OOM (error);
1474 bus_pending_activation_unref (pending_activation);
1475 bus_pending_activation_entry_free (pending_activation_entry);
1479 pending_activation->timeout_added = TRUE;
1481 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1483 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1485 BUS_SET_OOM (error);
1486 bus_pending_activation_unref (pending_activation);
1487 bus_pending_activation_entry_free (pending_activation_entry);
1491 pending_activation->n_entries += 1;
1492 pending_activation->activation->n_pending_activations += 1;
1495 _dbus_hash_iter_init (activation->pending_activations, &iter);
1496 while (_dbus_hash_iter_next (&iter))
1498 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1500 if (strcmp (p->exec, entry->exec) == 0)
1507 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1508 pending_activation->service_name,
1509 pending_activation))
1511 _dbus_verbose ("Failed to put pending activation in hash table\n");
1513 BUS_SET_OOM (error);
1514 bus_pending_activation_unref (pending_activation);
1519 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1521 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1522 BUS_SET_OOM (error);
1523 _dbus_hash_table_remove_string (activation->pending_activations,
1524 pending_activation->service_name);
1532 /* Now try to spawn the process */
1533 if (!_dbus_shell_parse_argv (entry->exec, &argc, &argv, error))
1535 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
1536 _DBUS_ASSERT_ERROR_IS_SET (error);
1538 _dbus_hash_table_remove_string (activation->pending_activations,
1539 pending_activation->service_name);
1544 _dbus_verbose ("Spawning %s ...\n", argv[0]);
1545 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1547 child_setup, activation,
1550 _dbus_verbose ("Failed to spawn child\n");
1551 _DBUS_ASSERT_ERROR_IS_SET (error);
1552 dbus_free_string_array (argv);
1557 dbus_free_string_array (argv);
1559 _dbus_assert (pending_activation->babysitter != NULL);
1561 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1562 add_babysitter_watch,
1563 remove_babysitter_watch,
1568 BUS_SET_OOM (error);
1569 _dbus_verbose ("Failed to set babysitter watch functions\n");
1577 bus_activation_list_services (BusActivation *activation,
1585 len = _dbus_hash_table_get_n_entries (activation->entries);
1586 retval = dbus_new (char *, len + 1);
1591 _dbus_hash_iter_init (activation->entries, &iter);
1593 while (_dbus_hash_iter_next (&iter))
1595 BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
1597 retval[i] = _dbus_strdup (entry->name);
1598 if (retval[i] == NULL)
1613 for (j = 0; j < i; j++)
1614 dbus_free (retval[i]);
1621 #ifdef DBUS_BUILD_TESTS
1625 #define SERVICE_NAME_1 "MyService1"
1626 #define SERVICE_NAME_2 "MyService2"
1627 #define SERVICE_NAME_3 "MyService3"
1629 #define SERVICE_FILE_1 "service-1.service"
1630 #define SERVICE_FILE_2 "service-2.service"
1631 #define SERVICE_FILE_3 "service-3.service"
1634 test_create_service_file (DBusString *dir,
1635 const char *filename,
1639 DBusString file_name, full_path;
1641 dbus_bool_t ret_val;
1644 _dbus_string_init_const (&file_name, filename);
1646 if (!_dbus_string_init (&full_path))
1649 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1650 !_dbus_concat_dir_and_file (&full_path, &file_name))
1656 file = fopen (_dbus_string_get_const_data (&full_path), "w");
1663 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
1667 _dbus_string_free (&full_path);
1672 test_remove_service_file (DBusString *dir, const char *filename)
1674 DBusString file_name, full_path;
1675 dbus_bool_t ret_val;
1679 _dbus_string_init_const (&file_name, filename);
1681 if (!_dbus_string_init (&full_path))
1684 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1685 !_dbus_concat_dir_and_file (&full_path, &file_name))
1691 if (!_dbus_delete_file (&full_path, NULL))
1698 _dbus_string_free (&full_path);
1703 test_remove_directory (DBusString *dir)
1706 DBusString filename, full_path;
1707 dbus_bool_t ret_val;
1711 if (!_dbus_string_init (&filename))
1714 if (!_dbus_string_init (&full_path))
1716 _dbus_string_free (&filename);
1720 iter = _dbus_directory_open (dir, NULL);
1727 while (_dbus_directory_get_next_file (iter, &filename, NULL))
1729 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
1735 _dbus_directory_close (iter);
1737 if (!_dbus_delete_directory (dir, NULL))
1744 _dbus_string_free (&filename);
1745 _dbus_string_free (&full_path);
1751 init_service_reload_test (DBusString *dir)
1755 if (!_dbus_stat (dir, &stat_buf, NULL))
1757 if (!_dbus_create_directory (dir, NULL))
1762 if (!test_remove_directory (dir))
1765 if (!_dbus_create_directory (dir, NULL))
1769 /* Create one initial file */
1770 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
1777 cleanup_service_reload_test (DBusString *dir)
1779 if (!test_remove_directory (dir))
1787 BusActivation *activation;
1788 const char *service_name;
1789 dbus_bool_t expecting_find;
1793 check_func (void *data)
1796 BusActivationEntry *entry;
1798 dbus_bool_t ret_val;
1803 dbus_error_init (&error);
1805 entry = activation_find_entry (d->activation, d->service_name, &error);
1808 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1814 if (d->expecting_find)
1818 dbus_error_free (&error);
1822 if (!d->expecting_find)
1830 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
1835 err = !_dbus_test_oom_handling (description, check_func, data);
1837 err = !check_func (data);
1840 _dbus_assert_not_reached ("Test failed");
1846 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
1848 BusActivation *activation;
1850 DBusList *directories;
1854 _dbus_string_init_const (&address, "");
1856 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
1859 activation = bus_activation_new (NULL, &address, &directories, NULL);
1863 d.activation = activation;
1865 /* Check for existing service file */
1866 d.expecting_find = TRUE;
1867 d.service_name = SERVICE_NAME_1;
1869 if (!do_test ("Existing service file", oom_test, &d))
1872 /* Check for non-existing service file */
1873 d.expecting_find = FALSE;
1874 d.service_name = SERVICE_NAME_3;
1876 if (!do_test ("Nonexisting service file", oom_test, &d))
1879 /* Check for added service file */
1880 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
1883 d.expecting_find = TRUE;
1884 d.service_name = SERVICE_NAME_2;
1886 if (!do_test ("Added service file", oom_test, &d))
1889 /* Check for removed service file */
1890 if (!test_remove_service_file (dir, SERVICE_FILE_2))
1893 d.expecting_find = FALSE;
1894 d.service_name = SERVICE_FILE_2;
1896 if (!do_test ("Removed service file", oom_test, &d))
1899 /* Check for updated service file */
1901 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
1903 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
1906 d.expecting_find = TRUE;
1907 d.service_name = SERVICE_NAME_3;
1909 if (!do_test ("Updated service file, part 1", oom_test, &d))
1912 d.expecting_find = FALSE;
1913 d.service_name = SERVICE_NAME_1;
1915 if (!do_test ("Updated service file, part 2", oom_test, &d))
1918 bus_activation_unref (activation);
1919 _dbus_list_clear (&directories);
1925 bus_activation_service_reload_test (const DBusString *test_data_dir)
1927 DBusString directory;
1929 if (!_dbus_string_init (&directory))
1932 if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
1935 if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
1936 !_dbus_generate_random_ascii (&directory, 6))
1941 /* Do normal tests */
1942 if (!init_service_reload_test (&directory))
1943 _dbus_assert_not_reached ("could not initiate service reload test");
1945 if (!do_service_reload_test (&directory, FALSE))
1949 if (!init_service_reload_test (&directory))
1950 _dbus_assert_not_reached ("could not initiate service reload test");
1952 if (!do_service_reload_test (&directory, TRUE))
1955 /* Cleanup test directory */
1956 if (!cleanup_service_reload_test (&directory))
1959 _dbus_string_free (&directory);
1964 #endif /* DBUS_BUILD_TESTS */