]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/analyze/analyze-condition.c
core: add missing conditions/asserts to unit file parsing
[thirdparty/systemd.git] / src / analyze / analyze-condition.c
CommitLineData
edfea9fe
ZJS
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <stdlib.h>
4
5#include "analyze-condition.h"
6#include "condition.h"
7#include "conf-parser.h"
8#include "load-fragment.h"
9#include "service.h"
10
edfea9fe 11static int parse_condition(Unit *u, const char *line) {
625a1640
LP
12 assert(u);
13 assert(line);
14
15 for (ConditionType t = 0; t < _CONDITION_TYPE_MAX; t++) {
16 ConfigParserCallback callback;
17 Condition **target;
18 const char *p, *name;
19
20 name = condition_type_to_string(t);
21 p = startswith(line, name);
22 if (p)
23 target = &u->conditions;
24 else {
25 name = assert_type_to_string(t);
26 p = startswith(line, name);
27 if (!p)
28 continue;
29
30 target = &u->asserts;
31 }
08ef6886 32
edfea9fe 33 p += strspn(p, WHITESPACE);
08ef6886 34
edfea9fe 35 if (*p != '=')
08ef6886
LP
36 continue;
37 p++;
edfea9fe 38
08ef6886 39 p += strspn(p, WHITESPACE);
edfea9fe 40
625a1640
LP
41 if (t == CONDITION_NULL) /* deprecated, but we should still parse this for now */
42 callback = config_parse_unit_condition_null;
43 else if (condition_takes_path(t))
44 callback = config_parse_unit_condition_path;
45 else
46 callback = config_parse_unit_condition_string;
47
48 return callback(NULL, "(cmdline)", 0, NULL, 0, name, t, p, target, u);
edfea9fe
ZJS
49 }
50
51 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot parse \"%s\".", line);
52}
53
54_printf_(7, 8)
55static int log_helper(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) {
56 Unit *u = userdata;
57 va_list ap;
58 int r;
59
60 assert(u);
61
62 /* "upgrade" debug messages */
63 level = MIN(LOG_INFO, level);
64
65 va_start(ap, format);
66 r = log_object_internalv(level, error, file, line, func,
67 NULL,
68 u->id,
69 NULL,
70 NULL,
71 format, ap);
72 va_end(ap);
73
74 return r;
75}
76
77int verify_conditions(char **lines, UnitFileScope scope) {
78 _cleanup_(manager_freep) Manager *m = NULL;
79 Unit *u;
80 char **line;
81 int r, q = 1;
82
83 r = manager_new(scope, MANAGER_TEST_RUN_MINIMAL, &m);
84 if (r < 0)
85 return log_error_errno(r, "Failed to initialize manager: %m");
86
87 log_debug("Starting manager...");
88 r = manager_startup(m, NULL, NULL);
89 if (r < 0)
90 return r;
91
92 r = unit_new_for_name(m, sizeof(Service), "test.service", &u);
93 if (r < 0)
94 return log_error_errno(r, "Failed to create test.service: %m");
95
96 STRV_FOREACH(line, lines) {
97 r = parse_condition(u, *line);
98 if (r < 0)
99 return r;
100 }
101
a0b191b7 102 r = condition_test_list(u->asserts, environ, assert_type_to_string, log_helper, u);
edfea9fe
ZJS
103 if (u->asserts)
104 log_notice("Asserts %s.", r > 0 ? "succeeded" : "failed");
105
a0b191b7 106 q = condition_test_list(u->conditions, environ, condition_type_to_string, log_helper, u);
edfea9fe
ZJS
107 if (u->conditions)
108 log_notice("Conditions %s.", q > 0 ? "succeeded" : "failed");
109
110 return r > 0 && q > 0 ? 0 : -EIO;
111}