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>
39 #define DBUS_SERVICE_SECTION "D-BUS Service"
40 #define DBUS_SERVICE_NAME "Name"
41 #define DBUS_SERVICE_EXEC "Exec"
46 DBusHashTable *entries;
47 DBusHashTable *pending_activations;
50 int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
51 * i.e. number of pending activation requests, not pending
54 DBusHashTable *directories;
61 DBusHashTable *entries;
62 } BusServiceDirectory;
70 BusServiceDirectory *s_dir;
74 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
76 struct BusPendingActivationEntry
78 DBusMessage *activation_message;
79 DBusConnection *connection;
81 dbus_bool_t auto_activation;
87 BusActivation *activation;
92 DBusBabysitter *babysitter;
94 unsigned int timeout_added : 1;
95 } BusPendingActivation;
98 static BusServiceDirectory *
99 bus_service_directory_ref (BusServiceDirectory *dir)
101 _dbus_assert (dir->refcount);
110 bus_service_directory_unref (BusServiceDirectory *dir)
115 _dbus_assert (dir->refcount > 0);
118 if (dir->refcount > 0)
122 _dbus_hash_table_unref (dir->entries);
124 dbus_free (dir->dir_c);
129 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
131 if (entry->activation_message)
132 dbus_message_unref (entry->activation_message);
134 if (entry->connection)
135 dbus_connection_unref (entry->connection);
141 handle_timeout_callback (DBusTimeout *timeout,
144 BusPendingActivation *pending_activation = data;
146 while (!dbus_timeout_handle (pending_activation->timeout))
147 _dbus_wait_for_memory ();
150 static BusPendingActivation *
151 bus_pending_activation_ref (BusPendingActivation *pending_activation)
153 _dbus_assert (pending_activation->refcount > 0);
154 pending_activation->refcount += 1;
156 return pending_activation;
160 bus_pending_activation_unref (BusPendingActivation *pending_activation)
164 if (pending_activation == NULL) /* hash table requires this */
167 _dbus_assert (pending_activation->refcount > 0);
168 pending_activation->refcount -= 1;
170 if (pending_activation->refcount > 0)
173 if (pending_activation->timeout_added)
175 _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
176 pending_activation->timeout,
177 handle_timeout_callback, pending_activation);
178 pending_activation->timeout_added = FALSE;
181 if (pending_activation->timeout)
182 _dbus_timeout_unref (pending_activation->timeout);
184 if (pending_activation->babysitter)
186 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
188 pending_activation->babysitter,
190 _dbus_assert_not_reached ("setting watch functions to NULL failed");
192 _dbus_babysitter_unref (pending_activation->babysitter);
195 dbus_free (pending_activation->service_name);
196 dbus_free (pending_activation->exec);
198 link = _dbus_list_get_first_link (&pending_activation->entries);
202 BusPendingActivationEntry *entry = link->data;
204 bus_pending_activation_entry_free (entry);
206 link = _dbus_list_get_next_link (&pending_activation->entries, link);
208 _dbus_list_clear (&pending_activation->entries);
210 pending_activation->activation->n_pending_activations -=
211 pending_activation->n_entries;
213 _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
215 dbus_free (pending_activation);
218 static BusActivationEntry *
219 bus_activation_entry_ref (BusActivationEntry *entry)
221 _dbus_assert (entry->refcount > 0);
228 bus_activation_entry_unref (BusActivationEntry *entry)
230 if (entry == NULL) /* hash table requires this */
233 _dbus_assert (entry->refcount > 0);
236 if (entry->refcount > 0)
239 dbus_free (entry->name);
240 dbus_free (entry->exec);
241 dbus_free (entry->filename);
247 update_desktop_file_entry (BusActivation *activation,
248 BusServiceDirectory *s_dir,
249 DBusString *filename,
250 BusDesktopFile *desktop_file,
254 BusActivationEntry *entry;
256 DBusString file_path;
258 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
264 if (!_dbus_string_init (&file_path))
270 if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
271 !_dbus_concat_dir_and_file (&file_path, filename))
277 if (!_dbus_stat (&file_path, &stat_buf, NULL))
279 dbus_set_error (error, DBUS_ERROR_FAILED,
280 "Can't stat the service file\n");
284 if (!bus_desktop_file_get_string (desktop_file,
285 DBUS_SERVICE_SECTION,
289 dbus_set_error (error, DBUS_ERROR_FAILED,
290 "No \""DBUS_SERVICE_NAME"\" key in .service file\n");
294 if (!bus_desktop_file_get_string (desktop_file,
295 DBUS_SERVICE_SECTION,
299 dbus_set_error (error, DBUS_ERROR_FAILED,
300 "No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
304 entry = _dbus_hash_table_lookup_string (s_dir->entries,
305 _dbus_string_get_const_data (filename));
306 if (entry == NULL) /* New file */
308 /* FIXME we need a better-defined algorithm for which service file to
309 * pick than "whichever one is first in the directory listing"
311 if (_dbus_hash_table_lookup_string (activation->entries, name))
313 dbus_set_error (error, DBUS_ERROR_FAILED,
314 "Service %s already exists in activation entry list\n", name);
318 entry = dbus_new0 (BusActivationEntry, 1);
329 entry->s_dir = s_dir;
330 entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
331 if (!entry->filename)
337 if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
343 if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
345 /* Revert the insertion in the entries table */
346 _dbus_hash_table_remove_string (activation->entries, entry->name);
351 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
353 else /* Just update the entry */
355 bus_activation_entry_ref (entry);
356 _dbus_hash_table_remove_string (activation->entries, entry->name);
358 if (_dbus_hash_table_lookup_string (activation->entries, name))
360 _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
361 name, _dbus_string_get_const_data (&file_path));
365 dbus_free (entry->name);
366 dbus_free (entry->exec);
369 if (!_dbus_hash_table_insert_string (activation->entries,
370 entry->name, bus_activation_entry_ref(entry)))
373 /* Also remove path to entries hash since we want this in sync with
374 * the entries hash table */
375 _dbus_hash_table_remove_string (entry->s_dir->entries,
377 bus_activation_entry_unref (entry);
382 entry->mtime = stat_buf.mtime;
384 _dbus_string_free (&file_path);
385 bus_activation_entry_unref (entry);
392 _dbus_string_free (&file_path);
395 bus_activation_entry_unref (entry);
401 check_service_file (BusActivation *activation,
402 BusActivationEntry *entry,
403 BusActivationEntry **updated_entry,
408 BusActivationEntry *tmp_entry;
409 DBusString file_path;
415 _dbus_string_init_const (&filename, entry->filename);
417 if (!_dbus_string_init (&file_path))
423 if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
424 !_dbus_concat_dir_and_file (&file_path, &filename))
431 if (!_dbus_stat (&file_path, &stat_buf, NULL))
433 _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
434 _dbus_string_get_const_data (&file_path));
436 _dbus_hash_table_remove_string (activation->entries, entry->name);
437 _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
445 if (stat_buf.mtime > entry->mtime)
447 BusDesktopFile *desktop_file;
450 dbus_error_init (&tmp_error);
452 desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
453 if (desktop_file == NULL)
455 _dbus_verbose ("Could not load %s: %s\n",
456 _dbus_string_get_const_data (&file_path),
458 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
460 dbus_move_error (&tmp_error, error);
464 dbus_error_free (&tmp_error);
469 if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
471 bus_desktop_file_free (desktop_file);
472 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
474 dbus_move_error (&tmp_error, error);
478 dbus_error_free (&tmp_error);
483 bus_desktop_file_free (desktop_file);
489 _dbus_string_free (&file_path);
491 if (updated_entry != NULL)
492 *updated_entry = tmp_entry;
497 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
498 * hash entries it already added.
501 update_directory (BusActivation *activation,
502 BusServiceDirectory *s_dir,
506 DBusString dir, filename;
507 BusDesktopFile *desktop_file;
510 BusActivationEntry *entry;
511 DBusString full_path;
513 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
518 _dbus_string_init_const (&dir, s_dir->dir_c);
520 if (!_dbus_string_init (&filename))
526 if (!_dbus_string_init (&full_path))
529 _dbus_string_free (&filename);
535 /* from this point it's safe to "goto out" */
537 iter = _dbus_directory_open (&dir, error);
540 _dbus_verbose ("Failed to open directory %s: %s\n",
542 error ? error->message : "unknown");
546 /* Now read the files */
547 dbus_error_init (&tmp_error);
548 while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
550 _dbus_assert (!dbus_error_is_set (&tmp_error));
552 _dbus_string_set_length (&full_path, 0);
554 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
556 _dbus_verbose ("Skipping non-.service file %s\n",
557 _dbus_string_get_const_data (&filename));
561 entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
562 if (entry) /* Already has this service file in the cache */
564 if (!check_service_file (activation, entry, NULL, error))
570 if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
571 !_dbus_concat_dir_and_file (&full_path, &filename))
578 desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
579 if (desktop_file == NULL)
581 _dbus_verbose ("Could not load %s: %s\n",
582 _dbus_string_get_const_data (&full_path),
585 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
587 dbus_move_error (&tmp_error, error);
591 dbus_error_free (&tmp_error);
595 if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
597 bus_desktop_file_free (desktop_file);
600 _dbus_verbose ("Could not add %s to activation entry list: %s\n",
601 _dbus_string_get_const_data (&full_path), tmp_error.message);
603 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
605 dbus_move_error (&tmp_error, error);
609 dbus_error_free (&tmp_error);
614 bus_desktop_file_free (desktop_file);
620 if (dbus_error_is_set (&tmp_error))
622 dbus_move_error (&tmp_error, error);
630 _DBUS_ASSERT_ERROR_IS_SET (error);
632 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
635 _dbus_directory_close (iter);
637 bus_desktop_file_free (desktop_file);
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 if (!update_directory (activation, s_dir, error))
743 link = _dbus_list_get_next_link (directories, link);
749 bus_activation_unref (activation);
754 bus_activation_ref (BusActivation *activation)
756 _dbus_assert (activation->refcount > 0);
758 activation->refcount += 1;
764 bus_activation_unref (BusActivation *activation)
766 _dbus_assert (activation->refcount > 0);
768 activation->refcount -= 1;
770 if (activation->refcount > 0)
773 dbus_free (activation->server_address);
774 if (activation->entries)
775 _dbus_hash_table_unref (activation->entries);
776 if (activation->pending_activations)
777 _dbus_hash_table_unref (activation->pending_activations);
778 if (activation->directories)
779 _dbus_hash_table_unref (activation->directories);
781 dbus_free (activation);
785 child_setup (void *data)
787 BusActivation *activation = data;
790 /* If no memory, we simply have the child exit, so it won't try
791 * to connect to the wrong thing.
793 if (!_dbus_setenv ("DBUS_STARTER_ADDRESS", activation->server_address))
796 type = bus_context_get_type (activation->context);
799 if (!_dbus_setenv ("DBUS_STARTER_BUS_TYPE", type))
802 if (strcmp (type, "session") == 0)
804 if (!_dbus_setenv ("DBUS_SESSION_BUS_ADDRESS",
805 activation->server_address))
808 else if (strcmp (type, "system") == 0)
810 if (!_dbus_setenv ("DBUS_SYSTEM_BUS_ADDRESS",
811 activation->server_address))
819 BusPendingActivation *pending_activation;
820 DBusPreallocatedHash *hash_entry;
821 } RestorePendingData;
824 restore_pending (void *data)
826 RestorePendingData *d = data;
828 _dbus_assert (d->pending_activation != NULL);
829 _dbus_assert (d->hash_entry != NULL);
831 _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
832 d->pending_activation->service_name,
833 d->pending_activation->timeout_added);
835 _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
837 d->pending_activation->service_name, d->pending_activation);
839 bus_pending_activation_ref (d->pending_activation);
841 d->hash_entry = NULL;
845 free_pending_restore_data (void *data)
847 RestorePendingData *d = data;
850 _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
853 bus_pending_activation_unref (d->pending_activation);
859 add_restore_pending_to_transaction (BusTransaction *transaction,
860 BusPendingActivation *pending_activation)
862 RestorePendingData *d;
864 d = dbus_new (RestorePendingData, 1);
868 d->pending_activation = pending_activation;
869 d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
871 bus_pending_activation_ref (d->pending_activation);
873 if (d->hash_entry == NULL ||
874 !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
875 free_pending_restore_data))
877 free_pending_restore_data (d);
881 _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
887 bus_activation_service_created (BusActivation *activation,
888 const char *service_name,
889 BusTransaction *transaction,
892 BusPendingActivation *pending_activation;
893 DBusMessage *message;
896 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
898 /* Check if it's a pending activation */
899 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
901 if (!pending_activation)
904 link = _dbus_list_get_first_link (&pending_activation->entries);
907 BusPendingActivationEntry *entry = link->data;
908 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
910 if (dbus_connection_get_is_connected (entry->connection))
912 /* Only send activation replies to regular activation requests. */
913 if (!entry->auto_activation)
915 dbus_uint32_t result;
917 message = dbus_message_new_method_return (entry->activation_message);
924 result = DBUS_START_REPLY_SUCCESS;
926 if (!dbus_message_append_args (message,
927 DBUS_TYPE_UINT32, &result,
930 dbus_message_unref (message);
935 if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
937 dbus_message_unref (message);
942 dbus_message_unref (message);
956 bus_activation_send_pending_auto_activation_messages (BusActivation *activation,
958 BusTransaction *transaction,
961 BusPendingActivation *pending_activation;
964 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
966 /* Check if it's a pending activation */
967 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
968 bus_service_get_name (service));
970 if (!pending_activation)
973 link = _dbus_list_get_first_link (&pending_activation->entries);
976 BusPendingActivationEntry *entry = link->data;
977 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
979 if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
981 DBusConnection *addressed_recipient;
983 addressed_recipient = bus_service_get_primary_owner (service);
985 /* Check the security policy, which has the side-effect of adding an
986 * expected pending reply.
988 if (!bus_context_check_security_policy (activation->context, transaction,
992 entry->activation_message, error))
995 if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
1005 if (!add_restore_pending_to_transaction (transaction, pending_activation))
1007 _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1008 BUS_SET_OOM (error);
1012 _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1021 * FIXME @todo the error messages here would ideally be preallocated
1022 * so we don't need to allocate memory to send them.
1023 * Using the usual tactic, prealloc an OOM message, then
1024 * if we can't alloc the real error send the OOM error instead.
1027 try_send_activation_failure (BusPendingActivation *pending_activation,
1028 const DBusError *how)
1030 BusActivation *activation;
1032 BusTransaction *transaction;
1034 activation = pending_activation->activation;
1036 transaction = bus_transaction_new (activation->context);
1037 if (transaction == NULL)
1040 link = _dbus_list_get_first_link (&pending_activation->entries);
1041 while (link != NULL)
1043 BusPendingActivationEntry *entry = link->data;
1044 DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1046 if (dbus_connection_get_is_connected (entry->connection))
1048 if (!bus_transaction_send_error_reply (transaction,
1051 entry->activation_message))
1058 bus_transaction_execute_and_free (transaction);
1064 bus_transaction_cancel_and_free (transaction);
1069 * Free the pending activation and send an error message to all the
1070 * connections that were waiting for it.
1073 pending_activation_failed (BusPendingActivation *pending_activation,
1074 const DBusError *how)
1076 /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1077 while (!try_send_activation_failure (pending_activation, how))
1078 _dbus_wait_for_memory ();
1080 /* Destroy this pending activation */
1081 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1082 pending_activation->service_name);
1086 babysitter_watch_callback (DBusWatch *watch,
1087 unsigned int condition,
1090 BusPendingActivation *pending_activation = data;
1092 DBusBabysitter *babysitter;
1094 babysitter = pending_activation->babysitter;
1096 _dbus_babysitter_ref (babysitter);
1098 retval = dbus_watch_handle (watch, condition);
1100 /* FIXME this is broken in the same way that
1101 * connection watches used to be; there should be
1102 * a separate callback for status change, instead
1103 * of doing "if we handled a watch status might
1106 * Fixing this lets us move dbus_watch_handle
1107 * calls into dbus-mainloop.c
1110 if (_dbus_babysitter_get_child_exited (babysitter))
1115 dbus_error_init (&error);
1116 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1118 /* Destroy all pending activations with the same exec */
1119 _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1121 while (_dbus_hash_iter_next (&iter))
1123 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1125 if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1126 pending_activation_failed (p, &error);
1129 /* Destroys the pending activation */
1130 pending_activation_failed (pending_activation, &error);
1132 dbus_error_free (&error);
1135 _dbus_babysitter_unref (babysitter);
1141 add_babysitter_watch (DBusWatch *watch,
1144 BusPendingActivation *pending_activation = data;
1146 return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1147 watch, babysitter_watch_callback, pending_activation,
1152 remove_babysitter_watch (DBusWatch *watch,
1155 BusPendingActivation *pending_activation = data;
1157 _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1158 watch, babysitter_watch_callback, pending_activation);
1162 pending_activation_timed_out (void *data)
1164 BusPendingActivation *pending_activation = data;
1167 /* Kill the spawned process, since it sucks
1168 * (not sure this is what we want to do, but
1169 * may as well try it for now)
1171 if (pending_activation->babysitter)
1172 _dbus_babysitter_kill_child (pending_activation->babysitter);
1174 dbus_error_init (&error);
1176 dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1177 "Activation of %s timed out",
1178 pending_activation->service_name);
1180 pending_activation_failed (pending_activation, &error);
1182 dbus_error_free (&error);
1188 cancel_pending (void *data)
1190 BusPendingActivation *pending_activation = data;
1192 _dbus_verbose ("Canceling pending activation of %s\n",
1193 pending_activation->service_name);
1195 if (pending_activation->babysitter)
1196 _dbus_babysitter_kill_child (pending_activation->babysitter);
1198 _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1199 pending_activation->service_name);
1203 free_pending_cancel_data (void *data)
1205 BusPendingActivation *pending_activation = data;
1207 bus_pending_activation_unref (pending_activation);
1211 add_cancel_pending_to_transaction (BusTransaction *transaction,
1212 BusPendingActivation *pending_activation)
1214 if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1216 free_pending_cancel_data))
1219 bus_pending_activation_ref (pending_activation);
1221 _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1227 update_service_cache (BusActivation *activation, DBusError *error)
1231 _dbus_hash_iter_init (activation->directories, &iter);
1232 while (_dbus_hash_iter_next (&iter))
1234 DBusError tmp_error;
1235 BusServiceDirectory *s_dir;
1237 s_dir = _dbus_hash_iter_get_value (&iter);
1239 dbus_error_init (&tmp_error);
1240 if (!update_directory (activation, s_dir, &tmp_error))
1242 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1244 dbus_move_error (&tmp_error, error);
1248 dbus_error_free (&tmp_error);
1256 static BusActivationEntry *
1257 activation_find_entry (BusActivation *activation,
1258 const char *service_name,
1261 BusActivationEntry *entry;
1263 entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1266 if (!update_service_cache (activation, error))
1269 entry = _dbus_hash_table_lookup_string (activation->entries,
1274 BusActivationEntry *updated_entry;
1276 if (!check_service_file (activation, entry, &updated_entry, error))
1279 entry = updated_entry;
1284 dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1285 "The name %s was not provided by any .service files",
1294 bus_activation_activate_service (BusActivation *activation,
1295 DBusConnection *connection,
1296 BusTransaction *transaction,
1297 dbus_bool_t auto_activation,
1298 DBusMessage *activation_message,
1299 const char *service_name,
1302 BusActivationEntry *entry;
1303 BusPendingActivation *pending_activation;
1304 BusPendingActivationEntry *pending_activation_entry;
1305 DBusMessage *message;
1306 DBusString service_str;
1311 dbus_bool_t activated;
1315 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1317 if (activation->n_pending_activations >=
1318 bus_context_get_max_pending_activations (activation->context))
1320 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1321 "The maximum number of pending activations has been reached, activation of %s failed",
1326 entry = activation_find_entry (activation, service_name, error);
1330 /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1331 * call us if the service is already active.
1333 if (!auto_activation)
1335 /* Check if the service is active */
1336 _dbus_string_init_const (&service_str, service_name);
1337 if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1339 dbus_uint32_t result;
1341 _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1343 message = dbus_message_new_method_return (activation_message);
1347 _dbus_verbose ("No memory to create reply to activate message\n");
1348 BUS_SET_OOM (error);
1352 result = DBUS_START_REPLY_ALREADY_RUNNING;
1354 if (!dbus_message_append_args (message,
1355 DBUS_TYPE_UINT32, &result,
1358 _dbus_verbose ("No memory to set args of reply to activate message\n");
1359 BUS_SET_OOM (error);
1360 dbus_message_unref (message);
1364 retval = bus_transaction_send_from_driver (transaction, connection, message);
1365 dbus_message_unref (message);
1368 _dbus_verbose ("Failed to send reply\n");
1369 BUS_SET_OOM (error);
1376 pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1377 if (!pending_activation_entry)
1379 _dbus_verbose ("Failed to create pending activation entry\n");
1380 BUS_SET_OOM (error);
1384 pending_activation_entry->auto_activation = auto_activation;
1386 pending_activation_entry->activation_message = activation_message;
1387 dbus_message_ref (activation_message);
1388 pending_activation_entry->connection = connection;
1389 dbus_connection_ref (connection);
1391 /* Check if the service is being activated */
1392 pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1393 if (pending_activation)
1395 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1397 _dbus_verbose ("Failed to append a new entry to pending activation\n");
1399 BUS_SET_OOM (error);
1400 bus_pending_activation_entry_free (pending_activation_entry);
1404 pending_activation->n_entries += 1;
1405 pending_activation->activation->n_pending_activations += 1;
1409 pending_activation = dbus_new0 (BusPendingActivation, 1);
1410 if (!pending_activation)
1412 _dbus_verbose ("Failed to create pending activation\n");
1414 BUS_SET_OOM (error);
1415 bus_pending_activation_entry_free (pending_activation_entry);
1419 pending_activation->activation = activation;
1420 pending_activation->refcount = 1;
1422 pending_activation->service_name = _dbus_strdup (service_name);
1423 if (!pending_activation->service_name)
1425 _dbus_verbose ("Failed to copy service name for pending activation\n");
1427 BUS_SET_OOM (error);
1428 bus_pending_activation_unref (pending_activation);
1429 bus_pending_activation_entry_free (pending_activation_entry);
1433 pending_activation->exec = _dbus_strdup (entry->exec);
1434 if (!pending_activation->exec)
1436 _dbus_verbose ("Failed to copy service exec for pending activation\n");
1437 BUS_SET_OOM (error);
1438 bus_pending_activation_unref (pending_activation);
1439 bus_pending_activation_entry_free (pending_activation_entry);
1443 pending_activation->timeout =
1444 _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1445 pending_activation_timed_out,
1448 if (!pending_activation->timeout)
1450 _dbus_verbose ("Failed to create timeout for pending activation\n");
1452 BUS_SET_OOM (error);
1453 bus_pending_activation_unref (pending_activation);
1454 bus_pending_activation_entry_free (pending_activation_entry);
1458 if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1459 pending_activation->timeout,
1460 handle_timeout_callback,
1464 _dbus_verbose ("Failed to add timeout for pending activation\n");
1466 BUS_SET_OOM (error);
1467 bus_pending_activation_unref (pending_activation);
1468 bus_pending_activation_entry_free (pending_activation_entry);
1472 pending_activation->timeout_added = TRUE;
1474 if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1476 _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1478 BUS_SET_OOM (error);
1479 bus_pending_activation_unref (pending_activation);
1480 bus_pending_activation_entry_free (pending_activation_entry);
1484 pending_activation->n_entries += 1;
1485 pending_activation->activation->n_pending_activations += 1;
1488 _dbus_hash_iter_init (activation->pending_activations, &iter);
1489 while (_dbus_hash_iter_next (&iter))
1491 BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1493 if (strcmp (p->exec, entry->exec) == 0)
1500 if (!_dbus_hash_table_insert_string (activation->pending_activations,
1501 pending_activation->service_name,
1502 pending_activation))
1504 _dbus_verbose ("Failed to put pending activation in hash table\n");
1506 BUS_SET_OOM (error);
1507 bus_pending_activation_unref (pending_activation);
1512 if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1514 _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1515 BUS_SET_OOM (error);
1516 _dbus_hash_table_remove_string (activation->pending_activations,
1517 pending_activation->service_name);
1525 /* Now try to spawn the process */
1526 if (!_dbus_shell_parse_argv (entry->exec, &argc, &argv, error))
1528 _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
1529 _DBUS_ASSERT_ERROR_IS_SET (error);
1531 _dbus_hash_table_remove_string (activation->pending_activations,
1532 pending_activation->service_name);
1537 _dbus_verbose ("Spawning %s ...\n", argv[0]);
1538 if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1539 child_setup, activation,
1542 _dbus_verbose ("Failed to spawn child\n");
1543 _DBUS_ASSERT_ERROR_IS_SET (error);
1544 dbus_free_string_array (argv);
1549 dbus_free_string_array (argv);
1551 _dbus_assert (pending_activation->babysitter != NULL);
1553 if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
1554 add_babysitter_watch,
1555 remove_babysitter_watch,
1560 BUS_SET_OOM (error);
1561 _dbus_verbose ("Failed to set babysitter watch functions\n");
1568 #ifdef DBUS_BUILD_TESTS
1572 #define SERVICE_NAME_1 "MyService1"
1573 #define SERVICE_NAME_2 "MyService2"
1574 #define SERVICE_NAME_3 "MyService3"
1576 #define SERVICE_FILE_1 "service-1.service"
1577 #define SERVICE_FILE_2 "service-2.service"
1578 #define SERVICE_FILE_3 "service-3.service"
1581 test_create_service_file (DBusString *dir,
1582 const char *filename,
1586 DBusString file_name, full_path;
1588 dbus_bool_t ret_val;
1591 _dbus_string_init_const (&file_name, filename);
1593 if (!_dbus_string_init (&full_path))
1596 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1597 !_dbus_concat_dir_and_file (&full_path, &file_name))
1603 file = fopen (_dbus_string_get_const_data (&full_path), "w");
1610 fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
1614 _dbus_string_free (&full_path);
1619 test_remove_service_file (DBusString *dir, const char *filename)
1621 DBusString file_name, full_path;
1622 dbus_bool_t ret_val;
1626 _dbus_string_init_const (&file_name, filename);
1628 if (!_dbus_string_init (&full_path))
1631 if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
1632 !_dbus_concat_dir_and_file (&full_path, &file_name))
1638 if (!_dbus_delete_file (&full_path, NULL))
1645 _dbus_string_free (&full_path);
1650 test_remove_directory (DBusString *dir)
1653 DBusString filename, full_path;
1654 dbus_bool_t ret_val;
1658 if (!_dbus_string_init (&filename))
1661 if (!_dbus_string_init (&full_path))
1663 _dbus_string_free (&filename);
1667 iter = _dbus_directory_open (dir, NULL);
1674 while (_dbus_directory_get_next_file (iter, &filename, NULL))
1676 if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
1682 _dbus_directory_close (iter);
1684 if (!_dbus_delete_directory (dir, NULL))
1691 _dbus_string_free (&filename);
1692 _dbus_string_free (&full_path);
1698 init_service_reload_test (DBusString *dir)
1702 if (!_dbus_stat (dir, &stat_buf, NULL))
1704 if (!_dbus_create_directory (dir, NULL))
1709 if (!test_remove_directory (dir))
1712 if (!_dbus_create_directory (dir, NULL))
1716 /* Create one initial file */
1717 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
1724 cleanup_service_reload_test (DBusString *dir)
1726 if (!test_remove_directory (dir))
1734 BusActivation *activation;
1735 const char *service_name;
1736 dbus_bool_t expecting_find;
1740 check_func (void *data)
1743 BusActivationEntry *entry;
1745 dbus_bool_t ret_val;
1750 dbus_error_init (&error);
1752 entry = activation_find_entry (d->activation, d->service_name, &error);
1755 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1761 if (d->expecting_find)
1765 dbus_error_free (&error);
1769 if (!d->expecting_find)
1777 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
1782 err = !_dbus_test_oom_handling (description, check_func, data);
1784 err = !check_func (data);
1787 _dbus_assert_not_reached ("Test failed");
1793 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
1795 BusActivation *activation;
1797 DBusList *directories;
1801 _dbus_string_init_const (&address, "");
1803 if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
1806 activation = bus_activation_new (NULL, &address, &directories, NULL);
1810 d.activation = activation;
1812 /* Check for existing service file */
1813 d.expecting_find = TRUE;
1814 d.service_name = SERVICE_NAME_1;
1816 if (!do_test ("Existing service file", oom_test, &d))
1819 /* Check for non-existing service file */
1820 d.expecting_find = FALSE;
1821 d.service_name = SERVICE_NAME_3;
1823 if (!do_test ("Nonexisting service file", oom_test, &d))
1826 /* Check for added service file */
1827 if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
1830 d.expecting_find = TRUE;
1831 d.service_name = SERVICE_NAME_2;
1833 if (!do_test ("Added service file", oom_test, &d))
1836 /* Check for removed service file */
1837 if (!test_remove_service_file (dir, SERVICE_FILE_2))
1840 d.expecting_find = FALSE;
1841 d.service_name = SERVICE_FILE_2;
1843 if (!do_test ("Removed service file", oom_test, &d))
1846 /* Check for updated service file */
1848 _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
1850 if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
1853 d.expecting_find = TRUE;
1854 d.service_name = SERVICE_NAME_3;
1856 if (!do_test ("Updated service file, part 1", oom_test, &d))
1859 d.expecting_find = FALSE;
1860 d.service_name = SERVICE_NAME_1;
1862 if (!do_test ("Updated service file, part 2", oom_test, &d))
1865 bus_activation_unref (activation);
1866 _dbus_list_clear (&directories);
1872 bus_activation_service_reload_test (const DBusString *test_data_dir)
1874 DBusString directory;
1876 if (!_dbus_string_init (&directory))
1879 if (!_dbus_string_append (&directory, "/tmp/dbus-reload-test-") ||
1880 !_dbus_generate_random_ascii (&directory, 6))
1885 /* Do normal tests */
1886 if (!init_service_reload_test (&directory))
1887 _dbus_assert_not_reached ("could not initiate service reload test");
1889 if (!do_service_reload_test (&directory, FALSE))
1893 if (!init_service_reload_test (&directory))
1894 _dbus_assert_not_reached ("could not initiate service reload test");
1896 if (!do_service_reload_test (&directory, TRUE))
1899 /* Cleanup test directory */
1900 if (!cleanup_service_reload_test (&directory))
1903 _dbus_string_free (&directory);
1908 #endif /* DBUS_BUILD_TESTS */