]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/condition.c
Merge pull request #15433 from mrc0mmand/test-reintroduce-parallelization
[thirdparty/systemd.git] / src / shared / condition.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <fnmatch.h>
6 #include <limits.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/utsname.h>
10 #include <time.h>
11 #include <unistd.h>
12
13 #include "sd-id128.h"
14
15 #include "alloc-util.h"
16 #include "apparmor-util.h"
17 #include "architecture.h"
18 #include "audit-util.h"
19 #include "cap-list.h"
20 #include "cgroup-util.h"
21 #include "condition.h"
22 #include "cpu-set-util.h"
23 #include "efi-loader.h"
24 #include "env-file.h"
25 #include "extract-word.h"
26 #include "fd-util.h"
27 #include "fileio.h"
28 #include "fs-util.h"
29 #include "glob-util.h"
30 #include "hostname-util.h"
31 #include "ima-util.h"
32 #include "limits-util.h"
33 #include "list.h"
34 #include "macro.h"
35 #include "mountpoint-util.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "proc-cmdline.h"
39 #include "process-util.h"
40 #include "selinux-util.h"
41 #include "smack-util.h"
42 #include "stat-util.h"
43 #include "string-table.h"
44 #include "string-util.h"
45 #include "tomoyo-util.h"
46 #include "user-util.h"
47 #include "util.h"
48 #include "virt.h"
49
50 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
51 Condition *c;
52
53 assert(type >= 0);
54 assert(type < _CONDITION_TYPE_MAX);
55 assert((!parameter) == (type == CONDITION_NULL));
56
57 c = new(Condition, 1);
58 if (!c)
59 return NULL;
60
61 *c = (Condition) {
62 .type = type,
63 .trigger = trigger,
64 .negate = negate,
65 };
66
67 if (parameter) {
68 c->parameter = strdup(parameter);
69 if (!c->parameter)
70 return mfree(c);
71 }
72
73 return c;
74 }
75
76 Condition* condition_free(Condition *c) {
77 assert(c);
78
79 free(c->parameter);
80 return mfree(c);
81 }
82
83 Condition* condition_free_list_type(Condition *head, ConditionType type) {
84 Condition *c, *n;
85
86 LIST_FOREACH_SAFE(conditions, c, n, head)
87 if (type < 0 || c->type == type) {
88 LIST_REMOVE(conditions, head, c);
89 condition_free(c);
90 }
91
92 assert(type >= 0 || !head);
93 return head;
94 }
95
96 static int condition_test_kernel_command_line(Condition *c, char **env) {
97 _cleanup_free_ char *line = NULL;
98 const char *p;
99 bool equal;
100 int r;
101
102 assert(c);
103 assert(c->parameter);
104 assert(c->type == CONDITION_KERNEL_COMMAND_LINE);
105
106 r = proc_cmdline(&line);
107 if (r < 0)
108 return r;
109
110 equal = strchr(c->parameter, '=');
111
112 for (p = line;;) {
113 _cleanup_free_ char *word = NULL;
114 bool found;
115
116 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
117 if (r < 0)
118 return r;
119 if (r == 0)
120 break;
121
122 if (equal)
123 found = streq(word, c->parameter);
124 else {
125 const char *f;
126
127 f = startswith(word, c->parameter);
128 found = f && IN_SET(*f, 0, '=');
129 }
130
131 if (found)
132 return true;
133 }
134
135 return false;
136 }
137
138 typedef enum {
139 /* Listed in order of checking. Note that some comparators are prefixes of others, hence the longest
140 * should be listed first. */
141 ORDER_LOWER_OR_EQUAL,
142 ORDER_GREATER_OR_EQUAL,
143 ORDER_LOWER,
144 ORDER_GREATER,
145 ORDER_EQUAL,
146 ORDER_UNEQUAL,
147 _ORDER_MAX,
148 _ORDER_INVALID = -1
149 } OrderOperator;
150
151 static OrderOperator parse_order(const char **s) {
152
153 static const char *const prefix[_ORDER_MAX] = {
154 [ORDER_LOWER_OR_EQUAL] = "<=",
155 [ORDER_GREATER_OR_EQUAL] = ">=",
156 [ORDER_LOWER] = "<",
157 [ORDER_GREATER] = ">",
158 [ORDER_EQUAL] = "=",
159 [ORDER_UNEQUAL] = "!=",
160 };
161
162 OrderOperator i;
163
164 for (i = 0; i < _ORDER_MAX; i++) {
165 const char *e;
166
167 e = startswith(*s, prefix[i]);
168 if (e) {
169 *s = e;
170 return i;
171 }
172 }
173
174 return _ORDER_INVALID;
175 }
176
177 static bool test_order(int k, OrderOperator p) {
178
179 switch (p) {
180
181 case ORDER_LOWER:
182 return k < 0;
183
184 case ORDER_LOWER_OR_EQUAL:
185 return k <= 0;
186
187 case ORDER_EQUAL:
188 return k == 0;
189
190 case ORDER_UNEQUAL:
191 return k != 0;
192
193 case ORDER_GREATER_OR_EQUAL:
194 return k >= 0;
195
196 case ORDER_GREATER:
197 return k > 0;
198
199 default:
200 assert_not_reached("unknown order");
201
202 }
203 }
204
205 static int condition_test_kernel_version(Condition *c, char **env) {
206 OrderOperator order;
207 struct utsname u;
208 const char *p;
209 bool first = true;
210
211 assert(c);
212 assert(c->parameter);
213 assert(c->type == CONDITION_KERNEL_VERSION);
214
215 assert_se(uname(&u) >= 0);
216
217 p = c->parameter;
218
219 for (;;) {
220 _cleanup_free_ char *word = NULL;
221 const char *s;
222 int r;
223
224 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
225 if (r < 0)
226 return log_debug_errno(r, "Failed to parse condition string \"%s\": %m", p);
227 if (r == 0)
228 break;
229
230 s = strstrip(word);
231 order = parse_order(&s);
232 if (order >= 0) {
233 s += strspn(s, WHITESPACE);
234 if (isempty(s)) {
235 if (first) {
236 /* For backwards compatibility, allow whitespace between the operator and
237 * value, without quoting, but only in the first expression. */
238 word = mfree(word);
239 r = extract_first_word(&p, &word, NULL, 0);
240 if (r < 0)
241 return log_debug_errno(r, "Failed to parse condition string \"%s\": %m", p);
242 if (r == 0)
243 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected end of expression: %s", p);
244 s = word;
245 } else
246 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected end of expression: %s", p);
247 }
248
249 r = test_order(str_verscmp(u.release, s), order);
250 } else
251 /* No prefix? Then treat as glob string */
252 r = fnmatch(s, u.release, 0) == 0;
253
254 if (r == 0)
255 return false;
256
257 first = false;
258 }
259
260 return true;
261 }
262
263 static int condition_test_memory(Condition *c, char **env) {
264 OrderOperator order;
265 uint64_t m, k;
266 const char *p;
267 int r;
268
269 assert(c);
270 assert(c->parameter);
271 assert(c->type == CONDITION_MEMORY);
272
273 m = physical_memory();
274
275 p = c->parameter;
276 order = parse_order(&p);
277 if (order < 0)
278 order = ORDER_GREATER_OR_EQUAL; /* default to >= check, if nothing is specified. */
279
280 r = safe_atou64(p, &k);
281 if (r < 0)
282 return log_debug_errno(r, "Failed to parse size: %m");
283
284 return test_order(CMP(m, k), order);
285 }
286
287 static int condition_test_cpus(Condition *c, char **env) {
288 OrderOperator order;
289 const char *p;
290 unsigned k;
291 int r, n;
292
293 assert(c);
294 assert(c->parameter);
295 assert(c->type == CONDITION_CPUS);
296
297 n = cpus_in_affinity_mask();
298 if (n < 0)
299 return log_debug_errno(n, "Failed to determine CPUs in affinity mask: %m");
300
301 p = c->parameter;
302 order = parse_order(&p);
303 if (order < 0)
304 order = ORDER_GREATER_OR_EQUAL; /* default to >= check, if nothing is specified. */
305
306 r = safe_atou(p, &k);
307 if (r < 0)
308 return log_debug_errno(r, "Failed to parse number of CPUs: %m");
309
310 return test_order(CMP((unsigned) n, k), order);
311 }
312
313 static int condition_test_user(Condition *c, char **env) {
314 uid_t id;
315 int r;
316 _cleanup_free_ char *username = NULL;
317 const char *u;
318
319 assert(c);
320 assert(c->parameter);
321 assert(c->type == CONDITION_USER);
322
323 r = parse_uid(c->parameter, &id);
324 if (r >= 0)
325 return id == getuid() || id == geteuid();
326
327 if (streq("@system", c->parameter))
328 return uid_is_system(getuid()) || uid_is_system(geteuid());
329
330 username = getusername_malloc();
331 if (!username)
332 return -ENOMEM;
333
334 if (streq(username, c->parameter))
335 return 1;
336
337 if (getpid_cached() == 1)
338 return streq(c->parameter, "root");
339
340 u = c->parameter;
341 r = get_user_creds(&u, &id, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
342 if (r < 0)
343 return 0;
344
345 return id == getuid() || id == geteuid();
346 }
347
348 static int condition_test_control_group_controller(Condition *c, char **env) {
349 int r;
350 CGroupMask system_mask, wanted_mask = 0;
351
352 assert(c);
353 assert(c->parameter);
354 assert(c->type == CONDITION_CONTROL_GROUP_CONTROLLER);
355
356 r = cg_mask_supported(&system_mask);
357 if (r < 0)
358 return log_debug_errno(r, "Failed to determine supported controllers: %m");
359
360 r = cg_mask_from_string(c->parameter, &wanted_mask);
361 if (r < 0 || wanted_mask <= 0) {
362 /* This won't catch the case that we have an unknown controller
363 * mixed in with valid ones -- these are only assessed on the
364 * validity of the valid controllers found. */
365 log_debug("Failed to parse cgroup string: %s", c->parameter);
366 return 1;
367 }
368
369 return FLAGS_SET(system_mask, wanted_mask);
370 }
371
372 static int condition_test_group(Condition *c, char **env) {
373 gid_t id;
374 int r;
375
376 assert(c);
377 assert(c->parameter);
378 assert(c->type == CONDITION_GROUP);
379
380 r = parse_gid(c->parameter, &id);
381 if (r >= 0)
382 return in_gid(id);
383
384 /* Avoid any NSS lookups if we are PID1 */
385 if (getpid_cached() == 1)
386 return streq(c->parameter, "root");
387
388 return in_group(c->parameter) > 0;
389 }
390
391 static int condition_test_virtualization(Condition *c, char **env) {
392 int b, v;
393
394 assert(c);
395 assert(c->parameter);
396 assert(c->type == CONDITION_VIRTUALIZATION);
397
398 if (streq(c->parameter, "private-users"))
399 return running_in_userns();
400
401 v = detect_virtualization();
402 if (v < 0)
403 return v;
404
405 /* First, compare with yes/no */
406 b = parse_boolean(c->parameter);
407 if (b >= 0)
408 return b == !!v;
409
410 /* Then, compare categorization */
411 if (streq(c->parameter, "vm"))
412 return VIRTUALIZATION_IS_VM(v);
413
414 if (streq(c->parameter, "container"))
415 return VIRTUALIZATION_IS_CONTAINER(v);
416
417 /* Finally compare id */
418 return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v));
419 }
420
421 static int condition_test_architecture(Condition *c, char **env) {
422 int a, b;
423
424 assert(c);
425 assert(c->parameter);
426 assert(c->type == CONDITION_ARCHITECTURE);
427
428 a = uname_architecture();
429 if (a < 0)
430 return a;
431
432 if (streq(c->parameter, "native"))
433 b = native_architecture();
434 else {
435 b = architecture_from_string(c->parameter);
436 if (b < 0) /* unknown architecture? Then it's definitely not ours */
437 return false;
438 }
439
440 return a == b;
441 }
442
443 static int condition_test_host(Condition *c, char **env) {
444 _cleanup_free_ char *h = NULL;
445 sd_id128_t x, y;
446 int r;
447
448 assert(c);
449 assert(c->parameter);
450 assert(c->type == CONDITION_HOST);
451
452 if (sd_id128_from_string(c->parameter, &x) >= 0) {
453
454 r = sd_id128_get_machine(&y);
455 if (r < 0)
456 return r;
457
458 return sd_id128_equal(x, y);
459 }
460
461 h = gethostname_malloc();
462 if (!h)
463 return -ENOMEM;
464
465 return fnmatch(c->parameter, h, FNM_CASEFOLD) == 0;
466 }
467
468 static int condition_test_ac_power(Condition *c, char **env) {
469 int r;
470
471 assert(c);
472 assert(c->parameter);
473 assert(c->type == CONDITION_AC_POWER);
474
475 r = parse_boolean(c->parameter);
476 if (r < 0)
477 return r;
478
479 return (on_ac_power() != 0) == !!r;
480 }
481
482 static int condition_test_security(Condition *c, char **env) {
483 assert(c);
484 assert(c->parameter);
485 assert(c->type == CONDITION_SECURITY);
486
487 if (streq(c->parameter, "selinux"))
488 return mac_selinux_use();
489 if (streq(c->parameter, "smack"))
490 return mac_smack_use();
491 if (streq(c->parameter, "apparmor"))
492 return mac_apparmor_use();
493 if (streq(c->parameter, "audit"))
494 return use_audit();
495 if (streq(c->parameter, "ima"))
496 return use_ima();
497 if (streq(c->parameter, "tomoyo"))
498 return mac_tomoyo_use();
499 if (streq(c->parameter, "uefi-secureboot"))
500 return is_efi_secure_boot();
501
502 return false;
503 }
504
505 static int condition_test_capability(Condition *c, char **env) {
506 unsigned long long capabilities = (unsigned long long) -1;
507 _cleanup_fclose_ FILE *f = NULL;
508 int value, r;
509
510 assert(c);
511 assert(c->parameter);
512 assert(c->type == CONDITION_CAPABILITY);
513
514 /* If it's an invalid capability, we don't have it */
515 value = capability_from_name(c->parameter);
516 if (value < 0)
517 return -EINVAL;
518
519 /* If it's a valid capability we default to assume
520 * that we have it */
521
522 f = fopen("/proc/self/status", "re");
523 if (!f)
524 return -errno;
525
526 for (;;) {
527 _cleanup_free_ char *line = NULL;
528 const char *p;
529
530 r = read_line(f, LONG_LINE_MAX, &line);
531 if (r < 0)
532 return r;
533 if (r == 0)
534 break;
535
536 p = startswith(line, "CapBnd:");
537 if (p) {
538 if (sscanf(line+7, "%llx", &capabilities) != 1)
539 return -EIO;
540
541 break;
542 }
543 }
544
545 return !!(capabilities & (1ULL << value));
546 }
547
548 static int condition_test_needs_update(Condition *c, char **env) {
549 struct stat usr, other;
550 const char *p;
551 bool b;
552 int r;
553
554 assert(c);
555 assert(c->parameter);
556 assert(c->type == CONDITION_NEEDS_UPDATE);
557
558 r = proc_cmdline_get_bool("systemd.condition-needs-update", &b);
559 if (r < 0)
560 log_debug_errno(r, "Failed to parse systemd.condition-needs-update= kernel command line argument, ignoring: %m");
561 if (r > 0)
562 return b;
563
564 if (!path_is_absolute(c->parameter)) {
565 log_debug("Specified condition parameter '%s' is not absolute, assuming an update is needed.", c->parameter);
566 return true;
567 }
568
569 /* If the file system is read-only we shouldn't suggest an update */
570 r = path_is_read_only_fs(c->parameter);
571 if (r < 0)
572 log_debug_errno(r, "Failed to determine if '%s' is read-only, ignoring: %m", c->parameter);
573 if (r > 0)
574 return false;
575
576 /* Any other failure means we should allow the condition to be true, so that we rather invoke too
577 * many update tools than too few. */
578
579 p = strjoina(c->parameter, "/.updated");
580 if (lstat(p, &other) < 0) {
581 if (errno != ENOENT)
582 log_debug_errno(errno, "Failed to stat() '%s', assuming an update is needed: %m", p);
583 return true;
584 }
585
586 if (lstat("/usr/", &usr) < 0) {
587 log_debug_errno(errno, "Failed to stat() /usr/, assuming an update is needed: %m");
588 return true;
589 }
590
591 /*
592 * First, compare seconds as they are always accurate...
593 */
594 if (usr.st_mtim.tv_sec != other.st_mtim.tv_sec)
595 return usr.st_mtim.tv_sec > other.st_mtim.tv_sec;
596
597 /*
598 * ...then compare nanoseconds.
599 *
600 * A false positive is only possible when /usr's nanoseconds > 0
601 * (otherwise /usr cannot be strictly newer than the target file)
602 * AND the target file's nanoseconds == 0
603 * (otherwise the filesystem supports nsec timestamps, see stat(2)).
604 */
605 if (usr.st_mtim.tv_nsec == 0 || other.st_mtim.tv_nsec > 0)
606 return usr.st_mtim.tv_nsec > other.st_mtim.tv_nsec;
607
608 _cleanup_free_ char *timestamp_str = NULL;
609 r = parse_env_file(NULL, p, "TIMESTAMP_NSEC", &timestamp_str);
610 if (r < 0) {
611 log_debug_errno(r, "Failed to parse timestamp file '%s', using mtime: %m", p);
612 return true;
613 } else if (r == 0) {
614 log_debug("No data in timestamp file '%s', using mtime.", p);
615 return true;
616 }
617
618 uint64_t timestamp;
619 r = safe_atou64(timestamp_str, &timestamp);
620 if (r < 0) {
621 log_debug_errno(r, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str, p);
622 return true;
623 }
624
625 return timespec_load_nsec(&usr.st_mtim) > timestamp;
626 }
627
628 static int condition_test_first_boot(Condition *c, char **env) {
629 int r, q;
630 bool b;
631
632 assert(c);
633 assert(c->parameter);
634 assert(c->type == CONDITION_FIRST_BOOT);
635
636 r = proc_cmdline_get_bool("systemd.condition-first-boot", &b);
637 if (r < 0)
638 log_debug_errno(r, "Failed to parse systemd.condition-first-boot= kernel command line argument, ignoring: %m");
639 if (r > 0)
640 return b == !!r;
641
642 r = parse_boolean(c->parameter);
643 if (r < 0)
644 return r;
645
646 q = access("/run/systemd/first-boot", F_OK);
647 if (q < 0 && errno != ENOENT)
648 log_debug_errno(errno, "Failed to check if /run/systemd/first-boot exists, ignoring: %m");
649
650 return (q >= 0) == !!r;
651 }
652
653 static int condition_test_environment(Condition *c, char **env) {
654 bool equal;
655 char **i;
656
657 assert(c);
658 assert(c->parameter);
659 assert(c->type == CONDITION_ENVIRONMENT);
660
661 equal = strchr(c->parameter, '=');
662
663 STRV_FOREACH(i, env) {
664 bool found;
665
666 if (equal)
667 found = streq(c->parameter, *i);
668 else {
669 const char *f;
670
671 f = startswith(*i, c->parameter);
672 found = f && IN_SET(*f, 0, '=');
673 }
674
675 if (found)
676 return true;
677 }
678
679 return false;
680 }
681
682 static int condition_test_path_exists(Condition *c, char **env) {
683 assert(c);
684 assert(c->parameter);
685 assert(c->type == CONDITION_PATH_EXISTS);
686
687 return access(c->parameter, F_OK) >= 0;
688 }
689
690 static int condition_test_path_exists_glob(Condition *c, char **env) {
691 assert(c);
692 assert(c->parameter);
693 assert(c->type == CONDITION_PATH_EXISTS_GLOB);
694
695 return glob_exists(c->parameter) > 0;
696 }
697
698 static int condition_test_path_is_directory(Condition *c, char **env) {
699 assert(c);
700 assert(c->parameter);
701 assert(c->type == CONDITION_PATH_IS_DIRECTORY);
702
703 return is_dir(c->parameter, true) > 0;
704 }
705
706 static int condition_test_path_is_symbolic_link(Condition *c, char **env) {
707 assert(c);
708 assert(c->parameter);
709 assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK);
710
711 return is_symlink(c->parameter) > 0;
712 }
713
714 static int condition_test_path_is_mount_point(Condition *c, char **env) {
715 assert(c);
716 assert(c->parameter);
717 assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
718
719 return path_is_mount_point(c->parameter, NULL, AT_SYMLINK_FOLLOW) > 0;
720 }
721
722 static int condition_test_path_is_read_write(Condition *c, char **env) {
723 assert(c);
724 assert(c->parameter);
725 assert(c->type == CONDITION_PATH_IS_READ_WRITE);
726
727 return path_is_read_only_fs(c->parameter) <= 0;
728 }
729
730 static int condition_test_path_is_encrypted(Condition *c, char **env) {
731 int r;
732
733 assert(c);
734 assert(c->parameter);
735 assert(c->type == CONDITION_PATH_IS_ENCRYPTED);
736
737 r = path_is_encrypted(c->parameter);
738 if (r < 0 && r != -ENOENT)
739 log_debug_errno(r, "Failed to determine if '%s' is encrypted: %m", c->parameter);
740
741 return r > 0;
742 }
743
744 static int condition_test_directory_not_empty(Condition *c, char **env) {
745 int r;
746
747 assert(c);
748 assert(c->parameter);
749 assert(c->type == CONDITION_DIRECTORY_NOT_EMPTY);
750
751 r = dir_is_empty(c->parameter);
752 return r <= 0 && r != -ENOENT;
753 }
754
755 static int condition_test_file_not_empty(Condition *c, char **env) {
756 struct stat st;
757
758 assert(c);
759 assert(c->parameter);
760 assert(c->type == CONDITION_FILE_NOT_EMPTY);
761
762 return (stat(c->parameter, &st) >= 0 &&
763 S_ISREG(st.st_mode) &&
764 st.st_size > 0);
765 }
766
767 static int condition_test_file_is_executable(Condition *c, char **env) {
768 struct stat st;
769
770 assert(c);
771 assert(c->parameter);
772 assert(c->type == CONDITION_FILE_IS_EXECUTABLE);
773
774 return (stat(c->parameter, &st) >= 0 &&
775 S_ISREG(st.st_mode) &&
776 (st.st_mode & 0111));
777 }
778
779 static int condition_test_null(Condition *c, char **env) {
780 assert(c);
781 assert(c->type == CONDITION_NULL);
782
783 /* Note that during parsing we already evaluate the string and
784 * store it in c->negate */
785 return true;
786 }
787
788 int condition_test(Condition *c, char **env) {
789
790 static int (*const condition_tests[_CONDITION_TYPE_MAX])(Condition *c, char **env) = {
791 [CONDITION_PATH_EXISTS] = condition_test_path_exists,
792 [CONDITION_PATH_EXISTS_GLOB] = condition_test_path_exists_glob,
793 [CONDITION_PATH_IS_DIRECTORY] = condition_test_path_is_directory,
794 [CONDITION_PATH_IS_SYMBOLIC_LINK] = condition_test_path_is_symbolic_link,
795 [CONDITION_PATH_IS_MOUNT_POINT] = condition_test_path_is_mount_point,
796 [CONDITION_PATH_IS_READ_WRITE] = condition_test_path_is_read_write,
797 [CONDITION_PATH_IS_ENCRYPTED] = condition_test_path_is_encrypted,
798 [CONDITION_DIRECTORY_NOT_EMPTY] = condition_test_directory_not_empty,
799 [CONDITION_FILE_NOT_EMPTY] = condition_test_file_not_empty,
800 [CONDITION_FILE_IS_EXECUTABLE] = condition_test_file_is_executable,
801 [CONDITION_KERNEL_COMMAND_LINE] = condition_test_kernel_command_line,
802 [CONDITION_KERNEL_VERSION] = condition_test_kernel_version,
803 [CONDITION_VIRTUALIZATION] = condition_test_virtualization,
804 [CONDITION_SECURITY] = condition_test_security,
805 [CONDITION_CAPABILITY] = condition_test_capability,
806 [CONDITION_HOST] = condition_test_host,
807 [CONDITION_AC_POWER] = condition_test_ac_power,
808 [CONDITION_ARCHITECTURE] = condition_test_architecture,
809 [CONDITION_NEEDS_UPDATE] = condition_test_needs_update,
810 [CONDITION_FIRST_BOOT] = condition_test_first_boot,
811 [CONDITION_USER] = condition_test_user,
812 [CONDITION_GROUP] = condition_test_group,
813 [CONDITION_CONTROL_GROUP_CONTROLLER] = condition_test_control_group_controller,
814 [CONDITION_NULL] = condition_test_null,
815 [CONDITION_CPUS] = condition_test_cpus,
816 [CONDITION_MEMORY] = condition_test_memory,
817 [CONDITION_ENVIRONMENT] = condition_test_environment,
818 };
819
820 int r, b;
821
822 assert(c);
823 assert(c->type >= 0);
824 assert(c->type < _CONDITION_TYPE_MAX);
825
826 r = condition_tests[c->type](c, env);
827 if (r < 0) {
828 c->result = CONDITION_ERROR;
829 return r;
830 }
831
832 b = (r > 0) == !c->negate;
833 c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED;
834 return b;
835 }
836
837 bool condition_test_list(
838 Condition *first,
839 char **env,
840 condition_to_string_t to_string,
841 condition_test_logger_t logger,
842 void *userdata) {
843
844 Condition *c;
845 int triggered = -1;
846
847 assert(!!logger == !!to_string);
848
849 /* If the condition list is empty, then it is true */
850 if (!first)
851 return true;
852
853 /* Otherwise, if all of the non-trigger conditions apply and
854 * if any of the trigger conditions apply (unless there are
855 * none) we return true */
856 LIST_FOREACH(conditions, c, first) {
857 int r;
858
859 r = condition_test(c, env);
860
861 if (logger) {
862 const char *p = c->type == CONDITION_NULL ? "true" : c->parameter;
863 assert(p);
864
865 if (r < 0)
866 logger(userdata, LOG_WARNING, r, PROJECT_FILE, __LINE__, __func__,
867 "Couldn't determine result for %s=%s%s%s, assuming failed: %m",
868 to_string(c->type),
869 c->trigger ? "|" : "",
870 c->negate ? "!" : "",
871 p);
872 else
873 logger(userdata, LOG_DEBUG, 0, PROJECT_FILE, __LINE__, __func__,
874 "%s=%s%s%s %s.",
875 to_string(c->type),
876 c->trigger ? "|" : "",
877 c->negate ? "!" : "",
878 p,
879 condition_result_to_string(c->result));
880 }
881
882 if (!c->trigger && r <= 0)
883 return false;
884
885 if (c->trigger && triggered <= 0)
886 triggered = r > 0;
887 }
888
889 return triggered != 0;
890 }
891
892 void condition_dump(Condition *c, FILE *f, const char *prefix, condition_to_string_t to_string) {
893 assert(c);
894 assert(f);
895 assert(to_string);
896
897 prefix = strempty(prefix);
898
899 fprintf(f,
900 "%s\t%s: %s%s%s %s\n",
901 prefix,
902 to_string(c->type),
903 c->trigger ? "|" : "",
904 c->negate ? "!" : "",
905 c->parameter,
906 condition_result_to_string(c->result));
907 }
908
909 void condition_dump_list(Condition *first, FILE *f, const char *prefix, condition_to_string_t to_string) {
910 Condition *c;
911
912 LIST_FOREACH(conditions, c, first)
913 condition_dump(c, f, prefix, to_string);
914 }
915
916 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
917 [CONDITION_ARCHITECTURE] = "ConditionArchitecture",
918 [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
919 [CONDITION_HOST] = "ConditionHost",
920 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
921 [CONDITION_KERNEL_VERSION] = "ConditionKernelVersion",
922 [CONDITION_SECURITY] = "ConditionSecurity",
923 [CONDITION_CAPABILITY] = "ConditionCapability",
924 [CONDITION_AC_POWER] = "ConditionACPower",
925 [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
926 [CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
927 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
928 [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
929 [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
930 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
931 [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
932 [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
933 [CONDITION_PATH_IS_ENCRYPTED] = "ConditionPathIsEncrypted",
934 [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
935 [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
936 [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
937 [CONDITION_USER] = "ConditionUser",
938 [CONDITION_GROUP] = "ConditionGroup",
939 [CONDITION_CONTROL_GROUP_CONTROLLER] = "ConditionControlGroupController",
940 [CONDITION_NULL] = "ConditionNull",
941 [CONDITION_CPUS] = "ConditionCPUs",
942 [CONDITION_MEMORY] = "ConditionMemory",
943 [CONDITION_ENVIRONMENT] = "ConditionEnvironment",
944 };
945
946 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
947
948 static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
949 [CONDITION_ARCHITECTURE] = "AssertArchitecture",
950 [CONDITION_VIRTUALIZATION] = "AssertVirtualization",
951 [CONDITION_HOST] = "AssertHost",
952 [CONDITION_KERNEL_COMMAND_LINE] = "AssertKernelCommandLine",
953 [CONDITION_KERNEL_VERSION] = "AssertKernelVersion",
954 [CONDITION_SECURITY] = "AssertSecurity",
955 [CONDITION_CAPABILITY] = "AssertCapability",
956 [CONDITION_AC_POWER] = "AssertACPower",
957 [CONDITION_NEEDS_UPDATE] = "AssertNeedsUpdate",
958 [CONDITION_FIRST_BOOT] = "AssertFirstBoot",
959 [CONDITION_PATH_EXISTS] = "AssertPathExists",
960 [CONDITION_PATH_EXISTS_GLOB] = "AssertPathExistsGlob",
961 [CONDITION_PATH_IS_DIRECTORY] = "AssertPathIsDirectory",
962 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "AssertPathIsSymbolicLink",
963 [CONDITION_PATH_IS_MOUNT_POINT] = "AssertPathIsMountPoint",
964 [CONDITION_PATH_IS_READ_WRITE] = "AssertPathIsReadWrite",
965 [CONDITION_PATH_IS_ENCRYPTED] = "AssertPathIsEncrypted",
966 [CONDITION_DIRECTORY_NOT_EMPTY] = "AssertDirectoryNotEmpty",
967 [CONDITION_FILE_NOT_EMPTY] = "AssertFileNotEmpty",
968 [CONDITION_FILE_IS_EXECUTABLE] = "AssertFileIsExecutable",
969 [CONDITION_USER] = "AssertUser",
970 [CONDITION_GROUP] = "AssertGroup",
971 [CONDITION_CONTROL_GROUP_CONTROLLER] = "AssertControlGroupController",
972 [CONDITION_NULL] = "AssertNull",
973 [CONDITION_CPUS] = "AssertCPUs",
974 [CONDITION_MEMORY] = "AssertMemory",
975 [CONDITION_ENVIRONMENT] = "AssertEnvironment",
976 };
977
978 DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType);
979
980 static const char* const condition_result_table[_CONDITION_RESULT_MAX] = {
981 [CONDITION_UNTESTED] = "untested",
982 [CONDITION_SUCCEEDED] = "succeeded",
983 [CONDITION_FAILED] = "failed",
984 [CONDITION_ERROR] = "error",
985 };
986
987 DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult);