xdg-autostart-generator: Add a generator for XDG autostart files

This generator can be used by desktop environments to launch autostart
applications and services. The feature is an opt-in, triggered by
xdg-desktop-autostart.target being activated.

Also included is the new binary xdg-autostart-condition. This binary is
used as an ExecCondition to test the OnlyShowIn and NotShowIn XDG
desktop file keys. These need to be evaluated against the
XDG_CURRENT_DESKTOP environment variable which may not be known at
generation time.

Co-authored-by: Henri Chain <henri.chain@enioka.com>
This commit is contained in:
Benjamin Berg
2020-03-25 16:59:40 +01:00
parent 8746820b87
commit 8feca2472c
9 changed files with 870 additions and 0 deletions

View File

@@ -1150,6 +1150,16 @@
<filename>gnome-session.target</filename>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><filename>xdg-desktop-autostart.target</filename></term>
<listitem>
<para>The XDG specification defines a way to autostart applications using XDG desktop files.
systemd ships a generator for the XDG desktop files in autostart directories.
Desktop Environments can opt-in to use this service by adding a <varname>Wants=</varname>
dependency on <literal>xdg-desktop-autostart.target</literal></para>.
</listitem>
</varlistentry>
</variablelist>
</refsect2>
</refsect1>

View File

@@ -1417,6 +1417,7 @@ foreach term : ['utmp',
'tmpfiles',
'hwdb',
'rfkill',
'xdg-autostart',
'ldconfig',
'efi',
'tpm',
@@ -1520,6 +1521,7 @@ includes = include_directories('src/basic',
'src/libudev',
'src/core',
'src/shutdown',
'src/xdg-autostart-generator',
'src/libsystemd/sd-bus',
'src/libsystemd/sd-device',
'src/libsystemd/sd-event',
@@ -2301,6 +2303,27 @@ if conf.get('HAVE_SYSV_COMPAT') == 1
install_dir : systemgeneratordir)
endif
if conf.get('ENABLE_XDG_AUTOSTART') == 1
executable(
'systemd-xdg-autostart-generator',
'src/xdg-autostart-generator/xdg-autostart-generator.c',
'src/xdg-autostart-generator/xdg-autostart-service.c',
include_directories : includes,
link_with : [libshared],
install_rpath : rootlibexecdir,
install : true,
install_dir : usergeneratordir)
executable(
'systemd-xdg-autostart-condition',
'src/xdg-autostart-generator/xdg-autostart-condition.c',
include_directories : includes,
link_with : [libshared],
install_rpath : rootlibexecdir,
install : true,
install_dir : rootlibexecdir)
endif
if conf.get('ENABLE_HOSTNAMED') == 1
executable(
'systemd-hostnamed',
@@ -3566,6 +3589,7 @@ foreach tuple : [
['randomseed'],
['backlight'],
['rfkill'],
['xdg-autostart'],
['logind'],
['machined'],
['portabled'],

View File

@@ -142,6 +142,8 @@ option('hwdb', type : 'boolean',
description : 'support for the hardware database')
option('rfkill', type : 'boolean',
description : 'support for the rfkill tools')
option('xdg-autostart', type : 'boolean',
description : 'install the xdg-autostart-generator and unit')
option('man', type : 'combo', choices : ['auto', 'true', 'false'],
value : 'false',
description : 'build and install man pages')

View File

@@ -0,0 +1,46 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "main-func.h"
#include "strv.h"
/*
* This binary is intended to be run as an ExecCondition= in units generated
* by the xdg-autostart-generator. It does the appropriate checks against
* XDG_CURRENT_DESKTOP that are too advanced for simple ConditionEnvironment=
* matches.
*/
static int run(int argc, char *argv[]) {
_cleanup_strv_free_ char **only_show_in = NULL, **not_show_in = NULL, **desktops = NULL;
const char *xdg_current_desktop;
char **d;
if (argc != 3)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Wrong argument count. Expected the OnlyShowIn= and NotShowIn= sets, each colon separated.");
xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP");
if (xdg_current_desktop) {
desktops = strv_split(xdg_current_desktop, ":");
if (!desktops)
return log_oom();
}
only_show_in = strv_split(argv[1], ":");
not_show_in = strv_split(argv[2], ":");
if (!only_show_in || !not_show_in)
return log_oom();
/* Each desktop in XDG_CURRENT_DESKTOP needs to be matched in order. */
STRV_FOREACH(d, desktops) {
if (strv_contains(only_show_in, *d))
return 0;
if (strv_contains(not_show_in, *d))
return 1;
}
/* non-zero exit code when only_show_in has a proper value */
return !strv_isempty(only_show_in);
}
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);

View File

@@ -0,0 +1,116 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include "dirent-util.h"
#include "fd-util.h"
#include "generator.h"
#include "hashmap.h"
#include "log.h"
#include "main-func.h"
#include "nulstr-util.h"
#include "path-lookup.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "xdg-autostart-service.h"
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(xdgautostartservice_hash_ops, char, string_hash_func, string_compare_func, XdgAutostartService, xdg_autostart_service_free);
static int enumerate_xdg_autostart(Hashmap *all_services) {
_cleanup_strv_free_ char **autostart_dirs = NULL;
_cleanup_strv_free_ char **config_dirs = NULL;
_unused_ _cleanup_strv_free_ char **data_dirs = NULL;
_cleanup_free_ char *user_config_autostart_dir = NULL;
char **path;
int r;
r = xdg_user_config_dir(&user_config_autostart_dir, "/autostart");
if (r < 0)
return r;
r = strv_extend(&autostart_dirs, user_config_autostart_dir);
if (r < 0)
return r;
r = xdg_user_dirs(&config_dirs, &data_dirs);
if (r < 0)
return r;
r = strv_extend_strv_concat(&autostart_dirs, config_dirs, "/autostart");
if (r < 0)
return r;
STRV_FOREACH(path, autostart_dirs) {
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
d = opendir(*path);
if (!d) {
if (errno != ENOENT)
log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path);
continue;
}
FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) {
_cleanup_free_ char *fpath = NULL, *name = NULL;
_cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL;
struct stat st;
if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name);
continue;
}
if (!S_ISREG(st.st_mode))
continue;
name = xdg_autostart_service_translate_name(de->d_name);
if (!name)
return log_oom();
if (hashmap_contains(all_services, name))
continue;
fpath = path_join(*path, de->d_name);
if (!fpath)
return log_oom();
service = xdg_autostart_service_parse_desktop(fpath);
if (!service)
return log_oom();
service->name = TAKE_PTR(name);
r = hashmap_put(all_services, service->name, service);
if (r < 0)
return log_oom();
TAKE_PTR(service);
}
}
return 0;
}
static int run(const char *dest, const char *dest_early, const char *dest_late) {
_cleanup_(hashmap_freep) Hashmap *all_services = NULL;
XdgAutostartService *service;
Iterator j;
int r;
assert_se(dest_late);
all_services = hashmap_new(&xdgautostartservice_hash_ops);
if (!all_services)
return log_oom();
r = enumerate_xdg_autostart(all_services);
if (r < 0)
return r;
HASHMAP_FOREACH(service, all_services, j)
(void) xdg_autostart_service_generate_unit(service, dest_late);
return 0;
}
DEFINE_MAIN_GENERATOR_FUNCTION(run);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include "macro.h"
typedef struct XdgAutostartService {
char *name;
char *path;
char *description; /* Name in XDG desktop file */
char *type; /* Purely as an assertion check */
char *exec_string;
char **only_show_in;
char **not_show_in;
char *try_exec;
char *autostart_condition; /* This is mostly GNOME specific */
char *kde_autostart_condition;
char *gnome_autostart_phase;
bool hidden;
bool systemd_skip;
} XdgAutostartService;
XdgAutostartService * xdg_autostart_service_free(XdgAutostartService *s);
DEFINE_TRIVIAL_CLEANUP_FUNC(XdgAutostartService*, xdg_autostart_service_free);
char *xdg_autostart_service_translate_name(const char *name);
int xdg_autostart_format_exec_start(const char *exec, char **ret_exec_start);
XdgAutostartService *xdg_autostart_service_parse_desktop(const char *path);
int xdg_autostart_service_generate_unit(XdgAutostartService *service, const char *dest);

View File

@@ -20,6 +20,10 @@ units = [
'timers.target',
]
if conf.get('ENABLE_XDG_AUTOSTART') == 1
units += [ 'xdg-desktop-autostart.target', ]
endif
foreach file : units
install_data(file,
install_dir : userunitdir)

View File

@@ -0,0 +1,14 @@
# SPDX-License-Identifier: LGPL-2.1+
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Startup of XDG autostart applications
Documentation=man:systemd.special(7)
RefuseManualStart=yes
StopWhenUnneeded=yes