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 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1715 "Invalid machine name %s.", name
);
1717 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1719 return log_error_errno(r
, "Failed to build unit name: %m");
1724 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1725 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1726 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1727 sd_bus
*bus
= userdata
;
1732 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1734 r
= bus_wait_for_jobs_new(bus
, &w
);
1738 for (i
= 1; i
< argc
; i
++) {
1739 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1740 _cleanup_free_
char *unit
= NULL
;
1743 r
= make_service_name(argv
[i
], &unit
);
1747 r
= image_exists(bus
, argv
[i
]);
1751 log_error("Machine image '%s' does not exist.", argv
[1]);
1755 r
= sd_bus_call_method(
1757 "org.freedesktop.systemd1",
1758 "/org/freedesktop/systemd1",
1759 "org.freedesktop.systemd1.Manager",
1763 "ss", unit
, "fail");
1765 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, -r
));
1767 r
= sd_bus_message_read(reply
, "o", &object
);
1769 return bus_log_parse_error(r
);
1771 r
= bus_wait_for_jobs_add(w
, object
);
1776 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1783 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1784 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1785 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1786 UnitFileChange
*changes
= NULL
;
1787 size_t n_changes
= 0;
1788 const char *method
= NULL
;
1789 sd_bus
*bus
= userdata
;
1794 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1796 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1798 r
= sd_bus_message_new_method_call(
1801 "org.freedesktop.systemd1",
1802 "/org/freedesktop/systemd1",
1803 "org.freedesktop.systemd1.Manager",
1806 return bus_log_create_error(r
);
1808 r
= sd_bus_message_open_container(m
, 'a', "s");
1810 return bus_log_create_error(r
);
1812 for (i
= 1; i
< argc
; i
++) {
1813 _cleanup_free_
char *unit
= NULL
;
1815 r
= make_service_name(argv
[i
], &unit
);
1819 r
= image_exists(bus
, argv
[i
]);
1823 log_error("Machine image '%s' does not exist.", argv
[1]);
1827 r
= sd_bus_message_append(m
, "s", unit
);
1829 return bus_log_create_error(r
);
1832 r
= sd_bus_message_close_container(m
);
1834 return bus_log_create_error(r
);
1836 if (streq(argv
[0], "enable"))
1837 r
= sd_bus_message_append(m
, "bb", false, false);
1839 r
= sd_bus_message_append(m
, "b", false);
1841 return bus_log_create_error(r
);
1843 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1845 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1847 if (streq(argv
[0], "enable")) {
1848 r
= sd_bus_message_read(reply
, "b", NULL
);
1850 return bus_log_parse_error(r
);
1853 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1857 r
= sd_bus_call_method(
1859 "org.freedesktop.systemd1",
1860 "/org/freedesktop/systemd1",
1861 "org.freedesktop.systemd1.Manager",
1867 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1874 unit_file_changes_free(changes
, n_changes
);
1879 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1880 const char **our_path
= userdata
, *line
;
1887 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1889 bus_log_parse_error(r
);
1893 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1896 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1899 log_full(priority
, "%s", line
);
1903 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1904 const char **our_path
= userdata
, *path
, *result
;
1911 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1913 bus_log_parse_error(r
);
1917 if (!streq_ptr(*our_path
, path
))
1920 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1924 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1929 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1931 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1935 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1936 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1937 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1938 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1939 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1940 const char *path
= NULL
;
1947 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1949 r
= sd_event_default(&event
);
1951 return log_error_errno(r
, "Failed to get event loop: %m");
1953 r
= sd_bus_attach_event(bus
, event
, 0);
1955 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1957 r
= sd_bus_match_signal_async(
1960 "org.freedesktop.import1",
1961 "/org/freedesktop/import1",
1962 "org.freedesktop.import1.Manager",
1964 match_transfer_removed
, NULL
, &path
);
1966 return log_error_errno(r
, "Failed to request match: %m");
1968 r
= sd_bus_match_signal_async(
1971 "org.freedesktop.import1",
1973 "org.freedesktop.import1.Transfer",
1975 match_log_message
, NULL
, &path
);
1977 return log_error_errno(r
, "Failed to request match: %m");
1979 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1981 return log_error_errno(r
, "Failed to transfer image: %s", bus_error_message(&error
, -r
));
1983 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1985 return bus_log_parse_error(r
);
1987 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1990 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1992 (void) sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1993 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1995 r
= sd_event_loop(event
);
1997 return log_error_errno(r
, "Failed to run event loop: %m");
2002 static int import_tar(int argc
, char *argv
[], void *userdata
) {
2003 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2004 _cleanup_free_
char *ll
= NULL
;
2005 _cleanup_close_
int fd
= -1;
2006 const char *local
= NULL
, *path
= NULL
;
2007 sd_bus
*bus
= userdata
;
2014 if (isempty(path
) || streq(path
, "-"))
2020 local
= basename(path
);
2021 if (isempty(local
) || streq(local
, "-"))
2025 log_error("Need either path or local name.");
2029 r
= tar_strip_suffixes(local
, &ll
);
2035 if (!machine_name_is_valid(local
)) {
2036 log_error("Local name %s is not a suitable machine name.", local
);
2041 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2043 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2046 r
= sd_bus_message_new_method_call(
2049 "org.freedesktop.import1",
2050 "/org/freedesktop/import1",
2051 "org.freedesktop.import1.Manager",
2054 return bus_log_create_error(r
);
2056 r
= sd_bus_message_append(
2059 fd
>= 0 ? fd
: STDIN_FILENO
,
2064 return bus_log_create_error(r
);
2066 return transfer_image_common(bus
, m
);
2069 static int import_raw(int argc
, char *argv
[], void *userdata
) {
2070 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2071 _cleanup_free_
char *ll
= NULL
;
2072 _cleanup_close_
int fd
= -1;
2073 const char *local
= NULL
, *path
= NULL
;
2074 sd_bus
*bus
= userdata
;
2081 if (isempty(path
) || streq(path
, "-"))
2087 local
= basename(path
);
2088 if (isempty(local
) || streq(local
, "-"))
2092 log_error("Need either path or local name.");
2096 r
= raw_strip_suffixes(local
, &ll
);
2102 if (!machine_name_is_valid(local
)) {
2103 log_error("Local name %s is not a suitable machine name.", local
);
2108 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2110 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2113 r
= sd_bus_message_new_method_call(
2116 "org.freedesktop.import1",
2117 "/org/freedesktop/import1",
2118 "org.freedesktop.import1.Manager",
2121 return bus_log_create_error(r
);
2123 r
= sd_bus_message_append(
2126 fd
>= 0 ? fd
: STDIN_FILENO
,
2131 return bus_log_create_error(r
);
2133 return transfer_image_common(bus
, m
);
2136 static void determine_compression_from_filename(const char *p
) {
2143 if (endswith(p
, ".xz"))
2145 else if (endswith(p
, ".gz"))
2146 arg_format
= "gzip";
2147 else if (endswith(p
, ".bz2"))
2148 arg_format
= "bzip2";
2151 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2152 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2153 _cleanup_close_
int fd
= -1;
2154 const char *local
= NULL
, *path
= NULL
;
2155 sd_bus
*bus
= userdata
;
2161 if (!machine_name_is_valid(local
)) {
2162 log_error("Machine name %s is not valid.", local
);
2168 if (isempty(path
) || streq(path
, "-"))
2172 determine_compression_from_filename(path
);
2174 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2176 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2179 r
= sd_bus_message_new_method_call(
2182 "org.freedesktop.import1",
2183 "/org/freedesktop/import1",
2184 "org.freedesktop.import1.Manager",
2187 return bus_log_create_error(r
);
2189 r
= sd_bus_message_append(
2193 fd
>= 0 ? fd
: STDOUT_FILENO
,
2196 return bus_log_create_error(r
);
2198 return transfer_image_common(bus
, m
);
2201 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2202 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2203 _cleanup_close_
int fd
= -1;
2204 const char *local
= NULL
, *path
= NULL
;
2205 sd_bus
*bus
= userdata
;
2211 if (!machine_name_is_valid(local
)) {
2212 log_error("Machine name %s is not valid.", local
);
2218 if (isempty(path
) || streq(path
, "-"))
2222 determine_compression_from_filename(path
);
2224 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2226 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2229 r
= sd_bus_message_new_method_call(
2232 "org.freedesktop.import1",
2233 "/org/freedesktop/import1",
2234 "org.freedesktop.import1.Manager",
2237 return bus_log_create_error(r
);
2239 r
= sd_bus_message_append(
2243 fd
>= 0 ? fd
: STDOUT_FILENO
,
2246 return bus_log_create_error(r
);
2248 return transfer_image_common(bus
, m
);
2251 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2252 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2253 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2254 const char *local
, *remote
;
2255 sd_bus
*bus
= userdata
;
2261 if (!http_url_is_valid(remote
)) {
2262 log_error("URL '%s' is not valid.", remote
);
2269 r
= import_url_last_component(remote
, &l
);
2271 return log_error_errno(r
, "Failed to get final component of URL: %m");
2276 if (isempty(local
) || streq(local
, "-"))
2280 r
= tar_strip_suffixes(local
, &ll
);
2286 if (!machine_name_is_valid(local
)) {
2287 log_error("Local name %s is not a suitable machine name.", local
);
2292 r
= sd_bus_message_new_method_call(
2295 "org.freedesktop.import1",
2296 "/org/freedesktop/import1",
2297 "org.freedesktop.import1.Manager",
2300 return bus_log_create_error(r
);
2302 r
= sd_bus_message_append(
2307 import_verify_to_string(arg_verify
),
2310 return bus_log_create_error(r
);
2312 return transfer_image_common(bus
, m
);
2315 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2316 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2317 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2318 const char *local
, *remote
;
2319 sd_bus
*bus
= userdata
;
2325 if (!http_url_is_valid(remote
)) {
2326 log_error("URL '%s' is not valid.", remote
);
2333 r
= import_url_last_component(remote
, &l
);
2335 return log_error_errno(r
, "Failed to get final component of URL: %m");
2340 if (isempty(local
) || streq(local
, "-"))
2344 r
= raw_strip_suffixes(local
, &ll
);
2350 if (!machine_name_is_valid(local
)) {
2351 log_error("Local name %s is not a suitable machine name.", local
);
2356 r
= sd_bus_message_new_method_call(
2359 "org.freedesktop.import1",
2360 "/org/freedesktop/import1",
2361 "org.freedesktop.import1.Manager",
2364 return bus_log_create_error(r
);
2366 r
= sd_bus_message_append(
2371 import_verify_to_string(arg_verify
),
2374 return bus_log_create_error(r
);
2376 return transfer_image_common(bus
, m
);
2379 typedef struct TransferInfo
{
2387 static int compare_transfer_info(const TransferInfo
*a
, const TransferInfo
*b
) {
2388 return strcmp(a
->local
, b
->local
);
2391 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2392 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2393 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2394 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2395 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2396 size_t n_transfers
= 0, n_allocated
= 0, j
;
2397 const char *type
, *remote
, *local
;
2398 sd_bus
*bus
= userdata
;
2399 uint32_t id
, max_id
= 0;
2403 (void) pager_open(arg_pager_flags
);
2405 r
= sd_bus_call_method(bus
,
2406 "org.freedesktop.import1",
2407 "/org/freedesktop/import1",
2408 "org.freedesktop.import1.Manager",
2414 return log_error_errno(r
, "Could not get transfers: %s", bus_error_message(&error
, -r
));
2416 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2418 return bus_log_parse_error(r
);
2420 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2423 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2426 transfers
[n_transfers
].id
= id
;
2427 transfers
[n_transfers
].type
= type
;
2428 transfers
[n_transfers
].remote
= remote
;
2429 transfers
[n_transfers
].local
= local
;
2430 transfers
[n_transfers
].progress
= progress
;
2450 return bus_log_parse_error(r
);
2452 r
= sd_bus_message_exit_container(reply
);
2454 return bus_log_parse_error(r
);
2456 typesafe_qsort(transfers
, n_transfers
, compare_transfer_info
);
2458 if (arg_legend
&& n_transfers
> 0)
2459 printf("%-*s %-*s %-*s %-*s %-*s\n",
2460 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2462 (int) max_type
, "TYPE",
2463 (int) max_local
, "LOCAL",
2464 (int) max_remote
, "REMOTE");
2466 for (j
= 0; j
< n_transfers
; j
++)
2467 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2468 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2469 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2470 (int) max_type
, transfers
[j
].type
,
2471 (int) max_local
, transfers
[j
].local
,
2472 (int) max_remote
, transfers
[j
].remote
);
2475 if (n_transfers
> 0)
2476 printf("\n%zu transfers listed.\n", n_transfers
);
2478 printf("No transfers.\n");
2484 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2485 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2486 sd_bus
*bus
= userdata
;
2491 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2493 for (i
= 1; i
< argc
; i
++) {
2496 r
= safe_atou32(argv
[i
], &id
);
2498 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2500 r
= sd_bus_call_method(
2502 "org.freedesktop.import1",
2503 "/org/freedesktop/import1",
2504 "org.freedesktop.import1.Manager",
2510 return log_error_errno(r
, "Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2516 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2517 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2518 sd_bus
*bus
= userdata
;
2522 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2524 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2525 limit
= (uint64_t) -1;
2527 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2529 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2533 /* With two arguments changes the quota limit of the
2534 * specified image */
2535 r
= sd_bus_call_method(
2537 "org.freedesktop.machine1",
2538 "/org/freedesktop/machine1",
2539 "org.freedesktop.machine1.Manager",
2543 "st", argv
[1], limit
);
2545 /* With one argument changes the pool quota limit */
2546 r
= sd_bus_call_method(
2548 "org.freedesktop.machine1",
2549 "/org/freedesktop/machine1",
2550 "org.freedesktop.machine1.Manager",
2557 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2562 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2563 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2564 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2565 uint64_t usage
, total
= 0;
2566 char fb
[FORMAT_BYTES_MAX
];
2567 sd_bus
*bus
= userdata
;
2572 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2574 r
= sd_bus_message_new_method_call(
2577 "org.freedesktop.machine1",
2578 "/org/freedesktop/machine1",
2579 "org.freedesktop.machine1.Manager",
2582 return bus_log_create_error(r
);
2584 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2586 return bus_log_create_error(r
);
2588 /* This is a slow operation, hence permit a longer time for completion. */
2589 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2591 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2593 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2595 return bus_log_parse_error(r
);
2597 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2598 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2599 name
, format_bytes(fb
, sizeof(fb
), usage
));
2605 r
= sd_bus_message_exit_container(reply
);
2607 return bus_log_parse_error(r
);
2609 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2610 c
, format_bytes(fb
, sizeof(fb
), total
));
2615 static int help(int argc
, char *argv
[], void *userdata
) {
2616 _cleanup_free_
char *link
= NULL
;
2619 (void) pager_open(arg_pager_flags
);
2621 r
= terminal_urlify_man("machinectl", "1", &link
);
2625 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2626 "Send control commands to or query the virtual machine and container\n"
2627 "registration manager.\n\n"
2628 " -h --help Show this help\n"
2629 " --version Show package version\n"
2630 " --no-pager Do not pipe output into a pager\n"
2631 " --no-legend Do not show the headers and footers\n"
2632 " --no-ask-password Do not ask for system passwords\n"
2633 " -H --host=[USER@]HOST Operate on remote host\n"
2634 " -M --machine=CONTAINER Operate on local container\n"
2635 " -p --property=NAME Show only properties by this name\n"
2636 " -q --quiet Suppress output\n"
2637 " -a --all Show all properties, including empty ones\n"
2638 " --value When showing properties, only print the value\n"
2639 " -l --full Do not ellipsize output\n"
2640 " --kill-who=WHO Who to send signal to\n"
2641 " -s --signal=SIGNAL Which signal to send\n"
2642 " --uid=USER Specify user ID to invoke shell as\n"
2643 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2644 " --read-only Create read-only bind mount\n"
2645 " --mkdir Create directory before bind mounting, if missing\n"
2646 " -n --lines=INTEGER Number of journal entries to show\n"
2647 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2648 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2649 " short-iso, short-iso-precise, short-full,\n"
2650 " short-monotonic, short-unix, verbose, export,\n"
2651 " json, json-pretty, json-sse, json-seq, cat,\n"
2653 " --verify=MODE Verification mode for downloaded images (no,\n"
2654 " checksum, signature)\n"
2655 " --force Download image even if already exists\n\n"
2656 "Machine Commands:\n"
2657 " list List running VMs and containers\n"
2658 " status NAME... Show VM/container details\n"
2659 " show [NAME...] Show properties of one or more VMs/containers\n"
2660 " start NAME... Start container as a service\n"
2661 " login [NAME] Get a login prompt in a container or on the\n"
2663 " shell [[USER@]NAME [COMMAND...]]\n"
2664 " Invoke a shell (or other command) in a container\n"
2665 " or on the local host\n"
2666 " enable NAME... Enable automatic container start at boot\n"
2667 " disable NAME... Disable automatic container start at boot\n"
2668 " poweroff NAME... Power off one or more containers\n"
2669 " reboot NAME... Reboot one or more containers\n"
2670 " terminate NAME... Terminate one or more VMs/containers\n"
2671 " kill NAME... Send signal to processes of a VM/container\n"
2672 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2673 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2674 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2676 " list-images Show available container and VM images\n"
2677 " image-status [NAME...] Show image details\n"
2678 " show-image [NAME...] Show properties of image\n"
2679 " clone NAME NAME Clone an image\n"
2680 " rename NAME NAME Rename an image\n"
2681 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2682 " remove NAME... Remove an image\n"
2683 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2684 " clean Remove hidden (or all) images\n\n"
2685 "Image Transfer Commands:\n"
2686 " pull-tar URL [NAME] Download a TAR container image\n"
2687 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2688 " import-tar FILE [NAME] Import a local TAR container image\n"
2689 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2690 " export-tar NAME [FILE] Export a TAR container image locally\n"
2691 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2692 " list-transfers Show list of downloads in progress\n"
2693 " cancel-transfer Cancel a download\n"
2694 "\nSee the %s for details.\n"
2695 , program_invocation_short_name
2702 static int parse_argv(int argc
, char *argv
[]) {
2705 ARG_VERSION
= 0x100,
2712 ARG_NO_ASK_PASSWORD
,
2720 static const struct option options
[] = {
2721 { "help", no_argument
, NULL
, 'h' },
2722 { "version", no_argument
, NULL
, ARG_VERSION
},
2723 { "property", required_argument
, NULL
, 'p' },
2724 { "all", no_argument
, NULL
, 'a' },
2725 { "value", no_argument
, NULL
, ARG_VALUE
},
2726 { "full", no_argument
, NULL
, 'l' },
2727 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2728 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2729 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2730 { "signal", required_argument
, NULL
, 's' },
2731 { "host", required_argument
, NULL
, 'H' },
2732 { "machine", required_argument
, NULL
, 'M' },
2733 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2734 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2735 { "quiet", no_argument
, NULL
, 'q' },
2736 { "lines", required_argument
, NULL
, 'n' },
2737 { "output", required_argument
, NULL
, 'o' },
2738 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2739 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2740 { "force", no_argument
, NULL
, ARG_FORCE
},
2741 { "format", required_argument
, NULL
, ARG_FORMAT
},
2742 { "uid", required_argument
, NULL
, ARG_UID
},
2743 { "setenv", required_argument
, NULL
, 'E' },
2744 { "max-addresses", required_argument
, NULL
, ARG_NUMBER_IPS
},
2748 bool reorder
= false;
2749 int c
, r
, shell
= -1;
2755 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2757 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2763 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2764 * non-option argument was discovered. */
2768 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2769 * for switches after the main verb. However, for "shell" we really don't want that, since we
2770 * want that switches specified after the machine name are passed to the program to execute,
2771 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2772 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2773 * non-option parameter. If it's the verb "shell" we remember its position and continue
2774 * processing options. In this case, as soon as we hit the next non-option argument we found
2775 * the machine name, and stop further processing. If the first non-option argument is any other
2776 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2780 /* If we already found the "shell" verb on the command line, and now found the next
2781 * non-option argument, then this is the machine name and we should stop processing
2782 * further arguments. */
2783 optind
--; /* don't process this argument, go one step back */
2786 if (streq(optarg
, "shell"))
2787 /* Remember the position of the "shell" verb, and continue processing normally. */
2792 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2793 * processing normally. */
2796 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2797 * at least once with a reset option index. Hence, let's reset the option index here,
2798 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2799 * likely already processed it), and the bump the option index so that we read the
2800 * intended argument again. */
2801 saved_optind
= optind
;
2803 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2804 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2810 return help(0, NULL
, NULL
);
2816 r
= strv_extend(&arg_property
, optarg
);
2820 /* If the user asked for a particular
2821 * property, show it to him, even if it is
2839 if (safe_atou(optarg
, &arg_lines
) < 0)
2840 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2841 "Failed to parse lines '%s'", optarg
);
2845 if (streq(optarg
, "help")) {
2846 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2850 arg_output
= output_mode_from_string(optarg
);
2852 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2853 "Unknown output '%s'.", optarg
);
2857 arg_pager_flags
|= PAGER_DISABLE
;
2865 arg_kill_who
= optarg
;
2869 if (streq(optarg
, "help")) {
2870 DUMP_STRING_TABLE(signal
, int, _NSIG
);
2874 arg_signal
= signal_from_string(optarg
);
2876 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2877 "Failed to parse signal string %s.", optarg
);
2880 case ARG_NO_ASK_PASSWORD
:
2881 arg_ask_password
= false;
2885 arg_transport
= BUS_TRANSPORT_REMOTE
;
2890 arg_transport
= BUS_TRANSPORT_MACHINE
;
2895 arg_read_only
= true;
2907 if (streq(optarg
, "help")) {
2908 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2912 arg_verify
= import_verify_from_string(optarg
);
2914 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2915 "Failed to parse --verify= setting: %s", optarg
);
2923 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2"))
2924 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2925 "Unknown format: %s", optarg
);
2927 arg_format
= optarg
;
2935 if (!env_assignment_is_valid(optarg
))
2936 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2937 "Environment assignment invalid: %s", optarg
);
2939 r
= strv_extend(&arg_setenv
, optarg
);
2944 case ARG_NUMBER_IPS
:
2945 if (streq(optarg
, "all"))
2946 arg_addrs
= ALL_IP_ADDRESSES
;
2947 else if (safe_atoi(optarg
, &arg_addrs
) < 0)
2948 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2949 "Invalid number of IPs");
2950 else if (arg_addrs
< 0)
2951 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2952 "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 static int run(int argc
, char*argv
[]) {
3026 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
3029 setlocale(LC_ALL
, "");
3030 log_parse_environment();
3033 /* The journal merging logic potentially needs a lot of fds. */
3034 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
3038 r
= parse_argv(argc
, argv
);
3042 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
3044 return log_error_errno(r
, "Failed to create bus connection: %m");
3046 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
3048 return machinectl_main(argc
, argv
, bus
);
3051 DEFINE_MAIN_FUNCTION(run
);