]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/xdg-autostart-generator/xdg-autostart-condition.c
Merge pull request #22791 from keszybz/bootctl-invert-order
[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
17 if (argc != 3)
18 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
19 "Wrong argument count. Expected the OnlyShowIn= and NotShowIn= sets, each colon separated.");
20
21 xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP");
22 if (xdg_current_desktop) {
23 desktops = strv_split(xdg_current_desktop, ":");
24 if (!desktops)
25 return log_oom();
26 }
27
28 only_show_in = strv_split(argv[1], ":");
29 not_show_in = strv_split(argv[2], ":");
30 if (!only_show_in || !not_show_in)
31 return log_oom();
32
33 /* Each desktop in XDG_CURRENT_DESKTOP needs to be matched in order. */
34 STRV_FOREACH(d, desktops) {
35 if (strv_contains(only_show_in, *d))
36 return 0;
37 if (strv_contains(not_show_in, *d))
38 return 1;
39 }
40
41 /* non-zero exit code when only_show_in has a proper value */
42 return !strv_isempty(only_show_in);
43 }
44
45 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);