]> git.proxmox.com Git - systemd.git/blame - src/machine/machine.c
Imported Upstream version 231
[systemd.git] / src / machine / machine.c
CommitLineData
14228c0d
MB
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
db2df898 20#include <errno.h>
14228c0d
MB
21#include <string.h>
22#include <unistd.h>
14228c0d 23
60f067b4 24#include "sd-messages.h"
14228c0d 25
db2df898
MP
26#include "alloc-util.h"
27#include "bus-error.h"
28#include "bus-util.h"
29#include "escape.h"
30#include "extract-word.h"
31#include "fd-util.h"
14228c0d 32#include "fileio.h"
db2df898
MP
33#include "formats-util.h"
34#include "hashmap.h"
35#include "machine-dbus.h"
36#include "machine.h"
37#include "mkdir.h"
38#include "parse-util.h"
39#include "process-util.h"
14228c0d 40#include "special.h"
db2df898
MP
41#include "string-table.h"
42#include "terminal-util.h"
14228c0d 43#include "unit-name.h"
db2df898 44#include "util.h"
14228c0d 45
13d276d0 46Machine* machine_new(Manager *manager, MachineClass class, const char *name) {
14228c0d
MB
47 Machine *m;
48
49 assert(manager);
13d276d0 50 assert(class < _MACHINE_CLASS_MAX);
14228c0d
MB
51 assert(name);
52
13d276d0
MP
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
14228c0d
MB
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
13d276d0
MP
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;
14228c0d
MB
72
73 if (hashmap_put(manager->machines, m->name, m) < 0)
74 goto fail;
75
14228c0d
MB
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
e3bff60a 91 while (m->operations)
aa27b158 92 operation_free(m->operations);
e3bff60a 93
14228c0d 94 if (m->in_gc_queue)
60f067b4 95 LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m);
14228c0d 96
e3bff60a 97 machine_release_unit(m);
14228c0d
MB
98
99 free(m->scope_job);
100
e3bff60a 101 (void) hashmap_remove(m->manager->machines, m->name);
14228c0d 102
13d276d0
MP
103 if (m->manager->host_machine == m)
104 m->manager->host_machine = NULL;
105
60f067b4 106 if (m->leader > 0)
db2df898 107 (void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader), m);
60f067b4
JS
108
109 sd_bus_message_unref(m->create_message);
14228c0d
MB
110
111 free(m->name);
112 free(m->state_file);
113 free(m->service);
114 free(m->root_directory);
5eef597e 115 free(m->netif);
14228c0d
MB
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);
13d276d0
MP
125
126 if (!m->state_file)
127 return 0;
14228c0d
MB
128
129 if (!m->started)
130 return 0;
131
132 r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0);
133 if (r < 0)
5fd56512 134 goto fail;
14228c0d
MB
135
136 r = fopen_temporary(m->state_file, &f, &temp_path);
137 if (r < 0)
5fd56512 138 goto fail;
14228c0d 139
5fd56512 140 (void) fchmod(fileno(f), 0644);
14228c0d
MB
141
142 fprintf(f,
143 "# This is private data. Do not parse.\n"
144 "NAME=%s\n",
145 m->name);
146
60f067b4
JS
147 if (m->unit) {
148 _cleanup_free_ char *escaped;
149
150 escaped = cescape(m->unit);
151 if (!escaped) {
152 r = -ENOMEM;
5fd56512 153 goto fail;
60f067b4
JS
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 }
14228c0d
MB
158
159 if (m->scope_job)
160 fprintf(f, "SCOPE_JOB=%s\n", m->scope_job);
161
60f067b4
JS
162 if (m->service) {
163 _cleanup_free_ char *escaped;
164
165 escaped = cescape(m->service);
166 if (!escaped) {
167 r = -ENOMEM;
5fd56512 168 goto fail;
60f067b4
JS
169 }
170 fprintf(f, "SERVICE=%s\n", escaped);
171 }
172
173 if (m->root_directory) {
174 _cleanup_free_ char *escaped;
14228c0d 175
60f067b4
JS
176 escaped = cescape(m->root_directory);
177 if (!escaped) {
178 r = -ENOMEM;
5fd56512 179 goto fail;
60f067b4
JS
180 }
181 fprintf(f, "ROOT=%s\n", escaped);
182 }
14228c0d 183
5a920b42 184 if (!sd_id128_is_null(m->id))
14228c0d
MB
185 fprintf(f, "ID=" SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->id));
186
187 if (m->leader != 0)
60f067b4 188 fprintf(f, "LEADER="PID_FMT"\n", m->leader);
14228c0d
MB
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,
60f067b4
JS
195 "REALTIME="USEC_FMT"\n"
196 "MONOTONIC="USEC_FMT"\n",
197 m->timestamp.realtime,
198 m->timestamp.monotonic);
14228c0d 199
5eef597e
MP
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
e842803a
MB
215 r = fflush_and_check(f);
216 if (r < 0)
5fd56512 217 goto fail;
14228c0d 218
e842803a 219 if (rename(temp_path, m->state_file) < 0) {
14228c0d 220 r = -errno;
5fd56512 221 goto fail;
14228c0d
MB
222 }
223
60f067b4
JS
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
e3bff60a 229 * each given unit. Ignore error. */
e735f4d4 230 sl = strjoina("/run/systemd/machines/unit:", m->unit);
e3bff60a 231 (void) symlink(m->name, sl);
60f067b4
JS
232 }
233
5fd56512 234 return 0;
e842803a 235
5fd56512
MP
236fail:
237 (void) unlink(m->state_file);
238
239 if (temp_path)
240 (void) unlink(temp_path);
14228c0d 241
5fd56512 242 return log_error_errno(r, "Failed to save machine data %s: %m", m->state_file);
14228c0d
MB
243}
244
60f067b4
JS
245static void machine_unlink(Machine *m) {
246 assert(m);
247
248 if (m->unit) {
249
250 char *sl;
251
e735f4d4 252 sl = strjoina("/run/systemd/machines/unit:", m->unit);
13d276d0 253 (void) unlink(sl);
60f067b4
JS
254 }
255
256 if (m->state_file)
13d276d0 257 (void) unlink(m->state_file);
60f067b4
JS
258}
259
14228c0d 260int machine_load(Machine *m) {
5eef597e 261 _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL, *netif = NULL;
14228c0d
MB
262 int r;
263
264 assert(m);
265
13d276d0
MP
266 if (!m->state_file)
267 return 0;
268
14228c0d 269 r = parse_env_file(m->state_file, NEWLINE,
60f067b4 270 "SCOPE", &m->unit,
14228c0d
MB
271 "SCOPE_JOB", &m->scope_job,
272 "SERVICE", &m->service,
273 "ROOT", &m->root_directory,
274 "ID", &id,
275 "LEADER", &leader,
276 "CLASS", &class,
277 "REALTIME", &realtime,
278 "MONOTONIC", &monotonic,
5eef597e 279 "NETIF", &netif,
14228c0d
MB
280 NULL);
281 if (r < 0) {
282 if (r == -ENOENT)
283 return 0;
284
f47781d8 285 return log_error_errno(r, "Failed to read %s: %m", m->state_file);
14228c0d
MB
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
aa27b158
MP
302 if (realtime)
303 timestamp_deserialize(realtime, &m->timestamp.realtime);
304 if (monotonic)
305 timestamp_deserialize(monotonic, &m->timestamp.monotonic);
14228c0d 306
5eef597e 307 if (netif) {
db2df898
MP
308 size_t allocated = 0, nr = 0;
309 const char *p;
5eef597e
MP
310 int *ni = NULL;
311
db2df898 312 p = netif;
aa27b158 313 for (;;) {
db2df898 314 _cleanup_free_ char *word = NULL;
5eef597e
MP
315 int ifi;
316
db2df898
MP
317 r = extract_first_word(&p, &word, NULL, 0);
318 if (r == 0)
319 break;
320 if (r == -ENOMEM)
321 return log_oom();
322 if (r < 0) {
323 log_warning_errno(r, "Failed to parse NETIF: %s", netif);
324 break;
325 }
5eef597e 326
db2df898 327 if (parse_ifindex(word, &ifi) < 0)
5eef597e
MP
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
14228c0d
MB
343 return r;
344}
345
60f067b4 346static int machine_start_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
14228c0d
MB
347 int r = 0;
348
349 assert(m);
13d276d0 350 assert(m->class != MACHINE_HOST);
14228c0d 351
60f067b4 352 if (!m->unit) {
14228c0d 353 _cleanup_free_ char *escaped = NULL;
60f067b4 354 char *scope, *description, *job = NULL;
14228c0d
MB
355
356 escaped = unit_name_escape(m->name);
357 if (!escaped)
358 return log_oom();
359
360 scope = strjoin("machine-", escaped, ".scope", NULL);
361 if (!scope)
362 return log_oom();
363
e735f4d4 364 description = strjoina(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name);
14228c0d 365
60f067b4 366 r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, properties, error, &job);
14228c0d 367 if (r < 0) {
60f067b4 368 log_error("Failed to start machine scope: %s", bus_error_message(error, r));
14228c0d
MB
369 free(scope);
370 return r;
371 } else {
60f067b4 372 m->unit = scope;
14228c0d
MB
373
374 free(m->scope_job);
375 m->scope_job = job;
376 }
377 }
378
60f067b4
JS
379 if (m->unit)
380 hashmap_put(m->manager->machine_units, m->unit, m);
14228c0d
MB
381
382 return r;
383}
384
60f067b4 385int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
14228c0d
MB
386 int r;
387
388 assert(m);
389
13d276d0
MP
390 if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
391 return -EOPNOTSUPP;
392
14228c0d
MB
393 if (m->started)
394 return 0;
395
db2df898 396 r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m);
60f067b4
JS
397 if (r < 0)
398 return r;
399
14228c0d 400 /* Create cgroup */
60f067b4 401 r = machine_start_scope(m, properties, error);
14228c0d
MB
402 if (r < 0)
403 return r;
404
405 log_struct(LOG_INFO,
f47781d8 406 LOG_MESSAGE_ID(SD_MESSAGE_MACHINE_START),
14228c0d 407 "NAME=%s", m->name,
60f067b4 408 "LEADER="PID_FMT, m->leader,
f47781d8 409 LOG_MESSAGE("New machine %s.", m->name),
14228c0d
MB
410 NULL);
411
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
425static int machine_stop_scope(Machine *m) {
4c89c718 426 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
60f067b4 427 char *job = NULL;
14228c0d
MB
428 int r;
429
430 assert(m);
13d276d0 431 assert(m->class != MACHINE_HOST);
14228c0d 432
60f067b4 433 if (!m->unit)
14228c0d
MB
434 return 0;
435
e735f4d4
MP
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;
14228c0d
MB
440 }
441
442 free(m->scope_job);
443 m->scope_job = job;
444
5eef597e 445 return 0;
14228c0d
MB
446}
447
448int machine_stop(Machine *m) {
13d276d0
MP
449 int r;
450 assert(m);
451
452 if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
453 return -EOPNOTSUPP;
454
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) {
14228c0d
MB
465 assert(m);
466
467 if (m->started)
468 log_struct(LOG_INFO,
f47781d8 469 LOG_MESSAGE_ID(SD_MESSAGE_MACHINE_STOP),
14228c0d 470 "NAME=%s", m->name,
60f067b4 471 "LEADER="PID_FMT, m->leader,
f47781d8 472 LOG_MESSAGE("Machine %s terminated.", m->name),
14228c0d
MB
473 NULL);
474
60f067b4 475 machine_unlink(m);
14228c0d
MB
476 machine_add_to_gc_queue(m);
477
13d276d0 478 if (m->started) {
14228c0d 479 machine_send_signal(m, false);
13d276d0
MP
480 m->started = false;
481 }
14228c0d 482
13d276d0 483 return 0;
14228c0d
MB
484}
485
60f067b4 486bool machine_check_gc(Machine *m, bool drop_not_started) {
14228c0d
MB
487 assert(m);
488
13d276d0
MP
489 if (m->class == MACHINE_HOST)
490 return true;
491
14228c0d 492 if (drop_not_started && !m->started)
60f067b4 493 return false;
14228c0d 494
60f067b4
JS
495 if (m->scope_job && manager_job_is_active(m->manager, m->scope_job))
496 return true;
14228c0d 497
60f067b4
JS
498 if (m->unit && manager_unit_is_active(m->manager, m->unit))
499 return true;
14228c0d 500
60f067b4 501 return false;
14228c0d
MB
502}
503
504void machine_add_to_gc_queue(Machine *m) {
505 assert(m);
506
507 if (m->in_gc_queue)
508 return;
509
60f067b4 510 LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m);
14228c0d
MB
511 m->in_gc_queue = true;
512}
513
514MachineState machine_get_state(Machine *s) {
515 assert(s);
516
13d276d0
MP
517 if (s->class == MACHINE_HOST)
518 return MACHINE_RUNNING;
519
520 if (s->stopping)
521 return MACHINE_CLOSING;
522
14228c0d 523 if (s->scope_job)
13d276d0 524 return MACHINE_OPENING;
14228c0d
MB
525
526 return MACHINE_RUNNING;
527}
528
529int machine_kill(Machine *m, KillWho who, int signo) {
530 assert(m);
531
13d276d0
MP
532 if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER))
533 return -EOPNOTSUPP;
534
60f067b4 535 if (!m->unit)
14228c0d
MB
536 return -ESRCH;
537
60f067b4
JS
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;
5eef597e
MP
543
544 return 0;
60f067b4
JS
545 }
546
db2df898 547 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
60f067b4 548 return manager_kill_unit(m->manager, m->unit, signo, NULL);
14228c0d
MB
549}
550
13d276d0
MP
551int machine_openpt(Machine *m, int flags) {
552 assert(m);
553
554 switch (m->class) {
555
d9dfd233
MP
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 }
13d276d0
MP
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
db2df898
MP
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
e3bff60a
MP
599void machine_release_unit(Machine *m) {
600 assert(m);
601
602 if (!m->unit)
603 return;
604
605 (void) hashmap_remove(m->manager->machine_units, m->unit);
6300502b 606 m->unit = mfree(m->unit);
e3bff60a
MP
607}
608
14228c0d
MB
609static const char* const machine_class_table[_MACHINE_CLASS_MAX] = {
610 [MACHINE_CONTAINER] = "container",
13d276d0
MP
611 [MACHINE_VM] = "vm",
612 [MACHINE_HOST] = "host",
14228c0d
MB
613};
614
615DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass);
616
617static const char* const machine_state_table[_MACHINE_STATE_MAX] = {
618 [MACHINE_OPENING] = "opening",
619 [MACHINE_RUNNING] = "running",
620 [MACHINE_CLOSING] = "closing"
621};
622
623DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState);
624
625static const char* const kill_who_table[_KILL_WHO_MAX] = {
626 [KILL_LEADER] = "leader",
627 [KILL_ALL] = "all"
628};
629
630DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);