Debian Codegraph @

src/analyze/analyze-condition.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "analyze.h"
7 #include "analyze-condition.h"
8 #include "analyze-verify-util.h"
9 #include "condition.h"
10 #include "load-fragment.h"
11 #include "manager.h"
12 #include "service.h"
13 #include "string-util.h"
14 #include "strv.h"
15
16 static int parse_condition(Unit *u, const char *line) {
17 assert(u);
18 assert(line);
19
20 line = skip_leading_chars(line, /* bad= */ NULL);
21
22 const char *eq = strchr(line, '=');
23 if (!eq)
24 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot parse \"%s\".", line);
25
26 _cleanup_free_ char *type_string = strndup(line, eq - line);
27 if (!type_string)
28 return log_oom();
29
30 delete_trailing_chars(type_string, /* bad= */ NULL);
31
32 Condition **target;
33 ConditionType type;
34 if (startswith(type_string, "Condition")) {
35 target = &u->conditions;
36 type = condition_type_from_string(type_string);
37 } else {
38 target = &u->asserts;
39 type = assert_type_from_string(type_string);
40 }
41 if (type < 0)
42 return log_error_errno(type, "Cannot parse \"%s\".", line);
43
44 const char *val = skip_leading_chars(eq + 1, /* bad= */ NULL);
45
46 ConfigParserCallback callback;
47 if (condition_takes_path(type))
48 callback = config_parse_unit_condition_path;
49 else
50 callback = config_parse_unit_condition_string;
51
52 return callback(/* unit= */ NULL,
53 /* filename= */ "(cmdline)",
54 /* line= */ 0,
55 /* section= */ NULL,
56 /* section_line= */ 0,
57 /* lvalue= */ type_string,
58 /* ltype= */ type,
59 /* rvalue= */ val,
60 /* data= */ target,
61 /* userdata= */ u);
62 }
63
64 _printf_(7, 8)
65 static int log_helper(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) {
66 Unit *u = ASSERT_PTR(userdata);
67 va_list ap;
68 int r;
69
70 /* "upgrade" debug messages */
71 level = MIN(LOG_INFO, level);
72
73 va_start(ap, format);
74 r = log_object_internalv(level, error, file, line, func,
75 /* object_field= */ unit_log_field(u),
76 /* object= */ u->id,
77 /* extra_field= */ NULL,
78 /* extra= */ NULL,
79 format, ap);
80 va_end(ap);
81
82 return r;
83 }
84
85 static int verify_conditions(char **lines, RuntimeScope scope, const char *unit, const char *root) {
86 _cleanup_(manager_freep) Manager *m = NULL;
87 Unit *u;
88 int r, q = 1;
89
90 if (unit) {
91 r = verify_set_unit_path(STRV_MAKE(unit));
92 if (r < 0)
93 return log_error_errno(r, "Failed to set unit load path: %m");
94 }
95
96 r = manager_new(scope, MANAGER_TEST_RUN_MINIMAL|MANAGER_TEST_DONT_OPEN_EXECUTOR, &m);
97 if (r < 0)
98 return log_error_errno(r, "Failed to initialize manager: %m");
99
100 log_debug("Starting manager...");
101 r = manager_startup(m, /* serialization= */ NULL, /* fds= */ NULL, /* named_listen_fds= */ NULL, root);
102 if (r < 0)
103 return r;
104
105 if (unit) {
106 _cleanup_free_ char *prepared = NULL;
107
108 r = verify_prepare_filename(unit, &prepared);
109 if (r < 0)
110 return log_error_errno(r, "Failed to prepare filename %s: %m", unit);
111
112 r = manager_load_startable_unit_or_warn(m, NULL, prepared, &u);
113 if (r < 0)
114 return r;
115 } else {
116 r = unit_new_for_name(m, sizeof(Service), "test.service", &u);
117 if (r < 0)
118 return log_error_errno(r, "Failed to create test.service: %m");
119
120 STRV_FOREACH(line, lines) {
121 r = parse_condition(u, *line);
122 if (r < 0)
123 return r;
124 }
125 }
126
127 condition_test_logger_t logger = arg_quiet ? NULL : log_helper;
128 r = condition_test_list(u->asserts, environ, assert_type_to_string, logger, u);
129 if (u->asserts)
130 log_full(arg_quiet ? LOG_DEBUG : LOG_NOTICE, "Asserts %s.", r > 0 ? "succeeded" : "failed");
131
132 q = condition_test_list(u->conditions, environ, condition_type_to_string, logger, u);
133 if (u->conditions)
134 log_full(arg_quiet ? LOG_DEBUG : LOG_NOTICE, "Conditions %s.", q > 0 ? "succeeded" : "failed");
135
136 return r > 0 && q > 0 ? 0 : -EIO;
137 }
138
139 int verb_condition(int argc, char *argv[], uintptr_t _data, void *userdata) {
140 int r;
141
142 r = verify_conditions(strv_skip(argv, 1), arg_runtime_scope, arg_unit, arg_root);
143 if (r < 0)
144 return r;
145
146 return EXIT_SUCCESS;
147 }