1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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_FIRMWARE,
13         CONDITION_VIRTUALIZATION,
14         CONDITION_HOST,
15         CONDITION_KERNEL_COMMAND_LINE,
16         CONDITION_KERNEL_VERSION,
17         CONDITION_SECURITY,
18         CONDITION_CAPABILITY,
19         CONDITION_AC_POWER,
20         CONDITION_MEMORY,
21         CONDITION_CPUS,
22         CONDITION_ENVIRONMENT,
23         CONDITION_CPU_FEATURE,
24         CONDITION_OS_RELEASE,
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_DIRECTORY_NOT_EMPTY,
40         CONDITION_FILE_NOT_EMPTY,
41         CONDITION_FILE_IS_EXECUTABLE,
42 
43         CONDITION_USER,
44         CONDITION_GROUP,
45 
46         CONDITION_CONTROL_GROUP_CONTROLLER,
47 
48         _CONDITION_TYPE_MAX,
49         _CONDITION_TYPE_INVALID = -EINVAL,
50 } ConditionType;
51 
52 typedef enum ConditionResult {
53         CONDITION_UNTESTED,
54         CONDITION_SUCCEEDED,
55         CONDITION_FAILED,
56         CONDITION_ERROR,
57         _CONDITION_RESULT_MAX,
58         _CONDITION_RESULT_INVALID = -EINVAL,
59 } ConditionResult;
60 
61 typedef struct Condition {
62         ConditionType type:8;
63 
64         bool trigger:1;
65         bool negate:1;
66 
67         ConditionResult result:6;
68 
69         char *parameter;
70 
71         LIST_FIELDS(struct Condition, conditions);
72 } Condition;
73 
74 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate);
75 Condition* condition_free(Condition *c);
76 Condition* condition_free_list_type(Condition *first, ConditionType type);
condition_free_list(Condition * first)77 static inline Condition* condition_free_list(Condition *first) {
78         return condition_free_list_type(first, _CONDITION_TYPE_INVALID);
79 }
80 
81 int condition_test(Condition *c, char **env);
82 
83 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);
84 typedef const char* (*condition_to_string_t)(ConditionType t) _const_;
85 bool condition_test_list(Condition *first, char **env, condition_to_string_t to_string, condition_test_logger_t logger, void *userdata);
86 
87 void condition_dump(Condition *c, FILE *f, const char *prefix, condition_to_string_t to_string);
88 void condition_dump_list(Condition *c, FILE *f, const char *prefix, condition_to_string_t to_string);
89 
90 const char* condition_type_to_string(ConditionType t) _const_;
91 ConditionType condition_type_from_string(const char *s) _pure_;
92 
93 const char* assert_type_to_string(ConditionType t) _const_;
94 ConditionType assert_type_from_string(const char *s) _pure_;
95 
96 const char* condition_result_to_string(ConditionResult r) _const_;
97 ConditionResult condition_result_from_string(const char *s) _pure_;
98 
condition_takes_path(ConditionType t)99 static inline bool condition_takes_path(ConditionType t) {
100         return IN_SET(t,
101                       CONDITION_PATH_EXISTS,
102                       CONDITION_PATH_EXISTS_GLOB,
103                       CONDITION_PATH_IS_DIRECTORY,
104                       CONDITION_PATH_IS_SYMBOLIC_LINK,
105                       CONDITION_PATH_IS_MOUNT_POINT,
106                       CONDITION_PATH_IS_READ_WRITE,
107                       CONDITION_PATH_IS_ENCRYPTED,
108                       CONDITION_DIRECTORY_NOT_EMPTY,
109                       CONDITION_FILE_NOT_EMPTY,
110                       CONDITION_FILE_IS_EXECUTABLE,
111                       CONDITION_NEEDS_UPDATE);
112 }
113