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 "condition.h"
30 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
33 c = new0(Condition, 1);
39 if (!(c->parameter = strdup(parameter))) {
47 void condition_free(Condition *c) {
54 void condition_free_list(Condition *first) {
57 LIST_FOREACH_SAFE(conditions, c, n, first)
61 static bool test_kernel_command_line(const char *parameter) {
62 char *line, *w, *state, *word = NULL;
70 if (detect_virtualization(NULL) > 0)
73 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
74 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
78 equal = !!strchr(parameter, '=');
79 pl = strlen(parameter);
81 FOREACH_WORD_QUOTED(w, l, line, state) {
84 if (!(word = strndup(w, l)))
88 if (streq(word, parameter)) {
93 if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
107 static bool test_virtualization(const char *parameter) {
113 if ((r = detect_virtualization(&id)) < 0) {
114 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-r));
118 b = parse_boolean(parameter);
123 if (r == 0 && b == 0)
126 return streq(parameter, id);
129 bool condition_test(Condition *c) {
134 case CONDITION_PATH_EXISTS:
135 return (access(c->parameter, F_OK) >= 0) == !c->negate;
137 case CONDITION_DIRECTORY_NOT_EMPTY: {
140 k = dir_is_empty(c->parameter);
141 return !(k == -ENOENT || k > 0) == !c->negate;
144 case CONDITION_KERNEL_COMMAND_LINE:
145 return test_kernel_command_line(c->parameter) == !c->negate;
147 case CONDITION_VIRTUALIZATION:
148 return test_virtualization(c->parameter) == !c->negate;
154 assert_not_reached("Invalid condition type.");
158 bool condition_test_list(Condition *first) {
162 /* If the condition list is empty, then it is true */
166 /* Otherwise, if all of the non-trigger conditions apply and
167 * if any of the trigger conditions apply (unless there are
168 * none) we return true */
169 LIST_FOREACH(conditions, c, first) {
172 b = condition_test(c);
174 if (!c->trigger && !b)
177 if (c->trigger && triggered <= 0)
181 return triggered != 0;
184 void condition_dump(Condition *c, FILE *f, const char *prefix) {
194 condition_type_to_string(c->type),
195 c->trigger ? "|" : "",
196 c->negate ? "!" : "",
200 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
203 LIST_FOREACH(conditions, c, first)
204 condition_dump(c, f, prefix);
207 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
208 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
209 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
210 [CONDITION_NULL] = "ConditionNull"
213 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);