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 negate) {
33 c = new0(Condition, 1);
38 if (!(c->parameter = strdup(parameter))) {
46 void condition_free(Condition *c) {
53 void condition_free_list(Condition *first) {
56 LIST_FOREACH_SAFE(conditions, c, n, first)
60 static bool test_kernel_command_line(const char *parameter) {
61 char *line, *w, *state, *word = NULL;
67 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
68 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
72 equal = !!strchr(parameter, '=');
73 pl = strlen(parameter);
75 FOREACH_WORD_QUOTED(w, l, line, state) {
78 if (!(word = strndup(w, l)))
82 if (streq(word, parameter)) {
87 if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
101 bool condition_test(Condition *c) {
106 case CONDITION_PATH_EXISTS:
107 return (access(c->parameter, F_OK) >= 0) == !c->negate;
109 case CONDITION_KERNEL_COMMAND_LINE:
110 return !!test_kernel_command_line(c->parameter) == !c->negate;
116 assert_not_reached("Invalid condition type.");
120 bool condition_test_list(Condition *first) {
123 /* If the condition list is empty, then it is true */
127 /* Otherwise, if any of the conditions apply we return true */
128 LIST_FOREACH(conditions, c, first)
129 if (condition_test(c))
135 void condition_dump(Condition *c, FILE *f, const char *prefix) {
145 condition_type_to_string(c->type),
146 c->negate ? "!" : "",
150 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
153 LIST_FOREACH(conditions, c, first)
154 condition_dump(c, f, prefix);
157 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
158 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
159 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
160 [CONDITION_NULL] = "ConditionNull"
163 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);