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"
20 #include "blockdev-util.h"
22 #include "cgroup-util.h"
23 #include "condition.h"
24 #include "cpu-set-util.h"
25 #include "efi-loader.h"
28 #include "extract-word.h"
32 #include "glob-util.h"
33 #include "hostname-util.h"
35 #include "limits-util.h"
38 #include "mountpoint-util.h"
40 #include "parse-util.h"
41 #include "path-util.h"
42 #include "proc-cmdline.h"
43 #include "process-util.h"
44 #include "selinux-util.h"
45 #include "smack-util.h"
46 #include "stat-util.h"
47 #include "string-table.h"
48 #include "string-util.h"
49 #include "tomoyo-util.h"
50 #include "uid-alloc-range.h"
51 #include "user-util.h"
55 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
59 assert(type
< _CONDITION_TYPE_MAX
);
62 c
= new(Condition
, 1);
73 c
->parameter
= strdup(parameter
);
81 Condition
* condition_free(Condition
*c
) {
88 Condition
* condition_free_list_type(Condition
*head
, ConditionType type
) {
91 LIST_FOREACH_SAFE(conditions
, c
, n
, head
)
92 if (type
< 0 || c
->type
== type
) {
93 LIST_REMOVE(conditions
, head
, c
);
97 assert(type
>= 0 || !head
);
101 static int condition_test_kernel_command_line(Condition
*c
, char **env
) {
102 _cleanup_free_
char *line
= NULL
;
108 assert(c
->parameter
);
109 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
111 r
= proc_cmdline(&line
);
115 equal
= strchr(c
->parameter
, '=');
118 _cleanup_free_
char *word
= NULL
;
121 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RELAX
);
128 found
= streq(word
, c
->parameter
);
132 f
= startswith(word
, c
->parameter
);
133 found
= f
&& IN_SET(*f
, 0, '=');
144 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
145 * should be listed first. */
146 ORDER_LOWER_OR_EQUAL
,
147 ORDER_GREATER_OR_EQUAL
,
153 _ORDER_INVALID
= -EINVAL
,
156 static OrderOperator
parse_order(const char **s
) {
158 static const char *const prefix
[_ORDER_MAX
] = {
159 [ORDER_LOWER_OR_EQUAL
] = "<=",
160 [ORDER_GREATER_OR_EQUAL
] = ">=",
162 [ORDER_GREATER
] = ">",
164 [ORDER_UNEQUAL
] = "!=",
169 for (i
= 0; i
< _ORDER_MAX
; i
++) {
172 e
= startswith(*s
, prefix
[i
]);
179 return _ORDER_INVALID
;
182 static bool test_order(int k
, OrderOperator p
) {
189 case ORDER_LOWER_OR_EQUAL
:
198 case ORDER_GREATER_OR_EQUAL
:
205 assert_not_reached();
210 static int condition_test_kernel_version(Condition
*c
, char **env
) {
217 assert(c
->parameter
);
218 assert(c
->type
== CONDITION_KERNEL_VERSION
);
220 assert_se(uname(&u
) >= 0);
225 _cleanup_free_
char *word
= NULL
;
229 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
);
231 return log_debug_errno(r
, "Failed to parse condition string \"%s\": %m", p
);
236 order
= parse_order(&s
);
238 s
+= strspn(s
, WHITESPACE
);
241 /* For backwards compatibility, allow whitespace between the operator and
242 * value, without quoting, but only in the first expression. */
244 r
= extract_first_word(&p
, &word
, NULL
, 0);
246 return log_debug_errno(r
, "Failed to parse condition string \"%s\": %m", p
);
248 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Unexpected end of expression: %s", p
);
251 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Unexpected end of expression: %s", p
);
254 r
= test_order(strverscmp_improved(u
.release
, s
), order
);
256 /* No prefix? Then treat as glob string */
257 r
= fnmatch(s
, u
.release
, 0) == 0;
268 static int condition_test_osrelease(Condition
*c
, char **env
) {
269 const char *parameter
= c
->parameter
;
273 assert(c
->parameter
);
274 assert(c
->type
== CONDITION_OS_RELEASE
);
277 _cleanup_free_
char *key
= NULL
, *condition
= NULL
, *actual_value
= NULL
;
282 r
= extract_first_word(¶meter
, &condition
, NULL
, EXTRACT_UNQUOTE
);
284 return log_debug_errno(r
, "Failed to parse parameter: %m");
288 /* parse_order() needs the string to start with the comparators */
290 r
= extract_first_word(&word
, &key
, "!<=>", EXTRACT_RETAIN_SEPARATORS
);
292 return log_debug_errno(r
, "Failed to parse parameter: %m");
293 /* The os-release spec mandates env-var-like key names */
294 if (r
== 0 || isempty(word
) || !env_name_is_valid(key
))
295 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
296 "Failed to parse parameter, key/value format expected: %m");
298 /* Do not allow whitespace after the separator, as that's not a valid os-release format */
299 order
= parse_order(&word
);
300 if (order
< 0 || isempty(word
) || strchr(WHITESPACE
, *word
) != NULL
)
301 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
302 "Failed to parse parameter, key/value format expected: %m");
304 r
= parse_os_release(NULL
, key
, &actual_value
);
306 return log_debug_errno(r
, "Failed to parse os-release: %m");
308 /* Might not be comparing versions, so do exact string matching */
309 if (order
== ORDER_EQUAL
)
310 matches
= streq_ptr(actual_value
, word
);
311 else if (order
== ORDER_UNEQUAL
)
312 matches
= !streq_ptr(actual_value
, word
);
314 matches
= test_order(strverscmp_improved(actual_value
, word
), order
);
323 static int condition_test_memory(Condition
*c
, char **env
) {
330 assert(c
->parameter
);
331 assert(c
->type
== CONDITION_MEMORY
);
333 m
= physical_memory();
336 order
= parse_order(&p
);
338 order
= ORDER_GREATER_OR_EQUAL
; /* default to >= check, if nothing is specified. */
340 r
= safe_atou64(p
, &k
);
342 return log_debug_errno(r
, "Failed to parse size: %m");
344 return test_order(CMP(m
, k
), order
);
347 static int condition_test_cpus(Condition
*c
, char **env
) {
354 assert(c
->parameter
);
355 assert(c
->type
== CONDITION_CPUS
);
357 n
= cpus_in_affinity_mask();
359 return log_debug_errno(n
, "Failed to determine CPUs in affinity mask: %m");
362 order
= parse_order(&p
);
364 order
= ORDER_GREATER_OR_EQUAL
; /* default to >= check, if nothing is specified. */
366 r
= safe_atou(p
, &k
);
368 return log_debug_errno(r
, "Failed to parse number of CPUs: %m");
370 return test_order(CMP((unsigned) n
, k
), order
);
373 static int condition_test_user(Condition
*c
, char **env
) {
376 _cleanup_free_
char *username
= NULL
;
380 assert(c
->parameter
);
381 assert(c
->type
== CONDITION_USER
);
383 r
= parse_uid(c
->parameter
, &id
);
385 return id
== getuid() || id
== geteuid();
387 if (streq("@system", c
->parameter
))
388 return uid_is_system(getuid()) || uid_is_system(geteuid());
390 username
= getusername_malloc();
394 if (streq(username
, c
->parameter
))
397 if (getpid_cached() == 1)
398 return streq(c
->parameter
, "root");
401 r
= get_user_creds(&u
, &id
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
405 return id
== getuid() || id
== geteuid();
408 static int condition_test_control_group_controller(Condition
*c
, char **env
) {
410 CGroupMask system_mask
, wanted_mask
= 0;
413 assert(c
->parameter
);
414 assert(c
->type
== CONDITION_CONTROL_GROUP_CONTROLLER
);
416 if (streq(c
->parameter
, "v2"))
417 return cg_all_unified();
418 if (streq(c
->parameter
, "v1")) {
419 r
= cg_all_unified();
425 r
= cg_mask_supported(&system_mask
);
427 return log_debug_errno(r
, "Failed to determine supported controllers: %m");
429 r
= cg_mask_from_string(c
->parameter
, &wanted_mask
);
430 if (r
< 0 || wanted_mask
<= 0) {
431 /* This won't catch the case that we have an unknown controller
432 * mixed in with valid ones -- these are only assessed on the
433 * validity of the valid controllers found. */
434 log_debug("Failed to parse cgroup string: %s", c
->parameter
);
438 return FLAGS_SET(system_mask
, wanted_mask
);
441 static int condition_test_group(Condition
*c
, char **env
) {
446 assert(c
->parameter
);
447 assert(c
->type
== CONDITION_GROUP
);
449 r
= parse_gid(c
->parameter
, &id
);
453 /* Avoid any NSS lookups if we are PID1 */
454 if (getpid_cached() == 1)
455 return streq(c
->parameter
, "root");
457 return in_group(c
->parameter
) > 0;
460 static int condition_test_virtualization(Condition
*c
, char **env
) {
464 assert(c
->parameter
);
465 assert(c
->type
== CONDITION_VIRTUALIZATION
);
467 if (streq(c
->parameter
, "private-users"))
468 return running_in_userns();
470 v
= detect_virtualization();
474 /* First, compare with yes/no */
475 b
= parse_boolean(c
->parameter
);
479 /* Then, compare categorization */
480 if (streq(c
->parameter
, "vm"))
481 return VIRTUALIZATION_IS_VM(v
);
483 if (streq(c
->parameter
, "container"))
484 return VIRTUALIZATION_IS_CONTAINER(v
);
486 /* Finally compare id */
487 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
490 static int condition_test_architecture(Condition
*c
, char **env
) {
494 assert(c
->parameter
);
495 assert(c
->type
== CONDITION_ARCHITECTURE
);
497 a
= uname_architecture();
501 if (streq(c
->parameter
, "native"))
502 b
= native_architecture();
504 b
= architecture_from_string(c
->parameter
);
505 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
512 #define DTCOMPAT_FILE "/proc/device-tree/compatible"
513 static int condition_test_firmware_devicetree_compatible(const char *dtcarg
) {
515 _cleanup_free_
char *dtcompat
= NULL
;
516 _cleanup_strv_free_
char **dtcompatlist
= NULL
;
519 r
= read_full_virtual_file(DTCOMPAT_FILE
, &dtcompat
, &size
);
521 /* if the path doesn't exist it is incompatible */
523 log_debug_errno(r
, "Failed to open() '%s', assuming machine is incompatible: %m", DTCOMPAT_FILE
);
527 /* Not sure this can happen, but play safe. */
529 log_debug("%s has zero length, assuming machine is incompatible", DTCOMPAT_FILE
);
533 /* /proc/device-tree/compatible consists of one or more strings, each ending in '\0'.
534 * So the last character in dtcompat must be a '\0'. */
535 if (dtcompat
[size
- 1] != '\0') {
536 log_debug("%s is in an unknown format, assuming machine is incompatible", DTCOMPAT_FILE
);
540 dtcompatlist
= strv_parse_nulstr(dtcompat
, size
);
544 return strv_contains(dtcompatlist
, dtcarg
);
547 static int condition_test_firmware(Condition
*c
, char **env
) {
551 assert(c
->parameter
);
552 assert(c
->type
== CONDITION_FIRMWARE
);
554 if (streq(c
->parameter
, "device-tree")) {
555 if (access("/sys/firmware/device-tree/", F_OK
) < 0) {
557 log_debug_errno(errno
, "Unexpected error when checking for /sys/firmware/device-tree/: %m");
561 } else if ((dtc
= startswith(c
->parameter
, "device-tree-compatible("))) {
562 _cleanup_free_
char *dtcarg
= NULL
;
565 end
= strchr(dtc
, ')');
566 if (!end
|| *(end
+ 1) != '\0') {
567 log_debug("Malformed Firmware condition \"%s\"", c
->parameter
);
571 dtcarg
= strndup(dtc
, end
- dtc
);
575 return condition_test_firmware_devicetree_compatible(dtcarg
);
576 } else if (streq(c
->parameter
, "uefi"))
577 return is_efi_boot();
579 log_debug("Unsupported Firmware condition \"%s\"", c
->parameter
);
584 static int condition_test_host(Condition
*c
, char **env
) {
585 _cleanup_free_
char *h
= NULL
;
590 assert(c
->parameter
);
591 assert(c
->type
== CONDITION_HOST
);
593 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
595 r
= sd_id128_get_machine(&y
);
599 return sd_id128_equal(x
, y
);
602 h
= gethostname_malloc();
606 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
609 static int condition_test_ac_power(Condition
*c
, char **env
) {
613 assert(c
->parameter
);
614 assert(c
->type
== CONDITION_AC_POWER
);
616 r
= parse_boolean(c
->parameter
);
620 return (on_ac_power() != 0) == !!r
;
623 static int has_tpm2(void) {
626 /* Checks whether the system has at least one TPM2 resource manager device, i.e. at least one "tpmrm"
629 r
= dir_is_empty("/sys/class/tpmrm");
631 return true; /* nice! we have a device */
633 /* Hmm, so Linux doesn't know of the TPM2 device (or we couldn't check for it), most likely because
634 * the driver wasn't loaded yet. Let's see if the firmware knows about a TPM2 device, in this
635 * case. This way we can answer the TPM2 question already during early boot (where we most likely
640 /* OK, this didn't work either, in this case propagate the original errors */
644 return log_debug_errno(r
, "Failed to determine whether system has TPM2 support: %m");
649 static int condition_test_security(Condition
*c
, char **env
) {
651 assert(c
->parameter
);
652 assert(c
->type
== CONDITION_SECURITY
);
654 if (streq(c
->parameter
, "selinux"))
655 return mac_selinux_use();
656 if (streq(c
->parameter
, "smack"))
657 return mac_smack_use();
658 if (streq(c
->parameter
, "apparmor"))
659 return mac_apparmor_use();
660 if (streq(c
->parameter
, "audit"))
662 if (streq(c
->parameter
, "ima"))
664 if (streq(c
->parameter
, "tomoyo"))
665 return mac_tomoyo_use();
666 if (streq(c
->parameter
, "uefi-secureboot"))
667 return is_efi_secure_boot();
668 if (streq(c
->parameter
, "tpm2"))
674 static int condition_test_capability(Condition
*c
, char **env
) {
675 unsigned long long capabilities
= (unsigned long long) -1;
676 _cleanup_fclose_
FILE *f
= NULL
;
680 assert(c
->parameter
);
681 assert(c
->type
== CONDITION_CAPABILITY
);
683 /* If it's an invalid capability, we don't have it */
684 value
= capability_from_name(c
->parameter
);
688 /* If it's a valid capability we default to assume
691 f
= fopen("/proc/self/status", "re");
696 _cleanup_free_
char *line
= NULL
;
699 r
= read_line(f
, LONG_LINE_MAX
, &line
);
705 p
= startswith(line
, "CapBnd:");
707 if (sscanf(line
+7, "%llx", &capabilities
) != 1)
714 return !!(capabilities
& (1ULL << value
));
717 static int condition_test_needs_update(Condition
*c
, char **env
) {
718 struct stat usr
, other
;
724 assert(c
->parameter
);
725 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
727 r
= proc_cmdline_get_bool("systemd.condition-needs-update", &b
);
729 log_debug_errno(r
, "Failed to parse systemd.condition-needs-update= kernel command line argument, ignoring: %m");
734 log_debug("We are in an initrd, not doing any updates.");
738 if (!path_is_absolute(c
->parameter
)) {
739 log_debug("Specified condition parameter '%s' is not absolute, assuming an update is needed.", c
->parameter
);
743 /* If the file system is read-only we shouldn't suggest an update */
744 r
= path_is_read_only_fs(c
->parameter
);
746 log_debug_errno(r
, "Failed to determine if '%s' is read-only, ignoring: %m", c
->parameter
);
750 /* Any other failure means we should allow the condition to be true, so that we rather invoke too
751 * many update tools than too few. */
753 p
= strjoina(c
->parameter
, "/.updated");
754 if (lstat(p
, &other
) < 0) {
756 log_debug_errno(errno
, "Failed to stat() '%s', assuming an update is needed: %m", p
);
760 if (lstat("/usr/", &usr
) < 0) {
761 log_debug_errno(errno
, "Failed to stat() /usr/, assuming an update is needed: %m");
766 * First, compare seconds as they are always accurate...
768 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
769 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
772 * ...then compare nanoseconds.
774 * A false positive is only possible when /usr's nanoseconds > 0
775 * (otherwise /usr cannot be strictly newer than the target file)
776 * AND the target file's nanoseconds == 0
777 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
779 if (usr
.st_mtim
.tv_nsec
== 0 || other
.st_mtim
.tv_nsec
> 0)
780 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
782 _cleanup_free_
char *timestamp_str
= NULL
;
783 r
= parse_env_file(NULL
, p
, "TIMESTAMP_NSEC", ×tamp_str
);
785 log_debug_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
788 log_debug("No data in timestamp file '%s', using mtime.", p
);
793 r
= safe_atou64(timestamp_str
, ×tamp
);
795 log_debug_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
799 return timespec_load_nsec(&usr
.st_mtim
) > timestamp
;
802 static int condition_test_first_boot(Condition
*c
, char **env
) {
807 assert(c
->parameter
);
808 assert(c
->type
== CONDITION_FIRST_BOOT
);
810 r
= proc_cmdline_get_bool("systemd.condition-first-boot", &b
);
812 log_debug_errno(r
, "Failed to parse systemd.condition-first-boot= kernel command line argument, ignoring: %m");
816 r
= parse_boolean(c
->parameter
);
820 q
= access("/run/systemd/first-boot", F_OK
);
821 if (q
< 0 && errno
!= ENOENT
)
822 log_debug_errno(errno
, "Failed to check if /run/systemd/first-boot exists, ignoring: %m");
824 return (q
>= 0) == !!r
;
827 static int condition_test_environment(Condition
*c
, char **env
) {
832 assert(c
->parameter
);
833 assert(c
->type
== CONDITION_ENVIRONMENT
);
835 equal
= strchr(c
->parameter
, '=');
837 STRV_FOREACH(i
, env
) {
841 found
= streq(c
->parameter
, *i
);
845 f
= startswith(*i
, c
->parameter
);
846 found
= f
&& IN_SET(*f
, 0, '=');
856 static int condition_test_path_exists(Condition
*c
, char **env
) {
858 assert(c
->parameter
);
859 assert(c
->type
== CONDITION_PATH_EXISTS
);
861 return access(c
->parameter
, F_OK
) >= 0;
864 static int condition_test_path_exists_glob(Condition
*c
, char **env
) {
866 assert(c
->parameter
);
867 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
869 return glob_exists(c
->parameter
) > 0;
872 static int condition_test_path_is_directory(Condition
*c
, char **env
) {
874 assert(c
->parameter
);
875 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
877 return is_dir(c
->parameter
, true) > 0;
880 static int condition_test_path_is_symbolic_link(Condition
*c
, char **env
) {
882 assert(c
->parameter
);
883 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
885 return is_symlink(c
->parameter
) > 0;
888 static int condition_test_path_is_mount_point(Condition
*c
, char **env
) {
890 assert(c
->parameter
);
891 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
893 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
896 static int condition_test_path_is_read_write(Condition
*c
, char **env
) {
898 assert(c
->parameter
);
899 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
901 return path_is_read_only_fs(c
->parameter
) <= 0;
904 static int condition_test_cpufeature(Condition
*c
, char **env
) {
906 assert(c
->parameter
);
907 assert(c
->type
== CONDITION_CPU_FEATURE
);
909 return has_cpu_with_flag(ascii_strlower(c
->parameter
));
912 static int condition_test_path_is_encrypted(Condition
*c
, char **env
) {
916 assert(c
->parameter
);
917 assert(c
->type
== CONDITION_PATH_IS_ENCRYPTED
);
919 r
= path_is_encrypted(c
->parameter
);
920 if (r
< 0 && r
!= -ENOENT
)
921 log_debug_errno(r
, "Failed to determine if '%s' is encrypted: %m", c
->parameter
);
926 static int condition_test_directory_not_empty(Condition
*c
, char **env
) {
930 assert(c
->parameter
);
931 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
933 r
= dir_is_empty(c
->parameter
);
934 return r
<= 0 && r
!= -ENOENT
;
937 static int condition_test_file_not_empty(Condition
*c
, char **env
) {
941 assert(c
->parameter
);
942 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
944 return (stat(c
->parameter
, &st
) >= 0 &&
945 S_ISREG(st
.st_mode
) &&
949 static int condition_test_file_is_executable(Condition
*c
, char **env
) {
953 assert(c
->parameter
);
954 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
956 return (stat(c
->parameter
, &st
) >= 0 &&
957 S_ISREG(st
.st_mode
) &&
958 (st
.st_mode
& 0111));
961 int condition_test(Condition
*c
, char **env
) {
963 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
, char **env
) = {
964 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
965 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
966 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
967 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
968 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
969 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
970 [CONDITION_PATH_IS_ENCRYPTED
] = condition_test_path_is_encrypted
,
971 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
972 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
973 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
974 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
975 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
976 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
977 [CONDITION_SECURITY
] = condition_test_security
,
978 [CONDITION_CAPABILITY
] = condition_test_capability
,
979 [CONDITION_HOST
] = condition_test_host
,
980 [CONDITION_AC_POWER
] = condition_test_ac_power
,
981 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
982 [CONDITION_FIRMWARE
] = condition_test_firmware
,
983 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
984 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
985 [CONDITION_USER
] = condition_test_user
,
986 [CONDITION_GROUP
] = condition_test_group
,
987 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
988 [CONDITION_CPUS
] = condition_test_cpus
,
989 [CONDITION_MEMORY
] = condition_test_memory
,
990 [CONDITION_ENVIRONMENT
] = condition_test_environment
,
991 [CONDITION_CPU_FEATURE
] = condition_test_cpufeature
,
992 [CONDITION_OS_RELEASE
] = condition_test_osrelease
,
998 assert(c
->type
>= 0);
999 assert(c
->type
< _CONDITION_TYPE_MAX
);
1001 r
= condition_tests
[c
->type
](c
, env
);
1003 c
->result
= CONDITION_ERROR
;
1007 b
= (r
> 0) == !c
->negate
;
1008 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
1012 bool condition_test_list(
1015 condition_to_string_t to_string
,
1016 condition_test_logger_t logger
,
1022 assert(!!logger
== !!to_string
);
1024 /* If the condition list is empty, then it is true */
1028 /* Otherwise, if all of the non-trigger conditions apply and
1029 * if any of the trigger conditions apply (unless there are
1030 * none) we return true */
1031 LIST_FOREACH(conditions
, c
, first
) {
1034 r
= condition_test(c
, env
);
1038 logger(userdata
, LOG_WARNING
, r
, PROJECT_FILE
, __LINE__
, __func__
,
1039 "Couldn't determine result for %s=%s%s%s, assuming failed: %m",
1041 c
->trigger
? "|" : "",
1042 c
->negate
? "!" : "",
1045 logger(userdata
, LOG_DEBUG
, 0, PROJECT_FILE
, __LINE__
, __func__
,
1048 c
->trigger
? "|" : "",
1049 c
->negate
? "!" : "",
1051 condition_result_to_string(c
->result
));
1054 if (!c
->trigger
&& r
<= 0)
1057 if (c
->trigger
&& triggered
<= 0)
1061 return triggered
!= 0;
1064 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, condition_to_string_t to_string
) {
1069 prefix
= strempty(prefix
);
1072 "%s\t%s: %s%s%s %s\n",
1075 c
->trigger
? "|" : "",
1076 c
->negate
? "!" : "",
1078 condition_result_to_string(c
->result
));
1081 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, condition_to_string_t to_string
) {
1084 LIST_FOREACH(conditions
, c
, first
)
1085 condition_dump(c
, f
, prefix
, to_string
);
1088 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
1089 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
1090 [CONDITION_FIRMWARE
] = "ConditionFirmware",
1091 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
1092 [CONDITION_HOST
] = "ConditionHost",
1093 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
1094 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
1095 [CONDITION_SECURITY
] = "ConditionSecurity",
1096 [CONDITION_CAPABILITY
] = "ConditionCapability",
1097 [CONDITION_AC_POWER
] = "ConditionACPower",
1098 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
1099 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
1100 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
1101 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
1102 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
1103 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
1104 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
1105 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
1106 [CONDITION_PATH_IS_ENCRYPTED
] = "ConditionPathIsEncrypted",
1107 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
1108 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
1109 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
1110 [CONDITION_USER
] = "ConditionUser",
1111 [CONDITION_GROUP
] = "ConditionGroup",
1112 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
1113 [CONDITION_CPUS
] = "ConditionCPUs",
1114 [CONDITION_MEMORY
] = "ConditionMemory",
1115 [CONDITION_ENVIRONMENT
] = "ConditionEnvironment",
1116 [CONDITION_CPU_FEATURE
] = "ConditionCPUFeature",
1117 [CONDITION_OS_RELEASE
] = "ConditionOSRelease",
1120 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
1122 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
1123 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
1124 [CONDITION_FIRMWARE
] = "AssertFirmware",
1125 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
1126 [CONDITION_HOST
] = "AssertHost",
1127 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
1128 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
1129 [CONDITION_SECURITY
] = "AssertSecurity",
1130 [CONDITION_CAPABILITY
] = "AssertCapability",
1131 [CONDITION_AC_POWER
] = "AssertACPower",
1132 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
1133 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
1134 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
1135 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
1136 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
1137 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
1138 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
1139 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
1140 [CONDITION_PATH_IS_ENCRYPTED
] = "AssertPathIsEncrypted",
1141 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
1142 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
1143 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
1144 [CONDITION_USER
] = "AssertUser",
1145 [CONDITION_GROUP
] = "AssertGroup",
1146 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
1147 [CONDITION_CPUS
] = "AssertCPUs",
1148 [CONDITION_MEMORY
] = "AssertMemory",
1149 [CONDITION_ENVIRONMENT
] = "AssertEnvironment",
1150 [CONDITION_CPU_FEATURE
] = "AssertCPUFeature",
1151 [CONDITION_OS_RELEASE
] = "AssertOSRelease",
1154 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
1156 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
1157 [CONDITION_UNTESTED
] = "untested",
1158 [CONDITION_SUCCEEDED
] = "succeeded",
1159 [CONDITION_FAILED
] = "failed",
1160 [CONDITION_ERROR
] = "error",
1163 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);