chiark / gitweb /
condition: optionally test against type of virtualization (vm vs. container)
[elogind.git] / src / condition.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
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 #ifdef HAVE_SELINUX
28 #include <selinux/selinux.h>
29 #endif
30
31 #include "util.h"
32 #include "condition.h"
33 #include "virt.h"
34
35 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
36         Condition *c;
37
38         assert(type < _CONDITION_TYPE_MAX);
39
40         c = new0(Condition, 1);
41         if (!c)
42                 return NULL;
43
44         c->type = type;
45         c->trigger = trigger;
46         c->negate = negate;
47
48         if (parameter) {
49                 c->parameter = strdup(parameter);
50                 if (!c->parameter) {
51                         free(c);
52                         return NULL;
53                 }
54         }
55
56         return c;
57 }
58
59 void condition_free(Condition *c) {
60         assert(c);
61
62         free(c->parameter);
63         free(c);
64 }
65
66 void condition_free_list(Condition *first) {
67         Condition *c, *n;
68
69         LIST_FOREACH_SAFE(conditions, c, n, first)
70                 condition_free(c);
71 }
72
73 static bool test_kernel_command_line(const char *parameter) {
74         char *line, *w, *state, *word = NULL;
75         bool equal;
76         int r;
77         size_t l, pl;
78         bool found = false;
79
80         assert(parameter);
81
82         if (detect_container(NULL) > 0)
83                 return false;
84
85         r = read_one_line_file("/proc/cmdline", &line);
86         if (r < 0) {
87                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
88                 return false;
89         }
90
91         equal = !!strchr(parameter, '=');
92         pl = strlen(parameter);
93
94         FOREACH_WORD_QUOTED(w, l, line, state) {
95
96                 free(word);
97                 word = strndup(w, l);
98                 if (!word)
99                         break;
100
101                 if (equal) {
102                         if (streq(word, parameter)) {
103                                 found = true;
104                                 break;
105                         }
106                 } else {
107                         if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
108                                 found = true;
109                                 break;
110                         }
111                 }
112
113         }
114
115         free(word);
116         free(line);
117
118         return found;
119 }
120
121 static bool test_virtualization(const char *parameter) {
122         int b;
123         Virtualization v;
124         const char *id;
125
126         assert(parameter);
127
128         v = detect_virtualization(&id);
129         if (v < 0) {
130                 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-v));
131                 return false;
132         }
133
134         /* First, compare with yes/no */
135         b = parse_boolean(parameter);
136
137         if (v > 0 && b > 0)
138                 return true;
139
140         if (v == 0 && b == 0)
141                 return true;
142
143         /* Then, compare categorization */
144         if (v == VIRTUALIZATION_VM && streq(parameter, "vm"))
145                 return true;
146
147         if (v == VIRTUALIZATION_CONTAINER && streq(parameter, "container"))
148                 return true;
149
150         /* Finally compare id */
151         return streq(parameter, id);
152 }
153
154 static bool test_security(const char *parameter) {
155 #ifdef HAVE_SELINUX
156         if (streq(parameter, "selinux"))
157                 return is_selinux_enabled() > 0;
158 #endif
159         return false;
160 }
161
162 bool condition_test(Condition *c) {
163         assert(c);
164
165         switch(c->type) {
166
167         case CONDITION_PATH_EXISTS:
168                 return (access(c->parameter, F_OK) >= 0) == !c->negate;
169
170         case CONDITION_PATH_EXISTS_GLOB:
171                 return (glob_exists(c->parameter) > 0) == !c->negate;
172
173         case CONDITION_PATH_IS_DIRECTORY: {
174                 struct stat st;
175
176                 if (stat(c->parameter, &st) < 0)
177                         return c->negate;
178                 return S_ISDIR(st.st_mode) == !c->negate;
179         }
180
181         case CONDITION_PATH_IS_SYMBOLIC_LINK: {
182                 struct stat st;
183
184                 if (lstat(c->parameter, &st) < 0)
185                         return c->negate;
186                 return S_ISLNK(st.st_mode) == !c->negate;
187         }
188
189         case CONDITION_PATH_IS_MOUNT_POINT:
190                 return (path_is_mount_point(c->parameter, true) > 0) == !c->negate;
191
192         case CONDITION_DIRECTORY_NOT_EMPTY: {
193                 int k;
194
195                 k = dir_is_empty(c->parameter);
196                 return !(k == -ENOENT || k > 0) == !c->negate;
197         }
198
199         case CONDITION_FILE_IS_EXECUTABLE: {
200                 struct stat st;
201
202                 if (stat(c->parameter, &st) < 0)
203                         return c->negate;
204
205                 return (S_ISREG(st.st_mode) && (st.st_mode & 0111)) == !c->negate;
206         }
207
208         case CONDITION_KERNEL_COMMAND_LINE:
209                 return test_kernel_command_line(c->parameter) == !c->negate;
210
211         case CONDITION_VIRTUALIZATION:
212                 return test_virtualization(c->parameter) == !c->negate;
213
214         case CONDITION_SECURITY:
215                 return test_security(c->parameter) == !c->negate;
216
217         case CONDITION_NULL:
218                 return !c->negate;
219
220         default:
221                 assert_not_reached("Invalid condition type.");
222         }
223 }
224
225 bool condition_test_list(Condition *first) {
226         Condition *c;
227         int triggered = -1;
228
229         /* If the condition list is empty, then it is true */
230         if (!first)
231                 return true;
232
233         /* Otherwise, if all of the non-trigger conditions apply and
234          * if any of the trigger conditions apply (unless there are
235          * none) we return true */
236         LIST_FOREACH(conditions, c, first) {
237                 bool b;
238
239                 b = condition_test(c);
240
241                 if (!c->trigger && !b)
242                         return false;
243
244                 if (c->trigger && triggered <= 0)
245                         triggered = b;
246         }
247
248         return triggered != 0;
249 }
250
251 void condition_dump(Condition *c, FILE *f, const char *prefix) {
252         assert(c);
253         assert(f);
254
255         if (!prefix)
256                 prefix = "";
257
258         fprintf(f,
259                 "%s\t%s: %s%s%s\n",
260                 prefix,
261                 condition_type_to_string(c->type),
262                 c->trigger ? "|" : "",
263                 c->negate ? "!" : "",
264                 c->parameter);
265 }
266
267 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
268         Condition *c;
269
270         LIST_FOREACH(conditions, c, first)
271                 condition_dump(c, f, prefix);
272 }
273
274 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
275         [CONDITION_PATH_EXISTS] = "ConditionPathExists",
276         [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
277         [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
278         [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
279         [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
280         [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
281         [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
282         [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
283         [CONDITION_SECURITY] = "ConditionSecurity",
284         [CONDITION_NULL] = "ConditionNull"
285 };
286
287 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);