]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machine.c
2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
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.
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.
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/>.
24 #include "sd-messages.h"
26 #include "alloc-util.h"
27 #include "bus-error.h"
30 #include "extract-word.h"
33 #include "formats-util.h"
35 #include "machine-dbus.h"
38 #include "parse-util.h"
39 #include "process-util.h"
41 #include "string-table.h"
42 #include "terminal-util.h"
43 #include "unit-name.h"
46 Machine
* machine_new(Manager
*manager
, MachineClass
class, const char *name
) {
50 assert(class < _MACHINE_CLASS_MAX
);
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. */
61 m
->name
= strdup(name
);
65 if (class != MACHINE_HOST
) {
66 m
->state_file
= strappend("/run/systemd/machines/", m
->name
);
73 if (hashmap_put(manager
->machines
, m
->name
, m
) < 0)
86 void machine_free(Machine
*m
) {
90 operation_free(m
->operations
);
93 LIST_REMOVE(gc_queue
, m
->manager
->machine_gc_queue
, m
);
95 machine_release_unit(m
);
99 (void) hashmap_remove(m
->manager
->machines
, m
->name
);
101 if (m
->manager
->host_machine
== m
)
102 m
->manager
->host_machine
= NULL
;
105 (void) hashmap_remove_value(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
107 sd_bus_message_unref(m
->create_message
);
112 free(m
->root_directory
);
117 int machine_save(Machine
*m
) {
118 _cleanup_free_
char *temp_path
= NULL
;
119 _cleanup_fclose_
FILE *f
= NULL
;
130 r
= mkdir_safe_label("/run/systemd/machines", 0755, 0, 0);
134 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
138 (void) fchmod(fileno(f
), 0644);
141 "# This is private data. Do not parse.\n"
146 _cleanup_free_
char *escaped
;
148 escaped
= cescape(m
->unit
);
154 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 */
158 fprintf(f
, "SCOPE_JOB=%s\n", m
->scope_job
);
161 _cleanup_free_
char *escaped
;
163 escaped
= cescape(m
->service
);
168 fprintf(f
, "SERVICE=%s\n", escaped
);
171 if (m
->root_directory
) {
172 _cleanup_free_
char *escaped
;
174 escaped
= cescape(m
->root_directory
);
179 fprintf(f
, "ROOT=%s\n", escaped
);
182 if (!sd_id128_is_null(m
->id
))
183 fprintf(f
, "ID=" SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(m
->id
));
186 fprintf(f
, "LEADER="PID_FMT
"\n", m
->leader
);
188 if (m
->class != _MACHINE_CLASS_INVALID
)
189 fprintf(f
, "CLASS=%s\n", machine_class_to_string(m
->class));
191 if (dual_timestamp_is_set(&m
->timestamp
))
193 "REALTIME="USEC_FMT
"\n"
194 "MONOTONIC="USEC_FMT
"\n",
195 m
->timestamp
.realtime
,
196 m
->timestamp
.monotonic
);
198 if (m
->n_netif
> 0) {
203 for (i
= 0; i
< m
->n_netif
; i
++) {
207 fprintf(f
, "%i", m
->netif
[i
]);
213 r
= fflush_and_check(f
);
217 if (rename(temp_path
, m
->state_file
) < 0) {
225 /* Create a symlink from the unit name to the machine
226 * name, so that we can quickly find the machine for
227 * each given unit. Ignore error. */
228 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
229 (void) symlink(m
->name
, sl
);
235 (void) unlink(m
->state_file
);
238 (void) unlink(temp_path
);
240 return log_error_errno(r
, "Failed to save machine data %s: %m", m
->state_file
);
243 static void machine_unlink(Machine
*m
) {
250 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
255 (void) unlink(m
->state_file
);
258 int machine_load(Machine
*m
) {
259 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *id
= NULL
, *leader
= NULL
, *class = NULL
, *netif
= NULL
;
267 r
= parse_env_file(m
->state_file
, NEWLINE
,
269 "SCOPE_JOB", &m
->scope_job
,
270 "SERVICE", &m
->service
,
271 "ROOT", &m
->root_directory
,
275 "REALTIME", &realtime
,
276 "MONOTONIC", &monotonic
,
283 return log_error_errno(r
, "Failed to read %s: %m", m
->state_file
);
287 sd_id128_from_string(id
, &m
->id
);
290 parse_pid(leader
, &m
->leader
);
295 c
= machine_class_from_string(class);
301 timestamp_deserialize(realtime
, &m
->timestamp
.realtime
);
303 timestamp_deserialize(monotonic
, &m
->timestamp
.monotonic
);
306 size_t allocated
= 0, nr
= 0;
312 _cleanup_free_
char *word
= NULL
;
315 r
= extract_first_word(&p
, &word
, NULL
, 0);
321 log_warning_errno(r
, "Failed to parse NETIF: %s", netif
);
325 if (parse_ifindex(word
, &ifi
) < 0)
328 if (!GREEDY_REALLOC(ni
, allocated
, nr
+1)) {
344 static int machine_start_scope(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
348 assert(m
->class != MACHINE_HOST
);
351 _cleanup_free_
char *escaped
= NULL
;
352 char *scope
, *description
, *job
= NULL
;
354 escaped
= unit_name_escape(m
->name
);
358 scope
= strjoin("machine-", escaped
, ".scope", NULL
);
362 description
= strjoina(m
->class == MACHINE_VM
? "Virtual Machine " : "Container ", m
->name
);
364 r
= manager_start_scope(m
->manager
, scope
, m
->leader
, SPECIAL_MACHINE_SLICE
, description
, properties
, error
, &job
);
366 log_error("Failed to start machine scope: %s", bus_error_message(error
, r
));
378 hashmap_put(m
->manager
->machine_units
, m
->unit
, m
);
383 int machine_start(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
388 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
394 r
= hashmap_put(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
399 r
= machine_start_scope(m
, properties
, error
);
404 LOG_MESSAGE_ID(SD_MESSAGE_MACHINE_START
),
406 "LEADER="PID_FMT
, m
->leader
,
407 LOG_MESSAGE("New machine %s.", m
->name
),
410 if (!dual_timestamp_is_set(&m
->timestamp
))
411 dual_timestamp_get(&m
->timestamp
);
415 /* Save new machine data */
418 machine_send_signal(m
, true);
423 static int machine_stop_scope(Machine
*m
) {
424 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
429 assert(m
->class != MACHINE_HOST
);
434 r
= manager_stop_unit(m
->manager
, m
->unit
, &error
, &job
);
436 log_error("Failed to stop machine scope: %s", bus_error_message(&error
, r
));
446 int machine_stop(Machine
*m
) {
450 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
453 r
= machine_stop_scope(m
);
462 int machine_finalize(Machine
*m
) {
467 LOG_MESSAGE_ID(SD_MESSAGE_MACHINE_STOP
),
469 "LEADER="PID_FMT
, m
->leader
,
470 LOG_MESSAGE("Machine %s terminated.", m
->name
),
474 machine_add_to_gc_queue(m
);
477 machine_send_signal(m
, false);
484 bool machine_check_gc(Machine
*m
, bool drop_not_started
) {
487 if (m
->class == MACHINE_HOST
)
490 if (drop_not_started
&& !m
->started
)
493 if (m
->scope_job
&& manager_job_is_active(m
->manager
, m
->scope_job
))
496 if (m
->unit
&& manager_unit_is_active(m
->manager
, m
->unit
))
502 void machine_add_to_gc_queue(Machine
*m
) {
508 LIST_PREPEND(gc_queue
, m
->manager
->machine_gc_queue
, m
);
509 m
->in_gc_queue
= true;
512 MachineState
machine_get_state(Machine
*s
) {
515 if (s
->class == MACHINE_HOST
)
516 return MACHINE_RUNNING
;
519 return MACHINE_CLOSING
;
522 return MACHINE_OPENING
;
524 return MACHINE_RUNNING
;
527 int machine_kill(Machine
*m
, KillWho who
, int signo
) {
530 if (!IN_SET(m
->class, MACHINE_VM
, MACHINE_CONTAINER
))
536 if (who
== KILL_LEADER
) {
537 /* If we shall simply kill the leader, do so directly */
539 if (kill(m
->leader
, signo
) < 0)
545 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
546 return manager_kill_unit(m
->manager
, m
->unit
, signo
, NULL
);
549 int machine_openpt(Machine
*m
, int flags
) {
557 fd
= posix_openpt(flags
);
561 if (unlockpt(fd
) < 0)
567 case MACHINE_CONTAINER
:
571 return openpt_in_namespace(m
->leader
, flags
);
578 int machine_open_terminal(Machine
*m
, const char *path
, int mode
) {
584 return open_terminal(path
, mode
);
586 case MACHINE_CONTAINER
:
590 return open_terminal_in_namespace(m
->leader
, path
, mode
);
597 void machine_release_unit(Machine
*m
) {
603 (void) hashmap_remove(m
->manager
->machine_units
, m
->unit
);
604 m
->unit
= mfree(m
->unit
);
607 static const char* const machine_class_table
[_MACHINE_CLASS_MAX
] = {
608 [MACHINE_CONTAINER
] = "container",
610 [MACHINE_HOST
] = "host",
613 DEFINE_STRING_TABLE_LOOKUP(machine_class
, MachineClass
);
615 static const char* const machine_state_table
[_MACHINE_STATE_MAX
] = {
616 [MACHINE_OPENING
] = "opening",
617 [MACHINE_RUNNING
] = "running",
618 [MACHINE_CLOSING
] = "closing"
621 DEFINE_STRING_TABLE_LOOKUP(machine_state
, MachineState
);
623 static const char* const kill_who_table
[_KILL_WHO_MAX
] = {
624 [KILL_LEADER
] = "leader",
628 DEFINE_STRING_TABLE_LOOKUP(kill_who
, KillWho
);