]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/machine.c
Merge pull request #2664 from zonque/bootchart-removal
[thirdparty/systemd.git] / src / machine / machine.c
CommitLineData
9444b1f2
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2011 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
66cb2fde 20#include <errno.h>
9444b1f2
LP
21#include <string.h>
22#include <unistd.h>
9444b1f2 23
c3350683 24#include "sd-messages.h"
fb6becb4 25
b5efdb8a 26#include "alloc-util.h"
66cb2fde
LP
27#include "bus-error.h"
28#include "bus-util.h"
4f5dd394 29#include "escape.h"
cf0fbc49 30#include "extract-word.h"
3ffd4af2 31#include "fd-util.h"
9444b1f2 32#include "fileio.h"
66cb2fde
LP
33#include "formats-util.h"
34#include "hashmap.h"
4f5dd394 35#include "machine-dbus.h"
3ffd4af2 36#include "machine.h"
66cb2fde 37#include "mkdir.h"
6bedfcbb 38#include "parse-util.h"
4a0b58c4 39#include "process-util.h"
9444b1f2 40#include "special.h"
8b43440b 41#include "string-table.h"
66cb2fde 42#include "terminal-util.h"
fb6becb4 43#include "unit-name.h"
66cb2fde 44#include "util.h"
9444b1f2 45
fbe55073 46Machine* machine_new(Manager *manager, MachineClass class, const char *name) {
9444b1f2
LP
47 Machine *m;
48
49 assert(manager);
fbe55073 50 assert(class < _MACHINE_CLASS_MAX);
9444b1f2
LP
51 assert(name);
52
fbe55073
LP
53 /* Passing class == _MACHINE_CLASS_INVALID here is fine. It
54 * means as much as "we don't know yet", and that we'll figure
55 * it out later when loading the state file. */
56
9444b1f2
LP
57 m = new0(Machine, 1);
58 if (!m)
59 return NULL;
60
61 m->name = strdup(name);
62 if (!m->name)
63 goto fail;
64
fbe55073
LP
65 if (class != MACHINE_HOST) {
66 m->state_file = strappend("/run/systemd/machines/", m->name);
67 if (!m->state_file)
68 goto fail;
69 }
70
71 m->class = class;
9444b1f2
LP
72
73 if (hashmap_put(manager->machines, m->name, m) < 0)
74 goto fail;
75
9444b1f2
LP
76 m->manager = manager;
77
78 return m;
79
80fail:
81 free(m->state_file);
82 free(m->name);
83 free(m);
84
85 return NULL;
86}
87
88void machine_free(Machine *m) {
89 assert(m);
90
0370612e
LP
91 while (m->operations)
92 machine_operation_unref(m->operations);
93
9444b1f2 94 if (m->in_gc_queue)
71fda00f 95 LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m);
9444b1f2 96
9b420b3c 97 machine_release_unit(m);
9444b1f2 98
fb6becb4
LP
99 free(m->scope_job);
100
9b420b3c 101 (void) hashmap_remove(m->manager->machines, m->name);
9444b1f2 102
fbe55073
LP
103 if (m->manager->host_machine == m)
104 m->manager->host_machine = NULL;
105
d3e84ddb 106 if (m->leader > 0)
4a0b58c4 107 (void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader), m);
d3e84ddb 108
c3350683 109 sd_bus_message_unref(m->create_message);
fb6becb4 110
9444b1f2
LP
111 free(m->name);
112 free(m->state_file);
113 free(m->service);
9444b1f2 114 free(m->root_directory);
9b5ed6fe 115 free(m->netif);
9444b1f2
LP
116 free(m);
117}
118
119int machine_save(Machine *m) {
120 _cleanup_free_ char *temp_path = NULL;
121 _cleanup_fclose_ FILE *f = NULL;
122 int r;
123
124 assert(m);
fbe55073
LP
125
126 if (!m->state_file)
127 return 0;
9444b1f2
LP
128
129 if (!m->started)
130 return 0;
131
132 r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0);
133 if (r < 0)
dacd6cee 134 goto fail;
9444b1f2
LP
135
136 r = fopen_temporary(m->state_file, &f, &temp_path);
137 if (r < 0)
dacd6cee 138 goto fail;
9444b1f2 139
dacd6cee 140 (void) fchmod(fileno(f), 0644);
9444b1f2
LP
141
142 fprintf(f,
143 "# This is private data. Do not parse.\n"
144 "NAME=%s\n",
145 m->name);
146
ca5405bb
LP
147 if (m->unit) {
148 _cleanup_free_ char *escaped;
149
150 escaped = cescape(m->unit);
151 if (!escaped) {
152 r = -ENOMEM;
dacd6cee 153 goto fail;
ca5405bb
LP
154 }
155
156 fprintf(f, "SCOPE=%s\n", escaped); /* We continue to call this "SCOPE=" because it is internal only, and we want to stay compatible with old files */
157 }
fb6becb4
LP
158
159 if (m->scope_job)
160 fprintf(f, "SCOPE_JOB=%s\n", m->scope_job);
9444b1f2 161
ca5405bb
LP
162 if (m->service) {
163 _cleanup_free_ char *escaped;
9444b1f2 164
ca5405bb
LP
165 escaped = cescape(m->service);
166 if (!escaped) {
167 r = -ENOMEM;
dacd6cee 168 goto fail;
ca5405bb
LP
169 }
170 fprintf(f, "SERVICE=%s\n", escaped);
171 }
172
173 if (m->root_directory) {
174 _cleanup_free_ char *escaped;
175
176 escaped = cescape(m->root_directory);
177 if (!escaped) {
178 r = -ENOMEM;
dacd6cee 179 goto fail;
ca5405bb
LP
180 }
181 fprintf(f, "ROOT=%s\n", escaped);
182 }
9444b1f2
LP
183
184 if (!sd_id128_equal(m->id, SD_ID128_NULL))
185 fprintf(f, "ID=" SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->id));
186
187 if (m->leader != 0)
90b2de37 188 fprintf(f, "LEADER="PID_FMT"\n", m->leader);
9444b1f2
LP
189
190 if (m->class != _MACHINE_CLASS_INVALID)
191 fprintf(f, "CLASS=%s\n", machine_class_to_string(m->class));
192
193 if (dual_timestamp_is_set(&m->timestamp))
194 fprintf(f,
90b2de37
ZJS
195 "REALTIME="USEC_FMT"\n"
196 "MONOTONIC="USEC_FMT"\n",
197 m->timestamp.realtime,
198 m->timestamp.monotonic);
9444b1f2 199
9b5ed6fe
LP
200 if (m->n_netif > 0) {
201 unsigned i;
202
203 fputs("NETIF=", f);
204
205 for (i = 0; i < m->n_netif; i++) {
206 if (i != 0)
207 fputc(' ', f);
208
209 fprintf(f, "%i", m->netif[i]);
210 }
211
212 fputc('\n', f);
213 }
214
034753ac
LP
215 r = fflush_and_check(f);
216 if (r < 0)
dacd6cee 217 goto fail;
9444b1f2 218
034753ac 219 if (rename(temp_path, m->state_file) < 0) {
9444b1f2 220 r = -errno;
dacd6cee 221 goto fail;
9444b1f2
LP
222 }
223
89f7c846
LP
224 if (m->unit) {
225 char *sl;
226
227 /* Create a symlink from the unit name to the machine
228 * name, so that we can quickly find the machine for
e62d9b81 229 * each given unit. Ignore error. */
63c372cb 230 sl = strjoina("/run/systemd/machines/unit:", m->unit);
e62d9b81 231 (void) symlink(m->name, sl);
89f7c846
LP
232 }
233
dacd6cee 234 return 0;
034753ac 235
dacd6cee
LP
236fail:
237 (void) unlink(m->state_file);
238
239 if (temp_path)
240 (void) unlink(temp_path);
9444b1f2 241
dacd6cee 242 return log_error_errno(r, "Failed to save machine data %s: %m", m->state_file);
9444b1f2
LP
243}
244
89f7c846
LP
245static void machine_unlink(Machine *m) {
246 assert(m);
247
248 if (m->unit) {
249
250 char *sl;
251
63c372cb 252 sl = strjoina("/run/systemd/machines/unit:", m->unit);
491ac9f2 253 (void) unlink(sl);
89f7c846
LP
254 }
255
256 if (m->state_file)
491ac9f2 257 (void) unlink(m->state_file);
89f7c846
LP
258}
259
9444b1f2 260int machine_load(Machine *m) {
9b5ed6fe 261 _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL, *netif = NULL;
9444b1f2
LP
262 int r;
263
264 assert(m);
265
fbe55073
LP
266 if (!m->state_file)
267 return 0;
268
9444b1f2 269 r = parse_env_file(m->state_file, NEWLINE,
89f7c846 270 "SCOPE", &m->unit,
fb6becb4 271 "SCOPE_JOB", &m->scope_job,
9444b1f2 272 "SERVICE", &m->service,
9444b1f2
LP
273 "ROOT", &m->root_directory,
274 "ID", &id,
275 "LEADER", &leader,
276 "CLASS", &class,
277 "REALTIME", &realtime,
278 "MONOTONIC", &monotonic,
9b5ed6fe 279 "NETIF", &netif,
9444b1f2
LP
280 NULL);
281 if (r < 0) {
282 if (r == -ENOENT)
283 return 0;
284
8d3d7072 285 return log_error_errno(r, "Failed to read %s: %m", m->state_file);
9444b1f2
LP
286 }
287
288 if (id)
289 sd_id128_from_string(id, &m->id);
290
291 if (leader)
292 parse_pid(leader, &m->leader);
293
294 if (class) {
295 MachineClass c;
296
297 c = machine_class_from_string(class);
298 if (c >= 0)
299 m->class = c;
300 }
301
b895a735
BR
302 if (realtime)
303 timestamp_deserialize(realtime, &m->timestamp.realtime);
304 if (monotonic)
305 timestamp_deserialize(monotonic, &m->timestamp.monotonic);
9444b1f2 306
9b5ed6fe 307 if (netif) {
75a8fd6a
SS
308 size_t allocated = 0, nr = 0;
309 const char *p;
9b5ed6fe
LP
310 int *ni = NULL;
311
75a8fd6a
SS
312 p = netif;
313 for(;;) {
314 _cleanup_free_ char *word = NULL;
9b5ed6fe
LP
315 int ifi;
316
75a8fd6a 317 r = extract_first_word(&p, &word, NULL, 0);
75a8fd6a
SS
318 if (r == 0)
319 break;
6a37c684 320 if (r == -ENOMEM)
52278ad3 321 return log_oom();
6a37c684 322 if (r < 0) {
52278ad3 323 log_warning_errno(r, "Failed to parse NETIF: %s", netif);
6a37c684 324 break;
52278ad3 325 }
75a8fd6a 326
6ad623a3 327 if (parse_ifindex(word, &ifi) < 0)
9b5ed6fe
LP
328 continue;
329
330 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
331 free(ni);
332 return log_oom();
333 }
334
335 ni[nr++] = ifi;
336 }
337
338 free(m->netif);
339 m->netif = ni;
340 m->n_netif = nr;
341 }
342
9444b1f2
LP
343 return r;
344}
345
c3350683 346static int machine_start_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
2c4c73b3 347 int r = 0;
9444b1f2
LP
348
349 assert(m);
fbe55073 350 assert(m->class != MACHINE_HOST);
9444b1f2 351
89f7c846 352 if (!m->unit) {
d0af76e6 353 _cleanup_free_ char *escaped = NULL;
39883f62 354 char *scope, *description, *job = NULL;
9444b1f2 355
fb6becb4 356 escaped = unit_name_escape(m->name);
9444b1f2
LP
357 if (!escaped)
358 return log_oom();
359
d0af76e6 360 scope = strjoin("machine-", escaped, ".scope", NULL);
f526ab7e 361 if (!scope)
9444b1f2 362 return log_oom();
9444b1f2 363
63c372cb 364 description = strjoina(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name);
9444b1f2 365
c3350683 366 r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, properties, error, &job);
d0af76e6 367 if (r < 0) {
c3350683 368 log_error("Failed to start machine scope: %s", bus_error_message(error, r));
d0af76e6 369 free(scope);
f2d4f98d 370 return r;
d0af76e6 371 } else {
89f7c846 372 m->unit = scope;
d0af76e6
LP
373
374 free(m->scope_job);
375 m->scope_job = job;
376 }
9444b1f2
LP
377 }
378
89f7c846
LP
379 if (m->unit)
380 hashmap_put(m->manager->machine_units, m->unit, m);
d0af76e6 381
fb6becb4 382 return r;
9444b1f2
LP
383}
384
c3350683 385int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
9444b1f2
LP
386 int r;
387
388 assert(m);
389
fbe55073
LP
390 if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
391 return -EOPNOTSUPP;
392
9444b1f2
LP
393 if (m->started)
394 return 0;
395
4a0b58c4 396 r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m);
d3e84ddb
LP
397 if (r < 0)
398 return r;
399
fb6becb4 400 /* Create cgroup */
c3350683 401 r = machine_start_scope(m, properties, error);
fb6becb4
LP
402 if (r < 0)
403 return r;
404
9444b1f2 405 log_struct(LOG_INFO,
e2cc6eca 406 LOG_MESSAGE_ID(SD_MESSAGE_MACHINE_START),
9444b1f2 407 "NAME=%s", m->name,
de0671ee 408 "LEADER="PID_FMT, m->leader,
e2cc6eca 409 LOG_MESSAGE("New machine %s.", m->name),
9444b1f2
LP
410 NULL);
411
9444b1f2
LP
412 if (!dual_timestamp_is_set(&m->timestamp))
413 dual_timestamp_get(&m->timestamp);
414
415 m->started = true;
416
417 /* Save new machine data */
418 machine_save(m);
419
420 machine_send_signal(m, true);
421
422 return 0;
423}
424
fb6becb4 425static int machine_stop_scope(Machine *m) {
4afd3348 426 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
39883f62 427 char *job = NULL;
9444b1f2 428 int r;
9444b1f2
LP
429
430 assert(m);
fbe55073 431 assert(m->class != MACHINE_HOST);
9444b1f2 432
89f7c846 433 if (!m->unit)
fb6becb4 434 return 0;
9444b1f2 435
c00a4c8f
LP
436 r = manager_stop_unit(m->manager, m->unit, &error, &job);
437 if (r < 0) {
438 log_error("Failed to stop machine scope: %s", bus_error_message(&error, r));
439 return r;
fb6becb4 440 }
9444b1f2 441
fb6becb4
LP
442 free(m->scope_job);
443 m->scope_job = job;
9444b1f2 444
f14aa1f1 445 return 0;
9444b1f2
LP
446}
447
448int machine_stop(Machine *m) {
49f3fffd
LP
449 int r;
450 assert(m);
451
fbe55073
LP
452 if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
453 return -EOPNOTSUPP;
454
49f3fffd
LP
455 r = machine_stop_scope(m);
456
457 m->stopping = true;
458
459 machine_save(m);
460
461 return r;
462}
463
464int machine_finalize(Machine *m) {
9444b1f2
LP
465 assert(m);
466
467 if (m->started)
468 log_struct(LOG_INFO,
e2cc6eca 469 LOG_MESSAGE_ID(SD_MESSAGE_MACHINE_STOP),
9444b1f2 470 "NAME=%s", m->name,
de0671ee 471 "LEADER="PID_FMT, m->leader,
e2cc6eca 472 LOG_MESSAGE("Machine %s terminated.", m->name),
9444b1f2
LP
473 NULL);
474
89f7c846 475 machine_unlink(m);
9444b1f2
LP
476 machine_add_to_gc_queue(m);
477
49f3fffd 478 if (m->started) {
9444b1f2 479 machine_send_signal(m, false);
49f3fffd
LP
480 m->started = false;
481 }
9444b1f2 482
49f3fffd 483 return 0;
9444b1f2
LP
484}
485
a658cafa 486bool machine_check_gc(Machine *m, bool drop_not_started) {
9444b1f2
LP
487 assert(m);
488
fbe55073
LP
489 if (m->class == MACHINE_HOST)
490 return true;
491
9444b1f2 492 if (drop_not_started && !m->started)
c3350683 493 return false;
9444b1f2 494
c3350683
LP
495 if (m->scope_job && manager_job_is_active(m->manager, m->scope_job))
496 return true;
9444b1f2 497
89f7c846 498 if (m->unit && manager_unit_is_active(m->manager, m->unit))
c3350683 499 return true;
9444b1f2 500
c3350683 501 return false;
9444b1f2
LP
502}
503
504void machine_add_to_gc_queue(Machine *m) {
505 assert(m);
506
507 if (m->in_gc_queue)
508 return;
509
71fda00f 510 LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m);
9444b1f2
LP
511 m->in_gc_queue = true;
512}
513
fb6becb4
LP
514MachineState machine_get_state(Machine *s) {
515 assert(s);
9444b1f2 516
fbe55073
LP
517 if (s->class == MACHINE_HOST)
518 return MACHINE_RUNNING;
519
49f3fffd
LP
520 if (s->stopping)
521 return MACHINE_CLOSING;
522
fb6becb4 523 if (s->scope_job)
49f3fffd 524 return MACHINE_OPENING;
9444b1f2 525
fb6becb4
LP
526 return MACHINE_RUNNING;
527}
9444b1f2 528
fb6becb4
LP
529int machine_kill(Machine *m, KillWho who, int signo) {
530 assert(m);
9444b1f2 531
fbe55073
LP
532 if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER))
533 return -EOPNOTSUPP;
534
89f7c846 535 if (!m->unit)
fb6becb4 536 return -ESRCH;
9444b1f2 537
de58a50e
LP
538 if (who == KILL_LEADER) {
539 /* If we shall simply kill the leader, do so directly */
540
541 if (kill(m->leader, signo) < 0)
542 return -errno;
9d685ca8
ED
543
544 return 0;
de58a50e
LP
545 }
546
b938cb90 547 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
de58a50e 548 return manager_kill_unit(m->manager, m->unit, signo, NULL);
9444b1f2
LP
549}
550
fbe55073
LP
551int machine_openpt(Machine *m, int flags) {
552 assert(m);
553
554 switch (m->class) {
555
5f430ff7
LP
556 case MACHINE_HOST: {
557 int fd;
558
559 fd = posix_openpt(flags);
560 if (fd < 0)
561 return -errno;
562
563 if (unlockpt(fd) < 0)
564 return -errno;
565
566 return fd;
567 }
fbe55073
LP
568
569 case MACHINE_CONTAINER:
570 if (m->leader <= 0)
571 return -EINVAL;
572
573 return openpt_in_namespace(m->leader, flags);
574
575 default:
576 return -EOPNOTSUPP;
577 }
578}
579
40e1f4ea
LP
580int machine_open_terminal(Machine *m, const char *path, int mode) {
581 assert(m);
582
583 switch (m->class) {
584
585 case MACHINE_HOST:
586 return open_terminal(path, mode);
587
588 case MACHINE_CONTAINER:
589 if (m->leader <= 0)
590 return -EINVAL;
591
592 return open_terminal_in_namespace(m->leader, path, mode);
593
594 default:
595 return -EOPNOTSUPP;
596 }
597}
598
0370612e
LP
599MachineOperation *machine_operation_unref(MachineOperation *o) {
600 if (!o)
601 return NULL;
602
603 sd_event_source_unref(o->event_source);
604
605 safe_close(o->errno_fd);
606
607 if (o->pid > 1)
608 (void) kill(o->pid, SIGKILL);
609
610 sd_bus_message_unref(o->message);
611
612 if (o->machine) {
613 LIST_REMOVE(operations, o->machine->operations, o);
614 o->machine->n_operations--;
615 }
616
617 free(o);
618 return NULL;
619}
620
9b420b3c
LP
621void machine_release_unit(Machine *m) {
622 assert(m);
623
624 if (!m->unit)
625 return;
626
627 (void) hashmap_remove(m->manager->machine_units, m->unit);
a1e58e8e 628 m->unit = mfree(m->unit);
9b420b3c
LP
629}
630
9444b1f2
LP
631static const char* const machine_class_table[_MACHINE_CLASS_MAX] = {
632 [MACHINE_CONTAINER] = "container",
fbe55073
LP
633 [MACHINE_VM] = "vm",
634 [MACHINE_HOST] = "host",
9444b1f2
LP
635};
636
637DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass);
fb6becb4
LP
638
639static const char* const machine_state_table[_MACHINE_STATE_MAX] = {
640 [MACHINE_OPENING] = "opening",
641 [MACHINE_RUNNING] = "running",
642 [MACHINE_CLOSING] = "closing"
643};
644
645DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState);
1ee306e1
LP
646
647static const char* const kill_who_table[_KILL_WHO_MAX] = {
648 [KILL_LEADER] = "leader",
649 [KILL_ALL] = "all"
650};
651
652DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);