aboutsummaryrefslogtreecommitdiffstats
path: root/src/xdg-autostart-generator/xdg-autostart-service.c
diff options
context:
space:
mode:
authorBenjamin Berg <bberg@redhat.com>2020-03-25 16:59:40 +0100
committerBenjamin Berg <bberg@redhat.com>2020-05-27 09:02:10 +0200
commit8feca2472ccb71d49716a37e86db2fc3a485ae4e (patch)
tree8fbc9062edf1a620a4236523abaa9676e1479453 /src/xdg-autostart-generator/xdg-autostart-service.c
parentsysv-generator: Downgrade directory listing fails to warning (diff)
downloadsystemd-8feca2472ccb71d49716a37e86db2fc3a485ae4e.tar.xz
systemd-8feca2472ccb71d49716a37e86db2fc3a485ae4e.zip
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>
Diffstat (limited to 'src/xdg-autostart-generator/xdg-autostart-service.c')
-rw-r--r--src/xdg-autostart-generator/xdg-autostart-service.c618
1 files changed, 618 insertions, 0 deletions
diff --git a/src/xdg-autostart-generator/xdg-autostart-service.c b/src/xdg-autostart-generator/xdg-autostart-service.c
new file mode 100644
index 00000000000..9ff80c5bb04
--- /dev/null
+++ b/src/xdg-autostart-generator/xdg-autostart-service.c
@@ -0,0 +1,618 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "xdg-autostart-service.h"
+
+#include "conf-parser.h"
+#include "escape.h"
+#include "unit-name.h"
+#include "path-util.h"
+#include "fd-util.h"
+#include "generator.h"
+#include "log.h"
+#include "specifier.h"
+#include "string-util.h"
+#include "nulstr-util.h"
+#include "strv.h"
+
+XdgAutostartService* xdg_autostart_service_free(XdgAutostartService *s) {
+ if (!s)
+ return NULL;
+
+ free(s->name);
+ free(s->path);
+ free(s->description);
+
+ free(s->type);
+ free(s->exec_string);
+
+ strv_free(s->only_show_in);
+ strv_free(s->not_show_in);
+
+ free(s->try_exec);
+ free(s->autostart_condition);
+ free(s->kde_autostart_condition);
+
+ free(s->gnome_autostart_phase);
+
+ return mfree(s);
+}
+
+char *xdg_autostart_service_translate_name(const char *name) {
+ _cleanup_free_ char *c = NULL, *escaped = NULL;
+ char *res;
+
+ c = strdup(name);
+ if (!c)
+ return NULL;
+
+ res = endswith(c, ".desktop");
+ if (res)
+ *res = '\0';
+
+ escaped = unit_name_escape(c);
+ if (!escaped)
+ return NULL;
+
+ return strjoin("app-", escaped, "-autostart.service");
+}
+
+static int xdg_config_parse_bool(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ bool *b = data;
+ const char *value;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ if (streq(rvalue, "true"))
+ *b = true;
+ else if (streq(rvalue, "false"))
+ *b = false;
+ else
+ return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Invalid value for boolean: %s", value);
+
+ return 0;
+}
+
+/* Unescapes the string in-place, returns non-zero status on error. */
+static int xdg_unescape_string(
+ const char *unit,
+ const char *filename,
+ int line,
+ char *str) {
+
+ char *in;
+ char *out;
+
+ assert(str);
+
+ in = out = str;
+
+ for (; *in; in++, out++) {
+ if (*in == '\\') {
+ /* Move forward, and ensure it is a valid escape. */
+ in++;
+
+ switch (*in) {
+ case 's':
+ *out = ' ';
+ break;
+ case 'n':
+ *out = '\n';
+ break;
+ case 't':
+ *out = '\t';
+ break;
+ case 'r':
+ *out = '\r';
+ break;
+ case '\\':
+ *out = '\\';
+ break;
+ case ';':
+ /* Technically only permitted for strv. */
+ *out = ';';
+ break;
+ default:
+ return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Undefined escape sequence \\%c.", *in);
+ }
+
+ continue;
+ }
+
+ *out = *in;
+ }
+ *out = '\0';
+
+ return 0;
+}
+
+/* Note: We do not bother with unescaping the strings, hence the _raw postfix. */
+static int xdg_config_parse_string(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ _cleanup_free_ char *res = NULL;
+ char **out = data;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ /* XDG does not allow duplicate definitions. */
+ if (*out) {
+ log_syntax(unit, LOG_ERR, filename, line, 0, "Key %s was defined multiple times, ignoring.", lvalue);
+ return 0;
+ }
+
+ res = strdup(rvalue);
+ if (!res)
+ return log_oom();
+
+ r = xdg_unescape_string(unit, filename, line, res);
+ if (r < 0)
+ return r;
+
+ *out = TAKE_PTR(res);
+ return 0;
+}
+
+static int xdg_config_parse_strv(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ char ***sv = data;
+ const char *start;
+ const char *end;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ /* XDG does not allow duplicate definitions. */
+ if (*sv) {
+ log_syntax(unit, LOG_ERR, filename, line, 0, "Key %s was defined multiple times, ignoring.", lvalue);
+ return 0;
+ }
+
+ *sv = strv_new(NULL);
+ if (!*sv)
+ return log_oom();
+
+ /* We cannot use strv_split because it does not handle escaping correctly. */
+ start = rvalue;
+
+ for (end = start; *end; end++) {
+ if (*end == '\\') {
+ /* Move forward, and ensure it is a valid escape. */
+ end++;
+ if (strchr("sntr\\;", *end) == NULL) {
+ log_syntax(unit, LOG_ERR, filename, line, 0, "Undefined escape sequence \\%c.", *end);
+ return 0;
+ }
+ continue;
+ }
+
+ if (*end == ';') {
+ _cleanup_free_ char *copy = NULL;
+
+ copy = strndup(start, end - start);
+ if (!copy)
+ return log_oom();
+ r = xdg_unescape_string(unit, filename, line, copy);
+ if (r < 0)
+ return r;
+ r = strv_consume(sv, TAKE_PTR(copy));
+ if (r < 0)
+ return log_oom();
+
+ start = end + 1;
+ }
+ }
+
+ /* Any trailing entry should be ignored if it is empty. */
+ if (end > start) {
+ r = strv_extend(sv, start);
+ if (r < 0)
+ return log_oom();
+ }
+
+ return 0;
+}
+
+static int xdg_config_item_table_lookup(
+ const void *table,
+ const char *section,
+ const char *lvalue,
+ ConfigParserCallback *func,
+ int *ltype,
+ void **data,
+ void *userdata) {
+
+ assert(lvalue);
+
+ /* Ignore any keys with [] as those are translations. */
+ if (strchr(lvalue, '[')) {
+ *func = NULL;
+ *ltype = 0;
+ *data = NULL;
+ return 1;
+ }
+
+ return config_item_table_lookup(table, section, lvalue, func, ltype, data, userdata);
+}
+
+XdgAutostartService *xdg_autostart_service_parse_desktop(const char *path) {
+ _cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL;
+ int r;
+
+ service = new0(XdgAutostartService, 1);
+ if (!service)
+ return NULL;
+
+ service->path = strdup(path);
+ if (!service->path)
+ return NULL;
+
+ const ConfigTableItem items[] = {
+ { "Desktop Entry", "Name", xdg_config_parse_string, 0, &service->description},
+ { "Desktop Entry", "Exec", xdg_config_parse_string, 0, &service->exec_string},
+ { "Desktop Entry", "TryExec", xdg_config_parse_string, 0, &service->try_exec},
+ { "Desktop Entry", "Type", xdg_config_parse_string, 0, &service->type},
+ { "Desktop Entry", "OnlyShowIn", xdg_config_parse_strv, 0, &service->only_show_in},
+ { "Desktop Entry", "NotShowIn", xdg_config_parse_strv, 0, &service->not_show_in},
+ { "Desktop Entry", "Hidden", xdg_config_parse_bool, 0, &service->hidden},
+ { "Desktop Entry", "AutostartCondition", xdg_config_parse_string, 0, &service->autostart_condition},
+ { "Desktop Entry", "X-KDE-autostart-condition", xdg_config_parse_string, 0, &service->kde_autostart_condition},
+ { "Desktop Entry", "X-GNOME-Autostart-Phase", xdg_config_parse_string, 0, &service->gnome_autostart_phase},
+ { "Desktop Entry", "X-systemd-skip", xdg_config_parse_bool, 0, &service->systemd_skip},
+
+ /* Common entries that we do not use currently. */
+ { "Desktop Entry", "Categories", NULL, 0, NULL},
+ { "Desktop Entry", "Comment", NULL, 0, NULL},
+ { "Desktop Entry", "Encoding", NULL, 0, NULL},
+ { "Desktop Entry", "GenericName", NULL, 0, NULL},
+ { "Desktop Entry", "Icon", NULL, 0, NULL},
+ { "Desktop Entry", "Keywords", NULL, 0, NULL},
+ { "Desktop Entry", "NoDisplay", NULL, 0, NULL},
+ { "Desktop Entry", "StartupNotify", NULL, 0, NULL},
+ { "Desktop Entry", "Terminal", NULL, 0, NULL},
+ { "Desktop Entry", "Version", NULL, 0, NULL},
+ {}
+ };
+
+ r = config_parse(NULL, service->path, NULL,
+ "Desktop Entry\0",
+ xdg_config_item_table_lookup, items,
+ CONFIG_PARSE_WARN, service);
+ /* If parsing failed, only hide the file so it will still mask others. */
+ if (r < 0) {
+ log_warning_errno(r, "Failed to parse %s, ignoring it", service->path);
+ service->hidden = true;
+ }
+
+ return TAKE_PTR(service);
+}
+
+int xdg_autostart_format_exec_start(
+ const char *exec,
+ char **ret_exec_start) {
+
+ _cleanup_strv_free_ char **exec_split = NULL;
+ char *res;
+ size_t n, i;
+ bool first_arg;
+ int r;
+
+ /*
+ * Unfortunately, there is a mismatch between systemd's idea of $PATH
+ * and XDGs. i.e. we need to ensure that we have an absolute path to
+ * support cases where $PATH has been modified from the default set.
+ *
+ * Note that this is only needed for development environments though;
+ * so while it is important, this should have no effect in production
+ * environments.
+ *
+ * To be compliant with the XDG specification, we also need to strip
+ * certain parameters and such. Doing so properly makes parsing the
+ * command line unavoidable.
+ *
+ * NOTE: Technically, XDG only specifies " as quotes, while this also
+ * accepts '.
+ */
+ exec_split = strv_split_full(exec, WHITESPACE, SPLIT_QUOTES | SPLIT_RELAX);
+ if (!exec_split)
+ return -ENOMEM;
+
+ if (strv_isempty(exec_split))
+ return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Exec line is empty");
+
+ first_arg = true;
+ for (i = n = 0; exec_split[i]; i++) {
+ _cleanup_free_ char *c = NULL, *raw = NULL, *p = NULL, *escaped = NULL, *quoted = NULL;
+
+ r = cunescape(exec_split[i], 0, &c);
+ if (r < 0)
+ return log_debug_errno(r, "Failed to unescape '%s': %m", exec_split[i]);
+
+ if (first_arg) {
+ _cleanup_free_ char *executable = NULL;
+
+ /* This is the executable, find it in $PATH */
+ first_arg = false;
+ r = find_binary(c, &executable);
+ if (r < 0)
+ return log_info_errno(r, "Exec binary '%s' does not exist: %m", c);
+
+ escaped = cescape(executable);
+ if (!escaped)
+ return log_oom();
+
+ free(exec_split[n]);
+ exec_split[n++] = TAKE_PTR(escaped);
+ continue;
+ }
+
+ /*
+ * Remove any standardised XDG fields; we assume they never appear as
+ * part of another argument as that just does not make any sense as
+ * they can be empty (GLib will e.g. turn "%f" into an empty argument).
+ * Other implementations may handle this differently.
+ */
+ if (STR_IN_SET(c,
+ "%f", "%F",
+ "%u", "%U",
+ "%d", "%D",
+ "%n", "%N",
+ "%i", /* Location of icon, could be implemented. */
+ "%c", /* Translated application name, could be implemented. */
+ "%k", /* Location of desktop file, could be implemented. */
+ "%v",
+ "%m"
+ ))
+ continue;
+
+ /*
+ * %% -> % and then % -> %% means that we correctly quote any %
+ * and also quote any left over (and invalid) % specifier from
+ * the desktop file.
+ */
+ raw = strreplace(c, "%%", "%");
+ if (!raw)
+ return log_oom();
+ p = strreplace(raw, "%", "%%");
+ if (!p)
+ return log_oom();
+ escaped = cescape(p);
+ if (!escaped)
+ return log_oom();
+
+ quoted = strjoin("\"", escaped, "\"");
+ if (!quoted)
+ return log_oom();
+
+ free(exec_split[n]);
+ exec_split[n++] = TAKE_PTR(quoted);
+ }
+ for (; exec_split[n]; n++)
+ exec_split[n] = mfree(exec_split[n]);
+
+ res = strv_join(exec_split, " ");
+ if (!res)
+ return log_oom();
+
+ *ret_exec_start = res;
+ return 0;
+}
+
+static int xdg_autostart_generate_desktop_condition(
+ FILE *f,
+ const char *test_binary,
+ const char *condition) {
+
+ int r;
+
+ /* Generate an ExecCondition for GNOME autostart condition */
+ if (!isempty(condition)) {
+ _cleanup_free_ char *gnome_autostart_condition_path = NULL, *e_autostart_condition = NULL;
+
+ r = find_binary(test_binary, &gnome_autostart_condition_path);
+ if (r < 0) {
+ log_full_errno(r == -ENOENT ? LOG_INFO : LOG_WARNING, r,
+ "%s not found: %m", test_binary);
+ fprintf(f, "# ExecCondition using %s skipped due to missing binary.\n", test_binary);
+ return r;
+ }
+
+ e_autostart_condition = cescape(condition);
+ if (!e_autostart_condition)
+ return log_oom();
+
+ fprintf(f,
+ "ExecCondition=%s --condition \"%s\"\n",
+ gnome_autostart_condition_path,
+ e_autostart_condition);
+ }
+
+ return 0;
+}
+
+int xdg_autostart_service_generate_unit(
+ XdgAutostartService *service,
+ const char *dest) {
+
+ _cleanup_free_ char *path_escaped = NULL, *exec_start = NULL, *unit = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ int r;
+
+ assert(service);
+
+ /* Nothing to do for hidden services. */
+ if (service->hidden) {
+ log_info("Not generating service for XDG autostart %s, it is hidden.", service->name);
+ return 0;
+ }
+
+ if (service->systemd_skip) {
+ log_info("Not generating service for XDG autostart %s, should be skipped by generator.", service->name);
+ return 0;
+ }
+
+ /* Nothing to do if type is not Application. */
+ if (!streq_ptr(service->type, "Application")) {
+ log_info("Not generating service for XDG autostart %s, it is hidden.", service->name);
+ return 0;
+ }
+
+ if (!service->exec_string) {
+ log_warning("Not generating service for XDG autostart %s, it is has no Exec= line.", service->name);
+ return 0;
+ }
+
+ /*
+ * The TryExec key cannot be checked properly from the systemd unit,
+ * it is trivial to check using find_binary though.
+ */
+ if (service->try_exec) {
+ r = find_binary(service->try_exec, NULL);
+ if (r < 0) {
+ log_full_errno(r == -ENOENT ? LOG_INFO : LOG_WARNING, r,
+ "Not generating service for XDG autostart %s, could not find TryExec= binary %s: %m",
+ service->name, service->try_exec);
+ return 0;
+ }
+ }
+
+ r = xdg_autostart_format_exec_start(service->exec_string, &exec_start);
+ if (r < 0) {
+ log_warning_errno(r,
+ "Not generating service for XDG autostart %s, error parsing Exec= line: %m",
+ service->name);
+ return 0;
+ }
+
+ if (streq_ptr(service->gnome_autostart_phase, "EarlyInitialization")) {
+ log_info("Not generating service for XDG autostart %s, EarlyInitialization needs to be handled separately.",
+ service->name);
+ return 0;
+ }
+
+ path_escaped = specifier_escape(service->path);
+ if (!path_escaped)
+ return log_oom();
+
+ unit = path_join(dest, service->name);
+ if (!unit)
+ return log_oom();
+
+ f = fopen(unit, "wxe");
+ if (!f)
+ return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
+
+ fprintf(f,
+ "# Automatically generated by systemd-xdg-autostart-generator\n\n"
+ "[Unit]\n"
+ "Documentation=man:systemd-xdg-autostart-generator(8)\n"
+ "SourcePath=%s\n"
+ "PartOf=graphical-session.target\n\n",
+ path_escaped);
+
+ if (service->description) {
+ _cleanup_free_ char *t = NULL;
+
+ t = specifier_escape(service->description);
+ if (!t)
+ return log_oom();
+
+ fprintf(f, "Description=%s\n", t);
+ }
+
+ /* Only start after the session is ready.
+ * XXX: GNOME has an autostart order which we may want to support.
+ * It is not clear how this can be implemented reliably, which
+ * is why it is skipped for now. */
+ fprintf(f,
+ "After=graphical-session.target\n");
+
+ fprintf(f,
+ "\n[Service]\n"
+ "Type=simple\n"
+ "ExecStart=:%s\n"
+ "Restart=no\n"
+ "TimeoutSec=5s\n"
+ "Slice=app.slice\n",
+ exec_start);
+
+ /* Generate an ExecCondition to check $XDG_CURRENT_DESKTOP */
+ if (!strv_isempty(service->only_show_in) || !strv_isempty(service->not_show_in)) {
+ _cleanup_free_ char *only_show_in = NULL, *not_show_in = NULL, *e_only_show_in = NULL, *e_not_show_in = NULL;
+
+ only_show_in = strv_join(service->only_show_in, ":");
+ not_show_in = strv_join(service->not_show_in, ":");
+ if (!only_show_in || !not_show_in)
+ return log_oom();
+
+ e_only_show_in = cescape(only_show_in);
+ e_not_show_in = cescape(not_show_in);
+ if (!e_only_show_in || !e_not_show_in)
+ return log_oom();
+
+ /* Just assume the values are reasonably sane */
+ fprintf(f,
+ "ExecCondition=" ROOTLIBEXECDIR "/systemd-xdg-autostart-condition \"%s\" \"%s\"\n",
+ e_only_show_in,
+ e_not_show_in);
+ }
+
+ r = xdg_autostart_generate_desktop_condition(f,
+ "gnome-systemd-autostart-condition",
+ service->autostart_condition);
+ if (r < 0)
+ return r;
+
+ r = xdg_autostart_generate_desktop_condition(f,
+ "kde-systemd-start-condition",
+ service->kde_autostart_condition);
+ if (r < 0)
+ return r;
+
+ (void) generator_add_symlink(dest, "xdg-desktop-autostart.target", "wants", service->name);
+
+ return 0;
+}