Run each app in a custom systemd user scope (if available)

tingping/wmclass
Alexander Larsson 2015-03-04 14:40:17 +01:00
parent c40eba6e97
commit 86cb39d30e
6 changed files with 160 additions and 2 deletions

1
.gitignore vendored
View File

@ -30,5 +30,6 @@ xdg-app-session-helper
doc/*.1
*~
xdg-app-dbus.[ch]
xdg-app-systemd-dbus.[ch]
xdg-app-resources.c
*.service

View File

@ -31,7 +31,8 @@ libexec_PROGRAMS = \
xdg_app_helper_SOURCES = xdg-app-helper.c
dbus_built_sources = xdg-app-dbus.c xdg-app-dbus.h
BUILT_SOURCES = $(dbus_built_sources)
systemd_dbus_built_sources = xdg-app-systemd-dbus.c xdg-app-systemd-dbus.h
BUILT_SOURCES = $(dbus_built_sources) $(systemd_dbus_built_sources)
$(dbus_built_sources) : Makefile.am xdg-app-dbus-interfaces.xml
$(AM_V_GEN) $(GDBUS_CODEGEN) \
@ -41,6 +42,14 @@ $(dbus_built_sources) : Makefile.am xdg-app-dbus-interfaces.xml
$(srcdir)/xdg-app-dbus-interfaces.xml \
$(NULL)
$(systemd_dbus_built_sources) : Makefile.am org.freedesktop.systemd1.xml
$(AM_V_GEN) $(GDBUS_CODEGEN) \
--interface-prefix org.freedesktop.systemd1. \
--c-namespace Systemd \
--generate-c-code xdg-app-systemd-dbus \
$(srcdir)/org.freedesktop.systemd1.xml \
$(NULL)
resource_files = $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir) --generate-dependencies $(srcdir)/xdg-app.gresource.xml)
xdg-app-resources.h: xdg-app.gresource.xml
@ -92,6 +101,7 @@ xdg_app_SOURCES = \
xdg-app-run.h \
xdg-app-utils.h \
xdg-app-utils.c \
$(systemd_dbus_built_sources) \
$(dbus_built_sources) \
$(NULL)

View File

@ -0,0 +1,19 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.systemd1.Manager">
<method name="StartTransientUnit">
<arg type="s" direction="in" name="name"/>
<arg type="s" direction="in" name="mode"/>
<arg type="a(sv)" direction="in" name="properties"/>
<arg type="a(sa(sv))" direction="in" name="aux"/>
<arg type="o" direction="out" name="job"/>
</method>
<signal name="JobRemoved">
<arg type="u" name="id"/>
<arg type="o" name="job"/>
<arg type="s" name="unit"/>
<arg type="s" name="result"/>
</signal>
</interface>
</node>

View File

@ -117,7 +117,6 @@ add_extension_args (GKeyFile *metakey, const char *full_ref,
return ret;
}
gboolean
xdg_app_builtin_run (int argc, char **argv, GCancellable *cancellable, GError **error)
{
@ -315,6 +314,8 @@ xdg_app_builtin_run (int argc, char **argv, GCancellable *cancellable, GError **
g_setenv ("XDG_CONFIG_HOME", gs_file_get_path_cached (app_id_dir_config), TRUE);
g_setenv ("XDG_CACHE_HOME", gs_file_get_path_cached (app_id_dir_cache), TRUE);
xdg_app_run_in_transient_unit (app);
if (execv (HELPER, (char **)argv_array->pdata) == -1)
{
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "Unable to start app");

View File

@ -3,12 +3,14 @@
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <gio/gio.h>
#include "libgsystem.h"
#include "xdg-app-run.h"
#include "xdg-app-utils.h"
#include "xdg-app-systemd-dbus.h"
gboolean
xdg_app_run_verify_environment_keys (const char **keys,
@ -335,3 +337,126 @@ xdg_app_ensure_data_dir (const char *app_id,
return g_object_ref (dir);
}
struct JobData {
char *job;
GMainLoop *main_loop;
};
static void
job_removed_cb (SystemdManager *manager,
guint32 id,
char *job,
char *unit,
char *result,
struct JobData *data)
{
if (strcmp (job, data->job) == 0)
g_main_loop_quit (data->main_loop);
}
void
xdg_app_run_in_transient_unit (const char *appid)
{
GDBusConnection *conn = NULL;
GError *error = NULL;
char *path = NULL;
char *address = NULL;
char *name = NULL;
char *job = NULL;
SystemdManager *manager = NULL;
GVariantBuilder builder;
GVariant *properties = NULL;
GVariant *aux = NULL;
guint32 pid;
GMainContext *main_context = NULL;
GMainLoop *main_loop = NULL;
struct JobData data;
path = g_strdup_printf ("/run/user/%d/systemd/private", getuid());
if (!g_file_test (path, G_FILE_TEST_EXISTS))
goto out;
main_context = g_main_context_new ();
main_loop = g_main_loop_new (main_context, FALSE);
g_main_context_push_thread_default (main_context);
address = g_strconcat ("unix:path=", path, NULL);
conn = g_dbus_connection_new_for_address_sync (address,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
NULL,
NULL, &error);
if (!conn)
{
g_warning ("Can't connect to systemd: %s\n", error->message);
goto out;
}
manager = systemd_manager_proxy_new_sync (conn,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
NULL,
"/org/freedesktop/systemd1",
NULL, &error);
if (!manager)
{
g_warning ("Can't create manager proxy: %s\n", error->message);
goto out;
}
name = g_strdup_printf ("xdg-app-%s-%d.scope", appid, getpid());
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sv)"));
pid = getpid ();
g_variant_builder_add (&builder, "(sv)",
"PIDs",
g_variant_new_fixed_array (G_VARIANT_TYPE ("u"),
&pid, 1, sizeof (guint32))
);
properties = g_variant_builder_end (&builder);
aux = g_variant_new_array (G_VARIANT_TYPE ("(sa(sv))"), NULL, 0);
if (!systemd_manager_call_start_transient_unit_sync (manager,
name,
"fail",
properties,
aux,
&job,
NULL,
&error))
{
g_warning ("Can't start transient unit: %s\n", error->message);
goto out;
}
data.job = job;
data.main_loop = main_loop;
g_signal_connect (manager,"job-removed", G_CALLBACK (job_removed_cb), &data);
g_main_loop_run (main_loop);
out:
if (main_context)
{
g_main_context_pop_thread_default (main_context);
g_main_context_unref (main_context);
}
if (main_loop)
g_main_loop_unref (main_loop);
if (error)
g_error_free (error);
if (manager)
g_object_unref (manager);
if (conn)
g_object_unref (conn);
g_free (path);
g_free (address);
g_free (job);
g_free (name);
}

View File

@ -1,6 +1,8 @@
#ifndef __XDG_APP_RUN_H__
#define __XDG_APP_RUN_H__
void xdg_app_run_in_transient_unit (const char *app_id);
gboolean xdg_app_run_verify_environment_keys (const char **keys,
GError **error);
void xdg_app_run_add_environment_args (GPtrArray *argv_array,