Debian Codegraph @

src/shared/condition.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "list.h"
5 #include "shared-forward.h"
6
7 typedef enum ConditionType {
8 CONDITION_ARCHITECTURE,
9 CONDITION_FIRMWARE,
10 CONDITION_VIRTUALIZATION,
11 CONDITION_HOST,
12 CONDITION_FRACTION,
13 CONDITION_KERNEL_COMMAND_LINE,
14 CONDITION_VERSION,
15 CONDITION_CREDENTIAL,
16 CONDITION_SECURITY,
17 CONDITION_CAPABILITY,
18 CONDITION_AC_POWER,
19 CONDITION_MEMORY,
20 CONDITION_CPUS,
21 CONDITION_ENVIRONMENT,
22 CONDITION_CPU_FEATURE,
23 CONDITION_OS_RELEASE,
24 CONDITION_MACHINE_TAG,
25 CONDITION_MEMORY_PRESSURE,
26 CONDITION_CPU_PRESSURE,
27 CONDITION_IO_PRESSURE,
28
29 CONDITION_NEEDS_UPDATE,
30 CONDITION_FIRST_BOOT,
31
32 CONDITION_PATH_EXISTS,
33 CONDITION_PATH_EXISTS_GLOB,
34 CONDITION_PATH_IS_DIRECTORY,
35 CONDITION_PATH_IS_SYMBOLIC_LINK,
36 CONDITION_PATH_IS_MOUNT_POINT,
37 CONDITION_PATH_IS_READ_WRITE,
38 CONDITION_PATH_IS_ENCRYPTED,
39 CONDITION_PATH_IS_SOCKET,
40 CONDITION_DIRECTORY_NOT_EMPTY,
41 CONDITION_FILE_NOT_EMPTY,
42 CONDITION_FILE_IS_EXECUTABLE,
43
44 CONDITION_USER,
45 CONDITION_GROUP,
46
47 CONDITION_CONTROL_GROUP_CONTROLLER,
48 CONDITION_KERNEL_MODULE_LOADED,
49
50 _CONDITION_TYPE_MAX,
51 _CONDITION_TYPE_INVALID = -EINVAL,
52 } ConditionType;
53
54 typedef enum ConditionResult {
55 CONDITION_UNTESTED,
56 CONDITION_SUCCEEDED,
57 CONDITION_FAILED,
58 CONDITION_ERROR,
59 _CONDITION_RESULT_MAX,
60 _CONDITION_RESULT_INVALID = -EINVAL,
61 } ConditionResult;
62
63 typedef struct Condition {
64 /* Use bitfields for ConditionType and ConditionResult to keep the whole struct in 32 bytes. */
65 ConditionType type:8;
66
67 bool trigger;
68 bool negate;
69
70 ConditionResult result:8;
71
72 char *parameter;
73
74 LIST_FIELDS(struct Condition, conditions);
75 } Condition;
76
77 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate);
78 Condition* condition_free(Condition *c);
79 Condition* condition_free_list_type(Condition *head, ConditionType type);
80 static inline Condition* condition_free_list(Condition *first) {
81 return condition_free_list_type(first, _CONDITION_TYPE_INVALID);
82 }
83
84 int condition_test(Condition *c, char **env);
85
86 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);
87 typedef const char* (*condition_to_string_t)(ConditionType t) _const_;
88 bool condition_test_list(Condition *first, char **env, condition_to_string_t to_string, condition_test_logger_t logger, void *userdata);
89
90 void condition_dump(Condition *c, FILE *f, const char *prefix, condition_to_string_t to_string);
91 void condition_dump_list(Condition *first, FILE *f, const char *prefix, condition_to_string_t to_string);
92
93 DECLARE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
94 void condition_types_list(void);
95
96 DECLARE_STRING_TABLE_LOOKUP(assert_type, ConditionType);
97 void assert_types_list(void);
98
99 DECLARE_STRING_TABLE_LOOKUP(condition_result, ConditionResult);
100
101 static inline bool condition_takes_path(ConditionType t) {
102 return IN_SET(t,
103 CONDITION_PATH_EXISTS,
104 CONDITION_PATH_EXISTS_GLOB,
105 CONDITION_PATH_IS_DIRECTORY,
106 CONDITION_PATH_IS_SYMBOLIC_LINK,
107 CONDITION_PATH_IS_MOUNT_POINT,
108 CONDITION_PATH_IS_READ_WRITE,
109 CONDITION_PATH_IS_ENCRYPTED,
110 CONDITION_PATH_IS_SOCKET,
111 CONDITION_DIRECTORY_NOT_EMPTY,
112 CONDITION_FILE_NOT_EMPTY,
113 CONDITION_FILE_IS_EXECUTABLE,
114 CONDITION_NEEDS_UPDATE);
115 }