]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/xdg-autostart-generator/xdg-autostart-condition.c
sd-boot+bootctl: invert order of entries w/o sort-key
[thirdparty/systemd.git] / src / xdg-autostart-generator / xdg-autostart-condition.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "main-func.h"
4 #include "strv.h"
5
6 /*
7 * This binary is intended to be run as an ExecCondition= in units generated
8 * by the xdg-autostart-generator. It does the appropriate checks against
9 * XDG_CURRENT_DESKTOP that are too advanced for simple ConditionEnvironment=
10 * matches.
11 */
12
13 static int run(int argc, char *argv[]) {
14 _cleanup_strv_free_ char **only_show_in = NULL, **not_show_in = NULL, **desktops = NULL;
15 const char *xdg_current_desktop;
16 char **d;
17
18 if (argc != 3)
19 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
20 "Wrong argument count. Expected the OnlyShowIn= and NotShowIn= sets, each colon separated.");
21
22 xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP");
23 if (xdg_current_desktop) {
24 desktops = strv_split(xdg_current_desktop, ":");
25 if (!desktops)
26 return log_oom();
27 }
28
29 only_show_in = strv_split(argv[1], ":");
30 not_show_in = strv_split(argv[2], ":");
31 if (!only_show_in || !not_show_in)
32 return log_oom();
33
34 /* Each desktop in XDG_CURRENT_DESKTOP needs to be matched in order. */
35 STRV_FOREACH(d, desktops) {
36 if (strv_contains(only_show_in, *d))
37 return 0;
38 if (strv_contains(not_show_in, *d))
39 return 1;
40 }
41
42 /* non-zero exit code when only_show_in has a proper value */
43 return !strv_isempty(only_show_in);
44 }
45
46 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);