1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include <sys/utsname.h>
16 #include "alloc-util.h"
17 #include "apparmor-util.h"
18 #include "architecture.h"
19 #include "audit-util.h"
21 #include "cgroup-util.h"
22 #include "condition.h"
23 #include "cpu-set-util.h"
24 #include "efi-loader.h"
27 #include "extract-word.h"
31 #include "glob-util.h"
32 #include "hostname-util.h"
34 #include "limits-util.h"
37 #include "mountpoint-util.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "proc-cmdline.h"
42 #include "process-util.h"
43 #include "selinux-util.h"
44 #include "smack-util.h"
45 #include "stat-util.h"
46 #include "string-table.h"
47 #include "string-util.h"
48 #include "tomoyo-util.h"
49 #include "user-record.h"
50 #include "user-util.h"
54 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
58 assert(type
< _CONDITION_TYPE_MAX
);
61 c
= new(Condition
, 1);
72 c
->parameter
= strdup(parameter
);
80 Condition
* condition_free(Condition
*c
) {
87 Condition
* condition_free_list_type(Condition
*head
, ConditionType type
) {
90 LIST_FOREACH_SAFE(conditions
, c
, n
, head
)
91 if (type
< 0 || c
->type
== type
) {
92 LIST_REMOVE(conditions
, head
, c
);
96 assert(type
>= 0 || !head
);
100 static int condition_test_kernel_command_line(Condition
*c
, char **env
) {
101 _cleanup_free_
char *line
= NULL
;
107 assert(c
->parameter
);
108 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
110 r
= proc_cmdline(&line
);
114 equal
= strchr(c
->parameter
, '=');
117 _cleanup_free_
char *word
= NULL
;
120 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RELAX
);
127 found
= streq(word
, c
->parameter
);
131 f
= startswith(word
, c
->parameter
);
132 found
= f
&& IN_SET(*f
, 0, '=');
143 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
144 * should be listed first. */
145 ORDER_LOWER_OR_EQUAL
,
146 ORDER_GREATER_OR_EQUAL
,
152 _ORDER_INVALID
= -EINVAL
,
155 static OrderOperator
parse_order(const char **s
) {
157 static const char *const prefix
[_ORDER_MAX
] = {
158 [ORDER_LOWER_OR_EQUAL
] = "<=",
159 [ORDER_GREATER_OR_EQUAL
] = ">=",
161 [ORDER_GREATER
] = ">",
163 [ORDER_UNEQUAL
] = "!=",
168 for (i
= 0; i
< _ORDER_MAX
; i
++) {
171 e
= startswith(*s
, prefix
[i
]);
178 return _ORDER_INVALID
;
181 static bool test_order(int k
, OrderOperator p
) {
188 case ORDER_LOWER_OR_EQUAL
:
197 case ORDER_GREATER_OR_EQUAL
:
204 assert_not_reached("unknown order");
209 static int condition_test_kernel_version(Condition
*c
, char **env
) {
216 assert(c
->parameter
);
217 assert(c
->type
== CONDITION_KERNEL_VERSION
);
219 assert_se(uname(&u
) >= 0);
224 _cleanup_free_
char *word
= NULL
;
228 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
);
230 return log_debug_errno(r
, "Failed to parse condition string \"%s\": %m", p
);
235 order
= parse_order(&s
);
237 s
+= strspn(s
, WHITESPACE
);
240 /* For backwards compatibility, allow whitespace between the operator and
241 * value, without quoting, but only in the first expression. */
243 r
= extract_first_word(&p
, &word
, NULL
, 0);
245 return log_debug_errno(r
, "Failed to parse condition string \"%s\": %m", p
);
247 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Unexpected end of expression: %s", p
);
250 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Unexpected end of expression: %s", p
);
253 r
= test_order(strverscmp_improved(u
.release
, s
), order
);
255 /* No prefix? Then treat as glob string */
256 r
= fnmatch(s
, u
.release
, 0) == 0;
267 static int condition_test_osrelease(Condition
*c
, char **env
) {
268 const char *parameter
= c
->parameter
;
272 assert(c
->parameter
);
273 assert(c
->type
== CONDITION_OS_RELEASE
);
276 _cleanup_free_
char *key
= NULL
, *condition
= NULL
, *actual_value
= NULL
;
281 r
= extract_first_word(¶meter
, &condition
, NULL
, EXTRACT_UNQUOTE
);
283 return log_debug_errno(r
, "Failed to parse parameter: %m");
287 /* parse_order() needs the string to start with the comparators */
289 r
= extract_first_word(&word
, &key
, "!<=>", EXTRACT_RETAIN_SEPARATORS
);
291 return log_debug_errno(r
, "Failed to parse parameter: %m");
292 /* The os-release spec mandates env-var-like key names */
293 if (r
== 0 || isempty(word
) || !env_name_is_valid(key
))
294 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
295 "Failed to parse parameter, key/value format expected: %m");
297 /* Do not allow whitespace after the separator, as that's not a valid os-release format */
298 order
= parse_order(&word
);
299 if (order
< 0 || isempty(word
) || strchr(WHITESPACE
, *word
) != NULL
)
300 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
301 "Failed to parse parameter, key/value format expected: %m");
303 r
= parse_os_release(NULL
, key
, &actual_value
);
305 return log_debug_errno(r
, "Failed to parse os-release: %m");
307 /* Might not be comparing versions, so do exact string matching */
308 if (order
== ORDER_EQUAL
)
309 matches
= streq_ptr(actual_value
, word
);
310 else if (order
== ORDER_UNEQUAL
)
311 matches
= !streq_ptr(actual_value
, word
);
313 matches
= test_order(strverscmp_improved(actual_value
, word
), order
);
322 static int condition_test_memory(Condition
*c
, char **env
) {
329 assert(c
->parameter
);
330 assert(c
->type
== CONDITION_MEMORY
);
332 m
= physical_memory();
335 order
= parse_order(&p
);
337 order
= ORDER_GREATER_OR_EQUAL
; /* default to >= check, if nothing is specified. */
339 r
= safe_atou64(p
, &k
);
341 return log_debug_errno(r
, "Failed to parse size: %m");
343 return test_order(CMP(m
, k
), order
);
346 static int condition_test_cpus(Condition
*c
, char **env
) {
353 assert(c
->parameter
);
354 assert(c
->type
== CONDITION_CPUS
);
356 n
= cpus_in_affinity_mask();
358 return log_debug_errno(n
, "Failed to determine CPUs in affinity mask: %m");
361 order
= parse_order(&p
);
363 order
= ORDER_GREATER_OR_EQUAL
; /* default to >= check, if nothing is specified. */
365 r
= safe_atou(p
, &k
);
367 return log_debug_errno(r
, "Failed to parse number of CPUs: %m");
369 return test_order(CMP((unsigned) n
, k
), order
);
372 static int condition_test_user(Condition
*c
, char **env
) {
375 _cleanup_free_
char *username
= NULL
;
379 assert(c
->parameter
);
380 assert(c
->type
== CONDITION_USER
);
382 r
= parse_uid(c
->parameter
, &id
);
384 return id
== getuid() || id
== geteuid();
386 if (streq("@system", c
->parameter
))
387 return uid_is_system(getuid()) || uid_is_system(geteuid());
389 username
= getusername_malloc();
393 if (streq(username
, c
->parameter
))
396 if (getpid_cached() == 1)
397 return streq(c
->parameter
, "root");
400 r
= get_user_creds(&u
, &id
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
404 return id
== getuid() || id
== geteuid();
407 static int condition_test_control_group_controller(Condition
*c
, char **env
) {
409 CGroupMask system_mask
, wanted_mask
= 0;
412 assert(c
->parameter
);
413 assert(c
->type
== CONDITION_CONTROL_GROUP_CONTROLLER
);
415 if (streq(c
->parameter
, "v2"))
416 return cg_all_unified();
417 if (streq(c
->parameter
, "v1")) {
418 r
= cg_all_unified();
424 r
= cg_mask_supported(&system_mask
);
426 return log_debug_errno(r
, "Failed to determine supported controllers: %m");
428 r
= cg_mask_from_string(c
->parameter
, &wanted_mask
);
429 if (r
< 0 || wanted_mask
<= 0) {
430 /* This won't catch the case that we have an unknown controller
431 * mixed in with valid ones -- these are only assessed on the
432 * validity of the valid controllers found. */
433 log_debug("Failed to parse cgroup string: %s", c
->parameter
);
437 return FLAGS_SET(system_mask
, wanted_mask
);
440 static int condition_test_group(Condition
*c
, char **env
) {
445 assert(c
->parameter
);
446 assert(c
->type
== CONDITION_GROUP
);
448 r
= parse_gid(c
->parameter
, &id
);
452 /* Avoid any NSS lookups if we are PID1 */
453 if (getpid_cached() == 1)
454 return streq(c
->parameter
, "root");
456 return in_group(c
->parameter
) > 0;
459 static int condition_test_virtualization(Condition
*c
, char **env
) {
463 assert(c
->parameter
);
464 assert(c
->type
== CONDITION_VIRTUALIZATION
);
466 if (streq(c
->parameter
, "private-users"))
467 return running_in_userns();
469 v
= detect_virtualization();
473 /* First, compare with yes/no */
474 b
= parse_boolean(c
->parameter
);
478 /* Then, compare categorization */
479 if (streq(c
->parameter
, "vm"))
480 return VIRTUALIZATION_IS_VM(v
);
482 if (streq(c
->parameter
, "container"))
483 return VIRTUALIZATION_IS_CONTAINER(v
);
485 /* Finally compare id */
486 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
489 static int condition_test_architecture(Condition
*c
, char **env
) {
493 assert(c
->parameter
);
494 assert(c
->type
== CONDITION_ARCHITECTURE
);
496 a
= uname_architecture();
500 if (streq(c
->parameter
, "native"))
501 b
= native_architecture();
503 b
= architecture_from_string(c
->parameter
);
504 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
511 #define DTCOMPAT_FILE "/sys/firmware/devicetree/base/compatible"
512 static int condition_test_firmware_devicetree_compatible(const char *dtcarg
) {
514 _cleanup_free_
char *dtcompat
= NULL
;
515 _cleanup_strv_free_
char **dtcompatlist
= NULL
;
518 r
= read_full_virtual_file(DTCOMPAT_FILE
, &dtcompat
, &size
);
520 /* if the path doesn't exist it is incompatible */
522 log_debug_errno(r
, "Failed to open() '%s', assuming machine is incompatible: %m", DTCOMPAT_FILE
);
526 /* Not sure this can happen, but play safe. */
528 log_debug("%s has zero length, assuming machine is incompatible", DTCOMPAT_FILE
);
533 * /sys/firmware/devicetree/base/compatible consists of one or more
534 * strings, each ending in '\0'. So the last character in dtcompat must
537 if (dtcompat
[size
- 1] != '\0') {
538 log_debug("%s is in an unknown format, assuming machine is incompatible", DTCOMPAT_FILE
);
542 dtcompatlist
= strv_parse_nulstr(dtcompat
, size
);
546 return strv_contains(dtcompatlist
, dtcarg
);
549 static int condition_test_firmware(Condition
*c
, char **env
) {
553 assert(c
->parameter
);
554 assert(c
->type
== CONDITION_FIRMWARE
);
556 if (streq(c
->parameter
, "device-tree")) {
557 if (access("/sys/firmware/device-tree/", F_OK
) < 0) {
559 log_debug_errno(errno
, "Unexpected error when checking for /sys/firmware/device-tree/: %m");
563 } else if ((dtc
= startswith(c
->parameter
, "device-tree-compatible("))) {
564 _cleanup_free_
char *dtcarg
= NULL
;
567 end
= strchr(dtc
, ')');
568 if (!end
|| *(end
+ 1) != '\0') {
569 log_debug("Malformed Firmware condition \"%s\"", c
->parameter
);
573 dtcarg
= strndup(dtc
, end
- dtc
);
577 return condition_test_firmware_devicetree_compatible(dtcarg
);
578 } else if (streq(c
->parameter
, "uefi"))
579 return is_efi_boot();
581 log_debug("Unsupported Firmware condition \"%s\"", c
->parameter
);
586 static int condition_test_host(Condition
*c
, char **env
) {
587 _cleanup_free_
char *h
= NULL
;
592 assert(c
->parameter
);
593 assert(c
->type
== CONDITION_HOST
);
595 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
597 r
= sd_id128_get_machine(&y
);
601 return sd_id128_equal(x
, y
);
604 h
= gethostname_malloc();
608 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
611 static int condition_test_ac_power(Condition
*c
, char **env
) {
615 assert(c
->parameter
);
616 assert(c
->type
== CONDITION_AC_POWER
);
618 r
= parse_boolean(c
->parameter
);
622 return (on_ac_power() != 0) == !!r
;
625 static int has_tpm2(void) {
628 /* Checks whether the system has at least one TPM2 resource manager device, i.e. at least one "tpmrm"
631 r
= dir_is_empty("/sys/class/tpmrm");
633 return true; /* nice! we have a device */
635 /* Hmm, so Linux doesn't know of the TPM2 device (or we couldn't check for it), most likely because
636 * the driver wasn't loaded yet. Let's see if the firmware knows about a TPM2 device, in this
637 * case. This way we can answer the TPM2 question already during early boot (where we most likely
642 /* OK, this didn't work either, in this case propagate the original errors */
646 return log_debug_errno(r
, "Failed to determine whether system has TPM2 support: %m");
651 static int condition_test_security(Condition
*c
, char **env
) {
653 assert(c
->parameter
);
654 assert(c
->type
== CONDITION_SECURITY
);
656 if (streq(c
->parameter
, "selinux"))
657 return mac_selinux_use();
658 if (streq(c
->parameter
, "smack"))
659 return mac_smack_use();
660 if (streq(c
->parameter
, "apparmor"))
661 return mac_apparmor_use();
662 if (streq(c
->parameter
, "audit"))
664 if (streq(c
->parameter
, "ima"))
666 if (streq(c
->parameter
, "tomoyo"))
667 return mac_tomoyo_use();
668 if (streq(c
->parameter
, "uefi-secureboot"))
669 return is_efi_secure_boot();
670 if (streq(c
->parameter
, "tpm2"))
676 static int condition_test_capability(Condition
*c
, char **env
) {
677 unsigned long long capabilities
= (unsigned long long) -1;
678 _cleanup_fclose_
FILE *f
= NULL
;
682 assert(c
->parameter
);
683 assert(c
->type
== CONDITION_CAPABILITY
);
685 /* If it's an invalid capability, we don't have it */
686 value
= capability_from_name(c
->parameter
);
690 /* If it's a valid capability we default to assume
693 f
= fopen("/proc/self/status", "re");
698 _cleanup_free_
char *line
= NULL
;
701 r
= read_line(f
, LONG_LINE_MAX
, &line
);
707 p
= startswith(line
, "CapBnd:");
709 if (sscanf(line
+7, "%llx", &capabilities
) != 1)
716 return !!(capabilities
& (1ULL << value
));
719 static int condition_test_needs_update(Condition
*c
, char **env
) {
720 struct stat usr
, other
;
726 assert(c
->parameter
);
727 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
729 r
= proc_cmdline_get_bool("systemd.condition-needs-update", &b
);
731 log_debug_errno(r
, "Failed to parse systemd.condition-needs-update= kernel command line argument, ignoring: %m");
736 log_debug("We are in an initrd, not doing any updates.");
740 if (!path_is_absolute(c
->parameter
)) {
741 log_debug("Specified condition parameter '%s' is not absolute, assuming an update is needed.", c
->parameter
);
745 /* If the file system is read-only we shouldn't suggest an update */
746 r
= path_is_read_only_fs(c
->parameter
);
748 log_debug_errno(r
, "Failed to determine if '%s' is read-only, ignoring: %m", c
->parameter
);
752 /* Any other failure means we should allow the condition to be true, so that we rather invoke too
753 * many update tools than too few. */
755 p
= strjoina(c
->parameter
, "/.updated");
756 if (lstat(p
, &other
) < 0) {
758 log_debug_errno(errno
, "Failed to stat() '%s', assuming an update is needed: %m", p
);
762 if (lstat("/usr/", &usr
) < 0) {
763 log_debug_errno(errno
, "Failed to stat() /usr/, assuming an update is needed: %m");
768 * First, compare seconds as they are always accurate...
770 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
771 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
774 * ...then compare nanoseconds.
776 * A false positive is only possible when /usr's nanoseconds > 0
777 * (otherwise /usr cannot be strictly newer than the target file)
778 * AND the target file's nanoseconds == 0
779 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
781 if (usr
.st_mtim
.tv_nsec
== 0 || other
.st_mtim
.tv_nsec
> 0)
782 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
784 _cleanup_free_
char *timestamp_str
= NULL
;
785 r
= parse_env_file(NULL
, p
, "TIMESTAMP_NSEC", ×tamp_str
);
787 log_debug_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
790 log_debug("No data in timestamp file '%s', using mtime.", p
);
795 r
= safe_atou64(timestamp_str
, ×tamp
);
797 log_debug_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
801 return timespec_load_nsec(&usr
.st_mtim
) > timestamp
;
804 static int condition_test_first_boot(Condition
*c
, char **env
) {
809 assert(c
->parameter
);
810 assert(c
->type
== CONDITION_FIRST_BOOT
);
812 r
= proc_cmdline_get_bool("systemd.condition-first-boot", &b
);
814 log_debug_errno(r
, "Failed to parse systemd.condition-first-boot= kernel command line argument, ignoring: %m");
818 r
= parse_boolean(c
->parameter
);
822 q
= access("/run/systemd/first-boot", F_OK
);
823 if (q
< 0 && errno
!= ENOENT
)
824 log_debug_errno(errno
, "Failed to check if /run/systemd/first-boot exists, ignoring: %m");
826 return (q
>= 0) == !!r
;
829 static int condition_test_environment(Condition
*c
, char **env
) {
834 assert(c
->parameter
);
835 assert(c
->type
== CONDITION_ENVIRONMENT
);
837 equal
= strchr(c
->parameter
, '=');
839 STRV_FOREACH(i
, env
) {
843 found
= streq(c
->parameter
, *i
);
847 f
= startswith(*i
, c
->parameter
);
848 found
= f
&& IN_SET(*f
, 0, '=');
858 static int condition_test_path_exists(Condition
*c
, char **env
) {
860 assert(c
->parameter
);
861 assert(c
->type
== CONDITION_PATH_EXISTS
);
863 return access(c
->parameter
, F_OK
) >= 0;
866 static int condition_test_path_exists_glob(Condition
*c
, char **env
) {
868 assert(c
->parameter
);
869 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
871 return glob_exists(c
->parameter
) > 0;
874 static int condition_test_path_is_directory(Condition
*c
, char **env
) {
876 assert(c
->parameter
);
877 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
879 return is_dir(c
->parameter
, true) > 0;
882 static int condition_test_path_is_symbolic_link(Condition
*c
, char **env
) {
884 assert(c
->parameter
);
885 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
887 return is_symlink(c
->parameter
) > 0;
890 static int condition_test_path_is_mount_point(Condition
*c
, char **env
) {
892 assert(c
->parameter
);
893 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
895 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
898 static int condition_test_path_is_read_write(Condition
*c
, char **env
) {
900 assert(c
->parameter
);
901 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
903 return path_is_read_only_fs(c
->parameter
) <= 0;
906 static int condition_test_cpufeature(Condition
*c
, char **env
) {
908 assert(c
->parameter
);
909 assert(c
->type
== CONDITION_CPU_FEATURE
);
911 return has_cpu_with_flag(ascii_strlower(c
->parameter
));
914 static int condition_test_path_is_encrypted(Condition
*c
, char **env
) {
918 assert(c
->parameter
);
919 assert(c
->type
== CONDITION_PATH_IS_ENCRYPTED
);
921 r
= path_is_encrypted(c
->parameter
);
922 if (r
< 0 && r
!= -ENOENT
)
923 log_debug_errno(r
, "Failed to determine if '%s' is encrypted: %m", c
->parameter
);
928 static int condition_test_directory_not_empty(Condition
*c
, char **env
) {
932 assert(c
->parameter
);
933 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
935 r
= dir_is_empty(c
->parameter
);
936 return r
<= 0 && r
!= -ENOENT
;
939 static int condition_test_file_not_empty(Condition
*c
, char **env
) {
943 assert(c
->parameter
);
944 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
946 return (stat(c
->parameter
, &st
) >= 0 &&
947 S_ISREG(st
.st_mode
) &&
951 static int condition_test_file_is_executable(Condition
*c
, char **env
) {
955 assert(c
->parameter
);
956 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
958 return (stat(c
->parameter
, &st
) >= 0 &&
959 S_ISREG(st
.st_mode
) &&
960 (st
.st_mode
& 0111));
963 int condition_test(Condition
*c
, char **env
) {
965 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
, char **env
) = {
966 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
967 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
968 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
969 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
970 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
971 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
972 [CONDITION_PATH_IS_ENCRYPTED
] = condition_test_path_is_encrypted
,
973 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
974 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
975 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
976 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
977 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
978 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
979 [CONDITION_SECURITY
] = condition_test_security
,
980 [CONDITION_CAPABILITY
] = condition_test_capability
,
981 [CONDITION_HOST
] = condition_test_host
,
982 [CONDITION_AC_POWER
] = condition_test_ac_power
,
983 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
984 [CONDITION_FIRMWARE
] = condition_test_firmware
,
985 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
986 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
987 [CONDITION_USER
] = condition_test_user
,
988 [CONDITION_GROUP
] = condition_test_group
,
989 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
990 [CONDITION_CPUS
] = condition_test_cpus
,
991 [CONDITION_MEMORY
] = condition_test_memory
,
992 [CONDITION_ENVIRONMENT
] = condition_test_environment
,
993 [CONDITION_CPU_FEATURE
] = condition_test_cpufeature
,
994 [CONDITION_OS_RELEASE
] = condition_test_osrelease
,
1000 assert(c
->type
>= 0);
1001 assert(c
->type
< _CONDITION_TYPE_MAX
);
1003 r
= condition_tests
[c
->type
](c
, env
);
1005 c
->result
= CONDITION_ERROR
;
1009 b
= (r
> 0) == !c
->negate
;
1010 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
1014 bool condition_test_list(
1017 condition_to_string_t to_string
,
1018 condition_test_logger_t logger
,
1024 assert(!!logger
== !!to_string
);
1026 /* If the condition list is empty, then it is true */
1030 /* Otherwise, if all of the non-trigger conditions apply and
1031 * if any of the trigger conditions apply (unless there are
1032 * none) we return true */
1033 LIST_FOREACH(conditions
, c
, first
) {
1036 r
= condition_test(c
, env
);
1040 logger(userdata
, LOG_WARNING
, r
, PROJECT_FILE
, __LINE__
, __func__
,
1041 "Couldn't determine result for %s=%s%s%s, assuming failed: %m",
1043 c
->trigger
? "|" : "",
1044 c
->negate
? "!" : "",
1047 logger(userdata
, LOG_DEBUG
, 0, PROJECT_FILE
, __LINE__
, __func__
,
1050 c
->trigger
? "|" : "",
1051 c
->negate
? "!" : "",
1053 condition_result_to_string(c
->result
));
1056 if (!c
->trigger
&& r
<= 0)
1059 if (c
->trigger
&& triggered
<= 0)
1063 return triggered
!= 0;
1066 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, condition_to_string_t to_string
) {
1071 prefix
= strempty(prefix
);
1074 "%s\t%s: %s%s%s %s\n",
1077 c
->trigger
? "|" : "",
1078 c
->negate
? "!" : "",
1080 condition_result_to_string(c
->result
));
1083 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, condition_to_string_t to_string
) {
1086 LIST_FOREACH(conditions
, c
, first
)
1087 condition_dump(c
, f
, prefix
, to_string
);
1090 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
1091 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
1092 [CONDITION_FIRMWARE
] = "ConditionFirmware",
1093 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
1094 [CONDITION_HOST
] = "ConditionHost",
1095 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
1096 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
1097 [CONDITION_SECURITY
] = "ConditionSecurity",
1098 [CONDITION_CAPABILITY
] = "ConditionCapability",
1099 [CONDITION_AC_POWER
] = "ConditionACPower",
1100 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
1101 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
1102 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
1103 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
1104 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
1105 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
1106 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
1107 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
1108 [CONDITION_PATH_IS_ENCRYPTED
] = "ConditionPathIsEncrypted",
1109 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
1110 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
1111 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
1112 [CONDITION_USER
] = "ConditionUser",
1113 [CONDITION_GROUP
] = "ConditionGroup",
1114 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
1115 [CONDITION_CPUS
] = "ConditionCPUs",
1116 [CONDITION_MEMORY
] = "ConditionMemory",
1117 [CONDITION_ENVIRONMENT
] = "ConditionEnvironment",
1118 [CONDITION_CPU_FEATURE
] = "ConditionCPUFeature",
1119 [CONDITION_OS_RELEASE
] = "ConditionOSRelease",
1122 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
1124 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
1125 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
1126 [CONDITION_FIRMWARE
] = "AssertFirmware",
1127 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
1128 [CONDITION_HOST
] = "AssertHost",
1129 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
1130 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
1131 [CONDITION_SECURITY
] = "AssertSecurity",
1132 [CONDITION_CAPABILITY
] = "AssertCapability",
1133 [CONDITION_AC_POWER
] = "AssertACPower",
1134 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
1135 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
1136 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
1137 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
1138 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
1139 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
1140 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
1141 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
1142 [CONDITION_PATH_IS_ENCRYPTED
] = "AssertPathIsEncrypted",
1143 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
1144 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
1145 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
1146 [CONDITION_USER
] = "AssertUser",
1147 [CONDITION_GROUP
] = "AssertGroup",
1148 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
1149 [CONDITION_CPUS
] = "AssertCPUs",
1150 [CONDITION_MEMORY
] = "AssertMemory",
1151 [CONDITION_ENVIRONMENT
] = "AssertEnvironment",
1152 [CONDITION_CPU_FEATURE
] = "AssertCPUFeature",
1153 [CONDITION_OS_RELEASE
] = "AssertOSRelease",
1156 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
1158 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
1159 [CONDITION_UNTESTED
] = "untested",
1160 [CONDITION_SUCCEEDED
] = "succeeded",
1161 [CONDITION_FAILED
] = "failed",
1162 [CONDITION_ERROR
] = "error",
1165 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);