1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include <netinet/in.h>
11 #include <sys/mount.h>
12 #include <sys/socket.h>
17 #include "alloc-util.h"
18 #include "bus-common-errors.h"
19 #include "bus-error.h"
20 #include "bus-unit-util.h"
22 #include "cgroup-show.h"
23 #include "cgroup-util.h"
27 #include "format-table.h"
28 #include "hostname-util.h"
29 #include "import-util.h"
30 #include "locale-util.h"
32 #include "logs-show.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "process-util.h"
41 #include "signal-util.h"
42 #include "spawn-polkit-agent.h"
43 #include "stdio-util.h"
44 #include "string-table.h"
46 #include "terminal-util.h"
47 #include "unit-name.h"
52 #define ALL_IP_ADDRESSES -1
54 static char **arg_property
= NULL
;
55 static bool arg_all
= false;
56 static bool arg_value
= false;
57 static bool arg_full
= false;
58 static bool arg_no_pager
= false;
59 static bool arg_legend
= true;
60 static const char *arg_kill_who
= NULL
;
61 static int arg_signal
= SIGTERM
;
62 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
63 static char *arg_host
= NULL
;
64 static bool arg_read_only
= false;
65 static bool arg_mkdir
= false;
66 static bool arg_quiet
= false;
67 static bool arg_ask_password
= true;
68 static unsigned arg_lines
= 10;
69 static OutputMode arg_output
= OUTPUT_SHORT
;
70 static bool arg_force
= false;
71 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
72 static const char* arg_format
= NULL
;
73 static const char *arg_uid
= NULL
;
74 static char **arg_setenv
= NULL
;
75 static int arg_addrs
= 1;
77 static OutputFlags
get_output_flags(void) {
79 arg_all
* OUTPUT_SHOW_ALL
|
80 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
81 colors_enabled() * OUTPUT_COLOR
|
82 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
85 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
86 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
87 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
88 const char *k
, *v
, *iter
, **query_res
= NULL
;
89 size_t count
= 0, awaited_args
= 0;
97 NULSTR_FOREACH(iter
, query
)
99 query_res
= newa0(const char *, awaited_args
);
101 r
= sd_bus_call_method(
103 "org.freedesktop.machine1",
104 "/org/freedesktop/machine1",
105 "org.freedesktop.machine1.Manager",
110 return log_debug_errno(r
, "Failed to call '%s()': %s", method
, bus_error_message(&error
, r
));
112 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
114 return bus_log_parse_error(r
);
116 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
118 NULSTR_FOREACH(iter
, query
) {
119 if (streq(k
, iter
)) {
120 query_res
[count
] = v
;
127 return bus_log_parse_error(r
);
129 r
= sd_bus_message_exit_container(reply
);
131 return bus_log_parse_error(r
);
134 for (count
= 0; count
< awaited_args
; count
++) {
137 out
= va_arg(ap
, char **);
139 if (query_res
[count
]) {
140 val
= strdup(query_res
[count
]);
153 static int call_get_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
, char **ret
) {
155 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
156 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
157 _cleanup_free_
char *addresses
= NULL
;
158 bool truncate
= false;
167 r
= sd_bus_call_method(bus
,
168 "org.freedesktop.machine1",
169 "/org/freedesktop/machine1",
170 "org.freedesktop.machine1.Manager",
171 "GetMachineAddresses",
176 return log_debug_errno(r
, "Could not get addresses: %s", bus_error_message(&error
, r
));
178 addresses
= strdup(prefix
);
183 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
185 return bus_log_parse_error(r
);
187 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
191 char buf_ifi
[DECIMAL_STR_MAX(int) + 2], buffer
[MAX(INET6_ADDRSTRLEN
, INET_ADDRSTRLEN
)];
193 r
= sd_bus_message_read(reply
, "i", &family
);
195 return bus_log_parse_error(r
);
197 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
199 return bus_log_parse_error(r
);
202 if (family
== AF_INET6
&& ifi
> 0)
203 xsprintf(buf_ifi
, "%%%i", ifi
);
207 if (!strextend(&addresses
, prefix
, inet_ntop(family
, a
, buffer
, sizeof(buffer
)), buf_ifi
, NULL
))
212 r
= sd_bus_message_exit_container(reply
);
214 return bus_log_parse_error(r
);
224 return bus_log_parse_error(r
);
226 r
= sd_bus_message_exit_container(reply
);
228 return bus_log_parse_error(r
);
232 if (!strextend(&addresses
, special_glyph(ELLIPSIS
), NULL
))
237 *ret
= TAKE_PTR(addresses
);
241 static int show_table(Table
*table
, const char *word
) {
247 if (table_get_rows(table
) > 1) {
248 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
250 return log_error_errno(r
, "Failed to sort table: %m");
252 table_set_header(table
, arg_legend
);
254 r
= table_print(table
, NULL
);
256 return log_error_errno(r
, "Failed to show table: %m");
260 if (table_get_rows(table
) > 1)
261 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
263 printf("No %s.\n", word
);
269 static int list_machines(int argc
, char *argv
[], void *userdata
) {
271 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
272 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
273 _cleanup_(table_unrefp
) Table
*table
= NULL
;
274 sd_bus
*bus
= userdata
;
279 (void) pager_open(arg_no_pager
, false);
281 r
= sd_bus_call_method(bus
,
282 "org.freedesktop.machine1",
283 "/org/freedesktop/machine1",
284 "org.freedesktop.machine1.Manager",
290 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
292 table
= table_new("MACHINE", "CLASS", "SERVICE", "OS", "VERSION", "ADDRESSES");
296 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
298 return bus_log_parse_error(r
);
301 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
302 const char *name
, *class, *service
;
304 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
306 return bus_log_parse_error(r
);
310 if (name
[0] == '.' && !arg_all
)
313 (void) call_get_os_release(
315 "GetMachineOSRelease",
322 (void) call_get_addresses(
331 r
= table_add_many(table
,
334 TABLE_STRING
, empty_to_dash(service
),
335 TABLE_STRING
, empty_to_dash(os
),
336 TABLE_STRING
, empty_to_dash(version_id
),
337 TABLE_STRING
, empty_to_dash(addresses
));
339 return log_error_errno(r
, "Failed to add table row: %m");
342 r
= sd_bus_message_exit_container(reply
);
344 return bus_log_parse_error(r
);
346 return show_table(table
, "machines");
349 static int list_images(int argc
, char *argv
[], void *userdata
) {
351 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
352 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
353 _cleanup_(table_unrefp
) Table
*table
= NULL
;
354 sd_bus
*bus
= userdata
;
359 (void) pager_open(arg_no_pager
, false);
361 r
= sd_bus_call_method(bus
,
362 "org.freedesktop.machine1",
363 "/org/freedesktop/machine1",
364 "org.freedesktop.machine1.Manager",
370 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
372 table
= table_new("NAME", "TYPE", "RO", "USAGE", "CREATED", "MODIFIED");
376 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
378 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
380 return bus_log_parse_error(r
);
383 uint64_t crtime
, mtime
, size
;
384 const char *name
, *type
;
389 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
391 return bus_log_parse_error(r
);
395 if (name
[0] == '.' && !arg_all
)
398 r
= table_add_many(table
,
402 return log_error_errno(r
, "Failed to add table row: %m");
405 r
= table_add_cell(table
, &cell
, TABLE_BOOLEAN
, &ro_bool
);
407 return log_error_errno(r
, "Failed to add table cell: %m");
410 r
= table_set_color(table
, cell
, ansi_highlight_red());
412 return log_error_errno(r
, "Failed to set table cell color: %m");
415 r
= table_add_many(table
,
417 TABLE_TIMESTAMP
, crtime
,
418 TABLE_TIMESTAMP
, mtime
);
420 return log_error_errno(r
, "Failed to add table row: %m");
423 r
= sd_bus_message_exit_container(reply
);
425 return bus_log_parse_error(r
);
427 return show_table(table
, "images");
430 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
431 _cleanup_free_
char *cgroup
= NULL
;
432 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
439 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
452 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
455 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
458 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
460 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
463 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
465 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
470 static int print_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
) {
471 _cleanup_free_
char *s
= NULL
;
474 r
= call_get_addresses(bus
, name
, ifi
, prefix
, prefix2
, n_addr
, &s
);
484 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
485 _cleanup_free_
char *pretty
= NULL
;
492 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
497 printf("%s%s\n", prefix
, pretty
);
502 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
503 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
504 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
511 r
= sd_bus_call_method(bus
,
512 "org.freedesktop.machine1",
513 "/org/freedesktop/machine1",
514 "org.freedesktop.machine1.Manager",
515 "GetMachineUIDShift",
520 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
522 r
= sd_bus_message_read(reply
, "u", &shift
);
526 if (shift
== 0) /* Don't show trivial mappings */
529 printf(" UID Shift: %" PRIu32
"\n", shift
);
533 typedef struct MachineStatusInfo
{
539 const char *root_directory
;
541 struct dual_timestamp timestamp
;
546 static void machine_status_info_clear(MachineStatusInfo
*info
) {
553 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
554 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
555 char since2
[FORMAT_TIMESTAMP_MAX
];
562 fputs(strna(i
->name
), stdout
);
564 if (!sd_id128_is_null(i
->id
))
565 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
569 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->timestamp
.realtime
);
570 s2
= format_timestamp(since2
, sizeof(since2
), i
->timestamp
.realtime
);
573 printf("\t Since: %s; %s\n", s2
, s1
);
575 printf("\t Since: %s\n", s2
);
578 _cleanup_free_
char *t
= NULL
;
580 printf("\t Leader: %u", (unsigned) i
->leader
);
582 get_process_comm(i
->leader
, &t
);
590 printf("\t Service: %s", i
->service
);
593 printf("; class %s", i
->class);
597 printf("\t Class: %s\n", i
->class);
599 if (i
->root_directory
)
600 printf("\t Root: %s\n", i
->root_directory
);
602 if (i
->n_netif
> 0) {
605 fputs("\t Iface:", stdout
);
607 for (c
= 0; c
< i
->n_netif
; c
++) {
608 char name
[IF_NAMESIZE
+1] = "";
610 if (if_indextoname(i
->netif
[c
], name
)) {
619 printf(" %i", i
->netif
[c
]);
625 if (print_addresses(bus
, i
->name
, ifi
,
628 ALL_IP_ADDRESSES
) > 0)
631 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
633 print_uid_shift(bus
, i
->name
);
636 printf("\t Unit: %s\n", i
->unit
);
637 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
639 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
641 show_journal_by_unit(
646 i
->timestamp
.monotonic
,
649 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
650 SD_JOURNAL_LOCAL_ONLY
,
656 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
657 MachineStatusInfo
*i
= userdata
;
662 assert_cc(sizeof(int32_t) == sizeof(int));
663 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
669 i
->n_netif
= l
/ sizeof(int32_t);
670 i
->netif
= memdup(v
, l
);
677 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
679 static const struct bus_properties_map map
[] = {
680 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
681 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
682 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
683 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
684 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
685 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
686 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
687 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
688 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
689 { "NetworkInterfaces", "ai", map_netif
, 0 },
693 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
694 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
695 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
703 r
= bus_map_all_properties(bus
,
704 "org.freedesktop.machine1",
712 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
718 print_machine_status_info(bus
, &info
);
723 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
735 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_value
, arg_all
, NULL
);
737 log_error_errno(r
, "Could not get properties: %m");
742 static int show_machine(int argc
, char *argv
[], void *userdata
) {
744 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
745 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
746 bool properties
, new_line
= false;
747 sd_bus
*bus
= userdata
;
752 properties
= !strstr(argv
[0], "status");
754 (void) pager_open(arg_no_pager
, false);
756 if (properties
&& argc
<= 1) {
758 /* If no argument is specified, inspect the manager
760 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
765 for (i
= 1; i
< argc
; i
++) {
766 const char *path
= NULL
;
768 r
= sd_bus_call_method(bus
,
769 "org.freedesktop.machine1",
770 "/org/freedesktop/machine1",
771 "org.freedesktop.machine1.Manager",
777 log_error("Could not get path to machine: %s", bus_error_message(&error
, -r
));
781 r
= sd_bus_message_read(reply
, "o", &path
);
783 return bus_log_parse_error(r
);
786 r
= show_machine_properties(bus
, path
, &new_line
);
788 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
794 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
795 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
799 r
= sd_bus_call_method(
801 "org.freedesktop.machine1",
802 "/org/freedesktop/machine1",
803 "org.freedesktop.machine1.Manager",
805 NULL
, &reply
, "s", name
);
809 r
= sd_bus_message_read(reply
, "s", &hn
);
814 printf("\tHostname: %s\n", hn
);
819 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
820 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
821 sd_id128_t id
= SD_ID128_NULL
;
826 r
= sd_bus_call_method(
828 "org.freedesktop.machine1",
829 "/org/freedesktop/machine1",
830 "org.freedesktop.machine1.Manager",
832 NULL
, &reply
, "s", name
);
836 r
= sd_bus_message_read_array(reply
, 'y', &p
, &size
);
840 if (size
== sizeof(sd_id128_t
))
841 memcpy(&id
, p
, size
);
843 if (!sd_id128_is_null(id
))
844 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
849 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
850 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
853 r
= sd_bus_call_method(
855 "org.freedesktop.machine1",
856 "/org/freedesktop/machine1",
857 "org.freedesktop.machine1.Manager",
858 "GetImageMachineInfo",
859 NULL
, &reply
, "s", name
);
863 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
870 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
876 if (streq(p
, "DEPLOYMENT"))
877 printf(" Deployment: %s\n", q
);
880 r
= sd_bus_message_exit_container(reply
);
887 typedef struct ImageStatusInfo
{
896 uint64_t usage_exclusive
;
897 uint64_t limit_exclusive
;
900 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
901 char ts_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
902 char ts_absolute
[FORMAT_TIMESTAMP_MAX
];
903 char bs
[FORMAT_BYTES_MAX
];
904 char bs_exclusive
[FORMAT_BYTES_MAX
];
905 const char *s1
, *s2
, *s3
, *s4
;
911 fputs(i
->name
, stdout
);
916 printf("\t Type: %s\n", i
->type
);
919 printf("\t Path: %s\n", i
->path
);
921 (void) print_image_hostname(bus
, i
->name
);
922 (void) print_image_machine_id(bus
, i
->name
);
923 (void) print_image_machine_info(bus
, i
->name
);
925 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
927 printf("\t RO: %s%s%s\n",
928 i
->read_only
? ansi_highlight_red() : "",
929 i
->read_only
? "read-only" : "writable",
930 i
->read_only
? ansi_normal() : "");
932 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->crtime
);
933 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->crtime
);
935 printf("\t Created: %s; %s\n", s2
, s1
);
937 printf("\t Created: %s\n", s2
);
939 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->mtime
);
940 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->mtime
);
942 printf("\tModified: %s; %s\n", s2
, s1
);
944 printf("\tModified: %s\n", s2
);
946 s3
= format_bytes(bs
, sizeof(bs
), i
->usage
);
947 s4
= i
->usage_exclusive
!= i
->usage
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->usage_exclusive
) : NULL
;
949 printf("\t Usage: %s (exclusive: %s)\n", s3
, s4
);
951 printf("\t Usage: %s\n", s3
);
953 s3
= format_bytes(bs
, sizeof(bs
), i
->limit
);
954 s4
= i
->limit_exclusive
!= i
->limit
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->limit_exclusive
) : NULL
;
956 printf("\t Limit: %s (exclusive: %s)\n", s3
, s4
);
958 printf("\t Limit: %s\n", s3
);
961 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
963 static const struct bus_properties_map map
[] = {
964 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
965 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
966 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
967 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
968 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
969 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
970 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
971 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
972 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
973 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
977 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
978 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
979 ImageStatusInfo info
= {};
986 r
= bus_map_all_properties(bus
,
987 "org.freedesktop.machine1",
990 BUS_MAP_BOOLEAN_AS_BOOL
,
995 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1001 print_image_status_info(bus
, &info
);
1006 typedef struct PoolStatusInfo
{
1012 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
1013 char bs
[FORMAT_BYTES_MAX
], *s
;
1016 printf("\t Path: %s\n", i
->path
);
1018 s
= format_bytes(bs
, sizeof(bs
), i
->usage
);
1020 printf("\t Usage: %s\n", s
);
1022 s
= format_bytes(bs
, sizeof(bs
), i
->limit
);
1024 printf("\t Limit: %s\n", s
);
1027 static int show_pool_info(sd_bus
*bus
) {
1029 static const struct bus_properties_map map
[] = {
1030 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
1031 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
1032 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
1036 PoolStatusInfo info
= {
1037 .usage
= (uint64_t) -1,
1038 .limit
= (uint64_t) -1,
1041 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1042 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1047 r
= bus_map_all_properties(bus
,
1048 "org.freedesktop.machine1",
1049 "/org/freedesktop/machine1",
1056 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1058 print_pool_status_info(bus
, &info
);
1063 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
1075 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_value
, arg_all
, NULL
);
1077 log_error_errno(r
, "Could not get properties: %m");
1082 static int show_image(int argc
, char *argv
[], void *userdata
) {
1084 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1085 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1086 bool properties
, new_line
= false;
1087 sd_bus
*bus
= userdata
;
1092 properties
= !strstr(argv
[0], "status");
1094 (void) pager_open(arg_no_pager
, false);
1098 /* If no argument is specified, inspect the manager
1102 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1104 r
= show_pool_info(bus
);
1109 for (i
= 1; i
< argc
; i
++) {
1110 const char *path
= NULL
;
1112 r
= sd_bus_call_method(
1114 "org.freedesktop.machine1",
1115 "/org/freedesktop/machine1",
1116 "org.freedesktop.machine1.Manager",
1122 log_error("Could not get path to image: %s", bus_error_message(&error
, -r
));
1126 r
= sd_bus_message_read(reply
, "o", &path
);
1128 return bus_log_parse_error(r
);
1131 r
= show_image_properties(bus
, path
, &new_line
);
1133 r
= show_image_info(bus
, path
, &new_line
);
1139 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1140 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1141 sd_bus
*bus
= userdata
;
1146 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1149 arg_kill_who
= "all";
1151 for (i
= 1; i
< argc
; i
++) {
1152 r
= sd_bus_call_method(
1154 "org.freedesktop.machine1",
1155 "/org/freedesktop/machine1",
1156 "org.freedesktop.machine1.Manager",
1160 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1162 log_error("Could not kill machine: %s", bus_error_message(&error
, -r
));
1170 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1171 arg_kill_who
= "leader";
1172 arg_signal
= SIGINT
; /* sysvinit + systemd */
1174 return kill_machine(argc
, argv
, userdata
);
1177 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1178 arg_kill_who
= "leader";
1179 arg_signal
= SIGRTMIN
+4; /* only systemd */
1181 return kill_machine(argc
, argv
, userdata
);
1184 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1185 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1186 sd_bus
*bus
= userdata
;
1191 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1193 for (i
= 1; i
< argc
; i
++) {
1194 r
= sd_bus_call_method(
1196 "org.freedesktop.machine1",
1197 "/org/freedesktop/machine1",
1198 "org.freedesktop.machine1.Manager",
1204 log_error("Could not terminate machine: %s", bus_error_message(&error
, -r
));
1212 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1213 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1214 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1215 _cleanup_free_
char *abs_host_path
= NULL
;
1216 char *dest
, *host_path
, *container_path
;
1217 sd_bus
*bus
= userdata
;
1223 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1225 copy_from
= streq(argv
[0], "copy-from");
1226 dest
= argv
[3] ?: argv
[2];
1227 host_path
= copy_from
? dest
: argv
[2];
1228 container_path
= copy_from
? argv
[2] : dest
;
1230 if (!path_is_absolute(host_path
)) {
1231 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1233 return log_error_errno(r
, "Failed to make path absolute: %m");
1235 host_path
= abs_host_path
;
1238 r
= sd_bus_message_new_method_call(
1241 "org.freedesktop.machine1",
1242 "/org/freedesktop/machine1",
1243 "org.freedesktop.machine1.Manager",
1244 copy_from
? "CopyFromMachine" : "CopyToMachine");
1246 return bus_log_create_error(r
);
1248 r
= sd_bus_message_append(
1252 copy_from
? container_path
: host_path
,
1253 copy_from
? host_path
: container_path
);
1255 return bus_log_create_error(r
);
1257 /* This is a slow operation, hence turn off any method call timeouts */
1258 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1260 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1265 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1266 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1267 sd_bus
*bus
= userdata
;
1272 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1274 r
= sd_bus_call_method(
1276 "org.freedesktop.machine1",
1277 "/org/freedesktop/machine1",
1278 "org.freedesktop.machine1.Manager",
1289 log_error("Failed to bind mount: %s", bus_error_message(&error
, -r
));
1296 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1297 PTYForward
** forward
= (PTYForward
**) userdata
;
1304 /* If the forwarder is already initialized, tell it to
1305 * exit on the next vhangup(), so that we still flush
1306 * out what might be queued and exit then. */
1308 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1312 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1315 /* On error, or when the forwarder is not initialized yet, quit immediately */
1316 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1320 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1326 assert(master
>= 0);
1329 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1332 if (streq(name
, ".host"))
1333 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1335 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1338 sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1339 sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1341 r
= pty_forward_new(event
, master
, flags
, forward
);
1343 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1345 r
= sd_event_loop(event
);
1347 return log_error_errno(r
, "Failed to run event loop: %m");
1349 pty_forward_get_last_char(*forward
, &last_char
);
1352 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1353 pty_forward_get_ignore_vhangup(*forward
) == 0;
1355 *forward
= pty_forward_free(*forward
);
1357 if (last_char
!= '\n')
1358 fputc('\n', stdout
);
1362 log_info("Machine %s terminated.", name
);
1363 else if (streq(name
, ".host"))
1364 log_info("Connection to the local host terminated.");
1366 log_info("Connection to machine %s terminated.", name
);
1369 sd_event_get_exit_code(event
, &ret
);
1373 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1375 * Whatever is specified in the spec takes priority over global arguments.
1378 const char *_machine
= NULL
;
1383 at
= strchr(spec
, '@');
1386 /* Do the same as ssh and refuse "@host". */
1390 _uid
= strndup(spec
, at
- spec
);
1397 if (arg_uid
&& !_uid
) {
1398 _uid
= strdup(arg_uid
);
1404 *machine
= isempty(_machine
) ? ".host" : _machine
;
1408 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1409 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1410 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1411 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1412 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1413 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1415 sd_bus
*bus
= userdata
;
1416 const char *match
, *machine
;
1420 if (!strv_isempty(arg_setenv
) || arg_uid
) {
1421 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1425 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1426 log_error("Login only supported on local machines.");
1430 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1432 r
= sd_event_default(&event
);
1434 return log_error_errno(r
, "Failed to get event loop: %m");
1436 r
= sd_bus_attach_event(bus
, event
, 0);
1438 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1440 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1442 match
= strjoina("type='signal',"
1443 "sender='org.freedesktop.machine1',"
1444 "path='/org/freedesktop/machine1',",
1445 "interface='org.freedesktop.machine1.Manager',"
1446 "member='MachineRemoved',"
1447 "arg0='", machine
, "'");
1449 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1451 return log_error_errno(r
, "Failed to request machine removal match: %m");
1453 r
= sd_bus_call_method(
1455 "org.freedesktop.machine1",
1456 "/org/freedesktop/machine1",
1457 "org.freedesktop.machine1.Manager",
1463 log_error("Failed to get login PTY: %s", bus_error_message(&error
, -r
));
1467 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1469 return bus_log_parse_error(r
);
1471 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1474 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1475 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1476 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1477 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1478 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1479 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1481 sd_bus
*bus
= userdata
;
1482 const char *match
, *machine
, *path
;
1483 _cleanup_free_
char *uid
= NULL
;
1487 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1488 log_error("Shell only supported on local machines.");
1492 /* Pass $TERM to shell session, if not explicitly specified. */
1493 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1496 t
= strv_find_prefix(environ
, "TERM=");
1498 if (strv_extend(&arg_setenv
, t
) < 0)
1503 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1505 r
= sd_event_default(&event
);
1507 return log_error_errno(r
, "Failed to get event loop: %m");
1509 r
= sd_bus_attach_event(bus
, event
, 0);
1511 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1513 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1515 return log_error_errno(r
, "Failed to parse machine specification: %m");
1517 match
= strjoina("type='signal',"
1518 "sender='org.freedesktop.machine1',"
1519 "path='/org/freedesktop/machine1',",
1520 "interface='org.freedesktop.machine1.Manager',"
1521 "member='MachineRemoved',"
1522 "arg0='", machine
, "'");
1524 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1526 return log_error_errno(r
, "Failed to request machine removal match: %m");
1528 r
= sd_bus_message_new_method_call(
1531 "org.freedesktop.machine1",
1532 "/org/freedesktop/machine1",
1533 "org.freedesktop.machine1.Manager",
1534 "OpenMachineShell");
1536 return bus_log_create_error(r
);
1538 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1540 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1542 return bus_log_create_error(r
);
1544 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1546 return bus_log_create_error(r
);
1548 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1550 return bus_log_create_error(r
);
1552 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1554 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, -r
));
1556 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1558 return bus_log_parse_error(r
);
1560 return process_forward(event
, &forward
, master
, 0, machine
);
1563 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1564 sd_bus
*bus
= userdata
;
1569 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1571 for (i
= 1; i
< argc
; i
++) {
1572 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1573 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1575 r
= sd_bus_message_new_method_call(
1578 "org.freedesktop.machine1",
1579 "/org/freedesktop/machine1",
1580 "org.freedesktop.machine1.Manager",
1583 return bus_log_create_error(r
);
1585 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1587 return bus_log_create_error(r
);
1589 /* This is a slow operation, hence turn off any method call timeouts */
1590 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1592 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1598 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1599 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1600 sd_bus
*bus
= userdata
;
1605 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1607 r
= sd_bus_call_method(
1609 "org.freedesktop.machine1",
1610 "/org/freedesktop/machine1",
1611 "org.freedesktop.machine1.Manager",
1615 "ss", argv
[1], argv
[2]);
1617 log_error("Could not rename image: %s", bus_error_message(&error
, -r
));
1624 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1625 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1626 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1627 sd_bus
*bus
= userdata
;
1632 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1634 r
= sd_bus_message_new_method_call(
1637 "org.freedesktop.machine1",
1638 "/org/freedesktop/machine1",
1639 "org.freedesktop.machine1.Manager",
1642 return bus_log_create_error(r
);
1644 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1646 return bus_log_create_error(r
);
1648 /* This is a slow operation, hence turn off any method call timeouts */
1649 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1651 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1656 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1657 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1658 sd_bus
*bus
= userdata
;
1664 b
= parse_boolean(argv
[2]);
1666 log_error("Failed to parse boolean argument: %s", argv
[2]);
1671 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1673 r
= sd_bus_call_method(
1675 "org.freedesktop.machine1",
1676 "/org/freedesktop/machine1",
1677 "org.freedesktop.machine1.Manager",
1678 "MarkImageReadOnly",
1683 log_error("Could not mark image read-only: %s", bus_error_message(&error
, -r
));
1690 static int image_exists(sd_bus
*bus
, const char *name
) {
1691 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1697 r
= sd_bus_call_method(
1699 "org.freedesktop.machine1",
1700 "/org/freedesktop/machine1",
1701 "org.freedesktop.machine1.Manager",
1707 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1710 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, -r
));
1716 static int make_service_name(const char *name
, char **ret
) {
1722 if (!machine_name_is_valid(name
)) {
1723 log_error("Invalid machine name %s.", name
);
1727 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1729 return log_error_errno(r
, "Failed to build unit name: %m");
1734 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1735 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1736 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1737 sd_bus
*bus
= userdata
;
1742 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1744 r
= bus_wait_for_jobs_new(bus
, &w
);
1748 for (i
= 1; i
< argc
; i
++) {
1749 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1750 _cleanup_free_
char *unit
= NULL
;
1753 r
= make_service_name(argv
[i
], &unit
);
1757 r
= image_exists(bus
, argv
[i
]);
1761 log_error("Machine image '%s' does not exist.", argv
[1]);
1765 r
= sd_bus_call_method(
1767 "org.freedesktop.systemd1",
1768 "/org/freedesktop/systemd1",
1769 "org.freedesktop.systemd1.Manager",
1773 "ss", unit
, "fail");
1775 log_error("Failed to start unit: %s", bus_error_message(&error
, -r
));
1779 r
= sd_bus_message_read(reply
, "o", &object
);
1781 return bus_log_parse_error(r
);
1783 r
= bus_wait_for_jobs_add(w
, object
);
1788 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1795 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1796 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1797 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1798 UnitFileChange
*changes
= NULL
;
1799 size_t n_changes
= 0;
1800 const char *method
= NULL
;
1801 sd_bus
*bus
= userdata
;
1806 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1808 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1810 r
= sd_bus_message_new_method_call(
1813 "org.freedesktop.systemd1",
1814 "/org/freedesktop/systemd1",
1815 "org.freedesktop.systemd1.Manager",
1818 return bus_log_create_error(r
);
1820 r
= sd_bus_message_open_container(m
, 'a', "s");
1822 return bus_log_create_error(r
);
1824 for (i
= 1; i
< argc
; i
++) {
1825 _cleanup_free_
char *unit
= NULL
;
1827 r
= make_service_name(argv
[i
], &unit
);
1831 r
= image_exists(bus
, argv
[i
]);
1835 log_error("Machine image '%s' does not exist.", argv
[1]);
1839 r
= sd_bus_message_append(m
, "s", unit
);
1841 return bus_log_create_error(r
);
1844 r
= sd_bus_message_close_container(m
);
1846 return bus_log_create_error(r
);
1848 if (streq(argv
[0], "enable"))
1849 r
= sd_bus_message_append(m
, "bb", false, false);
1851 r
= sd_bus_message_append(m
, "b", false);
1853 return bus_log_create_error(r
);
1855 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1857 log_error("Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1861 if (streq(argv
[0], "enable")) {
1862 r
= sd_bus_message_read(reply
, "b", NULL
);
1864 return bus_log_parse_error(r
);
1867 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1871 r
= sd_bus_call_method(
1873 "org.freedesktop.systemd1",
1874 "/org/freedesktop/systemd1",
1875 "org.freedesktop.systemd1.Manager",
1881 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1888 unit_file_changes_free(changes
, n_changes
);
1893 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1894 const char **our_path
= userdata
, *line
;
1901 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1903 bus_log_parse_error(r
);
1907 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1910 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1913 log_full(priority
, "%s", line
);
1917 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1918 const char **our_path
= userdata
, *path
, *result
;
1925 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1927 bus_log_parse_error(r
);
1931 if (!streq_ptr(*our_path
, path
))
1934 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1938 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1943 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1945 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1949 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1950 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1951 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1952 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1953 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1954 const char *path
= NULL
;
1961 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1963 r
= sd_event_default(&event
);
1965 return log_error_errno(r
, "Failed to get event loop: %m");
1967 r
= sd_bus_attach_event(bus
, event
, 0);
1969 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1971 r
= sd_bus_match_signal_async(
1974 "org.freedesktop.import1",
1975 "/org/freedesktop/import1",
1976 "org.freedesktop.import1.Manager",
1978 match_transfer_removed
, NULL
, &path
);
1980 return log_error_errno(r
, "Failed to request match: %m");
1982 r
= sd_bus_match_signal_async(
1985 "org.freedesktop.import1",
1987 "org.freedesktop.import1.Transfer",
1989 match_log_message
, NULL
, &path
);
1991 return log_error_errno(r
, "Failed to request match: %m");
1993 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1995 log_error("Failed to transfer image: %s", bus_error_message(&error
, -r
));
1999 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
2001 return bus_log_parse_error(r
);
2003 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
2006 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
2008 sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
2009 sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
2011 r
= sd_event_loop(event
);
2013 return log_error_errno(r
, "Failed to run event loop: %m");
2018 static int import_tar(int argc
, char *argv
[], void *userdata
) {
2019 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2020 _cleanup_free_
char *ll
= NULL
;
2021 _cleanup_close_
int fd
= -1;
2022 const char *local
= NULL
, *path
= NULL
;
2023 sd_bus
*bus
= userdata
;
2030 if (isempty(path
) || streq(path
, "-"))
2036 local
= basename(path
);
2037 if (isempty(local
) || streq(local
, "-"))
2041 log_error("Need either path or local name.");
2045 r
= tar_strip_suffixes(local
, &ll
);
2051 if (!machine_name_is_valid(local
)) {
2052 log_error("Local name %s is not a suitable machine name.", local
);
2057 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2059 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2062 r
= sd_bus_message_new_method_call(
2065 "org.freedesktop.import1",
2066 "/org/freedesktop/import1",
2067 "org.freedesktop.import1.Manager",
2070 return bus_log_create_error(r
);
2072 r
= sd_bus_message_append(
2075 fd
>= 0 ? fd
: STDIN_FILENO
,
2080 return bus_log_create_error(r
);
2082 return transfer_image_common(bus
, m
);
2085 static int import_raw(int argc
, char *argv
[], void *userdata
) {
2086 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2087 _cleanup_free_
char *ll
= NULL
;
2088 _cleanup_close_
int fd
= -1;
2089 const char *local
= NULL
, *path
= NULL
;
2090 sd_bus
*bus
= userdata
;
2097 if (isempty(path
) || streq(path
, "-"))
2103 local
= basename(path
);
2104 if (isempty(local
) || streq(local
, "-"))
2108 log_error("Need either path or local name.");
2112 r
= raw_strip_suffixes(local
, &ll
);
2118 if (!machine_name_is_valid(local
)) {
2119 log_error("Local name %s is not a suitable machine name.", local
);
2124 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2126 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2129 r
= sd_bus_message_new_method_call(
2132 "org.freedesktop.import1",
2133 "/org/freedesktop/import1",
2134 "org.freedesktop.import1.Manager",
2137 return bus_log_create_error(r
);
2139 r
= sd_bus_message_append(
2142 fd
>= 0 ? fd
: STDIN_FILENO
,
2147 return bus_log_create_error(r
);
2149 return transfer_image_common(bus
, m
);
2152 static void determine_compression_from_filename(const char *p
) {
2159 if (endswith(p
, ".xz"))
2161 else if (endswith(p
, ".gz"))
2162 arg_format
= "gzip";
2163 else if (endswith(p
, ".bz2"))
2164 arg_format
= "bzip2";
2167 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2168 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2169 _cleanup_close_
int fd
= -1;
2170 const char *local
= NULL
, *path
= NULL
;
2171 sd_bus
*bus
= userdata
;
2177 if (!machine_name_is_valid(local
)) {
2178 log_error("Machine name %s is not valid.", local
);
2184 if (isempty(path
) || streq(path
, "-"))
2188 determine_compression_from_filename(path
);
2190 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2192 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2195 r
= sd_bus_message_new_method_call(
2198 "org.freedesktop.import1",
2199 "/org/freedesktop/import1",
2200 "org.freedesktop.import1.Manager",
2203 return bus_log_create_error(r
);
2205 r
= sd_bus_message_append(
2209 fd
>= 0 ? fd
: STDOUT_FILENO
,
2212 return bus_log_create_error(r
);
2214 return transfer_image_common(bus
, m
);
2217 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2218 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2219 _cleanup_close_
int fd
= -1;
2220 const char *local
= NULL
, *path
= NULL
;
2221 sd_bus
*bus
= userdata
;
2227 if (!machine_name_is_valid(local
)) {
2228 log_error("Machine name %s is not valid.", local
);
2234 if (isempty(path
) || streq(path
, "-"))
2238 determine_compression_from_filename(path
);
2240 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2242 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2245 r
= sd_bus_message_new_method_call(
2248 "org.freedesktop.import1",
2249 "/org/freedesktop/import1",
2250 "org.freedesktop.import1.Manager",
2253 return bus_log_create_error(r
);
2255 r
= sd_bus_message_append(
2259 fd
>= 0 ? fd
: STDOUT_FILENO
,
2262 return bus_log_create_error(r
);
2264 return transfer_image_common(bus
, m
);
2267 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2268 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2269 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2270 const char *local
, *remote
;
2271 sd_bus
*bus
= userdata
;
2277 if (!http_url_is_valid(remote
)) {
2278 log_error("URL '%s' is not valid.", remote
);
2285 r
= import_url_last_component(remote
, &l
);
2287 return log_error_errno(r
, "Failed to get final component of URL: %m");
2292 if (isempty(local
) || streq(local
, "-"))
2296 r
= tar_strip_suffixes(local
, &ll
);
2302 if (!machine_name_is_valid(local
)) {
2303 log_error("Local name %s is not a suitable machine name.", local
);
2308 r
= sd_bus_message_new_method_call(
2311 "org.freedesktop.import1",
2312 "/org/freedesktop/import1",
2313 "org.freedesktop.import1.Manager",
2316 return bus_log_create_error(r
);
2318 r
= sd_bus_message_append(
2323 import_verify_to_string(arg_verify
),
2326 return bus_log_create_error(r
);
2328 return transfer_image_common(bus
, m
);
2331 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2332 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2333 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2334 const char *local
, *remote
;
2335 sd_bus
*bus
= userdata
;
2341 if (!http_url_is_valid(remote
)) {
2342 log_error("URL '%s' is not valid.", remote
);
2349 r
= import_url_last_component(remote
, &l
);
2351 return log_error_errno(r
, "Failed to get final component of URL: %m");
2356 if (isempty(local
) || streq(local
, "-"))
2360 r
= raw_strip_suffixes(local
, &ll
);
2366 if (!machine_name_is_valid(local
)) {
2367 log_error("Local name %s is not a suitable machine name.", local
);
2372 r
= sd_bus_message_new_method_call(
2375 "org.freedesktop.import1",
2376 "/org/freedesktop/import1",
2377 "org.freedesktop.import1.Manager",
2380 return bus_log_create_error(r
);
2382 r
= sd_bus_message_append(
2387 import_verify_to_string(arg_verify
),
2390 return bus_log_create_error(r
);
2392 return transfer_image_common(bus
, m
);
2395 typedef struct TransferInfo
{
2403 static int compare_transfer_info(const void *a
, const void *b
) {
2404 const TransferInfo
*x
= a
, *y
= b
;
2406 return strcmp(x
->local
, y
->local
);
2409 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2410 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2411 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2412 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2413 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2414 size_t n_transfers
= 0, n_allocated
= 0, j
;
2415 const char *type
, *remote
, *local
;
2416 sd_bus
*bus
= userdata
;
2417 uint32_t id
, max_id
= 0;
2421 (void) pager_open(arg_no_pager
, false);
2423 r
= sd_bus_call_method(bus
,
2424 "org.freedesktop.import1",
2425 "/org/freedesktop/import1",
2426 "org.freedesktop.import1.Manager",
2432 log_error("Could not get transfers: %s", bus_error_message(&error
, -r
));
2436 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2438 return bus_log_parse_error(r
);
2440 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2443 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2446 transfers
[n_transfers
].id
= id
;
2447 transfers
[n_transfers
].type
= type
;
2448 transfers
[n_transfers
].remote
= remote
;
2449 transfers
[n_transfers
].local
= local
;
2450 transfers
[n_transfers
].progress
= progress
;
2470 return bus_log_parse_error(r
);
2472 r
= sd_bus_message_exit_container(reply
);
2474 return bus_log_parse_error(r
);
2476 qsort_safe(transfers
, n_transfers
, sizeof(TransferInfo
), compare_transfer_info
);
2478 if (arg_legend
&& n_transfers
> 0)
2479 printf("%-*s %-*s %-*s %-*s %-*s\n",
2480 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2482 (int) max_type
, "TYPE",
2483 (int) max_local
, "LOCAL",
2484 (int) max_remote
, "REMOTE");
2486 for (j
= 0; j
< n_transfers
; j
++)
2487 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2488 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2489 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2490 (int) max_type
, transfers
[j
].type
,
2491 (int) max_local
, transfers
[j
].local
,
2492 (int) max_remote
, transfers
[j
].remote
);
2495 if (n_transfers
> 0)
2496 printf("\n%zu transfers listed.\n", n_transfers
);
2498 printf("No transfers.\n");
2504 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2505 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2506 sd_bus
*bus
= userdata
;
2511 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2513 for (i
= 1; i
< argc
; i
++) {
2516 r
= safe_atou32(argv
[i
], &id
);
2518 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2520 r
= sd_bus_call_method(
2522 "org.freedesktop.import1",
2523 "/org/freedesktop/import1",
2524 "org.freedesktop.import1.Manager",
2530 log_error("Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2538 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2539 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2540 sd_bus
*bus
= userdata
;
2544 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2546 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2547 limit
= (uint64_t) -1;
2549 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2551 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2555 /* With two arguments changes the quota limit of the
2556 * specified image */
2557 r
= sd_bus_call_method(
2559 "org.freedesktop.machine1",
2560 "/org/freedesktop/machine1",
2561 "org.freedesktop.machine1.Manager",
2565 "st", argv
[1], limit
);
2567 /* With one argument changes the pool quota limit */
2568 r
= sd_bus_call_method(
2570 "org.freedesktop.machine1",
2571 "/org/freedesktop/machine1",
2572 "org.freedesktop.machine1.Manager",
2579 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2584 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2585 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2586 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2587 uint64_t usage
, total
= 0;
2588 char fb
[FORMAT_BYTES_MAX
];
2589 sd_bus
*bus
= userdata
;
2594 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2596 r
= sd_bus_message_new_method_call(
2599 "org.freedesktop.machine1",
2600 "/org/freedesktop/machine1",
2601 "org.freedesktop.machine1.Manager",
2604 return bus_log_create_error(r
);
2606 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2608 return bus_log_create_error(r
);
2610 /* This is a slow operation, hence permit a longer time for completion. */
2611 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2613 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2615 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2617 return bus_log_parse_error(r
);
2619 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2620 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2621 name
, format_bytes(fb
, sizeof(fb
), usage
));
2627 r
= sd_bus_message_exit_container(reply
);
2629 return bus_log_parse_error(r
);
2631 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2632 c
, format_bytes(fb
, sizeof(fb
), total
));
2637 static int help(int argc
, char *argv
[], void *userdata
) {
2638 (void) pager_open(arg_no_pager
, false);
2640 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2641 "Send control commands to or query the virtual machine and container\n"
2642 "registration manager.\n\n"
2643 " -h --help Show this help\n"
2644 " --version Show package version\n"
2645 " --no-pager Do not pipe output into a pager\n"
2646 " --no-legend Do not show the headers and footers\n"
2647 " --no-ask-password Do not ask for system passwords\n"
2648 " -H --host=[USER@]HOST Operate on remote host\n"
2649 " -M --machine=CONTAINER Operate on local container\n"
2650 " -p --property=NAME Show only properties by this name\n"
2651 " -q --quiet Suppress output\n"
2652 " -a --all Show all properties, including empty ones\n"
2653 " --value When showing properties, only print the value\n"
2654 " -l --full Do not ellipsize output\n"
2655 " --kill-who=WHO Who to send signal to\n"
2656 " -s --signal=SIGNAL Which signal to send\n"
2657 " --uid=USER Specify user ID to invoke shell as\n"
2658 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2659 " --read-only Create read-only bind mount\n"
2660 " --mkdir Create directory before bind mounting, if missing\n"
2661 " -n --lines=INTEGER Number of journal entries to show\n"
2662 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2663 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2664 " short-iso, short-iso-precise, short-full,\n"
2665 " short-monotonic, short-unix, verbose, export,\n"
2666 " json, json-pretty, json-sse, cat)\n"
2667 " --verify=MODE Verification mode for downloaded images (no,\n"
2668 " checksum, signature)\n"
2669 " --force Download image even if already exists\n\n"
2670 "Machine Commands:\n"
2671 " list List running VMs and containers\n"
2672 " status NAME... Show VM/container details\n"
2673 " show [NAME...] Show properties of one or more VMs/containers\n"
2674 " start NAME... Start container as a service\n"
2675 " login [NAME] Get a login prompt in a container or on the\n"
2677 " shell [[USER@]NAME [COMMAND...]]\n"
2678 " Invoke a shell (or other command) in a container\n"
2679 " or on the local host\n"
2680 " enable NAME... Enable automatic container start at boot\n"
2681 " disable NAME... Disable automatic container start at boot\n"
2682 " poweroff NAME... Power off one or more containers\n"
2683 " reboot NAME... Reboot one or more containers\n"
2684 " terminate NAME... Terminate one or more VMs/containers\n"
2685 " kill NAME... Send signal to processes of a VM/container\n"
2686 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2687 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2688 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2690 " list-images Show available container and VM images\n"
2691 " image-status [NAME...] Show image details\n"
2692 " show-image [NAME...] Show properties of image\n"
2693 " clone NAME NAME Clone an image\n"
2694 " rename NAME NAME Rename an image\n"
2695 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2696 " remove NAME... Remove an image\n"
2697 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2698 " clean Remove hidden (or all) images\n\n"
2699 "Image Transfer Commands:\n"
2700 " pull-tar URL [NAME] Download a TAR container image\n"
2701 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2702 " import-tar FILE [NAME] Import a local TAR container image\n"
2703 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2704 " export-tar NAME [FILE] Export a TAR container image locally\n"
2705 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2706 " list-transfers Show list of downloads in progress\n"
2707 " cancel-transfer Cancel a download\n"
2708 , program_invocation_short_name
);
2713 static int parse_argv(int argc
, char *argv
[]) {
2716 ARG_VERSION
= 0x100,
2723 ARG_NO_ASK_PASSWORD
,
2731 static const struct option options
[] = {
2732 { "help", no_argument
, NULL
, 'h' },
2733 { "version", no_argument
, NULL
, ARG_VERSION
},
2734 { "property", required_argument
, NULL
, 'p' },
2735 { "all", no_argument
, NULL
, 'a' },
2736 { "value", no_argument
, NULL
, ARG_VALUE
},
2737 { "full", no_argument
, NULL
, 'l' },
2738 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2739 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2740 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2741 { "signal", required_argument
, NULL
, 's' },
2742 { "host", required_argument
, NULL
, 'H' },
2743 { "machine", required_argument
, NULL
, 'M' },
2744 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2745 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2746 { "quiet", no_argument
, NULL
, 'q' },
2747 { "lines", required_argument
, NULL
, 'n' },
2748 { "output", required_argument
, NULL
, 'o' },
2749 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2750 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2751 { "force", no_argument
, NULL
, ARG_FORCE
},
2752 { "format", required_argument
, NULL
, ARG_FORMAT
},
2753 { "uid", required_argument
, NULL
, ARG_UID
},
2754 { "setenv", required_argument
, NULL
, 'E' },
2755 { "max-addresses", required_argument
, NULL
, ARG_NUMBER_IPS
},
2759 bool reorder
= false;
2760 int c
, r
, shell
= -1;
2766 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2768 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2774 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2775 * non-option argument was discovered. */
2779 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2780 * for switches after the main verb. However, for "shell" we really don't want that, since we
2781 * want that switches specified after the machine name are passed to the program to execute,
2782 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2783 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2784 * non-option parameter. If it's the verb "shell" we remember its position and continue
2785 * processing options. In this case, as soon as we hit the next non-option argument we found
2786 * the machine name, and stop further processing. If the first non-option argument is any other
2787 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2791 /* If we already found the "shell" verb on the command line, and now found the next
2792 * non-option argument, then this is the machine name and we should stop processing
2793 * further arguments. */
2794 optind
--; /* don't process this argument, go one step back */
2797 if (streq(optarg
, "shell"))
2798 /* Remember the position of the "shell" verb, and continue processing normally. */
2803 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2804 * processing normally. */
2807 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2808 * at least once with a reset option index. Hence, let's reset the option index here,
2809 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2810 * likely already processed it), and the bump the option index so that we read the
2811 * intended argument again. */
2812 saved_optind
= optind
;
2814 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2815 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2821 return help(0, NULL
, NULL
);
2827 r
= strv_extend(&arg_property
, optarg
);
2831 /* If the user asked for a particular
2832 * property, show it to him, even if it is
2850 if (safe_atou(optarg
, &arg_lines
) < 0) {
2851 log_error("Failed to parse lines '%s'", optarg
);
2857 if (streq(optarg
, "help")) {
2858 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2862 arg_output
= output_mode_from_string(optarg
);
2863 if (arg_output
< 0) {
2864 log_error("Unknown output '%s'.", optarg
);
2870 arg_no_pager
= true;
2878 arg_kill_who
= optarg
;
2882 if (streq(optarg
, "help")) {
2883 DUMP_STRING_TABLE(signal
, int, _NSIG
);
2887 arg_signal
= signal_from_string(optarg
);
2888 if (arg_signal
< 0) {
2889 log_error("Failed to parse signal string %s.", optarg
);
2894 case ARG_NO_ASK_PASSWORD
:
2895 arg_ask_password
= false;
2899 arg_transport
= BUS_TRANSPORT_REMOTE
;
2904 arg_transport
= BUS_TRANSPORT_MACHINE
;
2909 arg_read_only
= true;
2921 if (streq(optarg
, "help")) {
2922 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2926 arg_verify
= import_verify_from_string(optarg
);
2927 if (arg_verify
< 0) {
2928 log_error("Failed to parse --verify= setting: %s", optarg
);
2938 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2")) {
2939 log_error("Unknown format: %s", optarg
);
2943 arg_format
= optarg
;
2951 if (!env_assignment_is_valid(optarg
)) {
2952 log_error("Environment assignment invalid: %s", optarg
);
2956 r
= strv_extend(&arg_setenv
, optarg
);
2961 case ARG_NUMBER_IPS
:
2962 if (streq(optarg
, "all"))
2963 arg_addrs
= ALL_IP_ADDRESSES
;
2964 else if (safe_atoi(optarg
, &arg_addrs
) < 0) {
2965 log_error("Invalid number of IPs");
2967 } else if (arg_addrs
< 0) {
2968 log_error("Number of IPs cannot be negative");
2977 assert_not_reached("Unhandled option");
2986 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2987 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2989 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2992 for (i
= shell
; i
< optind
; i
++)
2993 argv
[i
] = argv
[i
+1];
3000 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
3002 static const Verb verbs
[] = {
3003 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3004 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
3005 { "list-images", VERB_ANY
, 1, 0, list_images
},
3006 { "status", 2, VERB_ANY
, 0, show_machine
},
3007 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
3008 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
3009 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
3010 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
3011 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
3012 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
3013 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
3014 { "kill", 2, VERB_ANY
, 0, kill_machine
},
3015 { "login", VERB_ANY
, 2, 0, login_machine
},
3016 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
3017 { "bind", 3, 4, 0, bind_mount
},
3018 { "copy-to", 3, 4, 0, copy_files
},
3019 { "copy-from", 3, 4, 0, copy_files
},
3020 { "remove", 2, VERB_ANY
, 0, remove_image
},
3021 { "rename", 3, 3, 0, rename_image
},
3022 { "clone", 3, 3, 0, clone_image
},
3023 { "read-only", 2, 3, 0, read_only_image
},
3024 { "start", 2, VERB_ANY
, 0, start_machine
},
3025 { "enable", 2, VERB_ANY
, 0, enable_machine
},
3026 { "disable", 2, VERB_ANY
, 0, enable_machine
},
3027 { "import-tar", 2, 3, 0, import_tar
},
3028 { "import-raw", 2, 3, 0, import_raw
},
3029 { "export-tar", 2, 3, 0, export_tar
},
3030 { "export-raw", 2, 3, 0, export_raw
},
3031 { "pull-tar", 2, 3, 0, pull_tar
},
3032 { "pull-raw", 2, 3, 0, pull_raw
},
3033 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
3034 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
3035 { "set-limit", 2, 3, 0, set_limit
},
3036 { "clean", VERB_ANY
, 1, 0, clean_images
},
3040 return dispatch_verb(argc
, argv
, verbs
, bus
);
3043 int main(int argc
, char*argv
[]) {
3047 setlocale(LC_ALL
, "");
3048 log_parse_environment();
3052 r
= parse_argv(argc
, argv
);
3056 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
3058 log_error_errno(r
, "Failed to create bus connection: %m");
3062 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
3064 r
= machinectl_main(argc
, argv
, bus
);
3067 /* make sure we terminate the bus connection first, and then close the
3068 * pager, see issue #3543 for the details. */
3069 sd_bus_flush_close_unref(bus
);
3071 polkit_agent_close();
3073 strv_free(arg_property
);
3074 strv_free(arg_setenv
);
3076 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;