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"
34 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
37 assert(type < _CONDITION_TYPE_MAX);
39 if (!(c = new0(Condition, 1)))
47 if (!(c->parameter = strdup(parameter))) {
55 void condition_free(Condition *c) {
62 void condition_free_list(Condition *first) {
65 LIST_FOREACH_SAFE(conditions, c, n, first)
69 static bool test_kernel_command_line(const char *parameter) {
70 char *line, *w, *state, *word = NULL;
78 if (detect_virtualization(NULL) > 0)
81 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
82 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
86 equal = !!strchr(parameter, '=');
87 pl = strlen(parameter);
89 FOREACH_WORD_QUOTED(w, l, line, state) {
92 if (!(word = strndup(w, l)))
96 if (streq(word, parameter)) {
101 if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
115 static bool test_virtualization(const char *parameter) {
121 if ((r = detect_virtualization(&id)) < 0) {
122 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-r));
126 b = parse_boolean(parameter);
131 if (r == 0 && b == 0)
134 return streq(parameter, id);
137 static bool test_security(const char *parameter) {
139 if (streq(parameter, "selinux"))
140 return is_selinux_enabled() > 0;
145 bool condition_test(Condition *c) {
150 case CONDITION_PATH_EXISTS:
151 return (access(c->parameter, F_OK) >= 0) == !c->negate;
153 case CONDITION_PATH_EXISTS_GLOB:
154 return (glob_exists(c->parameter) > 0) == !c->negate;
156 case CONDITION_PATH_IS_DIRECTORY: {
159 if (lstat(c->parameter, &st) < 0)
161 return S_ISDIR(st.st_mode) == !c->negate;
164 case CONDITION_DIRECTORY_NOT_EMPTY: {
167 k = dir_is_empty(c->parameter);
168 return !(k == -ENOENT || k > 0) == !c->negate;
171 case CONDITION_FILE_IS_EXECUTABLE: {
174 if (lstat(c->parameter, &st) < 0)
177 return (S_ISREG(st.st_mode) && (st.st_mode & 0111)) == !c->negate;
180 case CONDITION_KERNEL_COMMAND_LINE:
181 return test_kernel_command_line(c->parameter) == !c->negate;
183 case CONDITION_VIRTUALIZATION:
184 return test_virtualization(c->parameter) == !c->negate;
186 case CONDITION_SECURITY:
187 return test_security(c->parameter) == !c->negate;
193 assert_not_reached("Invalid condition type.");
197 bool condition_test_list(Condition *first) {
201 /* If the condition list is empty, then it is true */
205 /* Otherwise, if all of the non-trigger conditions apply and
206 * if any of the trigger conditions apply (unless there are
207 * none) we return true */
208 LIST_FOREACH(conditions, c, first) {
211 b = condition_test(c);
213 if (!c->trigger && !b)
216 if (c->trigger && triggered <= 0)
220 return triggered != 0;
223 void condition_dump(Condition *c, FILE *f, const char *prefix) {
233 condition_type_to_string(c->type),
234 c->trigger ? "|" : "",
235 c->negate ? "!" : "",
239 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
242 LIST_FOREACH(conditions, c, first)
243 condition_dump(c, f, prefix);
246 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
247 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
248 [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
249 [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
250 [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
251 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
252 [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
253 [CONDITION_SECURITY] = "ConditionSecurity",
254 [CONDITION_NULL] = "ConditionNull"
257 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);