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
, *object
;
304 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, &object
);
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 const char *name
, *type
, *object
;
384 uint64_t crtime
, mtime
, size
;
389 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, &object
);
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 *pty
, *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
, &pty
);
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 *pty
, *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 log_error("Failed to get shell PTY: %s", bus_error_message(&error
, -r
));
1558 r
= sd_bus_message_read(reply
, "hs", &master
, &pty
);
1560 return bus_log_parse_error(r
);
1562 return process_forward(event
, &forward
, master
, 0, machine
);
1565 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1566 sd_bus
*bus
= userdata
;
1571 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1573 for (i
= 1; i
< argc
; i
++) {
1574 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1575 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1577 r
= sd_bus_message_new_method_call(
1580 "org.freedesktop.machine1",
1581 "/org/freedesktop/machine1",
1582 "org.freedesktop.machine1.Manager",
1585 return bus_log_create_error(r
);
1587 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1589 return bus_log_create_error(r
);
1591 /* This is a slow operation, hence turn off any method call timeouts */
1592 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1594 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1600 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1601 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1602 sd_bus
*bus
= userdata
;
1607 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1609 r
= sd_bus_call_method(
1611 "org.freedesktop.machine1",
1612 "/org/freedesktop/machine1",
1613 "org.freedesktop.machine1.Manager",
1617 "ss", argv
[1], argv
[2]);
1619 log_error("Could not rename image: %s", bus_error_message(&error
, -r
));
1626 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1627 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1628 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1629 sd_bus
*bus
= userdata
;
1634 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1636 r
= sd_bus_message_new_method_call(
1639 "org.freedesktop.machine1",
1640 "/org/freedesktop/machine1",
1641 "org.freedesktop.machine1.Manager",
1644 return bus_log_create_error(r
);
1646 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1648 return bus_log_create_error(r
);
1650 /* This is a slow operation, hence turn off any method call timeouts */
1651 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1653 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1658 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1659 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1660 sd_bus
*bus
= userdata
;
1666 b
= parse_boolean(argv
[2]);
1668 log_error("Failed to parse boolean argument: %s", argv
[2]);
1673 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1675 r
= sd_bus_call_method(
1677 "org.freedesktop.machine1",
1678 "/org/freedesktop/machine1",
1679 "org.freedesktop.machine1.Manager",
1680 "MarkImageReadOnly",
1685 log_error("Could not mark image read-only: %s", bus_error_message(&error
, -r
));
1692 static int image_exists(sd_bus
*bus
, const char *name
) {
1693 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1699 r
= sd_bus_call_method(
1701 "org.freedesktop.machine1",
1702 "/org/freedesktop/machine1",
1703 "org.freedesktop.machine1.Manager",
1709 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1712 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, -r
));
1718 static int make_service_name(const char *name
, char **ret
) {
1724 if (!machine_name_is_valid(name
)) {
1725 log_error("Invalid machine name %s.", name
);
1729 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1731 return log_error_errno(r
, "Failed to build unit name: %m");
1736 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1737 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1738 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1739 sd_bus
*bus
= userdata
;
1744 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1746 r
= bus_wait_for_jobs_new(bus
, &w
);
1750 for (i
= 1; i
< argc
; i
++) {
1751 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1752 _cleanup_free_
char *unit
= NULL
;
1755 r
= make_service_name(argv
[i
], &unit
);
1759 r
= image_exists(bus
, argv
[i
]);
1763 log_error("Machine image '%s' does not exist.", argv
[1]);
1767 r
= sd_bus_call_method(
1769 "org.freedesktop.systemd1",
1770 "/org/freedesktop/systemd1",
1771 "org.freedesktop.systemd1.Manager",
1775 "ss", unit
, "fail");
1777 log_error("Failed to start unit: %s", bus_error_message(&error
, -r
));
1781 r
= sd_bus_message_read(reply
, "o", &object
);
1783 return bus_log_parse_error(r
);
1785 r
= bus_wait_for_jobs_add(w
, object
);
1790 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1797 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1798 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1799 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1800 UnitFileChange
*changes
= NULL
;
1801 size_t n_changes
= 0;
1802 int carries_install_info
= 0;
1803 const char *method
= NULL
;
1804 sd_bus
*bus
= userdata
;
1809 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1811 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1813 r
= sd_bus_message_new_method_call(
1816 "org.freedesktop.systemd1",
1817 "/org/freedesktop/systemd1",
1818 "org.freedesktop.systemd1.Manager",
1821 return bus_log_create_error(r
);
1823 r
= sd_bus_message_open_container(m
, 'a', "s");
1825 return bus_log_create_error(r
);
1827 for (i
= 1; i
< argc
; i
++) {
1828 _cleanup_free_
char *unit
= NULL
;
1830 r
= make_service_name(argv
[i
], &unit
);
1834 r
= image_exists(bus
, argv
[i
]);
1838 log_error("Machine image '%s' does not exist.", argv
[1]);
1842 r
= sd_bus_message_append(m
, "s", unit
);
1844 return bus_log_create_error(r
);
1847 r
= sd_bus_message_close_container(m
);
1849 return bus_log_create_error(r
);
1851 if (streq(argv
[0], "enable"))
1852 r
= sd_bus_message_append(m
, "bb", false, false);
1854 r
= sd_bus_message_append(m
, "b", false);
1856 return bus_log_create_error(r
);
1858 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1860 log_error("Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1864 if (streq(argv
[0], "enable")) {
1865 r
= sd_bus_message_read(reply
, "b", carries_install_info
);
1867 return bus_log_parse_error(r
);
1870 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1874 r
= sd_bus_call_method(
1876 "org.freedesktop.systemd1",
1877 "/org/freedesktop/systemd1",
1878 "org.freedesktop.systemd1.Manager",
1884 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1891 unit_file_changes_free(changes
, n_changes
);
1896 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1897 const char **our_path
= userdata
, *line
;
1904 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1906 bus_log_parse_error(r
);
1910 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1913 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1916 log_full(priority
, "%s", line
);
1920 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1921 const char **our_path
= userdata
, *path
, *result
;
1928 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1930 bus_log_parse_error(r
);
1934 if (!streq_ptr(*our_path
, path
))
1937 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1941 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1946 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1948 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1952 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1953 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1954 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1955 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1956 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1957 const char *path
= NULL
;
1964 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1966 r
= sd_event_default(&event
);
1968 return log_error_errno(r
, "Failed to get event loop: %m");
1970 r
= sd_bus_attach_event(bus
, event
, 0);
1972 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1974 r
= sd_bus_match_signal_async(
1977 "org.freedesktop.import1",
1978 "/org/freedesktop/import1",
1979 "org.freedesktop.import1.Manager",
1981 match_transfer_removed
, NULL
, &path
);
1983 return log_error_errno(r
, "Failed to request match: %m");
1985 r
= sd_bus_match_signal_async(
1988 "org.freedesktop.import1",
1990 "org.freedesktop.import1.Transfer",
1992 match_log_message
, NULL
, &path
);
1994 return log_error_errno(r
, "Failed to request match: %m");
1996 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1998 log_error("Failed to transfer image: %s", bus_error_message(&error
, -r
));
2002 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
2004 return bus_log_parse_error(r
);
2006 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
2009 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
2011 sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
2012 sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
2014 r
= sd_event_loop(event
);
2016 return log_error_errno(r
, "Failed to run event loop: %m");
2021 static int import_tar(int argc
, char *argv
[], void *userdata
) {
2022 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2023 _cleanup_free_
char *ll
= NULL
;
2024 _cleanup_close_
int fd
= -1;
2025 const char *local
= NULL
, *path
= NULL
;
2026 sd_bus
*bus
= userdata
;
2033 if (isempty(path
) || streq(path
, "-"))
2039 local
= basename(path
);
2040 if (isempty(local
) || streq(local
, "-"))
2044 log_error("Need either path or local name.");
2048 r
= tar_strip_suffixes(local
, &ll
);
2054 if (!machine_name_is_valid(local
)) {
2055 log_error("Local name %s is not a suitable machine name.", local
);
2060 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2062 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2065 r
= sd_bus_message_new_method_call(
2068 "org.freedesktop.import1",
2069 "/org/freedesktop/import1",
2070 "org.freedesktop.import1.Manager",
2073 return bus_log_create_error(r
);
2075 r
= sd_bus_message_append(
2078 fd
>= 0 ? fd
: STDIN_FILENO
,
2083 return bus_log_create_error(r
);
2085 return transfer_image_common(bus
, m
);
2088 static int import_raw(int argc
, char *argv
[], void *userdata
) {
2089 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2090 _cleanup_free_
char *ll
= NULL
;
2091 _cleanup_close_
int fd
= -1;
2092 const char *local
= NULL
, *path
= NULL
;
2093 sd_bus
*bus
= userdata
;
2100 if (isempty(path
) || streq(path
, "-"))
2106 local
= basename(path
);
2107 if (isempty(local
) || streq(local
, "-"))
2111 log_error("Need either path or local name.");
2115 r
= raw_strip_suffixes(local
, &ll
);
2121 if (!machine_name_is_valid(local
)) {
2122 log_error("Local name %s is not a suitable machine name.", local
);
2127 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2129 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2132 r
= sd_bus_message_new_method_call(
2135 "org.freedesktop.import1",
2136 "/org/freedesktop/import1",
2137 "org.freedesktop.import1.Manager",
2140 return bus_log_create_error(r
);
2142 r
= sd_bus_message_append(
2145 fd
>= 0 ? fd
: STDIN_FILENO
,
2150 return bus_log_create_error(r
);
2152 return transfer_image_common(bus
, m
);
2155 static void determine_compression_from_filename(const char *p
) {
2162 if (endswith(p
, ".xz"))
2164 else if (endswith(p
, ".gz"))
2165 arg_format
= "gzip";
2166 else if (endswith(p
, ".bz2"))
2167 arg_format
= "bzip2";
2170 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2171 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2172 _cleanup_close_
int fd
= -1;
2173 const char *local
= NULL
, *path
= NULL
;
2174 sd_bus
*bus
= userdata
;
2180 if (!machine_name_is_valid(local
)) {
2181 log_error("Machine name %s is not valid.", local
);
2187 if (isempty(path
) || streq(path
, "-"))
2191 determine_compression_from_filename(path
);
2193 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2195 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2198 r
= sd_bus_message_new_method_call(
2201 "org.freedesktop.import1",
2202 "/org/freedesktop/import1",
2203 "org.freedesktop.import1.Manager",
2206 return bus_log_create_error(r
);
2208 r
= sd_bus_message_append(
2212 fd
>= 0 ? fd
: STDOUT_FILENO
,
2215 return bus_log_create_error(r
);
2217 return transfer_image_common(bus
, m
);
2220 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2221 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2222 _cleanup_close_
int fd
= -1;
2223 const char *local
= NULL
, *path
= NULL
;
2224 sd_bus
*bus
= userdata
;
2230 if (!machine_name_is_valid(local
)) {
2231 log_error("Machine name %s is not valid.", local
);
2237 if (isempty(path
) || streq(path
, "-"))
2241 determine_compression_from_filename(path
);
2243 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2245 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2248 r
= sd_bus_message_new_method_call(
2251 "org.freedesktop.import1",
2252 "/org/freedesktop/import1",
2253 "org.freedesktop.import1.Manager",
2256 return bus_log_create_error(r
);
2258 r
= sd_bus_message_append(
2262 fd
>= 0 ? fd
: STDOUT_FILENO
,
2265 return bus_log_create_error(r
);
2267 return transfer_image_common(bus
, m
);
2270 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2271 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2272 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2273 const char *local
, *remote
;
2274 sd_bus
*bus
= userdata
;
2280 if (!http_url_is_valid(remote
)) {
2281 log_error("URL '%s' is not valid.", remote
);
2288 r
= import_url_last_component(remote
, &l
);
2290 return log_error_errno(r
, "Failed to get final component of URL: %m");
2295 if (isempty(local
) || streq(local
, "-"))
2299 r
= tar_strip_suffixes(local
, &ll
);
2305 if (!machine_name_is_valid(local
)) {
2306 log_error("Local name %s is not a suitable machine name.", local
);
2311 r
= sd_bus_message_new_method_call(
2314 "org.freedesktop.import1",
2315 "/org/freedesktop/import1",
2316 "org.freedesktop.import1.Manager",
2319 return bus_log_create_error(r
);
2321 r
= sd_bus_message_append(
2326 import_verify_to_string(arg_verify
),
2329 return bus_log_create_error(r
);
2331 return transfer_image_common(bus
, m
);
2334 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2335 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2336 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2337 const char *local
, *remote
;
2338 sd_bus
*bus
= userdata
;
2344 if (!http_url_is_valid(remote
)) {
2345 log_error("URL '%s' is not valid.", remote
);
2352 r
= import_url_last_component(remote
, &l
);
2354 return log_error_errno(r
, "Failed to get final component of URL: %m");
2359 if (isempty(local
) || streq(local
, "-"))
2363 r
= raw_strip_suffixes(local
, &ll
);
2369 if (!machine_name_is_valid(local
)) {
2370 log_error("Local name %s is not a suitable machine name.", local
);
2375 r
= sd_bus_message_new_method_call(
2378 "org.freedesktop.import1",
2379 "/org/freedesktop/import1",
2380 "org.freedesktop.import1.Manager",
2383 return bus_log_create_error(r
);
2385 r
= sd_bus_message_append(
2390 import_verify_to_string(arg_verify
),
2393 return bus_log_create_error(r
);
2395 return transfer_image_common(bus
, m
);
2398 typedef struct TransferInfo
{
2406 static int compare_transfer_info(const void *a
, const void *b
) {
2407 const TransferInfo
*x
= a
, *y
= b
;
2409 return strcmp(x
->local
, y
->local
);
2412 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2413 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2414 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2415 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2416 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2417 size_t n_transfers
= 0, n_allocated
= 0, j
;
2418 const char *type
, *remote
, *local
, *object
;
2419 sd_bus
*bus
= userdata
;
2420 uint32_t id
, max_id
= 0;
2424 (void) pager_open(arg_no_pager
, false);
2426 r
= sd_bus_call_method(bus
,
2427 "org.freedesktop.import1",
2428 "/org/freedesktop/import1",
2429 "org.freedesktop.import1.Manager",
2435 log_error("Could not get transfers: %s", bus_error_message(&error
, -r
));
2439 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2441 return bus_log_parse_error(r
);
2443 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, &object
)) > 0) {
2446 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2449 transfers
[n_transfers
].id
= id
;
2450 transfers
[n_transfers
].type
= type
;
2451 transfers
[n_transfers
].remote
= remote
;
2452 transfers
[n_transfers
].local
= local
;
2453 transfers
[n_transfers
].progress
= progress
;
2473 return bus_log_parse_error(r
);
2475 r
= sd_bus_message_exit_container(reply
);
2477 return bus_log_parse_error(r
);
2479 qsort_safe(transfers
, n_transfers
, sizeof(TransferInfo
), compare_transfer_info
);
2481 if (arg_legend
&& n_transfers
> 0)
2482 printf("%-*s %-*s %-*s %-*s %-*s\n",
2483 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2485 (int) max_type
, "TYPE",
2486 (int) max_local
, "LOCAL",
2487 (int) max_remote
, "REMOTE");
2489 for (j
= 0; j
< n_transfers
; j
++)
2490 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2491 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2492 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2493 (int) max_type
, transfers
[j
].type
,
2494 (int) max_local
, transfers
[j
].local
,
2495 (int) max_remote
, transfers
[j
].remote
);
2498 if (n_transfers
> 0)
2499 printf("\n%zu transfers listed.\n", n_transfers
);
2501 printf("No transfers.\n");
2507 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2508 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2509 sd_bus
*bus
= userdata
;
2514 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2516 for (i
= 1; i
< argc
; i
++) {
2519 r
= safe_atou32(argv
[i
], &id
);
2521 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2523 r
= sd_bus_call_method(
2525 "org.freedesktop.import1",
2526 "/org/freedesktop/import1",
2527 "org.freedesktop.import1.Manager",
2533 log_error("Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2541 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2542 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2543 sd_bus
*bus
= userdata
;
2547 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2549 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2550 limit
= (uint64_t) -1;
2552 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2554 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2558 /* With two arguments changes the quota limit of the
2559 * specified image */
2560 r
= sd_bus_call_method(
2562 "org.freedesktop.machine1",
2563 "/org/freedesktop/machine1",
2564 "org.freedesktop.machine1.Manager",
2568 "st", argv
[1], limit
);
2570 /* With one argument changes the pool quota limit */
2571 r
= sd_bus_call_method(
2573 "org.freedesktop.machine1",
2574 "/org/freedesktop/machine1",
2575 "org.freedesktop.machine1.Manager",
2582 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2587 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2588 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2589 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2590 uint64_t usage
, total
= 0;
2591 char fb
[FORMAT_BYTES_MAX
];
2592 sd_bus
*bus
= userdata
;
2597 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2599 r
= sd_bus_message_new_method_call(
2602 "org.freedesktop.machine1",
2603 "/org/freedesktop/machine1",
2604 "org.freedesktop.machine1.Manager",
2607 return bus_log_create_error(r
);
2609 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2611 return bus_log_create_error(r
);
2613 /* This is a slow operation, hence permit a longer time for completion. */
2614 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2616 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2618 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2620 return bus_log_parse_error(r
);
2622 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2623 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2624 name
, format_bytes(fb
, sizeof(fb
), usage
));
2630 r
= sd_bus_message_exit_container(reply
);
2632 return bus_log_parse_error(r
);
2634 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2635 c
, format_bytes(fb
, sizeof(fb
), total
));
2640 static int help(int argc
, char *argv
[], void *userdata
) {
2641 (void) pager_open(arg_no_pager
, false);
2643 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2644 "Send control commands to or query the virtual machine and container\n"
2645 "registration manager.\n\n"
2646 " -h --help Show this help\n"
2647 " --version Show package version\n"
2648 " --no-pager Do not pipe output into a pager\n"
2649 " --no-legend Do not show the headers and footers\n"
2650 " --no-ask-password Do not ask for system passwords\n"
2651 " -H --host=[USER@]HOST Operate on remote host\n"
2652 " -M --machine=CONTAINER Operate on local container\n"
2653 " -p --property=NAME Show only properties by this name\n"
2654 " -q --quiet Suppress output\n"
2655 " -a --all Show all properties, including empty ones\n"
2656 " --value When showing properties, only print the value\n"
2657 " -l --full Do not ellipsize output\n"
2658 " --kill-who=WHO Who to send signal to\n"
2659 " -s --signal=SIGNAL Which signal to send\n"
2660 " --uid=USER Specify user ID to invoke shell as\n"
2661 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2662 " --read-only Create read-only bind mount\n"
2663 " --mkdir Create directory before bind mounting, if missing\n"
2664 " -n --lines=INTEGER Number of journal entries to show\n"
2665 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2666 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2667 " short-iso, short-iso-precise, short-full,\n"
2668 " short-monotonic, short-unix, verbose, export,\n"
2669 " json, json-pretty, json-sse, cat)\n"
2670 " --verify=MODE Verification mode for downloaded images (no,\n"
2671 " checksum, signature)\n"
2672 " --force Download image even if already exists\n\n"
2673 "Machine Commands:\n"
2674 " list List running VMs and containers\n"
2675 " status NAME... Show VM/container details\n"
2676 " show [NAME...] Show properties of one or more VMs/containers\n"
2677 " start NAME... Start container as a service\n"
2678 " login [NAME] Get a login prompt in a container or on the\n"
2680 " shell [[USER@]NAME [COMMAND...]]\n"
2681 " Invoke a shell (or other command) in a container\n"
2682 " or on the local host\n"
2683 " enable NAME... Enable automatic container start at boot\n"
2684 " disable NAME... Disable automatic container start at boot\n"
2685 " poweroff NAME... Power off one or more containers\n"
2686 " reboot NAME... Reboot one or more containers\n"
2687 " terminate NAME... Terminate one or more VMs/containers\n"
2688 " kill NAME... Send signal to processes of a VM/container\n"
2689 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2690 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2691 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2693 " list-images Show available container and VM images\n"
2694 " image-status [NAME...] Show image details\n"
2695 " show-image [NAME...] Show properties of image\n"
2696 " clone NAME NAME Clone an image\n"
2697 " rename NAME NAME Rename an image\n"
2698 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2699 " remove NAME... Remove an image\n"
2700 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2701 " clean Remove hidden (or all) images\n\n"
2702 "Image Transfer Commands:\n"
2703 " pull-tar URL [NAME] Download a TAR container image\n"
2704 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2705 " import-tar FILE [NAME] Import a local TAR container image\n"
2706 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2707 " export-tar NAME [FILE] Export a TAR container image locally\n"
2708 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2709 " list-transfers Show list of downloads in progress\n"
2710 " cancel-transfer Cancel a download\n"
2711 , program_invocation_short_name
);
2716 static int parse_argv(int argc
, char *argv
[]) {
2719 ARG_VERSION
= 0x100,
2726 ARG_NO_ASK_PASSWORD
,
2734 static const struct option options
[] = {
2735 { "help", no_argument
, NULL
, 'h' },
2736 { "version", no_argument
, NULL
, ARG_VERSION
},
2737 { "property", required_argument
, NULL
, 'p' },
2738 { "all", no_argument
, NULL
, 'a' },
2739 { "value", no_argument
, NULL
, ARG_VALUE
},
2740 { "full", no_argument
, NULL
, 'l' },
2741 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2742 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2743 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2744 { "signal", required_argument
, NULL
, 's' },
2745 { "host", required_argument
, NULL
, 'H' },
2746 { "machine", required_argument
, NULL
, 'M' },
2747 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2748 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2749 { "quiet", no_argument
, NULL
, 'q' },
2750 { "lines", required_argument
, NULL
, 'n' },
2751 { "output", required_argument
, NULL
, 'o' },
2752 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2753 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2754 { "force", no_argument
, NULL
, ARG_FORCE
},
2755 { "format", required_argument
, NULL
, ARG_FORMAT
},
2756 { "uid", required_argument
, NULL
, ARG_UID
},
2757 { "setenv", required_argument
, NULL
, 'E' },
2758 { "max-addresses", required_argument
, NULL
, ARG_NUMBER_IPS
},
2762 bool reorder
= false;
2763 int c
, r
, shell
= -1;
2769 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2771 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2777 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2778 * non-option argument was discovered. */
2782 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2783 * for switches after the main verb. However, for "shell" we really don't want that, since we
2784 * want that switches specified after the machine name are passed to the program to execute,
2785 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2786 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2787 * non-option parameter. If it's the verb "shell" we remember its position and continue
2788 * processing options. In this case, as soon as we hit the next non-option argument we found
2789 * the machine name, and stop further processing. If the first non-option argument is any other
2790 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2794 /* If we already found the "shell" verb on the command line, and now found the next
2795 * non-option argument, then this is the machine name and we should stop processing
2796 * further arguments. */
2797 optind
--; /* don't process this argument, go one step back */
2800 if (streq(optarg
, "shell"))
2801 /* Remember the position of the "shell" verb, and continue processing normally. */
2806 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2807 * processing normally. */
2810 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2811 * at least once with a reset option index. Hence, let's reset the option index here,
2812 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2813 * likely already processed it), and the bump the option index so that we read the
2814 * intended argument again. */
2815 saved_optind
= optind
;
2817 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2818 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2824 return help(0, NULL
, NULL
);
2830 r
= strv_extend(&arg_property
, optarg
);
2834 /* If the user asked for a particular
2835 * property, show it to him, even if it is
2853 if (safe_atou(optarg
, &arg_lines
) < 0) {
2854 log_error("Failed to parse lines '%s'", optarg
);
2860 if (streq(optarg
, "help")) {
2861 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2865 arg_output
= output_mode_from_string(optarg
);
2866 if (arg_output
< 0) {
2867 log_error("Unknown output '%s'.", optarg
);
2873 arg_no_pager
= true;
2881 arg_kill_who
= optarg
;
2885 if (streq(optarg
, "help")) {
2886 DUMP_STRING_TABLE(signal
, int, _NSIG
);
2890 arg_signal
= signal_from_string(optarg
);
2891 if (arg_signal
< 0) {
2892 log_error("Failed to parse signal string %s.", optarg
);
2897 case ARG_NO_ASK_PASSWORD
:
2898 arg_ask_password
= false;
2902 arg_transport
= BUS_TRANSPORT_REMOTE
;
2907 arg_transport
= BUS_TRANSPORT_MACHINE
;
2912 arg_read_only
= true;
2924 if (streq(optarg
, "help")) {
2925 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2929 arg_verify
= import_verify_from_string(optarg
);
2930 if (arg_verify
< 0) {
2931 log_error("Failed to parse --verify= setting: %s", optarg
);
2941 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2")) {
2942 log_error("Unknown format: %s", optarg
);
2946 arg_format
= optarg
;
2954 if (!env_assignment_is_valid(optarg
)) {
2955 log_error("Environment assignment invalid: %s", optarg
);
2959 r
= strv_extend(&arg_setenv
, optarg
);
2964 case ARG_NUMBER_IPS
:
2965 if (streq(optarg
, "all"))
2966 arg_addrs
= ALL_IP_ADDRESSES
;
2967 else if (safe_atoi(optarg
, &arg_addrs
) < 0) {
2968 log_error("Invalid number of IPs");
2970 } else if (arg_addrs
< 0) {
2971 log_error("Number of IPs cannot be negative");
2980 assert_not_reached("Unhandled option");
2989 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2990 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2992 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2995 for (i
= shell
; i
< optind
; i
++)
2996 argv
[i
] = argv
[i
+1];
3003 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
3005 static const Verb verbs
[] = {
3006 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3007 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
3008 { "list-images", VERB_ANY
, 1, 0, list_images
},
3009 { "status", 2, VERB_ANY
, 0, show_machine
},
3010 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
3011 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
3012 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
3013 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
3014 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
3015 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
3016 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
3017 { "kill", 2, VERB_ANY
, 0, kill_machine
},
3018 { "login", VERB_ANY
, 2, 0, login_machine
},
3019 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
3020 { "bind", 3, 4, 0, bind_mount
},
3021 { "copy-to", 3, 4, 0, copy_files
},
3022 { "copy-from", 3, 4, 0, copy_files
},
3023 { "remove", 2, VERB_ANY
, 0, remove_image
},
3024 { "rename", 3, 3, 0, rename_image
},
3025 { "clone", 3, 3, 0, clone_image
},
3026 { "read-only", 2, 3, 0, read_only_image
},
3027 { "start", 2, VERB_ANY
, 0, start_machine
},
3028 { "enable", 2, VERB_ANY
, 0, enable_machine
},
3029 { "disable", 2, VERB_ANY
, 0, enable_machine
},
3030 { "import-tar", 2, 3, 0, import_tar
},
3031 { "import-raw", 2, 3, 0, import_raw
},
3032 { "export-tar", 2, 3, 0, export_tar
},
3033 { "export-raw", 2, 3, 0, export_raw
},
3034 { "pull-tar", 2, 3, 0, pull_tar
},
3035 { "pull-raw", 2, 3, 0, pull_raw
},
3036 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
3037 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
3038 { "set-limit", 2, 3, 0, set_limit
},
3039 { "clean", VERB_ANY
, 1, 0, clean_images
},
3043 return dispatch_verb(argc
, argv
, verbs
, bus
);
3046 int main(int argc
, char*argv
[]) {
3050 setlocale(LC_ALL
, "");
3051 log_parse_environment();
3055 r
= parse_argv(argc
, argv
);
3059 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
3061 log_error_errno(r
, "Failed to create bus connection: %m");
3065 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
3067 r
= machinectl_main(argc
, argv
, bus
);
3070 /* make sure we terminate the bus connection first, and then close the
3071 * pager, see issue #3543 for the details. */
3072 sd_bus_flush_close_unref(bus
);
3074 polkit_agent_close();
3076 strv_free(arg_property
);
3077 strv_free(arg_setenv
);
3079 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;