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"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
19 #include "bus-locator.h"
20 #include "bus-map-properties.h"
21 #include "bus-print-properties.h"
22 #include "bus-unit-procs.h"
23 #include "bus-unit-util.h"
24 #include "bus-wait-for-jobs.h"
25 #include "cgroup-show.h"
26 #include "cgroup-util.h"
31 #include "format-table.h"
32 #include "hostname-util.h"
33 #include "import-util.h"
34 #include "locale-util.h"
36 #include "logs-show.h"
37 #include "machine-dbus.h"
39 #include "main-func.h"
41 #include "nulstr-util.h"
43 #include "parse-argument.h"
44 #include "parse-util.h"
45 #include "path-util.h"
46 #include "pretty-print.h"
47 #include "process-util.h"
49 #include "rlimit-util.h"
51 #include "signal-util.h"
52 #include "sort-util.h"
53 #include "spawn-ask-password-agent.h"
54 #include "spawn-polkit-agent.h"
55 #include "stdio-util.h"
56 #include "string-table.h"
58 #include "terminal-util.h"
59 #include "unit-name.h"
63 #define ALL_ADDRESSES -1
65 static char **arg_property
= NULL
;
66 static bool arg_all
= false;
67 static BusPrintPropertyFlags arg_print_flags
= 0;
68 static bool arg_full
= false;
69 static PagerFlags arg_pager_flags
= 0;
70 static bool arg_legend
= true;
71 static const char *arg_kill_who
= NULL
;
72 static int arg_signal
= SIGTERM
;
73 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
74 static const char *arg_host
= NULL
;
75 static bool arg_read_only
= false;
76 static bool arg_mkdir
= false;
77 static bool arg_quiet
= false;
78 static bool arg_ask_password
= true;
79 static unsigned arg_lines
= 10;
80 static OutputMode arg_output
= OUTPUT_SHORT
;
81 static bool arg_force
= false;
82 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
83 static const char* arg_format
= NULL
;
84 static const char *arg_uid
= NULL
;
85 static char **arg_setenv
= NULL
;
86 static int arg_max_addresses
= 1;
88 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
89 STATIC_DESTRUCTOR_REGISTER(arg_setenv
, strv_freep
);
91 static OutputFlags
get_output_flags(void) {
93 FLAGS_SET(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
) * OUTPUT_SHOW_ALL
|
94 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
95 colors_enabled() * OUTPUT_COLOR
|
96 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
99 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
100 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
101 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
102 const char *k
, *v
, *iter
, **query_res
= NULL
;
103 size_t count
= 0, awaited_args
= 0;
111 NULSTR_FOREACH(iter
, query
)
113 query_res
= newa0(const char *, awaited_args
);
115 r
= bus_call_method(bus
, bus_machine_mgr
, method
, &error
, &reply
, "s", name
);
117 return log_debug_errno(r
, "Failed to call '%s()': %s", method
, bus_error_message(&error
, r
));
119 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
121 return bus_log_parse_error(r
);
123 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
125 NULSTR_FOREACH(iter
, query
) {
126 if (streq(k
, iter
)) {
127 query_res
[count
] = v
;
134 return bus_log_parse_error(r
);
136 r
= sd_bus_message_exit_container(reply
);
138 return bus_log_parse_error(r
);
141 for (count
= 0; count
< awaited_args
; count
++) {
144 out
= va_arg(ap
, char **);
146 if (query_res
[count
]) {
147 val
= strdup(query_res
[count
]);
160 static int call_get_addresses(
168 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
169 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
170 _cleanup_free_
char *addresses
= NULL
;
179 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineAddresses", NULL
, &reply
, "s", name
);
181 return log_debug_errno(r
, "Could not get addresses: %s", bus_error_message(&error
, r
));
183 addresses
= strdup(prefix
);
188 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
190 return bus_log_parse_error(r
);
192 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
196 char buf_ifi
[1 + DECIMAL_STR_MAX(int)] = "";
198 r
= sd_bus_message_read(reply
, "i", &family
);
200 return bus_log_parse_error(r
);
202 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
204 return bus_log_parse_error(r
);
206 if (family
== AF_INET6
&& ifi
> 0)
207 xsprintf(buf_ifi
, "%%%i", ifi
);
209 if (!strextend(&addresses
, prefix
, IN_ADDR_TO_STRING(family
, a
), buf_ifi
))
212 r
= sd_bus_message_exit_container(reply
);
214 return bus_log_parse_error(r
);
221 return bus_log_parse_error(r
);
223 r
= sd_bus_message_exit_container(reply
);
225 return bus_log_parse_error(r
);
227 *ret
= TAKE_PTR(addresses
);
231 static int show_table(Table
*table
, const char *word
) {
237 if (table_get_rows(table
) > 1 || OUTPUT_MODE_IS_JSON(arg_output
)) {
238 r
= table_set_sort(table
, (size_t) 0);
240 return table_log_sort_error(r
);
242 table_set_header(table
, arg_legend
);
244 if (OUTPUT_MODE_IS_JSON(arg_output
))
245 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | JSON_FORMAT_COLOR_AUTO
);
247 r
= table_print(table
, NULL
);
249 return table_log_print_error(r
);
253 if (table_get_rows(table
) > 1)
254 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
256 printf("No %s.\n", word
);
262 static int list_machines(int argc
, char *argv
[], void *userdata
) {
264 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
265 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
266 _cleanup_(table_unrefp
) Table
*table
= NULL
;
267 sd_bus
*bus
= userdata
;
272 pager_open(arg_pager_flags
);
274 r
= bus_call_method(bus
, bus_machine_mgr
, "ListMachines", &error
, &reply
, NULL
);
276 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
278 table
= table_new("machine", "class", "service", "os", "version", "addresses");
282 table_set_empty_string(table
, "-");
283 if (!arg_full
&& arg_max_addresses
!= ALL_ADDRESSES
)
284 table_set_cell_height_max(table
, arg_max_addresses
);
287 table_set_width(table
, 0);
289 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
291 return bus_log_parse_error(r
);
294 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
295 const char *name
, *class, *service
;
297 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
299 return bus_log_parse_error(r
);
303 if (name
[0] == '.' && !arg_all
)
306 (void) call_get_os_release(
308 "GetMachineOSRelease",
315 (void) call_get_addresses(
323 r
= table_add_many(table
,
324 TABLE_STRING
, empty_to_null(name
),
325 TABLE_STRING
, empty_to_null(class),
326 TABLE_STRING
, empty_to_null(service
),
327 TABLE_STRING
, empty_to_null(os
),
328 TABLE_STRING
, empty_to_null(version_id
),
329 TABLE_STRING
, empty_to_null(addresses
));
331 return table_log_add_error(r
);
334 r
= sd_bus_message_exit_container(reply
);
336 return bus_log_parse_error(r
);
338 return show_table(table
, "machines");
341 static int list_images(int argc
, char *argv
[], void *userdata
) {
343 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
344 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
345 _cleanup_(table_unrefp
) Table
*table
= NULL
;
346 sd_bus
*bus
= userdata
;
351 pager_open(arg_pager_flags
);
353 r
= bus_call_method(bus
, bus_machine_mgr
, "ListImages", &error
, &reply
, NULL
);
355 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
357 table
= table_new("name", "type", "ro", "usage", "created", "modified");
362 table_set_width(table
, 0);
364 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
366 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
368 return bus_log_parse_error(r
);
371 uint64_t crtime
, mtime
, size
;
372 const char *name
, *type
;
375 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
377 return bus_log_parse_error(r
);
381 if (name
[0] == '.' && !arg_all
)
384 r
= table_add_many(table
,
387 TABLE_BOOLEAN
, ro_int
,
388 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
390 TABLE_TIMESTAMP
, crtime
,
391 TABLE_TIMESTAMP
, mtime
);
393 return table_log_add_error(r
);
396 r
= sd_bus_message_exit_container(reply
);
398 return bus_log_parse_error(r
);
400 return show_table(table
, "images");
403 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
404 _cleanup_free_
char *cgroup
= NULL
;
405 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
412 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
425 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
428 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
431 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
433 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
436 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
438 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
443 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
444 _cleanup_free_
char *pretty
= NULL
;
451 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
456 printf("%s%s\n", prefix
, pretty
);
461 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
462 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
463 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
470 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineUIDShift", &error
, &reply
, "s", name
);
472 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
474 r
= sd_bus_message_read(reply
, "u", &shift
);
478 if (shift
== 0) /* Don't show trivial mappings */
481 printf(" UID Shift: %" PRIu32
"\n", shift
);
485 typedef struct MachineStatusInfo
{
491 const char *root_directory
;
493 struct dual_timestamp timestamp
;
498 static void machine_status_info_clear(MachineStatusInfo
*info
) {
505 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
506 _cleanup_free_
char *addresses
= NULL
, *s1
= NULL
, *s2
= NULL
;
512 fputs(strna(i
->name
), stdout
);
514 if (!sd_id128_is_null(i
->id
))
515 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
519 s1
= strdup(strempty(FORMAT_TIMESTAMP_RELATIVE(i
->timestamp
.realtime
)));
520 s2
= strdup(strempty(FORMAT_TIMESTAMP(i
->timestamp
.realtime
)));
523 printf("\t Since: %s; %s\n", strna(s2
), s1
);
524 else if (!isempty(s2
))
525 printf("\t Since: %s\n", s2
);
528 _cleanup_free_
char *t
= NULL
;
530 printf("\t Leader: %u", (unsigned) i
->leader
);
532 (void) get_process_comm(i
->leader
, &t
);
540 printf("\t Service: %s", i
->service
);
543 printf("; class %s", i
->class);
547 printf("\t Class: %s\n", i
->class);
549 if (i
->root_directory
)
550 printf("\t Root: %s\n", i
->root_directory
);
552 if (i
->n_netif
> 0) {
553 fputs("\t Iface:", stdout
);
555 for (size_t c
= 0; c
< i
->n_netif
; c
++) {
556 char name
[IF_NAMESIZE
];
558 if (format_ifname(i
->netif
[c
], name
) >= 0) {
567 printf(" %i", i
->netif
[c
]);
573 if (call_get_addresses(bus
, i
->name
, ifi
,
574 "\t Address: ", "\n\t ",
576 fputs(addresses
, stdout
);
580 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
582 print_uid_shift(bus
, i
->name
);
585 printf("\t Unit: %s\n", i
->unit
);
586 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
588 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
590 show_journal_by_unit(
596 i
->timestamp
.monotonic
,
599 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
600 SD_JOURNAL_LOCAL_ONLY
,
606 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
607 MachineStatusInfo
*i
= userdata
;
612 assert_cc(sizeof(int32_t) == sizeof(int));
613 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
619 i
->n_netif
= l
/ sizeof(int32_t);
620 i
->netif
= memdup(v
, l
);
627 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
629 static const struct bus_properties_map map
[] = {
630 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
631 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
632 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
633 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
634 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
635 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
636 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
637 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
638 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
639 { "NetworkInterfaces", "ai", map_netif
, 0 },
643 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
644 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
645 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
653 r
= bus_map_all_properties(bus
,
654 "org.freedesktop.machine1",
662 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
668 print_machine_status_info(bus
, &info
);
673 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
685 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
687 log_error_errno(r
, "Could not get properties: %m");
692 static int show_machine(int argc
, char *argv
[], void *userdata
) {
694 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
695 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
696 bool properties
, new_line
= false;
697 sd_bus
*bus
= userdata
;
702 properties
= !strstr(argv
[0], "status");
704 pager_open(arg_pager_flags
);
706 if (properties
&& argc
<= 1) {
708 /* If no argument is specified, inspect the manager
710 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
715 for (int i
= 1; i
< argc
; i
++) {
716 const char *path
= NULL
;
718 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachine", &error
, &reply
, "s", argv
[i
]);
720 return log_error_errno(r
, "Could not get path to machine: %s", bus_error_message(&error
, r
));
722 r
= sd_bus_message_read(reply
, "o", &path
);
724 return bus_log_parse_error(r
);
727 r
= show_machine_properties(bus
, path
, &new_line
);
729 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
735 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
736 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
740 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageHostname", NULL
, &reply
, "s", name
);
744 r
= sd_bus_message_read(reply
, "s", &hn
);
749 printf("\tHostname: %s\n", hn
);
754 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
755 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
756 sd_id128_t id
= SD_ID128_NULL
;
761 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineID", NULL
, &reply
, "s", name
);
765 r
= sd_bus_message_read_array(reply
, 'y', &p
, &size
);
769 if (size
== sizeof(sd_id128_t
))
770 memcpy(&id
, p
, size
);
772 if (!sd_id128_is_null(id
))
773 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
778 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
779 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
782 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineInfo", NULL
, &reply
, "s", name
);
786 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
793 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
799 if (streq(p
, "DEPLOYMENT"))
800 printf(" Deployment: %s\n", q
);
803 r
= sd_bus_message_exit_container(reply
);
810 typedef struct ImageStatusInfo
{
819 uint64_t usage_exclusive
;
820 uint64_t limit_exclusive
;
823 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
828 fputs(i
->name
, stdout
);
833 printf("\t Type: %s\n", i
->type
);
836 printf("\t Path: %s\n", i
->path
);
838 (void) print_image_hostname(bus
, i
->name
);
839 (void) print_image_machine_id(bus
, i
->name
);
840 (void) print_image_machine_info(bus
, i
->name
);
842 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
844 printf("\t RO: %s%s%s\n",
845 i
->read_only
? ansi_highlight_red() : "",
846 i
->read_only
? "read-only" : "writable",
847 i
->read_only
? ansi_normal() : "");
849 if (timestamp_is_set(i
->crtime
))
850 printf("\t Created: %s; %s\n",
851 FORMAT_TIMESTAMP(i
->crtime
), FORMAT_TIMESTAMP_RELATIVE(i
->crtime
));
853 if (timestamp_is_set(i
->mtime
))
854 printf("\tModified: %s; %s\n",
855 FORMAT_TIMESTAMP(i
->mtime
), FORMAT_TIMESTAMP_RELATIVE(i
->mtime
));
857 if (i
->usage
!= UINT64_MAX
) {
858 if (i
->usage_exclusive
!= i
->usage
&& i
->usage_exclusive
!= UINT64_MAX
)
859 printf("\t Usage: %s (exclusive: %s)\n",
860 FORMAT_BYTES(i
->usage
), FORMAT_BYTES(i
->usage_exclusive
));
862 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
865 if (i
->limit
!= UINT64_MAX
) {
866 if (i
->limit_exclusive
!= i
->limit
&& i
->limit_exclusive
!= UINT64_MAX
)
867 printf("\t Limit: %s (exclusive: %s)\n",
868 FORMAT_BYTES(i
->limit
), FORMAT_BYTES(i
->limit_exclusive
));
870 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
874 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
876 static const struct bus_properties_map map
[] = {
877 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
878 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
879 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
880 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
881 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
882 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
883 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
884 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
885 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
886 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
890 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
891 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
892 ImageStatusInfo info
= {};
899 r
= bus_map_all_properties(bus
,
900 "org.freedesktop.machine1",
903 BUS_MAP_BOOLEAN_AS_BOOL
,
908 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
914 print_image_status_info(bus
, &info
);
919 typedef struct PoolStatusInfo
{
925 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
927 printf("\t Path: %s\n", i
->path
);
929 if (i
->usage
!= UINT64_MAX
)
930 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
932 if (i
->limit
!= UINT64_MAX
)
933 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
936 static int show_pool_info(sd_bus
*bus
) {
938 static const struct bus_properties_map map
[] = {
939 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
940 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
941 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
945 PoolStatusInfo info
= {
950 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
951 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
956 r
= bus_map_all_properties(bus
,
957 "org.freedesktop.machine1",
958 "/org/freedesktop/machine1",
965 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
967 print_pool_status_info(bus
, &info
);
972 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
984 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
986 log_error_errno(r
, "Could not get properties: %m");
991 static int show_image(int argc
, char *argv
[], void *userdata
) {
993 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
994 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
995 bool properties
, new_line
= false;
996 sd_bus
*bus
= userdata
;
1001 properties
= !strstr(argv
[0], "status");
1003 pager_open(arg_pager_flags
);
1007 /* If no argument is specified, inspect the manager
1011 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1013 r
= show_pool_info(bus
);
1018 for (int i
= 1; i
< argc
; i
++) {
1019 const char *path
= NULL
;
1021 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, &reply
, "s", argv
[i
]);
1023 return log_error_errno(r
, "Could not get path to image: %s", bus_error_message(&error
, r
));
1025 r
= sd_bus_message_read(reply
, "o", &path
);
1027 return bus_log_parse_error(r
);
1030 r
= show_image_properties(bus
, path
, &new_line
);
1032 r
= show_image_info(bus
, path
, &new_line
);
1038 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1039 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1040 sd_bus
*bus
= userdata
;
1045 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1048 arg_kill_who
= "all";
1050 for (int i
= 1; i
< argc
; i
++) {
1051 r
= bus_call_method(
1057 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1059 return log_error_errno(r
, "Could not kill machine: %s", bus_error_message(&error
, r
));
1065 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1066 arg_kill_who
= "leader";
1067 arg_signal
= SIGINT
; /* sysvinit + systemd */
1069 return kill_machine(argc
, argv
, userdata
);
1072 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1073 arg_kill_who
= "leader";
1074 arg_signal
= SIGRTMIN
+4; /* only systemd */
1076 return kill_machine(argc
, argv
, userdata
);
1079 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1080 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1081 sd_bus
*bus
= userdata
;
1086 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1088 for (int i
= 1; i
< argc
; i
++) {
1089 r
= bus_call_method(bus
, bus_machine_mgr
, "TerminateMachine", &error
, NULL
, "s", argv
[i
]);
1091 return log_error_errno(r
, "Could not terminate machine: %s", bus_error_message(&error
, r
));
1097 static const char *select_copy_method(bool copy_from
, bool force
) {
1099 return copy_from
? "CopyFromMachineWithFlags" : "CopyToMachineWithFlags";
1101 return copy_from
? "CopyFromMachine" : "CopyToMachine";
1104 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1105 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1106 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1107 _cleanup_free_
char *abs_host_path
= NULL
;
1108 char *dest
, *host_path
, *container_path
;
1109 sd_bus
*bus
= userdata
;
1115 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1117 copy_from
= streq(argv
[0], "copy-from");
1118 dest
= argv
[3] ?: argv
[2];
1119 host_path
= copy_from
? dest
: argv
[2];
1120 container_path
= copy_from
? argv
[2] : dest
;
1122 if (!path_is_absolute(host_path
)) {
1123 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1125 return log_error_errno(r
, "Failed to make path absolute: %m");
1127 host_path
= abs_host_path
;
1130 r
= bus_message_new_method_call(
1134 select_copy_method(copy_from
, arg_force
));
1136 return bus_log_create_error(r
);
1138 r
= sd_bus_message_append(
1142 copy_from
? container_path
: host_path
,
1143 copy_from
? host_path
: container_path
);
1145 return bus_log_create_error(r
);
1148 r
= sd_bus_message_append(m
, "t", MACHINE_COPY_REPLACE
);
1150 return bus_log_create_error(r
);
1153 /* This is a slow operation, hence turn off any method call timeouts */
1154 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1156 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1161 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1162 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1163 sd_bus
*bus
= userdata
;
1168 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1170 r
= bus_call_method(
1183 return log_error_errno(r
, "Failed to bind mount: %s", bus_error_message(&error
, r
));
1188 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1189 PTYForward
** forward
= (PTYForward
**) userdata
;
1196 /* If the forwarder is already initialized, tell it to
1197 * exit on the next vhangup(), so that we still flush
1198 * out what might be queued and exit then. */
1200 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1204 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1207 /* On error, or when the forwarder is not initialized yet, quit immediately */
1208 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1212 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1218 assert(master
>= 0);
1221 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1224 if (streq(name
, ".host"))
1225 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1227 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1230 (void) sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1231 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1233 r
= pty_forward_new(event
, master
, flags
, forward
);
1235 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1237 r
= sd_event_loop(event
);
1239 return log_error_errno(r
, "Failed to run event loop: %m");
1241 pty_forward_get_last_char(*forward
, &last_char
);
1244 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1245 pty_forward_get_ignore_vhangup(*forward
) == 0;
1247 *forward
= pty_forward_free(*forward
);
1249 if (last_char
!= '\n')
1250 fputc('\n', stdout
);
1254 log_info("Machine %s terminated.", name
);
1255 else if (streq(name
, ".host"))
1256 log_info("Connection to the local host terminated.");
1258 log_info("Connection to machine %s terminated.", name
);
1264 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1266 * Whatever is specified in the spec takes priority over global arguments.
1269 const char *_machine
= NULL
;
1274 at
= strchr(spec
, '@');
1277 /* Do the same as ssh and refuse "@host". */
1281 _uid
= strndup(spec
, at
- spec
);
1288 if (arg_uid
&& !_uid
) {
1289 _uid
= strdup(arg_uid
);
1295 *machine
= isempty(_machine
) ? ".host" : _machine
;
1299 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1300 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1301 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1302 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1303 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1304 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1306 sd_bus
*bus
= userdata
;
1307 const char *match
, *machine
;
1311 if (!strv_isempty(arg_setenv
) || arg_uid
)
1312 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1313 "--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1315 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1316 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1317 "Login only supported on local machines.");
1319 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1321 r
= sd_event_default(&event
);
1323 return log_error_errno(r
, "Failed to get event loop: %m");
1325 r
= sd_bus_attach_event(bus
, event
, 0);
1327 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1329 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1331 match
= strjoina("type='signal',"
1332 "sender='org.freedesktop.machine1',"
1333 "path='/org/freedesktop/machine1',",
1334 "interface='org.freedesktop.machine1.Manager',"
1335 "member='MachineRemoved',"
1336 "arg0='", machine
, "'");
1338 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1340 return log_error_errno(r
, "Failed to request machine removal match: %m");
1342 r
= bus_call_method(bus
, bus_machine_mgr
, "OpenMachineLogin", &error
, &reply
, "s", machine
);
1344 return log_error_errno(r
, "Failed to get login PTY: %s", bus_error_message(&error
, r
));
1346 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1348 return bus_log_parse_error(r
);
1350 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1353 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1354 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1355 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1356 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1357 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1358 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1360 sd_bus
*bus
= userdata
;
1361 const char *match
, *machine
, *path
;
1362 _cleanup_free_
char *uid
= NULL
;
1366 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1367 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1368 "Shell only supported on local machines.");
1370 /* Pass $TERM to shell session, if not explicitly specified. */
1371 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1374 t
= strv_find_prefix(environ
, "TERM=");
1376 if (strv_extend(&arg_setenv
, t
) < 0)
1381 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1383 r
= sd_event_default(&event
);
1385 return log_error_errno(r
, "Failed to get event loop: %m");
1387 r
= sd_bus_attach_event(bus
, event
, 0);
1389 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1391 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1393 return log_error_errno(r
, "Failed to parse machine specification: %m");
1395 match
= strjoina("type='signal',"
1396 "sender='org.freedesktop.machine1',"
1397 "path='/org/freedesktop/machine1',",
1398 "interface='org.freedesktop.machine1.Manager',"
1399 "member='MachineRemoved',"
1400 "arg0='", machine
, "'");
1402 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1404 return log_error_errno(r
, "Failed to request machine removal match: %m");
1406 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "OpenMachineShell");
1408 return bus_log_create_error(r
);
1410 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1412 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1414 return bus_log_create_error(r
);
1416 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1418 return bus_log_create_error(r
);
1420 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1422 return bus_log_create_error(r
);
1424 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1426 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, r
));
1428 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1430 return bus_log_parse_error(r
);
1432 return process_forward(event
, &forward
, master
, 0, machine
);
1435 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1436 sd_bus
*bus
= userdata
;
1441 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1443 for (int i
= 1; i
< argc
; i
++) {
1444 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1445 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1447 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "RemoveImage");
1449 return bus_log_create_error(r
);
1451 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1453 return bus_log_create_error(r
);
1455 /* This is a slow operation, hence turn off any method call timeouts */
1456 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1458 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1464 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1465 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1466 sd_bus
*bus
= userdata
;
1471 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1473 r
= bus_call_method(
1479 "ss", argv
[1], argv
[2]);
1481 return log_error_errno(r
, "Could not rename image: %s", bus_error_message(&error
, r
));
1486 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1487 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1488 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1489 sd_bus
*bus
= userdata
;
1494 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1496 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CloneImage");
1498 return bus_log_create_error(r
);
1500 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1502 return bus_log_create_error(r
);
1504 /* This is a slow operation, hence turn off any method call timeouts */
1505 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1507 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1512 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1513 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1514 sd_bus
*bus
= userdata
;
1520 b
= parse_boolean(argv
[2]);
1522 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1523 "Failed to parse boolean argument: %s",
1527 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1529 r
= bus_call_method(bus
, bus_machine_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1531 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1536 static int image_exists(sd_bus
*bus
, const char *name
) {
1537 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1543 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, NULL
, "s", name
);
1545 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1548 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, r
));
1554 static int make_service_name(const char *name
, char **ret
) {
1560 if (!hostname_is_valid(name
, 0))
1561 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1562 "Invalid machine name %s.", name
);
1564 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1566 return log_error_errno(r
, "Failed to build unit name: %m");
1571 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1572 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1573 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1574 sd_bus
*bus
= userdata
;
1579 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1580 ask_password_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1582 r
= bus_wait_for_jobs_new(bus
, &w
);
1586 for (int i
= 1; i
< argc
; i
++) {
1587 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1588 _cleanup_free_
char *unit
= NULL
;
1591 r
= make_service_name(argv
[i
], &unit
);
1595 r
= image_exists(bus
, argv
[i
]);
1599 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1600 "Machine image '%s' does not exist.",
1603 r
= bus_call_method(
1609 "ss", unit
, "fail");
1611 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, r
));
1613 r
= sd_bus_message_read(reply
, "o", &object
);
1615 return bus_log_parse_error(r
);
1617 r
= bus_wait_for_jobs_add(w
, object
);
1622 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1629 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1630 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1631 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1632 UnitFileChange
*changes
= NULL
;
1633 size_t n_changes
= 0;
1634 const char *method
= NULL
;
1635 sd_bus
*bus
= userdata
;
1640 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1642 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1644 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, method
);
1646 return bus_log_create_error(r
);
1648 r
= sd_bus_message_open_container(m
, 'a', "s");
1650 return bus_log_create_error(r
);
1652 for (int i
= 1; i
< argc
; i
++) {
1653 _cleanup_free_
char *unit
= NULL
;
1655 r
= make_service_name(argv
[i
], &unit
);
1659 r
= image_exists(bus
, argv
[i
]);
1663 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1664 "Machine image '%s' does not exist.",
1667 r
= sd_bus_message_append(m
, "s", unit
);
1669 return bus_log_create_error(r
);
1672 r
= sd_bus_message_close_container(m
);
1674 return bus_log_create_error(r
);
1676 if (streq(argv
[0], "enable"))
1677 r
= sd_bus_message_append(m
, "bb", false, false);
1679 r
= sd_bus_message_append(m
, "b", false);
1681 return bus_log_create_error(r
);
1683 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1685 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, r
));
1687 if (streq(argv
[0], "enable")) {
1688 r
= sd_bus_message_read(reply
, "b", NULL
);
1690 return bus_log_parse_error(r
);
1693 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1697 r
= bus_call_method(bus
, bus_systemd_mgr
, "Reload", &error
, NULL
, NULL
);
1699 log_error("Failed to reload daemon: %s", bus_error_message(&error
, r
));
1706 unit_file_changes_free(changes
, n_changes
);
1711 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1712 const char **our_path
= userdata
, *line
;
1719 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1721 bus_log_parse_error(r
);
1725 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1728 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1731 log_full(priority
, "%s", line
);
1735 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1736 const char **our_path
= userdata
, *path
, *result
;
1743 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1745 bus_log_parse_error(r
);
1749 if (!streq_ptr(*our_path
, path
))
1752 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1756 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1761 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1763 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1767 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1768 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1769 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1770 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1771 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1772 const char *path
= NULL
;
1779 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1781 r
= sd_event_default(&event
);
1783 return log_error_errno(r
, "Failed to get event loop: %m");
1785 r
= sd_bus_attach_event(bus
, event
, 0);
1787 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1789 r
= bus_match_signal_async(
1794 match_transfer_removed
, NULL
, &path
);
1796 return log_error_errno(r
, "Failed to request match: %m");
1798 r
= sd_bus_match_signal_async(
1801 "org.freedesktop.import1",
1803 "org.freedesktop.import1.Transfer",
1805 match_log_message
, NULL
, &path
);
1807 return log_error_errno(r
, "Failed to request match: %m");
1809 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1811 return log_error_errno(r
, "Failed to transfer image: %s", bus_error_message(&error
, r
));
1813 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1815 return bus_log_parse_error(r
);
1817 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1820 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1822 (void) sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1823 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1825 r
= sd_event_loop(event
);
1827 return log_error_errno(r
, "Failed to run event loop: %m");
1832 static int import_tar(int argc
, char *argv
[], void *userdata
) {
1833 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1834 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1835 const char *local
= NULL
, *path
= NULL
;
1836 _cleanup_close_
int fd
= -1;
1837 sd_bus
*bus
= userdata
;
1843 path
= empty_or_dash_to_null(argv
[1]);
1846 local
= empty_or_dash_to_null(argv
[2]);
1848 r
= path_extract_filename(path
, &fn
);
1850 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1851 if (r
== O_DIRECTORY
)
1852 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1853 "Path '%s' refers to directory, but we need a regular file: %m", path
);
1858 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1859 "Need either path or local name.");
1861 r
= tar_strip_suffixes(local
, &ll
);
1867 if (!hostname_is_valid(local
, 0))
1868 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1869 "Local name %s is not a suitable machine name.",
1873 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1875 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1878 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportTar");
1880 return bus_log_create_error(r
);
1882 r
= sd_bus_message_append(
1885 fd
>= 0 ? fd
: STDIN_FILENO
,
1890 return bus_log_create_error(r
);
1892 return transfer_image_common(bus
, m
);
1895 static int import_raw(int argc
, char *argv
[], void *userdata
) {
1896 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1897 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1898 const char *local
= NULL
, *path
= NULL
;
1899 _cleanup_close_
int fd
= -1;
1900 sd_bus
*bus
= userdata
;
1906 path
= empty_or_dash_to_null(argv
[1]);
1909 local
= empty_or_dash_to_null(argv
[2]);
1911 r
= path_extract_filename(path
, &fn
);
1913 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1914 if (r
== O_DIRECTORY
)
1915 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1916 "Path '%s' refers to directory, but we need a regular file: %m", path
);
1921 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1922 "Need either path or local name.");
1924 r
= raw_strip_suffixes(local
, &ll
);
1930 if (!hostname_is_valid(local
, 0))
1931 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1932 "Local name %s is not a suitable machine name.",
1936 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1938 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1941 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportRaw");
1943 return bus_log_create_error(r
);
1945 r
= sd_bus_message_append(
1948 fd
>= 0 ? fd
: STDIN_FILENO
,
1953 return bus_log_create_error(r
);
1955 return transfer_image_common(bus
, m
);
1958 static int import_fs(int argc
, char *argv
[], void *userdata
) {
1959 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1960 const char *local
= NULL
, *path
= NULL
;
1961 _cleanup_free_
char *fn
= NULL
;
1962 _cleanup_close_
int fd
= -1;
1963 sd_bus
*bus
= userdata
;
1969 path
= empty_or_dash_to_null(argv
[1]);
1972 local
= empty_or_dash_to_null(argv
[2]);
1974 r
= path_extract_filename(path
, &fn
);
1976 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1981 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1982 "Need either path or local name.");
1984 if (!hostname_is_valid(local
, 0))
1985 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1986 "Local name %s is not a suitable machine name.",
1990 fd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
1992 return log_error_errno(errno
, "Failed to open directory '%s': %m", path
);
1995 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportFileSystem");
1997 return bus_log_create_error(r
);
1999 r
= sd_bus_message_append(
2002 fd
>= 0 ? fd
: STDIN_FILENO
,
2007 return bus_log_create_error(r
);
2009 return transfer_image_common(bus
, m
);
2012 static void determine_compression_from_filename(const char *p
) {
2019 if (endswith(p
, ".xz"))
2021 else if (endswith(p
, ".gz"))
2022 arg_format
= "gzip";
2023 else if (endswith(p
, ".bz2"))
2024 arg_format
= "bzip2";
2027 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2028 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2029 _cleanup_close_
int fd
= -1;
2030 const char *local
= NULL
, *path
= NULL
;
2031 sd_bus
*bus
= userdata
;
2037 if (!hostname_is_valid(local
, 0))
2038 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2039 "Machine name %s is not valid.", local
);
2043 path
= empty_or_dash_to_null(path
);
2046 determine_compression_from_filename(path
);
2048 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2050 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2053 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ExportTar");
2055 return bus_log_create_error(r
);
2057 r
= sd_bus_message_append(
2061 fd
>= 0 ? fd
: STDOUT_FILENO
,
2064 return bus_log_create_error(r
);
2066 return transfer_image_common(bus
, m
);
2069 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2070 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2071 _cleanup_close_
int fd
= -1;
2072 const char *local
= NULL
, *path
= NULL
;
2073 sd_bus
*bus
= userdata
;
2079 if (!hostname_is_valid(local
, 0))
2080 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2081 "Machine name %s is not valid.", local
);
2085 path
= empty_or_dash_to_null(path
);
2088 determine_compression_from_filename(path
);
2090 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2092 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2095 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ExportRaw");
2097 return bus_log_create_error(r
);
2099 r
= sd_bus_message_append(
2103 fd
>= 0 ? fd
: STDOUT_FILENO
,
2106 return bus_log_create_error(r
);
2108 return transfer_image_common(bus
, m
);
2111 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2112 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2113 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2114 const char *local
, *remote
;
2115 sd_bus
*bus
= userdata
;
2121 if (!http_url_is_valid(remote
) && !file_url_is_valid(remote
))
2122 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2123 "URL '%s' is not valid.", remote
);
2128 r
= import_url_last_component(remote
, &l
);
2130 return log_error_errno(r
, "Failed to get final component of URL: %m");
2135 local
= empty_or_dash_to_null(local
);
2138 r
= tar_strip_suffixes(local
, &ll
);
2144 if (!hostname_is_valid(local
, 0))
2145 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2146 "Local name %s is not a suitable machine name.",
2150 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "PullTar");
2152 return bus_log_create_error(r
);
2154 r
= sd_bus_message_append(
2159 import_verify_to_string(arg_verify
),
2162 return bus_log_create_error(r
);
2164 return transfer_image_common(bus
, m
);
2167 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2168 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2169 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2170 const char *local
, *remote
;
2171 sd_bus
*bus
= userdata
;
2177 if (!http_url_is_valid(remote
) && !file_url_is_valid(remote
))
2178 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2179 "URL '%s' is not valid.", remote
);
2184 r
= import_url_last_component(remote
, &l
);
2186 return log_error_errno(r
, "Failed to get final component of URL: %m");
2191 local
= empty_or_dash_to_null(local
);
2194 r
= raw_strip_suffixes(local
, &ll
);
2200 if (!hostname_is_valid(local
, 0))
2201 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2202 "Local name %s is not a suitable machine name.",
2206 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "PullRaw");
2208 return bus_log_create_error(r
);
2210 r
= sd_bus_message_append(
2215 import_verify_to_string(arg_verify
),
2218 return bus_log_create_error(r
);
2220 return transfer_image_common(bus
, m
);
2223 typedef struct TransferInfo
{
2231 static int compare_transfer_info(const TransferInfo
*a
, const TransferInfo
*b
) {
2232 return strcmp(a
->local
, b
->local
);
2235 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2236 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2237 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2238 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2239 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2240 const char *type
, *remote
, *local
;
2241 sd_bus
*bus
= userdata
;
2242 uint32_t id
, max_id
= 0;
2243 size_t n_transfers
= 0;
2247 pager_open(arg_pager_flags
);
2249 r
= bus_call_method(bus
, bus_import_mgr
, "ListTransfers", &error
, &reply
, NULL
);
2251 return log_error_errno(r
, "Could not get transfers: %s", bus_error_message(&error
, r
));
2253 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2255 return bus_log_parse_error(r
);
2257 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2260 if (!GREEDY_REALLOC(transfers
, n_transfers
+ 1))
2263 transfers
[n_transfers
].id
= id
;
2264 transfers
[n_transfers
].type
= type
;
2265 transfers
[n_transfers
].remote
= remote
;
2266 transfers
[n_transfers
].local
= local
;
2267 transfers
[n_transfers
].progress
= progress
;
2287 return bus_log_parse_error(r
);
2289 r
= sd_bus_message_exit_container(reply
);
2291 return bus_log_parse_error(r
);
2293 typesafe_qsort(transfers
, n_transfers
, compare_transfer_info
);
2295 if (arg_legend
&& n_transfers
> 0)
2296 printf("%-*s %-*s %-*s %-*s %-*s\n",
2297 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2299 (int) max_type
, "TYPE",
2300 (int) max_local
, "LOCAL",
2301 (int) max_remote
, "REMOTE");
2303 for (size_t j
= 0; j
< n_transfers
; j
++)
2305 if (transfers
[j
].progress
< 0)
2306 printf("%*" PRIu32
" %*s %-*s %-*s %-*s\n",
2307 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2309 (int) max_type
, transfers
[j
].type
,
2310 (int) max_local
, transfers
[j
].local
,
2311 (int) max_remote
, transfers
[j
].remote
);
2313 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2314 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2315 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2316 (int) max_type
, transfers
[j
].type
,
2317 (int) max_local
, transfers
[j
].local
,
2318 (int) max_remote
, transfers
[j
].remote
);
2321 if (n_transfers
> 0)
2322 printf("\n%zu transfers listed.\n", n_transfers
);
2324 printf("No transfers.\n");
2330 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2331 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2332 sd_bus
*bus
= userdata
;
2337 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2339 for (int i
= 1; i
< argc
; i
++) {
2342 r
= safe_atou32(argv
[i
], &id
);
2344 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2346 r
= bus_call_method(bus
, bus_import_mgr
, "CancelTransfer", &error
, NULL
, "u", id
);
2348 return log_error_errno(r
, "Could not cancel transfer: %s", bus_error_message(&error
, r
));
2354 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2355 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2356 sd_bus
*bus
= userdata
;
2360 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2362 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2365 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2367 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2371 /* With two arguments changes the quota limit of the
2372 * specified image */
2373 r
= bus_call_method(bus
, bus_machine_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
2375 /* With one argument changes the pool quota limit */
2376 r
= bus_call_method(bus
, bus_machine_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
2379 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2384 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2385 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2386 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2387 uint64_t usage
, total
= 0;
2388 sd_bus
*bus
= userdata
;
2393 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2395 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CleanPool");
2397 return bus_log_create_error(r
);
2399 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2401 return bus_log_create_error(r
);
2403 /* This is a slow operation, hence permit a longer time for completion. */
2404 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2406 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2408 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2410 return bus_log_parse_error(r
);
2412 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2413 if (usage
== UINT64_MAX
) {
2414 log_info("Removed image '%s'", name
);
2417 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2418 name
, FORMAT_BYTES(usage
));
2419 if (total
!= UINT64_MAX
)
2425 r
= sd_bus_message_exit_container(reply
);
2427 return bus_log_parse_error(r
);
2429 if (total
== UINT64_MAX
)
2430 log_info("Removed %u images in total.", c
);
2432 log_info("Removed %u images in total. Total freed exclusive disk space: %s.",
2433 c
, FORMAT_BYTES(total
));
2438 static int help(int argc
, char *argv
[], void *userdata
) {
2439 _cleanup_free_
char *link
= NULL
;
2442 pager_open(arg_pager_flags
);
2444 r
= terminal_urlify_man("machinectl", "1", &link
);
2448 printf("%s [OPTIONS...] COMMAND ...\n\n"
2449 "%sSend control commands to or query the virtual machine and container%s\n"
2450 "%sregistration manager.%s\n"
2451 "\nMachine Commands:\n"
2452 " list List running VMs and containers\n"
2453 " status NAME... Show VM/container details\n"
2454 " show [NAME...] Show properties of one or more VMs/containers\n"
2455 " start NAME... Start container as a service\n"
2456 " login [NAME] Get a login prompt in a container or on the\n"
2458 " shell [[USER@]NAME [COMMAND...]]\n"
2459 " Invoke a shell (or other command) in a container\n"
2460 " or on the local host\n"
2461 " enable NAME... Enable automatic container start at boot\n"
2462 " disable NAME... Disable automatic container start at boot\n"
2463 " poweroff NAME... Power off one or more containers\n"
2464 " reboot NAME... Reboot one or more containers\n"
2465 " terminate NAME... Terminate one or more VMs/containers\n"
2466 " kill NAME... Send signal to processes of a VM/container\n"
2467 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2468 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2469 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2471 " list-images Show available container and VM images\n"
2472 " image-status [NAME...] Show image details\n"
2473 " show-image [NAME...] Show properties of image\n"
2474 " clone NAME NAME Clone an image\n"
2475 " rename NAME NAME Rename an image\n"
2476 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2477 " remove NAME... Remove an image\n"
2478 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2479 " clean Remove hidden (or all) images\n\n"
2480 "Image Transfer Commands:\n"
2481 " pull-tar URL [NAME] Download a TAR container image\n"
2482 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2483 " import-tar FILE [NAME] Import a local TAR container image\n"
2484 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2485 " import-fs DIRECTORY [NAME] Import a local directory container image\n"
2486 " export-tar NAME [FILE] Export a TAR container image locally\n"
2487 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2488 " list-transfers Show list of downloads in progress\n"
2489 " cancel-transfer Cancel a download\n"
2491 " -h --help Show this help\n"
2492 " --version Show package version\n"
2493 " --no-pager Do not pipe output into a pager\n"
2494 " --no-legend Do not show the headers and footers\n"
2495 " --no-ask-password Do not ask for system passwords\n"
2496 " -H --host=[USER@]HOST Operate on remote host\n"
2497 " -M --machine=CONTAINER Operate on local container\n"
2498 " -p --property=NAME Show only properties by this name\n"
2499 " -q --quiet Suppress output\n"
2500 " -a --all Show all properties, including empty ones\n"
2501 " --value When showing properties, only print the value\n"
2502 " -l --full Do not ellipsize output\n"
2503 " --kill-who=WHO Who to send signal to\n"
2504 " -s --signal=SIGNAL Which signal to send\n"
2505 " --uid=USER Specify user ID to invoke shell as\n"
2506 " -E --setenv=VAR[=VALUE] Add an environment variable for shell\n"
2507 " --read-only Create read-only bind mount\n"
2508 " --mkdir Create directory before bind mounting, if missing\n"
2509 " -n --lines=INTEGER Number of journal entries to show\n"
2510 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2511 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2512 " short-iso, short-iso-precise, short-full,\n"
2513 " short-monotonic, short-unix, verbose, export,\n"
2514 " json, json-pretty, json-sse, json-seq, cat,\n"
2516 " --verify=MODE Verification mode for downloaded images (no,\n"
2517 " checksum, signature)\n"
2518 " --force Download image even if already exists\n"
2519 "\nSee the %s for details.\n",
2520 program_invocation_short_name
,
2530 static int parse_argv(int argc
, char *argv
[]) {
2533 ARG_VERSION
= 0x100,
2540 ARG_NO_ASK_PASSWORD
,
2548 static const struct option options
[] = {
2549 { "help", no_argument
, NULL
, 'h' },
2550 { "version", no_argument
, NULL
, ARG_VERSION
},
2551 { "property", required_argument
, NULL
, 'p' },
2552 { "all", no_argument
, NULL
, 'a' },
2553 { "value", no_argument
, NULL
, ARG_VALUE
},
2554 { "full", no_argument
, NULL
, 'l' },
2555 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2556 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2557 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2558 { "signal", required_argument
, NULL
, 's' },
2559 { "host", required_argument
, NULL
, 'H' },
2560 { "machine", required_argument
, NULL
, 'M' },
2561 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2562 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2563 { "quiet", no_argument
, NULL
, 'q' },
2564 { "lines", required_argument
, NULL
, 'n' },
2565 { "output", required_argument
, NULL
, 'o' },
2566 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2567 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2568 { "force", no_argument
, NULL
, ARG_FORCE
},
2569 { "format", required_argument
, NULL
, ARG_FORMAT
},
2570 { "uid", required_argument
, NULL
, ARG_UID
},
2571 { "setenv", required_argument
, NULL
, 'E' },
2572 { "max-addresses", required_argument
, NULL
, ARG_MAX_ADDRESSES
},
2576 bool reorder
= false;
2577 int c
, r
, shell
= -1;
2583 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2585 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2591 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2592 * non-option argument was discovered. */
2596 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2597 * for switches after the main verb. However, for "shell" we really don't want that, since we
2598 * want that switches specified after the machine name are passed to the program to execute,
2599 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2600 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2601 * non-option parameter. If it's the verb "shell" we remember its position and continue
2602 * processing options. In this case, as soon as we hit the next non-option argument we found
2603 * the machine name, and stop further processing. If the first non-option argument is any other
2604 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2608 /* If we already found the "shell" verb on the command line, and now found the next
2609 * non-option argument, then this is the machine name and we should stop processing
2610 * further arguments. */
2611 optind
--; /* don't process this argument, go one step back */
2614 if (streq(optarg
, "shell"))
2615 /* Remember the position of the "shell" verb, and continue processing normally. */
2620 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2621 * processing normally. */
2624 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2625 * at least once with a reset option index. Hence, let's reset the option index here,
2626 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2627 * likely already processed it), and the bump the option index so that we read the
2628 * intended argument again. */
2629 saved_optind
= optind
;
2631 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2632 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2638 return help(0, NULL
, NULL
);
2644 r
= strv_extend(&arg_property
, optarg
);
2648 /* If the user asked for a particular
2649 * property, show it to them, even if it is
2651 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2655 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2660 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
2668 if (safe_atou(optarg
, &arg_lines
) < 0)
2669 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2670 "Failed to parse lines '%s'", optarg
);
2674 if (streq(optarg
, "help")) {
2675 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2679 r
= output_mode_from_string(optarg
);
2681 return log_error_errno(r
, "Unknown output '%s'.", optarg
);
2684 if (OUTPUT_MODE_IS_JSON(arg_output
))
2689 arg_pager_flags
|= PAGER_DISABLE
;
2697 arg_kill_who
= optarg
;
2701 r
= parse_signal_argument(optarg
, &arg_signal
);
2706 case ARG_NO_ASK_PASSWORD
:
2707 arg_ask_password
= false;
2711 arg_transport
= BUS_TRANSPORT_REMOTE
;
2716 arg_transport
= BUS_TRANSPORT_MACHINE
;
2721 arg_read_only
= true;
2733 if (streq(optarg
, "help")) {
2734 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2738 r
= import_verify_from_string(optarg
);
2740 return log_error_errno(r
, "Failed to parse --verify= setting: %s", optarg
);
2749 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2"))
2750 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2751 "Unknown format: %s", optarg
);
2753 arg_format
= optarg
;
2761 r
= strv_env_replace_strdup_passthrough(&arg_setenv
, optarg
);
2763 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
2766 case ARG_MAX_ADDRESSES
:
2767 if (streq(optarg
, "all"))
2768 arg_max_addresses
= ALL_ADDRESSES
;
2769 else if (safe_atoi(optarg
, &arg_max_addresses
) < 0)
2770 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2771 "Invalid number of addresses: %s", optarg
);
2772 else if (arg_max_addresses
<= 0)
2773 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2774 "Number of IPs cannot be negative or zero: %s", optarg
);
2781 assert_not_reached();
2789 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2790 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2792 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2795 for (int i
= shell
; i
< optind
; i
++)
2796 argv
[i
] = argv
[i
+1];
2803 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2805 static const Verb verbs
[] = {
2806 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2807 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2808 { "list-images", VERB_ANY
, 1, 0, list_images
},
2809 { "status", 2, VERB_ANY
, 0, show_machine
},
2810 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2811 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2812 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
2813 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
2814 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
2815 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
2816 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
2817 { "kill", 2, VERB_ANY
, 0, kill_machine
},
2818 { "login", VERB_ANY
, 2, 0, login_machine
},
2819 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
2820 { "bind", 3, 4, 0, bind_mount
},
2821 { "copy-to", 3, 4, 0, copy_files
},
2822 { "copy-from", 3, 4, 0, copy_files
},
2823 { "remove", 2, VERB_ANY
, 0, remove_image
},
2824 { "rename", 3, 3, 0, rename_image
},
2825 { "clone", 3, 3, 0, clone_image
},
2826 { "read-only", 2, 3, 0, read_only_image
},
2827 { "start", 2, VERB_ANY
, 0, start_machine
},
2828 { "enable", 2, VERB_ANY
, 0, enable_machine
},
2829 { "disable", 2, VERB_ANY
, 0, enable_machine
},
2830 { "import-tar", 2, 3, 0, import_tar
},
2831 { "import-raw", 2, 3, 0, import_raw
},
2832 { "import-fs", 2, 3, 0, import_fs
},
2833 { "export-tar", 2, 3, 0, export_tar
},
2834 { "export-raw", 2, 3, 0, export_raw
},
2835 { "pull-tar", 2, 3, 0, pull_tar
},
2836 { "pull-raw", 2, 3, 0, pull_raw
},
2837 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
2838 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
2839 { "set-limit", 2, 3, 0, set_limit
},
2840 { "clean", VERB_ANY
, 1, 0, clean_images
},
2844 return dispatch_verb(argc
, argv
, verbs
, bus
);
2847 static int run(int argc
, char *argv
[]) {
2848 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2851 setlocale(LC_ALL
, "");
2854 /* The journal merging logic potentially needs a lot of fds. */
2855 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
2859 r
= parse_argv(argc
, argv
);
2863 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
2865 return bus_log_connect_error(r
, arg_transport
);
2867 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
2869 return machinectl_main(argc
, argv
, bus
);
2872 DEFINE_MAIN_FUNCTION(run
);