2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <arpa/inet.h>
26 #include <netinet/in.h>
28 #include <sys/mount.h>
29 #include <sys/socket.h>
34 #include "alloc-util.h"
35 #include "bus-common-errors.h"
36 #include "bus-error.h"
37 #include "bus-unit-util.h"
39 #include "cgroup-show.h"
40 #include "cgroup-util.h"
44 #include "hostname-util.h"
45 #include "import-util.h"
47 #include "logs-show.h"
51 #include "parse-util.h"
52 #include "path-util.h"
53 #include "process-util.h"
56 #include "signal-util.h"
57 #include "spawn-polkit-agent.h"
58 #include "stdio-util.h"
60 #include "terminal-util.h"
61 #include "unit-name.h"
66 #define ALL_IP_ADDRESSES -1
68 static char **arg_property
= NULL
;
69 static bool arg_all
= false;
70 static bool arg_value
= false;
71 static bool arg_full
= false;
72 static bool arg_no_pager
= false;
73 static bool arg_legend
= true;
74 static const char *arg_kill_who
= NULL
;
75 static int arg_signal
= SIGTERM
;
76 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
77 static char *arg_host
= NULL
;
78 static bool arg_read_only
= false;
79 static bool arg_mkdir
= false;
80 static bool arg_quiet
= false;
81 static bool arg_ask_password
= true;
82 static unsigned arg_lines
= 10;
83 static OutputMode arg_output
= OUTPUT_SHORT
;
84 static bool arg_force
= false;
85 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
86 static const char* arg_format
= NULL
;
87 static const char *arg_uid
= NULL
;
88 static char **arg_setenv
= NULL
;
89 static int arg_addrs
= 1;
91 static int print_addresses(sd_bus
*bus
, const char *name
, int, const char *pr1
, const char *pr2
, int n_addr
);
93 static OutputFlags
get_output_flags(void) {
95 arg_all
* OUTPUT_SHOW_ALL
|
96 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
97 colors_enabled() * OUTPUT_COLOR
|
98 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
101 typedef struct MachineInfo
{
109 static int compare_machine_info(const void *a
, const void *b
) {
110 const MachineInfo
*x
= a
, *y
= b
;
112 return strcmp(x
->name
, y
->name
);
115 static void clean_machine_info(MachineInfo
*machines
, size_t n_machines
) {
118 if (!machines
|| n_machines
== 0)
121 for (i
= 0; i
< n_machines
; i
++) {
122 free(machines
[i
].os
);
123 free(machines
[i
].version_id
);
128 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
129 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
130 const char *k
, *v
, *iter
, **query_res
= NULL
;
131 size_t count
= 0, awaited_args
= 0;
139 NULSTR_FOREACH(iter
, query
)
141 query_res
= newa0(const char *, awaited_args
);
143 r
= sd_bus_call_method(
145 "org.freedesktop.machine1",
146 "/org/freedesktop/machine1",
147 "org.freedesktop.machine1.Manager",
149 NULL
, &reply
, "s", name
);
153 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
155 return bus_log_parse_error(r
);
157 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
159 NULSTR_FOREACH(iter
, query
) {
160 if (streq(k
, iter
)) {
161 query_res
[count
] = v
;
168 return bus_log_parse_error(r
);
170 r
= sd_bus_message_exit_container(reply
);
172 return bus_log_parse_error(r
);
175 for (count
= 0; count
< awaited_args
; count
++) {
178 out
= va_arg(ap
, char **);
180 if (query_res
[count
]) {
181 val
= strdup(query_res
[count
]);
194 static int list_machines(int argc
, char *argv
[], void *userdata
) {
196 size_t max_name
= strlen("MACHINE"), max_class
= strlen("CLASS"),
197 max_service
= strlen("SERVICE"), max_os
= strlen("OS"), max_version_id
= strlen("VERSION");
198 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
199 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
200 _cleanup_free_
char *prefix
= NULL
;
201 MachineInfo
*machines
= NULL
;
202 const char *name
, *class, *service
, *object
;
203 size_t n_machines
= 0, n_allocated
= 0, j
;
204 sd_bus
*bus
= userdata
;
209 pager_open(arg_no_pager
, false);
211 r
= sd_bus_call_method(bus
,
212 "org.freedesktop.machine1",
213 "/org/freedesktop/machine1",
214 "org.freedesktop.machine1.Manager",
220 log_error("Could not get machines: %s", bus_error_message(&error
, -r
));
224 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
226 return bus_log_parse_error(r
);
227 while ((r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, &object
)) > 0) {
230 if (name
[0] == '.' && !arg_all
)
233 if (!GREEDY_REALLOC0(machines
, n_allocated
, n_machines
+ 1)) {
238 machines
[n_machines
].name
= name
;
239 machines
[n_machines
].class = class;
240 machines
[n_machines
].service
= service
;
242 (void) call_get_os_release(
244 "GetMachineOSRelease",
248 &machines
[n_machines
].os
,
249 &machines
[n_machines
].version_id
);
263 l
= machines
[n_machines
].os
? strlen(machines
[n_machines
].os
) : 1;
267 l
= machines
[n_machines
].version_id
? strlen(machines
[n_machines
].version_id
) : 1;
268 if (l
> max_version_id
)
274 r
= bus_log_parse_error(r
);
278 r
= sd_bus_message_exit_container(reply
);
280 r
= bus_log_parse_error(r
);
284 qsort_safe(machines
, n_machines
, sizeof(MachineInfo
), compare_machine_info
);
286 /* Allocate for prefix max characters for all fields + spaces between them + strlen(",\n") */
287 r
= asprintf(&prefix
, "%-*s",
292 max_version_id
+ 5 + strlen(",\n")),
299 if (arg_legend
&& n_machines
> 0)
300 printf("%-*s %-*s %-*s %-*s %-*s %s\n",
301 (int) max_name
, "MACHINE",
302 (int) max_class
, "CLASS",
303 (int) max_service
, "SERVICE",
305 (int) max_version_id
, "VERSION",
308 for (j
= 0; j
< n_machines
; j
++) {
309 printf("%-*s %-*s %-*s %-*s %-*s ",
310 (int) max_name
, machines
[j
].name
,
311 (int) max_class
, machines
[j
].class,
312 (int) max_service
, strdash_if_empty(machines
[j
].service
),
313 (int) max_os
, strdash_if_empty(machines
[j
].os
),
314 (int) max_version_id
, strdash_if_empty(machines
[j
].version_id
));
316 r
= print_addresses(bus
, machines
[j
].name
, 0, "", prefix
, arg_addrs
);
317 if (r
<= 0) /* error or no addresses defined? */
318 fputs("-\n", stdout
);
325 printf("\n%zu machines listed.\n", n_machines
);
327 printf("No machines.\n");
332 clean_machine_info(machines
, n_machines
);
336 typedef struct ImageInfo
{
345 static int compare_image_info(const void *a
, const void *b
) {
346 const ImageInfo
*x
= a
, *y
= b
;
348 return strcmp(x
->name
, y
->name
);
351 static int list_images(int argc
, char *argv
[], void *userdata
) {
353 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
354 size_t max_name
= strlen("NAME"), max_type
= strlen("TYPE"), max_size
= strlen("USAGE"), max_crtime
= strlen("CREATED"), max_mtime
= strlen("MODIFIED");
355 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
356 _cleanup_free_ ImageInfo
*images
= NULL
;
357 size_t n_images
= 0, n_allocated
= 0, j
;
358 const char *name
, *type
, *object
;
359 sd_bus
*bus
= userdata
;
360 uint64_t crtime
, mtime
, size
;
365 pager_open(arg_no_pager
, false);
367 r
= sd_bus_call_method(bus
,
368 "org.freedesktop.machine1",
369 "/org/freedesktop/machine1",
370 "org.freedesktop.machine1.Manager",
376 log_error("Could not get images: %s", bus_error_message(&error
, -r
));
380 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
382 return bus_log_parse_error(r
);
384 while ((r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &read_only
, &crtime
, &mtime
, &size
, &object
)) > 0) {
385 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, FORMAT_BYTES_MAX
)];
388 if (name
[0] == '.' && !arg_all
)
391 if (!GREEDY_REALLOC(images
, n_allocated
, n_images
+ 1))
394 images
[n_images
].name
= name
;
395 images
[n_images
].type
= type
;
396 images
[n_images
].read_only
= read_only
;
397 images
[n_images
].crtime
= crtime
;
398 images
[n_images
].mtime
= mtime
;
399 images
[n_images
].size
= size
;
410 l
= strlen(strna(format_timestamp(buf
, sizeof(buf
), crtime
)));
416 l
= strlen(strna(format_timestamp(buf
, sizeof(buf
), mtime
)));
421 if (size
!= (uint64_t) -1) {
422 l
= strlen(strna(format_bytes(buf
, sizeof(buf
), size
)));
430 return bus_log_parse_error(r
);
432 r
= sd_bus_message_exit_container(reply
);
434 return bus_log_parse_error(r
);
436 qsort_safe(images
, n_images
, sizeof(ImageInfo
), compare_image_info
);
438 if (arg_legend
&& n_images
> 0)
439 printf("%-*s %-*s %-3s %-*s %-*s %-*s\n",
440 (int) max_name
, "NAME",
441 (int) max_type
, "TYPE",
443 (int) max_size
, "USAGE",
444 (int) max_crtime
, "CREATED",
445 (int) max_mtime
, "MODIFIED");
447 for (j
= 0; j
< n_images
; j
++) {
448 char crtime_buf
[FORMAT_TIMESTAMP_MAX
], mtime_buf
[FORMAT_TIMESTAMP_MAX
], size_buf
[FORMAT_BYTES_MAX
];
450 printf("%-*s %-*s %s%-3s%s %-*s %-*s %-*s\n",
451 (int) max_name
, images
[j
].name
,
452 (int) max_type
, images
[j
].type
,
453 images
[j
].read_only
? ansi_highlight_red() : "", yes_no(images
[j
].read_only
), images
[j
].read_only
? ansi_normal() : "",
454 (int) max_size
, strna(format_bytes(size_buf
, sizeof(size_buf
), images
[j
].size
)),
455 (int) max_crtime
, strna(format_timestamp(crtime_buf
, sizeof(crtime_buf
), images
[j
].crtime
)),
456 (int) max_mtime
, strna(format_timestamp(mtime_buf
, sizeof(mtime_buf
), images
[j
].mtime
)));
461 printf("\n%zu images listed.\n", n_images
);
463 printf("No images.\n");
469 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
470 _cleanup_free_
char *cgroup
= NULL
;
471 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
478 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
491 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
494 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
497 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
499 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
502 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
504 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
509 static int print_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
) {
510 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
511 _cleanup_free_
char *addresses
= NULL
;
512 bool truncate
= false;
521 r
= sd_bus_call_method(bus
,
522 "org.freedesktop.machine1",
523 "/org/freedesktop/machine1",
524 "org.freedesktop.machine1.Manager",
525 "GetMachineAddresses",
532 addresses
= strdup(prefix
);
537 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
539 return bus_log_parse_error(r
);
541 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
545 char buf_ifi
[DECIMAL_STR_MAX(int) + 2], buffer
[MAX(INET6_ADDRSTRLEN
, INET_ADDRSTRLEN
)];
547 r
= sd_bus_message_read(reply
, "i", &family
);
549 return bus_log_parse_error(r
);
551 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
553 return bus_log_parse_error(r
);
556 if (family
== AF_INET6
&& ifi
> 0)
557 xsprintf(buf_ifi
, "%%%i", ifi
);
561 if (!strextend(&addresses
, prefix
, inet_ntop(family
, a
, buffer
, sizeof(buffer
)), buf_ifi
, NULL
))
566 r
= sd_bus_message_exit_container(reply
);
568 return bus_log_parse_error(r
);
570 if (prefix
!= prefix2
)
579 return bus_log_parse_error(r
);
581 r
= sd_bus_message_exit_container(reply
);
583 return bus_log_parse_error(r
);
586 fprintf(stdout
, "%s%s", addresses
, truncate
? "..." : "");
591 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
592 _cleanup_free_
char *pretty
= NULL
;
599 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
604 printf("%s%s\n", prefix
, pretty
);
609 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
610 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
611 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
618 r
= sd_bus_call_method(bus
,
619 "org.freedesktop.machine1",
620 "/org/freedesktop/machine1",
621 "org.freedesktop.machine1.Manager",
622 "GetMachineUIDShift",
627 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
629 r
= sd_bus_message_read(reply
, "u", &shift
);
633 if (shift
== 0) /* Don't show trivial mappings */
636 printf(" UID Shift: %" PRIu32
"\n", shift
);
640 typedef struct MachineStatusInfo
{
646 char *root_directory
;
648 struct dual_timestamp timestamp
;
653 static void machine_status_info_clear(MachineStatusInfo
*info
) {
659 free(info
->root_directory
);
665 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
666 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
667 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
673 fputs(strna(i
->name
), stdout
);
675 if (!sd_id128_is_null(i
->id
))
676 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
680 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->timestamp
.realtime
);
681 s2
= format_timestamp(since2
, sizeof(since2
), i
->timestamp
.realtime
);
684 printf("\t Since: %s; %s\n", s2
, s1
);
686 printf("\t Since: %s\n", s2
);
689 _cleanup_free_
char *t
= NULL
;
691 printf("\t Leader: %u", (unsigned) i
->leader
);
693 get_process_comm(i
->leader
, &t
);
701 printf("\t Service: %s", i
->service
);
704 printf("; class %s", i
->class);
708 printf("\t Class: %s\n", i
->class);
710 if (i
->root_directory
)
711 printf("\t Root: %s\n", i
->root_directory
);
713 if (i
->n_netif
> 0) {
716 fputs("\t Iface:", stdout
);
718 for (c
= 0; c
< i
->n_netif
; c
++) {
719 char name
[IF_NAMESIZE
+1] = "";
721 if (if_indextoname(i
->netif
[c
], name
)) {
730 printf(" %i", i
->netif
[c
]);
736 if (print_addresses(bus
, i
->name
, ifi
,
739 ALL_IP_ADDRESSES
) > 0)
742 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
744 print_uid_shift(bus
, i
->name
);
747 printf("\t Unit: %s\n", i
->unit
);
748 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
750 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
752 show_journal_by_unit(
757 i
->timestamp
.monotonic
,
760 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
761 SD_JOURNAL_LOCAL_ONLY
,
767 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
768 MachineStatusInfo
*i
= userdata
;
773 assert_cc(sizeof(int32_t) == sizeof(int));
774 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
780 i
->n_netif
= l
/ sizeof(int32_t);
781 i
->netif
= memdup(v
, l
);
788 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
790 static const struct bus_properties_map map
[] = {
791 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
792 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
793 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
794 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
795 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
796 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
797 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
798 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
799 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
800 { "NetworkInterfaces", "ai", map_netif
, 0 },
804 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
805 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
813 r
= bus_map_all_properties(bus
,
814 "org.freedesktop.machine1",
820 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
826 print_machine_status_info(bus
, &info
);
831 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
843 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, arg_property
, arg_value
, arg_all
);
845 log_error_errno(r
, "Could not get properties: %m");
850 static int show_machine(int argc
, char *argv
[], void *userdata
) {
852 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
853 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
854 bool properties
, new_line
= false;
855 sd_bus
*bus
= userdata
;
860 properties
= !strstr(argv
[0], "status");
862 pager_open(arg_no_pager
, false);
864 if (properties
&& argc
<= 1) {
866 /* If no argument is specified, inspect the manager
868 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
873 for (i
= 1; i
< argc
; i
++) {
874 const char *path
= NULL
;
876 r
= sd_bus_call_method(bus
,
877 "org.freedesktop.machine1",
878 "/org/freedesktop/machine1",
879 "org.freedesktop.machine1.Manager",
885 log_error("Could not get path to machine: %s", bus_error_message(&error
, -r
));
889 r
= sd_bus_message_read(reply
, "o", &path
);
891 return bus_log_parse_error(r
);
894 r
= show_machine_properties(bus
, path
, &new_line
);
896 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
902 typedef struct ImageStatusInfo
{
911 uint64_t usage_exclusive
;
912 uint64_t limit_exclusive
;
915 static void image_status_info_clear(ImageStatusInfo
*info
) {
924 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
925 char ts_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
926 char ts_absolute
[FORMAT_TIMESTAMP_MAX
], *s2
;
927 char bs
[FORMAT_BYTES_MAX
], *s3
;
928 char bs_exclusive
[FORMAT_BYTES_MAX
], *s4
;
934 fputs(i
->name
, stdout
);
939 printf("\t Type: %s\n", i
->type
);
942 printf("\t Path: %s\n", i
->path
);
944 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
946 printf("\t RO: %s%s%s\n",
947 i
->read_only
? ansi_highlight_red() : "",
948 i
->read_only
? "read-only" : "writable",
949 i
->read_only
? ansi_normal() : "");
951 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->crtime
);
952 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->crtime
);
954 printf("\t Created: %s; %s\n", s2
, s1
);
956 printf("\t Created: %s\n", s2
);
958 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->mtime
);
959 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->mtime
);
961 printf("\tModified: %s; %s\n", s2
, s1
);
963 printf("\tModified: %s\n", s2
);
965 s3
= format_bytes(bs
, sizeof(bs
), i
->usage
);
966 s4
= i
->usage_exclusive
!= i
->usage
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->usage_exclusive
) : NULL
;
968 printf("\t Usage: %s (exclusive: %s)\n", s3
, s4
);
970 printf("\t Usage: %s\n", s3
);
972 s3
= format_bytes(bs
, sizeof(bs
), i
->limit
);
973 s4
= i
->limit_exclusive
!= i
->limit
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->limit_exclusive
) : NULL
;
975 printf("\t Limit: %s (exclusive: %s)\n", s3
, s4
);
977 printf("\t Limit: %s\n", s3
);
980 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
982 static const struct bus_properties_map map
[] = {
983 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
984 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
985 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
986 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
987 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
988 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
989 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
990 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
991 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
992 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
996 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
997 _cleanup_(image_status_info_clear
) ImageStatusInfo info
= {};
1004 r
= bus_map_all_properties(bus
,
1005 "org.freedesktop.machine1",
1011 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1017 print_image_status_info(bus
, &info
);
1022 typedef struct PoolStatusInfo
{
1028 static void pool_status_info_clear(PoolStatusInfo
*info
) {
1037 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
1038 char bs
[FORMAT_BYTES_MAX
], *s
;
1041 printf("\t Path: %s\n", i
->path
);
1043 s
= format_bytes(bs
, sizeof(bs
), i
->usage
);
1045 printf("\t Usage: %s\n", s
);
1047 s
= format_bytes(bs
, sizeof(bs
), i
->limit
);
1049 printf("\t Limit: %s\n", s
);
1052 static int show_pool_info(sd_bus
*bus
) {
1054 static const struct bus_properties_map map
[] = {
1055 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
1056 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
1057 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
1061 _cleanup_(pool_status_info_clear
) PoolStatusInfo info
= {
1062 .usage
= (uint64_t) -1,
1063 .limit
= (uint64_t) -1,
1066 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1071 r
= bus_map_all_properties(bus
,
1072 "org.freedesktop.machine1",
1073 "/org/freedesktop/machine1",
1078 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1080 print_pool_status_info(bus
, &info
);
1086 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
1098 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, arg_property
, arg_value
, arg_all
);
1100 log_error_errno(r
, "Could not get properties: %m");
1105 static int show_image(int argc
, char *argv
[], void *userdata
) {
1107 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1108 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1109 bool properties
, new_line
= false;
1110 sd_bus
*bus
= userdata
;
1115 properties
= !strstr(argv
[0], "status");
1117 pager_open(arg_no_pager
, false);
1121 /* If no argument is specified, inspect the manager
1125 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1127 r
= show_pool_info(bus
);
1132 for (i
= 1; i
< argc
; i
++) {
1133 const char *path
= NULL
;
1135 r
= sd_bus_call_method(
1137 "org.freedesktop.machine1",
1138 "/org/freedesktop/machine1",
1139 "org.freedesktop.machine1.Manager",
1145 log_error("Could not get path to image: %s", bus_error_message(&error
, -r
));
1149 r
= sd_bus_message_read(reply
, "o", &path
);
1151 return bus_log_parse_error(r
);
1154 r
= show_image_properties(bus
, path
, &new_line
);
1156 r
= show_image_info(bus
, path
, &new_line
);
1162 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1163 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1164 sd_bus
*bus
= userdata
;
1169 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1172 arg_kill_who
= "all";
1174 for (i
= 1; i
< argc
; i
++) {
1175 r
= sd_bus_call_method(
1177 "org.freedesktop.machine1",
1178 "/org/freedesktop/machine1",
1179 "org.freedesktop.machine1.Manager",
1183 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1185 log_error("Could not kill machine: %s", bus_error_message(&error
, -r
));
1193 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1194 arg_kill_who
= "leader";
1195 arg_signal
= SIGINT
; /* sysvinit + systemd */
1197 return kill_machine(argc
, argv
, userdata
);
1200 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1201 arg_kill_who
= "leader";
1202 arg_signal
= SIGRTMIN
+4; /* only systemd */
1204 return kill_machine(argc
, argv
, userdata
);
1207 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1208 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1209 sd_bus
*bus
= userdata
;
1214 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1216 for (i
= 1; i
< argc
; i
++) {
1217 r
= sd_bus_call_method(
1219 "org.freedesktop.machine1",
1220 "/org/freedesktop/machine1",
1221 "org.freedesktop.machine1.Manager",
1227 log_error("Could not terminate machine: %s", bus_error_message(&error
, -r
));
1235 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1236 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1237 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1238 _cleanup_free_
char *abs_host_path
= NULL
;
1239 char *dest
, *host_path
, *container_path
;
1240 sd_bus
*bus
= userdata
;
1246 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1248 copy_from
= streq(argv
[0], "copy-from");
1249 dest
= argv
[3] ?: argv
[2];
1250 host_path
= copy_from
? dest
: argv
[2];
1251 container_path
= copy_from
? argv
[2] : dest
;
1253 if (!path_is_absolute(host_path
)) {
1254 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1256 return log_error_errno(r
, "Failed to make path absolute: %m");
1258 host_path
= abs_host_path
;
1261 r
= sd_bus_message_new_method_call(
1264 "org.freedesktop.machine1",
1265 "/org/freedesktop/machine1",
1266 "org.freedesktop.machine1.Manager",
1267 copy_from
? "CopyFromMachine" : "CopyToMachine");
1269 return bus_log_create_error(r
);
1271 r
= sd_bus_message_append(
1275 copy_from
? container_path
: host_path
,
1276 copy_from
? host_path
: container_path
);
1278 return bus_log_create_error(r
);
1280 /* This is a slow operation, hence turn off any method call timeouts */
1281 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1283 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1288 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1289 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1290 sd_bus
*bus
= userdata
;
1295 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1297 r
= sd_bus_call_method(
1299 "org.freedesktop.machine1",
1300 "/org/freedesktop/machine1",
1301 "org.freedesktop.machine1.Manager",
1312 log_error("Failed to bind mount: %s", bus_error_message(&error
, -r
));
1319 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1320 PTYForward
** forward
= (PTYForward
**) userdata
;
1327 /* If the forwarder is already initialized, tell it to
1328 * exit on the next vhangup(), so that we still flush
1329 * out what might be queued and exit then. */
1331 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1335 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1338 /* On error, or when the forwarder is not initialized yet, quit immediately */
1339 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1343 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1349 assert(master
>= 0);
1352 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1355 if (streq(name
, ".host"))
1356 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1358 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1361 sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1362 sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1364 r
= pty_forward_new(event
, master
, flags
, forward
);
1366 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1368 r
= sd_event_loop(event
);
1370 return log_error_errno(r
, "Failed to run event loop: %m");
1372 pty_forward_get_last_char(*forward
, &last_char
);
1375 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1376 pty_forward_get_ignore_vhangup(*forward
) == 0;
1378 *forward
= pty_forward_free(*forward
);
1380 if (last_char
!= '\n')
1381 fputc('\n', stdout
);
1385 log_info("Machine %s terminated.", name
);
1386 else if (streq(name
, ".host"))
1387 log_info("Connection to the local host terminated.");
1389 log_info("Connection to machine %s terminated.", name
);
1392 sd_event_get_exit_code(event
, &ret
);
1396 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1398 * Whatever is specified in the spec takes priority over global arguments.
1401 const char *_machine
= NULL
;
1406 at
= strchr(spec
, '@');
1409 /* Do the same as ssh and refuse "@host". */
1413 _uid
= strndup(spec
, at
- spec
);
1420 if (arg_uid
&& !_uid
) {
1421 _uid
= strdup(arg_uid
);
1427 *machine
= isempty(_machine
) ? ".host" : _machine
;
1431 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1432 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1433 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1434 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1435 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1436 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1438 sd_bus
*bus
= userdata
;
1439 const char *pty
, *match
, *machine
;
1443 if (!strv_isempty(arg_setenv
) || arg_uid
) {
1444 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1448 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1449 log_error("Login only supported on local machines.");
1453 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1455 r
= sd_event_default(&event
);
1457 return log_error_errno(r
, "Failed to get event loop: %m");
1459 r
= sd_bus_attach_event(bus
, event
, 0);
1461 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1463 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1465 match
= strjoina("type='signal',"
1466 "sender='org.freedesktop.machine1',"
1467 "path='/org/freedesktop/machine1',",
1468 "interface='org.freedesktop.machine1.Manager',"
1469 "member='MachineRemoved',"
1470 "arg0='", machine
, "'");
1472 r
= sd_bus_add_match(bus
, &slot
, match
, on_machine_removed
, &forward
);
1474 return log_error_errno(r
, "Failed to add machine removal match: %m");
1476 r
= sd_bus_call_method(
1478 "org.freedesktop.machine1",
1479 "/org/freedesktop/machine1",
1480 "org.freedesktop.machine1.Manager",
1486 log_error("Failed to get login PTY: %s", bus_error_message(&error
, -r
));
1490 r
= sd_bus_message_read(reply
, "hs", &master
, &pty
);
1492 return bus_log_parse_error(r
);
1494 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1497 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1498 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1499 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1500 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1501 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1502 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1504 sd_bus
*bus
= userdata
;
1505 const char *pty
, *match
, *machine
, *path
;
1506 _cleanup_free_
char *uid
= NULL
;
1510 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1511 log_error("Shell only supported on local machines.");
1515 /* Pass $TERM to shell session, if not explicitly specified. */
1516 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1519 t
= strv_find_prefix(environ
, "TERM=");
1521 if (strv_extend(&arg_setenv
, t
) < 0)
1526 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1528 r
= sd_event_default(&event
);
1530 return log_error_errno(r
, "Failed to get event loop: %m");
1532 r
= sd_bus_attach_event(bus
, event
, 0);
1534 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1536 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1538 return log_error_errno(r
, "Failed to parse machine specification: %m");
1540 match
= strjoina("type='signal',"
1541 "sender='org.freedesktop.machine1',"
1542 "path='/org/freedesktop/machine1',",
1543 "interface='org.freedesktop.machine1.Manager',"
1544 "member='MachineRemoved',"
1545 "arg0='", machine
, "'");
1547 r
= sd_bus_add_match(bus
, &slot
, match
, on_machine_removed
, &forward
);
1549 return log_error_errno(r
, "Failed to add machine removal match: %m");
1551 r
= sd_bus_message_new_method_call(
1554 "org.freedesktop.machine1",
1555 "/org/freedesktop/machine1",
1556 "org.freedesktop.machine1.Manager",
1557 "OpenMachineShell");
1559 return bus_log_create_error(r
);
1561 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1563 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1565 return bus_log_create_error(r
);
1567 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1569 return bus_log_create_error(r
);
1571 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1573 return bus_log_create_error(r
);
1575 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1577 log_error("Failed to get shell PTY: %s", bus_error_message(&error
, -r
));
1581 r
= sd_bus_message_read(reply
, "hs", &master
, &pty
);
1583 return bus_log_parse_error(r
);
1585 return process_forward(event
, &forward
, master
, 0, machine
);
1588 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1589 sd_bus
*bus
= userdata
;
1594 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1596 for (i
= 1; i
< argc
; i
++) {
1597 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1598 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1600 r
= sd_bus_message_new_method_call(
1603 "org.freedesktop.machine1",
1604 "/org/freedesktop/machine1",
1605 "org.freedesktop.machine1.Manager",
1608 return bus_log_create_error(r
);
1610 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1612 return bus_log_create_error(r
);
1614 /* This is a slow operation, hence turn off any method call timeouts */
1615 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1617 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1623 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1624 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1625 sd_bus
*bus
= userdata
;
1628 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1630 r
= sd_bus_call_method(
1632 "org.freedesktop.machine1",
1633 "/org/freedesktop/machine1",
1634 "org.freedesktop.machine1.Manager",
1638 "ss", argv
[1], argv
[2]);
1640 log_error("Could not rename image: %s", bus_error_message(&error
, -r
));
1647 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1648 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1649 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1650 sd_bus
*bus
= userdata
;
1653 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1655 r
= sd_bus_message_new_method_call(
1658 "org.freedesktop.machine1",
1659 "/org/freedesktop/machine1",
1660 "org.freedesktop.machine1.Manager",
1663 return bus_log_create_error(r
);
1665 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1667 return bus_log_create_error(r
);
1669 /* This is a slow operation, hence turn off any method call timeouts */
1670 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1672 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1677 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1678 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1679 sd_bus
*bus
= userdata
;
1683 b
= parse_boolean(argv
[2]);
1685 log_error("Failed to parse boolean argument: %s", argv
[2]);
1690 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1692 r
= sd_bus_call_method(
1694 "org.freedesktop.machine1",
1695 "/org/freedesktop/machine1",
1696 "org.freedesktop.machine1.Manager",
1697 "MarkImageReadOnly",
1702 log_error("Could not mark image read-only: %s", bus_error_message(&error
, -r
));
1709 static int image_exists(sd_bus
*bus
, const char *name
) {
1710 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1716 r
= sd_bus_call_method(
1718 "org.freedesktop.machine1",
1719 "/org/freedesktop/machine1",
1720 "org.freedesktop.machine1.Manager",
1726 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1729 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, -r
));
1735 static int make_service_name(const char *name
, char **ret
) {
1741 if (!machine_name_is_valid(name
)) {
1742 log_error("Invalid machine name %s.", name
);
1746 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1748 return log_error_errno(r
, "Failed to build unit name: %m");
1753 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1754 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1755 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1756 sd_bus
*bus
= userdata
;
1761 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1763 r
= bus_wait_for_jobs_new(bus
, &w
);
1767 for (i
= 1; i
< argc
; i
++) {
1768 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1769 _cleanup_free_
char *unit
= NULL
;
1772 r
= make_service_name(argv
[i
], &unit
);
1776 r
= image_exists(bus
, argv
[i
]);
1780 log_error("Machine image '%s' does not exist.", argv
[1]);
1784 r
= sd_bus_call_method(
1786 "org.freedesktop.systemd1",
1787 "/org/freedesktop/systemd1",
1788 "org.freedesktop.systemd1.Manager",
1792 "ss", unit
, "fail");
1794 log_error("Failed to start unit: %s", bus_error_message(&error
, -r
));
1798 r
= sd_bus_message_read(reply
, "o", &object
);
1800 return bus_log_parse_error(r
);
1802 r
= bus_wait_for_jobs_add(w
, object
);
1807 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1814 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1815 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1816 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1817 UnitFileChange
*changes
= NULL
;
1818 unsigned n_changes
= 0;
1819 int carries_install_info
= 0;
1820 const char *method
= NULL
;
1821 sd_bus
*bus
= userdata
;
1826 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1828 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1830 r
= sd_bus_message_new_method_call(
1833 "org.freedesktop.systemd1",
1834 "/org/freedesktop/systemd1",
1835 "org.freedesktop.systemd1.Manager",
1838 return bus_log_create_error(r
);
1840 r
= sd_bus_message_open_container(m
, 'a', "s");
1842 return bus_log_create_error(r
);
1844 for (i
= 1; i
< argc
; i
++) {
1845 _cleanup_free_
char *unit
= NULL
;
1847 r
= make_service_name(argv
[i
], &unit
);
1851 r
= image_exists(bus
, argv
[i
]);
1855 log_error("Machine image '%s' does not exist.", argv
[1]);
1859 r
= sd_bus_message_append(m
, "s", unit
);
1861 return bus_log_create_error(r
);
1864 r
= sd_bus_message_close_container(m
);
1866 return bus_log_create_error(r
);
1868 if (streq(argv
[0], "enable"))
1869 r
= sd_bus_message_append(m
, "bb", false, false);
1871 r
= sd_bus_message_append(m
, "b", false);
1873 return bus_log_create_error(r
);
1875 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1877 log_error("Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1881 if (streq(argv
[0], "enable")) {
1882 r
= sd_bus_message_read(reply
, "b", carries_install_info
);
1884 return bus_log_parse_error(r
);
1887 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1891 r
= sd_bus_call_method(
1893 "org.freedesktop.systemd1",
1894 "/org/freedesktop/systemd1",
1895 "org.freedesktop.systemd1.Manager",
1901 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1908 unit_file_changes_free(changes
, n_changes
);
1913 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1914 const char **our_path
= userdata
, *line
;
1921 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1923 bus_log_parse_error(r
);
1927 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1930 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1933 log_full(priority
, "%s", line
);
1937 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1938 const char **our_path
= userdata
, *path
, *result
;
1945 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1947 bus_log_parse_error(r
);
1951 if (!streq_ptr(*our_path
, path
))
1954 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1958 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1963 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1965 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1969 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1970 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1971 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1972 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1973 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1974 const char *path
= NULL
;
1981 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1983 r
= sd_event_default(&event
);
1985 return log_error_errno(r
, "Failed to get event loop: %m");
1987 r
= sd_bus_attach_event(bus
, event
, 0);
1989 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1991 r
= sd_bus_add_match(
1995 "sender='org.freedesktop.import1',"
1996 "interface='org.freedesktop.import1.Manager',"
1997 "member='TransferRemoved',"
1998 "path='/org/freedesktop/import1'",
1999 match_transfer_removed
, &path
);
2001 return log_error_errno(r
, "Failed to install match: %m");
2003 r
= sd_bus_add_match(
2007 "sender='org.freedesktop.import1',"
2008 "interface='org.freedesktop.import1.Transfer',"
2009 "member='LogMessage'",
2010 match_log_message
, &path
);
2012 return log_error_errno(r
, "Failed to install match: %m");
2014 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
2016 log_error("Failed to transfer image: %s", bus_error_message(&error
, -r
));
2020 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
2022 return bus_log_parse_error(r
);
2024 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
2027 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
2029 sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
2030 sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
2032 r
= sd_event_loop(event
);
2034 return log_error_errno(r
, "Failed to run event loop: %m");
2039 static int import_tar(int argc
, char *argv
[], void *userdata
) {
2040 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2041 _cleanup_free_
char *ll
= NULL
;
2042 _cleanup_close_
int fd
= -1;
2043 const char *local
= NULL
, *path
= NULL
;
2044 sd_bus
*bus
= userdata
;
2051 if (isempty(path
) || streq(path
, "-"))
2057 local
= basename(path
);
2058 if (isempty(local
) || streq(local
, "-"))
2062 log_error("Need either path or local name.");
2066 r
= tar_strip_suffixes(local
, &ll
);
2072 if (!machine_name_is_valid(local
)) {
2073 log_error("Local name %s is not a suitable machine name.", local
);
2078 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2080 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2083 r
= sd_bus_message_new_method_call(
2086 "org.freedesktop.import1",
2087 "/org/freedesktop/import1",
2088 "org.freedesktop.import1.Manager",
2091 return bus_log_create_error(r
);
2093 r
= sd_bus_message_append(
2096 fd
>= 0 ? fd
: STDIN_FILENO
,
2101 return bus_log_create_error(r
);
2103 return transfer_image_common(bus
, m
);
2106 static int import_raw(int argc
, char *argv
[], void *userdata
) {
2107 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2108 _cleanup_free_
char *ll
= NULL
;
2109 _cleanup_close_
int fd
= -1;
2110 const char *local
= NULL
, *path
= NULL
;
2111 sd_bus
*bus
= userdata
;
2118 if (isempty(path
) || streq(path
, "-"))
2124 local
= basename(path
);
2125 if (isempty(local
) || streq(local
, "-"))
2129 log_error("Need either path or local name.");
2133 r
= raw_strip_suffixes(local
, &ll
);
2139 if (!machine_name_is_valid(local
)) {
2140 log_error("Local name %s is not a suitable machine name.", local
);
2145 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2147 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2150 r
= sd_bus_message_new_method_call(
2153 "org.freedesktop.import1",
2154 "/org/freedesktop/import1",
2155 "org.freedesktop.import1.Manager",
2158 return bus_log_create_error(r
);
2160 r
= sd_bus_message_append(
2163 fd
>= 0 ? fd
: STDIN_FILENO
,
2168 return bus_log_create_error(r
);
2170 return transfer_image_common(bus
, m
);
2173 static void determine_compression_from_filename(const char *p
) {
2180 if (endswith(p
, ".xz"))
2182 else if (endswith(p
, ".gz"))
2183 arg_format
= "gzip";
2184 else if (endswith(p
, ".bz2"))
2185 arg_format
= "bzip2";
2188 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2189 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2190 _cleanup_close_
int fd
= -1;
2191 const char *local
= NULL
, *path
= NULL
;
2192 sd_bus
*bus
= userdata
;
2198 if (!machine_name_is_valid(local
)) {
2199 log_error("Machine name %s is not valid.", local
);
2205 if (isempty(path
) || streq(path
, "-"))
2209 determine_compression_from_filename(path
);
2211 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2213 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2216 r
= sd_bus_message_new_method_call(
2219 "org.freedesktop.import1",
2220 "/org/freedesktop/import1",
2221 "org.freedesktop.import1.Manager",
2224 return bus_log_create_error(r
);
2226 r
= sd_bus_message_append(
2230 fd
>= 0 ? fd
: STDOUT_FILENO
,
2233 return bus_log_create_error(r
);
2235 return transfer_image_common(bus
, m
);
2238 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2239 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2240 _cleanup_close_
int fd
= -1;
2241 const char *local
= NULL
, *path
= NULL
;
2242 sd_bus
*bus
= userdata
;
2248 if (!machine_name_is_valid(local
)) {
2249 log_error("Machine name %s is not valid.", local
);
2255 if (isempty(path
) || streq(path
, "-"))
2259 determine_compression_from_filename(path
);
2261 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2263 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2266 r
= sd_bus_message_new_method_call(
2269 "org.freedesktop.import1",
2270 "/org/freedesktop/import1",
2271 "org.freedesktop.import1.Manager",
2274 return bus_log_create_error(r
);
2276 r
= sd_bus_message_append(
2280 fd
>= 0 ? fd
: STDOUT_FILENO
,
2283 return bus_log_create_error(r
);
2285 return transfer_image_common(bus
, m
);
2288 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2289 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2290 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2291 const char *local
, *remote
;
2292 sd_bus
*bus
= userdata
;
2298 if (!http_url_is_valid(remote
)) {
2299 log_error("URL '%s' is not valid.", remote
);
2306 r
= import_url_last_component(remote
, &l
);
2308 return log_error_errno(r
, "Failed to get final component of URL: %m");
2313 if (isempty(local
) || streq(local
, "-"))
2317 r
= tar_strip_suffixes(local
, &ll
);
2323 if (!machine_name_is_valid(local
)) {
2324 log_error("Local name %s is not a suitable machine name.", local
);
2329 r
= sd_bus_message_new_method_call(
2332 "org.freedesktop.import1",
2333 "/org/freedesktop/import1",
2334 "org.freedesktop.import1.Manager",
2337 return bus_log_create_error(r
);
2339 r
= sd_bus_message_append(
2344 import_verify_to_string(arg_verify
),
2347 return bus_log_create_error(r
);
2349 return transfer_image_common(bus
, m
);
2352 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2353 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2354 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2355 const char *local
, *remote
;
2356 sd_bus
*bus
= userdata
;
2362 if (!http_url_is_valid(remote
)) {
2363 log_error("URL '%s' is not valid.", remote
);
2370 r
= import_url_last_component(remote
, &l
);
2372 return log_error_errno(r
, "Failed to get final component of URL: %m");
2377 if (isempty(local
) || streq(local
, "-"))
2381 r
= raw_strip_suffixes(local
, &ll
);
2387 if (!machine_name_is_valid(local
)) {
2388 log_error("Local name %s is not a suitable machine name.", local
);
2393 r
= sd_bus_message_new_method_call(
2396 "org.freedesktop.import1",
2397 "/org/freedesktop/import1",
2398 "org.freedesktop.import1.Manager",
2401 return bus_log_create_error(r
);
2403 r
= sd_bus_message_append(
2408 import_verify_to_string(arg_verify
),
2411 return bus_log_create_error(r
);
2413 return transfer_image_common(bus
, m
);
2416 typedef struct TransferInfo
{
2424 static int compare_transfer_info(const void *a
, const void *b
) {
2425 const TransferInfo
*x
= a
, *y
= b
;
2427 return strcmp(x
->local
, y
->local
);
2430 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2431 size_t max_type
= strlen("TYPE"), max_local
= strlen("LOCAL"), max_remote
= strlen("REMOTE");
2432 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2433 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2434 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2435 size_t n_transfers
= 0, n_allocated
= 0, j
;
2436 const char *type
, *remote
, *local
, *object
;
2437 sd_bus
*bus
= userdata
;
2438 uint32_t id
, max_id
= 0;
2442 pager_open(arg_no_pager
, false);
2444 r
= sd_bus_call_method(bus
,
2445 "org.freedesktop.import1",
2446 "/org/freedesktop/import1",
2447 "org.freedesktop.import1.Manager",
2453 log_error("Could not get transfers: %s", bus_error_message(&error
, -r
));
2457 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2459 return bus_log_parse_error(r
);
2461 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, &object
)) > 0) {
2464 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2467 transfers
[n_transfers
].id
= id
;
2468 transfers
[n_transfers
].type
= type
;
2469 transfers
[n_transfers
].remote
= remote
;
2470 transfers
[n_transfers
].local
= local
;
2471 transfers
[n_transfers
].progress
= progress
;
2491 return bus_log_parse_error(r
);
2493 r
= sd_bus_message_exit_container(reply
);
2495 return bus_log_parse_error(r
);
2497 qsort_safe(transfers
, n_transfers
, sizeof(TransferInfo
), compare_transfer_info
);
2499 if (arg_legend
&& n_transfers
> 0)
2500 printf("%-*s %-*s %-*s %-*s %-*s\n",
2501 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2503 (int) max_type
, "TYPE",
2504 (int) max_local
, "LOCAL",
2505 (int) max_remote
, "REMOTE");
2507 for (j
= 0; j
< n_transfers
; j
++)
2508 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2509 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2510 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2511 (int) max_type
, transfers
[j
].type
,
2512 (int) max_local
, transfers
[j
].local
,
2513 (int) max_remote
, transfers
[j
].remote
);
2516 if (n_transfers
> 0)
2517 printf("\n%zu transfers listed.\n", n_transfers
);
2519 printf("No transfers.\n");
2525 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2526 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2527 sd_bus
*bus
= userdata
;
2532 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2534 for (i
= 1; i
< argc
; i
++) {
2537 r
= safe_atou32(argv
[i
], &id
);
2539 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2541 r
= sd_bus_call_method(
2543 "org.freedesktop.import1",
2544 "/org/freedesktop/import1",
2545 "org.freedesktop.import1.Manager",
2551 log_error("Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2559 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2560 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2561 sd_bus
*bus
= userdata
;
2565 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2566 limit
= (uint64_t) -1;
2568 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2570 return log_error("Failed to parse size: %s", argv
[argc
-1]);
2574 /* With two arguments changes the quota limit of the
2575 * specified image */
2576 r
= sd_bus_call_method(
2578 "org.freedesktop.machine1",
2579 "/org/freedesktop/machine1",
2580 "org.freedesktop.machine1.Manager",
2584 "st", argv
[1], limit
);
2586 /* With one argument changes the pool quota limit */
2587 r
= sd_bus_call_method(
2589 "org.freedesktop.machine1",
2590 "/org/freedesktop/machine1",
2591 "org.freedesktop.machine1.Manager",
2598 log_error("Could not set limit: %s", bus_error_message(&error
, -r
));
2605 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2606 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2607 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2608 uint64_t usage
, total
= 0;
2609 char fb
[FORMAT_BYTES_MAX
];
2610 sd_bus
*bus
= userdata
;
2615 r
= sd_bus_message_new_method_call(
2618 "org.freedesktop.machine1",
2619 "/org/freedesktop/machine1",
2620 "org.freedesktop.machine1.Manager",
2623 return bus_log_create_error(r
);
2625 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2627 return bus_log_create_error(r
);
2629 /* This is a slow operation, hence permit a longer time for completion. */
2630 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2632 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2634 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2636 return bus_log_parse_error(r
);
2638 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2639 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2640 name
, format_bytes(fb
, sizeof(fb
), usage
));
2646 r
= sd_bus_message_exit_container(reply
);
2648 return bus_log_parse_error(r
);
2650 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2651 c
, format_bytes(fb
, sizeof(fb
), total
));
2656 static int help(int argc
, char *argv
[], void *userdata
) {
2657 pager_open(arg_no_pager
, false);
2659 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2660 "Send control commands to or query the virtual machine and container\n"
2661 "registration manager.\n\n"
2662 " -h --help Show this help\n"
2663 " --version Show package version\n"
2664 " --no-pager Do not pipe output into a pager\n"
2665 " --no-legend Do not show the headers and footers\n"
2666 " --no-ask-password Do not ask for system passwords\n"
2667 " -H --host=[USER@]HOST Operate on remote host\n"
2668 " -M --machine=CONTAINER Operate on local container\n"
2669 " -p --property=NAME Show only properties by this name\n"
2670 " -q --quiet Suppress output\n"
2671 " -a --all Show all properties, including empty ones\n"
2672 " --value When showing properties, only print the value\n"
2673 " -l --full Do not ellipsize output\n"
2674 " --kill-who=WHO Who to send signal to\n"
2675 " -s --signal=SIGNAL Which signal to send\n"
2676 " --uid=USER Specify user ID to invoke shell as\n"
2677 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2678 " --read-only Create read-only bind mount\n"
2679 " --mkdir Create directory before bind mounting, if missing\n"
2680 " -n --lines=INTEGER Number of journal entries to show\n"
2681 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2682 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2683 " short-iso, short-iso-precise, short-full,\n"
2684 " short-monotonic, short-unix, verbose, export,\n"
2685 " json, json-pretty, json-sse, cat)\n"
2686 " --verify=MODE Verification mode for downloaded images (no,\n"
2687 " checksum, signature)\n"
2688 " --force Download image even if already exists\n\n"
2689 "Machine Commands:\n"
2690 " list List running VMs and containers\n"
2691 " status NAME... Show VM/container details\n"
2692 " show [NAME...] Show properties of one or more VMs/containers\n"
2693 " start NAME... Start container as a service\n"
2694 " login [NAME] Get a login prompt in a container or on the\n"
2696 " shell [[USER@]NAME [COMMAND...]]\n"
2697 " Invoke a shell (or other command) in a container\n"
2698 " or on the local host\n"
2699 " enable NAME... Enable automatic container start at boot\n"
2700 " disable NAME... Disable automatic container start at boot\n"
2701 " poweroff NAME... Power off one or more containers\n"
2702 " reboot NAME... Reboot one or more containers\n"
2703 " terminate NAME... Terminate one or more VMs/containers\n"
2704 " kill NAME... Send signal to processes of a VM/container\n"
2705 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2706 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2707 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2709 " list-images Show available container and VM images\n"
2710 " image-status [NAME...] Show image details\n"
2711 " show-image [NAME...] Show properties of image\n"
2712 " clone NAME NAME Clone an image\n"
2713 " rename NAME NAME Rename an image\n"
2714 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2715 " remove NAME... Remove an image\n"
2716 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2717 " clean Remove hidden (or all) images\n\n"
2718 "Image Transfer Commands:\n"
2719 " pull-tar URL [NAME] Download a TAR container image\n"
2720 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2721 " import-tar FILE [NAME] Import a local TAR container image\n"
2722 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2723 " export-tar NAME [FILE] Export a TAR container image locally\n"
2724 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2725 " list-transfers Show list of downloads in progress\n"
2726 " cancel-transfer Cancel a download\n"
2727 , program_invocation_short_name
);
2732 static int parse_argv(int argc
, char *argv
[]) {
2735 ARG_VERSION
= 0x100,
2742 ARG_NO_ASK_PASSWORD
,
2750 static const struct option options
[] = {
2751 { "help", no_argument
, NULL
, 'h' },
2752 { "version", no_argument
, NULL
, ARG_VERSION
},
2753 { "property", required_argument
, NULL
, 'p' },
2754 { "all", no_argument
, NULL
, 'a' },
2755 { "value", no_argument
, NULL
, ARG_VALUE
},
2756 { "full", no_argument
, NULL
, 'l' },
2757 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2758 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2759 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2760 { "signal", required_argument
, NULL
, 's' },
2761 { "host", required_argument
, NULL
, 'H' },
2762 { "machine", required_argument
, NULL
, 'M' },
2763 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2764 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2765 { "quiet", no_argument
, NULL
, 'q' },
2766 { "lines", required_argument
, NULL
, 'n' },
2767 { "output", required_argument
, NULL
, 'o' },
2768 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2769 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2770 { "force", no_argument
, NULL
, ARG_FORCE
},
2771 { "format", required_argument
, NULL
, ARG_FORMAT
},
2772 { "uid", required_argument
, NULL
, ARG_UID
},
2773 { "setenv", required_argument
, NULL
, 'E' },
2774 { "max-addresses", required_argument
, NULL
, ARG_NUMBER_IPS
},
2778 bool reorder
= false;
2779 int c
, r
, shell
= -1;
2785 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2787 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2793 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2794 * non-option argument was discovered. */
2798 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2799 * for switches after the main verb. However, for "shell" we really don't want that, since we
2800 * want that switches specified after the machine name are passed to the program to execute,
2801 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2802 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2803 * non-option parameter. If it's the verb "shell" we remember its position and continue
2804 * processing options. In this case, as soon as we hit the next non-option argument we found
2805 * the machine name, and stop further processing. If the first non-option argument is any other
2806 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2810 /* If we already found the "shell" verb on the command line, and now found the next
2811 * non-option argument, then this is the machine name and we should stop processing
2812 * further arguments. */
2813 optind
--; /* don't process this argument, go one step back */
2816 if (streq(optarg
, "shell"))
2817 /* Remember the position of the "shell" verb, and continue processing normally. */
2822 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2823 * processing normally. */
2826 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2827 * at least once with a reset option index. Hence, let's reset the option index here,
2828 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2829 * likely already processed it), and the bump the option index so that we read the
2830 * intended argument again. */
2831 saved_optind
= optind
;
2833 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2834 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2840 return help(0, NULL
, NULL
);
2846 r
= strv_extend(&arg_property
, optarg
);
2850 /* If the user asked for a particular
2851 * property, show it to him, even if it is
2869 if (safe_atou(optarg
, &arg_lines
) < 0) {
2870 log_error("Failed to parse lines '%s'", optarg
);
2876 arg_output
= output_mode_from_string(optarg
);
2877 if (arg_output
< 0) {
2878 log_error("Unknown output '%s'.", optarg
);
2884 arg_no_pager
= true;
2892 arg_kill_who
= optarg
;
2896 arg_signal
= signal_from_string_try_harder(optarg
);
2897 if (arg_signal
< 0) {
2898 log_error("Failed to parse signal string %s.", optarg
);
2903 case ARG_NO_ASK_PASSWORD
:
2904 arg_ask_password
= false;
2908 arg_transport
= BUS_TRANSPORT_REMOTE
;
2913 arg_transport
= BUS_TRANSPORT_MACHINE
;
2918 arg_read_only
= true;
2930 arg_verify
= import_verify_from_string(optarg
);
2931 if (arg_verify
< 0) {
2932 log_error("Failed to parse --verify= setting: %s", optarg
);
2942 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2")) {
2943 log_error("Unknown format: %s", optarg
);
2947 arg_format
= optarg
;
2955 if (!env_assignment_is_valid(optarg
)) {
2956 log_error("Environment assignment invalid: %s", optarg
);
2960 r
= strv_extend(&arg_setenv
, optarg
);
2965 case ARG_NUMBER_IPS
:
2966 if (streq(optarg
, "all"))
2967 arg_addrs
= ALL_IP_ADDRESSES
;
2968 else if (safe_atoi(optarg
, &arg_addrs
) < 0) {
2969 log_error("Invalid number of IPs");
2971 } else if (arg_addrs
< 0) {
2972 log_error("Number of IPs cannot be negative");
2981 assert_not_reached("Unhandled option");
2990 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2991 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2993 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2996 for (i
= shell
; i
< optind
; i
++)
2997 argv
[i
] = argv
[i
+1];
3004 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
3006 static const Verb verbs
[] = {
3007 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3008 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
3009 { "list-images", VERB_ANY
, 1, 0, list_images
},
3010 { "status", 2, VERB_ANY
, 0, show_machine
},
3011 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
3012 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
3013 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
3014 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
3015 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
3016 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
3017 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
3018 { "kill", 2, VERB_ANY
, 0, kill_machine
},
3019 { "login", VERB_ANY
, 2, 0, login_machine
},
3020 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
3021 { "bind", 3, 4, 0, bind_mount
},
3022 { "copy-to", 3, 4, 0, copy_files
},
3023 { "copy-from", 3, 4, 0, copy_files
},
3024 { "remove", 2, VERB_ANY
, 0, remove_image
},
3025 { "rename", 3, 3, 0, rename_image
},
3026 { "clone", 3, 3, 0, clone_image
},
3027 { "read-only", 2, 3, 0, read_only_image
},
3028 { "start", 2, VERB_ANY
, 0, start_machine
},
3029 { "enable", 2, VERB_ANY
, 0, enable_machine
},
3030 { "disable", 2, VERB_ANY
, 0, enable_machine
},
3031 { "import-tar", 2, 3, 0, import_tar
},
3032 { "import-raw", 2, 3, 0, import_raw
},
3033 { "export-tar", 2, 3, 0, export_tar
},
3034 { "export-raw", 2, 3, 0, export_raw
},
3035 { "pull-tar", 2, 3, 0, pull_tar
},
3036 { "pull-raw", 2, 3, 0, pull_raw
},
3037 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
3038 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
3039 { "set-limit", 2, 3, 0, set_limit
},
3040 { "clean", VERB_ANY
, 1, 0, clean_images
},
3044 return dispatch_verb(argc
, argv
, verbs
, bus
);
3047 int main(int argc
, char*argv
[]) {
3048 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
3051 setlocale(LC_ALL
, "");
3052 log_parse_environment();
3056 r
= parse_argv(argc
, argv
);
3060 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
3062 log_error_errno(r
, "Failed to create bus connection: %m");
3066 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
3068 r
= machinectl_main(argc
, argv
, bus
);
3072 polkit_agent_close();
3074 strv_free(arg_property
);
3075 strv_free(arg_setenv
);
3077 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;