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