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"
28 #include "format-table.h"
29 #include "hostname-util.h"
30 #include "import-util.h"
31 #include "locale-util.h"
33 #include "logs-show.h"
35 #include "main-func.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "pretty-print.h"
41 #include "process-util.h"
43 #include "rlimit-util.h"
45 #include "signal-util.h"
46 #include "spawn-polkit-agent.h"
47 #include "stdio-util.h"
48 #include "string-table.h"
50 #include "terminal-util.h"
51 #include "unit-name.h"
56 #define ALL_IP_ADDRESSES -1
58 static char **arg_property
= NULL
;
59 static bool arg_all
= false;
60 static bool arg_value
= false;
61 static bool arg_full
= false;
62 static PagerFlags arg_pager_flags
= 0;
63 static bool arg_legend
= true;
64 static const char *arg_kill_who
= NULL
;
65 static int arg_signal
= SIGTERM
;
66 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
67 static const char *arg_host
= NULL
;
68 static bool arg_read_only
= false;
69 static bool arg_mkdir
= false;
70 static bool arg_quiet
= false;
71 static bool arg_ask_password
= true;
72 static unsigned arg_lines
= 10;
73 static OutputMode arg_output
= OUTPUT_SHORT
;
74 static bool arg_force
= false;
75 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
76 static const char* arg_format
= NULL
;
77 static const char *arg_uid
= NULL
;
78 static char **arg_setenv
= NULL
;
79 static int arg_addrs
= 1;
81 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
82 STATIC_DESTRUCTOR_REGISTER(arg_setenv
, strv_freep
);
84 static OutputFlags
get_output_flags(void) {
86 arg_all
* OUTPUT_SHOW_ALL
|
87 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
88 colors_enabled() * OUTPUT_COLOR
|
89 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
92 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
93 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
94 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
95 const char *k
, *v
, *iter
, **query_res
= NULL
;
96 size_t count
= 0, awaited_args
= 0;
104 NULSTR_FOREACH(iter
, query
)
106 query_res
= newa0(const char *, awaited_args
);
108 r
= sd_bus_call_method(
110 "org.freedesktop.machine1",
111 "/org/freedesktop/machine1",
112 "org.freedesktop.machine1.Manager",
117 return log_debug_errno(r
, "Failed to call '%s()': %s", method
, bus_error_message(&error
, r
));
119 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
121 return bus_log_parse_error(r
);
123 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
125 NULSTR_FOREACH(iter
, query
) {
126 if (streq(k
, iter
)) {
127 query_res
[count
] = v
;
134 return bus_log_parse_error(r
);
136 r
= sd_bus_message_exit_container(reply
);
138 return bus_log_parse_error(r
);
141 for (count
= 0; count
< awaited_args
; count
++) {
144 out
= va_arg(ap
, char **);
146 if (query_res
[count
]) {
147 val
= strdup(query_res
[count
]);
160 static int call_get_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
, char **ret
) {
162 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
163 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
164 _cleanup_free_
char *addresses
= NULL
;
165 bool truncate
= false;
174 r
= sd_bus_call_method(bus
,
175 "org.freedesktop.machine1",
176 "/org/freedesktop/machine1",
177 "org.freedesktop.machine1.Manager",
178 "GetMachineAddresses",
183 return log_debug_errno(r
, "Could not get addresses: %s", bus_error_message(&error
, r
));
185 addresses
= strdup(prefix
);
190 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
192 return bus_log_parse_error(r
);
194 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
198 char buf_ifi
[DECIMAL_STR_MAX(int) + 2], buffer
[MAX(INET6_ADDRSTRLEN
, INET_ADDRSTRLEN
)];
200 r
= sd_bus_message_read(reply
, "i", &family
);
202 return bus_log_parse_error(r
);
204 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
206 return bus_log_parse_error(r
);
209 if (family
== AF_INET6
&& ifi
> 0)
210 xsprintf(buf_ifi
, "%%%i", ifi
);
214 if (!strextend(&addresses
, prefix
, inet_ntop(family
, a
, buffer
, sizeof(buffer
)), buf_ifi
, NULL
))
219 r
= sd_bus_message_exit_container(reply
);
221 return bus_log_parse_error(r
);
231 return bus_log_parse_error(r
);
233 r
= sd_bus_message_exit_container(reply
);
235 return bus_log_parse_error(r
);
239 if (!strextend(&addresses
, special_glyph(ELLIPSIS
), NULL
))
244 *ret
= TAKE_PTR(addresses
);
248 static int show_table(Table
*table
, const char *word
) {
254 if (table_get_rows(table
) > 1) {
255 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
257 return log_error_errno(r
, "Failed to sort table: %m");
259 table_set_header(table
, arg_legend
);
261 r
= table_print(table
, NULL
);
263 return log_error_errno(r
, "Failed to show table: %m");
267 if (table_get_rows(table
) > 1)
268 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
270 printf("No %s.\n", word
);
276 static int list_machines(int argc
, char *argv
[], void *userdata
) {
278 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
279 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
280 _cleanup_(table_unrefp
) Table
*table
= NULL
;
281 sd_bus
*bus
= userdata
;
286 (void) pager_open(arg_pager_flags
);
288 r
= sd_bus_call_method(bus
,
289 "org.freedesktop.machine1",
290 "/org/freedesktop/machine1",
291 "org.freedesktop.machine1.Manager",
297 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
299 table
= table_new("MACHINE", "CLASS", "SERVICE", "OS", "VERSION", "ADDRESSES");
303 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
305 return bus_log_parse_error(r
);
308 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
309 const char *name
, *class, *service
;
311 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
313 return bus_log_parse_error(r
);
317 if (name
[0] == '.' && !arg_all
)
320 (void) call_get_os_release(
322 "GetMachineOSRelease",
329 (void) call_get_addresses(
338 r
= table_add_many(table
,
341 TABLE_STRING
, empty_to_dash(service
),
342 TABLE_STRING
, empty_to_dash(os
),
343 TABLE_STRING
, empty_to_dash(version_id
),
344 TABLE_STRING
, empty_to_dash(addresses
));
346 return log_error_errno(r
, "Failed to add table row: %m");
349 r
= sd_bus_message_exit_container(reply
);
351 return bus_log_parse_error(r
);
353 return show_table(table
, "machines");
356 static int list_images(int argc
, char *argv
[], void *userdata
) {
358 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
359 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
360 _cleanup_(table_unrefp
) Table
*table
= NULL
;
361 sd_bus
*bus
= userdata
;
366 (void) pager_open(arg_pager_flags
);
368 r
= sd_bus_call_method(bus
,
369 "org.freedesktop.machine1",
370 "/org/freedesktop/machine1",
371 "org.freedesktop.machine1.Manager",
377 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
379 table
= table_new("NAME", "TYPE", "RO", "USAGE", "CREATED", "MODIFIED");
383 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
385 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
387 return bus_log_parse_error(r
);
390 uint64_t crtime
, mtime
, size
;
391 const char *name
, *type
;
396 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
398 return bus_log_parse_error(r
);
402 if (name
[0] == '.' && !arg_all
)
405 r
= table_add_many(table
,
409 return log_error_errno(r
, "Failed to add table row: %m");
412 r
= table_add_cell(table
, &cell
, TABLE_BOOLEAN
, &ro_bool
);
414 return log_error_errno(r
, "Failed to add table cell: %m");
417 r
= table_set_color(table
, cell
, ansi_highlight_red());
419 return log_error_errno(r
, "Failed to set table cell color: %m");
422 r
= table_add_many(table
,
424 TABLE_TIMESTAMP
, crtime
,
425 TABLE_TIMESTAMP
, mtime
);
427 return log_error_errno(r
, "Failed to add table row: %m");
430 r
= sd_bus_message_exit_container(reply
);
432 return bus_log_parse_error(r
);
434 return show_table(table
, "images");
437 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
438 _cleanup_free_
char *cgroup
= NULL
;
439 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
446 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
459 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
462 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
465 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
467 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
470 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
472 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
477 static int print_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
) {
478 _cleanup_free_
char *s
= NULL
;
481 r
= call_get_addresses(bus
, name
, ifi
, prefix
, prefix2
, n_addr
, &s
);
491 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
492 _cleanup_free_
char *pretty
= NULL
;
499 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
504 printf("%s%s\n", prefix
, pretty
);
509 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
510 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
511 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
518 r
= sd_bus_call_method(bus
,
519 "org.freedesktop.machine1",
520 "/org/freedesktop/machine1",
521 "org.freedesktop.machine1.Manager",
522 "GetMachineUIDShift",
527 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
529 r
= sd_bus_message_read(reply
, "u", &shift
);
533 if (shift
== 0) /* Don't show trivial mappings */
536 printf(" UID Shift: %" PRIu32
"\n", shift
);
540 typedef struct MachineStatusInfo
{
546 const char *root_directory
;
548 struct dual_timestamp timestamp
;
553 static void machine_status_info_clear(MachineStatusInfo
*info
) {
560 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
561 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
562 char since2
[FORMAT_TIMESTAMP_MAX
];
569 fputs(strna(i
->name
), stdout
);
571 if (!sd_id128_is_null(i
->id
))
572 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
576 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->timestamp
.realtime
);
577 s2
= format_timestamp(since2
, sizeof(since2
), i
->timestamp
.realtime
);
580 printf("\t Since: %s; %s\n", s2
, s1
);
582 printf("\t Since: %s\n", s2
);
585 _cleanup_free_
char *t
= NULL
;
587 printf("\t Leader: %u", (unsigned) i
->leader
);
589 get_process_comm(i
->leader
, &t
);
597 printf("\t Service: %s", i
->service
);
600 printf("; class %s", i
->class);
604 printf("\t Class: %s\n", i
->class);
606 if (i
->root_directory
)
607 printf("\t Root: %s\n", i
->root_directory
);
609 if (i
->n_netif
> 0) {
612 fputs("\t Iface:", stdout
);
614 for (c
= 0; c
< i
->n_netif
; c
++) {
615 char name
[IF_NAMESIZE
+1] = "";
617 if (if_indextoname(i
->netif
[c
], name
)) {
626 printf(" %i", i
->netif
[c
]);
632 if (print_addresses(bus
, i
->name
, ifi
,
635 ALL_IP_ADDRESSES
) > 0)
638 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
640 print_uid_shift(bus
, i
->name
);
643 printf("\t Unit: %s\n", i
->unit
);
644 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
646 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
648 show_journal_by_unit(
653 i
->timestamp
.monotonic
,
656 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
657 SD_JOURNAL_LOCAL_ONLY
,
663 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
664 MachineStatusInfo
*i
= userdata
;
669 assert_cc(sizeof(int32_t) == sizeof(int));
670 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
676 i
->n_netif
= l
/ sizeof(int32_t);
677 i
->netif
= memdup(v
, l
);
684 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
686 static const struct bus_properties_map map
[] = {
687 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
688 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
689 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
690 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
691 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
692 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
693 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
694 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
695 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
696 { "NetworkInterfaces", "ai", map_netif
, 0 },
700 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
701 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
702 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
710 r
= bus_map_all_properties(bus
,
711 "org.freedesktop.machine1",
719 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
725 print_machine_status_info(bus
, &info
);
730 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
742 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_value
, arg_all
, NULL
);
744 log_error_errno(r
, "Could not get properties: %m");
749 static int show_machine(int argc
, char *argv
[], void *userdata
) {
751 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
752 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
753 bool properties
, new_line
= false;
754 sd_bus
*bus
= userdata
;
759 properties
= !strstr(argv
[0], "status");
761 (void) pager_open(arg_pager_flags
);
763 if (properties
&& argc
<= 1) {
765 /* If no argument is specified, inspect the manager
767 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
772 for (i
= 1; i
< argc
; i
++) {
773 const char *path
= NULL
;
775 r
= sd_bus_call_method(bus
,
776 "org.freedesktop.machine1",
777 "/org/freedesktop/machine1",
778 "org.freedesktop.machine1.Manager",
784 return log_error_errno(r
, "Could not get path to machine: %s", bus_error_message(&error
, -r
));
786 r
= sd_bus_message_read(reply
, "o", &path
);
788 return bus_log_parse_error(r
);
791 r
= show_machine_properties(bus
, path
, &new_line
);
793 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
799 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
800 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
804 r
= sd_bus_call_method(
806 "org.freedesktop.machine1",
807 "/org/freedesktop/machine1",
808 "org.freedesktop.machine1.Manager",
810 NULL
, &reply
, "s", name
);
814 r
= sd_bus_message_read(reply
, "s", &hn
);
819 printf("\tHostname: %s\n", hn
);
824 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
825 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
826 sd_id128_t id
= SD_ID128_NULL
;
831 r
= sd_bus_call_method(
833 "org.freedesktop.machine1",
834 "/org/freedesktop/machine1",
835 "org.freedesktop.machine1.Manager",
837 NULL
, &reply
, "s", name
);
841 r
= sd_bus_message_read_array(reply
, 'y', &p
, &size
);
845 if (size
== sizeof(sd_id128_t
))
846 memcpy(&id
, p
, size
);
848 if (!sd_id128_is_null(id
))
849 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
854 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
855 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
858 r
= sd_bus_call_method(
860 "org.freedesktop.machine1",
861 "/org/freedesktop/machine1",
862 "org.freedesktop.machine1.Manager",
863 "GetImageMachineInfo",
864 NULL
, &reply
, "s", name
);
868 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
875 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
881 if (streq(p
, "DEPLOYMENT"))
882 printf(" Deployment: %s\n", q
);
885 r
= sd_bus_message_exit_container(reply
);
892 typedef struct ImageStatusInfo
{
901 uint64_t usage_exclusive
;
902 uint64_t limit_exclusive
;
905 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
906 char ts_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
907 char ts_absolute
[FORMAT_TIMESTAMP_MAX
];
908 char bs
[FORMAT_BYTES_MAX
];
909 char bs_exclusive
[FORMAT_BYTES_MAX
];
910 const char *s1
, *s2
, *s3
, *s4
;
916 fputs(i
->name
, stdout
);
921 printf("\t Type: %s\n", i
->type
);
924 printf("\t Path: %s\n", i
->path
);
926 (void) print_image_hostname(bus
, i
->name
);
927 (void) print_image_machine_id(bus
, i
->name
);
928 (void) print_image_machine_info(bus
, i
->name
);
930 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
932 printf("\t RO: %s%s%s\n",
933 i
->read_only
? ansi_highlight_red() : "",
934 i
->read_only
? "read-only" : "writable",
935 i
->read_only
? ansi_normal() : "");
937 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->crtime
);
938 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->crtime
);
940 printf("\t Created: %s; %s\n", s2
, s1
);
942 printf("\t Created: %s\n", s2
);
944 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->mtime
);
945 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->mtime
);
947 printf("\tModified: %s; %s\n", s2
, s1
);
949 printf("\tModified: %s\n", s2
);
951 s3
= format_bytes(bs
, sizeof(bs
), i
->usage
);
952 s4
= i
->usage_exclusive
!= i
->usage
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->usage_exclusive
) : NULL
;
954 printf("\t Usage: %s (exclusive: %s)\n", s3
, s4
);
956 printf("\t Usage: %s\n", s3
);
958 s3
= format_bytes(bs
, sizeof(bs
), i
->limit
);
959 s4
= i
->limit_exclusive
!= i
->limit
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->limit_exclusive
) : NULL
;
961 printf("\t Limit: %s (exclusive: %s)\n", s3
, s4
);
963 printf("\t Limit: %s\n", s3
);
966 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
968 static const struct bus_properties_map map
[] = {
969 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
970 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
971 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
972 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
973 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
974 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
975 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
976 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
977 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
978 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
982 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
983 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
984 ImageStatusInfo info
= {};
991 r
= bus_map_all_properties(bus
,
992 "org.freedesktop.machine1",
995 BUS_MAP_BOOLEAN_AS_BOOL
,
1000 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1006 print_image_status_info(bus
, &info
);
1011 typedef struct PoolStatusInfo
{
1017 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
1018 char bs
[FORMAT_BYTES_MAX
], *s
;
1021 printf("\t Path: %s\n", i
->path
);
1023 s
= format_bytes(bs
, sizeof(bs
), i
->usage
);
1025 printf("\t Usage: %s\n", s
);
1027 s
= format_bytes(bs
, sizeof(bs
), i
->limit
);
1029 printf("\t Limit: %s\n", s
);
1032 static int show_pool_info(sd_bus
*bus
) {
1034 static const struct bus_properties_map map
[] = {
1035 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
1036 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
1037 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
1041 PoolStatusInfo info
= {
1042 .usage
= (uint64_t) -1,
1043 .limit
= (uint64_t) -1,
1046 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1047 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1052 r
= bus_map_all_properties(bus
,
1053 "org.freedesktop.machine1",
1054 "/org/freedesktop/machine1",
1061 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1063 print_pool_status_info(bus
, &info
);
1068 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
1080 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_value
, arg_all
, NULL
);
1082 log_error_errno(r
, "Could not get properties: %m");
1087 static int show_image(int argc
, char *argv
[], void *userdata
) {
1089 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1090 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1091 bool properties
, new_line
= false;
1092 sd_bus
*bus
= userdata
;
1097 properties
= !strstr(argv
[0], "status");
1099 (void) pager_open(arg_pager_flags
);
1103 /* If no argument is specified, inspect the manager
1107 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1109 r
= show_pool_info(bus
);
1114 for (i
= 1; i
< argc
; i
++) {
1115 const char *path
= NULL
;
1117 r
= sd_bus_call_method(
1119 "org.freedesktop.machine1",
1120 "/org/freedesktop/machine1",
1121 "org.freedesktop.machine1.Manager",
1127 return log_error_errno(r
, "Could not get path to image: %s", bus_error_message(&error
, -r
));
1129 r
= sd_bus_message_read(reply
, "o", &path
);
1131 return bus_log_parse_error(r
);
1134 r
= show_image_properties(bus
, path
, &new_line
);
1136 r
= show_image_info(bus
, path
, &new_line
);
1142 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1143 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1144 sd_bus
*bus
= userdata
;
1149 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1152 arg_kill_who
= "all";
1154 for (i
= 1; i
< argc
; i
++) {
1155 r
= sd_bus_call_method(
1157 "org.freedesktop.machine1",
1158 "/org/freedesktop/machine1",
1159 "org.freedesktop.machine1.Manager",
1163 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1165 return log_error_errno(r
, "Could not kill machine: %s", bus_error_message(&error
, -r
));
1171 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1172 arg_kill_who
= "leader";
1173 arg_signal
= SIGINT
; /* sysvinit + systemd */
1175 return kill_machine(argc
, argv
, userdata
);
1178 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1179 arg_kill_who
= "leader";
1180 arg_signal
= SIGRTMIN
+4; /* only systemd */
1182 return kill_machine(argc
, argv
, userdata
);
1185 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1186 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1187 sd_bus
*bus
= userdata
;
1192 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1194 for (i
= 1; i
< argc
; i
++) {
1195 r
= sd_bus_call_method(
1197 "org.freedesktop.machine1",
1198 "/org/freedesktop/machine1",
1199 "org.freedesktop.machine1.Manager",
1205 return log_error_errno(r
, "Could not terminate machine: %s", bus_error_message(&error
, -r
));
1211 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1212 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1213 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1214 _cleanup_free_
char *abs_host_path
= NULL
;
1215 char *dest
, *host_path
, *container_path
;
1216 sd_bus
*bus
= userdata
;
1222 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1224 copy_from
= streq(argv
[0], "copy-from");
1225 dest
= argv
[3] ?: argv
[2];
1226 host_path
= copy_from
? dest
: argv
[2];
1227 container_path
= copy_from
? argv
[2] : dest
;
1229 if (!path_is_absolute(host_path
)) {
1230 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1232 return log_error_errno(r
, "Failed to make path absolute: %m");
1234 host_path
= abs_host_path
;
1237 r
= sd_bus_message_new_method_call(
1240 "org.freedesktop.machine1",
1241 "/org/freedesktop/machine1",
1242 "org.freedesktop.machine1.Manager",
1243 copy_from
? "CopyFromMachine" : "CopyToMachine");
1245 return bus_log_create_error(r
);
1247 r
= sd_bus_message_append(
1251 copy_from
? container_path
: host_path
,
1252 copy_from
? host_path
: container_path
);
1254 return bus_log_create_error(r
);
1256 /* This is a slow operation, hence turn off any method call timeouts */
1257 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1259 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1264 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1265 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1266 sd_bus
*bus
= userdata
;
1271 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1273 r
= sd_bus_call_method(
1275 "org.freedesktop.machine1",
1276 "/org/freedesktop/machine1",
1277 "org.freedesktop.machine1.Manager",
1288 return log_error_errno(r
, "Failed to bind mount: %s", bus_error_message(&error
, -r
));
1293 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1294 PTYForward
** forward
= (PTYForward
**) userdata
;
1301 /* If the forwarder is already initialized, tell it to
1302 * exit on the next vhangup(), so that we still flush
1303 * out what might be queued and exit then. */
1305 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1309 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1312 /* On error, or when the forwarder is not initialized yet, quit immediately */
1313 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1317 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1323 assert(master
>= 0);
1326 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1329 if (streq(name
, ".host"))
1330 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1332 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1335 (void) sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1336 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1338 r
= pty_forward_new(event
, master
, flags
, forward
);
1340 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1342 r
= sd_event_loop(event
);
1344 return log_error_errno(r
, "Failed to run event loop: %m");
1346 pty_forward_get_last_char(*forward
, &last_char
);
1349 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1350 pty_forward_get_ignore_vhangup(*forward
) == 0;
1352 *forward
= pty_forward_free(*forward
);
1354 if (last_char
!= '\n')
1355 fputc('\n', stdout
);
1359 log_info("Machine %s terminated.", name
);
1360 else if (streq(name
, ".host"))
1361 log_info("Connection to the local host terminated.");
1363 log_info("Connection to machine %s terminated.", name
);
1366 sd_event_get_exit_code(event
, &ret
);
1370 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1372 * Whatever is specified in the spec takes priority over global arguments.
1375 const char *_machine
= NULL
;
1380 at
= strchr(spec
, '@');
1383 /* Do the same as ssh and refuse "@host". */
1387 _uid
= strndup(spec
, at
- spec
);
1394 if (arg_uid
&& !_uid
) {
1395 _uid
= strdup(arg_uid
);
1401 *machine
= isempty(_machine
) ? ".host" : _machine
;
1405 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1406 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1407 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1408 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1409 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1410 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1412 sd_bus
*bus
= userdata
;
1413 const char *match
, *machine
;
1417 if (!strv_isempty(arg_setenv
) || arg_uid
) {
1418 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1422 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1423 log_error("Login only supported on local machines.");
1427 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1429 r
= sd_event_default(&event
);
1431 return log_error_errno(r
, "Failed to get event loop: %m");
1433 r
= sd_bus_attach_event(bus
, event
, 0);
1435 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1437 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1439 match
= strjoina("type='signal',"
1440 "sender='org.freedesktop.machine1',"
1441 "path='/org/freedesktop/machine1',",
1442 "interface='org.freedesktop.machine1.Manager',"
1443 "member='MachineRemoved',"
1444 "arg0='", machine
, "'");
1446 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1448 return log_error_errno(r
, "Failed to request machine removal match: %m");
1450 r
= sd_bus_call_method(
1452 "org.freedesktop.machine1",
1453 "/org/freedesktop/machine1",
1454 "org.freedesktop.machine1.Manager",
1460 return log_error_errno(r
, "Failed to get login PTY: %s", bus_error_message(&error
, -r
));
1462 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1464 return bus_log_parse_error(r
);
1466 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1469 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1470 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1471 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1472 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1473 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1474 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1476 sd_bus
*bus
= userdata
;
1477 const char *match
, *machine
, *path
;
1478 _cleanup_free_
char *uid
= NULL
;
1482 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1483 log_error("Shell only supported on local machines.");
1487 /* Pass $TERM to shell session, if not explicitly specified. */
1488 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1491 t
= strv_find_prefix(environ
, "TERM=");
1493 if (strv_extend(&arg_setenv
, t
) < 0)
1498 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1500 r
= sd_event_default(&event
);
1502 return log_error_errno(r
, "Failed to get event loop: %m");
1504 r
= sd_bus_attach_event(bus
, event
, 0);
1506 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1508 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1510 return log_error_errno(r
, "Failed to parse machine specification: %m");
1512 match
= strjoina("type='signal',"
1513 "sender='org.freedesktop.machine1',"
1514 "path='/org/freedesktop/machine1',",
1515 "interface='org.freedesktop.machine1.Manager',"
1516 "member='MachineRemoved',"
1517 "arg0='", machine
, "'");
1519 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1521 return log_error_errno(r
, "Failed to request machine removal match: %m");
1523 r
= sd_bus_message_new_method_call(
1526 "org.freedesktop.machine1",
1527 "/org/freedesktop/machine1",
1528 "org.freedesktop.machine1.Manager",
1529 "OpenMachineShell");
1531 return bus_log_create_error(r
);
1533 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1535 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1537 return bus_log_create_error(r
);
1539 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1541 return bus_log_create_error(r
);
1543 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1545 return bus_log_create_error(r
);
1547 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1549 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, -r
));
1551 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1553 return bus_log_parse_error(r
);
1555 return process_forward(event
, &forward
, master
, 0, machine
);
1558 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1559 sd_bus
*bus
= userdata
;
1564 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1566 for (i
= 1; i
< argc
; i
++) {
1567 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1568 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1570 r
= sd_bus_message_new_method_call(
1573 "org.freedesktop.machine1",
1574 "/org/freedesktop/machine1",
1575 "org.freedesktop.machine1.Manager",
1578 return bus_log_create_error(r
);
1580 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1582 return bus_log_create_error(r
);
1584 /* This is a slow operation, hence turn off any method call timeouts */
1585 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1587 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1593 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1594 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1595 sd_bus
*bus
= userdata
;
1600 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1602 r
= sd_bus_call_method(
1604 "org.freedesktop.machine1",
1605 "/org/freedesktop/machine1",
1606 "org.freedesktop.machine1.Manager",
1610 "ss", argv
[1], argv
[2]);
1612 return log_error_errno(r
, "Could not rename image: %s", bus_error_message(&error
, -r
));
1617 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1618 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1619 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1620 sd_bus
*bus
= userdata
;
1625 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1627 r
= sd_bus_message_new_method_call(
1630 "org.freedesktop.machine1",
1631 "/org/freedesktop/machine1",
1632 "org.freedesktop.machine1.Manager",
1635 return bus_log_create_error(r
);
1637 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1639 return bus_log_create_error(r
);
1641 /* This is a slow operation, hence turn off any method call timeouts */
1642 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1644 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1649 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1650 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1651 sd_bus
*bus
= userdata
;
1657 b
= parse_boolean(argv
[2]);
1659 log_error("Failed to parse boolean argument: %s", argv
[2]);
1664 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1666 r
= sd_bus_call_method(
1668 "org.freedesktop.machine1",
1669 "/org/freedesktop/machine1",
1670 "org.freedesktop.machine1.Manager",
1671 "MarkImageReadOnly",
1676 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, -r
));
1681 static int image_exists(sd_bus
*bus
, const char *name
) {
1682 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1688 r
= sd_bus_call_method(
1690 "org.freedesktop.machine1",
1691 "/org/freedesktop/machine1",
1692 "org.freedesktop.machine1.Manager",
1698 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1701 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, -r
));
1707 static int make_service_name(const char *name
, char **ret
) {
1713 if (!machine_name_is_valid(name
)) {
1714 log_error("Invalid machine name %s.", name
);
1718 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1720 return log_error_errno(r
, "Failed to build unit name: %m");
1725 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1726 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1727 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1728 sd_bus
*bus
= userdata
;
1733 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1735 r
= bus_wait_for_jobs_new(bus
, &w
);
1739 for (i
= 1; i
< argc
; i
++) {
1740 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1741 _cleanup_free_
char *unit
= NULL
;
1744 r
= make_service_name(argv
[i
], &unit
);
1748 r
= image_exists(bus
, argv
[i
]);
1752 log_error("Machine image '%s' does not exist.", argv
[1]);
1756 r
= sd_bus_call_method(
1758 "org.freedesktop.systemd1",
1759 "/org/freedesktop/systemd1",
1760 "org.freedesktop.systemd1.Manager",
1764 "ss", unit
, "fail");
1766 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, -r
));
1768 r
= sd_bus_message_read(reply
, "o", &object
);
1770 return bus_log_parse_error(r
);
1772 r
= bus_wait_for_jobs_add(w
, object
);
1777 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1784 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1785 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1786 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1787 UnitFileChange
*changes
= NULL
;
1788 size_t n_changes
= 0;
1789 const char *method
= NULL
;
1790 sd_bus
*bus
= userdata
;
1795 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1797 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1799 r
= sd_bus_message_new_method_call(
1802 "org.freedesktop.systemd1",
1803 "/org/freedesktop/systemd1",
1804 "org.freedesktop.systemd1.Manager",
1807 return bus_log_create_error(r
);
1809 r
= sd_bus_message_open_container(m
, 'a', "s");
1811 return bus_log_create_error(r
);
1813 for (i
= 1; i
< argc
; i
++) {
1814 _cleanup_free_
char *unit
= NULL
;
1816 r
= make_service_name(argv
[i
], &unit
);
1820 r
= image_exists(bus
, argv
[i
]);
1824 log_error("Machine image '%s' does not exist.", argv
[1]);
1828 r
= sd_bus_message_append(m
, "s", unit
);
1830 return bus_log_create_error(r
);
1833 r
= sd_bus_message_close_container(m
);
1835 return bus_log_create_error(r
);
1837 if (streq(argv
[0], "enable"))
1838 r
= sd_bus_message_append(m
, "bb", false, false);
1840 r
= sd_bus_message_append(m
, "b", false);
1842 return bus_log_create_error(r
);
1844 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1846 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1848 if (streq(argv
[0], "enable")) {
1849 r
= sd_bus_message_read(reply
, "b", NULL
);
1851 return bus_log_parse_error(r
);
1854 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1858 r
= sd_bus_call_method(
1860 "org.freedesktop.systemd1",
1861 "/org/freedesktop/systemd1",
1862 "org.freedesktop.systemd1.Manager",
1868 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1875 unit_file_changes_free(changes
, n_changes
);
1880 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1881 const char **our_path
= userdata
, *line
;
1888 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1890 bus_log_parse_error(r
);
1894 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1897 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1900 log_full(priority
, "%s", line
);
1904 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1905 const char **our_path
= userdata
, *path
, *result
;
1912 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1914 bus_log_parse_error(r
);
1918 if (!streq_ptr(*our_path
, path
))
1921 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1925 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1930 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1932 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1936 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1937 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1938 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1939 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1940 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1941 const char *path
= NULL
;
1948 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1950 r
= sd_event_default(&event
);
1952 return log_error_errno(r
, "Failed to get event loop: %m");
1954 r
= sd_bus_attach_event(bus
, event
, 0);
1956 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1958 r
= sd_bus_match_signal_async(
1961 "org.freedesktop.import1",
1962 "/org/freedesktop/import1",
1963 "org.freedesktop.import1.Manager",
1965 match_transfer_removed
, NULL
, &path
);
1967 return log_error_errno(r
, "Failed to request match: %m");
1969 r
= sd_bus_match_signal_async(
1972 "org.freedesktop.import1",
1974 "org.freedesktop.import1.Transfer",
1976 match_log_message
, NULL
, &path
);
1978 return log_error_errno(r
, "Failed to request match: %m");
1980 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1982 return log_error_errno(r
, "Failed to transfer image: %s", bus_error_message(&error
, -r
));
1984 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1986 return bus_log_parse_error(r
);
1988 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1991 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1993 (void) sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1994 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1996 r
= sd_event_loop(event
);
1998 return log_error_errno(r
, "Failed to run event loop: %m");
2003 static int import_tar(int argc
, char *argv
[], void *userdata
) {
2004 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2005 _cleanup_free_
char *ll
= NULL
;
2006 _cleanup_close_
int fd
= -1;
2007 const char *local
= NULL
, *path
= NULL
;
2008 sd_bus
*bus
= userdata
;
2015 if (isempty(path
) || streq(path
, "-"))
2021 local
= basename(path
);
2022 if (isempty(local
) || streq(local
, "-"))
2026 log_error("Need either path or local name.");
2030 r
= tar_strip_suffixes(local
, &ll
);
2036 if (!machine_name_is_valid(local
)) {
2037 log_error("Local name %s is not a suitable machine name.", local
);
2042 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2044 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2047 r
= sd_bus_message_new_method_call(
2050 "org.freedesktop.import1",
2051 "/org/freedesktop/import1",
2052 "org.freedesktop.import1.Manager",
2055 return bus_log_create_error(r
);
2057 r
= sd_bus_message_append(
2060 fd
>= 0 ? fd
: STDIN_FILENO
,
2065 return bus_log_create_error(r
);
2067 return transfer_image_common(bus
, m
);
2070 static int import_raw(int argc
, char *argv
[], void *userdata
) {
2071 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2072 _cleanup_free_
char *ll
= NULL
;
2073 _cleanup_close_
int fd
= -1;
2074 const char *local
= NULL
, *path
= NULL
;
2075 sd_bus
*bus
= userdata
;
2082 if (isempty(path
) || streq(path
, "-"))
2088 local
= basename(path
);
2089 if (isempty(local
) || streq(local
, "-"))
2093 log_error("Need either path or local name.");
2097 r
= raw_strip_suffixes(local
, &ll
);
2103 if (!machine_name_is_valid(local
)) {
2104 log_error("Local name %s is not a suitable machine name.", local
);
2109 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2111 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2114 r
= sd_bus_message_new_method_call(
2117 "org.freedesktop.import1",
2118 "/org/freedesktop/import1",
2119 "org.freedesktop.import1.Manager",
2122 return bus_log_create_error(r
);
2124 r
= sd_bus_message_append(
2127 fd
>= 0 ? fd
: STDIN_FILENO
,
2132 return bus_log_create_error(r
);
2134 return transfer_image_common(bus
, m
);
2137 static void determine_compression_from_filename(const char *p
) {
2144 if (endswith(p
, ".xz"))
2146 else if (endswith(p
, ".gz"))
2147 arg_format
= "gzip";
2148 else if (endswith(p
, ".bz2"))
2149 arg_format
= "bzip2";
2152 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2153 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2154 _cleanup_close_
int fd
= -1;
2155 const char *local
= NULL
, *path
= NULL
;
2156 sd_bus
*bus
= userdata
;
2162 if (!machine_name_is_valid(local
)) {
2163 log_error("Machine name %s is not valid.", local
);
2169 if (isempty(path
) || streq(path
, "-"))
2173 determine_compression_from_filename(path
);
2175 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2177 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2180 r
= sd_bus_message_new_method_call(
2183 "org.freedesktop.import1",
2184 "/org/freedesktop/import1",
2185 "org.freedesktop.import1.Manager",
2188 return bus_log_create_error(r
);
2190 r
= sd_bus_message_append(
2194 fd
>= 0 ? fd
: STDOUT_FILENO
,
2197 return bus_log_create_error(r
);
2199 return transfer_image_common(bus
, m
);
2202 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2203 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2204 _cleanup_close_
int fd
= -1;
2205 const char *local
= NULL
, *path
= NULL
;
2206 sd_bus
*bus
= userdata
;
2212 if (!machine_name_is_valid(local
)) {
2213 log_error("Machine name %s is not valid.", local
);
2219 if (isempty(path
) || streq(path
, "-"))
2223 determine_compression_from_filename(path
);
2225 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2227 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2230 r
= sd_bus_message_new_method_call(
2233 "org.freedesktop.import1",
2234 "/org/freedesktop/import1",
2235 "org.freedesktop.import1.Manager",
2238 return bus_log_create_error(r
);
2240 r
= sd_bus_message_append(
2244 fd
>= 0 ? fd
: STDOUT_FILENO
,
2247 return bus_log_create_error(r
);
2249 return transfer_image_common(bus
, m
);
2252 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2253 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2254 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2255 const char *local
, *remote
;
2256 sd_bus
*bus
= userdata
;
2262 if (!http_url_is_valid(remote
)) {
2263 log_error("URL '%s' is not valid.", remote
);
2270 r
= import_url_last_component(remote
, &l
);
2272 return log_error_errno(r
, "Failed to get final component of URL: %m");
2277 if (isempty(local
) || streq(local
, "-"))
2281 r
= tar_strip_suffixes(local
, &ll
);
2287 if (!machine_name_is_valid(local
)) {
2288 log_error("Local name %s is not a suitable machine name.", local
);
2293 r
= sd_bus_message_new_method_call(
2296 "org.freedesktop.import1",
2297 "/org/freedesktop/import1",
2298 "org.freedesktop.import1.Manager",
2301 return bus_log_create_error(r
);
2303 r
= sd_bus_message_append(
2308 import_verify_to_string(arg_verify
),
2311 return bus_log_create_error(r
);
2313 return transfer_image_common(bus
, m
);
2316 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2317 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2318 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2319 const char *local
, *remote
;
2320 sd_bus
*bus
= userdata
;
2326 if (!http_url_is_valid(remote
)) {
2327 log_error("URL '%s' is not valid.", remote
);
2334 r
= import_url_last_component(remote
, &l
);
2336 return log_error_errno(r
, "Failed to get final component of URL: %m");
2341 if (isempty(local
) || streq(local
, "-"))
2345 r
= raw_strip_suffixes(local
, &ll
);
2351 if (!machine_name_is_valid(local
)) {
2352 log_error("Local name %s is not a suitable machine name.", local
);
2357 r
= sd_bus_message_new_method_call(
2360 "org.freedesktop.import1",
2361 "/org/freedesktop/import1",
2362 "org.freedesktop.import1.Manager",
2365 return bus_log_create_error(r
);
2367 r
= sd_bus_message_append(
2372 import_verify_to_string(arg_verify
),
2375 return bus_log_create_error(r
);
2377 return transfer_image_common(bus
, m
);
2380 typedef struct TransferInfo
{
2388 static int compare_transfer_info(const TransferInfo
*a
, const TransferInfo
*b
) {
2389 return strcmp(a
->local
, b
->local
);
2392 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2393 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2394 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2395 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2396 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2397 size_t n_transfers
= 0, n_allocated
= 0, j
;
2398 const char *type
, *remote
, *local
;
2399 sd_bus
*bus
= userdata
;
2400 uint32_t id
, max_id
= 0;
2404 (void) pager_open(arg_pager_flags
);
2406 r
= sd_bus_call_method(bus
,
2407 "org.freedesktop.import1",
2408 "/org/freedesktop/import1",
2409 "org.freedesktop.import1.Manager",
2415 return log_error_errno(r
, "Could not get transfers: %s", bus_error_message(&error
, -r
));
2417 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2419 return bus_log_parse_error(r
);
2421 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2424 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2427 transfers
[n_transfers
].id
= id
;
2428 transfers
[n_transfers
].type
= type
;
2429 transfers
[n_transfers
].remote
= remote
;
2430 transfers
[n_transfers
].local
= local
;
2431 transfers
[n_transfers
].progress
= progress
;
2451 return bus_log_parse_error(r
);
2453 r
= sd_bus_message_exit_container(reply
);
2455 return bus_log_parse_error(r
);
2457 typesafe_qsort(transfers
, n_transfers
, compare_transfer_info
);
2459 if (arg_legend
&& n_transfers
> 0)
2460 printf("%-*s %-*s %-*s %-*s %-*s\n",
2461 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2463 (int) max_type
, "TYPE",
2464 (int) max_local
, "LOCAL",
2465 (int) max_remote
, "REMOTE");
2467 for (j
= 0; j
< n_transfers
; j
++)
2468 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2469 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2470 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2471 (int) max_type
, transfers
[j
].type
,
2472 (int) max_local
, transfers
[j
].local
,
2473 (int) max_remote
, transfers
[j
].remote
);
2476 if (n_transfers
> 0)
2477 printf("\n%zu transfers listed.\n", n_transfers
);
2479 printf("No transfers.\n");
2485 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2486 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2487 sd_bus
*bus
= userdata
;
2492 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2494 for (i
= 1; i
< argc
; i
++) {
2497 r
= safe_atou32(argv
[i
], &id
);
2499 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2501 r
= sd_bus_call_method(
2503 "org.freedesktop.import1",
2504 "/org/freedesktop/import1",
2505 "org.freedesktop.import1.Manager",
2511 return log_error_errno(r
, "Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2517 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2518 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2519 sd_bus
*bus
= userdata
;
2523 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2525 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2526 limit
= (uint64_t) -1;
2528 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2530 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2534 /* With two arguments changes the quota limit of the
2535 * specified image */
2536 r
= sd_bus_call_method(
2538 "org.freedesktop.machine1",
2539 "/org/freedesktop/machine1",
2540 "org.freedesktop.machine1.Manager",
2544 "st", argv
[1], limit
);
2546 /* With one argument changes the pool quota limit */
2547 r
= sd_bus_call_method(
2549 "org.freedesktop.machine1",
2550 "/org/freedesktop/machine1",
2551 "org.freedesktop.machine1.Manager",
2558 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2563 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2564 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2565 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2566 uint64_t usage
, total
= 0;
2567 char fb
[FORMAT_BYTES_MAX
];
2568 sd_bus
*bus
= userdata
;
2573 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2575 r
= sd_bus_message_new_method_call(
2578 "org.freedesktop.machine1",
2579 "/org/freedesktop/machine1",
2580 "org.freedesktop.machine1.Manager",
2583 return bus_log_create_error(r
);
2585 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2587 return bus_log_create_error(r
);
2589 /* This is a slow operation, hence permit a longer time for completion. */
2590 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2592 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2594 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2596 return bus_log_parse_error(r
);
2598 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2599 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2600 name
, format_bytes(fb
, sizeof(fb
), usage
));
2606 r
= sd_bus_message_exit_container(reply
);
2608 return bus_log_parse_error(r
);
2610 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2611 c
, format_bytes(fb
, sizeof(fb
), total
));
2616 static int help(int argc
, char *argv
[], void *userdata
) {
2617 _cleanup_free_
char *link
= NULL
;
2620 (void) pager_open(arg_pager_flags
);
2622 r
= terminal_urlify_man("machinectl", "1", &link
);
2626 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2627 "Send control commands to or query the virtual machine and container\n"
2628 "registration manager.\n\n"
2629 " -h --help Show this help\n"
2630 " --version Show package version\n"
2631 " --no-pager Do not pipe output into a pager\n"
2632 " --no-legend Do not show the headers and footers\n"
2633 " --no-ask-password Do not ask for system passwords\n"
2634 " -H --host=[USER@]HOST Operate on remote host\n"
2635 " -M --machine=CONTAINER Operate on local container\n"
2636 " -p --property=NAME Show only properties by this name\n"
2637 " -q --quiet Suppress output\n"
2638 " -a --all Show all properties, including empty ones\n"
2639 " --value When showing properties, only print the value\n"
2640 " -l --full Do not ellipsize output\n"
2641 " --kill-who=WHO Who to send signal to\n"
2642 " -s --signal=SIGNAL Which signal to send\n"
2643 " --uid=USER Specify user ID to invoke shell as\n"
2644 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2645 " --read-only Create read-only bind mount\n"
2646 " --mkdir Create directory before bind mounting, if missing\n"
2647 " -n --lines=INTEGER Number of journal entries to show\n"
2648 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2649 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2650 " short-iso, short-iso-precise, short-full,\n"
2651 " short-monotonic, short-unix, verbose, export,\n"
2652 " json, json-pretty, json-sse, json-seq, cat,\n"
2654 " --verify=MODE Verification mode for downloaded images (no,\n"
2655 " checksum, signature)\n"
2656 " --force Download image even if already exists\n\n"
2657 "Machine Commands:\n"
2658 " list List running VMs and containers\n"
2659 " status NAME... Show VM/container details\n"
2660 " show [NAME...] Show properties of one or more VMs/containers\n"
2661 " start NAME... Start container as a service\n"
2662 " login [NAME] Get a login prompt in a container or on the\n"
2664 " shell [[USER@]NAME [COMMAND...]]\n"
2665 " Invoke a shell (or other command) in a container\n"
2666 " or on the local host\n"
2667 " enable NAME... Enable automatic container start at boot\n"
2668 " disable NAME... Disable automatic container start at boot\n"
2669 " poweroff NAME... Power off one or more containers\n"
2670 " reboot NAME... Reboot one or more containers\n"
2671 " terminate NAME... Terminate one or more VMs/containers\n"
2672 " kill NAME... Send signal to processes of a VM/container\n"
2673 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2674 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2675 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2677 " list-images Show available container and VM images\n"
2678 " image-status [NAME...] Show image details\n"
2679 " show-image [NAME...] Show properties of image\n"
2680 " clone NAME NAME Clone an image\n"
2681 " rename NAME NAME Rename an image\n"
2682 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2683 " remove NAME... Remove an image\n"
2684 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2685 " clean Remove hidden (or all) images\n\n"
2686 "Image Transfer Commands:\n"
2687 " pull-tar URL [NAME] Download a TAR container image\n"
2688 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2689 " import-tar FILE [NAME] Import a local TAR container image\n"
2690 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2691 " export-tar NAME [FILE] Export a TAR container image locally\n"
2692 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2693 " list-transfers Show list of downloads in progress\n"
2694 " cancel-transfer Cancel a download\n"
2695 "\nSee the %s for details.\n"
2696 , program_invocation_short_name
2703 static int parse_argv(int argc
, char *argv
[]) {
2706 ARG_VERSION
= 0x100,
2713 ARG_NO_ASK_PASSWORD
,
2721 static const struct option options
[] = {
2722 { "help", no_argument
, NULL
, 'h' },
2723 { "version", no_argument
, NULL
, ARG_VERSION
},
2724 { "property", required_argument
, NULL
, 'p' },
2725 { "all", no_argument
, NULL
, 'a' },
2726 { "value", no_argument
, NULL
, ARG_VALUE
},
2727 { "full", no_argument
, NULL
, 'l' },
2728 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2729 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2730 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2731 { "signal", required_argument
, NULL
, 's' },
2732 { "host", required_argument
, NULL
, 'H' },
2733 { "machine", required_argument
, NULL
, 'M' },
2734 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2735 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2736 { "quiet", no_argument
, NULL
, 'q' },
2737 { "lines", required_argument
, NULL
, 'n' },
2738 { "output", required_argument
, NULL
, 'o' },
2739 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2740 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2741 { "force", no_argument
, NULL
, ARG_FORCE
},
2742 { "format", required_argument
, NULL
, ARG_FORMAT
},
2743 { "uid", required_argument
, NULL
, ARG_UID
},
2744 { "setenv", required_argument
, NULL
, 'E' },
2745 { "max-addresses", required_argument
, NULL
, ARG_NUMBER_IPS
},
2749 bool reorder
= false;
2750 int c
, r
, shell
= -1;
2756 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2758 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2764 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2765 * non-option argument was discovered. */
2769 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2770 * for switches after the main verb. However, for "shell" we really don't want that, since we
2771 * want that switches specified after the machine name are passed to the program to execute,
2772 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2773 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2774 * non-option parameter. If it's the verb "shell" we remember its position and continue
2775 * processing options. In this case, as soon as we hit the next non-option argument we found
2776 * the machine name, and stop further processing. If the first non-option argument is any other
2777 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2781 /* If we already found the "shell" verb on the command line, and now found the next
2782 * non-option argument, then this is the machine name and we should stop processing
2783 * further arguments. */
2784 optind
--; /* don't process this argument, go one step back */
2787 if (streq(optarg
, "shell"))
2788 /* Remember the position of the "shell" verb, and continue processing normally. */
2793 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2794 * processing normally. */
2797 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2798 * at least once with a reset option index. Hence, let's reset the option index here,
2799 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2800 * likely already processed it), and the bump the option index so that we read the
2801 * intended argument again. */
2802 saved_optind
= optind
;
2804 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2805 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2811 return help(0, NULL
, NULL
);
2817 r
= strv_extend(&arg_property
, optarg
);
2821 /* If the user asked for a particular
2822 * property, show it to him, even if it is
2840 if (safe_atou(optarg
, &arg_lines
) < 0) {
2841 log_error("Failed to parse lines '%s'", optarg
);
2847 if (streq(optarg
, "help")) {
2848 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2852 arg_output
= output_mode_from_string(optarg
);
2853 if (arg_output
< 0) {
2854 log_error("Unknown output '%s'.", optarg
);
2860 arg_pager_flags
|= PAGER_DISABLE
;
2868 arg_kill_who
= optarg
;
2872 if (streq(optarg
, "help")) {
2873 DUMP_STRING_TABLE(signal
, int, _NSIG
);
2877 arg_signal
= signal_from_string(optarg
);
2878 if (arg_signal
< 0) {
2879 log_error("Failed to parse signal string %s.", optarg
);
2884 case ARG_NO_ASK_PASSWORD
:
2885 arg_ask_password
= false;
2889 arg_transport
= BUS_TRANSPORT_REMOTE
;
2894 arg_transport
= BUS_TRANSPORT_MACHINE
;
2899 arg_read_only
= true;
2911 if (streq(optarg
, "help")) {
2912 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2916 arg_verify
= import_verify_from_string(optarg
);
2917 if (arg_verify
< 0) {
2918 log_error("Failed to parse --verify= setting: %s", optarg
);
2928 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2")) {
2929 log_error("Unknown format: %s", optarg
);
2933 arg_format
= optarg
;
2941 if (!env_assignment_is_valid(optarg
)) {
2942 log_error("Environment assignment invalid: %s", optarg
);
2946 r
= strv_extend(&arg_setenv
, optarg
);
2951 case ARG_NUMBER_IPS
:
2952 if (streq(optarg
, "all"))
2953 arg_addrs
= ALL_IP_ADDRESSES
;
2954 else if (safe_atoi(optarg
, &arg_addrs
) < 0) {
2955 log_error("Invalid number of IPs");
2957 } else if (arg_addrs
< 0) {
2958 log_error("Number of IPs cannot be negative");
2967 assert_not_reached("Unhandled option");
2976 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2977 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2979 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2982 for (i
= shell
; i
< optind
; i
++)
2983 argv
[i
] = argv
[i
+1];
2990 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2992 static const Verb verbs
[] = {
2993 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2994 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2995 { "list-images", VERB_ANY
, 1, 0, list_images
},
2996 { "status", 2, VERB_ANY
, 0, show_machine
},
2997 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2998 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2999 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
3000 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
3001 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
3002 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
3003 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
3004 { "kill", 2, VERB_ANY
, 0, kill_machine
},
3005 { "login", VERB_ANY
, 2, 0, login_machine
},
3006 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
3007 { "bind", 3, 4, 0, bind_mount
},
3008 { "copy-to", 3, 4, 0, copy_files
},
3009 { "copy-from", 3, 4, 0, copy_files
},
3010 { "remove", 2, VERB_ANY
, 0, remove_image
},
3011 { "rename", 3, 3, 0, rename_image
},
3012 { "clone", 3, 3, 0, clone_image
},
3013 { "read-only", 2, 3, 0, read_only_image
},
3014 { "start", 2, VERB_ANY
, 0, start_machine
},
3015 { "enable", 2, VERB_ANY
, 0, enable_machine
},
3016 { "disable", 2, VERB_ANY
, 0, enable_machine
},
3017 { "import-tar", 2, 3, 0, import_tar
},
3018 { "import-raw", 2, 3, 0, import_raw
},
3019 { "export-tar", 2, 3, 0, export_tar
},
3020 { "export-raw", 2, 3, 0, export_raw
},
3021 { "pull-tar", 2, 3, 0, pull_tar
},
3022 { "pull-raw", 2, 3, 0, pull_raw
},
3023 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
3024 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
3025 { "set-limit", 2, 3, 0, set_limit
},
3026 { "clean", VERB_ANY
, 1, 0, clean_images
},
3030 return dispatch_verb(argc
, argv
, verbs
, bus
);
3033 static int run(int argc
, char*argv
[]) {
3034 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
3037 setlocale(LC_ALL
, "");
3038 log_parse_environment();
3041 /* The journal merging logic potentially needs a lot of fds. */
3042 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
3046 r
= parse_argv(argc
, argv
);
3050 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
3052 return log_error_errno(r
, "Failed to create bus connection: %m");
3054 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
3056 return machinectl_main(argc
, argv
, bus
);
3059 DEFINE_MAIN_FUNCTION(run
);