1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "xdg-autostart-service.h"
9 #include "conf-parser.h"
11 #include "unit-name.h"
12 #include "path-util.h"
14 #include "generator.h"
16 #include "specifier.h"
17 #include "string-util.h"
18 #include "nulstr-util.h"
21 XdgAutostartService
* xdg_autostart_service_free(XdgAutostartService
*s
) {
31 free(s
->working_directory
);
33 strv_free(s
->only_show_in
);
34 strv_free(s
->not_show_in
);
37 free(s
->autostart_condition
);
38 free(s
->kde_autostart_condition
);
40 free(s
->gnome_autostart_phase
);
45 char *xdg_autostart_service_translate_name(const char *name
) {
46 _cleanup_free_
char *c
= NULL
, *escaped
= NULL
;
53 res
= endswith(c
, ".desktop");
57 escaped
= unit_name_escape(c
);
61 return strjoin("app-", escaped
, "@autostart.service");
64 static int xdg_config_parse_bool(
69 unsigned section_line
,
83 if (streq(rvalue
, "true"))
85 else if (streq(rvalue
, "false"))
88 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
), "Invalid value for boolean: %s", rvalue
);
93 /* Unescapes the string in-place, returns non-zero status on error. */
94 static int xdg_unescape_string(
107 for (; *in
; in
++, out
++) {
109 /* Move forward, and ensure it is a valid escape. */
129 /* Technically only permitted for strv. */
133 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
), "Undefined escape sequence \\%c.", *in
);
146 /* Note: We do not bother with unescaping the strings, hence the _raw postfix. */
147 static int xdg_config_parse_string(
149 const char *filename
,
152 unsigned section_line
,
159 _cleanup_free_
char *res
= NULL
;
168 /* XDG does not allow duplicate definitions. */
170 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Key %s was defined multiple times, ignoring.", lvalue
);
174 res
= strdup(rvalue
);
178 r
= xdg_unescape_string(unit
, filename
, line
, res
);
182 *out
= TAKE_PTR(res
);
186 static int strv_strndup_unescape_and_push(
188 const char *filename
,
198 _cleanup_free_
char *copy
= NULL
;
201 copy
= strndup(start
, end
- start
);
205 r
= xdg_unescape_string(unit
, filename
, line
, copy
);
209 if (!GREEDY_REALLOC(*sv
, *n
+ 2)) /* One extra for NULL */
212 (*sv
)[*n
] = TAKE_PTR(copy
);
213 (*sv
)[*n
+ 1] = NULL
;
219 static int xdg_config_parse_strv(
221 const char *filename
,
224 unsigned section_line
,
231 char ***ret_sv
= data
;
239 /* XDG does not allow duplicate definitions. */
241 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Key %s was already defined, ignoring.", lvalue
);
246 _cleanup_strv_free_
char **sv
= NULL
;
248 if (!GREEDY_REALLOC0(sv
, 1))
251 /* We cannot use strv_split because it does not handle escaping correctly. */
252 const char *start
= rvalue
, *end
;
254 for (end
= start
; *end
; end
++) {
256 /* Move forward, and ensure it is a valid escape. */
258 if (!strchr("sntr\\;", *end
)) {
259 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Undefined escape sequence \\%c.", *end
);
266 r
= strv_strndup_unescape_and_push(unit
, filename
, line
,
276 /* Handle the trailing entry after the last separator */
277 r
= strv_strndup_unescape_and_push(unit
, filename
, line
,
283 *ret_sv
= TAKE_PTR(sv
);
287 static int xdg_config_item_table_lookup(
291 ConfigParserCallback
*func
,
298 /* Ignore any keys with [] as those are translations. */
299 if (strchr(lvalue
, '[')) {
306 return config_item_table_lookup(table
, section
, lvalue
, func
, ltype
, data
, userdata
);
309 XdgAutostartService
*xdg_autostart_service_parse_desktop(const char *path
) {
310 _cleanup_(xdg_autostart_service_freep
) XdgAutostartService
*service
= NULL
;
313 service
= new0(XdgAutostartService
, 1);
317 service
->path
= strdup(path
);
321 const ConfigTableItem items
[] = {
322 { "Desktop Entry", "Name", xdg_config_parse_string
, 0, &service
->description
},
323 { "Desktop Entry", "Exec", xdg_config_parse_string
, 0, &service
->exec_string
},
324 { "Desktop Entry", "Path", xdg_config_parse_string
, 0, &service
->working_directory
},
325 { "Desktop Entry", "TryExec", xdg_config_parse_string
, 0, &service
->try_exec
},
326 { "Desktop Entry", "Type", xdg_config_parse_string
, 0, &service
->type
},
327 { "Desktop Entry", "OnlyShowIn", xdg_config_parse_strv
, 0, &service
->only_show_in
},
328 { "Desktop Entry", "NotShowIn", xdg_config_parse_strv
, 0, &service
->not_show_in
},
329 { "Desktop Entry", "Hidden", xdg_config_parse_bool
, 0, &service
->hidden
},
330 { "Desktop Entry", "AutostartCondition", xdg_config_parse_string
, 0, &service
->autostart_condition
},
331 { "Desktop Entry", "X-KDE-autostart-condition", xdg_config_parse_string
, 0, &service
->kde_autostart_condition
},
332 { "Desktop Entry", "X-GNOME-Autostart-Phase", xdg_config_parse_string
, 0, &service
->gnome_autostart_phase
},
333 { "Desktop Entry", "X-systemd-skip", xdg_config_parse_bool
, 0, &service
->systemd_skip
},
335 /* Common entries that we do not use currently. */
336 { "Desktop Entry", "Categories", NULL
, 0, NULL
},
337 { "Desktop Entry", "Comment", NULL
, 0, NULL
},
338 { "Desktop Entry", "DBusActivatable", NULL
, 0, NULL
},
339 { "Desktop Entry", "Encoding", NULL
, 0, NULL
},
340 { "Desktop Entry", "GenericName", NULL
, 0, NULL
},
341 { "Desktop Entry", "Icon", NULL
, 0, NULL
},
342 { "Desktop Entry", "Keywords", NULL
, 0, NULL
},
343 { "Desktop Entry", "MimeType", NULL
, 0, NULL
},
344 { "Desktop Entry", "NoDisplay", NULL
, 0, NULL
},
345 { "Desktop Entry", "StartupNotify", NULL
, 0, NULL
},
346 { "Desktop Entry", "StartupWMClass", NULL
, 0, NULL
},
347 { "Desktop Entry", "Terminal", NULL
, 0, NULL
},
348 { "Desktop Entry", "URL", NULL
, 0, NULL
},
349 { "Desktop Entry", "Version", NULL
, 0, NULL
},
353 r
= config_parse(NULL
, service
->path
, NULL
,
355 xdg_config_item_table_lookup
, items
,
356 CONFIG_PARSE_WARN
, service
,
358 /* If parsing failed, only hide the file so it will still mask others. */
360 log_warning_errno(r
, "Failed to parse %s, ignoring it", service
->path
);
361 service
->hidden
= true;
364 return TAKE_PTR(service
);
367 int xdg_autostart_format_exec_start(
369 char **ret_exec_start
) {
371 _cleanup_strv_free_
char **exec_split
= NULL
;
378 * Unfortunately, there is a mismatch between systemd's idea of $PATH and XDGs. I.e. we need to
379 * ensure that we have an absolute path to support cases where $PATH has been modified from the
382 * Note that this is only needed for development environments though; so while it is important, this
383 * should have no effect in production environments.
385 * To be compliant with the XDG specification, we also need to strip certain parameters and
386 * such. Doing so properly makes parsing the command line unavoidable.
388 * NOTE: Technically, XDG only specifies " as quotes, while this also accepts '.
390 r
= strv_split_full(&exec_split
, exec
, NULL
, EXTRACT_UNQUOTE
| EXTRACT_RELAX
);
394 if (strv_isempty(exec_split
))
395 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
), "Exec line is empty");
398 for (i
= n
= 0; exec_split
[i
]; i
++) {
399 _cleanup_free_
char *c
= NULL
, *raw
= NULL
, *p
= NULL
, *escaped
= NULL
, *quoted
= NULL
;
402 l
= cunescape(exec_split
[i
], 0, &c
);
404 return log_debug_errno(l
, "Failed to unescape '%s': %m", exec_split
[i
]);
407 _cleanup_free_
char *executable
= NULL
;
409 /* This is the executable, find it in $PATH */
411 r
= find_executable(c
, &executable
);
413 return log_info_errno(r
, "Exec binary '%s' does not exist: %m", c
);
415 escaped
= cescape(executable
);
419 free_and_replace(exec_split
[n
++], escaped
);
424 * Remove any standardised XDG fields; we assume they never appear as part of another
425 * argument as that just does not make any sense as they can be empty (GLib will e.g. turn
426 * "%f" into an empty argument). Other implementations may handle this differently.
433 "%i", /* Location of icon, could be implemented. */
434 "%c", /* Translated application name, could be implemented. */
435 "%k", /* Location of desktop file, could be implemented. */
442 * %% -> % and then % -> %% means that we correctly quote any % and also quote any left over
443 * (and invalid) % specifier from the desktop file.
445 raw
= strreplace(c
, "%%", "%");
448 p
= strreplace(raw
, "%", "%%");
451 escaped
= cescape(p
);
455 quoted
= strjoin("\"", escaped
, "\"");
459 free_and_replace(exec_split
[n
++], quoted
);
461 for (; exec_split
[n
]; n
++)
462 exec_split
[n
] = mfree(exec_split
[n
]);
464 res
= strv_join(exec_split
, " ");
468 *ret_exec_start
= res
;
472 static int xdg_autostart_generate_desktop_condition(
474 const char *test_binary
,
475 const char *condition
) {
479 /* Generate an ExecCondition for GNOME autostart condition */
480 if (!isempty(condition
)) {
481 _cleanup_free_
char *gnome_autostart_condition_path
= NULL
, *e_autostart_condition
= NULL
;
483 r
= find_executable(test_binary
, &gnome_autostart_condition_path
);
485 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
486 "%s not found: %m", test_binary
);
487 fprintf(f
, "# ExecCondition using %s skipped due to missing binary.\n", test_binary
);
491 e_autostart_condition
= cescape(condition
);
492 if (!e_autostart_condition
)
496 "ExecCondition=%s --condition \"%s\"\n",
497 gnome_autostart_condition_path
,
498 e_autostart_condition
);
504 int xdg_autostart_service_generate_unit(
505 XdgAutostartService
*service
,
508 _cleanup_free_
char *path_escaped
= NULL
, *exec_start
= NULL
, *unit
= NULL
;
509 _cleanup_fclose_
FILE *f
= NULL
;
514 /* Nothing to do for hidden services. */
515 if (service
->hidden
) {
516 log_debug("Not generating service for XDG autostart %s, it is hidden.", service
->name
);
520 if (service
->systemd_skip
) {
521 log_debug("Not generating service for XDG autostart %s, should be skipped by generator.", service
->name
);
525 /* Nothing to do if type is not Application. */
526 if (!streq_ptr(service
->type
, "Application")) {
527 log_debug("Not generating service for XDG autostart %s, only Type=Application is supported.", service
->name
);
531 if (!service
->exec_string
) {
532 log_warning("Not generating service for XDG autostart %s, it is has no Exec= line.", service
->name
);
536 /* The TryExec key cannot be checked properly from the systemd unit, it is trivial to check using
537 * find_executable though. */
538 if (service
->try_exec
) {
539 r
= find_executable(service
->try_exec
, NULL
);
541 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
542 "Not generating service for XDG autostart %s, could not find TryExec= binary %s: %m",
543 service
->name
, service
->try_exec
);
548 r
= xdg_autostart_format_exec_start(service
->exec_string
, &exec_start
);
551 "Not generating service for XDG autostart %s, error parsing Exec= line: %m",
556 if (service
->gnome_autostart_phase
) {
557 /* There is no explicit value for the "Application" phase. */
558 log_debug("Not generating service for XDG autostart %s, startup phases are not supported.",
563 path_escaped
= specifier_escape(service
->path
);
567 unit
= path_join(dest
, service
->name
);
571 f
= fopen(unit
, "wxe");
573 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
576 "# Automatically generated by systemd-xdg-autostart-generator\n\n"
578 "Documentation=man:systemd-xdg-autostart-generator(8)\n"
580 "PartOf=graphical-session.target\n\n",
583 if (service
->description
) {
584 _cleanup_free_
char *t
= NULL
;
586 t
= specifier_escape(service
->description
);
590 fprintf(f
, "Description=%s\n", t
);
593 /* Only start after the session is ready. */
595 "After=graphical-session.target\n");
606 if (service
->working_directory
) {
607 _cleanup_free_
char *e_working_directory
= NULL
;
609 e_working_directory
= cescape(service
->working_directory
);
610 if (!e_working_directory
)
613 fprintf(f
, "WorkingDirectory=-%s\n", e_working_directory
);
616 /* Generate an ExecCondition to check $XDG_CURRENT_DESKTOP */
617 if (!strv_isempty(service
->only_show_in
) || !strv_isempty(service
->not_show_in
)) {
618 _cleanup_free_
char *only_show_in
= NULL
, *not_show_in
= NULL
, *e_only_show_in
= NULL
, *e_not_show_in
= NULL
;
620 only_show_in
= strv_join(service
->only_show_in
, ":");
621 not_show_in
= strv_join(service
->not_show_in
, ":");
622 if (!only_show_in
|| !not_show_in
)
625 e_only_show_in
= cescape(only_show_in
);
626 e_not_show_in
= cescape(not_show_in
);
627 if (!e_only_show_in
|| !e_not_show_in
)
630 /* Just assume the values are reasonably sane */
632 "ExecCondition=" ROOTLIBEXECDIR
"/systemd-xdg-autostart-condition \"%s\" \"%s\"\n",
637 r
= xdg_autostart_generate_desktop_condition(f
,
638 "gnome-systemd-autostart-condition",
639 service
->autostart_condition
);
643 r
= xdg_autostart_generate_desktop_condition(f
,
644 "kde-systemd-start-condition",
645 service
->kde_autostart_condition
);
649 (void) generator_add_symlink(dest
, "xdg-desktop-autostart.target", "wants", service
->name
);