]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/condition.c
util: detect page size runtime.
[thirdparty/systemd.git] / src / condition.c
CommitLineData
52661efd
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
f23c09b0 6 Copyright 2010 Lennart Poettering
52661efd
LP
7
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.
12
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.
17
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/>.
20***/
21
22#include <stdlib.h>
23#include <errno.h>
24#include <string.h>
25#include <unistd.h>
26
27#include "util.h"
28#include "condition.h"
29
267632f0 30Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
52661efd
LP
31 Condition *c;
32
33 c = new0(Condition, 1);
34 c->type = type;
267632f0 35 c->trigger = trigger;
52661efd
LP
36 c->negate = negate;
37
d257ddef
LP
38 if (parameter)
39 if (!(c->parameter = strdup(parameter))) {
40 free(c);
41 return NULL;
42 }
52661efd
LP
43
44 return c;
45}
46
47void condition_free(Condition *c) {
48 assert(c);
49
50 free(c->parameter);
51 free(c);
52}
53
54void condition_free_list(Condition *first) {
55 Condition *c, *n;
56
57 LIST_FOREACH_SAFE(conditions, c, n, first)
58 condition_free(c);
59}
60
61static bool test_kernel_command_line(const char *parameter) {
62 char *line, *w, *state, *word = NULL;
63 bool equal;
64 int r;
65 size_t l, pl;
66 bool found = false;
67
039655a4
LP
68 assert(parameter);
69
2fc97846
LP
70 if (detect_virtualization(NULL) > 0)
71 return false;
72
52661efd
LP
73 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
74 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
75 return false;
76 }
77
78 equal = !!strchr(parameter, '=');
79 pl = strlen(parameter);
80
81 FOREACH_WORD_QUOTED(w, l, line, state) {
82
83 free(word);
84 if (!(word = strndup(w, l)))
85 break;
86
87 if (equal) {
88 if (streq(word, parameter)) {
89 found = true;
90 break;
91 }
92 } else {
93 if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
94 found = true;
95 break;
96 }
97 }
98
99 }
100
101 free(word);
102 free(line);
103
104 return found;
105}
106
039655a4
LP
107static bool test_virtualization(const char *parameter) {
108 int r, b;
109 const char *id;
110
111 assert(parameter);
112
113 if ((r = detect_virtualization(&id)) < 0) {
114 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-r));
115 return false;
116 }
117
118 b = parse_boolean(parameter);
119
120 if (r > 0 && b > 0)
121 return true;
122
123 if (r == 0 && b == 0)
124 return true;
125
126 return streq(parameter, id);
127}
128
52661efd
LP
129bool condition_test(Condition *c) {
130 assert(c);
131
132 switch(c->type) {
133
134 case CONDITION_PATH_EXISTS:
135 return (access(c->parameter, F_OK) >= 0) == !c->negate;
136
36af55d9
LP
137 case CONDITION_DIRECTORY_NOT_EMPTY: {
138 int k;
139
140 k = dir_is_empty(c->parameter);
141 return !(k == -ENOENT || k > 0) == !c->negate;
142 }
143
52661efd 144 case CONDITION_KERNEL_COMMAND_LINE:
cb40c15e 145 return test_kernel_command_line(c->parameter) == !c->negate;
52661efd 146
039655a4 147 case CONDITION_VIRTUALIZATION:
cb40c15e 148 return test_virtualization(c->parameter) == !c->negate;
039655a4 149
d257ddef
LP
150 case CONDITION_NULL:
151 return !c->negate;
152
52661efd
LP
153 default:
154 assert_not_reached("Invalid condition type.");
155 }
156}
157
158bool condition_test_list(Condition *first) {
159 Condition *c;
267632f0 160 int triggered = -1;
52661efd
LP
161
162 /* If the condition list is empty, then it is true */
163 if (!first)
164 return true;
165
267632f0
LP
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) {
170 bool b;
171
172 b = condition_test(c);
173
174 if (!c->trigger && !b)
175 return false;
176
177 if (c->trigger && triggered <= 0)
178 triggered = b;
179 }
52661efd 180
267632f0 181 return triggered != 0;
52661efd
LP
182}
183
184void condition_dump(Condition *c, FILE *f, const char *prefix) {
185 assert(c);
186 assert(f);
187
188 if (!prefix)
189 prefix = "";
190
191 fprintf(f,
267632f0 192 "%s%s: %s%s%s\n",
52661efd
LP
193 prefix,
194 condition_type_to_string(c->type),
267632f0 195 c->trigger ? "|" : "",
52661efd
LP
196 c->negate ? "!" : "",
197 c->parameter);
198}
199
200void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
201 Condition *c;
202
203 LIST_FOREACH(conditions, c, first)
204 condition_dump(c, f, prefix);
205}
206
207static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
208 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
d257ddef
LP
209 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
210 [CONDITION_NULL] = "ConditionNull"
52661efd
LP
211};
212
213DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);