Debian Codegraph @

tools/xenstored/lu.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /*
4 * Live Update interfaces for Xen Store Daemon.
5 * Copyright (C) 2022 Juergen Gross, SUSE LLC
6 */
7
8 #include <assert.h>
9 #include <ctype.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <syslog.h>
13 #include <time.h>
14 #include <sys/mman.h>
15 #include <sys/stat.h>
16
17 #include "talloc.h"
18 #include "core.h"
19 #include "domain.h"
20 #include "lu.h"
21 #include "watch.h"
22
23 #ifndef NO_LIVE_UPDATE
24
25 struct lu_dump_state {
26 void *buf;
27 unsigned int size;
28 int fd;
29 char *filename;
30 };
31
32 struct live_update *lu_status;
33
34 static int lu_destroy(void *data)
35 {
36 lu_status = NULL;
37
38 return 0;
39 }
40
41 const char *lu_begin(struct connection *conn)
42 {
43 if (lu_status)
44 return "live-update session already active.";
45
46 lu_status = talloc_zero(conn, struct live_update);
47 if (!lu_status)
48 return "Allocation failure.";
49 lu_status->conn = conn;
50 talloc_set_destructor(lu_status, lu_destroy);
51
52 return NULL;
53 }
54
55 struct connection *lu_get_connection(void)
56 {
57 return lu_status ? lu_status->conn : NULL;
58 }
59
60 unsigned int lu_write_response(FILE *fp)
61 {
62 struct xsd_sockmsg msg;
63
64 assert(lu_status);
65
66 msg = lu_status->in->hdr.msg;
67
68 msg.len = sizeof("OK");
69 if (fp && fwrite(&msg, sizeof(msg), 1, fp) != 1)
70 return 0;
71 if (fp && fwrite("OK", msg.len, 1, fp) != 1)
72 return 0;
73
74 return sizeof(msg) + msg.len;
75 }
76
77 bool lu_is_pending(void)
78 {
79 return lu_status != NULL;
80 }
81
82 static void lu_get_dump_state(struct lu_dump_state *state)
83 {
84 struct stat statbuf;
85
86 state->size = 0;
87
88 state->filename = talloc_asprintf(NULL, "%s/state_dump",
89 xenstore_rundir());
90 if (!state->filename)
91 barf("Allocation failure");
92
93 state->fd = open(state->filename, O_RDONLY);
94 if (state->fd < 0)
95 return;
96 if (fstat(state->fd, &statbuf) != 0)
97 goto out_close;
98 state->size = statbuf.st_size;
99
100 state->buf = mmap(NULL, state->size, PROT_READ, MAP_PRIVATE,
101 state->fd, 0);
102 if (state->buf == MAP_FAILED) {
103 state->size = 0;
104 goto out_close;
105 }
106
107 return;
108
109 out_close:
110 close(state->fd);
111 }
112
113 static void lu_close_dump_state(struct lu_dump_state *state)
114 {
115 assert(state->filename != NULL);
116
117 munmap(state->buf, state->size);
118 close(state->fd);
119
120 unlink(state->filename);
121 talloc_free(state->filename);
122 }
123
124 void lu_read_state(void)
125 {
126 struct lu_dump_state state = {};
127 struct xs_state_record_header *head;
128 void *ctx = talloc_new(NULL); /* Work context for subfunctions. */
129 struct xs_state_preamble *pre;
130
131 syslog(LOG_INFO, "live-update: read state\n");
132 lu_get_dump_state(&state);
133 if (state.size == 0)
134 barf_perror("No state found after live-update");
135
136 pre = state.buf;
137 if (memcmp(pre->ident, XS_STATE_IDENT, sizeof(pre->ident)) ||
138 pre->version != htobe32(XS_STATE_VERSION) ||
139 pre->flags != XS_STATE_FLAGS)
140 barf("Unknown record identifier");
141 for (head = state.buf + sizeof(*pre);
142 head->type != XS_STATE_TYPE_END &&
143 (void *)head - state.buf < state.size;
144 head = (void *)head + sizeof(*head) + head->length) {
145 switch (head->type) {
146 case XS_STATE_TYPE_GLOBAL:
147 read_state_global(ctx, head + 1);
148 break;
149 case XS_STATE_TYPE_CONN:
150 read_state_connection(ctx, head + 1);
151 break;
152 case XS_STATE_TYPE_WATCH:
153 read_state_watch(ctx, head + 1);
154 break;
155 case XS_STATE_TYPE_TA:
156 xprintf("live-update: ignore transaction record\n");
157 break;
158 case XS_STATE_TYPE_NODE:
159 read_state_node(ctx, head + 1);
160 break;
161 default:
162 xprintf("live-update: unknown state record %08x\n",
163 head->type);
164 break;
165 }
166 }
167
168 lu_close_dump_state(&state);
169
170 talloc_free(ctx);
171
172 /*
173 * We may have missed the VIRQ_DOM_EXC notification and a domain may
174 * have died while we were live-updating. So check all the domains are
175 * still alive.
176 */
177 check_domains();
178 }
179
180 static const char *lu_abort(const void *ctx, struct connection *conn)
181 {
182 syslog(LOG_INFO, "live-update: abort\n");
183
184 if (!lu_status)
185 return "No live-update session active.";
186
187 /* Destructor will do the real abort handling. */
188 talloc_free(lu_status);
189
190 return NULL;
191 }
192
193 static const char *lu_cmdline(const void *ctx, struct connection *conn,
194 const char *cmdline)
195 {
196 syslog(LOG_INFO, "live-update: cmdline %s\n", cmdline);
197
198 if (!lu_status || lu_status->conn != conn)
199 return "Not in live-update session.";
200
201 lu_status->cmdline = talloc_strdup(lu_status, cmdline);
202 if (!lu_status->cmdline)
203 return "Allocation failure.";
204
205 return NULL;
206 }
207
208 static bool lu_check_lu_allowed(void)
209 {
210 struct connection *conn;
211 time_t now = time(NULL);
212 unsigned int ta_total = 0, ta_long = 0;
213
214 list_for_each_entry(conn, &connections, list) {
215 if (conn->ta_start_time) {
216 ta_total++;
217 if (now - conn->ta_start_time >= lu_status->timeout)
218 ta_long++;
219 }
220 }
221
222 /*
223 * Allow LiveUpdate if one of the following conditions is met:
224 * - There is no active transactions
225 * - All transactions are long running (e.g. they have been
226 * active for more than lu_status->timeout sec) and the admin as
227 * requested to force the operation.
228 */
229 return ta_total ? (lu_status->force && ta_long == ta_total) : true;
230 }
231
232 static const char *lu_reject_reason(const void *ctx)
233 {
234 char *ret = NULL;
235 struct connection *conn;
236 time_t now = time(NULL);
237
238 list_for_each_entry(conn, &connections, list) {
239 unsigned long tdiff = now - conn->ta_start_time;
240
241 if (conn->ta_start_time && (tdiff >= lu_status->timeout)) {
242 ret = talloc_asprintf(ctx, "%s\nDomain %u: %ld s",
243 ret ? : "Domains with long running transactions:",
244 conn->id, tdiff);
245 }
246 }
247
248 return ret ? (const char *)ret : "Overlapping transactions";
249 }
250
251 static FILE *lu_dump_open(const void *ctx)
252 {
253 char *filename;
254 int fd;
255
256 filename = talloc_asprintf(ctx, "%s/state_dump",
257 xenstore_rundir());
258 if (!filename)
259 return NULL;
260
261 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
262 if (fd < 0)
263 return NULL;
264
265 return fdopen(fd, "w");
266 }
267
268 static void lu_dump_close(FILE *fp)
269 {
270 fclose(fp);
271 }
272
273 static const char *lu_dump_state(const void *ctx, struct connection *conn)
274 {
275 FILE *fp;
276 const char *ret;
277 struct xs_state_record_header end;
278 struct xs_state_preamble pre;
279
280 fp = lu_dump_open(ctx);
281 if (!fp)
282 return "Dump state open error";
283
284 memcpy(pre.ident, XS_STATE_IDENT, sizeof(pre.ident));
285 pre.version = htobe32(XS_STATE_VERSION);
286 pre.flags = XS_STATE_FLAGS;
287 if (fwrite(&pre, sizeof(pre), 1, fp) != 1) {
288 ret = "Dump write error";
289 goto out;
290 }
291
292 ret = dump_state_global(fp);
293 if (ret)
294 goto out;
295 ret = dump_state_connections(fp);
296 if (ret)
297 goto out;
298 ret = dump_state_nodes(fp, ctx);
299 if (ret)
300 goto out;
301
302 end.type = XS_STATE_TYPE_END;
303 end.length = 0;
304 if (fwrite(&end, sizeof(end), 1, fp) != 1)
305 ret = "Dump write error";
306
307 out:
308 lu_dump_close(fp);
309
310 return ret;
311 }
312
313 static const char *lu_activate_binary(const void *ctx)
314 {
315 int argc;
316 char **argv;
317 unsigned int i;
318
319 if (lu_status->cmdline) {
320 argc = 4; /* At least one arg + progname + "-U" + NULL. */
321 for (i = 0; lu_status->cmdline[i]; i++)
322 if (isspace(lu_status->cmdline[i]))
323 argc++;
324 argv = talloc_array(ctx, char *, argc);
325 if (!argv)
326 return "Allocation failure.";
327
328 i = 0;
329 argc = 1;
330 argv[1] = strtok(lu_status->cmdline, " \t");
331 while (argv[argc]) {
332 if (!strcmp(argv[argc], "-U"))
333 i = 1;
334 argc++;
335 argv[argc] = strtok(NULL, " \t");
336 }
337
338 if (!i) {
339 argv[argc++] = "-U";
340 argv[argc] = NULL;
341 }
342 } else {
343 for (i = 0; i < orig_argc; i++)
344 if (!strcmp(orig_argv[i], "-U"))
345 break;
346
347 argc = orig_argc;
348 argv = talloc_array(ctx, char *, orig_argc + 2);
349 if (!argv)
350 return "Allocation failure.";
351
352 memcpy(argv, orig_argv, orig_argc * sizeof(*argv));
353 if (i == orig_argc)
354 argv[argc++] = "-U";
355 argv[argc] = NULL;
356 }
357
358 domain_deinit();
359
360 return lu_exec(ctx, argc, argv);
361 }
362
363 static bool do_lu_start(struct delayed_request *req)
364 {
365 time_t now = time(NULL);
366 const char *ret;
367 struct buffered_data *saved_in;
368 struct connection *conn = req->data;
369
370 /*
371 * Cancellation may have been requested asynchronously. In this
372 * case, lu_status will be NULL.
373 */
374 if (!lu_status) {
375 ret = "Cancellation was requested";
376 goto out;
377 }
378
379 assert(lu_status->conn == conn);
380
381 if (!lu_check_lu_allowed()) {
382 if (now < lu_status->started_at + lu_status->timeout)
383 return false;
384 if (!lu_status->force) {
385 ret = lu_reject_reason(req);
386 goto out;
387 }
388 }
389
390 assert(req->in == lu_status->in);
391 /* Dump out internal state, including "OK" for live update. */
392 ret = lu_dump_state(req->in, conn);
393 if (!ret) {
394 /* Perform the activation of new binary. */
395 ret = lu_activate_binary(req->in);
396 }
397
398 /* We will reach this point only in case of failure. */
399 out:
400 /*
401 * send_reply() will send the response for conn->in. Save the current
402 * conn->in and restore it afterwards.
403 */
404 saved_in = conn->in;
405 conn->in = req->in;
406 send_reply(conn, XS_CONTROL, ret, strlen(ret) + 1);
407 conn->in = saved_in;
408 talloc_free(lu_status);
409
410 return true;
411 }
412
413 static const char *lu_start(const void *ctx, struct connection *conn,
414 bool force, unsigned int to)
415 {
416 syslog(LOG_INFO, "live-update: start, force=%d, to=%u\n", force, to);
417
418 if (!lu_status || lu_status->conn != conn)
419 return "Not in live-update session.";
420
421 #ifdef __MINIOS__
422 if (lu_status->kernel_size != lu_status->kernel_off)
423 return "Kernel not complete.";
424 #endif
425
426 lu_status->force = force;
427 lu_status->timeout = to;
428 lu_status->started_at = time(NULL);
429 lu_status->in = conn->in;
430
431 errno = delay_request(conn, conn->in, do_lu_start, conn, false);
432
433 return NULL;
434 }
435
436 int do_control_lu(const void *ctx, struct connection *conn, const char **vec,
437 int num)
438 {
439 const char *ret = NULL;
440 unsigned int i;
441 bool force = false;
442 unsigned int to = 0;
443
444 if (num < 1)
445 return EINVAL;
446
447 if (!strcmp(vec[0], "-a")) {
448 if (num == 1)
449 ret = lu_abort(ctx, conn);
450 else
451 return EINVAL;
452 } else if (!strcmp(vec[0], "-c")) {
453 if (num == 2)
454 ret = lu_cmdline(ctx, conn, vec[1]);
455 else
456 return EINVAL;
457 } else if (!strcmp(vec[0], "-s")) {
458 for (i = 1; i < num; i++) {
459 if (!strcmp(vec[i], "-F"))
460 force = true;
461 else if (!strcmp(vec[i], "-t") && i < num - 1) {
462 i++;
463 to = atoi(vec[i]);
464 } else
465 return EINVAL;
466 }
467 ret = lu_start(ctx, conn, force, to);
468 if (!ret)
469 return errno;
470 } else {
471 ret = lu_arch(ctx, conn, vec, num);
472 if (!ret && errno)
473 return errno;
474 }
475
476 if (!ret)
477 ret = "OK";
478 send_reply(conn, XS_CONTROL, ret, strlen(ret) + 1);
479 return 0;
480 }
481 #endif