Debian Codegraph @

src/core/luo.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-json.h"
4
5 #include "errno-util.h"
6 #include "fd-util.h"
7 #include "fdset.h"
8 #include "json-util.h"
9 #include "log.h"
10 #include "luo.h"
11 #include "luo-util.h"
12 #include "manager.h"
13 #include "serialize.h"
14 #include "service.h"
15 #include "unit.h"
16 #include "unit-name.h"
17
18 static int luo_read_mapping(int session_fd, sd_json_variant **ret) {
19 int r;
20
21 assert(session_fd >= 0);
22 assert(ret);
23
24 _cleanup_close_ int mapping_fd = luo_session_retrieve_fd(session_fd, LUO_MAPPING_INDEX);
25 if (mapping_fd < 0)
26 return log_warning_errno(mapping_fd, "Failed to retrieve LUO mapping fd (fd_index 0): %m");
27
28 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
29 r = sd_json_parse_fd(
30 "luo-mapping",
31 mapping_fd,
32 SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_REOPEN_FD,
33 &v,
34 /* reterr_line= */ NULL,
35 /* reterr_column= */ NULL);
36 if (r < 0)
37 return log_warning_errno(r, "Failed to parse LUO mapping JSON: %m");
38
39 *ret = TAKE_PTR(v);
40 return 0;
41 }
42
43 static void luo_session_finishp(int *fd) {
44 assert(fd);
45
46 if (*fd >= 0)
47 (void) luo_session_finish(*fd);
48 safe_close(*fd);
49 }
50
51 int manager_luo_restore_fd_stores(Manager *m) {
52 _cleanup_(sd_json_variant_unrefp) sd_json_variant *mapping = NULL;
53 _cleanup_close_ int device_fd = -EBADF;
54 _cleanup_(luo_session_finishp) int session_fd = -EBADF;
55 const char *unit_id;
56 sd_json_variant *fds_json;
57 int r, n_total = 0;
58
59 assert(m);
60
61 if (MANAGER_IS_USER(m))
62 return 0;
63
64 device_fd = luo_open_device();
65 if (ERRNO_IS_NEG_DEVICE_ABSENT(device_fd)) {
66 log_debug_errno(device_fd, "No /dev/liveupdate device found, skipping LUO fd store restoration.");
67 return 0;
68 }
69 if (device_fd < 0)
70 return log_warning_errno(device_fd, "Failed to open /dev/liveupdate: %m");
71
72 session_fd = luo_retrieve_session(device_fd, LUO_SESSION_NAME);
73 if (session_fd == -ENOENT) {
74 log_debug("No LUO session '%s' found, skipping fd store restoration.", LUO_SESSION_NAME);
75 return 0;
76 }
77 if (session_fd < 0)
78 return log_warning_errno(session_fd, "Failed to retrieve LUO session '%s': %m", LUO_SESSION_NAME);
79
80 log_debug("Found LUO session '%s', restoring fd stores.", LUO_SESSION_NAME);
81
82 r = luo_read_mapping(session_fd, &mapping);
83 if (r < 0)
84 return r;
85
86 /* Retrieve all fds from the session and dispatch each to the named unit, eagerly loading the
87 * unit if necessary. */
88 JSON_VARIANT_OBJECT_FOREACH(unit_id, fds_json, mapping) {
89 sd_json_variant *entry;
90
91 if (!unit_name_is_valid(unit_id, UNIT_NAME_ANY)) {
92 log_warning("Invalid unit name '%s' in LUO mapping, skipping.", unit_id);
93 continue;
94 }
95
96 if (!sd_json_variant_is_array(fds_json)) {
97 log_warning("LUO mapping for unit '%s' is not a JSON array, skipping.", unit_id);
98 continue;
99 }
100
101 JSON_VARIANT_ARRAY_FOREACH(entry, fds_json) {
102 struct {
103 const char *type;
104 const char *name;
105 uint64_t token;
106 const char *session_name;
107 } p = {
108 .token = UINT64_MAX,
109 };
110
111 static const sd_json_dispatch_field dispatch_table[] = {
112 { "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, type), SD_JSON_MANDATORY },
113 { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, name), SD_JSON_MANDATORY },
114 { "token", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, voffsetof(p, token), 0 },
115 { "sessionName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, session_name), 0 },
116 {}
117 };
118
119 _cleanup_close_ int fd = -EBADF;
120
121 r = sd_json_dispatch(entry, dispatch_table, SD_JSON_ALLOW_EXTENSIONS|SD_JSON_LOG|SD_JSON_WARNING, &p);
122 if (r < 0)
123 continue;
124
125 if (streq(p.type, "fd")) {
126 if (p.token == UINT64_MAX) {
127 log_warning("LUO mapping for unit '%s' fd '%s': missing 'token' field.", unit_id, p.name);
128 continue;
129 }
130 if (p.token == LUO_MAPPING_INDEX) {
131 log_warning("LUO mapping for unit '%s' fd '%s': token %" PRIu64 " is reserved for the mapping memfd.", unit_id, p.name, p.token);
132 continue;
133 }
134
135 fd = luo_session_retrieve_fd(session_fd, p.token);
136 if (fd < 0) {
137 log_warning_errno(fd, "Failed to retrieve LUO fd for unit '%s' name '%s' token %" PRIu64 ": %m",
138 unit_id, p.name, p.token);
139 continue;
140 }
141 } else if (streq(p.type, "luo_session")) {
142 if (!p.session_name) {
143 log_warning("LUO mapping for unit '%s' fd '%s': missing sessionName.", unit_id, p.name);
144 continue;
145 }
146
147 fd = luo_retrieve_session(device_fd, p.session_name);
148 if (fd < 0) {
149 log_warning_errno(fd, "Failed to retrieve LUO session '%s' for unit '%s' name '%s': %m",
150 p.session_name, unit_id, p.name);
151 continue;
152 }
153
154 log_debug("Retrieved LUO session '%s' for unit fd store '%s'.", p.session_name, p.name);
155 } else {
156 log_warning("LUO mapping for unit '%s' fd '%s': unknown type '%s', skipping.",
157 unit_id, p.name, p.type);
158 continue;
159 }
160
161 r = manager_dispatch_external_fd_to_unit(m, unit_id, p.name, /* index= */ 0, TAKE_FD(fd), "LUO");
162 if (r > 0)
163 n_total++;
164 /* On error fd is already consumed by manager_dispatch_external_fd_to_unit. */
165 }
166 }
167
168 if (n_total > 0)
169 log_debug("Restored %d fd(s) total from LUO session.", n_total);
170
171 return n_total;
172 }
173
174 int manager_luo_serialize_fd_stores(Manager *m, FILE **ret_f, FDSet **ret_fds) {
175 _cleanup_(sd_json_variant_unrefp) sd_json_variant *root = NULL;
176 _cleanup_fclose_ FILE *f = NULL;
177 _cleanup_fdset_free_ FDSet *fds = NULL;
178 Unit *u;
179 int r, n_serialized = 0;
180
181 assert(m);
182 assert(ret_f);
183 assert(ret_fds);
184
185 if (MANAGER_IS_USER(m)) {
186 *ret_f = NULL;
187 *ret_fds = NULL;
188 return 0;
189 }
190
191 fds = fdset_new();
192 if (!fds)
193 return log_oom();
194
195 /* Build a JSON object: { "unit_id": [ { "type": "fd", "name": "...", "fd": N },
196 * { "type": "luo_session", "name": "...", "fd": N, "sessionName": "..." } ], ... }
197 * This is passed to systemd-shutdown which will create a LUO session and preserve the fds. */
198 HASHMAP_FOREACH(u, m->units) {
199 _cleanup_(sd_json_variant_unrefp) sd_json_variant *entries = NULL;
200 Service *s;
201
202 if (u->type != UNIT_SERVICE)
203 continue;
204
205 s = SERVICE(u);
206
207 if (!IN_SET(s->fd_store_preserve_mode, EXEC_PRESERVE_YES, EXEC_PRESERVE_ON_SUCCESS))
208 continue;
209
210 if (!s->fd_store)
211 continue;
212
213 LIST_FOREACH(fd_store, fs, s->fd_store) {
214 _cleanup_free_ char *session_name = NULL;
215 int copy;
216
217 /* Check if this fd is itself a LUO session, as those cannot be nested and need
218 * special handling */
219 r = fd_get_luo_session_name(fs->fd, &session_name);
220 if (r < 0 && r != -EMEDIUMTYPE) {
221 log_warning_errno(r, "Failed to check if fd '%s' of unit '%s' is a LUO session, skipping: %m",
222 fs->fdname, u->id);
223 continue;
224 }
225
226 /* Ensure nobody tries to hijack our session, as we will create this later before
227 * kexec */
228 if (streq_ptr(session_name, LUO_SESSION_NAME)) {
229 log_warning("Skipping fd '%s' of unit '%s' for LUO serialization, as the session name '%s' infringes systemd's namespace.",
230 fs->fdname, u->id, session_name);
231 continue;
232 }
233
234 copy = fdset_put_dup(fds, fs->fd);
235 if (copy < 0)
236 return log_error_errno(copy, "Failed to duplicate fd for LUO serialization: %m");
237
238 r = sd_json_variant_append_arraybo(
239 &entries,
240 SD_JSON_BUILD_PAIR_STRING("type", session_name ? "luo_session" : "fd"),
241 SD_JSON_BUILD_PAIR_STRING("name", fs->fdname),
242 SD_JSON_BUILD_PAIR_INTEGER("fd", copy),
243 JSON_BUILD_PAIR_STRING_NON_EMPTY("sessionName", session_name));
244 if (r < 0)
245 return log_error_errno(r, "Failed to build JSON for LUO serialization: %m");
246
247 n_serialized++;
248 }
249
250 r = sd_json_variant_set_field(&root, u->id, entries);
251 if (r < 0)
252 return log_error_errno(r, "Failed to add unit to LUO serialization JSON: %m");
253 }
254
255 if (n_serialized == 0) {
256 log_debug("No fd store entries to serialize for LUO.");
257 *ret_f = NULL;
258 *ret_fds = NULL;
259 return 0;
260 }
261
262 r = open_serialization_file("luo-fd-store", &f);
263 if (r < 0)
264 return log_error_errno(r, "Failed to create LUO serialization file: %m");
265
266 r = sd_json_variant_dump(root, /* flags= */ 0, f, /* prefix= */ NULL);
267 if (r < 0)
268 return log_error_errno(r, "Failed to dump LUO serialization JSON: %m");
269
270 r = finish_serialization_file(f);
271 if (r < 0)
272 return log_error_errno(r, "Failed to finish LUO serialization file: %m");
273
274 r = fd_cloexec(fileno(f), false);
275 if (r < 0)
276 return log_error_errno(r, "Failed to disable O_CLOEXEC for LUO serialization: %m");
277
278 r = fdset_cloexec(fds, false);
279 if (r < 0)
280 return log_error_errno(r, "Failed to disable O_CLOEXEC for LUO serialization fds: %m");
281
282 log_info("Serialized %d fd store entries for LUO.", n_serialized);
283
284 *ret_f = TAKE_PTR(f);
285 *ret_fds = TAKE_PTR(fds);
286 return n_serialized;
287 }