1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include <selinux/selinux.h>
32 #include "condition.h"
35 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
38 assert(type < _CONDITION_TYPE_MAX);
40 c = new0(Condition, 1);
49 c->parameter = strdup(parameter);
59 void condition_free(Condition *c) {
66 void condition_free_list(Condition *first) {
69 LIST_FOREACH_SAFE(conditions, c, n, first)
73 static bool test_kernel_command_line(const char *parameter) {
74 char *line, *w, *state, *word = NULL;
82 if (detect_container(NULL) > 0)
85 r = read_one_line_file("/proc/cmdline", &line);
87 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
91 equal = !!strchr(parameter, '=');
92 pl = strlen(parameter);
94 FOREACH_WORD_QUOTED(w, l, line, state) {
102 if (streq(word, parameter)) {
107 if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
121 static bool test_virtualization(const char *parameter) {
128 v = detect_virtualization(&id);
130 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-v));
134 /* First, compare with yes/no */
135 b = parse_boolean(parameter);
140 if (v == 0 && b == 0)
143 /* Then, compare categorization */
144 if (v == VIRTUALIZATION_VM && streq(parameter, "vm"))
147 if (v == VIRTUALIZATION_CONTAINER && streq(parameter, "container"))
150 /* Finally compare id */
151 return streq(parameter, id);
154 static bool test_security(const char *parameter) {
156 if (streq(parameter, "selinux"))
157 return is_selinux_enabled() > 0;
162 bool condition_test(Condition *c) {
167 case CONDITION_PATH_EXISTS:
168 return (access(c->parameter, F_OK) >= 0) == !c->negate;
170 case CONDITION_PATH_EXISTS_GLOB:
171 return (glob_exists(c->parameter) > 0) == !c->negate;
173 case CONDITION_PATH_IS_DIRECTORY: {
176 if (stat(c->parameter, &st) < 0)
178 return S_ISDIR(st.st_mode) == !c->negate;
181 case CONDITION_PATH_IS_SYMBOLIC_LINK: {
184 if (lstat(c->parameter, &st) < 0)
186 return S_ISLNK(st.st_mode) == !c->negate;
189 case CONDITION_PATH_IS_MOUNT_POINT:
190 return (path_is_mount_point(c->parameter, true) > 0) == !c->negate;
192 case CONDITION_DIRECTORY_NOT_EMPTY: {
195 k = dir_is_empty(c->parameter);
196 return !(k == -ENOENT || k > 0) == !c->negate;
199 case CONDITION_FILE_IS_EXECUTABLE: {
202 if (stat(c->parameter, &st) < 0)
205 return (S_ISREG(st.st_mode) && (st.st_mode & 0111)) == !c->negate;
208 case CONDITION_KERNEL_COMMAND_LINE:
209 return test_kernel_command_line(c->parameter) == !c->negate;
211 case CONDITION_VIRTUALIZATION:
212 return test_virtualization(c->parameter) == !c->negate;
214 case CONDITION_SECURITY:
215 return test_security(c->parameter) == !c->negate;
221 assert_not_reached("Invalid condition type.");
225 bool condition_test_list(Condition *first) {
229 /* If the condition list is empty, then it is true */
233 /* Otherwise, if all of the non-trigger conditions apply and
234 * if any of the trigger conditions apply (unless there are
235 * none) we return true */
236 LIST_FOREACH(conditions, c, first) {
239 b = condition_test(c);
241 if (!c->trigger && !b)
244 if (c->trigger && triggered <= 0)
248 return triggered != 0;
251 void condition_dump(Condition *c, FILE *f, const char *prefix) {
261 condition_type_to_string(c->type),
262 c->trigger ? "|" : "",
263 c->negate ? "!" : "",
267 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
270 LIST_FOREACH(conditions, c, first)
271 condition_dump(c, f, prefix);
274 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
275 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
276 [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
277 [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
278 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
279 [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
280 [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
281 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
282 [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
283 [CONDITION_SECURITY] = "ConditionSecurity",
284 [CONDITION_NULL] = "ConditionNull"
287 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);