]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/machinectl.c
tree-wide: use sd_id128_is_null() instead of sd_id128_equal where appropriate
[thirdparty/systemd.git] / src / machine / machinectl.c
CommitLineData
1ee306e1
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2013 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
3f6fd1ba 20#include <arpa/inet.h>
1ee306e1 21#include <errno.h>
3f6fd1ba 22#include <fcntl.h>
1ee306e1 23#include <getopt.h>
1ee306e1 24#include <locale.h>
f48e75cb 25#include <net/if.h>
3f6fd1ba
LP
26#include <netinet/in.h>
27#include <string.h>
785890ac 28#include <sys/mount.h>
3f6fd1ba
LP
29#include <sys/socket.h>
30#include <unistd.h>
eef46c37 31
a1da8583 32#include "sd-bus.h"
3f6fd1ba 33
b5efdb8a 34#include "alloc-util.h"
68ce459f 35#include "bus-common-errors.h"
a1da8583 36#include "bus-error.h"
a0e27019 37#include "bus-unit-util.h"
3f6fd1ba 38#include "bus-util.h"
1ee306e1 39#include "cgroup-show.h"
9d127096 40#include "cgroup-util.h"
f2cbe59e 41#include "copy.h"
3f6fd1ba 42#include "env-util.h"
3ffd4af2 43#include "fd-util.h"
3f6fd1ba 44#include "hostname-util.h"
3d7415f4 45#include "import-util.h"
3f6fd1ba
LP
46#include "log.h"
47#include "logs-show.h"
48#include "macro.h"
49#include "mkdir.h"
50#include "pager.h"
6bedfcbb 51#include "parse-util.h"
3f6fd1ba 52#include "path-util.h"
0b452006 53#include "process-util.h"
3f6fd1ba 54#include "ptyfwd.h"
24882e06 55#include "signal-util.h"
3f6fd1ba
LP
56#include "spawn-polkit-agent.h"
57#include "strv.h"
58#include "terminal-util.h"
59#include "unit-name.h"
60#include "util.h"
61#include "verbs.h"
49cf4170 62#include "web-util.h"
1ee306e1
LP
63
64static char **arg_property = NULL;
65static bool arg_all = false;
85500523 66static bool arg_value = false;
1ee306e1
LP
67static bool arg_full = false;
68static bool arg_no_pager = false;
e56056e9 69static bool arg_legend = true;
1ee306e1
LP
70static const char *arg_kill_who = NULL;
71static int arg_signal = SIGTERM;
d21ed1ea 72static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
1ee306e1 73static char *arg_host = NULL;
785890ac
LP
74static bool arg_read_only = false;
75static bool arg_mkdir = false;
d8f52ed2 76static bool arg_quiet = false;
acf97e21 77static bool arg_ask_password = true;
8b0cc9a3
LP
78static unsigned arg_lines = 10;
79static OutputMode arg_output = OUTPUT_SHORT;
3d7415f4 80static bool arg_force = false;
6e18cc9f 81static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
587fec42 82static const char* arg_format = NULL;
c454426c
LP
83static const char *arg_uid = NULL;
84static char **arg_setenv = NULL;
1ee306e1 85
acf97e21
LP
86static void polkit_agent_open_if_enabled(void) {
87
88 /* Open the polkit agent as a child process if necessary */
89
90 if (!arg_ask_password)
91 return;
92
93 if (arg_transport != BUS_TRANSPORT_LOCAL)
94 return;
95
96 polkit_agent_open();
97}
98
8b0cc9a3
LP
99static OutputFlags get_output_flags(void) {
100 return
101 arg_all * OUTPUT_SHOW_ALL |
102 arg_full * OUTPUT_FULL_WIDTH |
103 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
40c9fe4c 104 colors_enabled() * OUTPUT_COLOR |
8b0cc9a3
LP
105 !arg_quiet * OUTPUT_WARN_CUTOFF;
106}
107
0b63e278
LP
108typedef struct MachineInfo {
109 const char *name;
110 const char *class;
111 const char *service;
112} MachineInfo;
113
114static int compare_machine_info(const void *a, const void *b) {
115 const MachineInfo *x = a, *y = b;
116
117 return strcmp(x->name, y->name);
118}
119
56159e0d
LP
120static int list_machines(int argc, char *argv[], void *userdata) {
121
0b63e278 122 size_t max_name = strlen("MACHINE"), max_class = strlen("CLASS"), max_service = strlen("SERVICE");
4afd3348
LP
123 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
124 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
0b63e278 125 _cleanup_free_ MachineInfo *machines = NULL;
a1da8583 126 const char *name, *class, *service, *object;
0b63e278 127 size_t n_machines = 0, n_allocated = 0, j;
56159e0d 128 sd_bus *bus = userdata;
1ee306e1
LP
129 int r;
130
56159e0d
LP
131 assert(bus);
132
ea4b98e6 133 pager_open(arg_no_pager, false);
1ee306e1 134
e138e7d7
ZJS
135 r = sd_bus_call_method(bus,
136 "org.freedesktop.machine1",
137 "/org/freedesktop/machine1",
138 "org.freedesktop.machine1.Manager",
139 "ListMachines",
140 &error,
141 &reply,
142 NULL);
a1da8583
TG
143 if (r < 0) {
144 log_error("Could not get machines: %s", bus_error_message(&error, -r));
1ee306e1 145 return r;
1ee306e1
LP
146 }
147
3d7415f4 148 r = sd_bus_message_enter_container(reply, 'a', "(ssso)");
a1da8583 149 if (r < 0)
5b30bef8 150 return bus_log_parse_error(r);
1ee306e1 151
a1da8583 152 while ((r = sd_bus_message_read(reply, "(ssso)", &name, &class, &service, &object)) > 0) {
0b63e278
LP
153 size_t l;
154
fee6d013
LP
155 if (name[0] == '.' && !arg_all)
156 continue;
157
0b63e278
LP
158 if (!GREEDY_REALLOC(machines, n_allocated, n_machines + 1))
159 return log_oom();
1ee306e1 160
0b63e278
LP
161 machines[n_machines].name = name;
162 machines[n_machines].class = class;
163 machines[n_machines].service = service;
164
165 l = strlen(name);
166 if (l > max_name)
167 max_name = l;
168
169 l = strlen(class);
170 if (l > max_class)
171 max_class = l;
172
173 l = strlen(service);
174 if (l > max_service)
175 max_service = l;
176
313cefa1 177 n_machines++;
1ee306e1 178 }
a1da8583 179 if (r < 0)
5b30bef8 180 return bus_log_parse_error(r);
a1da8583
TG
181
182 r = sd_bus_message_exit_container(reply);
183 if (r < 0)
5b30bef8 184 return bus_log_parse_error(r);
1ee306e1 185
0b63e278
LP
186 qsort_safe(machines, n_machines, sizeof(MachineInfo), compare_machine_info);
187
188 if (arg_legend)
189 printf("%-*s %-*s %-*s\n",
190 (int) max_name, "MACHINE",
191 (int) max_class, "CLASS",
192 (int) max_service, "SERVICE");
193
194 for (j = 0; j < n_machines; j++)
195 printf("%-*s %-*s %-*s\n",
196 (int) max_name, machines[j].name,
197 (int) max_class, machines[j].class,
198 (int) max_service, machines[j].service);
199
e56056e9 200 if (arg_legend)
0b63e278 201 printf("\n%zu machines listed.\n", n_machines);
1ee306e1
LP
202
203 return 0;
204}
205
cd61c3bf
LP
206typedef struct ImageInfo {
207 const char *name;
208 const char *type;
209 bool read_only;
10f9c755
LP
210 usec_t crtime;
211 usec_t mtime;
b6b18498 212 uint64_t size;
cd61c3bf
LP
213} ImageInfo;
214
215static int compare_image_info(const void *a, const void *b) {
216 const ImageInfo *x = a, *y = b;
217
218 return strcmp(x->name, y->name);
219}
220
56159e0d 221static int list_images(int argc, char *argv[], void *userdata) {
cd61c3bf 222
4afd3348 223 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c19de711 224 size_t max_name = strlen("NAME"), max_type = strlen("TYPE"), max_size = strlen("USAGE"), max_crtime = strlen("CREATED"), max_mtime = strlen("MODIFIED");
4afd3348 225 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
cd61c3bf
LP
226 _cleanup_free_ ImageInfo *images = NULL;
227 size_t n_images = 0, n_allocated = 0, j;
228 const char *name, *type, *object;
56159e0d 229 sd_bus *bus = userdata;
b6b18498 230 uint64_t crtime, mtime, size;
10f9c755 231 int read_only, r;
cd61c3bf 232
56159e0d
LP
233 assert(bus);
234
ea4b98e6 235 pager_open(arg_no_pager, false);
cd61c3bf 236
e138e7d7
ZJS
237 r = sd_bus_call_method(bus,
238 "org.freedesktop.machine1",
239 "/org/freedesktop/machine1",
240 "org.freedesktop.machine1.Manager",
241 "ListImages",
242 &error,
243 &reply,
244 "");
cd61c3bf
LP
245 if (r < 0) {
246 log_error("Could not get images: %s", bus_error_message(&error, -r));
247 return r;
248 }
249
b6b18498 250 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbttto)");
cd61c3bf
LP
251 if (r < 0)
252 return bus_log_parse_error(r);
253
b6b18498
LP
254 while ((r = sd_bus_message_read(reply, "(ssbttto)", &name, &type, &read_only, &crtime, &mtime, &size, &object)) > 0) {
255 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_BYTES_MAX)];
10f9c755 256 size_t l;
cd61c3bf
LP
257
258 if (name[0] == '.' && !arg_all)
259 continue;
260
261 if (!GREEDY_REALLOC(images, n_allocated, n_images + 1))
262 return log_oom();
263
264 images[n_images].name = name;
265 images[n_images].type = type;
266 images[n_images].read_only = read_only;
10f9c755
LP
267 images[n_images].crtime = crtime;
268 images[n_images].mtime = mtime;
b6b18498 269 images[n_images].size = size;
10f9c755
LP
270
271 l = strlen(name);
272 if (l > max_name)
273 max_name = l;
cd61c3bf 274
10f9c755
LP
275 l = strlen(type);
276 if (l > max_type)
277 max_type = l;
cd61c3bf 278
10f9c755 279 if (crtime != 0) {
b6b18498 280 l = strlen(strna(format_timestamp(buf, sizeof(buf), crtime)));
10f9c755
LP
281 if (l > max_crtime)
282 max_crtime = l;
283 }
284
285 if (mtime != 0) {
b6b18498 286 l = strlen(strna(format_timestamp(buf, sizeof(buf), mtime)));
10f9c755
LP
287 if (l > max_mtime)
288 max_mtime = l;
289 }
cd61c3bf 290
b6b18498
LP
291 if (size != (uint64_t) -1) {
292 l = strlen(strna(format_bytes(buf, sizeof(buf), size)));
293 if (l > max_size)
294 max_size = l;
295 }
296
cd61c3bf
LP
297 n_images++;
298 }
299 if (r < 0)
300 return bus_log_parse_error(r);
301
302 r = sd_bus_message_exit_container(reply);
303 if (r < 0)
304 return bus_log_parse_error(r);
305
306 qsort_safe(images, n_images, sizeof(ImageInfo), compare_image_info);
307
308 if (arg_legend)
b6b18498 309 printf("%-*s %-*s %-3s %-*s %-*s %-*s\n",
10f9c755
LP
310 (int) max_name, "NAME",
311 (int) max_type, "TYPE",
312 "RO",
c19de711 313 (int) max_size, "USAGE",
10f9c755
LP
314 (int) max_crtime, "CREATED",
315 (int) max_mtime, "MODIFIED");
cd61c3bf
LP
316
317 for (j = 0; j < n_images; j++) {
b6b18498 318 char crtime_buf[FORMAT_TIMESTAMP_MAX], mtime_buf[FORMAT_TIMESTAMP_MAX], size_buf[FORMAT_BYTES_MAX];
10f9c755 319
b6b18498 320 printf("%-*s %-*s %s%-3s%s %-*s %-*s %-*s\n",
cd61c3bf
LP
321 (int) max_name, images[j].name,
322 (int) max_type, images[j].type,
1fc464f6 323 images[j].read_only ? ansi_highlight_red() : "", yes_no(images[j].read_only), images[j].read_only ? ansi_normal() : "",
b6b18498
LP
324 (int) max_size, strna(format_bytes(size_buf, sizeof(size_buf), images[j].size)),
325 (int) max_crtime, strna(format_timestamp(crtime_buf, sizeof(crtime_buf), images[j].crtime)),
326 (int) max_mtime, strna(format_timestamp(mtime_buf, sizeof(mtime_buf), images[j].mtime)));
cd61c3bf
LP
327 }
328
cd61c3bf
LP
329 if (arg_legend)
330 printf("\n%zu images listed.\n", n_images);
331
332 return 0;
333}
334
89f7c846 335static int show_unit_cgroup(sd_bus *bus, const char *unit, pid_t leader) {
4afd3348 336 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
a0e27019 337 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
aa1936ea 338 _cleanup_free_ char *path = NULL;
aa1936ea 339 const char *cgroup;
8b0cc9a3 340 int r;
aa1936ea
LP
341 unsigned c;
342
343 assert(bus);
344 assert(unit);
345
aa1936ea
LP
346 path = unit_dbus_path_from_name(unit);
347 if (!path)
348 return log_oom();
349
a7893c6b 350 r = sd_bus_get_property(
aa1936ea
LP
351 bus,
352 "org.freedesktop.systemd1",
353 path,
21b735e7 354 unit_dbus_interface_from_name(unit),
a7893c6b 355 "ControlGroup",
aa1936ea 356 &error,
a1da8583 357 &reply,
a7893c6b 358 "s");
a0e27019
LP
359 if (r < 0)
360 return log_error_errno(r, "Failed to query ControlGroup: %s", bus_error_message(&error, r));
aa1936ea 361
a7893c6b 362 r = sd_bus_message_read(reply, "s", &cgroup);
5b30bef8
LP
363 if (r < 0)
364 return bus_log_parse_error(r);
aa1936ea 365
a0e27019 366 if (isempty(cgroup))
9d127096
LP
367 return 0;
368
aa1936ea
LP
369 c = columns();
370 if (c > 18)
371 c -= 18;
372 else
373 c = 0;
374
a0e27019
LP
375 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
376 if (r == -EBADR) {
377
378 if (arg_transport == BUS_TRANSPORT_REMOTE)
379 return 0;
380
381 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
382
383 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
384 return 0;
385
0ff308c8 386 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
a0e27019
LP
387 } else if (r < 0)
388 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
389
aa1936ea
LP
390 return 0;
391}
392
f48e75cb 393static int print_addresses(sd_bus *bus, const char *name, int ifi, const char *prefix, const char *prefix2) {
4afd3348 394 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
878cd7e9
LP
395 int r;
396
397 assert(bus);
398 assert(name);
399 assert(prefix);
400 assert(prefix2);
401
402 r = sd_bus_call_method(bus,
403 "org.freedesktop.machine1",
404 "/org/freedesktop/machine1",
405 "org.freedesktop.machine1.Manager",
406 "GetMachineAddresses",
407 NULL,
408 &reply,
409 "s", name);
410 if (r < 0)
411 return r;
412
0dd25fb9 413 r = sd_bus_message_enter_container(reply, 'a', "(iay)");
878cd7e9
LP
414 if (r < 0)
415 return bus_log_parse_error(r);
416
0dd25fb9
LP
417 while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
418 int family;
878cd7e9
LP
419 const void *a;
420 size_t sz;
421 char buffer[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
422
0dd25fb9 423 r = sd_bus_message_read(reply, "i", &family);
878cd7e9
LP
424 if (r < 0)
425 return bus_log_parse_error(r);
426
427 r = sd_bus_message_read_array(reply, 'y', &a, &sz);
428 if (r < 0)
429 return bus_log_parse_error(r);
430
f48e75cb
LP
431 fputs(prefix, stdout);
432 fputs(inet_ntop(family, a, buffer, sizeof(buffer)), stdout);
433 if (family == AF_INET6 && ifi > 0)
434 printf("%%%i", ifi);
435 fputc('\n', stdout);
878cd7e9
LP
436
437 r = sd_bus_message_exit_container(reply);
438 if (r < 0)
439 return bus_log_parse_error(r);
440
441 if (prefix != prefix2)
442 prefix = prefix2;
443 }
444 if (r < 0)
445 return bus_log_parse_error(r);
446
447 r = sd_bus_message_exit_container(reply);
448 if (r < 0)
449 return bus_log_parse_error(r);
450
451 return 0;
452}
453
717603e3 454static int print_os_release(sd_bus *bus, const char *name, const char *prefix) {
4afd3348 455 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
717603e3
LP
456 const char *k, *v, *pretty = NULL;
457 int r;
458
459 assert(bus);
460 assert(name);
461 assert(prefix);
462
463 r = sd_bus_call_method(bus,
464 "org.freedesktop.machine1",
465 "/org/freedesktop/machine1",
466 "org.freedesktop.machine1.Manager",
467 "GetMachineOSRelease",
468 NULL,
469 &reply,
470 "s", name);
471 if (r < 0)
472 return r;
473
474 r = sd_bus_message_enter_container(reply, 'a', "{ss}");
475 if (r < 0)
476 return bus_log_parse_error(r);
477
478 while ((r = sd_bus_message_read(reply, "{ss}", &k, &v)) > 0) {
479 if (streq(k, "PRETTY_NAME"))
480 pretty = v;
481
482 }
483 if (r < 0)
484 return bus_log_parse_error(r);
485
486 r = sd_bus_message_exit_container(reply);
487 if (r < 0)
488 return bus_log_parse_error(r);
489
490 if (pretty)
491 printf("%s%s\n", prefix, pretty);
492
493 return 0;
494}
495
1ee306e1 496typedef struct MachineStatusInfo {
9f6eb1cd 497 char *name;
1ee306e1 498 sd_id128_t id;
9f6eb1cd
KS
499 char *class;
500 char *service;
89f7c846 501 char *unit;
9f6eb1cd 502 char *root_directory;
1ee306e1 503 pid_t leader;
8b0cc9a3 504 struct dual_timestamp timestamp;
f48e75cb
LP
505 int *netif;
506 unsigned n_netif;
1ee306e1
LP
507} MachineStatusInfo;
508
e7e55dbd
DH
509static void machine_status_info_clear(MachineStatusInfo *info) {
510 if (info) {
511 free(info->name);
512 free(info->class);
513 free(info->service);
514 free(info->unit);
515 free(info->root_directory);
516 free(info->netif);
517 zero(*info);
518 }
519}
520
a1da8583 521static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
1ee306e1
LP
522 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
523 char since2[FORMAT_TIMESTAMP_MAX], *s2;
f48e75cb
LP
524 int ifi = -1;
525
56159e0d 526 assert(bus);
1ee306e1
LP
527 assert(i);
528
529 fputs(strna(i->name), stdout);
530
3bbaff3e 531 if (!sd_id128_is_null(i->id))
1ee306e1
LP
532 printf("(" SD_ID128_FORMAT_STR ")\n", SD_ID128_FORMAT_VAL(i->id));
533 else
534 putchar('\n');
535
8b0cc9a3
LP
536 s1 = format_timestamp_relative(since1, sizeof(since1), i->timestamp.realtime);
537 s2 = format_timestamp(since2, sizeof(since2), i->timestamp.realtime);
1ee306e1
LP
538
539 if (s1)
540 printf("\t Since: %s; %s\n", s2, s1);
541 else if (s2)
542 printf("\t Since: %s\n", s2);
543
544 if (i->leader > 0) {
545 _cleanup_free_ char *t = NULL;
546
547 printf("\t Leader: %u", (unsigned) i->leader);
548
549 get_process_comm(i->leader, &t);
550 if (t)
551 printf(" (%s)", t);
552
553 putchar('\n');
554 }
555
556 if (i->service) {
557 printf("\t Service: %s", i->service);
558
559 if (i->class)
560 printf("; class %s", i->class);
561
562 putchar('\n');
563 } else if (i->class)
564 printf("\t Class: %s\n", i->class);
565
1ee306e1
LP
566 if (i->root_directory)
567 printf("\t Root: %s\n", i->root_directory);
568
f48e75cb
LP
569 if (i->n_netif > 0) {
570 unsigned c;
571
572 fputs("\t Iface:", stdout);
573
574 for (c = 0; c < i->n_netif; c++) {
575 char name[IF_NAMESIZE+1] = "";
576
577 if (if_indextoname(i->netif[c], name)) {
578 fputc(' ', stdout);
579 fputs(name, stdout);
580
581 if (ifi < 0)
582 ifi = i->netif[c];
583 else
584 ifi = 0;
585 } else
586 printf(" %i", i->netif[c]);
587 }
588
589 fputc('\n', stdout);
590 }
591
592 print_addresses(bus, i->name, ifi,
878cd7e9
LP
593 "\t Address: ",
594 "\t ");
595
717603e3
LP
596 print_os_release(bus, i->name, "\t OS: ");
597
89f7c846
LP
598 if (i->unit) {
599 printf("\t Unit: %s\n", i->unit);
600 show_unit_cgroup(bus, i->unit, i->leader);
8b0cc9a3 601
ece174c5 602 if (arg_transport == BUS_TRANSPORT_LOCAL)
8b0cc9a3
LP
603
604 show_journal_by_unit(
605 stdout,
606 i->unit,
607 arg_output,
608 0,
609 i->timestamp.monotonic,
610 arg_lines,
611 0,
612 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
613 SD_JOURNAL_LOCAL_ONLY,
614 true,
615 NULL);
1ee306e1
LP
616 }
617}
618
f48e75cb
LP
619static int map_netif(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
620 MachineStatusInfo *i = userdata;
621 size_t l;
622 const void *v;
623 int r;
624
625 assert_cc(sizeof(int32_t) == sizeof(int));
626 r = sd_bus_message_read_array(m, SD_BUS_TYPE_INT32, &v, &l);
627 if (r < 0)
628 return r;
e7e9b6bb
ZJS
629 if (r == 0)
630 return -EBADMSG;
f48e75cb
LP
631
632 i->n_netif = l / sizeof(int32_t);
633 i->netif = memdup(v, l);
634 if (!i->netif)
635 return -ENOMEM;
636
637 return 0;
638}
639
fefdc04b 640static int show_machine_info(const char *verb, sd_bus *bus, const char *path, bool *new_line) {
a6c61602 641
9f6eb1cd 642 static const struct bus_properties_map map[] = {
8b0cc9a3
LP
643 { "Name", "s", NULL, offsetof(MachineStatusInfo, name) },
644 { "Class", "s", NULL, offsetof(MachineStatusInfo, class) },
645 { "Service", "s", NULL, offsetof(MachineStatusInfo, service) },
646 { "Unit", "s", NULL, offsetof(MachineStatusInfo, unit) },
647 { "RootDirectory", "s", NULL, offsetof(MachineStatusInfo, root_directory) },
648 { "Leader", "u", NULL, offsetof(MachineStatusInfo, leader) },
649 { "Timestamp", "t", NULL, offsetof(MachineStatusInfo, timestamp.realtime) },
650 { "TimestampMonotonic", "t", NULL, offsetof(MachineStatusInfo, timestamp.monotonic) },
651 { "Id", "ay", bus_map_id128, offsetof(MachineStatusInfo, id) },
652 { "NetworkInterfaces", "ai", map_netif, 0 },
9f6eb1cd
KS
653 {}
654 };
a6c61602 655
e7e55dbd 656 _cleanup_(machine_status_info_clear) MachineStatusInfo info = {};
a1da8583
TG
657 int r;
658
56159e0d
LP
659 assert(verb);
660 assert(bus);
1ee306e1
LP
661 assert(path);
662 assert(new_line);
663
9f6eb1cd
KS
664 r = bus_map_all_properties(bus,
665 "org.freedesktop.machine1",
666 path,
667 map,
668 &info);
f647962d
MS
669 if (r < 0)
670 return log_error_errno(r, "Could not get properties: %m");
1ee306e1 671
1ee306e1
LP
672 if (*new_line)
673 printf("\n");
1ee306e1
LP
674 *new_line = true;
675
9f6eb1cd 676 print_machine_status_info(bus, &info);
1ee306e1 677
9f6eb1cd
KS
678 return r;
679}
1ee306e1 680
fefdc04b 681static int show_machine_properties(sd_bus *bus, const char *path, bool *new_line) {
9f6eb1cd 682 int r;
1ee306e1 683
56159e0d
LP
684 assert(bus);
685 assert(path);
686 assert(new_line);
687
9f6eb1cd
KS
688 if (*new_line)
689 printf("\n");
1ee306e1 690
9f6eb1cd 691 *new_line = true;
a1da8583 692
85500523 693 r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_value, arg_all);
a1da8583 694 if (r < 0)
da927ba9 695 log_error_errno(r, "Could not get properties: %m");
1ee306e1 696
a7893c6b 697 return r;
1ee306e1
LP
698}
699
fefdc04b 700static int show_machine(int argc, char *argv[], void *userdata) {
56159e0d 701
4afd3348
LP
702 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
703 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
9f6eb1cd 704 bool properties, new_line = false;
56159e0d
LP
705 sd_bus *bus = userdata;
706 int r = 0, i;
1ee306e1
LP
707
708 assert(bus);
1ee306e1 709
56159e0d 710 properties = !strstr(argv[0], "status");
1ee306e1 711
ea4b98e6 712 pager_open(arg_no_pager, false);
1ee306e1 713
56159e0d 714 if (properties && argc <= 1) {
a1da8583 715
9f6eb1cd 716 /* If no argument is specified, inspect the manager
1ee306e1 717 * itself */
fefdc04b 718 r = show_machine_properties(bus, "/org/freedesktop/machine1", &new_line);
8c841f21 719 if (r < 0)
9f6eb1cd 720 return r;
1ee306e1
LP
721 }
722
56159e0d 723 for (i = 1; i < argc; i++) {
1ee306e1
LP
724 const char *path = NULL;
725
e138e7d7
ZJS
726 r = sd_bus_call_method(bus,
727 "org.freedesktop.machine1",
728 "/org/freedesktop/machine1",
729 "org.freedesktop.machine1.Manager",
730 "GetMachine",
731 &error,
732 &reply,
733 "s", argv[i]);
a1da8583
TG
734 if (r < 0) {
735 log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
736 return r;
737 }
738
739 r = sd_bus_message_read(reply, "o", &path);
5b30bef8
LP
740 if (r < 0)
741 return bus_log_parse_error(r);
1ee306e1 742
9f6eb1cd 743 if (properties)
fefdc04b 744 r = show_machine_properties(bus, path, &new_line);
9f6eb1cd 745 else
fefdc04b
LP
746 r = show_machine_info(argv[0], bus, path, &new_line);
747 }
748
749 return r;
750}
751
752typedef struct ImageStatusInfo {
753 char *name;
754 char *path;
755 char *type;
756 int read_only;
757 usec_t crtime;
758 usec_t mtime;
c19de711 759 uint64_t usage;
b6b18498 760 uint64_t limit;
c19de711 761 uint64_t usage_exclusive;
b6b18498 762 uint64_t limit_exclusive;
fefdc04b
LP
763} ImageStatusInfo;
764
e7e55dbd
DH
765static void image_status_info_clear(ImageStatusInfo *info) {
766 if (info) {
767 free(info->name);
768 free(info->path);
769 free(info->type);
770 zero(*info);
771 }
772}
773
fefdc04b
LP
774static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
775 char ts_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
776 char ts_absolute[FORMAT_TIMESTAMP_MAX], *s2;
b6b18498
LP
777 char bs[FORMAT_BYTES_MAX], *s3;
778 char bs_exclusive[FORMAT_BYTES_MAX], *s4;
fefdc04b
LP
779
780 assert(bus);
781 assert(i);
782
783 if (i->name) {
784 fputs(i->name, stdout);
785 putchar('\n');
786 }
787
9a14fb62 788 if (i->type)
fefdc04b
LP
789 printf("\t Type: %s\n", i->type);
790
791 if (i->path)
792 printf("\t Path: %s\n", i->path);
793
794 printf("\t RO: %s%s%s\n",
795 i->read_only ? ansi_highlight_red() : "",
796 i->read_only ? "read-only" : "writable",
1fc464f6 797 i->read_only ? ansi_normal() : "");
fefdc04b
LP
798
799 s1 = format_timestamp_relative(ts_relative, sizeof(ts_relative), i->crtime);
800 s2 = format_timestamp(ts_absolute, sizeof(ts_absolute), i->crtime);
b6b18498 801 if (s1 && s2)
fefdc04b
LP
802 printf("\t Created: %s; %s\n", s2, s1);
803 else if (s2)
804 printf("\t Created: %s\n", s2);
805
806 s1 = format_timestamp_relative(ts_relative, sizeof(ts_relative), i->mtime);
807 s2 = format_timestamp(ts_absolute, sizeof(ts_absolute), i->mtime);
b6b18498 808 if (s1 && s2)
fefdc04b
LP
809 printf("\tModified: %s; %s\n", s2, s1);
810 else if (s2)
811 printf("\tModified: %s\n", s2);
b6b18498 812
c19de711
LP
813 s3 = format_bytes(bs, sizeof(bs), i->usage);
814 s4 = i->usage_exclusive != i->usage ? format_bytes(bs_exclusive, sizeof(bs_exclusive), i->usage_exclusive) : NULL;
b6b18498 815 if (s3 && s4)
c19de711 816 printf("\t Usage: %s (exclusive: %s)\n", s3, s4);
b6b18498 817 else if (s3)
c19de711 818 printf("\t Usage: %s\n", s3);
b6b18498
LP
819
820 s3 = format_bytes(bs, sizeof(bs), i->limit);
821 s4 = i->limit_exclusive != i->limit ? format_bytes(bs_exclusive, sizeof(bs_exclusive), i->limit_exclusive) : NULL;
822 if (s3 && s4)
823 printf("\t Limit: %s (exclusive: %s)\n", s3, s4);
824 else if (s3)
825 printf("\t Limit: %s\n", s3);
fefdc04b
LP
826}
827
160e3793 828static int show_image_info(sd_bus *bus, const char *path, bool *new_line) {
fefdc04b
LP
829
830 static const struct bus_properties_map map[] = {
b6b18498
LP
831 { "Name", "s", NULL, offsetof(ImageStatusInfo, name) },
832 { "Path", "s", NULL, offsetof(ImageStatusInfo, path) },
833 { "Type", "s", NULL, offsetof(ImageStatusInfo, type) },
834 { "ReadOnly", "b", NULL, offsetof(ImageStatusInfo, read_only) },
835 { "CreationTimestamp", "t", NULL, offsetof(ImageStatusInfo, crtime) },
836 { "ModificationTimestamp", "t", NULL, offsetof(ImageStatusInfo, mtime) },
c19de711 837 { "Usage", "t", NULL, offsetof(ImageStatusInfo, usage) },
b6b18498 838 { "Limit", "t", NULL, offsetof(ImageStatusInfo, limit) },
c19de711 839 { "UsageExclusive", "t", NULL, offsetof(ImageStatusInfo, usage_exclusive) },
b6b18498 840 { "LimitExclusive", "t", NULL, offsetof(ImageStatusInfo, limit_exclusive) },
fefdc04b
LP
841 {}
842 };
843
e7e55dbd 844 _cleanup_(image_status_info_clear) ImageStatusInfo info = {};
fefdc04b
LP
845 int r;
846
fefdc04b
LP
847 assert(bus);
848 assert(path);
849 assert(new_line);
850
851 r = bus_map_all_properties(bus,
852 "org.freedesktop.machine1",
853 path,
854 map,
855 &info);
856 if (r < 0)
857 return log_error_errno(r, "Could not get properties: %m");
858
859 if (*new_line)
860 printf("\n");
861 *new_line = true;
862
863 print_image_status_info(bus, &info);
864
fefdc04b
LP
865 return r;
866}
867
160e3793
LP
868typedef struct PoolStatusInfo {
869 char *path;
870 uint64_t usage;
871 uint64_t limit;
872} PoolStatusInfo;
873
e7e55dbd
DH
874static void pool_status_info_clear(PoolStatusInfo *info) {
875 if (info) {
876 free(info->path);
877 zero(*info);
878 info->usage = -1;
879 info->limit = -1;
880 }
881}
882
160e3793
LP
883static void print_pool_status_info(sd_bus *bus, PoolStatusInfo *i) {
884 char bs[FORMAT_BYTES_MAX], *s;
885
886 if (i->path)
887 printf("\t Path: %s\n", i->path);
888
889 s = format_bytes(bs, sizeof(bs), i->usage);
890 if (s)
891 printf("\t Usage: %s\n", s);
892
893 s = format_bytes(bs, sizeof(bs), i->limit);
894 if (s)
895 printf("\t Limit: %s\n", s);
896}
897
898static int show_pool_info(sd_bus *bus) {
899
900 static const struct bus_properties_map map[] = {
901 { "PoolPath", "s", NULL, offsetof(PoolStatusInfo, path) },
902 { "PoolUsage", "t", NULL, offsetof(PoolStatusInfo, usage) },
903 { "PoolLimit", "t", NULL, offsetof(PoolStatusInfo, limit) },
904 {}
905 };
906
e7e55dbd 907 _cleanup_(pool_status_info_clear) PoolStatusInfo info = {
160e3793
LP
908 .usage = (uint64_t) -1,
909 .limit = (uint64_t) -1,
910 };
911 int r;
912
913 assert(bus);
914
915 r = bus_map_all_properties(bus,
916 "org.freedesktop.machine1",
917 "/org/freedesktop/machine1",
918 map,
919 &info);
920 if (r < 0)
921 return log_error_errno(r, "Could not get properties: %m");
922
923 print_pool_status_info(bus, &info);
924
160e3793
LP
925 return 0;
926}
927
928
fefdc04b
LP
929static int show_image_properties(sd_bus *bus, const char *path, bool *new_line) {
930 int r;
931
932 assert(bus);
933 assert(path);
934 assert(new_line);
935
936 if (*new_line)
937 printf("\n");
938
939 *new_line = true;
940
85500523 941 r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_value, arg_all);
fefdc04b
LP
942 if (r < 0)
943 log_error_errno(r, "Could not get properties: %m");
944
945 return r;
946}
947
948static int show_image(int argc, char *argv[], void *userdata) {
949
4afd3348
LP
950 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
951 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
fefdc04b
LP
952 bool properties, new_line = false;
953 sd_bus *bus = userdata;
954 int r = 0, i;
955
956 assert(bus);
957
958 properties = !strstr(argv[0], "status");
959
ea4b98e6 960 pager_open(arg_no_pager, false);
fefdc04b 961
160e3793 962 if (argc <= 1) {
fefdc04b
LP
963
964 /* If no argument is specified, inspect the manager
965 * itself */
160e3793
LP
966
967 if (properties)
968 r = show_image_properties(bus, "/org/freedesktop/machine1", &new_line);
969 else
970 r = show_pool_info(bus);
fefdc04b
LP
971 if (r < 0)
972 return r;
973 }
974
975 for (i = 1; i < argc; i++) {
976 const char *path = NULL;
977
978 r = sd_bus_call_method(
90adaa25
LP
979 bus,
980 "org.freedesktop.machine1",
981 "/org/freedesktop/machine1",
982 "org.freedesktop.machine1.Manager",
983 "GetImage",
984 &error,
985 &reply,
986 "s", argv[i]);
fefdc04b
LP
987 if (r < 0) {
988 log_error("Could not get path to image: %s", bus_error_message(&error, -r));
989 return r;
990 }
991
992 r = sd_bus_message_read(reply, "o", &path);
993 if (r < 0)
994 return bus_log_parse_error(r);
995
996 if (properties)
997 r = show_image_properties(bus, path, &new_line);
998 else
160e3793 999 r = show_image_info(bus, path, &new_line);
1ee306e1
LP
1000 }
1001
9f6eb1cd 1002 return r;
1ee306e1
LP
1003}
1004
56159e0d 1005static int kill_machine(int argc, char *argv[], void *userdata) {
4afd3348 1006 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
56159e0d 1007 sd_bus *bus = userdata;
90adaa25 1008 int r, i;
1ee306e1 1009
56159e0d 1010 assert(bus);
1ee306e1 1011
acf97e21
LP
1012 polkit_agent_open_if_enabled();
1013
1ee306e1
LP
1014 if (!arg_kill_who)
1015 arg_kill_who = "all";
1016
56159e0d 1017 for (i = 1; i < argc; i++) {
a1da8583 1018 r = sd_bus_call_method(
56159e0d
LP
1019 bus,
1020 "org.freedesktop.machine1",
1021 "/org/freedesktop/machine1",
1022 "org.freedesktop.machine1.Manager",
1023 "KillMachine",
1024 &error,
1025 NULL,
1026 "ssi", argv[i], arg_kill_who, arg_signal);
a1da8583
TG
1027 if (r < 0) {
1028 log_error("Could not kill machine: %s", bus_error_message(&error, -r));
1ee306e1 1029 return r;
a1da8583 1030 }
1ee306e1
LP
1031 }
1032
1033 return 0;
1034}
1035
56159e0d 1036static int reboot_machine(int argc, char *argv[], void *userdata) {
1dba654b
LP
1037 arg_kill_who = "leader";
1038 arg_signal = SIGINT; /* sysvinit + systemd */
1ee306e1 1039
56159e0d 1040 return kill_machine(argc, argv, userdata);
1dba654b 1041}
1ee306e1 1042
56159e0d 1043static int poweroff_machine(int argc, char *argv[], void *userdata) {
1dba654b
LP
1044 arg_kill_who = "leader";
1045 arg_signal = SIGRTMIN+4; /* only systemd */
1ee306e1 1046
56159e0d 1047 return kill_machine(argc, argv, userdata);
1ee306e1
LP
1048}
1049
56159e0d 1050static int terminate_machine(int argc, char *argv[], void *userdata) {
4afd3348 1051 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
56159e0d 1052 sd_bus *bus = userdata;
2723b3b5 1053 int r, i;
923d8fd3 1054
56159e0d 1055 assert(bus);
923d8fd3 1056
acf97e21
LP
1057 polkit_agent_open_if_enabled();
1058
56159e0d 1059 for (i = 1; i < argc; i++) {
923d8fd3
LP
1060 r = sd_bus_call_method(
1061 bus,
1062 "org.freedesktop.machine1",
1063 "/org/freedesktop/machine1",
1064 "org.freedesktop.machine1.Manager",
1dba654b 1065 "TerminateMachine",
923d8fd3 1066 &error,
1dba654b 1067 NULL,
56159e0d 1068 "s", argv[i]);
923d8fd3 1069 if (r < 0) {
1dba654b 1070 log_error("Could not terminate machine: %s", bus_error_message(&error, -r));
923d8fd3
LP
1071 return r;
1072 }
923d8fd3
LP
1073 }
1074
1075 return 0;
1076}
1077
0370612e 1078static int copy_files(int argc, char *argv[], void *userdata) {
4afd3348 1079 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d87174d 1080 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1fe6fa16
RM
1081 _cleanup_free_ char *abs_host_path = NULL;
1082 char *dest, *host_path, *container_path;
0370612e
LP
1083 sd_bus *bus = userdata;
1084 bool copy_from;
785890ac
LP
1085 int r;
1086
1087 assert(bus);
0370612e 1088
2723b3b5
LP
1089 polkit_agent_open_if_enabled();
1090
0370612e 1091 copy_from = streq(argv[0], "copy-from");
1fe6fa16
RM
1092 dest = argv[3] ?: argv[2];
1093 host_path = copy_from ? dest : argv[2];
1094 container_path = copy_from ? argv[2] : dest;
1095
1096 if (!path_is_absolute(host_path)) {
0f474365
LP
1097 r = path_make_absolute_cwd(host_path, &abs_host_path);
1098 if (r < 0)
1099 return log_error_errno(r, "Failed to make path absolute: %m");
1100
1fe6fa16
RM
1101 host_path = abs_host_path;
1102 }
785890ac 1103
3d87174d 1104 r = sd_bus_message_new_method_call(
785890ac 1105 bus,
3d87174d 1106 &m,
785890ac
LP
1107 "org.freedesktop.machine1",
1108 "/org/freedesktop/machine1",
1109 "org.freedesktop.machine1.Manager",
3d87174d
LP
1110 copy_from ? "CopyFromMachine" : "CopyToMachine");
1111 if (r < 0)
1112 return bus_log_create_error(r);
1113
1114 r = sd_bus_message_append(
1115 m,
0370612e
LP
1116 "sss",
1117 argv[1],
1fe6fa16
RM
1118 copy_from ? container_path : host_path,
1119 copy_from ? host_path : container_path);
3d87174d
LP
1120 if (r < 0)
1121 return bus_log_create_error(r);
1122
1123 /* This is a slow operation, hence turn off any method call timeouts */
1124 r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
0f474365
LP
1125 if (r < 0)
1126 return log_error_errno(r, "Failed to copy: %s", bus_error_message(&error, r));
f2cbe59e
LP
1127
1128 return 0;
1129}
1130
56159e0d 1131static int bind_mount(int argc, char *argv[], void *userdata) {
4afd3348 1132 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
56159e0d 1133 sd_bus *bus = userdata;
785890ac
LP
1134 int r;
1135
56159e0d
LP
1136 assert(bus);
1137
2723b3b5
LP
1138 polkit_agent_open_if_enabled();
1139
90adaa25
LP
1140 r = sd_bus_call_method(
1141 bus,
1142 "org.freedesktop.machine1",
1143 "/org/freedesktop/machine1",
1144 "org.freedesktop.machine1.Manager",
1145 "BindMountMachine",
1146 &error,
1147 NULL,
1148 "sssbb",
1149 argv[1],
1150 argv[2],
1151 argv[3],
1152 arg_read_only,
1153 arg_mkdir);
785890ac 1154 if (r < 0) {
90adaa25
LP
1155 log_error("Failed to bind mount: %s", bus_error_message(&error, -r));
1156 return r;
785890ac
LP
1157 }
1158
90adaa25 1159 return 0;
785890ac
LP
1160}
1161
19070062 1162static int on_machine_removed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
0ec5543c
LP
1163 PTYForward ** forward = (PTYForward**) userdata;
1164 int r;
1165
0ec5543c
LP
1166 assert(m);
1167 assert(forward);
1168
1169 if (*forward) {
1170 /* If the forwarder is already initialized, tell it to
da054c37
LP
1171 * exit on the next vhangup(), so that we still flush
1172 * out what might be queued and exit then. */
0ec5543c 1173
da054c37 1174 r = pty_forward_set_ignore_vhangup(*forward, false);
0ec5543c
LP
1175 if (r >= 0)
1176 return 0;
1177
da054c37 1178 log_error_errno(r, "Failed to set ignore_vhangup flag: %m");
0ec5543c
LP
1179 }
1180
1181 /* On error, or when the forwarder is not initialized yet, quit immediately */
19070062 1182 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), EXIT_FAILURE);
0ec5543c
LP
1183 return 0;
1184}
1185
ae3dde80 1186static int process_forward(sd_event *event, PTYForward **forward, int master, PTYForwardFlags flags, const char *name) {
c454426c
LP
1187 char last_char = 0;
1188 bool machine_died;
1189 int ret = 0, r;
1190
1191 assert(event);
1192 assert(master >= 0);
1193 assert(name);
1194
1195 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
1196
91913f58
LP
1197 if (streq(name, ".host"))
1198 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1199 else
1200 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name);
c454426c
LP
1201
1202 sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
1203 sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
1204
ae3dde80 1205 r = pty_forward_new(event, master, flags, forward);
c454426c
LP
1206 if (r < 0)
1207 return log_error_errno(r, "Failed to create PTY forwarder: %m");
1208
1209 r = sd_event_loop(event);
1210 if (r < 0)
1211 return log_error_errno(r, "Failed to run event loop: %m");
1212
1213 pty_forward_get_last_char(*forward, &last_char);
1214
1215 machine_died =
ae3dde80 1216 (flags & PTY_FORWARD_IGNORE_VHANGUP) &&
c454426c
LP
1217 pty_forward_get_ignore_vhangup(*forward) == 0;
1218
1219 *forward = pty_forward_free(*forward);
1220
1221 if (last_char != '\n')
1222 fputc('\n', stdout);
1223
1224 if (machine_died)
1225 log_info("Machine %s terminated.", name);
91913f58
LP
1226 else if (streq(name, ".host"))
1227 log_info("Connection to the local host terminated.");
c454426c
LP
1228 else
1229 log_info("Connection to machine %s terminated.", name);
1230
1231 sd_event_get_exit_code(event, &ret);
1232 return ret;
1233}
1234
56159e0d 1235static int login_machine(int argc, char *argv[], void *userdata) {
4afd3348
LP
1236 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1237 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
023fb90b 1238 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
4afd3348
LP
1239 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
1240 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
c454426c 1241 int master = -1, r;
56159e0d 1242 sd_bus *bus = userdata;
91913f58 1243 const char *pty, *match, *machine;
04d39279
LP
1244
1245 assert(bus);
04d39279 1246
c454426c
LP
1247 if (!strv_isempty(arg_setenv) || arg_uid) {
1248 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1249 return -EINVAL;
1250 }
1251
bf441e3d 1252 if (arg_transport != BUS_TRANSPORT_LOCAL &&
de33fc62 1253 arg_transport != BUS_TRANSPORT_MACHINE) {
923d8fd3 1254 log_error("Login only supported on local machines.");
15411c0c 1255 return -EOPNOTSUPP;
04d39279
LP
1256 }
1257
acf97e21
LP
1258 polkit_agent_open_if_enabled();
1259
023fb90b 1260 r = sd_event_default(&event);
f647962d
MS
1261 if (r < 0)
1262 return log_error_errno(r, "Failed to get event loop: %m");
023fb90b
LP
1263
1264 r = sd_bus_attach_event(bus, event, 0);
f647962d
MS
1265 if (r < 0)
1266 return log_error_errno(r, "Failed to attach bus to event loop: %m");
023fb90b 1267
91913f58
LP
1268 machine = argc < 2 || isempty(argv[1]) ? ".host" : argv[1];
1269
63c372cb 1270 match = strjoina("type='signal',"
c454426c
LP
1271 "sender='org.freedesktop.machine1',"
1272 "path='/org/freedesktop/machine1',",
1273 "interface='org.freedesktop.machine1.Manager',"
1274 "member='MachineRemoved',"
91913f58 1275 "arg0='", machine, "'");
0ec5543c
LP
1276
1277 r = sd_bus_add_match(bus, &slot, match, on_machine_removed, &forward);
1278 if (r < 0)
1279 return log_error_errno(r, "Failed to add machine removal match: %m");
1280
2723b3b5
LP
1281 r = sd_bus_call_method(
1282 bus,
1283 "org.freedesktop.machine1",
1284 "/org/freedesktop/machine1",
1285 "org.freedesktop.machine1.Manager",
1286 "OpenMachineLogin",
1287 &error,
1288 &reply,
91913f58 1289 "s", machine);
40205d70 1290 if (r < 0) {
c454426c 1291 log_error("Failed to get login PTY: %s", bus_error_message(&error, -r));
785890ac 1292 return r;
40205d70 1293 }
04d39279 1294
40205d70
LP
1295 r = sd_bus_message_read(reply, "hs", &master, &pty);
1296 if (r < 0)
ee451d76 1297 return bus_log_parse_error(r);
04d39279 1298
ae3dde80 1299 return process_forward(event, &forward, master, PTY_FORWARD_IGNORE_VHANGUP, machine);
c454426c 1300}
04d39279 1301
c454426c 1302static int shell_machine(int argc, char *argv[], void *userdata) {
4afd3348
LP
1303 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
1304 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c454426c 1305 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
4afd3348
LP
1306 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
1307 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
c454426c
LP
1308 int master = -1, r;
1309 sd_bus *bus = userdata;
ef3100e9 1310 const char *pty, *match, *machine, *path, *uid = NULL;
04d39279 1311
c454426c
LP
1312 assert(bus);
1313
1314 if (arg_transport != BUS_TRANSPORT_LOCAL &&
1315 arg_transport != BUS_TRANSPORT_MACHINE) {
1316 log_error("Shell only supported on local machines.");
1317 return -EOPNOTSUPP;
1318 }
1319
89fec318
LP
1320 /* Pass $TERM to shell session, if not explicitly specified. */
1321 if (!strv_find_prefix(arg_setenv, "TERM=")) {
1322 const char *t;
1323
1324 t = strv_find_prefix(environ, "TERM=");
1325 if (t) {
1326 if (strv_extend(&arg_setenv, t) < 0)
1327 return log_oom();
1328 }
1329 }
1330
c454426c 1331 polkit_agent_open_if_enabled();
023fb90b 1332
c454426c 1333 r = sd_event_default(&event);
f647962d 1334 if (r < 0)
c454426c 1335 return log_error_errno(r, "Failed to get event loop: %m");
023fb90b 1336
c454426c 1337 r = sd_bus_attach_event(bus, event, 0);
f647962d 1338 if (r < 0)
c454426c 1339 return log_error_errno(r, "Failed to attach bus to event loop: %m");
04d39279 1340
ef3100e9
LP
1341 machine = argc < 2 || isempty(argv[1]) ? NULL : argv[1];
1342
1343 if (arg_uid)
1344 uid = arg_uid;
1345 else if (machine) {
1346 const char *at;
1347
1348 at = strchr(machine, '@');
1349 if (at) {
1350 uid = strndupa(machine, at - machine);
1351 machine = at + 1;
1352 }
1353 }
1354
1355 if (isempty(machine))
1356 machine = ".host";
91913f58 1357
c454426c
LP
1358 match = strjoina("type='signal',"
1359 "sender='org.freedesktop.machine1',"
1360 "path='/org/freedesktop/machine1',",
1361 "interface='org.freedesktop.machine1.Manager',"
1362 "member='MachineRemoved',"
91913f58 1363 "arg0='", machine, "'");
c7b7d449 1364
c454426c
LP
1365 r = sd_bus_add_match(bus, &slot, match, on_machine_removed, &forward);
1366 if (r < 0)
1367 return log_error_errno(r, "Failed to add machine removal match: %m");
023fb90b 1368
c454426c
LP
1369 r = sd_bus_message_new_method_call(
1370 bus,
1371 &m,
1372 "org.freedesktop.machine1",
1373 "/org/freedesktop/machine1",
1374 "org.freedesktop.machine1.Manager",
1375 "OpenMachineShell");
1376 if (r < 0)
1377 return bus_log_create_error(r);
04d39279 1378
91913f58
LP
1379 path = argc < 3 || isempty(argv[2]) ? NULL : argv[2];
1380
ef3100e9 1381 r = sd_bus_message_append(m, "sss", machine, uid, path);
c454426c
LP
1382 if (r < 0)
1383 return bus_log_create_error(r);
04d39279 1384
91913f58 1385 r = sd_bus_message_append_strv(m, strv_length(argv) <= 3 ? NULL : argv + 2);
c454426c
LP
1386 if (r < 0)
1387 return bus_log_create_error(r);
1388
1389 r = sd_bus_message_append_strv(m, arg_setenv);
1390 if (r < 0)
1391 return bus_log_create_error(r);
1392
1393 r = sd_bus_call(bus, m, 0, &error, &reply);
1394 if (r < 0) {
1395 log_error("Failed to get shell PTY: %s", bus_error_message(&error, -r));
1396 return r;
1397 }
1398
1399 r = sd_bus_message_read(reply, "hs", &master, &pty);
1400 if (r < 0)
1401 return bus_log_parse_error(r);
1402
40e1f4ea 1403 return process_forward(event, &forward, master, 0, machine);
04d39279
LP
1404}
1405
08682124 1406static int remove_image(int argc, char *argv[], void *userdata) {
08682124 1407 sd_bus *bus = userdata;
ebd93cb6 1408 int r, i;
08682124
LP
1409
1410 assert(bus);
1411
acf97e21
LP
1412 polkit_agent_open_if_enabled();
1413
08682124 1414 for (i = 1; i < argc; i++) {
3d87174d
LP
1415 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1416 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1417
1418 r = sd_bus_message_new_method_call(
08682124 1419 bus,
3d87174d 1420 &m,
08682124
LP
1421 "org.freedesktop.machine1",
1422 "/org/freedesktop/machine1",
1423 "org.freedesktop.machine1.Manager",
3d87174d
LP
1424 "RemoveImage");
1425 if (r < 0)
1426 return bus_log_create_error(r);
1427
1428 r = sd_bus_message_append(m, "s", argv[i]);
1429 if (r < 0)
1430 return bus_log_create_error(r);
1431
1432 /* This is a slow operation, hence turn off any method call timeouts */
1433 r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
1434 if (r < 0)
1435 return log_error_errno(r, "Could not remove image: %s", bus_error_message(&error, r));
08682124
LP
1436 }
1437
1438 return 0;
1439}
1440
ebd93cb6 1441static int rename_image(int argc, char *argv[], void *userdata) {
4afd3348 1442 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ebd93cb6
LP
1443 sd_bus *bus = userdata;
1444 int r;
1445
acf97e21
LP
1446 polkit_agent_open_if_enabled();
1447
ebd93cb6
LP
1448 r = sd_bus_call_method(
1449 bus,
1450 "org.freedesktop.machine1",
1451 "/org/freedesktop/machine1",
1452 "org.freedesktop.machine1.Manager",
1453 "RenameImage",
1454 &error,
1455 NULL,
1456 "ss", argv[1], argv[2]);
1457 if (r < 0) {
1458 log_error("Could not rename image: %s", bus_error_message(&error, -r));
1459 return r;
1460 }
1461
1462 return 0;
1463}
1464
1465static int clone_image(int argc, char *argv[], void *userdata) {
4afd3348 1466 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d87174d 1467 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
ebd93cb6
LP
1468 sd_bus *bus = userdata;
1469 int r;
1470
acf97e21
LP
1471 polkit_agent_open_if_enabled();
1472
3d87174d 1473 r = sd_bus_message_new_method_call(
ebd93cb6 1474 bus,
3d87174d 1475 &m,
ebd93cb6
LP
1476 "org.freedesktop.machine1",
1477 "/org/freedesktop/machine1",
1478 "org.freedesktop.machine1.Manager",
3d87174d
LP
1479 "CloneImage");
1480 if (r < 0)
1481 return bus_log_create_error(r);
1482
1483 r = sd_bus_message_append(m, "ssb", argv[1], argv[2], arg_read_only);
1484 if (r < 0)
1485 return bus_log_create_error(r);
1486
1487 /* This is a slow operation, hence turn off any method call timeouts */
1488 r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
1489 if (r < 0)
1490 return log_error_errno(r, "Could not clone image: %s", bus_error_message(&error, r));
ebd93cb6
LP
1491
1492 return 0;
1493}
1494
1495static int read_only_image(int argc, char *argv[], void *userdata) {
4afd3348 1496 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ebd93cb6
LP
1497 sd_bus *bus = userdata;
1498 int b = true, r;
1499
1500 if (argc > 2) {
1501 b = parse_boolean(argv[2]);
1502 if (b < 0) {
1503 log_error("Failed to parse boolean argument: %s", argv[2]);
1504 return -EINVAL;
1505 }
1506 }
1507
acf97e21
LP
1508 polkit_agent_open_if_enabled();
1509
ebd93cb6
LP
1510 r = sd_bus_call_method(
1511 bus,
1512 "org.freedesktop.machine1",
1513 "/org/freedesktop/machine1",
1514 "org.freedesktop.machine1.Manager",
1515 "MarkImageReadOnly",
1516 &error,
1517 NULL,
1518 "sb", argv[1], b);
1519 if (r < 0) {
1520 log_error("Could not mark image read-only: %s", bus_error_message(&error, -r));
1521 return r;
1522 }
1523
1524 return 0;
1525}
1526
68ce459f
LP
1527static int image_exists(sd_bus *bus, const char *name) {
1528 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1529 int r;
1530
1531 assert(bus);
1532 assert(name);
1533
1534 r = sd_bus_call_method(
1535 bus,
1536 "org.freedesktop.machine1",
1537 "/org/freedesktop/machine1",
1538 "org.freedesktop.machine1.Manager",
1539 "GetImage",
1540 &error,
1541 NULL,
1542 "s", name);
1543 if (r < 0) {
1544 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_IMAGE))
1545 return 0;
1546
1547 return log_error_errno(r, "Failed to check whether image %s exists: %s", name, bus_error_message(&error, -r));
1548 }
1549
1550 return 1;
1551}
1552
7410616c 1553static int make_service_name(const char *name, char **ret) {
7410616c
LP
1554 int r;
1555
1556 assert(name);
1557 assert(ret);
1558
1559 if (!machine_name_is_valid(name)) {
1560 log_error("Invalid machine name %s.", name);
1561 return -EINVAL;
1562 }
1563
1e9707d4 1564 r = unit_name_build("systemd-nspawn", name, ".service", ret);
7410616c
LP
1565 if (r < 0)
1566 return log_error_errno(r, "Failed to build unit name: %m");
1567
1568 return 0;
1569}
1570
ebd011d9 1571static int start_machine(int argc, char *argv[], void *userdata) {
4afd3348 1572 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ebd011d9
LP
1573 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
1574 sd_bus *bus = userdata;
1575 int r, i;
1576
1577 assert(bus);
1578
acf97e21
LP
1579 polkit_agent_open_if_enabled();
1580
ebd011d9
LP
1581 r = bus_wait_for_jobs_new(bus, &w);
1582 if (r < 0)
1583 return log_oom();
1584
1585 for (i = 1; i < argc; i++) {
4afd3348 1586 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
7410616c 1587 _cleanup_free_ char *unit = NULL;
ebd011d9
LP
1588 const char *object;
1589
7410616c
LP
1590 r = make_service_name(argv[i], &unit);
1591 if (r < 0)
1592 return r;
ebd011d9 1593
68ce459f
LP
1594 r = image_exists(bus, argv[i]);
1595 if (r < 0)
1596 return r;
1597 if (r == 0) {
1598 log_error("Machine image '%s' does not exist.", argv[1]);
1599 return -ENXIO;
1600 }
1601
2723b3b5 1602 r = sd_bus_call_method(
ebd011d9 1603 bus,
ebd011d9
LP
1604 "org.freedesktop.systemd1",
1605 "/org/freedesktop/systemd1",
1606 "org.freedesktop.systemd1.Manager",
2723b3b5
LP
1607 "StartUnit",
1608 &error,
1609 &reply,
1610 "ss", unit, "fail");
ebd011d9
LP
1611 if (r < 0) {
1612 log_error("Failed to start unit: %s", bus_error_message(&error, -r));
1613 return r;
1614 }
1615
1616 r = sd_bus_message_read(reply, "o", &object);
1617 if (r < 0)
1618 return bus_log_parse_error(r);
1619
1620 r = bus_wait_for_jobs_add(w, object);
1621 if (r < 0)
1622 return log_oom();
1623 }
1624
10ba4835 1625 r = bus_wait_for_jobs(w, arg_quiet, NULL);
ebd011d9
LP
1626 if (r < 0)
1627 return r;
1628
1629 return 0;
1630}
1631
d8f52ed2 1632static int enable_machine(int argc, char *argv[], void *userdata) {
4afd3348
LP
1633 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1634 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
acc0269c
CH
1635 UnitFileChange *changes = NULL;
1636 unsigned n_changes = 0;
d8f52ed2
LP
1637 int carries_install_info = 0;
1638 const char *method = NULL;
1639 sd_bus *bus = userdata;
1640 int r, i;
1641
1642 assert(bus);
1643
acf97e21
LP
1644 polkit_agent_open_if_enabled();
1645
d8f52ed2
LP
1646 method = streq(argv[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1647
1648 r = sd_bus_message_new_method_call(
1649 bus,
1650 &m,
1651 "org.freedesktop.systemd1",
1652 "/org/freedesktop/systemd1",
1653 "org.freedesktop.systemd1.Manager",
1654 method);
1655 if (r < 0)
1656 return bus_log_create_error(r);
1657
d8f52ed2
LP
1658 r = sd_bus_message_open_container(m, 'a', "s");
1659 if (r < 0)
1660 return bus_log_create_error(r);
1661
1662 for (i = 1; i < argc; i++) {
90615ad7 1663 _cleanup_free_ char *unit = NULL;
d8f52ed2 1664
7410616c
LP
1665 r = make_service_name(argv[i], &unit);
1666 if (r < 0)
1667 return r;
d8f52ed2 1668
68ce459f
LP
1669 r = image_exists(bus, argv[i]);
1670 if (r < 0)
1671 return r;
1672 if (r == 0) {
1673 log_error("Machine image '%s' does not exist.", argv[1]);
1674 return -ENXIO;
1675 }
1676
d8f52ed2
LP
1677 r = sd_bus_message_append(m, "s", unit);
1678 if (r < 0)
1679 return bus_log_create_error(r);
1680 }
1681
1682 r = sd_bus_message_close_container(m);
1683 if (r < 0)
1684 return bus_log_create_error(r);
1685
1686 if (streq(argv[0], "enable"))
1687 r = sd_bus_message_append(m, "bb", false, false);
1688 else
1689 r = sd_bus_message_append(m, "b", false);
1690 if (r < 0)
1691 return bus_log_create_error(r);
1692
1693 r = sd_bus_call(bus, m, 0, &error, &reply);
1694 if (r < 0) {
1695 log_error("Failed to enable or disable unit: %s", bus_error_message(&error, -r));
1696 return r;
1697 }
1698
1699 if (streq(argv[0], "enable")) {
1700 r = sd_bus_message_read(reply, "b", carries_install_info);
1701 if (r < 0)
1702 return bus_log_parse_error(r);
1703 }
1704
acc0269c 1705 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
d8f52ed2 1706 if (r < 0)
acc0269c 1707 goto finish;
d8f52ed2 1708
2723b3b5 1709 r = sd_bus_call_method(
d8f52ed2 1710 bus,
d8f52ed2
LP
1711 "org.freedesktop.systemd1",
1712 "/org/freedesktop/systemd1",
1713 "org.freedesktop.systemd1.Manager",
2723b3b5
LP
1714 "Reload",
1715 &error,
1716 NULL,
1717 NULL);
d8f52ed2
LP
1718 if (r < 0) {
1719 log_error("Failed to reload daemon: %s", bus_error_message(&error, -r));
acc0269c 1720 goto finish;
d8f52ed2
LP
1721 }
1722
acc0269c
CH
1723 r = 0;
1724
1725finish:
1726 unit_file_changes_free(changes, n_changes);
1727
1728 return r;
d8f52ed2
LP
1729}
1730
19070062 1731static int match_log_message(sd_bus_message *m, void *userdata, sd_bus_error *error) {
6adf7b5e 1732 const char **our_path = userdata, *line;
3d7415f4
LP
1733 unsigned priority;
1734 int r;
1735
3d7415f4 1736 assert(m);
6adf7b5e 1737 assert(our_path);
3d7415f4
LP
1738
1739 r = sd_bus_message_read(m, "us", &priority, &line);
1740 if (r < 0) {
1741 bus_log_parse_error(r);
1742 return 0;
1743 }
1744
6adf7b5e 1745 if (!streq_ptr(*our_path, sd_bus_message_get_path(m)))
3d7415f4
LP
1746 return 0;
1747
1748 if (arg_quiet && LOG_PRI(priority) >= LOG_INFO)
1749 return 0;
1750
1751 log_full(priority, "%s", line);
1752 return 0;
1753}
1754
19070062 1755static int match_transfer_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
6adf7b5e 1756 const char **our_path = userdata, *path, *result;
3d7415f4
LP
1757 uint32_t id;
1758 int r;
1759
3d7415f4 1760 assert(m);
6adf7b5e 1761 assert(our_path);
3d7415f4
LP
1762
1763 r = sd_bus_message_read(m, "uos", &id, &path, &result);
1764 if (r < 0) {
1765 bus_log_parse_error(r);
1766 return 0;
1767 }
1768
6adf7b5e 1769 if (!streq_ptr(*our_path, path))
3d7415f4
LP
1770 return 0;
1771
19070062 1772 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), !streq_ptr(result, "done"));
6adf7b5e
LP
1773 return 0;
1774}
1775
1776static int transfer_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1777 assert(s);
1778 assert(si);
1779
1780 if (!arg_quiet)
cc98b302 1781 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32 "\" to abort transfer.", PTR_TO_UINT32(userdata));
6adf7b5e
LP
1782
1783 sd_event_exit(sd_event_source_get_event(s), EINTR);
3d7415f4
LP
1784 return 0;
1785}
1786
587fec42 1787static int transfer_image_common(sd_bus *bus, sd_bus_message *m) {
4afd3348
LP
1788 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_job_removed = NULL, *slot_log_message = NULL;
1789 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1790 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1791 _cleanup_(sd_event_unrefp) sd_event* event = NULL;
6adf7b5e 1792 const char *path = NULL;
3d7415f4
LP
1793 uint32_t id;
1794 int r;
1795
1796 assert(bus);
1797 assert(m);
1798
1799 polkit_agent_open_if_enabled();
1800
6adf7b5e
LP
1801 r = sd_event_default(&event);
1802 if (r < 0)
1803 return log_error_errno(r, "Failed to get event loop: %m");
1804
1805 r = sd_bus_attach_event(bus, event, 0);
1806 if (r < 0)
1807 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1808
3d7415f4
LP
1809 r = sd_bus_add_match(
1810 bus,
1811 &slot_job_removed,
1812 "type='signal',"
1813 "sender='org.freedesktop.import1',"
1814 "interface='org.freedesktop.import1.Manager',"
1815 "member='TransferRemoved',"
1816 "path='/org/freedesktop/import1'",
6adf7b5e 1817 match_transfer_removed, &path);
3d7415f4
LP
1818 if (r < 0)
1819 return log_error_errno(r, "Failed to install match: %m");
1820
1821 r = sd_bus_add_match(
1822 bus,
1823 &slot_log_message,
1824 "type='signal',"
1825 "sender='org.freedesktop.import1',"
1826 "interface='org.freedesktop.import1.Transfer',"
1827 "member='LogMessage'",
6adf7b5e 1828 match_log_message, &path);
3d7415f4
LP
1829 if (r < 0)
1830 return log_error_errno(r, "Failed to install match: %m");
1831
1832 r = sd_bus_call(bus, m, 0, &error, &reply);
1833 if (r < 0) {
953d28cc 1834 log_error("Failed to transfer image: %s", bus_error_message(&error, -r));
3d7415f4
LP
1835 return r;
1836 }
1837
6adf7b5e 1838 r = sd_bus_message_read(reply, "uo", &id, &path);
3d7415f4
LP
1839 if (r < 0)
1840 return bus_log_parse_error(r);
1841
72c0a2c2 1842 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
3d7415f4 1843
6adf7b5e
LP
1844 if (!arg_quiet)
1845 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id);
3d7415f4 1846
6adf7b5e
LP
1847 sd_event_add_signal(event, NULL, SIGINT, transfer_signal_handler, UINT32_TO_PTR(id));
1848 sd_event_add_signal(event, NULL, SIGTERM, transfer_signal_handler, UINT32_TO_PTR(id));
1849
1850 r = sd_event_loop(event);
1851 if (r < 0)
1852 return log_error_errno(r, "Failed to run event loop: %m");
3d7415f4 1853
6adf7b5e 1854 return -r;
3d7415f4
LP
1855}
1856
b6e676ce 1857static int import_tar(int argc, char *argv[], void *userdata) {
4afd3348 1858 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
b6e676ce
LP
1859 _cleanup_free_ char *ll = NULL;
1860 _cleanup_close_ int fd = -1;
1861 const char *local = NULL, *path = NULL;
1862 sd_bus *bus = userdata;
1863 int r;
1864
1865 assert(bus);
1866
1867 if (argc >= 2)
1868 path = argv[1];
1869 if (isempty(path) || streq(path, "-"))
1870 path = NULL;
1871
1872 if (argc >= 3)
1873 local = argv[2];
1874 else if (path)
1875 local = basename(path);
1876 if (isempty(local) || streq(local, "-"))
1877 local = NULL;
1878
1879 if (!local) {
1880 log_error("Need either path or local name.");
1881 return -EINVAL;
1882 }
1883
1884 r = tar_strip_suffixes(local, &ll);
1885 if (r < 0)
1886 return log_oom();
1887
1888 local = ll;
1889
1890 if (!machine_name_is_valid(local)) {
1891 log_error("Local name %s is not a suitable machine name.", local);
1892 return -EINVAL;
1893 }
1894
1895 if (path) {
1896 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
1897 if (fd < 0)
1898 return log_error_errno(errno, "Failed to open %s: %m", path);
1899 }
1900
1901 r = sd_bus_message_new_method_call(
1902 bus,
1903 &m,
1904 "org.freedesktop.import1",
1905 "/org/freedesktop/import1",
1906 "org.freedesktop.import1.Manager",
1907 "ImportTar");
1908 if (r < 0)
1909 return bus_log_create_error(r);
1910
1911 r = sd_bus_message_append(
1912 m,
1913 "hsbb",
1914 fd >= 0 ? fd : STDIN_FILENO,
1915 local,
1916 arg_force,
1917 arg_read_only);
1918 if (r < 0)
1919 return bus_log_create_error(r);
1920
587fec42 1921 return transfer_image_common(bus, m);
b6e676ce
LP
1922}
1923
1924static int import_raw(int argc, char *argv[], void *userdata) {
4afd3348 1925 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
b6e676ce
LP
1926 _cleanup_free_ char *ll = NULL;
1927 _cleanup_close_ int fd = -1;
1928 const char *local = NULL, *path = NULL;
1929 sd_bus *bus = userdata;
1930 int r;
1931
1932 assert(bus);
1933
1934 if (argc >= 2)
1935 path = argv[1];
1936 if (isempty(path) || streq(path, "-"))
1937 path = NULL;
1938
1939 if (argc >= 3)
1940 local = argv[2];
1941 else if (path)
1942 local = basename(path);
1943 if (isempty(local) || streq(local, "-"))
1944 local = NULL;
1945
1946 if (!local) {
1947 log_error("Need either path or local name.");
1948 return -EINVAL;
1949 }
1950
1951 r = raw_strip_suffixes(local, &ll);
1952 if (r < 0)
1953 return log_oom();
1954
1955 local = ll;
1956
1957 if (!machine_name_is_valid(local)) {
1958 log_error("Local name %s is not a suitable machine name.", local);
1959 return -EINVAL;
1960 }
1961
1962 if (path) {
1963 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
1964 if (fd < 0)
1965 return log_error_errno(errno, "Failed to open %s: %m", path);
1966 }
1967
1968 r = sd_bus_message_new_method_call(
1969 bus,
1970 &m,
1971 "org.freedesktop.import1",
1972 "/org/freedesktop/import1",
1973 "org.freedesktop.import1.Manager",
1974 "ImportRaw");
1975 if (r < 0)
1976 return bus_log_create_error(r);
1977
1978 r = sd_bus_message_append(
1979 m,
1980 "hsbb",
1981 fd >= 0 ? fd : STDIN_FILENO,
1982 local,
1983 arg_force,
1984 arg_read_only);
1985 if (r < 0)
1986 return bus_log_create_error(r);
1987
587fec42
LP
1988 return transfer_image_common(bus, m);
1989}
1990
1991static void determine_compression_from_filename(const char *p) {
1992 if (arg_format)
1993 return;
1994
1995 if (!p)
1996 return;
1997
1998 if (endswith(p, ".xz"))
1999 arg_format = "xz";
2000 else if (endswith(p, ".gz"))
2001 arg_format = "gzip";
2002 else if (endswith(p, ".bz2"))
2003 arg_format = "bzip2";
2004}
2005
2006static int export_tar(int argc, char *argv[], void *userdata) {
4afd3348 2007 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
587fec42
LP
2008 _cleanup_close_ int fd = -1;
2009 const char *local = NULL, *path = NULL;
2010 sd_bus *bus = userdata;
2011 int r;
2012
2013 assert(bus);
2014
2015 local = argv[1];
2016 if (!machine_name_is_valid(local)) {
2017 log_error("Machine name %s is not valid.", local);
2018 return -EINVAL;
2019 }
2020
2021 if (argc >= 3)
2022 path = argv[2];
2023 if (isempty(path) || streq(path, "-"))
2024 path = NULL;
2025
2026 if (path) {
2027 determine_compression_from_filename(path);
2028
2029 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
2030 if (fd < 0)
2031 return log_error_errno(errno, "Failed to open %s: %m", path);
2032 }
2033
2034 r = sd_bus_message_new_method_call(
2035 bus,
2036 &m,
2037 "org.freedesktop.import1",
2038 "/org/freedesktop/import1",
2039 "org.freedesktop.import1.Manager",
2040 "ExportTar");
2041 if (r < 0)
2042 return bus_log_create_error(r);
2043
2044 r = sd_bus_message_append(
2045 m,
2046 "shs",
2047 local,
2048 fd >= 0 ? fd : STDOUT_FILENO,
2049 arg_format);
2050 if (r < 0)
2051 return bus_log_create_error(r);
2052
2053 return transfer_image_common(bus, m);
2054}
2055
2056static int export_raw(int argc, char *argv[], void *userdata) {
4afd3348 2057 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
587fec42
LP
2058 _cleanup_close_ int fd = -1;
2059 const char *local = NULL, *path = NULL;
2060 sd_bus *bus = userdata;
2061 int r;
2062
2063 assert(bus);
2064
2065 local = argv[1];
2066 if (!machine_name_is_valid(local)) {
2067 log_error("Machine name %s is not valid.", local);
2068 return -EINVAL;
2069 }
2070
2071 if (argc >= 3)
2072 path = argv[2];
2073 if (isempty(path) || streq(path, "-"))
2074 path = NULL;
2075
2076 if (path) {
2077 determine_compression_from_filename(path);
2078
2079 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
2080 if (fd < 0)
2081 return log_error_errno(errno, "Failed to open %s: %m", path);
2082 }
2083
2084 r = sd_bus_message_new_method_call(
2085 bus,
2086 &m,
2087 "org.freedesktop.import1",
2088 "/org/freedesktop/import1",
2089 "org.freedesktop.import1.Manager",
2090 "ExportRaw");
2091 if (r < 0)
2092 return bus_log_create_error(r);
2093
2094 r = sd_bus_message_append(
2095 m,
2096 "shs",
2097 local,
2098 fd >= 0 ? fd : STDOUT_FILENO,
2099 arg_format);
2100 if (r < 0)
2101 return bus_log_create_error(r);
2102
2103 return transfer_image_common(bus, m);
b6e676ce
LP
2104}
2105
3d7415f4 2106static int pull_tar(int argc, char *argv[], void *userdata) {
4afd3348 2107 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
3d7415f4
LP
2108 _cleanup_free_ char *l = NULL, *ll = NULL;
2109 const char *local, *remote;
2110 sd_bus *bus = userdata;
2111 int r;
2112
2113 assert(bus);
2114
2115 remote = argv[1];
2116 if (!http_url_is_valid(remote)) {
2117 log_error("URL '%s' is not valid.", remote);
2118 return -EINVAL;
2119 }
2120
2121 if (argc >= 3)
2122 local = argv[2];
2123 else {
2124 r = import_url_last_component(remote, &l);
2125 if (r < 0)
2126 return log_error_errno(r, "Failed to get final component of URL: %m");
2127
2128 local = l;
2129 }
2130
2131 if (isempty(local) || streq(local, "-"))
2132 local = NULL;
2133
2134 if (local) {
2135 r = tar_strip_suffixes(local, &ll);
2136 if (r < 0)
b6e676ce 2137 return log_oom();
3d7415f4
LP
2138
2139 local = ll;
2140
2141 if (!machine_name_is_valid(local)) {
2142 log_error("Local name %s is not a suitable machine name.", local);
2143 return -EINVAL;
2144 }
2145 }
2146
2147 r = sd_bus_message_new_method_call(
2148 bus,
2149 &m,
2150 "org.freedesktop.import1",
2151 "/org/freedesktop/import1",
2152 "org.freedesktop.import1.Manager",
2153 "PullTar");
2154 if (r < 0)
2155 return bus_log_create_error(r);
2156
2157 r = sd_bus_message_append(
2158 m,
2159 "sssb",
2160 remote,
2161 local,
6e18cc9f 2162 import_verify_to_string(arg_verify),
3d7415f4
LP
2163 arg_force);
2164 if (r < 0)
2165 return bus_log_create_error(r);
2166
587fec42 2167 return transfer_image_common(bus, m);
3d7415f4
LP
2168}
2169
2170static int pull_raw(int argc, char *argv[], void *userdata) {
4afd3348 2171 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
3d7415f4
LP
2172 _cleanup_free_ char *l = NULL, *ll = NULL;
2173 const char *local, *remote;
2174 sd_bus *bus = userdata;
2175 int r;
2176
2177 assert(bus);
2178
2179 remote = argv[1];
2180 if (!http_url_is_valid(remote)) {
2181 log_error("URL '%s' is not valid.", remote);
2182 return -EINVAL;
2183 }
2184
2185 if (argc >= 3)
2186 local = argv[2];
2187 else {
2188 r = import_url_last_component(remote, &l);
2189 if (r < 0)
2190 return log_error_errno(r, "Failed to get final component of URL: %m");
2191
2192 local = l;
2193 }
2194
2195 if (isempty(local) || streq(local, "-"))
2196 local = NULL;
2197
2198 if (local) {
2199 r = raw_strip_suffixes(local, &ll);
2200 if (r < 0)
b6e676ce 2201 return log_oom();
3d7415f4
LP
2202
2203 local = ll;
2204
2205 if (!machine_name_is_valid(local)) {
2206 log_error("Local name %s is not a suitable machine name.", local);
2207 return -EINVAL;
2208 }
2209 }
2210
2211 r = sd_bus_message_new_method_call(
2212 bus,
2213 &m,
2214 "org.freedesktop.import1",
2215 "/org/freedesktop/import1",
2216 "org.freedesktop.import1.Manager",
2217 "PullRaw");
2218 if (r < 0)
2219 return bus_log_create_error(r);
2220
2221 r = sd_bus_message_append(
2222 m,
2223 "sssb",
2224 remote,
2225 local,
6e18cc9f 2226 import_verify_to_string(arg_verify),
3d7415f4
LP
2227 arg_force);
2228 if (r < 0)
2229 return bus_log_create_error(r);
2230
587fec42 2231 return transfer_image_common(bus, m);
3d7415f4
LP
2232}
2233
3d7415f4
LP
2234typedef struct TransferInfo {
2235 uint32_t id;
2236 const char *type;
2237 const char *remote;
2238 const char *local;
7079cfef 2239 double progress;
3d7415f4
LP
2240} TransferInfo;
2241
2242static int compare_transfer_info(const void *a, const void *b) {
2243 const TransferInfo *x = a, *y = b;
2244
2245 return strcmp(x->local, y->local);
2246}
2247
2248static int list_transfers(int argc, char *argv[], void *userdata) {
2249 size_t max_type = strlen("TYPE"), max_local = strlen("LOCAL"), max_remote = strlen("REMOTE");
4afd3348
LP
2250 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2251 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d7415f4
LP
2252 _cleanup_free_ TransferInfo *transfers = NULL;
2253 size_t n_transfers = 0, n_allocated = 0, j;
2254 const char *type, *remote, *local, *object;
2255 sd_bus *bus = userdata;
2256 uint32_t id, max_id = 0;
7079cfef 2257 double progress;
3d7415f4
LP
2258 int r;
2259
ea4b98e6 2260 pager_open(arg_no_pager, false);
3d7415f4 2261
e138e7d7
ZJS
2262 r = sd_bus_call_method(bus,
2263 "org.freedesktop.import1",
2264 "/org/freedesktop/import1",
2265 "org.freedesktop.import1.Manager",
2266 "ListTransfers",
2267 &error,
2268 &reply,
2269 NULL);
3d7415f4
LP
2270 if (r < 0) {
2271 log_error("Could not get transfers: %s", bus_error_message(&error, -r));
2272 return r;
2273 }
2274
7079cfef 2275 r = sd_bus_message_enter_container(reply, 'a', "(usssdo)");
3d7415f4
LP
2276 if (r < 0)
2277 return bus_log_parse_error(r);
2278
7079cfef 2279 while ((r = sd_bus_message_read(reply, "(usssdo)", &id, &type, &remote, &local, &progress, &object)) > 0) {
3d7415f4
LP
2280 size_t l;
2281
2282 if (!GREEDY_REALLOC(transfers, n_allocated, n_transfers + 1))
2283 return log_oom();
2284
2285 transfers[n_transfers].id = id;
2286 transfers[n_transfers].type = type;
2287 transfers[n_transfers].remote = remote;
2288 transfers[n_transfers].local = local;
7079cfef 2289 transfers[n_transfers].progress = progress;
3d7415f4
LP
2290
2291 l = strlen(type);
2292 if (l > max_type)
2293 max_type = l;
2294
2295 l = strlen(remote);
2296 if (l > max_remote)
2297 max_remote = l;
2298
2299 l = strlen(local);
2300 if (l > max_local)
2301 max_local = l;
2302
2303 if (id > max_id)
2304 max_id = id;
2305
313cefa1 2306 n_transfers++;
3d7415f4
LP
2307 }
2308 if (r < 0)
2309 return bus_log_parse_error(r);
2310
2311 r = sd_bus_message_exit_container(reply);
2312 if (r < 0)
2313 return bus_log_parse_error(r);
2314
2315 qsort_safe(transfers, n_transfers, sizeof(TransferInfo), compare_transfer_info);
2316
2317 if (arg_legend)
7079cfef 2318 printf("%-*s %-*s %-*s %-*s %-*s\n",
3d7415f4 2319 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id)), "ID",
7079cfef 2320 (int) 7, "PERCENT",
3d7415f4
LP
2321 (int) max_type, "TYPE",
2322 (int) max_local, "LOCAL",
2323 (int) max_remote, "REMOTE");
2324
2325 for (j = 0; j < n_transfers; j++)
7079cfef 2326 printf("%*" PRIu32 " %*u%% %-*s %-*s %-*s\n",
3d7415f4 2327 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id)), transfers[j].id,
7079cfef 2328 (int) 6, (unsigned) (transfers[j].progress * 100),
3d7415f4
LP
2329 (int) max_type, transfers[j].type,
2330 (int) max_local, transfers[j].local,
2331 (int) max_remote, transfers[j].remote);
2332
2333 if (arg_legend)
2334 printf("\n%zu transfers listed.\n", n_transfers);
2335
2336 return 0;
2337}
2338
2339static int cancel_transfer(int argc, char *argv[], void *userdata) {
4afd3348 2340 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d7415f4
LP
2341 sd_bus *bus = userdata;
2342 int r, i;
2343
2344 assert(bus);
2345
2346 polkit_agent_open_if_enabled();
2347
2348 for (i = 1; i < argc; i++) {
2349 uint32_t id;
2350
2351 r = safe_atou32(argv[i], &id);
2352 if (r < 0)
2353 return log_error_errno(r, "Failed to parse transfer id: %s", argv[i]);
2354
2355 r = sd_bus_call_method(
2356 bus,
2357 "org.freedesktop.import1",
2358 "/org/freedesktop/import1",
2359 "org.freedesktop.import1.Manager",
2360 "CancelTransfer",
2361 &error,
2362 NULL,
2363 "u", id);
2364 if (r < 0) {
2365 log_error("Could not cancel transfer: %s", bus_error_message(&error, -r));
2366 return r;
2367 }
2368 }
2369
2370 return 0;
2371}
2372
d6ce17c7 2373static int set_limit(int argc, char *argv[], void *userdata) {
4afd3348 2374 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
d6ce17c7
LP
2375 sd_bus *bus = userdata;
2376 uint64_t limit;
2377 int r;
2378
7705a405 2379 if (STR_IN_SET(argv[argc-1], "-", "none", "infinity"))
d6ce17c7
LP
2380 limit = (uint64_t) -1;
2381 else {
59f448cf 2382 r = parse_size(argv[argc-1], 1024, &limit);
d6ce17c7
LP
2383 if (r < 0)
2384 return log_error("Failed to parse size: %s", argv[argc-1]);
d6ce17c7
LP
2385 }
2386
2387 if (argc > 2)
2388 /* With two arguments changes the quota limit of the
2389 * specified image */
2390 r = sd_bus_call_method(
2391 bus,
2392 "org.freedesktop.machine1",
2393 "/org/freedesktop/machine1",
2394 "org.freedesktop.machine1.Manager",
2395 "SetImageLimit",
2396 &error,
2397 NULL,
2398 "st", argv[1], limit);
2399 else
2400 /* With one argument changes the pool quota limit */
2401 r = sd_bus_call_method(
2402 bus,
2403 "org.freedesktop.machine1",
2404 "/org/freedesktop/machine1",
2405 "org.freedesktop.machine1.Manager",
2406 "SetPoolLimit",
2407 &error,
2408 NULL,
2409 "t", limit);
2410
2411 if (r < 0) {
2412 log_error("Could not set limit: %s", bus_error_message(&error, -r));
2413 return r;
2414 }
2415
2416 return 0;
2417}
2418
d94c2b06 2419static int clean_images(int argc, char *argv[], void *userdata) {
03c2b288 2420 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
d94c2b06
LP
2421 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2422 uint64_t usage, total = 0;
2423 char fb[FORMAT_BYTES_MAX];
2424 sd_bus *bus = userdata;
2425 const char *name;
2426 unsigned c = 0;
2427 int r;
2428
03c2b288 2429 r = sd_bus_message_new_method_call(
d94c2b06 2430 bus,
03c2b288 2431 &m,
d94c2b06
LP
2432 "org.freedesktop.machine1",
2433 "/org/freedesktop/machine1",
2434 "org.freedesktop.machine1.Manager",
03c2b288
LP
2435 "CleanPool");
2436 if (r < 0)
2437 return bus_log_create_error(r);
2438
2439 r = sd_bus_message_append(m, "s", arg_all ? "all" : "hidden");
2440 if (r < 0)
2441 return bus_log_create_error(r);
2442
2443 /* This is a slow operation, hence permit a longer time for completion. */
2444 r = sd_bus_call(bus, m, USEC_INFINITY, &error, &reply);
d94c2b06
LP
2445 if (r < 0)
2446 return log_error_errno(r, "Could not clean pool: %s", bus_error_message(&error, r));
2447
2448 r = sd_bus_message_enter_container(reply, 'a', "(st)");
2449 if (r < 0)
2450 return bus_log_parse_error(r);
2451
2452 while ((r = sd_bus_message_read(reply, "(st)", &name, &usage)) > 0) {
2453 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2454 name, format_bytes(fb, sizeof(fb), usage));
2455
2456 total += usage;
2457 c++;
2458 }
2459
2460 r = sd_bus_message_exit_container(reply);
2461 if (r < 0)
2462 return bus_log_parse_error(r);
2463
2464 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2465 c, format_bytes(fb, sizeof(fb), total));
2466
2467 return 0;
2468}
2469
56159e0d
LP
2470static int help(int argc, char *argv[], void *userdata) {
2471
1ee306e1 2472 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
f2cbe59e
LP
2473 "Send control commands to or query the virtual machine and container\n"
2474 "registration manager.\n\n"
2475 " -h --help Show this help\n"
2476 " --version Show package version\n"
2477 " --no-pager Do not pipe output into a pager\n"
2478 " --no-legend Do not show the headers and footers\n"
acf97e21 2479 " --no-ask-password Do not ask for system passwords\n"
f2cbe59e
LP
2480 " -H --host=[USER@]HOST Operate on remote host\n"
2481 " -M --machine=CONTAINER Operate on local container\n"
2482 " -p --property=NAME Show only properties by this name\n"
d8f52ed2 2483 " -q --quiet Suppress output\n"
f2cbe59e 2484 " -a --all Show all properties, including empty ones\n"
85500523 2485 " --value When showing properties, only print the value\n"
f2cbe59e
LP
2486 " -l --full Do not ellipsize output\n"
2487 " --kill-who=WHO Who to send signal to\n"
2488 " -s --signal=SIGNAL Which signal to send\n"
c454426c 2489 " --uid=USER Specify user ID to invoke shell as\n"
4d46e5db 2490 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
f2cbe59e 2491 " --read-only Create read-only bind mount\n"
8b0cc9a3
LP
2492 " --mkdir Create directory before bind mounting, if missing\n"
2493 " -n --lines=INTEGER Number of journal entries to show\n"
2494 " -o --output=STRING Change journal output mode (short,\n"
2495 " short-monotonic, verbose, export, json,\n"
3d7415f4 2496 " json-pretty, json-sse, cat)\n"
7f444afa
LP
2497 " --verify=MODE Verification mode for downloaded images (no,\n"
2498 " checksum, signature)\n"
b43d75c3 2499 " --force Download image even if already exists\n\n"
cd61c3bf 2500 "Machine Commands:\n"
f2cbe59e 2501 " list List running VMs and containers\n"
fefdc04b 2502 " status NAME... Show VM/container details\n"
91913f58 2503 " show [NAME...] Show properties of one or more VMs/containers\n"
ebd011d9 2504 " start NAME... Start container as a service\n"
91913f58
LP
2505 " login [NAME] Get a login prompt in a container or on the\n"
2506 " local host\n"
ef3100e9
LP
2507 " shell [[USER@]NAME [COMMAND...]]\n"
2508 " Invoke a shell (or other command) in a container\n"
2509 " or on the local host\n"
d8f52ed2
LP
2510 " enable NAME... Enable automatic container start at boot\n"
2511 " disable NAME... Disable automatic container start at boot\n"
f2cbe59e
LP
2512 " poweroff NAME... Power off one or more containers\n"
2513 " reboot NAME... Reboot one or more containers\n"
f2cbe59e 2514 " terminate NAME... Terminate one or more VMs/containers\n"
ebd93cb6 2515 " kill NAME... Send signal to processes of a VM/container\n"
f2cbe59e 2516 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
ebd93cb6
LP
2517 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2518 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
fefdc04b 2519 "Image Commands:\n"
56b921c3 2520 " list-images Show available container and VM images\n"
91913f58
LP
2521 " image-status [NAME...] Show image details\n"
2522 " show-image [NAME...] Show properties of image\n"
ebd93cb6
LP
2523 " clone NAME NAME Clone an image\n"
2524 " rename NAME NAME Rename an image\n"
2525 " read-only NAME [BOOL] Mark or unmark image read-only\n"
d6ce17c7 2526 " remove NAME... Remove an image\n"
5bda1f47
LP
2527 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2528 " clean Remove hidden (or all) images\n\n"
b5b38b41
LP
2529 "Image Transfer Commands:\n"
2530 " pull-tar URL [NAME] Download a TAR container image\n"
2531 " pull-raw URL [NAME] Download a RAW container or VM image\n"
587fec42
LP
2532 " import-tar FILE [NAME] Import a local TAR container image\n"
2533 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
6e9efa59
LP
2534 " export-tar NAME [FILE] Export a TAR container image locally\n"
2535 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
b5b38b41 2536 " list-transfers Show list of downloads in progress\n"
3d7415f4 2537 " cancel-transfer Cancel a download\n"
f7621db0 2538 , program_invocation_short_name);
56159e0d
LP
2539
2540 return 0;
1ee306e1
LP
2541}
2542
2543static int parse_argv(int argc, char *argv[]) {
2544
2545 enum {
2546 ARG_VERSION = 0x100,
2547 ARG_NO_PAGER,
e56056e9 2548 ARG_NO_LEGEND,
85500523 2549 ARG_VALUE,
1ee306e1 2550 ARG_KILL_WHO,
785890ac
LP
2551 ARG_READ_ONLY,
2552 ARG_MKDIR,
acf97e21 2553 ARG_NO_ASK_PASSWORD,
3d7415f4
LP
2554 ARG_VERIFY,
2555 ARG_FORCE,
587fec42 2556 ARG_FORMAT,
c454426c 2557 ARG_UID,
1ee306e1
LP
2558 };
2559
2560 static const struct option options[] = {
2561 { "help", no_argument, NULL, 'h' },
2562 { "version", no_argument, NULL, ARG_VERSION },
2563 { "property", required_argument, NULL, 'p' },
2564 { "all", no_argument, NULL, 'a' },
85500523 2565 { "value", no_argument, NULL, ARG_VALUE },
1ee306e1
LP
2566 { "full", no_argument, NULL, 'l' },
2567 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
e56056e9 2568 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1ee306e1
LP
2569 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
2570 { "signal", required_argument, NULL, 's' },
2571 { "host", required_argument, NULL, 'H' },
a7893c6b 2572 { "machine", required_argument, NULL, 'M' },
785890ac
LP
2573 { "read-only", no_argument, NULL, ARG_READ_ONLY },
2574 { "mkdir", no_argument, NULL, ARG_MKDIR },
d8f52ed2 2575 { "quiet", no_argument, NULL, 'q' },
8b0cc9a3
LP
2576 { "lines", required_argument, NULL, 'n' },
2577 { "output", required_argument, NULL, 'o' },
acf97e21 2578 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
3d7415f4
LP
2579 { "verify", required_argument, NULL, ARG_VERIFY },
2580 { "force", no_argument, NULL, ARG_FORCE },
587fec42 2581 { "format", required_argument, NULL, ARG_FORMAT },
c454426c 2582 { "uid", required_argument, NULL, ARG_UID },
4d46e5db 2583 { "setenv", required_argument, NULL, 'E' },
eb9da376 2584 {}
1ee306e1
LP
2585 };
2586
368d2643 2587 bool reorder = false;
768c1dec 2588 int c, r, shell = -1;
1ee306e1
LP
2589
2590 assert(argc >= 0);
2591 assert(argv);
2592
368d2643 2593 for (;;) {
768c1dec 2594 static const char option_string[] = "-hp:als:H:M:qn:o:";
368d2643 2595
0bf50960 2596 c = getopt_long(argc, argv, option_string + reorder, options, NULL);
768c1dec
LP
2597 if (c < 0)
2598 break;
2599
2600 switch (c) {
2601
2602 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2603 * non-option argument was discovered. */
2604
2605 assert(!reorder);
2606
368d2643
LP
2607 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2608 * for switches after the main verb. However, for "shell" we really don't want that, since we
768c1dec
LP
2609 * want that switches specified after the machine name are passed to the program to execute,
2610 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2611 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2612 * non-option parameter. If it's the verb "shell" we remember its position and continue
2613 * processing options. In this case, as soon as we hit the next non-option argument we found
2614 * the machine name, and stop further processing. If the first non-option argument is any other
2615 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2616 * normally. */
2617
2618 if (shell >= 0) {
2619 /* If we already found the "shell" verb on the command line, and now found the next
2620 * non-option argument, then this is the machine name and we should stop processing
2621 * further arguments. */
2622 optind --; /* don't process this argument, go one step back */
2623 goto done;
2624 }
2625 if (streq(optarg, "shell"))
2626 /* Remember the position of the "shell" verb, and continue processing normally. */
2627 shell = optind - 1;
2628 else {
2629 int saved_optind;
2630
2631 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2632 * processing normally. */
368d2643 2633 reorder = true;
768c1dec
LP
2634
2635 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2636 * at least once with a reset option index. Hence, let's reset the option index here,
2637 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2638 * likely already processed it), and the bump the option index so that we read the
2639 * intended argument again. */
2640 saved_optind = optind;
2641 optind = 0;
2642 (void) getopt_long(argc, argv, option_string + reorder, options, NULL);
2643 optind = saved_optind - 1; /* go one step back, process this argument again */
368d2643
LP
2644 }
2645
2646 break;
1ee306e1
LP
2647
2648 case 'h':
56159e0d 2649 return help(0, NULL, NULL);
1ee306e1
LP
2650
2651 case ARG_VERSION:
3f6fd1ba 2652 return version();
1ee306e1 2653
a7893c6b
LP
2654 case 'p':
2655 r = strv_extend(&arg_property, optarg);
2656 if (r < 0)
2657 return log_oom();
1ee306e1
LP
2658
2659 /* If the user asked for a particular
2660 * property, show it to him, even if it is
2661 * empty. */
2662 arg_all = true;
2663 break;
1ee306e1
LP
2664
2665 case 'a':
2666 arg_all = true;
2667 break;
2668
85500523
ZJS
2669 case ARG_VALUE:
2670 arg_value = true;
2671 break;
2672
1ee306e1
LP
2673 case 'l':
2674 arg_full = true;
2675 break;
2676
8b0cc9a3
LP
2677 case 'n':
2678 if (safe_atou(optarg, &arg_lines) < 0) {
2679 log_error("Failed to parse lines '%s'", optarg);
2680 return -EINVAL;
2681 }
2682 break;
2683
2684 case 'o':
2685 arg_output = output_mode_from_string(optarg);
2686 if (arg_output < 0) {
2687 log_error("Unknown output '%s'.", optarg);
2688 return -EINVAL;
2689 }
2690 break;
2691
1ee306e1
LP
2692 case ARG_NO_PAGER:
2693 arg_no_pager = true;
2694 break;
2695
e56056e9
TA
2696 case ARG_NO_LEGEND:
2697 arg_legend = false;
2698 break;
2699
1ee306e1
LP
2700 case ARG_KILL_WHO:
2701 arg_kill_who = optarg;
2702 break;
2703
2704 case 's':
2705 arg_signal = signal_from_string_try_harder(optarg);
2706 if (arg_signal < 0) {
2707 log_error("Failed to parse signal string %s.", optarg);
2708 return -EINVAL;
2709 }
2710 break;
2711
acf97e21
LP
2712 case ARG_NO_ASK_PASSWORD:
2713 arg_ask_password = false;
2714 break;
2715
1ee306e1 2716 case 'H':
d21ed1ea 2717 arg_transport = BUS_TRANSPORT_REMOTE;
a7893c6b
LP
2718 arg_host = optarg;
2719 break;
2720
2721 case 'M':
de33fc62 2722 arg_transport = BUS_TRANSPORT_MACHINE;
a7893c6b 2723 arg_host = optarg;
1ee306e1
LP
2724 break;
2725
785890ac
LP
2726 case ARG_READ_ONLY:
2727 arg_read_only = true;
2728 break;
2729
2730 case ARG_MKDIR:
2731 arg_mkdir = true;
2732 break;
2733
d8f52ed2
LP
2734 case 'q':
2735 arg_quiet = true;
2736 break;
2737
3d7415f4 2738 case ARG_VERIFY:
6e18cc9f
LP
2739 arg_verify = import_verify_from_string(optarg);
2740 if (arg_verify < 0) {
2741 log_error("Failed to parse --verify= setting: %s", optarg);
2742 return -EINVAL;
2743 }
3d7415f4
LP
2744 break;
2745
2746 case ARG_FORCE:
2747 arg_force = true;
2748 break;
2749
587fec42
LP
2750 case ARG_FORMAT:
2751 if (!STR_IN_SET(optarg, "uncompressed", "xz", "gzip", "bzip2")) {
2752 log_error("Unknown format: %s", optarg);
2753 return -EINVAL;
2754 }
2755
2756 arg_format = optarg;
2757 break;
2758
c454426c
LP
2759 case ARG_UID:
2760 arg_uid = optarg;
2761 break;
2762
4d46e5db 2763 case 'E':
c454426c
LP
2764 if (!env_assignment_is_valid(optarg)) {
2765 log_error("Environment assignment invalid: %s", optarg);
2766 return -EINVAL;
2767 }
2768
2769 r = strv_extend(&arg_setenv, optarg);
2770 if (r < 0)
2771 return log_oom();
2772 break;
2773
1ee306e1
LP
2774 case '?':
2775 return -EINVAL;
2776
2777 default:
eb9da376 2778 assert_not_reached("Unhandled option");
1ee306e1 2779 }
368d2643 2780 }
1ee306e1 2781
768c1dec
LP
2782done:
2783 if (shell >= 0) {
2784 char *t;
2785 int i;
2786
2787 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2788 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2789
2790 optind -= 1; /* place the option index where the "shell" verb will be placed */
2791
2792 t = argv[shell];
2793 for (i = shell; i < optind; i++)
2794 argv[i] = argv[i+1];
2795 argv[optind] = t;
2796 }
2797
1ee306e1
LP
2798 return 1;
2799}
2800
56159e0d
LP
2801static int machinectl_main(int argc, char *argv[], sd_bus *bus) {
2802
2803 static const Verb verbs[] = {
3d7415f4
LP
2804 { "help", VERB_ANY, VERB_ANY, 0, help },
2805 { "list", VERB_ANY, 1, VERB_DEFAULT, list_machines },
2806 { "list-images", VERB_ANY, 1, 0, list_images },
2807 { "status", 2, VERB_ANY, 0, show_machine },
160e3793 2808 { "image-status", VERB_ANY, VERB_ANY, 0, show_image },
3d7415f4
LP
2809 { "show", VERB_ANY, VERB_ANY, 0, show_machine },
2810 { "show-image", VERB_ANY, VERB_ANY, 0, show_image },
2811 { "terminate", 2, VERB_ANY, 0, terminate_machine },
2812 { "reboot", 2, VERB_ANY, 0, reboot_machine },
2813 { "poweroff", 2, VERB_ANY, 0, poweroff_machine },
b2bb19bb 2814 { "stop", 2, VERB_ANY, 0, poweroff_machine }, /* Convenience alias */
3d7415f4 2815 { "kill", 2, VERB_ANY, 0, kill_machine },
91913f58
LP
2816 { "login", VERB_ANY, 2, 0, login_machine },
2817 { "shell", VERB_ANY, VERB_ANY, 0, shell_machine },
3d7415f4
LP
2818 { "bind", 3, 4, 0, bind_mount },
2819 { "copy-to", 3, 4, 0, copy_files },
2820 { "copy-from", 3, 4, 0, copy_files },
2821 { "remove", 2, VERB_ANY, 0, remove_image },
2822 { "rename", 3, 3, 0, rename_image },
2823 { "clone", 3, 3, 0, clone_image },
2824 { "read-only", 2, 3, 0, read_only_image },
2825 { "start", 2, VERB_ANY, 0, start_machine },
2826 { "enable", 2, VERB_ANY, 0, enable_machine },
2827 { "disable", 2, VERB_ANY, 0, enable_machine },
b6e676ce
LP
2828 { "import-tar", 2, 3, 0, import_tar },
2829 { "import-raw", 2, 3, 0, import_raw },
587fec42
LP
2830 { "export-tar", 2, 3, 0, export_tar },
2831 { "export-raw", 2, 3, 0, export_raw },
3d7415f4
LP
2832 { "pull-tar", 2, 3, 0, pull_tar },
2833 { "pull-raw", 2, 3, 0, pull_raw },
3d7415f4
LP
2834 { "list-transfers", VERB_ANY, 1, 0, list_transfers },
2835 { "cancel-transfer", 2, VERB_ANY, 0, cancel_transfer },
d6ce17c7 2836 { "set-limit", 2, 3, 0, set_limit },
d94c2b06 2837 { "clean", VERB_ANY, 1, 0, clean_images },
56159e0d 2838 {}
1ee306e1
LP
2839 };
2840
56159e0d 2841 return dispatch_verb(argc, argv, verbs, bus);
1ee306e1
LP
2842}
2843
2844int main(int argc, char*argv[]) {
cf647b69 2845 sd_bus *bus = NULL;
84f6181c 2846 int r;
1ee306e1
LP
2847
2848 setlocale(LC_ALL, "");
2849 log_parse_environment();
2850 log_open();
2851
2852 r = parse_argv(argc, argv);
84f6181c 2853 if (r <= 0)
1ee306e1 2854 goto finish;
1ee306e1 2855
266f3e26 2856 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
a1da8583 2857 if (r < 0) {
da927ba9 2858 log_error_errno(r, "Failed to create bus connection: %m");
a1da8583
TG
2859 goto finish;
2860 }
1ee306e1 2861
2723b3b5
LP
2862 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
2863
56159e0d 2864 r = machinectl_main(argc, argv, bus);
1ee306e1
LP
2865
2866finish:
cf647b69 2867 sd_bus_flush_close_unref(bus);
1ee306e1 2868 pager_close();
acf97e21 2869 polkit_agent_close();
1ee306e1 2870
84f6181c 2871 strv_free(arg_property);
c454426c 2872 strv_free(arg_setenv);
84f6181c
LP
2873
2874 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1ee306e1 2875}