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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/statvfs.h>
29 #include <systemd/sd-id128.h>
31 #include "condition-util.h"
33 #include "path-util.h"
36 #include "architecture.h"
38 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
41 assert(type < _CONDITION_TYPE_MAX);
43 c = new0(Condition, 1);
52 c->parameter = strdup(parameter);
62 void condition_free(Condition *c) {
69 void condition_free_list(Condition *first) {
72 LIST_FOREACH_SAFE(conditions, c, n, first)
76 bool condition_test_kernel_command_line(Condition *c) {
77 char *line, *word = NULL;
78 const char *w, *state;
86 assert(c->type == CONDITION_KERNEL_COMMAND_LINE);
88 r = proc_cmdline(&line);
90 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
94 equal = !!strchr(c->parameter, '=');
95 pl = strlen(c->parameter);
97 FOREACH_WORD_QUOTED(w, l, line, state) {
100 word = strndup(w, l);
105 if (streq(word, c->parameter)) {
110 if (startswith(word, c->parameter) && (word[pl] == '=' || word[pl] == 0)) {
121 return found == !c->negate;
124 bool condition_test_virtualization(Condition *c) {
129 assert(c->parameter);
130 assert(c->type == CONDITION_VIRTUALIZATION);
132 v = detect_virtualization(&id);
134 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-v));
138 /* First, compare with yes/no */
139 b = parse_boolean(c->parameter);
144 if (v == 0 && b == 0)
147 /* Then, compare categorization */
148 if (v == VIRTUALIZATION_VM && streq(c->parameter, "vm"))
151 if (v == VIRTUALIZATION_CONTAINER && streq(c->parameter, "container"))
154 /* Finally compare id */
155 return (v > 0 && streq(c->parameter, id)) == !c->negate;
158 bool condition_test_architecture(Condition *c) {
162 assert(c->parameter);
163 assert(c->type == CONDITION_ARCHITECTURE);
165 a = uname_architecture();
169 if (streq(c->parameter, "native"))
170 b = native_architecture();
172 b = architecture_from_string(c->parameter);
177 return (a == b) == !c->negate;
180 bool condition_test_host(Condition *c) {
181 _cleanup_free_ char *h = NULL;
186 assert(c->parameter);
187 assert(c->type == CONDITION_HOST);
189 if (sd_id128_from_string(c->parameter, &x) >= 0) {
191 r = sd_id128_get_machine(&y);
195 return sd_id128_equal(x, y) == !c->negate;
198 h = gethostname_malloc();
202 return (fnmatch(c->parameter, h, FNM_CASEFOLD) == 0) == !c->negate;
205 bool condition_test_ac_power(Condition *c) {
209 assert(c->parameter);
210 assert(c->type == CONDITION_AC_POWER);
212 r = parse_boolean(c->parameter);
216 return ((on_ac_power() != 0) == !!r) == !c->negate;
219 void condition_dump(Condition *c, FILE *f, const char *prefix) {
227 "%s\t%s: %s%s%s %s\n",
229 condition_type_to_string(c->type),
230 c->trigger ? "|" : "",
231 c->negate ? "!" : "",
233 c->state < 0 ? "failed" : c->state > 0 ? "succeeded" : "untested");
236 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
239 LIST_FOREACH(conditions, c, first)
240 condition_dump(c, f, prefix);
243 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
244 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
245 [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
246 [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
247 [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
248 [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
249 [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
250 [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
251 [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
252 [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
253 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
254 [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
255 [CONDITION_SECURITY] = "ConditionSecurity",
256 [CONDITION_CAPABILITY] = "ConditionCapability",
257 [CONDITION_HOST] = "ConditionHost",
258 [CONDITION_AC_POWER] = "ConditionACPower",
259 [CONDITION_ARCHITECTURE] = "ConditionArchitecture",
260 [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
261 [CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
262 [CONDITION_NULL] = "ConditionNull"
265 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);