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 "user-record.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 "/sys/firmware/devicetree/base/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
);
534 * /sys/firmware/devicetree/base/compatible consists of one or more
535 * strings, each ending in '\0'. So the last character in dtcompat must
538 if (dtcompat
[size
- 1] != '\0') {
539 log_debug("%s is in an unknown format, assuming machine is incompatible", DTCOMPAT_FILE
);
543 dtcompatlist
= strv_parse_nulstr(dtcompat
, size
);
547 return strv_contains(dtcompatlist
, dtcarg
);
550 static int condition_test_firmware(Condition
*c
, char **env
) {
554 assert(c
->parameter
);
555 assert(c
->type
== CONDITION_FIRMWARE
);
557 if (streq(c
->parameter
, "device-tree")) {
558 if (access("/sys/firmware/device-tree/", F_OK
) < 0) {
560 log_debug_errno(errno
, "Unexpected error when checking for /sys/firmware/device-tree/: %m");
564 } else if ((dtc
= startswith(c
->parameter
, "device-tree-compatible("))) {
565 _cleanup_free_
char *dtcarg
= NULL
;
568 end
= strchr(dtc
, ')');
569 if (!end
|| *(end
+ 1) != '\0') {
570 log_debug("Malformed Firmware condition \"%s\"", c
->parameter
);
574 dtcarg
= strndup(dtc
, end
- dtc
);
578 return condition_test_firmware_devicetree_compatible(dtcarg
);
579 } else if (streq(c
->parameter
, "uefi"))
580 return is_efi_boot();
582 log_debug("Unsupported Firmware condition \"%s\"", c
->parameter
);
587 static int condition_test_host(Condition
*c
, char **env
) {
588 _cleanup_free_
char *h
= NULL
;
593 assert(c
->parameter
);
594 assert(c
->type
== CONDITION_HOST
);
596 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
598 r
= sd_id128_get_machine(&y
);
602 return sd_id128_equal(x
, y
);
605 h
= gethostname_malloc();
609 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
612 static int condition_test_ac_power(Condition
*c
, char **env
) {
616 assert(c
->parameter
);
617 assert(c
->type
== CONDITION_AC_POWER
);
619 r
= parse_boolean(c
->parameter
);
623 return (on_ac_power() != 0) == !!r
;
626 static int has_tpm2(void) {
629 /* Checks whether the system has at least one TPM2 resource manager device, i.e. at least one "tpmrm"
632 r
= dir_is_empty("/sys/class/tpmrm");
634 return true; /* nice! we have a device */
636 /* Hmm, so Linux doesn't know of the TPM2 device (or we couldn't check for it), most likely because
637 * the driver wasn't loaded yet. Let's see if the firmware knows about a TPM2 device, in this
638 * case. This way we can answer the TPM2 question already during early boot (where we most likely
643 /* OK, this didn't work either, in this case propagate the original errors */
647 return log_debug_errno(r
, "Failed to determine whether system has TPM2 support: %m");
652 static int condition_test_security(Condition
*c
, char **env
) {
654 assert(c
->parameter
);
655 assert(c
->type
== CONDITION_SECURITY
);
657 if (streq(c
->parameter
, "selinux"))
658 return mac_selinux_use();
659 if (streq(c
->parameter
, "smack"))
660 return mac_smack_use();
661 if (streq(c
->parameter
, "apparmor"))
662 return mac_apparmor_use();
663 if (streq(c
->parameter
, "audit"))
665 if (streq(c
->parameter
, "ima"))
667 if (streq(c
->parameter
, "tomoyo"))
668 return mac_tomoyo_use();
669 if (streq(c
->parameter
, "uefi-secureboot"))
670 return is_efi_secure_boot();
671 if (streq(c
->parameter
, "tpm2"))
677 static int condition_test_capability(Condition
*c
, char **env
) {
678 unsigned long long capabilities
= (unsigned long long) -1;
679 _cleanup_fclose_
FILE *f
= NULL
;
683 assert(c
->parameter
);
684 assert(c
->type
== CONDITION_CAPABILITY
);
686 /* If it's an invalid capability, we don't have it */
687 value
= capability_from_name(c
->parameter
);
691 /* If it's a valid capability we default to assume
694 f
= fopen("/proc/self/status", "re");
699 _cleanup_free_
char *line
= NULL
;
702 r
= read_line(f
, LONG_LINE_MAX
, &line
);
708 p
= startswith(line
, "CapBnd:");
710 if (sscanf(line
+7, "%llx", &capabilities
) != 1)
717 return !!(capabilities
& (1ULL << value
));
720 static int condition_test_needs_update(Condition
*c
, char **env
) {
721 struct stat usr
, other
;
727 assert(c
->parameter
);
728 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
730 r
= proc_cmdline_get_bool("systemd.condition-needs-update", &b
);
732 log_debug_errno(r
, "Failed to parse systemd.condition-needs-update= kernel command line argument, ignoring: %m");
737 log_debug("We are in an initrd, not doing any updates.");
741 if (!path_is_absolute(c
->parameter
)) {
742 log_debug("Specified condition parameter '%s' is not absolute, assuming an update is needed.", c
->parameter
);
746 /* If the file system is read-only we shouldn't suggest an update */
747 r
= path_is_read_only_fs(c
->parameter
);
749 log_debug_errno(r
, "Failed to determine if '%s' is read-only, ignoring: %m", c
->parameter
);
753 /* Any other failure means we should allow the condition to be true, so that we rather invoke too
754 * many update tools than too few. */
756 p
= strjoina(c
->parameter
, "/.updated");
757 if (lstat(p
, &other
) < 0) {
759 log_debug_errno(errno
, "Failed to stat() '%s', assuming an update is needed: %m", p
);
763 if (lstat("/usr/", &usr
) < 0) {
764 log_debug_errno(errno
, "Failed to stat() /usr/, assuming an update is needed: %m");
769 * First, compare seconds as they are always accurate...
771 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
772 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
775 * ...then compare nanoseconds.
777 * A false positive is only possible when /usr's nanoseconds > 0
778 * (otherwise /usr cannot be strictly newer than the target file)
779 * AND the target file's nanoseconds == 0
780 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
782 if (usr
.st_mtim
.tv_nsec
== 0 || other
.st_mtim
.tv_nsec
> 0)
783 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
785 _cleanup_free_
char *timestamp_str
= NULL
;
786 r
= parse_env_file(NULL
, p
, "TIMESTAMP_NSEC", ×tamp_str
);
788 log_debug_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
791 log_debug("No data in timestamp file '%s', using mtime.", p
);
796 r
= safe_atou64(timestamp_str
, ×tamp
);
798 log_debug_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
802 return timespec_load_nsec(&usr
.st_mtim
) > timestamp
;
805 static int condition_test_first_boot(Condition
*c
, char **env
) {
810 assert(c
->parameter
);
811 assert(c
->type
== CONDITION_FIRST_BOOT
);
813 r
= proc_cmdline_get_bool("systemd.condition-first-boot", &b
);
815 log_debug_errno(r
, "Failed to parse systemd.condition-first-boot= kernel command line argument, ignoring: %m");
819 r
= parse_boolean(c
->parameter
);
823 q
= access("/run/systemd/first-boot", F_OK
);
824 if (q
< 0 && errno
!= ENOENT
)
825 log_debug_errno(errno
, "Failed to check if /run/systemd/first-boot exists, ignoring: %m");
827 return (q
>= 0) == !!r
;
830 static int condition_test_environment(Condition
*c
, char **env
) {
835 assert(c
->parameter
);
836 assert(c
->type
== CONDITION_ENVIRONMENT
);
838 equal
= strchr(c
->parameter
, '=');
840 STRV_FOREACH(i
, env
) {
844 found
= streq(c
->parameter
, *i
);
848 f
= startswith(*i
, c
->parameter
);
849 found
= f
&& IN_SET(*f
, 0, '=');
859 static int condition_test_path_exists(Condition
*c
, char **env
) {
861 assert(c
->parameter
);
862 assert(c
->type
== CONDITION_PATH_EXISTS
);
864 return access(c
->parameter
, F_OK
) >= 0;
867 static int condition_test_path_exists_glob(Condition
*c
, char **env
) {
869 assert(c
->parameter
);
870 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
872 return glob_exists(c
->parameter
) > 0;
875 static int condition_test_path_is_directory(Condition
*c
, char **env
) {
877 assert(c
->parameter
);
878 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
880 return is_dir(c
->parameter
, true) > 0;
883 static int condition_test_path_is_symbolic_link(Condition
*c
, char **env
) {
885 assert(c
->parameter
);
886 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
888 return is_symlink(c
->parameter
) > 0;
891 static int condition_test_path_is_mount_point(Condition
*c
, char **env
) {
893 assert(c
->parameter
);
894 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
896 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
899 static int condition_test_path_is_read_write(Condition
*c
, char **env
) {
901 assert(c
->parameter
);
902 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
904 return path_is_read_only_fs(c
->parameter
) <= 0;
907 static int condition_test_cpufeature(Condition
*c
, char **env
) {
909 assert(c
->parameter
);
910 assert(c
->type
== CONDITION_CPU_FEATURE
);
912 return has_cpu_with_flag(ascii_strlower(c
->parameter
));
915 static int condition_test_path_is_encrypted(Condition
*c
, char **env
) {
919 assert(c
->parameter
);
920 assert(c
->type
== CONDITION_PATH_IS_ENCRYPTED
);
922 r
= path_is_encrypted(c
->parameter
);
923 if (r
< 0 && r
!= -ENOENT
)
924 log_debug_errno(r
, "Failed to determine if '%s' is encrypted: %m", c
->parameter
);
929 static int condition_test_directory_not_empty(Condition
*c
, char **env
) {
933 assert(c
->parameter
);
934 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
936 r
= dir_is_empty(c
->parameter
);
937 return r
<= 0 && r
!= -ENOENT
;
940 static int condition_test_file_not_empty(Condition
*c
, char **env
) {
944 assert(c
->parameter
);
945 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
947 return (stat(c
->parameter
, &st
) >= 0 &&
948 S_ISREG(st
.st_mode
) &&
952 static int condition_test_file_is_executable(Condition
*c
, char **env
) {
956 assert(c
->parameter
);
957 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
959 return (stat(c
->parameter
, &st
) >= 0 &&
960 S_ISREG(st
.st_mode
) &&
961 (st
.st_mode
& 0111));
964 int condition_test(Condition
*c
, char **env
) {
966 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
, char **env
) = {
967 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
968 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
969 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
970 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
971 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
972 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
973 [CONDITION_PATH_IS_ENCRYPTED
] = condition_test_path_is_encrypted
,
974 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
975 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
976 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
977 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
978 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
979 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
980 [CONDITION_SECURITY
] = condition_test_security
,
981 [CONDITION_CAPABILITY
] = condition_test_capability
,
982 [CONDITION_HOST
] = condition_test_host
,
983 [CONDITION_AC_POWER
] = condition_test_ac_power
,
984 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
985 [CONDITION_FIRMWARE
] = condition_test_firmware
,
986 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
987 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
988 [CONDITION_USER
] = condition_test_user
,
989 [CONDITION_GROUP
] = condition_test_group
,
990 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
991 [CONDITION_CPUS
] = condition_test_cpus
,
992 [CONDITION_MEMORY
] = condition_test_memory
,
993 [CONDITION_ENVIRONMENT
] = condition_test_environment
,
994 [CONDITION_CPU_FEATURE
] = condition_test_cpufeature
,
995 [CONDITION_OS_RELEASE
] = condition_test_osrelease
,
1001 assert(c
->type
>= 0);
1002 assert(c
->type
< _CONDITION_TYPE_MAX
);
1004 r
= condition_tests
[c
->type
](c
, env
);
1006 c
->result
= CONDITION_ERROR
;
1010 b
= (r
> 0) == !c
->negate
;
1011 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
1015 bool condition_test_list(
1018 condition_to_string_t to_string
,
1019 condition_test_logger_t logger
,
1025 assert(!!logger
== !!to_string
);
1027 /* If the condition list is empty, then it is true */
1031 /* Otherwise, if all of the non-trigger conditions apply and
1032 * if any of the trigger conditions apply (unless there are
1033 * none) we return true */
1034 LIST_FOREACH(conditions
, c
, first
) {
1037 r
= condition_test(c
, env
);
1041 logger(userdata
, LOG_WARNING
, r
, PROJECT_FILE
, __LINE__
, __func__
,
1042 "Couldn't determine result for %s=%s%s%s, assuming failed: %m",
1044 c
->trigger
? "|" : "",
1045 c
->negate
? "!" : "",
1048 logger(userdata
, LOG_DEBUG
, 0, PROJECT_FILE
, __LINE__
, __func__
,
1051 c
->trigger
? "|" : "",
1052 c
->negate
? "!" : "",
1054 condition_result_to_string(c
->result
));
1057 if (!c
->trigger
&& r
<= 0)
1060 if (c
->trigger
&& triggered
<= 0)
1064 return triggered
!= 0;
1067 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, condition_to_string_t to_string
) {
1072 prefix
= strempty(prefix
);
1075 "%s\t%s: %s%s%s %s\n",
1078 c
->trigger
? "|" : "",
1079 c
->negate
? "!" : "",
1081 condition_result_to_string(c
->result
));
1084 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, condition_to_string_t to_string
) {
1087 LIST_FOREACH(conditions
, c
, first
)
1088 condition_dump(c
, f
, prefix
, to_string
);
1091 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
1092 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
1093 [CONDITION_FIRMWARE
] = "ConditionFirmware",
1094 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
1095 [CONDITION_HOST
] = "ConditionHost",
1096 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
1097 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
1098 [CONDITION_SECURITY
] = "ConditionSecurity",
1099 [CONDITION_CAPABILITY
] = "ConditionCapability",
1100 [CONDITION_AC_POWER
] = "ConditionACPower",
1101 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
1102 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
1103 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
1104 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
1105 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
1106 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
1107 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
1108 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
1109 [CONDITION_PATH_IS_ENCRYPTED
] = "ConditionPathIsEncrypted",
1110 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
1111 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
1112 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
1113 [CONDITION_USER
] = "ConditionUser",
1114 [CONDITION_GROUP
] = "ConditionGroup",
1115 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
1116 [CONDITION_CPUS
] = "ConditionCPUs",
1117 [CONDITION_MEMORY
] = "ConditionMemory",
1118 [CONDITION_ENVIRONMENT
] = "ConditionEnvironment",
1119 [CONDITION_CPU_FEATURE
] = "ConditionCPUFeature",
1120 [CONDITION_OS_RELEASE
] = "ConditionOSRelease",
1123 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
1125 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
1126 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
1127 [CONDITION_FIRMWARE
] = "AssertFirmware",
1128 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
1129 [CONDITION_HOST
] = "AssertHost",
1130 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
1131 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
1132 [CONDITION_SECURITY
] = "AssertSecurity",
1133 [CONDITION_CAPABILITY
] = "AssertCapability",
1134 [CONDITION_AC_POWER
] = "AssertACPower",
1135 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
1136 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
1137 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
1138 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
1139 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
1140 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
1141 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
1142 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
1143 [CONDITION_PATH_IS_ENCRYPTED
] = "AssertPathIsEncrypted",
1144 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
1145 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
1146 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
1147 [CONDITION_USER
] = "AssertUser",
1148 [CONDITION_GROUP
] = "AssertGroup",
1149 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
1150 [CONDITION_CPUS
] = "AssertCPUs",
1151 [CONDITION_MEMORY
] = "AssertMemory",
1152 [CONDITION_ENVIRONMENT
] = "AssertEnvironment",
1153 [CONDITION_CPU_FEATURE
] = "AssertCPUFeature",
1154 [CONDITION_OS_RELEASE
] = "AssertOSRelease",
1157 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
1159 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
1160 [CONDITION_UNTESTED
] = "untested",
1161 [CONDITION_SUCCEEDED
] = "succeeded",
1162 [CONDITION_FAILED
] = "failed",
1163 [CONDITION_ERROR
] = "error",
1166 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);