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