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