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"
26 #include "extract-word.h"
30 #include "glob-util.h"
31 #include "hostname-util.h"
33 #include "limits-util.h"
36 #include "mountpoint-util.h"
37 #include "parse-util.h"
38 #include "path-util.h"
39 #include "proc-cmdline.h"
40 #include "process-util.h"
41 #include "selinux-util.h"
42 #include "smack-util.h"
43 #include "stat-util.h"
44 #include "string-table.h"
45 #include "string-util.h"
46 #include "tomoyo-util.h"
47 #include "user-record.h"
48 #include "user-util.h"
52 Condition
* condition_new(ConditionType type
, const char *parameter
, bool trigger
, bool negate
) {
56 assert(type
< _CONDITION_TYPE_MAX
);
59 c
= new(Condition
, 1);
70 c
->parameter
= strdup(parameter
);
78 Condition
* condition_free(Condition
*c
) {
85 Condition
* condition_free_list_type(Condition
*head
, ConditionType type
) {
88 LIST_FOREACH_SAFE(conditions
, c
, n
, head
)
89 if (type
< 0 || c
->type
== type
) {
90 LIST_REMOVE(conditions
, head
, c
);
94 assert(type
>= 0 || !head
);
98 static int condition_test_kernel_command_line(Condition
*c
, char **env
) {
99 _cleanup_free_
char *line
= NULL
;
105 assert(c
->parameter
);
106 assert(c
->type
== CONDITION_KERNEL_COMMAND_LINE
);
108 r
= proc_cmdline(&line
);
112 equal
= strchr(c
->parameter
, '=');
115 _cleanup_free_
char *word
= NULL
;
118 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RELAX
);
125 found
= streq(word
, c
->parameter
);
129 f
= startswith(word
, c
->parameter
);
130 found
= f
&& IN_SET(*f
, 0, '=');
141 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
142 * should be listed first. */
143 ORDER_LOWER_OR_EQUAL
,
144 ORDER_GREATER_OR_EQUAL
,
150 _ORDER_INVALID
= -EINVAL
,
153 static OrderOperator
parse_order(const char **s
) {
155 static const char *const prefix
[_ORDER_MAX
] = {
156 [ORDER_LOWER_OR_EQUAL
] = "<=",
157 [ORDER_GREATER_OR_EQUAL
] = ">=",
159 [ORDER_GREATER
] = ">",
161 [ORDER_UNEQUAL
] = "!=",
166 for (i
= 0; i
< _ORDER_MAX
; i
++) {
169 e
= startswith(*s
, prefix
[i
]);
176 return _ORDER_INVALID
;
179 static bool test_order(int k
, OrderOperator p
) {
186 case ORDER_LOWER_OR_EQUAL
:
195 case ORDER_GREATER_OR_EQUAL
:
202 assert_not_reached("unknown order");
207 static int condition_test_kernel_version(Condition
*c
, char **env
) {
214 assert(c
->parameter
);
215 assert(c
->type
== CONDITION_KERNEL_VERSION
);
217 assert_se(uname(&u
) >= 0);
222 _cleanup_free_
char *word
= NULL
;
226 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
);
228 return log_debug_errno(r
, "Failed to parse condition string \"%s\": %m", p
);
233 order
= parse_order(&s
);
235 s
+= strspn(s
, WHITESPACE
);
238 /* For backwards compatibility, allow whitespace between the operator and
239 * value, without quoting, but only in the first expression. */
241 r
= extract_first_word(&p
, &word
, NULL
, 0);
243 return log_debug_errno(r
, "Failed to parse condition string \"%s\": %m", p
);
245 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Unexpected end of expression: %s", p
);
248 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Unexpected end of expression: %s", p
);
251 r
= test_order(strverscmp_improved(u
.release
, s
), order
);
253 /* No prefix? Then treat as glob string */
254 r
= fnmatch(s
, u
.release
, 0) == 0;
265 static int condition_test_memory(Condition
*c
, char **env
) {
272 assert(c
->parameter
);
273 assert(c
->type
== CONDITION_MEMORY
);
275 m
= physical_memory();
278 order
= parse_order(&p
);
280 order
= ORDER_GREATER_OR_EQUAL
; /* default to >= check, if nothing is specified. */
282 r
= safe_atou64(p
, &k
);
284 return log_debug_errno(r
, "Failed to parse size: %m");
286 return test_order(CMP(m
, k
), order
);
289 static int condition_test_cpus(Condition
*c
, char **env
) {
296 assert(c
->parameter
);
297 assert(c
->type
== CONDITION_CPUS
);
299 n
= cpus_in_affinity_mask();
301 return log_debug_errno(n
, "Failed to determine CPUs in affinity mask: %m");
304 order
= parse_order(&p
);
306 order
= ORDER_GREATER_OR_EQUAL
; /* default to >= check, if nothing is specified. */
308 r
= safe_atou(p
, &k
);
310 return log_debug_errno(r
, "Failed to parse number of CPUs: %m");
312 return test_order(CMP((unsigned) n
, k
), order
);
315 static int condition_test_user(Condition
*c
, char **env
) {
318 _cleanup_free_
char *username
= NULL
;
322 assert(c
->parameter
);
323 assert(c
->type
== CONDITION_USER
);
325 r
= parse_uid(c
->parameter
, &id
);
327 return id
== getuid() || id
== geteuid();
329 if (streq("@system", c
->parameter
))
330 return uid_is_system(getuid()) || uid_is_system(geteuid());
332 username
= getusername_malloc();
336 if (streq(username
, c
->parameter
))
339 if (getpid_cached() == 1)
340 return streq(c
->parameter
, "root");
343 r
= get_user_creds(&u
, &id
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
347 return id
== getuid() || id
== geteuid();
350 static int condition_test_control_group_controller(Condition
*c
, char **env
) {
352 CGroupMask system_mask
, wanted_mask
= 0;
355 assert(c
->parameter
);
356 assert(c
->type
== CONDITION_CONTROL_GROUP_CONTROLLER
);
358 if (streq(c
->parameter
, "v2"))
359 return cg_all_unified();
360 if (streq(c
->parameter
, "v1")) {
361 r
= cg_all_unified();
367 r
= cg_mask_supported(&system_mask
);
369 return log_debug_errno(r
, "Failed to determine supported controllers: %m");
371 r
= cg_mask_from_string(c
->parameter
, &wanted_mask
);
372 if (r
< 0 || wanted_mask
<= 0) {
373 /* This won't catch the case that we have an unknown controller
374 * mixed in with valid ones -- these are only assessed on the
375 * validity of the valid controllers found. */
376 log_debug("Failed to parse cgroup string: %s", c
->parameter
);
380 return FLAGS_SET(system_mask
, wanted_mask
);
383 static int condition_test_group(Condition
*c
, char **env
) {
388 assert(c
->parameter
);
389 assert(c
->type
== CONDITION_GROUP
);
391 r
= parse_gid(c
->parameter
, &id
);
395 /* Avoid any NSS lookups if we are PID1 */
396 if (getpid_cached() == 1)
397 return streq(c
->parameter
, "root");
399 return in_group(c
->parameter
) > 0;
402 static int condition_test_virtualization(Condition
*c
, char **env
) {
406 assert(c
->parameter
);
407 assert(c
->type
== CONDITION_VIRTUALIZATION
);
409 if (streq(c
->parameter
, "private-users"))
410 return running_in_userns();
412 v
= detect_virtualization();
416 /* First, compare with yes/no */
417 b
= parse_boolean(c
->parameter
);
421 /* Then, compare categorization */
422 if (streq(c
->parameter
, "vm"))
423 return VIRTUALIZATION_IS_VM(v
);
425 if (streq(c
->parameter
, "container"))
426 return VIRTUALIZATION_IS_CONTAINER(v
);
428 /* Finally compare id */
429 return v
!= VIRTUALIZATION_NONE
&& streq(c
->parameter
, virtualization_to_string(v
));
432 static int condition_test_architecture(Condition
*c
, char **env
) {
436 assert(c
->parameter
);
437 assert(c
->type
== CONDITION_ARCHITECTURE
);
439 a
= uname_architecture();
443 if (streq(c
->parameter
, "native"))
444 b
= native_architecture();
446 b
= architecture_from_string(c
->parameter
);
447 if (b
< 0) /* unknown architecture? Then it's definitely not ours */
454 #define DTCOMPAT_FILE "/sys/firmware/devicetree/base/compatible"
455 static int condition_test_firmware_devicetree_compatible(const char *dtcarg
) {
457 _cleanup_free_
char *dtcompat
= NULL
;
458 _cleanup_strv_free_
char **dtcompatlist
= NULL
;
461 r
= read_full_virtual_file(DTCOMPAT_FILE
, &dtcompat
, &size
);
463 /* if the path doesn't exist it is incompatible */
465 log_debug_errno(r
, "Failed to open() '%s', assuming machine is incompatible: %m", DTCOMPAT_FILE
);
469 /* Not sure this can happen, but play safe. */
471 log_debug("%s has zero length, assuming machine is incompatible", DTCOMPAT_FILE
);
476 * /sys/firmware/devicetree/base/compatible consists of one or more
477 * strings, each ending in '\0'. So the last character in dtcompat must
480 if (dtcompat
[size
- 1] != '\0') {
481 log_debug("%s is in an unknown format, assuming machine is incompatible", DTCOMPAT_FILE
);
485 dtcompatlist
= strv_parse_nulstr(dtcompat
, size
);
489 return strv_contains(dtcompatlist
, dtcarg
);
492 static int condition_test_firmware(Condition
*c
, char **env
) {
496 assert(c
->parameter
);
497 assert(c
->type
== CONDITION_FIRMWARE
);
499 if (streq(c
->parameter
, "device-tree")) {
500 if (access("/sys/firmware/device-tree/", F_OK
) < 0) {
502 log_debug_errno(errno
, "Unexpected error when checking for /sys/firmware/device-tree/: %m");
506 } else if ((dtc
= startswith(c
->parameter
, "device-tree-compatible("))) {
507 _cleanup_free_
char *dtcarg
= NULL
;
510 end
= strchr(dtc
, ')');
511 if (!end
|| *(end
+ 1) != '\0') {
512 log_debug("Malformed Firmware condition \"%s\"", c
->parameter
);
516 dtcarg
= strndup(dtc
, end
- dtc
);
520 return condition_test_firmware_devicetree_compatible(dtcarg
);
521 } else if (streq(c
->parameter
, "uefi"))
522 return is_efi_boot();
524 log_debug("Unsupported Firmware condition \"%s\"", c
->parameter
);
529 static int condition_test_host(Condition
*c
, char **env
) {
530 _cleanup_free_
char *h
= NULL
;
535 assert(c
->parameter
);
536 assert(c
->type
== CONDITION_HOST
);
538 if (sd_id128_from_string(c
->parameter
, &x
) >= 0) {
540 r
= sd_id128_get_machine(&y
);
544 return sd_id128_equal(x
, y
);
547 h
= gethostname_malloc();
551 return fnmatch(c
->parameter
, h
, FNM_CASEFOLD
) == 0;
554 static int condition_test_ac_power(Condition
*c
, char **env
) {
558 assert(c
->parameter
);
559 assert(c
->type
== CONDITION_AC_POWER
);
561 r
= parse_boolean(c
->parameter
);
565 return (on_ac_power() != 0) == !!r
;
568 static int has_tpm2(void) {
571 /* Checks whether the system has at least one TPM2 resource manager device, i.e. at least one "tpmrm"
574 r
= dir_is_empty("/sys/class/tpmrm");
576 return true; /* nice! we have a device */
578 /* Hmm, so Linux doesn't know of the TPM2 device (or we couldn't check for it), most likely because
579 * the driver wasn't loaded yet. Let's see if the firmware knows about a TPM2 device, in this
580 * case. This way we can answer the TPM2 question already during early boot (where we most likely
585 /* OK, this didn't work either, in this case propagate the original errors */
589 return log_debug_errno(r
, "Failed to determine whether system has TPM2 support: %m");
594 static int condition_test_security(Condition
*c
, char **env
) {
596 assert(c
->parameter
);
597 assert(c
->type
== CONDITION_SECURITY
);
599 if (streq(c
->parameter
, "selinux"))
600 return mac_selinux_use();
601 if (streq(c
->parameter
, "smack"))
602 return mac_smack_use();
603 if (streq(c
->parameter
, "apparmor"))
604 return mac_apparmor_use();
605 if (streq(c
->parameter
, "audit"))
607 if (streq(c
->parameter
, "ima"))
609 if (streq(c
->parameter
, "tomoyo"))
610 return mac_tomoyo_use();
611 if (streq(c
->parameter
, "uefi-secureboot"))
612 return is_efi_secure_boot();
613 if (streq(c
->parameter
, "tpm2"))
619 static int condition_test_capability(Condition
*c
, char **env
) {
620 unsigned long long capabilities
= (unsigned long long) -1;
621 _cleanup_fclose_
FILE *f
= NULL
;
625 assert(c
->parameter
);
626 assert(c
->type
== CONDITION_CAPABILITY
);
628 /* If it's an invalid capability, we don't have it */
629 value
= capability_from_name(c
->parameter
);
633 /* If it's a valid capability we default to assume
636 f
= fopen("/proc/self/status", "re");
641 _cleanup_free_
char *line
= NULL
;
644 r
= read_line(f
, LONG_LINE_MAX
, &line
);
650 p
= startswith(line
, "CapBnd:");
652 if (sscanf(line
+7, "%llx", &capabilities
) != 1)
659 return !!(capabilities
& (1ULL << value
));
662 static int condition_test_needs_update(Condition
*c
, char **env
) {
663 struct stat usr
, other
;
669 assert(c
->parameter
);
670 assert(c
->type
== CONDITION_NEEDS_UPDATE
);
672 r
= proc_cmdline_get_bool("systemd.condition-needs-update", &b
);
674 log_debug_errno(r
, "Failed to parse systemd.condition-needs-update= kernel command line argument, ignoring: %m");
679 log_debug("We are in an initrd, not doing any updates.");
683 if (!path_is_absolute(c
->parameter
)) {
684 log_debug("Specified condition parameter '%s' is not absolute, assuming an update is needed.", c
->parameter
);
688 /* If the file system is read-only we shouldn't suggest an update */
689 r
= path_is_read_only_fs(c
->parameter
);
691 log_debug_errno(r
, "Failed to determine if '%s' is read-only, ignoring: %m", c
->parameter
);
695 /* Any other failure means we should allow the condition to be true, so that we rather invoke too
696 * many update tools than too few. */
698 p
= strjoina(c
->parameter
, "/.updated");
699 if (lstat(p
, &other
) < 0) {
701 log_debug_errno(errno
, "Failed to stat() '%s', assuming an update is needed: %m", p
);
705 if (lstat("/usr/", &usr
) < 0) {
706 log_debug_errno(errno
, "Failed to stat() /usr/, assuming an update is needed: %m");
711 * First, compare seconds as they are always accurate...
713 if (usr
.st_mtim
.tv_sec
!= other
.st_mtim
.tv_sec
)
714 return usr
.st_mtim
.tv_sec
> other
.st_mtim
.tv_sec
;
717 * ...then compare nanoseconds.
719 * A false positive is only possible when /usr's nanoseconds > 0
720 * (otherwise /usr cannot be strictly newer than the target file)
721 * AND the target file's nanoseconds == 0
722 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
724 if (usr
.st_mtim
.tv_nsec
== 0 || other
.st_mtim
.tv_nsec
> 0)
725 return usr
.st_mtim
.tv_nsec
> other
.st_mtim
.tv_nsec
;
727 _cleanup_free_
char *timestamp_str
= NULL
;
728 r
= parse_env_file(NULL
, p
, "TIMESTAMP_NSEC", ×tamp_str
);
730 log_debug_errno(r
, "Failed to parse timestamp file '%s', using mtime: %m", p
);
733 log_debug("No data in timestamp file '%s', using mtime.", p
);
738 r
= safe_atou64(timestamp_str
, ×tamp
);
740 log_debug_errno(r
, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str
, p
);
744 return timespec_load_nsec(&usr
.st_mtim
) > timestamp
;
747 static int condition_test_first_boot(Condition
*c
, char **env
) {
752 assert(c
->parameter
);
753 assert(c
->type
== CONDITION_FIRST_BOOT
);
755 r
= proc_cmdline_get_bool("systemd.condition-first-boot", &b
);
757 log_debug_errno(r
, "Failed to parse systemd.condition-first-boot= kernel command line argument, ignoring: %m");
761 r
= parse_boolean(c
->parameter
);
765 q
= access("/run/systemd/first-boot", F_OK
);
766 if (q
< 0 && errno
!= ENOENT
)
767 log_debug_errno(errno
, "Failed to check if /run/systemd/first-boot exists, ignoring: %m");
769 return (q
>= 0) == !!r
;
772 static int condition_test_environment(Condition
*c
, char **env
) {
777 assert(c
->parameter
);
778 assert(c
->type
== CONDITION_ENVIRONMENT
);
780 equal
= strchr(c
->parameter
, '=');
782 STRV_FOREACH(i
, env
) {
786 found
= streq(c
->parameter
, *i
);
790 f
= startswith(*i
, c
->parameter
);
791 found
= f
&& IN_SET(*f
, 0, '=');
801 static int condition_test_path_exists(Condition
*c
, char **env
) {
803 assert(c
->parameter
);
804 assert(c
->type
== CONDITION_PATH_EXISTS
);
806 return access(c
->parameter
, F_OK
) >= 0;
809 static int condition_test_path_exists_glob(Condition
*c
, char **env
) {
811 assert(c
->parameter
);
812 assert(c
->type
== CONDITION_PATH_EXISTS_GLOB
);
814 return glob_exists(c
->parameter
) > 0;
817 static int condition_test_path_is_directory(Condition
*c
, char **env
) {
819 assert(c
->parameter
);
820 assert(c
->type
== CONDITION_PATH_IS_DIRECTORY
);
822 return is_dir(c
->parameter
, true) > 0;
825 static int condition_test_path_is_symbolic_link(Condition
*c
, char **env
) {
827 assert(c
->parameter
);
828 assert(c
->type
== CONDITION_PATH_IS_SYMBOLIC_LINK
);
830 return is_symlink(c
->parameter
) > 0;
833 static int condition_test_path_is_mount_point(Condition
*c
, char **env
) {
835 assert(c
->parameter
);
836 assert(c
->type
== CONDITION_PATH_IS_MOUNT_POINT
);
838 return path_is_mount_point(c
->parameter
, NULL
, AT_SYMLINK_FOLLOW
) > 0;
841 static int condition_test_path_is_read_write(Condition
*c
, char **env
) {
843 assert(c
->parameter
);
844 assert(c
->type
== CONDITION_PATH_IS_READ_WRITE
);
846 return path_is_read_only_fs(c
->parameter
) <= 0;
849 static int condition_test_cpufeature(Condition
*c
, char **env
) {
851 assert(c
->parameter
);
852 assert(c
->type
== CONDITION_CPU_FEATURE
);
854 return has_cpu_with_flag(ascii_strlower(c
->parameter
));
857 static int condition_test_path_is_encrypted(Condition
*c
, char **env
) {
861 assert(c
->parameter
);
862 assert(c
->type
== CONDITION_PATH_IS_ENCRYPTED
);
864 r
= path_is_encrypted(c
->parameter
);
865 if (r
< 0 && r
!= -ENOENT
)
866 log_debug_errno(r
, "Failed to determine if '%s' is encrypted: %m", c
->parameter
);
871 static int condition_test_directory_not_empty(Condition
*c
, char **env
) {
875 assert(c
->parameter
);
876 assert(c
->type
== CONDITION_DIRECTORY_NOT_EMPTY
);
878 r
= dir_is_empty(c
->parameter
);
879 return r
<= 0 && r
!= -ENOENT
;
882 static int condition_test_file_not_empty(Condition
*c
, char **env
) {
886 assert(c
->parameter
);
887 assert(c
->type
== CONDITION_FILE_NOT_EMPTY
);
889 return (stat(c
->parameter
, &st
) >= 0 &&
890 S_ISREG(st
.st_mode
) &&
894 static int condition_test_file_is_executable(Condition
*c
, char **env
) {
898 assert(c
->parameter
);
899 assert(c
->type
== CONDITION_FILE_IS_EXECUTABLE
);
901 return (stat(c
->parameter
, &st
) >= 0 &&
902 S_ISREG(st
.st_mode
) &&
903 (st
.st_mode
& 0111));
906 int condition_test(Condition
*c
, char **env
) {
908 static int (*const condition_tests
[_CONDITION_TYPE_MAX
])(Condition
*c
, char **env
) = {
909 [CONDITION_PATH_EXISTS
] = condition_test_path_exists
,
910 [CONDITION_PATH_EXISTS_GLOB
] = condition_test_path_exists_glob
,
911 [CONDITION_PATH_IS_DIRECTORY
] = condition_test_path_is_directory
,
912 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = condition_test_path_is_symbolic_link
,
913 [CONDITION_PATH_IS_MOUNT_POINT
] = condition_test_path_is_mount_point
,
914 [CONDITION_PATH_IS_READ_WRITE
] = condition_test_path_is_read_write
,
915 [CONDITION_PATH_IS_ENCRYPTED
] = condition_test_path_is_encrypted
,
916 [CONDITION_DIRECTORY_NOT_EMPTY
] = condition_test_directory_not_empty
,
917 [CONDITION_FILE_NOT_EMPTY
] = condition_test_file_not_empty
,
918 [CONDITION_FILE_IS_EXECUTABLE
] = condition_test_file_is_executable
,
919 [CONDITION_KERNEL_COMMAND_LINE
] = condition_test_kernel_command_line
,
920 [CONDITION_KERNEL_VERSION
] = condition_test_kernel_version
,
921 [CONDITION_VIRTUALIZATION
] = condition_test_virtualization
,
922 [CONDITION_SECURITY
] = condition_test_security
,
923 [CONDITION_CAPABILITY
] = condition_test_capability
,
924 [CONDITION_HOST
] = condition_test_host
,
925 [CONDITION_AC_POWER
] = condition_test_ac_power
,
926 [CONDITION_ARCHITECTURE
] = condition_test_architecture
,
927 [CONDITION_FIRMWARE
] = condition_test_firmware
,
928 [CONDITION_NEEDS_UPDATE
] = condition_test_needs_update
,
929 [CONDITION_FIRST_BOOT
] = condition_test_first_boot
,
930 [CONDITION_USER
] = condition_test_user
,
931 [CONDITION_GROUP
] = condition_test_group
,
932 [CONDITION_CONTROL_GROUP_CONTROLLER
] = condition_test_control_group_controller
,
933 [CONDITION_CPUS
] = condition_test_cpus
,
934 [CONDITION_MEMORY
] = condition_test_memory
,
935 [CONDITION_ENVIRONMENT
] = condition_test_environment
,
936 [CONDITION_CPU_FEATURE
] = condition_test_cpufeature
,
942 assert(c
->type
>= 0);
943 assert(c
->type
< _CONDITION_TYPE_MAX
);
945 r
= condition_tests
[c
->type
](c
, env
);
947 c
->result
= CONDITION_ERROR
;
951 b
= (r
> 0) == !c
->negate
;
952 c
->result
= b
? CONDITION_SUCCEEDED
: CONDITION_FAILED
;
956 bool condition_test_list(
959 condition_to_string_t to_string
,
960 condition_test_logger_t logger
,
966 assert(!!logger
== !!to_string
);
968 /* If the condition list is empty, then it is true */
972 /* Otherwise, if all of the non-trigger conditions apply and
973 * if any of the trigger conditions apply (unless there are
974 * none) we return true */
975 LIST_FOREACH(conditions
, c
, first
) {
978 r
= condition_test(c
, env
);
982 logger(userdata
, LOG_WARNING
, r
, PROJECT_FILE
, __LINE__
, __func__
,
983 "Couldn't determine result for %s=%s%s%s, assuming failed: %m",
985 c
->trigger
? "|" : "",
986 c
->negate
? "!" : "",
989 logger(userdata
, LOG_DEBUG
, 0, PROJECT_FILE
, __LINE__
, __func__
,
992 c
->trigger
? "|" : "",
993 c
->negate
? "!" : "",
995 condition_result_to_string(c
->result
));
998 if (!c
->trigger
&& r
<= 0)
1001 if (c
->trigger
&& triggered
<= 0)
1005 return triggered
!= 0;
1008 void condition_dump(Condition
*c
, FILE *f
, const char *prefix
, condition_to_string_t to_string
) {
1013 prefix
= strempty(prefix
);
1016 "%s\t%s: %s%s%s %s\n",
1019 c
->trigger
? "|" : "",
1020 c
->negate
? "!" : "",
1022 condition_result_to_string(c
->result
));
1025 void condition_dump_list(Condition
*first
, FILE *f
, const char *prefix
, condition_to_string_t to_string
) {
1028 LIST_FOREACH(conditions
, c
, first
)
1029 condition_dump(c
, f
, prefix
, to_string
);
1032 static const char* const condition_type_table
[_CONDITION_TYPE_MAX
] = {
1033 [CONDITION_ARCHITECTURE
] = "ConditionArchitecture",
1034 [CONDITION_FIRMWARE
] = "ConditionFirmware",
1035 [CONDITION_VIRTUALIZATION
] = "ConditionVirtualization",
1036 [CONDITION_HOST
] = "ConditionHost",
1037 [CONDITION_KERNEL_COMMAND_LINE
] = "ConditionKernelCommandLine",
1038 [CONDITION_KERNEL_VERSION
] = "ConditionKernelVersion",
1039 [CONDITION_SECURITY
] = "ConditionSecurity",
1040 [CONDITION_CAPABILITY
] = "ConditionCapability",
1041 [CONDITION_AC_POWER
] = "ConditionACPower",
1042 [CONDITION_NEEDS_UPDATE
] = "ConditionNeedsUpdate",
1043 [CONDITION_FIRST_BOOT
] = "ConditionFirstBoot",
1044 [CONDITION_PATH_EXISTS
] = "ConditionPathExists",
1045 [CONDITION_PATH_EXISTS_GLOB
] = "ConditionPathExistsGlob",
1046 [CONDITION_PATH_IS_DIRECTORY
] = "ConditionPathIsDirectory",
1047 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "ConditionPathIsSymbolicLink",
1048 [CONDITION_PATH_IS_MOUNT_POINT
] = "ConditionPathIsMountPoint",
1049 [CONDITION_PATH_IS_READ_WRITE
] = "ConditionPathIsReadWrite",
1050 [CONDITION_PATH_IS_ENCRYPTED
] = "ConditionPathIsEncrypted",
1051 [CONDITION_DIRECTORY_NOT_EMPTY
] = "ConditionDirectoryNotEmpty",
1052 [CONDITION_FILE_NOT_EMPTY
] = "ConditionFileNotEmpty",
1053 [CONDITION_FILE_IS_EXECUTABLE
] = "ConditionFileIsExecutable",
1054 [CONDITION_USER
] = "ConditionUser",
1055 [CONDITION_GROUP
] = "ConditionGroup",
1056 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "ConditionControlGroupController",
1057 [CONDITION_CPUS
] = "ConditionCPUs",
1058 [CONDITION_MEMORY
] = "ConditionMemory",
1059 [CONDITION_ENVIRONMENT
] = "ConditionEnvironment",
1060 [CONDITION_CPU_FEATURE
] = "ConditionCPUFeature",
1063 DEFINE_STRING_TABLE_LOOKUP(condition_type
, ConditionType
);
1065 static const char* const assert_type_table
[_CONDITION_TYPE_MAX
] = {
1066 [CONDITION_ARCHITECTURE
] = "AssertArchitecture",
1067 [CONDITION_FIRMWARE
] = "AssertFirmware",
1068 [CONDITION_VIRTUALIZATION
] = "AssertVirtualization",
1069 [CONDITION_HOST
] = "AssertHost",
1070 [CONDITION_KERNEL_COMMAND_LINE
] = "AssertKernelCommandLine",
1071 [CONDITION_KERNEL_VERSION
] = "AssertKernelVersion",
1072 [CONDITION_SECURITY
] = "AssertSecurity",
1073 [CONDITION_CAPABILITY
] = "AssertCapability",
1074 [CONDITION_AC_POWER
] = "AssertACPower",
1075 [CONDITION_NEEDS_UPDATE
] = "AssertNeedsUpdate",
1076 [CONDITION_FIRST_BOOT
] = "AssertFirstBoot",
1077 [CONDITION_PATH_EXISTS
] = "AssertPathExists",
1078 [CONDITION_PATH_EXISTS_GLOB
] = "AssertPathExistsGlob",
1079 [CONDITION_PATH_IS_DIRECTORY
] = "AssertPathIsDirectory",
1080 [CONDITION_PATH_IS_SYMBOLIC_LINK
] = "AssertPathIsSymbolicLink",
1081 [CONDITION_PATH_IS_MOUNT_POINT
] = "AssertPathIsMountPoint",
1082 [CONDITION_PATH_IS_READ_WRITE
] = "AssertPathIsReadWrite",
1083 [CONDITION_PATH_IS_ENCRYPTED
] = "AssertPathIsEncrypted",
1084 [CONDITION_DIRECTORY_NOT_EMPTY
] = "AssertDirectoryNotEmpty",
1085 [CONDITION_FILE_NOT_EMPTY
] = "AssertFileNotEmpty",
1086 [CONDITION_FILE_IS_EXECUTABLE
] = "AssertFileIsExecutable",
1087 [CONDITION_USER
] = "AssertUser",
1088 [CONDITION_GROUP
] = "AssertGroup",
1089 [CONDITION_CONTROL_GROUP_CONTROLLER
] = "AssertControlGroupController",
1090 [CONDITION_CPUS
] = "AssertCPUs",
1091 [CONDITION_MEMORY
] = "AssertMemory",
1092 [CONDITION_ENVIRONMENT
] = "AssertEnvironment",
1093 [CONDITION_CPU_FEATURE
] = "AssertCPUFeature",
1096 DEFINE_STRING_TABLE_LOOKUP(assert_type
, ConditionType
);
1098 static const char* const condition_result_table
[_CONDITION_RESULT_MAX
] = {
1099 [CONDITION_UNTESTED
] = "untested",
1100 [CONDITION_SUCCEEDED
] = "succeeded",
1101 [CONDITION_FAILED
] = "failed",
1102 [CONDITION_ERROR
] = "error",
1105 DEFINE_STRING_TABLE_LOOKUP(condition_result
, ConditionResult
);