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