* Copyright (C) 2003 CodeFactory AB
* Copyright (C) 2003 Red Hat, Inc.
* Copyright (C) 2004 Imendio HB
+ * Copyright (C) 2013 Samsung Electronics
*
* Licensed under the Academic Free License version 2.1
- *
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+
+#include <config.h>
#include "activation.h"
#include "activation-exit-codes.h"
#include "desktop-file.h"
+#include "dispatch.h"
#include "services.h"
#include "test.h"
#include "utils.h"
char *name;
char *exec;
char *user;
+ char *systemd_service;
unsigned long mtime;
BusServiceDirectory *s_dir;
char *filename;
struct BusPendingActivationEntry
{
+ /* Normally a method call, but if connection is NULL, this is a signal
+ * instead.
+ */
DBusMessage *activation_message;
+ /* NULL if this activation entry is for the dbus-daemon itself,
+ * waiting for systemd to start. In this case, auto_activation is always
+ * TRUE.
+ */
DBusConnection *connection;
dbus_bool_t auto_activation;
BusActivation *activation;
char *service_name;
char *exec;
+ char *systemd_service;
DBusList *entries;
int n_entries;
DBusBabysitter *babysitter;
bus_service_directory_ref (BusServiceDirectory *dir)
{
_dbus_assert (dir->refcount);
-
+
dir->refcount++;
return dir;
static void
bus_service_directory_unref (BusServiceDirectory *dir)
{
- if (dir == NULL)
- return;
+ if (dir == NULL)
+ return;
_dbus_assert (dir->refcount > 0);
dir->refcount--;
{
if (entry->activation_message)
dbus_message_unref (entry->activation_message);
-
+
if (entry->connection)
dbus_connection_unref (entry->connection);
-
- dbus_free (entry);
-}
-static void
-handle_timeout_callback (DBusTimeout *timeout,
- void *data)
-{
- BusPendingActivation *pending_activation = data;
-
- while (!dbus_timeout_handle (pending_activation->timeout))
- _dbus_wait_for_memory ();
+ dbus_free (entry);
}
-static BusPendingActivation *
+static BusPendingActivation *
bus_pending_activation_ref (BusPendingActivation *pending_activation)
{
_dbus_assert (pending_activation->refcount > 0);
bus_pending_activation_unref (BusPendingActivation *pending_activation)
{
DBusList *link;
-
+
if (pending_activation == NULL) /* hash table requires this */
return;
if (pending_activation->refcount > 0)
return;
-
+
if (pending_activation->timeout_added)
{
_dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
- pending_activation->timeout,
- handle_timeout_callback, pending_activation);
+ pending_activation->timeout);
pending_activation->timeout_added = FALSE;
}
if (pending_activation->timeout)
_dbus_timeout_unref (pending_activation->timeout);
-
+
if (pending_activation->babysitter)
{
if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
pending_activation->babysitter,
NULL))
_dbus_assert_not_reached ("setting watch functions to NULL failed");
-
+
_dbus_babysitter_unref (pending_activation->babysitter);
}
-
+
dbus_free (pending_activation->service_name);
dbus_free (pending_activation->exec);
+ dbus_free (pending_activation->systemd_service);
link = _dbus_list_get_first_link (&pending_activation->entries);
pending_activation->n_entries;
_dbus_assert (pending_activation->activation->n_pending_activations >= 0);
-
+
dbus_free (pending_activation);
}
{
if (entry == NULL) /* hash table requires this */
return;
-
+
_dbus_assert (entry->refcount > 0);
entry->refcount--;
-
- if (entry->refcount > 0)
+
+ if (entry->refcount > 0)
return;
-
+
dbus_free (entry->name);
dbus_free (entry->exec);
dbus_free (entry->user);
dbus_free (entry->filename);
+ dbus_free (entry->systemd_service);
dbus_free (entry);
}
BusDesktopFile *desktop_file,
DBusError *error)
{
- char *name, *exec, *user;
+ char *name, *exec, *user, *exec_tmp, *systemd_service;
BusActivationEntry *entry;
DBusStat stat_buf;
DBusString file_path;
DBusError tmp_error;
+ dbus_bool_t retval;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
+
+ retval = FALSE;
name = NULL;
exec = NULL;
user = NULL;
+ exec_tmp = NULL;
entry = NULL;
-
+ systemd_service = NULL;
+
dbus_error_init (&tmp_error);
if (!_dbus_string_init (&file_path))
BUS_SET_OOM (error);
return FALSE;
}
-
+
if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
!_dbus_concat_dir_and_file (&file_path, filename))
{
BUS_SET_OOM (error);
- goto failed;
+ goto out;
}
-
- if (!_dbus_stat (&file_path, &stat_buf, NULL))
+
+ if (!_dbus_stat (&file_path, &stat_buf, NULL))
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Can't stat the service file\n");
- goto failed;
+ goto out;
}
-
+
if (!bus_desktop_file_get_string (desktop_file,
DBUS_SERVICE_SECTION,
DBUS_SERVICE_NAME,
&name,
error))
- goto failed;
+ goto out;
if (!bus_desktop_file_get_string (desktop_file,
DBUS_SERVICE_SECTION,
DBUS_SERVICE_EXEC,
- &exec,
+ &exec_tmp,
error))
- goto failed;
+ goto out;
+
+ exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
+ dbus_free (exec_tmp);
+ exec_tmp = NULL;
/* user is not _required_ unless we are using system activation */
if (!bus_desktop_file_get_string (desktop_file,
if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
{
dbus_move_error (&tmp_error, error);
- goto failed;
+ goto out;
}
else
{
}
_DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
- entry = _dbus_hash_table_lookup_string (s_dir->entries,
+ /* systemd service is never required */
+ if (!bus_desktop_file_get_string (desktop_file,
+ DBUS_SERVICE_SECTION,
+ DBUS_SERVICE_SYSTEMD_SERVICE,
+ &systemd_service, &tmp_error))
+ {
+ _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+ /* if we got OOM, then exit */
+ if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
+ {
+ dbus_move_error (&tmp_error, error);
+ goto out;
+ }
+ else
+ {
+ /* if we have error because we didn't find anything then continue */
+ dbus_error_free (&tmp_error);
+ dbus_free (systemd_service);
+ systemd_service = NULL;
+ }
+ }
+
+ _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
+
+ entry = _dbus_hash_table_lookup_string (s_dir->entries,
_dbus_string_get_const_data (filename));
+
if (entry == NULL) /* New file */
- {
+ {
/* FIXME we need a better-defined algorithm for which service file to
* pick than "whichever one is first in the directory listing"
*/
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Service %s already exists in activation entry list\n", name);
- goto failed;
+ goto out;
}
-
+
entry = dbus_new0 (BusActivationEntry, 1);
if (entry == NULL)
{
BUS_SET_OOM (error);
- goto failed;
+ goto out;
}
-
+
entry->name = name;
entry->exec = exec;
entry->user = user;
+ entry->systemd_service = systemd_service;
entry->refcount = 1;
-
+
+ /* ownership has been transferred to entry, do not free separately */
+ name = NULL;
+ exec = NULL;
+ user = NULL;
+ systemd_service = NULL;
+
entry->s_dir = s_dir;
entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
if (!entry->filename)
{
BUS_SET_OOM (error);
- goto failed;
+ goto out;
}
if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
{
BUS_SET_OOM (error);
- goto failed;
+ goto out;
}
-
+
if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
{
/* Revert the insertion in the entries table */
_dbus_hash_table_remove_string (activation->entries, entry->name);
BUS_SET_OOM (error);
- goto failed;
+ goto out;
}
_dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
if (_dbus_hash_table_lookup_string (activation->entries, name))
{
- _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
+ _dbus_verbose ("The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
name, _dbus_string_get_const_data (&file_path));
- goto failed;
+ dbus_set_error (error, DBUS_ERROR_FAILED,
+ "The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
+ name, _dbus_string_get_const_data (&file_path));
+ goto out;
}
-
+
+ /* ownership has been transferred to entry, do not free separately */
dbus_free (entry->name);
- dbus_free (entry->exec);
- dbus_free (entry->user);
entry->name = name;
+ name = NULL;
+
+ dbus_free (entry->exec);
entry->exec = exec;
+ exec = NULL;
+
+ dbus_free (entry->user);
entry->user = user;
+ user = NULL;
+
+ dbus_free (entry->systemd_service);
+ entry->systemd_service = systemd_service;
+ systemd_service = NULL;
+
if (!_dbus_hash_table_insert_string (activation->entries,
entry->name, bus_activation_entry_ref(entry)))
{
BUS_SET_OOM (error);
/* Also remove path to entries hash since we want this in sync with
* the entries hash table */
- _dbus_hash_table_remove_string (entry->s_dir->entries,
+ _dbus_hash_table_remove_string (entry->s_dir->entries,
entry->filename);
- bus_activation_entry_unref (entry);
- return FALSE;
+ goto out;
}
}
-
- entry->mtime = stat_buf.mtime;
-
- _dbus_string_free (&file_path);
- bus_activation_entry_unref (entry);
- return TRUE;
+ entry->mtime = stat_buf.mtime;
+ retval = TRUE;
-failed:
+out:
+ /* if these have been transferred into entry, the variables will be NULL */
dbus_free (name);
dbus_free (exec);
dbus_free (user);
+ dbus_free (systemd_service);
_dbus_string_free (&file_path);
if (entry)
bus_activation_entry_unref (entry);
-
- return FALSE;
+
+ return retval;
}
static dbus_bool_t
retval = TRUE;
tmp_entry = entry;
-
+
_dbus_string_init_const (&filename, entry->filename);
-
+
if (!_dbus_string_init (&file_path))
{
BUS_SET_OOM (error);
return FALSE;
}
-
+
if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
!_dbus_concat_dir_and_file (&file_path, &filename))
{
retval = FALSE;
goto out;
}
-
+
if (!_dbus_stat (&file_path, &stat_buf, NULL))
{
_dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
retval = TRUE;
goto out;
}
- else
+ else
{
- if (stat_buf.mtime > entry->mtime)
+ if (stat_buf.mtime > entry->mtime)
{
BusDesktopFile *desktop_file;
DBusError tmp_error;
-
+
dbus_error_init (&tmp_error);
-
+
desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
if (desktop_file == NULL)
{
_dbus_verbose ("Could not load %s: %s\n",
- _dbus_string_get_const_data (&file_path),
+ _dbus_string_get_const_data (&file_path),
tmp_error.message);
if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
{
retval = TRUE;
goto out;
}
-
- /* @todo We can return OOM or a DBUS_ERROR_FAILED error
+
+ /* @todo We can return OOM or a DBUS_ERROR_FAILED error
* Handle these both better
- */
+ */
if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
{
bus_desktop_file_free (desktop_file);
retval = TRUE;
goto out;
}
-
+
bus_desktop_file_free (desktop_file);
retval = TRUE;
}
}
-
+
out:
_dbus_string_free (&file_path);
dbus_bool_t retval;
BusActivationEntry *entry;
DBusString full_path;
-
+
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
+
iter = NULL;
desktop_file = NULL;
-
+
_dbus_string_init_const (&dir, s_dir->dir_c);
-
+
if (!_dbus_string_init (&filename))
{
BUS_SET_OOM (error);
retval = FALSE;
/* from this point it's safe to "goto out" */
-
+
iter = _dbus_directory_open (&dir, error);
if (iter == NULL)
{
_dbus_verbose ("Failed to open directory %s: %s\n",
- s_dir->dir_c,
+ s_dir->dir_c,
error ? error->message : "unknown");
goto out;
}
-
+
/* Now read the files */
dbus_error_init (&tmp_error);
while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
{
_dbus_assert (!dbus_error_is_set (&tmp_error));
-
+
_dbus_string_set_length (&full_path, 0);
-
+
if (!_dbus_string_ends_with_c_str (&filename, ".service"))
{
_dbus_verbose ("Skipping non-.service file %s\n",
}
entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
- if (entry) /* Already has this service file in the cache */
+ if (entry) /* Already has this service file in the cache */
{
if (!check_service_file (activation, entry, NULL, error))
goto out;
continue;
}
-
+
if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
!_dbus_concat_dir_and_file (&full_path, &filename))
{
BUS_SET_OOM (error);
goto out;
}
-
+
/* New file */
desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
if (desktop_file == NULL)
dbus_move_error (&tmp_error, error);
goto out;
}
-
+
dbus_error_free (&tmp_error);
continue;
}
- /* @todo We can return OOM or a DBUS_ERROR_FAILED error
+ /* @todo We can return OOM or a DBUS_ERROR_FAILED error
* Handle these both better
- */
+ */
if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
{
bus_desktop_file_free (desktop_file);
desktop_file = NULL;
-
+
_dbus_verbose ("Could not add %s to activation entry list: %s\n",
_dbus_string_get_const_data (&full_path), tmp_error.message);
dbus_move_error (&tmp_error, error);
goto out;
}
-
+
retval = TRUE;
out:
_DBUS_ASSERT_ERROR_IS_SET (error);
else
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
+
if (iter != NULL)
_dbus_directory_close (iter);
_dbus_string_free (&filename);
_dbus_string_free (&full_path);
-
+
return retval;
}
DBusError *error)
{
BusActivation *activation;
- DBusList *link;
- char *dir;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
(DBusFreeFunction) dbus_free,
(DBusFreeFunction) dbus_free);
- if (activation->environment == NULL)
+ if (activation->environment == NULL)
{
BUS_SET_OOM (error);
goto failed;
}
return activation;
-
+
failed:
- bus_activation_unref (activation);
+ bus_activation_unref (activation);
return NULL;
}
bus_activation_ref (BusActivation *activation)
{
_dbus_assert (activation->refcount > 0);
-
+
activation->refcount += 1;
return activation;
if (activation->refcount > 0)
return;
-
+
dbus_free (activation->server_address);
if (activation->entries)
_dbus_hash_table_unref (activation->entries);
if (activation->pending_activations)
_dbus_hash_table_unref (activation->pending_activations);
- if (activation->directories)
+ if (activation->directories)
_dbus_hash_table_unref (activation->directories);
if (activation->environment)
_dbus_hash_table_unref (activation->environment);
DBusError *error)
{
const char *type;
-
+
if (!bus_activation_set_environment_variable (activation,
"DBUS_STARTER_ADDRESS",
activation->server_address,
error))
return FALSE;
-
+
type = bus_context_get_type (activation->context);
if (type != NULL)
{
_dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
d->pending_activation->service_name,
d->pending_activation->timeout_added);
-
+
_dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
d->hash_entry,
d->pending_activation->service_name, d->pending_activation);
bus_pending_activation_ref (d->pending_activation);
-
+
d->hash_entry = NULL;
}
d->hash_entry);
bus_pending_activation_unref (d->pending_activation);
-
+
dbus_free (d);
}
d = dbus_new (RestorePendingData, 1);
if (d == NULL)
return FALSE;
-
+
d->pending_activation = pending_activation;
d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
-
+
bus_pending_activation_ref (d->pending_activation);
-
+
if (d->hash_entry == NULL ||
!bus_transaction_add_cancel_hook (transaction, restore_pending, d,
free_pending_restore_data))
}
_dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
-
+
return TRUE;
}
DBusList *link;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
+
/* Check if it's a pending activation */
pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
if (!pending_activation)
return TRUE;
+ bus_context_log (activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
+ service_name);
+
link = _dbus_list_get_first_link (&pending_activation->entries);
while (link != NULL)
{
BusPendingActivationEntry *entry = link->data;
DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
-
- if (dbus_connection_get_is_connected (entry->connection))
+
+ /* entry->connection is NULL for activating systemd */
+ if (entry->connection && dbus_connection_get_is_connected (entry->connection))
{
/* Only send activation replies to regular activation requests. */
if (!entry->auto_activation)
{
dbus_uint32_t result;
-
+
message = dbus_message_new_method_return (entry->activation_message);
if (!message)
{
}
result = DBUS_START_REPLY_SUCCESS;
-
+
if (!dbus_message_append_args (message,
DBUS_TYPE_UINT32, &result,
DBUS_TYPE_INVALID))
BUS_SET_OOM (error);
goto error;
}
-
+
if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
{
dbus_message_unref (message);
BUS_SET_OOM (error);
goto error;
}
-
+
dbus_message_unref (message);
}
}
-
+
link = next;
}
DBusList *link;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
+
/* Check if it's a pending activation */
pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
bus_service_get_name (service));
BusPendingActivationEntry *entry = link->data;
DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
- if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
+ if (entry->auto_activation && (entry->connection == NULL || dbus_connection_get_is_connected (entry->connection)))
{
DBusConnection *addressed_recipient;
-
- addressed_recipient = bus_service_get_primary_owners_connection (service);
- /* Check the security policy, which has the side-effect of adding an
- * expected pending reply.
+#ifdef ENABLE_KDBUS_TRANSPORT
+ /* kdbus change - we can not send anything using phantom connections
+ * (DBusConnection structures for services other than daemon)
+ * so we have to use daemon connection
*/
- if (!bus_context_check_security_policy (activation->context, transaction,
- entry->connection,
- addressed_recipient,
- addressed_recipient,
- entry->activation_message, error))
- goto error;
+ if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+ addressed_recipient = entry->connection;
+ else
+#endif
+ addressed_recipient = bus_service_get_primary_owners_connection (service);
- if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
- {
- BUS_SET_OOM (error);
- goto error;
- }
+ /* Resume dispatching where we left off in bus_dispatch() */
+ if (!bus_dispatch_matches (transaction,
+ entry->connection,
+ addressed_recipient,
+ entry->activation_message, error))
+ goto error;
}
link = next;
BUS_SET_OOM (error);
goto error;
}
-
+
_dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
return TRUE;
BusActivation *activation;
DBusList *link;
BusTransaction *transaction;
-
+
activation = pending_activation->activation;
transaction = bus_transaction_new (activation->context);
if (transaction == NULL)
return FALSE;
-
+
link = _dbus_list_get_first_link (&pending_activation->entries);
while (link != NULL)
{
BusPendingActivationEntry *entry = link->data;
DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
-
- if (dbus_connection_get_is_connected (entry->connection))
+
+ if (entry->connection && dbus_connection_get_is_connected (entry->connection))
{
if (!bus_transaction_send_error_reply (transaction,
entry->connection,
entry->activation_message))
goto error;
}
-
+
link = next;
}
bus_transaction_execute_and_free (transaction);
-
+
return TRUE;
error:
}
}
-static dbus_bool_t
-babysitter_watch_callback (DBusWatch *watch,
- unsigned int condition,
- void *data)
+static void
+pending_activation_finished_cb (DBusBabysitter *babysitter,
+ void *data)
{
BusPendingActivation *pending_activation = data;
- dbus_bool_t retval;
- DBusBabysitter *babysitter;
dbus_bool_t uses_servicehelper;
- babysitter = pending_activation->babysitter;
-
+ _dbus_assert (babysitter == pending_activation->babysitter);
_dbus_babysitter_ref (babysitter);
- retval = dbus_watch_handle (watch, condition);
-
/* There are two major cases here; are we the system bus or the session? Here this
* is distinguished by whether or not we use a setuid helper launcher. With the launch helper,
* some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
*/
uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
- /* FIXME this is broken in the same way that
- * connection watches used to be; there should be
- * a separate callback for status change, instead
- * of doing "if we handled a watch status might
- * have changed"
- *
- * Fixing this lets us move dbus_watch_handle
- * calls into dbus-mainloop.c
- */
+ /* strictly speaking this is redundant with the check in dbus-spawn now */
if (_dbus_babysitter_get_child_exited (babysitter))
{
DBusError error;
if (activation_failed)
{
+ bus_context_log (pending_activation->activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Activated service '%s' failed: %s",
+ pending_activation->service_name,
+ error.message);
+
/* Destroy all pending activations with the same exec */
_dbus_hash_iter_init (pending_activation->activation->pending_activations,
&iter);
}
_dbus_babysitter_unref (babysitter);
-
- return retval;
}
static dbus_bool_t
{
BusPendingActivation *pending_activation = data;
- return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
- watch, babysitter_watch_callback, pending_activation,
- NULL);
+ return _dbus_loop_add_watch (
+ bus_context_get_loop (pending_activation->activation->context),
+ watch);
}
static void
void *data)
{
BusPendingActivation *pending_activation = data;
-
+
_dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
- watch, babysitter_watch_callback, pending_activation);
+ watch);
+}
+
+static void
+toggle_babysitter_watch (DBusWatch *watch,
+ void *data)
+{
+ BusPendingActivation *pending_activation = data;
+
+ _dbus_loop_toggle_watch (bus_context_get_loop (pending_activation->activation->context),
+ watch);
}
static dbus_bool_t
{
BusPendingActivation *pending_activation = data;
DBusError error;
-
+
/* Kill the spawned process, since it sucks
* (not sure this is what we want to do, but
* may as well try it for now)
*/
- if (pending_activation->babysitter)
+ if (pending_activation->babysitter)
_dbus_babysitter_kill_child (pending_activation->babysitter);
dbus_error_init (&error);
dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
"Activation of %s timed out",
pending_activation->service_name);
+ bus_context_log (pending_activation->activation->context,
+ DBUS_SYSTEM_LOG_INFO,
+ "Failed to activate service '%s': timed out",
+ pending_activation->service_name);
pending_activation_failed (pending_activation, &error);
if (pending_activation->babysitter)
_dbus_babysitter_kill_child (pending_activation->babysitter);
-
+
_dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
pending_activation->service_name);
}
free_pending_cancel_data (void *data)
{
BusPendingActivation *pending_activation = data;
-
+
bus_pending_activation_unref (pending_activation);
}
static dbus_bool_t
add_cancel_pending_to_transaction (BusTransaction *transaction,
BusPendingActivation *pending_activation)
-{
+{
if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
pending_activation,
free_pending_cancel_data))
return FALSE;
- bus_pending_activation_ref (pending_activation);
-
+ bus_pending_activation_ref (pending_activation);
+
_dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
-
+
return TRUE;
}
-static dbus_bool_t
+static dbus_bool_t
update_service_cache (BusActivation *activation, DBusError *error)
{
DBusHashIter iter;
-
+
_dbus_hash_iter_init (activation->directories, &iter);
while (_dbus_hash_iter_next (&iter))
{
continue;
}
}
-
+
return TRUE;
}
static BusActivationEntry *
-activation_find_entry (BusActivation *activation,
+activation_find_entry (BusActivation *activation,
const char *service_name,
DBusError *error)
{
BusActivationEntry *entry;
-
+
entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
if (!entry)
- {
- if (!update_service_cache (activation, error))
+ {
+ if (!update_service_cache (activation, error))
return NULL;
entry = _dbus_hash_table_lookup_string (activation->entries,
service_name);
}
- else
+ else
{
BusActivationEntry *updated_entry;
- if (!check_service_file (activation, entry, &updated_entry, error))
+ if (!check_service_file (activation, entry, &updated_entry, error))
return NULL;
entry = updated_entry;
}
- if (!entry)
+ if (!entry)
{
dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
"The name %s was not provided by any .service files",
const char *service_name,
DBusError *error)
{
+ DBusError tmp_error;
BusActivationEntry *entry;
BusPendingActivation *pending_activation;
BusPendingActivationEntry *pending_activation_entry;
char **envp = NULL;
int argc;
dbus_bool_t retval;
- DBusHashIter iter;
- dbus_bool_t activated;
+ dbus_bool_t was_pending_activation;
DBusString command;
-
- activated = TRUE;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
}
entry = activation_find_entry (activation, service_name, error);
- if (!entry)
+ if (!entry)
return FALSE;
/* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
{
dbus_uint32_t result;
-
+
_dbus_verbose ("Service \"%s\" is already active\n", service_name);
-
+
message = dbus_message_new_method_return (activation_message);
if (!message)
}
result = DBUS_START_REPLY_ALREADY_RUNNING;
-
+
if (!dbus_message_append_args (message,
DBUS_TYPE_UINT32, &result,
DBUS_TYPE_INVALID))
return retval;
}
}
-
+
pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
if (!pending_activation_entry)
{
pending_activation_entry->activation_message = activation_message;
dbus_message_ref (activation_message);
pending_activation_entry->connection = connection;
- dbus_connection_ref (connection);
-
+ if (connection)
+ dbus_connection_ref (connection);
+
/* Check if the service is being activated */
pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
- if (pending_activation)
+ was_pending_activation = (pending_activation != NULL);
+ if (was_pending_activation)
{
if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
{
_dbus_verbose ("Failed to append a new entry to pending activation\n");
-
+
BUS_SET_OOM (error);
bus_pending_activation_entry_free (pending_activation_entry);
return FALSE;
if (!pending_activation)
{
_dbus_verbose ("Failed to create pending activation\n");
-
+
BUS_SET_OOM (error);
- bus_pending_activation_entry_free (pending_activation_entry);
+ bus_pending_activation_entry_free (pending_activation_entry);
return FALSE;
}
pending_activation->activation = activation;
pending_activation->refcount = 1;
-
+
pending_activation->service_name = _dbus_strdup (service_name);
if (!pending_activation->service_name)
{
_dbus_verbose ("Failed to copy service name for pending activation\n");
-
+
BUS_SET_OOM (error);
bus_pending_activation_unref (pending_activation);
- bus_pending_activation_entry_free (pending_activation_entry);
+ bus_pending_activation_entry_free (pending_activation_entry);
return FALSE;
}
return FALSE;
}
+ if (entry->systemd_service)
+ {
+ pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
+ if (!pending_activation->systemd_service)
+ {
+ _dbus_verbose ("Failed to copy systemd service for pending activation\n");
+ BUS_SET_OOM (error);
+ bus_pending_activation_unref (pending_activation);
+ bus_pending_activation_entry_free (pending_activation_entry);
+ return FALSE;
+ }
+ }
+
pending_activation->timeout =
_dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
pending_activation_timed_out,
if (!pending_activation->timeout)
{
_dbus_verbose ("Failed to create timeout for pending activation\n");
-
+
BUS_SET_OOM (error);
bus_pending_activation_unref (pending_activation);
bus_pending_activation_entry_free (pending_activation_entry);
}
if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
- pending_activation->timeout,
- handle_timeout_callback,
- pending_activation,
- NULL))
+ pending_activation->timeout))
{
_dbus_verbose ("Failed to add timeout for pending activation\n");
-
+
BUS_SET_OOM (error);
bus_pending_activation_unref (pending_activation);
- bus_pending_activation_entry_free (pending_activation_entry);
+ bus_pending_activation_entry_free (pending_activation_entry);
return FALSE;
}
pending_activation->timeout_added = TRUE;
-
+
if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
{
_dbus_verbose ("Failed to add entry to just-created pending activation\n");
-
+
BUS_SET_OOM (error);
bus_pending_activation_unref (pending_activation);
- bus_pending_activation_entry_free (pending_activation_entry);
+ bus_pending_activation_entry_free (pending_activation_entry);
return FALSE;
}
pending_activation->n_entries += 1;
pending_activation->activation->n_pending_activations += 1;
-
- activated = FALSE;
- _dbus_hash_iter_init (activation->pending_activations, &iter);
- while (_dbus_hash_iter_next (&iter))
- {
- BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
-
- if (strcmp (p->exec, entry->exec) == 0)
- {
- activated = TRUE;
- break;
- }
- }
-
+
if (!_dbus_hash_table_insert_string (activation->pending_activations,
pending_activation->service_name,
pending_activation))
{
_dbus_verbose ("Failed to put pending activation in hash table\n");
-
+
BUS_SET_OOM (error);
bus_pending_activation_unref (pending_activation);
return FALSE;
}
}
-
+
if (!add_cancel_pending_to_transaction (transaction, pending_activation))
{
_dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
return FALSE;
}
-
- if (activated)
+
+ if (was_pending_activation)
return TRUE;
+ if (bus_context_get_systemd_activation (activation->context))
+ {
+ if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
+ /* systemd itself is missing apparently. That can happen
+ only during early startup. Let's just wait until systemd
+ connects to us and do nothing. */
+ return TRUE;
+
+ if (entry->systemd_service)
+ {
+ BusTransaction *activation_transaction;
+ DBusString service_string;
+ BusService *service;
+ BusRegistry *registry;
+
+ /* OK, we have a systemd service configured for this entry,
+ hence let's enqueue an activation request message. This
+ is implemented as a directed signal, not a method call,
+ for three reasons: 1) we don't expect a response on
+ success, where we just expect a name appearing on the
+ bus; 2) at this time the systemd service might not yet
+ have connected, so we wouldn't know the message serial at
+ this point to set up a pending call; 3) it is ugly if the
+ bus suddenly becomes the caller of a remote method. */
+
+ message = dbus_message_new_signal (DBUS_PATH_DBUS,
+ "org.freedesktop.systemd1.Activator",
+ "ActivationRequest");
+ if (!message)
+ {
+ _dbus_verbose ("No memory to create activation message\n");
+ BUS_SET_OOM (error);
+ return FALSE;
+ }
+
+ if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
+ !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
+ !dbus_message_append_args (message,
+ DBUS_TYPE_STRING, &entry->systemd_service,
+ DBUS_TYPE_INVALID))
+ {
+ _dbus_verbose ("No memory to set args of activation message\n");
+ dbus_message_unref (message);
+ BUS_SET_OOM (error);
+ return FALSE;
+ }
+
+ /* Create our transaction */
+ activation_transaction = bus_transaction_new (activation->context);
+ if (activation_transaction == NULL)
+ {
+ _dbus_verbose ("No memory to create activation transaction\n");
+ dbus_message_unref (message);
+ BUS_SET_OOM (error);
+ return FALSE;
+ }
+
+ /* Check whether systemd is already connected */
+ registry = bus_connection_get_registry (connection);
+ _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
+ service = bus_registry_lookup (registry, &service_string);
+
+ if (service != NULL)
+ {
+ bus_context_log (activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Activating via systemd: service name='%s' unit='%s'",
+ service_name,
+ entry->systemd_service);
+ /* Wonderful, systemd is connected, let's just send the msg */
+ retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
+ message, error);
+ }
+ else
+ {
+ bus_context_log (activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Activating systemd to hand-off: service name='%s' unit='%s'",
+ service_name,
+ entry->systemd_service);
+ /* systemd is not around, let's "activate" it. */
+ retval = bus_activation_activate_service (activation, NULL, activation_transaction, TRUE,
+ message, "org.freedesktop.systemd1", error);
+ }
+
+ dbus_message_unref (message);
+
+ if (!retval)
+ {
+ bus_context_log (activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Failed to activate via systemd: service name='%s' unit='%s'",
+ service_name,
+ entry->systemd_service);
+ _DBUS_ASSERT_ERROR_IS_SET (error);
+ _dbus_verbose ("failed to send activation message: %s\n", error->name);
+ bus_transaction_cancel_and_free (activation_transaction);
+ return FALSE;
+ }
+
+ bus_transaction_execute_and_free (activation_transaction);
+ return TRUE;
+ }
+
+ /* OK, we have no configured systemd service, hence let's
+ proceed with traditional activation. */
+ }
+
/* use command as system and session different */
if (!_dbus_string_init (&command))
{
{
_dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
_DBUS_ASSERT_ERROR_IS_SET (error);
-
+
_dbus_hash_table_remove_string (activation->pending_activations,
pending_activation->service_name);
}
_dbus_verbose ("Spawning %s ...\n", argv[0]);
+ if (servicehelper != NULL)
+ bus_context_log (activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Activating service name='%s' (using servicehelper)",
+ service_name);
+ else
+ bus_context_log (activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Activating service name='%s'",
+ service_name);
+
+ dbus_error_init (&tmp_error);
+
if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
envp,
- NULL, activation,
- error))
+ NULL, activation,
+ &tmp_error))
{
_dbus_verbose ("Failed to spawn child\n");
- _DBUS_ASSERT_ERROR_IS_SET (error);
+ bus_context_log (activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Failed to activate service %s: %s",
+ service_name,
+ tmp_error.message);
+ _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+ dbus_move_error (&tmp_error, error);
dbus_free_string_array (argv);
dbus_free_string_array (envp);
envp = NULL;
_dbus_assert (pending_activation->babysitter != NULL);
-
+
+ _dbus_babysitter_set_result_function (pending_activation->babysitter,
+ pending_activation_finished_cb,
+ pending_activation);
+
if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
add_babysitter_watch,
remove_babysitter_watch,
- NULL,
+ toggle_babysitter_watch,
pending_activation,
NULL))
{
_dbus_verbose ("Failed to set babysitter watch functions\n");
return FALSE;
}
-
+
return TRUE;
}
error:
for (j = 0; j < i; j++)
- dbus_free (retval[i]);
+ dbus_free (retval[j]);
dbus_free (retval);
return FALSE;
}
-
-#ifdef DBUS_BUILD_TESTS
+dbus_bool_t
+dbus_activation_systemd_failure (BusActivation *activation,
+ DBusMessage *message)
+{
+ DBusError error;
+ const char *code, *str, *unit = NULL;
+
+ dbus_error_init(&error);
+
+ /* This is called whenever the systemd activator sent us a
+ response. We'll invalidate all pending activations that match the
+ unit name. */
+
+ if (dbus_message_get_args (message, &error,
+ DBUS_TYPE_STRING, &unit,
+ DBUS_TYPE_STRING, &code,
+ DBUS_TYPE_STRING, &str,
+ DBUS_TYPE_INVALID))
+ dbus_set_error(&error, code, str);
+
+
+ if (unit)
+ {
+ DBusHashIter iter;
+
+ bus_context_log (activation->context,
+ DBUS_SYSTEM_LOG_INFO, "Activation via systemd failed for unit '%s': %s",
+ unit,
+ str);
+
+ _dbus_hash_iter_init (activation->pending_activations,
+ &iter);
+
+ while (_dbus_hash_iter_next (&iter))
+ {
+ BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
+
+ if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
+ pending_activation_failed(p, &error);
+ }
+ }
+
+ dbus_error_free(&error);
+
+ return TRUE;
+}
+
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
#include <stdio.h>
static dbus_bool_t
test_create_service_file (DBusString *dir,
- const char *filename,
- const char *name,
+ const char *filename,
+ const char *name,
const char *exec)
{
DBusString file_name, full_path;
ret_val = FALSE;
goto out;
}
-
+
file = fopen (_dbus_string_get_const_data (&full_path), "w");
if (!file)
{
{
DBusString file_name, full_path;
dbus_bool_t ret_val;
-
+
ret_val = TRUE;
-
+
_dbus_string_init_const (&file_name, filename);
if (!_dbus_string_init (&full_path))
DBusDirIter *iter;
DBusString filename, full_path;
dbus_bool_t ret_val;
-
+
ret_val = TRUE;
-
+
if (!_dbus_string_init (&filename))
return FALSE;
_dbus_string_free (&filename);
return FALSE;
}
-
+
iter = _dbus_directory_open (dir, NULL);
if (iter == NULL)
{
ret_val = FALSE;
goto out;
}
-
- while (_dbus_directory_get_next_file (iter, &filename, NULL))
+
+ while (_dbus_directory_get_next_file (iter, &filename, NULL))
{
if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
{
init_service_reload_test (DBusString *dir)
{
DBusStat stat_buf;
-
+
if (!_dbus_stat (dir, &stat_buf, NULL))
{
if (!_dbus_create_directory (dir, NULL))
return FALSE;
}
- else
+ else
{
if (!test_remove_directory (dir))
return FALSE;
return TRUE;
}
-typedef struct
+typedef struct
{
BusActivation *activation;
const char *service_name;
BusActivationEntry *entry;
DBusError error;
dbus_bool_t ret_val;
-
+
ret_val = TRUE;
d = data;
-
+
dbus_error_init (&error);
-
+
entry = activation_find_entry (d->activation, d->service_name, &error);
if (entry == NULL)
{
- if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
+ if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
{
ret_val = TRUE;
}
if (d->expecting_find)
ret_val = FALSE;
}
-
+
dbus_error_free (&error);
}
- else
+ else
{
if (!d->expecting_find)
ret_val = FALSE;
else
err = !check_func (data);
- if (err)
+ if (err)
_dbus_assert_not_reached ("Test failed");
return TRUE;
DBusString address;
DBusList *directories;
CheckData d;
-
+
directories = NULL;
_dbus_string_init_const (&address, "");
-
+
if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
- return FALSE;
+ return FALSE;
activation = bus_activation_new (NULL, &address, &directories, NULL);
if (!activation)
return FALSE;
d.activation = activation;
-
+
/* Check for existing service file */
d.expecting_find = TRUE;
d.service_name = SERVICE_NAME_1;
d.expecting_find = TRUE;
d.service_name = SERVICE_NAME_2;
-
+
if (!do_test ("Added service file", oom_test, &d))
return FALSE;
-
+
/* Check for removed service file */
if (!test_remove_service_file (dir, SERVICE_FILE_2))
return FALSE;
if (!do_test ("Removed service file", oom_test, &d))
return FALSE;
-
+
/* Check for updated service file */
-
+
_dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
d.service_name = SERVICE_NAME_1;
if (!do_test ("Updated service file, part 2", oom_test, &d))
- return FALSE;
+ return FALSE;
bus_activation_unref (activation);
_dbus_list_clear (&directories);
bus_activation_service_reload_test (const DBusString *test_data_dir)
{
DBusString directory;
+ const char *tmp;
if (!_dbus_string_init (&directory))
return FALSE;
-
- if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
+
+ tmp = _dbus_get_tmpdir ();
+
+ if (tmp == NULL)
return FALSE;
-
+
+ if (!_dbus_string_append (&directory, tmp))
+ return FALSE;
+
if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
!_dbus_generate_random_ascii (&directory, 6))
{
return FALSE;
}
-
+
/* Do normal tests */
if (!init_service_reload_test (&directory))
_dbus_assert_not_reached ("could not initiate service reload test");
-
+
if (!do_service_reload_test (&directory, FALSE))
- ; /* Do nothing? */
-
+ {
+ /* Do nothing? */
+ }
+
/* Do OOM tests */
if (!init_service_reload_test (&directory))
_dbus_assert_not_reached ("could not initiate service reload test");
-
+
if (!do_service_reload_test (&directory, TRUE))
- ; /* Do nothing? */
-
+ {
+ /* Do nothing? */
+ }
+
/* Cleanup test directory */
if (!cleanup_service_reload_test (&directory))
return FALSE;
-
+
_dbus_string_free (&directory);
-
+
return TRUE;
}
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */