]>
Commit | Line | Data |
---|---|---|
b77c08e0 TG |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
b77c08e0 | 20 | #include <errno.h> |
a8fbdf54 | 21 | #include <fcntl.h> |
84ac7bea | 22 | #include <fnmatch.h> |
a8fbdf54 | 23 | #include <limits.h> |
84ac7bea | 24 | #include <stdlib.h> |
b77c08e0 | 25 | #include <string.h> |
a8fbdf54 | 26 | #include <sys/stat.h> |
c465a29f | 27 | #include <sys/types.h> |
a8fbdf54 | 28 | #include <time.h> |
b77c08e0 | 29 | #include <unistd.h> |
b77c08e0 | 30 | |
d1bddcec | 31 | #include "sd-id128.h" |
84ac7bea | 32 | |
b5efdb8a | 33 | #include "alloc-util.h" |
d1bddcec | 34 | #include "apparmor-util.h" |
84ac7bea | 35 | #include "architecture.h" |
430f0182 | 36 | #include "audit-util.h" |
2822da4f | 37 | #include "cap-list.h" |
3ffd4af2 | 38 | #include "condition.h" |
84ac7bea | 39 | #include "extract-word.h" |
3ffd4af2 | 40 | #include "fd-util.h" |
fb8b0869 | 41 | #include "fileio.h" |
7d50b32a | 42 | #include "glob-util.h" |
958b66ea | 43 | #include "hostname-util.h" |
84ac7bea | 44 | #include "ima-util.h" |
a8fbdf54 TA |
45 | #include "list.h" |
46 | #include "macro.h" | |
4349cd7c | 47 | #include "mount-util.h" |
6bedfcbb | 48 | #include "parse-util.h" |
84ac7bea | 49 | #include "path-util.h" |
4e731273 | 50 | #include "proc-cmdline.h" |
df0ff127 | 51 | #include "process-util.h" |
84ac7bea LP |
52 | #include "selinux-util.h" |
53 | #include "smack-util.h" | |
8fcde012 | 54 | #include "stat-util.h" |
8b43440b | 55 | #include "string-table.h" |
07630cea | 56 | #include "string-util.h" |
c465a29f | 57 | #include "user-util.h" |
84ac7bea LP |
58 | #include "util.h" |
59 | #include "virt.h" | |
b77c08e0 TG |
60 | |
61 | Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) { | |
62 | Condition *c; | |
d1bddcec | 63 | int r; |
b77c08e0 | 64 | |
b80ba1da | 65 | assert(type >= 0); |
b77c08e0 | 66 | assert(type < _CONDITION_TYPE_MAX); |
8a9c6071 | 67 | assert((!parameter) == (type == CONDITION_NULL)); |
b77c08e0 TG |
68 | |
69 | c = new0(Condition, 1); | |
70 | if (!c) | |
71 | return NULL; | |
72 | ||
73 | c->type = type; | |
74 | c->trigger = trigger; | |
75 | c->negate = negate; | |
76 | ||
d1bddcec LP |
77 | r = free_and_strdup(&c->parameter, parameter); |
78 | if (r < 0) { | |
79 | free(c); | |
80 | return NULL; | |
b77c08e0 TG |
81 | } |
82 | ||
83 | return c; | |
84 | } | |
85 | ||
86 | void condition_free(Condition *c) { | |
87 | assert(c); | |
88 | ||
89 | free(c->parameter); | |
90 | free(c); | |
91 | } | |
92 | ||
447021aa | 93 | Condition* condition_free_list(Condition *first) { |
b77c08e0 TG |
94 | Condition *c, *n; |
95 | ||
96 | LIST_FOREACH_SAFE(conditions, c, n, first) | |
97 | condition_free(c); | |
447021aa ZJS |
98 | |
99 | return NULL; | |
b77c08e0 TG |
100 | } |
101 | ||
a4705396 | 102 | static int condition_test_kernel_command_line(Condition *c) { |
07318c29 LP |
103 | _cleanup_free_ char *line = NULL; |
104 | const char *p; | |
b77c08e0 TG |
105 | bool equal; |
106 | int r; | |
b77c08e0 TG |
107 | |
108 | assert(c); | |
109 | assert(c->parameter); | |
110 | assert(c->type == CONDITION_KERNEL_COMMAND_LINE); | |
111 | ||
112 | r = proc_cmdline(&line); | |
113 | if (r < 0) | |
592fd144 | 114 | return r; |
b77c08e0 TG |
115 | |
116 | equal = !!strchr(c->parameter, '='); | |
07318c29 | 117 | |
c58bd76a | 118 | for (p = line;;) { |
07318c29 LP |
119 | _cleanup_free_ char *word = NULL; |
120 | bool found; | |
121 | ||
12ba2c44 | 122 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX); |
592fd144 LP |
123 | if (r < 0) |
124 | return r; | |
125 | if (r == 0) | |
a4705396 | 126 | break; |
07318c29 LP |
127 | |
128 | if (equal) | |
129 | found = streq(word, c->parameter); | |
130 | else { | |
131 | const char *f; | |
132 | ||
133 | f = startswith(word, c->parameter); | |
4c701096 | 134 | found = f && IN_SET(*f, 0, '='); |
b77c08e0 TG |
135 | } |
136 | ||
07318c29 | 137 | if (found) |
a4705396 | 138 | return true; |
b77c08e0 | 139 | } |
b77c08e0 | 140 | |
a4705396 | 141 | return false; |
b77c08e0 TG |
142 | } |
143 | ||
c465a29f FS |
144 | static int condition_test_user(Condition *c) { |
145 | uid_t id; | |
146 | int r; | |
147 | _cleanup_free_ char *username = NULL; | |
148 | const char *u; | |
149 | ||
150 | assert(c); | |
151 | assert(c->parameter); | |
152 | assert(c->type == CONDITION_USER); | |
153 | ||
154 | r = parse_uid(c->parameter, &id); | |
155 | if (r >= 0) | |
156 | return id == getuid() || id == geteuid(); | |
157 | ||
534bab66 FS |
158 | if (streq("@system", c->parameter)) |
159 | return getuid() <= SYSTEM_UID_MAX || geteuid() <= SYSTEM_UID_MAX; | |
160 | ||
c465a29f FS |
161 | username = getusername_malloc(); |
162 | if (!username) | |
163 | return -ENOMEM; | |
164 | ||
165 | if (streq(username, c->parameter)) | |
166 | return 1; | |
167 | ||
df0ff127 | 168 | if (getpid_cached() == 1) |
c465a29f FS |
169 | return streq(c->parameter, "root"); |
170 | ||
171 | u = c->parameter; | |
172 | r = get_user_creds(&u, &id, NULL, NULL, NULL); | |
173 | if (r < 0) | |
174 | return 0; | |
175 | ||
176 | return id == getuid() || id == geteuid(); | |
177 | } | |
178 | ||
179 | static int condition_test_group(Condition *c) { | |
180 | gid_t id; | |
181 | int r; | |
182 | ||
183 | assert(c); | |
184 | assert(c->parameter); | |
185 | assert(c->type == CONDITION_GROUP); | |
186 | ||
187 | r = parse_gid(c->parameter, &id); | |
188 | if (r >= 0) | |
189 | return in_gid(id); | |
190 | ||
191 | /* Avoid any NSS lookups if we are PID1 */ | |
df0ff127 | 192 | if (getpid_cached() == 1) |
c465a29f FS |
193 | return streq(c->parameter, "root"); |
194 | ||
195 | return in_group(c->parameter) > 0; | |
196 | } | |
197 | ||
a4705396 | 198 | static int condition_test_virtualization(Condition *c) { |
248fab74 | 199 | int b, v; |
b77c08e0 TG |
200 | |
201 | assert(c); | |
202 | assert(c->parameter); | |
203 | assert(c->type == CONDITION_VIRTUALIZATION); | |
204 | ||
239a5707 ZJS |
205 | if (streq(c->parameter, "private-users")) |
206 | return running_in_userns(); | |
207 | ||
75f86906 | 208 | v = detect_virtualization(); |
592fd144 LP |
209 | if (v < 0) |
210 | return v; | |
b77c08e0 TG |
211 | |
212 | /* First, compare with yes/no */ | |
213 | b = parse_boolean(c->parameter); | |
0809d774 ZJS |
214 | if (b >= 0) |
215 | return b == !!v; | |
b77c08e0 TG |
216 | |
217 | /* Then, compare categorization */ | |
0809d774 ZJS |
218 | if (streq(c->parameter, "vm")) |
219 | return VIRTUALIZATION_IS_VM(v); | |
b77c08e0 | 220 | |
0809d774 ZJS |
221 | if (streq(c->parameter, "container")) |
222 | return VIRTUALIZATION_IS_CONTAINER(v); | |
b77c08e0 TG |
223 | |
224 | /* Finally compare id */ | |
75f86906 | 225 | return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v)); |
b77c08e0 TG |
226 | } |
227 | ||
a4705396 | 228 | static int condition_test_architecture(Condition *c) { |
592fd144 | 229 | int a, b; |
099524d7 LP |
230 | |
231 | assert(c); | |
232 | assert(c->parameter); | |
233 | assert(c->type == CONDITION_ARCHITECTURE); | |
234 | ||
235 | a = uname_architecture(); | |
236 | if (a < 0) | |
592fd144 | 237 | return a; |
099524d7 LP |
238 | |
239 | if (streq(c->parameter, "native")) | |
240 | b = native_architecture(); | |
2cb62395 | 241 | else { |
099524d7 | 242 | b = architecture_from_string(c->parameter); |
2cb62395 LP |
243 | if (b < 0) /* unknown architecture? Then it's definitely not ours */ |
244 | return false; | |
245 | } | |
099524d7 | 246 | |
a4705396 | 247 | return a == b; |
099524d7 LP |
248 | } |
249 | ||
a4705396 | 250 | static int condition_test_host(Condition *c) { |
dc92e62c | 251 | _cleanup_free_ char *h = NULL; |
b77c08e0 | 252 | sd_id128_t x, y; |
b77c08e0 | 253 | int r; |
b77c08e0 TG |
254 | |
255 | assert(c); | |
256 | assert(c->parameter); | |
257 | assert(c->type == CONDITION_HOST); | |
258 | ||
259 | if (sd_id128_from_string(c->parameter, &x) >= 0) { | |
260 | ||
261 | r = sd_id128_get_machine(&y); | |
262 | if (r < 0) | |
592fd144 | 263 | return r; |
b77c08e0 | 264 | |
a4705396 | 265 | return sd_id128_equal(x, y); |
b77c08e0 TG |
266 | } |
267 | ||
268 | h = gethostname_malloc(); | |
269 | if (!h) | |
592fd144 | 270 | return -ENOMEM; |
b77c08e0 | 271 | |
a4705396 | 272 | return fnmatch(c->parameter, h, FNM_CASEFOLD) == 0; |
b77c08e0 TG |
273 | } |
274 | ||
a4705396 | 275 | static int condition_test_ac_power(Condition *c) { |
b77c08e0 TG |
276 | int r; |
277 | ||
278 | assert(c); | |
279 | assert(c->parameter); | |
280 | assert(c->type == CONDITION_AC_POWER); | |
281 | ||
282 | r = parse_boolean(c->parameter); | |
283 | if (r < 0) | |
592fd144 | 284 | return r; |
b77c08e0 | 285 | |
a4705396 | 286 | return (on_ac_power() != 0) == !!r; |
b77c08e0 TG |
287 | } |
288 | ||
d1bddcec LP |
289 | static int condition_test_security(Condition *c) { |
290 | assert(c); | |
291 | assert(c->parameter); | |
292 | assert(c->type == CONDITION_SECURITY); | |
293 | ||
294 | if (streq(c->parameter, "selinux")) | |
6d395665 | 295 | return mac_selinux_use(); |
d1bddcec | 296 | if (streq(c->parameter, "smack")) |
a4705396 | 297 | return mac_smack_use(); |
d1bddcec | 298 | if (streq(c->parameter, "apparmor")) |
a4705396 | 299 | return mac_apparmor_use(); |
d1bddcec | 300 | if (streq(c->parameter, "audit")) |
a4705396 | 301 | return use_audit(); |
d1bddcec | 302 | if (streq(c->parameter, "ima")) |
a4705396 | 303 | return use_ima(); |
d1bddcec | 304 | |
a4705396 | 305 | return false; |
d1bddcec LP |
306 | } |
307 | ||
308 | static int condition_test_capability(Condition *c) { | |
309 | _cleanup_fclose_ FILE *f = NULL; | |
2822da4f | 310 | int value; |
d1bddcec LP |
311 | char line[LINE_MAX]; |
312 | unsigned long long capabilities = -1; | |
313 | ||
314 | assert(c); | |
315 | assert(c->parameter); | |
316 | assert(c->type == CONDITION_CAPABILITY); | |
317 | ||
318 | /* If it's an invalid capability, we don't have it */ | |
2822da4f LP |
319 | value = capability_from_name(c->parameter); |
320 | if (value < 0) | |
d1bddcec LP |
321 | return -EINVAL; |
322 | ||
323 | /* If it's a valid capability we default to assume | |
324 | * that we have it */ | |
325 | ||
326 | f = fopen("/proc/self/status", "re"); | |
327 | if (!f) | |
328 | return -errno; | |
329 | ||
330 | while (fgets(line, sizeof(line), f)) { | |
331 | truncate_nl(line); | |
332 | ||
333 | if (startswith(line, "CapBnd:")) { | |
334 | (void) sscanf(line+7, "%llx", &capabilities); | |
335 | break; | |
336 | } | |
337 | } | |
338 | ||
a4705396 | 339 | return !!(capabilities & (1ULL << value)); |
d1bddcec LP |
340 | } |
341 | ||
342 | static int condition_test_needs_update(Condition *c) { | |
343 | const char *p; | |
344 | struct stat usr, other; | |
345 | ||
346 | assert(c); | |
347 | assert(c->parameter); | |
348 | assert(c->type == CONDITION_NEEDS_UPDATE); | |
349 | ||
350 | /* If the file system is read-only we shouldn't suggest an update */ | |
351 | if (path_is_read_only_fs(c->parameter) > 0) | |
a4705396 | 352 | return false; |
d1bddcec LP |
353 | |
354 | /* Any other failure means we should allow the condition to be true, | |
96d49011 | 355 | * so that we rather invoke too many update tools than too |
d1bddcec LP |
356 | * few. */ |
357 | ||
358 | if (!path_is_absolute(c->parameter)) | |
a4705396 | 359 | return true; |
d1bddcec | 360 | |
63c372cb | 361 | p = strjoina(c->parameter, "/.updated"); |
d1bddcec | 362 | if (lstat(p, &other) < 0) |
a4705396 | 363 | return true; |
d1bddcec LP |
364 | |
365 | if (lstat("/usr/", &usr) < 0) | |
a4705396 | 366 | return true; |
d1bddcec | 367 | |
fb8b0869 IS |
368 | /* |
369 | * First, compare seconds as they are always accurate... | |
370 | */ | |
371 | if (usr.st_mtim.tv_sec != other.st_mtim.tv_sec) | |
372 | return usr.st_mtim.tv_sec > other.st_mtim.tv_sec; | |
373 | ||
374 | /* | |
375 | * ...then compare nanoseconds. | |
376 | * | |
377 | * A false positive is only possible when /usr's nanoseconds > 0 | |
378 | * (otherwise /usr cannot be strictly newer than the target file) | |
379 | * AND the target file's nanoseconds == 0 | |
380 | * (otherwise the filesystem supports nsec timestamps, see stat(2)). | |
381 | */ | |
382 | if (usr.st_mtim.tv_nsec > 0 && other.st_mtim.tv_nsec == 0) { | |
383 | _cleanup_free_ char *timestamp_str = NULL; | |
384 | uint64_t timestamp; | |
385 | int r; | |
386 | ||
ec2ebfd5 | 387 | r = parse_env_file(p, NULL, "TIMESTAMP_NSEC", ×tamp_str, NULL); |
fb8b0869 | 388 | if (r < 0) { |
ec2ebfd5 | 389 | log_error_errno(r, "Failed to parse timestamp file '%s', using mtime: %m", p); |
fb8b0869 IS |
390 | return true; |
391 | } else if (r == 0) { | |
392 | log_debug("No data in timestamp file '%s', using mtime", p); | |
393 | return true; | |
394 | } | |
395 | ||
396 | r = safe_atou64(timestamp_str, ×tamp); | |
397 | if (r < 0) { | |
ec2ebfd5 | 398 | log_error_errno(r, "Failed to parse timestamp value '%s' in file '%s', using mtime: %m", timestamp_str, p); |
fb8b0869 IS |
399 | return true; |
400 | } | |
401 | ||
ec2ebfd5 | 402 | timespec_store(&other.st_mtim, timestamp); |
fb8b0869 IS |
403 | } |
404 | ||
405 | return usr.st_mtim.tv_nsec > other.st_mtim.tv_nsec; | |
d1bddcec LP |
406 | } |
407 | ||
408 | static int condition_test_first_boot(Condition *c) { | |
409 | int r; | |
410 | ||
411 | assert(c); | |
412 | assert(c->parameter); | |
413 | assert(c->type == CONDITION_FIRST_BOOT); | |
414 | ||
415 | r = parse_boolean(c->parameter); | |
416 | if (r < 0) | |
417 | return r; | |
418 | ||
a4705396 | 419 | return (access("/run/systemd/first-boot", F_OK) >= 0) == !!r; |
d1bddcec LP |
420 | } |
421 | ||
422 | static int condition_test_path_exists(Condition *c) { | |
423 | assert(c); | |
424 | assert(c->parameter); | |
425 | assert(c->type == CONDITION_PATH_EXISTS); | |
426 | ||
a4705396 | 427 | return access(c->parameter, F_OK) >= 0; |
d1bddcec LP |
428 | } |
429 | ||
430 | static int condition_test_path_exists_glob(Condition *c) { | |
431 | assert(c); | |
432 | assert(c->parameter); | |
433 | assert(c->type == CONDITION_PATH_EXISTS_GLOB); | |
434 | ||
a4705396 | 435 | return glob_exists(c->parameter) > 0; |
d1bddcec LP |
436 | } |
437 | ||
438 | static int condition_test_path_is_directory(Condition *c) { | |
439 | assert(c); | |
440 | assert(c->parameter); | |
441 | assert(c->type == CONDITION_PATH_IS_DIRECTORY); | |
442 | ||
a4705396 | 443 | return is_dir(c->parameter, true) > 0; |
d1bddcec LP |
444 | } |
445 | ||
446 | static int condition_test_path_is_symbolic_link(Condition *c) { | |
447 | assert(c); | |
448 | assert(c->parameter); | |
449 | assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK); | |
450 | ||
a4705396 | 451 | return is_symlink(c->parameter) > 0; |
d1bddcec LP |
452 | } |
453 | ||
454 | static int condition_test_path_is_mount_point(Condition *c) { | |
455 | assert(c); | |
456 | assert(c->parameter); | |
457 | assert(c->type == CONDITION_PATH_IS_MOUNT_POINT); | |
458 | ||
e1873695 | 459 | return path_is_mount_point(c->parameter, NULL, AT_SYMLINK_FOLLOW) > 0; |
d1bddcec LP |
460 | } |
461 | ||
462 | static int condition_test_path_is_read_write(Condition *c) { | |
463 | assert(c); | |
464 | assert(c->parameter); | |
465 | assert(c->type == CONDITION_PATH_IS_READ_WRITE); | |
466 | ||
a4705396 | 467 | return path_is_read_only_fs(c->parameter) <= 0; |
d1bddcec LP |
468 | } |
469 | ||
470 | static int condition_test_directory_not_empty(Condition *c) { | |
471 | int r; | |
472 | ||
473 | assert(c); | |
474 | assert(c->parameter); | |
475 | assert(c->type == CONDITION_DIRECTORY_NOT_EMPTY); | |
476 | ||
477 | r = dir_is_empty(c->parameter); | |
a4705396 | 478 | return r <= 0 && r != -ENOENT; |
d1bddcec LP |
479 | } |
480 | ||
481 | static int condition_test_file_not_empty(Condition *c) { | |
482 | struct stat st; | |
483 | ||
484 | assert(c); | |
485 | assert(c->parameter); | |
486 | assert(c->type == CONDITION_FILE_NOT_EMPTY); | |
487 | ||
488 | return (stat(c->parameter, &st) >= 0 && | |
489 | S_ISREG(st.st_mode) && | |
a4705396 | 490 | st.st_size > 0); |
d1bddcec LP |
491 | } |
492 | ||
493 | static int condition_test_file_is_executable(Condition *c) { | |
494 | struct stat st; | |
495 | ||
496 | assert(c); | |
497 | assert(c->parameter); | |
498 | assert(c->type == CONDITION_FILE_IS_EXECUTABLE); | |
499 | ||
500 | return (stat(c->parameter, &st) >= 0 && | |
501 | S_ISREG(st.st_mode) && | |
a4705396 | 502 | (st.st_mode & 0111)); |
d1bddcec LP |
503 | } |
504 | ||
505 | static int condition_test_null(Condition *c) { | |
506 | assert(c); | |
d1bddcec LP |
507 | assert(c->type == CONDITION_NULL); |
508 | ||
509 | /* Note that during parsing we already evaluate the string and | |
510 | * store it in c->negate */ | |
a4705396 | 511 | return true; |
d1bddcec LP |
512 | } |
513 | ||
514 | int condition_test(Condition *c) { | |
515 | ||
516 | static int (*const condition_tests[_CONDITION_TYPE_MAX])(Condition *c) = { | |
517 | [CONDITION_PATH_EXISTS] = condition_test_path_exists, | |
518 | [CONDITION_PATH_EXISTS_GLOB] = condition_test_path_exists_glob, | |
519 | [CONDITION_PATH_IS_DIRECTORY] = condition_test_path_is_directory, | |
520 | [CONDITION_PATH_IS_SYMBOLIC_LINK] = condition_test_path_is_symbolic_link, | |
521 | [CONDITION_PATH_IS_MOUNT_POINT] = condition_test_path_is_mount_point, | |
522 | [CONDITION_PATH_IS_READ_WRITE] = condition_test_path_is_read_write, | |
523 | [CONDITION_DIRECTORY_NOT_EMPTY] = condition_test_directory_not_empty, | |
524 | [CONDITION_FILE_NOT_EMPTY] = condition_test_file_not_empty, | |
525 | [CONDITION_FILE_IS_EXECUTABLE] = condition_test_file_is_executable, | |
526 | [CONDITION_KERNEL_COMMAND_LINE] = condition_test_kernel_command_line, | |
527 | [CONDITION_VIRTUALIZATION] = condition_test_virtualization, | |
528 | [CONDITION_SECURITY] = condition_test_security, | |
529 | [CONDITION_CAPABILITY] = condition_test_capability, | |
530 | [CONDITION_HOST] = condition_test_host, | |
531 | [CONDITION_AC_POWER] = condition_test_ac_power, | |
532 | [CONDITION_ARCHITECTURE] = condition_test_architecture, | |
533 | [CONDITION_NEEDS_UPDATE] = condition_test_needs_update, | |
534 | [CONDITION_FIRST_BOOT] = condition_test_first_boot, | |
c465a29f FS |
535 | [CONDITION_USER] = condition_test_user, |
536 | [CONDITION_GROUP] = condition_test_group, | |
d1bddcec LP |
537 | [CONDITION_NULL] = condition_test_null, |
538 | }; | |
cc50ef13 LP |
539 | |
540 | int r, b; | |
d1bddcec LP |
541 | |
542 | assert(c); | |
543 | assert(c->type >= 0); | |
544 | assert(c->type < _CONDITION_TYPE_MAX); | |
545 | ||
a4705396 | 546 | r = condition_tests[c->type](c); |
cc50ef13 LP |
547 | if (r < 0) { |
548 | c->result = CONDITION_ERROR; | |
a4705396 | 549 | return r; |
cc50ef13 | 550 | } |
a4705396 | 551 | |
cc50ef13 LP |
552 | b = (r > 0) == !c->negate; |
553 | c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED; | |
554 | return b; | |
d1bddcec LP |
555 | } |
556 | ||
59fccdc5 | 557 | void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) { |
b77c08e0 TG |
558 | assert(c); |
559 | assert(f); | |
560 | ||
561 | if (!prefix) | |
562 | prefix = ""; | |
563 | ||
564 | fprintf(f, | |
565 | "%s\t%s: %s%s%s %s\n", | |
566 | prefix, | |
59fccdc5 | 567 | to_string(c->type), |
b77c08e0 TG |
568 | c->trigger ? "|" : "", |
569 | c->negate ? "!" : "", | |
570 | c->parameter, | |
cc50ef13 | 571 | condition_result_to_string(c->result)); |
b77c08e0 TG |
572 | } |
573 | ||
59fccdc5 | 574 | void condition_dump_list(Condition *first, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) { |
b77c08e0 TG |
575 | Condition *c; |
576 | ||
577 | LIST_FOREACH(conditions, c, first) | |
59fccdc5 | 578 | condition_dump(c, f, prefix, to_string); |
b77c08e0 TG |
579 | } |
580 | ||
581 | static const char* const condition_type_table[_CONDITION_TYPE_MAX] = { | |
651c3318 LP |
582 | [CONDITION_ARCHITECTURE] = "ConditionArchitecture", |
583 | [CONDITION_VIRTUALIZATION] = "ConditionVirtualization", | |
584 | [CONDITION_HOST] = "ConditionHost", | |
585 | [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine", | |
586 | [CONDITION_SECURITY] = "ConditionSecurity", | |
587 | [CONDITION_CAPABILITY] = "ConditionCapability", | |
588 | [CONDITION_AC_POWER] = "ConditionACPower", | |
589 | [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate", | |
590 | [CONDITION_FIRST_BOOT] = "ConditionFirstBoot", | |
b77c08e0 TG |
591 | [CONDITION_PATH_EXISTS] = "ConditionPathExists", |
592 | [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob", | |
593 | [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory", | |
594 | [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink", | |
595 | [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint", | |
596 | [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite", | |
597 | [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty", | |
598 | [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty", | |
599 | [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable", | |
c465a29f FS |
600 | [CONDITION_USER] = "ConditionUser", |
601 | [CONDITION_GROUP] = "ConditionGroup", | |
b77c08e0 TG |
602 | [CONDITION_NULL] = "ConditionNull" |
603 | }; | |
604 | ||
605 | DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType); | |
cc50ef13 | 606 | |
59fccdc5 | 607 | static const char* const assert_type_table[_CONDITION_TYPE_MAX] = { |
651c3318 LP |
608 | [CONDITION_ARCHITECTURE] = "AssertArchitecture", |
609 | [CONDITION_VIRTUALIZATION] = "AssertVirtualization", | |
610 | [CONDITION_HOST] = "AssertHost", | |
611 | [CONDITION_KERNEL_COMMAND_LINE] = "AssertKernelCommandLine", | |
612 | [CONDITION_SECURITY] = "AssertSecurity", | |
613 | [CONDITION_CAPABILITY] = "AssertCapability", | |
614 | [CONDITION_AC_POWER] = "AssertACPower", | |
615 | [CONDITION_NEEDS_UPDATE] = "AssertNeedsUpdate", | |
616 | [CONDITION_FIRST_BOOT] = "AssertFirstBoot", | |
59fccdc5 LP |
617 | [CONDITION_PATH_EXISTS] = "AssertPathExists", |
618 | [CONDITION_PATH_EXISTS_GLOB] = "AssertPathExistsGlob", | |
619 | [CONDITION_PATH_IS_DIRECTORY] = "AssertPathIsDirectory", | |
620 | [CONDITION_PATH_IS_SYMBOLIC_LINK] = "AssertPathIsSymbolicLink", | |
621 | [CONDITION_PATH_IS_MOUNT_POINT] = "AssertPathIsMountPoint", | |
622 | [CONDITION_PATH_IS_READ_WRITE] = "AssertPathIsReadWrite", | |
623 | [CONDITION_DIRECTORY_NOT_EMPTY] = "AssertDirectoryNotEmpty", | |
624 | [CONDITION_FILE_NOT_EMPTY] = "AssertFileNotEmpty", | |
625 | [CONDITION_FILE_IS_EXECUTABLE] = "AssertFileIsExecutable", | |
c465a29f FS |
626 | [CONDITION_USER] = "AssertUser", |
627 | [CONDITION_GROUP] = "AssertGroup", | |
59fccdc5 LP |
628 | [CONDITION_NULL] = "AssertNull" |
629 | }; | |
630 | ||
631 | DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType); | |
632 | ||
cc50ef13 LP |
633 | static const char* const condition_result_table[_CONDITION_RESULT_MAX] = { |
634 | [CONDITION_UNTESTED] = "untested", | |
635 | [CONDITION_SUCCEEDED] = "succeeded", | |
636 | [CONDITION_FAILED] = "failed", | |
637 | [CONDITION_ERROR] = "error", | |
638 | }; | |
639 | ||
640 | DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult); |