Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : #pragma once
3 :
4 : #include <stdbool.h>
5 : #include <stdio.h>
6 :
7 : #include "list.h"
8 : #include "macro.h"
9 :
10 : typedef enum ConditionType {
11 : CONDITION_ARCHITECTURE,
12 : CONDITION_VIRTUALIZATION,
13 : CONDITION_HOST,
14 : CONDITION_KERNEL_COMMAND_LINE,
15 : CONDITION_KERNEL_VERSION,
16 : CONDITION_SECURITY,
17 : CONDITION_CAPABILITY,
18 : CONDITION_AC_POWER,
19 : CONDITION_MEMORY,
20 : CONDITION_CPUS,
21 :
22 : CONDITION_NEEDS_UPDATE,
23 : CONDITION_FIRST_BOOT,
24 :
25 : CONDITION_PATH_EXISTS,
26 : CONDITION_PATH_EXISTS_GLOB,
27 : CONDITION_PATH_IS_DIRECTORY,
28 : CONDITION_PATH_IS_SYMBOLIC_LINK,
29 : CONDITION_PATH_IS_MOUNT_POINT,
30 : CONDITION_PATH_IS_READ_WRITE,
31 : CONDITION_DIRECTORY_NOT_EMPTY,
32 : CONDITION_FILE_NOT_EMPTY,
33 : CONDITION_FILE_IS_EXECUTABLE,
34 :
35 : CONDITION_NULL,
36 :
37 : CONDITION_USER,
38 : CONDITION_GROUP,
39 :
40 : CONDITION_CONTROL_GROUP_CONTROLLER,
41 :
42 : _CONDITION_TYPE_MAX,
43 : _CONDITION_TYPE_INVALID = -1
44 : } ConditionType;
45 :
46 : typedef enum ConditionResult {
47 : CONDITION_UNTESTED,
48 : CONDITION_SUCCEEDED,
49 : CONDITION_FAILED,
50 : CONDITION_ERROR,
51 : _CONDITION_RESULT_MAX,
52 : _CONDITION_RESULT_INVALID = -1
53 : } ConditionResult;
54 :
55 : typedef struct Condition {
56 : ConditionType type:8;
57 :
58 : bool trigger:1;
59 : bool negate:1;
60 :
61 : ConditionResult result:6;
62 :
63 : char *parameter;
64 :
65 : LIST_FIELDS(struct Condition, conditions);
66 : } Condition;
67 :
68 : Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate);
69 : void condition_free(Condition *c);
70 : Condition* condition_free_list_type(Condition *first, ConditionType type);
71 4346 : static inline Condition* condition_free_list(Condition *first) {
72 4346 : return condition_free_list_type(first, _CONDITION_TYPE_INVALID);
73 : }
74 :
75 : int condition_test(Condition *c);
76 : typedef int (*condition_test_logger_t)(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) _printf_(7, 8);
77 : bool condition_test_list(Condition *first, const char *(*to_string)(ConditionType t), condition_test_logger_t logger, void *userdata);
78 :
79 : void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t));
80 : void condition_dump_list(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t));
81 :
82 : const char* condition_type_to_string(ConditionType t) _const_;
83 : ConditionType condition_type_from_string(const char *s) _pure_;
84 :
85 : const char* assert_type_to_string(ConditionType t) _const_;
86 : ConditionType assert_type_from_string(const char *s) _pure_;
87 :
88 : const char* condition_result_to_string(ConditionResult r) _const_;
89 : ConditionResult condition_result_from_string(const char *s) _pure_;
90 :
91 0 : static inline bool condition_takes_path(ConditionType t) {
92 0 : return IN_SET(t,
93 : CONDITION_PATH_EXISTS,
94 : CONDITION_PATH_EXISTS_GLOB,
95 : CONDITION_PATH_IS_DIRECTORY,
96 : CONDITION_PATH_IS_SYMBOLIC_LINK,
97 : CONDITION_PATH_IS_MOUNT_POINT,
98 : CONDITION_PATH_IS_READ_WRITE,
99 : CONDITION_DIRECTORY_NOT_EMPTY,
100 : CONDITION_FILE_NOT_EMPTY,
101 : CONDITION_FILE_IS_EXECUTABLE,
102 : CONDITION_NEEDS_UPDATE);
103 : }
|