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"
55 #include "signal-util.h"
56 #include "spawn-polkit-agent.h"
58 #include "terminal-util.h"
59 #include "unit-name.h"
63 #include "stdio-util.h"
65 #define ALL_IP_ADDRESSES -1
67 static char **arg_property
= NULL
;
68 static bool arg_all
= false;
69 static bool arg_value
= false;
70 static bool arg_full
= false;
71 static bool arg_no_pager
= false;
72 static bool arg_legend
= true;
73 static const char *arg_kill_who
= NULL
;
74 static int arg_signal
= SIGTERM
;
75 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
76 static char *arg_host
= NULL
;
77 static bool arg_read_only
= false;
78 static bool arg_mkdir
= false;
79 static bool arg_quiet
= false;
80 static bool arg_ask_password
= true;
81 static unsigned arg_lines
= 10;
82 static OutputMode arg_output
= OUTPUT_SHORT
;
83 static bool arg_force
= false;
84 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
85 static const char* arg_format
= NULL
;
86 static const char *arg_uid
= NULL
;
87 static char **arg_setenv
= NULL
;
88 static int arg_addrs
= 1;
90 static int print_addresses(sd_bus
*bus
, const char *name
, int, const char *pr1
, const char *pr2
, int n_addr
);
92 static void polkit_agent_open_if_enabled(void) {
94 /* Open the polkit agent as a child process if necessary */
96 if (!arg_ask_password
)
99 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
105 static OutputFlags
get_output_flags(void) {
107 arg_all
* OUTPUT_SHOW_ALL
|
108 arg_full
* OUTPUT_FULL_WIDTH
|
109 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
110 colors_enabled() * OUTPUT_COLOR
|
111 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
114 typedef struct MachineInfo
{
122 static int compare_machine_info(const void *a
, const void *b
) {
123 const MachineInfo
*x
= a
, *y
= b
;
125 return strcmp(x
->name
, y
->name
);
128 static void clean_machine_info(MachineInfo
*machines
, size_t n_machines
) {
131 if (!machines
|| n_machines
== 0)
134 for (i
= 0; i
< n_machines
; i
++) {
135 free(machines
[i
].os
);
136 free(machines
[i
].version_id
);
141 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
142 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
143 const char *k
, *v
, *iter
, **query_res
= NULL
;
144 size_t count
= 0, awaited_args
= 0;
152 NULSTR_FOREACH(iter
, query
)
154 query_res
= newa0(const char *, awaited_args
);
156 r
= sd_bus_call_method(
158 "org.freedesktop.machine1",
159 "/org/freedesktop/machine1",
160 "org.freedesktop.machine1.Manager",
162 NULL
, &reply
, "s", name
);
166 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
168 return bus_log_parse_error(r
);
170 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
172 NULSTR_FOREACH(iter
, query
) {
173 if (streq(k
, iter
)) {
174 query_res
[count
] = v
;
181 return bus_log_parse_error(r
);
183 r
= sd_bus_message_exit_container(reply
);
185 return bus_log_parse_error(r
);
188 for (count
= 0; count
< awaited_args
; count
++) {
191 out
= va_arg(ap
, char **);
193 if (query_res
[count
]) {
194 val
= strdup(query_res
[count
]);
207 static int list_machines(int argc
, char *argv
[], void *userdata
) {
209 size_t max_name
= strlen("MACHINE"), max_class
= strlen("CLASS"),
210 max_service
= strlen("SERVICE"), max_os
= strlen("OS"), max_version_id
= strlen("VERSION");
211 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
212 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
213 _cleanup_free_
char *prefix
= NULL
;
214 MachineInfo
*machines
= NULL
;
215 const char *name
, *class, *service
, *object
;
216 size_t n_machines
= 0, n_allocated
= 0, j
;
217 sd_bus
*bus
= userdata
;
222 pager_open(arg_no_pager
, false);
224 r
= sd_bus_call_method(bus
,
225 "org.freedesktop.machine1",
226 "/org/freedesktop/machine1",
227 "org.freedesktop.machine1.Manager",
233 log_error("Could not get machines: %s", bus_error_message(&error
, -r
));
237 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
239 return bus_log_parse_error(r
);
240 while ((r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, &object
)) > 0) {
243 if (name
[0] == '.' && !arg_all
)
246 if (!GREEDY_REALLOC0(machines
, n_allocated
, n_machines
+ 1)) {
251 machines
[n_machines
].name
= name
;
252 machines
[n_machines
].class = class;
253 machines
[n_machines
].service
= service
;
255 (void) call_get_os_release(
257 "GetMachineOSRelease",
261 &machines
[n_machines
].os
,
262 &machines
[n_machines
].version_id
);
276 l
= machines
[n_machines
].os
? strlen(machines
[n_machines
].os
) : 1;
280 l
= machines
[n_machines
].version_id
? strlen(machines
[n_machines
].version_id
) : 1;
281 if (l
> max_version_id
)
287 r
= bus_log_parse_error(r
);
291 r
= sd_bus_message_exit_container(reply
);
293 r
= bus_log_parse_error(r
);
297 qsort_safe(machines
, n_machines
, sizeof(MachineInfo
), compare_machine_info
);
299 /* Allocate for prefix max characters for all fields + spaces between them + strlen(",\n") */
300 r
= asprintf(&prefix
, "%-*s",
305 max_version_id
+ 5 + strlen(",\n")),
312 if (arg_legend
&& n_machines
> 0)
313 printf("%-*s %-*s %-*s %-*s %-*s %s\n",
314 (int) max_name
, "MACHINE",
315 (int) max_class
, "CLASS",
316 (int) max_service
, "SERVICE",
318 (int) max_version_id
, "VERSION",
321 for (j
= 0; j
< n_machines
; j
++) {
322 printf("%-*s %-*s %-*s %-*s %-*s ",
323 (int) max_name
, machines
[j
].name
,
324 (int) max_class
, machines
[j
].class,
325 (int) max_service
, strdash_if_empty(machines
[j
].service
),
326 (int) max_os
, strdash_if_empty(machines
[j
].os
),
327 (int) max_version_id
, strdash_if_empty(machines
[j
].version_id
));
329 r
= print_addresses(bus
, machines
[j
].name
, 0, "", prefix
, arg_addrs
);
330 if (r
== -EOPNOTSUPP
)
336 printf("\n%zu machines listed.\n", n_machines
);
338 printf("No machines.\n");
342 clean_machine_info(machines
, n_machines
);
346 typedef struct ImageInfo
{
355 static int compare_image_info(const void *a
, const void *b
) {
356 const ImageInfo
*x
= a
, *y
= b
;
358 return strcmp(x
->name
, y
->name
);
361 static int list_images(int argc
, char *argv
[], void *userdata
) {
363 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
364 size_t max_name
= strlen("NAME"), max_type
= strlen("TYPE"), max_size
= strlen("USAGE"), max_crtime
= strlen("CREATED"), max_mtime
= strlen("MODIFIED");
365 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
366 _cleanup_free_ ImageInfo
*images
= NULL
;
367 size_t n_images
= 0, n_allocated
= 0, j
;
368 const char *name
, *type
, *object
;
369 sd_bus
*bus
= userdata
;
370 uint64_t crtime
, mtime
, size
;
375 pager_open(arg_no_pager
, false);
377 r
= sd_bus_call_method(bus
,
378 "org.freedesktop.machine1",
379 "/org/freedesktop/machine1",
380 "org.freedesktop.machine1.Manager",
386 log_error("Could not get images: %s", bus_error_message(&error
, -r
));
390 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
392 return bus_log_parse_error(r
);
394 while ((r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &read_only
, &crtime
, &mtime
, &size
, &object
)) > 0) {
395 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, FORMAT_BYTES_MAX
)];
398 if (name
[0] == '.' && !arg_all
)
401 if (!GREEDY_REALLOC(images
, n_allocated
, n_images
+ 1))
404 images
[n_images
].name
= name
;
405 images
[n_images
].type
= type
;
406 images
[n_images
].read_only
= read_only
;
407 images
[n_images
].crtime
= crtime
;
408 images
[n_images
].mtime
= mtime
;
409 images
[n_images
].size
= size
;
420 l
= strlen(strna(format_timestamp(buf
, sizeof(buf
), crtime
)));
426 l
= strlen(strna(format_timestamp(buf
, sizeof(buf
), mtime
)));
431 if (size
!= (uint64_t) -1) {
432 l
= strlen(strna(format_bytes(buf
, sizeof(buf
), size
)));
440 return bus_log_parse_error(r
);
442 r
= sd_bus_message_exit_container(reply
);
444 return bus_log_parse_error(r
);
446 qsort_safe(images
, n_images
, sizeof(ImageInfo
), compare_image_info
);
448 if (arg_legend
&& n_images
> 0)
449 printf("%-*s %-*s %-3s %-*s %-*s %-*s\n",
450 (int) max_name
, "NAME",
451 (int) max_type
, "TYPE",
453 (int) max_size
, "USAGE",
454 (int) max_crtime
, "CREATED",
455 (int) max_mtime
, "MODIFIED");
457 for (j
= 0; j
< n_images
; j
++) {
458 char crtime_buf
[FORMAT_TIMESTAMP_MAX
], mtime_buf
[FORMAT_TIMESTAMP_MAX
], size_buf
[FORMAT_BYTES_MAX
];
460 printf("%-*s %-*s %s%-3s%s %-*s %-*s %-*s\n",
461 (int) max_name
, images
[j
].name
,
462 (int) max_type
, images
[j
].type
,
463 images
[j
].read_only
? ansi_highlight_red() : "", yes_no(images
[j
].read_only
), images
[j
].read_only
? ansi_normal() : "",
464 (int) max_size
, strna(format_bytes(size_buf
, sizeof(size_buf
), images
[j
].size
)),
465 (int) max_crtime
, strna(format_timestamp(crtime_buf
, sizeof(crtime_buf
), images
[j
].crtime
)),
466 (int) max_mtime
, strna(format_timestamp(mtime_buf
, sizeof(mtime_buf
), images
[j
].mtime
)));
471 printf("\n%zu images listed.\n", n_images
);
473 printf("No images.\n");
479 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
480 _cleanup_free_
char *cgroup
= NULL
;
481 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
488 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
501 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
504 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
507 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
509 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
512 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
514 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
519 static int print_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
) {
520 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
521 _cleanup_free_
char *addresses
= NULL
;
522 bool truncate
= false;
530 r
= sd_bus_call_method(bus
,
531 "org.freedesktop.machine1",
532 "/org/freedesktop/machine1",
533 "org.freedesktop.machine1.Manager",
534 "GetMachineAddresses",
541 addresses
= strdup(prefix
);
546 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
548 return bus_log_parse_error(r
);
550 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
554 char buf_ifi
[DECIMAL_STR_MAX(int) + 2], buffer
[MAX(INET6_ADDRSTRLEN
, INET_ADDRSTRLEN
)];
556 r
= sd_bus_message_read(reply
, "i", &family
);
558 return bus_log_parse_error(r
);
560 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
562 return bus_log_parse_error(r
);
565 if (family
== AF_INET6
&& ifi
> 0)
566 xsprintf(buf_ifi
, "%%%i", ifi
);
570 if(!strextend(&addresses
, prefix
, inet_ntop(family
, a
, buffer
, sizeof(buffer
)), buf_ifi
, NULL
))
575 r
= sd_bus_message_exit_container(reply
);
577 return bus_log_parse_error(r
);
579 if (prefix
!= prefix2
)
586 return bus_log_parse_error(r
);
588 r
= sd_bus_message_exit_container(reply
);
590 return bus_log_parse_error(r
);
592 fprintf(stdout
, "%s%s\n", addresses
, truncate
? "..." : "");
596 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
597 _cleanup_free_
char *pretty
= NULL
;
604 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
609 printf("%s%s\n", prefix
, pretty
);
614 typedef struct MachineStatusInfo
{
620 char *root_directory
;
622 struct dual_timestamp timestamp
;
627 static void machine_status_info_clear(MachineStatusInfo
*info
) {
633 free(info
->root_directory
);
639 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
640 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
641 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
647 fputs(strna(i
->name
), stdout
);
649 if (!sd_id128_is_null(i
->id
))
650 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
654 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->timestamp
.realtime
);
655 s2
= format_timestamp(since2
, sizeof(since2
), i
->timestamp
.realtime
);
658 printf("\t Since: %s; %s\n", s2
, s1
);
660 printf("\t Since: %s\n", s2
);
663 _cleanup_free_
char *t
= NULL
;
665 printf("\t Leader: %u", (unsigned) i
->leader
);
667 get_process_comm(i
->leader
, &t
);
675 printf("\t Service: %s", i
->service
);
678 printf("; class %s", i
->class);
682 printf("\t Class: %s\n", i
->class);
684 if (i
->root_directory
)
685 printf("\t Root: %s\n", i
->root_directory
);
687 if (i
->n_netif
> 0) {
690 fputs("\t Iface:", stdout
);
692 for (c
= 0; c
< i
->n_netif
; c
++) {
693 char name
[IF_NAMESIZE
+1] = "";
695 if (if_indextoname(i
->netif
[c
], name
)) {
704 printf(" %i", i
->netif
[c
]);
710 print_addresses(bus
, i
->name
, ifi
,
715 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
718 printf("\t Unit: %s\n", i
->unit
);
719 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
721 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
723 show_journal_by_unit(
728 i
->timestamp
.monotonic
,
731 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
732 SD_JOURNAL_LOCAL_ONLY
,
738 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
739 MachineStatusInfo
*i
= userdata
;
744 assert_cc(sizeof(int32_t) == sizeof(int));
745 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
751 i
->n_netif
= l
/ sizeof(int32_t);
752 i
->netif
= memdup(v
, l
);
759 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
761 static const struct bus_properties_map map
[] = {
762 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
763 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
764 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
765 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
766 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
767 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
768 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
769 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
770 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
771 { "NetworkInterfaces", "ai", map_netif
, 0 },
775 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
776 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
784 r
= bus_map_all_properties(bus
,
785 "org.freedesktop.machine1",
791 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
797 print_machine_status_info(bus
, &info
);
802 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
814 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, arg_property
, arg_value
, arg_all
);
816 log_error_errno(r
, "Could not get properties: %m");
821 static int show_machine(int argc
, char *argv
[], void *userdata
) {
823 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
824 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
825 bool properties
, new_line
= false;
826 sd_bus
*bus
= userdata
;
831 properties
= !strstr(argv
[0], "status");
833 pager_open(arg_no_pager
, false);
835 if (properties
&& argc
<= 1) {
837 /* If no argument is specified, inspect the manager
839 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
844 for (i
= 1; i
< argc
; i
++) {
845 const char *path
= NULL
;
847 r
= sd_bus_call_method(bus
,
848 "org.freedesktop.machine1",
849 "/org/freedesktop/machine1",
850 "org.freedesktop.machine1.Manager",
856 log_error("Could not get path to machine: %s", bus_error_message(&error
, -r
));
860 r
= sd_bus_message_read(reply
, "o", &path
);
862 return bus_log_parse_error(r
);
865 r
= show_machine_properties(bus
, path
, &new_line
);
867 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
873 typedef struct ImageStatusInfo
{
882 uint64_t usage_exclusive
;
883 uint64_t limit_exclusive
;
886 static void image_status_info_clear(ImageStatusInfo
*info
) {
895 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
896 char ts_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
897 char ts_absolute
[FORMAT_TIMESTAMP_MAX
], *s2
;
898 char bs
[FORMAT_BYTES_MAX
], *s3
;
899 char bs_exclusive
[FORMAT_BYTES_MAX
], *s4
;
905 fputs(i
->name
, stdout
);
910 printf("\t Type: %s\n", i
->type
);
913 printf("\t Path: %s\n", i
->path
);
915 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
917 printf("\t RO: %s%s%s\n",
918 i
->read_only
? ansi_highlight_red() : "",
919 i
->read_only
? "read-only" : "writable",
920 i
->read_only
? ansi_normal() : "");
922 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->crtime
);
923 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->crtime
);
925 printf("\t Created: %s; %s\n", s2
, s1
);
927 printf("\t Created: %s\n", s2
);
929 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->mtime
);
930 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->mtime
);
932 printf("\tModified: %s; %s\n", s2
, s1
);
934 printf("\tModified: %s\n", s2
);
936 s3
= format_bytes(bs
, sizeof(bs
), i
->usage
);
937 s4
= i
->usage_exclusive
!= i
->usage
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->usage_exclusive
) : NULL
;
939 printf("\t Usage: %s (exclusive: %s)\n", s3
, s4
);
941 printf("\t Usage: %s\n", s3
);
943 s3
= format_bytes(bs
, sizeof(bs
), i
->limit
);
944 s4
= i
->limit_exclusive
!= i
->limit
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->limit_exclusive
) : NULL
;
946 printf("\t Limit: %s (exclusive: %s)\n", s3
, s4
);
948 printf("\t Limit: %s\n", s3
);
951 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
953 static const struct bus_properties_map map
[] = {
954 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
955 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
956 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
957 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
958 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
959 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
960 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
961 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
962 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
963 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
967 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
968 _cleanup_(image_status_info_clear
) ImageStatusInfo info
= {};
975 r
= bus_map_all_properties(bus
,
976 "org.freedesktop.machine1",
982 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
988 print_image_status_info(bus
, &info
);
993 typedef struct PoolStatusInfo
{
999 static void pool_status_info_clear(PoolStatusInfo
*info
) {
1008 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
1009 char bs
[FORMAT_BYTES_MAX
], *s
;
1012 printf("\t Path: %s\n", i
->path
);
1014 s
= format_bytes(bs
, sizeof(bs
), i
->usage
);
1016 printf("\t Usage: %s\n", s
);
1018 s
= format_bytes(bs
, sizeof(bs
), i
->limit
);
1020 printf("\t Limit: %s\n", s
);
1023 static int show_pool_info(sd_bus
*bus
) {
1025 static const struct bus_properties_map map
[] = {
1026 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
1027 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
1028 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
1032 _cleanup_(pool_status_info_clear
) PoolStatusInfo info
= {
1033 .usage
= (uint64_t) -1,
1034 .limit
= (uint64_t) -1,
1037 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1042 r
= bus_map_all_properties(bus
,
1043 "org.freedesktop.machine1",
1044 "/org/freedesktop/machine1",
1049 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1051 print_pool_status_info(bus
, &info
);
1057 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
1069 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, arg_property
, arg_value
, arg_all
);
1071 log_error_errno(r
, "Could not get properties: %m");
1076 static int show_image(int argc
, char *argv
[], void *userdata
) {
1078 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1079 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1080 bool properties
, new_line
= false;
1081 sd_bus
*bus
= userdata
;
1086 properties
= !strstr(argv
[0], "status");
1088 pager_open(arg_no_pager
, false);
1092 /* If no argument is specified, inspect the manager
1096 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1098 r
= show_pool_info(bus
);
1103 for (i
= 1; i
< argc
; i
++) {
1104 const char *path
= NULL
;
1106 r
= sd_bus_call_method(
1108 "org.freedesktop.machine1",
1109 "/org/freedesktop/machine1",
1110 "org.freedesktop.machine1.Manager",
1116 log_error("Could not get path to image: %s", bus_error_message(&error
, -r
));
1120 r
= sd_bus_message_read(reply
, "o", &path
);
1122 return bus_log_parse_error(r
);
1125 r
= show_image_properties(bus
, path
, &new_line
);
1127 r
= show_image_info(bus
, path
, &new_line
);
1133 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1134 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1135 sd_bus
*bus
= userdata
;
1140 polkit_agent_open_if_enabled();
1143 arg_kill_who
= "all";
1145 for (i
= 1; i
< argc
; i
++) {
1146 r
= sd_bus_call_method(
1148 "org.freedesktop.machine1",
1149 "/org/freedesktop/machine1",
1150 "org.freedesktop.machine1.Manager",
1154 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1156 log_error("Could not kill machine: %s", bus_error_message(&error
, -r
));
1164 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1165 arg_kill_who
= "leader";
1166 arg_signal
= SIGINT
; /* sysvinit + systemd */
1168 return kill_machine(argc
, argv
, userdata
);
1171 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1172 arg_kill_who
= "leader";
1173 arg_signal
= SIGRTMIN
+4; /* only systemd */
1175 return kill_machine(argc
, argv
, userdata
);
1178 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1179 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1180 sd_bus
*bus
= userdata
;
1185 polkit_agent_open_if_enabled();
1187 for (i
= 1; i
< argc
; i
++) {
1188 r
= sd_bus_call_method(
1190 "org.freedesktop.machine1",
1191 "/org/freedesktop/machine1",
1192 "org.freedesktop.machine1.Manager",
1198 log_error("Could not terminate machine: %s", bus_error_message(&error
, -r
));
1206 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1207 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1208 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1209 _cleanup_free_
char *abs_host_path
= NULL
;
1210 char *dest
, *host_path
, *container_path
;
1211 sd_bus
*bus
= userdata
;
1217 polkit_agent_open_if_enabled();
1219 copy_from
= streq(argv
[0], "copy-from");
1220 dest
= argv
[3] ?: argv
[2];
1221 host_path
= copy_from
? dest
: argv
[2];
1222 container_path
= copy_from
? argv
[2] : dest
;
1224 if (!path_is_absolute(host_path
)) {
1225 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1227 return log_error_errno(r
, "Failed to make path absolute: %m");
1229 host_path
= abs_host_path
;
1232 r
= sd_bus_message_new_method_call(
1235 "org.freedesktop.machine1",
1236 "/org/freedesktop/machine1",
1237 "org.freedesktop.machine1.Manager",
1238 copy_from
? "CopyFromMachine" : "CopyToMachine");
1240 return bus_log_create_error(r
);
1242 r
= sd_bus_message_append(
1246 copy_from
? container_path
: host_path
,
1247 copy_from
? host_path
: container_path
);
1249 return bus_log_create_error(r
);
1251 /* This is a slow operation, hence turn off any method call timeouts */
1252 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1254 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1259 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1260 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1261 sd_bus
*bus
= userdata
;
1266 polkit_agent_open_if_enabled();
1268 r
= sd_bus_call_method(
1270 "org.freedesktop.machine1",
1271 "/org/freedesktop/machine1",
1272 "org.freedesktop.machine1.Manager",
1283 log_error("Failed to bind mount: %s", bus_error_message(&error
, -r
));
1290 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1291 PTYForward
** forward
= (PTYForward
**) userdata
;
1298 /* If the forwarder is already initialized, tell it to
1299 * exit on the next vhangup(), so that we still flush
1300 * out what might be queued and exit then. */
1302 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1306 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1309 /* On error, or when the forwarder is not initialized yet, quit immediately */
1310 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1314 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1320 assert(master
>= 0);
1323 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1326 if (streq(name
, ".host"))
1327 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1329 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1332 sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1333 sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1335 r
= pty_forward_new(event
, master
, flags
, forward
);
1337 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1339 r
= sd_event_loop(event
);
1341 return log_error_errno(r
, "Failed to run event loop: %m");
1343 pty_forward_get_last_char(*forward
, &last_char
);
1346 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1347 pty_forward_get_ignore_vhangup(*forward
) == 0;
1349 *forward
= pty_forward_free(*forward
);
1351 if (last_char
!= '\n')
1352 fputc('\n', stdout
);
1356 log_info("Machine %s terminated.", name
);
1357 else if (streq(name
, ".host"))
1358 log_info("Connection to the local host terminated.");
1360 log_info("Connection to machine %s terminated.", name
);
1363 sd_event_get_exit_code(event
, &ret
);
1367 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1369 * Whatever is specified in the spec takes priority over global arguments.
1372 const char *_machine
= NULL
;
1377 at
= strchr(spec
, '@');
1380 /* Do the same as ssh and refuse "@host". */
1384 _uid
= strndup(spec
, at
- spec
);
1391 if (arg_uid
&& !_uid
) {
1392 _uid
= strdup(arg_uid
);
1398 *machine
= isempty(_machine
) ? ".host" : _machine
;
1402 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1403 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1404 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1405 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1406 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1407 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1409 sd_bus
*bus
= userdata
;
1410 const char *pty
, *match
, *machine
;
1414 if (!strv_isempty(arg_setenv
) || arg_uid
) {
1415 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1419 if (arg_transport
!= BUS_TRANSPORT_LOCAL
&&
1420 arg_transport
!= BUS_TRANSPORT_MACHINE
) {
1421 log_error("Login only supported on local machines.");
1425 polkit_agent_open_if_enabled();
1427 r
= sd_event_default(&event
);
1429 return log_error_errno(r
, "Failed to get event loop: %m");
1431 r
= sd_bus_attach_event(bus
, event
, 0);
1433 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1435 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1437 match
= strjoina("type='signal',"
1438 "sender='org.freedesktop.machine1',"
1439 "path='/org/freedesktop/machine1',",
1440 "interface='org.freedesktop.machine1.Manager',"
1441 "member='MachineRemoved',"
1442 "arg0='", machine
, "'");
1444 r
= sd_bus_add_match(bus
, &slot
, match
, on_machine_removed
, &forward
);
1446 return log_error_errno(r
, "Failed to add machine removal match: %m");
1448 r
= sd_bus_call_method(
1450 "org.freedesktop.machine1",
1451 "/org/freedesktop/machine1",
1452 "org.freedesktop.machine1.Manager",
1458 log_error("Failed to get login PTY: %s", bus_error_message(&error
, -r
));
1462 r
= sd_bus_message_read(reply
, "hs", &master
, &pty
);
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 *pty
, *match
, *machine
, *path
;
1478 _cleanup_free_
char *uid
= NULL
;
1482 if (arg_transport
!= BUS_TRANSPORT_LOCAL
&&
1483 arg_transport
!= BUS_TRANSPORT_MACHINE
) {
1484 log_error("Shell only supported on local machines.");
1488 /* Pass $TERM to shell session, if not explicitly specified. */
1489 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1492 t
= strv_find_prefix(environ
, "TERM=");
1494 if (strv_extend(&arg_setenv
, t
) < 0)
1499 polkit_agent_open_if_enabled();
1501 r
= sd_event_default(&event
);
1503 return log_error_errno(r
, "Failed to get event loop: %m");
1505 r
= sd_bus_attach_event(bus
, event
, 0);
1507 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1509 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1511 return log_error_errno(r
, "Failed to parse machine specification: %m");
1513 match
= strjoina("type='signal',"
1514 "sender='org.freedesktop.machine1',"
1515 "path='/org/freedesktop/machine1',",
1516 "interface='org.freedesktop.machine1.Manager',"
1517 "member='MachineRemoved',"
1518 "arg0='", machine
, "'");
1520 r
= sd_bus_add_match(bus
, &slot
, match
, on_machine_removed
, &forward
);
1522 return log_error_errno(r
, "Failed to add machine removal match: %m");
1524 r
= sd_bus_message_new_method_call(
1527 "org.freedesktop.machine1",
1528 "/org/freedesktop/machine1",
1529 "org.freedesktop.machine1.Manager",
1530 "OpenMachineShell");
1532 return bus_log_create_error(r
);
1534 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1536 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1538 return bus_log_create_error(r
);
1540 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1542 return bus_log_create_error(r
);
1544 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1546 return bus_log_create_error(r
);
1548 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1550 log_error("Failed to get shell PTY: %s", bus_error_message(&error
, -r
));
1554 r
= sd_bus_message_read(reply
, "hs", &master
, &pty
);
1556 return bus_log_parse_error(r
);
1558 return process_forward(event
, &forward
, master
, 0, machine
);
1561 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1562 sd_bus
*bus
= userdata
;
1567 polkit_agent_open_if_enabled();
1569 for (i
= 1; i
< argc
; i
++) {
1570 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1571 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1573 r
= sd_bus_message_new_method_call(
1576 "org.freedesktop.machine1",
1577 "/org/freedesktop/machine1",
1578 "org.freedesktop.machine1.Manager",
1581 return bus_log_create_error(r
);
1583 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1585 return bus_log_create_error(r
);
1587 /* This is a slow operation, hence turn off any method call timeouts */
1588 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1590 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1596 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1597 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1598 sd_bus
*bus
= userdata
;
1601 polkit_agent_open_if_enabled();
1603 r
= sd_bus_call_method(
1605 "org.freedesktop.machine1",
1606 "/org/freedesktop/machine1",
1607 "org.freedesktop.machine1.Manager",
1611 "ss", argv
[1], argv
[2]);
1613 log_error("Could not rename image: %s", bus_error_message(&error
, -r
));
1620 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1621 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1622 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1623 sd_bus
*bus
= userdata
;
1626 polkit_agent_open_if_enabled();
1628 r
= sd_bus_message_new_method_call(
1631 "org.freedesktop.machine1",
1632 "/org/freedesktop/machine1",
1633 "org.freedesktop.machine1.Manager",
1636 return bus_log_create_error(r
);
1638 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1640 return bus_log_create_error(r
);
1642 /* This is a slow operation, hence turn off any method call timeouts */
1643 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1645 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1650 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1651 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1652 sd_bus
*bus
= userdata
;
1656 b
= parse_boolean(argv
[2]);
1658 log_error("Failed to parse boolean argument: %s", argv
[2]);
1663 polkit_agent_open_if_enabled();
1665 r
= sd_bus_call_method(
1667 "org.freedesktop.machine1",
1668 "/org/freedesktop/machine1",
1669 "org.freedesktop.machine1.Manager",
1670 "MarkImageReadOnly",
1675 log_error("Could not mark image read-only: %s", bus_error_message(&error
, -r
));
1682 static int image_exists(sd_bus
*bus
, const char *name
) {
1683 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1689 r
= sd_bus_call_method(
1691 "org.freedesktop.machine1",
1692 "/org/freedesktop/machine1",
1693 "org.freedesktop.machine1.Manager",
1699 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1702 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, -r
));
1708 static int make_service_name(const char *name
, char **ret
) {
1714 if (!machine_name_is_valid(name
)) {
1715 log_error("Invalid machine name %s.", name
);
1719 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1721 return log_error_errno(r
, "Failed to build unit name: %m");
1726 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1727 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1728 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1729 sd_bus
*bus
= userdata
;
1734 polkit_agent_open_if_enabled();
1736 r
= bus_wait_for_jobs_new(bus
, &w
);
1740 for (i
= 1; i
< argc
; i
++) {
1741 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1742 _cleanup_free_
char *unit
= NULL
;
1745 r
= make_service_name(argv
[i
], &unit
);
1749 r
= image_exists(bus
, argv
[i
]);
1753 log_error("Machine image '%s' does not exist.", argv
[1]);
1757 r
= sd_bus_call_method(
1759 "org.freedesktop.systemd1",
1760 "/org/freedesktop/systemd1",
1761 "org.freedesktop.systemd1.Manager",
1765 "ss", unit
, "fail");
1767 log_error("Failed to start unit: %s", bus_error_message(&error
, -r
));
1771 r
= sd_bus_message_read(reply
, "o", &object
);
1773 return bus_log_parse_error(r
);
1775 r
= bus_wait_for_jobs_add(w
, object
);
1780 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1787 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1788 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1789 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1790 UnitFileChange
*changes
= NULL
;
1791 unsigned n_changes
= 0;
1792 int carries_install_info
= 0;
1793 const char *method
= NULL
;
1794 sd_bus
*bus
= userdata
;
1799 polkit_agent_open_if_enabled();
1801 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1803 r
= sd_bus_message_new_method_call(
1806 "org.freedesktop.systemd1",
1807 "/org/freedesktop/systemd1",
1808 "org.freedesktop.systemd1.Manager",
1811 return bus_log_create_error(r
);
1813 r
= sd_bus_message_open_container(m
, 'a', "s");
1815 return bus_log_create_error(r
);
1817 for (i
= 1; i
< argc
; i
++) {
1818 _cleanup_free_
char *unit
= NULL
;
1820 r
= make_service_name(argv
[i
], &unit
);
1824 r
= image_exists(bus
, argv
[i
]);
1828 log_error("Machine image '%s' does not exist.", argv
[1]);
1832 r
= sd_bus_message_append(m
, "s", unit
);
1834 return bus_log_create_error(r
);
1837 r
= sd_bus_message_close_container(m
);
1839 return bus_log_create_error(r
);
1841 if (streq(argv
[0], "enable"))
1842 r
= sd_bus_message_append(m
, "bb", false, false);
1844 r
= sd_bus_message_append(m
, "b", false);
1846 return bus_log_create_error(r
);
1848 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1850 log_error("Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1854 if (streq(argv
[0], "enable")) {
1855 r
= sd_bus_message_read(reply
, "b", carries_install_info
);
1857 return bus_log_parse_error(r
);
1860 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1864 r
= sd_bus_call_method(
1866 "org.freedesktop.systemd1",
1867 "/org/freedesktop/systemd1",
1868 "org.freedesktop.systemd1.Manager",
1874 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1881 unit_file_changes_free(changes
, n_changes
);
1886 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1887 const char **our_path
= userdata
, *line
;
1894 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1896 bus_log_parse_error(r
);
1900 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1903 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1906 log_full(priority
, "%s", line
);
1910 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1911 const char **our_path
= userdata
, *path
, *result
;
1918 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1920 bus_log_parse_error(r
);
1924 if (!streq_ptr(*our_path
, path
))
1927 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1931 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1936 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1938 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1942 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1943 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1944 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1945 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1946 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1947 const char *path
= NULL
;
1954 polkit_agent_open_if_enabled();
1956 r
= sd_event_default(&event
);
1958 return log_error_errno(r
, "Failed to get event loop: %m");
1960 r
= sd_bus_attach_event(bus
, event
, 0);
1962 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1964 r
= sd_bus_add_match(
1968 "sender='org.freedesktop.import1',"
1969 "interface='org.freedesktop.import1.Manager',"
1970 "member='TransferRemoved',"
1971 "path='/org/freedesktop/import1'",
1972 match_transfer_removed
, &path
);
1974 return log_error_errno(r
, "Failed to install match: %m");
1976 r
= sd_bus_add_match(
1980 "sender='org.freedesktop.import1',"
1981 "interface='org.freedesktop.import1.Transfer',"
1982 "member='LogMessage'",
1983 match_log_message
, &path
);
1985 return log_error_errno(r
, "Failed to install match: %m");
1987 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1989 log_error("Failed to transfer image: %s", bus_error_message(&error
, -r
));
1993 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1995 return bus_log_parse_error(r
);
1997 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
2000 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
2002 sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
2003 sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
2005 r
= sd_event_loop(event
);
2007 return log_error_errno(r
, "Failed to run event loop: %m");
2012 static int import_tar(int argc
, char *argv
[], void *userdata
) {
2013 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2014 _cleanup_free_
char *ll
= NULL
;
2015 _cleanup_close_
int fd
= -1;
2016 const char *local
= NULL
, *path
= NULL
;
2017 sd_bus
*bus
= userdata
;
2024 if (isempty(path
) || streq(path
, "-"))
2030 local
= basename(path
);
2031 if (isempty(local
) || streq(local
, "-"))
2035 log_error("Need either path or local name.");
2039 r
= tar_strip_suffixes(local
, &ll
);
2045 if (!machine_name_is_valid(local
)) {
2046 log_error("Local name %s is not a suitable machine name.", local
);
2051 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2053 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2056 r
= sd_bus_message_new_method_call(
2059 "org.freedesktop.import1",
2060 "/org/freedesktop/import1",
2061 "org.freedesktop.import1.Manager",
2064 return bus_log_create_error(r
);
2066 r
= sd_bus_message_append(
2069 fd
>= 0 ? fd
: STDIN_FILENO
,
2074 return bus_log_create_error(r
);
2076 return transfer_image_common(bus
, m
);
2079 static int import_raw(int argc
, char *argv
[], void *userdata
) {
2080 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2081 _cleanup_free_
char *ll
= NULL
;
2082 _cleanup_close_
int fd
= -1;
2083 const char *local
= NULL
, *path
= NULL
;
2084 sd_bus
*bus
= userdata
;
2091 if (isempty(path
) || streq(path
, "-"))
2097 local
= basename(path
);
2098 if (isempty(local
) || streq(local
, "-"))
2102 log_error("Need either path or local name.");
2106 r
= raw_strip_suffixes(local
, &ll
);
2112 if (!machine_name_is_valid(local
)) {
2113 log_error("Local name %s is not a suitable machine name.", local
);
2118 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2120 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2123 r
= sd_bus_message_new_method_call(
2126 "org.freedesktop.import1",
2127 "/org/freedesktop/import1",
2128 "org.freedesktop.import1.Manager",
2131 return bus_log_create_error(r
);
2133 r
= sd_bus_message_append(
2136 fd
>= 0 ? fd
: STDIN_FILENO
,
2141 return bus_log_create_error(r
);
2143 return transfer_image_common(bus
, m
);
2146 static void determine_compression_from_filename(const char *p
) {
2153 if (endswith(p
, ".xz"))
2155 else if (endswith(p
, ".gz"))
2156 arg_format
= "gzip";
2157 else if (endswith(p
, ".bz2"))
2158 arg_format
= "bzip2";
2161 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2162 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2163 _cleanup_close_
int fd
= -1;
2164 const char *local
= NULL
, *path
= NULL
;
2165 sd_bus
*bus
= userdata
;
2171 if (!machine_name_is_valid(local
)) {
2172 log_error("Machine name %s is not valid.", local
);
2178 if (isempty(path
) || streq(path
, "-"))
2182 determine_compression_from_filename(path
);
2184 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2186 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2189 r
= sd_bus_message_new_method_call(
2192 "org.freedesktop.import1",
2193 "/org/freedesktop/import1",
2194 "org.freedesktop.import1.Manager",
2197 return bus_log_create_error(r
);
2199 r
= sd_bus_message_append(
2203 fd
>= 0 ? fd
: STDOUT_FILENO
,
2206 return bus_log_create_error(r
);
2208 return transfer_image_common(bus
, m
);
2211 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2212 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2213 _cleanup_close_
int fd
= -1;
2214 const char *local
= NULL
, *path
= NULL
;
2215 sd_bus
*bus
= userdata
;
2221 if (!machine_name_is_valid(local
)) {
2222 log_error("Machine name %s is not valid.", local
);
2228 if (isempty(path
) || streq(path
, "-"))
2232 determine_compression_from_filename(path
);
2234 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2236 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2239 r
= sd_bus_message_new_method_call(
2242 "org.freedesktop.import1",
2243 "/org/freedesktop/import1",
2244 "org.freedesktop.import1.Manager",
2247 return bus_log_create_error(r
);
2249 r
= sd_bus_message_append(
2253 fd
>= 0 ? fd
: STDOUT_FILENO
,
2256 return bus_log_create_error(r
);
2258 return transfer_image_common(bus
, m
);
2261 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2262 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2263 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2264 const char *local
, *remote
;
2265 sd_bus
*bus
= userdata
;
2271 if (!http_url_is_valid(remote
)) {
2272 log_error("URL '%s' is not valid.", remote
);
2279 r
= import_url_last_component(remote
, &l
);
2281 return log_error_errno(r
, "Failed to get final component of URL: %m");
2286 if (isempty(local
) || streq(local
, "-"))
2290 r
= tar_strip_suffixes(local
, &ll
);
2296 if (!machine_name_is_valid(local
)) {
2297 log_error("Local name %s is not a suitable machine name.", local
);
2302 r
= sd_bus_message_new_method_call(
2305 "org.freedesktop.import1",
2306 "/org/freedesktop/import1",
2307 "org.freedesktop.import1.Manager",
2310 return bus_log_create_error(r
);
2312 r
= sd_bus_message_append(
2317 import_verify_to_string(arg_verify
),
2320 return bus_log_create_error(r
);
2322 return transfer_image_common(bus
, m
);
2325 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2326 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2327 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2328 const char *local
, *remote
;
2329 sd_bus
*bus
= userdata
;
2335 if (!http_url_is_valid(remote
)) {
2336 log_error("URL '%s' is not valid.", remote
);
2343 r
= import_url_last_component(remote
, &l
);
2345 return log_error_errno(r
, "Failed to get final component of URL: %m");
2350 if (isempty(local
) || streq(local
, "-"))
2354 r
= raw_strip_suffixes(local
, &ll
);
2360 if (!machine_name_is_valid(local
)) {
2361 log_error("Local name %s is not a suitable machine name.", local
);
2366 r
= sd_bus_message_new_method_call(
2369 "org.freedesktop.import1",
2370 "/org/freedesktop/import1",
2371 "org.freedesktop.import1.Manager",
2374 return bus_log_create_error(r
);
2376 r
= sd_bus_message_append(
2381 import_verify_to_string(arg_verify
),
2384 return bus_log_create_error(r
);
2386 return transfer_image_common(bus
, m
);
2389 typedef struct TransferInfo
{
2397 static int compare_transfer_info(const void *a
, const void *b
) {
2398 const TransferInfo
*x
= a
, *y
= b
;
2400 return strcmp(x
->local
, y
->local
);
2403 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2404 size_t max_type
= strlen("TYPE"), max_local
= strlen("LOCAL"), max_remote
= strlen("REMOTE");
2405 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2406 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2407 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2408 size_t n_transfers
= 0, n_allocated
= 0, j
;
2409 const char *type
, *remote
, *local
, *object
;
2410 sd_bus
*bus
= userdata
;
2411 uint32_t id
, max_id
= 0;
2415 pager_open(arg_no_pager
, false);
2417 r
= sd_bus_call_method(bus
,
2418 "org.freedesktop.import1",
2419 "/org/freedesktop/import1",
2420 "org.freedesktop.import1.Manager",
2426 log_error("Could not get transfers: %s", bus_error_message(&error
, -r
));
2430 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2432 return bus_log_parse_error(r
);
2434 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, &object
)) > 0) {
2437 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2440 transfers
[n_transfers
].id
= id
;
2441 transfers
[n_transfers
].type
= type
;
2442 transfers
[n_transfers
].remote
= remote
;
2443 transfers
[n_transfers
].local
= local
;
2444 transfers
[n_transfers
].progress
= progress
;
2464 return bus_log_parse_error(r
);
2466 r
= sd_bus_message_exit_container(reply
);
2468 return bus_log_parse_error(r
);
2470 qsort_safe(transfers
, n_transfers
, sizeof(TransferInfo
), compare_transfer_info
);
2472 if (arg_legend
&& n_transfers
> 0)
2473 printf("%-*s %-*s %-*s %-*s %-*s\n",
2474 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2476 (int) max_type
, "TYPE",
2477 (int) max_local
, "LOCAL",
2478 (int) max_remote
, "REMOTE");
2480 for (j
= 0; j
< n_transfers
; j
++)
2481 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2482 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2483 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2484 (int) max_type
, transfers
[j
].type
,
2485 (int) max_local
, transfers
[j
].local
,
2486 (int) max_remote
, transfers
[j
].remote
);
2489 if (n_transfers
> 0)
2490 printf("\n%zu transfers listed.\n", n_transfers
);
2492 printf("No transfers.\n");
2498 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2499 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2500 sd_bus
*bus
= userdata
;
2505 polkit_agent_open_if_enabled();
2507 for (i
= 1; i
< argc
; i
++) {
2510 r
= safe_atou32(argv
[i
], &id
);
2512 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2514 r
= sd_bus_call_method(
2516 "org.freedesktop.import1",
2517 "/org/freedesktop/import1",
2518 "org.freedesktop.import1.Manager",
2524 log_error("Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2532 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2533 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2534 sd_bus
*bus
= userdata
;
2538 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2539 limit
= (uint64_t) -1;
2541 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2543 return log_error("Failed to parse size: %s", argv
[argc
-1]);
2547 /* With two arguments changes the quota limit of the
2548 * specified image */
2549 r
= sd_bus_call_method(
2551 "org.freedesktop.machine1",
2552 "/org/freedesktop/machine1",
2553 "org.freedesktop.machine1.Manager",
2557 "st", argv
[1], limit
);
2559 /* With one argument changes the pool quota limit */
2560 r
= sd_bus_call_method(
2562 "org.freedesktop.machine1",
2563 "/org/freedesktop/machine1",
2564 "org.freedesktop.machine1.Manager",
2571 log_error("Could not set limit: %s", bus_error_message(&error
, -r
));
2578 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2579 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2580 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2581 uint64_t usage
, total
= 0;
2582 char fb
[FORMAT_BYTES_MAX
];
2583 sd_bus
*bus
= userdata
;
2588 r
= sd_bus_message_new_method_call(
2591 "org.freedesktop.machine1",
2592 "/org/freedesktop/machine1",
2593 "org.freedesktop.machine1.Manager",
2596 return bus_log_create_error(r
);
2598 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2600 return bus_log_create_error(r
);
2602 /* This is a slow operation, hence permit a longer time for completion. */
2603 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2605 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2607 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2609 return bus_log_parse_error(r
);
2611 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2612 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2613 name
, format_bytes(fb
, sizeof(fb
), usage
));
2619 r
= sd_bus_message_exit_container(reply
);
2621 return bus_log_parse_error(r
);
2623 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2624 c
, format_bytes(fb
, sizeof(fb
), total
));
2629 static int help(int argc
, char *argv
[], void *userdata
) {
2630 pager_open(arg_no_pager
, false);
2632 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2633 "Send control commands to or query the virtual machine and container\n"
2634 "registration manager.\n\n"
2635 " -h --help Show this help\n"
2636 " --version Show package version\n"
2637 " --no-pager Do not pipe output into a pager\n"
2638 " --no-legend Do not show the headers and footers\n"
2639 " --no-ask-password Do not ask for system passwords\n"
2640 " -H --host=[USER@]HOST Operate on remote host\n"
2641 " -M --machine=CONTAINER Operate on local container\n"
2642 " -p --property=NAME Show only properties by this name\n"
2643 " -q --quiet Suppress output\n"
2644 " -a --all Show all properties, including empty ones\n"
2645 " --value When showing properties, only print the value\n"
2646 " -l --full Do not ellipsize output\n"
2647 " --kill-who=WHO Who to send signal to\n"
2648 " -s --signal=SIGNAL Which signal to send\n"
2649 " --uid=USER Specify user ID to invoke shell as\n"
2650 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2651 " --read-only Create read-only bind mount\n"
2652 " --mkdir Create directory before bind mounting, if missing\n"
2653 " -n --lines=INTEGER Number of journal entries to show\n"
2654 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2655 " -o --output=STRING Change journal output mode (short,\n"
2656 " short-monotonic, verbose, export, json,\n"
2657 " json-pretty, json-sse, cat)\n"
2658 " --verify=MODE Verification mode for downloaded images (no,\n"
2659 " checksum, signature)\n"
2660 " --force Download image even if already exists\n\n"
2661 "Machine Commands:\n"
2662 " list List running VMs and containers\n"
2663 " status NAME... Show VM/container details\n"
2664 " show [NAME...] Show properties of one or more VMs/containers\n"
2665 " start NAME... Start container as a service\n"
2666 " login [NAME] Get a login prompt in a container or on the\n"
2668 " shell [[USER@]NAME [COMMAND...]]\n"
2669 " Invoke a shell (or other command) in a container\n"
2670 " or on the local host\n"
2671 " enable NAME... Enable automatic container start at boot\n"
2672 " disable NAME... Disable automatic container start at boot\n"
2673 " poweroff NAME... Power off one or more containers\n"
2674 " reboot NAME... Reboot one or more containers\n"
2675 " terminate NAME... Terminate one or more VMs/containers\n"
2676 " kill NAME... Send signal to processes of a VM/container\n"
2677 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2678 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2679 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2681 " list-images Show available container and VM images\n"
2682 " image-status [NAME...] Show image details\n"
2683 " show-image [NAME...] Show properties of image\n"
2684 " clone NAME NAME Clone an image\n"
2685 " rename NAME NAME Rename an image\n"
2686 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2687 " remove NAME... Remove an image\n"
2688 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2689 " clean Remove hidden (or all) images\n\n"
2690 "Image Transfer Commands:\n"
2691 " pull-tar URL [NAME] Download a TAR container image\n"
2692 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2693 " import-tar FILE [NAME] Import a local TAR container image\n"
2694 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2695 " export-tar NAME [FILE] Export a TAR container image locally\n"
2696 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2697 " list-transfers Show list of downloads in progress\n"
2698 " cancel-transfer Cancel a download\n"
2699 , program_invocation_short_name
);
2704 static int parse_argv(int argc
, char *argv
[]) {
2707 ARG_VERSION
= 0x100,
2714 ARG_NO_ASK_PASSWORD
,
2722 static const struct option options
[] = {
2723 { "help", no_argument
, NULL
, 'h' },
2724 { "version", no_argument
, NULL
, ARG_VERSION
},
2725 { "property", required_argument
, NULL
, 'p' },
2726 { "all", no_argument
, NULL
, 'a' },
2727 { "value", no_argument
, NULL
, ARG_VALUE
},
2728 { "full", no_argument
, NULL
, 'l' },
2729 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2730 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2731 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2732 { "signal", required_argument
, NULL
, 's' },
2733 { "host", required_argument
, NULL
, 'H' },
2734 { "machine", required_argument
, NULL
, 'M' },
2735 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2736 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2737 { "quiet", no_argument
, NULL
, 'q' },
2738 { "lines", required_argument
, NULL
, 'n' },
2739 { "output", required_argument
, NULL
, 'o' },
2740 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2741 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2742 { "force", no_argument
, NULL
, ARG_FORCE
},
2743 { "format", required_argument
, NULL
, ARG_FORMAT
},
2744 { "uid", required_argument
, NULL
, ARG_UID
},
2745 { "setenv", required_argument
, NULL
, 'E' },
2746 { "max-addresses", required_argument
, NULL
, ARG_NUMBER_IPS
},
2750 bool reorder
= false;
2751 int c
, r
, shell
= -1;
2757 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2759 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2765 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2766 * non-option argument was discovered. */
2770 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2771 * for switches after the main verb. However, for "shell" we really don't want that, since we
2772 * want that switches specified after the machine name are passed to the program to execute,
2773 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2774 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2775 * non-option parameter. If it's the verb "shell" we remember its position and continue
2776 * processing options. In this case, as soon as we hit the next non-option argument we found
2777 * the machine name, and stop further processing. If the first non-option argument is any other
2778 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2782 /* If we already found the "shell" verb on the command line, and now found the next
2783 * non-option argument, then this is the machine name and we should stop processing
2784 * further arguments. */
2785 optind
--; /* don't process this argument, go one step back */
2788 if (streq(optarg
, "shell"))
2789 /* Remember the position of the "shell" verb, and continue processing normally. */
2794 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2795 * processing normally. */
2798 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2799 * at least once with a reset option index. Hence, let's reset the option index here,
2800 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2801 * likely already processed it), and the bump the option index so that we read the
2802 * intended argument again. */
2803 saved_optind
= optind
;
2805 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2806 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2812 return help(0, NULL
, NULL
);
2818 r
= strv_extend(&arg_property
, optarg
);
2822 /* If the user asked for a particular
2823 * property, show it to him, even if it is
2841 if (safe_atou(optarg
, &arg_lines
) < 0) {
2842 log_error("Failed to parse lines '%s'", optarg
);
2848 arg_output
= output_mode_from_string(optarg
);
2849 if (arg_output
< 0) {
2850 log_error("Unknown output '%s'.", optarg
);
2856 arg_no_pager
= true;
2864 arg_kill_who
= optarg
;
2868 arg_signal
= signal_from_string_try_harder(optarg
);
2869 if (arg_signal
< 0) {
2870 log_error("Failed to parse signal string %s.", optarg
);
2875 case ARG_NO_ASK_PASSWORD
:
2876 arg_ask_password
= false;
2880 arg_transport
= BUS_TRANSPORT_REMOTE
;
2885 arg_transport
= BUS_TRANSPORT_MACHINE
;
2890 arg_read_only
= true;
2902 arg_verify
= import_verify_from_string(optarg
);
2903 if (arg_verify
< 0) {
2904 log_error("Failed to parse --verify= setting: %s", optarg
);
2914 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2")) {
2915 log_error("Unknown format: %s", optarg
);
2919 arg_format
= optarg
;
2927 if (!env_assignment_is_valid(optarg
)) {
2928 log_error("Environment assignment invalid: %s", optarg
);
2932 r
= strv_extend(&arg_setenv
, optarg
);
2937 case ARG_NUMBER_IPS
:
2938 if (streq(optarg
, "all"))
2939 arg_addrs
= ALL_IP_ADDRESSES
;
2940 else if (safe_atoi(optarg
, &arg_addrs
) < 0) {
2941 log_error("Invalid number of IPs");
2943 } else if (arg_addrs
< 0) {
2944 log_error("Number of IPs cannot be negative");
2953 assert_not_reached("Unhandled option");
2962 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2963 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2965 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2968 for (i
= shell
; i
< optind
; i
++)
2969 argv
[i
] = argv
[i
+1];
2976 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2978 static const Verb verbs
[] = {
2979 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2980 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2981 { "list-images", VERB_ANY
, 1, 0, list_images
},
2982 { "status", 2, VERB_ANY
, 0, show_machine
},
2983 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2984 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2985 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
2986 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
2987 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
2988 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
2989 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
2990 { "kill", 2, VERB_ANY
, 0, kill_machine
},
2991 { "login", VERB_ANY
, 2, 0, login_machine
},
2992 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
2993 { "bind", 3, 4, 0, bind_mount
},
2994 { "copy-to", 3, 4, 0, copy_files
},
2995 { "copy-from", 3, 4, 0, copy_files
},
2996 { "remove", 2, VERB_ANY
, 0, remove_image
},
2997 { "rename", 3, 3, 0, rename_image
},
2998 { "clone", 3, 3, 0, clone_image
},
2999 { "read-only", 2, 3, 0, read_only_image
},
3000 { "start", 2, VERB_ANY
, 0, start_machine
},
3001 { "enable", 2, VERB_ANY
, 0, enable_machine
},
3002 { "disable", 2, VERB_ANY
, 0, enable_machine
},
3003 { "import-tar", 2, 3, 0, import_tar
},
3004 { "import-raw", 2, 3, 0, import_raw
},
3005 { "export-tar", 2, 3, 0, export_tar
},
3006 { "export-raw", 2, 3, 0, export_raw
},
3007 { "pull-tar", 2, 3, 0, pull_tar
},
3008 { "pull-raw", 2, 3, 0, pull_raw
},
3009 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
3010 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
3011 { "set-limit", 2, 3, 0, set_limit
},
3012 { "clean", VERB_ANY
, 1, 0, clean_images
},
3016 return dispatch_verb(argc
, argv
, verbs
, bus
);
3019 int main(int argc
, char*argv
[]) {
3023 setlocale(LC_ALL
, "");
3024 log_parse_environment();
3027 r
= parse_argv(argc
, argv
);
3031 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
3033 log_error_errno(r
, "Failed to create bus connection: %m");
3037 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
3039 r
= machinectl_main(argc
, argv
, bus
);
3042 sd_bus_flush_close_unref(bus
);
3044 polkit_agent_close();
3046 strv_free(arg_property
);
3047 strv_free(arg_setenv
);
3049 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;