]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/condition.h
condition: introduce condition_takes_path()
[thirdparty/systemd.git] / src / shared / condition.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <stdbool.h>
24 #include <stdio.h>
25
26 #include "list.h"
27 #include "macro.h"
28
29 typedef enum ConditionType {
30 CONDITION_ARCHITECTURE,
31 CONDITION_VIRTUALIZATION,
32 CONDITION_HOST,
33 CONDITION_KERNEL_COMMAND_LINE,
34 CONDITION_KERNEL_VERSION,
35 CONDITION_SECURITY,
36 CONDITION_CAPABILITY,
37 CONDITION_AC_POWER,
38
39 CONDITION_NEEDS_UPDATE,
40 CONDITION_FIRST_BOOT,
41
42 CONDITION_PATH_EXISTS,
43 CONDITION_PATH_EXISTS_GLOB,
44 CONDITION_PATH_IS_DIRECTORY,
45 CONDITION_PATH_IS_SYMBOLIC_LINK,
46 CONDITION_PATH_IS_MOUNT_POINT,
47 CONDITION_PATH_IS_READ_WRITE,
48 CONDITION_DIRECTORY_NOT_EMPTY,
49 CONDITION_FILE_NOT_EMPTY,
50 CONDITION_FILE_IS_EXECUTABLE,
51
52 CONDITION_NULL,
53
54 CONDITION_USER,
55 CONDITION_GROUP,
56
57 CONDITION_CONTROL_GROUP_CONTROLLER,
58
59 _CONDITION_TYPE_MAX,
60 _CONDITION_TYPE_INVALID = -1
61 } ConditionType;
62
63 typedef enum ConditionResult {
64 CONDITION_UNTESTED,
65 CONDITION_SUCCEEDED,
66 CONDITION_FAILED,
67 CONDITION_ERROR,
68 _CONDITION_RESULT_MAX,
69 _CONDITION_RESULT_INVALID = -1
70 } ConditionResult;
71
72 typedef struct Condition {
73 ConditionType type:8;
74
75 bool trigger:1;
76 bool negate:1;
77
78 ConditionResult result:6;
79
80 char *parameter;
81
82 LIST_FIELDS(struct Condition, conditions);
83 } Condition;
84
85 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate);
86 void condition_free(Condition *c);
87 Condition* condition_free_list(Condition *c);
88
89 int condition_test(Condition *c);
90
91 void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t));
92 void condition_dump_list(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t));
93
94 const char* condition_type_to_string(ConditionType t) _const_;
95 ConditionType condition_type_from_string(const char *s) _pure_;
96
97 const char* assert_type_to_string(ConditionType t) _const_;
98 ConditionType assert_type_from_string(const char *s) _pure_;
99
100 const char* condition_result_to_string(ConditionResult r) _const_;
101 ConditionResult condition_result_from_string(const char *s) _pure_;
102
103 static inline bool condition_takes_path(ConditionType t) {
104 return IN_SET(t,
105 CONDITION_PATH_EXISTS,
106 CONDITION_PATH_EXISTS_GLOB,
107 CONDITION_PATH_IS_DIRECTORY,
108 CONDITION_PATH_IS_SYMBOLIC_LINK,
109 CONDITION_PATH_IS_MOUNT_POINT,
110 CONDITION_PATH_IS_READ_WRITE,
111 CONDITION_DIRECTORY_NOT_EMPTY,
112 CONDITION_FILE_NOT_EMPTY,
113 CONDITION_FILE_IS_EXECUTABLE,
114 CONDITION_NEEDS_UPDATE);
115 }