]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/analyze/analyze-condition.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / analyze / analyze-condition.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "analyze.h"
6 #include "analyze-condition.h"
7 #include "analyze-verify-util.h"
8 #include "condition.h"
9 #include "conf-parser.h"
10 #include "load-fragment.h"
11 #include "service.h"
12
13 static int parse_condition(Unit *u, const char *line) {
14 assert(u);
15 assert(line);
16
17 for (ConditionType t = 0; t < _CONDITION_TYPE_MAX; t++) {
18 ConfigParserCallback callback;
19 Condition **target;
20 const char *p, *name;
21
22 name = condition_type_to_string(t);
23 p = startswith(line, name);
24 if (p)
25 target = &u->conditions;
26 else {
27 name = assert_type_to_string(t);
28 p = startswith(line, name);
29 if (!p)
30 continue;
31
32 target = &u->asserts;
33 }
34
35 p += strspn(p, WHITESPACE);
36
37 if (*p != '=')
38 continue;
39 p++;
40
41 p += strspn(p, WHITESPACE);
42
43 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);
49 }
50
51 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot parse \"%s\".", line);
52 }
53
54 _printf_(7, 8)
55 static int log_helper(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) {
56 Unit *u = ASSERT_PTR(userdata);
57 va_list ap;
58 int r;
59
60 /* "upgrade" debug messages */
61 level = MIN(LOG_INFO, level);
62
63 va_start(ap, format);
64 r = log_object_internalv(level, error, file, line, func,
65 NULL,
66 u->id,
67 NULL,
68 NULL,
69 format, ap);
70 va_end(ap);
71
72 return r;
73 }
74
75 static int verify_conditions(char **lines, LookupScope scope, const char *unit, const char *root) {
76 _cleanup_(manager_freep) Manager *m = NULL;
77 Unit *u;
78 int r, q = 1;
79
80 if (unit) {
81 _cleanup_strv_free_ char **filenames = NULL;
82 _cleanup_free_ char *var = NULL;
83
84 filenames = strv_new(unit);
85 if (!filenames)
86 return log_oom();
87
88 r = verify_generate_path(&var, filenames);
89 if (r < 0)
90 return log_error_errno(r, "Failed to generate unit load path: %m");
91
92 assert_se(set_unit_path(var) >= 0);
93 }
94
95 r = manager_new(scope, MANAGER_TEST_RUN_MINIMAL, &m);
96 if (r < 0)
97 return log_error_errno(r, "Failed to initialize manager: %m");
98
99 log_debug("Starting manager...");
100 r = manager_startup(m, /* serialization= */ NULL, /* fds= */ NULL, root);
101 if (r < 0)
102 return r;
103
104 if (unit) {
105 _cleanup_free_ char *prepared = NULL;
106
107 r = verify_prepare_filename(unit, &prepared);
108 if (r < 0)
109 return log_error_errno(r, "Failed to prepare filename %s: %m", unit);
110
111 r = manager_load_startable_unit_or_warn(m, NULL, prepared, &u);
112 if (r < 0)
113 return r;
114 } else {
115 r = unit_new_for_name(m, sizeof(Service), "test.service", &u);
116 if (r < 0)
117 return log_error_errno(r, "Failed to create test.service: %m");
118
119 STRV_FOREACH(line, lines) {
120 r = parse_condition(u, *line);
121 if (r < 0)
122 return r;
123 }
124 }
125
126 r = condition_test_list(u->asserts, environ, assert_type_to_string, log_helper, u);
127 if (u->asserts)
128 log_notice("Asserts %s.", r > 0 ? "succeeded" : "failed");
129
130 q = condition_test_list(u->conditions, environ, condition_type_to_string, log_helper, u);
131 if (u->conditions)
132 log_notice("Conditions %s.", q > 0 ? "succeeded" : "failed");
133
134 return r > 0 && q > 0 ? 0 : -EIO;
135 }
136
137 int verb_condition(int argc, char *argv[], void *userdata) {
138 int r;
139
140 r = verify_conditions(strv_skip(argv, 1), arg_scope, arg_unit, arg_root);
141 if (r < 0)
142 return r;
143
144 return EXIT_SUCCESS;
145 }