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"
38 #include "main-func.h"
40 #include "nulstr-util.h"
42 #include "parse-argument.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "pretty-print.h"
46 #include "process-util.h"
48 #include "rlimit-util.h"
50 #include "signal-util.h"
51 #include "sort-util.h"
52 #include "spawn-ask-password-agent.h"
53 #include "spawn-polkit-agent.h"
54 #include "stdio-util.h"
55 #include "string-table.h"
57 #include "terminal-util.h"
58 #include "unit-name.h"
62 #define ALL_ADDRESSES -1
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_who
= 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 int 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
, *iter
, **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
[DECIMAL_STR_MAX(int) + 2], buffer
[MAX(INET6_ADDRSTRLEN
, INET_ADDRSTRLEN
)];
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
);
210 if (!strextend(&addresses
,
212 inet_ntop(family
, a
, buffer
, sizeof(buffer
)),
216 r
= sd_bus_message_exit_container(reply
);
218 return bus_log_parse_error(r
);
225 return bus_log_parse_error(r
);
227 r
= sd_bus_message_exit_container(reply
);
229 return bus_log_parse_error(r
);
231 *ret
= TAKE_PTR(addresses
);
235 static int show_table(Table
*table
, const char *word
) {
241 if (table_get_rows(table
) > 1 || OUTPUT_MODE_IS_JSON(arg_output
)) {
242 r
= table_set_sort(table
, (size_t) 0);
244 return table_log_sort_error(r
);
246 table_set_header(table
, arg_legend
);
248 if (OUTPUT_MODE_IS_JSON(arg_output
))
249 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | JSON_FORMAT_COLOR_AUTO
);
251 r
= table_print(table
, NULL
);
253 return table_log_print_error(r
);
257 if (table_get_rows(table
) > 1)
258 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
260 printf("No %s.\n", word
);
266 static int list_machines(int argc
, char *argv
[], void *userdata
) {
268 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
269 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
270 _cleanup_(table_unrefp
) Table
*table
= NULL
;
271 sd_bus
*bus
= userdata
;
276 (void) pager_open(arg_pager_flags
);
278 r
= bus_call_method(bus
, bus_machine_mgr
, "ListMachines", &error
, &reply
, NULL
);
280 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
282 table
= table_new("machine", "class", "service", "os", "version", "addresses");
286 table_set_empty_string(table
, "-");
287 if (!arg_full
&& arg_max_addresses
!= ALL_ADDRESSES
)
288 table_set_cell_height_max(table
, arg_max_addresses
);
291 table_set_width(table
, 0);
293 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
295 return bus_log_parse_error(r
);
298 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
299 const char *name
, *class, *service
;
301 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
303 return bus_log_parse_error(r
);
307 if (name
[0] == '.' && !arg_all
)
310 (void) call_get_os_release(
312 "GetMachineOSRelease",
319 (void) call_get_addresses(
327 r
= table_add_many(table
,
328 TABLE_STRING
, empty_to_null(name
),
329 TABLE_STRING
, empty_to_null(class),
330 TABLE_STRING
, empty_to_null(service
),
331 TABLE_STRING
, empty_to_null(os
),
332 TABLE_STRING
, empty_to_null(version_id
),
333 TABLE_STRING
, empty_to_null(addresses
));
335 return table_log_add_error(r
);
338 r
= sd_bus_message_exit_container(reply
);
340 return bus_log_parse_error(r
);
342 return show_table(table
, "machines");
345 static int list_images(int argc
, char *argv
[], void *userdata
) {
347 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
348 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
349 _cleanup_(table_unrefp
) Table
*table
= NULL
;
350 sd_bus
*bus
= userdata
;
355 (void) pager_open(arg_pager_flags
);
357 r
= bus_call_method(bus
, bus_machine_mgr
, "ListImages", &error
, &reply
, NULL
);
359 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
361 table
= table_new("name", "type", "ro", "usage", "created", "modified");
366 table_set_width(table
, 0);
368 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
370 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
372 return bus_log_parse_error(r
);
375 uint64_t crtime
, mtime
, size
;
376 const char *name
, *type
;
379 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
381 return bus_log_parse_error(r
);
385 if (name
[0] == '.' && !arg_all
)
388 r
= table_add_many(table
,
391 TABLE_BOOLEAN
, ro_int
,
392 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
394 TABLE_TIMESTAMP
, crtime
,
395 TABLE_TIMESTAMP
, mtime
);
397 return table_log_add_error(r
);
400 r
= sd_bus_message_exit_container(reply
);
402 return bus_log_parse_error(r
);
404 return show_table(table
, "images");
407 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
408 _cleanup_free_
char *cgroup
= NULL
;
409 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
416 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
429 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
432 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
435 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
437 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
440 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
442 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
447 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
448 _cleanup_free_
char *pretty
= NULL
;
455 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
460 printf("%s%s\n", prefix
, pretty
);
465 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
466 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
467 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
474 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineUIDShift", &error
, &reply
, "s", name
);
476 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
478 r
= sd_bus_message_read(reply
, "u", &shift
);
482 if (shift
== 0) /* Don't show trivial mappings */
485 printf(" UID Shift: %" PRIu32
"\n", shift
);
489 typedef struct MachineStatusInfo
{
495 const char *root_directory
;
497 struct dual_timestamp timestamp
;
502 static void machine_status_info_clear(MachineStatusInfo
*info
) {
509 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
510 _cleanup_free_
char *addresses
= NULL
, *s1
= NULL
, *s2
= NULL
;
516 fputs(strna(i
->name
), stdout
);
518 if (!sd_id128_is_null(i
->id
))
519 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
523 s1
= strdup(strempty(FORMAT_TIMESTAMP_RELATIVE(i
->timestamp
.realtime
)));
524 s2
= strdup(strempty(FORMAT_TIMESTAMP(i
->timestamp
.realtime
)));
527 printf("\t Since: %s; %s\n", strna(s2
), s1
);
528 else if (!isempty(s2
))
529 printf("\t Since: %s\n", s2
);
532 _cleanup_free_
char *t
= NULL
;
534 printf("\t Leader: %u", (unsigned) i
->leader
);
536 get_process_comm(i
->leader
, &t
);
544 printf("\t Service: %s", i
->service
);
547 printf("; class %s", i
->class);
551 printf("\t Class: %s\n", i
->class);
553 if (i
->root_directory
)
554 printf("\t Root: %s\n", i
->root_directory
);
556 if (i
->n_netif
> 0) {
557 fputs("\t Iface:", stdout
);
559 for (size_t c
= 0; c
< i
->n_netif
; c
++) {
560 char name
[IF_NAMESIZE
+1];
562 if (format_ifname(i
->netif
[c
], name
)) {
571 printf(" %i", i
->netif
[c
]);
577 if (call_get_addresses(bus
, i
->name
, ifi
,
578 "\t Address: ", "\n\t ",
580 fputs(addresses
, stdout
);
584 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
586 print_uid_shift(bus
, i
->name
);
589 printf("\t Unit: %s\n", i
->unit
);
590 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
592 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
594 show_journal_by_unit(
600 i
->timestamp
.monotonic
,
603 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
604 SD_JOURNAL_LOCAL_ONLY
,
610 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
611 MachineStatusInfo
*i
= userdata
;
616 assert_cc(sizeof(int32_t) == sizeof(int));
617 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
623 i
->n_netif
= l
/ sizeof(int32_t);
624 i
->netif
= memdup(v
, l
);
631 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
633 static const struct bus_properties_map map
[] = {
634 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
635 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
636 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
637 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
638 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
639 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
640 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
641 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
642 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
643 { "NetworkInterfaces", "ai", map_netif
, 0 },
647 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
648 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
649 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
657 r
= bus_map_all_properties(bus
,
658 "org.freedesktop.machine1",
666 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
672 print_machine_status_info(bus
, &info
);
677 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
689 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
691 log_error_errno(r
, "Could not get properties: %m");
696 static int show_machine(int argc
, char *argv
[], void *userdata
) {
698 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
699 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
700 bool properties
, new_line
= false;
701 sd_bus
*bus
= userdata
;
706 properties
= !strstr(argv
[0], "status");
708 (void) pager_open(arg_pager_flags
);
710 if (properties
&& argc
<= 1) {
712 /* If no argument is specified, inspect the manager
714 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
719 for (int i
= 1; i
< argc
; i
++) {
720 const char *path
= NULL
;
722 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachine", &error
, &reply
, "s", argv
[i
]);
724 return log_error_errno(r
, "Could not get path to machine: %s", bus_error_message(&error
, r
));
726 r
= sd_bus_message_read(reply
, "o", &path
);
728 return bus_log_parse_error(r
);
731 r
= show_machine_properties(bus
, path
, &new_line
);
733 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
739 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
740 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
744 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageHostname", NULL
, &reply
, "s", name
);
748 r
= sd_bus_message_read(reply
, "s", &hn
);
753 printf("\tHostname: %s\n", hn
);
758 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
759 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
760 sd_id128_t id
= SD_ID128_NULL
;
765 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineID", NULL
, &reply
, "s", name
);
769 r
= sd_bus_message_read_array(reply
, 'y', &p
, &size
);
773 if (size
== sizeof(sd_id128_t
))
774 memcpy(&id
, p
, size
);
776 if (!sd_id128_is_null(id
))
777 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
782 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
783 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
786 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineInfo", NULL
, &reply
, "s", name
);
790 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
797 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
803 if (streq(p
, "DEPLOYMENT"))
804 printf(" Deployment: %s\n", q
);
807 r
= sd_bus_message_exit_container(reply
);
814 typedef struct ImageStatusInfo
{
823 uint64_t usage_exclusive
;
824 uint64_t limit_exclusive
;
827 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
832 fputs(i
->name
, stdout
);
837 printf("\t Type: %s\n", i
->type
);
840 printf("\t Path: %s\n", i
->path
);
842 (void) print_image_hostname(bus
, i
->name
);
843 (void) print_image_machine_id(bus
, i
->name
);
844 (void) print_image_machine_info(bus
, i
->name
);
846 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
848 printf("\t RO: %s%s%s\n",
849 i
->read_only
? ansi_highlight_red() : "",
850 i
->read_only
? "read-only" : "writable",
851 i
->read_only
? ansi_normal() : "");
853 if (i
->crtime
> 0 && i
->crtime
< USEC_INFINITY
)
854 printf("\t Created: %s; %s\n",
855 FORMAT_TIMESTAMP(i
->crtime
), FORMAT_TIMESTAMP_RELATIVE(i
->crtime
));
857 if (i
->mtime
> 0 && i
->mtime
< USEC_INFINITY
)
858 printf("\tModified: %s; %s\n",
859 FORMAT_TIMESTAMP(i
->mtime
), FORMAT_TIMESTAMP_RELATIVE(i
->mtime
));
861 if (i
->usage
!= UINT64_MAX
) {
862 if (i
->usage_exclusive
!= i
->usage
&& i
->usage_exclusive
!= UINT64_MAX
)
863 printf("\t Usage: %s (exclusive: %s)\n",
864 FORMAT_BYTES(i
->usage
), FORMAT_BYTES(i
->usage_exclusive
));
866 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
869 if (i
->limit
!= UINT64_MAX
) {
870 if (i
->limit_exclusive
!= i
->limit
&& i
->limit_exclusive
!= UINT64_MAX
)
871 printf("\t Limit: %s (exclusive: %s)\n",
872 FORMAT_BYTES(i
->limit
), FORMAT_BYTES(i
->limit_exclusive
));
874 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
878 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
880 static const struct bus_properties_map map
[] = {
881 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
882 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
883 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
884 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
885 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
886 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
887 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
888 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
889 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
890 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
894 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
895 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
896 ImageStatusInfo info
= {};
903 r
= bus_map_all_properties(bus
,
904 "org.freedesktop.machine1",
907 BUS_MAP_BOOLEAN_AS_BOOL
,
912 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
918 print_image_status_info(bus
, &info
);
923 typedef struct PoolStatusInfo
{
929 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
931 printf("\t Path: %s\n", i
->path
);
933 if (i
->usage
!= UINT64_MAX
)
934 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
936 if (i
->limit
!= UINT64_MAX
)
937 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
940 static int show_pool_info(sd_bus
*bus
) {
942 static const struct bus_properties_map map
[] = {
943 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
944 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
945 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
949 PoolStatusInfo info
= {
954 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
955 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
960 r
= bus_map_all_properties(bus
,
961 "org.freedesktop.machine1",
962 "/org/freedesktop/machine1",
969 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
971 print_pool_status_info(bus
, &info
);
976 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
988 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
990 log_error_errno(r
, "Could not get properties: %m");
995 static int show_image(int argc
, char *argv
[], void *userdata
) {
997 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
998 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
999 bool properties
, new_line
= false;
1000 sd_bus
*bus
= userdata
;
1005 properties
= !strstr(argv
[0], "status");
1007 (void) pager_open(arg_pager_flags
);
1011 /* If no argument is specified, inspect the manager
1015 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1017 r
= show_pool_info(bus
);
1022 for (int i
= 1; i
< argc
; i
++) {
1023 const char *path
= NULL
;
1025 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, &reply
, "s", argv
[i
]);
1027 return log_error_errno(r
, "Could not get path to image: %s", bus_error_message(&error
, r
));
1029 r
= sd_bus_message_read(reply
, "o", &path
);
1031 return bus_log_parse_error(r
);
1034 r
= show_image_properties(bus
, path
, &new_line
);
1036 r
= show_image_info(bus
, path
, &new_line
);
1042 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1043 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1044 sd_bus
*bus
= userdata
;
1049 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1052 arg_kill_who
= "all";
1054 for (int i
= 1; i
< argc
; i
++) {
1055 r
= bus_call_method(
1061 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1063 return log_error_errno(r
, "Could not kill machine: %s", bus_error_message(&error
, r
));
1069 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1070 arg_kill_who
= "leader";
1071 arg_signal
= SIGINT
; /* sysvinit + systemd */
1073 return kill_machine(argc
, argv
, userdata
);
1076 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1077 arg_kill_who
= "leader";
1078 arg_signal
= SIGRTMIN
+4; /* only systemd */
1080 return kill_machine(argc
, argv
, userdata
);
1083 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1084 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1085 sd_bus
*bus
= userdata
;
1090 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1092 for (int i
= 1; i
< argc
; i
++) {
1093 r
= bus_call_method(bus
, bus_machine_mgr
, "TerminateMachine", &error
, NULL
, "s", argv
[i
]);
1095 return log_error_errno(r
, "Could not terminate machine: %s", bus_error_message(&error
, r
));
1101 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1102 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1103 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1104 _cleanup_free_
char *abs_host_path
= NULL
;
1105 char *dest
, *host_path
, *container_path
;
1106 sd_bus
*bus
= userdata
;
1112 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1114 copy_from
= streq(argv
[0], "copy-from");
1115 dest
= argv
[3] ?: argv
[2];
1116 host_path
= copy_from
? dest
: argv
[2];
1117 container_path
= copy_from
? argv
[2] : dest
;
1119 if (!path_is_absolute(host_path
)) {
1120 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1122 return log_error_errno(r
, "Failed to make path absolute: %m");
1124 host_path
= abs_host_path
;
1127 r
= bus_message_new_method_call(
1131 copy_from
? "CopyFromMachine" : "CopyToMachine");
1133 return bus_log_create_error(r
);
1135 r
= sd_bus_message_append(
1139 copy_from
? container_path
: host_path
,
1140 copy_from
? host_path
: container_path
);
1142 return bus_log_create_error(r
);
1144 /* This is a slow operation, hence turn off any method call timeouts */
1145 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1147 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1152 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1153 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1154 sd_bus
*bus
= userdata
;
1159 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1161 r
= bus_call_method(
1174 return log_error_errno(r
, "Failed to bind mount: %s", bus_error_message(&error
, r
));
1179 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1180 PTYForward
** forward
= (PTYForward
**) userdata
;
1187 /* If the forwarder is already initialized, tell it to
1188 * exit on the next vhangup(), so that we still flush
1189 * out what might be queued and exit then. */
1191 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1195 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1198 /* On error, or when the forwarder is not initialized yet, quit immediately */
1199 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1203 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1209 assert(master
>= 0);
1212 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1215 if (streq(name
, ".host"))
1216 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1218 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1221 (void) sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1222 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1224 r
= pty_forward_new(event
, master
, flags
, forward
);
1226 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1228 r
= sd_event_loop(event
);
1230 return log_error_errno(r
, "Failed to run event loop: %m");
1232 pty_forward_get_last_char(*forward
, &last_char
);
1235 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1236 pty_forward_get_ignore_vhangup(*forward
) == 0;
1238 *forward
= pty_forward_free(*forward
);
1240 if (last_char
!= '\n')
1241 fputc('\n', stdout
);
1245 log_info("Machine %s terminated.", name
);
1246 else if (streq(name
, ".host"))
1247 log_info("Connection to the local host terminated.");
1249 log_info("Connection to machine %s terminated.", name
);
1255 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1257 * Whatever is specified in the spec takes priority over global arguments.
1260 const char *_machine
= NULL
;
1265 at
= strchr(spec
, '@');
1268 /* Do the same as ssh and refuse "@host". */
1272 _uid
= strndup(spec
, at
- spec
);
1279 if (arg_uid
&& !_uid
) {
1280 _uid
= strdup(arg_uid
);
1286 *machine
= isempty(_machine
) ? ".host" : _machine
;
1290 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1291 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1292 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1293 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1294 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1295 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1297 sd_bus
*bus
= userdata
;
1298 const char *match
, *machine
;
1302 if (!strv_isempty(arg_setenv
) || arg_uid
)
1303 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1304 "--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1306 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1307 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1308 "Login only supported on local machines.");
1310 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1312 r
= sd_event_default(&event
);
1314 return log_error_errno(r
, "Failed to get event loop: %m");
1316 r
= sd_bus_attach_event(bus
, event
, 0);
1318 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1320 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1322 match
= strjoina("type='signal',"
1323 "sender='org.freedesktop.machine1',"
1324 "path='/org/freedesktop/machine1',",
1325 "interface='org.freedesktop.machine1.Manager',"
1326 "member='MachineRemoved',"
1327 "arg0='", machine
, "'");
1329 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1331 return log_error_errno(r
, "Failed to request machine removal match: %m");
1333 r
= bus_call_method(bus
, bus_machine_mgr
, "OpenMachineLogin", &error
, &reply
, "s", machine
);
1335 return log_error_errno(r
, "Failed to get login PTY: %s", bus_error_message(&error
, r
));
1337 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1339 return bus_log_parse_error(r
);
1341 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1344 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1345 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1346 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1347 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1348 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1349 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1351 sd_bus
*bus
= userdata
;
1352 const char *match
, *machine
, *path
;
1353 _cleanup_free_
char *uid
= NULL
;
1357 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1358 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1359 "Shell only supported on local machines.");
1361 /* Pass $TERM to shell session, if not explicitly specified. */
1362 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1365 t
= strv_find_prefix(environ
, "TERM=");
1367 if (strv_extend(&arg_setenv
, t
) < 0)
1372 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1374 r
= sd_event_default(&event
);
1376 return log_error_errno(r
, "Failed to get event loop: %m");
1378 r
= sd_bus_attach_event(bus
, event
, 0);
1380 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1382 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1384 return log_error_errno(r
, "Failed to parse machine specification: %m");
1386 match
= strjoina("type='signal',"
1387 "sender='org.freedesktop.machine1',"
1388 "path='/org/freedesktop/machine1',",
1389 "interface='org.freedesktop.machine1.Manager',"
1390 "member='MachineRemoved',"
1391 "arg0='", machine
, "'");
1393 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1395 return log_error_errno(r
, "Failed to request machine removal match: %m");
1397 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "OpenMachineShell");
1399 return bus_log_create_error(r
);
1401 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1403 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1405 return bus_log_create_error(r
);
1407 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1409 return bus_log_create_error(r
);
1411 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1413 return bus_log_create_error(r
);
1415 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1417 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, r
));
1419 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1421 return bus_log_parse_error(r
);
1423 return process_forward(event
, &forward
, master
, 0, machine
);
1426 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1427 sd_bus
*bus
= userdata
;
1432 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1434 for (int i
= 1; i
< argc
; i
++) {
1435 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1436 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1438 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "RemoveImage");
1440 return bus_log_create_error(r
);
1442 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1444 return bus_log_create_error(r
);
1446 /* This is a slow operation, hence turn off any method call timeouts */
1447 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1449 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1455 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1456 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1457 sd_bus
*bus
= userdata
;
1462 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1464 r
= bus_call_method(
1470 "ss", argv
[1], argv
[2]);
1472 return log_error_errno(r
, "Could not rename image: %s", bus_error_message(&error
, r
));
1477 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1478 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1479 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1480 sd_bus
*bus
= userdata
;
1485 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1487 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CloneImage");
1489 return bus_log_create_error(r
);
1491 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1493 return bus_log_create_error(r
);
1495 /* This is a slow operation, hence turn off any method call timeouts */
1496 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1498 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1503 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1504 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1505 sd_bus
*bus
= userdata
;
1511 b
= parse_boolean(argv
[2]);
1513 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1514 "Failed to parse boolean argument: %s",
1518 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1520 r
= bus_call_method(bus
, bus_machine_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1522 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1527 static int image_exists(sd_bus
*bus
, const char *name
) {
1528 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1534 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, NULL
, "s", name
);
1536 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1539 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, r
));
1545 static int make_service_name(const char *name
, char **ret
) {
1551 if (!hostname_is_valid(name
, 0))
1552 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1553 "Invalid machine name %s.", name
);
1555 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1557 return log_error_errno(r
, "Failed to build unit name: %m");
1562 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1563 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1564 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1565 sd_bus
*bus
= userdata
;
1570 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1571 ask_password_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1573 r
= bus_wait_for_jobs_new(bus
, &w
);
1577 for (int i
= 1; i
< argc
; i
++) {
1578 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1579 _cleanup_free_
char *unit
= NULL
;
1582 r
= make_service_name(argv
[i
], &unit
);
1586 r
= image_exists(bus
, argv
[i
]);
1590 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1591 "Machine image '%s' does not exist.",
1594 r
= sd_bus_call_method(
1596 "org.freedesktop.systemd1",
1597 "/org/freedesktop/systemd1",
1598 "org.freedesktop.systemd1.Manager",
1602 "ss", unit
, "fail");
1604 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, r
));
1606 r
= sd_bus_message_read(reply
, "o", &object
);
1608 return bus_log_parse_error(r
);
1610 r
= bus_wait_for_jobs_add(w
, object
);
1615 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1622 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1623 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1624 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1625 UnitFileChange
*changes
= NULL
;
1626 size_t n_changes
= 0;
1627 const char *method
= NULL
;
1628 sd_bus
*bus
= userdata
;
1633 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1635 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1637 r
= sd_bus_message_new_method_call(
1640 "org.freedesktop.systemd1",
1641 "/org/freedesktop/systemd1",
1642 "org.freedesktop.systemd1.Manager",
1645 return bus_log_create_error(r
);
1647 r
= sd_bus_message_open_container(m
, 'a', "s");
1649 return bus_log_create_error(r
);
1651 for (int i
= 1; i
< argc
; i
++) {
1652 _cleanup_free_
char *unit
= NULL
;
1654 r
= make_service_name(argv
[i
], &unit
);
1658 r
= image_exists(bus
, argv
[i
]);
1662 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1663 "Machine image '%s' does not exist.",
1666 r
= sd_bus_message_append(m
, "s", unit
);
1668 return bus_log_create_error(r
);
1671 r
= sd_bus_message_close_container(m
);
1673 return bus_log_create_error(r
);
1675 if (streq(argv
[0], "enable"))
1676 r
= sd_bus_message_append(m
, "bb", false, false);
1678 r
= sd_bus_message_append(m
, "b", false);
1680 return bus_log_create_error(r
);
1682 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1684 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, r
));
1686 if (streq(argv
[0], "enable")) {
1687 r
= sd_bus_message_read(reply
, "b", NULL
);
1689 return bus_log_parse_error(r
);
1692 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1696 r
= sd_bus_call_method(
1698 "org.freedesktop.systemd1",
1699 "/org/freedesktop/systemd1",
1700 "org.freedesktop.systemd1.Manager",
1706 log_error("Failed to reload daemon: %s", bus_error_message(&error
, r
));
1713 unit_file_changes_free(changes
, n_changes
);
1718 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1719 const char **our_path
= userdata
, *line
;
1726 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1728 bus_log_parse_error(r
);
1732 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1735 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1738 log_full(priority
, "%s", line
);
1742 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1743 const char **our_path
= userdata
, *path
, *result
;
1750 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1752 bus_log_parse_error(r
);
1756 if (!streq_ptr(*our_path
, path
))
1759 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1763 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1768 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1770 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1774 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1775 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1776 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1777 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1778 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1779 const char *path
= NULL
;
1786 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1788 r
= sd_event_default(&event
);
1790 return log_error_errno(r
, "Failed to get event loop: %m");
1792 r
= sd_bus_attach_event(bus
, event
, 0);
1794 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1796 r
= bus_match_signal_async(
1801 match_transfer_removed
, NULL
, &path
);
1803 return log_error_errno(r
, "Failed to request match: %m");
1805 r
= sd_bus_match_signal_async(
1808 "org.freedesktop.import1",
1810 "org.freedesktop.import1.Transfer",
1812 match_log_message
, NULL
, &path
);
1814 return log_error_errno(r
, "Failed to request match: %m");
1816 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1818 return log_error_errno(r
, "Failed to transfer image: %s", bus_error_message(&error
, r
));
1820 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1822 return bus_log_parse_error(r
);
1824 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1827 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1829 (void) sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1830 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1832 r
= sd_event_loop(event
);
1834 return log_error_errno(r
, "Failed to run event loop: %m");
1839 static int import_tar(int argc
, char *argv
[], void *userdata
) {
1840 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1841 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1842 const char *local
= NULL
, *path
= NULL
;
1843 _cleanup_close_
int fd
= -1;
1844 sd_bus
*bus
= userdata
;
1850 path
= empty_or_dash_to_null(argv
[1]);
1853 local
= empty_or_dash_to_null(argv
[2]);
1855 r
= path_extract_filename(path
, &fn
);
1857 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1858 if (r
== O_DIRECTORY
)
1859 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1860 "Path '%s' refers to directory, but we need a regular file: %m", path
);
1865 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1866 "Need either path or local name.");
1868 r
= tar_strip_suffixes(local
, &ll
);
1874 if (!hostname_is_valid(local
, 0))
1875 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1876 "Local name %s is not a suitable machine name.",
1880 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1882 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1885 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportTar");
1887 return bus_log_create_error(r
);
1889 r
= sd_bus_message_append(
1892 fd
>= 0 ? fd
: STDIN_FILENO
,
1897 return bus_log_create_error(r
);
1899 return transfer_image_common(bus
, m
);
1902 static int import_raw(int argc
, char *argv
[], void *userdata
) {
1903 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1904 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1905 const char *local
= NULL
, *path
= NULL
;
1906 _cleanup_close_
int fd
= -1;
1907 sd_bus
*bus
= userdata
;
1913 path
= empty_or_dash_to_null(argv
[1]);
1916 local
= empty_or_dash_to_null(argv
[2]);
1918 r
= path_extract_filename(path
, &fn
);
1920 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1921 if (r
== O_DIRECTORY
)
1922 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1923 "Path '%s' refers to directory, but we need a regular file: %m", path
);
1928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1929 "Need either path or local name.");
1931 r
= raw_strip_suffixes(local
, &ll
);
1937 if (!hostname_is_valid(local
, 0))
1938 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1939 "Local name %s is not a suitable machine name.",
1943 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1945 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1948 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportRaw");
1950 return bus_log_create_error(r
);
1952 r
= sd_bus_message_append(
1955 fd
>= 0 ? fd
: STDIN_FILENO
,
1960 return bus_log_create_error(r
);
1962 return transfer_image_common(bus
, m
);
1965 static int import_fs(int argc
, char *argv
[], void *userdata
) {
1966 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1967 const char *local
= NULL
, *path
= NULL
;
1968 _cleanup_free_
char *fn
= NULL
;
1969 _cleanup_close_
int fd
= -1;
1970 sd_bus
*bus
= userdata
;
1976 path
= empty_or_dash_to_null(argv
[1]);
1979 local
= empty_or_dash_to_null(argv
[2]);
1981 r
= path_extract_filename(path
, &fn
);
1983 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1988 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1989 "Need either path or local name.");
1991 if (!hostname_is_valid(local
, 0))
1992 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1993 "Local name %s is not a suitable machine name.",
1997 fd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
1999 return log_error_errno(errno
, "Failed to open directory '%s': %m", path
);
2002 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportFileSystem");
2004 return bus_log_create_error(r
);
2006 r
= sd_bus_message_append(
2009 fd
>= 0 ? fd
: STDIN_FILENO
,
2014 return bus_log_create_error(r
);
2016 return transfer_image_common(bus
, m
);
2019 static void determine_compression_from_filename(const char *p
) {
2026 if (endswith(p
, ".xz"))
2028 else if (endswith(p
, ".gz"))
2029 arg_format
= "gzip";
2030 else if (endswith(p
, ".bz2"))
2031 arg_format
= "bzip2";
2034 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2035 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2036 _cleanup_close_
int fd
= -1;
2037 const char *local
= NULL
, *path
= NULL
;
2038 sd_bus
*bus
= userdata
;
2044 if (!hostname_is_valid(local
, 0))
2045 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2046 "Machine name %s is not valid.", local
);
2050 path
= empty_or_dash_to_null(path
);
2053 determine_compression_from_filename(path
);
2055 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2057 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2060 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ExportTar");
2062 return bus_log_create_error(r
);
2064 r
= sd_bus_message_append(
2068 fd
>= 0 ? fd
: STDOUT_FILENO
,
2071 return bus_log_create_error(r
);
2073 return transfer_image_common(bus
, m
);
2076 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2077 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2078 _cleanup_close_
int fd
= -1;
2079 const char *local
= NULL
, *path
= NULL
;
2080 sd_bus
*bus
= userdata
;
2086 if (!hostname_is_valid(local
, 0))
2087 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2088 "Machine name %s is not valid.", local
);
2092 path
= empty_or_dash_to_null(path
);
2095 determine_compression_from_filename(path
);
2097 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2099 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2102 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ExportRaw");
2104 return bus_log_create_error(r
);
2106 r
= sd_bus_message_append(
2110 fd
>= 0 ? fd
: STDOUT_FILENO
,
2113 return bus_log_create_error(r
);
2115 return transfer_image_common(bus
, m
);
2118 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2119 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2120 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2121 const char *local
, *remote
;
2122 sd_bus
*bus
= userdata
;
2128 if (!http_url_is_valid(remote
))
2129 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2130 "URL '%s' is not valid.", remote
);
2135 r
= import_url_last_component(remote
, &l
);
2137 return log_error_errno(r
, "Failed to get final component of URL: %m");
2142 local
= empty_or_dash_to_null(local
);
2145 r
= tar_strip_suffixes(local
, &ll
);
2151 if (!hostname_is_valid(local
, 0))
2152 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2153 "Local name %s is not a suitable machine name.",
2157 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "PullTar");
2159 return bus_log_create_error(r
);
2161 r
= sd_bus_message_append(
2166 import_verify_to_string(arg_verify
),
2169 return bus_log_create_error(r
);
2171 return transfer_image_common(bus
, m
);
2174 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2175 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2176 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2177 const char *local
, *remote
;
2178 sd_bus
*bus
= userdata
;
2184 if (!http_url_is_valid(remote
))
2185 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2186 "URL '%s' is not valid.", remote
);
2191 r
= import_url_last_component(remote
, &l
);
2193 return log_error_errno(r
, "Failed to get final component of URL: %m");
2198 local
= empty_or_dash_to_null(local
);
2201 r
= raw_strip_suffixes(local
, &ll
);
2207 if (!hostname_is_valid(local
, 0))
2208 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2209 "Local name %s is not a suitable machine name.",
2213 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "PullRaw");
2215 return bus_log_create_error(r
);
2217 r
= sd_bus_message_append(
2222 import_verify_to_string(arg_verify
),
2225 return bus_log_create_error(r
);
2227 return transfer_image_common(bus
, m
);
2230 typedef struct TransferInfo
{
2238 static int compare_transfer_info(const TransferInfo
*a
, const TransferInfo
*b
) {
2239 return strcmp(a
->local
, b
->local
);
2242 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2243 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2244 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2245 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2246 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2247 const char *type
, *remote
, *local
;
2248 sd_bus
*bus
= userdata
;
2249 uint32_t id
, max_id
= 0;
2250 size_t n_transfers
= 0;
2254 (void) pager_open(arg_pager_flags
);
2256 r
= bus_call_method(bus
, bus_import_mgr
, "ListTransfers", &error
, &reply
, NULL
);
2258 return log_error_errno(r
, "Could not get transfers: %s", bus_error_message(&error
, r
));
2260 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2262 return bus_log_parse_error(r
);
2264 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2267 if (!GREEDY_REALLOC(transfers
, n_transfers
+ 1))
2270 transfers
[n_transfers
].id
= id
;
2271 transfers
[n_transfers
].type
= type
;
2272 transfers
[n_transfers
].remote
= remote
;
2273 transfers
[n_transfers
].local
= local
;
2274 transfers
[n_transfers
].progress
= progress
;
2294 return bus_log_parse_error(r
);
2296 r
= sd_bus_message_exit_container(reply
);
2298 return bus_log_parse_error(r
);
2300 typesafe_qsort(transfers
, n_transfers
, compare_transfer_info
);
2302 if (arg_legend
&& n_transfers
> 0)
2303 printf("%-*s %-*s %-*s %-*s %-*s\n",
2304 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2306 (int) max_type
, "TYPE",
2307 (int) max_local
, "LOCAL",
2308 (int) max_remote
, "REMOTE");
2310 for (size_t j
= 0; j
< n_transfers
; j
++)
2312 if (transfers
[j
].progress
< 0)
2313 printf("%*" PRIu32
" %*s %-*s %-*s %-*s\n",
2314 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2316 (int) max_type
, transfers
[j
].type
,
2317 (int) max_local
, transfers
[j
].local
,
2318 (int) max_remote
, transfers
[j
].remote
);
2320 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2321 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2322 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2323 (int) max_type
, transfers
[j
].type
,
2324 (int) max_local
, transfers
[j
].local
,
2325 (int) max_remote
, transfers
[j
].remote
);
2328 if (n_transfers
> 0)
2329 printf("\n%zu transfers listed.\n", n_transfers
);
2331 printf("No transfers.\n");
2337 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2338 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2339 sd_bus
*bus
= userdata
;
2344 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2346 for (int i
= 1; i
< argc
; i
++) {
2349 r
= safe_atou32(argv
[i
], &id
);
2351 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2353 r
= bus_call_method(bus
, bus_import_mgr
, "CancelTransfer", &error
, NULL
, "u", id
);
2355 return log_error_errno(r
, "Could not cancel transfer: %s", bus_error_message(&error
, r
));
2361 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2362 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2363 sd_bus
*bus
= userdata
;
2367 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2369 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2372 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2374 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2378 /* With two arguments changes the quota limit of the
2379 * specified image */
2380 r
= bus_call_method(bus
, bus_machine_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
2382 /* With one argument changes the pool quota limit */
2383 r
= bus_call_method(bus
, bus_machine_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
2386 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2391 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2392 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2393 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2394 uint64_t usage
, total
= 0;
2395 sd_bus
*bus
= userdata
;
2400 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2402 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CleanPool");
2404 return bus_log_create_error(r
);
2406 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2408 return bus_log_create_error(r
);
2410 /* This is a slow operation, hence permit a longer time for completion. */
2411 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2413 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2415 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2417 return bus_log_parse_error(r
);
2419 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2420 if (usage
== UINT64_MAX
) {
2421 log_info("Removed image '%s'", name
);
2424 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2425 name
, FORMAT_BYTES(usage
));
2426 if (total
!= UINT64_MAX
)
2432 r
= sd_bus_message_exit_container(reply
);
2434 return bus_log_parse_error(r
);
2436 if (total
== UINT64_MAX
)
2437 log_info("Removed %u images in total.", c
);
2439 log_info("Removed %u images in total. Total freed exclusive disk space: %s.",
2440 c
, FORMAT_BYTES(total
));
2445 static int help(int argc
, char *argv
[], void *userdata
) {
2446 _cleanup_free_
char *link
= NULL
;
2449 (void) pager_open(arg_pager_flags
);
2451 r
= terminal_urlify_man("machinectl", "1", &link
);
2455 printf("%s [OPTIONS...] COMMAND ...\n\n"
2456 "%sSend control commands to or query the virtual machine and container%s\n"
2457 "%sregistration manager.%s\n"
2458 "\nMachine Commands:\n"
2459 " list List running VMs and containers\n"
2460 " status NAME... Show VM/container details\n"
2461 " show [NAME...] Show properties of one or more VMs/containers\n"
2462 " start NAME... Start container as a service\n"
2463 " login [NAME] Get a login prompt in a container or on the\n"
2465 " shell [[USER@]NAME [COMMAND...]]\n"
2466 " Invoke a shell (or other command) in a container\n"
2467 " or on the local host\n"
2468 " enable NAME... Enable automatic container start at boot\n"
2469 " disable NAME... Disable automatic container start at boot\n"
2470 " poweroff NAME... Power off one or more containers\n"
2471 " reboot NAME... Reboot one or more containers\n"
2472 " terminate NAME... Terminate one or more VMs/containers\n"
2473 " kill NAME... Send signal to processes of a VM/container\n"
2474 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2475 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2476 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2478 " list-images Show available container and VM images\n"
2479 " image-status [NAME...] Show image details\n"
2480 " show-image [NAME...] Show properties of image\n"
2481 " clone NAME NAME Clone an image\n"
2482 " rename NAME NAME Rename an image\n"
2483 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2484 " remove NAME... Remove an image\n"
2485 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2486 " clean Remove hidden (or all) images\n\n"
2487 "Image Transfer Commands:\n"
2488 " pull-tar URL [NAME] Download a TAR container image\n"
2489 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2490 " import-tar FILE [NAME] Import a local TAR container image\n"
2491 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2492 " import-fs DIRECTORY [NAME] Import a local directory container image\n"
2493 " export-tar NAME [FILE] Export a TAR container image locally\n"
2494 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2495 " list-transfers Show list of downloads in progress\n"
2496 " cancel-transfer Cancel a download\n"
2498 " -h --help Show this help\n"
2499 " --version Show package version\n"
2500 " --no-pager Do not pipe output into a pager\n"
2501 " --no-legend Do not show the headers and footers\n"
2502 " --no-ask-password Do not ask for system passwords\n"
2503 " -H --host=[USER@]HOST Operate on remote host\n"
2504 " -M --machine=CONTAINER Operate on local container\n"
2505 " -p --property=NAME Show only properties by this name\n"
2506 " -q --quiet Suppress output\n"
2507 " -a --all Show all properties, including empty ones\n"
2508 " --value When showing properties, only print the value\n"
2509 " -l --full Do not ellipsize output\n"
2510 " --kill-who=WHO Who to send signal to\n"
2511 " -s --signal=SIGNAL Which signal to send\n"
2512 " --uid=USER Specify user ID to invoke shell as\n"
2513 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2514 " --read-only Create read-only bind mount\n"
2515 " --mkdir Create directory before bind mounting, if missing\n"
2516 " -n --lines=INTEGER Number of journal entries to show\n"
2517 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2518 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2519 " short-iso, short-iso-precise, short-full,\n"
2520 " short-monotonic, short-unix, verbose, export,\n"
2521 " json, json-pretty, json-sse, json-seq, cat,\n"
2523 " --verify=MODE Verification mode for downloaded images (no,\n"
2524 " checksum, signature)\n"
2525 " --force Download image even if already exists\n"
2526 "\nSee the %s for details.\n",
2527 program_invocation_short_name
,
2537 static int parse_argv(int argc
, char *argv
[]) {
2540 ARG_VERSION
= 0x100,
2547 ARG_NO_ASK_PASSWORD
,
2555 static const struct option options
[] = {
2556 { "help", no_argument
, NULL
, 'h' },
2557 { "version", no_argument
, NULL
, ARG_VERSION
},
2558 { "property", required_argument
, NULL
, 'p' },
2559 { "all", no_argument
, NULL
, 'a' },
2560 { "value", no_argument
, NULL
, ARG_VALUE
},
2561 { "full", no_argument
, NULL
, 'l' },
2562 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2563 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2564 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2565 { "signal", required_argument
, NULL
, 's' },
2566 { "host", required_argument
, NULL
, 'H' },
2567 { "machine", required_argument
, NULL
, 'M' },
2568 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2569 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2570 { "quiet", no_argument
, NULL
, 'q' },
2571 { "lines", required_argument
, NULL
, 'n' },
2572 { "output", required_argument
, NULL
, 'o' },
2573 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2574 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2575 { "force", no_argument
, NULL
, ARG_FORCE
},
2576 { "format", required_argument
, NULL
, ARG_FORMAT
},
2577 { "uid", required_argument
, NULL
, ARG_UID
},
2578 { "setenv", required_argument
, NULL
, 'E' },
2579 { "max-addresses", required_argument
, NULL
, ARG_MAX_ADDRESSES
},
2583 bool reorder
= false;
2584 int c
, r
, shell
= -1;
2590 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2592 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2598 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2599 * non-option argument was discovered. */
2603 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2604 * for switches after the main verb. However, for "shell" we really don't want that, since we
2605 * want that switches specified after the machine name are passed to the program to execute,
2606 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2607 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2608 * non-option parameter. If it's the verb "shell" we remember its position and continue
2609 * processing options. In this case, as soon as we hit the next non-option argument we found
2610 * the machine name, and stop further processing. If the first non-option argument is any other
2611 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2615 /* If we already found the "shell" verb on the command line, and now found the next
2616 * non-option argument, then this is the machine name and we should stop processing
2617 * further arguments. */
2618 optind
--; /* don't process this argument, go one step back */
2621 if (streq(optarg
, "shell"))
2622 /* Remember the position of the "shell" verb, and continue processing normally. */
2627 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2628 * processing normally. */
2631 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2632 * at least once with a reset option index. Hence, let's reset the option index here,
2633 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2634 * likely already processed it), and the bump the option index so that we read the
2635 * intended argument again. */
2636 saved_optind
= optind
;
2638 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2639 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2645 return help(0, NULL
, NULL
);
2651 r
= strv_extend(&arg_property
, optarg
);
2655 /* If the user asked for a particular
2656 * property, show it to them, even if it is
2658 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2662 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2667 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
2675 if (safe_atou(optarg
, &arg_lines
) < 0)
2676 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2677 "Failed to parse lines '%s'", optarg
);
2681 if (streq(optarg
, "help")) {
2682 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2686 r
= output_mode_from_string(optarg
);
2688 return log_error_errno(r
, "Unknown output '%s'.", optarg
);
2691 if (OUTPUT_MODE_IS_JSON(arg_output
))
2696 arg_pager_flags
|= PAGER_DISABLE
;
2704 arg_kill_who
= optarg
;
2708 r
= parse_signal_argument(optarg
, &arg_signal
);
2713 case ARG_NO_ASK_PASSWORD
:
2714 arg_ask_password
= false;
2718 arg_transport
= BUS_TRANSPORT_REMOTE
;
2723 arg_transport
= BUS_TRANSPORT_MACHINE
;
2728 arg_read_only
= true;
2740 if (streq(optarg
, "help")) {
2741 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2745 r
= import_verify_from_string(optarg
);
2747 return log_error_errno(r
, "Failed to parse --verify= setting: %s", optarg
);
2756 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2"))
2757 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2758 "Unknown format: %s", optarg
);
2760 arg_format
= optarg
;
2768 if (!env_assignment_is_valid(optarg
))
2769 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2770 "Environment assignment invalid: %s", optarg
);
2772 r
= strv_extend(&arg_setenv
, optarg
);
2777 case ARG_MAX_ADDRESSES
:
2778 if (streq(optarg
, "all"))
2779 arg_max_addresses
= ALL_ADDRESSES
;
2780 else if (safe_atoi(optarg
, &arg_max_addresses
) < 0)
2781 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2782 "Invalid number of addresses: %s", optarg
);
2783 else if (arg_max_addresses
<= 0)
2784 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2785 "Number of IPs cannot be negative or zero: %s", optarg
);
2792 assert_not_reached("Unhandled option");
2800 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2801 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2803 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2806 for (int i
= shell
; i
< optind
; i
++)
2807 argv
[i
] = argv
[i
+1];
2814 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2816 static const Verb verbs
[] = {
2817 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2818 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2819 { "list-images", VERB_ANY
, 1, 0, list_images
},
2820 { "status", 2, VERB_ANY
, 0, show_machine
},
2821 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2822 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2823 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
2824 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
2825 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
2826 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
2827 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
2828 { "kill", 2, VERB_ANY
, 0, kill_machine
},
2829 { "login", VERB_ANY
, 2, 0, login_machine
},
2830 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
2831 { "bind", 3, 4, 0, bind_mount
},
2832 { "copy-to", 3, 4, 0, copy_files
},
2833 { "copy-from", 3, 4, 0, copy_files
},
2834 { "remove", 2, VERB_ANY
, 0, remove_image
},
2835 { "rename", 3, 3, 0, rename_image
},
2836 { "clone", 3, 3, 0, clone_image
},
2837 { "read-only", 2, 3, 0, read_only_image
},
2838 { "start", 2, VERB_ANY
, 0, start_machine
},
2839 { "enable", 2, VERB_ANY
, 0, enable_machine
},
2840 { "disable", 2, VERB_ANY
, 0, enable_machine
},
2841 { "import-tar", 2, 3, 0, import_tar
},
2842 { "import-raw", 2, 3, 0, import_raw
},
2843 { "import-fs", 2, 3, 0, import_fs
},
2844 { "export-tar", 2, 3, 0, export_tar
},
2845 { "export-raw", 2, 3, 0, export_raw
},
2846 { "pull-tar", 2, 3, 0, pull_tar
},
2847 { "pull-raw", 2, 3, 0, pull_raw
},
2848 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
2849 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
2850 { "set-limit", 2, 3, 0, set_limit
},
2851 { "clean", VERB_ANY
, 1, 0, clean_images
},
2855 return dispatch_verb(argc
, argv
, verbs
, bus
);
2858 static int run(int argc
, char *argv
[]) {
2859 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2862 setlocale(LC_ALL
, "");
2865 /* The journal merging logic potentially needs a lot of fds. */
2866 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
2870 r
= parse_argv(argc
, argv
);
2874 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
2876 return bus_log_connect_error(r
);
2878 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
2880 return machinectl_main(argc
, argv
, bus
);
2883 DEFINE_MAIN_FUNCTION(run
);