1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include <netinet/in.h>
10 #include <sys/mount.h>
11 #include <sys/socket.h>
16 #include "alloc-util.h"
18 #include "bus-common-errors.h"
19 #include "bus-error.h"
20 #include "bus-locator.h"
21 #include "bus-map-properties.h"
22 #include "bus-print-properties.h"
23 #include "bus-unit-procs.h"
24 #include "bus-unit-util.h"
25 #include "bus-wait-for-jobs.h"
26 #include "cgroup-show.h"
27 #include "cgroup-util.h"
28 #include "constants.h"
32 #include "format-table.h"
33 #include "hostname-util.h"
34 #include "import-util.h"
35 #include "locale-util.h"
37 #include "logs-show.h"
38 #include "machine-dbus.h"
40 #include "main-func.h"
42 #include "nulstr-util.h"
44 #include "parse-argument.h"
45 #include "parse-util.h"
46 #include "path-util.h"
47 #include "pretty-print.h"
48 #include "process-util.h"
50 #include "rlimit-util.h"
52 #include "signal-util.h"
53 #include "sort-util.h"
54 #include "spawn-ask-password-agent.h"
55 #include "spawn-polkit-agent.h"
56 #include "stdio-util.h"
57 #include "string-table.h"
59 #include "terminal-util.h"
60 #include "unit-name.h"
64 static char **arg_property
= NULL
;
65 static bool arg_all
= false;
66 static BusPrintPropertyFlags arg_print_flags
= 0;
67 static bool arg_full
= false;
68 static PagerFlags arg_pager_flags
= 0;
69 static bool arg_legend
= true;
70 static const char *arg_kill_whom
= NULL
;
71 static int arg_signal
= SIGTERM
;
72 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
73 static const char *arg_host
= NULL
;
74 static bool arg_read_only
= false;
75 static bool arg_mkdir
= false;
76 static bool arg_quiet
= false;
77 static bool arg_ask_password
= true;
78 static unsigned arg_lines
= 10;
79 static OutputMode arg_output
= OUTPUT_SHORT
;
80 static bool arg_force
= false;
81 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
82 static const char* arg_format
= NULL
;
83 static const char *arg_uid
= NULL
;
84 static char **arg_setenv
= NULL
;
85 static unsigned arg_max_addresses
= 1;
87 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
88 STATIC_DESTRUCTOR_REGISTER(arg_setenv
, strv_freep
);
90 static OutputFlags
get_output_flags(void) {
92 FLAGS_SET(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
) * OUTPUT_SHOW_ALL
|
93 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
94 colors_enabled() * OUTPUT_COLOR
|
95 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
98 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
99 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
100 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
101 const char *k
, *v
, **query_res
= NULL
;
102 size_t count
= 0, awaited_args
= 0;
110 NULSTR_FOREACH(iter
, query
)
112 query_res
= newa0(const char *, awaited_args
);
114 r
= bus_call_method(bus
, bus_machine_mgr
, method
, &error
, &reply
, "s", name
);
116 return log_debug_errno(r
, "Failed to call '%s()': %s", method
, bus_error_message(&error
, r
));
118 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
120 return bus_log_parse_error(r
);
122 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
124 NULSTR_FOREACH(iter
, query
) {
125 if (streq(k
, iter
)) {
126 query_res
[count
] = v
;
133 return bus_log_parse_error(r
);
135 r
= sd_bus_message_exit_container(reply
);
137 return bus_log_parse_error(r
);
140 for (count
= 0; count
< awaited_args
; count
++) {
143 out
= va_arg(ap
, char **);
145 if (query_res
[count
]) {
146 val
= strdup(query_res
[count
]);
159 static int call_get_addresses(
167 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
168 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
169 _cleanup_free_
char *addresses
= NULL
;
178 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineAddresses", NULL
, &reply
, "s", name
);
180 return log_debug_errno(r
, "Could not get addresses: %s", bus_error_message(&error
, r
));
182 addresses
= strdup(prefix
);
187 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
189 return bus_log_parse_error(r
);
191 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
195 char buf_ifi
[1 + DECIMAL_STR_MAX(int)] = "";
197 r
= sd_bus_message_read(reply
, "i", &family
);
199 return bus_log_parse_error(r
);
201 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
203 return bus_log_parse_error(r
);
205 if (family
== AF_INET6
&& ifi
> 0)
206 xsprintf(buf_ifi
, "%%%i", ifi
);
208 if (!strextend(&addresses
, prefix
, IN_ADDR_TO_STRING(family
, a
), buf_ifi
))
211 r
= sd_bus_message_exit_container(reply
);
213 return bus_log_parse_error(r
);
220 return bus_log_parse_error(r
);
222 r
= sd_bus_message_exit_container(reply
);
224 return bus_log_parse_error(r
);
226 *ret
= TAKE_PTR(addresses
);
230 static int show_table(Table
*table
, const char *word
) {
236 if (table_get_rows(table
) > 1 || OUTPUT_MODE_IS_JSON(arg_output
)) {
237 r
= table_set_sort(table
, (size_t) 0);
239 return table_log_sort_error(r
);
241 table_set_header(table
, arg_legend
);
243 if (OUTPUT_MODE_IS_JSON(arg_output
))
244 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | JSON_FORMAT_COLOR_AUTO
);
246 r
= table_print(table
, NULL
);
248 return table_log_print_error(r
);
252 if (table_get_rows(table
) > 1)
253 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
255 printf("No %s.\n", word
);
261 static int list_machines(int argc
, char *argv
[], void *userdata
) {
262 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
263 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
264 _cleanup_(table_unrefp
) Table
*table
= NULL
;
265 sd_bus
*bus
= ASSERT_PTR(userdata
);
268 pager_open(arg_pager_flags
);
270 r
= bus_call_method(bus
, bus_machine_mgr
, "ListMachines", &error
, &reply
, NULL
);
272 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
274 table
= table_new("machine", "class", "service", "os", "version",
275 arg_max_addresses
> 0 ? "addresses" : NULL
);
279 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
280 if (!arg_full
&& arg_max_addresses
> 0 && arg_max_addresses
< UINT_MAX
)
281 table_set_cell_height_max(table
, arg_max_addresses
);
284 table_set_width(table
, 0);
286 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
288 return bus_log_parse_error(r
);
291 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
292 const char *name
, *class, *service
;
294 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
296 return bus_log_parse_error(r
);
300 if (name
[0] == '.' && !arg_all
)
303 (void) call_get_os_release(
305 "GetMachineOSRelease",
312 r
= table_add_many(table
,
313 TABLE_STRING
, empty_to_null(name
),
314 TABLE_STRING
, empty_to_null(class),
315 TABLE_STRING
, empty_to_null(service
),
316 TABLE_STRING
, empty_to_null(os
),
317 TABLE_STRING
, empty_to_null(version_id
));
319 return table_log_add_error(r
);
321 if (arg_max_addresses
> 0) {
322 (void) call_get_addresses(bus
, name
, 0, "", "\n", &addresses
);
324 r
= table_add_many(table
,
325 TABLE_STRING
, empty_to_null(addresses
));
327 return table_log_add_error(r
);
331 r
= sd_bus_message_exit_container(reply
);
333 return bus_log_parse_error(r
);
335 return show_table(table
, "machines");
338 static int list_images(int argc
, char *argv
[], void *userdata
) {
340 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
341 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
342 _cleanup_(table_unrefp
) Table
*table
= NULL
;
343 sd_bus
*bus
= ASSERT_PTR(userdata
);
346 pager_open(arg_pager_flags
);
348 r
= bus_call_method(bus
, bus_machine_mgr
, "ListImages", &error
, &reply
, NULL
);
350 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
352 table
= table_new("name", "type", "ro", "usage", "created", "modified");
357 table_set_width(table
, 0);
359 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
361 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
363 return bus_log_parse_error(r
);
366 uint64_t crtime
, mtime
, size
;
367 const char *name
, *type
;
370 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
372 return bus_log_parse_error(r
);
376 if (name
[0] == '.' && !arg_all
)
379 r
= table_add_many(table
,
382 TABLE_BOOLEAN
, ro_int
,
383 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
385 TABLE_TIMESTAMP
, crtime
,
386 TABLE_TIMESTAMP
, mtime
);
388 return table_log_add_error(r
);
391 r
= sd_bus_message_exit_container(reply
);
393 return bus_log_parse_error(r
);
395 return show_table(table
, "images");
398 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
399 _cleanup_free_
char *cgroup
= NULL
;
400 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
407 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
420 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
423 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
426 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
428 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
431 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
433 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
438 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
439 _cleanup_free_
char *pretty
= NULL
;
446 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
451 printf("%s%s\n", prefix
, pretty
);
456 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
457 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
458 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
465 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineUIDShift", &error
, &reply
, "s", name
);
467 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
469 r
= sd_bus_message_read(reply
, "u", &shift
);
473 if (shift
== 0) /* Don't show trivial mappings */
476 printf(" UID Shift: %" PRIu32
"\n", shift
);
480 typedef struct MachineStatusInfo
{
486 const char *root_directory
;
488 struct dual_timestamp timestamp
;
493 static void machine_status_info_clear(MachineStatusInfo
*info
) {
500 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
501 _cleanup_free_
char *addresses
= NULL
, *s1
= NULL
, *s2
= NULL
;
507 fputs(strna(i
->name
), stdout
);
509 if (!sd_id128_is_null(i
->id
))
510 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
514 s1
= strdup(strempty(FORMAT_TIMESTAMP_RELATIVE(i
->timestamp
.realtime
)));
515 s2
= strdup(strempty(FORMAT_TIMESTAMP(i
->timestamp
.realtime
)));
518 printf("\t Since: %s; %s\n", strna(s2
), s1
);
519 else if (!isempty(s2
))
520 printf("\t Since: %s\n", s2
);
523 _cleanup_free_
char *t
= NULL
;
525 printf("\t Leader: %u", (unsigned) i
->leader
);
527 (void) get_process_comm(i
->leader
, &t
);
535 printf("\t Service: %s", i
->service
);
538 printf("; class %s", i
->class);
542 printf("\t Class: %s\n", i
->class);
544 if (i
->root_directory
)
545 printf("\t Root: %s\n", i
->root_directory
);
547 if (i
->n_netif
> 0) {
548 fputs("\t Iface:", stdout
);
550 for (size_t c
= 0; c
< i
->n_netif
; c
++) {
551 char name
[IF_NAMESIZE
];
553 if (format_ifname(i
->netif
[c
], name
) >= 0) {
562 printf(" %i", i
->netif
[c
]);
568 if (call_get_addresses(bus
, i
->name
, ifi
,
569 "\t Address: ", "\n\t ",
571 fputs(addresses
, stdout
);
575 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
577 print_uid_shift(bus
, i
->name
);
580 printf("\t Unit: %s\n", i
->unit
);
581 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
583 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
585 show_journal_by_unit(
591 i
->timestamp
.monotonic
,
594 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
595 SD_JOURNAL_LOCAL_ONLY
,
601 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
602 MachineStatusInfo
*i
= userdata
;
607 assert_cc(sizeof(int32_t) == sizeof(int));
608 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
614 i
->n_netif
= l
/ sizeof(int32_t);
615 i
->netif
= memdup(v
, l
);
622 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
624 static const struct bus_properties_map map
[] = {
625 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
626 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
627 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
628 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
629 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
630 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
631 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
632 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
633 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
634 { "NetworkInterfaces", "ai", map_netif
, 0 },
638 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
639 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
640 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
648 r
= bus_map_all_properties(bus
,
649 "org.freedesktop.machine1",
657 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
663 print_machine_status_info(bus
, &info
);
668 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
680 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
682 log_error_errno(r
, "Could not get properties: %m");
687 static int show_machine(int argc
, char *argv
[], void *userdata
) {
689 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
690 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
691 bool properties
, new_line
= false;
692 sd_bus
*bus
= ASSERT_PTR(userdata
);
695 properties
= !strstr(argv
[0], "status");
697 pager_open(arg_pager_flags
);
699 if (properties
&& argc
<= 1) {
701 /* If no argument is specified, inspect the manager
703 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
708 for (int i
= 1; i
< argc
; i
++) {
709 const char *path
= NULL
;
711 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachine", &error
, &reply
, "s", argv
[i
]);
713 return log_error_errno(r
, "Could not get path to machine: %s", bus_error_message(&error
, r
));
715 r
= sd_bus_message_read(reply
, "o", &path
);
717 return bus_log_parse_error(r
);
720 r
= show_machine_properties(bus
, path
, &new_line
);
722 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
728 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
729 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
733 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageHostname", NULL
, &reply
, "s", name
);
737 r
= sd_bus_message_read(reply
, "s", &hn
);
742 printf("\tHostname: %s\n", hn
);
747 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
748 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
749 sd_id128_t id
= SD_ID128_NULL
;
754 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineID", NULL
, &reply
, "s", name
);
758 r
= sd_bus_message_read_array(reply
, 'y', &p
, &size
);
762 if (size
== sizeof(sd_id128_t
))
763 memcpy(&id
, p
, size
);
765 if (!sd_id128_is_null(id
))
766 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
771 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
772 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
775 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineInfo", NULL
, &reply
, "s", name
);
779 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
786 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
792 if (streq(p
, "DEPLOYMENT"))
793 printf(" Deployment: %s\n", q
);
796 r
= sd_bus_message_exit_container(reply
);
803 typedef struct ImageStatusInfo
{
812 uint64_t usage_exclusive
;
813 uint64_t limit_exclusive
;
816 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
821 fputs(i
->name
, stdout
);
826 printf("\t Type: %s\n", i
->type
);
829 printf("\t Path: %s\n", i
->path
);
831 (void) print_image_hostname(bus
, i
->name
);
832 (void) print_image_machine_id(bus
, i
->name
);
833 (void) print_image_machine_info(bus
, i
->name
);
835 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
837 printf("\t RO: %s%s%s\n",
838 i
->read_only
? ansi_highlight_red() : "",
839 i
->read_only
? "read-only" : "writable",
840 i
->read_only
? ansi_normal() : "");
842 if (timestamp_is_set(i
->crtime
))
843 printf("\t Created: %s; %s\n",
844 FORMAT_TIMESTAMP(i
->crtime
), FORMAT_TIMESTAMP_RELATIVE(i
->crtime
));
846 if (timestamp_is_set(i
->mtime
))
847 printf("\tModified: %s; %s\n",
848 FORMAT_TIMESTAMP(i
->mtime
), FORMAT_TIMESTAMP_RELATIVE(i
->mtime
));
850 if (i
->usage
!= UINT64_MAX
) {
851 if (i
->usage_exclusive
!= i
->usage
&& i
->usage_exclusive
!= UINT64_MAX
)
852 printf("\t Usage: %s (exclusive: %s)\n",
853 FORMAT_BYTES(i
->usage
), FORMAT_BYTES(i
->usage_exclusive
));
855 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
858 if (i
->limit
!= UINT64_MAX
) {
859 if (i
->limit_exclusive
!= i
->limit
&& i
->limit_exclusive
!= UINT64_MAX
)
860 printf("\t Limit: %s (exclusive: %s)\n",
861 FORMAT_BYTES(i
->limit
), FORMAT_BYTES(i
->limit_exclusive
));
863 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
867 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
869 static const struct bus_properties_map map
[] = {
870 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
871 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
872 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
873 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
874 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
875 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
876 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
877 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
878 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
879 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
883 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
884 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
885 ImageStatusInfo info
= {};
892 r
= bus_map_all_properties(bus
,
893 "org.freedesktop.machine1",
896 BUS_MAP_BOOLEAN_AS_BOOL
,
901 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
907 print_image_status_info(bus
, &info
);
912 typedef struct PoolStatusInfo
{
918 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
920 printf("\t Path: %s\n", i
->path
);
922 if (i
->usage
!= UINT64_MAX
)
923 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
925 if (i
->limit
!= UINT64_MAX
)
926 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
929 static int show_pool_info(sd_bus
*bus
) {
931 static const struct bus_properties_map map
[] = {
932 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
933 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
934 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
938 PoolStatusInfo info
= {
943 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
944 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
949 r
= bus_map_all_properties(bus
,
950 "org.freedesktop.machine1",
951 "/org/freedesktop/machine1",
958 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
960 print_pool_status_info(bus
, &info
);
965 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
977 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
979 log_error_errno(r
, "Could not get properties: %m");
984 static int show_image(int argc
, char *argv
[], void *userdata
) {
986 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
987 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
988 bool properties
, new_line
= false;
989 sd_bus
*bus
= ASSERT_PTR(userdata
);
992 properties
= !strstr(argv
[0], "status");
994 pager_open(arg_pager_flags
);
998 /* If no argument is specified, inspect the manager
1002 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1004 r
= show_pool_info(bus
);
1009 for (int i
= 1; i
< argc
; i
++) {
1010 const char *path
= NULL
;
1012 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, &reply
, "s", argv
[i
]);
1014 return log_error_errno(r
, "Could not get path to image: %s", bus_error_message(&error
, r
));
1016 r
= sd_bus_message_read(reply
, "o", &path
);
1018 return bus_log_parse_error(r
);
1021 r
= show_image_properties(bus
, path
, &new_line
);
1023 r
= show_image_info(bus
, path
, &new_line
);
1029 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1030 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1031 sd_bus
*bus
= ASSERT_PTR(userdata
);
1034 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1037 arg_kill_whom
= "all";
1039 for (int i
= 1; i
< argc
; i
++) {
1040 r
= bus_call_method(
1046 "ssi", argv
[i
], arg_kill_whom
, arg_signal
);
1048 return log_error_errno(r
, "Could not kill machine: %s", bus_error_message(&error
, r
));
1054 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1055 arg_kill_whom
= "leader";
1056 arg_signal
= SIGINT
; /* sysvinit + systemd */
1058 return kill_machine(argc
, argv
, userdata
);
1061 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1062 arg_kill_whom
= "leader";
1063 arg_signal
= SIGRTMIN
+4; /* only systemd */
1065 return kill_machine(argc
, argv
, userdata
);
1068 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1069 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1070 sd_bus
*bus
= ASSERT_PTR(userdata
);
1073 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1075 for (int i
= 1; i
< argc
; i
++) {
1076 r
= bus_call_method(bus
, bus_machine_mgr
, "TerminateMachine", &error
, NULL
, "s", argv
[i
]);
1078 return log_error_errno(r
, "Could not terminate machine: %s", bus_error_message(&error
, r
));
1084 static const char *select_copy_method(bool copy_from
, bool force
) {
1086 return copy_from
? "CopyFromMachineWithFlags" : "CopyToMachineWithFlags";
1088 return copy_from
? "CopyFromMachine" : "CopyToMachine";
1091 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1092 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1093 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1094 _cleanup_free_
char *abs_host_path
= NULL
;
1095 char *dest
, *host_path
, *container_path
;
1096 sd_bus
*bus
= ASSERT_PTR(userdata
);
1100 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1102 copy_from
= streq(argv
[0], "copy-from");
1103 dest
= argv
[3] ?: argv
[2];
1104 host_path
= copy_from
? dest
: argv
[2];
1105 container_path
= copy_from
? argv
[2] : dest
;
1107 if (!path_is_absolute(host_path
)) {
1108 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1110 return log_error_errno(r
, "Failed to make path absolute: %m");
1112 host_path
= abs_host_path
;
1115 r
= bus_message_new_method_call(
1119 select_copy_method(copy_from
, arg_force
));
1121 return bus_log_create_error(r
);
1123 r
= sd_bus_message_append(
1127 copy_from
? container_path
: host_path
,
1128 copy_from
? host_path
: container_path
);
1130 return bus_log_create_error(r
);
1133 r
= sd_bus_message_append(m
, "t", MACHINE_COPY_REPLACE
);
1135 return bus_log_create_error(r
);
1138 /* This is a slow operation, hence turn off any method call timeouts */
1139 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1141 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1146 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1147 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1148 sd_bus
*bus
= ASSERT_PTR(userdata
);
1151 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1153 r
= bus_call_method(
1166 return log_error_errno(r
, "Failed to bind mount: %s", bus_error_message(&error
, r
));
1171 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1172 PTYForward
** forward
= (PTYForward
**) userdata
;
1179 /* If the forwarder is already initialized, tell it to
1180 * exit on the next vhangup(), so that we still flush
1181 * out what might be queued and exit then. */
1183 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1187 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1190 /* On error, or when the forwarder is not initialized yet, quit immediately */
1191 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1195 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1201 assert(master
>= 0);
1204 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1207 if (streq(name
, ".host"))
1208 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1210 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1213 (void) sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1214 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1216 r
= pty_forward_new(event
, master
, flags
, forward
);
1218 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1220 r
= sd_event_loop(event
);
1222 return log_error_errno(r
, "Failed to run event loop: %m");
1224 pty_forward_get_last_char(*forward
, &last_char
);
1227 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1228 pty_forward_get_ignore_vhangup(*forward
) == 0;
1230 *forward
= pty_forward_free(*forward
);
1232 if (last_char
!= '\n')
1233 fputc('\n', stdout
);
1237 log_info("Machine %s terminated.", name
);
1238 else if (streq(name
, ".host"))
1239 log_info("Connection to the local host terminated.");
1241 log_info("Connection to machine %s terminated.", name
);
1247 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1249 * Whatever is specified in the spec takes priority over global arguments.
1252 const char *_machine
= NULL
;
1257 at
= strchr(spec
, '@');
1260 /* Do the same as ssh and refuse "@host". */
1264 _uid
= strndup(spec
, at
- spec
);
1271 if (arg_uid
&& !_uid
) {
1272 _uid
= strdup(arg_uid
);
1278 *machine
= isempty(_machine
) ? ".host" : _machine
;
1282 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1283 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1284 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1285 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1286 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1287 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1289 sd_bus
*bus
= ASSERT_PTR(userdata
);
1290 const char *match
, *machine
;
1292 if (!strv_isempty(arg_setenv
) || arg_uid
)
1293 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1294 "--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1296 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1297 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1298 "Login only supported on local machines.");
1300 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1302 r
= sd_event_default(&event
);
1304 return log_error_errno(r
, "Failed to get event loop: %m");
1306 r
= sd_bus_attach_event(bus
, event
, 0);
1308 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1310 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1312 match
= strjoina("type='signal',"
1313 "sender='org.freedesktop.machine1',"
1314 "path='/org/freedesktop/machine1',",
1315 "interface='org.freedesktop.machine1.Manager',"
1316 "member='MachineRemoved',"
1317 "arg0='", machine
, "'");
1319 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1321 return log_error_errno(r
, "Failed to request machine removal match: %m");
1323 r
= bus_call_method(bus
, bus_machine_mgr
, "OpenMachineLogin", &error
, &reply
, "s", machine
);
1325 return log_error_errno(r
, "Failed to get login PTY: %s", bus_error_message(&error
, r
));
1327 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1329 return bus_log_parse_error(r
);
1331 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1334 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1335 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1336 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1337 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1338 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1339 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1341 sd_bus
*bus
= ASSERT_PTR(userdata
);
1342 const char *match
, *machine
, *path
;
1343 _cleanup_free_
char *uid
= NULL
;
1345 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1346 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1347 "Shell only supported on local machines.");
1349 /* Pass $TERM to shell session, if not explicitly specified. */
1350 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1353 t
= strv_find_prefix(environ
, "TERM=");
1355 if (strv_extend(&arg_setenv
, t
) < 0)
1360 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1362 r
= sd_event_default(&event
);
1364 return log_error_errno(r
, "Failed to get event loop: %m");
1366 r
= sd_bus_attach_event(bus
, event
, 0);
1368 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1370 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1372 return log_error_errno(r
, "Failed to parse machine specification: %m");
1374 match
= strjoina("type='signal',"
1375 "sender='org.freedesktop.machine1',"
1376 "path='/org/freedesktop/machine1',",
1377 "interface='org.freedesktop.machine1.Manager',"
1378 "member='MachineRemoved',"
1379 "arg0='", machine
, "'");
1381 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1383 return log_error_errno(r
, "Failed to request machine removal match: %m");
1385 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "OpenMachineShell");
1387 return bus_log_create_error(r
);
1389 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1391 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1393 return bus_log_create_error(r
);
1395 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1397 return bus_log_create_error(r
);
1399 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1401 return bus_log_create_error(r
);
1403 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1405 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, r
));
1407 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1409 return bus_log_parse_error(r
);
1411 return process_forward(event
, &forward
, master
, 0, machine
);
1414 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1415 sd_bus
*bus
= ASSERT_PTR(userdata
);
1418 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1420 for (int i
= 1; i
< argc
; i
++) {
1421 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1422 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1424 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "RemoveImage");
1426 return bus_log_create_error(r
);
1428 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1430 return bus_log_create_error(r
);
1432 /* This is a slow operation, hence turn off any method call timeouts */
1433 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1435 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1441 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1442 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1443 sd_bus
*bus
= ASSERT_PTR(userdata
);
1446 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1448 r
= bus_call_method(
1454 "ss", argv
[1], argv
[2]);
1456 return log_error_errno(r
, "Could not rename image: %s", bus_error_message(&error
, r
));
1461 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1462 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1463 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1464 sd_bus
*bus
= ASSERT_PTR(userdata
);
1467 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1469 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CloneImage");
1471 return bus_log_create_error(r
);
1473 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1475 return bus_log_create_error(r
);
1477 /* This is a slow operation, hence turn off any method call timeouts */
1478 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1480 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1485 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1486 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1487 sd_bus
*bus
= ASSERT_PTR(userdata
);
1491 b
= parse_boolean(argv
[2]);
1493 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1494 "Failed to parse boolean argument: %s",
1498 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1500 r
= bus_call_method(bus
, bus_machine_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1502 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1507 static int image_exists(sd_bus
*bus
, const char *name
) {
1508 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1514 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, NULL
, "s", name
);
1516 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1519 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, r
));
1525 static int make_service_name(const char *name
, char **ret
) {
1531 if (!hostname_is_valid(name
, 0))
1532 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1533 "Invalid machine name %s.", name
);
1535 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1537 return log_error_errno(r
, "Failed to build unit name: %m");
1542 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1543 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1544 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1545 sd_bus
*bus
= ASSERT_PTR(userdata
);
1548 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1549 ask_password_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1551 r
= bus_wait_for_jobs_new(bus
, &w
);
1555 for (int i
= 1; i
< argc
; i
++) {
1556 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1557 _cleanup_free_
char *unit
= NULL
;
1560 r
= make_service_name(argv
[i
], &unit
);
1564 r
= image_exists(bus
, argv
[i
]);
1568 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1569 "Machine image '%s' does not exist.",
1572 r
= bus_call_method(
1578 "ss", unit
, "fail");
1580 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, r
));
1582 r
= sd_bus_message_read(reply
, "o", &object
);
1584 return bus_log_parse_error(r
);
1586 r
= bus_wait_for_jobs_add(w
, object
);
1591 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1598 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1599 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1600 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1601 InstallChange
*changes
= NULL
;
1602 size_t n_changes
= 0;
1603 const char *method
= NULL
;
1604 sd_bus
*bus
= ASSERT_PTR(userdata
);
1607 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1609 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1611 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, method
);
1613 return bus_log_create_error(r
);
1615 r
= sd_bus_message_open_container(m
, 'a', "s");
1617 return bus_log_create_error(r
);
1619 for (int i
= 1; i
< argc
; i
++) {
1620 _cleanup_free_
char *unit
= NULL
;
1622 r
= make_service_name(argv
[i
], &unit
);
1626 r
= image_exists(bus
, argv
[i
]);
1630 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1631 "Machine image '%s' does not exist.",
1634 r
= sd_bus_message_append(m
, "s", unit
);
1636 return bus_log_create_error(r
);
1639 r
= sd_bus_message_close_container(m
);
1641 return bus_log_create_error(r
);
1643 if (streq(argv
[0], "enable"))
1644 r
= sd_bus_message_append(m
, "bb", false, false);
1646 r
= sd_bus_message_append(m
, "b", false);
1648 return bus_log_create_error(r
);
1650 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1652 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, r
));
1654 if (streq(argv
[0], "enable")) {
1655 r
= sd_bus_message_read(reply
, "b", NULL
);
1657 return bus_log_parse_error(r
);
1660 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1664 r
= bus_call_method(bus
, bus_systemd_mgr
, "Reload", &error
, NULL
, NULL
);
1666 log_error("Failed to reload daemon: %s", bus_error_message(&error
, r
));
1673 install_changes_free(changes
, n_changes
);
1678 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1679 const char **our_path
= userdata
, *line
;
1686 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1688 bus_log_parse_error(r
);
1692 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1695 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1698 log_full(priority
, "%s", line
);
1702 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1703 const char **our_path
= userdata
, *path
, *result
;
1710 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1712 bus_log_parse_error(r
);
1716 if (!streq_ptr(*our_path
, path
))
1719 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1723 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1728 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1730 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1734 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1735 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1736 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1737 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1738 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1739 const char *path
= NULL
;
1746 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1748 r
= sd_event_default(&event
);
1750 return log_error_errno(r
, "Failed to get event loop: %m");
1752 r
= sd_bus_attach_event(bus
, event
, 0);
1754 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1756 r
= bus_match_signal_async(
1761 match_transfer_removed
, NULL
, &path
);
1763 return log_error_errno(r
, "Failed to request match: %m");
1765 r
= sd_bus_match_signal_async(
1768 "org.freedesktop.import1",
1770 "org.freedesktop.import1.Transfer",
1772 match_log_message
, NULL
, &path
);
1774 return log_error_errno(r
, "Failed to request match: %m");
1776 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1778 return log_error_errno(r
, "Failed to transfer image: %s", bus_error_message(&error
, r
));
1780 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1782 return bus_log_parse_error(r
);
1784 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1787 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1789 (void) sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1790 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1792 r
= sd_event_loop(event
);
1794 return log_error_errno(r
, "Failed to run event loop: %m");
1799 static int import_tar(int argc
, char *argv
[], void *userdata
) {
1800 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1801 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1802 const char *local
= NULL
, *path
= NULL
;
1803 _cleanup_close_
int fd
= -EBADF
;
1804 sd_bus
*bus
= ASSERT_PTR(userdata
);
1808 path
= empty_or_dash_to_null(argv
[1]);
1811 local
= empty_or_dash_to_null(argv
[2]);
1813 r
= path_extract_filename(path
, &fn
);
1815 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1816 if (r
== O_DIRECTORY
)
1817 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1818 "Path '%s' refers to directory, but we need a regular file: %m", path
);
1823 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1824 "Need either path or local name.");
1826 r
= tar_strip_suffixes(local
, &ll
);
1832 if (!hostname_is_valid(local
, 0))
1833 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1834 "Local name %s is not a suitable machine name.",
1838 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1840 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1843 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportTar");
1845 return bus_log_create_error(r
);
1847 r
= sd_bus_message_append(
1850 fd
>= 0 ? fd
: STDIN_FILENO
,
1855 return bus_log_create_error(r
);
1857 return transfer_image_common(bus
, m
);
1860 static int import_raw(int argc
, char *argv
[], void *userdata
) {
1861 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1862 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1863 const char *local
= NULL
, *path
= NULL
;
1864 _cleanup_close_
int fd
= -EBADF
;
1865 sd_bus
*bus
= ASSERT_PTR(userdata
);
1869 path
= empty_or_dash_to_null(argv
[1]);
1872 local
= empty_or_dash_to_null(argv
[2]);
1874 r
= path_extract_filename(path
, &fn
);
1876 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1877 if (r
== O_DIRECTORY
)
1878 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1879 "Path '%s' refers to directory, but we need a regular file: %m", path
);
1884 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1885 "Need either path or local name.");
1887 r
= raw_strip_suffixes(local
, &ll
);
1893 if (!hostname_is_valid(local
, 0))
1894 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1895 "Local name %s is not a suitable machine name.",
1899 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1901 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1904 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportRaw");
1906 return bus_log_create_error(r
);
1908 r
= sd_bus_message_append(
1911 fd
>= 0 ? fd
: STDIN_FILENO
,
1916 return bus_log_create_error(r
);
1918 return transfer_image_common(bus
, m
);
1921 static int import_fs(int argc
, char *argv
[], void *userdata
) {
1922 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1923 const char *local
= NULL
, *path
= NULL
;
1924 _cleanup_free_
char *fn
= NULL
;
1925 _cleanup_close_
int fd
= -EBADF
;
1926 sd_bus
*bus
= ASSERT_PTR(userdata
);
1930 path
= empty_or_dash_to_null(argv
[1]);
1933 local
= empty_or_dash_to_null(argv
[2]);
1935 r
= path_extract_filename(path
, &fn
);
1937 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1942 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1943 "Need either path or local name.");
1945 if (!hostname_is_valid(local
, 0))
1946 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1947 "Local name %s is not a suitable machine name.",
1951 fd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
1953 return log_error_errno(errno
, "Failed to open directory '%s': %m", path
);
1956 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportFileSystem");
1958 return bus_log_create_error(r
);
1960 r
= sd_bus_message_append(
1963 fd
>= 0 ? fd
: STDIN_FILENO
,
1968 return bus_log_create_error(r
);
1970 return transfer_image_common(bus
, m
);
1973 static void determine_compression_from_filename(const char *p
) {
1980 if (endswith(p
, ".xz"))
1982 else if (endswith(p
, ".gz"))
1983 arg_format
= "gzip";
1984 else if (endswith(p
, ".bz2"))
1985 arg_format
= "bzip2";
1988 static int export_tar(int argc
, char *argv
[], void *userdata
) {
1989 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1990 _cleanup_close_
int fd
= -EBADF
;
1991 const char *local
= NULL
, *path
= NULL
;
1992 sd_bus
*bus
= ASSERT_PTR(userdata
);
1996 if (!hostname_is_valid(local
, 0))
1997 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1998 "Machine name %s is not valid.", local
);
2002 path
= empty_or_dash_to_null(path
);
2005 determine_compression_from_filename(path
);
2007 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2009 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2012 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ExportTar");
2014 return bus_log_create_error(r
);
2016 r
= sd_bus_message_append(
2020 fd
>= 0 ? fd
: STDOUT_FILENO
,
2023 return bus_log_create_error(r
);
2025 return transfer_image_common(bus
, m
);
2028 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2029 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2030 _cleanup_close_
int fd
= -EBADF
;
2031 const char *local
= NULL
, *path
= NULL
;
2032 sd_bus
*bus
= ASSERT_PTR(userdata
);
2036 if (!hostname_is_valid(local
, 0))
2037 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2038 "Machine name %s is not valid.", local
);
2042 path
= empty_or_dash_to_null(path
);
2045 determine_compression_from_filename(path
);
2047 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2049 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2052 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ExportRaw");
2054 return bus_log_create_error(r
);
2056 r
= sd_bus_message_append(
2060 fd
>= 0 ? fd
: STDOUT_FILENO
,
2063 return bus_log_create_error(r
);
2065 return transfer_image_common(bus
, m
);
2068 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2069 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2070 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2071 const char *local
, *remote
;
2072 sd_bus
*bus
= ASSERT_PTR(userdata
);
2076 if (!http_url_is_valid(remote
) && !file_url_is_valid(remote
))
2077 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2078 "URL '%s' is not valid.", remote
);
2083 r
= import_url_last_component(remote
, &l
);
2085 return log_error_errno(r
, "Failed to get final component of URL: %m");
2090 local
= empty_or_dash_to_null(local
);
2093 r
= tar_strip_suffixes(local
, &ll
);
2099 if (!hostname_is_valid(local
, 0))
2100 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2101 "Local name %s is not a suitable machine name.",
2105 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "PullTar");
2107 return bus_log_create_error(r
);
2109 r
= sd_bus_message_append(
2114 import_verify_to_string(arg_verify
),
2117 return bus_log_create_error(r
);
2119 return transfer_image_common(bus
, m
);
2122 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2123 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2124 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2125 const char *local
, *remote
;
2126 sd_bus
*bus
= ASSERT_PTR(userdata
);
2130 if (!http_url_is_valid(remote
) && !file_url_is_valid(remote
))
2131 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2132 "URL '%s' is not valid.", remote
);
2137 r
= import_url_last_component(remote
, &l
);
2139 return log_error_errno(r
, "Failed to get final component of URL: %m");
2144 local
= empty_or_dash_to_null(local
);
2147 r
= raw_strip_suffixes(local
, &ll
);
2153 if (!hostname_is_valid(local
, 0))
2154 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2155 "Local name %s is not a suitable machine name.",
2159 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "PullRaw");
2161 return bus_log_create_error(r
);
2163 r
= sd_bus_message_append(
2168 import_verify_to_string(arg_verify
),
2171 return bus_log_create_error(r
);
2173 return transfer_image_common(bus
, m
);
2176 typedef struct TransferInfo
{
2184 static int compare_transfer_info(const TransferInfo
*a
, const TransferInfo
*b
) {
2185 return strcmp(a
->local
, b
->local
);
2188 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2189 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2190 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2191 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2192 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2193 const char *type
, *remote
, *local
;
2194 sd_bus
*bus
= userdata
;
2195 uint32_t id
, max_id
= 0;
2196 size_t n_transfers
= 0;
2200 pager_open(arg_pager_flags
);
2202 r
= bus_call_method(bus
, bus_import_mgr
, "ListTransfers", &error
, &reply
, NULL
);
2204 return log_error_errno(r
, "Could not get transfers: %s", bus_error_message(&error
, r
));
2206 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2208 return bus_log_parse_error(r
);
2210 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2213 if (!GREEDY_REALLOC(transfers
, n_transfers
+ 1))
2216 transfers
[n_transfers
].id
= id
;
2217 transfers
[n_transfers
].type
= type
;
2218 transfers
[n_transfers
].remote
= remote
;
2219 transfers
[n_transfers
].local
= local
;
2220 transfers
[n_transfers
].progress
= progress
;
2240 return bus_log_parse_error(r
);
2242 r
= sd_bus_message_exit_container(reply
);
2244 return bus_log_parse_error(r
);
2246 typesafe_qsort(transfers
, n_transfers
, compare_transfer_info
);
2248 if (arg_legend
&& n_transfers
> 0)
2249 printf("%-*s %-*s %-*s %-*s %-*s\n",
2250 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2252 (int) max_type
, "TYPE",
2253 (int) max_local
, "LOCAL",
2254 (int) max_remote
, "REMOTE");
2256 for (size_t j
= 0; j
< n_transfers
; j
++)
2258 if (transfers
[j
].progress
< 0)
2259 printf("%*" PRIu32
" %*s %-*s %-*s %-*s\n",
2260 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2262 (int) max_type
, transfers
[j
].type
,
2263 (int) max_local
, transfers
[j
].local
,
2264 (int) max_remote
, transfers
[j
].remote
);
2266 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2267 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2268 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2269 (int) max_type
, transfers
[j
].type
,
2270 (int) max_local
, transfers
[j
].local
,
2271 (int) max_remote
, transfers
[j
].remote
);
2274 if (n_transfers
> 0)
2275 printf("\n%zu transfers listed.\n", n_transfers
);
2277 printf("No transfers.\n");
2283 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2284 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2285 sd_bus
*bus
= ASSERT_PTR(userdata
);
2288 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2290 for (int i
= 1; i
< argc
; i
++) {
2293 r
= safe_atou32(argv
[i
], &id
);
2295 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2297 r
= bus_call_method(bus
, bus_import_mgr
, "CancelTransfer", &error
, NULL
, "u", id
);
2299 return log_error_errno(r
, "Could not cancel transfer: %s", bus_error_message(&error
, r
));
2305 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2306 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2307 sd_bus
*bus
= userdata
;
2311 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2313 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2316 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2318 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2322 /* With two arguments changes the quota limit of the
2323 * specified image */
2324 r
= bus_call_method(bus
, bus_machine_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
2326 /* With one argument changes the pool quota limit */
2327 r
= bus_call_method(bus
, bus_machine_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
2330 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2335 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2336 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2337 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2338 uint64_t usage
, total
= 0;
2339 sd_bus
*bus
= userdata
;
2344 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2346 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CleanPool");
2348 return bus_log_create_error(r
);
2350 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2352 return bus_log_create_error(r
);
2354 /* This is a slow operation, hence permit a longer time for completion. */
2355 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2357 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2359 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2361 return bus_log_parse_error(r
);
2363 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2364 if (usage
== UINT64_MAX
) {
2365 log_info("Removed image '%s'", name
);
2368 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2369 name
, FORMAT_BYTES(usage
));
2370 if (total
!= UINT64_MAX
)
2376 r
= sd_bus_message_exit_container(reply
);
2378 return bus_log_parse_error(r
);
2380 if (total
== UINT64_MAX
)
2381 log_info("Removed %u images in total.", c
);
2383 log_info("Removed %u images in total. Total freed exclusive disk space: %s.",
2384 c
, FORMAT_BYTES(total
));
2389 static int help(int argc
, char *argv
[], void *userdata
) {
2390 _cleanup_free_
char *link
= NULL
;
2393 pager_open(arg_pager_flags
);
2395 r
= terminal_urlify_man("machinectl", "1", &link
);
2399 printf("%s [OPTIONS...] COMMAND ...\n\n"
2400 "%sSend control commands to or query the virtual machine and container%s\n"
2401 "%sregistration manager.%s\n"
2402 "\nMachine Commands:\n"
2403 " list List running VMs and containers\n"
2404 " status NAME... Show VM/container details\n"
2405 " show [NAME...] Show properties of one or more VMs/containers\n"
2406 " start NAME... Start container as a service\n"
2407 " login [NAME] Get a login prompt in a container or on the\n"
2409 " shell [[USER@]NAME [COMMAND...]]\n"
2410 " Invoke a shell (or other command) in a container\n"
2411 " or on the local host\n"
2412 " enable NAME... Enable automatic container start at boot\n"
2413 " disable NAME... Disable automatic container start at boot\n"
2414 " poweroff NAME... Power off one or more containers\n"
2415 " reboot NAME... Reboot one or more containers\n"
2416 " terminate NAME... Terminate one or more VMs/containers\n"
2417 " kill NAME... Send signal to processes of a VM/container\n"
2418 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2419 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2420 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2422 " list-images Show available container and VM images\n"
2423 " image-status [NAME...] Show image details\n"
2424 " show-image [NAME...] Show properties of image\n"
2425 " clone NAME NAME Clone an image\n"
2426 " rename NAME NAME Rename an image\n"
2427 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2428 " remove NAME... Remove an image\n"
2429 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2430 " clean Remove hidden (or all) images\n\n"
2431 "Image Transfer Commands:\n"
2432 " pull-tar URL [NAME] Download a TAR container image\n"
2433 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2434 " import-tar FILE [NAME] Import a local TAR container image\n"
2435 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2436 " import-fs DIRECTORY [NAME] Import a local directory container image\n"
2437 " export-tar NAME [FILE] Export a TAR container image locally\n"
2438 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2439 " list-transfers Show list of downloads in progress\n"
2440 " cancel-transfer Cancel a download\n"
2442 " -h --help Show this help\n"
2443 " --version Show package version\n"
2444 " --no-pager Do not pipe output into a pager\n"
2445 " --no-legend Do not show the headers and footers\n"
2446 " --no-ask-password Do not ask for system passwords\n"
2447 " -H --host=[USER@]HOST Operate on remote host\n"
2448 " -M --machine=CONTAINER Operate on local container\n"
2449 " -p --property=NAME Show only properties by this name\n"
2450 " -q --quiet Suppress output\n"
2451 " -a --all Show all properties, including empty ones\n"
2452 " --value When showing properties, only print the value\n"
2453 " -l --full Do not ellipsize output\n"
2454 " --kill-whom=WHOM Whom to send signal to\n"
2455 " -s --signal=SIGNAL Which signal to send\n"
2456 " --uid=USER Specify user ID to invoke shell as\n"
2457 " -E --setenv=VAR[=VALUE] Add an environment variable for shell\n"
2458 " --read-only Create read-only bind mount\n"
2459 " --mkdir Create directory before bind mounting, if missing\n"
2460 " -n --lines=INTEGER Number of journal entries to show\n"
2461 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2462 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2463 " short-iso, short-iso-precise, short-full,\n"
2464 " short-monotonic, short-unix, short-delta,\n"
2465 " json, json-pretty, json-sse, json-seq, cat,\n"
2466 " verbose, export, with-unit)\n"
2467 " --verify=MODE Verification mode for downloaded images (no,\n"
2468 " checksum, signature)\n"
2469 " --force Download image even if already exists\n"
2470 "\nSee the %s for details.\n",
2471 program_invocation_short_name
,
2481 static int parse_argv(int argc
, char *argv
[]) {
2484 ARG_VERSION
= 0x100,
2491 ARG_NO_ASK_PASSWORD
,
2499 static const struct option options
[] = {
2500 { "help", no_argument
, NULL
, 'h' },
2501 { "version", no_argument
, NULL
, ARG_VERSION
},
2502 { "property", required_argument
, NULL
, 'p' },
2503 { "all", no_argument
, NULL
, 'a' },
2504 { "value", no_argument
, NULL
, ARG_VALUE
},
2505 { "full", no_argument
, NULL
, 'l' },
2506 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2507 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2508 { "kill-whom", required_argument
, NULL
, ARG_KILL_WHOM
},
2509 { "signal", required_argument
, NULL
, 's' },
2510 { "host", required_argument
, NULL
, 'H' },
2511 { "machine", required_argument
, NULL
, 'M' },
2512 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2513 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2514 { "quiet", no_argument
, NULL
, 'q' },
2515 { "lines", required_argument
, NULL
, 'n' },
2516 { "output", required_argument
, NULL
, 'o' },
2517 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2518 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2519 { "force", no_argument
, NULL
, ARG_FORCE
},
2520 { "format", required_argument
, NULL
, ARG_FORMAT
},
2521 { "uid", required_argument
, NULL
, ARG_UID
},
2522 { "setenv", required_argument
, NULL
, 'E' },
2523 { "max-addresses", required_argument
, NULL
, ARG_MAX_ADDRESSES
},
2527 bool reorder
= false;
2528 int c
, r
, shell
= -1;
2534 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2536 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2542 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2543 * non-option argument was discovered. */
2547 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2548 * for switches after the main verb. However, for "shell" we really don't want that, since we
2549 * want that switches specified after the machine name are passed to the program to execute,
2550 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2551 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2552 * non-option parameter. If it's the verb "shell" we remember its position and continue
2553 * processing options. In this case, as soon as we hit the next non-option argument we found
2554 * the machine name, and stop further processing. If the first non-option argument is any other
2555 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2559 /* If we already found the "shell" verb on the command line, and now found the next
2560 * non-option argument, then this is the machine name and we should stop processing
2561 * further arguments. */
2562 optind
--; /* don't process this argument, go one step back */
2565 if (streq(optarg
, "shell"))
2566 /* Remember the position of the "shell" verb, and continue processing normally. */
2571 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2572 * processing normally. */
2575 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2576 * at least once with a reset option index. Hence, let's reset the option index here,
2577 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2578 * likely already processed it), and the bump the option index so that we read the
2579 * intended argument again. */
2580 saved_optind
= optind
;
2582 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2583 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2589 return help(0, NULL
, NULL
);
2595 r
= strv_extend(&arg_property
, optarg
);
2599 /* If the user asked for a particular
2600 * property, show it to them, even if it is
2602 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2606 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2611 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
2619 if (safe_atou(optarg
, &arg_lines
) < 0)
2620 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2621 "Failed to parse lines '%s'", optarg
);
2625 if (streq(optarg
, "help")) {
2626 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2630 r
= output_mode_from_string(optarg
);
2632 return log_error_errno(r
, "Unknown output '%s'.", optarg
);
2635 if (OUTPUT_MODE_IS_JSON(arg_output
))
2640 arg_pager_flags
|= PAGER_DISABLE
;
2648 arg_kill_whom
= optarg
;
2652 r
= parse_signal_argument(optarg
, &arg_signal
);
2657 case ARG_NO_ASK_PASSWORD
:
2658 arg_ask_password
= false;
2662 arg_transport
= BUS_TRANSPORT_REMOTE
;
2667 arg_transport
= BUS_TRANSPORT_MACHINE
;
2672 arg_read_only
= true;
2684 if (streq(optarg
, "help")) {
2685 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2689 r
= import_verify_from_string(optarg
);
2691 return log_error_errno(r
, "Failed to parse --verify= setting: %s", optarg
);
2700 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2"))
2701 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2702 "Unknown format: %s", optarg
);
2704 arg_format
= optarg
;
2712 r
= strv_env_replace_strdup_passthrough(&arg_setenv
, optarg
);
2714 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
2717 case ARG_MAX_ADDRESSES
:
2718 if (streq(optarg
, "all"))
2719 arg_max_addresses
= UINT_MAX
;
2720 else if (safe_atou(optarg
, &arg_max_addresses
) < 0)
2721 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2722 "Invalid number of addresses: %s", optarg
);
2729 assert_not_reached();
2737 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2738 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2740 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2743 for (int i
= shell
; i
< optind
; i
++)
2744 argv
[i
] = argv
[i
+1];
2751 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2753 static const Verb verbs
[] = {
2754 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2755 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2756 { "list-images", VERB_ANY
, 1, 0, list_images
},
2757 { "status", 2, VERB_ANY
, 0, show_machine
},
2758 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2759 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2760 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
2761 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
2762 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
2763 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
2764 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
2765 { "kill", 2, VERB_ANY
, 0, kill_machine
},
2766 { "login", VERB_ANY
, 2, 0, login_machine
},
2767 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
2768 { "bind", 3, 4, 0, bind_mount
},
2769 { "copy-to", 3, 4, 0, copy_files
},
2770 { "copy-from", 3, 4, 0, copy_files
},
2771 { "remove", 2, VERB_ANY
, 0, remove_image
},
2772 { "rename", 3, 3, 0, rename_image
},
2773 { "clone", 3, 3, 0, clone_image
},
2774 { "read-only", 2, 3, 0, read_only_image
},
2775 { "start", 2, VERB_ANY
, 0, start_machine
},
2776 { "enable", 2, VERB_ANY
, 0, enable_machine
},
2777 { "disable", 2, VERB_ANY
, 0, enable_machine
},
2778 { "import-tar", 2, 3, 0, import_tar
},
2779 { "import-raw", 2, 3, 0, import_raw
},
2780 { "import-fs", 2, 3, 0, import_fs
},
2781 { "export-tar", 2, 3, 0, export_tar
},
2782 { "export-raw", 2, 3, 0, export_raw
},
2783 { "pull-tar", 2, 3, 0, pull_tar
},
2784 { "pull-raw", 2, 3, 0, pull_raw
},
2785 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
2786 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
2787 { "set-limit", 2, 3, 0, set_limit
},
2788 { "clean", VERB_ANY
, 1, 0, clean_images
},
2792 return dispatch_verb(argc
, argv
, verbs
, bus
);
2795 static int run(int argc
, char *argv
[]) {
2796 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2799 setlocale(LC_ALL
, "");
2802 /* The journal merging logic potentially needs a lot of fds. */
2803 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
2807 r
= parse_argv(argc
, argv
);
2811 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
2813 return bus_log_connect_error(r
, arg_transport
);
2815 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
2817 return machinectl_main(argc
, argv
, bus
);
2820 DEFINE_MAIN_FUNCTION(run
);