1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include <netinet/in.h>
11 #include <sys/mount.h>
12 #include <sys/socket.h>
17 #include "alloc-util.h"
18 #include "bus-common-errors.h"
19 #include "bus-error.h"
20 #include "bus-unit-util.h"
22 #include "cgroup-show.h"
23 #include "cgroup-util.h"
27 #include "format-table.h"
28 #include "hostname-util.h"
29 #include "import-util.h"
30 #include "locale-util.h"
32 #include "logs-show.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "process-util.h"
41 #include "signal-util.h"
42 #include "spawn-polkit-agent.h"
43 #include "stdio-util.h"
44 #include "string-table.h"
46 #include "terminal-util.h"
47 #include "unit-name.h"
52 #define ALL_IP_ADDRESSES -1
54 static char **arg_property
= NULL
;
55 static bool arg_all
= false;
56 static bool arg_value
= false;
57 static bool arg_full
= false;
58 static bool arg_no_pager
= false;
59 static bool arg_legend
= true;
60 static const char *arg_kill_who
= NULL
;
61 static int arg_signal
= SIGTERM
;
62 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
63 static char *arg_host
= NULL
;
64 static bool arg_read_only
= false;
65 static bool arg_mkdir
= false;
66 static bool arg_quiet
= false;
67 static bool arg_ask_password
= true;
68 static unsigned arg_lines
= 10;
69 static OutputMode arg_output
= OUTPUT_SHORT
;
70 static bool arg_force
= false;
71 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
72 static const char* arg_format
= NULL
;
73 static const char *arg_uid
= NULL
;
74 static char **arg_setenv
= NULL
;
75 static int arg_addrs
= 1;
77 static OutputFlags
get_output_flags(void) {
79 arg_all
* OUTPUT_SHOW_ALL
|
80 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
81 colors_enabled() * OUTPUT_COLOR
|
82 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
85 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
86 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
87 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
88 const char *k
, *v
, *iter
, **query_res
= NULL
;
89 size_t count
= 0, awaited_args
= 0;
97 NULSTR_FOREACH(iter
, query
)
99 query_res
= newa0(const char *, awaited_args
);
101 r
= sd_bus_call_method(
103 "org.freedesktop.machine1",
104 "/org/freedesktop/machine1",
105 "org.freedesktop.machine1.Manager",
110 return log_debug_errno(r
, "Failed to call '%s()': %s", method
, bus_error_message(&error
, r
));
112 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
114 return bus_log_parse_error(r
);
116 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
118 NULSTR_FOREACH(iter
, query
) {
119 if (streq(k
, iter
)) {
120 query_res
[count
] = v
;
127 return bus_log_parse_error(r
);
129 r
= sd_bus_message_exit_container(reply
);
131 return bus_log_parse_error(r
);
134 for (count
= 0; count
< awaited_args
; count
++) {
137 out
= va_arg(ap
, char **);
139 if (query_res
[count
]) {
140 val
= strdup(query_res
[count
]);
153 static int call_get_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
, char **ret
) {
155 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
156 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
157 _cleanup_free_
char *addresses
= NULL
;
158 bool truncate
= false;
167 r
= sd_bus_call_method(bus
,
168 "org.freedesktop.machine1",
169 "/org/freedesktop/machine1",
170 "org.freedesktop.machine1.Manager",
171 "GetMachineAddresses",
176 return log_debug_errno(r
, "Could not get addresses: %s", bus_error_message(&error
, r
));
178 addresses
= strdup(prefix
);
183 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
185 return bus_log_parse_error(r
);
187 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
191 char buf_ifi
[DECIMAL_STR_MAX(int) + 2], buffer
[MAX(INET6_ADDRSTRLEN
, INET_ADDRSTRLEN
)];
193 r
= sd_bus_message_read(reply
, "i", &family
);
195 return bus_log_parse_error(r
);
197 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
199 return bus_log_parse_error(r
);
202 if (family
== AF_INET6
&& ifi
> 0)
203 xsprintf(buf_ifi
, "%%%i", ifi
);
207 if (!strextend(&addresses
, prefix
, inet_ntop(family
, a
, buffer
, sizeof(buffer
)), buf_ifi
, NULL
))
212 r
= sd_bus_message_exit_container(reply
);
214 return bus_log_parse_error(r
);
224 return bus_log_parse_error(r
);
226 r
= sd_bus_message_exit_container(reply
);
228 return bus_log_parse_error(r
);
232 if (!strextend(&addresses
, special_glyph(ELLIPSIS
), NULL
))
237 *ret
= TAKE_PTR(addresses
);
241 static int show_table(Table
*table
, const char *word
) {
247 if (table_get_rows(table
) > 1) {
248 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
250 return log_error_errno(r
, "Failed to sort table: %m");
252 table_set_header(table
, arg_legend
);
254 r
= table_print(table
, NULL
);
256 return log_error_errno(r
, "Failed to show table: %m");
260 if (table_get_rows(table
) > 1)
261 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
263 printf("No %s.\n", word
);
269 static int list_machines(int argc
, char *argv
[], void *userdata
) {
271 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
272 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
273 _cleanup_(table_unrefp
) Table
*table
= NULL
;
274 sd_bus
*bus
= userdata
;
279 (void) pager_open(arg_no_pager
, false);
281 r
= sd_bus_call_method(bus
,
282 "org.freedesktop.machine1",
283 "/org/freedesktop/machine1",
284 "org.freedesktop.machine1.Manager",
290 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
292 table
= table_new("MACHINE", "CLASS", "SERVICE", "OS", "VERSION", "ADDRESSES");
296 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
298 return bus_log_parse_error(r
);
301 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
302 const char *name
, *class, *service
;
304 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
306 return bus_log_parse_error(r
);
310 if (name
[0] == '.' && !arg_all
)
313 (void) call_get_os_release(
315 "GetMachineOSRelease",
322 (void) call_get_addresses(
331 r
= table_add_many(table
,
334 TABLE_STRING
, empty_to_dash(service
),
335 TABLE_STRING
, empty_to_dash(os
),
336 TABLE_STRING
, empty_to_dash(version_id
),
337 TABLE_STRING
, empty_to_dash(addresses
));
339 return log_error_errno(r
, "Failed to add table row: %m");
342 r
= sd_bus_message_exit_container(reply
);
344 return bus_log_parse_error(r
);
346 return show_table(table
, "machines");
349 static int list_images(int argc
, char *argv
[], void *userdata
) {
351 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
352 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
353 _cleanup_(table_unrefp
) Table
*table
= NULL
;
354 sd_bus
*bus
= userdata
;
359 (void) pager_open(arg_no_pager
, false);
361 r
= sd_bus_call_method(bus
,
362 "org.freedesktop.machine1",
363 "/org/freedesktop/machine1",
364 "org.freedesktop.machine1.Manager",
370 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
372 table
= table_new("NAME", "TYPE", "RO", "USAGE", "CREATED", "MODIFIED");
376 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
378 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
380 return bus_log_parse_error(r
);
383 uint64_t crtime
, mtime
, size
;
384 const char *name
, *type
;
389 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
391 return bus_log_parse_error(r
);
395 if (name
[0] == '.' && !arg_all
)
398 r
= table_add_many(table
,
402 return log_error_errno(r
, "Failed to add table row: %m");
405 r
= table_add_cell(table
, &cell
, TABLE_BOOLEAN
, &ro_bool
);
407 return log_error_errno(r
, "Failed to add table cell: %m");
410 r
= table_set_color(table
, cell
, ansi_highlight_red());
412 return log_error_errno(r
, "Failed to set table cell color: %m");
415 r
= table_add_many(table
,
417 TABLE_TIMESTAMP
, crtime
,
418 TABLE_TIMESTAMP
, mtime
);
420 return log_error_errno(r
, "Failed to add table row: %m");
423 r
= sd_bus_message_exit_container(reply
);
425 return bus_log_parse_error(r
);
427 return show_table(table
, "images");
430 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
431 _cleanup_free_
char *cgroup
= NULL
;
432 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
439 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
452 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
455 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
458 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
460 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
463 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
465 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
470 static int print_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
) {
471 _cleanup_free_
char *s
= NULL
;
474 r
= call_get_addresses(bus
, name
, ifi
, prefix
, prefix2
, n_addr
, &s
);
484 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
485 _cleanup_free_
char *pretty
= NULL
;
492 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
497 printf("%s%s\n", prefix
, pretty
);
502 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
503 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
504 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
511 r
= sd_bus_call_method(bus
,
512 "org.freedesktop.machine1",
513 "/org/freedesktop/machine1",
514 "org.freedesktop.machine1.Manager",
515 "GetMachineUIDShift",
520 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
522 r
= sd_bus_message_read(reply
, "u", &shift
);
526 if (shift
== 0) /* Don't show trivial mappings */
529 printf(" UID Shift: %" PRIu32
"\n", shift
);
533 typedef struct MachineStatusInfo
{
539 const char *root_directory
;
541 struct dual_timestamp timestamp
;
546 static void machine_status_info_clear(MachineStatusInfo
*info
) {
553 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
554 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
555 char since2
[FORMAT_TIMESTAMP_MAX
];
562 fputs(strna(i
->name
), stdout
);
564 if (!sd_id128_is_null(i
->id
))
565 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
569 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->timestamp
.realtime
);
570 s2
= format_timestamp(since2
, sizeof(since2
), i
->timestamp
.realtime
);
573 printf("\t Since: %s; %s\n", s2
, s1
);
575 printf("\t Since: %s\n", s2
);
578 _cleanup_free_
char *t
= NULL
;
580 printf("\t Leader: %u", (unsigned) i
->leader
);
582 get_process_comm(i
->leader
, &t
);
590 printf("\t Service: %s", i
->service
);
593 printf("; class %s", i
->class);
597 printf("\t Class: %s\n", i
->class);
599 if (i
->root_directory
)
600 printf("\t Root: %s\n", i
->root_directory
);
602 if (i
->n_netif
> 0) {
605 fputs("\t Iface:", stdout
);
607 for (c
= 0; c
< i
->n_netif
; c
++) {
608 char name
[IF_NAMESIZE
+1] = "";
610 if (if_indextoname(i
->netif
[c
], name
)) {
619 printf(" %i", i
->netif
[c
]);
625 if (print_addresses(bus
, i
->name
, ifi
,
628 ALL_IP_ADDRESSES
) > 0)
631 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
633 print_uid_shift(bus
, i
->name
);
636 printf("\t Unit: %s\n", i
->unit
);
637 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
639 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
641 show_journal_by_unit(
646 i
->timestamp
.monotonic
,
649 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
650 SD_JOURNAL_LOCAL_ONLY
,
656 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
657 MachineStatusInfo
*i
= userdata
;
662 assert_cc(sizeof(int32_t) == sizeof(int));
663 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
669 i
->n_netif
= l
/ sizeof(int32_t);
670 i
->netif
= memdup(v
, l
);
677 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
679 static const struct bus_properties_map map
[] = {
680 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
681 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
682 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
683 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
684 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
685 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
686 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
687 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
688 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
689 { "NetworkInterfaces", "ai", map_netif
, 0 },
693 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
694 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
695 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
703 r
= bus_map_all_properties(bus
,
704 "org.freedesktop.machine1",
712 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
718 print_machine_status_info(bus
, &info
);
723 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
735 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_value
, arg_all
, NULL
);
737 log_error_errno(r
, "Could not get properties: %m");
742 static int show_machine(int argc
, char *argv
[], void *userdata
) {
744 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
745 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
746 bool properties
, new_line
= false;
747 sd_bus
*bus
= userdata
;
752 properties
= !strstr(argv
[0], "status");
754 (void) pager_open(arg_no_pager
, false);
756 if (properties
&& argc
<= 1) {
758 /* If no argument is specified, inspect the manager
760 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
765 for (i
= 1; i
< argc
; i
++) {
766 const char *path
= NULL
;
768 r
= sd_bus_call_method(bus
,
769 "org.freedesktop.machine1",
770 "/org/freedesktop/machine1",
771 "org.freedesktop.machine1.Manager",
777 return log_error_errno(r
, "Could not get path to machine: %s", bus_error_message(&error
, -r
));
779 r
= sd_bus_message_read(reply
, "o", &path
);
781 return bus_log_parse_error(r
);
784 r
= show_machine_properties(bus
, path
, &new_line
);
786 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
792 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
793 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
797 r
= sd_bus_call_method(
799 "org.freedesktop.machine1",
800 "/org/freedesktop/machine1",
801 "org.freedesktop.machine1.Manager",
803 NULL
, &reply
, "s", name
);
807 r
= sd_bus_message_read(reply
, "s", &hn
);
812 printf("\tHostname: %s\n", hn
);
817 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
818 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
819 sd_id128_t id
= SD_ID128_NULL
;
824 r
= sd_bus_call_method(
826 "org.freedesktop.machine1",
827 "/org/freedesktop/machine1",
828 "org.freedesktop.machine1.Manager",
830 NULL
, &reply
, "s", name
);
834 r
= sd_bus_message_read_array(reply
, 'y', &p
, &size
);
838 if (size
== sizeof(sd_id128_t
))
839 memcpy(&id
, p
, size
);
841 if (!sd_id128_is_null(id
))
842 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
847 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
848 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
851 r
= sd_bus_call_method(
853 "org.freedesktop.machine1",
854 "/org/freedesktop/machine1",
855 "org.freedesktop.machine1.Manager",
856 "GetImageMachineInfo",
857 NULL
, &reply
, "s", name
);
861 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
868 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
874 if (streq(p
, "DEPLOYMENT"))
875 printf(" Deployment: %s\n", q
);
878 r
= sd_bus_message_exit_container(reply
);
885 typedef struct ImageStatusInfo
{
894 uint64_t usage_exclusive
;
895 uint64_t limit_exclusive
;
898 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
899 char ts_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
900 char ts_absolute
[FORMAT_TIMESTAMP_MAX
];
901 char bs
[FORMAT_BYTES_MAX
];
902 char bs_exclusive
[FORMAT_BYTES_MAX
];
903 const char *s1
, *s2
, *s3
, *s4
;
909 fputs(i
->name
, stdout
);
914 printf("\t Type: %s\n", i
->type
);
917 printf("\t Path: %s\n", i
->path
);
919 (void) print_image_hostname(bus
, i
->name
);
920 (void) print_image_machine_id(bus
, i
->name
);
921 (void) print_image_machine_info(bus
, i
->name
);
923 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
925 printf("\t RO: %s%s%s\n",
926 i
->read_only
? ansi_highlight_red() : "",
927 i
->read_only
? "read-only" : "writable",
928 i
->read_only
? ansi_normal() : "");
930 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->crtime
);
931 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->crtime
);
933 printf("\t Created: %s; %s\n", s2
, s1
);
935 printf("\t Created: %s\n", s2
);
937 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->mtime
);
938 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->mtime
);
940 printf("\tModified: %s; %s\n", s2
, s1
);
942 printf("\tModified: %s\n", s2
);
944 s3
= format_bytes(bs
, sizeof(bs
), i
->usage
);
945 s4
= i
->usage_exclusive
!= i
->usage
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->usage_exclusive
) : NULL
;
947 printf("\t Usage: %s (exclusive: %s)\n", s3
, s4
);
949 printf("\t Usage: %s\n", s3
);
951 s3
= format_bytes(bs
, sizeof(bs
), i
->limit
);
952 s4
= i
->limit_exclusive
!= i
->limit
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->limit_exclusive
) : NULL
;
954 printf("\t Limit: %s (exclusive: %s)\n", s3
, s4
);
956 printf("\t Limit: %s\n", s3
);
959 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
961 static const struct bus_properties_map map
[] = {
962 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
963 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
964 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
965 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
966 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
967 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
968 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
969 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
970 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
971 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
975 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
976 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
977 ImageStatusInfo info
= {};
984 r
= bus_map_all_properties(bus
,
985 "org.freedesktop.machine1",
988 BUS_MAP_BOOLEAN_AS_BOOL
,
993 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
999 print_image_status_info(bus
, &info
);
1004 typedef struct PoolStatusInfo
{
1010 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
1011 char bs
[FORMAT_BYTES_MAX
], *s
;
1014 printf("\t Path: %s\n", i
->path
);
1016 s
= format_bytes(bs
, sizeof(bs
), i
->usage
);
1018 printf("\t Usage: %s\n", s
);
1020 s
= format_bytes(bs
, sizeof(bs
), i
->limit
);
1022 printf("\t Limit: %s\n", s
);
1025 static int show_pool_info(sd_bus
*bus
) {
1027 static const struct bus_properties_map map
[] = {
1028 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
1029 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
1030 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
1034 PoolStatusInfo info
= {
1035 .usage
= (uint64_t) -1,
1036 .limit
= (uint64_t) -1,
1039 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1040 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1045 r
= bus_map_all_properties(bus
,
1046 "org.freedesktop.machine1",
1047 "/org/freedesktop/machine1",
1054 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1056 print_pool_status_info(bus
, &info
);
1061 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
1073 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_value
, arg_all
, NULL
);
1075 log_error_errno(r
, "Could not get properties: %m");
1080 static int show_image(int argc
, char *argv
[], void *userdata
) {
1082 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1083 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1084 bool properties
, new_line
= false;
1085 sd_bus
*bus
= userdata
;
1090 properties
= !strstr(argv
[0], "status");
1092 (void) pager_open(arg_no_pager
, false);
1096 /* If no argument is specified, inspect the manager
1100 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1102 r
= show_pool_info(bus
);
1107 for (i
= 1; i
< argc
; i
++) {
1108 const char *path
= NULL
;
1110 r
= sd_bus_call_method(
1112 "org.freedesktop.machine1",
1113 "/org/freedesktop/machine1",
1114 "org.freedesktop.machine1.Manager",
1120 return log_error_errno(r
, "Could not get path to image: %s", bus_error_message(&error
, -r
));
1122 r
= sd_bus_message_read(reply
, "o", &path
);
1124 return bus_log_parse_error(r
);
1127 r
= show_image_properties(bus
, path
, &new_line
);
1129 r
= show_image_info(bus
, path
, &new_line
);
1135 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1136 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1137 sd_bus
*bus
= userdata
;
1142 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1145 arg_kill_who
= "all";
1147 for (i
= 1; i
< argc
; i
++) {
1148 r
= sd_bus_call_method(
1150 "org.freedesktop.machine1",
1151 "/org/freedesktop/machine1",
1152 "org.freedesktop.machine1.Manager",
1156 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1158 return log_error_errno(r
, "Could not kill machine: %s", bus_error_message(&error
, -r
));
1164 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1165 arg_kill_who
= "leader";
1166 arg_signal
= SIGINT
; /* sysvinit + systemd */
1168 return kill_machine(argc
, argv
, userdata
);
1171 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1172 arg_kill_who
= "leader";
1173 arg_signal
= SIGRTMIN
+4; /* only systemd */
1175 return kill_machine(argc
, argv
, userdata
);
1178 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1179 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1180 sd_bus
*bus
= userdata
;
1185 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1187 for (i
= 1; i
< argc
; i
++) {
1188 r
= sd_bus_call_method(
1190 "org.freedesktop.machine1",
1191 "/org/freedesktop/machine1",
1192 "org.freedesktop.machine1.Manager",
1198 return log_error_errno(r
, "Could not terminate machine: %s", bus_error_message(&error
, -r
));
1204 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1205 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1206 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1207 _cleanup_free_
char *abs_host_path
= NULL
;
1208 char *dest
, *host_path
, *container_path
;
1209 sd_bus
*bus
= userdata
;
1215 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1217 copy_from
= streq(argv
[0], "copy-from");
1218 dest
= argv
[3] ?: argv
[2];
1219 host_path
= copy_from
? dest
: argv
[2];
1220 container_path
= copy_from
? argv
[2] : dest
;
1222 if (!path_is_absolute(host_path
)) {
1223 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1225 return log_error_errno(r
, "Failed to make path absolute: %m");
1227 host_path
= abs_host_path
;
1230 r
= sd_bus_message_new_method_call(
1233 "org.freedesktop.machine1",
1234 "/org/freedesktop/machine1",
1235 "org.freedesktop.machine1.Manager",
1236 copy_from
? "CopyFromMachine" : "CopyToMachine");
1238 return bus_log_create_error(r
);
1240 r
= sd_bus_message_append(
1244 copy_from
? container_path
: host_path
,
1245 copy_from
? host_path
: container_path
);
1247 return bus_log_create_error(r
);
1249 /* This is a slow operation, hence turn off any method call timeouts */
1250 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1252 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1257 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1258 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1259 sd_bus
*bus
= userdata
;
1264 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1266 r
= sd_bus_call_method(
1268 "org.freedesktop.machine1",
1269 "/org/freedesktop/machine1",
1270 "org.freedesktop.machine1.Manager",
1281 return log_error_errno(r
, "Failed to bind mount: %s", bus_error_message(&error
, -r
));
1286 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1287 PTYForward
** forward
= (PTYForward
**) userdata
;
1294 /* If the forwarder is already initialized, tell it to
1295 * exit on the next vhangup(), so that we still flush
1296 * out what might be queued and exit then. */
1298 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1302 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1305 /* On error, or when the forwarder is not initialized yet, quit immediately */
1306 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1310 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1316 assert(master
>= 0);
1319 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1322 if (streq(name
, ".host"))
1323 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1325 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1328 sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1329 sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1331 r
= pty_forward_new(event
, master
, flags
, forward
);
1333 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1335 r
= sd_event_loop(event
);
1337 return log_error_errno(r
, "Failed to run event loop: %m");
1339 pty_forward_get_last_char(*forward
, &last_char
);
1342 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1343 pty_forward_get_ignore_vhangup(*forward
) == 0;
1345 *forward
= pty_forward_free(*forward
);
1347 if (last_char
!= '\n')
1348 fputc('\n', stdout
);
1352 log_info("Machine %s terminated.", name
);
1353 else if (streq(name
, ".host"))
1354 log_info("Connection to the local host terminated.");
1356 log_info("Connection to machine %s terminated.", name
);
1359 sd_event_get_exit_code(event
, &ret
);
1363 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1365 * Whatever is specified in the spec takes priority over global arguments.
1368 const char *_machine
= NULL
;
1373 at
= strchr(spec
, '@');
1376 /* Do the same as ssh and refuse "@host". */
1380 _uid
= strndup(spec
, at
- spec
);
1387 if (arg_uid
&& !_uid
) {
1388 _uid
= strdup(arg_uid
);
1394 *machine
= isempty(_machine
) ? ".host" : _machine
;
1398 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1399 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1400 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1401 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1402 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1403 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1405 sd_bus
*bus
= userdata
;
1406 const char *match
, *machine
;
1410 if (!strv_isempty(arg_setenv
) || arg_uid
) {
1411 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1415 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1416 log_error("Login only supported on local machines.");
1420 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1422 r
= sd_event_default(&event
);
1424 return log_error_errno(r
, "Failed to get event loop: %m");
1426 r
= sd_bus_attach_event(bus
, event
, 0);
1428 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1430 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1432 match
= strjoina("type='signal',"
1433 "sender='org.freedesktop.machine1',"
1434 "path='/org/freedesktop/machine1',",
1435 "interface='org.freedesktop.machine1.Manager',"
1436 "member='MachineRemoved',"
1437 "arg0='", machine
, "'");
1439 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1441 return log_error_errno(r
, "Failed to request machine removal match: %m");
1443 r
= sd_bus_call_method(
1445 "org.freedesktop.machine1",
1446 "/org/freedesktop/machine1",
1447 "org.freedesktop.machine1.Manager",
1453 return log_error_errno(r
, "Failed to get login PTY: %s", bus_error_message(&error
, -r
));
1455 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1457 return bus_log_parse_error(r
);
1459 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1462 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1463 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1464 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1465 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1466 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1467 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1469 sd_bus
*bus
= userdata
;
1470 const char *match
, *machine
, *path
;
1471 _cleanup_free_
char *uid
= NULL
;
1475 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1476 log_error("Shell only supported on local machines.");
1480 /* Pass $TERM to shell session, if not explicitly specified. */
1481 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1484 t
= strv_find_prefix(environ
, "TERM=");
1486 if (strv_extend(&arg_setenv
, t
) < 0)
1491 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1493 r
= sd_event_default(&event
);
1495 return log_error_errno(r
, "Failed to get event loop: %m");
1497 r
= sd_bus_attach_event(bus
, event
, 0);
1499 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1501 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1503 return log_error_errno(r
, "Failed to parse machine specification: %m");
1505 match
= strjoina("type='signal',"
1506 "sender='org.freedesktop.machine1',"
1507 "path='/org/freedesktop/machine1',",
1508 "interface='org.freedesktop.machine1.Manager',"
1509 "member='MachineRemoved',"
1510 "arg0='", machine
, "'");
1512 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1514 return log_error_errno(r
, "Failed to request machine removal match: %m");
1516 r
= sd_bus_message_new_method_call(
1519 "org.freedesktop.machine1",
1520 "/org/freedesktop/machine1",
1521 "org.freedesktop.machine1.Manager",
1522 "OpenMachineShell");
1524 return bus_log_create_error(r
);
1526 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1528 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1530 return bus_log_create_error(r
);
1532 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1534 return bus_log_create_error(r
);
1536 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1538 return bus_log_create_error(r
);
1540 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1542 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, -r
));
1544 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1546 return bus_log_parse_error(r
);
1548 return process_forward(event
, &forward
, master
, 0, machine
);
1551 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1552 sd_bus
*bus
= userdata
;
1557 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1559 for (i
= 1; i
< argc
; i
++) {
1560 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1561 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1563 r
= sd_bus_message_new_method_call(
1566 "org.freedesktop.machine1",
1567 "/org/freedesktop/machine1",
1568 "org.freedesktop.machine1.Manager",
1571 return bus_log_create_error(r
);
1573 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1575 return bus_log_create_error(r
);
1577 /* This is a slow operation, hence turn off any method call timeouts */
1578 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1580 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1586 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1587 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1588 sd_bus
*bus
= userdata
;
1593 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1595 r
= sd_bus_call_method(
1597 "org.freedesktop.machine1",
1598 "/org/freedesktop/machine1",
1599 "org.freedesktop.machine1.Manager",
1603 "ss", argv
[1], argv
[2]);
1605 return log_error_errno(r
, "Could not rename image: %s", bus_error_message(&error
, -r
));
1610 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1611 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1612 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1613 sd_bus
*bus
= userdata
;
1618 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1620 r
= sd_bus_message_new_method_call(
1623 "org.freedesktop.machine1",
1624 "/org/freedesktop/machine1",
1625 "org.freedesktop.machine1.Manager",
1628 return bus_log_create_error(r
);
1630 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1632 return bus_log_create_error(r
);
1634 /* This is a slow operation, hence turn off any method call timeouts */
1635 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1637 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1642 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1643 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1644 sd_bus
*bus
= userdata
;
1650 b
= parse_boolean(argv
[2]);
1652 log_error("Failed to parse boolean argument: %s", argv
[2]);
1657 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1659 r
= sd_bus_call_method(
1661 "org.freedesktop.machine1",
1662 "/org/freedesktop/machine1",
1663 "org.freedesktop.machine1.Manager",
1664 "MarkImageReadOnly",
1669 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, -r
));
1674 static int image_exists(sd_bus
*bus
, const char *name
) {
1675 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1681 r
= sd_bus_call_method(
1683 "org.freedesktop.machine1",
1684 "/org/freedesktop/machine1",
1685 "org.freedesktop.machine1.Manager",
1691 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1694 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, -r
));
1700 static int make_service_name(const char *name
, char **ret
) {
1706 if (!machine_name_is_valid(name
)) {
1707 log_error("Invalid machine name %s.", name
);
1711 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1713 return log_error_errno(r
, "Failed to build unit name: %m");
1718 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1719 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1720 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1721 sd_bus
*bus
= userdata
;
1726 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1728 r
= bus_wait_for_jobs_new(bus
, &w
);
1732 for (i
= 1; i
< argc
; i
++) {
1733 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1734 _cleanup_free_
char *unit
= NULL
;
1737 r
= make_service_name(argv
[i
], &unit
);
1741 r
= image_exists(bus
, argv
[i
]);
1745 log_error("Machine image '%s' does not exist.", argv
[1]);
1749 r
= sd_bus_call_method(
1751 "org.freedesktop.systemd1",
1752 "/org/freedesktop/systemd1",
1753 "org.freedesktop.systemd1.Manager",
1757 "ss", unit
, "fail");
1759 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, -r
));
1761 r
= sd_bus_message_read(reply
, "o", &object
);
1763 return bus_log_parse_error(r
);
1765 r
= bus_wait_for_jobs_add(w
, object
);
1770 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1777 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1778 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1779 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1780 UnitFileChange
*changes
= NULL
;
1781 size_t n_changes
= 0;
1782 const char *method
= NULL
;
1783 sd_bus
*bus
= userdata
;
1788 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1790 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1792 r
= sd_bus_message_new_method_call(
1795 "org.freedesktop.systemd1",
1796 "/org/freedesktop/systemd1",
1797 "org.freedesktop.systemd1.Manager",
1800 return bus_log_create_error(r
);
1802 r
= sd_bus_message_open_container(m
, 'a', "s");
1804 return bus_log_create_error(r
);
1806 for (i
= 1; i
< argc
; i
++) {
1807 _cleanup_free_
char *unit
= NULL
;
1809 r
= make_service_name(argv
[i
], &unit
);
1813 r
= image_exists(bus
, argv
[i
]);
1817 log_error("Machine image '%s' does not exist.", argv
[1]);
1821 r
= sd_bus_message_append(m
, "s", unit
);
1823 return bus_log_create_error(r
);
1826 r
= sd_bus_message_close_container(m
);
1828 return bus_log_create_error(r
);
1830 if (streq(argv
[0], "enable"))
1831 r
= sd_bus_message_append(m
, "bb", false, false);
1833 r
= sd_bus_message_append(m
, "b", false);
1835 return bus_log_create_error(r
);
1837 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1839 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1841 if (streq(argv
[0], "enable")) {
1842 r
= sd_bus_message_read(reply
, "b", NULL
);
1844 return bus_log_parse_error(r
);
1847 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1851 r
= sd_bus_call_method(
1853 "org.freedesktop.systemd1",
1854 "/org/freedesktop/systemd1",
1855 "org.freedesktop.systemd1.Manager",
1861 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1868 unit_file_changes_free(changes
, n_changes
);
1873 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1874 const char **our_path
= userdata
, *line
;
1881 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1883 bus_log_parse_error(r
);
1887 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1890 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1893 log_full(priority
, "%s", line
);
1897 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1898 const char **our_path
= userdata
, *path
, *result
;
1905 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1907 bus_log_parse_error(r
);
1911 if (!streq_ptr(*our_path
, path
))
1914 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1918 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1923 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1925 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1929 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1930 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1931 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1932 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1933 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1934 const char *path
= NULL
;
1941 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1943 r
= sd_event_default(&event
);
1945 return log_error_errno(r
, "Failed to get event loop: %m");
1947 r
= sd_bus_attach_event(bus
, event
, 0);
1949 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1951 r
= sd_bus_match_signal_async(
1954 "org.freedesktop.import1",
1955 "/org/freedesktop/import1",
1956 "org.freedesktop.import1.Manager",
1958 match_transfer_removed
, NULL
, &path
);
1960 return log_error_errno(r
, "Failed to request match: %m");
1962 r
= sd_bus_match_signal_async(
1965 "org.freedesktop.import1",
1967 "org.freedesktop.import1.Transfer",
1969 match_log_message
, NULL
, &path
);
1971 return log_error_errno(r
, "Failed to request match: %m");
1973 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1975 return log_error_errno(r
, "Failed to transfer image: %s", bus_error_message(&error
, -r
));
1977 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1979 return bus_log_parse_error(r
);
1981 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1984 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1986 sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1987 sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1989 r
= sd_event_loop(event
);
1991 return log_error_errno(r
, "Failed to run event loop: %m");
1996 static int import_tar(int argc
, char *argv
[], void *userdata
) {
1997 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1998 _cleanup_free_
char *ll
= NULL
;
1999 _cleanup_close_
int fd
= -1;
2000 const char *local
= NULL
, *path
= NULL
;
2001 sd_bus
*bus
= userdata
;
2008 if (isempty(path
) || streq(path
, "-"))
2014 local
= basename(path
);
2015 if (isempty(local
) || streq(local
, "-"))
2019 log_error("Need either path or local name.");
2023 r
= tar_strip_suffixes(local
, &ll
);
2029 if (!machine_name_is_valid(local
)) {
2030 log_error("Local name %s is not a suitable machine name.", local
);
2035 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2037 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2040 r
= sd_bus_message_new_method_call(
2043 "org.freedesktop.import1",
2044 "/org/freedesktop/import1",
2045 "org.freedesktop.import1.Manager",
2048 return bus_log_create_error(r
);
2050 r
= sd_bus_message_append(
2053 fd
>= 0 ? fd
: STDIN_FILENO
,
2058 return bus_log_create_error(r
);
2060 return transfer_image_common(bus
, m
);
2063 static int import_raw(int argc
, char *argv
[], void *userdata
) {
2064 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2065 _cleanup_free_
char *ll
= NULL
;
2066 _cleanup_close_
int fd
= -1;
2067 const char *local
= NULL
, *path
= NULL
;
2068 sd_bus
*bus
= userdata
;
2075 if (isempty(path
) || streq(path
, "-"))
2081 local
= basename(path
);
2082 if (isempty(local
) || streq(local
, "-"))
2086 log_error("Need either path or local name.");
2090 r
= raw_strip_suffixes(local
, &ll
);
2096 if (!machine_name_is_valid(local
)) {
2097 log_error("Local name %s is not a suitable machine name.", local
);
2102 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2104 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2107 r
= sd_bus_message_new_method_call(
2110 "org.freedesktop.import1",
2111 "/org/freedesktop/import1",
2112 "org.freedesktop.import1.Manager",
2115 return bus_log_create_error(r
);
2117 r
= sd_bus_message_append(
2120 fd
>= 0 ? fd
: STDIN_FILENO
,
2125 return bus_log_create_error(r
);
2127 return transfer_image_common(bus
, m
);
2130 static void determine_compression_from_filename(const char *p
) {
2137 if (endswith(p
, ".xz"))
2139 else if (endswith(p
, ".gz"))
2140 arg_format
= "gzip";
2141 else if (endswith(p
, ".bz2"))
2142 arg_format
= "bzip2";
2145 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2146 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2147 _cleanup_close_
int fd
= -1;
2148 const char *local
= NULL
, *path
= NULL
;
2149 sd_bus
*bus
= userdata
;
2155 if (!machine_name_is_valid(local
)) {
2156 log_error("Machine name %s is not valid.", local
);
2162 if (isempty(path
) || streq(path
, "-"))
2166 determine_compression_from_filename(path
);
2168 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2170 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2173 r
= sd_bus_message_new_method_call(
2176 "org.freedesktop.import1",
2177 "/org/freedesktop/import1",
2178 "org.freedesktop.import1.Manager",
2181 return bus_log_create_error(r
);
2183 r
= sd_bus_message_append(
2187 fd
>= 0 ? fd
: STDOUT_FILENO
,
2190 return bus_log_create_error(r
);
2192 return transfer_image_common(bus
, m
);
2195 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2196 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2197 _cleanup_close_
int fd
= -1;
2198 const char *local
= NULL
, *path
= NULL
;
2199 sd_bus
*bus
= userdata
;
2205 if (!machine_name_is_valid(local
)) {
2206 log_error("Machine name %s is not valid.", local
);
2212 if (isempty(path
) || streq(path
, "-"))
2216 determine_compression_from_filename(path
);
2218 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2220 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2223 r
= sd_bus_message_new_method_call(
2226 "org.freedesktop.import1",
2227 "/org/freedesktop/import1",
2228 "org.freedesktop.import1.Manager",
2231 return bus_log_create_error(r
);
2233 r
= sd_bus_message_append(
2237 fd
>= 0 ? fd
: STDOUT_FILENO
,
2240 return bus_log_create_error(r
);
2242 return transfer_image_common(bus
, m
);
2245 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2246 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2247 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2248 const char *local
, *remote
;
2249 sd_bus
*bus
= userdata
;
2255 if (!http_url_is_valid(remote
)) {
2256 log_error("URL '%s' is not valid.", remote
);
2263 r
= import_url_last_component(remote
, &l
);
2265 return log_error_errno(r
, "Failed to get final component of URL: %m");
2270 if (isempty(local
) || streq(local
, "-"))
2274 r
= tar_strip_suffixes(local
, &ll
);
2280 if (!machine_name_is_valid(local
)) {
2281 log_error("Local name %s is not a suitable machine name.", local
);
2286 r
= sd_bus_message_new_method_call(
2289 "org.freedesktop.import1",
2290 "/org/freedesktop/import1",
2291 "org.freedesktop.import1.Manager",
2294 return bus_log_create_error(r
);
2296 r
= sd_bus_message_append(
2301 import_verify_to_string(arg_verify
),
2304 return bus_log_create_error(r
);
2306 return transfer_image_common(bus
, m
);
2309 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2310 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2311 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2312 const char *local
, *remote
;
2313 sd_bus
*bus
= userdata
;
2319 if (!http_url_is_valid(remote
)) {
2320 log_error("URL '%s' is not valid.", remote
);
2327 r
= import_url_last_component(remote
, &l
);
2329 return log_error_errno(r
, "Failed to get final component of URL: %m");
2334 if (isempty(local
) || streq(local
, "-"))
2338 r
= raw_strip_suffixes(local
, &ll
);
2344 if (!machine_name_is_valid(local
)) {
2345 log_error("Local name %s is not a suitable machine name.", local
);
2350 r
= sd_bus_message_new_method_call(
2353 "org.freedesktop.import1",
2354 "/org/freedesktop/import1",
2355 "org.freedesktop.import1.Manager",
2358 return bus_log_create_error(r
);
2360 r
= sd_bus_message_append(
2365 import_verify_to_string(arg_verify
),
2368 return bus_log_create_error(r
);
2370 return transfer_image_common(bus
, m
);
2373 typedef struct TransferInfo
{
2381 static int compare_transfer_info(const TransferInfo
*a
, const TransferInfo
*b
) {
2382 return strcmp(a
->local
, b
->local
);
2385 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2386 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2387 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2388 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2389 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2390 size_t n_transfers
= 0, n_allocated
= 0, j
;
2391 const char *type
, *remote
, *local
;
2392 sd_bus
*bus
= userdata
;
2393 uint32_t id
, max_id
= 0;
2397 (void) pager_open(arg_no_pager
, false);
2399 r
= sd_bus_call_method(bus
,
2400 "org.freedesktop.import1",
2401 "/org/freedesktop/import1",
2402 "org.freedesktop.import1.Manager",
2408 return log_error_errno(r
, "Could not get transfers: %s", bus_error_message(&error
, -r
));
2410 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2412 return bus_log_parse_error(r
);
2414 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2417 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2420 transfers
[n_transfers
].id
= id
;
2421 transfers
[n_transfers
].type
= type
;
2422 transfers
[n_transfers
].remote
= remote
;
2423 transfers
[n_transfers
].local
= local
;
2424 transfers
[n_transfers
].progress
= progress
;
2444 return bus_log_parse_error(r
);
2446 r
= sd_bus_message_exit_container(reply
);
2448 return bus_log_parse_error(r
);
2450 typesafe_qsort(transfers
, n_transfers
, compare_transfer_info
);
2452 if (arg_legend
&& n_transfers
> 0)
2453 printf("%-*s %-*s %-*s %-*s %-*s\n",
2454 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2456 (int) max_type
, "TYPE",
2457 (int) max_local
, "LOCAL",
2458 (int) max_remote
, "REMOTE");
2460 for (j
= 0; j
< n_transfers
; j
++)
2461 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2462 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2463 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2464 (int) max_type
, transfers
[j
].type
,
2465 (int) max_local
, transfers
[j
].local
,
2466 (int) max_remote
, transfers
[j
].remote
);
2469 if (n_transfers
> 0)
2470 printf("\n%zu transfers listed.\n", n_transfers
);
2472 printf("No transfers.\n");
2478 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2479 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2480 sd_bus
*bus
= userdata
;
2485 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2487 for (i
= 1; i
< argc
; i
++) {
2490 r
= safe_atou32(argv
[i
], &id
);
2492 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2494 r
= sd_bus_call_method(
2496 "org.freedesktop.import1",
2497 "/org/freedesktop/import1",
2498 "org.freedesktop.import1.Manager",
2504 return log_error_errno(r
, "Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2510 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2511 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2512 sd_bus
*bus
= userdata
;
2516 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2518 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2519 limit
= (uint64_t) -1;
2521 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2523 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2527 /* With two arguments changes the quota limit of the
2528 * specified image */
2529 r
= sd_bus_call_method(
2531 "org.freedesktop.machine1",
2532 "/org/freedesktop/machine1",
2533 "org.freedesktop.machine1.Manager",
2537 "st", argv
[1], limit
);
2539 /* With one argument changes the pool quota limit */
2540 r
= sd_bus_call_method(
2542 "org.freedesktop.machine1",
2543 "/org/freedesktop/machine1",
2544 "org.freedesktop.machine1.Manager",
2551 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2556 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2557 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2558 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2559 uint64_t usage
, total
= 0;
2560 char fb
[FORMAT_BYTES_MAX
];
2561 sd_bus
*bus
= userdata
;
2566 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2568 r
= sd_bus_message_new_method_call(
2571 "org.freedesktop.machine1",
2572 "/org/freedesktop/machine1",
2573 "org.freedesktop.machine1.Manager",
2576 return bus_log_create_error(r
);
2578 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2580 return bus_log_create_error(r
);
2582 /* This is a slow operation, hence permit a longer time for completion. */
2583 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2585 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2587 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2589 return bus_log_parse_error(r
);
2591 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2592 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2593 name
, format_bytes(fb
, sizeof(fb
), usage
));
2599 r
= sd_bus_message_exit_container(reply
);
2601 return bus_log_parse_error(r
);
2603 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2604 c
, format_bytes(fb
, sizeof(fb
), total
));
2609 static int help(int argc
, char *argv
[], void *userdata
) {
2610 _cleanup_free_
char *link
= NULL
;
2613 (void) pager_open(arg_no_pager
, false);
2615 r
= terminal_urlify_man("machinectl", "1", &link
);
2619 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2620 "Send control commands to or query the virtual machine and container\n"
2621 "registration manager.\n\n"
2622 " -h --help Show this help\n"
2623 " --version Show package version\n"
2624 " --no-pager Do not pipe output into a pager\n"
2625 " --no-legend Do not show the headers and footers\n"
2626 " --no-ask-password Do not ask for system passwords\n"
2627 " -H --host=[USER@]HOST Operate on remote host\n"
2628 " -M --machine=CONTAINER Operate on local container\n"
2629 " -p --property=NAME Show only properties by this name\n"
2630 " -q --quiet Suppress output\n"
2631 " -a --all Show all properties, including empty ones\n"
2632 " --value When showing properties, only print the value\n"
2633 " -l --full Do not ellipsize output\n"
2634 " --kill-who=WHO Who to send signal to\n"
2635 " -s --signal=SIGNAL Which signal to send\n"
2636 " --uid=USER Specify user ID to invoke shell as\n"
2637 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2638 " --read-only Create read-only bind mount\n"
2639 " --mkdir Create directory before bind mounting, if missing\n"
2640 " -n --lines=INTEGER Number of journal entries to show\n"
2641 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2642 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2643 " short-iso, short-iso-precise, short-full,\n"
2644 " short-monotonic, short-unix, verbose, export,\n"
2645 " json, json-pretty, json-sse, cat)\n"
2646 " --verify=MODE Verification mode for downloaded images (no,\n"
2647 " checksum, signature)\n"
2648 " --force Download image even if already exists\n\n"
2649 "Machine Commands:\n"
2650 " list List running VMs and containers\n"
2651 " status NAME... Show VM/container details\n"
2652 " show [NAME...] Show properties of one or more VMs/containers\n"
2653 " start NAME... Start container as a service\n"
2654 " login [NAME] Get a login prompt in a container or on the\n"
2656 " shell [[USER@]NAME [COMMAND...]]\n"
2657 " Invoke a shell (or other command) in a container\n"
2658 " or on the local host\n"
2659 " enable NAME... Enable automatic container start at boot\n"
2660 " disable NAME... Disable automatic container start at boot\n"
2661 " poweroff NAME... Power off one or more containers\n"
2662 " reboot NAME... Reboot one or more containers\n"
2663 " terminate NAME... Terminate one or more VMs/containers\n"
2664 " kill NAME... Send signal to processes of a VM/container\n"
2665 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2666 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2667 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2669 " list-images Show available container and VM images\n"
2670 " image-status [NAME...] Show image details\n"
2671 " show-image [NAME...] Show properties of image\n"
2672 " clone NAME NAME Clone an image\n"
2673 " rename NAME NAME Rename an image\n"
2674 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2675 " remove NAME... Remove an image\n"
2676 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2677 " clean Remove hidden (or all) images\n\n"
2678 "Image Transfer Commands:\n"
2679 " pull-tar URL [NAME] Download a TAR container image\n"
2680 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2681 " import-tar FILE [NAME] Import a local TAR container image\n"
2682 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2683 " export-tar NAME [FILE] Export a TAR container image locally\n"
2684 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2685 " list-transfers Show list of downloads in progress\n"
2686 " cancel-transfer Cancel a download\n"
2687 "\nSee the %s for details.\n"
2688 , program_invocation_short_name
2695 static int parse_argv(int argc
, char *argv
[]) {
2698 ARG_VERSION
= 0x100,
2705 ARG_NO_ASK_PASSWORD
,
2713 static const struct option options
[] = {
2714 { "help", no_argument
, NULL
, 'h' },
2715 { "version", no_argument
, NULL
, ARG_VERSION
},
2716 { "property", required_argument
, NULL
, 'p' },
2717 { "all", no_argument
, NULL
, 'a' },
2718 { "value", no_argument
, NULL
, ARG_VALUE
},
2719 { "full", no_argument
, NULL
, 'l' },
2720 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2721 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2722 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2723 { "signal", required_argument
, NULL
, 's' },
2724 { "host", required_argument
, NULL
, 'H' },
2725 { "machine", required_argument
, NULL
, 'M' },
2726 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2727 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2728 { "quiet", no_argument
, NULL
, 'q' },
2729 { "lines", required_argument
, NULL
, 'n' },
2730 { "output", required_argument
, NULL
, 'o' },
2731 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2732 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2733 { "force", no_argument
, NULL
, ARG_FORCE
},
2734 { "format", required_argument
, NULL
, ARG_FORMAT
},
2735 { "uid", required_argument
, NULL
, ARG_UID
},
2736 { "setenv", required_argument
, NULL
, 'E' },
2737 { "max-addresses", required_argument
, NULL
, ARG_NUMBER_IPS
},
2741 bool reorder
= false;
2742 int c
, r
, shell
= -1;
2748 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2750 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2756 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2757 * non-option argument was discovered. */
2761 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2762 * for switches after the main verb. However, for "shell" we really don't want that, since we
2763 * want that switches specified after the machine name are passed to the program to execute,
2764 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2765 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2766 * non-option parameter. If it's the verb "shell" we remember its position and continue
2767 * processing options. In this case, as soon as we hit the next non-option argument we found
2768 * the machine name, and stop further processing. If the first non-option argument is any other
2769 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2773 /* If we already found the "shell" verb on the command line, and now found the next
2774 * non-option argument, then this is the machine name and we should stop processing
2775 * further arguments. */
2776 optind
--; /* don't process this argument, go one step back */
2779 if (streq(optarg
, "shell"))
2780 /* Remember the position of the "shell" verb, and continue processing normally. */
2785 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2786 * processing normally. */
2789 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2790 * at least once with a reset option index. Hence, let's reset the option index here,
2791 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2792 * likely already processed it), and the bump the option index so that we read the
2793 * intended argument again. */
2794 saved_optind
= optind
;
2796 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2797 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2803 return help(0, NULL
, NULL
);
2809 r
= strv_extend(&arg_property
, optarg
);
2813 /* If the user asked for a particular
2814 * property, show it to him, even if it is
2832 if (safe_atou(optarg
, &arg_lines
) < 0) {
2833 log_error("Failed to parse lines '%s'", optarg
);
2839 if (streq(optarg
, "help")) {
2840 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2844 arg_output
= output_mode_from_string(optarg
);
2845 if (arg_output
< 0) {
2846 log_error("Unknown output '%s'.", optarg
);
2852 arg_no_pager
= true;
2860 arg_kill_who
= optarg
;
2864 if (streq(optarg
, "help")) {
2865 DUMP_STRING_TABLE(signal
, int, _NSIG
);
2869 arg_signal
= signal_from_string(optarg
);
2870 if (arg_signal
< 0) {
2871 log_error("Failed to parse signal string %s.", optarg
);
2876 case ARG_NO_ASK_PASSWORD
:
2877 arg_ask_password
= false;
2881 arg_transport
= BUS_TRANSPORT_REMOTE
;
2886 arg_transport
= BUS_TRANSPORT_MACHINE
;
2891 arg_read_only
= true;
2903 if (streq(optarg
, "help")) {
2904 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2908 arg_verify
= import_verify_from_string(optarg
);
2909 if (arg_verify
< 0) {
2910 log_error("Failed to parse --verify= setting: %s", optarg
);
2920 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2")) {
2921 log_error("Unknown format: %s", optarg
);
2925 arg_format
= optarg
;
2933 if (!env_assignment_is_valid(optarg
)) {
2934 log_error("Environment assignment invalid: %s", optarg
);
2938 r
= strv_extend(&arg_setenv
, optarg
);
2943 case ARG_NUMBER_IPS
:
2944 if (streq(optarg
, "all"))
2945 arg_addrs
= ALL_IP_ADDRESSES
;
2946 else if (safe_atoi(optarg
, &arg_addrs
) < 0) {
2947 log_error("Invalid number of IPs");
2949 } else if (arg_addrs
< 0) {
2950 log_error("Number of IPs cannot be negative");
2959 assert_not_reached("Unhandled option");
2968 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2969 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2971 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2974 for (i
= shell
; i
< optind
; i
++)
2975 argv
[i
] = argv
[i
+1];
2982 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2984 static const Verb verbs
[] = {
2985 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2986 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2987 { "list-images", VERB_ANY
, 1, 0, list_images
},
2988 { "status", 2, VERB_ANY
, 0, show_machine
},
2989 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2990 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2991 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
2992 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
2993 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
2994 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
2995 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
2996 { "kill", 2, VERB_ANY
, 0, kill_machine
},
2997 { "login", VERB_ANY
, 2, 0, login_machine
},
2998 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
2999 { "bind", 3, 4, 0, bind_mount
},
3000 { "copy-to", 3, 4, 0, copy_files
},
3001 { "copy-from", 3, 4, 0, copy_files
},
3002 { "remove", 2, VERB_ANY
, 0, remove_image
},
3003 { "rename", 3, 3, 0, rename_image
},
3004 { "clone", 3, 3, 0, clone_image
},
3005 { "read-only", 2, 3, 0, read_only_image
},
3006 { "start", 2, VERB_ANY
, 0, start_machine
},
3007 { "enable", 2, VERB_ANY
, 0, enable_machine
},
3008 { "disable", 2, VERB_ANY
, 0, enable_machine
},
3009 { "import-tar", 2, 3, 0, import_tar
},
3010 { "import-raw", 2, 3, 0, import_raw
},
3011 { "export-tar", 2, 3, 0, export_tar
},
3012 { "export-raw", 2, 3, 0, export_raw
},
3013 { "pull-tar", 2, 3, 0, pull_tar
},
3014 { "pull-raw", 2, 3, 0, pull_raw
},
3015 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
3016 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
3017 { "set-limit", 2, 3, 0, set_limit
},
3018 { "clean", VERB_ANY
, 1, 0, clean_images
},
3022 return dispatch_verb(argc
, argv
, verbs
, bus
);
3025 int main(int argc
, char*argv
[]) {
3029 setlocale(LC_ALL
, "");
3030 log_parse_environment();
3034 r
= parse_argv(argc
, argv
);
3038 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
3040 log_error_errno(r
, "Failed to create bus connection: %m");
3044 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
3046 r
= machinectl_main(argc
, argv
, bus
);
3049 /* make sure we terminate the bus connection first, and then close the
3050 * pager, see issue #3543 for the details. */
3051 sd_bus_flush_close_unref(bus
);
3053 polkit_agent_close();
3055 strv_free(arg_property
);
3056 strv_free(arg_setenv
);
3058 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;